GNU Linux-libre 4.14.303-gnu1
[releases.git] / drivers / media / platform / mtk-vpu / mtk_vpu.c
1 /*
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 #include <linux/dma-mapping.h>
27
28 #include "mtk_vpu.h"
29
30 /**
31  * VPU (video processor unit) is a tiny processor controlling video hardware
32  * related to video codec, scaling and color format converting.
33  * VPU interfaces with other blocks by share memory and interrupt.
34  **/
35
36 #define INIT_TIMEOUT_MS         2000U
37 #define IPI_TIMEOUT_MS          2000U
38 #define VPU_FW_VER_LEN          16
39
40 /* maximum program/data TCM (Tightly-Coupled Memory) size */
41 #define VPU_PTCM_SIZE           (96 * SZ_1K)
42 #define VPU_DTCM_SIZE           (32 * SZ_1K)
43 /* the offset to get data tcm address */
44 #define VPU_DTCM_OFFSET         0x18000UL
45 /* daynamic allocated maximum extended memory size */
46 #define VPU_EXT_P_SIZE          SZ_1M
47 #define VPU_EXT_D_SIZE          SZ_4M
48 /* maximum binary firmware size */
49 #define VPU_P_FW_SIZE           (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
50 #define VPU_D_FW_SIZE           (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
51 /* the size of share buffer between Host and  VPU */
52 #define SHARE_BUF_SIZE          48
53
54 /* binary firmware name */
55 #define VPU_P_FW                "/*(DEBLOBBED)*/"
56 #define VPU_D_FW                "/*(DEBLOBBED)*/"
57
58 #define VPU_RESET               0x0
59 #define VPU_TCM_CFG             0x0008
60 #define VPU_PMEM_EXT0_ADDR      0x000C
61 #define VPU_PMEM_EXT1_ADDR      0x0010
62 #define VPU_TO_HOST             0x001C
63 #define VPU_DMEM_EXT0_ADDR      0x0014
64 #define VPU_DMEM_EXT1_ADDR      0x0018
65 #define HOST_TO_VPU             0x0024
66 #define VPU_PC_REG              0x0060
67 #define VPU_WDT_REG             0x0084
68
69 /* vpu inter-processor communication interrupt */
70 #define VPU_IPC_INT             BIT(8)
71
72 /**
73  * enum vpu_fw_type - VPU firmware type
74  *
75  * @P_FW: program firmware
76  * @D_FW: data firmware
77  *
78  */
79 enum vpu_fw_type {
80         P_FW,
81         D_FW,
82 };
83
84 /**
85  * struct vpu_mem - VPU extended program/data memory information
86  *
87  * @va:         the kernel virtual memory address of VPU extended memory
88  * @pa:         the physical memory address of VPU extended memory
89  *
90  */
91 struct vpu_mem {
92         void *va;
93         dma_addr_t pa;
94 };
95
96 /**
97  * struct vpu_regs - VPU TCM and configuration registers
98  *
99  * @tcm:        the register for VPU Tightly-Coupled Memory
100  * @cfg:        the register for VPU configuration
101  * @irq:        the irq number for VPU interrupt
102  */
103 struct vpu_regs {
104         void __iomem *tcm;
105         void __iomem *cfg;
106         int irq;
107 };
108
109 /**
110  * struct vpu_wdt_handler - VPU watchdog reset handler
111  *
112  * @reset_func: reset handler
113  * @priv:       private data
114  */
115 struct vpu_wdt_handler {
116         void (*reset_func)(void *);
117         void *priv;
118 };
119
120 /**
121  * struct vpu_wdt - VPU watchdog workqueue
122  *
123  * @handler:    VPU watchdog reset handler
124  * @ws:         workstruct for VPU watchdog
125  * @wq:         workqueue for VPU watchdog
126  */
127 struct vpu_wdt {
128         struct vpu_wdt_handler handler[VPU_RST_MAX];
129         struct work_struct ws;
130         struct workqueue_struct *wq;
131 };
132
133 /**
134  * struct vpu_run - VPU initialization status
135  *
136  * @signaled:           the signal of vpu initialization completed
137  * @fw_ver:             VPU firmware version
138  * @dec_capability:     decoder capability which is not used for now and
139  *                      the value is reserved for future use
140  * @enc_capability:     encoder capability which is not used for now and
141  *                      the value is reserved for future use
142  * @wq:                 wait queue for VPU initialization status
143  */
144 struct vpu_run {
145         u32 signaled;
146         char fw_ver[VPU_FW_VER_LEN];
147         unsigned int    dec_capability;
148         unsigned int    enc_capability;
149         wait_queue_head_t wq;
150 };
151
152 /**
153  * struct vpu_ipi_desc - VPU IPI descriptor
154  *
155  * @handler:    IPI handler
156  * @name:       the name of IPI handler
157  * @priv:       the private data of IPI handler
158  */
159 struct vpu_ipi_desc {
160         ipi_handler_t handler;
161         const char *name;
162         void *priv;
163 };
164
165 /**
166  * struct share_obj - DTCM (Data Tightly-Coupled Memory) buffer shared with
167  *                    AP and VPU
168  *
169  * @id:         IPI id
170  * @len:        share buffer length
171  * @share_buf:  share buffer data
172  */
173 struct share_obj {
174         s32 id;
175         u32 len;
176         unsigned char share_buf[SHARE_BUF_SIZE];
177 };
178
179 /**
180  * struct mtk_vpu - vpu driver data
181  * @extmem:             VPU extended memory information
182  * @reg:                VPU TCM and configuration registers
183  * @run:                VPU initialization status
184  * @ipi_desc:           VPU IPI descriptor
185  * @recv_buf:           VPU DTCM share buffer for receiving. The
186  *                      receive buffer is only accessed in interrupt context.
187  * @send_buf:           VPU DTCM share buffer for sending
188  * @dev:                VPU struct device
189  * @clk:                VPU clock on/off
190  * @fw_loaded:          indicate VPU firmware loaded
191  * @enable_4GB:         VPU 4GB mode on/off
192  * @vpu_mutex:          protect mtk_vpu (except recv_buf) and ensure only
193  *                      one client to use VPU service at a time. For example,
194  *                      suppose a client is using VPU to decode VP8.
195  *                      If the other client wants to encode VP8,
196  *                      it has to wait until VP8 decode completes.
197  * @wdt_refcnt          WDT reference count to make sure the watchdog can be
198  *                      disabled if no other client is using VPU service
199  * @ack_wq:             The wait queue for each codec and mdp. When sleeping
200  *                      processes wake up, they will check the condition
201  *                      "ipi_id_ack" to run the corresponding action or
202  *                      go back to sleep.
203  * @ipi_id_ack:         The ACKs for registered IPI function sending
204  *                      interrupt to VPU
205  *
206  */
207 struct mtk_vpu {
208         struct vpu_mem extmem[2];
209         struct vpu_regs reg;
210         struct vpu_run run;
211         struct vpu_wdt wdt;
212         struct vpu_ipi_desc ipi_desc[IPI_MAX];
213         struct share_obj *recv_buf;
214         struct share_obj *send_buf;
215         struct device *dev;
216         struct clk *clk;
217         bool fw_loaded;
218         bool enable_4GB;
219         struct mutex vpu_mutex; /* for protecting vpu data data structure */
220         u32 wdt_refcnt;
221         wait_queue_head_t ack_wq;
222         bool ipi_id_ack[IPI_MAX];
223 };
224
225 static inline void vpu_cfg_writel(struct mtk_vpu *vpu, u32 val, u32 offset)
226 {
227         writel(val, vpu->reg.cfg + offset);
228 }
229
230 static inline u32 vpu_cfg_readl(struct mtk_vpu *vpu, u32 offset)
231 {
232         return readl(vpu->reg.cfg + offset);
233 }
234
235 static inline bool vpu_running(struct mtk_vpu *vpu)
236 {
237         return vpu_cfg_readl(vpu, VPU_RESET) & BIT(0);
238 }
239
240 static void vpu_clock_disable(struct mtk_vpu *vpu)
241 {
242         /* Disable VPU watchdog */
243         mutex_lock(&vpu->vpu_mutex);
244         if (!--vpu->wdt_refcnt)
245                 vpu_cfg_writel(vpu,
246                                vpu_cfg_readl(vpu, VPU_WDT_REG) & ~(1L << 31),
247                                VPU_WDT_REG);
248         mutex_unlock(&vpu->vpu_mutex);
249
250         clk_disable(vpu->clk);
251 }
252
253 static int vpu_clock_enable(struct mtk_vpu *vpu)
254 {
255         int ret;
256
257         ret = clk_enable(vpu->clk);
258         if (ret)
259                 return ret;
260         /* Enable VPU watchdog */
261         mutex_lock(&vpu->vpu_mutex);
262         if (!vpu->wdt_refcnt++)
263                 vpu_cfg_writel(vpu,
264                                vpu_cfg_readl(vpu, VPU_WDT_REG) | (1L << 31),
265                                VPU_WDT_REG);
266         mutex_unlock(&vpu->vpu_mutex);
267
268         return ret;
269 }
270
271 int vpu_ipi_register(struct platform_device *pdev,
272                      enum ipi_id id, ipi_handler_t handler,
273                      const char *name, void *priv)
274 {
275         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
276         struct vpu_ipi_desc *ipi_desc;
277
278         if (!vpu) {
279                 dev_err(&pdev->dev, "vpu device in not ready\n");
280                 return -EPROBE_DEFER;
281         }
282
283         if (id >= 0 && id < IPI_MAX && handler) {
284                 ipi_desc = vpu->ipi_desc;
285                 ipi_desc[id].name = name;
286                 ipi_desc[id].handler = handler;
287                 ipi_desc[id].priv = priv;
288                 return 0;
289         }
290
291         dev_err(&pdev->dev, "register vpu ipi id %d with invalid arguments\n",
292                 id);
293         return -EINVAL;
294 }
295 EXPORT_SYMBOL_GPL(vpu_ipi_register);
296
297 int vpu_ipi_send(struct platform_device *pdev,
298                  enum ipi_id id, void *buf,
299                  unsigned int len)
300 {
301         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
302         struct share_obj *send_obj = vpu->send_buf;
303         unsigned long timeout;
304         int ret = 0;
305
306         if (id <= IPI_VPU_INIT || id >= IPI_MAX ||
307             len > sizeof(send_obj->share_buf) || !buf) {
308                 dev_err(vpu->dev, "failed to send ipi message\n");
309                 return -EINVAL;
310         }
311
312         ret = vpu_clock_enable(vpu);
313         if (ret) {
314                 dev_err(vpu->dev, "failed to enable vpu clock\n");
315                 return ret;
316         }
317         if (!vpu_running(vpu)) {
318                 dev_err(vpu->dev, "vpu_ipi_send: VPU is not running\n");
319                 ret = -EINVAL;
320                 goto clock_disable;
321         }
322
323         mutex_lock(&vpu->vpu_mutex);
324
325          /* Wait until VPU receives the last command */
326         timeout = jiffies + msecs_to_jiffies(IPI_TIMEOUT_MS);
327         do {
328                 if (time_after(jiffies, timeout)) {
329                         dev_err(vpu->dev, "vpu_ipi_send: IPI timeout!\n");
330                         ret = -EIO;
331                         goto mut_unlock;
332                 }
333         } while (vpu_cfg_readl(vpu, HOST_TO_VPU));
334
335         memcpy((void *)send_obj->share_buf, buf, len);
336         send_obj->len = len;
337         send_obj->id = id;
338
339         vpu->ipi_id_ack[id] = false;
340         /* send the command to VPU */
341         vpu_cfg_writel(vpu, 0x1, HOST_TO_VPU);
342
343         mutex_unlock(&vpu->vpu_mutex);
344
345         /* wait for VPU's ACK */
346         timeout = msecs_to_jiffies(IPI_TIMEOUT_MS);
347         ret = wait_event_timeout(vpu->ack_wq, vpu->ipi_id_ack[id], timeout);
348         vpu->ipi_id_ack[id] = false;
349         if (ret == 0) {
350                 dev_err(vpu->dev, "vpu ipi %d ack time out !", id);
351                 ret = -EIO;
352                 goto clock_disable;
353         }
354         vpu_clock_disable(vpu);
355
356         return 0;
357
358 mut_unlock:
359         mutex_unlock(&vpu->vpu_mutex);
360 clock_disable:
361         vpu_clock_disable(vpu);
362
363         return ret;
364 }
365 EXPORT_SYMBOL_GPL(vpu_ipi_send);
366
367 static void vpu_wdt_reset_func(struct work_struct *ws)
368 {
369         struct vpu_wdt *wdt = container_of(ws, struct vpu_wdt, ws);
370         struct mtk_vpu *vpu = container_of(wdt, struct mtk_vpu, wdt);
371         struct vpu_wdt_handler *handler = wdt->handler;
372         int index, ret;
373
374         dev_info(vpu->dev, "vpu reset\n");
375         ret = vpu_clock_enable(vpu);
376         if (ret) {
377                 dev_err(vpu->dev, "[VPU] wdt enables clock failed %d\n", ret);
378                 return;
379         }
380         mutex_lock(&vpu->vpu_mutex);
381         vpu_cfg_writel(vpu, 0x0, VPU_RESET);
382         vpu->fw_loaded = false;
383         mutex_unlock(&vpu->vpu_mutex);
384         vpu_clock_disable(vpu);
385
386         for (index = 0; index < VPU_RST_MAX; index++) {
387                 if (handler[index].reset_func) {
388                         handler[index].reset_func(handler[index].priv);
389                         dev_dbg(vpu->dev, "wdt handler func %d\n", index);
390                 }
391         }
392 }
393
394 int vpu_wdt_reg_handler(struct platform_device *pdev,
395                         void wdt_reset(void *),
396                         void *priv, enum rst_id id)
397 {
398         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
399         struct vpu_wdt_handler *handler;
400
401         if (!vpu) {
402                 dev_err(&pdev->dev, "vpu device in not ready\n");
403                 return -EPROBE_DEFER;
404         }
405
406         handler = vpu->wdt.handler;
407
408         if (id >= 0 && id < VPU_RST_MAX && wdt_reset) {
409                 dev_dbg(vpu->dev, "wdt register id %d\n", id);
410                 mutex_lock(&vpu->vpu_mutex);
411                 handler[id].reset_func = wdt_reset;
412                 handler[id].priv = priv;
413                 mutex_unlock(&vpu->vpu_mutex);
414                 return 0;
415         }
416
417         dev_err(vpu->dev, "register vpu wdt handler failed\n");
418         return -EINVAL;
419 }
420 EXPORT_SYMBOL_GPL(vpu_wdt_reg_handler);
421
422 unsigned int vpu_get_vdec_hw_capa(struct platform_device *pdev)
423 {
424         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
425
426         return vpu->run.dec_capability;
427 }
428 EXPORT_SYMBOL_GPL(vpu_get_vdec_hw_capa);
429
430 unsigned int vpu_get_venc_hw_capa(struct platform_device *pdev)
431 {
432         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
433
434         return vpu->run.enc_capability;
435 }
436 EXPORT_SYMBOL_GPL(vpu_get_venc_hw_capa);
437
438 void *vpu_mapping_dm_addr(struct platform_device *pdev,
439                           u32 dtcm_dmem_addr)
440 {
441         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
442
443         if (!dtcm_dmem_addr ||
444             (dtcm_dmem_addr > (VPU_DTCM_SIZE + VPU_EXT_D_SIZE))) {
445                 dev_err(vpu->dev, "invalid virtual data memory address\n");
446                 return ERR_PTR(-EINVAL);
447         }
448
449         if (dtcm_dmem_addr < VPU_DTCM_SIZE)
450                 return (__force void *)(dtcm_dmem_addr + vpu->reg.tcm +
451                                         VPU_DTCM_OFFSET);
452
453         return vpu->extmem[D_FW].va + (dtcm_dmem_addr - VPU_DTCM_SIZE);
454 }
455 EXPORT_SYMBOL_GPL(vpu_mapping_dm_addr);
456
457 struct platform_device *vpu_get_plat_device(struct platform_device *pdev)
458 {
459         struct device *dev = &pdev->dev;
460         struct device_node *vpu_node;
461         struct platform_device *vpu_pdev;
462
463         vpu_node = of_parse_phandle(dev->of_node, "mediatek,vpu", 0);
464         if (!vpu_node) {
465                 dev_err(dev, "can't get vpu node\n");
466                 return NULL;
467         }
468
469         vpu_pdev = of_find_device_by_node(vpu_node);
470         if (WARN_ON(!vpu_pdev)) {
471                 dev_err(dev, "vpu pdev failed\n");
472                 of_node_put(vpu_node);
473                 return NULL;
474         }
475
476         return vpu_pdev;
477 }
478 EXPORT_SYMBOL_GPL(vpu_get_plat_device);
479
480 /* load vpu program/data memory */
481 static int load_requested_vpu(struct mtk_vpu *vpu,
482                               const struct firmware *vpu_fw,
483                               u8 fw_type)
484 {
485         size_t tcm_size = fw_type ? VPU_DTCM_SIZE : VPU_PTCM_SIZE;
486         size_t fw_size = fw_type ? VPU_D_FW_SIZE : VPU_P_FW_SIZE;
487         char *fw_name = fw_type ? VPU_D_FW : VPU_P_FW;
488         size_t dl_size = 0;
489         size_t extra_fw_size = 0;
490         void *dest;
491         int ret;
492
493         ret = reject_firmware(&vpu_fw, fw_name, vpu->dev);
494         if (ret < 0) {
495                 dev_err(vpu->dev, "Failed to load %s, %d\n", fw_name, ret);
496                 return ret;
497         }
498         dl_size = vpu_fw->size;
499         if (dl_size > fw_size) {
500                 dev_err(vpu->dev, "fw %s size %zu is abnormal\n", fw_name,
501                         dl_size);
502                 release_firmware(vpu_fw);
503                 return  -EFBIG;
504         }
505         dev_dbg(vpu->dev, "Downloaded fw %s size: %zu.\n",
506                 fw_name,
507                 dl_size);
508         /* reset VPU */
509         vpu_cfg_writel(vpu, 0x0, VPU_RESET);
510
511         /* handle extended firmware size */
512         if (dl_size > tcm_size) {
513                 dev_dbg(vpu->dev, "fw size %zu > limited fw size %zu\n",
514                         dl_size, tcm_size);
515                 extra_fw_size = dl_size - tcm_size;
516                 dev_dbg(vpu->dev, "extra_fw_size %zu\n", extra_fw_size);
517                 dl_size = tcm_size;
518         }
519         dest = (__force void *)vpu->reg.tcm;
520         if (fw_type == D_FW)
521                 dest += VPU_DTCM_OFFSET;
522         memcpy(dest, vpu_fw->data, dl_size);
523         /* download to extended memory if need */
524         if (extra_fw_size > 0) {
525                 dest = vpu->extmem[fw_type].va;
526                 dev_dbg(vpu->dev, "download extended memory type %x\n",
527                         fw_type);
528                 memcpy(dest, vpu_fw->data + tcm_size, extra_fw_size);
529         }
530
531         release_firmware(vpu_fw);
532
533         return 0;
534 }
535
536 int vpu_load_firmware(struct platform_device *pdev)
537 {
538         struct mtk_vpu *vpu;
539         struct device *dev = &pdev->dev;
540         struct vpu_run *run;
541         const struct firmware *vpu_fw = NULL;
542         int ret;
543
544         if (!pdev) {
545                 dev_err(dev, "VPU platform device is invalid\n");
546                 return -EINVAL;
547         }
548
549         vpu = platform_get_drvdata(pdev);
550         run = &vpu->run;
551
552         mutex_lock(&vpu->vpu_mutex);
553         if (vpu->fw_loaded) {
554                 mutex_unlock(&vpu->vpu_mutex);
555                 return 0;
556         }
557         mutex_unlock(&vpu->vpu_mutex);
558
559         ret = vpu_clock_enable(vpu);
560         if (ret) {
561                 dev_err(dev, "enable clock failed %d\n", ret);
562                 return ret;
563         }
564
565         mutex_lock(&vpu->vpu_mutex);
566
567         run->signaled = false;
568         dev_dbg(vpu->dev, "firmware request\n");
569         /* Downloading program firmware to device*/
570         ret = load_requested_vpu(vpu, vpu_fw, P_FW);
571         if (ret < 0) {
572                 dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
573                 goto OUT_LOAD_FW;
574         }
575
576         /* Downloading data firmware to device */
577         ret = load_requested_vpu(vpu, vpu_fw, D_FW);
578         if (ret < 0) {
579                 dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
580                 goto OUT_LOAD_FW;
581         }
582
583         vpu->fw_loaded = true;
584         /* boot up vpu */
585         vpu_cfg_writel(vpu, 0x1, VPU_RESET);
586
587         ret = wait_event_interruptible_timeout(run->wq,
588                                                run->signaled,
589                                                msecs_to_jiffies(INIT_TIMEOUT_MS)
590                                                );
591         if (ret == 0) {
592                 ret = -ETIME;
593                 dev_err(dev, "wait vpu initialization timeout!\n");
594                 goto OUT_LOAD_FW;
595         } else if (-ERESTARTSYS == ret) {
596                 dev_err(dev, "wait vpu interrupted by a signal!\n");
597                 goto OUT_LOAD_FW;
598         }
599
600         ret = 0;
601         dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
602
603 OUT_LOAD_FW:
604         mutex_unlock(&vpu->vpu_mutex);
605         vpu_clock_disable(vpu);
606
607         return ret;
608 }
609 EXPORT_SYMBOL_GPL(vpu_load_firmware);
610
611 static void vpu_init_ipi_handler(void *data, unsigned int len, void *priv)
612 {
613         struct mtk_vpu *vpu = (struct mtk_vpu *)priv;
614         struct vpu_run *run = (struct vpu_run *)data;
615
616         vpu->run.signaled = run->signaled;
617         strncpy(vpu->run.fw_ver, run->fw_ver, VPU_FW_VER_LEN);
618         vpu->run.dec_capability = run->dec_capability;
619         vpu->run.enc_capability = run->enc_capability;
620         wake_up_interruptible(&vpu->run.wq);
621 }
622
623 #ifdef CONFIG_DEBUG_FS
624 static ssize_t vpu_debug_read(struct file *file, char __user *user_buf,
625                               size_t count, loff_t *ppos)
626 {
627         char buf[256];
628         unsigned int len;
629         unsigned int running, pc, vpu_to_host, host_to_vpu, wdt;
630         int ret;
631         struct device *dev = file->private_data;
632         struct mtk_vpu *vpu = dev_get_drvdata(dev);
633
634         ret = vpu_clock_enable(vpu);
635         if (ret) {
636                 dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
637                 return 0;
638         }
639
640         /* vpu register status */
641         running = vpu_running(vpu);
642         pc = vpu_cfg_readl(vpu, VPU_PC_REG);
643         wdt = vpu_cfg_readl(vpu, VPU_WDT_REG);
644         host_to_vpu = vpu_cfg_readl(vpu, HOST_TO_VPU);
645         vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
646         vpu_clock_disable(vpu);
647
648         if (running) {
649                 len = snprintf(buf, sizeof(buf), "VPU is running\n\n"
650                 "FW Version: %s\n"
651                 "PC: 0x%x\n"
652                 "WDT: 0x%x\n"
653                 "Host to VPU: 0x%x\n"
654                 "VPU to Host: 0x%x\n",
655                 vpu->run.fw_ver, pc, wdt,
656                 host_to_vpu, vpu_to_host);
657         } else {
658                 len = snprintf(buf, sizeof(buf), "VPU not running\n");
659         }
660
661         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
662 }
663
664 static const struct file_operations vpu_debug_fops = {
665         .open = simple_open,
666         .read = vpu_debug_read,
667 };
668 #endif /* CONFIG_DEBUG_FS */
669
670 static void vpu_free_ext_mem(struct mtk_vpu *vpu, u8 fw_type)
671 {
672         struct device *dev = vpu->dev;
673         size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
674
675         dma_free_coherent(dev, fw_ext_size, vpu->extmem[fw_type].va,
676                           vpu->extmem[fw_type].pa);
677 }
678
679 static int vpu_alloc_ext_mem(struct mtk_vpu *vpu, u32 fw_type)
680 {
681         struct device *dev = vpu->dev;
682         size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
683         u32 vpu_ext_mem0 = fw_type ? VPU_DMEM_EXT0_ADDR : VPU_PMEM_EXT0_ADDR;
684         u32 vpu_ext_mem1 = fw_type ? VPU_DMEM_EXT1_ADDR : VPU_PMEM_EXT1_ADDR;
685         u32 offset_4gb = vpu->enable_4GB ? 0x40000000 : 0;
686
687         vpu->extmem[fw_type].va = dma_alloc_coherent(dev,
688                                                fw_ext_size,
689                                                &vpu->extmem[fw_type].pa,
690                                                GFP_KERNEL);
691         if (!vpu->extmem[fw_type].va) {
692                 dev_err(dev, "Failed to allocate the extended program memory\n");
693                 return -ENOMEM;
694         }
695
696         /* Disable extend0. Enable extend1 */
697         vpu_cfg_writel(vpu, 0x1, vpu_ext_mem0);
698         vpu_cfg_writel(vpu, (vpu->extmem[fw_type].pa & 0xFFFFF000) + offset_4gb,
699                        vpu_ext_mem1);
700
701         dev_info(dev, "%s extend memory phy=0x%llx virt=0x%p\n",
702                  fw_type ? "Data" : "Program",
703                  (unsigned long long)vpu->extmem[fw_type].pa,
704                  vpu->extmem[fw_type].va);
705
706         return 0;
707 }
708
709 static void vpu_ipi_handler(struct mtk_vpu *vpu)
710 {
711         struct share_obj *rcv_obj = vpu->recv_buf;
712         struct vpu_ipi_desc *ipi_desc = vpu->ipi_desc;
713
714         if (rcv_obj->id < IPI_MAX && ipi_desc[rcv_obj->id].handler) {
715                 ipi_desc[rcv_obj->id].handler(rcv_obj->share_buf,
716                                               rcv_obj->len,
717                                               ipi_desc[rcv_obj->id].priv);
718                 if (rcv_obj->id > IPI_VPU_INIT) {
719                         vpu->ipi_id_ack[rcv_obj->id] = true;
720                         wake_up(&vpu->ack_wq);
721                 }
722         } else {
723                 dev_err(vpu->dev, "No such ipi id = %d\n", rcv_obj->id);
724         }
725 }
726
727 static int vpu_ipi_init(struct mtk_vpu *vpu)
728 {
729         /* Disable VPU to host interrupt */
730         vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
731
732         /* shared buffer initialization */
733         vpu->recv_buf = (__force struct share_obj *)(vpu->reg.tcm +
734                                                      VPU_DTCM_OFFSET);
735         vpu->send_buf = vpu->recv_buf + 1;
736         memset(vpu->recv_buf, 0, sizeof(struct share_obj));
737         memset(vpu->send_buf, 0, sizeof(struct share_obj));
738
739         return 0;
740 }
741
742 static irqreturn_t vpu_irq_handler(int irq, void *priv)
743 {
744         struct mtk_vpu *vpu = priv;
745         u32 vpu_to_host;
746         int ret;
747
748         /*
749          * Clock should have been enabled already.
750          * Enable again in case vpu_ipi_send times out
751          * and has disabled the clock.
752          */
753         ret = clk_enable(vpu->clk);
754         if (ret) {
755                 dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
756                 return IRQ_NONE;
757         }
758         vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
759         if (vpu_to_host & VPU_IPC_INT) {
760                 vpu_ipi_handler(vpu);
761         } else {
762                 dev_err(vpu->dev, "vpu watchdog timeout! 0x%x", vpu_to_host);
763                 queue_work(vpu->wdt.wq, &vpu->wdt.ws);
764         }
765
766         /* VPU won't send another interrupt until we set VPU_TO_HOST to 0. */
767         vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
768         clk_disable(vpu->clk);
769
770         return IRQ_HANDLED;
771 }
772
773 #ifdef CONFIG_DEBUG_FS
774 static struct dentry *vpu_debugfs;
775 #endif
776 static int mtk_vpu_probe(struct platform_device *pdev)
777 {
778         struct mtk_vpu *vpu;
779         struct device *dev;
780         struct resource *res;
781         int ret = 0;
782
783         dev_dbg(&pdev->dev, "initialization\n");
784
785         dev = &pdev->dev;
786         vpu = devm_kzalloc(dev, sizeof(*vpu), GFP_KERNEL);
787         if (!vpu)
788                 return -ENOMEM;
789
790         vpu->dev = &pdev->dev;
791         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcm");
792         vpu->reg.tcm = devm_ioremap_resource(dev, res);
793         if (IS_ERR((__force void *)vpu->reg.tcm))
794                 return PTR_ERR((__force void *)vpu->reg.tcm);
795
796         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_reg");
797         vpu->reg.cfg = devm_ioremap_resource(dev, res);
798         if (IS_ERR((__force void *)vpu->reg.cfg))
799                 return PTR_ERR((__force void *)vpu->reg.cfg);
800
801         /* Get VPU clock */
802         vpu->clk = devm_clk_get(dev, "main");
803         if (IS_ERR(vpu->clk)) {
804                 dev_err(dev, "get vpu clock failed\n");
805                 return PTR_ERR(vpu->clk);
806         }
807
808         platform_set_drvdata(pdev, vpu);
809
810         ret = clk_prepare(vpu->clk);
811         if (ret) {
812                 dev_err(dev, "prepare vpu clock failed\n");
813                 return ret;
814         }
815
816         /* VPU watchdog */
817         vpu->wdt.wq = create_singlethread_workqueue("vpu_wdt");
818         if (!vpu->wdt.wq) {
819                 dev_err(dev, "initialize wdt workqueue failed\n");
820                 ret = -ENOMEM;
821                 goto clk_unprepare;
822         }
823         INIT_WORK(&vpu->wdt.ws, vpu_wdt_reset_func);
824         mutex_init(&vpu->vpu_mutex);
825
826         ret = vpu_clock_enable(vpu);
827         if (ret) {
828                 dev_err(dev, "enable vpu clock failed\n");
829                 goto workqueue_destroy;
830         }
831
832         dev_dbg(dev, "vpu ipi init\n");
833         ret = vpu_ipi_init(vpu);
834         if (ret) {
835                 dev_err(dev, "Failed to init ipi\n");
836                 goto disable_vpu_clk;
837         }
838
839         /* register vpu initialization IPI */
840         ret = vpu_ipi_register(pdev, IPI_VPU_INIT, vpu_init_ipi_handler,
841                                "vpu_init", vpu);
842         if (ret) {
843                 dev_err(dev, "Failed to register IPI_VPU_INIT\n");
844                 goto vpu_mutex_destroy;
845         }
846
847 #ifdef CONFIG_DEBUG_FS
848         vpu_debugfs = debugfs_create_file("mtk_vpu", S_IRUGO, NULL, (void *)dev,
849                                           &vpu_debug_fops);
850         if (!vpu_debugfs) {
851                 ret = -ENOMEM;
852                 goto cleanup_ipi;
853         }
854 #endif
855
856         /* Set PTCM to 96K and DTCM to 32K */
857         vpu_cfg_writel(vpu, 0x2, VPU_TCM_CFG);
858
859         vpu->enable_4GB = !!(totalram_pages > (SZ_2G >> PAGE_SHIFT));
860         dev_info(dev, "4GB mode %u\n", vpu->enable_4GB);
861
862         if (vpu->enable_4GB) {
863                 ret = of_reserved_mem_device_init(dev);
864                 if (ret)
865                         dev_info(dev, "init reserved memory failed\n");
866                         /* continue to use dynamic allocation if failed */
867         }
868
869         ret = vpu_alloc_ext_mem(vpu, D_FW);
870         if (ret) {
871                 dev_err(dev, "Allocate DM failed\n");
872                 goto remove_debugfs;
873         }
874
875         ret = vpu_alloc_ext_mem(vpu, P_FW);
876         if (ret) {
877                 dev_err(dev, "Allocate PM failed\n");
878                 goto free_d_mem;
879         }
880
881         init_waitqueue_head(&vpu->run.wq);
882         init_waitqueue_head(&vpu->ack_wq);
883
884         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
885         if (!res) {
886                 dev_err(dev, "get IRQ resource failed.\n");
887                 ret = -ENXIO;
888                 goto free_p_mem;
889         }
890         vpu->reg.irq = platform_get_irq(pdev, 0);
891         ret = devm_request_irq(dev, vpu->reg.irq, vpu_irq_handler, 0,
892                                pdev->name, vpu);
893         if (ret) {
894                 dev_err(dev, "failed to request irq\n");
895                 goto free_p_mem;
896         }
897
898         vpu_clock_disable(vpu);
899         dev_dbg(dev, "initialization completed\n");
900
901         return 0;
902
903 free_p_mem:
904         vpu_free_ext_mem(vpu, P_FW);
905 free_d_mem:
906         vpu_free_ext_mem(vpu, D_FW);
907 remove_debugfs:
908         of_reserved_mem_device_release(dev);
909 #ifdef CONFIG_DEBUG_FS
910         debugfs_remove(vpu_debugfs);
911 cleanup_ipi:
912 #endif
913         memset(vpu->ipi_desc, 0, sizeof(struct vpu_ipi_desc) * IPI_MAX);
914 vpu_mutex_destroy:
915         mutex_destroy(&vpu->vpu_mutex);
916 disable_vpu_clk:
917         vpu_clock_disable(vpu);
918 workqueue_destroy:
919         destroy_workqueue(vpu->wdt.wq);
920 clk_unprepare:
921         clk_unprepare(vpu->clk);
922
923         return ret;
924 }
925
926 static const struct of_device_id mtk_vpu_match[] = {
927         {
928                 .compatible = "mediatek,mt8173-vpu",
929         },
930         {},
931 };
932 MODULE_DEVICE_TABLE(of, mtk_vpu_match);
933
934 static int mtk_vpu_remove(struct platform_device *pdev)
935 {
936         struct mtk_vpu *vpu = platform_get_drvdata(pdev);
937
938 #ifdef CONFIG_DEBUG_FS
939         debugfs_remove(vpu_debugfs);
940 #endif
941         if (vpu->wdt.wq) {
942                 flush_workqueue(vpu->wdt.wq);
943                 destroy_workqueue(vpu->wdt.wq);
944         }
945         vpu_free_ext_mem(vpu, P_FW);
946         vpu_free_ext_mem(vpu, D_FW);
947         mutex_destroy(&vpu->vpu_mutex);
948         clk_unprepare(vpu->clk);
949
950         return 0;
951 }
952
953 static struct platform_driver mtk_vpu_driver = {
954         .probe  = mtk_vpu_probe,
955         .remove = mtk_vpu_remove,
956         .driver = {
957                 .name   = "mtk_vpu",
958                 .of_match_table = mtk_vpu_match,
959         },
960 };
961
962 module_platform_driver(mtk_vpu_driver);
963
964 MODULE_LICENSE("GPL v2");
965 MODULE_DESCRIPTION("Mediatek Video Prosessor Unit driver");