blob: d19d6c820760c2e63490428487a40142492a9ed2 [file] [log] [blame]
Angel Pons585495e2020-04-03 01:21:38 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Rothb28f4662018-05-26 17:58:47 -06002
Stefan Reinauere2b53e12004-06-28 11:59:45 +00003#include <console/console.h>
Gerd Hoffmannaa588e02013-05-31 09:26:55 +02004#include <cpu/cpu.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +01005#include <cpu/x86/lapic_def.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00006#include <arch/io.h>
Elyes HAOUASed69de32019-12-19 17:36:53 +01007#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Uwe Hermann74d1a6e2010-10-12 17:34:08 +00009#include <arch/ioapic.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000010#include <stdint.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000011#include <device/device.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000012#include <stdlib.h>
13#include <string.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020014#include <smbios.h>
Patrick Rudolph69d5ef92018-11-11 12:43:48 +010015#include "memory.h"
Myles Watson0520d552009-05-11 22:44:14 +000016
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020017#include "fw_cfg.h"
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010018#include "fw_cfg_if.h"
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020019
Vladimir Serbinenko41877d82014-09-01 22:18:01 +020020#include "acpi.h"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020021
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020022static void qemu_reserve_ports(struct device *dev, unsigned int idx,
23 unsigned int base, unsigned int size,
24 const char *name)
25{
26 unsigned int end = base + size -1;
27 struct resource *res;
28
29 printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n",
30 base, end, name);
31 res = new_resource(dev, idx);
32 res->base = base;
33 res->size = size;
34 res->limit = 0xffff;
35 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
36 IORESOURCE_ASSIGNED;
37}
38
Kyösti Mälkkiccb95022018-05-22 00:16:23 +030039static void cpu_pci_domain_set_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000040{
Myles Watson894a3472010-06-09 22:41:35 +000041 assign_resources(dev->link_list);
Eric Biederman6e53f502004-10-27 08:53:57 +000042}
Stefan Reinauere2b53e12004-06-28 11:59:45 +000043
Myles Watson29cc9ed2009-07-02 18:56:24 +000044static void cpu_pci_domain_read_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000045{
Kyösti Mälkki98a91742018-05-21 21:29:16 +030046 u16 nbid = pci_read_config16(pcidev_on_root(0x0, 0), PCI_DEVICE_ID);
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020047 int i440fx = (nbid == 0x1237);
Gerd Hoffmannad690f22013-09-17 10:35:43 +020048 int q35 = (nbid == 0x29c0);
Myles Watson29cc9ed2009-07-02 18:56:24 +000049 struct resource *res;
Gerd Hoffmann9839a382013-06-17 12:26:17 +020050 unsigned long tomk = 0, high;
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020051 int idx = 10;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010052 FWCfgFile f;
Myles Watson29cc9ed2009-07-02 18:56:24 +000053
54 pci_domain_read_resources(dev);
55
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010056 if (!fw_cfg_check_file(&f, "etc/e820") && f.size > 0) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010057 /* supported by qemu 1.7+ */
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010058 FwCfgE820Entry *list = malloc(f.size);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010059 int i;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010060 fw_cfg_get(f.select, list, f.size);
61 for (i = 0; i < f.size / sizeof(*list); i++) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010062 switch (list[i].type) {
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +020063 case 1: /* RAM */
Himanshu Sahdev660ff202019-09-10 16:15:41 +053064 printk(BIOS_DEBUG, "QEMU: e820/ram: 0x%08llx + 0x%08llx\n",
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010065 list[i].address, list[i].length);
66 if (list[i].address == 0) {
67 tomk = list[i].length / 1024;
68 ram_resource(dev, idx++, 0, 640);
69 ram_resource(dev, idx++, 768, tomk - 768);
70 } else {
71 ram_resource(dev, idx++,
72 list[i].address / 1024,
73 list[i].length / 1024);
74 }
75 break;
76 case 2: /* reserved */
77 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
78 list[i].address, list[i].length);
79 res = new_resource(dev, idx++);
80 res->base = list[i].address;
81 res->size = list[i].length;
82 res->limit = 0xffffffff;
83 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
84 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
85 break;
86 default:
87 /* skip unknown */
88 break;
89 }
90 }
91 free(list);
92 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020093
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010094 if (!tomk) {
95 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
96 tomk = qemu_get_memory_size();
97 high = qemu_get_high_memory_size();
98 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM below 4G.\n", tomk / 1024);
99 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM above 4G.\n", high / 1024);
100
101 /* Report the memory regions. */
102 ram_resource(dev, idx++, 0, 640);
103 ram_resource(dev, idx++, 768, tomk - 768);
104 if (high)
105 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
106 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200107
Gerd Hoffmann05d3f492013-08-06 10:48:41 +0200108 /* Reserve I/O ports used by QEMU */
109 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
110 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
111 if (i440fx) {
112 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
113 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
114 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
115 }
116 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
117 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
118 "debugcon");
119 }
120
Duncan Laurieddd4f9a2020-03-17 18:46:28 -0700121 /* A segment is legacy VGA region */
122 mmio_resource(dev, idx++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
123
124 /* C segment to 1MB is reserved RAM (low tables) */
125 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, (1 * MiB - 0xc0000) / KiB);
126
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200127 if (q35 && ((tomk * 1024) < 0xb0000000)) {
128 /*
129 * Reserve the region between top-of-ram and the
130 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
131 * place pci bars there. The region isn't declared as
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +0200132 * pci io window in the ACPI tables (\_SB.PCI0._CRS).
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200133 */
134 res = new_resource(dev, idx++);
135 res->base = tomk * 1024;
136 res->size = 0xb0000000 - tomk * 1024;
137 res->limit = 0xffffffff;
138 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
139 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
140 }
141
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200142 if (i440fx) {
143 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200144 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200145 * to put it in. */
146 res = new_resource(dev, 2);
147 res->base = IO_APIC_ADDR;
148 res->size = 0x100000UL;
149 res->limit = 0xffffffffUL;
150 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
151 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
152 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000153
154 /* Reserve space for the LAPIC. There's one in every processor, but
155 * the space only needs to be reserved once, so we do it here. */
156 res = new_resource(dev, 3);
Patrick Georgic8feedd2012-02-16 18:43:25 +0100157 res->base = LOCAL_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000158 res->size = 0x10000UL;
159 res->limit = 0xffffffffUL;
160 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
161 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000162}
163
Julius Wernercd49cce2019-03-05 16:53:33 -0800164#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200165static int qemu_get_smbios_data16(int handle, unsigned long *current)
166{
167 struct smbios_type16 *t = (struct smbios_type16 *)*current;
168 int len = sizeof(struct smbios_type16);
169
170 memset(t, 0, sizeof(struct smbios_type16));
171 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
172 t->handle = handle;
173 t->length = len - 2;
Paul Menzelb4d07572017-03-12 18:18:06 +0100174 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
175 t->use = MEMORY_ARRAY_USE_SYSTEM;
176 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200177 t->maximum_capacity = qemu_get_memory_size();
178 *current += len;
179 return len;
180}
181
182static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
183{
184 struct smbios_type17 *t = (struct smbios_type17 *)*current;
185 int len;
186
187 memset(t, 0, sizeof(struct smbios_type17));
188 t->type = SMBIOS_MEMORY_DEVICE;
189 t->handle = handle;
190 t->phys_memory_array_handle = parent_handle;
191 t->length = sizeof(struct smbios_type17) - 2;
192 t->size = qemu_get_memory_size() / 1024;
193 t->data_width = 64;
194 t->total_width = 64;
Elyes HAOUASa92acec2020-07-19 10:20:55 +0200195 t->form_factor = MEMORY_FORMFACTOR_DIMM;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200196 t->device_locator = smbios_add_string(t->eos, "Virtual");
Elyes HAOUASa92acec2020-07-19 10:20:55 +0200197 t->memory_type = MEMORY_TYPE_DDR;
198 t->type_detail = MEMORY_TYPE_DETAIL_SYNCHRONOUS;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200199 t->speed = 200;
200 t->clock_speed = 200;
201 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
202 len = t->length + smbios_string_table_len(t->eos);
203 *current += len;
204 return len;
205}
206
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300207static int qemu_get_smbios_data(struct device *dev, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200208{
209 int len;
Gerd Hoffmanndb9d1692014-08-27 11:25:13 +0200210
211 len = fw_cfg_smbios_tables(handle, current);
212 if (len != 0)
213 return len;
214
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200215 len = qemu_get_smbios_data16(*handle, current);
216 len += qemu_get_smbios_data17(*handle+1, *handle, current);
217 *handle += 2;
218 return len;
219}
220#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700221
222#if CONFIG(HAVE_ACPI_TABLES)
223static const char *qemu_acpi_name(const struct device *dev)
224{
225 if (dev->path.type == DEVICE_PATH_DOMAIN)
226 return "PCI0";
227
228 if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
229 return NULL;
230
231 return NULL;
232}
233#endif
234
Eric Biederman6e53f502004-10-27 08:53:57 +0000235static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000236 .read_resources = cpu_pci_domain_read_resources,
237 .set_resources = cpu_pci_domain_set_resources,
Myles Watson032a9652009-05-11 22:24:53 +0000238 .scan_bus = pci_domain_scan_bus,
Julius Wernercd49cce2019-03-05 16:53:33 -0800239#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200240 .get_smbios_data = qemu_get_smbios_data,
241#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700242#if CONFIG(HAVE_ACPI_TABLES)
243 .acpi_name = qemu_acpi_name,
244#endif
Myles Watson032a9652009-05-11 22:24:53 +0000245};
Eric Biederman6e53f502004-10-27 08:53:57 +0000246
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300247static void cpu_bus_init(struct device *dev)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200248{
249 initialize_cpus(dev->link_list);
250}
251
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300252static void cpu_bus_scan(struct device *bus)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200253{
254 int max_cpus = fw_cfg_max_cpus();
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300255 struct device *cpu;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200256 int i;
257
258 if (max_cpus < 0)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200259 return;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200260
261 /*
262 * TODO: This only handles the simple "qemu -smp $nr" case
263 * correctly. qemu also allows to specify the number of
264 * cores, threads & sockets.
265 */
266 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
267 for (i = 0; i < max_cpus; i++) {
268 cpu = add_cpu_device(bus->link_list, i, 1);
269 if (cpu)
270 set_cpu_topology(cpu, 1, 0, i, 0);
271 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200272}
273
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200274static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200275 .read_resources = noop_read_resources,
276 .set_resources = noop_set_resources,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200277 .init = cpu_bus_init,
278 .scan_bus = cpu_bus_scan,
279};
280
Paul Menzel5f20b352013-02-24 14:27:03 +0100281static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000282{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000283 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800284 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000285 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000286 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200287 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
288 dev->ops = &cpu_bus_ops;
289 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000290}
291
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200292struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
293 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100294 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000295};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200296
297struct chip_operations mainboard_emulation_qemu_q35_ops = {
298 CHIP_NAME("QEMU Northbridge q35")
299 .enable_dev = northbridge_enable,
300};