prom.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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. #ifdef CONFIG_ALTIVEC
  816. /* Check if we have a VMX and eventually update CPU features */
  817. prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
  818. if (prop && (*prop) > 0) {
  819. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  820. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  821. }
  822. /* Same goes for Apple's "altivec" property */
  823. prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
  824. if (prop) {
  825. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  826. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  827. }
  828. #endif /* CONFIG_ALTIVEC */
  829. /*
  830. * Check for an SMT capable CPU and set the CPU feature. We do
  831. * this by looking at the size of the ibm,ppc-interrupt-server#s
  832. * property
  833. */
  834. prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
  835. &size);
  836. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  837. if (prop && ((size / sizeof(u32)) > 1))
  838. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  839. return 0;
  840. }
  841. static int __init early_init_dt_scan_chosen(unsigned long node,
  842. const char *full_path, void *data)
  843. {
  844. u32 *prop;
  845. u64 *prop64;
  846. extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
  847. if (strcmp(full_path, "/chosen") != 0)
  848. return 0;
  849. /* get platform type */
  850. prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
  851. if (prop == NULL)
  852. return 0;
  853. systemcfg->platform = *prop;
  854. /* check if iommu is forced on or off */
  855. if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  856. iommu_is_off = 1;
  857. if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  858. iommu_force_on = 1;
  859. prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
  860. if (prop64)
  861. memory_limit = *prop64;
  862. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  863. if (prop64)
  864. tce_alloc_start = *prop64;
  865. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  866. if (prop64)
  867. tce_alloc_end = *prop64;
  868. #ifdef CONFIG_PPC_RTAS
  869. /* To help early debugging via the front panel, we retreive a minimal
  870. * set of RTAS infos now if available
  871. */
  872. {
  873. u64 *basep, *entryp;
  874. basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
  875. entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  876. prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
  877. if (basep && entryp && prop) {
  878. rtas.base = *basep;
  879. rtas.entry = *entryp;
  880. rtas.size = *prop;
  881. }
  882. }
  883. #endif /* CONFIG_PPC_RTAS */
  884. /* break now */
  885. return 1;
  886. }
  887. static int __init early_init_dt_scan_root(unsigned long node,
  888. const char *full_path, void *data)
  889. {
  890. u32 *prop;
  891. if (strcmp(full_path, "/") != 0)
  892. return 0;
  893. prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
  894. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  895. prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
  896. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  897. /* break now */
  898. return 1;
  899. }
  900. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  901. {
  902. cell_t *p = *cellp;
  903. unsigned long r = 0;
  904. /* Ignore more than 2 cells */
  905. while (s > 2) {
  906. p++;
  907. s--;
  908. }
  909. while (s) {
  910. r <<= 32;
  911. r |= *(p++);
  912. s--;
  913. }
  914. *cellp = p;
  915. return r;
  916. }
  917. static int __init early_init_dt_scan_memory(unsigned long node,
  918. const char *full_path, void *data)
  919. {
  920. char *type = get_flat_dt_prop(node, "device_type", NULL);
  921. cell_t *reg, *endp;
  922. unsigned long l;
  923. /* We are scanning "memory" nodes only */
  924. if (type == NULL || strcmp(type, "memory") != 0)
  925. return 0;
  926. reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
  927. if (reg == NULL)
  928. return 0;
  929. endp = reg + (l / sizeof(cell_t));
  930. DBG("memory scan node %s ...\n", full_path);
  931. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  932. unsigned long base, size;
  933. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  934. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  935. if (size == 0)
  936. continue;
  937. DBG(" - %lx , %lx\n", base, size);
  938. if (iommu_is_off) {
  939. if (base >= 0x80000000ul)
  940. continue;
  941. if ((base + size) > 0x80000000ul)
  942. size = 0x80000000ul - base;
  943. }
  944. lmb_add(base, size);
  945. }
  946. return 0;
  947. }
  948. static void __init early_reserve_mem(void)
  949. {
  950. u64 base, size;
  951. u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  952. initial_boot_params->off_mem_rsvmap);
  953. while (1) {
  954. base = *(reserve_map++);
  955. size = *(reserve_map++);
  956. if (size == 0)
  957. break;
  958. DBG("reserving: %lx -> %lx\n", base, size);
  959. lmb_reserve(base, size);
  960. }
  961. #if 0
  962. DBG("memory reserved, lmbs :\n");
  963. lmb_dump_all();
  964. #endif
  965. }
  966. void __init early_init_devtree(void *params)
  967. {
  968. DBG(" -> early_init_devtree()\n");
  969. /* Setup flat device-tree pointer */
  970. initial_boot_params = params;
  971. /* By default, hash size is not set */
  972. ppc64_pft_size = 0;
  973. /* Retreive various informations from the /chosen node of the
  974. * device-tree, including the platform type, initrd location and
  975. * size, TCE reserve, and more ...
  976. */
  977. scan_flat_dt(early_init_dt_scan_chosen, NULL);
  978. /* Scan memory nodes and rebuild LMBs */
  979. lmb_init();
  980. scan_flat_dt(early_init_dt_scan_root, NULL);
  981. scan_flat_dt(early_init_dt_scan_memory, NULL);
  982. lmb_enforce_memory_limit();
  983. lmb_analyze();
  984. systemcfg->physicalMemorySize = lmb_phys_mem_size();
  985. lmb_reserve(0, __pa(klimit));
  986. DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
  987. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  988. early_reserve_mem();
  989. DBG("Scanning CPUs ...\n");
  990. /* Retreive hash table size from flattened tree plus other
  991. * CPU related informations (altivec support, boot CPU ID, ...)
  992. */
  993. scan_flat_dt(early_init_dt_scan_cpus, NULL);
  994. /* If hash size wasn't obtained above, we calculate it now based on
  995. * the total RAM size
  996. */
  997. if (ppc64_pft_size == 0) {
  998. unsigned long rnd_mem_size, pteg_count;
  999. /* round mem_size up to next power of 2 */
  1000. rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
  1001. if (rnd_mem_size < systemcfg->physicalMemorySize)
  1002. rnd_mem_size <<= 1;
  1003. /* # pages / 2 */
  1004. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  1005. ppc64_pft_size = __ilog2(pteg_count << 7);
  1006. }
  1007. DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
  1008. DBG(" <- early_init_devtree()\n");
  1009. }
  1010. #undef printk
  1011. int
  1012. prom_n_addr_cells(struct device_node* np)
  1013. {
  1014. int* ip;
  1015. do {
  1016. if (np->parent)
  1017. np = np->parent;
  1018. ip = (int *) get_property(np, "#address-cells", NULL);
  1019. if (ip != NULL)
  1020. return *ip;
  1021. } while (np->parent);
  1022. /* No #address-cells property for the root node, default to 1 */
  1023. return 1;
  1024. }
  1025. int
  1026. prom_n_size_cells(struct device_node* np)
  1027. {
  1028. int* ip;
  1029. do {
  1030. if (np->parent)
  1031. np = np->parent;
  1032. ip = (int *) get_property(np, "#size-cells", NULL);
  1033. if (ip != NULL)
  1034. return *ip;
  1035. } while (np->parent);
  1036. /* No #size-cells property for the root node, default to 1 */
  1037. return 1;
  1038. }
  1039. /**
  1040. * Work out the sense (active-low level / active-high edge)
  1041. * of each interrupt from the device tree.
  1042. */
  1043. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1044. {
  1045. struct device_node *np;
  1046. int i, j;
  1047. /* default to level-triggered */
  1048. memset(senses, 1, max - off);
  1049. for (np = allnodes; np != 0; np = np->allnext) {
  1050. for (j = 0; j < np->n_intrs; j++) {
  1051. i = np->intrs[j].line;
  1052. if (i >= off && i < max)
  1053. senses[i-off] = np->intrs[j].sense ?
  1054. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
  1055. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
  1056. }
  1057. }
  1058. }
  1059. /**
  1060. * Construct and return a list of the device_nodes with a given name.
  1061. */
  1062. struct device_node *
  1063. find_devices(const char *name)
  1064. {
  1065. struct device_node *head, **prevp, *np;
  1066. prevp = &head;
  1067. for (np = allnodes; np != 0; np = np->allnext) {
  1068. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1069. *prevp = np;
  1070. prevp = &np->next;
  1071. }
  1072. }
  1073. *prevp = NULL;
  1074. return head;
  1075. }
  1076. EXPORT_SYMBOL(find_devices);
  1077. /**
  1078. * Construct and return a list of the device_nodes with a given type.
  1079. */
  1080. struct device_node *
  1081. find_type_devices(const char *type)
  1082. {
  1083. struct device_node *head, **prevp, *np;
  1084. prevp = &head;
  1085. for (np = allnodes; np != 0; np = np->allnext) {
  1086. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1087. *prevp = np;
  1088. prevp = &np->next;
  1089. }
  1090. }
  1091. *prevp = NULL;
  1092. return head;
  1093. }
  1094. EXPORT_SYMBOL(find_type_devices);
  1095. /**
  1096. * Returns all nodes linked together
  1097. */
  1098. struct device_node *
  1099. find_all_nodes(void)
  1100. {
  1101. struct device_node *head, **prevp, *np;
  1102. prevp = &head;
  1103. for (np = allnodes; np != 0; np = np->allnext) {
  1104. *prevp = np;
  1105. prevp = &np->next;
  1106. }
  1107. *prevp = NULL;
  1108. return head;
  1109. }
  1110. EXPORT_SYMBOL(find_all_nodes);
  1111. /** Checks if the given "compat" string matches one of the strings in
  1112. * the device's "compatible" property
  1113. */
  1114. int
  1115. device_is_compatible(struct device_node *device, const char *compat)
  1116. {
  1117. const char* cp;
  1118. int cplen, l;
  1119. cp = (char *) get_property(device, "compatible", &cplen);
  1120. if (cp == NULL)
  1121. return 0;
  1122. while (cplen > 0) {
  1123. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1124. return 1;
  1125. l = strlen(cp) + 1;
  1126. cp += l;
  1127. cplen -= l;
  1128. }
  1129. return 0;
  1130. }
  1131. EXPORT_SYMBOL(device_is_compatible);
  1132. /**
  1133. * Indicates whether the root node has a given value in its
  1134. * compatible property.
  1135. */
  1136. int
  1137. machine_is_compatible(const char *compat)
  1138. {
  1139. struct device_node *root;
  1140. int rc = 0;
  1141. root = of_find_node_by_path("/");
  1142. if (root) {
  1143. rc = device_is_compatible(root, compat);
  1144. of_node_put(root);
  1145. }
  1146. return rc;
  1147. }
  1148. EXPORT_SYMBOL(machine_is_compatible);
  1149. /**
  1150. * Construct and return a list of the device_nodes with a given type
  1151. * and compatible property.
  1152. */
  1153. struct device_node *
  1154. find_compatible_devices(const char *type, const char *compat)
  1155. {
  1156. struct device_node *head, **prevp, *np;
  1157. prevp = &head;
  1158. for (np = allnodes; np != 0; np = np->allnext) {
  1159. if (type != NULL
  1160. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1161. continue;
  1162. if (device_is_compatible(np, compat)) {
  1163. *prevp = np;
  1164. prevp = &np->next;
  1165. }
  1166. }
  1167. *prevp = NULL;
  1168. return head;
  1169. }
  1170. EXPORT_SYMBOL(find_compatible_devices);
  1171. /**
  1172. * Find the device_node with a given full_name.
  1173. */
  1174. struct device_node *
  1175. find_path_device(const char *path)
  1176. {
  1177. struct device_node *np;
  1178. for (np = allnodes; np != 0; np = np->allnext)
  1179. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1180. return np;
  1181. return NULL;
  1182. }
  1183. EXPORT_SYMBOL(find_path_device);
  1184. /*******
  1185. *
  1186. * New implementation of the OF "find" APIs, return a refcounted
  1187. * object, call of_node_put() when done. The device tree and list
  1188. * are protected by a rw_lock.
  1189. *
  1190. * Note that property management will need some locking as well,
  1191. * this isn't dealt with yet.
  1192. *
  1193. *******/
  1194. /**
  1195. * of_find_node_by_name - Find a node by its "name" property
  1196. * @from: The node to start searching from or NULL, the node
  1197. * you pass will not be searched, only the next one
  1198. * will; typically, you pass what the previous call
  1199. * returned. of_node_put() will be called on it
  1200. * @name: The name string to match against
  1201. *
  1202. * Returns a node pointer with refcount incremented, use
  1203. * of_node_put() on it when done.
  1204. */
  1205. struct device_node *of_find_node_by_name(struct device_node *from,
  1206. const char *name)
  1207. {
  1208. struct device_node *np;
  1209. read_lock(&devtree_lock);
  1210. np = from ? from->allnext : allnodes;
  1211. for (; np != 0; np = np->allnext)
  1212. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1213. && of_node_get(np))
  1214. break;
  1215. if (from)
  1216. of_node_put(from);
  1217. read_unlock(&devtree_lock);
  1218. return np;
  1219. }
  1220. EXPORT_SYMBOL(of_find_node_by_name);
  1221. /**
  1222. * of_find_node_by_type - Find a node by its "device_type" property
  1223. * @from: The node to start searching from or NULL, the node
  1224. * you pass will not be searched, only the next one
  1225. * will; typically, you pass what the previous call
  1226. * returned. of_node_put() will be called on it
  1227. * @name: The type string to match against
  1228. *
  1229. * Returns a node pointer with refcount incremented, use
  1230. * of_node_put() on it when done.
  1231. */
  1232. struct device_node *of_find_node_by_type(struct device_node *from,
  1233. const char *type)
  1234. {
  1235. struct device_node *np;
  1236. read_lock(&devtree_lock);
  1237. np = from ? from->allnext : allnodes;
  1238. for (; np != 0; np = np->allnext)
  1239. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1240. && of_node_get(np))
  1241. break;
  1242. if (from)
  1243. of_node_put(from);
  1244. read_unlock(&devtree_lock);
  1245. return np;
  1246. }
  1247. EXPORT_SYMBOL(of_find_node_by_type);
  1248. /**
  1249. * of_find_compatible_node - Find a node based on type and one of the
  1250. * tokens in its "compatible" property
  1251. * @from: The node to start searching from or NULL, the node
  1252. * you pass will not be searched, only the next one
  1253. * will; typically, you pass what the previous call
  1254. * returned. of_node_put() will be called on it
  1255. * @type: The type string to match "device_type" or NULL to ignore
  1256. * @compatible: The string to match to one of the tokens in the device
  1257. * "compatible" list.
  1258. *
  1259. * Returns a node pointer with refcount incremented, use
  1260. * of_node_put() on it when done.
  1261. */
  1262. struct device_node *of_find_compatible_node(struct device_node *from,
  1263. const char *type, const char *compatible)
  1264. {
  1265. struct device_node *np;
  1266. read_lock(&devtree_lock);
  1267. np = from ? from->allnext : allnodes;
  1268. for (; np != 0; np = np->allnext) {
  1269. if (type != NULL
  1270. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1271. continue;
  1272. if (device_is_compatible(np, compatible) && of_node_get(np))
  1273. break;
  1274. }
  1275. if (from)
  1276. of_node_put(from);
  1277. read_unlock(&devtree_lock);
  1278. return np;
  1279. }
  1280. EXPORT_SYMBOL(of_find_compatible_node);
  1281. /**
  1282. * of_find_node_by_path - Find a node matching a full OF path
  1283. * @path: The full path to match
  1284. *
  1285. * Returns a node pointer with refcount incremented, use
  1286. * of_node_put() on it when done.
  1287. */
  1288. struct device_node *of_find_node_by_path(const char *path)
  1289. {
  1290. struct device_node *np = allnodes;
  1291. read_lock(&devtree_lock);
  1292. for (; np != 0; np = np->allnext)
  1293. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1294. && of_node_get(np))
  1295. break;
  1296. read_unlock(&devtree_lock);
  1297. return np;
  1298. }
  1299. EXPORT_SYMBOL(of_find_node_by_path);
  1300. /**
  1301. * of_find_node_by_phandle - Find a node given a phandle
  1302. * @handle: phandle of the node to find
  1303. *
  1304. * Returns a node pointer with refcount incremented, use
  1305. * of_node_put() on it when done.
  1306. */
  1307. struct device_node *of_find_node_by_phandle(phandle handle)
  1308. {
  1309. struct device_node *np;
  1310. read_lock(&devtree_lock);
  1311. for (np = allnodes; np != 0; np = np->allnext)
  1312. if (np->linux_phandle == handle)
  1313. break;
  1314. if (np)
  1315. of_node_get(np);
  1316. read_unlock(&devtree_lock);
  1317. return np;
  1318. }
  1319. EXPORT_SYMBOL(of_find_node_by_phandle);
  1320. /**
  1321. * of_find_all_nodes - Get next node in global list
  1322. * @prev: Previous node or NULL to start iteration
  1323. * of_node_put() will be called on it
  1324. *
  1325. * Returns a node pointer with refcount incremented, use
  1326. * of_node_put() on it when done.
  1327. */
  1328. struct device_node *of_find_all_nodes(struct device_node *prev)
  1329. {
  1330. struct device_node *np;
  1331. read_lock(&devtree_lock);
  1332. np = prev ? prev->allnext : allnodes;
  1333. for (; np != 0; np = np->allnext)
  1334. if (of_node_get(np))
  1335. break;
  1336. if (prev)
  1337. of_node_put(prev);
  1338. read_unlock(&devtree_lock);
  1339. return np;
  1340. }
  1341. EXPORT_SYMBOL(of_find_all_nodes);
  1342. /**
  1343. * of_get_parent - Get a node's parent if any
  1344. * @node: Node to get parent
  1345. *
  1346. * Returns a node pointer with refcount incremented, use
  1347. * of_node_put() on it when done.
  1348. */
  1349. struct device_node *of_get_parent(const struct device_node *node)
  1350. {
  1351. struct device_node *np;
  1352. if (!node)
  1353. return NULL;
  1354. read_lock(&devtree_lock);
  1355. np = of_node_get(node->parent);
  1356. read_unlock(&devtree_lock);
  1357. return np;
  1358. }
  1359. EXPORT_SYMBOL(of_get_parent);
  1360. /**
  1361. * of_get_next_child - Iterate a node childs
  1362. * @node: parent node
  1363. * @prev: previous child of the parent node, or NULL to get first
  1364. *
  1365. * Returns a node pointer with refcount incremented, use
  1366. * of_node_put() on it when done.
  1367. */
  1368. struct device_node *of_get_next_child(const struct device_node *node,
  1369. struct device_node *prev)
  1370. {
  1371. struct device_node *next;
  1372. read_lock(&devtree_lock);
  1373. next = prev ? prev->sibling : node->child;
  1374. for (; next != 0; next = next->sibling)
  1375. if (of_node_get(next))
  1376. break;
  1377. if (prev)
  1378. of_node_put(prev);
  1379. read_unlock(&devtree_lock);
  1380. return next;
  1381. }
  1382. EXPORT_SYMBOL(of_get_next_child);
  1383. /**
  1384. * of_node_get - Increment refcount of a node
  1385. * @node: Node to inc refcount, NULL is supported to
  1386. * simplify writing of callers
  1387. *
  1388. * Returns node.
  1389. */
  1390. struct device_node *of_node_get(struct device_node *node)
  1391. {
  1392. if (node)
  1393. kref_get(&node->kref);
  1394. return node;
  1395. }
  1396. EXPORT_SYMBOL(of_node_get);
  1397. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1398. {
  1399. return container_of(kref, struct device_node, kref);
  1400. }
  1401. /**
  1402. * of_node_release - release a dynamically allocated node
  1403. * @kref: kref element of the node to be released
  1404. *
  1405. * In of_node_put() this function is passed to kref_put()
  1406. * as the destructor.
  1407. */
  1408. static void of_node_release(struct kref *kref)
  1409. {
  1410. struct device_node *node = kref_to_device_node(kref);
  1411. struct property *prop = node->properties;
  1412. if (!OF_IS_DYNAMIC(node))
  1413. return;
  1414. while (prop) {
  1415. struct property *next = prop->next;
  1416. kfree(prop->name);
  1417. kfree(prop->value);
  1418. kfree(prop);
  1419. prop = next;
  1420. }
  1421. kfree(node->intrs);
  1422. kfree(node->addrs);
  1423. kfree(node->full_name);
  1424. kfree(node);
  1425. }
  1426. /**
  1427. * of_node_put - Decrement refcount of a node
  1428. * @node: Node to dec refcount, NULL is supported to
  1429. * simplify writing of callers
  1430. *
  1431. */
  1432. void of_node_put(struct device_node *node)
  1433. {
  1434. if (node)
  1435. kref_put(&node->kref, of_node_release);
  1436. }
  1437. EXPORT_SYMBOL(of_node_put);
  1438. /*
  1439. * Fix up the uninitialized fields in a new device node:
  1440. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1441. *
  1442. * A lot of boot-time code is duplicated here, because functions such
  1443. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1444. * slab allocator.
  1445. *
  1446. * This should probably be split up into smaller chunks.
  1447. */
  1448. static int of_finish_dynamic_node(struct device_node *node,
  1449. unsigned long *unused1, int unused2,
  1450. int unused3, int unused4)
  1451. {
  1452. struct device_node *parent = of_get_parent(node);
  1453. int err = 0;
  1454. phandle *ibm_phandle;
  1455. node->name = get_property(node, "name", NULL);
  1456. node->type = get_property(node, "device_type", NULL);
  1457. if (!parent) {
  1458. err = -ENODEV;
  1459. goto out;
  1460. }
  1461. /* We don't support that function on PowerMac, at least
  1462. * not yet
  1463. */
  1464. if (systemcfg->platform == PLATFORM_POWERMAC)
  1465. return -ENODEV;
  1466. /* fix up new node's linux_phandle field */
  1467. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1468. node->linux_phandle = *ibm_phandle;
  1469. out:
  1470. of_node_put(parent);
  1471. return err;
  1472. }
  1473. /*
  1474. * Plug a device node into the tree and global list.
  1475. */
  1476. void of_attach_node(struct device_node *np)
  1477. {
  1478. write_lock(&devtree_lock);
  1479. np->sibling = np->parent->child;
  1480. np->allnext = allnodes;
  1481. np->parent->child = np;
  1482. allnodes = np;
  1483. write_unlock(&devtree_lock);
  1484. }
  1485. /*
  1486. * "Unplug" a node from the device tree. The caller must hold
  1487. * a reference to the node. The memory associated with the node
  1488. * is not freed until its refcount goes to zero.
  1489. */
  1490. void of_detach_node(const struct device_node *np)
  1491. {
  1492. struct device_node *parent;
  1493. write_lock(&devtree_lock);
  1494. parent = np->parent;
  1495. if (allnodes == np)
  1496. allnodes = np->allnext;
  1497. else {
  1498. struct device_node *prev;
  1499. for (prev = allnodes;
  1500. prev->allnext != np;
  1501. prev = prev->allnext)
  1502. ;
  1503. prev->allnext = np->allnext;
  1504. }
  1505. if (parent->child == np)
  1506. parent->child = np->sibling;
  1507. else {
  1508. struct device_node *prevsib;
  1509. for (prevsib = np->parent->child;
  1510. prevsib->sibling != np;
  1511. prevsib = prevsib->sibling)
  1512. ;
  1513. prevsib->sibling = np->sibling;
  1514. }
  1515. write_unlock(&devtree_lock);
  1516. }
  1517. static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1518. {
  1519. int err;
  1520. switch (action) {
  1521. case PSERIES_RECONFIG_ADD:
  1522. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1523. if (err < 0) {
  1524. printk(KERN_ERR "finish_node returned %d\n", err);
  1525. err = NOTIFY_BAD;
  1526. }
  1527. break;
  1528. default:
  1529. err = NOTIFY_DONE;
  1530. break;
  1531. }
  1532. return err;
  1533. }
  1534. static struct notifier_block prom_reconfig_nb = {
  1535. .notifier_call = prom_reconfig_notifier,
  1536. .priority = 10, /* This one needs to run first */
  1537. };
  1538. static int __init prom_reconfig_setup(void)
  1539. {
  1540. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1541. }
  1542. __initcall(prom_reconfig_setup);
  1543. /*
  1544. * Find a property with a given name for a given node
  1545. * and return the value.
  1546. */
  1547. unsigned char *
  1548. get_property(struct device_node *np, const char *name, int *lenp)
  1549. {
  1550. struct property *pp;
  1551. for (pp = np->properties; pp != 0; pp = pp->next)
  1552. if (strcmp(pp->name, name) == 0) {
  1553. if (lenp != 0)
  1554. *lenp = pp->length;
  1555. return pp->value;
  1556. }
  1557. return NULL;
  1558. }
  1559. EXPORT_SYMBOL(get_property);
  1560. /*
  1561. * Add a property to a node
  1562. */
  1563. void
  1564. prom_add_property(struct device_node* np, struct property* prop)
  1565. {
  1566. struct property **next = &np->properties;
  1567. prop->next = NULL;
  1568. while (*next)
  1569. next = &(*next)->next;
  1570. *next = prop;
  1571. }
  1572. #if 0
  1573. void
  1574. print_properties(struct device_node *np)
  1575. {
  1576. struct property *pp;
  1577. char *cp;
  1578. int i, n;
  1579. for (pp = np->properties; pp != 0; pp = pp->next) {
  1580. printk(KERN_INFO "%s", pp->name);
  1581. for (i = strlen(pp->name); i < 16; ++i)
  1582. printk(" ");
  1583. cp = (char *) pp->value;
  1584. for (i = pp->length; i > 0; --i, ++cp)
  1585. if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
  1586. || (i == 1 && *cp != 0))
  1587. break;
  1588. if (i == 0 && pp->length > 1) {
  1589. /* looks like a string */
  1590. printk(" %s\n", (char *) pp->value);
  1591. } else {
  1592. /* dump it in hex */
  1593. n = pp->length;
  1594. if (n > 64)
  1595. n = 64;
  1596. if (pp->length % 4 == 0) {
  1597. unsigned int *p = (unsigned int *) pp->value;
  1598. n /= 4;
  1599. for (i = 0; i < n; ++i) {
  1600. if (i != 0 && (i % 4) == 0)
  1601. printk("\n ");
  1602. printk(" %08x", *p++);
  1603. }
  1604. } else {
  1605. unsigned char *bp = pp->value;
  1606. for (i = 0; i < n; ++i) {
  1607. if (i != 0 && (i % 16) == 0)
  1608. printk("\n ");
  1609. printk(" %02x", *bp++);
  1610. }
  1611. }
  1612. printk("\n");
  1613. if (pp->length > 64)
  1614. printk(" ... (length = %d)\n",
  1615. pp->length);
  1616. }
  1617. }
  1618. }
  1619. #endif