prom.c 41 KB

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