1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2020-2021 NXP
4 */
5
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/pm_domain.h>
19 #include <linux/firmware.h>
20 #include <linux/vmalloc.h>
21 #include "vpu.h"
22 #include "vpu_defs.h"
23 #include "vpu_core.h"
24 #include "vpu_mbox.h"
25 #include "vpu_msgs.h"
26 #include "vpu_rpc.h"
27 #include "vpu_cmds.h"
28
csr_writel(struct vpu_core * core,u32 reg,u32 val)29 void csr_writel(struct vpu_core *core, u32 reg, u32 val)
30 {
31 writel(val, core->base + reg);
32 }
33
csr_readl(struct vpu_core * core,u32 reg)34 u32 csr_readl(struct vpu_core *core, u32 reg)
35 {
36 return readl(core->base + reg);
37 }
38
vpu_core_load_firmware(struct vpu_core * core)39 static int vpu_core_load_firmware(struct vpu_core *core)
40 {
41 const struct firmware *pfw = NULL;
42 int ret = 0;
43
44 if (!core->fw.virt) {
45 dev_err(core->dev, "firmware buffer is not ready\n");
46 return -EINVAL;
47 }
48
49 ret = request_firmware(&pfw, core->res->fwname, core->dev);
50 dev_dbg(core->dev, "request_firmware %s : %d\n", core->res->fwname, ret);
51 if (ret) {
52 dev_err(core->dev, "request firmware %s failed, ret = %d\n",
53 core->res->fwname, ret);
54 return ret;
55 }
56
57 if (core->fw.length < pfw->size) {
58 dev_err(core->dev, "firmware buffer size want %zu, but %d\n",
59 pfw->size, core->fw.length);
60 ret = -EINVAL;
61 goto exit;
62 }
63
64 memset(core->fw.virt, 0, core->fw.length);
65 memcpy(core->fw.virt, pfw->data, pfw->size);
66 core->fw.bytesused = pfw->size;
67 ret = vpu_iface_on_firmware_loaded(core);
68 exit:
69 release_firmware(pfw);
70 pfw = NULL;
71
72 return ret;
73 }
74
vpu_core_boot_done(struct vpu_core * core)75 static int vpu_core_boot_done(struct vpu_core *core)
76 {
77 u32 fw_version;
78
79 fw_version = vpu_iface_get_version(core);
80 dev_info(core->dev, "%s firmware version : %d.%d.%d\n",
81 vpu_core_type_desc(core->type),
82 (fw_version >> 16) & 0xff,
83 (fw_version >> 8) & 0xff,
84 fw_version & 0xff);
85 core->supported_instance_count = vpu_iface_get_max_instance_count(core);
86 if (core->res->act_size) {
87 u32 count = core->act.length / core->res->act_size;
88
89 core->supported_instance_count = min(core->supported_instance_count, count);
90 }
91 core->fw_version = fw_version;
92 core->state = VPU_CORE_ACTIVE;
93
94 return 0;
95 }
96
vpu_core_wait_boot_done(struct vpu_core * core)97 static int vpu_core_wait_boot_done(struct vpu_core *core)
98 {
99 int ret;
100
101 ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
102 if (!ret) {
103 dev_err(core->dev, "boot timeout\n");
104 return -EINVAL;
105 }
106 return vpu_core_boot_done(core);
107 }
108
vpu_core_boot(struct vpu_core * core,bool load)109 static int vpu_core_boot(struct vpu_core *core, bool load)
110 {
111 int ret;
112
113 reinit_completion(&core->cmp);
114 if (load) {
115 ret = vpu_core_load_firmware(core);
116 if (ret)
117 return ret;
118 }
119
120 vpu_iface_boot_core(core);
121 return vpu_core_wait_boot_done(core);
122 }
123
vpu_core_shutdown(struct vpu_core * core)124 static int vpu_core_shutdown(struct vpu_core *core)
125 {
126 return vpu_iface_shutdown_core(core);
127 }
128
vpu_core_restore(struct vpu_core * core)129 static int vpu_core_restore(struct vpu_core *core)
130 {
131 int ret;
132
133 ret = vpu_core_sw_reset(core);
134 if (ret)
135 return ret;
136
137 vpu_core_boot_done(core);
138 return vpu_iface_restore_core(core);
139 }
140
__vpu_alloc_dma(struct device * dev,struct vpu_buffer * buf)141 static int __vpu_alloc_dma(struct device *dev, struct vpu_buffer *buf)
142 {
143 gfp_t gfp = GFP_KERNEL | GFP_DMA32;
144
145 if (!buf->length)
146 return 0;
147
148 buf->virt = dma_alloc_coherent(dev, buf->length, &buf->phys, gfp);
149 if (!buf->virt)
150 return -ENOMEM;
151
152 buf->dev = dev;
153
154 return 0;
155 }
156
vpu_free_dma(struct vpu_buffer * buf)157 void vpu_free_dma(struct vpu_buffer *buf)
158 {
159 if (!buf->virt || !buf->dev)
160 return;
161
162 dma_free_coherent(buf->dev, buf->length, buf->virt, buf->phys);
163 buf->virt = NULL;
164 buf->phys = 0;
165 buf->length = 0;
166 buf->bytesused = 0;
167 buf->dev = NULL;
168 }
169
vpu_alloc_dma(struct vpu_core * core,struct vpu_buffer * buf)170 int vpu_alloc_dma(struct vpu_core *core, struct vpu_buffer *buf)
171 {
172 return __vpu_alloc_dma(core->dev, buf);
173 }
174
vpu_core_check_hang(struct vpu_core * core)175 static void vpu_core_check_hang(struct vpu_core *core)
176 {
177 if (core->hang_mask)
178 core->state = VPU_CORE_HANG;
179 }
180
vpu_core_find_proper_by_type(struct vpu_dev * vpu,u32 type)181 static struct vpu_core *vpu_core_find_proper_by_type(struct vpu_dev *vpu, u32 type)
182 {
183 struct vpu_core *core = NULL;
184 int request_count = INT_MAX;
185 struct vpu_core *c;
186
187 list_for_each_entry(c, &vpu->cores, list) {
188 dev_dbg(c->dev, "instance_mask = 0x%lx, state = %d\n", c->instance_mask, c->state);
189 if (c->type != type)
190 continue;
191 if (c->state == VPU_CORE_DEINIT) {
192 core = c;
193 break;
194 }
195 vpu_core_check_hang(c);
196 if (c->state != VPU_CORE_ACTIVE)
197 continue;
198 if (c->request_count < request_count) {
199 request_count = c->request_count;
200 core = c;
201 }
202 if (!request_count)
203 break;
204 }
205
206 return core;
207 }
208
vpu_core_is_exist(struct vpu_dev * vpu,struct vpu_core * core)209 static bool vpu_core_is_exist(struct vpu_dev *vpu, struct vpu_core *core)
210 {
211 struct vpu_core *c;
212
213 list_for_each_entry(c, &vpu->cores, list) {
214 if (c == core)
215 return true;
216 }
217
218 return false;
219 }
220
vpu_core_get_vpu(struct vpu_core * core)221 static void vpu_core_get_vpu(struct vpu_core *core)
222 {
223 core->vpu->get_vpu(core->vpu);
224 if (core->type == VPU_CORE_TYPE_ENC)
225 core->vpu->get_enc(core->vpu);
226 if (core->type == VPU_CORE_TYPE_DEC)
227 core->vpu->get_dec(core->vpu);
228 }
229
vpu_core_register(struct device * dev,struct vpu_core * core)230 static int vpu_core_register(struct device *dev, struct vpu_core *core)
231 {
232 struct vpu_dev *vpu = dev_get_drvdata(dev);
233 int ret = 0;
234
235 dev_dbg(core->dev, "register core %s\n", vpu_core_type_desc(core->type));
236 if (vpu_core_is_exist(vpu, core))
237 return 0;
238
239 core->workqueue = alloc_workqueue("vpu", WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
240 if (!core->workqueue) {
241 dev_err(core->dev, "fail to alloc workqueue\n");
242 return -ENOMEM;
243 }
244 INIT_WORK(&core->msg_work, vpu_msg_run_work);
245 INIT_DELAYED_WORK(&core->msg_delayed_work, vpu_msg_delayed_work);
246 core->msg_buffer_size = roundup_pow_of_two(VPU_MSG_BUFFER_SIZE);
247 core->msg_buffer = vzalloc(core->msg_buffer_size);
248 if (!core->msg_buffer) {
249 dev_err(core->dev, "failed allocate buffer for fifo\n");
250 ret = -ENOMEM;
251 goto error;
252 }
253 ret = kfifo_init(&core->msg_fifo, core->msg_buffer, core->msg_buffer_size);
254 if (ret) {
255 dev_err(core->dev, "failed init kfifo\n");
256 goto error;
257 }
258
259 list_add_tail(&core->list, &vpu->cores);
260
261 vpu_core_get_vpu(core);
262
263 if (vpu_iface_get_power_state(core))
264 ret = vpu_core_restore(core);
265 if (ret)
266 goto error;
267
268 return 0;
269 error:
270 if (core->msg_buffer) {
271 vfree(core->msg_buffer);
272 core->msg_buffer = NULL;
273 }
274 if (core->workqueue) {
275 destroy_workqueue(core->workqueue);
276 core->workqueue = NULL;
277 }
278 return ret;
279 }
280
vpu_core_put_vpu(struct vpu_core * core)281 static void vpu_core_put_vpu(struct vpu_core *core)
282 {
283 if (core->type == VPU_CORE_TYPE_ENC)
284 core->vpu->put_enc(core->vpu);
285 if (core->type == VPU_CORE_TYPE_DEC)
286 core->vpu->put_dec(core->vpu);
287 core->vpu->put_vpu(core->vpu);
288 }
289
vpu_core_unregister(struct device * dev,struct vpu_core * core)290 static int vpu_core_unregister(struct device *dev, struct vpu_core *core)
291 {
292 list_del_init(&core->list);
293
294 vpu_core_put_vpu(core);
295 core->vpu = NULL;
296 vfree(core->msg_buffer);
297 core->msg_buffer = NULL;
298
299 if (core->workqueue) {
300 cancel_work_sync(&core->msg_work);
301 cancel_delayed_work_sync(&core->msg_delayed_work);
302 destroy_workqueue(core->workqueue);
303 core->workqueue = NULL;
304 }
305
306 return 0;
307 }
308
vpu_core_acquire_instance(struct vpu_core * core)309 static int vpu_core_acquire_instance(struct vpu_core *core)
310 {
311 int id;
312
313 id = ffz(core->instance_mask);
314 if (id >= core->supported_instance_count)
315 return -EINVAL;
316
317 set_bit(id, &core->instance_mask);
318
319 return id;
320 }
321
vpu_core_release_instance(struct vpu_core * core,int id)322 static void vpu_core_release_instance(struct vpu_core *core, int id)
323 {
324 if (id < 0 || id >= core->supported_instance_count)
325 return;
326
327 clear_bit(id, &core->instance_mask);
328 }
329
vpu_inst_get(struct vpu_inst * inst)330 struct vpu_inst *vpu_inst_get(struct vpu_inst *inst)
331 {
332 if (!inst)
333 return NULL;
334
335 atomic_inc(&inst->ref_count);
336
337 return inst;
338 }
339
vpu_inst_put(struct vpu_inst * inst)340 void vpu_inst_put(struct vpu_inst *inst)
341 {
342 if (!inst)
343 return;
344 if (atomic_dec_and_test(&inst->ref_count)) {
345 if (inst->release)
346 inst->release(inst);
347 }
348 }
349
vpu_request_core(struct vpu_dev * vpu,enum vpu_core_type type)350 struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum vpu_core_type type)
351 {
352 struct vpu_core *core = NULL;
353 int ret;
354
355 mutex_lock(&vpu->lock);
356
357 core = vpu_core_find_proper_by_type(vpu, type);
358 if (!core)
359 goto exit;
360
361 mutex_lock(&core->lock);
362 pm_runtime_resume_and_get(core->dev);
363
364 if (core->state == VPU_CORE_DEINIT) {
365 ret = vpu_core_boot(core, true);
366 if (ret) {
367 pm_runtime_put_sync(core->dev);
368 mutex_unlock(&core->lock);
369 core = NULL;
370 goto exit;
371 }
372 }
373
374 core->request_count++;
375
376 mutex_unlock(&core->lock);
377 exit:
378 mutex_unlock(&vpu->lock);
379
380 return core;
381 }
382
vpu_release_core(struct vpu_core * core)383 void vpu_release_core(struct vpu_core *core)
384 {
385 if (!core)
386 return;
387
388 mutex_lock(&core->lock);
389 pm_runtime_put_sync(core->dev);
390 if (core->request_count)
391 core->request_count--;
392 mutex_unlock(&core->lock);
393 }
394
vpu_inst_register(struct vpu_inst * inst)395 int vpu_inst_register(struct vpu_inst *inst)
396 {
397 struct vpu_dev *vpu;
398 struct vpu_core *core;
399 int ret = 0;
400
401 vpu = inst->vpu;
402 core = inst->core;
403 if (!core) {
404 core = vpu_request_core(vpu, inst->type);
405 if (!core) {
406 dev_err(vpu->dev, "there is no vpu core for %s\n",
407 vpu_core_type_desc(inst->type));
408 return -EINVAL;
409 }
410 inst->core = core;
411 inst->dev = get_device(core->dev);
412 }
413
414 mutex_lock(&core->lock);
415 if (inst->id >= 0 && inst->id < core->supported_instance_count)
416 goto exit;
417
418 ret = vpu_core_acquire_instance(core);
419 if (ret < 0)
420 goto exit;
421
422 vpu_trace(inst->dev, "[%d] %p\n", ret, inst);
423 inst->id = ret;
424 list_add_tail(&inst->list, &core->instances);
425 ret = 0;
426 if (core->res->act_size) {
427 inst->act.phys = core->act.phys + core->res->act_size * inst->id;
428 inst->act.virt = core->act.virt + core->res->act_size * inst->id;
429 inst->act.length = core->res->act_size;
430 }
431 vpu_inst_create_dbgfs_file(inst);
432 exit:
433 mutex_unlock(&core->lock);
434
435 if (ret)
436 dev_err(core->dev, "register instance fail\n");
437 return ret;
438 }
439
vpu_inst_unregister(struct vpu_inst * inst)440 int vpu_inst_unregister(struct vpu_inst *inst)
441 {
442 struct vpu_core *core;
443
444 if (!inst->core)
445 return 0;
446
447 core = inst->core;
448 vpu_clear_request(inst);
449 mutex_lock(&core->lock);
450 if (inst->id >= 0 && inst->id < core->supported_instance_count) {
451 vpu_inst_remove_dbgfs_file(inst);
452 list_del_init(&inst->list);
453 vpu_core_release_instance(core, inst->id);
454 inst->id = VPU_INST_NULL_ID;
455 }
456 vpu_core_check_hang(core);
457 if (core->state == VPU_CORE_HANG && !core->instance_mask) {
458 int err;
459
460 dev_info(core->dev, "reset hang core\n");
461 mutex_unlock(&core->lock);
462 err = vpu_core_sw_reset(core);
463 mutex_lock(&core->lock);
464 if (!err) {
465 core->state = VPU_CORE_ACTIVE;
466 core->hang_mask = 0;
467 }
468 }
469 mutex_unlock(&core->lock);
470
471 return 0;
472 }
473
vpu_core_find_instance(struct vpu_core * core,u32 index)474 struct vpu_inst *vpu_core_find_instance(struct vpu_core *core, u32 index)
475 {
476 struct vpu_inst *inst = NULL;
477 struct vpu_inst *tmp;
478
479 mutex_lock(&core->lock);
480 if (index >= core->supported_instance_count || !test_bit(index, &core->instance_mask))
481 goto exit;
482 list_for_each_entry(tmp, &core->instances, list) {
483 if (tmp->id == index) {
484 inst = vpu_inst_get(tmp);
485 break;
486 }
487 }
488 exit:
489 mutex_unlock(&core->lock);
490
491 return inst;
492 }
493
vpu_get_resource(struct vpu_inst * inst)494 const struct vpu_core_resources *vpu_get_resource(struct vpu_inst *inst)
495 {
496 struct vpu_dev *vpu;
497 struct vpu_core *core = NULL;
498 const struct vpu_core_resources *res = NULL;
499
500 if (!inst || !inst->vpu)
501 return NULL;
502
503 if (inst->core && inst->core->res)
504 return inst->core->res;
505
506 vpu = inst->vpu;
507 mutex_lock(&vpu->lock);
508 list_for_each_entry(core, &vpu->cores, list) {
509 if (core->type == inst->type) {
510 res = core->res;
511 break;
512 }
513 }
514 mutex_unlock(&vpu->lock);
515
516 return res;
517 }
518
vpu_core_parse_dt(struct vpu_core * core,struct device_node * np)519 static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np)
520 {
521 struct device_node *node;
522 struct resource res;
523 int ret;
524
525 if (of_count_phandle_with_args(np, "memory-region", NULL) < 2) {
526 dev_err(core->dev, "need 2 memory-region for boot and rpc\n");
527 return -ENODEV;
528 }
529
530 node = of_parse_phandle(np, "memory-region", 0);
531 if (!node) {
532 dev_err(core->dev, "boot-region of_parse_phandle error\n");
533 return -ENODEV;
534 }
535 if (of_address_to_resource(node, 0, &res)) {
536 dev_err(core->dev, "boot-region of_address_to_resource error\n");
537 of_node_put(node);
538 return -EINVAL;
539 }
540 core->fw.phys = res.start;
541 core->fw.length = resource_size(&res);
542
543 of_node_put(node);
544
545 node = of_parse_phandle(np, "memory-region", 1);
546 if (!node) {
547 dev_err(core->dev, "rpc-region of_parse_phandle error\n");
548 return -ENODEV;
549 }
550 if (of_address_to_resource(node, 0, &res)) {
551 dev_err(core->dev, "rpc-region of_address_to_resource error\n");
552 of_node_put(node);
553 return -EINVAL;
554 }
555 core->rpc.phys = res.start;
556 core->rpc.length = resource_size(&res);
557
558 if (core->rpc.length < core->res->rpc_size + core->res->fwlog_size) {
559 dev_err(core->dev, "the rpc-region <%pad, 0x%x> is not enough\n",
560 &core->rpc.phys, core->rpc.length);
561 of_node_put(node);
562 return -EINVAL;
563 }
564
565 core->fw.virt = memremap(core->fw.phys, core->fw.length, MEMREMAP_WC);
566 core->rpc.virt = memremap(core->rpc.phys, core->rpc.length, MEMREMAP_WC);
567 memset(core->rpc.virt, 0, core->rpc.length);
568
569 ret = vpu_iface_check_memory_region(core, core->rpc.phys, core->rpc.length);
570 if (ret != VPU_CORE_MEMORY_UNCACHED) {
571 dev_err(core->dev, "rpc region<%pad, 0x%x> isn't uncached\n",
572 &core->rpc.phys, core->rpc.length);
573 of_node_put(node);
574 return -EINVAL;
575 }
576
577 core->log.phys = core->rpc.phys + core->res->rpc_size;
578 core->log.virt = core->rpc.virt + core->res->rpc_size;
579 core->log.length = core->res->fwlog_size;
580 core->act.phys = core->log.phys + core->log.length;
581 core->act.virt = core->log.virt + core->log.length;
582 core->act.length = core->rpc.length - core->res->rpc_size - core->log.length;
583 core->rpc.length = core->res->rpc_size;
584
585 of_node_put(node);
586
587 return 0;
588 }
589
vpu_core_probe(struct platform_device * pdev)590 static int vpu_core_probe(struct platform_device *pdev)
591 {
592 struct device *dev = &pdev->dev;
593 struct vpu_core *core;
594 struct vpu_dev *vpu = dev_get_drvdata(dev->parent);
595 struct vpu_shared_addr *iface;
596 u32 iface_data_size;
597 int ret;
598
599 dev_dbg(dev, "probe\n");
600 if (!vpu)
601 return -EINVAL;
602 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
603 if (!core)
604 return -ENOMEM;
605
606 core->pdev = pdev;
607 core->dev = dev;
608 platform_set_drvdata(pdev, core);
609 core->vpu = vpu;
610 INIT_LIST_HEAD(&core->instances);
611 mutex_init(&core->lock);
612 mutex_init(&core->cmd_lock);
613 init_completion(&core->cmp);
614 init_waitqueue_head(&core->ack_wq);
615 core->state = VPU_CORE_DEINIT;
616
617 core->res = of_device_get_match_data(dev);
618 if (!core->res)
619 return -ENODEV;
620
621 core->type = core->res->type;
622 core->id = of_alias_get_id(dev->of_node, "vpu_core");
623 if (core->id < 0) {
624 dev_err(dev, "can't get vpu core id\n");
625 return core->id;
626 }
627 dev_info(core->dev, "[%d] = %s\n", core->id, vpu_core_type_desc(core->type));
628 ret = vpu_core_parse_dt(core, dev->of_node);
629 if (ret)
630 return ret;
631
632 core->base = devm_platform_ioremap_resource(pdev, 0);
633 if (IS_ERR(core->base))
634 return PTR_ERR(core->base);
635
636 if (!vpu_iface_check_codec(core)) {
637 dev_err(core->dev, "is not supported\n");
638 return -EINVAL;
639 }
640
641 ret = vpu_mbox_init(core);
642 if (ret)
643 return ret;
644
645 iface = devm_kzalloc(dev, sizeof(*iface), GFP_KERNEL);
646 if (!iface)
647 return -ENOMEM;
648
649 iface_data_size = vpu_iface_get_data_size(core);
650 if (iface_data_size) {
651 iface->priv = devm_kzalloc(dev, iface_data_size, GFP_KERNEL);
652 if (!iface->priv)
653 return -ENOMEM;
654 }
655
656 ret = vpu_iface_init(core, iface, &core->rpc, core->fw.phys);
657 if (ret) {
658 dev_err(core->dev, "init iface fail, ret = %d\n", ret);
659 return ret;
660 }
661
662 vpu_iface_config_system(core, vpu->res->mreg_base, vpu->base);
663 vpu_iface_set_log_buf(core, &core->log);
664
665 pm_runtime_enable(dev);
666 ret = pm_runtime_resume_and_get(dev);
667 if (ret) {
668 pm_runtime_put_noidle(dev);
669 pm_runtime_set_suspended(dev);
670 goto err_runtime_disable;
671 }
672
673 ret = vpu_core_register(dev->parent, core);
674 if (ret)
675 goto err_core_register;
676 core->parent = dev->parent;
677
678 pm_runtime_put_sync(dev);
679 vpu_core_create_dbgfs_file(core);
680
681 return 0;
682
683 err_core_register:
684 pm_runtime_put_sync(dev);
685 err_runtime_disable:
686 pm_runtime_disable(dev);
687
688 return ret;
689 }
690
vpu_core_remove(struct platform_device * pdev)691 static int vpu_core_remove(struct platform_device *pdev)
692 {
693 struct device *dev = &pdev->dev;
694 struct vpu_core *core = platform_get_drvdata(pdev);
695 int ret;
696
697 vpu_core_remove_dbgfs_file(core);
698 ret = pm_runtime_resume_and_get(dev);
699 WARN_ON(ret < 0);
700
701 vpu_core_shutdown(core);
702 pm_runtime_put_sync(dev);
703 pm_runtime_disable(dev);
704
705 vpu_core_unregister(core->parent, core);
706 memunmap(core->fw.virt);
707 memunmap(core->rpc.virt);
708 mutex_destroy(&core->lock);
709 mutex_destroy(&core->cmd_lock);
710
711 return 0;
712 }
713
vpu_core_runtime_resume(struct device * dev)714 static int __maybe_unused vpu_core_runtime_resume(struct device *dev)
715 {
716 struct vpu_core *core = dev_get_drvdata(dev);
717
718 return vpu_mbox_request(core);
719 }
720
vpu_core_runtime_suspend(struct device * dev)721 static int __maybe_unused vpu_core_runtime_suspend(struct device *dev)
722 {
723 struct vpu_core *core = dev_get_drvdata(dev);
724
725 vpu_mbox_free(core);
726 return 0;
727 }
728
vpu_core_cancel_work(struct vpu_core * core)729 static void vpu_core_cancel_work(struct vpu_core *core)
730 {
731 struct vpu_inst *inst = NULL;
732
733 cancel_work_sync(&core->msg_work);
734 cancel_delayed_work_sync(&core->msg_delayed_work);
735
736 mutex_lock(&core->lock);
737 list_for_each_entry(inst, &core->instances, list)
738 cancel_work_sync(&inst->msg_work);
739 mutex_unlock(&core->lock);
740 }
741
vpu_core_resume_work(struct vpu_core * core)742 static void vpu_core_resume_work(struct vpu_core *core)
743 {
744 struct vpu_inst *inst = NULL;
745 unsigned long delay = msecs_to_jiffies(10);
746
747 queue_work(core->workqueue, &core->msg_work);
748 queue_delayed_work(core->workqueue, &core->msg_delayed_work, delay);
749
750 mutex_lock(&core->lock);
751 list_for_each_entry(inst, &core->instances, list)
752 queue_work(inst->workqueue, &inst->msg_work);
753 mutex_unlock(&core->lock);
754 }
755
vpu_core_resume(struct device * dev)756 static int __maybe_unused vpu_core_resume(struct device *dev)
757 {
758 struct vpu_core *core = dev_get_drvdata(dev);
759 int ret = 0;
760
761 mutex_lock(&core->lock);
762 pm_runtime_resume_and_get(dev);
763 vpu_core_get_vpu(core);
764 if (core->state != VPU_CORE_SNAPSHOT)
765 goto exit;
766
767 if (!vpu_iface_get_power_state(core)) {
768 if (!list_empty(&core->instances)) {
769 ret = vpu_core_boot(core, false);
770 if (ret) {
771 dev_err(core->dev, "%s boot fail\n", __func__);
772 core->state = VPU_CORE_DEINIT;
773 goto exit;
774 }
775 } else {
776 core->state = VPU_CORE_DEINIT;
777 }
778 } else {
779 if (!list_empty(&core->instances)) {
780 ret = vpu_core_sw_reset(core);
781 if (ret) {
782 dev_err(core->dev, "%s sw_reset fail\n", __func__);
783 core->state = VPU_CORE_HANG;
784 goto exit;
785 }
786 }
787 core->state = VPU_CORE_ACTIVE;
788 }
789
790 exit:
791 pm_runtime_put_sync(dev);
792 mutex_unlock(&core->lock);
793
794 vpu_core_resume_work(core);
795 return ret;
796 }
797
vpu_core_suspend(struct device * dev)798 static int __maybe_unused vpu_core_suspend(struct device *dev)
799 {
800 struct vpu_core *core = dev_get_drvdata(dev);
801 int ret = 0;
802
803 mutex_lock(&core->lock);
804 if (core->state == VPU_CORE_ACTIVE) {
805 if (!list_empty(&core->instances)) {
806 ret = vpu_core_snapshot(core);
807 if (ret) {
808 mutex_unlock(&core->lock);
809 return ret;
810 }
811 }
812
813 core->state = VPU_CORE_SNAPSHOT;
814 }
815 mutex_unlock(&core->lock);
816
817 vpu_core_cancel_work(core);
818
819 mutex_lock(&core->lock);
820 vpu_core_put_vpu(core);
821 mutex_unlock(&core->lock);
822 return ret;
823 }
824
825 static const struct dev_pm_ops vpu_core_pm_ops = {
826 SET_RUNTIME_PM_OPS(vpu_core_runtime_suspend, vpu_core_runtime_resume, NULL)
827 SET_SYSTEM_SLEEP_PM_OPS(vpu_core_suspend, vpu_core_resume)
828 };
829
830 static struct vpu_core_resources imx8q_enc = {
831 .type = VPU_CORE_TYPE_ENC,
832 .fwname = "vpu/vpu_fw_imx8_enc.bin",
833 .stride = 16,
834 .max_width = 1920,
835 .max_height = 1920,
836 .min_width = 64,
837 .min_height = 48,
838 .step_width = 2,
839 .step_height = 2,
840 .rpc_size = 0x80000,
841 .fwlog_size = 0x80000,
842 .act_size = 0xc0000,
843 };
844
845 static struct vpu_core_resources imx8q_dec = {
846 .type = VPU_CORE_TYPE_DEC,
847 .fwname = "vpu/vpu_fw_imx8_dec.bin",
848 .stride = 256,
849 .max_width = 8188,
850 .max_height = 8188,
851 .min_width = 16,
852 .min_height = 16,
853 .step_width = 1,
854 .step_height = 1,
855 .rpc_size = 0x80000,
856 .fwlog_size = 0x80000,
857 };
858
859 static const struct of_device_id vpu_core_dt_match[] = {
860 { .compatible = "nxp,imx8q-vpu-encoder", .data = &imx8q_enc },
861 { .compatible = "nxp,imx8q-vpu-decoder", .data = &imx8q_dec },
862 {}
863 };
864 MODULE_DEVICE_TABLE(of, vpu_core_dt_match);
865
866 static struct platform_driver amphion_vpu_core_driver = {
867 .probe = vpu_core_probe,
868 .remove = vpu_core_remove,
869 .driver = {
870 .name = "amphion-vpu-core",
871 .of_match_table = vpu_core_dt_match,
872 .pm = &vpu_core_pm_ops,
873 },
874 };
875
vpu_core_driver_init(void)876 int __init vpu_core_driver_init(void)
877 {
878 return platform_driver_register(&hion_vpu_core_driver);
879 }
880
vpu_core_driver_exit(void)881 void __exit vpu_core_driver_exit(void)
882 {
883 platform_driver_unregister(&hion_vpu_core_driver);
884 }
885