prom.c 44 KB

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