prom.c 42 KB

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