mtrr.c 8.1 KB

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