devicetree.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Architecture specific OF callbacks.
  3. */
  4. #include <linux/bootmem.h>
  5. #include <linux/export.h>
  6. #include <linux/io.h>
  7. #include <linux/irqdomain.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/slab.h>
  16. #include <linux/pci.h>
  17. #include <linux/of_pci.h>
  18. #include <linux/initrd.h>
  19. #include <asm/hpet.h>
  20. #include <asm/apic.h>
  21. #include <asm/pci_x86.h>
  22. #include <asm/setup.h>
  23. __initdata u64 initial_dtb;
  24. char __initdata cmd_line[COMMAND_LINE_SIZE];
  25. int __initdata of_ioapic;
  26. void __init early_init_dt_scan_chosen_arch(unsigned long node)
  27. {
  28. BUG();
  29. }
  30. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  31. {
  32. BUG();
  33. }
  34. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  35. {
  36. return __alloc_bootmem(size, align, __pa(MAX_DMA_ADDRESS));
  37. }
  38. void __init add_dtb(u64 data)
  39. {
  40. initial_dtb = data + offsetof(struct setup_data, data);
  41. }
  42. /*
  43. * CE4100 ids. Will be moved to machine_device_initcall() once we have it.
  44. */
  45. static struct of_device_id __initdata ce4100_ids[] = {
  46. { .compatible = "intel,ce4100-cp", },
  47. { .compatible = "isa", },
  48. { .compatible = "pci", },
  49. {},
  50. };
  51. static int __init add_bus_probe(void)
  52. {
  53. if (!of_have_populated_dt())
  54. return 0;
  55. return of_platform_bus_probe(NULL, ce4100_ids, NULL);
  56. }
  57. module_init(add_bus_probe);
  58. #ifdef CONFIG_PCI
  59. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  60. {
  61. struct device_node *np;
  62. for_each_node_by_type(np, "pci") {
  63. const void *prop;
  64. unsigned int bus_min;
  65. prop = of_get_property(np, "bus-range", NULL);
  66. if (!prop)
  67. continue;
  68. bus_min = be32_to_cpup(prop);
  69. if (bus->number == bus_min)
  70. return np;
  71. }
  72. return NULL;
  73. }
  74. static int x86_of_pci_irq_enable(struct pci_dev *dev)
  75. {
  76. struct of_irq oirq;
  77. u32 virq;
  78. int ret;
  79. u8 pin;
  80. ret = pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  81. if (ret)
  82. return ret;
  83. if (!pin)
  84. return 0;
  85. ret = of_irq_map_pci(dev, &oirq);
  86. if (ret)
  87. return ret;
  88. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  89. oirq.size);
  90. if (virq == 0)
  91. return -EINVAL;
  92. dev->irq = virq;
  93. return 0;
  94. }
  95. static void x86_of_pci_irq_disable(struct pci_dev *dev)
  96. {
  97. }
  98. void x86_of_pci_init(void)
  99. {
  100. pcibios_enable_irq = x86_of_pci_irq_enable;
  101. pcibios_disable_irq = x86_of_pci_irq_disable;
  102. }
  103. #endif
  104. static void __init dtb_setup_hpet(void)
  105. {
  106. #ifdef CONFIG_HPET_TIMER
  107. struct device_node *dn;
  108. struct resource r;
  109. int ret;
  110. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-hpet");
  111. if (!dn)
  112. return;
  113. ret = of_address_to_resource(dn, 0, &r);
  114. if (ret) {
  115. WARN_ON(1);
  116. return;
  117. }
  118. hpet_address = r.start;
  119. #endif
  120. }
  121. static void __init dtb_lapic_setup(void)
  122. {
  123. #ifdef CONFIG_X86_LOCAL_APIC
  124. struct device_node *dn;
  125. struct resource r;
  126. int ret;
  127. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-lapic");
  128. if (!dn)
  129. return;
  130. ret = of_address_to_resource(dn, 0, &r);
  131. if (WARN_ON(ret))
  132. return;
  133. /* Did the boot loader setup the local APIC ? */
  134. if (!cpu_has_apic) {
  135. if (apic_force_enable(r.start))
  136. return;
  137. }
  138. smp_found_config = 1;
  139. pic_mode = 1;
  140. register_lapic_address(r.start);
  141. generic_processor_info(boot_cpu_physical_apicid,
  142. GET_APIC_VERSION(apic_read(APIC_LVR)));
  143. #endif
  144. }
  145. #ifdef CONFIG_X86_IO_APIC
  146. static unsigned int ioapic_id;
  147. static void __init dtb_add_ioapic(struct device_node *dn)
  148. {
  149. struct resource r;
  150. int ret;
  151. ret = of_address_to_resource(dn, 0, &r);
  152. if (ret) {
  153. printk(KERN_ERR "Can't obtain address from node %s.\n",
  154. dn->full_name);
  155. return;
  156. }
  157. mp_register_ioapic(++ioapic_id, r.start, gsi_top);
  158. }
  159. static void __init dtb_ioapic_setup(void)
  160. {
  161. struct device_node *dn;
  162. for_each_compatible_node(dn, NULL, "intel,ce4100-ioapic")
  163. dtb_add_ioapic(dn);
  164. if (nr_ioapics) {
  165. of_ioapic = 1;
  166. return;
  167. }
  168. printk(KERN_ERR "Error: No information about IO-APIC in OF.\n");
  169. }
  170. #else
  171. static void __init dtb_ioapic_setup(void) {}
  172. #endif
  173. static void __init dtb_apic_setup(void)
  174. {
  175. dtb_lapic_setup();
  176. dtb_ioapic_setup();
  177. }
  178. #ifdef CONFIG_OF_FLATTREE
  179. static void __init x86_flattree_get_config(void)
  180. {
  181. u32 size, map_len;
  182. struct boot_param_header *dt;
  183. if (!initial_dtb)
  184. return;
  185. map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK),
  186. (u64)sizeof(struct boot_param_header));
  187. dt = early_memremap(initial_dtb, map_len);
  188. size = be32_to_cpu(dt->totalsize);
  189. if (map_len < size) {
  190. early_iounmap(dt, map_len);
  191. dt = early_memremap(initial_dtb, size);
  192. map_len = size;
  193. }
  194. initial_boot_params = dt;
  195. unflatten_and_copy_device_tree();
  196. early_iounmap(dt, map_len);
  197. }
  198. #else
  199. static inline void x86_flattree_get_config(void) { }
  200. #endif
  201. void __init x86_dtb_init(void)
  202. {
  203. x86_flattree_get_config();
  204. if (!of_have_populated_dt())
  205. return;
  206. dtb_setup_hpet();
  207. dtb_apic_setup();
  208. }
  209. #ifdef CONFIG_X86_IO_APIC
  210. struct of_ioapic_type {
  211. u32 out_type;
  212. u32 trigger;
  213. u32 polarity;
  214. };
  215. static struct of_ioapic_type of_ioapic_type[] =
  216. {
  217. {
  218. .out_type = IRQ_TYPE_EDGE_RISING,
  219. .trigger = IOAPIC_EDGE,
  220. .polarity = 1,
  221. },
  222. {
  223. .out_type = IRQ_TYPE_LEVEL_LOW,
  224. .trigger = IOAPIC_LEVEL,
  225. .polarity = 0,
  226. },
  227. {
  228. .out_type = IRQ_TYPE_LEVEL_HIGH,
  229. .trigger = IOAPIC_LEVEL,
  230. .polarity = 1,
  231. },
  232. {
  233. .out_type = IRQ_TYPE_EDGE_FALLING,
  234. .trigger = IOAPIC_EDGE,
  235. .polarity = 0,
  236. },
  237. };
  238. static int ioapic_xlate(struct irq_domain *domain,
  239. struct device_node *controller,
  240. const u32 *intspec, u32 intsize,
  241. irq_hw_number_t *out_hwirq, u32 *out_type)
  242. {
  243. struct io_apic_irq_attr attr;
  244. struct of_ioapic_type *it;
  245. u32 line, idx;
  246. int rc;
  247. if (WARN_ON(intsize < 2))
  248. return -EINVAL;
  249. line = intspec[0];
  250. if (intspec[1] >= ARRAY_SIZE(of_ioapic_type))
  251. return -EINVAL;
  252. it = &of_ioapic_type[intspec[1]];
  253. idx = (u32) domain->host_data;
  254. set_io_apic_irq_attr(&attr, idx, line, it->trigger, it->polarity);
  255. rc = io_apic_setup_irq_pin_once(irq_find_mapping(domain, line),
  256. cpu_to_node(0), &attr);
  257. if (rc)
  258. return rc;
  259. *out_hwirq = line;
  260. *out_type = it->out_type;
  261. return 0;
  262. }
  263. const struct irq_domain_ops ioapic_irq_domain_ops = {
  264. .xlate = ioapic_xlate,
  265. };
  266. static void dt_add_ioapic_domain(unsigned int ioapic_num,
  267. struct device_node *np)
  268. {
  269. struct irq_domain *id;
  270. struct mp_ioapic_gsi *gsi_cfg;
  271. int ret;
  272. int num;
  273. gsi_cfg = mp_ioapic_gsi_routing(ioapic_num);
  274. num = gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
  275. id = irq_domain_add_linear(np, num, &ioapic_irq_domain_ops,
  276. (void *)ioapic_num);
  277. BUG_ON(!id);
  278. if (gsi_cfg->gsi_base == 0) {
  279. /*
  280. * The first NR_IRQS_LEGACY irq descs are allocated in
  281. * early_irq_init() and need just a mapping. The
  282. * remaining irqs need both. All of them are preallocated
  283. * and assigned so we can keep the 1:1 mapping which the ioapic
  284. * is having.
  285. */
  286. irq_domain_associate_many(id, 0, 0, NR_IRQS_LEGACY);
  287. if (num > NR_IRQS_LEGACY) {
  288. ret = irq_create_strict_mappings(id, NR_IRQS_LEGACY,
  289. NR_IRQS_LEGACY, num - NR_IRQS_LEGACY);
  290. if (ret)
  291. pr_err("Error creating mapping for the "
  292. "remaining IRQs: %d\n", ret);
  293. }
  294. irq_set_default_host(id);
  295. } else {
  296. ret = irq_create_strict_mappings(id, gsi_cfg->gsi_base, 0, num);
  297. if (ret)
  298. pr_err("Error creating IRQ mapping: %d\n", ret);
  299. }
  300. }
  301. static void __init ioapic_add_ofnode(struct device_node *np)
  302. {
  303. struct resource r;
  304. int i, ret;
  305. ret = of_address_to_resource(np, 0, &r);
  306. if (ret) {
  307. printk(KERN_ERR "Failed to obtain address for %s\n",
  308. np->full_name);
  309. return;
  310. }
  311. for (i = 0; i < nr_ioapics; i++) {
  312. if (r.start == mpc_ioapic_addr(i)) {
  313. dt_add_ioapic_domain(i, np);
  314. return;
  315. }
  316. }
  317. printk(KERN_ERR "IOxAPIC at %s is not registered.\n", np->full_name);
  318. }
  319. void __init x86_add_irq_domains(void)
  320. {
  321. struct device_node *dp;
  322. if (!of_have_populated_dt())
  323. return;
  324. for_each_node_with_property(dp, "interrupt-controller") {
  325. if (of_device_is_compatible(dp, "intel,ce4100-ioapic"))
  326. ioapic_add_ofnode(dp);
  327. }
  328. }
  329. #else
  330. void __init x86_add_irq_domains(void) { }
  331. #endif