[Intel-gfx] [PATCH i-g-t v5 1/4] lib/i915: Add minimum GTT alignment helper
Janusz Krzysztofik
janusz.krzysztofik at linux.intel.com
Mon Nov 4 17:13:27 UTC 2019
Some tests assume 4kB offset alignment while using softpin. That
assumption may be wrong on future GEM backends with possibly larger
minimum page sizes. As a result, those tests may either fail on
softpin at offsets which are incorrectly aligned, may silently skip
such incorrectly aligned addresses assuming them occupied by other
users, or may always succeed when examining invalid use patterns.
Provide a helper function that detects minimum GTT alignment. Tests
may use it to calculate softpin offsets valid on actually used backing
store.
Also expose a new object validation helper just created, it may be
useful for checking if a shared GTT address is not reserved, for
example.
v2: Rename the helper, use 'minimum GTT alignment' term across the
change (Chris),
- use error numbers to distinguish between invalid offsets and
addresses occupied by other users,
- simplify the code (Chris).
v3: Put the code under lib/i915/, not in lib/ioctl_wrappers.c (Chris),
- validate objects with an invalid reloc applied so execbuf requests
called only for validation purposes are actually not emitted to GPU
(Chris),
- move object validation code to a separate helper.
Signed-off-by: Janusz Krzysztofik <janusz.krzysztofik at linux.intel.com>
Cc: Chris Wilson <chris at chris-wilson.co.uk>
Cc: Daniele Ceraolo Spurio <daniele.ceraolospurio at intel.com>
Cc: Stuart Summers <stuart.summers at intel.com>
---
lib/Makefile.sources | 2 +
lib/i915/gem_gtt_topology.c | 118 ++++++++++++++++++++++++++++++++++++
lib/i915/gem_gtt_topology.h | 36 +++++++++++
lib/meson.build | 1 +
4 files changed, 157 insertions(+)
create mode 100644 lib/i915/gem_gtt_topology.c
create mode 100644 lib/i915/gem_gtt_topology.h
diff --git a/lib/Makefile.sources b/lib/Makefile.sources
index 34e0c012..975200fc 100644
--- a/lib/Makefile.sources
+++ b/lib/Makefile.sources
@@ -7,6 +7,8 @@ lib_source_list = \
i915/gem_context.h \
i915/gem_engine_topology.c \
i915/gem_engine_topology.h \
+ i915/gem_gtt_topology.c \
+ i915/gem_gtt_topology.h \
i915/gem_scheduler.c \
i915/gem_scheduler.h \
i915/gem_submission.c \
diff --git a/lib/i915/gem_gtt_topology.c b/lib/i915/gem_gtt_topology.c
new file mode 100644
index 00000000..427f0c14
--- /dev/null
+++ b/lib/i915/gem_gtt_topology.c
@@ -0,0 +1,118 @@
+/*
+ * Copyright © 2019 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "i915_drm.h"
+
+#include "igt_core.h"
+#include "intel_reg.h"
+#include "ioctl_wrappers.h"
+
+#include "i915/gem_gtt_topology.h"
+
+/**
+ * gem_gtt_validate_object:
+ * @fd: open i915 drm file descriptor
+ *
+ * This function verifies validity of GEM object attributes. For example,
+ * it is useful for validation of the object softpin offset.
+ *
+ * Returns:
+ * 0 on success, negative error code returned by GEM_EXECBUF IOCTL on failure
+ */
+
+int gem_gtt_validate_object(int fd, struct drm_i915_gem_exec_object2 *obj)
+{
+ struct drm_i915_gem_relocation_entry reloc;
+ struct drm_i915_gem_execbuffer2 execbuf;
+ const uint32_t bbe = MI_BATCH_BUFFER_END;
+ uintptr_t old_reloc;
+ int old_count, err;
+
+ memset(&reloc, 0, sizeof(reloc));
+ memset(&execbuf, 0, sizeof(execbuf));
+
+ /* use invalid reloc to save request emission */
+ old_reloc = obj->relocs_ptr;
+ old_count = obj->relocation_count;
+ obj->relocs_ptr = to_user_pointer(&reloc);
+ obj->relocation_count = 1;
+ gem_write(fd, obj->handle, 0, &bbe, sizeof(bbe));
+
+ execbuf.buffers_ptr = to_user_pointer(obj);
+ execbuf.buffer_count = 1;
+
+ err = __gem_execbuf(fd, &execbuf);
+ if (err == -ENOENT) {
+ /* suppress -ENOENT we get for invalid reloc handle */
+ err = 0;
+ }
+
+ obj->relocs_ptr = old_reloc;
+ obj->relocation_count = old_count;
+
+ return err;
+}
+
+/**
+ * gem_gtt_min_alignment_order:
+ * @fd: open i915 drm file descriptor
+ *
+ * This function detects the minimum possible alignment of a soft-pinned gem
+ * object allocated from a default backing store. It is useful for calculating
+ * correctly aligned softpin offsets.
+ * Since size order to size conversion (size = 1 << order) is less trivial
+ * than the opposite, the function returns the alignment order as more handy.
+ *
+ * Returns:
+ * Size order of the minimum GTT alignment
+ */
+
+int gem_gtt_min_alignment_order(int fd)
+{
+ struct drm_i915_gem_exec_object2 obj;
+ int order;
+
+ /* no softpin => 4kB page size */
+ if (!gem_has_softpin(fd))
+ return 12;
+
+ memset(&obj, 0, sizeof(obj));
+
+ obj.handle = gem_create(fd, 4096);
+ obj.flags = EXEC_OBJECT_PINNED | EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
+
+ for (order = 12; order < 64; order++) {
+ obj.offset = 1ull << order;
+ if (gem_gtt_validate_object(fd, &obj) != -EINVAL)
+ break;
+ }
+ igt_assert(obj.offset < gem_aperture_size(fd));
+
+ gem_close(fd, obj.handle);
+ igt_debug("minimum GTT alignment is %#llx\n", (long long)obj.offset);
+ return order;
+}
diff --git a/lib/i915/gem_gtt_topology.h b/lib/i915/gem_gtt_topology.h
new file mode 100644
index 00000000..59161c8a
--- /dev/null
+++ b/lib/i915/gem_gtt_topology.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2019 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Eric Anholt <eric at anholt.net>
+ * Daniel Vetter <daniel.vetter at ffwll.ch>
+ *
+ */
+
+#ifndef GEM_GTT_TOPOLOGY_H
+#define GEM_GTT_TOPOLOGY_H
+
+int gem_gtt_validate_object(int fd, struct drm_i915_gem_exec_object2 *obj);
+int gem_gtt_min_alignment_order(int fd);
+#define gem_gtt_min_alignment(fd) (1ull << gem_gtt_min_alignment_order(fd))
+
+#endif /* GEM_GTT_TOPOLOGY_H */
diff --git a/lib/meson.build b/lib/meson.build
index fbc0c8d1..125de1a1 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -2,6 +2,7 @@ lib_sources = [
'drmtest.c',
'i915/gem_context.c',
'i915/gem_engine_topology.c',
+ 'i915/gem_gtt_topology.c',
'i915/gem_scheduler.c',
'i915/gem_submission.c',
'i915/gem_ring.c',
--
2.21.0
More information about the Intel-gfx
mailing list