xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_ids.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2017 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  */
23b843c749SSergey Zigachev #include "amdgpu_ids.h"
24b843c749SSergey Zigachev 
25b843c749SSergey Zigachev #include <linux/idr.h>
26b843c749SSergey Zigachev #include <linux/dma-fence-array.h>
27b843c749SSergey Zigachev #include <drm/drmP.h>
28b843c749SSergey Zigachev 
29b843c749SSergey Zigachev #include "amdgpu.h"
30b843c749SSergey Zigachev #include "amdgpu_trace.h"
31b843c749SSergey Zigachev 
32b843c749SSergey Zigachev /*
33b843c749SSergey Zigachev  * PASID manager
34b843c749SSergey Zigachev  *
35b843c749SSergey Zigachev  * PASIDs are global address space identifiers that can be shared
36b843c749SSergey Zigachev  * between the GPU, an IOMMU and the driver. VMs on different devices
37b843c749SSergey Zigachev  * may use the same PASID if they share the same address
38b843c749SSergey Zigachev  * space. Therefore PASIDs are allocated using a global IDA. VMs are
39b843c749SSergey Zigachev  * looked up from the PASID per amdgpu_device.
40b843c749SSergey Zigachev  */
41b843c749SSergey Zigachev static DEFINE_IDA(amdgpu_pasid_ida);
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev /* Helper to free pasid from a fence callback */
44b843c749SSergey Zigachev struct amdgpu_pasid_cb {
45b843c749SSergey Zigachev 	struct dma_fence_cb cb;
46b843c749SSergey Zigachev 	unsigned int pasid;
47b843c749SSergey Zigachev };
48b843c749SSergey Zigachev 
49b843c749SSergey Zigachev /**
50b843c749SSergey Zigachev  * amdgpu_pasid_alloc - Allocate a PASID
51b843c749SSergey Zigachev  * @bits: Maximum width of the PASID in bits, must be at least 1
52b843c749SSergey Zigachev  *
53b843c749SSergey Zigachev  * Allocates a PASID of the given width while keeping smaller PASIDs
54b843c749SSergey Zigachev  * available if possible.
55b843c749SSergey Zigachev  *
56b843c749SSergey Zigachev  * Returns a positive integer on success. Returns %-EINVAL if bits==0.
57b843c749SSergey Zigachev  * Returns %-ENOSPC if no PASID was available. Returns %-ENOMEM on
58b843c749SSergey Zigachev  * memory allocation failure.
59b843c749SSergey Zigachev  */
amdgpu_pasid_alloc(unsigned int bits)60b843c749SSergey Zigachev int amdgpu_pasid_alloc(unsigned int bits)
61b843c749SSergey Zigachev {
62b843c749SSergey Zigachev 	int pasid = -EINVAL;
63b843c749SSergey Zigachev 
64b843c749SSergey Zigachev 	for (bits = min(bits, 31U); bits > 0; bits--) {
65b843c749SSergey Zigachev 		pasid = ida_simple_get(&amdgpu_pasid_ida,
66b843c749SSergey Zigachev 				       1U << (bits - 1), 1U << bits,
67b843c749SSergey Zigachev 				       GFP_KERNEL);
68b843c749SSergey Zigachev 		if (pasid != -ENOSPC)
69b843c749SSergey Zigachev 			break;
70b843c749SSergey Zigachev 	}
71b843c749SSergey Zigachev 
72*78973132SSergey Zigachev #if 0
73b843c749SSergey Zigachev 	if (pasid >= 0)
74b843c749SSergey Zigachev 		trace_amdgpu_pasid_allocated(pasid);
75*78973132SSergey Zigachev #endif
76b843c749SSergey Zigachev 
77b843c749SSergey Zigachev 	return pasid;
78b843c749SSergey Zigachev }
79b843c749SSergey Zigachev 
80b843c749SSergey Zigachev /**
81b843c749SSergey Zigachev  * amdgpu_pasid_free - Free a PASID
82b843c749SSergey Zigachev  * @pasid: PASID to free
83b843c749SSergey Zigachev  */
amdgpu_pasid_free(unsigned int pasid)84b843c749SSergey Zigachev void amdgpu_pasid_free(unsigned int pasid)
85b843c749SSergey Zigachev {
86*78973132SSergey Zigachev #if 0
87b843c749SSergey Zigachev 	trace_amdgpu_pasid_freed(pasid);
88*78973132SSergey Zigachev #endif
89b843c749SSergey Zigachev 	ida_simple_remove(&amdgpu_pasid_ida, pasid);
90b843c749SSergey Zigachev }
91b843c749SSergey Zigachev 
amdgpu_pasid_free_cb(struct dma_fence * fence,struct dma_fence_cb * _cb)92b843c749SSergey Zigachev static void amdgpu_pasid_free_cb(struct dma_fence *fence,
93b843c749SSergey Zigachev 				 struct dma_fence_cb *_cb)
94b843c749SSergey Zigachev {
95b843c749SSergey Zigachev 	struct amdgpu_pasid_cb *cb =
96b843c749SSergey Zigachev 		container_of(_cb, struct amdgpu_pasid_cb, cb);
97b843c749SSergey Zigachev 
98b843c749SSergey Zigachev 	amdgpu_pasid_free(cb->pasid);
99b843c749SSergey Zigachev 	dma_fence_put(fence);
100b843c749SSergey Zigachev 	kfree(cb);
101b843c749SSergey Zigachev }
102b843c749SSergey Zigachev 
103b843c749SSergey Zigachev /**
104b843c749SSergey Zigachev  * amdgpu_pasid_free_delayed - free pasid when fences signal
105b843c749SSergey Zigachev  *
106b843c749SSergey Zigachev  * @resv: reservation object with the fences to wait for
107b843c749SSergey Zigachev  * @pasid: pasid to free
108b843c749SSergey Zigachev  *
109b843c749SSergey Zigachev  * Free the pasid only after all the fences in resv are signaled.
110b843c749SSergey Zigachev  */
amdgpu_pasid_free_delayed(struct reservation_object * resv,unsigned int pasid)111b843c749SSergey Zigachev void amdgpu_pasid_free_delayed(struct reservation_object *resv,
112b843c749SSergey Zigachev 			       unsigned int pasid)
113b843c749SSergey Zigachev {
114b843c749SSergey Zigachev 	struct dma_fence *fence, **fences;
115b843c749SSergey Zigachev 	struct amdgpu_pasid_cb *cb;
116b843c749SSergey Zigachev 	unsigned count;
117b843c749SSergey Zigachev 	int r;
118b843c749SSergey Zigachev 
119b843c749SSergey Zigachev 	r = reservation_object_get_fences_rcu(resv, NULL, &count, &fences);
120b843c749SSergey Zigachev 	if (r)
121b843c749SSergey Zigachev 		goto fallback;
122b843c749SSergey Zigachev 
123b843c749SSergey Zigachev 	if (count == 0) {
124b843c749SSergey Zigachev 		amdgpu_pasid_free(pasid);
125b843c749SSergey Zigachev 		return;
126b843c749SSergey Zigachev 	}
127b843c749SSergey Zigachev 
128b843c749SSergey Zigachev 	if (count == 1) {
129b843c749SSergey Zigachev 		fence = fences[0];
130b843c749SSergey Zigachev 		kfree(fences);
131b843c749SSergey Zigachev 	} else {
132b843c749SSergey Zigachev 		uint64_t context = dma_fence_context_alloc(1);
133b843c749SSergey Zigachev 		struct dma_fence_array *array;
134b843c749SSergey Zigachev 
135b843c749SSergey Zigachev 		array = dma_fence_array_create(count, fences, context,
136b843c749SSergey Zigachev 					       1, false);
137b843c749SSergey Zigachev 		if (!array) {
138b843c749SSergey Zigachev 			kfree(fences);
139b843c749SSergey Zigachev 			goto fallback;
140b843c749SSergey Zigachev 		}
141b843c749SSergey Zigachev 		fence = &array->base;
142b843c749SSergey Zigachev 	}
143b843c749SSergey Zigachev 
144*78973132SSergey Zigachev 	cb = kmalloc(sizeof(*cb), M_DRM, GFP_KERNEL);
145b843c749SSergey Zigachev 	if (!cb) {
146b843c749SSergey Zigachev 		/* Last resort when we are OOM */
147b843c749SSergey Zigachev 		dma_fence_wait(fence, false);
148b843c749SSergey Zigachev 		dma_fence_put(fence);
149b843c749SSergey Zigachev 		amdgpu_pasid_free(pasid);
150b843c749SSergey Zigachev 	} else {
151b843c749SSergey Zigachev 		cb->pasid = pasid;
152b843c749SSergey Zigachev 		if (dma_fence_add_callback(fence, &cb->cb,
153b843c749SSergey Zigachev 					   amdgpu_pasid_free_cb))
154b843c749SSergey Zigachev 			amdgpu_pasid_free_cb(fence, &cb->cb);
155b843c749SSergey Zigachev 	}
156b843c749SSergey Zigachev 
157b843c749SSergey Zigachev 	return;
158b843c749SSergey Zigachev 
159b843c749SSergey Zigachev fallback:
160b843c749SSergey Zigachev 	/* Not enough memory for the delayed delete, as last resort
161b843c749SSergey Zigachev 	 * block for all the fences to complete.
162b843c749SSergey Zigachev 	 */
163b843c749SSergey Zigachev 	reservation_object_wait_timeout_rcu(resv, true, false,
164b843c749SSergey Zigachev 					    MAX_SCHEDULE_TIMEOUT);
165b843c749SSergey Zigachev 	amdgpu_pasid_free(pasid);
166b843c749SSergey Zigachev }
167b843c749SSergey Zigachev 
168b843c749SSergey Zigachev /*
169b843c749SSergey Zigachev  * VMID manager
170b843c749SSergey Zigachev  *
171b843c749SSergey Zigachev  * VMIDs are a per VMHUB identifier for page tables handling.
172b843c749SSergey Zigachev  */
173b843c749SSergey Zigachev 
174b843c749SSergey Zigachev /**
175b843c749SSergey Zigachev  * amdgpu_vmid_had_gpu_reset - check if reset occured since last use
176b843c749SSergey Zigachev  *
177b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
178b843c749SSergey Zigachev  * @id: VMID structure
179b843c749SSergey Zigachev  *
180b843c749SSergey Zigachev  * Check if GPU reset occured since last use of the VMID.
181b843c749SSergey Zigachev  */
amdgpu_vmid_had_gpu_reset(struct amdgpu_device * adev,struct amdgpu_vmid * id)182b843c749SSergey Zigachev bool amdgpu_vmid_had_gpu_reset(struct amdgpu_device *adev,
183b843c749SSergey Zigachev 			       struct amdgpu_vmid *id)
184b843c749SSergey Zigachev {
185b843c749SSergey Zigachev 	return id->current_gpu_reset_count !=
186b843c749SSergey Zigachev 		atomic_read(&adev->gpu_reset_counter);
187b843c749SSergey Zigachev }
188b843c749SSergey Zigachev 
189b843c749SSergey Zigachev /**
190b843c749SSergey Zigachev  * amdgpu_vm_grab_idle - grab idle VMID
191b843c749SSergey Zigachev  *
192b843c749SSergey Zigachev  * @vm: vm to allocate id for
193b843c749SSergey Zigachev  * @ring: ring we want to submit job to
194b843c749SSergey Zigachev  * @sync: sync object where we add dependencies
195b843c749SSergey Zigachev  * @idle: resulting idle VMID
196b843c749SSergey Zigachev  *
197b843c749SSergey Zigachev  * Try to find an idle VMID, if none is idle add a fence to wait to the sync
198b843c749SSergey Zigachev  * object. Returns -ENOMEM when we are out of memory.
199b843c749SSergey Zigachev  */
amdgpu_vmid_grab_idle(struct amdgpu_vm * vm,struct amdgpu_ring * ring,struct amdgpu_sync * sync,struct amdgpu_vmid ** idle)200b843c749SSergey Zigachev static int amdgpu_vmid_grab_idle(struct amdgpu_vm *vm,
201b843c749SSergey Zigachev 				 struct amdgpu_ring *ring,
202b843c749SSergey Zigachev 				 struct amdgpu_sync *sync,
203b843c749SSergey Zigachev 				 struct amdgpu_vmid **idle)
204b843c749SSergey Zigachev {
205b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
206b843c749SSergey Zigachev 	unsigned vmhub = ring->funcs->vmhub;
207b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
208b843c749SSergey Zigachev 	struct dma_fence **fences;
209b843c749SSergey Zigachev 	unsigned i;
210b843c749SSergey Zigachev 	int r;
211b843c749SSergey Zigachev 
212b843c749SSergey Zigachev 	if (ring->vmid_wait && !dma_fence_is_signaled(ring->vmid_wait))
213b843c749SSergey Zigachev 		return amdgpu_sync_fence(adev, sync, ring->vmid_wait, false);
214b843c749SSergey Zigachev 
215b843c749SSergey Zigachev 	fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
216b843c749SSergey Zigachev 	if (!fences)
217b843c749SSergey Zigachev 		return -ENOMEM;
218b843c749SSergey Zigachev 
219b843c749SSergey Zigachev 	/* Check if we have an idle VMID */
220b843c749SSergey Zigachev 	i = 0;
221b843c749SSergey Zigachev 	list_for_each_entry((*idle), &id_mgr->ids_lru, list) {
222b843c749SSergey Zigachev 		fences[i] = amdgpu_sync_peek_fence(&(*idle)->active, ring);
223b843c749SSergey Zigachev 		if (!fences[i])
224b843c749SSergey Zigachev 			break;
225b843c749SSergey Zigachev 		++i;
226b843c749SSergey Zigachev 	}
227b843c749SSergey Zigachev 
228b843c749SSergey Zigachev 	/* If we can't find a idle VMID to use, wait till one becomes available */
229b843c749SSergey Zigachev 	if (&(*idle)->list == &id_mgr->ids_lru) {
230b843c749SSergey Zigachev 		u64 fence_context = adev->vm_manager.fence_context + ring->idx;
231b843c749SSergey Zigachev 		unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
232b843c749SSergey Zigachev 		struct dma_fence_array *array;
233b843c749SSergey Zigachev 		unsigned j;
234b843c749SSergey Zigachev 
235b843c749SSergey Zigachev 		*idle = NULL;
236b843c749SSergey Zigachev 		for (j = 0; j < i; ++j)
237b843c749SSergey Zigachev 			dma_fence_get(fences[j]);
238b843c749SSergey Zigachev 
239b843c749SSergey Zigachev 		array = dma_fence_array_create(i, fences, fence_context,
240b843c749SSergey Zigachev 					       seqno, true);
241b843c749SSergey Zigachev 		if (!array) {
242b843c749SSergey Zigachev 			for (j = 0; j < i; ++j)
243b843c749SSergey Zigachev 				dma_fence_put(fences[j]);
244b843c749SSergey Zigachev 			kfree(fences);
245b843c749SSergey Zigachev 			return -ENOMEM;
246b843c749SSergey Zigachev 		}
247b843c749SSergey Zigachev 
248b843c749SSergey Zigachev 		r = amdgpu_sync_fence(adev, sync, &array->base, false);
249b843c749SSergey Zigachev 		dma_fence_put(ring->vmid_wait);
250b843c749SSergey Zigachev 		ring->vmid_wait = &array->base;
251b843c749SSergey Zigachev 		return r;
252b843c749SSergey Zigachev 	}
253b843c749SSergey Zigachev 	kfree(fences);
254b843c749SSergey Zigachev 
255b843c749SSergey Zigachev 	return 0;
256b843c749SSergey Zigachev }
257b843c749SSergey Zigachev 
258b843c749SSergey Zigachev /**
259b843c749SSergey Zigachev  * amdgpu_vm_grab_reserved - try to assign reserved VMID
260b843c749SSergey Zigachev  *
261b843c749SSergey Zigachev  * @vm: vm to allocate id for
262b843c749SSergey Zigachev  * @ring: ring we want to submit job to
263b843c749SSergey Zigachev  * @sync: sync object where we add dependencies
264b843c749SSergey Zigachev  * @fence: fence protecting ID from reuse
265b843c749SSergey Zigachev  * @job: job who wants to use the VMID
266b843c749SSergey Zigachev  *
267b843c749SSergey Zigachev  * Try to assign a reserved VMID.
268b843c749SSergey Zigachev  */
amdgpu_vmid_grab_reserved(struct amdgpu_vm * vm,struct amdgpu_ring * ring,struct amdgpu_sync * sync,struct dma_fence * fence,struct amdgpu_job * job,struct amdgpu_vmid ** id)269b843c749SSergey Zigachev static int amdgpu_vmid_grab_reserved(struct amdgpu_vm *vm,
270b843c749SSergey Zigachev 				     struct amdgpu_ring *ring,
271b843c749SSergey Zigachev 				     struct amdgpu_sync *sync,
272b843c749SSergey Zigachev 				     struct dma_fence *fence,
273b843c749SSergey Zigachev 				     struct amdgpu_job *job,
274b843c749SSergey Zigachev 				     struct amdgpu_vmid **id)
275b843c749SSergey Zigachev {
276b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
277b843c749SSergey Zigachev 	unsigned vmhub = ring->funcs->vmhub;
278b843c749SSergey Zigachev 	uint64_t fence_context = adev->fence_context + ring->idx;
279b843c749SSergey Zigachev 	struct dma_fence *updates = sync->last_vm_update;
280b843c749SSergey Zigachev 	bool needs_flush = vm->use_cpu_for_update;
281b843c749SSergey Zigachev 	int r = 0;
282b843c749SSergey Zigachev 
283b843c749SSergey Zigachev 	*id = vm->reserved_vmid[vmhub];
284b843c749SSergey Zigachev 	if (updates && (*id)->flushed_updates &&
285b843c749SSergey Zigachev 	    updates->context == (*id)->flushed_updates->context &&
286b843c749SSergey Zigachev 	    !dma_fence_is_later(updates, (*id)->flushed_updates))
287b843c749SSergey Zigachev 	    updates = NULL;
288b843c749SSergey Zigachev 
289b843c749SSergey Zigachev 	if ((*id)->owner != vm->entity.fence_context ||
290b843c749SSergey Zigachev 	    job->vm_pd_addr != (*id)->pd_gpu_addr ||
291b843c749SSergey Zigachev 	    updates || !(*id)->last_flush ||
292b843c749SSergey Zigachev 	    ((*id)->last_flush->context != fence_context &&
293b843c749SSergey Zigachev 	     !dma_fence_is_signaled((*id)->last_flush))) {
294b843c749SSergey Zigachev 		struct dma_fence *tmp;
295b843c749SSergey Zigachev 
296b843c749SSergey Zigachev 		/* to prevent one context starved by another context */
297b843c749SSergey Zigachev 		(*id)->pd_gpu_addr = 0;
298b843c749SSergey Zigachev 		tmp = amdgpu_sync_peek_fence(&(*id)->active, ring);
299b843c749SSergey Zigachev 		if (tmp) {
300b843c749SSergey Zigachev 			*id = NULL;
301b843c749SSergey Zigachev 			r = amdgpu_sync_fence(adev, sync, tmp, false);
302b843c749SSergey Zigachev 			return r;
303b843c749SSergey Zigachev 		}
304b843c749SSergey Zigachev 		needs_flush = true;
305b843c749SSergey Zigachev 	}
306b843c749SSergey Zigachev 
307b843c749SSergey Zigachev 	/* Good we can use this VMID. Remember this submission as
308b843c749SSergey Zigachev 	* user of the VMID.
309b843c749SSergey Zigachev 	*/
310b843c749SSergey Zigachev 	r = amdgpu_sync_fence(ring->adev, &(*id)->active, fence, false);
311b843c749SSergey Zigachev 	if (r)
312b843c749SSergey Zigachev 		return r;
313b843c749SSergey Zigachev 
314b843c749SSergey Zigachev 	if (updates) {
315b843c749SSergey Zigachev 		dma_fence_put((*id)->flushed_updates);
316b843c749SSergey Zigachev 		(*id)->flushed_updates = dma_fence_get(updates);
317b843c749SSergey Zigachev 	}
318b843c749SSergey Zigachev 	job->vm_needs_flush = needs_flush;
319b843c749SSergey Zigachev 	return 0;
320b843c749SSergey Zigachev }
321b843c749SSergey Zigachev 
322b843c749SSergey Zigachev /**
323b843c749SSergey Zigachev  * amdgpu_vm_grab_used - try to reuse a VMID
324b843c749SSergey Zigachev  *
325b843c749SSergey Zigachev  * @vm: vm to allocate id for
326b843c749SSergey Zigachev  * @ring: ring we want to submit job to
327b843c749SSergey Zigachev  * @sync: sync object where we add dependencies
328b843c749SSergey Zigachev  * @fence: fence protecting ID from reuse
329b843c749SSergey Zigachev  * @job: job who wants to use the VMID
330b843c749SSergey Zigachev  * @id: resulting VMID
331b843c749SSergey Zigachev  *
332b843c749SSergey Zigachev  * Try to reuse a VMID for this submission.
333b843c749SSergey Zigachev  */
amdgpu_vmid_grab_used(struct amdgpu_vm * vm,struct amdgpu_ring * ring,struct amdgpu_sync * sync,struct dma_fence * fence,struct amdgpu_job * job,struct amdgpu_vmid ** id)334b843c749SSergey Zigachev static int amdgpu_vmid_grab_used(struct amdgpu_vm *vm,
335b843c749SSergey Zigachev 				 struct amdgpu_ring *ring,
336b843c749SSergey Zigachev 				 struct amdgpu_sync *sync,
337b843c749SSergey Zigachev 				 struct dma_fence *fence,
338b843c749SSergey Zigachev 				 struct amdgpu_job *job,
339b843c749SSergey Zigachev 				 struct amdgpu_vmid **id)
340b843c749SSergey Zigachev {
341b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
342b843c749SSergey Zigachev 	unsigned vmhub = ring->funcs->vmhub;
343b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
344b843c749SSergey Zigachev 	uint64_t fence_context = adev->fence_context + ring->idx;
345b843c749SSergey Zigachev 	struct dma_fence *updates = sync->last_vm_update;
346b843c749SSergey Zigachev 	int r;
347b843c749SSergey Zigachev 
348b843c749SSergey Zigachev 	job->vm_needs_flush = vm->use_cpu_for_update;
349b843c749SSergey Zigachev 
350b843c749SSergey Zigachev 	/* Check if we can use a VMID already assigned to this VM */
351b843c749SSergey Zigachev 	list_for_each_entry_reverse((*id), &id_mgr->ids_lru, list) {
352b843c749SSergey Zigachev 		bool needs_flush = vm->use_cpu_for_update;
353b843c749SSergey Zigachev 		struct dma_fence *flushed;
354b843c749SSergey Zigachev 
355b843c749SSergey Zigachev 		/* Check all the prerequisites to using this VMID */
356b843c749SSergey Zigachev 		if ((*id)->owner != vm->entity.fence_context)
357b843c749SSergey Zigachev 			continue;
358b843c749SSergey Zigachev 
359b843c749SSergey Zigachev 		if ((*id)->pd_gpu_addr != job->vm_pd_addr)
360b843c749SSergey Zigachev 			continue;
361b843c749SSergey Zigachev 
362b843c749SSergey Zigachev 		if (!(*id)->last_flush ||
363b843c749SSergey Zigachev 		    ((*id)->last_flush->context != fence_context &&
364b843c749SSergey Zigachev 		     !dma_fence_is_signaled((*id)->last_flush)))
365b843c749SSergey Zigachev 			needs_flush = true;
366b843c749SSergey Zigachev 
367b843c749SSergey Zigachev 		flushed  = (*id)->flushed_updates;
368b843c749SSergey Zigachev 		if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
369b843c749SSergey Zigachev 			needs_flush = true;
370b843c749SSergey Zigachev 
371b843c749SSergey Zigachev 		/* Concurrent flushes are only possible starting with Vega10 */
372b843c749SSergey Zigachev 		if (adev->asic_type < CHIP_VEGA10 && needs_flush)
373b843c749SSergey Zigachev 			continue;
374b843c749SSergey Zigachev 
375b843c749SSergey Zigachev 		/* Good, we can use this VMID. Remember this submission as
376b843c749SSergey Zigachev 		 * user of the VMID.
377b843c749SSergey Zigachev 		 */
378b843c749SSergey Zigachev 		r = amdgpu_sync_fence(ring->adev, &(*id)->active, fence, false);
379b843c749SSergey Zigachev 		if (r)
380b843c749SSergey Zigachev 			return r;
381b843c749SSergey Zigachev 
382b843c749SSergey Zigachev 		if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
383b843c749SSergey Zigachev 			dma_fence_put((*id)->flushed_updates);
384b843c749SSergey Zigachev 			(*id)->flushed_updates = dma_fence_get(updates);
385b843c749SSergey Zigachev 		}
386b843c749SSergey Zigachev 
387b843c749SSergey Zigachev 		job->vm_needs_flush |= needs_flush;
388b843c749SSergey Zigachev 		return 0;
389b843c749SSergey Zigachev 	}
390b843c749SSergey Zigachev 
391b843c749SSergey Zigachev 	*id = NULL;
392b843c749SSergey Zigachev 	return 0;
393b843c749SSergey Zigachev }
394b843c749SSergey Zigachev 
395b843c749SSergey Zigachev /**
396b843c749SSergey Zigachev  * amdgpu_vm_grab_id - allocate the next free VMID
397b843c749SSergey Zigachev  *
398b843c749SSergey Zigachev  * @vm: vm to allocate id for
399b843c749SSergey Zigachev  * @ring: ring we want to submit job to
400b843c749SSergey Zigachev  * @sync: sync object where we add dependencies
401b843c749SSergey Zigachev  * @fence: fence protecting ID from reuse
402b843c749SSergey Zigachev  * @job: job who wants to use the VMID
403b843c749SSergey Zigachev  *
404b843c749SSergey Zigachev  * Allocate an id for the vm, adding fences to the sync obj as necessary.
405b843c749SSergey Zigachev  */
amdgpu_vmid_grab(struct amdgpu_vm * vm,struct amdgpu_ring * ring,struct amdgpu_sync * sync,struct dma_fence * fence,struct amdgpu_job * job)406b843c749SSergey Zigachev int amdgpu_vmid_grab(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
407b843c749SSergey Zigachev 		     struct amdgpu_sync *sync, struct dma_fence *fence,
408b843c749SSergey Zigachev 		     struct amdgpu_job *job)
409b843c749SSergey Zigachev {
410b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
411b843c749SSergey Zigachev 	unsigned vmhub = ring->funcs->vmhub;
412b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
413b843c749SSergey Zigachev 	struct amdgpu_vmid *idle = NULL;
414b843c749SSergey Zigachev 	struct amdgpu_vmid *id = NULL;
415b843c749SSergey Zigachev 	int r = 0;
416b843c749SSergey Zigachev 
417b843c749SSergey Zigachev 	mutex_lock(&id_mgr->lock);
418b843c749SSergey Zigachev 	r = amdgpu_vmid_grab_idle(vm, ring, sync, &idle);
419b843c749SSergey Zigachev 	if (r || !idle)
420b843c749SSergey Zigachev 		goto error;
421b843c749SSergey Zigachev 
422b843c749SSergey Zigachev 	if (vm->reserved_vmid[vmhub]) {
423b843c749SSergey Zigachev 		r = amdgpu_vmid_grab_reserved(vm, ring, sync, fence, job, &id);
424b843c749SSergey Zigachev 		if (r || !id)
425b843c749SSergey Zigachev 			goto error;
426b843c749SSergey Zigachev 	} else {
427b843c749SSergey Zigachev 		r = amdgpu_vmid_grab_used(vm, ring, sync, fence, job, &id);
428b843c749SSergey Zigachev 		if (r)
429b843c749SSergey Zigachev 			goto error;
430b843c749SSergey Zigachev 
431b843c749SSergey Zigachev 		if (!id) {
432b843c749SSergey Zigachev 			struct dma_fence *updates = sync->last_vm_update;
433b843c749SSergey Zigachev 
434b843c749SSergey Zigachev 			/* Still no ID to use? Then use the idle one found earlier */
435b843c749SSergey Zigachev 			id = idle;
436b843c749SSergey Zigachev 
437b843c749SSergey Zigachev 			/* Remember this submission as user of the VMID */
438b843c749SSergey Zigachev 			r = amdgpu_sync_fence(ring->adev, &id->active,
439b843c749SSergey Zigachev 					      fence, false);
440b843c749SSergey Zigachev 			if (r)
441b843c749SSergey Zigachev 				goto error;
442b843c749SSergey Zigachev 
443b843c749SSergey Zigachev 			dma_fence_put(id->flushed_updates);
444b843c749SSergey Zigachev 			id->flushed_updates = dma_fence_get(updates);
445b843c749SSergey Zigachev 			job->vm_needs_flush = true;
446b843c749SSergey Zigachev 		}
447b843c749SSergey Zigachev 
448b843c749SSergey Zigachev 		list_move_tail(&id->list, &id_mgr->ids_lru);
449b843c749SSergey Zigachev 	}
450b843c749SSergey Zigachev 
451b843c749SSergey Zigachev 	id->pd_gpu_addr = job->vm_pd_addr;
452b843c749SSergey Zigachev 	id->owner = vm->entity.fence_context;
453b843c749SSergey Zigachev 
454b843c749SSergey Zigachev 	if (job->vm_needs_flush) {
455b843c749SSergey Zigachev 		dma_fence_put(id->last_flush);
456b843c749SSergey Zigachev 		id->last_flush = NULL;
457b843c749SSergey Zigachev 	}
458b843c749SSergey Zigachev 	job->vmid = id - id_mgr->ids;
459b843c749SSergey Zigachev 	job->pasid = vm->pasid;
460b843c749SSergey Zigachev 	trace_amdgpu_vm_grab_id(vm, ring, job);
461b843c749SSergey Zigachev 
462b843c749SSergey Zigachev error:
463b843c749SSergey Zigachev 	mutex_unlock(&id_mgr->lock);
464b843c749SSergey Zigachev 	return r;
465b843c749SSergey Zigachev }
466b843c749SSergey Zigachev 
amdgpu_vmid_alloc_reserved(struct amdgpu_device * adev,struct amdgpu_vm * vm,unsigned vmhub)467b843c749SSergey Zigachev int amdgpu_vmid_alloc_reserved(struct amdgpu_device *adev,
468b843c749SSergey Zigachev 			       struct amdgpu_vm *vm,
469b843c749SSergey Zigachev 			       unsigned vmhub)
470b843c749SSergey Zigachev {
471b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr;
472b843c749SSergey Zigachev 	struct amdgpu_vmid *idle;
473b843c749SSergey Zigachev 	int r = 0;
474b843c749SSergey Zigachev 
475b843c749SSergey Zigachev 	id_mgr = &adev->vm_manager.id_mgr[vmhub];
476b843c749SSergey Zigachev 	mutex_lock(&id_mgr->lock);
477b843c749SSergey Zigachev 	if (vm->reserved_vmid[vmhub])
478b843c749SSergey Zigachev 		goto unlock;
479b843c749SSergey Zigachev 	if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
480b843c749SSergey Zigachev 	    AMDGPU_VM_MAX_RESERVED_VMID) {
481b843c749SSergey Zigachev 		DRM_ERROR("Over limitation of reserved vmid\n");
482b843c749SSergey Zigachev 		atomic_dec(&id_mgr->reserved_vmid_num);
483b843c749SSergey Zigachev 		r = -EINVAL;
484b843c749SSergey Zigachev 		goto unlock;
485b843c749SSergey Zigachev 	}
486b843c749SSergey Zigachev 	/* Select the first entry VMID */
487b843c749SSergey Zigachev 	idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vmid, list);
488b843c749SSergey Zigachev 	list_del_init(&idle->list);
489b843c749SSergey Zigachev 	vm->reserved_vmid[vmhub] = idle;
490b843c749SSergey Zigachev 	mutex_unlock(&id_mgr->lock);
491b843c749SSergey Zigachev 
492b843c749SSergey Zigachev 	return 0;
493b843c749SSergey Zigachev unlock:
494b843c749SSergey Zigachev 	mutex_unlock(&id_mgr->lock);
495b843c749SSergey Zigachev 	return r;
496b843c749SSergey Zigachev }
497b843c749SSergey Zigachev 
amdgpu_vmid_free_reserved(struct amdgpu_device * adev,struct amdgpu_vm * vm,unsigned vmhub)498b843c749SSergey Zigachev void amdgpu_vmid_free_reserved(struct amdgpu_device *adev,
499b843c749SSergey Zigachev 			       struct amdgpu_vm *vm,
500b843c749SSergey Zigachev 			       unsigned vmhub)
501b843c749SSergey Zigachev {
502b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
503b843c749SSergey Zigachev 
504b843c749SSergey Zigachev 	mutex_lock(&id_mgr->lock);
505b843c749SSergey Zigachev 	if (vm->reserved_vmid[vmhub]) {
506b843c749SSergey Zigachev 		list_add(&vm->reserved_vmid[vmhub]->list,
507b843c749SSergey Zigachev 			&id_mgr->ids_lru);
508b843c749SSergey Zigachev 		vm->reserved_vmid[vmhub] = NULL;
509b843c749SSergey Zigachev 		atomic_dec(&id_mgr->reserved_vmid_num);
510b843c749SSergey Zigachev 	}
511b843c749SSergey Zigachev 	mutex_unlock(&id_mgr->lock);
512b843c749SSergey Zigachev }
513b843c749SSergey Zigachev 
514b843c749SSergey Zigachev /**
515b843c749SSergey Zigachev  * amdgpu_vmid_reset - reset VMID to zero
516b843c749SSergey Zigachev  *
517b843c749SSergey Zigachev  * @adev: amdgpu device structure
518b843c749SSergey Zigachev  * @vmid: vmid number to use
519b843c749SSergey Zigachev  *
520b843c749SSergey Zigachev  * Reset saved GDW, GWS and OA to force switch on next flush.
521b843c749SSergey Zigachev  */
amdgpu_vmid_reset(struct amdgpu_device * adev,unsigned vmhub,unsigned vmid)522b843c749SSergey Zigachev void amdgpu_vmid_reset(struct amdgpu_device *adev, unsigned vmhub,
523b843c749SSergey Zigachev 		       unsigned vmid)
524b843c749SSergey Zigachev {
525b843c749SSergey Zigachev 	struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
526b843c749SSergey Zigachev 	struct amdgpu_vmid *id = &id_mgr->ids[vmid];
527b843c749SSergey Zigachev 
528b843c749SSergey Zigachev 	mutex_lock(&id_mgr->lock);
529b843c749SSergey Zigachev 	id->owner = 0;
530b843c749SSergey Zigachev 	id->gds_base = 0;
531b843c749SSergey Zigachev 	id->gds_size = 0;
532b843c749SSergey Zigachev 	id->gws_base = 0;
533b843c749SSergey Zigachev 	id->gws_size = 0;
534b843c749SSergey Zigachev 	id->oa_base = 0;
535b843c749SSergey Zigachev 	id->oa_size = 0;
536b843c749SSergey Zigachev 	mutex_unlock(&id_mgr->lock);
537b843c749SSergey Zigachev }
538b843c749SSergey Zigachev 
539b843c749SSergey Zigachev /**
540b843c749SSergey Zigachev  * amdgpu_vmid_reset_all - reset VMID to zero
541b843c749SSergey Zigachev  *
542b843c749SSergey Zigachev  * @adev: amdgpu device structure
543b843c749SSergey Zigachev  *
544b843c749SSergey Zigachev  * Reset VMID to force flush on next use
545b843c749SSergey Zigachev  */
amdgpu_vmid_reset_all(struct amdgpu_device * adev)546b843c749SSergey Zigachev void amdgpu_vmid_reset_all(struct amdgpu_device *adev)
547b843c749SSergey Zigachev {
548b843c749SSergey Zigachev 	unsigned i, j;
549b843c749SSergey Zigachev 
550b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
551b843c749SSergey Zigachev 		struct amdgpu_vmid_mgr *id_mgr =
552b843c749SSergey Zigachev 			&adev->vm_manager.id_mgr[i];
553b843c749SSergey Zigachev 
554b843c749SSergey Zigachev 		for (j = 1; j < id_mgr->num_ids; ++j)
555b843c749SSergey Zigachev 			amdgpu_vmid_reset(adev, i, j);
556b843c749SSergey Zigachev 	}
557b843c749SSergey Zigachev }
558b843c749SSergey Zigachev 
559b843c749SSergey Zigachev /**
560b843c749SSergey Zigachev  * amdgpu_vmid_mgr_init - init the VMID manager
561b843c749SSergey Zigachev  *
562b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
563b843c749SSergey Zigachev  *
564b843c749SSergey Zigachev  * Initialize the VM manager structures
565b843c749SSergey Zigachev  */
amdgpu_vmid_mgr_init(struct amdgpu_device * adev)566b843c749SSergey Zigachev void amdgpu_vmid_mgr_init(struct amdgpu_device *adev)
567b843c749SSergey Zigachev {
568b843c749SSergey Zigachev 	unsigned i, j;
569b843c749SSergey Zigachev 
570b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
571b843c749SSergey Zigachev 		struct amdgpu_vmid_mgr *id_mgr =
572b843c749SSergey Zigachev 			&adev->vm_manager.id_mgr[i];
573b843c749SSergey Zigachev 
574*78973132SSergey Zigachev 		lockinit(&id_mgr->lock, "agdimgrl", 0, LK_CANRECURSE);
575b843c749SSergey Zigachev 		INIT_LIST_HEAD(&id_mgr->ids_lru);
576b843c749SSergey Zigachev 		atomic_set(&id_mgr->reserved_vmid_num, 0);
577b843c749SSergey Zigachev 
578b843c749SSergey Zigachev 		/* skip over VMID 0, since it is the system VM */
579b843c749SSergey Zigachev 		for (j = 1; j < id_mgr->num_ids; ++j) {
580b843c749SSergey Zigachev 			amdgpu_vmid_reset(adev, i, j);
581b843c749SSergey Zigachev 			amdgpu_sync_create(&id_mgr->ids[j].active);
582b843c749SSergey Zigachev 			list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
583b843c749SSergey Zigachev 		}
584b843c749SSergey Zigachev 	}
585b843c749SSergey Zigachev }
586b843c749SSergey Zigachev 
587b843c749SSergey Zigachev /**
588b843c749SSergey Zigachev  * amdgpu_vmid_mgr_fini - cleanup VM manager
589b843c749SSergey Zigachev  *
590b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
591b843c749SSergey Zigachev  *
592b843c749SSergey Zigachev  * Cleanup the VM manager and free resources.
593b843c749SSergey Zigachev  */
amdgpu_vmid_mgr_fini(struct amdgpu_device * adev)594b843c749SSergey Zigachev void amdgpu_vmid_mgr_fini(struct amdgpu_device *adev)
595b843c749SSergey Zigachev {
596b843c749SSergey Zigachev 	unsigned i, j;
597b843c749SSergey Zigachev 
598b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
599b843c749SSergey Zigachev 		struct amdgpu_vmid_mgr *id_mgr =
600b843c749SSergey Zigachev 			&adev->vm_manager.id_mgr[i];
601b843c749SSergey Zigachev 
602b843c749SSergey Zigachev 		mutex_destroy(&id_mgr->lock);
603b843c749SSergey Zigachev 		for (j = 0; j < AMDGPU_NUM_VMID; ++j) {
604b843c749SSergey Zigachev 			struct amdgpu_vmid *id = &id_mgr->ids[j];
605b843c749SSergey Zigachev 
606b843c749SSergey Zigachev 			amdgpu_sync_free(&id->active);
607b843c749SSergey Zigachev 			dma_fence_put(id->flushed_updates);
608b843c749SSergey Zigachev 			dma_fence_put(id->last_flush);
609b843c749SSergey Zigachev 			dma_fence_put(id->pasid_mapping);
610b843c749SSergey Zigachev 		}
611b843c749SSergey Zigachev 	}
612b843c749SSergey Zigachev }
613