devicetree.c 7.6 KB

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