prom.c 51 KB

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