devicetree.c 6.3 KB

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