minigbm: use struct vma for (*bo_map)/(*bo_unmap) callbacks

This sets better expectations for what we expect from the
backends.

BUG=chromium:764871
TEST=mmap_test

Change-Id: I7fb815b58fae8e9fbd73bf7c0263c7db44488844
Reviewed-on: https://chromium-review.googlesource.com/770519
Commit-Ready: Gurchetan Singh <gurchetansingh@chromium.org>
Tested-by: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Joe Kniss <djmk@google.com>
Reviewed-by: Stéphane Marchesin <marcheu@chromium.org>
diff --git a/amdgpu.c b/amdgpu.c
index 57d82e5..4a3eb7b 100644
--- a/amdgpu.c
+++ b/amdgpu.c
@@ -406,7 +406,7 @@
 	return ret;
 }
 
-static void *amdgpu_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags)
+static void *amdgpu_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	union drm_amdgpu_gem_mmap gem_map;
@@ -420,7 +420,7 @@
 		return MAP_FAILED;
 	}
 
-	mapping->vma->length = bo->total_size;
+	vma->length = bo->total_size;
 
 	return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 		    gem_map.out.addr_ptr);
diff --git a/drv.c b/drv.c
index 50d3401..cd8251f 100644
--- a/drv.c
+++ b/drv.c
@@ -413,7 +413,7 @@
 	}
 
 	mapping.vma = calloc(1, sizeof(*mapping.vma));
-	addr = bo->drv->backend->bo_map(bo, &mapping, plane, map_flags);
+	addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
 	if (addr == MAP_FAILED) {
 		*map_data = NULL;
 		free(mapping.vma);
@@ -448,7 +448,7 @@
 	pthread_mutex_lock(&bo->drv->driver_lock);
 
 	if (!--mapping->vma->refcount) {
-		ret = bo->drv->backend->bo_unmap(bo, mapping);
+		ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
 		free(mapping->vma);
 	}
 
diff --git a/drv_priv.h b/drv_priv.h
index dceeeb7..048b9a3 100644
--- a/drv_priv.h
+++ b/drv_priv.h
@@ -75,8 +75,8 @@
 					uint32_t format, const uint64_t *modifiers, uint32_t count);
 	int (*bo_destroy)(struct bo *bo);
 	int (*bo_import)(struct bo *bo, struct drv_import_fd_data *data);
-	void *(*bo_map)(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags);
-	int (*bo_unmap)(struct bo *bo, struct mapping *mapping);
+	void *(*bo_map)(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags);
+	int (*bo_unmap)(struct bo *bo, struct vma *vma);
 	int (*bo_invalidate)(struct bo *bo, struct mapping *mapping);
 	int (*bo_flush)(struct bo *bo, struct mapping *mapping);
 	uint32_t (*resolve_format)(uint32_t format, uint64_t use_flags);
diff --git a/helpers.c b/helpers.c
index d861b9c..9a33804 100644
--- a/helpers.c
+++ b/helpers.c
@@ -309,7 +309,7 @@
 	return 0;
 }
 
-void *drv_dumb_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags)
+void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	size_t i;
@@ -326,15 +326,15 @@
 
 	for (i = 0; i < bo->num_planes; i++)
 		if (bo->handles[i].u32 == bo->handles[plane].u32)
-			mapping->vma->length += bo->sizes[i];
+			vma->length += bo->sizes[i];
 
-	return mmap(0, mapping->vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
+	return mmap(0, vma->length, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 		    map_dumb.offset);
 }
 
-int drv_bo_munmap(struct bo *bo, struct mapping *mapping)
+int drv_bo_munmap(struct bo *bo, struct vma *vma)
 {
-	return munmap(mapping->vma->addr, mapping->vma->length);
+	return munmap(vma->addr, vma->length);
 }
 
 int drv_mapping_destroy(struct bo *bo)
@@ -359,7 +359,7 @@
 			}
 
 			if (!--mapping->vma->refcount) {
-				ret = bo->drv->backend->bo_unmap(bo, mapping);
+				ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
 				if (ret) {
 					fprintf(stderr, "drv: munmap failed");
 					return ret;
diff --git a/helpers.h b/helpers.h
index 5de7225..9c9dcc6 100644
--- a/helpers.h
+++ b/helpers.h
@@ -18,8 +18,8 @@
 int drv_dumb_bo_destroy(struct bo *bo);
 int drv_gem_bo_destroy(struct bo *bo);
 int drv_prime_bo_import(struct bo *bo, struct drv_import_fd_data *data);
-void *drv_dumb_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags);
-int drv_bo_munmap(struct bo *bo, struct mapping *mapping);
+void *drv_dumb_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags);
+int drv_bo_munmap(struct bo *bo, struct vma *vma);
 int drv_mapping_destroy(struct bo *bo);
 int drv_get_prot(uint32_t map_flags);
 uintptr_t drv_get_reference_count(struct driver *drv, struct bo *bo, size_t plane);
diff --git a/i915.c b/i915.c
index 965f62d..0b30703 100644
--- a/i915.c
+++ b/i915.c
@@ -404,7 +404,9 @@
 					 uint32_t format, const uint64_t *modifiers, uint32_t count)
 {
 	static const uint64_t modifier_order[] = {
-		I915_FORMAT_MOD_Y_TILED, I915_FORMAT_MOD_X_TILED, DRM_FORMAT_MOD_LINEAR,
+		I915_FORMAT_MOD_Y_TILED,
+		I915_FORMAT_MOD_X_TILED,
+		DRM_FORMAT_MOD_LINEAR,
 	};
 	uint64_t modifier;
 
@@ -445,7 +447,7 @@
 	return 0;
 }
 
-static void *i915_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags)
+static void *i915_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	void *addr;
@@ -489,7 +491,7 @@
 		return addr;
 	}
 
-	mapping->vma->length = bo->total_size;
+	vma->length = bo->total_size;
 	return addr;
 }
 
diff --git a/mediatek.c b/mediatek.c
index 33f57fd..ebc000f 100644
--- a/mediatek.c
+++ b/mediatek.c
@@ -78,8 +78,7 @@
 	return 0;
 }
 
-static void *mediatek_bo_map(struct bo *bo, struct mapping *mapping, size_t plane,
-			     uint32_t map_flags)
+static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	struct drm_mtk_gem_map_off gem_map;
@@ -97,31 +96,31 @@
 	void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 			  gem_map.offset);
 
-	mapping->vma->length = bo->total_size;
+	vma->length = bo->total_size;
 
 	if (bo->use_flags & BO_USE_RENDERSCRIPT) {
 		priv = calloc(1, sizeof(*priv));
 		priv->cached_addr = calloc(1, bo->total_size);
 		priv->gem_addr = addr;
 		memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
-		mapping->vma->priv = priv;
+		vma->priv = priv;
 		addr = priv->cached_addr;
 	}
 
 	return addr;
 }
 
-static int mediatek_bo_unmap(struct bo *bo, struct mapping *mapping)
+static int mediatek_bo_unmap(struct bo *bo, struct vma *vma)
 {
-	if (mapping->vma->priv) {
-		struct mediatek_private_map_data *priv = mapping->vma->priv;
-		mapping->vma->addr = priv->gem_addr;
+	if (vma->priv) {
+		struct mediatek_private_map_data *priv = vma->priv;
+		vma->addr = priv->gem_addr;
 		free(priv->cached_addr);
 		free(priv);
-		mapping->vma->priv = NULL;
+		vma->priv = NULL;
 	}
 
-	return munmap(mapping->vma->addr, mapping->vma->length);
+	return munmap(vma->addr, vma->length);
 }
 
 static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
diff --git a/rockchip.c b/rockchip.c
index 5076f1f..14c042b 100644
--- a/rockchip.c
+++ b/rockchip.c
@@ -239,8 +239,7 @@
 						 ARRAY_SIZE(modifiers));
 }
 
-static void *rockchip_bo_map(struct bo *bo, struct mapping *mapping, size_t plane,
-			     uint32_t map_flags)
+static void *rockchip_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	struct drm_rockchip_gem_map_off gem_map;
@@ -263,31 +262,31 @@
 	void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 			  gem_map.offset);
 
-	mapping->vma->length = bo->total_size;
+	vma->length = bo->total_size;
 
 	if (bo->use_flags & BO_USE_RENDERSCRIPT) {
 		priv = calloc(1, sizeof(*priv));
 		priv->cached_addr = calloc(1, bo->total_size);
 		priv->gem_addr = addr;
 		memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
-		mapping->vma->priv = priv;
+		vma->priv = priv;
 		addr = priv->cached_addr;
 	}
 
 	return addr;
 }
 
-static int rockchip_bo_unmap(struct bo *bo, struct mapping *mapping)
+static int rockchip_bo_unmap(struct bo *bo, struct vma *vma)
 {
-	if (mapping->vma->priv) {
-		struct rockchip_private_map_data *priv = mapping->vma->priv;
-		mapping->vma->addr = priv->gem_addr;
+	if (vma->priv) {
+		struct rockchip_private_map_data *priv = vma->priv;
+		vma->addr = priv->gem_addr;
 		free(priv->cached_addr);
 		free(priv);
-		mapping->vma->priv = NULL;
+		vma->priv = NULL;
 	}
 
-	return munmap(mapping->vma->addr, mapping->vma->length);
+	return munmap(vma->addr, vma->length);
 }
 
 static int rockchip_bo_flush(struct bo *bo, struct mapping *mapping)
diff --git a/tegra.c b/tegra.c
index e365593..4551fcf 100644
--- a/tegra.c
+++ b/tegra.c
@@ -300,7 +300,7 @@
 	return 0;
 }
 
-static void *tegra_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags)
+static void *tegra_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	struct drm_tegra_gem_mmap gem_map;
@@ -317,12 +317,12 @@
 
 	void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 			  gem_map.offset);
-	mapping->vma->length = bo->total_size;
+	vma->length = bo->total_size;
 	if ((bo->tiling & 0xFF) == NV_MEM_KIND_C32_2CRA && addr != MAP_FAILED) {
 		priv = calloc(1, sizeof(*priv));
 		priv->untiled = calloc(1, bo->total_size);
 		priv->tiled = addr;
-		mapping->vma->priv = priv;
+		vma->priv = priv;
 		transfer_tiled_memory(bo, priv->tiled, priv->untiled, TEGRA_READ_TILED_BUFFER);
 		addr = priv->untiled;
 	}
@@ -330,17 +330,17 @@
 	return addr;
 }
 
-static int tegra_bo_unmap(struct bo *bo, struct mapping *mapping)
+static int tegra_bo_unmap(struct bo *bo, struct vma *vma)
 {
-	if (mapping->vma->priv) {
-		struct tegra_private_map_data *priv = mapping->vma->priv;
-		mapping->vma->addr = priv->tiled;
+	if (vma->priv) {
+		struct tegra_private_map_data *priv = vma->priv;
+		vma->addr = priv->tiled;
 		free(priv->untiled);
 		free(priv);
-		mapping->vma->priv = NULL;
+		vma->priv = NULL;
 	}
 
-	return munmap(mapping->vma->addr, mapping->vma->length);
+	return munmap(vma->addr, vma->length);
 }
 
 static int tegra_bo_flush(struct bo *bo, struct mapping *mapping)
diff --git a/vc4.c b/vc4.c
index cfcc219..82b7047 100644
--- a/vc4.c
+++ b/vc4.c
@@ -62,7 +62,7 @@
 	return 0;
 }
 
-static void *vc4_bo_map(struct bo *bo, struct mapping *mapping, size_t plane, uint32_t map_flags)
+static void *vc4_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
 	int ret;
 	struct drm_vc4_mmap_bo bo_map;
@@ -76,7 +76,7 @@
 		return MAP_FAILED;
 	}
 
-	mapping->vma->length = bo->total_size;
+	vma->length = bo->total_size;
 	return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 		    bo_map.offset);
 }