prom.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  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) +
  560. initial_boot_params->off_dt_strings + 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 *uname, int depth,
  569. void *data),
  570. void *data)
  571. {
  572. unsigned long p = ((unsigned long)initial_boot_params) +
  573. initial_boot_params->off_dt_struct;
  574. int rc = 0;
  575. int depth = -1;
  576. do {
  577. u32 tag = *((u32 *)p);
  578. char *pathp;
  579. p += 4;
  580. if (tag == OF_DT_END_NODE) {
  581. depth --;
  582. continue;
  583. }
  584. if (tag == OF_DT_NOP)
  585. continue;
  586. if (tag == OF_DT_END)
  587. break;
  588. if (tag == OF_DT_PROP) {
  589. u32 sz = *((u32 *)p);
  590. p += 8;
  591. if (initial_boot_params->version < 0x10)
  592. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  593. p += sz;
  594. p = _ALIGN(p, 4);
  595. continue;
  596. }
  597. if (tag != OF_DT_BEGIN_NODE) {
  598. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  599. " device tree !\n", tag);
  600. return -EINVAL;
  601. }
  602. depth++;
  603. pathp = (char *)p;
  604. p = _ALIGN(p + strlen(pathp) + 1, 4);
  605. if ((*pathp) == '/') {
  606. char *lp, *np;
  607. for (lp = NULL, np = pathp; *np; np++)
  608. if ((*np) == '/')
  609. lp = np+1;
  610. if (lp != NULL)
  611. pathp = lp;
  612. }
  613. rc = it(p, pathp, depth, data);
  614. if (rc != 0)
  615. break;
  616. } while(1);
  617. return rc;
  618. }
  619. /**
  620. * This function can be used within scan_flattened_dt callback to get
  621. * access to properties
  622. */
  623. static void* __init get_flat_dt_prop(unsigned long node, const char *name,
  624. unsigned long *size)
  625. {
  626. unsigned long p = node;
  627. do {
  628. u32 tag = *((u32 *)p);
  629. u32 sz, noff;
  630. const char *nstr;
  631. p += 4;
  632. if (tag == OF_DT_NOP)
  633. continue;
  634. if (tag != OF_DT_PROP)
  635. return NULL;
  636. sz = *((u32 *)p);
  637. noff = *((u32 *)(p + 4));
  638. p += 8;
  639. if (initial_boot_params->version < 0x10)
  640. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  641. nstr = find_flat_dt_string(noff);
  642. if (nstr == NULL) {
  643. printk(KERN_WARNING "Can't find property index"
  644. " name !\n");
  645. return NULL;
  646. }
  647. if (strcmp(name, nstr) == 0) {
  648. if (size)
  649. *size = sz;
  650. return (void *)p;
  651. }
  652. p += sz;
  653. p = _ALIGN(p, 4);
  654. } while(1);
  655. }
  656. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  657. unsigned long align)
  658. {
  659. void *res;
  660. *mem = _ALIGN(*mem, align);
  661. res = (void *)*mem;
  662. *mem += size;
  663. return res;
  664. }
  665. static unsigned long __init unflatten_dt_node(unsigned long mem,
  666. unsigned long *p,
  667. struct device_node *dad,
  668. struct device_node ***allnextpp,
  669. unsigned long fpsize)
  670. {
  671. struct device_node *np;
  672. struct property *pp, **prev_pp = NULL;
  673. char *pathp;
  674. u32 tag;
  675. unsigned int l, allocl;
  676. int has_name = 0;
  677. int new_format = 0;
  678. tag = *((u32 *)(*p));
  679. if (tag != OF_DT_BEGIN_NODE) {
  680. printk("Weird tag at start of node: %x\n", tag);
  681. return mem;
  682. }
  683. *p += 4;
  684. pathp = (char *)*p;
  685. l = allocl = strlen(pathp) + 1;
  686. *p = _ALIGN(*p + l, 4);
  687. /* version 0x10 has a more compact unit name here instead of the full
  688. * path. we accumulate the full path size using "fpsize", we'll rebuild
  689. * it later. We detect this because the first character of the name is
  690. * not '/'.
  691. */
  692. if ((*pathp) != '/') {
  693. new_format = 1;
  694. if (fpsize == 0) {
  695. /* root node: special case. fpsize accounts for path
  696. * plus terminating zero. root node only has '/', so
  697. * fpsize should be 2, but we want to avoid the first
  698. * level nodes to have two '/' so we use fpsize 1 here
  699. */
  700. fpsize = 1;
  701. allocl = 2;
  702. } else {
  703. /* account for '/' and path size minus terminal 0
  704. * already in 'l'
  705. */
  706. fpsize += l;
  707. allocl = fpsize;
  708. }
  709. }
  710. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  711. __alignof__(struct device_node));
  712. if (allnextpp) {
  713. memset(np, 0, sizeof(*np));
  714. np->full_name = ((char*)np) + sizeof(struct device_node);
  715. if (new_format) {
  716. char *p = np->full_name;
  717. /* rebuild full path for new format */
  718. if (dad && dad->parent) {
  719. strcpy(p, dad->full_name);
  720. #ifdef DEBUG
  721. if ((strlen(p) + l + 1) != allocl) {
  722. DBG("%s: p: %d, l: %d, a: %d\n",
  723. pathp, strlen(p), l, allocl);
  724. }
  725. #endif
  726. p += strlen(p);
  727. }
  728. *(p++) = '/';
  729. memcpy(p, pathp, l);
  730. } else
  731. memcpy(np->full_name, pathp, l);
  732. prev_pp = &np->properties;
  733. **allnextpp = np;
  734. *allnextpp = &np->allnext;
  735. if (dad != NULL) {
  736. np->parent = dad;
  737. /* we temporarily use the next field as `last_child'*/
  738. if (dad->next == 0)
  739. dad->child = np;
  740. else
  741. dad->next->sibling = np;
  742. dad->next = np;
  743. }
  744. kref_init(&np->kref);
  745. }
  746. while(1) {
  747. u32 sz, noff;
  748. char *pname;
  749. tag = *((u32 *)(*p));
  750. if (tag == OF_DT_NOP) {
  751. *p += 4;
  752. continue;
  753. }
  754. if (tag != OF_DT_PROP)
  755. break;
  756. *p += 4;
  757. sz = *((u32 *)(*p));
  758. noff = *((u32 *)((*p) + 4));
  759. *p += 8;
  760. if (initial_boot_params->version < 0x10)
  761. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  762. pname = find_flat_dt_string(noff);
  763. if (pname == NULL) {
  764. printk("Can't find property name in list !\n");
  765. break;
  766. }
  767. if (strcmp(pname, "name") == 0)
  768. has_name = 1;
  769. l = strlen(pname) + 1;
  770. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  771. __alignof__(struct property));
  772. if (allnextpp) {
  773. if (strcmp(pname, "linux,phandle") == 0) {
  774. np->node = *((u32 *)*p);
  775. if (np->linux_phandle == 0)
  776. np->linux_phandle = np->node;
  777. }
  778. if (strcmp(pname, "ibm,phandle") == 0)
  779. np->linux_phandle = *((u32 *)*p);
  780. pp->name = pname;
  781. pp->length = sz;
  782. pp->value = (void *)*p;
  783. *prev_pp = pp;
  784. prev_pp = &pp->next;
  785. }
  786. *p = _ALIGN((*p) + sz, 4);
  787. }
  788. /* with version 0x10 we may not have the name property, recreate
  789. * it here from the unit name if absent
  790. */
  791. if (!has_name) {
  792. char *p = pathp, *ps = pathp, *pa = NULL;
  793. int sz;
  794. while (*p) {
  795. if ((*p) == '@')
  796. pa = p;
  797. if ((*p) == '/')
  798. ps = p + 1;
  799. p++;
  800. }
  801. if (pa < ps)
  802. pa = p;
  803. sz = (pa - ps) + 1;
  804. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  805. __alignof__(struct property));
  806. if (allnextpp) {
  807. pp->name = "name";
  808. pp->length = sz;
  809. pp->value = (unsigned char *)(pp + 1);
  810. *prev_pp = pp;
  811. prev_pp = &pp->next;
  812. memcpy(pp->value, ps, sz - 1);
  813. ((char *)pp->value)[sz - 1] = 0;
  814. DBG("fixed up name for %s -> %s\n", pathp, pp->value);
  815. }
  816. }
  817. if (allnextpp) {
  818. *prev_pp = NULL;
  819. np->name = get_property(np, "name", NULL);
  820. np->type = get_property(np, "device_type", NULL);
  821. if (!np->name)
  822. np->name = "<NULL>";
  823. if (!np->type)
  824. np->type = "<NULL>";
  825. }
  826. while (tag == OF_DT_BEGIN_NODE) {
  827. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  828. tag = *((u32 *)(*p));
  829. }
  830. if (tag != OF_DT_END_NODE) {
  831. printk("Weird tag at end of node: %x\n", tag);
  832. return mem;
  833. }
  834. *p += 4;
  835. return mem;
  836. }
  837. /**
  838. * unflattens the device-tree passed by the firmware, creating the
  839. * tree of struct device_node. It also fills the "name" and "type"
  840. * pointers of the nodes so the normal device-tree walking functions
  841. * can be used (this used to be done by finish_device_tree)
  842. */
  843. void __init unflatten_device_tree(void)
  844. {
  845. unsigned long start, mem, size;
  846. struct device_node **allnextp = &allnodes;
  847. char *p = NULL;
  848. int l = 0;
  849. DBG(" -> unflatten_device_tree()\n");
  850. /* First pass, scan for size */
  851. start = ((unsigned long)initial_boot_params) +
  852. initial_boot_params->off_dt_struct;
  853. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  854. size = (size | 3) + 1;
  855. DBG(" size is %lx, allocating...\n", size);
  856. /* Allocate memory for the expanded device tree */
  857. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  858. if (!mem) {
  859. DBG("Couldn't allocate memory with lmb_alloc()!\n");
  860. panic("Couldn't allocate memory with lmb_alloc()!\n");
  861. }
  862. mem = (unsigned long)abs_to_virt(mem);
  863. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  864. DBG(" unflattening...\n", mem);
  865. /* Second pass, do actual unflattening */
  866. start = ((unsigned long)initial_boot_params) +
  867. initial_boot_params->off_dt_struct;
  868. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  869. if (*((u32 *)start) != OF_DT_END)
  870. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  871. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  872. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  873. ((u32 *)mem)[size / 4] );
  874. *allnextp = NULL;
  875. /* Get pointer to OF "/chosen" node for use everywhere */
  876. of_chosen = of_find_node_by_path("/chosen");
  877. /* Retreive command line */
  878. if (of_chosen != NULL) {
  879. p = (char *)get_property(of_chosen, "bootargs", &l);
  880. if (p != NULL && l > 0)
  881. strlcpy(cmd_line, p, min(l, COMMAND_LINE_SIZE));
  882. }
  883. #ifdef CONFIG_CMDLINE
  884. if (l == 0 || (l == 1 && (*p) == 0))
  885. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  886. #endif /* CONFIG_CMDLINE */
  887. DBG("Command line is: %s\n", cmd_line);
  888. DBG(" <- unflatten_device_tree()\n");
  889. }
  890. static int __init early_init_dt_scan_cpus(unsigned long node,
  891. const char *uname, int depth, void *data)
  892. {
  893. char *type = get_flat_dt_prop(node, "device_type", NULL);
  894. u32 *prop;
  895. unsigned long size;
  896. /* We are scanning "cpu" nodes only */
  897. if (type == NULL || strcmp(type, "cpu") != 0)
  898. return 0;
  899. /* On LPAR, look for the first ibm,pft-size property for the hash table size
  900. */
  901. if (systemcfg->platform == PLATFORM_PSERIES_LPAR && ppc64_pft_size == 0) {
  902. u32 *pft_size;
  903. pft_size = (u32 *)get_flat_dt_prop(node, "ibm,pft-size", NULL);
  904. if (pft_size != NULL) {
  905. /* pft_size[0] is the NUMA CEC cookie */
  906. ppc64_pft_size = pft_size[1];
  907. }
  908. }
  909. if (initial_boot_params && initial_boot_params->version >= 2) {
  910. /* version 2 of the kexec param format adds the phys cpuid
  911. * of booted proc.
  912. */
  913. boot_cpuid_phys = initial_boot_params->boot_cpuid_phys;
  914. boot_cpuid = 0;
  915. } else {
  916. /* Check if it's the boot-cpu, set it's hw index in paca now */
  917. if (get_flat_dt_prop(node, "linux,boot-cpu", NULL) != NULL) {
  918. u32 *prop = get_flat_dt_prop(node, "reg", NULL);
  919. set_hard_smp_processor_id(0, prop == NULL ? 0 : *prop);
  920. boot_cpuid_phys = get_hard_smp_processor_id(0);
  921. }
  922. }
  923. #ifdef CONFIG_ALTIVEC
  924. /* Check if we have a VMX and eventually update CPU features */
  925. prop = (u32 *)get_flat_dt_prop(node, "ibm,vmx", NULL);
  926. if (prop && (*prop) > 0) {
  927. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  928. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  929. }
  930. /* Same goes for Apple's "altivec" property */
  931. prop = (u32 *)get_flat_dt_prop(node, "altivec", NULL);
  932. if (prop) {
  933. cur_cpu_spec->cpu_features |= CPU_FTR_ALTIVEC;
  934. cur_cpu_spec->cpu_user_features |= PPC_FEATURE_HAS_ALTIVEC;
  935. }
  936. #endif /* CONFIG_ALTIVEC */
  937. /*
  938. * Check for an SMT capable CPU and set the CPU feature. We do
  939. * this by looking at the size of the ibm,ppc-interrupt-server#s
  940. * property
  941. */
  942. prop = (u32 *)get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s",
  943. &size);
  944. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  945. if (prop && ((size / sizeof(u32)) > 1))
  946. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  947. return 0;
  948. }
  949. static int __init early_init_dt_scan_chosen(unsigned long node,
  950. const char *uname, int depth, void *data)
  951. {
  952. u32 *prop;
  953. u64 *prop64;
  954. extern unsigned long memory_limit, tce_alloc_start, tce_alloc_end;
  955. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  956. if (depth != 1 || strcmp(uname, "chosen") != 0)
  957. return 0;
  958. /* get platform type */
  959. prop = (u32 *)get_flat_dt_prop(node, "linux,platform", NULL);
  960. if (prop == NULL)
  961. return 0;
  962. systemcfg->platform = *prop;
  963. /* check if iommu is forced on or off */
  964. if (get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  965. iommu_is_off = 1;
  966. if (get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  967. iommu_force_on = 1;
  968. prop64 = (u64*)get_flat_dt_prop(node, "linux,memory-limit", NULL);
  969. if (prop64)
  970. memory_limit = *prop64;
  971. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  972. if (prop64)
  973. tce_alloc_start = *prop64;
  974. prop64 = (u64*)get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  975. if (prop64)
  976. tce_alloc_end = *prop64;
  977. #ifdef CONFIG_PPC_RTAS
  978. /* To help early debugging via the front panel, we retreive a minimal
  979. * set of RTAS infos now if available
  980. */
  981. {
  982. u64 *basep, *entryp;
  983. basep = (u64*)get_flat_dt_prop(node, "linux,rtas-base", NULL);
  984. entryp = (u64*)get_flat_dt_prop(node, "linux,rtas-entry", NULL);
  985. prop = (u32*)get_flat_dt_prop(node, "linux,rtas-size", NULL);
  986. if (basep && entryp && prop) {
  987. rtas.base = *basep;
  988. rtas.entry = *entryp;
  989. rtas.size = *prop;
  990. }
  991. }
  992. #endif /* CONFIG_PPC_RTAS */
  993. /* break now */
  994. return 1;
  995. }
  996. static int __init early_init_dt_scan_root(unsigned long node,
  997. const char *uname, int depth, void *data)
  998. {
  999. u32 *prop;
  1000. if (depth != 0)
  1001. return 0;
  1002. prop = (u32 *)get_flat_dt_prop(node, "#size-cells", NULL);
  1003. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  1004. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  1005. prop = (u32 *)get_flat_dt_prop(node, "#address-cells", NULL);
  1006. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  1007. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  1008. /* break now */
  1009. return 1;
  1010. }
  1011. static unsigned long __init dt_mem_next_cell(int s, cell_t **cellp)
  1012. {
  1013. cell_t *p = *cellp;
  1014. unsigned long r = 0;
  1015. /* Ignore more than 2 cells */
  1016. while (s > 2) {
  1017. p++;
  1018. s--;
  1019. }
  1020. while (s) {
  1021. r <<= 32;
  1022. r |= *(p++);
  1023. s--;
  1024. }
  1025. *cellp = p;
  1026. return r;
  1027. }
  1028. static int __init early_init_dt_scan_memory(unsigned long node,
  1029. const char *uname, int depth, void *data)
  1030. {
  1031. char *type = get_flat_dt_prop(node, "device_type", NULL);
  1032. cell_t *reg, *endp;
  1033. unsigned long l;
  1034. /* We are scanning "memory" nodes only */
  1035. if (type == NULL || strcmp(type, "memory") != 0)
  1036. return 0;
  1037. reg = (cell_t *)get_flat_dt_prop(node, "reg", &l);
  1038. if (reg == NULL)
  1039. return 0;
  1040. endp = reg + (l / sizeof(cell_t));
  1041. DBG("memory scan node %s ..., reg size %ld, data: %x %x %x %x, ...\n",
  1042. uname, l, reg[0], reg[1], reg[2], reg[3]);
  1043. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  1044. unsigned long base, size;
  1045. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  1046. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  1047. if (size == 0)
  1048. continue;
  1049. DBG(" - %lx , %lx\n", base, size);
  1050. if (iommu_is_off) {
  1051. if (base >= 0x80000000ul)
  1052. continue;
  1053. if ((base + size) > 0x80000000ul)
  1054. size = 0x80000000ul - base;
  1055. }
  1056. lmb_add(base, size);
  1057. }
  1058. return 0;
  1059. }
  1060. static void __init early_reserve_mem(void)
  1061. {
  1062. u64 base, size;
  1063. u64 *reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  1064. initial_boot_params->off_mem_rsvmap);
  1065. while (1) {
  1066. base = *(reserve_map++);
  1067. size = *(reserve_map++);
  1068. if (size == 0)
  1069. break;
  1070. DBG("reserving: %lx -> %lx\n", base, size);
  1071. lmb_reserve(base, size);
  1072. }
  1073. #if 0
  1074. DBG("memory reserved, lmbs :\n");
  1075. lmb_dump_all();
  1076. #endif
  1077. }
  1078. void __init early_init_devtree(void *params)
  1079. {
  1080. DBG(" -> early_init_devtree()\n");
  1081. /* Setup flat device-tree pointer */
  1082. initial_boot_params = params;
  1083. /* By default, hash size is not set */
  1084. ppc64_pft_size = 0;
  1085. /* Retreive various informations from the /chosen node of the
  1086. * device-tree, including the platform type, initrd location and
  1087. * size, TCE reserve, and more ...
  1088. */
  1089. scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1090. /* Scan memory nodes and rebuild LMBs */
  1091. lmb_init();
  1092. scan_flat_dt(early_init_dt_scan_root, NULL);
  1093. scan_flat_dt(early_init_dt_scan_memory, NULL);
  1094. lmb_enforce_memory_limit();
  1095. lmb_analyze();
  1096. systemcfg->physicalMemorySize = lmb_phys_mem_size();
  1097. lmb_reserve(0, __pa(klimit));
  1098. DBG("Phys. mem: %lx\n", systemcfg->physicalMemorySize);
  1099. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1100. early_reserve_mem();
  1101. DBG("Scanning CPUs ...\n");
  1102. /* Retreive hash table size from flattened tree plus other
  1103. * CPU related informations (altivec support, boot CPU ID, ...)
  1104. */
  1105. scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1106. /* If hash size wasn't obtained above, we calculate it now based on
  1107. * the total RAM size
  1108. */
  1109. if (ppc64_pft_size == 0) {
  1110. unsigned long rnd_mem_size, pteg_count;
  1111. /* round mem_size up to next power of 2 */
  1112. rnd_mem_size = 1UL << __ilog2(systemcfg->physicalMemorySize);
  1113. if (rnd_mem_size < systemcfg->physicalMemorySize)
  1114. rnd_mem_size <<= 1;
  1115. /* # pages / 2 */
  1116. pteg_count = max(rnd_mem_size >> (12 + 1), 1UL << 11);
  1117. ppc64_pft_size = __ilog2(pteg_count << 7);
  1118. }
  1119. DBG("Hash pftSize: %x\n", (int)ppc64_pft_size);
  1120. DBG(" <- early_init_devtree()\n");
  1121. }
  1122. #undef printk
  1123. int
  1124. prom_n_addr_cells(struct device_node* np)
  1125. {
  1126. int* ip;
  1127. do {
  1128. if (np->parent)
  1129. np = np->parent;
  1130. ip = (int *) get_property(np, "#address-cells", NULL);
  1131. if (ip != NULL)
  1132. return *ip;
  1133. } while (np->parent);
  1134. /* No #address-cells property for the root node, default to 1 */
  1135. return 1;
  1136. }
  1137. int
  1138. prom_n_size_cells(struct device_node* np)
  1139. {
  1140. int* ip;
  1141. do {
  1142. if (np->parent)
  1143. np = np->parent;
  1144. ip = (int *) get_property(np, "#size-cells", NULL);
  1145. if (ip != NULL)
  1146. return *ip;
  1147. } while (np->parent);
  1148. /* No #size-cells property for the root node, default to 1 */
  1149. return 1;
  1150. }
  1151. /**
  1152. * Work out the sense (active-low level / active-high edge)
  1153. * of each interrupt from the device tree.
  1154. */
  1155. void __init prom_get_irq_senses(unsigned char *senses, int off, int max)
  1156. {
  1157. struct device_node *np;
  1158. int i, j;
  1159. /* default to level-triggered */
  1160. memset(senses, 1, max - off);
  1161. for (np = allnodes; np != 0; np = np->allnext) {
  1162. for (j = 0; j < np->n_intrs; j++) {
  1163. i = np->intrs[j].line;
  1164. if (i >= off && i < max)
  1165. senses[i-off] = np->intrs[j].sense ?
  1166. IRQ_SENSE_LEVEL | IRQ_POLARITY_NEGATIVE :
  1167. IRQ_SENSE_EDGE | IRQ_POLARITY_POSITIVE;
  1168. }
  1169. }
  1170. }
  1171. /**
  1172. * Construct and return a list of the device_nodes with a given name.
  1173. */
  1174. struct device_node *
  1175. find_devices(const char *name)
  1176. {
  1177. struct device_node *head, **prevp, *np;
  1178. prevp = &head;
  1179. for (np = allnodes; np != 0; np = np->allnext) {
  1180. if (np->name != 0 && strcasecmp(np->name, name) == 0) {
  1181. *prevp = np;
  1182. prevp = &np->next;
  1183. }
  1184. }
  1185. *prevp = NULL;
  1186. return head;
  1187. }
  1188. EXPORT_SYMBOL(find_devices);
  1189. /**
  1190. * Construct and return a list of the device_nodes with a given type.
  1191. */
  1192. struct device_node *
  1193. find_type_devices(const char *type)
  1194. {
  1195. struct device_node *head, **prevp, *np;
  1196. prevp = &head;
  1197. for (np = allnodes; np != 0; np = np->allnext) {
  1198. if (np->type != 0 && strcasecmp(np->type, type) == 0) {
  1199. *prevp = np;
  1200. prevp = &np->next;
  1201. }
  1202. }
  1203. *prevp = NULL;
  1204. return head;
  1205. }
  1206. EXPORT_SYMBOL(find_type_devices);
  1207. /**
  1208. * Returns all nodes linked together
  1209. */
  1210. struct device_node *
  1211. find_all_nodes(void)
  1212. {
  1213. struct device_node *head, **prevp, *np;
  1214. prevp = &head;
  1215. for (np = allnodes; np != 0; np = np->allnext) {
  1216. *prevp = np;
  1217. prevp = &np->next;
  1218. }
  1219. *prevp = NULL;
  1220. return head;
  1221. }
  1222. EXPORT_SYMBOL(find_all_nodes);
  1223. /** Checks if the given "compat" string matches one of the strings in
  1224. * the device's "compatible" property
  1225. */
  1226. int
  1227. device_is_compatible(struct device_node *device, const char *compat)
  1228. {
  1229. const char* cp;
  1230. int cplen, l;
  1231. cp = (char *) get_property(device, "compatible", &cplen);
  1232. if (cp == NULL)
  1233. return 0;
  1234. while (cplen > 0) {
  1235. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  1236. return 1;
  1237. l = strlen(cp) + 1;
  1238. cp += l;
  1239. cplen -= l;
  1240. }
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL(device_is_compatible);
  1244. /**
  1245. * Indicates whether the root node has a given value in its
  1246. * compatible property.
  1247. */
  1248. int
  1249. machine_is_compatible(const char *compat)
  1250. {
  1251. struct device_node *root;
  1252. int rc = 0;
  1253. root = of_find_node_by_path("/");
  1254. if (root) {
  1255. rc = device_is_compatible(root, compat);
  1256. of_node_put(root);
  1257. }
  1258. return rc;
  1259. }
  1260. EXPORT_SYMBOL(machine_is_compatible);
  1261. /**
  1262. * Construct and return a list of the device_nodes with a given type
  1263. * and compatible property.
  1264. */
  1265. struct device_node *
  1266. find_compatible_devices(const char *type, const char *compat)
  1267. {
  1268. struct device_node *head, **prevp, *np;
  1269. prevp = &head;
  1270. for (np = allnodes; np != 0; np = np->allnext) {
  1271. if (type != NULL
  1272. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1273. continue;
  1274. if (device_is_compatible(np, compat)) {
  1275. *prevp = np;
  1276. prevp = &np->next;
  1277. }
  1278. }
  1279. *prevp = NULL;
  1280. return head;
  1281. }
  1282. EXPORT_SYMBOL(find_compatible_devices);
  1283. /**
  1284. * Find the device_node with a given full_name.
  1285. */
  1286. struct device_node *
  1287. find_path_device(const char *path)
  1288. {
  1289. struct device_node *np;
  1290. for (np = allnodes; np != 0; np = np->allnext)
  1291. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0)
  1292. return np;
  1293. return NULL;
  1294. }
  1295. EXPORT_SYMBOL(find_path_device);
  1296. /*******
  1297. *
  1298. * New implementation of the OF "find" APIs, return a refcounted
  1299. * object, call of_node_put() when done. The device tree and list
  1300. * are protected by a rw_lock.
  1301. *
  1302. * Note that property management will need some locking as well,
  1303. * this isn't dealt with yet.
  1304. *
  1305. *******/
  1306. /**
  1307. * of_find_node_by_name - Find a node by its "name" property
  1308. * @from: The node to start searching from or NULL, the node
  1309. * you pass will not be searched, only the next one
  1310. * will; typically, you pass what the previous call
  1311. * returned. of_node_put() will be called on it
  1312. * @name: The name string to match against
  1313. *
  1314. * Returns a node pointer with refcount incremented, use
  1315. * of_node_put() on it when done.
  1316. */
  1317. struct device_node *of_find_node_by_name(struct device_node *from,
  1318. const char *name)
  1319. {
  1320. struct device_node *np;
  1321. read_lock(&devtree_lock);
  1322. np = from ? from->allnext : allnodes;
  1323. for (; np != 0; np = np->allnext)
  1324. if (np->name != 0 && strcasecmp(np->name, name) == 0
  1325. && of_node_get(np))
  1326. break;
  1327. if (from)
  1328. of_node_put(from);
  1329. read_unlock(&devtree_lock);
  1330. return np;
  1331. }
  1332. EXPORT_SYMBOL(of_find_node_by_name);
  1333. /**
  1334. * of_find_node_by_type - Find a node by its "device_type" property
  1335. * @from: The node to start searching from or NULL, the node
  1336. * you pass will not be searched, only the next one
  1337. * will; typically, you pass what the previous call
  1338. * returned. of_node_put() will be called on it
  1339. * @name: The type string to match against
  1340. *
  1341. * Returns a node pointer with refcount incremented, use
  1342. * of_node_put() on it when done.
  1343. */
  1344. struct device_node *of_find_node_by_type(struct device_node *from,
  1345. const char *type)
  1346. {
  1347. struct device_node *np;
  1348. read_lock(&devtree_lock);
  1349. np = from ? from->allnext : allnodes;
  1350. for (; np != 0; np = np->allnext)
  1351. if (np->type != 0 && strcasecmp(np->type, type) == 0
  1352. && of_node_get(np))
  1353. break;
  1354. if (from)
  1355. of_node_put(from);
  1356. read_unlock(&devtree_lock);
  1357. return np;
  1358. }
  1359. EXPORT_SYMBOL(of_find_node_by_type);
  1360. /**
  1361. * of_find_compatible_node - Find a node based on type and one of the
  1362. * tokens in its "compatible" property
  1363. * @from: The node to start searching from or NULL, the node
  1364. * you pass will not be searched, only the next one
  1365. * will; typically, you pass what the previous call
  1366. * returned. of_node_put() will be called on it
  1367. * @type: The type string to match "device_type" or NULL to ignore
  1368. * @compatible: The string to match to one of the tokens in the device
  1369. * "compatible" list.
  1370. *
  1371. * Returns a node pointer with refcount incremented, use
  1372. * of_node_put() on it when done.
  1373. */
  1374. struct device_node *of_find_compatible_node(struct device_node *from,
  1375. const char *type, const char *compatible)
  1376. {
  1377. struct device_node *np;
  1378. read_lock(&devtree_lock);
  1379. np = from ? from->allnext : allnodes;
  1380. for (; np != 0; np = np->allnext) {
  1381. if (type != NULL
  1382. && !(np->type != 0 && strcasecmp(np->type, type) == 0))
  1383. continue;
  1384. if (device_is_compatible(np, compatible) && of_node_get(np))
  1385. break;
  1386. }
  1387. if (from)
  1388. of_node_put(from);
  1389. read_unlock(&devtree_lock);
  1390. return np;
  1391. }
  1392. EXPORT_SYMBOL(of_find_compatible_node);
  1393. /**
  1394. * of_find_node_by_path - Find a node matching a full OF path
  1395. * @path: The full path to match
  1396. *
  1397. * Returns a node pointer with refcount incremented, use
  1398. * of_node_put() on it when done.
  1399. */
  1400. struct device_node *of_find_node_by_path(const char *path)
  1401. {
  1402. struct device_node *np = allnodes;
  1403. read_lock(&devtree_lock);
  1404. for (; np != 0; np = np->allnext) {
  1405. if (np->full_name != 0 && strcasecmp(np->full_name, path) == 0
  1406. && of_node_get(np))
  1407. break;
  1408. }
  1409. read_unlock(&devtree_lock);
  1410. return np;
  1411. }
  1412. EXPORT_SYMBOL(of_find_node_by_path);
  1413. /**
  1414. * of_find_node_by_phandle - Find a node given a phandle
  1415. * @handle: phandle of the node to find
  1416. *
  1417. * Returns a node pointer with refcount incremented, use
  1418. * of_node_put() on it when done.
  1419. */
  1420. struct device_node *of_find_node_by_phandle(phandle handle)
  1421. {
  1422. struct device_node *np;
  1423. read_lock(&devtree_lock);
  1424. for (np = allnodes; np != 0; np = np->allnext)
  1425. if (np->linux_phandle == handle)
  1426. break;
  1427. if (np)
  1428. of_node_get(np);
  1429. read_unlock(&devtree_lock);
  1430. return np;
  1431. }
  1432. EXPORT_SYMBOL(of_find_node_by_phandle);
  1433. /**
  1434. * of_find_all_nodes - Get next node in global list
  1435. * @prev: Previous node or NULL to start iteration
  1436. * of_node_put() will be called on it
  1437. *
  1438. * Returns a node pointer with refcount incremented, use
  1439. * of_node_put() on it when done.
  1440. */
  1441. struct device_node *of_find_all_nodes(struct device_node *prev)
  1442. {
  1443. struct device_node *np;
  1444. read_lock(&devtree_lock);
  1445. np = prev ? prev->allnext : allnodes;
  1446. for (; np != 0; np = np->allnext)
  1447. if (of_node_get(np))
  1448. break;
  1449. if (prev)
  1450. of_node_put(prev);
  1451. read_unlock(&devtree_lock);
  1452. return np;
  1453. }
  1454. EXPORT_SYMBOL(of_find_all_nodes);
  1455. /**
  1456. * of_get_parent - Get a node's parent if any
  1457. * @node: Node to get parent
  1458. *
  1459. * Returns a node pointer with refcount incremented, use
  1460. * of_node_put() on it when done.
  1461. */
  1462. struct device_node *of_get_parent(const struct device_node *node)
  1463. {
  1464. struct device_node *np;
  1465. if (!node)
  1466. return NULL;
  1467. read_lock(&devtree_lock);
  1468. np = of_node_get(node->parent);
  1469. read_unlock(&devtree_lock);
  1470. return np;
  1471. }
  1472. EXPORT_SYMBOL(of_get_parent);
  1473. /**
  1474. * of_get_next_child - Iterate a node childs
  1475. * @node: parent node
  1476. * @prev: previous child of the parent node, or NULL to get first
  1477. *
  1478. * Returns a node pointer with refcount incremented, use
  1479. * of_node_put() on it when done.
  1480. */
  1481. struct device_node *of_get_next_child(const struct device_node *node,
  1482. struct device_node *prev)
  1483. {
  1484. struct device_node *next;
  1485. read_lock(&devtree_lock);
  1486. next = prev ? prev->sibling : node->child;
  1487. for (; next != 0; next = next->sibling)
  1488. if (of_node_get(next))
  1489. break;
  1490. if (prev)
  1491. of_node_put(prev);
  1492. read_unlock(&devtree_lock);
  1493. return next;
  1494. }
  1495. EXPORT_SYMBOL(of_get_next_child);
  1496. /**
  1497. * of_node_get - Increment refcount of a node
  1498. * @node: Node to inc refcount, NULL is supported to
  1499. * simplify writing of callers
  1500. *
  1501. * Returns node.
  1502. */
  1503. struct device_node *of_node_get(struct device_node *node)
  1504. {
  1505. if (node)
  1506. kref_get(&node->kref);
  1507. return node;
  1508. }
  1509. EXPORT_SYMBOL(of_node_get);
  1510. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1511. {
  1512. return container_of(kref, struct device_node, kref);
  1513. }
  1514. /**
  1515. * of_node_release - release a dynamically allocated node
  1516. * @kref: kref element of the node to be released
  1517. *
  1518. * In of_node_put() this function is passed to kref_put()
  1519. * as the destructor.
  1520. */
  1521. static void of_node_release(struct kref *kref)
  1522. {
  1523. struct device_node *node = kref_to_device_node(kref);
  1524. struct property *prop = node->properties;
  1525. if (!OF_IS_DYNAMIC(node))
  1526. return;
  1527. while (prop) {
  1528. struct property *next = prop->next;
  1529. kfree(prop->name);
  1530. kfree(prop->value);
  1531. kfree(prop);
  1532. prop = next;
  1533. }
  1534. kfree(node->intrs);
  1535. kfree(node->addrs);
  1536. kfree(node->full_name);
  1537. kfree(node);
  1538. }
  1539. /**
  1540. * of_node_put - Decrement refcount of a node
  1541. * @node: Node to dec refcount, NULL is supported to
  1542. * simplify writing of callers
  1543. *
  1544. */
  1545. void of_node_put(struct device_node *node)
  1546. {
  1547. if (node)
  1548. kref_put(&node->kref, of_node_release);
  1549. }
  1550. EXPORT_SYMBOL(of_node_put);
  1551. /*
  1552. * Fix up the uninitialized fields in a new device node:
  1553. * name, type, n_addrs, addrs, n_intrs, intrs, and pci-specific fields
  1554. *
  1555. * A lot of boot-time code is duplicated here, because functions such
  1556. * as finish_node_interrupts, interpret_pci_props, etc. cannot use the
  1557. * slab allocator.
  1558. *
  1559. * This should probably be split up into smaller chunks.
  1560. */
  1561. static int of_finish_dynamic_node(struct device_node *node,
  1562. unsigned long *unused1, int unused2,
  1563. int unused3, int unused4)
  1564. {
  1565. struct device_node *parent = of_get_parent(node);
  1566. int err = 0;
  1567. phandle *ibm_phandle;
  1568. node->name = get_property(node, "name", NULL);
  1569. node->type = get_property(node, "device_type", NULL);
  1570. if (!parent) {
  1571. err = -ENODEV;
  1572. goto out;
  1573. }
  1574. /* We don't support that function on PowerMac, at least
  1575. * not yet
  1576. */
  1577. if (systemcfg->platform == PLATFORM_POWERMAC)
  1578. return -ENODEV;
  1579. /* fix up new node's linux_phandle field */
  1580. if ((ibm_phandle = (unsigned int *)get_property(node, "ibm,phandle", NULL)))
  1581. node->linux_phandle = *ibm_phandle;
  1582. out:
  1583. of_node_put(parent);
  1584. return err;
  1585. }
  1586. /*
  1587. * Plug a device node into the tree and global list.
  1588. */
  1589. void of_attach_node(struct device_node *np)
  1590. {
  1591. write_lock(&devtree_lock);
  1592. np->sibling = np->parent->child;
  1593. np->allnext = allnodes;
  1594. np->parent->child = np;
  1595. allnodes = np;
  1596. write_unlock(&devtree_lock);
  1597. }
  1598. /*
  1599. * "Unplug" a node from the device tree. The caller must hold
  1600. * a reference to the node. The memory associated with the node
  1601. * is not freed until its refcount goes to zero.
  1602. */
  1603. void of_detach_node(const struct device_node *np)
  1604. {
  1605. struct device_node *parent;
  1606. write_lock(&devtree_lock);
  1607. parent = np->parent;
  1608. if (allnodes == np)
  1609. allnodes = np->allnext;
  1610. else {
  1611. struct device_node *prev;
  1612. for (prev = allnodes;
  1613. prev->allnext != np;
  1614. prev = prev->allnext)
  1615. ;
  1616. prev->allnext = np->allnext;
  1617. }
  1618. if (parent->child == np)
  1619. parent->child = np->sibling;
  1620. else {
  1621. struct device_node *prevsib;
  1622. for (prevsib = np->parent->child;
  1623. prevsib->sibling != np;
  1624. prevsib = prevsib->sibling)
  1625. ;
  1626. prevsib->sibling = np->sibling;
  1627. }
  1628. write_unlock(&devtree_lock);
  1629. }
  1630. static int prom_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
  1631. {
  1632. int err;
  1633. switch (action) {
  1634. case PSERIES_RECONFIG_ADD:
  1635. err = finish_node(node, NULL, of_finish_dynamic_node, 0, 0, 0);
  1636. if (err < 0) {
  1637. printk(KERN_ERR "finish_node returned %d\n", err);
  1638. err = NOTIFY_BAD;
  1639. }
  1640. break;
  1641. default:
  1642. err = NOTIFY_DONE;
  1643. break;
  1644. }
  1645. return err;
  1646. }
  1647. static struct notifier_block prom_reconfig_nb = {
  1648. .notifier_call = prom_reconfig_notifier,
  1649. .priority = 10, /* This one needs to run first */
  1650. };
  1651. static int __init prom_reconfig_setup(void)
  1652. {
  1653. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1654. }
  1655. __initcall(prom_reconfig_setup);
  1656. /*
  1657. * Find a property with a given name for a given node
  1658. * and return the value.
  1659. */
  1660. unsigned char *
  1661. get_property(struct device_node *np, const char *name, int *lenp)
  1662. {
  1663. struct property *pp;
  1664. for (pp = np->properties; pp != 0; pp = pp->next)
  1665. if (strcmp(pp->name, name) == 0) {
  1666. if (lenp != 0)
  1667. *lenp = pp->length;
  1668. return pp->value;
  1669. }
  1670. return NULL;
  1671. }
  1672. EXPORT_SYMBOL(get_property);
  1673. /*
  1674. * Add a property to a node
  1675. */
  1676. void
  1677. prom_add_property(struct device_node* np, struct property* prop)
  1678. {
  1679. struct property **next = &np->properties;
  1680. prop->next = NULL;
  1681. while (*next)
  1682. next = &(*next)->next;
  1683. *next = prop;
  1684. }
  1685. #if 0
  1686. void
  1687. print_properties(struct device_node *np)
  1688. {
  1689. struct property *pp;
  1690. char *cp;
  1691. int i, n;
  1692. for (pp = np->properties; pp != 0; pp = pp->next) {
  1693. printk(KERN_INFO "%s", pp->name);
  1694. for (i = strlen(pp->name); i < 16; ++i)
  1695. printk(" ");
  1696. cp = (char *) pp->value;
  1697. for (i = pp->length; i > 0; --i, ++cp)
  1698. if ((i > 1 && (*cp < 0x20 || *cp > 0x7e))
  1699. || (i == 1 && *cp != 0))
  1700. break;
  1701. if (i == 0 && pp->length > 1) {
  1702. /* looks like a string */
  1703. printk(" %s\n", (char *) pp->value);
  1704. } else {
  1705. /* dump it in hex */
  1706. n = pp->length;
  1707. if (n > 64)
  1708. n = 64;
  1709. if (pp->length % 4 == 0) {
  1710. unsigned int *p = (unsigned int *) pp->value;
  1711. n /= 4;
  1712. for (i = 0; i < n; ++i) {
  1713. if (i != 0 && (i % 4) == 0)
  1714. printk("\n ");
  1715. printk(" %08x", *p++);
  1716. }
  1717. } else {
  1718. unsigned char *bp = pp->value;
  1719. for (i = 0; i < n; ++i) {
  1720. if (i != 0 && (i % 16) == 0)
  1721. printk("\n ");
  1722. printk(" %02x", *bp++);
  1723. }
  1724. }
  1725. printk("\n");
  1726. if (pp->length > 64)
  1727. printk(" ... (length = %d)\n",
  1728. pp->length);
  1729. }
  1730. }
  1731. }
  1732. #endif