All of lore.kernel.org
 help / color / mirror / Atom feed
From: Matthew Auld <matthew.auld@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>,
	thomas.hellstrom@linux.intel.com,
	Michal Winiarski <michal.winiarski@intel.com>,
	Janulgue Abdiel <abdiel.janulgue@intel.com>,
	intel-gfx@lists.freedesktop.org,
	Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Subject: [Intel-gfx] [PATCH i-g-t 09/12] lib/i915/intel_memory_region/dg1: Add new lib to query memory region
Date: Tue, 11 May 2021 17:51:14 +0100	[thread overview]
Message-ID: <20210511165117.428062-10-matthew.auld@intel.com> (raw)
In-Reply-To: <20210511165117.428062-1-matthew.auld@intel.com>

From: Lukasz Kalamarz <lukasz.kalamarz@intel.com>

With an introduction of Local Memory concept we should be able to
allocate object in specific memory region. This patch implements
helper functions that allow this.

Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Cc: Janulgue Abdiel <abdiel.janulgue@intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Michal Winiarski <michal.winiarski@intel.com>
Cc: Katarzyna Dec <katarzyna.dec@intel.com>
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>
Cc: Stuart Summers <stuart.summers@intel.com>
---
 lib/i915/intel_memory_region.c | 130 ++++++++++++++++++++-------------
 lib/i915/intel_memory_region.h |   5 ++
 2 files changed, 86 insertions(+), 49 deletions(-)

diff --git a/lib/i915/intel_memory_region.c b/lib/i915/intel_memory_region.c
index 412367e4..91d3ef18 100644
--- a/lib/i915/intel_memory_region.c
+++ b/lib/i915/intel_memory_region.c
@@ -27,7 +27,6 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/sysinfo.h>
 #include <fcntl.h>
 
 #include "i915/gem_create.h"
@@ -41,10 +40,42 @@
 #include "intel_chipset.h"
 #include "igt_collection.h"
 #include "igt_device.h"
-#include "igt_aux.h"
 
 #include "i915/intel_memory_region.h"
 
+#define i915_query_items(fd, items, n_items) do { \
+		igt_assert_eq(__i915_query_items(fd, items, n_items), 0); \
+		errno = 0; \
+	} while (0)
+#define i915_query_items_err(fd, items, n_items, err) do { \
+		igt_assert_eq(__i915_query_items(fd, items, n_items), -err); \
+	} while (0)
+
+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_items(int fd, struct drm_i915_query_item *items, uint32_t n_items)
+{
+	struct drm_i915_query q = {
+		.num_items = n_items,
+		.items_ptr = to_user_pointer(items),
+	};
+	return __i915_query(fd, &q);
+}
+
+bool gem_has_query_support(int fd)
+{
+	struct drm_i915_query query = {};
+
+	return __i915_query(fd, &query) == 0;
+}
+
 const char *get_memory_region_name(uint32_t region)
 {
 	uint16_t class = MEMORY_TYPE_FROM_REGION(region);
@@ -59,76 +90,77 @@ const char *get_memory_region_name(uint32_t region)
  *  gem_get_batch_size:
  *  @fd: open i915 drm file descriptor
  *  @mem_region_type: used memory_region type
+ *
+ *  With introduction of LMEM we observe different page sizes for those two
+ *  memory regions. Without this helper funtion we may be prone to forget
+ *  about setting proper page size.
  */
 uint32_t gem_get_batch_size(int fd, uint8_t mem_region_type)
 {
-	/* temporary solution, to be erased later */
-	(void) fd;
-	(void) mem_region_type;
-
-	return 4096;
+	return (mem_region_type == I915_MEMORY_CLASS_DEVICE) ? 65536 : 4096;
 }
 
-static uint64_t __get_meminfo(const char *info, const char *tag)
+/**
+ * gem_get_query_memory_regions:
+ * @fd: open i915 drm file descriptor
+ *
+ * This function wraps query mechanism for memory regions.
+ *
+ * Returns: Filled struct with available memory regions.
+ */
+struct drm_i915_query_memory_regions *gem_get_query_memory_regions(int fd)
 {
-	const char *str;
-	unsigned long val;
+	struct drm_i915_query_item item;
+	struct drm_i915_query_memory_regions *query_info;
 
-	str = strstr(info, tag);
-	if (str && sscanf(str + strlen(tag), " %lu", &val) == 1)
-		return (uint64_t)val << 10;
+	memset(&item, 0, sizeof(item));
+	item.query_id = DRM_I915_QUERY_MEMORY_REGIONS;
+	i915_query_items(fd, &item, 1);
 
-	igt_warn("Unrecognized /proc/meminfo field: '%s'\n", tag);
-	return 0;
-}
+	query_info = calloc(1, item.length);
 
-static uint64_t __get_available_smem(int fd)
-{
-	uint64_t retval;
-	char *info;
-	int proc_fd;
+	item.data_ptr = to_user_pointer(query_info);
+	i915_query_items(fd, &item, 1);
 
-	intel_purge_vm_caches(fd);
+	return query_info;
+}
 
-	proc_fd = open("/proc", O_RDONLY);
-	info = igt_sysfs_get(proc_fd, "meminfo");
-	close(proc_fd);
+/**
+ * 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 drm_i915_query_memory_regions *query_info;
+	uint8_t num_regions;
+	uint8_t lmem_regions = 0;
 
-	if (info) {
-		retval = __get_meminfo(info, "MemAvailable:");
-	} else {
-		struct sysinfo sysinf;
+	query_info = gem_get_query_memory_regions(fd);
+	num_regions = query_info->num_regions;
 
-		igt_assert(sysinfo(&sysinf) == 0);
-		retval = sysinf.freeram;
-		retval *= sysinf.mem_unit;
+	for (int i = 0; i < num_regions; i++) {
+		if (query_info->regions[i].region.memory_class == I915_MEMORY_CLASS_DEVICE)
+			lmem_regions += 1;
 	}
 
-	return retval;
+	return lmem_regions;
 }
 
 /**
- * gem_get_query_memory_regions:
+ * gem_has_lmem:
  * @fd: open i915 drm file descriptor
  *
- * This function is prepared as a wrapper for the upcoming memory
- * regions implementation.
+ * Helper function to check if lmem is available on device.
  *
- * Returns: Filled struct with available memory regions.
+ * Returns: True if at least one lmem region was found.
  */
-struct drm_i915_query_memory_regions *gem_get_query_memory_regions(int fd)
+bool gem_has_lmem(int fd)
 {
-	struct drm_i915_query_memory_regions *query_info;
-
-	query_info = calloc(1, sizeof(struct drm_i915_query_memory_regions)
-			    + sizeof(struct drm_i915_memory_region_info));
-
-	query_info->num_regions = 1;
-	query_info->regions[0].region.memory_class = I915_MEMORY_CLASS_SYSTEM;
-	query_info->regions[0].probed_size = intel_get_total_ram_mb() << 20;
-	query_info->regions[0].unallocated_size = __get_available_smem(fd);
-
-	return query_info;
+	return gem_get_lmem_region_count(fd) > 0;
 }
 
 /* A version of gem_create_in_memory_region_list which can be allowed to
diff --git a/lib/i915/intel_memory_region.h b/lib/i915/intel_memory_region.h
index 0ddc9f98..3e53d8de 100644
--- a/lib/i915/intel_memory_region.h
+++ b/lib/i915/intel_memory_region.h
@@ -39,11 +39,16 @@
 
 #define REGION_SMEM    INTEL_MEMORY_REGION_ID(I915_MEMORY_CLASS_SYSTEM, 0)
 
+bool gem_has_query_support(int fd);
+
 const char *get_memory_region_name(uint32_t region);
 uint32_t gem_get_batch_size(int fd, uint8_t mem_region_type);
 
 struct drm_i915_query_memory_regions *gem_get_query_memory_regions(int fd);
 
+uint8_t gem_get_lmem_region_count(int fd);
+
+bool gem_has_lmem(int fd);
 
 int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t size,
 				       struct drm_i915_gem_memory_class_instance *mem_regions,
-- 
2.26.3

_______________________________________________
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

WARNING: multiple messages have this Message-ID (diff)
From: Matthew Auld <matthew.auld@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>,
	thomas.hellstrom@linux.intel.com,
	Michal Winiarski <michal.winiarski@intel.com>,
	petri.latvala@intel.com,
	Janulgue Abdiel <abdiel.janulgue@intel.com>,
	intel-gfx@lists.freedesktop.org,
	Lukasz Kalamarz <lukasz.kalamarz@intel.com>,
	daniel@ffwll.ch
Subject: [igt-dev] [PATCH i-g-t 09/12] lib/i915/intel_memory_region/dg1: Add new lib to query memory region
Date: Tue, 11 May 2021 17:51:14 +0100	[thread overview]
Message-ID: <20210511165117.428062-10-matthew.auld@intel.com> (raw)
In-Reply-To: <20210511165117.428062-1-matthew.auld@intel.com>

From: Lukasz Kalamarz <lukasz.kalamarz@intel.com>

With an introduction of Local Memory concept we should be able to
allocate object in specific memory region. This patch implements
helper functions that allow this.

Signed-off-by: Lukasz Kalamarz <lukasz.kalamarz@intel.com>
Cc: Janulgue Abdiel <abdiel.janulgue@intel.com>
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Michal Winiarski <michal.winiarski@intel.com>
Cc: Katarzyna Dec <katarzyna.dec@intel.com>
Cc: Vanshidhar Konda <vanshidhar.r.konda@intel.com>
Cc: Stuart Summers <stuart.summers@intel.com>
---
 lib/i915/intel_memory_region.c | 130 ++++++++++++++++++++-------------
 lib/i915/intel_memory_region.h |   5 ++
 2 files changed, 86 insertions(+), 49 deletions(-)

diff --git a/lib/i915/intel_memory_region.c b/lib/i915/intel_memory_region.c
index 412367e4..91d3ef18 100644
--- a/lib/i915/intel_memory_region.c
+++ b/lib/i915/intel_memory_region.c
@@ -27,7 +27,6 @@
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/sysinfo.h>
 #include <fcntl.h>
 
 #include "i915/gem_create.h"
@@ -41,10 +40,42 @@
 #include "intel_chipset.h"
 #include "igt_collection.h"
 #include "igt_device.h"
-#include "igt_aux.h"
 
 #include "i915/intel_memory_region.h"
 
+#define i915_query_items(fd, items, n_items) do { \
+		igt_assert_eq(__i915_query_items(fd, items, n_items), 0); \
+		errno = 0; \
+	} while (0)
+#define i915_query_items_err(fd, items, n_items, err) do { \
+		igt_assert_eq(__i915_query_items(fd, items, n_items), -err); \
+	} while (0)
+
+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_items(int fd, struct drm_i915_query_item *items, uint32_t n_items)
+{
+	struct drm_i915_query q = {
+		.num_items = n_items,
+		.items_ptr = to_user_pointer(items),
+	};
+	return __i915_query(fd, &q);
+}
+
+bool gem_has_query_support(int fd)
+{
+	struct drm_i915_query query = {};
+
+	return __i915_query(fd, &query) == 0;
+}
+
 const char *get_memory_region_name(uint32_t region)
 {
 	uint16_t class = MEMORY_TYPE_FROM_REGION(region);
@@ -59,76 +90,77 @@ const char *get_memory_region_name(uint32_t region)
  *  gem_get_batch_size:
  *  @fd: open i915 drm file descriptor
  *  @mem_region_type: used memory_region type
+ *
+ *  With introduction of LMEM we observe different page sizes for those two
+ *  memory regions. Without this helper funtion we may be prone to forget
+ *  about setting proper page size.
  */
 uint32_t gem_get_batch_size(int fd, uint8_t mem_region_type)
 {
-	/* temporary solution, to be erased later */
-	(void) fd;
-	(void) mem_region_type;
-
-	return 4096;
+	return (mem_region_type == I915_MEMORY_CLASS_DEVICE) ? 65536 : 4096;
 }
 
-static uint64_t __get_meminfo(const char *info, const char *tag)
+/**
+ * gem_get_query_memory_regions:
+ * @fd: open i915 drm file descriptor
+ *
+ * This function wraps query mechanism for memory regions.
+ *
+ * Returns: Filled struct with available memory regions.
+ */
+struct drm_i915_query_memory_regions *gem_get_query_memory_regions(int fd)
 {
-	const char *str;
-	unsigned long val;
+	struct drm_i915_query_item item;
+	struct drm_i915_query_memory_regions *query_info;
 
-	str = strstr(info, tag);
-	if (str && sscanf(str + strlen(tag), " %lu", &val) == 1)
-		return (uint64_t)val << 10;
+	memset(&item, 0, sizeof(item));
+	item.query_id = DRM_I915_QUERY_MEMORY_REGIONS;
+	i915_query_items(fd, &item, 1);
 
-	igt_warn("Unrecognized /proc/meminfo field: '%s'\n", tag);
-	return 0;
-}
+	query_info = calloc(1, item.length);
 
-static uint64_t __get_available_smem(int fd)
-{
-	uint64_t retval;
-	char *info;
-	int proc_fd;
+	item.data_ptr = to_user_pointer(query_info);
+	i915_query_items(fd, &item, 1);
 
-	intel_purge_vm_caches(fd);
+	return query_info;
+}
 
-	proc_fd = open("/proc", O_RDONLY);
-	info = igt_sysfs_get(proc_fd, "meminfo");
-	close(proc_fd);
+/**
+ * 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 drm_i915_query_memory_regions *query_info;
+	uint8_t num_regions;
+	uint8_t lmem_regions = 0;
 
-	if (info) {
-		retval = __get_meminfo(info, "MemAvailable:");
-	} else {
-		struct sysinfo sysinf;
+	query_info = gem_get_query_memory_regions(fd);
+	num_regions = query_info->num_regions;
 
-		igt_assert(sysinfo(&sysinf) == 0);
-		retval = sysinf.freeram;
-		retval *= sysinf.mem_unit;
+	for (int i = 0; i < num_regions; i++) {
+		if (query_info->regions[i].region.memory_class == I915_MEMORY_CLASS_DEVICE)
+			lmem_regions += 1;
 	}
 
-	return retval;
+	return lmem_regions;
 }
 
 /**
- * gem_get_query_memory_regions:
+ * gem_has_lmem:
  * @fd: open i915 drm file descriptor
  *
- * This function is prepared as a wrapper for the upcoming memory
- * regions implementation.
+ * Helper function to check if lmem is available on device.
  *
- * Returns: Filled struct with available memory regions.
+ * Returns: True if at least one lmem region was found.
  */
-struct drm_i915_query_memory_regions *gem_get_query_memory_regions(int fd)
+bool gem_has_lmem(int fd)
 {
-	struct drm_i915_query_memory_regions *query_info;
-
-	query_info = calloc(1, sizeof(struct drm_i915_query_memory_regions)
-			    + sizeof(struct drm_i915_memory_region_info));
-
-	query_info->num_regions = 1;
-	query_info->regions[0].region.memory_class = I915_MEMORY_CLASS_SYSTEM;
-	query_info->regions[0].probed_size = intel_get_total_ram_mb() << 20;
-	query_info->regions[0].unallocated_size = __get_available_smem(fd);
-
-	return query_info;
+	return gem_get_lmem_region_count(fd) > 0;
 }
 
 /* A version of gem_create_in_memory_region_list which can be allowed to
diff --git a/lib/i915/intel_memory_region.h b/lib/i915/intel_memory_region.h
index 0ddc9f98..3e53d8de 100644
--- a/lib/i915/intel_memory_region.h
+++ b/lib/i915/intel_memory_region.h
@@ -39,11 +39,16 @@
 
 #define REGION_SMEM    INTEL_MEMORY_REGION_ID(I915_MEMORY_CLASS_SYSTEM, 0)
 
+bool gem_has_query_support(int fd);
+
 const char *get_memory_region_name(uint32_t region);
 uint32_t gem_get_batch_size(int fd, uint8_t mem_region_type);
 
 struct drm_i915_query_memory_regions *gem_get_query_memory_regions(int fd);
 
+uint8_t gem_get_lmem_region_count(int fd);
+
+bool gem_has_lmem(int fd);
 
 int __gem_create_in_memory_region_list(int fd, uint32_t *handle, uint64_t size,
 				       struct drm_i915_gem_memory_class_instance *mem_regions,
-- 
2.26.3

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

  parent reply	other threads:[~2021-05-11 16:51 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-11 16:51 [Intel-gfx] [PATCH i-g-t 00/12] DG1/LMEM uAPI basics Matthew Auld
2021-05-11 16:51 ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 01/12] lib/i915/gem_create: Add gem_create_ext Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 02/12] lib/i915/intel_memory_region: Add memory regions stubs Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 03/12] tests/gem_gpgpu_fill: Convert from simple to standard igt_main Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 04/12] tests/i915/gem_exec_basic: Use memory region interface Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 05/12] tests/i915/gem_gpgpu_fill: " Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 06/12] tests/i915/gem_media_fill: " Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 07/12] i915_drm.h sync Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-19  8:19   ` [Intel-gfx] " Petri Latvala
2021-05-19  8:19     ` [igt-dev] " Petri Latvala
2021-05-19  8:25     ` [Intel-gfx] " Jani Nikula
2021-05-19  8:25       ` [igt-dev] " Jani Nikula
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 08/12] Synchronize memory region uapi and tests with i915_drm.h Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` Matthew Auld [this message]
2021-05-11 16:51   ` [igt-dev] [PATCH i-g-t 09/12] lib/i915/intel_memory_region/dg1: Add new lib to query memory region Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 10/12] tests/i915/gem_create: exercise placements extension Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 11/12] lib/i915/intel_memory_region: Add new macros and support for igt_collection Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 16:51 ` [Intel-gfx] [PATCH i-g-t 12/12] tests/i915/gem_exec_basic/dg1: Iterate over all memory regions Matthew Auld
2021-05-11 16:51   ` [igt-dev] " Matthew Auld
2021-05-11 17:52 ` [igt-dev] ✗ Fi.CI.BAT: failure for DG1/LMEM uAPI basics Patchwork
2021-05-19  8:13 ` [Intel-gfx] [igt-dev] [PATCH i-g-t 00/12] " Matthew Auld
2021-05-19  8:50   ` Petri Latvala
2021-05-19 10:45     ` Matthew Auld
2021-05-19 10:45       ` Matthew Auld
2021-05-19 11:02       ` [Intel-gfx] " Petri Latvala
2021-05-19 11:02         ` Petri Latvala
2021-05-19 11:36         ` [Intel-gfx] " Matthew Auld
2021-05-19 11:36           ` Matthew Auld
2021-05-19 12:07           ` [Intel-gfx] " Petri Latvala
2021-05-19 12:07             ` Petri Latvala

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=20210511165117.428062-10-matthew.auld@intel.com \
    --to=matthew.auld@intel.com \
    --cc=abdiel.janulgue@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=lukasz.kalamarz@intel.com \
    --cc=michal.winiarski@intel.com \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=vanshidhar.r.konda@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.