Merge branch 'pm-tools'
[firefly-linux-kernel-4.4.55.git] / drivers / of / of_pci.c
1 #include <linux/kernel.h>
2 #include <linux/export.h>
3 #include <linux/of.h>
4 #include <linux/of_address.h>
5 #include <linux/of_device.h>
6 #include <linux/of_pci.h>
7 #include <linux/slab.h>
8
9 static inline int __of_pci_pci_compare(struct device_node *node,
10                                        unsigned int data)
11 {
12         int devfn;
13
14         devfn = of_pci_get_devfn(node);
15         if (devfn < 0)
16                 return 0;
17
18         return devfn == data;
19 }
20
21 struct device_node *of_pci_find_child_device(struct device_node *parent,
22                                              unsigned int devfn)
23 {
24         struct device_node *node, *node2;
25
26         for_each_child_of_node(parent, node) {
27                 if (__of_pci_pci_compare(node, devfn))
28                         return node;
29                 /*
30                  * Some OFs create a parent node "multifunc-device" as
31                  * a fake root for all functions of a multi-function
32                  * device we go down them as well.
33                  */
34                 if (!strcmp(node->name, "multifunc-device")) {
35                         for_each_child_of_node(node, node2) {
36                                 if (__of_pci_pci_compare(node2, devfn)) {
37                                         of_node_put(node);
38                                         return node2;
39                                 }
40                         }
41                 }
42         }
43         return NULL;
44 }
45 EXPORT_SYMBOL_GPL(of_pci_find_child_device);
46
47 /**
48  * of_pci_get_devfn() - Get device and function numbers for a device node
49  * @np: device node
50  *
51  * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
52  * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
53  * and function numbers respectively. On error a negative error code is
54  * returned.
55  */
56 int of_pci_get_devfn(struct device_node *np)
57 {
58         unsigned int size;
59         const __be32 *reg;
60
61         reg = of_get_property(np, "reg", &size);
62
63         if (!reg || size < 5 * sizeof(__be32))
64                 return -EINVAL;
65
66         return (be32_to_cpup(reg) >> 8) & 0xff;
67 }
68 EXPORT_SYMBOL_GPL(of_pci_get_devfn);
69
70 /**
71  * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
72  * @node: device node
73  * @res: address to a struct resource to return the bus-range
74  *
75  * Returns 0 on success or a negative error-code on failure.
76  */
77 int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
78 {
79         const __be32 *values;
80         int len;
81
82         values = of_get_property(node, "bus-range", &len);
83         if (!values || len < sizeof(*values) * 2)
84                 return -EINVAL;
85
86         res->name = node->name;
87         res->start = be32_to_cpup(values++);
88         res->end = be32_to_cpup(values);
89         res->flags = IORESOURCE_BUS;
90
91         return 0;
92 }
93 EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
94
95 /**
96  * This function will try to obtain the host bridge domain number by
97  * finding a property called "linux,pci-domain" of the given device node.
98  *
99  * @node: device tree node with the domain information
100  *
101  * Returns the associated domain number from DT in the range [0-0xffff], or
102  * a negative value if the required property is not found.
103  */
104 int of_get_pci_domain_nr(struct device_node *node)
105 {
106         const __be32 *value;
107         int len;
108         u16 domain;
109
110         value = of_get_property(node, "linux,pci-domain", &len);
111         if (!value || len < sizeof(*value))
112                 return -EINVAL;
113
114         domain = (u16)be32_to_cpup(value);
115
116         return domain;
117 }
118 EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
119
120 #if defined(CONFIG_OF_ADDRESS)
121 /**
122  * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
123  * @dev: device node of the host bridge having the range property
124  * @busno: bus number associated with the bridge root bus
125  * @bus_max: maximum number of buses for this bridge
126  * @resources: list where the range of resources will be added after DT parsing
127  * @io_base: pointer to a variable that will contain on return the physical
128  * address for the start of the I/O range. Can be NULL if the caller doesn't
129  * expect IO ranges to be present in the device tree.
130  *
131  * It is the caller's job to free the @resources list.
132  *
133  * This function will parse the "ranges" property of a PCI host bridge device
134  * node and setup the resource mapping based on its content. It is expected
135  * that the property conforms with the Power ePAPR document.
136  *
137  * It returns zero if the range parsing has been successful or a standard error
138  * value if it failed.
139  */
140 int of_pci_get_host_bridge_resources(struct device_node *dev,
141                         unsigned char busno, unsigned char bus_max,
142                         struct list_head *resources, resource_size_t *io_base)
143 {
144         struct resource_entry *window;
145         struct resource *res;
146         struct resource *bus_range;
147         struct of_pci_range range;
148         struct of_pci_range_parser parser;
149         char range_type[4];
150         int err;
151
152         if (io_base)
153                 *io_base = (resource_size_t)OF_BAD_ADDR;
154
155         bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL);
156         if (!bus_range)
157                 return -ENOMEM;
158
159         pr_info("PCI host bridge %s ranges:\n", dev->full_name);
160
161         err = of_pci_parse_bus_range(dev, bus_range);
162         if (err) {
163                 bus_range->start = busno;
164                 bus_range->end = bus_max;
165                 bus_range->flags = IORESOURCE_BUS;
166                 pr_info("  No bus range found for %s, using %pR\n",
167                         dev->full_name, bus_range);
168         } else {
169                 if (bus_range->end > bus_range->start + bus_max)
170                         bus_range->end = bus_range->start + bus_max;
171         }
172         pci_add_resource(resources, bus_range);
173
174         /* Check for ranges property */
175         err = of_pci_range_parser_init(&parser, dev);
176         if (err)
177                 goto parse_failed;
178
179         pr_debug("Parsing ranges property...\n");
180         for_each_of_pci_range(&parser, &range) {
181                 /* Read next ranges element */
182                 if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
183                         snprintf(range_type, 4, " IO");
184                 else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
185                         snprintf(range_type, 4, "MEM");
186                 else
187                         snprintf(range_type, 4, "err");
188                 pr_info("  %s %#010llx..%#010llx -> %#010llx\n", range_type,
189                         range.cpu_addr, range.cpu_addr + range.size - 1,
190                         range.pci_addr);
191
192                 /*
193                  * If we failed translation or got a zero-sized region
194                  * then skip this range
195                  */
196                 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
197                         continue;
198
199                 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
200                 if (!res) {
201                         err = -ENOMEM;
202                         goto parse_failed;
203                 }
204
205                 err = of_pci_range_to_resource(&range, dev, res);
206                 if (err)
207                         goto conversion_failed;
208
209                 if (resource_type(res) == IORESOURCE_IO) {
210                         if (!io_base) {
211                                 pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n",
212                                         dev->full_name);
213                                 err = -EINVAL;
214                                 goto conversion_failed;
215                         }
216                         if (*io_base != (resource_size_t)OF_BAD_ADDR)
217                                 pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n",
218                                         dev->full_name);
219                         *io_base = range.cpu_addr;
220                 }
221
222                 pci_add_resource_offset(resources, res, res->start - range.pci_addr);
223         }
224
225         return 0;
226
227 conversion_failed:
228         kfree(res);
229 parse_failed:
230         resource_list_for_each_entry(window, resources)
231                 kfree(window->res);
232         pci_free_resource_list(resources);
233         return err;
234 }
235 EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
236 #endif /* CONFIG_OF_ADDRESS */
237
238 #ifdef CONFIG_PCI_MSI
239
240 static LIST_HEAD(of_pci_msi_chip_list);
241 static DEFINE_MUTEX(of_pci_msi_chip_mutex);
242
243 int of_pci_msi_chip_add(struct msi_controller *chip)
244 {
245         if (!of_property_read_bool(chip->of_node, "msi-controller"))
246                 return -EINVAL;
247
248         mutex_lock(&of_pci_msi_chip_mutex);
249         list_add(&chip->list, &of_pci_msi_chip_list);
250         mutex_unlock(&of_pci_msi_chip_mutex);
251
252         return 0;
253 }
254 EXPORT_SYMBOL_GPL(of_pci_msi_chip_add);
255
256 void of_pci_msi_chip_remove(struct msi_controller *chip)
257 {
258         mutex_lock(&of_pci_msi_chip_mutex);
259         list_del(&chip->list);
260         mutex_unlock(&of_pci_msi_chip_mutex);
261 }
262 EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove);
263
264 struct msi_controller *of_pci_find_msi_chip_by_node(struct device_node *of_node)
265 {
266         struct msi_controller *c;
267
268         mutex_lock(&of_pci_msi_chip_mutex);
269         list_for_each_entry(c, &of_pci_msi_chip_list, list) {
270                 if (c->of_node == of_node) {
271                         mutex_unlock(&of_pci_msi_chip_mutex);
272                         return c;
273                 }
274         }
275         mutex_unlock(&of_pci_msi_chip_mutex);
276
277         return NULL;
278 }
279 EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node);
280
281 #endif /* CONFIG_PCI_MSI */