#include <linux/init.h>1#include <linux/mm.h>2#include <asm/mtrr.h>3#include <asm/msr.h>45#include "mtrr.h"67static void8amd_get_mtrr(unsigned int reg, unsigned long *base,9unsigned long *size, mtrr_type *type)10{11unsigned long low, high;1213rdmsr(MSR_K6_UWCCR, low, high);14/* Upper dword is region 1, lower is region 0 */15if (reg == 1)16low = high;17/* The base masks off on the right alignment */18*base = (low & 0xFFFE0000) >> PAGE_SHIFT;19*type = 0;20if (low & 1)21*type = MTRR_TYPE_UNCACHABLE;22if (low & 2)23*type = MTRR_TYPE_WRCOMB;24if (!(low & 3)) {25*size = 0;26return;27}28/*29* This needs a little explaining. The size is stored as an30* inverted mask of bits of 128K granularity 15 bits long offset31* 2 bits.32*33* So to get a size we do invert the mask and add 1 to the lowest34* mask bit (4 as its 2 bits in). This gives us a size we then shift35* to turn into 128K blocks.36*37* eg 111 1111 1111 1100 is 512K38*39* invert 000 0000 0000 001140* +1 000 0000 0000 010041* *128K ...42*/43low = (~low) & 0x1FFFC;44*size = (low + 4) << (15 - PAGE_SHIFT);45}4647/**48* amd_set_mtrr - Set variable MTRR register on the local CPU.49*50* @reg The register to set.51* @base The base address of the region.52* @size The size of the region. If this is 0 the region is disabled.53* @type The type of the region.54*55* Returns nothing.56*/57static void58amd_set_mtrr(unsigned int reg, unsigned long base, unsigned long size, mtrr_type type)59{60u32 regs[2];6162/*63* Low is MTRR0, High MTRR 164*/65rdmsr(MSR_K6_UWCCR, regs[0], regs[1]);66/*67* Blank to disable68*/69if (size == 0) {70regs[reg] = 0;71} else {72/*73* Set the register to the base, the type (off by one) and an74* inverted bitmask of the size The size is the only odd75* bit. We are fed say 512K We invert this and we get 111 111176* 1111 1011 but if you subtract one and invert you get the77* desired 111 1111 1111 1100 mask78*79* But ~(x - 1) == ~x + 1 == -x. Two's complement rocks!80*/81regs[reg] = (-size >> (15 - PAGE_SHIFT) & 0x0001FFFC)82| (base << PAGE_SHIFT) | (type + 1);83}8485/*86* The writeback rule is quite specific. See the manual. Its87* disable local interrupts, write back the cache, set the mtrr88*/89wbinvd();90wrmsr(MSR_K6_UWCCR, regs[0], regs[1]);91}9293static int94amd_validate_add_page(unsigned long base, unsigned long size, unsigned int type)95{96/*97* Apply the K6 block alignment and size rules98* In order99* o Uncached or gathering only100* o 128K or bigger block101* o Power of 2 block102* o base suitably aligned to the power103*/104if (type > MTRR_TYPE_WRCOMB || size < (1 << (17 - PAGE_SHIFT))105|| (size & ~(size - 1)) - size || (base & (size - 1)))106return -EINVAL;107return 0;108}109110static const struct mtrr_ops amd_mtrr_ops = {111.vendor = X86_VENDOR_AMD,112.set = amd_set_mtrr,113.get = amd_get_mtrr,114.get_free_region = generic_get_free_region,115.validate_add_page = amd_validate_add_page,116.have_wrcomb = positive_have_wrcomb,117};118119int __init amd_init_mtrr(void)120{121set_mtrr_ops(&amd_mtrr_ops);122return 0;123}124125126