]>
Commit | Line | Data |
---|---|---|
6b884a8d | 1 | |
5019f0b1 | 2 | #include <linux/device.h> |
6b884a8d GL |
3 | #include <linux/io.h> |
4 | #include <linux/ioport.h> | |
dbbdee94 | 5 | #include <linux/module.h> |
6b884a8d | 6 | #include <linux/of_address.h> |
dbbdee94 | 7 | #include <linux/pci_regs.h> |
41f8bba7 LD |
8 | #include <linux/sizes.h> |
9 | #include <linux/slab.h> | |
dbbdee94 | 10 | #include <linux/string.h> |
6b884a8d | 11 | |
dbbdee94 GL |
12 | /* Max address size we deal with */ |
13 | #define OF_MAX_ADDR_CELLS 4 | |
5d61b165 SW |
14 | #define OF_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS) |
15 | #define OF_CHECK_COUNTS(na, ns) (OF_CHECK_ADDR_COUNT(na) && (ns) > 0) | |
dbbdee94 GL |
16 | |
17 | static struct of_bus *of_match_bus(struct device_node *np); | |
0131d897 SAS |
18 | static int __of_address_to_resource(struct device_node *dev, |
19 | const __be32 *addrp, u64 size, unsigned int flags, | |
35f3da32 | 20 | const char *name, struct resource *r); |
dbbdee94 GL |
21 | |
22 | /* Debug utility */ | |
23 | #ifdef DEBUG | |
0131d897 | 24 | static void of_dump_addr(const char *s, const __be32 *addr, int na) |
dbbdee94 GL |
25 | { |
26 | printk(KERN_DEBUG "%s", s); | |
27 | while (na--) | |
154063a9 | 28 | printk(" %08x", be32_to_cpu(*(addr++))); |
dbbdee94 GL |
29 | printk("\n"); |
30 | } | |
31 | #else | |
0131d897 | 32 | static void of_dump_addr(const char *s, const __be32 *addr, int na) { } |
dbbdee94 GL |
33 | #endif |
34 | ||
35 | /* Callbacks for bus specific translators */ | |
36 | struct of_bus { | |
37 | const char *name; | |
38 | const char *addresses; | |
39 | int (*match)(struct device_node *parent); | |
40 | void (*count_cells)(struct device_node *child, | |
41 | int *addrc, int *sizec); | |
47b1e689 | 42 | u64 (*map)(__be32 *addr, const __be32 *range, |
dbbdee94 | 43 | int na, int ns, int pna); |
47b1e689 | 44 | int (*translate)(__be32 *addr, u64 offset, int na); |
0131d897 | 45 | unsigned int (*get_flags)(const __be32 *addr); |
dbbdee94 GL |
46 | }; |
47 | ||
48 | /* | |
49 | * Default translator (generic bus) | |
50 | */ | |
51 | ||
52 | static void of_bus_default_count_cells(struct device_node *dev, | |
53 | int *addrc, int *sizec) | |
54 | { | |
55 | if (addrc) | |
56 | *addrc = of_n_addr_cells(dev); | |
57 | if (sizec) | |
58 | *sizec = of_n_size_cells(dev); | |
59 | } | |
60 | ||
47b1e689 | 61 | static u64 of_bus_default_map(__be32 *addr, const __be32 *range, |
dbbdee94 GL |
62 | int na, int ns, int pna) |
63 | { | |
64 | u64 cp, s, da; | |
65 | ||
66 | cp = of_read_number(range, na); | |
67 | s = of_read_number(range + na + pna, ns); | |
68 | da = of_read_number(addr, na); | |
69 | ||
70 | pr_debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", | |
71 | (unsigned long long)cp, (unsigned long long)s, | |
72 | (unsigned long long)da); | |
73 | ||
74 | if (da < cp || da >= (cp + s)) | |
75 | return OF_BAD_ADDR; | |
76 | return da - cp; | |
77 | } | |
78 | ||
47b1e689 | 79 | static int of_bus_default_translate(__be32 *addr, u64 offset, int na) |
dbbdee94 GL |
80 | { |
81 | u64 a = of_read_number(addr, na); | |
82 | memset(addr, 0, na * 4); | |
83 | a += offset; | |
84 | if (na > 1) | |
154063a9 GL |
85 | addr[na - 2] = cpu_to_be32(a >> 32); |
86 | addr[na - 1] = cpu_to_be32(a & 0xffffffffu); | |
dbbdee94 GL |
87 | |
88 | return 0; | |
89 | } | |
90 | ||
0131d897 | 91 | static unsigned int of_bus_default_get_flags(const __be32 *addr) |
dbbdee94 GL |
92 | { |
93 | return IORESOURCE_MEM; | |
94 | } | |
95 | ||
25a31579 | 96 | #ifdef CONFIG_OF_ADDRESS_PCI |
dbbdee94 GL |
97 | /* |
98 | * PCI bus specific translator | |
99 | */ | |
100 | ||
101 | static int of_bus_pci_match(struct device_node *np) | |
102 | { | |
6dd18e46 | 103 | /* |
14e2abb7 | 104 | * "pciex" is PCI Express |
6dd18e46 BH |
105 | * "vci" is for the /chaos bridge on 1st-gen PCI powermacs |
106 | * "ht" is hypertransport | |
107 | */ | |
14e2abb7 KSS |
108 | return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") || |
109 | !strcmp(np->type, "vci") || !strcmp(np->type, "ht"); | |
dbbdee94 GL |
110 | } |
111 | ||
112 | static void of_bus_pci_count_cells(struct device_node *np, | |
113 | int *addrc, int *sizec) | |
114 | { | |
115 | if (addrc) | |
116 | *addrc = 3; | |
117 | if (sizec) | |
118 | *sizec = 2; | |
119 | } | |
120 | ||
0131d897 | 121 | static unsigned int of_bus_pci_get_flags(const __be32 *addr) |
dbbdee94 GL |
122 | { |
123 | unsigned int flags = 0; | |
0131d897 | 124 | u32 w = be32_to_cpup(addr); |
dbbdee94 GL |
125 | |
126 | switch((w >> 24) & 0x03) { | |
127 | case 0x01: | |
128 | flags |= IORESOURCE_IO; | |
129 | break; | |
130 | case 0x02: /* 32 bits */ | |
131 | case 0x03: /* 64 bits */ | |
132 | flags |= IORESOURCE_MEM; | |
133 | break; | |
134 | } | |
135 | if (w & 0x40000000) | |
136 | flags |= IORESOURCE_PREFETCH; | |
137 | return flags; | |
138 | } | |
139 | ||
47b1e689 | 140 | static u64 of_bus_pci_map(__be32 *addr, const __be32 *range, int na, int ns, |
0131d897 | 141 | int pna) |
dbbdee94 GL |
142 | { |
143 | u64 cp, s, da; | |
144 | unsigned int af, rf; | |
145 | ||
146 | af = of_bus_pci_get_flags(addr); | |
147 | rf = of_bus_pci_get_flags(range); | |
148 | ||
149 | /* Check address type match */ | |
150 | if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) | |
151 | return OF_BAD_ADDR; | |
152 | ||
153 | /* Read address values, skipping high cell */ | |
154 | cp = of_read_number(range + 1, na - 1); | |
155 | s = of_read_number(range + na + pna, ns); | |
156 | da = of_read_number(addr + 1, na - 1); | |
157 | ||
158 | pr_debug("OF: PCI map, cp=%llx, s=%llx, da=%llx\n", | |
159 | (unsigned long long)cp, (unsigned long long)s, | |
160 | (unsigned long long)da); | |
161 | ||
162 | if (da < cp || da >= (cp + s)) | |
163 | return OF_BAD_ADDR; | |
164 | return da - cp; | |
165 | } | |
166 | ||
47b1e689 | 167 | static int of_bus_pci_translate(__be32 *addr, u64 offset, int na) |
dbbdee94 GL |
168 | { |
169 | return of_bus_default_translate(addr + 1, offset, na - 1); | |
170 | } | |
25a31579 | 171 | #endif /* CONFIG_OF_ADDRESS_PCI */ |
dbbdee94 | 172 | |
25a31579 | 173 | #ifdef CONFIG_PCI |
0131d897 | 174 | const __be32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size, |
dbbdee94 GL |
175 | unsigned int *flags) |
176 | { | |
a9fadeef | 177 | const __be32 *prop; |
dbbdee94 GL |
178 | unsigned int psize; |
179 | struct device_node *parent; | |
180 | struct of_bus *bus; | |
181 | int onesize, i, na, ns; | |
182 | ||
183 | /* Get parent & match bus type */ | |
184 | parent = of_get_parent(dev); | |
185 | if (parent == NULL) | |
186 | return NULL; | |
187 | bus = of_match_bus(parent); | |
188 | if (strcmp(bus->name, "pci")) { | |
189 | of_node_put(parent); | |
190 | return NULL; | |
191 | } | |
192 | bus->count_cells(dev, &na, &ns); | |
193 | of_node_put(parent); | |
5d61b165 | 194 | if (!OF_CHECK_ADDR_COUNT(na)) |
dbbdee94 GL |
195 | return NULL; |
196 | ||
197 | /* Get "reg" or "assigned-addresses" property */ | |
198 | prop = of_get_property(dev, bus->addresses, &psize); | |
199 | if (prop == NULL) | |
200 | return NULL; | |
201 | psize /= 4; | |
202 | ||
203 | onesize = na + ns; | |
154063a9 GL |
204 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) { |
205 | u32 val = be32_to_cpu(prop[0]); | |
206 | if ((val & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0)) { | |
dbbdee94 GL |
207 | if (size) |
208 | *size = of_read_number(prop + na, ns); | |
209 | if (flags) | |
210 | *flags = bus->get_flags(prop); | |
211 | return prop; | |
212 | } | |
154063a9 | 213 | } |
dbbdee94 GL |
214 | return NULL; |
215 | } | |
216 | EXPORT_SYMBOL(of_get_pci_address); | |
217 | ||
218 | int of_pci_address_to_resource(struct device_node *dev, int bar, | |
219 | struct resource *r) | |
220 | { | |
0131d897 | 221 | const __be32 *addrp; |
dbbdee94 GL |
222 | u64 size; |
223 | unsigned int flags; | |
224 | ||
225 | addrp = of_get_pci_address(dev, bar, &size, &flags); | |
226 | if (addrp == NULL) | |
227 | return -EINVAL; | |
35f3da32 | 228 | return __of_address_to_resource(dev, addrp, size, flags, NULL, r); |
dbbdee94 GL |
229 | } |
230 | EXPORT_SYMBOL_GPL(of_pci_address_to_resource); | |
29b635c0 AM |
231 | |
232 | int of_pci_range_parser_init(struct of_pci_range_parser *parser, | |
233 | struct device_node *node) | |
234 | { | |
235 | const int na = 3, ns = 2; | |
236 | int rlen; | |
237 | ||
238 | parser->node = node; | |
239 | parser->pna = of_n_addr_cells(node); | |
240 | parser->np = parser->pna + na + ns; | |
241 | ||
242 | parser->range = of_get_property(node, "ranges", &rlen); | |
243 | if (parser->range == NULL) | |
244 | return -ENOENT; | |
245 | ||
246 | parser->end = parser->range + rlen / sizeof(__be32); | |
247 | ||
248 | return 0; | |
249 | } | |
250 | EXPORT_SYMBOL_GPL(of_pci_range_parser_init); | |
251 | ||
252 | struct of_pci_range *of_pci_range_parser_one(struct of_pci_range_parser *parser, | |
253 | struct of_pci_range *range) | |
254 | { | |
255 | const int na = 3, ns = 2; | |
256 | ||
257 | if (!range) | |
258 | return NULL; | |
259 | ||
260 | if (!parser->range || parser->range + parser->np > parser->end) | |
261 | return NULL; | |
262 | ||
263 | range->pci_space = parser->range[0]; | |
264 | range->flags = of_bus_pci_get_flags(parser->range); | |
265 | range->pci_addr = of_read_number(parser->range + 1, ns); | |
266 | range->cpu_addr = of_translate_address(parser->node, | |
267 | parser->range + na); | |
268 | range->size = of_read_number(parser->range + parser->pna + na, ns); | |
269 | ||
270 | parser->range += parser->np; | |
271 | ||
272 | /* Now consume following elements while they are contiguous */ | |
273 | while (parser->range + parser->np <= parser->end) { | |
274 | u32 flags, pci_space; | |
275 | u64 pci_addr, cpu_addr, size; | |
276 | ||
277 | pci_space = be32_to_cpup(parser->range); | |
278 | flags = of_bus_pci_get_flags(parser->range); | |
279 | pci_addr = of_read_number(parser->range + 1, ns); | |
280 | cpu_addr = of_translate_address(parser->node, | |
281 | parser->range + na); | |
282 | size = of_read_number(parser->range + parser->pna + na, ns); | |
283 | ||
284 | if (flags != range->flags) | |
285 | break; | |
286 | if (pci_addr != range->pci_addr + range->size || | |
287 | cpu_addr != range->cpu_addr + range->size) | |
288 | break; | |
289 | ||
290 | range->size += size; | |
291 | parser->range += parser->np; | |
292 | } | |
293 | ||
294 | return range; | |
295 | } | |
296 | EXPORT_SYMBOL_GPL(of_pci_range_parser_one); | |
297 | ||
dbbdee94 GL |
298 | #endif /* CONFIG_PCI */ |
299 | ||
300 | /* | |
301 | * ISA bus specific translator | |
302 | */ | |
303 | ||
304 | static int of_bus_isa_match(struct device_node *np) | |
305 | { | |
306 | return !strcmp(np->name, "isa"); | |
307 | } | |
308 | ||
309 | static void of_bus_isa_count_cells(struct device_node *child, | |
310 | int *addrc, int *sizec) | |
311 | { | |
312 | if (addrc) | |
313 | *addrc = 2; | |
314 | if (sizec) | |
315 | *sizec = 1; | |
316 | } | |
317 | ||
47b1e689 | 318 | static u64 of_bus_isa_map(__be32 *addr, const __be32 *range, int na, int ns, |
0131d897 | 319 | int pna) |
dbbdee94 GL |
320 | { |
321 | u64 cp, s, da; | |
322 | ||
323 | /* Check address type match */ | |
0131d897 | 324 | if ((addr[0] ^ range[0]) & cpu_to_be32(1)) |
dbbdee94 GL |
325 | return OF_BAD_ADDR; |
326 | ||
327 | /* Read address values, skipping high cell */ | |
328 | cp = of_read_number(range + 1, na - 1); | |
329 | s = of_read_number(range + na + pna, ns); | |
330 | da = of_read_number(addr + 1, na - 1); | |
331 | ||
332 | pr_debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", | |
333 | (unsigned long long)cp, (unsigned long long)s, | |
334 | (unsigned long long)da); | |
335 | ||
336 | if (da < cp || da >= (cp + s)) | |
337 | return OF_BAD_ADDR; | |
338 | return da - cp; | |
339 | } | |
340 | ||
47b1e689 | 341 | static int of_bus_isa_translate(__be32 *addr, u64 offset, int na) |
dbbdee94 GL |
342 | { |
343 | return of_bus_default_translate(addr + 1, offset, na - 1); | |
344 | } | |
345 | ||
0131d897 | 346 | static unsigned int of_bus_isa_get_flags(const __be32 *addr) |
dbbdee94 GL |
347 | { |
348 | unsigned int flags = 0; | |
0131d897 | 349 | u32 w = be32_to_cpup(addr); |
dbbdee94 GL |
350 | |
351 | if (w & 1) | |
352 | flags |= IORESOURCE_IO; | |
353 | else | |
354 | flags |= IORESOURCE_MEM; | |
355 | return flags; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Array of bus specific translators | |
360 | */ | |
361 | ||
362 | static struct of_bus of_busses[] = { | |
25a31579 | 363 | #ifdef CONFIG_OF_ADDRESS_PCI |
dbbdee94 GL |
364 | /* PCI */ |
365 | { | |
366 | .name = "pci", | |
367 | .addresses = "assigned-addresses", | |
368 | .match = of_bus_pci_match, | |
369 | .count_cells = of_bus_pci_count_cells, | |
370 | .map = of_bus_pci_map, | |
371 | .translate = of_bus_pci_translate, | |
372 | .get_flags = of_bus_pci_get_flags, | |
373 | }, | |
25a31579 | 374 | #endif /* CONFIG_OF_ADDRESS_PCI */ |
dbbdee94 GL |
375 | /* ISA */ |
376 | { | |
377 | .name = "isa", | |
378 | .addresses = "reg", | |
379 | .match = of_bus_isa_match, | |
380 | .count_cells = of_bus_isa_count_cells, | |
381 | .map = of_bus_isa_map, | |
382 | .translate = of_bus_isa_translate, | |
383 | .get_flags = of_bus_isa_get_flags, | |
384 | }, | |
385 | /* Default */ | |
386 | { | |
387 | .name = "default", | |
388 | .addresses = "reg", | |
389 | .match = NULL, | |
390 | .count_cells = of_bus_default_count_cells, | |
391 | .map = of_bus_default_map, | |
392 | .translate = of_bus_default_translate, | |
393 | .get_flags = of_bus_default_get_flags, | |
394 | }, | |
395 | }; | |
396 | ||
397 | static struct of_bus *of_match_bus(struct device_node *np) | |
398 | { | |
399 | int i; | |
400 | ||
401 | for (i = 0; i < ARRAY_SIZE(of_busses); i++) | |
402 | if (!of_busses[i].match || of_busses[i].match(np)) | |
403 | return &of_busses[i]; | |
404 | BUG(); | |
405 | return NULL; | |
406 | } | |
407 | ||
408 | static int of_translate_one(struct device_node *parent, struct of_bus *bus, | |
47b1e689 | 409 | struct of_bus *pbus, __be32 *addr, |
dbbdee94 GL |
410 | int na, int ns, int pna, const char *rprop) |
411 | { | |
0131d897 | 412 | const __be32 *ranges; |
dbbdee94 GL |
413 | unsigned int rlen; |
414 | int rone; | |
415 | u64 offset = OF_BAD_ADDR; | |
416 | ||
417 | /* Normally, an absence of a "ranges" property means we are | |
418 | * crossing a non-translatable boundary, and thus the addresses | |
419 | * below the current not cannot be converted to CPU physical ones. | |
420 | * Unfortunately, while this is very clear in the spec, it's not | |
421 | * what Apple understood, and they do have things like /uni-n or | |
422 | * /ht nodes with no "ranges" property and a lot of perfectly | |
423 | * useable mapped devices below them. Thus we treat the absence of | |
424 | * "ranges" as equivalent to an empty "ranges" property which means | |
425 | * a 1:1 translation at that level. It's up to the caller not to try | |
426 | * to translate addresses that aren't supposed to be translated in | |
427 | * the first place. --BenH. | |
3930f294 GL |
428 | * |
429 | * As far as we know, this damage only exists on Apple machines, so | |
430 | * This code is only enabled on powerpc. --gcl | |
dbbdee94 GL |
431 | */ |
432 | ranges = of_get_property(parent, rprop, &rlen); | |
3930f294 GL |
433 | #if !defined(CONFIG_PPC) |
434 | if (ranges == NULL) { | |
435 | pr_err("OF: no ranges; cannot translate\n"); | |
436 | return 1; | |
437 | } | |
438 | #endif /* !defined(CONFIG_PPC) */ | |
dbbdee94 GL |
439 | if (ranges == NULL || rlen == 0) { |
440 | offset = of_read_number(addr, na); | |
441 | memset(addr, 0, pna * 4); | |
3930f294 | 442 | pr_debug("OF: empty ranges; 1:1 translation\n"); |
dbbdee94 GL |
443 | goto finish; |
444 | } | |
445 | ||
446 | pr_debug("OF: walking ranges...\n"); | |
447 | ||
448 | /* Now walk through the ranges */ | |
449 | rlen /= 4; | |
450 | rone = na + pna + ns; | |
451 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
452 | offset = bus->map(addr, ranges, na, ns, pna); | |
453 | if (offset != OF_BAD_ADDR) | |
454 | break; | |
455 | } | |
456 | if (offset == OF_BAD_ADDR) { | |
457 | pr_debug("OF: not found !\n"); | |
458 | return 1; | |
459 | } | |
460 | memcpy(addr, ranges + na, 4 * pna); | |
461 | ||
462 | finish: | |
463 | of_dump_addr("OF: parent translation for:", addr, pna); | |
464 | pr_debug("OF: with offset: %llx\n", (unsigned long long)offset); | |
465 | ||
466 | /* Translate it into parent bus space */ | |
467 | return pbus->translate(addr, offset, pna); | |
468 | } | |
469 | ||
470 | /* | |
471 | * Translate an address from the device-tree into a CPU physical address, | |
472 | * this walks up the tree and applies the various bus mappings on the | |
473 | * way. | |
474 | * | |
475 | * Note: We consider that crossing any level with #size-cells == 0 to mean | |
476 | * that translation is impossible (that is we are not dealing with a value | |
477 | * that can be mapped to a cpu physical address). This is not really specified | |
478 | * that way, but this is traditionally the way IBM at least do things | |
479 | */ | |
47b1e689 KP |
480 | static u64 __of_translate_address(struct device_node *dev, |
481 | const __be32 *in_addr, const char *rprop) | |
dbbdee94 GL |
482 | { |
483 | struct device_node *parent = NULL; | |
484 | struct of_bus *bus, *pbus; | |
47b1e689 | 485 | __be32 addr[OF_MAX_ADDR_CELLS]; |
dbbdee94 GL |
486 | int na, ns, pna, pns; |
487 | u64 result = OF_BAD_ADDR; | |
488 | ||
8804827b | 489 | pr_debug("OF: ** translation for device %s **\n", of_node_full_name(dev)); |
dbbdee94 GL |
490 | |
491 | /* Increase refcount at current level */ | |
492 | of_node_get(dev); | |
493 | ||
494 | /* Get parent & match bus type */ | |
495 | parent = of_get_parent(dev); | |
496 | if (parent == NULL) | |
497 | goto bail; | |
498 | bus = of_match_bus(parent); | |
499 | ||
59f5ca48 | 500 | /* Count address cells & copy address locally */ |
dbbdee94 GL |
501 | bus->count_cells(dev, &na, &ns); |
502 | if (!OF_CHECK_COUNTS(na, ns)) { | |
d9c6866b | 503 | pr_debug("OF: Bad cell count for %s\n", of_node_full_name(dev)); |
dbbdee94 GL |
504 | goto bail; |
505 | } | |
506 | memcpy(addr, in_addr, na * 4); | |
507 | ||
508 | pr_debug("OF: bus is %s (na=%d, ns=%d) on %s\n", | |
8804827b | 509 | bus->name, na, ns, of_node_full_name(parent)); |
dbbdee94 GL |
510 | of_dump_addr("OF: translating address:", addr, na); |
511 | ||
512 | /* Translate */ | |
513 | for (;;) { | |
514 | /* Switch to parent bus */ | |
515 | of_node_put(dev); | |
516 | dev = parent; | |
517 | parent = of_get_parent(dev); | |
518 | ||
519 | /* If root, we have finished */ | |
520 | if (parent == NULL) { | |
521 | pr_debug("OF: reached root node\n"); | |
522 | result = of_read_number(addr, na); | |
523 | break; | |
524 | } | |
525 | ||
526 | /* Get new parent bus and counts */ | |
527 | pbus = of_match_bus(parent); | |
528 | pbus->count_cells(dev, &pna, &pns); | |
529 | if (!OF_CHECK_COUNTS(pna, pns)) { | |
530 | printk(KERN_ERR "prom_parse: Bad cell count for %s\n", | |
0c02c800 | 531 | of_node_full_name(dev)); |
dbbdee94 GL |
532 | break; |
533 | } | |
534 | ||
535 | pr_debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n", | |
0c02c800 | 536 | pbus->name, pna, pns, of_node_full_name(parent)); |
dbbdee94 GL |
537 | |
538 | /* Apply bus translation */ | |
539 | if (of_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop)) | |
540 | break; | |
541 | ||
542 | /* Complete the move up one level */ | |
543 | na = pna; | |
544 | ns = pns; | |
545 | bus = pbus; | |
546 | ||
547 | of_dump_addr("OF: one level translation:", addr, na); | |
548 | } | |
549 | bail: | |
550 | of_node_put(parent); | |
551 | of_node_put(dev); | |
552 | ||
553 | return result; | |
554 | } | |
555 | ||
0131d897 | 556 | u64 of_translate_address(struct device_node *dev, const __be32 *in_addr) |
dbbdee94 GL |
557 | { |
558 | return __of_translate_address(dev, in_addr, "ranges"); | |
559 | } | |
560 | EXPORT_SYMBOL(of_translate_address); | |
561 | ||
0131d897 | 562 | u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr) |
dbbdee94 GL |
563 | { |
564 | return __of_translate_address(dev, in_addr, "dma-ranges"); | |
565 | } | |
566 | EXPORT_SYMBOL(of_translate_dma_address); | |
567 | ||
0131d897 | 568 | const __be32 *of_get_address(struct device_node *dev, int index, u64 *size, |
dbbdee94 GL |
569 | unsigned int *flags) |
570 | { | |
0131d897 | 571 | const __be32 *prop; |
dbbdee94 GL |
572 | unsigned int psize; |
573 | struct device_node *parent; | |
574 | struct of_bus *bus; | |
575 | int onesize, i, na, ns; | |
576 | ||
577 | /* Get parent & match bus type */ | |
578 | parent = of_get_parent(dev); | |
579 | if (parent == NULL) | |
580 | return NULL; | |
581 | bus = of_match_bus(parent); | |
582 | bus->count_cells(dev, &na, &ns); | |
583 | of_node_put(parent); | |
5d61b165 | 584 | if (!OF_CHECK_ADDR_COUNT(na)) |
dbbdee94 GL |
585 | return NULL; |
586 | ||
587 | /* Get "reg" or "assigned-addresses" property */ | |
588 | prop = of_get_property(dev, bus->addresses, &psize); | |
589 | if (prop == NULL) | |
590 | return NULL; | |
591 | psize /= 4; | |
592 | ||
593 | onesize = na + ns; | |
594 | for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++) | |
595 | if (i == index) { | |
596 | if (size) | |
597 | *size = of_read_number(prop + na, ns); | |
598 | if (flags) | |
599 | *flags = bus->get_flags(prop); | |
600 | return prop; | |
601 | } | |
602 | return NULL; | |
603 | } | |
604 | EXPORT_SYMBOL(of_get_address); | |
605 | ||
41f8bba7 LD |
606 | #ifdef PCI_IOBASE |
607 | struct io_range { | |
608 | struct list_head list; | |
609 | phys_addr_t start; | |
610 | resource_size_t size; | |
611 | }; | |
612 | ||
613 | static LIST_HEAD(io_range_list); | |
614 | static DEFINE_SPINLOCK(io_range_lock); | |
615 | #endif | |
616 | ||
617 | /* | |
618 | * Record the PCI IO range (expressed as CPU physical address + size). | |
619 | * Return a negative value if an error has occured, zero otherwise | |
620 | */ | |
621 | int __weak pci_register_io_range(phys_addr_t addr, resource_size_t size) | |
622 | { | |
623 | int err = 0; | |
624 | ||
625 | #ifdef PCI_IOBASE | |
626 | struct io_range *range; | |
627 | resource_size_t allocated_size = 0; | |
628 | ||
629 | /* check if the range hasn't been previously recorded */ | |
630 | spin_lock(&io_range_lock); | |
631 | list_for_each_entry(range, &io_range_list, list) { | |
632 | if (addr >= range->start && addr + size <= range->start + size) { | |
633 | /* range already registered, bail out */ | |
634 | goto end_register; | |
635 | } | |
636 | allocated_size += range->size; | |
637 | } | |
638 | ||
639 | /* range not registed yet, check for available space */ | |
640 | if (allocated_size + size - 1 > IO_SPACE_LIMIT) { | |
641 | /* if it's too big check if 64K space can be reserved */ | |
642 | if (allocated_size + SZ_64K - 1 > IO_SPACE_LIMIT) { | |
643 | err = -E2BIG; | |
644 | goto end_register; | |
645 | } | |
646 | ||
647 | size = SZ_64K; | |
648 | pr_warn("Requested IO range too big, new size set to 64K\n"); | |
649 | } | |
650 | ||
651 | /* add the range to the list */ | |
652 | range = kzalloc(sizeof(*range), GFP_KERNEL); | |
653 | if (!range) { | |
654 | err = -ENOMEM; | |
655 | goto end_register; | |
656 | } | |
657 | ||
658 | range->start = addr; | |
659 | range->size = size; | |
660 | ||
661 | list_add_tail(&range->list, &io_range_list); | |
662 | ||
663 | end_register: | |
664 | spin_unlock(&io_range_lock); | |
665 | #endif | |
666 | ||
667 | return err; | |
668 | } | |
669 | ||
670 | phys_addr_t pci_pio_to_address(unsigned long pio) | |
671 | { | |
672 | phys_addr_t address = (phys_addr_t)OF_BAD_ADDR; | |
673 | ||
674 | #ifdef PCI_IOBASE | |
675 | struct io_range *range; | |
676 | resource_size_t allocated_size = 0; | |
677 | ||
678 | if (pio > IO_SPACE_LIMIT) | |
679 | return address; | |
680 | ||
681 | spin_lock(&io_range_lock); | |
682 | list_for_each_entry(range, &io_range_list, list) { | |
683 | if (pio >= allocated_size && pio < allocated_size + range->size) { | |
684 | address = range->start + pio - allocated_size; | |
685 | break; | |
686 | } | |
687 | allocated_size += range->size; | |
688 | } | |
689 | spin_unlock(&io_range_lock); | |
690 | #endif | |
691 | ||
692 | return address; | |
693 | } | |
694 | ||
25ff7944 RH |
695 | unsigned long __weak pci_address_to_pio(phys_addr_t address) |
696 | { | |
41f8bba7 LD |
697 | #ifdef PCI_IOBASE |
698 | struct io_range *res; | |
699 | resource_size_t offset = 0; | |
700 | unsigned long addr = -1; | |
701 | ||
702 | spin_lock(&io_range_lock); | |
703 | list_for_each_entry(res, &io_range_list, list) { | |
704 | if (address >= res->start && address < res->start + res->size) { | |
705 | addr = res->start - address + offset; | |
706 | break; | |
707 | } | |
708 | offset += res->size; | |
709 | } | |
710 | spin_unlock(&io_range_lock); | |
711 | ||
712 | return addr; | |
713 | #else | |
25ff7944 RH |
714 | if (address > IO_SPACE_LIMIT) |
715 | return (unsigned long)-1; | |
716 | ||
717 | return (unsigned long) address; | |
41f8bba7 | 718 | #endif |
25ff7944 RH |
719 | } |
720 | ||
0131d897 SAS |
721 | static int __of_address_to_resource(struct device_node *dev, |
722 | const __be32 *addrp, u64 size, unsigned int flags, | |
35f3da32 | 723 | const char *name, struct resource *r) |
1f5bef30 GL |
724 | { |
725 | u64 taddr; | |
726 | ||
727 | if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0) | |
728 | return -EINVAL; | |
729 | taddr = of_translate_address(dev, addrp); | |
730 | if (taddr == OF_BAD_ADDR) | |
731 | return -EINVAL; | |
732 | memset(r, 0, sizeof(struct resource)); | |
733 | if (flags & IORESOURCE_IO) { | |
734 | unsigned long port; | |
735 | port = pci_address_to_pio(taddr); | |
736 | if (port == (unsigned long)-1) | |
737 | return -EINVAL; | |
738 | r->start = port; | |
739 | r->end = port + size - 1; | |
740 | } else { | |
741 | r->start = taddr; | |
742 | r->end = taddr + size - 1; | |
743 | } | |
744 | r->flags = flags; | |
35f3da32 BC |
745 | r->name = name ? name : dev->full_name; |
746 | ||
1f5bef30 GL |
747 | return 0; |
748 | } | |
749 | ||
750 | /** | |
751 | * of_address_to_resource - Translate device tree address and return as resource | |
752 | * | |
753 | * Note that if your address is a PIO address, the conversion will fail if | |
754 | * the physical address can't be internally converted to an IO token with | |
755 | * pci_address_to_pio(), that is because it's either called to early or it | |
756 | * can't be matched to any host bridge IO space | |
757 | */ | |
758 | int of_address_to_resource(struct device_node *dev, int index, | |
759 | struct resource *r) | |
760 | { | |
0131d897 | 761 | const __be32 *addrp; |
1f5bef30 GL |
762 | u64 size; |
763 | unsigned int flags; | |
35f3da32 | 764 | const char *name = NULL; |
1f5bef30 GL |
765 | |
766 | addrp = of_get_address(dev, index, &size, &flags); | |
767 | if (addrp == NULL) | |
768 | return -EINVAL; | |
35f3da32 BC |
769 | |
770 | /* Get optional "reg-names" property to add a name to a resource */ | |
771 | of_property_read_string_index(dev, "reg-names", index, &name); | |
772 | ||
773 | return __of_address_to_resource(dev, addrp, size, flags, name, r); | |
1f5bef30 GL |
774 | } |
775 | EXPORT_SYMBOL_GPL(of_address_to_resource); | |
776 | ||
90e33f62 GL |
777 | struct device_node *of_find_matching_node_by_address(struct device_node *from, |
778 | const struct of_device_id *matches, | |
779 | u64 base_address) | |
780 | { | |
781 | struct device_node *dn = of_find_matching_node(from, matches); | |
782 | struct resource res; | |
783 | ||
784 | while (dn) { | |
785 | if (of_address_to_resource(dn, 0, &res)) | |
786 | continue; | |
787 | if (res.start == base_address) | |
788 | return dn; | |
789 | dn = of_find_matching_node(dn, matches); | |
790 | } | |
791 | ||
792 | return NULL; | |
793 | } | |
794 | ||
1f5bef30 | 795 | |
6b884a8d GL |
796 | /** |
797 | * of_iomap - Maps the memory mapped IO for a given device_node | |
798 | * @device: the device whose io range will be mapped | |
799 | * @index: index of the io range | |
800 | * | |
801 | * Returns a pointer to the mapped memory | |
802 | */ | |
803 | void __iomem *of_iomap(struct device_node *np, int index) | |
804 | { | |
805 | struct resource res; | |
806 | ||
807 | if (of_address_to_resource(np, index, &res)) | |
808 | return NULL; | |
809 | ||
28c1b6d6 | 810 | return ioremap(res.start, resource_size(&res)); |
6b884a8d GL |
811 | } |
812 | EXPORT_SYMBOL(of_iomap); | |
18308c94 | 813 | |
efd342fb MB |
814 | /* |
815 | * of_io_request_and_map - Requests a resource and maps the memory mapped IO | |
816 | * for a given device_node | |
817 | * @device: the device whose io range will be mapped | |
818 | * @index: index of the io range | |
819 | * @name: name of the resource | |
820 | * | |
821 | * Returns a pointer to the requested and mapped memory or an ERR_PTR() encoded | |
822 | * error code on failure. Usage example: | |
823 | * | |
824 | * base = of_io_request_and_map(node, 0, "foo"); | |
825 | * if (IS_ERR(base)) | |
826 | * return PTR_ERR(base); | |
827 | */ | |
828 | void __iomem *of_io_request_and_map(struct device_node *np, int index, | |
829 | char *name) | |
830 | { | |
831 | struct resource res; | |
832 | void __iomem *mem; | |
833 | ||
834 | if (of_address_to_resource(np, index, &res)) | |
835 | return IOMEM_ERR_PTR(-EINVAL); | |
836 | ||
837 | if (!request_mem_region(res.start, resource_size(&res), name)) | |
838 | return IOMEM_ERR_PTR(-EBUSY); | |
839 | ||
840 | mem = ioremap(res.start, resource_size(&res)); | |
841 | if (!mem) { | |
842 | release_mem_region(res.start, resource_size(&res)); | |
843 | return IOMEM_ERR_PTR(-ENOMEM); | |
844 | } | |
845 | ||
846 | return mem; | |
847 | } | |
848 | EXPORT_SYMBOL(of_io_request_and_map); | |
849 | ||
18308c94 GS |
850 | /** |
851 | * of_dma_get_range - Get DMA range info | |
852 | * @np: device node to get DMA range info | |
853 | * @dma_addr: pointer to store initial DMA address of DMA range | |
854 | * @paddr: pointer to store initial CPU address of DMA range | |
855 | * @size: pointer to store size of DMA range | |
856 | * | |
857 | * Look in bottom up direction for the first "dma-ranges" property | |
858 | * and parse it. | |
859 | * dma-ranges format: | |
860 | * DMA addr (dma_addr) : naddr cells | |
861 | * CPU addr (phys_addr_t) : pna cells | |
862 | * size : nsize cells | |
863 | * | |
864 | * It returns -ENODEV if "dma-ranges" property was not found | |
865 | * for this device in DT. | |
866 | */ | |
867 | int of_dma_get_range(struct device_node *np, u64 *dma_addr, u64 *paddr, u64 *size) | |
868 | { | |
869 | struct device_node *node = of_node_get(np); | |
870 | const __be32 *ranges = NULL; | |
871 | int len, naddr, nsize, pna; | |
872 | int ret = 0; | |
873 | u64 dmaaddr; | |
874 | ||
875 | if (!node) | |
876 | return -EINVAL; | |
877 | ||
878 | while (1) { | |
879 | naddr = of_n_addr_cells(node); | |
880 | nsize = of_n_size_cells(node); | |
881 | node = of_get_next_parent(node); | |
882 | if (!node) | |
883 | break; | |
884 | ||
885 | ranges = of_get_property(node, "dma-ranges", &len); | |
886 | ||
887 | /* Ignore empty ranges, they imply no translation required */ | |
888 | if (ranges && len > 0) | |
889 | break; | |
890 | ||
891 | /* | |
892 | * At least empty ranges has to be defined for parent node if | |
893 | * DMA is supported | |
894 | */ | |
895 | if (!ranges) | |
896 | break; | |
897 | } | |
898 | ||
899 | if (!ranges) { | |
900 | pr_debug("%s: no dma-ranges found for node(%s)\n", | |
901 | __func__, np->full_name); | |
902 | ret = -ENODEV; | |
903 | goto out; | |
904 | } | |
905 | ||
906 | len /= sizeof(u32); | |
907 | ||
908 | pna = of_n_addr_cells(node); | |
909 | ||
910 | /* dma-ranges format: | |
911 | * DMA addr : naddr cells | |
912 | * CPU addr : pna cells | |
913 | * size : nsize cells | |
914 | */ | |
915 | dmaaddr = of_read_number(ranges, naddr); | |
916 | *paddr = of_translate_dma_address(np, ranges); | |
917 | if (*paddr == OF_BAD_ADDR) { | |
918 | pr_err("%s: translation of DMA address(%pad) to CPU address failed node(%s)\n", | |
919 | __func__, dma_addr, np->full_name); | |
920 | ret = -EINVAL; | |
921 | goto out; | |
922 | } | |
923 | *dma_addr = dmaaddr; | |
924 | ||
925 | *size = of_read_number(ranges + naddr + pna, nsize); | |
926 | ||
927 | pr_debug("dma_addr(%llx) cpu_addr(%llx) size(%llx)\n", | |
928 | *dma_addr, *paddr, *size); | |
929 | ||
930 | out: | |
931 | of_node_put(node); | |
932 | ||
933 | return ret; | |
934 | } | |
935 | EXPORT_SYMBOL_GPL(of_dma_get_range); | |
92ea637e SS |
936 | |
937 | /** | |
938 | * of_dma_is_coherent - Check if device is coherent | |
939 | * @np: device node | |
940 | * | |
941 | * It returns true if "dma-coherent" property was found | |
942 | * for this device in DT. | |
943 | */ | |
944 | bool of_dma_is_coherent(struct device_node *np) | |
945 | { | |
946 | struct device_node *node = of_node_get(np); | |
947 | ||
948 | while (node) { | |
949 | if (of_property_read_bool(node, "dma-coherent")) { | |
950 | of_node_put(node); | |
951 | return true; | |
952 | } | |
953 | node = of_get_next_parent(node); | |
954 | } | |
955 | of_node_put(node); | |
956 | return false; | |
957 | } | |
eb3d3ec5 | 958 | EXPORT_SYMBOL_GPL(of_dma_is_coherent); |