2 * SPDX-License-Identifier: MIT
4 * Copyright © 2018 Intel Corporation
7 #include <linux/nospec.h>
10 #include "i915_query.h"
11 #include <uapi/drm/i915_drm.h>
13 static int copy_query_item(void *query_hdr, size_t query_sz,
15 struct drm_i915_query_item *query_item)
17 if (query_item->length == 0)
20 if (query_item->length < total_length)
23 if (copy_from_user(query_hdr, u64_to_user_ptr(query_item->data_ptr),
27 if (!access_ok(u64_to_user_ptr(query_item->data_ptr),
34 static int query_topology_info(struct drm_i915_private *dev_priv,
35 struct drm_i915_query_item *query_item)
37 const struct sseu_dev_info *sseu = &RUNTIME_INFO(dev_priv)->sseu;
38 struct drm_i915_query_topology_info topo;
39 u32 slice_length, subslice_length, eu_length, total_length;
40 u8 subslice_stride = GEN_SSEU_STRIDE(sseu->max_subslices);
41 u8 eu_stride = GEN_SSEU_STRIDE(sseu->max_eus_per_subslice);
44 if (query_item->flags != 0)
47 if (sseu->max_slices == 0)
50 BUILD_BUG_ON(sizeof(u8) != sizeof(sseu->slice_mask));
52 slice_length = sizeof(sseu->slice_mask);
53 subslice_length = sseu->max_slices * subslice_stride;
54 eu_length = sseu->max_slices * sseu->max_subslices * eu_stride;
55 total_length = sizeof(topo) + slice_length + subslice_length +
58 ret = copy_query_item(&topo, sizeof(topo), total_length,
66 memset(&topo, 0, sizeof(topo));
67 topo.max_slices = sseu->max_slices;
68 topo.max_subslices = sseu->max_subslices;
69 topo.max_eus_per_subslice = sseu->max_eus_per_subslice;
71 topo.subslice_offset = slice_length;
72 topo.subslice_stride = subslice_stride;
73 topo.eu_offset = slice_length + subslice_length;
74 topo.eu_stride = eu_stride;
76 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr),
80 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr + sizeof(topo)),
81 &sseu->slice_mask, slice_length))
84 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr +
85 sizeof(topo) + slice_length),
86 sseu->subslice_mask, subslice_length))
89 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr +
91 slice_length + subslice_length),
92 sseu->eu_mask, eu_length))
99 query_engine_info(struct drm_i915_private *i915,
100 struct drm_i915_query_item *query_item)
102 struct drm_i915_query_engine_info __user *query_ptr =
103 u64_to_user_ptr(query_item->data_ptr);
104 struct drm_i915_engine_info __user *info_ptr;
105 struct drm_i915_query_engine_info query;
106 struct drm_i915_engine_info info = { };
107 struct intel_engine_cs *engine;
108 enum intel_engine_id id;
111 if (query_item->flags)
114 len = sizeof(struct drm_i915_query_engine_info) +
115 RUNTIME_INFO(i915)->num_engines *
116 sizeof(struct drm_i915_engine_info);
118 ret = copy_query_item(&query, sizeof(query), len, query_item);
122 if (query.num_engines || query.rsvd[0] || query.rsvd[1] ||
126 info_ptr = &query_ptr->engines[0];
128 for_each_engine(engine, i915, id) {
129 info.engine.engine_class = engine->uabi_class;
130 info.engine.engine_instance = engine->instance;
131 info.capabilities = engine->uabi_capabilities;
133 if (__copy_to_user(info_ptr, &info, sizeof(info)))
140 if (__copy_to_user(query_ptr, &query, sizeof(query)))
146 static int (* const i915_query_funcs[])(struct drm_i915_private *dev_priv,
147 struct drm_i915_query_item *query_item) = {
152 int i915_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
154 struct drm_i915_private *dev_priv = to_i915(dev);
155 struct drm_i915_query *args = data;
156 struct drm_i915_query_item __user *user_item_ptr =
157 u64_to_user_ptr(args->items_ptr);
160 if (args->flags != 0)
163 for (i = 0; i < args->num_items; i++, user_item_ptr++) {
164 struct drm_i915_query_item item;
165 unsigned long func_idx;
168 if (copy_from_user(&item, user_item_ptr, sizeof(item)))
171 if (item.query_id == 0)
174 if (overflows_type(item.query_id - 1, unsigned long))
177 func_idx = item.query_id - 1;
180 if (func_idx < ARRAY_SIZE(i915_query_funcs)) {
181 func_idx = array_index_nospec(func_idx,
182 ARRAY_SIZE(i915_query_funcs));
183 ret = i915_query_funcs[func_idx](dev_priv, &item);
186 /* Only write the length back to userspace if they differ. */
187 if (ret != item.length && put_user(ret, &user_item_ptr->length))