cvp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/init.h>
  9. #include <linux/ioctl.h>
  10. #include <linux/list.h>
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/version.h>
  17. #include <linux/io.h>
  18. #include "msm_cvp_core.h"
  19. #include "msm_cvp_common.h"
  20. #include "msm_cvp_debug.h"
  21. #include "msm_cvp_internal.h"
  22. #include "msm_cvp_res_parse.h"
  23. #include "msm_cvp_resources.h"
  24. #include "msm_cvp_buf.h"
  25. #include "cvp_hfi_api.h"
  26. #include "cvp_private.h"
  27. #include "msm_cvp_clocks.h"
  28. #include "msm_cvp_dsp.h"
  29. #include "msm_cvp.h"
  30. #include "vm/cvp_vm.h"
  31. #define CLASS_NAME "cvp"
  32. #define DRIVER_NAME "cvp"
  33. struct msm_cvp_drv *cvp_driver;
  34. static int cvp_open(struct inode *inode, struct file *filp)
  35. {
  36. struct msm_cvp_inst *inst;
  37. dprintk(CVP_SESS, "%s\n", __func__);
  38. inst = msm_cvp_open(MSM_CVP_USER, current);
  39. if (!inst) {
  40. dprintk(CVP_ERR, "Failed to create cvp instance\n");
  41. return -ENOMEM;
  42. }
  43. filp->private_data = inst;
  44. return 0;
  45. }
  46. static int cvp_close(struct inode *inode, struct file *filp)
  47. {
  48. int rc = 0;
  49. struct msm_cvp_inst *inst = filp->private_data;
  50. rc = msm_cvp_close(inst);
  51. filp->private_data = NULL;
  52. return rc;
  53. }
  54. static unsigned int cvp_poll(struct file *filp, struct poll_table_struct *p)
  55. {
  56. int rc = 0;
  57. struct msm_cvp_inst *inst = filp->private_data;
  58. unsigned long flags = 0;
  59. poll_wait(filp, &inst->event_handler.wq, p);
  60. spin_lock_irqsave(&inst->event_handler.lock, flags);
  61. if (inst->event_handler.event == CVP_SSR_EVENT)
  62. rc |= POLLPRI;
  63. if (inst->event_handler.event == CVP_DUMP_EVENT)
  64. rc |= POLLIN;
  65. inst->event_handler.event = CVP_NO_EVENT;
  66. spin_unlock_irqrestore(&inst->event_handler.lock, flags);
  67. return rc;
  68. }
  69. static const struct file_operations cvp_fops = {
  70. .owner = THIS_MODULE,
  71. .open = cvp_open,
  72. .release = cvp_close,
  73. .unlocked_ioctl = cvp_unblocked_ioctl,
  74. .compat_ioctl = cvp_compat_ioctl,
  75. .poll = cvp_poll,
  76. };
  77. static int read_platform_resources(struct msm_cvp_core *core,
  78. struct platform_device *pdev)
  79. {
  80. int rc = 0;
  81. if (!core || !pdev) {
  82. dprintk(CVP_ERR, "%s: Invalid params %pK %pK\n",
  83. __func__, core, pdev);
  84. return -EINVAL;
  85. }
  86. core->hfi_type = CVP_HFI_IRIS;
  87. core->resources.pdev = pdev;
  88. if (pdev->dev.of_node) {
  89. /* Target supports DT, parse from it */
  90. rc = cvp_read_platform_resources_from_drv_data(core);
  91. rc = cvp_read_platform_resources_from_dt(&core->resources);
  92. } else {
  93. dprintk(CVP_ERR, "pdev node is NULL\n");
  94. rc = -EINVAL;
  95. }
  96. return rc;
  97. }
  98. static int msm_cvp_initialize_core(struct platform_device *pdev,
  99. struct msm_cvp_core *core)
  100. {
  101. int i = 0;
  102. int rc = 0;
  103. if (!core)
  104. return -EINVAL;
  105. rc = read_platform_resources(core, pdev);
  106. if (rc) {
  107. dprintk(CVP_ERR, "Failed to get platform resources\n");
  108. return rc;
  109. }
  110. INIT_LIST_HEAD(&core->instances);
  111. mutex_init(&core->lock);
  112. mutex_init(&core->clk_lock);
  113. core->state = CVP_CORE_UNINIT;
  114. for (i = SYS_MSG_INDEX(SYS_MSG_START);
  115. i <= SYS_MSG_INDEX(SYS_MSG_END); i++) {
  116. init_completion(&core->completions[i]);
  117. }
  118. INIT_WORK(&core->ssr_work, msm_cvp_ssr_handler);
  119. core->ssr_count = 0;
  120. return rc;
  121. }
  122. static ssize_t link_name_show(struct device *dev,
  123. struct device_attribute *attr,
  124. char *buf)
  125. {
  126. struct msm_cvp_core *core = dev_get_drvdata(dev);
  127. if (core)
  128. if (dev == core->dev)
  129. return snprintf(buf, PAGE_SIZE, "msm_cvp\n");
  130. else
  131. return 0;
  132. else
  133. return 0;
  134. }
  135. static DEVICE_ATTR_RO(link_name);
  136. static ssize_t pwr_collapse_delay_store(struct device *dev,
  137. struct device_attribute *attr,
  138. const char *buf, size_t count)
  139. {
  140. unsigned long val = 0;
  141. int rc = 0;
  142. struct msm_cvp_core *core = NULL;
  143. rc = kstrtoul(buf, 0, &val);
  144. if (rc)
  145. return rc;
  146. else if (!val)
  147. return -EINVAL;
  148. core = cvp_driver->cvp_core;
  149. if (!core)
  150. return -EINVAL;
  151. core->resources.msm_cvp_pwr_collapse_delay = val;
  152. return count;
  153. }
  154. static ssize_t pwr_collapse_delay_show(struct device *dev,
  155. struct device_attribute *attr,
  156. char *buf)
  157. {
  158. struct msm_cvp_core *core = NULL;
  159. core = cvp_driver->cvp_core;
  160. if (!core)
  161. return -EINVAL;
  162. return snprintf(buf, PAGE_SIZE, "%u\n",
  163. core->resources.msm_cvp_pwr_collapse_delay);
  164. }
  165. static DEVICE_ATTR_RW(pwr_collapse_delay);
  166. static ssize_t thermal_level_show(struct device *dev,
  167. struct device_attribute *attr,
  168. char *buf)
  169. {
  170. return snprintf(buf, PAGE_SIZE, "%d\n", cvp_driver->thermal_level);
  171. }
  172. static ssize_t thermal_level_store(struct device *dev,
  173. struct device_attribute *attr,
  174. const char *buf, size_t count)
  175. {
  176. int rc = 0, val = 0;
  177. rc = kstrtoint(buf, 0, &val);
  178. if (rc || val < 0) {
  179. dprintk(CVP_WARN,
  180. "Invalid thermal level value: %s\n", buf);
  181. return -EINVAL;
  182. }
  183. dprintk(CVP_PWR, "Thermal level old %d new %d\n",
  184. cvp_driver->thermal_level, val);
  185. if (val == cvp_driver->thermal_level)
  186. return count;
  187. cvp_driver->thermal_level = val;
  188. msm_cvp_comm_handle_thermal_event();
  189. return count;
  190. }
  191. static DEVICE_ATTR_RW(thermal_level);
  192. static ssize_t sku_version_show(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. return scnprintf(buf, PAGE_SIZE, "%d",
  196. cvp_driver->sku_version);
  197. }
  198. static DEVICE_ATTR_RO(sku_version);
  199. static ssize_t boot_store(struct device *dev,
  200. struct device_attribute *attr,
  201. const char *buf, size_t count)
  202. {
  203. int rc = 0, val = 0;
  204. static int booted;
  205. rc = kstrtoint(buf, 0, &val);
  206. if (rc || val < 0) {
  207. dprintk(CVP_WARN,
  208. "Invalid boot value: %s\n", buf);
  209. return -EINVAL;
  210. }
  211. if (val == 1 && booted == 0) {
  212. struct msm_cvp_inst *inst;
  213. inst = msm_cvp_open(MSM_CVP_BOOT, current);
  214. if (!inst) {
  215. dprintk(CVP_ERR,
  216. "Failed to create cvp instance\n");
  217. return -ENOMEM;
  218. }
  219. rc = msm_cvp_close(inst);
  220. if (rc) {
  221. dprintk(CVP_ERR,
  222. "Failed to close cvp instance\n");
  223. return rc;
  224. }
  225. } else if (val == 2) {
  226. struct msm_cvp_inst *inst;
  227. inst = msm_cvp_open(MSM_CVP_USER, current);
  228. if (!inst) {
  229. dprintk(CVP_ERR,
  230. "Failed to create eva instance\n");
  231. return -ENOMEM;
  232. }
  233. rc = msm_cvp_session_create(inst);
  234. if (rc)
  235. dprintk(CVP_ERR, "Failed to create eva session\n");
  236. rc = msm_cvp_close(inst);
  237. if (rc) {
  238. dprintk(CVP_ERR,
  239. "Failed to close eva instance\n");
  240. return rc;
  241. }
  242. }
  243. booted = 1;
  244. return count;
  245. }
  246. static DEVICE_ATTR_WO(boot);
  247. static struct attribute *msm_cvp_core_attrs[] = {
  248. &dev_attr_pwr_collapse_delay.attr,
  249. &dev_attr_thermal_level.attr,
  250. &dev_attr_sku_version.attr,
  251. &dev_attr_link_name.attr,
  252. &dev_attr_boot.attr,
  253. NULL
  254. };
  255. static struct attribute_group msm_cvp_core_attr_group = {
  256. .attrs = msm_cvp_core_attrs,
  257. };
  258. static const struct of_device_id msm_cvp_plat_match[] = {
  259. {.compatible = "qcom,msm-cvp"},
  260. {.compatible = "qcom,msm-cvp,context-bank"},
  261. {.compatible = "qcom,msm-cvp,bus"},
  262. {.compatible = "qcom,msm-cvp,mem-cdsp"},
  263. {}
  264. };
  265. static int msm_probe_cvp_device(struct platform_device *pdev)
  266. {
  267. int rc = 0;
  268. struct msm_cvp_core *core;
  269. if (!cvp_driver) {
  270. dprintk(CVP_ERR, "Invalid cvp driver\n");
  271. return -EINVAL;
  272. }
  273. core = kzalloc(sizeof(*core), GFP_KERNEL);
  274. if (!core)
  275. return -ENOMEM;
  276. core->platform_data = cvp_get_drv_data(&pdev->dev);
  277. dev_set_drvdata(&pdev->dev, core);
  278. rc = msm_cvp_initialize_core(pdev, core);
  279. if (rc) {
  280. dprintk(CVP_ERR, "Failed to init core\n");
  281. goto err_core_init;
  282. }
  283. rc = alloc_chrdev_region(&core->dev_num, 0, 1, DRIVER_NAME);
  284. if (rc < 0) {
  285. dprintk(CVP_ERR, "alloc_chrdev_region failed: %d\n",
  286. rc);
  287. goto err_alloc_chrdev;
  288. }
  289. core->class = class_create(THIS_MODULE, CLASS_NAME);
  290. if (IS_ERR(core->class)) {
  291. rc = PTR_ERR(core->class);
  292. dprintk(CVP_ERR, "class_create failed: %d\n",
  293. rc);
  294. goto err_class_create;
  295. }
  296. core->dev = device_create(core->class, NULL,
  297. core->dev_num, NULL, DRIVER_NAME);
  298. if (IS_ERR(core->dev)) {
  299. rc = PTR_ERR(core->dev);
  300. dprintk(CVP_ERR, "device_create failed: %d\n",
  301. rc);
  302. goto err_device_create;
  303. }
  304. dev_set_drvdata(core->dev, core);
  305. cdev_init(&core->cdev, &cvp_fops);
  306. rc = cdev_add(&core->cdev,
  307. MKDEV(MAJOR(core->dev_num), 0), 1);
  308. if (rc < 0) {
  309. dprintk(CVP_ERR, "cdev_add failed: %d\n",
  310. rc);
  311. goto error_cdev_add;
  312. }
  313. rc = sysfs_create_group(&core->dev->kobj, &msm_cvp_core_attr_group);
  314. if (rc) {
  315. dprintk(CVP_ERR,
  316. "Failed to create attributes\n");
  317. goto err_cores_exceeded;
  318. }
  319. /* VM manager shall be started before HFI init */
  320. vm_manager.vm_ops->vm_start(core);
  321. core->dev_ops = cvp_hfi_initialize(core->hfi_type,
  322. &core->resources, &cvp_handle_cmd_response);
  323. if (IS_ERR_OR_NULL(core->dev_ops)) {
  324. mutex_lock(&cvp_driver->lock);
  325. mutex_unlock(&cvp_driver->lock);
  326. rc = PTR_ERR(core->dev_ops) ?: -EBADHANDLE;
  327. if (rc != -EPROBE_DEFER)
  328. dprintk(CVP_ERR, "Failed to create HFI device\n");
  329. else
  330. dprintk(CVP_CORE, "msm_cvp: request probe defer\n");
  331. goto err_hfi_initialize;
  332. }
  333. cvp_synx_ftbl_init(core);
  334. mutex_lock(&cvp_driver->lock);
  335. cvp_driver->cvp_core = core;
  336. mutex_unlock(&cvp_driver->lock);
  337. cvp_driver->debugfs_root = msm_cvp_debugfs_init_drv();
  338. if (!cvp_driver->debugfs_root)
  339. dprintk(CVP_ERR, "Failed to create debugfs for msm_cvp\n");
  340. core->debugfs_root = msm_cvp_debugfs_init_core(
  341. core, cvp_driver->debugfs_root);
  342. cvp_driver->sku_version = core->resources.sku_version;
  343. dprintk(CVP_CORE, "populating sub devices\n");
  344. /*
  345. * Trigger probe for each sub-device i.e. qcom,msm-cvp,context-bank.
  346. * When msm_cvp_probe is called for each sub-device, parse the
  347. * context-bank details and store it in core->resources.context_banks
  348. * list.
  349. */
  350. rc = of_platform_populate(pdev->dev.of_node, msm_cvp_plat_match, NULL,
  351. &pdev->dev);
  352. if (rc) {
  353. dprintk(CVP_ERR, "Failed to trigger probe for sub-devices\n");
  354. goto err_fail_sub_device_probe;
  355. }
  356. atomic64_set(&core->kernel_trans_id, ARRAY_SIZE(cvp_hfi_defs));
  357. if (core->resources.dsp_enabled) {
  358. rc = cvp_dsp_device_init();
  359. if (rc)
  360. dprintk(CVP_WARN, "Failed to initialize DSP driver\n");
  361. } else {
  362. dprintk(CVP_DSP, "DSP interface not enabled\n");
  363. }
  364. return rc;
  365. err_fail_sub_device_probe:
  366. cvp_hfi_deinitialize(core->hfi_type, core->dev_ops);
  367. debugfs_remove_recursive(cvp_driver->debugfs_root);
  368. err_hfi_initialize:
  369. err_cores_exceeded:
  370. cdev_del(&core->cdev);
  371. error_cdev_add:
  372. device_destroy(core->class, core->dev_num);
  373. err_device_create:
  374. class_destroy(core->class);
  375. err_class_create:
  376. unregister_chrdev_region(core->dev_num, 1);
  377. err_alloc_chrdev:
  378. sysfs_remove_group(&pdev->dev.kobj, &msm_cvp_core_attr_group);
  379. err_core_init:
  380. dev_set_drvdata(&pdev->dev, NULL);
  381. kfree(core);
  382. return rc;
  383. }
  384. static int msm_cvp_probe_mem_cdsp(struct platform_device *pdev)
  385. {
  386. return cvp_read_mem_cdsp_resources_from_dt(pdev);
  387. }
  388. static int msm_cvp_probe_context_bank(struct platform_device *pdev)
  389. {
  390. return cvp_read_context_bank_resources_from_dt(pdev);
  391. }
  392. static int msm_cvp_probe_bus(struct platform_device *pdev)
  393. {
  394. return cvp_read_bus_resources_from_dt(pdev);
  395. }
  396. static int msm_cvp_probe(struct platform_device *pdev)
  397. {
  398. /*
  399. * Sub devices probe will be triggered by of_platform_populate() towards
  400. * the end of the probe function after msm-cvp device probe is
  401. * completed. Return immediately after completing sub-device probe.
  402. */
  403. if (of_device_is_compatible(pdev->dev.of_node, "qcom,msm-cvp")) {
  404. return msm_probe_cvp_device(pdev);
  405. } else if (of_device_is_compatible(pdev->dev.of_node,
  406. "qcom,msm-cvp,bus")) {
  407. return msm_cvp_probe_bus(pdev);
  408. } else if (of_device_is_compatible(pdev->dev.of_node,
  409. "qcom,msm-cvp,context-bank")) {
  410. return msm_cvp_probe_context_bank(pdev);
  411. } else if (of_device_is_compatible(pdev->dev.of_node,
  412. "qcom,msm-cvp,mem-cdsp")) {
  413. return msm_cvp_probe_mem_cdsp(pdev);
  414. }
  415. /* How did we end up here? */
  416. MSM_CVP_ERROR(1);
  417. return -EINVAL;
  418. }
  419. static int msm_cvp_remove(struct platform_device *pdev)
  420. {
  421. int rc = 0;
  422. struct msm_cvp_core *core;
  423. if (!pdev) {
  424. dprintk(CVP_ERR, "%s invalid input %pK", __func__, pdev);
  425. return -EINVAL;
  426. }
  427. core = dev_get_drvdata(&pdev->dev);
  428. if (!core) {
  429. dprintk(CVP_ERR, "%s invalid core", __func__);
  430. return -EINVAL;
  431. }
  432. cvp_hfi_deinitialize(core->hfi_type, core->dev_ops);
  433. msm_cvp_free_platform_resources(&core->resources);
  434. sysfs_remove_group(&pdev->dev.kobj, &msm_cvp_core_attr_group);
  435. dev_set_drvdata(&pdev->dev, NULL);
  436. mutex_destroy(&core->lock);
  437. mutex_destroy(&core->clk_lock);
  438. kfree(core);
  439. return rc;
  440. }
  441. static int msm_cvp_pm_suspend(struct device *dev)
  442. {
  443. int rc = 0;
  444. struct msm_cvp_core *core;
  445. /*
  446. * Bail out if
  447. * - driver possibly not probed yet
  448. * - not the main device. We don't support power management on
  449. * subdevices (e.g. context banks)
  450. */
  451. if (!dev || !dev->driver ||
  452. !of_device_is_compatible(dev->of_node, "qcom,msm-cvp"))
  453. return 0;
  454. core = dev_get_drvdata(dev);
  455. if (!core) {
  456. dprintk(CVP_ERR, "%s invalid core\n", __func__);
  457. return -EINVAL;
  458. }
  459. rc = msm_cvp_suspend();
  460. if (rc == -ENOTSUPP)
  461. rc = 0;
  462. else if (rc)
  463. dprintk(CVP_WARN, "Failed to suspend: %d\n", rc);
  464. return rc;
  465. }
  466. static int msm_cvp_pm_resume(struct device *dev)
  467. {
  468. dprintk(CVP_INFO, "%s\n", __func__);
  469. return 0;
  470. }
  471. static const struct dev_pm_ops msm_cvp_pm_ops = {
  472. SET_SYSTEM_SLEEP_PM_OPS(msm_cvp_pm_suspend, msm_cvp_pm_resume)
  473. };
  474. MODULE_DEVICE_TABLE(of, msm_cvp_plat_match);
  475. static struct platform_driver msm_cvp_driver = {
  476. .probe = msm_cvp_probe,
  477. .remove = msm_cvp_remove,
  478. .driver = {
  479. .name = "msm_cvp",
  480. .of_match_table = msm_cvp_plat_match,
  481. .pm = &msm_cvp_pm_ops,
  482. },
  483. };
  484. static int __init msm_cvp_init(void)
  485. {
  486. int rc = 0;
  487. cvp_driver = kzalloc(sizeof(*cvp_driver), GFP_KERNEL);
  488. if (!cvp_driver) {
  489. dprintk(CVP_ERR,
  490. "Failed to allocate memroy for msm_cvp_drv\n");
  491. return -ENOMEM;
  492. }
  493. mutex_init(&cvp_driver->lock);
  494. rc = platform_driver_register(&msm_cvp_driver);
  495. if (rc) {
  496. dprintk(CVP_ERR,
  497. "Failed to register platform driver\n");
  498. kfree(cvp_driver);
  499. cvp_driver = NULL;
  500. return rc;
  501. }
  502. cvp_driver->msg_cache.cache = KMEM_CACHE(cvp_session_msg, 0);
  503. cvp_driver->frame_cache.cache = KMEM_CACHE(msm_cvp_frame, 0);
  504. cvp_driver->buf_cache.cache = KMEM_CACHE(cvp_internal_buf, 0);
  505. cvp_driver->smem_cache.cache = KMEM_CACHE(msm_cvp_smem, 0);
  506. mutex_init(&wncc_buf_pool.lock);
  507. return rc;
  508. }
  509. static void __exit msm_cvp_exit(void)
  510. {
  511. cvp_dsp_device_exit();
  512. kmem_cache_destroy(cvp_driver->msg_cache.cache);
  513. kmem_cache_destroy(cvp_driver->frame_cache.cache);
  514. kmem_cache_destroy(cvp_driver->buf_cache.cache);
  515. kmem_cache_destroy(cvp_driver->smem_cache.cache);
  516. platform_driver_unregister(&msm_cvp_driver);
  517. debugfs_remove_recursive(cvp_driver->debugfs_root);
  518. mutex_destroy(&cvp_driver->lock);
  519. mutex_destroy(&wncc_buf_pool.lock);
  520. kfree(cvp_driver);
  521. cvp_driver = NULL;
  522. }
  523. module_init(msm_cvp_init);
  524. module_exit(msm_cvp_exit);
  525. MODULE_SOFTDEP("pre: msm-mmrm");
  526. MODULE_SOFTDEP("pre: synx-driver");
  527. MODULE_SOFTDEP("pre: frpc-adsprpc");
  528. MODULE_LICENSE("GPL v2");
  529. MODULE_IMPORT_NS(DMA_BUF);