ttm_global.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "ttm/ttm_module.h"
  31. #include <linux/mutex.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. struct ttm_global_item {
  35. struct mutex mutex;
  36. void *object;
  37. int refcount;
  38. };
  39. static struct ttm_global_item glob[TTM_GLOBAL_NUM];
  40. void ttm_global_init(void)
  41. {
  42. int i;
  43. for (i = 0; i < TTM_GLOBAL_NUM; ++i) {
  44. struct ttm_global_item *item = &glob[i];
  45. mutex_init(&item->mutex);
  46. item->object = NULL;
  47. item->refcount = 0;
  48. }
  49. }
  50. void ttm_global_release(void)
  51. {
  52. int i;
  53. for (i = 0; i < TTM_GLOBAL_NUM; ++i) {
  54. struct ttm_global_item *item = &glob[i];
  55. BUG_ON(item->object != NULL);
  56. BUG_ON(item->refcount != 0);
  57. }
  58. }
  59. int ttm_global_item_ref(struct ttm_global_reference *ref)
  60. {
  61. int ret;
  62. struct ttm_global_item *item = &glob[ref->global_type];
  63. void *object;
  64. mutex_lock(&item->mutex);
  65. if (item->refcount == 0) {
  66. item->object = kmalloc(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. ++item->refcount;
  76. }
  77. ref->object = item->object;
  78. object = item->object;
  79. mutex_unlock(&item->mutex);
  80. return 0;
  81. out_err:
  82. kfree(item->object);
  83. mutex_unlock(&item->mutex);
  84. item->object = NULL;
  85. return ret;
  86. }
  87. EXPORT_SYMBOL(ttm_global_item_ref);
  88. void ttm_global_item_unref(struct ttm_global_reference *ref)
  89. {
  90. struct ttm_global_item *item = &glob[ref->global_type];
  91. mutex_lock(&item->mutex);
  92. BUG_ON(item->refcount == 0);
  93. BUG_ON(ref->object != item->object);
  94. if (--item->refcount == 0) {
  95. ref->release(ref);
  96. kfree(item->object);
  97. item->object = NULL;
  98. }
  99. mutex_unlock(&item->mutex);
  100. }
  101. EXPORT_SYMBOL(ttm_global_item_unref);