1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 
6 #include <drm/drm_fourcc.h>
7 
8 #include "display/intel_display.h"
9 #include "gem/i915_gem_ioctls.h"
10 #include "gem/i915_gem_lmem.h"
11 #include "gem/i915_gem_region.h"
12 #include "pxp/intel_pxp.h"
13 
14 #include "i915_drv.h"
15 #include "i915_gem_create.h"
16 #include "i915_trace.h"
17 #include "i915_user_extensions.h"
18 
object_max_page_size(struct intel_memory_region ** placements,unsigned int n_placements)19 static u32 object_max_page_size(struct intel_memory_region **placements,
20 				unsigned int n_placements)
21 {
22 	u32 max_page_size = 0;
23 	int i;
24 
25 	for (i = 0; i < n_placements; i++) {
26 		struct intel_memory_region *mr = placements[i];
27 
28 		GEM_BUG_ON(!is_power_of_2(mr->min_page_size));
29 		max_page_size = max_t(u32, max_page_size, mr->min_page_size);
30 	}
31 
32 	GEM_BUG_ON(!max_page_size);
33 	return max_page_size;
34 }
35 
object_set_placements(struct drm_i915_gem_object * obj,struct intel_memory_region ** placements,unsigned int n_placements)36 static int object_set_placements(struct drm_i915_gem_object *obj,
37 				 struct intel_memory_region **placements,
38 				 unsigned int n_placements)
39 {
40 	struct intel_memory_region **arr;
41 	unsigned int i;
42 
43 	GEM_BUG_ON(!n_placements);
44 
45 	/*
46 	 * For the common case of one memory region, skip storing an
47 	 * allocated array and just point at the region directly.
48 	 */
49 	if (n_placements == 1) {
50 		struct intel_memory_region *mr = placements[0];
51 		struct drm_i915_private *i915 = mr->i915;
52 
53 		obj->mm.placements = &i915->mm.regions[mr->id];
54 		obj->mm.n_placements = 1;
55 	} else {
56 		arr = kmalloc_array(n_placements,
57 				    sizeof(struct intel_memory_region *),
58 				    GFP_KERNEL);
59 		if (!arr)
60 			return -ENOMEM;
61 
62 		for (i = 0; i < n_placements; i++)
63 			arr[i] = placements[i];
64 
65 		obj->mm.placements = arr;
66 		obj->mm.n_placements = n_placements;
67 	}
68 
69 	return 0;
70 }
71 
i915_gem_publish(struct drm_i915_gem_object * obj,struct drm_file * file,u64 * size_p,u32 * handle_p)72 static int i915_gem_publish(struct drm_i915_gem_object *obj,
73 			    struct drm_file *file,
74 			    u64 *size_p,
75 			    u32 *handle_p)
76 {
77 	u64 size = obj->base.size;
78 	int ret;
79 
80 	ret = drm_gem_handle_create(file, &obj->base, handle_p);
81 	/* drop reference from allocate - handle holds it now */
82 	i915_gem_object_put(obj);
83 	if (ret)
84 		return ret;
85 
86 	*size_p = size;
87 	return 0;
88 }
89 
90 static struct drm_i915_gem_object *
__i915_gem_object_create_user_ext(struct drm_i915_private * i915,u64 size,struct intel_memory_region ** placements,unsigned int n_placements,unsigned int ext_flags)91 __i915_gem_object_create_user_ext(struct drm_i915_private *i915, u64 size,
92 				  struct intel_memory_region **placements,
93 				  unsigned int n_placements,
94 				  unsigned int ext_flags)
95 {
96 	struct intel_memory_region *mr = placements[0];
97 	struct drm_i915_gem_object *obj;
98 	unsigned int flags;
99 	int ret;
100 
101 	i915_gem_flush_free_objects(i915);
102 
103 	size = round_up(size, object_max_page_size(placements, n_placements));
104 	if (size == 0)
105 		return ERR_PTR(-EINVAL);
106 
107 	/* For most of the ABI (e.g. mmap) we think in system pages */
108 	GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
109 
110 	if (i915_gem_object_size_2big(size))
111 		return ERR_PTR(-E2BIG);
112 
113 	obj = i915_gem_object_alloc();
114 	if (!obj)
115 		return ERR_PTR(-ENOMEM);
116 
117 	ret = object_set_placements(obj, placements, n_placements);
118 	if (ret)
119 		goto object_free;
120 
121 	/*
122 	 * I915_BO_ALLOC_USER will make sure the object is cleared before
123 	 * any user access.
124 	 */
125 	flags = I915_BO_ALLOC_USER;
126 
127 	ret = mr->ops->init_object(mr, obj, I915_BO_INVALID_OFFSET, size, 0, flags);
128 	if (ret)
129 		goto object_free;
130 
131 	GEM_BUG_ON(size != obj->base.size);
132 
133 	/* Add any flag set by create_ext options */
134 	obj->flags |= ext_flags;
135 
136 	trace_i915_gem_object_create(obj);
137 	return obj;
138 
139 object_free:
140 	if (obj->mm.n_placements > 1)
141 		kfree(obj->mm.placements);
142 	i915_gem_object_free(obj);
143 	return ERR_PTR(ret);
144 }
145 
146 /**
147  * Creates a new object using the same path as DRM_I915_GEM_CREATE_EXT
148  * @i915: i915 private
149  * @size: size of the buffer, in bytes
150  * @placements: possible placement regions, in priority order
151  * @n_placements: number of possible placement regions
152  *
153  * This function is exposed primarily for selftests and does very little
154  * error checking.  It is assumed that the set of placement regions has
155  * already been verified to be valid.
156  */
157 struct drm_i915_gem_object *
__i915_gem_object_create_user(struct drm_i915_private * i915,u64 size,struct intel_memory_region ** placements,unsigned int n_placements)158 __i915_gem_object_create_user(struct drm_i915_private *i915, u64 size,
159 			      struct intel_memory_region **placements,
160 			      unsigned int n_placements)
161 {
162 	return __i915_gem_object_create_user_ext(i915, size, placements,
163 						 n_placements, 0);
164 }
165 
166 int
i915_gem_dumb_create(struct drm_file * file,struct drm_device * dev,struct drm_mode_create_dumb * args)167 i915_gem_dumb_create(struct drm_file *file,
168 		     struct drm_device *dev,
169 		     struct drm_mode_create_dumb *args)
170 {
171 	struct drm_i915_gem_object *obj;
172 	struct intel_memory_region *mr;
173 	enum intel_memory_type mem_type;
174 	int cpp = DIV_ROUND_UP(args->bpp, 8);
175 	u32 format;
176 
177 	switch (cpp) {
178 	case 1:
179 		format = DRM_FORMAT_C8;
180 		break;
181 	case 2:
182 		format = DRM_FORMAT_RGB565;
183 		break;
184 	case 4:
185 		format = DRM_FORMAT_XRGB8888;
186 		break;
187 	default:
188 		return -EINVAL;
189 	}
190 
191 	/* have to work out size/pitch and return them */
192 	args->pitch = ALIGN(args->width * cpp, 64);
193 
194 	/* align stride to page size so that we can remap */
195 	if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
196 						    DRM_FORMAT_MOD_LINEAR))
197 		args->pitch = ALIGN(args->pitch, 4096);
198 
199 	if (args->pitch < args->width)
200 		return -EINVAL;
201 
202 	args->size = mul_u32_u32(args->pitch, args->height);
203 
204 	mem_type = INTEL_MEMORY_SYSTEM;
205 	if (HAS_LMEM(to_i915(dev)))
206 		mem_type = INTEL_MEMORY_LOCAL;
207 
208 	mr = intel_memory_region_by_type(to_i915(dev), mem_type);
209 
210 	obj = __i915_gem_object_create_user(to_i915(dev), args->size, &mr, 1);
211 	if (IS_ERR(obj))
212 		return PTR_ERR(obj);
213 
214 	return i915_gem_publish(obj, file, &args->size, &args->handle);
215 }
216 
217 /**
218  * Creates a new mm object and returns a handle to it.
219  * @dev: drm device pointer
220  * @data: ioctl data blob
221  * @file: drm file pointer
222  */
223 int
i915_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)224 i915_gem_create_ioctl(struct drm_device *dev, void *data,
225 		      struct drm_file *file)
226 {
227 	struct drm_i915_private *i915 = to_i915(dev);
228 	struct drm_i915_gem_create *args = data;
229 	struct drm_i915_gem_object *obj;
230 	struct intel_memory_region *mr;
231 
232 	mr = intel_memory_region_by_type(i915, INTEL_MEMORY_SYSTEM);
233 
234 	obj = __i915_gem_object_create_user(i915, args->size, &mr, 1);
235 	if (IS_ERR(obj))
236 		return PTR_ERR(obj);
237 
238 	return i915_gem_publish(obj, file, &args->size, &args->handle);
239 }
240 
241 struct create_ext {
242 	struct drm_i915_private *i915;
243 	struct intel_memory_region *placements[INTEL_REGION_UNKNOWN];
244 	unsigned int n_placements;
245 	unsigned int placement_mask;
246 	unsigned long flags;
247 };
248 
repr_placements(char * buf,size_t size,struct intel_memory_region ** placements,int n_placements)249 static void repr_placements(char *buf, size_t size,
250 			    struct intel_memory_region **placements,
251 			    int n_placements)
252 {
253 	int i;
254 
255 	buf[0] = '\0';
256 
257 	for (i = 0; i < n_placements; i++) {
258 		struct intel_memory_region *mr = placements[i];
259 		int r;
260 
261 		r = snprintf(buf, size, "\n  %s -> { class: %d, inst: %d }",
262 			     mr->name, mr->type, mr->instance);
263 		if (r >= size)
264 			return;
265 
266 		buf += r;
267 		size -= r;
268 	}
269 }
270 
set_placements(struct drm_i915_gem_create_ext_memory_regions * args,struct create_ext * ext_data)271 static int set_placements(struct drm_i915_gem_create_ext_memory_regions *args,
272 			  struct create_ext *ext_data)
273 {
274 	struct drm_i915_private *i915 = ext_data->i915;
275 	struct drm_i915_gem_memory_class_instance __user *uregions =
276 		u64_to_user_ptr(args->regions);
277 	struct intel_memory_region *placements[INTEL_REGION_UNKNOWN];
278 	u32 mask;
279 	int i, ret = 0;
280 
281 	if (args->pad) {
282 		drm_dbg(&i915->drm, "pad should be zero\n");
283 		ret = -EINVAL;
284 	}
285 
286 	if (!args->num_regions) {
287 		drm_dbg(&i915->drm, "num_regions is zero\n");
288 		ret = -EINVAL;
289 	}
290 
291 	BUILD_BUG_ON(ARRAY_SIZE(i915->mm.regions) != ARRAY_SIZE(placements));
292 	BUILD_BUG_ON(ARRAY_SIZE(ext_data->placements) != ARRAY_SIZE(placements));
293 	if (args->num_regions > ARRAY_SIZE(i915->mm.regions)) {
294 		drm_dbg(&i915->drm, "num_regions is too large\n");
295 		ret = -EINVAL;
296 	}
297 
298 	if (ret)
299 		return ret;
300 
301 	mask = 0;
302 	for (i = 0; i < args->num_regions; i++) {
303 		struct drm_i915_gem_memory_class_instance region;
304 		struct intel_memory_region *mr;
305 
306 		if (copy_from_user(&region, uregions, sizeof(region)))
307 			return -EFAULT;
308 
309 		mr = intel_memory_region_lookup(i915,
310 						region.memory_class,
311 						region.memory_instance);
312 		if (!mr || mr->private) {
313 			drm_dbg(&i915->drm, "Device is missing region { class: %d, inst: %d } at index = %d\n",
314 				region.memory_class, region.memory_instance, i);
315 			ret = -EINVAL;
316 			goto out_dump;
317 		}
318 
319 		if (mask & BIT(mr->id)) {
320 			drm_dbg(&i915->drm, "Found duplicate placement %s -> { class: %d, inst: %d } at index = %d\n",
321 				mr->name, region.memory_class,
322 				region.memory_instance, i);
323 			ret = -EINVAL;
324 			goto out_dump;
325 		}
326 
327 		placements[i] = mr;
328 		mask |= BIT(mr->id);
329 
330 		++uregions;
331 	}
332 
333 	if (ext_data->n_placements) {
334 		ret = -EINVAL;
335 		goto out_dump;
336 	}
337 
338 	ext_data->n_placements = args->num_regions;
339 	for (i = 0; i < args->num_regions; i++)
340 		ext_data->placements[i] = placements[i];
341 
342 	ext_data->placement_mask = mask;
343 	return 0;
344 
345 out_dump:
346 	if (1) {
347 		char buf[256];
348 
349 		if (ext_data->n_placements) {
350 			repr_placements(buf,
351 					sizeof(buf),
352 					ext_data->placements,
353 					ext_data->n_placements);
354 			drm_dbg(&i915->drm,
355 				"Placements were already set in previous EXT. Existing placements: %s\n",
356 				buf);
357 		}
358 
359 		repr_placements(buf, sizeof(buf), placements, i);
360 		drm_dbg(&i915->drm, "New placements(so far validated): %s\n", buf);
361 	}
362 
363 	return ret;
364 }
365 
ext_set_placements(struct i915_user_extension __user * base,void * data)366 static int ext_set_placements(struct i915_user_extension __user *base,
367 			      void *data)
368 {
369 	struct drm_i915_gem_create_ext_memory_regions ext;
370 
371 	if (copy_from_user(&ext, base, sizeof(ext)))
372 		return -EFAULT;
373 
374 	return set_placements(&ext, data);
375 }
376 
ext_set_protected(struct i915_user_extension __user * base,void * data)377 static int ext_set_protected(struct i915_user_extension __user *base, void *data)
378 {
379 	struct drm_i915_gem_create_ext_protected_content ext;
380 	struct create_ext *ext_data = data;
381 
382 	if (copy_from_user(&ext, base, sizeof(ext)))
383 		return -EFAULT;
384 
385 	if (ext.flags)
386 		return -EINVAL;
387 
388 	if (!intel_pxp_is_enabled(ext_data->i915->pxp))
389 		return -ENODEV;
390 
391 	ext_data->flags |= I915_BO_PROTECTED;
392 
393 	return 0;
394 }
395 
396 static const i915_user_extension_fn create_extensions[] = {
397 	[I915_GEM_CREATE_EXT_MEMORY_REGIONS] = ext_set_placements,
398 	[I915_GEM_CREATE_EXT_PROTECTED_CONTENT] = ext_set_protected,
399 };
400 
401 /**
402  * Creates a new mm object and returns a handle to it.
403  * @dev: drm device pointer
404  * @data: ioctl data blob
405  * @file: drm file pointer
406  */
407 int
i915_gem_create_ext_ioctl(struct drm_device * dev,void * data,struct drm_file * file)408 i915_gem_create_ext_ioctl(struct drm_device *dev, void *data,
409 			  struct drm_file *file)
410 {
411 	struct drm_i915_private *i915 = to_i915(dev);
412 	struct drm_i915_gem_create_ext *args = data;
413 	struct create_ext ext_data = { .i915 = i915 };
414 	struct drm_i915_gem_object *obj;
415 	int ret;
416 
417 	if (args->flags & ~I915_GEM_CREATE_EXT_FLAG_NEEDS_CPU_ACCESS)
418 		return -EINVAL;
419 
420 	ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
421 				   create_extensions,
422 				   ARRAY_SIZE(create_extensions),
423 				   &ext_data);
424 	if (ret)
425 		return ret;
426 
427 	if (!ext_data.n_placements) {
428 		ext_data.placements[0] =
429 			intel_memory_region_by_type(i915, INTEL_MEMORY_SYSTEM);
430 		ext_data.n_placements = 1;
431 	}
432 
433 	if (args->flags & I915_GEM_CREATE_EXT_FLAG_NEEDS_CPU_ACCESS) {
434 		if (ext_data.n_placements == 1)
435 			return -EINVAL;
436 
437 		/*
438 		 * We always need to be able to spill to system memory, if we
439 		 * can't place in the mappable part of LMEM.
440 		 */
441 		if (!(ext_data.placement_mask & BIT(INTEL_REGION_SMEM)))
442 			return -EINVAL;
443 	} else {
444 		if (ext_data.n_placements > 1 ||
445 		    ext_data.placements[0]->type != INTEL_MEMORY_SYSTEM)
446 			ext_data.flags |= I915_BO_ALLOC_GPU_ONLY;
447 	}
448 
449 	obj = __i915_gem_object_create_user_ext(i915, args->size,
450 						ext_data.placements,
451 						ext_data.n_placements,
452 						ext_data.flags);
453 	if (IS_ERR(obj))
454 		return PTR_ERR(obj);
455 
456 	return i915_gem_publish(obj, file, &args->size, &args->handle);
457 }
458