Skip to contentSkip to conversation

Support for mapping an object page by page

PATCH Superseded [email protected] 2 participants 5 messages Nov 7, 2015

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Support for mapping an object page by page

From: Ankitprasad Sharma <[email protected]>

It is possible that when we want to map an object to the aperture, either we run out of aperture space or the size of the object is larger than the mappable aperture. In such cases we might not be able to map the whole object to the aperture. For cases as such, here we introduce insert_page() which allows us to map a single page in to the mappable aperture space (which has a higher probabilty of succeeding). This can be iterated over to access the whole object by using space as meagre as page size.

Here we try to use insert_page() for pwrite_fast in case a nonblocking pin for the whole object fails, which helps us to iterate over the whole object and perform the pwrite without mapping the whole object to the mappable aperture.

We also introduce i915_gem_object_get_dma_address() to perform fast sequential lookup of the dma address associated with any page within the object.

v2: Addressed Comments

Ankitprasad Sharma (1):
drm/i915: Use insert_page for pwrite_fast

Chris Wilson (2):
drm/i915: Add support for mapping an object page by page drm/i915: Introduce i915_gem_object_get_dma_address()

 drivers/char/agp/intel-gtt.c        |  9 +++++
 drivers/gpu/drm/i915/i915_drv.h     | 17 ++++++++++
 drivers/gpu/drm/i915/i915_gem.c     | 68 +++++++++++++++++++++++++++++--------
 drivers/gpu/drm/i915/i915_gem_gtt.c | 49 ++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_gem_gtt.h |  5 +++
 include/drm/intel-gtt.h             |  3 ++
 6 files changed, 136 insertions(+), 15 deletions(-)
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH v2 1/3 drm/i915: Add support for mapping an object page by page

From: Chris Wilson <[email protected]>

Introduced a new vm specfic callback insert_page() to program a single pte in ggtt or ppgtt. This allows us to map a single page in to the mappable aperture space. This can be iterated over to access the whole object by using space as meagre as page size.

---
 drivers/char/agp/intel-gtt.c        |  9 +++++++
 drivers/gpu/drm/i915/i915_gem_gtt.c | 49 +++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/i915/i915_gem_gtt.h |  5 ++++
 include/drm/intel-gtt.h             |  3 +++
 4 files changed, 66 insertions(+)
drivers/char/agp/intel-gtt.c +9 -0
index 1341a94..7c68576 100644
@@ -838,6 +838,15 @@ static bool i830_check_flags(unsigned int flags)
 	return false;
 }
 
+void intel_gtt_insert_page(dma_addr_t addr,
+			   unsigned int pg,
+			   unsigned int flags)
+{
+	intel_private.driver->write_entry(addr, pg, flags);
+	wmb();
+}
+EXPORT_SYMBOL(intel_gtt_insert_page);
+
 void intel_gtt_insert_sg_entries(struct sg_table *st,
 				 unsigned int pg_start,
 				 unsigned int flags)
drivers/gpu/drm/i915/i915_gem_gtt.c +49 -0

85 lines not on this page · raw message

drivers/gpu/drm/i915/i915_gem_gtt.h +5 -0
index 9fbb07d..f7e3b45 100644
@@ -320,6 +320,11 @@ struct i915_address_space {
 			    uint64_t start,
 			    uint64_t length,
 			    bool use_scratch);
+	void (*insert_page)(struct i915_address_space *vm,
+			    dma_addr_t addr,
+			    uint64_t offset,
+			    enum i915_cache_level cache_level,
+			    u32 flags);
 	void (*insert_entries)(struct i915_address_space *vm,
 			       struct sg_table *st,
 			       uint64_t start,
include/drm/intel-gtt.h +3 -0
index 9e9bddaa5..f49edec 100644
@@ -13,6 +13,9 @@ void intel_gmch_remove(void);
 bool intel_enable_gtt(void);
 
 void intel_gtt_chipset_flush(void);
+void intel_gtt_insert_page(dma_addr_t addr,
+			   unsigned int pg,
+			   unsigned int flags);
 void intel_gtt_insert_sg_entries(struct sg_table *st,
 				 unsigned int pg_start,
 				 unsigned int flags);
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH v2 2/3 drm/i915: Introduce i915_gem_object_get_dma_address()

In reply to [email protected]

From: Chris Wilson <[email protected]>

This utility function is a companion to i915_gem_object_get_page() that uses the same cached iterator for the scatterlist to perform fast sequential lookup of the dma address associated with any page within the object.

---
 drivers/gpu/drm/i915/i915_drv.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/gpu/drm/i915/i915_drv.h +17 -0
index 2b5d587..4baf252 100644
@@ -2854,6 +2854,23 @@ static inline int __sg_page_count(struct scatterlist *sg)
 	return sg->length >> PAGE_SHIFT;
 }
 
+static inline dma_addr_t
+i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj, int n)
+{
+	if (n < obj->get_page.last) {
+		obj->get_page.sg = obj->pages->sgl;
+		obj->get_page.last = 0;
+	}
+
+	while (obj->get_page.last + __sg_page_count(obj->get_page.sg) <= n) {
+		obj->get_page.last += __sg_page_count(obj->get_page.sg++);
+		if (unlikely(sg_is_chain(obj->get_page.sg)))
+			obj->get_page.sg = sg_chain_ptr(obj->get_page.sg);
+	}
+
+	return sg_dma_address(obj->get_page.sg) + ((n - obj->get_page.last) << PAGE_SHIFT);
+}
+
 static inline struct page *
 i915_gem_object_get_page(struct drm_i915_gem_object *obj, int n)
 {
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH v2 3/3 drm/i915: Use insert_page for pwrite_fast

In reply to [email protected]

From: Ankitprasad Sharma <[email protected]>

In pwrite_fast, map an object page by page if obj_ggtt_pin fails. First, we try a nonblocking pin for the whole object (since that is fastest if reused), then failing that we try to grab one page in the mappable aperture. It also allows us to handle objects larger than the mappable aperture (e.g. if we need to pwrite with vGPU restricting the aperture to a measely 8MiB or something like that).

v2: Pin pages before starting pwrite, Combined duplicate loops (Chris)

---
 drivers/gpu/drm/i915/i915_gem.c | 68 ++++++++++++++++++++++++++++++++---------
 1 file changed, 53 insertions(+), 15 deletions(-)
drivers/gpu/drm/i915/i915_gem.c +53 -15
index bf5ef7a..2556936 100644
@@ -766,14 +766,26 @@ i915_gem_gtt_pwrite_fast(struct drm_device *dev,
 			 struct drm_file *file)
 {
 	struct drm_i915_private *dev_priv = dev->dev_private;
+	struct drm_mm_node node;
 	ssize_t remain;
 	loff_t offset, page_base;
 	char __user *user_data;
-	int page_offset, page_length, ret;
+	int page_offset, page_length, ret, i = 0;
+	bool pinned = true;
 
 	ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
-	if (ret)
-		goto out;
+	if (ret) {
+		pinned = false;
+		memset(&node, 0, sizeof(node));
+		ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
+							  &node, 4096, 0,
+							  I915_CACHE_NONE, 0,
+							  dev_priv->gtt.mappable_end,
+							  DRM_MM_SEARCH_DEFAULT,
+							  DRM_MM_CREATE_DEFAULT);
+		if (ret)
+			goto out;
+	}
 
 	ret = i915_gem_object_set_to_gtt_domain(obj, true);
 	if (ret)
@@ -783,26 +795,41 @@ i915_gem_gtt_pwrite_fast(struct drm_device *dev,
 	if (ret)
 		goto out_unpin;
 
+	i915_gem_object_pin_pages(obj);
+
 	user_data = to_user_ptr(args->data_ptr);
 	remain = args->size;
 
-	offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
-
 	intel_fb_obj_invalidate(obj, ORIGIN_GTT);
 
-	while (remain > 0) {
-		/* Operation in this page
-		 *
-		 * page_base = page offset within aperture
-		 * page_offset = offset within page
-		 * page_length = bytes to copy for this page
-		 */
+	if (pinned) {
+		offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
+		/* page_base = page offset within aperture */
 		page_base = offset & PAGE_MASK;
-		page_offset = offset_in_page(offset);
+	} else {
+		offset = args->offset;
+		page_base = node.start;
+		i = args->offset / PAGE_SIZE;
+	}
+
+	/* page_offset = offset within page */
+	page_offset = offset_in_page(offset);
+	while (remain > 0) {
+		/* page_length = bytes to copy for the page */
 		page_length = remain;
 		if ((page_offset + remain) > PAGE_SIZE)
 			page_length = PAGE_SIZE - page_offset;
 
+		if (!pinned) {
+			wmb();
+			dev_priv->gtt.base.insert_page(&dev_priv->gtt.base,
+					i915_gem_object_get_dma_address(obj, i),
+					node.start,
+					I915_CACHE_NONE,
+					0);
+			wmb();
+			i++;
+		}
 		/* If we get a fault while copying data, then (presumably) our
 		 * source page isn't available.  Return the error and we'll
 		 * retry in the slow path.
@@ -815,13 +842,24 @@ i915_gem_gtt_pwrite_fast(struct drm_device *dev,
 
 		remain -= page_length;
 		user_data += page_length;
-		offset += page_length;
+		page_offset = 0;
 	}
 
 out_flush:
+	if (!pinned) {
+		wmb();
+		dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
+				node.start, node.size,
+				true);
+	}
+
+	i915_gem_object_unpin_pages(obj);
 	intel_fb_obj_flush(obj, false, ORIGIN_GTT);
 out_unpin:
-	i915_gem_object_ggtt_unpin(obj);
+	if (pinned)
+		i915_gem_object_ggtt_unpin(obj);
+	else
+		drm_mm_remove_node(&node);
 out:
 	return ret;
 }
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

Re: [PATCH 3/3] drm/i915: Use insert_page for pwrite_fast

Quoted 10 lines · On Sat, Nov 07, 2015 at 01:32:15PM +0530, [email protected] wrote:

From: Ankitprasad Sharma <[email protected]>

In pwrite_fast, map an object page by page if obj_ggtt_pin fails. First, we try a nonblocking pin for the whole object (since that is fastest if reused), then failing that we try to grab one page in the mappable aperture. It also allows us to handle objects larger than the mappable aperture (e.g. if we need to pwrite with vGPU restricting the aperture to a measely 8MiB or something like that).

v2: Pin pages before starting pwrite, Combined duplicate loops (Chris)

Please just use my code as you haven't combined the two paths as well yet.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx