sde_vm_primary.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  7. #include <linux/notifier.h>
  8. #include <linux/gunyah/gh_rm_drv.h>
  9. #include <linux/gunyah/gh_irq_lend.h>
  10. #include <linux/gunyah/gh_mem_notifier.h>
  11. #include "sde_kms.h"
  12. #include "sde_vm.h"
  13. #include "sde_vm_common.h"
  14. #include "sde_vm_msgq.h"
  15. #define to_vm_primary(vm) ((struct sde_vm_primary *)vm)
  16. static bool _sde_vm_owns_hw(struct sde_kms *sde_kms)
  17. {
  18. struct sde_vm_primary *sde_vm;
  19. bool owns_irq, owns_mem_io;
  20. sde_vm = to_vm_primary(sde_kms->vm);
  21. owns_irq = !atomic_read(&sde_vm->base.n_irq_lent);
  22. owns_mem_io = (sde_vm->base.io_mem_handle < 0);
  23. return (owns_irq & owns_mem_io);
  24. }
  25. void sde_vm_irq_release_notification_handler(void *req,
  26. unsigned long notif_type, enum gh_irq_label label)
  27. {
  28. SDE_INFO("irq release notification for label: %d\n", label);
  29. }
  30. static void sde_vm_mem_release_notification_handler(
  31. enum gh_mem_notifier_tag tag, unsigned long notif_type,
  32. void *entry_data, void *notif_msg)
  33. {
  34. SDE_INFO("mem release notification for tag: %d\n", tag);
  35. }
  36. int _sde_vm_reclaim_mem(struct sde_kms *sde_kms)
  37. {
  38. struct sde_vm_primary *sde_vm = to_vm_primary(sde_kms->vm);
  39. int rc = 0;
  40. if (sde_vm->base.io_mem_handle < 0)
  41. return 0;
  42. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  43. rc = ghd_rm_mem_reclaim(sde_vm->base.io_mem_handle, 0);
  44. #else
  45. rc = gh_rm_mem_reclaim(sde_vm->base.io_mem_handle, 0);
  46. #endif
  47. if (rc) {
  48. SDE_ERROR("failed to reclaim IO memory, rc=%d\n", rc);
  49. return rc;
  50. }
  51. SDE_INFO("mem reclaim succeeded\n");
  52. sde_vm->base.io_mem_handle = -1;
  53. return rc;
  54. }
  55. int _sde_vm_reclaim_irq(struct sde_kms *sde_kms)
  56. {
  57. struct sde_vm_primary *sde_vm = to_vm_primary(sde_kms->vm);
  58. struct sde_vm_irq_desc *irq_desc;
  59. int rc = 0, i;
  60. if (!sde_vm->irq_desc)
  61. return 0;
  62. irq_desc = sde_vm->irq_desc;
  63. for (i = atomic_read(&sde_vm->base.n_irq_lent) - 1; i >= 0; i--) {
  64. struct sde_vm_irq_entry *entry = &irq_desc->irq_entries[i];
  65. rc = gh_irq_reclaim(entry->label);
  66. if (rc) {
  67. SDE_ERROR("failed to reclaim irq label: %d rc = %d\n",
  68. entry->label, rc);
  69. goto reclaim_fail;
  70. }
  71. atomic_dec(&sde_vm->base.n_irq_lent);
  72. SDE_INFO("irq reclaim succeeded for label: %d\n", entry->label);
  73. }
  74. reclaim_fail:
  75. sde_vm_free_irq(sde_vm->irq_desc);
  76. sde_vm->irq_desc = NULL;
  77. atomic_set(&sde_vm->base.n_irq_lent, 0);
  78. return rc;
  79. }
  80. static int _sde_vm_reclaim(struct sde_kms *sde_kms)
  81. {
  82. int rc = 0;
  83. rc = _sde_vm_reclaim_mem(sde_kms);
  84. if (rc) {
  85. SDE_ERROR("vm reclaim mem failed, rc=%d\n", rc);
  86. goto end;
  87. }
  88. rc = _sde_vm_reclaim_irq(sde_kms);
  89. if (rc)
  90. SDE_ERROR("vm reclaim irq failed, rc=%d\n", rc);
  91. end:
  92. return rc;
  93. }
  94. static int _sde_vm_lend_mem(struct sde_vm *vm,
  95. struct msm_io_res *io_res)
  96. {
  97. struct sde_vm_primary *sde_vm;
  98. struct gh_acl_desc *acl_desc;
  99. struct gh_sgl_desc *sgl_desc;
  100. struct gh_notify_vmid_desc *vmid_desc;
  101. gh_memparcel_handle_t mem_handle;
  102. gh_vmid_t trusted_vmid;
  103. int rc = 0;
  104. sde_vm = to_vm_primary(vm);
  105. acl_desc = sde_vm_populate_acl(GH_TRUSTED_VM);
  106. if (IS_ERR(acl_desc)) {
  107. SDE_ERROR("failed to populate acl descriptor, rc = %ld\n",
  108. PTR_ERR(acl_desc));
  109. return -EINVAL;
  110. }
  111. sgl_desc = sde_vm_populate_sgl(io_res);
  112. if (IS_ERR_OR_NULL(sgl_desc)) {
  113. SDE_ERROR("failed to populate sgl descriptor, rc = %ld\n",
  114. PTR_ERR(sgl_desc));
  115. rc = -EINVAL;
  116. goto sgl_fail;
  117. }
  118. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  119. rc = ghd_rm_mem_lend(GH_RM_MEM_TYPE_IO, 0, SDE_VM_MEM_LABEL,
  120. acl_desc, sgl_desc, NULL, &mem_handle);
  121. #else
  122. rc = gh_rm_mem_lend(GH_RM_MEM_TYPE_IO, 0, SDE_VM_MEM_LABEL,
  123. acl_desc, sgl_desc, NULL, &mem_handle);
  124. #endif
  125. if (rc) {
  126. SDE_ERROR("hyp lend failed with error, rc: %d\n", rc);
  127. goto fail;
  128. }
  129. sde_vm->base.io_mem_handle = mem_handle;
  130. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  131. ghd_rm_get_vmid(GH_TRUSTED_VM, &trusted_vmid);
  132. #else
  133. gh_rm_get_vmid(GH_TRUSTED_VM, &trusted_vmid);
  134. #endif
  135. vmid_desc = sde_vm_populate_vmid(trusted_vmid);
  136. rc = gh_rm_mem_notify(mem_handle, GH_RM_MEM_NOTIFY_RECIPIENT_SHARED,
  137. GH_MEM_NOTIFIER_TAG_DISPLAY, vmid_desc);
  138. if (rc) {
  139. SDE_ERROR("hyp mem notify failed, rc = %d\n", rc);
  140. goto notify_fail;
  141. }
  142. SDE_INFO("IO memory lend suceeded for tag: %d\n",
  143. GH_MEM_NOTIFIER_TAG_DISPLAY);
  144. notify_fail:
  145. kfree(vmid_desc);
  146. fail:
  147. kfree(sgl_desc);
  148. sgl_fail:
  149. kfree(acl_desc);
  150. return rc;
  151. }
  152. static int _sde_vm_lend_irq(struct sde_vm *vm, struct msm_io_res *io_res)
  153. {
  154. struct sde_vm_primary *sde_vm;
  155. struct sde_vm_irq_desc *irq_desc;
  156. int i, rc = 0;
  157. sde_vm = to_vm_primary(vm);
  158. irq_desc = sde_vm_populate_irq(io_res);
  159. /* cache the irq list for validation during reclaim */
  160. sde_vm->irq_desc = irq_desc;
  161. for (i = 0; i < irq_desc->n_irq; i++) {
  162. struct sde_vm_irq_entry *entry = &irq_desc->irq_entries[i];
  163. rc = gh_irq_lend_v2(entry->label, GH_TRUSTED_VM, entry->irq,
  164. sde_vm_irq_release_notification_handler,
  165. sde_vm);
  166. if (rc) {
  167. SDE_ERROR("irq lend failed for irq label: %d, rc=%d\n",
  168. entry->label, rc);
  169. goto done;
  170. }
  171. atomic_inc(&sde_vm->base.n_irq_lent);
  172. rc = gh_irq_lend_notify(entry->label);
  173. if (rc) {
  174. SDE_ERROR("irq lend notify failed, label: %d, rc=%d\n",
  175. entry->label, rc);
  176. goto done;
  177. }
  178. SDE_INFO("vm lend suceeded for IRQ label: %d\n", entry->label);
  179. }
  180. done:
  181. return rc;
  182. }
  183. static int _sde_vm_release(struct sde_kms *kms)
  184. {
  185. struct msm_io_res io_res;
  186. struct sde_vm_primary *sde_vm;
  187. int rc = 0;
  188. if (!kms->vm)
  189. return 0;
  190. sde_vm = to_vm_primary(kms->vm);
  191. INIT_LIST_HEAD(&io_res.mem);
  192. INIT_LIST_HEAD(&io_res.irq);
  193. rc = sde_vm_get_resources(kms, &io_res);
  194. if (rc) {
  195. SDE_ERROR("fail to get resources\n");
  196. goto done;
  197. }
  198. rc = _sde_vm_lend_mem(kms->vm, &io_res);
  199. if (rc) {
  200. SDE_ERROR("fail to lend notify resources\n");
  201. goto res_lend_fail;
  202. }
  203. rc = _sde_vm_lend_irq(kms->vm, &io_res);
  204. if (rc) {
  205. SDE_ERROR("failed to lend irq's\n");
  206. goto res_lend_fail;
  207. }
  208. goto done;
  209. res_lend_fail:
  210. _sde_vm_reclaim(kms);
  211. done:
  212. sde_vm_free_resources(&io_res);
  213. return rc;
  214. }
  215. static void _sde_vm_deinit(struct sde_kms *sde_kms, struct sde_vm_ops *ops)
  216. {
  217. struct sde_vm_primary *sde_vm;
  218. if (!sde_kms->vm)
  219. return;
  220. memset(ops, 0, sizeof(*ops));
  221. sde_vm = to_vm_primary(sde_kms->vm);
  222. sde_vm_msgq_deinit(sde_kms->vm);
  223. if (sde_vm->base.mem_notification_cookie)
  224. gh_mem_notifier_unregister(
  225. sde_vm->base.mem_notification_cookie);
  226. if (sde_vm->irq_desc)
  227. sde_vm_free_irq(sde_vm->irq_desc);
  228. kfree(sde_vm);
  229. }
  230. static void _sde_vm_set_ops(struct sde_vm_ops *ops)
  231. {
  232. memset(ops, 0, sizeof(*ops));
  233. ops->vm_client_pre_release = sde_vm_pre_release;
  234. ops->vm_client_post_acquire = sde_vm_post_acquire;
  235. ops->vm_release = _sde_vm_release;
  236. ops->vm_acquire = _sde_vm_reclaim;
  237. ops->vm_owns_hw = _sde_vm_owns_hw;
  238. ops->vm_deinit = _sde_vm_deinit;
  239. ops->vm_prepare_commit = sde_kms_vm_primary_prepare_commit;
  240. ops->vm_post_commit = sde_kms_vm_primary_post_commit;
  241. ops->vm_request_valid = sde_vm_request_valid;
  242. ops->vm_msg_send = sde_vm_msg_send;
  243. }
  244. int sde_vm_primary_init(struct sde_kms *kms)
  245. {
  246. struct sde_vm_primary *sde_vm;
  247. void *cookie;
  248. int rc = 0;
  249. sde_vm = kzalloc(sizeof(*sde_vm), GFP_KERNEL);
  250. if (!sde_vm)
  251. return -ENOMEM;
  252. _sde_vm_set_ops(&sde_vm->base.vm_ops);
  253. cookie = gh_mem_notifier_register(GH_MEM_NOTIFIER_TAG_DISPLAY,
  254. sde_vm_mem_release_notification_handler, sde_vm);
  255. if (!cookie) {
  256. SDE_ERROR("fails to register RM mem release notifier\n");
  257. rc = -EINVAL;
  258. goto init_fail;
  259. }
  260. sde_vm->base.mem_notification_cookie = cookie;
  261. sde_vm->base.sde_kms = kms;
  262. sde_vm->base.io_mem_handle = -1; // 0 is a valid handle
  263. kms->vm = &sde_vm->base;
  264. mutex_init(&sde_vm->base.vm_res_lock);
  265. rc = sde_vm_msgq_init(kms->vm);
  266. if (rc) {
  267. SDE_ERROR("failed to initialize the msgq, rc=%d\n", rc);
  268. goto init_fail;
  269. }
  270. return 0;
  271. init_fail:
  272. _sde_vm_deinit(kms, &sde_vm->base.vm_ops);
  273. return rc;
  274. }