1
0

exynos_drm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics Co., Ltd.
  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. * Inki Dae <inki.dae@samsung.com>
  25. */
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <sys/mman.h>
  32. #include <xf86drm.h>
  33. #include "libdrm_macros.h"
  34. #include "exynos_drm.h"
  35. #include "exynos_drmif.h"
  36. #define U642VOID(x) ((void *)(unsigned long)(x))
  37. /*
  38. * Create exynos drm device object.
  39. *
  40. * @fd: file descriptor to exynos drm driver opened.
  41. *
  42. * if true, return the device object else NULL.
  43. */
  44. drm_public struct exynos_device * exynos_device_create(int fd)
  45. {
  46. struct exynos_device *dev;
  47. dev = calloc(sizeof(*dev), 1);
  48. if (!dev) {
  49. fprintf(stderr, "failed to create device[%s].\n",
  50. strerror(errno));
  51. return NULL;
  52. }
  53. dev->fd = fd;
  54. return dev;
  55. }
  56. /*
  57. * Destroy exynos drm device object
  58. *
  59. * @dev: exynos drm device object.
  60. */
  61. drm_public void exynos_device_destroy(struct exynos_device *dev)
  62. {
  63. free(dev);
  64. }
  65. /*
  66. * Create a exynos buffer object to exynos drm device.
  67. *
  68. * @dev: exynos drm device object.
  69. * @size: user-desired size.
  70. * flags: user-desired memory type.
  71. * user can set one or more types among several types to memory
  72. * allocation and cache attribute types. and as default,
  73. * EXYNOS_BO_NONCONTIG and EXYNOS-BO_NONCACHABLE types would
  74. * be used.
  75. *
  76. * if true, return a exynos buffer object else NULL.
  77. */
  78. drm_public struct exynos_bo * exynos_bo_create(struct exynos_device *dev,
  79. size_t size, uint32_t flags)
  80. {
  81. struct exynos_bo *bo;
  82. struct drm_exynos_gem_create req = {
  83. .size = size,
  84. .flags = flags,
  85. };
  86. if (size == 0) {
  87. fprintf(stderr, "invalid size.\n");
  88. goto fail;
  89. }
  90. bo = calloc(sizeof(*bo), 1);
  91. if (!bo) {
  92. fprintf(stderr, "failed to create bo[%s].\n",
  93. strerror(errno));
  94. goto err_free_bo;
  95. }
  96. bo->dev = dev;
  97. if (drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_CREATE, &req)){
  98. fprintf(stderr, "failed to create gem object[%s].\n",
  99. strerror(errno));
  100. goto err_free_bo;
  101. }
  102. bo->handle = req.handle;
  103. bo->size = size;
  104. bo->flags = flags;
  105. return bo;
  106. err_free_bo:
  107. free(bo);
  108. fail:
  109. return NULL;
  110. }
  111. /*
  112. * Get information to gem region allocated.
  113. *
  114. * @dev: exynos drm device object.
  115. * @handle: gem handle to request gem info.
  116. * @size: size to gem object and returned by kernel side.
  117. * @flags: gem flags to gem object and returned by kernel side.
  118. *
  119. * with this function call, you can get flags and size to gem handle
  120. * through bo object.
  121. *
  122. * if true, return 0 else negative.
  123. */
  124. drm_public int exynos_bo_get_info(struct exynos_device *dev, uint32_t handle,
  125. size_t *size, uint32_t *flags)
  126. {
  127. int ret;
  128. struct drm_exynos_gem_info req = {
  129. .handle = handle,
  130. };
  131. ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_GEM_GET, &req);
  132. if (ret < 0) {
  133. fprintf(stderr, "failed to get gem object information[%s].\n",
  134. strerror(errno));
  135. return ret;
  136. }
  137. *size = req.size;
  138. *flags = req.flags;
  139. return 0;
  140. }
  141. /*
  142. * Destroy a exynos buffer object.
  143. *
  144. * @bo: a exynos buffer object to be destroyed.
  145. */
  146. drm_public void exynos_bo_destroy(struct exynos_bo *bo)
  147. {
  148. if (!bo)
  149. return;
  150. if (bo->vaddr)
  151. munmap(bo->vaddr, bo->size);
  152. if (bo->handle) {
  153. drmCloseBufferHandle(bo->dev->fd, bo->handle);
  154. }
  155. free(bo);
  156. }
  157. /*
  158. * Get a exynos buffer object from a gem global object name.
  159. *
  160. * @dev: a exynos device object.
  161. * @name: a gem global object name exported by another process.
  162. *
  163. * this interface is used to get a exynos buffer object from a gem
  164. * global object name sent by another process for buffer sharing.
  165. *
  166. * if true, return a exynos buffer object else NULL.
  167. *
  168. */
  169. drm_public struct exynos_bo *
  170. exynos_bo_from_name(struct exynos_device *dev, uint32_t name)
  171. {
  172. struct exynos_bo *bo;
  173. struct drm_gem_open req = {
  174. .name = name,
  175. };
  176. bo = calloc(sizeof(*bo), 1);
  177. if (!bo) {
  178. fprintf(stderr, "failed to allocate bo[%s].\n",
  179. strerror(errno));
  180. return NULL;
  181. }
  182. if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
  183. fprintf(stderr, "failed to open gem object[%s].\n",
  184. strerror(errno));
  185. goto err_free_bo;
  186. }
  187. bo->dev = dev;
  188. bo->name = name;
  189. bo->handle = req.handle;
  190. return bo;
  191. err_free_bo:
  192. free(bo);
  193. return NULL;
  194. }
  195. /*
  196. * Get a gem global object name from a gem object handle.
  197. *
  198. * @bo: a exynos buffer object including gem handle.
  199. * @name: a gem global object name to be got by kernel driver.
  200. *
  201. * this interface is used to get a gem global object name from a gem object
  202. * handle to a buffer that wants to share it with another process.
  203. *
  204. * if true, return 0 else negative.
  205. */
  206. drm_public int exynos_bo_get_name(struct exynos_bo *bo, uint32_t *name)
  207. {
  208. if (!bo->name) {
  209. struct drm_gem_flink req = {
  210. .handle = bo->handle,
  211. };
  212. int ret;
  213. ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
  214. if (ret) {
  215. fprintf(stderr, "failed to get gem global name[%s].\n",
  216. strerror(errno));
  217. return ret;
  218. }
  219. bo->name = req.name;
  220. }
  221. *name = bo->name;
  222. return 0;
  223. }
  224. drm_public uint32_t exynos_bo_handle(struct exynos_bo *bo)
  225. {
  226. return bo->handle;
  227. }
  228. /*
  229. * Mmap a buffer to user space.
  230. *
  231. * @bo: a exynos buffer object including a gem object handle to be mmapped
  232. * to user space.
  233. *
  234. * if true, user pointer mmapped else NULL.
  235. */
  236. drm_public void *exynos_bo_map(struct exynos_bo *bo)
  237. {
  238. if (!bo->vaddr) {
  239. struct exynos_device *dev = bo->dev;
  240. struct drm_mode_map_dumb arg;
  241. void *map = NULL;
  242. int ret;
  243. memset(&arg, 0, sizeof(arg));
  244. arg.handle = bo->handle;
  245. ret = drmIoctl(dev->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
  246. if (ret) {
  247. fprintf(stderr, "failed to map dumb buffer[%s].\n",
  248. strerror(errno));
  249. return NULL;
  250. }
  251. map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
  252. dev->fd, arg.offset);
  253. if (map != MAP_FAILED)
  254. bo->vaddr = map;
  255. }
  256. return bo->vaddr;
  257. }
  258. /*
  259. * Export gem object to dmabuf as file descriptor.
  260. *
  261. * @dev: exynos device object
  262. * @handle: gem handle to export as file descriptor of dmabuf
  263. * @fd: file descriptor returned from kernel
  264. *
  265. * @return: 0 on success, -1 on error, and errno will be set
  266. */
  267. drm_public int
  268. exynos_prime_handle_to_fd(struct exynos_device *dev, uint32_t handle, int *fd)
  269. {
  270. return drmPrimeHandleToFD(dev->fd, handle, 0, fd);
  271. }
  272. /*
  273. * Import file descriptor into gem handle.
  274. *
  275. * @dev: exynos device object
  276. * @fd: file descriptor of dmabuf to import
  277. * @handle: gem handle returned from kernel
  278. *
  279. * @return: 0 on success, -1 on error, and errno will be set
  280. */
  281. drm_public int
  282. exynos_prime_fd_to_handle(struct exynos_device *dev, int fd, uint32_t *handle)
  283. {
  284. return drmPrimeFDToHandle(dev->fd, fd, handle);
  285. }
  286. /*
  287. * Request Wireless Display connection or disconnection.
  288. *
  289. * @dev: a exynos device object.
  290. * @connect: indicate whether connectoin or disconnection request.
  291. * @ext: indicate whether edid data includes extensions data or not.
  292. * @edid: a pointer to edid data from Wireless Display device.
  293. *
  294. * this interface is used to request Virtual Display driver connection or
  295. * disconnection. for this, user should get a edid data from the Wireless
  296. * Display device and then send that data to kernel driver with connection
  297. * request
  298. *
  299. * if true, return 0 else negative.
  300. */
  301. drm_public int
  302. exynos_vidi_connection(struct exynos_device *dev, uint32_t connect,
  303. uint32_t ext, void *edid)
  304. {
  305. struct drm_exynos_vidi_connection req = {
  306. .connection = connect,
  307. .extensions = ext,
  308. .edid = (uint64_t)(uintptr_t)edid,
  309. };
  310. int ret;
  311. ret = drmIoctl(dev->fd, DRM_IOCTL_EXYNOS_VIDI_CONNECTION, &req);
  312. if (ret) {
  313. fprintf(stderr, "failed to request vidi connection[%s].\n",
  314. strerror(errno));
  315. return ret;
  316. }
  317. return 0;
  318. }
  319. static void
  320. exynos_handle_vendor(int fd, struct drm_event *e, void *ctx)
  321. {
  322. struct drm_exynos_g2d_event *g2d;
  323. struct exynos_event_context *ectx = ctx;
  324. switch (e->type) {
  325. case DRM_EXYNOS_G2D_EVENT:
  326. if (ectx->version < 1 || ectx->g2d_event_handler == NULL)
  327. break;
  328. g2d = (struct drm_exynos_g2d_event *)e;
  329. ectx->g2d_event_handler(fd, g2d->cmdlist_no, g2d->tv_sec,
  330. g2d->tv_usec, U642VOID(g2d->user_data));
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. drm_public int
  337. exynos_handle_event(struct exynos_device *dev, struct exynos_event_context *ctx)
  338. {
  339. char buffer[1024];
  340. int len, i;
  341. struct drm_event *e;
  342. struct drm_event_vblank *vblank;
  343. drmEventContextPtr evctx = &ctx->base;
  344. /* The DRM read semantics guarantees that we always get only
  345. * complete events. */
  346. len = read(dev->fd, buffer, sizeof buffer);
  347. if (len == 0)
  348. return 0;
  349. if (len < (int)sizeof *e)
  350. return -1;
  351. i = 0;
  352. while (i < len) {
  353. e = (struct drm_event *)(buffer + i);
  354. switch (e->type) {
  355. case DRM_EVENT_VBLANK:
  356. if (evctx->version < 1 ||
  357. evctx->vblank_handler == NULL)
  358. break;
  359. vblank = (struct drm_event_vblank *) e;
  360. evctx->vblank_handler(dev->fd,
  361. vblank->sequence,
  362. vblank->tv_sec,
  363. vblank->tv_usec,
  364. U642VOID (vblank->user_data));
  365. break;
  366. case DRM_EVENT_FLIP_COMPLETE:
  367. if (evctx->version < 2 ||
  368. evctx->page_flip_handler == NULL)
  369. break;
  370. vblank = (struct drm_event_vblank *) e;
  371. evctx->page_flip_handler(dev->fd,
  372. vblank->sequence,
  373. vblank->tv_sec,
  374. vblank->tv_usec,
  375. U642VOID (vblank->user_data));
  376. break;
  377. default:
  378. exynos_handle_vendor(dev->fd, e, evctx);
  379. break;
  380. }
  381. i += e->length;
  382. }
  383. return 0;
  384. }