prom.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175
  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. };
  859. static void __init check_cpu_pa_features(unsigned long node)
  860. {
  861. unsigned char *pa_ftrs;
  862. unsigned long len, tablelen, i, bit;
  863. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  864. if (pa_ftrs == NULL)
  865. return;
  866. /* find descriptor with type == 0 */
  867. for (;;) {
  868. if (tablelen < 3)
  869. return;
  870. len = 2 + pa_ftrs[0];
  871. if (tablelen < len)
  872. return; /* descriptor 0 not found */
  873. if (pa_ftrs[1] == 0)
  874. break;
  875. tablelen -= len;
  876. pa_ftrs += len;
  877. }
  878. /* loop over bits we know about */
  879. for (i = 0; i < ARRAY_SIZE(ibm_pa_features); ++i) {
  880. struct ibm_pa_feature *fp = &ibm_pa_features[i];
  881. if (fp->pabyte >= pa_ftrs[0])
  882. continue;
  883. bit = (pa_ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  884. if (bit ^ fp->invert) {
  885. cur_cpu_spec->cpu_features |= fp->cpu_features;
  886. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  887. } else {
  888. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  889. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  890. }
  891. }
  892. }
  893. static int __init early_init_dt_scan_cpus(unsigned long node,
  894. const char *uname, int depth,
  895. void *data)
  896. {
  897. static int logical_cpuid = 0;
  898. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  899. #ifdef CONFIG_ALTIVEC
  900. u32 *prop;
  901. #endif
  902. u32 *intserv;
  903. int i, nthreads;
  904. unsigned long len;
  905. int found = 0;
  906. /* We are scanning "cpu" nodes only */
  907. if (type == NULL || strcmp(type, "cpu") != 0)
  908. return 0;
  909. /* Get physical cpuid */
  910. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  911. if (intserv) {
  912. nthreads = len / sizeof(int);
  913. } else {
  914. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  915. nthreads = 1;
  916. }
  917. /*
  918. * Now see if any of these threads match our boot cpu.
  919. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  920. */
  921. for (i = 0; i < nthreads; i++) {
  922. /*
  923. * version 2 of the kexec param format adds the phys cpuid of
  924. * booted proc.
  925. */
  926. if (initial_boot_params && initial_boot_params->version >= 2) {
  927. if (intserv[i] ==
  928. initial_boot_params->boot_cpuid_phys) {
  929. found = 1;
  930. break;
  931. }
  932. } else {
  933. /*
  934. * Check if it's the boot-cpu, set it's hw index now,
  935. * unfortunately this format did not support booting
  936. * off secondary threads.
  937. */
  938. if (of_get_flat_dt_prop(node,
  939. "linux,boot-cpu", NULL) != NULL) {
  940. found = 1;
  941. break;
  942. }
  943. }
  944. #ifdef CONFIG_SMP
  945. /* logical cpu id is always 0 on UP kernels */
  946. logical_cpuid++;
  947. #endif
  948. }
  949. if (found) {
  950. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  951. intserv[i]);
  952. boot_cpuid = logical_cpuid;
  953. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  954. }
  955. #ifdef CONFIG_ALTIVEC
  956. /* Check if we have a VMX and eventually update CPU features */
  957. prop = (u32 *)of_get_flat_dt_prop(node, "ibm,vmx", NULL);
  958. if (prop && (*prop) > 0) {
  959. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  960. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  961. }
  962. /* Same goes for Apple's "altivec" property */
  963. prop = (u32 *)of_get_flat_dt_prop(node, "altivec", NULL);
  964. if (prop) {
  965. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  966. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  967. }
  968. #endif /* CONFIG_ALTIVEC */
  969. check_cpu_pa_features(node);
  970. #ifdef CONFIG_PPC_PSERIES
  971. if (nthreads > 1)
  972. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  973. else
  974. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  975. #endif
  976. return 0;
  977. }
  978. static int __init early_init_dt_scan_chosen(unsigned long node,
  979. const char *uname, int depth, void *data)
  980. {
  981. unsigned long *lprop;
  982. unsigned long l;
  983. char *p;
  984. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  985. if (depth != 1 ||
  986. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  987. return 0;
  988. #ifdef CONFIG_PPC64
  989. /* check if iommu is forced on or off */
  990. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  991. iommu_is_off = 1;
  992. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  993. iommu_force_on = 1;
  994. #endif
  995. /* mem=x on the command line is the preferred mechanism */
  996. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  997. if (lprop)
  998. memory_limit = *lprop;
  999. #ifdef CONFIG_PPC64
  1000. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  1001. if (lprop)
  1002. tce_alloc_start = *lprop;
  1003. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  1004. if (lprop)
  1005. tce_alloc_end = *lprop;
  1006. #endif
  1007. #ifdef CONFIG_PPC_RTAS
  1008. /* To help early debugging via the front panel, we retrieve a minimal
  1009. * set of RTAS infos now if available
  1010. */
  1011. {
  1012. u64 *basep, *entryp, *sizep;
  1013. basep = of_get_flat_dt_prop(node, "linux,rtas-base", NULL);
  1014. entryp = of_get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  1015. sizep = of_get_flat_dt_prop(node, "linux,rtas-size", NULL);
  1016. if (basep && entryp && sizep) {
  1017. rtas.base = *basep;
  1018. rtas.entry = *entryp;
  1019. rtas.size = *sizep;
  1020. }
  1021. }
  1022. #endif /* CONFIG_PPC_RTAS */
  1023. #ifdef CONFIG_KEXEC
  1024. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  1025. if (lprop)
  1026. crashk_res.start = *lprop;
  1027. lprop = (u64*)of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  1028. if (lprop)
  1029. crashk_res.end = crashk_res.start + *lprop - 1;
  1030. #endif
  1031. /* Retreive command line */
  1032. p = of_get_flat_dt_prop(node, "bootargs", &l);
  1033. if (p != NULL && l > 0)
  1034. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  1035. #ifdef CONFIG_CMDLINE
  1036. if (l == 0 || (l == 1 && (*p) == 0))
  1037. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  1038. #endif /* CONFIG_CMDLINE */
  1039. DBG("Command line is: %s\n", cmd_line);
  1040. /* break now */
  1041. return 1;
  1042. }
  1043. static int __init early_init_dt_scan_root(unsigned long node,
  1044. const char *uname, int depth, void *data)
  1045. {
  1046. u32 *prop;
  1047. if (depth != 0)
  1048. return 0;
  1049. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  1050. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  1051. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  1052. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  1053. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  1054. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  1055. /* break now */
  1056. return 1;
  1057. }
  1058. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  1059. {
  1060. cell_t *p = *cellp;
  1061. unsigned long r;
  1062. /* Ignore more than 2 cells */
  1063. while (s > sizeof(unsigned long) / 4) {
  1064. p++;
  1065. s--;
  1066. }
  1067. r = *p++;
  1068. #ifdef CONFIG_PPC64
  1069. if (s > 1) {
  1070. r <<= 32;
  1071. r |= *(p++);
  1072. s--;
  1073. }
  1074. #endif
  1075. *cellp = p;
  1076. return r;
  1077. }
  1078. static int __init early_init_dt_scan_memory(unsigned long node,
  1079. const char *uname, int depth, void *data)
  1080. {
  1081. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  1082. cell_t *reg, *endp;
  1083. unsigned long l;
  1084. /* We are scanning "memory" nodes only */
  1085. if (type == NULL) {
  1086. /*
  1087. * The longtrail doesn't have a device_type on the
  1088. * /memory node, so look for the node called /memory@0.
  1089. */
  1090. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  1091. return 0;
  1092. } else if (strcmp(type, "memory") != 0)
  1093. return 0;
  1094. reg = (cell_t *)of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  1095. if (reg == NULL)
  1096. reg = (cell_t *)of_get_flat_dt_prop(node, "reg", &l);
  1097. if (reg == NULL)
  1098. return 0;
  1099. endp = reg + (l / sizeof(cell_t));
  1100. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  1101. uname, l, reg[0], reg[1], reg[2], reg[3]);
  1102. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  1103. unsigned long base, size;
  1104. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  1105. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  1106. if (size == 0)
  1107. continue;
  1108. DBG(" - %lx , %lx\n", base, size);
  1109. #ifdef CONFIG_PPC64
  1110. if (iommu_is_off) {
  1111. if (base >= 0x80000000ul)
  1112. continue;
  1113. if ((base + size) > 0x80000000ul)
  1114. size = 0x80000000ul - base;
  1115. }
  1116. #endif
  1117. lmb_add(base, size);
  1118. }
  1119. return 0;
  1120. }
  1121. static void __init early_reserve_mem(void)
  1122. {
  1123. u64 base, size;
  1124. u64 *reserve_map;
  1125. unsigned long self_base;
  1126. unsigned long self_size;
  1127. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  1128. initial_boot_params->off_mem_rsvmap);
  1129. /* before we do anything, lets reserve the dt blob */
  1130. self_base = __pa((unsigned long)initial_boot_params);
  1131. self_size = initial_boot_params->totalsize;
  1132. lmb_reserve(self_base, self_size);
  1133. #ifdef CONFIG_PPC32
  1134. /*
  1135. * Handle the case where we might be booting from an old kexec
  1136. * image that setup the mem_rsvmap as pairs of 32-bit values
  1137. */
  1138. if (*reserve_map > 0xffffffffull) {
  1139. u32 base_32, size_32;
  1140. u32 *reserve_map_32 = (u32 *)reserve_map;
  1141. while (1) {
  1142. base_32 = *(reserve_map_32++);
  1143. size_32 = *(reserve_map_32++);
  1144. if (size_32 == 0)
  1145. break;
  1146. /* skip if the reservation is for the blob */
  1147. if (base_32 == self_base && size_32 == self_size)
  1148. continue;
  1149. DBG("reserving: %x -> %x\n", base_32, size_32);
  1150. lmb_reserve(base_32, size_32);
  1151. }
  1152. return;
  1153. }
  1154. #endif
  1155. while (1) {
  1156. base = *(reserve_map++);
  1157. size = *(reserve_map++);
  1158. if (size == 0)
  1159. break;
  1160. /* skip if the reservation is for the blob */
  1161. if (base == self_base && size == self_size)
  1162. continue;
  1163. DBG("reserving: %llx -> %llx\n", base, size);
  1164. lmb_reserve(base, size);
  1165. }
  1166. #if 0
  1167. DBG("memory reserved, lmbs :\n");
  1168. lmb_dump_all();
  1169. #endif
  1170. }
  1171. void __init early_init_devtree(void *params)
  1172. {
  1173. DBG(" -> early_init_devtree()\n");
  1174. /* Setup flat device-tree pointer */
  1175. initial_boot_params = params;
  1176. /* Retrieve various informations from the /chosen node of the
  1177. * device-tree, including the platform type, initrd location and
  1178. * size, TCE reserve, and more ...
  1179. */
  1180. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1181. /* Scan memory nodes and rebuild LMBs */
  1182. lmb_init();
  1183. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1184. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1185. /* Save command line for /proc/cmdline and then parse parameters */
  1186. strlcpy(saved_command_line, cmd_line, COMMAND_LINE_SIZE);
  1187. parse_early_param();
  1188. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1189. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  1190. reserve_kdump_trampoline();
  1191. reserve_crashkernel();
  1192. early_reserve_mem();
  1193. lmb_enforce_memory_limit(memory_limit);
  1194. lmb_analyze();
  1195. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  1196. /* We may need to relocate the flat tree, do it now.
  1197. * FIXME .. and the initrd too? */
  1198. move_device_tree();
  1199. DBG("Scanning CPUs ...\n");
  1200. /* Retreive CPU related informations from the flat tree
  1201. * (altivec support, boot CPU ID, ...)
  1202. */
  1203. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1204. DBG(" <- early_init_devtree()\n");
  1205. }
  1206. #undef printk
  1207. int
  1208. prom_n_addr_cells(struct device_node* np)
  1209. {
  1210. int* ip;
  1211. do {
  1212. if (np->parent)
  1213. np = np->parent;
  1214. ip = (int *) get_property(np, "#address-cells", NULL);
  1215. if (ip != NULL)
  1216. return *ip;
  1217. } while (np->parent);
  1218. /* No #address-cells property for the root node, default to 1 */
  1219. return 1;
  1220. }
  1221. EXPORT_SYMBOL(prom_n_addr_cells);
  1222. int
  1223. prom_n_size_cells(struct device_node* np)
  1224. {
  1225. int* ip;
  1226. do {
  1227. if (np->parent)
  1228. np = np->parent;
  1229. ip = (int *) get_property(np, "#size-cells", NULL);
  1230. if (ip != NULL)
  1231. return *ip;
  1232. } while (np->parent);
  1233. /* No #size-cells property for the root node, default to 1 */
  1234. return 1;
  1235. }
  1236. EXPORT_SYMBOL(prom_n_size_cells);
  1237. /**
  1238. * Work out the sense (active-low level / active-high edge)
  1239. * of each interrupt from the device tree.
  1240. */
  1241. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1242. {
  1243. struct device_node *np;
  1244. int i, j;
  1245. /* default to level-triggered */
  1246. memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
  1247. for (np = allnodes; np != 0; np = np->allnext) {
  1248. for (j = 0; j < np->n_intrs; j++) {
  1249. i = np->intrs[j].line;
  1250. if (i >= off && i < max)
  1251. senses[i-off] = np->intrs[j].sense;
  1252. }
  1253. }
  1254. }
  1255. /**
  1256. * Construct and return a list of the device_nodes with a given name.
  1257. */
  1258. struct device_node *find_devices(const char *name)
  1259. {
  1260. struct device_node *head, **prevp, *np;
  1261. prevp = &head;
  1262. for (np = allnodes; np != 0; np = np->allnext) {
  1263. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1264. *prevp = np;
  1265. prevp = &np->next;
  1266. }
  1267. }
  1268. *prevp = NULL;
  1269. return head;
  1270. }
  1271. EXPORT_SYMBOL(find_devices);
  1272. /**
  1273. * Construct and return a list of the device_nodes with a given type.
  1274. */
  1275. struct device_node *find_type_devices(const char *type)
  1276. {
  1277. struct device_node *head, **prevp, *np;
  1278. prevp = &head;
  1279. for (np = allnodes; np != 0; np = np->allnext) {
  1280. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1281. *prevp = np;
  1282. prevp = &np->next;
  1283. }
  1284. }
  1285. *prevp = NULL;
  1286. return head;
  1287. }
  1288. EXPORT_SYMBOL(find_type_devices);
  1289. /**
  1290. * Returns all nodes linked together
  1291. */
  1292. struct device_node *find_all_nodes(void)
  1293. {
  1294. struct device_node *head, **prevp, *np;
  1295. prevp = &head;
  1296. for (np = allnodes; np != 0; np = np->allnext) {
  1297. *prevp = np;
  1298. prevp = &np->next;
  1299. }
  1300. *prevp = NULL;
  1301. return head;
  1302. }
  1303. EXPORT_SYMBOL(find_all_nodes);
  1304. /** Checks if the given "compat" string matches one of the strings in
  1305. * the device's "compatible" property
  1306. */
  1307. int device_is_compatible(struct device_node *device, const char *compat)
  1308. {
  1309. const char* cp;
  1310. int cplen, l;
  1311. cp = (char *) get_property(device, "compatible", &cplen);
  1312. if (cp == NULL)
  1313. return 0;
  1314. while (cplen > 0) {
  1315. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1316. return 1;
  1317. l = strlen(cp) + 1;
  1318. cp += l;
  1319. cplen -= l;
  1320. }
  1321. return 0;
  1322. }
  1323. EXPORT_SYMBOL(device_is_compatible);
  1324. /**
  1325. * Indicates whether the root node has a given value in its
  1326. * compatible property.
  1327. */
  1328. int machine_is_compatible(const char *compat)
  1329. {
  1330. struct device_node *root;
  1331. int rc = 0;
  1332. root = of_find_node_by_path("/");
  1333. if (root) {
  1334. rc = device_is_compatible(root, compat);
  1335. of_node_put(root);
  1336. }
  1337. return rc;
  1338. }
  1339. EXPORT_SYMBOL(machine_is_compatible);
  1340. /**
  1341. * Construct and return a list of the device_nodes with a given type
  1342. * and compatible property.
  1343. */
  1344. struct device_node *find_compatible_devices(const char *type,
  1345. const char *compat)
  1346. {
  1347. struct device_node *head, **prevp, *np;
  1348. prevp = &head;
  1349. for (np = allnodes; np != 0; np = np->allnext) {
  1350. if (type != NULL
  1351. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1352. continue;
  1353. if (device_is_compatible(np, compat)) {
  1354. *prevp = np;
  1355. prevp = &np->next;
  1356. }
  1357. }
  1358. *prevp = NULL;
  1359. return head;
  1360. }
  1361. EXPORT_SYMBOL(find_compatible_devices);
  1362. /**
  1363. * Find the device_node with a given full_name.
  1364. */
  1365. struct device_node *find_path_device(const char *path)
  1366. {
  1367. struct device_node *np;
  1368. for (np = allnodes; np != 0; np = np->allnext)
  1369. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1370. return np;
  1371. return NULL;
  1372. }
  1373. EXPORT_SYMBOL(find_path_device);
  1374. /*******
  1375. *
  1376. * New implementation of the OF "find" APIs, return a refcounted
  1377. * object, call of_node_put() when done. The device tree and list
  1378. * are protected by a rw_lock.
  1379. *
  1380. * Note that property management will need some locking as well,
  1381. * this isn't dealt with yet.
  1382. *
  1383. *******/
  1384. /**
  1385. * of_find_node_by_name - Find a node by its "name" property
  1386. * @from: The node to start searching from or NULL, the node
  1387. * you pass will not be searched, only the next one
  1388. * will; typically, you pass what the previous call
  1389. * returned. of_node_put() will be called on it
  1390. * @name: The name string to match against
  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_node_by_name(struct device_node *from,
  1396. const char *name)
  1397. {
  1398. struct device_node *np;
  1399. read_lock(&devtree_lock);
  1400. np = from ? from->allnext : allnodes;
  1401. for (; np != NULL; np = np->allnext)
  1402. if (np->name != NULL && strcasecmp(np->name, name) == 0
  1403. && of_node_get(np))
  1404. break;
  1405. if (from)
  1406. of_node_put(from);
  1407. read_unlock(&devtree_lock);
  1408. return np;
  1409. }
  1410. EXPORT_SYMBOL(of_find_node_by_name);
  1411. /**
  1412. * of_find_node_by_type - Find a node by its "device_type" property
  1413. * @from: The node to start searching from or NULL, the node
  1414. * you pass will not be searched, only the next one
  1415. * will; typically, you pass what the previous call
  1416. * returned. of_node_put() will be called on it
  1417. * @name: The type string to match against
  1418. *
  1419. * Returns a node pointer with refcount incremented, use
  1420. * of_node_put() on it when done.
  1421. */
  1422. struct device_node *of_find_node_by_type(struct device_node *from,
  1423. const char *type)
  1424. {
  1425. struct device_node *np;
  1426. read_lock(&devtree_lock);
  1427. np = from ? from->allnext : allnodes;
  1428. for (; np != 0; np = np->allnext)
  1429. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1430. && of_node_get(np))
  1431. break;
  1432. if (from)
  1433. of_node_put(from);
  1434. read_unlock(&devtree_lock);
  1435. return np;
  1436. }
  1437. EXPORT_SYMBOL(of_find_node_by_type);
  1438. /**
  1439. * of_find_compatible_node - Find a node based on type and one of the
  1440. * tokens in its "compatible" property
  1441. * @from: The node to start searching from or NULL, the node
  1442. * you pass will not be searched, only the next one
  1443. * will; typically, you pass what the previous call
  1444. * returned. of_node_put() will be called on it
  1445. * @type: The type string to match "device_type" or NULL to ignore
  1446. * @compatible: The string to match to one of the tokens in the device
  1447. * "compatible" list.
  1448. *
  1449. * Returns a node pointer with refcount incremented, use
  1450. * of_node_put() on it when done.
  1451. */
  1452. struct device_node *of_find_compatible_node(struct device_node *from,
  1453. const char *type, const char *compatible)
  1454. {
  1455. struct device_node *np;
  1456. read_lock(&devtree_lock);
  1457. np = from ? from->allnext : allnodes;
  1458. for (; np != 0; np = np->allnext) {
  1459. if (type != NULL
  1460. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1461. continue;
  1462. if (device_is_compatible(np, compatible) && of_node_get(np))
  1463. break;
  1464. }
  1465. if (from)
  1466. of_node_put(from);
  1467. read_unlock(&devtree_lock);
  1468. return np;
  1469. }
  1470. EXPORT_SYMBOL(of_find_compatible_node);
  1471. /**
  1472. * of_find_node_by_path - Find a node matching a full OF path
  1473. * @path: The full path to match
  1474. *
  1475. * Returns a node pointer with refcount incremented, use
  1476. * of_node_put() on it when done.
  1477. */
  1478. struct device_node *of_find_node_by_path(const char *path)
  1479. {
  1480. struct device_node *np = allnodes;
  1481. read_lock(&devtree_lock);
  1482. for (; np != 0; np = np->allnext) {
  1483. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1484. && of_node_get(np))
  1485. break;
  1486. }
  1487. read_unlock(&devtree_lock);
  1488. return np;
  1489. }
  1490. EXPORT_SYMBOL(of_find_node_by_path);
  1491. /**
  1492. * of_find_node_by_phandle - Find a node given a phandle
  1493. * @handle: phandle of the node to find
  1494. *
  1495. * Returns a node pointer with refcount incremented, use
  1496. * of_node_put() on it when done.
  1497. */
  1498. struct device_node *of_find_node_by_phandle(phandle handle)
  1499. {
  1500. struct device_node *np;
  1501. read_lock(&devtree_lock);
  1502. for (np = allnodes; np != 0; np = np->allnext)
  1503. if (np->linux_phandle == handle)
  1504. break;
  1505. if (np)
  1506. of_node_get(np);
  1507. read_unlock(&devtree_lock);
  1508. return np;
  1509. }
  1510. EXPORT_SYMBOL(of_find_node_by_phandle);
  1511. /**
  1512. * of_find_all_nodes - Get next node in global list
  1513. * @prev: Previous node or NULL to start iteration
  1514. * of_node_put() will be called on it
  1515. *
  1516. * Returns a node pointer with refcount incremented, use
  1517. * of_node_put() on it when done.
  1518. */
  1519. struct device_node *of_find_all_nodes(struct device_node *prev)
  1520. {
  1521. struct device_node *np;
  1522. read_lock(&devtree_lock);
  1523. np = prev ? prev->allnext : allnodes;
  1524. for (; np != 0; np = np->allnext)
  1525. if (of_node_get(np))
  1526. break;
  1527. if (prev)
  1528. of_node_put(prev);
  1529. read_unlock(&devtree_lock);
  1530. return np;
  1531. }
  1532. EXPORT_SYMBOL(of_find_all_nodes);
  1533. /**
  1534. * of_get_parent - Get a node's parent if any
  1535. * @node: Node to get parent
  1536. *
  1537. * Returns a node pointer with refcount incremented, use
  1538. * of_node_put() on it when done.
  1539. */
  1540. struct device_node *of_get_parent(const struct device_node *node)
  1541. {
  1542. struct device_node *np;
  1543. if (!node)
  1544. return NULL;
  1545. read_lock(&devtree_lock);
  1546. np = of_node_get(node->parent);
  1547. read_unlock(&devtree_lock);
  1548. return np;
  1549. }
  1550. EXPORT_SYMBOL(of_get_parent);
  1551. /**
  1552. * of_get_next_child - Iterate a node childs
  1553. * @node: parent node
  1554. * @prev: previous child of the parent node, or NULL to get first
  1555. *
  1556. * Returns a node pointer with refcount incremented, use
  1557. * of_node_put() on it when done.
  1558. */
  1559. struct device_node *of_get_next_child(const struct device_node *node,
  1560. struct device_node *prev)
  1561. {
  1562. struct device_node *next;
  1563. read_lock(&devtree_lock);
  1564. next = prev ? prev->sibling : node->child;
  1565. for (; next != 0; next = next->sibling)
  1566. if (of_node_get(next))
  1567. break;
  1568. if (prev)
  1569. of_node_put(prev);
  1570. read_unlock(&devtree_lock);
  1571. return next;
  1572. }
  1573. EXPORT_SYMBOL(of_get_next_child);
  1574. /**
  1575. * of_node_get - Increment refcount of a node
  1576. * @node: Node to inc refcount, NULL is supported to
  1577. * simplify writing of callers
  1578. *
  1579. * Returns node.
  1580. */
  1581. struct device_node *of_node_get(struct device_node *node)
  1582. {
  1583. if (node)
  1584. kref_get(&node->kref);
  1585. return node;
  1586. }
  1587. EXPORT_SYMBOL(of_node_get);
  1588. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1589. {
  1590. return container_of(kref, struct device_node, kref);
  1591. }
  1592. /**
  1593. * of_node_release - release a dynamically allocated node
  1594. * @kref: kref element of the node to be released
  1595. *
  1596. * In of_node_put() this function is passed to kref_put()
  1597. * as the destructor.
  1598. */
  1599. static void of_node_release(struct kref *kref)
  1600. {
  1601. struct device_node *node = kref_to_device_node(kref);
  1602. struct property *prop = node->properties;
  1603. if (!OF_IS_DYNAMIC(node))
  1604. return;
  1605. while (prop) {
  1606. struct property *next = prop->next;
  1607. kfree(prop->name);
  1608. kfree(prop->value);
  1609. kfree(prop);
  1610. prop = next;
  1611. if (!prop) {
  1612. prop = node->deadprops;
  1613. node->deadprops = NULL;
  1614. }
  1615. }
  1616. kfree(node->intrs);
  1617. kfree(node->full_name);
  1618. kfree(node->data);
  1619. kfree(node);
  1620. }
  1621. /**
  1622. * of_node_put - Decrement refcount of a node
  1623. * @node: Node to dec refcount, NULL is supported to
  1624. * simplify writing of callers
  1625. *
  1626. */
  1627. void of_node_put(struct device_node *node)
  1628. {
  1629. if (node)
  1630. kref_put(&node->kref, of_node_release);
  1631. }
  1632. EXPORT_SYMBOL(of_node_put);
  1633. /*
  1634. * Plug a device node into the tree and global list.
  1635. */
  1636. void of_attach_node(struct device_node *np)
  1637. {
  1638. write_lock(&devtree_lock);
  1639. np->sibling = np->parent->child;
  1640. np->allnext = allnodes;
  1641. np->parent->child = np;
  1642. allnodes = np;
  1643. write_unlock(&devtree_lock);
  1644. }
  1645. /*
  1646. * "Unplug" a node from the device tree. The caller must hold
  1647. * a reference to the node. The memory associated with the node
  1648. * is not freed until its refcount goes to zero.
  1649. */
  1650. void of_detach_node(const struct device_node *np)
  1651. {
  1652. struct device_node *parent;
  1653. write_lock(&devtree_lock);
  1654. parent = np->parent;
  1655. if (allnodes == np)
  1656. allnodes = np->allnext;
  1657. else {
  1658. struct device_node *prev;
  1659. for (prev = allnodes;
  1660. prev->allnext != np;
  1661. prev = prev->allnext)
  1662. ;
  1663. prev->allnext = np->allnext;
  1664. }
  1665. if (parent->child == np)
  1666. parent->child = np->sibling;
  1667. else {
  1668. struct device_node *prevsib;
  1669. for (prevsib = np->parent->child;
  1670. prevsib->sibling != np;
  1671. prevsib = prevsib->sibling)
  1672. ;
  1673. prevsib->sibling = np->sibling;
  1674. }
  1675. write_unlock(&devtree_lock);
  1676. }
  1677. #ifdef CONFIG_PPC_PSERIES
  1678. /*
  1679. * Fix up the uninitialized fields in a new device node:
  1680. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1681. *
  1682. * A lot of boot-time code is duplicated here, because functions such
  1683. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1684. * slab allocator.
  1685. *
  1686. * This should probably be split up into smaller chunks.
  1687. */
  1688. static int of_finish_dynamic_node(struct device_node *node)
  1689. {
  1690. struct device_node *parent = of_get_parent(node);
  1691. int err = 0;
  1692. phandle *ibm_phandle;
  1693. node->name = get_property(node, "name", NULL);
  1694. node->type = get_property(node, "device_type", NULL);
  1695. if (!parent) {
  1696. err = -ENODEV;
  1697. goto out;
  1698. }
  1699. /* We don't support that function on PowerMac, at least
  1700. * not yet
  1701. */
  1702. if (machine_is(powermac))
  1703. return -ENODEV;
  1704. /* fix up new node's linux_phandle field */
  1705. if ((ibm_phandle = (unsigned int *)get_property(node,
  1706. "ibm,phandle", NULL)))
  1707. node->linux_phandle = *ibm_phandle;
  1708. out:
  1709. of_node_put(parent);
  1710. return err;
  1711. }
  1712. static int prom_reconfig_notifier(struct notifier_block *nb,
  1713. unsigned long action, void *node)
  1714. {
  1715. int err;
  1716. switch (action) {
  1717. case PSERIES_RECONFIG_ADD:
  1718. err = of_finish_dynamic_node(node);
  1719. if (!err)
  1720. finish_node(node, NULL, 0);
  1721. if (err < 0) {
  1722. printk(KERN_ERR "finish_node returned %d\n", err);
  1723. err = NOTIFY_BAD;
  1724. }
  1725. break;
  1726. default:
  1727. err = NOTIFY_DONE;
  1728. break;
  1729. }
  1730. return err;
  1731. }
  1732. static struct notifier_block prom_reconfig_nb = {
  1733. .notifier_call = prom_reconfig_notifier,
  1734. .priority = 10, /* This one needs to run first */
  1735. };
  1736. static int __init prom_reconfig_setup(void)
  1737. {
  1738. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1739. }
  1740. __initcall(prom_reconfig_setup);
  1741. #endif
  1742. struct property *of_find_property(struct device_node *np, const char *name,
  1743. int *lenp)
  1744. {
  1745. struct property *pp;
  1746. read_lock(&devtree_lock);
  1747. for (pp = np->properties; pp != 0; pp = pp->next)
  1748. if (strcmp(pp->name, name) == 0) {
  1749. if (lenp != 0)
  1750. *lenp = pp->length;
  1751. break;
  1752. }
  1753. read_unlock(&devtree_lock);
  1754. return pp;
  1755. }
  1756. /*
  1757. * Find a property with a given name for a given node
  1758. * and return the value.
  1759. */
  1760. unsigned char *get_property(struct device_node *np, const char *name,
  1761. int *lenp)
  1762. {
  1763. struct property *pp = of_find_property(np,name,lenp);
  1764. return pp ? pp->value : NULL;
  1765. }
  1766. EXPORT_SYMBOL(get_property);
  1767. /*
  1768. * Add a property to a node
  1769. */
  1770. int prom_add_property(struct device_node* np, struct property* prop)
  1771. {
  1772. struct property **next;
  1773. prop->next = NULL;
  1774. write_lock(&devtree_lock);
  1775. next = &np->properties;
  1776. while (*next) {
  1777. if (strcmp(prop->name, (*next)->name) == 0) {
  1778. /* duplicate ! don't insert it */
  1779. write_unlock(&devtree_lock);
  1780. return -1;
  1781. }
  1782. next = &(*next)->next;
  1783. }
  1784. *next = prop;
  1785. write_unlock(&devtree_lock);
  1786. #ifdef CONFIG_PROC_DEVICETREE
  1787. /* try to add to proc as well if it was initialized */
  1788. if (np->pde)
  1789. proc_device_tree_add_prop(np->pde, prop);
  1790. #endif /* CONFIG_PROC_DEVICETREE */
  1791. return 0;
  1792. }
  1793. /*
  1794. * Remove a property from a node. Note that we don't actually
  1795. * remove it, since we have given out who-knows-how-many pointers
  1796. * to the data using get-property. Instead we just move the property
  1797. * to the "dead properties" list, so it won't be found any more.
  1798. */
  1799. int prom_remove_property(struct device_node *np, struct property *prop)
  1800. {
  1801. struct property **next;
  1802. int found = 0;
  1803. write_lock(&devtree_lock);
  1804. next = &np->properties;
  1805. while (*next) {
  1806. if (*next == prop) {
  1807. /* found the node */
  1808. *next = prop->next;
  1809. prop->next = np->deadprops;
  1810. np->deadprops = prop;
  1811. found = 1;
  1812. break;
  1813. }
  1814. next = &(*next)->next;
  1815. }
  1816. write_unlock(&devtree_lock);
  1817. if (!found)
  1818. return -ENODEV;
  1819. #ifdef CONFIG_PROC_DEVICETREE
  1820. /* try to remove the proc node as well */
  1821. if (np->pde)
  1822. proc_device_tree_remove_prop(np->pde, prop);
  1823. #endif /* CONFIG_PROC_DEVICETREE */
  1824. return 0;
  1825. }
  1826. /*
  1827. * Update a property in a node. Note that we don't actually
  1828. * remove it, since we have given out who-knows-how-many pointers
  1829. * to the data using get-property. Instead we just move the property
  1830. * to the "dead properties" list, and add the new property to the
  1831. * property list
  1832. */
  1833. int prom_update_property(struct device_node *np,
  1834. struct property *newprop,
  1835. struct property *oldprop)
  1836. {
  1837. struct property **next;
  1838. int found = 0;
  1839. write_lock(&devtree_lock);
  1840. next = &np->properties;
  1841. while (*next) {
  1842. if (*next == oldprop) {
  1843. /* found the node */
  1844. newprop->next = oldprop->next;
  1845. *next = newprop;
  1846. oldprop->next = np->deadprops;
  1847. np->deadprops = oldprop;
  1848. found = 1;
  1849. break;
  1850. }
  1851. next = &(*next)->next;
  1852. }
  1853. write_unlock(&devtree_lock);
  1854. if (!found)
  1855. return -ENODEV;
  1856. #ifdef CONFIG_PROC_DEVICETREE
  1857. /* try to add to proc as well if it was initialized */
  1858. if (np->pde)
  1859. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1860. #endif /* CONFIG_PROC_DEVICETREE */
  1861. return 0;
  1862. }
  1863. /* Find the device node for a given logical cpu number, also returns the cpu
  1864. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1865. * asked for (non NULL)
  1866. */
  1867. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1868. {
  1869. int hardid;
  1870. struct device_node *np;
  1871. hardid = get_hard_smp_processor_id(cpu);
  1872. for_each_node_by_type(np, "cpu") {
  1873. u32 *intserv;
  1874. unsigned int plen, t;
  1875. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1876. * fallback to "reg" property and assume no threads
  1877. */
  1878. intserv = (u32 *)get_property(np, "ibm,ppc-interrupt-server#s",
  1879. &plen);
  1880. if (intserv == NULL) {
  1881. u32 *reg = (u32 *)get_property(np, "reg", NULL);
  1882. if (reg == NULL)
  1883. continue;
  1884. if (*reg == hardid) {
  1885. if (thread)
  1886. *thread = 0;
  1887. return np;
  1888. }
  1889. } else {
  1890. plen /= sizeof(u32);
  1891. for (t = 0; t < plen; t++) {
  1892. if (hardid == intserv[t]) {
  1893. if (thread)
  1894. *thread = t;
  1895. return np;
  1896. }
  1897. }
  1898. }
  1899. }
  1900. return NULL;
  1901. }
  1902. #ifdef DEBUG
  1903. static struct debugfs_blob_wrapper flat_dt_blob;
  1904. static int __init export_flat_device_tree(void)
  1905. {
  1906. struct dentry *d;
  1907. d = debugfs_create_dir("powerpc", NULL);
  1908. if (!d)
  1909. return 1;
  1910. flat_dt_blob.data = initial_boot_params;
  1911. flat_dt_blob.size = initial_boot_params->totalsize;
  1912. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1913. d, &flat_dt_blob);
  1914. if (!d)
  1915. return 1;
  1916. return 0;
  1917. }
  1918. __initcall(export_flat_device_tree);
  1919. #endif