prom.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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. !strcmp(dp->name, "niu"))
  887. return sun4v_vdev_irq_trans_init(dp);
  888. }
  889. static int is_root_node(const struct device_node *dp)
  890. {
  891. if (!dp)
  892. return 0;
  893. return (dp->parent == NULL);
  894. }
  895. /* The following routines deal with the black magic of fully naming a
  896. * node.
  897. *
  898. * Certain well known named nodes are just the simple name string.
  899. *
  900. * Actual devices have an address specifier appended to the base name
  901. * string, like this "foo@addr". The "addr" can be in any number of
  902. * formats, and the platform plus the type of the node determine the
  903. * format and how it is constructed.
  904. *
  905. * For children of the ROOT node, the naming convention is fixed and
  906. * determined by whether this is a sun4u or sun4v system.
  907. *
  908. * For children of other nodes, it is bus type specific. So
  909. * we walk up the tree until we discover a "device_type" property
  910. * we recognize and we go from there.
  911. *
  912. * As an example, the boot device on my workstation has a full path:
  913. *
  914. * /pci@1e,600000/ide@d/disk@0,0:c
  915. */
  916. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  917. {
  918. struct linux_prom64_registers *regs;
  919. struct property *rprop;
  920. u32 high_bits, low_bits, type;
  921. rprop = of_find_property(dp, "reg", NULL);
  922. if (!rprop)
  923. return;
  924. regs = rprop->value;
  925. if (!is_root_node(dp->parent)) {
  926. sprintf(tmp_buf, "%s@%x,%x",
  927. dp->name,
  928. (unsigned int) (regs->phys_addr >> 32UL),
  929. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  930. return;
  931. }
  932. type = regs->phys_addr >> 60UL;
  933. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  934. low_bits = (regs->phys_addr & 0xffffffffUL);
  935. if (type == 0 || type == 8) {
  936. const char *prefix = (type == 0) ? "m" : "i";
  937. if (low_bits)
  938. sprintf(tmp_buf, "%s@%s%x,%x",
  939. dp->name, prefix,
  940. high_bits, low_bits);
  941. else
  942. sprintf(tmp_buf, "%s@%s%x",
  943. dp->name,
  944. prefix,
  945. high_bits);
  946. } else if (type == 12) {
  947. sprintf(tmp_buf, "%s@%x",
  948. dp->name, high_bits);
  949. }
  950. }
  951. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  952. {
  953. struct linux_prom64_registers *regs;
  954. struct property *prop;
  955. prop = of_find_property(dp, "reg", NULL);
  956. if (!prop)
  957. return;
  958. regs = prop->value;
  959. if (!is_root_node(dp->parent)) {
  960. sprintf(tmp_buf, "%s@%x,%x",
  961. dp->name,
  962. (unsigned int) (regs->phys_addr >> 32UL),
  963. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  964. return;
  965. }
  966. prop = of_find_property(dp, "upa-portid", NULL);
  967. if (!prop)
  968. prop = of_find_property(dp, "portid", NULL);
  969. if (prop) {
  970. unsigned long mask = 0xffffffffUL;
  971. if (tlb_type >= cheetah)
  972. mask = 0x7fffff;
  973. sprintf(tmp_buf, "%s@%x,%x",
  974. dp->name,
  975. *(u32 *)prop->value,
  976. (unsigned int) (regs->phys_addr & mask));
  977. }
  978. }
  979. /* "name@slot,offset" */
  980. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  981. {
  982. struct linux_prom_registers *regs;
  983. struct property *prop;
  984. prop = of_find_property(dp, "reg", NULL);
  985. if (!prop)
  986. return;
  987. regs = prop->value;
  988. sprintf(tmp_buf, "%s@%x,%x",
  989. dp->name,
  990. regs->which_io,
  991. regs->phys_addr);
  992. }
  993. /* "name@devnum[,func]" */
  994. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  995. {
  996. struct linux_prom_pci_registers *regs;
  997. struct property *prop;
  998. unsigned int devfn;
  999. prop = of_find_property(dp, "reg", NULL);
  1000. if (!prop)
  1001. return;
  1002. regs = prop->value;
  1003. devfn = (regs->phys_hi >> 8) & 0xff;
  1004. if (devfn & 0x07) {
  1005. sprintf(tmp_buf, "%s@%x,%x",
  1006. dp->name,
  1007. devfn >> 3,
  1008. devfn & 0x07);
  1009. } else {
  1010. sprintf(tmp_buf, "%s@%x",
  1011. dp->name,
  1012. devfn >> 3);
  1013. }
  1014. }
  1015. /* "name@UPA_PORTID,offset" */
  1016. static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
  1017. {
  1018. struct linux_prom64_registers *regs;
  1019. struct property *prop;
  1020. prop = of_find_property(dp, "reg", NULL);
  1021. if (!prop)
  1022. return;
  1023. regs = prop->value;
  1024. prop = of_find_property(dp, "upa-portid", NULL);
  1025. if (!prop)
  1026. return;
  1027. sprintf(tmp_buf, "%s@%x,%x",
  1028. dp->name,
  1029. *(u32 *) prop->value,
  1030. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  1031. }
  1032. /* "name@reg" */
  1033. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  1034. {
  1035. struct property *prop;
  1036. u32 *regs;
  1037. prop = of_find_property(dp, "reg", NULL);
  1038. if (!prop)
  1039. return;
  1040. regs = prop->value;
  1041. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  1042. }
  1043. /* "name@addrhi,addrlo" */
  1044. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  1045. {
  1046. struct linux_prom64_registers *regs;
  1047. struct property *prop;
  1048. prop = of_find_property(dp, "reg", NULL);
  1049. if (!prop)
  1050. return;
  1051. regs = prop->value;
  1052. sprintf(tmp_buf, "%s@%x,%x",
  1053. dp->name,
  1054. (unsigned int) (regs->phys_addr >> 32UL),
  1055. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  1056. }
  1057. /* "name@bus,addr" */
  1058. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  1059. {
  1060. struct property *prop;
  1061. u32 *regs;
  1062. prop = of_find_property(dp, "reg", NULL);
  1063. if (!prop)
  1064. return;
  1065. regs = prop->value;
  1066. /* This actually isn't right... should look at the #address-cells
  1067. * property of the i2c bus node etc. etc.
  1068. */
  1069. sprintf(tmp_buf, "%s@%x,%x",
  1070. dp->name, regs[0], regs[1]);
  1071. }
  1072. /* "name@reg0[,reg1]" */
  1073. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  1074. {
  1075. struct property *prop;
  1076. u32 *regs;
  1077. prop = of_find_property(dp, "reg", NULL);
  1078. if (!prop)
  1079. return;
  1080. regs = prop->value;
  1081. if (prop->length == sizeof(u32) || regs[1] == 1) {
  1082. sprintf(tmp_buf, "%s@%x",
  1083. dp->name, regs[0]);
  1084. } else {
  1085. sprintf(tmp_buf, "%s@%x,%x",
  1086. dp->name, regs[0], regs[1]);
  1087. }
  1088. }
  1089. /* "name@reg0reg1[,reg2reg3]" */
  1090. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  1091. {
  1092. struct property *prop;
  1093. u32 *regs;
  1094. prop = of_find_property(dp, "reg", NULL);
  1095. if (!prop)
  1096. return;
  1097. regs = prop->value;
  1098. if (regs[2] || regs[3]) {
  1099. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  1100. dp->name, regs[0], regs[1], regs[2], regs[3]);
  1101. } else {
  1102. sprintf(tmp_buf, "%s@%08x%08x",
  1103. dp->name, regs[0], regs[1]);
  1104. }
  1105. }
  1106. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  1107. {
  1108. struct device_node *parent = dp->parent;
  1109. if (parent != NULL) {
  1110. if (!strcmp(parent->type, "pci") ||
  1111. !strcmp(parent->type, "pciex"))
  1112. return pci_path_component(dp, tmp_buf);
  1113. if (!strcmp(parent->type, "sbus"))
  1114. return sbus_path_component(dp, tmp_buf);
  1115. if (!strcmp(parent->type, "upa"))
  1116. return upa_path_component(dp, tmp_buf);
  1117. if (!strcmp(parent->type, "ebus"))
  1118. return ebus_path_component(dp, tmp_buf);
  1119. if (!strcmp(parent->name, "usb") ||
  1120. !strcmp(parent->name, "hub"))
  1121. return usb_path_component(dp, tmp_buf);
  1122. if (!strcmp(parent->type, "i2c"))
  1123. return i2c_path_component(dp, tmp_buf);
  1124. if (!strcmp(parent->type, "firewire"))
  1125. return ieee1394_path_component(dp, tmp_buf);
  1126. if (!strcmp(parent->type, "virtual-devices"))
  1127. return vdev_path_component(dp, tmp_buf);
  1128. /* "isa" is handled with platform naming */
  1129. }
  1130. /* Use platform naming convention. */
  1131. if (tlb_type == hypervisor)
  1132. return sun4v_path_component(dp, tmp_buf);
  1133. else
  1134. return sun4u_path_component(dp, tmp_buf);
  1135. }
  1136. static char * __init build_path_component(struct device_node *dp)
  1137. {
  1138. char tmp_buf[64], *n;
  1139. tmp_buf[0] = '\0';
  1140. __build_path_component(dp, tmp_buf);
  1141. if (tmp_buf[0] == '\0')
  1142. strcpy(tmp_buf, dp->name);
  1143. n = prom_early_alloc(strlen(tmp_buf) + 1);
  1144. strcpy(n, tmp_buf);
  1145. return n;
  1146. }
  1147. static char * __init build_full_name(struct device_node *dp)
  1148. {
  1149. int len, ourlen, plen;
  1150. char *n;
  1151. plen = strlen(dp->parent->full_name);
  1152. ourlen = strlen(dp->path_component_name);
  1153. len = ourlen + plen + 2;
  1154. n = prom_early_alloc(len);
  1155. strcpy(n, dp->parent->full_name);
  1156. if (!is_root_node(dp->parent)) {
  1157. strcpy(n + plen, "/");
  1158. plen++;
  1159. }
  1160. strcpy(n + plen, dp->path_component_name);
  1161. return n;
  1162. }
  1163. static unsigned int unique_id;
  1164. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  1165. {
  1166. static struct property *tmp = NULL;
  1167. struct property *p;
  1168. if (tmp) {
  1169. p = tmp;
  1170. memset(p, 0, sizeof(*p) + 32);
  1171. tmp = NULL;
  1172. } else {
  1173. p = prom_early_alloc(sizeof(struct property) + 32);
  1174. p->unique_id = unique_id++;
  1175. }
  1176. p->name = (char *) (p + 1);
  1177. if (special_name) {
  1178. strcpy(p->name, special_name);
  1179. p->length = special_len;
  1180. p->value = prom_early_alloc(special_len);
  1181. memcpy(p->value, special_val, special_len);
  1182. } else {
  1183. if (prev == NULL) {
  1184. prom_firstprop(node, p->name);
  1185. } else {
  1186. prom_nextprop(node, prev, p->name);
  1187. }
  1188. if (strlen(p->name) == 0) {
  1189. tmp = p;
  1190. return NULL;
  1191. }
  1192. p->length = prom_getproplen(node, p->name);
  1193. if (p->length <= 0) {
  1194. p->length = 0;
  1195. } else {
  1196. p->value = prom_early_alloc(p->length + 1);
  1197. prom_getproperty(node, p->name, p->value, p->length);
  1198. ((unsigned char *)p->value)[p->length] = '\0';
  1199. }
  1200. }
  1201. return p;
  1202. }
  1203. static struct property * __init build_prop_list(phandle node)
  1204. {
  1205. struct property *head, *tail;
  1206. head = tail = build_one_prop(node, NULL,
  1207. ".node", &node, sizeof(node));
  1208. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  1209. tail = tail->next;
  1210. while(tail) {
  1211. tail->next = build_one_prop(node, tail->name,
  1212. NULL, NULL, 0);
  1213. tail = tail->next;
  1214. }
  1215. return head;
  1216. }
  1217. static char * __init get_one_property(phandle node, const char *name)
  1218. {
  1219. char *buf = "<NULL>";
  1220. int len;
  1221. len = prom_getproplen(node, name);
  1222. if (len > 0) {
  1223. buf = prom_early_alloc(len);
  1224. prom_getproperty(node, name, buf, len);
  1225. }
  1226. return buf;
  1227. }
  1228. static struct device_node * __init create_node(phandle node, struct device_node *parent)
  1229. {
  1230. struct device_node *dp;
  1231. if (!node)
  1232. return NULL;
  1233. dp = prom_early_alloc(sizeof(*dp));
  1234. dp->unique_id = unique_id++;
  1235. dp->parent = parent;
  1236. kref_init(&dp->kref);
  1237. dp->name = get_one_property(node, "name");
  1238. dp->type = get_one_property(node, "device_type");
  1239. dp->node = node;
  1240. dp->properties = build_prop_list(node);
  1241. irq_trans_init(dp);
  1242. return dp;
  1243. }
  1244. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  1245. {
  1246. struct device_node *ret = NULL, *prev_sibling = NULL;
  1247. struct device_node *dp;
  1248. while (1) {
  1249. dp = create_node(node, parent);
  1250. if (!dp)
  1251. break;
  1252. if (prev_sibling)
  1253. prev_sibling->sibling = dp;
  1254. if (!ret)
  1255. ret = dp;
  1256. prev_sibling = dp;
  1257. *(*nextp) = dp;
  1258. *nextp = &dp->allnext;
  1259. dp->path_component_name = build_path_component(dp);
  1260. dp->full_name = build_full_name(dp);
  1261. dp->child = build_tree(dp, prom_getchild(node), nextp);
  1262. node = prom_getsibling(node);
  1263. }
  1264. return ret;
  1265. }
  1266. static const char *get_mid_prop(void)
  1267. {
  1268. return (tlb_type == spitfire ? "upa-portid" : "portid");
  1269. }
  1270. struct device_node *of_find_node_by_cpuid(int cpuid)
  1271. {
  1272. struct device_node *dp;
  1273. const char *mid_prop = get_mid_prop();
  1274. for_each_node_by_type(dp, "cpu") {
  1275. int id = of_getintprop_default(dp, mid_prop, -1);
  1276. const char *this_mid_prop = mid_prop;
  1277. if (id < 0) {
  1278. this_mid_prop = "cpuid";
  1279. id = of_getintprop_default(dp, this_mid_prop, -1);
  1280. }
  1281. if (id < 0) {
  1282. prom_printf("OF: Serious problem, cpu lacks "
  1283. "%s property", this_mid_prop);
  1284. prom_halt();
  1285. }
  1286. if (cpuid == id)
  1287. return dp;
  1288. }
  1289. return NULL;
  1290. }
  1291. static void __init of_fill_in_cpu_data(void)
  1292. {
  1293. struct device_node *dp;
  1294. const char *mid_prop = get_mid_prop();
  1295. ncpus_probed = 0;
  1296. for_each_node_by_type(dp, "cpu") {
  1297. int cpuid = of_getintprop_default(dp, mid_prop, -1);
  1298. const char *this_mid_prop = mid_prop;
  1299. struct device_node *portid_parent;
  1300. int portid = -1;
  1301. portid_parent = NULL;
  1302. if (cpuid < 0) {
  1303. this_mid_prop = "cpuid";
  1304. cpuid = of_getintprop_default(dp, this_mid_prop, -1);
  1305. if (cpuid >= 0) {
  1306. int limit = 2;
  1307. portid_parent = dp;
  1308. while (limit--) {
  1309. portid_parent = portid_parent->parent;
  1310. if (!portid_parent)
  1311. break;
  1312. portid = of_getintprop_default(portid_parent,
  1313. "portid", -1);
  1314. if (portid >= 0)
  1315. break;
  1316. }
  1317. }
  1318. }
  1319. if (cpuid < 0) {
  1320. prom_printf("OF: Serious problem, cpu lacks "
  1321. "%s property", this_mid_prop);
  1322. prom_halt();
  1323. }
  1324. ncpus_probed++;
  1325. #ifdef CONFIG_SMP
  1326. if (cpuid >= NR_CPUS) {
  1327. printk(KERN_WARNING "Ignoring CPU %d which is "
  1328. ">= NR_CPUS (%d)\n",
  1329. cpuid, NR_CPUS);
  1330. continue;
  1331. }
  1332. #else
  1333. /* On uniprocessor we only want the values for the
  1334. * real physical cpu the kernel booted onto, however
  1335. * cpu_data() only has one entry at index 0.
  1336. */
  1337. if (cpuid != real_hard_smp_processor_id())
  1338. continue;
  1339. cpuid = 0;
  1340. #endif
  1341. cpu_data(cpuid).clock_tick =
  1342. of_getintprop_default(dp, "clock-frequency", 0);
  1343. if (portid_parent) {
  1344. cpu_data(cpuid).dcache_size =
  1345. of_getintprop_default(dp, "l1-dcache-size",
  1346. 16 * 1024);
  1347. cpu_data(cpuid).dcache_line_size =
  1348. of_getintprop_default(dp, "l1-dcache-line-size",
  1349. 32);
  1350. cpu_data(cpuid).icache_size =
  1351. of_getintprop_default(dp, "l1-icache-size",
  1352. 8 * 1024);
  1353. cpu_data(cpuid).icache_line_size =
  1354. of_getintprop_default(dp, "l1-icache-line-size",
  1355. 32);
  1356. cpu_data(cpuid).ecache_size =
  1357. of_getintprop_default(dp, "l2-cache-size", 0);
  1358. cpu_data(cpuid).ecache_line_size =
  1359. of_getintprop_default(dp, "l2-cache-line-size", 0);
  1360. if (!cpu_data(cpuid).ecache_size ||
  1361. !cpu_data(cpuid).ecache_line_size) {
  1362. cpu_data(cpuid).ecache_size =
  1363. of_getintprop_default(portid_parent,
  1364. "l2-cache-size",
  1365. (4 * 1024 * 1024));
  1366. cpu_data(cpuid).ecache_line_size =
  1367. of_getintprop_default(portid_parent,
  1368. "l2-cache-line-size", 64);
  1369. }
  1370. cpu_data(cpuid).core_id = portid + 1;
  1371. cpu_data(cpuid).proc_id = portid;
  1372. #ifdef CONFIG_SMP
  1373. sparc64_multi_core = 1;
  1374. #endif
  1375. } else {
  1376. cpu_data(cpuid).dcache_size =
  1377. of_getintprop_default(dp, "dcache-size", 16 * 1024);
  1378. cpu_data(cpuid).dcache_line_size =
  1379. of_getintprop_default(dp, "dcache-line-size", 32);
  1380. cpu_data(cpuid).icache_size =
  1381. of_getintprop_default(dp, "icache-size", 16 * 1024);
  1382. cpu_data(cpuid).icache_line_size =
  1383. of_getintprop_default(dp, "icache-line-size", 32);
  1384. cpu_data(cpuid).ecache_size =
  1385. of_getintprop_default(dp, "ecache-size",
  1386. (4 * 1024 * 1024));
  1387. cpu_data(cpuid).ecache_line_size =
  1388. of_getintprop_default(dp, "ecache-line-size", 64);
  1389. cpu_data(cpuid).core_id = 0;
  1390. cpu_data(cpuid).proc_id = -1;
  1391. }
  1392. #ifdef CONFIG_SMP
  1393. cpu_set(cpuid, cpu_present_map);
  1394. cpu_set(cpuid, cpu_possible_map);
  1395. #endif
  1396. }
  1397. smp_fill_in_sib_core_maps();
  1398. }
  1399. struct device_node *of_console_device;
  1400. EXPORT_SYMBOL(of_console_device);
  1401. char *of_console_path;
  1402. EXPORT_SYMBOL(of_console_path);
  1403. char *of_console_options;
  1404. EXPORT_SYMBOL(of_console_options);
  1405. static void __init of_console_init(void)
  1406. {
  1407. char *msg = "OF stdout device is: %s\n";
  1408. struct device_node *dp;
  1409. const char *type;
  1410. phandle node;
  1411. of_console_path = prom_early_alloc(256);
  1412. if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
  1413. prom_printf("Cannot obtain path of stdout.\n");
  1414. prom_halt();
  1415. }
  1416. of_console_options = strrchr(of_console_path, ':');
  1417. if (of_console_options) {
  1418. of_console_options++;
  1419. if (*of_console_options == '\0')
  1420. of_console_options = NULL;
  1421. }
  1422. node = prom_inst2pkg(prom_stdout);
  1423. if (!node) {
  1424. prom_printf("Cannot resolve stdout node from "
  1425. "instance %08x.\n", prom_stdout);
  1426. prom_halt();
  1427. }
  1428. dp = of_find_node_by_phandle(node);
  1429. type = of_get_property(dp, "device_type", NULL);
  1430. if (!type) {
  1431. prom_printf("Console stdout lacks device_type property.\n");
  1432. prom_halt();
  1433. }
  1434. if (strcmp(type, "display") && strcmp(type, "serial")) {
  1435. prom_printf("Console device_type is neither display "
  1436. "nor serial.\n");
  1437. prom_halt();
  1438. }
  1439. of_console_device = dp;
  1440. prom_printf(msg, of_console_path);
  1441. printk(msg, of_console_path);
  1442. }
  1443. void __init prom_build_devicetree(void)
  1444. {
  1445. struct device_node **nextp;
  1446. allnodes = create_node(prom_root_node, NULL);
  1447. allnodes->path_component_name = "";
  1448. allnodes->full_name = "/";
  1449. nextp = &allnodes->allnext;
  1450. allnodes->child = build_tree(allnodes,
  1451. prom_getchild(allnodes->node),
  1452. &nextp);
  1453. of_console_init();
  1454. printk("PROM: Built device tree with %u bytes of memory.\n",
  1455. prom_early_allocated);
  1456. if (tlb_type != hypervisor)
  1457. of_fill_in_cpu_data();
  1458. }