prom.c 42 KB

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