blob: b7630a138b34198927b004faaa3f95ad7fe5fbc1 [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>
Arthur Heymansa75a2fa2020-12-01 15:20:10 +01006#include <cpu/x86/mp.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00007#include <arch/io.h>
Elyes HAOUASed69de32019-12-19 17:36:53 +01008#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Uwe Hermann74d1a6e2010-10-12 17:34:08 +000010#include <arch/ioapic.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000011#include <stdint.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000012#include <device/device.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +000013#include <stdlib.h>
14#include <string.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020015#include <smbios.h>
Felix Heldd27ef5b2021-10-20 20:18:12 +020016#include <types.h>
Patrick Rudolph69d5ef92018-11-11 12:43:48 +010017#include "memory.h"
Myles Watson0520d552009-05-11 22:44:14 +000018
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020019#include "fw_cfg.h"
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010020#include "fw_cfg_if.h"
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020021
Vladimir Serbinenko41877d82014-09-01 22:18:01 +020022#include "acpi.h"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020023
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020024static void qemu_reserve_ports(struct device *dev, unsigned int idx,
25 unsigned int base, unsigned int size,
26 const char *name)
27{
28 unsigned int end = base + size -1;
29 struct resource *res;
30
31 printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n",
32 base, end, name);
33 res = new_resource(dev, idx);
34 res->base = base;
35 res->size = size;
36 res->limit = 0xffff;
37 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
38 IORESOURCE_ASSIGNED;
39}
40
Kyösti Mälkkiccb95022018-05-22 00:16:23 +030041static void cpu_pci_domain_set_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000042{
Myles Watson894a3472010-06-09 22:41:35 +000043 assign_resources(dev->link_list);
Eric Biederman6e53f502004-10-27 08:53:57 +000044}
Stefan Reinauere2b53e12004-06-28 11:59:45 +000045
Myles Watson29cc9ed2009-07-02 18:56:24 +000046static void cpu_pci_domain_read_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000047{
Kyösti Mälkki98a91742018-05-21 21:29:16 +030048 u16 nbid = pci_read_config16(pcidev_on_root(0x0, 0), PCI_DEVICE_ID);
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020049 int i440fx = (nbid == 0x1237);
Gerd Hoffmannad690f22013-09-17 10:35:43 +020050 int q35 = (nbid == 0x29c0);
Myles Watson29cc9ed2009-07-02 18:56:24 +000051 struct resource *res;
Gerd Hoffmann9839a382013-06-17 12:26:17 +020052 unsigned long tomk = 0, high;
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020053 int idx = 10;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010054 FWCfgFile f;
Myles Watson29cc9ed2009-07-02 18:56:24 +000055
56 pci_domain_read_resources(dev);
57
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010058 if (!fw_cfg_check_file(&f, "etc/e820") && f.size > 0) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010059 /* supported by qemu 1.7+ */
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010060 FwCfgE820Entry *list = malloc(f.size);
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010061 int i;
Thomas Heijligenbcd84fe2019-01-10 16:53:34 +010062 fw_cfg_get(f.select, list, f.size);
63 for (i = 0; i < f.size / sizeof(*list); i++) {
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010064 switch (list[i].type) {
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +020065 case 1: /* RAM */
Himanshu Sahdev660ff202019-09-10 16:15:41 +053066 printk(BIOS_DEBUG, "QEMU: e820/ram: 0x%08llx + 0x%08llx\n",
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010067 list[i].address, list[i].length);
68 if (list[i].address == 0) {
69 tomk = list[i].length / 1024;
70 ram_resource(dev, idx++, 0, 640);
71 ram_resource(dev, idx++, 768, tomk - 768);
72 } else {
73 ram_resource(dev, idx++,
74 list[i].address / 1024,
75 list[i].length / 1024);
76 }
77 break;
78 case 2: /* reserved */
79 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
80 list[i].address, list[i].length);
81 res = new_resource(dev, idx++);
82 res->base = list[i].address;
83 res->size = list[i].length;
84 res->limit = 0xffffffff;
85 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
86 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
87 break;
88 default:
89 /* skip unknown */
90 break;
91 }
92 }
93 free(list);
94 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020095
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010096 if (!tomk) {
97 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
98 tomk = qemu_get_memory_size();
99 high = qemu_get_high_memory_size();
100 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM below 4G.\n", tomk / 1024);
101 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM above 4G.\n", high / 1024);
102
103 /* Report the memory regions. */
104 ram_resource(dev, idx++, 0, 640);
105 ram_resource(dev, idx++, 768, tomk - 768);
106 if (high)
107 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
108 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200109
Gerd Hoffmann05d3f492013-08-06 10:48:41 +0200110 /* Reserve I/O ports used by QEMU */
111 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
112 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
113 if (i440fx) {
114 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
115 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
116 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
117 }
118 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
119 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
120 "debugcon");
121 }
122
Duncan Laurieddd4f9a2020-03-17 18:46:28 -0700123 /* A segment is legacy VGA region */
124 mmio_resource(dev, idx++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
125
126 /* C segment to 1MB is reserved RAM (low tables) */
127 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, (1 * MiB - 0xc0000) / KiB);
128
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200129 if (q35 && ((tomk * 1024) < 0xb0000000)) {
130 /*
131 * Reserve the region between top-of-ram and the
132 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
133 * place pci bars there. The region isn't declared as
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +0200134 * pci io window in the ACPI tables (\_SB.PCI0._CRS).
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200135 */
136 res = new_resource(dev, idx++);
137 res->base = tomk * 1024;
138 res->size = 0xb0000000 - tomk * 1024;
139 res->limit = 0xffffffff;
140 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
141 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
142 }
143
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200144 if (i440fx) {
145 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200146 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200147 * to put it in. */
148 res = new_resource(dev, 2);
149 res->base = IO_APIC_ADDR;
150 res->size = 0x100000UL;
151 res->limit = 0xffffffffUL;
152 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
153 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
154 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000155
156 /* Reserve space for the LAPIC. There's one in every processor, but
157 * the space only needs to be reserved once, so we do it here. */
158 res = new_resource(dev, 3);
Kyösti Mälkkidea42e02021-05-31 20:26:16 +0300159 res->base = cpu_get_lapic_addr();
Myles Watson29cc9ed2009-07-02 18:56:24 +0000160 res->size = 0x10000UL;
161 res->limit = 0xffffffffUL;
162 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
163 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000164}
165
Julius Wernercd49cce2019-03-05 16:53:33 -0800166#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200167static int qemu_get_smbios_data16(int handle, unsigned long *current)
168{
Angel Ponsd62a5012021-06-28 17:18:06 +0200169 struct smbios_type16 *t = smbios_carve_table(*current, SMBIOS_PHYS_MEMORY_ARRAY,
170 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200171
Paul Menzelb4d07572017-03-12 18:18:06 +0100172 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
173 t->use = MEMORY_ARRAY_USE_SYSTEM;
174 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200175 t->maximum_capacity = qemu_get_memory_size();
Angel Ponsd62a5012021-06-28 17:18:06 +0200176
Angel Ponsa37701a2021-06-28 17:36:53 +0200177 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200178 *current += len;
179 return len;
180}
181
182static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
183{
Angel Ponsd62a5012021-06-28 17:18:06 +0200184 struct smbios_type17 *t = smbios_carve_table(*current, SMBIOS_MEMORY_DEVICE,
185 sizeof(*t), handle);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200186
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200187 t->phys_memory_array_handle = parent_handle;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200188 t->size = qemu_get_memory_size() / 1024;
189 t->data_width = 64;
190 t->total_width = 64;
Elyes HAOUASa92acec2020-07-19 10:20:55 +0200191 t->form_factor = MEMORY_FORMFACTOR_DIMM;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200192 t->device_locator = smbios_add_string(t->eos, "Virtual");
Elyes HAOUASa92acec2020-07-19 10:20:55 +0200193 t->memory_type = MEMORY_TYPE_DDR;
194 t->type_detail = MEMORY_TYPE_DETAIL_SYNCHRONOUS;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200195 t->speed = 200;
196 t->clock_speed = 200;
197 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
Angel Ponsd62a5012021-06-28 17:18:06 +0200198
Angel Ponsa37701a2021-06-28 17:36:53 +0200199 const int len = smbios_full_table_len(&t->header, t->eos);
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200200 *current += len;
201 return len;
202}
203
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300204static int qemu_get_smbios_data(struct device *dev, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200205{
206 int len;
Gerd Hoffmanndb9d1692014-08-27 11:25:13 +0200207
208 len = fw_cfg_smbios_tables(handle, current);
209 if (len != 0)
210 return len;
211
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200212 len = qemu_get_smbios_data16(*handle, current);
213 len += qemu_get_smbios_data17(*handle+1, *handle, current);
214 *handle += 2;
215 return len;
216}
217#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700218
219#if CONFIG(HAVE_ACPI_TABLES)
220static const char *qemu_acpi_name(const struct device *dev)
221{
222 if (dev->path.type == DEVICE_PATH_DOMAIN)
223 return "PCI0";
224
225 if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
226 return NULL;
227
228 return NULL;
229}
230#endif
231
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 Watson032a9652009-05-11 22:24:53 +0000235 .scan_bus = pci_domain_scan_bus,
Julius Wernercd49cce2019-03-05 16:53:33 -0800236#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200237 .get_smbios_data = qemu_get_smbios_data,
238#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700239#if CONFIG(HAVE_ACPI_TABLES)
240 .acpi_name = qemu_acpi_name,
241#endif
Myles Watson032a9652009-05-11 22:24:53 +0000242};
Eric Biederman6e53f502004-10-27 08:53:57 +0000243
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100244static const struct mp_ops mp_ops_no_smm = {
245 .get_cpu_count = fw_cfg_max_cpus,
246};
247
Arthur Heymanse69d2df2020-12-01 18:29:13 +0100248extern const struct mp_ops mp_ops_with_smm;
249
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100250void mp_init_cpus(struct bus *cpu_bus)
251{
Arthur Heymanse69d2df2020-12-01 18:29:13 +0100252 const struct mp_ops *ops = CONFIG(SMM_TSEG) ? &mp_ops_with_smm : &mp_ops_no_smm;
253
Felix Heldd27ef5b2021-10-20 20:18:12 +0200254 if (mp_init_with_smm(cpu_bus, ops) != CB_SUCCESS)
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100255 printk(BIOS_ERR, "MP initialization failure.\n");
256}
257
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300258static void cpu_bus_init(struct device *dev)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200259{
Arthur Heymansa75a2fa2020-12-01 15:20:10 +0100260 if (CONFIG(PARALLEL_MP))
261 mp_cpu_bus_init(dev);
262 else
263 initialize_cpus(dev->link_list);
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200264}
265
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300266static void cpu_bus_scan(struct device *bus)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200267{
268 int max_cpus = fw_cfg_max_cpus();
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300269 struct device *cpu;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200270 int i;
271
272 if (max_cpus < 0)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200273 return;
Patrick Rudolphcfa02252021-02-02 18:14:24 +0100274 /*
275 * Do not install more CPUs than supported by coreboot.
276 * This will cause a buffer overflow where fixed arrays of CONFIG_MAX_CPUS
277 * are used and might result in a boot failure.
278 */
279 max_cpus = MIN(max_cpus, CONFIG_MAX_CPUS);
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200280
281 /*
282 * TODO: This only handles the simple "qemu -smp $nr" case
283 * correctly. qemu also allows to specify the number of
284 * cores, threads & sockets.
285 */
286 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
287 for (i = 0; i < max_cpus; i++) {
288 cpu = add_cpu_device(bus->link_list, i, 1);
289 if (cpu)
290 set_cpu_topology(cpu, 1, 0, i, 0);
291 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200292}
293
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200294static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200295 .read_resources = noop_read_resources,
296 .set_resources = noop_set_resources,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200297 .init = cpu_bus_init,
298 .scan_bus = cpu_bus_scan,
299};
300
Paul Menzel5f20b352013-02-24 14:27:03 +0100301static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000302{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000303 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800304 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000305 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000306 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200307 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
308 dev->ops = &cpu_bus_ops;
309 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000310}
311
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200312struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
313 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100314 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000315};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200316
317struct chip_operations mainboard_emulation_qemu_q35_ops = {
318 CHIP_NAME("QEMU Northbridge q35")
319 .enable_dev = northbridge_enable,
320};