prom.c 43 KB

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