blob: 463b69c934bef04db809db84d665382591e75e80 [file] [log] [blame]
Andrew-CT Chen3003a182016-05-03 07:11:21 -03001/*
2* Copyright (c) 2016 MediaTek Inc.
3* Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
4*
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.
8*
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.
13*/
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>
26
27#include "mtk_vpu.h"
28
29/**
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.
33 **/
34
35#define INIT_TIMEOUT_MS 2000U
36#define IPI_TIMEOUT_MS 2000U
37#define VPU_FW_VER_LEN 16
38
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
52
53/* binary firmware name */
54#define VPU_P_FW "vpu_p.bin"
55#define VPU_D_FW "vpu_d.bin"
56
57#define VPU_RESET 0x0
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
67
68/* vpu inter-processor communication interrupt */
69#define VPU_IPC_INT BIT(8)
70
71/**
72 * enum vpu_fw_type - VPU firmware type
73 *
74 * @P_FW: program firmware
75 * @D_FW: data firmware
76 *
77 */
78enum vpu_fw_type {
79 P_FW,
80 D_FW,
81};
82
83/**
84 * struct vpu_mem - VPU extended program/data memory information
85 *
86 * @va: the kernel virtual memory address of VPU extended memory
87 * @pa: the physical memory address of VPU extended memory
88 *
89 */
90struct vpu_mem {
91 void *va;
92 dma_addr_t pa;
93};
94
95/**
96 * struct vpu_regs - VPU TCM and configuration registers
97 *
98 * @tcm: the register for VPU Tightly-Coupled Memory
99 * @cfg: the register for VPU configuration
100 * @irq: the irq number for VPU interrupt
101 */
102struct vpu_regs {
103 void __iomem *tcm;
104 void __iomem *cfg;
105 int irq;
106};
107
108/**
109 * struct vpu_wdt_handler - VPU watchdog reset handler
110 *
111 * @reset_func: reset handler
112 * @priv: private data
113 */
114struct vpu_wdt_handler {
115 void (*reset_func)(void *);
116 void *priv;
117};
118
119/**
120 * struct vpu_wdt - VPU watchdog workqueue
121 *
122 * @handler: VPU watchdog reset handler
123 * @ws: workstruct for VPU watchdog
124 * @wq: workqueue for VPU watchdog
125 */
126struct vpu_wdt {
127 struct vpu_wdt_handler handler[VPU_RST_MAX];
128 struct work_struct ws;
129 struct workqueue_struct *wq;
130};
131
132/**
133 * struct vpu_run - VPU initialization status
134 *
135 * @signaled: the signal of vpu initialization completed
136 * @fw_ver: VPU firmware version
Andrew-CT Chene2818a52016-09-02 09:19:52 -0300137 * @dec_capability: decoder capability which is not used for now and
138 * the value is reserved for future use
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300139 * @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
142 */
143struct vpu_run {
144 u32 signaled;
145 char fw_ver[VPU_FW_VER_LEN];
Andrew-CT Chene2818a52016-09-02 09:19:52 -0300146 unsigned int dec_capability;
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300147 unsigned int enc_capability;
148 wait_queue_head_t wq;
149};
150
151/**
152 * struct vpu_ipi_desc - VPU IPI descriptor
153 *
154 * @handler: IPI handler
155 * @name: the name of IPI handler
156 * @priv: the private data of IPI handler
157 */
158struct vpu_ipi_desc {
159 ipi_handler_t handler;
160 const char *name;
161 void *priv;
162};
163
164/**
165 * struct share_obj - DTCM (Data Tightly-Coupled Memory) buffer shared with
166 * AP and VPU
167 *
168 * @id: IPI id
169 * @len: share buffer length
170 * @share_buf: share buffer data
171 */
172struct share_obj {
173 s32 id;
174 u32 len;
175 unsigned char share_buf[SHARE_BUF_SIZE];
176};
177
178/**
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
201 * go back to sleep.
202 * @ipi_id_ack: The ACKs for registered IPI function sending
203 * interrupt to VPU
204 *
205 */
206struct mtk_vpu {
207 struct vpu_mem extmem[2];
208 struct vpu_regs reg;
209 struct vpu_run run;
210 struct vpu_wdt wdt;
211 struct vpu_ipi_desc ipi_desc[IPI_MAX];
212 struct share_obj *recv_buf;
213 struct share_obj *send_buf;
214 struct device *dev;
215 struct clk *clk;
216 bool fw_loaded;
217 bool enable_4GB;
218 struct mutex vpu_mutex; /* for protecting vpu data data structure */
219 u32 wdt_refcnt;
220 wait_queue_head_t ack_wq;
221 bool ipi_id_ack[IPI_MAX];
222};
223
224static inline void vpu_cfg_writel(struct mtk_vpu *vpu, u32 val, u32 offset)
225{
226 writel(val, vpu->reg.cfg + offset);
227}
228
229static inline u32 vpu_cfg_readl(struct mtk_vpu *vpu, u32 offset)
230{
231 return readl(vpu->reg.cfg + offset);
232}
233
234static inline bool vpu_running(struct mtk_vpu *vpu)
235{
236 return vpu_cfg_readl(vpu, VPU_RESET) & BIT(0);
237}
238
239static void vpu_clock_disable(struct mtk_vpu *vpu)
240{
241 /* Disable VPU watchdog */
242 mutex_lock(&vpu->vpu_mutex);
243 if (!--vpu->wdt_refcnt)
244 vpu_cfg_writel(vpu,
245 vpu_cfg_readl(vpu, VPU_WDT_REG) & ~(1L << 31),
246 VPU_WDT_REG);
247 mutex_unlock(&vpu->vpu_mutex);
248
249 clk_disable(vpu->clk);
250}
251
252static int vpu_clock_enable(struct mtk_vpu *vpu)
253{
254 int ret;
255
256 ret = clk_enable(vpu->clk);
257 if (ret)
258 return ret;
259 /* Enable VPU watchdog */
260 mutex_lock(&vpu->vpu_mutex);
261 if (!vpu->wdt_refcnt++)
262 vpu_cfg_writel(vpu,
263 vpu_cfg_readl(vpu, VPU_WDT_REG) | (1L << 31),
264 VPU_WDT_REG);
265 mutex_unlock(&vpu->vpu_mutex);
266
267 return ret;
268}
269
270int vpu_ipi_register(struct platform_device *pdev,
271 enum ipi_id id, ipi_handler_t handler,
272 const char *name, void *priv)
273{
274 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
275 struct vpu_ipi_desc *ipi_desc;
276
277 if (!vpu) {
278 dev_err(&pdev->dev, "vpu device in not ready\n");
279 return -EPROBE_DEFER;
280 }
281
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;
287 return 0;
288 }
289
290 dev_err(&pdev->dev, "register vpu ipi id %d with invalid arguments\n",
291 id);
292 return -EINVAL;
293}
294EXPORT_SYMBOL_GPL(vpu_ipi_register);
295
296int vpu_ipi_send(struct platform_device *pdev,
297 enum ipi_id id, void *buf,
298 unsigned int len)
299{
300 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
301 struct share_obj *send_obj = vpu->send_buf;
302 unsigned long timeout;
303 int ret = 0;
304
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");
308 return -EINVAL;
309 }
310
311 ret = vpu_clock_enable(vpu);
312 if (ret) {
313 dev_err(vpu->dev, "failed to enable vpu clock\n");
314 return ret;
315 }
316 if (!vpu_running(vpu)) {
317 dev_err(vpu->dev, "vpu_ipi_send: VPU is not running\n");
318 ret = -EINVAL;
319 goto clock_disable;
320 }
321
322 mutex_lock(&vpu->vpu_mutex);
323
324 /* Wait until VPU receives the last command */
325 timeout = jiffies + msecs_to_jiffies(IPI_TIMEOUT_MS);
326 do {
327 if (time_after(jiffies, timeout)) {
328 dev_err(vpu->dev, "vpu_ipi_send: IPI timeout!\n");
329 ret = -EIO;
330 goto mut_unlock;
331 }
332 } while (vpu_cfg_readl(vpu, HOST_TO_VPU));
333
334 memcpy((void *)send_obj->share_buf, buf, len);
335 send_obj->len = len;
336 send_obj->id = id;
337
338 vpu->ipi_id_ack[id] = false;
339 /* send the command to VPU */
340 vpu_cfg_writel(vpu, 0x1, HOST_TO_VPU);
341
342 mutex_unlock(&vpu->vpu_mutex);
343
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;
348 if (ret == 0) {
349 dev_err(vpu->dev, "vpu ipi %d ack time out !", id);
350 ret = -EIO;
351 goto clock_disable;
352 }
353 vpu_clock_disable(vpu);
354
355 return 0;
356
357mut_unlock:
358 mutex_unlock(&vpu->vpu_mutex);
359clock_disable:
360 vpu_clock_disable(vpu);
361
362 return ret;
363}
364EXPORT_SYMBOL_GPL(vpu_ipi_send);
365
366static void vpu_wdt_reset_func(struct work_struct *ws)
367{
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;
371 int index, ret;
372
373 dev_info(vpu->dev, "vpu reset\n");
374 ret = vpu_clock_enable(vpu);
375 if (ret) {
376 dev_err(vpu->dev, "[VPU] wdt enables clock failed %d\n", ret);
377 return;
378 }
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);
384
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);
389 }
390 }
391}
392
393int vpu_wdt_reg_handler(struct platform_device *pdev,
394 void wdt_reset(void *),
395 void *priv, enum rst_id id)
396{
397 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
398 struct vpu_wdt_handler *handler;
399
400 if (!vpu) {
401 dev_err(&pdev->dev, "vpu device in not ready\n");
402 return -EPROBE_DEFER;
403 }
404
405 handler = vpu->wdt.handler;
406
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);
413 return 0;
414 }
415
416 dev_err(vpu->dev, "register vpu wdt handler failed\n");
417 return -EINVAL;
418}
419EXPORT_SYMBOL_GPL(vpu_wdt_reg_handler);
420
Andrew-CT Chene2818a52016-09-02 09:19:52 -0300421unsigned int vpu_get_vdec_hw_capa(struct platform_device *pdev)
422{
423 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
424
425 return vpu->run.dec_capability;
426}
427EXPORT_SYMBOL_GPL(vpu_get_vdec_hw_capa);
428
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300429unsigned int vpu_get_venc_hw_capa(struct platform_device *pdev)
430{
431 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
432
433 return vpu->run.enc_capability;
434}
435EXPORT_SYMBOL_GPL(vpu_get_venc_hw_capa);
436
437void *vpu_mapping_dm_addr(struct platform_device *pdev,
438 u32 dtcm_dmem_addr)
439{
440 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
441
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);
446 }
447
448 if (dtcm_dmem_addr < VPU_DTCM_SIZE)
449 return (__force void *)(dtcm_dmem_addr + vpu->reg.tcm +
450 VPU_DTCM_OFFSET);
451
452 return vpu->extmem[D_FW].va + (dtcm_dmem_addr - VPU_DTCM_SIZE);
453}
454EXPORT_SYMBOL_GPL(vpu_mapping_dm_addr);
455
456struct platform_device *vpu_get_plat_device(struct platform_device *pdev)
457{
458 struct device *dev = &pdev->dev;
459 struct device_node *vpu_node;
460 struct platform_device *vpu_pdev;
461
462 vpu_node = of_parse_phandle(dev->of_node, "mediatek,vpu", 0);
463 if (!vpu_node) {
464 dev_err(dev, "can't get vpu node\n");
465 return NULL;
466 }
467
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);
472 return NULL;
473 }
474
475 return vpu_pdev;
476}
477EXPORT_SYMBOL_GPL(vpu_get_plat_device);
478
479/* load vpu program/data memory */
480static int load_requested_vpu(struct mtk_vpu *vpu,
481 const struct firmware *vpu_fw,
482 u8 fw_type)
483{
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;
487 size_t dl_size = 0;
488 size_t extra_fw_size = 0;
489 void *dest;
490 int ret;
491
492 ret = request_firmware(&vpu_fw, fw_name, vpu->dev);
493 if (ret < 0) {
494 dev_err(vpu->dev, "Failed to load %s, %d\n", fw_name, ret);
495 return ret;
496 }
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,
500 dl_size);
501 release_firmware(vpu_fw);
502 return -EFBIG;
503 }
504 dev_dbg(vpu->dev, "Downloaded fw %s size: %zu.\n",
505 fw_name,
506 dl_size);
507 /* reset VPU */
508 vpu_cfg_writel(vpu, 0x0, VPU_RESET);
509
510 /* handle extended firmware size */
511 if (dl_size > tcm_size) {
512 dev_dbg(vpu->dev, "fw size %zu > limited fw size %zu\n",
513 dl_size, tcm_size);
514 extra_fw_size = dl_size - tcm_size;
515 dev_dbg(vpu->dev, "extra_fw_size %zu\n", extra_fw_size);
516 dl_size = tcm_size;
517 }
518 dest = (__force void *)vpu->reg.tcm;
519 if (fw_type == D_FW)
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",
526 fw_type);
527 memcpy(dest, vpu_fw->data + tcm_size, extra_fw_size);
528 }
529
530 release_firmware(vpu_fw);
531
532 return 0;
533}
534
535int vpu_load_firmware(struct platform_device *pdev)
536{
Colin Ian King211eba92016-09-07 14:10:27 -0300537 struct mtk_vpu *vpu;
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300538 struct device *dev = &pdev->dev;
Colin Ian King211eba92016-09-07 14:10:27 -0300539 struct vpu_run *run;
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300540 const struct firmware *vpu_fw = NULL;
541 int ret;
542
543 if (!pdev) {
544 dev_err(dev, "VPU platform device is invalid\n");
545 return -EINVAL;
546 }
547
Colin Ian King211eba92016-09-07 14:10:27 -0300548 vpu = platform_get_drvdata(pdev);
549 run = &vpu->run;
550
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300551 mutex_lock(&vpu->vpu_mutex);
552 if (vpu->fw_loaded) {
553 mutex_unlock(&vpu->vpu_mutex);
554 return 0;
555 }
556 mutex_unlock(&vpu->vpu_mutex);
557
558 ret = vpu_clock_enable(vpu);
559 if (ret) {
560 dev_err(dev, "enable clock failed %d\n", ret);
561 return ret;
562 }
563
564 mutex_lock(&vpu->vpu_mutex);
565
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);
570 if (ret < 0) {
571 dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
572 goto OUT_LOAD_FW;
573 }
574
575 /* Downloading data firmware to device */
576 ret = load_requested_vpu(vpu, vpu_fw, D_FW);
577 if (ret < 0) {
578 dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
579 goto OUT_LOAD_FW;
580 }
581
582 vpu->fw_loaded = true;
583 /* boot up vpu */
584 vpu_cfg_writel(vpu, 0x1, VPU_RESET);
585
586 ret = wait_event_interruptible_timeout(run->wq,
587 run->signaled,
588 msecs_to_jiffies(INIT_TIMEOUT_MS)
589 );
590 if (ret == 0) {
591 ret = -ETIME;
592 dev_err(dev, "wait vpu initialization timout!\n");
593 goto OUT_LOAD_FW;
594 } else if (-ERESTARTSYS == ret) {
595 dev_err(dev, "wait vpu interrupted by a signal!\n");
596 goto OUT_LOAD_FW;
597 }
598
599 ret = 0;
600 dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
601
602OUT_LOAD_FW:
603 mutex_unlock(&vpu->vpu_mutex);
604 vpu_clock_disable(vpu);
605
606 return ret;
607}
608EXPORT_SYMBOL_GPL(vpu_load_firmware);
609
610static void vpu_init_ipi_handler(void *data, unsigned int len, void *priv)
611{
612 struct mtk_vpu *vpu = (struct mtk_vpu *)priv;
613 struct vpu_run *run = (struct vpu_run *)data;
614
615 vpu->run.signaled = run->signaled;
616 strncpy(vpu->run.fw_ver, run->fw_ver, VPU_FW_VER_LEN);
Andrew-CT Chene2818a52016-09-02 09:19:52 -0300617 vpu->run.dec_capability = run->dec_capability;
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300618 vpu->run.enc_capability = run->enc_capability;
619 wake_up_interruptible(&vpu->run.wq);
620}
621
622#ifdef CONFIG_DEBUG_FS
623static ssize_t vpu_debug_read(struct file *file, char __user *user_buf,
624 size_t count, loff_t *ppos)
625{
626 char buf[256];
627 unsigned int len;
628 unsigned int running, pc, vpu_to_host, host_to_vpu, wdt;
629 int ret;
630 struct device *dev = file->private_data;
631 struct mtk_vpu *vpu = dev_get_drvdata(dev);
632
633 ret = vpu_clock_enable(vpu);
634 if (ret) {
635 dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
636 return 0;
637 }
638
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);
646
647 if (running) {
648 len = snprintf(buf, sizeof(buf), "VPU is running\n\n"
649 "FW Version: %s\n"
650 "PC: 0x%x\n"
651 "WDT: 0x%x\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);
656 } else {
657 len = snprintf(buf, sizeof(buf), "VPU not running\n");
658 }
659
660 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
661}
662
663static const struct file_operations vpu_debug_fops = {
664 .open = simple_open,
665 .read = vpu_debug_read,
666};
667#endif /* CONFIG_DEBUG_FS */
668
669static void vpu_free_ext_mem(struct mtk_vpu *vpu, u8 fw_type)
670{
671 struct device *dev = vpu->dev;
672 size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
673
674 dma_free_coherent(dev, fw_ext_size, vpu->extmem[fw_type].va,
675 vpu->extmem[fw_type].pa);
676}
677
678static int vpu_alloc_ext_mem(struct mtk_vpu *vpu, u32 fw_type)
679{
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;
685
686 vpu->extmem[fw_type].va = dma_alloc_coherent(dev,
687 fw_ext_size,
688 &vpu->extmem[fw_type].pa,
689 GFP_KERNEL);
690 if (!vpu->extmem[fw_type].va) {
691 dev_err(dev, "Failed to allocate the extended program memory\n");
Christophe JAILLET8a5d2ac2016-09-23 18:19:01 -0300692 return -ENOMEM;
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300693 }
694
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,
698 vpu_ext_mem1);
699
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);
704
705 return 0;
706}
707
708static void vpu_ipi_handler(struct mtk_vpu *vpu)
709{
710 struct share_obj *rcv_obj = vpu->recv_buf;
711 struct vpu_ipi_desc *ipi_desc = vpu->ipi_desc;
712
713 if (rcv_obj->id < IPI_MAX && ipi_desc[rcv_obj->id].handler) {
714 ipi_desc[rcv_obj->id].handler(rcv_obj->share_buf,
715 rcv_obj->len,
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);
720 }
721 } else {
722 dev_err(vpu->dev, "No such ipi id = %d\n", rcv_obj->id);
723 }
724}
725
726static int vpu_ipi_init(struct mtk_vpu *vpu)
727{
728 /* Disable VPU to host interrupt */
729 vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
730
731 /* shared buffer initialization */
732 vpu->recv_buf = (__force struct share_obj *)(vpu->reg.tcm +
733 VPU_DTCM_OFFSET);
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));
737
738 return 0;
739}
740
741static irqreturn_t vpu_irq_handler(int irq, void *priv)
742{
743 struct mtk_vpu *vpu = priv;
744 u32 vpu_to_host;
745 int ret;
746
747 /*
748 * Clock should have been enabled already.
749 * Enable again in case vpu_ipi_send times out
750 * and has disabled the clock.
751 */
752 ret = clk_enable(vpu->clk);
753 if (ret) {
754 dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
755 return IRQ_NONE;
756 }
757 vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
758 if (vpu_to_host & VPU_IPC_INT) {
759 vpu_ipi_handler(vpu);
760 } else {
761 dev_err(vpu->dev, "vpu watchdog timeout! 0x%x", vpu_to_host);
762 queue_work(vpu->wdt.wq, &vpu->wdt.ws);
763 }
764
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);
768
769 return IRQ_HANDLED;
770}
771
772#ifdef CONFIG_DEBUG_FS
773static struct dentry *vpu_debugfs;
774#endif
775static int mtk_vpu_probe(struct platform_device *pdev)
776{
777 struct mtk_vpu *vpu;
778 struct device *dev;
779 struct resource *res;
780 int ret = 0;
781
782 dev_dbg(&pdev->dev, "initialization\n");
783
784 dev = &pdev->dev;
785 vpu = devm_kzalloc(dev, sizeof(*vpu), GFP_KERNEL);
786 if (!vpu)
787 return -ENOMEM;
788
789 vpu->dev = &pdev->dev;
790 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcm");
791 vpu->reg.tcm = devm_ioremap_resource(dev, res);
Wei Yongjun14ee4522016-07-12 08:01:26 -0300792 if (IS_ERR((__force void *)vpu->reg.tcm))
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300793 return PTR_ERR((__force void *)vpu->reg.tcm);
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300794
795 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_reg");
796 vpu->reg.cfg = devm_ioremap_resource(dev, res);
Wei Yongjun14ee4522016-07-12 08:01:26 -0300797 if (IS_ERR((__force void *)vpu->reg.cfg))
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300798 return PTR_ERR((__force void *)vpu->reg.cfg);
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300799
800 /* Get VPU clock */
801 vpu->clk = devm_clk_get(dev, "main");
Wei Yongjunffe57032016-07-12 08:00:55 -0300802 if (IS_ERR(vpu->clk)) {
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300803 dev_err(dev, "get vpu clock failed\n");
Wei Yongjunffe57032016-07-12 08:00:55 -0300804 return PTR_ERR(vpu->clk);
Andrew-CT Chen3003a182016-05-03 07:11:21 -0300805 }
806
807 platform_set_drvdata(pdev, vpu);
808
809 ret = clk_prepare(vpu->clk);
810 if (ret) {
811 dev_err(dev, "prepare vpu clock failed\n");
812 return ret;
813 }
814
815 /* VPU watchdog */
816 vpu->wdt.wq = create_singlethread_workqueue("vpu_wdt");
817 if (!vpu->wdt.wq) {
818 dev_err(dev, "initialize wdt workqueue failed\n");
819 return -ENOMEM;
820 }
821 INIT_WORK(&vpu->wdt.ws, vpu_wdt_reset_func);
822 mutex_init(&vpu->vpu_mutex);
823
824 ret = vpu_clock_enable(vpu);
825 if (ret) {
826 dev_err(dev, "enable vpu clock failed\n");
827 goto workqueue_destroy;
828 }
829
830 dev_dbg(dev, "vpu ipi init\n");
831 ret = vpu_ipi_init(vpu);
832 if (ret) {
833 dev_err(dev, "Failed to init ipi\n");
834 goto disable_vpu_clk;
835 }
836
837 /* register vpu initialization IPI */
838 ret = vpu_ipi_register(pdev, IPI_VPU_INIT, vpu_init_ipi_handler,
839 "vpu_init", vpu);
840 if (ret) {
841 dev_err(dev, "Failed to register IPI_VPU_INIT\n");
842 goto vpu_mutex_destroy;
843 }
844
845#ifdef CONFIG_DEBUG_FS
846 vpu_debugfs = debugfs_create_file("mtk_vpu", S_IRUGO, NULL, (void *)dev,
847 &vpu_debug_fops);
848 if (!vpu_debugfs) {
849 ret = -ENOMEM;
850 goto cleanup_ipi;
851 }
852#endif
853
854 /* Set PTCM to 96K and DTCM to 32K */
855 vpu_cfg_writel(vpu, 0x2, VPU_TCM_CFG);
856
857 vpu->enable_4GB = !!(totalram_pages > (SZ_2G >> PAGE_SHIFT));
858 dev_info(dev, "4GB mode %u\n", vpu->enable_4GB);
859
860 if (vpu->enable_4GB) {
861 ret = of_reserved_mem_device_init(dev);
862 if (ret)
863 dev_info(dev, "init reserved memory failed\n");
864 /* continue to use dynamic allocation if failed */
865 }
866
867 ret = vpu_alloc_ext_mem(vpu, D_FW);
868 if (ret) {
869 dev_err(dev, "Allocate DM failed\n");
870 goto remove_debugfs;
871 }
872
873 ret = vpu_alloc_ext_mem(vpu, P_FW);
874 if (ret) {
875 dev_err(dev, "Allocate PM failed\n");
876 goto free_d_mem;
877 }
878
879 init_waitqueue_head(&vpu->run.wq);
880 init_waitqueue_head(&vpu->ack_wq);
881
882 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
883 if (!res) {
884 dev_err(dev, "get IRQ resource failed.\n");
885 ret = -ENXIO;
886 goto free_p_mem;
887 }
888 vpu->reg.irq = platform_get_irq(pdev, 0);
889 ret = devm_request_irq(dev, vpu->reg.irq, vpu_irq_handler, 0,
890 pdev->name, vpu);
891 if (ret) {
892 dev_err(dev, "failed to request irq\n");
893 goto free_p_mem;
894 }
895
896 vpu_clock_disable(vpu);
897 dev_dbg(dev, "initialization completed\n");
898
899 return 0;
900
901free_p_mem:
902 vpu_free_ext_mem(vpu, P_FW);
903free_d_mem:
904 vpu_free_ext_mem(vpu, D_FW);
905remove_debugfs:
906 of_reserved_mem_device_release(dev);
907#ifdef CONFIG_DEBUG_FS
908 debugfs_remove(vpu_debugfs);
909cleanup_ipi:
910#endif
911 memset(vpu->ipi_desc, 0, sizeof(struct vpu_ipi_desc) * IPI_MAX);
912vpu_mutex_destroy:
913 mutex_destroy(&vpu->vpu_mutex);
914disable_vpu_clk:
915 vpu_clock_disable(vpu);
916workqueue_destroy:
917 destroy_workqueue(vpu->wdt.wq);
918
919 return ret;
920}
921
922static const struct of_device_id mtk_vpu_match[] = {
923 {
924 .compatible = "mediatek,mt8173-vpu",
925 },
926 {},
927};
928MODULE_DEVICE_TABLE(of, mtk_vpu_match);
929
930static int mtk_vpu_remove(struct platform_device *pdev)
931{
932 struct mtk_vpu *vpu = platform_get_drvdata(pdev);
933
934#ifdef CONFIG_DEBUG_FS
935 debugfs_remove(vpu_debugfs);
936#endif
937 if (vpu->wdt.wq) {
938 flush_workqueue(vpu->wdt.wq);
939 destroy_workqueue(vpu->wdt.wq);
940 }
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);
945
946 return 0;
947}
948
949static struct platform_driver mtk_vpu_driver = {
950 .probe = mtk_vpu_probe,
951 .remove = mtk_vpu_remove,
952 .driver = {
953 .name = "mtk_vpu",
954 .of_match_table = mtk_vpu_match,
955 },
956};
957
958module_platform_driver(mtk_vpu_driver);
959
960MODULE_LICENSE("GPL v2");
961MODULE_DESCRIPTION("Mediatek Video Prosessor Unit driver");