prom.c 36 KB

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