ia64/xen-unstable

view unmodified_drivers/linux-2.6/platform-pci/platform-pci.c @ 13889:a68a3c6cac5f

Small cleanups to PV-on-HVM support code.
Signed-off-by: Keir Fraser <keir@xensource.com>
author Keir Fraser <keir@xensource.com>
date Thu Feb 08 17:38:30 2007 +0000 (2007-02-08)
parents 311b27546cf6
children e3dc8cea5bc0
line source
1 /******************************************************************************
2 * evtchn-pci.c
3 * xen event channel fake PCI device driver
4 * Copyright (C) 2005, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 *
19 */
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/errno.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/version.h>
28 #include <linux/interrupt.h>
29 #include <linux/vmalloc.h>
30 #include <linux/mm.h>
31 #include <asm/system.h>
32 #include <asm/io.h>
33 #include <asm/irq.h>
34 #include <asm/uaccess.h>
35 #include <asm/hypervisor.h>
36 #include <asm/pgtable.h>
37 #include <xen/interface/memory.h>
38 #include <xen/features.h>
39 #ifdef __ia64__
40 #include <asm/xen/xencomm.h>
41 #endif
43 #include "platform-pci.h"
45 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
46 #include <xen/platform-compat.h>
47 #endif
49 #define DRV_NAME "xen-platform-pci"
50 #define DRV_VERSION "0.10"
51 #define DRV_RELDATE "03/03/2005"
53 char *hypercall_stubs;
54 EXPORT_SYMBOL(hypercall_stubs);
56 // Used to be xiaofeng.ling@intel.com
57 MODULE_AUTHOR("ssmith@xensource.com");
58 MODULE_DESCRIPTION("Xen platform PCI device");
59 MODULE_LICENSE("GPL");
61 unsigned long *phys_to_machine_mapping;
62 EXPORT_SYMBOL(phys_to_machine_mapping);
64 static int __devinit init_xen_info(void)
65 {
66 unsigned long shared_info_frame;
67 struct xen_add_to_physmap xatp;
68 extern void *shared_info_area;
70 #ifdef __ia64__
71 xencomm_init();
72 #endif
74 setup_xen_features();
76 shared_info_frame = alloc_xen_mmio(PAGE_SIZE) >> PAGE_SHIFT;
77 xatp.domid = DOMID_SELF;
78 xatp.idx = 0;
79 xatp.space = XENMAPSPACE_shared_info;
80 xatp.gpfn = shared_info_frame;
81 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
82 BUG();
84 shared_info_area =
85 ioremap(shared_info_frame << PAGE_SHIFT, PAGE_SIZE);
86 if (shared_info_area == NULL)
87 panic("can't map shared info\n");
89 phys_to_machine_mapping = NULL;
91 gnttab_init();
93 return 0;
94 }
96 static void __devexit platform_pci_remove(struct pci_dev *pdev)
97 {
98 long ioaddr, iolen;
99 long mmio_addr, mmio_len;
101 ioaddr = pci_resource_start(pdev, 0);
102 iolen = pci_resource_len(pdev, 0);
103 mmio_addr = pci_resource_start(pdev, 1);
104 mmio_len = pci_resource_len(pdev, 1);
106 release_region(ioaddr, iolen);
107 release_mem_region(mmio_addr, mmio_len);
109 pci_set_drvdata(pdev, NULL);
110 free_irq(pdev->irq, pdev);
111 }
113 static unsigned long platform_mmio;
114 static unsigned long platform_mmio_alloc;
115 static unsigned long platform_mmiolen;
117 unsigned long alloc_xen_mmio(unsigned long len)
118 {
119 unsigned long addr;
121 addr = platform_mmio + platform_mmio_alloc;
122 platform_mmio_alloc += len;
123 BUG_ON(platform_mmio_alloc > platform_mmiolen);
125 return addr;
126 }
128 #ifndef __ia64__
129 /* Lifted from hvmloader.c */
130 static int get_hypercall_stubs(void)
131 {
132 uint32_t eax, ebx, ecx, edx, pages, msr, i;
133 char signature[13];
135 cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
136 *(uint32_t*)(signature + 0) = ebx;
137 *(uint32_t*)(signature + 4) = ecx;
138 *(uint32_t*)(signature + 8) = edx;
139 signature[12] = 0;
141 if (strcmp("XenVMMXenVMM", signature) || (eax < 0x40000002)) {
142 printk(KERN_WARNING
143 "Detected Xen platform device but not Xen VMM?"
144 " (sig %s, eax %x)\n",
145 signature, eax);
146 return -EINVAL;
147 }
149 cpuid(0x40000001, &eax, &ebx, &ecx, &edx);
151 printk(KERN_INFO "Xen version %d.%d.\n", eax >> 16, eax & 0xffff);
153 cpuid(0x40000002, &pages, &msr, &ecx, &edx);
155 printk(KERN_INFO "Hypercall area is %u pages.\n", pages);
157 /* Use __vmalloc() because vmalloc_exec() is not an exported symbol. */
158 /* PAGE_KERNEL_EXEC also is not exported, hence we use PAGE_KERNEL. */
159 /* hypercall_stubs = vmalloc_exec(pages * PAGE_SIZE); */
160 hypercall_stubs = __vmalloc(pages * PAGE_SIZE,
161 GFP_KERNEL | __GFP_HIGHMEM,
162 __pgprot(__PAGE_KERNEL & ~_PAGE_NX));
163 if (hypercall_stubs == NULL)
164 return -ENOMEM;
166 for (i = 0; i < pages; i++) {
167 unsigned long pfn;
168 pfn = vmalloc_to_pfn((char *)hypercall_stubs + i*PAGE_SIZE);
169 wrmsrl(msr, ((u64)pfn << PAGE_SHIFT) + i);
170 }
172 return 0;
173 }
174 #else /* __ia64__ */
175 #define get_hypercall_stubs() (0)
176 #endif
178 static uint64_t get_callback_via(struct pci_dev *pdev)
179 {
180 u8 pin;
181 int irq;
183 #ifdef __ia64__
184 for (irq = 0; irq < 16; irq++) {
185 if (isa_irq_to_vector(irq) == pdev->irq)
186 return irq; /* ISA IRQ */
187 }
188 #else /* !__ia64__ */
189 irq = pdev->irq;
190 if (irq < 16)
191 return irq; /* ISA IRQ */
192 #endif
194 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
195 pin = pdev->pin;
196 #else
197 pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
198 #endif
200 /* We don't know the GSI. Specify the PCI INTx line instead. */
201 return (((uint64_t)0x01 << 56) | /* PCI INTx identifier */
202 ((uint64_t)pci_domain_nr(pdev->bus) << 32) |
203 ((uint64_t)pdev->bus->number << 16) |
204 ((uint64_t)(pdev->devfn & 0xff) << 8) |
205 ((uint64_t)(pin - 1) & 3));
206 }
208 /* Invalidate foreign mappings (e.g., in qemu-based device model). */
209 static uint16_t invlmap_port;
210 void xen_invalidate_foreign_mappings(void)
211 {
212 outb(0, invlmap_port);
213 }
214 EXPORT_SYMBOL(xen_invalidate_foreign_mappings);
216 static int __devinit platform_pci_init(struct pci_dev *pdev,
217 const struct pci_device_id *ent)
218 {
219 int i, ret;
220 long ioaddr, iolen;
221 long mmio_addr, mmio_len;
222 uint64_t callback_via;
224 i = pci_enable_device(pdev);
225 if (i)
226 return i;
228 ioaddr = pci_resource_start(pdev, 0);
229 iolen = pci_resource_len(pdev, 0);
231 mmio_addr = pci_resource_start(pdev, 1);
232 mmio_len = pci_resource_len(pdev, 1);
234 callback_via = get_callback_via(pdev);
236 if (mmio_addr == 0 || ioaddr == 0 || callback_via == 0) {
237 printk(KERN_WARNING DRV_NAME ":no resources found\n");
238 return -ENOENT;
239 }
241 invlmap_port = ioaddr;
243 if (request_mem_region(mmio_addr, mmio_len, DRV_NAME) == NULL)
244 {
245 printk(KERN_ERR ":MEM I/O resource 0x%lx @ 0x%lx busy\n",
246 mmio_addr, mmio_len);
247 return -EBUSY;
248 }
250 if (request_region(ioaddr, iolen, DRV_NAME) == NULL)
251 {
252 printk(KERN_ERR DRV_NAME ":I/O resource 0x%lx @ 0x%lx busy\n",
253 iolen, ioaddr);
254 release_mem_region(mmio_addr, mmio_len);
255 return -EBUSY;
256 }
258 platform_mmio = mmio_addr;
259 platform_mmiolen = mmio_len;
261 ret = get_hypercall_stubs();
262 if (ret < 0)
263 goto out;
265 if ((ret = init_xen_info()))
266 goto out;
268 if ((ret = request_irq(pdev->irq, evtchn_interrupt,
269 SA_SHIRQ | SA_SAMPLE_RANDOM,
270 "xen-platform-pci", pdev)))
271 goto out;
273 if ((ret = set_callback_via(callback_via)))
274 goto out;
276 out:
277 if (ret) {
278 release_mem_region(mmio_addr, mmio_len);
279 release_region(ioaddr, iolen);
280 }
282 return ret;
283 }
285 #define XEN_PLATFORM_VENDOR_ID 0x5853
286 #define XEN_PLATFORM_DEVICE_ID 0x0001
287 static struct pci_device_id platform_pci_tbl[] __devinitdata = {
288 {XEN_PLATFORM_VENDOR_ID, XEN_PLATFORM_DEVICE_ID,
289 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
290 /* Continue to recognise the old ID for now */
291 {0xfffd, 0x0101, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
292 {0,}
293 };
295 MODULE_DEVICE_TABLE(pci, platform_pci_tbl);
297 static struct pci_driver platform_driver = {
298 name: DRV_NAME,
299 probe: platform_pci_init,
300 remove: __devexit_p(platform_pci_remove),
301 id_table: platform_pci_tbl,
302 };
304 static int pci_device_registered;
306 static int __init platform_pci_module_init(void)
307 {
308 int rc;
310 rc = pci_module_init(&platform_driver);
311 if (rc)
312 printk(KERN_INFO DRV_NAME ":No platform pci device model found\n");
313 else
314 pci_device_registered = 1;
316 return rc;
317 }
319 static void __exit platform_pci_module_cleanup(void)
320 {
321 printk(KERN_INFO DRV_NAME ":Do platform module cleanup\n");
322 /* disable hypervisor for callback irq */
323 set_callback_via(0);
324 if (pci_device_registered)
325 pci_unregister_driver(&platform_driver);
326 }
328 module_init(platform_pci_module_init);
329 module_exit(platform_pci_module_cleanup);