devicetree.c 8.4 KB

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