prom.c 48 KB

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