summaryrefslogtreecommitdiff
path: root/sys/arch/amd64/include/mtrr.h
blob: 4cf4376a9db64286d31859ad7730c446e942acc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*	$OpenBSD: mtrr.h,v 1.2 2008/06/26 05:42:09 ray Exp $	*/
/* $NetBSD: mtrr.h,v 1.1 2003/02/26 21:26:11 fvdl Exp $ */

/*-
 * Copyright (c) 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Bill Sommerfeld
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _X86_MTRR_H_
#define _X86_MTRR_H_

#define MTRR_I686_FIXED_IDX64K	0
#define MTRR_I686_FIXED_IDX16K	1
#define MTRR_I686_FIXED_IDX4K	3

#define MTRR_I686_NVAR		8

#define MTRR_I686_64K_START		0x00000
#define MTRR_I686_16K_START		0x80000
#define MTRR_I686_4K_START		0xc0000

#define MTRR_I686_NFIXED_64K		1
#define MTRR_I686_NFIXED_16K		2
#define MTRR_I686_NFIXED_4K		8
#define MTRR_I686_NFIXED		11
#define MTRR_I686_NFIXED_SOFT_64K	(MTRR_I686_NFIXED_64K * 8)
#define MTRR_I686_NFIXED_SOFT_16K	(MTRR_I686_NFIXED_16K * 8)
#define MTRR_I686_NFIXED_SOFT_4K	(MTRR_I686_NFIXED_4K * 8)
#define MTRR_I686_NFIXED_SOFT		(MTRR_I686_NFIXED * 8)

#define MTRR_I686_ENABLE_MASK	0x0800
#define MTRR_I686_FIXED_ENABLE_MASK	0x0400

#define MTRR_I686_CAP_VCNT_MASK	0x00ff
#define MTRR_I686_CAP_FIX_MASK	0x0100
#define MTRR_I686_CAP_WC_MASK	0x0400

#define MTRR_TYPE_UC		0
#define MTRR_TYPE_WC		1
#define MTRR_TYPE_UNDEF1	2
#define MTRR_TYPE_UNDEF2	3
#define MTRR_TYPE_WT		4
#define MTRR_TYPE_WP		5
#define MTRR_TYPE_WB		6

struct mtrr_state {
	uint32_t msraddr;
	uint64_t msrval;
};
	
#define MTRR_PRIVATE	0x0001		/* 'own' range, reset at exit */
#define MTRR_FIXED	0x0002		/* use fixed range mtrr */
#define MTRR_VALID	0x0004		/* entry is valid */

#define MTRR_CANTSET	MTRR_FIXED

#define MTRR_I686_MASK_VALID	(1 << 11)

/*
 * AMD K6 MTRRs.
 *
 * There are two of these MTRR-like registers in the UWCRR.
 */

#define	MTRR_K6_ADDR_SHIFT 17
#define	MTRR_K6_ADDR	(0x7fffU << MTRR_K6_ADDR_SHIFT)
#define	MTRR_K6_MASK_SHIFT 2
#define	MTRR_K6_MASK	(0x7fffU << MTRR_K6_MASK_SHIFT)
#define	MTRR_K6_WC	(1U << 1)	/* write-combine */
#define	MTRR_K6_UC	(1U << 0)	/* uncached */

#define	MTRR_K6_NVAR	2

#ifdef _KERNEL

#define mtrr_base_value(mtrrp) \
    (((uint64_t)(mtrrp)->base) | ((uint64_t)(mtrrp)->type))
#define mtrr_mask_value(mtrrp) \
    ((~((mtrrp)->len - 1) & 0x0000000ffffff000))
	

#define mtrr_len(val) \
    ((~((val) & 0x0000000ffffff000)+1) & 0x0000000ffffff000)
#define mtrr_base(val)		((val) & 0x0000000ffffff000)
#define mtrr_type(val)		((uint8_t)((val) & 0x00000000000000ff))
#define mtrr_valid(val)		(((val) & MTRR_I686_MASK_VALID) != 0)

struct proc;
struct mtrr;

void i686_mtrr_init_first(void);
void k6_mtrr_init_first(void);

struct mtrr_funcs {
	void (*init_cpu)(struct cpu_info *ci);
	void (*reload_cpu)(struct cpu_info *ci);
	void (*clean)(struct proc *p);
	int (*set)(struct mtrr *, int *n, struct proc *p, int flags);
	int (*get)(struct mtrr *, int *n, struct proc *p, int flags);
	void (*commit)(void);
	void (*dump)(const char *tag);
};

extern struct mtrr_funcs i686_mtrr_funcs;
extern struct mtrr_funcs k6_mtrr_funcs;
extern struct mtrr_funcs *mtrr_funcs;

#define mtrr_init_cpu(ci)	mtrr_funcs->init_cpu(ci)
#define mtrr_reload_cpu(ci)	mtrr_funcs->reload_cpu(ci)
#define mtrr_clean(p)		mtrr_funcs->clean(p)
#define mtrr_set(mp,n,p,f)	mtrr_funcs->set(mp,n,p,f)
#define mtrr_get(mp,n,p,f)	mtrr_funcs->get(mp,n,p,f)
#define mtrr_dump(s)		mtrr_funcs->dump(s)
#define mtrr_commit()		mtrr_funcs->commit()

#define MTRR_GETSET_USER	0x0001
#define MTRR_GETSET_KERNEL	0x0002

#endif /* _KERNEL */

struct mtrr {
	uint64_t base;		/* physical base address */
	uint64_t len;
	uint8_t type;
	int flags;
	pid_t owner;		/* valid if MTRR_PRIVATE set in flags */
};

#endif /* _X86_MTRR_H_ */