OSDN Git Service

minigbm: minimal buffer allocation support using msm gem ioctls
authorTanmay Shah <tanmay@codeaurora.org>
Wed, 11 Jul 2018 23:41:05 +0000 (16:41 -0700)
committerchrome-bot <chrome-bot@chromium.org>
Sat, 29 Sep 2018 11:27:35 +0000 (04:27 -0700)
DRM_IOCTL_MSM_GEM_NEW is used to create buffer and
DRM_IOCTL_MSM_GEM_INFO is used to map buffer.

BUG=none
TEST=mmap_test, plane_test

Change-Id: I7db0cf36b03625f02828b63946550c768c422419
Signed-off-by: Tanmay Shah <tanmay@codeaurora.org>
Reviewed-on: https://chromium-review.googlesource.com/1134481
Reviewed-by: Kristian H. Kristensen <hoegsberg@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
msm.c

diff --git a/msm.c b/msm.c
index 10ada0b..8060cea 100644 (file)
--- a/msm.c
+++ b/msm.c
@@ -6,26 +6,32 @@
 
 #ifdef DRV_MSM
 
+#include <errno.h>
+#include <msm_drm.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <xf86drm.h>
+
 #include "drv_priv.h"
 #include "helpers.h"
 #include "util.h"
 
-#define MESA_LLVMPIPE_TILE_ORDER 6
-#define MESA_LLVMPIPE_TILE_SIZE (1 << MESA_LLVMPIPE_TILE_ORDER)
+#define DEFAULT_ALIGNMENT 64
 
 static const uint32_t render_target_formats[] = { DRM_FORMAT_ABGR8888, DRM_FORMAT_ARGB8888,
                                                  DRM_FORMAT_RGB565, DRM_FORMAT_XBGR8888,
                                                  DRM_FORMAT_XRGB8888 };
 
-static const uint32_t supported_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8, DRM_FORMAT_YVU420,
-                                             DRM_FORMAT_YVU420_ANDROID };
+static const uint32_t texture_source_formats[] = { DRM_FORMAT_NV12, DRM_FORMAT_R8,
+                                                  DRM_FORMAT_YVU420, DRM_FORMAT_YVU420_ANDROID };
 
 static int msm_init(struct driver *drv)
 {
        drv_add_combinations(drv, render_target_formats, ARRAY_SIZE(render_target_formats),
                             &LINEAR_METADATA, BO_USE_RENDER_MASK);
 
-       drv_add_combinations(drv, supported_formats, ARRAY_SIZE(supported_formats),
+       drv_add_combinations(drv, texture_source_formats, ARRAY_SIZE(texture_source_formats),
                             &LINEAR_METADATA, BO_USE_TEXTURE_MASK | BO_USE_HW_VIDEO_DECODER);
 
        /* Android CTS tests require this. */
@@ -34,11 +40,22 @@ static int msm_init(struct driver *drv)
        return drv_modify_linear_combinations(drv);
 }
 
+/* msm_bo_create will create linear buffers for now */
 static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
                         uint64_t flags)
 {
-       width = ALIGN(width, MESA_LLVMPIPE_TILE_SIZE);
-       height = ALIGN(height, MESA_LLVMPIPE_TILE_SIZE);
+       struct drm_msm_gem_new req;
+       uint32_t stride, alignw, alignh;
+       int ret;
+       size_t i;
+
+       /* will get alignment from libadreno eventually */
+       alignw = ALIGN(width, DEFAULT_ALIGNMENT);
+       alignh = ALIGN(height, DEFAULT_ALIGNMENT);
+
+       /* HAL_PIXEL_FORMAT_YV12 requires that the buffer's height not be aligned. */
+       if (bo->format == DRM_FORMAT_YVU420_ANDROID)
+               alignh = bo->height;
 
        /*
         * The extra 12KB at the end are a requirement of the Venus codec driver.
@@ -46,19 +63,60 @@ static int msm_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_
         * we multiply this padding by 2/3 here.
         */
        if (bo->format == DRM_FORMAT_NV12)
-               height += 2 * DIV_ROUND_UP(0x3000, 3 * width);
+               alignh += 2 * DIV_ROUND_UP(0x3000, 3 * alignw);
+
+       stride = drv_stride_from_format(format, alignw, 0);
+
+       /* Calculate size and assign stride, size, offset to each plane based on format */
+       drv_bo_from_format(bo, stride, alignh, format);
 
-       return drv_dumb_bo_create(bo, width, height, format, flags);
+       memset(&req, 0, sizeof(req));
+       req.flags = MSM_BO_WC | MSM_BO_SCANOUT;
+       req.size = bo->total_size;
+
+       ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_NEW, &req);
+       if (ret) {
+               drv_log("DRM_IOCTL_MSM_GEM_NEW failed with %s\n", strerror(errno));
+               return ret;
+       }
+
+       /*
+        * Though we use only one plane, we need to set handle for
+        * all planes to pass kernel checks
+        */
+       for (i = 0; i < bo->num_planes; i++) {
+               bo->handles[i].u32 = req.handle;
+       }
+
+       return 0;
+}
+
+static void *msm_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
+{
+       int ret;
+       struct drm_msm_gem_info req;
+
+       memset(&req, 0, sizeof(req));
+       req.handle = bo->handles[0].u32;
+
+       ret = drmIoctl(bo->drv->fd, DRM_IOCTL_MSM_GEM_INFO, &req);
+       if (ret) {
+               drv_log("DRM_IOCLT_MSM_GEM_INFO failed with %s\n", strerror(errno));
+               return MAP_FAILED;
+       }
+       vma->length = bo->total_size;
+
+       return mmap(0, bo->total_size, drv_get_prot(map_flags), MAP_SHARED, bo->drv->fd,
+                   req.offset);
 }
 
 const struct backend backend_msm = {
        .name = "msm",
        .init = msm_init,
        .bo_create = msm_bo_create,
-       .bo_destroy = drv_dumb_bo_destroy,
+       .bo_destroy = drv_gem_bo_destroy,
        .bo_import = drv_prime_bo_import,
-       .bo_map = drv_dumb_bo_map,
+       .bo_map = msm_bo_map,
        .bo_unmap = drv_bo_munmap,
 };
-
 #endif /* DRV_MSM */