prom.c 52 KB

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