]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_hmm.c
Merge tag 'x86_mm_for_6.2_v2' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_hmm.c
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 <[email protected]>
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 #define MAX_WALK_BYTE   (2UL << 30)
55
56 /**
57  * amdgpu_hmm_invalidate_gfx - callback to notify about mm change
58  *
59  * @mni: the range (mm) is about to update
60  * @range: details on the invalidation
61  * @cur_seq: Value to pass to mmu_interval_set_seq()
62  *
63  * Block for operations on BOs to finish and mark pages as accessed and
64  * potentially dirty.
65  */
66 static bool amdgpu_hmm_invalidate_gfx(struct mmu_interval_notifier *mni,
67                                       const struct mmu_notifier_range *range,
68                                       unsigned long cur_seq)
69 {
70         struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
71         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
72         long r;
73
74         if (!mmu_notifier_range_blockable(range))
75                 return false;
76
77         mutex_lock(&adev->notifier_lock);
78
79         mmu_interval_set_seq(mni, cur_seq);
80
81         r = dma_resv_wait_timeout(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP,
82                                   false, MAX_SCHEDULE_TIMEOUT);
83         mutex_unlock(&adev->notifier_lock);
84         if (r <= 0)
85                 DRM_ERROR("(%ld) failed to wait for user bo\n", r);
86         return true;
87 }
88
89 static const struct mmu_interval_notifier_ops amdgpu_hmm_gfx_ops = {
90         .invalidate = amdgpu_hmm_invalidate_gfx,
91 };
92
93 /**
94  * amdgpu_hmm_invalidate_hsa - callback to notify about mm change
95  *
96  * @mni: the range (mm) is about to update
97  * @range: details on the invalidation
98  * @cur_seq: Value to pass to mmu_interval_set_seq()
99  *
100  * We temporarily evict the BO attached to this range. This necessitates
101  * evicting all user-mode queues of the process.
102  */
103 static bool amdgpu_hmm_invalidate_hsa(struct mmu_interval_notifier *mni,
104                                       const struct mmu_notifier_range *range,
105                                       unsigned long cur_seq)
106 {
107         struct amdgpu_bo *bo = container_of(mni, struct amdgpu_bo, notifier);
108         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
109
110         if (!mmu_notifier_range_blockable(range))
111                 return false;
112
113         mutex_lock(&adev->notifier_lock);
114
115         mmu_interval_set_seq(mni, cur_seq);
116
117         amdgpu_amdkfd_evict_userptr(bo->kfd_bo, bo->notifier.mm);
118         mutex_unlock(&adev->notifier_lock);
119
120         return true;
121 }
122
123 static const struct mmu_interval_notifier_ops amdgpu_hmm_hsa_ops = {
124         .invalidate = amdgpu_hmm_invalidate_hsa,
125 };
126
127 /**
128  * amdgpu_hmm_register - register a BO for notifier updates
129  *
130  * @bo: amdgpu buffer object
131  * @addr: userptr addr we should monitor
132  *
133  * Registers a mmu_notifier for the given BO at the specified address.
134  * Returns 0 on success, -ERRNO if anything goes wrong.
135  */
136 int amdgpu_hmm_register(struct amdgpu_bo *bo, unsigned long addr)
137 {
138         if (bo->kfd_bo)
139                 return mmu_interval_notifier_insert(&bo->notifier, current->mm,
140                                                     addr, amdgpu_bo_size(bo),
141                                                     &amdgpu_hmm_hsa_ops);
142         return mmu_interval_notifier_insert(&bo->notifier, current->mm, addr,
143                                             amdgpu_bo_size(bo),
144                                             &amdgpu_hmm_gfx_ops);
145 }
146
147 /**
148  * amdgpu_hmm_unregister - unregister a BO for notifier updates
149  *
150  * @bo: amdgpu buffer object
151  *
152  * Remove any registration of mmu notifier updates from the buffer object.
153  */
154 void amdgpu_hmm_unregister(struct amdgpu_bo *bo)
155 {
156         if (!bo->notifier.mm)
157                 return;
158         mmu_interval_notifier_remove(&bo->notifier);
159         bo->notifier.mm = NULL;
160 }
161
162 int amdgpu_hmm_range_get_pages(struct mmu_interval_notifier *notifier,
163                                uint64_t start, uint64_t npages, bool readonly,
164                                void *owner, struct page **pages,
165                                struct hmm_range **phmm_range)
166 {
167         struct hmm_range *hmm_range;
168         unsigned long end;
169         unsigned long timeout;
170         unsigned long i;
171         unsigned long *pfns;
172         int r = 0;
173
174         hmm_range = kzalloc(sizeof(*hmm_range), GFP_KERNEL);
175         if (unlikely(!hmm_range))
176                 return -ENOMEM;
177
178         pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
179         if (unlikely(!pfns)) {
180                 r = -ENOMEM;
181                 goto out_free_range;
182         }
183
184         hmm_range->notifier = notifier;
185         hmm_range->default_flags = HMM_PFN_REQ_FAULT;
186         if (!readonly)
187                 hmm_range->default_flags |= HMM_PFN_REQ_WRITE;
188         hmm_range->hmm_pfns = pfns;
189         hmm_range->start = start;
190         end = start + npages * PAGE_SIZE;
191         hmm_range->dev_private_owner = owner;
192
193         do {
194                 hmm_range->end = min(hmm_range->start + MAX_WALK_BYTE, end);
195
196                 pr_debug("hmm range: start = 0x%lx, end = 0x%lx",
197                         hmm_range->start, hmm_range->end);
198
199                 /* Assuming 512MB takes maxmium 1 second to fault page address */
200                 timeout = max((hmm_range->end - hmm_range->start) >> 29, 1UL);
201                 timeout *= HMM_RANGE_DEFAULT_TIMEOUT;
202                 timeout = jiffies + msecs_to_jiffies(timeout);
203
204 retry:
205                 hmm_range->notifier_seq = mmu_interval_read_begin(notifier);
206                 r = hmm_range_fault(hmm_range);
207                 if (unlikely(r)) {
208                         /*
209                          * FIXME: This timeout should encompass the retry from
210                          * mmu_interval_read_retry() as well.
211                          */
212                         if (r == -EBUSY && !time_after(jiffies, timeout))
213                                 goto retry;
214                         goto out_free_pfns;
215                 }
216
217                 if (hmm_range->end == end)
218                         break;
219                 hmm_range->hmm_pfns += MAX_WALK_BYTE >> PAGE_SHIFT;
220                 hmm_range->start = hmm_range->end;
221                 schedule();
222         } while (hmm_range->end < end);
223
224         hmm_range->start = start;
225         hmm_range->hmm_pfns = pfns;
226
227         /*
228          * Due to default_flags, all pages are HMM_PFN_VALID or
229          * hmm_range_fault() fails. FIXME: The pages cannot be touched outside
230          * the notifier_lock, and mmu_interval_read_retry() must be done first.
231          */
232         for (i = 0; pages && i < npages; i++)
233                 pages[i] = hmm_pfn_to_page(pfns[i]);
234
235         *phmm_range = hmm_range;
236
237         return 0;
238
239 out_free_pfns:
240         kvfree(pfns);
241 out_free_range:
242         kfree(hmm_range);
243
244         return r;
245 }
246
247 int amdgpu_hmm_range_get_pages_done(struct hmm_range *hmm_range)
248 {
249         int r;
250
251         r = mmu_interval_read_retry(hmm_range->notifier,
252                                     hmm_range->notifier_seq);
253         kvfree(hmm_range->hmm_pfns);
254         kfree(hmm_range);
255
256         return r;
257 }
This page took 0.045801 seconds and 4 git commands to generate.