prom_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 char * __init get_one_property(phandle node, const char *name)
  338. {
  339. char *buf = "<NULL>";
  340. int len;
  341. len = prom_getproplen(node, name);
  342. if (len > 0) {
  343. buf = prom_early_alloc(len);
  344. prom_getproperty(node, name, buf, len);
  345. }
  346. return buf;
  347. }
  348. static struct device_node * __init create_node(phandle node, struct device_node *parent)
  349. {
  350. struct device_node *dp;
  351. if (!node)
  352. return NULL;
  353. dp = prom_early_alloc(sizeof(*dp));
  354. dp->unique_id = prom_unique_id++;
  355. dp->parent = parent;
  356. kref_init(&dp->kref);
  357. dp->name = get_one_property(node, "name");
  358. dp->type = get_one_property(node, "device_type");
  359. dp->node = node;
  360. dp->properties = build_prop_list(node);
  361. irq_trans_init(dp);
  362. return dp;
  363. }
  364. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  365. {
  366. struct device_node *ret = NULL, *prev_sibling = NULL;
  367. struct device_node *dp;
  368. while (1) {
  369. dp = create_node(node, parent);
  370. if (!dp)
  371. break;
  372. if (prev_sibling)
  373. prev_sibling->sibling = dp;
  374. if (!ret)
  375. ret = dp;
  376. prev_sibling = dp;
  377. *(*nextp) = dp;
  378. *nextp = &dp->allnext;
  379. dp->path_component_name = build_path_component(dp);
  380. dp->full_name = build_full_name(dp);
  381. dp->child = build_tree(dp, prom_getchild(node), nextp);
  382. node = prom_getsibling(node);
  383. }
  384. return ret;
  385. }
  386. static const char *get_mid_prop(void)
  387. {
  388. return (tlb_type == spitfire ? "upa-portid" : "portid");
  389. }
  390. struct device_node *of_find_node_by_cpuid(int cpuid)
  391. {
  392. struct device_node *dp;
  393. const char *mid_prop = get_mid_prop();
  394. for_each_node_by_type(dp, "cpu") {
  395. int id = of_getintprop_default(dp, mid_prop, -1);
  396. const char *this_mid_prop = mid_prop;
  397. if (id < 0) {
  398. this_mid_prop = "cpuid";
  399. id = of_getintprop_default(dp, this_mid_prop, -1);
  400. }
  401. if (id < 0) {
  402. prom_printf("OF: Serious problem, cpu lacks "
  403. "%s property", this_mid_prop);
  404. prom_halt();
  405. }
  406. if (cpuid == id)
  407. return dp;
  408. }
  409. return NULL;
  410. }
  411. static void __init of_fill_in_cpu_data(void)
  412. {
  413. struct device_node *dp;
  414. const char *mid_prop = get_mid_prop();
  415. ncpus_probed = 0;
  416. for_each_node_by_type(dp, "cpu") {
  417. int cpuid = of_getintprop_default(dp, mid_prop, -1);
  418. const char *this_mid_prop = mid_prop;
  419. struct device_node *portid_parent;
  420. int portid = -1;
  421. portid_parent = NULL;
  422. if (cpuid < 0) {
  423. this_mid_prop = "cpuid";
  424. cpuid = of_getintprop_default(dp, this_mid_prop, -1);
  425. if (cpuid >= 0) {
  426. int limit = 2;
  427. portid_parent = dp;
  428. while (limit--) {
  429. portid_parent = portid_parent->parent;
  430. if (!portid_parent)
  431. break;
  432. portid = of_getintprop_default(portid_parent,
  433. "portid", -1);
  434. if (portid >= 0)
  435. break;
  436. }
  437. }
  438. }
  439. if (cpuid < 0) {
  440. prom_printf("OF: Serious problem, cpu lacks "
  441. "%s property", this_mid_prop);
  442. prom_halt();
  443. }
  444. ncpus_probed++;
  445. #ifdef CONFIG_SMP
  446. if (cpuid >= NR_CPUS) {
  447. printk(KERN_WARNING "Ignoring CPU %d which is "
  448. ">= NR_CPUS (%d)\n",
  449. cpuid, NR_CPUS);
  450. continue;
  451. }
  452. #else
  453. /* On uniprocessor we only want the values for the
  454. * real physical cpu the kernel booted onto, however
  455. * cpu_data() only has one entry at index 0.
  456. */
  457. if (cpuid != real_hard_smp_processor_id())
  458. continue;
  459. cpuid = 0;
  460. #endif
  461. cpu_data(cpuid).clock_tick =
  462. of_getintprop_default(dp, "clock-frequency", 0);
  463. if (portid_parent) {
  464. cpu_data(cpuid).dcache_size =
  465. of_getintprop_default(dp, "l1-dcache-size",
  466. 16 * 1024);
  467. cpu_data(cpuid).dcache_line_size =
  468. of_getintprop_default(dp, "l1-dcache-line-size",
  469. 32);
  470. cpu_data(cpuid).icache_size =
  471. of_getintprop_default(dp, "l1-icache-size",
  472. 8 * 1024);
  473. cpu_data(cpuid).icache_line_size =
  474. of_getintprop_default(dp, "l1-icache-line-size",
  475. 32);
  476. cpu_data(cpuid).ecache_size =
  477. of_getintprop_default(dp, "l2-cache-size", 0);
  478. cpu_data(cpuid).ecache_line_size =
  479. of_getintprop_default(dp, "l2-cache-line-size", 0);
  480. if (!cpu_data(cpuid).ecache_size ||
  481. !cpu_data(cpuid).ecache_line_size) {
  482. cpu_data(cpuid).ecache_size =
  483. of_getintprop_default(portid_parent,
  484. "l2-cache-size",
  485. (4 * 1024 * 1024));
  486. cpu_data(cpuid).ecache_line_size =
  487. of_getintprop_default(portid_parent,
  488. "l2-cache-line-size", 64);
  489. }
  490. cpu_data(cpuid).core_id = portid + 1;
  491. cpu_data(cpuid).proc_id = portid;
  492. #ifdef CONFIG_SMP
  493. sparc64_multi_core = 1;
  494. #endif
  495. } else {
  496. cpu_data(cpuid).dcache_size =
  497. of_getintprop_default(dp, "dcache-size", 16 * 1024);
  498. cpu_data(cpuid).dcache_line_size =
  499. of_getintprop_default(dp, "dcache-line-size", 32);
  500. cpu_data(cpuid).icache_size =
  501. of_getintprop_default(dp, "icache-size", 16 * 1024);
  502. cpu_data(cpuid).icache_line_size =
  503. of_getintprop_default(dp, "icache-line-size", 32);
  504. cpu_data(cpuid).ecache_size =
  505. of_getintprop_default(dp, "ecache-size",
  506. (4 * 1024 * 1024));
  507. cpu_data(cpuid).ecache_line_size =
  508. of_getintprop_default(dp, "ecache-line-size", 64);
  509. cpu_data(cpuid).core_id = 0;
  510. cpu_data(cpuid).proc_id = -1;
  511. }
  512. #ifdef CONFIG_SMP
  513. cpu_set(cpuid, cpu_present_map);
  514. cpu_set(cpuid, cpu_possible_map);
  515. #endif
  516. }
  517. smp_fill_in_sib_core_maps();
  518. }
  519. struct device_node *of_console_device;
  520. EXPORT_SYMBOL(of_console_device);
  521. char *of_console_path;
  522. EXPORT_SYMBOL(of_console_path);
  523. char *of_console_options;
  524. EXPORT_SYMBOL(of_console_options);
  525. static void __init of_console_init(void)
  526. {
  527. char *msg = "OF stdout device is: %s\n";
  528. struct device_node *dp;
  529. const char *type;
  530. phandle node;
  531. of_console_path = prom_early_alloc(256);
  532. if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
  533. prom_printf("Cannot obtain path of stdout.\n");
  534. prom_halt();
  535. }
  536. of_console_options = strrchr(of_console_path, ':');
  537. if (of_console_options) {
  538. of_console_options++;
  539. if (*of_console_options == '\0')
  540. of_console_options = NULL;
  541. }
  542. node = prom_inst2pkg(prom_stdout);
  543. if (!node) {
  544. prom_printf("Cannot resolve stdout node from "
  545. "instance %08x.\n", prom_stdout);
  546. prom_halt();
  547. }
  548. dp = of_find_node_by_phandle(node);
  549. type = of_get_property(dp, "device_type", NULL);
  550. if (!type) {
  551. prom_printf("Console stdout lacks device_type property.\n");
  552. prom_halt();
  553. }
  554. if (strcmp(type, "display") && strcmp(type, "serial")) {
  555. prom_printf("Console device_type is neither display "
  556. "nor serial.\n");
  557. prom_halt();
  558. }
  559. of_console_device = dp;
  560. printk(msg, of_console_path);
  561. }
  562. void __init prom_build_devicetree(void)
  563. {
  564. struct device_node **nextp;
  565. allnodes = create_node(prom_root_node, NULL);
  566. allnodes->path_component_name = "";
  567. allnodes->full_name = "/";
  568. nextp = &allnodes->allnext;
  569. allnodes->child = build_tree(allnodes,
  570. prom_getchild(allnodes->node),
  571. &nextp);
  572. of_console_init();
  573. printk("PROM: Built device tree with %u bytes of memory.\n",
  574. prom_early_allocated);
  575. if (tlb_type != hypervisor)
  576. of_fill_in_cpu_data();
  577. }