prom.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  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. int
  1213. prom_n_size_cells(struct device_node* np)
  1214. {
  1215. int* ip;
  1216. do {
  1217. if (np->parent)
  1218. np = np->parent;
  1219. ip = (int *) get_property(np, "#size-cells", NULL);
  1220. if (ip != NULL)
  1221. return *ip;
  1222. } while (np->parent);
  1223. /* No #size-cells property for the root node, default to 1 */
  1224. return 1;
  1225. }
  1226. /**
  1227. * Work out the sense (active-low level / active-high edge)
  1228. * of each interrupt from the device tree.
  1229. */
  1230. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1231. {
  1232. struct device_node *np;
  1233. int i, j;
  1234. /* default to level-triggered */
  1235. memset(senses, IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE, max - off);
  1236. for (np = allnodes; np != 0; np = np->allnext) {
  1237. for (j = 0; j < np->n_intrs; j++) {
  1238. i = np->intrs[j].line;
  1239. if (i >= off && i < max)
  1240. senses[i-off] = np->intrs[j].sense;
  1241. }
  1242. }
  1243. }
  1244. /**
  1245. * Construct and return a list of the device_nodes with a given name.
  1246. */
  1247. struct device_node *find_devices(const char *name)
  1248. {
  1249. struct device_node *head, **prevp, *np;
  1250. prevp = &head;
  1251. for (np = allnodes; np != 0; np = np->allnext) {
  1252. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1253. *prevp = np;
  1254. prevp = &np->next;
  1255. }
  1256. }
  1257. *prevp = NULL;
  1258. return head;
  1259. }
  1260. EXPORT_SYMBOL(find_devices);
  1261. /**
  1262. * Construct and return a list of the device_nodes with a given type.
  1263. */
  1264. struct device_node *find_type_devices(const char *type)
  1265. {
  1266. struct device_node *head, **prevp, *np;
  1267. prevp = &head;
  1268. for (np = allnodes; np != 0; np = np->allnext) {
  1269. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1270. *prevp = np;
  1271. prevp = &np->next;
  1272. }
  1273. }
  1274. *prevp = NULL;
  1275. return head;
  1276. }
  1277. EXPORT_SYMBOL(find_type_devices);
  1278. /**
  1279. * Returns all nodes linked together
  1280. */
  1281. struct device_node *find_all_nodes(void)
  1282. {
  1283. struct device_node *head, **prevp, *np;
  1284. prevp = &head;
  1285. for (np = allnodes; np != 0; np = np->allnext) {
  1286. *prevp = np;
  1287. prevp = &np->next;
  1288. }
  1289. *prevp = NULL;
  1290. return head;
  1291. }
  1292. EXPORT_SYMBOL(find_all_nodes);
  1293. /** Checks if the given "compat" string matches one of the strings in
  1294. * the device's "compatible" property
  1295. */
  1296. int device_is_compatible(struct device_node *device, const char *compat)
  1297. {
  1298. const char* cp;
  1299. int cplen, l;
  1300. cp = (char *) get_property(device, "compatible", &cplen);
  1301. if (cp == NULL)
  1302. return 0;
  1303. while (cplen > 0) {
  1304. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1305. return 1;
  1306. l = strlen(cp) + 1;
  1307. cp += l;
  1308. cplen -= l;
  1309. }
  1310. return 0;
  1311. }
  1312. EXPORT_SYMBOL(device_is_compatible);
  1313. /**
  1314. * Indicates whether the root node has a given value in its
  1315. * compatible property.
  1316. */
  1317. int machine_is_compatible(const char *compat)
  1318. {
  1319. struct device_node *root;
  1320. int rc = 0;
  1321. root = of_find_node_by_path("/");
  1322. if (root) {
  1323. rc = device_is_compatible(root, compat);
  1324. of_node_put(root);
  1325. }
  1326. return rc;
  1327. }
  1328. EXPORT_SYMBOL(machine_is_compatible);
  1329. /**
  1330. * Construct and return a list of the device_nodes with a given type
  1331. * and compatible property.
  1332. */
  1333. struct device_node *find_compatible_devices(const char *type,
  1334. const char *compat)
  1335. {
  1336. struct device_node *head, **prevp, *np;
  1337. prevp = &head;
  1338. for (np = allnodes; np != 0; np = np->allnext) {
  1339. if (type != NULL
  1340. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1341. continue;
  1342. if (device_is_compatible(np, compat)) {
  1343. *prevp = np;
  1344. prevp = &np->next;
  1345. }
  1346. }
  1347. *prevp = NULL;
  1348. return head;
  1349. }
  1350. EXPORT_SYMBOL(find_compatible_devices);
  1351. /**
  1352. * Find the device_node with a given full_name.
  1353. */
  1354. struct device_node *find_path_device(const char *path)
  1355. {
  1356. struct device_node *np;
  1357. for (np = allnodes; np != 0; np = np->allnext)
  1358. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1359. return np;
  1360. return NULL;
  1361. }
  1362. EXPORT_SYMBOL(find_path_device);
  1363. /*******
  1364. *
  1365. * New implementation of the OF "find" APIs, return a refcounted
  1366. * object, call of_node_put() when done. The device tree and list
  1367. * are protected by a rw_lock.
  1368. *
  1369. * Note that property management will need some locking as well,
  1370. * this isn't dealt with yet.
  1371. *
  1372. *******/
  1373. /**
  1374. * of_find_node_by_name - Find a node by its "name" property
  1375. * @from: The node to start searching from or NULL, the node
  1376. * you pass will not be searched, only the next one
  1377. * will; typically, you pass what the previous call
  1378. * returned. of_node_put() will be called on it
  1379. * @name: The name string to match against
  1380. *
  1381. * Returns a node pointer with refcount incremented, use
  1382. * of_node_put() on it when done.
  1383. */
  1384. struct device_node *of_find_node_by_name(struct device_node *from,
  1385. const char *name)
  1386. {
  1387. struct device_node *np;
  1388. read_lock(&devtree_lock);
  1389. np = from ? from->allnext : allnodes;
  1390. for (; np != 0; np = np->allnext)
  1391. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1392. && of_node_get(np))
  1393. break;
  1394. if (from)
  1395. of_node_put(from);
  1396. read_unlock(&devtree_lock);
  1397. return np;
  1398. }
  1399. EXPORT_SYMBOL(of_find_node_by_name);
  1400. /**
  1401. * of_find_node_by_type - Find a node by its "device_type" property
  1402. * @from: The node to start searching from or NULL, the node
  1403. * you pass will not be searched, only the next one
  1404. * will; typically, you pass what the previous call
  1405. * returned. of_node_put() will be called on it
  1406. * @name: The type string to match against
  1407. *
  1408. * Returns a node pointer with refcount incremented, use
  1409. * of_node_put() on it when done.
  1410. */
  1411. struct device_node *of_find_node_by_type(struct device_node *from,
  1412. const char *type)
  1413. {
  1414. struct device_node *np;
  1415. read_lock(&devtree_lock);
  1416. np = from ? from->allnext : allnodes;
  1417. for (; np != 0; np = np->allnext)
  1418. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1419. && of_node_get(np))
  1420. break;
  1421. if (from)
  1422. of_node_put(from);
  1423. read_unlock(&devtree_lock);
  1424. return np;
  1425. }
  1426. EXPORT_SYMBOL(of_find_node_by_type);
  1427. /**
  1428. * of_find_compatible_node - Find a node based on type and one of the
  1429. * tokens in its "compatible" property
  1430. * @from: The node to start searching from or NULL, the node
  1431. * you pass will not be searched, only the next one
  1432. * will; typically, you pass what the previous call
  1433. * returned. of_node_put() will be called on it
  1434. * @type: The type string to match "device_type" or NULL to ignore
  1435. * @compatible: The string to match to one of the tokens in the device
  1436. * "compatible" list.
  1437. *
  1438. * Returns a node pointer with refcount incremented, use
  1439. * of_node_put() on it when done.
  1440. */
  1441. struct device_node *of_find_compatible_node(struct device_node *from,
  1442. const char *type, const char *compatible)
  1443. {
  1444. struct device_node *np;
  1445. read_lock(&devtree_lock);
  1446. np = from ? from->allnext : allnodes;
  1447. for (; np != 0; np = np->allnext) {
  1448. if (type != NULL
  1449. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1450. continue;
  1451. if (device_is_compatible(np, compatible) && of_node_get(np))
  1452. break;
  1453. }
  1454. if (from)
  1455. of_node_put(from);
  1456. read_unlock(&devtree_lock);
  1457. return np;
  1458. }
  1459. EXPORT_SYMBOL(of_find_compatible_node);
  1460. /**
  1461. * of_find_node_by_path - Find a node matching a full OF path
  1462. * @path: The full path to match
  1463. *
  1464. * Returns a node pointer with refcount incremented, use
  1465. * of_node_put() on it when done.
  1466. */
  1467. struct device_node *of_find_node_by_path(const char *path)
  1468. {
  1469. struct device_node *np = allnodes;
  1470. read_lock(&devtree_lock);
  1471. for (; np != 0; np = np->allnext) {
  1472. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1473. && of_node_get(np))
  1474. break;
  1475. }
  1476. read_unlock(&devtree_lock);
  1477. return np;
  1478. }
  1479. EXPORT_SYMBOL(of_find_node_by_path);
  1480. /**
  1481. * of_find_node_by_phandle - Find a node given a phandle
  1482. * @handle: phandle of the node to find
  1483. *
  1484. * Returns a node pointer with refcount incremented, use
  1485. * of_node_put() on it when done.
  1486. */
  1487. struct device_node *of_find_node_by_phandle(phandle handle)
  1488. {
  1489. struct device_node *np;
  1490. read_lock(&devtree_lock);
  1491. for (np = allnodes; np != 0; np = np->allnext)
  1492. if (np->linux_phandle == handle)
  1493. break;
  1494. if (np)
  1495. of_node_get(np);
  1496. read_unlock(&devtree_lock);
  1497. return np;
  1498. }
  1499. EXPORT_SYMBOL(of_find_node_by_phandle);
  1500. /**
  1501. * of_find_all_nodes - Get next node in global list
  1502. * @prev: Previous node or NULL to start iteration
  1503. * of_node_put() will be called on it
  1504. *
  1505. * Returns a node pointer with refcount incremented, use
  1506. * of_node_put() on it when done.
  1507. */
  1508. struct device_node *of_find_all_nodes(struct device_node *prev)
  1509. {
  1510. struct device_node *np;
  1511. read_lock(&devtree_lock);
  1512. np = prev ? prev->allnext : allnodes;
  1513. for (; np != 0; np = np->allnext)
  1514. if (of_node_get(np))
  1515. break;
  1516. if (prev)
  1517. of_node_put(prev);
  1518. read_unlock(&devtree_lock);
  1519. return np;
  1520. }
  1521. EXPORT_SYMBOL(of_find_all_nodes);
  1522. /**
  1523. * of_get_parent - Get a node's parent if any
  1524. * @node: Node to get parent
  1525. *
  1526. * Returns a node pointer with refcount incremented, use
  1527. * of_node_put() on it when done.
  1528. */
  1529. struct device_node *of_get_parent(const struct device_node *node)
  1530. {
  1531. struct device_node *np;
  1532. if (!node)
  1533. return NULL;
  1534. read_lock(&devtree_lock);
  1535. np = of_node_get(node->parent);
  1536. read_unlock(&devtree_lock);
  1537. return np;
  1538. }
  1539. EXPORT_SYMBOL(of_get_parent);
  1540. /**
  1541. * of_get_next_child - Iterate a node childs
  1542. * @node: parent node
  1543. * @prev: previous child of the parent node, or NULL to get first
  1544. *
  1545. * Returns a node pointer with refcount incremented, use
  1546. * of_node_put() on it when done.
  1547. */
  1548. struct device_node *of_get_next_child(const struct device_node *node,
  1549. struct device_node *prev)
  1550. {
  1551. struct device_node *next;
  1552. read_lock(&devtree_lock);
  1553. next = prev ? prev->sibling : node->child;
  1554. for (; next != 0; next = next->sibling)
  1555. if (of_node_get(next))
  1556. break;
  1557. if (prev)
  1558. of_node_put(prev);
  1559. read_unlock(&devtree_lock);
  1560. return next;
  1561. }
  1562. EXPORT_SYMBOL(of_get_next_child);
  1563. /**
  1564. * of_node_get - Increment refcount of a node
  1565. * @node: Node to inc refcount, NULL is supported to
  1566. * simplify writing of callers
  1567. *
  1568. * Returns node.
  1569. */
  1570. struct device_node *of_node_get(struct device_node *node)
  1571. {
  1572. if (node)
  1573. kref_get(&node->kref);
  1574. return node;
  1575. }
  1576. EXPORT_SYMBOL(of_node_get);
  1577. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1578. {
  1579. return container_of(kref, struct device_node, kref);
  1580. }
  1581. /**
  1582. * of_node_release - release a dynamically allocated node
  1583. * @kref: kref element of the node to be released
  1584. *
  1585. * In of_node_put() this function is passed to kref_put()
  1586. * as the destructor.
  1587. */
  1588. static void of_node_release(struct kref *kref)
  1589. {
  1590. struct device_node *node = kref_to_device_node(kref);
  1591. struct property *prop = node->properties;
  1592. if (!OF_IS_DYNAMIC(node))
  1593. return;
  1594. while (prop) {
  1595. struct property *next = prop->next;
  1596. kfree(prop->name);
  1597. kfree(prop->value);
  1598. kfree(prop);
  1599. prop = next;
  1600. }
  1601. kfree(node->intrs);
  1602. kfree(node->addrs);
  1603. kfree(node->full_name);
  1604. kfree(node->data);
  1605. kfree(node);
  1606. }
  1607. /**
  1608. * of_node_put - Decrement refcount of a node
  1609. * @node: Node to dec refcount, NULL is supported to
  1610. * simplify writing of callers
  1611. *
  1612. */
  1613. void of_node_put(struct device_node *node)
  1614. {
  1615. if (node)
  1616. kref_put(&node->kref, of_node_release);
  1617. }
  1618. EXPORT_SYMBOL(of_node_put);
  1619. /*
  1620. * Plug a device node into the tree and global list.
  1621. */
  1622. void of_attach_node(struct device_node *np)
  1623. {
  1624. write_lock(&devtree_lock);
  1625. np->sibling = np->parent->child;
  1626. np->allnext = allnodes;
  1627. np->parent->child = np;
  1628. allnodes = np;
  1629. write_unlock(&devtree_lock);
  1630. }
  1631. /*
  1632. * "Unplug" a node from the device tree. The caller must hold
  1633. * a reference to the node. The memory associated with the node
  1634. * is not freed until its refcount goes to zero.
  1635. */
  1636. void of_detach_node(const struct device_node *np)
  1637. {
  1638. struct device_node *parent;
  1639. write_lock(&devtree_lock);
  1640. parent = np->parent;
  1641. if (allnodes == np)
  1642. allnodes = np->allnext;
  1643. else {
  1644. struct device_node *prev;
  1645. for (prev = allnodes;
  1646. prev->allnext != np;
  1647. prev = prev->allnext)
  1648. ;
  1649. prev->allnext = np->allnext;
  1650. }
  1651. if (parent->child == np)
  1652. parent->child = np->sibling;
  1653. else {
  1654. struct device_node *prevsib;
  1655. for (prevsib = np->parent->child;
  1656. prevsib->sibling != np;
  1657. prevsib = prevsib->sibling)
  1658. ;
  1659. prevsib->sibling = np->sibling;
  1660. }
  1661. write_unlock(&devtree_lock);
  1662. }
  1663. #ifdef CONFIG_PPC_PSERIES
  1664. /*
  1665. * Fix up the uninitialized fields in a new device node:
  1666. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1667. *
  1668. * A lot of boot-time code is duplicated here, because functions such
  1669. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1670. * slab allocator.
  1671. *
  1672. * This should probably be split up into smaller chunks.
  1673. */
  1674. static int of_finish_dynamic_node(struct device_node *node,
  1675. unsigned long *unused1, int unused2,
  1676. int unused3, int unused4)
  1677. {
  1678. struct device_node *parent = of_get_parent(node);
  1679. int err = 0;
  1680. phandle *ibm_phandle;
  1681. node->name = get_property(node, "name", NULL);
  1682. node->type = get_property(node, "device_type", NULL);
  1683. if (!parent) {
  1684. err = -ENODEV;
  1685. goto out;
  1686. }
  1687. /* We don't support that function on PowerMac, at least
  1688. * not yet
  1689. */
  1690. if (_machine == PLATFORM_POWERMAC)
  1691. return -ENODEV;
  1692. /* fix up new node's linux_phandle field */
  1693. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1694. node->linux_phandle = *ibm_phandle;
  1695. out:
  1696. of_node_put(parent);
  1697. return err;
  1698. }
  1699. static int prom_reconfig_notifier(struct notifier_block *nb,
  1700. unsigned long action, void *node)
  1701. {
  1702. int err;
  1703. switch (action) {
  1704. case PSERIES_RECONFIG_ADD:
  1705. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1706. if (err < 0) {
  1707. printk(KERN_ERR "finish_node returned %d\n", err);
  1708. err = NOTIFY_BAD;
  1709. }
  1710. break;
  1711. default:
  1712. err = NOTIFY_DONE;
  1713. break;
  1714. }
  1715. return err;
  1716. }
  1717. static struct notifier_block prom_reconfig_nb = {
  1718. .notifier_call = prom_reconfig_notifier,
  1719. .priority = 10, /* This one needs to run first */
  1720. };
  1721. static int __init prom_reconfig_setup(void)
  1722. {
  1723. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1724. }
  1725. __initcall(prom_reconfig_setup);
  1726. #endif
  1727. /*
  1728. * Find a property with a given name for a given node
  1729. * and return the value.
  1730. */
  1731. unsigned char *get_property(struct device_node *np, const char *name,
  1732. int *lenp)
  1733. {
  1734. struct property *pp;
  1735. for (pp = np->properties; pp != 0; pp = pp->next)
  1736. if (strcmp(pp->name, name) == 0) {
  1737. if (lenp != 0)
  1738. *lenp = pp->length;
  1739. return pp->value;
  1740. }
  1741. return NULL;
  1742. }
  1743. EXPORT_SYMBOL(get_property);
  1744. /*
  1745. * Add a property to a node
  1746. */
  1747. int prom_add_property(struct device_node* np, struct property* prop)
  1748. {
  1749. struct property **next;
  1750. prop->next = NULL;
  1751. write_lock(&devtree_lock);
  1752. next = &np->properties;
  1753. while (*next) {
  1754. if (strcmp(prop->name, (*next)->name) == 0) {
  1755. /* duplicate ! don't insert it */
  1756. write_unlock(&devtree_lock);
  1757. return -1;
  1758. }
  1759. next = &(*next)->next;
  1760. }
  1761. *next = prop;
  1762. write_unlock(&devtree_lock);
  1763. #ifdef CONFIG_PROC_DEVICETREE
  1764. /* try to add to proc as well if it was initialized */
  1765. if (np->pde)
  1766. proc_device_tree_add_prop(np->pde, prop);
  1767. #endif /* CONFIG_PROC_DEVICETREE */
  1768. return 0;
  1769. }
  1770. /* I quickly hacked that one, check against spec ! */
  1771. static inline unsigned long
  1772. bus_space_to_resource_flags(unsigned int bus_space)
  1773. {
  1774. u8 space = (bus_space >> 24) & 0xf;
  1775. if (space == 0)
  1776. space = 0x02;
  1777. if (space == 0x02)
  1778. return IORESOURCE_MEM;
  1779. else if (space == 0x01)
  1780. return IORESOURCE_IO;
  1781. else {
  1782. printk(KERN_WARNING "prom.c: bus_space_to_resource_flags(), space: %x\n",
  1783. bus_space);
  1784. return 0;
  1785. }
  1786. }
  1787. #ifdef CONFIG_PCI
  1788. static struct resource *find_parent_pci_resource(struct pci_dev* pdev,
  1789. struct address_range *range)
  1790. {
  1791. unsigned long mask;
  1792. int i;
  1793. /* Check this one */
  1794. mask = bus_space_to_resource_flags(range->space);
  1795. for (i=0; i<DEVICE_COUNT_RESOURCE; i++) {
  1796. if ((pdev->resource[i].flags & mask) == mask &&
  1797. pdev->resource[i].start <= range->address &&
  1798. pdev->resource[i].end > range->address) {
  1799. if ((range->address + range->size - 1) > pdev->resource[i].end) {
  1800. /* Add better message */
  1801. printk(KERN_WARNING "PCI/OF resource overlap !\n");
  1802. return NULL;
  1803. }
  1804. break;
  1805. }
  1806. }
  1807. if (i == DEVICE_COUNT_RESOURCE)
  1808. return NULL;
  1809. return &pdev->resource[i];
  1810. }
  1811. /*
  1812. * Request an OF device resource. Currently handles child of PCI devices,
  1813. * or other nodes attached to the root node. Ultimately, put some
  1814. * link to resources in the OF node.
  1815. */
  1816. struct resource *request_OF_resource(struct device_node* node, int index,
  1817. const char* name_postfix)
  1818. {
  1819. struct pci_dev* pcidev;
  1820. u8 pci_bus, pci_devfn;
  1821. unsigned long iomask;
  1822. struct device_node* nd;
  1823. struct resource* parent;
  1824. struct resource *res = NULL;
  1825. int nlen, plen;
  1826. if (index >= node->n_addrs)
  1827. goto fail;
  1828. /* Sanity check on bus space */
  1829. iomask = bus_space_to_resource_flags(node->addrs[index].space);
  1830. if (iomask & IORESOURCE_MEM)
  1831. parent = &iomem_resource;
  1832. else if (iomask & IORESOURCE_IO)
  1833. parent = &ioport_resource;
  1834. else
  1835. goto fail;
  1836. /* Find a PCI parent if any */
  1837. nd = node;
  1838. pcidev = NULL;
  1839. while (nd) {
  1840. if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
  1841. pcidev = pci_find_slot(pci_bus, pci_devfn);
  1842. if (pcidev) break;
  1843. nd = nd->parent;
  1844. }
  1845. if (pcidev)
  1846. parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
  1847. if (!parent) {
  1848. printk(KERN_WARNING "request_OF_resource(%s), parent not found\n",
  1849. node->name);
  1850. goto fail;
  1851. }
  1852. res = __request_region(parent, node->addrs[index].address,
  1853. node->addrs[index].size, NULL);
  1854. if (!res)
  1855. goto fail;
  1856. nlen = strlen(node->name);
  1857. plen = name_postfix ? strlen(name_postfix) : 0;
  1858. res->name = (const char *)kmalloc(nlen+plen+1, GFP_KERNEL);
  1859. if (res->name) {
  1860. strcpy((char *)res->name, node->name);
  1861. if (plen)
  1862. strcpy((char *)res->name+nlen, name_postfix);
  1863. }
  1864. return res;
  1865. fail:
  1866. return NULL;
  1867. }
  1868. EXPORT_SYMBOL(request_OF_resource);
  1869. int release_OF_resource(struct device_node *node, int index)
  1870. {
  1871. struct pci_dev* pcidev;
  1872. u8 pci_bus, pci_devfn;
  1873. unsigned long iomask, start, end;
  1874. struct device_node* nd;
  1875. struct resource* parent;
  1876. struct resource *res = NULL;
  1877. if (index >= node->n_addrs)
  1878. return -EINVAL;
  1879. /* Sanity check on bus space */
  1880. iomask = bus_space_to_resource_flags(node->addrs[index].space);
  1881. if (iomask & IORESOURCE_MEM)
  1882. parent = &iomem_resource;
  1883. else if (iomask & IORESOURCE_IO)
  1884. parent = &ioport_resource;
  1885. else
  1886. return -EINVAL;
  1887. /* Find a PCI parent if any */
  1888. nd = node;
  1889. pcidev = NULL;
  1890. while(nd) {
  1891. if (!pci_device_from_OF_node(nd, &pci_bus, &pci_devfn))
  1892. pcidev = pci_find_slot(pci_bus, pci_devfn);
  1893. if (pcidev) break;
  1894. nd = nd->parent;
  1895. }
  1896. if (pcidev)
  1897. parent = find_parent_pci_resource(pcidev, &node->addrs[index]);
  1898. if (!parent) {
  1899. printk(KERN_WARNING "release_OF_resource(%s), parent not found\n",
  1900. node->name);
  1901. return -ENODEV;
  1902. }
  1903. /* Find us in the parent and its childs */
  1904. res = parent->child;
  1905. start = node->addrs[index].address;
  1906. end = start + node->addrs[index].size - 1;
  1907. while (res) {
  1908. if (res->start == start && res->end == end &&
  1909. (res->flags & IORESOURCE_BUSY))
  1910. break;
  1911. if (res->start <= start && res->end >= end)
  1912. res = res->child;
  1913. else
  1914. res = res->sibling;
  1915. }
  1916. if (!res)
  1917. return -ENODEV;
  1918. if (res->name) {
  1919. kfree(res->name);
  1920. res->name = NULL;
  1921. }
  1922. release_resource(res);
  1923. kfree(res);
  1924. return 0;
  1925. }
  1926. EXPORT_SYMBOL(release_OF_resource);
  1927. #endif /* CONFIG_PCI */