prom_64.c 15 KB

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