blob: c0174de150fcf547fc1ad4075b98725ecfafb5fa [file] [log] [blame]
arch import user (historical)ef03afa2005-07-06 17:15:30 +00001/* This should be done by Eric
2 2004.12 yhlu add dual core support
Uwe Hermann607614d2010-11-18 20:12:13 +00003 2005.01 yhlu add support move apic before pci_domain in MB devicetree.cb
arch import user (historical)ef03afa2005-07-06 17:15:30 +00004 2005.02 yhlu add e0 memory hole support
Stefan Reinauer7ce8c542005-12-02 21:52:30 +00005 2005.11 yhlu add put sb ht chain on bus 0
arch import user (historical)ef03afa2005-07-06 17:15:30 +00006*/
7
Eric Biederman0ac6b412003-09-02 17:16:48 +00008#include <console/console.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00009#include <arch/io.h>
10#include <stdint.h>
Eric Biederman2c018fb2003-07-21 20:13:45 +000011#include <device/device.h>
12#include <device/pci.h>
Eric Biederman17729a22003-12-08 21:48:01 +000013#include <device/pci_ids.h>
Eric Biederman0ac6b412003-09-02 17:16:48 +000014#include <device/hypertransport.h>
Eric Biederman0ac6b412003-09-02 17:16:48 +000015#include <stdlib.h>
16#include <string.h>
Ronald G. Minnich5079a0d2012-11-27 11:32:38 -080017#include <lib.h>
Eric Biederman7003ba42004-10-16 06:20:29 +000018#include <cpu/cpu.h>
Vladimir Serbinenkof21271e2014-10-16 18:00:27 +020019#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
Vladimir Serbinenko6985d4e2014-09-21 14:31:19 +020020#include <arch/acpi.h>
21#include "acpi.h"
22#endif
arch import user (historical)ef03afa2005-07-06 17:15:30 +000023
24#include <cpu/x86/lapic.h>
Kyösti Mälkkiba589e32012-07-11 08:03:13 +030025#include <cpu/amd/mtrr.h>
arch import user (historical)ef03afa2005-07-06 17:15:30 +000026
Stefan Reinauer9a16e3e2010-03-29 14:45:36 +000027#include <cpu/amd/multicore.h>
Patrick Georgie1667822012-05-05 15:29:32 +020028#if CONFIG_LOGICAL_CPUS
arch import user (historical)ef03afa2005-07-06 17:15:30 +000029#include <pc80/mc146818rtc.h>
30#endif
31
Eric Biederman0ac6b412003-09-02 17:16:48 +000032#include "northbridge.h"
Stefan Reinauerf5183cf2005-12-01 11:01:01 +000033
Eric Biederman4ab9f172003-12-06 00:11:56 +000034#include "amdk8.h"
Stefan Reinauerf5183cf2005-12-01 11:01:01 +000035
Stefan Reinauerf5183cf2005-12-01 11:01:01 +000036#include <cpu/amd/model_fxx_rev.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000037
Yinghai Lud4b278c2006-10-04 20:46:15 +000038#include <cpu/amd/amdk8_sysconf.h>
39
40struct amdk8_sysconf_t sysconf;
41
Myles Watsonc7233e02009-07-02 19:02:33 +000042#define MAX_FX_DEVS 8
43static device_t __f0_dev[MAX_FX_DEVS];
44static device_t __f1_dev[MAX_FX_DEVS];
45static unsigned fx_devs=0;
Eric Biederman0ac6b412003-09-02 17:16:48 +000046
Eric Biedermanb78c1972004-10-14 20:54:17 +000047static void get_fx_devs(void)
Eric Biederman0ac6b412003-09-02 17:16:48 +000048{
49 int i;
Myles Watsonc7233e02009-07-02 19:02:33 +000050 for(i = 0; i < MAX_FX_DEVS; i++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +000051 __f0_dev[i] = dev_find_slot(0, PCI_DEVFN(0x18 + i, 0));
Eric Biederman0ac6b412003-09-02 17:16:48 +000052 __f1_dev[i] = dev_find_slot(0, PCI_DEVFN(0x18 + i, 1));
Myles Watsonc7233e02009-07-02 19:02:33 +000053 if (__f0_dev[i] != NULL && __f1_dev[i] != NULL)
54 fx_devs = i+1;
Eric Biederman0ac6b412003-09-02 17:16:48 +000055 }
Myles Watsonc7233e02009-07-02 19:02:33 +000056 if (__f1_dev[0] == NULL || __f0_dev[0] == NULL || fx_devs == 0) {
57 die("Cannot find 0:0x18.[0|1]\n");
Eric Biederman0ac6b412003-09-02 17:16:48 +000058 }
59}
60
Myles Watson6507b392010-06-09 22:39:00 +000061static u32 f1_read_config32(unsigned reg)
Eric Biederman0ac6b412003-09-02 17:16:48 +000062{
Myles Watson6507b392010-06-09 22:39:00 +000063 if (fx_devs == 0)
Myles Watsonc7233e02009-07-02 19:02:33 +000064 get_fx_devs();
Eric Biederman0ac6b412003-09-02 17:16:48 +000065 return pci_read_config32(__f1_dev[0], reg);
66}
67
Myles Watson6507b392010-06-09 22:39:00 +000068static void f1_write_config32(unsigned reg, u32 value)
Eric Biederman0ac6b412003-09-02 17:16:48 +000069{
70 int i;
Myles Watson6507b392010-06-09 22:39:00 +000071 if (fx_devs == 0)
Myles Watsonc7233e02009-07-02 19:02:33 +000072 get_fx_devs();
73 for(i = 0; i < fx_devs; i++) {
Eric Biederman0ac6b412003-09-02 17:16:48 +000074 device_t dev;
75 dev = __f1_dev[i];
Eric Biedermanb78c1972004-10-14 20:54:17 +000076 if (dev && dev->enabled) {
Eric Biederman0ac6b412003-09-02 17:16:48 +000077 pci_write_config32(dev, reg, value);
78 }
79 }
80}
81
Kyösti Mälkki11c79d72015-02-04 15:23:03 +020082static bool is_non_coherent_link(struct device *dev, struct bus *link)
83{
84 u32 link_type;
85 do {
86 link_type = pci_read_config32(dev, link->cap + 0x18);
87 } while (link_type & ConnectionPending);
88
89 if (!(link_type & LinkConnected))
90 return false;
91
92 do {
93 link_type = pci_read_config32(dev, link->cap + 0x18);
94 } while (!(link_type & InitComplete));
95
96 return !!(link_type & NonCoherent);
97}
98
Myles Watson6507b392010-06-09 22:39:00 +000099static u32 amdk8_nodeid(device_t dev)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000100{
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000101 return (dev->path.pci.devfn >> 3) - 0x18;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000102}
103
Kyösti Mälkki26c66472015-02-04 13:09:06 +0200104static u32 amdk8_scan_chain(device_t dev, u32 nodeid, struct bus *link, bool is_sblink,
105 u32 max)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000106{
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000107 int i;
Kyösti Mälkkib39714e2015-03-12 16:16:59 +0200108 unsigned int next_unitid;
Myles Watson6507b392010-06-09 22:39:00 +0000109 u32 busses, config_busses;
110 u32 free_reg, config_reg;
111 u32 ht_unitid_base[4]; // here assume only 4 HT device on chain
112 u32 max_bus;
113 u32 min_bus;
114 u32 max_devfn;
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000115
Kyösti Mälkki260a6db2015-02-04 11:09:52 +0200116 link->cap = 0x80 + (link->link_num * 0x20);
Kyösti Mälkki11c79d72015-02-04 15:23:03 +0200117 if (!is_non_coherent_link(dev, link))
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000118 return max;
Kyösti Mälkki11c79d72015-02-04 15:23:03 +0200119
Li-Ta Lo3a812852004-12-03 22:39:34 +0000120 /* See if there is an available configuration space mapping
Myles Watsond61ada62008-10-02 19:20:22 +0000121 * register in function 1.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000122 */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000123 free_reg = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000124 for(config_reg = 0xe0; config_reg <= 0xec; config_reg += 4) {
Myles Watson6507b392010-06-09 22:39:00 +0000125 u32 config;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000126 config = f1_read_config32(config_reg);
127 if (!free_reg && ((config & 3) == 0)) {
128 free_reg = config_reg;
129 continue;
130 }
Myles Watsond61ada62008-10-02 19:20:22 +0000131 if (((config & 3) == 3) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000132 (((config >> 4) & 7) == nodeid) &&
Kyösti Mälkki260a6db2015-02-04 11:09:52 +0200133 (((config >> 8) & 3) == link->link_num)) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000134 break;
135 }
136 }
137 if (free_reg && (config_reg > 0xec)) {
138 config_reg = free_reg;
139 }
Li-Ta Lo3a812852004-12-03 22:39:34 +0000140 /* If we can't find an available configuration space mapping
Myles Watsond61ada62008-10-02 19:20:22 +0000141 * register skip this bus
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000142 */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000143 if (config_reg > 0xec) {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000144 return max;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000145 }
146
Li-Ta Lo3a812852004-12-03 22:39:34 +0000147 /* Set up the primary, secondary and subordinate bus numbers.
148 * We have no idea how many busses are behind this bridge yet,
149 * so we set the subordinate bus number to 0xff for the moment.
Eric Biederman0ac6b412003-09-02 17:16:48 +0000150 */
Stefan Reinauer08670622009-06-30 15:17:49 +0000151#if CONFIG_SB_HT_CHAIN_ON_BUS0 > 0
Myles Watsond61ada62008-10-02 19:20:22 +0000152 // first chain will on bus 0
Kyösti Mälkki26c66472015-02-04 13:09:06 +0200153 if(is_sblink) { // actually max is 0 here
Myles Watsond61ada62008-10-02 19:20:22 +0000154 min_bus = max;
155 }
Stefan Reinauer08670622009-06-30 15:17:49 +0000156 #if CONFIG_SB_HT_CHAIN_ON_BUS0 > 1
Yinghai Lu653ee542005-12-08 18:47:33 +0000157 // second chain will be on 0x40, third 0x80, forth 0xc0
Myles Watsond61ada62008-10-02 19:20:22 +0000158 else {
159 min_bus = ((max>>6) + 1) * 0x40;
160 }
161 max = min_bus;
162 #else
163 //other ...
Myles Watson6507b392010-06-09 22:39:00 +0000164 else {
Myles Watsond61ada62008-10-02 19:20:22 +0000165 min_bus = ++max;
166 }
167 #endif
Yinghai Lub3b1b2d2005-12-06 23:40:58 +0000168#else
Myles Watsond61ada62008-10-02 19:20:22 +0000169 min_bus = ++max;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000170#endif
Myles Watsond61ada62008-10-02 19:20:22 +0000171 max_bus = 0xff;
Yinghai Lub3b1b2d2005-12-06 23:40:58 +0000172
Myles Watson894a3472010-06-09 22:41:35 +0000173 link->secondary = min_bus;
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200174 link->subordinate = link->secondary;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000175
176 /* Read the existing primary/secondary/subordinate bus
177 * number configuration.
178 */
Myles Watson894a3472010-06-09 22:41:35 +0000179 busses = pci_read_config32(dev, link->cap + 0x14);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000180 config_busses = f1_read_config32(config_reg);
Myles Watsond61ada62008-10-02 19:20:22 +0000181
Eric Biederman0ac6b412003-09-02 17:16:48 +0000182 /* Configure the bus numbers for this bridge: the configuration
Li-Ta Lo3a812852004-12-03 22:39:34 +0000183 * transactions will not be propagates by the bridge if it is
184 * not correctly configured
Eric Biederman0ac6b412003-09-02 17:16:48 +0000185 */
186 busses &= 0xff000000;
187 busses |= (((unsigned int)(dev->bus->secondary) << 0) |
Myles Watson894a3472010-06-09 22:41:35 +0000188 ((unsigned int)(link->secondary) << 8) |
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200189 (max_bus << 16));
Myles Watson894a3472010-06-09 22:41:35 +0000190 pci_write_config32(dev, link->cap + 0x14, busses);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000191
Eric Biederman17729a22003-12-08 21:48:01 +0000192 config_busses &= 0x000fc88;
Myles Watsond61ada62008-10-02 19:20:22 +0000193 config_busses |=
Eric Biederman17729a22003-12-08 21:48:01 +0000194 (3 << 0) | /* rw enable, no device compare */
Myles Watsond61ada62008-10-02 19:20:22 +0000195 (( nodeid & 7) << 4) |
Kyösti Mälkki260a6db2015-02-04 11:09:52 +0200196 ((link->link_num & 3) << 8) |
Myles Watson894a3472010-06-09 22:41:35 +0000197 ((link->secondary) << 16) |
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200198 (max_bus << 24);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000199 f1_write_config32(config_reg, config_busses);
200
Li-Ta Lo3a812852004-12-03 22:39:34 +0000201 /* Now we can scan all of the subordinate busses i.e. the
Myles Watsond61ada62008-10-02 19:20:22 +0000202 * chain on the hypertranport link
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000203 */
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000204 for(i=0;i<4;i++) {
205 ht_unitid_base[i] = 0x20;
206 }
Yinghai Lu18c70d72007-09-14 14:58:33 +0000207
Myles Watsond61ada62008-10-02 19:20:22 +0000208 if (min_bus == 0)
Yinghai Lu18c70d72007-09-14 14:58:33 +0000209 max_devfn = (0x17<<3) | 7;
210 else
211 max_devfn = (0x1f<<3) | 7;
212
Kyösti Mälkkib39714e2015-03-12 16:16:59 +0200213 next_unitid = hypertransport_scan_chain(link, 0, max_devfn, ht_unitid_base, offset_unit_id(is_sblink));
214
215 /* Now that nothing is overlapping it is safe to scan the children. */
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200216 link->subordinate = pci_scan_bus(link, 0x00, ((next_unitid - 1) << 3) | 7, link->secondary);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000217
Li-Ta Lo3a812852004-12-03 22:39:34 +0000218 /* We know the number of busses behind this bridge. Set the
219 * subordinate bus number to it's real value
Eric Biederman0ac6b412003-09-02 17:16:48 +0000220 */
Eric Biederman0ac6b412003-09-02 17:16:48 +0000221 busses = (busses & 0xff00ffff) |
Myles Watson894a3472010-06-09 22:41:35 +0000222 ((unsigned int) (link->subordinate) << 16);
223 pci_write_config32(dev, link->cap + 0x14, busses);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000224
Li-Ta Lo3a812852004-12-03 22:39:34 +0000225 config_busses = (config_busses & 0x00ffffff) |
Myles Watson894a3472010-06-09 22:41:35 +0000226 (link->subordinate << 24);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000227 f1_write_config32(config_reg, config_busses);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000228
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000229 {
Myles Watson6507b392010-06-09 22:39:00 +0000230 // use config_reg and ht_unitid_base to update hcdn_reg
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000231 int index;
Myles Watson6507b392010-06-09 22:39:00 +0000232 u32 temp = 0;
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000233 index = (config_reg-0xe0) >> 2;
234 for(i=0;i<4;i++) {
235 temp |= (ht_unitid_base[i] & 0xff) << (i*8);
236 }
237
Yinghai Lud4b278c2006-10-04 20:46:15 +0000238 sysconf.hcdn_reg[index] = temp;
Stefan Reinauerbbdd8f42005-12-04 21:52:58 +0000239
240 }
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200241 return link->subordinate;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000242}
243
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200244static unsigned amdk8_scan_chains(device_t dev, unsigned unused)
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000245{
Myles Watsond61ada62008-10-02 19:20:22 +0000246 unsigned nodeid;
Myles Watson894a3472010-06-09 22:41:35 +0000247 struct bus *link;
Myles Watsond61ada62008-10-02 19:20:22 +0000248 unsigned sblink = 0;
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200249 unsigned int max = dev->bus->subordinate;
Myles Watson6507b392010-06-09 22:39:00 +0000250
Myles Watsond61ada62008-10-02 19:20:22 +0000251 nodeid = amdk8_nodeid(dev);
Kyösti Mälkkife57eeb2015-02-02 20:07:58 +0200252 if (nodeid == 0)
Myles Watsond61ada62008-10-02 19:20:22 +0000253 sblink = (pci_read_config32(dev, 0x64)>>8) & 3;
Kyösti Mälkkife57eeb2015-02-02 20:07:58 +0200254
255 // do sb ht chain at first, in case s2885 put sb chain (8131/8111) on link2, but put 8151 on link0
256 for (link = dev->link_list; link; link = link->next) {
Kyösti Mälkki26c66472015-02-04 13:09:06 +0200257 bool is_sblink = (nodeid == 0) && (link->link_num == sblink);
258 if ((CONFIG_SB_HT_CHAIN_ON_BUS0 > 0) && is_sblink)
259 max = amdk8_scan_chain(dev, nodeid, link, is_sblink, max);
Myles Watsond61ada62008-10-02 19:20:22 +0000260 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000261
Myles Watson894a3472010-06-09 22:41:35 +0000262 for (link = dev->link_list; link; link = link->next) {
Kyösti Mälkki26c66472015-02-04 13:09:06 +0200263 bool is_sblink = (nodeid == 0) && (link->link_num == sblink);
264 if ((CONFIG_SB_HT_CHAIN_ON_BUS0 > 0) && is_sblink)
Kyösti Mälkkife57eeb2015-02-02 20:07:58 +0200265 continue;
266
Kyösti Mälkki26c66472015-02-04 13:09:06 +0200267 max = amdk8_scan_chain(dev, nodeid, link, is_sblink, max);
Myles Watsond61ada62008-10-02 19:20:22 +0000268 }
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +0200269
270 dev->bus->subordinate = max;
271
272 return unused;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000273}
274
275
Myles Watson6507b392010-06-09 22:39:00 +0000276static int reg_useable(unsigned reg, device_t goal_dev, unsigned goal_nodeid,
277 unsigned goal_link)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000278{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000279 struct resource *res;
Myles Watson6507b392010-06-09 22:39:00 +0000280 unsigned nodeid, link = 0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000281 int result;
282 res = 0;
Myles Watsonc7233e02009-07-02 19:02:33 +0000283 for(nodeid = 0; !res && (nodeid < fx_devs); nodeid++) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000284 device_t dev;
285 dev = __f0_dev[nodeid];
Rudolf Marek3a8565a2009-03-26 21:45:26 +0000286 if (!dev)
287 continue;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000288 for(link = 0; !res && (link < 3); link++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000289 res = probe_resource(dev, IOINDEX(0x100 + reg, link));
Eric Biederman0ac6b412003-09-02 17:16:48 +0000290 }
291 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000292 result = 2;
293 if (res) {
294 result = 0;
Myles Watsond61ada62008-10-02 19:20:22 +0000295 if ( (goal_link == (link - 1)) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000296 (goal_nodeid == (nodeid - 1)) &&
297 (res->flags <= 1)) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000298 result = 1;
299 }
300 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000301 return result;
302}
303
Myles Watsonc7233e02009-07-02 19:02:33 +0000304static unsigned amdk8_find_reg(device_t dev, unsigned nodeid, unsigned link,
305 unsigned min, unsigned max)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000306{
Myles Watsonc7233e02009-07-02 19:02:33 +0000307 unsigned resource;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000308 unsigned free_reg, reg;
309 resource = 0;
310 free_reg = 0;
Myles Watsonc7233e02009-07-02 19:02:33 +0000311 for(reg = min; reg <= max; reg += 0x8) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000312 int result;
313 result = reg_useable(reg, dev, nodeid, link);
314 if (result == 1) {
315 /* I have been allocated this one */
316 break;
317 }
318 else if (result > 1) {
319 /* I have a free register pair */
320 free_reg = reg;
321 }
322 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000323 if (reg > max) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000324 reg = free_reg;
325 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000326 if (reg > 0) {
Myles Watsonc7233e02009-07-02 19:02:33 +0000327 resource = IOINDEX(0x100 + reg, link);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000328 }
329 return resource;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000330}
331
Myles Watsonc7233e02009-07-02 19:02:33 +0000332static unsigned amdk8_find_iopair(device_t dev, unsigned nodeid, unsigned link)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000333{
Myles Watsonc7233e02009-07-02 19:02:33 +0000334 return amdk8_find_reg(dev, nodeid, link, 0xc0, 0xd8);
335}
336
337static unsigned amdk8_find_mempair(device_t dev, unsigned nodeid, unsigned link)
338{
339 return amdk8_find_reg(dev, nodeid, link, 0x80, 0xb8);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000340}
Li-Ta Lo3a812852004-12-03 22:39:34 +0000341
Eric Biederman0ac6b412003-09-02 17:16:48 +0000342static void amdk8_link_read_bases(device_t dev, unsigned nodeid, unsigned link)
343{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000344 struct resource *resource;
Myles Watsond61ada62008-10-02 19:20:22 +0000345
Eric Biederman0ac6b412003-09-02 17:16:48 +0000346 /* Initialize the io space constraints on the current bus */
Myles Watsonc7233e02009-07-02 19:02:33 +0000347 resource = new_resource(dev, IOINDEX(0, link));
Eric Biedermanb78c1972004-10-14 20:54:17 +0000348 if (resource) {
349 resource->base = 0;
350 resource->size = 0;
351 resource->align = log2(HT_IO_HOST_ALIGN);
352 resource->gran = log2(HT_IO_HOST_ALIGN);
353 resource->limit = 0xffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000354 resource->flags = IORESOURCE_IO | IORESOURCE_BRIDGE;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000355 }
356
357 /* Initialize the prefetchable memory constraints on the current bus */
Myles Watsonc7233e02009-07-02 19:02:33 +0000358 resource = new_resource(dev, IOINDEX(2, link));
Eric Biedermanb78c1972004-10-14 20:54:17 +0000359 if (resource) {
Myles Watson6507b392010-06-09 22:39:00 +0000360 resource->base = 0;
361 resource->size = 0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000362 resource->align = log2(HT_MEM_HOST_ALIGN);
Myles Watson6507b392010-06-09 22:39:00 +0000363 resource->gran = log2(HT_MEM_HOST_ALIGN);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000364 resource->limit = 0xffffffffffULL;
365 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000366 resource->flags |= IORESOURCE_BRIDGE;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000367 }
368
369 /* Initialize the memory constraints on the current bus */
Myles Watsonc7233e02009-07-02 19:02:33 +0000370 resource = new_resource(dev, IOINDEX(1, link));
Eric Biedermanb78c1972004-10-14 20:54:17 +0000371 if (resource) {
Myles Watson6507b392010-06-09 22:39:00 +0000372 resource->base = 0;
373 resource->size = 0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000374 resource->align = log2(HT_MEM_HOST_ALIGN);
Myles Watson6507b392010-06-09 22:39:00 +0000375 resource->gran = log2(HT_MEM_HOST_ALIGN);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000376 resource->limit = 0xffffffffULL;
377 resource->flags = IORESOURCE_MEM | IORESOURCE_BRIDGE;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000378 }
Eric Biederman0ac6b412003-09-02 17:16:48 +0000379}
380
Myles Watsonc7233e02009-07-02 19:02:33 +0000381static void amdk8_create_vga_resource(device_t dev, unsigned nodeid);
382
Eric Biederman0ac6b412003-09-02 17:16:48 +0000383static void amdk8_read_resources(device_t dev)
384{
Myles Watson894a3472010-06-09 22:41:35 +0000385 unsigned nodeid;
386 struct bus *link;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000387 nodeid = amdk8_nodeid(dev);
Myles Watson894a3472010-06-09 22:41:35 +0000388 for(link = dev->link_list; link; link = link->next) {
389 if (link->children) {
390 amdk8_link_read_bases(dev, nodeid, link->link_num);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000391 }
392 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000393 amdk8_create_vga_resource(dev, nodeid);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000394}
395
Eric Biedermanb78c1972004-10-14 20:54:17 +0000396static void amdk8_set_resource(device_t dev, struct resource *resource, unsigned nodeid)
Eric Biederman0ac6b412003-09-02 17:16:48 +0000397{
Myles Watson894a3472010-06-09 22:41:35 +0000398 struct bus *link;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000399 resource_t rbase, rend;
Myles Watson894a3472010-06-09 22:41:35 +0000400 unsigned reg, link_num;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000401 char buf[50];
Eric Biederman5cd81732004-03-11 15:01:31 +0000402
Eric Biederman0ac6b412003-09-02 17:16:48 +0000403 /* Make certain the resource has actually been set */
Eric Biederman5cd81732004-03-11 15:01:31 +0000404 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000405 printk(BIOS_ERR, "%s: can't set unassigned resource @%lx %lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000406 __func__, resource->index, resource->flags);
Eric Biederman5cd81732004-03-11 15:01:31 +0000407 return;
408 }
409
410 /* If I have already stored this resource don't worry about it */
411 if (resource->flags & IORESOURCE_STORED) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000412 printk(BIOS_ERR, "%s: can't set stored resource @%lx %lx\n", __func__,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000413 resource->index, resource->flags);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000414 return;
415 }
Myles Watsond61ada62008-10-02 19:20:22 +0000416
Eric Biederman0ac6b412003-09-02 17:16:48 +0000417 /* Only handle PCI memory and IO resources */
418 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
419 return;
420
Eric Biedermanb78c1972004-10-14 20:54:17 +0000421 /* Ensure I am actually looking at a resource of function 1 */
422 if (resource->index < 0x100) {
423 return;
424 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000425
426 if (resource->size == 0)
427 return;
428
Eric Biederman0ac6b412003-09-02 17:16:48 +0000429 /* Get the base address */
430 rbase = resource->base;
Myles Watsond61ada62008-10-02 19:20:22 +0000431
Eric Biederman0ac6b412003-09-02 17:16:48 +0000432 /* Get the limit (rounded up) */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000433 rend = resource_end(resource);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000434
435 /* Get the register and link */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000436 reg = resource->index & 0xfc;
Myles Watson894a3472010-06-09 22:41:35 +0000437 link_num = IOINDEX_LINK(resource->index);
438
439 for (link = dev->link_list; link; link = link->next)
440 if (link->link_num == link_num)
441 break;
442
443 if (link == NULL) {
444 printk(BIOS_ERR, "%s: can't find link %x for %lx\n", __func__,
445 link_num, resource->index);
446 return;
447 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000448
Eric Biederman0ac6b412003-09-02 17:16:48 +0000449 if (resource->flags & IORESOURCE_IO) {
Myles Watson6507b392010-06-09 22:39:00 +0000450 u32 base, limit;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000451 base = f1_read_config32(reg);
452 limit = f1_read_config32(reg + 0x4);
453 base &= 0xfe000fcc;
454 base |= rbase & 0x01fff000;
455 base |= 3;
456 limit &= 0xfe000fc8;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000457 limit |= rend & 0x01fff000;
Myles Watson894a3472010-06-09 22:41:35 +0000458 limit |= (link_num & 3) << 4;
Li-Ta Lo32597842004-02-23 22:33:10 +0000459 limit |= (nodeid & 7);
Eric Biederman5cd81732004-03-11 15:01:31 +0000460
Myles Watson894a3472010-06-09 22:41:35 +0000461 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000462 printk(BIOS_SPEW, "%s, enabling legacy VGA IO forwarding for %s link 0x%x\n",
Myles Watson894a3472010-06-09 22:41:35 +0000463 __func__, dev_path(dev), link_num);
Eric Biederman5cd81732004-03-11 15:01:31 +0000464 base |= PCI_IO_BASE_VGA_EN;
David W. Hendricks854e4522004-02-09 22:47:38 +0000465 }
Myles Watson894a3472010-06-09 22:41:35 +0000466 if (link->bridge_ctrl & PCI_BRIDGE_CTL_NO_ISA) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000467 base |= PCI_IO_BASE_NO_ISA;
468 }
Myles Watsond61ada62008-10-02 19:20:22 +0000469
Eric Biederman0ac6b412003-09-02 17:16:48 +0000470 f1_write_config32(reg + 0x4, limit);
471 f1_write_config32(reg, base);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000472 }
473 else if (resource->flags & IORESOURCE_MEM) {
Myles Watson6507b392010-06-09 22:39:00 +0000474 u32 base, limit;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000475 base = f1_read_config32(reg);
476 limit = f1_read_config32(reg + 0x4);
477 base &= 0x000000f0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000478 base |= (rbase >> 8) & 0xffffff00;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000479 base |= 3;
480 limit &= 0x00000048;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000481 limit |= (rend >> 8) & 0xffffff00;
Myles Watson894a3472010-06-09 22:41:35 +0000482 limit |= (link_num & 3) << 4;
Li-Ta Lo32597842004-02-23 22:33:10 +0000483 limit |= (nodeid & 7);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000484 f1_write_config32(reg + 0x4, limit);
485 f1_write_config32(reg, base);
486 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000487 resource->flags |= IORESOURCE_STORED;
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100488 snprintf(buf, sizeof (buf), " <node %x link %x>",
489 nodeid, link_num);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000490 report_resource_stored(dev, resource, buf);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000491}
492
Li-Ta Lo3a812852004-12-03 22:39:34 +0000493static void amdk8_create_vga_resource(device_t dev, unsigned nodeid)
494{
495 struct resource *resource;
Myles Watson894a3472010-06-09 22:41:35 +0000496 struct bus *link;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000497
498 /* find out which link the VGA card is connected,
499 * we only deal with the 'first' vga card */
Myles Watson894a3472010-06-09 22:41:35 +0000500 for (link = dev->link_list; link; link = link->next) {
501 if (link->bridge_ctrl & PCI_BRIDGE_CTL_VGA) {
Patrick Georgie1667822012-05-05 15:29:32 +0200502#if CONFIG_MULTIPLE_VGA_ADAPTERS
Stefan Reinauerabc0c852010-11-22 08:09:50 +0000503 extern device_t vga_pri; // the primary vga device, defined in device.c
Myles Watson894a3472010-06-09 22:41:35 +0000504 printk(BIOS_DEBUG, "VGA: vga_pri bus num = %d link bus range [%d,%d]\n", vga_pri->bus->secondary,
505 link->secondary,link->subordinate);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000506 /* We need to make sure the vga_pri is under the link */
Myles Watson894a3472010-06-09 22:41:35 +0000507 if((vga_pri->bus->secondary >= link->secondary ) &&
508 (vga_pri->bus->secondary <= link->subordinate )
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000509 )
510#endif
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000511 break;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000512 }
513 }
Myles Watsond61ada62008-10-02 19:20:22 +0000514
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000515 /* no VGA card installed */
Myles Watson894a3472010-06-09 22:41:35 +0000516 if (link == NULL)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000517 return;
518
Myles Watson894a3472010-06-09 22:41:35 +0000519 printk(BIOS_DEBUG, "VGA: %s (aka node %d) link %d has VGA device\n", dev_path(dev), nodeid, link->link_num);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000520
Myles Watsonc7233e02009-07-02 19:02:33 +0000521 /* allocate a temp resource for the legacy VGA buffer */
Myles Watson894a3472010-06-09 22:41:35 +0000522 resource = new_resource(dev, IOINDEX(4, link->link_num));
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000523 if(!resource){
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000524 printk(BIOS_DEBUG, "VGA: %s out of resources.\n", dev_path(dev));
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000525 return;
526 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000527 resource->base = 0xa0000;
528 resource->size = 0x20000;
Myles Watsonc7233e02009-07-02 19:02:33 +0000529 resource->limit = 0xffffffff;
530 resource->flags = IORESOURCE_FIXED | IORESOURCE_MEM |
531 IORESOURCE_ASSIGNED;
Li-Ta Lo3a812852004-12-03 22:39:34 +0000532}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000533
Eric Biederman0ac6b412003-09-02 17:16:48 +0000534static void amdk8_set_resources(device_t dev)
535{
Myles Watson894a3472010-06-09 22:41:35 +0000536 unsigned nodeid;
537 struct bus *bus;
Myles Watsonc25cc112010-05-21 14:33:48 +0000538 struct resource *res;
Eric Biederman0ac6b412003-09-02 17:16:48 +0000539
540 /* Find the nodeid */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000541 nodeid = amdk8_nodeid(dev);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000542
543 /* Set each resource we have found */
Myles Watsonc25cc112010-05-21 14:33:48 +0000544 for(res = dev->resource_list; res; res = res->next) {
Myles Watsonc7233e02009-07-02 19:02:33 +0000545 struct resource *old = NULL;
546 unsigned index;
547
548 if (res->size == 0) /* No need to allocate registers. */
549 continue;
550
551 if (res->flags & IORESOURCE_IO)
552 index = amdk8_find_iopair(dev, nodeid,
553 IOINDEX_LINK(res->index));
554 else
555 index = amdk8_find_mempair(dev, nodeid,
556 IOINDEX_LINK(res->index));
557
558 old = probe_resource(dev, index);
559 if (old) {
560 res->index = old->index;
561 old->index = 0;
562 old->flags = 0;
563 }
564 else
565 res->index = index;
566
567 amdk8_set_resource(dev, res, nodeid);
Eric Biederman0ac6b412003-09-02 17:16:48 +0000568 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000569
Myles Watsonc7233e02009-07-02 19:02:33 +0000570 compact_resources(dev);
571
Myles Watson894a3472010-06-09 22:41:35 +0000572 for(bus = dev->link_list; bus; bus = bus->next) {
Eric Biederman0ac6b412003-09-02 17:16:48 +0000573 if (bus->children) {
574 assign_resources(bus);
575 }
576 }
577}
578
Eric Biederman5cd81732004-03-11 15:01:31 +0000579static void mcf0_control_init(struct device *dev)
David W. Hendricks854e4522004-02-09 22:47:38 +0000580{
Myles Watsond61ada62008-10-02 19:20:22 +0000581#if 0
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000582 printk(BIOS_DEBUG, "NB: Function 0 Misc Control.. ");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000583#endif
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000584#if 0
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000585 printk(BIOS_DEBUG, "done.\n");
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000586#endif
Li-Ta Loe5266692004-03-23 21:28:05 +0000587}
588
Eric Biederman0ac6b412003-09-02 17:16:48 +0000589static struct device_operations northbridge_operations = {
Myles Watson6507b392010-06-09 22:39:00 +0000590 .read_resources = amdk8_read_resources,
591 .set_resources = amdk8_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +0000592 .enable_resources = pci_dev_enable_resources,
Vladimir Serbinenkof21271e2014-10-16 18:00:27 +0200593#if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES)
Vladimir Serbinenko6985d4e2014-09-21 14:31:19 +0200594 .acpi_fill_ssdt_generator = k8acpi_write_vars,
595 .write_acpi_tables = northbridge_write_acpi_tables,
596#endif
Myles Watson6507b392010-06-09 22:39:00 +0000597 .init = mcf0_control_init,
598 .scan_bus = amdk8_scan_chains,
599 .enable = 0,
600 .ops_pci = 0,
Eric Biederman0ac6b412003-09-02 17:16:48 +0000601};
602
Eric Biedermanb78c1972004-10-14 20:54:17 +0000603
Stefan Reinauerf1cf1f72007-10-24 09:08:58 +0000604static const struct pci_driver mcf0_driver __pci_driver = {
Myles Watson6507b392010-06-09 22:39:00 +0000605 .ops = &northbridge_operations,
Eric Biederman5cd81732004-03-11 15:01:31 +0000606 .vendor = PCI_VENDOR_ID_AMD,
607 .device = 0x1100,
608};
609
Eric Biederman8bd55522004-11-05 07:04:54 +0000610struct chip_operations northbridge_amd_amdk8_ops = {
611 CHIP_NAME("AMD K8 Northbridge")
612 .enable_dev = 0,
613};
614
Myles Watson29cc9ed2009-07-02 18:56:24 +0000615static void amdk8_domain_read_resources(device_t dev)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000616{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000617 unsigned reg;
618
619 /* Find the already assigned resource pairs */
620 get_fx_devs();
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000621 for(reg = 0x80; reg <= 0xd8; reg+= 0x08) {
Myles Watson6507b392010-06-09 22:39:00 +0000622 u32 base, limit;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000623 base = f1_read_config32(reg);
624 limit = f1_read_config32(reg + 0x04);
625 /* Is this register allocated? */
626 if ((base & 3) != 0) {
Myles Watson6507b392010-06-09 22:39:00 +0000627 unsigned nodeid, reg_link;
Myles Watsonfa12b672009-04-30 22:45:41 +0000628 device_t reg_dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000629 nodeid = limit & 7;
Myles Watson6507b392010-06-09 22:39:00 +0000630 reg_link = (limit >> 4) & 3;
Myles Watsonfa12b672009-04-30 22:45:41 +0000631 reg_dev = __f0_dev[nodeid];
632 if (reg_dev) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000633 /* Reserve the resource */
Myles Watsonc7233e02009-07-02 19:02:33 +0000634 struct resource *res;
Myles Watson6507b392010-06-09 22:39:00 +0000635 res = new_resource(reg_dev, IOINDEX(0x100 + reg, reg_link));
Myles Watsonc7233e02009-07-02 19:02:33 +0000636 if (res) {
637 res->base = base;
638 res->limit = limit;
639 res->flags = 1;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000640 }
641 }
642 }
643 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000644
Myles Watson29cc9ed2009-07-02 18:56:24 +0000645 pci_domain_read_resources(dev);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000646
Patrick Georgie1667822012-05-05 15:29:32 +0200647#if CONFIG_PCI_64BIT_PREF_MEM
Myles Watsond61ada62008-10-02 19:20:22 +0000648 /* Initialize the system wide prefetchable memory resources constraints */
Myles Watsond61ada62008-10-02 19:20:22 +0000649 resource = new_resource(dev, 2);
650 resource->limit = 0xfcffffffffULL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000651 resource->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000652#endif
Eric Biedermanb78c1972004-10-14 20:54:17 +0000653}
654
Uwe Hermann4b42a622010-10-11 19:36:13 +0000655static void my_tolm_test(void *gp, struct device *dev, struct resource *new)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000656{
657 struct resource **best_p = gp;
658 struct resource *best;
659 best = *best_p;
Myles Watsonc7233e02009-07-02 19:02:33 +0000660 /* Skip VGA. */
661 if (!best || (best->base > new->base && new->base > 0xa0000)) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000662 best = new;
663 }
664 *best_p = best;
665}
666
Uwe Hermann4b42a622010-10-11 19:36:13 +0000667static u32 my_find_pci_tolm(struct bus *bus)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000668{
669 struct resource *min;
Myles Watson6507b392010-06-09 22:39:00 +0000670 u32 tolm;
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000671 min = 0;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000672 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, my_tolm_test, &min);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000673 tolm = 0xffffffffUL;
674 if (min && tolm > min->base) {
675 tolm = min->base;
676 }
677 return tolm;
678}
679
Stefan Reinauer08670622009-06-30 15:17:49 +0000680#if CONFIG_HW_MEM_HOLE_SIZEK != 0
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000681
682struct hw_mem_hole_info {
683 unsigned hole_startk;
684 int node_id;
685};
686
687static struct hw_mem_hole_info get_hw_mem_hole_info(void)
Jason Schildt8b26cab2005-10-25 21:24:23 +0000688{
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000689 struct hw_mem_hole_info mem_hole;
690 int i;
Jason Schildt8b26cab2005-10-25 21:24:23 +0000691
Stefan Reinauer08670622009-06-30 15:17:49 +0000692 mem_hole.hole_startk = CONFIG_HW_MEM_HOLE_SIZEK;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000693 mem_hole.node_id = -1;
Jason Schildt8b26cab2005-10-25 21:24:23 +0000694
Myles Watsonc7233e02009-07-02 19:02:33 +0000695 for (i = 0; i < fx_devs; i++) {
Myles Watson6507b392010-06-09 22:39:00 +0000696 u32 base;
697 u32 hole;
Myles Watsond61ada62008-10-02 19:20:22 +0000698 base = f1_read_config32(0x40 + (i << 3));
699 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
700 continue;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000701 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000702
Myles Watsond61ada62008-10-02 19:20:22 +0000703 hole = pci_read_config32(__f1_dev[i], 0xf0);
704 if(hole & 1) { // we find the hole
705 mem_hole.hole_startk = (hole & (0xff<<24)) >> 10;
706 mem_hole.node_id = i; // record the node No with hole
707 break; // only one hole
708 }
709 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000710
Kyösti Mälkki2f9b3af2014-06-26 05:30:54 +0300711 /* We need to double check if there is special set on base reg and limit reg
712 * are not continuous instead of hole, it will find out its hole_startk.
713 */
Myles Watsond61ada62008-10-02 19:20:22 +0000714 if(mem_hole.node_id==-1) {
Myles Watson6507b392010-06-09 22:39:00 +0000715 u32 limitk_pri = 0;
Myles Watsond61ada62008-10-02 19:20:22 +0000716 for(i=0; i<8; i++) {
Myles Watson6507b392010-06-09 22:39:00 +0000717 u32 base, limit;
Myles Watsond61ada62008-10-02 19:20:22 +0000718 unsigned base_k, limit_k;
719 base = f1_read_config32(0x40 + (i << 3));
720 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
721 continue;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000722 }
723
Myles Watsond61ada62008-10-02 19:20:22 +0000724 base_k = (base & 0xffff0000) >> 2;
725 if(limitk_pri != base_k) { // we find the hole
726 mem_hole.hole_startk = limitk_pri;
727 mem_hole.node_id = i;
728 break; //only one hole
729 }
730
731 limit = f1_read_config32(0x44 + (i << 3));
732 limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
733 limitk_pri = limit_k;
734 }
735 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000736 return mem_hole;
Jason Schildt8b26cab2005-10-25 21:24:23 +0000737}
Myles Watsonc7233e02009-07-02 19:02:33 +0000738
739static void disable_hoist_memory(unsigned long hole_startk, int node_id)
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000740{
Myles Watsonc7233e02009-07-02 19:02:33 +0000741 int i;
Myles Watsond61ada62008-10-02 19:20:22 +0000742 device_t dev;
Myles Watson6507b392010-06-09 22:39:00 +0000743 u32 base, limit;
744 u32 hoist;
745 u32 hole_sizek;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000746
747
Myles Watsond61ada62008-10-02 19:20:22 +0000748 //1. find which node has hole
749 //2. change limit in that node.
750 //3. change base and limit in later node
751 //4. clear that node f0
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000752
753 //if there is not mem hole enabled, we need to change it's base instead
754
755 hole_sizek = (4*1024*1024) - hole_startk;
756
Myles Watsonc7233e02009-07-02 19:02:33 +0000757 for(i=7;i>node_id;i--) {
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000758
Myles Watsonc7233e02009-07-02 19:02:33 +0000759 base = f1_read_config32(0x40 + (i << 3));
Myles Watsond61ada62008-10-02 19:20:22 +0000760 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
761 continue;
762 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000763 limit = f1_read_config32(0x44 + (i << 3));
764 f1_write_config32(0x44 + (i << 3),limit - (hole_sizek << 2));
765 f1_write_config32(0x40 + (i << 3),base - (hole_sizek << 2));
Myles Watsond61ada62008-10-02 19:20:22 +0000766 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000767 limit = f1_read_config32(0x44 + (node_id << 3));
768 f1_write_config32(0x44 + (node_id << 3),limit - (hole_sizek << 2));
769 dev = __f1_dev[node_id];
770 if (dev == NULL) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000771 printk(BIOS_ERR, "%s: node %x is NULL!\n", __func__, node_id);
Myles Watsonc7233e02009-07-02 19:02:33 +0000772 return;
773 }
774 hoist = pci_read_config32(dev, 0xf0);
Myles Watson6507b392010-06-09 22:39:00 +0000775 if(hoist & 1) {
Myles Watsonc7233e02009-07-02 19:02:33 +0000776 pci_write_config32(dev, 0xf0, 0);
Myles Watson6507b392010-06-09 22:39:00 +0000777 } else {
Myles Watsonc7233e02009-07-02 19:02:33 +0000778 base = pci_read_config32(dev, 0x40 + (node_id << 3));
779 f1_write_config32(0x40 + (node_id << 3),base - (hole_sizek << 2));
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000780 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000781}
782
Myles Watson6507b392010-06-09 22:39:00 +0000783static u32 hoist_memory(unsigned long hole_startk, int node_id)
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000784{
Myles Watsonc7233e02009-07-02 19:02:33 +0000785 int i;
Myles Watson6507b392010-06-09 22:39:00 +0000786 u32 carry_over;
Myles Watsond61ada62008-10-02 19:20:22 +0000787 device_t dev;
Myles Watson6507b392010-06-09 22:39:00 +0000788 u32 base, limit;
789 u32 basek;
790 u32 hoist;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000791
Myles Watsond61ada62008-10-02 19:20:22 +0000792 carry_over = (4*1024*1024) - hole_startk;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000793
Myles Watsonc7233e02009-07-02 19:02:33 +0000794 for(i=7;i>node_id;i--) {
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000795
Myles Watsonc7233e02009-07-02 19:02:33 +0000796 base = f1_read_config32(0x40 + (i << 3));
Myles Watsond61ada62008-10-02 19:20:22 +0000797 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
798 continue;
799 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000800 limit = f1_read_config32(0x44 + (i << 3));
801 f1_write_config32(0x44 + (i << 3),limit + (carry_over << 2));
802 f1_write_config32(0x40 + (i << 3),base + (carry_over << 2));
Myles Watsond61ada62008-10-02 19:20:22 +0000803 }
Myles Watsonc7233e02009-07-02 19:02:33 +0000804 limit = f1_read_config32(0x44 + (node_id << 3));
805 f1_write_config32(0x44 + (node_id << 3),limit + (carry_over << 2));
806 dev = __f1_dev[node_id];
807 base = pci_read_config32(dev, 0x40 + (node_id << 3));
Myles Watsond61ada62008-10-02 19:20:22 +0000808 basek = (base & 0xffff0000) >> 2;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000809 if(basek == hole_startk) {
810 //don't need set memhole here, because hole off set will be 0, overflow
811 //so need to change base reg instead, new basek will be 4*1024*1024
812 base &= 0x0000ffff;
813 base |= (4*1024*1024)<<2;
Myles Watsonc7233e02009-07-02 19:02:33 +0000814 f1_write_config32(0x40 + (node_id<<3), base);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000815 }
Rudolf Marek3a8565a2009-03-26 21:45:26 +0000816 else if (dev)
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000817 {
Myles Watsond61ada62008-10-02 19:20:22 +0000818 hoist = /* hole start address */
819 ((hole_startk << 10) & 0xff000000) +
820 /* hole address to memory controller address */
821 (((basek + carry_over) >> 6) & 0x0000ff00) +
822 /* enable */
823 1;
824
825 pci_write_config32(dev, 0xf0, hoist);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000826 }
827
Myles Watsond61ada62008-10-02 19:20:22 +0000828 return carry_over;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000829}
830#endif
Jason Schildt8b26cab2005-10-25 21:24:23 +0000831
Rudolf Marekbcaea142010-11-22 22:00:52 +0000832#include <cbmem.h>
Scott Duplichanf3cce2f2010-11-13 19:07:59 +0000833
Kyösti Mälkki6b5eb1c2012-07-19 19:26:43 +0300834static void setup_uma_memory(void)
Kyösti Mälkkiba589e32012-07-11 08:03:13 +0300835{
836#if CONFIG_GFXUMA
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300837 uint32_t topmem = (uint32_t) bsp_topmem();
Kyösti Mälkkiba589e32012-07-11 08:03:13 +0300838
839#if !CONFIG_BOARD_ASROCK_939A785GMH && !CONFIG_BOARD_AMD_MAHOGANY
840
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300841 switch (topmem) {
Kyösti Mälkkiba589e32012-07-11 08:03:13 +0300842 case 0x10000000: /* 256M system memory */
843 uma_memory_size = 0x2000000; /* 32M recommended UMA */
844 break;
845
846 case 0x18000000: /* 384M system memory */
847 uma_memory_size = 0x4000000; /* 64M recommended UMA */
848 break;
849
850 case 0x20000000: /* 512M system memory */
851 uma_memory_size = 0x4000000; /* 64M recommended UMA */
852 break;
853
854 default: /* 1GB and above system memory */
855 uma_memory_size = 0x8000000; /* 128M recommended UMA */
856 break;
857 }
858#else
859 /* refer to UMA Size Consideration in 780 BDG. */
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300860 switch (topmem) {
Kyösti Mälkkiba589e32012-07-11 08:03:13 +0300861 case 0x10000000: /* 256M system memory */
862 uma_memory_size = 0x4000000; /* 64M recommended UMA */
863 break;
864
865 case 0x20000000: /* 512M system memory */
866 uma_memory_size = 0x8000000; /* 128M recommended UMA */
867 break;
868
869 default: /* 1GB and above system memory */
870 uma_memory_size = 0x10000000; /* 256M recommended UMA */
871 break;
872 }
873#endif
874
Kyösti Mälkkidbc47392012-08-05 12:11:40 +0300875 uma_memory_base = topmem - uma_memory_size; /* TOP_MEM1 */
Kyösti Mälkkiba589e32012-07-11 08:03:13 +0300876 printk(BIOS_INFO, "%s: uma size 0x%08llx, memory start 0x%08llx\n",
877 __func__, uma_memory_size, uma_memory_base);
Kyösti Mälkkiba589e32012-07-11 08:03:13 +0300878#endif
879}
880
Myles Watson29cc9ed2009-07-02 18:56:24 +0000881static void amdk8_domain_set_resources(device_t dev)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000882{
Patrick Georgie1667822012-05-05 15:29:32 +0200883#if CONFIG_PCI_64BIT_PREF_MEM
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000884 struct resource *io, *mem1, *mem2;
Myles Watsonc25cc112010-05-21 14:33:48 +0000885 struct resource *res;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000886#endif
Eric Biedermanb78c1972004-10-14 20:54:17 +0000887 unsigned long mmio_basek;
Myles Watson6507b392010-06-09 22:39:00 +0000888 u32 pci_tolm;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +0300889 u64 ramtop = 0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000890 int i, idx;
Stefan Reinauer08670622009-06-30 15:17:49 +0000891#if CONFIG_HW_MEM_HOLE_SIZEK != 0
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000892 struct hw_mem_hole_info mem_hole;
Myles Watson6507b392010-06-09 22:39:00 +0000893 u32 reset_memhole = 1;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000894#endif
895
896#if 0
Myles Watsond61ada62008-10-02 19:20:22 +0000897 /* Place the IO devices somewhere safe */
898 io = find_resource(dev, 0);
899 io->base = DEVICE_IO_START;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000900#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200901#if CONFIG_PCI_64BIT_PREF_MEM
Myles Watsond61ada62008-10-02 19:20:22 +0000902 /* Now reallocate the pci resources memory with the
903 * highest addresses I can manage.
904 */
905 mem1 = find_resource(dev, 1);
906 mem2 = find_resource(dev, 2);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000907
908#if 1
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000909 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
Myles Watsond61ada62008-10-02 19:20:22 +0000910 mem1->base, mem1->limit, mem1->size, mem1->align);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000911 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
Myles Watsond61ada62008-10-02 19:20:22 +0000912 mem2->base, mem2->limit, mem2->size, mem2->align);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000913#endif
914
Myles Watsond61ada62008-10-02 19:20:22 +0000915 /* See if both resources have roughly the same limits */
916 if (((mem1->limit <= 0xffffffff) && (mem2->limit <= 0xffffffff)) ||
917 ((mem1->limit > 0xffffffff) && (mem2->limit > 0xffffffff)))
918 {
919 /* If so place the one with the most stringent alignment first
920 */
921 if (mem2->align > mem1->align) {
922 struct resource *tmp;
923 tmp = mem1;
924 mem1 = mem2;
925 mem2 = tmp;
926 }
927 /* Now place the memory as high up as it will go */
928 mem2->base = resource_max(mem2);
929 mem1->limit = mem2->base - 1;
930 mem1->base = resource_max(mem1);
931 }
932 else {
933 /* Place the resources as high up as they will go */
934 mem2->base = resource_max(mem2);
935 mem1->base = resource_max(mem1);
936 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000937
938#if 1
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000939 printk(BIOS_DEBUG, "base1: 0x%08Lx limit1: 0x%08Lx size: 0x%08Lx align: %d\n",
Myles Watsond61ada62008-10-02 19:20:22 +0000940 mem1->base, mem1->limit, mem1->size, mem1->align);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000941 printk(BIOS_DEBUG, "base2: 0x%08Lx limit2: 0x%08Lx size: 0x%08Lx align: %d\n",
Myles Watsond61ada62008-10-02 19:20:22 +0000942 mem2->base, mem2->limit, mem2->size, mem2->align);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000943#endif
944
Myles Watsonc25cc112010-05-21 14:33:48 +0000945 for(res = dev->resource_list; res; res = res->next)
Myles Watsond61ada62008-10-02 19:20:22 +0000946 {
Myles Watsonc25cc112010-05-21 14:33:48 +0000947 res->flags |= IORESOURCE_ASSIGNED;
948 res->flags |= IORESOURCE_STORED;
949 report_resource_stored(dev, res, "");
Myles Watsond61ada62008-10-02 19:20:22 +0000950 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000951#endif
952
Uwe Hermann4b42a622010-10-11 19:36:13 +0000953 pci_tolm = my_find_pci_tolm(dev->link_list);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000954
Stefan Reinauer29ceae22010-04-20 11:03:41 +0000955 // FIXME handle interleaved nodes. If you fix this here, please fix
956 // amdfam10, too.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000957 mmio_basek = pci_tolm >> 10;
958 /* Round mmio_basek to something the processor can support */
959 mmio_basek &= ~((1 << 6) -1);
960
Stefan Reinauer29ceae22010-04-20 11:03:41 +0000961 // FIXME improve mtrr.c so we don't use up all of the mtrrs with a 64M
962 // MMIO hole. If you fix this here, please fix amdfam10, too.
Stefan Reinauerd4f53732010-04-09 14:46:51 +0000963 /* Round the mmio hole to 64M */
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000964 mmio_basek &= ~((64*1024) - 1);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000965
Stefan Reinauer08670622009-06-30 15:17:49 +0000966#if CONFIG_HW_MEM_HOLE_SIZEK != 0
Myles Watsond61ada62008-10-02 19:20:22 +0000967 /* if the hw mem hole is already set in raminit stage, here we will compare mmio_basek and hole_basek
968 * if mmio_basek is bigger that hole_basek and will use hole_basek as mmio_basek and we don't need to reset hole.
969 * otherwise We reset the hole to the mmio_basek
970 */
Patrick Georgie1667822012-05-05 15:29:32 +0200971 #if !CONFIG_K8_REV_F_SUPPORT
Myles Watsond61ada62008-10-02 19:20:22 +0000972 if (!is_cpu_pre_e0()) {
973 #endif
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000974
975 mem_hole = get_hw_mem_hole_info();
976
Myles Watsond61ada62008-10-02 19:20:22 +0000977 if ((mem_hole.node_id != -1) && (mmio_basek > mem_hole.hole_startk)) { //We will use hole_basek as mmio_basek, and we don't need to reset hole anymore
978 mmio_basek = mem_hole.hole_startk;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000979 reset_memhole = 0;
Myles Watsond61ada62008-10-02 19:20:22 +0000980 }
981
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000982 //mmio_basek = 3*1024*1024; // for debug to meet boundary
983
984 if(reset_memhole) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000985 if(mem_hole.node_id!=-1) { // We need to select CONFIG_HW_MEM_HOLE_SIZEK for raminit, it can not make hole_startk to some basek too....!
Myles Watsond61ada62008-10-02 19:20:22 +0000986 // We need to reset our Mem Hole, because We want more big HOLE than we already set
987 //Before that We need to disable mem hole at first, becase memhole could already be set on i+1 instead
988 disable_hoist_memory(mem_hole.hole_startk, mem_hole.node_id);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000989 }
990
Patrick Georgie1667822012-05-05 15:29:32 +0200991 #if CONFIG_HW_MEM_HOLE_SIZE_AUTO_INC
Stefan Reinauerf5183cf2005-12-01 11:01:01 +0000992 //We need to double check if the mmio_basek is valid for hole setting, if it is equal to basek, we need to decrease it some
Myles Watson6507b392010-06-09 22:39:00 +0000993 u32 basek_pri;
Myles Watsonc7233e02009-07-02 19:02:33 +0000994 for (i = 0; i < fx_devs; i++) {
Myles Watson6507b392010-06-09 22:39:00 +0000995 u32 base;
996 u32 basek;
Myles Watsond61ada62008-10-02 19:20:22 +0000997 base = f1_read_config32(0x40 + (i << 3));
998 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
999 continue;
1000 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001001
1002 basek = (base & 0xffff0000) >> 2;
1003 if(mmio_basek == basek) {
Myles Watsond61ada62008-10-02 19:20:22 +00001004 mmio_basek -= (basek - basek_pri)>>1; // increase mem hole size to make sure it is on middle of pri node
1005 break;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001006 }
1007 basek_pri = basek;
Myles Watsond61ada62008-10-02 19:20:22 +00001008 }
1009 #endif
1010 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001011
Patrick Georgie1667822012-05-05 15:29:32 +02001012#if !CONFIG_K8_REV_F_SUPPORT
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001013 } // is_cpu_pre_e0
Yinghai Lud4b278c2006-10-04 20:46:15 +00001014#endif
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001015
1016#endif
1017
Stefan Reinauerf622d592005-11-26 16:56:05 +00001018 idx = 0x10;
Myles Watsonc7233e02009-07-02 19:02:33 +00001019 for(i = 0; i < fx_devs; i++) {
Myles Watson6507b392010-06-09 22:39:00 +00001020 u32 base, limit;
1021 u32 basek, limitk, sizek;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001022 base = f1_read_config32(0x40 + (i << 3));
1023 limit = f1_read_config32(0x44 + (i << 3));
1024 if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
1025 continue;
1026 }
1027 basek = (base & 0xffff0000) >> 2;
1028 limitk = ((limit + 0x00010000) & 0xffff0000) >> 2;
1029 sizek = limitk - basek;
1030
1031 /* see if we need a hole from 0xa0000 to 0xbffff */
1032 if ((basek < ((8*64)+(8*16))) && (sizek > ((8*64)+(16*16)))) {
Stefan Reinauerf622d592005-11-26 16:56:05 +00001033 ram_resource(dev, (idx | i), basek, ((8*64)+(8*16)) - basek);
1034 idx += 0x10;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001035 basek = (8*64)+(16*16);
1036 sizek = limitk - ((8*64)+(16*16));
Myles Watsond61ada62008-10-02 19:20:22 +00001037
Eric Biedermanb78c1972004-10-14 20:54:17 +00001038 }
arch import user (historical)ef03afa2005-07-06 17:15:30 +00001039
Myles Watsond61ada62008-10-02 19:20:22 +00001040
Patrick Georgie1667822012-05-05 15:29:32 +02001041#if CONFIG_GFXUMA
Myles Watson08e0fb82010-03-22 16:33:25 +00001042 printk(BIOS_DEBUG, "node %d : uma_memory_base/1024=0x%08llx, mmio_basek=0x%08lx, basek=0x%08x, limitk=0x%08x\n", i, uma_memory_base >> 10, mmio_basek, basek, limitk);
Stefan Reinauer8f3b8582009-10-24 12:40:52 +00001043 if ((uma_memory_base >> 10) < mmio_basek)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001044 printk(BIOS_ALERT, "node %d: UMA memory starts below mmio_basek\n", i);
Stefan Reinauer8f3b8582009-10-24 12:40:52 +00001045#else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001046// printk(BIOS_DEBUG, "node %d : mmio_basek=%08x, basek=%08x, limitk=%08x\n", i, mmio_basek, basek, limitk); //yhlu
Stefan Reinauer8f3b8582009-10-24 12:40:52 +00001047#endif
Myles Watsond61ada62008-10-02 19:20:22 +00001048
Kyösti Mälkki26c65432014-06-26 05:30:54 +03001049 /* See if I need to split the region to accommodate pci memory space */
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001050 if ( (basek < 4*1024*1024 ) && (limitk > mmio_basek) ) {
1051 if (basek <= mmio_basek) {
Eric Biedermanb78c1972004-10-14 20:54:17 +00001052 unsigned pre_sizek;
1053 pre_sizek = mmio_basek - basek;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001054 if(pre_sizek>0) {
1055 ram_resource(dev, (idx | i), basek, pre_sizek);
1056 idx += 0x10;
1057 sizek -= pre_sizek;
Kyösti Mälkki2b790f62013-09-03 05:25:57 +03001058 if (!ramtop)
1059 ramtop = mmio_basek * 1024;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001060 }
Stefan Reinauer08670622009-06-30 15:17:49 +00001061 #if CONFIG_HW_MEM_HOLE_SIZEK != 0
Myles Watsond61ada62008-10-02 19:20:22 +00001062 if(reset_memhole)
Patrick Georgie1667822012-05-05 15:29:32 +02001063 #if !CONFIG_K8_REV_F_SUPPORT
Myles Watsond61ada62008-10-02 19:20:22 +00001064 if(!is_cpu_pre_e0() )
Yinghai Lud4b278c2006-10-04 20:46:15 +00001065 #endif
Myles Watsond61ada62008-10-02 19:20:22 +00001066 sizek += hoist_memory(mmio_basek,i);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001067 #endif
Myles Watsond61ada62008-10-02 19:20:22 +00001068
Eric Biedermanb78c1972004-10-14 20:54:17 +00001069 basek = mmio_basek;
1070 }
1071 if ((basek + sizek) <= 4*1024*1024) {
1072 sizek = 0;
1073 }
1074 else {
1075 basek = 4*1024*1024;
1076 sizek -= (4*1024*1024 - mmio_basek);
1077 }
1078 }
Kyösti Mälkkide3dde42012-07-10 13:10:24 +03001079
1080 ram_resource(dev, (idx | i), basek, sizek);
Stefan Reinauerf622d592005-11-26 16:56:05 +00001081 idx += 0x10;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001082 printk(BIOS_DEBUG, "%d: mmio_basek=%08lx, basek=%08x, limitk=%08x\n",
Myles Watson283a4942009-03-10 20:39:27 +00001083 i, mmio_basek, basek, limitk);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +03001084 if (!ramtop)
1085 ramtop = limitk * 1024;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001086 }
Scott Duplichanf3cce2f2010-11-13 19:07:59 +00001087
Patrick Georgie1667822012-05-05 15:29:32 +02001088#if CONFIG_GFXUMA
Kyösti Mälkki2b790f62013-09-03 05:25:57 +03001089 set_top_of_ram(uma_memory_base);
Kyösti Mälkki63f8c082012-07-10 13:27:26 +03001090 uma_resource(dev, 7, uma_memory_base >> 10, uma_memory_size >> 10);
Kyösti Mälkki2b790f62013-09-03 05:25:57 +03001091#else
1092 set_top_of_ram(ramtop);
Scott Duplichanf3cce2f2010-11-13 19:07:59 +00001093#endif
Myles Watson894a3472010-06-09 22:41:35 +00001094 assign_resources(dev->link_list);
Myles Watson283a4942009-03-10 20:39:27 +00001095
Eric Biedermanb78c1972004-10-14 20:54:17 +00001096}
1097
Myles Watson6507b392010-06-09 22:39:00 +00001098static u32 amdk8_domain_scan_bus(device_t dev, u32 max)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001099{
Myles Watson6507b392010-06-09 22:39:00 +00001100 u32 reg;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001101 int i;
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +02001102 struct bus *link = dev->link_list;
1103
Eric Biedermanb78c1972004-10-14 20:54:17 +00001104 /* Unmap all of the HT chains */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001105 for(reg = 0xe0; reg <= 0xec; reg += 4) {
Eric Biedermanb78c1972004-10-14 20:54:17 +00001106 f1_write_config32(reg, 0);
1107 }
Kyösti Mälkki6ccf1192015-03-12 05:56:22 +02001108
1109 link->secondary = dev->bus->subordinate;
1110 link->subordinate = pci_scan_bus(link, PCI_DEVFN(0x18, 0), 0xff, link->secondary);
1111 dev->bus->subordinate = link->subordinate;
Myles Watsond61ada62008-10-02 19:20:22 +00001112
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001113 /* Tune the hypertransport transaction for best performance.
1114 * Including enabling relaxed ordering if it is safe.
1115 */
1116 get_fx_devs();
Myles Watsonc7233e02009-07-02 19:02:33 +00001117 for(i = 0; i < fx_devs; i++) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001118 device_t f0_dev;
1119 f0_dev = __f0_dev[i];
1120 if (f0_dev && f0_dev->enabled) {
Myles Watson6507b392010-06-09 22:39:00 +00001121 u32 httc;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001122 httc = pci_read_config32(f0_dev, HT_TRANSACTION_CONTROL);
1123 httc &= ~HTTC_RSP_PASS_PW;
Myles Watson894a3472010-06-09 22:41:35 +00001124 if (!dev->link_list->disable_relaxed_ordering) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001125 httc |= HTTC_RSP_PASS_PW;
1126 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001127 printk(BIOS_SPEW, "%s passpw: %s\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001128 dev_path(dev),
Myles Watson894a3472010-06-09 22:41:35 +00001129 (!dev->link_list->disable_relaxed_ordering)?
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001130 "enabled":"disabled");
1131 pci_write_config32(f0_dev, HT_TRANSACTION_CONTROL, httc);
1132 }
1133 }
Eric Biederman7003ba42004-10-16 06:20:29 +00001134 return max;
1135}
1136
1137static struct device_operations pci_domain_ops = {
Myles Watson6507b392010-06-09 22:39:00 +00001138 .read_resources = amdk8_domain_read_resources,
1139 .set_resources = amdk8_domain_set_resources,
Myles Watson7eac4452010-06-17 16:16:56 +00001140 .enable_resources = NULL,
1141 .init = NULL,
Myles Watson6507b392010-06-09 22:39:00 +00001142 .scan_bus = amdk8_domain_scan_bus,
Kyösti Mälkki33e5df32013-07-03 10:51:34 +03001143 .ops_pci_bus = pci_bus_default_ops,
Eric Biederman7003ba42004-10-16 06:20:29 +00001144};
1145
Myles Watson894a3472010-06-09 22:41:35 +00001146static void add_more_links(device_t dev, unsigned total_links)
1147{
1148 struct bus *link, *last = NULL;
Kyösti Mälkkic0ee9372015-02-21 23:56:07 +02001149 int link_num = -1;
Myles Watson894a3472010-06-09 22:41:35 +00001150
Kyösti Mälkkic0ee9372015-02-21 23:56:07 +02001151 for (link = dev->link_list; link; link = link->next) {
1152 if (link_num < link->link_num)
1153 link_num = link->link_num;
Myles Watson894a3472010-06-09 22:41:35 +00001154 last = link;
Kyösti Mälkkic0ee9372015-02-21 23:56:07 +02001155 }
Myles Watson894a3472010-06-09 22:41:35 +00001156
1157 if (last) {
Kyösti Mälkkic0ee9372015-02-21 23:56:07 +02001158 int links = total_links - (link_num + 1);
Myles Watson894a3472010-06-09 22:41:35 +00001159 if (links > 0) {
1160 link = malloc(links*sizeof(*link));
1161 if (!link)
1162 die("Couldn't allocate more links!\n");
1163 memset(link, 0, links*sizeof(*link));
1164 last->next = link;
1165 }
1166 }
1167 else {
Myles Watson894a3472010-06-09 22:41:35 +00001168 link = malloc(total_links*sizeof(*link));
1169 memset(link, 0, total_links*sizeof(*link));
1170 dev->link_list = link;
1171 }
1172
1173 for (link_num = link_num + 1; link_num < total_links; link_num++) {
1174 link->link_num = link_num;
1175 link->dev = dev;
1176 link->next = link + 1;
1177 last = link;
1178 link = link->next;
1179 }
1180 last->next = NULL;
1181}
1182
Kyösti Mälkkicd37a2b2015-02-25 07:38:01 +02001183static u32 cpu_bus_scan(device_t dev, u32 passthru)
Eric Biederman7003ba42004-10-16 06:20:29 +00001184{
1185 struct bus *cpu_bus;
Yinghai Lu90a04ee2005-01-07 21:12:05 +00001186 device_t dev_mc;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001187 int bsp_apicid;
arch import user (historical)ef03afa2005-07-06 17:15:30 +00001188 int i,j;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001189 unsigned nb_cfg_54;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001190 unsigned siblings;
Myles Watsond61ada62008-10-02 19:20:22 +00001191 int e0_later_single_core;
Luc Verhaegena9c5ea02009-06-03 14:19:33 +00001192 int disable_siblings;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001193
1194 nb_cfg_54 = 0;
Yinghai Lud4b278c2006-10-04 20:46:15 +00001195 sysconf.enabled_apic_ext_id = 0;
1196 sysconf.lift_bsp_apicid = 0;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001197 siblings = 0;
1198
1199 /* Find the bootstrap processors apicid */
1200 bsp_apicid = lapicid();
Yinghai Lud4b278c2006-10-04 20:46:15 +00001201 sysconf.apicid_offset = bsp_apicid;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001202
1203 disable_siblings = !CONFIG_LOGICAL_CPUS;
Patrick Georgie1667822012-05-05 15:29:32 +02001204#if CONFIG_LOGICAL_CPUS
Myles Watson4839e2c2010-04-08 15:06:44 +00001205 get_option(&disable_siblings, "multi_core");
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001206#endif
1207
Myles Watson6507b392010-06-09 22:39:00 +00001208 // for pre_e0, nb_cfg_54 can not be set, (when you read it still is 0)
1209 // How can I get the nb_cfg_54 of every node's nb_cfg_54 in bsp???
1210 // and differ d0 and e0 single core
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001211 nb_cfg_54 = read_nb_cfg_54();
Yinghai Lu90a04ee2005-01-07 21:12:05 +00001212
arch import user (historical)ef03afa2005-07-06 17:15:30 +00001213 dev_mc = dev_find_slot(0, PCI_DEVFN(0x18, 0));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001214 if (!dev_mc) {
1215 die("0:18.0 not found?");
1216 }
arch import user (historical)ef03afa2005-07-06 17:15:30 +00001217
Yinghai Lud4b278c2006-10-04 20:46:15 +00001218 sysconf.nodes = ((pci_read_config32(dev_mc, 0x60)>>4) & 7) + 1;
Myles Watsond61ada62008-10-02 19:20:22 +00001219
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001220
1221 if (pci_read_config32(dev_mc, 0x68) & (HTTC_APIC_EXT_ID|HTTC_APIC_EXT_BRD_CST))
1222 {
Yinghai Lud4b278c2006-10-04 20:46:15 +00001223 sysconf.enabled_apic_ext_id = 1;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001224 if(bsp_apicid == 0) {
1225 /* bsp apic id is not changed */
Stefan Reinauer08670622009-06-30 15:17:49 +00001226 sysconf.apicid_offset = CONFIG_APIC_ID_OFFSET;
Myles Watsond61ada62008-10-02 19:20:22 +00001227 } else
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001228 {
Yinghai Lud4b278c2006-10-04 20:46:15 +00001229 sysconf.lift_bsp_apicid = 1;
Myles Watsond61ada62008-10-02 19:20:22 +00001230 }
1231
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001232 }
1233
Eric Biedermanb78c1972004-10-14 20:54:17 +00001234 /* Find which cpus are present */
Myles Watson894a3472010-06-09 22:41:35 +00001235 cpu_bus = dev->link_list;
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001236
1237 /* Always use the devicetree node with lapic_id 0 for BSP. */
1238 remap_bsp_lapic(cpu_bus);
1239
Yinghai Lud4b278c2006-10-04 20:46:15 +00001240 for(i = 0; i < sysconf.nodes; i++) {
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001241 device_t cpu_dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001242
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001243 /* Find the cpu's pci device */
Myles Watsonfa12b672009-04-30 22:45:41 +00001244 cpu_dev = dev_find_slot(0, PCI_DEVFN(0x18 + i, 3));
1245 if (!cpu_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001246 /* If I am probing things in a weird order
1247 * ensure all of the cpu's pci devices are found.
1248 */
Myles Watsonfa12b672009-04-30 22:45:41 +00001249 int local_j;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001250 device_t dev_f0;
Myles Watsonfa12b672009-04-30 22:45:41 +00001251 for(local_j = 0; local_j <= 3; local_j++) {
1252 cpu_dev = pci_probe_dev(NULL, dev_mc->bus,
1253 PCI_DEVFN(0x18 + i, local_j));
Eric Biedermanb78c1972004-10-14 20:54:17 +00001254 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001255 /* Ok, We need to set the links for that device.
1256 * otherwise the device under it will not be scanned
1257 */
1258 dev_f0 = dev_find_slot(0, PCI_DEVFN(0x18+i,0));
1259 if(dev_f0) {
Myles Watson894a3472010-06-09 22:41:35 +00001260 add_more_links(dev_f0, 3);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001261 }
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001262 }
1263
1264 e0_later_single_core = 0;
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001265 int enable_node = cpu_dev && cpu_dev->enabled;
1266 if (enable_node) {
Myles Watsonfa12b672009-04-30 22:45:41 +00001267 j = pci_read_config32(cpu_dev, 0xe8);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001268 j = (j >> 12) & 3; // dev is func 3
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001269 printk(BIOS_DEBUG, " %s siblings=%d\n", dev_path(cpu_dev), j);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001270
1271 if(nb_cfg_54) {
Myles Watsond61ada62008-10-02 19:20:22 +00001272 // For e0 single core if nb_cfg_54 is set, apicid will be 0, 2, 4....
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001273 // ----> you can mixed single core e0 and dual core e0 at any sequence
1274 // That is the typical case
1275
Myles Watsond61ada62008-10-02 19:20:22 +00001276 if(j == 0 ){
Patrick Georgie1667822012-05-05 15:29:32 +02001277 #if !CONFIG_K8_REV_F_SUPPORT
Myles Watsond61ada62008-10-02 19:20:22 +00001278 e0_later_single_core = is_e0_later_in_bsp(i); // single core
Yinghai Lud4b278c2006-10-04 20:46:15 +00001279 #else
1280 e0_later_single_core = is_cpu_f0_in_bsp(i); // We can read cpuid(1) from Func3
1281 #endif
Myles Watsond61ada62008-10-02 19:20:22 +00001282 } else {
1283 e0_later_single_core = 0;
1284 }
1285 if(e0_later_single_core) {
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001286 printk(BIOS_DEBUG, "\tFound Rev E or Rev F later single core\n");
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001287
Myles Watsond61ada62008-10-02 19:20:22 +00001288 j=1;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001289 }
Myles Watsond61ada62008-10-02 19:20:22 +00001290
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001291 if(siblings > j ) {
1292 }
1293 else {
1294 siblings = j;
1295 }
1296 } else {
1297 siblings = j;
1298 }
Eric Biedermanb78c1972004-10-14 20:54:17 +00001299 }
Myles Watsond61ada62008-10-02 19:20:22 +00001300
Myles Watson6507b392010-06-09 22:39:00 +00001301 u32 jj;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001302 if(e0_later_single_core || disable_siblings) {
1303 jj = 0;
Myles Watsond61ada62008-10-02 19:20:22 +00001304 } else
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001305 {
1306 jj = siblings;
1307 }
Myles Watsond61ada62008-10-02 19:20:22 +00001308
1309 for (j = 0; j <=jj; j++ ) {
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001310 u32 apic_id = i * (nb_cfg_54?(siblings+1):1) + j * (nb_cfg_54?1:8);
Kyösti Mälkkicd9fc1a2012-07-06 19:02:56 +03001311 if(sysconf.enabled_apic_ext_id) {
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001312 if (apic_id != 0 || sysconf.lift_bsp_apicid) {
1313 apic_id += sysconf.apicid_offset;
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001314 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001315 }
Myles Watsond61ada62008-10-02 19:20:22 +00001316
Kyösti Mälkkic33f1e92012-08-07 17:12:11 +03001317 device_t cpu = add_cpu_device(cpu_bus, apic_id, enable_node);
1318 if (cpu)
1319 amd_cpu_topology(cpu, i, j);
Stefan Reinauerf5183cf2005-12-01 11:01:01 +00001320 } //j
1321 }
Kyösti Mälkkicd37a2b2015-02-25 07:38:01 +02001322 return passthru;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001323}
1324
Eric Biederman7003ba42004-10-16 06:20:29 +00001325static void cpu_bus_init(device_t dev)
1326{
Kyösti Mälkki7bdf85b2012-07-07 13:42:03 +03001327#if CONFIG_WAIT_BEFORE_CPUS_INIT
1328 cpus_ready_for_init();
1329#endif
Myles Watson894a3472010-06-09 22:41:35 +00001330 initialize_cpus(dev->link_list);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +00001331}
1332
Eric Biederman7003ba42004-10-16 06:20:29 +00001333static struct device_operations cpu_bus_ops = {
Edward O'Callaghan812d2a42014-10-31 08:17:23 +11001334 .read_resources = DEVICE_NOOP,
1335 .set_resources = DEVICE_NOOP,
1336 .enable_resources = DEVICE_NOOP,
Myles Watson6507b392010-06-09 22:39:00 +00001337 .init = cpu_bus_init,
1338 .scan_bus = cpu_bus_scan,
Eric Biedermanb78c1972004-10-14 20:54:17 +00001339};
1340
Eric Biederman8bd55522004-11-05 07:04:54 +00001341static void root_complex_enable_dev(struct device *dev)
Eric Biederman0ac6b412003-09-02 17:16:48 +00001342{
Kyösti Mälkki87213b62012-08-27 20:00:33 +03001343 static int done = 0;
1344
1345 /* Do not delay UMA setup, as a device on the PCI bus may evaluate
1346 the global uma_memory variables already in its enable function. */
1347 if (!done) {
1348 setup_bsp_ramtop();
1349 setup_uma_memory();
1350 done = 1;
1351 }
1352
Eric Biederman7003ba42004-10-16 06:20:29 +00001353 /* Set the operations if it is a special bus type */
Stefan Reinauer4aff4452013-02-12 14:17:15 -08001354 if (dev->path.type == DEVICE_PATH_DOMAIN) {
Eric Biederman7003ba42004-10-16 06:20:29 +00001355 dev->ops = &pci_domain_ops;
1356 }
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001357 else if (dev->path.type == DEVICE_PATH_CPU_CLUSTER) {
Eric Biederman7003ba42004-10-16 06:20:29 +00001358 dev->ops = &cpu_bus_ops;
Eric Biedermanb78c1972004-10-14 20:54:17 +00001359 }
Eric Biederman0ac6b412003-09-02 17:16:48 +00001360}
1361
Eric Biederman8bd55522004-11-05 07:04:54 +00001362struct chip_operations northbridge_amd_amdk8_root_complex_ops = {
1363 CHIP_NAME("AMD K8 Root Complex")
1364 .enable_dev = root_complex_enable_dev,
Eric Biederman0ac6b412003-09-02 17:16:48 +00001365};