mtk_vpu.c 24 KB

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