prom.c 52 KB

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