prom_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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/memblock.h>
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/cpu.h>
  22. #include <linux/mm.h>
  23. #include <linux/of.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 = memblock_alloc(size, SMP_CACHE_BYTES);
  34. void *ret;
  35. if (!paddr) {
  36. prom_printf("prom_early_alloc(%lu) failed\n", size);
  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 (!of_node_is_root(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 (!of_node_is_root(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. bool arch_find_n_match_cpu_physical_id(struct device_node *cpun,
  319. int cpu, unsigned int *thread)
  320. {
  321. const char *mid_prop = get_mid_prop();
  322. int this_cpu_id;
  323. /* On hypervisor based platforms we interrogate the 'reg'
  324. * property. On everything else we look for a 'upa-portis',
  325. * 'portid', or 'cpuid' property.
  326. */
  327. if (tlb_type == hypervisor) {
  328. struct property *prop = of_find_property(cpun, "reg", NULL);
  329. u32 *regs;
  330. if (!prop) {
  331. pr_warn("CPU node missing reg property\n");
  332. return false;
  333. }
  334. regs = prop->value;
  335. this_cpu_id = regs[0] & 0x0fffffff;
  336. } else {
  337. this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
  338. if (this_cpu_id < 0) {
  339. mid_prop = "cpuid";
  340. this_cpu_id = of_getintprop_default(cpun, mid_prop, -1);
  341. }
  342. if (this_cpu_id < 0) {
  343. pr_warn("CPU node missing cpu ID property\n");
  344. return false;
  345. }
  346. }
  347. if (this_cpu_id == cpu) {
  348. if (thread) {
  349. int proc_id = cpu_data(cpu).proc_id;
  350. /* On sparc64, the cpu thread information is obtained
  351. * either from OBP or the machine description. We've
  352. * actually probed this information already long before
  353. * this interface gets called so instead of interrogating
  354. * both the OF node and the MDESC again, just use what
  355. * we discovered already.
  356. */
  357. if (proc_id < 0)
  358. proc_id = 0;
  359. *thread = proc_id;
  360. }
  361. return true;
  362. }
  363. return false;
  364. }
  365. static void *of_iterate_over_cpus(void *(*func)(struct device_node *, int, int), int arg)
  366. {
  367. struct device_node *dp;
  368. const char *mid_prop;
  369. mid_prop = get_mid_prop();
  370. for_each_node_by_type(dp, "cpu") {
  371. int cpuid = of_getintprop_default(dp, mid_prop, -1);
  372. const char *this_mid_prop = mid_prop;
  373. void *ret;
  374. if (cpuid < 0) {
  375. this_mid_prop = "cpuid";
  376. cpuid = of_getintprop_default(dp, this_mid_prop, -1);
  377. }
  378. if (cpuid < 0) {
  379. prom_printf("OF: Serious problem, cpu lacks "
  380. "%s property", this_mid_prop);
  381. prom_halt();
  382. }
  383. #ifdef CONFIG_SMP
  384. if (cpuid >= NR_CPUS) {
  385. printk(KERN_WARNING "Ignoring CPU %d which is "
  386. ">= NR_CPUS (%d)\n",
  387. cpuid, NR_CPUS);
  388. continue;
  389. }
  390. #endif
  391. ret = func(dp, cpuid, arg);
  392. if (ret)
  393. return ret;
  394. }
  395. return NULL;
  396. }
  397. static void *check_cpu_node(struct device_node *dp, int cpuid, int id)
  398. {
  399. if (id == cpuid)
  400. return dp;
  401. return NULL;
  402. }
  403. struct device_node *of_find_node_by_cpuid(int cpuid)
  404. {
  405. return of_iterate_over_cpus(check_cpu_node, cpuid);
  406. }
  407. static void *record_one_cpu(struct device_node *dp, int cpuid, int arg)
  408. {
  409. ncpus_probed++;
  410. #ifdef CONFIG_SMP
  411. set_cpu_present(cpuid, true);
  412. set_cpu_possible(cpuid, true);
  413. #endif
  414. return NULL;
  415. }
  416. void __init of_populate_present_mask(void)
  417. {
  418. if (tlb_type == hypervisor)
  419. return;
  420. ncpus_probed = 0;
  421. of_iterate_over_cpus(record_one_cpu, 0);
  422. }
  423. static void *fill_in_one_cpu(struct device_node *dp, int cpuid, int arg)
  424. {
  425. struct device_node *portid_parent = NULL;
  426. int portid = -1;
  427. if (of_find_property(dp, "cpuid", NULL)) {
  428. int limit = 2;
  429. portid_parent = dp;
  430. while (limit--) {
  431. portid_parent = portid_parent->parent;
  432. if (!portid_parent)
  433. break;
  434. portid = of_getintprop_default(portid_parent,
  435. "portid", -1);
  436. if (portid >= 0)
  437. break;
  438. }
  439. }
  440. #ifndef CONFIG_SMP
  441. /* On uniprocessor we only want the values for the
  442. * real physical cpu the kernel booted onto, however
  443. * cpu_data() only has one entry at index 0.
  444. */
  445. if (cpuid != real_hard_smp_processor_id())
  446. return NULL;
  447. cpuid = 0;
  448. #endif
  449. cpu_data(cpuid).clock_tick =
  450. of_getintprop_default(dp, "clock-frequency", 0);
  451. if (portid_parent) {
  452. cpu_data(cpuid).dcache_size =
  453. of_getintprop_default(dp, "l1-dcache-size",
  454. 16 * 1024);
  455. cpu_data(cpuid).dcache_line_size =
  456. of_getintprop_default(dp, "l1-dcache-line-size",
  457. 32);
  458. cpu_data(cpuid).icache_size =
  459. of_getintprop_default(dp, "l1-icache-size",
  460. 8 * 1024);
  461. cpu_data(cpuid).icache_line_size =
  462. of_getintprop_default(dp, "l1-icache-line-size",
  463. 32);
  464. cpu_data(cpuid).ecache_size =
  465. of_getintprop_default(dp, "l2-cache-size", 0);
  466. cpu_data(cpuid).ecache_line_size =
  467. of_getintprop_default(dp, "l2-cache-line-size", 0);
  468. if (!cpu_data(cpuid).ecache_size ||
  469. !cpu_data(cpuid).ecache_line_size) {
  470. cpu_data(cpuid).ecache_size =
  471. of_getintprop_default(portid_parent,
  472. "l2-cache-size",
  473. (4 * 1024 * 1024));
  474. cpu_data(cpuid).ecache_line_size =
  475. of_getintprop_default(portid_parent,
  476. "l2-cache-line-size", 64);
  477. }
  478. cpu_data(cpuid).core_id = portid + 1;
  479. cpu_data(cpuid).proc_id = portid;
  480. } else {
  481. cpu_data(cpuid).dcache_size =
  482. of_getintprop_default(dp, "dcache-size", 16 * 1024);
  483. cpu_data(cpuid).dcache_line_size =
  484. of_getintprop_default(dp, "dcache-line-size", 32);
  485. cpu_data(cpuid).icache_size =
  486. of_getintprop_default(dp, "icache-size", 16 * 1024);
  487. cpu_data(cpuid).icache_line_size =
  488. of_getintprop_default(dp, "icache-line-size", 32);
  489. cpu_data(cpuid).ecache_size =
  490. of_getintprop_default(dp, "ecache-size",
  491. (4 * 1024 * 1024));
  492. cpu_data(cpuid).ecache_line_size =
  493. of_getintprop_default(dp, "ecache-line-size", 64);
  494. cpu_data(cpuid).core_id = 0;
  495. cpu_data(cpuid).proc_id = -1;
  496. }
  497. return NULL;
  498. }
  499. void __init of_fill_in_cpu_data(void)
  500. {
  501. if (tlb_type == hypervisor)
  502. return;
  503. of_iterate_over_cpus(fill_in_one_cpu, 0);
  504. smp_fill_in_sib_core_maps();
  505. }
  506. void __init of_console_init(void)
  507. {
  508. char *msg = "OF stdout device is: %s\n";
  509. struct device_node *dp;
  510. const char *type;
  511. phandle node;
  512. of_console_path = prom_early_alloc(256);
  513. if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
  514. prom_printf("Cannot obtain path of stdout.\n");
  515. prom_halt();
  516. }
  517. of_console_options = strrchr(of_console_path, ':');
  518. if (of_console_options) {
  519. of_console_options++;
  520. if (*of_console_options == '\0')
  521. of_console_options = NULL;
  522. }
  523. node = prom_inst2pkg(prom_stdout);
  524. if (!node) {
  525. prom_printf("Cannot resolve stdout node from "
  526. "instance %08x.\n", prom_stdout);
  527. prom_halt();
  528. }
  529. dp = of_find_node_by_phandle(node);
  530. type = of_get_property(dp, "device_type", NULL);
  531. if (!type) {
  532. prom_printf("Console stdout lacks device_type property.\n");
  533. prom_halt();
  534. }
  535. if (strcmp(type, "display") && strcmp(type, "serial")) {
  536. prom_printf("Console device_type is neither display "
  537. "nor serial.\n");
  538. prom_halt();
  539. }
  540. of_console_device = dp;
  541. printk(msg, of_console_path);
  542. }