mtrr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * MTRR write-combining.
  3. *
  4. * The VESA LFB is mapped uncacheable (UC) by paging.c, so every dword the
  5. * 30 Hz 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. Returns
  101. * the next free kdbg info row. */
  102. static unsigned int dump_mtrrs(unsigned int vcnt)
  103. {
  104. char buf[56];
  105. char *p;
  106. unsigned int i, lo, hi, mlo, mhi;
  107. unsigned int row = 0;
  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. kdbg_info(row++, buf);
  116. serial_puts(SERIAL_COM1, buf);
  117. serial_puts(SERIAL_COM1, "\n");
  118. for (i = 0; i < vcnt && i < 8; i++) {
  119. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  120. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  121. p = buf;
  122. *p++ = 'M';
  123. *p++ = (char)('0' + i);
  124. *p++ = ' ';
  125. if (!(mlo & MTRR_VALID)) {
  126. p = app(p, "-");
  127. } else {
  128. p = app(p, "b=");
  129. p = hex8(p, lo & 0xFFFFF000u);
  130. p = app(p, " m=");
  131. p = hex8(p, mlo & 0xFFFFF000u);
  132. *p++ = ' ';
  133. p = app(p, type_name(lo));
  134. }
  135. *p = '\0';
  136. kdbg_info(row++, buf);
  137. serial_puts(SERIAL_COM1, buf);
  138. serial_puts(SERIAL_COM1, "\n");
  139. }
  140. return row;
  141. }
  142. void mtrr_set_wc(unsigned int base, unsigned int size)
  143. {
  144. unsigned int lo, hi, mlo;
  145. unsigned int def_type;
  146. unsigned int vcnt;
  147. unsigned int i, use = ~0u;
  148. unsigned int region_size, align, physmask;
  149. unsigned int row;
  150. char buf[56];
  151. char *p;
  152. if (!cpu_has_mtrr() || size == 0)
  153. return;
  154. rdmsr(MSR_MTRRCAP, &lo, &hi);
  155. vcnt = lo & 0xFF;
  156. if (vcnt > 16)
  157. vcnt = 16; /* sanity cap; CPUs ship 8..10 variable ranges */
  158. if (vcnt == 0)
  159. return;
  160. /* Dump the first 8 at most (kdbg has 10 info rows); the search
  161. * below still covers every variable range. */
  162. row = dump_mtrrs(vcnt > 8 ? 8 : vcnt);
  163. /* Round size up to the next power of two (min 4 KB). */
  164. region_size = 0x1000;
  165. while (region_size < size) {
  166. region_size <<= 1;
  167. if (region_size == 0)
  168. return; /* overflow (size > 2 GB) -- give up */
  169. }
  170. /* A variable-range MTRR must be aligned to its own size. The largest
  171. * region we may use is therefore the base address's own power-of-two
  172. * alignment. */
  173. align = base & (~base + 1u);
  174. if (align == 0 || align > 0x80000000u)
  175. align = 0x80000000u;
  176. if (region_size > align)
  177. region_size = align;
  178. if (region_size < 0x1000)
  179. return;
  180. base &= ~(region_size - 1);
  181. /* Only ever use a free variable entry. BIOS-populated entries may
  182. * keep chipset MMIO uncached and must not be disturbed. */
  183. for (i = 0; i < vcnt; i++) {
  184. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  185. if (!(mlo & MTRR_VALID)) {
  186. use = i;
  187. break;
  188. }
  189. }
  190. p = buf;
  191. if (use == ~0u) {
  192. p = app(p, "WC skipped: no free MTRR entry");
  193. *p = '\0';
  194. kdbg_info(row, buf);
  195. serial_puts(SERIAL_COM1, "mtrr: no free variable entry; skipping WC\n");
  196. return;
  197. }
  198. /* PhysMask bits [35:12] are compare bits: 1 = the address bit must
  199. * match PHYSBASE. For a 2^n-sized region, bits [n-1:12] are
  200. * don't-care (0) and every higher bit must match (1), i.e. the
  201. * complement of (size - 1). */
  202. physmask = (~(region_size - 1) & 0xFFFFF000u) | MTRR_VALID;
  203. /* Update the entry with the global enable temporarily cleared so an
  204. * in-progress region never exposes a bogus memory type. */
  205. rdmsr(MSR_MTRR_DEF_TYPE, &def_type, &hi);
  206. wrmsr(MSR_MTRR_DEF_TYPE, def_type & ~MTRR_VALID, 0);
  207. wrmsr(MSR_MTRR_PHYSBASE0 + 2 * use, base | MTRR_TYPE_WC, 0);
  208. wrmsr(MSR_MTRR_PHYSMASK0 + 2 * use, physmask, mtrr_hi_mask());
  209. wrmsr(MSR_MTRR_DEF_TYPE, def_type, 0);
  210. p = app(p, "WC=e");
  211. *p++ = (char)('0' + (use > 9 ? 9 : use));
  212. p = app(p, " b=");
  213. p = hex8(p, base);
  214. p = app(p, " s=");
  215. p = hex8(p, region_size);
  216. *p = '\0';
  217. kdbg_info(row, buf);
  218. serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
  219. serial_puthex(SERIAL_COM1, base);
  220. serial_puts(SERIAL_COM1, " +");
  221. serial_puthex(SERIAL_COM1, region_size);
  222. serial_puts(SERIAL_COM1, "]\n");
  223. }