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