prom.c 50 KB

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