blob: ea21c6952a62c497f9d3f5968d871ccc74b5d581 [file] [log] [blame]
Frank Vibrans39fca802011-02-14 18:35:15 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Advanced Micro Devices, Inc.
5 *
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.
Frank Vibrans39fca802011-02-14 18:35:15 +000014 */
15
16#include <console/console.h>
17#include <arch/io.h>
Kyösti Mälkki8ae16a42014-06-19 20:44:34 +030018#include <arch/acpi.h>
Kyösti Mälkki68a83df2014-11-26 09:51:14 +020019#include <arch/acpigen.h>
Frank Vibrans39fca802011-02-14 18:35:15 +000020#include <stdint.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include <device/hypertransport.h>
25#include <stdlib.h>
26#include <string.h>
Ronald G. Minnich5079a0d2012-11-27 11:32:38 -080027#include <lib.h>
Frank Vibrans39fca802011-02-14 18:35:15 +000028#include <cpu/cpu.h>
Marc Jones5750ed22012-03-15 13:21:41 -060029#include <cbmem.h>
Frank Vibrans39fca802011-02-14 18:35:15 +000030
31#include <cpu/x86/lapic.h>
Kyösti Mälkki55fff9302012-07-11 08:02:39 +030032#include <cpu/amd/mtrr.h>
Frank Vibrans39fca802011-02-14 18:35:15 +000033
Kyösti Mälkki28c4d2f2016-11-25 11:21:02 +020034#include <northbridge/amd/agesa/state_machine.h>
Kyösti Mälkkid610c582017-03-05 06:28:18 +020035#include <northbridge/amd/agesa/agesa_helper.h>
36
Kerry Shefeed3292011-08-18 18:03:44 +080037#include <sb_cimx.h>
Frank Vibrans39fca802011-02-14 18:35:15 +000038
Frank Vibrans39fca802011-02-14 18:35:15 +000039#define FX_DEVS 1
40
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030041static struct device *__f0_dev[FX_DEVS];
42static struct device *__f1_dev[FX_DEVS];
43static struct device *__f2_dev[FX_DEVS];
44static struct device *__f4_dev[FX_DEVS];
Marc Jones8d595692012-03-15 12:55:26 -060045static unsigned fx_devs = 0;
Frank Vibrans39fca802011-02-14 18:35:15 +000046
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +030047static struct device *get_node_pci(u32 nodeid, u32 fn)
Frank Vibrans39fca802011-02-14 18:35:15 +000048{
Kyösti Mälkki113f6702018-05-20 20:12:32 +030049 return dev_find_slot(CONFIG_CBB, PCI_DEVFN(CONFIG_CDB + nodeid, fn));
Frank Vibrans39fca802011-02-14 18:35:15 +000050}
51
Frank Vibrans39fca802011-02-14 18:35:15 +000052static void get_fx_devs(void)
53{
Marc Jones8d595692012-03-15 12:55:26 -060054 int i;
55 for (i = 0; i < FX_DEVS; i++) {
56 __f0_dev[i] = get_node_pci(i, 0);
57 __f1_dev[i] = get_node_pci(i, 1);
58 __f2_dev[i] = get_node_pci(i, 2);
59 __f4_dev[i] = get_node_pci(i, 4);
60 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
61 fx_devs = i + 1;
62 }
63 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
64 die("Cannot find 0:0x18.[0|1]\n");
65 }
Frank Vibrans39fca802011-02-14 18:35:15 +000066}
67
Frank Vibrans39fca802011-02-14 18:35:15 +000068static u32 f1_read_config32(unsigned reg)
69{
Marc Jones8d595692012-03-15 12:55:26 -060070 if (fx_devs == 0)
71 get_fx_devs();
72 return pci_read_config32(__f1_dev[0], reg);
Frank Vibrans39fca802011-02-14 18:35:15 +000073}
74
Frank Vibrans39fca802011-02-14 18:35:15 +000075static void f1_write_config32(unsigned reg, u32 value)
76{
Marc Jones8d595692012-03-15 12:55:26 -060077 int i;
78 if (fx_devs == 0)
79 get_fx_devs();
80 for (i = 0; i < fx_devs; i++) {
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +020081 struct device *dev;
Marc Jones8d595692012-03-15 12:55:26 -060082 dev = __f1_dev[i];
83 if (dev && dev->enabled) {
84 pci_write_config32(dev, reg, value);
85 }
86 }
Frank Vibrans39fca802011-02-14 18:35:15 +000087}
88
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +020089static u32 amdfam14_nodeid(struct device *dev)
Frank Vibrans39fca802011-02-14 18:35:15 +000090{
Marc Jones8d595692012-03-15 12:55:26 -060091 return (dev->path.pci.devfn >> 3) - CONFIG_CDB;
Frank Vibrans39fca802011-02-14 18:35:15 +000092}
93
Frank Vibrans39fca802011-02-14 18:35:15 +000094#include "amdfam14_conf.c"
95
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +020096static void northbridge_init(struct device *dev)
Frank Vibrans39fca802011-02-14 18:35:15 +000097{
Marc Jones8d595692012-03-15 12:55:26 -060098 printk(BIOS_DEBUG, "Northbridge init\n");
Frank Vibrans39fca802011-02-14 18:35:15 +000099}
100
Frank Vibrans39fca802011-02-14 18:35:15 +0000101static void set_vga_enable_reg(u32 nodeid, u32 linkn)
102{
Marc Jones8d595692012-03-15 12:55:26 -0600103 u32 val;
Frank Vibrans39fca802011-02-14 18:35:15 +0000104
Marc Jones8d595692012-03-15 12:55:26 -0600105 val = 1 | (nodeid << 4) | (linkn << 12);
106 /* it will routing (1)mmio 0xa0000:0xbffff (2) io 0x3b0:0x3bb,
107 0x3c0:0x3df */
108 f1_write_config32(0xf4, val);
Frank Vibrans39fca802011-02-14 18:35:15 +0000109
110}
111
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200112static int reg_useable(unsigned reg, struct device *goal_dev,
113 unsigned goal_nodeid, unsigned goal_link)
Frank Vibrans39fca802011-02-14 18:35:15 +0000114{
Marc Jones8d595692012-03-15 12:55:26 -0600115 struct resource *res;
116 unsigned nodeid, link = 0;
117 int result;
118 res = 0;
119 for (nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200120 struct device *dev;
Marc Jones8d595692012-03-15 12:55:26 -0600121 dev = __f0_dev[nodeid];
122 if (!dev)
123 continue;
124 for (link = 0; !res && (link < 8); link++) {
125 res = probe_resource(dev, IOINDEX(0x1000 + reg, link));
126 }
127 }
128 result = 2;
129 if (res) {
130 result = 0;
131 if ((goal_link == (link - 1)) &&
132 (goal_nodeid == (nodeid - 1)) && (res->flags <= 1)) {
133 result = 1;
134 }
135 }
136 return result;
Frank Vibrans39fca802011-02-14 18:35:15 +0000137}
138
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200139static struct resource *amdfam14_find_iopair(struct device *dev,
140 unsigned nodeid, unsigned link)
Frank Vibrans39fca802011-02-14 18:35:15 +0000141{
Marc Jones8d595692012-03-15 12:55:26 -0600142 struct resource *resource;
143 u32 result, reg;
144 resource = 0;
145 reg = 0;
146 result = reg_useable(0xc0, dev, nodeid, link);
147 if (result >= 1) {
148 /* I have been allocated this one */
149 reg = 0xc0;
150 }
151 /* Ext conf space */
152 if (!reg) {
153 /* Because of Extend conf space, we will never run out of reg,
154 * but we need one index to differ them. So ,same node and same
155 * link can have multi range
156 */
157 u32 index = get_io_addr_index(nodeid, link);
158 reg = 0x110 + (index << 24) + (4 << 20); // index could be 0, 255
159 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000160
Marc Jones8d595692012-03-15 12:55:26 -0600161 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
Frank Vibrans39fca802011-02-14 18:35:15 +0000162
Marc Jones8d595692012-03-15 12:55:26 -0600163 return resource;
Frank Vibrans39fca802011-02-14 18:35:15 +0000164}
165
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200166static struct resource *amdfam14_find_mempair(struct device *dev, u32 nodeid,
Marc Jones8d595692012-03-15 12:55:26 -0600167 u32 link)
Frank Vibrans39fca802011-02-14 18:35:15 +0000168{
Marc Jones8d595692012-03-15 12:55:26 -0600169 struct resource *resource;
170 u32 free_reg, reg;
171 resource = 0;
172 free_reg = 0;
173 for (reg = 0x80; reg <= 0xb8; reg += 0x8) {
174 int result;
175 result = reg_useable(reg, dev, nodeid, link);
176 if (result == 1) {
177 /* I have been allocated this one */
178 break;
179 } else if (result > 1) {
180 /* I have a free register pair */
181 free_reg = reg;
182 }
183 }
184 if (reg > 0xb8) {
185 reg = free_reg;
186 }
187 /* Ext conf space */
188 if (!reg) {
189 /* Because of Extend conf space, we will never run out of reg,
190 * but we need one index to differ them. So ,same node and same
191 * link can have multi range
192 */
193 u32 index = get_mmio_addr_index(nodeid, link);
194 reg = 0x110 + (index << 24) + (6 << 20); // index could be 0, 63
Frank Vibrans39fca802011-02-14 18:35:15 +0000195
Marc Jones8d595692012-03-15 12:55:26 -0600196 }
197 resource = new_resource(dev, IOINDEX(0x1000 + reg, link));
198 return resource;
Frank Vibrans39fca802011-02-14 18:35:15 +0000199}
200
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200201static void amdfam14_link_read_bases(struct device *dev, u32 nodeid, u32 link)
Frank Vibrans39fca802011-02-14 18:35:15 +0000202{
Marc Jones8d595692012-03-15 12:55:26 -0600203 struct resource *resource;
Frank Vibrans39fca802011-02-14 18:35:15 +0000204
Marc Jones8d595692012-03-15 12:55:26 -0600205 /* Initialize the io space constraints on the current bus */
206 resource = amdfam14_find_iopair(dev, nodeid, link);
207 if (resource) {
208 u32 align;
Kyösti Mälkkiac7402d2014-12-14 08:30:17 +0200209 align = log2(HT_IO_HOST_ALIGN);
Marc Jones8d595692012-03-15 12:55:26 -0600210 resource->base = 0;
211 resource->size = 0;
212 resource->align = align;
213 resource->gran = align;
214 resource->limit = 0xffffUL;
215 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
216 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000217
Marc Jones8d595692012-03-15 12:55:26 -0600218 /* Initialize the prefetchable memory constraints on the current bus */
219 resource = amdfam14_find_mempair(dev, nodeid, link);
220 if (resource) {
221 resource->base = 0;
222 resource->size = 0;
223 resource->align = log2(HT_MEM_HOST_ALIGN);
224 resource->gran = log2(HT_MEM_HOST_ALIGN);
225 resource->limit = 0xffffffffffULL;
226 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
227 resource->flags |= IORESOURCE_BRIDGE;
Marc Jones8d595692012-03-15 12:55:26 -0600228 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000229
Marc Jones8d595692012-03-15 12:55:26 -0600230 /* Initialize the memory constraints on the current bus */
231 resource = amdfam14_find_mempair(dev, nodeid, link);
232 if (resource) {
233 resource->base = 0;
234 resource->size = 0;
235 resource->align = log2(HT_MEM_HOST_ALIGN);
236 resource->gran = log2(HT_MEM_HOST_ALIGN);
237 resource->limit = 0xffffffffffULL;
238 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
Marc Jones8d595692012-03-15 12:55:26 -0600239 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000240}
241
242static u32 my_find_pci_tolm(struct bus *bus, u32 tolm)
243{
Marc Jones8d595692012-03-15 12:55:26 -0600244 struct resource *min;
245 min = 0;
246 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test,
247 &min);
248 if (min && tolm > min->base) {
249 tolm = min->base;
250 }
251 return tolm;
Frank Vibrans39fca802011-02-14 18:35:15 +0000252}
253
254#if CONFIG_HW_MEM_HOLE_SIZEK != 0
255
256struct hw_mem_hole_info {
Marc Jones8d595692012-03-15 12:55:26 -0600257 unsigned hole_startk;
258 int node_id;
Frank Vibrans39fca802011-02-14 18:35:15 +0000259};
260
261static struct hw_mem_hole_info get_hw_mem_hole_info(void)
262{
Marc Jones8d595692012-03-15 12:55:26 -0600263 struct hw_mem_hole_info mem_hole;
Frank Vibrans39fca802011-02-14 18:35:15 +0000264
Marc Jones8d595692012-03-15 12:55:26 -0600265 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
266 mem_hole.node_id = -1;
Frank Vibrans39fca802011-02-14 18:35:15 +0000267
Marc Jones8d595692012-03-15 12:55:26 -0600268 struct dram_base_mask_t d;
269 u32 hole;
270 d = get_dram_base_mask(0);
271 if (d.mask & 1) {
272 hole = pci_read_config32(__f1_dev[0], 0xf0);
273 if (hole & 1) { // we find the hole
274 mem_hole.hole_startk = (hole & (0xff << 24)) >> 10;
275 mem_hole.node_id = 0; // record the node No with hole
276 }
277 }
Marc Jones8d595692012-03-15 12:55:26 -0600278 return mem_hole;
Frank Vibrans39fca802011-02-14 18:35:15 +0000279}
280#endif
281
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200282static void nb_read_resources(struct device *dev)
Frank Vibrans39fca802011-02-14 18:35:15 +0000283{
Marc Jones8d595692012-03-15 12:55:26 -0600284 u32 nodeid;
285 struct bus *link;
Frank Vibrans39fca802011-02-14 18:35:15 +0000286
Mike Loptien58089e82013-01-29 15:45:09 -0700287 printk(BIOS_DEBUG, "\nFam14h - %s\n", __func__);
Frank Vibrans39fca802011-02-14 18:35:15 +0000288
Marc Jones8d595692012-03-15 12:55:26 -0600289 nodeid = amdfam14_nodeid(dev);
290 for (link = dev->link_list; link; link = link->next) {
291 if (link->children) {
292 amdfam14_link_read_bases(dev, nodeid, link->link_num);
293 }
294 }
Marc Jonesd5c998b2013-01-16 17:14:24 -0700295
296 /*
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800297 * This MMCONF resource must be reserved in the PCI domain.
Marc Jonesd5c998b2013-01-16 17:14:24 -0700298 * It is not honored by the coreboot resource allocator if it is in
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800299 * the CPU_CLUSTER.
Marc Jonesd5c998b2013-01-16 17:14:24 -0700300 */
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200301 mmconf_resource(dev, 0xc0010058);
Frank Vibrans39fca802011-02-14 18:35:15 +0000302}
303
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200304static void set_resource(struct device *dev, struct resource *resource,
305 u32 nodeid)
Frank Vibrans39fca802011-02-14 18:35:15 +0000306{
Marc Jones8d595692012-03-15 12:55:26 -0600307 resource_t rbase, rend;
308 unsigned reg, link_num;
309 char buf[50];
Frank Vibrans39fca802011-02-14 18:35:15 +0000310
Mike Loptien58089e82013-01-29 15:45:09 -0700311 printk(BIOS_DEBUG, "\nFam14h - %s\n", __func__);
Frank Vibrans39fca802011-02-14 18:35:15 +0000312
Marc Jones8d595692012-03-15 12:55:26 -0600313 /* Make certain the resource has actually been set */
314 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
315 return;
316 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000317
Marc Jones8d595692012-03-15 12:55:26 -0600318 /* If I have already stored this resource don't worry about it */
319 if (resource->flags & IORESOURCE_STORED) {
320 return;
321 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000322
Marc Jones8d595692012-03-15 12:55:26 -0600323 /* Only handle PCI memory and IO resources */
324 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
325 return;
Frank Vibrans39fca802011-02-14 18:35:15 +0000326
Marc Jones8d595692012-03-15 12:55:26 -0600327 /* Ensure I am actually looking at a resource of function 1 */
328 if ((resource->index & 0xffff) < 0x1000) {
329 return;
330 }
331 /* Get the base address */
332 rbase = resource->base;
Frank Vibrans39fca802011-02-14 18:35:15 +0000333
Marc Jones8d595692012-03-15 12:55:26 -0600334 /* Get the limit (rounded up) */
335 rend = resource_end(resource);
Frank Vibrans39fca802011-02-14 18:35:15 +0000336
Marc Jones8d595692012-03-15 12:55:26 -0600337 /* Get the register and link */
338 reg = resource->index & 0xfff; // 4k
339 link_num = IOINDEX_LINK(resource->index);
Frank Vibrans39fca802011-02-14 18:35:15 +0000340
Marc Jones8d595692012-03-15 12:55:26 -0600341 if (resource->flags & IORESOURCE_IO) {
342 set_io_addr_reg(dev, nodeid, link_num, reg, rbase >> 8,
343 rend >> 8);
344 } else if (resource->flags & IORESOURCE_MEM) {
345 set_mmio_addr_reg(nodeid, link_num, reg, (resource->index >> 24),
346 rbase >> 8, rend >> 8, 1); // [39:8]
347 }
348 resource->flags |= IORESOURCE_STORED;
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200349 snprintf(buf, sizeof(buf), " <node %x link %x>", nodeid, link_num);
Marc Jones8d595692012-03-15 12:55:26 -0600350 report_resource_stored(dev, resource, buf);
Frank Vibrans39fca802011-02-14 18:35:15 +0000351}
352
Martin Roth77a58b92017-06-24 14:45:48 -0600353#if IS_ENABLED(CONFIG_CONSOLE_VGA_MULTI)
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300354extern struct device *vga_pri; // the primary vga device, defined in device.c
Frank Vibrans39fca802011-02-14 18:35:15 +0000355#endif
356
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200357static void create_vga_resource(struct device *dev, unsigned nodeid)
Frank Vibrans39fca802011-02-14 18:35:15 +0000358{
Marc Jones8d595692012-03-15 12:55:26 -0600359 struct bus *link;
Frank Vibrans39fca802011-02-14 18:35:15 +0000360
Mike Loptien58089e82013-01-29 15:45:09 -0700361 printk(BIOS_DEBUG, "\nFam14h - %s\n", __func__);
Frank Vibrans39fca802011-02-14 18:35:15 +0000362
Marc Jones8d595692012-03-15 12:55:26 -0600363 /* find out which link the VGA card is connected,
364 * we only deal with the 'first' vga card */
365 for (link = dev->link_list; link; link = link->next) {
366 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Martin Roth77a58b92017-06-24 14:45:48 -0600367#if IS_ENABLED(CONFIG_CONSOLE_VGA_MULTI)
Marc Jones8d595692012-03-15 12:55:26 -0600368 printk(BIOS_DEBUG,
369 "VGA: vga_pri bus num = %d bus range [%d,%d]\n",
370 vga_pri->bus->secondary, link->secondary,
371 link->subordinate);
372 /* We need to make sure the vga_pri is under the link */
373 if ((vga_pri->bus->secondary >= link->secondary) &&
374 (vga_pri->bus->secondary <= link->subordinate))
Frank Vibrans39fca802011-02-14 18:35:15 +0000375#endif
Marc Jones8d595692012-03-15 12:55:26 -0600376 break;
377 }
378 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000379
Marc Jones8d595692012-03-15 12:55:26 -0600380 /* no VGA card installed */
381 if (link == NULL)
382 return;
Frank Vibrans39fca802011-02-14 18:35:15 +0000383
Marc Jones8d595692012-03-15 12:55:26 -0600384 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n",
385 dev_path(dev), nodeid, link->link_num);
386 set_vga_enable_reg(nodeid, link->link_num);
Frank Vibrans39fca802011-02-14 18:35:15 +0000387}
388
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200389static void nb_set_resources(struct device *dev)
Frank Vibrans39fca802011-02-14 18:35:15 +0000390{
Marc Jones8d595692012-03-15 12:55:26 -0600391 unsigned nodeid;
392 struct bus *bus;
393 struct resource *res;
Frank Vibrans39fca802011-02-14 18:35:15 +0000394
Mike Loptien58089e82013-01-29 15:45:09 -0700395 printk(BIOS_DEBUG, "\nFam14h - %s\n", __func__);
efdesign9805a89ab2011-06-20 17:38:49 -0700396
Marc Jones8d595692012-03-15 12:55:26 -0600397 /* Find the nodeid */
398 nodeid = amdfam14_nodeid(dev);
Frank Vibrans39fca802011-02-14 18:35:15 +0000399
Marc Jones8d595692012-03-15 12:55:26 -0600400 create_vga_resource(dev, nodeid);
Frank Vibrans39fca802011-02-14 18:35:15 +0000401
Marc Jones8d595692012-03-15 12:55:26 -0600402 /* Set each resource we have found */
403 for (res = dev->resource_list; res; res = res->next) {
404 set_resource(dev, res, nodeid);
405 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000406
Marc Jones8d595692012-03-15 12:55:26 -0600407 for (bus = dev->link_list; bus; bus = bus->next) {
408 if (bus->children) {
409 assign_resources(bus);
410 }
411 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000412}
413
Frank Vibrans39fca802011-02-14 18:35:15 +0000414/* Domain/Root Complex related code */
415
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200416static void domain_read_resources(struct device *dev)
Frank Vibrans39fca802011-02-14 18:35:15 +0000417{
Marc Jones8d595692012-03-15 12:55:26 -0600418 unsigned reg;
Frank Vibrans39fca802011-02-14 18:35:15 +0000419
Mike Loptien58089e82013-01-29 15:45:09 -0700420 printk(BIOS_DEBUG, "\nFam14h - %s\n", __func__);
Frank Vibrans39fca802011-02-14 18:35:15 +0000421
Marc Jones8d595692012-03-15 12:55:26 -0600422 /* Find the already assigned resource pairs */
423 get_fx_devs();
424 for (reg = 0x80; reg <= 0xc0; reg += 0x08) {
425 u32 base, limit;
426 base = f1_read_config32(reg);
427 limit = f1_read_config32(reg + 0x04);
428 /* Is this register allocated? */
429 if ((base & 3) != 0) {
430 unsigned nodeid, reg_link;
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200431 struct device *reg_dev;
Marc Jones8d595692012-03-15 12:55:26 -0600432 if (reg < 0xc0) { // mmio
433 nodeid = (limit & 0xf) + (base & 0x30);
434 } else { // io
435 nodeid = (limit & 0xf) + ((base >> 4) & 0x30);
436 }
437 reg_link = (limit >> 4) & 7;
438 reg_dev = __f0_dev[nodeid];
439 if (reg_dev) {
440 /* Reserve the resource */
441 struct resource *res;
442 res =
443 new_resource(reg_dev,
444 IOINDEX(0x1000 + reg,
445 reg_link));
446 if (res) {
447 res->flags = 1;
448 }
449 }
450 }
451 }
452 /* FIXME: do we need to check extend conf space?
453 I don't believe that much preset value */
Frank Vibrans39fca802011-02-14 18:35:15 +0000454
Marc Jones8d595692012-03-15 12:55:26 -0600455 pci_domain_read_resources(dev);
Frank Vibrans39fca802011-02-14 18:35:15 +0000456}
457
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200458static void domain_set_resources(struct device *dev)
Frank Vibrans39fca802011-02-14 18:35:15 +0000459{
Mike Loptien58089e82013-01-29 15:45:09 -0700460 printk(BIOS_DEBUG, "\nFam14h - %s\n", __func__);
Stefan Reinauer29e65482015-06-18 01:18:09 -0700461 printk(BIOS_DEBUG, " amsr - incoming dev = %p\n", dev);
Frank Vibrans39fca802011-02-14 18:35:15 +0000462
Marc Jones8d595692012-03-15 12:55:26 -0600463 unsigned long mmio_basek;
464 u32 pci_tolm;
465 int idx;
466 struct bus *link;
Frank Vibrans39fca802011-02-14 18:35:15 +0000467#if CONFIG_HW_MEM_HOLE_SIZEK != 0
Marc Jones8d595692012-03-15 12:55:26 -0600468 struct hw_mem_hole_info mem_hole;
469 u32 reset_memhole = 1;
Frank Vibrans39fca802011-02-14 18:35:15 +0000470#endif
471
Marc Jones8d595692012-03-15 12:55:26 -0600472 pci_tolm = 0xffffffffUL;
473 for (link = dev->link_list; link; link = link->next) {
474 pci_tolm = my_find_pci_tolm(link, pci_tolm);
475 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000476
Marc Jones8d595692012-03-15 12:55:26 -0600477 // FIXME handle interleaved nodes. If you fix this here, please fix
478 // amdk8, too.
479 mmio_basek = pci_tolm >> 10;
480 /* Round mmio_basek to something the processor can support */
481 mmio_basek &= ~((1 << 6) - 1);
Frank Vibrans39fca802011-02-14 18:35:15 +0000482
Marc Jones8d595692012-03-15 12:55:26 -0600483 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
484 // MMIO hole. If you fix this here, please fix amdk8, too.
485 /* Round the mmio hole to 64M */
486 mmio_basek &= ~((64 * 1024) - 1);
Frank Vibrans39fca802011-02-14 18:35:15 +0000487
488#if CONFIG_HW_MEM_HOLE_SIZEK != 0
489/* if the hw mem hole is already set in raminit stage, here we will compare
490 * mmio_basek and hole_basek. if mmio_basek is bigger that hole_basek and will
491 * use hole_basek as mmio_basek and we don't need to reset hole.
492 * otherwise We reset the hole to the mmio_basek
493 */
494
Marc Jones8d595692012-03-15 12:55:26 -0600495 mem_hole = get_hw_mem_hole_info();
Frank Vibrans39fca802011-02-14 18:35:15 +0000496
Marc Jones8d595692012-03-15 12:55:26 -0600497 // Use hole_basek as mmio_basek, and we don't need to reset hole anymore
498 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) {
499 mmio_basek = mem_hole.hole_startk;
500 reset_memhole = 0;
501 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000502#endif
503
Marc Jones8d595692012-03-15 12:55:26 -0600504 idx = 0x10;
Frank Vibrans39fca802011-02-14 18:35:15 +0000505
Marc Jones8d595692012-03-15 12:55:26 -0600506 struct dram_base_mask_t d;
507 resource_t basek, limitk, sizek; // 4 1T
Frank Vibrans39fca802011-02-14 18:35:15 +0000508
Marc Jones8d595692012-03-15 12:55:26 -0600509 d = get_dram_base_mask(0);
Frank Vibrans39fca802011-02-14 18:35:15 +0000510
Marc Jones8d595692012-03-15 12:55:26 -0600511 if (d.mask & 1) {
512 basek = ((resource_t) ((u64) d.base)) << 8;
513 limitk = (resource_t) (((u64) d.mask << 8) | 0xFFFFFF);
514 printk(BIOS_DEBUG,
515 "adsr: (before) basek = %llx, limitk = %llx.\n", basek,
516 limitk);
Frank Vibrans39fca802011-02-14 18:35:15 +0000517
Marc Jones8d595692012-03-15 12:55:26 -0600518 /* Convert these values to multiples of 1K for ease of math. */
519 basek >>= 10;
520 limitk >>= 10;
521 sizek = limitk - basek + 1;
Frank Vibrans39fca802011-02-14 18:35:15 +0000522
Marc Jones8d595692012-03-15 12:55:26 -0600523 printk(BIOS_DEBUG,
524 "adsr: (after) basek = %llx, limitk = %llx, sizek = %llx.\n",
525 basek, limitk, sizek);
Frank Vibrans39fca802011-02-14 18:35:15 +0000526
Marc Jones8d595692012-03-15 12:55:26 -0600527 /* see if we need a hole from 0xa0000 to 0xbffff */
528 if ((basek < 640) && (sizek > 768)) {
529 printk(BIOS_DEBUG,"adsr - 0xa0000 to 0xbffff resource.\n");
530 ram_resource(dev, (idx | 0), basek, 640 - basek);
531 idx += 0x10;
532 basek = 768;
533 sizek = limitk - 768;
534 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000535
Marc Jones8d595692012-03-15 12:55:26 -0600536 printk(BIOS_DEBUG,
537 "adsr: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n",
538 mmio_basek, basek, limitk);
Frank Vibrans39fca802011-02-14 18:35:15 +0000539
Kyösti Mälkki26c65432014-06-26 05:30:54 +0300540 /* split the region to accommodate pci memory space */
Marc Jones8d595692012-03-15 12:55:26 -0600541 if ((basek < 4 * 1024 * 1024) && (limitk > mmio_basek)) {
542 if (basek <= mmio_basek) {
543 unsigned pre_sizek;
544 pre_sizek = mmio_basek - basek;
545 if (pre_sizek > 0) {
546 ram_resource(dev, idx, basek,
547 pre_sizek);
548 idx += 0x10;
549 sizek -= pre_sizek;
Marc Jones8d595692012-03-15 12:55:26 -0600550 }
Marc Jones8d595692012-03-15 12:55:26 -0600551 basek = mmio_basek;
552 }
553 if ((basek + sizek) <= 4 * 1024 * 1024) {
554 sizek = 0;
555 } else {
556 basek = 4 * 1024 * 1024;
557 sizek -= (4 * 1024 * 1024 - mmio_basek);
558 }
559 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000560
Marc Jones8d595692012-03-15 12:55:26 -0600561 ram_resource(dev, (idx | 0), basek, sizek);
562 idx += 0x10;
Marc Jones8d595692012-03-15 12:55:26 -0600563 printk(BIOS_DEBUG,
564 "%d: mmio_basek=%08lx, basek=%08llx, limitk=%08llx\n", 0,
565 mmio_basek, basek, limitk);
Marc Jones8d595692012-03-15 12:55:26 -0600566 }
567 printk(BIOS_DEBUG, " adsr - mmio_basek = %lx.\n", mmio_basek);
Frank Vibrans39fca802011-02-14 18:35:15 +0000568
Kyösti Mälkki61be3602017-04-15 20:07:53 +0300569 add_uma_resource_below_tolm(dev, 7);
Frank Vibrans39fca802011-02-14 18:35:15 +0000570
Marc Jones8d595692012-03-15 12:55:26 -0600571 for (link = dev->link_list; link; link = link->next) {
572 if (link->children) {
573 assign_resources(link);
574 }
575 }
576 printk(BIOS_DEBUG, " adsr - leaving this lovely routine.\n");
Frank Vibrans39fca802011-02-14 18:35:15 +0000577}
578
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600579static const char *domain_acpi_name(const struct device *dev)
Tobias Diedrichd8a2c1f2017-02-20 02:46:19 +0100580{
581 if (dev->path.type == DEVICE_PATH_DOMAIN)
582 return "PCI0";
583
584 return NULL;
585}
586
Frank Vibrans39fca802011-02-14 18:35:15 +0000587/* Bus related code */
588
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200589static void cpu_bus_scan(struct device *dev)
zbaof7223732012-04-13 13:42:15 +0800590{
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +0300591 struct bus *cpu_bus = dev->link_list;
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200592 struct device *cpu;
zbaof7223732012-04-13 13:42:15 +0800593 int apic_id, cores_found;
Scott Duplichan9ab3c6c2011-05-15 21:45:46 +0000594
zbaof7223732012-04-13 13:42:15 +0800595 /* There is only one node for fam14, but there may be multiple cores. */
596 cpu = dev_find_slot(0, PCI_DEVFN(0x18, 0));
597 if (!cpu)
598 printk(BIOS_ERR, "ERROR: %02x:%02x.0 not found", 0, 0x18);
Scott Duplichan9ab3c6c2011-05-15 21:45:46 +0000599
zbaof7223732012-04-13 13:42:15 +0800600 cores_found = (pci_read_config32(dev_find_slot(0,PCI_DEVFN(0x18,0x3)), 0xe8) >> 12) & 3;
601 printk(BIOS_DEBUG, " AP siblings=%d\n", cores_found);
602
zbaof7223732012-04-13 13:42:15 +0800603 for (apic_id = 0; apic_id <= cores_found; apic_id++) {
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +0300604 cpu = add_cpu_device(cpu_bus, apic_id, 1);
605 if (cpu)
606 amd_cpu_topology(cpu, 0, apic_id);
Marc Jones8d595692012-03-15 12:55:26 -0600607 }
zbaof7223732012-04-13 13:42:15 +0800608}
609
Elyes HAOUAS0d7c7a82018-05-09 17:58:33 +0200610static void cpu_bus_init(struct device *dev)
zbaof7223732012-04-13 13:42:15 +0800611{
612 initialize_cpus(dev->link_list);
Frank Vibrans39fca802011-02-14 18:35:15 +0000613}
614
Frank Vibrans39fca802011-02-14 18:35:15 +0000615/* North Bridge Structures */
616
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300617static void northbridge_fill_ssdt_generator(struct device *device)
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200618{
619 msr_t msr;
620 char pscope[] = "\\_SB.PCI0";
621
622 acpigen_write_scope(pscope);
623 msr = rdmsr(TOP_MEM);
624 acpigen_write_name_dword("TOM1", msr.lo);
625 msr = rdmsr(TOP_MEM2);
626 /*
627 * Since XP only implements parts of ACPI 2.0, we can't use a qword
628 * here.
629 * See http://www.acpi.info/presentations/S01USMOBS169_OS%2520new.ppt
630 * slide 22ff.
631 * Shift value right by 20 bit to make it fit into 32bit,
632 * giving us 1MB granularity and a limit of almost 4Exabyte of memory.
633 */
634 acpigen_write_name_dword("TOM2", (msr.hi << 12) | msr.lo >> 20);
635 acpigen_pop_len();
636}
637
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100638static unsigned long acpi_fill_hest(acpi_hest_t *hest)
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200639{
640 void *addr, *current;
641
642 /* Skip the HEST header. */
643 current = (void *)(hest + 1);
644
645 addr = agesawrapper_getlateinitptr(PICK_WHEA_MCE);
646 if (addr != NULL)
Stefan Reinauer29e65482015-06-18 01:18:09 -0700647 current += acpi_create_hest_error_source(hest, current, 0,
648 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200649
650 addr = agesawrapper_getlateinitptr(PICK_WHEA_CMC);
651 if (addr != NULL)
Stefan Reinauer29e65482015-06-18 01:18:09 -0700652 current += acpi_create_hest_error_source(hest, current, 1,
653 addr + 2, *(UINT16 *)addr - 2);
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200654
655 return (unsigned long)current;
656}
657
Kyösti Mälkkie2c2a4c2018-05-20 20:59:52 +0300658static unsigned long agesa_write_acpi_tables(struct device *device,
Alexander Couzens83fc32f2015-04-12 22:28:37 +0200659 unsigned long current,
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200660 acpi_rsdp_t *rsdp)
661{
662 acpi_srat_t *srat;
663 acpi_slit_t *slit;
664 acpi_header_t *ssdt;
665 acpi_header_t *alib;
666 acpi_hest_t *hest;
667
668 /* HEST */
669 current = ALIGN(current, 8);
670 hest = (acpi_hest_t *)current;
Vladimir Serbinenko807127f2014-11-09 13:36:18 +0100671 acpi_write_hest((void *)current, acpi_fill_hest);
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200672 acpi_add_table(rsdp, (void *)current);
673 current += ((acpi_header_t *)current)->length;
674
675 /* SRAT */
676 current = ALIGN(current, 8);
677 printk(BIOS_DEBUG, "ACPI: * SRAT at %lx\n", current);
678 srat = (acpi_srat_t *) agesawrapper_getlateinitptr (PICK_SRAT);
679 if (srat != NULL) {
680 memcpy((void *)current, srat, srat->header.length);
681 srat = (acpi_srat_t *) current;
682 current += srat->header.length;
683 acpi_add_table(rsdp, srat);
684 }
685 else {
686 printk(BIOS_DEBUG, " AGESA SRAT table NULL. Skipping.\n");
687 }
688
689 /* SLIT */
690 current = ALIGN(current, 8);
691 printk(BIOS_DEBUG, "ACPI: * SLIT at %lx\n", current);
692 slit = (acpi_slit_t *) agesawrapper_getlateinitptr (PICK_SLIT);
693 if (slit != NULL) {
694 memcpy((void *)current, slit, slit->header.length);
695 slit = (acpi_slit_t *) current;
696 current += slit->header.length;
697 acpi_add_table(rsdp, slit);
698 }
699 else {
700 printk(BIOS_DEBUG, " AGESA SLIT table NULL. Skipping.\n");
701 }
702
703 /* SSDT */
704 current = ALIGN(current, 16);
705 printk(BIOS_DEBUG, "ACPI: * AGESA ALIB SSDT at %lx\n", current);
706 alib = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_ALIB);
707 if (alib != NULL) {
708 memcpy((void *)current, alib, alib->length);
709 alib = (acpi_header_t *) current;
710 current += alib->length;
711 acpi_add_table(rsdp, (void *)alib);
712 } else {
713 printk(BIOS_DEBUG, " AGESA ALIB SSDT table NULL. Skipping.\n");
714 }
715
716 /* The DSDT needs additional work for the AGESA SSDT Pstate table */
717 /* Keep the comment for a while. */
718 current = ALIGN(current, 16);
719 printk(BIOS_DEBUG, "ACPI: * AGESA SSDT Pstate at %lx\n", current);
720 ssdt = (acpi_header_t *)agesawrapper_getlateinitptr (PICK_PSTATE);
721 if (ssdt != NULL) {
722 memcpy((void *)current, ssdt, ssdt->length);
723 ssdt = (acpi_header_t *) current;
724 current += ssdt->length;
725 acpi_add_table(rsdp,ssdt);
726 } else {
727 printk(BIOS_DEBUG, " AGESA SSDT Pstate table NULL. Skipping.\n");
728 }
729
730 return current;
731}
732
Frank Vibrans39fca802011-02-14 18:35:15 +0000733static struct device_operations northbridge_operations = {
Marc Jones8a49ac72013-01-16 17:02:20 -0700734 .read_resources = nb_read_resources,
735 .set_resources = nb_set_resources,
Marc Jones8d595692012-03-15 12:55:26 -0600736 .enable_resources = pci_dev_enable_resources,
Vladimir Serbinenko4ab588b2014-10-08 21:18:31 +0200737 .acpi_fill_ssdt_generator = northbridge_fill_ssdt_generator,
738 .write_acpi_tables = agesa_write_acpi_tables,
Marc Jones8d595692012-03-15 12:55:26 -0600739 .init = northbridge_init,
740 .enable = 0,.ops_pci = 0,
Frank Vibrans39fca802011-02-14 18:35:15 +0000741};
742
Frank Vibrans39fca802011-02-14 18:35:15 +0000743static const struct pci_driver northbridge_driver __pci_driver = {
Marc Jones8d595692012-03-15 12:55:26 -0600744 .ops = &northbridge_operations,
745 .vendor = PCI_VENDOR_ID_AMD,
746 .device = 0x1510,
Frank Vibrans39fca802011-02-14 18:35:15 +0000747};
748
efdesign9805a89ab2011-06-20 17:38:49 -0700749struct chip_operations northbridge_amd_agesa_family14_ops = {
Marc Jones8d595692012-03-15 12:55:26 -0600750 CHIP_NAME("AMD Family 14h Northbridge")
751 .enable_dev = 0,
Frank Vibrans39fca802011-02-14 18:35:15 +0000752};
753
Frank Vibrans39fca802011-02-14 18:35:15 +0000754/* Root Complex Structures */
755
Frank Vibrans39fca802011-02-14 18:35:15 +0000756static struct device_operations pci_domain_ops = {
Marc Jones8d595692012-03-15 12:55:26 -0600757 .read_resources = domain_read_resources,
758 .set_resources = domain_set_resources,
Edward O'Callaghane9e1d7a2015-01-02 15:11:49 +1100759 .init = DEVICE_NOOP,
Marc Jones8d595692012-03-15 12:55:26 -0600760 .scan_bus = pci_domain_scan_bus,
Tobias Diedrichd8a2c1f2017-02-20 02:46:19 +0100761 .acpi_name = domain_acpi_name,
Frank Vibrans39fca802011-02-14 18:35:15 +0000762};
763
Frank Vibrans39fca802011-02-14 18:35:15 +0000764static struct device_operations cpu_bus_ops = {
Edward O'Callaghan2837ab22014-11-06 08:57:40 +1100765 .read_resources = DEVICE_NOOP,
766 .set_resources = DEVICE_NOOP,
Edward O'Callaghane9e1d7a2015-01-02 15:11:49 +1100767 .enable_resources = DEVICE_NOOP,
Marc Jones8d595692012-03-15 12:55:26 -0600768 .init = cpu_bus_init,
zbaof7223732012-04-13 13:42:15 +0800769 .scan_bus = cpu_bus_scan,
Frank Vibrans39fca802011-02-14 18:35:15 +0000770};
771
Kyösti Mälkki87213b62012-08-27 20:00:33 +0300772static void root_complex_enable_dev(struct device *dev)
773{
774 static int done = 0;
775
Kyösti Mälkki87213b62012-08-27 20:00:33 +0300776 if (!done) {
777 setup_bsp_ramtop();
Kyösti Mälkki87213b62012-08-27 20:00:33 +0300778 done = 1;
779 }
780
Marc Jones8d595692012-03-15 12:55:26 -0600781 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800782 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Marc Jones8d595692012-03-15 12:55:26 -0600783 dev->ops = &pci_domain_ops;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800784 } else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
Marc Jones8d595692012-03-15 12:55:26 -0600785 dev->ops = &cpu_bus_ops;
786 }
Frank Vibrans39fca802011-02-14 18:35:15 +0000787}
788
efdesign9805a89ab2011-06-20 17:38:49 -0700789struct chip_operations northbridge_amd_agesa_family14_root_complex_ops = {
Marc Jones8d595692012-03-15 12:55:26 -0600790 CHIP_NAME("AMD Family 14h Root Complex")
791 .enable_dev = root_complex_enable_dev,
Frank Vibrans39fca802011-02-14 18:35:15 +0000792};