xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_jpeg.c (revision 2a1eea8fd601db4c52f0d14f8871663b7b052c91)
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_jpeg.h"
29 #include "amdgpu_pm.h"
30 #include "soc15d.h"
31 #include "soc15_common.h"
32 
33 #define JPEG_IDLE_TIMEOUT	msecs_to_jiffies(1000)
34 
35 static void amdgpu_jpeg_idle_work_handler(struct work_struct *work);
36 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev);
37 
38 int amdgpu_jpeg_sw_init(struct amdgpu_device *adev)
39 {
40 	int i, r;
41 
42 	INIT_DELAYED_WORK(&adev->jpeg.idle_work, amdgpu_jpeg_idle_work_handler);
43 	mutex_init(&adev->jpeg.jpeg_pg_lock);
44 	atomic_set(&adev->jpeg.total_submission_cnt, 0);
45 
46 	if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
47 	    (adev->pg_flags & AMD_PG_SUPPORT_JPEG_DPG))
48 		adev->jpeg.indirect_sram = true;
49 
50 	for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
51 		if (adev->jpeg.harvest_config & (1U << i))
52 			continue;
53 
54 		if (adev->jpeg.indirect_sram) {
55 			r = amdgpu_bo_create_kernel(adev, 64 * 2 * 4, PAGE_SIZE,
56 					AMDGPU_GEM_DOMAIN_VRAM |
57 					AMDGPU_GEM_DOMAIN_GTT,
58 					&adev->jpeg.inst[i].dpg_sram_bo,
59 					&adev->jpeg.inst[i].dpg_sram_gpu_addr,
60 					&adev->jpeg.inst[i].dpg_sram_cpu_addr);
61 			if (r) {
62 				dev_err(adev->dev,
63 				"JPEG %d (%d) failed to allocate DPG bo\n", i, r);
64 				return r;
65 			}
66 		}
67 	}
68 
69 	return 0;
70 }
71 
72 int amdgpu_jpeg_sw_fini(struct amdgpu_device *adev)
73 {
74 	int i, j;
75 
76 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
77 		if (adev->jpeg.harvest_config & (1U << i))
78 			continue;
79 
80 		amdgpu_bo_free_kernel(
81 			&adev->jpeg.inst[i].dpg_sram_bo,
82 			&adev->jpeg.inst[i].dpg_sram_gpu_addr,
83 			(void **)&adev->jpeg.inst[i].dpg_sram_cpu_addr);
84 
85 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j)
86 			amdgpu_ring_fini(&adev->jpeg.inst[i].ring_dec[j]);
87 	}
88 
89 	if (adev->jpeg.reg_list)
90 		amdgpu_jpeg_reg_dump_fini(adev);
91 
92 	mutex_destroy(&adev->jpeg.jpeg_pg_lock);
93 
94 	return 0;
95 }
96 
97 int amdgpu_jpeg_suspend(struct amdgpu_device *adev)
98 {
99 	cancel_delayed_work_sync(&adev->jpeg.idle_work);
100 
101 	return 0;
102 }
103 
104 int amdgpu_jpeg_resume(struct amdgpu_device *adev)
105 {
106 	return 0;
107 }
108 
109 static void amdgpu_jpeg_idle_work_handler(struct work_struct *work)
110 {
111 	struct amdgpu_device *adev =
112 		container_of(work, struct amdgpu_device, jpeg.idle_work.work);
113 	unsigned int fences = 0;
114 	unsigned int i, j;
115 
116 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
117 		if (adev->jpeg.harvest_config & (1U << i))
118 			continue;
119 
120 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j)
121 			fences += amdgpu_fence_count_emitted(&adev->jpeg.inst[i].ring_dec[j]);
122 	}
123 
124 	if (!fences && !atomic_read(&adev->jpeg.total_submission_cnt))
125 		amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
126 						       AMD_PG_STATE_GATE);
127 	else
128 		schedule_delayed_work(&adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT);
129 }
130 
131 void amdgpu_jpeg_ring_begin_use(struct amdgpu_ring *ring)
132 {
133 	struct amdgpu_device *adev = ring->adev;
134 
135 	atomic_inc(&adev->jpeg.total_submission_cnt);
136 	cancel_delayed_work_sync(&adev->jpeg.idle_work);
137 
138 	mutex_lock(&adev->jpeg.jpeg_pg_lock);
139 	amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_JPEG,
140 						       AMD_PG_STATE_UNGATE);
141 	mutex_unlock(&adev->jpeg.jpeg_pg_lock);
142 }
143 
144 void amdgpu_jpeg_ring_end_use(struct amdgpu_ring *ring)
145 {
146 	atomic_dec(&ring->adev->jpeg.total_submission_cnt);
147 	schedule_delayed_work(&ring->adev->jpeg.idle_work, JPEG_IDLE_TIMEOUT);
148 }
149 
150 int amdgpu_jpeg_dec_ring_test_ring(struct amdgpu_ring *ring)
151 {
152 	struct amdgpu_device *adev = ring->adev;
153 	uint32_t tmp = 0;
154 	unsigned i;
155 	int r;
156 
157 	/* JPEG in SRIOV does not support direct register read/write */
158 	if (amdgpu_sriov_vf(adev))
159 		return 0;
160 
161 	r = amdgpu_ring_alloc(ring, 3);
162 	if (r)
163 		return r;
164 
165 	WREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe], 0xCAFEDEAD);
166 	/* Add a read register to make sure the write register is executed. */
167 	RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]);
168 
169 	amdgpu_ring_write(ring, PACKET0(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0));
170 	amdgpu_ring_write(ring, 0xABADCAFE);
171 	amdgpu_ring_commit(ring);
172 
173 	for (i = 0; i < adev->usec_timeout; i++) {
174 		tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]);
175 		if (tmp == 0xABADCAFE)
176 			break;
177 		udelay(1);
178 	}
179 
180 	if (i >= adev->usec_timeout)
181 		r = -ETIMEDOUT;
182 
183 	return r;
184 }
185 
186 static int amdgpu_jpeg_dec_set_reg(struct amdgpu_ring *ring, uint32_t handle,
187 		struct dma_fence **fence)
188 {
189 	struct amdgpu_device *adev = ring->adev;
190 	struct amdgpu_job *job;
191 	struct amdgpu_ib *ib;
192 	struct dma_fence *f = NULL;
193 	const unsigned ib_size_dw = 16;
194 	int i, r;
195 
196 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4,
197 				     AMDGPU_IB_POOL_DIRECT, &job,
198 				     AMDGPU_KERNEL_JOB_ID_VCN_RING_TEST);
199 	if (r)
200 		return r;
201 
202 	ib = &job->ibs[0];
203 
204 	ib->ptr[0] = PACKETJ(adev->jpeg.internal.jpeg_pitch[ring->pipe], 0, 0, PACKETJ_TYPE0);
205 	ib->ptr[1] = 0xDEADBEEF;
206 	for (i = 2; i < 16; i += 2) {
207 		ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6);
208 		ib->ptr[i+1] = 0;
209 	}
210 	ib->length_dw = 16;
211 
212 	r = amdgpu_job_submit_direct(job, ring, &f);
213 	if (r)
214 		goto err;
215 
216 	if (fence)
217 		*fence = dma_fence_get(f);
218 	dma_fence_put(f);
219 
220 	return 0;
221 
222 err:
223 	amdgpu_job_free(job);
224 	return r;
225 }
226 
227 int amdgpu_jpeg_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
228 {
229 	struct amdgpu_device *adev = ring->adev;
230 	uint32_t tmp = 0;
231 	unsigned i;
232 	struct dma_fence *fence = NULL;
233 	long r = 0;
234 
235 	r = amdgpu_jpeg_dec_set_reg(ring, 1, &fence);
236 	if (r)
237 		goto error;
238 
239 	r = dma_fence_wait_timeout(fence, false, timeout);
240 	if (r == 0) {
241 		r = -ETIMEDOUT;
242 		goto error;
243 	} else if (r < 0) {
244 		goto error;
245 	} else {
246 		r = 0;
247 	}
248 
249 	if (!amdgpu_sriov_vf(adev)) {
250 		for (i = 0; i < adev->usec_timeout; i++) {
251 			tmp = RREG32(adev->jpeg.inst[ring->me].external.jpeg_pitch[ring->pipe]);
252 			if (tmp == 0xDEADBEEF)
253 				break;
254 			udelay(1);
255 			if (amdgpu_emu_mode == 1)
256 				udelay(10);
257 		}
258 
259 		if (i >= adev->usec_timeout)
260 			r = -ETIMEDOUT;
261 	}
262 
263 	dma_fence_put(fence);
264 error:
265 	return r;
266 }
267 
268 int amdgpu_jpeg_process_poison_irq(struct amdgpu_device *adev,
269 				struct amdgpu_irq_src *source,
270 				struct amdgpu_iv_entry *entry)
271 {
272 	struct ras_common_if *ras_if = adev->jpeg.ras_if;
273 	struct ras_dispatch_if ih_data = {
274 		.entry = entry,
275 	};
276 
277 	if (!ras_if)
278 		return 0;
279 
280 	ih_data.head = *ras_if;
281 	amdgpu_ras_interrupt_dispatch(adev, &ih_data);
282 
283 	return 0;
284 }
285 
286 int amdgpu_jpeg_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
287 {
288 	int r, i;
289 
290 	r = amdgpu_ras_block_late_init(adev, ras_block);
291 	if (r)
292 		return r;
293 
294 	if (amdgpu_ras_is_supported(adev, ras_block->block)) {
295 		for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
296 			if (adev->jpeg.harvest_config & (1 << i) ||
297 			    !adev->jpeg.inst[i].ras_poison_irq.funcs)
298 				continue;
299 
300 			r = amdgpu_irq_get(adev, &adev->jpeg.inst[i].ras_poison_irq, 0);
301 			if (r)
302 				goto late_fini;
303 		}
304 	}
305 	return 0;
306 
307 late_fini:
308 	amdgpu_ras_block_late_fini(adev, ras_block);
309 	return r;
310 }
311 
312 int amdgpu_jpeg_ras_sw_init(struct amdgpu_device *adev)
313 {
314 	int err;
315 	struct amdgpu_jpeg_ras *ras;
316 
317 	if (!adev->jpeg.ras)
318 		return 0;
319 
320 	ras = adev->jpeg.ras;
321 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
322 	if (err) {
323 		dev_err(adev->dev, "Failed to register jpeg ras block!\n");
324 		return err;
325 	}
326 
327 	strcpy(ras->ras_block.ras_comm.name, "jpeg");
328 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__JPEG;
329 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__POISON;
330 	adev->jpeg.ras_if = &ras->ras_block.ras_comm;
331 
332 	if (!ras->ras_block.ras_late_init)
333 		ras->ras_block.ras_late_init = amdgpu_jpeg_ras_late_init;
334 
335 	return 0;
336 }
337 
338 int amdgpu_jpeg_psp_update_sram(struct amdgpu_device *adev, int inst_idx,
339 			       enum AMDGPU_UCODE_ID ucode_id)
340 {
341 	struct amdgpu_firmware_info ucode = {
342 		.ucode_id = AMDGPU_UCODE_ID_JPEG_RAM,
343 		.mc_addr = adev->jpeg.inst[inst_idx].dpg_sram_gpu_addr,
344 		.ucode_size = ((uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_curr_addr -
345 			      (uintptr_t)adev->jpeg.inst[inst_idx].dpg_sram_cpu_addr),
346 	};
347 
348 	return psp_execute_ip_fw_load(&adev->psp, &ucode);
349 }
350 
351 /*
352  * debugfs for to enable/disable jpeg job submission to specific core.
353  */
354 #if defined(CONFIG_DEBUG_FS)
355 static int amdgpu_debugfs_jpeg_sched_mask_set(void *data, u64 val)
356 {
357 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
358 	u32 i, j;
359 	u64 mask = 0;
360 	struct amdgpu_ring *ring;
361 
362 	if (!adev)
363 		return -ENODEV;
364 
365 	mask = (1ULL << (adev->jpeg.num_jpeg_inst * adev->jpeg.num_jpeg_rings)) - 1;
366 	if ((val & mask) == 0)
367 		return -EINVAL;
368 
369 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
370 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) {
371 			ring = &adev->jpeg.inst[i].ring_dec[j];
372 			if (val & (1 << ((i * adev->jpeg.num_jpeg_rings) + j)))
373 				ring->sched.ready = true;
374 			else
375 				ring->sched.ready = false;
376 		}
377 	}
378 	/* publish sched.ready flag update effective immediately across smp */
379 	smp_rmb();
380 	return 0;
381 }
382 
383 static int amdgpu_debugfs_jpeg_sched_mask_get(void *data, u64 *val)
384 {
385 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
386 	u32 i, j;
387 	u64 mask = 0;
388 	struct amdgpu_ring *ring;
389 
390 	if (!adev)
391 		return -ENODEV;
392 	for (i = 0; i < adev->jpeg.num_jpeg_inst; ++i) {
393 		for (j = 0; j < adev->jpeg.num_jpeg_rings; ++j) {
394 			ring = &adev->jpeg.inst[i].ring_dec[j];
395 			if (ring->sched.ready)
396 				mask |= 1ULL << ((i * adev->jpeg.num_jpeg_rings) + j);
397 		}
398 	}
399 	*val = mask;
400 	return 0;
401 }
402 
403 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_jpeg_sched_mask_fops,
404 			 amdgpu_debugfs_jpeg_sched_mask_get,
405 			 amdgpu_debugfs_jpeg_sched_mask_set, "%llx\n");
406 
407 #endif
408 
409 void amdgpu_debugfs_jpeg_sched_mask_init(struct amdgpu_device *adev)
410 {
411 #if defined(CONFIG_DEBUG_FS)
412 	struct drm_minor *minor = adev_to_drm(adev)->primary;
413 	struct dentry *root = minor->debugfs_root;
414 	char name[32];
415 
416 	if (!(adev->jpeg.num_jpeg_inst > 1) && !(adev->jpeg.num_jpeg_rings > 1))
417 		return;
418 	sprintf(name, "amdgpu_jpeg_sched_mask");
419 	debugfs_create_file(name, 0600, root, adev,
420 			    &amdgpu_debugfs_jpeg_sched_mask_fops);
421 #endif
422 }
423 
424 static ssize_t amdgpu_get_jpeg_reset_mask(struct device *dev,
425 						struct device_attribute *attr,
426 						char *buf)
427 {
428 	struct drm_device *ddev = dev_get_drvdata(dev);
429 	struct amdgpu_device *adev = drm_to_adev(ddev);
430 
431 	if (!adev)
432 		return -ENODEV;
433 
434 	return amdgpu_show_reset_mask(buf, adev->jpeg.supported_reset);
435 }
436 
437 static DEVICE_ATTR(jpeg_reset_mask, 0444,
438 		   amdgpu_get_jpeg_reset_mask, NULL);
439 
440 int amdgpu_jpeg_sysfs_reset_mask_init(struct amdgpu_device *adev)
441 {
442 	int r = 0;
443 
444 	if (adev->jpeg.num_jpeg_inst) {
445 		r = device_create_file(adev->dev, &dev_attr_jpeg_reset_mask);
446 		if (r)
447 			return r;
448 	}
449 
450 	return r;
451 }
452 
453 void amdgpu_jpeg_sysfs_reset_mask_fini(struct amdgpu_device *adev)
454 {
455 	if (adev->dev->kobj.sd) {
456 		if (adev->jpeg.num_jpeg_inst)
457 			device_remove_file(adev->dev, &dev_attr_jpeg_reset_mask);
458 	}
459 }
460 
461 int amdgpu_jpeg_reg_dump_init(struct amdgpu_device *adev,
462 			       const struct amdgpu_hwip_reg_entry *reg, u32 count)
463 {
464 	adev->jpeg.ip_dump = kcalloc(adev->jpeg.num_jpeg_inst * count,
465 				     sizeof(uint32_t), GFP_KERNEL);
466 	if (!adev->jpeg.ip_dump) {
467 		dev_err(adev->dev,
468 			"Failed to allocate memory for JPEG IP Dump\n");
469 		return -ENOMEM;
470 	}
471 	adev->jpeg.reg_list = reg;
472 	adev->jpeg.reg_count = count;
473 
474 	return 0;
475 }
476 
477 static void amdgpu_jpeg_reg_dump_fini(struct amdgpu_device *adev)
478 {
479 	kfree(adev->jpeg.ip_dump);
480 	adev->jpeg.reg_list = NULL;
481 	adev->jpeg.reg_count = 0;
482 }
483 
484 void amdgpu_jpeg_dump_ip_state(struct amdgpu_ip_block *ip_block)
485 {
486 	struct amdgpu_device *adev = ip_block->adev;
487 	u32 inst_off, inst_id, is_powered;
488 	int i, j;
489 
490 	if (!adev->jpeg.ip_dump)
491 		return;
492 
493 	for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
494 		if (adev->jpeg.harvest_config & (1 << i))
495 			continue;
496 
497 		inst_id = GET_INST(JPEG, i);
498 		inst_off = i * adev->jpeg.reg_count;
499 		/* check power status from UVD_JPEG_POWER_STATUS */
500 		adev->jpeg.ip_dump[inst_off] =
501 			RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[0],
502 							   inst_id));
503 		is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1);
504 
505 		if (is_powered)
506 			for (j = 1; j < adev->jpeg.reg_count; j++)
507 				adev->jpeg.ip_dump[inst_off + j] =
508 					RREG32(SOC15_REG_ENTRY_OFFSET_INST(adev->jpeg.reg_list[j],
509 									   inst_id));
510 	}
511 }
512 
513 void amdgpu_jpeg_print_ip_state(struct amdgpu_ip_block *ip_block, struct drm_printer *p)
514 {
515 	struct amdgpu_device *adev = ip_block->adev;
516 	u32 inst_off, is_powered;
517 	int i, j;
518 
519 	if (!adev->jpeg.ip_dump)
520 		return;
521 
522 	drm_printf(p, "num_instances:%d\n", adev->jpeg.num_jpeg_inst);
523 	for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
524 		if (adev->jpeg.harvest_config & (1 << i)) {
525 			drm_printf(p, "\nHarvested Instance:JPEG%d Skipping dump\n", i);
526 			continue;
527 		}
528 
529 		inst_off = i * adev->jpeg.reg_count;
530 		is_powered = ((adev->jpeg.ip_dump[inst_off] & 0x1) != 1);
531 
532 		if (is_powered) {
533 			drm_printf(p, "Active Instance:JPEG%d\n", i);
534 			for (j = 0; j < adev->jpeg.reg_count; j++)
535 				drm_printf(p, "%-50s \t 0x%08x\n", adev->jpeg.reg_list[j].reg_name,
536 					   adev->jpeg.ip_dump[inst_off + j]);
537 		} else
538 			drm_printf(p, "\nInactive Instance:JPEG%d\n", i);
539 	}
540 }
541