dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Zimmermann <tzimmermann@suse.de>
To: daniel@ffwll.ch, kraxel@redhat.com, sam@ravnborg.org,
	airlied@redhat.com, yc_chen@aspeedtech.com,
	Christian.Koenig@amd.com
Cc: Thomas Zimmermann <tzimmermann@suse.de>, dri-devel@lists.freedesktop.org
Subject: [PATCH 1/3] drm/vram: Provide vmap and vunmap operations for GEM VRAM objects
Date: Wed, 24 Jul 2019 13:30:18 +0200	[thread overview]
Message-ID: <20190724113020.3752-2-tzimmermann@suse.de> (raw)
In-Reply-To: <20190724113020.3752-1-tzimmermann@suse.de>

The pattern of temporarily pinning and kmap-ing the BO's memory is
common enough to justify helper functions that do and undo these
operations.

The implementation of vmap and vunmap for GEM VRAM helpers is
already in PRIME helpers. The patch moves the operations to separate
functions and exports them for general use.

The patch also adds a note about possible kmap counting. So far this
isn't required by drivers, but more complex use cases might make it
necessary.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
---
 drivers/gpu/drm/drm_gem_vram_helper.c | 55 ++++++++++++++++++++++-----
 include/drm/drm_gem_vram_helper.h     | 12 ++++++
 2 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
index e0fbfb6570cf..54758e4debca 100644
--- a/drivers/gpu/drm/drm_gem_vram_helper.c
+++ b/drivers/gpu/drm/drm_gem_vram_helper.c
@@ -340,6 +340,48 @@ void drm_gem_vram_kunmap(struct drm_gem_vram_object *gbo)
 }
 EXPORT_SYMBOL(drm_gem_vram_kunmap);
 
+/**
+ * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
+	space
+ * @gem:	The GEM VRAM object to map
+ *
+ * The vmap function pins a GEM VRAM object to it's current location, either
+ * system or video memory, and maps it's buffer into kernel address space. As
+ * pinned object cannot be reloacted, you should not permanently pin objects.
+ *
+ * Returns:
+ * The buffer's virtual address on success, or
+ * an ERR_PTR()-encoded error code otherwise.
+ */
+void *drm_gem_vram_vmap(struct drm_gem_vram_object *gbo)
+{
+	int ret;
+	void *base;
+
+	ret = drm_gem_vram_pin(gbo, 0);
+	if (ret)
+		return ERR_PTR(ret);
+	base = drm_gem_vram_kmap(gbo, true, NULL);
+	if (IS_ERR(base)) {
+		drm_gem_vram_unpin(gbo);
+		return base;
+	}
+	return base;
+}
+EXPORT_SYMBOL(drm_gem_vram_vmap);
+
+/**
+ * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
+ * @gem:	The GEM VRAM object to unmap
+ * @vaddr:	The mapping's base address
+ */
+void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, void *vaddr)
+{
+	drm_gem_vram_kunmap(gbo);
+	drm_gem_vram_unpin(gbo);
+}
+EXPORT_SYMBOL(drm_gem_vram_vunmap);
+
 /**
  * drm_gem_vram_fill_create_dumb() - \
 	Helper for implementing &struct drm_driver.dumb_create
@@ -595,17 +637,11 @@ static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
 static void *drm_gem_vram_object_vmap(struct drm_gem_object *gem)
 {
 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
-	int ret;
 	void *base;
 
-	ret = drm_gem_vram_pin(gbo, 0);
-	if (ret)
+	base = drm_gem_vram_vmap(gbo);
+	if (IS_ERR(base))
 		return NULL;
-	base = drm_gem_vram_kmap(gbo, true, NULL);
-	if (IS_ERR(base)) {
-		drm_gem_vram_unpin(gbo);
-		return NULL;
-	}
 	return base;
 }
 
@@ -620,8 +656,7 @@ static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
 {
 	struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
 
-	drm_gem_vram_kunmap(gbo);
-	drm_gem_vram_unpin(gbo);
+	drm_gem_vram_vunmap(gbo, vaddr);
 }
 
 /*
diff --git a/include/drm/drm_gem_vram_helper.h b/include/drm/drm_gem_vram_helper.h
index b41d932eb53a..5192c169cec2 100644
--- a/include/drm/drm_gem_vram_helper.h
+++ b/include/drm/drm_gem_vram_helper.h
@@ -44,6 +44,16 @@ struct drm_gem_vram_object {
 	struct ttm_placement placement;
 	struct ttm_place placements[2];
 
+	/* TODO: Maybe implement a map counter.
+	 *
+	 * So far, drivers based on VRAM helpers don't have overlapping
+	 * mapping operations. A driver temporarily maps an object and
+	 * unmaps it ASAP. This works well for fbdev emulation or cursors.
+	 *
+	 * If we ever have a driver with buffer objects that are mapped
+	 * by multiple code fragments concurrently, we may need a map
+	 * counter to get the mapping right.
+	 */
 	int pin_count;
 };
 
@@ -84,6 +94,8 @@ int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);
 void *drm_gem_vram_kmap(struct drm_gem_vram_object *gbo, bool map,
 			bool *is_iomem);
 void drm_gem_vram_kunmap(struct drm_gem_vram_object *gbo);
+void *drm_gem_vram_vmap(struct drm_gem_vram_object *gbo);
+void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, void *vaddr);
 
 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
 				  struct drm_device *dev,
-- 
2.22.0

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  reply	other threads:[~2019-07-24 11:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-24 11:30 [PATCH 0/3] Provide vmap/vunmap for VRAM helpers Thomas Zimmermann
2019-07-24 11:30 ` Thomas Zimmermann [this message]
2019-07-24 12:00   ` [PATCH 1/3] drm/vram: Provide vmap and vunmap operations for GEM VRAM objects Daniel Vetter
2019-07-24 16:55     ` Thomas Zimmermann
2019-07-24 11:30 ` [PATCH 2/3] drm/ast: Use drm_gem_vram_{vmap, vunmap}() to map cursor source BO Thomas Zimmermann
2019-07-24 11:30 ` [PATCH 3/3] drm/mgag200: " Thomas Zimmermann
  -- strict thread matches above, loose matches on Subject: below --
2019-09-11 12:03 [PATCH 0/3] drm/vram: Provide GEM VRAM vmap()/vunmap/() Thomas Zimmermann
2019-09-11 12:03 ` [PATCH 1/3] drm/vram: Provide vmap and vunmap operations for GEM VRAM objects Thomas Zimmermann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190724113020.3752-2-tzimmermann@suse.de \
    --to=tzimmermann@suse.de \
    --cc=Christian.Koenig@amd.com \
    --cc=airlied@redhat.com \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=kraxel@redhat.com \
    --cc=sam@ravnborg.org \
    --cc=yc_chen@aspeedtech.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox