blob: 609e389899655e6051aa3a817f9d8c89c2c203d9 [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
Kyösti Mälkkib78e4622022-12-15 22:12:29 +02003#include <arch/io.h>
4#include <arch/ioapic.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00005#include <console/console.h>
Gerd Hoffmannaa588e02013-05-31 09:26:55 +02006#include <cpu/cpu.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +01007#include <cpu/x86/lapic_def.h>
Arthur Heymansa75a2fa2020-12-01 15:20:10 +01008#include <cpu/x86/mp.h>
Elyes HAOUASed69de32019-12-19 17:36:53 +01009#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020010#include <device/pci_ops.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000011#include <device/device.h>
Kyösti Mälkkib78e4622022-12-15 22:12:29 +020012#include <identity.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000013#include <stdlib.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020014#include <smbios.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020015#include <types.h>
Patrick Rudolph69d5ef92018-11-11 12:43:48 +010016#include "memory.h"
Myles Watson0520d552009-05-11 22:44:14 +000017
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020018#include "fw_cfg.h"
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010019#include "fw_cfg_if.h"
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020020
Vladimir Serbinenko41877d82014-09-01 22:18:01 +020021#include "acpi.h"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020022
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020023static void qemu_reserve_ports(struct device *dev, unsigned int idx,
24 unsigned int base, unsigned int size,
25 const char *name)
26{
27 unsigned int end = base + size -1;
28 struct resource *res;
29
30 printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n",
31 base, end, name);
32 res = new_resource(dev, idx);
33 res->base = base;
34 res->size = size;
35 res->limit = 0xffff;
36 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
37 IORESOURCE_ASSIGNED;
38}
39
Kyösti Mälkkiccb95022018-05-22 00:16:23 +030040static void cpu_pci_domain_set_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000041{
Arthur Heymans7fcd4d52023-08-24 15:12:19 +020042 assign_resources(dev->downstream);
Eric Biederman6e53f502004-10-27 08:53:57 +000043}
Stefan Reinauere2b53e12004-06-28 11:59:45 +000044
Myles Watson29cc9ed2009-07-02 18:56:24 +000045static void cpu_pci_domain_read_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000046{
Kyösti Mälkki98a91742018-05-21 21:29:16 +030047 u16 nbid = pci_read_config16(pcidev_on_root(0x0, 0), PCI_DEVICE_ID);
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020048 int i440fx = (nbid == 0x1237);
Gerd Hoffmannad690f22013-09-17 10:35:43 +020049 int q35 = (nbid == 0x29c0);
Myles Watson29cc9ed2009-07-02 18:56:24 +000050 struct resource *res;
Kyösti Mälkki7c600682022-07-01 18:50:26 +030051 uint64_t tomk = 0;
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020052 int idx = 10;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010053 FWCfgFile f;
Myles Watson29cc9ed2009-07-02 18:56:24 +000054
55 pci_domain_read_resources(dev);
56
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010057 if (!fw_cfg_check_file(&f, "etc/e820") && f.size > 0) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010058 /* supported by qemu 1.7+ */
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010059 FwCfgE820Entry *list = malloc(f.size);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010060 int i;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010061 fw_cfg_get(f.select, list, f.size);
62 for (i = 0; i < f.size / sizeof(*list); i++) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010063 switch (list[i].type) {
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +020064 case 1: /* RAM */
Himanshu Sahdev660ff202019-09-10 16:15:41 +053065 printk(BIOS_DEBUG, "QEMU: e820/ram: 0x%08llx + 0x%08llx\n",
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010066 list[i].address, list[i].length);
67 if (list[i].address == 0) {
68 tomk = list[i].length / 1024;
Kyösti Mälkkib20a7142021-06-28 00:00:04 +030069 ram_from_to(dev, idx++, 0, 0xa0000);
70 ram_from_to(dev, idx++, 0xc0000, tomk * KiB);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010071 } else {
Kyösti Mälkkib20a7142021-06-28 00:00:04 +030072 ram_range(dev, idx++, list[i].address, list[i].length);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010073 }
74 break;
75 case 2: /* reserved */
76 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
77 list[i].address, list[i].length);
78 res = new_resource(dev, idx++);
79 res->base = list[i].address;
80 res->size = list[i].length;
81 res->limit = 0xffffffff;
82 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
83 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
84 break;
85 default:
86 /* skip unknown */
87 break;
88 }
89 }
90 free(list);
91 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020092
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010093 if (!tomk) {
94 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
95 tomk = qemu_get_memory_size();
Kyösti Mälkkib20a7142021-06-28 00:00:04 +030096 uint64_t high = qemu_get_high_memory_size();
Kyösti Mälkki7c600682022-07-01 18:50:26 +030097 printk(BIOS_DEBUG, "QEMU: cmos: %llu MiB RAM below 4G.\n", tomk / 1024);
Kyösti Mälkkib20a7142021-06-28 00:00:04 +030098 printk(BIOS_DEBUG, "QEMU: cmos: %llu MiB RAM above 4G.\n", high / 1024);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010099
100 /* Report the memory regions. */
Kyösti Mälkkib20a7142021-06-28 00:00:04 +0300101 ram_from_to(dev, idx++, 0, 0xa0000);
102 ram_from_to(dev, idx++, 0xc0000, tomk * KiB);
103
Gerd Hoffmannbaa78202013-11-06 14:36:17 +0100104 if (high)
Kyösti Mälkkib20a7142021-06-28 00:00:04 +0300105 upper_ram_end(dev, idx++, 4ull * GiB + high * KiB);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +0100106 }
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 */
Kyösti Mälkkib20a7142021-06-28 00:00:04 +0300122 mmio_from_to(dev, idx++, 0xa0000, 0xc0000);
Duncan Laurieddd4f9a2020-03-17 18:46:28 -0700123
124 /* C segment to 1MB is reserved RAM (low tables) */
Kyösti Mälkkib20a7142021-06-28 00:00:04 +0300125 reserved_ram_from_to(dev, idx++, 0xc0000, 1 * MiB);
Duncan Laurieddd4f9a2020-03-17 18:46:28 -0700126
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);
Kyösti Mälkkidea42e02021-05-31 20:26:16 +0300157 res->base = cpu_get_lapic_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{
Angel Ponsd62a5012021-06-28 17:18:06 +0200167 struct smbios_type16 *t = smbios_carve_table(*current, SMBIOS_PHYS_MEMORY_ARRAY,
168 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200169
Paul Menzelb4d07572017-03-12 18:18:06 +0100170 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
171 t->use = MEMORY_ARRAY_USE_SYSTEM;
172 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200173 t->maximum_capacity = qemu_get_memory_size();
Angel Ponsd62a5012021-06-28 17:18:06 +0200174
Angel Ponsa37701a2021-06-28 17:36:53 +0200175 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200176 *current += len;
177 return len;
178}
179
180static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
181{
Angel Ponsd62a5012021-06-28 17:18:06 +0200182 struct smbios_type17 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE,
183 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200184
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200185 t->phys_memory_array_handle = parent_handle;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200186 t->size = qemu_get_memory_size() / 1024;
187 t->data_width = 64;
188 t->total_width = 64;
Elyes HAOUASa92acec2020-07-19 10:20:55 +0200189 t->form_factor = MEMORY_FORMFACTOR_DIMM;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200190 t->device_locator = smbios_add_string(t->eos, "Virtual");
Elyes HAOUASa92acec2020-07-19 10:20:55 +0200191 t->memory_type = MEMORY_TYPE_DDR;
192 t->type_detail = MEMORY_TYPE_DETAIL_SYNCHRONOUS;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200193 t->speed = 200;
194 t->clock_speed = 200;
Kyösti Mälkkib78e4622022-12-15 22:12:29 +0200195 t->manufacturer = smbios_add_string(t->eos, mainboard_vendor);
Angel Ponsd62a5012021-06-28 17:18:06 +0200196
Angel Ponsa37701a2021-06-28 17:36:53 +0200197 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200198 *current += len;
199 return len;
200}
201
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300202static int qemu_get_smbios_data(struct device *dev, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200203{
204 int len;
Gerd Hoffmanndb9d1692014-08-27 11:25:13 +0200205
206 len = fw_cfg_smbios_tables(handle, current);
207 if (len != 0)
208 return len;
209
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200210 len = qemu_get_smbios_data16(*handle, current);
211 len += qemu_get_smbios_data17(*handle+1, *handle, current);
212 *handle += 2;
213 return len;
214}
215#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700216
217#if CONFIG(HAVE_ACPI_TABLES)
218static const char *qemu_acpi_name(const struct device *dev)
219{
220 if (dev->path.type == DEVICE_PATH_DOMAIN)
221 return "PCI0";
222
Fabio Aiuto61ed4ef2022-09-30 14:55:53 +0200223 if (!is_pci_dev_on_bus(dev, 0))
Duncan Laurieb40e7802020-03-17 18:47:36 -0700224 return NULL;
225
226 return NULL;
227}
228#endif
229
Eric Biederman6e53f502004-10-27 08:53:57 +0000230static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000231 .read_resources = cpu_pci_domain_read_resources,
232 .set_resources = cpu_pci_domain_set_resources,
Arthur Heymans0b0113f2023-08-31 17:09:28 +0200233 .scan_bus = pci_host_bridge_scan_bus,
Julius Wernercd49cce2019-03-05 16:53:33 -0800234#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200235 .get_smbios_data = qemu_get_smbios_data,
236#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700237#if CONFIG(HAVE_ACPI_TABLES)
238 .acpi_name = qemu_acpi_name,
239#endif
Myles Watson032a9652009-05-11 22:24:53 +0000240};
Eric Biederman6e53f502004-10-27 08:53:57 +0000241
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100242static const struct mp_ops mp_ops_no_smm = {
243 .get_cpu_count = fw_cfg_max_cpus,
244};
245
Arthur Heymanse69d2df2020-12-01 18:29:13 +0100246extern const struct mp_ops mp_ops_with_smm;
247
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100248void mp_init_cpus(struct bus *cpu_bus)
249{
Arthur Heymans4db2e8e2021-10-28 16:48:36 +0200250 const struct mp_ops *ops = CONFIG(NO_SMM) ? &mp_ops_no_smm : &mp_ops_with_smm;
Arthur Heymanse69d2df2020-12-01 18:29:13 +0100251
Felix Held4dd7d112021-10-20 23:31:43 +0200252 /* TODO: Handle mp_init_with_smm failure? */
253 mp_init_with_smm(cpu_bus, ops);
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100254}
255
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300256static void cpu_bus_init(struct device *dev)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200257{
Arthur Heymansc8a20b92022-11-08 06:49:12 +0100258 mp_cpu_bus_init(dev);
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200259}
260
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200261static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200262 .read_resources = noop_read_resources,
263 .set_resources = noop_set_resources,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200264 .init = cpu_bus_init,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200265};
266
Paul Menzel5f20b352013-02-24 14:27:03 +0100267static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000268{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000269 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800270 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000271 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000272 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200273 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
274 dev->ops = &cpu_bus_ops;
275 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000276}
277
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200278struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900279 .name = "QEMU Northbridge i440fx",
Paul Menzel5f20b352013-02-24 14:27:03 +0100280 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000281};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200282
283struct chip_operations mainboard_emulation_qemu_q35_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900284 .name = "QEMU Northbridge q35",
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200285 .enable_dev = northbridge_enable,
286};