1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) "OF: " fmt
3
4 #include <linux/device.h>
5 #include <linux/fwnode.h>
6 #include <linux/io.h>
7 #include <linux/ioport.h>
8 #include <linux/logic_pio.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/overflow.h>
12 #include <linux/pci.h>
13 #include <linux/pci_regs.h>
14 #include <linux/sizes.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 #include <linux/dma-direct.h> /* for bus_dma_region */
18
19 #include "of_private.h"
20
21 /* Max address size we deal with */
22 #define OF_MAX_ADDR_CELLS 4
23 #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS)
24 #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0)
25
26 /* Debug utility */
27 #ifdef DEBUG
of_dump_addr(const char * s,const __be32 * addr,int na)28 static void of_dump_addr(const char *s, const __be32 *addr, int na)
29 {
30 pr_debug("%s", s);
31 while (na--)
32 pr_cont(" %08x", be32_to_cpu(*(addr++)));
33 pr_cont("\n");
34 }
35 #else
of_dump_addr(const char * s,const __be32 * addr,int na)36 static void of_dump_addr(const char *s, const __be32 *addr, int na) { }
37 #endif
38
39 /* Callbacks for bus specific translators */
40 struct of_bus {
41 const char *name;
42 const char *addresses;
43 int (*match)(struct device_node *parent);
44 void (*count_cells)(struct device_node *child,
45 int *addrc, int *sizec);
46 u64 (*map)(__be32 *addr, const __be32 *range,
47 int na, int ns, int pna, int fna);
48 int (*translate)(__be32 *addr, u64 offset, int na);
49 int flag_cells;
50 unsigned int (*get_flags)(const __be32 *addr);
51 };
52
53 /*
54 * Default translator (generic bus)
55 */
56
of_bus_default_count_cells(struct device_node * dev,int * addrc,int * sizec)57 static void of_bus_default_count_cells(struct device_node *dev,
58 int *addrc, int *sizec)
59 {
60 if (addrc)
61 *addrc = of_n_addr_cells(dev);
62 if (sizec)
63 *sizec = of_n_size_cells(dev);
64 }
65
of_bus_default_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)66 static u64 of_bus_default_map(__be32 *addr, const __be32 *range,
67 int na, int ns, int pna, int fna)
68 {
69 u64 cp, s, da;
70
71 cp = of_read_number(range + fna, na - fna);
72 s = of_read_number(range + na + pna, ns);
73 da = of_read_number(addr + fna, na - fna);
74
75 pr_debug("default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
76
77 if (da < cp || da >= (cp + s))
78 return OF_BAD_ADDR;
79 return da - cp;
80 }
81
of_bus_default_translate(__be32 * addr,u64 offset,int na)82 static int of_bus_default_translate(__be32 *addr, u64 offset, int na)
83 {
84 u64 a = of_read_number(addr, na);
85 memset(addr, 0, na * 4);
86 a += offset;
87 if (na > 1)
88 addr[na - 2] = cpu_to_be32(a >> 32);
89 addr[na - 1] = cpu_to_be32(a & 0xffffffffu);
90
91 return 0;
92 }
93
of_bus_default_flags_get_flags(const __be32 * addr)94 static unsigned int of_bus_default_flags_get_flags(const __be32 *addr)
95 {
96 return of_read_number(addr, 1);
97 }
98
of_bus_default_get_flags(const __be32 * addr)99 static unsigned int of_bus_default_get_flags(const __be32 *addr)
100 {
101 return IORESOURCE_MEM;
102 }
103
of_bus_default_flags_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)104 static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na,
105 int ns, int pna, int fna)
106 {
107 /* Check that flags match */
108 if (*addr != *range)
109 return OF_BAD_ADDR;
110
111 return of_bus_default_map(addr, range, na, ns, pna, fna);
112 }
113
of_bus_default_flags_translate(__be32 * addr,u64 offset,int na)114 static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na)
115 {
116 /* Keep "flags" part (high cell) in translated address */
117 return of_bus_default_translate(addr + 1, offset, na - 1);
118 }
119
120 #ifdef CONFIG_PCI
of_bus_pci_get_flags(const __be32 * addr)121 static unsigned int of_bus_pci_get_flags(const __be32 *addr)
122 {
123 unsigned int flags = 0;
124 u32 w = be32_to_cpup(addr);
125
126 if (!IS_ENABLED(CONFIG_PCI))
127 return 0;
128
129 switch((w >> 24) & 0x03) {
130 case 0x01:
131 flags |= IORESOURCE_IO;
132 break;
133 case 0x02: /* 32 bits */
134 flags |= IORESOURCE_MEM;
135 break;
136
137 case 0x03: /* 64 bits */
138 flags |= IORESOURCE_MEM | IORESOURCE_MEM_64;
139 break;
140 }
141 if (w & 0x40000000)
142 flags |= IORESOURCE_PREFETCH;
143 return flags;
144 }
145
146 /*
147 * PCI bus specific translator
148 */
149
of_node_is_pcie(struct device_node * np)150 static bool of_node_is_pcie(struct device_node *np)
151 {
152 bool is_pcie = of_node_name_eq(np, "pcie");
153
154 if (is_pcie)
155 pr_warn_once("%pOF: Missing device_type\n", np);
156
157 return is_pcie;
158 }
159
of_bus_pci_match(struct device_node * np)160 static int of_bus_pci_match(struct device_node *np)
161 {
162 /*
163 * "pciex" is PCI Express
164 * "vci" is for the /chaos bridge on 1st-gen PCI powermacs
165 * "ht" is hypertransport
166 *
167 * If none of the device_type match, and that the node name is
168 * "pcie", accept the device as PCI (with a warning).
169 */
170 return of_node_is_type(np, "pci") || of_node_is_type(np, "pciex") ||
171 of_node_is_type(np, "vci") || of_node_is_type(np, "ht") ||
172 of_node_is_pcie(np);
173 }
174
of_bus_pci_count_cells(struct device_node * np,int * addrc,int * sizec)175 static void of_bus_pci_count_cells(struct device_node *np,
176 int *addrc, int *sizec)
177 {
178 if (addrc)
179 *addrc = 3;
180 if (sizec)
181 *sizec = 2;
182 }
183
of_bus_pci_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)184 static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns,
185 int pna, int fna)
186 {
187 unsigned int af, rf;
188
189 af = of_bus_pci_get_flags(addr);
190 rf = of_bus_pci_get_flags(range);
191
192 /* Check address type match */
193 if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO))
194 return OF_BAD_ADDR;
195
196 return of_bus_default_map(addr, range, na, ns, pna, fna);
197 }
198
199 #endif /* CONFIG_PCI */
200
__of_address_resource_bounds(struct resource * r,u64 start,u64 size)201 static int __of_address_resource_bounds(struct resource *r, u64 start, u64 size)
202 {
203 if (overflows_type(start, r->start))
204 return -EOVERFLOW;
205
206 r->start = start;
207
208 if (!size)
209 r->end = wrapping_sub(typeof(r->end), r->start, 1);
210 else if (size && check_add_overflow(r->start, size - 1, &r->end))
211 return -EOVERFLOW;
212
213 return 0;
214 }
215
216 /*
217 * of_pci_range_to_resource - Create a resource from an of_pci_range
218 * @range: the PCI range that describes the resource
219 * @np: device node where the range belongs to
220 * @res: pointer to a valid resource that will be updated to
221 * reflect the values contained in the range.
222 *
223 * Returns -EINVAL if the range cannot be converted to resource.
224 *
225 * Note that if the range is an IO range, the resource will be converted
226 * using pci_address_to_pio() which can fail if it is called too early or
227 * if the range cannot be matched to any host bridge IO space (our case here).
228 * To guard against that we try to register the IO range first.
229 * If that fails we know that pci_address_to_pio() will do too.
230 */
of_pci_range_to_resource(struct of_pci_range * range,struct device_node * np,struct resource * res)231 int of_pci_range_to_resource(struct of_pci_range *range,
232 struct device_node *np, struct resource *res)
233 {
234 u64 start;
235 int err;
236 res->flags = range->flags;
237 res->parent = res->child = res->sibling = NULL;
238 res->name = np->full_name;
239
240 if (res->flags & IORESOURCE_IO) {
241 unsigned long port;
242 err = pci_register_io_range(&np->fwnode, range->cpu_addr,
243 range->size);
244 if (err)
245 goto invalid_range;
246 port = pci_address_to_pio(range->cpu_addr);
247 if (port == (unsigned long)-1) {
248 err = -EINVAL;
249 goto invalid_range;
250 }
251 start = port;
252 } else {
253 start = range->cpu_addr;
254 }
255 return __of_address_resource_bounds(res, start, range->size);
256
257 invalid_range:
258 res->start = (resource_size_t)OF_BAD_ADDR;
259 res->end = (resource_size_t)OF_BAD_ADDR;
260 return err;
261 }
262 EXPORT_SYMBOL(of_pci_range_to_resource);
263
264 /*
265 * of_range_to_resource - Create a resource from a ranges entry
266 * @np: device node where the range belongs to
267 * @index: the 'ranges' index to convert to a resource
268 * @res: pointer to a valid resource that will be updated to
269 * reflect the values contained in the range.
270 *
271 * Returns -ENOENT if the entry is not found or -EOVERFLOW if the range
272 * cannot be converted to resource.
273 */
of_range_to_resource(struct device_node * np,int index,struct resource * res)274 int of_range_to_resource(struct device_node *np, int index, struct resource *res)
275 {
276 int ret, i = 0;
277 struct of_range_parser parser;
278 struct of_range range;
279
280 ret = of_range_parser_init(&parser, np);
281 if (ret)
282 return ret;
283
284 for_each_of_range(&parser, &range)
285 if (i++ == index)
286 return of_pci_range_to_resource(&range, np, res);
287
288 return -ENOENT;
289 }
290 EXPORT_SYMBOL(of_range_to_resource);
291
292 /*
293 * ISA bus specific translator
294 */
295
of_bus_isa_match(struct device_node * np)296 static int of_bus_isa_match(struct device_node *np)
297 {
298 return of_node_name_eq(np, "isa");
299 }
300
of_bus_isa_count_cells(struct device_node * child,int * addrc,int * sizec)301 static void of_bus_isa_count_cells(struct device_node *child,
302 int *addrc, int *sizec)
303 {
304 if (addrc)
305 *addrc = 2;
306 if (sizec)
307 *sizec = 1;
308 }
309
of_bus_isa_map(__be32 * addr,const __be32 * range,int na,int ns,int pna,int fna)310 static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns,
311 int pna, int fna)
312 {
313 /* Check address type match */
314 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
315 return OF_BAD_ADDR;
316
317 return of_bus_default_map(addr, range, na, ns, pna, fna);
318 }
319
of_bus_isa_get_flags(const __be32 * addr)320 static unsigned int of_bus_isa_get_flags(const __be32 *addr)
321 {
322 unsigned int flags = 0;
323 u32 w = be32_to_cpup(addr);
324
325 if (w & 1)
326 flags |= IORESOURCE_IO;
327 else
328 flags |= IORESOURCE_MEM;
329 return flags;
330 }
331
of_bus_default_flags_match(struct device_node * np)332 static int of_bus_default_flags_match(struct device_node *np)
333 {
334 return of_bus_n_addr_cells(np) == 3;
335 }
336
337 /*
338 * Array of bus specific translators
339 */
340
341 static struct of_bus of_busses[] = {
342 #ifdef CONFIG_PCI
343 /* PCI */
344 {
345 .name = "pci",
346 .addresses = "assigned-addresses",
347 .match = of_bus_pci_match,
348 .count_cells = of_bus_pci_count_cells,
349 .map = of_bus_pci_map,
350 .translate = of_bus_default_flags_translate,
351 .flag_cells = 1,
352 .get_flags = of_bus_pci_get_flags,
353 },
354 #endif /* CONFIG_PCI */
355 /* ISA */
356 {
357 .name = "isa",
358 .addresses = "reg",
359 .match = of_bus_isa_match,
360 .count_cells = of_bus_isa_count_cells,
361 .map = of_bus_isa_map,
362 .translate = of_bus_default_flags_translate,
363 .flag_cells = 1,
364 .get_flags = of_bus_isa_get_flags,
365 },
366 /* Default with flags cell */
367 {
368 .name = "default-flags",
369 .addresses = "reg",
370 .match = of_bus_default_flags_match,
371 .count_cells = of_bus_default_count_cells,
372 .map = of_bus_default_flags_map,
373 .translate = of_bus_default_flags_translate,
374 .flag_cells = 1,
375 .get_flags = of_bus_default_flags_get_flags,
376 },
377 /* Default */
378 {
379 .name = "default",
380 .addresses = "reg",
381 .match = NULL,
382 .count_cells = of_bus_default_count_cells,
383 .map = of_bus_default_map,
384 .translate = of_bus_default_translate,
385 .get_flags = of_bus_default_get_flags,
386 },
387 };
388
of_match_bus(struct device_node * np)389 static struct of_bus *of_match_bus(struct device_node *np)
390 {
391 int i;
392
393 for (i = 0; i < ARRAY_SIZE(of_busses); i++)
394 if (!of_busses[i].match || of_busses[i].match(np))
395 return &of_busses[i];
396 BUG();
397 return NULL;
398 }
399
of_empty_ranges_quirk(struct device_node * np)400 static int of_empty_ranges_quirk(struct device_node *np)
401 {
402 if (IS_ENABLED(CONFIG_PPC)) {
403 /* To save cycles, we cache the result for global "Mac" setting */
404 static int quirk_state = -1;
405
406 /* PA-SEMI sdc DT bug */
407 if (of_device_is_compatible(np, "1682m-sdc"))
408 return true;
409
410 /* Make quirk cached */
411 if (quirk_state < 0)
412 quirk_state =
413 of_machine_is_compatible("Power Macintosh") ||
414 of_machine_is_compatible("MacRISC");
415 return quirk_state;
416 }
417 return false;
418 }
419
of_translate_one(struct device_node * parent,struct of_bus * bus,struct of_bus * pbus,__be32 * addr,int na,int ns,int pna,const char * rprop)420 static int of_translate_one(struct device_node *parent, struct of_bus *bus,
421 struct of_bus *pbus, __be32 *addr,
422 int na, int ns, int pna, const char *rprop)
423 {
424 const __be32 *ranges;
425 unsigned int rlen;
426 int rone;
427 u64 offset = OF_BAD_ADDR;
428
429 /*
430 * Normally, an absence of a "ranges" property means we are
431 * crossing a non-translatable boundary, and thus the addresses
432 * below the current cannot be converted to CPU physical ones.
433 * Unfortunately, while this is very clear in the spec, it's not
434 * what Apple understood, and they do have things like /uni-n or
435 * /ht nodes with no "ranges" property and a lot of perfectly
436 * useable mapped devices below them. Thus we treat the absence of
437 * "ranges" as equivalent to an empty "ranges" property which means
438 * a 1:1 translation at that level. It's up to the caller not to try
439 * to translate addresses that aren't supposed to be translated in
440 * the first place. --BenH.
441 *
442 * As far as we know, this damage only exists on Apple machines, so
443 * This code is only enabled on powerpc. --gcl
444 *
445 * This quirk also applies for 'dma-ranges' which frequently exist in
446 * child nodes without 'dma-ranges' in the parent nodes. --RobH
447 */
448 ranges = of_get_property(parent, rprop, &rlen);
449 if (ranges == NULL && !of_empty_ranges_quirk(parent) &&
450 strcmp(rprop, "dma-ranges")) {
451 pr_debug("no ranges; cannot translate\n");
452 return 1;
453 }
454 if (ranges == NULL || rlen == 0) {
455 offset = of_read_number(addr, na);
456 /* set address to zero, pass flags through */
457 memset(addr + pbus->flag_cells, 0, (pna - pbus->flag_cells) * 4);
458 pr_debug("empty ranges; 1:1 translation\n");
459 goto finish;
460 }
461
462 pr_debug("walking ranges...\n");
463
464 /* Now walk through the ranges */
465 rlen /= 4;
466 rone = na + pna + ns;
467 for (; rlen >= rone; rlen -= rone, ranges += rone) {
468 offset = bus->map(addr, ranges, na, ns, pna, bus->flag_cells);
469 if (offset != OF_BAD_ADDR)
470 break;
471 }
472 if (offset == OF_BAD_ADDR) {
473 pr_debug("not found !\n");
474 return 1;
475 }
476 memcpy(addr, ranges + na, 4 * pna);
477
478 finish:
479 of_dump_addr("parent translation for:", addr, pna);
480 pr_debug("with offset: %llx\n", offset);
481
482 /* Translate it into parent bus space */
483 return pbus->translate(addr, offset, pna);
484 }
485
486 /*
487 * Translate an address from the device-tree into a CPU physical address,
488 * this walks up the tree and applies the various bus mappings on the
489 * way.
490 *
491 * Note: We consider that crossing any level with #size-cells == 0 to mean
492 * that translation is impossible (that is we are not dealing with a value
493 * that can be mapped to a cpu physical address). This is not really specified
494 * that way, but this is traditionally the way IBM at least do things
495 *
496 * Whenever the translation fails, the *host pointer will be set to the
497 * device that had registered logical PIO mapping, and the return code is
498 * relative to that node.
499 */
__of_translate_address(struct device_node * node,struct device_node * (* get_parent)(const struct device_node *),const __be32 * in_addr,const char * rprop,struct device_node ** host)500 static u64 __of_translate_address(struct device_node *node,
501 struct device_node *(*get_parent)(const struct device_node *),
502 const __be32 *in_addr, const char *rprop,
503 struct device_node **host)
504 {
505 struct device_node *dev __free(device_node) = of_node_get(node);
506 struct device_node *parent __free(device_node) = get_parent(dev);
507 struct of_bus *bus, *pbus;
508 __be32 addr[OF_MAX_ADDR_CELLS];
509 int na, ns, pna, pns;
510
511 pr_debug("** translation for device %pOF **\n", dev);
512
513 *host = NULL;
514
515 if (parent == NULL)
516 return OF_BAD_ADDR;
517 bus = of_match_bus(parent);
518
519 /* Count address cells & copy address locally */
520 bus->count_cells(dev, &na, &ns);
521 if (!OF_CHECK_COUNTS(na, ns)) {
522 pr_debug("Bad cell count for %pOF\n", dev);
523 return OF_BAD_ADDR;
524 }
525 memcpy(addr, in_addr, na * 4);
526
527 pr_debug("bus is %s (na=%d, ns=%d) on %pOF\n",
528 bus->name, na, ns, parent);
529 of_dump_addr("translating address:", addr, na);
530
531 /* Translate */
532 for (;;) {
533 struct logic_pio_hwaddr *iorange;
534
535 /* Switch to parent bus */
536 of_node_put(dev);
537 dev = parent;
538 parent = get_parent(dev);
539
540 /* If root, we have finished */
541 if (parent == NULL) {
542 pr_debug("reached root node\n");
543 return of_read_number(addr, na);
544 }
545
546 /*
547 * For indirectIO device which has no ranges property, get
548 * the address from reg directly.
549 */
550 iorange = find_io_range_by_fwnode(&dev->fwnode);
551 if (iorange && (iorange->flags != LOGIC_PIO_CPU_MMIO)) {
552 u64 result = of_read_number(addr + 1, na - 1);
553 pr_debug("indirectIO matched(%pOF) 0x%llx\n",
554 dev, result);
555 *host = no_free_ptr(dev);
556 return result;
557 }
558
559 /* Get new parent bus and counts */
560 pbus = of_match_bus(parent);
561 pbus->count_cells(dev, &pna, &pns);
562 if (!OF_CHECK_COUNTS(pna, pns)) {
563 pr_err("Bad cell count for %pOF\n", dev);
564 return OF_BAD_ADDR;
565 }
566
567 pr_debug("parent bus is %s (na=%d, ns=%d) on %pOF\n",
568 pbus->name, pna, pns, parent);
569
570 /* Apply bus translation */
571 if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop))
572 return OF_BAD_ADDR;
573
574 /* Complete the move up one level */
575 na = pna;
576 ns = pns;
577 bus = pbus;
578
579 of_dump_addr("one level translation:", addr, na);
580 }
581
582 unreachable();
583 }
584
of_translate_address(struct device_node * dev,const __be32 * in_addr)585 u64 of_translate_address(struct device_node *dev, const __be32 *in_addr)
586 {
587 struct device_node *host;
588 u64 ret;
589
590 ret = __of_translate_address(dev, of_get_parent,
591 in_addr, "ranges", &host);
592 if (host) {
593 of_node_put(host);
594 return OF_BAD_ADDR;
595 }
596
597 return ret;
598 }
599 EXPORT_SYMBOL(of_translate_address);
600
601 #ifdef CONFIG_HAS_DMA
__of_get_dma_parent(const struct device_node * np)602 struct device_node *__of_get_dma_parent(const struct device_node *np)
603 {
604 struct of_phandle_args args;
605 int ret, index;
606
607 index = of_property_match_string(np, "interconnect-names", "dma-mem");
608 if (index < 0)
609 return of_get_parent(np);
610
611 ret = of_parse_phandle_with_args(np, "interconnects",
612 "#interconnect-cells",
613 index, &args);
614 if (ret < 0)
615 return of_get_parent(np);
616
617 return args.np;
618 }
619 #endif
620
of_get_next_dma_parent(struct device_node * np)621 static struct device_node *of_get_next_dma_parent(struct device_node *np)
622 {
623 struct device_node *parent;
624
625 parent = __of_get_dma_parent(np);
626 of_node_put(np);
627
628 return parent;
629 }
630
of_translate_dma_address(struct device_node * dev,const __be32 * in_addr)631 u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr)
632 {
633 struct device_node *host;
634 u64 ret;
635
636 ret = __of_translate_address(dev, __of_get_dma_parent,
637 in_addr, "dma-ranges", &host);
638
639 if (host) {
640 of_node_put(host);
641 return OF_BAD_ADDR;
642 }
643
644 return ret;
645 }
646 EXPORT_SYMBOL(of_translate_dma_address);
647
648 /**
649 * of_translate_dma_region - Translate device tree address and size tuple
650 * @dev: device tree node for which to translate
651 * @prop: pointer into array of cells
652 * @start: return value for the start of the DMA range
653 * @length: return value for the length of the DMA range
654 *
655 * Returns a pointer to the cell immediately following the translated DMA region.
656 */
of_translate_dma_region(struct device_node * dev,const __be32 * prop,phys_addr_t * start,size_t * length)657 const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *prop,
658 phys_addr_t *start, size_t *length)
659 {
660 struct device_node *parent __free(device_node) = __of_get_dma_parent(dev);
661 u64 address, size;
662 int na, ns;
663
664 if (!parent)
665 return NULL;
666
667 na = of_bus_n_addr_cells(parent);
668 ns = of_bus_n_size_cells(parent);
669
670 address = of_translate_dma_address(dev, prop);
671 if (address == OF_BAD_ADDR)
672 return NULL;
673
674 size = of_read_number(prop + na, ns);
675
676 if (start)
677 *start = address;
678
679 if (length)
680 *length = size;
681
682 return prop + na + ns;
683 }
684 EXPORT_SYMBOL(of_translate_dma_region);
685
__of_get_address(struct device_node * dev,int index,int bar_no,u64 * size,unsigned int * flags)686 const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,
687 u64 *size, unsigned int *flags)
688 {
689 const __be32 *prop;
690 unsigned int psize;
691 struct device_node *parent __free(device_node) = of_get_parent(dev);
692 struct of_bus *bus;
693 int onesize, i, na, ns;
694
695 if (parent == NULL)
696 return NULL;
697
698 /* match the parent's bus type */
699 bus = of_match_bus(parent);
700 if (strcmp(bus->name, "pci") && (bar_no >= 0))
701 return NULL;
702
703 bus->count_cells(dev, &na, &ns);
704 if (!OF_CHECK_ADDR_COUNT(na))
705 return NULL;
706
707 /* Get "reg" or "assigned-addresses" property */
708 prop = of_get_property(dev, bus->addresses, &psize);
709 if (prop == NULL)
710 return NULL;
711 psize /= 4;
712
713 onesize = na + ns;
714 for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) {
715 u32 val = be32_to_cpu(prop[0]);
716 /* PCI bus matches on BAR number instead of index */
717 if (((bar_no >= 0) && ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))) ||
718 ((index >= 0) && (i == index))) {
719 if (size)
720 *size = of_read_number(prop + na, ns);
721 if (flags)
722 *flags = bus->get_flags(prop);
723 return prop;
724 }
725 }
726 return NULL;
727 }
728 EXPORT_SYMBOL(__of_get_address);
729
730 /**
731 * of_property_read_reg - Retrieve the specified "reg" entry index without translating
732 * @np: device tree node for which to retrieve "reg" from
733 * @idx: "reg" entry index to read
734 * @addr: return value for the untranslated address
735 * @size: return value for the entry size
736 *
737 * Returns -EINVAL if "reg" is not found. Returns 0 on success with addr and
738 * size values filled in.
739 */
of_property_read_reg(struct device_node * np,int idx,u64 * addr,u64 * size)740 int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)
741 {
742 const __be32 *prop = of_get_address(np, idx, size, NULL);
743
744 if (!prop)
745 return -EINVAL;
746
747 *addr = of_read_number(prop, of_n_addr_cells(np));
748
749 return 0;
750 }
751 EXPORT_SYMBOL(of_property_read_reg);
752
parser_init(struct of_pci_range_parser * parser,struct device_node * node,const char * name)753 static int parser_init(struct of_pci_range_parser *parser,
754 struct device_node *node, const char *name)
755 {
756 int rlen;
757
758 parser->node = node;
759 parser->pna = of_n_addr_cells(node);
760 parser->na = of_bus_n_addr_cells(node);
761 parser->ns = of_bus_n_size_cells(node);
762 parser->dma = !strcmp(name, "dma-ranges");
763 parser->bus = of_match_bus(node);
764
765 parser->range = of_get_property(node, name, &rlen);
766 if (parser->range == NULL)
767 return -ENOENT;
768
769 parser->end = parser->range + rlen / sizeof(__be32);
770
771 return 0;
772 }
773
of_pci_range_parser_init(struct of_pci_range_parser * parser,struct device_node * node)774 int of_pci_range_parser_init(struct of_pci_range_parser *parser,
775 struct device_node *node)
776 {
777 return parser_init(parser, node, "ranges");
778 }
779 EXPORT_SYMBOL_GPL(of_pci_range_parser_init);
780
of_pci_dma_range_parser_init(struct of_pci_range_parser * parser,struct device_node * node)781 int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
782 struct device_node *node)
783 {
784 return parser_init(parser, node, "dma-ranges");
785 }
786 EXPORT_SYMBOL_GPL(of_pci_dma_range_parser_init);
787 #define of_dma_range_parser_init of_pci_dma_range_parser_init
788
of_pci_range_parser_one(struct of_pci_range_parser * parser,struct of_pci_range * range)789 struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser,
790 struct of_pci_range *range)
791 {
792 int na = parser->na;
793 int ns = parser->ns;
794 int np = parser->pna + na + ns;
795 int busflag_na = parser->bus->flag_cells;
796
797 if (!range)
798 return NULL;
799
800 if (!parser->range || parser->range + np > parser->end)
801 return NULL;
802
803 range->flags = parser->bus->get_flags(parser->range);
804
805 range->bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
806
807 if (parser->dma)
808 range->cpu_addr = of_translate_dma_address(parser->node,
809 parser->range + na);
810 else
811 range->cpu_addr = of_translate_address(parser->node,
812 parser->range + na);
813 range->size = of_read_number(parser->range + parser->pna + na, ns);
814
815 parser->range += np;
816
817 /* Now consume following elements while they are contiguous */
818 while (parser->range + np <= parser->end) {
819 u32 flags = 0;
820 u64 bus_addr, cpu_addr, size;
821
822 flags = parser->bus->get_flags(parser->range);
823 bus_addr = of_read_number(parser->range + busflag_na, na - busflag_na);
824 if (parser->dma)
825 cpu_addr = of_translate_dma_address(parser->node,
826 parser->range + na);
827 else
828 cpu_addr = of_translate_address(parser->node,
829 parser->range + na);
830 size = of_read_number(parser->range + parser->pna + na, ns);
831
832 if (flags != range->flags)
833 break;
834 if (bus_addr != range->bus_addr + range->size ||
835 cpu_addr != range->cpu_addr + range->size)
836 break;
837
838 range->size += size;
839 parser->range += np;
840 }
841
842 return range;
843 }
844 EXPORT_SYMBOL_GPL(of_pci_range_parser_one);
845
of_translate_ioport(struct device_node * dev,const __be32 * in_addr,u64 size)846 static u64 of_translate_ioport(struct device_node *dev, const __be32 *in_addr,
847 u64 size)
848 {
849 u64 taddr;
850 unsigned long port;
851 struct device_node *host;
852
853 taddr = __of_translate_address(dev, of_get_parent,
854 in_addr, "ranges", &host);
855 if (host) {
856 /* host-specific port access */
857 port = logic_pio_trans_hwaddr(&host->fwnode, taddr, size);
858 of_node_put(host);
859 } else {
860 /* memory-mapped I/O range */
861 port = pci_address_to_pio(taddr);
862 }
863
864 if (port == (unsigned long)-1)
865 return OF_BAD_ADDR;
866
867 return port;
868 }
869
870 #ifdef CONFIG_HAS_DMA
871 /**
872 * of_dma_get_range - Get DMA range info and put it into a map array
873 * @np: device node to get DMA range info
874 * @map: dma range structure to return
875 *
876 * Look in bottom up direction for the first "dma-ranges" property
877 * and parse it. Put the information into a DMA offset map array.
878 *
879 * dma-ranges format:
880 * DMA addr (dma_addr) : naddr cells
881 * CPU addr (phys_addr_t) : pna cells
882 * size : nsize cells
883 *
884 * It returns -ENODEV if "dma-ranges" property was not found for this
885 * device in the DT.
886 */
of_dma_get_range(struct device_node * np,const struct bus_dma_region ** map)887 int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
888 {
889 struct device_node *node __free(device_node) = of_node_get(np);
890 const __be32 *ranges = NULL;
891 bool found_dma_ranges = false;
892 struct of_range_parser parser;
893 struct of_range range;
894 struct bus_dma_region *r;
895 int len, num_ranges = 0;
896
897 while (node) {
898 ranges = of_get_property(node, "dma-ranges", &len);
899
900 /* Ignore empty ranges, they imply no translation required */
901 if (ranges && len > 0)
902 break;
903
904 /* Once we find 'dma-ranges', then a missing one is an error */
905 if (found_dma_ranges && !ranges)
906 return -ENODEV;
907
908 found_dma_ranges = true;
909
910 node = of_get_next_dma_parent(node);
911 }
912
913 if (!node || !ranges) {
914 pr_debug("no dma-ranges found for node(%pOF)\n", np);
915 return -ENODEV;
916 }
917 of_dma_range_parser_init(&parser, node);
918 for_each_of_range(&parser, &range) {
919 if (range.cpu_addr == OF_BAD_ADDR) {
920 pr_err("translation of DMA address(%llx) to CPU address failed node(%pOF)\n",
921 range.bus_addr, node);
922 continue;
923 }
924 num_ranges++;
925 }
926
927 if (!num_ranges)
928 return -EINVAL;
929
930 r = kcalloc(num_ranges + 1, sizeof(*r), GFP_KERNEL);
931 if (!r)
932 return -ENOMEM;
933
934 /*
935 * Record all info in the generic DMA ranges array for struct device,
936 * returning an error if we don't find any parsable ranges.
937 */
938 *map = r;
939 of_dma_range_parser_init(&parser, node);
940 for_each_of_range(&parser, &range) {
941 pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n",
942 range.bus_addr, range.cpu_addr, range.size);
943 if (range.cpu_addr == OF_BAD_ADDR)
944 continue;
945 r->cpu_start = range.cpu_addr;
946 r->dma_start = range.bus_addr;
947 r->size = range.size;
948 r++;
949 }
950 return 0;
951 }
952 #endif /* CONFIG_HAS_DMA */
953
954 /**
955 * of_dma_get_max_cpu_address - Gets highest CPU address suitable for DMA
956 * @np: The node to start searching from or NULL to start from the root
957 *
958 * Gets the highest CPU physical address that is addressable by all DMA masters
959 * in the sub-tree pointed by np, or the whole tree if NULL is passed. If no
960 * DMA constrained device is found, it returns PHYS_ADDR_MAX.
961 */
of_dma_get_max_cpu_address(struct device_node * np)962 phys_addr_t __init of_dma_get_max_cpu_address(struct device_node *np)
963 {
964 phys_addr_t max_cpu_addr = PHYS_ADDR_MAX;
965 struct of_range_parser parser;
966 phys_addr_t subtree_max_addr;
967 struct device_node *child;
968 struct of_range range;
969 const __be32 *ranges;
970 u64 cpu_end = 0;
971 int len;
972
973 if (!np)
974 np = of_root;
975
976 ranges = of_get_property(np, "dma-ranges", &len);
977 if (ranges && len) {
978 of_dma_range_parser_init(&parser, np);
979 for_each_of_range(&parser, &range)
980 if (range.cpu_addr + range.size > cpu_end)
981 cpu_end = range.cpu_addr + range.size - 1;
982
983 if (max_cpu_addr > cpu_end)
984 max_cpu_addr = cpu_end;
985 }
986
987 for_each_available_child_of_node(np, child) {
988 subtree_max_addr = of_dma_get_max_cpu_address(child);
989 if (max_cpu_addr > subtree_max_addr)
990 max_cpu_addr = subtree_max_addr;
991 }
992
993 return max_cpu_addr;
994 }
995
996 /**
997 * of_dma_is_coherent - Check if device is coherent
998 * @np: device node
999 *
1000 * It returns true if "dma-coherent" property was found
1001 * for this device in the DT, or if DMA is coherent by
1002 * default for OF devices on the current platform and no
1003 * "dma-noncoherent" property was found for this device.
1004 */
of_dma_is_coherent(struct device_node * np)1005 bool of_dma_is_coherent(struct device_node *np)
1006 {
1007 struct device_node *node __free(device_node) = of_node_get(np);
1008
1009 while (node) {
1010 if (of_property_read_bool(node, "dma-coherent"))
1011 return true;
1012
1013 if (of_property_read_bool(node, "dma-noncoherent"))
1014 return false;
1015
1016 node = of_get_next_dma_parent(node);
1017 }
1018 return dma_default_coherent;
1019 }
1020 EXPORT_SYMBOL_GPL(of_dma_is_coherent);
1021
1022 /**
1023 * of_mmio_is_nonposted - Check if device uses non-posted MMIO
1024 * @np: device node
1025 *
1026 * Returns true if the "nonposted-mmio" property was found for
1027 * the device's bus.
1028 *
1029 * This is currently only enabled on builds that support Apple ARM devices, as
1030 * an optimization.
1031 */
of_mmio_is_nonposted(struct device_node * np)1032 static bool of_mmio_is_nonposted(struct device_node *np)
1033 {
1034 if (!IS_ENABLED(CONFIG_ARCH_APPLE))
1035 return false;
1036
1037 struct device_node *parent __free(device_node) = of_get_parent(np);
1038 if (!parent)
1039 return false;
1040
1041 return of_property_read_bool(parent, "nonposted-mmio");
1042 }
1043
__of_address_to_resource(struct device_node * dev,int index,int bar_no,struct resource * r)1044 static int __of_address_to_resource(struct device_node *dev, int index, int bar_no,
1045 struct resource *r)
1046 {
1047 u64 taddr;
1048 const __be32 *addrp;
1049 u64 size;
1050 unsigned int flags;
1051 const char *name = NULL;
1052
1053 addrp = __of_get_address(dev, index, bar_no, &size, &flags);
1054 if (addrp == NULL)
1055 return -EINVAL;
1056
1057 /* Get optional "reg-names" property to add a name to a resource */
1058 if (index >= 0)
1059 of_property_read_string_index(dev, "reg-names", index, &name);
1060
1061 if (flags & IORESOURCE_MEM)
1062 taddr = of_translate_address(dev, addrp);
1063 else if (flags & IORESOURCE_IO)
1064 taddr = of_translate_ioport(dev, addrp, size);
1065 else
1066 return -EINVAL;
1067
1068 if (taddr == OF_BAD_ADDR)
1069 return -EINVAL;
1070 memset(r, 0, sizeof(struct resource));
1071
1072 if (of_mmio_is_nonposted(dev))
1073 flags |= IORESOURCE_MEM_NONPOSTED;
1074
1075 r->flags = flags;
1076 r->name = name ? name : dev->full_name;
1077
1078 return __of_address_resource_bounds(r, taddr, size);
1079 }
1080
1081 /**
1082 * of_address_to_resource - Translate device tree address and return as resource
1083 * @dev: Caller's Device Node
1084 * @index: Index into the array
1085 * @r: Pointer to resource array
1086 *
1087 * Returns -EINVAL if the range cannot be converted to resource.
1088 *
1089 * Note that if your address is a PIO address, the conversion will fail if
1090 * the physical address can't be internally converted to an IO token with
1091 * pci_address_to_pio(), that is because it's either called too early or it
1092 * can't be matched to any host bridge IO space
1093 */
of_address_to_resource(struct device_node * dev,int index,struct resource * r)1094 int of_address_to_resource(struct device_node *dev, int index,
1095 struct resource *r)
1096 {
1097 return __of_address_to_resource(dev, index, -1, r);
1098 }
1099 EXPORT_SYMBOL_GPL(of_address_to_resource);
1100
of_pci_address_to_resource(struct device_node * dev,int bar,struct resource * r)1101 int of_pci_address_to_resource(struct device_node *dev, int bar,
1102 struct resource *r)
1103 {
1104
1105 if (!IS_ENABLED(CONFIG_PCI))
1106 return -ENOSYS;
1107
1108 return __of_address_to_resource(dev, -1, bar, r);
1109 }
1110 EXPORT_SYMBOL_GPL(of_pci_address_to_resource);
1111
1112 /**
1113 * of_iomap - Maps the memory mapped IO for a given device_node
1114 * @np: the device whose io range will be mapped
1115 * @index: index of the io range
1116 *
1117 * Returns a pointer to the mapped memory
1118 */
of_iomap(struct device_node * np,int index)1119 void __iomem *of_iomap(struct device_node *np, int index)
1120 {
1121 struct resource res;
1122
1123 if (of_address_to_resource(np, index, &res))
1124 return NULL;
1125
1126 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1127 return ioremap_np(res.start, resource_size(&res));
1128 else
1129 return ioremap(res.start, resource_size(&res));
1130 }
1131 EXPORT_SYMBOL(of_iomap);
1132
1133 /*
1134 * of_io_request_and_map - Requests a resource and maps the memory mapped IO
1135 * for a given device_node
1136 * @device: the device whose io range will be mapped
1137 * @index: index of the io range
1138 * @name: name "override" for the memory region request or NULL
1139 *
1140 * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded
1141 * error code on failure. Usage example:
1142 *
1143 * base = of_io_request_and_map(node, 0, "foo");
1144 * if (IS_ERR(base))
1145 * return PTR_ERR(base);
1146 */
of_io_request_and_map(struct device_node * np,int index,const char * name)1147 void __iomem *of_io_request_and_map(struct device_node *np, int index,
1148 const char *name)
1149 {
1150 struct resource res;
1151 void __iomem *mem;
1152
1153 if (of_address_to_resource(np, index, &res))
1154 return IOMEM_ERR_PTR(-EINVAL);
1155
1156 if (!name)
1157 name = res.name;
1158 if (!request_mem_region(res.start, resource_size(&res), name))
1159 return IOMEM_ERR_PTR(-EBUSY);
1160
1161 if (res.flags & IORESOURCE_MEM_NONPOSTED)
1162 mem = ioremap_np(res.start, resource_size(&res));
1163 else
1164 mem = ioremap(res.start, resource_size(&res));
1165
1166 if (!mem) {
1167 release_mem_region(res.start, resource_size(&res));
1168 return IOMEM_ERR_PTR(-ENOMEM);
1169 }
1170
1171 return mem;
1172 }
1173 EXPORT_SYMBOL(of_io_request_and_map);
1174