prom_64.c 13 KB

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