sde_vm_primary.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/notifier.h>
  6. #include <linux/haven/hh_rm_drv.h>
  7. #include <linux/haven/hh_irq_lend.h>
  8. #include <linux/haven/hh_mem_notifier.h>
  9. #include "sde_kms.h"
  10. #include "sde_vm.h"
  11. #include "sde_vm_common.h"
  12. #define to_vm_primary(vm) ((struct sde_vm_primary *)vm)
  13. static bool sde_vm_owns_hw(struct sde_kms *sde_kms)
  14. {
  15. struct sde_vm_primary *sde_vm;
  16. bool owns_irq, owns_mem_io;
  17. sde_vm = to_vm_primary(sde_kms->vm);
  18. owns_irq = !atomic_read(&sde_vm->base.n_irq_lent);
  19. owns_mem_io = (sde_vm->base.io_mem_handle < 0);
  20. return (owns_irq & owns_mem_io);
  21. }
  22. void sde_vm_irq_release_notification_handler(void *req, enum hh_irq_label label)
  23. {
  24. struct sde_vm_primary *sde_vm;
  25. int rc = 0;
  26. if (!req) {
  27. SDE_ERROR("invalid data on release notificaiton\n");
  28. return;
  29. }
  30. sde_vm = to_vm_primary(req);
  31. mutex_lock(&sde_vm->base.vm_res_lock);
  32. rc = hh_irq_reclaim(label);
  33. if (rc) {
  34. SDE_ERROR("failed to reclaim irq label: %d\n", label);
  35. goto notify_end;
  36. }
  37. /**
  38. * Skipping per IRQ label verification since IRQ's are MDSS centric.
  39. * Need to enable addition verifications when per-display IRQ's are
  40. * supported.
  41. */
  42. atomic_dec(&sde_vm->base.n_irq_lent);
  43. SDE_INFO("irq reclaim succeeded for label: %d\n", label);
  44. notify_end:
  45. mutex_unlock(&sde_vm->base.vm_res_lock);
  46. }
  47. static void sde_vm_mem_release_notification_handler(
  48. enum hh_mem_notifier_tag tag, unsigned long notif_type,
  49. void *entry_data, void *notif_msg)
  50. {
  51. struct hh_rm_notif_mem_released_payload *payload;
  52. struct sde_vm_primary *sde_vm;
  53. struct sde_kms *sde_kms;
  54. int rc = 0;
  55. if (notif_type != HH_RM_NOTIF_MEM_RELEASED ||
  56. tag != HH_MEM_NOTIFIER_TAG_DISPLAY)
  57. return;
  58. if (!entry_data || !notif_msg)
  59. return;
  60. payload = (struct hh_rm_notif_mem_released_payload *)notif_msg;
  61. sde_vm = (struct sde_vm_primary *)entry_data;
  62. sde_kms = sde_vm->base.sde_kms;
  63. mutex_lock(&sde_vm->base.vm_res_lock);
  64. if (payload->mem_handle != sde_vm->base.io_mem_handle)
  65. goto notify_end;
  66. rc = hh_rm_mem_reclaim(payload->mem_handle, 0);
  67. if (rc) {
  68. SDE_ERROR("failed to reclaim IO memory, rc=%d\n", rc);
  69. goto notify_end;
  70. }
  71. sde_vm->base.io_mem_handle = -1;
  72. SDE_INFO("mem reclaim succeeded for tag: %d\n", tag);
  73. notify_end:
  74. mutex_unlock(&sde_vm->base.vm_res_lock);
  75. }
  76. static int _sde_vm_lend_notify_registers(struct sde_vm *vm,
  77. struct msm_io_res *io_res)
  78. {
  79. struct sde_vm_primary *sde_vm;
  80. struct hh_acl_desc *acl_desc;
  81. struct hh_sgl_desc *sgl_desc;
  82. struct hh_notify_vmid_desc *vmid_desc;
  83. hh_memparcel_handle_t mem_handle;
  84. hh_vmid_t trusted_vmid;
  85. int rc = 0;
  86. sde_vm = to_vm_primary(vm);
  87. acl_desc = sde_vm_populate_acl(HH_TRUSTED_VM);
  88. if (IS_ERR(acl_desc)) {
  89. SDE_ERROR("failed to populate acl descriptor, rc = %d\n",
  90. PTR_ERR(acl_desc));
  91. return rc;
  92. }
  93. sgl_desc = sde_vm_populate_sgl(io_res);
  94. if (IS_ERR_OR_NULL(sgl_desc)) {
  95. SDE_ERROR("failed to populate sgl descriptor, rc = %d\n",
  96. PTR_ERR(sgl_desc));
  97. goto sgl_fail;
  98. }
  99. rc = hh_rm_mem_lend(HH_RM_MEM_TYPE_IO, 0, SDE_VM_MEM_LABEL,
  100. acl_desc, sgl_desc, NULL, &mem_handle);
  101. if (rc) {
  102. SDE_ERROR("hyp lend failed with error, rc: %d\n", rc);
  103. goto fail;
  104. }
  105. hh_rm_get_vmid(HH_TRUSTED_VM, &trusted_vmid);
  106. vmid_desc = sde_vm_populate_vmid(trusted_vmid);
  107. rc = hh_rm_mem_notify(mem_handle, HH_RM_MEM_NOTIFY_RECIPIENT,
  108. HH_MEM_NOTIFIER_TAG_DISPLAY, vmid_desc);
  109. if (rc) {
  110. SDE_ERROR("hyp mem notify failed, rc = %d\n", rc);
  111. goto notify_fail;
  112. }
  113. sde_vm->base.io_mem_handle = mem_handle;
  114. SDE_INFO("IO memory lend suceeded for tag: %d\n",
  115. HH_MEM_NOTIFIER_TAG_DISPLAY);
  116. notify_fail:
  117. kfree(vmid_desc);
  118. fail:
  119. kfree(sgl_desc);
  120. sgl_fail:
  121. kfree(acl_desc);
  122. return rc;
  123. }
  124. static int _sde_vm_lend_irq(struct sde_vm *vm, struct msm_io_res *io_res)
  125. {
  126. struct sde_vm_primary *sde_vm;
  127. struct sde_vm_irq_desc *irq_desc;
  128. int i, rc = 0;
  129. sde_vm = to_vm_primary(vm);
  130. irq_desc = sde_vm_populate_irq(io_res);
  131. for (i = 0; i < irq_desc->n_irq; i++) {
  132. struct sde_vm_irq_entry *entry = &irq_desc->irq_entries[i];
  133. rc = hh_irq_lend(entry->label, HH_TRUSTED_VM, entry->irq,
  134. sde_vm_irq_release_notification_handler,
  135. sde_vm);
  136. if (rc) {
  137. SDE_ERROR("irq lend failed for irq label: %d, rc=%d\n",
  138. entry->label, rc);
  139. hh_irq_reclaim(entry->label);
  140. return rc;
  141. }
  142. SDE_INFO("vm lend suceeded for IRQ label: %d\n", entry->label);
  143. }
  144. // cache the irq list for validation during release
  145. sde_vm->irq_desc = irq_desc;
  146. atomic_set(&sde_vm->base.n_irq_lent, sde_vm->irq_desc->n_irq);
  147. return rc;
  148. }
  149. static int _sde_vm_release(struct sde_kms *kms)
  150. {
  151. struct msm_io_res io_res;
  152. struct sde_vm_primary *sde_vm;
  153. int rc = 0;
  154. if (!kms->vm)
  155. return 0;
  156. sde_vm = to_vm_primary(kms->vm);
  157. INIT_LIST_HEAD(&io_res.mem);
  158. INIT_LIST_HEAD(&io_res.irq);
  159. rc = sde_vm_get_resources(kms, &io_res);
  160. if (rc) {
  161. SDE_ERROR("fail to get resources\n");
  162. goto assign_fail;
  163. }
  164. mutex_lock(&sde_vm->base.vm_res_lock);
  165. rc = _sde_vm_lend_notify_registers(kms->vm, &io_res);
  166. if (rc) {
  167. SDE_ERROR("fail to lend notify resources\n");
  168. goto assign_fail;
  169. }
  170. rc = _sde_vm_lend_irq(kms->vm, &io_res);
  171. if (rc) {
  172. SDE_ERROR("failed to lend irq's\n");
  173. goto assign_fail;
  174. }
  175. assign_fail:
  176. sde_vm_free_resources(&io_res);
  177. mutex_unlock(&sde_vm->base.vm_res_lock);
  178. return rc;
  179. }
  180. static void _sde_vm_deinit(struct sde_kms *sde_kms, struct sde_vm_ops *ops)
  181. {
  182. struct sde_vm_primary *sde_vm;
  183. if (!sde_kms->vm)
  184. return;
  185. memset(ops, 0, sizeof(*ops));
  186. sde_vm = to_vm_primary(sde_kms->vm);
  187. if (sde_vm->base.mem_notification_cookie)
  188. hh_mem_notifier_unregister(
  189. sde_vm->base.mem_notification_cookie);
  190. if (sde_vm->irq_desc)
  191. sde_vm_free_irq(sde_vm->irq_desc);
  192. kfree(sde_vm);
  193. }
  194. static void _sde_vm_set_ops(struct sde_vm_ops *ops)
  195. {
  196. memset(ops, 0, sizeof(*ops));
  197. ops->vm_release = _sde_vm_release;
  198. ops->vm_owns_hw = sde_vm_owns_hw;
  199. ops->vm_deinit = _sde_vm_deinit;
  200. }
  201. int sde_vm_primary_init(struct sde_kms *kms, struct sde_vm_ops *ops)
  202. {
  203. struct sde_vm_primary *sde_vm;
  204. void *cookie;
  205. int rc = 0;
  206. sde_vm = kzalloc(sizeof(*sde_vm), GFP_KERNEL);
  207. if (!sde_vm)
  208. return -ENOMEM;
  209. _sde_vm_set_ops(ops);
  210. cookie = hh_mem_notifier_register(HH_MEM_NOTIFIER_TAG_DISPLAY,
  211. sde_vm_mem_release_notification_handler, sde_vm);
  212. if (!cookie) {
  213. SDE_ERROR("fails to register RM mem release notifier\n");
  214. rc = -EINVAL;
  215. goto init_fail;
  216. }
  217. sde_vm->base.mem_notification_cookie = cookie;
  218. sde_vm->base.sde_kms = kms;
  219. sde_vm->base.io_mem_handle = -1; // 0 is a valid handle
  220. kms->vm = &sde_vm->base;
  221. mutex_init(&sde_vm->base.vm_res_lock);
  222. return 0;
  223. init_fail:
  224. _sde_vm_deinit(kms, ops);
  225. return rc;
  226. }