prom.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  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. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #undef DEBUG
  16. #include <stdarg.h>
  17. #include <linux/config.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/init.h>
  21. #include <linux/threads.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/stringify.h>
  26. #include <linux/delay.h>
  27. #include <linux/initrd.h>
  28. #include <linux/bitops.h>
  29. #include <linux/module.h>
  30. #include <linux/kexec.h>
  31. #include <linux/debugfs.h>
  32. #include <asm/prom.h>
  33. #include <asm/rtas.h>
  34. #include <asm/lmb.h>
  35. #include <asm/page.h>
  36. #include <asm/processor.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/kdump.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/btext.h>
  47. #include <asm/sections.h>
  48. #include <asm/machdep.h>
  49. #include <asm/pSeries_reconfig.h>
  50. #include <asm/pci-bridge.h>
  51. #include <asm/kexec.h>
  52. #ifdef DEBUG
  53. #define DBG(fmt...) printk(KERN_ERR fmt)
  54. #else
  55. #define DBG(fmt...)
  56. #endif
  57. static int __initdata dt_root_addr_cells;
  58. static int __initdata dt_root_size_cells;
  59. #ifdef CONFIG_PPC64
  60. int __initdata iommu_is_off;
  61. int __initdata iommu_force_on;
  62. unsigned long tce_alloc_start, tce_alloc_end;
  63. #endif
  64. typedef u32 cell_t;
  65. #if 0
  66. static struct boot_param_header *initial_boot_params __initdata;
  67. #else
  68. struct boot_param_header *initial_boot_params;
  69. #endif
  70. static struct device_node *allnodes = NULL;
  71. /* use when traversing tree through the allnext, child, sibling,
  72. * or parent members of struct device_node.
  73. */
  74. static DEFINE_RWLOCK(devtree_lock);
  75. /* export that to outside world */
  76. struct device_node *of_chosen;
  77. struct device_node *dflt_interrupt_controller;
  78. int num_interrupt_controllers;
  79. /*
  80. * Wrapper for allocating memory for various data that needs to be
  81. * attached to device nodes as they are processed at boot or when
  82. * added to the device tree later (e.g. DLPAR). At boot there is
  83. * already a region reserved so we just increment *mem_start by size;
  84. * otherwise we call kmalloc.
  85. */
  86. static void * prom_alloc(unsigned long size, unsigned long *mem_start)
  87. {
  88. unsigned long tmp;
  89. if (!mem_start)
  90. return kmalloc(size, GFP_KERNEL);
  91. tmp = *mem_start;
  92. *mem_start += size;
  93. return (void *)tmp;
  94. }
  95. /*
  96. * Find the device_node with a given phandle.
  97. */
  98. static struct device_node * find_phandle(phandle ph)
  99. {
  100. struct device_node *np;
  101. for (np = allnodes; np != 0; np = np->allnext)
  102. if (np->linux_phandle == ph)
  103. return np;
  104. return NULL;
  105. }
  106. /*
  107. * Find the interrupt parent of a node.
  108. */
  109. static struct device_node * __devinit intr_parent(struct device_node *p)
  110. {
  111. phandle *parp;
  112. parp = (phandle *) get_property(p, "interrupt-parent", NULL);
  113. if (parp == NULL)
  114. return p->parent;
  115. p = find_phandle(*parp);
  116. if (p != NULL)
  117. return p;
  118. /*
  119. * On a powermac booted with BootX, we don't get to know the
  120. * phandles for any nodes, so find_phandle will return NULL.
  121. * Fortunately these machines only have one interrupt controller
  122. * so there isn't in fact any ambiguity. -- paulus
  123. */
  124. if (num_interrupt_controllers == 1)
  125. p = dflt_interrupt_controller;
  126. return p;
  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 && num_interrupt_controllers == 1)
  203. /* cope with BootX not giving us phandles */
  204. ipar = dflt_interrupt_controller;
  205. if (ipar == NULL) {
  206. printk("oops, no int parent %x in map of %s\n",
  207. imap[-1], p->full_name);
  208. return 0;
  209. }
  210. /* find the parent's # addr and intr cells */
  211. ip = (unsigned int *)
  212. get_property(ipar, "#interrupt-cells", NULL);
  213. if (ip == NULL) {
  214. printk("oops, no #interrupt-cells on %s\n",
  215. ipar->full_name);
  216. return 0;
  217. }
  218. newintrc = *ip;
  219. ip = (unsigned int *)
  220. get_property(ipar, "#address-cells", NULL);
  221. newaddrc = (ip == NULL)? 0: *ip;
  222. imap += newaddrc + newintrc;
  223. imaplen -= newaddrc + newintrc;
  224. }
  225. if (imaplen < 0) {
  226. printk("oops, error decoding int-map on %s, len=%d\n",
  227. p->full_name, imaplen);
  228. return 0;
  229. }
  230. if (!match) {
  231. #ifdef DEBUG_IRQ
  232. printk("oops, no match in %s int-map for %s\n",
  233. p->full_name, np->full_name);
  234. #endif
  235. return 0;
  236. }
  237. p = ipar;
  238. naddrc = newaddrc;
  239. nintrc = newintrc;
  240. ints = imap - nintrc;
  241. reg = ints - naddrc;
  242. }
  243. if (p == NULL) {
  244. #ifdef DEBUG_IRQ
  245. printk("hmmm, int tree for %s doesn't have ctrler\n",
  246. np->full_name);
  247. #endif
  248. return 0;
  249. }
  250. *irq = ints;
  251. *ictrler = p;
  252. return nintrc;
  253. }
  254. static unsigned char map_isa_senses[4] = {
  255. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
  256. IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
  257. IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
  258. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE
  259. };
  260. static unsigned char map_mpic_senses[4] = {
  261. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE,
  262. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE,
  263. /* 2 seems to be used for the 8259 cascade... */
  264. IRQ_SENSE_LEVEL | IRQ_POLARITY_POSITIVE,
  265. IRQ_SENSE_EDGE | IRQ_POLARITY_NEGATIVE,
  266. };
  267. static int __devinit finish_node_interrupts(struct device_node *np,
  268. unsigned long *mem_start,
  269. int measure_only)
  270. {
  271. unsigned int *ints;
  272. int intlen, intrcells, intrcount;
  273. int i, j, n, sense;
  274. unsigned int *irq, virq;
  275. struct device_node *ic;
  276. int trace = 0;
  277. //#define TRACE(fmt...) do { if (trace) { printk(fmt); mdelay(1000); } } while(0)
  278. #define TRACE(fmt...)
  279. if (!strcmp(np->name, "smu-doorbell"))
  280. trace = 1;
  281. TRACE("Finishing SMU doorbell ! num_interrupt_controllers = %d\n",
  282. num_interrupt_controllers);
  283. if (num_interrupt_controllers == 0) {
  284. /*
  285. * Old machines just have a list of interrupt numbers
  286. * and no interrupt-controller nodes.
  287. */
  288. ints = (unsigned int *) get_property(np, "AAPL,interrupts",
  289. &intlen);
  290. /* XXX old interpret_pci_props looked in parent too */
  291. /* XXX old interpret_macio_props looked for interrupts
  292. before AAPL,interrupts */
  293. if (ints == NULL)
  294. ints = (unsigned int *) get_property(np, "interrupts",
  295. &intlen);
  296. if (ints == NULL)
  297. return 0;
  298. np->n_intrs = intlen / sizeof(unsigned int);
  299. np->intrs = prom_alloc(np->n_intrs * sizeof(np->intrs[0]),
  300. mem_start);
  301. if (!np->intrs)
  302. return -ENOMEM;
  303. if (measure_only)
  304. return 0;
  305. for (i = 0; i < np->n_intrs; ++i) {
  306. np->intrs[i].line = *ints++;
  307. np->intrs[i].sense = IRQ_SENSE_LEVEL
  308. | IRQ_POLARITY_NEGATIVE;
  309. }
  310. return 0;
  311. }
  312. ints = (unsigned int *) get_property(np, "interrupts", &intlen);
  313. TRACE("ints=%p, intlen=%d\n", ints, intlen);
  314. if (ints == NULL)
  315. return 0;
  316. intrcells = prom_n_intr_cells(np);
  317. intlen /= intrcells * sizeof(unsigned int);
  318. TRACE("intrcells=%d, new intlen=%d\n", intrcells, intlen);
  319. np->intrs = prom_alloc(intlen * sizeof(*(np->intrs)), mem_start);
  320. if (!np->intrs)
  321. return -ENOMEM;
  322. if (measure_only)
  323. return 0;
  324. intrcount = 0;
  325. for (i = 0; i < intlen; ++i, ints += intrcells) {
  326. n = map_interrupt(&irq, &ic, np, ints, intrcells);
  327. TRACE("map, irq=%d, ic=%p, n=%d\n", irq, ic, n);
  328. if (n <= 0)
  329. continue;
  330. /* don't map IRQ numbers under a cascaded 8259 controller */
  331. if (ic && device_is_compatible(ic, "chrp,iic")) {
  332. np->intrs[intrcount].line = irq[0];
  333. sense = (n > 1)? (irq[1] & 3): 3;
  334. np->intrs[intrcount].sense = map_isa_senses[sense];
  335. } else {
  336. virq = virt_irq_create_mapping(irq[0]);
  337. TRACE("virq=%d\n", virq);
  338. #ifdef CONFIG_PPC64
  339. if (virq == NO_IRQ) {
  340. printk(KERN_CRIT "Could not allocate interrupt"
  341. " number for %s\n", np->full_name);
  342. continue;
  343. }
  344. #endif
  345. np->intrs[intrcount].line = irq_offset_up(virq);
  346. sense = (n > 1)? (irq[1] & 3): 1;
  347. /* Apple uses bits in there in a different way, let's
  348. * only keep the real sense bit on macs
  349. */
  350. if (machine_is(powermac))
  351. sense &= 0x1;
  352. np->intrs[intrcount].sense = map_mpic_senses[sense];
  353. }
  354. #ifdef CONFIG_PPC64
  355. /* We offset irq numbers for the u3 MPIC by 128 in PowerMac */
  356. if (machine_is(powermac) && ic && ic->parent) {
  357. char *name = get_property(ic->parent, "name", NULL);
  358. if (name && !strcmp(name, "u3"))
  359. np->intrs[intrcount].line += 128;
  360. else if (!(name && (!strcmp(name, "mac-io") ||
  361. !strcmp(name, "u4"))))
  362. /* ignore other cascaded controllers, such as
  363. the k2-sata-root */
  364. break;
  365. }
  366. #endif /* CONFIG_PPC64 */
  367. if (n > 2) {
  368. printk("hmmm, got %d intr cells for %s:", n,
  369. np->full_name);
  370. for (j = 0; j < n; ++j)
  371. printk(" %d", irq[j]);
  372. printk("\n");
  373. }
  374. ++intrcount;
  375. }
  376. np->n_intrs = intrcount;
  377. return 0;
  378. }
  379. static int __devinit finish_node(struct device_node *np,
  380. unsigned long *mem_start,
  381. int measure_only)
  382. {
  383. struct device_node *child;
  384. int rc = 0;
  385. rc = finish_node_interrupts(np, mem_start, measure_only);
  386. if (rc)
  387. goto out;
  388. for (child = np->child; child != NULL; child = child->sibling) {
  389. rc = finish_node(child, mem_start, measure_only);
  390. if (rc)
  391. goto out;
  392. }
  393. out:
  394. return rc;
  395. }
  396. static void __init scan_interrupt_controllers(void)
  397. {
  398. struct device_node *np;
  399. int n = 0;
  400. char *name, *ic;
  401. int iclen;
  402. for (np = allnodes; np != NULL; np = np->allnext) {
  403. ic = get_property(np, "interrupt-controller", &iclen);
  404. name = get_property(np, "name", NULL);
  405. /* checking iclen makes sure we don't get a false
  406. match on /chosen.interrupt_controller */
  407. if ((name != NULL
  408. && strcmp(name, "interrupt-controller") == 0)
  409. || (ic != NULL && iclen == 0
  410. && strcmp(name, "AppleKiwi"))) {
  411. if (n == 0)
  412. dflt_interrupt_controller = np;
  413. ++n;
  414. }
  415. }
  416. num_interrupt_controllers = n;
  417. }
  418. /**
  419. * finish_device_tree is called once things are running normally
  420. * (i.e. with text and data mapped to the address they were linked at).
  421. * It traverses the device tree and fills in some of the additional,
  422. * fields in each node like {n_}addrs and {n_}intrs, the virt interrupt
  423. * mapping is also initialized at this point.
  424. */
  425. void __init finish_device_tree(void)
  426. {
  427. unsigned long start, end, size = 0;
  428. DBG(" -> finish_device_tree\n");
  429. #ifdef CONFIG_PPC64
  430. /* Initialize virtual IRQ map */
  431. virt_irq_init();
  432. #endif
  433. scan_interrupt_controllers();
  434. /*
  435. * Finish device-tree (pre-parsing some properties etc...)
  436. * We do this in 2 passes. One with "measure_only" set, which
  437. * will only measure the amount of memory needed, then we can
  438. * allocate that memory, and call finish_node again. However,
  439. * we must be careful as most routines will fail nowadays when
  440. * prom_alloc() returns 0, so we must make sure our first pass
  441. * doesn't start at 0. We pre-initialize size to 16 for that
  442. * reason and then remove those additional 16 bytes
  443. */
  444. size = 16;
  445. finish_node(allnodes, &size, 1);
  446. size -= 16;
  447. if (0 == size)
  448. end = start = 0;
  449. else
  450. end = start = (unsigned long)__va(lmb_alloc(size, 128));
  451. finish_node(allnodes, &end, 0);
  452. BUG_ON(end != start + size);
  453. DBG(" <- finish_device_tree\n");
  454. }
  455. static inline char *find_flat_dt_string(u32 offset)
  456. {
  457. return ((char *)initial_boot_params) +
  458. initial_boot_params->off_dt_strings + offset;
  459. }
  460. /**
  461. * This function is used to scan the flattened device-tree, it is
  462. * used to extract the memory informations at boot before we can
  463. * unflatten the tree
  464. */
  465. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  466. const char *uname, int depth,
  467. void *data),
  468. void *data)
  469. {
  470. unsigned long p = ((unsigned long)initial_boot_params) +
  471. initial_boot_params->off_dt_struct;
  472. int rc = 0;
  473. int depth = -1;
  474. do {
  475. u32 tag = *((u32 *)p);
  476. char *pathp;
  477. p += 4;
  478. if (tag == OF_DT_END_NODE) {
  479. depth --;
  480. continue;
  481. }
  482. if (tag == OF_DT_NOP)
  483. continue;
  484. if (tag == OF_DT_END)
  485. break;
  486. if (tag == OF_DT_PROP) {
  487. u32 sz = *((u32 *)p);
  488. p += 8;
  489. if (initial_boot_params->version < 0x10)
  490. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  491. p += sz;
  492. p = _ALIGN(p, 4);
  493. continue;
  494. }
  495. if (tag != OF_DT_BEGIN_NODE) {
  496. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  497. " device tree !\n", tag);
  498. return -EINVAL;
  499. }
  500. depth++;
  501. pathp = (char *)p;
  502. p = _ALIGN(p + strlen(pathp) + 1, 4);
  503. if ((*pathp) == '/') {
  504. char *lp, *np;
  505. for (lp = NULL, np = pathp; *np; np++)
  506. if ((*np) == '/')
  507. lp = np+1;
  508. if (lp != NULL)
  509. pathp = lp;
  510. }
  511. rc = it(p, pathp, depth, data);
  512. if (rc != 0)
  513. break;
  514. } while(1);
  515. return rc;
  516. }
  517. unsigned long __init of_get_flat_dt_root(void)
  518. {
  519. unsigned long p = ((unsigned long)initial_boot_params) +
  520. initial_boot_params->off_dt_struct;
  521. while(*((u32 *)p) == OF_DT_NOP)
  522. p += 4;
  523. BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
  524. p += 4;
  525. return _ALIGN(p + strlen((char *)p) + 1, 4);
  526. }
  527. /**
  528. * This function can be used within scan_flattened_dt callback to get
  529. * access to properties
  530. */
  531. void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
  532. unsigned long *size)
  533. {
  534. unsigned long p = node;
  535. do {
  536. u32 tag = *((u32 *)p);
  537. u32 sz, noff;
  538. const char *nstr;
  539. p += 4;
  540. if (tag == OF_DT_NOP)
  541. continue;
  542. if (tag != OF_DT_PROP)
  543. return NULL;
  544. sz = *((u32 *)p);
  545. noff = *((u32 *)(p + 4));
  546. p += 8;
  547. if (initial_boot_params->version < 0x10)
  548. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  549. nstr = find_flat_dt_string(noff);
  550. if (nstr == NULL) {
  551. printk(KERN_WARNING "Can't find property index"
  552. " name !\n");
  553. return NULL;
  554. }
  555. if (strcmp(name, nstr) == 0) {
  556. if (size)
  557. *size = sz;
  558. return (void *)p;
  559. }
  560. p += sz;
  561. p = _ALIGN(p, 4);
  562. } while(1);
  563. }
  564. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  565. {
  566. const char* cp;
  567. unsigned long cplen, l;
  568. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  569. if (cp == NULL)
  570. return 0;
  571. while (cplen > 0) {
  572. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  573. return 1;
  574. l = strlen(cp) + 1;
  575. cp += l;
  576. cplen -= l;
  577. }
  578. return 0;
  579. }
  580. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  581. unsigned long align)
  582. {
  583. void *res;
  584. *mem = _ALIGN(*mem, align);
  585. res = (void *)*mem;
  586. *mem += size;
  587. return res;
  588. }
  589. static unsigned long __init unflatten_dt_node(unsigned long mem,
  590. unsigned long *p,
  591. struct device_node *dad,
  592. struct device_node ***allnextpp,
  593. unsigned long fpsize)
  594. {
  595. struct device_node *np;
  596. struct property *pp, **prev_pp = NULL;
  597. char *pathp;
  598. u32 tag;
  599. unsigned int l, allocl;
  600. int has_name = 0;
  601. int new_format = 0;
  602. tag = *((u32 *)(*p));
  603. if (tag != OF_DT_BEGIN_NODE) {
  604. printk("Weird tag at start of node: %x\n", tag);
  605. return mem;
  606. }
  607. *p += 4;
  608. pathp = (char *)*p;
  609. l = allocl = strlen(pathp) + 1;
  610. *p = _ALIGN(*p + l, 4);
  611. /* version 0x10 has a more compact unit name here instead of the full
  612. * path. we accumulate the full path size using "fpsize", we'll rebuild
  613. * it later. We detect this because the first character of the name is
  614. * not '/'.
  615. */
  616. if ((*pathp) != '/') {
  617. new_format = 1;
  618. if (fpsize == 0) {
  619. /* root node: special case. fpsize accounts for path
  620. * plus terminating zero. root node only has '/', so
  621. * fpsize should be 2, but we want to avoid the first
  622. * level nodes to have two '/' so we use fpsize 1 here
  623. */
  624. fpsize = 1;
  625. allocl = 2;
  626. } else {
  627. /* account for '/' and path size minus terminal 0
  628. * already in 'l'
  629. */
  630. fpsize += l;
  631. allocl = fpsize;
  632. }
  633. }
  634. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  635. __alignof__(struct device_node));
  636. if (allnextpp) {
  637. memset(np, 0, sizeof(*np));
  638. np->full_name = ((char*)np) + sizeof(struct device_node);
  639. if (new_format) {
  640. char *p = np->full_name;
  641. /* rebuild full path for new format */
  642. if (dad && dad->parent) {
  643. strcpy(p, dad->full_name);
  644. #ifdef DEBUG
  645. if ((strlen(p) + l + 1) != allocl) {
  646. DBG("%s: p: %d, l: %d, a: %d\n",
  647. pathp, (int)strlen(p), l, allocl);
  648. }
  649. #endif
  650. p += strlen(p);
  651. }
  652. *(p++) = '/';
  653. memcpy(p, pathp, l);
  654. } else
  655. memcpy(np->full_name, pathp, l);
  656. prev_pp = &np->properties;
  657. **allnextpp = np;
  658. *allnextpp = &np->allnext;
  659. if (dad != NULL) {
  660. np->parent = dad;
  661. /* we temporarily use the next field as `last_child'*/
  662. if (dad->next == 0)
  663. dad->child = np;
  664. else
  665. dad->next->sibling = np;
  666. dad->next = np;
  667. }
  668. kref_init(&np->kref);
  669. }
  670. while(1) {
  671. u32 sz, noff;
  672. char *pname;
  673. tag = *((u32 *)(*p));
  674. if (tag == OF_DT_NOP) {
  675. *p += 4;
  676. continue;
  677. }
  678. if (tag != OF_DT_PROP)
  679. break;
  680. *p += 4;
  681. sz = *((u32 *)(*p));
  682. noff = *((u32 *)((*p) + 4));
  683. *p += 8;
  684. if (initial_boot_params->version < 0x10)
  685. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  686. pname = find_flat_dt_string(noff);
  687. if (pname == NULL) {
  688. printk("Can't find property name in list !\n");
  689. break;
  690. }
  691. if (strcmp(pname, "name") == 0)
  692. has_name = 1;
  693. l = strlen(pname) + 1;
  694. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  695. __alignof__(struct property));
  696. if (allnextpp) {
  697. if (strcmp(pname, "linux,phandle") == 0) {
  698. np->node = *((u32 *)*p);
  699. if (np->linux_phandle == 0)
  700. np->linux_phandle = np->node;
  701. }
  702. if (strcmp(pname, "ibm,phandle") == 0)
  703. np->linux_phandle = *((u32 *)*p);
  704. pp->name = pname;
  705. pp->length = sz;
  706. pp->value = (void *)*p;
  707. *prev_pp = pp;
  708. prev_pp = &pp->next;
  709. }
  710. *p = _ALIGN((*p) + sz, 4);
  711. }
  712. /* with version 0x10 we may not have the name property, recreate
  713. * it here from the unit name if absent
  714. */
  715. if (!has_name) {
  716. char *p = pathp, *ps = pathp, *pa = NULL;
  717. int sz;
  718. while (*p) {
  719. if ((*p) == '@')
  720. pa = p;
  721. if ((*p) == '/')
  722. ps = p + 1;
  723. p++;
  724. }
  725. if (pa < ps)
  726. pa = p;
  727. sz = (pa - ps) + 1;
  728. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  729. __alignof__(struct property));
  730. if (allnextpp) {
  731. pp->name = "name";
  732. pp->length = sz;
  733. pp->value = (unsigned char *)(pp + 1);
  734. *prev_pp = pp;
  735. prev_pp = &pp->next;
  736. memcpy(pp->value, ps, sz - 1);
  737. ((char *)pp->value)[sz - 1] = 0;
  738. DBG("fixed up name for %s -> %s\n", pathp, pp->value);
  739. }
  740. }
  741. if (allnextpp) {
  742. *prev_pp = NULL;
  743. np->name = get_property(np, "name", NULL);
  744. np->type = get_property(np, "device_type", NULL);
  745. if (!np->name)
  746. np->name = "<NULL>";
  747. if (!np->type)
  748. np->type = "<NULL>";
  749. }
  750. while (tag == OF_DT_BEGIN_NODE) {
  751. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  752. tag = *((u32 *)(*p));
  753. }
  754. if (tag != OF_DT_END_NODE) {
  755. printk("Weird tag at end of node: %x\n", tag);
  756. return mem;
  757. }
  758. *p += 4;
  759. return mem;
  760. }
  761. static int __init early_parse_mem(char *p)
  762. {
  763. if (!p)
  764. return 1;
  765. memory_limit = PAGE_ALIGN(memparse(p, &p));
  766. DBG("memory limit = 0x%lx\n", memory_limit);
  767. return 0;
  768. }
  769. early_param("mem", early_parse_mem);
  770. /*
  771. * The device tree may be allocated below our memory limit, or inside the
  772. * crash kernel region for kdump. If so, move it out now.
  773. */
  774. static void move_device_tree(void)
  775. {
  776. unsigned long start, size;
  777. void *p;
  778. DBG("-> move_device_tree\n");
  779. start = __pa(initial_boot_params);
  780. size = initial_boot_params->totalsize;
  781. if ((memory_limit && (start + size) > memory_limit) ||
  782. overlaps_crashkernel(start, size)) {
  783. p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
  784. memcpy(p, initial_boot_params, size);
  785. initial_boot_params = (struct boot_param_header *)p;
  786. DBG("Moved device tree to 0x%p\n", p);
  787. }
  788. DBG("<- move_device_tree\n");
  789. }
  790. /**
  791. * unflattens the device-tree passed by the firmware, creating the
  792. * tree of struct device_node. It also fills the "name" and "type"
  793. * pointers of the nodes so the normal device-tree walking functions
  794. * can be used (this used to be done by finish_device_tree)
  795. */
  796. void __init unflatten_device_tree(void)
  797. {
  798. unsigned long start, mem, size;
  799. struct device_node **allnextp = &allnodes;
  800. DBG(" -> unflatten_device_tree()\n");
  801. /* First pass, scan for size */
  802. start = ((unsigned long)initial_boot_params) +
  803. initial_boot_params->off_dt_struct;
  804. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  805. size = (size | 3) + 1;
  806. DBG(" size is %lx, allocating...\n", size);
  807. /* Allocate memory for the expanded device tree */
  808. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  809. mem = (unsigned long) __va(mem);
  810. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  811. DBG(" unflattening %lx...\n", mem);
  812. /* Second pass, do actual unflattening */
  813. start = ((unsigned long)initial_boot_params) +
  814. initial_boot_params->off_dt_struct;
  815. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  816. if (*((u32 *)start) != OF_DT_END)
  817. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  818. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  819. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  820. ((u32 *)mem)[size / 4] );
  821. *allnextp = NULL;
  822. /* Get pointer to OF "/chosen" node for use everywhere */
  823. of_chosen = of_find_node_by_path("/chosen");
  824. if (of_chosen == NULL)
  825. of_chosen = of_find_node_by_path("/chosen@0");
  826. DBG(" <- unflatten_device_tree()\n");
  827. }
  828. /*
  829. * ibm,pa-features is a per-cpu property that contains a string of
  830. * attribute descriptors, each of which has a 2 byte header plus up
  831. * to 254 bytes worth of processor attribute bits. First header
  832. * byte specifies the number of bytes following the header.
  833. * Second header byte is an "attribute-specifier" type, of which
  834. * zero is the only currently-defined value.
  835. * Implementation: Pass in the byte and bit offset for the feature
  836. * that we are interested in. The function will return -1 if the
  837. * pa-features property is missing, or a 1/0 to indicate if the feature
  838. * is supported/not supported. Note that the bit numbers are
  839. * big-endian to match the definition in PAPR.
  840. */
  841. static struct ibm_pa_feature {
  842. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  843. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  844. unsigned char pabyte; /* byte number in ibm,pa-features */
  845. unsigned char pabit; /* bit number (big-endian) */
  846. unsigned char invert; /* if 1, pa bit set => clear feature */
  847. } ibm_pa_features[] __initdata = {
  848. {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  849. {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  850. {CPU_FTR_SLB, 0, 0, 2, 0},
  851. {CPU_FTR_CTRL, 0, 0, 3, 0},
  852. {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
  853. {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
  854. #if 0
  855. /* put this back once we know how to test if firmware does 64k IO */
  856. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  857. #endif
  858. };
  859. static void __init check_cpu_pa_features(unsigned long node)
  860. {
  861. unsigned char *pa_ftrs;
  862. unsigned long len, tablelen, i, bit;
  863. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  864. if (pa_ftrs == NULL)
  865. return;
  866. /* find descriptor with type == 0 */
  867. for (;;) {
  868. if (tablelen < 3)
  869. return;
  870. len = 2 + pa_ftrs[0];
  871. if (tablelen < len)
  872. return; /* descriptor 0 not found */
  873. if (pa_ftrs[1] == 0)
  874. break;
  875. tablelen -= len;
  876. pa_ftrs += len;
  877. }
  878. /* loop over bits we know about */
  879. for (i = 0; i < ARRAY_SIZE(ibm_pa_features); ++i) {
  880. struct ibm_pa_feature *fp = &ibm_pa_features[i];
  881. if (fp->pabyte >= pa_ftrs[0])
  882. continue;
  883. bit = (pa_ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  884. if (bit ^ fp->invert) {
  885. cur_cpu_spec->cpu_features |= fp->cpu_features;
  886. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  887. } else {
  888. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  889. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  890. }
  891. }
  892. }
  893. static int __init early_init_dt_scan_cpus(unsigned long node,
  894. const char *uname, int depth,
  895. void *data)
  896. {
  897. static int logical_cpuid = 0;
  898. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  899. #ifdef CONFIG_ALTIVEC
  900. u32 *prop;
  901. #endif
  902. u32 *intserv;
  903. int i, nthreads;
  904. unsigned long len;
  905. int found = 0;
  906. /* We are scanning "cpu" nodes only */
  907. if (type == NULL || strcmp(type, "cpu") != 0)
  908. return 0;
  909. /* Get physical cpuid */
  910. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  911. if (intserv) {
  912. nthreads = len / sizeof(int);
  913. } else {
  914. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  915. nthreads = 1;
  916. }
  917. /*
  918. * Now see if any of these threads match our boot cpu.
  919. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  920. */
  921. for (i = 0; i < nthreads; i++) {
  922. /*
  923. * version 2 of the kexec param format adds the phys cpuid of
  924. * booted proc.
  925. */
  926. if (initial_boot_params && initial_boot_params->version >= 2) {
  927. if (intserv[i] ==
  928. initial_boot_params->boot_cpuid_phys) {
  929. found = 1;
  930. break;
  931. }
  932. } else {
  933. /*
  934. * Check if it's the boot-cpu, set it's hw index now,
  935. * unfortunately this format did not support booting
  936. * off secondary threads.
  937. */
  938. if (of_get_flat_dt_prop(node,
  939. "linux,boot-cpu", NULL) != NULL) {
  940. found = 1;
  941. break;
  942. }
  943. }
  944. #ifdef CONFIG_SMP
  945. /* logical cpu id is always 0 on UP kernels */
  946. logical_cpuid++;
  947. #endif
  948. }
  949. if (found) {
  950. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  951. intserv[i]);
  952. boot_cpuid = logical_cpuid;
  953. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  954. }
  955. #ifdef CONFIG_ALTIVEC
  956. /* Check if we have a VMX and eventually update CPU features */
  957. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
  958. if (prop && (*prop) > 0) {
  959. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  960. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  961. }
  962. /* Same goes for Apple's "altivec" property */
  963. prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
  964. if (prop) {
  965. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  966. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  967. }
  968. #endif /* CONFIG_ALTIVEC */
  969. check_cpu_pa_features(node);
  970. #ifdef CONFIG_PPC_PSERIES
  971. if (nthreads > 1)
  972. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  973. else
  974. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  975. #endif
  976. return 0;
  977. }
  978. static int __init early_init_dt_scan_chosen(unsigned long node,
  979. const char *uname, int depth, void *data)
  980. {
  981. unsigned long *lprop;
  982. unsigned long l;
  983. char *p;
  984. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  985. if (depth != 1 ||
  986. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  987. return 0;
  988. #ifdef CONFIG_PPC64
  989. /* check if iommu is forced on or off */
  990. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  991. iommu_is_off = 1;
  992. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  993. iommu_force_on = 1;
  994. #endif
  995. /* mem=x on the command line is the preferred mechanism */
  996. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  997. if (lprop)
  998. memory_limit = *lprop;
  999. #ifdef CONFIG_PPC64
  1000. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  1001. if (lprop)
  1002. tce_alloc_start = *lprop;
  1003. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  1004. if (lprop)
  1005. tce_alloc_end = *lprop;
  1006. #endif
  1007. #ifdef CONFIG_KEXEC
  1008. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  1009. if (lprop)
  1010. crashk_res.start = *lprop;
  1011. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  1012. if (lprop)
  1013. crashk_res.end = crashk_res.start + *lprop - 1;
  1014. #endif
  1015. /* Retreive command line */
  1016. p = of_get_flat_dt_prop(node, "bootargs", &l);
  1017. if (p != NULL && l > 0)
  1018. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  1019. #ifdef CONFIG_CMDLINE
  1020. if (l == 0 || (l == 1 && (*p) == 0))
  1021. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  1022. #endif /* CONFIG_CMDLINE */
  1023. DBG("Command line is: %s\n", cmd_line);
  1024. /* break now */
  1025. return 1;
  1026. }
  1027. static int __init early_init_dt_scan_root(unsigned long node,
  1028. const char *uname, int depth, void *data)
  1029. {
  1030. u32 *prop;
  1031. if (depth != 0)
  1032. return 0;
  1033. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  1034. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  1035. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  1036. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  1037. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  1038. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  1039. /* break now */
  1040. return 1;
  1041. }
  1042. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  1043. {
  1044. cell_t *p = *cellp;
  1045. unsigned long r;
  1046. /* Ignore more than 2 cells */
  1047. while (s > sizeof(unsigned long) / 4) {
  1048. p++;
  1049. s--;
  1050. }
  1051. r = *p++;
  1052. #ifdef CONFIG_PPC64
  1053. if (s > 1) {
  1054. r <<= 32;
  1055. r |= *(p++);
  1056. s--;
  1057. }
  1058. #endif
  1059. *cellp = p;
  1060. return r;
  1061. }
  1062. static int __init early_init_dt_scan_memory(unsigned long node,
  1063. const char *uname, int depth, void *data)
  1064. {
  1065. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  1066. cell_t *reg, *endp;
  1067. unsigned long l;
  1068. /* We are scanning "memory" nodes only */
  1069. if (type == NULL) {
  1070. /*
  1071. * The longtrail doesn't have a device_type on the
  1072. * /memory node, so look for the node called /memory@0.
  1073. */
  1074. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  1075. return 0;
  1076. } else if (strcmp(type, "memory") != 0)
  1077. return 0;
  1078. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  1079. if (reg == NULL)
  1080. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  1081. if (reg == NULL)
  1082. return 0;
  1083. endp = reg + (l / sizeof(cell_t));
  1084. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  1085. uname, l, reg[0], reg[1], reg[2], reg[3]);
  1086. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  1087. unsigned long base, size;
  1088. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  1089. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  1090. if (size == 0)
  1091. continue;
  1092. DBG(" - %lx , %lx\n", base, size);
  1093. #ifdef CONFIG_PPC64
  1094. if (iommu_is_off) {
  1095. if (base >= 0x80000000ul)
  1096. continue;
  1097. if ((base + size) > 0x80000000ul)
  1098. size = 0x80000000ul - base;
  1099. }
  1100. #endif
  1101. lmb_add(base, size);
  1102. }
  1103. return 0;
  1104. }
  1105. static void __init early_reserve_mem(void)
  1106. {
  1107. u64 base, size;
  1108. u64 *reserve_map;
  1109. unsigned long self_base;
  1110. unsigned long self_size;
  1111. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  1112. initial_boot_params->off_mem_rsvmap);
  1113. /* before we do anything, lets reserve the dt blob */
  1114. self_base = __pa((unsigned long)initial_boot_params);
  1115. self_size = initial_boot_params->totalsize;
  1116. lmb_reserve(self_base, self_size);
  1117. #ifdef CONFIG_PPC32
  1118. /*
  1119. * Handle the case where we might be booting from an old kexec
  1120. * image that setup the mem_rsvmap as pairs of 32-bit values
  1121. */
  1122. if (*reserve_map > 0xffffffffull) {
  1123. u32 base_32, size_32;
  1124. u32 *reserve_map_32 = (u32 *)reserve_map;
  1125. while (1) {
  1126. base_32 = *(reserve_map_32++);
  1127. size_32 = *(reserve_map_32++);
  1128. if (size_32 == 0)
  1129. break;
  1130. /* skip if the reservation is for the blob */
  1131. if (base_32 == self_base && size_32 == self_size)
  1132. continue;
  1133. DBG("reserving: %x -> %x\n", base_32, size_32);
  1134. lmb_reserve(base_32, size_32);
  1135. }
  1136. return;
  1137. }
  1138. #endif
  1139. while (1) {
  1140. base = *(reserve_map++);
  1141. size = *(reserve_map++);
  1142. if (size == 0)
  1143. break;
  1144. /* skip if the reservation is for the blob */
  1145. if (base == self_base && size == self_size)
  1146. continue;
  1147. DBG("reserving: %llx -> %llx\n", base, size);
  1148. lmb_reserve(base, size);
  1149. }
  1150. #if 0
  1151. DBG("memory reserved, lmbs :\n");
  1152. lmb_dump_all();
  1153. #endif
  1154. }
  1155. void __init early_init_devtree(void *params)
  1156. {
  1157. DBG(" -> early_init_devtree()\n");
  1158. /* Setup flat device-tree pointer */
  1159. initial_boot_params = params;
  1160. #ifdef CONFIG_PPC_RTAS
  1161. /* Some machines might need RTAS info for debugging, grab it now. */
  1162. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  1163. #endif
  1164. /* Retrieve various informations from the /chosen node of the
  1165. * device-tree, including the platform type, initrd location and
  1166. * size, TCE reserve, and more ...
  1167. */
  1168. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1169. /* Scan memory nodes and rebuild LMBs */
  1170. lmb_init();
  1171. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1172. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1173. /* Save command line for /proc/cmdline and then parse parameters */
  1174. strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
  1175. parse_early_param();
  1176. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1177. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  1178. reserve_kdump_trampoline();
  1179. reserve_crashkernel();
  1180. early_reserve_mem();
  1181. lmb_enforce_memory_limit(memory_limit);
  1182. lmb_analyze();
  1183. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  1184. /* We may need to relocate the flat tree, do it now.
  1185. * FIXME .. and the initrd too? */
  1186. move_device_tree();
  1187. DBG("Scanning CPUs ...\n");
  1188. /* Retreive CPU related informations from the flat tree
  1189. * (altivec support, boot CPU ID, ...)
  1190. */
  1191. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1192. DBG(" <- early_init_devtree()\n");
  1193. }
  1194. #undef printk
  1195. int
  1196. prom_n_addr_cells(struct device_node* np)
  1197. {
  1198. int* ip;
  1199. do {
  1200. if (np->parent)
  1201. np = np->parent;
  1202. ip = (int *) get_property(np, "#address-cells", NULL);
  1203. if (ip != NULL)
  1204. return *ip;
  1205. } while (np->parent);
  1206. /* No #address-cells property for the root node, default to 1 */
  1207. return 1;
  1208. }
  1209. EXPORT_SYMBOL(prom_n_addr_cells);
  1210. int
  1211. prom_n_size_cells(struct device_node* np)
  1212. {
  1213. int* ip;
  1214. do {
  1215. if (np->parent)
  1216. np = np->parent;
  1217. ip = (int *) get_property(np, "#size-cells", NULL);
  1218. if (ip != NULL)
  1219. return *ip;
  1220. } while (np->parent);
  1221. /* No #size-cells property for the root node, default to 1 */
  1222. return 1;
  1223. }
  1224. EXPORT_SYMBOL(prom_n_size_cells);
  1225. /**
  1226. * Work out the sense (active-low level / active-high edge)
  1227. * of each interrupt from the device tree.
  1228. */
  1229. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1230. {
  1231. struct device_node *np;
  1232. int i, j;
  1233. /* default to level-triggered */
  1234. memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
  1235. for (np = allnodes; np != 0; np = np->allnext) {
  1236. for (j = 0; j < np->n_intrs; j++) {
  1237. i = np->intrs[j].line;
  1238. if (i >= off && i < max)
  1239. senses[i-off] = np->intrs[j].sense;
  1240. }
  1241. }
  1242. }
  1243. /**
  1244. * Construct and return a list of the device_nodes with a given name.
  1245. */
  1246. struct device_node *find_devices(const char *name)
  1247. {
  1248. struct device_node *head, **prevp, *np;
  1249. prevp = &head;
  1250. for (np = allnodes; np != 0; np = np->allnext) {
  1251. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1252. *prevp = np;
  1253. prevp = &np->next;
  1254. }
  1255. }
  1256. *prevp = NULL;
  1257. return head;
  1258. }
  1259. EXPORT_SYMBOL(find_devices);
  1260. /**
  1261. * Construct and return a list of the device_nodes with a given type.
  1262. */
  1263. struct device_node *find_type_devices(const char *type)
  1264. {
  1265. struct device_node *head, **prevp, *np;
  1266. prevp = &head;
  1267. for (np = allnodes; np != 0; np = np->allnext) {
  1268. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1269. *prevp = np;
  1270. prevp = &np->next;
  1271. }
  1272. }
  1273. *prevp = NULL;
  1274. return head;
  1275. }
  1276. EXPORT_SYMBOL(find_type_devices);
  1277. /**
  1278. * Returns all nodes linked together
  1279. */
  1280. struct device_node *find_all_nodes(void)
  1281. {
  1282. struct device_node *head, **prevp, *np;
  1283. prevp = &head;
  1284. for (np = allnodes; np != 0; np = np->allnext) {
  1285. *prevp = np;
  1286. prevp = &np->next;
  1287. }
  1288. *prevp = NULL;
  1289. return head;
  1290. }
  1291. EXPORT_SYMBOL(find_all_nodes);
  1292. /** Checks if the given "compat" string matches one of the strings in
  1293. * the device's "compatible" property
  1294. */
  1295. int device_is_compatible(struct device_node *device, const char *compat)
  1296. {
  1297. const char* cp;
  1298. int cplen, l;
  1299. cp = (char *) get_property(device, "compatible", &cplen);
  1300. if (cp == NULL)
  1301. return 0;
  1302. while (cplen > 0) {
  1303. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1304. return 1;
  1305. l = strlen(cp) + 1;
  1306. cp += l;
  1307. cplen -= l;
  1308. }
  1309. return 0;
  1310. }
  1311. EXPORT_SYMBOL(device_is_compatible);
  1312. /**
  1313. * Indicates whether the root node has a given value in its
  1314. * compatible property.
  1315. */
  1316. int machine_is_compatible(const char *compat)
  1317. {
  1318. struct device_node *root;
  1319. int rc = 0;
  1320. root = of_find_node_by_path("/");
  1321. if (root) {
  1322. rc = device_is_compatible(root, compat);
  1323. of_node_put(root);
  1324. }
  1325. return rc;
  1326. }
  1327. EXPORT_SYMBOL(machine_is_compatible);
  1328. /**
  1329. * Construct and return a list of the device_nodes with a given type
  1330. * and compatible property.
  1331. */
  1332. struct device_node *find_compatible_devices(const char *type,
  1333. const char *compat)
  1334. {
  1335. struct device_node *head, **prevp, *np;
  1336. prevp = &head;
  1337. for (np = allnodes; np != 0; np = np->allnext) {
  1338. if (type != NULL
  1339. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1340. continue;
  1341. if (device_is_compatible(np, compat)) {
  1342. *prevp = np;
  1343. prevp = &np->next;
  1344. }
  1345. }
  1346. *prevp = NULL;
  1347. return head;
  1348. }
  1349. EXPORT_SYMBOL(find_compatible_devices);
  1350. /**
  1351. * Find the device_node with a given full_name.
  1352. */
  1353. struct device_node *find_path_device(const char *path)
  1354. {
  1355. struct device_node *np;
  1356. for (np = allnodes; np != 0; np = np->allnext)
  1357. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1358. return np;
  1359. return NULL;
  1360. }
  1361. EXPORT_SYMBOL(find_path_device);
  1362. /*******
  1363. *
  1364. * New implementation of the OF "find" APIs, return a refcounted
  1365. * object, call of_node_put() when done. The device tree and list
  1366. * are protected by a rw_lock.
  1367. *
  1368. * Note that property management will need some locking as well,
  1369. * this isn't dealt with yet.
  1370. *
  1371. *******/
  1372. /**
  1373. * of_find_node_by_name - Find a node by its "name" property
  1374. * @from: The node to start searching from or NULL, the node
  1375. * you pass will not be searched, only the next one
  1376. * will; typically, you pass what the previous call
  1377. * returned. of_node_put() will be called on it
  1378. * @name: The name string to match against
  1379. *
  1380. * Returns a node pointer with refcount incremented, use
  1381. * of_node_put() on it when done.
  1382. */
  1383. struct device_node *of_find_node_by_name(struct device_node *from,
  1384. const char *name)
  1385. {
  1386. struct device_node *np;
  1387. read_lock(&devtree_lock);
  1388. np = from ? from->allnext : allnodes;
  1389. for (; np != NULL; np = np->allnext)
  1390. if (np->name != NULL && strcasecmp(np->name, name) == 0
  1391. && of_node_get(np))
  1392. break;
  1393. if (from)
  1394. of_node_put(from);
  1395. read_unlock(&devtree_lock);
  1396. return np;
  1397. }
  1398. EXPORT_SYMBOL(of_find_node_by_name);
  1399. /**
  1400. * of_find_node_by_type - Find a node by its "device_type" property
  1401. * @from: The node to start searching from or NULL, the node
  1402. * you pass will not be searched, only the next one
  1403. * will; typically, you pass what the previous call
  1404. * returned. of_node_put() will be called on it
  1405. * @name: The type string to match against
  1406. *
  1407. * Returns a node pointer with refcount incremented, use
  1408. * of_node_put() on it when done.
  1409. */
  1410. struct device_node *of_find_node_by_type(struct device_node *from,
  1411. const char *type)
  1412. {
  1413. struct device_node *np;
  1414. read_lock(&devtree_lock);
  1415. np = from ? from->allnext : allnodes;
  1416. for (; np != 0; np = np->allnext)
  1417. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1418. && of_node_get(np))
  1419. break;
  1420. if (from)
  1421. of_node_put(from);
  1422. read_unlock(&devtree_lock);
  1423. return np;
  1424. }
  1425. EXPORT_SYMBOL(of_find_node_by_type);
  1426. /**
  1427. * of_find_compatible_node - Find a node based on type and one of the
  1428. * tokens in its "compatible" property
  1429. * @from: The node to start searching from or NULL, the node
  1430. * you pass will not be searched, only the next one
  1431. * will; typically, you pass what the previous call
  1432. * returned. of_node_put() will be called on it
  1433. * @type: The type string to match "device_type" or NULL to ignore
  1434. * @compatible: The string to match to one of the tokens in the device
  1435. * "compatible" list.
  1436. *
  1437. * Returns a node pointer with refcount incremented, use
  1438. * of_node_put() on it when done.
  1439. */
  1440. struct device_node *of_find_compatible_node(struct device_node *from,
  1441. const char *type, const char *compatible)
  1442. {
  1443. struct device_node *np;
  1444. read_lock(&devtree_lock);
  1445. np = from ? from->allnext : allnodes;
  1446. for (; np != 0; np = np->allnext) {
  1447. if (type != NULL
  1448. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1449. continue;
  1450. if (device_is_compatible(np, compatible) && of_node_get(np))
  1451. break;
  1452. }
  1453. if (from)
  1454. of_node_put(from);
  1455. read_unlock(&devtree_lock);
  1456. return np;
  1457. }
  1458. EXPORT_SYMBOL(of_find_compatible_node);
  1459. /**
  1460. * of_find_node_by_path - Find a node matching a full OF path
  1461. * @path: The full path to match
  1462. *
  1463. * Returns a node pointer with refcount incremented, use
  1464. * of_node_put() on it when done.
  1465. */
  1466. struct device_node *of_find_node_by_path(const char *path)
  1467. {
  1468. struct device_node *np = allnodes;
  1469. read_lock(&devtree_lock);
  1470. for (; np != 0; np = np->allnext) {
  1471. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1472. && of_node_get(np))
  1473. break;
  1474. }
  1475. read_unlock(&devtree_lock);
  1476. return np;
  1477. }
  1478. EXPORT_SYMBOL(of_find_node_by_path);
  1479. /**
  1480. * of_find_node_by_phandle - Find a node given a phandle
  1481. * @handle: phandle of the node to find
  1482. *
  1483. * Returns a node pointer with refcount incremented, use
  1484. * of_node_put() on it when done.
  1485. */
  1486. struct device_node *of_find_node_by_phandle(phandle handle)
  1487. {
  1488. struct device_node *np;
  1489. read_lock(&devtree_lock);
  1490. for (np = allnodes; np != 0; np = np->allnext)
  1491. if (np->linux_phandle == handle)
  1492. break;
  1493. if (np)
  1494. of_node_get(np);
  1495. read_unlock(&devtree_lock);
  1496. return np;
  1497. }
  1498. EXPORT_SYMBOL(of_find_node_by_phandle);
  1499. /**
  1500. * of_find_all_nodes - Get next node in global list
  1501. * @prev: Previous node or NULL to start iteration
  1502. * of_node_put() will be called on it
  1503. *
  1504. * Returns a node pointer with refcount incremented, use
  1505. * of_node_put() on it when done.
  1506. */
  1507. struct device_node *of_find_all_nodes(struct device_node *prev)
  1508. {
  1509. struct device_node *np;
  1510. read_lock(&devtree_lock);
  1511. np = prev ? prev->allnext : allnodes;
  1512. for (; np != 0; np = np->allnext)
  1513. if (of_node_get(np))
  1514. break;
  1515. if (prev)
  1516. of_node_put(prev);
  1517. read_unlock(&devtree_lock);
  1518. return np;
  1519. }
  1520. EXPORT_SYMBOL(of_find_all_nodes);
  1521. /**
  1522. * of_get_parent - Get a node's parent if any
  1523. * @node: Node to get parent
  1524. *
  1525. * Returns a node pointer with refcount incremented, use
  1526. * of_node_put() on it when done.
  1527. */
  1528. struct device_node *of_get_parent(const struct device_node *node)
  1529. {
  1530. struct device_node *np;
  1531. if (!node)
  1532. return NULL;
  1533. read_lock(&devtree_lock);
  1534. np = of_node_get(node->parent);
  1535. read_unlock(&devtree_lock);
  1536. return np;
  1537. }
  1538. EXPORT_SYMBOL(of_get_parent);
  1539. /**
  1540. * of_get_next_child - Iterate a node childs
  1541. * @node: parent node
  1542. * @prev: previous child of the parent node, or NULL to get first
  1543. *
  1544. * Returns a node pointer with refcount incremented, use
  1545. * of_node_put() on it when done.
  1546. */
  1547. struct device_node *of_get_next_child(const struct device_node *node,
  1548. struct device_node *prev)
  1549. {
  1550. struct device_node *next;
  1551. read_lock(&devtree_lock);
  1552. next = prev ? prev->sibling : node->child;
  1553. for (; next != 0; next = next->sibling)
  1554. if (of_node_get(next))
  1555. break;
  1556. if (prev)
  1557. of_node_put(prev);
  1558. read_unlock(&devtree_lock);
  1559. return next;
  1560. }
  1561. EXPORT_SYMBOL(of_get_next_child);
  1562. /**
  1563. * of_node_get - Increment refcount of a node
  1564. * @node: Node to inc refcount, NULL is supported to
  1565. * simplify writing of callers
  1566. *
  1567. * Returns node.
  1568. */
  1569. struct device_node *of_node_get(struct device_node *node)
  1570. {
  1571. if (node)
  1572. kref_get(&node->kref);
  1573. return node;
  1574. }
  1575. EXPORT_SYMBOL(of_node_get);
  1576. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1577. {
  1578. return container_of(kref, struct device_node, kref);
  1579. }
  1580. /**
  1581. * of_node_release - release a dynamically allocated node
  1582. * @kref: kref element of the node to be released
  1583. *
  1584. * In of_node_put() this function is passed to kref_put()
  1585. * as the destructor.
  1586. */
  1587. static void of_node_release(struct kref *kref)
  1588. {
  1589. struct device_node *node = kref_to_device_node(kref);
  1590. struct property *prop = node->properties;
  1591. if (!OF_IS_DYNAMIC(node))
  1592. return;
  1593. while (prop) {
  1594. struct property *next = prop->next;
  1595. kfree(prop->name);
  1596. kfree(prop->value);
  1597. kfree(prop);
  1598. prop = next;
  1599. if (!prop) {
  1600. prop = node->deadprops;
  1601. node->deadprops = NULL;
  1602. }
  1603. }
  1604. kfree(node->intrs);
  1605. kfree(node->full_name);
  1606. kfree(node->data);
  1607. kfree(node);
  1608. }
  1609. /**
  1610. * of_node_put - Decrement refcount of a node
  1611. * @node: Node to dec refcount, NULL is supported to
  1612. * simplify writing of callers
  1613. *
  1614. */
  1615. void of_node_put(struct device_node *node)
  1616. {
  1617. if (node)
  1618. kref_put(&node->kref, of_node_release);
  1619. }
  1620. EXPORT_SYMBOL(of_node_put);
  1621. /*
  1622. * Plug a device node into the tree and global list.
  1623. */
  1624. void of_attach_node(struct device_node *np)
  1625. {
  1626. write_lock(&devtree_lock);
  1627. np->sibling = np->parent->child;
  1628. np->allnext = allnodes;
  1629. np->parent->child = np;
  1630. allnodes = np;
  1631. write_unlock(&devtree_lock);
  1632. }
  1633. /*
  1634. * "Unplug" a node from the device tree. The caller must hold
  1635. * a reference to the node. The memory associated with the node
  1636. * is not freed until its refcount goes to zero.
  1637. */
  1638. void of_detach_node(const struct device_node *np)
  1639. {
  1640. struct device_node *parent;
  1641. write_lock(&devtree_lock);
  1642. parent = np->parent;
  1643. if (allnodes == np)
  1644. allnodes = np->allnext;
  1645. else {
  1646. struct device_node *prev;
  1647. for (prev = allnodes;
  1648. prev->allnext != np;
  1649. prev = prev->allnext)
  1650. ;
  1651. prev->allnext = np->allnext;
  1652. }
  1653. if (parent->child == np)
  1654. parent->child = np->sibling;
  1655. else {
  1656. struct device_node *prevsib;
  1657. for (prevsib = np->parent->child;
  1658. prevsib->sibling != np;
  1659. prevsib = prevsib->sibling)
  1660. ;
  1661. prevsib->sibling = np->sibling;
  1662. }
  1663. write_unlock(&devtree_lock);
  1664. }
  1665. #ifdef CONFIG_PPC_PSERIES
  1666. /*
  1667. * Fix up the uninitialized fields in a new device node:
  1668. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1669. *
  1670. * A lot of boot-time code is duplicated here, because functions such
  1671. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1672. * slab allocator.
  1673. *
  1674. * This should probably be split up into smaller chunks.
  1675. */
  1676. static int of_finish_dynamic_node(struct device_node *node)
  1677. {
  1678. struct device_node *parent = of_get_parent(node);
  1679. int err = 0;
  1680. phandle *ibm_phandle;
  1681. node->name = get_property(node, "name", NULL);
  1682. node->type = get_property(node, "device_type", NULL);
  1683. if (!parent) {
  1684. err = -ENODEV;
  1685. goto out;
  1686. }
  1687. /* We don't support that function on PowerMac, at least
  1688. * not yet
  1689. */
  1690. if (machine_is(powermac))
  1691. return -ENODEV;
  1692. /* fix up new node's linux_phandle field */
  1693. if ((ibm_phandle = (unsigned int *)get_property(node,
  1694. "ibm,phandle", NULL)))
  1695. node->linux_phandle = *ibm_phandle;
  1696. out:
  1697. of_node_put(parent);
  1698. return err;
  1699. }
  1700. static int prom_reconfig_notifier(struct notifier_block *nb,
  1701. unsigned long action, void *node)
  1702. {
  1703. int err;
  1704. switch (action) {
  1705. case PSERIES_RECONFIG_ADD:
  1706. err = of_finish_dynamic_node(node);
  1707. if (!err)
  1708. finish_node(node, NULL, 0);
  1709. if (err < 0) {
  1710. printk(KERN_ERR "finish_node returned %d\n", err);
  1711. err = NOTIFY_BAD;
  1712. }
  1713. break;
  1714. default:
  1715. err = NOTIFY_DONE;
  1716. break;
  1717. }
  1718. return err;
  1719. }
  1720. static struct notifier_block prom_reconfig_nb = {
  1721. .notifier_call = prom_reconfig_notifier,
  1722. .priority = 10, /* This one needs to run first */
  1723. };
  1724. static int __init prom_reconfig_setup(void)
  1725. {
  1726. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1727. }
  1728. __initcall(prom_reconfig_setup);
  1729. #endif
  1730. struct property *of_find_property(struct device_node *np, const char *name,
  1731. int *lenp)
  1732. {
  1733. struct property *pp;
  1734. read_lock(&devtree_lock);
  1735. for (pp = np->properties; pp != 0; pp = pp->next)
  1736. if (strcmp(pp->name, name) == 0) {
  1737. if (lenp != 0)
  1738. *lenp = pp->length;
  1739. break;
  1740. }
  1741. read_unlock(&devtree_lock);
  1742. return pp;
  1743. }
  1744. /*
  1745. * Find a property with a given name for a given node
  1746. * and return the value.
  1747. */
  1748. unsigned char *get_property(struct device_node *np, const char *name,
  1749. int *lenp)
  1750. {
  1751. struct property *pp = of_find_property(np,name,lenp);
  1752. return pp ? pp->value : NULL;
  1753. }
  1754. EXPORT_SYMBOL(get_property);
  1755. /*
  1756. * Add a property to a node
  1757. */
  1758. int prom_add_property(struct device_node* np, struct property* prop)
  1759. {
  1760. struct property **next;
  1761. prop->next = NULL;
  1762. write_lock(&devtree_lock);
  1763. next = &np->properties;
  1764. while (*next) {
  1765. if (strcmp(prop->name, (*next)->name) == 0) {
  1766. /* duplicate ! don't insert it */
  1767. write_unlock(&devtree_lock);
  1768. return -1;
  1769. }
  1770. next = &(*next)->next;
  1771. }
  1772. *next = prop;
  1773. write_unlock(&devtree_lock);
  1774. #ifdef CONFIG_PROC_DEVICETREE
  1775. /* try to add to proc as well if it was initialized */
  1776. if (np->pde)
  1777. proc_device_tree_add_prop(np->pde, prop);
  1778. #endif /* CONFIG_PROC_DEVICETREE */
  1779. return 0;
  1780. }
  1781. /*
  1782. * Remove a property from a node. Note that we don't actually
  1783. * remove it, since we have given out who-knows-how-many pointers
  1784. * to the data using get-property. Instead we just move the property
  1785. * to the "dead properties" list, so it won't be found any more.
  1786. */
  1787. int prom_remove_property(struct device_node *np, struct property *prop)
  1788. {
  1789. struct property **next;
  1790. int found = 0;
  1791. write_lock(&devtree_lock);
  1792. next = &np->properties;
  1793. while (*next) {
  1794. if (*next == prop) {
  1795. /* found the node */
  1796. *next = prop->next;
  1797. prop->next = np->deadprops;
  1798. np->deadprops = prop;
  1799. found = 1;
  1800. break;
  1801. }
  1802. next = &(*next)->next;
  1803. }
  1804. write_unlock(&devtree_lock);
  1805. if (!found)
  1806. return -ENODEV;
  1807. #ifdef CONFIG_PROC_DEVICETREE
  1808. /* try to remove the proc node as well */
  1809. if (np->pde)
  1810. proc_device_tree_remove_prop(np->pde, prop);
  1811. #endif /* CONFIG_PROC_DEVICETREE */
  1812. return 0;
  1813. }
  1814. /*
  1815. * Update a property in a node. Note that we don't actually
  1816. * remove it, since we have given out who-knows-how-many pointers
  1817. * to the data using get-property. Instead we just move the property
  1818. * to the "dead properties" list, and add the new property to the
  1819. * property list
  1820. */
  1821. int prom_update_property(struct device_node *np,
  1822. struct property *newprop,
  1823. struct property *oldprop)
  1824. {
  1825. struct property **next;
  1826. int found = 0;
  1827. write_lock(&devtree_lock);
  1828. next = &np->properties;
  1829. while (*next) {
  1830. if (*next == oldprop) {
  1831. /* found the node */
  1832. newprop->next = oldprop->next;
  1833. *next = newprop;
  1834. oldprop->next = np->deadprops;
  1835. np->deadprops = oldprop;
  1836. found = 1;
  1837. break;
  1838. }
  1839. next = &(*next)->next;
  1840. }
  1841. write_unlock(&devtree_lock);
  1842. if (!found)
  1843. return -ENODEV;
  1844. #ifdef CONFIG_PROC_DEVICETREE
  1845. /* try to add to proc as well if it was initialized */
  1846. if (np->pde)
  1847. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1848. #endif /* CONFIG_PROC_DEVICETREE */
  1849. return 0;
  1850. }
  1851. /* Find the device node for a given logical cpu number, also returns the cpu
  1852. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1853. * asked for (non NULL)
  1854. */
  1855. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1856. {
  1857. int hardid;
  1858. struct device_node *np;
  1859. hardid = get_hard_smp_processor_id(cpu);
  1860. for_each_node_by_type(np, "cpu") {
  1861. u32 *intserv;
  1862. unsigned int plen, t;
  1863. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1864. * fallback to "reg" property and assume no threads
  1865. */
  1866. intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s",
  1867. &plen);
  1868. if (intserv == NULL) {
  1869. u32 *reg = (u32 *)get_property(np, "reg", NULL);
  1870. if (reg == NULL)
  1871. continue;
  1872. if (*reg == hardid) {
  1873. if (thread)
  1874. *thread = 0;
  1875. return np;
  1876. }
  1877. } else {
  1878. plen /= sizeof(u32);
  1879. for (t = 0; t < plen; t++) {
  1880. if (hardid == intserv[t]) {
  1881. if (thread)
  1882. *thread = t;
  1883. return np;
  1884. }
  1885. }
  1886. }
  1887. }
  1888. return NULL;
  1889. }
  1890. #ifdef DEBUG
  1891. static struct debugfs_blob_wrapper flat_dt_blob;
  1892. static int __init export_flat_device_tree(void)
  1893. {
  1894. struct dentry *d;
  1895. d = debugfs_create_dir("powerpc", NULL);
  1896. if (!d)
  1897. return 1;
  1898. flat_dt_blob.data = initial_boot_params;
  1899. flat_dt_blob.size = initial_boot_params->totalsize;
  1900. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1901. d, &flat_dt_blob);
  1902. if (!d)
  1903. return 1;
  1904. return 0;
  1905. }
  1906. __initcall(export_flat_device_tree);
  1907. #endif