drm_global.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**************************************************************************
  2. *
  3. * Copyright 2008-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  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. int drm_global_item_ref(struct drm_global_reference *ref)
  60. {
  61. int ret;
  62. struct drm_global_item *item = &glob[ref->global_type];
  63. mutex_lock(&item->mutex);
  64. if (item->refcount == 0) {
  65. item->object = kzalloc(ref->size, GFP_KERNEL);
  66. if (unlikely(item->object == NULL)) {
  67. ret = -ENOMEM;
  68. goto out_err;
  69. }
  70. ref->object = item->object;
  71. ret = ref->init(ref);
  72. if (unlikely(ret != 0))
  73. goto out_err;
  74. }
  75. ++item->refcount;
  76. ref->object = item->object;
  77. mutex_unlock(&item->mutex);
  78. return 0;
  79. out_err:
  80. mutex_unlock(&item->mutex);
  81. item->object = NULL;
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(drm_global_item_ref);
  85. void drm_global_item_unref(struct drm_global_reference *ref)
  86. {
  87. struct drm_global_item *item = &glob[ref->global_type];
  88. mutex_lock(&item->mutex);
  89. BUG_ON(item->refcount == 0);
  90. BUG_ON(ref->object != item->object);
  91. if (--item->refcount == 0) {
  92. ref->release(ref);
  93. item->object = NULL;
  94. }
  95. mutex_unlock(&item->mutex);
  96. }
  97. EXPORT_SYMBOL(drm_global_item_unref);