hfi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2017 Linaro Ltd.
  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 and
  7. * only version 2 as 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. */
  15. #include <linux/slab.h>
  16. #include <linux/mutex.h>
  17. #include <linux/list.h>
  18. #include <linux/completion.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/videodev2.h>
  21. #include "core.h"
  22. #include "hfi.h"
  23. #include "hfi_cmds.h"
  24. #include "hfi_venus.h"
  25. #define TIMEOUT msecs_to_jiffies(1000)
  26. static u32 to_codec_type(u32 pixfmt)
  27. {
  28. switch (pixfmt) {
  29. case V4L2_PIX_FMT_H264:
  30. case V4L2_PIX_FMT_H264_NO_SC:
  31. return HFI_VIDEO_CODEC_H264;
  32. case V4L2_PIX_FMT_H263:
  33. return HFI_VIDEO_CODEC_H263;
  34. case V4L2_PIX_FMT_MPEG1:
  35. return HFI_VIDEO_CODEC_MPEG1;
  36. case V4L2_PIX_FMT_MPEG2:
  37. return HFI_VIDEO_CODEC_MPEG2;
  38. case V4L2_PIX_FMT_MPEG4:
  39. return HFI_VIDEO_CODEC_MPEG4;
  40. case V4L2_PIX_FMT_VC1_ANNEX_G:
  41. case V4L2_PIX_FMT_VC1_ANNEX_L:
  42. return HFI_VIDEO_CODEC_VC1;
  43. case V4L2_PIX_FMT_VP8:
  44. return HFI_VIDEO_CODEC_VP8;
  45. case V4L2_PIX_FMT_VP9:
  46. return HFI_VIDEO_CODEC_VP9;
  47. case V4L2_PIX_FMT_XVID:
  48. return HFI_VIDEO_CODEC_DIVX;
  49. case V4L2_PIX_FMT_HEVC:
  50. return HFI_VIDEO_CODEC_HEVC;
  51. default:
  52. return 0;
  53. }
  54. }
  55. int hfi_core_init(struct venus_core *core)
  56. {
  57. int ret = 0;
  58. mutex_lock(&core->lock);
  59. if (core->state >= CORE_INIT)
  60. goto unlock;
  61. reinit_completion(&core->done);
  62. ret = core->ops->core_init(core);
  63. if (ret)
  64. goto unlock;
  65. ret = wait_for_completion_timeout(&core->done, TIMEOUT);
  66. if (!ret) {
  67. ret = -ETIMEDOUT;
  68. goto unlock;
  69. }
  70. ret = 0;
  71. if (core->error != HFI_ERR_NONE) {
  72. ret = -EIO;
  73. goto unlock;
  74. }
  75. core->state = CORE_INIT;
  76. unlock:
  77. mutex_unlock(&core->lock);
  78. return ret;
  79. }
  80. int hfi_core_deinit(struct venus_core *core, bool blocking)
  81. {
  82. int ret = 0, empty;
  83. mutex_lock(&core->lock);
  84. if (core->state == CORE_UNINIT)
  85. goto unlock;
  86. empty = list_empty(&core->instances);
  87. if (!empty && !blocking) {
  88. ret = -EBUSY;
  89. goto unlock;
  90. }
  91. if (!empty) {
  92. mutex_unlock(&core->lock);
  93. wait_var_event(&core->insts_count,
  94. !atomic_read(&core->insts_count));
  95. mutex_lock(&core->lock);
  96. }
  97. ret = core->ops->core_deinit(core);
  98. if (!ret)
  99. core->state = CORE_UNINIT;
  100. unlock:
  101. mutex_unlock(&core->lock);
  102. return ret;
  103. }
  104. int hfi_core_suspend(struct venus_core *core)
  105. {
  106. if (core->state != CORE_INIT)
  107. return 0;
  108. return core->ops->suspend(core);
  109. }
  110. int hfi_core_resume(struct venus_core *core, bool force)
  111. {
  112. if (!force && core->state != CORE_INIT)
  113. return 0;
  114. return core->ops->resume(core);
  115. }
  116. int hfi_core_trigger_ssr(struct venus_core *core, u32 type)
  117. {
  118. return core->ops->core_trigger_ssr(core, type);
  119. }
  120. int hfi_core_ping(struct venus_core *core)
  121. {
  122. int ret;
  123. mutex_lock(&core->lock);
  124. ret = core->ops->core_ping(core, 0xbeef);
  125. if (ret)
  126. goto unlock;
  127. ret = wait_for_completion_timeout(&core->done, TIMEOUT);
  128. if (!ret) {
  129. ret = -ETIMEDOUT;
  130. goto unlock;
  131. }
  132. ret = 0;
  133. if (core->error != HFI_ERR_NONE)
  134. ret = -ENODEV;
  135. unlock:
  136. mutex_unlock(&core->lock);
  137. return ret;
  138. }
  139. static int wait_session_msg(struct venus_inst *inst)
  140. {
  141. int ret;
  142. ret = wait_for_completion_timeout(&inst->done, TIMEOUT);
  143. if (!ret)
  144. return -ETIMEDOUT;
  145. if (inst->error != HFI_ERR_NONE)
  146. return -EIO;
  147. return 0;
  148. }
  149. int hfi_session_create(struct venus_inst *inst, const struct hfi_inst_ops *ops)
  150. {
  151. struct venus_core *core = inst->core;
  152. if (!ops)
  153. return -EINVAL;
  154. inst->state = INST_UNINIT;
  155. init_completion(&inst->done);
  156. inst->ops = ops;
  157. mutex_lock(&core->lock);
  158. list_add_tail(&inst->list, &core->instances);
  159. atomic_inc(&core->insts_count);
  160. mutex_unlock(&core->lock);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(hfi_session_create);
  164. int hfi_session_init(struct venus_inst *inst, u32 pixfmt)
  165. {
  166. struct venus_core *core = inst->core;
  167. const struct hfi_ops *ops = core->ops;
  168. int ret;
  169. inst->hfi_codec = to_codec_type(pixfmt);
  170. reinit_completion(&inst->done);
  171. ret = ops->session_init(inst, inst->session_type, inst->hfi_codec);
  172. if (ret)
  173. return ret;
  174. ret = wait_session_msg(inst);
  175. if (ret)
  176. return ret;
  177. inst->state = INST_INIT;
  178. return 0;
  179. }
  180. EXPORT_SYMBOL_GPL(hfi_session_init);
  181. void hfi_session_destroy(struct venus_inst *inst)
  182. {
  183. struct venus_core *core = inst->core;
  184. mutex_lock(&core->lock);
  185. list_del_init(&inst->list);
  186. if (atomic_dec_and_test(&core->insts_count))
  187. wake_up_var(&core->insts_count);
  188. mutex_unlock(&core->lock);
  189. }
  190. EXPORT_SYMBOL_GPL(hfi_session_destroy);
  191. int hfi_session_deinit(struct venus_inst *inst)
  192. {
  193. const struct hfi_ops *ops = inst->core->ops;
  194. int ret;
  195. if (inst->state == INST_UNINIT)
  196. return 0;
  197. if (inst->state < INST_INIT)
  198. return -EINVAL;
  199. reinit_completion(&inst->done);
  200. ret = ops->session_end(inst);
  201. if (ret)
  202. return ret;
  203. ret = wait_session_msg(inst);
  204. if (ret)
  205. return ret;
  206. inst->state = INST_UNINIT;
  207. return 0;
  208. }
  209. EXPORT_SYMBOL_GPL(hfi_session_deinit);
  210. int hfi_session_start(struct venus_inst *inst)
  211. {
  212. const struct hfi_ops *ops = inst->core->ops;
  213. int ret;
  214. if (inst->state != INST_LOAD_RESOURCES)
  215. return -EINVAL;
  216. reinit_completion(&inst->done);
  217. ret = ops->session_start(inst);
  218. if (ret)
  219. return ret;
  220. ret = wait_session_msg(inst);
  221. if (ret)
  222. return ret;
  223. inst->state = INST_START;
  224. return 0;
  225. }
  226. int hfi_session_stop(struct venus_inst *inst)
  227. {
  228. const struct hfi_ops *ops = inst->core->ops;
  229. int ret;
  230. if (inst->state != INST_START)
  231. return -EINVAL;
  232. reinit_completion(&inst->done);
  233. ret = ops->session_stop(inst);
  234. if (ret)
  235. return ret;
  236. ret = wait_session_msg(inst);
  237. if (ret)
  238. return ret;
  239. inst->state = INST_STOP;
  240. return 0;
  241. }
  242. int hfi_session_continue(struct venus_inst *inst)
  243. {
  244. struct venus_core *core = inst->core;
  245. if (core->res->hfi_version == HFI_VERSION_1XX)
  246. return 0;
  247. return core->ops->session_continue(inst);
  248. }
  249. EXPORT_SYMBOL_GPL(hfi_session_continue);
  250. int hfi_session_abort(struct venus_inst *inst)
  251. {
  252. const struct hfi_ops *ops = inst->core->ops;
  253. int ret;
  254. reinit_completion(&inst->done);
  255. ret = ops->session_abort(inst);
  256. if (ret)
  257. return ret;
  258. ret = wait_session_msg(inst);
  259. if (ret)
  260. return ret;
  261. return 0;
  262. }
  263. int hfi_session_load_res(struct venus_inst *inst)
  264. {
  265. const struct hfi_ops *ops = inst->core->ops;
  266. int ret;
  267. if (inst->state != INST_INIT)
  268. return -EINVAL;
  269. reinit_completion(&inst->done);
  270. ret = ops->session_load_res(inst);
  271. if (ret)
  272. return ret;
  273. ret = wait_session_msg(inst);
  274. if (ret)
  275. return ret;
  276. inst->state = INST_LOAD_RESOURCES;
  277. return 0;
  278. }
  279. int hfi_session_unload_res(struct venus_inst *inst)
  280. {
  281. const struct hfi_ops *ops = inst->core->ops;
  282. int ret;
  283. if (inst->state != INST_STOP)
  284. return -EINVAL;
  285. reinit_completion(&inst->done);
  286. ret = ops->session_release_res(inst);
  287. if (ret)
  288. return ret;
  289. ret = wait_session_msg(inst);
  290. if (ret)
  291. return ret;
  292. inst->state = INST_RELEASE_RESOURCES;
  293. return 0;
  294. }
  295. int hfi_session_flush(struct venus_inst *inst)
  296. {
  297. const struct hfi_ops *ops = inst->core->ops;
  298. int ret;
  299. reinit_completion(&inst->done);
  300. ret = ops->session_flush(inst, HFI_FLUSH_ALL);
  301. if (ret)
  302. return ret;
  303. ret = wait_session_msg(inst);
  304. if (ret)
  305. return ret;
  306. return 0;
  307. }
  308. EXPORT_SYMBOL_GPL(hfi_session_flush);
  309. int hfi_session_set_buffers(struct venus_inst *inst, struct hfi_buffer_desc *bd)
  310. {
  311. const struct hfi_ops *ops = inst->core->ops;
  312. return ops->session_set_buffers(inst, bd);
  313. }
  314. int hfi_session_unset_buffers(struct venus_inst *inst,
  315. struct hfi_buffer_desc *bd)
  316. {
  317. const struct hfi_ops *ops = inst->core->ops;
  318. int ret;
  319. reinit_completion(&inst->done);
  320. ret = ops->session_unset_buffers(inst, bd);
  321. if (ret)
  322. return ret;
  323. if (!bd->response_required)
  324. return 0;
  325. ret = wait_session_msg(inst);
  326. if (ret)
  327. return ret;
  328. return 0;
  329. }
  330. int hfi_session_get_property(struct venus_inst *inst, u32 ptype,
  331. union hfi_get_property *hprop)
  332. {
  333. const struct hfi_ops *ops = inst->core->ops;
  334. int ret;
  335. if (inst->state < INST_INIT || inst->state >= INST_STOP)
  336. return -EINVAL;
  337. reinit_completion(&inst->done);
  338. ret = ops->session_get_property(inst, ptype);
  339. if (ret)
  340. return ret;
  341. ret = wait_session_msg(inst);
  342. if (ret)
  343. return ret;
  344. *hprop = inst->hprop;
  345. return 0;
  346. }
  347. EXPORT_SYMBOL_GPL(hfi_session_get_property);
  348. int hfi_session_set_property(struct venus_inst *inst, u32 ptype, void *pdata)
  349. {
  350. const struct hfi_ops *ops = inst->core->ops;
  351. if (inst->state < INST_INIT || inst->state >= INST_STOP)
  352. return -EINVAL;
  353. return ops->session_set_property(inst, ptype, pdata);
  354. }
  355. EXPORT_SYMBOL_GPL(hfi_session_set_property);
  356. int hfi_session_process_buf(struct venus_inst *inst, struct hfi_frame_data *fd)
  357. {
  358. const struct hfi_ops *ops = inst->core->ops;
  359. if (fd->buffer_type == HFI_BUFFER_INPUT)
  360. return ops->session_etb(inst, fd);
  361. else if (fd->buffer_type == HFI_BUFFER_OUTPUT ||
  362. fd->buffer_type == HFI_BUFFER_OUTPUT2)
  363. return ops->session_ftb(inst, fd);
  364. return -EINVAL;
  365. }
  366. EXPORT_SYMBOL_GPL(hfi_session_process_buf);
  367. irqreturn_t hfi_isr_thread(int irq, void *dev_id)
  368. {
  369. struct venus_core *core = dev_id;
  370. return core->ops->isr_thread(core);
  371. }
  372. irqreturn_t hfi_isr(int irq, void *dev)
  373. {
  374. struct venus_core *core = dev;
  375. return core->ops->isr(core);
  376. }
  377. int hfi_create(struct venus_core *core, const struct hfi_core_ops *ops)
  378. {
  379. int ret;
  380. if (!ops)
  381. return -EINVAL;
  382. atomic_set(&core->insts_count, 0);
  383. core->core_ops = ops;
  384. core->state = CORE_UNINIT;
  385. init_completion(&core->done);
  386. pkt_set_version(core->res->hfi_version);
  387. ret = venus_hfi_create(core);
  388. return ret;
  389. }
  390. void hfi_destroy(struct venus_core *core)
  391. {
  392. venus_hfi_destroy(core);
  393. }