drm_global.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_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. void *object;
  64. mutex_lock(&item->mutex);
  65. if (item->refcount == 0) {
  66. item->object = kzalloc(ref->size, GFP_KERNEL);
  67. if (unlikely(item->object == NULL)) {
  68. ret = -ENOMEM;
  69. goto out_err;
  70. }
  71. ref->object = item->object;
  72. ret = ref->init(ref);
  73. if (unlikely(ret != 0))
  74. goto out_err;
  75. }
  76. ++item->refcount;
  77. ref->object = item->object;
  78. object = item->object;
  79. mutex_unlock(&item->mutex);
  80. return 0;
  81. out_err:
  82. mutex_unlock(&item->mutex);
  83. item->object = NULL;
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(drm_global_item_ref);
  87. void drm_global_item_unref(struct drm_global_reference *ref)
  88. {
  89. struct drm_global_item *item = &glob[ref->global_type];
  90. mutex_lock(&item->mutex);
  91. BUG_ON(item->refcount == 0);
  92. BUG_ON(ref->object != item->object);
  93. if (--item->refcount == 0) {
  94. ref->release(ref);
  95. item->object = NULL;
  96. }
  97. mutex_unlock(&item->mutex);
  98. }
  99. EXPORT_SYMBOL(drm_global_item_unref);