blob: 5e369f2bedea48a7ccc65029c1fc2e42ccf19aba [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'Connor2d2fa312013-09-14 21:55:26 -040011#include "byteorder.h" // be32_to_cpu
12#include "config.h" // CONFIG_QEMU
Kevin O'Connor5d369d82013-09-02 20:48:46 -040013#include "hw/pci.h" // create_pirtable
Gerd Hoffmannebfece82013-09-03 11:41:01 +020014#include "hw/pci_regs.h" // PCI_DEVICE_ID
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040015#include "hw/rtc.h" // CMOS_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "ioport.h" // outw
Kevin O'Connor9dea5902013-09-14 20:23:54 -040017#include "malloc.h" // malloc_tmp
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "memmap.h" // add_e820
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040019#include "output.h" // dprintf
20#include "paravirt.h" // qemu_cfg_preinit
Kevin O'Connor41639f82013-09-14 19:37:36 -040021#include "romfile.h" // romfile_loadint
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040022#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040023#include "util.h" // pci_setup
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040024#include "x86.h" // cpuid
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040025#include "xen.h" // xen_biostable_setup
Kevin O'Connore7cc7642009-10-04 10:05:16 -040026
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -050027// Amount of continuous ram under 4Gig
28u32 RamSize;
29// Amount of continuous ram >4Gig
30u64 RamSizeOver4G;
Kevin O'Connor89a2f962013-02-18 23:36:03 -050031// Type of emulator platform.
32int PlatformRunningOn VARFSEG;
33
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050034/* 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
Gerd Hoffmannebfece82013-09-03 11:41:01 +020039static void kvm_detect(void)
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050040{
Kevin O'Connor7b5bc502013-02-09 13:07:23 -050041 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
Gerd Hoffmannebfece82013-09-03 11:41:01 +020056static 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
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050088void
Kevin O'Connora2a86e22013-02-13 19:35:12 -050089qemu_preinit(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -050090{
Gerd Hoffmannebfece82013-09-03 11:41:01 +020091 qemu_detect();
92
Kevin O'Connor02313b22013-02-07 22:42:25 -050093 if (!CONFIG_QEMU)
94 return;
95
Kevin O'Connora2a86e22013-02-13 19:35:12 -050096 if (runningOnXen()) {
97 xen_ramsize_preinit();
98 return;
99 }
100
Gerd Hoffmannebfece82013-09-03 11:41:01 +0200101 if (!runningOnQEMU()) {
102 dprintf(1, "Warning: No QEMU Northbridge found (isapc?)\n");
103 PlatformRunningOn |= PF_QEMU;
104 kvm_detect();
105 }
Kevin O'Connor02313b22013-02-07 22:42:25 -0500106
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500107 // On emulators, get memory size from nvram.
Kevin O'Connor8b7861c2013-09-15 02:29:06 -0400108 u32 rs = ((rtc_read(CMOS_MEM_EXTMEM2_LOW) << 16)
109 | (rtc_read(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500110 if (rs)
111 rs += 16 * 1024 * 1024;
112 else
Kevin O'Connor8b7861c2013-09-15 02:29:06 -0400113 rs = (((rtc_read(CMOS_MEM_EXTMEM_LOW) << 10)
114 | (rtc_read(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500115 + 1 * 1024 * 1024);
116 RamSize = rs;
117 add_e820(0, rs, E820_RAM);
118
119 // Check for memory over 4Gig
Kevin O'Connor8b7861c2013-09-15 02:29:06 -0400120 u64 high = ((rtc_read(CMOS_MEM_HIGHMEM_LOW) << 16)
121 | ((u32)rtc_read(CMOS_MEM_HIGHMEM_MID) << 24)
122 | ((u64)rtc_read(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500123 RamSizeOver4G = high;
124 add_e820(0x100000000ull, high, E820_RAM);
125
126 /* reserve 256KB BIOS area at the end of 4 GB */
127 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf85e4bc2013-02-19 01:33:45 -0500128
129 dprintf(1, "Ram Size=0x%08x (0x%016llx high)\n", RamSize, RamSizeOver4G);
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500130}
131
132void
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500133qemu_platform_setup(void)
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500134{
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500135 if (!CONFIG_QEMU)
136 return;
137
138 if (runningOnXen()) {
139 pci_probe_devices();
140 xen_hypercall_setup();
141 xen_biostable_setup();
142 return;
143 }
144
145 // Initialize pci
146 pci_setup();
Kevin O'Connorcdbac7f2013-03-08 19:33:39 -0500147 smm_device_setup();
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500148 smm_setup();
149
150 // Initialize mtrr and smp
151 mtrr_setup();
152 smp_setup();
153
154 // Create bios tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500155 pirtable_setup();
156 mptable_setup();
157 smbios_setup();
158 acpi_setup();
159}
160
Kevin O'Connor4edda082013-02-09 13:21:08 -0500161
162/****************************************************************
163 * QEMU firmware config (fw_cfg) interface
164 ****************************************************************/
165
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500166// List of QEMU fw_cfg entries. DO NOT ADD MORE. (All new content
167// should be passed via the fw_cfg "file" interface.)
Kevin O'Connor4edda082013-02-09 13:21:08 -0500168#define QEMU_CFG_SIGNATURE 0x00
169#define QEMU_CFG_ID 0x01
170#define QEMU_CFG_UUID 0x02
171#define QEMU_CFG_NUMA 0x0d
172#define QEMU_CFG_BOOT_MENU 0x0e
173#define QEMU_CFG_MAX_CPUS 0x0f
174#define QEMU_CFG_FILE_DIR 0x19
175#define QEMU_CFG_ARCH_LOCAL 0x8000
176#define QEMU_CFG_ACPI_TABLES (QEMU_CFG_ARCH_LOCAL + 0)
177#define QEMU_CFG_SMBIOS_ENTRIES (QEMU_CFG_ARCH_LOCAL + 1)
178#define QEMU_CFG_IRQ0_OVERRIDE (QEMU_CFG_ARCH_LOCAL + 2)
179#define QEMU_CFG_E820_TABLE (QEMU_CFG_ARCH_LOCAL + 3)
180
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400181static void
182qemu_cfg_select(u16 f)
183{
184 outw(f, PORT_QEMU_CFG_CTL);
185}
186
187static void
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500188qemu_cfg_read(void *buf, int len)
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400189{
Kevin O'Connor6039fc52010-08-25 21:43:19 -0400190 insb(PORT_QEMU_CFG_DATA, buf, len);
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400191}
192
193static void
Kevin O'Connor4e4b4102009-10-08 21:21:59 -0400194qemu_cfg_skip(int len)
195{
196 while (len--)
197 inb(PORT_QEMU_CFG_DATA);
198}
199
200static void
Kevin O'Connore7cc7642009-10-04 10:05:16 -0400201qemu_cfg_read_entry(void *buf, int e, int len)
202{
203 qemu_cfg_select(e);
204 qemu_cfg_read(buf, len);
205}
206
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400207struct qemu_romfile_s {
208 struct romfile_s file;
209 int select, skip;
210};
211
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500212static int
213qemu_cfg_read_file(struct romfile_s *file, void *dst, u32 maxlen)
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100214{
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400215 if (file->size > maxlen)
216 return -1;
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400217 struct qemu_romfile_s *qfile;
218 qfile = container_of(file, struct qemu_romfile_s, file);
219 qemu_cfg_select(qfile->select);
220 qemu_cfg_skip(qfile->skip);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500221 qemu_cfg_read(dst, file->size);
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400222 return file->size;
223}
224
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500225static void
226qemu_romfile_add(char *name, int select, int skip, int size)
227{
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400228 struct qemu_romfile_s *qfile = malloc_tmp(sizeof(*qfile));
229 if (!qfile) {
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500230 warn_noalloc();
231 return;
232 }
Kevin O'Connor4158c8c2013-03-30 09:12:11 -0400233 memset(qfile, 0, sizeof(*qfile));
234 strtcpy(qfile->file.name, name, sizeof(qfile->file.name));
235 qfile->file.size = size;
236 qfile->select = select;
237 qfile->skip = skip;
238 qfile->file.copy = qemu_cfg_read_file;
239 romfile_add(&qfile->file);
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500240}
241
Kevin O'Connorfe090302013-02-09 20:00:06 -0500242struct e820_reservation {
243 u64 address;
244 u64 length;
245 u32 type;
246};
247
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500248#define SMBIOS_FIELD_ENTRY 0
249#define SMBIOS_TABLE_ENTRY 1
250
251struct qemu_smbios_header {
252 u16 length;
253 u8 headertype;
254 u8 tabletype;
255 u16 fieldoffset;
256} PACKED;
257
Kevin O'Connor188d9942013-02-09 15:24:08 -0500258// Populate romfile entries for legacy fw_cfg ports (that predate the
259// "file" interface).
260static void
261qemu_cfg_legacy(void)
262{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400263 if (!CONFIG_QEMU)
264 return;
265
Kevin O'Connor56c50892013-02-09 19:25:51 -0500266 // Misc config items.
267 qemu_romfile_add("etc/show-boot-menu", QEMU_CFG_BOOT_MENU, 0, 2);
268 qemu_romfile_add("etc/irq0-override", QEMU_CFG_IRQ0_OVERRIDE, 0, 1);
269 qemu_romfile_add("etc/max-cpus", QEMU_CFG_MAX_CPUS, 0, 2);
270
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500271 // NUMA data
272 u64 numacount;
273 qemu_cfg_read_entry(&numacount, QEMU_CFG_NUMA, sizeof(numacount));
Kevin O'Connorfb76cff2013-03-23 11:38:45 -0400274 int max_cpu = romfile_loadint("etc/max-cpus", 0);
275 qemu_romfile_add("etc/numa-cpu-map", QEMU_CFG_NUMA, sizeof(numacount)
276 , max_cpu*sizeof(u64));
277 qemu_romfile_add("etc/numa-nodes", QEMU_CFG_NUMA
278 , sizeof(numacount) + max_cpu*sizeof(u64)
Kevin O'Connorf9e4e372013-02-09 19:45:45 -0500279 , numacount*sizeof(u64));
280
Kevin O'Connorfe090302013-02-09 20:00:06 -0500281 // e820 data
282 u32 count32;
283 qemu_cfg_read_entry(&count32, QEMU_CFG_E820_TABLE, sizeof(count32));
284 if (count32) {
285 struct e820_reservation entry;
286 int i;
287 for (i = 0; i < count32; i++) {
288 qemu_cfg_read(&entry, sizeof(entry));
289 add_e820(entry.address, entry.length, entry.type);
290 }
291 } else if (runningOnKVM()) {
292 // Backwards compatibility - provide hard coded range.
293 // 4 pages before the bios, 3 pages for vmx tss pages, the
294 // other page for EPT real mode pagetable
295 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
296 }
297
Kevin O'Connor188d9942013-02-09 15:24:08 -0500298 // ACPI tables
299 char name[128];
300 u16 cnt;
301 qemu_cfg_read_entry(&cnt, QEMU_CFG_ACPI_TABLES, sizeof(cnt));
302 int i, offset = sizeof(cnt);
303 for (i = 0; i < cnt; i++) {
304 u16 len;
305 qemu_cfg_read(&len, sizeof(len));
306 offset += sizeof(len);
307 snprintf(name, sizeof(name), "acpi/table%d", i);
308 qemu_romfile_add(name, QEMU_CFG_ACPI_TABLES, offset, len);
309 qemu_cfg_skip(len);
310 offset += len;
311 }
Kevin O'Connorde9e7052013-02-09 19:09:20 -0500312
313 // SMBIOS info
314 qemu_cfg_read_entry(&cnt, QEMU_CFG_SMBIOS_ENTRIES, sizeof(cnt));
315 offset = sizeof(cnt);
316 for (i = 0; i < cnt; i++) {
317 struct qemu_smbios_header header;
318 qemu_cfg_read(&header, sizeof(header));
319 if (header.headertype == SMBIOS_FIELD_ENTRY) {
320 snprintf(name, sizeof(name), "smbios/field%d-%d"
321 , header.tabletype, header.fieldoffset);
322 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
323 , offset + sizeof(header)
324 , header.length - sizeof(header));
325 } else {
326 snprintf(name, sizeof(name), "smbios/table%d-%d"
327 , header.tabletype, i);
328 qemu_romfile_add(name, QEMU_CFG_SMBIOS_ENTRIES
329 , offset + 3, header.length - 3);
330 }
331 qemu_cfg_skip(header.length - sizeof(header));
332 offset += header.length;
333 }
Kevin O'Connor188d9942013-02-09 15:24:08 -0500334}
335
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400336struct QemuCfgFile {
337 u32 size; /* file size */
338 u16 select; /* write this to 0x510 to read it */
339 u16 reserved;
340 char name[56];
341};
342
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500343void qemu_cfg_init(void)
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400344{
Kevin O'Connor7507ce22013-06-13 20:04:31 -0400345 if (!runningOnQEMU())
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400346 return;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100347
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500348 // Detect fw_cfg interface.
349 qemu_cfg_select(QEMU_CFG_SIGNATURE);
350 char *sig = "QEMU";
351 int i;
352 for (i = 0; i < 4; i++)
353 if (inb(PORT_QEMU_CFG_DATA) != sig[i])
354 return;
355 dprintf(1, "Found QEMU fw_cfg\n");
356
Kevin O'Connor188d9942013-02-09 15:24:08 -0500357 // Populate romfiles for legacy fw_cfg entries
358 qemu_cfg_legacy();
359
360 // Load files found in the fw_cfg file directory
Kevin O'Connore2304262010-06-13 16:05:17 -0400361 u32 count;
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100362 qemu_cfg_read_entry(&count, QEMU_CFG_FILE_DIR, sizeof(count));
Kevin O'Connorb3064592012-08-14 21:20:10 -0400363 count = be32_to_cpu(count);
Kevin O'Connore2304262010-06-13 16:05:17 -0400364 u32 e;
365 for (e = 0; e < count; e++) {
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400366 struct QemuCfgFile qfile;
Kevin O'Connorc40e3fa2013-02-09 14:55:30 -0500367 qemu_cfg_read(&qfile, sizeof(qfile));
368 qemu_romfile_add(qfile.name, be16_to_cpu(qfile.select)
369 , 0, be32_to_cpu(qfile.size));
Gerd Hoffmannc4c9fae2009-12-18 12:16:04 +0100370 }
Kevin O'Connor8b565782011-07-05 20:32:44 -0400371}