1/* SPDX-License-Identifier: LGPL-2.0+ */
2/* Generic MTRR (Memory Type Range Register) ioctls.
3
4 Copyright (C) 1997-1999 Richard Gooch
5
6 Richard Gooch may be reached by email at rgooch@atnf.csiro.au
7 The postal address is:
8 Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
9*/
10#ifndef _ASM_X86_MTRR_H
11#define _ASM_X86_MTRR_H
12
13#include <linux/bits.h>
14#include <uapi/asm/mtrr.h>
15
16/* Defines for hardware MTRR registers. */
17#define MTRR_CAP_VCNT GENMASK(7, 0)
18#define MTRR_CAP_FIX BIT_MASK(8)
19#define MTRR_CAP_WC BIT_MASK(10)
20
21#define MTRR_DEF_TYPE_TYPE GENMASK(7, 0)
22#define MTRR_DEF_TYPE_FE BIT_MASK(10)
23#define MTRR_DEF_TYPE_E BIT_MASK(11)
24
25#define MTRR_DEF_TYPE_ENABLE (MTRR_DEF_TYPE_FE | MTRR_DEF_TYPE_E)
26#define MTRR_DEF_TYPE_DISABLE ~(MTRR_DEF_TYPE_TYPE | MTRR_DEF_TYPE_ENABLE)
27
28#define MTRR_PHYSBASE_TYPE GENMASK(7, 0)
29#define MTRR_PHYSBASE_RSVD GENMASK(11, 8)
30
31#define MTRR_PHYSMASK_RSVD GENMASK(10, 0)
32#define MTRR_PHYSMASK_V BIT_MASK(11)
33
34struct mtrr_state_type {
35 struct mtrr_var_range var_ranges[MTRR_MAX_VAR_RANGES];
36 mtrr_type fixed_ranges[MTRR_NUM_FIXED_RANGES];
37 unsigned char enabled;
38 bool have_fixed;
39 mtrr_type def_type;
40};
41
42/*
43 * The following functions are for use by other drivers that cannot use
44 * arch_phys_wc_add and arch_phys_wc_del.
45 */
46# ifdef CONFIG_MTRR
47void mtrr_bp_init(void);
48void guest_force_mtrr_state(struct mtrr_var_range *var, unsigned int num_var,
49 mtrr_type def_type);
50extern u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform);
51extern void mtrr_save_fixed_ranges(void *);
52extern void mtrr_save_state(void);
53extern int mtrr_add(unsigned long base, unsigned long size,
54 unsigned int type, bool increment);
55extern int mtrr_add_page(unsigned long base, unsigned long size,
56 unsigned int type, bool increment);
57extern int mtrr_del(int reg, unsigned long base, unsigned long size);
58extern int mtrr_del_page(int reg, unsigned long base, unsigned long size);
59extern int mtrr_trim_uncached_memory(unsigned long end_pfn);
60extern int amd_special_default_mtrr(void);
61void mtrr_disable(void);
62void mtrr_enable(void);
63void mtrr_generic_set_state(void);
64# else
65static inline void guest_force_mtrr_state(struct mtrr_var_range *var,
66 unsigned int num_var,
67 mtrr_type def_type)
68{
69}
70
71static inline u8 mtrr_type_lookup(u64 addr, u64 end, u8 *uniform)
72{
73 /*
74 * Return the default MTRR type, without any known other types in
75 * that range.
76 */
77 *uniform = 1;
78
79 return MTRR_TYPE_UNCACHABLE;
80}
81#define mtrr_save_fixed_ranges(arg) do {} while (0)
82#define mtrr_save_state() do {} while (0)
83static inline int mtrr_add(unsigned long base, unsigned long size,
84 unsigned int type, bool increment)
85{
86 return -ENODEV;
87}
88static inline int mtrr_add_page(unsigned long base, unsigned long size,
89 unsigned int type, bool increment)
90{
91 return -ENODEV;
92}
93static inline int mtrr_del(int reg, unsigned long base, unsigned long size)
94{
95 return -ENODEV;
96}
97static inline int mtrr_del_page(int reg, unsigned long base, unsigned long size)
98{
99 return -ENODEV;
100}
101static inline int mtrr_trim_uncached_memory(unsigned long end_pfn)
102{
103 return 0;
104}
105#define mtrr_bp_init() do {} while (0)
106#define mtrr_disable() do {} while (0)
107#define mtrr_enable() do {} while (0)
108#define mtrr_generic_set_state() do {} while (0)
109# endif
110
111#ifdef CONFIG_COMPAT
112#include <linux/compat.h>
113
114struct mtrr_sentry32 {
115 compat_ulong_t base; /* Base address */
116 compat_uint_t size; /* Size of region */
117 compat_uint_t type; /* Type of region */
118};
119
120struct mtrr_gentry32 {
121 compat_ulong_t regnum; /* Register number */
122 compat_uint_t base; /* Base address */
123 compat_uint_t size; /* Size of region */
124 compat_uint_t type; /* Type of region */
125};
126
127#define MTRR_IOCTL_BASE 'M'
128
129#define MTRRIOC32_ADD_ENTRY _IOW(MTRR_IOCTL_BASE, 0, struct mtrr_sentry32)
130#define MTRRIOC32_SET_ENTRY _IOW(MTRR_IOCTL_BASE, 1, struct mtrr_sentry32)
131#define MTRRIOC32_DEL_ENTRY _IOW(MTRR_IOCTL_BASE, 2, struct mtrr_sentry32)
132#define MTRRIOC32_GET_ENTRY _IOWR(MTRR_IOCTL_BASE, 3, struct mtrr_gentry32)
133#define MTRRIOC32_KILL_ENTRY _IOW(MTRR_IOCTL_BASE, 4, struct mtrr_sentry32)
134#define MTRRIOC32_ADD_PAGE_ENTRY _IOW(MTRR_IOCTL_BASE, 5, struct mtrr_sentry32)
135#define MTRRIOC32_SET_PAGE_ENTRY _IOW(MTRR_IOCTL_BASE, 6, struct mtrr_sentry32)
136#define MTRRIOC32_DEL_PAGE_ENTRY _IOW(MTRR_IOCTL_BASE, 7, struct mtrr_sentry32)
137#define MTRRIOC32_GET_PAGE_ENTRY _IOWR(MTRR_IOCTL_BASE, 8, struct mtrr_gentry32)
138#define MTRRIOC32_KILL_PAGE_ENTRY \
139 _IOW(MTRR_IOCTL_BASE, 9, struct mtrr_sentry32)
140#endif /* CONFIG_COMPAT */
141
142/* Bit fields for enabled in struct mtrr_state_type */
143#define MTRR_STATE_SHIFT 10
144#define MTRR_STATE_MTRR_FIXED_ENABLED (MTRR_DEF_TYPE_FE >> MTRR_STATE_SHIFT)
145#define MTRR_STATE_MTRR_ENABLED (MTRR_DEF_TYPE_E >> MTRR_STATE_SHIFT)
146
147#endif /* _ASM_X86_MTRR_H */
148