blob: c87868925c12764cd8c6179e89d308b3ccff478b [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"
16
Stefan Reinauer597ff872013-01-07 13:21:22 -080017#include "memory.c"
Sven Schnelle164bcfd2011-08-14 20:56:34 +020018
Gerd Hoffmann9839a382013-06-17 12:26:17 +020019static unsigned long qemu_get_high_memory_size(void)
20{
21 unsigned long high;
22 outb (HIGH_HIGHRAM_ADDR, CMOS_ADDR_PORT);
23 high = ((unsigned long) inb(CMOS_DATA_PORT)) << 22;
24 outb (MID_HIGHRAM_ADDR, CMOS_ADDR_PORT);
25 high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 14;
26 outb (LOW_HIGHRAM_ADDR, CMOS_ADDR_PORT);
27 high |= ((unsigned long) inb(CMOS_DATA_PORT)) << 6;
28 return high;
29}
30
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020031static void qemu_reserve_ports(struct device *dev, unsigned int idx,
32 unsigned int base, unsigned int size,
33 const char *name)
34{
35 unsigned int end = base + size -1;
36 struct resource *res;
37
38 printk(BIOS_DEBUG, "QEMU: reserve ioports 0x%04x-0x%04x [%s]\n",
39 base, end, name);
40 res = new_resource(dev, idx);
41 res->base = base;
42 res->size = size;
43 res->limit = 0xffff;
44 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
45 IORESOURCE_ASSIGNED;
46}
47
Myles Watson29cc9ed2009-07-02 18:56:24 +000048static void cpu_pci_domain_set_resources(device_t dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000049{
Myles Watson894a3472010-06-09 22:41:35 +000050 assign_resources(dev->link_list);
Eric Biederman6e53f502004-10-27 08:53:57 +000051}
Stefan Reinauere2b53e12004-06-28 11:59:45 +000052
Myles Watson29cc9ed2009-07-02 18:56:24 +000053static void cpu_pci_domain_read_resources(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +000054{
Gerd Hoffmanna4e70572013-08-09 10:02:22 +020055 u16 nbid = pci_read_config16(dev_find_slot(0, 0), PCI_DEVICE_ID);
56 int i440fx = (nbid == 0x1237);
Gerd Hoffmannad690f22013-09-17 10:35:43 +020057 int q35 = (nbid == 0x29c0);
Myles Watson29cc9ed2009-07-02 18:56:24 +000058 struct resource *res;
Gerd Hoffmann9839a382013-06-17 12:26:17 +020059 unsigned long tomk = 0, high;
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020060 int idx = 10;
Myles Watson29cc9ed2009-07-02 18:56:24 +000061
62 pci_domain_read_resources(dev);
63
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020064 tomk = qemu_get_memory_size();
Gerd Hoffmann9839a382013-06-17 12:26:17 +020065 high = qemu_get_high_memory_size();
66 printk(BIOS_DEBUG, "Detected %lu MiB RAM below 4G.\n", tomk / 1024);
67 printk(BIOS_DEBUG, "Detected %lu MiB RAM above 4G.\n", high / 1024);
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020068
69 /* Report the memory regions. */
70 idx = 10;
71 ram_resource(dev, idx++, 0, 640);
72 ram_resource(dev, idx++, 768, tomk - 768);
Gerd Hoffmann9839a382013-06-17 12:26:17 +020073 if (high)
74 ram_resource(dev, idx++, 4 * 1024 * 1024, high);
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020075
Gerd Hoffmann05d3f492013-08-06 10:48:41 +020076 /* Reserve I/O ports used by QEMU */
77 qemu_reserve_ports(dev, idx++, 0x0510, 0x02, "firmware-config");
78 qemu_reserve_ports(dev, idx++, 0x5658, 0x01, "vmware-port");
79 if (i440fx) {
80 qemu_reserve_ports(dev, idx++, 0xae00, 0x10, "pci-hotplug");
81 qemu_reserve_ports(dev, idx++, 0xaf00, 0x20, "cpu-hotplug");
82 qemu_reserve_ports(dev, idx++, 0xafe0, 0x04, "piix4-gpe0");
83 }
84 if (inb(CONFIG_CONSOLE_QEMU_DEBUGCON_PORT) == 0xe9) {
85 qemu_reserve_ports(dev, idx++, CONFIG_CONSOLE_QEMU_DEBUGCON_PORT, 1,
86 "debugcon");
87 }
88
Denis 'GNUtoo' Carikli378d0462013-06-19 08:30:33 +020089#if !CONFIG_DYNAMIC_CBMEM
Kyösti Mälkki42f46512013-06-27 08:20:09 +030090 set_top_of_ram(tomk * 1024);
Denis 'GNUtoo' Carikli378d0462013-06-19 08:30:33 +020091#endif
Gerd Hoffmann44b11f22013-06-17 13:30:50 +020092
Gerd Hoffmannad690f22013-09-17 10:35:43 +020093 if (q35 && ((tomk * 1024) < 0xb0000000)) {
94 /*
95 * Reserve the region between top-of-ram and the
96 * mmconf xbar (ar 0xb0000000), so coreboot doesn't
97 * place pci bars there. The region isn't declared as
98 * pci io window in the acpi tables (\_SB.PCI0._CRS).
99 */
100 res = new_resource(dev, idx++);
101 res->base = tomk * 1024;
102 res->size = 0xb0000000 - tomk * 1024;
103 res->limit = 0xffffffff;
104 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
105 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
106 }
107
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200108 if (i440fx) {
109 /* Reserve space for the IOAPIC. This should be in
Patrick Georgi3f34fc42013-08-15 20:41:15 +0200110 * the southbridge, but I couldn't tell which device
Gerd Hoffmanna4e70572013-08-09 10:02:22 +0200111 * to put it in. */
112 res = new_resource(dev, 2);
113 res->base = IO_APIC_ADDR;
114 res->size = 0x100000UL;
115 res->limit = 0xffffffffUL;
116 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
117 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
118 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000119
120 /* Reserve space for the LAPIC. There's one in every processor, but
121 * the space only needs to be reserved once, so we do it here. */
122 res = new_resource(dev, 3);
Patrick Georgic8feedd2012-02-16 18:43:25 +0100123 res->base = LOCAL_APIC_ADDR;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000124 res->size = 0x10000UL;
125 res->limit = 0xffffffffUL;
126 res->flags = IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
127 IORESOURCE_ASSIGNED;
Eric Biederman6e53f502004-10-27 08:53:57 +0000128}
129
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200130#if CONFIG_GENERATE_SMBIOS_TABLES
131static int qemu_get_smbios_data16(int handle, unsigned long *current)
132{
133 struct smbios_type16 *t = (struct smbios_type16 *)*current;
134 int len = sizeof(struct smbios_type16);
135
136 memset(t, 0, sizeof(struct smbios_type16));
137 t->type = SMBIOS_PHYS_MEMORY_ARRAY;
138 t->handle = handle;
139 t->length = len - 2;
140 t->location = 3; /* Location: System Board */
141 t->use = 3; /* System memory */
142 t->memory_error_correction = 3; /* No error correction */
143 t->maximum_capacity = qemu_get_memory_size();
144 *current += len;
145 return len;
146}
147
148static int qemu_get_smbios_data17(int handle, int parent_handle, unsigned long *current)
149{
150 struct smbios_type17 *t = (struct smbios_type17 *)*current;
151 int len;
152
153 memset(t, 0, sizeof(struct smbios_type17));
154 t->type = SMBIOS_MEMORY_DEVICE;
155 t->handle = handle;
156 t->phys_memory_array_handle = parent_handle;
157 t->length = sizeof(struct smbios_type17) - 2;
158 t->size = qemu_get_memory_size() / 1024;
159 t->data_width = 64;
160 t->total_width = 64;
161 t->form_factor = 9; /* DIMM */
162 t->device_locator = smbios_add_string(t->eos, "Virtual");
163 t->memory_type = 0x12; /* DDR */
164 t->type_detail = 0x80; /* Synchronous */
165 t->speed = 200;
166 t->clock_speed = 200;
167 t->manufacturer = smbios_add_string(t->eos, CONFIG_MAINBOARD_VENDOR);
168 len = t->length + smbios_string_table_len(t->eos);
169 *current += len;
170 return len;
171}
172
173static int qemu_get_smbios_data(device_t dev, int *handle, unsigned long *current)
174{
175 int len;
176 len = qemu_get_smbios_data16(*handle, current);
177 len += qemu_get_smbios_data17(*handle+1, *handle, current);
178 *handle += 2;
179 return len;
180}
181#endif
Eric Biederman6e53f502004-10-27 08:53:57 +0000182static struct device_operations pci_domain_ops = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000183 .read_resources = cpu_pci_domain_read_resources,
184 .set_resources = cpu_pci_domain_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000185 .enable_resources = NULL,
186 .init = NULL,
Myles Watson032a9652009-05-11 22:24:53 +0000187 .scan_bus = pci_domain_scan_bus,
Kyösti Mälkki33e5df32013-07-03 10:51:34 +0300188 .ops_pci_bus = pci_bus_default_ops,
Sven Schnelle164bcfd2011-08-14 20:56:34 +0200189#if CONFIG_GENERATE_SMBIOS_TABLES
190 .get_smbios_data = qemu_get_smbios_data,
191#endif
Myles Watson032a9652009-05-11 22:24:53 +0000192};
Eric Biederman6e53f502004-10-27 08:53:57 +0000193
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200194static void cpu_bus_init(device_t dev)
195{
196 initialize_cpus(dev->link_list);
197}
198
199static unsigned int cpu_bus_scan(device_t bus, unsigned int max)
200{
201 int max_cpus = fw_cfg_max_cpus();
202 device_t cpu;
203 int i;
204
205 if (max_cpus < 0)
206 return 0;
207
208 /*
209 * TODO: This only handles the simple "qemu -smp $nr" case
210 * correctly. qemu also allows to specify the number of
211 * cores, threads & sockets.
212 */
213 printk(BIOS_INFO, "QEMU: max_cpus is %d\n", max_cpus);
214 for (i = 0; i < max_cpus; i++) {
215 cpu = add_cpu_device(bus->link_list, i, 1);
216 if (cpu)
217 set_cpu_topology(cpu, 1, 0, i, 0);
218 }
219 return max_cpus;
220}
221
222static void cpu_bus_noop(device_t dev)
223{
224}
225
226static struct device_operations cpu_bus_ops = {
227 .read_resources = cpu_bus_noop,
228 .set_resources = cpu_bus_noop,
229 .enable_resources = cpu_bus_noop,
230 .init = cpu_bus_init,
231 .scan_bus = cpu_bus_scan,
232};
233
Paul Menzel5f20b352013-02-24 14:27:03 +0100234static void northbridge_enable(struct device *dev)
Eric Biederman6e53f502004-10-27 08:53:57 +0000235{
Eric Biederman018d8dd2004-11-04 11:04:33 +0000236 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800237 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman018d8dd2004-11-04 11:04:33 +0000238 dev->ops = &pci_domain_ops;
Eric Biederman018d8dd2004-11-04 11:04:33 +0000239 }
Gerd Hoffmannaa588e02013-05-31 09:26:55 +0200240 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
241 dev->ops = &cpu_bus_ops;
242 }
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000243}
244
Gerd Hoffmann00cc7f432013-06-07 15:46:23 +0200245struct chip_operations mainboard_emulation_qemu_i440fx_ops = {
246 CHIP_NAME("QEMU Northbridge i440fx")
Paul Menzel5f20b352013-02-24 14:27:03 +0100247 .enable_dev = northbridge_enable,
Stefan Reinauere2b53e12004-06-28 11:59:45 +0000248};
Gerd Hoffmannee941b382013-06-07 16:03:44 +0200249
250struct chip_operations mainboard_emulation_qemu_q35_ops = {
251 CHIP_NAME("QEMU Northbridge q35")
252 .enable_dev = northbridge_enable,
253};