prom.c 43 KB

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