omap_drm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
  2. /*
  3. * Copyright (C) 2011 Texas Instruments, Inc
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Authors:
  25. * Rob Clark <rob@ti.com>
  26. */
  27. #include <stdlib.h>
  28. #include <errno.h>
  29. #include <sys/mman.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <pthread.h>
  33. #include <libdrm_macros.h>
  34. #include <xf86drm.h>
  35. #include <xf86atomic.h>
  36. #include "omap_drm.h"
  37. #include "omap_drmif.h"
  38. static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
  39. static void * dev_table;
  40. struct omap_device {
  41. int fd;
  42. atomic_t refcnt;
  43. /* The handle_table is used to track GEM bo handles associated w/
  44. * this fd. This is needed, in particular, when importing
  45. * dmabuf's because we don't want multiple 'struct omap_bo's
  46. * floating around with the same handle. Otherwise, when the
  47. * first one is omap_bo_del()'d the handle becomes no longer
  48. * valid, and the remaining 'struct omap_bo's are left pointing
  49. * to an invalid handle (and possible a GEM bo that is already
  50. * free'd).
  51. */
  52. void *handle_table;
  53. };
  54. /* a GEM buffer object allocated from the DRM device */
  55. struct omap_bo {
  56. struct omap_device *dev;
  57. void *map; /* userspace mmap'ing (if there is one) */
  58. uint32_t size;
  59. uint32_t handle;
  60. uint32_t name; /* flink global handle (DRI2 name) */
  61. uint64_t offset; /* offset to mmap() */
  62. int fd; /* dmabuf handle */
  63. atomic_t refcnt;
  64. };
  65. static struct omap_device * omap_device_new_impl(int fd)
  66. {
  67. struct omap_device *dev = calloc(sizeof(*dev), 1);
  68. if (!dev)
  69. return NULL;
  70. dev->fd = fd;
  71. atomic_set(&dev->refcnt, 1);
  72. dev->handle_table = drmHashCreate();
  73. return dev;
  74. }
  75. drm_public struct omap_device * omap_device_new(int fd)
  76. {
  77. struct omap_device *dev = NULL;
  78. pthread_mutex_lock(&table_lock);
  79. if (!dev_table)
  80. dev_table = drmHashCreate();
  81. if (drmHashLookup(dev_table, fd, (void **)&dev)) {
  82. /* not found, create new device */
  83. dev = omap_device_new_impl(fd);
  84. drmHashInsert(dev_table, fd, dev);
  85. } else {
  86. /* found, just incr refcnt */
  87. dev = omap_device_ref(dev);
  88. }
  89. pthread_mutex_unlock(&table_lock);
  90. return dev;
  91. }
  92. drm_public struct omap_device * omap_device_ref(struct omap_device *dev)
  93. {
  94. atomic_inc(&dev->refcnt);
  95. return dev;
  96. }
  97. drm_public void omap_device_del(struct omap_device *dev)
  98. {
  99. if (!atomic_dec_and_test(&dev->refcnt))
  100. return;
  101. pthread_mutex_lock(&table_lock);
  102. drmHashDestroy(dev->handle_table);
  103. drmHashDelete(dev_table, dev->fd);
  104. pthread_mutex_unlock(&table_lock);
  105. free(dev);
  106. }
  107. drm_public int
  108. omap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
  109. {
  110. struct drm_omap_param req = {
  111. .param = param,
  112. };
  113. int ret;
  114. ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
  115. if (ret) {
  116. return ret;
  117. }
  118. *value = req.value;
  119. return 0;
  120. }
  121. drm_public int
  122. omap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
  123. {
  124. struct drm_omap_param req = {
  125. .param = param,
  126. .value = value,
  127. };
  128. return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
  129. }
  130. /* lookup a buffer from it's handle, call w/ table_lock held: */
  131. static struct omap_bo * lookup_bo(struct omap_device *dev,
  132. uint32_t handle)
  133. {
  134. struct omap_bo *bo = NULL;
  135. if (!drmHashLookup(dev->handle_table, handle, (void **)&bo)) {
  136. /* found, incr refcnt and return: */
  137. bo = omap_bo_ref(bo);
  138. }
  139. return bo;
  140. }
  141. /* allocate a new buffer object, call w/ table_lock held */
  142. static struct omap_bo * bo_from_handle(struct omap_device *dev,
  143. uint32_t handle)
  144. {
  145. struct omap_bo *bo = calloc(sizeof(*bo), 1);
  146. if (!bo) {
  147. drmCloseBufferHandle(dev->fd, handle);
  148. return NULL;
  149. }
  150. bo->dev = omap_device_ref(dev);
  151. bo->handle = handle;
  152. bo->fd = -1;
  153. atomic_set(&bo->refcnt, 1);
  154. /* add ourselves to the handle table: */
  155. drmHashInsert(dev->handle_table, handle, bo);
  156. return bo;
  157. }
  158. /* allocate a new buffer object */
  159. static struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
  160. union omap_gem_size size, uint32_t flags)
  161. {
  162. struct omap_bo *bo = NULL;
  163. struct drm_omap_gem_new req = {
  164. .size = size,
  165. .flags = flags,
  166. };
  167. if (size.bytes == 0) {
  168. goto fail;
  169. }
  170. if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
  171. goto fail;
  172. }
  173. pthread_mutex_lock(&table_lock);
  174. bo = bo_from_handle(dev, req.handle);
  175. pthread_mutex_unlock(&table_lock);
  176. return bo;
  177. fail:
  178. free(bo);
  179. return NULL;
  180. }
  181. /* allocate a new (un-tiled) buffer object */
  182. drm_public struct omap_bo *
  183. omap_bo_new(struct omap_device *dev, uint32_t size, uint32_t flags)
  184. {
  185. union omap_gem_size gsize = {
  186. .bytes = size,
  187. };
  188. if (flags & OMAP_BO_TILED) {
  189. return NULL;
  190. }
  191. return omap_bo_new_impl(dev, gsize, flags);
  192. }
  193. /* allocate a new buffer object */
  194. drm_public struct omap_bo *
  195. omap_bo_new_tiled(struct omap_device *dev, uint32_t width,
  196. uint32_t height, uint32_t flags)
  197. {
  198. union omap_gem_size gsize = {
  199. .tiled = {
  200. .width = width,
  201. .height = height,
  202. },
  203. };
  204. if (!(flags & OMAP_BO_TILED)) {
  205. return NULL;
  206. }
  207. return omap_bo_new_impl(dev, gsize, flags);
  208. }
  209. drm_public struct omap_bo *omap_bo_ref(struct omap_bo *bo)
  210. {
  211. atomic_inc(&bo->refcnt);
  212. return bo;
  213. }
  214. /* get buffer info */
  215. static int get_buffer_info(struct omap_bo *bo)
  216. {
  217. struct drm_omap_gem_info req = {
  218. .handle = bo->handle,
  219. };
  220. int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
  221. &req, sizeof(req));
  222. if (ret) {
  223. return ret;
  224. }
  225. /* really all we need for now is mmap offset */
  226. bo->offset = req.offset;
  227. bo->size = req.size;
  228. return 0;
  229. }
  230. /* import a buffer object from DRI2 name */
  231. drm_public struct omap_bo *
  232. omap_bo_from_name(struct omap_device *dev, uint32_t name)
  233. {
  234. struct omap_bo *bo = NULL;
  235. struct drm_gem_open req = {
  236. .name = name,
  237. };
  238. pthread_mutex_lock(&table_lock);
  239. if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
  240. goto fail;
  241. }
  242. bo = lookup_bo(dev, req.handle);
  243. if (!bo) {
  244. bo = bo_from_handle(dev, req.handle);
  245. bo->name = name;
  246. }
  247. pthread_mutex_unlock(&table_lock);
  248. return bo;
  249. fail:
  250. pthread_mutex_unlock(&table_lock);
  251. free(bo);
  252. return NULL;
  253. }
  254. /* import a buffer from dmabuf fd, does not take ownership of the
  255. * fd so caller should close() the fd when it is otherwise done
  256. * with it (even if it is still using the 'struct omap_bo *')
  257. */
  258. drm_public struct omap_bo *
  259. omap_bo_from_dmabuf(struct omap_device *dev, int fd)
  260. {
  261. struct omap_bo *bo = NULL;
  262. struct drm_prime_handle req = {
  263. .fd = fd,
  264. };
  265. int ret;
  266. pthread_mutex_lock(&table_lock);
  267. ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
  268. if (ret) {
  269. goto fail;
  270. }
  271. bo = lookup_bo(dev, req.handle);
  272. if (!bo) {
  273. bo = bo_from_handle(dev, req.handle);
  274. }
  275. pthread_mutex_unlock(&table_lock);
  276. return bo;
  277. fail:
  278. pthread_mutex_unlock(&table_lock);
  279. free(bo);
  280. return NULL;
  281. }
  282. /* destroy a buffer object */
  283. drm_public void omap_bo_del(struct omap_bo *bo)
  284. {
  285. if (!bo) {
  286. return;
  287. }
  288. if (!atomic_dec_and_test(&bo->refcnt))
  289. return;
  290. if (bo->map) {
  291. munmap(bo->map, bo->size);
  292. }
  293. if (bo->fd >= 0) {
  294. close(bo->fd);
  295. }
  296. if (bo->handle) {
  297. pthread_mutex_lock(&table_lock);
  298. drmHashDelete(bo->dev->handle_table, bo->handle);
  299. drmCloseBufferHandle(bo->dev->fd, bo->handle);
  300. pthread_mutex_unlock(&table_lock);
  301. }
  302. omap_device_del(bo->dev);
  303. free(bo);
  304. }
  305. /* get the global flink/DRI2 buffer name */
  306. drm_public int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
  307. {
  308. if (!bo->name) {
  309. struct drm_gem_flink req = {
  310. .handle = bo->handle,
  311. };
  312. int ret;
  313. ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
  314. if (ret) {
  315. return ret;
  316. }
  317. bo->name = req.name;
  318. }
  319. *name = bo->name;
  320. return 0;
  321. }
  322. drm_public uint32_t omap_bo_handle(struct omap_bo *bo)
  323. {
  324. return bo->handle;
  325. }
  326. /* caller owns the dmabuf fd that is returned and is responsible
  327. * to close() it when done
  328. */
  329. drm_public int omap_bo_dmabuf(struct omap_bo *bo)
  330. {
  331. if (bo->fd < 0) {
  332. struct drm_prime_handle req = {
  333. .handle = bo->handle,
  334. .flags = DRM_CLOEXEC | DRM_RDWR,
  335. };
  336. int ret;
  337. ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
  338. if (ret) {
  339. return ret;
  340. }
  341. bo->fd = req.fd;
  342. }
  343. return dup(bo->fd);
  344. }
  345. drm_public uint32_t omap_bo_size(struct omap_bo *bo)
  346. {
  347. if (!bo->size) {
  348. get_buffer_info(bo);
  349. }
  350. return bo->size;
  351. }
  352. drm_public void *omap_bo_map(struct omap_bo *bo)
  353. {
  354. if (!bo->map) {
  355. if (!bo->size || !bo->offset) {
  356. get_buffer_info(bo);
  357. }
  358. bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
  359. MAP_SHARED, bo->dev->fd, bo->offset);
  360. if (bo->map == MAP_FAILED) {
  361. bo->map = NULL;
  362. }
  363. }
  364. return bo->map;
  365. }
  366. drm_public int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
  367. {
  368. struct drm_omap_gem_cpu_prep req = {
  369. .handle = bo->handle,
  370. .op = op,
  371. };
  372. return drmCommandWrite(bo->dev->fd,
  373. DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
  374. }
  375. drm_public int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
  376. {
  377. struct drm_omap_gem_cpu_fini req = {
  378. .handle = bo->handle,
  379. .op = op,
  380. .nregions = 0,
  381. };
  382. return drmCommandWrite(bo->dev->fd,
  383. DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
  384. }