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