prom.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. /*
  2. *
  3. *
  4. * Procedures for interfacing to Open Firmware.
  5. *
  6. * Paul Mackerras August 1996.
  7. * Copyright (C) 1996 Paul Mackerras.
  8. *
  9. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  10. * {engebret|bergner}@us.ibm.com
  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. #undef DEBUG
  18. #include <stdarg.h>
  19. #include <linux/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/version.h>
  24. #include <linux/threads.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/types.h>
  27. #include <linux/pci.h>
  28. #include <linux/stringify.h>
  29. #include <linux/delay.h>
  30. #include <linux/initrd.h>
  31. #include <linux/bitops.h>
  32. #include <linux/module.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. #include <asm/lmb.h>
  36. #include <asm/abs_addr.h>
  37. #include <asm/page.h>
  38. #include <asm/processor.h>
  39. #include <asm/irq.h>
  40. #include <asm/io.h>
  41. #include <asm/smp.h>
  42. #include <asm/system.h>
  43. #include <asm/mmu.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/pci.h>
  46. #include <asm/iommu.h>
  47. #include <asm/bootinfo.h>
  48. #include <asm/ppcdebug.h>
  49. #include <asm/btext.h>
  50. #include <asm/sections.h>
  51. #include <asm/machdep.h>
  52. #include <asm/pSeries_reconfig.h>
  53. #ifdef DEBUG
  54. #define DBG(fmt...) udbg_printf(fmt)
  55. #else
  56. #define DBG(fmt...)
  57. #endif
  58. struct pci_reg_property {
  59. struct pci_address addr;
  60. u32 size_hi;
  61. u32 size_lo;
  62. };
  63. struct isa_reg_property {
  64. u32 space;
  65. u32 address;
  66. u32 size;
  67. };
  68. typedef int interpret_func(struct device_node *, unsigned long *,
  69. int, int, int);
  70. extern struct rtas_t rtas;
  71. extern struct lmb lmb;
  72. extern unsigned long klimit;
  73. static int __initdata dt_root_addr_cells;
  74. static int __initdata dt_root_size_cells;
  75. static int __initdata iommu_is_off;
  76. int __initdata iommu_force_on;
  77. typedef u32 cell_t;
  78. #if 0
  79. static struct boot_param_header *initial_boot_params __initdata;
  80. #else
  81. struct boot_param_header *initial_boot_params;
  82. #endif
  83. static struct device_node *allnodes = NULL;
  84. /* use when traversing tree through the allnext, child, sibling,
  85. * or parent members of struct device_node.
  86. */
  87. static DEFINE_RWLOCK(devtree_lock);
  88. /* export that to outside world */
  89. struct device_node *of_chosen;
  90. /*
  91. * Wrapper for allocating memory for various data that needs to be
  92. * attached to device nodes as they are processed at boot or when
  93. * added to the device tree later (e.g. DLPAR). At boot there is
  94. * already a region reserved so we just increment *mem_start by size;
  95. * otherwise we call kmalloc.
  96. */
  97. static void * prom_alloc(unsigned long size, unsigned long *mem_start)
  98. {
  99. unsigned long tmp;
  100. if (!mem_start)
  101. return kmalloc(size, GFP_KERNEL);
  102. tmp = *mem_start;
  103. *mem_start += size;
  104. return (void *)tmp;
  105. }
  106. /*
  107. * Find the device_node with a given phandle.
  108. */
  109. static struct device_node * find_phandle(phandle ph)
  110. {
  111. struct device_node *np;
  112. for (np = allnodes; np != 0; np = np->allnext)
  113. if (np->linux_phandle == ph)
  114. return np;
  115. return NULL;
  116. }
  117. /*
  118. * Find the interrupt parent of a node.
  119. */
  120. static struct device_node * __devinit intr_parent(struct device_node *p)
  121. {
  122. phandle *parp;
  123. parp = (phandle *) get_property(p, "interrupt-parent", NULL);
  124. if (parp == NULL)
  125. return p->parent;
  126. return find_phandle(*parp);
  127. }
  128. /*
  129. * Find out the size of each entry of the interrupts property
  130. * for a node.
  131. */
  132. int __devinit prom_n_intr_cells(struct device_node *np)
  133. {
  134. struct device_node *p;
  135. unsigned int *icp;
  136. for (p = np; (p = intr_parent(p)) != NULL; ) {
  137. icp = (unsigned int *)
  138. get_property(p, "#interrupt-cells", NULL);
  139. if (icp != NULL)
  140. return *icp;
  141. if (get_property(p, "interrupt-controller", NULL) != NULL
  142. || get_property(p, "interrupt-map", NULL) != NULL) {
  143. printk("oops, node %s doesn't have #interrupt-cells\n",
  144. p->full_name);
  145. return 1;
  146. }
  147. }
  148. #ifdef DEBUG_IRQ
  149. printk("prom_n_intr_cells failed for %s\n", np->full_name);
  150. #endif
  151. return 1;
  152. }
  153. /*
  154. * Map an interrupt from a device up to the platform interrupt
  155. * descriptor.
  156. */
  157. static int __devinit map_interrupt(unsigned int **irq, struct device_node **ictrler,
  158. struct device_node *np, unsigned int *ints,
  159. int nintrc)
  160. {
  161. struct device_node *p, *ipar;
  162. unsigned int *imap, *imask, *ip;
  163. int i, imaplen, match;
  164. int newintrc = 0, newaddrc = 0;
  165. unsigned int *reg;
  166. int naddrc;
  167. reg = (unsigned int *) get_property(np, "reg", NULL);
  168. naddrc = prom_n_addr_cells(np);
  169. p = intr_parent(np);
  170. while (p != NULL) {
  171. if (get_property(p, "interrupt-controller", NULL) != NULL)
  172. /* this node is an interrupt controller, stop here */
  173. break;
  174. imap = (unsigned int *)
  175. get_property(p, "interrupt-map", &imaplen);
  176. if (imap == NULL) {
  177. p = intr_parent(p);
  178. continue;
  179. }
  180. imask = (unsigned int *)
  181. get_property(p, "interrupt-map-mask", NULL);
  182. if (imask == NULL) {
  183. printk("oops, %s has interrupt-map but no mask\n",
  184. p->full_name);
  185. return 0;
  186. }
  187. imaplen /= sizeof(unsigned int);
  188. match = 0;
  189. ipar = NULL;
  190. while (imaplen > 0 && !match) {
  191. /* check the child-interrupt field */
  192. match = 1;
  193. for (i = 0; i < naddrc && match; ++i)
  194. match = ((reg[i] ^ imap[i]) & imask[i]) == 0;
  195. for (; i < naddrc + nintrc && match; ++i)
  196. match = ((ints[i-naddrc] ^ imap[i]) & imask[i]) == 0;
  197. imap += naddrc + nintrc;
  198. imaplen -= naddrc + nintrc;
  199. /* grab the interrupt parent */
  200. ipar = find_phandle((phandle) *imap++);
  201. --imaplen;
  202. if (ipar == NULL) {
  203. printk("oops, no int parent %x in map of %s\n",
  204. imap[-1], p->full_name);
  205. return 0;
  206. }
  207. /* find the parent's # addr and intr cells */
  208. ip = (unsigned int *)
  209. get_property(ipar, "#interrupt-cells", NULL);
  210. if (ip == NULL) {
  211. printk("oops, no #interrupt-cells on %s\n",
  212. ipar->full_name);
  213. return 0;
  214. }
  215. newintrc = *ip;
  216. ip = (unsigned int *)
  217. get_property(ipar, "#address-cells", NULL);
  218. newaddrc = (ip == NULL)? 0: *ip;
  219. imap += newaddrc + newintrc;
  220. imaplen -= newaddrc + newintrc;
  221. }
  222. if (imaplen < 0) {
  223. printk("oops, error decoding int-map on %s, len=%d\n",
  224. p->full_name, imaplen);
  225. return 0;
  226. }
  227. if (!match) {
  228. #ifdef DEBUG_IRQ
  229. printk("oops, no match in %s int-map for %s\n",
  230. p->full_name, np->full_name);
  231. #endif
  232. return 0;
  233. }
  234. p = ipar;
  235. naddrc = newaddrc;
  236. nintrc = newintrc;
  237. ints = imap - nintrc;
  238. reg = ints - naddrc;
  239. }
  240. if (p == NULL) {
  241. #ifdef DEBUG_IRQ
  242. printk("hmmm, int tree for %s doesn't have ctrler\n",
  243. np->full_name);
  244. #endif
  245. return 0;
  246. }
  247. *irq = ints;
  248. *ictrler = p;
  249. return nintrc;
  250. }
  251. static int __devinit finish_node_interrupts(struct device_node *np,
  252. unsigned long *mem_start,
  253. int measure_only)
  254. {
  255. unsigned int *ints;
  256. int intlen, intrcells, intrcount;
  257. int i, j, n;
  258. unsigned int *irq, virq;
  259. struct device_node *ic;
  260. ints = (unsigned int *) get_property(np, "interrupts", &intlen);
  261. if (ints == NULL)
  262. return 0;
  263. intrcells = prom_n_intr_cells(np);
  264. intlen /= intrcells * sizeof(unsigned int);
  265. np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
  266. if (!np->intrs)
  267. return -ENOMEM;
  268. if (measure_only)
  269. return 0;
  270. intrcount = 0;
  271. for (i = 0; i < intlen; ++i, ints += intrcells) {
  272. n = map_interrupt(&irq, &ic, np, ints, intrcells);
  273. if (n <= 0)
  274. continue;
  275. /* don't map IRQ numbers under a cascaded 8259 controller */
  276. if (ic && device_is_compatible(ic, "chrp,iic")) {
  277. np->intrs[intrcount].line = irq[0];
  278. } else {
  279. virq = virt_irq_create_mapping(irq[0]);
  280. if (virq == NO_IRQ) {
  281. printk(KERN_CRIT "Could not allocate interrupt"
  282. " number for %s\n", np->full_name);
  283. continue;
  284. }
  285. np->intrs[intrcount].line = irq_offset_up(virq);
  286. }
  287. /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
  288. if (systemcfg->platform == PLATFORM_POWERMAC && ic && ic->parent) {
  289. char *name = get_property(ic->parent, "name", NULL);
  290. if (name && !strcmp(name, "u3"))
  291. np->intrs[intrcount].line += 128;
  292. else if (!(name && !strcmp(name, "mac-io")))
  293. /* ignore other cascaded controllers, such as
  294. the k2-sata-root */
  295. break;
  296. }
  297. np->intrs[intrcount].sense = 1;
  298. if (n > 1)
  299. np->intrs[intrcount].sense = irq[1];
  300. if (n > 2) {
  301. printk("hmmm, got %d intr cells for %s:", n,
  302. np->full_name);
  303. for (j = 0; j < n; ++j)
  304. printk(" %d", irq[j]);
  305. printk("\n");
  306. }
  307. ++intrcount;
  308. }
  309. np->n_intrs = intrcount;
  310. return 0;
  311. }
  312. static int __devinit interpret_pci_props(struct device_node *np,
  313. unsigned long *mem_start,
  314. int naddrc, int nsizec,
  315. int measure_only)
  316. {
  317. struct address_range *adr;
  318. struct pci_reg_property *pci_addrs;
  319. int i, l, n_addrs;
  320. pci_addrs = (struct pci_reg_property *)
  321. get_property(np, "assigned-addresses", &l);
  322. if (!pci_addrs)
  323. return 0;
  324. n_addrs = l / sizeof(*pci_addrs);
  325. adr = prom_alloc(n_addrs * sizeof(*adr), mem_start);
  326. if (!adr)
  327. return -ENOMEM;
  328. if (measure_only)
  329. return 0;
  330. np->addrs = adr;
  331. np->n_addrs = n_addrs;
  332. for (i = 0; i < n_addrs; i++) {
  333. adr[i].space = pci_addrs[i].addr.a_hi;
  334. adr[i].address = pci_addrs[i].addr.a_lo |
  335. ((u64)pci_addrs[i].addr.a_mid << 32);
  336. adr[i].size = pci_addrs[i].size_lo;
  337. }
  338. return 0;
  339. }
  340. static int __init interpret_dbdma_props(struct device_node *np,
  341. unsigned long *mem_start,
  342. int naddrc, int nsizec,
  343. int measure_only)
  344. {
  345. struct reg_property32 *rp;
  346. struct address_range *adr;
  347. unsigned long base_address;
  348. int i, l;
  349. struct device_node *db;
  350. base_address = 0;
  351. if (!measure_only) {
  352. for (db = np->parent; db != NULL; db = db->parent) {
  353. if (!strcmp(db->type, "dbdma") && db->n_addrs != 0) {
  354. base_address = db->addrs[0].address;
  355. break;
  356. }
  357. }
  358. }
  359. rp = (struct reg_property32 *) get_property(np, "reg", &l);
  360. if (rp != 0 && l >= sizeof(struct reg_property32)) {
  361. i = 0;
  362. adr = (struct address_range *) (*mem_start);
  363. while ((l -= sizeof(struct reg_property32)) >= 0) {
  364. if (!measure_only) {
  365. adr[i].space = 2;
  366. adr[i].address = rp[i].address + base_address;
  367. adr[i].size = rp[i].size;
  368. }
  369. ++i;
  370. }
  371. np->addrs = adr;
  372. np->n_addrs = i;
  373. (*mem_start) += i * sizeof(struct address_range);
  374. }
  375. return 0;
  376. }
  377. static int __init interpret_macio_props(struct device_node *np,
  378. unsigned long *mem_start,
  379. int naddrc, int nsizec,
  380. int measure_only)
  381. {
  382. struct reg_property32 *rp;
  383. struct address_range *adr;
  384. unsigned long base_address;
  385. int i, l;
  386. struct device_node *db;
  387. base_address = 0;
  388. if (!measure_only) {
  389. for (db = np->parent; db != NULL; db = db->parent) {
  390. if (!strcmp(db->type, "mac-io") && db->n_addrs != 0) {
  391. base_address = db->addrs[0].address;
  392. break;
  393. }
  394. }
  395. }
  396. rp = (struct reg_property32 *) get_property(np, "reg", &l);
  397. if (rp != 0 && l >= sizeof(struct reg_property32)) {
  398. i = 0;
  399. adr = (struct address_range *) (*mem_start);
  400. while ((l -= sizeof(struct reg_property32)) >= 0) {
  401. if (!measure_only) {
  402. adr[i].space = 2;
  403. adr[i].address = rp[i].address + base_address;
  404. adr[i].size = rp[i].size;
  405. }
  406. ++i;
  407. }
  408. np->addrs = adr;
  409. np->n_addrs = i;
  410. (*mem_start) += i * sizeof(struct address_range);
  411. }
  412. return 0;
  413. }
  414. static int __init interpret_isa_props(struct device_node *np,
  415. unsigned long *mem_start,
  416. int naddrc, int nsizec,
  417. int measure_only)
  418. {
  419. struct isa_reg_property *rp;
  420. struct address_range *adr;
  421. int i, l;
  422. rp = (struct isa_reg_property *) get_property(np, "reg", &l);
  423. if (rp != 0 && l >= sizeof(struct isa_reg_property)) {
  424. i = 0;
  425. adr = (struct address_range *) (*mem_start);
  426. while ((l -= sizeof(struct isa_reg_property)) >= 0) {
  427. if (!measure_only) {
  428. adr[i].space = rp[i].space;
  429. adr[i].address = rp[i].address;
  430. adr[i].size = rp[i].size;
  431. }
  432. ++i;
  433. }
  434. np->addrs = adr;
  435. np->n_addrs = i;
  436. (*mem_start) += i * sizeof(struct address_range);
  437. }
  438. return 0;
  439. }
  440. static int __init interpret_root_props(struct device_node *np,
  441. unsigned long *mem_start,
  442. int naddrc, int nsizec,
  443. int measure_only)
  444. {
  445. struct address_range *adr;
  446. int i, l;
  447. unsigned int *rp;
  448. int rpsize = (naddrc + nsizec) * sizeof(unsigned int);
  449. rp = (unsigned int *) get_property(np, "reg", &l);
  450. if (rp != 0 && l >= rpsize) {
  451. i = 0;
  452. adr = (struct address_range *) (*mem_start);
  453. while ((l -= rpsize) >= 0) {
  454. if (!measure_only) {
  455. adr[i].space = 0;
  456. adr[i].address = rp[naddrc - 1];
  457. adr[i].size = rp[naddrc + nsizec - 1];
  458. }
  459. ++i;
  460. rp += naddrc + nsizec;
  461. }
  462. np->addrs = adr;
  463. np->n_addrs = i;
  464. (*mem_start) += i * sizeof(struct address_range);
  465. }
  466. return 0;
  467. }
  468. static int __devinit finish_node(struct device_node *np,
  469. unsigned long *mem_start,
  470. interpret_func *ifunc,
  471. int naddrc, int nsizec,
  472. int measure_only)
  473. {
  474. struct device_node *child;
  475. int *ip, rc = 0;
  476. /* get the device addresses and interrupts */
  477. if (ifunc != NULL)
  478. rc = ifunc(np, mem_start, naddrc, nsizec, measure_only);
  479. if (rc)
  480. goto out;
  481. rc = finish_node_interrupts(np, mem_start, measure_only);
  482. if (rc)
  483. goto out;
  484. /* Look for #address-cells and #size-cells properties. */
  485. ip = (int *) get_property(np, "#address-cells", NULL);
  486. if (ip != NULL)
  487. naddrc = *ip;
  488. ip = (int *) get_property(np, "#size-cells", NULL);
  489. if (ip != NULL)
  490. nsizec = *ip;
  491. if (!strcmp(np->name, "device-tree") || np->parent == NULL)
  492. ifunc = interpret_root_props;
  493. else if (np->type == 0)
  494. ifunc = NULL;
  495. else if (!strcmp(np->type, "pci") || !strcmp(np->type, "vci"))
  496. ifunc = interpret_pci_props;
  497. else if (!strcmp(np->type, "dbdma"))
  498. ifunc = interpret_dbdma_props;
  499. else if (!strcmp(np->type, "mac-io") || ifunc == interpret_macio_props)
  500. ifunc = interpret_macio_props;
  501. else if (!strcmp(np->type, "isa"))
  502. ifunc = interpret_isa_props;
  503. else if (!strcmp(np->name, "uni-n") || !strcmp(np->name, "u3"))
  504. ifunc = interpret_root_props;
  505. else if (!((ifunc == interpret_dbdma_props
  506. || ifunc == interpret_macio_props)
  507. && (!strcmp(np->type, "escc")
  508. || !strcmp(np->type, "media-bay"))))
  509. ifunc = NULL;
  510. for (child = np->child; child != NULL; child = child->sibling) {
  511. rc = finish_node(child, mem_start, ifunc,
  512. naddrc, nsizec, measure_only);
  513. if (rc)
  514. goto out;
  515. }
  516. out:
  517. return rc;
  518. }
  519. /**
  520. * finish_device_tree is called once things are running normally
  521. * (i.e. with text and data mapped to the address they were linked at).
  522. * It traverses the device tree and fills in some of the additional,
  523. * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
  524. * mapping is also initialized at this point.
  525. */
  526. void __init finish_device_tree(void)
  527. {
  528. unsigned long start, end, size = 0;
  529. DBG(" -> finish_device_tree\n");
  530. if (ppc64_interrupt_controller == IC_INVALID) {
  531. DBG("failed to configure interrupt controller type\n");
  532. panic("failed to configure interrupt controller type\n");
  533. }
  534. /* Initialize virtual IRQ map */
  535. virt_irq_init();
  536. /*
  537. * Finish device-tree (pre-parsing some properties etc...)
  538. * We do this in 2 passes. One with "measure_only" set, which
  539. * will only measure the amount of memory needed, then we can
  540. * allocate that memory, and call finish_node again. However,
  541. * we must be careful as most routines will fail nowadays when
  542. * prom_alloc() returns 0, so we must make sure our first pass
  543. * doesn't start at 0. We pre-initialize size to 16 for that
  544. * reason and then remove those additional 16 bytes
  545. */
  546. size = 16;
  547. finish_node(allnodes, &size, NULL, 0, 0, 1);
  548. size -= 16;
  549. end = start = (unsigned long)abs_to_virt(lmb_alloc(size, 128));
  550. finish_node(allnodes, &end, NULL, 0, 0, 0);
  551. BUG_ON(end != start + size);
  552. DBG(" <- finish_device_tree\n");
  553. }
  554. #ifdef DEBUG
  555. #define printk udbg_printf
  556. #endif
  557. static inline char *find_flat_dt_string(u32 offset)
  558. {
  559. return ((char *)initial_boot_params) +
  560. initial_boot_params->off_dt_strings + offset;
  561. }
  562. /**
  563. * This function is used to scan the flattened device-tree, it is
  564. * used to extract the memory informations at boot before we can
  565. * unflatten the tree
  566. */
  567. static int __init scan_flat_dt(int (*it)(unsigned long node,
  568. const char *uname, int depth,
  569. void *data),
  570. void *data)
  571. {
  572. unsigned long p = ((unsigned long)initial_boot_params) +
  573. initial_boot_params->off_dt_struct;
  574. int rc = 0;
  575. int depth = -1;
  576. do {
  577. u32 tag = *((u32 *)p);
  578. char *pathp;
  579. p += 4;
  580. if (tag == OF_DT_END_NODE) {
  581. depth --;
  582. continue;
  583. }
  584. if (tag == OF_DT_NOP)
  585. continue;
  586. if (tag == OF_DT_END)
  587. break;
  588. if (tag == OF_DT_PROP) {
  589. u32 sz = *((u32 *)p);
  590. p += 8;
  591. if (initial_boot_params->version < 0x10)
  592. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  593. p += sz;
  594. p = _ALIGN(p, 4);
  595. continue;
  596. }
  597. if (tag != OF_DT_BEGIN_NODE) {
  598. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  599. " device tree !\n", tag);
  600. return -EINVAL;
  601. }
  602. depth++;
  603. pathp = (char *)p;
  604. p = _ALIGN(p + strlen(pathp) + 1, 4);
  605. if ((*pathp) == '/') {
  606. char *lp, *np;
  607. for (lp = NULL, np = pathp; *np; np++)
  608. if ((*np) == '/')
  609. lp = np+1;
  610. if (lp != NULL)
  611. pathp = lp;
  612. }
  613. rc = it(p, pathp, depth, data);
  614. if (rc != 0)
  615. break;
  616. } while(1);
  617. return rc;
  618. }
  619. /**
  620. * This function can be used within scan_flattened_dt callback to get
  621. * access to properties
  622. */
  623. static void* __init get_flat_dt_prop(unsigned long node, const char *name,
  624. unsigned long *size)
  625. {
  626. unsigned long p = node;
  627. do {
  628. u32 tag = *((u32 *)p);
  629. u32 sz, noff;
  630. const char *nstr;
  631. p += 4;
  632. if (tag == OF_DT_NOP)
  633. continue;
  634. if (tag != OF_DT_PROP)
  635. return NULL;
  636. sz = *((u32 *)p);
  637. noff = *((u32 *)(p + 4));
  638. p += 8;
  639. if (initial_boot_params->version < 0x10)
  640. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  641. nstr = find_flat_dt_string(noff);
  642. if (nstr == NULL) {
  643. printk(KERN_WARNING "Can't find property index"
  644. " name !\n");
  645. return NULL;
  646. }
  647. if (strcmp(name, nstr) == 0) {
  648. if (size)
  649. *size = sz;
  650. return (void *)p;
  651. }
  652. p += sz;
  653. p = _ALIGN(p, 4);
  654. } while(1);
  655. }
  656. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  657. unsigned long align)
  658. {
  659. void *res;
  660. *mem = _ALIGN(*mem, align);
  661. res = (void *)*mem;
  662. *mem += size;
  663. return res;
  664. }
  665. static unsigned long __init unflatten_dt_node(unsigned long mem,
  666. unsigned long *p,
  667. struct device_node *dad,
  668. struct device_node ***allnextpp,
  669. unsigned long fpsize)
  670. {
  671. struct device_node *np;
  672. struct property *pp, **prev_pp = NULL;
  673. char *pathp;
  674. u32 tag;
  675. unsigned int l, allocl;
  676. int has_name = 0;
  677. int new_format = 0;
  678. tag = *((u32 *)(*p));
  679. if (tag != OF_DT_BEGIN_NODE) {
  680. printk("Weird tag at start of node: %x\n", tag);
  681. return mem;
  682. }
  683. *p += 4;
  684. pathp = (char *)*p;
  685. l = allocl = strlen(pathp) + 1;
  686. *p = _ALIGN(*p + l, 4);
  687. /* version 0x10 has a more compact unit name here instead of the full
  688. * path. we accumulate the full path size using "fpsize", we'll rebuild
  689. * it later. We detect this because the first character of the name is
  690. * not '/'.
  691. */
  692. if ((*pathp) != '/') {
  693. new_format = 1;
  694. if (fpsize == 0) {
  695. /* root node: special case. fpsize accounts for path
  696. * plus terminating zero. root node only has '/', so
  697. * fpsize should be 2, but we want to avoid the first
  698. * level nodes to have two '/' so we use fpsize 1 here
  699. */
  700. fpsize = 1;
  701. allocl = 2;
  702. } else {
  703. /* account for '/' and path size minus terminal 0
  704. * already in 'l'
  705. */
  706. fpsize += l;
  707. allocl = fpsize;
  708. }
  709. }
  710. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  711. __alignof__(struct device_node));
  712. if (allnextpp) {
  713. memset(np, 0, sizeof(*np));
  714. np->full_name = ((char*)np) + sizeof(struct device_node);
  715. if (new_format) {
  716. char *p = np->full_name;
  717. /* rebuild full path for new format */
  718. if (dad && dad->parent) {
  719. strcpy(p, dad->full_name);
  720. #ifdef DEBUG
  721. if ((strlen(p) + l + 1) != allocl) {
  722. DBG("%s: p: %d, l: %d, a: %d\n",
  723. pathp, strlen(p), l, allocl);
  724. }
  725. #endif
  726. p += strlen(p);
  727. }
  728. *(p++) = '/';
  729. memcpy(p, pathp, l);
  730. } else
  731. memcpy(np->full_name, pathp, l);
  732. prev_pp = &np->properties;
  733. **allnextpp = np;
  734. *allnextpp = &np->allnext;
  735. if (dad != NULL) {
  736. np->parent = dad;
  737. /* we temporarily use the next field as `last_child'*/
  738. if (dad->next == 0)
  739. dad->child = np;
  740. else
  741. dad->next->sibling = np;
  742. dad->next = np;
  743. }
  744. kref_init(&np->kref);
  745. }
  746. while(1) {
  747. u32 sz, noff;
  748. char *pname;
  749. tag = *((u32 *)(*p));
  750. if (tag == OF_DT_NOP) {
  751. *p += 4;
  752. continue;
  753. }
  754. if (tag != OF_DT_PROP)
  755. break;
  756. *p += 4;
  757. sz = *((u32 *)(*p));
  758. noff = *((u32 *)((*p) + 4));
  759. *p += 8;
  760. if (initial_boot_params->version < 0x10)
  761. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  762. pname = find_flat_dt_string(noff);
  763. if (pname == NULL) {
  764. printk("Can't find property name in list !\n");
  765. break;
  766. }
  767. if (strcmp(pname, "name") == 0)
  768. has_name = 1;
  769. l = strlen(pname) + 1;
  770. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  771. __alignof__(struct property));
  772. if (allnextpp) {
  773. if (strcmp(pname, "linux,phandle") == 0) {
  774. np->node = *((u32 *)*p);
  775. if (np->linux_phandle == 0)
  776. np->linux_phandle = np->node;
  777. }
  778. if (strcmp(pname, "ibm,phandle") == 0)
  779. np->linux_phandle = *((u32 *)*p);
  780. pp->name = pname;
  781. pp->length = sz;
  782. pp->value = (void *)*p;
  783. *prev_pp = pp;
  784. prev_pp = &pp->next;
  785. }
  786. *p = _ALIGN((*p) + sz, 4);
  787. }
  788. /* with version 0x10 we may not have the name property, recreate
  789. * it here from the unit name if absent
  790. */
  791. if (!has_name) {
  792. char *p = pathp, *ps = pathp, *pa = NULL;
  793. int sz;
  794. while (*p) {
  795. if ((*p) == '@')
  796. pa = p;
  797. if ((*p) == '/')
  798. ps = p + 1;
  799. p++;
  800. }
  801. if (pa < ps)
  802. pa = p;
  803. sz = (pa - ps) + 1;
  804. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  805. __alignof__(struct property));
  806. if (allnextpp) {
  807. pp->name = "name";
  808. pp->length = sz;
  809. pp->value = (unsigned char *)(pp + 1);
  810. *prev_pp = pp;
  811. prev_pp = &pp->next;
  812. memcpy(pp->value, ps, sz - 1);
  813. ((char *)pp->value)[sz - 1] = 0;
  814. DBG("fixed up name for %s -> %s\n", pathp, pp->value);
  815. }
  816. }
  817. if (allnextpp) {
  818. *prev_pp = NULL;
  819. np->name = get_property(np, "name", NULL);
  820. np->type = get_property(np, "device_type", NULL);
  821. if (!np->name)
  822. np->name = "<NULL>";
  823. if (!np->type)
  824. np->type = "<NULL>";
  825. }
  826. while (tag == OF_DT_BEGIN_NODE) {
  827. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  828. tag = *((u32 *)(*p));
  829. }
  830. if (tag != OF_DT_END_NODE) {
  831. printk("Weird tag at end of node: %x\n", tag);
  832. return mem;
  833. }
  834. *p += 4;
  835. return mem;
  836. }
  837. /**
  838. * unflattens the device-tree passed by the firmware, creating the
  839. * tree of struct device_node. It also fills the "name" and "type"
  840. * pointers of the nodes so the normal device-tree walking functions
  841. * can be used (this used to be done by finish_device_tree)
  842. */
  843. void __init unflatten_device_tree(void)
  844. {
  845. unsigned long start, mem, size;
  846. struct device_node **allnextp = &allnodes;
  847. char *p = NULL;
  848. int l = 0;
  849. DBG(" -> unflatten_device_tree()\n");
  850. /* First pass, scan for size */
  851. start = ((unsigned long)initial_boot_params) +
  852. initial_boot_params->off_dt_struct;
  853. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  854. size = (size | 3) + 1;
  855. DBG(" size is %lx, allocating...\n", size);
  856. /* Allocate memory for the expanded device tree */
  857. mem = (unsigned long)abs_to_virt(lmb_alloc(size + 4,
  858. __alignof__(struct device_node)));
  859. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  860. DBG(" unflattening...\n", mem);
  861. /* Second pass, do actual unflattening */
  862. start = ((unsigned long)initial_boot_params) +
  863. initial_boot_params->off_dt_struct;
  864. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  865. if (*((u32 *)start) != OF_DT_END)
  866. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  867. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  868. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  869. ((u32 *)mem)[size / 4] );
  870. *allnextp = NULL;
  871. /* Get pointer to OF "/chosen" node for use everywhere */
  872. of_chosen = of_find_node_by_path("/chosen");
  873. /* Retreive command line */
  874. if (of_chosen != NULL) {
  875. p = (char *)get_property(of_chosen, "bootargs", &l);
  876. if (p != NULL && l > 0)
  877. strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
  878. }
  879. #ifdef CONFIG_CMDLINE
  880. if (l == 0 || (l == 1 && (*p) == 0))
  881. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  882. #endif /* CONFIG_CMDLINE */
  883. DBG("Command line is: %s\n", cmd_line);
  884. DBG(" <- unflatten_device_tree()\n");
  885. }
  886. static int __init early_init_dt_scan_cpus(unsigned long node,
  887. const char *uname, int depth, void *data)
  888. {
  889. char *type = get_flat_dt_prop(node, "device_type", NULL);
  890. u32 *prop;
  891. unsigned long size;
  892. /* We are scanning "cpu" nodes only */
  893. if (type == NULL || strcmp(type, "cpu") != 0)
  894. return 0;
  895. /* On LPAR, look for the first ibm,pft-size property for the hash table size
  896. */
  897. if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
  898. u32 *pft_size;
  899. pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
  900. if (pft_size != NULL) {
  901. /* pft_size[0] is the NUMA CEC cookie */
  902. ppc64_pft_size = pft_size[1];
  903. }
  904. }
  905. if (initial_boot_params && initial_boot_params->version >= 2) {
  906. /* version 2 of the kexec param format adds the phys cpuid
  907. * of booted proc.
  908. */
  909. boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
  910. boot_cpuid = 0;
  911. } else {
  912. /* Check if it's the boot-cpu, set it's hw index in paca now */
  913. if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
  914. u32 *prop = get_flat_dt_prop(node, "reg", NULL);
  915. set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
  916. boot_cpuid_phys = get_hard_smp_processor_id(0);
  917. }
  918. }
  919. #ifdef CONFIG_ALTIVEC
  920. /* Check if we have a VMX and eventually update CPU features */
  921. prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
  922. if (prop && (*prop) > 0) {
  923. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  924. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  925. }
  926. /* Same goes for Apple's "altivec" property */
  927. prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
  928. if (prop) {
  929. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  930. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  931. }
  932. #endif /* CONFIG_ALTIVEC */
  933. /*
  934. * Check for an SMT capable CPU and set the CPU feature. We do
  935. * this by looking at the size of the ibm,ppc-interrupt-server#s
  936. * property
  937. */
  938. prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
  939. &size);
  940. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  941. if (prop && ((size / sizeof(u32)) > 1))
  942. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  943. return 0;
  944. }
  945. static int __init early_init_dt_scan_chosen(unsigned long node,
  946. const char *uname, int depth, void *data)
  947. {
  948. u32 *prop;
  949. u64 *prop64;
  950. extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
  951. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  952. if (depth != 1 || strcmp(uname, "chosen") != 0)
  953. return 0;
  954. /* get platform type */
  955. prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
  956. if (prop == NULL)
  957. return 0;
  958. systemcfg->platform = *prop;
  959. /* check if iommu is forced on or off */
  960. if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  961. iommu_is_off = 1;
  962. if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  963. iommu_force_on = 1;
  964. prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
  965. if (prop64)
  966. memory_limit = *prop64;
  967. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  968. if (prop64)
  969. tce_alloc_start = *prop64;
  970. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  971. if (prop64)
  972. tce_alloc_end = *prop64;
  973. #ifdef CONFIG_PPC_RTAS
  974. /* To help early debugging via the front panel, we retreive a minimal
  975. * set of RTAS infos now if available
  976. */
  977. {
  978. u64 *basep, *entryp;
  979. basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
  980. entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  981. prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
  982. if (basep && entryp && prop) {
  983. rtas.base = *basep;
  984. rtas.entry = *entryp;
  985. rtas.size = *prop;
  986. }
  987. }
  988. #endif /* CONFIG_PPC_RTAS */
  989. /* break now */
  990. return 1;
  991. }
  992. static int __init early_init_dt_scan_root(unsigned long node,
  993. const char *uname, int depth, void *data)
  994. {
  995. u32 *prop;
  996. if (depth != 0)
  997. return 0;
  998. prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
  999. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  1000. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  1001. prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
  1002. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  1003. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  1004. /* break now */
  1005. return 1;
  1006. }
  1007. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  1008. {
  1009. cell_t *p = *cellp;
  1010. unsigned long r = 0;
  1011. /* Ignore more than 2 cells */
  1012. while (s > 2) {
  1013. p++;
  1014. s--;
  1015. }
  1016. while (s) {
  1017. r <<= 32;
  1018. r |= *(p++);
  1019. s--;
  1020. }
  1021. *cellp = p;
  1022. return r;
  1023. }
  1024. static int __init early_init_dt_scan_memory(unsigned long node,
  1025. const char *uname, int depth, void *data)
  1026. {
  1027. char *type = get_flat_dt_prop(node, "device_type", NULL);
  1028. cell_t *reg, *endp;
  1029. unsigned long l;
  1030. /* We are scanning "memory" nodes only */
  1031. if (type == NULL || strcmp(type, "memory") != 0)
  1032. return 0;
  1033. reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
  1034. if (reg == NULL)
  1035. return 0;
  1036. endp = reg + (l / sizeof(cell_t));
  1037. DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
  1038. uname, l, reg[0], reg[1], reg[2], reg[3]);
  1039. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  1040. unsigned long base, size;
  1041. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  1042. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  1043. if (size == 0)
  1044. continue;
  1045. DBG(" - %lx , %lx\n", base, size);
  1046. if (iommu_is_off) {
  1047. if (base >= 0x80000000ul)
  1048. continue;
  1049. if ((base + size) > 0x80000000ul)
  1050. size = 0x80000000ul - base;
  1051. }
  1052. lmb_add(base, size);
  1053. }
  1054. return 0;
  1055. }
  1056. static void __init early_reserve_mem(void)
  1057. {
  1058. u64 base, size;
  1059. u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  1060. initial_boot_params->off_mem_rsvmap);
  1061. while (1) {
  1062. base = *(reserve_map++);
  1063. size = *(reserve_map++);
  1064. if (size == 0)
  1065. break;
  1066. DBG("reserving: %lx -> %lx\n", base, size);
  1067. lmb_reserve(base, size);
  1068. }
  1069. #if 0
  1070. DBG("memory reserved, lmbs :\n");
  1071. lmb_dump_all();
  1072. #endif
  1073. }
  1074. void __init early_init_devtree(void *params)
  1075. {
  1076. DBG(" -> early_init_devtree()\n");
  1077. /* Setup flat device-tree pointer */
  1078. initial_boot_params = params;
  1079. /* By default, hash size is not set */
  1080. ppc64_pft_size = 0;
  1081. /* Retreive various informations from the /chosen node of the
  1082. * device-tree, including the platform type, initrd location and
  1083. * size, TCE reserve, and more ...
  1084. */
  1085. scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1086. /* Scan memory nodes and rebuild LMBs */
  1087. lmb_init();
  1088. scan_flat_dt(early_init_dt_scan_root, NULL);
  1089. scan_flat_dt(early_init_dt_scan_memory, NULL);
  1090. lmb_enforce_memory_limit();
  1091. lmb_analyze();
  1092. systemcfg->physicalMemorySize = lmb_phys_mem_size();
  1093. lmb_reserve(0, __pa(klimit));
  1094. DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
  1095. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1096. early_reserve_mem();
  1097. DBG("Scanning CPUs ...\n");
  1098. /* Retreive hash table size from flattened tree plus other
  1099. * CPU related informations (altivec support, boot CPU ID, ...)
  1100. */
  1101. scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1102. /* If hash size wasn't obtained above, we calculate it now based on
  1103. * the total RAM size
  1104. */
  1105. if (ppc64_pft_size == 0) {
  1106. unsigned long rnd_mem_size, pteg_count;
  1107. /* round mem_size up to next power of 2 */
  1108. rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
  1109. if (rnd_mem_size < systemcfg->physicalMemorySize)
  1110. rnd_mem_size <<= 1;
  1111. /* # pages / 2 */
  1112. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  1113. ppc64_pft_size = __ilog2(pteg_count << 7);
  1114. }
  1115. DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
  1116. DBG(" <- early_init_devtree()\n");
  1117. }
  1118. #undef printk
  1119. int
  1120. prom_n_addr_cells(struct device_node* np)
  1121. {
  1122. int* ip;
  1123. do {
  1124. if (np->parent)
  1125. np = np->parent;
  1126. ip = (int *) get_property(np, "#address-cells", NULL);
  1127. if (ip != NULL)
  1128. return *ip;
  1129. } while (np->parent);
  1130. /* No #address-cells property for the root node, default to 1 */
  1131. return 1;
  1132. }
  1133. int
  1134. prom_n_size_cells(struct device_node* np)
  1135. {
  1136. int* ip;
  1137. do {
  1138. if (np->parent)
  1139. np = np->parent;
  1140. ip = (int *) get_property(np, "#size-cells", NULL);
  1141. if (ip != NULL)
  1142. return *ip;
  1143. } while (np->parent);
  1144. /* No #size-cells property for the root node, default to 1 */
  1145. return 1;
  1146. }
  1147. /**
  1148. * Work out the sense (active-low level / active-high edge)
  1149. * of each interrupt from the device tree.
  1150. */
  1151. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1152. {
  1153. struct device_node *np;
  1154. int i, j;
  1155. /* default to level-triggered */
  1156. memset(senses, 1, max - off);
  1157. for (np = allnodes; np != 0; np = np->allnext) {
  1158. for (j = 0; j < np->n_intrs; j++) {
  1159. i = np->intrs[j].line;
  1160. if (i >= off && i < max)
  1161. senses[i-off] = np->intrs[j].sense ?
  1162. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
  1163. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
  1164. }
  1165. }
  1166. }
  1167. /**
  1168. * Construct and return a list of the device_nodes with a given name.
  1169. */
  1170. struct device_node *
  1171. find_devices(const char *name)
  1172. {
  1173. struct device_node *head, **prevp, *np;
  1174. prevp = &head;
  1175. for (np = allnodes; np != 0; np = np->allnext) {
  1176. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1177. *prevp = np;
  1178. prevp = &np->next;
  1179. }
  1180. }
  1181. *prevp = NULL;
  1182. return head;
  1183. }
  1184. EXPORT_SYMBOL(find_devices);
  1185. /**
  1186. * Construct and return a list of the device_nodes with a given type.
  1187. */
  1188. struct device_node *
  1189. find_type_devices(const char *type)
  1190. {
  1191. struct device_node *head, **prevp, *np;
  1192. prevp = &head;
  1193. for (np = allnodes; np != 0; np = np->allnext) {
  1194. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1195. *prevp = np;
  1196. prevp = &np->next;
  1197. }
  1198. }
  1199. *prevp = NULL;
  1200. return head;
  1201. }
  1202. EXPORT_SYMBOL(find_type_devices);
  1203. /**
  1204. * Returns all nodes linked together
  1205. */
  1206. struct device_node *
  1207. find_all_nodes(void)
  1208. {
  1209. struct device_node *head, **prevp, *np;
  1210. prevp = &head;
  1211. for (np = allnodes; np != 0; np = np->allnext) {
  1212. *prevp = np;
  1213. prevp = &np->next;
  1214. }
  1215. *prevp = NULL;
  1216. return head;
  1217. }
  1218. EXPORT_SYMBOL(find_all_nodes);
  1219. /** Checks if the given "compat" string matches one of the strings in
  1220. * the device's "compatible" property
  1221. */
  1222. int
  1223. device_is_compatible(struct device_node *device, const char *compat)
  1224. {
  1225. const char* cp;
  1226. int cplen, l;
  1227. cp = (char *) get_property(device, "compatible", &cplen);
  1228. if (cp == NULL)
  1229. return 0;
  1230. while (cplen > 0) {
  1231. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1232. return 1;
  1233. l = strlen(cp) + 1;
  1234. cp += l;
  1235. cplen -= l;
  1236. }
  1237. return 0;
  1238. }
  1239. EXPORT_SYMBOL(device_is_compatible);
  1240. /**
  1241. * Indicates whether the root node has a given value in its
  1242. * compatible property.
  1243. */
  1244. int
  1245. machine_is_compatible(const char *compat)
  1246. {
  1247. struct device_node *root;
  1248. int rc = 0;
  1249. root = of_find_node_by_path("/");
  1250. if (root) {
  1251. rc = device_is_compatible(root, compat);
  1252. of_node_put(root);
  1253. }
  1254. return rc;
  1255. }
  1256. EXPORT_SYMBOL(machine_is_compatible);
  1257. /**
  1258. * Construct and return a list of the device_nodes with a given type
  1259. * and compatible property.
  1260. */
  1261. struct device_node *
  1262. find_compatible_devices(const char *type, const char *compat)
  1263. {
  1264. struct device_node *head, **prevp, *np;
  1265. prevp = &head;
  1266. for (np = allnodes; np != 0; np = np->allnext) {
  1267. if (type != NULL
  1268. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1269. continue;
  1270. if (device_is_compatible(np, compat)) {
  1271. *prevp = np;
  1272. prevp = &np->next;
  1273. }
  1274. }
  1275. *prevp = NULL;
  1276. return head;
  1277. }
  1278. EXPORT_SYMBOL(find_compatible_devices);
  1279. /**
  1280. * Find the device_node with a given full_name.
  1281. */
  1282. struct device_node *
  1283. find_path_device(const char *path)
  1284. {
  1285. struct device_node *np;
  1286. for (np = allnodes; np != 0; np = np->allnext)
  1287. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1288. return np;
  1289. return NULL;
  1290. }
  1291. EXPORT_SYMBOL(find_path_device);
  1292. /*******
  1293. *
  1294. * New implementation of the OF "find" APIs, return a refcounted
  1295. * object, call of_node_put() when done. The device tree and list
  1296. * are protected by a rw_lock.
  1297. *
  1298. * Note that property management will need some locking as well,
  1299. * this isn't dealt with yet.
  1300. *
  1301. *******/
  1302. /**
  1303. * of_find_node_by_name - Find a node by its "name" property
  1304. * @from: The node to start searching from or NULL, the node
  1305. * you pass will not be searched, only the next one
  1306. * will; typically, you pass what the previous call
  1307. * returned. of_node_put() will be called on it
  1308. * @name: The name string to match against
  1309. *
  1310. * Returns a node pointer with refcount incremented, use
  1311. * of_node_put() on it when done.
  1312. */
  1313. struct device_node *of_find_node_by_name(struct device_node *from,
  1314. const char *name)
  1315. {
  1316. struct device_node *np;
  1317. read_lock(&devtree_lock);
  1318. np = from ? from->allnext : allnodes;
  1319. for (; np != 0; np = np->allnext)
  1320. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1321. && of_node_get(np))
  1322. break;
  1323. if (from)
  1324. of_node_put(from);
  1325. read_unlock(&devtree_lock);
  1326. return np;
  1327. }
  1328. EXPORT_SYMBOL(of_find_node_by_name);
  1329. /**
  1330. * of_find_node_by_type - Find a node by its "device_type" property
  1331. * @from: The node to start searching from or NULL, the node
  1332. * you pass will not be searched, only the next one
  1333. * will; typically, you pass what the previous call
  1334. * returned. of_node_put() will be called on it
  1335. * @name: The type string to match against
  1336. *
  1337. * Returns a node pointer with refcount incremented, use
  1338. * of_node_put() on it when done.
  1339. */
  1340. struct device_node *of_find_node_by_type(struct device_node *from,
  1341. const char *type)
  1342. {
  1343. struct device_node *np;
  1344. read_lock(&devtree_lock);
  1345. np = from ? from->allnext : allnodes;
  1346. for (; np != 0; np = np->allnext)
  1347. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1348. && of_node_get(np))
  1349. break;
  1350. if (from)
  1351. of_node_put(from);
  1352. read_unlock(&devtree_lock);
  1353. return np;
  1354. }
  1355. EXPORT_SYMBOL(of_find_node_by_type);
  1356. /**
  1357. * of_find_compatible_node - Find a node based on type and one of the
  1358. * tokens in its "compatible" property
  1359. * @from: The node to start searching from or NULL, the node
  1360. * you pass will not be searched, only the next one
  1361. * will; typically, you pass what the previous call
  1362. * returned. of_node_put() will be called on it
  1363. * @type: The type string to match "device_type" or NULL to ignore
  1364. * @compatible: The string to match to one of the tokens in the device
  1365. * "compatible" list.
  1366. *
  1367. * Returns a node pointer with refcount incremented, use
  1368. * of_node_put() on it when done.
  1369. */
  1370. struct device_node *of_find_compatible_node(struct device_node *from,
  1371. const char *type, const char *compatible)
  1372. {
  1373. struct device_node *np;
  1374. read_lock(&devtree_lock);
  1375. np = from ? from->allnext : allnodes;
  1376. for (; np != 0; np = np->allnext) {
  1377. if (type != NULL
  1378. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1379. continue;
  1380. if (device_is_compatible(np, compatible) && of_node_get(np))
  1381. break;
  1382. }
  1383. if (from)
  1384. of_node_put(from);
  1385. read_unlock(&devtree_lock);
  1386. return np;
  1387. }
  1388. EXPORT_SYMBOL(of_find_compatible_node);
  1389. /**
  1390. * of_find_node_by_path - Find a node matching a full OF path
  1391. * @path: The full path to match
  1392. *
  1393. * Returns a node pointer with refcount incremented, use
  1394. * of_node_put() on it when done.
  1395. */
  1396. struct device_node *of_find_node_by_path(const char *path)
  1397. {
  1398. struct device_node *np = allnodes;
  1399. read_lock(&devtree_lock);
  1400. for (; np != 0; np = np->allnext) {
  1401. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1402. && of_node_get(np))
  1403. break;
  1404. }
  1405. read_unlock(&devtree_lock);
  1406. return np;
  1407. }
  1408. EXPORT_SYMBOL(of_find_node_by_path);
  1409. /**
  1410. * of_find_node_by_phandle - Find a node given a phandle
  1411. * @handle: phandle of the node to find
  1412. *
  1413. * Returns a node pointer with refcount incremented, use
  1414. * of_node_put() on it when done.
  1415. */
  1416. struct device_node *of_find_node_by_phandle(phandle handle)
  1417. {
  1418. struct device_node *np;
  1419. read_lock(&devtree_lock);
  1420. for (np = allnodes; np != 0; np = np->allnext)
  1421. if (np->linux_phandle == handle)
  1422. break;
  1423. if (np)
  1424. of_node_get(np);
  1425. read_unlock(&devtree_lock);
  1426. return np;
  1427. }
  1428. EXPORT_SYMBOL(of_find_node_by_phandle);
  1429. /**
  1430. * of_find_all_nodes - Get next node in global list
  1431. * @prev: Previous node or NULL to start iteration
  1432. * of_node_put() will be called on it
  1433. *
  1434. * Returns a node pointer with refcount incremented, use
  1435. * of_node_put() on it when done.
  1436. */
  1437. struct device_node *of_find_all_nodes(struct device_node *prev)
  1438. {
  1439. struct device_node *np;
  1440. read_lock(&devtree_lock);
  1441. np = prev ? prev->allnext : allnodes;
  1442. for (; np != 0; np = np->allnext)
  1443. if (of_node_get(np))
  1444. break;
  1445. if (prev)
  1446. of_node_put(prev);
  1447. read_unlock(&devtree_lock);
  1448. return np;
  1449. }
  1450. EXPORT_SYMBOL(of_find_all_nodes);
  1451. /**
  1452. * of_get_parent - Get a node's parent if any
  1453. * @node: Node to get parent
  1454. *
  1455. * Returns a node pointer with refcount incremented, use
  1456. * of_node_put() on it when done.
  1457. */
  1458. struct device_node *of_get_parent(const struct device_node *node)
  1459. {
  1460. struct device_node *np;
  1461. if (!node)
  1462. return NULL;
  1463. read_lock(&devtree_lock);
  1464. np = of_node_get(node->parent);
  1465. read_unlock(&devtree_lock);
  1466. return np;
  1467. }
  1468. EXPORT_SYMBOL(of_get_parent);
  1469. /**
  1470. * of_get_next_child - Iterate a node childs
  1471. * @node: parent node
  1472. * @prev: previous child of the parent node, or NULL to get first
  1473. *
  1474. * Returns a node pointer with refcount incremented, use
  1475. * of_node_put() on it when done.
  1476. */
  1477. struct device_node *of_get_next_child(const struct device_node *node,
  1478. struct device_node *prev)
  1479. {
  1480. struct device_node *next;
  1481. read_lock(&devtree_lock);
  1482. next = prev ? prev->sibling : node->child;
  1483. for (; next != 0; next = next->sibling)
  1484. if (of_node_get(next))
  1485. break;
  1486. if (prev)
  1487. of_node_put(prev);
  1488. read_unlock(&devtree_lock);
  1489. return next;
  1490. }
  1491. EXPORT_SYMBOL(of_get_next_child);
  1492. /**
  1493. * of_node_get - Increment refcount of a node
  1494. * @node: Node to inc refcount, NULL is supported to
  1495. * simplify writing of callers
  1496. *
  1497. * Returns node.
  1498. */
  1499. struct device_node *of_node_get(struct device_node *node)
  1500. {
  1501. if (node)
  1502. kref_get(&node->kref);
  1503. return node;
  1504. }
  1505. EXPORT_SYMBOL(of_node_get);
  1506. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1507. {
  1508. return container_of(kref, struct device_node, kref);
  1509. }
  1510. /**
  1511. * of_node_release - release a dynamically allocated node
  1512. * @kref: kref element of the node to be released
  1513. *
  1514. * In of_node_put() this function is passed to kref_put()
  1515. * as the destructor.
  1516. */
  1517. static void of_node_release(struct kref *kref)
  1518. {
  1519. struct device_node *node = kref_to_device_node(kref);
  1520. struct property *prop = node->properties;
  1521. if (!OF_IS_DYNAMIC(node))
  1522. return;
  1523. while (prop) {
  1524. struct property *next = prop->next;
  1525. kfree(prop->name);
  1526. kfree(prop->value);
  1527. kfree(prop);
  1528. prop = next;
  1529. }
  1530. kfree(node->intrs);
  1531. kfree(node->addrs);
  1532. kfree(node->full_name);
  1533. kfree(node);
  1534. }
  1535. /**
  1536. * of_node_put - Decrement refcount of a node
  1537. * @node: Node to dec refcount, NULL is supported to
  1538. * simplify writing of callers
  1539. *
  1540. */
  1541. void of_node_put(struct device_node *node)
  1542. {
  1543. if (node)
  1544. kref_put(&node->kref, of_node_release);
  1545. }
  1546. EXPORT_SYMBOL(of_node_put);
  1547. /*
  1548. * Fix up the uninitialized fields in a new device node:
  1549. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1550. *
  1551. * A lot of boot-time code is duplicated here, because functions such
  1552. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1553. * slab allocator.
  1554. *
  1555. * This should probably be split up into smaller chunks.
  1556. */
  1557. static int of_finish_dynamic_node(struct device_node *node,
  1558. unsigned long *unused1, int unused2,
  1559. int unused3, int unused4)
  1560. {
  1561. struct device_node *parent = of_get_parent(node);
  1562. int err = 0;
  1563. phandle *ibm_phandle;
  1564. node->name = get_property(node, "name", NULL);
  1565. node->type = get_property(node, "device_type", NULL);
  1566. if (!parent) {
  1567. err = -ENODEV;
  1568. goto out;
  1569. }
  1570. /* We don't support that function on PowerMac, at least
  1571. * not yet
  1572. */
  1573. if (systemcfg->platform == PLATFORM_POWERMAC)
  1574. return -ENODEV;
  1575. /* fix up new node's linux_phandle field */
  1576. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1577. node->linux_phandle = *ibm_phandle;
  1578. out:
  1579. of_node_put(parent);
  1580. return err;
  1581. }
  1582. /*
  1583. * Plug a device node into the tree and global list.
  1584. */
  1585. void of_attach_node(struct device_node *np)
  1586. {
  1587. write_lock(&devtree_lock);
  1588. np->sibling = np->parent->child;
  1589. np->allnext = allnodes;
  1590. np->parent->child = np;
  1591. allnodes = np;
  1592. write_unlock(&devtree_lock);
  1593. }
  1594. /*
  1595. * "Unplug" a node from the device tree. The caller must hold
  1596. * a reference to the node. The memory associated with the node
  1597. * is not freed until its refcount goes to zero.
  1598. */
  1599. void of_detach_node(const struct device_node *np)
  1600. {
  1601. struct device_node *parent;
  1602. write_lock(&devtree_lock);
  1603. parent = np->parent;
  1604. if (allnodes == np)
  1605. allnodes = np->allnext;
  1606. else {
  1607. struct device_node *prev;
  1608. for (prev = allnodes;
  1609. prev->allnext != np;
  1610. prev = prev->allnext)
  1611. ;
  1612. prev->allnext = np->allnext;
  1613. }
  1614. if (parent->child == np)
  1615. parent->child = np->sibling;
  1616. else {
  1617. struct device_node *prevsib;
  1618. for (prevsib = np->parent->child;
  1619. prevsib->sibling != np;
  1620. prevsib = prevsib->sibling)
  1621. ;
  1622. prevsib->sibling = np->sibling;
  1623. }
  1624. write_unlock(&devtree_lock);
  1625. }
  1626. static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1627. {
  1628. int err;
  1629. switch (action) {
  1630. case PSERIES_RECONFIG_ADD:
  1631. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1632. if (err < 0) {
  1633. printk(KERN_ERR "finish_node returned %d\n", err);
  1634. err = NOTIFY_BAD;
  1635. }
  1636. break;
  1637. default:
  1638. err = NOTIFY_DONE;
  1639. break;
  1640. }
  1641. return err;
  1642. }
  1643. static struct notifier_block prom_reconfig_nb = {
  1644. .notifier_call = prom_reconfig_notifier,
  1645. .priority = 10, /* This one needs to run first */
  1646. };
  1647. static int __init prom_reconfig_setup(void)
  1648. {
  1649. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1650. }
  1651. __initcall(prom_reconfig_setup);
  1652. /*
  1653. * Find a property with a given name for a given node
  1654. * and return the value.
  1655. */
  1656. unsigned char *
  1657. get_property(struct device_node *np, const char *name, int *lenp)
  1658. {
  1659. struct property *pp;
  1660. for (pp = np->properties; pp != 0; pp = pp->next)
  1661. if (strcmp(pp->name, name) == 0) {
  1662. if (lenp != 0)
  1663. *lenp = pp->length;
  1664. return pp->value;
  1665. }
  1666. return NULL;
  1667. }
  1668. EXPORT_SYMBOL(get_property);
  1669. /*
  1670. * Add a property to a node
  1671. */
  1672. void
  1673. prom_add_property(struct device_node* np, struct property* prop)
  1674. {
  1675. struct property **next = &np->properties;
  1676. prop->next = NULL;
  1677. while (*next)
  1678. next = &(*next)->next;
  1679. *next = prop;
  1680. }
  1681. #if 0
  1682. void
  1683. print_properties(struct device_node *np)
  1684. {
  1685. struct property *pp;
  1686. char *cp;
  1687. int i, n;
  1688. for (pp = np->properties; pp != 0; pp = pp->next) {
  1689. printk(KERN_INFO "%s", pp->name);
  1690. for (i = strlen(pp->name); i < 16; ++i)
  1691. printk(" ");
  1692. cp = (char *) pp->value;
  1693. for (i = pp->length; i > 0; --i, ++cp)
  1694. if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
  1695. || (i == 1 && *cp != 0))
  1696. break;
  1697. if (i == 0 && pp->length > 1) {
  1698. /* looks like a string */
  1699. printk(" %s\n", (char *) pp->value);
  1700. } else {
  1701. /* dump it in hex */
  1702. n = pp->length;
  1703. if (n > 64)
  1704. n = 64;
  1705. if (pp->length % 4 == 0) {
  1706. unsigned int *p = (unsigned int *) pp->value;
  1707. n /= 4;
  1708. for (i = 0; i < n; ++i) {
  1709. if (i != 0 && (i % 4) == 0)
  1710. printk("\n ");
  1711. printk(" %08x", *p++);
  1712. }
  1713. } else {
  1714. unsigned char *bp = pp->value;
  1715. for (i = 0; i < n; ++i) {
  1716. if (i != 0 && (i % 16) == 0)
  1717. printk("\n ");
  1718. printk(" %02x", *bp++);
  1719. }
  1720. }
  1721. printk("\n");
  1722. if (pp->length > 64)
  1723. printk(" ... (length = %d)\n",
  1724. pp->length);
  1725. }
  1726. }
  1727. }
  1728. #endif