1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
7 #include <linux/debugfs.h>
8 #include <linux/firmware.h>
9 #include <linux/interrupt.h>
10 #include <linux/iommu.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/of_reserved_mem.h>
16 #include <linux/sched.h>
17 #include <linux/sizes.h>
18 #include <linux/dma-mapping.h>
23 * VPU (video processor unit) is a tiny processor controlling video hardware
24 * related to video codec, scaling and color format converting.
25 * VPU interfaces with other blocks by share memory and interrupt.
28 #define INIT_TIMEOUT_MS 2000U
29 #define IPI_TIMEOUT_MS 2000U
30 #define VPU_IDLE_TIMEOUT_MS 1000U
31 #define VPU_FW_VER_LEN 16
33 /* maximum program/data TCM (Tightly-Coupled Memory) size */
34 #define VPU_PTCM_SIZE (96 * SZ_1K)
35 #define VPU_DTCM_SIZE (32 * SZ_1K)
36 /* the offset to get data tcm address */
37 #define VPU_DTCM_OFFSET 0x18000UL
38 /* daynamic allocated maximum extended memory size */
39 #define VPU_EXT_P_SIZE SZ_1M
40 #define VPU_EXT_D_SIZE SZ_4M
41 /* maximum binary firmware size */
42 #define VPU_P_FW_SIZE (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
43 #define VPU_D_FW_SIZE (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
44 /* the size of share buffer between Host and VPU */
45 #define SHARE_BUF_SIZE 48
47 /* binary firmware name */
48 #define VPU_P_FW "vpu_p.bin"
49 #define VPU_D_FW "vpu_d.bin"
50 #define VPU_P_FW_NEW "mediatek/mt8173/vpu_p.bin"
51 #define VPU_D_FW_NEW "mediatek/mt8173/vpu_d.bin"
54 #define VPU_TCM_CFG 0x0008
55 #define VPU_PMEM_EXT0_ADDR 0x000C
56 #define VPU_PMEM_EXT1_ADDR 0x0010
57 #define VPU_TO_HOST 0x001C
58 #define VPU_DMEM_EXT0_ADDR 0x0014
59 #define VPU_DMEM_EXT1_ADDR 0x0018
60 #define HOST_TO_VPU 0x0024
61 #define VPU_IDLE_REG 0x002C
62 #define VPU_INT_STATUS 0x0034
63 #define VPU_PC_REG 0x0060
64 #define VPU_SP_REG 0x0064
65 #define VPU_RA_REG 0x0068
66 #define VPU_WDT_REG 0x0084
68 /* vpu inter-processor communication interrupt */
69 #define VPU_IPC_INT BIT(8)
71 #define VPU_IDLE_STATE BIT(23)
74 * enum vpu_fw_type - VPU firmware type
76 * @P_FW: program firmware
77 * @D_FW: data firmware
86 * struct vpu_mem - VPU extended program/data memory information
88 * @va: the kernel virtual memory address of VPU extended memory
89 * @pa: the physical memory address of VPU extended memory
98 * struct vpu_regs - VPU TCM and configuration registers
100 * @tcm: the register for VPU Tightly-Coupled Memory
101 * @cfg: the register for VPU configuration
102 * @irq: the irq number for VPU interrupt
111 * struct vpu_wdt_handler - VPU watchdog reset handler
113 * @reset_func: reset handler
114 * @priv: private data
116 struct vpu_wdt_handler
{
117 void (*reset_func
)(void *);
122 * struct vpu_wdt - VPU watchdog workqueue
124 * @handler: VPU watchdog reset handler
125 * @ws: workstruct for VPU watchdog
126 * @wq: workqueue for VPU watchdog
129 struct vpu_wdt_handler handler
[VPU_RST_MAX
];
130 struct work_struct ws
;
131 struct workqueue_struct
*wq
;
135 * struct vpu_run - VPU initialization status
137 * @signaled: the signal of vpu initialization completed
138 * @fw_ver: VPU firmware version
139 * @dec_capability: decoder capability which is not used for now and
140 * the value is reserved for future use
141 * @enc_capability: encoder capability which is not used for now and
142 * the value is reserved for future use
143 * @wq: wait queue for VPU initialization status
147 char fw_ver
[VPU_FW_VER_LEN
];
148 unsigned int dec_capability
;
149 unsigned int enc_capability
;
150 wait_queue_head_t wq
;
154 * struct vpu_ipi_desc - VPU IPI descriptor
156 * @handler: IPI handler
157 * @name: the name of IPI handler
158 * @priv: the private data of IPI handler
160 struct vpu_ipi_desc
{
161 ipi_handler_t handler
;
167 * struct share_obj - DTCM (Data Tightly-Coupled Memory) buffer shared with
171 * @len: share buffer length
172 * @share_buf: share buffer data
177 unsigned char share_buf
[SHARE_BUF_SIZE
];
181 * struct mtk_vpu - vpu driver data
182 * @extmem: VPU extended memory information
183 * @reg: VPU TCM and configuration registers
184 * @run: VPU initialization status
185 * @wdt: VPU watchdog workqueue
186 * @ipi_desc: VPU IPI descriptor
187 * @recv_buf: VPU DTCM share buffer for receiving. The
188 * receive buffer is only accessed in interrupt context.
189 * @send_buf: VPU DTCM share buffer for sending
190 * @dev: VPU struct device
191 * @clk: VPU clock on/off
192 * @fw_loaded: indicate VPU firmware loaded
193 * @enable_4GB: VPU 4GB mode on/off
194 * @vpu_mutex: protect mtk_vpu (except recv_buf) and ensure only
195 * one client to use VPU service at a time. For example,
196 * suppose a client is using VPU to decode VP8.
197 * If the other client wants to encode VP8,
198 * it has to wait until VP8 decode completes.
199 * @wdt_refcnt: WDT reference count to make sure the watchdog can be
200 * disabled if no other client is using VPU service
201 * @ack_wq: The wait queue for each codec and mdp. When sleeping
202 * processes wake up, they will check the condition
203 * "ipi_id_ack" to run the corresponding action or
205 * @ipi_id_ack: The ACKs for registered IPI function sending
210 struct vpu_mem extmem
[2];
214 struct vpu_ipi_desc ipi_desc
[IPI_MAX
];
215 struct share_obj __iomem
*recv_buf
;
216 struct share_obj __iomem
*send_buf
;
221 struct mutex vpu_mutex
; /* for protecting vpu data data structure */
223 wait_queue_head_t ack_wq
;
224 bool ipi_id_ack
[IPI_MAX
];
227 static inline void vpu_cfg_writel(struct mtk_vpu
*vpu
, u32 val
, u32 offset
)
229 writel(val
, vpu
->reg
.cfg
+ offset
);
232 static inline u32
vpu_cfg_readl(struct mtk_vpu
*vpu
, u32 offset
)
234 return readl(vpu
->reg
.cfg
+ offset
);
237 static inline bool vpu_running(struct mtk_vpu
*vpu
)
239 return vpu_cfg_readl(vpu
, VPU_RESET
) & BIT(0);
242 static void vpu_clock_disable(struct mtk_vpu
*vpu
)
244 /* Disable VPU watchdog */
245 mutex_lock(&vpu
->vpu_mutex
);
246 if (!--vpu
->wdt_refcnt
)
248 vpu_cfg_readl(vpu
, VPU_WDT_REG
) & ~(1L << 31),
250 mutex_unlock(&vpu
->vpu_mutex
);
252 clk_disable(vpu
->clk
);
255 static int vpu_clock_enable(struct mtk_vpu
*vpu
)
259 ret
= clk_enable(vpu
->clk
);
262 /* Enable VPU watchdog */
263 mutex_lock(&vpu
->vpu_mutex
);
264 if (!vpu
->wdt_refcnt
++)
266 vpu_cfg_readl(vpu
, VPU_WDT_REG
) | (1L << 31),
268 mutex_unlock(&vpu
->vpu_mutex
);
273 static void vpu_dump_status(struct mtk_vpu
*vpu
)
276 "vpu: run %x, pc = 0x%x, ra = 0x%x, sp = 0x%x, idle = 0x%x\n"
277 "vpu: int %x, hv = 0x%x, vh = 0x%x, wdt = 0x%x\n",
278 vpu_running(vpu
), vpu_cfg_readl(vpu
, VPU_PC_REG
),
279 vpu_cfg_readl(vpu
, VPU_RA_REG
), vpu_cfg_readl(vpu
, VPU_SP_REG
),
280 vpu_cfg_readl(vpu
, VPU_IDLE_REG
),
281 vpu_cfg_readl(vpu
, VPU_INT_STATUS
),
282 vpu_cfg_readl(vpu
, HOST_TO_VPU
),
283 vpu_cfg_readl(vpu
, VPU_TO_HOST
),
284 vpu_cfg_readl(vpu
, VPU_WDT_REG
));
287 int vpu_ipi_register(struct platform_device
*pdev
,
288 enum ipi_id id
, ipi_handler_t handler
,
289 const char *name
, void *priv
)
291 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
292 struct vpu_ipi_desc
*ipi_desc
;
295 dev_err(&pdev
->dev
, "vpu device in not ready\n");
296 return -EPROBE_DEFER
;
299 if (id
< IPI_MAX
&& handler
) {
300 ipi_desc
= vpu
->ipi_desc
;
301 ipi_desc
[id
].name
= name
;
302 ipi_desc
[id
].handler
= handler
;
303 ipi_desc
[id
].priv
= priv
;
307 dev_err(&pdev
->dev
, "register vpu ipi id %d with invalid arguments\n",
311 EXPORT_SYMBOL_GPL(vpu_ipi_register
);
313 int vpu_ipi_send(struct platform_device
*pdev
,
314 enum ipi_id id
, void *buf
,
317 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
318 struct share_obj __iomem
*send_obj
= vpu
->send_buf
;
319 unsigned long timeout
;
322 if (id
<= IPI_VPU_INIT
|| id
>= IPI_MAX
||
323 len
> sizeof(send_obj
->share_buf
) || !buf
) {
324 dev_err(vpu
->dev
, "failed to send ipi message\n");
328 ret
= vpu_clock_enable(vpu
);
330 dev_err(vpu
->dev
, "failed to enable vpu clock\n");
333 if (!vpu_running(vpu
)) {
334 dev_err(vpu
->dev
, "vpu_ipi_send: VPU is not running\n");
339 mutex_lock(&vpu
->vpu_mutex
);
341 /* Wait until VPU receives the last command */
342 timeout
= jiffies
+ msecs_to_jiffies(IPI_TIMEOUT_MS
);
344 if (time_after(jiffies
, timeout
)) {
345 dev_err(vpu
->dev
, "vpu_ipi_send: IPI timeout!\n");
347 vpu_dump_status(vpu
);
350 } while (vpu_cfg_readl(vpu
, HOST_TO_VPU
));
352 memcpy_toio(send_obj
->share_buf
, buf
, len
);
353 writel(len
, &send_obj
->len
);
354 writel(id
, &send_obj
->id
);
356 vpu
->ipi_id_ack
[id
] = false;
357 /* send the command to VPU */
358 vpu_cfg_writel(vpu
, 0x1, HOST_TO_VPU
);
360 mutex_unlock(&vpu
->vpu_mutex
);
362 /* wait for VPU's ACK */
363 timeout
= msecs_to_jiffies(IPI_TIMEOUT_MS
);
364 ret
= wait_event_timeout(vpu
->ack_wq
, vpu
->ipi_id_ack
[id
], timeout
);
365 vpu
->ipi_id_ack
[id
] = false;
367 dev_err(vpu
->dev
, "vpu ipi %d ack time out !\n", id
);
369 vpu_dump_status(vpu
);
372 vpu_clock_disable(vpu
);
377 mutex_unlock(&vpu
->vpu_mutex
);
379 vpu_clock_disable(vpu
);
383 EXPORT_SYMBOL_GPL(vpu_ipi_send
);
385 static void vpu_wdt_reset_func(struct work_struct
*ws
)
387 struct vpu_wdt
*wdt
= container_of(ws
, struct vpu_wdt
, ws
);
388 struct mtk_vpu
*vpu
= container_of(wdt
, struct mtk_vpu
, wdt
);
389 struct vpu_wdt_handler
*handler
= wdt
->handler
;
392 dev_info(vpu
->dev
, "vpu reset\n");
393 ret
= vpu_clock_enable(vpu
);
395 dev_err(vpu
->dev
, "[VPU] wdt enables clock failed %d\n", ret
);
398 mutex_lock(&vpu
->vpu_mutex
);
399 vpu_cfg_writel(vpu
, 0x0, VPU_RESET
);
400 vpu
->fw_loaded
= false;
401 mutex_unlock(&vpu
->vpu_mutex
);
402 vpu_clock_disable(vpu
);
404 for (index
= 0; index
< VPU_RST_MAX
; index
++) {
405 if (handler
[index
].reset_func
) {
406 handler
[index
].reset_func(handler
[index
].priv
);
407 dev_dbg(vpu
->dev
, "wdt handler func %d\n", index
);
412 int vpu_wdt_reg_handler(struct platform_device
*pdev
,
413 void wdt_reset(void *),
414 void *priv
, enum rst_id id
)
416 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
417 struct vpu_wdt_handler
*handler
;
420 dev_err(&pdev
->dev
, "vpu device in not ready\n");
421 return -EPROBE_DEFER
;
424 handler
= vpu
->wdt
.handler
;
426 if (id
< VPU_RST_MAX
&& wdt_reset
) {
427 dev_dbg(vpu
->dev
, "wdt register id %d\n", id
);
428 mutex_lock(&vpu
->vpu_mutex
);
429 handler
[id
].reset_func
= wdt_reset
;
430 handler
[id
].priv
= priv
;
431 mutex_unlock(&vpu
->vpu_mutex
);
435 dev_err(vpu
->dev
, "register vpu wdt handler failed\n");
438 EXPORT_SYMBOL_GPL(vpu_wdt_reg_handler
);
440 unsigned int vpu_get_vdec_hw_capa(struct platform_device
*pdev
)
442 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
444 return vpu
->run
.dec_capability
;
446 EXPORT_SYMBOL_GPL(vpu_get_vdec_hw_capa
);
448 unsigned int vpu_get_venc_hw_capa(struct platform_device
*pdev
)
450 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
452 return vpu
->run
.enc_capability
;
454 EXPORT_SYMBOL_GPL(vpu_get_venc_hw_capa
);
456 void *vpu_mapping_dm_addr(struct platform_device
*pdev
,
459 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
461 if (!dtcm_dmem_addr
||
462 (dtcm_dmem_addr
> (VPU_DTCM_SIZE
+ VPU_EXT_D_SIZE
))) {
463 dev_err(vpu
->dev
, "invalid virtual data memory address\n");
464 return ERR_PTR(-EINVAL
);
467 if (dtcm_dmem_addr
< VPU_DTCM_SIZE
)
468 return (__force
void *)(dtcm_dmem_addr
+ vpu
->reg
.tcm
+
471 return vpu
->extmem
[D_FW
].va
+ (dtcm_dmem_addr
- VPU_DTCM_SIZE
);
473 EXPORT_SYMBOL_GPL(vpu_mapping_dm_addr
);
475 struct platform_device
*vpu_get_plat_device(struct platform_device
*pdev
)
477 struct device
*dev
= &pdev
->dev
;
478 struct device_node
*vpu_node
;
479 struct platform_device
*vpu_pdev
;
481 vpu_node
= of_parse_phandle(dev
->of_node
, "mediatek,vpu", 0);
483 dev_err(dev
, "can't get vpu node\n");
487 vpu_pdev
= of_find_device_by_node(vpu_node
);
488 of_node_put(vpu_node
);
489 if (WARN_ON(!vpu_pdev
)) {
490 dev_err(dev
, "vpu pdev failed\n");
496 EXPORT_SYMBOL_GPL(vpu_get_plat_device
);
498 /* load vpu program/data memory */
499 static int load_requested_vpu(struct mtk_vpu
*vpu
,
502 size_t tcm_size
= fw_type
? VPU_DTCM_SIZE
: VPU_PTCM_SIZE
;
503 size_t fw_size
= fw_type
? VPU_D_FW_SIZE
: VPU_P_FW_SIZE
;
504 char *fw_name
= fw_type
? VPU_D_FW
: VPU_P_FW
;
505 char *fw_new_name
= fw_type
? VPU_D_FW_NEW
: VPU_P_FW_NEW
;
506 const struct firmware
*vpu_fw
;
508 size_t extra_fw_size
= 0;
512 ret
= request_firmware(&vpu_fw
, fw_new_name
, vpu
->dev
);
514 dev_info(vpu
->dev
, "Failed to load %s, %d, retry\n",
517 ret
= request_firmware(&vpu_fw
, fw_name
, vpu
->dev
);
519 dev_err(vpu
->dev
, "Failed to load %s, %d\n", fw_name
,
524 dl_size
= vpu_fw
->size
;
525 if (dl_size
> fw_size
) {
526 dev_err(vpu
->dev
, "fw %s size %zu is abnormal\n", fw_name
,
528 release_firmware(vpu_fw
);
531 dev_dbg(vpu
->dev
, "Downloaded fw %s size: %zu.\n",
535 vpu_cfg_writel(vpu
, 0x0, VPU_RESET
);
537 /* handle extended firmware size */
538 if (dl_size
> tcm_size
) {
539 dev_dbg(vpu
->dev
, "fw size %zu > limited fw size %zu\n",
541 extra_fw_size
= dl_size
- tcm_size
;
542 dev_dbg(vpu
->dev
, "extra_fw_size %zu\n", extra_fw_size
);
545 dest
= (__force
void *)vpu
->reg
.tcm
;
547 dest
+= VPU_DTCM_OFFSET
;
548 memcpy(dest
, vpu_fw
->data
, dl_size
);
549 /* download to extended memory if need */
550 if (extra_fw_size
> 0) {
551 dest
= vpu
->extmem
[fw_type
].va
;
552 dev_dbg(vpu
->dev
, "download extended memory type %x\n",
554 memcpy(dest
, vpu_fw
->data
+ tcm_size
, extra_fw_size
);
557 release_firmware(vpu_fw
);
562 int vpu_load_firmware(struct platform_device
*pdev
)
565 struct device
*dev
= &pdev
->dev
;
570 dev_err(dev
, "VPU platform device is invalid\n");
574 vpu
= platform_get_drvdata(pdev
);
577 mutex_lock(&vpu
->vpu_mutex
);
578 if (vpu
->fw_loaded
) {
579 mutex_unlock(&vpu
->vpu_mutex
);
582 mutex_unlock(&vpu
->vpu_mutex
);
584 ret
= vpu_clock_enable(vpu
);
586 dev_err(dev
, "enable clock failed %d\n", ret
);
590 mutex_lock(&vpu
->vpu_mutex
);
592 run
->signaled
= false;
593 dev_dbg(vpu
->dev
, "firmware request\n");
594 /* Downloading program firmware to device*/
595 ret
= load_requested_vpu(vpu
, P_FW
);
597 dev_err(dev
, "Failed to request %s, %d\n", VPU_P_FW
, ret
);
601 /* Downloading data firmware to device */
602 ret
= load_requested_vpu(vpu
, D_FW
);
604 dev_err(dev
, "Failed to request %s, %d\n", VPU_D_FW
, ret
);
608 vpu
->fw_loaded
= true;
610 vpu_cfg_writel(vpu
, 0x1, VPU_RESET
);
612 ret
= wait_event_interruptible_timeout(run
->wq
,
614 msecs_to_jiffies(INIT_TIMEOUT_MS
)
618 dev_err(dev
, "wait vpu initialization timeout!\n");
620 } else if (-ERESTARTSYS
== ret
) {
621 dev_err(dev
, "wait vpu interrupted by a signal!\n");
626 dev_info(dev
, "vpu is ready. Fw version %s\n", run
->fw_ver
);
629 mutex_unlock(&vpu
->vpu_mutex
);
630 vpu_clock_disable(vpu
);
634 EXPORT_SYMBOL_GPL(vpu_load_firmware
);
636 static void vpu_init_ipi_handler(const void *data
, unsigned int len
, void *priv
)
638 struct mtk_vpu
*vpu
= priv
;
639 const struct vpu_run
*run
= data
;
641 vpu
->run
.signaled
= run
->signaled
;
642 strscpy(vpu
->run
.fw_ver
, run
->fw_ver
, sizeof(vpu
->run
.fw_ver
));
643 vpu
->run
.dec_capability
= run
->dec_capability
;
644 vpu
->run
.enc_capability
= run
->enc_capability
;
645 wake_up_interruptible(&vpu
->run
.wq
);
648 #ifdef CONFIG_DEBUG_FS
649 static ssize_t
vpu_debug_read(struct file
*file
, char __user
*user_buf
,
650 size_t count
, loff_t
*ppos
)
654 unsigned int running
, pc
, vpu_to_host
, host_to_vpu
, wdt
, idle
, ra
, sp
;
656 struct device
*dev
= file
->private_data
;
657 struct mtk_vpu
*vpu
= dev_get_drvdata(dev
);
659 ret
= vpu_clock_enable(vpu
);
661 dev_err(vpu
->dev
, "[VPU] enable clock failed %d\n", ret
);
665 /* vpu register status */
666 running
= vpu_running(vpu
);
667 pc
= vpu_cfg_readl(vpu
, VPU_PC_REG
);
668 wdt
= vpu_cfg_readl(vpu
, VPU_WDT_REG
);
669 host_to_vpu
= vpu_cfg_readl(vpu
, HOST_TO_VPU
);
670 vpu_to_host
= vpu_cfg_readl(vpu
, VPU_TO_HOST
);
671 ra
= vpu_cfg_readl(vpu
, VPU_RA_REG
);
672 sp
= vpu_cfg_readl(vpu
, VPU_SP_REG
);
673 idle
= vpu_cfg_readl(vpu
, VPU_IDLE_REG
);
675 vpu_clock_disable(vpu
);
678 len
= snprintf(buf
, sizeof(buf
), "VPU is running\n\n"
682 "Host to VPU: 0x%x\n"
683 "VPU to Host: 0x%x\n"
687 vpu
->run
.fw_ver
, pc
, wdt
,
688 host_to_vpu
, vpu_to_host
, sp
, ra
, idle
);
690 len
= snprintf(buf
, sizeof(buf
), "VPU not running\n");
693 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
696 static const struct file_operations vpu_debug_fops
= {
698 .read
= vpu_debug_read
,
700 #endif /* CONFIG_DEBUG_FS */
702 static void vpu_free_ext_mem(struct mtk_vpu
*vpu
, u8 fw_type
)
704 struct device
*dev
= vpu
->dev
;
705 size_t fw_ext_size
= fw_type
? VPU_EXT_D_SIZE
: VPU_EXT_P_SIZE
;
707 dma_free_coherent(dev
, fw_ext_size
, vpu
->extmem
[fw_type
].va
,
708 vpu
->extmem
[fw_type
].pa
);
711 static int vpu_alloc_ext_mem(struct mtk_vpu
*vpu
, u32 fw_type
)
713 struct device
*dev
= vpu
->dev
;
714 size_t fw_ext_size
= fw_type
? VPU_EXT_D_SIZE
: VPU_EXT_P_SIZE
;
715 u32 vpu_ext_mem0
= fw_type
? VPU_DMEM_EXT0_ADDR
: VPU_PMEM_EXT0_ADDR
;
716 u32 vpu_ext_mem1
= fw_type
? VPU_DMEM_EXT1_ADDR
: VPU_PMEM_EXT1_ADDR
;
717 u32 offset_4gb
= vpu
->enable_4GB
? 0x40000000 : 0;
719 vpu
->extmem
[fw_type
].va
= dma_alloc_coherent(dev
,
721 &vpu
->extmem
[fw_type
].pa
,
723 if (!vpu
->extmem
[fw_type
].va
) {
724 dev_err(dev
, "Failed to allocate the extended program memory\n");
728 /* Disable extend0. Enable extend1 */
729 vpu_cfg_writel(vpu
, 0x1, vpu_ext_mem0
);
730 vpu_cfg_writel(vpu
, (vpu
->extmem
[fw_type
].pa
& 0xFFFFF000) + offset_4gb
,
733 dev_info(dev
, "%s extend memory phy=0x%llx virt=0x%p\n",
734 fw_type
? "Data" : "Program",
735 (unsigned long long)vpu
->extmem
[fw_type
].pa
,
736 vpu
->extmem
[fw_type
].va
);
741 static void vpu_ipi_handler(struct mtk_vpu
*vpu
)
743 struct share_obj __iomem
*rcv_obj
= vpu
->recv_buf
;
744 struct vpu_ipi_desc
*ipi_desc
= vpu
->ipi_desc
;
745 unsigned char data
[SHARE_BUF_SIZE
];
746 s32 id
= readl(&rcv_obj
->id
);
748 memcpy_fromio(data
, rcv_obj
->share_buf
, sizeof(data
));
749 if (id
< IPI_MAX
&& ipi_desc
[id
].handler
) {
750 ipi_desc
[id
].handler(data
, readl(&rcv_obj
->len
),
752 if (id
> IPI_VPU_INIT
) {
753 vpu
->ipi_id_ack
[id
] = true;
754 wake_up(&vpu
->ack_wq
);
757 dev_err(vpu
->dev
, "No such ipi id = %d\n", id
);
761 static int vpu_ipi_init(struct mtk_vpu
*vpu
)
763 /* Disable VPU to host interrupt */
764 vpu_cfg_writel(vpu
, 0x0, VPU_TO_HOST
);
766 /* shared buffer initialization */
767 vpu
->recv_buf
= vpu
->reg
.tcm
+ VPU_DTCM_OFFSET
;
768 vpu
->send_buf
= vpu
->recv_buf
+ 1;
769 memset_io(vpu
->recv_buf
, 0, sizeof(struct share_obj
));
770 memset_io(vpu
->send_buf
, 0, sizeof(struct share_obj
));
775 static irqreturn_t
vpu_irq_handler(int irq
, void *priv
)
777 struct mtk_vpu
*vpu
= priv
;
782 * Clock should have been enabled already.
783 * Enable again in case vpu_ipi_send times out
784 * and has disabled the clock.
786 ret
= clk_enable(vpu
->clk
);
788 dev_err(vpu
->dev
, "[VPU] enable clock failed %d\n", ret
);
791 vpu_to_host
= vpu_cfg_readl(vpu
, VPU_TO_HOST
);
792 if (vpu_to_host
& VPU_IPC_INT
) {
793 vpu_ipi_handler(vpu
);
795 dev_err(vpu
->dev
, "vpu watchdog timeout! 0x%x", vpu_to_host
);
796 queue_work(vpu
->wdt
.wq
, &vpu
->wdt
.ws
);
799 /* VPU won't send another interrupt until we set VPU_TO_HOST to 0. */
800 vpu_cfg_writel(vpu
, 0x0, VPU_TO_HOST
);
801 clk_disable(vpu
->clk
);
806 #ifdef CONFIG_DEBUG_FS
807 static struct dentry
*vpu_debugfs
;
809 static int mtk_vpu_probe(struct platform_device
*pdev
)
813 struct resource
*res
;
816 dev_dbg(&pdev
->dev
, "initialization\n");
819 vpu
= devm_kzalloc(dev
, sizeof(*vpu
), GFP_KERNEL
);
823 vpu
->dev
= &pdev
->dev
;
824 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "tcm");
825 vpu
->reg
.tcm
= devm_ioremap_resource(dev
, res
);
826 if (IS_ERR((__force
void *)vpu
->reg
.tcm
))
827 return PTR_ERR((__force
void *)vpu
->reg
.tcm
);
829 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cfg_reg");
830 vpu
->reg
.cfg
= devm_ioremap_resource(dev
, res
);
831 if (IS_ERR((__force
void *)vpu
->reg
.cfg
))
832 return PTR_ERR((__force
void *)vpu
->reg
.cfg
);
835 vpu
->clk
= devm_clk_get(dev
, "main");
836 if (IS_ERR(vpu
->clk
)) {
837 dev_err(dev
, "get vpu clock failed\n");
838 return PTR_ERR(vpu
->clk
);
841 platform_set_drvdata(pdev
, vpu
);
843 ret
= clk_prepare(vpu
->clk
);
845 dev_err(dev
, "prepare vpu clock failed\n");
850 vpu
->wdt
.wq
= create_singlethread_workqueue("vpu_wdt");
852 dev_err(dev
, "initialize wdt workqueue failed\n");
855 INIT_WORK(&vpu
->wdt
.ws
, vpu_wdt_reset_func
);
856 mutex_init(&vpu
->vpu_mutex
);
858 ret
= vpu_clock_enable(vpu
);
860 dev_err(dev
, "enable vpu clock failed\n");
861 goto workqueue_destroy
;
864 dev_dbg(dev
, "vpu ipi init\n");
865 ret
= vpu_ipi_init(vpu
);
867 dev_err(dev
, "Failed to init ipi\n");
868 goto disable_vpu_clk
;
871 /* register vpu initialization IPI */
872 ret
= vpu_ipi_register(pdev
, IPI_VPU_INIT
, vpu_init_ipi_handler
,
875 dev_err(dev
, "Failed to register IPI_VPU_INIT\n");
876 goto vpu_mutex_destroy
;
879 #ifdef CONFIG_DEBUG_FS
880 vpu_debugfs
= debugfs_create_file("mtk_vpu", S_IRUGO
, NULL
, (void *)dev
,
884 /* Set PTCM to 96K and DTCM to 32K */
885 vpu_cfg_writel(vpu
, 0x2, VPU_TCM_CFG
);
887 vpu
->enable_4GB
= !!(totalram_pages() > (SZ_2G
>> PAGE_SHIFT
));
888 dev_info(dev
, "4GB mode %u\n", vpu
->enable_4GB
);
890 if (vpu
->enable_4GB
) {
891 ret
= of_reserved_mem_device_init(dev
);
893 dev_info(dev
, "init reserved memory failed\n");
894 /* continue to use dynamic allocation if failed */
897 ret
= vpu_alloc_ext_mem(vpu
, D_FW
);
899 dev_err(dev
, "Allocate DM failed\n");
903 ret
= vpu_alloc_ext_mem(vpu
, P_FW
);
905 dev_err(dev
, "Allocate PM failed\n");
909 init_waitqueue_head(&vpu
->run
.wq
);
910 init_waitqueue_head(&vpu
->ack_wq
);
912 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
914 dev_err(dev
, "get IRQ resource failed.\n");
918 vpu
->reg
.irq
= platform_get_irq(pdev
, 0);
919 ret
= devm_request_irq(dev
, vpu
->reg
.irq
, vpu_irq_handler
, 0,
922 dev_err(dev
, "failed to request irq\n");
926 vpu_clock_disable(vpu
);
927 dev_dbg(dev
, "initialization completed\n");
932 vpu_free_ext_mem(vpu
, P_FW
);
934 vpu_free_ext_mem(vpu
, D_FW
);
936 of_reserved_mem_device_release(dev
);
937 #ifdef CONFIG_DEBUG_FS
938 debugfs_remove(vpu_debugfs
);
940 memset(vpu
->ipi_desc
, 0, sizeof(struct vpu_ipi_desc
) * IPI_MAX
);
942 mutex_destroy(&vpu
->vpu_mutex
);
944 vpu_clock_disable(vpu
);
946 destroy_workqueue(vpu
->wdt
.wq
);
951 static const struct of_device_id mtk_vpu_match
[] = {
953 .compatible
= "mediatek,mt8173-vpu",
957 MODULE_DEVICE_TABLE(of
, mtk_vpu_match
);
959 static int mtk_vpu_remove(struct platform_device
*pdev
)
961 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
963 #ifdef CONFIG_DEBUG_FS
964 debugfs_remove(vpu_debugfs
);
967 flush_workqueue(vpu
->wdt
.wq
);
968 destroy_workqueue(vpu
->wdt
.wq
);
970 vpu_free_ext_mem(vpu
, P_FW
);
971 vpu_free_ext_mem(vpu
, D_FW
);
972 mutex_destroy(&vpu
->vpu_mutex
);
973 clk_unprepare(vpu
->clk
);
978 static int mtk_vpu_suspend(struct device
*dev
)
980 struct mtk_vpu
*vpu
= dev_get_drvdata(dev
);
981 unsigned long timeout
;
984 ret
= vpu_clock_enable(vpu
);
986 dev_err(dev
, "failed to enable vpu clock\n");
990 mutex_lock(&vpu
->vpu_mutex
);
991 /* disable vpu timer interrupt */
992 vpu_cfg_writel(vpu
, vpu_cfg_readl(vpu
, VPU_INT_STATUS
) | VPU_IDLE_STATE
,
994 /* check if vpu is idle for system suspend */
995 timeout
= jiffies
+ msecs_to_jiffies(VPU_IDLE_TIMEOUT_MS
);
997 if (time_after(jiffies
, timeout
)) {
998 dev_err(dev
, "vpu idle timeout\n");
999 mutex_unlock(&vpu
->vpu_mutex
);
1000 vpu_clock_disable(vpu
);
1003 } while (!vpu_cfg_readl(vpu
, VPU_IDLE_REG
));
1005 mutex_unlock(&vpu
->vpu_mutex
);
1006 vpu_clock_disable(vpu
);
1007 clk_unprepare(vpu
->clk
);
1012 static int mtk_vpu_resume(struct device
*dev
)
1014 struct mtk_vpu
*vpu
= dev_get_drvdata(dev
);
1017 clk_prepare(vpu
->clk
);
1018 ret
= vpu_clock_enable(vpu
);
1020 dev_err(dev
, "failed to enable vpu clock\n");
1024 mutex_lock(&vpu
->vpu_mutex
);
1025 /* enable vpu timer interrupt */
1027 vpu_cfg_readl(vpu
, VPU_INT_STATUS
) & ~(VPU_IDLE_STATE
),
1029 mutex_unlock(&vpu
->vpu_mutex
);
1030 vpu_clock_disable(vpu
);
1035 static const struct dev_pm_ops mtk_vpu_pm
= {
1036 .suspend
= mtk_vpu_suspend
,
1037 .resume
= mtk_vpu_resume
,
1040 static struct platform_driver mtk_vpu_driver
= {
1041 .probe
= mtk_vpu_probe
,
1042 .remove
= mtk_vpu_remove
,
1046 .of_match_table
= mtk_vpu_match
,
1050 module_platform_driver(mtk_vpu_driver
);
1052 MODULE_LICENSE("GPL v2");
1053 MODULE_DESCRIPTION("Mediatek Video Processor Unit driver");