prom.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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_find_all_nodes - Get next node in global list
  1149. * @prev: Previous node or NULL to start iteration
  1150. * of_node_put() will be called on it
  1151. *
  1152. * Returns a node pointer with refcount incremented, use
  1153. * of_node_put() on it when done.
  1154. */
  1155. struct device_node *of_find_all_nodes(struct device_node *prev)
  1156. {
  1157. struct device_node *np;
  1158. read_lock(&devtree_lock);
  1159. np = prev ? prev->allnext : allnodes;
  1160. for (; np != 0; np = np->allnext)
  1161. if (of_node_get(np))
  1162. break;
  1163. of_node_put(prev);
  1164. read_unlock(&devtree_lock);
  1165. return np;
  1166. }
  1167. EXPORT_SYMBOL(of_find_all_nodes);
  1168. /**
  1169. * of_node_get - Increment refcount of a node
  1170. * @node: Node to inc refcount, NULL is supported to
  1171. * simplify writing of callers
  1172. *
  1173. * Returns node.
  1174. */
  1175. struct device_node *of_node_get(struct device_node *node)
  1176. {
  1177. if (node)
  1178. kref_get(&node->kref);
  1179. return node;
  1180. }
  1181. EXPORT_SYMBOL(of_node_get);
  1182. static inline struct device_node * kref_to_device_node(struct kref *kref)
  1183. {
  1184. return container_of(kref, struct device_node, kref);
  1185. }
  1186. /**
  1187. * of_node_release - release a dynamically allocated node
  1188. * @kref: kref element of the node to be released
  1189. *
  1190. * In of_node_put() this function is passed to kref_put()
  1191. * as the destructor.
  1192. */
  1193. static void of_node_release(struct kref *kref)
  1194. {
  1195. struct device_node *node = kref_to_device_node(kref);
  1196. struct property *prop = node->properties;
  1197. /* We should never be releasing nodes that haven't been detached. */
  1198. if (!of_node_check_flag(node, OF_DETACHED)) {
  1199. printk("WARNING: Bad of_node_put() on %s\n", node->full_name);
  1200. dump_stack();
  1201. kref_init(&node->kref);
  1202. return;
  1203. }
  1204. if (!of_node_check_flag(node, OF_DYNAMIC))
  1205. return;
  1206. while (prop) {
  1207. struct property *next = prop->next;
  1208. kfree(prop->name);
  1209. kfree(prop->value);
  1210. kfree(prop);
  1211. prop = next;
  1212. if (!prop) {
  1213. prop = node->deadprops;
  1214. node->deadprops = NULL;
  1215. }
  1216. }
  1217. kfree(node->full_name);
  1218. kfree(node->data);
  1219. kfree(node);
  1220. }
  1221. /**
  1222. * of_node_put - Decrement refcount of a node
  1223. * @node: Node to dec refcount, NULL is supported to
  1224. * simplify writing of callers
  1225. *
  1226. */
  1227. void of_node_put(struct device_node *node)
  1228. {
  1229. if (node)
  1230. kref_put(&node->kref, of_node_release);
  1231. }
  1232. EXPORT_SYMBOL(of_node_put);
  1233. /*
  1234. * Plug a device node into the tree and global list.
  1235. */
  1236. void of_attach_node(struct device_node *np)
  1237. {
  1238. unsigned long flags;
  1239. write_lock_irqsave(&devtree_lock, flags);
  1240. np->sibling = np->parent->child;
  1241. np->allnext = allnodes;
  1242. np->parent->child = np;
  1243. allnodes = np;
  1244. write_unlock_irqrestore(&devtree_lock, flags);
  1245. }
  1246. /*
  1247. * "Unplug" a node from the device tree. The caller must hold
  1248. * a reference to the node. The memory associated with the node
  1249. * is not freed until its refcount goes to zero.
  1250. */
  1251. void of_detach_node(struct device_node *np)
  1252. {
  1253. struct device_node *parent;
  1254. unsigned long flags;
  1255. write_lock_irqsave(&devtree_lock, flags);
  1256. parent = np->parent;
  1257. if (!parent)
  1258. goto out_unlock;
  1259. if (allnodes == np)
  1260. allnodes = np->allnext;
  1261. else {
  1262. struct device_node *prev;
  1263. for (prev = allnodes;
  1264. prev->allnext != np;
  1265. prev = prev->allnext)
  1266. ;
  1267. prev->allnext = np->allnext;
  1268. }
  1269. if (parent->child == np)
  1270. parent->child = np->sibling;
  1271. else {
  1272. struct device_node *prevsib;
  1273. for (prevsib = np->parent->child;
  1274. prevsib->sibling != np;
  1275. prevsib = prevsib->sibling)
  1276. ;
  1277. prevsib->sibling = np->sibling;
  1278. }
  1279. of_node_set_flag(np, OF_DETACHED);
  1280. out_unlock:
  1281. write_unlock_irqrestore(&devtree_lock, flags);
  1282. }
  1283. #ifdef CONFIG_PPC_PSERIES
  1284. /*
  1285. * Fix up the uninitialized fields in a new device node:
  1286. * name, type and pci-specific fields
  1287. */
  1288. static int of_finish_dynamic_node(struct device_node *node)
  1289. {
  1290. struct device_node *parent = of_get_parent(node);
  1291. int err = 0;
  1292. const phandle *ibm_phandle;
  1293. node->name = of_get_property(node, "name", NULL);
  1294. node->type = of_get_property(node, "device_type", NULL);
  1295. if (!node->name)
  1296. node->name = "<NULL>";
  1297. if (!node->type)
  1298. node->type = "<NULL>";
  1299. if (!parent) {
  1300. err = -ENODEV;
  1301. goto out;
  1302. }
  1303. /* We don't support that function on PowerMac, at least
  1304. * not yet
  1305. */
  1306. if (machine_is(powermac))
  1307. return -ENODEV;
  1308. /* fix up new node's linux_phandle field */
  1309. if ((ibm_phandle = of_get_property(node, "ibm,phandle", NULL)))
  1310. node->linux_phandle = *ibm_phandle;
  1311. out:
  1312. of_node_put(parent);
  1313. return err;
  1314. }
  1315. static int prom_reconfig_notifier(struct notifier_block *nb,
  1316. unsigned long action, void *node)
  1317. {
  1318. int err;
  1319. switch (action) {
  1320. case PSERIES_RECONFIG_ADD:
  1321. err = of_finish_dynamic_node(node);
  1322. if (err < 0) {
  1323. printk(KERN_ERR "finish_node returned %d\n", err);
  1324. err = NOTIFY_BAD;
  1325. }
  1326. break;
  1327. default:
  1328. err = NOTIFY_DONE;
  1329. break;
  1330. }
  1331. return err;
  1332. }
  1333. static struct notifier_block prom_reconfig_nb = {
  1334. .notifier_call = prom_reconfig_notifier,
  1335. .priority = 10, /* This one needs to run first */
  1336. };
  1337. static int __init prom_reconfig_setup(void)
  1338. {
  1339. return pSeries_reconfig_notifier_register(&prom_reconfig_nb);
  1340. }
  1341. __initcall(prom_reconfig_setup);
  1342. #endif
  1343. /*
  1344. * Add a property to a node
  1345. */
  1346. int prom_add_property(struct device_node* np, struct property* prop)
  1347. {
  1348. struct property **next;
  1349. unsigned long flags;
  1350. prop->next = NULL;
  1351. write_lock_irqsave(&devtree_lock, flags);
  1352. next = &np->properties;
  1353. while (*next) {
  1354. if (strcmp(prop->name, (*next)->name) == 0) {
  1355. /* duplicate ! don't insert it */
  1356. write_unlock_irqrestore(&devtree_lock, flags);
  1357. return -1;
  1358. }
  1359. next = &(*next)->next;
  1360. }
  1361. *next = prop;
  1362. write_unlock_irqrestore(&devtree_lock, flags);
  1363. #ifdef CONFIG_PROC_DEVICETREE
  1364. /* try to add to proc as well if it was initialized */
  1365. if (np->pde)
  1366. proc_device_tree_add_prop(np->pde, prop);
  1367. #endif /* CONFIG_PROC_DEVICETREE */
  1368. return 0;
  1369. }
  1370. /*
  1371. * Remove a property from a node. Note that we don't actually
  1372. * remove it, since we have given out who-knows-how-many pointers
  1373. * to the data using get-property. Instead we just move the property
  1374. * to the "dead properties" list, so it won't be found any more.
  1375. */
  1376. int prom_remove_property(struct device_node *np, struct property *prop)
  1377. {
  1378. struct property **next;
  1379. unsigned long flags;
  1380. int found = 0;
  1381. write_lock_irqsave(&devtree_lock, flags);
  1382. next = &np->properties;
  1383. while (*next) {
  1384. if (*next == prop) {
  1385. /* found the node */
  1386. *next = prop->next;
  1387. prop->next = np->deadprops;
  1388. np->deadprops = prop;
  1389. found = 1;
  1390. break;
  1391. }
  1392. next = &(*next)->next;
  1393. }
  1394. write_unlock_irqrestore(&devtree_lock, flags);
  1395. if (!found)
  1396. return -ENODEV;
  1397. #ifdef CONFIG_PROC_DEVICETREE
  1398. /* try to remove the proc node as well */
  1399. if (np->pde)
  1400. proc_device_tree_remove_prop(np->pde, prop);
  1401. #endif /* CONFIG_PROC_DEVICETREE */
  1402. return 0;
  1403. }
  1404. /*
  1405. * Update a property in a node. Note that we don't actually
  1406. * remove it, since we have given out who-knows-how-many pointers
  1407. * to the data using get-property. Instead we just move the property
  1408. * to the "dead properties" list, and add the new property to the
  1409. * property list
  1410. */
  1411. int prom_update_property(struct device_node *np,
  1412. struct property *newprop,
  1413. struct property *oldprop)
  1414. {
  1415. struct property **next;
  1416. unsigned long flags;
  1417. int found = 0;
  1418. write_lock_irqsave(&devtree_lock, flags);
  1419. next = &np->properties;
  1420. while (*next) {
  1421. if (*next == oldprop) {
  1422. /* found the node */
  1423. newprop->next = oldprop->next;
  1424. *next = newprop;
  1425. oldprop->next = np->deadprops;
  1426. np->deadprops = oldprop;
  1427. found = 1;
  1428. break;
  1429. }
  1430. next = &(*next)->next;
  1431. }
  1432. write_unlock_irqrestore(&devtree_lock, flags);
  1433. if (!found)
  1434. return -ENODEV;
  1435. #ifdef CONFIG_PROC_DEVICETREE
  1436. /* try to add to proc as well if it was initialized */
  1437. if (np->pde)
  1438. proc_device_tree_update_prop(np->pde, newprop, oldprop);
  1439. #endif /* CONFIG_PROC_DEVICETREE */
  1440. return 0;
  1441. }
  1442. /* Find the device node for a given logical cpu number, also returns the cpu
  1443. * local thread number (index in ibm,interrupt-server#s) if relevant and
  1444. * asked for (non NULL)
  1445. */
  1446. struct device_node *of_get_cpu_node(int cpu, unsigned int *thread)
  1447. {
  1448. int hardid;
  1449. struct device_node *np;
  1450. hardid = get_hard_smp_processor_id(cpu);
  1451. for_each_node_by_type(np, "cpu") {
  1452. const u32 *intserv;
  1453. unsigned int plen, t;
  1454. /* Check for ibm,ppc-interrupt-server#s. If it doesn't exist
  1455. * fallback to "reg" property and assume no threads
  1456. */
  1457. intserv = of_get_property(np, "ibm,ppc-interrupt-server#s",
  1458. &plen);
  1459. if (intserv == NULL) {
  1460. const u32 *reg = of_get_property(np, "reg", NULL);
  1461. if (reg == NULL)
  1462. continue;
  1463. if (*reg == hardid) {
  1464. if (thread)
  1465. *thread = 0;
  1466. return np;
  1467. }
  1468. } else {
  1469. plen /= sizeof(u32);
  1470. for (t = 0; t < plen; t++) {
  1471. if (hardid == intserv[t]) {
  1472. if (thread)
  1473. *thread = t;
  1474. return np;
  1475. }
  1476. }
  1477. }
  1478. }
  1479. return NULL;
  1480. }
  1481. EXPORT_SYMBOL(of_get_cpu_node);
  1482. #if defined(CONFIG_DEBUG_FS) && defined(DEBUG)
  1483. static struct debugfs_blob_wrapper flat_dt_blob;
  1484. static int __init export_flat_device_tree(void)
  1485. {
  1486. struct dentry *d;
  1487. flat_dt_blob.data = initial_boot_params;
  1488. flat_dt_blob.size = initial_boot_params->totalsize;
  1489. d = debugfs_create_blob("flat-device-tree", S_IFREG | S_IRUSR,
  1490. powerpc_debugfs_root, &flat_dt_blob);
  1491. if (!d)
  1492. return 1;
  1493. return 0;
  1494. }
  1495. __initcall(export_flat_device_tree);
  1496. #endif