[igt-dev] [RFC 1/2] lib/i915/intel_memory_region: wrappers for memory regions ops

Ramalingam C ramalingam.c at intel.com
Sat Jan 4 10:08:25 UTC 2020


As of now lmem details are exposed from kernel through debugfs.
Wrappers for detecting the lmem and reading it's details are written.

Signed-off-by: Ramalingam C <ramalingam.c at intel.com>
cc: Zbigniew Kempczy_ski <zbigniew.kempczynski at intel.com>
cc: Vinay Belgaumkar <vinay.belgaumkar at intel.com>
---
 lib/Makefile.sources           |   2 +
 lib/i915/intel_memory_region.c | 105 +++++++++++++++++++++++++++++++++
 lib/i915/intel_memory_region.h |  30 ++++++++++
 lib/meson.build                |   1 +
 4 files changed, 138 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 5dd3962ee019..9f1a457825f4 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 000000000000..95d0646c3863
--- /dev/null
+++ b/lib/i915/intel_memory_region.c
@@ -0,0 +1,105 @@
+/*
+ * Copyright © 2020 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 "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"
+
+static void __debugfs_read(int fd, const char *param, char *buf, int len)
+{
+	len = igt_debugfs_simple_read(fd, param, buf, len);
+	if (len < 0)
+		igt_assert_eq(len, -ENODEV);
+}
+
+#define debugfs_read(fd, p, arr) __debugfs_read(fd, p, arr, sizeof(arr))
+
+#define MAX_i915_GEM_OBJ_BUF_LEN	5000
+
+static int get_i915_gem_objects_str(int drm_fd, char *buf)
+{
+	int fd;
+
+	fd = igt_debugfs_dir(drm_fd);
+	if (fd < 0)
+		return -ENODEV;
+
+	debugfs_read(fd, "i915_gem_objects", buf);
+	close(fd);
+
+	return 0;
+}
+/**
+ * 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)
+{
+	char buf[MAX_i915_GEM_OBJ_BUF_LEN];
+
+	if (get_i915_gem_objects_str(fd, buf) < 0)
+		return false;
+
+	return strstr(buf, "local");
+}
+
+/**
+ * gem_lmem_total_size:
+ * @drm_fd: open i915 drm file descriptor
+ *
+ * Helper function to get the lmem size on device.
+ *
+ * Returns: returns lmem size if it is found else 0.
+ */
+uint64_t gem_lmem_total_size(int fd)
+{
+	char buf[MAX_i915_GEM_OBJ_BUF_LEN];
+	char *str;
+	int ret;
+
+	ret = get_i915_gem_objects_str(fd, buf);
+	if (ret < 0)
+		return 0;
+
+	str = strstr(buf, "local");
+	if (str) {
+		str = strstr(str, "total: ");
+		igt_assert_f(str, "expected string not found");
+		return strtoul(str + strlen("total: "), NULL, 16);
+	}
+
+	return 0;
+}
diff --git a/lib/i915/intel_memory_region.h b/lib/i915/intel_memory_region.h
new file mode 100644
index 000000000000..bbcaef6a19fb
--- /dev/null
+++ b/lib/i915/intel_memory_region.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright © 2020 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
+
+bool gem_has_lmem(int fd);
+uint64_t gem_lmem_total_size(int fd);
+
+#endif /* INTEL_MEMORY_REGION_H */
diff --git a/lib/meson.build b/lib/meson.build
index 57eb7d938639..25a9960a57e2 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.20.1



More information about the igt-dev mailing list