Subversion Repositories Kolibri OS

Rev

Rev 4392 | Rev 4560 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3263 Serge 1
/*
2
 * Copyright © 2008,2010 Intel Corporation
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * copy of this software and associated documentation files (the "Software"),
6
 * to deal in the Software without restriction, including without limitation
7
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice (including the next
12
 * paragraph) shall be included in all copies or substantial portions of the
13
 * Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
 * IN THE SOFTWARE.
22
 *
23
 * Authors:
24
 *    Eric Anholt 
25
 *    Chris Wilson 
26
 *
27
 */
28
 
29
#include 
30
#include 
31
#include "i915_drv.h"
32
#include "i915_trace.h"
33
#include "intel_drv.h"
34
//#include 
35
 
36
 
37
static unsigned long
38
copy_to_user(void __user *to, const void *from, unsigned long n)
39
{
40
    memcpy(to, from, n);
41
    return 0;
42
}
43
 
44
static unsigned long
45
copy_from_user(void *to, const void __user *from, unsigned long n)
46
{
47
    memcpy(to, from, n);
48
    return 0;
49
}
50
 
51
struct eb_objects {
3480 Serge 52
	struct list_head objects;
3263 Serge 53
	int and;
3480 Serge 54
	union {
55
		struct drm_i915_gem_object *lut[0];
3263 Serge 56
	struct hlist_head buckets[0];
3480 Serge 57
	};
3263 Serge 58
};
59
 
60
static struct eb_objects *
3480 Serge 61
eb_create(struct drm_i915_gem_execbuffer2 *args)
3263 Serge 62
{
3480 Serge 63
	struct eb_objects *eb = NULL;
64
 
65
	if (args->flags & I915_EXEC_HANDLE_LUT) {
66
		int size = args->buffer_count;
67
		size *= sizeof(struct drm_i915_gem_object *);
68
		size += sizeof(struct eb_objects);
69
		eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
70
	}
71
 
72
	if (eb == NULL) {
73
		int size = args->buffer_count;
3263 Serge 74
	int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
3480 Serge 75
		BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
76
		while (count > 2*size)
3263 Serge 77
		count >>= 1;
78
	eb = kzalloc(count*sizeof(struct hlist_head) +
79
		     sizeof(struct eb_objects),
3480 Serge 80
			     GFP_TEMPORARY);
3263 Serge 81
	if (eb == NULL)
82
		return eb;
83
 
84
	eb->and = count - 1;
3480 Serge 85
	} else
86
		eb->and = -args->buffer_count;
87
 
88
	INIT_LIST_HEAD(&eb->objects);
3263 Serge 89
	return eb;
90
}
91
 
92
static void
93
eb_reset(struct eb_objects *eb)
94
{
3480 Serge 95
	if (eb->and >= 0)
3263 Serge 96
	memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
97
}
98
 
3480 Serge 99
static int
100
eb_lookup_objects(struct eb_objects *eb,
101
		  struct drm_i915_gem_exec_object2 *exec,
102
		  const struct drm_i915_gem_execbuffer2 *args,
103
		  struct drm_file *file)
3263 Serge 104
{
3480 Serge 105
	int i;
106
 
107
	spin_lock(&file->table_lock);
108
	for (i = 0; i < args->buffer_count; i++) {
109
		struct drm_i915_gem_object *obj;
110
 
111
		    obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
112
		if (obj == NULL) {
113
			spin_unlock(&file->table_lock);
114
			DRM_DEBUG("Invalid object handle %d at index %d\n",
115
				   exec[i].handle, i);
116
			return -ENOENT;
117
		}
118
 
119
		if (!list_empty(&obj->exec_list)) {
120
			spin_unlock(&file->table_lock);
121
			DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
122
				   obj, exec[i].handle, i);
123
			return -EINVAL;
124
		}
125
 
126
		drm_gem_object_reference(&obj->base);
127
		list_add_tail(&obj->exec_list, &eb->objects);
128
 
129
		obj->exec_entry = &exec[i];
130
		if (eb->and < 0) {
131
			eb->lut[i] = obj;
132
		} else {
133
			uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
134
			obj->exec_handle = handle;
3263 Serge 135
	hlist_add_head(&obj->exec_node,
3480 Serge 136
				       &eb->buckets[handle & eb->and]);
137
		}
138
	}
139
	spin_unlock(&file->table_lock);
140
 
141
	return 0;
3263 Serge 142
}
143
 
144
static struct drm_i915_gem_object *
145
eb_get_object(struct eb_objects *eb, unsigned long handle)
146
{
3480 Serge 147
	if (eb->and < 0) {
148
		if (handle >= -eb->and)
149
			return NULL;
150
		return eb->lut[handle];
151
	} else {
3263 Serge 152
	struct hlist_head *head;
153
	struct hlist_node *node;
154
 
155
	head = &eb->buckets[handle & eb->and];
156
	hlist_for_each(node, head) {
3480 Serge 157
			struct drm_i915_gem_object *obj;
158
 
3263 Serge 159
		obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
160
		if (obj->exec_handle == handle)
161
			return obj;
162
	}
163
	return NULL;
3480 Serge 164
	}
3263 Serge 165
}
166
 
167
static void
168
eb_destroy(struct eb_objects *eb)
169
{
3480 Serge 170
	while (!list_empty(&eb->objects)) {
171
		struct drm_i915_gem_object *obj;
172
 
173
		obj = list_first_entry(&eb->objects,
174
				       struct drm_i915_gem_object,
175
				       exec_list);
176
		list_del_init(&obj->exec_list);
177
		drm_gem_object_unreference(&obj->base);
178
	}
3263 Serge 179
	kfree(eb);
180
}
181
 
182
static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
183
{
184
	return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
185
		!obj->map_and_fenceable ||
186
		obj->cache_level != I915_CACHE_NONE);
187
}
188
 
189
static int
4371 Serge 190
relocate_entry_cpu(struct drm_i915_gem_object *obj,
191
		   struct drm_i915_gem_relocation_entry *reloc)
192
{
4539 Serge 193
    struct drm_device *dev = obj->base.dev;
194
    struct drm_i915_private *dev_priv = dev->dev_private;
4371 Serge 195
	uint32_t page_offset = offset_in_page(reloc->offset);
196
	char *vaddr;
197
	int ret = -EINVAL;
198
 
199
	ret = i915_gem_object_set_to_cpu_domain(obj, 1);
200
	if (ret)
201
		return ret;
202
 
4539 Serge 203
    vaddr = dev_priv->gtt.mappable+4096;
204
    MapPage(vaddr,(addr_t)i915_gem_object_get_page(obj,reloc->offset >> PAGE_SHIFT), PG_SW);
4371 Serge 205
	*(uint32_t *)(vaddr + page_offset) = reloc->delta;
206
 
207
	return 0;
208
}
209
 
210
static int
211
relocate_entry_gtt(struct drm_i915_gem_object *obj,
212
		   struct drm_i915_gem_relocation_entry *reloc)
213
{
214
	struct drm_device *dev = obj->base.dev;
215
	struct drm_i915_private *dev_priv = dev->dev_private;
216
	uint32_t __iomem *reloc_entry;
217
	void __iomem *reloc_page;
218
	int ret = -EINVAL;
219
 
220
	ret = i915_gem_object_set_to_gtt_domain(obj, true);
221
	if (ret)
222
		return ret;
223
 
224
	ret = i915_gem_object_put_fence(obj);
225
	if (ret)
226
		return ret;
227
 
228
	/* Map the page containing the relocation we're going to perform.  */
229
	reloc->offset += i915_gem_obj_ggtt_offset(obj);
4539 Serge 230
    MapPage(dev_priv->gtt.mappable,dev_priv->gtt.mappable_base +
231
                                 (reloc->offset & PAGE_MASK), PG_SW);
232
	reloc_page = dev_priv->gtt.mappable;
4371 Serge 233
	reloc_entry = (uint32_t __iomem *)
234
		(reloc_page + offset_in_page(reloc->offset));
235
	iowrite32(reloc->delta, reloc_entry);
236
 
237
	return 0;
238
}
239
 
240
static int
3263 Serge 241
i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
242
				   struct eb_objects *eb,
4104 Serge 243
				   struct drm_i915_gem_relocation_entry *reloc,
244
				   struct i915_address_space *vm)
3263 Serge 245
{
246
	struct drm_device *dev = obj->base.dev;
247
	struct drm_gem_object *target_obj;
248
	struct drm_i915_gem_object *target_i915_obj;
249
	uint32_t target_offset;
250
	int ret = -EINVAL;
251
 
252
	/* we've already hold a reference to all valid objects */
253
	target_obj = &eb_get_object(eb, reloc->target_handle)->base;
254
	if (unlikely(target_obj == NULL))
255
		return -ENOENT;
256
 
257
	target_i915_obj = to_intel_bo(target_obj);
4104 Serge 258
	target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
3263 Serge 259
 
260
	/* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
261
	 * pipe_control writes because the gpu doesn't properly redirect them
262
	 * through the ppgtt for non_secure batchbuffers. */
263
	if (unlikely(IS_GEN6(dev) &&
264
	    reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
265
	    !target_i915_obj->has_global_gtt_mapping)) {
266
		i915_gem_gtt_bind_object(target_i915_obj,
267
					 target_i915_obj->cache_level);
268
	}
269
 
270
	/* Validate that the target is in a valid r/w GPU domain */
271
	if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
272
		DRM_DEBUG("reloc with multiple write domains: "
273
			  "obj %p target %d offset %d "
274
			  "read %08x write %08x",
275
			  obj, reloc->target_handle,
276
			  (int) reloc->offset,
277
			  reloc->read_domains,
278
			  reloc->write_domain);
279
		return ret;
280
	}
281
	if (unlikely((reloc->write_domain | reloc->read_domains)
282
		     & ~I915_GEM_GPU_DOMAINS)) {
283
		DRM_DEBUG("reloc with read/write non-GPU domains: "
284
			  "obj %p target %d offset %d "
285
			  "read %08x write %08x",
286
			  obj, reloc->target_handle,
287
			  (int) reloc->offset,
288
			  reloc->read_domains,
289
			  reloc->write_domain);
290
		return ret;
291
	}
292
 
293
	target_obj->pending_read_domains |= reloc->read_domains;
294
	target_obj->pending_write_domain |= reloc->write_domain;
295
 
296
	/* If the relocation already has the right value in it, no
297
	 * more work needs to be done.
298
	 */
299
	if (target_offset == reloc->presumed_offset)
300
		return 0;
301
 
302
	/* Check that the relocation address is valid... */
303
	if (unlikely(reloc->offset > obj->base.size - 4)) {
304
		DRM_DEBUG("Relocation beyond object bounds: "
305
			  "obj %p target %d offset %d size %d.\n",
306
			  obj, reloc->target_handle,
307
			  (int) reloc->offset,
308
			  (int) obj->base.size);
309
		return ret;
310
	}
311
	if (unlikely(reloc->offset & 3)) {
312
		DRM_DEBUG("Relocation not 4-byte aligned: "
313
			  "obj %p target %d offset %d.\n",
314
			  obj, reloc->target_handle,
315
			  (int) reloc->offset);
316
		return ret;
317
	}
318
 
319
	/* We can't wait for rendering with pagefaults disabled */
320
 
321
	reloc->delta += target_offset;
4371 Serge 322
	if (use_cpu_reloc(obj))
323
		ret = relocate_entry_cpu(obj, reloc);
324
	else
325
		ret = relocate_entry_gtt(obj, reloc);
3263 Serge 326
 
327
		if (ret)
328
			return ret;
329
 
330
	/* and update the user's relocation entry */
331
	reloc->presumed_offset = target_offset;
332
 
333
	return 0;
334
}
335
 
336
static int
337
i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
4104 Serge 338
				    struct eb_objects *eb,
339
				    struct i915_address_space *vm)
3263 Serge 340
{
341
#define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
3266 Serge 342
	struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(64)];
3263 Serge 343
	struct drm_i915_gem_relocation_entry __user *user_relocs;
344
	struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
345
	int remain, ret;
346
 
4539 Serge 347
	user_relocs = to_user_ptr(entry->relocs_ptr);
3263 Serge 348
 
349
	remain = entry->relocation_count;
350
	while (remain) {
351
		struct drm_i915_gem_relocation_entry *r = stack_reloc;
352
		int count = remain;
353
		if (count > ARRAY_SIZE(stack_reloc))
354
			count = ARRAY_SIZE(stack_reloc);
355
		remain -= count;
356
 
357
        memcpy(r, user_relocs, count*sizeof(r[0]));
358
 
359
		do {
360
			u64 offset = r->presumed_offset;
361
 
4104 Serge 362
			ret = i915_gem_execbuffer_relocate_entry(obj, eb, r,
363
								 vm);
3263 Serge 364
			if (ret)
365
				return ret;
366
 
4392 Serge 367
		if (r->presumed_offset != offset)
368
		{
3263 Serge 369
            memcpy(&user_relocs->presumed_offset,
370
                   &r->presumed_offset,
371
                   sizeof(r->presumed_offset));
4392 Serge 372
		}
3263 Serge 373
 
374
			user_relocs++;
375
			r++;
376
		} while (--count);
377
	}
378
 
379
	return 0;
380
#undef N_RELOC
381
}
382
 
383
static int
384
i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
385
					 struct eb_objects *eb,
4104 Serge 386
					 struct drm_i915_gem_relocation_entry *relocs,
387
					 struct i915_address_space *vm)
3263 Serge 388
{
389
	const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
390
	int i, ret;
391
 
392
	for (i = 0; i < entry->relocation_count; i++) {
4104 Serge 393
		ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i],
394
							 vm);
3263 Serge 395
		if (ret)
396
			return ret;
397
	}
398
 
399
	return 0;
400
}
401
 
402
static int
4104 Serge 403
i915_gem_execbuffer_relocate(struct eb_objects *eb,
404
			     struct i915_address_space *vm)
3263 Serge 405
{
406
	struct drm_i915_gem_object *obj;
407
	int ret = 0;
408
 
409
	/* This is the fast path and we cannot handle a pagefault whilst
410
	 * holding the struct mutex lest the user pass in the relocations
411
	 * contained within a mmaped bo. For in such a case we, the page
412
	 * fault handler would call i915_gem_fault() and we would try to
413
	 * acquire the struct mutex again. Obviously this is bad and so
414
	 * lockdep complains vehemently.
415
	 */
4104 Serge 416
//	pagefault_disable();
3480 Serge 417
	list_for_each_entry(obj, &eb->objects, exec_list) {
4104 Serge 418
		ret = i915_gem_execbuffer_relocate_object(obj, eb, vm);
3263 Serge 419
		if (ret)
420
			break;
421
	}
422
//   pagefault_enable();
423
 
424
	return ret;
425
}
426
 
427
#define  __EXEC_OBJECT_HAS_PIN (1<<31)
428
#define  __EXEC_OBJECT_HAS_FENCE (1<<30)
429
 
430
static int
431
need_reloc_mappable(struct drm_i915_gem_object *obj)
432
{
433
	struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
434
	return entry->relocation_count && !use_cpu_reloc(obj);
435
}
436
 
437
static int
438
i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
3480 Serge 439
				   struct intel_ring_buffer *ring,
4104 Serge 440
				   struct i915_address_space *vm,
3480 Serge 441
				   bool *need_reloc)
3263 Serge 442
{
443
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
444
	struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
445
	bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
446
	bool need_fence, need_mappable;
447
	int ret;
448
 
449
	need_fence =
450
		has_fenced_gpu_access &&
451
		entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
452
		obj->tiling_mode != I915_TILING_NONE;
453
	need_mappable = need_fence || need_reloc_mappable(obj);
454
 
4104 Serge 455
	ret = i915_gem_object_pin(obj, vm, entry->alignment, need_mappable,
456
				  false);
3263 Serge 457
	if (ret)
458
		return ret;
459
 
460
	entry->flags |= __EXEC_OBJECT_HAS_PIN;
461
 
462
	if (has_fenced_gpu_access) {
463
		if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
464
			ret = i915_gem_object_get_fence(obj);
465
			if (ret)
466
				return ret;
467
 
468
			if (i915_gem_object_pin_fence(obj))
469
				entry->flags |= __EXEC_OBJECT_HAS_FENCE;
470
 
471
			obj->pending_fenced_gpu_access = true;
472
		}
473
	}
474
 
475
	/* Ensure ppgtt mapping exists if needed */
476
	if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
477
		i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
478
				       obj, obj->cache_level);
479
 
480
		obj->has_aliasing_ppgtt_mapping = 1;
481
	}
482
 
4104 Serge 483
	if (entry->offset != i915_gem_obj_offset(obj, vm)) {
484
		entry->offset = i915_gem_obj_offset(obj, vm);
3480 Serge 485
		*need_reloc = true;
486
	}
3266 Serge 487
 
3480 Serge 488
	if (entry->flags & EXEC_OBJECT_WRITE) {
489
		obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
490
		obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
491
	}
492
 
493
	if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
494
	    !obj->has_global_gtt_mapping)
495
		i915_gem_gtt_bind_object(obj, obj->cache_level);
496
 
3263 Serge 497
	return 0;
498
}
499
 
500
static void
501
i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
502
{
503
	struct drm_i915_gem_exec_object2 *entry;
504
 
4104 Serge 505
	if (!i915_gem_obj_bound_any(obj))
3263 Serge 506
		return;
507
 
508
	entry = obj->exec_entry;
509
 
510
	if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
511
		i915_gem_object_unpin_fence(obj);
512
 
513
	if (entry->flags & __EXEC_OBJECT_HAS_PIN)
514
		i915_gem_object_unpin(obj);
515
 
516
	entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
517
}
518
 
519
static int
520
i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
3480 Serge 521
			    struct list_head *objects,
4104 Serge 522
			    struct i915_address_space *vm,
3480 Serge 523
			    bool *need_relocs)
3263 Serge 524
{
525
	struct drm_i915_gem_object *obj;
526
	struct list_head ordered_objects;
527
	bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
528
	int retry;
529
 
530
	INIT_LIST_HEAD(&ordered_objects);
531
	while (!list_empty(objects)) {
532
		struct drm_i915_gem_exec_object2 *entry;
533
		bool need_fence, need_mappable;
534
 
535
		obj = list_first_entry(objects,
536
				       struct drm_i915_gem_object,
537
				       exec_list);
538
		entry = obj->exec_entry;
539
 
540
		need_fence =
541
			has_fenced_gpu_access &&
542
			entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
543
			obj->tiling_mode != I915_TILING_NONE;
544
		need_mappable = need_fence || need_reloc_mappable(obj);
545
 
546
		if (need_mappable)
547
			list_move(&obj->exec_list, &ordered_objects);
548
		else
549
			list_move_tail(&obj->exec_list, &ordered_objects);
550
 
3480 Serge 551
		obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
3263 Serge 552
		obj->base.pending_write_domain = 0;
553
		obj->pending_fenced_gpu_access = false;
554
	}
555
	list_splice(&ordered_objects, objects);
556
 
557
	/* Attempt to pin all of the buffers into the GTT.
558
	 * This is done in 3 phases:
559
	 *
560
	 * 1a. Unbind all objects that do not match the GTT constraints for
561
	 *     the execbuffer (fenceable, mappable, alignment etc).
562
	 * 1b. Increment pin count for already bound objects.
563
	 * 2.  Bind new objects.
564
	 * 3.  Decrement pin count.
565
	 *
566
	 * This avoid unnecessary unbinding of later objects in order to make
567
	 * room for the earlier objects *unless* we need to defragment.
568
	 */
569
	retry = 0;
570
	do {
571
		int ret = 0;
572
 
573
		/* Unbind any ill-fitting objects or pin. */
574
		list_for_each_entry(obj, objects, exec_list) {
575
			struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
576
			bool need_fence, need_mappable;
4104 Serge 577
			u32 obj_offset;
3263 Serge 578
 
4104 Serge 579
			if (!i915_gem_obj_bound(obj, vm))
3263 Serge 580
				continue;
581
 
4104 Serge 582
			obj_offset = i915_gem_obj_offset(obj, vm);
3263 Serge 583
			need_fence =
584
				has_fenced_gpu_access &&
585
				entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
586
				obj->tiling_mode != I915_TILING_NONE;
587
			need_mappable = need_fence || need_reloc_mappable(obj);
588
 
4246 Serge 589
			WARN_ON((need_mappable || need_fence) &&
590
				!i915_is_ggtt(vm));
4104 Serge 591
 
592
			if ((entry->alignment &&
593
			     obj_offset & (entry->alignment - 1)) ||
3263 Serge 594
			    (need_mappable && !obj->map_and_fenceable))
4104 Serge 595
				ret = i915_vma_unbind(i915_gem_obj_to_vma(obj, vm));
3263 Serge 596
			else
4104 Serge 597
				ret = i915_gem_execbuffer_reserve_object(obj, ring, vm, need_relocs);
3263 Serge 598
			if (ret)
599
				goto err;
600
		}
601
 
602
		/* Bind fresh objects */
603
		list_for_each_entry(obj, objects, exec_list) {
4104 Serge 604
			if (i915_gem_obj_bound(obj, vm))
3263 Serge 605
				continue;
606
 
4104 Serge 607
			ret = i915_gem_execbuffer_reserve_object(obj, ring, vm, need_relocs);
3263 Serge 608
			if (ret)
609
				goto err;
610
		}
611
 
612
err:		/* Decrement pin count for bound objects */
613
		list_for_each_entry(obj, objects, exec_list)
614
			i915_gem_execbuffer_unreserve_object(obj);
615
 
616
		if (ret != -ENOSPC || retry++)
617
			return ret;
618
 
619
//       ret = i915_gem_evict_everything(ring->dev);
620
		if (ret)
621
			return ret;
622
	} while (1);
623
}
624
 
625
static int
626
i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3480 Serge 627
				  struct drm_i915_gem_execbuffer2 *args,
3263 Serge 628
				  struct drm_file *file,
629
				  struct intel_ring_buffer *ring,
630
				  struct eb_objects *eb,
4104 Serge 631
				  struct drm_i915_gem_exec_object2 *exec,
632
				  struct i915_address_space *vm)
3263 Serge 633
{
634
	struct drm_i915_gem_relocation_entry *reloc;
635
	struct drm_i915_gem_object *obj;
3480 Serge 636
	bool need_relocs;
3263 Serge 637
	int *reloc_offset;
638
	int i, total, ret;
3480 Serge 639
	int count = args->buffer_count;
3263 Serge 640
 
641
	/* We may process another execbuffer during the unlock... */
3480 Serge 642
	while (!list_empty(&eb->objects)) {
643
		obj = list_first_entry(&eb->objects,
3263 Serge 644
				       struct drm_i915_gem_object,
645
				       exec_list);
646
		list_del_init(&obj->exec_list);
647
		drm_gem_object_unreference(&obj->base);
648
	}
649
 
650
	mutex_unlock(&dev->struct_mutex);
651
 
652
	total = 0;
653
	for (i = 0; i < count; i++)
654
		total += exec[i].relocation_count;
655
 
656
    reloc_offset = malloc(count * sizeof(*reloc_offset));
657
    reloc = malloc(total * sizeof(*reloc));
658
	if (reloc == NULL || reloc_offset == NULL) {
3266 Serge 659
        kfree(reloc);
660
        kfree(reloc_offset);
3263 Serge 661
		mutex_lock(&dev->struct_mutex);
662
		return -ENOMEM;
663
	}
664
 
665
	total = 0;
666
	for (i = 0; i < count; i++) {
667
		struct drm_i915_gem_relocation_entry __user *user_relocs;
668
		u64 invalid_offset = (u64)-1;
669
		int j;
670
 
4539 Serge 671
		user_relocs = to_user_ptr(exec[i].relocs_ptr);
3263 Serge 672
 
673
		if (copy_from_user(reloc+total, user_relocs,
674
				   exec[i].relocation_count * sizeof(*reloc))) {
675
			ret = -EFAULT;
676
			mutex_lock(&dev->struct_mutex);
677
			goto err;
678
		}
679
 
680
		/* As we do not update the known relocation offsets after
681
		 * relocating (due to the complexities in lock handling),
682
		 * we need to mark them as invalid now so that we force the
683
		 * relocation processing next time. Just in case the target
684
		 * object is evicted and then rebound into its old
685
		 * presumed_offset before the next execbuffer - if that
686
		 * happened we would make the mistake of assuming that the
687
		 * relocations were valid.
688
		 */
689
		for (j = 0; j < exec[i].relocation_count; j++) {
690
			if (copy_to_user(&user_relocs[j].presumed_offset,
691
					 &invalid_offset,
692
					 sizeof(invalid_offset))) {
693
				ret = -EFAULT;
694
				mutex_lock(&dev->struct_mutex);
695
				goto err;
696
			}
697
		}
698
 
699
		reloc_offset[i] = total;
700
		total += exec[i].relocation_count;
701
	}
702
 
703
	ret = i915_mutex_lock_interruptible(dev);
704
	if (ret) {
705
		mutex_lock(&dev->struct_mutex);
706
		goto err;
707
	}
708
 
709
	/* reacquire the objects */
710
	eb_reset(eb);
3480 Serge 711
	ret = eb_lookup_objects(eb, exec, args, file);
712
	if (ret)
3263 Serge 713
			goto err;
714
 
3480 Serge 715
	need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
4104 Serge 716
	ret = i915_gem_execbuffer_reserve(ring, &eb->objects, vm, &need_relocs);
3263 Serge 717
	if (ret)
718
		goto err;
719
 
3480 Serge 720
	list_for_each_entry(obj, &eb->objects, exec_list) {
3263 Serge 721
		int offset = obj->exec_entry - exec;
722
		ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
4104 Serge 723
							       reloc + reloc_offset[offset],
724
							       vm);
3263 Serge 725
		if (ret)
726
			goto err;
727
	}
728
 
729
	/* Leave the user relocations as are, this is the painfully slow path,
730
	 * and we want to avoid the complication of dropping the lock whilst
731
	 * having buffers reserved in the aperture and so causing spurious
732
	 * ENOSPC for random operations.
733
	 */
734
 
735
err:
3266 Serge 736
    kfree(reloc);
737
    kfree(reloc_offset);
3263 Serge 738
	return ret;
739
}
740
 
741
static int
742
i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
743
				struct list_head *objects)
744
{
745
	struct drm_i915_gem_object *obj;
746
	uint32_t flush_domains = 0;
4104 Serge 747
	bool flush_chipset = false;
3263 Serge 748
	int ret;
749
 
750
	list_for_each_entry(obj, objects, exec_list) {
751
		ret = i915_gem_object_sync(obj, ring);
752
		if (ret)
753
			return ret;
754
 
755
		if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
4104 Serge 756
			flush_chipset |= i915_gem_clflush_object(obj, false);
3263 Serge 757
 
758
		flush_domains |= obj->base.write_domain;
759
	}
760
 
4104 Serge 761
	if (flush_chipset)
3263 Serge 762
		i915_gem_chipset_flush(ring->dev);
763
 
764
	if (flush_domains & I915_GEM_DOMAIN_GTT)
765
		wmb();
766
 
767
	/* Unconditionally invalidate gpu caches and ensure that we do flush
768
	 * any residual writes from the previous batch.
769
	 */
770
	return intel_ring_invalidate_all_caches(ring);
771
}
772
 
773
static bool
774
i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
775
{
3480 Serge 776
	if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
777
		return false;
778
 
3263 Serge 779
	return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
780
}
781
 
782
static int
783
validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
784
		   int count)
785
{
786
	int i;
3480 Serge 787
	int relocs_total = 0;
788
	int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
3263 Serge 789
 
790
	for (i = 0; i < count; i++) {
3746 Serge 791
		char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
3263 Serge 792
		int length; /* limited by fault_in_pages_readable() */
793
 
3480 Serge 794
		if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
3263 Serge 795
			return -EINVAL;
796
 
3480 Serge 797
		/* First check for malicious input causing overflow in
798
		 * the worst case where we need to allocate the entire
799
		 * relocation tree as a single array.
800
		 */
801
		if (exec[i].relocation_count > relocs_max - relocs_total)
802
			return -EINVAL;
803
		relocs_total += exec[i].relocation_count;
804
 
3263 Serge 805
		length = exec[i].relocation_count *
806
			sizeof(struct drm_i915_gem_relocation_entry);
3746 Serge 807
		/*
808
		 * We must check that the entire relocation array is safe
809
		 * to read, but since we may need to update the presumed
810
		 * offsets during execution, check for full write access.
811
		 */
3263 Serge 812
 
813
	}
814
 
815
	return 0;
816
}
817
 
818
static void
819
i915_gem_execbuffer_move_to_active(struct list_head *objects,
4104 Serge 820
				   struct i915_address_space *vm,
3263 Serge 821
				   struct intel_ring_buffer *ring)
822
{
823
	struct drm_i915_gem_object *obj;
824
 
825
	list_for_each_entry(obj, objects, exec_list) {
826
		u32 old_read = obj->base.read_domains;
827
		u32 old_write = obj->base.write_domain;
828
 
3480 Serge 829
		obj->base.write_domain = obj->base.pending_write_domain;
830
		if (obj->base.write_domain == 0)
831
			obj->base.pending_read_domains |= obj->base.read_domains;
3263 Serge 832
		obj->base.read_domains = obj->base.pending_read_domains;
833
		obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
834
 
4104 Serge 835
		/* FIXME: This lookup gets fixed later <-- danvet */
836
		list_move_tail(&i915_gem_obj_to_vma(obj, vm)->mm_list, &vm->active_list);
3263 Serge 837
		i915_gem_object_move_to_active(obj, ring);
838
		if (obj->base.write_domain) {
839
			obj->dirty = 1;
840
			obj->last_write_seqno = intel_ring_get_seqno(ring);
841
			if (obj->pin_count) /* check for potential scanout */
4104 Serge 842
				intel_mark_fb_busy(obj, ring);
3263 Serge 843
		}
844
 
845
		trace_i915_gem_object_change_domain(obj, old_read, old_write);
846
	}
847
}
848
 
849
static void
850
i915_gem_execbuffer_retire_commands(struct drm_device *dev,
851
				    struct drm_file *file,
4104 Serge 852
				    struct intel_ring_buffer *ring,
853
				    struct drm_i915_gem_object *obj)
3263 Serge 854
{
855
	/* Unconditionally force add_request to emit a full flush. */
856
	ring->gpu_caches_dirty = true;
857
 
858
	/* Add a breadcrumb for the completion of the batch buffer */
4104 Serge 859
	(void)__i915_add_request(ring, file, obj, NULL);
3263 Serge 860
}
861
 
862
static int
863
i915_reset_gen7_sol_offsets(struct drm_device *dev,
864
			    struct intel_ring_buffer *ring)
865
{
866
	drm_i915_private_t *dev_priv = dev->dev_private;
867
	int ret, i;
868
 
869
	if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
870
		return 0;
871
 
872
	ret = intel_ring_begin(ring, 4 * 3);
873
	if (ret)
874
		return ret;
875
 
876
	for (i = 0; i < 4; i++) {
877
		intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
878
		intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
879
		intel_ring_emit(ring, 0);
880
	}
881
 
882
	intel_ring_advance(ring);
883
 
884
	return 0;
885
}
886
 
887
static int
888
i915_gem_do_execbuffer(struct drm_device *dev, void *data,
889
		       struct drm_file *file,
890
		       struct drm_i915_gem_execbuffer2 *args,
4104 Serge 891
		       struct drm_i915_gem_exec_object2 *exec,
892
		       struct i915_address_space *vm)
3263 Serge 893
{
894
	drm_i915_private_t *dev_priv = dev->dev_private;
895
	struct eb_objects *eb;
896
	struct drm_i915_gem_object *batch_obj;
897
	struct drm_clip_rect *cliprects = NULL;
898
	struct intel_ring_buffer *ring;
899
	u32 ctx_id = i915_execbuffer2_get_context_id(*args);
900
	u32 exec_start, exec_len;
3480 Serge 901
	u32 mask, flags;
3263 Serge 902
	int ret, mode, i;
3480 Serge 903
	bool need_relocs;
3263 Serge 904
 
3480 Serge 905
	if (!i915_gem_check_execbuffer(args))
3263 Serge 906
		return -EINVAL;
907
 
908
	ret = validate_exec_list(exec, args->buffer_count);
909
	if (ret)
910
		return ret;
911
 
912
	flags = 0;
913
	if (args->flags & I915_EXEC_SECURE) {
914
 
915
		flags |= I915_DISPATCH_SECURE;
916
	}
917
	if (args->flags & I915_EXEC_IS_PINNED)
918
		flags |= I915_DISPATCH_PINNED;
919
 
920
	switch (args->flags & I915_EXEC_RING_MASK) {
921
	case I915_EXEC_DEFAULT:
922
	case I915_EXEC_RENDER:
923
		ring = &dev_priv->ring[RCS];
924
		break;
925
	case I915_EXEC_BSD:
926
		ring = &dev_priv->ring[VCS];
4104 Serge 927
		if (ctx_id != DEFAULT_CONTEXT_ID) {
3263 Serge 928
			DRM_DEBUG("Ring %s doesn't support contexts\n",
929
				  ring->name);
930
			return -EPERM;
931
		}
932
		break;
933
	case I915_EXEC_BLT:
934
		ring = &dev_priv->ring[BCS];
4104 Serge 935
		if (ctx_id != DEFAULT_CONTEXT_ID) {
3263 Serge 936
			DRM_DEBUG("Ring %s doesn't support contexts\n",
937
				  ring->name);
938
			return -EPERM;
939
		}
940
		break;
4104 Serge 941
	case I915_EXEC_VEBOX:
942
		ring = &dev_priv->ring[VECS];
943
		if (ctx_id != DEFAULT_CONTEXT_ID) {
944
			DRM_DEBUG("Ring %s doesn't support contexts\n",
945
				  ring->name);
946
			return -EPERM;
947
		}
948
		break;
949
 
3263 Serge 950
	default:
951
		DRM_DEBUG("execbuf with unknown ring: %d\n",
952
			  (int)(args->flags & I915_EXEC_RING_MASK));
953
		return -EINVAL;
954
	}
955
	if (!intel_ring_initialized(ring)) {
956
		DRM_DEBUG("execbuf with invalid ring: %d\n",
957
			  (int)(args->flags & I915_EXEC_RING_MASK));
958
		return -EINVAL;
959
	}
960
 
961
	mode = args->flags & I915_EXEC_CONSTANTS_MASK;
962
	mask = I915_EXEC_CONSTANTS_MASK;
963
	switch (mode) {
964
	case I915_EXEC_CONSTANTS_REL_GENERAL:
965
	case I915_EXEC_CONSTANTS_ABSOLUTE:
966
	case I915_EXEC_CONSTANTS_REL_SURFACE:
967
		if (ring == &dev_priv->ring[RCS] &&
968
		    mode != dev_priv->relative_constants_mode) {
969
			if (INTEL_INFO(dev)->gen < 4)
970
				return -EINVAL;
971
 
972
			if (INTEL_INFO(dev)->gen > 5 &&
973
			    mode == I915_EXEC_CONSTANTS_REL_SURFACE)
974
				return -EINVAL;
975
 
976
			/* The HW changed the meaning on this bit on gen6 */
977
			if (INTEL_INFO(dev)->gen >= 6)
978
				mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
979
		}
980
		break;
981
	default:
982
		DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
983
		return -EINVAL;
984
	}
985
 
986
	if (args->buffer_count < 1) {
987
		DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
988
		return -EINVAL;
989
	}
990
 
991
	if (args->num_cliprects != 0) {
992
		if (ring != &dev_priv->ring[RCS]) {
993
			DRM_DEBUG("clip rectangles are only valid with the render ring\n");
994
			return -EINVAL;
995
		}
996
 
997
		if (INTEL_INFO(dev)->gen >= 5) {
998
			DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
999
			return -EINVAL;
1000
		}
1001
 
1002
		if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
1003
			DRM_DEBUG("execbuf with %u cliprects\n",
1004
				  args->num_cliprects);
1005
			return -EINVAL;
1006
		}
1007
 
1008
		cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
1009
				    GFP_KERNEL);
1010
		if (cliprects == NULL) {
1011
			ret = -ENOMEM;
1012
			goto pre_mutex_err;
1013
		}
1014
 
1015
		if (copy_from_user(cliprects,
3746 Serge 1016
				   to_user_ptr(args->cliprects_ptr),
3263 Serge 1017
				     sizeof(*cliprects)*args->num_cliprects)) {
1018
			ret = -EFAULT;
1019
			goto pre_mutex_err;
1020
		}
1021
	}
1022
 
1023
	ret = i915_mutex_lock_interruptible(dev);
1024
	if (ret)
1025
		goto pre_mutex_err;
1026
 
4104 Serge 1027
	if (dev_priv->ums.mm_suspended) {
3263 Serge 1028
		mutex_unlock(&dev->struct_mutex);
1029
		ret = -EBUSY;
1030
		goto pre_mutex_err;
1031
	}
1032
 
3480 Serge 1033
	eb = eb_create(args);
3263 Serge 1034
	if (eb == NULL) {
1035
		mutex_unlock(&dev->struct_mutex);
1036
		ret = -ENOMEM;
1037
		goto pre_mutex_err;
1038
	}
1039
 
1040
	/* Look up object handles */
3480 Serge 1041
	ret = eb_lookup_objects(eb, exec, args, file);
1042
	if (ret)
3263 Serge 1043
			goto err;
1044
 
1045
	/* take note of the batch buffer before we might reorder the lists */
3480 Serge 1046
	batch_obj = list_entry(eb->objects.prev,
3263 Serge 1047
			       struct drm_i915_gem_object,
1048
			       exec_list);
1049
 
1050
	/* Move the objects en-masse into the GTT, evicting if necessary. */
3480 Serge 1051
	need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
4104 Serge 1052
	ret = i915_gem_execbuffer_reserve(ring, &eb->objects, vm, &need_relocs);
3263 Serge 1053
	if (ret)
1054
		goto err;
1055
 
1056
	/* The objects are in their final locations, apply the relocations. */
3480 Serge 1057
	if (need_relocs)
4104 Serge 1058
		ret = i915_gem_execbuffer_relocate(eb, vm);
3263 Serge 1059
	if (ret) {
1060
		if (ret == -EFAULT) {
3480 Serge 1061
			ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
4104 Serge 1062
								eb, exec, vm);
3263 Serge 1063
			BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1064
		}
1065
		if (ret)
1066
			goto err;
1067
	}
1068
 
1069
	/* Set the pending read domains for the batch buffer to COMMAND */
1070
	if (batch_obj->base.pending_write_domain) {
1071
		DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
1072
		ret = -EINVAL;
1073
		goto err;
1074
	}
1075
	batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
1076
 
1077
	/* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
1078
	 * batch" bit. Hence we need to pin secure batches into the global gtt.
1079
	 * hsw should have this fixed, but let's be paranoid and do it
1080
	 * unconditionally for now. */
1081
	if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
1082
		i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
1083
 
3480 Serge 1084
	ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->objects);
3263 Serge 1085
	if (ret)
1086
		goto err;
1087
 
1088
	ret = i915_switch_context(ring, file, ctx_id);
1089
	if (ret)
1090
		goto err;
1091
 
1092
	if (ring == &dev_priv->ring[RCS] &&
1093
	    mode != dev_priv->relative_constants_mode) {
1094
		ret = intel_ring_begin(ring, 4);
1095
		if (ret)
1096
				goto err;
1097
 
1098
		intel_ring_emit(ring, MI_NOOP);
1099
		intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
1100
		intel_ring_emit(ring, INSTPM);
1101
		intel_ring_emit(ring, mask << 16 | mode);
1102
		intel_ring_advance(ring);
1103
 
1104
		dev_priv->relative_constants_mode = mode;
1105
	}
1106
 
1107
	if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
1108
		ret = i915_reset_gen7_sol_offsets(dev, ring);
1109
		if (ret)
1110
			goto err;
1111
	}
1112
 
4104 Serge 1113
	exec_start = i915_gem_obj_offset(batch_obj, vm) +
1114
		args->batch_start_offset;
3263 Serge 1115
	exec_len = args->batch_len;
1116
	if (cliprects) {
4246 Serge 1117
		for (i = 0; i < args->num_cliprects; i++) {
1118
			ret = i915_emit_box(dev, &cliprects[i],
1119
					    args->DR1, args->DR4);
1120
			if (ret)
1121
				goto err;
3263 Serge 1122
 
4246 Serge 1123
			ret = ring->dispatch_execbuffer(ring,
1124
							exec_start, exec_len,
1125
							flags);
1126
			if (ret)
1127
				goto err;
1128
		}
3263 Serge 1129
	} else {
1130
		ret = ring->dispatch_execbuffer(ring,
1131
						exec_start, exec_len,
1132
						flags);
1133
		if (ret)
1134
			goto err;
1135
	}
1136
 
3266 Serge 1137
	trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
3263 Serge 1138
 
4104 Serge 1139
	i915_gem_execbuffer_move_to_active(&eb->objects, vm, ring);
1140
	i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
3266 Serge 1141
 
3263 Serge 1142
err:
1143
	eb_destroy(eb);
1144
 
1145
	mutex_unlock(&dev->struct_mutex);
1146
 
1147
pre_mutex_err:
4104 Serge 1148
    kfree(cliprects);
3263 Serge 1149
	return ret;
1150
}
1151
 
4246 Serge 1152
#if 0
1153
/*
1154
 * Legacy execbuffer just creates an exec2 list from the original exec object
1155
 * list array and passes it to the real function.
1156
 */
1157
int
1158
i915_gem_execbuffer(struct drm_device *dev, void *data,
1159
		    struct drm_file *file)
1160
{
1161
	struct drm_i915_private *dev_priv = dev->dev_private;
1162
	struct drm_i915_gem_execbuffer *args = data;
1163
	struct drm_i915_gem_execbuffer2 exec2;
1164
	struct drm_i915_gem_exec_object *exec_list = NULL;
1165
	struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1166
	int ret, i;
3480 Serge 1167
 
4246 Serge 1168
	if (args->buffer_count < 1) {
1169
		DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
1170
		return -EINVAL;
1171
	}
3480 Serge 1172
 
4246 Serge 1173
	/* Copy in the exec list from userland */
1174
	exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
1175
	exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
1176
	if (exec_list == NULL || exec2_list == NULL) {
1177
		DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1178
			  args->buffer_count);
1179
		drm_free_large(exec_list);
1180
		drm_free_large(exec2_list);
1181
		return -ENOMEM;
1182
	}
1183
	ret = copy_from_user(exec_list,
1184
			     to_user_ptr(args->buffers_ptr),
1185
			     sizeof(*exec_list) * args->buffer_count);
1186
	if (ret != 0) {
1187
		DRM_DEBUG("copy %d exec entries failed %d\n",
1188
			  args->buffer_count, ret);
1189
		drm_free_large(exec_list);
1190
		drm_free_large(exec2_list);
1191
		return -EFAULT;
1192
	}
1193
 
1194
	for (i = 0; i < args->buffer_count; i++) {
1195
		exec2_list[i].handle = exec_list[i].handle;
1196
		exec2_list[i].relocation_count = exec_list[i].relocation_count;
1197
		exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
1198
		exec2_list[i].alignment = exec_list[i].alignment;
1199
		exec2_list[i].offset = exec_list[i].offset;
1200
		if (INTEL_INFO(dev)->gen < 4)
1201
			exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
1202
		else
1203
			exec2_list[i].flags = 0;
1204
	}
1205
 
1206
	exec2.buffers_ptr = args->buffers_ptr;
1207
	exec2.buffer_count = args->buffer_count;
1208
	exec2.batch_start_offset = args->batch_start_offset;
1209
	exec2.batch_len = args->batch_len;
1210
	exec2.DR1 = args->DR1;
1211
	exec2.DR4 = args->DR4;
1212
	exec2.num_cliprects = args->num_cliprects;
1213
	exec2.cliprects_ptr = args->cliprects_ptr;
1214
	exec2.flags = I915_EXEC_RENDER;
1215
	i915_execbuffer2_set_context_id(exec2, 0);
1216
 
1217
	ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list,
1218
				     &dev_priv->gtt.base);
1219
	if (!ret) {
1220
		/* Copy the new buffer offsets back to the user's exec list. */
1221
		for (i = 0; i < args->buffer_count; i++)
1222
			exec_list[i].offset = exec2_list[i].offset;
1223
		/* ... and back out to userspace */
1224
		ret = copy_to_user(to_user_ptr(args->buffers_ptr),
1225
				   exec_list,
1226
				   sizeof(*exec_list) * args->buffer_count);
1227
		if (ret) {
1228
			ret = -EFAULT;
1229
			DRM_DEBUG("failed to copy %d exec entries "
1230
				  "back to user (%d)\n",
1231
				  args->buffer_count, ret);
1232
		}
1233
	}
1234
 
1235
	drm_free_large(exec_list);
1236
	drm_free_large(exec2_list);
1237
	return ret;
1238
}
1239
#endif
1240
 
3263 Serge 1241
int
1242
i915_gem_execbuffer2(struct drm_device *dev, void *data,
1243
		     struct drm_file *file)
1244
{
4104 Serge 1245
	struct drm_i915_private *dev_priv = dev->dev_private;
3263 Serge 1246
	struct drm_i915_gem_execbuffer2 *args = data;
1247
	struct drm_i915_gem_exec_object2 *exec2_list = NULL;
1248
	int ret;
1249
 
1250
	if (args->buffer_count < 1 ||
1251
	    args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
1252
		DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
1253
		return -EINVAL;
1254
	}
1255
 
3480 Serge 1256
	exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
1257
			     GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
3263 Serge 1258
	if (exec2_list == NULL) {
1259
		DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
1260
			  args->buffer_count);
1261
		return -ENOMEM;
1262
	}
1263
	ret = copy_from_user(exec2_list,
4539 Serge 1264
			     to_user_ptr(args->buffers_ptr),
3263 Serge 1265
			     sizeof(*exec2_list) * args->buffer_count);
1266
	if (ret != 0) {
1267
		DRM_DEBUG("copy %d exec entries failed %d\n",
1268
			  args->buffer_count, ret);
3266 Serge 1269
        kfree(exec2_list);
1270
        FAIL();
3263 Serge 1271
		return -EFAULT;
1272
	}
1273
 
4104 Serge 1274
	ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list,
1275
				     &dev_priv->gtt.base);
3263 Serge 1276
	if (!ret) {
1277
		/* Copy the new buffer offsets back to the user's exec list. */
4539 Serge 1278
		ret = copy_to_user(to_user_ptr(args->buffers_ptr),
3263 Serge 1279
				   exec2_list,
1280
				   sizeof(*exec2_list) * args->buffer_count);
1281
		if (ret) {
1282
			ret = -EFAULT;
1283
			DRM_DEBUG("failed to copy %d exec entries "
1284
				  "back to user (%d)\n",
1285
				  args->buffer_count, ret);
1286
		}
1287
	}
1288
 
3266 Serge 1289
    kfree(exec2_list);
3263 Serge 1290
	return ret;
1291
}