prom.c 46 KB

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