prom.c 40 KB

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