sde_vm_msgq.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/haven/hh_msgq.h>
  6. #include <linux/kthread.h>
  7. #include "sde_kms.h"
  8. #include "sde_vm.h"
  9. static void _sde_vm_msgq_process_msg(struct kthread_work *work)
  10. {
  11. struct sde_vm_msg_work *vm_work =
  12. container_of(work, struct sde_vm_msg_work, work);
  13. struct sde_vm_ops *vm_ops = &vm_work->sde_vm->vm_ops;
  14. if (vm_ops->vm_msg_recv_cb)
  15. vm_ops->vm_msg_recv_cb(vm_work->sde_vm, vm_work->msg_buf,
  16. vm_work->msg_size);
  17. kfree(vm_work->msg_buf);
  18. }
  19. static int _sde_vm_msgq_listener(void *data)
  20. {
  21. struct sde_vm *sde_vm = (struct sde_vm *)data;
  22. struct sde_kms *sde_kms = sde_vm->sde_kms;
  23. struct sde_vm_msg_work *vm_work;
  24. struct msm_drm_private *priv;
  25. struct msm_drm_thread *event_thread;
  26. void *buf;
  27. size_t size;
  28. int ret = 0;
  29. priv = sde_kms->dev->dev_private;
  30. event_thread = &priv->event_thread[0];
  31. vm_work = &sde_vm->vm_work;
  32. while (true) {
  33. buf = kzalloc(HH_MSGQ_MAX_MSG_SIZE_BYTES, GFP_KERNEL);
  34. if (!buf)
  35. return -ENOMEM;
  36. ret = hh_msgq_recv(sde_vm->msgq_handle, buf,
  37. HH_MSGQ_MAX_MSG_SIZE_BYTES, &size, 0);
  38. if (ret < 0) {
  39. kfree(buf);
  40. SDE_ERROR("hh_msgq_recv failed, rc=%d\n", ret);
  41. return -EINVAL;
  42. }
  43. vm_work->msg_buf = buf;
  44. vm_work->msg_size = size;
  45. vm_work->sde_vm = sde_vm;
  46. kthread_queue_work(&event_thread->worker, &vm_work->work);
  47. }
  48. return 0;
  49. }
  50. int sde_vm_msgq_send(struct sde_vm *sde_vm, void *msg, size_t msg_size)
  51. {
  52. if (!sde_vm->msgq_handle) {
  53. SDE_ERROR("Failed to send msg, invalid msgq handle\n");
  54. return -EINVAL;
  55. }
  56. if (msg_size > HH_MSGQ_MAX_MSG_SIZE_BYTES) {
  57. SDE_ERROR("msg size unsupported for msgq: %ld > %d\n", msg_size,
  58. HH_MSGQ_MAX_MSG_SIZE_BYTES);
  59. return -E2BIG;
  60. }
  61. return hh_msgq_send(sde_vm->msgq_handle, msg, msg_size, HH_MSGQ_TX_PUSH);
  62. }
  63. int sde_vm_msgq_init(struct sde_vm *sde_vm)
  64. {
  65. struct sde_vm_ops *vm_ops = &sde_vm->vm_ops;
  66. void *msgq_handle = NULL;
  67. struct task_struct *msgq_listener_thread = NULL;
  68. int rc = 0;
  69. msgq_handle = hh_msgq_register(HH_MSGQ_LABEL_DISPLAY);
  70. if (IS_ERR(msgq_handle)) {
  71. SDE_ERROR("hh_msgq_register failed, hdl=%p\n", msgq_handle);
  72. return -EINVAL;
  73. }
  74. sde_vm->msgq_handle = msgq_handle;
  75. if (!vm_ops->vm_msg_recv_cb)
  76. goto done;
  77. msgq_listener_thread = kthread_run(_sde_vm_msgq_listener,
  78. (void *)sde_vm, "disp_msgq_listener");
  79. if (IS_ERR(msgq_listener_thread)) {
  80. SDE_ERROR("kthread creation failed for msgq, hdl: %p\n",
  81. msgq_listener_thread);
  82. rc = -EINVAL;
  83. goto kthread_create_fail;
  84. }
  85. kthread_init_work(&sde_vm->vm_work.work, _sde_vm_msgq_process_msg);
  86. sde_vm->msgq_listener_thread = msgq_listener_thread;
  87. return 0;
  88. kthread_create_fail:
  89. hh_msgq_unregister(msgq_handle);
  90. sde_vm->msgq_handle = NULL;
  91. done:
  92. return rc;
  93. }
  94. void sde_vm_msgq_deinit(struct sde_vm *sde_vm)
  95. {
  96. if (sde_vm->msgq_listener_thread)
  97. kthread_stop(sde_vm->msgq_listener_thread);
  98. if (sde_vm->msgq_handle)
  99. hh_msgq_unregister(sde_vm->msgq_handle);
  100. }