mtrr.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. (The "P5 USWC" write-combining comment in vga_copy.S
  9. * does not hold on the Pentium 4 / Athlon class machines that ship with
  10. * Radeon RV280 cards.)
  11. */
  12. #include "mtrr.h"
  13. #include "serial.h"
  14. #define MSR_MTRRCAP 0x0FE
  15. #define MSR_MTRR_DEF_TYPE 0x2FF
  16. #define MSR_MTRR_PHYSBASE0 0x200
  17. #define MSR_MTRR_PHYSMASK0 0x201
  18. #define MTRR_TYPE_WC 0x01
  19. static void rdmsr(unsigned int msr, unsigned int *lo, unsigned int *hi)
  20. {
  21. __asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
  22. }
  23. static void wrmsr(unsigned int msr, unsigned int lo, unsigned int hi)
  24. {
  25. __asm__ volatile ("wrmsr" : : "a"(lo), "d"(hi), "c"(msr));
  26. }
  27. static int cpu_has_mtrr(void)
  28. {
  29. unsigned int eax, ebx, ecx, edx;
  30. __asm__ volatile ("cpuid"
  31. : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
  32. : "a"(1), "c"(0));
  33. return (edx & (1u << 12)) != 0;
  34. }
  35. void mtrr_set_wc(unsigned int base, unsigned int size)
  36. {
  37. unsigned int lo, hi;
  38. unsigned int def_type;
  39. unsigned int vcnt;
  40. unsigned int region_size, align;
  41. unsigned int physmask;
  42. if (!cpu_has_mtrr())
  43. return;
  44. if (size == 0)
  45. return;
  46. rdmsr(MSR_MTRRCAP, &lo, &hi);
  47. vcnt = lo & 0xFF;
  48. if (vcnt == 0)
  49. return;
  50. /* Round size up to the next power of two (min 4 KB). */
  51. region_size = 0x1000;
  52. while (region_size < size) {
  53. region_size <<= 1;
  54. if (region_size == 0)
  55. return; /* overflow (size > 2 GB) -- give up */
  56. }
  57. /* A variable-range MTRR must be aligned to its own size. The largest
  58. * region we may use is therefore the base address's own power-of-two
  59. * alignment (base & -base). */
  60. align = base & (~base + 1u);
  61. if (align == 0 || align > 0x80000000u)
  62. align = 0x80000000u;
  63. if (region_size > align)
  64. region_size = align;
  65. if (region_size < 0x1000)
  66. return;
  67. base &= ~(region_size - 1);
  68. /* PhysMask: address bits [11:0] are implicitly don't-care; bits
  69. * [35:12] are the mask, with a 1 = don't-care. For a <4 GB region the
  70. * high 32-bit mask is 0 (bits 32..35 must match physbase == 0). */
  71. physmask = ((region_size - 1) & 0xFFFFF000u) | 0x800;
  72. /* Update MTRR with the global enable temporarily cleared so an
  73. * in-progress region never exposes a bogus memory type. */
  74. rdmsr(MSR_MTRR_DEF_TYPE, &def_type, &hi);
  75. wrmsr(MSR_MTRR_DEF_TYPE, def_type & ~0x800u, 0);
  76. wrmsr(MSR_MTRR_PHYSBASE0, base | MTRR_TYPE_WC, 0);
  77. wrmsr(MSR_MTRR_PHYSMASK0, physmask, 0);
  78. wrmsr(MSR_MTRR_DEF_TYPE, def_type, 0);
  79. serial_puts(SERIAL_COM1, "mtrr: LFB WC [");
  80. serial_puthex(SERIAL_COM1, base);
  81. serial_puts(SERIAL_COM1, " +");
  82. serial_puthex(SERIAL_COM1, region_size);
  83. serial_puts(SERIAL_COM1, "]\n");
  84. }