dmabuf-heap.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <dirent.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/mman.h>
  12. #include <sys/types.h>
  13. #include <linux/dma-buf.h>
  14. #include <linux/dma-heap.h>
  15. #include <drm/drm.h>
  16. #define DEVPATH "/dev/dma_heap"
  17. static int check_vgem(int fd)
  18. {
  19. drm_version_t version = { 0 };
  20. char name[5];
  21. int ret;
  22. version.name_len = 4;
  23. version.name = name;
  24. ret = ioctl(fd, DRM_IOCTL_VERSION, &version);
  25. if (ret)
  26. return 0;
  27. return !strcmp(name, "vgem");
  28. }
  29. static int open_vgem(void)
  30. {
  31. int i, fd;
  32. const char *drmstr = "/dev/dri/card";
  33. fd = -1;
  34. for (i = 0; i < 16; i++) {
  35. char name[80];
  36. snprintf(name, 80, "%s%u", drmstr, i);
  37. fd = open(name, O_RDWR);
  38. if (fd < 0)
  39. continue;
  40. if (!check_vgem(fd)) {
  41. close(fd);
  42. fd = -1;
  43. continue;
  44. } else {
  45. break;
  46. }
  47. }
  48. return fd;
  49. }
  50. static int import_vgem_fd(int vgem_fd, int dma_buf_fd, uint32_t *handle)
  51. {
  52. struct drm_prime_handle import_handle = {
  53. .fd = dma_buf_fd,
  54. .flags = 0,
  55. .handle = 0,
  56. };
  57. int ret;
  58. ret = ioctl(vgem_fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &import_handle);
  59. if (ret == 0)
  60. *handle = import_handle.handle;
  61. return ret;
  62. }
  63. static void close_handle(int vgem_fd, uint32_t handle)
  64. {
  65. struct drm_gem_close close = {
  66. .handle = handle,
  67. };
  68. ioctl(vgem_fd, DRM_IOCTL_GEM_CLOSE, &close);
  69. }
  70. static int dmabuf_heap_open(char *name)
  71. {
  72. int ret, fd;
  73. char buf[256];
  74. ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
  75. if (ret < 0) {
  76. printf("snprintf failed!\n");
  77. return ret;
  78. }
  79. fd = open(buf, O_RDWR);
  80. if (fd < 0)
  81. printf("open %s failed!\n", buf);
  82. return fd;
  83. }
  84. static int dmabuf_heap_alloc_fdflags(int fd, size_t len, unsigned int fd_flags,
  85. unsigned int heap_flags, int *dmabuf_fd)
  86. {
  87. struct dma_heap_allocation_data data = {
  88. .len = len,
  89. .fd = 0,
  90. .fd_flags = fd_flags,
  91. .heap_flags = heap_flags,
  92. };
  93. int ret;
  94. if (!dmabuf_fd)
  95. return -EINVAL;
  96. ret = ioctl(fd, DMA_HEAP_IOCTL_ALLOC, &data);
  97. if (ret < 0)
  98. return ret;
  99. *dmabuf_fd = (int)data.fd;
  100. return ret;
  101. }
  102. static int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags,
  103. int *dmabuf_fd)
  104. {
  105. return dmabuf_heap_alloc_fdflags(fd, len, O_RDWR | O_CLOEXEC, flags,
  106. dmabuf_fd);
  107. }
  108. static int dmabuf_sync(int fd, int start_stop)
  109. {
  110. struct dma_buf_sync sync = {
  111. .flags = start_stop | DMA_BUF_SYNC_RW,
  112. };
  113. return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
  114. }
  115. #define ONE_MEG (1024 * 1024)
  116. static int test_alloc_and_import(char *heap_name)
  117. {
  118. int heap_fd = -1, dmabuf_fd = -1, importer_fd = -1;
  119. uint32_t handle = 0;
  120. void *p = NULL;
  121. int ret;
  122. heap_fd = dmabuf_heap_open(heap_name);
  123. if (heap_fd < 0)
  124. return -1;
  125. printf(" Testing allocation and importing: ");
  126. ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0, &dmabuf_fd);
  127. if (ret) {
  128. printf("FAIL (Allocation Failed!)\n");
  129. ret = -1;
  130. goto out;
  131. }
  132. /* mmap and write a simple pattern */
  133. p = mmap(NULL,
  134. ONE_MEG,
  135. PROT_READ | PROT_WRITE,
  136. MAP_SHARED,
  137. dmabuf_fd,
  138. 0);
  139. if (p == MAP_FAILED) {
  140. printf("FAIL (mmap() failed)\n");
  141. ret = -1;
  142. goto out;
  143. }
  144. dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
  145. memset(p, 1, ONE_MEG / 2);
  146. memset((char *)p + ONE_MEG / 2, 0, ONE_MEG / 2);
  147. dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
  148. importer_fd = open_vgem();
  149. if (importer_fd < 0) {
  150. ret = importer_fd;
  151. printf("(Could not open vgem - skipping): ");
  152. } else {
  153. ret = import_vgem_fd(importer_fd, dmabuf_fd, &handle);
  154. if (ret < 0) {
  155. printf("FAIL (Failed to import buffer)\n");
  156. goto out;
  157. }
  158. }
  159. ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_START);
  160. if (ret < 0) {
  161. printf("FAIL (DMA_BUF_SYNC_START failed!)\n");
  162. goto out;
  163. }
  164. memset(p, 0xff, ONE_MEG);
  165. ret = dmabuf_sync(dmabuf_fd, DMA_BUF_SYNC_END);
  166. if (ret < 0) {
  167. printf("FAIL (DMA_BUF_SYNC_END failed!)\n");
  168. goto out;
  169. }
  170. close_handle(importer_fd, handle);
  171. ret = 0;
  172. printf(" OK\n");
  173. out:
  174. if (p)
  175. munmap(p, ONE_MEG);
  176. if (importer_fd >= 0)
  177. close(importer_fd);
  178. if (dmabuf_fd >= 0)
  179. close(dmabuf_fd);
  180. if (heap_fd >= 0)
  181. close(heap_fd);
  182. return ret;
  183. }
  184. static int test_alloc_zeroed(char *heap_name, size_t size)
  185. {
  186. int heap_fd = -1, dmabuf_fd[32];
  187. int i, j, ret;
  188. void *p = NULL;
  189. char *c;
  190. printf(" Testing alloced %ldk buffers are zeroed: ", size / 1024);
  191. heap_fd = dmabuf_heap_open(heap_name);
  192. if (heap_fd < 0)
  193. return -1;
  194. /* Allocate and fill a bunch of buffers */
  195. for (i = 0; i < 32; i++) {
  196. ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
  197. if (ret < 0) {
  198. printf("FAIL (Allocation (%i) failed)\n", i);
  199. goto out;
  200. }
  201. /* mmap and fill with simple pattern */
  202. p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
  203. if (p == MAP_FAILED) {
  204. printf("FAIL (mmap() failed!)\n");
  205. ret = -1;
  206. goto out;
  207. }
  208. dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
  209. memset(p, 0xff, size);
  210. dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
  211. munmap(p, size);
  212. }
  213. /* close them all */
  214. for (i = 0; i < 32; i++)
  215. close(dmabuf_fd[i]);
  216. /* Allocate and validate all buffers are zeroed */
  217. for (i = 0; i < 32; i++) {
  218. ret = dmabuf_heap_alloc(heap_fd, size, 0, &dmabuf_fd[i]);
  219. if (ret < 0) {
  220. printf("FAIL (Allocation (%i) failed)\n", i);
  221. goto out;
  222. }
  223. /* mmap and validate everything is zero */
  224. p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd[i], 0);
  225. if (p == MAP_FAILED) {
  226. printf("FAIL (mmap() failed!)\n");
  227. ret = -1;
  228. goto out;
  229. }
  230. dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_START);
  231. c = (char *)p;
  232. for (j = 0; j < size; j++) {
  233. if (c[j] != 0) {
  234. printf("FAIL (Allocated buffer not zeroed @ %i)\n", j);
  235. break;
  236. }
  237. }
  238. dmabuf_sync(dmabuf_fd[i], DMA_BUF_SYNC_END);
  239. munmap(p, size);
  240. }
  241. /* close them all */
  242. for (i = 0; i < 32; i++)
  243. close(dmabuf_fd[i]);
  244. close(heap_fd);
  245. printf("OK\n");
  246. return 0;
  247. out:
  248. while (i > 0) {
  249. close(dmabuf_fd[i]);
  250. i--;
  251. }
  252. close(heap_fd);
  253. return ret;
  254. }
  255. /* Test the ioctl version compatibility w/ a smaller structure then expected */
  256. static int dmabuf_heap_alloc_older(int fd, size_t len, unsigned int flags,
  257. int *dmabuf_fd)
  258. {
  259. int ret;
  260. unsigned int older_alloc_ioctl;
  261. struct dma_heap_allocation_data_smaller {
  262. __u64 len;
  263. __u32 fd;
  264. __u32 fd_flags;
  265. } data = {
  266. .len = len,
  267. .fd = 0,
  268. .fd_flags = O_RDWR | O_CLOEXEC,
  269. };
  270. older_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
  271. struct dma_heap_allocation_data_smaller);
  272. if (!dmabuf_fd)
  273. return -EINVAL;
  274. ret = ioctl(fd, older_alloc_ioctl, &data);
  275. if (ret < 0)
  276. return ret;
  277. *dmabuf_fd = (int)data.fd;
  278. return ret;
  279. }
  280. /* Test the ioctl version compatibility w/ a larger structure then expected */
  281. static int dmabuf_heap_alloc_newer(int fd, size_t len, unsigned int flags,
  282. int *dmabuf_fd)
  283. {
  284. int ret;
  285. unsigned int newer_alloc_ioctl;
  286. struct dma_heap_allocation_data_bigger {
  287. __u64 len;
  288. __u32 fd;
  289. __u32 fd_flags;
  290. __u64 heap_flags;
  291. __u64 garbage1;
  292. __u64 garbage2;
  293. __u64 garbage3;
  294. } data = {
  295. .len = len,
  296. .fd = 0,
  297. .fd_flags = O_RDWR | O_CLOEXEC,
  298. .heap_flags = flags,
  299. .garbage1 = 0xffffffff,
  300. .garbage2 = 0x88888888,
  301. .garbage3 = 0x11111111,
  302. };
  303. newer_alloc_ioctl = _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,
  304. struct dma_heap_allocation_data_bigger);
  305. if (!dmabuf_fd)
  306. return -EINVAL;
  307. ret = ioctl(fd, newer_alloc_ioctl, &data);
  308. if (ret < 0)
  309. return ret;
  310. *dmabuf_fd = (int)data.fd;
  311. return ret;
  312. }
  313. static int test_alloc_compat(char *heap_name)
  314. {
  315. int heap_fd = -1, dmabuf_fd = -1;
  316. int ret;
  317. heap_fd = dmabuf_heap_open(heap_name);
  318. if (heap_fd < 0)
  319. return -1;
  320. printf(" Testing (theoretical)older alloc compat: ");
  321. ret = dmabuf_heap_alloc_older(heap_fd, ONE_MEG, 0, &dmabuf_fd);
  322. if (ret) {
  323. printf("FAIL (Older compat allocation failed!)\n");
  324. ret = -1;
  325. goto out;
  326. }
  327. close(dmabuf_fd);
  328. printf("OK\n");
  329. printf(" Testing (theoretical)newer alloc compat: ");
  330. ret = dmabuf_heap_alloc_newer(heap_fd, ONE_MEG, 0, &dmabuf_fd);
  331. if (ret) {
  332. printf("FAIL (Newer compat allocation failed!)\n");
  333. ret = -1;
  334. goto out;
  335. }
  336. printf("OK\n");
  337. out:
  338. if (dmabuf_fd >= 0)
  339. close(dmabuf_fd);
  340. if (heap_fd >= 0)
  341. close(heap_fd);
  342. return ret;
  343. }
  344. static int test_alloc_errors(char *heap_name)
  345. {
  346. int heap_fd = -1, dmabuf_fd = -1;
  347. int ret;
  348. heap_fd = dmabuf_heap_open(heap_name);
  349. if (heap_fd < 0)
  350. return -1;
  351. printf(" Testing expected error cases: ");
  352. ret = dmabuf_heap_alloc(0, ONE_MEG, 0x111111, &dmabuf_fd);
  353. if (!ret) {
  354. printf("FAIL (Did not see expected error (invalid fd)!)\n");
  355. ret = -1;
  356. goto out;
  357. }
  358. ret = dmabuf_heap_alloc(heap_fd, ONE_MEG, 0x111111, &dmabuf_fd);
  359. if (!ret) {
  360. printf("FAIL (Did not see expected error (invalid heap flags)!)\n");
  361. ret = -1;
  362. goto out;
  363. }
  364. ret = dmabuf_heap_alloc_fdflags(heap_fd, ONE_MEG,
  365. ~(O_RDWR | O_CLOEXEC), 0, &dmabuf_fd);
  366. if (!ret) {
  367. printf("FAIL (Did not see expected error (invalid fd flags)!)\n");
  368. ret = -1;
  369. goto out;
  370. }
  371. printf("OK\n");
  372. ret = 0;
  373. out:
  374. if (dmabuf_fd >= 0)
  375. close(dmabuf_fd);
  376. if (heap_fd >= 0)
  377. close(heap_fd);
  378. return ret;
  379. }
  380. int main(void)
  381. {
  382. DIR *d;
  383. struct dirent *dir;
  384. int ret = -1;
  385. d = opendir(DEVPATH);
  386. if (!d) {
  387. printf("No %s directory?\n", DEVPATH);
  388. return -1;
  389. }
  390. while ((dir = readdir(d)) != NULL) {
  391. if (!strncmp(dir->d_name, ".", 2))
  392. continue;
  393. if (!strncmp(dir->d_name, "..", 3))
  394. continue;
  395. printf("Testing heap: %s\n", dir->d_name);
  396. printf("=======================================\n");
  397. ret = test_alloc_and_import(dir->d_name);
  398. if (ret)
  399. break;
  400. ret = test_alloc_zeroed(dir->d_name, 4 * 1024);
  401. if (ret)
  402. break;
  403. ret = test_alloc_zeroed(dir->d_name, ONE_MEG);
  404. if (ret)
  405. break;
  406. ret = test_alloc_compat(dir->d_name);
  407. if (ret)
  408. break;
  409. ret = test_alloc_errors(dir->d_name);
  410. if (ret)
  411. break;
  412. }
  413. closedir(d);
  414. return ret;
  415. }