prom.c 51 KB

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