1 /* 2 * Copyright 2014 Advanced Micro Devices, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19 * USE OR OTHER DEALINGS IN THE SOFTWARE. 20 * 21 * The above copyright notice and this permission notice (including the 22 * next paragraph) shall be included in all copies or substantial portions 23 * of the Software. 24 * 25 */ 26 /* 27 * Authors: 28 * Christian König <christian.koenig@amd.com> 29 */ 30 31 /** 32 * DOC: MMU Notifier 33 * 34 * For coherent userptr handling registers an MMU notifier to inform the driver 35 * about updates on the page tables of a process. 36 * 37 * When somebody tries to invalidate the page tables we block the update until 38 * all operations on the pages in question are completed, then those pages are 39 * marked as accessed and also dirty if it wasn't a read only access. 40 * 41 * New command submissions using the userptrs in question are delayed until all 42 * page table invalidation are completed and we once more see a coherent process 43 * address space. 44 */ 45 46 #include <linux/firmware.h> 47 #include <linux/module.h> 48 #include <drm/drm.h> 49 50 #include "amdgpu.h" 51 #include "amdgpu_amdkfd.h" 52 #include "amdgpu_hmm.h" 53 54 /** 55 * amdgpu_hmm_invalidate_gfx - callback to notify about mm change 56 * 57 * @mni: the range (mm) is about to update 58 * @range: details on the invalidation 59 * @cur_seq: Value to pass to mmu_interval_set_seq() 60 * 61 * Block for operations on BOs to finish and mark pages as accessed and 62 * potentially dirty. 63 */ 64 static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni, 65 const struct mmu_notifier_range *range, 66 unsigned long cur_seq) 67 { 68 struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier); 69 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 70 long r; 71 72 if (!mmu_notifier_range_blockable(range)) 73 return false; 74 75 mutex_lock(&adev->notifier_lock); 76 77 mmu_interval_set_seq(mni, cur_seq); 78 79 amdgpu_vm_bo_invalidate(bo, false); 80 r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP, 81 false, MAX_SCHEDULE_TIMEOUT); 82 mutex_unlock(&adev->notifier_lock); 83 if (r <= 0) 84 DRM_ERROR("(%ld) failed to wait for user bo\n", r); 85 return true; 86 } 87 88 static const struct mmu_interval_notifier_ops amdgpu_hmm_gfx_ops = { 89 .invalidate = amdgpu_hmm_invalidate_gfx, 90 }; 91 92 /** 93 * amdgpu_hmm_invalidate_hsa - callback to notify about mm change 94 * 95 * @mni: the range (mm) is about to update 96 * @range: details on the invalidation 97 * @cur_seq: Value to pass to mmu_interval_set_seq() 98 * 99 * We temporarily evict the BO attached to this range. This necessitates 100 * evicting all user-mode queues of the process. 101 */ 102 static bool amdgpu_hmm_invalidate_hsa(struct mmu_interval_notifier *mni, 103 const struct mmu_notifier_range *range, 104 unsigned long cur_seq) 105 { 106 struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier); 107 108 if (!mmu_notifier_range_blockable(range)) 109 return false; 110 111 amdgpu_amdkfd_evict_userptr(mni, cur_seq, bo->kfd_bo); 112 113 return true; 114 } 115 116 static const struct mmu_interval_notifier_ops amdgpu_hmm_hsa_ops = { 117 .invalidate = amdgpu_hmm_invalidate_hsa, 118 }; 119 120 /** 121 * amdgpu_hmm_register - register a BO for notifier updates 122 * 123 * @bo: amdgpu buffer object 124 * @addr: userptr addr we should monitor 125 * 126 * Registers a mmu_notifier for the given BO at the specified address. 127 * Returns 0 on success, -ERRNO if anything goes wrong. 128 */ 129 int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr) 130 { 131 int r; 132 133 if (bo->kfd_bo) 134 r = mmu_interval_notifier_insert(&bo->notifier, current->mm, 135 addr, amdgpu_bo_size(bo), 136 &amdgpu_hmm_hsa_ops); 137 else 138 r = mmu_interval_notifier_insert(&bo->notifier, current->mm, addr, 139 amdgpu_bo_size(bo), 140 &amdgpu_hmm_gfx_ops); 141 if (r) 142 /* 143 * Make sure amdgpu_hmm_unregister() doesn't call 144 * mmu_interval_notifier_remove() when the notifier isn't properly 145 * initialized. 146 */ 147 bo->notifier.mm = NULL; 148 149 return r; 150 } 151 152 /** 153 * amdgpu_hmm_unregister - unregister a BO for notifier updates 154 * 155 * @bo: amdgpu buffer object 156 * 157 * Remove any registration of mmu notifier updates from the buffer object. 158 */ 159 void amdgpu_hmm_unregister(struct amdgpu_bo *bo) 160 { 161 if (!bo->notifier.mm) 162 return; 163 mmu_interval_notifier_remove(&bo->notifier); 164 bo->notifier.mm = NULL; 165 } 166 167 int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier, 168 uint64_t start, uint64_t npages, bool readonly, 169 void *owner, 170 struct amdgpu_hmm_range *range) 171 { 172 const u64 max_bytes = SZ_2G; 173 174 struct hmm_range *hmm_range = &range->hmm_range; 175 unsigned long timeout; 176 unsigned long *pfns; 177 unsigned long end; 178 int r; 179 180 pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL); 181 if (unlikely(!pfns)) { 182 r = -ENOMEM; 183 goto out_free_range; 184 } 185 186 hmm_range->notifier = notifier; 187 hmm_range->default_flags = HMM_PFN_REQ_FAULT; 188 if (!readonly) 189 hmm_range->default_flags |= HMM_PFN_REQ_WRITE; 190 hmm_range->hmm_pfns = pfns; 191 hmm_range->start = start; 192 end = start + npages * PAGE_SIZE; 193 hmm_range->dev_private_owner = owner; 194 195 hmm_range->notifier_seq = mmu_interval_read_begin(notifier); 196 do { 197 hmm_range->end = min(hmm_range->start + max_bytes, end); 198 199 pr_debug("hmm range: start = 0x%lx, end = 0x%lx", 200 hmm_range->start, hmm_range->end); 201 202 timeout = jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); 203 204 retry: 205 r = hmm_range_fault(hmm_range); 206 if (unlikely(r)) { 207 if (r == -EBUSY && !time_after(jiffies, timeout)) 208 goto retry; 209 goto out_free_pfns; 210 } 211 212 if (hmm_range->end == end) 213 break; 214 hmm_range->hmm_pfns += max_bytes >> PAGE_SHIFT; 215 hmm_range->start = hmm_range->end; 216 } while (hmm_range->end < end); 217 218 hmm_range->start = start; 219 hmm_range->hmm_pfns = pfns; 220 221 return 0; 222 223 out_free_pfns: 224 kvfree(pfns); 225 hmm_range->hmm_pfns = NULL; 226 out_free_range: 227 if (r == -EBUSY) 228 r = -EAGAIN; 229 return r; 230 } 231 232 /** 233 * amdgpu_hmm_range_valid - check if an HMM range is still valid 234 * @range: pointer to the &struct amdgpu_hmm_range to validate 235 * 236 * Determines whether the given HMM range @range is still valid by 237 * checking for invalidations via the MMU notifier sequence. This is 238 * typically used to verify that the range has not been invalidated 239 * by concurrent address space updates before it is accessed. 240 * 241 * Return: 242 * * true if @range is valid and can be used safely 243 * * false if @range is NULL or has been invalidated 244 */ 245 bool amdgpu_hmm_range_valid(struct amdgpu_hmm_range *range) 246 { 247 if (!range) 248 return false; 249 250 return !mmu_interval_read_retry(range->hmm_range.notifier, 251 range->hmm_range.notifier_seq); 252 } 253 254 /** 255 * amdgpu_hmm_range_alloc - allocate and initialize an AMDGPU HMM range 256 * @bo: optional buffer object to associate with this HMM range 257 * 258 * Allocates memory for amdgpu_hmm_range and associates it with the @bo passed. 259 * The reference count of the @bo is incremented. 260 * 261 * Return: 262 * Pointer to a newly allocated struct amdgpu_hmm_range on success, 263 * or NULL if memory allocation fails. 264 */ 265 struct amdgpu_hmm_range *amdgpu_hmm_range_alloc(struct amdgpu_bo *bo) 266 { 267 struct amdgpu_hmm_range *range; 268 269 range = kzalloc_obj(*range); 270 if (!range) 271 return NULL; 272 273 range->bo = amdgpu_bo_ref(bo); 274 return range; 275 } 276 277 /** 278 * amdgpu_hmm_range_free - release an AMDGPU HMM range 279 * @range: pointer to the range object to free 280 * 281 * Releases all resources held by @range, including the associated 282 * hmm_pfns and the dropping reference of associated bo if any. 283 * 284 * Return: void 285 */ 286 void amdgpu_hmm_range_free(struct amdgpu_hmm_range *range) 287 { 288 if (!range) 289 return; 290 291 kvfree(range->hmm_range.hmm_pfns); 292 amdgpu_bo_unref(&range->bo); 293 kfree(range); 294 } 295