prom.c 52 KB

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