x86: dtb: Add early parsing of IO_APIC
[linux-block.git] / arch / x86 / kernel / devicetree.c
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/slab.h>
13
14 #include <asm/irq_controller.h>
15 #include <asm/apic.h>
16
17 __initdata u64 initial_dtb;
18 char __initdata cmd_line[COMMAND_LINE_SIZE];
19 static LIST_HEAD(irq_domains);
20 static DEFINE_RAW_SPINLOCK(big_irq_lock);
21
22 int __initdata of_ioapic;
23
24 void add_interrupt_host(struct irq_domain *ih)
25 {
26         unsigned long flags;
27
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
33 static struct irq_domain *get_ih_from_node(struct device_node *controller)
34 {
35         struct irq_domain *ih, *found = NULL;
36         unsigned long flags;
37
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
49 unsigned int irq_create_of_mapping(struct device_node *controller,
50                                    const u32 *intspec, unsigned int intsize)
51 {
52         struct irq_domain *ih;
53         u32 virq, type;
54         int ret;
55
56         ih = get_ih_from_node(controller);
57         if (!ih)
58                 return 0;
59         ret = ih->xlate(ih, intspec, intsize, &virq, &type);
60         if (ret)
61                 return ret;
62         if (type == IRQ_TYPE_NONE)
63                 return virq;
64         /* set the mask if it is different from current */
65         if (type == (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
66                 set_irq_type(virq, type);
67         return virq;
68 }
69 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
70
71 unsigned long pci_address_to_pio(phys_addr_t address)
72 {
73         /*
74          * The ioport address can be directly used by inX / outX
75          */
76         BUG_ON(address >= (1 << 16));
77         return (unsigned long)address;
78 }
79 EXPORT_SYMBOL_GPL(pci_address_to_pio);
80
81 void __init early_init_dt_scan_chosen_arch(unsigned long node)
82 {
83         BUG();
84 }
85
86 void __init early_init_dt_add_memory_arch(u64 base, u64 size)
87 {
88         BUG();
89 }
90
91 void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
92 {
93         return __alloc_bootmem(size, align, __pa(MAX_DMA_ADDRESS));
94 }
95
96 void __init add_dtb(u64 data)
97 {
98         initial_dtb = data + offsetof(struct setup_data, data);
99 }
100
101 static void __init dtb_lapic_setup(void)
102 {
103 #ifdef CONFIG_X86_LOCAL_APIC
104         if (apic_force_enable())
105                 return;
106
107         smp_found_config = 1;
108         pic_mode = 1;
109         /* Required for ioapic registration */
110         set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
111         if (boot_cpu_physical_apicid == -1U)
112                 boot_cpu_physical_apicid = read_apic_id();
113
114         generic_processor_info(boot_cpu_physical_apicid,
115                         GET_APIC_VERSION(apic_read(APIC_LVR)));
116 #endif
117 }
118
119 #ifdef CONFIG_X86_IO_APIC
120 static unsigned int ioapic_id;
121
122 static void __init dtb_add_ioapic(struct device_node *dn)
123 {
124         struct resource r;
125         int ret;
126
127         ret = of_address_to_resource(dn, 0, &r);
128         if (ret) {
129                 printk(KERN_ERR "Can't obtain address from node %s.\n",
130                                 dn->full_name);
131                 return;
132         }
133         mp_register_ioapic(++ioapic_id, r.start, gsi_top);
134 }
135
136 static void __init dtb_ioapic_setup(void)
137 {
138         struct device_node *dn;
139
140         if (!smp_found_config)
141                 return;
142
143         for_each_compatible_node(dn, NULL, "intel,ce4100-ioapic")
144                 dtb_add_ioapic(dn);
145
146         if (nr_ioapics) {
147                 of_ioapic = 1;
148                 return;
149         }
150         printk(KERN_ERR "Error: No information about IO-APIC in OF.\n");
151         smp_found_config = 0;
152 }
153 #else
154 static void __init dtb_ioapic_setup(void) {}
155 #endif
156
157 static void __init dtb_apic_setup(void)
158 {
159         dtb_lapic_setup();
160         dtb_ioapic_setup();
161 }
162
163 void __init x86_dtb_find_config(void)
164 {
165         if (initial_dtb)
166                 smp_found_config = 1;
167         else
168                 printk(KERN_ERR "Missing device tree!.\n");
169 }
170
171 void __init x86_dtb_get_config(unsigned int unused)
172 {
173         u32 size, map_len;
174         void *new_dtb;
175
176         if (!initial_dtb)
177                 return;
178
179         map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK),
180                         (u64)sizeof(struct boot_param_header));
181
182         initial_boot_params = early_memremap(initial_dtb, map_len);
183         size = be32_to_cpu(initial_boot_params->totalsize);
184         if (map_len < size) {
185                 early_iounmap(initial_boot_params, map_len);
186                 initial_boot_params = early_memremap(initial_dtb, size);
187                 map_len = size;
188         }
189
190         new_dtb = alloc_bootmem(size);
191         memcpy(new_dtb, initial_boot_params, size);
192         early_iounmap(initial_boot_params, map_len);
193
194         initial_boot_params = new_dtb;
195
196         /* root level address cells */
197         of_scan_flat_dt(early_init_dt_scan_root, NULL);
198
199         unflatten_device_tree();
200         dtb_apic_setup();
201 }