anv: Use intel_i915_query_alloc for memory regions

Error handling with DRM_IOCTL_I915_QUERY is tricky and we got it wrong
in one of the two calls here.  Use the common helper instead.  This also
fixes a theoretical bug where calloc() fails.  While we're here, inline
anv_track_meminfo because we're not really benefiting from having it
separate anymore.

Fixes: 65e8d72bc1 "anv: Query memory region info"
Reviewed-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/11770>
This commit is contained in:
Jason Ekstrand 2021-07-07 14:32:23 -05:00 committed by Marge Bot
parent ffdf4d7683
commit 35ec1d9730
1 changed files with 8 additions and 30 deletions

View File

@ -328,13 +328,17 @@ get_device_extensions(const struct anv_physical_device *device,
};
}
static void
anv_track_meminfo(struct anv_physical_device *device,
const struct drm_i915_query_memory_regions *mem_regions)
static bool
anv_get_query_meminfo(struct anv_physical_device *device, int fd)
{
struct drm_i915_query_memory_regions *mem_regions =
intel_i915_query_alloc(fd, DRM_I915_QUERY_MEMORY_REGIONS);
if (mem_regions == NULL)
return false;
for(int i = 0; i < mem_regions->num_regions; i++) {
switch(mem_regions->regions[i].region.memory_class) {
case I915_MEMORY_CLASS_SYSTEM:
case I915_MEMORY_CLASS_SYSTEM:
device->sys.region = mem_regions->regions[i].region;
device->sys.size = mem_regions->regions[i].probed_size;
break;
@ -346,32 +350,6 @@ anv_track_meminfo(struct anv_physical_device *device,
break;
}
}
}
static bool
anv_get_query_meminfo(struct anv_physical_device *device, int fd)
{
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,
};
if (drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query))
return false;
struct drm_i915_query_memory_regions *mem_regions = calloc(1, item.length);
item.data_ptr = (uintptr_t) mem_regions;
if (drmIoctl(fd, DRM_IOCTL_I915_QUERY, &query) || item.length <= 0) {
free(mem_regions);
return false;
}
anv_track_meminfo(device, mem_regions);
free(mem_regions);
return true;