* [igt-dev] [PATCH i-g-t v4 2/4] lib/i915/gem_mman: add mmap_offset support
2019-11-22 7:48 [igt-dev] [PATCH i-g-t v4 0/4] Basic LMEM support in IGT Zbigniew Kempczyński
2019-11-22 7:48 ` [igt-dev] [PATCH i-g-t v4 1/4] include/drm-uapi/i915_drm: Add local defs for memory region uAPI Zbigniew Kempczyński
@ 2019-11-22 7:48 ` Zbigniew Kempczyński
2019-11-22 7:48 ` [igt-dev] [PATCH i-g-t v4 3/4] lib/i915/intel_memory_region: Add lib to manage memory regions Zbigniew Kempczyński
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Zbigniew Kempczyński @ 2019-11-22 7:48 UTC (permalink / raw)
To: igt-dev
From: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
With introduction of LMEM concept new IOCTL call were implemented
- gem_mmap_offset. This patch add support in IGT for it.
Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Signed-off-by: Antonio Argenziano <antonio.argenziano@intel.com>
Signed-off-by: Zbigniew Kempczyński <zbigniew.kempczynski@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>
---
lib/i915/gem_mman.c | 225 ++++++++++++++++++++++++++++++++++++--------
lib/i915/gem_mman.h | 18 +++-
2 files changed, 203 insertions(+), 40 deletions(-)
diff --git a/lib/i915/gem_mman.c b/lib/i915/gem_mman.c
index 6256627b..8d6db0d1 100644
--- a/lib/i915/gem_mman.c
+++ b/lib/i915/gem_mman.c
@@ -40,6 +40,33 @@
#define VG(x) do {} while (0)
#endif
+#define LOCAL_I915_PARAM_MMAP_OFFSET_VERSION 54
+
+static int gem_mmap_gtt_version(int fd)
+{
+ struct drm_i915_getparam gp;
+ int gtt_version = -1;
+
+ memset(&gp, 0, sizeof(gp));
+ gp.param = I915_PARAM_MMAP_GTT_VERSION;
+ gp.value = >t_version;
+ ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
+
+ return gtt_version;
+}
+
+static bool gem_has_mmap_offset(int fd)
+{
+ int gtt_version = gem_mmap_gtt_version(fd);
+
+ return gtt_version >= 4;
+}
+
+void gem_require_mmap_offset(int i915)
+{
+ igt_require(gem_has_mmap_offset(i915));
+}
+
/**
* __gem_mmap__gtt:
* @fd: open i915 drm file descriptor
@@ -101,46 +128,60 @@ int gem_munmap(void *ptr, uint64_t size)
return ret;
}
-bool gem_mmap__has_wc(int fd)
+bool __gem_mmap__has_wc(int fd)
{
- static int has_wc = -1;
-
- if (has_wc == -1) {
- struct drm_i915_getparam gp;
- int mmap_version = -1;
- int gtt_version = -1;
-
- has_wc = 0;
-
- memset(&gp, 0, sizeof(gp));
- gp.param = I915_PARAM_MMAP_GTT_VERSION;
- gp.value = >t_version;
- ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
-
- memset(&gp, 0, sizeof(gp));
- gp.param = I915_PARAM_MMAP_VERSION;
- gp.value = &mmap_version;
- ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
-
- /* Do we have the new mmap_ioctl with DOMAIN_WC? */
- if (mmap_version >= 1 && gtt_version >= 2) {
- struct drm_i915_gem_mmap arg;
-
- /* Does this device support wc-mmaps ? */
- memset(&arg, 0, sizeof(arg));
- arg.handle = gem_create(fd, 4096);
- arg.offset = 0;
- arg.size = 4096;
- arg.flags = I915_MMAP_WC;
- has_wc = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg) == 0;
- gem_close(fd, arg.handle);
- }
- errno = 0;
+ int has_wc = 0;
+
+ struct drm_i915_getparam gp;
+ int mmap_version = -1;
+
+ memset(&gp, 0, sizeof(gp));
+ gp.param = I915_PARAM_MMAP_VERSION;
+ gp.value = &mmap_version;
+ ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
+
+ /* Do we have the mmap_ioctl with DOMAIN_WC? */
+ if (mmap_version >= 1 && gem_mmap_gtt_version(fd) >= 2) {
+ struct drm_i915_gem_mmap arg;
+
+ /* Does this device support wc-mmaps ? */
+ memset(&arg, 0, sizeof(arg));
+ arg.handle = gem_create(fd, 4096);
+ arg.offset = 0;
+ arg.size = 4096;
+ arg.flags = I915_MMAP_WC;
+ has_wc = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg) == 0;
+ gem_close(fd, arg.handle);
}
+ errno = 0;
+
+ return has_wc > 0;
+}
+
+bool __gem_mmap_offset__has_wc(int fd)
+{
+ int has_wc = 0;
+ struct local_i915_gem_mmap_offset arg;
+
+ /* Does this device support wc-mmaps ? */
+ memset(&arg, 0, sizeof(arg));
+ arg.handle = gem_create(fd, 4096);
+ arg.offset = 0;
+ arg.flags = LOCAL_I915_MMAP_OFFSET_WC;
+ has_wc = igt_ioctl(fd, LOCAL_IOCTL_I915_GEM_MMAP_OFFSET,
+ &arg) == 0;
+ gem_close(fd, arg.handle);
+
+ errno = 0;
return has_wc > 0;
}
+bool gem_mmap__has_wc(int fd)
+{
+ return __gem_mmap_offset__has_wc(fd) || __gem_mmap__has_wc(fd);
+}
+
/**
* __gem_mmap:
* @fd: open i915 drm file descriptor
@@ -157,11 +198,13 @@ bool gem_mmap__has_wc(int fd)
*
* Returns: A pointer to the created memory mapping, NULL on failure.
*/
-static void
-*__gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned int prot, uint64_t flags)
+void *__gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size,
+ unsigned int prot, uint64_t flags)
{
struct drm_i915_gem_mmap arg;
+ igt_assert(offset == 0);
+
memset(&arg, 0, sizeof(arg));
arg.handle = handle;
arg.offset = offset;
@@ -177,6 +220,47 @@ static void
return from_user_pointer(arg.addr_ptr);
}
+/**
+ * __gem_mmap_offset:
+ * @fd: open i915 drm file descriptor
+ * @handle: gem buffer object handle
+ * @offset: offset in the gem buffer of the mmap arena
+ * @size: size of the mmap arena
+ * @prot: memory protection bits as used by mmap()
+ * @flags: flags used to determine caching
+ *
+ * Mmap the gem buffer memory on offset returned in GEM_MMAP_OFFSET ioctl.
+ * Offset argument passed in function call must be 0. In the future
+ * when driver will allow slice mapping of buffer object this restriction
+ * will be removed.
+ *
+ * Returns: A pointer to the created memory mapping, NULL on failure.
+ */
+void *__gem_mmap_offset(int fd, uint32_t handle, uint64_t offset, uint64_t size,
+ unsigned int prot, uint64_t flags)
+{
+ struct local_i915_gem_mmap_offset arg;
+ void *ptr;
+
+ igt_assert(offset == 0);
+
+ memset(&arg, 0, sizeof(arg));
+ arg.handle = handle;
+ arg.flags = flags;
+
+ if (igt_ioctl(fd, LOCAL_IOCTL_I915_GEM_MMAP_OFFSET, &arg))
+ return NULL;
+
+ ptr = mmap64(0, size, prot, MAP_SHARED, fd, arg.offset);
+
+ if (ptr == MAP_FAILED)
+ ptr = NULL;
+ else
+ errno = 0;
+
+ return ptr;
+}
+
/**
* __gem_mmap__wc:
* @fd: open i915 drm file descriptor
@@ -194,7 +278,12 @@ static void
*/
void *__gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
- return __gem_mmap(fd, handle, offset, size, prot, I915_MMAP_WC);
+ void *ptr = __gem_mmap_offset(fd, handle, offset, size, prot,
+ LOCAL_I915_MMAP_OFFSET_WC);
+ if (!ptr)
+ ptr = __gem_mmap(fd, handle, offset, size, prot, I915_MMAP_WC);
+
+ return ptr;
}
/**
@@ -205,14 +294,68 @@ void *__gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, un
* @size: size of the mmap arena
* @prot: memory protection bits as used by mmap()
*
- * Like __gem_mmap__wc() except we assert on failure.
+ * Try to __gem_mmap__wc(). Assert on failure.
*
* Returns: A pointer to the created memory mapping
*/
void *gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
void *ptr = __gem_mmap__wc(fd, handle, offset, size, prot);
+
+ igt_assert(ptr);
+ return ptr;
+}
+
+/**
+ * __gem_mmap__device_coherent:
+ * @fd: open i915 drm file descriptor
+ * @handle: gem buffer object handle
+ * @offset: offset in the gem buffer of the mmap arena
+ * @size: size of the mmap arena
+ * @prot: memory protection bits as used by mmap()
+ *
+ * Returns: A pointer to a block of linear device memory mapped into the
+ * process with WC semantics. When no WC is available try to mmap using GGTT.
+ */
+void *__gem_mmap__device_coherent(int fd, uint32_t handle, uint64_t offset,
+ uint64_t size, unsigned prot)
+{
+ void *ptr = __gem_mmap_offset(fd, handle, offset, size, prot,
+ LOCAL_I915_MMAP_OFFSET_WC);
+ if (!ptr)
+ ptr = __gem_mmap__wc(fd, handle, offset, size, prot);
+
+ if (!ptr)
+ ptr = __gem_mmap__gtt(fd, handle, size, prot);
+
+ return ptr;
+}
+
+/**
+ * gem_mmap__device_coherent:
+ * @fd: open i915 drm file descriptor
+ * @handle: gem buffer object handle
+ * @offset: offset in the gem buffer of the mmap arena
+ * @size: size of the mmap arena
+ * @prot: memory protection bits as used by mmap()
+ *
+ * Call __gem_mmap__device__coherent(), asserts on fail.
+ * Offset argument passed in function call must be 0. In the future
+ * when driver will allow slice mapping of buffer object this restriction
+ * will be removed.
+ *
+ * Returns: A pointer to the created memory mapping.
+ */
+void *gem_mmap__device_coherent(int fd, uint32_t handle, uint64_t offset,
+ uint64_t size, unsigned prot)
+{
+ void *ptr;
+
+ igt_assert(offset == 0);
+
+ ptr = gem_mmap__device_coherent(fd, handle, offset, size, prot);
igt_assert(ptr);
+
return ptr;
}
@@ -248,7 +391,11 @@ void *__gem_mmap__cpu(int fd, uint32_t handle, uint64_t offset, uint64_t size, u
*/
void *gem_mmap__cpu(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
- void *ptr = __gem_mmap__cpu(fd, handle, offset, size, prot);
+ void *ptr = __gem_mmap_offset(fd, handle, offset, size, prot,
+ LOCAL_I915_MMAP_OFFSET_WB);
+ if (!ptr)
+ ptr = __gem_mmap__cpu(fd, handle, offset, size, prot);
+
igt_assert(ptr);
return ptr;
}
diff --git a/lib/i915/gem_mman.h b/lib/i915/gem_mman.h
index 096ff592..fe1299c0 100644
--- a/lib/i915/gem_mman.h
+++ b/lib/i915/gem_mman.h
@@ -25,12 +25,22 @@
#ifndef GEM_MMAN_H
#define GEM_MMAN_H
+#define LOCAL_I915_GEM_MMAP_OFFSET DRM_I915_GEM_MMAP_GTT
+#define LOCAL_IOCTL_I915_GEM_MMAP_OFFSET DRM_IOWR(DRM_COMMAND_BASE + \
+ LOCAL_I915_GEM_MMAP_OFFSET, struct local_i915_gem_mmap_offset)
+
+void gem_require_mmap_offset(int i915);
+bool gem_mmap_has_gtt(int fd);
+
void *gem_mmap__gtt(int fd, uint32_t handle, uint64_t size, unsigned prot);
void *gem_mmap__cpu(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot);
+bool __gem_mmap__has_wc(int fd);
+bool __gem_mmap_offset__has_wc(int fd);
bool gem_mmap__has_wc(int fd);
void *gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot);
-
+void *gem_mmap__device_coherent(int fd, uint32_t handle, uint64_t offset,
+ uint64_t size, unsigned prot);
#ifndef I915_GEM_DOMAIN_WC
#define I915_GEM_DOMAIN_WC 0x80
#endif
@@ -38,9 +48,15 @@ void *gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsi
bool gem_has_mappable_ggtt(int i915);
void gem_require_mappable_ggtt(int i915);
+void *__gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size,
+ unsigned int prot, uint64_t flags);
+void *__gem_mmap_offset(int fd, uint32_t handle, uint64_t offset, uint64_t size,
+ unsigned int prot, uint64_t flags);
void *__gem_mmap__gtt(int fd, uint32_t handle, uint64_t size, unsigned prot);
void *__gem_mmap__cpu(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot);
void *__gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot);
+void *__gem_mmap__device_coherent(int fd, uint32_t handle, uint64_t offset,
+ uint64_t size, unsigned prot);
int gem_munmap(void *ptr, uint64_t size);
--
2.23.0
_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [igt-dev] [PATCH i-g-t v4 3/4] lib/i915/intel_memory_region: Add lib to manage memory regions
2019-11-22 7:48 [igt-dev] [PATCH i-g-t v4 0/4] Basic LMEM support in IGT Zbigniew Kempczyński
2019-11-22 7:48 ` [igt-dev] [PATCH i-g-t v4 1/4] include/drm-uapi/i915_drm: Add local defs for memory region uAPI Zbigniew Kempczyński
2019-11-22 7:48 ` [igt-dev] [PATCH i-g-t v4 2/4] lib/i915/gem_mman: add mmap_offset support Zbigniew Kempczyński
@ 2019-11-22 7:48 ` Zbigniew Kempczyński
2019-11-22 7:48 ` [igt-dev] [PATCH i-g-t v4 4/4] tests/i915/gem_mmap_offset: Add new API test for gem_mmap_offset Zbigniew Kempczyński
2019-11-22 8:29 ` [igt-dev] ✗ Fi.CI.BAT: failure for Basic LMEM support in IGT (rev4) Patchwork
4 siblings, 0 replies; 9+ messages in thread
From: Zbigniew Kempczyński @ 2019-11-22 7:48 UTC (permalink / raw)
To: igt-dev
From: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
LMEM series introduced concept of memory_regions. This patch implement
helper functions that allow user to manage them in more convenient way.
v2: when driver doesn't support i915_query supports memory region
switch to fallback and probe system and device memory regions.
Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Signed-off-by: Zbigniew Kempczyński <lukasz.kalamarz@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>
---
lib/Makefile.sources | 2 +
lib/i915/intel_memory_region.c | 370 +++++++++++++++++++++++++++++++++
lib/i915/intel_memory_region.h | 88 ++++++++
lib/ioctl_wrappers.h | 1 +
lib/meson.build | 1 +
5 files changed, 462 insertions(+)
create mode 100644 lib/i915/intel_memory_region.c
create mode 100644 lib/i915/intel_memory_region.h
diff --git a/lib/Makefile.sources b/lib/Makefile.sources
index 9d1a4e06..87b9f146 100644
--- a/lib/Makefile.sources
+++ b/lib/Makefile.sources
@@ -17,6 +17,8 @@ lib_source_list = \
i915/gem_mman.h \
i915/gem_vm.c \
i915/gem_vm.h \
+ i915/intel_memory_region.c \
+ i915/intel_memory_region.h \
i915_3d.h \
i915_reg.h \
i915_pciids.h \
diff --git a/lib/i915/intel_memory_region.c b/lib/i915/intel_memory_region.c
new file mode 100644
index 00000000..3e33b8e7
--- /dev/null
+++ b/lib/i915/intel_memory_region.c
@@ -0,0 +1,370 @@
+/*
+ * 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 <signal.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <stdarg.h>
+#include <alloca.h>
+
+#include "intel_reg.h"
+#include "drmtest.h"
+#include "ioctl_wrappers.h"
+#include "igt_dummyload.h"
+#include "igt_gt.h"
+#include "intel_chipset.h"
+
+#include "i915/intel_memory_region.h"
+
+#define SZ_4K (4096)
+#define SZ_64K (65536)
+
+static int __i915_query(int fd, struct drm_i915_query *q)
+{
+ if (igt_ioctl(fd, DRM_IOCTL_I915_QUERY, q))
+ return -errno;
+
+ return 0;
+}
+
+static int __i915_query_item(int fd, struct drm_i915_query_item *item)
+{
+ struct drm_i915_query q = {
+ .num_items = 1,
+ .items_ptr = to_user_pointer(item),
+ };
+
+ return __i915_query(fd, &q);
+}
+
+bool gem_has_query_support(int fd)
+{
+ struct drm_i915_query query = {};
+
+ return __i915_query(fd, &query) == 0;
+}
+
+const struct intel_memory_region intel_memory_regions[] = {
+ { "SMEM", LOCAL_I915_SYSTEM_MEMORY, 0 },
+ { "LMEM", LOCAL_I915_DEVICE_MEMORY, 0 },
+ { "STLN", LOCAL_I915_STOLEN_MEMORY, 0 },
+ { NULL, 0, 0 },
+};
+
+const char *memory_region_name(uint32_t region) {
+ uint32_t type;
+
+ type = MEMORY_TYPE_FROM_REGION(region);
+
+ return intel_memory_regions[type].region_name;
+}
+/**
+ * gem_get_batch_size:
+ * @fd: open i915 drm file descriptor
+ * @region: region in which we want to create a batch
+ *
+ * FIXME: Currently function assumes we have 64K on DEVICE and 4K
+ * on SYSTEM memory. To be fixed after memory region page size detection
+ * patch will be merged.
+ */
+uint32_t gem_get_batch_size(int fd, uint32_t region)
+{
+ return IS_DEVICE_MEMORY_REGION(region) ? SZ_64K : SZ_4K;
+}
+
+static bool __try_gem_create_in_region(int fd, uint32_t region)
+{
+ uint32_t size, handle;
+ int ret;
+
+ size = gem_get_batch_size(fd, region);
+ handle = gem_create(fd, size);
+ ret = gem_migrate_to_memory_region(fd, handle, region);
+
+ gem_close(fd, handle);
+
+ return !ret;
+}
+
+static struct local_i915_query_memory_region_info *__probe_regions(int fd)
+{
+ struct local_i915_query_memory_region_info *query_info;
+ uint32_t length = sizeof(*query_info);
+ uint32_t *regions = NULL;
+ int max_regions = INTEL_MEMORY_TYPE_SHIFT;
+ int num_regions = 0;
+
+ for (int i = 0; intel_memory_regions[i].region_name; i++) {
+ uint8_t type;
+
+ /* We're not interested of other memory region types */
+ type = intel_memory_regions[i].memory_type;
+ if (type != LOCAL_I915_SYSTEM_MEMORY &&
+ type != LOCAL_I915_DEVICE_MEMORY)
+ continue;
+
+ for (int instance = 0; instance <= max_regions; instance++) {
+ uint32_t region;
+ int exists;
+
+ region = INTEL_MEMORY_REGION_ID(type, instance);
+ igt_debug("Probing memory region: %08x\n", region);
+ exists = __try_gem_create_in_region(fd, region);
+ if (!exists)
+ break;
+
+ igt_debug("Region %08x exists\n", region);
+ regions = realloc(regions, (num_regions + 1) *
+ sizeof(uint32_t));
+ igt_assert(regions);
+ regions[num_regions++] = region;
+ }
+ }
+
+ if (!num_regions)
+ return NULL;
+
+ length += sizeof(struct local_i915_memory_region_info) * num_regions;
+ query_info = calloc(1, length);
+ igt_assert(query_info);
+
+ /* Prepare query_info structure with regions id filled */
+ for (int i = 0; i < num_regions; i++) {
+ query_info->regions[i].id = regions[i];
+ query_info->regions[i].size = 0;
+ }
+ free(regions);
+ query_info->num_regions = num_regions;
+
+ return query_info;
+}
+
+/**
+ * gem_query_memory_regions:
+ * @fd: open i915 drm file descriptor
+ *
+ * This function wraps query mechanism for memory regions. If there's
+ * no i915_query probe memory regions and return same structure as from query.
+ *
+ * Returns: Filled struct with available memory regions. Allocates structure
+ * which must by freed by caller.
+ */
+struct local_i915_query_memory_region_info *gem_query_memory_regions(int fd)
+{
+ struct drm_i915_query_item item;
+ struct local_i915_query_memory_region_info *query_info;
+ int ret;
+
+ memset(&item, 0, sizeof(item));
+ item.query_id = LOCAL_I915_QUERY_MEMREGION_INFO;
+
+ ret = __i915_query_item(fd, &item);
+ ret = 1;
+ if (!ret) {
+ query_info = calloc(1, item.length);
+ igt_assert(query_info);
+
+ item.data_ptr = to_user_pointer(query_info);
+ __i915_query_item(fd, &item);
+
+ return query_info;
+ }
+
+ /* Fallback, there's no memregion i915_query supported. Try probe(). */
+ query_info = __probe_regions(fd);
+ igt_assert(query_info);
+
+ return query_info;
+}
+
+/**
+ * gem_get_lmem_region_count:
+ * @fd: open i915 drm file descriptor
+ *
+ * Helper function to check how many lmem regions are available on device.
+ *
+ * Returns: Number of found lmem regions.
+ */
+uint8_t gem_get_lmem_region_count(int fd)
+{
+ struct local_i915_query_memory_region_info *query_info;
+ uint8_t num_regions;
+ uint8_t lmem_regions = 0;
+
+ query_info = gem_query_memory_regions(fd);
+ num_regions = query_info->num_regions;
+
+ for (int i = 0; i < num_regions; i++) {
+ if (IS_DEVICE_MEMORY_REGION(query_info->regions[i].id))
+ lmem_regions += 1;
+ }
+ free(query_info);
+
+ return lmem_regions;
+}
+
+/**
+ * gem_has_lmem:
+ * @fd: open i915 drm file descriptor
+ *
+ * Helper function to check if lmem is available on device.
+ *
+ * Returns: True if at least one lmem region was found.
+ */
+bool gem_has_lmem(int fd)
+{
+ return gem_get_lmem_region_count(fd) > 0;
+}
+
+/**
+ * gem_query_has_memory_region:
+ * @query_info: query result of memory regions
+ * @region: region existance to check inside @query_info regions
+ *
+ * This function check existence of region in @query_info
+ *
+ * Returns: true if memory region was found. Otherwise false.
+ */
+bool gem_query_has_memory_region(struct local_i915_query_memory_region_info *query_info,
+ uint32_t region)
+{
+ for (int i = 0; i < query_info->num_regions; i++)
+ if (query_info->regions[i].id == region)
+ return true;
+
+ return false;
+}
+
+/**
+ * gem_query_require_region:
+ * @query_info: query result of memory regions
+ * @region: region to check inside query
+ *
+ * Function lead to skipping test if @region doesn't exists in @query_info.
+ */
+void gem_query_require_region(struct local_i915_query_memory_region_info *query_info,
+ uint32_t region)
+{
+ igt_require(gem_query_has_memory_region(query_info, region));
+}
+
+/**
+ * __gem_migrate_to_memory_regions:
+ * @fd: open i915 drm file descriptor
+ * @handle: buffer object handle
+ * @mem_regions: memory regions id array
+ * @size: memory regions array size
+ *
+ * Wrapper function on IOCTL_I915_GEM_OBJECT_SETPARAM. It sets object to be
+ * migrated into one of memory region specified in the array. Array contains
+ * memory regions in requested priority order - if no migration to first
+ * memory region is possible next one is selected and so on.
+ *
+ * Returns: errno
+ */
+static int __gem_migrate_to_memory_regions(int fd, uint32_t handle,
+ uint32_t *mem_regions,
+ uint32_t size)
+{
+ struct local_i915_gem_object_param obj;
+ int err = 0;
+
+ memset(&obj, 0, sizeof(obj));
+ obj.handle = handle;
+ obj.size = size;
+ obj.param = I915_PARAM_MEMORY_REGION | I915_OBJECT_PARAM;
+ obj.data = to_user_pointer(mem_regions);
+
+ if (igt_ioctl(fd, LOCAL_IOCTL_I915_GEM_OBJECT_SETPARAM, &obj)) {
+ err = -errno;
+ errno = 0;
+ }
+
+ return err;
+}
+
+/**
+ * gem_migrate_to_memory_region:
+ * @fd: open i915 drm file descriptor
+ * @handle: handle to GEM bo
+ * @type: memory region type
+ * @instance: memory region instance
+ *
+ * This function wraps GEM_OBJECT_SETPARAM into user friendly version. Object
+ * which user pass @handle will be migrated to memory region, specified
+ * by @type and @instance.
+ *
+ * Returns: errno if error occurred.
+ */
+int gem_migrate_to_memory_region(int fd, uint32_t handle, uint32_t region)
+{
+ return __gem_migrate_to_memory_regions(fd, handle, ®ion, 1);
+}
+
+/**
+ * gem_migrate_to_lmem:
+ * @fd: open i915 drm file descriptor
+ * @handle: handle to object that user want to migrate to LMEM
+ *
+ * This function wraps GEM_OBJECT_SETPARAM into user friendly version. Object
+ * which user pass @handle will be migrated to LMEM.
+ *
+ * Returns: errno if error occurred.
+ */
+int gem_migrate_to_lmem(int fd, uint32_t handle)
+{
+ return gem_migrate_to_memory_region(fd, handle, REGION_DEVICE_MEMORY(0));
+}
+
+/**
+ * gem_migrate_to_smem:
+ * @fd: open i915 drm file descriptor
+ * @handle: handle to object that user want to migrate to SMEM
+ *
+ * This function wraps GEM_OBJECT_SETPARAM into user friendly version. Object
+ * onto which user pass @handle will be migrated to SMEM.
+ *
+ * Returns: errno if error occurred.
+ */
+int gem_migrate_to_smem(int fd, uint32_t handle)
+{
+ return gem_migrate_to_memory_region(fd, handle, REGION_SYSTEM_MEMORY(0));
+}
+
+/* gem_create_in_memory_region_list:
+ * @fd: opened i915 drm file descriptor
+ * @size: requested size of the buffer
+ * @mem_regions: memory regions array (priority list)
+ * @num_regions: @mem_regions length
+ */
+uint32_t gem_create_in_memory_region_list(int fd, uint64_t size,
+ uint32_t *mem_regions,
+ int num_regions)
+{
+ uint32_t handle = gem_create(fd, size);
+ int ret = __gem_migrate_to_memory_regions(fd, handle,
+ mem_regions, num_regions);
+ igt_assert_eq(ret, 0);
+
+ return handle;
+}
diff --git a/lib/i915/intel_memory_region.h b/lib/i915/intel_memory_region.h
new file mode 100644
index 00000000..5e453a13
--- /dev/null
+++ b/lib/i915/intel_memory_region.h
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+
+#ifndef INTEL_MEMORY_REGION_H
+#define INTEL_MEMORY_REGION_H
+
+#include <stdint.h>
+#include "igt_aux.h"
+#include "i915_drm.h"
+
+#define INTEL_MEMORY_TYPE_SHIFT 16
+
+#define INTEL_MEMORY_REGION_ID(type, instance) \
+ ((BIT((type) + INTEL_MEMORY_TYPE_SHIFT)) | BIT(instance))
+
+#define MEMORY_TYPE_FROM_REGION(r) (igt_fls(r >> INTEL_MEMORY_TYPE_SHIFT) - 1)
+#define MEMORY_INSTANCE_FROM_REGION(r) (igt_fls(r & 0xffff) - 1)
+
+#define IS_DEVICE_MEMORY_REGION(region) \
+ (MEMORY_TYPE_FROM_REGION(region) == LOCAL_I915_DEVICE_MEMORY)
+#define IS_SYSTEM_MEMORY_REGION(region) \
+ (MEMORY_TYPE_FROM_REGION(region) == LOCAL_I915_SYSTEM_MEMORY)
+#define IS_STOLEN_MEMORY_REGION(region) \
+ (MEMORY_TYPE_FROM_REGION(region) == LOCAL_I915_STOLEN_MEMORY)
+
+/* Region macros for migration */
+#define REGION_SYSTEM_MEMORY(n) INTEL_MEMORY_REGION_ID(LOCAL_I915_SYSTEM_MEMORY, n)
+#define REGION_DEVICE_MEMORY(n) INTEL_MEMORY_REGION_ID(LOCAL_I915_DEVICE_MEMORY, n)
+
+extern const struct intel_memory_region {
+ const char *region_name;
+ uint8_t memory_type;
+ uint8_t memory_instance;
+} intel_memory_regions[];
+
+bool gem_has_query_support(int fd);
+const char *memory_region_name(uint32_t region);
+
+uint32_t gem_get_batch_size(int fd, uint32_t region);
+
+struct local_i915_query_memory_region_info *gem_query_memory_regions(int fd);
+
+uint8_t gem_get_lmem_region_count(int fd);
+
+bool gem_has_lmem(int fd);
+bool gem_query_has_memory_region(struct local_i915_query_memory_region_info *query_info,
+ uint32_t region);
+void gem_query_require_region(struct local_i915_query_memory_region_info *query_info,
+ uint32_t region);
+int gem_migrate_to_memory_region(int fd, uint32_t handle, uint32_t region);
+int gem_migrate_to_lmem(int fd, uint32_t handle);
+int gem_migrate_to_smem(int fd, uint32_t handle);
+
+uint32_t gem_create_in_memory_region_list(int fd, uint64_t size,
+ uint32_t *mem_regions,
+ int num_regions);
+#define gem_create_in_memory_regions(fd, size, regions...) ({ \
+ unsigned int arr__[] = { regions }; \
+ gem_create_in_memory_region_list(fd, size, arr__, ARRAY_SIZE(arr__)); \
+})
+
+#define for_each_memory_region(query_info__, region__) \
+ for (int __r = 0; \
+ region__ = &(query_info__->regions[__r]), \
+ __r < query_info__->num_regions; \
+ __r++)
+
+#endif /* INTEL_MEMORY_REGION_H */
diff --git a/lib/ioctl_wrappers.h b/lib/ioctl_wrappers.h
index f2412d78..885cbb06 100644
--- a/lib/ioctl_wrappers.h
+++ b/lib/ioctl_wrappers.h
@@ -38,6 +38,7 @@
#include "i915/gem_context.h"
#include "i915/gem_scheduler.h"
+#include "i915/intel_memory_region.h"
/**
* igt_ioctl:
diff --git a/lib/meson.build b/lib/meson.build
index 3f908912..7a155ae7 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -7,6 +7,7 @@ lib_sources = [
'i915/gem_ring.c',
'i915/gem_mman.c',
'i915/gem_vm.c',
+ 'i915/intel_memory_region.c',
'igt_color_encoding.c',
'igt_debugfs.c',
'igt_device.c',
--
2.23.0
_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [igt-dev] [PATCH i-g-t v4 4/4] tests/i915/gem_mmap_offset: Add new API test for gem_mmap_offset
2019-11-22 7:48 [igt-dev] [PATCH i-g-t v4 0/4] Basic LMEM support in IGT Zbigniew Kempczyński
` (2 preceding siblings ...)
2019-11-22 7:48 ` [igt-dev] [PATCH i-g-t v4 3/4] lib/i915/intel_memory_region: Add lib to manage memory regions Zbigniew Kempczyński
@ 2019-11-22 7:48 ` Zbigniew Kempczyński
2019-11-22 8:07 ` Chris Wilson
2019-11-22 8:29 ` [igt-dev] ✗ Fi.CI.BAT: failure for Basic LMEM support in IGT (rev4) Patchwork
4 siblings, 1 reply; 9+ messages in thread
From: Zbigniew Kempczyński @ 2019-11-22 7:48 UTC (permalink / raw)
To: igt-dev
From: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Few simple tests which tries to create / mmap buffer objects
in system / device memory regions.
Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Signed-off-by: Zbigniew Kempczyński <zbigniew.kempczynski@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>
---
tests/Makefile.sources | 3 +
tests/i915/gem_mmap_offset.c | 163 +++++++++++++++++++++++++++++++++++
tests/meson.build | 1 +
3 files changed, 167 insertions(+)
create mode 100644 tests/i915/gem_mmap_offset.c
diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index 27801c89..9c6c3933 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -320,6 +320,9 @@ gem_mmap_SOURCES = i915/gem_mmap.c
TESTS_progs += gem_mmap_gtt
gem_mmap_gtt_SOURCES = i915/gem_mmap_gtt.c
+TESTS_progs += gem_mmap_offset
+gem_mmap_offset_SOURCES = i915/gem_mmap_offset.c
+
TESTS_progs += gem_mmap_offset_exhaustion
gem_mmap_offset_exhaustion_SOURCES = i915/gem_mmap_offset_exhaustion.c
diff --git a/tests/i915/gem_mmap_offset.c b/tests/i915/gem_mmap_offset.c
new file mode 100644
index 00000000..a4432f93
--- /dev/null
+++ b/tests/i915/gem_mmap_offset.c
@@ -0,0 +1,163 @@
+/*
+ * 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 "igt.h"
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include "drm.h"
+
+IGT_TEST_DESCRIPTION("Basic MMAP_OFFSET IOCTL tests for mem regions\n");
+
+static int mmap_offset_ioctl(int fd, struct local_i915_gem_mmap_offset *arg)
+{
+ int err = 0;
+
+ if (igt_ioctl(fd, LOCAL_IOCTL_I915_GEM_MMAP_OFFSET, arg))
+ err = -errno;
+
+ errno = 0;
+ return err;
+}
+
+static void
+mmap_offset_bad_object(int fd, struct local_i915_query_memory_region_info *regions)
+{
+ const char *region_name;
+ struct local_i915_memory_region_info *mem_region;
+ uint32_t region, obj_size, batch_size, real_handle;
+ uint32_t handles[20];
+
+ for_each_memory_region(regions, mem_region) {
+ int i = 0;
+
+ region = mem_region->id;
+ region_name = memory_region_name(region);
+ if (IS_STOLEN_MEMORY_REGION(region)) {
+ continue;
+ }
+ igt_debug("mmap-offset-bad-object: region name: %s, "
+ "region: %08x\n", region_name, region);
+
+ batch_size = gem_get_batch_size(fd, region);
+ obj_size = 4 * batch_size;
+ real_handle = gem_create_in_memory_regions(fd, obj_size,
+ region);
+ handles[i++] = 0xdeadbeef;
+ for (int bit = 0; bit < 16; bit++)
+ handles[i++] = real_handle | (1 << (bit + 16));
+ handles[i] = real_handle + 1;
+
+ for (; i >= 0; i--) {
+ struct local_i915_gem_mmap_offset arg = {
+ .handle = handles[i],
+ .flags = LOCAL_I915_MMAP_OFFSET_WC,
+ };
+
+ igt_debug("Trying MMAP IOCTL with handle %x\n",
+ handles[i]);
+ igt_assert_eq(mmap_offset_ioctl(fd, &arg),
+ -ENOENT);
+ }
+
+ gem_close(fd, real_handle);
+ }
+}
+
+static void
+mmap_offset_basic(int fd, struct local_i915_query_memory_region_info *regions)
+{
+ const char *region_name;
+ struct local_i915_memory_region_info *mem_region;
+ uint64_t flags;
+ uint32_t region, handle, obj_size;
+ uint8_t *expected, *buf, *addr;
+
+ for_each_memory_region(regions, mem_region) {
+ region = mem_region->id;
+ region_name = memory_region_name(region);
+ if (IS_STOLEN_MEMORY_REGION(region)) {
+ continue;
+ }
+ igt_debug("mmap-offset-basic: region name: %s, region: %08x\n",
+ region_name, region);
+
+ obj_size = 4 * gem_get_batch_size(fd, region);
+ handle = gem_create_in_memory_regions(fd, obj_size,
+ region);
+ flags = LOCAL_I915_MMAP_OFFSET_WC;
+ addr = __gem_mmap_offset(fd, handle, 0, obj_size,
+ PROT_READ | PROT_WRITE,
+ flags);
+ igt_assert(addr);
+
+ igt_debug("Testing contents of newly created object.\n");
+ expected = calloc(obj_size, sizeof(*expected));
+ igt_assert_eq(memcmp(addr, expected, obj_size), 0);
+ free(expected);
+
+ igt_debug("Testing coherency of writes and mmap reads.\n");
+ buf = calloc(obj_size, sizeof(*buf));
+ memset(buf + 1024, 0x01, 1024);
+ gem_write(fd, handle, 0, buf, obj_size);
+ igt_assert_eq(memcmp(buf, addr, obj_size), 0);
+
+ igt_debug("Testing that mapping stays after close\n");
+ gem_close(fd, handle);
+ igt_assert_eq(memcmp(buf, addr, obj_size), 0);
+ free(buf);
+
+ igt_debug("Testing unmapping\n");
+ munmap(addr, obj_size);
+ }
+}
+
+igt_main
+{
+ int fd;
+ struct local_i915_query_memory_region_info *regions;
+
+ igt_fixture {
+ fd = drm_open_driver(DRIVER_INTEL);
+ gem_require_mmap_offset(fd);
+ igt_require(gem_mmap__has_wc(fd));
+
+ regions = gem_query_memory_regions(fd);
+ igt_assert(regions);
+ }
+
+ igt_describe("Verify mapping to invalid gem objects won't be created"
+ " in different memory regions");
+ igt_subtest_f("mmap-offset-bad-object")
+ mmap_offset_bad_object(fd, regions);
+
+ igt_describe("Check buffer object mapping is coherent with "
+ "data written by gem_write in all memory regions");
+ igt_subtest_f("mmap-offset-basic")
+ mmap_offset_basic(fd, regions);
+
+ igt_fixture {
+ free(regions);
+ close(fd);
+ }
+}
diff --git a/tests/meson.build b/tests/meson.build
index 755fc9e6..644b5504 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -174,6 +174,7 @@ i915_progs = [
'gem_media_vme',
'gem_mmap',
'gem_mmap_gtt',
+ 'gem_mmap_offset',
'gem_mmap_offset_exhaustion',
'gem_mmap_wc',
'gem_partial_pwrite_pread',
--
2.23.0
_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev
^ permalink raw reply related [flat|nested] 9+ messages in thread