blob: 355f13cd36d7e2a9de7d4a4ca9ece6fdbff0d09e [file] [log] [blame]
Martin Rothb28f4662018-05-26 17:58:47 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Stefan Reinauere2b53e12004-06-28 11:59:45 +000014#include <console/console.h>
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020015#include <cpu/cpu.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +010016#include <cpu/x86/lapic_def.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000017#include <arch/io.h>
Elyes HAOUASed69de32019-12-19 17:36:53 +010018#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020019#include <device/pci_ops.h>
Uwe Hermann74d1a6e2010-10-12 17:34:08 +000020#include <arch/ioapic.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000021#include <stdint.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000022#include <device/device.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000023#include <stdlib.h>
24#include <string.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020025#include <smbios.h>
Patrick Rudolph69d5ef92018-11-11 12:43:48 +010026#include "memory.h"
Myles Watson0520d552009-05-11 22:44:14 +000027
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020028#include "fw_cfg.h"
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010029#include "fw_cfg_if.h"
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020030
Vladimir Serbinenko41877d82014-09-01 22:18:01 +020031#include "acpi.h"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020032
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020033static void qemu_reserve_ports(struct device *dev, unsigned int idx,
34 unsigned int base, unsigned int size,
35 const char *name)
36{
37 unsigned int end = base + size -1;
38 struct resource *res;
39
40 printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n",
41 base, end, name);
42 res = new_resource(dev, idx);
43 res->base = base;
44 res->size = size;
45 res->limit = 0xffff;
46 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
47 IORESOURCE_ASSIGNED;
48}
49
Kyösti Mälkkiccb95022018-05-22 00:16:23 +030050static void cpu_pci_domain_set_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000051{
Myles Watson894a3472010-06-09 22:41:35 +000052 assign_resources(dev->link_list);
Eric Biederman6e53f502004-10-27 08:53:57 +000053}
Stefan Reinauere2b53e12004-06-28 11:59:45 +000054
Myles Watson29cc9ed2009-07-02 18:56:24 +000055static void cpu_pci_domain_read_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000056{
Kyösti Mälkki98a91742018-05-21 21:29:16 +030057 u16 nbid = pci_read_config16(pcidev_on_root(0x0, 0), PCI_DEVICE_ID);
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020058 int i440fx = (nbid == 0x1237);
Gerd Hoffmannad690f22013-09-17 10:35:43 +020059 int q35 = (nbid == 0x29c0);
Myles Watson29cc9ed2009-07-02 18:56:24 +000060 struct resource *res;
Gerd Hoffmann9839a382013-06-17 12:26:17 +020061 unsigned long tomk = 0, high;
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020062 int idx = 10;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010063 FWCfgFile f;
Myles Watson29cc9ed2009-07-02 18:56:24 +000064
65 pci_domain_read_resources(dev);
66
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010067 if (!fw_cfg_check_file(&f, "etc/e820") && f.size > 0) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010068 /* supported by qemu 1.7+ */
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010069 FwCfgE820Entry *list = malloc(f.size);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010070 int i;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010071 fw_cfg_get(f.select, list, f.size);
72 for (i = 0; i < f.size / sizeof(*list); i++) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010073 switch (list[i].type) {
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +020074 case 1: /* RAM */
Himanshu Sahdev660ff202019-09-10 16:15:41 +053075 printk(BIOS_DEBUG, "QEMU: e820/ram: 0x%08llx + 0x%08llx\n",
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010076 list[i].address, list[i].length);
77 if (list[i].address == 0) {
78 tomk = list[i].length / 1024;
79 ram_resource(dev, idx++, 0, 640);
80 ram_resource(dev, idx++, 768, tomk - 768);
81 } else {
82 ram_resource(dev, idx++,
83 list[i].address / 1024,
84 list[i].length / 1024);
85 }
86 break;
87 case 2: /* reserved */
88 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
89 list[i].address, list[i].length);
90 res = new_resource(dev, idx++);
91 res->base = list[i].address;
92 res->size = list[i].length;
93 res->limit = 0xffffffff;
94 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
95 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
96 break;
97 default:
98 /* skip unknown */
99 break;
100 }
101 }
102 free(list);
103 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200104
Gerd Hoffmannbaa78202013-11-06 14:36:17 +0100105 if (!tomk) {
106 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
107 tomk = qemu_get_memory_size();
108 high = qemu_get_high_memory_size();
109 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM below 4G.\n", tomk / 1024);
110 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM above 4G.\n", high / 1024);
111
112 /* Report the memory regions. */
113 ram_resource(dev, idx++, 0, 640);
114 ram_resource(dev, idx++, 768, tomk - 768);
115 if (high)
116 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
117 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200118
Gerd Hoffmann05d3f492013-08-06 10:48:41 +0200119 /* Reserve I/O ports used by QEMU */
120 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
121 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
122 if (i440fx) {
123 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
124 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
125 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
126 }
127 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
128 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
129 "debugcon");
130 }
131
Duncan Laurieddd4f9a2020-03-17 18:46:28 -0700132 /* A segment is legacy VGA region */
133 mmio_resource(dev, idx++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
134
135 /* C segment to 1MB is reserved RAM (low tables) */
136 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, (1 * MiB - 0xc0000) / KiB);
137
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200138 if (q35 && ((tomk * 1024) < 0xb0000000)) {
139 /*
140 * Reserve the region between top-of-ram and the
141 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
142 * place pci bars there. The region isn't declared as
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +0200143 * pci io window in the ACPI tables (\_SB.PCI0._CRS).
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200144 */
145 res = new_resource(dev, idx++);
146 res->base = tomk * 1024;
147 res->size = 0xb0000000 - tomk * 1024;
148 res->limit = 0xffffffff;
149 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
150 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
151 }
152
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200153 if (i440fx) {
154 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200155 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200156 * to put it in. */
157 res = new_resource(dev, 2);
158 res->base = IO_APIC_ADDR;
159 res->size = 0x100000UL;
160 res->limit = 0xffffffffUL;
161 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
162 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
163 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000164
165 /* Reserve space for the LAPIC. There's one in every processor, but
166 * the space only needs to be reserved once, so we do it here. */
167 res = new_resource(dev, 3);
Patrick Georgic8feedd2012-02-16 18:43:25 +0100168 res->base = LOCAL_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000169 res->size = 0x10000UL;
170 res->limit = 0xffffffffUL;
171 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
172 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000173}
174
Julius Wernercd49cce2019-03-05 16:53:33 -0800175#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200176static int qemu_get_smbios_data16(int handle, unsigned long *current)
177{
178 struct smbios_type16 *t = (struct smbios_type16 *)*current;
179 int len = sizeof(struct smbios_type16);
180
181 memset(t, 0, sizeof(struct smbios_type16));
182 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
183 t->handle = handle;
184 t->length = len - 2;
Paul Menzelb4d07572017-03-12 18:18:06 +0100185 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
186 t->use = MEMORY_ARRAY_USE_SYSTEM;
187 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200188 t->maximum_capacity = qemu_get_memory_size();
189 *current += len;
190 return len;
191}
192
193static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
194{
195 struct smbios_type17 *t = (struct smbios_type17 *)*current;
196 int len;
197
198 memset(t, 0, sizeof(struct smbios_type17));
199 t->type = SMBIOS_MEMORY_DEVICE;
200 t->handle = handle;
201 t->phys_memory_array_handle = parent_handle;
202 t->length = sizeof(struct smbios_type17) - 2;
203 t->size = qemu_get_memory_size() / 1024;
204 t->data_width = 64;
205 t->total_width = 64;
206 t->form_factor = 9; /* DIMM */
207 t->device_locator = smbios_add_string(t->eos, "Virtual");
208 t->memory_type = 0x12; /* DDR */
209 t->type_detail = 0x80; /* Synchronous */
210 t->speed = 200;
211 t->clock_speed = 200;
212 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
213 len = t->length + smbios_string_table_len(t->eos);
214 *current += len;
215 return len;
216}
217
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300218static int qemu_get_smbios_data(struct device *dev, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200219{
220 int len;
Gerd Hoffmanndb9d1692014-08-27 11:25:13 +0200221
222 len = fw_cfg_smbios_tables(handle, current);
223 if (len != 0)
224 return len;
225
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200226 len = qemu_get_smbios_data16(*handle, current);
227 len += qemu_get_smbios_data17(*handle+1, *handle, current);
228 *handle += 2;
229 return len;
230}
231#endif
Eric Biederman6e53f502004-10-27 08:53:57 +0000232static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000233 .read_resources = cpu_pci_domain_read_resources,
234 .set_resources = cpu_pci_domain_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000235 .enable_resources = NULL,
236 .init = NULL,
Myles Watson032a9652009-05-11 22:24:53 +0000237 .scan_bus = pci_domain_scan_bus,
Julius Wernercd49cce2019-03-05 16:53:33 -0800238#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200239 .get_smbios_data = qemu_get_smbios_data,
240#endif
Myles Watson032a9652009-05-11 22:24:53 +0000241};
Eric Biederman6e53f502004-10-27 08:53:57 +0000242
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300243static void cpu_bus_init(struct device *dev)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200244{
245 initialize_cpus(dev->link_list);
246}
247
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300248static void cpu_bus_scan(struct device *bus)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200249{
250 int max_cpus = fw_cfg_max_cpus();
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300251 struct device *cpu;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200252 int i;
253
254 if (max_cpus < 0)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200255 return;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200256
257 /*
258 * TODO: This only handles the simple "qemu -smp $nr" case
259 * correctly. qemu also allows to specify the number of
260 * cores, threads & sockets.
261 */
262 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
263 for (i = 0; i < max_cpus; i++) {
264 cpu = add_cpu_device(bus->link_list, i, 1);
265 if (cpu)
266 set_cpu_topology(cpu, 1, 0, i, 0);
267 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200268}
269
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200270static struct device_operations cpu_bus_ops = {
Edward O'Callaghanc3f38cd2014-10-31 08:04:30 +1100271 .read_resources = DEVICE_NOOP,
272 .set_resources = DEVICE_NOOP,
273 .enable_resources = DEVICE_NOOP,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200274 .init = cpu_bus_init,
275 .scan_bus = cpu_bus_scan,
276};
277
Paul Menzel5f20b352013-02-24 14:27:03 +0100278static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000279{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000280 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800281 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000282 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000283 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200284 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
285 dev->ops = &cpu_bus_ops;
286 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000287}
288
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200289struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
290 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100291 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000292};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200293
294struct chip_operations mainboard_emulation_qemu_q35_ops = {
295 CHIP_NAME("QEMU Northbridge q35")
296 .enable_dev = northbridge_enable,
297};