2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 #include <linux/clk.h>
15 #include <linux/debugfs.h>
16 #include <linux/firmware.h>
17 #include <linux/interrupt.h>
18 #include <linux/iommu.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/of_reserved_mem.h>
24 #include <linux/sched.h>
25 #include <linux/sizes.h>
30 * VPU (video processor unit) is a tiny processor controlling video hardware
31 * related to video codec, scaling and color format converting.
32 * VPU interfaces with other blocks by share memory and interrupt.
35 #define INIT_TIMEOUT_MS 2000U
36 #define IPI_TIMEOUT_MS 2000U
37 #define VPU_FW_VER_LEN 16
39 /* maximum program/data TCM (Tightly-Coupled Memory) size */
40 #define VPU_PTCM_SIZE (96 * SZ_1K)
41 #define VPU_DTCM_SIZE (32 * SZ_1K)
42 /* the offset to get data tcm address */
43 #define VPU_DTCM_OFFSET 0x18000UL
44 /* daynamic allocated maximum extended memory size */
45 #define VPU_EXT_P_SIZE SZ_1M
46 #define VPU_EXT_D_SIZE SZ_4M
47 /* maximum binary firmware size */
48 #define VPU_P_FW_SIZE (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
49 #define VPU_D_FW_SIZE (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
50 /* the size of share buffer between Host and VPU */
51 #define SHARE_BUF_SIZE 48
53 /* binary firmware name */
54 #define VPU_P_FW "vpu_p.bin"
55 #define VPU_D_FW "vpu_d.bin"
58 #define VPU_TCM_CFG 0x0008
59 #define VPU_PMEM_EXT0_ADDR 0x000C
60 #define VPU_PMEM_EXT1_ADDR 0x0010
61 #define VPU_TO_HOST 0x001C
62 #define VPU_DMEM_EXT0_ADDR 0x0014
63 #define VPU_DMEM_EXT1_ADDR 0x0018
64 #define HOST_TO_VPU 0x0024
65 #define VPU_PC_REG 0x0060
66 #define VPU_WDT_REG 0x0084
68 /* vpu inter-processor communication interrupt */
69 #define VPU_IPC_INT BIT(8)
72 * enum vpu_fw_type - VPU firmware type
74 * @P_FW: program firmware
75 * @D_FW: data firmware
84 * struct vpu_mem - VPU extended program/data memory information
86 * @va: the kernel virtual memory address of VPU extended memory
87 * @pa: the physical memory address of VPU extended memory
96 * struct vpu_regs - VPU TCM and configuration registers
98 * @tcm: the register for VPU Tightly-Coupled Memory
99 * @cfg: the register for VPU configuration
100 * @irq: the irq number for VPU interrupt
109 * struct vpu_wdt_handler - VPU watchdog reset handler
111 * @reset_func: reset handler
112 * @priv: private data
114 struct vpu_wdt_handler
{
115 void (*reset_func
)(void *);
120 * struct vpu_wdt - VPU watchdog workqueue
122 * @handler: VPU watchdog reset handler
123 * @ws: workstruct for VPU watchdog
124 * @wq: workqueue for VPU watchdog
127 struct vpu_wdt_handler handler
[VPU_RST_MAX
];
128 struct work_struct ws
;
129 struct workqueue_struct
*wq
;
133 * struct vpu_run - VPU initialization status
135 * @signaled: the signal of vpu initialization completed
136 * @fw_ver: VPU firmware version
137 * @dec_capability: decoder capability which is not used for now and
138 * the value is reserved for future use
139 * @enc_capability: encoder capability which is not used for now and
140 * the value is reserved for future use
141 * @wq: wait queue for VPU initialization status
145 char fw_ver
[VPU_FW_VER_LEN
];
146 unsigned int dec_capability
;
147 unsigned int enc_capability
;
148 wait_queue_head_t wq
;
152 * struct vpu_ipi_desc - VPU IPI descriptor
154 * @handler: IPI handler
155 * @name: the name of IPI handler
156 * @priv: the private data of IPI handler
158 struct vpu_ipi_desc
{
159 ipi_handler_t handler
;
165 * struct share_obj - DTCM (Data Tightly-Coupled Memory) buffer shared with
169 * @len: share buffer length
170 * @share_buf: share buffer data
175 unsigned char share_buf
[SHARE_BUF_SIZE
];
179 * struct mtk_vpu - vpu driver data
180 * @extmem: VPU extended memory information
181 * @reg: VPU TCM and configuration registers
182 * @run: VPU initialization status
183 * @ipi_desc: VPU IPI descriptor
184 * @recv_buf: VPU DTCM share buffer for receiving. The
185 * receive buffer is only accessed in interrupt context.
186 * @send_buf: VPU DTCM share buffer for sending
187 * @dev: VPU struct device
188 * @clk: VPU clock on/off
189 * @fw_loaded: indicate VPU firmware loaded
190 * @enable_4GB: VPU 4GB mode on/off
191 * @vpu_mutex: protect mtk_vpu (except recv_buf) and ensure only
192 * one client to use VPU service at a time. For example,
193 * suppose a client is using VPU to decode VP8.
194 * If the other client wants to encode VP8,
195 * it has to wait until VP8 decode completes.
196 * @wdt_refcnt WDT reference count to make sure the watchdog can be
197 * disabled if no other client is using VPU service
198 * @ack_wq: The wait queue for each codec and mdp. When sleeping
199 * processes wake up, they will check the condition
200 * "ipi_id_ack" to run the corresponding action or
202 * @ipi_id_ack: The ACKs for registered IPI function sending
207 struct vpu_mem extmem
[2];
211 struct vpu_ipi_desc ipi_desc
[IPI_MAX
];
212 struct share_obj
*recv_buf
;
213 struct share_obj
*send_buf
;
218 struct mutex vpu_mutex
; /* for protecting vpu data data structure */
220 wait_queue_head_t ack_wq
;
221 bool ipi_id_ack
[IPI_MAX
];
224 static inline void vpu_cfg_writel(struct mtk_vpu
*vpu
, u32 val
, u32 offset
)
226 writel(val
, vpu
->reg
.cfg
+ offset
);
229 static inline u32
vpu_cfg_readl(struct mtk_vpu
*vpu
, u32 offset
)
231 return readl(vpu
->reg
.cfg
+ offset
);
234 static inline bool vpu_running(struct mtk_vpu
*vpu
)
236 return vpu_cfg_readl(vpu
, VPU_RESET
) & BIT(0);
239 static void vpu_clock_disable(struct mtk_vpu
*vpu
)
241 /* Disable VPU watchdog */
242 mutex_lock(&vpu
->vpu_mutex
);
243 if (!--vpu
->wdt_refcnt
)
245 vpu_cfg_readl(vpu
, VPU_WDT_REG
) & ~(1L << 31),
247 mutex_unlock(&vpu
->vpu_mutex
);
249 clk_disable(vpu
->clk
);
252 static int vpu_clock_enable(struct mtk_vpu
*vpu
)
256 ret
= clk_enable(vpu
->clk
);
259 /* Enable VPU watchdog */
260 mutex_lock(&vpu
->vpu_mutex
);
261 if (!vpu
->wdt_refcnt
++)
263 vpu_cfg_readl(vpu
, VPU_WDT_REG
) | (1L << 31),
265 mutex_unlock(&vpu
->vpu_mutex
);
270 int vpu_ipi_register(struct platform_device
*pdev
,
271 enum ipi_id id
, ipi_handler_t handler
,
272 const char *name
, void *priv
)
274 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
275 struct vpu_ipi_desc
*ipi_desc
;
278 dev_err(&pdev
->dev
, "vpu device in not ready\n");
279 return -EPROBE_DEFER
;
282 if (id
>= 0 && id
< IPI_MAX
&& handler
) {
283 ipi_desc
= vpu
->ipi_desc
;
284 ipi_desc
[id
].name
= name
;
285 ipi_desc
[id
].handler
= handler
;
286 ipi_desc
[id
].priv
= priv
;
290 dev_err(&pdev
->dev
, "register vpu ipi id %d with invalid arguments\n",
294 EXPORT_SYMBOL_GPL(vpu_ipi_register
);
296 int vpu_ipi_send(struct platform_device
*pdev
,
297 enum ipi_id id
, void *buf
,
300 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
301 struct share_obj
*send_obj
= vpu
->send_buf
;
302 unsigned long timeout
;
305 if (id
<= IPI_VPU_INIT
|| id
>= IPI_MAX
||
306 len
> sizeof(send_obj
->share_buf
) || !buf
) {
307 dev_err(vpu
->dev
, "failed to send ipi message\n");
311 ret
= vpu_clock_enable(vpu
);
313 dev_err(vpu
->dev
, "failed to enable vpu clock\n");
316 if (!vpu_running(vpu
)) {
317 dev_err(vpu
->dev
, "vpu_ipi_send: VPU is not running\n");
322 mutex_lock(&vpu
->vpu_mutex
);
324 /* Wait until VPU receives the last command */
325 timeout
= jiffies
+ msecs_to_jiffies(IPI_TIMEOUT_MS
);
327 if (time_after(jiffies
, timeout
)) {
328 dev_err(vpu
->dev
, "vpu_ipi_send: IPI timeout!\n");
332 } while (vpu_cfg_readl(vpu
, HOST_TO_VPU
));
334 memcpy((void *)send_obj
->share_buf
, buf
, len
);
338 vpu
->ipi_id_ack
[id
] = false;
339 /* send the command to VPU */
340 vpu_cfg_writel(vpu
, 0x1, HOST_TO_VPU
);
342 mutex_unlock(&vpu
->vpu_mutex
);
344 /* wait for VPU's ACK */
345 timeout
= msecs_to_jiffies(IPI_TIMEOUT_MS
);
346 ret
= wait_event_timeout(vpu
->ack_wq
, vpu
->ipi_id_ack
[id
], timeout
);
347 vpu
->ipi_id_ack
[id
] = false;
349 dev_err(vpu
->dev
, "vpu ipi %d ack time out !", id
);
353 vpu_clock_disable(vpu
);
358 mutex_unlock(&vpu
->vpu_mutex
);
360 vpu_clock_disable(vpu
);
364 EXPORT_SYMBOL_GPL(vpu_ipi_send
);
366 static void vpu_wdt_reset_func(struct work_struct
*ws
)
368 struct vpu_wdt
*wdt
= container_of(ws
, struct vpu_wdt
, ws
);
369 struct mtk_vpu
*vpu
= container_of(wdt
, struct mtk_vpu
, wdt
);
370 struct vpu_wdt_handler
*handler
= wdt
->handler
;
373 dev_info(vpu
->dev
, "vpu reset\n");
374 ret
= vpu_clock_enable(vpu
);
376 dev_err(vpu
->dev
, "[VPU] wdt enables clock failed %d\n", ret
);
379 mutex_lock(&vpu
->vpu_mutex
);
380 vpu_cfg_writel(vpu
, 0x0, VPU_RESET
);
381 vpu
->fw_loaded
= false;
382 mutex_unlock(&vpu
->vpu_mutex
);
383 vpu_clock_disable(vpu
);
385 for (index
= 0; index
< VPU_RST_MAX
; index
++) {
386 if (handler
[index
].reset_func
) {
387 handler
[index
].reset_func(handler
[index
].priv
);
388 dev_dbg(vpu
->dev
, "wdt handler func %d\n", index
);
393 int vpu_wdt_reg_handler(struct platform_device
*pdev
,
394 void wdt_reset(void *),
395 void *priv
, enum rst_id id
)
397 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
398 struct vpu_wdt_handler
*handler
;
401 dev_err(&pdev
->dev
, "vpu device in not ready\n");
402 return -EPROBE_DEFER
;
405 handler
= vpu
->wdt
.handler
;
407 if (id
>= 0 && id
< VPU_RST_MAX
&& wdt_reset
) {
408 dev_dbg(vpu
->dev
, "wdt register id %d\n", id
);
409 mutex_lock(&vpu
->vpu_mutex
);
410 handler
[id
].reset_func
= wdt_reset
;
411 handler
[id
].priv
= priv
;
412 mutex_unlock(&vpu
->vpu_mutex
);
416 dev_err(vpu
->dev
, "register vpu wdt handler failed\n");
419 EXPORT_SYMBOL_GPL(vpu_wdt_reg_handler
);
421 unsigned int vpu_get_vdec_hw_capa(struct platform_device
*pdev
)
423 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
425 return vpu
->run
.dec_capability
;
427 EXPORT_SYMBOL_GPL(vpu_get_vdec_hw_capa
);
429 unsigned int vpu_get_venc_hw_capa(struct platform_device
*pdev
)
431 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
433 return vpu
->run
.enc_capability
;
435 EXPORT_SYMBOL_GPL(vpu_get_venc_hw_capa
);
437 void *vpu_mapping_dm_addr(struct platform_device
*pdev
,
440 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
442 if (!dtcm_dmem_addr
||
443 (dtcm_dmem_addr
> (VPU_DTCM_SIZE
+ VPU_EXT_D_SIZE
))) {
444 dev_err(vpu
->dev
, "invalid virtual data memory address\n");
445 return ERR_PTR(-EINVAL
);
448 if (dtcm_dmem_addr
< VPU_DTCM_SIZE
)
449 return (__force
void *)(dtcm_dmem_addr
+ vpu
->reg
.tcm
+
452 return vpu
->extmem
[D_FW
].va
+ (dtcm_dmem_addr
- VPU_DTCM_SIZE
);
454 EXPORT_SYMBOL_GPL(vpu_mapping_dm_addr
);
456 struct platform_device
*vpu_get_plat_device(struct platform_device
*pdev
)
458 struct device
*dev
= &pdev
->dev
;
459 struct device_node
*vpu_node
;
460 struct platform_device
*vpu_pdev
;
462 vpu_node
= of_parse_phandle(dev
->of_node
, "mediatek,vpu", 0);
464 dev_err(dev
, "can't get vpu node\n");
468 vpu_pdev
= of_find_device_by_node(vpu_node
);
469 if (WARN_ON(!vpu_pdev
)) {
470 dev_err(dev
, "vpu pdev failed\n");
471 of_node_put(vpu_node
);
477 EXPORT_SYMBOL_GPL(vpu_get_plat_device
);
479 /* load vpu program/data memory */
480 static int load_requested_vpu(struct mtk_vpu
*vpu
,
481 const struct firmware
*vpu_fw
,
484 size_t tcm_size
= fw_type
? VPU_DTCM_SIZE
: VPU_PTCM_SIZE
;
485 size_t fw_size
= fw_type
? VPU_D_FW_SIZE
: VPU_P_FW_SIZE
;
486 char *fw_name
= fw_type
? VPU_D_FW
: VPU_P_FW
;
488 size_t extra_fw_size
= 0;
492 ret
= request_firmware(&vpu_fw
, fw_name
, vpu
->dev
);
494 dev_err(vpu
->dev
, "Failed to load %s, %d\n", fw_name
, ret
);
497 dl_size
= vpu_fw
->size
;
498 if (dl_size
> fw_size
) {
499 dev_err(vpu
->dev
, "fw %s size %zu is abnormal\n", fw_name
,
501 release_firmware(vpu_fw
);
504 dev_dbg(vpu
->dev
, "Downloaded fw %s size: %zu.\n",
508 vpu_cfg_writel(vpu
, 0x0, VPU_RESET
);
510 /* handle extended firmware size */
511 if (dl_size
> tcm_size
) {
512 dev_dbg(vpu
->dev
, "fw size %zu > limited fw size %zu\n",
514 extra_fw_size
= dl_size
- tcm_size
;
515 dev_dbg(vpu
->dev
, "extra_fw_size %zu\n", extra_fw_size
);
518 dest
= (__force
void *)vpu
->reg
.tcm
;
520 dest
+= VPU_DTCM_OFFSET
;
521 memcpy(dest
, vpu_fw
->data
, dl_size
);
522 /* download to extended memory if need */
523 if (extra_fw_size
> 0) {
524 dest
= vpu
->extmem
[fw_type
].va
;
525 dev_dbg(vpu
->dev
, "download extended memory type %x\n",
527 memcpy(dest
, vpu_fw
->data
+ tcm_size
, extra_fw_size
);
530 release_firmware(vpu_fw
);
535 int vpu_load_firmware(struct platform_device
*pdev
)
538 struct device
*dev
= &pdev
->dev
;
540 const struct firmware
*vpu_fw
= NULL
;
544 dev_err(dev
, "VPU platform device is invalid\n");
548 vpu
= platform_get_drvdata(pdev
);
551 mutex_lock(&vpu
->vpu_mutex
);
552 if (vpu
->fw_loaded
) {
553 mutex_unlock(&vpu
->vpu_mutex
);
556 mutex_unlock(&vpu
->vpu_mutex
);
558 ret
= vpu_clock_enable(vpu
);
560 dev_err(dev
, "enable clock failed %d\n", ret
);
564 mutex_lock(&vpu
->vpu_mutex
);
566 run
->signaled
= false;
567 dev_dbg(vpu
->dev
, "firmware request\n");
568 /* Downloading program firmware to device*/
569 ret
= load_requested_vpu(vpu
, vpu_fw
, P_FW
);
571 dev_err(dev
, "Failed to request %s, %d\n", VPU_P_FW
, ret
);
575 /* Downloading data firmware to device */
576 ret
= load_requested_vpu(vpu
, vpu_fw
, D_FW
);
578 dev_err(dev
, "Failed to request %s, %d\n", VPU_D_FW
, ret
);
582 vpu
->fw_loaded
= true;
584 vpu_cfg_writel(vpu
, 0x1, VPU_RESET
);
586 ret
= wait_event_interruptible_timeout(run
->wq
,
588 msecs_to_jiffies(INIT_TIMEOUT_MS
)
592 dev_err(dev
, "wait vpu initialization timout!\n");
594 } else if (-ERESTARTSYS
== ret
) {
595 dev_err(dev
, "wait vpu interrupted by a signal!\n");
600 dev_info(dev
, "vpu is ready. Fw version %s\n", run
->fw_ver
);
603 mutex_unlock(&vpu
->vpu_mutex
);
604 vpu_clock_disable(vpu
);
608 EXPORT_SYMBOL_GPL(vpu_load_firmware
);
610 static void vpu_init_ipi_handler(void *data
, unsigned int len
, void *priv
)
612 struct mtk_vpu
*vpu
= (struct mtk_vpu
*)priv
;
613 struct vpu_run
*run
= (struct vpu_run
*)data
;
615 vpu
->run
.signaled
= run
->signaled
;
616 strncpy(vpu
->run
.fw_ver
, run
->fw_ver
, VPU_FW_VER_LEN
);
617 vpu
->run
.dec_capability
= run
->dec_capability
;
618 vpu
->run
.enc_capability
= run
->enc_capability
;
619 wake_up_interruptible(&vpu
->run
.wq
);
622 #ifdef CONFIG_DEBUG_FS
623 static ssize_t
vpu_debug_read(struct file
*file
, char __user
*user_buf
,
624 size_t count
, loff_t
*ppos
)
628 unsigned int running
, pc
, vpu_to_host
, host_to_vpu
, wdt
;
630 struct device
*dev
= file
->private_data
;
631 struct mtk_vpu
*vpu
= dev_get_drvdata(dev
);
633 ret
= vpu_clock_enable(vpu
);
635 dev_err(vpu
->dev
, "[VPU] enable clock failed %d\n", ret
);
639 /* vpu register status */
640 running
= vpu_running(vpu
);
641 pc
= vpu_cfg_readl(vpu
, VPU_PC_REG
);
642 wdt
= vpu_cfg_readl(vpu
, VPU_WDT_REG
);
643 host_to_vpu
= vpu_cfg_readl(vpu
, HOST_TO_VPU
);
644 vpu_to_host
= vpu_cfg_readl(vpu
, VPU_TO_HOST
);
645 vpu_clock_disable(vpu
);
648 len
= snprintf(buf
, sizeof(buf
), "VPU is running\n\n"
652 "Host to VPU: 0x%x\n"
653 "VPU to Host: 0x%x\n",
654 vpu
->run
.fw_ver
, pc
, wdt
,
655 host_to_vpu
, vpu_to_host
);
657 len
= snprintf(buf
, sizeof(buf
), "VPU not running\n");
660 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, len
);
663 static const struct file_operations vpu_debug_fops
= {
665 .read
= vpu_debug_read
,
667 #endif /* CONFIG_DEBUG_FS */
669 static void vpu_free_ext_mem(struct mtk_vpu
*vpu
, u8 fw_type
)
671 struct device
*dev
= vpu
->dev
;
672 size_t fw_ext_size
= fw_type
? VPU_EXT_D_SIZE
: VPU_EXT_P_SIZE
;
674 dma_free_coherent(dev
, fw_ext_size
, vpu
->extmem
[fw_type
].va
,
675 vpu
->extmem
[fw_type
].pa
);
678 static int vpu_alloc_ext_mem(struct mtk_vpu
*vpu
, u32 fw_type
)
680 struct device
*dev
= vpu
->dev
;
681 size_t fw_ext_size
= fw_type
? VPU_EXT_D_SIZE
: VPU_EXT_P_SIZE
;
682 u32 vpu_ext_mem0
= fw_type
? VPU_DMEM_EXT0_ADDR
: VPU_PMEM_EXT0_ADDR
;
683 u32 vpu_ext_mem1
= fw_type
? VPU_DMEM_EXT1_ADDR
: VPU_PMEM_EXT1_ADDR
;
684 u32 offset_4gb
= vpu
->enable_4GB
? 0x40000000 : 0;
686 vpu
->extmem
[fw_type
].va
= dma_alloc_coherent(dev
,
688 &vpu
->extmem
[fw_type
].pa
,
690 if (!vpu
->extmem
[fw_type
].va
) {
691 dev_err(dev
, "Failed to allocate the extended program memory\n");
695 /* Disable extend0. Enable extend1 */
696 vpu_cfg_writel(vpu
, 0x1, vpu_ext_mem0
);
697 vpu_cfg_writel(vpu
, (vpu
->extmem
[fw_type
].pa
& 0xFFFFF000) + offset_4gb
,
700 dev_info(dev
, "%s extend memory phy=0x%llx virt=0x%p\n",
701 fw_type
? "Data" : "Program",
702 (unsigned long long)vpu
->extmem
[fw_type
].pa
,
703 vpu
->extmem
[fw_type
].va
);
708 static void vpu_ipi_handler(struct mtk_vpu
*vpu
)
710 struct share_obj
*rcv_obj
= vpu
->recv_buf
;
711 struct vpu_ipi_desc
*ipi_desc
= vpu
->ipi_desc
;
713 if (rcv_obj
->id
< IPI_MAX
&& ipi_desc
[rcv_obj
->id
].handler
) {
714 ipi_desc
[rcv_obj
->id
].handler(rcv_obj
->share_buf
,
716 ipi_desc
[rcv_obj
->id
].priv
);
717 if (rcv_obj
->id
> IPI_VPU_INIT
) {
718 vpu
->ipi_id_ack
[rcv_obj
->id
] = true;
719 wake_up(&vpu
->ack_wq
);
722 dev_err(vpu
->dev
, "No such ipi id = %d\n", rcv_obj
->id
);
726 static int vpu_ipi_init(struct mtk_vpu
*vpu
)
728 /* Disable VPU to host interrupt */
729 vpu_cfg_writel(vpu
, 0x0, VPU_TO_HOST
);
731 /* shared buffer initialization */
732 vpu
->recv_buf
= (__force
struct share_obj
*)(vpu
->reg
.tcm
+
734 vpu
->send_buf
= vpu
->recv_buf
+ 1;
735 memset(vpu
->recv_buf
, 0, sizeof(struct share_obj
));
736 memset(vpu
->send_buf
, 0, sizeof(struct share_obj
));
741 static irqreturn_t
vpu_irq_handler(int irq
, void *priv
)
743 struct mtk_vpu
*vpu
= priv
;
748 * Clock should have been enabled already.
749 * Enable again in case vpu_ipi_send times out
750 * and has disabled the clock.
752 ret
= clk_enable(vpu
->clk
);
754 dev_err(vpu
->dev
, "[VPU] enable clock failed %d\n", ret
);
757 vpu_to_host
= vpu_cfg_readl(vpu
, VPU_TO_HOST
);
758 if (vpu_to_host
& VPU_IPC_INT
) {
759 vpu_ipi_handler(vpu
);
761 dev_err(vpu
->dev
, "vpu watchdog timeout! 0x%x", vpu_to_host
);
762 queue_work(vpu
->wdt
.wq
, &vpu
->wdt
.ws
);
765 /* VPU won't send another interrupt until we set VPU_TO_HOST to 0. */
766 vpu_cfg_writel(vpu
, 0x0, VPU_TO_HOST
);
767 clk_disable(vpu
->clk
);
772 #ifdef CONFIG_DEBUG_FS
773 static struct dentry
*vpu_debugfs
;
775 static int mtk_vpu_probe(struct platform_device
*pdev
)
779 struct resource
*res
;
782 dev_dbg(&pdev
->dev
, "initialization\n");
785 vpu
= devm_kzalloc(dev
, sizeof(*vpu
), GFP_KERNEL
);
789 vpu
->dev
= &pdev
->dev
;
790 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "tcm");
791 vpu
->reg
.tcm
= devm_ioremap_resource(dev
, res
);
792 if (IS_ERR((__force
void *)vpu
->reg
.tcm
))
793 return PTR_ERR((__force
void *)vpu
->reg
.tcm
);
795 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cfg_reg");
796 vpu
->reg
.cfg
= devm_ioremap_resource(dev
, res
);
797 if (IS_ERR((__force
void *)vpu
->reg
.cfg
))
798 return PTR_ERR((__force
void *)vpu
->reg
.cfg
);
801 vpu
->clk
= devm_clk_get(dev
, "main");
802 if (IS_ERR(vpu
->clk
)) {
803 dev_err(dev
, "get vpu clock failed\n");
804 return PTR_ERR(vpu
->clk
);
807 platform_set_drvdata(pdev
, vpu
);
809 ret
= clk_prepare(vpu
->clk
);
811 dev_err(dev
, "prepare vpu clock failed\n");
816 vpu
->wdt
.wq
= create_singlethread_workqueue("vpu_wdt");
818 dev_err(dev
, "initialize wdt workqueue failed\n");
821 INIT_WORK(&vpu
->wdt
.ws
, vpu_wdt_reset_func
);
822 mutex_init(&vpu
->vpu_mutex
);
824 ret
= vpu_clock_enable(vpu
);
826 dev_err(dev
, "enable vpu clock failed\n");
827 goto workqueue_destroy
;
830 dev_dbg(dev
, "vpu ipi init\n");
831 ret
= vpu_ipi_init(vpu
);
833 dev_err(dev
, "Failed to init ipi\n");
834 goto disable_vpu_clk
;
837 /* register vpu initialization IPI */
838 ret
= vpu_ipi_register(pdev
, IPI_VPU_INIT
, vpu_init_ipi_handler
,
841 dev_err(dev
, "Failed to register IPI_VPU_INIT\n");
842 goto vpu_mutex_destroy
;
845 #ifdef CONFIG_DEBUG_FS
846 vpu_debugfs
= debugfs_create_file("mtk_vpu", S_IRUGO
, NULL
, (void *)dev
,
854 /* Set PTCM to 96K and DTCM to 32K */
855 vpu_cfg_writel(vpu
, 0x2, VPU_TCM_CFG
);
857 vpu
->enable_4GB
= !!(totalram_pages
> (SZ_2G
>> PAGE_SHIFT
));
858 dev_info(dev
, "4GB mode %u\n", vpu
->enable_4GB
);
860 if (vpu
->enable_4GB
) {
861 ret
= of_reserved_mem_device_init(dev
);
863 dev_info(dev
, "init reserved memory failed\n");
864 /* continue to use dynamic allocation if failed */
867 ret
= vpu_alloc_ext_mem(vpu
, D_FW
);
869 dev_err(dev
, "Allocate DM failed\n");
873 ret
= vpu_alloc_ext_mem(vpu
, P_FW
);
875 dev_err(dev
, "Allocate PM failed\n");
879 init_waitqueue_head(&vpu
->run
.wq
);
880 init_waitqueue_head(&vpu
->ack_wq
);
882 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
884 dev_err(dev
, "get IRQ resource failed.\n");
888 vpu
->reg
.irq
= platform_get_irq(pdev
, 0);
889 ret
= devm_request_irq(dev
, vpu
->reg
.irq
, vpu_irq_handler
, 0,
892 dev_err(dev
, "failed to request irq\n");
896 vpu_clock_disable(vpu
);
897 dev_dbg(dev
, "initialization completed\n");
902 vpu_free_ext_mem(vpu
, P_FW
);
904 vpu_free_ext_mem(vpu
, D_FW
);
906 of_reserved_mem_device_release(dev
);
907 #ifdef CONFIG_DEBUG_FS
908 debugfs_remove(vpu_debugfs
);
911 memset(vpu
->ipi_desc
, 0, sizeof(struct vpu_ipi_desc
) * IPI_MAX
);
913 mutex_destroy(&vpu
->vpu_mutex
);
915 vpu_clock_disable(vpu
);
917 destroy_workqueue(vpu
->wdt
.wq
);
922 static const struct of_device_id mtk_vpu_match
[] = {
924 .compatible
= "mediatek,mt8173-vpu",
928 MODULE_DEVICE_TABLE(of
, mtk_vpu_match
);
930 static int mtk_vpu_remove(struct platform_device
*pdev
)
932 struct mtk_vpu
*vpu
= platform_get_drvdata(pdev
);
934 #ifdef CONFIG_DEBUG_FS
935 debugfs_remove(vpu_debugfs
);
938 flush_workqueue(vpu
->wdt
.wq
);
939 destroy_workqueue(vpu
->wdt
.wq
);
941 vpu_free_ext_mem(vpu
, P_FW
);
942 vpu_free_ext_mem(vpu
, D_FW
);
943 mutex_destroy(&vpu
->vpu_mutex
);
944 clk_unprepare(vpu
->clk
);
949 static struct platform_driver mtk_vpu_driver
= {
950 .probe
= mtk_vpu_probe
,
951 .remove
= mtk_vpu_remove
,
954 .of_match_table
= mtk_vpu_match
,
958 module_platform_driver(mtk_vpu_driver
);
960 MODULE_LICENSE("GPL v2");
961 MODULE_DESCRIPTION("Mediatek Video Prosessor Unit driver");