prom_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. * Adapted for sparc64 by David S. Miller davem@davemloft.net
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/lmb.h>
  23. #include <linux/of_device.h>
  24. #include <asm/prom.h>
  25. #include <asm/oplib.h>
  26. #include <asm/irq.h>
  27. #include <asm/asi.h>
  28. #include <asm/upa.h>
  29. #include <asm/smp.h>
  30. #include "prom.h"
  31. static unsigned int prom_early_allocated __initdata;
  32. void * __init prom_early_alloc(unsigned long size)
  33. {
  34. unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
  35. void *ret;
  36. if (!paddr) {
  37. prom_printf("prom_early_alloc(%lu) failed\n");
  38. prom_halt();
  39. }
  40. ret = __va(paddr);
  41. memset(ret, 0, size);
  42. prom_early_allocated += size;
  43. return ret;
  44. }
  45. static int is_root_node(const struct device_node *dp)
  46. {
  47. if (!dp)
  48. return 0;
  49. return (dp->parent == NULL);
  50. }
  51. /* The following routines deal with the black magic of fully naming a
  52. * node.
  53. *
  54. * Certain well known named nodes are just the simple name string.
  55. *
  56. * Actual devices have an address specifier appended to the base name
  57. * string, like this "foo@addr". The "addr" can be in any number of
  58. * formats, and the platform plus the type of the node determine the
  59. * format and how it is constructed.
  60. *
  61. * For children of the ROOT node, the naming convention is fixed and
  62. * determined by whether this is a sun4u or sun4v system.
  63. *
  64. * For children of other nodes, it is bus type specific. So
  65. * we walk up the tree until we discover a "device_type" property
  66. * we recognize and we go from there.
  67. *
  68. * As an example, the boot device on my workstation has a full path:
  69. *
  70. * /pci@1e,600000/ide@d/disk@0,0:c
  71. */
  72. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  73. {
  74. struct linux_prom64_registers *regs;
  75. struct property *rprop;
  76. u32 high_bits, low_bits, type;
  77. rprop = of_find_property(dp, "reg", NULL);
  78. if (!rprop)
  79. return;
  80. regs = rprop->value;
  81. if (!is_root_node(dp->parent)) {
  82. sprintf(tmp_buf, "%s@%x,%x",
  83. dp->name,
  84. (unsigned int) (regs->phys_addr >> 32UL),
  85. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  86. return;
  87. }
  88. type = regs->phys_addr >> 60UL;
  89. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  90. low_bits = (regs->phys_addr & 0xffffffffUL);
  91. if (type == 0 || type == 8) {
  92. const char *prefix = (type == 0) ? "m" : "i";
  93. if (low_bits)
  94. sprintf(tmp_buf, "%s@%s%x,%x",
  95. dp->name, prefix,
  96. high_bits, low_bits);
  97. else
  98. sprintf(tmp_buf, "%s@%s%x",
  99. dp->name,
  100. prefix,
  101. high_bits);
  102. } else if (type == 12) {
  103. sprintf(tmp_buf, "%s@%x",
  104. dp->name, high_bits);
  105. }
  106. }
  107. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  108. {
  109. struct linux_prom64_registers *regs;
  110. struct property *prop;
  111. prop = of_find_property(dp, "reg", NULL);
  112. if (!prop)
  113. return;
  114. regs = prop->value;
  115. if (!is_root_node(dp->parent)) {
  116. sprintf(tmp_buf, "%s@%x,%x",
  117. dp->name,
  118. (unsigned int) (regs->phys_addr >> 32UL),
  119. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  120. return;
  121. }
  122. prop = of_find_property(dp, "upa-portid", NULL);
  123. if (!prop)
  124. prop = of_find_property(dp, "portid", NULL);
  125. if (prop) {
  126. unsigned long mask = 0xffffffffUL;
  127. if (tlb_type >= cheetah)
  128. mask = 0x7fffff;
  129. sprintf(tmp_buf, "%s@%x,%x",
  130. dp->name,
  131. *(u32 *)prop->value,
  132. (unsigned int) (regs->phys_addr & mask));
  133. }
  134. }
  135. /* "name@slot,offset" */
  136. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  137. {
  138. struct linux_prom_registers *regs;
  139. struct property *prop;
  140. prop = of_find_property(dp, "reg", NULL);
  141. if (!prop)
  142. return;
  143. regs = prop->value;
  144. sprintf(tmp_buf, "%s@%x,%x",
  145. dp->name,
  146. regs->which_io,
  147. regs->phys_addr);
  148. }
  149. /* "name@devnum[,func]" */
  150. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  151. {
  152. struct linux_prom_pci_registers *regs;
  153. struct property *prop;
  154. unsigned int devfn;
  155. prop = of_find_property(dp, "reg", NULL);
  156. if (!prop)
  157. return;
  158. regs = prop->value;
  159. devfn = (regs->phys_hi >> 8) & 0xff;
  160. if (devfn & 0x07) {
  161. sprintf(tmp_buf, "%s@%x,%x",
  162. dp->name,
  163. devfn >> 3,
  164. devfn & 0x07);
  165. } else {
  166. sprintf(tmp_buf, "%s@%x",
  167. dp->name,
  168. devfn >> 3);
  169. }
  170. }
  171. /* "name@UPA_PORTID,offset" */
  172. static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
  173. {
  174. struct linux_prom64_registers *regs;
  175. struct property *prop;
  176. prop = of_find_property(dp, "reg", NULL);
  177. if (!prop)
  178. return;
  179. regs = prop->value;
  180. prop = of_find_property(dp, "upa-portid", NULL);
  181. if (!prop)
  182. return;
  183. sprintf(tmp_buf, "%s@%x,%x",
  184. dp->name,
  185. *(u32 *) prop->value,
  186. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  187. }
  188. /* "name@reg" */
  189. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  190. {
  191. struct property *prop;
  192. u32 *regs;
  193. prop = of_find_property(dp, "reg", NULL);
  194. if (!prop)
  195. return;
  196. regs = prop->value;
  197. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  198. }
  199. /* "name@addrhi,addrlo" */
  200. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  201. {
  202. struct linux_prom64_registers *regs;
  203. struct property *prop;
  204. prop = of_find_property(dp, "reg", NULL);
  205. if (!prop)
  206. return;
  207. regs = prop->value;
  208. sprintf(tmp_buf, "%s@%x,%x",
  209. dp->name,
  210. (unsigned int) (regs->phys_addr >> 32UL),
  211. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  212. }
  213. /* "name@bus,addr" */
  214. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  215. {
  216. struct property *prop;
  217. u32 *regs;
  218. prop = of_find_property(dp, "reg", NULL);
  219. if (!prop)
  220. return;
  221. regs = prop->value;
  222. /* This actually isn't right... should look at the #address-cells
  223. * property of the i2c bus node etc. etc.
  224. */
  225. sprintf(tmp_buf, "%s@%x,%x",
  226. dp->name, regs[0], regs[1]);
  227. }
  228. /* "name@reg0[,reg1]" */
  229. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  230. {
  231. struct property *prop;
  232. u32 *regs;
  233. prop = of_find_property(dp, "reg", NULL);
  234. if (!prop)
  235. return;
  236. regs = prop->value;
  237. if (prop->length == sizeof(u32) || regs[1] == 1) {
  238. sprintf(tmp_buf, "%s@%x",
  239. dp->name, regs[0]);
  240. } else {
  241. sprintf(tmp_buf, "%s@%x,%x",
  242. dp->name, regs[0], regs[1]);
  243. }
  244. }
  245. /* "name@reg0reg1[,reg2reg3]" */
  246. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  247. {
  248. struct property *prop;
  249. u32 *regs;
  250. prop = of_find_property(dp, "reg", NULL);
  251. if (!prop)
  252. return;
  253. regs = prop->value;
  254. if (regs[2] || regs[3]) {
  255. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  256. dp->name, regs[0], regs[1], regs[2], regs[3]);
  257. } else {
  258. sprintf(tmp_buf, "%s@%08x%08x",
  259. dp->name, regs[0], regs[1]);
  260. }
  261. }
  262. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  263. {
  264. struct device_node *parent = dp->parent;
  265. if (parent != NULL) {
  266. if (!strcmp(parent->type, "pci") ||
  267. !strcmp(parent->type, "pciex")) {
  268. pci_path_component(dp, tmp_buf);
  269. return;
  270. }
  271. if (!strcmp(parent->type, "sbus")) {
  272. sbus_path_component(dp, tmp_buf);
  273. return;
  274. }
  275. if (!strcmp(parent->type, "upa")) {
  276. upa_path_component(dp, tmp_buf);
  277. return;
  278. }
  279. if (!strcmp(parent->type, "ebus")) {
  280. ebus_path_component(dp, tmp_buf);
  281. return;
  282. }
  283. if (!strcmp(parent->name, "usb") ||
  284. !strcmp(parent->name, "hub")) {
  285. usb_path_component(dp, tmp_buf);
  286. return;
  287. }
  288. if (!strcmp(parent->type, "i2c")) {
  289. i2c_path_component(dp, tmp_buf);
  290. return;
  291. }
  292. if (!strcmp(parent->type, "firewire")) {
  293. ieee1394_path_component(dp, tmp_buf);
  294. return;
  295. }
  296. if (!strcmp(parent->type, "virtual-devices")) {
  297. vdev_path_component(dp, tmp_buf);
  298. return;
  299. }
  300. /* "isa" is handled with platform naming */
  301. }
  302. /* Use platform naming convention. */
  303. if (tlb_type == hypervisor) {
  304. sun4v_path_component(dp, tmp_buf);
  305. return;
  306. } else {
  307. sun4u_path_component(dp, tmp_buf);
  308. }
  309. }
  310. static char * __init build_path_component(struct device_node *dp)
  311. {
  312. char tmp_buf[64], *n;
  313. tmp_buf[0] = '\0';
  314. __build_path_component(dp, tmp_buf);
  315. if (tmp_buf[0] == '\0')
  316. strcpy(tmp_buf, dp->name);
  317. n = prom_early_alloc(strlen(tmp_buf) + 1);
  318. strcpy(n, tmp_buf);
  319. return n;
  320. }
  321. static char * __init build_full_name(struct device_node *dp)
  322. {
  323. int len, ourlen, plen;
  324. char *n;
  325. plen = strlen(dp->parent->full_name);
  326. ourlen = strlen(dp->path_component_name);
  327. len = ourlen + plen + 2;
  328. n = prom_early_alloc(len);
  329. strcpy(n, dp->parent->full_name);
  330. if (!is_root_node(dp->parent)) {
  331. strcpy(n + plen, "/");
  332. plen++;
  333. }
  334. strcpy(n + plen, dp->path_component_name);
  335. return n;
  336. }
  337. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  338. {
  339. struct device_node *ret = NULL, *prev_sibling = NULL;
  340. struct device_node *dp;
  341. while (1) {
  342. dp = create_node(node, parent);
  343. if (!dp)
  344. break;
  345. if (prev_sibling)
  346. prev_sibling->sibling = dp;
  347. if (!ret)
  348. ret = dp;
  349. prev_sibling = dp;
  350. *(*nextp) = dp;
  351. *nextp = &dp->allnext;
  352. dp->path_component_name = build_path_component(dp);
  353. dp->full_name = build_full_name(dp);
  354. dp->child = build_tree(dp, prom_getchild(node), nextp);
  355. node = prom_getsibling(node);
  356. }
  357. return ret;
  358. }
  359. static const char *get_mid_prop(void)
  360. {
  361. return (tlb_type == spitfire ? "upa-portid" : "portid");
  362. }
  363. struct device_node *of_find_node_by_cpuid(int cpuid)
  364. {
  365. struct device_node *dp;
  366. const char *mid_prop = get_mid_prop();
  367. for_each_node_by_type(dp, "cpu") {
  368. int id = of_getintprop_default(dp, mid_prop, -1);
  369. const char *this_mid_prop = mid_prop;
  370. if (id < 0) {
  371. this_mid_prop = "cpuid";
  372. id = of_getintprop_default(dp, this_mid_prop, -1);
  373. }
  374. if (id < 0) {
  375. prom_printf("OF: Serious problem, cpu lacks "
  376. "%s property", this_mid_prop);
  377. prom_halt();
  378. }
  379. if (cpuid == id)
  380. return dp;
  381. }
  382. return NULL;
  383. }
  384. static void __init of_fill_in_cpu_data(void)
  385. {
  386. struct device_node *dp;
  387. const char *mid_prop = get_mid_prop();
  388. ncpus_probed = 0;
  389. for_each_node_by_type(dp, "cpu") {
  390. int cpuid = of_getintprop_default(dp, mid_prop, -1);
  391. const char *this_mid_prop = mid_prop;
  392. struct device_node *portid_parent;
  393. int portid = -1;
  394. portid_parent = NULL;
  395. if (cpuid < 0) {
  396. this_mid_prop = "cpuid";
  397. cpuid = of_getintprop_default(dp, this_mid_prop, -1);
  398. if (cpuid >= 0) {
  399. int limit = 2;
  400. portid_parent = dp;
  401. while (limit--) {
  402. portid_parent = portid_parent->parent;
  403. if (!portid_parent)
  404. break;
  405. portid = of_getintprop_default(portid_parent,
  406. "portid", -1);
  407. if (portid >= 0)
  408. break;
  409. }
  410. }
  411. }
  412. if (cpuid < 0) {
  413. prom_printf("OF: Serious problem, cpu lacks "
  414. "%s property", this_mid_prop);
  415. prom_halt();
  416. }
  417. ncpus_probed++;
  418. #ifdef CONFIG_SMP
  419. if (cpuid >= NR_CPUS) {
  420. printk(KERN_WARNING "Ignoring CPU %d which is "
  421. ">= NR_CPUS (%d)\n",
  422. cpuid, NR_CPUS);
  423. continue;
  424. }
  425. #else
  426. /* On uniprocessor we only want the values for the
  427. * real physical cpu the kernel booted onto, however
  428. * cpu_data() only has one entry at index 0.
  429. */
  430. if (cpuid != real_hard_smp_processor_id())
  431. continue;
  432. cpuid = 0;
  433. #endif
  434. cpu_data(cpuid).clock_tick =
  435. of_getintprop_default(dp, "clock-frequency", 0);
  436. if (portid_parent) {
  437. cpu_data(cpuid).dcache_size =
  438. of_getintprop_default(dp, "l1-dcache-size",
  439. 16 * 1024);
  440. cpu_data(cpuid).dcache_line_size =
  441. of_getintprop_default(dp, "l1-dcache-line-size",
  442. 32);
  443. cpu_data(cpuid).icache_size =
  444. of_getintprop_default(dp, "l1-icache-size",
  445. 8 * 1024);
  446. cpu_data(cpuid).icache_line_size =
  447. of_getintprop_default(dp, "l1-icache-line-size",
  448. 32);
  449. cpu_data(cpuid).ecache_size =
  450. of_getintprop_default(dp, "l2-cache-size", 0);
  451. cpu_data(cpuid).ecache_line_size =
  452. of_getintprop_default(dp, "l2-cache-line-size", 0);
  453. if (!cpu_data(cpuid).ecache_size ||
  454. !cpu_data(cpuid).ecache_line_size) {
  455. cpu_data(cpuid).ecache_size =
  456. of_getintprop_default(portid_parent,
  457. "l2-cache-size",
  458. (4 * 1024 * 1024));
  459. cpu_data(cpuid).ecache_line_size =
  460. of_getintprop_default(portid_parent,
  461. "l2-cache-line-size", 64);
  462. }
  463. cpu_data(cpuid).core_id = portid + 1;
  464. cpu_data(cpuid).proc_id = portid;
  465. #ifdef CONFIG_SMP
  466. sparc64_multi_core = 1;
  467. #endif
  468. } else {
  469. cpu_data(cpuid).dcache_size =
  470. of_getintprop_default(dp, "dcache-size", 16 * 1024);
  471. cpu_data(cpuid).dcache_line_size =
  472. of_getintprop_default(dp, "dcache-line-size", 32);
  473. cpu_data(cpuid).icache_size =
  474. of_getintprop_default(dp, "icache-size", 16 * 1024);
  475. cpu_data(cpuid).icache_line_size =
  476. of_getintprop_default(dp, "icache-line-size", 32);
  477. cpu_data(cpuid).ecache_size =
  478. of_getintprop_default(dp, "ecache-size",
  479. (4 * 1024 * 1024));
  480. cpu_data(cpuid).ecache_line_size =
  481. of_getintprop_default(dp, "ecache-line-size", 64);
  482. cpu_data(cpuid).core_id = 0;
  483. cpu_data(cpuid).proc_id = -1;
  484. }
  485. #ifdef CONFIG_SMP
  486. cpu_set(cpuid, cpu_present_map);
  487. cpu_set(cpuid, cpu_possible_map);
  488. #endif
  489. }
  490. smp_fill_in_sib_core_maps();
  491. }
  492. struct device_node *of_console_device;
  493. EXPORT_SYMBOL(of_console_device);
  494. char *of_console_path;
  495. EXPORT_SYMBOL(of_console_path);
  496. char *of_console_options;
  497. EXPORT_SYMBOL(of_console_options);
  498. static void __init of_console_init(void)
  499. {
  500. char *msg = "OF stdout device is: %s\n";
  501. struct device_node *dp;
  502. const char *type;
  503. phandle node;
  504. of_console_path = prom_early_alloc(256);
  505. if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
  506. prom_printf("Cannot obtain path of stdout.\n");
  507. prom_halt();
  508. }
  509. of_console_options = strrchr(of_console_path, ':');
  510. if (of_console_options) {
  511. of_console_options++;
  512. if (*of_console_options == '\0')
  513. of_console_options = NULL;
  514. }
  515. node = prom_inst2pkg(prom_stdout);
  516. if (!node) {
  517. prom_printf("Cannot resolve stdout node from "
  518. "instance %08x.\n", prom_stdout);
  519. prom_halt();
  520. }
  521. dp = of_find_node_by_phandle(node);
  522. type = of_get_property(dp, "device_type", NULL);
  523. if (!type) {
  524. prom_printf("Console stdout lacks device_type property.\n");
  525. prom_halt();
  526. }
  527. if (strcmp(type, "display") && strcmp(type, "serial")) {
  528. prom_printf("Console device_type is neither display "
  529. "nor serial.\n");
  530. prom_halt();
  531. }
  532. of_console_device = dp;
  533. printk(msg, of_console_path);
  534. }
  535. void __init prom_build_devicetree(void)
  536. {
  537. struct device_node **nextp;
  538. allnodes = create_node(prom_root_node, NULL);
  539. allnodes->path_component_name = "";
  540. allnodes->full_name = "/";
  541. nextp = &allnodes->allnext;
  542. allnodes->child = build_tree(allnodes,
  543. prom_getchild(allnodes->node),
  544. &nextp);
  545. of_console_init();
  546. printk("PROM: Built device tree with %u bytes of memory.\n",
  547. prom_early_allocated);
  548. if (tlb_type != hypervisor)
  549. of_fill_in_cpu_data();
  550. }