Module: Mesa
Branch: main
Commit: 6f467fc6a37060777060d310c8209fa70719be1b
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=6f467fc6a37060777060d310c8209fa70719be1b

Author: Lucas Fryzek <[email protected]>
Date:   Mon Feb 27 14:50:39 2023 -0500

freedreno/drm: Add more APIs to per backend API

Add bo_map, bo_from_dmabuf, and bo_close_handle to the per backend
APIs for freedreno/drm. These changes are required to implement
a KGSL backend as part of freedreno/drm.

Signed-off-by: Rob Clark <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24733>

---

 src/freedreno/drm/freedreno_bo.c         | 52 +++++++++++++++++++++++---------
 src/freedreno/drm/freedreno_bo_heap.c    |  1 +
 src/freedreno/drm/freedreno_drmif.h      |  2 ++
 src/freedreno/drm/freedreno_priv.h       |  6 ++++
 src/freedreno/drm/msm/msm_bo.c           |  2 ++
 src/freedreno/drm/msm/msm_device.c       |  2 ++
 src/freedreno/drm/virtio/virtio_bo.c     |  2 ++
 src/freedreno/drm/virtio/virtio_device.c |  2 ++
 8 files changed, 55 insertions(+), 14 deletions(-)

diff --git a/src/freedreno/drm/freedreno_bo.c b/src/freedreno/drm/freedreno_bo.c
index 3c8ed16a59f..4a48d3e91f5 100644
--- a/src/freedreno/drm/freedreno_bo.c
+++ b/src/freedreno/drm/freedreno_bo.c
@@ -219,7 +219,7 @@ out_unlock:
 }
 
 struct fd_bo *
-fd_bo_from_dmabuf(struct fd_device *dev, int fd)
+fd_bo_from_dmabuf_drm(struct fd_device *dev, int fd)
 {
    int ret, size;
    uint32_t handle;
@@ -254,6 +254,12 @@ out_unlock:
    return bo;
 }
 
+struct fd_bo *
+fd_bo_from_dmabuf(struct fd_device *dev, int fd)
+{
+   return dev->funcs->bo_from_dmabuf(dev, fd);
+}
+
 struct fd_bo *
 fd_bo_from_name(struct fd_device *dev, uint32_t name)
 {
@@ -430,6 +436,15 @@ fd_bo_fini_fences(struct fd_bo *bo)
       free(bo->fences);
 }
 
+void
+fd_bo_close_handle_drm(struct fd_device *dev, uint32_t handle)
+{
+   struct drm_gem_close req = {
+      .handle = handle,
+   };
+   drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
+}
+
 /**
  * Helper called by backends bo->funcs->destroy()
  *
@@ -453,10 +468,7 @@ fd_bo_fini_common(struct fd_bo *bo)
 
    if (handle) {
       simple_mtx_lock(&table_lock);
-      struct drm_gem_close req = {
-         .handle = handle,
-      };
-      drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
+      dev->funcs->bo_close_handle(dev, handle);
       _mesa_hash_table_remove_key(dev->handle_table, &handle);
       if (bo->name)
          _mesa_hash_table_remove_key(dev->name_table, &bo->name);
@@ -526,16 +538,28 @@ fd_bo_handle(struct fd_bo *bo)
 }
 
 int
-fd_bo_dmabuf(struct fd_bo *bo)
+fd_bo_dmabuf_drm(struct fd_bo *bo)
 {
    int ret, prime_fd;
 
+   ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR,
+                            &prime_fd);
+   if (ret < 0)
+      return ret;
+
+   return prime_fd;
+}
+
+int
+fd_bo_dmabuf(struct fd_bo *bo)
+{
+   int ret;
+
    if (suballoc_bo(bo))
       return -1;
 
-   ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR,
-                            &prime_fd);
-   if (ret) {
+   ret = bo->funcs->dmabuf(bo);
+   if (ret < 0) {
       ERROR_MSG("failed to get dmabuf fd: %d", ret);
       return ret;
    }
@@ -544,7 +568,7 @@ fd_bo_dmabuf(struct fd_bo *bo)
    bo->alloc_flags |= FD_BO_SHARED;
    bo_flush(bo);
 
-   return prime_fd;
+   return ret;
 }
 
 uint32_t
@@ -559,8 +583,8 @@ fd_bo_is_cached(struct fd_bo *bo)
    return !!(bo->alloc_flags & FD_BO_CACHED_COHERENT);
 }
 
-static void *
-bo_map(struct fd_bo *bo)
+void *
+fd_bo_map_os_mmap(struct fd_bo *bo)
 {
    if (!bo->map) {
       uint64_t offset;
@@ -590,7 +614,7 @@ fd_bo_map(struct fd_bo *bo)
    if (bo->alloc_flags & FD_BO_NOMAP)
       return NULL;
 
-   return bo_map(bo);
+   return bo->funcs->map(bo);
 }
 
 void
@@ -601,7 +625,7 @@ fd_bo_upload(struct fd_bo *bo, void *src, unsigned off, 
unsigned len)
       return;
    }
 
-   memcpy((uint8_t *)bo_map(bo) + off, src, len);
+   memcpy((uint8_t *)bo->funcs->map(bo) + off, src, len);
 }
 
 bool
diff --git a/src/freedreno/drm/freedreno_bo_heap.c 
b/src/freedreno/drm/freedreno_bo_heap.c
index 413c388bf0e..dc1af739d23 100644
--- a/src/freedreno/drm/freedreno_bo_heap.c
+++ b/src/freedreno/drm/freedreno_bo_heap.c
@@ -176,6 +176,7 @@ sa_destroy(struct fd_bo *bo)
 static struct fd_bo_funcs heap_bo_funcs = {
       .madvise = sa_madvise,
       .iova = sa_iova,
+      .map = fd_bo_map_os_mmap,
       .set_name = sa_set_name,
       .destroy = sa_destroy,
 };
diff --git a/src/freedreno/drm/freedreno_drmif.h 
b/src/freedreno/drm/freedreno_drmif.h
index 6a20e7ac3dd..886d1252568 100644
--- a/src/freedreno/drm/freedreno_drmif.h
+++ b/src/freedreno/drm/freedreno_drmif.h
@@ -286,6 +286,7 @@ struct fd_bo *fd_bo_from_handle(struct fd_device *dev, 
uint32_t handle,
                                 uint32_t size);
 struct fd_bo *fd_bo_from_name(struct fd_device *dev, uint32_t name);
 struct fd_bo *fd_bo_from_dmabuf(struct fd_device *dev, int fd);
+struct fd_bo *fd_bo_from_dmabuf_drm(struct fd_device *dev, int fd);
 void fd_bo_mark_for_dump(struct fd_bo *bo);
 
 static inline uint64_t
@@ -302,6 +303,7 @@ void fd_bo_del_array(struct fd_bo **bos, int count);
 void fd_bo_del_list_nocache(struct list_head *list);
 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name);
 uint32_t fd_bo_handle(struct fd_bo *bo);
+int fd_bo_dmabuf_drm(struct fd_bo *bo);
 int fd_bo_dmabuf(struct fd_bo *bo);
 uint32_t fd_bo_size(struct fd_bo *bo);
 void *fd_bo_map(struct fd_bo *bo);
diff --git a/src/freedreno/drm/freedreno_priv.h 
b/src/freedreno/drm/freedreno_priv.h
index 564afd5935f..e0aac089cb4 100644
--- a/src/freedreno/drm/freedreno_priv.h
+++ b/src/freedreno/drm/freedreno_priv.h
@@ -108,6 +108,8 @@ struct fd_device_funcs {
     */
    struct fd_bo *(*bo_from_handle)(struct fd_device *dev, uint32_t size,
                                    uint32_t handle);
+   struct fd_bo *(*bo_from_dmabuf)(struct fd_device *dev, int fd);
+   void (*bo_close_handle)(struct fd_device *dev, uint32_t handle);
 
    struct fd_pipe *(*pipe_new)(struct fd_device *dev, enum fd_pipe_id id,
                                unsigned prio);
@@ -421,10 +423,12 @@ fd_dev_count_deferred_cmds(struct fd_device *dev)
 
 struct fd_bo_funcs {
    int (*offset)(struct fd_bo *bo, uint64_t *offset);
+   void *(*map)(struct fd_bo *bo);
    int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
    int (*madvise)(struct fd_bo *bo, int willneed);
    uint64_t (*iova)(struct fd_bo *bo);
    void (*set_name)(struct fd_bo *bo, const char *fmt, va_list ap);
+   int (*dmabuf)(struct fd_bo *bo);
 
    /**
     * Optional hook that is called before ->destroy().  In the case of
@@ -453,6 +457,7 @@ struct fd_bo_funcs {
 };
 
 void fd_bo_add_fence(struct fd_bo *bo, struct fd_fence *fence);
+void *fd_bo_map_os_mmap(struct fd_bo *bo);
 
 enum fd_bo_state {
    FD_BO_STATE_IDLE,
@@ -466,6 +471,7 @@ void fd_bo_fini_fences(struct fd_bo *bo);
 void fd_bo_fini_common(struct fd_bo *bo);
 
 struct fd_bo *fd_bo_new_ring(struct fd_device *dev, uint32_t size);
+void fd_bo_close_handle_drm(struct fd_device *dev, uint32_t handle);
 
 #define enable_debug 0 /* TODO make dynamic */
 
diff --git a/src/freedreno/drm/msm/msm_bo.c b/src/freedreno/drm/msm/msm_bo.c
index 22db5cc1fdd..424aef1bf40 100644
--- a/src/freedreno/drm/msm/msm_bo.c
+++ b/src/freedreno/drm/msm/msm_bo.c
@@ -138,10 +138,12 @@ msm_bo_set_name(struct fd_bo *bo, const char *fmt, 
va_list ap)
 
 static const struct fd_bo_funcs funcs = {
    .offset = msm_bo_offset,
+   .map = fd_bo_map_os_mmap,
    .cpu_prep = msm_bo_cpu_prep,
    .madvise = msm_bo_madvise,
    .iova = msm_bo_iova,
    .set_name = msm_bo_set_name,
+   .dmabuf = fd_bo_dmabuf_drm,
    .destroy = fd_bo_fini_common,
 };
 
diff --git a/src/freedreno/drm/msm/msm_device.c 
b/src/freedreno/drm/msm/msm_device.c
index 4e353c1375c..605808a3d4b 100644
--- a/src/freedreno/drm/msm/msm_device.c
+++ b/src/freedreno/drm/msm/msm_device.c
@@ -38,6 +38,8 @@ msm_device_destroy(struct fd_device *dev)
 static const struct fd_device_funcs funcs = {
    .bo_new = msm_bo_new,
    .bo_from_handle = msm_bo_from_handle,
+   .bo_from_dmabuf = fd_bo_from_dmabuf_drm,
+   .bo_close_handle = fd_bo_close_handle_drm,
    .pipe_new = msm_pipe_new,
    .destroy = msm_device_destroy,
 };
diff --git a/src/freedreno/drm/virtio/virtio_bo.c 
b/src/freedreno/drm/virtio/virtio_bo.c
index a98b8c45b86..66073cee100 100644
--- a/src/freedreno/drm/virtio/virtio_bo.c
+++ b/src/freedreno/drm/virtio/virtio_bo.c
@@ -273,10 +273,12 @@ virtio_bo_finalize(struct fd_bo *bo)
 
 static const struct fd_bo_funcs funcs = {
    .offset = virtio_bo_offset,
+   .map = fd_bo_map_os_mmap,
    .cpu_prep = virtio_bo_cpu_prep,
    .madvise = virtio_bo_madvise,
    .iova = virtio_bo_iova,
    .set_name = virtio_bo_set_name,
+   .dmabuf = fd_bo_dmabuf_drm,
    .upload = virtio_bo_upload,
    .prefer_upload = virtio_bo_prefer_upload,
    .finalize = virtio_bo_finalize,
diff --git a/src/freedreno/drm/virtio/virtio_device.c 
b/src/freedreno/drm/virtio/virtio_device.c
index fb44cbdfb6b..59781f22553 100644
--- a/src/freedreno/drm/virtio/virtio_device.c
+++ b/src/freedreno/drm/virtio/virtio_device.c
@@ -42,6 +42,8 @@ virtio_device_destroy(struct fd_device *dev)
 static const struct fd_device_funcs funcs = {
    .bo_new = virtio_bo_new,
    .bo_from_handle = virtio_bo_from_handle,
+   .bo_from_dmabuf = fd_bo_from_dmabuf_drm,
+   .bo_close_handle = fd_bo_close_handle_drm,
    .pipe_new = virtio_pipe_new,
    .flush = virtio_execbuf_flush,
    .destroy = virtio_device_destroy,

Reply via email to