sde_hw_blk.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
  6. #include <linux/mutex.h>
  7. #include <linux/errno.h>
  8. #include <linux/slab.h>
  9. #include "sde_hw_mdss.h"
  10. #include "sde_hw_blk.h"
  11. /* Serialization lock for sde_hw_blk_list */
  12. static DEFINE_MUTEX(sde_hw_blk_lock);
  13. /* List of all hw block objects */
  14. static LIST_HEAD(sde_hw_blk_list);
  15. /**
  16. * sde_hw_blk_init - initialize hw block object
  17. * @type: hw block type - enum sde_hw_blk_type
  18. * @id: instance id of the hw block
  19. * @ops: Pointer to block operations
  20. * return: 0 if success; error code otherwise
  21. */
  22. int sde_hw_blk_init(struct sde_hw_blk *hw_blk, u32 type, int id,
  23. struct sde_hw_blk_ops *ops)
  24. {
  25. if (!hw_blk) {
  26. pr_err("invalid parameters\n");
  27. return -EINVAL;
  28. }
  29. INIT_LIST_HEAD(&hw_blk->list);
  30. hw_blk->type = type;
  31. hw_blk->id = id;
  32. atomic_set(&hw_blk->refcount, 0);
  33. if (ops)
  34. hw_blk->ops = *ops;
  35. mutex_lock(&sde_hw_blk_lock);
  36. list_add(&hw_blk->list, &sde_hw_blk_list);
  37. mutex_unlock(&sde_hw_blk_lock);
  38. return 0;
  39. }
  40. /**
  41. * sde_hw_blk_destroy - destroy hw block object.
  42. * @hw_blk: pointer to hw block object
  43. * return: none
  44. */
  45. void sde_hw_blk_destroy(struct sde_hw_blk *hw_blk)
  46. {
  47. if (!hw_blk) {
  48. pr_err("invalid parameters\n");
  49. return;
  50. }
  51. if (atomic_read(&hw_blk->refcount))
  52. pr_err("hw_blk:%d.%d invalid refcount\n", hw_blk->type,
  53. hw_blk->id);
  54. mutex_lock(&sde_hw_blk_lock);
  55. list_del(&hw_blk->list);
  56. mutex_unlock(&sde_hw_blk_lock);
  57. }
  58. /**
  59. * sde_hw_blk_get - get hw_blk from free pool
  60. * @hw_blk: if specified, increment reference count only
  61. * @type: if hw_blk is not specified, allocate the next available of this type
  62. * @id: if specified (>= 0), allocate the given instance of the above type
  63. * return: pointer to hw block object
  64. */
  65. struct sde_hw_blk *sde_hw_blk_get(struct sde_hw_blk *hw_blk, u32 type, int id)
  66. {
  67. struct sde_hw_blk *curr;
  68. int rc, refcount;
  69. if (!hw_blk) {
  70. mutex_lock(&sde_hw_blk_lock);
  71. list_for_each_entry(curr, &sde_hw_blk_list, list) {
  72. if ((curr->type != type) ||
  73. (id >= 0 && curr->id != id) ||
  74. (id < 0 &&
  75. atomic_read(&curr->refcount)))
  76. continue;
  77. hw_blk = curr;
  78. break;
  79. }
  80. mutex_unlock(&sde_hw_blk_lock);
  81. }
  82. if (!hw_blk) {
  83. pr_debug("no hw_blk:%d\n", type);
  84. return NULL;
  85. }
  86. refcount = atomic_inc_return(&hw_blk->refcount);
  87. if (refcount == 1 && hw_blk->ops.start) {
  88. rc = hw_blk->ops.start(hw_blk);
  89. if (rc) {
  90. pr_err("failed to start hw_blk:%d rc:%d\n", type, rc);
  91. goto error_start;
  92. }
  93. }
  94. pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk->type,
  95. hw_blk->id, refcount);
  96. return hw_blk;
  97. error_start:
  98. sde_hw_blk_put(hw_blk);
  99. return ERR_PTR(rc);
  100. }
  101. /**
  102. * sde_hw_blk_put - put hw_blk to free pool if decremented refcount is zero
  103. * @hw_blk: hw block to be freed
  104. * @free_blk: function to be called when reference count goes to zero
  105. */
  106. void sde_hw_blk_put(struct sde_hw_blk *hw_blk)
  107. {
  108. if (!hw_blk) {
  109. pr_err("invalid parameters\n");
  110. return;
  111. }
  112. pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk->type, hw_blk->id,
  113. atomic_read(&hw_blk->refcount));
  114. if (!atomic_read(&hw_blk->refcount)) {
  115. pr_err("hw_blk:%d.%d invalid put\n", hw_blk->type, hw_blk->id);
  116. return;
  117. }
  118. if (atomic_dec_return(&hw_blk->refcount))
  119. return;
  120. if (hw_blk->ops.stop)
  121. hw_blk->ops.stop(hw_blk);
  122. }