devicetree.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Architecture specific OF callbacks.
  3. */
  4. #include <linux/bootmem.h>
  5. #include <linux/io.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/list.h>
  8. #include <linux/of.h>
  9. #include <linux/of_fdt.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/slab.h>
  14. #include <linux/pci.h>
  15. #include <linux/of_pci.h>
  16. #include <asm/hpet.h>
  17. #include <asm/irq_controller.h>
  18. #include <asm/apic.h>
  19. #include <asm/pci_x86.h>
  20. __initdata u64 initial_dtb;
  21. char __initdata cmd_line[COMMAND_LINE_SIZE];
  22. static LIST_HEAD(irq_domains);
  23. static DEFINE_RAW_SPINLOCK(big_irq_lock);
  24. int __initdata of_ioapic;
  25. #ifdef CONFIG_X86_IO_APIC
  26. static void add_interrupt_host(struct irq_domain *ih)
  27. {
  28. unsigned long flags;
  29. raw_spin_lock_irqsave(&big_irq_lock, flags);
  30. list_add(&ih->l, &irq_domains);
  31. raw_spin_unlock_irqrestore(&big_irq_lock, flags);
  32. }
  33. #endif
  34. static struct irq_domain *get_ih_from_node(struct device_node *controller)
  35. {
  36. struct irq_domain *ih, *found = NULL;
  37. unsigned long flags;
  38. raw_spin_lock_irqsave(&big_irq_lock, flags);
  39. list_for_each_entry(ih, &irq_domains, l) {
  40. if (ih->controller == controller) {
  41. found = ih;
  42. break;
  43. }
  44. }
  45. raw_spin_unlock_irqrestore(&big_irq_lock, flags);
  46. return found;
  47. }
  48. unsigned int irq_create_of_mapping(struct device_node *controller,
  49. const u32 *intspec, unsigned int intsize)
  50. {
  51. struct irq_domain *ih;
  52. u32 virq, type;
  53. int ret;
  54. ih = get_ih_from_node(controller);
  55. if (!ih)
  56. return 0;
  57. ret = ih->xlate(ih, intspec, intsize, &virq, &type);
  58. if (ret)
  59. return 0;
  60. if (type == IRQ_TYPE_NONE)
  61. return virq;
  62. irq_set_irq_type(virq, type);
  63. return virq;
  64. }
  65. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  66. unsigned long pci_address_to_pio(phys_addr_t address)
  67. {
  68. /*
  69. * The ioport address can be directly used by inX / outX
  70. */
  71. BUG_ON(address >= (1 << 16));
  72. return (unsigned long)address;
  73. }
  74. EXPORT_SYMBOL_GPL(pci_address_to_pio);
  75. void __init early_init_dt_scan_chosen_arch(unsigned long node)
  76. {
  77. BUG();
  78. }
  79. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  80. {
  81. BUG();
  82. }
  83. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  84. {
  85. return __alloc_bootmem(size, align, __pa(MAX_DMA_ADDRESS));
  86. }
  87. void __init add_dtb(u64 data)
  88. {
  89. initial_dtb = data + offsetof(struct setup_data, data);
  90. }
  91. /*
  92. * CE4100 ids. Will be moved to machine_device_initcall() once we have it.
  93. */
  94. static struct of_device_id __initdata ce4100_ids[] = {
  95. { .compatible = "intel,ce4100-cp", },
  96. { .compatible = "isa", },
  97. { .compatible = "pci", },
  98. {},
  99. };
  100. static int __init add_bus_probe(void)
  101. {
  102. if (!of_have_populated_dt())
  103. return 0;
  104. return of_platform_bus_probe(NULL, ce4100_ids, NULL);
  105. }
  106. module_init(add_bus_probe);
  107. #ifdef CONFIG_PCI
  108. static int x86_of_pci_irq_enable(struct pci_dev *dev)
  109. {
  110. struct of_irq oirq;
  111. u32 virq;
  112. int ret;
  113. u8 pin;
  114. ret = pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  115. if (ret)
  116. return ret;
  117. if (!pin)
  118. return 0;
  119. ret = of_irq_map_pci(dev, &oirq);
  120. if (ret)
  121. return ret;
  122. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  123. oirq.size);
  124. if (virq == 0)
  125. return -EINVAL;
  126. dev->irq = virq;
  127. return 0;
  128. }
  129. static void x86_of_pci_irq_disable(struct pci_dev *dev)
  130. {
  131. }
  132. void __cpuinit x86_of_pci_init(void)
  133. {
  134. struct device_node *np;
  135. pcibios_enable_irq = x86_of_pci_irq_enable;
  136. pcibios_disable_irq = x86_of_pci_irq_disable;
  137. for_each_node_by_type(np, "pci") {
  138. const void *prop;
  139. struct pci_bus *bus;
  140. unsigned int bus_min;
  141. struct device_node *child;
  142. prop = of_get_property(np, "bus-range", NULL);
  143. if (!prop)
  144. continue;
  145. bus_min = be32_to_cpup(prop);
  146. bus = pci_find_bus(0, bus_min);
  147. if (!bus) {
  148. printk(KERN_ERR "Can't find a node for bus %s.\n",
  149. np->full_name);
  150. continue;
  151. }
  152. if (bus->self)
  153. bus->self->dev.of_node = np;
  154. else
  155. bus->dev.of_node = np;
  156. for_each_child_of_node(np, child) {
  157. struct pci_dev *dev;
  158. u32 devfn;
  159. prop = of_get_property(child, "reg", NULL);
  160. if (!prop)
  161. continue;
  162. devfn = (be32_to_cpup(prop) >> 8) & 0xff;
  163. dev = pci_get_slot(bus, devfn);
  164. if (!dev)
  165. continue;
  166. dev->dev.of_node = child;
  167. pci_dev_put(dev);
  168. }
  169. }
  170. }
  171. #endif
  172. static void __init dtb_setup_hpet(void)
  173. {
  174. #ifdef CONFIG_HPET_TIMER
  175. struct device_node *dn;
  176. struct resource r;
  177. int ret;
  178. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-hpet");
  179. if (!dn)
  180. return;
  181. ret = of_address_to_resource(dn, 0, &r);
  182. if (ret) {
  183. WARN_ON(1);
  184. return;
  185. }
  186. hpet_address = r.start;
  187. #endif
  188. }
  189. static void __init dtb_lapic_setup(void)
  190. {
  191. #ifdef CONFIG_X86_LOCAL_APIC
  192. struct device_node *dn;
  193. struct resource r;
  194. int ret;
  195. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-lapic");
  196. if (!dn)
  197. return;
  198. ret = of_address_to_resource(dn, 0, &r);
  199. if (WARN_ON(ret))
  200. return;
  201. /* Did the boot loader setup the local APIC ? */
  202. if (!cpu_has_apic) {
  203. if (apic_force_enable(r.start))
  204. return;
  205. }
  206. smp_found_config = 1;
  207. pic_mode = 1;
  208. register_lapic_address(r.start);
  209. generic_processor_info(boot_cpu_physical_apicid,
  210. GET_APIC_VERSION(apic_read(APIC_LVR)));
  211. #endif
  212. }
  213. #ifdef CONFIG_X86_IO_APIC
  214. static unsigned int ioapic_id;
  215. static void __init dtb_add_ioapic(struct device_node *dn)
  216. {
  217. struct resource r;
  218. int ret;
  219. ret = of_address_to_resource(dn, 0, &r);
  220. if (ret) {
  221. printk(KERN_ERR "Can't obtain address from node %s.\n",
  222. dn->full_name);
  223. return;
  224. }
  225. mp_register_ioapic(++ioapic_id, r.start, gsi_top);
  226. }
  227. static void __init dtb_ioapic_setup(void)
  228. {
  229. struct device_node *dn;
  230. for_each_compatible_node(dn, NULL, "intel,ce4100-ioapic")
  231. dtb_add_ioapic(dn);
  232. if (nr_ioapics) {
  233. of_ioapic = 1;
  234. return;
  235. }
  236. printk(KERN_ERR "Error: No information about IO-APIC in OF.\n");
  237. }
  238. #else
  239. static void __init dtb_ioapic_setup(void) {}
  240. #endif
  241. static void __init dtb_apic_setup(void)
  242. {
  243. dtb_lapic_setup();
  244. dtb_ioapic_setup();
  245. }
  246. #ifdef CONFIG_OF_FLATTREE
  247. static void __init x86_flattree_get_config(void)
  248. {
  249. u32 size, map_len;
  250. void *new_dtb;
  251. if (!initial_dtb)
  252. return;
  253. map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK),
  254. (u64)sizeof(struct boot_param_header));
  255. initial_boot_params = early_memremap(initial_dtb, map_len);
  256. size = be32_to_cpu(initial_boot_params->totalsize);
  257. if (map_len < size) {
  258. early_iounmap(initial_boot_params, map_len);
  259. initial_boot_params = early_memremap(initial_dtb, size);
  260. map_len = size;
  261. }
  262. new_dtb = alloc_bootmem(size);
  263. memcpy(new_dtb, initial_boot_params, size);
  264. early_iounmap(initial_boot_params, map_len);
  265. initial_boot_params = new_dtb;
  266. /* root level address cells */
  267. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  268. unflatten_device_tree();
  269. }
  270. #else
  271. static inline void x86_flattree_get_config(void) { }
  272. #endif
  273. void __init x86_dtb_init(void)
  274. {
  275. x86_flattree_get_config();
  276. if (!of_have_populated_dt())
  277. return;
  278. dtb_setup_hpet();
  279. dtb_apic_setup();
  280. }
  281. #ifdef CONFIG_X86_IO_APIC
  282. struct of_ioapic_type {
  283. u32 out_type;
  284. u32 trigger;
  285. u32 polarity;
  286. };
  287. static struct of_ioapic_type of_ioapic_type[] =
  288. {
  289. {
  290. .out_type = IRQ_TYPE_EDGE_RISING,
  291. .trigger = IOAPIC_EDGE,
  292. .polarity = 1,
  293. },
  294. {
  295. .out_type = IRQ_TYPE_LEVEL_LOW,
  296. .trigger = IOAPIC_LEVEL,
  297. .polarity = 0,
  298. },
  299. {
  300. .out_type = IRQ_TYPE_LEVEL_HIGH,
  301. .trigger = IOAPIC_LEVEL,
  302. .polarity = 1,
  303. },
  304. {
  305. .out_type = IRQ_TYPE_EDGE_FALLING,
  306. .trigger = IOAPIC_EDGE,
  307. .polarity = 0,
  308. },
  309. };
  310. static int ioapic_xlate(struct irq_domain *id, const u32 *intspec, u32 intsize,
  311. u32 *out_hwirq, u32 *out_type)
  312. {
  313. struct io_apic_irq_attr attr;
  314. struct of_ioapic_type *it;
  315. u32 line, idx, type;
  316. if (intsize < 2)
  317. return -EINVAL;
  318. line = *intspec;
  319. idx = (u32) id->priv;
  320. *out_hwirq = line + mp_gsi_routing[idx].gsi_base;
  321. intspec++;
  322. type = *intspec;
  323. if (type >= ARRAY_SIZE(of_ioapic_type))
  324. return -EINVAL;
  325. it = of_ioapic_type + type;
  326. *out_type = it->out_type;
  327. set_io_apic_irq_attr(&attr, idx, line, it->trigger, it->polarity);
  328. return io_apic_setup_irq_pin(*out_hwirq, cpu_to_node(0), &attr);
  329. }
  330. static void __init ioapic_add_ofnode(struct device_node *np)
  331. {
  332. struct resource r;
  333. int i, ret;
  334. ret = of_address_to_resource(np, 0, &r);
  335. if (ret) {
  336. printk(KERN_ERR "Failed to obtain address for %s\n",
  337. np->full_name);
  338. return;
  339. }
  340. for (i = 0; i < nr_ioapics; i++) {
  341. if (r.start == mp_ioapics[i].apicaddr) {
  342. struct irq_domain *id;
  343. id = kzalloc(sizeof(*id), GFP_KERNEL);
  344. BUG_ON(!id);
  345. id->controller = np;
  346. id->xlate = ioapic_xlate;
  347. id->priv = (void *)i;
  348. add_interrupt_host(id);
  349. return;
  350. }
  351. }
  352. printk(KERN_ERR "IOxAPIC at %s is not registered.\n", np->full_name);
  353. }
  354. void __init x86_add_irq_domains(void)
  355. {
  356. struct device_node *dp;
  357. if (!of_have_populated_dt())
  358. return;
  359. for_each_node_with_property(dp, "interrupt-controller") {
  360. if (of_device_is_compatible(dp, "intel,ce4100-ioapic"))
  361. ioapic_add_ofnode(dp);
  362. }
  363. }
  364. #else
  365. void __init x86_add_irq_domains(void) { }
  366. #endif