prom.c 53 KB

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