^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) .. SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) =========================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) MTRR (Memory Type Range Register) control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) =========================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) :Authors: - Richard Gooch <rgooch@atnf.csiro.au> - 3 Jun 1999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) - Luis R. Rodriguez <mcgrof@do-not-panic.com> - April 9, 2015
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) Phasing out MTRR use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) ====================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) MTRR use is replaced on modern x86 hardware with PAT. Direct MTRR use by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) drivers on Linux is now completely phased out, device drivers should use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) arch_phys_wc_add() in combination with ioremap_wc() to make MTRR effective on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) non-PAT systems while a no-op but equally effective on PAT enabled systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) Even if Linux does not use MTRRs directly, some x86 platform firmware may still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) set up MTRRs early before booting the OS. They do this as some platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) firmware may still have implemented access to MTRRs which would be controlled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) and handled by the platform firmware directly. An example of platform use of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MTRRs is through the use of SMI handlers, one case could be for fan control,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) the platform code would need uncachable access to some of its fan control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) registers. Such platform access does not need any Operating System MTRR code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) place other than mtrr_type_lookup() to ensure any OS specific mapping requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) are aligned with platform MTRR setup. If MTRRs are only set up by the platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) firmware code though and the OS does not make any specific MTRR mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) requests mtrr_type_lookup() should always return MTRR_TYPE_INVALID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) For details refer to :doc:`pat`.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .. tip::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) On Intel P6 family processors (Pentium Pro, Pentium II and later)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) the Memory Type Range Registers (MTRRs) may be used to control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) processor access to memory ranges. This is most useful when you have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) a video (VGA) card on a PCI or AGP bus. Enabling write-combining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) allows bus write transfers to be combined into a larger transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) before bursting over the PCI/AGP bus. This can increase performance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) of image write operations 2.5 times or more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) The Cyrix 6x86, 6x86MX and M II processors have Address Range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) Registers (ARRs) which provide a similar functionality to MTRRs. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) these, the ARRs are used to emulate the MTRRs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) The AMD K6-2 (stepping 8 and above) and K6-3 processors have two
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) MTRRs. These are supported. The AMD Athlon family provide 8 Intel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) style MTRRs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) The Centaur C6 (WinChip) has 8 MCRs, allowing write-combining. These
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) are supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) The VIA Cyrix III and VIA C3 CPUs offer 8 Intel style MTRRs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) The CONFIG_MTRR option creates a /proc/mtrr file which may be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) to manipulate your MTRRs. Typically the X server should use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) this. This should have a reasonably generic interface so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) similar control registers on other processors can be easily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) There are two interfaces to /proc/mtrr: one is an ASCII interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) which allows you to read and write. The other is an ioctl()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) interface. The ASCII interface is meant for administration. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ioctl() interface is meant for C programs (i.e. the X server). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) interfaces are described below, with sample commands and C code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) Reading MTRRs from the shell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) % cat /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) reg00: base=0x00000000 ( 0MB), size= 128MB: write-back, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) reg01: base=0x08000000 ( 128MB), size= 64MB: write-back, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) Creating MTRRs from the C-shell::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) # echo "base=0xf8000000 size=0x400000 type=write-combining" >! /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) or if you use bash::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) # echo "base=0xf8000000 size=0x400000 type=write-combining" >| /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) And the result thereof::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) % cat /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) reg00: base=0x00000000 ( 0MB), size= 128MB: write-back, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) reg01: base=0x08000000 ( 128MB), size= 64MB: write-back, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) reg02: base=0xf8000000 (3968MB), size= 4MB: write-combining, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) This is for video RAM at base address 0xf8000000 and size 4 megabytes. To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) find out your base address, you need to look at the output of your X
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) server, which tells you where the linear framebuffer address is. A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) typical line that you may get is::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) (--) S3: PCI: 968 rev 0, Linear FB @ 0xf8000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) Note that you should only use the value from the X server, as it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) move the framebuffer base address, so the only value you can trust is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) that reported by the X server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) To find out the size of your framebuffer (what, you don't actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) know?), the following line will tell you::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (--) S3: videoram: 4096k
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) That's 4 megabytes, which is 0x400000 bytes (in hexadecimal).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) A patch is being written for XFree86 which will make this automatic:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) in other words the X server will manipulate /proc/mtrr using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ioctl() interface, so users won't have to do anything. If you use a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) commercial X server, lobby your vendor to add support for MTRRs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) Creating overlapping MTRRs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ==========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) %echo "base=0xfb000000 size=0x1000000 type=write-combining" >/proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) %echo "base=0xfb000000 size=0x1000 type=uncachable" >/proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) And the results::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) % cat /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) reg00: base=0x00000000 ( 0MB), size= 64MB: write-back, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) reg01: base=0xfb000000 (4016MB), size= 16MB: write-combining, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) reg02: base=0xfb000000 (4016MB), size= 4kB: uncachable, count=1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) Some cards (especially Voodoo Graphics boards) need this 4 kB area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) excluded from the beginning of the region because it is used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) NOTE: You can only create type=uncachable region, if the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) region that you created is type=write-combining.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) Removing MTRRs from the C-shel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ==============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) % echo "disable=2" >! /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) or using bash::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) % echo "disable=2" >| /proc/mtrr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) Reading MTRRs from a C program using ioctl()'s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ==============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* mtrr-show.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) Source file for mtrr-show (example program to show MTRRs using ioctl()'s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) Copyright (C) 1997-1998 Richard Gooch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) Richard Gooch may be reached by email at rgooch@atnf.csiro.au
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) The postal address is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) This program will use an ioctl() on /proc/mtrr to show the current MTRR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) settings. This is an alternative to reading /proc/mtrr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) Written by Richard Gooch 17-DEC-1997
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) Last updated by Richard Gooch 2-MAY-1998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #include <asm/mtrr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #define TRUE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #define FALSE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #define ERRSTRING strerror (errno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static char *mtrr_strings[MTRR_NUM_TYPES] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) "uncachable", /* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) "write-combining", /* 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) "?", /* 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) "?", /* 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) "write-through", /* 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) "write-protect", /* 5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) "write-back", /* 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) int main ()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct mtrr_gentry gentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if ( ( fd = open ("/proc/mtrr", O_RDONLY, 0) ) == -1 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) exit (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) exit (2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) for (gentry.regnum = 0; ioctl (fd, MTRRIOC_GET_ENTRY, &gentry) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ++gentry.regnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (gentry.size < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) fprintf (stderr, "Register: %u disabled\n", gentry.regnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) fprintf (stderr, "Register: %u base: 0x%lx size: 0x%lx type: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) gentry.regnum, gentry.base, gentry.size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) mtrr_strings[gentry.type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (errno == EINVAL) exit (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) exit (3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) } /* End Function main */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) Creating MTRRs from a C programme using ioctl()'s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) =================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /* mtrr-add.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) Source file for mtrr-add (example programme to add an MTRRs using ioctl())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) Copyright (C) 1997-1998 Richard Gooch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) along with this program; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) Richard Gooch may be reached by email at rgooch@atnf.csiro.au
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) The postal address is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) This programme will use an ioctl() on /proc/mtrr to add an entry. The first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) available mtrr is used. This is an alternative to writing /proc/mtrr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) Written by Richard Gooch 17-DEC-1997
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) Last updated by Richard Gooch 2-MAY-1998
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) #include <asm/mtrr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #define TRUE 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) #define FALSE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #define ERRSTRING strerror (errno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static char *mtrr_strings[MTRR_NUM_TYPES] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) "uncachable", /* 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) "write-combining", /* 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) "?", /* 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) "?", /* 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) "write-through", /* 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) "write-protect", /* 5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) "write-back", /* 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) int main (int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct mtrr_sentry sentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (argc != 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) fprintf (stderr, "Usage:\tmtrr-add base size type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) exit (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) sentry.base = strtoul (argv[1], NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) sentry.size = strtoul (argv[2], NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) for (sentry.type = 0; sentry.type < MTRR_NUM_TYPES; ++sentry.type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (strcmp (argv[3], mtrr_strings[sentry.type]) == 0) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (sentry.type >= MTRR_NUM_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) fprintf (stderr, "Illegal type: \"%s\"\n", argv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) exit (2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if ( ( fd = open ("/proc/mtrr", O_WRONLY, 0) ) == -1 )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (errno == ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) exit (3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) exit (4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (ioctl (fd, MTRRIOC_ADD_ENTRY, &sentry) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) exit (5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) fprintf (stderr, "Sleeping for 5 seconds so you can see the new entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) sleep (5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) close (fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) fputs ("I've just closed /proc/mtrr so now the new entry should be gone\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) } /* End Function main */