xref: /openbmc/linux/drivers/gpu/drm/i915/i915_query.c (revision 160b8e75)
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2018 Intel Corporation
5  */
6 
7 #include "i915_drv.h"
8 #include "i915_query.h"
9 #include <uapi/drm/i915_drm.h>
10 
11 static int query_topology_info(struct drm_i915_private *dev_priv,
12 			       struct drm_i915_query_item *query_item)
13 {
14 	const struct sseu_dev_info *sseu = &INTEL_INFO(dev_priv)->sseu;
15 	struct drm_i915_query_topology_info topo;
16 	u32 slice_length, subslice_length, eu_length, total_length;
17 
18 	if (query_item->flags != 0)
19 		return -EINVAL;
20 
21 	if (sseu->max_slices == 0)
22 		return -ENODEV;
23 
24 	BUILD_BUG_ON(sizeof(u8) != sizeof(sseu->slice_mask));
25 
26 	slice_length = sizeof(sseu->slice_mask);
27 	subslice_length = sseu->max_slices *
28 		DIV_ROUND_UP(sseu->max_subslices,
29 			     sizeof(sseu->subslice_mask[0]) * BITS_PER_BYTE);
30 	eu_length = sseu->max_slices * sseu->max_subslices *
31 		DIV_ROUND_UP(sseu->max_eus_per_subslice, BITS_PER_BYTE);
32 
33 	total_length = sizeof(topo) + slice_length + subslice_length + eu_length;
34 
35 	if (query_item->length == 0)
36 		return total_length;
37 
38 	if (query_item->length < total_length)
39 		return -EINVAL;
40 
41 	if (copy_from_user(&topo, u64_to_user_ptr(query_item->data_ptr),
42 			   sizeof(topo)))
43 		return -EFAULT;
44 
45 	if (topo.flags != 0)
46 		return -EINVAL;
47 
48 	if (!access_ok(VERIFY_WRITE, u64_to_user_ptr(query_item->data_ptr),
49 		       total_length))
50 		return -EFAULT;
51 
52 	memset(&topo, 0, sizeof(topo));
53 	topo.max_slices = sseu->max_slices;
54 	topo.max_subslices = sseu->max_subslices;
55 	topo.max_eus_per_subslice = sseu->max_eus_per_subslice;
56 
57 	topo.subslice_offset = slice_length;
58 	topo.subslice_stride = DIV_ROUND_UP(sseu->max_subslices, BITS_PER_BYTE);
59 	topo.eu_offset = slice_length + subslice_length;
60 	topo.eu_stride =
61 		DIV_ROUND_UP(sseu->max_eus_per_subslice, BITS_PER_BYTE);
62 
63 	if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr),
64 			   &topo, sizeof(topo)))
65 		return -EFAULT;
66 
67 	if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr + sizeof(topo)),
68 			   &sseu->slice_mask, slice_length))
69 		return -EFAULT;
70 
71 	if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr +
72 					   sizeof(topo) + slice_length),
73 			   sseu->subslice_mask, subslice_length))
74 		return -EFAULT;
75 
76 	if (__copy_to_user(u64_to_user_ptr(query_item->data_ptr +
77 					   sizeof(topo) +
78 					   slice_length + subslice_length),
79 			   sseu->eu_mask, eu_length))
80 		return -EFAULT;
81 
82 	return total_length;
83 }
84 
85 static int (* const i915_query_funcs[])(struct drm_i915_private *dev_priv,
86 					struct drm_i915_query_item *query_item) = {
87 	query_topology_info,
88 };
89 
90 int i915_query_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
91 {
92 	struct drm_i915_private *dev_priv = to_i915(dev);
93 	struct drm_i915_query *args = data;
94 	struct drm_i915_query_item __user *user_item_ptr =
95 		u64_to_user_ptr(args->items_ptr);
96 	u32 i;
97 
98 	if (args->flags != 0)
99 		return -EINVAL;
100 
101 	for (i = 0; i < args->num_items; i++, user_item_ptr++) {
102 		struct drm_i915_query_item item;
103 		u64 func_idx;
104 		int ret;
105 
106 		if (copy_from_user(&item, user_item_ptr, sizeof(item)))
107 			return -EFAULT;
108 
109 		if (item.query_id == 0)
110 			return -EINVAL;
111 
112 		func_idx = item.query_id - 1;
113 
114 		if (func_idx < ARRAY_SIZE(i915_query_funcs))
115 			ret = i915_query_funcs[func_idx](dev_priv, &item);
116 		else
117 			ret = -EINVAL;
118 
119 		/* Only write the length back to userspace if they differ. */
120 		if (ret != item.length && put_user(ret, &user_item_ptr->length))
121 			return -EFAULT;
122 	}
123 
124 	return 0;
125 }
126