blob: 6312d8e7f7a07197bfe4b948c15c1c9d59cce927 [file] [log] [blame]
Angel Pons585495e2020-04-03 01:21:38 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Martin Rothb28f4662018-05-26 17:58:47 -06003
Stefan Reinauere2b53e12004-06-28 11:59:45 +00004#include <console/console.h>
Gerd Hoffmannaa588e02013-05-31 09:26:55 +02005#include <cpu/cpu.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +01006#include <cpu/x86/lapic_def.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>
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{
Myles Watson894a3472010-06-09 22:41:35 +000042 assign_resources(dev->link_list);
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;
Gerd Hoffmann9839a382013-06-17 12:26:17 +020051 unsigned long tomk = 0, high;
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;
69 ram_resource(dev, idx++, 0, 640);
70 ram_resource(dev, idx++, 768, tomk - 768);
71 } else {
72 ram_resource(dev, idx++,
73 list[i].address / 1024,
74 list[i].length / 1024);
75 }
76 break;
77 case 2: /* reserved */
78 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
79 list[i].address, list[i].length);
80 res = new_resource(dev, idx++);
81 res->base = list[i].address;
82 res->size = list[i].length;
83 res->limit = 0xffffffff;
84 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
85 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
86 break;
87 default:
88 /* skip unknown */
89 break;
90 }
91 }
92 free(list);
93 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020094
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010095 if (!tomk) {
96 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
97 tomk = qemu_get_memory_size();
98 high = qemu_get_high_memory_size();
99 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM below 4G.\n", tomk / 1024);
100 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM above 4G.\n", high / 1024);
101
102 /* Report the memory regions. */
103 ram_resource(dev, idx++, 0, 640);
104 ram_resource(dev, idx++, 768, tomk - 768);
105 if (high)
106 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
107 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200108
Gerd Hoffmann05d3f492013-08-06 10:48:41 +0200109 /* Reserve I/O ports used by QEMU */
110 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
111 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
112 if (i440fx) {
113 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
114 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
115 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
116 }
117 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
118 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
119 "debugcon");
120 }
121
Duncan Laurieddd4f9a2020-03-17 18:46:28 -0700122 /* A segment is legacy VGA region */
123 mmio_resource(dev, idx++, 0xa0000 / KiB, (0xc0000 - 0xa0000) / KiB);
124
125 /* C segment to 1MB is reserved RAM (low tables) */
126 reserved_ram_resource(dev, idx++, 0xc0000 / KiB, (1 * MiB - 0xc0000) / KiB);
127
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200128 if (q35 && ((tomk * 1024) < 0xb0000000)) {
129 /*
130 * Reserve the region between top-of-ram and the
131 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
132 * place pci bars there. The region isn't declared as
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +0200133 * pci io window in the ACPI tables (\_SB.PCI0._CRS).
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200134 */
135 res = new_resource(dev, idx++);
136 res->base = tomk * 1024;
137 res->size = 0xb0000000 - tomk * 1024;
138 res->limit = 0xffffffff;
139 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
140 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
141 }
142
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200143 if (i440fx) {
144 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200145 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200146 * to put it in. */
147 res = new_resource(dev, 2);
148 res->base = IO_APIC_ADDR;
149 res->size = 0x100000UL;
150 res->limit = 0xffffffffUL;
151 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
152 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
153 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000154
155 /* Reserve space for the LAPIC. There's one in every processor, but
156 * the space only needs to be reserved once, so we do it here. */
157 res = new_resource(dev, 3);
Patrick Georgic8feedd2012-02-16 18:43:25 +0100158 res->base = LOCAL_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000159 res->size = 0x10000UL;
160 res->limit = 0xffffffffUL;
161 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
162 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000163}
164
Julius Wernercd49cce2019-03-05 16:53:33 -0800165#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200166static int qemu_get_smbios_data16(int handle, unsigned long *current)
167{
168 struct smbios_type16 *t = (struct smbios_type16 *)*current;
169 int len = sizeof(struct smbios_type16);
170
171 memset(t, 0, sizeof(struct smbios_type16));
172 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
173 t->handle = handle;
174 t->length = len - 2;
Paul Menzelb4d07572017-03-12 18:18:06 +0100175 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
176 t->use = MEMORY_ARRAY_USE_SYSTEM;
177 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200178 t->maximum_capacity = qemu_get_memory_size();
179 *current += len;
180 return len;
181}
182
183static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
184{
185 struct smbios_type17 *t = (struct smbios_type17 *)*current;
186 int len;
187
188 memset(t, 0, sizeof(struct smbios_type17));
189 t->type = SMBIOS_MEMORY_DEVICE;
190 t->handle = handle;
191 t->phys_memory_array_handle = parent_handle;
192 t->length = sizeof(struct smbios_type17) - 2;
193 t->size = qemu_get_memory_size() / 1024;
194 t->data_width = 64;
195 t->total_width = 64;
196 t->form_factor = 9; /* DIMM */
197 t->device_locator = smbios_add_string(t->eos, "Virtual");
198 t->memory_type = 0x12; /* DDR */
199 t->type_detail = 0x80; /* Synchronous */
200 t->speed = 200;
201 t->clock_speed = 200;
202 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
203 len = t->length + smbios_string_table_len(t->eos);
204 *current += len;
205 return len;
206}
207
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300208static int qemu_get_smbios_data(struct device *dev, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200209{
210 int len;
Gerd Hoffmanndb9d1692014-08-27 11:25:13 +0200211
212 len = fw_cfg_smbios_tables(handle, current);
213 if (len != 0)
214 return len;
215
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200216 len = qemu_get_smbios_data16(*handle, current);
217 len += qemu_get_smbios_data17(*handle+1, *handle, current);
218 *handle += 2;
219 return len;
220}
221#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700222
223#if CONFIG(HAVE_ACPI_TABLES)
224static const char *qemu_acpi_name(const struct device *dev)
225{
226 if (dev->path.type == DEVICE_PATH_DOMAIN)
227 return "PCI0";
228
229 if (dev->path.type != DEVICE_PATH_PCI || dev->bus->secondary != 0)
230 return NULL;
231
232 return NULL;
233}
234#endif
235
Eric Biederman6e53f502004-10-27 08:53:57 +0000236static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000237 .read_resources = cpu_pci_domain_read_resources,
238 .set_resources = cpu_pci_domain_set_resources,
Myles Watson032a9652009-05-11 22:24:53 +0000239 .scan_bus = pci_domain_scan_bus,
Julius Wernercd49cce2019-03-05 16:53:33 -0800240#if CONFIG(GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200241 .get_smbios_data = qemu_get_smbios_data,
242#endif
Duncan Laurieb40e7802020-03-17 18:47:36 -0700243#if CONFIG(HAVE_ACPI_TABLES)
244 .acpi_name = qemu_acpi_name,
245#endif
Myles Watson032a9652009-05-11 22:24:53 +0000246};
Eric Biederman6e53f502004-10-27 08:53:57 +0000247
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300248static void cpu_bus_init(struct device *dev)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200249{
250 initialize_cpus(dev->link_list);
251}
252
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300253static void cpu_bus_scan(struct device *bus)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200254{
255 int max_cpus = fw_cfg_max_cpus();
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300256 struct device *cpu;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200257 int i;
258
259 if (max_cpus < 0)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200260 return;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200261
262 /*
263 * TODO: This only handles the simple "qemu -smp $nr" case
264 * correctly. qemu also allows to specify the number of
265 * cores, threads & sockets.
266 */
267 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
268 for (i = 0; i < max_cpus; i++) {
269 cpu = add_cpu_device(bus->link_list, i, 1);
270 if (cpu)
271 set_cpu_topology(cpu, 1, 0, i, 0);
272 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200273}
274
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200275static struct device_operations cpu_bus_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200276 .read_resources = noop_read_resources,
277 .set_resources = noop_set_resources,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200278 .init = cpu_bus_init,
279 .scan_bus = cpu_bus_scan,
280};
281
Paul Menzel5f20b352013-02-24 14:27:03 +0100282static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000283{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000284 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800285 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000286 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000287 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200288 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
289 dev->ops = &cpu_bus_ops;
290 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000291}
292
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200293struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
294 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100295 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000296};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200297
298struct chip_operations mainboard_emulation_qemu_q35_ops = {
299 CHIP_NAME("QEMU Northbridge q35")
300 .enable_dev = northbridge_enable,
301};