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