mtrr.txt 12 KB

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