drm/i915/query: Expose memory regions through the query uAPI
authorAbdiel Janulgue <abdiel.janulgue@linux.intel.com>
Thu, 29 Apr 2021 10:30:50 +0000 (11:30 +0100)
committerMatthew Auld <matthew.auld@intel.com>
Tue, 4 May 2021 09:58:51 +0000 (10:58 +0100)
Returns the available memory region areas supported by the HW.

v2(Daniel & Jason):
    - Add some kernel-doc, including example usage.
    - Drop all the extra rsvd
v3(Jason & Tvrtko)
    - add back rsvd

Signed-off-by: Abdiel Janulgue <abdiel.janulgue@linux.intel.com>
Signed-off-by: Matthew Auld <matthew.auld@intel.com>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
Cc: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Cc: Daniele Ceraolo Spurio <daniele.ceraolospurio@intel.com>
Cc: Lionel Landwerlin <lionel.g.landwerlin@linux.intel.com>
Cc: Jon Bloomfield <jon.bloomfield@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Daniel Vetter <daniel.vetter@intel.com>
Cc: Kenneth Graunke <kenneth@whitecape.org>
Cc: Jason Ekstrand <jason@jlekstrand.net>
Cc: Dave Airlie <airlied@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Cc: mesa-dev@lists.freedesktop.org
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20210429103056.407067-3-matthew.auld@intel.com
drivers/gpu/drm/i915/i915_query.c
drivers/gpu/drm/i915/intel_memory_region.h
include/uapi/drm/i915_drm.h

index fed337a..5e2b909 100644 (file)
@@ -419,11 +419,70 @@ static int query_perf_config(struct drm_i915_private *i915,
        }
 }
 
+static int query_memregion_info(struct drm_i915_private *i915,
+                               struct drm_i915_query_item *query_item)
+{
+       struct drm_i915_query_memory_regions __user *query_ptr =
+               u64_to_user_ptr(query_item->data_ptr);
+       struct drm_i915_memory_region_info __user *info_ptr =
+               &query_ptr->regions[0];
+       struct drm_i915_memory_region_info info = { };
+       struct drm_i915_query_memory_regions query;
+       struct intel_memory_region *mr;
+       u32 total_length;
+       int ret, id, i;
+
+       if (query_item->flags != 0)
+               return -EINVAL;
+
+       total_length = sizeof(query);
+       for_each_memory_region(mr, i915, id) {
+               if (mr->private)
+                       continue;
+
+               total_length += sizeof(info);
+       }
+
+       ret = copy_query_item(&query, sizeof(query), total_length, query_item);
+       if (ret != 0)
+               return ret;
+
+       if (query.num_regions)
+               return -EINVAL;
+
+       for (i = 0; i < ARRAY_SIZE(query.rsvd); i++) {
+               if (query.rsvd[i])
+                       return -EINVAL;
+       }
+
+       for_each_memory_region(mr, i915, id) {
+               if (mr->private)
+                       continue;
+
+               info.region.memory_class = mr->type;
+               info.region.memory_instance = mr->instance;
+               info.probed_size = mr->total;
+               info.unallocated_size = mr->avail;
+
+               if (__copy_to_user(info_ptr, &info, sizeof(info)))
+                       return -EFAULT;
+
+               query.num_regions++;
+               info_ptr++;
+       }
+
+       if (__copy_to_user(query_ptr, &query, sizeof(query)))
+               return -EFAULT;
+
+       return total_length;
+}
+
 static int (* const i915_query_funcs[])(struct drm_i915_private *dev_priv,
                                        struct drm_i915_query_item *query_item) = {
        query_topology_info,
        query_engine_info,
        query_perf_config,
+       query_memregion_info,
 };
 
 int i915_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
index 942fc4f..7cd8e3d 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/mutex.h>
 #include <linux/io-mapping.h>
 #include <drm/drm_mm.h>
+#include <drm/i915_drm.h>
 
 #include "i915_buddy.h"
 
@@ -19,12 +20,9 @@ struct drm_i915_gem_object;
 struct intel_memory_region;
 struct sg_table;
 
-/**
- *  Base memory type
- */
 enum intel_memory_type {
-       INTEL_MEMORY_SYSTEM = 0,
-       INTEL_MEMORY_LOCAL,
+       INTEL_MEMORY_SYSTEM = I915_MEMORY_CLASS_SYSTEM,
+       INTEL_MEMORY_LOCAL = I915_MEMORY_CLASS_DEVICE,
        INTEL_MEMORY_STOLEN_SYSTEM,
        INTEL_MEMORY_STOLEN_LOCAL,
 };
index 6a34243..582ee6f 100644 (file)
@@ -2230,6 +2230,7 @@ struct drm_i915_query_item {
 #define DRM_I915_QUERY_TOPOLOGY_INFO    1
 #define DRM_I915_QUERY_ENGINE_INFO     2
 #define DRM_I915_QUERY_PERF_CONFIG      3
+#define DRM_I915_QUERY_MEMORY_REGIONS   4
 /* Must be kept compact -- no holes and well documented */
 
        /**
@@ -2464,6 +2465,139 @@ struct drm_i915_query_perf_config {
        __u8 data[];
 };
 
+/**
+ * enum drm_i915_gem_memory_class - Supported memory classes
+ */
+enum drm_i915_gem_memory_class {
+       /** @I915_MEMORY_CLASS_SYSTEM: System memory */
+       I915_MEMORY_CLASS_SYSTEM = 0,
+       /** @I915_MEMORY_CLASS_DEVICE: Device local-memory */
+       I915_MEMORY_CLASS_DEVICE,
+};
+
+/**
+ * struct drm_i915_gem_memory_class_instance - Identify particular memory region
+ */
+struct drm_i915_gem_memory_class_instance {
+       /** @memory_class: See enum drm_i915_gem_memory_class */
+       __u16 memory_class;
+
+       /** @memory_instance: Which instance */
+       __u16 memory_instance;
+};
+
+/**
+ * struct drm_i915_memory_region_info - Describes one region as known to the
+ * driver.
+ *
+ * Note that we reserve some stuff here for potential future work. As an example
+ * we might want expose the capabilities for a given region, which could include
+ * things like if the region is CPU mappable/accessible, what are the supported
+ * mapping types etc.
+ *
+ * Note that to extend struct drm_i915_memory_region_info and struct
+ * drm_i915_query_memory_regions in the future the plan is to do the following:
+ *
+ * .. code-block:: C
+ *
+ *     struct drm_i915_memory_region_info {
+ *             struct drm_i915_gem_memory_class_instance region;
+ *             union {
+ *                     __u32 rsvd0;
+ *                     __u32 new_thing1;
+ *             };
+ *             ...
+ *             union {
+ *                     __u64 rsvd1[8];
+ *                     struct {
+ *                             __u64 new_thing2;
+ *                             __u64 new_thing3;
+ *                             ...
+ *                     };
+ *             };
+ *     };
+ *
+ * With this things should remain source compatible between versions for
+ * userspace, even as we add new fields.
+ *
+ * Note this is using both struct drm_i915_query_item and struct drm_i915_query.
+ * For this new query we are adding the new query id DRM_I915_QUERY_MEMORY_REGIONS
+ * at &drm_i915_query_item.query_id.
+ */
+struct drm_i915_memory_region_info {
+       /** @region: The class:instance pair encoding */
+       struct drm_i915_gem_memory_class_instance region;
+
+       /** @rsvd0: MBZ */
+       __u32 rsvd0;
+
+       /** @probed_size: Memory probed by the driver (-1 = unknown) */
+       __u64 probed_size;
+
+       /** @unallocated_size: Estimate of memory remaining (-1 = unknown) */
+       __u64 unallocated_size;
+
+       /** @rsvd1: MBZ */
+       __u64 rsvd1[8];
+};
+
+/**
+ * struct drm_i915_query_memory_regions
+ *
+ * The region info query enumerates all regions known to the driver by filling
+ * in an array of struct drm_i915_memory_region_info structures.
+ *
+ * Example for getting the list of supported regions:
+ *
+ * .. code-block:: C
+ *
+ *     struct drm_i915_query_memory_regions *info;
+ *     struct drm_i915_query_item item = {
+ *             .query_id = DRM_I915_QUERY_MEMORY_REGIONS;
+ *     };
+ *     struct drm_i915_query query = {
+ *             .num_items = 1,
+ *             .items_ptr = (uintptr_t)&item,
+ *     };
+ *     int err, i;
+ *
+ *     // First query the size of the blob we need, this needs to be large
+ *     // enough to hold our array of regions. The kernel will fill out the
+ *     // item.length for us, which is the number of bytes we need.
+ *     err = ioctl(fd, DRM_IOCTL_I915_QUERY, &query);
+ *     if (err) ...
+ *
+ *     info = calloc(1, item.length);
+ *     // Now that we allocated the required number of bytes, we call the ioctl
+ *     // again, this time with the data_ptr pointing to our newly allocated
+ *     // blob, which the kernel can then populate with the all the region info.
+ *     item.data_ptr = (uintptr_t)&info,
+ *
+ *     err = ioctl(fd, DRM_IOCTL_I915_QUERY, &query);
+ *     if (err) ...
+ *
+ *     // We can now access each region in the array
+ *     for (i = 0; i < info->num_regions; i++) {
+ *             struct drm_i915_memory_region_info mr = info->regions[i];
+ *             u16 class = mr.region.class;
+ *             u16 instance = mr.region.instance;
+ *
+ *             ....
+ *     }
+ *
+ *     free(info);
+ */
+struct drm_i915_query_memory_regions {
+       /** @num_regions: Number of supported regions */
+       __u32 num_regions;
+
+       /** @rsvd: MBZ */
+       __u32 rsvd[3];
+
+       /** @regions: Info about each supported region */
+       struct drm_i915_memory_region_info regions[];
+};
+
 #if defined(__cplusplus)
 }
 #endif