1 /* 2 * Copyright 2018 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 26 #include "vm_helper.h" 27 28 static void mark_vmid_used(struct vm_helper *vm_helper, unsigned int pos, uint8_t hubp_idx) 29 { 30 struct vmid_usage vmids = vm_helper->hubp_vmid_usage[hubp_idx]; 31 32 vmids.vmid_usage[0] = vmids.vmid_usage[1]; 33 vmids.vmid_usage[1] = 1 << pos; 34 } 35 36 static void add_ptb_to_table(struct vm_helper *vm_helper, unsigned int vmid, uint64_t ptb) 37 { 38 vm_helper->ptb_assigned_to_vmid[vmid] = ptb; 39 vm_helper->num_vmids_available--; 40 } 41 42 static void clear_entry_from_vmid_table(struct vm_helper *vm_helper, unsigned int vmid) 43 { 44 vm_helper->ptb_assigned_to_vmid[vmid] = 0; 45 vm_helper->num_vmids_available++; 46 } 47 48 static void evict_vmids(struct vm_helper *vm_helper) 49 { 50 int i; 51 uint16_t ord = 0; 52 53 for (i = 0; i < vm_helper->num_vmid; i++) 54 ord |= vm_helper->hubp_vmid_usage[i].vmid_usage[0] | vm_helper->hubp_vmid_usage[i].vmid_usage[1]; 55 56 // At this point any positions with value 0 are unused vmids, evict them 57 for (i = 1; i < vm_helper->num_vmid; i++) { 58 if (ord & (1u << i)) 59 clear_entry_from_vmid_table(vm_helper, i); 60 } 61 } 62 63 // Return value of -1 indicates vmid table unitialized or ptb dne in the table 64 static int get_existing_vmid_for_ptb(struct vm_helper *vm_helper, uint64_t ptb) 65 { 66 int i; 67 68 for (i = 0; i < vm_helper->num_vmid; i++) { 69 if (vm_helper->ptb_assigned_to_vmid[i] == ptb) 70 return i; 71 } 72 73 return -1; 74 } 75 76 // Expected to be called only when there's an available vmid 77 static int get_next_available_vmid(struct vm_helper *vm_helper) 78 { 79 int i; 80 81 for (i = 1; i < vm_helper->num_vmid; i++) { 82 if (vm_helper->ptb_assigned_to_vmid[i] == 0) 83 return i; 84 } 85 86 return -1; 87 } 88 89 uint8_t get_vmid_for_ptb(struct vm_helper *vm_helper, int64_t ptb, uint8_t hubp_idx) 90 { 91 unsigned int vmid = 0; 92 int vmid_exists = -1; 93 94 // Physical address gets vmid 0 95 if (ptb == 0) 96 return 0; 97 98 vmid_exists = get_existing_vmid_for_ptb(vm_helper, ptb); 99 100 if (vmid_exists != -1) { 101 mark_vmid_used(vm_helper, vmid_exists, hubp_idx); 102 vmid = vmid_exists; 103 } else { 104 if (vm_helper->num_vmids_available == 0) 105 evict_vmids(vm_helper); 106 107 vmid = get_next_available_vmid(vm_helper); 108 mark_vmid_used(vm_helper, vmid, hubp_idx); 109 add_ptb_to_table(vm_helper, vmid, ptb); 110 } 111 112 return vmid; 113 } 114 115 void init_vm_helper(struct vm_helper *vm_helper, unsigned int num_vmid, unsigned int num_hubp) 116 { 117 vm_helper->num_vmid = num_vmid; 118 vm_helper->num_hubp = num_hubp; 119 vm_helper->num_vmids_available = num_vmid - 1; 120 121 memset(vm_helper->hubp_vmid_usage, 0, sizeof(vm_helper->hubp_vmid_usage[0]) * MAX_HUBP); 122 memset(vm_helper->ptb_assigned_to_vmid, 0, sizeof(vm_helper->ptb_assigned_to_vmid[0]) * MAX_VMID); 123 } 124