blob: 4a26c6e9b295eae9b18a64444b82fd041c1602b5 [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'Connorc40e3fa2013-02-09 14:55:30 -0500169static int
170qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100171{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400172 if (file->size > maxlen)
173 return -1;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500174 qemu_cfg_select(file->id);
175 qemu_cfg_skip(file->rawsize);
176 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400177 return file->size;
178}
179
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500180static void
181qemu_romfile_add(char *name, int select, int skip, int size)
182{
183 struct romfile_s *file = malloc_tmp(sizeof(*file));
184 if (!file) {
185 warn_noalloc();
186 return;
187 }
188 memset(file, 0, sizeof(*file));
189 strtcpy(file->name, name, sizeof(file->name));
190 file->id = select;
191 file->rawsize = skip; // Use rawsize to indicate skip length.
192 file->size = size;
193 file->copy = qemu_cfg_read_file;
194 romfile_add(file);
195}
196
Kevin O'Connorfe090302013-02-09 20:00:06 -0500197struct e820_reservation {
198 u64 address;
199 u64 length;
200 u32 type;
201};
202
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500203#define SMBIOS_FIELD_ENTRY 0
204#define SMBIOS_TABLE_ENTRY 1
205
206struct qemu_smbios_header {
207 u16 length;
208 u8 headertype;
209 u8 tabletype;
210 u16 fieldoffset;
211} PACKED;
212
Kevin O'Connor188d9942013-02-09 15:24:08 -0500213// Populate romfile entries for legacy fw_cfg ports (that predate the
214// "file" interface).
215static void
216qemu_cfg_legacy(void)
217{
Kevin O'Connor56c50892013-02-09 19:25:51 -0500218 // Misc config items.
219 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
220 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
221 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
222
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500223 // NUMA data
224 u64 numacount;
225 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
226 numacount += romfile_loadint("etc/max-cpus", 0);
227 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA, sizeof(numacount)
228 , numacount*sizeof(u64));
229
Kevin O'Connorfe090302013-02-09 20:00:06 -0500230 // e820 data
231 u32 count32;
232 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
233 if (count32) {
234 struct e820_reservation entry;
235 int i;
236 for (i = 0; i < count32; i++) {
237 qemu_cfg_read(&entry, sizeof(entry));
238 add_e820(entry.address, entry.length, entry.type);
239 }
240 } else if (runningOnKVM()) {
241 // Backwards compatibility - provide hard coded range.
242 // 4 pages before the bios, 3 pages for vmx tss pages, the
243 // other page for EPT real mode pagetable
244 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
245 }
246
Kevin O'Connor188d9942013-02-09 15:24:08 -0500247 // ACPI tables
248 char name[128];
249 u16 cnt;
250 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
251 int i, offset = sizeof(cnt);
252 for (i = 0; i < cnt; i++) {
253 u16 len;
254 qemu_cfg_read(&len, sizeof(len));
255 offset += sizeof(len);
256 snprintf(name, sizeof(name), "acpi/table%d", i);
257 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
258 qemu_cfg_skip(len);
259 offset += len;
260 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500261
262 // SMBIOS info
263 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
264 offset = sizeof(cnt);
265 for (i = 0; i < cnt; i++) {
266 struct qemu_smbios_header header;
267 qemu_cfg_read(&header, sizeof(header));
268 if (header.headertype == SMBIOS_FIELD_ENTRY) {
269 snprintf(name, sizeof(name), "smbios/field%d-%d"
270 , header.tabletype, header.fieldoffset);
271 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
272 , offset + sizeof(header)
273 , header.length - sizeof(header));
274 } else {
275 snprintf(name, sizeof(name), "smbios/table%d-%d"
276 , header.tabletype, i);
277 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
278 , offset + 3, header.length - 3);
279 }
280 qemu_cfg_skip(header.length - sizeof(header));
281 offset += header.length;
282 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500283}
284
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400285struct QemuCfgFile {
286 u32 size; /* file size */
287 u16 select; /* write this to 0x510 to read it */
288 u16 reserved;
289 char name[56];
290};
291
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500292void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400293{
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500294 if (!CONFIG_QEMU)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400295 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100296
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500297 // Detect fw_cfg interface.
298 qemu_cfg_select(QEMU_CFG_SIGNATURE);
299 char *sig = "QEMU";
300 int i;
301 for (i = 0; i < 4; i++)
302 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
303 return;
304 dprintf(1, "Found QEMU fw_cfg\n");
305
Kevin O'Connor188d9942013-02-09 15:24:08 -0500306 // Populate romfiles for legacy fw_cfg entries
307 qemu_cfg_legacy();
308
309 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400310 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100311 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400312 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400313 u32 e;
314 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400315 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500316 qemu_cfg_read(&qfile, sizeof(qfile));
317 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
318 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100319 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400320}