prom.c 44 KB

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