Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / x86 / drivers / xen / hvm.c
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <ipxe/malloc.h>
26 #include <ipxe/pci.h>
27 #include <ipxe/cpuid.h>
28 #include <ipxe/msr.h>
29 #include <ipxe/xen.h>
30 #include <ipxe/xenver.h>
31 #include <ipxe/xenmem.h>
32 #include <ipxe/xenstore.h>
33 #include <ipxe/xenbus.h>
34 #include <ipxe/xengrant.h>
35 #include "hvm.h"
36
37 /** @file
38  *
39  * Xen HVM driver
40  *
41  */
42
43 /**
44  * Get CPUID base
45  *
46  * @v hvm               HVM device
47  * @ret rc              Return status code
48  */
49 static int hvm_cpuid_base ( struct hvm_device *hvm ) {
50         struct {
51                 uint32_t ebx;
52                 uint32_t ecx;
53                 uint32_t edx;
54         } __attribute__ (( packed )) signature;
55         uint32_t base;
56         uint32_t version;
57         uint32_t discard_eax;
58         uint32_t discard_ebx;
59         uint32_t discard_ecx;
60         uint32_t discard_edx;
61
62         /* Scan for magic signature */
63         for ( base = HVM_CPUID_MIN ; base <= HVM_CPUID_MAX ;
64               base += HVM_CPUID_STEP ) {
65                 cpuid ( base, &discard_eax, &signature.ebx, &signature.ecx,
66                         &signature.edx );
67                 if ( memcmp ( &signature, HVM_CPUID_MAGIC,
68                               sizeof ( signature ) ) == 0 ) {
69                         hvm->cpuid_base = base;
70                         cpuid ( ( base + HVM_CPUID_VERSION ), &version,
71                                 &discard_ebx, &discard_ecx, &discard_edx );
72                         DBGC2 ( hvm, "HVM using CPUID base %#08x (v%d.%d)\n",
73                                 base, ( version >> 16 ), ( version & 0xffff ) );
74                         return 0;
75                 }
76         }
77
78         DBGC ( hvm, "HVM could not find hypervisor\n" );
79         return -ENODEV;
80 }
81
82 /**
83  * Map hypercall page(s)
84  *
85  * @v hvm               HVM device
86  * @ret rc              Return status code
87  */
88 static int hvm_map_hypercall ( struct hvm_device *hvm ) {
89         uint32_t pages;
90         uint32_t msr;
91         uint32_t discard_ecx;
92         uint32_t discard_edx;
93         physaddr_t hypercall_phys;
94         uint32_t version;
95         static xen_extraversion_t extraversion;
96         int xenrc;
97         int rc;
98
99         /* Get number of hypercall pages and MSR to use */
100         cpuid ( ( hvm->cpuid_base + HVM_CPUID_PAGES ), &pages, &msr,
101                 &discard_ecx, &discard_edx );
102
103         /* Allocate pages */
104         hvm->hypercall_len = ( pages * PAGE_SIZE );
105         hvm->xen.hypercall = malloc_dma ( hvm->hypercall_len, PAGE_SIZE );
106         if ( ! hvm->xen.hypercall ) {
107                 DBGC ( hvm, "HVM could not allocate %d hypercall page(s)\n",
108                        pages );
109                 return -ENOMEM;
110         }
111         hypercall_phys = virt_to_phys ( hvm->xen.hypercall );
112         DBGC2 ( hvm, "HVM hypercall page(s) at [%#08lx,%#08lx) via MSR %#08x\n",
113                 hypercall_phys, ( hypercall_phys + hvm->hypercall_len ), msr );
114
115         /* Write to MSR */
116         wrmsr ( msr, hypercall_phys );
117
118         /* Check that hypercall mechanism is working */
119         version = xenver_version ( &hvm->xen );
120         if ( ( xenrc = xenver_extraversion ( &hvm->xen, &extraversion ) ) != 0){
121                 rc = -EXEN ( xenrc );
122                 DBGC ( hvm, "HVM could not get extraversion: %s\n",
123                        strerror ( rc ) );
124                 return rc;
125         }
126         DBGC2 ( hvm, "HVM found Xen version %d.%d%s\n",
127                 ( version >> 16 ), ( version & 0xffff ) , extraversion );
128
129         return 0;
130 }
131
132 /**
133  * Unmap hypercall page(s)
134  *
135  * @v hvm               HVM device
136  */
137 static void hvm_unmap_hypercall ( struct hvm_device *hvm ) {
138
139         /* Free pages */
140         free_dma ( hvm->xen.hypercall, hvm->hypercall_len );
141 }
142
143 /**
144  * Allocate and map MMIO space
145  *
146  * @v hvm               HVM device
147  * @v space             Source mapping space
148  * @v len               Length (must be a multiple of PAGE_SIZE)
149  * @ret mmio            MMIO space address, or NULL on error
150  */
151 static void * hvm_ioremap ( struct hvm_device *hvm, unsigned int space,
152                             size_t len ) {
153         struct xen_add_to_physmap add;
154         struct xen_remove_from_physmap remove;
155         unsigned int pages = ( len / PAGE_SIZE );
156         physaddr_t mmio_phys;
157         unsigned int i;
158         void *mmio;
159         int xenrc;
160         int rc;
161
162         /* Sanity check */
163         assert ( ( len % PAGE_SIZE ) == 0 );
164
165         /* Check for available space */
166         if ( ( hvm->mmio_offset + len ) > hvm->mmio_len ) {
167                 DBGC ( hvm, "HVM could not allocate %zd bytes of MMIO space "
168                        "(%zd of %zd remaining)\n", len,
169                        ( hvm->mmio_len - hvm->mmio_offset ), hvm->mmio_len );
170                 goto err_no_space;
171         }
172
173         /* Map this space */
174         mmio = ioremap ( ( hvm->mmio + hvm->mmio_offset ), len );
175         if ( ! mmio ) {
176                 DBGC ( hvm, "HVM could not map MMIO space [%08lx,%08lx)\n",
177                        ( hvm->mmio + hvm->mmio_offset ),
178                        ( hvm->mmio + hvm->mmio_offset + len ) );
179                 goto err_ioremap;
180         }
181         mmio_phys = virt_to_phys ( mmio );
182
183         /* Add to physical address space */
184         for ( i = 0 ; i < pages ; i++ ) {
185                 add.domid = DOMID_SELF;
186                 add.idx = i;
187                 add.space = space;
188                 add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
189                 if ( ( xenrc = xenmem_add_to_physmap ( &hvm->xen, &add ) ) !=0){
190                         rc = -EXEN ( xenrc );
191                         DBGC ( hvm, "HVM could not add space %d idx %d at "
192                                "[%08lx,%08lx): %s\n", space, i,
193                                ( mmio_phys + ( i * PAGE_SIZE ) ),
194                                ( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
195                                strerror ( rc ) );
196                         goto err_add_to_physmap;
197                 }
198         }
199
200         /* Update offset */
201         hvm->mmio_offset += len;
202
203         return mmio;
204
205         i = pages;
206  err_add_to_physmap:
207         for ( i-- ; ( signed int ) i >= 0 ; i-- ) {
208                 remove.domid = DOMID_SELF;
209                 add.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
210                 xenmem_remove_from_physmap ( &hvm->xen, &remove );
211         }
212         iounmap ( mmio );
213  err_ioremap:
214  err_no_space:
215         return NULL;
216 }
217
218 /**
219  * Unmap MMIO space
220  *
221  * @v hvm               HVM device
222  * @v mmio              MMIO space address
223  * @v len               Length (must be a multiple of PAGE_SIZE)
224  */
225 static void hvm_iounmap ( struct hvm_device *hvm, void *mmio, size_t len ) {
226         struct xen_remove_from_physmap remove;
227         physaddr_t mmio_phys = virt_to_phys ( mmio );
228         unsigned int pages = ( len / PAGE_SIZE );
229         unsigned int i;
230         int xenrc;
231         int rc;
232
233         /* Unmap this space */
234         iounmap ( mmio );
235
236         /* Remove from physical address space */
237         for ( i = 0 ; i < pages ; i++ ) {
238                 remove.domid = DOMID_SELF;
239                 remove.gpfn = ( ( mmio_phys / PAGE_SIZE ) + i );
240                 if ( ( xenrc = xenmem_remove_from_physmap ( &hvm->xen,
241                                                             &remove ) ) != 0 ) {
242                         rc = -EXEN ( xenrc );
243                         DBGC ( hvm, "HVM could not remove space [%08lx,%08lx): "
244                                "%s\n", ( mmio_phys + ( i * PAGE_SIZE ) ),
245                                ( mmio_phys + ( ( i + 1 ) * PAGE_SIZE ) ),
246                                strerror ( rc ) );
247                         /* Nothing we can do about this */
248                 }
249         }
250 }
251
252 /**
253  * Map shared info page
254  *
255  * @v hvm               HVM device
256  * @ret rc              Return status code
257  */
258 static int hvm_map_shared_info ( struct hvm_device *hvm ) {
259         physaddr_t shared_info_phys;
260         int rc;
261
262         /* Map shared info page */
263         hvm->xen.shared = hvm_ioremap ( hvm, XENMAPSPACE_shared_info,
264                                         PAGE_SIZE );
265         if ( ! hvm->xen.shared ) {
266                 rc = -ENOMEM;
267                 goto err_alloc;
268         }
269         shared_info_phys = virt_to_phys ( hvm->xen.shared );
270         DBGC2 ( hvm, "HVM shared info page at [%#08lx,%#08lx)\n",
271                 shared_info_phys, ( shared_info_phys + PAGE_SIZE ) );
272
273         /* Sanity check */
274         DBGC2 ( hvm, "HVM wallclock time is %d\n",
275                 readl ( &hvm->xen.shared->wc_sec ) );
276
277         return 0;
278
279         hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
280  err_alloc:
281         return rc;
282 }
283
284 /**
285  * Unmap shared info page
286  *
287  * @v hvm               HVM device
288  */
289 static void hvm_unmap_shared_info ( struct hvm_device *hvm ) {
290
291         /* Unmap shared info page */
292         hvm_iounmap ( hvm, hvm->xen.shared, PAGE_SIZE );
293 }
294
295 /**
296  * Map grant table
297  *
298  * @v hvm               HVM device
299  * @ret rc              Return status code
300  */
301 static int hvm_map_grant ( struct hvm_device *hvm ) {
302         physaddr_t grant_phys;
303         int rc;
304
305         /* Initialise grant table */
306         if ( ( rc = xengrant_init ( &hvm->xen ) ) != 0 ) {
307                 DBGC ( hvm, "HVM could not initialise grant table: %s\n",
308                        strerror ( rc ) );
309                 return rc;
310         }
311
312         /* Map grant table */
313         hvm->xen.grant.table = hvm_ioremap ( hvm, XENMAPSPACE_grant_table,
314                                              hvm->xen.grant.len );
315         if ( ! hvm->xen.grant.table )
316                 return -ENODEV;
317
318         grant_phys = virt_to_phys ( hvm->xen.grant.table );
319         DBGC2 ( hvm, "HVM mapped grant table at [%08lx,%08lx)\n",
320                 grant_phys, ( grant_phys + hvm->xen.grant.len ) );
321         return 0;
322 }
323
324 /**
325  * Unmap grant table
326  *
327  * @v hvm               HVM device
328  */
329 static void hvm_unmap_grant ( struct hvm_device *hvm ) {
330
331         /* Unmap grant table */
332         hvm_iounmap ( hvm, hvm->xen.grant.table, hvm->xen.grant.len );
333 }
334
335 /**
336  * Map XenStore
337  *
338  * @v hvm               HVM device
339  * @ret rc              Return status code
340  */
341 static int hvm_map_xenstore ( struct hvm_device *hvm ) {
342         uint64_t xenstore_evtchn;
343         uint64_t xenstore_pfn;
344         physaddr_t xenstore_phys;
345         char *name;
346         int xenrc;
347         int rc;
348
349         /* Get XenStore event channel */
350         if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_EVTCHN,
351                                            &xenstore_evtchn ) ) != 0 ) {
352                 rc = -EXEN ( xenrc );
353                 DBGC ( hvm, "HVM could not get XenStore event channel: %s\n",
354                        strerror ( rc ) );
355                 return rc;
356         }
357         hvm->xen.store.port = xenstore_evtchn;
358
359         /* Get XenStore PFN */
360         if ( ( xenrc = xen_hvm_get_param ( &hvm->xen, HVM_PARAM_STORE_PFN,
361                                            &xenstore_pfn ) ) != 0 ) {
362                 rc = -EXEN ( xenrc );
363                 DBGC ( hvm, "HVM could not get XenStore PFN: %s\n",
364                        strerror ( rc ) );
365                 return rc;
366         }
367         xenstore_phys = ( xenstore_pfn * PAGE_SIZE );
368
369         /* Map XenStore */
370         hvm->xen.store.intf = ioremap ( xenstore_phys, PAGE_SIZE );
371         if ( ! hvm->xen.store.intf ) {
372                 DBGC ( hvm, "HVM could not map XenStore at [%08lx,%08lx)\n",
373                        xenstore_phys, ( xenstore_phys + PAGE_SIZE ) );
374                 return -ENODEV;
375         }
376         DBGC2 ( hvm, "HVM mapped XenStore at [%08lx,%08lx) with event port "
377                 "%d\n", xenstore_phys, ( xenstore_phys + PAGE_SIZE ),
378                 hvm->xen.store.port );
379
380         /* Check that XenStore is working */
381         if ( ( rc = xenstore_read ( &hvm->xen, &name, "name", NULL ) ) != 0 ) {
382                 DBGC ( hvm, "HVM could not read domain name: %s\n",
383                        strerror ( rc ) );
384                 return rc;
385         }
386         DBGC2 ( hvm, "HVM running in domain \"%s\"\n", name );
387         free ( name );
388
389         return 0;
390 }
391
392 /**
393  * Unmap XenStore
394  *
395  * @v hvm               HVM device
396  */
397 static void hvm_unmap_xenstore ( struct hvm_device *hvm ) {
398
399         /* Unmap XenStore */
400         iounmap ( hvm->xen.store.intf );
401 }
402
403 /**
404  * Probe PCI device
405  *
406  * @v pci               PCI device
407  * @ret rc              Return status code
408  */
409 static int hvm_probe ( struct pci_device *pci ) {
410         struct hvm_device *hvm;
411         int rc;
412
413         /* Allocate and initialise structure */
414         hvm = zalloc ( sizeof ( *hvm ) );
415         if ( ! hvm ) {
416                 rc = -ENOMEM;
417                 goto err_alloc;
418         }
419         hvm->mmio = pci_bar_start ( pci, HVM_MMIO_BAR );
420         hvm->mmio_len = pci_bar_size ( pci, HVM_MMIO_BAR );
421         DBGC2 ( hvm, "HVM has MMIO space [%08lx,%08lx)\n",
422                 hvm->mmio, ( hvm->mmio + hvm->mmio_len ) );
423
424         /* Fix up PCI device */
425         adjust_pci_device ( pci );
426
427         /* Attach to hypervisor */
428         if ( ( rc = hvm_cpuid_base ( hvm ) ) != 0 )
429                 goto err_cpuid_base;
430         if ( ( rc = hvm_map_hypercall ( hvm ) ) != 0 )
431                 goto err_map_hypercall;
432         if ( ( rc = hvm_map_shared_info ( hvm ) ) != 0 )
433                 goto err_map_shared_info;
434         if ( ( rc = hvm_map_grant ( hvm ) ) != 0 )
435                 goto err_map_grant;
436         if ( ( rc = hvm_map_xenstore ( hvm ) ) != 0 )
437                 goto err_map_xenstore;
438
439         /* Probe Xen devices */
440         if ( ( rc = xenbus_probe ( &hvm->xen, &pci->dev ) ) != 0 ) {
441                 DBGC ( hvm, "HVM could not probe Xen bus: %s\n",
442                        strerror ( rc ) );
443                 goto err_xenbus_probe;
444         }
445
446         pci_set_drvdata ( pci, hvm );
447         return 0;
448
449         xenbus_remove ( &hvm->xen, &pci->dev );
450  err_xenbus_probe:
451         hvm_unmap_xenstore ( hvm );
452  err_map_xenstore:
453         hvm_unmap_grant ( hvm );
454  err_map_grant:
455         hvm_unmap_shared_info ( hvm );
456  err_map_shared_info:
457         hvm_unmap_hypercall ( hvm );
458  err_map_hypercall:
459  err_cpuid_base:
460         free ( hvm );
461  err_alloc:
462         return rc;
463 }
464
465 /**
466  * Remove PCI device
467  *
468  * @v pci               PCI device
469  */
470 static void hvm_remove ( struct pci_device *pci ) {
471         struct hvm_device *hvm = pci_get_drvdata ( pci );
472
473         xenbus_remove ( &hvm->xen, &pci->dev );
474         hvm_unmap_xenstore ( hvm );
475         hvm_unmap_grant ( hvm );
476         hvm_unmap_shared_info ( hvm );
477         hvm_unmap_hypercall ( hvm );
478         free ( hvm );
479 }
480
481 /** PCI device IDs */
482 static struct pci_device_id hvm_ids[] = {
483         PCI_ROM ( 0x5853, 0x0001, "hvm", "hvm", 0 ),
484         PCI_ROM ( 0x5853, 0x0002, "hvm2", "hvm2", 0 ),
485 };
486
487 /** PCI driver */
488 struct pci_driver hvm_driver __pci_driver = {
489         .ids = hvm_ids,
490         .id_count = ( sizeof ( hvm_ids ) / sizeof ( hvm_ids[0] ) ),
491         .probe = hvm_probe,
492         .remove = hvm_remove,
493 };
494
495 /* Drag in netfront driver */
496 REQUIRE_OBJECT ( netfront );