mtrr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. static int cpu_is_amd(void)
  53. {
  54. unsigned int eax = 0, ebx, ecx, edx;
  55. __asm__ volatile ("cpuid"
  56. : "+a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx));
  57. /* "AuthenticAMD" */
  58. return ebx == 0x68747541u && edx == 0x69746E65u &&
  59. ecx == 0x444D4163u;
  60. }
  61. /* Mask bits for physical address bits 32..35+. Those are reserved (writing
  62. * 1 to an unimplemented bit faults) so ask CPUID for the real width. All
  63. * ones for the implemented high bits means "must match PHYSBASE", which is
  64. * zero, so addresses >= 4 GB never match the WC entry. */
  65. static unsigned int mtrr_hi_mask(void)
  66. {
  67. unsigned int eax, ebx, ecx, edx;
  68. unsigned int bits, n;
  69. __asm__ volatile ("cpuid"
  70. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  71. : "a"(0x80000000u), "c"(0));
  72. if (eax < 0x80000008u)
  73. return 0;
  74. __asm__ volatile ("cpuid"
  75. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  76. : "a"(0x80000008u), "c"(0));
  77. bits = eax & 0xFF;
  78. if (bits <= 32)
  79. return 0;
  80. n = bits - 32;
  81. if (n >= 32)
  82. return 0xFFFFFFFFu;
  83. return (1u << n) - 1;
  84. }
  85. static char *app(char *p, const char *s)
  86. {
  87. while (*s)
  88. *p++ = *s++;
  89. return p;
  90. }
  91. static char *hex8(char *p, unsigned int v)
  92. {
  93. static const char h[] = "0123456789ABCDEF";
  94. int i;
  95. for (i = 7; i >= 0; i--)
  96. *p++ = h[(v >> (i * 4)) & 0xF];
  97. return p;
  98. }
  99. static const char *type_name(unsigned int t)
  100. {
  101. switch (t & 7) {
  102. case 0: return "UC";
  103. case 1: return "WC";
  104. case 4: return "WT";
  105. case 5: return "WP";
  106. case 6: return "WB";
  107. default: return "??";
  108. }
  109. }
  110. /* Effective variable-range MTRR memory type of one physical address below
  111. * 4 GB (above the fixed ranges): UC wins over everything, WT over WB, and
  112. * MTRRdefType applies where no entry matches. -1 when the CPU has no MTRRs
  113. * or they are disabled (then nothing can be learned from them). */
  114. int mtrr_mem_type(unsigned int phys)
  115. {
  116. unsigned int lo, hi, mlo, mhi, def, vcnt, i;
  117. int type = -1;
  118. if (!cpu_has_mtrr())
  119. return -1;
  120. rdmsr(MSR_MTRR_DEF_TYPE, &def, &hi);
  121. if (!(def & MTRR_VALID))
  122. return -1;
  123. rdmsr(MSR_MTRRCAP, &vcnt, &hi);
  124. vcnt &= 0xFF;
  125. if (vcnt > 16)
  126. vcnt = 16;
  127. for (i = 0; i < vcnt; i++) {
  128. int t;
  129. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  130. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  131. if (!(mlo & MTRR_VALID))
  132. continue;
  133. /* The address's bits 32+ are zero: a base with high bits
  134. * set under the mask never matches. */
  135. if ((hi & mhi) != 0 ||
  136. ((phys ^ lo) & mlo & 0xFFFFF000u) != 0)
  137. continue;
  138. t = (int)(lo & 0xFF);
  139. if (t == 0)
  140. return 0;
  141. if (type < 0 || (type == 6 && t == 4))
  142. type = t;
  143. }
  144. return type < 0 ? (int)(def & 0xFF) : type;
  145. }
  146. /* Dump MTRRdefType and the variable entries to serial. */
  147. static void dump_mtrrs(unsigned int vcnt)
  148. {
  149. char buf[56];
  150. char *p;
  151. unsigned int i, lo, hi, mlo, mhi;
  152. rdmsr(MSR_MTRR_DEF_TYPE, &lo, &hi);
  153. p = buf;
  154. p = app(p, "MTRRdef ");
  155. p = hex8(p, lo);
  156. p = app(p, " vcnt ");
  157. *p++ = (char)('0' + (vcnt > 9 ? 9 : vcnt));
  158. *p = '\0';
  159. serial_puts(SERIAL_COM1, buf);
  160. serial_puts(SERIAL_COM1, "\n");
  161. for (i = 0; i < vcnt && i < 8; i++) {
  162. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  163. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  164. p = buf;
  165. *p++ = 'M';
  166. *p++ = (char)('0' + i);
  167. *p++ = ' ';
  168. if (!(mlo & MTRR_VALID)) {
  169. p = app(p, "-");
  170. } else {
  171. p = app(p, "b=");
  172. p = hex8(p, lo & 0xFFFFF000u);
  173. p = app(p, " m=");
  174. p = hex8(p, mlo & 0xFFFFF000u);
  175. *p++ = ' ';
  176. p = app(p, type_name(lo));
  177. }
  178. *p = '\0';
  179. serial_puts(SERIAL_COM1, buf);
  180. serial_puts(SERIAL_COM1, "\n");
  181. }
  182. }
  183. /* AMD family 15h WC path. The Intel path's read-modify-write of
  184. * MSR_MTRRdefType is what reset the 970A-DS3P during the first paging init.
  185. * AMD only has eight variable MTRRs, and the LFB is below 4 GB, so this path
  186. * never touches MTRRdefType and writes a zero high mask (all 32..63 address
  187. * bits are don't-care); that avoids every reserved-bit write while still
  188. * covering the framebuffer range. Only a free entry is used. */
  189. static int mtrr_set_wc_amd(unsigned int base, unsigned int size,
  190. unsigned int *wc_base, unsigned int *wc_size)
  191. {
  192. unsigned int lo, hi, mlo, mhi;
  193. unsigned int vcnt, i, use = ~0u;
  194. unsigned int region_size, align, physmask;
  195. rdmsr(MSR_MTRRCAP, &lo, &hi);
  196. vcnt = lo & 0xFF;
  197. if (vcnt > 8)
  198. vcnt = 8;
  199. if (vcnt == 0)
  200. return 0;
  201. rdmsr(MSR_MTRR_DEF_TYPE, &lo, &hi);
  202. if (!(lo & MTRR_VALID)) {
  203. serial_puts(SERIAL_COM1, "mtrr: AMD MTRR disabled by BIOS\n");
  204. return 0;
  205. }
  206. region_size = 0x1000;
  207. while (region_size < size) {
  208. region_size <<= 1;
  209. if (region_size == 0)
  210. return 0;
  211. }
  212. align = base & (~base + 1u);
  213. if (align == 0 || align > 0x80000000u)
  214. align = 0x80000000u;
  215. if (region_size > align)
  216. region_size = align;
  217. if (region_size < 0x1000)
  218. return 0;
  219. base &= ~(region_size - 1);
  220. for (i = 0; i < vcnt; i++) {
  221. unsigned int esize;
  222. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  223. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  224. if (!(mlo & MTRR_VALID) || (lo & 0xFF) != MTRR_TYPE_WC ||
  225. (lo & 0xFFFFF000u) != base)
  226. continue;
  227. esize = ((~mlo) & 0xFFFFF000u) + 0x1000;
  228. if (esize < region_size)
  229. continue;
  230. serial_puts(SERIAL_COM1,
  231. "mtrr: AMD LFB already write-combining\n");
  232. if (wc_base)
  233. *wc_base = base;
  234. if (wc_size)
  235. *wc_size = esize;
  236. return 1;
  237. }
  238. for (i = 0; i < vcnt; i++) {
  239. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &mhi);
  240. if (!(mlo & MTRR_VALID)) {
  241. use = i;
  242. break;
  243. }
  244. }
  245. if (use == ~0u) {
  246. serial_puts(SERIAL_COM1,
  247. "mtrr: AMD no free variable entry; skipping WC\n");
  248. return 0;
  249. }
  250. physmask = (~(region_size - 1) & 0xFFFFF000u) | MTRR_VALID;
  251. wrmsr(MSR_MTRR_PHYSBASE0 + 2 * use, base | MTRR_TYPE_WC, 0);
  252. wrmsr(MSR_MTRR_PHYSMASK0 + 2 * use, physmask, 0);
  253. serial_puts(SERIAL_COM1, "mtrr: AMD LFB WC [");
  254. serial_puthex(SERIAL_COM1, base);
  255. serial_puts(SERIAL_COM1, " +");
  256. serial_puthex(SERIAL_COM1, region_size);
  257. serial_puts(SERIAL_COM1, "]\n");
  258. if (wc_base)
  259. *wc_base = base;
  260. if (wc_size)
  261. *wc_size = region_size;
  262. return 1;
  263. }
  264. int mtrr_set_wc(unsigned int base, unsigned int size,
  265. unsigned int *wc_base, unsigned int *wc_size)
  266. {
  267. unsigned int lo, hi, mlo;
  268. unsigned int def_type;
  269. unsigned int vcnt;
  270. unsigned int i, use = ~0u;
  271. unsigned int region_size, align, physmask;
  272. if (!cpu_has_mtrr() || size == 0)
  273. return 0;
  274. /* AMD's MTRR register layout and BIOS setup differ enough from the
  275. * Intel path that the latter reset the 970A-DS3P. Use the separate
  276. * free-entry/zero-high-mask path. */
  277. if (cpu_is_amd())
  278. return mtrr_set_wc_amd(base, size, wc_base, wc_size);
  279. rdmsr(MSR_MTRRCAP, &lo, &hi);
  280. vcnt = lo & 0xFF;
  281. if (vcnt > 16)
  282. vcnt = 16; /* sanity cap; CPUs ship 8..10 variable ranges */
  283. if (vcnt == 0)
  284. return 0;
  285. /* Dump the first 8 at most; the search below still covers every
  286. * variable range. */
  287. dump_mtrrs(vcnt > 8 ? 8 : vcnt);
  288. /* Round size up to the next power of two (min 4 KB). */
  289. region_size = 0x1000;
  290. while (region_size < size) {
  291. region_size <<= 1;
  292. if (region_size == 0)
  293. return 0; /* overflow (size > 2 GB) -- give up */
  294. }
  295. /* A variable-range MTRR must be aligned to its own size. The largest
  296. * region we may use is therefore the base address's own power-of-two
  297. * alignment. */
  298. align = base & (~base + 1u);
  299. if (align == 0 || align > 0x80000000u)
  300. align = 0x80000000u;
  301. if (region_size > align)
  302. region_size = align;
  303. if (region_size < 0x1000)
  304. return 0;
  305. base &= ~(region_size - 1);
  306. /* A kernel reload does not reset the MTRRs: reuse the WC entry an
  307. * earlier boot left for this base instead of burning another variable
  308. * slot (the kexec dev loop would otherwise exhaust the entries). */
  309. for (i = 0; i < vcnt; i++) {
  310. unsigned int esize;
  311. rdmsr(MSR_MTRR_PHYSBASE0 + 2 * i, &lo, &hi);
  312. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  313. if (!(mlo & MTRR_VALID) || (lo & 0xFF) != MTRR_TYPE_WC ||
  314. (lo & 0xFFFFF000u) != base)
  315. continue;
  316. esize = ((~mlo) & 0xFFFFF000u) + 0x1000;
  317. if (esize < region_size)
  318. continue;
  319. serial_puts(SERIAL_COM1, "mtrr: LFB already write-combining\n");
  320. if (wc_base)
  321. *wc_base = base;
  322. if (wc_size)
  323. *wc_size = esize;
  324. return 1;
  325. }
  326. /* Only ever use a free variable entry. BIOS-populated entries may
  327. * keep chipset MMIO uncached and must not be disturbed. */
  328. for (i = 0; i < vcnt; i++) {
  329. rdmsr(MSR_MTRR_PHYSMASK0 + 2 * i, &mlo, &hi);
  330. if (!(mlo & MTRR_VALID)) {
  331. use = i;
  332. break;
  333. }
  334. }
  335. if (use == ~0u) {
  336. serial_puts(SERIAL_COM1, "mtrr: no free variable entry; skipping WC\n");
  337. return 0;
  338. }
  339. /* PhysMask bits [35:12] are compare bits: 1 = the address bit must
  340. * match PHYSBASE. For a 2^n-sized region, bits [n-1:12] are
  341. * don't-care (0) and every higher bit must match (1), i.e. the
  342. * complement of (size - 1). */
  343. physmask = (~(region_size - 1) & 0xFFFFF000u) | MTRR_VALID;
  344. /* Update the entry with the global enable temporarily cleared so an
  345. * in-progress region never exposes a bogus memory type. */
  346. rdmsr(MSR_MTRR_DEF_TYPE, &def_type, &hi);
  347. wrmsr(MSR_MTRR_DEF_TYPE, def_type & ~MTRR_VALID, 0);
  348. wrmsr(MSR_MTRR_PHYSBASE0 + 2 * use, base | MTRR_TYPE_WC, 0);
  349. wrmsr(MSR_MTRR_PHYSMASK0 + 2 * use, physmask, mtrr_hi_mask());
  350. wrmsr(MSR_MTRR_DEF_TYPE, def_type, 0);
  351. serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
  352. serial_puthex(SERIAL_COM1, base);
  353. serial_puts(SERIAL_COM1, " +");
  354. serial_puthex(SERIAL_COM1, region_size);
  355. serial_puts(SERIAL_COM1, "]\n");
  356. if (wc_base)
  357. *wc_base = base;
  358. if (wc_size)
  359. *wc_size = region_size;
  360. return 1;
  361. }