blob: d1a5d3e2f20bd739d0ec2acad42223c01a994f68 [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
18#include "cmos.h" // CMOS_*
19#include "acpi.h" // acpi_setup
20#include "mptable.h" // mptable_setup
21#include "pci.h" // create_pirtable
Kevin O'Connora2a86e22013-02-13 19:35:12 -050022#include "xen.h" // xen_biostable_setup
Kevin O'Connore7cc7642009-10-04 10:05:16 -040023
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050024// Amount of continuous ram under 4Gig
25u32 RamSize;
26// Amount of continuous ram >4Gig
27u64 RamSizeOver4G;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050028// Type of emulator platform.
29int PlatformRunningOn VARFSEG;
30
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050031/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
32 * should be used to determine that a VM is running under KVM.
33 */
34#define KVM_CPUID_SIGNATURE 0x40000000
35
36static void kvm_preinit(void)
37{
38 if (!CONFIG_QEMU)
39 return;
40 unsigned int eax, ebx, ecx, edx;
41 char signature[13];
42
43 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
44 memcpy(signature + 0, &ebx, 4);
45 memcpy(signature + 4, &ecx, 4);
46 memcpy(signature + 8, &edx, 4);
47 signature[12] = 0;
48
49 if (strcmp(signature, "KVMKVMKVM") == 0) {
50 dprintf(1, "Running on KVM\n");
51 PlatformRunningOn |= PF_KVM;
52 }
53}
54
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050055void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050056qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050057{
Kevin O'Connor02313b22013-02-07 22:42:25 -050058 if (!CONFIG_QEMU)
59 return;
60
Kevin O'Connora2a86e22013-02-13 19:35:12 -050061 if (runningOnXen()) {
62 xen_ramsize_preinit();
63 return;
64 }
65
Kevin O'Connor02313b22013-02-07 22:42:25 -050066 PlatformRunningOn = PF_QEMU;
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050067 kvm_preinit();
Kevin O'Connor02313b22013-02-07 22:42:25 -050068
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050069 // On emulators, get memory size from nvram.
70 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
71 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
72 if (rs)
73 rs += 16 * 1024 * 1024;
74 else
75 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
76 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
77 + 1 * 1024 * 1024);
78 RamSize = rs;
79 add_e820(0, rs, E820_RAM);
80
81 // Check for memory over 4Gig
82 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
83 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
84 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
85 RamSizeOver4G = high;
86 add_e820(0x100000000ull, high, E820_RAM);
87
88 /* reserve 256KB BIOS area at the end of 4 GB */
89 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050090
91 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050092}
93
94void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050095qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050096{
Kevin O'Connora2a86e22013-02-13 19:35:12 -050097 if (!CONFIG_QEMU)
98 return;
99
100 if (runningOnXen()) {
101 pci_probe_devices();
102 xen_hypercall_setup();
103 xen_biostable_setup();
104 return;
105 }
106
107 // Initialize pci
108 pci_setup();
Kevin O'Connorcdbac7f2013-03-08 19:33:39 -0500109 smm_device_setup();
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500110 smm_setup();
111
112 // Initialize mtrr and smp
113 mtrr_setup();
114 smp_setup();
115
116 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500117 pirtable_setup();
118 mptable_setup();
119 smbios_setup();
120 acpi_setup();
121}
122
Kevin O'Connor4edda082013-02-09 13:21:08 -0500123
124/****************************************************************
125 * QEMU firmware config (fw_cfg) interface
126 ****************************************************************/
127
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500128// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
129// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500130#define QEMU_CFG_SIGNATURE 0x00
131#define QEMU_CFG_ID 0x01
132#define QEMU_CFG_UUID 0x02
133#define QEMU_CFG_NUMA 0x0d
134#define QEMU_CFG_BOOT_MENU 0x0e
135#define QEMU_CFG_MAX_CPUS 0x0f
136#define QEMU_CFG_FILE_DIR 0x19
137#define QEMU_CFG_ARCH_LOCAL 0x8000
138#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
139#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
140#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
141#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
142
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400143static void
144qemu_cfg_select(u16 f)
145{
146 outw(f, PORT_QEMU_CFG_CTL);
147}
148
149static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500150qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400151{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400152 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400153}
154
155static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400156qemu_cfg_skip(int len)
157{
158 while (len--)
159 inb(PORT_QEMU_CFG_DATA);
160}
161
162static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400163qemu_cfg_read_entry(void *buf, int e, int len)
164{
165 qemu_cfg_select(e);
166 qemu_cfg_read(buf, len);
167}
168
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400169struct qemu_romfile_s {
170 struct romfile_s file;
171 int select, skip;
172};
173
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500174static int
175qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100176{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400177 if (file->size > maxlen)
178 return -1;
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400179 struct qemu_romfile_s *qfile;
180 qfile = container_of(file, struct qemu_romfile_s, file);
181 qemu_cfg_select(qfile->select);
182 qemu_cfg_skip(qfile->skip);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500183 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400184 return file->size;
185}
186
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500187static void
188qemu_romfile_add(char *name, int select, int skip, int size)
189{
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400190 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
191 if (!qfile) {
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500192 warn_noalloc();
193 return;
194 }
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400195 memset(qfile, 0, sizeof(*qfile));
196 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
197 qfile->file.size = size;
198 qfile->select = select;
199 qfile->skip = skip;
200 qfile->file.copy = qemu_cfg_read_file;
201 romfile_add(&qfile->file);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500202}
203
Kevin O'Connorfe090302013-02-09 20:00:06 -0500204struct e820_reservation {
205 u64 address;
206 u64 length;
207 u32 type;
208};
209
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500210#define SMBIOS_FIELD_ENTRY 0
211#define SMBIOS_TABLE_ENTRY 1
212
213struct qemu_smbios_header {
214 u16 length;
215 u8 headertype;
216 u8 tabletype;
217 u16 fieldoffset;
218} PACKED;
219
Kevin O'Connor188d9942013-02-09 15:24:08 -0500220// Populate romfile entries for legacy fw_cfg ports (that predate the
221// "file" interface).
222static void
223qemu_cfg_legacy(void)
224{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400225 if (!CONFIG_QEMU)
226 return;
227
Kevin O'Connor56c50892013-02-09 19:25:51 -0500228 // Misc config items.
229 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
230 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
231 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
232
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500233 // NUMA data
234 u64 numacount;
235 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
Kevin O'Connorfb76cff2013-03-23 11:38:45 -0400236 int max_cpu = romfile_loadint("etc/max-cpus", 0);
237 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
238 , max_cpu*sizeof(u64));
239 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
240 , sizeof(numacount) + max_cpu*sizeof(u64)
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500241 , numacount*sizeof(u64));
242
Kevin O'Connorfe090302013-02-09 20:00:06 -0500243 // e820 data
244 u32 count32;
245 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
246 if (count32) {
247 struct e820_reservation entry;
248 int i;
249 for (i = 0; i < count32; i++) {
250 qemu_cfg_read(&entry, sizeof(entry));
251 add_e820(entry.address, entry.length, entry.type);
252 }
253 } else if (runningOnKVM()) {
254 // Backwards compatibility - provide hard coded range.
255 // 4 pages before the bios, 3 pages for vmx tss pages, the
256 // other page for EPT real mode pagetable
257 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
258 }
259
Kevin O'Connor188d9942013-02-09 15:24:08 -0500260 // ACPI tables
261 char name[128];
262 u16 cnt;
263 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
264 int i, offset = sizeof(cnt);
265 for (i = 0; i < cnt; i++) {
266 u16 len;
267 qemu_cfg_read(&len, sizeof(len));
268 offset += sizeof(len);
269 snprintf(name, sizeof(name), "acpi/table%d", i);
270 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
271 qemu_cfg_skip(len);
272 offset += len;
273 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500274
275 // SMBIOS info
276 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
277 offset = sizeof(cnt);
278 for (i = 0; i < cnt; i++) {
279 struct qemu_smbios_header header;
280 qemu_cfg_read(&header, sizeof(header));
281 if (header.headertype == SMBIOS_FIELD_ENTRY) {
282 snprintf(name, sizeof(name), "smbios/field%d-%d"
283 , header.tabletype, header.fieldoffset);
284 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
285 , offset + sizeof(header)
286 , header.length - sizeof(header));
287 } else {
288 snprintf(name, sizeof(name), "smbios/table%d-%d"
289 , header.tabletype, i);
290 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
291 , offset + 3, header.length - 3);
292 }
293 qemu_cfg_skip(header.length - sizeof(header));
294 offset += header.length;
295 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500296}
297
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400298struct QemuCfgFile {
299 u32 size; /* file size */
300 u16 select; /* write this to 0x510 to read it */
301 u16 reserved;
302 char name[56];
303};
304
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500305void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400306{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400307 if (!runningOnQEMU())
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400308 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100309
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500310 // Detect fw_cfg interface.
311 qemu_cfg_select(QEMU_CFG_SIGNATURE);
312 char *sig = "QEMU";
313 int i;
314 for (i = 0; i < 4; i++)
315 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
316 return;
317 dprintf(1, "Found QEMU fw_cfg\n");
318
Kevin O'Connor188d9942013-02-09 15:24:08 -0500319 // Populate romfiles for legacy fw_cfg entries
320 qemu_cfg_legacy();
321
322 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400323 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100324 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400325 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400326 u32 e;
327 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400328 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500329 qemu_cfg_read(&qfile, sizeof(qfile));
330 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
331 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100332 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400333}