prom.c 42 KB

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