drm_global.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #include <linux/mutex.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <drm/drm_global.h>
  34. struct drm_global_item {
  35. struct mutex mutex;
  36. void *object;
  37. int refcount;
  38. };
  39. static struct drm_global_item glob[DRM_GLOBAL_NUM];
  40. void drm_global_init(void)
  41. {
  42. int i;
  43. for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
  44. struct drm_global_item *item = &glob[i];
  45. mutex_init(&item->mutex);
  46. item->object = NULL;
  47. item->refcount = 0;
  48. }
  49. }
  50. void drm_global_release(void)
  51. {
  52. int i;
  53. for (i = 0; i < DRM_GLOBAL_NUM; ++i) {
  54. struct drm_global_item *item = &glob[i];
  55. BUG_ON(item->object != NULL);
  56. BUG_ON(item->refcount != 0);
  57. }
  58. }
  59. /**
  60. * drm_global_item_ref - Initialize and acquire reference to memory
  61. * object
  62. * @ref: Object for initialization
  63. *
  64. * This initializes a memory object, allocating memory and calling the
  65. * .init() hook. Further calls will increase the reference count for
  66. * that item.
  67. *
  68. * Returns:
  69. * Zero on success, non-zero otherwise.
  70. */
  71. int drm_global_item_ref(struct drm_global_reference *ref)
  72. {
  73. int ret = 0;
  74. struct drm_global_item *item = &glob[ref->global_type];
  75. mutex_lock(&item->mutex);
  76. if (item->refcount == 0) {
  77. ref->object = kzalloc(ref->size, GFP_KERNEL);
  78. if (unlikely(ref->object == NULL)) {
  79. ret = -ENOMEM;
  80. goto error_unlock;
  81. }
  82. ret = ref->init(ref);
  83. if (unlikely(ret != 0))
  84. goto error_free;
  85. item->object = ref->object;
  86. } else {
  87. ref->object = item->object;
  88. }
  89. ++item->refcount;
  90. mutex_unlock(&item->mutex);
  91. return 0;
  92. error_free:
  93. kfree(ref->object);
  94. ref->object = NULL;
  95. error_unlock:
  96. mutex_unlock(&item->mutex);
  97. return ret;
  98. }
  99. EXPORT_SYMBOL(drm_global_item_ref);
  100. /**
  101. * drm_global_item_unref - Drop reference to memory
  102. * object
  103. * @ref: Object being removed
  104. *
  105. * Drop a reference to the memory object and eventually call the
  106. * release() hook. The allocated object should be dropped in the
  107. * release() hook or before calling this function
  108. *
  109. */
  110. void drm_global_item_unref(struct drm_global_reference *ref)
  111. {
  112. struct drm_global_item *item = &glob[ref->global_type];
  113. mutex_lock(&item->mutex);
  114. BUG_ON(item->refcount == 0);
  115. BUG_ON(ref->object != item->object);
  116. if (--item->refcount == 0) {
  117. ref->release(ref);
  118. item->object = NULL;
  119. }
  120. mutex_unlock(&item->mutex);
  121. }
  122. EXPORT_SYMBOL(drm_global_item_unref);