prom.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #undef DEBUG
  16. #include <stdarg.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/init.h>
  20. #include <linux/threads.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include <linux/stringify.h>
  25. #include <linux/delay.h>
  26. #include <linux/initrd.h>
  27. #include <linux/bitops.h>
  28. #include <linux/module.h>
  29. #include <linux/kexec.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/irq.h>
  32. #include <linux/lmb.h>
  33. #include <asm/prom.h>
  34. #include <asm/rtas.h>
  35. #include <asm/page.h>
  36. #include <asm/processor.h>
  37. #include <asm/irq.h>
  38. #include <asm/io.h>
  39. #include <asm/kdump.h>
  40. #include <asm/smp.h>
  41. #include <asm/system.h>
  42. #include <asm/mmu.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pci.h>
  45. #include <asm/iommu.h>
  46. #include <asm/btext.h>
  47. #include <asm/sections.h>
  48. #include <asm/machdep.h>
  49. #include <asm/pSeries_reconfig.h>
  50. #include <asm/pci-bridge.h>
  51. #include <asm/phyp_dump.h>
  52. #include <asm/kexec.h>
  53. #include <mm/mmu_decl.h>
  54. #ifdef DEBUG
  55. #define DBG(fmt...) printk(KERN_ERR fmt)
  56. #else
  57. #define DBG(fmt...)
  58. #endif
  59. static int __initdata dt_root_addr_cells;
  60. static int __initdata dt_root_size_cells;
  61. #ifdef CONFIG_PPC64
  62. int __initdata iommu_is_off;
  63. int __initdata iommu_force_on;
  64. unsigned long tce_alloc_start, tce_alloc_end;
  65. #endif
  66. typedef u32 cell_t;
  67. #if 0
  68. static struct boot_param_header *initial_boot_params __initdata;
  69. #else
  70. struct boot_param_header *initial_boot_params;
  71. #endif
  72. extern struct device_node *allnodes; /* temporary while merging */
  73. extern rwlock_t devtree_lock; /* temporary while merging */
  74. /* export that to outside world */
  75. struct device_node *of_chosen;
  76. static inline char *find_flat_dt_string(u32 offset)
  77. {
  78. return ((char *)initial_boot_params) +
  79. initial_boot_params->off_dt_strings + offset;
  80. }
  81. /**
  82. * This function is used to scan the flattened device-tree, it is
  83. * used to extract the memory informations at boot before we can
  84. * unflatten the tree
  85. */
  86. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  87. const char *uname, int depth,
  88. void *data),
  89. void *data)
  90. {
  91. unsigned long p = ((unsigned long)initial_boot_params) +
  92. initial_boot_params->off_dt_struct;
  93. int rc = 0;
  94. int depth = -1;
  95. do {
  96. u32 tag = *((u32 *)p);
  97. char *pathp;
  98. p += 4;
  99. if (tag == OF_DT_END_NODE) {
  100. depth --;
  101. continue;
  102. }
  103. if (tag == OF_DT_NOP)
  104. continue;
  105. if (tag == OF_DT_END)
  106. break;
  107. if (tag == OF_DT_PROP) {
  108. u32 sz = *((u32 *)p);
  109. p += 8;
  110. if (initial_boot_params->version < 0x10)
  111. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  112. p += sz;
  113. p = _ALIGN(p, 4);
  114. continue;
  115. }
  116. if (tag != OF_DT_BEGIN_NODE) {
  117. printk(KERN_WARNING "Invalid tag %x scanning flattened"
  118. " device tree !\n", tag);
  119. return -EINVAL;
  120. }
  121. depth++;
  122. pathp = (char *)p;
  123. p = _ALIGN(p + strlen(pathp) + 1, 4);
  124. if ((*pathp) == '/') {
  125. char *lp, *np;
  126. for (lp = NULL, np = pathp; *np; np++)
  127. if ((*np) == '/')
  128. lp = np+1;
  129. if (lp != NULL)
  130. pathp = lp;
  131. }
  132. rc = it(p, pathp, depth, data);
  133. if (rc != 0)
  134. break;
  135. } while(1);
  136. return rc;
  137. }
  138. unsigned long __init of_get_flat_dt_root(void)
  139. {
  140. unsigned long p = ((unsigned long)initial_boot_params) +
  141. initial_boot_params->off_dt_struct;
  142. while(*((u32 *)p) == OF_DT_NOP)
  143. p += 4;
  144. BUG_ON (*((u32 *)p) != OF_DT_BEGIN_NODE);
  145. p += 4;
  146. return _ALIGN(p + strlen((char *)p) + 1, 4);
  147. }
  148. /**
  149. * This function can be used within scan_flattened_dt callback to get
  150. * access to properties
  151. */
  152. void* __init of_get_flat_dt_prop(unsigned long node, const char *name,
  153. unsigned long *size)
  154. {
  155. unsigned long p = node;
  156. do {
  157. u32 tag = *((u32 *)p);
  158. u32 sz, noff;
  159. const char *nstr;
  160. p += 4;
  161. if (tag == OF_DT_NOP)
  162. continue;
  163. if (tag != OF_DT_PROP)
  164. return NULL;
  165. sz = *((u32 *)p);
  166. noff = *((u32 *)(p + 4));
  167. p += 8;
  168. if (initial_boot_params->version < 0x10)
  169. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  170. nstr = find_flat_dt_string(noff);
  171. if (nstr == NULL) {
  172. printk(KERN_WARNING "Can't find property index"
  173. " name !\n");
  174. return NULL;
  175. }
  176. if (strcmp(name, nstr) == 0) {
  177. if (size)
  178. *size = sz;
  179. return (void *)p;
  180. }
  181. p += sz;
  182. p = _ALIGN(p, 4);
  183. } while(1);
  184. }
  185. int __init of_flat_dt_is_compatible(unsigned long node, const char *compat)
  186. {
  187. const char* cp;
  188. unsigned long cplen, l;
  189. cp = of_get_flat_dt_prop(node, "compatible", &cplen);
  190. if (cp == NULL)
  191. return 0;
  192. while (cplen > 0) {
  193. if (strncasecmp(cp, compat, strlen(compat)) == 0)
  194. return 1;
  195. l = strlen(cp) + 1;
  196. cp += l;
  197. cplen -= l;
  198. }
  199. return 0;
  200. }
  201. static void *__init unflatten_dt_alloc(unsigned long *mem, unsigned long size,
  202. unsigned long align)
  203. {
  204. void *res;
  205. *mem = _ALIGN(*mem, align);
  206. res = (void *)*mem;
  207. *mem += size;
  208. return res;
  209. }
  210. static unsigned long __init unflatten_dt_node(unsigned long mem,
  211. unsigned long *p,
  212. struct device_node *dad,
  213. struct device_node ***allnextpp,
  214. unsigned long fpsize)
  215. {
  216. struct device_node *np;
  217. struct property *pp, **prev_pp = NULL;
  218. char *pathp;
  219. u32 tag;
  220. unsigned int l, allocl;
  221. int has_name = 0;
  222. int new_format = 0;
  223. tag = *((u32 *)(*p));
  224. if (tag != OF_DT_BEGIN_NODE) {
  225. printk("Weird tag at start of node: %x\n", tag);
  226. return mem;
  227. }
  228. *p += 4;
  229. pathp = (char *)*p;
  230. l = allocl = strlen(pathp) + 1;
  231. *p = _ALIGN(*p + l, 4);
  232. /* version 0x10 has a more compact unit name here instead of the full
  233. * path. we accumulate the full path size using "fpsize", we'll rebuild
  234. * it later. We detect this because the first character of the name is
  235. * not '/'.
  236. */
  237. if ((*pathp) != '/') {
  238. new_format = 1;
  239. if (fpsize == 0) {
  240. /* root node: special case. fpsize accounts for path
  241. * plus terminating zero. root node only has '/', so
  242. * fpsize should be 2, but we want to avoid the first
  243. * level nodes to have two '/' so we use fpsize 1 here
  244. */
  245. fpsize = 1;
  246. allocl = 2;
  247. } else {
  248. /* account for '/' and path size minus terminal 0
  249. * already in 'l'
  250. */
  251. fpsize += l;
  252. allocl = fpsize;
  253. }
  254. }
  255. np = unflatten_dt_alloc(&mem, sizeof(struct device_node) + allocl,
  256. __alignof__(struct device_node));
  257. if (allnextpp) {
  258. memset(np, 0, sizeof(*np));
  259. np->full_name = ((char*)np) + sizeof(struct device_node);
  260. if (new_format) {
  261. char *p = np->full_name;
  262. /* rebuild full path for new format */
  263. if (dad && dad->parent) {
  264. strcpy(p, dad->full_name);
  265. #ifdef DEBUG
  266. if ((strlen(p) + l + 1) != allocl) {
  267. DBG("%s: p: %d, l: %d, a: %d\n",
  268. pathp, (int)strlen(p), l, allocl);
  269. }
  270. #endif
  271. p += strlen(p);
  272. }
  273. *(p++) = '/';
  274. memcpy(p, pathp, l);
  275. } else
  276. memcpy(np->full_name, pathp, l);
  277. prev_pp = &np->properties;
  278. **allnextpp = np;
  279. *allnextpp = &np->allnext;
  280. if (dad != NULL) {
  281. np->parent = dad;
  282. /* we temporarily use the next field as `last_child'*/
  283. if (dad->next == 0)
  284. dad->child = np;
  285. else
  286. dad->next->sibling = np;
  287. dad->next = np;
  288. }
  289. kref_init(&np->kref);
  290. }
  291. while(1) {
  292. u32 sz, noff;
  293. char *pname;
  294. tag = *((u32 *)(*p));
  295. if (tag == OF_DT_NOP) {
  296. *p += 4;
  297. continue;
  298. }
  299. if (tag != OF_DT_PROP)
  300. break;
  301. *p += 4;
  302. sz = *((u32 *)(*p));
  303. noff = *((u32 *)((*p) + 4));
  304. *p += 8;
  305. if (initial_boot_params->version < 0x10)
  306. *p = _ALIGN(*p, sz >= 8 ? 8 : 4);
  307. pname = find_flat_dt_string(noff);
  308. if (pname == NULL) {
  309. printk("Can't find property name in list !\n");
  310. break;
  311. }
  312. if (strcmp(pname, "name") == 0)
  313. has_name = 1;
  314. l = strlen(pname) + 1;
  315. pp = unflatten_dt_alloc(&mem, sizeof(struct property),
  316. __alignof__(struct property));
  317. if (allnextpp) {
  318. if (strcmp(pname, "linux,phandle") == 0) {
  319. np->node = *((u32 *)*p);
  320. if (np->linux_phandle == 0)
  321. np->linux_phandle = np->node;
  322. }
  323. if (strcmp(pname, "ibm,phandle") == 0)
  324. np->linux_phandle = *((u32 *)*p);
  325. pp->name = pname;
  326. pp->length = sz;
  327. pp->value = (void *)*p;
  328. *prev_pp = pp;
  329. prev_pp = &pp->next;
  330. }
  331. *p = _ALIGN((*p) + sz, 4);
  332. }
  333. /* with version 0x10 we may not have the name property, recreate
  334. * it here from the unit name if absent
  335. */
  336. if (!has_name) {
  337. char *p = pathp, *ps = pathp, *pa = NULL;
  338. int sz;
  339. while (*p) {
  340. if ((*p) == '@')
  341. pa = p;
  342. if ((*p) == '/')
  343. ps = p + 1;
  344. p++;
  345. }
  346. if (pa < ps)
  347. pa = p;
  348. sz = (pa - ps) + 1;
  349. pp = unflatten_dt_alloc(&mem, sizeof(struct property) + sz,
  350. __alignof__(struct property));
  351. if (allnextpp) {
  352. pp->name = "name";
  353. pp->length = sz;
  354. pp->value = pp + 1;
  355. *prev_pp = pp;
  356. prev_pp = &pp->next;
  357. memcpy(pp->value, ps, sz - 1);
  358. ((char *)pp->value)[sz - 1] = 0;
  359. DBG("fixed up name for %s -> %s\n", pathp,
  360. (char *)pp->value);
  361. }
  362. }
  363. if (allnextpp) {
  364. *prev_pp = NULL;
  365. np->name = of_get_property(np, "name", NULL);
  366. np->type = of_get_property(np, "device_type", NULL);
  367. if (!np->name)
  368. np->name = "<NULL>";
  369. if (!np->type)
  370. np->type = "<NULL>";
  371. }
  372. while (tag == OF_DT_BEGIN_NODE) {
  373. mem = unflatten_dt_node(mem, p, np, allnextpp, fpsize);
  374. tag = *((u32 *)(*p));
  375. }
  376. if (tag != OF_DT_END_NODE) {
  377. printk("Weird tag at end of node: %x\n", tag);
  378. return mem;
  379. }
  380. *p += 4;
  381. return mem;
  382. }
  383. static int __init early_parse_mem(char *p)
  384. {
  385. if (!p)
  386. return 1;
  387. memory_limit = PAGE_ALIGN(memparse(p, &p));
  388. DBG("memory limit = 0x%llx\n", (unsigned long long)memory_limit);
  389. return 0;
  390. }
  391. early_param("mem", early_parse_mem);
  392. /**
  393. * move_device_tree - move tree to an unused area, if needed.
  394. *
  395. * The device tree may be allocated beyond our memory limit, or inside the
  396. * crash kernel region for kdump. If so, move it out of the way.
  397. */
  398. static void __init move_device_tree(void)
  399. {
  400. unsigned long start, size;
  401. void *p;
  402. DBG("-> move_device_tree\n");
  403. start = __pa(initial_boot_params);
  404. size = initial_boot_params->totalsize;
  405. if ((memory_limit && (start + size) > memory_limit) ||
  406. overlaps_crashkernel(start, size)) {
  407. p = __va(lmb_alloc_base(size, PAGE_SIZE, lmb.rmo_size));
  408. memcpy(p, initial_boot_params, size);
  409. initial_boot_params = (struct boot_param_header *)p;
  410. DBG("Moved device tree to 0x%p\n", p);
  411. }
  412. DBG("<- move_device_tree\n");
  413. }
  414. /**
  415. * unflattens the device-tree passed by the firmware, creating the
  416. * tree of struct device_node. It also fills the "name" and "type"
  417. * pointers of the nodes so the normal device-tree walking functions
  418. * can be used (this used to be done by finish_device_tree)
  419. */
  420. void __init unflatten_device_tree(void)
  421. {
  422. unsigned long start, mem, size;
  423. struct device_node **allnextp = &allnodes;
  424. DBG(" -> unflatten_device_tree()\n");
  425. /* First pass, scan for size */
  426. start = ((unsigned long)initial_boot_params) +
  427. initial_boot_params->off_dt_struct;
  428. size = unflatten_dt_node(0, &start, NULL, NULL, 0);
  429. size = (size | 3) + 1;
  430. DBG(" size is %lx, allocating...\n", size);
  431. /* Allocate memory for the expanded device tree */
  432. mem = lmb_alloc(size + 4, __alignof__(struct device_node));
  433. mem = (unsigned long) __va(mem);
  434. ((u32 *)mem)[size / 4] = 0xdeadbeef;
  435. DBG(" unflattening %lx...\n", mem);
  436. /* Second pass, do actual unflattening */
  437. start = ((unsigned long)initial_boot_params) +
  438. initial_boot_params->off_dt_struct;
  439. unflatten_dt_node(mem, &start, NULL, &allnextp, 0);
  440. if (*((u32 *)start) != OF_DT_END)
  441. printk(KERN_WARNING "Weird tag at end of tree: %08x\n", *((u32 *)start));
  442. if (((u32 *)mem)[size / 4] != 0xdeadbeef)
  443. printk(KERN_WARNING "End of tree marker overwritten: %08x\n",
  444. ((u32 *)mem)[size / 4] );
  445. *allnextp = NULL;
  446. /* Get pointer to OF "/chosen" node for use everywhere */
  447. of_chosen = of_find_node_by_path("/chosen");
  448. if (of_chosen == NULL)
  449. of_chosen = of_find_node_by_path("/chosen@0");
  450. DBG(" <- unflatten_device_tree()\n");
  451. }
  452. /*
  453. * ibm,pa-features is a per-cpu property that contains a string of
  454. * attribute descriptors, each of which has a 2 byte header plus up
  455. * to 254 bytes worth of processor attribute bits. First header
  456. * byte specifies the number of bytes following the header.
  457. * Second header byte is an "attribute-specifier" type, of which
  458. * zero is the only currently-defined value.
  459. * Implementation: Pass in the byte and bit offset for the feature
  460. * that we are interested in. The function will return -1 if the
  461. * pa-features property is missing, or a 1/0 to indicate if the feature
  462. * is supported/not supported. Note that the bit numbers are
  463. * big-endian to match the definition in PAPR.
  464. */
  465. static struct ibm_pa_feature {
  466. unsigned long cpu_features; /* CPU_FTR_xxx bit */
  467. unsigned int cpu_user_ftrs; /* PPC_FEATURE_xxx bit */
  468. unsigned char pabyte; /* byte number in ibm,pa-features */
  469. unsigned char pabit; /* bit number (big-endian) */
  470. unsigned char invert; /* if 1, pa bit set => clear feature */
  471. } ibm_pa_features[] __initdata = {
  472. {0, PPC_FEATURE_HAS_MMU, 0, 0, 0},
  473. {0, PPC_FEATURE_HAS_FPU, 0, 1, 0},
  474. {CPU_FTR_SLB, 0, 0, 2, 0},
  475. {CPU_FTR_CTRL, 0, 0, 3, 0},
  476. {CPU_FTR_NOEXECUTE, 0, 0, 6, 0},
  477. {CPU_FTR_NODSISRALIGN, 0, 1, 1, 1},
  478. {CPU_FTR_CI_LARGE_PAGE, 0, 1, 2, 0},
  479. {CPU_FTR_REAL_LE, PPC_FEATURE_TRUE_LE, 5, 0, 0},
  480. };
  481. static void __init scan_features(unsigned long node, unsigned char *ftrs,
  482. unsigned long tablelen,
  483. struct ibm_pa_feature *fp,
  484. unsigned long ft_size)
  485. {
  486. unsigned long i, len, bit;
  487. /* find descriptor with type == 0 */
  488. for (;;) {
  489. if (tablelen < 3)
  490. return;
  491. len = 2 + ftrs[0];
  492. if (tablelen < len)
  493. return; /* descriptor 0 not found */
  494. if (ftrs[1] == 0)
  495. break;
  496. tablelen -= len;
  497. ftrs += len;
  498. }
  499. /* loop over bits we know about */
  500. for (i = 0; i < ft_size; ++i, ++fp) {
  501. if (fp->pabyte >= ftrs[0])
  502. continue;
  503. bit = (ftrs[2 + fp->pabyte] >> (7 - fp->pabit)) & 1;
  504. if (bit ^ fp->invert) {
  505. cur_cpu_spec->cpu_features |= fp->cpu_features;
  506. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftrs;
  507. } else {
  508. cur_cpu_spec->cpu_features &= ~fp->cpu_features;
  509. cur_cpu_spec->cpu_user_features &= ~fp->cpu_user_ftrs;
  510. }
  511. }
  512. }
  513. static void __init check_cpu_pa_features(unsigned long node)
  514. {
  515. unsigned char *pa_ftrs;
  516. unsigned long tablelen;
  517. pa_ftrs = of_get_flat_dt_prop(node, "ibm,pa-features", &tablelen);
  518. if (pa_ftrs == NULL)
  519. return;
  520. scan_features(node, pa_ftrs, tablelen,
  521. ibm_pa_features, ARRAY_SIZE(ibm_pa_features));
  522. }
  523. #ifdef CONFIG_PPC_STD_MMU_64
  524. static void __init check_cpu_slb_size(unsigned long node)
  525. {
  526. u32 *slb_size_ptr;
  527. slb_size_ptr = of_get_flat_dt_prop(node, "slb-size", NULL);
  528. if (slb_size_ptr != NULL) {
  529. mmu_slb_size = *slb_size_ptr;
  530. return;
  531. }
  532. slb_size_ptr = of_get_flat_dt_prop(node, "ibm,slb-size", NULL);
  533. if (slb_size_ptr != NULL) {
  534. mmu_slb_size = *slb_size_ptr;
  535. }
  536. }
  537. #else
  538. #define check_cpu_slb_size(node) do { } while(0)
  539. #endif
  540. static struct feature_property {
  541. const char *name;
  542. u32 min_value;
  543. unsigned long cpu_feature;
  544. unsigned long cpu_user_ftr;
  545. } feature_properties[] __initdata = {
  546. #ifdef CONFIG_ALTIVEC
  547. {"altivec", 0, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  548. {"ibm,vmx", 1, CPU_FTR_ALTIVEC, PPC_FEATURE_HAS_ALTIVEC},
  549. #endif /* CONFIG_ALTIVEC */
  550. #ifdef CONFIG_VSX
  551. /* Yes, this _really_ is ibm,vmx == 2 to enable VSX */
  552. {"ibm,vmx", 2, CPU_FTR_VSX, PPC_FEATURE_HAS_VSX},
  553. #endif /* CONFIG_VSX */
  554. #ifdef CONFIG_PPC64
  555. {"ibm,dfp", 1, 0, PPC_FEATURE_HAS_DFP},
  556. {"ibm,purr", 1, CPU_FTR_PURR, 0},
  557. {"ibm,spurr", 1, CPU_FTR_SPURR, 0},
  558. #endif /* CONFIG_PPC64 */
  559. };
  560. #if defined(CONFIG_44x) && defined(CONFIG_PPC_FPU)
  561. static inline void identical_pvr_fixup(unsigned long node)
  562. {
  563. unsigned int pvr;
  564. char *model = of_get_flat_dt_prop(node, "model", NULL);
  565. /*
  566. * Since 440GR(x)/440EP(x) processors have the same pvr,
  567. * we check the node path and set bit 28 in the cur_cpu_spec
  568. * pvr for EP(x) processor version. This bit is always 0 in
  569. * the "real" pvr. Then we call identify_cpu again with
  570. * the new logical pvr to enable FPU support.
  571. */
  572. if (model && strstr(model, "440EP")) {
  573. pvr = cur_cpu_spec->pvr_value | 0x8;
  574. identify_cpu(0, pvr);
  575. DBG("Using logical pvr %x for %s\n", pvr, model);
  576. }
  577. }
  578. #else
  579. #define identical_pvr_fixup(node) do { } while(0)
  580. #endif
  581. static void __init check_cpu_feature_properties(unsigned long node)
  582. {
  583. unsigned long i;
  584. struct feature_property *fp = feature_properties;
  585. const u32 *prop;
  586. for (i = 0; i < ARRAY_SIZE(feature_properties); ++i, ++fp) {
  587. prop = of_get_flat_dt_prop(node, fp->name, NULL);
  588. if (prop && *prop >= fp->min_value) {
  589. cur_cpu_spec->cpu_features |= fp->cpu_feature;
  590. cur_cpu_spec->cpu_user_features |= fp->cpu_user_ftr;
  591. }
  592. }
  593. }
  594. static int __init early_init_dt_scan_cpus(unsigned long node,
  595. const char *uname, int depth,
  596. void *data)
  597. {
  598. static int logical_cpuid = 0;
  599. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  600. const u32 *prop;
  601. const u32 *intserv;
  602. int i, nthreads;
  603. unsigned long len;
  604. int found = 0;
  605. /* We are scanning "cpu" nodes only */
  606. if (type == NULL || strcmp(type, "cpu") != 0)
  607. return 0;
  608. /* Get physical cpuid */
  609. intserv = of_get_flat_dt_prop(node, "ibm,ppc-interrupt-server#s", &len);
  610. if (intserv) {
  611. nthreads = len / sizeof(int);
  612. } else {
  613. intserv = of_get_flat_dt_prop(node, "reg", NULL);
  614. nthreads = 1;
  615. }
  616. /*
  617. * Now see if any of these threads match our boot cpu.
  618. * NOTE: This must match the parsing done in smp_setup_cpu_maps.
  619. */
  620. for (i = 0; i < nthreads; i++) {
  621. /*
  622. * version 2 of the kexec param format adds the phys cpuid of
  623. * booted proc.
  624. */
  625. if (initial_boot_params && initial_boot_params->version >= 2) {
  626. if (intserv[i] ==
  627. initial_boot_params->boot_cpuid_phys) {
  628. found = 1;
  629. break;
  630. }
  631. } else {
  632. /*
  633. * Check if it's the boot-cpu, set it's hw index now,
  634. * unfortunately this format did not support booting
  635. * off secondary threads.
  636. */
  637. if (of_get_flat_dt_prop(node,
  638. "linux,boot-cpu", NULL) != NULL) {
  639. found = 1;
  640. break;
  641. }
  642. }
  643. #ifdef CONFIG_SMP
  644. /* logical cpu id is always 0 on UP kernels */
  645. logical_cpuid++;
  646. #endif
  647. }
  648. if (found) {
  649. DBG("boot cpu: logical %d physical %d\n", logical_cpuid,
  650. intserv[i]);
  651. boot_cpuid = logical_cpuid;
  652. set_hard_smp_processor_id(boot_cpuid, intserv[i]);
  653. /*
  654. * PAPR defines "logical" PVR values for cpus that
  655. * meet various levels of the architecture:
  656. * 0x0f000001 Architecture version 2.04
  657. * 0x0f000002 Architecture version 2.05
  658. * If the cpu-version property in the cpu node contains
  659. * such a value, we call identify_cpu again with the
  660. * logical PVR value in order to use the cpu feature
  661. * bits appropriate for the architecture level.
  662. *
  663. * A POWER6 partition in "POWER6 architected" mode
  664. * uses the 0x0f000002 PVR value; in POWER5+ mode
  665. * it uses 0x0f000001.
  666. */
  667. prop = of_get_flat_dt_prop(node, "cpu-version", NULL);
  668. if (prop && (*prop & 0xff000000) == 0x0f000000)
  669. identify_cpu(0, *prop);
  670. identical_pvr_fixup(node);
  671. }
  672. check_cpu_feature_properties(node);
  673. check_cpu_pa_features(node);
  674. check_cpu_slb_size(node);
  675. #ifdef CONFIG_PPC_PSERIES
  676. if (nthreads > 1)
  677. cur_cpu_spec->cpu_features |= CPU_FTR_SMT;
  678. else
  679. cur_cpu_spec->cpu_features &= ~CPU_FTR_SMT;
  680. #endif
  681. return 0;
  682. }
  683. #ifdef CONFIG_BLK_DEV_INITRD
  684. static void __init early_init_dt_check_for_initrd(unsigned long node)
  685. {
  686. unsigned long l;
  687. u32 *prop;
  688. DBG("Looking for initrd properties... ");
  689. prop = of_get_flat_dt_prop(node, "linux,initrd-start", &l);
  690. if (prop) {
  691. initrd_start = (unsigned long)__va(of_read_ulong(prop, l/4));
  692. prop = of_get_flat_dt_prop(node, "linux,initrd-end", &l);
  693. if (prop) {
  694. initrd_end = (unsigned long)
  695. __va(of_read_ulong(prop, l/4));
  696. initrd_below_start_ok = 1;
  697. } else {
  698. initrd_start = 0;
  699. }
  700. }
  701. DBG("initrd_start=0x%lx initrd_end=0x%lx\n", initrd_start, initrd_end);
  702. }
  703. #else
  704. static inline void early_init_dt_check_for_initrd(unsigned long node)
  705. {
  706. }
  707. #endif /* CONFIG_BLK_DEV_INITRD */
  708. static int __init early_init_dt_scan_chosen(unsigned long node,
  709. const char *uname, int depth, void *data)
  710. {
  711. unsigned long *lprop;
  712. unsigned long l;
  713. char *p;
  714. DBG("search \"chosen\", depth: %d, uname: %s\n", depth, uname);
  715. if (depth != 1 ||
  716. (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  717. return 0;
  718. #ifdef CONFIG_PPC64
  719. /* check if iommu is forced on or off */
  720. if (of_get_flat_dt_prop(node, "linux,iommu-off", NULL) != NULL)
  721. iommu_is_off = 1;
  722. if (of_get_flat_dt_prop(node, "linux,iommu-force-on", NULL) != NULL)
  723. iommu_force_on = 1;
  724. #endif
  725. /* mem=x on the command line is the preferred mechanism */
  726. lprop = of_get_flat_dt_prop(node, "linux,memory-limit", NULL);
  727. if (lprop)
  728. memory_limit = *lprop;
  729. #ifdef CONFIG_PPC64
  730. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-start", NULL);
  731. if (lprop)
  732. tce_alloc_start = *lprop;
  733. lprop = of_get_flat_dt_prop(node, "linux,tce-alloc-end", NULL);
  734. if (lprop)
  735. tce_alloc_end = *lprop;
  736. #endif
  737. #ifdef CONFIG_KEXEC
  738. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-base", NULL);
  739. if (lprop)
  740. crashk_res.start = *lprop;
  741. lprop = of_get_flat_dt_prop(node, "linux,crashkernel-size", NULL);
  742. if (lprop)
  743. crashk_res.end = crashk_res.start + *lprop - 1;
  744. #endif
  745. early_init_dt_check_for_initrd(node);
  746. /* Retreive command line */
  747. p = of_get_flat_dt_prop(node, "bootargs", &l);
  748. if (p != NULL && l > 0)
  749. strlcpy(cmd_line, p, min((int)l, COMMAND_LINE_SIZE));
  750. #ifdef CONFIG_CMDLINE
  751. if (p == NULL || l == 0 || (l == 1 && (*p) == 0))
  752. strlcpy(cmd_line, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
  753. #endif /* CONFIG_CMDLINE */
  754. DBG("Command line is: %s\n", cmd_line);
  755. /* break now */
  756. return 1;
  757. }
  758. static int __init early_init_dt_scan_root(unsigned long node,
  759. const char *uname, int depth, void *data)
  760. {
  761. u32 *prop;
  762. if (depth != 0)
  763. return 0;
  764. prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
  765. dt_root_size_cells = (prop == NULL) ? 1 : *prop;
  766. DBG("dt_root_size_cells = %x\n", dt_root_size_cells);
  767. prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
  768. dt_root_addr_cells = (prop == NULL) ? 2 : *prop;
  769. DBG("dt_root_addr_cells = %x\n", dt_root_addr_cells);
  770. /* break now */
  771. return 1;
  772. }
  773. static u64 __init dt_mem_next_cell(int s, cell_t **cellp)
  774. {
  775. cell_t *p = *cellp;
  776. *cellp = p + s;
  777. return of_read_number(p, s);
  778. }
  779. #ifdef CONFIG_PPC_PSERIES
  780. /*
  781. * Interpret the ibm,dynamic-memory property in the
  782. * /ibm,dynamic-reconfiguration-memory node.
  783. * This contains a list of memory blocks along with NUMA affinity
  784. * information.
  785. */
  786. static int __init early_init_dt_scan_drconf_memory(unsigned long node)
  787. {
  788. cell_t *dm, *ls, *usm;
  789. unsigned long l, n, flags;
  790. u64 base, size, lmb_size;
  791. unsigned int is_kexec_kdump = 0, rngs;
  792. ls = of_get_flat_dt_prop(node, "ibm,lmb-size", &l);
  793. if (ls == NULL || l < dt_root_size_cells * sizeof(cell_t))
  794. return 0;
  795. lmb_size = dt_mem_next_cell(dt_root_size_cells, &ls);
  796. dm = of_get_flat_dt_prop(node, "ibm,dynamic-memory", &l);
  797. if (dm == NULL || l < sizeof(cell_t))
  798. return 0;
  799. n = *dm++; /* number of entries */
  800. if (l < (n * (dt_root_addr_cells + 4) + 1) * sizeof(cell_t))
  801. return 0;
  802. /* check if this is a kexec/kdump kernel. */
  803. usm = of_get_flat_dt_prop(node, "linux,drconf-usable-memory",
  804. &l);
  805. if (usm != NULL)
  806. is_kexec_kdump = 1;
  807. for (; n != 0; --n) {
  808. base = dt_mem_next_cell(dt_root_addr_cells, &dm);
  809. flags = dm[3];
  810. /* skip DRC index, pad, assoc. list index, flags */
  811. dm += 4;
  812. /* skip this block if the reserved bit is set in flags (0x80)
  813. or if the block is not assigned to this partition (0x8) */
  814. if ((flags & 0x80) || !(flags & 0x8))
  815. continue;
  816. size = lmb_size;
  817. rngs = 1;
  818. if (is_kexec_kdump) {
  819. /*
  820. * For each lmb in ibm,dynamic-memory, a corresponding
  821. * entry in linux,drconf-usable-memory property contains
  822. * a counter 'p' followed by 'p' (base, size) duple.
  823. * Now read the counter from
  824. * linux,drconf-usable-memory property
  825. */
  826. rngs = dt_mem_next_cell(dt_root_size_cells, &usm);
  827. if (!rngs) /* there are no (base, size) duple */
  828. continue;
  829. }
  830. do {
  831. if (is_kexec_kdump) {
  832. base = dt_mem_next_cell(dt_root_addr_cells,
  833. &usm);
  834. size = dt_mem_next_cell(dt_root_size_cells,
  835. &usm);
  836. }
  837. if (iommu_is_off) {
  838. if (base >= 0x80000000ul)
  839. continue;
  840. if ((base + size) > 0x80000000ul)
  841. size = 0x80000000ul - base;
  842. }
  843. lmb_add(base, size);
  844. } while (--rngs);
  845. }
  846. lmb_dump_all();
  847. return 0;
  848. }
  849. #else
  850. #define early_init_dt_scan_drconf_memory(node) 0
  851. #endif /* CONFIG_PPC_PSERIES */
  852. static int __init early_init_dt_scan_memory(unsigned long node,
  853. const char *uname, int depth, void *data)
  854. {
  855. char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  856. cell_t *reg, *endp;
  857. unsigned long l;
  858. /* Look for the ibm,dynamic-reconfiguration-memory node */
  859. if (depth == 1 &&
  860. strcmp(uname, "ibm,dynamic-reconfiguration-memory") == 0)
  861. return early_init_dt_scan_drconf_memory(node);
  862. /* We are scanning "memory" nodes only */
  863. if (type == NULL) {
  864. /*
  865. * The longtrail doesn't have a device_type on the
  866. * /memory node, so look for the node called /memory@0.
  867. */
  868. if (depth != 1 || strcmp(uname, "memory@0") != 0)
  869. return 0;
  870. } else if (strcmp(type, "memory") != 0)
  871. return 0;
  872. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  873. if (reg == NULL)
  874. reg = of_get_flat_dt_prop(node, "reg", &l);
  875. if (reg == NULL)
  876. return 0;
  877. endp = reg + (l / sizeof(cell_t));
  878. DBG("memory scan node %s, reg size %ld, data: %x %x %x %x,\n",
  879. uname, l, reg[0], reg[1], reg[2], reg[3]);
  880. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  881. u64 base, size;
  882. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  883. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  884. if (size == 0)
  885. continue;
  886. DBG(" - %llx , %llx\n", (unsigned long long)base,
  887. (unsigned long long)size);
  888. #ifdef CONFIG_PPC64
  889. if (iommu_is_off) {
  890. if (base >= 0x80000000ul)
  891. continue;
  892. if ((base + size) > 0x80000000ul)
  893. size = 0x80000000ul - base;
  894. }
  895. #endif
  896. lmb_add(base, size);
  897. memstart_addr = min((u64)memstart_addr, base);
  898. }
  899. return 0;
  900. }
  901. static void __init early_reserve_mem(void)
  902. {
  903. u64 base, size;
  904. u64 *reserve_map;
  905. unsigned long self_base;
  906. unsigned long self_size;
  907. reserve_map = (u64 *)(((unsigned long)initial_boot_params) +
  908. initial_boot_params->off_mem_rsvmap);
  909. /* before we do anything, lets reserve the dt blob */
  910. self_base = __pa((unsigned long)initial_boot_params);
  911. self_size = initial_boot_params->totalsize;
  912. lmb_reserve(self_base, self_size);
  913. #ifdef CONFIG_BLK_DEV_INITRD
  914. /* then reserve the initrd, if any */
  915. if (initrd_start && (initrd_end > initrd_start))
  916. lmb_reserve(__pa(initrd_start), initrd_end - initrd_start);
  917. #endif /* CONFIG_BLK_DEV_INITRD */
  918. #ifdef CONFIG_PPC32
  919. /*
  920. * Handle the case where we might be booting from an old kexec
  921. * image that setup the mem_rsvmap as pairs of 32-bit values
  922. */
  923. if (*reserve_map > 0xffffffffull) {
  924. u32 base_32, size_32;
  925. u32 *reserve_map_32 = (u32 *)reserve_map;
  926. while (1) {
  927. base_32 = *(reserve_map_32++);
  928. size_32 = *(reserve_map_32++);
  929. if (size_32 == 0)
  930. break;
  931. /* skip if the reservation is for the blob */
  932. if (base_32 == self_base && size_32 == self_size)
  933. continue;
  934. DBG("reserving: %x -> %x\n", base_32, size_32);
  935. lmb_reserve(base_32, size_32);
  936. }
  937. return;
  938. }
  939. #endif
  940. while (1) {
  941. base = *(reserve_map++);
  942. size = *(reserve_map++);
  943. if (size == 0)
  944. break;
  945. DBG("reserving: %llx -> %llx\n", base, size);
  946. lmb_reserve(base, size);
  947. }
  948. }
  949. #ifdef CONFIG_PHYP_DUMP
  950. /**
  951. * phyp_dump_calculate_reserve_size() - reserve variable boot area 5% or arg
  952. *
  953. * Function to find the largest size we need to reserve
  954. * during early boot process.
  955. *
  956. * It either looks for boot param and returns that OR
  957. * returns larger of 256 or 5% rounded down to multiples of 256MB.
  958. *
  959. */
  960. static inline unsigned long phyp_dump_calculate_reserve_size(void)
  961. {
  962. unsigned long tmp;
  963. if (phyp_dump_info->reserve_bootvar)
  964. return phyp_dump_info->reserve_bootvar;
  965. /* divide by 20 to get 5% of value */
  966. tmp = lmb_end_of_DRAM();
  967. do_div(tmp, 20);
  968. /* round it down in multiples of 256 */
  969. tmp = tmp & ~0x0FFFFFFFUL;
  970. return (tmp > PHYP_DUMP_RMR_END ? tmp : PHYP_DUMP_RMR_END);
  971. }
  972. /**
  973. * phyp_dump_reserve_mem() - reserve all not-yet-dumped mmemory
  974. *
  975. * This routine may reserve memory regions in the kernel only
  976. * if the system is supported and a dump was taken in last
  977. * boot instance or if the hardware is supported and the
  978. * scratch area needs to be setup. In other instances it returns
  979. * without reserving anything. The memory in case of dump being
  980. * active is freed when the dump is collected (by userland tools).
  981. */
  982. static void __init phyp_dump_reserve_mem(void)
  983. {
  984. unsigned long base, size;
  985. unsigned long variable_reserve_size;
  986. if (!phyp_dump_info->phyp_dump_configured) {
  987. printk(KERN_ERR "Phyp-dump not supported on this hardware\n");
  988. return;
  989. }
  990. if (!phyp_dump_info->phyp_dump_at_boot) {
  991. printk(KERN_INFO "Phyp-dump disabled at boot time\n");
  992. return;
  993. }
  994. variable_reserve_size = phyp_dump_calculate_reserve_size();
  995. if (phyp_dump_info->phyp_dump_is_active) {
  996. /* Reserve *everything* above RMR.Area freed by userland tools*/
  997. base = variable_reserve_size;
  998. size = lmb_end_of_DRAM() - base;
  999. /* XXX crashed_ram_end is wrong, since it may be beyond
  1000. * the memory_limit, it will need to be adjusted. */
  1001. lmb_reserve(base, size);
  1002. phyp_dump_info->init_reserve_start = base;
  1003. phyp_dump_info->init_reserve_size = size;
  1004. } else {
  1005. size = phyp_dump_info->cpu_state_size +
  1006. phyp_dump_info->hpte_region_size +
  1007. variable_reserve_size;
  1008. base = lmb_end_of_DRAM() - size;
  1009. lmb_reserve(base, size);
  1010. phyp_dump_info->init_reserve_start = base;
  1011. phyp_dump_info->init_reserve_size = size;
  1012. }
  1013. }
  1014. #else
  1015. static inline void __init phyp_dump_reserve_mem(void) {}
  1016. #endif /* CONFIG_PHYP_DUMP && CONFIG_PPC_RTAS */
  1017. void __init early_init_devtree(void *params)
  1018. {
  1019. phys_addr_t limit;
  1020. DBG(" -> early_init_devtree(%p)\n", params);
  1021. /* Setup flat device-tree pointer */
  1022. initial_boot_params = params;
  1023. #ifdef CONFIG_PPC_RTAS
  1024. /* Some machines might need RTAS info for debugging, grab it now. */
  1025. of_scan_flat_dt(early_init_dt_scan_rtas, NULL);
  1026. #endif
  1027. #ifdef CONFIG_PHYP_DUMP
  1028. /* scan tree to see if dump occured during last boot */
  1029. of_scan_flat_dt(early_init_dt_scan_phyp_dump, NULL);
  1030. #endif
  1031. /* Retrieve various informations from the /chosen node of the
  1032. * device-tree, including the platform type, initrd location and
  1033. * size, TCE reserve, and more ...
  1034. */
  1035. of_scan_flat_dt(early_init_dt_scan_chosen, NULL);
  1036. /* Scan memory nodes and rebuild LMBs */
  1037. lmb_init();
  1038. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  1039. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  1040. /* Save command line for /proc/cmdline and then parse parameters */
  1041. strlcpy(boot_command_line, cmd_line, COMMAND_LINE_SIZE);
  1042. parse_early_param();
  1043. /* Reserve LMB regions used by kernel, initrd, dt, etc... */
  1044. lmb_reserve(PHYSICAL_START, __pa(klimit) - PHYSICAL_START);
  1045. /* If relocatable, reserve first 32k for interrupt vectors etc. */
  1046. if (PHYSICAL_START > MEMORY_START)
  1047. lmb_reserve(MEMORY_START, 0x8000);
  1048. reserve_kdump_trampoline();
  1049. reserve_crashkernel();
  1050. early_reserve_mem();
  1051. phyp_dump_reserve_mem();
  1052. limit = memory_limit;
  1053. if (! limit) {
  1054. phys_addr_t memsize;
  1055. /* Ensure that total memory size is page-aligned, because
  1056. * otherwise mark_bootmem() gets upset. */
  1057. lmb_analyze();
  1058. memsize = lmb_phys_mem_size();
  1059. if ((memsize & PAGE_MASK) != memsize)
  1060. limit = memsize & PAGE_MASK;
  1061. }
  1062. lmb_enforce_memory_limit(limit);
  1063. lmb_analyze();
  1064. lmb_dump_all();
  1065. DBG("Phys. mem: %llx\n", lmb_phys_mem_size());
  1066. /* We may need to relocate the flat tree, do it now.
  1067. * FIXME .. and the initrd too? */
  1068. move_device_tree();
  1069. DBG("Scanning CPUs ...\n");
  1070. /* Retreive CPU related informations from the flat tree
  1071. * (altivec support, boot CPU ID, ...)
  1072. */
  1073. of_scan_flat_dt(early_init_dt_scan_cpus, NULL);
  1074. DBG(" <- early_init_devtree()\n");
  1075. }
  1076. /**
  1077. * Indicates whether the root node has a given value in its
  1078. * compatible property.
  1079. */
  1080. int machine_is_compatible(const char *compat)
  1081. {
  1082. struct device_node *root;
  1083. int rc = 0;
  1084. root = of_find_node_by_path("/");
  1085. if (root) {
  1086. rc = of_device_is_compatible(root, compat);
  1087. of_node_put(root);
  1088. }
  1089. return rc;
  1090. }
  1091. EXPORT_SYMBOL(machine_is_compatible);
  1092. /*******
  1093. *
  1094. * New implementation of the OF "find" APIs, return a refcounted
  1095. * object, call of_node_put() when done. The device tree and list
  1096. * are protected by a rw_lock.
  1097. *
  1098. * Note that property management will need some locking as well,
  1099. * this isn't dealt with yet.
  1100. *
  1101. *******/
  1102. /**
  1103. * of_find_node_by_phandle - Find a node given a phandle
  1104. * @handle: phandle of the node to find
  1105. *
  1106. * Returns a node pointer with refcount incremented, use
  1107. * of_node_put() on it when done.
  1108. */
  1109. struct device_node *of_find_node_by_phandle(phandle handle)
  1110. {
  1111. struct device_node *np;
  1112. read_lock(&devtree_lock);
  1113. for (np = allnodes; np != 0; np = np->allnext)
  1114. if (np->linux_phandle == handle)
  1115. break;
  1116. of_node_get(np);
  1117. read_unlock(&devtree_lock);
  1118. return np;
  1119. }
  1120. EXPORT_SYMBOL(of_find_node_by_phandle);
  1121. /**
  1122. * of_find_next_cache_node - Find a node's subsidiary cache
  1123. * @np: node of type "cpu" or "cache"
  1124. *
  1125. * Returns a node pointer with refcount incremented, use
  1126. * of_node_put() on it when done. Caller should hold a reference
  1127. * to np.
  1128. */
  1129. struct device_node *of_find_next_cache_node(struct device_node *np)
  1130. {
  1131. struct device_node *child;
  1132. const phandle *handle;
  1133. handle = of_get_property(np, "l2-cache", NULL);
  1134. if (!handle)
  1135. handle = of_get_property(np, "next-level-cache", NULL);
  1136. if (handle)
  1137. return of_find_node_by_phandle(*handle);
  1138. /* OF on pmac has nodes instead of properties named "l2-cache"
  1139. * beneath CPU nodes.
  1140. */
  1141. if (!strcmp(np->type, "cpu"))
  1142. for_each_child_of_node(np, child)
  1143. if (!strcmp(child->type, "cache"))
  1144. return child;
  1145. return NULL;
  1146. }
  1147. /**
  1148. * of_node_get - Increment refcount of a node
  1149. * @node: Node to inc refcount, NULL is supported to
  1150. * simplify writing of callers
  1151. *
  1152. * Returns node.
  1153. */
  1154. struct device_node *of_node_get(struct device_node *node)
  1155. {
  1156. if (node)
  1157. kref_get(&node->kref);
  1158. return node;
  1159. }
  1160. EXPORT_SYMBOL(of_node_get);
  1161. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1162. {
  1163. return container_of(kref, struct device_node, kref);
  1164. }
  1165. /**
  1166. * of_node_release - release a dynamically allocated node
  1167. * @kref: kref element of the node to be released
  1168. *
  1169. * In of_node_put() this function is passed to kref_put()
  1170. * as the destructor.
  1171. */
  1172. static void of_node_release(struct kref *kref)
  1173. {
  1174. struct device_node *node = kref_to_device_node(kref);
  1175. struct property *prop = node->properties;
  1176. /* We should never be releasing nodes that haven't been detached. */
  1177. if (!of_node_check_flag(node, OF_DETACHED)) {
  1178. printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
  1179. dump_stack();
  1180. kref_init(&node->kref);
  1181. return;
  1182. }
  1183. if (!of_node_check_flag(node, OF_DYNAMIC))
  1184. return;
  1185. while (prop) {
  1186. struct property *next = prop->next;
  1187. kfree(prop->name);
  1188. kfree(prop->value);
  1189. kfree(prop);
  1190. prop = next;
  1191. if (!prop) {
  1192. prop = node->deadprops;
  1193. node->deadprops = NULL;
  1194. }
  1195. }
  1196. kfree(node->full_name);
  1197. kfree(node->data);
  1198. kfree(node);
  1199. }
  1200. /**
  1201. * of_node_put - Decrement refcount of a node
  1202. * @node: Node to dec refcount, NULL is supported to
  1203. * simplify writing of callers
  1204. *
  1205. */
  1206. void of_node_put(struct device_node *node)
  1207. {
  1208. if (node)
  1209. kref_put(&node->kref, of_node_release);
  1210. }
  1211. EXPORT_SYMBOL(of_node_put);
  1212. /*
  1213. * Plug a device node into the tree and global list.
  1214. */
  1215. void of_attach_node(struct device_node *np)
  1216. {
  1217. unsigned long flags;
  1218. write_lock_irqsave(&devtree_lock, flags);
  1219. np->sibling = np->parent->child;
  1220. np->allnext = allnodes;
  1221. np->parent->child = np;
  1222. allnodes = np;
  1223. write_unlock_irqrestore(&devtree_lock, flags);
  1224. }
  1225. /*
  1226. * "Unplug" a node from the device tree. The caller must hold
  1227. * a reference to the node. The memory associated with the node
  1228. * is not freed until its refcount goes to zero.
  1229. */
  1230. void of_detach_node(struct device_node *np)
  1231. {
  1232. struct device_node *parent;
  1233. unsigned long flags;
  1234. write_lock_irqsave(&devtree_lock, flags);
  1235. parent = np->parent;
  1236. if (!parent)
  1237. goto out_unlock;
  1238. if (allnodes == np)
  1239. allnodes = np->allnext;
  1240. else {
  1241. struct device_node *prev;
  1242. for (prev = allnodes;
  1243. prev->allnext != np;
  1244. prev = prev->allnext)
  1245. ;
  1246. prev->allnext = np->allnext;
  1247. }
  1248. if (parent->child == np)
  1249. parent->child = np->sibling;
  1250. else {
  1251. struct device_node *prevsib;
  1252. for (prevsib = np->parent->child;
  1253. prevsib->sibling != np;
  1254. prevsib = prevsib->sibling)
  1255. ;
  1256. prevsib->sibling = np->sibling;
  1257. }
  1258. of_node_set_flag(np, OF_DETACHED);
  1259. out_unlock:
  1260. write_unlock_irqrestore(&devtree_lock, flags);
  1261. }
  1262. #ifdef CONFIG_PPC_PSERIES
  1263. /*
  1264. * Fix up the uninitialized fields in a new device node:
  1265. * name, type and pci-specific fields
  1266. */
  1267. static int of_finish_dynamic_node(struct device_node *node)
  1268. {
  1269. struct device_node *parent = of_get_parent(node);
  1270. int err = 0;
  1271. const phandle *ibm_phandle;
  1272. node->name = of_get_property(node, "name", NULL);
  1273. node->type = of_get_property(node, "device_type", NULL);
  1274. if (!node->name)
  1275. node->name = "<NULL>";
  1276. if (!node->type)
  1277. node->type = "<NULL>";
  1278. if (!parent) {
  1279. err = -ENODEV;
  1280. goto out;
  1281. }
  1282. /* We don't support that function on PowerMac, at least
  1283. * not yet
  1284. */
  1285. if (machine_is(powermac))
  1286. return -ENODEV;
  1287. /* fix up new node's linux_phandle field */
  1288. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1289. node->linux_phandle = *ibm_phandle;
  1290. out:
  1291. of_node_put(parent);
  1292. return err;
  1293. }
  1294. static int prom_reconfig_notifier(struct notifier_block *nb,
  1295. unsigned long action, void *node)
  1296. {
  1297. int err;
  1298. switch (action) {
  1299. case PSERIES_RECONFIG_ADD:
  1300. err = of_finish_dynamic_node(node);
  1301. if (err < 0) {
  1302. printk(KERN_ERR "finish_node returned %d\n", err);
  1303. err = NOTIFY_BAD;
  1304. }
  1305. break;
  1306. default:
  1307. err = NOTIFY_DONE;
  1308. break;
  1309. }
  1310. return err;
  1311. }
  1312. static struct notifier_block prom_reconfig_nb = {
  1313. .notifier_call = prom_reconfig_notifier,
  1314. .priority = 10, /* This one needs to run first */
  1315. };
  1316. static int __init prom_reconfig_setup(void)
  1317. {
  1318. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1319. }
  1320. __initcall(prom_reconfig_setup);
  1321. #endif
  1322. /*
  1323. * Add a property to a node
  1324. */
  1325. int prom_add_property(struct device_node* np, struct property* prop)
  1326. {
  1327. struct property **next;
  1328. unsigned long flags;
  1329. prop->next = NULL;
  1330. write_lock_irqsave(&devtree_lock, flags);
  1331. next = &np->properties;
  1332. while (*next) {
  1333. if (strcmp(prop->name, (*next)->name) == 0) {
  1334. /* duplicate ! don't insert it */
  1335. write_unlock_irqrestore(&devtree_lock, flags);
  1336. return -1;
  1337. }
  1338. next = &(*next)->next;
  1339. }
  1340. *next = prop;
  1341. write_unlock_irqrestore(&devtree_lock, flags);
  1342. #ifdef CONFIG_PROC_DEVICETREE
  1343. /* try to add to proc as well if it was initialized */
  1344. if (np->pde)
  1345. proc_device_tree_add_prop(np->pde, prop);
  1346. #endif /* CONFIG_PROC_DEVICETREE */
  1347. return 0;
  1348. }
  1349. /*
  1350. * Remove a property from a node. Note that we don't actually
  1351. * remove it, since we have given out who-knows-how-many pointers
  1352. * to the data using get-property. Instead we just move the property
  1353. * to the "dead properties" list, so it won't be found any more.
  1354. */
  1355. int prom_remove_property(struct device_node *np, struct property *prop)
  1356. {
  1357. struct property **next;
  1358. unsigned long flags;
  1359. int found = 0;
  1360. write_lock_irqsave(&devtree_lock, flags);
  1361. next = &np->properties;
  1362. while (*next) {
  1363. if (*next == prop) {
  1364. /* found the node */
  1365. *next = prop->next;
  1366. prop->next = np->deadprops;
  1367. np->deadprops = prop;
  1368. found = 1;
  1369. break;
  1370. }
  1371. next = &(*next)->next;
  1372. }
  1373. write_unlock_irqrestore(&devtree_lock, flags);
  1374. if (!found)
  1375. return -ENODEV;
  1376. #ifdef CONFIG_PROC_DEVICETREE
  1377. /* try to remove the proc node as well */
  1378. if (np->pde)
  1379. proc_device_tree_remove_prop(np->pde, prop);
  1380. #endif /* CONFIG_PROC_DEVICETREE */
  1381. return 0;
  1382. }
  1383. /*
  1384. * Update a property in a node. Note that we don't actually
  1385. * remove it, since we have given out who-knows-how-many pointers
  1386. * to the data using get-property. Instead we just move the property
  1387. * to the "dead properties" list, and add the new property to the
  1388. * property list
  1389. */
  1390. int prom_update_property(struct device_node *np,
  1391. struct property *newprop,
  1392. struct property *oldprop)
  1393. {
  1394. struct property **next;
  1395. unsigned long flags;
  1396. int found = 0;
  1397. write_lock_irqsave(&devtree_lock, flags);
  1398. next = &np->properties;
  1399. while (*next) {
  1400. if (*next == oldprop) {
  1401. /* found the node */
  1402. newprop->next = oldprop->next;
  1403. *next = newprop;
  1404. oldprop->next = np->deadprops;
  1405. np->deadprops = oldprop;
  1406. found = 1;
  1407. break;
  1408. }
  1409. next = &(*next)->next;
  1410. }
  1411. write_unlock_irqrestore(&devtree_lock, flags);
  1412. if (!found)
  1413. return -ENODEV;
  1414. #ifdef CONFIG_PROC_DEVICETREE
  1415. /* try to add to proc as well if it was initialized */
  1416. if (np->pde)
  1417. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1418. #endif /* CONFIG_PROC_DEVICETREE */
  1419. return 0;
  1420. }
  1421. /* Find the device node for a given logical cpu number, also returns the cpu
  1422. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1423. * asked for (non NULL)
  1424. */
  1425. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1426. {
  1427. int hardid;
  1428. struct device_node *np;
  1429. hardid = get_hard_smp_processor_id(cpu);
  1430. for_each_node_by_type(np, "cpu") {
  1431. const u32 *intserv;
  1432. unsigned int plen, t;
  1433. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1434. * fallback to "reg" property and assume no threads
  1435. */
  1436. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1437. &plen);
  1438. if (intserv == NULL) {
  1439. const u32 *reg = of_get_property(np, "reg", NULL);
  1440. if (reg == NULL)
  1441. continue;
  1442. if (*reg == hardid) {
  1443. if (thread)
  1444. *thread = 0;
  1445. return np;
  1446. }
  1447. } else {
  1448. plen /= sizeof(u32);
  1449. for (t = 0; t < plen; t++) {
  1450. if (hardid == intserv[t]) {
  1451. if (thread)
  1452. *thread = t;
  1453. return np;
  1454. }
  1455. }
  1456. }
  1457. }
  1458. return NULL;
  1459. }
  1460. EXPORT_SYMBOL(of_get_cpu_node);
  1461. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  1462. static struct debugfs_blob_wrapper flat_dt_blob;
  1463. static int __init export_flat_device_tree(void)
  1464. {
  1465. struct dentry *d;
  1466. flat_dt_blob.data = initial_boot_params;
  1467. flat_dt_blob.size = initial_boot_params->totalsize;
  1468. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1469. powerpc_debugfs_root, &flat_dt_blob);
  1470. if (!d)
  1471. return 1;
  1472. return 0;
  1473. }
  1474. __initcall(export_flat_device_tree);
  1475. #endif