xref: /linux/drivers/media/platform/qcom/venus/hfi_venus.c (revision ec8a42e7343234802b9054874fe01810880289ce)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2017 Linaro Ltd.
5  */
6 
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/interrupt.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 
15 #include "core.h"
16 #include "hfi_cmds.h"
17 #include "hfi_msgs.h"
18 #include "hfi_venus.h"
19 #include "hfi_venus_io.h"
20 #include "firmware.h"
21 
22 #define HFI_MASK_QHDR_TX_TYPE		0xff000000
23 #define HFI_MASK_QHDR_RX_TYPE		0x00ff0000
24 #define HFI_MASK_QHDR_PRI_TYPE		0x0000ff00
25 #define HFI_MASK_QHDR_ID_TYPE		0x000000ff
26 
27 #define HFI_HOST_TO_CTRL_CMD_Q		0
28 #define HFI_CTRL_TO_HOST_MSG_Q		1
29 #define HFI_CTRL_TO_HOST_DBG_Q		2
30 #define HFI_MASK_QHDR_STATUS		0x000000ff
31 
32 #define IFACEQ_NUM			3
33 #define IFACEQ_CMD_IDX			0
34 #define IFACEQ_MSG_IDX			1
35 #define IFACEQ_DBG_IDX			2
36 #define IFACEQ_MAX_BUF_COUNT		50
37 #define IFACEQ_MAX_PARALLEL_CLNTS	16
38 #define IFACEQ_DFLT_QHDR		0x01010000
39 
40 #define POLL_INTERVAL_US		50
41 
42 #define IFACEQ_MAX_PKT_SIZE		1024
43 #define IFACEQ_MED_PKT_SIZE		768
44 #define IFACEQ_MIN_PKT_SIZE		8
45 #define IFACEQ_VAR_SMALL_PKT_SIZE	100
46 #define IFACEQ_VAR_LARGE_PKT_SIZE	512
47 #define IFACEQ_VAR_HUGE_PKT_SIZE	(1024 * 12)
48 
49 struct hfi_queue_table_header {
50 	u32 version;
51 	u32 size;
52 	u32 qhdr0_offset;
53 	u32 qhdr_size;
54 	u32 num_q;
55 	u32 num_active_q;
56 };
57 
58 struct hfi_queue_header {
59 	u32 status;
60 	u32 start_addr;
61 	u32 type;
62 	u32 q_size;
63 	u32 pkt_size;
64 	u32 pkt_drop_cnt;
65 	u32 rx_wm;
66 	u32 tx_wm;
67 	u32 rx_req;
68 	u32 tx_req;
69 	u32 rx_irq_status;
70 	u32 tx_irq_status;
71 	u32 read_idx;
72 	u32 write_idx;
73 };
74 
75 #define IFACEQ_TABLE_SIZE	\
76 	(sizeof(struct hfi_queue_table_header) +	\
77 	 sizeof(struct hfi_queue_header) * IFACEQ_NUM)
78 
79 #define IFACEQ_QUEUE_SIZE	(IFACEQ_MAX_PKT_SIZE *	\
80 	IFACEQ_MAX_BUF_COUNT * IFACEQ_MAX_PARALLEL_CLNTS)
81 
82 #define IFACEQ_GET_QHDR_START_ADDR(ptr, i)	\
83 	(void *)(((ptr) + sizeof(struct hfi_queue_table_header)) +	\
84 		((i) * sizeof(struct hfi_queue_header)))
85 
86 #define QDSS_SIZE		SZ_4K
87 #define SFR_SIZE		SZ_4K
88 #define QUEUE_SIZE		\
89 	(IFACEQ_TABLE_SIZE + (IFACEQ_QUEUE_SIZE * IFACEQ_NUM))
90 
91 #define ALIGNED_QDSS_SIZE	ALIGN(QDSS_SIZE, SZ_4K)
92 #define ALIGNED_SFR_SIZE	ALIGN(SFR_SIZE, SZ_4K)
93 #define ALIGNED_QUEUE_SIZE	ALIGN(QUEUE_SIZE, SZ_4K)
94 #define SHARED_QSIZE		ALIGN(ALIGNED_SFR_SIZE + ALIGNED_QUEUE_SIZE + \
95 				      ALIGNED_QDSS_SIZE, SZ_1M)
96 
97 struct mem_desc {
98 	dma_addr_t da;	/* device address */
99 	void *kva;	/* kernel virtual address */
100 	u32 size;
101 	unsigned long attrs;
102 };
103 
104 struct iface_queue {
105 	struct hfi_queue_header *qhdr;
106 	struct mem_desc qmem;
107 };
108 
109 enum venus_state {
110 	VENUS_STATE_DEINIT = 1,
111 	VENUS_STATE_INIT,
112 };
113 
114 struct venus_hfi_device {
115 	struct venus_core *core;
116 	u32 irq_status;
117 	u32 last_packet_type;
118 	bool power_enabled;
119 	bool suspended;
120 	enum venus_state state;
121 	/* serialize read / write to the shared memory */
122 	struct mutex lock;
123 	struct completion pwr_collapse_prep;
124 	struct completion release_resource;
125 	struct mem_desc ifaceq_table;
126 	struct mem_desc sfr;
127 	struct iface_queue queues[IFACEQ_NUM];
128 	u8 pkt_buf[IFACEQ_VAR_HUGE_PKT_SIZE];
129 	u8 dbg_buf[IFACEQ_VAR_HUGE_PKT_SIZE];
130 };
131 
132 static bool venus_pkt_debug;
133 int venus_fw_debug = HFI_DEBUG_MSG_ERROR | HFI_DEBUG_MSG_FATAL;
134 static bool venus_sys_idle_indicator;
135 static bool venus_fw_low_power_mode = true;
136 static int venus_hw_rsp_timeout = 1000;
137 static bool venus_fw_coverage;
138 
139 static void venus_set_state(struct venus_hfi_device *hdev,
140 			    enum venus_state state)
141 {
142 	mutex_lock(&hdev->lock);
143 	hdev->state = state;
144 	mutex_unlock(&hdev->lock);
145 }
146 
147 static bool venus_is_valid_state(struct venus_hfi_device *hdev)
148 {
149 	return hdev->state != VENUS_STATE_DEINIT;
150 }
151 
152 static void venus_dump_packet(struct venus_hfi_device *hdev, const void *packet)
153 {
154 	size_t pkt_size = *(u32 *)packet;
155 
156 	if (!venus_pkt_debug)
157 		return;
158 
159 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 1, packet,
160 		       pkt_size, true);
161 }
162 
163 static int venus_write_queue(struct venus_hfi_device *hdev,
164 			     struct iface_queue *queue,
165 			     void *packet, u32 *rx_req)
166 {
167 	struct hfi_queue_header *qhdr;
168 	u32 dwords, new_wr_idx;
169 	u32 empty_space, rd_idx, wr_idx, qsize;
170 	u32 *wr_ptr;
171 
172 	if (!queue->qmem.kva)
173 		return -EINVAL;
174 
175 	qhdr = queue->qhdr;
176 	if (!qhdr)
177 		return -EINVAL;
178 
179 	venus_dump_packet(hdev, packet);
180 
181 	dwords = (*(u32 *)packet) >> 2;
182 	if (!dwords)
183 		return -EINVAL;
184 
185 	rd_idx = qhdr->read_idx;
186 	wr_idx = qhdr->write_idx;
187 	qsize = qhdr->q_size;
188 	/* ensure rd/wr indices's are read from memory */
189 	rmb();
190 
191 	if (wr_idx >= rd_idx)
192 		empty_space = qsize - (wr_idx - rd_idx);
193 	else
194 		empty_space = rd_idx - wr_idx;
195 
196 	if (empty_space <= dwords) {
197 		qhdr->tx_req = 1;
198 		/* ensure tx_req is updated in memory */
199 		wmb();
200 		return -ENOSPC;
201 	}
202 
203 	qhdr->tx_req = 0;
204 	/* ensure tx_req is updated in memory */
205 	wmb();
206 
207 	new_wr_idx = wr_idx + dwords;
208 	wr_ptr = (u32 *)(queue->qmem.kva + (wr_idx << 2));
209 	if (new_wr_idx < qsize) {
210 		memcpy(wr_ptr, packet, dwords << 2);
211 	} else {
212 		size_t len;
213 
214 		new_wr_idx -= qsize;
215 		len = (dwords - new_wr_idx) << 2;
216 		memcpy(wr_ptr, packet, len);
217 		memcpy(queue->qmem.kva, packet + len, new_wr_idx << 2);
218 	}
219 
220 	/* make sure packet is written before updating the write index */
221 	wmb();
222 
223 	qhdr->write_idx = new_wr_idx;
224 	*rx_req = qhdr->rx_req ? 1 : 0;
225 
226 	/* make sure write index is updated before an interrupt is raised */
227 	mb();
228 
229 	return 0;
230 }
231 
232 static int venus_read_queue(struct venus_hfi_device *hdev,
233 			    struct iface_queue *queue, void *pkt, u32 *tx_req)
234 {
235 	struct hfi_queue_header *qhdr;
236 	u32 dwords, new_rd_idx;
237 	u32 rd_idx, wr_idx, type, qsize;
238 	u32 *rd_ptr;
239 	u32 recv_request = 0;
240 	int ret = 0;
241 
242 	if (!queue->qmem.kva)
243 		return -EINVAL;
244 
245 	qhdr = queue->qhdr;
246 	if (!qhdr)
247 		return -EINVAL;
248 
249 	type = qhdr->type;
250 	rd_idx = qhdr->read_idx;
251 	wr_idx = qhdr->write_idx;
252 	qsize = qhdr->q_size;
253 
254 	/* make sure data is valid before using it */
255 	rmb();
256 
257 	/*
258 	 * Do not set receive request for debug queue, if set, Venus generates
259 	 * interrupt for debug messages even when there is no response message
260 	 * available. In general debug queue will not become full as it is being
261 	 * emptied out for every interrupt from Venus. Venus will anyway
262 	 * generates interrupt if it is full.
263 	 */
264 	if (type & HFI_CTRL_TO_HOST_MSG_Q)
265 		recv_request = 1;
266 
267 	if (rd_idx == wr_idx) {
268 		qhdr->rx_req = recv_request;
269 		*tx_req = 0;
270 		/* update rx_req field in memory */
271 		wmb();
272 		return -ENODATA;
273 	}
274 
275 	rd_ptr = (u32 *)(queue->qmem.kva + (rd_idx << 2));
276 	dwords = *rd_ptr >> 2;
277 	if (!dwords)
278 		return -EINVAL;
279 
280 	new_rd_idx = rd_idx + dwords;
281 	if (((dwords << 2) <= IFACEQ_VAR_HUGE_PKT_SIZE) && rd_idx <= qsize) {
282 		if (new_rd_idx < qsize) {
283 			memcpy(pkt, rd_ptr, dwords << 2);
284 		} else {
285 			size_t len;
286 
287 			new_rd_idx -= qsize;
288 			len = (dwords - new_rd_idx) << 2;
289 			memcpy(pkt, rd_ptr, len);
290 			memcpy(pkt + len, queue->qmem.kva, new_rd_idx << 2);
291 		}
292 	} else {
293 		/* bad packet received, dropping */
294 		new_rd_idx = qhdr->write_idx;
295 		ret = -EBADMSG;
296 	}
297 
298 	/* ensure the packet is read before updating read index */
299 	rmb();
300 
301 	qhdr->read_idx = new_rd_idx;
302 	/* ensure updating read index */
303 	wmb();
304 
305 	rd_idx = qhdr->read_idx;
306 	wr_idx = qhdr->write_idx;
307 	/* ensure rd/wr indices are read from memory */
308 	rmb();
309 
310 	if (rd_idx != wr_idx)
311 		qhdr->rx_req = 0;
312 	else
313 		qhdr->rx_req = recv_request;
314 
315 	*tx_req = qhdr->tx_req ? 1 : 0;
316 
317 	/* ensure rx_req is stored to memory and tx_req is loaded from memory */
318 	mb();
319 
320 	venus_dump_packet(hdev, pkt);
321 
322 	return ret;
323 }
324 
325 static int venus_alloc(struct venus_hfi_device *hdev, struct mem_desc *desc,
326 		       u32 size)
327 {
328 	struct device *dev = hdev->core->dev;
329 
330 	desc->attrs = DMA_ATTR_WRITE_COMBINE;
331 	desc->size = ALIGN(size, SZ_4K);
332 
333 	desc->kva = dma_alloc_attrs(dev, desc->size, &desc->da, GFP_KERNEL,
334 				    desc->attrs);
335 	if (!desc->kva)
336 		return -ENOMEM;
337 
338 	return 0;
339 }
340 
341 static void venus_free(struct venus_hfi_device *hdev, struct mem_desc *mem)
342 {
343 	struct device *dev = hdev->core->dev;
344 
345 	dma_free_attrs(dev, mem->size, mem->kva, mem->da, mem->attrs);
346 }
347 
348 static void venus_writel(struct venus_hfi_device *hdev, u32 reg, u32 value)
349 {
350 	writel(value, hdev->core->base + reg);
351 }
352 
353 static u32 venus_readl(struct venus_hfi_device *hdev, u32 reg)
354 {
355 	return readl(hdev->core->base + reg);
356 }
357 
358 static void venus_set_registers(struct venus_hfi_device *hdev)
359 {
360 	const struct venus_resources *res = hdev->core->res;
361 	const struct reg_val *tbl = res->reg_tbl;
362 	unsigned int count = res->reg_tbl_size;
363 	unsigned int i;
364 
365 	for (i = 0; i < count; i++)
366 		venus_writel(hdev, tbl[i].reg, tbl[i].value);
367 }
368 
369 static void venus_soft_int(struct venus_hfi_device *hdev)
370 {
371 	venus_writel(hdev, CPU_IC_SOFTINT, BIT(CPU_IC_SOFTINT_H2A_SHIFT));
372 }
373 
374 static int venus_iface_cmdq_write_nolock(struct venus_hfi_device *hdev,
375 					 void *pkt)
376 {
377 	struct device *dev = hdev->core->dev;
378 	struct hfi_pkt_hdr *cmd_packet;
379 	struct iface_queue *queue;
380 	u32 rx_req;
381 	int ret;
382 
383 	if (!venus_is_valid_state(hdev))
384 		return -EINVAL;
385 
386 	cmd_packet = (struct hfi_pkt_hdr *)pkt;
387 	hdev->last_packet_type = cmd_packet->pkt_type;
388 
389 	queue = &hdev->queues[IFACEQ_CMD_IDX];
390 
391 	ret = venus_write_queue(hdev, queue, pkt, &rx_req);
392 	if (ret) {
393 		dev_err(dev, "write to iface cmd queue failed (%d)\n", ret);
394 		return ret;
395 	}
396 
397 	if (rx_req)
398 		venus_soft_int(hdev);
399 
400 	return 0;
401 }
402 
403 static int venus_iface_cmdq_write(struct venus_hfi_device *hdev, void *pkt)
404 {
405 	int ret;
406 
407 	mutex_lock(&hdev->lock);
408 	ret = venus_iface_cmdq_write_nolock(hdev, pkt);
409 	mutex_unlock(&hdev->lock);
410 
411 	return ret;
412 }
413 
414 static int venus_hfi_core_set_resource(struct venus_core *core, u32 id,
415 				       u32 size, u32 addr, void *cookie)
416 {
417 	struct venus_hfi_device *hdev = to_hfi_priv(core);
418 	struct hfi_sys_set_resource_pkt *pkt;
419 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
420 	int ret;
421 
422 	if (id == VIDC_RESOURCE_NONE)
423 		return 0;
424 
425 	pkt = (struct hfi_sys_set_resource_pkt *)packet;
426 
427 	ret = pkt_sys_set_resource(pkt, id, size, addr, cookie);
428 	if (ret)
429 		return ret;
430 
431 	ret = venus_iface_cmdq_write(hdev, pkt);
432 	if (ret)
433 		return ret;
434 
435 	return 0;
436 }
437 
438 static int venus_boot_core(struct venus_hfi_device *hdev)
439 {
440 	struct device *dev = hdev->core->dev;
441 	static const unsigned int max_tries = 100;
442 	u32 ctrl_status = 0;
443 	unsigned int count = 0;
444 	int ret = 0;
445 
446 	venus_writel(hdev, VIDC_CTRL_INIT, BIT(VIDC_CTRL_INIT_CTRL_SHIFT));
447 	venus_writel(hdev, WRAPPER_INTR_MASK, WRAPPER_INTR_MASK_A2HVCODEC_MASK);
448 	venus_writel(hdev, CPU_CS_SCIACMDARG3, 1);
449 
450 	while (!ctrl_status && count < max_tries) {
451 		ctrl_status = venus_readl(hdev, CPU_CS_SCIACMDARG0);
452 		if ((ctrl_status & CPU_CS_SCIACMDARG0_ERROR_STATUS_MASK) == 4) {
453 			dev_err(dev, "invalid setting for UC_REGION\n");
454 			ret = -EINVAL;
455 			break;
456 		}
457 
458 		usleep_range(500, 1000);
459 		count++;
460 	}
461 
462 	if (count >= max_tries)
463 		ret = -ETIMEDOUT;
464 
465 	return ret;
466 }
467 
468 static u32 venus_hwversion(struct venus_hfi_device *hdev)
469 {
470 	struct device *dev = hdev->core->dev;
471 	u32 ver = venus_readl(hdev, WRAPPER_HW_VERSION);
472 	u32 major, minor, step;
473 
474 	major = ver & WRAPPER_HW_VERSION_MAJOR_VERSION_MASK;
475 	major = major >> WRAPPER_HW_VERSION_MAJOR_VERSION_SHIFT;
476 	minor = ver & WRAPPER_HW_VERSION_MINOR_VERSION_MASK;
477 	minor = minor >> WRAPPER_HW_VERSION_MINOR_VERSION_SHIFT;
478 	step = ver & WRAPPER_HW_VERSION_STEP_VERSION_MASK;
479 
480 	dev_dbg(dev, VDBGL "venus hw version %x.%x.%x\n", major, minor, step);
481 
482 	return major;
483 }
484 
485 static int venus_run(struct venus_hfi_device *hdev)
486 {
487 	struct device *dev = hdev->core->dev;
488 	int ret;
489 
490 	/*
491 	 * Re-program all of the registers that get reset as a result of
492 	 * regulator_disable() and _enable()
493 	 */
494 	venus_set_registers(hdev);
495 
496 	venus_writel(hdev, UC_REGION_ADDR, hdev->ifaceq_table.da);
497 	venus_writel(hdev, UC_REGION_SIZE, SHARED_QSIZE);
498 	venus_writel(hdev, CPU_CS_SCIACMDARG2, hdev->ifaceq_table.da);
499 	venus_writel(hdev, CPU_CS_SCIACMDARG1, 0x01);
500 	if (hdev->sfr.da)
501 		venus_writel(hdev, SFR_ADDR, hdev->sfr.da);
502 
503 	ret = venus_boot_core(hdev);
504 	if (ret) {
505 		dev_err(dev, "failed to reset venus core\n");
506 		return ret;
507 	}
508 
509 	venus_hwversion(hdev);
510 
511 	return 0;
512 }
513 
514 static int venus_halt_axi(struct venus_hfi_device *hdev)
515 {
516 	void __iomem *base = hdev->core->base;
517 	struct device *dev = hdev->core->dev;
518 	u32 val;
519 	int ret;
520 
521 	if (IS_V4(hdev->core)) {
522 		val = venus_readl(hdev, WRAPPER_CPU_AXI_HALT);
523 		val |= WRAPPER_CPU_AXI_HALT_HALT;
524 		venus_writel(hdev, WRAPPER_CPU_AXI_HALT, val);
525 
526 		ret = readl_poll_timeout(base + WRAPPER_CPU_AXI_HALT_STATUS,
527 					 val,
528 					 val & WRAPPER_CPU_AXI_HALT_STATUS_IDLE,
529 					 POLL_INTERVAL_US,
530 					 VBIF_AXI_HALT_ACK_TIMEOUT_US);
531 		if (ret) {
532 			dev_err(dev, "AXI bus port halt timeout\n");
533 			return ret;
534 		}
535 
536 		return 0;
537 	}
538 
539 	/* Halt AXI and AXI IMEM VBIF Access */
540 	val = venus_readl(hdev, VBIF_AXI_HALT_CTRL0);
541 	val |= VBIF_AXI_HALT_CTRL0_HALT_REQ;
542 	venus_writel(hdev, VBIF_AXI_HALT_CTRL0, val);
543 
544 	/* Request for AXI bus port halt */
545 	ret = readl_poll_timeout(base + VBIF_AXI_HALT_CTRL1, val,
546 				 val & VBIF_AXI_HALT_CTRL1_HALT_ACK,
547 				 POLL_INTERVAL_US,
548 				 VBIF_AXI_HALT_ACK_TIMEOUT_US);
549 	if (ret) {
550 		dev_err(dev, "AXI bus port halt timeout\n");
551 		return ret;
552 	}
553 
554 	return 0;
555 }
556 
557 static int venus_power_off(struct venus_hfi_device *hdev)
558 {
559 	int ret;
560 
561 	if (!hdev->power_enabled)
562 		return 0;
563 
564 	ret = venus_set_hw_state_suspend(hdev->core);
565 	if (ret)
566 		return ret;
567 
568 	ret = venus_halt_axi(hdev);
569 	if (ret)
570 		return ret;
571 
572 	hdev->power_enabled = false;
573 
574 	return 0;
575 }
576 
577 static int venus_power_on(struct venus_hfi_device *hdev)
578 {
579 	int ret;
580 
581 	if (hdev->power_enabled)
582 		return 0;
583 
584 	ret = venus_set_hw_state_resume(hdev->core);
585 	if (ret)
586 		goto err;
587 
588 	ret = venus_run(hdev);
589 	if (ret)
590 		goto err_suspend;
591 
592 	hdev->power_enabled = true;
593 
594 	return 0;
595 
596 err_suspend:
597 	venus_set_hw_state_suspend(hdev->core);
598 err:
599 	hdev->power_enabled = false;
600 	return ret;
601 }
602 
603 static int venus_iface_msgq_read_nolock(struct venus_hfi_device *hdev,
604 					void *pkt)
605 {
606 	struct iface_queue *queue;
607 	u32 tx_req;
608 	int ret;
609 
610 	if (!venus_is_valid_state(hdev))
611 		return -EINVAL;
612 
613 	queue = &hdev->queues[IFACEQ_MSG_IDX];
614 
615 	ret = venus_read_queue(hdev, queue, pkt, &tx_req);
616 	if (ret)
617 		return ret;
618 
619 	if (tx_req)
620 		venus_soft_int(hdev);
621 
622 	return 0;
623 }
624 
625 static int venus_iface_msgq_read(struct venus_hfi_device *hdev, void *pkt)
626 {
627 	int ret;
628 
629 	mutex_lock(&hdev->lock);
630 	ret = venus_iface_msgq_read_nolock(hdev, pkt);
631 	mutex_unlock(&hdev->lock);
632 
633 	return ret;
634 }
635 
636 static int venus_iface_dbgq_read_nolock(struct venus_hfi_device *hdev,
637 					void *pkt)
638 {
639 	struct iface_queue *queue;
640 	u32 tx_req;
641 	int ret;
642 
643 	ret = venus_is_valid_state(hdev);
644 	if (!ret)
645 		return -EINVAL;
646 
647 	queue = &hdev->queues[IFACEQ_DBG_IDX];
648 
649 	ret = venus_read_queue(hdev, queue, pkt, &tx_req);
650 	if (ret)
651 		return ret;
652 
653 	if (tx_req)
654 		venus_soft_int(hdev);
655 
656 	return 0;
657 }
658 
659 static int venus_iface_dbgq_read(struct venus_hfi_device *hdev, void *pkt)
660 {
661 	int ret;
662 
663 	if (!pkt)
664 		return -EINVAL;
665 
666 	mutex_lock(&hdev->lock);
667 	ret = venus_iface_dbgq_read_nolock(hdev, pkt);
668 	mutex_unlock(&hdev->lock);
669 
670 	return ret;
671 }
672 
673 static void venus_set_qhdr_defaults(struct hfi_queue_header *qhdr)
674 {
675 	qhdr->status = 1;
676 	qhdr->type = IFACEQ_DFLT_QHDR;
677 	qhdr->q_size = IFACEQ_QUEUE_SIZE / 4;
678 	qhdr->pkt_size = 0;
679 	qhdr->rx_wm = 1;
680 	qhdr->tx_wm = 1;
681 	qhdr->rx_req = 1;
682 	qhdr->tx_req = 0;
683 	qhdr->rx_irq_status = 0;
684 	qhdr->tx_irq_status = 0;
685 	qhdr->read_idx = 0;
686 	qhdr->write_idx = 0;
687 }
688 
689 static void venus_interface_queues_release(struct venus_hfi_device *hdev)
690 {
691 	mutex_lock(&hdev->lock);
692 
693 	venus_free(hdev, &hdev->ifaceq_table);
694 	venus_free(hdev, &hdev->sfr);
695 
696 	memset(hdev->queues, 0, sizeof(hdev->queues));
697 	memset(&hdev->ifaceq_table, 0, sizeof(hdev->ifaceq_table));
698 	memset(&hdev->sfr, 0, sizeof(hdev->sfr));
699 
700 	mutex_unlock(&hdev->lock);
701 }
702 
703 static int venus_interface_queues_init(struct venus_hfi_device *hdev)
704 {
705 	struct hfi_queue_table_header *tbl_hdr;
706 	struct iface_queue *queue;
707 	struct hfi_sfr *sfr;
708 	struct mem_desc desc = {0};
709 	unsigned int offset;
710 	unsigned int i;
711 	int ret;
712 
713 	ret = venus_alloc(hdev, &desc, ALIGNED_QUEUE_SIZE);
714 	if (ret)
715 		return ret;
716 
717 	hdev->ifaceq_table = desc;
718 	offset = IFACEQ_TABLE_SIZE;
719 
720 	for (i = 0; i < IFACEQ_NUM; i++) {
721 		queue = &hdev->queues[i];
722 		queue->qmem.da = desc.da + offset;
723 		queue->qmem.kva = desc.kva + offset;
724 		queue->qmem.size = IFACEQ_QUEUE_SIZE;
725 		offset += queue->qmem.size;
726 		queue->qhdr =
727 			IFACEQ_GET_QHDR_START_ADDR(hdev->ifaceq_table.kva, i);
728 
729 		venus_set_qhdr_defaults(queue->qhdr);
730 
731 		queue->qhdr->start_addr = queue->qmem.da;
732 
733 		if (i == IFACEQ_CMD_IDX)
734 			queue->qhdr->type |= HFI_HOST_TO_CTRL_CMD_Q;
735 		else if (i == IFACEQ_MSG_IDX)
736 			queue->qhdr->type |= HFI_CTRL_TO_HOST_MSG_Q;
737 		else if (i == IFACEQ_DBG_IDX)
738 			queue->qhdr->type |= HFI_CTRL_TO_HOST_DBG_Q;
739 	}
740 
741 	tbl_hdr = hdev->ifaceq_table.kva;
742 	tbl_hdr->version = 0;
743 	tbl_hdr->size = IFACEQ_TABLE_SIZE;
744 	tbl_hdr->qhdr0_offset = sizeof(struct hfi_queue_table_header);
745 	tbl_hdr->qhdr_size = sizeof(struct hfi_queue_header);
746 	tbl_hdr->num_q = IFACEQ_NUM;
747 	tbl_hdr->num_active_q = IFACEQ_NUM;
748 
749 	/*
750 	 * Set receive request to zero on debug queue as there is no
751 	 * need of interrupt from video hardware for debug messages
752 	 */
753 	queue = &hdev->queues[IFACEQ_DBG_IDX];
754 	queue->qhdr->rx_req = 0;
755 
756 	ret = venus_alloc(hdev, &desc, ALIGNED_SFR_SIZE);
757 	if (ret) {
758 		hdev->sfr.da = 0;
759 	} else {
760 		hdev->sfr = desc;
761 		sfr = hdev->sfr.kva;
762 		sfr->buf_size = ALIGNED_SFR_SIZE;
763 	}
764 
765 	/* ensure table and queue header structs are settled in memory */
766 	wmb();
767 
768 	return 0;
769 }
770 
771 static int venus_sys_set_debug(struct venus_hfi_device *hdev, u32 debug)
772 {
773 	struct hfi_sys_set_property_pkt *pkt;
774 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
775 	int ret;
776 
777 	pkt = (struct hfi_sys_set_property_pkt *)packet;
778 
779 	pkt_sys_debug_config(pkt, HFI_DEBUG_MODE_QUEUE, debug);
780 
781 	ret = venus_iface_cmdq_write(hdev, pkt);
782 	if (ret)
783 		return ret;
784 
785 	return 0;
786 }
787 
788 static int venus_sys_set_coverage(struct venus_hfi_device *hdev, u32 mode)
789 {
790 	struct hfi_sys_set_property_pkt *pkt;
791 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
792 	int ret;
793 
794 	pkt = (struct hfi_sys_set_property_pkt *)packet;
795 
796 	pkt_sys_coverage_config(pkt, mode);
797 
798 	ret = venus_iface_cmdq_write(hdev, pkt);
799 	if (ret)
800 		return ret;
801 
802 	return 0;
803 }
804 
805 static int venus_sys_set_idle_message(struct venus_hfi_device *hdev,
806 				      bool enable)
807 {
808 	struct hfi_sys_set_property_pkt *pkt;
809 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
810 	int ret;
811 
812 	if (!enable)
813 		return 0;
814 
815 	pkt = (struct hfi_sys_set_property_pkt *)packet;
816 
817 	pkt_sys_idle_indicator(pkt, enable);
818 
819 	ret = venus_iface_cmdq_write(hdev, pkt);
820 	if (ret)
821 		return ret;
822 
823 	return 0;
824 }
825 
826 static int venus_sys_set_power_control(struct venus_hfi_device *hdev,
827 				       bool enable)
828 {
829 	struct hfi_sys_set_property_pkt *pkt;
830 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
831 	int ret;
832 
833 	pkt = (struct hfi_sys_set_property_pkt *)packet;
834 
835 	pkt_sys_power_control(pkt, enable);
836 
837 	ret = venus_iface_cmdq_write(hdev, pkt);
838 	if (ret)
839 		return ret;
840 
841 	return 0;
842 }
843 
844 static int venus_get_queue_size(struct venus_hfi_device *hdev,
845 				unsigned int index)
846 {
847 	struct hfi_queue_header *qhdr;
848 
849 	if (index >= IFACEQ_NUM)
850 		return -EINVAL;
851 
852 	qhdr = hdev->queues[index].qhdr;
853 	if (!qhdr)
854 		return -EINVAL;
855 
856 	return abs(qhdr->read_idx - qhdr->write_idx);
857 }
858 
859 static int venus_sys_set_default_properties(struct venus_hfi_device *hdev)
860 {
861 	struct device *dev = hdev->core->dev;
862 	int ret;
863 
864 	ret = venus_sys_set_debug(hdev, venus_fw_debug);
865 	if (ret)
866 		dev_warn(dev, "setting fw debug msg ON failed (%d)\n", ret);
867 
868 	/*
869 	 * Idle indicator is disabled by default on some 4xx firmware versions,
870 	 * enable it explicitly in order to make suspend functional by checking
871 	 * WFI (wait-for-interrupt) bit.
872 	 */
873 	if (IS_V4(hdev->core))
874 		venus_sys_idle_indicator = true;
875 
876 	ret = venus_sys_set_idle_message(hdev, venus_sys_idle_indicator);
877 	if (ret)
878 		dev_warn(dev, "setting idle response ON failed (%d)\n", ret);
879 
880 	ret = venus_sys_set_power_control(hdev, venus_fw_low_power_mode);
881 	if (ret)
882 		dev_warn(dev, "setting hw power collapse ON failed (%d)\n",
883 			 ret);
884 
885 	return ret;
886 }
887 
888 static int venus_session_cmd(struct venus_inst *inst, u32 pkt_type)
889 {
890 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
891 	struct hfi_session_pkt pkt;
892 
893 	pkt_session_cmd(&pkt, pkt_type, inst);
894 
895 	return venus_iface_cmdq_write(hdev, &pkt);
896 }
897 
898 static void venus_flush_debug_queue(struct venus_hfi_device *hdev)
899 {
900 	struct device *dev = hdev->core->dev;
901 	void *packet = hdev->dbg_buf;
902 
903 	while (!venus_iface_dbgq_read(hdev, packet)) {
904 		struct hfi_msg_sys_coverage_pkt *pkt = packet;
905 
906 		if (pkt->hdr.pkt_type != HFI_MSG_SYS_COV) {
907 			struct hfi_msg_sys_debug_pkt *pkt = packet;
908 
909 			dev_dbg(dev, VDBGFW "%s", pkt->msg_data);
910 		}
911 	}
912 }
913 
914 static int venus_prepare_power_collapse(struct venus_hfi_device *hdev,
915 					bool wait)
916 {
917 	unsigned long timeout = msecs_to_jiffies(venus_hw_rsp_timeout);
918 	struct hfi_sys_pc_prep_pkt pkt;
919 	int ret;
920 
921 	init_completion(&hdev->pwr_collapse_prep);
922 
923 	pkt_sys_pc_prep(&pkt);
924 
925 	ret = venus_iface_cmdq_write(hdev, &pkt);
926 	if (ret)
927 		return ret;
928 
929 	if (!wait)
930 		return 0;
931 
932 	ret = wait_for_completion_timeout(&hdev->pwr_collapse_prep, timeout);
933 	if (!ret) {
934 		venus_flush_debug_queue(hdev);
935 		return -ETIMEDOUT;
936 	}
937 
938 	return 0;
939 }
940 
941 static int venus_are_queues_empty(struct venus_hfi_device *hdev)
942 {
943 	int ret1, ret2;
944 
945 	ret1 = venus_get_queue_size(hdev, IFACEQ_MSG_IDX);
946 	if (ret1 < 0)
947 		return ret1;
948 
949 	ret2 = venus_get_queue_size(hdev, IFACEQ_CMD_IDX);
950 	if (ret2 < 0)
951 		return ret2;
952 
953 	if (!ret1 && !ret2)
954 		return 1;
955 
956 	return 0;
957 }
958 
959 static void venus_sfr_print(struct venus_hfi_device *hdev)
960 {
961 	struct device *dev = hdev->core->dev;
962 	struct hfi_sfr *sfr = hdev->sfr.kva;
963 	void *p;
964 
965 	if (!sfr)
966 		return;
967 
968 	p = memchr(sfr->data, '\0', sfr->buf_size);
969 	/*
970 	 * SFR isn't guaranteed to be NULL terminated since SYS_ERROR indicates
971 	 * that Venus is in the process of crashing.
972 	 */
973 	if (!p)
974 		sfr->data[sfr->buf_size - 1] = '\0';
975 
976 	dev_err_ratelimited(dev, "SFR message from FW: %s\n", sfr->data);
977 }
978 
979 static void venus_process_msg_sys_error(struct venus_hfi_device *hdev,
980 					void *packet)
981 {
982 	struct hfi_msg_event_notify_pkt *event_pkt = packet;
983 
984 	if (event_pkt->event_id != HFI_EVENT_SYS_ERROR)
985 		return;
986 
987 	venus_set_state(hdev, VENUS_STATE_DEINIT);
988 
989 	venus_sfr_print(hdev);
990 }
991 
992 static irqreturn_t venus_isr_thread(struct venus_core *core)
993 {
994 	struct venus_hfi_device *hdev = to_hfi_priv(core);
995 	const struct venus_resources *res;
996 	void *pkt;
997 	u32 msg_ret;
998 
999 	if (!hdev)
1000 		return IRQ_NONE;
1001 
1002 	res = hdev->core->res;
1003 	pkt = hdev->pkt_buf;
1004 
1005 
1006 	while (!venus_iface_msgq_read(hdev, pkt)) {
1007 		msg_ret = hfi_process_msg_packet(core, pkt);
1008 		switch (msg_ret) {
1009 		case HFI_MSG_EVENT_NOTIFY:
1010 			venus_process_msg_sys_error(hdev, pkt);
1011 			break;
1012 		case HFI_MSG_SYS_INIT:
1013 			venus_hfi_core_set_resource(core, res->vmem_id,
1014 						    res->vmem_size,
1015 						    res->vmem_addr,
1016 						    hdev);
1017 			break;
1018 		case HFI_MSG_SYS_RELEASE_RESOURCE:
1019 			complete(&hdev->release_resource);
1020 			break;
1021 		case HFI_MSG_SYS_PC_PREP:
1022 			complete(&hdev->pwr_collapse_prep);
1023 			break;
1024 		default:
1025 			break;
1026 		}
1027 	}
1028 
1029 	venus_flush_debug_queue(hdev);
1030 
1031 	return IRQ_HANDLED;
1032 }
1033 
1034 static irqreturn_t venus_isr(struct venus_core *core)
1035 {
1036 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1037 	u32 status;
1038 
1039 	if (!hdev)
1040 		return IRQ_NONE;
1041 
1042 	status = venus_readl(hdev, WRAPPER_INTR_STATUS);
1043 
1044 	if (status & WRAPPER_INTR_STATUS_A2H_MASK ||
1045 	    status & WRAPPER_INTR_STATUS_A2HWD_MASK ||
1046 	    status & CPU_CS_SCIACMDARG0_INIT_IDLE_MSG_MASK)
1047 		hdev->irq_status = status;
1048 
1049 	venus_writel(hdev, CPU_CS_A2HSOFTINTCLR, 1);
1050 	venus_writel(hdev, WRAPPER_INTR_CLEAR, status);
1051 
1052 	return IRQ_WAKE_THREAD;
1053 }
1054 
1055 static int venus_core_init(struct venus_core *core)
1056 {
1057 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1058 	struct device *dev = core->dev;
1059 	struct hfi_sys_get_property_pkt version_pkt;
1060 	struct hfi_sys_init_pkt pkt;
1061 	int ret;
1062 
1063 	pkt_sys_init(&pkt, HFI_VIDEO_ARCH_OX);
1064 
1065 	venus_set_state(hdev, VENUS_STATE_INIT);
1066 
1067 	ret = venus_iface_cmdq_write(hdev, &pkt);
1068 	if (ret)
1069 		return ret;
1070 
1071 	pkt_sys_image_version(&version_pkt);
1072 
1073 	ret = venus_iface_cmdq_write(hdev, &version_pkt);
1074 	if (ret)
1075 		dev_warn(dev, "failed to send image version pkt to fw\n");
1076 
1077 	ret = venus_sys_set_default_properties(hdev);
1078 	if (ret)
1079 		return ret;
1080 
1081 	return 0;
1082 }
1083 
1084 static int venus_core_deinit(struct venus_core *core)
1085 {
1086 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1087 
1088 	venus_set_state(hdev, VENUS_STATE_DEINIT);
1089 	hdev->suspended = true;
1090 	hdev->power_enabled = false;
1091 
1092 	return 0;
1093 }
1094 
1095 static int venus_core_ping(struct venus_core *core, u32 cookie)
1096 {
1097 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1098 	struct hfi_sys_ping_pkt pkt;
1099 
1100 	pkt_sys_ping(&pkt, cookie);
1101 
1102 	return venus_iface_cmdq_write(hdev, &pkt);
1103 }
1104 
1105 static int venus_core_trigger_ssr(struct venus_core *core, u32 trigger_type)
1106 {
1107 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1108 	struct hfi_sys_test_ssr_pkt pkt;
1109 	int ret;
1110 
1111 	ret = pkt_sys_ssr_cmd(&pkt, trigger_type);
1112 	if (ret)
1113 		return ret;
1114 
1115 	return venus_iface_cmdq_write(hdev, &pkt);
1116 }
1117 
1118 static int venus_session_init(struct venus_inst *inst, u32 session_type,
1119 			      u32 codec)
1120 {
1121 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1122 	struct hfi_session_init_pkt pkt;
1123 	int ret;
1124 
1125 	ret = venus_sys_set_debug(hdev, venus_fw_debug);
1126 	if (ret)
1127 		goto err;
1128 
1129 	ret = pkt_session_init(&pkt, inst, session_type, codec);
1130 	if (ret)
1131 		goto err;
1132 
1133 	ret = venus_iface_cmdq_write(hdev, &pkt);
1134 	if (ret)
1135 		goto err;
1136 
1137 	return 0;
1138 
1139 err:
1140 	venus_flush_debug_queue(hdev);
1141 	return ret;
1142 }
1143 
1144 static int venus_session_end(struct venus_inst *inst)
1145 {
1146 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1147 	struct device *dev = hdev->core->dev;
1148 
1149 	if (venus_fw_coverage) {
1150 		if (venus_sys_set_coverage(hdev, venus_fw_coverage))
1151 			dev_warn(dev, "fw coverage msg ON failed\n");
1152 	}
1153 
1154 	return venus_session_cmd(inst, HFI_CMD_SYS_SESSION_END);
1155 }
1156 
1157 static int venus_session_abort(struct venus_inst *inst)
1158 {
1159 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1160 
1161 	venus_flush_debug_queue(hdev);
1162 
1163 	return venus_session_cmd(inst, HFI_CMD_SYS_SESSION_ABORT);
1164 }
1165 
1166 static int venus_session_flush(struct venus_inst *inst, u32 flush_mode)
1167 {
1168 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1169 	struct hfi_session_flush_pkt pkt;
1170 	int ret;
1171 
1172 	ret = pkt_session_flush(&pkt, inst, flush_mode);
1173 	if (ret)
1174 		return ret;
1175 
1176 	return venus_iface_cmdq_write(hdev, &pkt);
1177 }
1178 
1179 static int venus_session_start(struct venus_inst *inst)
1180 {
1181 	return venus_session_cmd(inst, HFI_CMD_SESSION_START);
1182 }
1183 
1184 static int venus_session_stop(struct venus_inst *inst)
1185 {
1186 	return venus_session_cmd(inst, HFI_CMD_SESSION_STOP);
1187 }
1188 
1189 static int venus_session_continue(struct venus_inst *inst)
1190 {
1191 	return venus_session_cmd(inst, HFI_CMD_SESSION_CONTINUE);
1192 }
1193 
1194 static int venus_session_etb(struct venus_inst *inst,
1195 			     struct hfi_frame_data *in_frame)
1196 {
1197 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1198 	u32 session_type = inst->session_type;
1199 	int ret;
1200 
1201 	if (session_type == VIDC_SESSION_TYPE_DEC) {
1202 		struct hfi_session_empty_buffer_compressed_pkt pkt;
1203 
1204 		ret = pkt_session_etb_decoder(&pkt, inst, in_frame);
1205 		if (ret)
1206 			return ret;
1207 
1208 		ret = venus_iface_cmdq_write(hdev, &pkt);
1209 	} else if (session_type == VIDC_SESSION_TYPE_ENC) {
1210 		struct hfi_session_empty_buffer_uncompressed_plane0_pkt pkt;
1211 
1212 		ret = pkt_session_etb_encoder(&pkt, inst, in_frame);
1213 		if (ret)
1214 			return ret;
1215 
1216 		ret = venus_iface_cmdq_write(hdev, &pkt);
1217 	} else {
1218 		ret = -EINVAL;
1219 	}
1220 
1221 	return ret;
1222 }
1223 
1224 static int venus_session_ftb(struct venus_inst *inst,
1225 			     struct hfi_frame_data *out_frame)
1226 {
1227 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1228 	struct hfi_session_fill_buffer_pkt pkt;
1229 	int ret;
1230 
1231 	ret = pkt_session_ftb(&pkt, inst, out_frame);
1232 	if (ret)
1233 		return ret;
1234 
1235 	return venus_iface_cmdq_write(hdev, &pkt);
1236 }
1237 
1238 static int venus_session_set_buffers(struct venus_inst *inst,
1239 				     struct hfi_buffer_desc *bd)
1240 {
1241 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1242 	struct hfi_session_set_buffers_pkt *pkt;
1243 	u8 packet[IFACEQ_VAR_LARGE_PKT_SIZE];
1244 	int ret;
1245 
1246 	if (bd->buffer_type == HFI_BUFFER_INPUT)
1247 		return 0;
1248 
1249 	pkt = (struct hfi_session_set_buffers_pkt *)packet;
1250 
1251 	ret = pkt_session_set_buffers(pkt, inst, bd);
1252 	if (ret)
1253 		return ret;
1254 
1255 	return venus_iface_cmdq_write(hdev, pkt);
1256 }
1257 
1258 static int venus_session_unset_buffers(struct venus_inst *inst,
1259 				       struct hfi_buffer_desc *bd)
1260 {
1261 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1262 	struct hfi_session_release_buffer_pkt *pkt;
1263 	u8 packet[IFACEQ_VAR_LARGE_PKT_SIZE];
1264 	int ret;
1265 
1266 	if (bd->buffer_type == HFI_BUFFER_INPUT)
1267 		return 0;
1268 
1269 	pkt = (struct hfi_session_release_buffer_pkt *)packet;
1270 
1271 	ret = pkt_session_unset_buffers(pkt, inst, bd);
1272 	if (ret)
1273 		return ret;
1274 
1275 	return venus_iface_cmdq_write(hdev, pkt);
1276 }
1277 
1278 static int venus_session_load_res(struct venus_inst *inst)
1279 {
1280 	return venus_session_cmd(inst, HFI_CMD_SESSION_LOAD_RESOURCES);
1281 }
1282 
1283 static int venus_session_release_res(struct venus_inst *inst)
1284 {
1285 	return venus_session_cmd(inst, HFI_CMD_SESSION_RELEASE_RESOURCES);
1286 }
1287 
1288 static int venus_session_parse_seq_hdr(struct venus_inst *inst, u32 seq_hdr,
1289 				       u32 seq_hdr_len)
1290 {
1291 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1292 	struct hfi_session_parse_sequence_header_pkt *pkt;
1293 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
1294 	int ret;
1295 
1296 	pkt = (struct hfi_session_parse_sequence_header_pkt *)packet;
1297 
1298 	ret = pkt_session_parse_seq_header(pkt, inst, seq_hdr, seq_hdr_len);
1299 	if (ret)
1300 		return ret;
1301 
1302 	ret = venus_iface_cmdq_write(hdev, pkt);
1303 	if (ret)
1304 		return ret;
1305 
1306 	return 0;
1307 }
1308 
1309 static int venus_session_get_seq_hdr(struct venus_inst *inst, u32 seq_hdr,
1310 				     u32 seq_hdr_len)
1311 {
1312 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1313 	struct hfi_session_get_sequence_header_pkt *pkt;
1314 	u8 packet[IFACEQ_VAR_SMALL_PKT_SIZE];
1315 	int ret;
1316 
1317 	pkt = (struct hfi_session_get_sequence_header_pkt *)packet;
1318 
1319 	ret = pkt_session_get_seq_hdr(pkt, inst, seq_hdr, seq_hdr_len);
1320 	if (ret)
1321 		return ret;
1322 
1323 	return venus_iface_cmdq_write(hdev, pkt);
1324 }
1325 
1326 static int venus_session_set_property(struct venus_inst *inst, u32 ptype,
1327 				      void *pdata)
1328 {
1329 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1330 	struct hfi_session_set_property_pkt *pkt;
1331 	u8 packet[IFACEQ_VAR_LARGE_PKT_SIZE];
1332 	int ret;
1333 
1334 	pkt = (struct hfi_session_set_property_pkt *)packet;
1335 
1336 	ret = pkt_session_set_property(pkt, inst, ptype, pdata);
1337 	if (ret == -ENOTSUPP)
1338 		return 0;
1339 	if (ret)
1340 		return ret;
1341 
1342 	return venus_iface_cmdq_write(hdev, pkt);
1343 }
1344 
1345 static int venus_session_get_property(struct venus_inst *inst, u32 ptype)
1346 {
1347 	struct venus_hfi_device *hdev = to_hfi_priv(inst->core);
1348 	struct hfi_session_get_property_pkt pkt;
1349 	int ret;
1350 
1351 	ret = pkt_session_get_property(&pkt, inst, ptype);
1352 	if (ret)
1353 		return ret;
1354 
1355 	return venus_iface_cmdq_write(hdev, &pkt);
1356 }
1357 
1358 static int venus_resume(struct venus_core *core)
1359 {
1360 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1361 	int ret = 0;
1362 
1363 	mutex_lock(&hdev->lock);
1364 
1365 	if (!hdev->suspended)
1366 		goto unlock;
1367 
1368 	ret = venus_power_on(hdev);
1369 
1370 unlock:
1371 	if (!ret)
1372 		hdev->suspended = false;
1373 
1374 	mutex_unlock(&hdev->lock);
1375 
1376 	return ret;
1377 }
1378 
1379 static int venus_suspend_1xx(struct venus_core *core)
1380 {
1381 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1382 	struct device *dev = core->dev;
1383 	u32 ctrl_status;
1384 	int ret;
1385 
1386 	if (!hdev->power_enabled || hdev->suspended)
1387 		return 0;
1388 
1389 	mutex_lock(&hdev->lock);
1390 	ret = venus_is_valid_state(hdev);
1391 	mutex_unlock(&hdev->lock);
1392 
1393 	if (!ret) {
1394 		dev_err(dev, "bad state, cannot suspend\n");
1395 		return -EINVAL;
1396 	}
1397 
1398 	ret = venus_prepare_power_collapse(hdev, true);
1399 	if (ret) {
1400 		dev_err(dev, "prepare for power collapse fail (%d)\n", ret);
1401 		return ret;
1402 	}
1403 
1404 	mutex_lock(&hdev->lock);
1405 
1406 	if (hdev->last_packet_type != HFI_CMD_SYS_PC_PREP) {
1407 		mutex_unlock(&hdev->lock);
1408 		return -EINVAL;
1409 	}
1410 
1411 	ret = venus_are_queues_empty(hdev);
1412 	if (ret < 0 || !ret) {
1413 		mutex_unlock(&hdev->lock);
1414 		return -EINVAL;
1415 	}
1416 
1417 	ctrl_status = venus_readl(hdev, CPU_CS_SCIACMDARG0);
1418 	if (!(ctrl_status & CPU_CS_SCIACMDARG0_PC_READY)) {
1419 		mutex_unlock(&hdev->lock);
1420 		return -EINVAL;
1421 	}
1422 
1423 	ret = venus_power_off(hdev);
1424 	if (ret) {
1425 		mutex_unlock(&hdev->lock);
1426 		return ret;
1427 	}
1428 
1429 	hdev->suspended = true;
1430 
1431 	mutex_unlock(&hdev->lock);
1432 
1433 	return 0;
1434 }
1435 
1436 static bool venus_cpu_and_video_core_idle(struct venus_hfi_device *hdev)
1437 {
1438 	u32 ctrl_status, cpu_status;
1439 
1440 	cpu_status = venus_readl(hdev, WRAPPER_CPU_STATUS);
1441 	ctrl_status = venus_readl(hdev, CPU_CS_SCIACMDARG0);
1442 
1443 	if (cpu_status & WRAPPER_CPU_STATUS_WFI &&
1444 	    ctrl_status & CPU_CS_SCIACMDARG0_INIT_IDLE_MSG_MASK)
1445 		return true;
1446 
1447 	return false;
1448 }
1449 
1450 static bool venus_cpu_idle_and_pc_ready(struct venus_hfi_device *hdev)
1451 {
1452 	u32 ctrl_status, cpu_status;
1453 
1454 	cpu_status = venus_readl(hdev, WRAPPER_CPU_STATUS);
1455 	ctrl_status = venus_readl(hdev, CPU_CS_SCIACMDARG0);
1456 
1457 	if (cpu_status & WRAPPER_CPU_STATUS_WFI &&
1458 	    ctrl_status & CPU_CS_SCIACMDARG0_PC_READY)
1459 		return true;
1460 
1461 	return false;
1462 }
1463 
1464 static int venus_suspend_3xx(struct venus_core *core)
1465 {
1466 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1467 	struct device *dev = core->dev;
1468 	u32 ctrl_status;
1469 	bool val;
1470 	int ret;
1471 
1472 	if (!hdev->power_enabled || hdev->suspended)
1473 		return 0;
1474 
1475 	mutex_lock(&hdev->lock);
1476 	ret = venus_is_valid_state(hdev);
1477 	mutex_unlock(&hdev->lock);
1478 
1479 	if (!ret) {
1480 		dev_err(dev, "bad state, cannot suspend\n");
1481 		return -EINVAL;
1482 	}
1483 
1484 	ctrl_status = venus_readl(hdev, CPU_CS_SCIACMDARG0);
1485 	if (ctrl_status & CPU_CS_SCIACMDARG0_PC_READY)
1486 		goto power_off;
1487 
1488 	/*
1489 	 * Power collapse sequence for Venus 3xx and 4xx versions:
1490 	 * 1. Check for ARM9 and video core to be idle by checking WFI bit
1491 	 *    (bit 0) in CPU status register and by checking Idle (bit 30) in
1492 	 *    Control status register for video core.
1493 	 * 2. Send a command to prepare for power collapse.
1494 	 * 3. Check for WFI and PC_READY bits.
1495 	 */
1496 	ret = readx_poll_timeout(venus_cpu_and_video_core_idle, hdev, val, val,
1497 				 1500, 100 * 1500);
1498 	if (ret)
1499 		return ret;
1500 
1501 	ret = venus_prepare_power_collapse(hdev, false);
1502 	if (ret) {
1503 		dev_err(dev, "prepare for power collapse fail (%d)\n", ret);
1504 		return ret;
1505 	}
1506 
1507 	ret = readx_poll_timeout(venus_cpu_idle_and_pc_ready, hdev, val, val,
1508 				 1500, 100 * 1500);
1509 	if (ret)
1510 		return ret;
1511 
1512 power_off:
1513 	mutex_lock(&hdev->lock);
1514 
1515 	ret = venus_power_off(hdev);
1516 	if (ret) {
1517 		dev_err(dev, "venus_power_off (%d)\n", ret);
1518 		mutex_unlock(&hdev->lock);
1519 		return ret;
1520 	}
1521 
1522 	hdev->suspended = true;
1523 
1524 	mutex_unlock(&hdev->lock);
1525 
1526 	return 0;
1527 }
1528 
1529 static int venus_suspend(struct venus_core *core)
1530 {
1531 	if (IS_V3(core) || IS_V4(core))
1532 		return venus_suspend_3xx(core);
1533 
1534 	return venus_suspend_1xx(core);
1535 }
1536 
1537 static const struct hfi_ops venus_hfi_ops = {
1538 	.core_init			= venus_core_init,
1539 	.core_deinit			= venus_core_deinit,
1540 	.core_ping			= venus_core_ping,
1541 	.core_trigger_ssr		= venus_core_trigger_ssr,
1542 
1543 	.session_init			= venus_session_init,
1544 	.session_end			= venus_session_end,
1545 	.session_abort			= venus_session_abort,
1546 	.session_flush			= venus_session_flush,
1547 	.session_start			= venus_session_start,
1548 	.session_stop			= venus_session_stop,
1549 	.session_continue		= venus_session_continue,
1550 	.session_etb			= venus_session_etb,
1551 	.session_ftb			= venus_session_ftb,
1552 	.session_set_buffers		= venus_session_set_buffers,
1553 	.session_unset_buffers		= venus_session_unset_buffers,
1554 	.session_load_res		= venus_session_load_res,
1555 	.session_release_res		= venus_session_release_res,
1556 	.session_parse_seq_hdr		= venus_session_parse_seq_hdr,
1557 	.session_get_seq_hdr		= venus_session_get_seq_hdr,
1558 	.session_set_property		= venus_session_set_property,
1559 	.session_get_property		= venus_session_get_property,
1560 
1561 	.resume				= venus_resume,
1562 	.suspend			= venus_suspend,
1563 
1564 	.isr				= venus_isr,
1565 	.isr_thread			= venus_isr_thread,
1566 };
1567 
1568 void venus_hfi_destroy(struct venus_core *core)
1569 {
1570 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1571 
1572 	venus_interface_queues_release(hdev);
1573 	mutex_destroy(&hdev->lock);
1574 	kfree(hdev);
1575 	core->priv = NULL;
1576 	core->ops = NULL;
1577 }
1578 
1579 int venus_hfi_create(struct venus_core *core)
1580 {
1581 	struct venus_hfi_device *hdev;
1582 	int ret;
1583 
1584 	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
1585 	if (!hdev)
1586 		return -ENOMEM;
1587 
1588 	mutex_init(&hdev->lock);
1589 
1590 	hdev->core = core;
1591 	hdev->suspended = true;
1592 	core->priv = hdev;
1593 	core->ops = &venus_hfi_ops;
1594 	core->core_caps = ENC_ROTATION_CAPABILITY | ENC_SCALING_CAPABILITY |
1595 			  ENC_DEINTERLACE_CAPABILITY |
1596 			  DEC_MULTI_STREAM_CAPABILITY;
1597 
1598 	ret = venus_interface_queues_init(hdev);
1599 	if (ret)
1600 		goto err_kfree;
1601 
1602 	return 0;
1603 
1604 err_kfree:
1605 	kfree(hdev);
1606 	core->priv = NULL;
1607 	core->ops = NULL;
1608 	return ret;
1609 }
1610 
1611 void venus_hfi_queues_reinit(struct venus_core *core)
1612 {
1613 	struct venus_hfi_device *hdev = to_hfi_priv(core);
1614 	struct hfi_queue_table_header *tbl_hdr;
1615 	struct iface_queue *queue;
1616 	struct hfi_sfr *sfr;
1617 	unsigned int i;
1618 
1619 	mutex_lock(&hdev->lock);
1620 
1621 	for (i = 0; i < IFACEQ_NUM; i++) {
1622 		queue = &hdev->queues[i];
1623 		queue->qhdr =
1624 			IFACEQ_GET_QHDR_START_ADDR(hdev->ifaceq_table.kva, i);
1625 
1626 		venus_set_qhdr_defaults(queue->qhdr);
1627 
1628 		queue->qhdr->start_addr = queue->qmem.da;
1629 
1630 		if (i == IFACEQ_CMD_IDX)
1631 			queue->qhdr->type |= HFI_HOST_TO_CTRL_CMD_Q;
1632 		else if (i == IFACEQ_MSG_IDX)
1633 			queue->qhdr->type |= HFI_CTRL_TO_HOST_MSG_Q;
1634 		else if (i == IFACEQ_DBG_IDX)
1635 			queue->qhdr->type |= HFI_CTRL_TO_HOST_DBG_Q;
1636 	}
1637 
1638 	tbl_hdr = hdev->ifaceq_table.kva;
1639 	tbl_hdr->version = 0;
1640 	tbl_hdr->size = IFACEQ_TABLE_SIZE;
1641 	tbl_hdr->qhdr0_offset = sizeof(struct hfi_queue_table_header);
1642 	tbl_hdr->qhdr_size = sizeof(struct hfi_queue_header);
1643 	tbl_hdr->num_q = IFACEQ_NUM;
1644 	tbl_hdr->num_active_q = IFACEQ_NUM;
1645 
1646 	/*
1647 	 * Set receive request to zero on debug queue as there is no
1648 	 * need of interrupt from video hardware for debug messages
1649 	 */
1650 	queue = &hdev->queues[IFACEQ_DBG_IDX];
1651 	queue->qhdr->rx_req = 0;
1652 
1653 	sfr = hdev->sfr.kva;
1654 	sfr->buf_size = ALIGNED_SFR_SIZE;
1655 
1656 	/* ensure table and queue header structs are settled in memory */
1657 	wmb();
1658 
1659 	mutex_unlock(&hdev->lock);
1660 }
1661