mtrr.c 2.2 KB

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