prom.c 52 KB

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