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