prom.c 52 KB

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