prom_64.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/lmb.h>
  23. #include <linux/of_device.h>
  24. #include <asm/prom.h>
  25. #include <asm/oplib.h>
  26. #include <asm/irq.h>
  27. #include <asm/asi.h>
  28. #include <asm/upa.h>
  29. #include <asm/smp.h>
  30. #include "prom.h"
  31. static unsigned int prom_early_allocated __initdata;
  32. static void * __init prom_early_alloc(unsigned long size)
  33. {
  34. unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
  35. void *ret;
  36. if (!paddr) {
  37. prom_printf("prom_early_alloc(%lu) failed\n");
  38. prom_halt();
  39. }
  40. ret = __va(paddr);
  41. memset(ret, 0, size);
  42. prom_early_allocated += size;
  43. return ret;
  44. }
  45. #ifdef CONFIG_PCI
  46. /* PSYCHO interrupt mapping support. */
  47. #define PSYCHO_IMAP_A_SLOT0 0x0c00UL
  48. #define PSYCHO_IMAP_B_SLOT0 0x0c20UL
  49. static unsigned long psycho_pcislot_imap_offset(unsigned long ino)
  50. {
  51. unsigned int bus = (ino & 0x10) >> 4;
  52. unsigned int slot = (ino & 0x0c) >> 2;
  53. if (bus == 0)
  54. return PSYCHO_IMAP_A_SLOT0 + (slot * 8);
  55. else
  56. return PSYCHO_IMAP_B_SLOT0 + (slot * 8);
  57. }
  58. #define PSYCHO_OBIO_IMAP_BASE 0x1000UL
  59. #define PSYCHO_ONBOARD_IRQ_BASE 0x20
  60. #define psycho_onboard_imap_offset(__ino) \
  61. (PSYCHO_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
  62. #define PSYCHO_ICLR_A_SLOT0 0x1400UL
  63. #define PSYCHO_ICLR_SCSI 0x1800UL
  64. #define psycho_iclr_offset(ino) \
  65. ((ino & 0x20) ? (PSYCHO_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
  66. (PSYCHO_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
  67. static unsigned int psycho_irq_build(struct device_node *dp,
  68. unsigned int ino,
  69. void *_data)
  70. {
  71. unsigned long controller_regs = (unsigned long) _data;
  72. unsigned long imap, iclr;
  73. unsigned long imap_off, iclr_off;
  74. int inofixup = 0;
  75. ino &= 0x3f;
  76. if (ino < PSYCHO_ONBOARD_IRQ_BASE) {
  77. /* PCI slot */
  78. imap_off = psycho_pcislot_imap_offset(ino);
  79. } else {
  80. /* Onboard device */
  81. imap_off = psycho_onboard_imap_offset(ino);
  82. }
  83. /* Now build the IRQ bucket. */
  84. imap = controller_regs + imap_off;
  85. iclr_off = psycho_iclr_offset(ino);
  86. iclr = controller_regs + iclr_off;
  87. if ((ino & 0x20) == 0)
  88. inofixup = ino & 0x03;
  89. return build_irq(inofixup, iclr, imap);
  90. }
  91. static void __init psycho_irq_trans_init(struct device_node *dp)
  92. {
  93. const struct linux_prom64_registers *regs;
  94. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  95. dp->irq_trans->irq_build = psycho_irq_build;
  96. regs = of_get_property(dp, "reg", NULL);
  97. dp->irq_trans->data = (void *) regs[2].phys_addr;
  98. }
  99. #define sabre_read(__reg) \
  100. ({ u64 __ret; \
  101. __asm__ __volatile__("ldxa [%1] %2, %0" \
  102. : "=r" (__ret) \
  103. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  104. : "memory"); \
  105. __ret; \
  106. })
  107. struct sabre_irq_data {
  108. unsigned long controller_regs;
  109. unsigned int pci_first_busno;
  110. };
  111. #define SABRE_CONFIGSPACE 0x001000000UL
  112. #define SABRE_WRSYNC 0x1c20UL
  113. #define SABRE_CONFIG_BASE(CONFIG_SPACE) \
  114. (CONFIG_SPACE | (1UL << 24))
  115. #define SABRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
  116. (((unsigned long)(BUS) << 16) | \
  117. ((unsigned long)(DEVFN) << 8) | \
  118. ((unsigned long)(REG)))
  119. /* When a device lives behind a bridge deeper in the PCI bus topology
  120. * than APB, a special sequence must run to make sure all pending DMA
  121. * transfers at the time of IRQ delivery are visible in the coherency
  122. * domain by the cpu. This sequence is to perform a read on the far
  123. * side of the non-APB bridge, then perform a read of Sabre's DMA
  124. * write-sync register.
  125. */
  126. static void sabre_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
  127. {
  128. unsigned int phys_hi = (unsigned int) (unsigned long) _arg1;
  129. struct sabre_irq_data *irq_data = _arg2;
  130. unsigned long controller_regs = irq_data->controller_regs;
  131. unsigned long sync_reg = controller_regs + SABRE_WRSYNC;
  132. unsigned long config_space = controller_regs + SABRE_CONFIGSPACE;
  133. unsigned int bus, devfn;
  134. u16 _unused;
  135. config_space = SABRE_CONFIG_BASE(config_space);
  136. bus = (phys_hi >> 16) & 0xff;
  137. devfn = (phys_hi >> 8) & 0xff;
  138. config_space |= SABRE_CONFIG_ENCODE(bus, devfn, 0x00);
  139. __asm__ __volatile__("membar #Sync\n\t"
  140. "lduha [%1] %2, %0\n\t"
  141. "membar #Sync"
  142. : "=r" (_unused)
  143. : "r" ((u16 *) config_space),
  144. "i" (ASI_PHYS_BYPASS_EC_E_L)
  145. : "memory");
  146. sabre_read(sync_reg);
  147. }
  148. #define SABRE_IMAP_A_SLOT0 0x0c00UL
  149. #define SABRE_IMAP_B_SLOT0 0x0c20UL
  150. #define SABRE_ICLR_A_SLOT0 0x1400UL
  151. #define SABRE_ICLR_B_SLOT0 0x1480UL
  152. #define SABRE_ICLR_SCSI 0x1800UL
  153. #define SABRE_ICLR_ETH 0x1808UL
  154. #define SABRE_ICLR_BPP 0x1810UL
  155. #define SABRE_ICLR_AU_REC 0x1818UL
  156. #define SABRE_ICLR_AU_PLAY 0x1820UL
  157. #define SABRE_ICLR_PFAIL 0x1828UL
  158. #define SABRE_ICLR_KMS 0x1830UL
  159. #define SABRE_ICLR_FLPY 0x1838UL
  160. #define SABRE_ICLR_SHW 0x1840UL
  161. #define SABRE_ICLR_KBD 0x1848UL
  162. #define SABRE_ICLR_MS 0x1850UL
  163. #define SABRE_ICLR_SER 0x1858UL
  164. #define SABRE_ICLR_UE 0x1870UL
  165. #define SABRE_ICLR_CE 0x1878UL
  166. #define SABRE_ICLR_PCIERR 0x1880UL
  167. static unsigned long sabre_pcislot_imap_offset(unsigned long ino)
  168. {
  169. unsigned int bus = (ino & 0x10) >> 4;
  170. unsigned int slot = (ino & 0x0c) >> 2;
  171. if (bus == 0)
  172. return SABRE_IMAP_A_SLOT0 + (slot * 8);
  173. else
  174. return SABRE_IMAP_B_SLOT0 + (slot * 8);
  175. }
  176. #define SABRE_OBIO_IMAP_BASE 0x1000UL
  177. #define SABRE_ONBOARD_IRQ_BASE 0x20
  178. #define sabre_onboard_imap_offset(__ino) \
  179. (SABRE_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
  180. #define sabre_iclr_offset(ino) \
  181. ((ino & 0x20) ? (SABRE_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
  182. (SABRE_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
  183. static int sabre_device_needs_wsync(struct device_node *dp)
  184. {
  185. struct device_node *parent = dp->parent;
  186. const char *parent_model, *parent_compat;
  187. /* This traversal up towards the root is meant to
  188. * handle two cases:
  189. *
  190. * 1) non-PCI bus sitting under PCI, such as 'ebus'
  191. * 2) the PCI controller interrupts themselves, which
  192. * will use the sabre_irq_build but do not need
  193. * the DMA synchronization handling
  194. */
  195. while (parent) {
  196. if (!strcmp(parent->type, "pci"))
  197. break;
  198. parent = parent->parent;
  199. }
  200. if (!parent)
  201. return 0;
  202. parent_model = of_get_property(parent,
  203. "model", NULL);
  204. if (parent_model &&
  205. (!strcmp(parent_model, "SUNW,sabre") ||
  206. !strcmp(parent_model, "SUNW,simba")))
  207. return 0;
  208. parent_compat = of_get_property(parent,
  209. "compatible", NULL);
  210. if (parent_compat &&
  211. (!strcmp(parent_compat, "pci108e,a000") ||
  212. !strcmp(parent_compat, "pci108e,a001")))
  213. return 0;
  214. return 1;
  215. }
  216. static unsigned int sabre_irq_build(struct device_node *dp,
  217. unsigned int ino,
  218. void *_data)
  219. {
  220. struct sabre_irq_data *irq_data = _data;
  221. unsigned long controller_regs = irq_data->controller_regs;
  222. const struct linux_prom_pci_registers *regs;
  223. unsigned long imap, iclr;
  224. unsigned long imap_off, iclr_off;
  225. int inofixup = 0;
  226. int virt_irq;
  227. ino &= 0x3f;
  228. if (ino < SABRE_ONBOARD_IRQ_BASE) {
  229. /* PCI slot */
  230. imap_off = sabre_pcislot_imap_offset(ino);
  231. } else {
  232. /* onboard device */
  233. imap_off = sabre_onboard_imap_offset(ino);
  234. }
  235. /* Now build the IRQ bucket. */
  236. imap = controller_regs + imap_off;
  237. iclr_off = sabre_iclr_offset(ino);
  238. iclr = controller_regs + iclr_off;
  239. if ((ino & 0x20) == 0)
  240. inofixup = ino & 0x03;
  241. virt_irq = build_irq(inofixup, iclr, imap);
  242. /* If the parent device is a PCI<->PCI bridge other than
  243. * APB, we have to install a pre-handler to ensure that
  244. * all pending DMA is drained before the interrupt handler
  245. * is run.
  246. */
  247. regs = of_get_property(dp, "reg", NULL);
  248. if (regs && sabre_device_needs_wsync(dp)) {
  249. irq_install_pre_handler(virt_irq,
  250. sabre_wsync_handler,
  251. (void *) (long) regs->phys_hi,
  252. (void *) irq_data);
  253. }
  254. return virt_irq;
  255. }
  256. static void __init sabre_irq_trans_init(struct device_node *dp)
  257. {
  258. const struct linux_prom64_registers *regs;
  259. struct sabre_irq_data *irq_data;
  260. const u32 *busrange;
  261. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  262. dp->irq_trans->irq_build = sabre_irq_build;
  263. irq_data = prom_early_alloc(sizeof(struct sabre_irq_data));
  264. regs = of_get_property(dp, "reg", NULL);
  265. irq_data->controller_regs = regs[0].phys_addr;
  266. busrange = of_get_property(dp, "bus-range", NULL);
  267. irq_data->pci_first_busno = busrange[0];
  268. dp->irq_trans->data = irq_data;
  269. }
  270. /* SCHIZO interrupt mapping support. Unlike Psycho, for this controller the
  271. * imap/iclr registers are per-PBM.
  272. */
  273. #define SCHIZO_IMAP_BASE 0x1000UL
  274. #define SCHIZO_ICLR_BASE 0x1400UL
  275. static unsigned long schizo_imap_offset(unsigned long ino)
  276. {
  277. return SCHIZO_IMAP_BASE + (ino * 8UL);
  278. }
  279. static unsigned long schizo_iclr_offset(unsigned long ino)
  280. {
  281. return SCHIZO_ICLR_BASE + (ino * 8UL);
  282. }
  283. static unsigned long schizo_ino_to_iclr(unsigned long pbm_regs,
  284. unsigned int ino)
  285. {
  286. return pbm_regs + schizo_iclr_offset(ino);
  287. }
  288. static unsigned long schizo_ino_to_imap(unsigned long pbm_regs,
  289. unsigned int ino)
  290. {
  291. return pbm_regs + schizo_imap_offset(ino);
  292. }
  293. #define schizo_read(__reg) \
  294. ({ u64 __ret; \
  295. __asm__ __volatile__("ldxa [%1] %2, %0" \
  296. : "=r" (__ret) \
  297. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  298. : "memory"); \
  299. __ret; \
  300. })
  301. #define schizo_write(__reg, __val) \
  302. __asm__ __volatile__("stxa %0, [%1] %2" \
  303. : /* no outputs */ \
  304. : "r" (__val), "r" (__reg), \
  305. "i" (ASI_PHYS_BYPASS_EC_E) \
  306. : "memory")
  307. static void tomatillo_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
  308. {
  309. unsigned long sync_reg = (unsigned long) _arg2;
  310. u64 mask = 1UL << (ino & IMAP_INO);
  311. u64 val;
  312. int limit;
  313. schizo_write(sync_reg, mask);
  314. limit = 100000;
  315. val = 0;
  316. while (--limit) {
  317. val = schizo_read(sync_reg);
  318. if (!(val & mask))
  319. break;
  320. }
  321. if (limit <= 0) {
  322. printk("tomatillo_wsync_handler: DMA won't sync [%lx:%lx]\n",
  323. val, mask);
  324. }
  325. if (_arg1) {
  326. static unsigned char cacheline[64]
  327. __attribute__ ((aligned (64)));
  328. __asm__ __volatile__("rd %%fprs, %0\n\t"
  329. "or %0, %4, %1\n\t"
  330. "wr %1, 0x0, %%fprs\n\t"
  331. "stda %%f0, [%5] %6\n\t"
  332. "wr %0, 0x0, %%fprs\n\t"
  333. "membar #Sync"
  334. : "=&r" (mask), "=&r" (val)
  335. : "0" (mask), "1" (val),
  336. "i" (FPRS_FEF), "r" (&cacheline[0]),
  337. "i" (ASI_BLK_COMMIT_P));
  338. }
  339. }
  340. struct schizo_irq_data {
  341. unsigned long pbm_regs;
  342. unsigned long sync_reg;
  343. u32 portid;
  344. int chip_version;
  345. };
  346. static unsigned int schizo_irq_build(struct device_node *dp,
  347. unsigned int ino,
  348. void *_data)
  349. {
  350. struct schizo_irq_data *irq_data = _data;
  351. unsigned long pbm_regs = irq_data->pbm_regs;
  352. unsigned long imap, iclr;
  353. int ign_fixup;
  354. int virt_irq;
  355. int is_tomatillo;
  356. ino &= 0x3f;
  357. /* Now build the IRQ bucket. */
  358. imap = schizo_ino_to_imap(pbm_regs, ino);
  359. iclr = schizo_ino_to_iclr(pbm_regs, ino);
  360. /* On Schizo, no inofixup occurs. This is because each
  361. * INO has it's own IMAP register. On Psycho and Sabre
  362. * there is only one IMAP register for each PCI slot even
  363. * though four different INOs can be generated by each
  364. * PCI slot.
  365. *
  366. * But, for JBUS variants (essentially, Tomatillo), we have
  367. * to fixup the lowest bit of the interrupt group number.
  368. */
  369. ign_fixup = 0;
  370. is_tomatillo = (irq_data->sync_reg != 0UL);
  371. if (is_tomatillo) {
  372. if (irq_data->portid & 1)
  373. ign_fixup = (1 << 6);
  374. }
  375. virt_irq = build_irq(ign_fixup, iclr, imap);
  376. if (is_tomatillo) {
  377. irq_install_pre_handler(virt_irq,
  378. tomatillo_wsync_handler,
  379. ((irq_data->chip_version <= 4) ?
  380. (void *) 1 : (void *) 0),
  381. (void *) irq_data->sync_reg);
  382. }
  383. return virt_irq;
  384. }
  385. static void __init __schizo_irq_trans_init(struct device_node *dp,
  386. int is_tomatillo)
  387. {
  388. const struct linux_prom64_registers *regs;
  389. struct schizo_irq_data *irq_data;
  390. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  391. dp->irq_trans->irq_build = schizo_irq_build;
  392. irq_data = prom_early_alloc(sizeof(struct schizo_irq_data));
  393. regs = of_get_property(dp, "reg", NULL);
  394. dp->irq_trans->data = irq_data;
  395. irq_data->pbm_regs = regs[0].phys_addr;
  396. if (is_tomatillo)
  397. irq_data->sync_reg = regs[3].phys_addr + 0x1a18UL;
  398. else
  399. irq_data->sync_reg = 0UL;
  400. irq_data->portid = of_getintprop_default(dp, "portid", 0);
  401. irq_data->chip_version = of_getintprop_default(dp, "version#", 0);
  402. }
  403. static void __init schizo_irq_trans_init(struct device_node *dp)
  404. {
  405. __schizo_irq_trans_init(dp, 0);
  406. }
  407. static void __init tomatillo_irq_trans_init(struct device_node *dp)
  408. {
  409. __schizo_irq_trans_init(dp, 1);
  410. }
  411. static unsigned int pci_sun4v_irq_build(struct device_node *dp,
  412. unsigned int devino,
  413. void *_data)
  414. {
  415. u32 devhandle = (u32) (unsigned long) _data;
  416. return sun4v_build_irq(devhandle, devino);
  417. }
  418. static void __init pci_sun4v_irq_trans_init(struct device_node *dp)
  419. {
  420. const struct linux_prom64_registers *regs;
  421. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  422. dp->irq_trans->irq_build = pci_sun4v_irq_build;
  423. regs = of_get_property(dp, "reg", NULL);
  424. dp->irq_trans->data = (void *) (unsigned long)
  425. ((regs->phys_addr >> 32UL) & 0x0fffffff);
  426. }
  427. struct fire_irq_data {
  428. unsigned long pbm_regs;
  429. u32 portid;
  430. };
  431. #define FIRE_IMAP_BASE 0x001000
  432. #define FIRE_ICLR_BASE 0x001400
  433. static unsigned long fire_imap_offset(unsigned long ino)
  434. {
  435. return FIRE_IMAP_BASE + (ino * 8UL);
  436. }
  437. static unsigned long fire_iclr_offset(unsigned long ino)
  438. {
  439. return FIRE_ICLR_BASE + (ino * 8UL);
  440. }
  441. static unsigned long fire_ino_to_iclr(unsigned long pbm_regs,
  442. unsigned int ino)
  443. {
  444. return pbm_regs + fire_iclr_offset(ino);
  445. }
  446. static unsigned long fire_ino_to_imap(unsigned long pbm_regs,
  447. unsigned int ino)
  448. {
  449. return pbm_regs + fire_imap_offset(ino);
  450. }
  451. static unsigned int fire_irq_build(struct device_node *dp,
  452. unsigned int ino,
  453. void *_data)
  454. {
  455. struct fire_irq_data *irq_data = _data;
  456. unsigned long pbm_regs = irq_data->pbm_regs;
  457. unsigned long imap, iclr;
  458. unsigned long int_ctrlr;
  459. ino &= 0x3f;
  460. /* Now build the IRQ bucket. */
  461. imap = fire_ino_to_imap(pbm_regs, ino);
  462. iclr = fire_ino_to_iclr(pbm_regs, ino);
  463. /* Set the interrupt controller number. */
  464. int_ctrlr = 1 << 6;
  465. upa_writeq(int_ctrlr, imap);
  466. /* The interrupt map registers do not have an INO field
  467. * like other chips do. They return zero in the INO
  468. * field, and the interrupt controller number is controlled
  469. * in bits 6 to 9. So in order for build_irq() to get
  470. * the INO right we pass it in as part of the fixup
  471. * which will get added to the map register zero value
  472. * read by build_irq().
  473. */
  474. ino |= (irq_data->portid << 6);
  475. ino -= int_ctrlr;
  476. return build_irq(ino, iclr, imap);
  477. }
  478. static void __init fire_irq_trans_init(struct device_node *dp)
  479. {
  480. const struct linux_prom64_registers *regs;
  481. struct fire_irq_data *irq_data;
  482. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  483. dp->irq_trans->irq_build = fire_irq_build;
  484. irq_data = prom_early_alloc(sizeof(struct fire_irq_data));
  485. regs = of_get_property(dp, "reg", NULL);
  486. dp->irq_trans->data = irq_data;
  487. irq_data->pbm_regs = regs[0].phys_addr;
  488. irq_data->portid = of_getintprop_default(dp, "portid", 0);
  489. }
  490. #endif /* CONFIG_PCI */
  491. #ifdef CONFIG_SBUS
  492. /* INO number to IMAP register offset for SYSIO external IRQ's.
  493. * This should conform to both Sunfire/Wildfire server and Fusion
  494. * desktop designs.
  495. */
  496. #define SYSIO_IMAP_SLOT0 0x2c00UL
  497. #define SYSIO_IMAP_SLOT1 0x2c08UL
  498. #define SYSIO_IMAP_SLOT2 0x2c10UL
  499. #define SYSIO_IMAP_SLOT3 0x2c18UL
  500. #define SYSIO_IMAP_SCSI 0x3000UL
  501. #define SYSIO_IMAP_ETH 0x3008UL
  502. #define SYSIO_IMAP_BPP 0x3010UL
  503. #define SYSIO_IMAP_AUDIO 0x3018UL
  504. #define SYSIO_IMAP_PFAIL 0x3020UL
  505. #define SYSIO_IMAP_KMS 0x3028UL
  506. #define SYSIO_IMAP_FLPY 0x3030UL
  507. #define SYSIO_IMAP_SHW 0x3038UL
  508. #define SYSIO_IMAP_KBD 0x3040UL
  509. #define SYSIO_IMAP_MS 0x3048UL
  510. #define SYSIO_IMAP_SER 0x3050UL
  511. #define SYSIO_IMAP_TIM0 0x3060UL
  512. #define SYSIO_IMAP_TIM1 0x3068UL
  513. #define SYSIO_IMAP_UE 0x3070UL
  514. #define SYSIO_IMAP_CE 0x3078UL
  515. #define SYSIO_IMAP_SBERR 0x3080UL
  516. #define SYSIO_IMAP_PMGMT 0x3088UL
  517. #define SYSIO_IMAP_GFX 0x3090UL
  518. #define SYSIO_IMAP_EUPA 0x3098UL
  519. #define bogon ((unsigned long) -1)
  520. static unsigned long sysio_irq_offsets[] = {
  521. /* SBUS Slot 0 --> 3, level 1 --> 7 */
  522. SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
  523. SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
  524. SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
  525. SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
  526. SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
  527. SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
  528. SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
  529. SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
  530. /* Onboard devices (not relevant/used on SunFire). */
  531. SYSIO_IMAP_SCSI,
  532. SYSIO_IMAP_ETH,
  533. SYSIO_IMAP_BPP,
  534. bogon,
  535. SYSIO_IMAP_AUDIO,
  536. SYSIO_IMAP_PFAIL,
  537. bogon,
  538. bogon,
  539. SYSIO_IMAP_KMS,
  540. SYSIO_IMAP_FLPY,
  541. SYSIO_IMAP_SHW,
  542. SYSIO_IMAP_KBD,
  543. SYSIO_IMAP_MS,
  544. SYSIO_IMAP_SER,
  545. bogon,
  546. bogon,
  547. SYSIO_IMAP_TIM0,
  548. SYSIO_IMAP_TIM1,
  549. bogon,
  550. bogon,
  551. SYSIO_IMAP_UE,
  552. SYSIO_IMAP_CE,
  553. SYSIO_IMAP_SBERR,
  554. SYSIO_IMAP_PMGMT,
  555. SYSIO_IMAP_GFX,
  556. SYSIO_IMAP_EUPA,
  557. };
  558. #undef bogon
  559. #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
  560. /* Convert Interrupt Mapping register pointer to associated
  561. * Interrupt Clear register pointer, SYSIO specific version.
  562. */
  563. #define SYSIO_ICLR_UNUSED0 0x3400UL
  564. #define SYSIO_ICLR_SLOT0 0x3408UL
  565. #define SYSIO_ICLR_SLOT1 0x3448UL
  566. #define SYSIO_ICLR_SLOT2 0x3488UL
  567. #define SYSIO_ICLR_SLOT3 0x34c8UL
  568. static unsigned long sysio_imap_to_iclr(unsigned long imap)
  569. {
  570. unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
  571. return imap + diff;
  572. }
  573. static unsigned int sbus_of_build_irq(struct device_node *dp,
  574. unsigned int ino,
  575. void *_data)
  576. {
  577. unsigned long reg_base = (unsigned long) _data;
  578. const struct linux_prom_registers *regs;
  579. unsigned long imap, iclr;
  580. int sbus_slot = 0;
  581. int sbus_level = 0;
  582. ino &= 0x3f;
  583. regs = of_get_property(dp, "reg", NULL);
  584. if (regs)
  585. sbus_slot = regs->which_io;
  586. if (ino < 0x20)
  587. ino += (sbus_slot * 8);
  588. imap = sysio_irq_offsets[ino];
  589. if (imap == ((unsigned long)-1)) {
  590. prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
  591. ino);
  592. prom_halt();
  593. }
  594. imap += reg_base;
  595. /* SYSIO inconsistency. For external SLOTS, we have to select
  596. * the right ICLR register based upon the lower SBUS irq level
  597. * bits.
  598. */
  599. if (ino >= 0x20) {
  600. iclr = sysio_imap_to_iclr(imap);
  601. } else {
  602. sbus_level = ino & 0x7;
  603. switch(sbus_slot) {
  604. case 0:
  605. iclr = reg_base + SYSIO_ICLR_SLOT0;
  606. break;
  607. case 1:
  608. iclr = reg_base + SYSIO_ICLR_SLOT1;
  609. break;
  610. case 2:
  611. iclr = reg_base + SYSIO_ICLR_SLOT2;
  612. break;
  613. default:
  614. case 3:
  615. iclr = reg_base + SYSIO_ICLR_SLOT3;
  616. break;
  617. };
  618. iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
  619. }
  620. return build_irq(sbus_level, iclr, imap);
  621. }
  622. static void __init sbus_irq_trans_init(struct device_node *dp)
  623. {
  624. const struct linux_prom64_registers *regs;
  625. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  626. dp->irq_trans->irq_build = sbus_of_build_irq;
  627. regs = of_get_property(dp, "reg", NULL);
  628. dp->irq_trans->data = (void *) (unsigned long) regs->phys_addr;
  629. }
  630. #endif /* CONFIG_SBUS */
  631. static unsigned int central_build_irq(struct device_node *dp,
  632. unsigned int ino,
  633. void *_data)
  634. {
  635. struct device_node *central_dp = _data;
  636. struct of_device *central_op = of_find_device_by_node(central_dp);
  637. struct resource *res;
  638. unsigned long imap, iclr;
  639. u32 tmp;
  640. if (!strcmp(dp->name, "eeprom")) {
  641. res = &central_op->resource[5];
  642. } else if (!strcmp(dp->name, "zs")) {
  643. res = &central_op->resource[4];
  644. } else if (!strcmp(dp->name, "clock-board")) {
  645. res = &central_op->resource[3];
  646. } else {
  647. return ino;
  648. }
  649. imap = res->start + 0x00UL;
  650. iclr = res->start + 0x10UL;
  651. /* Set the INO state to idle, and disable. */
  652. upa_writel(0, iclr);
  653. upa_readl(iclr);
  654. tmp = upa_readl(imap);
  655. tmp &= ~0x80000000;
  656. upa_writel(tmp, imap);
  657. return build_irq(0, iclr, imap);
  658. }
  659. static void __init central_irq_trans_init(struct device_node *dp)
  660. {
  661. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  662. dp->irq_trans->irq_build = central_build_irq;
  663. dp->irq_trans->data = dp;
  664. }
  665. struct irq_trans {
  666. const char *name;
  667. void (*init)(struct device_node *);
  668. };
  669. #ifdef CONFIG_PCI
  670. static struct irq_trans __initdata pci_irq_trans_table[] = {
  671. { "SUNW,sabre", sabre_irq_trans_init },
  672. { "pci108e,a000", sabre_irq_trans_init },
  673. { "pci108e,a001", sabre_irq_trans_init },
  674. { "SUNW,psycho", psycho_irq_trans_init },
  675. { "pci108e,8000", psycho_irq_trans_init },
  676. { "SUNW,schizo", schizo_irq_trans_init },
  677. { "pci108e,8001", schizo_irq_trans_init },
  678. { "SUNW,schizo+", schizo_irq_trans_init },
  679. { "pci108e,8002", schizo_irq_trans_init },
  680. { "SUNW,tomatillo", tomatillo_irq_trans_init },
  681. { "pci108e,a801", tomatillo_irq_trans_init },
  682. { "SUNW,sun4v-pci", pci_sun4v_irq_trans_init },
  683. { "pciex108e,80f0", fire_irq_trans_init },
  684. };
  685. #endif
  686. static unsigned int sun4v_vdev_irq_build(struct device_node *dp,
  687. unsigned int devino,
  688. void *_data)
  689. {
  690. u32 devhandle = (u32) (unsigned long) _data;
  691. return sun4v_build_irq(devhandle, devino);
  692. }
  693. static void __init sun4v_vdev_irq_trans_init(struct device_node *dp)
  694. {
  695. const struct linux_prom64_registers *regs;
  696. dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
  697. dp->irq_trans->irq_build = sun4v_vdev_irq_build;
  698. regs = of_get_property(dp, "reg", NULL);
  699. dp->irq_trans->data = (void *) (unsigned long)
  700. ((regs->phys_addr >> 32UL) & 0x0fffffff);
  701. }
  702. static void __init irq_trans_init(struct device_node *dp)
  703. {
  704. #ifdef CONFIG_PCI
  705. const char *model;
  706. int i;
  707. #endif
  708. #ifdef CONFIG_PCI
  709. model = of_get_property(dp, "model", NULL);
  710. if (!model)
  711. model = of_get_property(dp, "compatible", NULL);
  712. if (model) {
  713. for (i = 0; i < ARRAY_SIZE(pci_irq_trans_table); i++) {
  714. struct irq_trans *t = &pci_irq_trans_table[i];
  715. if (!strcmp(model, t->name)) {
  716. t->init(dp);
  717. return;
  718. }
  719. }
  720. }
  721. #endif
  722. #ifdef CONFIG_SBUS
  723. if (!strcmp(dp->name, "sbus") ||
  724. !strcmp(dp->name, "sbi")) {
  725. sbus_irq_trans_init(dp);
  726. return;
  727. }
  728. #endif
  729. if (!strcmp(dp->name, "fhc") &&
  730. !strcmp(dp->parent->name, "central")) {
  731. central_irq_trans_init(dp);
  732. return;
  733. }
  734. if (!strcmp(dp->name, "virtual-devices") ||
  735. !strcmp(dp->name, "niu")) {
  736. sun4v_vdev_irq_trans_init(dp);
  737. return;
  738. }
  739. }
  740. static int is_root_node(const struct device_node *dp)
  741. {
  742. if (!dp)
  743. return 0;
  744. return (dp->parent == NULL);
  745. }
  746. /* The following routines deal with the black magic of fully naming a
  747. * node.
  748. *
  749. * Certain well known named nodes are just the simple name string.
  750. *
  751. * Actual devices have an address specifier appended to the base name
  752. * string, like this "foo@addr". The "addr" can be in any number of
  753. * formats, and the platform plus the type of the node determine the
  754. * format and how it is constructed.
  755. *
  756. * For children of the ROOT node, the naming convention is fixed and
  757. * determined by whether this is a sun4u or sun4v system.
  758. *
  759. * For children of other nodes, it is bus type specific. So
  760. * we walk up the tree until we discover a "device_type" property
  761. * we recognize and we go from there.
  762. *
  763. * As an example, the boot device on my workstation has a full path:
  764. *
  765. * /pci@1e,600000/ide@d/disk@0,0:c
  766. */
  767. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  768. {
  769. struct linux_prom64_registers *regs;
  770. struct property *rprop;
  771. u32 high_bits, low_bits, type;
  772. rprop = of_find_property(dp, "reg", NULL);
  773. if (!rprop)
  774. return;
  775. regs = rprop->value;
  776. if (!is_root_node(dp->parent)) {
  777. sprintf(tmp_buf, "%s@%x,%x",
  778. dp->name,
  779. (unsigned int) (regs->phys_addr >> 32UL),
  780. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  781. return;
  782. }
  783. type = regs->phys_addr >> 60UL;
  784. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  785. low_bits = (regs->phys_addr & 0xffffffffUL);
  786. if (type == 0 || type == 8) {
  787. const char *prefix = (type == 0) ? "m" : "i";
  788. if (low_bits)
  789. sprintf(tmp_buf, "%s@%s%x,%x",
  790. dp->name, prefix,
  791. high_bits, low_bits);
  792. else
  793. sprintf(tmp_buf, "%s@%s%x",
  794. dp->name,
  795. prefix,
  796. high_bits);
  797. } else if (type == 12) {
  798. sprintf(tmp_buf, "%s@%x",
  799. dp->name, high_bits);
  800. }
  801. }
  802. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  803. {
  804. struct linux_prom64_registers *regs;
  805. struct property *prop;
  806. prop = of_find_property(dp, "reg", NULL);
  807. if (!prop)
  808. return;
  809. regs = prop->value;
  810. if (!is_root_node(dp->parent)) {
  811. sprintf(tmp_buf, "%s@%x,%x",
  812. dp->name,
  813. (unsigned int) (regs->phys_addr >> 32UL),
  814. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  815. return;
  816. }
  817. prop = of_find_property(dp, "upa-portid", NULL);
  818. if (!prop)
  819. prop = of_find_property(dp, "portid", NULL);
  820. if (prop) {
  821. unsigned long mask = 0xffffffffUL;
  822. if (tlb_type >= cheetah)
  823. mask = 0x7fffff;
  824. sprintf(tmp_buf, "%s@%x,%x",
  825. dp->name,
  826. *(u32 *)prop->value,
  827. (unsigned int) (regs->phys_addr & mask));
  828. }
  829. }
  830. /* "name@slot,offset" */
  831. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  832. {
  833. struct linux_prom_registers *regs;
  834. struct property *prop;
  835. prop = of_find_property(dp, "reg", NULL);
  836. if (!prop)
  837. return;
  838. regs = prop->value;
  839. sprintf(tmp_buf, "%s@%x,%x",
  840. dp->name,
  841. regs->which_io,
  842. regs->phys_addr);
  843. }
  844. /* "name@devnum[,func]" */
  845. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  846. {
  847. struct linux_prom_pci_registers *regs;
  848. struct property *prop;
  849. unsigned int devfn;
  850. prop = of_find_property(dp, "reg", NULL);
  851. if (!prop)
  852. return;
  853. regs = prop->value;
  854. devfn = (regs->phys_hi >> 8) & 0xff;
  855. if (devfn & 0x07) {
  856. sprintf(tmp_buf, "%s@%x,%x",
  857. dp->name,
  858. devfn >> 3,
  859. devfn & 0x07);
  860. } else {
  861. sprintf(tmp_buf, "%s@%x",
  862. dp->name,
  863. devfn >> 3);
  864. }
  865. }
  866. /* "name@UPA_PORTID,offset" */
  867. static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
  868. {
  869. struct linux_prom64_registers *regs;
  870. struct property *prop;
  871. prop = of_find_property(dp, "reg", NULL);
  872. if (!prop)
  873. return;
  874. regs = prop->value;
  875. prop = of_find_property(dp, "upa-portid", NULL);
  876. if (!prop)
  877. return;
  878. sprintf(tmp_buf, "%s@%x,%x",
  879. dp->name,
  880. *(u32 *) prop->value,
  881. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  882. }
  883. /* "name@reg" */
  884. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  885. {
  886. struct property *prop;
  887. u32 *regs;
  888. prop = of_find_property(dp, "reg", NULL);
  889. if (!prop)
  890. return;
  891. regs = prop->value;
  892. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  893. }
  894. /* "name@addrhi,addrlo" */
  895. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  896. {
  897. struct linux_prom64_registers *regs;
  898. struct property *prop;
  899. prop = of_find_property(dp, "reg", NULL);
  900. if (!prop)
  901. return;
  902. regs = prop->value;
  903. sprintf(tmp_buf, "%s@%x,%x",
  904. dp->name,
  905. (unsigned int) (regs->phys_addr >> 32UL),
  906. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  907. }
  908. /* "name@bus,addr" */
  909. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  910. {
  911. struct property *prop;
  912. u32 *regs;
  913. prop = of_find_property(dp, "reg", NULL);
  914. if (!prop)
  915. return;
  916. regs = prop->value;
  917. /* This actually isn't right... should look at the #address-cells
  918. * property of the i2c bus node etc. etc.
  919. */
  920. sprintf(tmp_buf, "%s@%x,%x",
  921. dp->name, regs[0], regs[1]);
  922. }
  923. /* "name@reg0[,reg1]" */
  924. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  925. {
  926. struct property *prop;
  927. u32 *regs;
  928. prop = of_find_property(dp, "reg", NULL);
  929. if (!prop)
  930. return;
  931. regs = prop->value;
  932. if (prop->length == sizeof(u32) || regs[1] == 1) {
  933. sprintf(tmp_buf, "%s@%x",
  934. dp->name, regs[0]);
  935. } else {
  936. sprintf(tmp_buf, "%s@%x,%x",
  937. dp->name, regs[0], regs[1]);
  938. }
  939. }
  940. /* "name@reg0reg1[,reg2reg3]" */
  941. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  942. {
  943. struct property *prop;
  944. u32 *regs;
  945. prop = of_find_property(dp, "reg", NULL);
  946. if (!prop)
  947. return;
  948. regs = prop->value;
  949. if (regs[2] || regs[3]) {
  950. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  951. dp->name, regs[0], regs[1], regs[2], regs[3]);
  952. } else {
  953. sprintf(tmp_buf, "%s@%08x%08x",
  954. dp->name, regs[0], regs[1]);
  955. }
  956. }
  957. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  958. {
  959. struct device_node *parent = dp->parent;
  960. if (parent != NULL) {
  961. if (!strcmp(parent->type, "pci") ||
  962. !strcmp(parent->type, "pciex")) {
  963. pci_path_component(dp, tmp_buf);
  964. return;
  965. }
  966. if (!strcmp(parent->type, "sbus")) {
  967. sbus_path_component(dp, tmp_buf);
  968. return;
  969. }
  970. if (!strcmp(parent->type, "upa")) {
  971. upa_path_component(dp, tmp_buf);
  972. return;
  973. }
  974. if (!strcmp(parent->type, "ebus")) {
  975. ebus_path_component(dp, tmp_buf);
  976. return;
  977. }
  978. if (!strcmp(parent->name, "usb") ||
  979. !strcmp(parent->name, "hub")) {
  980. usb_path_component(dp, tmp_buf);
  981. return;
  982. }
  983. if (!strcmp(parent->type, "i2c")) {
  984. i2c_path_component(dp, tmp_buf);
  985. return;
  986. }
  987. if (!strcmp(parent->type, "firewire")) {
  988. ieee1394_path_component(dp, tmp_buf);
  989. return;
  990. }
  991. if (!strcmp(parent->type, "virtual-devices")) {
  992. vdev_path_component(dp, tmp_buf);
  993. return;
  994. }
  995. /* "isa" is handled with platform naming */
  996. }
  997. /* Use platform naming convention. */
  998. if (tlb_type == hypervisor) {
  999. sun4v_path_component(dp, tmp_buf);
  1000. return;
  1001. } else {
  1002. sun4u_path_component(dp, tmp_buf);
  1003. }
  1004. }
  1005. static char * __init build_path_component(struct device_node *dp)
  1006. {
  1007. char tmp_buf[64], *n;
  1008. tmp_buf[0] = '\0';
  1009. __build_path_component(dp, tmp_buf);
  1010. if (tmp_buf[0] == '\0')
  1011. strcpy(tmp_buf, dp->name);
  1012. n = prom_early_alloc(strlen(tmp_buf) + 1);
  1013. strcpy(n, tmp_buf);
  1014. return n;
  1015. }
  1016. static char * __init build_full_name(struct device_node *dp)
  1017. {
  1018. int len, ourlen, plen;
  1019. char *n;
  1020. plen = strlen(dp->parent->full_name);
  1021. ourlen = strlen(dp->path_component_name);
  1022. len = ourlen + plen + 2;
  1023. n = prom_early_alloc(len);
  1024. strcpy(n, dp->parent->full_name);
  1025. if (!is_root_node(dp->parent)) {
  1026. strcpy(n + plen, "/");
  1027. plen++;
  1028. }
  1029. strcpy(n + plen, dp->path_component_name);
  1030. return n;
  1031. }
  1032. static unsigned int unique_id;
  1033. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  1034. {
  1035. static struct property *tmp = NULL;
  1036. struct property *p;
  1037. if (tmp) {
  1038. p = tmp;
  1039. memset(p, 0, sizeof(*p) + 32);
  1040. tmp = NULL;
  1041. } else {
  1042. p = prom_early_alloc(sizeof(struct property) + 32);
  1043. p->unique_id = unique_id++;
  1044. }
  1045. p->name = (char *) (p + 1);
  1046. if (special_name) {
  1047. strcpy(p->name, special_name);
  1048. p->length = special_len;
  1049. p->value = prom_early_alloc(special_len);
  1050. memcpy(p->value, special_val, special_len);
  1051. } else {
  1052. if (prev == NULL) {
  1053. prom_firstprop(node, p->name);
  1054. } else {
  1055. prom_nextprop(node, prev, p->name);
  1056. }
  1057. if (strlen(p->name) == 0) {
  1058. tmp = p;
  1059. return NULL;
  1060. }
  1061. p->length = prom_getproplen(node, p->name);
  1062. if (p->length <= 0) {
  1063. p->length = 0;
  1064. } else {
  1065. p->value = prom_early_alloc(p->length + 1);
  1066. prom_getproperty(node, p->name, p->value, p->length);
  1067. ((unsigned char *)p->value)[p->length] = '\0';
  1068. }
  1069. }
  1070. return p;
  1071. }
  1072. static struct property * __init build_prop_list(phandle node)
  1073. {
  1074. struct property *head, *tail;
  1075. head = tail = build_one_prop(node, NULL,
  1076. ".node", &node, sizeof(node));
  1077. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  1078. tail = tail->next;
  1079. while(tail) {
  1080. tail->next = build_one_prop(node, tail->name,
  1081. NULL, NULL, 0);
  1082. tail = tail->next;
  1083. }
  1084. return head;
  1085. }
  1086. static char * __init get_one_property(phandle node, const char *name)
  1087. {
  1088. char *buf = "<NULL>";
  1089. int len;
  1090. len = prom_getproplen(node, name);
  1091. if (len > 0) {
  1092. buf = prom_early_alloc(len);
  1093. prom_getproperty(node, name, buf, len);
  1094. }
  1095. return buf;
  1096. }
  1097. static struct device_node * __init create_node(phandle node, struct device_node *parent)
  1098. {
  1099. struct device_node *dp;
  1100. if (!node)
  1101. return NULL;
  1102. dp = prom_early_alloc(sizeof(*dp));
  1103. dp->unique_id = unique_id++;
  1104. dp->parent = parent;
  1105. kref_init(&dp->kref);
  1106. dp->name = get_one_property(node, "name");
  1107. dp->type = get_one_property(node, "device_type");
  1108. dp->node = node;
  1109. dp->properties = build_prop_list(node);
  1110. irq_trans_init(dp);
  1111. return dp;
  1112. }
  1113. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  1114. {
  1115. struct device_node *ret = NULL, *prev_sibling = NULL;
  1116. struct device_node *dp;
  1117. while (1) {
  1118. dp = create_node(node, parent);
  1119. if (!dp)
  1120. break;
  1121. if (prev_sibling)
  1122. prev_sibling->sibling = dp;
  1123. if (!ret)
  1124. ret = dp;
  1125. prev_sibling = dp;
  1126. *(*nextp) = dp;
  1127. *nextp = &dp->allnext;
  1128. dp->path_component_name = build_path_component(dp);
  1129. dp->full_name = build_full_name(dp);
  1130. dp->child = build_tree(dp, prom_getchild(node), nextp);
  1131. node = prom_getsibling(node);
  1132. }
  1133. return ret;
  1134. }
  1135. static const char *get_mid_prop(void)
  1136. {
  1137. return (tlb_type == spitfire ? "upa-portid" : "portid");
  1138. }
  1139. struct device_node *of_find_node_by_cpuid(int cpuid)
  1140. {
  1141. struct device_node *dp;
  1142. const char *mid_prop = get_mid_prop();
  1143. for_each_node_by_type(dp, "cpu") {
  1144. int id = of_getintprop_default(dp, mid_prop, -1);
  1145. const char *this_mid_prop = mid_prop;
  1146. if (id < 0) {
  1147. this_mid_prop = "cpuid";
  1148. id = of_getintprop_default(dp, this_mid_prop, -1);
  1149. }
  1150. if (id < 0) {
  1151. prom_printf("OF: Serious problem, cpu lacks "
  1152. "%s property", this_mid_prop);
  1153. prom_halt();
  1154. }
  1155. if (cpuid == id)
  1156. return dp;
  1157. }
  1158. return NULL;
  1159. }
  1160. static void __init of_fill_in_cpu_data(void)
  1161. {
  1162. struct device_node *dp;
  1163. const char *mid_prop = get_mid_prop();
  1164. ncpus_probed = 0;
  1165. for_each_node_by_type(dp, "cpu") {
  1166. int cpuid = of_getintprop_default(dp, mid_prop, -1);
  1167. const char *this_mid_prop = mid_prop;
  1168. struct device_node *portid_parent;
  1169. int portid = -1;
  1170. portid_parent = NULL;
  1171. if (cpuid < 0) {
  1172. this_mid_prop = "cpuid";
  1173. cpuid = of_getintprop_default(dp, this_mid_prop, -1);
  1174. if (cpuid >= 0) {
  1175. int limit = 2;
  1176. portid_parent = dp;
  1177. while (limit--) {
  1178. portid_parent = portid_parent->parent;
  1179. if (!portid_parent)
  1180. break;
  1181. portid = of_getintprop_default(portid_parent,
  1182. "portid", -1);
  1183. if (portid >= 0)
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. if (cpuid < 0) {
  1189. prom_printf("OF: Serious problem, cpu lacks "
  1190. "%s property", this_mid_prop);
  1191. prom_halt();
  1192. }
  1193. ncpus_probed++;
  1194. #ifdef CONFIG_SMP
  1195. if (cpuid >= NR_CPUS) {
  1196. printk(KERN_WARNING "Ignoring CPU %d which is "
  1197. ">= NR_CPUS (%d)\n",
  1198. cpuid, NR_CPUS);
  1199. continue;
  1200. }
  1201. #else
  1202. /* On uniprocessor we only want the values for the
  1203. * real physical cpu the kernel booted onto, however
  1204. * cpu_data() only has one entry at index 0.
  1205. */
  1206. if (cpuid != real_hard_smp_processor_id())
  1207. continue;
  1208. cpuid = 0;
  1209. #endif
  1210. cpu_data(cpuid).clock_tick =
  1211. of_getintprop_default(dp, "clock-frequency", 0);
  1212. if (portid_parent) {
  1213. cpu_data(cpuid).dcache_size =
  1214. of_getintprop_default(dp, "l1-dcache-size",
  1215. 16 * 1024);
  1216. cpu_data(cpuid).dcache_line_size =
  1217. of_getintprop_default(dp, "l1-dcache-line-size",
  1218. 32);
  1219. cpu_data(cpuid).icache_size =
  1220. of_getintprop_default(dp, "l1-icache-size",
  1221. 8 * 1024);
  1222. cpu_data(cpuid).icache_line_size =
  1223. of_getintprop_default(dp, "l1-icache-line-size",
  1224. 32);
  1225. cpu_data(cpuid).ecache_size =
  1226. of_getintprop_default(dp, "l2-cache-size", 0);
  1227. cpu_data(cpuid).ecache_line_size =
  1228. of_getintprop_default(dp, "l2-cache-line-size", 0);
  1229. if (!cpu_data(cpuid).ecache_size ||
  1230. !cpu_data(cpuid).ecache_line_size) {
  1231. cpu_data(cpuid).ecache_size =
  1232. of_getintprop_default(portid_parent,
  1233. "l2-cache-size",
  1234. (4 * 1024 * 1024));
  1235. cpu_data(cpuid).ecache_line_size =
  1236. of_getintprop_default(portid_parent,
  1237. "l2-cache-line-size", 64);
  1238. }
  1239. cpu_data(cpuid).core_id = portid + 1;
  1240. cpu_data(cpuid).proc_id = portid;
  1241. #ifdef CONFIG_SMP
  1242. sparc64_multi_core = 1;
  1243. #endif
  1244. } else {
  1245. cpu_data(cpuid).dcache_size =
  1246. of_getintprop_default(dp, "dcache-size", 16 * 1024);
  1247. cpu_data(cpuid).dcache_line_size =
  1248. of_getintprop_default(dp, "dcache-line-size", 32);
  1249. cpu_data(cpuid).icache_size =
  1250. of_getintprop_default(dp, "icache-size", 16 * 1024);
  1251. cpu_data(cpuid).icache_line_size =
  1252. of_getintprop_default(dp, "icache-line-size", 32);
  1253. cpu_data(cpuid).ecache_size =
  1254. of_getintprop_default(dp, "ecache-size",
  1255. (4 * 1024 * 1024));
  1256. cpu_data(cpuid).ecache_line_size =
  1257. of_getintprop_default(dp, "ecache-line-size", 64);
  1258. cpu_data(cpuid).core_id = 0;
  1259. cpu_data(cpuid).proc_id = -1;
  1260. }
  1261. #ifdef CONFIG_SMP
  1262. cpu_set(cpuid, cpu_present_map);
  1263. cpu_set(cpuid, cpu_possible_map);
  1264. #endif
  1265. }
  1266. smp_fill_in_sib_core_maps();
  1267. }
  1268. struct device_node *of_console_device;
  1269. EXPORT_SYMBOL(of_console_device);
  1270. char *of_console_path;
  1271. EXPORT_SYMBOL(of_console_path);
  1272. char *of_console_options;
  1273. EXPORT_SYMBOL(of_console_options);
  1274. static void __init of_console_init(void)
  1275. {
  1276. char *msg = "OF stdout device is: %s\n";
  1277. struct device_node *dp;
  1278. const char *type;
  1279. phandle node;
  1280. of_console_path = prom_early_alloc(256);
  1281. if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
  1282. prom_printf("Cannot obtain path of stdout.\n");
  1283. prom_halt();
  1284. }
  1285. of_console_options = strrchr(of_console_path, ':');
  1286. if (of_console_options) {
  1287. of_console_options++;
  1288. if (*of_console_options == '\0')
  1289. of_console_options = NULL;
  1290. }
  1291. node = prom_inst2pkg(prom_stdout);
  1292. if (!node) {
  1293. prom_printf("Cannot resolve stdout node from "
  1294. "instance %08x.\n", prom_stdout);
  1295. prom_halt();
  1296. }
  1297. dp = of_find_node_by_phandle(node);
  1298. type = of_get_property(dp, "device_type", NULL);
  1299. if (!type) {
  1300. prom_printf("Console stdout lacks device_type property.\n");
  1301. prom_halt();
  1302. }
  1303. if (strcmp(type, "display") && strcmp(type, "serial")) {
  1304. prom_printf("Console device_type is neither display "
  1305. "nor serial.\n");
  1306. prom_halt();
  1307. }
  1308. of_console_device = dp;
  1309. printk(msg, of_console_path);
  1310. }
  1311. void __init prom_build_devicetree(void)
  1312. {
  1313. struct device_node **nextp;
  1314. allnodes = create_node(prom_root_node, NULL);
  1315. allnodes->path_component_name = "";
  1316. allnodes->full_name = "/";
  1317. nextp = &allnodes->allnext;
  1318. allnodes->child = build_tree(allnodes,
  1319. prom_getchild(allnodes->node),
  1320. &nextp);
  1321. of_console_init();
  1322. printk("PROM: Built device tree with %u bytes of memory.\n",
  1323. prom_early_allocated);
  1324. if (tlb_type != hypervisor)
  1325. of_fill_in_cpu_data();
  1326. }