prom_64.c 15 KB

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