prom.c 52 KB

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