]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/pci/host/pci-host-common.c
PCI: generic: Fix pci_remap_iospace() failure path
[karo-tx-linux.git] / drivers / pci / host / pci-host-common.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright (C) 2014 ARM Limited
15  *
16  * Author: Will Deacon <will.deacon@arm.com>
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_pci.h>
23 #include <linux/pci-ecam.h>
24 #include <linux/platform_device.h>
25
26 static int gen_pci_parse_request_of_pci_ranges(struct device *dev,
27                        struct list_head *resources, struct resource **bus_range)
28 {
29         int err, res_valid = 0;
30         struct device_node *np = dev->of_node;
31         resource_size_t iobase;
32         struct resource_entry *win, *tmp;
33
34         err = of_pci_get_host_bridge_resources(np, 0, 0xff, resources, &iobase);
35         if (err)
36                 return err;
37
38         err = devm_request_pci_bus_resources(dev, resources);
39         if (err)
40                 return err;
41
42         resource_list_for_each_entry_safe(win, tmp, resources) {
43                 struct resource *res = win->res;
44
45                 switch (resource_type(res)) {
46                 case IORESOURCE_IO:
47                         err = pci_remap_iospace(res, iobase);
48                         if (err) {
49                                 dev_warn(dev, "error %d: failed to map resource %pR\n",
50                                          err, res);
51                                 resource_list_destroy_entry(win);
52                         }
53                         break;
54                 case IORESOURCE_MEM:
55                         res_valid |= !(res->flags & IORESOURCE_PREFETCH);
56                         break;
57                 case IORESOURCE_BUS:
58                         *bus_range = res;
59                         break;
60                 }
61         }
62
63         if (res_valid)
64                 return 0;
65
66         dev_err(dev, "non-prefetchable memory resource required\n");
67         return -EINVAL;
68 }
69
70 static void gen_pci_unmap_cfg(void *ptr)
71 {
72         pci_ecam_free((struct pci_config_window *)ptr);
73 }
74
75 static struct pci_config_window *gen_pci_init(struct device *dev,
76                 struct list_head *resources, struct pci_ecam_ops *ops)
77 {
78         int err;
79         struct resource cfgres;
80         struct resource *bus_range = NULL;
81         struct pci_config_window *cfg;
82
83         /* Parse our PCI ranges and request their resources */
84         err = gen_pci_parse_request_of_pci_ranges(dev, resources, &bus_range);
85         if (err)
86                 goto err_out;
87
88         err = of_address_to_resource(dev->of_node, 0, &cfgres);
89         if (err) {
90                 dev_err(dev, "missing \"reg\" property\n");
91                 goto err_out;
92         }
93
94         cfg = pci_ecam_create(dev, &cfgres, bus_range, ops);
95         if (IS_ERR(cfg)) {
96                 err = PTR_ERR(cfg);
97                 goto err_out;
98         }
99
100         err = devm_add_action(dev, gen_pci_unmap_cfg, cfg);
101         if (err) {
102                 gen_pci_unmap_cfg(cfg);
103                 goto err_out;
104         }
105         return cfg;
106
107 err_out:
108         pci_free_resource_list(resources);
109         return ERR_PTR(err);
110 }
111
112 int pci_host_common_probe(struct platform_device *pdev,
113                           struct pci_ecam_ops *ops)
114 {
115         const char *type;
116         struct device *dev = &pdev->dev;
117         struct device_node *np = dev->of_node;
118         struct pci_bus *bus, *child;
119         struct pci_config_window *cfg;
120         struct list_head resources;
121
122         type = of_get_property(np, "device_type", NULL);
123         if (!type || strcmp(type, "pci")) {
124                 dev_err(dev, "invalid \"device_type\" %s\n", type);
125                 return -EINVAL;
126         }
127
128         of_pci_check_probe_only();
129
130         /* Parse and map our Configuration Space windows */
131         INIT_LIST_HEAD(&resources);
132         cfg = gen_pci_init(dev, &resources, ops);
133         if (IS_ERR(cfg))
134                 return PTR_ERR(cfg);
135
136         /* Do not reassign resources if probe only */
137         if (!pci_has_flag(PCI_PROBE_ONLY))
138                 pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
139
140         bus = pci_scan_root_bus(dev, cfg->busr.start, &ops->pci_ops, cfg,
141                                 &resources);
142         if (!bus) {
143                 dev_err(dev, "Scanning rootbus failed");
144                 return -ENODEV;
145         }
146
147         pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
148
149         /*
150          * We insert PCI resources into the iomem_resource and
151          * ioport_resource trees in either pci_bus_claim_resources()
152          * or pci_bus_assign_resources().
153          */
154         if (pci_has_flag(PCI_PROBE_ONLY)) {
155                 pci_bus_claim_resources(bus);
156         } else {
157                 pci_bus_size_bridges(bus);
158                 pci_bus_assign_resources(bus);
159
160                 list_for_each_entry(child, &bus->children, node)
161                         pcie_bus_configure_settings(child);
162         }
163
164         pci_bus_add_devices(bus);
165         return 0;
166 }
167
168 MODULE_DESCRIPTION("Generic PCI host driver common code");
169 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
170 MODULE_LICENSE("GPL v2");