prom_parse.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #undef DEBUG
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/pci_regs.h>
  5. #include <linux/module.h>
  6. #include <asm/prom.h>
  7. #ifdef DEBUG
  8. #define DBG(fmt...) do { printk(fmt); } while(0)
  9. #else
  10. #define DBG(fmt...) do { } while(0)
  11. #endif
  12. #ifdef CONFIG_PPC64
  13. #define PRu64 "%lx"
  14. #else
  15. #define PRu64 "%llx"
  16. #endif
  17. /* Max address size we deal with */
  18. #define OF_MAX_ADDR_CELLS 4
  19. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  20. (ns) > 0)
  21. /* Debug utility */
  22. #ifdef DEBUG
  23. static void of_dump_addr(const char *s, u32 *addr, int na)
  24. {
  25. printk("%s", s);
  26. while(na--)
  27. printk(" %08x", *(addr++));
  28. printk("\n");
  29. }
  30. #else
  31. static void of_dump_addr(const char *s, u32 *addr, int na) { }
  32. #endif
  33. /* Read a big address */
  34. static inline u64 of_read_addr(u32 *cell, int size)
  35. {
  36. u64 r = 0;
  37. while (size--)
  38. r = (r << 32) | *(cell++);
  39. return r;
  40. }
  41. /* Callbacks for bus specific translators */
  42. struct of_bus {
  43. const char *name;
  44. const char *addresses;
  45. int (*match)(struct device_node *parent);
  46. void (*count_cells)(struct device_node *child,
  47. int *addrc, int *sizec);
  48. u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna);
  49. int (*translate)(u32 *addr, u64 offset, int na);
  50. };
  51. /*
  52. * Default translator (generic bus)
  53. */
  54. static void of_default_count_cells(struct device_node *dev,
  55. int *addrc, int *sizec)
  56. {
  57. if (addrc)
  58. *addrc = prom_n_addr_cells(dev);
  59. if (sizec)
  60. *sizec = prom_n_size_cells(dev);
  61. }
  62. static u64 of_default_map(u32 *addr, u32 *range, int na, int ns, int pna)
  63. {
  64. u64 cp, s, da;
  65. cp = of_read_addr(range, na);
  66. s = of_read_addr(range + na + pna, ns);
  67. da = of_read_addr(addr, na);
  68. DBG("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  69. cp, s, da);
  70. if (da < cp || da >= (cp + s))
  71. return OF_BAD_ADDR;
  72. return da - cp;
  73. }
  74. static int of_default_translate(u32 *addr, u64 offset, int na)
  75. {
  76. u64 a = of_read_addr(addr, na);
  77. memset(addr, 0, na * 4);
  78. a += offset;
  79. if (na > 1)
  80. addr[na - 2] = a >> 32;
  81. addr[na - 1] = a & 0xffffffffu;
  82. return 0;
  83. }
  84. /*
  85. * PCI bus specific translator
  86. */
  87. static int of_bus_pci_match(struct device_node *np)
  88. {
  89. return !strcmp(np->type, "pci");
  90. }
  91. static void of_bus_pci_count_cells(struct device_node *np,
  92. int *addrc, int *sizec)
  93. {
  94. if (addrc)
  95. *addrc = 3;
  96. if (sizec)
  97. *sizec = 2;
  98. }
  99. static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna)
  100. {
  101. u64 cp, s, da;
  102. /* Check address type match */
  103. if ((addr[0] ^ range[0]) & 0x03000000)
  104. return OF_BAD_ADDR;
  105. /* Read address values, skipping high cell */
  106. cp = of_read_addr(range + 1, na - 1);
  107. s = of_read_addr(range + na + pna, ns);
  108. da = of_read_addr(addr + 1, na - 1);
  109. DBG("OF: PCI map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  110. if (da < cp || da >= (cp + s))
  111. return OF_BAD_ADDR;
  112. return da - cp;
  113. }
  114. static int of_bus_pci_translate(u32 *addr, u64 offset, int na)
  115. {
  116. return of_default_translate(addr + 1, offset, na - 1);
  117. }
  118. /*
  119. * ISA bus specific translator
  120. */
  121. static int of_bus_isa_match(struct device_node *np)
  122. {
  123. return !strcmp(np->name, "isa");
  124. }
  125. static void of_bus_isa_count_cells(struct device_node *child,
  126. int *addrc, int *sizec)
  127. {
  128. if (addrc)
  129. *addrc = 2;
  130. if (sizec)
  131. *sizec = 1;
  132. }
  133. static u64 of_bus_isa_map(u32 *addr, u32 *range, int na, int ns, int pna)
  134. {
  135. u64 cp, s, da;
  136. /* Check address type match */
  137. if ((addr[0] ^ range[0]) & 0x00000001)
  138. return OF_BAD_ADDR;
  139. /* Read address values, skipping high cell */
  140. cp = of_read_addr(range + 1, na - 1);
  141. s = of_read_addr(range + na + pna, ns);
  142. da = of_read_addr(addr + 1, na - 1);
  143. DBG("OF: ISA map, cp="PRu64", s="PRu64", da="PRu64"\n", cp, s, da);
  144. if (da < cp || da >= (cp + s))
  145. return OF_BAD_ADDR;
  146. return da - cp;
  147. }
  148. static int of_bus_isa_translate(u32 *addr, u64 offset, int na)
  149. {
  150. return of_default_translate(addr + 1, offset, na - 1);
  151. }
  152. /*
  153. * Array of bus specific translators
  154. */
  155. static struct of_bus of_busses[] = {
  156. /* PCI */
  157. {
  158. .name = "pci",
  159. .addresses = "assigned-addresses",
  160. .match = of_bus_pci_match,
  161. .count_cells = of_bus_pci_count_cells,
  162. .map = of_bus_pci_map,
  163. .translate = of_bus_pci_translate,
  164. },
  165. /* ISA */
  166. {
  167. .name = "isa",
  168. .addresses = "reg",
  169. .match = of_bus_isa_match,
  170. .count_cells = of_bus_isa_count_cells,
  171. .map = of_bus_isa_map,
  172. .translate = of_bus_isa_translate,
  173. },
  174. /* Default */
  175. {
  176. .name = "default",
  177. .addresses = "reg",
  178. .match = NULL,
  179. .count_cells = of_default_count_cells,
  180. .map = of_default_map,
  181. .translate = of_default_translate,
  182. },
  183. };
  184. static struct of_bus *of_match_bus(struct device_node *np)
  185. {
  186. int i;
  187. for (i = 0; i < ARRAY_SIZE(of_busses); i ++)
  188. if (!of_busses[i].match || of_busses[i].match(np))
  189. return &of_busses[i];
  190. BUG();
  191. return NULL;
  192. }
  193. static int of_translate_one(struct device_node *parent, struct of_bus *bus,
  194. struct of_bus *pbus, u32 *addr,
  195. int na, int ns, int pna)
  196. {
  197. u32 *ranges;
  198. unsigned int rlen;
  199. int rone;
  200. u64 offset = OF_BAD_ADDR;
  201. /* Normally, an absence of a "ranges" property means we are
  202. * crossing a non-translatable boundary, and thus the addresses
  203. * below the current not cannot be converted to CPU physical ones.
  204. * Unfortunately, while this is very clear in the spec, it's not
  205. * what Apple understood, and they do have things like /uni-n or
  206. * /ht nodes with no "ranges" property and a lot of perfectly
  207. * useable mapped devices below them. Thus we treat the absence of
  208. * "ranges" as equivalent to an empty "ranges" property which means
  209. * a 1:1 translation at that level. It's up to the caller not to try
  210. * to translate addresses that aren't supposed to be translated in
  211. * the first place. --BenH.
  212. */
  213. ranges = (u32 *)get_property(parent, "ranges", &rlen);
  214. if (ranges == NULL || rlen == 0) {
  215. offset = of_read_addr(addr, na);
  216. memset(addr, 0, pna);
  217. goto finish;
  218. }
  219. DBG("OF: walking ranges...\n");
  220. /* Now walk through the ranges */
  221. rlen /= 4;
  222. rone = na + pna + ns;
  223. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  224. offset = bus->map(addr, ranges, na, ns, pna);
  225. if (offset != OF_BAD_ADDR)
  226. break;
  227. }
  228. if (offset == OF_BAD_ADDR) {
  229. DBG("OF: not found !\n");
  230. return 1;
  231. }
  232. memcpy(addr, ranges + na, 4 * pna);
  233. finish:
  234. of_dump_addr("OF: parent translation for:", addr, pna);
  235. DBG("OF: with offset: "PRu64"\n", offset);
  236. /* Translate it into parent bus space */
  237. return pbus->translate(addr, offset, pna);
  238. }
  239. /*
  240. * Translate an address from the device-tree into a CPU physical address,
  241. * this walks up the tree and applies the various bus mappings on the
  242. * way.
  243. *
  244. * Note: We consider that crossing any level with #size-cells == 0 to mean
  245. * that translation is impossible (that is we are not dealing with a value
  246. * that can be mapped to a cpu physical address). This is not really specified
  247. * that way, but this is traditionally the way IBM at least do things
  248. */
  249. u64 of_translate_address(struct device_node *dev, u32 *in_addr)
  250. {
  251. struct device_node *parent = NULL;
  252. struct of_bus *bus, *pbus;
  253. u32 addr[OF_MAX_ADDR_CELLS];
  254. int na, ns, pna, pns;
  255. u64 result = OF_BAD_ADDR;
  256. DBG("OF: ** translation for device %s **\n", dev->full_name);
  257. /* Increase refcount at current level */
  258. of_node_get(dev);
  259. /* Get parent & match bus type */
  260. parent = of_get_parent(dev);
  261. if (parent == NULL)
  262. goto bail;
  263. bus = of_match_bus(parent);
  264. /* Cound address cells & copy address locally */
  265. bus->count_cells(dev, &na, &ns);
  266. if (!OF_CHECK_COUNTS(na, ns)) {
  267. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  268. dev->full_name);
  269. goto bail;
  270. }
  271. memcpy(addr, in_addr, na * 4);
  272. DBG("OF: bus is %s (na=%d, ns=%d) on %s\n",
  273. bus->name, na, ns, parent->full_name);
  274. of_dump_addr("OF: translating address:", addr, na);
  275. /* Translate */
  276. for (;;) {
  277. /* Switch to parent bus */
  278. of_node_put(dev);
  279. dev = parent;
  280. parent = of_get_parent(dev);
  281. /* If root, we have finished */
  282. if (parent == NULL) {
  283. DBG("OF: reached root node\n");
  284. result = of_read_addr(addr, na);
  285. break;
  286. }
  287. /* Get new parent bus and counts */
  288. pbus = of_match_bus(parent);
  289. pbus->count_cells(dev, &pna, &pns);
  290. if (!OF_CHECK_COUNTS(pna, pns)) {
  291. printk(KERN_ERR "prom_parse: Bad cell count for %s\n",
  292. dev->full_name);
  293. break;
  294. }
  295. DBG("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  296. pbus->name, pna, pns, parent->full_name);
  297. /* Apply bus translation */
  298. if (of_translate_one(dev, bus, pbus, addr, na, ns, pna))
  299. break;
  300. /* Complete the move up one level */
  301. na = pna;
  302. ns = pns;
  303. bus = pbus;
  304. of_dump_addr("OF: one level translation:", addr, na);
  305. }
  306. bail:
  307. of_node_put(parent);
  308. of_node_put(dev);
  309. return result;
  310. }
  311. EXPORT_SYMBOL(of_translate_address);
  312. u32 *of_get_address(struct device_node *dev, int index, u64 *size)
  313. {
  314. u32 *prop;
  315. unsigned int psize;
  316. struct device_node *parent;
  317. struct of_bus *bus;
  318. int onesize, i, na, ns;
  319. /* Get parent & match bus type */
  320. parent = of_get_parent(dev);
  321. if (parent == NULL)
  322. return NULL;
  323. bus = of_match_bus(parent);
  324. bus->count_cells(dev, &na, &ns);
  325. of_node_put(parent);
  326. if (!OF_CHECK_COUNTS(na, ns))
  327. return NULL;
  328. /* Get "reg" or "assigned-addresses" property */
  329. prop = (u32 *)get_property(dev, bus->addresses, &psize);
  330. if (prop == NULL)
  331. return NULL;
  332. psize /= 4;
  333. onesize = na + ns;
  334. for (i = 0; psize >= onesize; psize -= onesize, prop += onesize, i++)
  335. if (i == index) {
  336. if (size)
  337. *size = of_read_addr(prop + na, ns);
  338. return prop;
  339. }
  340. return NULL;
  341. }
  342. EXPORT_SYMBOL(of_get_address);
  343. u32 *of_get_pci_address(struct device_node *dev, int bar_no, u64 *size)
  344. {
  345. u32 *addr;
  346. int index;
  347. for (index = 0; (addr = of_get_address(dev, index, size)) != NULL;
  348. index++) {
  349. if ((addr[0] & 0xff) == ((bar_no * 4) + PCI_BASE_ADDRESS_0))
  350. return addr;
  351. }
  352. return NULL;
  353. }
  354. EXPORT_SYMBOL(of_get_pci_address);