devicetree.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. void add_interrupt_host(struct irq_domain *ih)
  26. {
  27. unsigned long flags;
  28. raw_spin_lock_irqsave(&big_irq_lock, flags);
  29. list_add(&ih->l, &irq_domains);
  30. raw_spin_unlock_irqrestore(&big_irq_lock, flags);
  31. }
  32. static struct irq_domain *get_ih_from_node(struct device_node *controller)
  33. {
  34. struct irq_domain *ih, *found = NULL;
  35. unsigned long flags;
  36. raw_spin_lock_irqsave(&big_irq_lock, flags);
  37. list_for_each_entry(ih, &irq_domains, l) {
  38. if (ih->controller == controller) {
  39. found = ih;
  40. break;
  41. }
  42. }
  43. raw_spin_unlock_irqrestore(&big_irq_lock, flags);
  44. return found;
  45. }
  46. unsigned int irq_create_of_mapping(struct device_node *controller,
  47. const u32 *intspec, unsigned int intsize)
  48. {
  49. struct irq_domain *ih;
  50. u32 virq, type;
  51. int ret;
  52. ih = get_ih_from_node(controller);
  53. if (!ih)
  54. return 0;
  55. ret = ih->xlate(ih, intspec, intsize, &virq, &type);
  56. if (ret)
  57. return ret;
  58. if (type == IRQ_TYPE_NONE)
  59. return virq;
  60. /* set the mask if it is different from current */
  61. if (type == (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
  62. 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 (!initial_boot_params)
  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. struct device_node *dn;
  175. struct resource r;
  176. int ret;
  177. dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-hpet");
  178. if (!dn)
  179. return;
  180. ret = of_address_to_resource(dn, 0, &r);
  181. if (ret) {
  182. WARN_ON(1);
  183. return;
  184. }
  185. hpet_address = r.start;
  186. }
  187. static void __init dtb_lapic_setup(void)
  188. {
  189. #ifdef CONFIG_X86_LOCAL_APIC
  190. if (apic_force_enable())
  191. return;
  192. smp_found_config = 1;
  193. pic_mode = 1;
  194. /* Required for ioapic registration */
  195. set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
  196. if (boot_cpu_physical_apicid == -1U)
  197. boot_cpu_physical_apicid = read_apic_id();
  198. generic_processor_info(boot_cpu_physical_apicid,
  199. GET_APIC_VERSION(apic_read(APIC_LVR)));
  200. #endif
  201. }
  202. #ifdef CONFIG_X86_IO_APIC
  203. static unsigned int ioapic_id;
  204. static void __init dtb_add_ioapic(struct device_node *dn)
  205. {
  206. struct resource r;
  207. int ret;
  208. ret = of_address_to_resource(dn, 0, &r);
  209. if (ret) {
  210. printk(KERN_ERR "Can't obtain address from node %s.\n",
  211. dn->full_name);
  212. return;
  213. }
  214. mp_register_ioapic(++ioapic_id, r.start, gsi_top);
  215. }
  216. static void __init dtb_ioapic_setup(void)
  217. {
  218. struct device_node *dn;
  219. if (!smp_found_config)
  220. return;
  221. for_each_compatible_node(dn, NULL, "intel,ce4100-ioapic")
  222. dtb_add_ioapic(dn);
  223. if (nr_ioapics) {
  224. of_ioapic = 1;
  225. return;
  226. }
  227. printk(KERN_ERR "Error: No information about IO-APIC in OF.\n");
  228. smp_found_config = 0;
  229. }
  230. #else
  231. static void __init dtb_ioapic_setup(void) {}
  232. #endif
  233. static void __init dtb_apic_setup(void)
  234. {
  235. dtb_lapic_setup();
  236. dtb_ioapic_setup();
  237. }
  238. void __init x86_dtb_find_config(void)
  239. {
  240. if (initial_dtb)
  241. smp_found_config = 1;
  242. else
  243. printk(KERN_ERR "Missing device tree!.\n");
  244. }
  245. void __init x86_dtb_get_config(unsigned int unused)
  246. {
  247. u32 size, map_len;
  248. void *new_dtb;
  249. if (!initial_dtb)
  250. return;
  251. map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK),
  252. (u64)sizeof(struct boot_param_header));
  253. initial_boot_params = early_memremap(initial_dtb, map_len);
  254. size = be32_to_cpu(initial_boot_params->totalsize);
  255. if (map_len < size) {
  256. early_iounmap(initial_boot_params, map_len);
  257. initial_boot_params = early_memremap(initial_dtb, size);
  258. map_len = size;
  259. }
  260. new_dtb = alloc_bootmem(size);
  261. memcpy(new_dtb, initial_boot_params, size);
  262. early_iounmap(initial_boot_params, map_len);
  263. initial_boot_params = new_dtb;
  264. /* root level address cells */
  265. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  266. unflatten_device_tree();
  267. dtb_setup_hpet();
  268. dtb_apic_setup();
  269. }
  270. #ifdef CONFIG_X86_IO_APIC
  271. struct of_ioapic_type {
  272. u32 out_type;
  273. u32 trigger;
  274. u32 polarity;
  275. };
  276. static struct of_ioapic_type of_ioapic_type[] =
  277. {
  278. {
  279. .out_type = IRQ_TYPE_EDGE_RISING,
  280. .trigger = IOAPIC_EDGE,
  281. .polarity = 1,
  282. },
  283. {
  284. .out_type = IRQ_TYPE_LEVEL_LOW,
  285. .trigger = IOAPIC_LEVEL,
  286. .polarity = 0,
  287. },
  288. {
  289. .out_type = IRQ_TYPE_LEVEL_HIGH,
  290. .trigger = IOAPIC_LEVEL,
  291. .polarity = 1,
  292. },
  293. {
  294. .out_type = IRQ_TYPE_EDGE_FALLING,
  295. .trigger = IOAPIC_EDGE,
  296. .polarity = 0,
  297. },
  298. };
  299. static int ioapic_xlate(struct irq_domain *id, const u32 *intspec, u32 intsize,
  300. u32 *out_hwirq, u32 *out_type)
  301. {
  302. struct io_apic_irq_attr attr;
  303. struct of_ioapic_type *it;
  304. u32 line, idx, type;
  305. if (intsize < 2)
  306. return -EINVAL;
  307. line = *intspec;
  308. idx = (u32) id->priv;
  309. *out_hwirq = line + mp_gsi_routing[idx].gsi_base;
  310. intspec++;
  311. type = *intspec;
  312. if (type >= ARRAY_SIZE(of_ioapic_type))
  313. return -EINVAL;
  314. it = of_ioapic_type + type;
  315. *out_type = it->out_type;
  316. set_io_apic_irq_attr(&attr, idx, line, it->trigger, it->polarity);
  317. return io_apic_setup_irq_pin(*out_hwirq, cpu_to_node(0), &attr);
  318. }
  319. static void __init ioapic_add_ofnode(struct device_node *np)
  320. {
  321. struct resource r;
  322. int i, ret;
  323. ret = of_address_to_resource(np, 0, &r);
  324. if (ret) {
  325. printk(KERN_ERR "Failed to obtain address for %s\n",
  326. np->full_name);
  327. return;
  328. }
  329. for (i = 0; i < nr_ioapics; i++) {
  330. if (r.start == mp_ioapics[i].apicaddr) {
  331. struct irq_domain *id;
  332. id = kzalloc(sizeof(*id), GFP_KERNEL);
  333. BUG_ON(!id);
  334. id->controller = np;
  335. id->xlate = ioapic_xlate;
  336. id->priv = (void *)i;
  337. add_interrupt_host(id);
  338. return;
  339. }
  340. }
  341. printk(KERN_ERR "IOxAPIC at %s is not registered.\n", np->full_name);
  342. }
  343. void __init x86_add_irq_domains(void)
  344. {
  345. struct device_node *dp;
  346. if (!initial_boot_params)
  347. return;
  348. for_each_node_with_property(dp, "interrupt-controller") {
  349. if (of_device_is_compatible(dp, "intel,ce4100-ioapic"))
  350. ioapic_add_ofnode(dp);
  351. }
  352. }
  353. #else
  354. void __init x86_add_irq_domains(void) { }
  355. #endif