prom.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  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. static 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 == PLATFORM_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 == PLATFORM_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. end = start = (unsigned long) __va(lmb_alloc(size, 128));
  446. finish_node(allnodes, &end, 0);
  447. BUG_ON(end != start + size);
  448. DBG(" <- finish_device_tree\n");
  449. }
  450. static inline char *find_flat_dt_string(u32 offset)
  451. {
  452. return ((char *)initial_boot_params) +
  453. initial_boot_params->off_dt_strings + offset;
  454. }
  455. /**
  456. * This function is used to scan the flattened device-tree, it is
  457. * used to extract the memory informations at boot before we can
  458. * unflatten the tree
  459. */
  460. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  461. const char *uname, int depth,
  462. void *data),
  463. void *data)
  464. {
  465. unsigned long p = ((unsigned long)initial_boot_params) +
  466. initial_boot_params->off_dt_struct;
  467. int rc = 0;
  468. int depth = -1;
  469. do {
  470. u32 tag = *((u32 *)p);
  471. char *pathp;
  472. p += 4;
  473. if (tag == OF_DT_END_NODE) {
  474. depth --;
  475. continue;
  476. }
  477. if (tag == OF_DT_NOP)
  478. continue;
  479. if (tag == OF_DT_END)
  480. break;
  481. if (tag == OF_DT_PROP) {
  482. u32 sz = *((u32 *)p);
  483. p += 8;
  484. if (initial_boot_params->version < 0x10)
  485. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  486. p += sz;
  487. p = _ALIGN(p, 4);
  488. continue;
  489. }
  490. if (tag != OF_DT_BEGIN_NODE) {
  491. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  492. " device tree !\n", tag);
  493. return -EINVAL;
  494. }
  495. depth++;
  496. pathp = (char *)p;
  497. p = _ALIGN(p + strlen(pathp) + 1, 4);
  498. if ((*pathp) == '/') {
  499. char *lp, *np;
  500. for (lp = NULL, np = pathp; *np; np++)
  501. if ((*np) == '/')
  502. lp = np+1;
  503. if (lp != NULL)
  504. pathp = lp;
  505. }
  506. rc = it(p, pathp, depth, data);
  507. if (rc != 0)
  508. break;
  509. } while(1);
  510. return rc;
  511. }
  512. /**
  513. * This function can be used within scan_flattened_dt callback to get
  514. * access to properties
  515. */
  516. void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
  517. unsigned long *size)
  518. {
  519. unsigned long p = node;
  520. do {
  521. u32 tag = *((u32 *)p);
  522. u32 sz, noff;
  523. const char *nstr;
  524. p += 4;
  525. if (tag == OF_DT_NOP)
  526. continue;
  527. if (tag != OF_DT_PROP)
  528. return NULL;
  529. sz = *((u32 *)p);
  530. noff = *((u32 *)(p + 4));
  531. p += 8;
  532. if (initial_boot_params->version < 0x10)
  533. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  534. nstr = find_flat_dt_string(noff);
  535. if (nstr == NULL) {
  536. printk(KERN_WARNING "Can't find property index"
  537. " name !\n");
  538. return NULL;
  539. }
  540. if (strcmp(name, nstr) == 0) {
  541. if (size)
  542. *size = sz;
  543. return (void *)p;
  544. }
  545. p += sz;
  546. p = _ALIGN(p, 4);
  547. } while(1);
  548. }
  549. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  550. unsigned long align)
  551. {
  552. void *res;
  553. *mem = _ALIGN(*mem, align);
  554. res = (void *)*mem;
  555. *mem += size;
  556. return res;
  557. }
  558. static unsigned long __init unflatten_dt_node(unsigned long mem,
  559. unsigned long *p,
  560. struct device_node *dad,
  561. struct device_node ***allnextpp,
  562. unsigned long fpsize)
  563. {
  564. struct device_node *np;
  565. struct property *pp, **prev_pp = NULL;
  566. char *pathp;
  567. u32 tag;
  568. unsigned int l, allocl;
  569. int has_name = 0;
  570. int new_format = 0;
  571. tag = *((u32 *)(*p));
  572. if (tag != OF_DT_BEGIN_NODE) {
  573. printk("Weird tag at start of node: %x\n", tag);
  574. return mem;
  575. }
  576. *p += 4;
  577. pathp = (char *)*p;
  578. l = allocl = strlen(pathp) + 1;
  579. *p = _ALIGN(*p + l, 4);
  580. /* version 0x10 has a more compact unit name here instead of the full
  581. * path. we accumulate the full path size using "fpsize", we'll rebuild
  582. * it later. We detect this because the first character of the name is
  583. * not '/'.
  584. */
  585. if ((*pathp) != '/') {
  586. new_format = 1;
  587. if (fpsize == 0) {
  588. /* root node: special case. fpsize accounts for path
  589. * plus terminating zero. root node only has '/', so
  590. * fpsize should be 2, but we want to avoid the first
  591. * level nodes to have two '/' so we use fpsize 1 here
  592. */
  593. fpsize = 1;
  594. allocl = 2;
  595. } else {
  596. /* account for '/' and path size minus terminal 0
  597. * already in 'l'
  598. */
  599. fpsize += l;
  600. allocl = fpsize;
  601. }
  602. }
  603. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  604. __alignof__(struct device_node));
  605. if (allnextpp) {
  606. memset(np, 0, sizeof(*np));
  607. np->full_name = ((char*)np) + sizeof(struct device_node);
  608. if (new_format) {
  609. char *p = np->full_name;
  610. /* rebuild full path for new format */
  611. if (dad && dad->parent) {
  612. strcpy(p, dad->full_name);
  613. #ifdef DEBUG
  614. if ((strlen(p) + l + 1) != allocl) {
  615. DBG("%s: p: %d, l: %d, a: %d\n",
  616. pathp, strlen(p), l, allocl);
  617. }
  618. #endif
  619. p += strlen(p);
  620. }
  621. *(p++) = '/';
  622. memcpy(p, pathp, l);
  623. } else
  624. memcpy(np->full_name, pathp, l);
  625. prev_pp = &np->properties;
  626. **allnextpp = np;
  627. *allnextpp = &np->allnext;
  628. if (dad != NULL) {
  629. np->parent = dad;
  630. /* we temporarily use the next field as `last_child'*/
  631. if (dad->next == 0)
  632. dad->child = np;
  633. else
  634. dad->next->sibling = np;
  635. dad->next = np;
  636. }
  637. kref_init(&np->kref);
  638. }
  639. while(1) {
  640. u32 sz, noff;
  641. char *pname;
  642. tag = *((u32 *)(*p));
  643. if (tag == OF_DT_NOP) {
  644. *p += 4;
  645. continue;
  646. }
  647. if (tag != OF_DT_PROP)
  648. break;
  649. *p += 4;
  650. sz = *((u32 *)(*p));
  651. noff = *((u32 *)((*p) + 4));
  652. *p += 8;
  653. if (initial_boot_params->version < 0x10)
  654. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  655. pname = find_flat_dt_string(noff);
  656. if (pname == NULL) {
  657. printk("Can't find property name in list !\n");
  658. break;
  659. }
  660. if (strcmp(pname, "name") == 0)
  661. has_name = 1;
  662. l = strlen(pname) + 1;
  663. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  664. __alignof__(struct property));
  665. if (allnextpp) {
  666. if (strcmp(pname, "linux,phandle") == 0) {
  667. np->node = *((u32 *)*p);
  668. if (np->linux_phandle == 0)
  669. np->linux_phandle = np->node;
  670. }
  671. if (strcmp(pname, "ibm,phandle") == 0)
  672. np->linux_phandle = *((u32 *)*p);
  673. pp->name = pname;
  674. pp->length = sz;
  675. pp->value = (void *)*p;
  676. *prev_pp = pp;
  677. prev_pp = &pp->next;
  678. }
  679. *p = _ALIGN((*p) + sz, 4);
  680. }
  681. /* with version 0x10 we may not have the name property, recreate
  682. * it here from the unit name if absent
  683. */
  684. if (!has_name) {
  685. char *p = pathp, *ps = pathp, *pa = NULL;
  686. int sz;
  687. while (*p) {
  688. if ((*p) == '@')
  689. pa = p;
  690. if ((*p) == '/')
  691. ps = p + 1;
  692. p++;
  693. }
  694. if (pa < ps)
  695. pa = p;
  696. sz = (pa - ps) + 1;
  697. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  698. __alignof__(struct property));
  699. if (allnextpp) {
  700. pp->name = "name";
  701. pp->length = sz;
  702. pp->value = (unsigned char *)(pp + 1);
  703. *prev_pp = pp;
  704. prev_pp = &pp->next;
  705. memcpy(pp->value, ps, sz - 1);
  706. ((char *)pp->value)[sz - 1] = 0;
  707. DBG("fixed up name for %s -> %s\n", pathp, pp->value);
  708. }
  709. }
  710. if (allnextpp) {
  711. *prev_pp = NULL;
  712. np->name = get_property(np, "name", NULL);
  713. np->type = get_property(np, "device_type", NULL);
  714. if (!np->name)
  715. np->name = "<NULL>";
  716. if (!np->type)
  717. np->type = "<NULL>";
  718. }
  719. while (tag == OF_DT_BEGIN_NODE) {
  720. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  721. tag = *((u32 *)(*p));
  722. }
  723. if (tag != OF_DT_END_NODE) {
  724. printk("Weird tag at end of node: %x\n", tag);
  725. return mem;
  726. }
  727. *p += 4;
  728. return mem;
  729. }
  730. /**
  731. * unflattens the device-tree passed by the firmware, creating the
  732. * tree of struct device_node. It also fills the "name" and "type"
  733. * pointers of the nodes so the normal device-tree walking functions
  734. * can be used (this used to be done by finish_device_tree)
  735. */
  736. void __init unflatten_device_tree(void)
  737. {
  738. unsigned long start, mem, size;
  739. struct device_node **allnextp = &allnodes;
  740. char *p = NULL;
  741. int l = 0;
  742. DBG(" -> unflatten_device_tree()\n");
  743. /* First pass, scan for size */
  744. start = ((unsigned long)initial_boot_params) +
  745. initial_boot_params->off_dt_struct;
  746. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  747. size = (size | 3) + 1;
  748. DBG(" size is %lx, allocating...\n", size);
  749. /* Allocate memory for the expanded device tree */
  750. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  751. if (!mem) {
  752. DBG("Couldn't allocate memory with lmb_alloc()!\n");
  753. panic("Couldn't allocate memory with lmb_alloc()!\n");
  754. }
  755. mem = (unsigned long) __va(mem);
  756. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  757. DBG(" unflattening %lx...\n", mem);
  758. /* Second pass, do actual unflattening */
  759. start = ((unsigned long)initial_boot_params) +
  760. initial_boot_params->off_dt_struct;
  761. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  762. if (*((u32 *)start) != OF_DT_END)
  763. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  764. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  765. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  766. ((u32 *)mem)[size / 4] );
  767. *allnextp = NULL;
  768. /* Get pointer to OF "/chosen" node for use everywhere */
  769. of_chosen = of_find_node_by_path("/chosen");
  770. if (of_chosen == NULL)
  771. of_chosen = of_find_node_by_path("/chosen@0");
  772. /* Retreive command line */
  773. if (of_chosen != NULL) {
  774. p = (char *)get_property(of_chosen, "bootargs", &l);
  775. if (p != NULL && l > 0)
  776. strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
  777. }
  778. #ifdef CONFIG_CMDLINE
  779. if (l == 0 || (l == 1 && (*p) == 0))
  780. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  781. #endif /* CONFIG_CMDLINE */
  782. DBG("Command line is: %s\n", cmd_line);
  783. DBG(" <- unflatten_device_tree()\n");
  784. }
  785. static int __init early_init_dt_scan_cpus(unsigned long node,
  786. const char *uname, int depth, void *data)
  787. {
  788. u32 *prop;
  789. unsigned long size;
  790. char *type = of_get_flat_dt_prop(node, "device_type", &size);
  791. /* We are scanning "cpu" nodes only */
  792. if (type == NULL || strcmp(type, "cpu") != 0)
  793. return 0;
  794. boot_cpuid = 0;
  795. boot_cpuid_phys = 0;
  796. if (initial_boot_params && initial_boot_params->version >= 2) {
  797. /* version 2 of the kexec param format adds the phys cpuid
  798. * of booted proc.
  799. */
  800. boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
  801. } else {
  802. /* Check if it's the boot-cpu, set it's hw index now */
  803. if (of_get_flat_dt_prop(node,
  804. "linux,boot-cpu", NULL) != NULL) {
  805. prop = of_get_flat_dt_prop(node, "reg", NULL);
  806. if (prop != NULL)
  807. boot_cpuid_phys = *prop;
  808. }
  809. }
  810. set_hard_smp_processor_id(0, boot_cpuid_phys);
  811. #ifdef CONFIG_ALTIVEC
  812. /* Check if we have a VMX and eventually update CPU features */
  813. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
  814. if (prop && (*prop) > 0) {
  815. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  816. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  817. }
  818. /* Same goes for Apple's "altivec" property */
  819. prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
  820. if (prop) {
  821. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  822. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  823. }
  824. #endif /* CONFIG_ALTIVEC */
  825. #ifdef CONFIG_PPC_PSERIES
  826. /*
  827. * Check for an SMT capable CPU and set the CPU feature. We do
  828. * this by looking at the size of the ibm,ppc-interrupt-server#s
  829. * property
  830. */
  831. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
  832. &size);
  833. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  834. if (prop && ((size / sizeof(u32)) > 1))
  835. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  836. #endif
  837. return 0;
  838. }
  839. static int __init early_init_dt_scan_chosen(unsigned long node,
  840. const char *uname, int depth, void *data)
  841. {
  842. u32 *prop;
  843. unsigned long *lprop;
  844. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  845. if (depth != 1 ||
  846. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  847. return 0;
  848. /* get platform type */
  849. prop = (u32 *)of_get_flat_dt_prop(node, "linux,platform", NULL);
  850. if (prop == NULL)
  851. return 0;
  852. #ifdef CONFIG_PPC_MULTIPLATFORM
  853. _machine = *prop;
  854. #endif
  855. #ifdef CONFIG_PPC64
  856. /* check if iommu is forced on or off */
  857. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  858. iommu_is_off = 1;
  859. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  860. iommu_force_on = 1;
  861. #endif
  862. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  863. if (lprop)
  864. memory_limit = *lprop;
  865. #ifdef CONFIG_PPC64
  866. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  867. if (lprop)
  868. tce_alloc_start = *lprop;
  869. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  870. if (lprop)
  871. tce_alloc_end = *lprop;
  872. #endif
  873. #ifdef CONFIG_PPC_RTAS
  874. /* To help early debugging via the front panel, we retrieve a minimal
  875. * set of RTAS infos now if available
  876. */
  877. {
  878. u64 *basep, *entryp;
  879. basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
  880. entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  881. prop = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
  882. if (basep && entryp && prop) {
  883. rtas.base = *basep;
  884. rtas.entry = *entryp;
  885. rtas.size = *prop;
  886. }
  887. }
  888. #endif /* CONFIG_PPC_RTAS */
  889. #ifdef CONFIG_KEXEC
  890. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  891. if (lprop)
  892. crashk_res.start = *lprop;
  893. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  894. if (lprop)
  895. crashk_res.end = crashk_res.start + *lprop - 1;
  896. #endif
  897. /* break now */
  898. return 1;
  899. }
  900. static int __init early_init_dt_scan_root(unsigned long node,
  901. const char *uname, int depth, void *data)
  902. {
  903. u32 *prop;
  904. if (depth != 0)
  905. return 0;
  906. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  907. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  908. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  909. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  910. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  911. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  912. /* break now */
  913. return 1;
  914. }
  915. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  916. {
  917. cell_t *p = *cellp;
  918. unsigned long r;
  919. /* Ignore more than 2 cells */
  920. while (s > sizeof(unsigned long) / 4) {
  921. p++;
  922. s--;
  923. }
  924. r = *p++;
  925. #ifdef CONFIG_PPC64
  926. if (s > 1) {
  927. r <<= 32;
  928. r |= *(p++);
  929. s--;
  930. }
  931. #endif
  932. *cellp = p;
  933. return r;
  934. }
  935. static int __init early_init_dt_scan_memory(unsigned long node,
  936. const char *uname, int depth, void *data)
  937. {
  938. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  939. cell_t *reg, *endp;
  940. unsigned long l;
  941. /* We are scanning "memory" nodes only */
  942. if (type == NULL) {
  943. /*
  944. * The longtrail doesn't have a device_type on the
  945. * /memory node, so look for the node called /memory@0.
  946. */
  947. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  948. return 0;
  949. } else if (strcmp(type, "memory") != 0)
  950. return 0;
  951. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  952. if (reg == NULL)
  953. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  954. if (reg == NULL)
  955. return 0;
  956. endp = reg + (l / sizeof(cell_t));
  957. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  958. uname, l, reg[0], reg[1], reg[2], reg[3]);
  959. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  960. unsigned long base, size;
  961. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  962. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  963. if (size == 0)
  964. continue;
  965. DBG(" - %lx , %lx\n", base, size);
  966. #ifdef CONFIG_PPC64
  967. if (iommu_is_off) {
  968. if (base >= 0x80000000ul)
  969. continue;
  970. if ((base + size) > 0x80000000ul)
  971. size = 0x80000000ul - base;
  972. }
  973. #endif
  974. lmb_add(base, size);
  975. }
  976. return 0;
  977. }
  978. static void __init early_reserve_mem(void)
  979. {
  980. u64 base, size;
  981. u64 *reserve_map;
  982. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  983. initial_boot_params->off_mem_rsvmap);
  984. #ifdef CONFIG_PPC32
  985. /*
  986. * Handle the case where we might be booting from an old kexec
  987. * image that setup the mem_rsvmap as pairs of 32-bit values
  988. */
  989. if (*reserve_map > 0xffffffffull) {
  990. u32 base_32, size_32;
  991. u32 *reserve_map_32 = (u32 *)reserve_map;
  992. while (1) {
  993. base_32 = *(reserve_map_32++);
  994. size_32 = *(reserve_map_32++);
  995. if (size_32 == 0)
  996. break;
  997. DBG("reserving: %lx -> %lx\n", base_32, size_32);
  998. lmb_reserve(base_32, size_32);
  999. }
  1000. return;
  1001. }
  1002. #endif
  1003. while (1) {
  1004. base = *(reserve_map++);
  1005. size = *(reserve_map++);
  1006. if (size == 0)
  1007. break;
  1008. DBG("reserving: %llx -> %llx\n", base, size);
  1009. lmb_reserve(base, size);
  1010. }
  1011. #if 0
  1012. DBG("memory reserved, lmbs :\n");
  1013. lmb_dump_all();
  1014. #endif
  1015. }
  1016. void __init early_init_devtree(void *params)
  1017. {
  1018. DBG(" -> early_init_devtree()\n");
  1019. /* Setup flat device-tree pointer */
  1020. initial_boot_params = params;
  1021. /* Retrieve various informations from the /chosen node of the
  1022. * device-tree, including the platform type, initrd location and
  1023. * size, TCE reserve, and more ...
  1024. */
  1025. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1026. /* Scan memory nodes and rebuild LMBs */
  1027. lmb_init();
  1028. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1029. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1030. lmb_enforce_memory_limit(memory_limit);
  1031. lmb_analyze();
  1032. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  1033. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1034. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  1035. #ifdef CONFIG_CRASH_DUMP
  1036. lmb_reserve(0, KDUMP_RESERVE_LIMIT);
  1037. #endif
  1038. early_reserve_mem();
  1039. DBG("Scanning CPUs ...\n");
  1040. /* Retreive CPU related informations from the flat tree
  1041. * (altivec support, boot CPU ID, ...)
  1042. */
  1043. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1044. DBG(" <- early_init_devtree()\n");
  1045. }
  1046. #undef printk
  1047. int
  1048. prom_n_addr_cells(struct device_node* np)
  1049. {
  1050. int* ip;
  1051. do {
  1052. if (np->parent)
  1053. np = np->parent;
  1054. ip = (int *) get_property(np, "#address-cells", NULL);
  1055. if (ip != NULL)
  1056. return *ip;
  1057. } while (np->parent);
  1058. /* No #address-cells property for the root node, default to 1 */
  1059. return 1;
  1060. }
  1061. EXPORT_SYMBOL(prom_n_addr_cells);
  1062. int
  1063. prom_n_size_cells(struct device_node* np)
  1064. {
  1065. int* ip;
  1066. do {
  1067. if (np->parent)
  1068. np = np->parent;
  1069. ip = (int *) get_property(np, "#size-cells", NULL);
  1070. if (ip != NULL)
  1071. return *ip;
  1072. } while (np->parent);
  1073. /* No #size-cells property for the root node, default to 1 */
  1074. return 1;
  1075. }
  1076. EXPORT_SYMBOL(prom_n_size_cells);
  1077. /**
  1078. * Work out the sense (active-low level / active-high edge)
  1079. * of each interrupt from the device tree.
  1080. */
  1081. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1082. {
  1083. struct device_node *np;
  1084. int i, j;
  1085. /* default to level-triggered */
  1086. memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
  1087. for (np = allnodes; np != 0; np = np->allnext) {
  1088. for (j = 0; j < np->n_intrs; j++) {
  1089. i = np->intrs[j].line;
  1090. if (i >= off && i < max)
  1091. senses[i-off] = np->intrs[j].sense;
  1092. }
  1093. }
  1094. }
  1095. /**
  1096. * Construct and return a list of the device_nodes with a given name.
  1097. */
  1098. struct device_node *find_devices(const char *name)
  1099. {
  1100. struct device_node *head, **prevp, *np;
  1101. prevp = &head;
  1102. for (np = allnodes; np != 0; np = np->allnext) {
  1103. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1104. *prevp = np;
  1105. prevp = &np->next;
  1106. }
  1107. }
  1108. *prevp = NULL;
  1109. return head;
  1110. }
  1111. EXPORT_SYMBOL(find_devices);
  1112. /**
  1113. * Construct and return a list of the device_nodes with a given type.
  1114. */
  1115. struct device_node *find_type_devices(const char *type)
  1116. {
  1117. struct device_node *head, **prevp, *np;
  1118. prevp = &head;
  1119. for (np = allnodes; np != 0; np = np->allnext) {
  1120. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1121. *prevp = np;
  1122. prevp = &np->next;
  1123. }
  1124. }
  1125. *prevp = NULL;
  1126. return head;
  1127. }
  1128. EXPORT_SYMBOL(find_type_devices);
  1129. /**
  1130. * Returns all nodes linked together
  1131. */
  1132. struct device_node *find_all_nodes(void)
  1133. {
  1134. struct device_node *head, **prevp, *np;
  1135. prevp = &head;
  1136. for (np = allnodes; np != 0; np = np->allnext) {
  1137. *prevp = np;
  1138. prevp = &np->next;
  1139. }
  1140. *prevp = NULL;
  1141. return head;
  1142. }
  1143. EXPORT_SYMBOL(find_all_nodes);
  1144. /** Checks if the given "compat" string matches one of the strings in
  1145. * the device's "compatible" property
  1146. */
  1147. int device_is_compatible(struct device_node *device, const char *compat)
  1148. {
  1149. const char* cp;
  1150. int cplen, l;
  1151. cp = (char *) get_property(device, "compatible", &cplen);
  1152. if (cp == NULL)
  1153. return 0;
  1154. while (cplen > 0) {
  1155. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1156. return 1;
  1157. l = strlen(cp) + 1;
  1158. cp += l;
  1159. cplen -= l;
  1160. }
  1161. return 0;
  1162. }
  1163. EXPORT_SYMBOL(device_is_compatible);
  1164. /**
  1165. * Indicates whether the root node has a given value in its
  1166. * compatible property.
  1167. */
  1168. int machine_is_compatible(const char *compat)
  1169. {
  1170. struct device_node *root;
  1171. int rc = 0;
  1172. root = of_find_node_by_path("/");
  1173. if (root) {
  1174. rc = device_is_compatible(root, compat);
  1175. of_node_put(root);
  1176. }
  1177. return rc;
  1178. }
  1179. EXPORT_SYMBOL(machine_is_compatible);
  1180. /**
  1181. * Construct and return a list of the device_nodes with a given type
  1182. * and compatible property.
  1183. */
  1184. struct device_node *find_compatible_devices(const char *type,
  1185. const char *compat)
  1186. {
  1187. struct device_node *head, **prevp, *np;
  1188. prevp = &head;
  1189. for (np = allnodes; np != 0; np = np->allnext) {
  1190. if (type != NULL
  1191. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1192. continue;
  1193. if (device_is_compatible(np, compat)) {
  1194. *prevp = np;
  1195. prevp = &np->next;
  1196. }
  1197. }
  1198. *prevp = NULL;
  1199. return head;
  1200. }
  1201. EXPORT_SYMBOL(find_compatible_devices);
  1202. /**
  1203. * Find the device_node with a given full_name.
  1204. */
  1205. struct device_node *find_path_device(const char *path)
  1206. {
  1207. struct device_node *np;
  1208. for (np = allnodes; np != 0; np = np->allnext)
  1209. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1210. return np;
  1211. return NULL;
  1212. }
  1213. EXPORT_SYMBOL(find_path_device);
  1214. /*******
  1215. *
  1216. * New implementation of the OF "find" APIs, return a refcounted
  1217. * object, call of_node_put() when done. The device tree and list
  1218. * are protected by a rw_lock.
  1219. *
  1220. * Note that property management will need some locking as well,
  1221. * this isn't dealt with yet.
  1222. *
  1223. *******/
  1224. /**
  1225. * of_find_node_by_name - Find a node by its "name" property
  1226. * @from: The node to start searching from or NULL, the node
  1227. * you pass will not be searched, only the next one
  1228. * will; typically, you pass what the previous call
  1229. * returned. of_node_put() will be called on it
  1230. * @name: The name string to match against
  1231. *
  1232. * Returns a node pointer with refcount incremented, use
  1233. * of_node_put() on it when done.
  1234. */
  1235. struct device_node *of_find_node_by_name(struct device_node *from,
  1236. const char *name)
  1237. {
  1238. struct device_node *np;
  1239. read_lock(&devtree_lock);
  1240. np = from ? from->allnext : allnodes;
  1241. for (; np != 0; np = np->allnext)
  1242. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1243. && of_node_get(np))
  1244. break;
  1245. if (from)
  1246. of_node_put(from);
  1247. read_unlock(&devtree_lock);
  1248. return np;
  1249. }
  1250. EXPORT_SYMBOL(of_find_node_by_name);
  1251. /**
  1252. * of_find_node_by_type - Find a node by its "device_type" property
  1253. * @from: The node to start searching from or NULL, the node
  1254. * you pass will not be searched, only the next one
  1255. * will; typically, you pass what the previous call
  1256. * returned. of_node_put() will be called on it
  1257. * @name: The type string to match against
  1258. *
  1259. * Returns a node pointer with refcount incremented, use
  1260. * of_node_put() on it when done.
  1261. */
  1262. struct device_node *of_find_node_by_type(struct device_node *from,
  1263. const char *type)
  1264. {
  1265. struct device_node *np;
  1266. read_lock(&devtree_lock);
  1267. np = from ? from->allnext : allnodes;
  1268. for (; np != 0; np = np->allnext)
  1269. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1270. && of_node_get(np))
  1271. break;
  1272. if (from)
  1273. of_node_put(from);
  1274. read_unlock(&devtree_lock);
  1275. return np;
  1276. }
  1277. EXPORT_SYMBOL(of_find_node_by_type);
  1278. /**
  1279. * of_find_compatible_node - Find a node based on type and one of the
  1280. * tokens in its "compatible" property
  1281. * @from: The node to start searching from or NULL, the node
  1282. * you pass will not be searched, only the next one
  1283. * will; typically, you pass what the previous call
  1284. * returned. of_node_put() will be called on it
  1285. * @type: The type string to match "device_type" or NULL to ignore
  1286. * @compatible: The string to match to one of the tokens in the device
  1287. * "compatible" list.
  1288. *
  1289. * Returns a node pointer with refcount incremented, use
  1290. * of_node_put() on it when done.
  1291. */
  1292. struct device_node *of_find_compatible_node(struct device_node *from,
  1293. const char *type, const char *compatible)
  1294. {
  1295. struct device_node *np;
  1296. read_lock(&devtree_lock);
  1297. np = from ? from->allnext : allnodes;
  1298. for (; np != 0; np = np->allnext) {
  1299. if (type != NULL
  1300. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1301. continue;
  1302. if (device_is_compatible(np, compatible) && of_node_get(np))
  1303. break;
  1304. }
  1305. if (from)
  1306. of_node_put(from);
  1307. read_unlock(&devtree_lock);
  1308. return np;
  1309. }
  1310. EXPORT_SYMBOL(of_find_compatible_node);
  1311. /**
  1312. * of_find_node_by_path - Find a node matching a full OF path
  1313. * @path: The full path to match
  1314. *
  1315. * Returns a node pointer with refcount incremented, use
  1316. * of_node_put() on it when done.
  1317. */
  1318. struct device_node *of_find_node_by_path(const char *path)
  1319. {
  1320. struct device_node *np = allnodes;
  1321. read_lock(&devtree_lock);
  1322. for (; np != 0; np = np->allnext) {
  1323. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1324. && of_node_get(np))
  1325. break;
  1326. }
  1327. read_unlock(&devtree_lock);
  1328. return np;
  1329. }
  1330. EXPORT_SYMBOL(of_find_node_by_path);
  1331. /**
  1332. * of_find_node_by_phandle - Find a node given a phandle
  1333. * @handle: phandle of the node to find
  1334. *
  1335. * Returns a node pointer with refcount incremented, use
  1336. * of_node_put() on it when done.
  1337. */
  1338. struct device_node *of_find_node_by_phandle(phandle handle)
  1339. {
  1340. struct device_node *np;
  1341. read_lock(&devtree_lock);
  1342. for (np = allnodes; np != 0; np = np->allnext)
  1343. if (np->linux_phandle == handle)
  1344. break;
  1345. if (np)
  1346. of_node_get(np);
  1347. read_unlock(&devtree_lock);
  1348. return np;
  1349. }
  1350. EXPORT_SYMBOL(of_find_node_by_phandle);
  1351. /**
  1352. * of_find_all_nodes - Get next node in global list
  1353. * @prev: Previous node or NULL to start iteration
  1354. * of_node_put() will be called on it
  1355. *
  1356. * Returns a node pointer with refcount incremented, use
  1357. * of_node_put() on it when done.
  1358. */
  1359. struct device_node *of_find_all_nodes(struct device_node *prev)
  1360. {
  1361. struct device_node *np;
  1362. read_lock(&devtree_lock);
  1363. np = prev ? prev->allnext : allnodes;
  1364. for (; np != 0; np = np->allnext)
  1365. if (of_node_get(np))
  1366. break;
  1367. if (prev)
  1368. of_node_put(prev);
  1369. read_unlock(&devtree_lock);
  1370. return np;
  1371. }
  1372. EXPORT_SYMBOL(of_find_all_nodes);
  1373. /**
  1374. * of_get_parent - Get a node's parent if any
  1375. * @node: Node to get parent
  1376. *
  1377. * Returns a node pointer with refcount incremented, use
  1378. * of_node_put() on it when done.
  1379. */
  1380. struct device_node *of_get_parent(const struct device_node *node)
  1381. {
  1382. struct device_node *np;
  1383. if (!node)
  1384. return NULL;
  1385. read_lock(&devtree_lock);
  1386. np = of_node_get(node->parent);
  1387. read_unlock(&devtree_lock);
  1388. return np;
  1389. }
  1390. EXPORT_SYMBOL(of_get_parent);
  1391. /**
  1392. * of_get_next_child - Iterate a node childs
  1393. * @node: parent node
  1394. * @prev: previous child of the parent node, or NULL to get first
  1395. *
  1396. * Returns a node pointer with refcount incremented, use
  1397. * of_node_put() on it when done.
  1398. */
  1399. struct device_node *of_get_next_child(const struct device_node *node,
  1400. struct device_node *prev)
  1401. {
  1402. struct device_node *next;
  1403. read_lock(&devtree_lock);
  1404. next = prev ? prev->sibling : node->child;
  1405. for (; next != 0; next = next->sibling)
  1406. if (of_node_get(next))
  1407. break;
  1408. if (prev)
  1409. of_node_put(prev);
  1410. read_unlock(&devtree_lock);
  1411. return next;
  1412. }
  1413. EXPORT_SYMBOL(of_get_next_child);
  1414. /**
  1415. * of_node_get - Increment refcount of a node
  1416. * @node: Node to inc refcount, NULL is supported to
  1417. * simplify writing of callers
  1418. *
  1419. * Returns node.
  1420. */
  1421. struct device_node *of_node_get(struct device_node *node)
  1422. {
  1423. if (node)
  1424. kref_get(&node->kref);
  1425. return node;
  1426. }
  1427. EXPORT_SYMBOL(of_node_get);
  1428. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1429. {
  1430. return container_of(kref, struct device_node, kref);
  1431. }
  1432. /**
  1433. * of_node_release - release a dynamically allocated node
  1434. * @kref: kref element of the node to be released
  1435. *
  1436. * In of_node_put() this function is passed to kref_put()
  1437. * as the destructor.
  1438. */
  1439. static void of_node_release(struct kref *kref)
  1440. {
  1441. struct device_node *node = kref_to_device_node(kref);
  1442. struct property *prop = node->properties;
  1443. if (!OF_IS_DYNAMIC(node))
  1444. return;
  1445. while (prop) {
  1446. struct property *next = prop->next;
  1447. kfree(prop->name);
  1448. kfree(prop->value);
  1449. kfree(prop);
  1450. prop = next;
  1451. if (!prop) {
  1452. prop = node->deadprops;
  1453. node->deadprops = NULL;
  1454. }
  1455. }
  1456. kfree(node->intrs);
  1457. kfree(node->full_name);
  1458. kfree(node->data);
  1459. kfree(node);
  1460. }
  1461. /**
  1462. * of_node_put - Decrement refcount of a node
  1463. * @node: Node to dec refcount, NULL is supported to
  1464. * simplify writing of callers
  1465. *
  1466. */
  1467. void of_node_put(struct device_node *node)
  1468. {
  1469. if (node)
  1470. kref_put(&node->kref, of_node_release);
  1471. }
  1472. EXPORT_SYMBOL(of_node_put);
  1473. /*
  1474. * Plug a device node into the tree and global list.
  1475. */
  1476. void of_attach_node(struct device_node *np)
  1477. {
  1478. write_lock(&devtree_lock);
  1479. np->sibling = np->parent->child;
  1480. np->allnext = allnodes;
  1481. np->parent->child = np;
  1482. allnodes = np;
  1483. write_unlock(&devtree_lock);
  1484. }
  1485. /*
  1486. * "Unplug" a node from the device tree. The caller must hold
  1487. * a reference to the node. The memory associated with the node
  1488. * is not freed until its refcount goes to zero.
  1489. */
  1490. void of_detach_node(const struct device_node *np)
  1491. {
  1492. struct device_node *parent;
  1493. write_lock(&devtree_lock);
  1494. parent = np->parent;
  1495. if (allnodes == np)
  1496. allnodes = np->allnext;
  1497. else {
  1498. struct device_node *prev;
  1499. for (prev = allnodes;
  1500. prev->allnext != np;
  1501. prev = prev->allnext)
  1502. ;
  1503. prev->allnext = np->allnext;
  1504. }
  1505. if (parent->child == np)
  1506. parent->child = np->sibling;
  1507. else {
  1508. struct device_node *prevsib;
  1509. for (prevsib = np->parent->child;
  1510. prevsib->sibling != np;
  1511. prevsib = prevsib->sibling)
  1512. ;
  1513. prevsib->sibling = np->sibling;
  1514. }
  1515. write_unlock(&devtree_lock);
  1516. }
  1517. #ifdef CONFIG_PPC_PSERIES
  1518. /*
  1519. * Fix up the uninitialized fields in a new device node:
  1520. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1521. *
  1522. * A lot of boot-time code is duplicated here, because functions such
  1523. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1524. * slab allocator.
  1525. *
  1526. * This should probably be split up into smaller chunks.
  1527. */
  1528. static int of_finish_dynamic_node(struct device_node *node)
  1529. {
  1530. struct device_node *parent = of_get_parent(node);
  1531. int err = 0;
  1532. phandle *ibm_phandle;
  1533. node->name = get_property(node, "name", NULL);
  1534. node->type = get_property(node, "device_type", NULL);
  1535. if (!parent) {
  1536. err = -ENODEV;
  1537. goto out;
  1538. }
  1539. /* We don't support that function on PowerMac, at least
  1540. * not yet
  1541. */
  1542. if (_machine == PLATFORM_POWERMAC)
  1543. return -ENODEV;
  1544. /* fix up new node's linux_phandle field */
  1545. if ((ibm_phandle = (unsigned int *)get_property(node,
  1546. "ibm,phandle", NULL)))
  1547. node->linux_phandle = *ibm_phandle;
  1548. out:
  1549. of_node_put(parent);
  1550. return err;
  1551. }
  1552. static int prom_reconfig_notifier(struct notifier_block *nb,
  1553. unsigned long action, void *node)
  1554. {
  1555. int err;
  1556. switch (action) {
  1557. case PSERIES_RECONFIG_ADD:
  1558. err = of_finish_dynamic_node(node);
  1559. if (!err)
  1560. finish_node(node, NULL, 0);
  1561. if (err < 0) {
  1562. printk(KERN_ERR "finish_node returned %d\n", err);
  1563. err = NOTIFY_BAD;
  1564. }
  1565. break;
  1566. default:
  1567. err = NOTIFY_DONE;
  1568. break;
  1569. }
  1570. return err;
  1571. }
  1572. static struct notifier_block prom_reconfig_nb = {
  1573. .notifier_call = prom_reconfig_notifier,
  1574. .priority = 10, /* This one needs to run first */
  1575. };
  1576. static int __init prom_reconfig_setup(void)
  1577. {
  1578. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1579. }
  1580. __initcall(prom_reconfig_setup);
  1581. #endif
  1582. struct property *of_find_property(struct device_node *np, const char *name,
  1583. int *lenp)
  1584. {
  1585. struct property *pp;
  1586. read_lock(&devtree_lock);
  1587. for (pp = np->properties; pp != 0; pp = pp->next)
  1588. if (strcmp(pp->name, name) == 0) {
  1589. if (lenp != 0)
  1590. *lenp = pp->length;
  1591. break;
  1592. }
  1593. read_unlock(&devtree_lock);
  1594. return pp;
  1595. }
  1596. /*
  1597. * Find a property with a given name for a given node
  1598. * and return the value.
  1599. */
  1600. unsigned char *get_property(struct device_node *np, const char *name,
  1601. int *lenp)
  1602. {
  1603. struct property *pp = of_find_property(np,name,lenp);
  1604. return pp ? pp->value : NULL;
  1605. }
  1606. EXPORT_SYMBOL(get_property);
  1607. /*
  1608. * Add a property to a node
  1609. */
  1610. int prom_add_property(struct device_node* np, struct property* prop)
  1611. {
  1612. struct property **next;
  1613. prop->next = NULL;
  1614. write_lock(&devtree_lock);
  1615. next = &np->properties;
  1616. while (*next) {
  1617. if (strcmp(prop->name, (*next)->name) == 0) {
  1618. /* duplicate ! don't insert it */
  1619. write_unlock(&devtree_lock);
  1620. return -1;
  1621. }
  1622. next = &(*next)->next;
  1623. }
  1624. *next = prop;
  1625. write_unlock(&devtree_lock);
  1626. #ifdef CONFIG_PROC_DEVICETREE
  1627. /* try to add to proc as well if it was initialized */
  1628. if (np->pde)
  1629. proc_device_tree_add_prop(np->pde, prop);
  1630. #endif /* CONFIG_PROC_DEVICETREE */
  1631. return 0;
  1632. }
  1633. /*
  1634. * Remove a property from a node. Note that we don't actually
  1635. * remove it, since we have given out who-knows-how-many pointers
  1636. * to the data using get-property. Instead we just move the property
  1637. * to the "dead properties" list, so it won't be found any more.
  1638. */
  1639. int prom_remove_property(struct device_node *np, struct property *prop)
  1640. {
  1641. struct property **next;
  1642. int found = 0;
  1643. write_lock(&devtree_lock);
  1644. next = &np->properties;
  1645. while (*next) {
  1646. if (*next == prop) {
  1647. /* found the node */
  1648. *next = prop->next;
  1649. prop->next = np->deadprops;
  1650. np->deadprops = prop;
  1651. found = 1;
  1652. break;
  1653. }
  1654. next = &(*next)->next;
  1655. }
  1656. write_unlock(&devtree_lock);
  1657. if (!found)
  1658. return -ENODEV;
  1659. #ifdef CONFIG_PROC_DEVICETREE
  1660. /* try to remove the proc node as well */
  1661. if (np->pde)
  1662. proc_device_tree_remove_prop(np->pde, prop);
  1663. #endif /* CONFIG_PROC_DEVICETREE */
  1664. return 0;
  1665. }
  1666. /*
  1667. * Update a property in a node. Note that we don't actually
  1668. * remove it, since we have given out who-knows-how-many pointers
  1669. * to the data using get-property. Instead we just move the property
  1670. * to the "dead properties" list, and add the new property to the
  1671. * property list
  1672. */
  1673. int prom_update_property(struct device_node *np,
  1674. struct property *newprop,
  1675. struct property *oldprop)
  1676. {
  1677. struct property **next;
  1678. int found = 0;
  1679. write_lock(&devtree_lock);
  1680. next = &np->properties;
  1681. while (*next) {
  1682. if (*next == oldprop) {
  1683. /* found the node */
  1684. newprop->next = oldprop->next;
  1685. *next = newprop;
  1686. oldprop->next = np->deadprops;
  1687. np->deadprops = oldprop;
  1688. found = 1;
  1689. break;
  1690. }
  1691. next = &(*next)->next;
  1692. }
  1693. write_unlock(&devtree_lock);
  1694. if (!found)
  1695. return -ENODEV;
  1696. #ifdef CONFIG_PROC_DEVICETREE
  1697. /* try to add to proc as well if it was initialized */
  1698. if (np->pde)
  1699. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1700. #endif /* CONFIG_PROC_DEVICETREE */
  1701. return 0;
  1702. }