xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_jpeg.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
1 /*
2  * Copyright 2019 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 #include "amdgpu.h"
28 #include "amdgpu_ip.h"
29 #include "amdgpu_jpeg.h"
30 #include "amdgpu_pm.h"
31 #include "soc15d.h"
32 #include "soc15_common.h"
33 
34 #define JPEG_IDLE_TIMEOUT	msecs_to_jiffies(1000)
35 
36 static void amdgpu_jpeg_idle_work_handler(struct work_struct *work);
37 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev);
38 
39 int amdgpu_jpeg_sw_init(struct amdgpu_device *adev)
40 {
41 	int i, r;
42 
43 	INIT_DELAYED_WORK(&adev->jpeg.idle_work, amdgpu_jpeg_idle_work_handler);
44 	mutex_init(&adev->jpeg.jpeg_pg_lock);
45 	atomic_set(&adev->jpeg.total_submission_cnt, 0);
46 
47 	if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
48 	    (adev->pg_flags & AMD_PG_SUPPORT_JPEG_DPG))
49 		adev->jpeg.indirect_sram = true;
50 
51 	for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
52 		if (adev->jpeg.harvest_config & (1U << i))
53 			continue;
54 
55 		if (adev->jpeg.indirect_sram) {
56 			r = amdgpu_bo_create_kernel(adev, 64 * 2 * 4, PAGE_SIZE,
57 					AMDGPU_GEM_DOMAIN_VRAM |
58 					AMDGPU_GEM_DOMAIN_GTT,
59 					&adev->jpeg.inst[i].dpg_sram_bo,
60 					&adev->jpeg.inst[i].dpg_sram_gpu_addr,
61 					&adev->jpeg.inst[i].dpg_sram_cpu_addr);
62 			if (r) {
63 				dev_err(adev->dev,
64 				"JPEG %d (%d) failed to allocate DPG bo\n", i, r);
65 				return r;
66 			}
67 		}
68 	}
69 
70 	return 0;
71 }
72 
73 int amdgpu_jpeg_sw_fini(struct amdgpu_device *adev)
74 {
75 	int i, j;
76 
77 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
78 		if (adev->jpeg.harvest_config & (1U << i))
79 			continue;
80 
81 		amdgpu_bo_free_kernel(
82 			&adev->jpeg.inst[i].dpg_sram_bo,
83 			&adev->jpeg.inst[i].dpg_sram_gpu_addr,
84 			(void **)&adev->jpeg.inst[i].dpg_sram_cpu_addr);
85 
86 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j)
87 			amdgpu_ring_fini(&adev->jpeg.inst[i].ring_dec[j]);
88 	}
89 
90 	if (adev->jpeg.reg_list)
91 		amdgpu_jpeg_reg_dump_fini(adev);
92 
93 	mutex_destroy(&adev->jpeg.jpeg_pg_lock);
94 
95 	return 0;
96 }
97 
98 int amdgpu_jpeg_early_fini(struct amdgpu_ip_block *ip_block)
99 {
100 	/* Called on teardown even if sw_init never ran */
101 	if (!ip_block->status.sw)
102 		return 0;
103 
104 	/* hw_fini may not wait while a peer in the reset domain resets */
105 	cancel_delayed_work_sync(&ip_block->adev->jpeg.idle_work);
106 
107 	return 0;
108 }
109 
110 void amdgpu_jpeg_cancel_idle_work(struct amdgpu_device *adev)
111 {
112 	/* The reset mustn't wait for the idle work, which waits for the reset */
113 	if (amdgpu_in_reset(adev))
114 		cancel_delayed_work(&adev->jpeg.idle_work);
115 	else
116 		cancel_delayed_work_sync(&adev->jpeg.idle_work);
117 }
118 
119 int amdgpu_jpeg_resume(struct amdgpu_device *adev)
120 {
121 	return 0;
122 }
123 
124 static void amdgpu_jpeg_idle_work_handler(struct work_struct *work)
125 {
126 	struct amdgpu_device *adev =
127 		container_of(work, struct amdgpu_device, jpeg.idle_work.work);
128 	unsigned int fences = 0;
129 	unsigned int i, j;
130 
131 	/* Nothing holding the reset semaphore waits for this work */
132 	down_read(&adev->reset_domain->sem);
133 
134 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
135 		if (adev->jpeg.harvest_config & (1U << i))
136 			continue;
137 
138 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j)
139 			fences += amdgpu_fence_count_emitted(&adev->jpeg.inst[i].ring_dec[j]);
140 	}
141 
142 	if (!fences && !atomic_read(&adev->jpeg.total_submission_cnt)) {
143 		mutex_lock(&adev->jpeg.jpeg_pg_lock);
144 		amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
145 						       AMD_PG_STATE_GATE);
146 		mutex_unlock(&adev->jpeg.jpeg_pg_lock);
147 	} else
148 		schedule_delayed_work(&adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT);
149 
150 	up_read(&adev->reset_domain->sem);
151 }
152 
153 void amdgpu_jpeg_ring_begin_use(struct amdgpu_ring *ring)
154 {
155 	struct amdgpu_device *adev = ring->adev;
156 
157 	if (!atomic_fetch_inc(&adev->jpeg.total_submission_cnt))
158 		amdgpu_jpeg_cancel_idle_work(adev);
159 
160 	mutex_lock(&adev->jpeg.jpeg_pg_lock);
161 	amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
162 						       AMD_PG_STATE_UNGATE);
163 	mutex_unlock(&adev->jpeg.jpeg_pg_lock);
164 }
165 
166 void amdgpu_jpeg_ring_end_use(struct amdgpu_ring *ring)
167 {
168 	if (atomic_dec_and_test(&ring->adev->jpeg.total_submission_cnt))
169 		schedule_delayed_work(&ring->adev->jpeg.idle_work,
170 				      JPEG_IDLE_TIMEOUT);
171 }
172 
173 int amdgpu_jpeg_dec_ring_test_ring(struct amdgpu_ring *ring)
174 {
175 	struct amdgpu_device *adev = ring->adev;
176 	uint32_t tmp = 0;
177 	unsigned i;
178 	int r;
179 
180 	/* JPEG in SRIOV does not support direct register read/write */
181 	if (amdgpu_sriov_vf(adev))
182 		return 0;
183 
184 	r = amdgpu_ring_alloc(ring, 3);
185 	if (r)
186 		return r;
187 
188 	WREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe], 0xCAFEDEAD);
189 	/* Add a read register to make sure the write register is executed. */
190 	RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]);
191 
192 	amdgpu_ring_write(ring, PACKET0(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0));
193 	amdgpu_ring_write(ring, 0xABADCAFE);
194 	amdgpu_ring_commit(ring);
195 
196 	for (i = 0; i < adev->usec_timeout; i++) {
197 		tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]);
198 		if (tmp == 0xABADCAFE)
199 			break;
200 		udelay(1);
201 	}
202 
203 	if (i >= adev->usec_timeout)
204 		r = -ETIMEDOUT;
205 
206 	return r;
207 }
208 
209 static int amdgpu_jpeg_dec_set_reg(struct amdgpu_ring *ring, uint32_t handle,
210 		struct dma_fence **fence)
211 {
212 	struct amdgpu_device *adev = ring->adev;
213 	struct amdgpu_job *job;
214 	struct amdgpu_ib *ib;
215 	struct dma_fence *f = NULL;
216 	const unsigned ib_size_dw = 16;
217 	int i, r;
218 
219 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4,
220 				     AMDGPU_IB_POOL_DIRECT,
221 				     AMDGPU_KERNEL_JOB_ID_VCN_RING_TEST,
222 				     &job);
223 	if (r)
224 		return r;
225 
226 	ib = &job->ibs[0];
227 
228 	ib->ptr[0] = PACKETJ(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0, 0, PACKETJ_TYPE0);
229 	ib->ptr[1] = 0xDEADBEEF;
230 	for (i = 2; i < 16; i += 2) {
231 		ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6);
232 		ib->ptr[i+1] = 0;
233 	}
234 	ib->length_dw = 16;
235 
236 	r = amdgpu_job_submit_direct(job, ring, &f);
237 	if (r)
238 		goto err;
239 
240 	if (fence)
241 		*fence = dma_fence_get(f);
242 	dma_fence_put(f);
243 
244 	return 0;
245 
246 err:
247 	amdgpu_job_free(job);
248 	return r;
249 }
250 
251 int amdgpu_jpeg_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
252 {
253 	struct amdgpu_device *adev = ring->adev;
254 	uint32_t tmp = 0;
255 	unsigned i;
256 	struct dma_fence *fence = NULL;
257 	long r = 0;
258 
259 	r = amdgpu_jpeg_dec_set_reg(ring, 1, &fence);
260 	if (r)
261 		goto error;
262 
263 	r = dma_fence_wait_timeout(fence, false, timeout);
264 	if (r == 0) {
265 		r = -ETIMEDOUT;
266 		goto error;
267 	} else if (r < 0) {
268 		goto error;
269 	} else {
270 		r = 0;
271 	}
272 
273 	if (!amdgpu_sriov_vf(adev)) {
274 		for (i = 0; i < adev->usec_timeout; i++) {
275 			tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]);
276 			if (tmp == 0xDEADBEEF)
277 				break;
278 			udelay(1);
279 			if (amdgpu_emu_mode == 1)
280 				udelay(10);
281 		}
282 
283 		if (i >= adev->usec_timeout)
284 			r = -ETIMEDOUT;
285 	}
286 
287 	dma_fence_put(fence);
288 error:
289 	return r;
290 }
291 
292 int amdgpu_jpeg_process_poison_irq(struct amdgpu_device *adev,
293 				struct amdgpu_irq_src *source,
294 				struct amdgpu_iv_entry *entry)
295 {
296 	struct ras_common_if *ras_if = adev->jpeg.ras_if;
297 	struct ras_dispatch_if ih_data = {
298 		.entry = entry,
299 	};
300 
301 	if (!ras_if)
302 		return 0;
303 
304 	ih_data.head = *ras_if;
305 	amdgpu_ras_interrupt_dispatch(adev, &ih_data);
306 
307 	return 0;
308 }
309 
310 int amdgpu_jpeg_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
311 {
312 	int r, i;
313 
314 	r = amdgpu_ras_block_late_init(adev, ras_block);
315 	if (r)
316 		return r;
317 
318 	if (amdgpu_ras_is_supported(adev, ras_block->block)) {
319 		for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
320 			if (adev->jpeg.harvest_config & (1 << i) ||
321 			    !adev->jpeg.inst[i].ras_poison_irq.funcs)
322 				continue;
323 
324 			r = amdgpu_irq_get(adev, &adev->jpeg.inst[i].ras_poison_irq, 0);
325 			if (r)
326 				goto late_fini;
327 		}
328 	}
329 	return 0;
330 
331 late_fini:
332 	amdgpu_ras_block_late_fini(adev, ras_block);
333 	return r;
334 }
335 
336 int amdgpu_jpeg_ras_sw_init(struct amdgpu_device *adev)
337 {
338 	int err;
339 	struct amdgpu_jpeg_ras *ras;
340 
341 	if (!adev->jpeg.ras)
342 		return 0;
343 
344 	ras = adev->jpeg.ras;
345 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
346 	if (err) {
347 		dev_err(adev->dev, "Failed to register jpeg ras block!\n");
348 		return err;
349 	}
350 
351 	strcpy(ras->ras_block.ras_comm.name, "jpeg");
352 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__JPEG;
353 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__POISON;
354 	adev->jpeg.ras_if = &ras->ras_block.ras_comm;
355 
356 	if (!ras->ras_block.ras_late_init)
357 		ras->ras_block.ras_late_init = amdgpu_jpeg_ras_late_init;
358 
359 	return 0;
360 }
361 
362 int amdgpu_jpeg_psp_update_sram(struct amdgpu_device *adev, int inst_idx,
363 			       enum AMDGPU_UCODE_ID ucode_id)
364 {
365 	struct amdgpu_firmware_info ucode = {
366 		.ucode_id = AMDGPU_UCODE_ID_JPEG_RAM,
367 		.mc_addr = adev->jpeg.inst[inst_idx].dpg_sram_gpu_addr,
368 		.ucode_size = ((uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_curr_addr -
369 			      (uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_cpu_addr),
370 	};
371 
372 	return psp_execute_ip_fw_load(&adev->psp, &ucode);
373 }
374 
375 /*
376  * debugfs for to enable/disable jpeg job submission to specific core.
377  */
378 #if defined(CONFIG_DEBUG_FS)
379 static int amdgpu_debugfs_jpeg_sched_mask_set(void *data, u64 val)
380 {
381 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
382 	u32 i, j;
383 	u64 mask = 0;
384 	struct amdgpu_ring *ring;
385 
386 	if (!adev)
387 		return -ENODEV;
388 
389 	mask = (1ULL << (adev->jpeg.num_jpeg_inst * adev->jpeg.num_jpeg_rings)) - 1;
390 	if ((val & mask) == 0)
391 		return -EINVAL;
392 
393 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
394 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) {
395 			ring = &adev->jpeg.inst[i].ring_dec[j];
396 			if (val & (BIT_ULL((i * adev->jpeg.num_jpeg_rings) + j)))
397 				ring->sched.ready = true;
398 			else
399 				ring->sched.ready = false;
400 		}
401 	}
402 	/* publish sched.ready flag update effective immediately across smp */
403 	smp_rmb();
404 	return 0;
405 }
406 
407 static int amdgpu_debugfs_jpeg_sched_mask_get(void *data, u64 *val)
408 {
409 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
410 	u32 i, j;
411 	u64 mask = 0;
412 	struct amdgpu_ring *ring;
413 
414 	if (!adev)
415 		return -ENODEV;
416 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
417 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) {
418 			ring = &adev->jpeg.inst[i].ring_dec[j];
419 			if (ring->sched.ready)
420 				mask |= 1ULL << ((i * adev->jpeg.num_jpeg_rings) + j);
421 		}
422 	}
423 	*val = mask;
424 	return 0;
425 }
426 
427 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_jpeg_sched_mask_fops,
428 			 amdgpu_debugfs_jpeg_sched_mask_get,
429 			 amdgpu_debugfs_jpeg_sched_mask_set, "%llx\n");
430 
431 #endif
432 
433 void amdgpu_debugfs_jpeg_sched_mask_init(struct amdgpu_device *adev)
434 {
435 #if defined(CONFIG_DEBUG_FS)
436 	struct drm_minor *minor = adev_to_drm(adev)->primary;
437 	struct dentry *root = minor->debugfs_root;
438 	char name[32];
439 
440 	if (!(adev->jpeg.num_jpeg_inst > 1) && !(adev->jpeg.num_jpeg_rings > 1))
441 		return;
442 	sprintf(name, "amdgpu_jpeg_sched_mask");
443 	debugfs_create_file(name, 0600, root, adev,
444 			    &amdgpu_debugfs_jpeg_sched_mask_fops);
445 #endif
446 }
447 
448 static ssize_t amdgpu_get_jpeg_reset_mask(struct device *dev,
449 						struct device_attribute *attr,
450 						char *buf)
451 {
452 	struct drm_device *ddev = dev_get_drvdata(dev);
453 	struct amdgpu_device *adev = drm_to_adev(ddev);
454 
455 	if (!adev)
456 		return -ENODEV;
457 
458 	return amdgpu_show_reset_mask(buf, adev->jpeg.supported_reset);
459 }
460 
461 static DEVICE_ATTR(jpeg_reset_mask, 0444,
462 		   amdgpu_get_jpeg_reset_mask, NULL);
463 
464 int amdgpu_jpeg_sysfs_reset_mask_init(struct amdgpu_device *adev)
465 {
466 	int r = 0;
467 
468 	if (adev->jpeg.num_jpeg_inst) {
469 		r = device_create_file(adev->dev, &dev_attr_jpeg_reset_mask);
470 		if (r)
471 			return r;
472 	}
473 
474 	return r;
475 }
476 
477 void amdgpu_jpeg_sysfs_reset_mask_fini(struct amdgpu_device *adev)
478 {
479 	if (adev->dev->kobj.sd) {
480 		if (adev->jpeg.num_jpeg_inst)
481 			device_remove_file(adev->dev, &dev_attr_jpeg_reset_mask);
482 	}
483 }
484 
485 int amdgpu_jpeg_reg_dump_init(struct amdgpu_device *adev,
486 			       const struct amdgpu_hwip_reg_entry *reg, u32 count)
487 {
488 	adev->jpeg.ip_dump = kzalloc_objs(*adev->jpeg.ip_dump,
489 					  adev->jpeg.num_jpeg_inst * count);
490 	if (!adev->jpeg.ip_dump) {
491 		dev_err(adev->dev,
492 			"Failed to allocate memory for JPEG IP Dump\n");
493 		return -ENOMEM;
494 	}
495 	adev->jpeg.reg_list = reg;
496 	adev->jpeg.reg_count = count;
497 
498 	return 0;
499 }
500 
501 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev)
502 {
503 	kfree(adev->jpeg.ip_dump);
504 	adev->jpeg.reg_list = NULL;
505 	adev->jpeg.reg_count = 0;
506 }
507 
508 void amdgpu_jpeg_dump_ip_state(struct amdgpu_ip_block *ip_block)
509 {
510 	struct amdgpu_device *adev = ip_block->adev;
511 	u32 inst_off, inst_id, is_powered;
512 	int i, j;
513 
514 	if (!adev->jpeg.ip_dump)
515 		return;
516 
517 	for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
518 		if (adev->jpeg.harvest_config & (1 << i))
519 			continue;
520 
521 		inst_id = GET_INST(JPEG, i);
522 		inst_off = i * adev->jpeg.reg_count;
523 		/* check power status from UVD_JPEG_POWER_STATUS */
524 		adev->jpeg.ip_dump[inst_off] =
525 			RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[0],
526 							   inst_id));
527 		is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1);
528 
529 		if (is_powered)
530 			for (j = 1; j < adev->jpeg.reg_count; j++)
531 				adev->jpeg.ip_dump[inst_off + j] =
532 					RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[j],
533 									   inst_id));
534 	}
535 }
536 
537 void amdgpu_jpeg_print_ip_state(struct amdgpu_ip_block *ip_block, struct drm_printer *p)
538 {
539 	struct amdgpu_device *adev = ip_block->adev;
540 	u32 inst_off, is_powered;
541 	int i, j;
542 
543 	if (!adev->jpeg.ip_dump)
544 		return;
545 
546 	drm_printf(p, "num_instances:%d\n", adev->jpeg.num_jpeg_inst);
547 	for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
548 		if (adev->jpeg.harvest_config & (1 << i)) {
549 			drm_printf(p, "\nHarvested Instance:JPEG%d Skipping dump\n", i);
550 			continue;
551 		}
552 
553 		inst_off = i * adev->jpeg.reg_count;
554 		is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1);
555 
556 		if (is_powered) {
557 			drm_printf(p, "Active Instance:JPEG%d\n", i);
558 			for (j = 0; j < adev->jpeg.reg_count; j++)
559 				drm_printf(p, "%-50s \t 0x%08x\n", adev->jpeg.reg_list[j].reg_name,
560 					   adev->jpeg.ip_dump[inst_off + j]);
561 		} else
562 			drm_printf(p, "\nInactive Instance:JPEG%d\n", i);
563 	}
564 }
565 
566 static inline bool amdgpu_jpeg_reg_valid(u32 reg)
567 {
568 	if (reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END ||
569 	    (reg >= JPEG_ATOMIC_RANGE_START && reg <= JPEG_ATOMIC_RANGE_END))
570 		return false;
571 	else
572 		return true;
573 }
574 
575 /**
576  * amdgpu_jpeg_dec_parse_cs - command submission parser
577  *
578  * @parser: Command submission parser context
579  * @job: the job to parse
580  * @ib: the IB to parse
581  *
582  * Parse the command stream, return -EINVAL for invalid packet,
583  * 0 otherwise
584  */
585 
586 int amdgpu_jpeg_dec_parse_cs(struct amdgpu_cs_parser *parser,
587 			      struct amdgpu_job *job,
588 			      struct amdgpu_ib *ib)
589 {
590 	u32 i, reg, res, cond, type;
591 	struct amdgpu_device *adev = parser->adev;
592 
593 	for (i = 0; i < ib->length_dw ; i += 2) {
594 		reg  = CP_PACKETJ_GET_REG(ib->ptr[i]);
595 		res  = CP_PACKETJ_GET_RES(ib->ptr[i]);
596 		cond = CP_PACKETJ_GET_COND(ib->ptr[i]);
597 		type = CP_PACKETJ_GET_TYPE(ib->ptr[i]);
598 
599 		if (res) /* only support 0 at the moment */
600 			return -EINVAL;
601 
602 		switch (type) {
603 		case PACKETJ_TYPE0:
604 			if (cond != PACKETJ_CONDITION_CHECK0 ||
605 			    !amdgpu_jpeg_reg_valid(reg)) {
606 				dev_err(adev->dev, "Invalid packet [0x%08x]!\n", ib->ptr[i]);
607 				return -EINVAL;
608 			}
609 			break;
610 		case PACKETJ_TYPE3:
611 			if (cond != PACKETJ_CONDITION_CHECK3 ||
612 			    !amdgpu_jpeg_reg_valid(reg)) {
613 				dev_err(adev->dev, "Invalid packet [0x%08x]!\n", ib->ptr[i]);
614 				return -EINVAL;
615 			}
616 			break;
617 		case PACKETJ_TYPE6:
618 			if (ib->ptr[i] == CP_PACKETJ_NOP)
619 				continue;
620 			dev_err(adev->dev, "Invalid packet [0x%08x]!\n", ib->ptr[i]);
621 			return -EINVAL;
622 		default:
623 			dev_err(adev->dev, "Unknown packet type %d !\n", type);
624 			return -EINVAL;
625 		}
626 	}
627 
628 	return 0;
629 }
630 
631 /**
632  * amdgpu_jpeg_is_shared_inv_eng - Check if a ring is a JPEG ring that shares a VM invalidation
633  * engine
634  * @adev: Pointer to the AMDGPU device structure
635  * @ring: Pointer to the ring structure to check
636  *
637  * All decode rings within one JPEG instance share a single VM invalidation
638  * engine (see amdgpu_jpeg_set_shared_inv_eng()). This returns true for every
639  * ring in the instance except the first (ring->pipe == 0), which owns the
640  * engine allocated by amdgpu_gmc_allocate_vm_inv_eng().
641  */
642 bool amdgpu_jpeg_is_shared_inv_eng(struct amdgpu_device *adev, struct amdgpu_ring *ring)
643 {
644 	return ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG && ring->pipe != 0;
645 }
646 
647 /**
648  * amdgpu_jpeg_set_shared_inv_eng - Propagate a VM invalidation engine to the rest of a JPEG
649  * instance
650  * @adev: Pointer to the AMDGPU device structure
651  * @ring: Pointer to the ring that just had a VM invalidation engine allocated
652  *
653  * No-op unless @ring is the owner (ring->pipe == 0) of a JPEG instance.
654  * When it is, mirrors its freshly allocated vm_inv_eng onto the rest of the
655  * rings in that instance, so the whole instance shares one engine instead of
656  * consuming one per ring.
657  */
658 void amdgpu_jpeg_set_shared_inv_eng(struct amdgpu_device *adev, struct amdgpu_ring *ring)
659 {
660 	int j;
661 
662 	if (ring->funcs->type != AMDGPU_RING_TYPE_VCN_JPEG || ring->pipe != 0)
663 		return;
664 
665 	for (j = 1; j < adev->jpeg.num_jpeg_rings; j++)
666 		adev->jpeg.inst[ring->me].ring_dec[j].vm_inv_eng = ring->vm_inv_eng;
667 }
668