vmid.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2019 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "mod_vmid.h"
  26. struct core_vmid {
  27. struct mod_vmid public;
  28. struct dc *dc;
  29. unsigned int num_vmid;
  30. unsigned int num_vmids_available;
  31. uint64_t ptb_assigned_to_vmid[MAX_VMID];
  32. struct dc_virtual_addr_space_config base_config;
  33. };
  34. #define MOD_VMID_TO_CORE(mod_vmid)\
  35. container_of(mod_vmid, struct core_vmid, public)
  36. static void add_ptb_to_table(struct core_vmid *core_vmid, unsigned int vmid, uint64_t ptb)
  37. {
  38. if (vmid < MAX_VMID) {
  39. core_vmid->ptb_assigned_to_vmid[vmid] = ptb;
  40. core_vmid->num_vmids_available--;
  41. }
  42. }
  43. static void clear_entry_from_vmid_table(struct core_vmid *core_vmid, unsigned int vmid)
  44. {
  45. if (vmid < MAX_VMID) {
  46. core_vmid->ptb_assigned_to_vmid[vmid] = 0;
  47. core_vmid->num_vmids_available++;
  48. }
  49. }
  50. static void evict_vmids(struct core_vmid *core_vmid)
  51. {
  52. int i;
  53. uint16_t ord = dc_get_vmid_use_vector(core_vmid->dc);
  54. // At this point any positions with value 0 are unused vmids, evict them
  55. for (i = 1; i < core_vmid->num_vmid; i++) {
  56. if (!(ord & (1u << i)))
  57. clear_entry_from_vmid_table(core_vmid, i);
  58. }
  59. }
  60. // Return value of -1 indicates vmid table uninitialized or ptb dne in the table
  61. static int get_existing_vmid_for_ptb(struct core_vmid *core_vmid, uint64_t ptb)
  62. {
  63. int i;
  64. for (i = 0; i < core_vmid->num_vmid; i++) {
  65. if (core_vmid->ptb_assigned_to_vmid[i] == ptb)
  66. return i;
  67. }
  68. return -1;
  69. }
  70. // Expected to be called only when there's an available vmid
  71. static int get_next_available_vmid(struct core_vmid *core_vmid)
  72. {
  73. int i;
  74. for (i = 1; i < core_vmid->num_vmid; i++) {
  75. if (core_vmid->ptb_assigned_to_vmid[i] == 0)
  76. return i;
  77. }
  78. return -1;
  79. }
  80. uint8_t mod_vmid_get_for_ptb(struct mod_vmid *mod_vmid, uint64_t ptb)
  81. {
  82. struct core_vmid *core_vmid = MOD_VMID_TO_CORE(mod_vmid);
  83. int vmid = 0;
  84. // Physical address gets vmid 0
  85. if (ptb == 0)
  86. return 0;
  87. vmid = get_existing_vmid_for_ptb(core_vmid, ptb);
  88. if (vmid == -1) {
  89. struct dc_virtual_addr_space_config va_config = core_vmid->base_config;
  90. va_config.page_table_base_addr = ptb;
  91. if (core_vmid->num_vmids_available == 0)
  92. evict_vmids(core_vmid);
  93. vmid = get_next_available_vmid(core_vmid);
  94. if (vmid != -1) {
  95. add_ptb_to_table(core_vmid, vmid, ptb);
  96. dc_setup_vm_context(core_vmid->dc, &va_config, vmid);
  97. } else
  98. ASSERT(0);
  99. }
  100. return vmid;
  101. }
  102. void mod_vmid_reset(struct mod_vmid *mod_vmid)
  103. {
  104. struct core_vmid *core_vmid = MOD_VMID_TO_CORE(mod_vmid);
  105. core_vmid->num_vmids_available = core_vmid->num_vmid - 1;
  106. memset(core_vmid->ptb_assigned_to_vmid, 0, sizeof(core_vmid->ptb_assigned_to_vmid[0]) * MAX_VMID);
  107. }
  108. struct mod_vmid *mod_vmid_create(
  109. struct dc *dc,
  110. unsigned int num_vmid,
  111. struct dc_virtual_addr_space_config *va_config)
  112. {
  113. struct core_vmid *core_vmid;
  114. if (num_vmid <= 1)
  115. goto fail_no_vm_ctx;
  116. if (dc == NULL)
  117. goto fail_dc_null;
  118. core_vmid = kzalloc(sizeof(struct core_vmid), GFP_KERNEL);
  119. if (core_vmid == NULL)
  120. goto fail_alloc_context;
  121. core_vmid->dc = dc;
  122. core_vmid->num_vmid = num_vmid;
  123. core_vmid->num_vmids_available = num_vmid - 1;
  124. core_vmid->base_config = *va_config;
  125. memset(core_vmid->ptb_assigned_to_vmid, 0, sizeof(core_vmid->ptb_assigned_to_vmid[0]) * MAX_VMID);
  126. return &core_vmid->public;
  127. fail_no_vm_ctx:
  128. fail_alloc_context:
  129. fail_dc_null:
  130. return NULL;
  131. }
  132. void mod_vmid_destroy(struct mod_vmid *mod_vmid)
  133. {
  134. if (mod_vmid != NULL) {
  135. struct core_vmid *core_vmid = MOD_VMID_TO_CORE(mod_vmid);
  136. kfree(core_vmid);
  137. }
  138. }