Subversion Repositories Kolibri OS

Rev

Rev 4104 | Rev 4371 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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