1
2
3
4
5
6
7#define pr_fmt(fmt) "PCI: OF: " fmt
8
9#include <linux/irqdomain.h>
10#include <linux/kernel.h>
11#include <linux/pci.h>
12#include <linux/of.h>
13#include <linux/of_irq.h>
14#include <linux/of_address.h>
15#include <linux/of_pci.h>
16#include "pci.h"
17
18void pci_set_of_node(struct pci_dev *dev)
19{
20 if (!dev->bus->dev.of_node)
21 return;
22 dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
23 dev->devfn);
24}
25
26void pci_release_of_node(struct pci_dev *dev)
27{
28 of_node_put(dev->dev.of_node);
29 dev->dev.of_node = NULL;
30}
31
32void pci_set_bus_of_node(struct pci_bus *bus)
33{
34 struct device_node *node;
35
36 if (bus->self == NULL) {
37 node = pcibios_get_phb_of_node(bus);
38 } else {
39 node = of_node_get(bus->self->dev.of_node);
40 if (node && of_property_read_bool(node, "external-facing"))
41 bus->self->external_facing = true;
42 }
43 bus->dev.of_node = node;
44}
45
46void pci_release_bus_of_node(struct pci_bus *bus)
47{
48 of_node_put(bus->dev.of_node);
49 bus->dev.of_node = NULL;
50}
51
52struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
53{
54
55 if (WARN_ON(bus->self || bus->parent))
56 return NULL;
57
58
59
60
61
62
63 if (bus->bridge->of_node)
64 return of_node_get(bus->bridge->of_node);
65 if (bus->bridge->parent && bus->bridge->parent->of_node)
66 return of_node_get(bus->bridge->parent->of_node);
67 return NULL;
68}
69
70struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
71{
72#ifdef CONFIG_IRQ_DOMAIN
73 struct irq_domain *d;
74
75 if (!bus->dev.of_node)
76 return NULL;
77
78
79 d = of_msi_get_domain(&bus->dev, bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
80 if (d)
81 return d;
82
83
84
85
86
87 d = irq_find_matching_host(bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
88 if (d)
89 return d;
90
91 return irq_find_host(bus->dev.of_node);
92#else
93 return NULL;
94#endif
95}
96
97static inline int __of_pci_pci_compare(struct device_node *node,
98 unsigned int data)
99{
100 int devfn;
101
102 devfn = of_pci_get_devfn(node);
103 if (devfn < 0)
104 return 0;
105
106 return devfn == data;
107}
108
109struct device_node *of_pci_find_child_device(struct device_node *parent,
110 unsigned int devfn)
111{
112 struct device_node *node, *node2;
113
114 for_each_child_of_node(parent, node) {
115 if (__of_pci_pci_compare(node, devfn))
116 return node;
117
118
119
120
121
122 if (of_node_name_eq(node, "multifunc-device")) {
123 for_each_child_of_node(node, node2) {
124 if (__of_pci_pci_compare(node2, devfn)) {
125 of_node_put(node);
126 return node2;
127 }
128 }
129 }
130 }
131 return NULL;
132}
133EXPORT_SYMBOL_GPL(of_pci_find_child_device);
134
135
136
137
138
139
140
141
142
143
144int of_pci_get_devfn(struct device_node *np)
145{
146 u32 reg[5];
147 int error;
148
149 error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
150 if (error)
151 return error;
152
153 return (reg[0] >> 8) & 0xff;
154}
155EXPORT_SYMBOL_GPL(of_pci_get_devfn);
156
157
158
159
160
161
162
163
164int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
165{
166 u32 bus_range[2];
167 int error;
168
169 error = of_property_read_u32_array(node, "bus-range", bus_range,
170 ARRAY_SIZE(bus_range));
171 if (error)
172 return error;
173
174 res->name = node->name;
175 res->start = bus_range[0];
176 res->end = bus_range[1];
177 res->flags = IORESOURCE_BUS;
178
179 return 0;
180}
181EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
182
183
184
185
186
187
188
189
190
191
192int of_get_pci_domain_nr(struct device_node *node)
193{
194 u32 domain;
195 int error;
196
197 error = of_property_read_u32(node, "linux,pci-domain", &domain);
198 if (error)
199 return error;
200
201 return (u16)domain;
202}
203EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
204
205
206
207
208
209
210
211
212
213
214int of_pci_get_max_link_speed(struct device_node *node)
215{
216 u32 max_link_speed;
217
218 if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
219 max_link_speed > 4)
220 return -EINVAL;
221
222 return max_link_speed;
223}
224EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
225
226
227
228
229
230void of_pci_check_probe_only(void)
231{
232 u32 val;
233 int ret;
234
235 ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
236 if (ret) {
237 if (ret == -ENODATA || ret == -EOVERFLOW)
238 pr_warn("linux,pci-probe-only without valid value, ignoring\n");
239 return;
240 }
241
242 if (val)
243 pci_add_flags(PCI_PROBE_ONLY);
244 else
245 pci_clear_flags(PCI_PROBE_ONLY);
246
247 pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis");
248}
249EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
250
251#if defined(CONFIG_OF_ADDRESS)
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270int devm_of_pci_get_host_bridge_resources(struct device *dev,
271 unsigned char busno, unsigned char bus_max,
272 struct list_head *resources, resource_size_t *io_base)
273{
274 struct device_node *dev_node = dev->of_node;
275 struct resource *res, tmp_res;
276 struct resource *bus_range;
277 struct of_pci_range range;
278 struct of_pci_range_parser parser;
279 char range_type[4];
280 int err;
281
282 if (io_base)
283 *io_base = (resource_size_t)OF_BAD_ADDR;
284
285 bus_range = devm_kzalloc(dev, sizeof(*bus_range), GFP_KERNEL);
286 if (!bus_range)
287 return -ENOMEM;
288
289 dev_info(dev, "host bridge %pOF ranges:\n", dev_node);
290
291 err = of_pci_parse_bus_range(dev_node, bus_range);
292 if (err) {
293 bus_range->start = busno;
294 bus_range->end = bus_max;
295 bus_range->flags = IORESOURCE_BUS;
296 dev_info(dev, " No bus range found for %pOF, using %pR\n",
297 dev_node, bus_range);
298 } else {
299 if (bus_range->end > bus_range->start + bus_max)
300 bus_range->end = bus_range->start + bus_max;
301 }
302 pci_add_resource(resources, bus_range);
303
304
305 err = of_pci_range_parser_init(&parser, dev_node);
306 if (err)
307 goto failed;
308
309 dev_dbg(dev, "Parsing ranges property...\n");
310 for_each_of_pci_range(&parser, &range) {
311
312 if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
313 snprintf(range_type, 4, " IO");
314 else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
315 snprintf(range_type, 4, "MEM");
316 else
317 snprintf(range_type, 4, "err");
318 dev_info(dev, " %s %#010llx..%#010llx -> %#010llx\n",
319 range_type, range.cpu_addr,
320 range.cpu_addr + range.size - 1, range.pci_addr);
321
322
323
324
325
326 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
327 continue;
328
329 err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
330 if (err)
331 continue;
332
333 res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
334 if (!res) {
335 err = -ENOMEM;
336 goto failed;
337 }
338
339 if (resource_type(res) == IORESOURCE_IO) {
340 if (!io_base) {
341 dev_err(dev, "I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
342 dev_node);
343 err = -EINVAL;
344 goto failed;
345 }
346 if (*io_base != (resource_size_t)OF_BAD_ADDR)
347 dev_warn(dev, "More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
348 dev_node);
349 *io_base = range.cpu_addr;
350 }
351
352 pci_add_resource_offset(resources, res, res->start - range.pci_addr);
353 }
354
355 return 0;
356
357failed:
358 pci_free_resource_list(resources);
359 return err;
360}
361EXPORT_SYMBOL_GPL(devm_of_pci_get_host_bridge_resources);
362#endif
363
364#if IS_ENABLED(CONFIG_OF_IRQ)
365
366
367
368
369
370
371
372
373
374
375
376static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
377{
378 struct device_node *dn, *ppnode;
379 struct pci_dev *ppdev;
380 __be32 laddr[3];
381 u8 pin;
382 int rc;
383
384
385
386
387
388 dn = pci_device_to_OF_node(pdev);
389 if (dn) {
390 rc = of_irq_parse_one(dn, 0, out_irq);
391 if (!rc)
392 return rc;
393 }
394
395
396
397
398
399
400 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
401 if (rc != 0)
402 goto err;
403
404 if (pin == 0)
405 return -ENODEV;
406
407
408 for (;;) {
409
410 ppdev = pdev->bus->self;
411
412
413 if (ppdev == NULL) {
414 ppnode = pci_bus_to_OF_node(pdev->bus);
415
416
417 if (ppnode == NULL) {
418 rc = -EINVAL;
419 goto err;
420 }
421 } else {
422
423 ppnode = pci_device_to_OF_node(ppdev);
424 }
425
426
427
428
429
430
431
432
433
434
435
436
437
438 if (ppnode)
439 break;
440
441
442
443
444
445 pin = pci_swizzle_interrupt_pin(pdev, pin);
446 pdev = ppdev;
447 }
448
449 out_irq->np = ppnode;
450 out_irq->args_count = 1;
451 out_irq->args[0] = pin;
452 laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
453 laddr[1] = laddr[2] = cpu_to_be32(0);
454 rc = of_irq_parse_raw(laddr, out_irq);
455 if (rc)
456 goto err;
457 return 0;
458err:
459 if (rc == -ENOENT) {
460 dev_warn(&pdev->dev,
461 "%s: no interrupt-map found, INTx interrupts not available\n",
462 __func__);
463 pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n",
464 __func__);
465 } else {
466 dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc);
467 }
468 return rc;
469}
470
471
472
473
474
475
476
477
478
479
480
481int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin)
482{
483 struct of_phandle_args oirq;
484 int ret;
485
486 ret = of_irq_parse_pci(dev, &oirq);
487 if (ret)
488 return 0;
489
490 return irq_create_of_mapping(&oirq);
491}
492EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
493#endif
494
495int pci_parse_request_of_pci_ranges(struct device *dev,
496 struct list_head *resources,
497 struct resource **bus_range)
498{
499 int err, res_valid = 0;
500 resource_size_t iobase;
501 struct resource_entry *win, *tmp;
502
503 INIT_LIST_HEAD(resources);
504 err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, resources,
505 &iobase);
506 if (err)
507 return err;
508
509 err = devm_request_pci_bus_resources(dev, resources);
510 if (err)
511 goto out_release_res;
512
513 resource_list_for_each_entry_safe(win, tmp, resources) {
514 struct resource *res = win->res;
515
516 switch (resource_type(res)) {
517 case IORESOURCE_IO:
518 err = devm_pci_remap_iospace(dev, res, iobase);
519 if (err) {
520 dev_warn(dev, "error %d: failed to map resource %pR\n",
521 err, res);
522 resource_list_destroy_entry(win);
523 }
524 break;
525 case IORESOURCE_MEM:
526 res_valid |= !(res->flags & IORESOURCE_PREFETCH);
527 break;
528 case IORESOURCE_BUS:
529 if (bus_range)
530 *bus_range = res;
531 break;
532 }
533 }
534
535 if (res_valid)
536 return 0;
537
538 dev_err(dev, "non-prefetchable memory resource required\n");
539 err = -EINVAL;
540
541 out_release_res:
542 pci_free_resource_list(resources);
543 return err;
544}
545EXPORT_SYMBOL_GPL(pci_parse_request_of_pci_ranges);
546
547