amd_bus.c 10.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
#include <linux/init.h>
#include <linux/pci.h>
3
#include <linux/topology.h>
4
#include <linux/cpu.h>
5 6
#include <linux/range.h>

7
#include <asm/amd_nb.h>
8
#include <asm/pci_x86.h>
9

Y
Yinghai Lu 已提交
10
#include <asm/pci-direct.h>
L
Linus Torvalds 已提交
11

12 13
#include "bus_numa.h"

L
Linus Torvalds 已提交
14 15
/*
 * This discovers the pcibus <-> node mapping on AMD K8.
16
 * also get peer root bus resource for io,mmio
L
Linus Torvalds 已提交
17 18
 */

19 20 21 22 23 24 25 26 27 28 29 30 31 32
struct pci_hostbridge_probe {
	u32 bus;
	u32 slot;
	u32 vendor;
	u32 device;
};

static struct pci_hostbridge_probe pci_probes[] __initdata = {
	{ 0, 0x18, PCI_VENDOR_ID_AMD, 0x1100 },
	{ 0, 0x18, PCI_VENDOR_ID_AMD, 0x1200 },
	{ 0xff, 0, PCI_VENDOR_ID_AMD, 0x1200 },
	{ 0, 0x18, PCI_VENDOR_ID_AMD, 0x1300 },
};

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static u64 __initdata fam10h_mmconf_start;
static u64 __initdata fam10h_mmconf_end;
static void __init get_pci_mmcfg_amd_fam10h_range(void)
{
	u32 address;
	u64 base, msr;
	unsigned segn_busn_bits;

	/* assume all cpus from fam10h have mmconf */
        if (boot_cpu_data.x86 < 0x10)
		return;

	address = MSR_FAM10H_MMIO_CONF_BASE;
	rdmsrl(address, msr);

	/* mmconfig is not enable */
	if (!(msr & FAM10H_MMIO_CONF_ENABLE))
		return;

	base = msr & (FAM10H_MMIO_CONF_BASE_MASK<<FAM10H_MMIO_CONF_BASE_SHIFT);

	segn_busn_bits = (msr >> FAM10H_MMIO_CONF_BUSRANGE_SHIFT) &
			 FAM10H_MMIO_CONF_BUSRANGE_MASK;

	fam10h_mmconf_start = base;
	fam10h_mmconf_end = base + (1ULL<<(segn_busn_bits + 20)) - 1;
}

61 62
#define RANGE_NUM 16

L
Linus Torvalds 已提交
63
/**
Y
Yinghai Lu 已提交
64 65
 * early_fill_mp_bus_to_node()
 * called before pcibios_scan_root and pci_scan_bus
L
Linus Torvalds 已提交
66 67 68
 * fills the mp_bus_to_cpumask array based according to the LDT Bus Number
 * Registers found in the K8 northbridge
 */
69
static int __init early_fill_mp_bus_info(void)
L
Linus Torvalds 已提交
70
{
71 72 73
	int i;
	int j;
	unsigned bus;
Y
Yinghai Lu 已提交
74
	unsigned slot;
75
	int node;
76 77 78 79 80 81
	int link;
	int def_node;
	int def_link;
	struct pci_root_info *info;
	u32 reg;
	struct resource *res;
82 83
	u64 start;
	u64 end;
84
	struct range range[RANGE_NUM];
85 86
	u64 val;
	u32 address;
87
	bool found;
L
Linus Torvalds 已提交
88

Y
Yinghai Lu 已提交
89 90 91
	if (!early_pci_allowed())
		return -1;

92
	found = false;
93 94 95 96
	for (i = 0; i < ARRAY_SIZE(pci_probes); i++) {
		u32 id;
		u16 device;
		u16 vendor;
97

98 99 100
		bus = pci_probes[i].bus;
		slot = pci_probes[i].slot;
		id = read_pci_config(bus, slot, 0, PCI_VENDOR_ID);
101

102 103 104 105
		vendor = id & 0xffff;
		device = (id>>16) & 0xffff;
		if (pci_probes[i].vendor == vendor &&
		    pci_probes[i].device == device) {
106
			found = true;
107 108 109 110
			break;
		}
	}

111
	if (!found)
112
		return 0;
113

114 115 116 117 118
	pci_root_num = 0;
	for (i = 0; i < 4; i++) {
		int min_bus;
		int max_bus;
		reg = read_pci_config(bus, slot, 1, 0xe0 + (i << 2));
119 120

		/* Check if that register is enabled for bus range */
121
		if ((reg & 7) != 3)
122 123
			continue;

124 125 126 127
		min_bus = (reg >> 16) & 0xff;
		max_bus = (reg >> 24) & 0xff;
		node = (reg >> 4) & 0x07;
#ifdef CONFIG_NUMA
128
		for (j = min_bus; j <= max_bus; j++)
129
			set_mp_bus_to_node(j, node);
130 131 132 133 134 135 136 137 138 139
#endif
		link = (reg >> 8) & 0x03;

		info = &pci_root_info[pci_root_num];
		info->bus_min = min_bus;
		info->bus_max = max_bus;
		info->node = node;
		info->link = link;
		sprintf(info->name, "PCI Bus #%02x", min_bus);
		pci_root_num++;
L
Linus Torvalds 已提交
140 141
	}

142 143 144 145 146 147 148
	/* get the default node and link for left over res */
	reg = read_pci_config(bus, slot, 0, 0x60);
	def_node = (reg >> 8) & 0x07;
	reg = read_pci_config(bus, slot, 0, 0x64);
	def_link = (reg >> 8) & 0x03;

	memset(range, 0, sizeof(range));
Y
Yinghai Lu 已提交
149
	add_range(range, RANGE_NUM, 0, 0, 0xffff + 1);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	/* io port resource */
	for (i = 0; i < 4; i++) {
		reg = read_pci_config(bus, slot, 1, 0xc0 + (i << 3));
		if (!(reg & 3))
			continue;

		start = reg & 0xfff000;
		reg = read_pci_config(bus, slot, 1, 0xc4 + (i << 3));
		node = reg & 0x07;
		link = (reg >> 4) & 0x03;
		end = (reg & 0xfff000) | 0xfff;

		/* find the position */
		for (j = 0; j < pci_root_num; j++) {
			info = &pci_root_info[j];
			if (info->node == node && info->link == link)
				break;
		}
		if (j == pci_root_num)
			continue; /* not found */

		info = &pci_root_info[j];
172
		printk(KERN_DEBUG "node %d link %d: io port [%llx, %llx]\n",
173
		       node, link, start, end);
174 175 176 177 178

		/* kernel only handle 16 bit only */
		if (end > 0xffff)
			end = 0xffff;
		update_res(info, start, end, IORESOURCE_IO, 1);
Y
Yinghai Lu 已提交
179
		subtract_range(range, RANGE_NUM, start, end + 1);
180 181 182 183 184 185 186 187 188 189 190 191 192 193
	}
	/* add left over io port range to def node/link, [0, 0xffff] */
	/* find the position */
	for (j = 0; j < pci_root_num; j++) {
		info = &pci_root_info[j];
		if (info->node == def_node && info->link == def_link)
			break;
	}
	if (j < pci_root_num) {
		info = &pci_root_info[j];
		for (i = 0; i < RANGE_NUM; i++) {
			if (!range[i].end)
				continue;

Y
Yinghai Lu 已提交
194
			update_res(info, range[i].start, range[i].end - 1,
195 196 197 198 199 200
				   IORESOURCE_IO, 1);
		}
	}

	memset(range, 0, sizeof(range));
	/* 0xfd00000000-0xffffffffff for HT */
Y
Yinghai Lu 已提交
201 202 203
	end = cap_resource((0xfdULL<<32) - 1);
	end++;
	add_range(range, RANGE_NUM, 0, 0, end);
204 205 206 207

	/* need to take out [0, TOM) for RAM*/
	address = MSR_K8_TOP_MEM1;
	rdmsrl(address, val);
Y
Yinghai Lu 已提交
208
	end = (val & 0xffffff800000ULL);
209
	printk(KERN_INFO "TOM: %016llx aka %lldM\n", end, end>>20);
210
	if (end < (1ULL<<32))
Y
Yinghai Lu 已提交
211
		subtract_range(range, RANGE_NUM, 0, end);
212

213 214 215 216 217
	/* get mmconfig */
	get_pci_mmcfg_amd_fam10h_range();
	/* need to take out mmconf range */
	if (fam10h_mmconf_end) {
		printk(KERN_DEBUG "Fam 10h mmconf [%llx, %llx]\n", fam10h_mmconf_start, fam10h_mmconf_end);
Y
Yinghai Lu 已提交
218 219
		subtract_range(range, RANGE_NUM, fam10h_mmconf_start,
				 fam10h_mmconf_end + 1);
220 221
	}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	/* mmio resource */
	for (i = 0; i < 8; i++) {
		reg = read_pci_config(bus, slot, 1, 0x80 + (i << 3));
		if (!(reg & 3))
			continue;

		start = reg & 0xffffff00; /* 39:16 on 31:8*/
		start <<= 8;
		reg = read_pci_config(bus, slot, 1, 0x84 + (i << 3));
		node = reg & 0x07;
		link = (reg >> 4) & 0x03;
		end = (reg & 0xffffff00);
		end <<= 8;
		end |= 0xffff;

		/* find the position */
		for (j = 0; j < pci_root_num; j++) {
			info = &pci_root_info[j];
			if (info->node == node && info->link == link)
				break;
		}
		if (j == pci_root_num)
			continue; /* not found */

		info = &pci_root_info[j];
247 248

		printk(KERN_DEBUG "node %d link %d: mmio [%llx, %llx]",
249
		       node, link, start, end);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		/*
		 * some sick allocation would have range overlap with fam10h
		 * mmconf range, so need to update start and end.
		 */
		if (fam10h_mmconf_end) {
			int changed = 0;
			u64 endx = 0;
			if (start >= fam10h_mmconf_start &&
			    start <= fam10h_mmconf_end) {
				start = fam10h_mmconf_end + 1;
				changed = 1;
			}

			if (end >= fam10h_mmconf_start &&
			    end <= fam10h_mmconf_end) {
				end = fam10h_mmconf_start - 1;
				changed = 1;
			}

			if (start < fam10h_mmconf_start &&
			    end > fam10h_mmconf_end) {
				/* we got a hole */
				endx = fam10h_mmconf_start - 1;
				update_res(info, start, endx, IORESOURCE_MEM, 0);
Y
Yinghai Lu 已提交
274 275
				subtract_range(range, RANGE_NUM, start,
						 endx + 1);
276
				printk(KERN_CONT " ==> [%llx, %llx]", start, endx);
277 278 279 280 281
				start = fam10h_mmconf_end + 1;
				changed = 1;
			}
			if (changed) {
				if (start <= end) {
282
					printk(KERN_CONT " %s [%llx, %llx]", endx ? "and" : "==>", start, end);
283 284 285 286 287 288 289
				} else {
					printk(KERN_CONT "%s\n", endx?"":" ==> none");
					continue;
				}
			}
		}

Y
Yinghai Lu 已提交
290 291
		update_res(info, cap_resource(start), cap_resource(end),
				 IORESOURCE_MEM, 1);
Y
Yinghai Lu 已提交
292
		subtract_range(range, RANGE_NUM, start, end + 1);
293
		printk(KERN_CONT "\n");
294 295 296 297 298 299 300 301 302 303 304
	}

	/* need to take out [4G, TOM2) for RAM*/
	/* SYS_CFG */
	address = MSR_K8_SYSCFG;
	rdmsrl(address, val);
	/* TOP_MEM2 is enabled? */
	if (val & (1<<21)) {
		/* TOP_MEM2 */
		address = MSR_K8_TOP_MEM2;
		rdmsrl(address, val);
Y
Yinghai Lu 已提交
305
		end = (val & 0xffffff800000ULL);
306
		printk(KERN_INFO "TOM2: %016llx aka %lldM\n", end, end>>20);
Y
Yinghai Lu 已提交
307
		subtract_range(range, RANGE_NUM, 1ULL<<32, end);
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	}

	/*
	 * add left over mmio range to def node/link ?
	 * that is tricky, just record range in from start_min to 4G
	 */
	for (j = 0; j < pci_root_num; j++) {
		info = &pci_root_info[j];
		if (info->node == def_node && info->link == def_link)
			break;
	}
	if (j < pci_root_num) {
		info = &pci_root_info[j];

		for (i = 0; i < RANGE_NUM; i++) {
			if (!range[i].end)
				continue;

Y
Yinghai Lu 已提交
326
			update_res(info, cap_resource(range[i].start),
Y
Yinghai Lu 已提交
327
				   cap_resource(range[i].end - 1),
328 329 330 331 332 333 334 335 336 337 338
				   IORESOURCE_MEM, 1);
		}
	}

	for (i = 0; i < pci_root_num; i++) {
		int res_num;
		int busnum;

		info = &pci_root_info[i];
		res_num = info->res_num;
		busnum = info->bus_min;
339
		printk(KERN_DEBUG "bus: [%02x, %02x] on node %x link %x\n",
340 341 342
		       info->bus_min, info->bus_max, info->node, info->link);
		for (j = 0; j < res_num; j++) {
			res = &info->res[j];
343 344
			printk(KERN_DEBUG "bus: %02x index %x %pR\n",
				       busnum, j, res);
345 346 347
		}
	}

L
Linus Torvalds 已提交
348 349 350
	return 0;
}

351 352
#define ENABLE_CF8_EXT_CFG      (1ULL << 46)

353
static void __cpuinit enable_pci_io_ecs(void *unused)
354 355 356 357 358 359 360 361 362
{
	u64 reg;
	rdmsrl(MSR_AMD64_NB_CFG, reg);
	if (!(reg & ENABLE_CF8_EXT_CFG)) {
		reg |= ENABLE_CF8_EXT_CFG;
		wrmsrl(MSR_AMD64_NB_CFG, reg);
	}
}

363 364
static int __cpuinit amd_cpu_notify(struct notifier_block *self,
				    unsigned long action, void *hcpu)
365
{
366
	int cpu = (long)hcpu;
367
	switch (action) {
368 369 370 371 372 373 374 375 376 377 378 379 380 381
	case CPU_ONLINE:
	case CPU_ONLINE_FROZEN:
		smp_call_function_single(cpu, enable_pci_io_ecs, NULL, 0);
		break;
	default:
		break;
	}
	return NOTIFY_OK;
}

static struct notifier_block __cpuinitdata amd_cpu_notifier = {
	.notifier_call	= amd_cpu_notify,
};

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
static void __init pci_enable_pci_io_ecs(void)
{
#ifdef CONFIG_AMD_NB
	unsigned int i, n;

	for (n = i = 0; !n && amd_nb_bus_dev_ranges[i].dev_limit; ++i) {
		u8 bus = amd_nb_bus_dev_ranges[i].bus;
		u8 slot = amd_nb_bus_dev_ranges[i].dev_base;
		u8 limit = amd_nb_bus_dev_ranges[i].dev_limit;

		for (; slot < limit; ++slot) {
			u32 val = read_pci_config(bus, slot, 3, 0);

			if (!early_is_amd_nb(val))
				continue;

			val = read_pci_config(bus, slot, 3, 0x8c);
			if (!(val & (ENABLE_CF8_EXT_CFG >> 32))) {
				val |= ENABLE_CF8_EXT_CFG >> 32;
				write_pci_config(bus, slot, 3, 0x8c, val);
			}
			++n;
		}
	}
	pr_info("Extended Config Space enabled on %u nodes\n", n);
#endif
}

410 411 412 413
static int __init pci_io_ecs_init(void)
{
	int cpu;

414 415 416
	/* assume all cpus from fam10h have IO ECS */
        if (boot_cpu_data.x86 < 0x10)
		return 0;
417

418 419 420 421
	/* Try the PCI method first. */
	if (early_pci_allowed())
		pci_enable_pci_io_ecs();

422 423 424 425
	register_cpu_notifier(&amd_cpu_notifier);
	for_each_online_cpu(cpu)
		amd_cpu_notify(&amd_cpu_notifier, (unsigned long)CPU_ONLINE,
			       (void *)(long)cpu);
426
	pci_probe |= PCI_HAS_IO_ECS;
427

428 429 430
	return 0;
}

431 432 433 434 435 436
static int __init amd_postcore_init(void)
{
	if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
		return 0;

	early_fill_mp_bus_info();
437
	pci_io_ecs_init();
438 439 440 441 442

	return 0;
}

postcore_initcall(amd_postcore_init);