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 query_topology_info(struct drm_i915_private *dev_priv,
14 struct drm_i915_query_item *query_item)
16 const struct sseu_dev_info *sseu = &RUNTIME_INFO(dev_priv)->sseu;
17 struct drm_i915_query_topology_info topo;
18 u32 slice_length, subslice_length, eu_length, total_length;
20 if (query_item->flags != 0)
23 if (sseu->max_slices == 0)
26 BUILD_BUG_ON(sizeof(u8) != sizeof(sseu->slice_mask));
28 slice_length = sizeof(sseu->slice_mask);
29 subslice_length = sseu->max_slices *
30 DIV_ROUND_UP(sseu->max_subslices, BITS_PER_BYTE);
31 eu_length = sseu->max_slices * sseu->max_subslices *
32 DIV_ROUND_UP(sseu->max_eus_per_subslice, BITS_PER_BYTE);
34 total_length = sizeof(topo) + slice_length + subslice_length + eu_length;
36 if (query_item->length == 0)
39 if (query_item->length < total_length)
42 if (copy_from_user(&topo, u64_to_user_ptr(query_item->data_ptr),
49 if (!access_ok(u64_to_user_ptr(query_item->data_ptr),
53 memset(&topo, 0, sizeof(topo));
54 topo.max_slices = sseu->max_slices;
55 topo.max_subslices = sseu->max_subslices;
56 topo.max_eus_per_subslice = sseu->max_eus_per_subslice;
58 topo.subslice_offset = slice_length;
59 topo.subslice_stride = DIV_ROUND_UP(sseu->max_subslices, BITS_PER_BYTE);
60 topo.eu_offset = slice_length + subslice_length;
62 DIV_ROUND_UP(sseu->max_eus_per_subslice, BITS_PER_BYTE);
64 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr),
68 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr + sizeof(topo)),
69 &sseu->slice_mask, slice_length))
72 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr +
73 sizeof(topo) + slice_length),
74 sseu->subslice_mask, subslice_length))
77 if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr +
79 slice_length + subslice_length),
80 sseu->eu_mask, eu_length))
86 static int (* const i915_query_funcs[])(struct drm_i915_private *dev_priv,
87 struct drm_i915_query_item *query_item) = {
91 int i915_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
93 struct drm_i915_private *dev_priv = to_i915(dev);
94 struct drm_i915_query *args = data;
95 struct drm_i915_query_item __user *user_item_ptr =
96 u64_to_user_ptr(args->items_ptr);
102 for (i = 0; i < args->num_items; i++, user_item_ptr++) {
103 struct drm_i915_query_item item;
104 unsigned long func_idx;
107 if (copy_from_user(&item, user_item_ptr, sizeof(item)))
110 if (item.query_id == 0)
113 if (overflows_type(item.query_id - 1, unsigned long))
116 func_idx = item.query_id - 1;
119 if (func_idx < ARRAY_SIZE(i915_query_funcs)) {
120 func_idx = array_index_nospec(func_idx,
121 ARRAY_SIZE(i915_query_funcs));
122 ret = i915_query_funcs[func_idx](dev_priv, &item);
125 /* Only write the length back to userspace if they differ. */
126 if (ret != item.length && put_user(ret, &user_item_ptr->length))