2022-08-29 01:59:35

by oushixiong

[permalink] [raw]
Subject: [PTACH v3] drm/ast: add dmabuf/prime buffer sharing support

This patch adds ast specific codes for DRM prime feature, this is to
allow for offloading of rending in one direction and outputs in other.

This patch is designed to solve the problem that the AST is not displayed
when the server plug in a discrete graphics graphics card at the same time.
We call the dirty callback function to copy the rendering results of the
discrete graphics card to the ast side by dma-buf.

v1->v2:
- Fix the comment.
v2->v3:
- we remove the gem_prime_import_sg_table callback and use the gem_prime_import
callback, becuase it just map and access the buffer with the CPU. and do not
to pin the buffer.

Signed-off-by: oushixiong <[email protected]>
---
drivers/gpu/drm/ast/ast_drv.c | 27 +++++++
drivers/gpu/drm/ast/ast_mode.c | 125 ++++++++++++++++++++++++++++++++-
2 files changed, 151 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/ast/ast_drv.c b/drivers/gpu/drm/ast/ast_drv.c
index 7465c4f0156a..107383a56ca7 100644
--- a/drivers/gpu/drm/ast/ast_drv.c
+++ b/drivers/gpu/drm/ast/ast_drv.c
@@ -28,6 +28,7 @@

#include <linux/module.h>
#include <linux/pci.h>
+#include <linux/dma-buf.h>

#include <drm/drm_aperture.h>
#include <drm/drm_atomic_helper.h>
@@ -50,6 +51,29 @@ module_param_named(modeset, ast_modeset, int, 0400);

DEFINE_DRM_GEM_FOPS(ast_fops);

+struct drm_gem_object *ast_gem_prime_import(struct drm_device *dev,
+ struct dma_buf *dma_buf)
+{
+ struct drm_gem_vram_object *gbo;
+
+ gbo = drm_gem_vram_of_gem(dma_buf->priv);
+ if (gbo->bo.base.dev == dev) {
+ /*
+ * Importing dmabuf exported from out own gem increases
+ * refcount on gem itself instead of f_count of dmabuf.
+ */
+ drm_gem_object_get(&gbo->bo.base);
+ return &gbo->bo.base;
+ }
+
+ gbo = drm_gem_vram_create(dev, dma_buf->size, 0);
+ if (IS_ERR(gbo))
+ return NULL;
+
+ get_dma_buf(dma_buf);
+ return &gbo->bo.base;
+}
+
static const struct drm_driver ast_driver = {
.driver_features = DRIVER_ATOMIC |
DRIVER_GEM |
@@ -63,6 +87,9 @@ static const struct drm_driver ast_driver = {
.minor = DRIVER_MINOR,
.patchlevel = DRIVER_PATCHLEVEL,

+ .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+ .gem_prime_import = ast_gem_prime_import,
+
DRM_GEM_VRAM_DRIVER
};

diff --git a/drivers/gpu/drm/ast/ast_mode.c b/drivers/gpu/drm/ast/ast_mode.c
index 45b56b39ad47..65a4342c5622 100644
--- a/drivers/gpu/drm/ast/ast_mode.c
+++ b/drivers/gpu/drm/ast/ast_mode.c
@@ -48,6 +48,8 @@
#include "ast_drv.h"
#include "ast_tables.h"

+MODULE_IMPORT_NS(DMA_BUF);
+
static inline void ast_load_palette_index(struct ast_private *ast,
u8 index, u8 red, u8 green,
u8 blue)
@@ -1535,8 +1537,129 @@ static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs =
.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
};

+static int ast_handle_damage(struct drm_framebuffer *fb, int x, int y,
+ int width, int height)
+{
+ struct drm_gem_vram_object *dst_bo = NULL;
+ void *dst = NULL;
+ int ret = 0, i;
+ unsigned long offset = 0;
+ bool unmap = false;
+ unsigned int bytesPerPixel;
+ struct iosys_map map;
+ struct iosys_map dmabuf_map;
+
+ bytesPerPixel = fb->format->cpp[0];
+
+ if (!fb->obj[0]->dma_buf)
+ return -EINVAL;
+
+ if (!fb->obj[0]->dma_buf->vmap_ptr.vaddr) {
+ ret = dma_buf_vmap(fb->obj[0]->dma_buf, &dmabuf_map);
+ if (ret)
+ return ret;
+ } else
+ dmabuf_map.vaddr = fb->obj[0]->dma_buf->vmap_ptr.vaddr;
+
+ dst_bo = drm_gem_vram_of_gem(fb->obj[0]);
+
+ ret = drm_gem_vram_pin(dst_bo, 0);
+ if (ret) {
+ DRM_ERROR("ast_bo_pin failed\n");
+ return ret;
+ }
+
+ if (!dst_bo->map.vaddr) {
+ ret = drm_gem_vram_vmap(dst_bo, &map);
+ if (ret) {
+ drm_gem_vram_unpin(dst_bo);
+ DRM_ERROR("failed to vmap fbcon\n");
+ return ret;
+ }
+ unmap = true;
+ }
+ dst = dst_bo->map.vaddr;
+
+ for (i = y; i < y + height; i++) {
+ offset = i * fb->pitches[0] + (x * bytesPerPixel);
+ memcpy_toio(dst + offset, dmabuf_map.vaddr + offset,
+ width * bytesPerPixel);
+ }
+
+ if (unmap)
+ drm_gem_vram_vunmap(dst_bo, &map);
+
+ drm_gem_vram_unpin(dst_bo);
+
+ return 0;
+}
+
+
+static int ast_user_framebuffer_dirty(struct drm_framebuffer *fb,
+ struct drm_file *file,
+ unsigned int flags,
+ unsigned int color,
+ struct drm_clip_rect *clips,
+ unsigned int num_clips)
+{
+ int i, ret = 0;
+
+ drm_modeset_lock_all(fb->dev);
+ if (fb->obj[0]->dma_buf) {
+ ret = dma_buf_begin_cpu_access(fb->obj[0]->dma_buf,
+ DMA_FROM_DEVICE);
+ if (ret)
+ goto unlock;
+ }
+
+ for (i = 0; i < num_clips; i++) {
+ ret = ast_handle_damage(fb, clips[i].x1, clips[i].y1,
+ clips[i].x2 - clips[i].x1, clips[i].y2 - clips[i].y1);
+ if (ret)
+ break;
+ }
+
+ if (fb->obj[0]->dma_buf) {
+ dma_buf_end_cpu_access(fb->obj[0]->dma_buf,
+ DMA_FROM_DEVICE);
+ }
+
+unlock:
+ drm_modeset_unlock_all(fb->dev);
+
+ return ret;
+}
+
+static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
+{
+ struct iosys_map dmabuf_map;
+
+ if (fb->obj[0]->dma_buf) {
+ dmabuf_map.is_iomem = fb->obj[0]->dma_buf->vmap_ptr.is_iomem;
+ dmabuf_map.vaddr = fb->obj[0]->dma_buf->vmap_ptr.vaddr;
+ if (dmabuf_map.vaddr)
+ dma_buf_vunmap(fb->obj[0]->dma_buf, &dmabuf_map);
+ }
+
+ drm_gem_fb_destroy(fb);
+}
+
+static const struct drm_framebuffer_funcs ast_gem_fb_funcs_dirtyfb = {
+ .destroy = ast_user_framebuffer_destroy,
+ .create_handle = drm_gem_fb_create_handle,
+ .dirty = ast_user_framebuffer_dirty,
+};
+
+static struct drm_framebuffer *
+ast_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
+ const struct drm_mode_fb_cmd2 *mode_cmd)
+{
+ return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
+ &ast_gem_fb_funcs_dirtyfb);
+}
+
static const struct drm_mode_config_funcs ast_mode_config_funcs = {
- .fb_create = drm_gem_fb_create,
+ .fb_create = ast_gem_fb_create_with_dirty,
.mode_valid = drm_vram_helper_mode_valid,
.atomic_check = drm_atomic_helper_check,
.atomic_commit = drm_atomic_helper_commit,
--
2.17.1


No virus found
Checked by Hillstone Network AntiVirus


2022-08-29 04:56:32

by kernel test robot

[permalink] [raw]
Subject: Re: [PTACH v3] drm/ast: add dmabuf/prime buffer sharing support

Hi oushixiong,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on drm-misc/drm-misc-next]
[also build test WARNING on drm/drm-next drm-intel/for-linux-next drm-tip/drm-tip linus/master v6.0-rc3 next-20220826]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/oushixiong/drm-ast-add-dmabuf-prime-buffer-sharing-support/20220829-084713
base: git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
config: i386-randconfig-a013 (https://download.01.org/0day-ci/archive/20220829/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/da31884b8b7e33af5cd8aa750dea30fde59d52aa
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review oushixiong/drm-ast-add-dmabuf-prime-buffer-sharing-support/20220829-084713
git checkout da31884b8b7e33af5cd8aa750dea30fde59d52aa
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/gpu/drm/ast/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <[email protected]>

All warnings (new ones prefixed by >>):

>> drivers/gpu/drm/ast/ast_drv.c:54:24: warning: no previous prototype for function 'ast_gem_prime_import' [-Wmissing-prototypes]
struct drm_gem_object *ast_gem_prime_import(struct drm_device *dev,
^
drivers/gpu/drm/ast/ast_drv.c:54:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
struct drm_gem_object *ast_gem_prime_import(struct drm_device *dev,
^
static
1 warning generated.


vim +/ast_gem_prime_import +54 drivers/gpu/drm/ast/ast_drv.c

53
> 54 struct drm_gem_object *ast_gem_prime_import(struct drm_device *dev,
55 struct dma_buf *dma_buf)
56 {
57 struct drm_gem_vram_object *gbo;
58
59 gbo = drm_gem_vram_of_gem(dma_buf->priv);
60 if (gbo->bo.base.dev == dev) {
61 /*
62 * Importing dmabuf exported from out own gem increases
63 * refcount on gem itself instead of f_count of dmabuf.
64 */
65 drm_gem_object_get(&gbo->bo.base);
66 return &gbo->bo.base;
67 }
68
69 gbo = drm_gem_vram_create(dev, dma_buf->size, 0);
70 if (IS_ERR(gbo))
71 return NULL;
72
73 get_dma_buf(dma_buf);
74 return &gbo->bo.base;
75 }
76

--
0-DAY CI Kernel Test Service
https://01.org/lkp

2022-08-29 06:30:06

by Christian König

[permalink] [raw]
Subject: Re: [PTACH v3] drm/ast: add dmabuf/prime buffer sharing support

Am 27.08.22 um 11:10 schrieb oushixiong:
> This patch adds ast specific codes for DRM prime feature, this is to
> allow for offloading of rending in one direction and outputs in other.
>
> This patch is designed to solve the problem that the AST is not displayed
> when the server plug in a discrete graphics graphics card at the same time.
> We call the dirty callback function to copy the rendering results of the
> discrete graphics card to the ast side by dma-buf.
>
> v1->v2:
> - Fix the comment.
> v2->v3:
> - we remove the gem_prime_import_sg_table callback and use the gem_prime_import
> callback, becuase it just map and access the buffer with the CPU. and do not
> to pin the buffer.
>
> Signed-off-by: oushixiong <[email protected]>

Thomas should probably comment as well, I think at least the memcpy_toio
usage can still be made cleaner.

But from the DMA-buf side I think that looks good now, feel free to add
an Acked-by: Christian König <[email protected]>

Regards,
Christian.

> ---
> drivers/gpu/drm/ast/ast_drv.c | 27 +++++++
> drivers/gpu/drm/ast/ast_mode.c | 125 ++++++++++++++++++++++++++++++++-
> 2 files changed, 151 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/ast/ast_drv.c b/drivers/gpu/drm/ast/ast_drv.c
> index 7465c4f0156a..107383a56ca7 100644
> --- a/drivers/gpu/drm/ast/ast_drv.c
> +++ b/drivers/gpu/drm/ast/ast_drv.c
> @@ -28,6 +28,7 @@
>
> #include <linux/module.h>
> #include <linux/pci.h>
> +#include <linux/dma-buf.h>
>
> #include <drm/drm_aperture.h>
> #include <drm/drm_atomic_helper.h>
> @@ -50,6 +51,29 @@ module_param_named(modeset, ast_modeset, int, 0400);
>
> DEFINE_DRM_GEM_FOPS(ast_fops);
>
> +struct drm_gem_object *ast_gem_prime_import(struct drm_device *dev,
> + struct dma_buf *dma_buf)
> +{
> + struct drm_gem_vram_object *gbo;
> +
> + gbo = drm_gem_vram_of_gem(dma_buf->priv);
> + if (gbo->bo.base.dev == dev) {
> + /*
> + * Importing dmabuf exported from out own gem increases
> + * refcount on gem itself instead of f_count of dmabuf.
> + */
> + drm_gem_object_get(&gbo->bo.base);
> + return &gbo->bo.base;
> + }
> +
> + gbo = drm_gem_vram_create(dev, dma_buf->size, 0);
> + if (IS_ERR(gbo))
> + return NULL;
> +
> + get_dma_buf(dma_buf);
> + return &gbo->bo.base;
> +}
> +
> static const struct drm_driver ast_driver = {
> .driver_features = DRIVER_ATOMIC |
> DRIVER_GEM |
> @@ -63,6 +87,9 @@ static const struct drm_driver ast_driver = {
> .minor = DRIVER_MINOR,
> .patchlevel = DRIVER_PATCHLEVEL,
>
> + .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
> + .gem_prime_import = ast_gem_prime_import,
> +
> DRM_GEM_VRAM_DRIVER
> };
>
> diff --git a/drivers/gpu/drm/ast/ast_mode.c b/drivers/gpu/drm/ast/ast_mode.c
> index 45b56b39ad47..65a4342c5622 100644
> --- a/drivers/gpu/drm/ast/ast_mode.c
> +++ b/drivers/gpu/drm/ast/ast_mode.c
> @@ -48,6 +48,8 @@
> #include "ast_drv.h"
> #include "ast_tables.h"
>
> +MODULE_IMPORT_NS(DMA_BUF);
> +
> static inline void ast_load_palette_index(struct ast_private *ast,
> u8 index, u8 red, u8 green,
> u8 blue)
> @@ -1535,8 +1537,129 @@ static const struct drm_mode_config_helper_funcs ast_mode_config_helper_funcs =
> .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
> };
>
> +static int ast_handle_damage(struct drm_framebuffer *fb, int x, int y,
> + int width, int height)
> +{
> + struct drm_gem_vram_object *dst_bo = NULL;
> + void *dst = NULL;
> + int ret = 0, i;
> + unsigned long offset = 0;
> + bool unmap = false;
> + unsigned int bytesPerPixel;
> + struct iosys_map map;
> + struct iosys_map dmabuf_map;
> +
> + bytesPerPixel = fb->format->cpp[0];
> +
> + if (!fb->obj[0]->dma_buf)
> + return -EINVAL;
> +
> + if (!fb->obj[0]->dma_buf->vmap_ptr.vaddr) {
> + ret = dma_buf_vmap(fb->obj[0]->dma_buf, &dmabuf_map);
> + if (ret)
> + return ret;
> + } else
> + dmabuf_map.vaddr = fb->obj[0]->dma_buf->vmap_ptr.vaddr;
> +
> + dst_bo = drm_gem_vram_of_gem(fb->obj[0]);
> +
> + ret = drm_gem_vram_pin(dst_bo, 0);
> + if (ret) {
> + DRM_ERROR("ast_bo_pin failed\n");
> + return ret;
> + }
> +
> + if (!dst_bo->map.vaddr) {
> + ret = drm_gem_vram_vmap(dst_bo, &map);
> + if (ret) {
> + drm_gem_vram_unpin(dst_bo);
> + DRM_ERROR("failed to vmap fbcon\n");
> + return ret;
> + }
> + unmap = true;
> + }
> + dst = dst_bo->map.vaddr;
> +
> + for (i = y; i < y + height; i++) {
> + offset = i * fb->pitches[0] + (x * bytesPerPixel);
> + memcpy_toio(dst + offset, dmabuf_map.vaddr + offset,
> + width * bytesPerPixel);
> + }
> +
> + if (unmap)
> + drm_gem_vram_vunmap(dst_bo, &map);
> +
> + drm_gem_vram_unpin(dst_bo);
> +
> + return 0;
> +}
> +
> +
> +static int ast_user_framebuffer_dirty(struct drm_framebuffer *fb,
> + struct drm_file *file,
> + unsigned int flags,
> + unsigned int color,
> + struct drm_clip_rect *clips,
> + unsigned int num_clips)
> +{
> + int i, ret = 0;
> +
> + drm_modeset_lock_all(fb->dev);
> + if (fb->obj[0]->dma_buf) {
> + ret = dma_buf_begin_cpu_access(fb->obj[0]->dma_buf,
> + DMA_FROM_DEVICE);
> + if (ret)
> + goto unlock;
> + }
> +
> + for (i = 0; i < num_clips; i++) {
> + ret = ast_handle_damage(fb, clips[i].x1, clips[i].y1,
> + clips[i].x2 - clips[i].x1, clips[i].y2 - clips[i].y1);
> + if (ret)
> + break;
> + }
> +
> + if (fb->obj[0]->dma_buf) {
> + dma_buf_end_cpu_access(fb->obj[0]->dma_buf,
> + DMA_FROM_DEVICE);
> + }
> +
> +unlock:
> + drm_modeset_unlock_all(fb->dev);
> +
> + return ret;
> +}
> +
> +static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
> +{
> + struct iosys_map dmabuf_map;
> +
> + if (fb->obj[0]->dma_buf) {
> + dmabuf_map.is_iomem = fb->obj[0]->dma_buf->vmap_ptr.is_iomem;
> + dmabuf_map.vaddr = fb->obj[0]->dma_buf->vmap_ptr.vaddr;
> + if (dmabuf_map.vaddr)
> + dma_buf_vunmap(fb->obj[0]->dma_buf, &dmabuf_map);
> + }
> +
> + drm_gem_fb_destroy(fb);
> +}
> +
> +static const struct drm_framebuffer_funcs ast_gem_fb_funcs_dirtyfb = {
> + .destroy = ast_user_framebuffer_destroy,
> + .create_handle = drm_gem_fb_create_handle,
> + .dirty = ast_user_framebuffer_dirty,
> +};
> +
> +static struct drm_framebuffer *
> +ast_gem_fb_create_with_dirty(struct drm_device *dev, struct drm_file *file,
> + const struct drm_mode_fb_cmd2 *mode_cmd)
> +{
> + return drm_gem_fb_create_with_funcs(dev, file, mode_cmd,
> + &ast_gem_fb_funcs_dirtyfb);
> +}
> +
> static const struct drm_mode_config_funcs ast_mode_config_funcs = {
> - .fb_create = drm_gem_fb_create,
> + .fb_create = ast_gem_fb_create_with_dirty,
> .mode_valid = drm_vram_helper_mode_valid,
> .atomic_check = drm_atomic_helper_check,
> .atomic_commit = drm_atomic_helper_commit,