dmabuf_test: Use dumb ioctls for creating and mapping BOs

Like the vgem based test, use the dumb ioctls for creating and mapping
BOs.  Using minigbm means that we end up using DRM_IOCTL_I915_GEM_MMAP
on imported dma-bufs, which i915.ko doesn't support.

BUG=b:167236452
TEST=dmabuf_test passes

Change-Id: I5daa48069380e241c8671dbf021d83a10edeb8eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/drm-tests/+/2550658
Reviewed-by: Rob Clark <robdclark@chromium.org>
Reviewed-by: Ilja H. Friedel <ihf@chromium.org>
Tested-by: Ilja H. Friedel <ihf@chromium.org>
Commit-Queue: Kristian H. Kristensen <hoegsberg@chromium.org>
diff --git a/dmabuf_test.c b/dmabuf_test.c
index c4c4d31..6dd8073 100644
--- a/dmabuf_test.c
+++ b/dmabuf_test.c
@@ -121,11 +121,13 @@
 	return fd;
 }
 
-void test_import_dma_buf(struct gbm_device *gbm)
+void test_import_dma_buf(int native_fd)
 {
 	int width = 256;
 	int height = 256;
-	size_t size = width * height * 256;
+	int bpp = 4;
+	int stride = width * bpp;
+	size_t size = stride * height;
 
 	int memfd_fd = create_memfd(size);
 	fail_if(memfd_fd == -1, "failed to create memfd");
@@ -144,70 +146,54 @@
 
 	fail_if(close(memfd_fd) != 0, "close memfd failed");
 
-	struct gbm_import_fd_modifier_data gbm_import_data = {
-		.width = width,
-		.height = height,
-		.format = GBM_FORMAT_ARGB8888,
-		.num_fds = 1,
-		.fds[0] = udmabuf_fd,
-		.strides[0] = width * 4,
-		.offsets[0] = 0,
-		.modifier = DRM_FORMAT_MOD_LINEAR,
-	};
+	uint32_t foreign_imported_handle;
+	fail_if(drmPrimeFDToHandle(native_fd, udmabuf_fd, &foreign_imported_handle) != 0,
+		"drmPrimeFDToHandle failed");
 
-	struct gbm_bo *bo =
-	    gbm_bo_import(gbm, GBM_BO_IMPORT_FD_MODIFIER,
-			  &gbm_import_data, GBM_BO_USE_RENDERING);
-	fail_if(bo == NULL, "failed to import bo");
+	uint32_t *bo_ptr =
+		mmap_dumb_bo(native_fd, foreign_imported_handle, size);
+	fail_if(bo_ptr == MAP_FAILED,
+		"failed to map imported buffer object\n");
 
 	fail_if(close(udmabuf_fd) != 0, "failed to close udmabuf_fd");
 
-	uint32_t stride;
-	void *map_data;
-	void *bo_map = gbm_bo_map(bo,0, 0, width, height,
-				  GBM_BO_TRANSFER_READ, &stride, &map_data);
-
-	fail_if(bo_map == MAP_FAILED, "gbm_bo_map failed");
-
-	fail_if(!verify_pattern(bo_map, size), "pattern mismatch after gbm_bo_map");
-
-	gbm_bo_unmap(bo, map_data);
-
-	gbm_bo_destroy(bo);
+	fail_if(!verify_pattern(bo_ptr, size), "pattern mismatch after map");
 }
 
-void test_export_dma_buf(struct gbm_device *gbm)
+void test_export_dma_buf(int native_fd)
 {
-	int width = 64;
-	int height = 64;
+	struct drm_mode_create_dumb create;
+	uint32_t *bo_ptr;
 
-	struct gbm_bo *bo = gbm_bo_create(gbm, width, height,
-		GBM_FORMAT_ARGB8888, GBM_BO_USE_LINEAR);
-	fail_if(bo == NULL, "create bo failed");
+	memset(&create, 0, sizeof(create));
+	create.width = 640;
+	create.height = 480;
+	create.bpp = 32;
 
-	uint32_t stride;
-	void *map_data;
-	void *bo_map = gbm_bo_map(bo,0, 0, width, height,
-				  GBM_BO_TRANSFER_WRITE, &stride, &map_data);
-	fail_if(bo_map == MAP_FAILED, "gbm_bo_map failed");
+	fail_if(drmIoctl(native_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create) != 0,
+		"failed to create dumb buffer object");
 
-	fail_if(stride != gbm_bo_get_stride(bo),
-		"mapped buffer stride doesn't match bo stride");
-	uint32_t size = stride * height;
-	write_pattern(bo_map, size);
+	bo_ptr = mmap_dumb_bo(native_fd, create.handle, create.size);
+	fail_if(bo_ptr == MAP_FAILED, "failed to map dumb bo");
 
-	gbm_bo_unmap(bo, map_data);
+	write_pattern(bo_ptr, create.size);
 
-	int dmabuf_fd = gbm_bo_get_fd(bo);
-	fail_if(dmabuf_fd == -1, "dma-buf export failed");
+	munmap(bo_ptr, create.size);
+
+	int dmabuf_fd;
+	fail_if(drmPrimeHandleToFD(native_fd, create.handle, DRM_CLOEXEC | DRM_RDWR, &dmabuf_fd) != 0,
+		"drmPrimeHandleToFD failed");
 
 	uint32_t *dmabuf_map =
-	    mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, dmabuf_fd, 0);
+	    mmap(NULL, create.size, PROT_WRITE | PROT_READ, MAP_SHARED, dmabuf_fd, 0);
 	fail_if(dmabuf_map == MAP_FAILED, "failed to mmap memfd");
 
-	fail_if(!verify_pattern(dmabuf_map, size), "pattern mismatch after gbm_bo_map");
+	fail_if(!verify_pattern(dmabuf_map, create.size),
+		"pattern mismatch after gbm_bo_map");
 
-	fail_if(munmap(dmabuf_map, size) != 0, "munmap failed");
+	fail_if(munmap(dmabuf_map, create.size) != 0, "munmap failed");
+
+	fail_if(close(dmabuf_fd) != 0, "failed to close dmabuf_fd");
 }
 
 int main(int argc, char *argv[])
@@ -215,14 +201,10 @@
 	int fd = bs_drm_open_for_display();
 	fail_if(fd == -1, "error opening dri card");
 
-	struct gbm_device* gbm = gbm_create_device(fd);
-	fail_if(gbm == NULL, "failed to create gbm device");
+	test_import_dma_buf(fd);
 
-	test_import_dma_buf(gbm);
+	test_export_dma_buf(fd);
 
-	test_export_dma_buf(gbm);
-
-	gbm_device_destroy(gbm);
 	close(fd);
 
 	return EXIT_SUCCESS;