prom_64.c 13 KB

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