etnaviv_bo.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "etnaviv_priv.h"
  27. #include "etnaviv_drmif.h"
  28. drm_private pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
  29. drm_private void bo_del(struct etna_bo *bo);
  30. /* set buffer name, and add to table, call w/ table_lock held: */
  31. static void set_name(struct etna_bo *bo, uint32_t name)
  32. {
  33. bo->name = name;
  34. /* add ourself into the name table: */
  35. drmHashInsert(bo->dev->name_table, name, bo);
  36. }
  37. /* Called under table_lock */
  38. drm_private void bo_del(struct etna_bo *bo)
  39. {
  40. if (bo->map)
  41. drm_munmap(bo->map, bo->size);
  42. if (bo->name)
  43. drmHashDelete(bo->dev->name_table, bo->name);
  44. if (bo->handle) {
  45. drmHashDelete(bo->dev->handle_table, bo->handle);
  46. drmCloseBufferHandle(bo->dev->fd, bo->handle);
  47. }
  48. free(bo);
  49. }
  50. /* lookup a buffer from it's handle, call w/ table_lock held: */
  51. static struct etna_bo *lookup_bo(void *tbl, uint32_t handle)
  52. {
  53. struct etna_bo *bo = NULL;
  54. if (!drmHashLookup(tbl, handle, (void **)&bo)) {
  55. /* found, incr refcnt and return: */
  56. bo = etna_bo_ref(bo);
  57. /* don't break the bucket if this bo was found in one */
  58. list_delinit(&bo->list);
  59. }
  60. return bo;
  61. }
  62. /* allocate a new buffer object, call w/ table_lock held */
  63. static struct etna_bo *bo_from_handle(struct etna_device *dev,
  64. uint32_t size, uint32_t handle, uint32_t flags)
  65. {
  66. struct etna_bo *bo = calloc(sizeof(*bo), 1);
  67. if (!bo) {
  68. drmCloseBufferHandle(dev->fd, handle);
  69. return NULL;
  70. }
  71. bo->dev = etna_device_ref(dev);
  72. bo->size = size;
  73. bo->handle = handle;
  74. bo->flags = flags;
  75. atomic_set(&bo->refcnt, 1);
  76. list_inithead(&bo->list);
  77. /* add ourselves to the handle table: */
  78. drmHashInsert(dev->handle_table, handle, bo);
  79. return bo;
  80. }
  81. /* allocate a new (un-tiled) buffer object */
  82. drm_public struct etna_bo *etna_bo_new(struct etna_device *dev, uint32_t size,
  83. uint32_t flags)
  84. {
  85. struct etna_bo *bo;
  86. int ret;
  87. struct drm_etnaviv_gem_new req = {
  88. .flags = flags,
  89. };
  90. bo = etna_bo_cache_alloc(&dev->bo_cache, &size, flags);
  91. if (bo)
  92. return bo;
  93. req.size = size;
  94. ret = drmCommandWriteRead(dev->fd, DRM_ETNAVIV_GEM_NEW,
  95. &req, sizeof(req));
  96. if (ret)
  97. return NULL;
  98. pthread_mutex_lock(&table_lock);
  99. bo = bo_from_handle(dev, size, req.handle, flags);
  100. bo->reuse = 1;
  101. pthread_mutex_unlock(&table_lock);
  102. return bo;
  103. }
  104. drm_public struct etna_bo *etna_bo_ref(struct etna_bo *bo)
  105. {
  106. atomic_inc(&bo->refcnt);
  107. return bo;
  108. }
  109. /* get buffer info */
  110. static int get_buffer_info(struct etna_bo *bo)
  111. {
  112. int ret;
  113. struct drm_etnaviv_gem_info req = {
  114. .handle = bo->handle,
  115. };
  116. ret = drmCommandWriteRead(bo->dev->fd, DRM_ETNAVIV_GEM_INFO,
  117. &req, sizeof(req));
  118. if (ret) {
  119. return ret;
  120. }
  121. /* really all we need for now is mmap offset */
  122. bo->offset = req.offset;
  123. return 0;
  124. }
  125. /* import a buffer object from DRI2 name */
  126. drm_public struct etna_bo *etna_bo_from_name(struct etna_device *dev,
  127. uint32_t name)
  128. {
  129. struct etna_bo *bo;
  130. struct drm_gem_open req = {
  131. .name = name,
  132. };
  133. pthread_mutex_lock(&table_lock);
  134. /* check name table first, to see if bo is already open: */
  135. bo = lookup_bo(dev->name_table, name);
  136. if (bo)
  137. goto out_unlock;
  138. if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
  139. ERROR_MSG("gem-open failed: %s", strerror(errno));
  140. goto out_unlock;
  141. }
  142. bo = lookup_bo(dev->handle_table, req.handle);
  143. if (bo)
  144. goto out_unlock;
  145. bo = bo_from_handle(dev, req.size, req.handle, 0);
  146. if (bo)
  147. set_name(bo, name);
  148. out_unlock:
  149. pthread_mutex_unlock(&table_lock);
  150. return bo;
  151. }
  152. /* import a buffer from dmabuf fd, does not take ownership of the
  153. * fd so caller should close() the fd when it is otherwise done
  154. * with it (even if it is still using the 'struct etna_bo *')
  155. */
  156. drm_public struct etna_bo *etna_bo_from_dmabuf(struct etna_device *dev, int fd)
  157. {
  158. struct etna_bo *bo;
  159. int ret, size;
  160. uint32_t handle;
  161. /* take the lock before calling drmPrimeFDToHandle to avoid
  162. * racing against etna_bo_del, which might invalidate the
  163. * returned handle.
  164. */
  165. pthread_mutex_lock(&table_lock);
  166. ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
  167. if (ret) {
  168. pthread_mutex_unlock(&table_lock);
  169. return NULL;
  170. }
  171. bo = lookup_bo(dev->handle_table, handle);
  172. if (bo)
  173. goto out_unlock;
  174. /* lseek() to get bo size */
  175. size = lseek(fd, 0, SEEK_END);
  176. lseek(fd, 0, SEEK_CUR);
  177. bo = bo_from_handle(dev, size, handle, 0);
  178. out_unlock:
  179. pthread_mutex_unlock(&table_lock);
  180. return bo;
  181. }
  182. /* destroy a buffer object */
  183. drm_public void etna_bo_del(struct etna_bo *bo)
  184. {
  185. struct etna_device *dev = bo->dev;
  186. if (!bo)
  187. return;
  188. if (!atomic_dec_and_test(&bo->refcnt))
  189. return;
  190. pthread_mutex_lock(&table_lock);
  191. if (bo->reuse && (etna_bo_cache_free(&dev->bo_cache, bo) == 0))
  192. goto out;
  193. bo_del(bo);
  194. etna_device_del_locked(dev);
  195. out:
  196. pthread_mutex_unlock(&table_lock);
  197. }
  198. /* get the global flink/DRI2 buffer name */
  199. drm_public int etna_bo_get_name(struct etna_bo *bo, uint32_t *name)
  200. {
  201. if (!bo->name) {
  202. struct drm_gem_flink req = {
  203. .handle = bo->handle,
  204. };
  205. int ret;
  206. ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
  207. if (ret) {
  208. return ret;
  209. }
  210. pthread_mutex_lock(&table_lock);
  211. set_name(bo, req.name);
  212. pthread_mutex_unlock(&table_lock);
  213. bo->reuse = 0;
  214. }
  215. *name = bo->name;
  216. return 0;
  217. }
  218. drm_public uint32_t etna_bo_handle(struct etna_bo *bo)
  219. {
  220. return bo->handle;
  221. }
  222. /* caller owns the dmabuf fd that is returned and is responsible
  223. * to close() it when done
  224. */
  225. drm_public int etna_bo_dmabuf(struct etna_bo *bo)
  226. {
  227. int ret, prime_fd;
  228. ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
  229. &prime_fd);
  230. if (ret) {
  231. ERROR_MSG("failed to get dmabuf fd: %d", ret);
  232. return ret;
  233. }
  234. bo->reuse = 0;
  235. return prime_fd;
  236. }
  237. drm_public uint32_t etna_bo_size(struct etna_bo *bo)
  238. {
  239. return bo->size;
  240. }
  241. drm_public void *etna_bo_map(struct etna_bo *bo)
  242. {
  243. if (!bo->map) {
  244. if (!bo->offset) {
  245. get_buffer_info(bo);
  246. }
  247. bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
  248. MAP_SHARED, bo->dev->fd, bo->offset);
  249. if (bo->map == MAP_FAILED) {
  250. ERROR_MSG("mmap failed: %s", strerror(errno));
  251. bo->map = NULL;
  252. }
  253. }
  254. return bo->map;
  255. }
  256. drm_public int etna_bo_cpu_prep(struct etna_bo *bo, uint32_t op)
  257. {
  258. struct drm_etnaviv_gem_cpu_prep req = {
  259. .handle = bo->handle,
  260. .op = op,
  261. };
  262. get_abs_timeout(&req.timeout, 5000000000);
  263. return drmCommandWrite(bo->dev->fd, DRM_ETNAVIV_GEM_CPU_PREP,
  264. &req, sizeof(req));
  265. }
  266. drm_public void etna_bo_cpu_fini(struct etna_bo *bo)
  267. {
  268. struct drm_etnaviv_gem_cpu_fini req = {
  269. .handle = bo->handle,
  270. };
  271. drmCommandWrite(bo->dev->fd, DRM_ETNAVIV_GEM_CPU_FINI,
  272. &req, sizeof(req));
  273. }