prom.c 52 KB

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