centaur.c 10.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <asm/processor.h>
#include <asm/msr.h>
#include <asm/e820.h>
7
#include <asm/mtrr.h>
L
Linus Torvalds 已提交
8 9 10 11
#include "cpu.h"

#ifdef CONFIG_X86_OOSTORE

12
static u32 __cpuinit power2(u32 x)
L
Linus Torvalds 已提交
13
{
14 15 16 17
	u32 s = 1;
	while(s <= x)
		s <<= 1;
	return s >>= 1;
L
Linus Torvalds 已提交
18 19 20 21 22 23
}


/*
 *	Set up an actual MCR
 */
24

25
static void __cpuinit centaur_mcr_insert(int reg, u32 base, u32 size, int key)
L
Linus Torvalds 已提交
26 27
{
	u32 lo, hi;
28

L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42
	hi = base & ~0xFFF;
	lo = ~(size-1);		/* Size is a power of 2 so this makes a mask */
	lo &= ~0xFFF;		/* Remove the ctrl value bits */
	lo |= key;		/* Attribute we wish to set */
	wrmsr(reg+MSR_IDT_MCR0, lo, hi);
	mtrr_centaur_report_mcr(reg, lo, hi);	/* Tell the mtrr driver */
}

/*
 *	Figure what we can cover with MCR's
 *
 *	Shortcut: We know you can't put 4Gig of RAM on a winchip
 */

43
static u32 __cpuinit ramtop(void)		/* 16388 */
L
Linus Torvalds 已提交
44 45 46 47
{
	int i;
	u32 top = 0;
	u32 clip = 0xFFFFFFFFUL;
48

L
Linus Torvalds 已提交
49 50 51 52 53 54 55
	for (i = 0; i < e820.nr_map; i++) {
		unsigned long start, end;

		if (e820.map[i].addr > 0xFFFFFFFFUL)
			continue;
		/*
		 *	Don't MCR over reserved space. Ignore the ISA hole
S
Simon Arlott 已提交
56
		 *	we frob around that catastrophe already
L
Linus Torvalds 已提交
57
		 */
58

L
Linus Torvalds 已提交
59 60
		if (e820.map[i].type == E820_RESERVED)
		{
61
			if (e820.map[i].addr >= 0x100000UL && e820.map[i].addr < clip)
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73
				clip = e820.map[i].addr;
			continue;
		}
		start = e820.map[i].addr;
		end = e820.map[i].addr + e820.map[i].size;
		if (start >= end)
			continue;
		if (end > top)
			top = end;
	}
	/* Everything below 'top' should be RAM except for the ISA hole.
	   Because of the limited MCR's we want to map NV/ACPI into our
74 75
	   MCR range for gunk in RAM

L
Linus Torvalds 已提交
76 77 78
	   Clip might cause us to MCR insufficient RAM but that is an
	   acceptable failure mode and should only bite obscure boxes with
	   a VESA hole at 15Mb
79

L
Linus Torvalds 已提交
80 81 82
	   The second case Clip sometimes kicks in is when the EBDA is marked
	   as reserved. Again we fail safe with reasonable results
	*/
83 84 85 86

	if(top > clip)
		top = clip;

L
Linus Torvalds 已提交
87 88 89 90 91 92 93
	return top;
}

/*
 *	Compute a set of MCR's to give maximum coverage
 */

94
static int __cpuinit centaur_mcr_compute(int nr, int key)
L
Linus Torvalds 已提交
95 96 97 98 99 100 101
{
	u32 mem = ramtop();
	u32 root = power2(mem);
	u32 base = root;
	u32 top = root;
	u32 floor = 0;
	int ct = 0;
102 103

	while (ct < nr)
L
Linus Torvalds 已提交
104 105 106 107 108 109 110
	{
		u32 fspace = 0;

		/*
		 *	Find the largest block we will fill going upwards
		 */

111
		u32 high = power2(mem-top);
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121

		/*
		 *	Find the largest block we will fill going downwards
		 */

		u32 low = base/2;

		/*
		 *	Don't fill below 1Mb going downwards as there
		 *	is an ISA hole in the way.
122 123 124
		 */

		if (base <= 1024*1024)
L
Linus Torvalds 已提交
125
			low = 0;
126

L
Linus Torvalds 已提交
127 128 129 130
		/*
		 *	See how much space we could cover by filling below
		 *	the ISA hole
		 */
131 132

		if (floor == 0)
L
Linus Torvalds 已提交
133
			fspace = 512*1024;
134
		else if (floor == 512*1024)
L
Linus Torvalds 已提交
135 136 137
			fspace = 128*1024;

		/* And forget ROM space */
138

L
Linus Torvalds 已提交
139 140 141
		/*
		 *	Now install the largest coverage we get
		 */
142 143

		if (fspace > high && fspace > low)
L
Linus Torvalds 已提交
144 145 146 147
		{
			centaur_mcr_insert(ct, floor, fspace, key);
			floor += fspace;
		}
148
		else if (high > low) {
L
Linus Torvalds 已提交
149 150 151
			centaur_mcr_insert(ct, top, high, key);
			top += high;
		}
152
		else if (low > 0) {
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162
			base -= low;
			centaur_mcr_insert(ct, base, low, key);
		}
		else break;
		ct++;
	}
	/*
	 *	We loaded ct values. We now need to set the mask. The caller
	 *	must do this bit.
	 */
163

L
Linus Torvalds 已提交
164 165 166
	return ct;
}

167
static void __cpuinit centaur_create_optimal_mcr(void)
L
Linus Torvalds 已提交
168 169 170 171 172 173
{
	int i;
	/*
	 *	Allocate up to 6 mcrs to mark as much of ram as possible
	 *	as write combining and weak write ordered.
	 *
174
	 *	To experiment with: Linux never uses stack operations for
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184
	 *	mmio spaces so we could globally enable stack operation wc
	 *
	 *	Load the registers with type 31 - full write combining, all
	 *	writes weakly ordered.
	 */
	int used = centaur_mcr_compute(6, 31);

	/*
	 *	Wipe unused MCRs
	 */
185 186

	for (i = used; i < 8; i++)
L
Linus Torvalds 已提交
187 188 189
		wrmsr(MSR_IDT_MCR0+i, 0, 0);
}

190
static void __cpuinit winchip2_create_optimal_mcr(void)
L
Linus Torvalds 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
	u32 lo, hi;
	int i;

	/*
	 *	Allocate up to 6 mcrs to mark as much of ram as possible
	 *	as write combining, weak store ordered.
	 *
	 *	Load the registers with type 25
	 *		8	-	weak write ordering
	 *		16	-	weak read ordering
	 *		1	-	write combining
	 */

	int used = centaur_mcr_compute(6, 25);
206

L
Linus Torvalds 已提交
207 208 209
	/*
	 *	Mark the registers we are using.
	 */
210

L
Linus Torvalds 已提交
211
	rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
212 213
	for (i = 0; i < used; i++)
		lo |= 1<<(9+i);
L
Linus Torvalds 已提交
214
	wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
215

L
Linus Torvalds 已提交
216 217 218
	/*
	 *	Wipe unused MCRs
	 */
219 220

	for (i = used; i < 8; i++)
L
Linus Torvalds 已提交
221 222 223 224 225 226 227
		wrmsr(MSR_IDT_MCR0+i, 0, 0);
}

/*
 *	Handle the MCR key on the Winchip 2.
 */

228
static void __cpuinit winchip2_unprotect_mcr(void)
L
Linus Torvalds 已提交
229 230 231
{
	u32 lo, hi;
	u32 key;
232

L
Linus Torvalds 已提交
233
	rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
234
	lo &= ~0x1C0;	/* blank bits 8-6 */
L
Linus Torvalds 已提交
235 236 237 238 239
	key = (lo>>17) & 7;
	lo |= key<<6;	/* replace with unlock key */
	wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
}

240
static void __cpuinit winchip2_protect_mcr(void)
L
Linus Torvalds 已提交
241 242
{
	u32 lo, hi;
243

L
Linus Torvalds 已提交
244
	rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
245
	lo &= ~0x1C0;	/* blank bits 8-6 */
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257
	wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
}
#endif /* CONFIG_X86_OOSTORE */

#define ACE_PRESENT	(1 << 6)
#define ACE_ENABLED	(1 << 7)
#define ACE_FCR		(1 << 28)	/* MSR_VIA_FCR */

#define RNG_PRESENT	(1 << 2)
#define RNG_ENABLED	(1 << 3)
#define RNG_ENABLE	(1 << 6)	/* MSR_VIA_RNG */

258
static void __cpuinit init_c3(struct cpuinfo_x86 *c)
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267
{
	u32  lo, hi;

	/* Test for Centaur Extended Feature Flags presence */
	if (cpuid_eax(0xC0000000) >= 0xC0000001) {
		u32 tmp = cpuid_edx(0xC0000001);

		/* enable ACE unit, if present and disabled */
		if ((tmp & (ACE_PRESENT | ACE_ENABLED)) == ACE_PRESENT) {
268
			rdmsr(MSR_VIA_FCR, lo, hi);
L
Linus Torvalds 已提交
269
			lo |= ACE_FCR;		/* enable ACE unit */
270
			wrmsr(MSR_VIA_FCR, lo, hi);
L
Linus Torvalds 已提交
271 272 273 274 275
			printk(KERN_INFO "CPU: Enabled ACE h/w crypto\n");
		}

		/* enable RNG unit, if present and disabled */
		if ((tmp & (RNG_PRESENT | RNG_ENABLED)) == RNG_PRESENT) {
276
			rdmsr(MSR_VIA_RNG, lo, hi);
L
Linus Torvalds 已提交
277
			lo |= RNG_ENABLE;	/* enable RNG unit */
278
			wrmsr(MSR_VIA_RNG, lo, hi);
L
Linus Torvalds 已提交
279 280 281 282 283 284 285 286 287
			printk(KERN_INFO "CPU: Enabled h/w RNG\n");
		}

		/* store Centaur Extended Feature Flags as
		 * word 5 of the CPU capability bit array
		 */
		c->x86_capability[5] = cpuid_edx(0xC0000001);
	}

S
Simon Arlott 已提交
288
	/* Cyrix III family needs CX8 & PGE explicitly enabled. */
289 290
	if (c->x86_model >= 6 && c->x86_model <= 9) {
		rdmsr(MSR_VIA_FCR, lo, hi);
L
Linus Torvalds 已提交
291
		lo |= (1<<1 | 1<<7);
292
		wrmsr(MSR_VIA_FCR, lo, hi);
L
Linus Torvalds 已提交
293 294 295 296
		set_bit(X86_FEATURE_CX8, c->x86_capability);
	}

	/* Before Nehemiah, the C3's had 3dNOW! */
297
	if (c->x86_model >= 6 && c->x86_model < 9)
L
Linus Torvalds 已提交
298 299 300 301 302 303
		set_bit(X86_FEATURE_3DNOW, c->x86_capability);

	get_model_name(c);
	display_cacheinfo(c);
}

304
static void __cpuinit init_centaur(struct cpuinfo_x86 *c)
L
Linus Torvalds 已提交
305 306
{
	enum {
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		ECX8 = 1<<1,
		EIERRINT = 1<<2,
		DPM = 1<<3,
		DMCE = 1<<4,
		DSTPCLK = 1<<5,
		ELINEAR = 1<<6,
		DSMC = 1<<7,
		DTLOCK = 1<<8,
		EDCTLB = 1<<8,
		EMMX = 1<<9,
		DPDC = 1<<11,
		EBRPRED = 1<<12,
		DIC = 1<<13,
		DDC = 1<<14,
		DNA = 1<<15,
		ERETSTK = 1<<16,
		E2MMX = 1<<19,
		EAMD3D = 1<<20,
L
Linus Torvalds 已提交
325 326 327
	};

	char *name;
328 329 330 331
	u32  fcr_set = 0;
	u32  fcr_clr = 0;
	u32  lo, hi, newlo;
	u32  aa, bb, cc, dd;
L
Linus Torvalds 已提交
332 333 334 335 336 337 338

	/* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
	   3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
	clear_bit(0*32+31, c->x86_capability);

	switch (c->x86) {

339 340
	case 5:
			switch (c->x86_model) {
L
Linus Torvalds 已提交
341
			case 4:
342 343 344
				name = "C6";
				fcr_set = ECX8|DSMC|EDCTLB|EMMX|ERETSTK;
				fcr_clr = DPDC;
L
Linus Torvalds 已提交
345 346 347 348 349 350 351
				printk(KERN_NOTICE "Disabling bugged TSC.\n");
				clear_bit(X86_FEATURE_TSC, c->x86_capability);
#ifdef CONFIG_X86_OOSTORE
				centaur_create_optimal_mcr();
				/* Enable
					write combining on non-stack, non-string
					write combining on string, all types
352 353 354 355
					weak write ordering

				   The C6 original lacks weak read order

L
Linus Torvalds 已提交
356
				   Note 0x120 is write only on Winchip 1 */
357

L
Linus Torvalds 已提交
358
				wrmsr(MSR_IDT_MCR_CTRL, 0x01F0001F, 0);
359
#endif
L
Linus Torvalds 已提交
360 361
				break;
			case 8:
362
				switch (c->x86_mask) {
L
Linus Torvalds 已提交
363
				default:
364
					name = "2";
L
Linus Torvalds 已提交
365 366
					break;
				case 7 ... 9:
367
					name = "2A";
L
Linus Torvalds 已提交
368 369
					break;
				case 10 ... 15:
370
					name = "2B";
L
Linus Torvalds 已提交
371 372
					break;
				}
373 374
				fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|E2MMX|EAMD3D;
				fcr_clr = DPDC;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381
#ifdef CONFIG_X86_OOSTORE
				winchip2_unprotect_mcr();
				winchip2_create_optimal_mcr();
				rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
				/* Enable
					write combining on non-stack, non-string
					write combining on string, all types
382
					weak write ordering
L
Linus Torvalds 已提交
383
				*/
384
				lo |= 31;
L
Linus Torvalds 已提交
385 386 387 388 389
				wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
				winchip2_protect_mcr();
#endif
				break;
			case 9:
390 391 392
				name = "3";
				fcr_set = ECX8|DSMC|DTLOCK|EMMX|EBRPRED|ERETSTK|E2MMX|EAMD3D;
				fcr_clr = DPDC;
L
Linus Torvalds 已提交
393 394 395 396 397 398 399
#ifdef CONFIG_X86_OOSTORE
				winchip2_unprotect_mcr();
				winchip2_create_optimal_mcr();
				rdmsr(MSR_IDT_MCR_CTRL, lo, hi);
				/* Enable
					write combining on non-stack, non-string
					write combining on string, all types
400
					weak write ordering
L
Linus Torvalds 已提交
401
				*/
402
				lo |= 31;
L
Linus Torvalds 已提交
403 404 405 406 407
				wrmsr(MSR_IDT_MCR_CTRL, lo, hi);
				winchip2_protect_mcr();
#endif
				break;
			default:
408
				name = "??";
L
Linus Torvalds 已提交
409 410 411
			}

			rdmsr(MSR_IDT_FCR1, lo, hi);
412
			newlo = (lo|fcr_set) & (~fcr_clr);
L
Linus Torvalds 已提交
413

414 415 416
			if (newlo != lo) {
				printk(KERN_INFO "Centaur FCR was 0x%X now 0x%X\n", lo, newlo);
				wrmsr(MSR_IDT_FCR1, newlo, hi);
L
Linus Torvalds 已提交
417
			} else {
418
				printk(KERN_INFO "Centaur FCR is 0x%X\n", lo);
L
Linus Torvalds 已提交
419 420 421 422 423 424
			}
			/* Emulate MTRRs using Centaur's MCR. */
			set_bit(X86_FEATURE_CENTAUR_MCR, c->x86_capability);
			/* Report CX8 */
			set_bit(X86_FEATURE_CX8, c->x86_capability);
			/* Set 3DNow! on Winchip 2 and above. */
425
			if (c->x86_model >= 8)
L
Linus Torvalds 已提交
426 427
				set_bit(X86_FEATURE_3DNOW, c->x86_capability);
			/* See if we can find out some more. */
428
			if (cpuid_eax(0x80000000) >= 0x80000005) {
L
Linus Torvalds 已提交
429
				/* Yes, we can. */
430
				cpuid(0x80000005, &aa, &bb, &cc, &dd);
L
Linus Torvalds 已提交
431 432 433
				/* Add L1 data and code cache sizes. */
				c->x86_cache_size = (cc>>24)+(dd>>24);
			}
434
			sprintf(c->x86_model_id, "WinChip %s", name);
L
Linus Torvalds 已提交
435 436
			break;

437
	case 6:
L
Linus Torvalds 已提交
438 439 440 441 442
			init_c3(c);
			break;
	}
}

443
static unsigned int __cpuinit centaur_size_cache(struct cpuinfo_x86 *c, unsigned int size)
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451
{
	/* VIA C3 CPUs (670-68F) need further shifting. */
	if ((c->x86 == 6) && ((c->x86_model == 7) || (c->x86_model == 8)))
		size >>= 8;

	/* VIA also screwed up Nehemiah stepping 1, and made
	   it return '65KB' instead of '64KB'
	   - Note, it seems this may only be in engineering samples. */
452 453
	if ((c->x86 == 6) && (c->x86_model == 9) && (c->x86_mask == 1) && (size == 65))
		size -= 1;
L
Linus Torvalds 已提交
454 455 456 457

	return size;
}

458
static struct cpu_dev centaur_cpu_dev __cpuinitdata = {
L
Linus Torvalds 已提交
459 460 461 462 463 464
	.c_vendor	= "Centaur",
	.c_ident	= { "CentaurHauls" },
	.c_init		= init_centaur,
	.c_size_cache	= centaur_size_cache,
};

465
cpu_vendor_dev_register(X86_VENDOR_CENTAUR, &centaur_cpu_dev);