mtrr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 serial, so this
  26. * can be inspected on machines without a VGA console.
  27. */
  28. #include "mtrr.h"
  29. #include "serial.h"
  30. #define MSR_MTRRCAP 0x0FE
  31. #define MSR_MTRR_DEF_TYPE 0x2FF
  32. #define MSR_MTRR_PHYSBASE0 0x200
  33. #define MSR_MTRR_PHYSMASK0 0x201
  34. #define MTRR_TYPE_WC 0x01
  35. #define MTRR_VALID 0x800
  36. static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
  37. {
  38. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  39. }
  40. static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
  41. {
  42. __asm__ volatile ("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
  43. }
  44. static int cpu_has_mtrr(void)
  45. {
  46. unsigned int eax, ebx, ecx, edx;
  47. __asm__ volatile ("cpuid"
  48. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  49. : "a"(1), "c"(0));
  50. return (edx & (1u << 12)) != 0;
  51. }
  52. /* Mask bits for physical address bits 32..35+. Those are reserved (writing
  53. * 1 to an unimplemented bit faults) so ask CPUID for the real width. All
  54. * ones for the implemented high bits means "must match PHYSBASE", which is
  55. * zero, so addresses >= 4 GB never match the WC entry. */
  56. static unsigned int mtrr_hi_mask(void)
  57. {
  58. unsigned int eax, ebx, ecx, edx;
  59. unsigned int bits, n;
  60. __asm__ volatile ("cpuid"
  61. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  62. : "a"(0x80000000u), "c"(0));
  63. if (eax < 0x80000008u)
  64. return 0;
  65. __asm__ volatile ("cpuid"
  66. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  67. : "a"(0x80000008u), "c"(0));
  68. bits = eax & 0xFF;
  69. if (bits <= 32)
  70. return 0;
  71. n = bits - 32;
  72. if (n >= 32)
  73. return 0xFFFFFFFFu;
  74. return (1u << n) - 1;
  75. }
  76. static char *app(char *p, const char *s)
  77. {
  78. while (*s)
  79. *p++ = *s++;
  80. return p;
  81. }
  82. static char *hex8(char *p, unsigned int v)
  83. {
  84. static const char h[] = "0123456789ABCDEF";
  85. int i;
  86. for (i = 7; i >= 0; i--)
  87. *p++ = h[(v >> (i * 4)) & 0xF];
  88. return p;
  89. }
  90. static const char *type_name(unsigned int t)
  91. {
  92. switch (t & 7) {
  93. case 0: return "UC";
  94. case 1: return "WC";
  95. case 4: return "WT";
  96. case 5: return "WP";
  97. case 6: return "WB";
  98. default: return "??";
  99. }
  100. }
  101. /* Dump MTRRdefType and the variable entries to serial. */
  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. rdmsr(MSR_MTRR_DEF_TYPE, &lo, &hi);
  108. p = buf;
  109. p = app(p, "MTRRdef ");
  110. p = hex8(p, lo);
  111. p = app(p, " vcnt ");
  112. *p++ = (char)('0' + (vcnt > 9 ? 9 : vcnt));
  113. *p = '\0';
  114. serial_puts(SERIAL_COM1, buf);
  115. serial_puts(SERIAL_COM1, "\n");
  116. for (i = 0; i < vcnt && i < 8; i++) {
  117. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  118. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  119. p = buf;
  120. *p++ = 'M';
  121. *p++ = (char)('0' + i);
  122. *p++ = ' ';
  123. if (!(mlo & MTRR_VALID)) {
  124. p = app(p, "-");
  125. } else {
  126. p = app(p, "b=");
  127. p = hex8(p, lo & 0xFFFFF000u);
  128. p = app(p, " m=");
  129. p = hex8(p, mlo & 0xFFFFF000u);
  130. *p++ = ' ';
  131. p = app(p, type_name(lo));
  132. }
  133. *p = '\0';
  134. serial_puts(SERIAL_COM1, buf);
  135. serial_puts(SERIAL_COM1, "\n");
  136. }
  137. }
  138. int mtrr_set_wc(unsigned int base, unsigned int size,
  139. unsigned int *wc_base, unsigned int *wc_size)
  140. {
  141. unsigned int lo, hi, mlo;
  142. unsigned int def_type;
  143. unsigned int vcnt;
  144. unsigned int i, use = ~0u;
  145. unsigned int region_size, align, physmask;
  146. if (!cpu_has_mtrr() || size == 0)
  147. return 0;
  148. rdmsr(MSR_MTRRCAP, &lo, &hi);
  149. vcnt = lo & 0xFF;
  150. if (vcnt > 16)
  151. vcnt = 16; /* sanity cap; CPUs ship 8..10 variable ranges */
  152. if (vcnt == 0)
  153. return 0;
  154. /* Dump the first 8 at most; the search below still covers every
  155. * variable range. */
  156. dump_mtrrs(vcnt > 8 ? 8 : vcnt);
  157. /* Round size up to the next power of two (min 4 KB). */
  158. region_size = 0x1000;
  159. while (region_size < size) {
  160. region_size <<= 1;
  161. if (region_size == 0)
  162. return 0; /* overflow (size > 2 GB) -- give up */
  163. }
  164. /* A variable-range MTRR must be aligned to its own size. The largest
  165. * region we may use is therefore the base address's own power-of-two
  166. * alignment. */
  167. align = base & (~base + 1u);
  168. if (align == 0 || align > 0x80000000u)
  169. align = 0x80000000u;
  170. if (region_size > align)
  171. region_size = align;
  172. if (region_size < 0x1000)
  173. return 0;
  174. base &= ~(region_size - 1);
  175. /* A kernel reload does not reset the MTRRs: reuse the WC entry an
  176. * earlier boot left for this base instead of burning another variable
  177. * slot (the kexec dev loop would otherwise exhaust the entries). */
  178. for (i = 0; i < vcnt; i++) {
  179. unsigned int esize;
  180. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  181. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  182. if (!(mlo & MTRR_VALID) || (lo & 0xFF) != MTRR_TYPE_WC ||
  183. (lo & 0xFFFFF000u) != base)
  184. continue;
  185. esize = ((~mlo) & 0xFFFFF000u) + 0x1000;
  186. if (esize < region_size)
  187. continue;
  188. serial_puts(SERIAL_COM1, "mtrr: LFB already write-combining\n");
  189. if (wc_base)
  190. *wc_base = base;
  191. if (wc_size)
  192. *wc_size = esize;
  193. return 1;
  194. }
  195. /* Only ever use a free variable entry. BIOS-populated entries may
  196. * keep chipset MMIO uncached and must not be disturbed. */
  197. for (i = 0; i < vcnt; i++) {
  198. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  199. if (!(mlo & MTRR_VALID)) {
  200. use = i;
  201. break;
  202. }
  203. }
  204. if (use == ~0u) {
  205. serial_puts(SERIAL_COM1, "mtrr: no free variable entry; skipping WC\n");
  206. return 0;
  207. }
  208. /* PhysMask bits [35:12] are compare bits: 1 = the address bit must
  209. * match PHYSBASE. For a 2^n-sized region, bits [n-1:12] are
  210. * don't-care (0) and every higher bit must match (1), i.e. the
  211. * complement of (size - 1). */
  212. physmask = (~(region_size - 1) & 0xFFFFF000u) | MTRR_VALID;
  213. /* Update the entry with the global enable temporarily cleared so an
  214. * in-progress region never exposes a bogus memory type. */
  215. rdmsr(MSR_MTRR_DEF_TYPE, &def_type, &hi);
  216. wrmsr(MSR_MTRR_DEF_TYPE, def_type & ~MTRR_VALID, 0);
  217. wrmsr(MSR_MTRR_PHYSBASE0 + 2 * use, base | MTRR_TYPE_WC, 0);
  218. wrmsr(MSR_MTRR_PHYSMASK0 + 2 * use, physmask, mtrr_hi_mask());
  219. wrmsr(MSR_MTRR_DEF_TYPE, def_type, 0);
  220. serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
  221. serial_puthex(SERIAL_COM1, base);
  222. serial_puts(SERIAL_COM1, " +");
  223. serial_puthex(SERIAL_COM1, region_size);
  224. serial_puts(SERIAL_COM1, "]\n");
  225. if (wc_base)
  226. *wc_base = base;
  227. if (wc_size)
  228. *wc_size = region_size;
  229. return 1;
  230. }