arch/x86/kernel/cpu/cacheinfo.c

Source file repositories/reference/linux-study-clean/arch/x86/kernel/cpu/cacheinfo.c

File Facts

System
Linux kernel
Corpus path
arch/x86/kernel/cpu/cacheinfo.c
Extension
.c
Size
20375 bytes
Lines
821
Domain
Architecture Layer
Bucket
arch/x86
Inferred role
Architecture Layer: implementation source
Status
source implementation candidate

Why This File Exists

CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.

Dependency Surface

Detected Declarations

Annotated Snippet

struct _cpuid4_info {
	union _cpuid4_leaf_eax eax;
	union _cpuid4_leaf_ebx ebx;
	union _cpuid4_leaf_ecx ecx;
	unsigned int id;
	unsigned long size;
};

/* Map CPUID(0x4) EAX.cache_type to <linux/cacheinfo.h> types */
static const enum cache_type cache_type_map[] = {
	[CTYPE_NULL]	= CACHE_TYPE_NOCACHE,
	[CTYPE_DATA]	= CACHE_TYPE_DATA,
	[CTYPE_INST]	= CACHE_TYPE_INST,
	[CTYPE_UNIFIED] = CACHE_TYPE_UNIFIED,
};

/*
 * Fallback AMD CPUID(0x4) emulation
 * AMD CPUs with TOPOEXT can just use CPUID(0x8000001d)
 *
 * @AMD_L2_L3_INVALID_ASSOC: cache info for the respective L2/L3 cache should
 * be determined from CPUID(0x8000001d) instead of CPUID(0x80000006).
 */

#define AMD_CPUID4_FULLY_ASSOCIATIVE	0xffff
#define AMD_L2_L3_INVALID_ASSOC		0x9

union l1_cache {
	struct {
		unsigned line_size	:8;
		unsigned lines_per_tag	:8;
		unsigned assoc		:8;
		unsigned size_in_kb	:8;
	};
	unsigned int val;
};

union l2_cache {
	struct {
		unsigned line_size	:8;
		unsigned lines_per_tag	:4;
		unsigned assoc		:4;
		unsigned size_in_kb	:16;
	};
	unsigned int val;
};

union l3_cache {
	struct {
		unsigned line_size	:8;
		unsigned lines_per_tag	:4;
		unsigned assoc		:4;
		unsigned res		:2;
		unsigned size_encoded	:14;
	};
	unsigned int val;
};

/* L2/L3 associativity mapping */
static const unsigned short assocs[] = {
	[1]		= 1,
	[2]		= 2,
	[3]		= 3,
	[4]		= 4,
	[5]		= 6,
	[6]		= 8,
	[8]		= 16,
	[0xa]		= 32,
	[0xb]		= 48,
	[0xc]		= 64,
	[0xd]		= 96,
	[0xe]		= 128,
	[0xf]		= AMD_CPUID4_FULLY_ASSOCIATIVE
};

static const unsigned char levels[] = { 1, 1, 2, 3 };
static const unsigned char types[]  = { 1, 2, 3, 3 };

static void legacy_amd_cpuid4(int index, union _cpuid4_leaf_eax *eax,
			      union _cpuid4_leaf_ebx *ebx, union _cpuid4_leaf_ecx *ecx)
{
	unsigned int dummy, line_size, lines_per_tag, assoc, size_in_kb;
	union l1_cache l1i, l1d, *l1;
	union l2_cache l2;
	union l3_cache l3;

	eax->full = 0;
	ebx->full = 0;
	ecx->full = 0;

Annotation

Implementation Notes