etnaviv_device.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2014 Etnaviv Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Christian Gmeiner <christian.gmeiner@gmail.com>
  25. */
  26. #include <stdlib.h>
  27. #include <sys/types.h>
  28. #include <errno.h>
  29. #include <sys/mman.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <pthread.h>
  33. #include <xf86drm.h>
  34. #include <xf86atomic.h>
  35. #include "etnaviv_priv.h"
  36. #include "etnaviv_drmif.h"
  37. static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
  38. drm_public struct etna_device *etna_device_new(int fd)
  39. {
  40. struct etna_device *dev = calloc(sizeof(*dev), 1);
  41. if (!dev)
  42. return NULL;
  43. atomic_set(&dev->refcnt, 1);
  44. dev->fd = fd;
  45. dev->handle_table = drmHashCreate();
  46. dev->name_table = drmHashCreate();
  47. etna_bo_cache_init(&dev->bo_cache);
  48. return dev;
  49. }
  50. /* like etna_device_new() but creates it's own private dup() of the fd
  51. * which is close()d when the device is finalized. */
  52. drm_public struct etna_device *etna_device_new_dup(int fd)
  53. {
  54. int dup_fd = dup(fd);
  55. struct etna_device *dev = etna_device_new(dup_fd);
  56. if (dev)
  57. dev->closefd = 1;
  58. else
  59. close(dup_fd);
  60. return dev;
  61. }
  62. drm_public struct etna_device *etna_device_ref(struct etna_device *dev)
  63. {
  64. atomic_inc(&dev->refcnt);
  65. return dev;
  66. }
  67. static void etna_device_del_impl(struct etna_device *dev)
  68. {
  69. etna_bo_cache_cleanup(&dev->bo_cache, 0);
  70. drmHashDestroy(dev->handle_table);
  71. drmHashDestroy(dev->name_table);
  72. if (dev->closefd)
  73. close(dev->fd);
  74. free(dev);
  75. }
  76. drm_private void etna_device_del_locked(struct etna_device *dev)
  77. {
  78. if (!atomic_dec_and_test(&dev->refcnt))
  79. return;
  80. etna_device_del_impl(dev);
  81. }
  82. drm_public void etna_device_del(struct etna_device *dev)
  83. {
  84. if (!atomic_dec_and_test(&dev->refcnt))
  85. return;
  86. pthread_mutex_lock(&table_lock);
  87. etna_device_del_impl(dev);
  88. pthread_mutex_unlock(&table_lock);
  89. }
  90. drm_public int etna_device_fd(struct etna_device *dev)
  91. {
  92. return dev->fd;
  93. }