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