Skip to content

Commit 7961c5b

Browse files
mlankhorstmatt-auld
authored andcommitted
drm/i915: Add TTM offset argument to mmap.
The FIXED mapping is only used for ttm, and tells userspace that the mapping type is pre-defined. This disables the other type of mmap offsets when discrete memory is used, so fix the selftests as well. Document the struct as well, so it shows up in docbook. Cc: Jason Ekstrand <[email protected]> Reviewed-by: Daniel Vetter <[email protected]> Reviewed-by: Jason Ekstrand <[email protected]> Signed-off-by: Maarten Lankhorst <[email protected]> [mauld: Included minor fixes from the review comments] Signed-off-by: Matthew Auld <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 15eb083 commit 7961c5b

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

drivers/gpu/drm/i915/gem/i915_gem_mman.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,16 @@ __assign_mmap_offset(struct drm_i915_gem_object *obj,
679679
return -ENODEV;
680680

681681
if (obj->ops->mmap_offset) {
682+
if (mmap_type != I915_MMAP_TYPE_FIXED)
683+
return -ENODEV;
684+
682685
*offset = obj->ops->mmap_offset(obj);
683686
return 0;
684687
}
685688

689+
if (mmap_type == I915_MMAP_TYPE_FIXED)
690+
return -ENODEV;
691+
686692
if (mmap_type != I915_MMAP_TYPE_GTT &&
687693
!i915_gem_object_has_struct_page(obj) &&
688694
!i915_gem_object_has_iomem(obj))
@@ -727,7 +733,9 @@ i915_gem_dumb_mmap_offset(struct drm_file *file,
727733
{
728734
enum i915_mmap_type mmap_type;
729735

730-
if (boot_cpu_has(X86_FEATURE_PAT))
736+
if (HAS_LMEM(to_i915(dev)))
737+
mmap_type = I915_MMAP_TYPE_FIXED;
738+
else if (boot_cpu_has(X86_FEATURE_PAT))
731739
mmap_type = I915_MMAP_TYPE_WC;
732740
else if (!i915_ggtt_has_aperture(&to_i915(dev)->ggtt))
733741
return -ENODEV;
@@ -798,6 +806,10 @@ i915_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
798806
type = I915_MMAP_TYPE_UC;
799807
break;
800808

809+
case I915_MMAP_OFFSET_FIXED:
810+
type = I915_MMAP_TYPE_FIXED;
811+
break;
812+
801813
default:
802814
return -EINVAL;
803815
}
@@ -968,6 +980,9 @@ int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
968980
vma->vm_ops = &vm_ops_cpu;
969981
break;
970982

983+
case I915_MMAP_TYPE_FIXED:
984+
GEM_WARN_ON(1);
985+
fallthrough;
971986
case I915_MMAP_TYPE_WB:
972987
vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
973988
vma->vm_ops = &vm_ops_cpu;

drivers/gpu/drm/i915/gem/i915_gem_object_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ enum i915_mmap_type {
105105
I915_MMAP_TYPE_WC,
106106
I915_MMAP_TYPE_WB,
107107
I915_MMAP_TYPE_UC,
108+
I915_MMAP_TYPE_FIXED,
108109
};
109110

110111
struct i915_mmap_offset {

drivers/gpu/drm/i915/gem/selftests/i915_gem_mman.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,14 @@ static int make_obj_busy(struct drm_i915_gem_object *obj)
573573
return 0;
574574
}
575575

576+
static enum i915_mmap_type default_mapping(struct drm_i915_private *i915)
577+
{
578+
if (HAS_LMEM(i915))
579+
return I915_MMAP_TYPE_FIXED;
580+
581+
return I915_MMAP_TYPE_GTT;
582+
}
583+
576584
static bool assert_mmap_offset(struct drm_i915_private *i915,
577585
unsigned long size,
578586
int expected)
@@ -585,7 +593,7 @@ static bool assert_mmap_offset(struct drm_i915_private *i915,
585593
if (IS_ERR(obj))
586594
return expected && expected == PTR_ERR(obj);
587595

588-
ret = __assign_mmap_offset(obj, I915_MMAP_TYPE_GTT, &offset, NULL);
596+
ret = __assign_mmap_offset(obj, default_mapping(i915), &offset, NULL);
589597
i915_gem_object_put(obj);
590598

591599
return ret == expected;
@@ -689,7 +697,7 @@ static int igt_mmap_offset_exhaustion(void *arg)
689697
goto out;
690698
}
691699

692-
err = __assign_mmap_offset(obj, I915_MMAP_TYPE_GTT, &offset, NULL);
700+
err = __assign_mmap_offset(obj, default_mapping(i915), &offset, NULL);
693701
if (err) {
694702
pr_err("Unable to insert object into reclaimed hole\n");
695703
goto err_obj;
@@ -831,8 +839,14 @@ static int wc_check(struct drm_i915_gem_object *obj)
831839

832840
static bool can_mmap(struct drm_i915_gem_object *obj, enum i915_mmap_type type)
833841
{
842+
struct drm_i915_private *i915 = to_i915(obj->base.dev);
834843
bool no_map;
835844

845+
if (HAS_LMEM(i915))
846+
return type == I915_MMAP_TYPE_FIXED;
847+
else if (type == I915_MMAP_TYPE_FIXED)
848+
return false;
849+
836850
if (type == I915_MMAP_TYPE_GTT &&
837851
!i915_ggtt_has_aperture(&to_i915(obj->base.dev)->ggtt))
838852
return false;
@@ -970,6 +984,8 @@ static int igt_mmap(void *arg)
970984
err = __igt_mmap(i915, obj, I915_MMAP_TYPE_GTT);
971985
if (err == 0)
972986
err = __igt_mmap(i915, obj, I915_MMAP_TYPE_WC);
987+
if (err == 0)
988+
err = __igt_mmap(i915, obj, I915_MMAP_TYPE_FIXED);
973989

974990
i915_gem_object_put(obj);
975991
if (err)
@@ -987,6 +1003,7 @@ static const char *repr_mmap_type(enum i915_mmap_type type)
9871003
case I915_MMAP_TYPE_WB: return "wb";
9881004
case I915_MMAP_TYPE_WC: return "wc";
9891005
case I915_MMAP_TYPE_UC: return "uc";
1006+
case I915_MMAP_TYPE_FIXED: return "fixed";
9901007
default: return "unknown";
9911008
}
9921009
}
@@ -1100,6 +1117,8 @@ static int igt_mmap_access(void *arg)
11001117
err = __igt_mmap_access(i915, obj, I915_MMAP_TYPE_WC);
11011118
if (err == 0)
11021119
err = __igt_mmap_access(i915, obj, I915_MMAP_TYPE_UC);
1120+
if (err == 0)
1121+
err = __igt_mmap_access(i915, obj, I915_MMAP_TYPE_FIXED);
11031122

11041123
i915_gem_object_put(obj);
11051124
if (err)
@@ -1241,6 +1260,8 @@ static int igt_mmap_gpu(void *arg)
12411260
err = __igt_mmap_gpu(i915, obj, I915_MMAP_TYPE_GTT);
12421261
if (err == 0)
12431262
err = __igt_mmap_gpu(i915, obj, I915_MMAP_TYPE_WC);
1263+
if (err == 0)
1264+
err = __igt_mmap_gpu(i915, obj, I915_MMAP_TYPE_FIXED);
12441265

12451266
i915_gem_object_put(obj);
12461267
if (err)
@@ -1396,6 +1417,8 @@ static int igt_mmap_revoke(void *arg)
13961417
err = __igt_mmap_revoke(i915, obj, I915_MMAP_TYPE_GTT);
13971418
if (err == 0)
13981419
err = __igt_mmap_revoke(i915, obj, I915_MMAP_TYPE_WC);
1420+
if (err == 0)
1421+
err = __igt_mmap_revoke(i915, obj, I915_MMAP_TYPE_FIXED);
13991422

14001423
i915_gem_object_put(obj);
14011424
if (err)

include/uapi/drm/i915_drm.h

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -849,31 +849,56 @@ struct drm_i915_gem_mmap_gtt {
849849
__u64 offset;
850850
};
851851

852+
/**
853+
* struct drm_i915_gem_mmap_offset - Retrieve an offset so we can mmap this buffer object.
854+
*
855+
* This struct is passed as argument to the `DRM_IOCTL_I915_GEM_MMAP_OFFSET` ioctl,
856+
* and is used to retrieve the fake offset to mmap an object specified by &handle.
857+
*
858+
* The legacy way of using `DRM_IOCTL_I915_GEM_MMAP` is removed on gen12+.
859+
* `DRM_IOCTL_I915_GEM_MMAP_GTT` is an older supported alias to this struct, but will behave
860+
* as setting the &extensions to 0, and &flags to `I915_MMAP_OFFSET_GTT`.
861+
*/
852862
struct drm_i915_gem_mmap_offset {
853-
/** Handle for the object being mapped. */
863+
/** @handle: Handle for the object being mapped. */
854864
__u32 handle;
865+
/** @pad: Must be zero */
855866
__u32 pad;
856867
/**
857-
* Fake offset to use for subsequent mmap call
868+
* @offset: The fake offset to use for subsequent mmap call
858869
*
859870
* This is a fixed-size type for 32/64 compatibility.
860871
*/
861872
__u64 offset;
862873

863874
/**
864-
* Flags for extended behaviour.
875+
* @flags: Flags for extended behaviour.
865876
*
866-
* It is mandatory that one of the MMAP_OFFSET types
867-
* (GTT, WC, WB, UC, etc) should be included.
877+
* It is mandatory that one of the `MMAP_OFFSET` types
878+
* should be included:
879+
*
880+
* - `I915_MMAP_OFFSET_GTT`: Use mmap with the object bound to GTT. (Write-Combined)
881+
* - `I915_MMAP_OFFSET_WC`: Use Write-Combined caching.
882+
* - `I915_MMAP_OFFSET_WB`: Use Write-Back caching.
883+
* - `I915_MMAP_OFFSET_FIXED`: Use object placement to determine caching.
884+
*
885+
* On devices with local memory `I915_MMAP_OFFSET_FIXED` is the only valid
886+
* type. On devices without local memory, this caching mode is invalid.
887+
*
888+
* As caching mode when specifying `I915_MMAP_OFFSET_FIXED`, WC or WB will
889+
* be used, depending on the object placement on creation. WB will be used
890+
* when the object can only exist in system memory, WC otherwise.
868891
*/
869892
__u64 flags;
870-
#define I915_MMAP_OFFSET_GTT 0
871-
#define I915_MMAP_OFFSET_WC 1
872-
#define I915_MMAP_OFFSET_WB 2
873-
#define I915_MMAP_OFFSET_UC 3
874893

875-
/*
876-
* Zero-terminated chain of extensions.
894+
#define I915_MMAP_OFFSET_GTT 0
895+
#define I915_MMAP_OFFSET_WC 1
896+
#define I915_MMAP_OFFSET_WB 2
897+
#define I915_MMAP_OFFSET_UC 3
898+
#define I915_MMAP_OFFSET_FIXED 4
899+
900+
/**
901+
* @extensions: Zero-terminated chain of extensions.
877902
*
878903
* No current extensions defined; mbz.
879904
*/

0 commit comments

Comments
 (0)