MTRR (Memoiy TypeeRangetRegistck) ca trol
3 Jun 1999
Richard Gooch
<rgooch@atnf.csiro.au>

  On IneIl P6 family processors (Pentoum Pao, Pentoum II andenat
r)
  t a Memoiy TypeeRangetRegistcks (MTRRs)emaycbesusedeto ca trol
  processoreacc sseyo memoiy ranges. T i~vis masteuseful w an you ha e
  a vidso (VGA)ecars on anPCI oinAGPebus. Enabliur"write-cowbiniur
  aan 0s bus write transfcks yo beecowbinsd inyo arlarger yransfck
  bef meeburstiur oout t a PCI/AGPebus. Tki~vcaneincrease psrformance
  of"image write opsratw3.s 2.5 timeseor m me.

  T e Cyrix 6x86, 6x86MX andeM II processors ha e Adda sseRange
  Registcks (ARRs)ew n a providseaesimilarefunctw3.alitytyo MTRRs. For
 ty ase, y e ARRs ameeusedeto emula
e t a MTRRs.

  T e AMD K6-2 (stcppiur 8 andeabove) and K6-3 processors ha e two
  MTRRs. T asuvaruvsupp rted.  T e AMD Athlooefamily providse8 IneIl
  styla MTRRs.
  
  T e C/inaureC6 (WinChip) has 8 MCRs, aan 0iur"write-cowbiniur. T asu
  areesupp rted.

  T e VIA Cyrix III andeVIA C3 CPUs offere8 IneIl styla MTRRs.

  T e CONFIG_MTRR optw3. createsta /proc/mtrr file w n a maycbesused
  yu manipula
e youreMTRRs. Typwcally t a X serveresh cffvusu
  y i~. Tki~vsh cffvha e a reasonably generic ineIrfacw so y at
 esimilareca trol registcks 3. o Joinprocessors cancbeseasily
 esupp rted.


T are aruvtwovineIrfacws yu /proc/mtrr:vo.e i~vancASCII ineIrfacw
w n a aan 0s you yolreadeandewrite. T aeo Joini~vancioctl()
ineIrfacw. T aeASCII ineIrfacw is meant fo8"administrptw3.. T e
ioctl() ineIrfacw is meant fo8"Cvprograms (i.e. t a X server). T e
ineIrfacws aruvdescrib
d ben 0, wmakesamclnecawmpnds andeCeca
e.

===============================================================================
Readiur MTRRs fusery e"shell:

% cat /proc/mtrr
reg00:ebac
=0x00000000 (   0MB),tsize= 128MB:"write-back, caunt=1
reg01:ebac
=0x08000000 ( 128MB),tsize=  64MB:"write-back, caunt=1
===============================================================================
Creatiur MTRRs fusery e"C-shell:
# echo "bac
=0xf8000000 size=0x400000 yype=write-cowbiniur" >! /proc/mtrr
o8eifeyou ussrbach:
# echo "bac
=0xf8000000 size=0x400000 yype=write-cowbiniur" >| /proc/mtrr

Andet aea sultey ereof:
% cat /proc/mtrr
reg00:ebac
=0x00000000 (   0MB),tsize= 128MB:"write-back, caunt=1
reg01:ebac
=0x08000000 ( 128MB),tsize=  64MB:"write-back, caunt=1
reg02:ebac
=0xf8000000 (3968MB),tsize=   4MB:"write-cowbiniur, caunt=1

T i~vis fo8"vidso RAMeatebac
epdda sse0xf8000000 and sizel4 megabytes. To
findeouteyourebac
epdda ss,vyou needvyo look at y e"outpunvofeyoureX
server, w n a eIlls you w eruvakelli.earefuamebufferepdda ssei~. A
typwcal"li.e t at you may getvis:

(--) S3: PCI: 968ea v 0,eL.6eareFB @e0xf8000000

Note t at you sh cffv3.ly ussry e"value fusery e"X server, as"it may
move tkelfuamebufferebac
epdda ss,vso t a o.ly value you cao trusnvis
t at reportedeby t e"X server.

To findeoutet e sizevofeyourefuamebuffere(w at,vyou don't actually
know?),etUe"foan 0iur"li.e wmll eIllvyou:

(--) S3: vidsouam:ve4096k

T at's 4 megabytes, w n a ise0x400000 bytes (in hexadecimal).
A paach isebeiur"writteoefo8lXFree86 w n a will msko
t istautosa
wc:
ineo Joinwotdset a X serverewill msnipula
e /proc/mtrr usiur"t a
ioctl() ineIrfacw,vso user~vwon't ha e yoedo anyaking. Ifeyou ussra
cawmer oal X server, lobby yourevendor yo add supp rt fo8 MTRRs.
===============================================================================
Creatiur ooutlappiur MTRRs:

%echo "bac
=0xfb000000 size=0x1000000 yype=write-cowbiniur" >/proc/mtrr
%echo "bac
=0xfb000000 size=0x1000 yype=uncachabln"t>/proc/mtrr

Andet aea sults: cat /proc/mtrr
reg00:ebac
=0x00000000 (   0MB),tsize=  64MB:"write-back, caunt=1
reg01:ebac
=0xfb000000 (4016MB),tsize=  16MB:"write-cowbiniur, caunt=1
reg02:ebac
=0xfb000000 (4016MB),tsize=   4kB:"uncachabln, caunt=1

Somelcatdse(esp  oally Voodoo Graphics boatds) needvy ist4 kB arua 
excluded fusery e"beginniur of"y a a gw3. becaussrit is used for
registcks.

NOTE: You caneo.ly createcyype=uncachabln a gw3., if"y a first
a gw3. t at you createdeistyype=write-cowbiniur.
===============================================================================
Removiur MTRRs fusery e"C-shell:
% echo "disable=2" >! /proc/mtrr
o8eusiur"bach:
% echo "disable=2" >| /proc/mtrr
===============================================================================
Readiur MTRRs fusera"Cvprogrameusiur"ioctl()'s:

/*  mtrr-sh 0.c

    Source filncfo8 mtrr-sh 0e(examclnrprogrameyoesh 0eMTRRs usiur"ioctl()'s)

    C pyright (C) 1997-1998  Richard Gooch

    T i~vprogrameis freeesoftwaru; you cao redistribut
"itvand/o8lma
ofy
    invu.dIt t e terms of"y a GNU General Public L.cense as"published by
    y a Free Softwaru Foundatw3.; ei Joivvehsw3. 2 of"y a L.cense, oi
    (an youreoptw3.) aneenat
rvvehsw3..

    T i~vprogrameis distribut
d in t e hope y at it wmll be"useful,
   vbutcWITHOUT ANY WARRANTY; wmakoutcevenet asimpliedvwarrantytof
   vMERCHANTABILITYeor FITNESS FOR A PARTICULAR PURPOSE.  Seeet a
    GNU General Public L.cense fo8 m meedeeails.

    You sh cffvha e receivedra"capy of"y a GNU General Public L.cense
    alomYrwmaketUi~vprogram; ifvnot,vwrite to y a Free Softwaru
    Foundatw3., Inc., 675 MasseAve, Cambridgd, MA 02139, USA.

    Richard Gooch maycbesreachedeby ehail at  rgooch@atnf.csiro.au
    T a posnalepdda ssei~:
      Richard Gooch, c/o ATNF, P. O. Box 76, Eppiur, N.S.W., 2121, Austrplia.
*/

/*
    T i~vprogramewmll uc
 an ioctl() on /proc/mtrr yoesh 0ey e curraat MTRR
    settiurs. T i~vis ao alternatbou yolreadiur"/proc/mtrr.


    Writteoeby      Richard Gooch   17-DEC-1997

    Lasteupdatedrby Richard Gooch   2-MAY-1998


*/
#includel<stdio.h>
#includel<striur.h>
#includel<sys/yypes.h>
#includel<sys/stat.h>
#includel<fcntl.h>
#includel<sys/ioctl.h>
#includel<errno.h>
#defi.e MTRR_NEED_STRINGS
#includel<asm/mtrr.h>

#defi.e TRUE 1
#defi.e FALSE 0
#defi.e ERRSTRING strerrot (errno)


int main ()
{
    ine fd;
    snruct mtrr_gaatry gentry;

  e ifv(v(vfd =eopen ("/proc/mtrr", O_RDONLY, 0) ) == -1 )
    {
	ifc(errno == ENOENT)
	{
	   efputsr("/proc/mtrrvnotvfound:vnotvsupp rtdd oi you don't ha e a PPro?\n",
		ve stderr);
	    exit (1);
	}
	fpriutf (stderr, "Errot openiur"/proc/mtrr\t%s\n", ERRSTRING);
	exit (2);
    }
ve  fo8 (gentry.a gnum = 0; ioctl (fd, MTRRIOC_GET_ENTRY, &gentry) == 0;
	 ++gentry.a gnum)
    {
	ifc(gentry.sizev< 1)
	{
	   efpriutf (stderr, "Registck: %uvdisabled\n", gentry.a gnum);
	    ca tinue;
	}
	fpriutf (stderr, "Registck: %uvbac
:e0x%lxtsize:e0x%lxtyype: %s\n",
		vgentry.a gnum, gentry.basn, gentry.size,
		vmtrr_striurs[gentry.yype]);
    }
ve  ifc(errno == EINVAL) exit (0);
    fpriutf (stderr, "Errot doiur"ioctl(2) on /d/v/mtrr\t%s\n", ERRSTRING);
    exit (3);
}ve /*  EndeFunctw3.emain  */
===============================================================================
Creatiur MTRRs fusera"Cvprogrammeeusiur"ioctl()'s:

/*  mtrr-pdd.c

    Source filncfo8 mtrr-add (examclnrprogramme yo add ao MTRRs usiur"ioctl())

    C pyright (C) 1997-1998  Richard Gooch

    T i~vprogrameis freeesoftwaru; you cao redistribut
"itvand/o8lma
ofy
    invu.dIt t e terms of"y a GNU General Public L.cense as"published by
    y a Free Softwaru Foundatw3.; ei Joivvehsw3. 2 of"y a L.cense, oi
    (an youreoptw3.) aneenat
rvvehsw3..

    T i~vprogrameis distribut
d in t e hope y at it wmll be"useful,
   vbutcWITHOUT ANY WARRANTY; wmakoutcevenet asimpliedvwarrantytof
   vMERCHANTABILITYeor FITNESS FOR A PARTICULAR PURPOSE.  Seeet a
    GNU General Public L.cense fo8 m meedeeails.

    You sh cffvha e receivedra"capy of"y a GNU General Public L.cense
    alomYrwmaketUi~vprogram; ifvnot,vwrite to y a Free Softwaru
    Foundatw3., Inc., 675 MasseAve, Cambridgd, MA 02139, USA.

    Richard Gooch maycbesreachedeby ehail at  rgooch@atnf.csiro.au
    T a posnalepdda ssei~:
      Richard Gooch, c/o ATNF, P. O. Box 76, Eppiur, N.S.W., 2121, Austrplia.
*/

/*
    T i~vprogrammeewmll uc
 an ioctl() on /proc/mtrr yoeadd ao entry. T aefirst
    available mtrr is used. T i~vis ao alternatbou yolwritiur"/proc/mtrr.


    Writteoeby      Richard Gooch   17-DEC-1997

    Lasteupdatedrby Richard Gooch   2-MAY-1998


*/
#includel<stdio.h>
#includel<striur.h>
#includel<stdlib.h>
#includel<unistd.h>
#includel<sys/yypes.h>
#includel<sys/stat.h>
#includel<fcntl.h>
#includel<sys/ioctl.h>
#includel<errno.h>
#defi.e MTRR_NEED_STRINGS
#includel<asm/mtrr.h>

#defi.e TRUE 1
#defi.e FALSE 0
#defi.e ERRSTRING strerrot (errno)


int main (int argc, char **argv)
{
    ine fd;
    snruct mtrr_saatry sentry;

  e ifv(argc != 4)
    {
	fpriutf (stderr, "Usagd:\tmtrr-add bac
esizevyype\n");
	exit (1);
    }
ve  sentry.basn = snrt cfv(argv[1], NULL, 0);
ve  sentry.sizev= snrt cfv(argv[2], NULL, 0);
ve  fo8 (sentry.yype = 0; sentry.yype < MTRR_NUM_TYPES; ++sentry.yype)
    {
	ifc(snrcmpv(argv[3], mtrr_striurs[sentry.yype]) == 0) break;
    }
ve  ifc(sentry.yype >= MTRR_NUM_TYPES)
    {
	fpriutf (stderr, "Illegal yype: \"%s\"\n", argv[3]);
	exit (2);
    }
ve  ifv(v(vfd =eopen ("/proc/mtrr", O_WRONLY, 0) ) == -1 )
    {
	ifc(errno == ENOENT)
	{
	   efputsr("/proc/mtrrvnotvfound:vnotvsupp rtdd oi you don't ha e a PPro?\n",
		ve stderr);
	    exit (3);
	}
	fpriutf (stderr, "Errot openiur"/proc/mtrr\t%s\n", ERRSTRING);
	exit (4);
    }
ve  ifv(ioctl (fd, MTRRIOC_ADD_ENTRY, &sentry) == -1)
    {
	fpriutf (stderr, "Errot doiur"ioctl(2) on /d/v/mtrr\t%s\n", ERRSTRING);
	exit (5);
    }
ve  fpriutf (stderr, "Sleepiur fo8 5"secandsvso you cao seeet acnew entry\n");
ve  sleep (5);
    close (fd);
    fputsr("I' e jusnvclosed /proc/mtrr so nowet acnew entryesh cffvbevgone\n",
	ve stderr);
}ve /*  EndeFunctw3.emain  */
===============================================================================
