Subversion Repositories Kolibri OS

Rev

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