blob: 63a03adc5d5d82531ae65701215dfa87284414fd [file] [log] [blame]
Stefan Reinauer278534d2008-10-29 04:51:07 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer43b29cf2009-03-06 19:11:52 +00004 * Copyright (C) 2007-2009 coresystems GmbH
Stefan Reinauer278534d2008-10-29 04:51:07 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <console/console.h>
21#include <arch/io.h>
22#include <stdint.h>
23#include <device/device.h>
24#include <device/pci.h>
25#include <device/pci_ids.h>
26#include <device/hypertransport.h>
27#include <stdlib.h>
28#include <string.h>
Stefan Reinauer278534d2008-10-29 04:51:07 +000029#include <cpu/cpu.h>
Stefan Reinauerab872542011-10-14 15:18:29 -070030#include <arch/acpi.h>
Stefan Reinauer278534d2008-10-29 04:51:07 +000031#include "i945.h"
32
Stefan Reinauerde3206a2010-02-22 06:09:43 +000033static int get_pcie_bar(u32 *base, u32 *len)
Stefan Reinauer71a3d962009-07-21 21:44:24 +000034{
35 device_t dev;
36 u32 pciexbar_reg;
37
38 *base = 0;
39 *len = 0;
40
41 dev = dev_find_slot(0, PCI_DEVFN(0, 0));
42 if (!dev)
43 return 0;
Stefan Reinauer109ab312009-08-12 16:08:05 +000044
Stefan Reinaueraca6ec62009-10-26 17:12:21 +000045 pciexbar_reg = pci_read_config32(dev, PCIEXBAR);
Stefan Reinauer71a3d962009-07-21 21:44:24 +000046
47 if (!(pciexbar_reg & (1 << 0)))
48 return 0;
49
50 switch ((pciexbar_reg >> 1) & 3) {
51 case 0: // 256MB
52 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28));
53 *len = 256 * 1024 * 1024;
54 return 1;
55 case 1: // 128M
56 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27));
57 *len = 128 * 1024 * 1024;
58 return 1;
59 case 2: // 64M
60 *base = pciexbar_reg & ((1 << 31)|(1 << 30)|(1 << 29)|(1 << 28)|(1 << 27)|(1 << 26));
61 *len = 64 * 1024 * 1024;
62 return 1;
63 }
64
65 return 0;
66}
67
Myles Watson25d12132010-09-13 13:14:48 +000068static void add_fixed_resources(struct device *dev, int index)
Stefan Reinauer71a3d962009-07-21 21:44:24 +000069{
Myles Watson25d12132010-09-13 13:14:48 +000070 struct resource *resource;
Stefan Reinauer71a3d962009-07-21 21:44:24 +000071 u32 pcie_config_base, pcie_config_size;
72
Myles Watson25d12132010-09-13 13:14:48 +000073 if (get_pcie_bar(&pcie_config_base, &pcie_config_size)) {
74 printk(BIOS_DEBUG, "Adding PCIe config bar\n");
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +030075 resource = new_resource(dev, index++);
Myles Watson25d12132010-09-13 13:14:48 +000076 resource->base = (resource_t) pcie_config_base;
77 resource->size = (resource_t) pcie_config_size;
78 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
79 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
80 }
Stefan Reinauer71a3d962009-07-21 21:44:24 +000081}
82
Patrick Georgie1667822012-05-05 15:29:32 +020083#if CONFIG_WRITE_HIGH_TABLES
Rudolf Marek97be27e2010-12-13 19:50:25 +000084#include <cbmem.h>
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +000085#endif
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +000086
Stefan Reinauer278534d2008-10-29 04:51:07 +000087static void pci_domain_set_resources(device_t dev)
88{
89 uint32_t pci_tolm;
90 uint8_t tolud, reg8;
91 uint16_t reg16;
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +030092 unsigned long long tomk, tomk_stolen;
93 uint64_t tseg_memory_base = 0, tseg_memory_size = 0;
Stefan Reinauer278534d2008-10-29 04:51:07 +000094
Stefan Reinauer71a3d962009-07-21 21:44:24 +000095 /* Can we find out how much memory we can use at most
96 * this way?
97 */
Myles Watson894a3472010-06-09 22:41:35 +000098 pci_tolm = find_pci_tolm(dev->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000099 printk(BIOS_DEBUG, "pci_tolm: 0x%x\n", pci_tolm);
Stefan Reinauer278534d2008-10-29 04:51:07 +0000100
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000101 printk(BIOS_SPEW, "Base of stolen memory: 0x%08x\n",
Stefan Reinauer278534d2008-10-29 04:51:07 +0000102 pci_read_config32(dev_find_slot(0, PCI_DEVFN(2, 0)), 0x5c));
103
104 tolud = pci_read_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), 0x9c);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000105 printk(BIOS_SPEW, "Top of Low Used DRAM: 0x%08x\n", tolud << 24);
Stefan Reinauer278534d2008-10-29 04:51:07 +0000106
107 tomk = tolud << 14;
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300108 tomk_stolen = tomk;
Stefan Reinauer278534d2008-10-29 04:51:07 +0000109
110 /* Note: subtract IGD device and TSEG */
111 reg8 = pci_read_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), 0x9e);
112 if (reg8 & 1) {
113 int tseg_size = 0;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000114 printk(BIOS_DEBUG, "TSEG decoded, subtracting ");
Stefan Reinauer278534d2008-10-29 04:51:07 +0000115 reg8 >>= 1;
116 reg8 &= 3;
117 switch (reg8) {
118 case 0:
119 tseg_size = 1024;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000120 break; /* TSEG = 1M */
Stefan Reinauer278534d2008-10-29 04:51:07 +0000121 case 1:
122 tseg_size = 2048;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000123 break; /* TSEG = 2M */
Stefan Reinauer278534d2008-10-29 04:51:07 +0000124 case 2:
125 tseg_size = 8192;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000126 break; /* TSEG = 8M */
Stefan Reinauer278534d2008-10-29 04:51:07 +0000127 }
128
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000129 printk(BIOS_DEBUG, "%dM\n", tseg_size >> 10);
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300130 tomk_stolen -= tseg_size;
131
132 /* For reserving TSEG memory in the memory map */
133 tseg_memory_base = tomk_stolen * 1024ULL;
134 tseg_memory_size = tseg_size * 1024ULL;
Stefan Reinauer278534d2008-10-29 04:51:07 +0000135 }
136
137 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
138 if (!(reg16 & 2)) {
139 int uma_size = 0;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000140 printk(BIOS_DEBUG, "IGD decoded, subtracting ");
Stefan Reinauer278534d2008-10-29 04:51:07 +0000141 reg16 >>= 4;
142 reg16 &= 7;
143 switch (reg16) {
144 case 1:
145 uma_size = 1024;
146 break;
147 case 3:
148 uma_size = 8192;
149 break;
150 }
151
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000152 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300153 tomk_stolen -= uma_size;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000154
155 /* For reserving UMA memory in the memory map */
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300156 uma_memory_base = tomk_stolen * 1024ULL;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000157 uma_memory_size = uma_size * 1024ULL;
Stefan Reinauer278534d2008-10-29 04:51:07 +0000158 }
159
160 /* The following needs to be 2 lines, otherwise the second
161 * number is always 0
162 */
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300163 printk(BIOS_INFO, "Available memory: %dK", (uint32_t)tomk_stolen);
164 printk(BIOS_INFO, " (%dM)\n", (uint32_t)(tomk_stolen >> 10));
Stefan Reinauer278534d2008-10-29 04:51:07 +0000165
166 /* Report the memory regions */
167 ram_resource(dev, 3, 0, 640);
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000168 ram_resource(dev, 4, 768, (tomk - 768));
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300169 uma_resource(dev, 5, uma_memory_base >> 10, uma_memory_size >> 10);
170 mmio_resource(dev, 6, tseg_memory_base >> 10, tseg_memory_size >> 10);
171
172 add_fixed_resources(dev, 7);
Myles Watson25d12132010-09-13 13:14:48 +0000173
Myles Watson894a3472010-06-09 22:41:35 +0000174 assign_resources(dev->link_list);
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000175
Patrick Georgie1667822012-05-05 15:29:32 +0200176#if CONFIG_WRITE_HIGH_TABLES
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000177 /* Leave some space for ACPI, PIRQ and MP tables */
Kyösti Mälkki6ff1d362012-07-27 08:42:20 +0300178 high_tables_base = (tomk_stolen * 1024) - HIGH_MEMORY_SIZE;
Rudolf Marek97be27e2010-12-13 19:50:25 +0000179 high_tables_size = HIGH_MEMORY_SIZE;
Stefan Reinauer3c7f46b2009-02-27 23:09:55 +0000180#endif
Stefan Reinauer278534d2008-10-29 04:51:07 +0000181}
182
Stefan Reinauer278534d2008-10-29 04:51:07 +0000183 /* TODO We could determine how many PCIe busses we need in
184 * the bar. For now that number is hardcoded to a max of 64.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000185 * See e7525/northbridge.c for an example.
Stefan Reinauer278534d2008-10-29 04:51:07 +0000186 */
Stefan Reinauer278534d2008-10-29 04:51:07 +0000187static struct device_operations pci_domain_ops = {
188 .read_resources = pci_domain_read_resources,
189 .set_resources = pci_domain_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000190 .enable_resources = NULL,
191 .init = NULL,
Stefan Reinauer278534d2008-10-29 04:51:07 +0000192 .scan_bus = pci_domain_scan_bus,
Stefan Reinauer08670622009-06-30 15:17:49 +0000193#if CONFIG_MMCONF_SUPPORT_DEFAULT
Stefan Reinauer43b29cf2009-03-06 19:11:52 +0000194 .ops_pci_bus = &pci_ops_mmconf,
195#else
196 .ops_pci_bus = &pci_cf8_conf1,
197#endif
Stefan Reinauer278534d2008-10-29 04:51:07 +0000198};
199
200static void mc_read_resources(device_t dev)
201{
202 struct resource *resource;
203
204 pci_dev_read_resources(dev);
205
206 /* So, this is one of the big mysteries in the coreboot resource
207 * allocator. This resource should make sure that the address space
208 * of the PCIe memory mapped config space bar. But it does not.
209 */
210
211 /* We use 0xcf as an unused index for our PCIe bar so that we find it again */
212 resource = new_resource(dev, 0xcf);
213 resource->base = DEFAULT_PCIEXBAR;
214 resource->size = 64 * 1024 * 1024; /* 64MB hard coded PCIe config space */
215 resource->flags =
216 IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
217 IORESOURCE_ASSIGNED;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000218 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
Stefan Reinauer30140a52009-03-11 16:20:39 +0000219 (unsigned long)(resource->base), (unsigned long)(resource->base + resource->size));
Stefan Reinauer278534d2008-10-29 04:51:07 +0000220}
221
222static void mc_set_resources(device_t dev)
223{
Stefan Reinauer71a3d962009-07-21 21:44:24 +0000224 struct resource *resource;
Stefan Reinauer278534d2008-10-29 04:51:07 +0000225
226 /* Report the PCIe BAR */
Stefan Reinauer278534d2008-10-29 04:51:07 +0000227 resource = find_resource(dev, 0xcf);
228 if (resource) {
229 report_resource_stored(dev, resource, "<mmconfig>");
230 }
231
232 /* And call the normal set_resources */
233 pci_dev_set_resources(dev);
234}
235
236static void intel_set_subsystem(device_t dev, unsigned vendor, unsigned device)
237{
Stefan Reinauer30140a52009-03-11 16:20:39 +0000238 if (!vendor || !device) {
239 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
240 pci_read_config32(dev, PCI_VENDOR_ID));
241 } else {
242 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
243 ((device & 0xffff) << 16) | (vendor & 0xffff));
244 }
Stefan Reinauer278534d2008-10-29 04:51:07 +0000245}
246
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000247#if CONFIG_HAVE_ACPI_RESUME
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000248static void northbridge_init(struct device *dev)
249{
250 switch (pci_read_config32(dev, SKPAD)) {
Sven Schnelled8c68a92011-06-15 09:26:34 +0200251 case SKPAD_NORMAL_BOOT_MAGIC:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000252 printk(BIOS_DEBUG, "Normal boot.\n");
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000253 acpi_slp_type=0;
254 break;
Sven Schnelled8c68a92011-06-15 09:26:34 +0200255 case SKPAD_ACPI_S3_MAGIC:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000256 printk(BIOS_DEBUG, "S3 Resume.\n");
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000257 acpi_slp_type=3;
258 break;
259 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000260 printk(BIOS_DEBUG, "Unknown boot method, assuming normal.\n");
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000261 acpi_slp_type=0;
262 break;
263 }
264}
265#endif
266
Stefan Reinauer278534d2008-10-29 04:51:07 +0000267static struct pci_operations intel_pci_ops = {
268 .set_subsystem = intel_set_subsystem,
269};
270
271static struct device_operations mc_ops = {
272 .read_resources = mc_read_resources,
273 .set_resources = mc_set_resources,
274 .enable_resources = pci_dev_enable_resources,
Stefan Reinaueraca6ec62009-10-26 17:12:21 +0000275#if CONFIG_HAVE_ACPI_RESUME
276 .init = northbridge_init,
277#endif
Stefan Reinauer278534d2008-10-29 04:51:07 +0000278 .scan_bus = 0,
279 .ops_pci = &intel_pci_ops,
280};
281
282static const struct pci_driver mc_driver __pci_driver = {
283 .ops = &mc_ops,
284 .vendor = PCI_VENDOR_ID_INTEL,
Uwe Hermann5d7a1c82008-10-31 18:41:09 +0000285 .device = 0x27a0,
Stefan Reinauer278534d2008-10-29 04:51:07 +0000286};
287
288static void cpu_bus_init(device_t dev)
289{
Myles Watson894a3472010-06-09 22:41:35 +0000290 initialize_cpus(dev->link_list);
Stefan Reinauer278534d2008-10-29 04:51:07 +0000291}
292
293static void cpu_bus_noop(device_t dev)
294{
295}
296
297static struct device_operations cpu_bus_ops = {
298 .read_resources = cpu_bus_noop,
299 .set_resources = cpu_bus_noop,
300 .enable_resources = cpu_bus_noop,
301 .init = cpu_bus_init,
302 .scan_bus = 0,
303};
304
305static void enable_dev(device_t dev)
306{
307 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800308 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Stefan Reinauer278534d2008-10-29 04:51:07 +0000309 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800310 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
Stefan Reinauer278534d2008-10-29 04:51:07 +0000311 dev->ops = &cpu_bus_ops;
312 }
313}
314
315struct chip_operations northbridge_intel_i945_ops = {
316 CHIP_NAME("Intel i945 Northbridge")
317 .enable_dev = enable_dev,
318};