mtrr.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * (C) Copyright 2014 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Memory Type Range Regsters - these are used to tell the CPU whether
  7. * memory is cacheable and if so the cache write mode to use.
  8. *
  9. * These can speed up booting. See the mtrr command.
  10. *
  11. * Reference: Intel Architecture Software Developer's Manual, Volume 3:
  12. * System Programming
  13. */
  14. #include <common.h>
  15. #include <asm/io.h>
  16. #include <asm/msr.h>
  17. #include <asm/mtrr.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /* Prepare to adjust MTRRs */
  20. void mtrr_open(struct mtrr_state *state)
  21. {
  22. if (!gd->arch.has_mtrr)
  23. return;
  24. state->enable_cache = dcache_status();
  25. if (state->enable_cache)
  26. disable_caches();
  27. state->deftype = native_read_msr(MTRR_DEF_TYPE_MSR);
  28. wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype & ~MTRR_DEF_TYPE_EN);
  29. }
  30. /* Clean up after adjusting MTRRs, and enable them */
  31. void mtrr_close(struct mtrr_state *state)
  32. {
  33. if (!gd->arch.has_mtrr)
  34. return;
  35. wrmsrl(MTRR_DEF_TYPE_MSR, state->deftype | MTRR_DEF_TYPE_EN);
  36. if (state->enable_cache)
  37. enable_caches();
  38. }
  39. int mtrr_commit(bool do_caches)
  40. {
  41. struct mtrr_request *req = gd->arch.mtrr_req;
  42. struct mtrr_state state;
  43. uint64_t mask;
  44. int i;
  45. if (!gd->arch.has_mtrr)
  46. return -ENOSYS;
  47. mtrr_open(&state);
  48. for (i = 0; i < gd->arch.mtrr_req_count; i++, req++) {
  49. mask = ~(req->size - 1);
  50. mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
  51. wrmsrl(MTRR_PHYS_BASE_MSR(i), req->start | req->type);
  52. wrmsrl(MTRR_PHYS_MASK_MSR(i), mask | MTRR_PHYS_MASK_VALID);
  53. }
  54. /* Clear the ones that are unused */
  55. for (; i < MTRR_COUNT; i++)
  56. wrmsrl(MTRR_PHYS_MASK_MSR(i), 0);
  57. mtrr_close(&state);
  58. return 0;
  59. }
  60. int mtrr_add_request(int type, uint64_t start, uint64_t size)
  61. {
  62. struct mtrr_request *req;
  63. uint64_t mask;
  64. if (!gd->arch.has_mtrr)
  65. return -ENOSYS;
  66. if (gd->arch.mtrr_req_count == MAX_MTRR_REQUESTS)
  67. return -ENOSPC;
  68. req = &gd->arch.mtrr_req[gd->arch.mtrr_req_count++];
  69. req->type = type;
  70. req->start = start;
  71. req->size = size;
  72. debug("%d: type=%d, %08llx %08llx\n", gd->arch.mtrr_req_count - 1,
  73. req->type, req->start, req->size);
  74. mask = ~(req->size - 1);
  75. mask &= (1ULL << CONFIG_CPU_ADDR_BITS) - 1;
  76. mask |= MTRR_PHYS_MASK_VALID;
  77. debug(" %016llx %016llx\n", req->start | req->type, mask);
  78. return 0;
  79. }