1 /*
2  * Copyright 2022 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  */
23 #include <linux/slab.h>
24 #include <drm/drm_print.h>
25 
26 #include "amdgpu_ring_mux.h"
27 #include "amdgpu_ring.h"
28 #include "amdgpu.h"
29 
30 #define AMDGPU_MUX_RESUBMIT_JIFFIES_TIMEOUT (HZ / 2)
31 #define AMDGPU_MAX_LAST_UNSIGNALED_THRESHOLD_US 10000
32 
33 static const struct ring_info {
34 	unsigned int hw_pio;
35 	const char *ring_name;
36 } sw_ring_info[] = {
37 	{ AMDGPU_RING_PRIO_DEFAULT, "gfx_low"},
38 	{ AMDGPU_RING_PRIO_2, "gfx_high"},
39 };
40 
41 static struct pool amdgpu_mux_chunk_slab;
42 
amdgpu_ring_mux_sw_entry(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)43 static inline struct amdgpu_mux_entry *amdgpu_ring_mux_sw_entry(struct amdgpu_ring_mux *mux,
44 								struct amdgpu_ring *ring)
45 {
46 	return ring->entry_index < mux->ring_entry_size ?
47 			&mux->ring_entry[ring->entry_index] : NULL;
48 }
49 
50 /* copy packages on sw ring range[begin, end) */
amdgpu_ring_mux_copy_pkt_from_sw_ring(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring,u64 s_start,u64 s_end)51 static void amdgpu_ring_mux_copy_pkt_from_sw_ring(struct amdgpu_ring_mux *mux,
52 						  struct amdgpu_ring *ring,
53 						  u64 s_start, u64 s_end)
54 {
55 	u64 start, end;
56 	struct amdgpu_ring *real_ring = mux->real_ring;
57 
58 	start = s_start & ring->buf_mask;
59 	end = s_end & ring->buf_mask;
60 
61 	if (start == end) {
62 		DRM_ERROR("no more data copied from sw ring\n");
63 		return;
64 	}
65 	if (start > end) {
66 		amdgpu_ring_alloc(real_ring, (ring->ring_size >> 2) + end - start);
67 		amdgpu_ring_write_multiple(real_ring, (void *)&ring->ring[start],
68 					   (ring->ring_size >> 2) - start);
69 		amdgpu_ring_write_multiple(real_ring, (void *)&ring->ring[0], end);
70 	} else {
71 		amdgpu_ring_alloc(real_ring, end - start);
72 		amdgpu_ring_write_multiple(real_ring, (void *)&ring->ring[start], end - start);
73 	}
74 }
75 
amdgpu_mux_resubmit_chunks(struct amdgpu_ring_mux * mux)76 static void amdgpu_mux_resubmit_chunks(struct amdgpu_ring_mux *mux)
77 {
78 	struct amdgpu_mux_entry *e = NULL;
79 	struct amdgpu_mux_chunk *chunk;
80 	uint32_t seq, last_seq;
81 	int i;
82 
83 	/*find low priority entries:*/
84 	if (!mux->s_resubmit)
85 		return;
86 
87 	for (i = 0; i < mux->num_ring_entries; i++) {
88 		if (mux->ring_entry[i].ring->hw_prio <= AMDGPU_RING_PRIO_DEFAULT) {
89 			e = &mux->ring_entry[i];
90 			break;
91 		}
92 	}
93 
94 	if (!e) {
95 		DRM_ERROR("%s no low priority ring found\n", __func__);
96 		return;
97 	}
98 
99 	last_seq = atomic_read(&e->ring->fence_drv.last_seq);
100 	seq = mux->seqno_to_resubmit;
101 	if (last_seq < seq) {
102 		/*resubmit all the fences between (last_seq, seq]*/
103 		list_for_each_entry(chunk, &e->list, entry) {
104 			if (chunk->sync_seq > last_seq && chunk->sync_seq <= seq) {
105 				amdgpu_fence_update_start_timestamp(e->ring,
106 								    chunk->sync_seq,
107 								    ktime_get());
108 				if (chunk->sync_seq ==
109 					le32_to_cpu(*(e->ring->fence_drv.cpu_addr + 2))) {
110 					if (chunk->cntl_offset <= e->ring->buf_mask)
111 						amdgpu_ring_patch_cntl(e->ring,
112 								       chunk->cntl_offset);
113 					if (chunk->ce_offset <= e->ring->buf_mask)
114 						amdgpu_ring_patch_ce(e->ring, chunk->ce_offset);
115 					if (chunk->de_offset <= e->ring->buf_mask)
116 						amdgpu_ring_patch_de(e->ring, chunk->de_offset);
117 				}
118 				amdgpu_ring_mux_copy_pkt_from_sw_ring(mux, e->ring,
119 								      chunk->start,
120 								      chunk->end);
121 				mux->wptr_resubmit = chunk->end;
122 				amdgpu_ring_commit(mux->real_ring);
123 			}
124 		}
125 	}
126 
127 	del_timer(&mux->resubmit_timer);
128 	mux->s_resubmit = false;
129 }
130 
amdgpu_ring_mux_schedule_resubmit(struct amdgpu_ring_mux * mux)131 static void amdgpu_ring_mux_schedule_resubmit(struct amdgpu_ring_mux *mux)
132 {
133 	mod_timer(&mux->resubmit_timer, jiffies + AMDGPU_MUX_RESUBMIT_JIFFIES_TIMEOUT);
134 }
135 
amdgpu_mux_resubmit_fallback(void * arg)136 static void amdgpu_mux_resubmit_fallback(void *arg)
137 {
138 	struct amdgpu_ring_mux *mux = arg;
139 
140 	if (!spin_trylock(&mux->lock)) {
141 		amdgpu_ring_mux_schedule_resubmit(mux);
142 		DRM_ERROR("reschedule resubmit\n");
143 		return;
144 	}
145 	amdgpu_mux_resubmit_chunks(mux);
146 	spin_unlock(&mux->lock);
147 }
148 
amdgpu_ring_mux_init(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring,unsigned int entry_size)149 int amdgpu_ring_mux_init(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring,
150 			 unsigned int entry_size)
151 {
152 	mux->real_ring = ring;
153 	mux->num_ring_entries = 0;
154 
155 	mux->ring_entry = kcalloc(entry_size, sizeof(struct amdgpu_mux_entry), GFP_KERNEL);
156 	if (!mux->ring_entry)
157 		return -ENOMEM;
158 
159 	mux->ring_entry_size = entry_size;
160 	mux->s_resubmit = false;
161 
162 #ifdef __linux__
163 	amdgpu_mux_chunk_slab = KMEM_CACHE(amdgpu_mux_chunk, SLAB_HWCACHE_ALIGN);
164 	if (!amdgpu_mux_chunk_slab) {
165 		DRM_ERROR("create amdgpu_mux_chunk cache failed\n");
166 		return -ENOMEM;
167 	}
168 #else
169 	pool_init(&amdgpu_mux_chunk_slab, sizeof(struct amdgpu_mux_chunk),
170 	    CACHELINESIZE, IPL_TTY, 0, "amdgpu_mux_chunk", NULL);
171 #endif
172 
173 	mtx_init(&mux->lock, IPL_NONE);
174 #ifdef __linux__
175 	timer_setup(&mux->resubmit_timer, amdgpu_mux_resubmit_fallback, 0);
176 #else
177 	timeout_set(&mux->resubmit_timer, amdgpu_mux_resubmit_fallback, mux);
178 #endif
179 
180 	return 0;
181 }
182 
amdgpu_ring_mux_fini(struct amdgpu_ring_mux * mux)183 void amdgpu_ring_mux_fini(struct amdgpu_ring_mux *mux)
184 {
185 	struct amdgpu_mux_entry *e;
186 	struct amdgpu_mux_chunk *chunk, *chunk2;
187 	int i;
188 
189 	for (i = 0; i < mux->num_ring_entries; i++) {
190 		e = &mux->ring_entry[i];
191 		list_for_each_entry_safe(chunk, chunk2, &e->list, entry) {
192 			list_del(&chunk->entry);
193 #ifdef __linux__
194 			kmem_cache_free(amdgpu_mux_chunk_slab, chunk);
195 #else
196 			pool_put(&amdgpu_mux_chunk_slab, chunk);
197 #endif
198 		}
199 	}
200 #ifdef __linux__
201 	kmem_cache_destroy(amdgpu_mux_chunk_slab);
202 #else
203 	pool_destroy(&amdgpu_mux_chunk_slab);
204 #endif
205 	kfree(mux->ring_entry);
206 	mux->ring_entry = NULL;
207 	mux->num_ring_entries = 0;
208 	mux->ring_entry_size = 0;
209 }
210 
amdgpu_ring_mux_add_sw_ring(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)211 int amdgpu_ring_mux_add_sw_ring(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring)
212 {
213 	struct amdgpu_mux_entry *e;
214 
215 	if (mux->num_ring_entries >= mux->ring_entry_size) {
216 		DRM_ERROR("add sw ring exceeding max entry size\n");
217 		return -ENOENT;
218 	}
219 
220 	e = &mux->ring_entry[mux->num_ring_entries];
221 	ring->entry_index = mux->num_ring_entries;
222 	e->ring = ring;
223 
224 	INIT_LIST_HEAD(&e->list);
225 	mux->num_ring_entries += 1;
226 	return 0;
227 }
228 
amdgpu_ring_mux_set_wptr(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring,u64 wptr)229 void amdgpu_ring_mux_set_wptr(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring, u64 wptr)
230 {
231 	struct amdgpu_mux_entry *e;
232 
233 	spin_lock(&mux->lock);
234 
235 	if (ring->hw_prio <= AMDGPU_RING_PRIO_DEFAULT)
236 		amdgpu_mux_resubmit_chunks(mux);
237 
238 	e = amdgpu_ring_mux_sw_entry(mux, ring);
239 	if (!e) {
240 		DRM_ERROR("cannot find entry for sw ring\n");
241 		spin_unlock(&mux->lock);
242 		return;
243 	}
244 
245 	/* We could skip this set wptr as preemption in process. */
246 	if (ring->hw_prio <= AMDGPU_RING_PRIO_DEFAULT && mux->pending_trailing_fence_signaled) {
247 		spin_unlock(&mux->lock);
248 		return;
249 	}
250 
251 	e->sw_cptr = e->sw_wptr;
252 	/* Update cptr if the package already copied in resubmit functions */
253 	if (ring->hw_prio <= AMDGPU_RING_PRIO_DEFAULT && e->sw_cptr < mux->wptr_resubmit)
254 		e->sw_cptr = mux->wptr_resubmit;
255 	e->sw_wptr = wptr;
256 	e->start_ptr_in_hw_ring = mux->real_ring->wptr;
257 
258 	/* Skip copying for the packages already resubmitted.*/
259 	if (ring->hw_prio > AMDGPU_RING_PRIO_DEFAULT || mux->wptr_resubmit < wptr) {
260 		amdgpu_ring_mux_copy_pkt_from_sw_ring(mux, ring, e->sw_cptr, wptr);
261 		e->end_ptr_in_hw_ring = mux->real_ring->wptr;
262 		amdgpu_ring_commit(mux->real_ring);
263 	} else {
264 		e->end_ptr_in_hw_ring = mux->real_ring->wptr;
265 	}
266 	spin_unlock(&mux->lock);
267 }
268 
amdgpu_ring_mux_get_wptr(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)269 u64 amdgpu_ring_mux_get_wptr(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring)
270 {
271 	struct amdgpu_mux_entry *e;
272 
273 	e = amdgpu_ring_mux_sw_entry(mux, ring);
274 	if (!e) {
275 		DRM_ERROR("cannot find entry for sw ring\n");
276 		return 0;
277 	}
278 
279 	return e->sw_wptr;
280 }
281 
282 /**
283  * amdgpu_ring_mux_get_rptr - get the readptr of the software ring
284  * @mux: the multiplexer the software rings attach to
285  * @ring: the software ring of which we calculate the readptr
286  *
287  * The return value of the readptr is not precise while the other rings could
288  * write data onto the real ring buffer.After overwriting on the real ring, we
289  * can not decide if our packages have been excuted or not read yet. However,
290  * this function is only called by the tools such as umr to collect the latest
291  * packages for the hang analysis. We assume the hang happens near our latest
292  * submit. Thus we could use the following logic to give the clue:
293  * If the readptr is between start and end, then we return the copy pointer
294  * plus the distance from start to readptr. If the readptr is before start, we
295  * return the copy pointer. Lastly, if the readptr is past end, we return the
296  * write pointer.
297  */
amdgpu_ring_mux_get_rptr(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)298 u64 amdgpu_ring_mux_get_rptr(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring)
299 {
300 	struct amdgpu_mux_entry *e;
301 	u64 readp, offset, start, end;
302 
303 	e = amdgpu_ring_mux_sw_entry(mux, ring);
304 	if (!e) {
305 		DRM_ERROR("no sw entry found!\n");
306 		return 0;
307 	}
308 
309 	readp = amdgpu_ring_get_rptr(mux->real_ring);
310 
311 	start = e->start_ptr_in_hw_ring & mux->real_ring->buf_mask;
312 	end = e->end_ptr_in_hw_ring & mux->real_ring->buf_mask;
313 	if (start > end) {
314 		if (readp <= end)
315 			readp += mux->real_ring->ring_size >> 2;
316 		end += mux->real_ring->ring_size >> 2;
317 	}
318 
319 	if (start <= readp && readp <= end) {
320 		offset = readp - start;
321 		e->sw_rptr = (e->sw_cptr + offset) & ring->buf_mask;
322 	} else if (readp < start) {
323 		e->sw_rptr = e->sw_cptr;
324 	} else {
325 		/* end < readptr */
326 		e->sw_rptr = e->sw_wptr;
327 	}
328 
329 	return e->sw_rptr;
330 }
331 
amdgpu_sw_ring_get_rptr_gfx(struct amdgpu_ring * ring)332 u64 amdgpu_sw_ring_get_rptr_gfx(struct amdgpu_ring *ring)
333 {
334 	struct amdgpu_device *adev = ring->adev;
335 	struct amdgpu_ring_mux *mux = &adev->gfx.muxer;
336 
337 	WARN_ON(!ring->is_sw_ring);
338 	return amdgpu_ring_mux_get_rptr(mux, ring);
339 }
340 
amdgpu_sw_ring_get_wptr_gfx(struct amdgpu_ring * ring)341 u64 amdgpu_sw_ring_get_wptr_gfx(struct amdgpu_ring *ring)
342 {
343 	struct amdgpu_device *adev = ring->adev;
344 	struct amdgpu_ring_mux *mux = &adev->gfx.muxer;
345 
346 	WARN_ON(!ring->is_sw_ring);
347 	return amdgpu_ring_mux_get_wptr(mux, ring);
348 }
349 
amdgpu_sw_ring_set_wptr_gfx(struct amdgpu_ring * ring)350 void amdgpu_sw_ring_set_wptr_gfx(struct amdgpu_ring *ring)
351 {
352 	struct amdgpu_device *adev = ring->adev;
353 	struct amdgpu_ring_mux *mux = &adev->gfx.muxer;
354 
355 	WARN_ON(!ring->is_sw_ring);
356 	amdgpu_ring_mux_set_wptr(mux, ring, ring->wptr);
357 }
358 
359 /* Override insert_nop to prevent emitting nops to the software rings */
amdgpu_sw_ring_insert_nop(struct amdgpu_ring * ring,uint32_t count)360 void amdgpu_sw_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
361 {
362 	WARN_ON(!ring->is_sw_ring);
363 }
364 
amdgpu_sw_ring_name(int idx)365 const char *amdgpu_sw_ring_name(int idx)
366 {
367 	return idx < ARRAY_SIZE(sw_ring_info) ?
368 		sw_ring_info[idx].ring_name : NULL;
369 }
370 
amdgpu_sw_ring_priority(int idx)371 unsigned int amdgpu_sw_ring_priority(int idx)
372 {
373 	return idx < ARRAY_SIZE(sw_ring_info) ?
374 		sw_ring_info[idx].hw_pio : AMDGPU_RING_PRIO_DEFAULT;
375 }
376 
377 /*Scan on low prio rings to have unsignaled fence and high ring has no fence.*/
amdgpu_mcbp_scan(struct amdgpu_ring_mux * mux)378 static int amdgpu_mcbp_scan(struct amdgpu_ring_mux *mux)
379 {
380 	struct amdgpu_ring *ring;
381 	int i, need_preempt;
382 
383 	need_preempt = 0;
384 	for (i = 0; i < mux->num_ring_entries; i++) {
385 		ring = mux->ring_entry[i].ring;
386 		if (ring->hw_prio > AMDGPU_RING_PRIO_DEFAULT &&
387 		    amdgpu_fence_count_emitted(ring) > 0)
388 			return 0;
389 		if (ring->hw_prio <= AMDGPU_RING_PRIO_DEFAULT &&
390 		    amdgpu_fence_last_unsignaled_time_us(ring) >
391 		    AMDGPU_MAX_LAST_UNSIGNALED_THRESHOLD_US)
392 			need_preempt = 1;
393 	}
394 	return need_preempt && !mux->s_resubmit;
395 }
396 
397 /* Trigger Mid-Command Buffer Preemption (MCBP) and find if we need to resubmit. */
amdgpu_mcbp_trigger_preempt(struct amdgpu_ring_mux * mux)398 static int amdgpu_mcbp_trigger_preempt(struct amdgpu_ring_mux *mux)
399 {
400 	int r;
401 
402 	spin_lock(&mux->lock);
403 	mux->pending_trailing_fence_signaled = true;
404 	r = amdgpu_ring_preempt_ib(mux->real_ring);
405 	spin_unlock(&mux->lock);
406 	return r;
407 }
408 
amdgpu_sw_ring_ib_begin(struct amdgpu_ring * ring)409 void amdgpu_sw_ring_ib_begin(struct amdgpu_ring *ring)
410 {
411 	struct amdgpu_device *adev = ring->adev;
412 	struct amdgpu_ring_mux *mux = &adev->gfx.muxer;
413 
414 	WARN_ON(!ring->is_sw_ring);
415 	if (adev->gfx.mcbp && ring->hw_prio > AMDGPU_RING_PRIO_DEFAULT) {
416 		if (amdgpu_mcbp_scan(mux) > 0)
417 			amdgpu_mcbp_trigger_preempt(mux);
418 		return;
419 	}
420 
421 	amdgpu_ring_mux_start_ib(mux, ring);
422 }
423 
amdgpu_sw_ring_ib_end(struct amdgpu_ring * ring)424 void amdgpu_sw_ring_ib_end(struct amdgpu_ring *ring)
425 {
426 	struct amdgpu_device *adev = ring->adev;
427 	struct amdgpu_ring_mux *mux = &adev->gfx.muxer;
428 
429 	WARN_ON(!ring->is_sw_ring);
430 	if (adev->gfx.mcbp && ring->hw_prio > AMDGPU_RING_PRIO_DEFAULT)
431 		return;
432 	amdgpu_ring_mux_end_ib(mux, ring);
433 }
434 
amdgpu_sw_ring_ib_mark_offset(struct amdgpu_ring * ring,enum amdgpu_ring_mux_offset_type type)435 void amdgpu_sw_ring_ib_mark_offset(struct amdgpu_ring *ring, enum amdgpu_ring_mux_offset_type type)
436 {
437 	struct amdgpu_device *adev = ring->adev;
438 	struct amdgpu_ring_mux *mux = &adev->gfx.muxer;
439 	unsigned offset;
440 
441 	if (ring->hw_prio > AMDGPU_RING_PRIO_DEFAULT)
442 		return;
443 
444 	offset = ring->wptr & ring->buf_mask;
445 
446 	amdgpu_ring_mux_ib_mark_offset(mux, ring, offset, type);
447 }
448 
amdgpu_ring_mux_start_ib(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)449 void amdgpu_ring_mux_start_ib(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring)
450 {
451 	struct amdgpu_mux_entry *e;
452 	struct amdgpu_mux_chunk *chunk;
453 
454 	spin_lock(&mux->lock);
455 	amdgpu_mux_resubmit_chunks(mux);
456 	spin_unlock(&mux->lock);
457 
458 	e = amdgpu_ring_mux_sw_entry(mux, ring);
459 	if (!e) {
460 		DRM_ERROR("cannot find entry!\n");
461 		return;
462 	}
463 
464 #ifdef __linux__
465 	chunk = kmem_cache_alloc(amdgpu_mux_chunk_slab, GFP_KERNEL);
466 #else
467 	chunk = pool_get(&amdgpu_mux_chunk_slab, PR_WAITOK);
468 #endif
469 	if (!chunk) {
470 		DRM_ERROR("alloc amdgpu_mux_chunk_slab failed\n");
471 		return;
472 	}
473 
474 	chunk->start = ring->wptr;
475 	/* the initialized value used to check if they are set by the ib submission*/
476 	chunk->cntl_offset = ring->buf_mask + 1;
477 	chunk->de_offset = ring->buf_mask + 1;
478 	chunk->ce_offset = ring->buf_mask + 1;
479 	list_add_tail(&chunk->entry, &e->list);
480 }
481 
scan_and_remove_signaled_chunk(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)482 static void scan_and_remove_signaled_chunk(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring)
483 {
484 	uint32_t last_seq = 0;
485 	struct amdgpu_mux_entry *e;
486 	struct amdgpu_mux_chunk *chunk, *tmp;
487 
488 	e = amdgpu_ring_mux_sw_entry(mux, ring);
489 	if (!e) {
490 		DRM_ERROR("cannot find entry!\n");
491 		return;
492 	}
493 
494 	last_seq = atomic_read(&ring->fence_drv.last_seq);
495 
496 	list_for_each_entry_safe(chunk, tmp, &e->list, entry) {
497 		if (chunk->sync_seq <= last_seq) {
498 			list_del(&chunk->entry);
499 #ifdef __linux__
500 			kmem_cache_free(amdgpu_mux_chunk_slab, chunk);
501 #else
502 			pool_put(&amdgpu_mux_chunk_slab, chunk);
503 #endif
504 		}
505 	}
506 }
507 
amdgpu_ring_mux_ib_mark_offset(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring,u64 offset,enum amdgpu_ring_mux_offset_type type)508 void amdgpu_ring_mux_ib_mark_offset(struct amdgpu_ring_mux *mux,
509 				    struct amdgpu_ring *ring, u64 offset,
510 				    enum amdgpu_ring_mux_offset_type type)
511 {
512 	struct amdgpu_mux_entry *e;
513 	struct amdgpu_mux_chunk *chunk;
514 
515 	e = amdgpu_ring_mux_sw_entry(mux, ring);
516 	if (!e) {
517 		DRM_ERROR("cannot find entry!\n");
518 		return;
519 	}
520 
521 	chunk = list_last_entry(&e->list, struct amdgpu_mux_chunk, entry);
522 	if (!chunk) {
523 		DRM_ERROR("cannot find chunk!\n");
524 		return;
525 	}
526 
527 	switch (type) {
528 	case AMDGPU_MUX_OFFSET_TYPE_CONTROL:
529 		chunk->cntl_offset = offset;
530 		break;
531 	case AMDGPU_MUX_OFFSET_TYPE_DE:
532 		chunk->de_offset = offset;
533 		break;
534 	case AMDGPU_MUX_OFFSET_TYPE_CE:
535 		chunk->ce_offset = offset;
536 		break;
537 	default:
538 		DRM_ERROR("invalid type (%d)\n", type);
539 		break;
540 	}
541 }
542 
amdgpu_ring_mux_end_ib(struct amdgpu_ring_mux * mux,struct amdgpu_ring * ring)543 void amdgpu_ring_mux_end_ib(struct amdgpu_ring_mux *mux, struct amdgpu_ring *ring)
544 {
545 	struct amdgpu_mux_entry *e;
546 	struct amdgpu_mux_chunk *chunk;
547 
548 	e = amdgpu_ring_mux_sw_entry(mux, ring);
549 	if (!e) {
550 		DRM_ERROR("cannot find entry!\n");
551 		return;
552 	}
553 
554 	chunk = list_last_entry(&e->list, struct amdgpu_mux_chunk, entry);
555 	if (!chunk) {
556 		DRM_ERROR("cannot find chunk!\n");
557 		return;
558 	}
559 
560 	chunk->end = ring->wptr;
561 	chunk->sync_seq = READ_ONCE(ring->fence_drv.sync_seq);
562 
563 	scan_and_remove_signaled_chunk(mux, ring);
564 }
565 
amdgpu_mcbp_handle_trailing_fence_irq(struct amdgpu_ring_mux * mux)566 bool amdgpu_mcbp_handle_trailing_fence_irq(struct amdgpu_ring_mux *mux)
567 {
568 	struct amdgpu_mux_entry *e;
569 	struct amdgpu_ring *ring = NULL;
570 	int i;
571 
572 	if (!mux->pending_trailing_fence_signaled)
573 		return false;
574 
575 	if (mux->real_ring->trail_seq != le32_to_cpu(*mux->real_ring->trail_fence_cpu_addr))
576 		return false;
577 
578 	for (i = 0; i < mux->num_ring_entries; i++) {
579 		e = &mux->ring_entry[i];
580 		if (e->ring->hw_prio <= AMDGPU_RING_PRIO_DEFAULT) {
581 			ring = e->ring;
582 			break;
583 		}
584 	}
585 
586 	if (!ring) {
587 		DRM_ERROR("cannot find low priority ring\n");
588 		return false;
589 	}
590 
591 	amdgpu_fence_process(ring);
592 	if (amdgpu_fence_count_emitted(ring) > 0) {
593 		mux->s_resubmit = true;
594 		mux->seqno_to_resubmit = ring->fence_drv.sync_seq;
595 		amdgpu_ring_mux_schedule_resubmit(mux);
596 	}
597 
598 	mux->pending_trailing_fence_signaled = false;
599 	return true;
600 }
601