devicetree.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Architecture specific OF callbacks.
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/export.h>
  7. #include <linux/io.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/list.h>
  10. #include <linux/of.h>
  11. #include <linux/of_fdt.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/libfdt.h>
  16. #include <linux/slab.h>
  17. #include <linux/pci.h>
  18. #include <linux/of_pci.h>
  19. #include <linux/initrd.h>
  20. #include <asm/irqdomain.h>
  21. #include <asm/hpet.h>
  22. #include <asm/apic.h>
  23. #include <asm/io_apic.h>
  24. #include <asm/pci_x86.h>
  25. #include <asm/setup.h>
  26. #include <asm/i8259.h>
  27. #include <asm/numa.h>
  28. #include <asm/prom.h>
  29. __initdata u64 initial_dtb;
  30. char __initdata cmd_line[COMMAND_LINE_SIZE];
  31. int __initdata of_ioapic;
  32. void __init add_dtb(u64 data)
  33. {
  34. initial_dtb = data + offsetof(struct setup_data, data);
  35. }
  36. /*
  37. * CE4100 ids. Will be moved to machine_device_initcall() once we have it.
  38. */
  39. static struct of_device_id __initdata ce4100_ids[] = {
  40. { .compatible = "intel,ce4100-cp", },
  41. { .compatible = "isa", },
  42. { .compatible = "pci", },
  43. {},
  44. };
  45. static int __init add_bus_probe(void)
  46. {
  47. if (!of_have_populated_dt())
  48. return 0;
  49. return of_platform_bus_probe(NULL, ce4100_ids, NULL);
  50. }
  51. device_initcall(add_bus_probe);
  52. #ifdef CONFIG_PCI
  53. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  54. {
  55. struct device_node *np;
  56. for_each_node_by_type(np, "pci") {
  57. const void *prop;
  58. unsigned int bus_min;
  59. prop = of_get_property(np, "bus-range", NULL);
  60. if (!prop)
  61. continue;
  62. bus_min = be32_to_cpup(prop);
  63. if (bus->number == bus_min)
  64. return np;
  65. }
  66. return NULL;
  67. }
  68. static int x86_of_pci_irq_enable(struct pci_dev *dev)
  69. {
  70. u32 virq;
  71. int ret;
  72. u8 pin;
  73. ret = pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  74. if (ret)
  75. return pcibios_err_to_errno(ret);
  76. if (!pin)
  77. return 0;
  78. virq = of_irq_parse_and_map_pci(dev, 0, 0);
  79. if (virq == 0)
  80. return -EINVAL;
  81. dev->irq = virq;
  82. return 0;
  83. }
  84. static void x86_of_pci_irq_disable(struct pci_dev *dev)
  85. {
  86. }
  87. void x86_of_pci_init(void)
  88. {
  89. pcibios_enable_irq = x86_of_pci_irq_enable;
  90. pcibios_disable_irq = x86_of_pci_irq_disable;
  91. }
  92. #endif
  93. static void __init dtb_setup_hpet(void)
  94. {
  95. #ifdef CONFIG_HPET_TIMER
  96. struct device_node *dn;
  97. struct resource r;
  98. int ret;
  99. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-hpet");
  100. if (!dn)
  101. return;
  102. ret = of_address_to_resource(dn, 0, &r);
  103. if (ret) {
  104. WARN_ON(1);
  105. return;
  106. }
  107. hpet_address = r.start;
  108. #endif
  109. }
  110. #ifdef CONFIG_X86_LOCAL_APIC
  111. static void __init dtb_cpu_setup(void)
  112. {
  113. struct device_node *dn;
  114. u32 apic_id;
  115. for_each_of_cpu_node(dn) {
  116. apic_id = of_get_cpu_hwid(dn, 0);
  117. if (apic_id == ~0U) {
  118. pr_warn("%pOF: missing local APIC ID\n", dn);
  119. continue;
  120. }
  121. topology_register_apic(apic_id, CPU_ACPIID_INVALID, true);
  122. set_apicid_to_node(apic_id, of_node_to_nid(dn));
  123. }
  124. }
  125. static void __init dtb_lapic_setup(void)
  126. {
  127. struct device_node *dn;
  128. struct resource r;
  129. unsigned long lapic_addr = APIC_DEFAULT_PHYS_BASE;
  130. int ret;
  131. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-lapic");
  132. if (dn) {
  133. ret = of_address_to_resource(dn, 0, &r);
  134. if (WARN_ON(ret))
  135. return;
  136. lapic_addr = r.start;
  137. }
  138. /* Did the boot loader setup the local APIC ? */
  139. if (!boot_cpu_has(X86_FEATURE_APIC)) {
  140. /* Try force enabling, which registers the APIC address */
  141. if (!apic_force_enable(lapic_addr))
  142. return;
  143. } else {
  144. register_lapic_address(lapic_addr);
  145. }
  146. smp_found_config = 1;
  147. pic_mode = !of_property_read_bool(dn, "intel,virtual-wire-mode");
  148. pr_info("%s compatibility mode.\n", pic_mode ? "IMCR and PIC" : "Virtual Wire");
  149. }
  150. #endif /* CONFIG_X86_LOCAL_APIC */
  151. #ifdef CONFIG_X86_IO_APIC
  152. static unsigned int ioapic_id;
  153. struct of_ioapic_type {
  154. u32 out_type;
  155. u32 is_level;
  156. u32 active_low;
  157. };
  158. static struct of_ioapic_type of_ioapic_type[] =
  159. {
  160. {
  161. .out_type = IRQ_TYPE_EDGE_FALLING,
  162. .is_level = 0,
  163. .active_low = 1,
  164. },
  165. {
  166. .out_type = IRQ_TYPE_LEVEL_HIGH,
  167. .is_level = 1,
  168. .active_low = 0,
  169. },
  170. {
  171. .out_type = IRQ_TYPE_LEVEL_LOW,
  172. .is_level = 1,
  173. .active_low = 1,
  174. },
  175. {
  176. .out_type = IRQ_TYPE_EDGE_RISING,
  177. .is_level = 0,
  178. .active_low = 0,
  179. },
  180. };
  181. static int dt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
  182. unsigned int nr_irqs, void *arg)
  183. {
  184. struct irq_fwspec *fwspec = (struct irq_fwspec *)arg;
  185. struct of_ioapic_type *it;
  186. struct irq_alloc_info tmp;
  187. int type_index;
  188. if (WARN_ON(fwspec->param_count < 2))
  189. return -EINVAL;
  190. type_index = fwspec->param[1];
  191. if (type_index >= ARRAY_SIZE(of_ioapic_type))
  192. return -EINVAL;
  193. it = &of_ioapic_type[type_index];
  194. ioapic_set_alloc_attr(&tmp, NUMA_NO_NODE, it->is_level, it->active_low);
  195. tmp.devid = mpc_ioapic_id(mp_irqdomain_ioapic_idx(domain));
  196. tmp.ioapic.pin = fwspec->param[0];
  197. return mp_irqdomain_alloc(domain, virq, nr_irqs, &tmp);
  198. }
  199. static const struct irq_domain_ops ioapic_irq_domain_ops = {
  200. .alloc = dt_irqdomain_alloc,
  201. .free = mp_irqdomain_free,
  202. .activate = mp_irqdomain_activate,
  203. .deactivate = mp_irqdomain_deactivate,
  204. };
  205. static void __init dtb_add_ioapic(struct device_node *dn)
  206. {
  207. struct resource r;
  208. int ret;
  209. struct ioapic_domain_cfg cfg = {
  210. .type = IOAPIC_DOMAIN_DYNAMIC,
  211. .ops = &ioapic_irq_domain_ops,
  212. .dev = dn,
  213. };
  214. ret = of_address_to_resource(dn, 0, &r);
  215. if (ret) {
  216. pr_err("Can't obtain address from device node %pOF.\n", dn);
  217. return;
  218. }
  219. mp_register_ioapic(++ioapic_id, r.start, gsi_top, &cfg);
  220. }
  221. static void __init dtb_ioapic_setup(void)
  222. {
  223. struct device_node *dn;
  224. for_each_compatible_node(dn, NULL, "intel,ce4100-ioapic")
  225. dtb_add_ioapic(dn);
  226. if (nr_ioapics) {
  227. of_ioapic = 1;
  228. return;
  229. }
  230. pr_err("Error: No information about IO-APIC in OF.\n");
  231. }
  232. #else
  233. static void __init dtb_ioapic_setup(void) {}
  234. #endif
  235. static void __init dtb_apic_setup(void)
  236. {
  237. #ifdef CONFIG_X86_LOCAL_APIC
  238. dtb_lapic_setup();
  239. dtb_cpu_setup();
  240. #endif
  241. dtb_ioapic_setup();
  242. }
  243. static void __init x86_dtb_parse_smp_config(void)
  244. {
  245. if (!of_have_populated_dt())
  246. return;
  247. dtb_setup_hpet();
  248. dtb_apic_setup();
  249. }
  250. void __init x86_flattree_get_config(void)
  251. {
  252. #ifdef CONFIG_OF_EARLY_FLATTREE
  253. u32 size, map_len;
  254. void *dt;
  255. if (initial_dtb) {
  256. map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK), (u64)128);
  257. dt = early_memremap(initial_dtb, map_len);
  258. size = fdt_totalsize(dt);
  259. if (map_len < size) {
  260. early_memunmap(dt, map_len);
  261. dt = early_memremap(initial_dtb, size);
  262. map_len = size;
  263. }
  264. early_init_dt_verify(dt, __pa(dt));
  265. }
  266. unflatten_and_copy_device_tree();
  267. if (initial_dtb)
  268. early_memunmap(dt, map_len);
  269. #endif
  270. if (acpi_disabled && of_have_populated_dt())
  271. x86_init.mpparse.parse_smp_cfg = x86_dtb_parse_smp_config;
  272. }