prom.c 52 KB

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