blob: a68e3a19d5611921210692e8dd01cb1dda619204 [file] [log] [blame]
Kevin O'Connore7cc7642009-10-04 10:05:16 -04001// Paravirtualization support.
2//
Kevin O'Connorb840ba92013-02-09 20:09:22 -05003// Copyright (C) 2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connore7cc7642009-10-04 10:05:16 -04004// 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
Kevin O'Connor897fb112013-02-07 23:32:48 -050011#include "config.h" // CONFIG_QEMU
Kevin O'Connorb3064592012-08-14 21:20:10 -040012#include "util.h" // dprintf
13#include "byteorder.h" // be32_to_cpu
Kevin O'Connor01a85202009-10-18 09:49:59 -040014#include "ioport.h" // outw
Kevin O'Connord83c87b2013-01-21 01:14:12 -050015#include "paravirt.h" // qemu_cfg_preinit
Kevin O'Connorde9e7052013-02-09 19:09:20 -050016#include "smbios.h" // smbios_setup
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050017#include "memmap.h" // add_e820
Kevin O'Connor5d369d82013-09-02 20:48:46 -040018#include "hw/cmos.h" // CMOS_*
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050019#include "acpi.h" // acpi_setup
20#include "mptable.h" // mptable_setup
Kevin O'Connor5d369d82013-09-02 20:48:46 -040021#include "hw/pci.h" // create_pirtable
Kevin O'Connora2a86e22013-02-13 19:35:12 -050022#include "xen.h" // xen_biostable_setup
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040023#include "x86.h" // cpuid
Kevin O'Connore7cc7642009-10-04 10:05:16 -040024
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050025// Amount of continuous ram under 4Gig
26u32 RamSize;
27// Amount of continuous ram >4Gig
28u64 RamSizeOver4G;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050029// Type of emulator platform.
30int PlatformRunningOn VARFSEG;
31
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050032/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
33 * should be used to determine that a VM is running under KVM.
34 */
35#define KVM_CPUID_SIGNATURE 0x40000000
36
37static void kvm_preinit(void)
38{
39 if (!CONFIG_QEMU)
40 return;
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
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050056void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050057qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050058{
Kevin O'Connor02313b22013-02-07 22:42:25 -050059 if (!CONFIG_QEMU)
60 return;
61
Kevin O'Connora2a86e22013-02-13 19:35:12 -050062 if (runningOnXen()) {
63 xen_ramsize_preinit();
64 return;
65 }
66
Kevin O'Connor02313b22013-02-07 22:42:25 -050067 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050068 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050069
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050070 // On emulators, get memory size from nvram.
71 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
72 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
73 if (rs)
74 rs += 16 * 1024 * 1024;
75 else
76 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
77 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
78 + 1 * 1024 * 1024);
79 RamSize = rs;
80 add_e820(0, rs, E820_RAM);
81
82 // Check for memory over 4Gig
83 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
84 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
85 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
86 RamSizeOver4G = high;
87 add_e820(0x100000000ull, high, E820_RAM);
88
89 /* reserve 256KB BIOS area at the end of 4 GB */
90 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050091
92 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050093}
94
95void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050096qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050097{
Kevin O'Connora2a86e22013-02-13 19:35:12 -050098 if (!CONFIG_QEMU)
99 return;
100
101 if (runningOnXen()) {
102 pci_probe_devices();
103 xen_hypercall_setup();
104 xen_biostable_setup();
105 return;
106 }
107
108 // Initialize pci
109 pci_setup();
Kevin O'Connorcdbac7f2013-03-08 19:33:39 -0500110 smm_device_setup();
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500111 smm_setup();
112
113 // Initialize mtrr and smp
114 mtrr_setup();
115 smp_setup();
116
117 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500118 pirtable_setup();
119 mptable_setup();
120 smbios_setup();
121 acpi_setup();
122}
123
Kevin O'Connor4edda082013-02-09 13:21:08 -0500124
125/****************************************************************
126 * QEMU firmware config (fw_cfg) interface
127 ****************************************************************/
128
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500129// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
130// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500131#define QEMU_CFG_SIGNATURE 0x00
132#define QEMU_CFG_ID 0x01
133#define QEMU_CFG_UUID 0x02
134#define QEMU_CFG_NUMA 0x0d
135#define QEMU_CFG_BOOT_MENU 0x0e
136#define QEMU_CFG_MAX_CPUS 0x0f
137#define QEMU_CFG_FILE_DIR 0x19
138#define QEMU_CFG_ARCH_LOCAL 0x8000
139#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
140#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
141#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
142#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
143
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400144static void
145qemu_cfg_select(u16 f)
146{
147 outw(f, PORT_QEMU_CFG_CTL);
148}
149
150static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500151qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400152{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400153 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400154}
155
156static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400157qemu_cfg_skip(int len)
158{
159 while (len--)
160 inb(PORT_QEMU_CFG_DATA);
161}
162
163static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400164qemu_cfg_read_entry(void *buf, int e, int len)
165{
166 qemu_cfg_select(e);
167 qemu_cfg_read(buf, len);
168}
169
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400170struct qemu_romfile_s {
171 struct romfile_s file;
172 int select, skip;
173};
174
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500175static int
176qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100177{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400178 if (file->size > maxlen)
179 return -1;
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400180 struct qemu_romfile_s *qfile;
181 qfile = container_of(file, struct qemu_romfile_s, file);
182 qemu_cfg_select(qfile->select);
183 qemu_cfg_skip(qfile->skip);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500184 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400185 return file->size;
186}
187
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500188static void
189qemu_romfile_add(char *name, int select, int skip, int size)
190{
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400191 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
192 if (!qfile) {
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500193 warn_noalloc();
194 return;
195 }
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400196 memset(qfile, 0, sizeof(*qfile));
197 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
198 qfile->file.size = size;
199 qfile->select = select;
200 qfile->skip = skip;
201 qfile->file.copy = qemu_cfg_read_file;
202 romfile_add(&qfile->file);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500203}
204
Kevin O'Connorfe090302013-02-09 20:00:06 -0500205struct e820_reservation {
206 u64 address;
207 u64 length;
208 u32 type;
209};
210
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500211#define SMBIOS_FIELD_ENTRY 0
212#define SMBIOS_TABLE_ENTRY 1
213
214struct qemu_smbios_header {
215 u16 length;
216 u8 headertype;
217 u8 tabletype;
218 u16 fieldoffset;
219} PACKED;
220
Kevin O'Connor188d9942013-02-09 15:24:08 -0500221// Populate romfile entries for legacy fw_cfg ports (that predate the
222// "file" interface).
223static void
224qemu_cfg_legacy(void)
225{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400226 if (!CONFIG_QEMU)
227 return;
228
Kevin O'Connor56c50892013-02-09 19:25:51 -0500229 // Misc config items.
230 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
231 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
232 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
233
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500234 // NUMA data
235 u64 numacount;
236 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
Kevin O'Connorfb76cff2013-03-23 11:38:45 -0400237 int max_cpu = romfile_loadint("etc/max-cpus", 0);
238 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
239 , max_cpu*sizeof(u64));
240 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
241 , sizeof(numacount) + max_cpu*sizeof(u64)
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500242 , numacount*sizeof(u64));
243
Kevin O'Connorfe090302013-02-09 20:00:06 -0500244 // e820 data
245 u32 count32;
246 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
247 if (count32) {
248 struct e820_reservation entry;
249 int i;
250 for (i = 0; i < count32; i++) {
251 qemu_cfg_read(&entry, sizeof(entry));
252 add_e820(entry.address, entry.length, entry.type);
253 }
254 } else if (runningOnKVM()) {
255 // Backwards compatibility - provide hard coded range.
256 // 4 pages before the bios, 3 pages for vmx tss pages, the
257 // other page for EPT real mode pagetable
258 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
259 }
260
Kevin O'Connor188d9942013-02-09 15:24:08 -0500261 // ACPI tables
262 char name[128];
263 u16 cnt;
264 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
265 int i, offset = sizeof(cnt);
266 for (i = 0; i < cnt; i++) {
267 u16 len;
268 qemu_cfg_read(&len, sizeof(len));
269 offset += sizeof(len);
270 snprintf(name, sizeof(name), "acpi/table%d", i);
271 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
272 qemu_cfg_skip(len);
273 offset += len;
274 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500275
276 // SMBIOS info
277 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
278 offset = sizeof(cnt);
279 for (i = 0; i < cnt; i++) {
280 struct qemu_smbios_header header;
281 qemu_cfg_read(&header, sizeof(header));
282 if (header.headertype == SMBIOS_FIELD_ENTRY) {
283 snprintf(name, sizeof(name), "smbios/field%d-%d"
284 , header.tabletype, header.fieldoffset);
285 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
286 , offset + sizeof(header)
287 , header.length - sizeof(header));
288 } else {
289 snprintf(name, sizeof(name), "smbios/table%d-%d"
290 , header.tabletype, i);
291 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
292 , offset + 3, header.length - 3);
293 }
294 qemu_cfg_skip(header.length - sizeof(header));
295 offset += header.length;
296 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500297}
298
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400299struct QemuCfgFile {
300 u32 size; /* file size */
301 u16 select; /* write this to 0x510 to read it */
302 u16 reserved;
303 char name[56];
304};
305
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500306void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400307{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400308 if (!runningOnQEMU())
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400309 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100310
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500311 // Detect fw_cfg interface.
312 qemu_cfg_select(QEMU_CFG_SIGNATURE);
313 char *sig = "QEMU";
314 int i;
315 for (i = 0; i < 4; i++)
316 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
317 return;
318 dprintf(1, "Found QEMU fw_cfg\n");
319
Kevin O'Connor188d9942013-02-09 15:24:08 -0500320 // Populate romfiles for legacy fw_cfg entries
321 qemu_cfg_legacy();
322
323 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400324 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100325 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400326 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400327 u32 e;
328 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400329 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500330 qemu_cfg_read(&qfile, sizeof(qfile));
331 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
332 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100333 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400334}