minigbm: mediatek: wait for outstanding operations when invalidating

Without doing this it's possible for the CPU to access the memory when it's
in use by another device.

TEST=adb shell "am instrument -w --abi armeabi-v7a -e class\
 android.media.cts.EncodeDecodeTest#testEncodeDecodeVideoFromPersistentSurfaceToSurfaceQCIF\
 android.media.cts/android.support.test.runner.AndroidJUnitRunner"
BUG=b:71835379

Change-Id: I3b1508a0eab3b020b7c42978cb1e1099ebc029fd
Signed-off-by: Luigi Santivetti <luigi.santivetti@imagination.corp-partner.google.com>
Reviewed-on: https://chromium-review.googlesource.com/1193302
Commit-Ready: Gurchetan Singh <gurchetansingh@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
diff --git a/mediatek.c b/mediatek.c
index 21a10af..3b8ecbf 100644
--- a/mediatek.c
+++ b/mediatek.c
@@ -7,9 +7,12 @@
 #ifdef DRV_MEDIATEK
 
 // clang-format off
+#include <fcntl.h>
+#include <poll.h>
 #include <stdio.h>
 #include <string.h>
 #include <sys/mman.h>
+#include <unistd.h>
 #include <xf86drm.h>
 #include <mediatek_drm.h>
 // clang-format on
@@ -23,6 +26,7 @@
 struct mediatek_private_map_data {
 	void *cached_addr;
 	void *gem_addr;
+	int prime_fd;
 };
 
 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
@@ -85,7 +89,7 @@
 
 static void *mediatek_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
 {
-	int ret;
+	int ret, prime_fd;
 	struct drm_mtk_gem_map_off gem_map;
 	struct mediatek_private_map_data *priv;
 
@@ -98,16 +102,24 @@
 		return MAP_FAILED;
 	}
 
+	ret = drmPrimeHandleToFD(bo->drv->fd, gem_map.handle, DRM_CLOEXEC, &prime_fd);
+	if (ret) {
+		drv_log("Failed to get a prime fd\n");
+		return MAP_FAILED;
+	}
+
 	void *addr = mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
 			  gem_map.offset);
 
 	vma->length = bo->total_size;
 
+	priv = calloc(1, sizeof(*priv));
+	priv->prime_fd = prime_fd;
+	vma->priv = priv;
+
 	if (bo->use_flags & BO_USE_RENDERSCRIPT) {
-		priv = calloc(1, sizeof(*priv));
 		priv->cached_addr = calloc(1, bo->total_size);
 		priv->gem_addr = addr;
-		vma->priv = priv;
 		addr = priv->cached_addr;
 	}
 
@@ -119,6 +131,7 @@
 	if (vma->priv) {
 		struct mediatek_private_map_data *priv = vma->priv;
 		vma->addr = priv->gem_addr;
+		close(priv->prime_fd);
 		free(priv->cached_addr);
 		free(priv);
 		vma->priv = NULL;
@@ -129,9 +142,25 @@
 
 static int mediatek_bo_invalidate(struct bo *bo, struct mapping *mapping)
 {
-	if (mapping->vma->priv) {
-		struct mediatek_private_map_data *priv = mapping->vma->priv;
-		memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
+	struct mediatek_private_map_data *priv = mapping->vma->priv;
+
+	if (priv) {
+		struct pollfd fds = {
+			.fd = priv->prime_fd,
+		};
+
+		if (mapping->vma->map_flags & BO_MAP_WRITE)
+			fds.events |= POLLOUT;
+
+		if (mapping->vma->map_flags & BO_MAP_READ)
+			fds.events |= POLLIN;
+
+		poll(&fds, 1, -1);
+		if (fds.revents != fds.events)
+			drv_log("poll prime_fd failed\n");
+
+		if (priv->cached_addr)
+			memcpy(priv->cached_addr, priv->gem_addr, bo->total_size);
 	}
 
 	return 0;
@@ -140,7 +169,7 @@
 static int mediatek_bo_flush(struct bo *bo, struct mapping *mapping)
 {
 	struct mediatek_private_map_data *priv = mapping->vma->priv;
-	if (priv && (mapping->vma->map_flags & BO_MAP_WRITE))
+	if (priv && priv->cached_addr && (mapping->vma->map_flags & BO_MAP_WRITE))
 		memcpy(priv->gem_addr, priv->cached_addr, bo->total_size);
 
 	return 0;