mtrr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * MTRR write-combining.
  3. *
  4. * The VESA LFB is mapped uncacheable (UC) by paging.c, so every dword the
  5. * shadow flush writes to VRAM is a separate uncached PCI transaction.
  6. * Marking the LFB range write-combining (WC) via an MTRR lets the CPU batch
  7. * those writes into burst transfers, which is the single biggest rendering
  8. * win on real hardware.
  9. *
  10. * Two hard rules (the first key press froze an ASUS AT3IONT-I; these were
  11. * the only places where the kernel touched cacheability behind the BIOS's
  12. * back, and overwriting the BIOS's MMIO-cacheability entries is the prime
  13. * suspect, so both are fixed to be safe regardless):
  14. *
  15. * 1. NEVER overwrite an enabled BIOS MTRR entry. Only ever add the WC
  16. * range to a free variable entry; if none is free, skip WC entirely.
  17. * 2. Get the physmask direction right: mask bits are "compare" bits and
  18. * are therefore the bitwise COMPLEMENT of (size - 1). (The previous
  19. * version had them inverted, so e.g. a 16 MB request marked the first
  20. * 4 KB of every 16 MB block as WC instead of the framebuffer range.)
  21. *
  22. * The MTRR layout found at boot and the result are dumped to the on-screen
  23. * debug lines (kdbg) and to serial, so this can be inspected on machines
  24. * without a serial console.
  25. */
  26. #include "mtrr.h"
  27. #include "kdbg.h"
  28. #include "serial.h"
  29. #define MSR_MTRRCAP 0x0FE
  30. #define MSR_MTRR_DEF_TYPE 0x2FF
  31. #define MSR_MTRR_PHYSBASE0 0x200
  32. #define MSR_MTRR_PHYSMASK0 0x201
  33. #define MTRR_TYPE_WC 0x01
  34. #define MTRR_VALID 0x800
  35. static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
  36. {
  37. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  38. }
  39. static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
  40. {
  41. __asm__ volatile ("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
  42. }
  43. static int cpu_has_mtrr(void)
  44. {
  45. unsigned int eax, ebx, ecx, edx;
  46. __asm__ volatile ("cpuid"
  47. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  48. : "a"(1), "c"(0));
  49. return (edx & (1u << 12)) != 0;
  50. }
  51. /* Mask bits for physical address bits 32..35+. Those are reserved (writing
  52. * 1 to an unimplemented bit faults) so ask CPUID for the real width. All
  53. * ones for the implemented high bits means "must match PHYSBASE", which is
  54. * zero, so addresses >= 4 GB never match the WC entry. */
  55. static unsigned int mtrr_hi_mask(void)
  56. {
  57. unsigned int eax, ebx, ecx, edx;
  58. unsigned int bits, n;
  59. __asm__ volatile ("cpuid"
  60. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  61. : "a"(0x80000000u), "c"(0));
  62. if (eax < 0x80000008u)
  63. return 0;
  64. __asm__ volatile ("cpuid"
  65. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  66. : "a"(0x80000008u), "c"(0));
  67. bits = eax & 0xFF;
  68. if (bits <= 32)
  69. return 0;
  70. n = bits - 32;
  71. if (n >= 32)
  72. return 0xFFFFFFFFu;
  73. return (1u << n) - 1;
  74. }
  75. static char *app(char *p, const char *s)
  76. {
  77. while (*s)
  78. *p++ = *s++;
  79. return p;
  80. }
  81. static char *hex8(char *p, unsigned int v)
  82. {
  83. static const char h[] = "0123456789ABCDEF";
  84. int i;
  85. for (i = 7; i >= 0; i--)
  86. *p++ = h[(v >> (i * 4)) & 0xF];
  87. return p;
  88. }
  89. static const char *type_name(unsigned int t)
  90. {
  91. switch (t & 7) {
  92. case 0: return "UC";
  93. case 1: return "WC";
  94. case 4: return "WT";
  95. case 5: return "WP";
  96. case 6: return "WB";
  97. default: return "??";
  98. }
  99. }
  100. /* Dump MTRRdefType and the variable entries to kdbg and serial. Rows come
  101. * from the kdbg info pool, so other subsystems keep their own. */
  102. static void dump_mtrrs(unsigned int vcnt)
  103. {
  104. char buf[56];
  105. char *p;
  106. unsigned int i, lo, hi, mlo, mhi;
  107. int row;
  108. rdmsr(MSR_MTRR_DEF_TYPE, &lo, &hi);
  109. p = buf;
  110. p = app(p, "MTRRdef ");
  111. p = hex8(p, lo);
  112. p = app(p, " vcnt ");
  113. *p++ = (char)('0' + (vcnt > 9 ? 9 : vcnt));
  114. *p = '\0';
  115. row = kdbg_info_alloc();
  116. if (row >= 0)
  117. kdbg_info((unsigned int)row, buf);
  118. serial_puts(SERIAL_COM1, buf);
  119. serial_puts(SERIAL_COM1, "\n");
  120. for (i = 0; i < vcnt && i < 8; i++) {
  121. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  122. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  123. p = buf;
  124. *p++ = 'M';
  125. *p++ = (char)('0' + i);
  126. *p++ = ' ';
  127. if (!(mlo & MTRR_VALID)) {
  128. p = app(p, "-");
  129. } else {
  130. p = app(p, "b=");
  131. p = hex8(p, lo & 0xFFFFF000u);
  132. p = app(p, " m=");
  133. p = hex8(p, mlo & 0xFFFFF000u);
  134. *p++ = ' ';
  135. p = app(p, type_name(lo));
  136. }
  137. *p = '\0';
  138. row = kdbg_info_alloc();
  139. if (row >= 0)
  140. kdbg_info((unsigned int)row, buf);
  141. serial_puts(SERIAL_COM1, buf);
  142. serial_puts(SERIAL_COM1, "\n");
  143. }
  144. }
  145. void mtrr_set_wc(unsigned int base, unsigned int size)
  146. {
  147. unsigned int lo, hi, mlo;
  148. unsigned int def_type;
  149. unsigned int vcnt;
  150. unsigned int i, use = ~0u;
  151. unsigned int region_size, align, physmask;
  152. int row;
  153. char buf[56];
  154. char *p;
  155. if (!cpu_has_mtrr() || size == 0)
  156. return;
  157. rdmsr(MSR_MTRRCAP, &lo, &hi);
  158. vcnt = lo & 0xFF;
  159. if (vcnt > 16)
  160. vcnt = 16; /* sanity cap; CPUs ship 8..10 variable ranges */
  161. if (vcnt == 0)
  162. return;
  163. /* Dump the first 8 at most (kdbg info rows are a shared pool); the
  164. * search below still covers every variable range. */
  165. dump_mtrrs(vcnt > 8 ? 8 : vcnt);
  166. /* Round size up to the next power of two (min 4 KB). */
  167. region_size = 0x1000;
  168. while (region_size < size) {
  169. region_size <<= 1;
  170. if (region_size == 0)
  171. return; /* overflow (size > 2 GB) -- give up */
  172. }
  173. /* A variable-range MTRR must be aligned to its own size. The largest
  174. * region we may use is therefore the base address's own power-of-two
  175. * alignment. */
  176. align = base & (~base + 1u);
  177. if (align == 0 || align > 0x80000000u)
  178. align = 0x80000000u;
  179. if (region_size > align)
  180. region_size = align;
  181. if (region_size < 0x1000)
  182. return;
  183. base &= ~(region_size - 1);
  184. /* A kernel reload does not reset the MTRRs: reuse the WC entry an
  185. * earlier boot left for this base instead of burning another variable
  186. * slot (the kexec dev loop would otherwise exhaust the entries). */
  187. for (i = 0; i < vcnt; i++) {
  188. unsigned int esize;
  189. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  190. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  191. if (!(mlo & MTRR_VALID) || (lo & 0xFF) != MTRR_TYPE_WC ||
  192. (lo & 0xFFFFF000u) != base)
  193. continue;
  194. esize = ((~mlo) & 0xFFFFF000u) + 0x1000;
  195. if (esize < region_size)
  196. continue;
  197. serial_puts(SERIAL_COM1, "mtrr: LFB already write-combining\n");
  198. return;
  199. }
  200. /* Only ever use a free variable entry. BIOS-populated entries may
  201. * keep chipset MMIO uncached and must not be disturbed. */
  202. for (i = 0; i < vcnt; i++) {
  203. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  204. if (!(mlo & MTRR_VALID)) {
  205. use = i;
  206. break;
  207. }
  208. }
  209. p = buf;
  210. if (use == ~0u) {
  211. p = app(p, "WC skipped: no free MTRR entry");
  212. *p = '\0';
  213. row = kdbg_info_alloc();
  214. if (row >= 0)
  215. kdbg_info((unsigned int)row, buf);
  216. serial_puts(SERIAL_COM1, "mtrr: no free variable entry; skipping WC\n");
  217. return;
  218. }
  219. /* PhysMask bits [35:12] are compare bits: 1 = the address bit must
  220. * match PHYSBASE. For a 2^n-sized region, bits [n-1:12] are
  221. * don't-care (0) and every higher bit must match (1), i.e. the
  222. * complement of (size - 1). */
  223. physmask = (~(region_size - 1) & 0xFFFFF000u) | MTRR_VALID;
  224. /* Update the entry with the global enable temporarily cleared so an
  225. * in-progress region never exposes a bogus memory type. */
  226. rdmsr(MSR_MTRR_DEF_TYPE, &def_type, &hi);
  227. wrmsr(MSR_MTRR_DEF_TYPE, def_type & ~MTRR_VALID, 0);
  228. wrmsr(MSR_MTRR_PHYSBASE0 + 2 * use, base | MTRR_TYPE_WC, 0);
  229. wrmsr(MSR_MTRR_PHYSMASK0 + 2 * use, physmask, mtrr_hi_mask());
  230. wrmsr(MSR_MTRR_DEF_TYPE, def_type, 0);
  231. p = app(p, "WC=e");
  232. *p++ = (char)('0' + (use > 9 ? 9 : use));
  233. p = app(p, " b=");
  234. p = hex8(p, base);
  235. p = app(p, " s=");
  236. p = hex8(p, region_size);
  237. *p = '\0';
  238. row = kdbg_info_alloc();
  239. if (row >= 0)
  240. kdbg_info((unsigned int)row, buf);
  241. serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
  242. serial_puthex(SERIAL_COM1, base);
  243. serial_puts(SERIAL_COM1, " +");
  244. serial_puthex(SERIAL_COM1, region_size);
  245. serial_puts(SERIAL_COM1, "]\n");
  246. }