devicetree.c 8.7 KB

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