x86: dtb: Add support for PCI devices backed by dtb nodes
[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/of_irq.h>
13 #include <linux/slab.h>
14 #include <linux/pci.h>
15 #include <linux/of_pci.h>
16
17 #include <asm/hpet.h>
18 #include <asm/irq_controller.h>
19 #include <asm/apic.h>
20 #include <asm/pci_x86.h>
21
22 __initdata u64 initial_dtb;
23 char __initdata cmd_line[COMMAND_LINE_SIZE];
24 static LIST_HEAD(irq_domains);
25 static DEFINE_RAW_SPINLOCK(big_irq_lock);
26
27 int __initdata of_ioapic;
28
29 void add_interrupt_host(struct irq_domain *ih)
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 }
53
54 unsigned int irq_create_of_mapping(struct device_node *controller,
55                                    const u32 *intspec, unsigned int intsize)
56 {
57         struct irq_domain *ih;
58         u32 virq, type;
59         int ret;
60
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;
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 {
103         initial_dtb = data + offsetof(struct setup_data, data);
104 }
105
106 #ifdef CONFIG_PCI
107 static int x86_of_pci_irq_enable(struct pci_dev *dev)
108 {
109         struct of_irq oirq;
110         u32 virq;
111         int ret;
112         u8 pin;
113
114         ret = pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
115         if (ret)
116                 return ret;
117         if (!pin)
118                 return 0;
119
120         ret = of_irq_map_pci(dev, &oirq);
121         if (ret)
122                 return ret;
123
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
132 static void x86_of_pci_irq_disable(struct pci_dev *dev)
133 {
134 }
135
136 void __cpuinit x86_of_pci_init(void)
137 {
138         struct device_node *np;
139
140         pcibios_enable_irq = x86_of_pci_irq_enable;
141         pcibios_disable_irq = x86_of_pci_irq_disable;
142
143         for_each_node_by_type(np, "pci") {
144                 const void *prop;
145                 struct pci_bus *bus;
146                 unsigned int bus_min;
147                 struct device_node *child;
148
149                 prop = of_get_property(np, "bus-range", NULL);
150                 if (!prop)
151                         continue;
152                 bus_min = be32_to_cpup(prop);
153
154                 bus = pci_find_bus(0, bus_min);
155                 if (!bus) {
156                         printk(KERN_ERR "Can't find a node for bus %s.\n",
157                                         np->full_name);
158                         continue;
159                 }
160
161                 if (bus->self)
162                         bus->self->dev.of_node = np;
163                 else
164                         bus->dev.of_node = np;
165
166                 for_each_child_of_node(np, child) {
167                         struct pci_dev *dev;
168                         u32 devfn;
169
170                         prop = of_get_property(child, "reg", NULL);
171                         if (!prop)
172                                 continue;
173
174                         devfn = (be32_to_cpup(prop) >> 8) & 0xff;
175                         dev = pci_get_slot(bus, devfn);
176                         if (!dev)
177                                 continue;
178                         dev->dev.of_node = child;
179                         pci_dev_put(dev);
180                 }
181         }
182 }
183 #endif
184
185 static void __init dtb_setup_hpet(void)
186 {
187         struct device_node *dn;
188         struct resource r;
189         int ret;
190
191         dn = of_find_compatible_node(NULL, NULL, "intel,ce4100-hpet");
192         if (!dn)
193                 return;
194         ret = of_address_to_resource(dn, 0, &r);
195         if (ret) {
196                 WARN_ON(1);
197                 return;
198         }
199         hpet_address = r.start;
200 }
201
202 static void __init dtb_lapic_setup(void)
203 {
204 #ifdef CONFIG_X86_LOCAL_APIC
205         if (apic_force_enable())
206                 return;
207
208         smp_found_config = 1;
209         pic_mode = 1;
210         /* Required for ioapic registration */
211         set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
212         if (boot_cpu_physical_apicid == -1U)
213                 boot_cpu_physical_apicid = read_apic_id();
214
215         generic_processor_info(boot_cpu_physical_apicid,
216                         GET_APIC_VERSION(apic_read(APIC_LVR)));
217 #endif
218 }
219
220 #ifdef CONFIG_X86_IO_APIC
221 static unsigned int ioapic_id;
222
223 static void __init dtb_add_ioapic(struct device_node *dn)
224 {
225         struct resource r;
226         int ret;
227
228         ret = of_address_to_resource(dn, 0, &r);
229         if (ret) {
230                 printk(KERN_ERR "Can't obtain address from node %s.\n",
231                                 dn->full_name);
232                 return;
233         }
234         mp_register_ioapic(++ioapic_id, r.start, gsi_top);
235 }
236
237 static void __init dtb_ioapic_setup(void)
238 {
239         struct device_node *dn;
240
241         if (!smp_found_config)
242                 return;
243
244         for_each_compatible_node(dn, NULL, "intel,ce4100-ioapic")
245                 dtb_add_ioapic(dn);
246
247         if (nr_ioapics) {
248                 of_ioapic = 1;
249                 return;
250         }
251         printk(KERN_ERR "Error: No information about IO-APIC in OF.\n");
252         smp_found_config = 0;
253 }
254 #else
255 static void __init dtb_ioapic_setup(void) {}
256 #endif
257
258 static void __init dtb_apic_setup(void)
259 {
260         dtb_lapic_setup();
261         dtb_ioapic_setup();
262 }
263
264 void __init x86_dtb_find_config(void)
265 {
266         if (initial_dtb)
267                 smp_found_config = 1;
268         else
269                 printk(KERN_ERR "Missing device tree!.\n");
270 }
271
272 void __init x86_dtb_get_config(unsigned int unused)
273 {
274         u32 size, map_len;
275         void *new_dtb;
276
277         if (!initial_dtb)
278                 return;
279
280         map_len = max(PAGE_SIZE - (initial_dtb & ~PAGE_MASK),
281                         (u64)sizeof(struct boot_param_header));
282
283         initial_boot_params = early_memremap(initial_dtb, map_len);
284         size = be32_to_cpu(initial_boot_params->totalsize);
285         if (map_len < size) {
286                 early_iounmap(initial_boot_params, map_len);
287                 initial_boot_params = early_memremap(initial_dtb, size);
288                 map_len = size;
289         }
290
291         new_dtb = alloc_bootmem(size);
292         memcpy(new_dtb, initial_boot_params, size);
293         early_iounmap(initial_boot_params, map_len);
294
295         initial_boot_params = new_dtb;
296
297         /* root level address cells */
298         of_scan_flat_dt(early_init_dt_scan_root, NULL);
299
300         unflatten_device_tree();
301         dtb_setup_hpet();
302         dtb_apic_setup();
303 }