xref: /linux/drivers/media/platform/qcom/venus/hfi.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 #include <linux/slab.h>
7 #include <linux/mutex.h>
8 #include <linux/list.h>
9 #include <linux/completion.h>
10 #include <linux/platform_device.h>
11 #include <linux/videodev2.h>
12 
13 #include "core.h"
14 #include "hfi.h"
15 #include "hfi_cmds.h"
16 #include "hfi_venus.h"
17 
18 #define TIMEOUT		msecs_to_jiffies(1000)
19 
20 static u32 to_codec_type(u32 pixfmt)
21 {
22 	switch (pixfmt) {
23 	case V4L2_PIX_FMT_H264:
24 	case V4L2_PIX_FMT_H264_NO_SC:
25 		return HFI_VIDEO_CODEC_H264;
26 	case V4L2_PIX_FMT_H263:
27 		return HFI_VIDEO_CODEC_H263;
28 	case V4L2_PIX_FMT_MPEG1:
29 		return HFI_VIDEO_CODEC_MPEG1;
30 	case V4L2_PIX_FMT_MPEG2:
31 		return HFI_VIDEO_CODEC_MPEG2;
32 	case V4L2_PIX_FMT_MPEG4:
33 		return HFI_VIDEO_CODEC_MPEG4;
34 	case V4L2_PIX_FMT_VC1_ANNEX_G:
35 	case V4L2_PIX_FMT_VC1_ANNEX_L:
36 		return HFI_VIDEO_CODEC_VC1;
37 	case V4L2_PIX_FMT_VP8:
38 		return HFI_VIDEO_CODEC_VP8;
39 	case V4L2_PIX_FMT_VP9:
40 		return HFI_VIDEO_CODEC_VP9;
41 	case V4L2_PIX_FMT_XVID:
42 		return HFI_VIDEO_CODEC_DIVX;
43 	case V4L2_PIX_FMT_HEVC:
44 		return HFI_VIDEO_CODEC_HEVC;
45 	default:
46 		return 0;
47 	}
48 }
49 
50 int hfi_core_init(struct venus_core *core)
51 {
52 	int ret = 0;
53 
54 	mutex_lock(&core->lock);
55 
56 	if (core->state >= CORE_INIT)
57 		goto unlock;
58 
59 	reinit_completion(&core->done);
60 
61 	ret = core->ops->core_init(core);
62 	if (ret)
63 		goto unlock;
64 
65 	ret = wait_for_completion_timeout(&core->done, TIMEOUT);
66 	if (!ret) {
67 		ret = -ETIMEDOUT;
68 		goto unlock;
69 	}
70 
71 	ret = 0;
72 
73 	if (core->error != HFI_ERR_NONE) {
74 		ret = -EIO;
75 		goto unlock;
76 	}
77 
78 	core->state = CORE_INIT;
79 unlock:
80 	mutex_unlock(&core->lock);
81 	return ret;
82 }
83 
84 int hfi_core_deinit(struct venus_core *core, bool blocking)
85 {
86 	int ret = 0, empty;
87 
88 	mutex_lock(&core->lock);
89 
90 	if (core->state == CORE_UNINIT)
91 		goto unlock;
92 
93 	empty = list_empty(&core->instances);
94 
95 	if (!empty && !blocking) {
96 		ret = -EBUSY;
97 		goto unlock;
98 	}
99 
100 	if (!empty) {
101 		mutex_unlock(&core->lock);
102 		wait_var_event(&core->insts_count,
103 			       !atomic_read(&core->insts_count));
104 		mutex_lock(&core->lock);
105 	}
106 
107 	ret = core->ops->core_deinit(core);
108 
109 	if (!ret)
110 		core->state = CORE_UNINIT;
111 
112 unlock:
113 	mutex_unlock(&core->lock);
114 	return ret;
115 }
116 
117 int hfi_core_suspend(struct venus_core *core)
118 {
119 	if (core->state != CORE_INIT)
120 		return 0;
121 
122 	return core->ops->suspend(core);
123 }
124 
125 int hfi_core_resume(struct venus_core *core, bool force)
126 {
127 	if (!force && core->state != CORE_INIT)
128 		return 0;
129 
130 	return core->ops->resume(core);
131 }
132 
133 int hfi_core_trigger_ssr(struct venus_core *core, u32 type)
134 {
135 	return core->ops->core_trigger_ssr(core, type);
136 }
137 
138 int hfi_core_ping(struct venus_core *core)
139 {
140 	int ret;
141 
142 	mutex_lock(&core->lock);
143 
144 	ret = core->ops->core_ping(core, 0xbeef);
145 	if (ret)
146 		goto unlock;
147 
148 	ret = wait_for_completion_timeout(&core->done, TIMEOUT);
149 	if (!ret) {
150 		ret = -ETIMEDOUT;
151 		goto unlock;
152 	}
153 	ret = 0;
154 	if (core->error != HFI_ERR_NONE)
155 		ret = -ENODEV;
156 unlock:
157 	mutex_unlock(&core->lock);
158 	return ret;
159 }
160 
161 static int wait_session_msg(struct venus_inst *inst)
162 {
163 	int ret;
164 
165 	ret = wait_for_completion_timeout(&inst->done, TIMEOUT);
166 	if (!ret)
167 		return -ETIMEDOUT;
168 
169 	if (inst->error != HFI_ERR_NONE)
170 		return -EIO;
171 
172 	return 0;
173 }
174 
175 int hfi_session_create(struct venus_inst *inst, const struct hfi_inst_ops *ops)
176 {
177 	struct venus_core *core = inst->core;
178 
179 	if (!ops)
180 		return -EINVAL;
181 
182 	inst->state = INST_UNINIT;
183 	init_completion(&inst->done);
184 	inst->ops = ops;
185 
186 	mutex_lock(&core->lock);
187 	list_add_tail(&inst->list, &core->instances);
188 	atomic_inc(&core->insts_count);
189 	mutex_unlock(&core->lock);
190 
191 	return 0;
192 }
193 EXPORT_SYMBOL_GPL(hfi_session_create);
194 
195 int hfi_session_init(struct venus_inst *inst, u32 pixfmt)
196 {
197 	struct venus_core *core = inst->core;
198 	const struct hfi_ops *ops = core->ops;
199 	int ret;
200 
201 	/*
202 	 * If core shutdown is in progress or if we are in system
203 	 * recovery, return an error as during system error recovery
204 	 * session_init() can't pass successfully
205 	 */
206 	mutex_lock(&core->lock);
207 	if (!core->ops || core->sys_error) {
208 		mutex_unlock(&core->lock);
209 		return -EIO;
210 	}
211 	mutex_unlock(&core->lock);
212 
213 	if (inst->state != INST_UNINIT)
214 		return -EINVAL;
215 
216 	inst->hfi_codec = to_codec_type(pixfmt);
217 	reinit_completion(&inst->done);
218 
219 	ret = ops->session_init(inst, inst->session_type, inst->hfi_codec);
220 	if (ret)
221 		return ret;
222 
223 	ret = wait_session_msg(inst);
224 	if (ret)
225 		return ret;
226 
227 	inst->state = INST_INIT;
228 
229 	return 0;
230 }
231 EXPORT_SYMBOL_GPL(hfi_session_init);
232 
233 void hfi_session_destroy(struct venus_inst *inst)
234 {
235 	struct venus_core *core = inst->core;
236 
237 	mutex_lock(&core->lock);
238 	list_del_init(&inst->list);
239 	if (atomic_dec_and_test(&core->insts_count))
240 		wake_up_var(&core->insts_count);
241 	mutex_unlock(&core->lock);
242 }
243 EXPORT_SYMBOL_GPL(hfi_session_destroy);
244 
245 int hfi_session_deinit(struct venus_inst *inst)
246 {
247 	const struct hfi_ops *ops = inst->core->ops;
248 	int ret;
249 
250 	if (inst->state == INST_UNINIT)
251 		return 0;
252 
253 	if (inst->state < INST_INIT)
254 		return -EINVAL;
255 
256 	reinit_completion(&inst->done);
257 
258 	ret = ops->session_end(inst);
259 	if (ret)
260 		return ret;
261 
262 	ret = wait_session_msg(inst);
263 	if (ret)
264 		return ret;
265 
266 	inst->state = INST_UNINIT;
267 
268 	return 0;
269 }
270 EXPORT_SYMBOL_GPL(hfi_session_deinit);
271 
272 int hfi_session_start(struct venus_inst *inst)
273 {
274 	const struct hfi_ops *ops = inst->core->ops;
275 	int ret;
276 
277 	if (inst->state != INST_LOAD_RESOURCES)
278 		return -EINVAL;
279 
280 	reinit_completion(&inst->done);
281 
282 	ret = ops->session_start(inst);
283 	if (ret)
284 		return ret;
285 
286 	ret = wait_session_msg(inst);
287 	if (ret)
288 		return ret;
289 
290 	inst->state = INST_START;
291 
292 	return 0;
293 }
294 EXPORT_SYMBOL_GPL(hfi_session_start);
295 
296 int hfi_session_stop(struct venus_inst *inst)
297 {
298 	const struct hfi_ops *ops = inst->core->ops;
299 	int ret;
300 
301 	if (inst->state != INST_START)
302 		return -EINVAL;
303 
304 	reinit_completion(&inst->done);
305 
306 	ret = ops->session_stop(inst);
307 	if (ret)
308 		return ret;
309 
310 	ret = wait_session_msg(inst);
311 	if (ret)
312 		return ret;
313 
314 	inst->state = INST_STOP;
315 
316 	return 0;
317 }
318 EXPORT_SYMBOL_GPL(hfi_session_stop);
319 
320 int hfi_session_continue(struct venus_inst *inst)
321 {
322 	struct venus_core *core = inst->core;
323 
324 	if (core->res->hfi_version == HFI_VERSION_1XX)
325 		return 0;
326 
327 	return core->ops->session_continue(inst);
328 }
329 EXPORT_SYMBOL_GPL(hfi_session_continue);
330 
331 int hfi_session_abort(struct venus_inst *inst)
332 {
333 	const struct hfi_ops *ops = inst->core->ops;
334 	int ret;
335 
336 	reinit_completion(&inst->done);
337 
338 	ret = ops->session_abort(inst);
339 	if (ret)
340 		return ret;
341 
342 	ret = wait_session_msg(inst);
343 	if (ret)
344 		return ret;
345 
346 	return 0;
347 }
348 EXPORT_SYMBOL_GPL(hfi_session_abort);
349 
350 int hfi_session_load_res(struct venus_inst *inst)
351 {
352 	const struct hfi_ops *ops = inst->core->ops;
353 	int ret;
354 
355 	if (inst->state != INST_INIT)
356 		return -EINVAL;
357 
358 	reinit_completion(&inst->done);
359 
360 	ret = ops->session_load_res(inst);
361 	if (ret)
362 		return ret;
363 
364 	ret = wait_session_msg(inst);
365 	if (ret)
366 		return ret;
367 
368 	inst->state = INST_LOAD_RESOURCES;
369 
370 	return 0;
371 }
372 
373 int hfi_session_unload_res(struct venus_inst *inst)
374 {
375 	const struct hfi_ops *ops = inst->core->ops;
376 	int ret;
377 
378 	if (inst->state != INST_STOP)
379 		return -EINVAL;
380 
381 	reinit_completion(&inst->done);
382 
383 	ret = ops->session_release_res(inst);
384 	if (ret)
385 		return ret;
386 
387 	ret = wait_session_msg(inst);
388 	if (ret)
389 		return ret;
390 
391 	inst->state = INST_RELEASE_RESOURCES;
392 
393 	return 0;
394 }
395 EXPORT_SYMBOL_GPL(hfi_session_unload_res);
396 
397 int hfi_session_flush(struct venus_inst *inst, u32 type, bool block)
398 {
399 	const struct hfi_ops *ops = inst->core->ops;
400 	int ret;
401 
402 	reinit_completion(&inst->done);
403 
404 	ret = ops->session_flush(inst, type);
405 	if (ret)
406 		return ret;
407 
408 	if (block) {
409 		ret = wait_session_msg(inst);
410 		if (ret)
411 			return ret;
412 	}
413 
414 	return 0;
415 }
416 EXPORT_SYMBOL_GPL(hfi_session_flush);
417 
418 int hfi_session_set_buffers(struct venus_inst *inst, struct hfi_buffer_desc *bd)
419 {
420 	const struct hfi_ops *ops = inst->core->ops;
421 
422 	return ops->session_set_buffers(inst, bd);
423 }
424 
425 int hfi_session_unset_buffers(struct venus_inst *inst,
426 			      struct hfi_buffer_desc *bd)
427 {
428 	const struct hfi_ops *ops = inst->core->ops;
429 	int ret;
430 
431 	reinit_completion(&inst->done);
432 
433 	ret = ops->session_unset_buffers(inst, bd);
434 	if (ret)
435 		return ret;
436 
437 	if (!bd->response_required)
438 		return 0;
439 
440 	ret = wait_session_msg(inst);
441 	if (ret)
442 		return ret;
443 
444 	return 0;
445 }
446 
447 int hfi_session_get_property(struct venus_inst *inst, u32 ptype,
448 			     union hfi_get_property *hprop)
449 {
450 	const struct hfi_ops *ops = inst->core->ops;
451 	int ret;
452 
453 	if (inst->state < INST_INIT || inst->state >= INST_STOP)
454 		return -EINVAL;
455 
456 	reinit_completion(&inst->done);
457 
458 	ret = ops->session_get_property(inst, ptype);
459 	if (ret)
460 		return ret;
461 
462 	ret = wait_session_msg(inst);
463 	if (ret)
464 		return ret;
465 
466 	*hprop = inst->hprop;
467 
468 	return 0;
469 }
470 EXPORT_SYMBOL_GPL(hfi_session_get_property);
471 
472 int hfi_session_set_property(struct venus_inst *inst, u32 ptype, void *pdata)
473 {
474 	const struct hfi_ops *ops = inst->core->ops;
475 
476 	if (inst->state < INST_INIT || inst->state >= INST_STOP)
477 		return -EINVAL;
478 
479 	return ops->session_set_property(inst, ptype, pdata);
480 }
481 EXPORT_SYMBOL_GPL(hfi_session_set_property);
482 
483 int hfi_session_process_buf(struct venus_inst *inst, struct hfi_frame_data *fd)
484 {
485 	const struct hfi_ops *ops = inst->core->ops;
486 
487 	if (fd->buffer_type == HFI_BUFFER_INPUT)
488 		return ops->session_etb(inst, fd);
489 	else if (fd->buffer_type == HFI_BUFFER_OUTPUT ||
490 		 fd->buffer_type == HFI_BUFFER_OUTPUT2)
491 		return ops->session_ftb(inst, fd);
492 
493 	return -EINVAL;
494 }
495 EXPORT_SYMBOL_GPL(hfi_session_process_buf);
496 
497 irqreturn_t hfi_isr_thread(int irq, void *dev_id)
498 {
499 	struct venus_core *core = dev_id;
500 
501 	return core->ops->isr_thread(core);
502 }
503 
504 irqreturn_t hfi_isr(int irq, void *dev)
505 {
506 	struct venus_core *core = dev;
507 
508 	return core->ops->isr(core);
509 }
510 
511 int hfi_create(struct venus_core *core, const struct hfi_core_ops *ops)
512 {
513 	int ret;
514 
515 	if (!ops)
516 		return -EINVAL;
517 
518 	atomic_set(&core->insts_count, 0);
519 	core->core_ops = ops;
520 	core->state = CORE_UNINIT;
521 	init_completion(&core->done);
522 	pkt_set_version(core->res->hfi_version);
523 	ret = venus_hfi_create(core);
524 
525 	return ret;
526 }
527 
528 void hfi_destroy(struct venus_core *core)
529 {
530 	venus_hfi_destroy(core);
531 }
532 
533 void hfi_reinit(struct venus_core *core)
534 {
535 	venus_hfi_queues_reinit(core);
536 }
537