powerpc/pci: Use of_irq_parse_and_map_pci() helper
[linux-2.6-block.git] / drivers / pci / of.c
CommitLineData
98d9f30c
BH
1/*
2 * PCI <-> OF mapping helpers
3 *
4 * Copyright 2011 IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
4670d610 11#define pr_fmt(fmt) "PCI: OF: " fmt
98d9f30c 12
b165e2b6 13#include <linux/irqdomain.h>
98d9f30c
BH
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/of.h>
c8d17588 17#include <linux/of_irq.h>
4670d610 18#include <linux/of_address.h>
98d9f30c
BH
19#include <linux/of_pci.h>
20#include "pci.h"
21
22void pci_set_of_node(struct pci_dev *dev)
23{
24 if (!dev->bus->dev.of_node)
25 return;
26 dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
27 dev->devfn);
28}
29
30void pci_release_of_node(struct pci_dev *dev)
31{
32 of_node_put(dev->dev.of_node);
33 dev->dev.of_node = NULL;
34}
35
36void pci_set_bus_of_node(struct pci_bus *bus)
37{
38 if (bus->self == NULL)
39 bus->dev.of_node = pcibios_get_phb_of_node(bus);
40 else
41 bus->dev.of_node = of_node_get(bus->self->dev.of_node);
42}
43
44void pci_release_bus_of_node(struct pci_bus *bus)
45{
46 of_node_put(bus->dev.of_node);
47 bus->dev.of_node = NULL;
48}
49
50struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
51{
52 /* This should only be called for PHBs */
53 if (WARN_ON(bus->self || bus->parent))
54 return NULL;
55
4670d610
RH
56 /*
57 * Look for a node pointer in either the intermediary device we
58 * create above the root bus or its own parent. Normally only
98d9f30c
BH
59 * the later is populated.
60 */
61 if (bus->bridge->of_node)
62 return of_node_get(bus->bridge->of_node);
69566dd8 63 if (bus->bridge->parent && bus->bridge->parent->of_node)
98d9f30c
BH
64 return of_node_get(bus->bridge->parent->of_node);
65 return NULL;
66}
b165e2b6
MZ
67
68struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
69{
70#ifdef CONFIG_IRQ_DOMAIN
b165e2b6
MZ
71 struct irq_domain *d;
72
73 if (!bus->dev.of_node)
74 return NULL;
75
76 /* Start looking for a phandle to an MSI controller. */
c8d17588
MZ
77 d = of_msi_get_domain(&bus->dev, bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
78 if (d)
79 return d;
471c931c
MZ
80
81 /*
82 * If we don't have an msi-parent property, look for a domain
83 * directly attached to the host bridge.
84 */
c8d17588 85 d = irq_find_matching_host(bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
b165e2b6
MZ
86 if (d)
87 return d;
88
c8d17588 89 return irq_find_host(bus->dev.of_node);
b165e2b6
MZ
90#else
91 return NULL;
92#endif
93}
4670d610
RH
94
95
96static inline int __of_pci_pci_compare(struct device_node *node,
97 unsigned int data)
98{
99 int devfn;
100
101 devfn = of_pci_get_devfn(node);
102 if (devfn < 0)
103 return 0;
104
105 return devfn == data;
106}
107
108struct device_node *of_pci_find_child_device(struct device_node *parent,
109 unsigned int devfn)
110{
111 struct device_node *node, *node2;
112
113 for_each_child_of_node(parent, node) {
114 if (__of_pci_pci_compare(node, devfn))
115 return node;
116 /*
117 * Some OFs create a parent node "multifunc-device" as
118 * a fake root for all functions of a multi-function
119 * device we go down them as well.
120 */
121 if (!strcmp(node->name, "multifunc-device")) {
122 for_each_child_of_node(node, node2) {
123 if (__of_pci_pci_compare(node2, devfn)) {
124 of_node_put(node);
125 return node2;
126 }
127 }
128 }
129 }
130 return NULL;
131}
132EXPORT_SYMBOL_GPL(of_pci_find_child_device);
133
134/**
135 * of_pci_get_devfn() - Get device and function numbers for a device node
136 * @np: device node
137 *
138 * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
139 * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
140 * and function numbers respectively. On error a negative error code is
141 * returned.
142 */
143int of_pci_get_devfn(struct device_node *np)
144{
145 u32 reg[5];
146 int error;
147
148 error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
149 if (error)
150 return error;
151
152 return (reg[0] >> 8) & 0xff;
153}
154EXPORT_SYMBOL_GPL(of_pci_get_devfn);
155
156/**
157 * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
158 * @node: device node
159 * @res: address to a struct resource to return the bus-range
160 *
161 * Returns 0 on success or a negative error-code on failure.
162 */
163int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
164{
165 u32 bus_range[2];
166 int error;
167
168 error = of_property_read_u32_array(node, "bus-range", bus_range,
169 ARRAY_SIZE(bus_range));
170 if (error)
171 return error;
172
173 res->name = node->name;
174 res->start = bus_range[0];
175 res->end = bus_range[1];
176 res->flags = IORESOURCE_BUS;
177
178 return 0;
179}
180EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
181
182/**
183 * This function will try to obtain the host bridge domain number by
184 * finding a property called "linux,pci-domain" of the given device node.
185 *
186 * @node: device tree node with the domain information
187 *
188 * Returns the associated domain number from DT in the range [0-0xffff], or
189 * a negative value if the required property is not found.
190 */
191int of_get_pci_domain_nr(struct device_node *node)
192{
193 u32 domain;
194 int error;
195
196 error = of_property_read_u32(node, "linux,pci-domain", &domain);
197 if (error)
198 return error;
199
200 return (u16)domain;
201}
202EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
203
204/**
205 * This function will try to find the limitation of link speed by finding
206 * a property called "max-link-speed" of the given device node.
207 *
208 * @node: device tree node with the max link speed information
209 *
210 * Returns the associated max link speed from DT, or a negative value if the
211 * required property is not found or is invalid.
212 */
213int of_pci_get_max_link_speed(struct device_node *node)
214{
215 u32 max_link_speed;
216
217 if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
218 max_link_speed > 4)
219 return -EINVAL;
220
221 return max_link_speed;
222}
223EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
224
225/**
226 * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
227 * is present and valid
228 */
229void of_pci_check_probe_only(void)
230{
231 u32 val;
232 int ret;
233
234 ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
235 if (ret) {
236 if (ret == -ENODATA || ret == -EOVERFLOW)
237 pr_warn("linux,pci-probe-only without valid value, ignoring\n");
238 return;
239 }
240
241 if (val)
242 pci_add_flags(PCI_PROBE_ONLY);
243 else
244 pci_clear_flags(PCI_PROBE_ONLY);
245
246 pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis");
247}
248EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
249
250#if defined(CONFIG_OF_ADDRESS)
251/**
252 * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT
253 * @dev: device node of the host bridge having the range property
254 * @busno: bus number associated with the bridge root bus
255 * @bus_max: maximum number of buses for this bridge
256 * @resources: list where the range of resources will be added after DT parsing
257 * @io_base: pointer to a variable that will contain on return the physical
258 * address for the start of the I/O range. Can be NULL if the caller doesn't
259 * expect I/O ranges to be present in the device tree.
260 *
261 * It is the caller's job to free the @resources list.
262 *
263 * This function will parse the "ranges" property of a PCI host bridge device
264 * node and setup the resource mapping based on its content. It is expected
265 * that the property conforms with the Power ePAPR document.
266 *
267 * It returns zero if the range parsing has been successful or a standard error
268 * value if it failed.
269 */
270int of_pci_get_host_bridge_resources(struct device_node *dev,
271 unsigned char busno, unsigned char bus_max,
272 struct list_head *resources, resource_size_t *io_base)
273{
274 struct resource_entry *window;
275 struct resource *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 = kzalloc(sizeof(*bus_range), GFP_KERNEL);
286 if (!bus_range)
287 return -ENOMEM;
288
289 pr_info("host bridge %pOF ranges:\n", dev);
290
291 err = of_pci_parse_bus_range(dev, bus_range);
292 if (err) {
293 bus_range->start = busno;
294 bus_range->end = bus_max;
295 bus_range->flags = IORESOURCE_BUS;
296 pr_info(" No bus range found for %pOF, using %pR\n",
297 dev, 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 /* Check for ranges property */
305 err = of_pci_range_parser_init(&parser, dev);
306 if (err)
307 goto parse_failed;
308
309 pr_debug("Parsing ranges property...\n");
310 for_each_of_pci_range(&parser, &range) {
311 /* Read next ranges element */
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 pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type,
319 range.cpu_addr, range.cpu_addr + range.size - 1,
320 range.pci_addr);
321
322 /*
323 * If we failed translation or got a zero-sized region
324 * then skip this range
325 */
326 if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
327 continue;
328
329 res = kzalloc(sizeof(struct resource), GFP_KERNEL);
330 if (!res) {
331 err = -ENOMEM;
332 goto parse_failed;
333 }
334
335 err = of_pci_range_to_resource(&range, dev, res);
336 if (err) {
337 kfree(res);
338 continue;
339 }
340
341 if (resource_type(res) == IORESOURCE_IO) {
342 if (!io_base) {
343 pr_err("I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
344 dev);
345 err = -EINVAL;
346 goto conversion_failed;
347 }
348 if (*io_base != (resource_size_t)OF_BAD_ADDR)
349 pr_warn("More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
350 dev);
351 *io_base = range.cpu_addr;
352 }
353
354 pci_add_resource_offset(resources, res, res->start - range.pci_addr);
355 }
356
357 return 0;
358
359conversion_failed:
360 kfree(res);
361parse_failed:
362 resource_list_for_each_entry(window, resources)
363 kfree(window->res);
364 pci_free_resource_list(resources);
365 return err;
366}
367EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
368#endif /* CONFIG_OF_ADDRESS */
369
370/**
371 * of_pci_map_rid - Translate a requester ID through a downstream mapping.
372 * @np: root complex device node.
373 * @rid: PCI requester ID to map.
374 * @map_name: property name of the map to use.
375 * @map_mask_name: optional property name of the mask to use.
376 * @target: optional pointer to a target device node.
377 * @id_out: optional pointer to receive the translated ID.
378 *
379 * Given a PCI requester ID, look up the appropriate implementation-defined
380 * platform ID and/or the target device which receives transactions on that
381 * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
382 * @id_out may be NULL if only the other is required. If @target points to
383 * a non-NULL device node pointer, only entries targeting that node will be
384 * matched; if it points to a NULL value, it will receive the device node of
385 * the first matching target phandle, with a reference held.
386 *
387 * Return: 0 on success or a standard error code on failure.
388 */
389int of_pci_map_rid(struct device_node *np, u32 rid,
390 const char *map_name, const char *map_mask_name,
391 struct device_node **target, u32 *id_out)
392{
393 u32 map_mask, masked_rid;
394 int map_len;
395 const __be32 *map = NULL;
396
397 if (!np || !map_name || (!target && !id_out))
398 return -EINVAL;
399
400 map = of_get_property(np, map_name, &map_len);
401 if (!map) {
402 if (target)
403 return -ENODEV;
404 /* Otherwise, no map implies no translation */
405 *id_out = rid;
406 return 0;
407 }
408
409 if (!map_len || map_len % (4 * sizeof(*map))) {
410 pr_err("%pOF: Error: Bad %s length: %d\n", np,
411 map_name, map_len);
412 return -EINVAL;
413 }
414
415 /* The default is to select all bits. */
416 map_mask = 0xffffffff;
417
418 /*
419 * Can be overridden by "{iommu,msi}-map-mask" property.
420 * If of_property_read_u32() fails, the default is used.
421 */
422 if (map_mask_name)
423 of_property_read_u32(np, map_mask_name, &map_mask);
424
425 masked_rid = map_mask & rid;
426 for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
427 struct device_node *phandle_node;
428 u32 rid_base = be32_to_cpup(map + 0);
429 u32 phandle = be32_to_cpup(map + 1);
430 u32 out_base = be32_to_cpup(map + 2);
431 u32 rid_len = be32_to_cpup(map + 3);
432
433 if (rid_base & ~map_mask) {
434 pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
435 np, map_name, map_name,
436 map_mask, rid_base);
437 return -EFAULT;
438 }
439
440 if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
441 continue;
442
443 phandle_node = of_find_node_by_phandle(phandle);
444 if (!phandle_node)
445 return -ENODEV;
446
447 if (target) {
448 if (*target)
449 of_node_put(phandle_node);
450 else
451 *target = phandle_node;
452
453 if (*target != phandle_node)
454 continue;
455 }
456
457 if (id_out)
458 *id_out = masked_rid - rid_base + out_base;
459
460 pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
461 np, map_name, map_mask, rid_base, out_base,
462 rid_len, rid, masked_rid - rid_base + out_base);
463 return 0;
464 }
465
466 pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
467 np, map_name, rid, target && *target ? *target : NULL);
468 return -EFAULT;
469}
470
471#if IS_ENABLED(CONFIG_OF_IRQ)
472/**
473 * of_irq_parse_pci - Resolve the interrupt for a PCI device
474 * @pdev: the device whose interrupt is to be resolved
475 * @out_irq: structure of_irq filled by this function
476 *
477 * This function resolves the PCI interrupt for a given PCI device. If a
478 * device-node exists for a given pci_dev, it will use normal OF tree
479 * walking. If not, it will implement standard swizzling and walk up the
480 * PCI tree until an device-node is found, at which point it will finish
481 * resolving using the OF tree walking.
482 */
483int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
484{
485 struct device_node *dn, *ppnode;
486 struct pci_dev *ppdev;
487 __be32 laddr[3];
488 u8 pin;
489 int rc;
490
491 /*
492 * Check if we have a device node, if yes, fallback to standard
493 * device tree parsing
494 */
495 dn = pci_device_to_OF_node(pdev);
496 if (dn) {
497 rc = of_irq_parse_one(dn, 0, out_irq);
498 if (!rc)
499 return rc;
500 }
501
502 /*
503 * Ok, we don't, time to have fun. Let's start by building up an
504 * interrupt spec. we assume #interrupt-cells is 1, which is standard
505 * for PCI. If you do different, then don't use that routine.
506 */
507 rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
508 if (rc != 0)
509 goto err;
510 /* No pin, exit with no error message. */
511 if (pin == 0)
512 return -ENODEV;
513
514 /* Now we walk up the PCI tree */
515 for (;;) {
516 /* Get the pci_dev of our parent */
517 ppdev = pdev->bus->self;
518
519 /* Ouch, it's a host bridge... */
520 if (ppdev == NULL) {
521 ppnode = pci_bus_to_OF_node(pdev->bus);
522
523 /* No node for host bridge ? give up */
524 if (ppnode == NULL) {
525 rc = -EINVAL;
526 goto err;
527 }
528 } else {
529 /* We found a P2P bridge, check if it has a node */
530 ppnode = pci_device_to_OF_node(ppdev);
531 }
532
533 /*
534 * Ok, we have found a parent with a device-node, hand over to
535 * the OF parsing code.
536 * We build a unit address from the linux device to be used for
537 * resolution. Note that we use the linux bus number which may
538 * not match your firmware bus numbering.
539 * Fortunately, in most cases, interrupt-map-mask doesn't
540 * include the bus number as part of the matching.
541 * You should still be careful about that though if you intend
542 * to rely on this function (you ship a firmware that doesn't
543 * create device nodes for all PCI devices).
544 */
545 if (ppnode)
546 break;
547
548 /*
549 * We can only get here if we hit a P2P bridge with no node;
550 * let's do standard swizzling and try again
551 */
552 pin = pci_swizzle_interrupt_pin(pdev, pin);
553 pdev = ppdev;
554 }
555
556 out_irq->np = ppnode;
557 out_irq->args_count = 1;
558 out_irq->args[0] = pin;
559 laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
560 laddr[1] = laddr[2] = cpu_to_be32(0);
561 rc = of_irq_parse_raw(laddr, out_irq);
562 if (rc)
563 goto err;
564 return 0;
565err:
566 if (rc == -ENOENT) {
567 dev_warn(&pdev->dev,
568 "%s: no interrupt-map found, INTx interrupts not available\n",
569 __func__);
570 pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n",
571 __func__);
572 } else {
573 dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc);
574 }
575 return rc;
576}
577EXPORT_SYMBOL_GPL(of_irq_parse_pci);
578
579/**
580 * of_irq_parse_and_map_pci() - Decode a PCI IRQ from the device tree and map to a VIRQ
581 * @dev: The PCI device needing an IRQ
582 * @slot: PCI slot number; passed when used as map_irq callback. Unused
583 * @pin: PCI IRQ pin number; passed when used as map_irq callback. Unused
584 *
585 * @slot and @pin are unused, but included in the function so that this
586 * function can be used directly as the map_irq callback to
587 * pci_assign_irq() and struct pci_host_bridge.map_irq pointer
588 */
589int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin)
590{
591 struct of_phandle_args oirq;
592 int ret;
593
594 ret = of_irq_parse_pci(dev, &oirq);
595 if (ret)
596 return 0; /* Proper return code 0 == NO_IRQ */
597
598 return irq_create_of_mapping(&oirq);
599}
600EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
601#endif /* CONFIG_OF_IRQ */