prom.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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. DBG(" <- early_init_devtree()\n");
  1171. }
  1172. #undef printk
  1173. int
  1174. prom_n_addr_cells(struct device_node* np)
  1175. {
  1176. int* ip;
  1177. do {
  1178. if (np->parent)
  1179. np = np->parent;
  1180. ip = (int *) get_property(np, "#address-cells", NULL);
  1181. if (ip != NULL)
  1182. return *ip;
  1183. } while (np->parent);
  1184. /* No #address-cells property for the root node, default to 1 */
  1185. return 1;
  1186. }
  1187. int
  1188. prom_n_size_cells(struct device_node* np)
  1189. {
  1190. int* ip;
  1191. do {
  1192. if (np->parent)
  1193. np = np->parent;
  1194. ip = (int *) get_property(np, "#size-cells", NULL);
  1195. if (ip != NULL)
  1196. return *ip;
  1197. } while (np->parent);
  1198. /* No #size-cells property for the root node, default to 1 */
  1199. return 1;
  1200. }
  1201. /**
  1202. * Work out the sense (active-low level / active-high edge)
  1203. * of each interrupt from the device tree.
  1204. */
  1205. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1206. {
  1207. struct device_node *np;
  1208. int i, j;
  1209. /* default to level-triggered */
  1210. memset(senses, 1, max - off);
  1211. for (np = allnodes; np != 0; np = np->allnext) {
  1212. for (j = 0; j < np->n_intrs; j++) {
  1213. i = np->intrs[j].line;
  1214. if (i >= off && i < max)
  1215. senses[i-off] = np->intrs[j].sense ?
  1216. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
  1217. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
  1218. }
  1219. }
  1220. }
  1221. /**
  1222. * Construct and return a list of the device_nodes with a given name.
  1223. */
  1224. struct device_node *find_devices(const char *name)
  1225. {
  1226. struct device_node *head, **prevp, *np;
  1227. prevp = &head;
  1228. for (np = allnodes; np != 0; np = np->allnext) {
  1229. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1230. *prevp = np;
  1231. prevp = &np->next;
  1232. }
  1233. }
  1234. *prevp = NULL;
  1235. return head;
  1236. }
  1237. EXPORT_SYMBOL(find_devices);
  1238. /**
  1239. * Construct and return a list of the device_nodes with a given type.
  1240. */
  1241. struct device_node *find_type_devices(const char *type)
  1242. {
  1243. struct device_node *head, **prevp, *np;
  1244. prevp = &head;
  1245. for (np = allnodes; np != 0; np = np->allnext) {
  1246. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1247. *prevp = np;
  1248. prevp = &np->next;
  1249. }
  1250. }
  1251. *prevp = NULL;
  1252. return head;
  1253. }
  1254. EXPORT_SYMBOL(find_type_devices);
  1255. /**
  1256. * Returns all nodes linked together
  1257. */
  1258. struct device_node *find_all_nodes(void)
  1259. {
  1260. struct device_node *head, **prevp, *np;
  1261. prevp = &head;
  1262. for (np = allnodes; np != 0; np = np->allnext) {
  1263. *prevp = np;
  1264. prevp = &np->next;
  1265. }
  1266. *prevp = NULL;
  1267. return head;
  1268. }
  1269. EXPORT_SYMBOL(find_all_nodes);
  1270. /** Checks if the given "compat" string matches one of the strings in
  1271. * the device's "compatible" property
  1272. */
  1273. int device_is_compatible(struct device_node *device, const char *compat)
  1274. {
  1275. const char* cp;
  1276. int cplen, l;
  1277. cp = (char *) get_property(device, "compatible", &cplen);
  1278. if (cp == NULL)
  1279. return 0;
  1280. while (cplen > 0) {
  1281. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1282. return 1;
  1283. l = strlen(cp) + 1;
  1284. cp += l;
  1285. cplen -= l;
  1286. }
  1287. return 0;
  1288. }
  1289. EXPORT_SYMBOL(device_is_compatible);
  1290. /**
  1291. * Indicates whether the root node has a given value in its
  1292. * compatible property.
  1293. */
  1294. int machine_is_compatible(const char *compat)
  1295. {
  1296. struct device_node *root;
  1297. int rc = 0;
  1298. root = of_find_node_by_path("/");
  1299. if (root) {
  1300. rc = device_is_compatible(root, compat);
  1301. of_node_put(root);
  1302. }
  1303. return rc;
  1304. }
  1305. EXPORT_SYMBOL(machine_is_compatible);
  1306. /**
  1307. * Construct and return a list of the device_nodes with a given type
  1308. * and compatible property.
  1309. */
  1310. struct device_node *find_compatible_devices(const char *type,
  1311. const char *compat)
  1312. {
  1313. struct device_node *head, **prevp, *np;
  1314. prevp = &head;
  1315. for (np = allnodes; np != 0; np = np->allnext) {
  1316. if (type != NULL
  1317. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1318. continue;
  1319. if (device_is_compatible(np, compat)) {
  1320. *prevp = np;
  1321. prevp = &np->next;
  1322. }
  1323. }
  1324. *prevp = NULL;
  1325. return head;
  1326. }
  1327. EXPORT_SYMBOL(find_compatible_devices);
  1328. /**
  1329. * Find the device_node with a given full_name.
  1330. */
  1331. struct device_node *find_path_device(const char *path)
  1332. {
  1333. struct device_node *np;
  1334. for (np = allnodes; np != 0; np = np->allnext)
  1335. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1336. return np;
  1337. return NULL;
  1338. }
  1339. EXPORT_SYMBOL(find_path_device);
  1340. /*******
  1341. *
  1342. * New implementation of the OF "find" APIs, return a refcounted
  1343. * object, call of_node_put() when done. The device tree and list
  1344. * are protected by a rw_lock.
  1345. *
  1346. * Note that property management will need some locking as well,
  1347. * this isn't dealt with yet.
  1348. *
  1349. *******/
  1350. /**
  1351. * of_find_node_by_name - Find a node by its "name" property
  1352. * @from: The node to start searching from or NULL, the node
  1353. * you pass will not be searched, only the next one
  1354. * will; typically, you pass what the previous call
  1355. * returned. of_node_put() will be called on it
  1356. * @name: The name string to match against
  1357. *
  1358. * Returns a node pointer with refcount incremented, use
  1359. * of_node_put() on it when done.
  1360. */
  1361. struct device_node *of_find_node_by_name(struct device_node *from,
  1362. const char *name)
  1363. {
  1364. struct device_node *np;
  1365. read_lock(&devtree_lock);
  1366. np = from ? from->allnext : allnodes;
  1367. for (; np != 0; np = np->allnext)
  1368. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1369. && of_node_get(np))
  1370. break;
  1371. if (from)
  1372. of_node_put(from);
  1373. read_unlock(&devtree_lock);
  1374. return np;
  1375. }
  1376. EXPORT_SYMBOL(of_find_node_by_name);
  1377. /**
  1378. * of_find_node_by_type - Find a node by its "device_type" property
  1379. * @from: The node to start searching from or NULL, the node
  1380. * you pass will not be searched, only the next one
  1381. * will; typically, you pass what the previous call
  1382. * returned. of_node_put() will be called on it
  1383. * @name: The type string to match against
  1384. *
  1385. * Returns a node pointer with refcount incremented, use
  1386. * of_node_put() on it when done.
  1387. */
  1388. struct device_node *of_find_node_by_type(struct device_node *from,
  1389. const char *type)
  1390. {
  1391. struct device_node *np;
  1392. read_lock(&devtree_lock);
  1393. np = from ? from->allnext : allnodes;
  1394. for (; np != 0; np = np->allnext)
  1395. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1396. && of_node_get(np))
  1397. break;
  1398. if (from)
  1399. of_node_put(from);
  1400. read_unlock(&devtree_lock);
  1401. return np;
  1402. }
  1403. EXPORT_SYMBOL(of_find_node_by_type);
  1404. /**
  1405. * of_find_compatible_node - Find a node based on type and one of the
  1406. * tokens in its "compatible" property
  1407. * @from: The node to start searching from or NULL, the node
  1408. * you pass will not be searched, only the next one
  1409. * will; typically, you pass what the previous call
  1410. * returned. of_node_put() will be called on it
  1411. * @type: The type string to match "device_type" or NULL to ignore
  1412. * @compatible: The string to match to one of the tokens in the device
  1413. * "compatible" list.
  1414. *
  1415. * Returns a node pointer with refcount incremented, use
  1416. * of_node_put() on it when done.
  1417. */
  1418. struct device_node *of_find_compatible_node(struct device_node *from,
  1419. const char *type, const char *compatible)
  1420. {
  1421. struct device_node *np;
  1422. read_lock(&devtree_lock);
  1423. np = from ? from->allnext : allnodes;
  1424. for (; np != 0; np = np->allnext) {
  1425. if (type != NULL
  1426. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1427. continue;
  1428. if (device_is_compatible(np, compatible) && of_node_get(np))
  1429. break;
  1430. }
  1431. if (from)
  1432. of_node_put(from);
  1433. read_unlock(&devtree_lock);
  1434. return np;
  1435. }
  1436. EXPORT_SYMBOL(of_find_compatible_node);
  1437. /**
  1438. * of_find_node_by_path - Find a node matching a full OF path
  1439. * @path: The full path to match
  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_node_by_path(const char *path)
  1445. {
  1446. struct device_node *np = allnodes;
  1447. read_lock(&devtree_lock);
  1448. for (; np != 0; np = np->allnext) {
  1449. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1450. && of_node_get(np))
  1451. break;
  1452. }
  1453. read_unlock(&devtree_lock);
  1454. return np;
  1455. }
  1456. EXPORT_SYMBOL(of_find_node_by_path);
  1457. /**
  1458. * of_find_node_by_phandle - Find a node given a phandle
  1459. * @handle: phandle of the node to find
  1460. *
  1461. * Returns a node pointer with refcount incremented, use
  1462. * of_node_put() on it when done.
  1463. */
  1464. struct device_node *of_find_node_by_phandle(phandle handle)
  1465. {
  1466. struct device_node *np;
  1467. read_lock(&devtree_lock);
  1468. for (np = allnodes; np != 0; np = np->allnext)
  1469. if (np->linux_phandle == handle)
  1470. break;
  1471. if (np)
  1472. of_node_get(np);
  1473. read_unlock(&devtree_lock);
  1474. return np;
  1475. }
  1476. EXPORT_SYMBOL(of_find_node_by_phandle);
  1477. /**
  1478. * of_find_all_nodes - Get next node in global list
  1479. * @prev: Previous node or NULL to start iteration
  1480. * of_node_put() will be called on it
  1481. *
  1482. * Returns a node pointer with refcount incremented, use
  1483. * of_node_put() on it when done.
  1484. */
  1485. struct device_node *of_find_all_nodes(struct device_node *prev)
  1486. {
  1487. struct device_node *np;
  1488. read_lock(&devtree_lock);
  1489. np = prev ? prev->allnext : allnodes;
  1490. for (; np != 0; np = np->allnext)
  1491. if (of_node_get(np))
  1492. break;
  1493. if (prev)
  1494. of_node_put(prev);
  1495. read_unlock(&devtree_lock);
  1496. return np;
  1497. }
  1498. EXPORT_SYMBOL(of_find_all_nodes);
  1499. /**
  1500. * of_get_parent - Get a node's parent if any
  1501. * @node: Node to get parent
  1502. *
  1503. * Returns a node pointer with refcount incremented, use
  1504. * of_node_put() on it when done.
  1505. */
  1506. struct device_node *of_get_parent(const struct device_node *node)
  1507. {
  1508. struct device_node *np;
  1509. if (!node)
  1510. return NULL;
  1511. read_lock(&devtree_lock);
  1512. np = of_node_get(node->parent);
  1513. read_unlock(&devtree_lock);
  1514. return np;
  1515. }
  1516. EXPORT_SYMBOL(of_get_parent);
  1517. /**
  1518. * of_get_next_child - Iterate a node childs
  1519. * @node: parent node
  1520. * @prev: previous child of the parent node, or NULL to get first
  1521. *
  1522. * Returns a node pointer with refcount incremented, use
  1523. * of_node_put() on it when done.
  1524. */
  1525. struct device_node *of_get_next_child(const struct device_node *node,
  1526. struct device_node *prev)
  1527. {
  1528. struct device_node *next;
  1529. read_lock(&devtree_lock);
  1530. next = prev ? prev->sibling : node->child;
  1531. for (; next != 0; next = next->sibling)
  1532. if (of_node_get(next))
  1533. break;
  1534. if (prev)
  1535. of_node_put(prev);
  1536. read_unlock(&devtree_lock);
  1537. return next;
  1538. }
  1539. EXPORT_SYMBOL(of_get_next_child);
  1540. /**
  1541. * of_node_get - Increment refcount of a node
  1542. * @node: Node to inc refcount, NULL is supported to
  1543. * simplify writing of callers
  1544. *
  1545. * Returns node.
  1546. */
  1547. struct device_node *of_node_get(struct device_node *node)
  1548. {
  1549. if (node)
  1550. kref_get(&node->kref);
  1551. return node;
  1552. }
  1553. EXPORT_SYMBOL(of_node_get);
  1554. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1555. {
  1556. return container_of(kref, struct device_node, kref);
  1557. }
  1558. /**
  1559. * of_node_release - release a dynamically allocated node
  1560. * @kref: kref element of the node to be released
  1561. *
  1562. * In of_node_put() this function is passed to kref_put()
  1563. * as the destructor.
  1564. */
  1565. static void of_node_release(struct kref *kref)
  1566. {
  1567. struct device_node *node = kref_to_device_node(kref);
  1568. struct property *prop = node->properties;
  1569. if (!OF_IS_DYNAMIC(node))
  1570. return;
  1571. while (prop) {
  1572. struct property *next = prop->next;
  1573. kfree(prop->name);
  1574. kfree(prop->value);
  1575. kfree(prop);
  1576. prop = next;
  1577. }
  1578. kfree(node->intrs);
  1579. kfree(node->addrs);
  1580. kfree(node->full_name);
  1581. kfree(node->data);
  1582. kfree(node);
  1583. }
  1584. /**
  1585. * of_node_put - Decrement refcount of a node
  1586. * @node: Node to dec refcount, NULL is supported to
  1587. * simplify writing of callers
  1588. *
  1589. */
  1590. void of_node_put(struct device_node *node)
  1591. {
  1592. if (node)
  1593. kref_put(&node->kref, of_node_release);
  1594. }
  1595. EXPORT_SYMBOL(of_node_put);
  1596. /*
  1597. * Plug a device node into the tree and global list.
  1598. */
  1599. void of_attach_node(struct device_node *np)
  1600. {
  1601. write_lock(&devtree_lock);
  1602. np->sibling = np->parent->child;
  1603. np->allnext = allnodes;
  1604. np->parent->child = np;
  1605. allnodes = np;
  1606. write_unlock(&devtree_lock);
  1607. }
  1608. /*
  1609. * "Unplug" a node from the device tree. The caller must hold
  1610. * a reference to the node. The memory associated with the node
  1611. * is not freed until its refcount goes to zero.
  1612. */
  1613. void of_detach_node(const struct device_node *np)
  1614. {
  1615. struct device_node *parent;
  1616. write_lock(&devtree_lock);
  1617. parent = np->parent;
  1618. if (allnodes == np)
  1619. allnodes = np->allnext;
  1620. else {
  1621. struct device_node *prev;
  1622. for (prev = allnodes;
  1623. prev->allnext != np;
  1624. prev = prev->allnext)
  1625. ;
  1626. prev->allnext = np->allnext;
  1627. }
  1628. if (parent->child == np)
  1629. parent->child = np->sibling;
  1630. else {
  1631. struct device_node *prevsib;
  1632. for (prevsib = np->parent->child;
  1633. prevsib->sibling != np;
  1634. prevsib = prevsib->sibling)
  1635. ;
  1636. prevsib->sibling = np->sibling;
  1637. }
  1638. write_unlock(&devtree_lock);
  1639. }
  1640. #ifdef CONFIG_PPC_PSERIES
  1641. /*
  1642. * Fix up the uninitialized fields in a new device node:
  1643. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1644. *
  1645. * A lot of boot-time code is duplicated here, because functions such
  1646. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1647. * slab allocator.
  1648. *
  1649. * This should probably be split up into smaller chunks.
  1650. */
  1651. static int of_finish_dynamic_node(struct device_node *node,
  1652. unsigned long *unused1, int unused2,
  1653. int unused3, int unused4)
  1654. {
  1655. struct device_node *parent = of_get_parent(node);
  1656. int err = 0;
  1657. phandle *ibm_phandle;
  1658. node->name = get_property(node, "name", NULL);
  1659. node->type = get_property(node, "device_type", NULL);
  1660. if (!parent) {
  1661. err = -ENODEV;
  1662. goto out;
  1663. }
  1664. /* We don't support that function on PowerMac, at least
  1665. * not yet
  1666. */
  1667. if (systemcfg->platform == PLATFORM_POWERMAC)
  1668. return -ENODEV;
  1669. /* fix up new node's linux_phandle field */
  1670. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1671. node->linux_phandle = *ibm_phandle;
  1672. out:
  1673. of_node_put(parent);
  1674. return err;
  1675. }
  1676. static int prom_reconfig_notifier(struct notifier_block *nb,
  1677. unsigned long action, void *node)
  1678. {
  1679. int err;
  1680. switch (action) {
  1681. case PSERIES_RECONFIG_ADD:
  1682. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1683. if (err < 0) {
  1684. printk(KERN_ERR "finish_node returned %d\n", err);
  1685. err = NOTIFY_BAD;
  1686. }
  1687. break;
  1688. default:
  1689. err = NOTIFY_DONE;
  1690. break;
  1691. }
  1692. return err;
  1693. }
  1694. static struct notifier_block prom_reconfig_nb = {
  1695. .notifier_call = prom_reconfig_notifier,
  1696. .priority = 10, /* This one needs to run first */
  1697. };
  1698. static int __init prom_reconfig_setup(void)
  1699. {
  1700. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1701. }
  1702. __initcall(prom_reconfig_setup);
  1703. #endif
  1704. /*
  1705. * Find a property with a given name for a given node
  1706. * and return the value.
  1707. */
  1708. unsigned char *get_property(struct device_node *np, const char *name,
  1709. int *lenp)
  1710. {
  1711. struct property *pp;
  1712. for (pp = np->properties; pp != 0; pp = pp->next)
  1713. if (strcmp(pp->name, name) == 0) {
  1714. if (lenp != 0)
  1715. *lenp = pp->length;
  1716. return pp->value;
  1717. }
  1718. return NULL;
  1719. }
  1720. EXPORT_SYMBOL(get_property);
  1721. /*
  1722. * Add a property to a node
  1723. */
  1724. void prom_add_property(struct device_node* np, struct property* prop)
  1725. {
  1726. struct property **next = &np->properties;
  1727. prop->next = NULL;
  1728. while (*next)
  1729. next = &(*next)->next;
  1730. *next = prop;
  1731. }
  1732. /* I quickly hacked that one, check against spec ! */
  1733. static inline unsigned long
  1734. bus_space_to_resource_flags(unsigned int bus_space)
  1735. {
  1736. u8 space = (bus_space >> 24) & 0xf;
  1737. if (space == 0)
  1738. space = 0x02;
  1739. if (space == 0x02)
  1740. return IORESOURCE_MEM;
  1741. else if (space == 0x01)
  1742. return IORESOURCE_IO;
  1743. else {
  1744. printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
  1745. bus_space);
  1746. return 0;
  1747. }
  1748. }
  1749. static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
  1750. struct address_range *range)
  1751. {
  1752. unsigned long mask;
  1753. int i;
  1754. /* Check this one */
  1755. mask = bus_space_to_resource_flags(range->space);
  1756. for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
  1757. if ((pdev->resource[i].flags & mask) == mask &&
  1758. pdev->resource[i].start <= range->address &&
  1759. pdev->resource[i].end > range->address) {
  1760. if ((range->address + range->size - 1) > pdev->resource[i].end) {
  1761. /* Add better message */
  1762. printk(KERN_WARNING "PCI/OF resource overlap !\n");
  1763. return NULL;
  1764. }
  1765. break;
  1766. }
  1767. }
  1768. if (i == DEVICE_COUNT_RESOURCE)
  1769. return NULL;
  1770. return &pdev->resource[i];
  1771. }
  1772. /*
  1773. * Request an OF device resource. Currently handles child of PCI devices,
  1774. * or other nodes attached to the root node. Ultimately, put some
  1775. * link to resources in the OF node.
  1776. */
  1777. struct resource *request_OF_resource(struct device_node* node, int index,
  1778. const char* name_postfix)
  1779. {
  1780. struct pci_dev* pcidev;
  1781. u8 pci_bus, pci_devfn;
  1782. unsigned long iomask;
  1783. struct device_node* nd;
  1784. struct resource* parent;
  1785. struct resource *res = NULL;
  1786. int nlen, plen;
  1787. if (index >= node->n_addrs)
  1788. goto fail;
  1789. /* Sanity check on bus space */
  1790. iomask = bus_space_to_resource_flags(node->addrs[index].space);
  1791. if (iomask & IORESOURCE_MEM)
  1792. parent = &iomem_resource;
  1793. else if (iomask & IORESOURCE_IO)
  1794. parent = &ioport_resource;
  1795. else
  1796. goto fail;
  1797. /* Find a PCI parent if any */
  1798. nd = node;
  1799. pcidev = NULL;
  1800. while (nd) {
  1801. if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
  1802. pcidev = pci_find_slot(pci_bus, pci_devfn);
  1803. if (pcidev) break;
  1804. nd = nd->parent;
  1805. }
  1806. if (pcidev)
  1807. parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
  1808. if (!parent) {
  1809. printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
  1810. node->name);
  1811. goto fail;
  1812. }
  1813. res = __request_region(parent, node->addrs[index].address,
  1814. node->addrs[index].size, NULL);
  1815. if (!res)
  1816. goto fail;
  1817. nlen = strlen(node->name);
  1818. plen = name_postfix ? strlen(name_postfix) : 0;
  1819. res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
  1820. if (res->name) {
  1821. strcpy((char *)res->name, node->name);
  1822. if (plen)
  1823. strcpy((char *)res->name+nlen, name_postfix);
  1824. }
  1825. return res;
  1826. fail:
  1827. return NULL;
  1828. }
  1829. EXPORT_SYMBOL(request_OF_resource);
  1830. int release_OF_resource(struct device_node *node, int index)
  1831. {
  1832. struct pci_dev* pcidev;
  1833. u8 pci_bus, pci_devfn;
  1834. unsigned long iomask, start, end;
  1835. struct device_node* nd;
  1836. struct resource* parent;
  1837. struct resource *res = NULL;
  1838. if (index >= node->n_addrs)
  1839. return -EINVAL;
  1840. /* Sanity check on bus space */
  1841. iomask = bus_space_to_resource_flags(node->addrs[index].space);
  1842. if (iomask & IORESOURCE_MEM)
  1843. parent = &iomem_resource;
  1844. else if (iomask & IORESOURCE_IO)
  1845. parent = &ioport_resource;
  1846. else
  1847. return -EINVAL;
  1848. /* Find a PCI parent if any */
  1849. nd = node;
  1850. pcidev = NULL;
  1851. while(nd) {
  1852. if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
  1853. pcidev = pci_find_slot(pci_bus, pci_devfn);
  1854. if (pcidev) break;
  1855. nd = nd->parent;
  1856. }
  1857. if (pcidev)
  1858. parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
  1859. if (!parent) {
  1860. printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
  1861. node->name);
  1862. return -ENODEV;
  1863. }
  1864. /* Find us in the parent and its childs */
  1865. res = parent->child;
  1866. start = node->addrs[index].address;
  1867. end = start + node->addrs[index].size - 1;
  1868. while (res) {
  1869. if (res->start == start && res->end == end &&
  1870. (res->flags & IORESOURCE_BUSY))
  1871. break;
  1872. if (res->start <= start && res->end >= end)
  1873. res = res->child;
  1874. else
  1875. res = res->sibling;
  1876. }
  1877. if (!res)
  1878. return -ENODEV;
  1879. if (res->name) {
  1880. kfree(res->name);
  1881. res->name = NULL;
  1882. }
  1883. release_resource(res);
  1884. kfree(res);
  1885. return 0;
  1886. }
  1887. EXPORT_SYMBOL(release_OF_resource);