blob: b27e0f8502a2734b65253329cb4d907dda7bcb57 [file] [log] [blame]
Stefan Reinauere2b53e12004-06-28 11:59:45 +00001#include <console/console.h>
Gerd Hoffmannaa588e02013-05-31 09:26:55 +02002#include <cpu/cpu.h>
Patrick Georgic8feedd2012-02-16 18:43:25 +01003#include <cpu/x86/lapic_def.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00004#include <arch/io.h>
Uwe Hermann74d1a6e2010-10-12 17:34:08 +00005#include <arch/ioapic.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00006#include <stdint.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00007#include <device/device.h>
8#include <device/pci.h>
Stefan Reinauere2b53e12004-06-28 11:59:45 +00009#include <stdlib.h>
10#include <string.h>
Myles Watson2e672732009-11-12 16:38:03 +000011#include <delay.h>
Sven Schnelle164bcfd2011-08-14 20:56:34 +020012#include <smbios.h>
Rudolf Marek97be27e2010-12-13 19:50:25 +000013#include <cbmem.h>
Myles Watson0520d552009-05-11 22:44:14 +000014
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020015#include "fw_cfg.h"
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010016#include "fw_cfg_if.h"
Gerd Hoffmannaa588e02013-05-31 09:26:55 +020017
Stefan Reinauer597ff872013-01-07 13:21:22 -080018#include "memory.c"
Vladimir Serbinenko41877d82014-09-01 22:18:01 +020019#include "acpi.h"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020020
Gerd Hoffmann9839a382013-06-17 12:26:17 +020021static unsigned long qemu_get_high_memory_size(void)
22{
23 unsigned long high;
24 outb (HIGH_HIGHRAM_ADDR, CMOS_ADDR_PORT);
25 high = ((unsigned long) inb(CMOS_DATA_PORT)) << 22;
26 outb (MID_HIGHRAM_ADDR, CMOS_ADDR_PORT);
27 high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 14;
28 outb (LOW_HIGHRAM_ADDR, CMOS_ADDR_PORT);
29 high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 6;
30 return high;
31}
32
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{
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020057 u16 nbid = pci_read_config16(dev_find_slot(0, 0), PCI_DEVICE_ID);
58 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;
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010063 int size;
Myles Watson29cc9ed2009-07-02 18:56:24 +000064
65 pci_domain_read_resources(dev);
66
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010067 size = fw_cfg_check_file("etc/e820");
68 if (size > 0) {
69 /* supported by qemu 1.7+ */
70 FwCfgE820Entry *list = malloc(size);
71 int i;
72 fw_cfg_load_file("etc/e820", list);
73 for (i = 0; i < size/sizeof(*list); i++) {
74 switch (list[i].type) {
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +020075 case 1: /* RAM */
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010076 printk(BIOS_DEBUG, "QEMU: e820/ram: 0x%08llx +0x%08llx\n",
77 list[i].address, list[i].length);
78 if (list[i].address == 0) {
79 tomk = list[i].length / 1024;
80 ram_resource(dev, idx++, 0, 640);
81 ram_resource(dev, idx++, 768, tomk - 768);
82 } else {
83 ram_resource(dev, idx++,
84 list[i].address / 1024,
85 list[i].length / 1024);
86 }
87 break;
88 case 2: /* reserved */
89 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
90 list[i].address, list[i].length);
91 res = new_resource(dev, idx++);
92 res->base = list[i].address;
93 res->size = list[i].length;
94 res->limit = 0xffffffff;
95 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
96 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
97 break;
98 default:
99 /* skip unknown */
100 break;
101 }
102 }
103 free(list);
104 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200105
Gerd Hoffmannbaa78202013-11-06 14:36:17 +0100106 if (!tomk) {
107 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
108 tomk = qemu_get_memory_size();
109 high = qemu_get_high_memory_size();
110 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM below 4G.\n", tomk / 1024);
111 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM above 4G.\n", high / 1024);
112
113 /* Report the memory regions. */
114 ram_resource(dev, idx++, 0, 640);
115 ram_resource(dev, idx++, 768, tomk - 768);
116 if (high)
117 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
118 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200119
Gerd Hoffmann05d3f492013-08-06 10:48:41 +0200120 /* Reserve I/O ports used by QEMU */
121 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
122 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
123 if (i440fx) {
124 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
125 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
126 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
127 }
128 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
129 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
130 "debugcon");
131 }
132
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200133 if (q35 && ((tomk * 1024) < 0xb0000000)) {
134 /*
135 * Reserve the region between top-of-ram and the
136 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
137 * place pci bars there. The region isn't declared as
Elyes HAOUAS8ab989e2016-07-30 17:46:17 +0200138 * pci io window in the ACPI tables (\_SB.PCI0._CRS).
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200139 */
140 res = new_resource(dev, idx++);
141 res->base = tomk * 1024;
142 res->size = 0xb0000000 - tomk * 1024;
143 res->limit = 0xffffffff;
144 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
145 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
146 }
147
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200148 if (i440fx) {
149 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200150 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200151 * to put it in. */
152 res = new_resource(dev, 2);
153 res->base = IO_APIC_ADDR;
154 res->size = 0x100000UL;
155 res->limit = 0xffffffffUL;
156 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
157 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
158 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000159
160 /* Reserve space for the LAPIC. There's one in every processor, but
161 * the space only needs to be reserved once, so we do it here. */
162 res = new_resource(dev, 3);
Patrick Georgic8feedd2012-02-16 18:43:25 +0100163 res->base = LOCAL_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000164 res->size = 0x10000UL;
165 res->limit = 0xffffffffUL;
166 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
167 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000168}
169
Martin Rothf95911a2017-06-24 21:45:13 -0600170#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200171static int qemu_get_smbios_data16(int handle, unsigned long *current)
172{
173 struct smbios_type16 *t = (struct smbios_type16 *)*current;
174 int len = sizeof(struct smbios_type16);
175
176 memset(t, 0, sizeof(struct smbios_type16));
177 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
178 t->handle = handle;
179 t->length = len - 2;
Paul Menzelb4d07572017-03-12 18:18:06 +0100180 t->location = MEMORY_ARRAY_LOCATION_SYSTEM_BOARD;
181 t->use = MEMORY_ARRAY_USE_SYSTEM;
182 t->memory_error_correction = MEMORY_ARRAY_ECC_NONE;
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200183 t->maximum_capacity = qemu_get_memory_size();
184 *current += len;
185 return len;
186}
187
188static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
189{
190 struct smbios_type17 *t = (struct smbios_type17 *)*current;
191 int len;
192
193 memset(t, 0, sizeof(struct smbios_type17));
194 t->type = SMBIOS_MEMORY_DEVICE;
195 t->handle = handle;
196 t->phys_memory_array_handle = parent_handle;
197 t->length = sizeof(struct smbios_type17) - 2;
198 t->size = qemu_get_memory_size() / 1024;
199 t->data_width = 64;
200 t->total_width = 64;
201 t->form_factor = 9; /* DIMM */
202 t->device_locator = smbios_add_string(t->eos, "Virtual");
203 t->memory_type = 0x12; /* DDR */
204 t->type_detail = 0x80; /* Synchronous */
205 t->speed = 200;
206 t->clock_speed = 200;
207 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
208 len = t->length + smbios_string_table_len(t->eos);
209 *current += len;
210 return len;
211}
212
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300213static int qemu_get_smbios_data(struct device *dev, int *handle, unsigned long *current)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200214{
215 int len;
Gerd Hoffmanndb9d1692014-08-27 11:25:13 +0200216
217 len = fw_cfg_smbios_tables(handle, current);
218 if (len != 0)
219 return len;
220
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200221 len = qemu_get_smbios_data16(*handle, current);
222 len += qemu_get_smbios_data17(*handle+1, *handle, current);
223 *handle += 2;
224 return len;
225}
226#endif
Eric Biederman6e53f502004-10-27 08:53:57 +0000227static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000228 .read_resources = cpu_pci_domain_read_resources,
229 .set_resources = cpu_pci_domain_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000230 .enable_resources = NULL,
231 .init = NULL,
Myles Watson032a9652009-05-11 22:24:53 +0000232 .scan_bus = pci_domain_scan_bus,
Martin Rothf95911a2017-06-24 21:45:13 -0600233#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLES)
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200234 .get_smbios_data = qemu_get_smbios_data,
235#endif
Myles Watson032a9652009-05-11 22:24:53 +0000236};
Eric Biederman6e53f502004-10-27 08:53:57 +0000237
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300238static void cpu_bus_init(struct device *dev)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200239{
240 initialize_cpus(dev->link_list);
241}
242
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300243static void cpu_bus_scan(struct device *bus)
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200244{
245 int max_cpus = fw_cfg_max_cpus();
Kyösti Mälkkiccb95022018-05-22 00:16:23 +0300246 struct device *cpu;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200247 int i;
248
249 if (max_cpus < 0)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200250 return;
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200251
252 /*
253 * TODO: This only handles the simple "qemu -smp $nr" case
254 * correctly. qemu also allows to specify the number of
255 * cores, threads & sockets.
256 */
257 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
258 for (i = 0; i < max_cpus; i++) {
259 cpu = add_cpu_device(bus->link_list, i, 1);
260 if (cpu)
261 set_cpu_topology(cpu, 1, 0, i, 0);
262 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200263}
264
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200265static struct device_operations cpu_bus_ops = {
Edward O'Callaghanc3f38cd2014-10-31 08:04:30 +1100266 .read_resources = DEVICE_NOOP,
267 .set_resources = DEVICE_NOOP,
268 .enable_resources = DEVICE_NOOP,
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200269 .init = cpu_bus_init,
270 .scan_bus = cpu_bus_scan,
271};
272
Paul Menzel5f20b352013-02-24 14:27:03 +0100273static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000274{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000275 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800276 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000277 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000278 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200279 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
280 dev->ops = &cpu_bus_ops;
281 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000282}
283
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200284struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
285 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100286 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000287};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200288
289struct chip_operations mainboard_emulation_qemu_q35_ops = {
290 CHIP_NAME("QEMU Northbridge q35")
291 .enable_dev = northbridge_enable,
292};