treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / platform / qcom / venus / core.c
blob07312a2fab2478dee747fe3a17f3742140ddbc33
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/clk.h>
7 #include <linux/init.h>
8 #include <linux/interconnect.h>
9 #include <linux/ioctl.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/pm_runtime.h>
17 #include <media/videobuf2-v4l2.h>
18 #include <media/v4l2-mem2mem.h>
19 #include <media/v4l2-ioctl.h>
21 #include "core.h"
22 #include "vdec.h"
23 #include "venc.h"
24 #include "firmware.h"
26 static void venus_event_notify(struct venus_core *core, u32 event)
28 struct venus_inst *inst;
30 switch (event) {
31 case EVT_SYS_WATCHDOG_TIMEOUT:
32 case EVT_SYS_ERROR:
33 break;
34 default:
35 return;
38 mutex_lock(&core->lock);
39 core->sys_error = true;
40 list_for_each_entry(inst, &core->instances, list)
41 inst->ops->event_notify(inst, EVT_SESSION_ERROR, NULL);
42 mutex_unlock(&core->lock);
44 disable_irq_nosync(core->irq);
47 * Delay recovery to ensure venus has completed any pending cache
48 * operations. Without this sleep, we see device reset when firmware is
49 * unloaded after a system error.
51 schedule_delayed_work(&core->work, msecs_to_jiffies(100));
54 static const struct hfi_core_ops venus_core_ops = {
55 .event_notify = venus_event_notify,
58 static void venus_sys_error_handler(struct work_struct *work)
60 struct venus_core *core =
61 container_of(work, struct venus_core, work.work);
62 int ret = 0;
64 dev_warn(core->dev, "system error has occurred, starting recovery!\n");
66 pm_runtime_get_sync(core->dev);
68 hfi_core_deinit(core, true);
69 hfi_destroy(core);
70 mutex_lock(&core->lock);
71 venus_shutdown(core);
73 pm_runtime_put_sync(core->dev);
75 ret |= hfi_create(core, &venus_core_ops);
77 pm_runtime_get_sync(core->dev);
79 ret |= venus_boot(core);
81 ret |= hfi_core_resume(core, true);
83 enable_irq(core->irq);
85 mutex_unlock(&core->lock);
87 ret |= hfi_core_init(core);
89 pm_runtime_put_sync(core->dev);
91 if (ret) {
92 disable_irq_nosync(core->irq);
93 dev_warn(core->dev, "recovery failed (%d)\n", ret);
94 schedule_delayed_work(&core->work, msecs_to_jiffies(10));
95 return;
98 mutex_lock(&core->lock);
99 core->sys_error = false;
100 mutex_unlock(&core->lock);
103 static int venus_clks_get(struct venus_core *core)
105 const struct venus_resources *res = core->res;
106 struct device *dev = core->dev;
107 unsigned int i;
109 for (i = 0; i < res->clks_num; i++) {
110 core->clks[i] = devm_clk_get(dev, res->clks[i]);
111 if (IS_ERR(core->clks[i]))
112 return PTR_ERR(core->clks[i]);
115 return 0;
118 static int venus_clks_enable(struct venus_core *core)
120 const struct venus_resources *res = core->res;
121 unsigned int i;
122 int ret;
124 for (i = 0; i < res->clks_num; i++) {
125 ret = clk_prepare_enable(core->clks[i]);
126 if (ret)
127 goto err;
130 return 0;
131 err:
132 while (i--)
133 clk_disable_unprepare(core->clks[i]);
135 return ret;
138 static void venus_clks_disable(struct venus_core *core)
140 const struct venus_resources *res = core->res;
141 unsigned int i = res->clks_num;
143 while (i--)
144 clk_disable_unprepare(core->clks[i]);
147 static u32 to_v4l2_codec_type(u32 codec)
149 switch (codec) {
150 case HFI_VIDEO_CODEC_H264:
151 return V4L2_PIX_FMT_H264;
152 case HFI_VIDEO_CODEC_H263:
153 return V4L2_PIX_FMT_H263;
154 case HFI_VIDEO_CODEC_MPEG1:
155 return V4L2_PIX_FMT_MPEG1;
156 case HFI_VIDEO_CODEC_MPEG2:
157 return V4L2_PIX_FMT_MPEG2;
158 case HFI_VIDEO_CODEC_MPEG4:
159 return V4L2_PIX_FMT_MPEG4;
160 case HFI_VIDEO_CODEC_VC1:
161 return V4L2_PIX_FMT_VC1_ANNEX_G;
162 case HFI_VIDEO_CODEC_VP8:
163 return V4L2_PIX_FMT_VP8;
164 case HFI_VIDEO_CODEC_VP9:
165 return V4L2_PIX_FMT_VP9;
166 case HFI_VIDEO_CODEC_DIVX:
167 case HFI_VIDEO_CODEC_DIVX_311:
168 return V4L2_PIX_FMT_XVID;
169 default:
170 return 0;
174 static int venus_enumerate_codecs(struct venus_core *core, u32 type)
176 const struct hfi_inst_ops dummy_ops = {};
177 struct venus_inst *inst;
178 u32 codec, codecs;
179 unsigned int i;
180 int ret;
182 if (core->res->hfi_version != HFI_VERSION_1XX)
183 return 0;
185 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
186 if (!inst)
187 return -ENOMEM;
189 mutex_init(&inst->lock);
190 inst->core = core;
191 inst->session_type = type;
192 if (type == VIDC_SESSION_TYPE_DEC)
193 codecs = core->dec_codecs;
194 else
195 codecs = core->enc_codecs;
197 ret = hfi_session_create(inst, &dummy_ops);
198 if (ret)
199 goto err;
201 for (i = 0; i < MAX_CODEC_NUM; i++) {
202 codec = (1UL << i) & codecs;
203 if (!codec)
204 continue;
206 ret = hfi_session_init(inst, to_v4l2_codec_type(codec));
207 if (ret)
208 goto done;
210 ret = hfi_session_deinit(inst);
211 if (ret)
212 goto done;
215 done:
216 hfi_session_destroy(inst);
217 err:
218 mutex_destroy(&inst->lock);
219 kfree(inst);
221 return ret;
224 static int venus_probe(struct platform_device *pdev)
226 struct device *dev = &pdev->dev;
227 struct venus_core *core;
228 struct resource *r;
229 int ret;
231 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
232 if (!core)
233 return -ENOMEM;
235 core->dev = dev;
236 platform_set_drvdata(pdev, core);
238 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
239 core->base = devm_ioremap_resource(dev, r);
240 if (IS_ERR(core->base))
241 return PTR_ERR(core->base);
243 core->video_path = of_icc_get(dev, "video-mem");
244 if (IS_ERR(core->video_path))
245 return PTR_ERR(core->video_path);
247 core->cpucfg_path = of_icc_get(dev, "cpu-cfg");
248 if (IS_ERR(core->cpucfg_path))
249 return PTR_ERR(core->cpucfg_path);
251 core->irq = platform_get_irq(pdev, 0);
252 if (core->irq < 0)
253 return core->irq;
255 core->res = of_device_get_match_data(dev);
256 if (!core->res)
257 return -ENODEV;
259 ret = venus_clks_get(core);
260 if (ret)
261 return ret;
263 ret = dma_set_mask_and_coherent(dev, core->res->dma_mask);
264 if (ret)
265 return ret;
267 if (!dev->dma_parms) {
268 dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
269 GFP_KERNEL);
270 if (!dev->dma_parms)
271 return -ENOMEM;
273 dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
275 INIT_LIST_HEAD(&core->instances);
276 mutex_init(&core->lock);
277 INIT_DELAYED_WORK(&core->work, venus_sys_error_handler);
279 ret = devm_request_threaded_irq(dev, core->irq, hfi_isr, hfi_isr_thread,
280 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
281 "venus", core);
282 if (ret)
283 return ret;
285 ret = icc_set_bw(core->cpucfg_path, 0, kbps_to_icc(1000));
286 if (ret)
287 return ret;
289 ret = hfi_create(core, &venus_core_ops);
290 if (ret)
291 return ret;
293 pm_runtime_enable(dev);
295 ret = pm_runtime_get_sync(dev);
296 if (ret < 0)
297 goto err_runtime_disable;
299 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
300 if (ret)
301 goto err_runtime_disable;
303 ret = venus_firmware_init(core);
304 if (ret)
305 goto err_runtime_disable;
307 ret = venus_boot(core);
308 if (ret)
309 goto err_runtime_disable;
311 ret = hfi_core_resume(core, true);
312 if (ret)
313 goto err_venus_shutdown;
315 ret = hfi_core_init(core);
316 if (ret)
317 goto err_venus_shutdown;
319 ret = venus_enumerate_codecs(core, VIDC_SESSION_TYPE_DEC);
320 if (ret)
321 goto err_venus_shutdown;
323 ret = venus_enumerate_codecs(core, VIDC_SESSION_TYPE_ENC);
324 if (ret)
325 goto err_venus_shutdown;
327 ret = v4l2_device_register(dev, &core->v4l2_dev);
328 if (ret)
329 goto err_core_deinit;
331 ret = pm_runtime_put_sync(dev);
332 if (ret)
333 goto err_dev_unregister;
335 return 0;
337 err_dev_unregister:
338 v4l2_device_unregister(&core->v4l2_dev);
339 err_core_deinit:
340 hfi_core_deinit(core, false);
341 err_venus_shutdown:
342 venus_shutdown(core);
343 err_runtime_disable:
344 pm_runtime_set_suspended(dev);
345 pm_runtime_disable(dev);
346 hfi_destroy(core);
347 return ret;
350 static int venus_remove(struct platform_device *pdev)
352 struct venus_core *core = platform_get_drvdata(pdev);
353 struct device *dev = core->dev;
354 int ret;
356 ret = pm_runtime_get_sync(dev);
357 WARN_ON(ret < 0);
359 ret = hfi_core_deinit(core, true);
360 WARN_ON(ret);
362 hfi_destroy(core);
363 venus_shutdown(core);
364 of_platform_depopulate(dev);
366 venus_firmware_deinit(core);
368 pm_runtime_put_sync(dev);
369 pm_runtime_disable(dev);
371 icc_put(core->video_path);
372 icc_put(core->cpucfg_path);
374 v4l2_device_unregister(&core->v4l2_dev);
376 return ret;
379 static __maybe_unused int venus_runtime_suspend(struct device *dev)
381 struct venus_core *core = dev_get_drvdata(dev);
382 int ret;
384 ret = hfi_core_suspend(core);
386 venus_clks_disable(core);
388 return ret;
391 static __maybe_unused int venus_runtime_resume(struct device *dev)
393 struct venus_core *core = dev_get_drvdata(dev);
394 int ret;
396 ret = venus_clks_enable(core);
397 if (ret)
398 return ret;
400 ret = hfi_core_resume(core, false);
401 if (ret)
402 goto err_clks_disable;
404 return 0;
406 err_clks_disable:
407 venus_clks_disable(core);
408 return ret;
411 static const struct dev_pm_ops venus_pm_ops = {
412 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
413 pm_runtime_force_resume)
414 SET_RUNTIME_PM_OPS(venus_runtime_suspend, venus_runtime_resume, NULL)
417 static const struct freq_tbl msm8916_freq_table[] = {
418 { 352800, 228570000 }, /* 1920x1088 @ 30 + 1280x720 @ 30 */
419 { 244800, 160000000 }, /* 1920x1088 @ 30 */
420 { 108000, 100000000 }, /* 1280x720 @ 30 */
423 static const struct reg_val msm8916_reg_preset[] = {
424 { 0xe0020, 0x05555556 },
425 { 0xe0024, 0x05555556 },
426 { 0x80124, 0x00000003 },
429 static const struct venus_resources msm8916_res = {
430 .freq_tbl = msm8916_freq_table,
431 .freq_tbl_size = ARRAY_SIZE(msm8916_freq_table),
432 .reg_tbl = msm8916_reg_preset,
433 .reg_tbl_size = ARRAY_SIZE(msm8916_reg_preset),
434 .clks = { "core", "iface", "bus", },
435 .clks_num = 3,
436 .max_load = 352800, /* 720p@30 + 1080p@30 */
437 .hfi_version = HFI_VERSION_1XX,
438 .vmem_id = VIDC_RESOURCE_NONE,
439 .vmem_size = 0,
440 .vmem_addr = 0,
441 .dma_mask = 0xddc00000 - 1,
442 .fwname = "qcom/venus-1.8/venus.mdt",
445 static const struct freq_tbl msm8996_freq_table[] = {
446 { 1944000, 520000000 }, /* 4k UHD @ 60 (decode only) */
447 { 972000, 520000000 }, /* 4k UHD @ 30 */
448 { 489600, 346666667 }, /* 1080p @ 60 */
449 { 244800, 150000000 }, /* 1080p @ 30 */
450 { 108000, 75000000 }, /* 720p @ 30 */
453 static const struct reg_val msm8996_reg_preset[] = {
454 { 0x80010, 0xffffffff },
455 { 0x80018, 0x00001556 },
456 { 0x8001C, 0x00001556 },
459 static const struct venus_resources msm8996_res = {
460 .freq_tbl = msm8996_freq_table,
461 .freq_tbl_size = ARRAY_SIZE(msm8996_freq_table),
462 .reg_tbl = msm8996_reg_preset,
463 .reg_tbl_size = ARRAY_SIZE(msm8996_reg_preset),
464 .clks = {"core", "iface", "bus", "mbus" },
465 .clks_num = 4,
466 .max_load = 2563200,
467 .hfi_version = HFI_VERSION_3XX,
468 .vmem_id = VIDC_RESOURCE_NONE,
469 .vmem_size = 0,
470 .vmem_addr = 0,
471 .dma_mask = 0xddc00000 - 1,
472 .fwname = "qcom/venus-4.2/venus.mdt",
475 static const struct freq_tbl sdm845_freq_table[] = {
476 { 3110400, 533000000 }, /* 4096x2160@90 */
477 { 2073600, 444000000 }, /* 4096x2160@60 */
478 { 1944000, 404000000 }, /* 3840x2160@60 */
479 { 972000, 330000000 }, /* 3840x2160@30 */
480 { 489600, 200000000 }, /* 1920x1080@60 */
481 { 244800, 100000000 }, /* 1920x1080@30 */
484 static struct codec_freq_data sdm845_codec_freq_data[] = {
485 { V4L2_PIX_FMT_H264, VIDC_SESSION_TYPE_ENC, 675, 10 },
486 { V4L2_PIX_FMT_HEVC, VIDC_SESSION_TYPE_ENC, 675, 10 },
487 { V4L2_PIX_FMT_VP8, VIDC_SESSION_TYPE_ENC, 675, 10 },
488 { V4L2_PIX_FMT_MPEG2, VIDC_SESSION_TYPE_DEC, 200, 10 },
489 { V4L2_PIX_FMT_H264, VIDC_SESSION_TYPE_DEC, 200, 10 },
490 { V4L2_PIX_FMT_HEVC, VIDC_SESSION_TYPE_DEC, 200, 10 },
491 { V4L2_PIX_FMT_VP8, VIDC_SESSION_TYPE_DEC, 200, 10 },
492 { V4L2_PIX_FMT_VP9, VIDC_SESSION_TYPE_DEC, 200, 10 },
495 static const struct bw_tbl sdm845_bw_table_enc[] = {
496 { 1944000, 1612000, 0, 2416000, 0 }, /* 3840x2160@60 */
497 { 972000, 951000, 0, 1434000, 0 }, /* 3840x2160@30 */
498 { 489600, 723000, 0, 973000, 0 }, /* 1920x1080@60 */
499 { 244800, 370000, 0, 495000, 0 }, /* 1920x1080@30 */
502 static const struct bw_tbl sdm845_bw_table_dec[] = {
503 { 2073600, 3929000, 0, 5551000, 0 }, /* 4096x2160@60 */
504 { 1036800, 1987000, 0, 2797000, 0 }, /* 4096x2160@30 */
505 { 489600, 1040000, 0, 1298000, 0 }, /* 1920x1080@60 */
506 { 244800, 530000, 0, 659000, 0 }, /* 1920x1080@30 */
509 static const struct venus_resources sdm845_res = {
510 .freq_tbl = sdm845_freq_table,
511 .freq_tbl_size = ARRAY_SIZE(sdm845_freq_table),
512 .bw_tbl_enc = sdm845_bw_table_enc,
513 .bw_tbl_enc_size = ARRAY_SIZE(sdm845_bw_table_enc),
514 .bw_tbl_dec = sdm845_bw_table_dec,
515 .bw_tbl_dec_size = ARRAY_SIZE(sdm845_bw_table_dec),
516 .codec_freq_data = sdm845_codec_freq_data,
517 .codec_freq_data_size = ARRAY_SIZE(sdm845_codec_freq_data),
518 .clks = {"core", "iface", "bus" },
519 .clks_num = 3,
520 .max_load = 3110400, /* 4096x2160@90 */
521 .hfi_version = HFI_VERSION_4XX,
522 .vmem_id = VIDC_RESOURCE_NONE,
523 .vmem_size = 0,
524 .vmem_addr = 0,
525 .dma_mask = 0xe0000000 - 1,
526 .fwname = "qcom/venus-5.2/venus.mdt",
529 static const struct of_device_id venus_dt_match[] = {
530 { .compatible = "qcom,msm8916-venus", .data = &msm8916_res, },
531 { .compatible = "qcom,msm8996-venus", .data = &msm8996_res, },
532 { .compatible = "qcom,sdm845-venus", .data = &sdm845_res, },
535 MODULE_DEVICE_TABLE(of, venus_dt_match);
537 static struct platform_driver qcom_venus_driver = {
538 .probe = venus_probe,
539 .remove = venus_remove,
540 .driver = {
541 .name = "qcom-venus",
542 .of_match_table = venus_dt_match,
543 .pm = &venus_pm_ops,
546 module_platform_driver(qcom_venus_driver);
548 MODULE_ALIAS("platform:qcom-venus");
549 MODULE_DESCRIPTION("Qualcomm Venus video encoder and decoder driver");
550 MODULE_LICENSE("GPL v2");