mtrr.rst 12 KB

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