These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / seabios / src / fw / paravirt.c
1 // Paravirtualization support.
2 //
3 // Copyright (C) 2013  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2009 Red Hat Inc.
5 //
6 // Authors:
7 //  Gleb Natapov <gnatapov@redhat.com>
8 //
9 // This file may be distributed under the terms of the GNU LGPLv3 license.
10
11 #include "byteorder.h" // be32_to_cpu
12 #include "config.h" // CONFIG_QEMU
13 #include "e820map.h" // e820_add
14 #include "hw/pci.h" // create_pirtable
15 #include "hw/pci_regs.h" // PCI_DEVICE_ID
16 #include "hw/rtc.h" // CMOS_*
17 #include "malloc.h" // malloc_tmp
18 #include "output.h" // dprintf
19 #include "paravirt.h" // qemu_cfg_preinit
20 #include "romfile.h" // romfile_loadint
21 #include "romfile_loader.h" // romfile_loader_execute
22 #include "string.h" // memset
23 #include "util.h" // pci_setup
24 #include "x86.h" // cpuid
25 #include "xen.h" // xen_biostable_setup
26 #include "stacks.h" // yield
27
28 // Amount of continuous ram under 4Gig
29 u32 RamSize;
30 // Amount of continuous ram >4Gig
31 u64 RamSizeOver4G;
32 // Type of emulator platform.
33 int PlatformRunningOn VARFSEG;
34 // cfg_dma enabled
35 int cfg_dma_enabled = 0;
36
37 inline int qemu_cfg_dma_enabled(void)
38 {
39     return cfg_dma_enabled;
40 }
41
42 /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx.  It
43  * should be used to determine that a VM is running under KVM.
44  */
45 #define KVM_CPUID_SIGNATURE     0x40000000
46
47 static void kvm_detect(void)
48 {
49     unsigned int eax, ebx, ecx, edx;
50     char signature[13];
51
52     cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
53     memcpy(signature + 0, &ebx, 4);
54     memcpy(signature + 4, &ecx, 4);
55     memcpy(signature + 8, &edx, 4);
56     signature[12] = 0;
57
58     if (strcmp(signature, "KVMKVMKVM") == 0) {
59         dprintf(1, "Running on KVM\n");
60         PlatformRunningOn |= PF_KVM;
61     }
62 }
63
64 static void qemu_detect(void)
65 {
66     if (!CONFIG_QEMU_HARDWARE)
67         return;
68
69     // check northbridge @ 00:00.0
70     u16 v = pci_config_readw(0, PCI_VENDOR_ID);
71     if (v == 0x0000 || v == 0xffff)
72         return;
73     u16 d = pci_config_readw(0, PCI_DEVICE_ID);
74     u16 sv = pci_config_readw(0, PCI_SUBSYSTEM_VENDOR_ID);
75     u16 sd = pci_config_readw(0, PCI_SUBSYSTEM_ID);
76
77     if (sv != 0x1af4 || /* Red Hat, Inc */
78         sd != 0x1100)   /* Qemu virtual machine */
79         return;
80
81     PlatformRunningOn |= PF_QEMU;
82     switch (d) {
83     case 0x1237:
84         dprintf(1, "Running on QEMU (i440fx)\n");
85         break;
86     case 0x29c0:
87         dprintf(1, "Running on QEMU (q35)\n");
88         break;
89     default:
90         dprintf(1, "Running on QEMU (unknown nb: %04x:%04x)\n", v, d);
91         break;
92     }
93     kvm_detect();
94 }
95
96 void
97 qemu_preinit(void)
98 {
99     qemu_detect();
100
101     if (!CONFIG_QEMU)
102         return;
103
104     if (runningOnXen()) {
105         xen_ramsize_preinit();
106         return;
107     }
108
109     if (!runningOnQEMU()) {
110         dprintf(1, "Warning: No QEMU Northbridge found (isapc?)\n");
111         PlatformRunningOn |= PF_QEMU;
112         kvm_detect();
113     }
114
115     // On emulators, get memory size from nvram.
116     u32 rs = ((rtc_read(CMOS_MEM_EXTMEM2_LOW) << 16)
117               | (rtc_read(CMOS_MEM_EXTMEM2_HIGH) << 24));
118     if (rs)
119         rs += 16 * 1024 * 1024;
120     else
121         rs = (((rtc_read(CMOS_MEM_EXTMEM_LOW) << 10)
122                | (rtc_read(CMOS_MEM_EXTMEM_HIGH) << 18))
123               + 1 * 1024 * 1024);
124     RamSize = rs;
125     e820_add(0, rs, E820_RAM);
126
127     /* reserve 256KB BIOS area at the end of 4 GB */
128     e820_add(0xfffc0000, 256*1024, E820_RESERVED);
129
130     dprintf(1, "RamSize: 0x%08x [cmos]\n", RamSize);
131 }
132
133 void
134 qemu_platform_setup(void)
135 {
136     if (!CONFIG_QEMU)
137         return;
138
139     if (runningOnXen()) {
140         pci_probe_devices();
141         xen_hypercall_setup();
142         xen_biostable_setup();
143         return;
144     }
145
146     // Initialize pci
147     pci_setup();
148     smm_device_setup();
149     smm_setup();
150
151     // Initialize mtrr and smp
152     mtrr_setup();
153     smp_setup();
154
155     // Create bios tables
156     pirtable_setup();
157     mptable_setup();
158     smbios_setup();
159
160     if (CONFIG_FW_ROMFILE_LOAD) {
161         int loader_err;
162
163         dprintf(3, "load ACPI tables\n");
164
165         loader_err = romfile_loader_execute("etc/table-loader");
166
167         RsdpAddr = find_acpi_rsdp();
168
169         if (RsdpAddr)
170             return;
171
172         /* If present, loader should have installed an RSDP.
173          * Not installed? We might still be able to continue
174          * using the builtin RSDP.
175          */
176         if (!loader_err)
177             warn_internalerror();
178     }
179
180     acpi_setup();
181 }
182
183
184 /****************************************************************
185  * QEMU firmware config (fw_cfg) interface
186  ****************************************************************/
187
188 // List of QEMU fw_cfg entries.  DO NOT ADD MORE.  (All new content
189 // should be passed via the fw_cfg "file" interface.)
190 #define QEMU_CFG_SIGNATURE              0x00
191 #define QEMU_CFG_ID                     0x01
192 #define QEMU_CFG_UUID                   0x02
193 #define QEMU_CFG_NUMA                   0x0d
194 #define QEMU_CFG_BOOT_MENU              0x0e
195 #define QEMU_CFG_MAX_CPUS               0x0f
196 #define QEMU_CFG_FILE_DIR               0x19
197 #define QEMU_CFG_ARCH_LOCAL             0x8000
198 #define QEMU_CFG_ACPI_TABLES            (QEMU_CFG_ARCH_LOCAL + 0)
199 #define QEMU_CFG_SMBIOS_ENTRIES         (QEMU_CFG_ARCH_LOCAL + 1)
200 #define QEMU_CFG_IRQ0_OVERRIDE          (QEMU_CFG_ARCH_LOCAL + 2)
201 #define QEMU_CFG_E820_TABLE             (QEMU_CFG_ARCH_LOCAL + 3)
202
203 static void
204 qemu_cfg_select(u16 f)
205 {
206     outw(f, PORT_QEMU_CFG_CTL);
207 }
208
209 static void
210 qemu_cfg_dma_transfer(void *address, u32 length, u32 control)
211 {
212     QemuCfgDmaAccess access;
213
214     access.address = cpu_to_be64((u64)(u32)address);
215     access.length = cpu_to_be32(length);
216     access.control = cpu_to_be32(control);
217
218     barrier();
219
220     outl(cpu_to_be32((u32)&access), PORT_QEMU_CFG_DMA_ADDR_LOW);
221
222     while(be32_to_cpu(access.control) & ~QEMU_CFG_DMA_CTL_ERROR) {
223         yield();
224     }
225 }
226
227 static void
228 qemu_cfg_read(void *buf, int len)
229 {
230     if (len == 0) {
231         return;
232     }
233
234     if (qemu_cfg_dma_enabled()) {
235         qemu_cfg_dma_transfer(buf, len, QEMU_CFG_DMA_CTL_READ);
236     } else {
237         insb(PORT_QEMU_CFG_DATA, buf, len);
238     }
239 }
240
241 static void
242 qemu_cfg_skip(int len)
243 {
244     if (len == 0) {
245         return;
246     }
247
248     if (qemu_cfg_dma_enabled()) {
249         qemu_cfg_dma_transfer(0, len, QEMU_CFG_DMA_CTL_SKIP);
250     } else {
251         while (len--)
252             inb(PORT_QEMU_CFG_DATA);
253     }
254 }
255
256 static void
257 qemu_cfg_read_entry(void *buf, int e, int len)
258 {
259     if (qemu_cfg_dma_enabled()) {
260         u32 control = (e << 16) | QEMU_CFG_DMA_CTL_SELECT
261                         | QEMU_CFG_DMA_CTL_READ;
262         qemu_cfg_dma_transfer(buf, len, control);
263     } else {
264         qemu_cfg_select(e);
265         qemu_cfg_read(buf, len);
266     }
267 }
268
269 struct qemu_romfile_s {
270     struct romfile_s file;
271     int select, skip;
272 };
273
274 static int
275 qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
276 {
277     if (file->size > maxlen)
278         return -1;
279     struct qemu_romfile_s *qfile;
280     qfile = container_of(file, struct qemu_romfile_s, file);
281     if (qfile->skip == 0) {
282         /* Do it in one transfer */
283         qemu_cfg_read_entry(dst, qfile->select, file->size);
284     } else {
285         qemu_cfg_select(qfile->select);
286         qemu_cfg_skip(qfile->skip);
287         qemu_cfg_read(dst, file->size);
288     }
289     return file->size;
290 }
291
292 static void
293 qemu_romfile_add(char *name, int select, int skip, int size)
294 {
295     struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
296     if (!qfile) {
297         warn_noalloc();
298         return;
299     }
300     memset(qfile, 0, sizeof(*qfile));
301     strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
302     qfile->file.size = size;
303     qfile->select = select;
304     qfile->skip = skip;
305     qfile->file.copy = qemu_cfg_read_file;
306     romfile_add(&qfile->file);
307 }
308
309 struct e820_reservation {
310     u64 address;
311     u64 length;
312     u32 type;
313 };
314
315 #define SMBIOS_FIELD_ENTRY 0
316 #define SMBIOS_TABLE_ENTRY 1
317
318 struct qemu_smbios_header {
319     u16 length;
320     u8 headertype;
321     u8 tabletype;
322     u16 fieldoffset;
323 } PACKED;
324
325 static void
326 qemu_cfg_e820(void)
327 {
328     struct e820_reservation *table;
329     int i, size;
330
331     if (!CONFIG_QEMU)
332         return;
333
334     // "etc/e820" has both ram and reservations
335     table = romfile_loadfile("etc/e820", &size);
336     if (table) {
337         for (i = 0; i < size / sizeof(struct e820_reservation); i++) {
338             switch (table[i].type) {
339             case E820_RAM:
340                 dprintf(1, "RamBlock: addr 0x%016llx len 0x%016llx [e820]\n",
341                         table[i].address, table[i].length);
342                 if (table[i].address < RamSize)
343                     // ignore, preinit got it from cmos already and
344                     // adding this again would ruin any reservations
345                     // done so far
346                     continue;
347                 if (table[i].address < 0x100000000LL) {
348                     // below 4g -- adjust RamSize to mark highest lowram addr
349                     if (RamSize < table[i].address + table[i].length)
350                         RamSize = table[i].address + table[i].length;
351                 } else {
352                     // above 4g -- adjust RamSizeOver4G to mark highest ram addr
353                     if (0x100000000LL + RamSizeOver4G < table[i].address + table[i].length)
354                         RamSizeOver4G = table[i].address + table[i].length - 0x100000000LL;
355                 }
356                 /* fall through */
357             case E820_RESERVED:
358                 e820_add(table[i].address, table[i].length, table[i].type);
359                 break;
360             default:
361                 /*
362                  * Qemu 1.7 uses RAM + RESERVED only.  Ignore
363                  * everything else, so we have the option to
364                  * extend this in the future without breakage.
365                  */
366                 break;
367             }
368         }
369         return;
370     }
371
372     // QEMU_CFG_E820_TABLE has reservations only
373     u32 count32;
374     qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
375     if (count32) {
376         struct e820_reservation entry;
377         int i;
378         for (i = 0; i < count32; i++) {
379             qemu_cfg_read(&entry, sizeof(entry));
380             e820_add(entry.address, entry.length, entry.type);
381         }
382     } else if (runningOnKVM()) {
383         // Backwards compatibility - provide hard coded range.
384         // 4 pages before the bios, 3 pages for vmx tss pages, the
385         // other page for EPT real mode pagetable
386         e820_add(0xfffbc000, 4*4096, E820_RESERVED);
387     }
388
389     // Check for memory over 4Gig in cmos
390     u64 high = ((rtc_read(CMOS_MEM_HIGHMEM_LOW) << 16)
391                 | ((u32)rtc_read(CMOS_MEM_HIGHMEM_MID) << 24)
392                 | ((u64)rtc_read(CMOS_MEM_HIGHMEM_HIGH) << 32));
393     RamSizeOver4G = high;
394     e820_add(0x100000000ull, high, E820_RAM);
395     dprintf(1, "RamSizeOver4G: 0x%016llx [cmos]\n", RamSizeOver4G);
396 }
397
398 // Populate romfile entries for legacy fw_cfg ports (that predate the
399 // "file" interface).
400 static void
401 qemu_cfg_legacy(void)
402 {
403     if (!CONFIG_QEMU)
404         return;
405
406     // Misc config items.
407     qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
408     qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
409     qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
410
411     // NUMA data
412     u64 numacount;
413     qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
414     int max_cpu = romfile_loadint("etc/max-cpus", 0);
415     qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
416                      , max_cpu*sizeof(u64));
417     qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
418                      , sizeof(numacount) + max_cpu*sizeof(u64)
419                      , numacount*sizeof(u64));
420
421     // ACPI tables
422     char name[128];
423     u16 cnt;
424     qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
425     int i, offset = sizeof(cnt);
426     for (i = 0; i < cnt; i++) {
427         u16 len;
428         qemu_cfg_read(&len, sizeof(len));
429         offset += sizeof(len);
430         snprintf(name, sizeof(name), "acpi/table%d", i);
431         qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
432         qemu_cfg_skip(len);
433         offset += len;
434     }
435
436     // SMBIOS info
437     qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
438     offset = sizeof(cnt);
439     for (i = 0; i < cnt; i++) {
440         struct qemu_smbios_header header;
441         qemu_cfg_read(&header, sizeof(header));
442         if (header.headertype == SMBIOS_FIELD_ENTRY) {
443             snprintf(name, sizeof(name), "smbios/field%d-%d"
444                      , header.tabletype, header.fieldoffset);
445             qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
446                              , offset + sizeof(header)
447                              , header.length - sizeof(header));
448         } else {
449             snprintf(name, sizeof(name), "smbios/table%d-%d"
450                      , header.tabletype, i);
451             qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
452                              , offset + 3, header.length - 3);
453         }
454         qemu_cfg_skip(header.length - sizeof(header));
455         offset += header.length;
456     }
457 }
458
459 struct QemuCfgFile {
460     u32  size;        /* file size */
461     u16  select;      /* write this to 0x510 to read it */
462     u16  reserved;
463     char name[56];
464 };
465
466 void qemu_cfg_init(void)
467 {
468     if (!runningOnQEMU())
469         return;
470
471     // Detect fw_cfg interface.
472     qemu_cfg_select(QEMU_CFG_SIGNATURE);
473     char *sig = "QEMU";
474     int i;
475     for (i = 0; i < 4; i++)
476         if (inb(PORT_QEMU_CFG_DATA) != sig[i])
477             return;
478
479     dprintf(1, "Found QEMU fw_cfg\n");
480
481     // Detect DMA interface.
482     u32 id;
483     qemu_cfg_read_entry(&id, QEMU_CFG_ID, sizeof(id));
484
485     if (id & QEMU_CFG_VERSION_DMA) {
486         dprintf(1, "QEMU fw_cfg DMA interface supported\n");
487         cfg_dma_enabled = 1;
488     }
489
490     // Populate romfiles for legacy fw_cfg entries
491     qemu_cfg_legacy();
492
493     // Load files found in the fw_cfg file directory
494     u32 count;
495     qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
496     count = be32_to_cpu(count);
497     u32 e;
498     for (e = 0; e < count; e++) {
499         struct QemuCfgFile qfile;
500         qemu_cfg_read(&qfile, sizeof(qfile));
501         qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
502                          , 0, be32_to_cpu(qfile.size));
503     }
504
505     qemu_cfg_e820();
506
507     if (romfile_find("etc/table-loader")) {
508         acpi_pm_base = 0x0600;
509         dprintf(1, "Moving pm_base to 0x%x\n", acpi_pm_base);
510     }
511 }