• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  *
5  * author:
6  *	Alpha Lin, alpha.lin@rock-chips.com
7  *	Randy Li, randy.li@rock-chips.com
8  *	Ding Wei, leo.ding@rock-chips.com
9  *
10  */
11 #include <asm/cacheflush.h>
12 #include <linux/delay.h>
13 #include <linux/iopoll.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/of_platform.h>
18 #include <linux/slab.h>
19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/regmap.h>
22 #include <linux/proc_fs.h>
23 #include <linux/nospec.h>
24 #include <soc/rockchip/pm_domains.h>
25 
26 #include "mpp_debug.h"
27 #include "mpp_common.h"
28 #include "mpp_iommu.h"
29 
30 #define VEPU2_DRIVER_NAME		"mpp_vepu2"
31 
32 #define	VEPU2_SESSION_MAX_BUFFERS		20
33 /* The maximum registers number of all the version */
34 #define VEPU2_REG_NUM				184
35 #define VEPU2_REG_HW_ID_INDEX		-1 /* INVALID */
36 #define VEPU2_REG_START_INDEX			0
37 #define VEPU2_REG_END_INDEX			183
38 
39 #define VEPU2_REG_ENC_EN			0x19c
40 #define VEPU2_REG_ENC_EN_INDEX			(103)
41 #define VEPU2_ENC_START				BIT(0)
42 
43 #define VEPU2_GET_FORMAT(x)			(((x) >> 4) & 0x3)
44 #define VEPU2_FORMAT_MASK			(0x30)
45 #define VEPU2_GET_WIDTH(x)			(((x >> 8) & 0x1ff) << 4)
46 #define VEPU2_GET_HEIGHT(x)			(((x >> 20) & 0x1ff) << 4)
47 
48 #define VEPU2_FMT_RESERVED			(0)
49 #define VEPU2_FMT_VP8E				(1)
50 #define VEPU2_FMT_JPEGE				(2)
51 #define VEPU2_FMT_H264E				(3)
52 
53 #define VEPU2_REG_MB_CTRL			0x1a0
54 #define VEPU2_REG_MB_CTRL_INDEX			(104)
55 
56 #define VEPU2_REG_INT				0x1b4
57 #define VEPU2_REG_INT_INDEX			(109)
58 #define VEPU2_MV_SAD_WR_EN			BIT(24)
59 #define VEPU2_ROCON_WRITE_DIS			BIT(20)
60 #define VEPU2_INT_SLICE_EN			BIT(16)
61 #define VEPU2_CLOCK_GATE_EN			BIT(12)
62 #define VEPU2_INT_TIMEOUT_EN			BIT(10)
63 #define VEPU2_INT_CLEAR				BIT(9)
64 #define VEPU2_IRQ_DIS				BIT(8)
65 #define VEPU2_INT_TIMEOUT			BIT(6)
66 #define VEPU2_INT_BUF_FULL			BIT(5)
67 #define VEPU2_INT_BUS_ERROR			BIT(4)
68 #define VEPU2_INT_SLICE				BIT(2)
69 #define VEPU2_INT_RDY				BIT(1)
70 #define VEPU2_INT_RAW				BIT(0)
71 
72 #define RKVPUE2_REG_DMV_4P_1P(i)		(0x1e0 + ((i) << 4))
73 #define RKVPUE2_REG_DMV_4P_1P_INDEX(i)		(120 + (i))
74 
75 #define VEPU2_REG_CLR_CACHE_BASE		0xc10
76 
77 #define to_vepu_task(task)		\
78 		container_of(task, struct vepu_task, mpp_task)
79 #define to_vepu_dev(dev)		\
80 		container_of(dev, struct vepu_dev, mpp)
81 
82 struct vepu_task {
83 	struct mpp_task mpp_task;
84 
85 	enum MPP_CLOCK_MODE clk_mode;
86 	u32 reg[VEPU2_REG_NUM];
87 
88 	struct reg_offset_info off_inf;
89 	u32 irq_status;
90 	/* req for current task */
91 	u32 w_req_cnt;
92 	struct mpp_request w_reqs[MPP_MAX_MSG_NUM];
93 	u32 r_req_cnt;
94 	struct mpp_request r_reqs[MPP_MAX_MSG_NUM];
95 	/* image info */
96 	u32 width;
97 	u32 height;
98 	u32 pixels;
99 };
100 
101 struct vepu_session_priv {
102 	struct rw_semaphore rw_sem;
103 	/* codec info from user */
104 	struct {
105 		/* show mode */
106 		u32 flag;
107 		/* item data */
108 		u64 val;
109 	} codec_info[ENC_INFO_BUTT];
110 };
111 
112 struct vepu_dev {
113 	struct mpp_dev mpp;
114 
115 	struct mpp_clk_info aclk_info;
116 	struct mpp_clk_info hclk_info;
117 	u32 default_max_load;
118 #ifdef CONFIG_ROCKCHIP_MPP_PROC_FS
119 	struct proc_dir_entry *procfs;
120 #endif
121 	struct reset_control *rst_a;
122 	struct reset_control *rst_h;
123 	/* for ccu(central control unit) */
124 	struct vepu_ccu *ccu;
125 	struct list_head core_link;
126 	bool disable_work;
127 };
128 
129 struct vepu_ccu {
130 	u32 core_num;
131 	/* lock for core attach */
132 	struct mutex lock;
133 	struct list_head core_list;
134 	struct mpp_dev *main_core;
135 };
136 
137 static struct mpp_hw_info vepu_v2_hw_info = {
138 	.reg_num = VEPU2_REG_NUM,
139 	.reg_id = VEPU2_REG_HW_ID_INDEX,
140 	.reg_start = VEPU2_REG_START_INDEX,
141 	.reg_end = VEPU2_REG_END_INDEX,
142 	.reg_en = VEPU2_REG_ENC_EN_INDEX,
143 };
144 
145 /*
146  * file handle translate information
147  */
148 static const u16 trans_tbl_default[] = {
149 	48, 49, 50, 56, 57, 63, 64, 77, 78, 81
150 };
151 
152 static const u16 trans_tbl_vp8e[] = {
153 	27, 44, 45, 48, 49, 50, 56, 57, 63, 64,
154 	76, 77, 78, 80, 81, 106, 108,
155 };
156 
157 static struct mpp_trans_info trans_rk_vepu2[] = {
158 	[VEPU2_FMT_RESERVED] = {
159 		.count = 0,
160 		.table = NULL,
161 	},
162 	[VEPU2_FMT_VP8E] = {
163 		.count = ARRAY_SIZE(trans_tbl_vp8e),
164 		.table = trans_tbl_vp8e,
165 	},
166 	[VEPU2_FMT_JPEGE] = {
167 		.count = ARRAY_SIZE(trans_tbl_default),
168 		.table = trans_tbl_default,
169 	},
170 	[VEPU2_FMT_H264E] = {
171 		.count = ARRAY_SIZE(trans_tbl_default),
172 		.table = trans_tbl_default,
173 	},
174 };
175 
vepu_process_reg_fd(struct mpp_session * session,struct vepu_task * task,struct mpp_task_msgs * msgs)176 static int vepu_process_reg_fd(struct mpp_session *session,
177 			       struct vepu_task *task,
178 			       struct mpp_task_msgs *msgs)
179 {
180 	int ret;
181 	int fmt = VEPU2_GET_FORMAT(task->reg[VEPU2_REG_ENC_EN_INDEX]);
182 
183 	ret = mpp_translate_reg_address(session, &task->mpp_task,
184 					fmt, task->reg, &task->off_inf);
185 	if (ret)
186 		return ret;
187 
188 	mpp_translate_reg_offset_info(&task->mpp_task,
189 				      &task->off_inf, task->reg);
190 
191 	return 0;
192 }
193 
vepu_extract_task_msg(struct vepu_task * task,struct mpp_task_msgs * msgs)194 static int vepu_extract_task_msg(struct vepu_task *task,
195 				 struct mpp_task_msgs *msgs)
196 {
197 	u32 i;
198 	int ret;
199 	struct mpp_request *req;
200 	struct mpp_hw_info *hw_info = task->mpp_task.hw_info;
201 
202 	for (i = 0; i < msgs->req_cnt; i++) {
203 		u32 off_s, off_e;
204 
205 		req = &msgs->reqs[i];
206 		if (!req->size)
207 			continue;
208 
209 		switch (req->cmd) {
210 		case MPP_CMD_SET_REG_WRITE: {
211 			off_s = hw_info->reg_start * sizeof(u32);
212 			off_e = hw_info->reg_end * sizeof(u32);
213 			ret = mpp_check_req(req, 0, sizeof(task->reg),
214 					    off_s, off_e);
215 			if (ret)
216 				continue;
217 			if (copy_from_user((u8 *)task->reg + req->offset,
218 					   req->data, req->size)) {
219 				mpp_err("copy_from_user reg failed\n");
220 				return -EIO;
221 			}
222 			memcpy(&task->w_reqs[task->w_req_cnt++],
223 			       req, sizeof(*req));
224 		} break;
225 		case MPP_CMD_SET_REG_READ: {
226 			off_s = hw_info->reg_start * sizeof(u32);
227 			off_e = hw_info->reg_end * sizeof(u32);
228 			ret = mpp_check_req(req, 0, sizeof(task->reg),
229 					    off_s, off_e);
230 			if (ret)
231 				continue;
232 			memcpy(&task->r_reqs[task->r_req_cnt++],
233 			       req, sizeof(*req));
234 		} break;
235 		case MPP_CMD_SET_REG_ADDR_OFFSET: {
236 			mpp_extract_reg_offset_info(&task->off_inf, req);
237 		} break;
238 		default:
239 			break;
240 		}
241 	}
242 	mpp_debug(DEBUG_TASK_INFO, "w_req_cnt %d, r_req_cnt %d\n",
243 		  task->w_req_cnt, task->r_req_cnt);
244 
245 	return 0;
246 }
247 
vepu_alloc_task(struct mpp_session * session,struct mpp_task_msgs * msgs)248 static void *vepu_alloc_task(struct mpp_session *session,
249 			     struct mpp_task_msgs *msgs)
250 {
251 	int ret;
252 	struct mpp_task *mpp_task = NULL;
253 	struct vepu_task *task = NULL;
254 	struct mpp_dev *mpp = session->mpp;
255 
256 	mpp_debug_enter();
257 
258 	task = kzalloc(sizeof(*task), GFP_KERNEL);
259 	if (!task)
260 		return NULL;
261 
262 	mpp_task = &task->mpp_task;
263 	mpp_task_init(session, mpp_task);
264 	mpp_task->hw_info = mpp->var->hw_info;
265 	mpp_task->reg = task->reg;
266 	/* extract reqs for current task */
267 	ret = vepu_extract_task_msg(task, msgs);
268 	if (ret)
269 		goto fail;
270 	/* process fd in register */
271 	if (!(msgs->flags & MPP_FLAGS_REG_FD_NO_TRANS)) {
272 		ret = vepu_process_reg_fd(session, task, msgs);
273 		if (ret)
274 			goto fail;
275 	}
276 	task->clk_mode = CLK_MODE_NORMAL;
277 	/* get resolution info */
278 	task->width = VEPU2_GET_WIDTH(task->reg[VEPU2_REG_ENC_EN_INDEX]);
279 	task->height = VEPU2_GET_HEIGHT(task->reg[VEPU2_REG_ENC_EN_INDEX]);
280 	task->pixels = task->width * task->height;
281 	mpp_debug(DEBUG_TASK_INFO, "width=%d, height=%d\n", task->width, task->height);
282 
283 	mpp_debug_leave();
284 
285 	return mpp_task;
286 
287 fail:
288 	mpp_task_dump_mem_region(mpp, mpp_task);
289 	mpp_task_dump_reg(mpp, mpp_task);
290 	mpp_task_finalize(session, mpp_task);
291 	kfree(task);
292 	return NULL;
293 }
294 
vepu_prepare(struct mpp_dev * mpp,struct mpp_task * mpp_task)295 static void *vepu_prepare(struct mpp_dev *mpp, struct mpp_task *mpp_task)
296 {
297 	struct mpp_taskqueue *queue = mpp->queue;
298 	unsigned long flags;
299 	s32 core_id;
300 
301 	spin_lock_irqsave(&queue->running_lock, flags);
302 
303 	core_id = find_first_bit(&queue->core_idle, queue->core_count);
304 
305 	if (core_id >= queue->core_count) {
306 		mpp_task = NULL;
307 		mpp_dbg_core("core %d all busy %lx\n", core_id, queue->core_idle);
308 	} else {
309 		unsigned long core_idle = queue->core_idle;
310 
311 		clear_bit(core_id, &queue->core_idle);
312 		mpp_task->mpp = queue->cores[core_id];
313 		mpp_task->core_id = core_id;
314 
315 		mpp_dbg_core("core %d set idle %lx -> %lx\n", core_id,
316 			     core_idle, queue->core_idle);
317 	}
318 
319 	spin_unlock_irqrestore(&queue->running_lock, flags);
320 
321 	return mpp_task;
322 }
323 
vepu_run(struct mpp_dev * mpp,struct mpp_task * mpp_task)324 static int vepu_run(struct mpp_dev *mpp,
325 		    struct mpp_task *mpp_task)
326 {
327 	u32 i;
328 	u32 reg_en;
329 	struct vepu_task *task = to_vepu_task(mpp_task);
330 
331 	mpp_debug_enter();
332 
333 	/* clear cache */
334 	mpp_write_relaxed(mpp, VEPU2_REG_CLR_CACHE_BASE, 1);
335 
336 	reg_en = mpp_task->hw_info->reg_en;
337 	/* First, flush correct encoder format */
338 	mpp_write_relaxed(mpp, VEPU2_REG_ENC_EN,
339 			  task->reg[reg_en] & VEPU2_FORMAT_MASK);
340 	/* Second, flush others register */
341 	for (i = 0; i < task->w_req_cnt; i++) {
342 		struct mpp_request *req = &task->w_reqs[i];
343 		int s = req->offset / sizeof(u32);
344 		int e = s + req->size / sizeof(u32);
345 
346 		mpp_write_req(mpp, task->reg, s, e, reg_en);
347 	}
348 	/* init current task */
349 	mpp->cur_task = mpp_task;
350 	/* Last, flush the registers */
351 	wmb();
352 	mpp_write(mpp, VEPU2_REG_ENC_EN,
353 		  task->reg[reg_en] | VEPU2_ENC_START);
354 
355 	mpp_debug_leave();
356 
357 	return 0;
358 }
359 
vepu_irq(struct mpp_dev * mpp)360 static int vepu_irq(struct mpp_dev *mpp)
361 {
362 	mpp->irq_status = mpp_read(mpp, VEPU2_REG_INT);
363 	if (!(mpp->irq_status & VEPU2_INT_RAW))
364 		return IRQ_NONE;
365 
366 	mpp_write(mpp, VEPU2_REG_INT, 0);
367 
368 	return IRQ_WAKE_THREAD;
369 }
370 
vepu_isr(struct mpp_dev * mpp)371 static int vepu_isr(struct mpp_dev *mpp)
372 {
373 	u32 err_mask;
374 	struct vepu_task *task = NULL;
375 	struct mpp_task *mpp_task = mpp->cur_task;
376 	struct mpp_taskqueue *queue = mpp->queue;
377 	unsigned long core_idle;
378 
379 	/* FIXME use a spin lock here */
380 	if (!mpp_task) {
381 		dev_err(mpp->dev, "no current task\n");
382 		return IRQ_HANDLED;
383 	}
384 	mpp_time_diff(mpp_task);
385 	mpp->cur_task = NULL;
386 	task = to_vepu_task(mpp_task);
387 	task->irq_status = mpp->irq_status;
388 	mpp_debug(DEBUG_IRQ_STATUS, "irq_status: %08x\n",
389 		  task->irq_status);
390 
391 	err_mask = VEPU2_INT_TIMEOUT
392 		| VEPU2_INT_BUF_FULL
393 		| VEPU2_INT_BUS_ERROR;
394 
395 	if (err_mask & task->irq_status)
396 		atomic_inc(&mpp->reset_request);
397 
398 	mpp_task_finish(mpp_task->session, mpp_task);
399 
400 	core_idle = queue->core_idle;
401 	set_bit(mpp->core_id, &queue->core_idle);
402 
403 	mpp_dbg_core("core %d isr idle %lx -> %lx\n", mpp->core_id, core_idle,
404 		     queue->core_idle);
405 
406 	mpp_debug_leave();
407 
408 	return IRQ_HANDLED;
409 }
410 
vepu_finish(struct mpp_dev * mpp,struct mpp_task * mpp_task)411 static int vepu_finish(struct mpp_dev *mpp,
412 		       struct mpp_task *mpp_task)
413 {
414 	u32 i;
415 	u32 s, e;
416 	struct mpp_request *req;
417 	struct vepu_task *task = to_vepu_task(mpp_task);
418 
419 	mpp_debug_enter();
420 
421 	/* read register after running */
422 	for (i = 0; i < task->r_req_cnt; i++) {
423 		req = &task->r_reqs[i];
424 		s = req->offset / sizeof(u32);
425 		e = s + req->size / sizeof(u32);
426 		mpp_read_req(mpp, task->reg, s, e);
427 	}
428 	/* revert hack for irq status */
429 	task->reg[VEPU2_REG_INT_INDEX] = task->irq_status;
430 
431 	mpp_debug_leave();
432 
433 	return 0;
434 }
435 
vepu_result(struct mpp_dev * mpp,struct mpp_task * mpp_task,struct mpp_task_msgs * msgs)436 static int vepu_result(struct mpp_dev *mpp,
437 		       struct mpp_task *mpp_task,
438 		       struct mpp_task_msgs *msgs)
439 {
440 	u32 i;
441 	struct mpp_request *req;
442 	struct vepu_task *task = to_vepu_task(mpp_task);
443 
444 	/* FIXME may overflow the kernel */
445 	for (i = 0; i < task->r_req_cnt; i++) {
446 		req = &task->r_reqs[i];
447 
448 		if (copy_to_user(req->data,
449 				 (u8 *)task->reg + req->offset,
450 				 req->size)) {
451 			mpp_err("copy_to_user reg fail\n");
452 			return -EIO;
453 		}
454 	}
455 
456 	return 0;
457 }
458 
vepu_free_task(struct mpp_session * session,struct mpp_task * mpp_task)459 static int vepu_free_task(struct mpp_session *session,
460 			  struct mpp_task *mpp_task)
461 {
462 	struct vepu_task *task = to_vepu_task(mpp_task);
463 
464 	mpp_task_finalize(session, mpp_task);
465 	kfree(task);
466 
467 	return 0;
468 }
469 
vepu_control(struct mpp_session * session,struct mpp_request * req)470 static int vepu_control(struct mpp_session *session, struct mpp_request *req)
471 {
472 	switch (req->cmd) {
473 	case MPP_CMD_SEND_CODEC_INFO: {
474 		int i;
475 		int cnt;
476 		struct codec_info_elem elem;
477 		struct vepu_session_priv *priv;
478 
479 		if (!session || !session->priv) {
480 			mpp_err("session info null\n");
481 			return -EINVAL;
482 		}
483 		priv = session->priv;
484 
485 		cnt = req->size / sizeof(elem);
486 		cnt = (cnt > ENC_INFO_BUTT) ? ENC_INFO_BUTT : cnt;
487 		mpp_debug(DEBUG_IOCTL, "codec info count %d\n", cnt);
488 		for (i = 0; i < cnt; i++) {
489 			if (copy_from_user(&elem, req->data + i * sizeof(elem), sizeof(elem))) {
490 				mpp_err("copy_from_user failed\n");
491 				continue;
492 			}
493 			if (elem.type > ENC_INFO_BASE && elem.type < ENC_INFO_BUTT &&
494 			    elem.flag > CODEC_INFO_FLAG_NULL && elem.flag < CODEC_INFO_FLAG_BUTT) {
495 				elem.type = array_index_nospec(elem.type, ENC_INFO_BUTT);
496 				priv->codec_info[elem.type].flag = elem.flag;
497 				priv->codec_info[elem.type].val = elem.data;
498 			} else {
499 				mpp_err("codec info invalid, type %d, flag %d\n",
500 					elem.type, elem.flag);
501 			}
502 		}
503 	} break;
504 	default: {
505 		mpp_err("unknown mpp ioctl cmd %x\n", req->cmd);
506 	} break;
507 	}
508 
509 	return 0;
510 }
511 
vepu_free_session(struct mpp_session * session)512 static int vepu_free_session(struct mpp_session *session)
513 {
514 	if (session && session->priv) {
515 		kfree(session->priv);
516 		session->priv = NULL;
517 	}
518 
519 	return 0;
520 }
521 
vepu_init_session(struct mpp_session * session)522 static int vepu_init_session(struct mpp_session *session)
523 {
524 	struct vepu_session_priv *priv;
525 
526 	if (!session) {
527 		mpp_err("session is null\n");
528 		return -EINVAL;
529 	}
530 
531 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
532 	if (!priv)
533 		return -ENOMEM;
534 
535 	init_rwsem(&priv->rw_sem);
536 	session->priv = priv;
537 
538 	return 0;
539 }
540 
541 #ifdef CONFIG_ROCKCHIP_MPP_PROC_FS
vepu_procfs_remove(struct mpp_dev * mpp)542 static int vepu_procfs_remove(struct mpp_dev *mpp)
543 {
544 	struct vepu_dev *enc = to_vepu_dev(mpp);
545 
546 	if (enc->procfs) {
547 		proc_remove(enc->procfs);
548 		enc->procfs = NULL;
549 	}
550 
551 	return 0;
552 }
553 
vepu_dump_session(struct mpp_session * session,struct seq_file * seq)554 static int vepu_dump_session(struct mpp_session *session, struct seq_file *seq)
555 {
556 	int i;
557 	struct vepu_session_priv *priv = session->priv;
558 
559 	down_read(&priv->rw_sem);
560 	/* item name */
561 	seq_puts(seq, "------------------------------------------------------");
562 	seq_puts(seq, "------------------------------------------------------\n");
563 	seq_printf(seq, "|%8s|", (const char *)"session");
564 	seq_printf(seq, "%8s|", (const char *)"device");
565 	for (i = ENC_INFO_BASE; i < ENC_INFO_BUTT; i++) {
566 		bool show = priv->codec_info[i].flag;
567 
568 		if (show)
569 			seq_printf(seq, "%8s|", enc_info_item_name[i]);
570 	}
571 	seq_puts(seq, "\n");
572 	/* item data*/
573 	seq_printf(seq, "|%8p|", session);
574 	seq_printf(seq, "%8s|", mpp_device_name[session->device_type]);
575 	for (i = ENC_INFO_BASE; i < ENC_INFO_BUTT; i++) {
576 		u32 flag = priv->codec_info[i].flag;
577 
578 		if (!flag)
579 			continue;
580 		if (flag == CODEC_INFO_FLAG_NUMBER) {
581 			u32 data = priv->codec_info[i].val;
582 
583 			seq_printf(seq, "%8d|", data);
584 		} else if (flag == CODEC_INFO_FLAG_STRING) {
585 			const char *name = (const char *)&priv->codec_info[i].val;
586 
587 			seq_printf(seq, "%8s|", name);
588 		} else {
589 			seq_printf(seq, "%8s|", (const char *)"null");
590 		}
591 	}
592 	seq_puts(seq, "\n");
593 	up_read(&priv->rw_sem);
594 
595 	return 0;
596 }
597 
vepu_show_session_info(struct seq_file * seq,void * offset)598 static int vepu_show_session_info(struct seq_file *seq, void *offset)
599 {
600 	struct mpp_session *session = NULL, *n;
601 	struct mpp_dev *mpp = seq->private;
602 
603 	mutex_lock(&mpp->srv->session_lock);
604 	list_for_each_entry_safe(session, n,
605 				 &mpp->srv->session_list,
606 				 session_link) {
607 		if (session->device_type != MPP_DEVICE_VEPU2)
608 			continue;
609 		if (!session->priv)
610 			continue;
611 		if (mpp->dev_ops->dump_session)
612 			mpp->dev_ops->dump_session(session, seq);
613 	}
614 	mutex_unlock(&mpp->srv->session_lock);
615 
616 	return 0;
617 }
618 
vepu_procfs_init(struct mpp_dev * mpp)619 static int vepu_procfs_init(struct mpp_dev *mpp)
620 {
621 	struct vepu_dev *enc = to_vepu_dev(mpp);
622 	char name[32];
623 
624 	if (!mpp->dev || !mpp->dev->of_node || !mpp->dev->of_node->name ||
625 	    !mpp->srv || !mpp->srv->procfs)
626 		return -EINVAL;
627 
628 	snprintf(name, sizeof(name) - 1, "%s%d",
629 		 mpp->dev->of_node->name, mpp->core_id);
630 
631 	enc->procfs = proc_mkdir(name, mpp->srv->procfs);
632 	if (IS_ERR_OR_NULL(enc->procfs)) {
633 		mpp_err("failed on open procfs\n");
634 		enc->procfs = NULL;
635 		return -EIO;
636 	}
637 	mpp_procfs_create_u32("aclk", 0644,
638 			      enc->procfs, &enc->aclk_info.debug_rate_hz);
639 	mpp_procfs_create_u32("session_buffers", 0644,
640 			      enc->procfs, &mpp->session_max_buffers);
641 	/* for show session info */
642 	proc_create_single_data("sessions-info", 0444,
643 				enc->procfs, vepu_show_session_info, mpp);
644 
645 	return 0;
646 }
647 
vepu_procfs_ccu_init(struct mpp_dev * mpp)648 static int vepu_procfs_ccu_init(struct mpp_dev *mpp)
649 {
650 	struct vepu_dev *enc = to_vepu_dev(mpp);
651 
652 	if (!enc->procfs)
653 		goto done;
654 
655 	mpp_procfs_create_u32("disable_work", 0644,
656 			      enc->procfs, &enc->disable_work);
657 done:
658 	return 0;
659 }
660 #else
vepu_procfs_remove(struct mpp_dev * mpp)661 static inline int vepu_procfs_remove(struct mpp_dev *mpp)
662 {
663 	return 0;
664 }
665 
vepu_procfs_init(struct mpp_dev * mpp)666 static inline int vepu_procfs_init(struct mpp_dev *mpp)
667 {
668 	return 0;
669 }
670 
vepu_procfs_ccu_init(struct mpp_dev * mpp)671 static inline int vepu_procfs_ccu_init(struct mpp_dev *mpp)
672 {
673 	return 0;
674 }
675 
vepu_dump_session(struct mpp_session * session,struct seq_file * seq)676 static inline int vepu_dump_session(struct mpp_session *session, struct seq_file *seq)
677 {
678 	return 0;
679 }
680 #endif
681 
vepu_init(struct mpp_dev * mpp)682 static int vepu_init(struct mpp_dev *mpp)
683 {
684 	int ret;
685 	struct vepu_dev *enc = to_vepu_dev(mpp);
686 
687 	mpp->grf_info = &mpp->srv->grf_infos[MPP_DRIVER_VEPU2];
688 
689 	/* Get clock info from dtsi */
690 	ret = mpp_get_clk_info(mpp, &enc->aclk_info, "aclk_vcodec");
691 	if (ret)
692 		mpp_err("failed on clk_get aclk_vcodec\n");
693 	ret = mpp_get_clk_info(mpp, &enc->hclk_info, "hclk_vcodec");
694 	if (ret)
695 		mpp_err("failed on clk_get hclk_vcodec\n");
696 	/* Get normal max workload from dtsi */
697 	of_property_read_u32(mpp->dev->of_node,
698 			     "rockchip,default-max-load", &enc->default_max_load);
699 	/* Set default rates */
700 	mpp_set_clk_info_rate_hz(&enc->aclk_info, CLK_MODE_DEFAULT, 300 * MHZ);
701 
702 	/* Get reset control from dtsi */
703 	enc->rst_a = mpp_reset_control_get(mpp, RST_TYPE_A, "video_a");
704 	if (!enc->rst_a)
705 		mpp_err("No aclk reset resource define\n");
706 	enc->rst_h = mpp_reset_control_get(mpp, RST_TYPE_H, "video_h");
707 	if (!enc->rst_h)
708 		mpp_err("No hclk reset resource define\n");
709 
710 	return 0;
711 }
712 
vepu_clk_on(struct mpp_dev * mpp)713 static int vepu_clk_on(struct mpp_dev *mpp)
714 {
715 	struct vepu_dev *enc = to_vepu_dev(mpp);
716 
717 	mpp_clk_safe_enable(enc->aclk_info.clk);
718 	mpp_clk_safe_enable(enc->hclk_info.clk);
719 
720 	return 0;
721 }
722 
vepu_clk_off(struct mpp_dev * mpp)723 static int vepu_clk_off(struct mpp_dev *mpp)
724 {
725 	struct vepu_dev *enc = to_vepu_dev(mpp);
726 
727 	mpp_clk_safe_disable(enc->aclk_info.clk);
728 	mpp_clk_safe_disable(enc->hclk_info.clk);
729 
730 	return 0;
731 }
732 
vepu_get_freq(struct mpp_dev * mpp,struct mpp_task * mpp_task)733 static int vepu_get_freq(struct mpp_dev *mpp,
734 			 struct mpp_task *mpp_task)
735 {
736 	u32 task_cnt;
737 	u32 workload;
738 	struct mpp_task *loop = NULL, *n;
739 	struct vepu_dev *enc = to_vepu_dev(mpp);
740 	struct vepu_task *task = to_vepu_task(mpp_task);
741 
742 	/* if not set max load, consider not have advanced mode */
743 	if (!enc->default_max_load)
744 		return 0;
745 
746 	task_cnt = 1;
747 	workload = task->pixels;
748 	/* calc workload in pending list */
749 	mutex_lock(&mpp->queue->pending_lock);
750 	list_for_each_entry_safe(loop, n,
751 				 &mpp->queue->pending_list,
752 				 queue_link) {
753 		struct vepu_task *loop_task = to_vepu_task(loop);
754 
755 		task_cnt++;
756 		workload += loop_task->pixels;
757 	}
758 	mutex_unlock(&mpp->queue->pending_lock);
759 
760 	if (workload > enc->default_max_load)
761 		task->clk_mode = CLK_MODE_ADVANCED;
762 
763 	mpp_debug(DEBUG_TASK_INFO, "pending task %d, workload %d, clk_mode=%d\n",
764 		  task_cnt, workload, task->clk_mode);
765 
766 	return 0;
767 }
768 
vepu_set_freq(struct mpp_dev * mpp,struct mpp_task * mpp_task)769 static int vepu_set_freq(struct mpp_dev *mpp,
770 			 struct mpp_task *mpp_task)
771 {
772 	struct vepu_dev *enc = to_vepu_dev(mpp);
773 	struct vepu_task *task = to_vepu_task(mpp_task);
774 
775 	mpp_clk_set_rate(&enc->aclk_info, task->clk_mode);
776 
777 	return 0;
778 }
779 
vepu_reduce_freq(struct mpp_dev * mpp)780 static int vepu_reduce_freq(struct mpp_dev *mpp)
781 {
782 	struct vepu_dev *enc = to_vepu_dev(mpp);
783 
784 	mpp_clk_set_rate(&enc->aclk_info, CLK_MODE_REDUCE);
785 
786 	return 0;
787 }
788 
vepu_reset(struct mpp_dev * mpp)789 static int vepu_reset(struct mpp_dev *mpp)
790 {
791 	struct vepu_dev *enc = to_vepu_dev(mpp);
792 	struct mpp_taskqueue *queue = mpp->queue;
793 
794 	if (enc->rst_a && enc->rst_h) {
795 		/* Don't skip this or iommu won't work after reset */
796 		mpp_pmu_idle_request(mpp, true);
797 		mpp_safe_reset(enc->rst_a);
798 		mpp_safe_reset(enc->rst_h);
799 		udelay(5);
800 		mpp_safe_unreset(enc->rst_a);
801 		mpp_safe_unreset(enc->rst_h);
802 		mpp_pmu_idle_request(mpp, false);
803 	}
804 	mpp_write(mpp, VEPU2_REG_INT, VEPU2_INT_CLEAR);
805 
806 	set_bit(mpp->core_id, &queue->core_idle);
807 	mpp_dbg_core("core %d reset idle %lx\n", mpp->core_id, queue->core_idle);
808 
809 	return 0;
810 }
811 
812 static struct mpp_hw_ops vepu_v2_hw_ops = {
813 	.init = vepu_init,
814 	.clk_on = vepu_clk_on,
815 	.clk_off = vepu_clk_off,
816 	.get_freq = vepu_get_freq,
817 	.set_freq = vepu_set_freq,
818 	.reduce_freq = vepu_reduce_freq,
819 	.reset = vepu_reset,
820 };
821 
822 static struct mpp_dev_ops vepu_v2_dev_ops = {
823 	.alloc_task = vepu_alloc_task,
824 	.run = vepu_run,
825 	.irq = vepu_irq,
826 	.isr = vepu_isr,
827 	.finish = vepu_finish,
828 	.result = vepu_result,
829 	.free_task = vepu_free_task,
830 	.ioctl = vepu_control,
831 	.init_session = vepu_init_session,
832 	.free_session = vepu_free_session,
833 	.dump_session = vepu_dump_session,
834 };
835 
836 static struct mpp_dev_ops vepu_ccu_dev_ops = {
837 	.alloc_task = vepu_alloc_task,
838 	.prepare = vepu_prepare,
839 	.run = vepu_run,
840 	.irq = vepu_irq,
841 	.isr = vepu_isr,
842 	.finish = vepu_finish,
843 	.result = vepu_result,
844 	.free_task = vepu_free_task,
845 	.ioctl = vepu_control,
846 	.init_session = vepu_init_session,
847 	.free_session = vepu_free_session,
848 	.dump_session = vepu_dump_session,
849 };
850 
851 
852 static const struct mpp_dev_var vepu_v2_data = {
853 	.device_type = MPP_DEVICE_VEPU2,
854 	.hw_info = &vepu_v2_hw_info,
855 	.trans_info = trans_rk_vepu2,
856 	.hw_ops = &vepu_v2_hw_ops,
857 	.dev_ops = &vepu_v2_dev_ops,
858 };
859 
860 static const struct mpp_dev_var vepu_ccu_data = {
861 	.device_type = MPP_DEVICE_VEPU2,
862 	.hw_info = &vepu_v2_hw_info,
863 	.trans_info = trans_rk_vepu2,
864 	.hw_ops = &vepu_v2_hw_ops,
865 	.dev_ops = &vepu_ccu_dev_ops,
866 };
867 
868 static const struct of_device_id mpp_vepu2_dt_match[] = {
869 	{
870 		.compatible = "rockchip,vpu-encoder-v2",
871 		.data = &vepu_v2_data,
872 	},
873 #ifdef CONFIG_CPU_RK3588
874 	{
875 		.compatible = "rockchip,vpu-encoder-v2-core",
876 		.data = &vepu_ccu_data,
877 	},
878 	{
879 		.compatible = "rockchip,vpu-encoder-v2-ccu",
880 	},
881 #endif
882 	{},
883 };
884 
vepu_ccu_probe(struct platform_device * pdev)885 static int vepu_ccu_probe(struct platform_device *pdev)
886 {
887 	struct vepu_ccu *ccu;
888 	struct device *dev = &pdev->dev;
889 
890 	ccu = devm_kzalloc(dev, sizeof(*ccu), GFP_KERNEL);
891 	if (!ccu)
892 		return -ENOMEM;
893 
894 	platform_set_drvdata(pdev, ccu);
895 	mutex_init(&ccu->lock);
896 	INIT_LIST_HEAD(&ccu->core_list);
897 
898 	return 0;
899 }
900 
vepu_attach_ccu(struct device * dev,struct vepu_dev * enc)901 static int vepu_attach_ccu(struct device *dev, struct vepu_dev *enc)
902 {
903 	struct device_node *np;
904 	struct platform_device *pdev;
905 	struct vepu_ccu *ccu;
906 
907 	np = of_parse_phandle(dev->of_node, "rockchip,ccu", 0);
908 	if (!np || !of_device_is_available(np))
909 		return -ENODEV;
910 
911 	pdev = of_find_device_by_node(np);
912 	of_node_put(np);
913 	if (!pdev)
914 		return -ENODEV;
915 
916 	ccu = platform_get_drvdata(pdev);
917 	if (!ccu)
918 		return -ENOMEM;
919 
920 	INIT_LIST_HEAD(&enc->core_link);
921 	mutex_lock(&ccu->lock);
922 	ccu->core_num++;
923 	list_add_tail(&enc->core_link, &ccu->core_list);
924 	mutex_unlock(&ccu->lock);
925 
926 	/* attach the ccu-domain to current core */
927 	if (!ccu->main_core) {
928 		/**
929 		 * set the first device for the main-core,
930 		 * then the domain of the main-core named ccu-domain
931 		 */
932 		ccu->main_core = &enc->mpp;
933 	} else {
934 		struct mpp_iommu_info *ccu_info, *cur_info;
935 
936 		/* set the ccu domain for current device */
937 		ccu_info = ccu->main_core->iommu_info;
938 		cur_info = enc->mpp.iommu_info;
939 
940 		cur_info->domain = ccu_info->domain;
941 		mpp_iommu_attach(cur_info);
942 	}
943 	enc->ccu = ccu;
944 
945 	dev_info(dev, "attach ccu success\n");
946 	return 0;
947 }
948 
vepu_core_probe(struct platform_device * pdev)949 static int vepu_core_probe(struct platform_device *pdev)
950 {
951 	struct device *dev = &pdev->dev;
952 	struct vepu_dev *enc = NULL;
953 	struct mpp_dev *mpp = NULL;
954 	const struct of_device_id *match = NULL;
955 	int ret = 0;
956 
957 	enc = devm_kzalloc(dev, sizeof(struct vepu_dev), GFP_KERNEL);
958 	if (!enc)
959 		return -ENOMEM;
960 
961 	mpp = &enc->mpp;
962 	platform_set_drvdata(pdev, mpp);
963 
964 	if (pdev->dev.of_node) {
965 		match = of_match_node(mpp_vepu2_dt_match, pdev->dev.of_node);
966 		if (match)
967 			mpp->var = (struct mpp_dev_var *)match->data;
968 
969 		mpp->core_id = of_alias_get_id(pdev->dev.of_node, "jpege");
970 	}
971 
972 	ret = mpp_dev_probe(mpp, pdev);
973 	if (ret) {
974 		dev_err(dev, "probe sub driver failed\n");
975 		return -EINVAL;
976 	}
977 	/* current device attach to ccu */
978 	ret = vepu_attach_ccu(dev, enc);
979 	if (ret)
980 		return ret;
981 
982 	ret = devm_request_threaded_irq(dev, mpp->irq,
983 					mpp_dev_irq,
984 					mpp_dev_isr_sched,
985 					IRQF_SHARED,
986 					dev_name(dev), mpp);
987 	if (ret) {
988 		dev_err(dev, "register interrupter runtime failed\n");
989 		return -EINVAL;
990 	}
991 
992 	mpp->session_max_buffers = VEPU2_SESSION_MAX_BUFFERS;
993 	vepu_procfs_init(mpp);
994 	vepu_procfs_ccu_init(mpp);
995 	/* if current is main-core, register current device to mpp service */
996 	if (mpp == enc->ccu->main_core)
997 		mpp_dev_register_srv(mpp, mpp->srv);
998 
999 	return 0;
1000 }
1001 
vepu_probe_default(struct platform_device * pdev)1002 static int vepu_probe_default(struct platform_device *pdev)
1003 {
1004 	struct device *dev = &pdev->dev;
1005 	struct vepu_dev *enc = NULL;
1006 	struct mpp_dev *mpp = NULL;
1007 	const struct of_device_id *match = NULL;
1008 	int ret = 0;
1009 
1010 	enc = devm_kzalloc(dev, sizeof(struct vepu_dev), GFP_KERNEL);
1011 	if (!enc)
1012 		return -ENOMEM;
1013 
1014 	mpp = &enc->mpp;
1015 	platform_set_drvdata(pdev, mpp);
1016 
1017 	if (pdev->dev.of_node) {
1018 		match = of_match_node(mpp_vepu2_dt_match, pdev->dev.of_node);
1019 		if (match)
1020 			mpp->var = (struct mpp_dev_var *)match->data;
1021 
1022 		mpp->core_id = of_alias_get_id(pdev->dev.of_node, "vepu");
1023 	}
1024 
1025 	ret = mpp_dev_probe(mpp, pdev);
1026 	if (ret) {
1027 		dev_err(dev, "probe sub driver failed\n");
1028 		return -EINVAL;
1029 	}
1030 
1031 	ret = devm_request_threaded_irq(dev, mpp->irq,
1032 					mpp_dev_irq,
1033 					mpp_dev_isr_sched,
1034 					IRQF_SHARED,
1035 					dev_name(dev), mpp);
1036 	if (ret) {
1037 		dev_err(dev, "register interrupter runtime failed\n");
1038 		return -EINVAL;
1039 	}
1040 
1041 	mpp->session_max_buffers = VEPU2_SESSION_MAX_BUFFERS;
1042 	vepu_procfs_init(mpp);
1043 	/* register current device to mpp service */
1044 	mpp_dev_register_srv(mpp, mpp->srv);
1045 
1046 	return 0;
1047 }
1048 
vepu_probe(struct platform_device * pdev)1049 static int vepu_probe(struct platform_device *pdev)
1050 {
1051 	int ret;
1052 	struct device *dev = &pdev->dev;
1053 	struct device_node *np = dev->of_node;
1054 
1055 	dev_info(dev, "probing start\n");
1056 
1057 	if (strstr(np->name, "ccu"))
1058 		ret = vepu_ccu_probe(pdev);
1059 	else if (strstr(np->name, "core"))
1060 		ret = vepu_core_probe(pdev);
1061 	else
1062 		ret = vepu_probe_default(pdev);
1063 
1064 	dev_info(dev, "probing finish\n");
1065 
1066 	return ret;
1067 }
1068 
vepu_remove(struct platform_device * pdev)1069 static int vepu_remove(struct platform_device *pdev)
1070 {
1071 	struct device *dev = &pdev->dev;
1072 	struct device_node *np = dev->of_node;
1073 
1074 	if (strstr(np->name, "ccu")) {
1075 		dev_info(dev, "remove ccu device\n");
1076 	} else if (strstr(np->name, "core")) {
1077 		struct mpp_dev *mpp = dev_get_drvdata(dev);
1078 		struct vepu_dev *enc = to_vepu_dev(mpp);
1079 
1080 		dev_info(dev, "remove core\n");
1081 		if (enc->ccu) {
1082 			mutex_lock(&enc->ccu->lock);
1083 			list_del_init(&enc->core_link);
1084 			enc->ccu->core_num--;
1085 			mutex_unlock(&enc->ccu->lock);
1086 		}
1087 		mpp_dev_remove(&enc->mpp);
1088 		vepu_procfs_remove(&enc->mpp);
1089 	} else {
1090 		struct mpp_dev *mpp = dev_get_drvdata(dev);
1091 
1092 		dev_info(dev, "remove device\n");
1093 		mpp_dev_remove(mpp);
1094 		vepu_procfs_remove(mpp);
1095 	}
1096 
1097 	return 0;
1098 }
1099 
vepu_shutdown(struct platform_device * pdev)1100 static void vepu_shutdown(struct platform_device *pdev)
1101 {
1102 	struct device *dev = &pdev->dev;
1103 
1104 	if (!strstr(dev_name(dev), "ccu"))
1105 		mpp_dev_shutdown(pdev);
1106 }
1107 
1108 struct platform_driver rockchip_vepu2_driver = {
1109 	.probe = vepu_probe,
1110 	.remove = vepu_remove,
1111 	.shutdown = vepu_shutdown,
1112 	.driver = {
1113 		.name = VEPU2_DRIVER_NAME,
1114 		.of_match_table = of_match_ptr(mpp_vepu2_dt_match),
1115 	},
1116 };
1117 EXPORT_SYMBOL(rockchip_vepu2_driver);
1118