prom.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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. static int __init 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. static void* __init 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 = 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. #ifdef CONFIG_PPC_PSERIES
  984. /* On LPAR, look for the first ibm,pft-size property for the hash table size
  985. */
  986. if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
  987. u32 *pft_size;
  988. pft_size = get_flat_dt_prop(node, "ibm,pft-size", NULL);
  989. if (pft_size != NULL) {
  990. /* pft_size[0] is the NUMA CEC cookie */
  991. ppc64_pft_size = pft_size[1];
  992. }
  993. }
  994. #endif
  995. boot_cpuid = 0;
  996. boot_cpuid_phys = 0;
  997. if (initial_boot_params && initial_boot_params->version >= 2) {
  998. /* version 2 of the kexec param format adds the phys cpuid
  999. * of booted proc.
  1000. */
  1001. boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
  1002. } else {
  1003. /* Check if it's the boot-cpu, set it's hw index now */
  1004. if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
  1005. prop = get_flat_dt_prop(node, "reg", NULL);
  1006. if (prop != NULL)
  1007. boot_cpuid_phys = *prop;
  1008. }
  1009. }
  1010. set_hard_smp_processor_id(0, boot_cpuid_phys);
  1011. #ifdef CONFIG_ALTIVEC
  1012. /* Check if we have a VMX and eventually update CPU features */
  1013. prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", &size);
  1014. if (prop && (*prop) > 0) {
  1015. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  1016. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  1017. }
  1018. /* Same goes for Apple's "altivec" property */
  1019. prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
  1020. if (prop) {
  1021. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  1022. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  1023. }
  1024. #endif /* CONFIG_ALTIVEC */
  1025. #ifdef CONFIG_PPC_PSERIES
  1026. /*
  1027. * Check for an SMT capable CPU and set the CPU feature. We do
  1028. * this by looking at the size of the ibm,ppc-interrupt-server#s
  1029. * property
  1030. */
  1031. prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
  1032. &size);
  1033. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  1034. if (prop && ((size / sizeof(u32)) > 1))
  1035. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  1036. #endif
  1037. return 0;
  1038. }
  1039. static int __init early_init_dt_scan_chosen(unsigned long node,
  1040. const char *uname, int depth, void *data)
  1041. {
  1042. u32 *prop;
  1043. unsigned long *lprop;
  1044. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  1045. if (depth != 1 ||
  1046. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  1047. return 0;
  1048. /* get platform type */
  1049. prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
  1050. if (prop == NULL)
  1051. return 0;
  1052. #ifdef CONFIG_PPC64
  1053. systemcfg->platform = *prop;
  1054. #else
  1055. #ifdef CONFIG_PPC_MULTIPLATFORM
  1056. _machine = *prop;
  1057. #endif
  1058. #endif
  1059. #ifdef CONFIG_PPC64
  1060. /* check if iommu is forced on or off */
  1061. if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  1062. iommu_is_off = 1;
  1063. if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  1064. iommu_force_on = 1;
  1065. #endif
  1066. lprop = get_flat_dt_prop(node, "linux,memory-limit", NULL);
  1067. if (lprop)
  1068. memory_limit = *lprop;
  1069. #ifdef CONFIG_PPC64
  1070. lprop = get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  1071. if (lprop)
  1072. tce_alloc_start = *lprop;
  1073. lprop = get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  1074. if (lprop)
  1075. tce_alloc_end = *lprop;
  1076. #endif
  1077. #ifdef CONFIG_PPC_RTAS
  1078. /* To help early debugging via the front panel, we retreive a minimal
  1079. * set of RTAS infos now if available
  1080. */
  1081. {
  1082. u64 *basep, *entryp;
  1083. basep = get_flat_dt_prop(node, "linux,rtas-base", NULL);
  1084. entryp = get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  1085. prop = get_flat_dt_prop(node, "linux,rtas-size", NULL);
  1086. if (basep && entryp && prop) {
  1087. rtas.base = *basep;
  1088. rtas.entry = *entryp;
  1089. rtas.size = *prop;
  1090. }
  1091. }
  1092. #endif /* CONFIG_PPC_RTAS */
  1093. /* break now */
  1094. return 1;
  1095. }
  1096. static int __init early_init_dt_scan_root(unsigned long node,
  1097. const char *uname, int depth, void *data)
  1098. {
  1099. u32 *prop;
  1100. if (depth != 0)
  1101. return 0;
  1102. prop = get_flat_dt_prop(node, "#size-cells", NULL);
  1103. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  1104. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  1105. prop = get_flat_dt_prop(node, "#address-cells", NULL);
  1106. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  1107. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  1108. /* break now */
  1109. return 1;
  1110. }
  1111. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  1112. {
  1113. cell_t *p = *cellp;
  1114. unsigned long r;
  1115. /* Ignore more than 2 cells */
  1116. while (s > sizeof(unsigned long) / 4) {
  1117. p++;
  1118. s--;
  1119. }
  1120. r = *p++;
  1121. #ifdef CONFIG_PPC64
  1122. if (s > 1) {
  1123. r <<= 32;
  1124. r |= *(p++);
  1125. s--;
  1126. }
  1127. #endif
  1128. *cellp = p;
  1129. return r;
  1130. }
  1131. static int __init early_init_dt_scan_memory(unsigned long node,
  1132. const char *uname, int depth, void *data)
  1133. {
  1134. char *type = get_flat_dt_prop(node, "device_type", NULL);
  1135. cell_t *reg, *endp;
  1136. unsigned long l;
  1137. /* We are scanning "memory" nodes only */
  1138. if (type == NULL || strcmp(type, "memory") != 0)
  1139. return 0;
  1140. reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
  1141. if (reg == NULL)
  1142. return 0;
  1143. endp = reg + (l / sizeof(cell_t));
  1144. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  1145. uname, l, reg[0], reg[1], reg[2], reg[3]);
  1146. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  1147. unsigned long base, size;
  1148. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  1149. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  1150. if (size == 0)
  1151. continue;
  1152. DBG(" - %lx , %lx\n", base, size);
  1153. #ifdef CONFIG_PPC64
  1154. if (iommu_is_off) {
  1155. if (base >= 0x80000000ul)
  1156. continue;
  1157. if ((base + size) > 0x80000000ul)
  1158. size = 0x80000000ul - base;
  1159. }
  1160. #endif
  1161. lmb_add(base, size);
  1162. }
  1163. return 0;
  1164. }
  1165. static void __init early_reserve_mem(void)
  1166. {
  1167. unsigned long base, size;
  1168. unsigned long *reserve_map;
  1169. reserve_map = (unsigned long *)(((unsigned long)initial_boot_params) +
  1170. initial_boot_params->off_mem_rsvmap);
  1171. while (1) {
  1172. base = *(reserve_map++);
  1173. size = *(reserve_map++);
  1174. if (size == 0)
  1175. break;
  1176. DBG("reserving: %lx -> %lx\n", base, size);
  1177. lmb_reserve(base, size);
  1178. }
  1179. #if 0
  1180. DBG("memory reserved, lmbs :\n");
  1181. lmb_dump_all();
  1182. #endif
  1183. }
  1184. void __init early_init_devtree(void *params)
  1185. {
  1186. DBG(" -> early_init_devtree()\n");
  1187. /* Setup flat device-tree pointer */
  1188. initial_boot_params = params;
  1189. /* Retrieve various informations from the /chosen node of the
  1190. * device-tree, including the platform type, initrd location and
  1191. * size, TCE reserve, and more ...
  1192. */
  1193. scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1194. /* Scan memory nodes and rebuild LMBs */
  1195. lmb_init();
  1196. scan_flat_dt(early_init_dt_scan_root, NULL);
  1197. scan_flat_dt(early_init_dt_scan_memory, NULL);
  1198. lmb_enforce_memory_limit(memory_limit);
  1199. lmb_analyze();
  1200. #ifdef CONFIG_PPC64
  1201. systemcfg->physicalMemorySize = lmb_phys_mem_size();
  1202. #endif
  1203. lmb_reserve(0, __pa(klimit));
  1204. DBG("Phys. mem: %lx\n", lmb_phys_mem_size());
  1205. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1206. early_reserve_mem();
  1207. DBG("Scanning CPUs ...\n");
  1208. /* Retreive hash table size from flattened tree plus other
  1209. * CPU related informations (altivec support, boot CPU ID, ...)
  1210. */
  1211. scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1212. DBG(" <- early_init_devtree()\n");
  1213. }
  1214. #undef printk
  1215. int
  1216. prom_n_addr_cells(struct device_node* np)
  1217. {
  1218. int* ip;
  1219. do {
  1220. if (np->parent)
  1221. np = np->parent;
  1222. ip = (int *) get_property(np, "#address-cells", NULL);
  1223. if (ip != NULL)
  1224. return *ip;
  1225. } while (np->parent);
  1226. /* No #address-cells property for the root node, default to 1 */
  1227. return 1;
  1228. }
  1229. int
  1230. prom_n_size_cells(struct device_node* np)
  1231. {
  1232. int* ip;
  1233. do {
  1234. if (np->parent)
  1235. np = np->parent;
  1236. ip = (int *) get_property(np, "#size-cells", NULL);
  1237. if (ip != NULL)
  1238. return *ip;
  1239. } while (np->parent);
  1240. /* No #size-cells property for the root node, default to 1 */
  1241. return 1;
  1242. }
  1243. /**
  1244. * Work out the sense (active-low level / active-high edge)
  1245. * of each interrupt from the device tree.
  1246. */
  1247. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1248. {
  1249. struct device_node *np;
  1250. int i, j;
  1251. /* default to level-triggered */
  1252. memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
  1253. for (np = allnodes; np != 0; np = np->allnext) {
  1254. for (j = 0; j < np->n_intrs; j++) {
  1255. i = np->intrs[j].line;
  1256. if (i >= off && i < max)
  1257. senses[i-off] = np->intrs[j].sense;
  1258. }
  1259. }
  1260. }
  1261. /**
  1262. * Construct and return a list of the device_nodes with a given name.
  1263. */
  1264. struct device_node *find_devices(const char *name)
  1265. {
  1266. struct device_node *head, **prevp, *np;
  1267. prevp = &head;
  1268. for (np = allnodes; np != 0; np = np->allnext) {
  1269. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1270. *prevp = np;
  1271. prevp = &np->next;
  1272. }
  1273. }
  1274. *prevp = NULL;
  1275. return head;
  1276. }
  1277. EXPORT_SYMBOL(find_devices);
  1278. /**
  1279. * Construct and return a list of the device_nodes with a given type.
  1280. */
  1281. struct device_node *find_type_devices(const char *type)
  1282. {
  1283. struct device_node *head, **prevp, *np;
  1284. prevp = &head;
  1285. for (np = allnodes; np != 0; np = np->allnext) {
  1286. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1287. *prevp = np;
  1288. prevp = &np->next;
  1289. }
  1290. }
  1291. *prevp = NULL;
  1292. return head;
  1293. }
  1294. EXPORT_SYMBOL(find_type_devices);
  1295. /**
  1296. * Returns all nodes linked together
  1297. */
  1298. struct device_node *find_all_nodes(void)
  1299. {
  1300. struct device_node *head, **prevp, *np;
  1301. prevp = &head;
  1302. for (np = allnodes; np != 0; np = np->allnext) {
  1303. *prevp = np;
  1304. prevp = &np->next;
  1305. }
  1306. *prevp = NULL;
  1307. return head;
  1308. }
  1309. EXPORT_SYMBOL(find_all_nodes);
  1310. /** Checks if the given "compat" string matches one of the strings in
  1311. * the device's "compatible" property
  1312. */
  1313. int device_is_compatible(struct device_node *device, const char *compat)
  1314. {
  1315. const char* cp;
  1316. int cplen, l;
  1317. cp = (char *) get_property(device, "compatible", &cplen);
  1318. if (cp == NULL)
  1319. return 0;
  1320. while (cplen > 0) {
  1321. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1322. return 1;
  1323. l = strlen(cp) + 1;
  1324. cp += l;
  1325. cplen -= l;
  1326. }
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL(device_is_compatible);
  1330. /**
  1331. * Indicates whether the root node has a given value in its
  1332. * compatible property.
  1333. */
  1334. int machine_is_compatible(const char *compat)
  1335. {
  1336. struct device_node *root;
  1337. int rc = 0;
  1338. root = of_find_node_by_path("/");
  1339. if (root) {
  1340. rc = device_is_compatible(root, compat);
  1341. of_node_put(root);
  1342. }
  1343. return rc;
  1344. }
  1345. EXPORT_SYMBOL(machine_is_compatible);
  1346. /**
  1347. * Construct and return a list of the device_nodes with a given type
  1348. * and compatible property.
  1349. */
  1350. struct device_node *find_compatible_devices(const char *type,
  1351. const char *compat)
  1352. {
  1353. struct device_node *head, **prevp, *np;
  1354. prevp = &head;
  1355. for (np = allnodes; np != 0; np = np->allnext) {
  1356. if (type != NULL
  1357. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1358. continue;
  1359. if (device_is_compatible(np, compat)) {
  1360. *prevp = np;
  1361. prevp = &np->next;
  1362. }
  1363. }
  1364. *prevp = NULL;
  1365. return head;
  1366. }
  1367. EXPORT_SYMBOL(find_compatible_devices);
  1368. /**
  1369. * Find the device_node with a given full_name.
  1370. */
  1371. struct device_node *find_path_device(const char *path)
  1372. {
  1373. struct device_node *np;
  1374. for (np = allnodes; np != 0; np = np->allnext)
  1375. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1376. return np;
  1377. return NULL;
  1378. }
  1379. EXPORT_SYMBOL(find_path_device);
  1380. /*******
  1381. *
  1382. * New implementation of the OF "find" APIs, return a refcounted
  1383. * object, call of_node_put() when done. The device tree and list
  1384. * are protected by a rw_lock.
  1385. *
  1386. * Note that property management will need some locking as well,
  1387. * this isn't dealt with yet.
  1388. *
  1389. *******/
  1390. /**
  1391. * of_find_node_by_name - Find a node by its "name" property
  1392. * @from: The node to start searching from or NULL, the node
  1393. * you pass will not be searched, only the next one
  1394. * will; typically, you pass what the previous call
  1395. * returned. of_node_put() will be called on it
  1396. * @name: The name string to match against
  1397. *
  1398. * Returns a node pointer with refcount incremented, use
  1399. * of_node_put() on it when done.
  1400. */
  1401. struct device_node *of_find_node_by_name(struct device_node *from,
  1402. const char *name)
  1403. {
  1404. struct device_node *np;
  1405. read_lock(&devtree_lock);
  1406. np = from ? from->allnext : allnodes;
  1407. for (; np != 0; np = np->allnext)
  1408. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1409. && of_node_get(np))
  1410. break;
  1411. if (from)
  1412. of_node_put(from);
  1413. read_unlock(&devtree_lock);
  1414. return np;
  1415. }
  1416. EXPORT_SYMBOL(of_find_node_by_name);
  1417. /**
  1418. * of_find_node_by_type - Find a node by its "device_type" property
  1419. * @from: The node to start searching from or NULL, the node
  1420. * you pass will not be searched, only the next one
  1421. * will; typically, you pass what the previous call
  1422. * returned. of_node_put() will be called on it
  1423. * @name: The type string to match against
  1424. *
  1425. * Returns a node pointer with refcount incremented, use
  1426. * of_node_put() on it when done.
  1427. */
  1428. struct device_node *of_find_node_by_type(struct device_node *from,
  1429. const char *type)
  1430. {
  1431. struct device_node *np;
  1432. read_lock(&devtree_lock);
  1433. np = from ? from->allnext : allnodes;
  1434. for (; np != 0; np = np->allnext)
  1435. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1436. && of_node_get(np))
  1437. break;
  1438. if (from)
  1439. of_node_put(from);
  1440. read_unlock(&devtree_lock);
  1441. return np;
  1442. }
  1443. EXPORT_SYMBOL(of_find_node_by_type);
  1444. /**
  1445. * of_find_compatible_node - Find a node based on type and one of the
  1446. * tokens in its "compatible" property
  1447. * @from: The node to start searching from or NULL, the node
  1448. * you pass will not be searched, only the next one
  1449. * will; typically, you pass what the previous call
  1450. * returned. of_node_put() will be called on it
  1451. * @type: The type string to match "device_type" or NULL to ignore
  1452. * @compatible: The string to match to one of the tokens in the device
  1453. * "compatible" list.
  1454. *
  1455. * Returns a node pointer with refcount incremented, use
  1456. * of_node_put() on it when done.
  1457. */
  1458. struct device_node *of_find_compatible_node(struct device_node *from,
  1459. const char *type, const char *compatible)
  1460. {
  1461. struct device_node *np;
  1462. read_lock(&devtree_lock);
  1463. np = from ? from->allnext : allnodes;
  1464. for (; np != 0; np = np->allnext) {
  1465. if (type != NULL
  1466. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1467. continue;
  1468. if (device_is_compatible(np, compatible) && of_node_get(np))
  1469. break;
  1470. }
  1471. if (from)
  1472. of_node_put(from);
  1473. read_unlock(&devtree_lock);
  1474. return np;
  1475. }
  1476. EXPORT_SYMBOL(of_find_compatible_node);
  1477. /**
  1478. * of_find_node_by_path - Find a node matching a full OF path
  1479. * @path: The full path to match
  1480. *
  1481. * Returns a node pointer with refcount incremented, use
  1482. * of_node_put() on it when done.
  1483. */
  1484. struct device_node *of_find_node_by_path(const char *path)
  1485. {
  1486. struct device_node *np = allnodes;
  1487. read_lock(&devtree_lock);
  1488. for (; np != 0; np = np->allnext) {
  1489. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1490. && of_node_get(np))
  1491. break;
  1492. }
  1493. read_unlock(&devtree_lock);
  1494. return np;
  1495. }
  1496. EXPORT_SYMBOL(of_find_node_by_path);
  1497. /**
  1498. * of_find_node_by_phandle - Find a node given a phandle
  1499. * @handle: phandle of the node to find
  1500. *
  1501. * Returns a node pointer with refcount incremented, use
  1502. * of_node_put() on it when done.
  1503. */
  1504. struct device_node *of_find_node_by_phandle(phandle handle)
  1505. {
  1506. struct device_node *np;
  1507. read_lock(&devtree_lock);
  1508. for (np = allnodes; np != 0; np = np->allnext)
  1509. if (np->linux_phandle == handle)
  1510. break;
  1511. if (np)
  1512. of_node_get(np);
  1513. read_unlock(&devtree_lock);
  1514. return np;
  1515. }
  1516. EXPORT_SYMBOL(of_find_node_by_phandle);
  1517. /**
  1518. * of_find_all_nodes - Get next node in global list
  1519. * @prev: Previous node or NULL to start iteration
  1520. * of_node_put() will be called on it
  1521. *
  1522. * Returns a node pointer with refcount incremented, use
  1523. * of_node_put() on it when done.
  1524. */
  1525. struct device_node *of_find_all_nodes(struct device_node *prev)
  1526. {
  1527. struct device_node *np;
  1528. read_lock(&devtree_lock);
  1529. np = prev ? prev->allnext : allnodes;
  1530. for (; np != 0; np = np->allnext)
  1531. if (of_node_get(np))
  1532. break;
  1533. if (prev)
  1534. of_node_put(prev);
  1535. read_unlock(&devtree_lock);
  1536. return np;
  1537. }
  1538. EXPORT_SYMBOL(of_find_all_nodes);
  1539. /**
  1540. * of_get_parent - Get a node's parent if any
  1541. * @node: Node to get parent
  1542. *
  1543. * Returns a node pointer with refcount incremented, use
  1544. * of_node_put() on it when done.
  1545. */
  1546. struct device_node *of_get_parent(const struct device_node *node)
  1547. {
  1548. struct device_node *np;
  1549. if (!node)
  1550. return NULL;
  1551. read_lock(&devtree_lock);
  1552. np = of_node_get(node->parent);
  1553. read_unlock(&devtree_lock);
  1554. return np;
  1555. }
  1556. EXPORT_SYMBOL(of_get_parent);
  1557. /**
  1558. * of_get_next_child - Iterate a node childs
  1559. * @node: parent node
  1560. * @prev: previous child of the parent node, or NULL to get first
  1561. *
  1562. * Returns a node pointer with refcount incremented, use
  1563. * of_node_put() on it when done.
  1564. */
  1565. struct device_node *of_get_next_child(const struct device_node *node,
  1566. struct device_node *prev)
  1567. {
  1568. struct device_node *next;
  1569. read_lock(&devtree_lock);
  1570. next = prev ? prev->sibling : node->child;
  1571. for (; next != 0; next = next->sibling)
  1572. if (of_node_get(next))
  1573. break;
  1574. if (prev)
  1575. of_node_put(prev);
  1576. read_unlock(&devtree_lock);
  1577. return next;
  1578. }
  1579. EXPORT_SYMBOL(of_get_next_child);
  1580. /**
  1581. * of_node_get - Increment refcount of a node
  1582. * @node: Node to inc refcount, NULL is supported to
  1583. * simplify writing of callers
  1584. *
  1585. * Returns node.
  1586. */
  1587. struct device_node *of_node_get(struct device_node *node)
  1588. {
  1589. if (node)
  1590. kref_get(&node->kref);
  1591. return node;
  1592. }
  1593. EXPORT_SYMBOL(of_node_get);
  1594. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1595. {
  1596. return container_of(kref, struct device_node, kref);
  1597. }
  1598. /**
  1599. * of_node_release - release a dynamically allocated node
  1600. * @kref: kref element of the node to be released
  1601. *
  1602. * In of_node_put() this function is passed to kref_put()
  1603. * as the destructor.
  1604. */
  1605. static void of_node_release(struct kref *kref)
  1606. {
  1607. struct device_node *node = kref_to_device_node(kref);
  1608. struct property *prop = node->properties;
  1609. if (!OF_IS_DYNAMIC(node))
  1610. return;
  1611. while (prop) {
  1612. struct property *next = prop->next;
  1613. kfree(prop->name);
  1614. kfree(prop->value);
  1615. kfree(prop);
  1616. prop = next;
  1617. }
  1618. kfree(node->intrs);
  1619. kfree(node->addrs);
  1620. kfree(node->full_name);
  1621. kfree(node->data);
  1622. kfree(node);
  1623. }
  1624. /**
  1625. * of_node_put - Decrement refcount of a node
  1626. * @node: Node to dec refcount, NULL is supported to
  1627. * simplify writing of callers
  1628. *
  1629. */
  1630. void of_node_put(struct device_node *node)
  1631. {
  1632. if (node)
  1633. kref_put(&node->kref, of_node_release);
  1634. }
  1635. EXPORT_SYMBOL(of_node_put);
  1636. /*
  1637. * Plug a device node into the tree and global list.
  1638. */
  1639. void of_attach_node(struct device_node *np)
  1640. {
  1641. write_lock(&devtree_lock);
  1642. np->sibling = np->parent->child;
  1643. np->allnext = allnodes;
  1644. np->parent->child = np;
  1645. allnodes = np;
  1646. write_unlock(&devtree_lock);
  1647. }
  1648. /*
  1649. * "Unplug" a node from the device tree. The caller must hold
  1650. * a reference to the node. The memory associated with the node
  1651. * is not freed until its refcount goes to zero.
  1652. */
  1653. void of_detach_node(const struct device_node *np)
  1654. {
  1655. struct device_node *parent;
  1656. write_lock(&devtree_lock);
  1657. parent = np->parent;
  1658. if (allnodes == np)
  1659. allnodes = np->allnext;
  1660. else {
  1661. struct device_node *prev;
  1662. for (prev = allnodes;
  1663. prev->allnext != np;
  1664. prev = prev->allnext)
  1665. ;
  1666. prev->allnext = np->allnext;
  1667. }
  1668. if (parent->child == np)
  1669. parent->child = np->sibling;
  1670. else {
  1671. struct device_node *prevsib;
  1672. for (prevsib = np->parent->child;
  1673. prevsib->sibling != np;
  1674. prevsib = prevsib->sibling)
  1675. ;
  1676. prevsib->sibling = np->sibling;
  1677. }
  1678. write_unlock(&devtree_lock);
  1679. }
  1680. #ifdef CONFIG_PPC_PSERIES
  1681. /*
  1682. * Fix up the uninitialized fields in a new device node:
  1683. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1684. *
  1685. * A lot of boot-time code is duplicated here, because functions such
  1686. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1687. * slab allocator.
  1688. *
  1689. * This should probably be split up into smaller chunks.
  1690. */
  1691. static int of_finish_dynamic_node(struct device_node *node,
  1692. unsigned long *unused1, int unused2,
  1693. int unused3, int unused4)
  1694. {
  1695. struct device_node *parent = of_get_parent(node);
  1696. int err = 0;
  1697. phandle *ibm_phandle;
  1698. node->name = get_property(node, "name", NULL);
  1699. node->type = get_property(node, "device_type", NULL);
  1700. if (!parent) {
  1701. err = -ENODEV;
  1702. goto out;
  1703. }
  1704. /* We don't support that function on PowerMac, at least
  1705. * not yet
  1706. */
  1707. if (systemcfg->platform == PLATFORM_POWERMAC)
  1708. return -ENODEV;
  1709. /* fix up new node's linux_phandle field */
  1710. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1711. node->linux_phandle = *ibm_phandle;
  1712. out:
  1713. of_node_put(parent);
  1714. return err;
  1715. }
  1716. static int prom_reconfig_notifier(struct notifier_block *nb,
  1717. unsigned long action, void *node)
  1718. {
  1719. int err;
  1720. switch (action) {
  1721. case PSERIES_RECONFIG_ADD:
  1722. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1723. if (err < 0) {
  1724. printk(KERN_ERR "finish_node returned %d\n", err);
  1725. err = NOTIFY_BAD;
  1726. }
  1727. break;
  1728. default:
  1729. err = NOTIFY_DONE;
  1730. break;
  1731. }
  1732. return err;
  1733. }
  1734. static struct notifier_block prom_reconfig_nb = {
  1735. .notifier_call = prom_reconfig_notifier,
  1736. .priority = 10, /* This one needs to run first */
  1737. };
  1738. static int __init prom_reconfig_setup(void)
  1739. {
  1740. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1741. }
  1742. __initcall(prom_reconfig_setup);
  1743. #endif
  1744. /*
  1745. * Find a property with a given name for a given node
  1746. * and return the value.
  1747. */
  1748. unsigned char *get_property(struct device_node *np, const char *name,
  1749. int *lenp)
  1750. {
  1751. struct property *pp;
  1752. for (pp = np->properties; pp != 0; pp = pp->next)
  1753. if (strcmp(pp->name, name) == 0) {
  1754. if (lenp != 0)
  1755. *lenp = pp->length;
  1756. return pp->value;
  1757. }
  1758. return NULL;
  1759. }
  1760. EXPORT_SYMBOL(get_property);
  1761. /*
  1762. * Add a property to a node
  1763. */
  1764. void prom_add_property(struct device_node* np, struct property* prop)
  1765. {
  1766. struct property **next = &np->properties;
  1767. prop->next = NULL;
  1768. while (*next)
  1769. next = &(*next)->next;
  1770. *next = prop;
  1771. }
  1772. /* I quickly hacked that one, check against spec ! */
  1773. static inline unsigned long
  1774. bus_space_to_resource_flags(unsigned int bus_space)
  1775. {
  1776. u8 space = (bus_space >> 24) & 0xf;
  1777. if (space == 0)
  1778. space = 0x02;
  1779. if (space == 0x02)
  1780. return IORESOURCE_MEM;
  1781. else if (space == 0x01)
  1782. return IORESOURCE_IO;
  1783. else {
  1784. printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
  1785. bus_space);
  1786. return 0;
  1787. }
  1788. }
  1789. #ifdef CONFIG_PCI
  1790. static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
  1791. struct address_range *range)
  1792. {
  1793. unsigned long mask;
  1794. int i;
  1795. /* Check this one */
  1796. mask = bus_space_to_resource_flags(range->space);
  1797. for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
  1798. if ((pdev->resource[i].flags & mask) == mask &&
  1799. pdev->resource[i].start <= range->address &&
  1800. pdev->resource[i].end > range->address) {
  1801. if ((range->address + range->size - 1) > pdev->resource[i].end) {
  1802. /* Add better message */
  1803. printk(KERN_WARNING "PCI/OF resource overlap !\n");
  1804. return NULL;
  1805. }
  1806. break;
  1807. }
  1808. }
  1809. if (i == DEVICE_COUNT_RESOURCE)
  1810. return NULL;
  1811. return &pdev->resource[i];
  1812. }
  1813. /*
  1814. * Request an OF device resource. Currently handles child of PCI devices,
  1815. * or other nodes attached to the root node. Ultimately, put some
  1816. * link to resources in the OF node.
  1817. */
  1818. struct resource *request_OF_resource(struct device_node* node, int index,
  1819. const char* name_postfix)
  1820. {
  1821. struct pci_dev* pcidev;
  1822. u8 pci_bus, pci_devfn;
  1823. unsigned long iomask;
  1824. struct device_node* nd;
  1825. struct resource* parent;
  1826. struct resource *res = NULL;
  1827. int nlen, plen;
  1828. if (index >= node->n_addrs)
  1829. goto fail;
  1830. /* Sanity check on bus space */
  1831. iomask = bus_space_to_resource_flags(node->addrs[index].space);
  1832. if (iomask & IORESOURCE_MEM)
  1833. parent = &iomem_resource;
  1834. else if (iomask & IORESOURCE_IO)
  1835. parent = &ioport_resource;
  1836. else
  1837. goto fail;
  1838. /* Find a PCI parent if any */
  1839. nd = node;
  1840. pcidev = NULL;
  1841. while (nd) {
  1842. if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
  1843. pcidev = pci_find_slot(pci_bus, pci_devfn);
  1844. if (pcidev) break;
  1845. nd = nd->parent;
  1846. }
  1847. if (pcidev)
  1848. parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
  1849. if (!parent) {
  1850. printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
  1851. node->name);
  1852. goto fail;
  1853. }
  1854. res = __request_region(parent, node->addrs[index].address,
  1855. node->addrs[index].size, NULL);
  1856. if (!res)
  1857. goto fail;
  1858. nlen = strlen(node->name);
  1859. plen = name_postfix ? strlen(name_postfix) : 0;
  1860. res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
  1861. if (res->name) {
  1862. strcpy((char *)res->name, node->name);
  1863. if (plen)
  1864. strcpy((char *)res->name+nlen, name_postfix);
  1865. }
  1866. return res;
  1867. fail:
  1868. return NULL;
  1869. }
  1870. EXPORT_SYMBOL(request_OF_resource);
  1871. int release_OF_resource(struct device_node *node, int index)
  1872. {
  1873. struct pci_dev* pcidev;
  1874. u8 pci_bus, pci_devfn;
  1875. unsigned long iomask, start, end;
  1876. struct device_node* nd;
  1877. struct resource* parent;
  1878. struct resource *res = NULL;
  1879. if (index >= node->n_addrs)
  1880. return -EINVAL;
  1881. /* Sanity check on bus space */
  1882. iomask = bus_space_to_resource_flags(node->addrs[index].space);
  1883. if (iomask & IORESOURCE_MEM)
  1884. parent = &iomem_resource;
  1885. else if (iomask & IORESOURCE_IO)
  1886. parent = &ioport_resource;
  1887. else
  1888. return -EINVAL;
  1889. /* Find a PCI parent if any */
  1890. nd = node;
  1891. pcidev = NULL;
  1892. while(nd) {
  1893. if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
  1894. pcidev = pci_find_slot(pci_bus, pci_devfn);
  1895. if (pcidev) break;
  1896. nd = nd->parent;
  1897. }
  1898. if (pcidev)
  1899. parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
  1900. if (!parent) {
  1901. printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
  1902. node->name);
  1903. return -ENODEV;
  1904. }
  1905. /* Find us in the parent and its childs */
  1906. res = parent->child;
  1907. start = node->addrs[index].address;
  1908. end = start + node->addrs[index].size - 1;
  1909. while (res) {
  1910. if (res->start == start && res->end == end &&
  1911. (res->flags & IORESOURCE_BUSY))
  1912. break;
  1913. if (res->start <= start && res->end >= end)
  1914. res = res->child;
  1915. else
  1916. res = res->sibling;
  1917. }
  1918. if (!res)
  1919. return -ENODEV;
  1920. if (res->name) {
  1921. kfree(res->name);
  1922. res->name = NULL;
  1923. }
  1924. release_resource(res);
  1925. kfree(res);
  1926. return 0;
  1927. }
  1928. EXPORT_SYMBOL(release_OF_resource);
  1929. #endif /* CONFIG_PCI */