blob: 0f8c0c23f36c4bb46adbdde88213331b6c408a1c [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"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020019
Gerd Hoffmann9839a382013-06-17 12:26:17 +020020static unsigned long qemu_get_high_memory_size(void)
21{
22 unsigned long high;
23 outb (HIGH_HIGHRAM_ADDR, CMOS_ADDR_PORT);
24 high = ((unsigned long) inb(CMOS_DATA_PORT)) << 22;
25 outb (MID_HIGHRAM_ADDR, CMOS_ADDR_PORT);
26 high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 14;
27 outb (LOW_HIGHRAM_ADDR, CMOS_ADDR_PORT);
28 high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 6;
29 return high;
30}
31
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020032static void qemu_reserve_ports(struct device *dev, unsigned int idx,
33 unsigned int base, unsigned int size,
34 const char *name)
35{
36 unsigned int end = base + size -1;
37 struct resource *res;
38
39 printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n",
40 base, end, name);
41 res = new_resource(dev, idx);
42 res->base = base;
43 res->size = size;
44 res->limit = 0xffff;
45 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
46 IORESOURCE_ASSIGNED;
47}
48
Myles Watson29cc9ed2009-07-02 18:56:24 +000049static void cpu_pci_domain_set_resources(device_t dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000050{
Myles Watson894a3472010-06-09 22:41:35 +000051 assign_resources(dev->link_list);
Eric Biederman6e53f502004-10-27 08:53:57 +000052}
Stefan Reinauere2b53e12004-06-28 11:59:45 +000053
Myles Watson29cc9ed2009-07-02 18:56:24 +000054static void cpu_pci_domain_read_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000055{
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020056 u16 nbid = pci_read_config16(dev_find_slot(0, 0), PCI_DEVICE_ID);
57 int i440fx = (nbid == 0x1237);
Gerd Hoffmannad690f22013-09-17 10:35:43 +020058 int q35 = (nbid == 0x29c0);
Myles Watson29cc9ed2009-07-02 18:56:24 +000059 struct resource *res;
Gerd Hoffmann9839a382013-06-17 12:26:17 +020060 unsigned long tomk = 0, high;
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020061 int idx = 10;
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010062 int size;
Myles Watson29cc9ed2009-07-02 18:56:24 +000063
64 pci_domain_read_resources(dev);
65
Gerd Hoffmannbaa78202013-11-06 14:36:17 +010066 size = fw_cfg_check_file("etc/e820");
67 if (size > 0) {
68 /* supported by qemu 1.7+ */
69 FwCfgE820Entry *list = malloc(size);
70 int i;
71 fw_cfg_load_file("etc/e820", list);
72 for (i = 0; i < size/sizeof(*list); i++) {
73 switch (list[i].type) {
74 case 1: /* ram */
75 printk(BIOS_DEBUG, "QEMU: e820/ram: 0x%08llx +0x%08llx\n",
76 list[i].address, list[i].length);
77 if (list[i].address == 0) {
78 tomk = list[i].length / 1024;
79 ram_resource(dev, idx++, 0, 640);
80 ram_resource(dev, idx++, 768, tomk - 768);
81 } else {
82 ram_resource(dev, idx++,
83 list[i].address / 1024,
84 list[i].length / 1024);
85 }
86 break;
87 case 2: /* reserved */
88 printk(BIOS_DEBUG, "QEMU: e820/res: 0x%08llx +0x%08llx\n",
89 list[i].address, list[i].length);
90 res = new_resource(dev, idx++);
91 res->base = list[i].address;
92 res->size = list[i].length;
93 res->limit = 0xffffffff;
94 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
95 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
96 break;
97 default:
98 /* skip unknown */
99 break;
100 }
101 }
102 free(list);
103 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200104
Gerd Hoffmannbaa78202013-11-06 14:36:17 +0100105 if (!tomk) {
106 /* qemu older than 1.7, or reading etc/e820 failed. Fallback to cmos. */
107 tomk = qemu_get_memory_size();
108 high = qemu_get_high_memory_size();
109 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM below 4G.\n", tomk / 1024);
110 printk(BIOS_DEBUG, "QEMU: cmos: %lu MiB RAM above 4G.\n", high / 1024);
111
112 /* Report the memory regions. */
113 ram_resource(dev, idx++, 0, 640);
114 ram_resource(dev, idx++, 768, tomk - 768);
115 if (high)
116 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
117 }
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200118
Gerd Hoffmann05d3f492013-08-06 10:48:41 +0200119 /* Reserve I/O ports used by QEMU */
120 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
121 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
122 if (i440fx) {
123 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
124 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
125 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
126 }
127 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
128 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
129 "debugcon");
130 }
131
Denis 'GNUtoo' Carikli378d0462013-06-19 08:30:33 +0200132#if !CONFIG_DYNAMIC_CBMEM
Kyösti Mälkki42f46512013-06-27 08:20:09 +0300133 set_top_of_ram(tomk * 1024);
Denis 'GNUtoo' Carikli378d0462013-06-19 08:30:33 +0200134#endif
Gerd Hoffmann44b11f22013-06-17 13:30:50 +0200135
Gerd Hoffmannad690f22013-09-17 10:35:43 +0200136 if (q35 && ((tomk * 1024) < 0xb0000000)) {
137 /*
138 * Reserve the region between top-of-ram and the
139 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
140 * place pci bars there. The region isn't declared as
141 * pci io window in the acpi tables (\_SB.PCI0._CRS).
142 */
143 res = new_resource(dev, idx++);
144 res->base = tomk * 1024;
145 res->size = 0xb0000000 - tomk * 1024;
146 res->limit = 0xffffffff;
147 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
148 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
149 }
150
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200151 if (i440fx) {
152 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200153 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200154 * to put it in. */
155 res = new_resource(dev, 2);
156 res->base = IO_APIC_ADDR;
157 res->size = 0x100000UL;
158 res->limit = 0xffffffffUL;
159 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
160 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
161 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000162
163 /* Reserve space for the LAPIC. There's one in every processor, but
164 * the space only needs to be reserved once, so we do it here. */
165 res = new_resource(dev, 3);
Patrick Georgic8feedd2012-02-16 18:43:25 +0100166 res->base = LOCAL_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000167 res->size = 0x10000UL;
168 res->limit = 0xffffffffUL;
169 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
170 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000171}
172
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200173#if CONFIG_GENERATE_SMBIOS_TABLES
174static int qemu_get_smbios_data16(int handle, unsigned long *current)
175{
176 struct smbios_type16 *t = (struct smbios_type16 *)*current;
177 int len = sizeof(struct smbios_type16);
178
179 memset(t, 0, sizeof(struct smbios_type16));
180 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
181 t->handle = handle;
182 t->length = len - 2;
183 t->location = 3; /* Location: System Board */
184 t->use = 3; /* System memory */
185 t->memory_error_correction = 3; /* No error correction */
186 t->maximum_capacity = qemu_get_memory_size();
187 *current += len;
188 return len;
189}
190
191static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
192{
193 struct smbios_type17 *t = (struct smbios_type17 *)*current;
194 int len;
195
196 memset(t, 0, sizeof(struct smbios_type17));
197 t->type = SMBIOS_MEMORY_DEVICE;
198 t->handle = handle;
199 t->phys_memory_array_handle = parent_handle;
200 t->length = sizeof(struct smbios_type17) - 2;
201 t->size = qemu_get_memory_size() / 1024;
202 t->data_width = 64;
203 t->total_width = 64;
204 t->form_factor = 9; /* DIMM */
205 t->device_locator = smbios_add_string(t->eos, "Virtual");
206 t->memory_type = 0x12; /* DDR */
207 t->type_detail = 0x80; /* Synchronous */
208 t->speed = 200;
209 t->clock_speed = 200;
210 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
211 len = t->length + smbios_string_table_len(t->eos);
212 *current += len;
213 return len;
214}
215
216static int qemu_get_smbios_data(device_t dev, int *handle, unsigned long *current)
217{
218 int len;
219 len = qemu_get_smbios_data16(*handle, current);
220 len += qemu_get_smbios_data17(*handle+1, *handle, current);
221 *handle += 2;
222 return len;
223}
224#endif
Eric Biederman6e53f502004-10-27 08:53:57 +0000225static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000226 .read_resources = cpu_pci_domain_read_resources,
227 .set_resources = cpu_pci_domain_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000228 .enable_resources = NULL,
229 .init = NULL,
Myles Watson032a9652009-05-11 22:24:53 +0000230 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki33e5df32013-07-03 10:51:34 +0300231 .ops_pci_bus = pci_bus_default_ops,
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200232#if CONFIG_GENERATE_SMBIOS_TABLES
233 .get_smbios_data = qemu_get_smbios_data,
234#endif
Myles Watson032a9652009-05-11 22:24:53 +0000235};
Eric Biederman6e53f502004-10-27 08:53:57 +0000236
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200237static void cpu_bus_init(device_t dev)
238{
239 initialize_cpus(dev->link_list);
240}
241
242static unsigned int cpu_bus_scan(device_t bus, unsigned int max)
243{
244 int max_cpus = fw_cfg_max_cpus();
245 device_t cpu;
246 int i;
247
248 if (max_cpus < 0)
249 return 0;
250
251 /*
252 * TODO: This only handles the simple "qemu -smp $nr" case
253 * correctly. qemu also allows to specify the number of
254 * cores, threads & sockets.
255 */
256 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
257 for (i = 0; i < max_cpus; i++) {
258 cpu = add_cpu_device(bus->link_list, i, 1);
259 if (cpu)
260 set_cpu_topology(cpu, 1, 0, i, 0);
261 }
262 return max_cpus;
263}
264
265static void cpu_bus_noop(device_t dev)
266{
267}
268
269static struct device_operations cpu_bus_ops = {
270 .read_resources = cpu_bus_noop,
271 .set_resources = cpu_bus_noop,
272 .enable_resources = cpu_bus_noop,
273 .init = cpu_bus_init,
274 .scan_bus = cpu_bus_scan,
275};
276
Paul Menzel5f20b352013-02-24 14:27:03 +0100277static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000278{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000279 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800280 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000281 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000282 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200283 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
284 dev->ops = &cpu_bus_ops;
285 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000286}
287
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200288struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
289 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100290 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000291};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200292
293struct chip_operations mainboard_emulation_qemu_q35_ops = {
294 CHIP_NAME("QEMU Northbridge q35")
295 .enable_dev = northbridge_enable,
296};