Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/init.h>
  3#include <linux/mm.h>
  4
  5#include <asm/mtrr.h>
  6#include <asm/msr.h>
  7
  8#include "mtrr.h"
  9
 10static struct {
 11	unsigned long high;
 12	unsigned long low;
 13} centaur_mcr[8];
 14
 15static u8 centaur_mcr_reserved;
 16static u8 centaur_mcr_type;	/* 0 for winchip, 1 for winchip2 */
 17
 18/**
 19 * centaur_get_free_region - Get a free MTRR.
 20 *
 21 * @base: The starting (base) address of the region.
 22 * @size: The size (in bytes) of the region.
 23 *
 24 * Returns: the index of the region on success, else -1 on error.
 25 */
 26static int
 27centaur_get_free_region(unsigned long base, unsigned long size, int replace_reg)
 28{
 29	unsigned long lbase, lsize;
 30	mtrr_type ltype;
 31	int i, max;
 32
 33	max = num_var_ranges;
 34	if (replace_reg >= 0 && replace_reg < max)
 35		return replace_reg;
 36
 37	for (i = 0; i < max; ++i) {
 38		if (centaur_mcr_reserved & (1 << i))
 39			continue;
 40		mtrr_if->get(i, &lbase, &lsize, &ltype);
 41		if (lsize == 0)
 42			return i;
 43	}
 44
 45	return -ENOSPC;
 46}
 47
 48/*
 49 * Report boot time MCR setups
 50 */
 51void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)
 52{
 53	centaur_mcr[mcr].low = lo;
 54	centaur_mcr[mcr].high = hi;
 55}
 56
 57static void
 58centaur_get_mcr(unsigned int reg, unsigned long *base,
 59		unsigned long *size, mtrr_type * type)
 60{
 61	*base = centaur_mcr[reg].high >> PAGE_SHIFT;
 62	*size = -(centaur_mcr[reg].low & 0xfffff000) >> PAGE_SHIFT;
 63	*type = MTRR_TYPE_WRCOMB;		/* write-combining  */
 64
 65	if (centaur_mcr_type == 1 && ((centaur_mcr[reg].low & 31) & 2))
 66		*type = MTRR_TYPE_UNCACHABLE;
 67	if (centaur_mcr_type == 1 && (centaur_mcr[reg].low & 31) == 25)
 68		*type = MTRR_TYPE_WRBACK;
 69	if (centaur_mcr_type == 0 && (centaur_mcr[reg].low & 31) == 31)
 70		*type = MTRR_TYPE_WRBACK;
 71}
 72
 73static void
 74centaur_set_mcr(unsigned int reg, unsigned long base,
 75		unsigned long size, mtrr_type type)
 76{
 77	unsigned long low, high;
 78
 79	if (size == 0) {
 80		/* Disable */
 81		high = low = 0;
 82	} else {
 83		high = base << PAGE_SHIFT;
 84		if (centaur_mcr_type == 0) {
 85			/* Only support write-combining... */
 86			low = -size << PAGE_SHIFT | 0x1f;
 87		} else {
 88			if (type == MTRR_TYPE_UNCACHABLE)
 89				low = -size << PAGE_SHIFT | 0x02; /* NC */
 90			else
 91				low = -size << PAGE_SHIFT | 0x09; /* WWO, WC */
 92		}
 93	}
 94	centaur_mcr[reg].high = high;
 95	centaur_mcr[reg].low = low;
 96	wrmsr(MSR_IDT_MCR0 + reg, low, high);
 97}
 98
 99static int
100centaur_validate_add_page(unsigned long base, unsigned long size, unsigned int type)
101{
102	/*
103	 * FIXME: Winchip2 supports uncached
104	 */
105	if (type != MTRR_TYPE_WRCOMB &&
106	    (centaur_mcr_type == 0 || type != MTRR_TYPE_UNCACHABLE)) {
107		pr_warn("mtrr: only write-combining%s supported\n",
108			   centaur_mcr_type ? " and uncacheable are" : " is");
109		return -EINVAL;
110	}
111	return 0;
112}
113
114static const struct mtrr_ops centaur_mtrr_ops = {
115	.vendor            = X86_VENDOR_CENTAUR,
116	.set               = centaur_set_mcr,
117	.get               = centaur_get_mcr,
118	.get_free_region   = centaur_get_free_region,
119	.validate_add_page = centaur_validate_add_page,
120	.have_wrcomb       = positive_have_wrcomb,
121};
122
123int __init centaur_init_mtrr(void)
124{
125	set_mtrr_ops(&centaur_mtrr_ops);
126	return 0;
127}
v3.5.6
 
  1#include <linux/init.h>
  2#include <linux/mm.h>
  3
  4#include <asm/mtrr.h>
  5#include <asm/msr.h>
  6
  7#include "mtrr.h"
  8
  9static struct {
 10	unsigned long high;
 11	unsigned long low;
 12} centaur_mcr[8];
 13
 14static u8 centaur_mcr_reserved;
 15static u8 centaur_mcr_type;	/* 0 for winchip, 1 for winchip2 */
 16
 17/**
 18 * centaur_get_free_region - Get a free MTRR.
 19 *
 20 * @base: The starting (base) address of the region.
 21 * @size: The size (in bytes) of the region.
 22 *
 23 * Returns: the index of the region on success, else -1 on error.
 24 */
 25static int
 26centaur_get_free_region(unsigned long base, unsigned long size, int replace_reg)
 27{
 28	unsigned long lbase, lsize;
 29	mtrr_type ltype;
 30	int i, max;
 31
 32	max = num_var_ranges;
 33	if (replace_reg >= 0 && replace_reg < max)
 34		return replace_reg;
 35
 36	for (i = 0; i < max; ++i) {
 37		if (centaur_mcr_reserved & (1 << i))
 38			continue;
 39		mtrr_if->get(i, &lbase, &lsize, &ltype);
 40		if (lsize == 0)
 41			return i;
 42	}
 43
 44	return -ENOSPC;
 45}
 46
 47/*
 48 * Report boot time MCR setups
 49 */
 50void mtrr_centaur_report_mcr(int mcr, u32 lo, u32 hi)
 51{
 52	centaur_mcr[mcr].low = lo;
 53	centaur_mcr[mcr].high = hi;
 54}
 55
 56static void
 57centaur_get_mcr(unsigned int reg, unsigned long *base,
 58		unsigned long *size, mtrr_type * type)
 59{
 60	*base = centaur_mcr[reg].high >> PAGE_SHIFT;
 61	*size = -(centaur_mcr[reg].low & 0xfffff000) >> PAGE_SHIFT;
 62	*type = MTRR_TYPE_WRCOMB;		/* write-combining  */
 63
 64	if (centaur_mcr_type == 1 && ((centaur_mcr[reg].low & 31) & 2))
 65		*type = MTRR_TYPE_UNCACHABLE;
 66	if (centaur_mcr_type == 1 && (centaur_mcr[reg].low & 31) == 25)
 67		*type = MTRR_TYPE_WRBACK;
 68	if (centaur_mcr_type == 0 && (centaur_mcr[reg].low & 31) == 31)
 69		*type = MTRR_TYPE_WRBACK;
 70}
 71
 72static void
 73centaur_set_mcr(unsigned int reg, unsigned long base,
 74		unsigned long size, mtrr_type type)
 75{
 76	unsigned long low, high;
 77
 78	if (size == 0) {
 79		/* Disable */
 80		high = low = 0;
 81	} else {
 82		high = base << PAGE_SHIFT;
 83		if (centaur_mcr_type == 0) {
 84			/* Only support write-combining... */
 85			low = -size << PAGE_SHIFT | 0x1f;
 86		} else {
 87			if (type == MTRR_TYPE_UNCACHABLE)
 88				low = -size << PAGE_SHIFT | 0x02; /* NC */
 89			else
 90				low = -size << PAGE_SHIFT | 0x09; /* WWO, WC */
 91		}
 92	}
 93	centaur_mcr[reg].high = high;
 94	centaur_mcr[reg].low = low;
 95	wrmsr(MSR_IDT_MCR0 + reg, low, high);
 96}
 97
 98static int
 99centaur_validate_add_page(unsigned long base, unsigned long size, unsigned int type)
100{
101	/*
102	 * FIXME: Winchip2 supports uncached
103	 */
104	if (type != MTRR_TYPE_WRCOMB &&
105	    (centaur_mcr_type == 0 || type != MTRR_TYPE_UNCACHABLE)) {
106		pr_warning("mtrr: only write-combining%s supported\n",
107			   centaur_mcr_type ? " and uncacheable are" : " is");
108		return -EINVAL;
109	}
110	return 0;
111}
112
113static const struct mtrr_ops centaur_mtrr_ops = {
114	.vendor            = X86_VENDOR_CENTAUR,
115	.set               = centaur_set_mcr,
116	.get               = centaur_get_mcr,
117	.get_free_region   = centaur_get_free_region,
118	.validate_add_page = centaur_validate_add_page,
119	.have_wrcomb       = positive_have_wrcomb,
120};
121
122int __init centaur_init_mtrr(void)
123{
124	set_mtrr_ops(&centaur_mtrr_ops);
125	return 0;
126}