prom.c 53 KB

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