Subversion Repositories Kolibri OS

Rev

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

Rev 3482 Rev 3746
1
/*
1
/*
2
 * Copyright © 2008 Intel Corporation
2
 * Copyright © 2008 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
 *
25
 *
26
 */
26
 */
27
 
27
 
28
#include 
28
#include 
29
#include 
29
#include 
30
#include "i915_drv.h"
30
#include "i915_drv.h"
31
#include "i915_trace.h"
31
#include "i915_trace.h"
32
#include "intel_drv.h"
32
#include "intel_drv.h"
33
#include 
33
#include 
34
#include 
34
#include 
35
//#include 
35
//#include 
-
 
36
#include 
36
#include 
37
#include 
37
 
38
 
38
extern int x86_clflush_size;
39
extern int x86_clflush_size;
39
 
40
 
40
#define PROT_READ       0x1             /* page can be read */
41
#define PROT_READ       0x1             /* page can be read */
41
#define PROT_WRITE      0x2             /* page can be written */
42
#define PROT_WRITE      0x2             /* page can be written */
42
#define MAP_SHARED      0x01            /* Share changes */
43
#define MAP_SHARED      0x01            /* Share changes */
43
 
44
 
44
#undef mb
45
#undef mb
45
#undef rmb
46
#undef rmb
46
#undef wmb
47
#undef wmb
47
#define mb() asm volatile("mfence")
48
#define mb() asm volatile("mfence")
48
#define rmb() asm volatile ("lfence")
49
#define rmb() asm volatile ("lfence")
49
#define wmb() asm volatile ("sfence")
50
#define wmb() asm volatile ("sfence")
50
 
51
 
51
struct drm_i915_gem_object *get_fb_obj();
52
struct drm_i915_gem_object *get_fb_obj();
52
 
53
 
53
unsigned long vm_mmap(struct file *file, unsigned long addr,
54
unsigned long vm_mmap(struct file *file, unsigned long addr,
54
         unsigned long len, unsigned long prot,
55
         unsigned long len, unsigned long prot,
55
         unsigned long flag, unsigned long offset);
56
         unsigned long flag, unsigned long offset);
56
 
57
 
57
static inline void clflush(volatile void *__p)
58
static inline void clflush(volatile void *__p)
58
{
59
{
59
    asm volatile("clflush %0" : "+m" (*(volatile char*)__p));
60
    asm volatile("clflush %0" : "+m" (*(volatile char*)__p));
60
}
61
}
61
 
62
 
62
#define MAX_ERRNO       4095
63
#define MAX_ERRNO       4095
63
 
64
 
64
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
65
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
65
 
66
 
66
 
67
 
67
#define I915_EXEC_CONSTANTS_MASK        (3<<6)
68
#define I915_EXEC_CONSTANTS_MASK        (3<<6)
68
#define I915_EXEC_CONSTANTS_REL_GENERAL (0<<6) /* default */
69
#define I915_EXEC_CONSTANTS_REL_GENERAL (0<<6) /* default */
69
#define I915_EXEC_CONSTANTS_ABSOLUTE    (1<<6)
70
#define I915_EXEC_CONSTANTS_ABSOLUTE    (1<<6)
70
#define I915_EXEC_CONSTANTS_REL_SURFACE (2<<6) /* gen4/5 only */
71
#define I915_EXEC_CONSTANTS_REL_SURFACE (2<<6) /* gen4/5 only */
71
 
72
 
72
static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
73
static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
73
static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
74
static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
74
static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
75
static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
75
						    unsigned alignment,
76
						    unsigned alignment,
76
						    bool map_and_fenceable,
77
						    bool map_and_fenceable,
77
						    bool nonblocking);
78
						    bool nonblocking);
78
static int i915_gem_phys_pwrite(struct drm_device *dev,
79
static int i915_gem_phys_pwrite(struct drm_device *dev,
79
				struct drm_i915_gem_object *obj,
80
				struct drm_i915_gem_object *obj,
80
				struct drm_i915_gem_pwrite *args,
81
				struct drm_i915_gem_pwrite *args,
81
				struct drm_file *file);
82
				struct drm_file *file);
82
 
83
 
83
static void i915_gem_write_fence(struct drm_device *dev, int reg,
84
static void i915_gem_write_fence(struct drm_device *dev, int reg,
84
				 struct drm_i915_gem_object *obj);
85
				 struct drm_i915_gem_object *obj);
85
static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
86
static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
86
					 struct drm_i915_fence_reg *fence,
87
					 struct drm_i915_fence_reg *fence,
87
					 bool enable);
88
					 bool enable);
88
 
89
 
89
static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
90
static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
90
static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
91
static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
91
static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
92
static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
92
 
93
 
93
static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
94
static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
94
{
95
{
95
	if (obj->tiling_mode)
96
	if (obj->tiling_mode)
96
		i915_gem_release_mmap(obj);
97
		i915_gem_release_mmap(obj);
97
 
98
 
98
	/* As we do not have an associated fence register, we will force
99
	/* As we do not have an associated fence register, we will force
99
	 * a tiling change if we ever need to acquire one.
100
	 * a tiling change if we ever need to acquire one.
100
	 */
101
	 */
101
	obj->fence_dirty = false;
102
	obj->fence_dirty = false;
102
	obj->fence_reg = I915_FENCE_REG_NONE;
103
	obj->fence_reg = I915_FENCE_REG_NONE;
103
}
104
}
104
 
105
 
105
/* some bookkeeping */
106
/* some bookkeeping */
106
static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
107
static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
107
				  size_t size)
108
				  size_t size)
108
{
109
{
109
	dev_priv->mm.object_count++;
110
	dev_priv->mm.object_count++;
110
	dev_priv->mm.object_memory += size;
111
	dev_priv->mm.object_memory += size;
111
}
112
}
112
 
113
 
113
static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
114
static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
114
				     size_t size)
115
				     size_t size)
115
{
116
{
116
	dev_priv->mm.object_count--;
117
	dev_priv->mm.object_count--;
117
	dev_priv->mm.object_memory -= size;
118
	dev_priv->mm.object_memory -= size;
118
}
119
}
119
 
120
 
120
static int
121
static int
121
i915_gem_wait_for_error(struct i915_gpu_error *error)
122
i915_gem_wait_for_error(struct i915_gpu_error *error)
122
{
123
{
123
	int ret;
124
	int ret;
124
 
125
 
125
#define EXIT_COND (!i915_reset_in_progress(error))
126
#define EXIT_COND (!i915_reset_in_progress(error))
126
	if (EXIT_COND)
127
	if (EXIT_COND)
127
		return 0;
128
		return 0;
128
#if 0
129
#if 0
129
	/*
130
	/*
130
	 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
131
	 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
131
	 * userspace. If it takes that long something really bad is going on and
132
	 * userspace. If it takes that long something really bad is going on and
132
	 * we should simply try to bail out and fail as gracefully as possible.
133
	 * we should simply try to bail out and fail as gracefully as possible.
133
	 */
134
	 */
134
	ret = wait_event_interruptible_timeout(error->reset_queue,
135
	ret = wait_event_interruptible_timeout(error->reset_queue,
135
					       EXIT_COND,
136
					       EXIT_COND,
136
					       10*HZ);
137
					       10*HZ);
137
	if (ret == 0) {
138
	if (ret == 0) {
138
		DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
139
		DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
139
		return -EIO;
140
		return -EIO;
140
	} else if (ret < 0) {
141
	} else if (ret < 0) {
141
		return ret;
142
		return ret;
142
	}
143
	}
143
 
144
 
144
#endif
145
#endif
145
#undef EXIT_COND
146
#undef EXIT_COND
146
 
147
 
147
	return 0;
148
	return 0;
148
}
149
}
149
 
150
 
150
int i915_mutex_lock_interruptible(struct drm_device *dev)
151
int i915_mutex_lock_interruptible(struct drm_device *dev)
151
{
152
{
152
	struct drm_i915_private *dev_priv = dev->dev_private;
153
	struct drm_i915_private *dev_priv = dev->dev_private;
153
	int ret;
154
	int ret;
154
 
155
 
155
	ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
156
	ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
156
	if (ret)
157
	if (ret)
157
		return ret;
158
		return ret;
158
 
159
 
159
	ret = mutex_lock_interruptible(&dev->struct_mutex);
160
	ret = mutex_lock_interruptible(&dev->struct_mutex);
160
	if (ret)
161
	if (ret)
161
		return ret;
162
		return ret;
162
 
163
 
163
	WARN_ON(i915_verify_lists(dev));
164
	WARN_ON(i915_verify_lists(dev));
164
	return 0;
165
	return 0;
165
}
166
}
166
 
167
 
167
static inline bool
168
static inline bool
168
i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
169
i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
169
{
170
{
170
	return obj->gtt_space && !obj->active;
171
	return obj->gtt_space && !obj->active;
171
}
172
}
172
 
173
 
173
 
174
 
174
#if 0
175
#if 0
175
 
176
 
176
int
177
int
177
i915_gem_init_ioctl(struct drm_device *dev, void *data,
178
i915_gem_init_ioctl(struct drm_device *dev, void *data,
178
		    struct drm_file *file)
179
		    struct drm_file *file)
179
{
180
{
180
	struct drm_i915_private *dev_priv = dev->dev_private;
181
	struct drm_i915_private *dev_priv = dev->dev_private;
181
	struct drm_i915_gem_init *args = data;
182
	struct drm_i915_gem_init *args = data;
182
 
183
 
183
	if (drm_core_check_feature(dev, DRIVER_MODESET))
184
	if (drm_core_check_feature(dev, DRIVER_MODESET))
184
		return -ENODEV;
185
		return -ENODEV;
185
 
186
 
186
	if (args->gtt_start >= args->gtt_end ||
187
	if (args->gtt_start >= args->gtt_end ||
187
	    (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
188
	    (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
188
		return -EINVAL;
189
		return -EINVAL;
189
 
190
 
190
	/* GEM with user mode setting was never supported on ilk and later. */
191
	/* GEM with user mode setting was never supported on ilk and later. */
191
	if (INTEL_INFO(dev)->gen >= 5)
192
	if (INTEL_INFO(dev)->gen >= 5)
192
		return -ENODEV;
193
		return -ENODEV;
193
 
194
 
194
	mutex_lock(&dev->struct_mutex);
195
	mutex_lock(&dev->struct_mutex);
195
	i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
196
	i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
196
				  args->gtt_end);
197
				  args->gtt_end);
197
	dev_priv->gtt.mappable_end = args->gtt_end;
198
	dev_priv->gtt.mappable_end = args->gtt_end;
198
	mutex_unlock(&dev->struct_mutex);
199
	mutex_unlock(&dev->struct_mutex);
199
 
200
 
200
	return 0;
201
	return 0;
201
}
202
}
202
#endif
203
#endif
203
 
204
 
204
int
205
int
205
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
206
i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
206
			    struct drm_file *file)
207
			    struct drm_file *file)
207
{
208
{
208
	struct drm_i915_private *dev_priv = dev->dev_private;
209
	struct drm_i915_private *dev_priv = dev->dev_private;
209
	struct drm_i915_gem_get_aperture *args = data;
210
	struct drm_i915_gem_get_aperture *args = data;
210
	struct drm_i915_gem_object *obj;
211
	struct drm_i915_gem_object *obj;
211
	size_t pinned;
212
	size_t pinned;
212
 
213
 
213
	pinned = 0;
214
	pinned = 0;
214
	mutex_lock(&dev->struct_mutex);
215
	mutex_lock(&dev->struct_mutex);
215
	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
216
	list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list)
216
		if (obj->pin_count)
217
		if (obj->pin_count)
217
		pinned += obj->gtt_space->size;
218
		pinned += obj->gtt_space->size;
218
	mutex_unlock(&dev->struct_mutex);
219
	mutex_unlock(&dev->struct_mutex);
219
 
220
 
220
	args->aper_size = dev_priv->gtt.total;
221
	args->aper_size = dev_priv->gtt.total;
221
	args->aper_available_size = args->aper_size - pinned;
222
	args->aper_available_size = args->aper_size - pinned;
222
 
223
 
223
	return 0;
224
	return 0;
224
}
225
}
225
 
226
 
226
void *i915_gem_object_alloc(struct drm_device *dev)
227
void *i915_gem_object_alloc(struct drm_device *dev)
227
{
228
{
228
	struct drm_i915_private *dev_priv = dev->dev_private;
229
	struct drm_i915_private *dev_priv = dev->dev_private;
229
	return kmalloc(sizeof(struct drm_i915_gem_object), 0);
230
	return kmalloc(sizeof(struct drm_i915_gem_object), 0);
230
}
231
}
231
 
232
 
232
void i915_gem_object_free(struct drm_i915_gem_object *obj)
233
void i915_gem_object_free(struct drm_i915_gem_object *obj)
233
{
234
{
234
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
235
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
235
	kfree(obj);
236
	kfree(obj);
236
}
237
}
237
 
238
 
238
static int
239
static int
239
i915_gem_create(struct drm_file *file,
240
i915_gem_create(struct drm_file *file,
240
		struct drm_device *dev,
241
		struct drm_device *dev,
241
		uint64_t size,
242
		uint64_t size,
242
		uint32_t *handle_p)
243
		uint32_t *handle_p)
243
{
244
{
244
	struct drm_i915_gem_object *obj;
245
	struct drm_i915_gem_object *obj;
245
	int ret;
246
	int ret;
246
	u32 handle;
247
	u32 handle;
247
 
248
 
248
	size = roundup(size, PAGE_SIZE);
249
	size = roundup(size, PAGE_SIZE);
249
	if (size == 0)
250
	if (size == 0)
250
		return -EINVAL;
251
		return -EINVAL;
251
 
252
 
252
	/* Allocate the new object */
253
	/* Allocate the new object */
253
	obj = i915_gem_alloc_object(dev, size);
254
	obj = i915_gem_alloc_object(dev, size);
254
	if (obj == NULL)
255
	if (obj == NULL)
255
		return -ENOMEM;
256
		return -ENOMEM;
256
 
257
 
257
	ret = drm_gem_handle_create(file, &obj->base, &handle);
258
	ret = drm_gem_handle_create(file, &obj->base, &handle);
258
	if (ret) {
259
	if (ret) {
259
		drm_gem_object_release(&obj->base);
260
		drm_gem_object_release(&obj->base);
260
		i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
261
		i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
261
		kfree(obj);
262
		kfree(obj);
262
		return ret;
263
		return ret;
263
	}
264
	}
264
 
265
 
265
	/* drop reference from allocate - handle holds it now */
266
	/* drop reference from allocate - handle holds it now */
266
	drm_gem_object_unreference(&obj->base);
267
	drm_gem_object_unreference(&obj->base);
267
	trace_i915_gem_object_create(obj);
268
	trace_i915_gem_object_create(obj);
268
 
269
 
269
	*handle_p = handle;
270
	*handle_p = handle;
270
	return 0;
271
	return 0;
271
}
272
}
272
 
273
 
273
int
274
int
274
i915_gem_dumb_create(struct drm_file *file,
275
i915_gem_dumb_create(struct drm_file *file,
275
		     struct drm_device *dev,
276
		     struct drm_device *dev,
276
		     struct drm_mode_create_dumb *args)
277
		     struct drm_mode_create_dumb *args)
277
{
278
{
278
	/* have to work out size/pitch and return them */
279
	/* have to work out size/pitch and return them */
279
	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
280
	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
280
	args->size = args->pitch * args->height;
281
	args->size = args->pitch * args->height;
281
	return i915_gem_create(file, dev,
282
	return i915_gem_create(file, dev,
282
			       args->size, &args->handle);
283
			       args->size, &args->handle);
283
}
284
}
284
 
285
 
285
int i915_gem_dumb_destroy(struct drm_file *file,
286
int i915_gem_dumb_destroy(struct drm_file *file,
286
			  struct drm_device *dev,
287
			  struct drm_device *dev,
287
			  uint32_t handle)
288
			  uint32_t handle)
288
{
289
{
289
	return drm_gem_handle_delete(file, handle);
290
	return drm_gem_handle_delete(file, handle);
290
}
291
}
291
 
292
 
292
/**
293
/**
293
 * Creates a new mm object and returns a handle to it.
294
 * Creates a new mm object and returns a handle to it.
294
 */
295
 */
295
int
296
int
296
i915_gem_create_ioctl(struct drm_device *dev, void *data,
297
i915_gem_create_ioctl(struct drm_device *dev, void *data,
297
		      struct drm_file *file)
298
		      struct drm_file *file)
298
{
299
{
299
	struct drm_i915_gem_create *args = data;
300
	struct drm_i915_gem_create *args = data;
300
 
301
 
301
	return i915_gem_create(file, dev,
302
	return i915_gem_create(file, dev,
302
			       args->size, &args->handle);
303
			       args->size, &args->handle);
303
}
304
}
304
 
305
 
305
 
306
 
306
#if 0
307
#if 0
307
 
308
 
308
static inline int
309
static inline int
309
__copy_to_user_swizzled(char __user *cpu_vaddr,
310
__copy_to_user_swizzled(char __user *cpu_vaddr,
310
			const char *gpu_vaddr, int gpu_offset,
311
			const char *gpu_vaddr, int gpu_offset,
311
		int length)
312
		int length)
312
{
313
{
313
	int ret, cpu_offset = 0;
314
	int ret, cpu_offset = 0;
314
 
315
 
315
	while (length > 0) {
316
	while (length > 0) {
316
		int cacheline_end = ALIGN(gpu_offset + 1, 64);
317
		int cacheline_end = ALIGN(gpu_offset + 1, 64);
317
		int this_length = min(cacheline_end - gpu_offset, length);
318
		int this_length = min(cacheline_end - gpu_offset, length);
318
		int swizzled_gpu_offset = gpu_offset ^ 64;
319
		int swizzled_gpu_offset = gpu_offset ^ 64;
319
 
320
 
320
		ret = __copy_to_user(cpu_vaddr + cpu_offset,
321
		ret = __copy_to_user(cpu_vaddr + cpu_offset,
321
				     gpu_vaddr + swizzled_gpu_offset,
322
				     gpu_vaddr + swizzled_gpu_offset,
322
				     this_length);
323
				     this_length);
323
		if (ret)
324
		if (ret)
324
			return ret + length;
325
			return ret + length;
325
 
326
 
326
		cpu_offset += this_length;
327
		cpu_offset += this_length;
327
		gpu_offset += this_length;
328
		gpu_offset += this_length;
328
		length -= this_length;
329
		length -= this_length;
329
	}
330
	}
330
 
331
 
331
	return 0;
332
	return 0;
332
}
333
}
333
 
334
 
334
static inline int
335
static inline int
335
__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
336
__copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
336
			  const char __user *cpu_vaddr,
337
			  const char __user *cpu_vaddr,
337
			  int length)
338
			  int length)
338
{
339
{
339
	int ret, cpu_offset = 0;
340
	int ret, cpu_offset = 0;
340
 
341
 
341
	while (length > 0) {
342
	while (length > 0) {
342
		int cacheline_end = ALIGN(gpu_offset + 1, 64);
343
		int cacheline_end = ALIGN(gpu_offset + 1, 64);
343
		int this_length = min(cacheline_end - gpu_offset, length);
344
		int this_length = min(cacheline_end - gpu_offset, length);
344
		int swizzled_gpu_offset = gpu_offset ^ 64;
345
		int swizzled_gpu_offset = gpu_offset ^ 64;
345
 
346
 
346
		ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
347
		ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
347
			       cpu_vaddr + cpu_offset,
348
			       cpu_vaddr + cpu_offset,
348
			       this_length);
349
			       this_length);
349
		if (ret)
350
		if (ret)
350
			return ret + length;
351
			return ret + length;
351
 
352
 
352
		cpu_offset += this_length;
353
		cpu_offset += this_length;
353
		gpu_offset += this_length;
354
		gpu_offset += this_length;
354
		length -= this_length;
355
		length -= this_length;
355
	}
356
	}
356
 
357
 
357
	return 0;
358
	return 0;
358
}
359
}
359
 
360
 
360
/* Per-page copy function for the shmem pread fastpath.
361
/* Per-page copy function for the shmem pread fastpath.
361
 * Flushes invalid cachelines before reading the target if
362
 * Flushes invalid cachelines before reading the target if
362
 * needs_clflush is set. */
363
 * needs_clflush is set. */
363
static int
364
static int
364
shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
365
shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
365
		 char __user *user_data,
366
		 char __user *user_data,
366
		 bool page_do_bit17_swizzling, bool needs_clflush)
367
		 bool page_do_bit17_swizzling, bool needs_clflush)
367
{
368
{
368
		char *vaddr;
369
		char *vaddr;
369
		int ret;
370
		int ret;
370
 
371
 
371
	if (unlikely(page_do_bit17_swizzling))
372
	if (unlikely(page_do_bit17_swizzling))
372
		return -EINVAL;
373
		return -EINVAL;
373
 
374
 
374
		vaddr = kmap_atomic(page);
375
		vaddr = kmap_atomic(page);
375
	if (needs_clflush)
376
	if (needs_clflush)
376
		drm_clflush_virt_range(vaddr + shmem_page_offset,
377
		drm_clflush_virt_range(vaddr + shmem_page_offset,
377
				       page_length);
378
				       page_length);
378
		ret = __copy_to_user_inatomic(user_data,
379
		ret = __copy_to_user_inatomic(user_data,
379
				      vaddr + shmem_page_offset,
380
				      vaddr + shmem_page_offset,
380
					      page_length);
381
					      page_length);
381
		kunmap_atomic(vaddr);
382
		kunmap_atomic(vaddr);
382
 
383
 
383
	return ret ? -EFAULT : 0;
384
	return ret ? -EFAULT : 0;
384
}
385
}
385
 
386
 
386
static void
387
static void
387
shmem_clflush_swizzled_range(char *addr, unsigned long length,
388
shmem_clflush_swizzled_range(char *addr, unsigned long length,
388
			     bool swizzled)
389
			     bool swizzled)
389
{
390
{
390
	if (unlikely(swizzled)) {
391
	if (unlikely(swizzled)) {
391
		unsigned long start = (unsigned long) addr;
392
		unsigned long start = (unsigned long) addr;
392
		unsigned long end = (unsigned long) addr + length;
393
		unsigned long end = (unsigned long) addr + length;
393
 
394
 
394
		/* For swizzling simply ensure that we always flush both
395
		/* For swizzling simply ensure that we always flush both
395
		 * channels. Lame, but simple and it works. Swizzled
396
		 * channels. Lame, but simple and it works. Swizzled
396
		 * pwrite/pread is far from a hotpath - current userspace
397
		 * pwrite/pread is far from a hotpath - current userspace
397
		 * doesn't use it at all. */
398
		 * doesn't use it at all. */
398
		start = round_down(start, 128);
399
		start = round_down(start, 128);
399
		end = round_up(end, 128);
400
		end = round_up(end, 128);
400
 
401
 
401
		drm_clflush_virt_range((void *)start, end - start);
402
		drm_clflush_virt_range((void *)start, end - start);
402
	} else {
403
	} else {
403
		drm_clflush_virt_range(addr, length);
404
		drm_clflush_virt_range(addr, length);
404
	}
405
	}
405
 
406
 
406
}
407
}
407
 
408
 
408
/* Only difference to the fast-path function is that this can handle bit17
409
/* Only difference to the fast-path function is that this can handle bit17
409
 * and uses non-atomic copy and kmap functions. */
410
 * and uses non-atomic copy and kmap functions. */
410
static int
411
static int
411
shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
412
shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
412
		 char __user *user_data,
413
		 char __user *user_data,
413
		 bool page_do_bit17_swizzling, bool needs_clflush)
414
		 bool page_do_bit17_swizzling, bool needs_clflush)
414
{
415
{
415
	char *vaddr;
416
	char *vaddr;
416
	int ret;
417
	int ret;
417
 
418
 
418
	vaddr = kmap(page);
419
	vaddr = kmap(page);
419
	if (needs_clflush)
420
	if (needs_clflush)
420
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
421
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
421
					     page_length,
422
					     page_length,
422
					     page_do_bit17_swizzling);
423
					     page_do_bit17_swizzling);
423
 
424
 
424
	if (page_do_bit17_swizzling)
425
	if (page_do_bit17_swizzling)
425
		ret = __copy_to_user_swizzled(user_data,
426
		ret = __copy_to_user_swizzled(user_data,
426
					      vaddr, shmem_page_offset,
427
					      vaddr, shmem_page_offset,
427
					      page_length);
428
					      page_length);
428
	else
429
	else
429
		ret = __copy_to_user(user_data,
430
		ret = __copy_to_user(user_data,
430
				     vaddr + shmem_page_offset,
431
				     vaddr + shmem_page_offset,
431
				     page_length);
432
				     page_length);
432
	kunmap(page);
433
	kunmap(page);
433
 
434
 
434
	return ret ? - EFAULT : 0;
435
	return ret ? - EFAULT : 0;
435
}
436
}
436
 
437
 
437
static int
438
static int
438
i915_gem_shmem_pread(struct drm_device *dev,
439
i915_gem_shmem_pread(struct drm_device *dev,
439
			  struct drm_i915_gem_object *obj,
440
			  struct drm_i915_gem_object *obj,
440
			  struct drm_i915_gem_pread *args,
441
			  struct drm_i915_gem_pread *args,
441
			  struct drm_file *file)
442
			  struct drm_file *file)
442
{
443
{
443
	char __user *user_data;
444
	char __user *user_data;
444
	ssize_t remain;
445
	ssize_t remain;
445
	loff_t offset;
446
	loff_t offset;
446
	int shmem_page_offset, page_length, ret = 0;
447
	int shmem_page_offset, page_length, ret = 0;
447
	int obj_do_bit17_swizzling, page_do_bit17_swizzling;
448
	int obj_do_bit17_swizzling, page_do_bit17_swizzling;
448
	int prefaulted = 0;
449
	int prefaulted = 0;
449
	int needs_clflush = 0;
450
	int needs_clflush = 0;
450
	struct scatterlist *sg;
451
	struct sg_page_iter sg_iter;
451
	int i;
-
 
452
 
452
 
453
	user_data = (char __user *) (uintptr_t) args->data_ptr;
453
	user_data = to_user_ptr(args->data_ptr);
454
	remain = args->size;
454
	remain = args->size;
455
 
455
 
456
	obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
456
	obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
457
 
457
 
458
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
458
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
459
		/* If we're not in the cpu read domain, set ourself into the gtt
459
		/* If we're not in the cpu read domain, set ourself into the gtt
460
		 * read domain and manually flush cachelines (if required). This
460
		 * read domain and manually flush cachelines (if required). This
461
		 * optimizes for the case when the gpu will dirty the data
461
		 * optimizes for the case when the gpu will dirty the data
462
		 * anyway again before the next pread happens. */
462
		 * anyway again before the next pread happens. */
463
		if (obj->cache_level == I915_CACHE_NONE)
463
		if (obj->cache_level == I915_CACHE_NONE)
464
			needs_clflush = 1;
464
			needs_clflush = 1;
465
		if (obj->gtt_space) {
465
		if (obj->gtt_space) {
466
			ret = i915_gem_object_set_to_gtt_domain(obj, false);
466
			ret = i915_gem_object_set_to_gtt_domain(obj, false);
467
			if (ret)
467
			if (ret)
468
				return ret;
468
				return ret;
469
		}
469
		}
470
	}
470
	}
471
 
471
 
472
	ret = i915_gem_object_get_pages(obj);
472
	ret = i915_gem_object_get_pages(obj);
473
	if (ret)
473
	if (ret)
474
		return ret;
474
		return ret;
475
 
475
 
476
	i915_gem_object_pin_pages(obj);
476
	i915_gem_object_pin_pages(obj);
477
 
477
 
478
	offset = args->offset;
478
	offset = args->offset;
479
 
479
 
480
	for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
-
 
481
		struct page *page;
-
 
482
 
480
	for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
483
		if (i < offset >> PAGE_SHIFT)
481
			 offset >> PAGE_SHIFT) {
484
			continue;
482
		struct page *page = sg_page_iter_page(&sg_iter);
485
 
483
 
486
		if (remain <= 0)
484
		if (remain <= 0)
487
			break;
485
			break;
488
 
486
 
489
		/* Operation in this page
487
		/* Operation in this page
490
		 *
488
		 *
491
		 * shmem_page_offset = offset within page in shmem file
489
		 * shmem_page_offset = offset within page in shmem file
492
		 * page_length = bytes to copy for this page
490
		 * page_length = bytes to copy for this page
493
		 */
491
		 */
494
		shmem_page_offset = offset_in_page(offset);
492
		shmem_page_offset = offset_in_page(offset);
495
		page_length = remain;
493
		page_length = remain;
496
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
494
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
497
			page_length = PAGE_SIZE - shmem_page_offset;
495
			page_length = PAGE_SIZE - shmem_page_offset;
498
 
-
 
499
		page = sg_page(sg);
496
 
500
		page_do_bit17_swizzling = obj_do_bit17_swizzling &&
497
		page_do_bit17_swizzling = obj_do_bit17_swizzling &&
501
			(page_to_phys(page) & (1 << 17)) != 0;
498
			(page_to_phys(page) & (1 << 17)) != 0;
502
 
499
 
503
		ret = shmem_pread_fast(page, shmem_page_offset, page_length,
500
		ret = shmem_pread_fast(page, shmem_page_offset, page_length,
504
				       user_data, page_do_bit17_swizzling,
501
				       user_data, page_do_bit17_swizzling,
505
				       needs_clflush);
502
				       needs_clflush);
506
		if (ret == 0)
503
		if (ret == 0)
507
			goto next_page;
504
			goto next_page;
508
 
505
 
509
		mutex_unlock(&dev->struct_mutex);
506
		mutex_unlock(&dev->struct_mutex);
510
 
507
 
511
		if (!prefaulted) {
508
		if (!prefaulted) {
512
			ret = fault_in_multipages_writeable(user_data, remain);
509
			ret = fault_in_multipages_writeable(user_data, remain);
513
			/* Userspace is tricking us, but we've already clobbered
510
			/* Userspace is tricking us, but we've already clobbered
514
			 * its pages with the prefault and promised to write the
511
			 * its pages with the prefault and promised to write the
515
			 * data up to the first fault. Hence ignore any errors
512
			 * data up to the first fault. Hence ignore any errors
516
			 * and just continue. */
513
			 * and just continue. */
517
			(void)ret;
514
			(void)ret;
518
			prefaulted = 1;
515
			prefaulted = 1;
519
		}
516
		}
520
 
517
 
521
		ret = shmem_pread_slow(page, shmem_page_offset, page_length,
518
		ret = shmem_pread_slow(page, shmem_page_offset, page_length,
522
				       user_data, page_do_bit17_swizzling,
519
				       user_data, page_do_bit17_swizzling,
523
				       needs_clflush);
520
				       needs_clflush);
524
 
521
 
525
		mutex_lock(&dev->struct_mutex);
522
		mutex_lock(&dev->struct_mutex);
526
 
523
 
527
next_page:
524
next_page:
528
		mark_page_accessed(page);
525
		mark_page_accessed(page);
529
 
526
 
530
		if (ret)
527
		if (ret)
531
			goto out;
528
			goto out;
532
 
529
 
533
		remain -= page_length;
530
		remain -= page_length;
534
		user_data += page_length;
531
		user_data += page_length;
535
		offset += page_length;
532
		offset += page_length;
536
	}
533
	}
537
 
534
 
538
out:
535
out:
539
	i915_gem_object_unpin_pages(obj);
536
	i915_gem_object_unpin_pages(obj);
540
 
537
 
541
	return ret;
538
	return ret;
542
}
539
}
543
 
540
 
544
/**
541
/**
545
 * Reads data from the object referenced by handle.
542
 * Reads data from the object referenced by handle.
546
 *
543
 *
547
 * On error, the contents of *data are undefined.
544
 * On error, the contents of *data are undefined.
548
 */
545
 */
549
int
546
int
550
i915_gem_pread_ioctl(struct drm_device *dev, void *data,
547
i915_gem_pread_ioctl(struct drm_device *dev, void *data,
551
		     struct drm_file *file)
548
		     struct drm_file *file)
552
{
549
{
553
	struct drm_i915_gem_pread *args = data;
550
	struct drm_i915_gem_pread *args = data;
554
	struct drm_i915_gem_object *obj;
551
	struct drm_i915_gem_object *obj;
555
	int ret = 0;
552
	int ret = 0;
556
 
553
 
557
	if (args->size == 0)
554
	if (args->size == 0)
558
		return 0;
555
		return 0;
559
 
556
 
560
	if (!access_ok(VERIFY_WRITE,
557
	if (!access_ok(VERIFY_WRITE,
561
		       (char __user *)(uintptr_t)args->data_ptr,
558
		       to_user_ptr(args->data_ptr),
562
		       args->size))
559
		       args->size))
563
		return -EFAULT;
560
		return -EFAULT;
564
 
561
 
565
	ret = i915_mutex_lock_interruptible(dev);
562
	ret = i915_mutex_lock_interruptible(dev);
566
	if (ret)
563
	if (ret)
567
		return ret;
564
		return ret;
568
 
565
 
569
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
566
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
570
	if (&obj->base == NULL) {
567
	if (&obj->base == NULL) {
571
		ret = -ENOENT;
568
		ret = -ENOENT;
572
		goto unlock;
569
		goto unlock;
573
	}
570
	}
574
 
571
 
575
	/* Bounds check source.  */
572
	/* Bounds check source.  */
576
	if (args->offset > obj->base.size ||
573
	if (args->offset > obj->base.size ||
577
	    args->size > obj->base.size - args->offset) {
574
	    args->size > obj->base.size - args->offset) {
578
		ret = -EINVAL;
575
		ret = -EINVAL;
579
		goto out;
576
		goto out;
580
	}
577
	}
581
 
578
 
582
	/* prime objects have no backing filp to GEM pread/pwrite
579
	/* prime objects have no backing filp to GEM pread/pwrite
583
	 * pages from.
580
	 * pages from.
584
	 */
581
	 */
585
	if (!obj->base.filp) {
582
	if (!obj->base.filp) {
586
		ret = -EINVAL;
583
		ret = -EINVAL;
587
		goto out;
584
		goto out;
588
	}
585
	}
589
 
586
 
590
	trace_i915_gem_object_pread(obj, args->offset, args->size);
587
	trace_i915_gem_object_pread(obj, args->offset, args->size);
591
 
588
 
592
	ret = i915_gem_shmem_pread(dev, obj, args, file);
589
	ret = i915_gem_shmem_pread(dev, obj, args, file);
593
 
590
 
594
out:
591
out:
595
	drm_gem_object_unreference(&obj->base);
592
	drm_gem_object_unreference(&obj->base);
596
unlock:
593
unlock:
597
	mutex_unlock(&dev->struct_mutex);
594
	mutex_unlock(&dev->struct_mutex);
598
	return ret;
595
	return ret;
599
}
596
}
600
 
597
 
601
/* This is the fast write path which cannot handle
598
/* This is the fast write path which cannot handle
602
 * page faults in the source data
599
 * page faults in the source data
603
 */
600
 */
604
 
601
 
605
static inline int
602
static inline int
606
fast_user_write(struct io_mapping *mapping,
603
fast_user_write(struct io_mapping *mapping,
607
		loff_t page_base, int page_offset,
604
		loff_t page_base, int page_offset,
608
		char __user *user_data,
605
		char __user *user_data,
609
		int length)
606
		int length)
610
{
607
{
611
	void __iomem *vaddr_atomic;
608
	void __iomem *vaddr_atomic;
612
	void *vaddr;
609
	void *vaddr;
613
	unsigned long unwritten;
610
	unsigned long unwritten;
614
 
611
 
615
	vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
612
	vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
616
	/* We can use the cpu mem copy function because this is X86. */
613
	/* We can use the cpu mem copy function because this is X86. */
617
	vaddr = (void __force*)vaddr_atomic + page_offset;
614
	vaddr = (void __force*)vaddr_atomic + page_offset;
618
	unwritten = __copy_from_user_inatomic_nocache(vaddr,
615
	unwritten = __copy_from_user_inatomic_nocache(vaddr,
619
						      user_data, length);
616
						      user_data, length);
620
	io_mapping_unmap_atomic(vaddr_atomic);
617
	io_mapping_unmap_atomic(vaddr_atomic);
621
	return unwritten;
618
	return unwritten;
622
}
619
}
623
#endif
620
#endif
624
 
621
 
625
#define offset_in_page(p)       ((unsigned long)(p) & ~PAGE_MASK)
622
#define offset_in_page(p)       ((unsigned long)(p) & ~PAGE_MASK)
626
/**
623
/**
627
 * This is the fast pwrite path, where we copy the data directly from the
624
 * This is the fast pwrite path, where we copy the data directly from the
628
 * user into the GTT, uncached.
625
 * user into the GTT, uncached.
629
 */
626
 */
630
static int
627
static int
631
i915_gem_gtt_pwrite_fast(struct drm_device *dev,
628
i915_gem_gtt_pwrite_fast(struct drm_device *dev,
632
			 struct drm_i915_gem_object *obj,
629
			 struct drm_i915_gem_object *obj,
633
			 struct drm_i915_gem_pwrite *args,
630
			 struct drm_i915_gem_pwrite *args,
634
			 struct drm_file *file)
631
			 struct drm_file *file)
635
{
632
{
636
	drm_i915_private_t *dev_priv = dev->dev_private;
633
	drm_i915_private_t *dev_priv = dev->dev_private;
637
	ssize_t remain;
634
	ssize_t remain;
638
	loff_t offset, page_base;
635
	loff_t offset, page_base;
639
	char __user *user_data;
636
	char __user *user_data;
640
	int page_offset, page_length, ret;
637
	int page_offset, page_length, ret;
641
    char *vaddr;
638
    char *vaddr;
642
 
639
 
643
	ret = i915_gem_object_pin(obj, 0, true, true);
640
	ret = i915_gem_object_pin(obj, 0, true, true);
644
	if (ret)
641
	if (ret)
645
		goto out;
642
		goto out;
646
 
643
 
647
	ret = i915_gem_object_set_to_gtt_domain(obj, true);
644
	ret = i915_gem_object_set_to_gtt_domain(obj, true);
648
	if (ret)
645
	if (ret)
649
		goto out_unpin;
646
		goto out_unpin;
650
 
647
 
651
	ret = i915_gem_object_put_fence(obj);
648
	ret = i915_gem_object_put_fence(obj);
652
	if (ret)
649
	if (ret)
653
		goto out_unpin;
650
		goto out_unpin;
654
 
651
 
655
    vaddr = AllocKernelSpace(4096);
652
    vaddr = AllocKernelSpace(4096);
656
    if(vaddr == NULL)
653
    if(vaddr == NULL)
657
    {
654
    {
658
        ret = -ENOSPC;
655
        ret = -ENOSPC;
659
        goto out_unpin;
656
        goto out_unpin;
660
    };
657
    };
661
 
658
 
662
	user_data = (char __user *) (uintptr_t) args->data_ptr;
659
	user_data = (char __user *) (uintptr_t) args->data_ptr;
663
	remain = args->size;
660
	remain = args->size;
664
 
661
 
665
	offset = obj->gtt_offset + args->offset;
662
	offset = obj->gtt_offset + args->offset;
666
 
663
 
667
	while (remain > 0) {
664
	while (remain > 0) {
668
		/* Operation in this page
665
		/* Operation in this page
669
		 *
666
		 *
670
		 * page_base = page offset within aperture
667
		 * page_base = page offset within aperture
671
		 * page_offset = offset within page
668
		 * page_offset = offset within page
672
		 * page_length = bytes to copy for this page
669
		 * page_length = bytes to copy for this page
673
		 */
670
		 */
674
		page_base = offset & PAGE_MASK;
671
		page_base = offset & PAGE_MASK;
675
		page_offset = offset_in_page(offset);
672
		page_offset = offset_in_page(offset);
676
		page_length = remain;
673
		page_length = remain;
677
		if ((page_offset + remain) > PAGE_SIZE)
674
		if ((page_offset + remain) > PAGE_SIZE)
678
			page_length = PAGE_SIZE - page_offset;
675
			page_length = PAGE_SIZE - page_offset;
679
 
676
 
680
        MapPage(vaddr, page_base, PG_SW|PG_NOCACHE);
677
        MapPage(vaddr, page_base, PG_SW|PG_NOCACHE);
681
 
678
 
682
        memcpy(vaddr+page_offset, user_data, page_length);
679
        memcpy(vaddr+page_offset, user_data, page_length);
683
 
680
 
684
		remain -= page_length;
681
		remain -= page_length;
685
		user_data += page_length;
682
		user_data += page_length;
686
		offset += page_length;
683
		offset += page_length;
687
	}
684
	}
688
 
685
 
689
    FreeKernelSpace(vaddr);
686
    FreeKernelSpace(vaddr);
690
 
687
 
691
out_unpin:
688
out_unpin:
692
	i915_gem_object_unpin(obj);
689
	i915_gem_object_unpin(obj);
693
out:
690
out:
694
	return ret;
691
	return ret;
695
}
692
}
696
 
693
 
697
/* Per-page copy function for the shmem pwrite fastpath.
694
/* Per-page copy function for the shmem pwrite fastpath.
698
 * Flushes invalid cachelines before writing to the target if
695
 * Flushes invalid cachelines before writing to the target if
699
 * needs_clflush_before is set and flushes out any written cachelines after
696
 * needs_clflush_before is set and flushes out any written cachelines after
700
 * writing if needs_clflush is set. */
697
 * writing if needs_clflush is set. */
701
static int
698
static int
702
shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
699
shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
703
		  char __user *user_data,
700
		  char __user *user_data,
704
		  bool page_do_bit17_swizzling,
701
		  bool page_do_bit17_swizzling,
705
		  bool needs_clflush_before,
702
		  bool needs_clflush_before,
706
		  bool needs_clflush_after)
703
		  bool needs_clflush_after)
707
{
704
{
708
	char *vaddr;
705
	char *vaddr;
709
	int ret = 0;
706
	int ret = 0;
710
 
707
 
711
	if (unlikely(page_do_bit17_swizzling))
708
	if (unlikely(page_do_bit17_swizzling))
712
		return -EINVAL;
709
		return -EINVAL;
713
 
710
 
714
	vaddr = (char *)MapIoMem((addr_t)page, 4096, PG_SW);
711
	vaddr = (char *)MapIoMem((addr_t)page, 4096, PG_SW);
715
	if (needs_clflush_before)
712
	if (needs_clflush_before)
716
		drm_clflush_virt_range(vaddr + shmem_page_offset,
713
		drm_clflush_virt_range(vaddr + shmem_page_offset,
717
				       page_length);
714
				       page_length);
718
	memcpy(vaddr + shmem_page_offset,
715
	memcpy(vaddr + shmem_page_offset,
719
						user_data,
716
						user_data,
720
						page_length);
717
						page_length);
721
	if (needs_clflush_after)
718
	if (needs_clflush_after)
722
		drm_clflush_virt_range(vaddr + shmem_page_offset,
719
		drm_clflush_virt_range(vaddr + shmem_page_offset,
723
				       page_length);
720
				       page_length);
724
	FreeKernelSpace(vaddr);
721
	FreeKernelSpace(vaddr);
725
 
722
 
726
	return ret ? -EFAULT : 0;
723
	return ret ? -EFAULT : 0;
727
}
724
}
728
#if 0
725
#if 0
729
 
726
 
730
/* Only difference to the fast-path function is that this can handle bit17
727
/* Only difference to the fast-path function is that this can handle bit17
731
 * and uses non-atomic copy and kmap functions. */
728
 * and uses non-atomic copy and kmap functions. */
732
static int
729
static int
733
shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
730
shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
734
		  char __user *user_data,
731
		  char __user *user_data,
735
		  bool page_do_bit17_swizzling,
732
		  bool page_do_bit17_swizzling,
736
		  bool needs_clflush_before,
733
		  bool needs_clflush_before,
737
		  bool needs_clflush_after)
734
		  bool needs_clflush_after)
738
{
735
{
739
	char *vaddr;
736
	char *vaddr;
740
	int ret;
737
	int ret;
741
 
738
 
742
	vaddr = kmap(page);
739
	vaddr = kmap(page);
743
	if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
740
	if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
744
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
741
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
745
					     page_length,
742
					     page_length,
746
					     page_do_bit17_swizzling);
743
					     page_do_bit17_swizzling);
747
	if (page_do_bit17_swizzling)
744
	if (page_do_bit17_swizzling)
748
		ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
745
		ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
749
						user_data,
746
						user_data,
750
						page_length);
747
						page_length);
751
	else
748
	else
752
		ret = __copy_from_user(vaddr + shmem_page_offset,
749
		ret = __copy_from_user(vaddr + shmem_page_offset,
753
				       user_data,
750
				       user_data,
754
				       page_length);
751
				       page_length);
755
	if (needs_clflush_after)
752
	if (needs_clflush_after)
756
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
753
		shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
757
					     page_length,
754
					     page_length,
758
					     page_do_bit17_swizzling);
755
					     page_do_bit17_swizzling);
759
	kunmap(page);
756
	kunmap(page);
760
 
757
 
761
	return ret ? -EFAULT : 0;
758
	return ret ? -EFAULT : 0;
762
}
759
}
763
#endif
760
#endif
764
 
761
 
765
 
762
 
766
static int
763
static int
767
i915_gem_shmem_pwrite(struct drm_device *dev,
764
i915_gem_shmem_pwrite(struct drm_device *dev,
768
		      struct drm_i915_gem_object *obj,
765
		      struct drm_i915_gem_object *obj,
769
		      struct drm_i915_gem_pwrite *args,
766
		      struct drm_i915_gem_pwrite *args,
770
		      struct drm_file *file)
767
		      struct drm_file *file)
771
{
768
{
772
	ssize_t remain;
769
	ssize_t remain;
773
	loff_t offset;
770
	loff_t offset;
774
	char __user *user_data;
771
	char __user *user_data;
775
	int shmem_page_offset, page_length, ret = 0;
772
	int shmem_page_offset, page_length, ret = 0;
776
	int obj_do_bit17_swizzling, page_do_bit17_swizzling;
773
	int obj_do_bit17_swizzling, page_do_bit17_swizzling;
777
	int hit_slowpath = 0;
774
	int hit_slowpath = 0;
778
	int needs_clflush_after = 0;
775
	int needs_clflush_after = 0;
779
	int needs_clflush_before = 0;
776
	int needs_clflush_before = 0;
780
	int i;
-
 
781
	struct scatterlist *sg;
777
	struct sg_page_iter sg_iter;
782
 
778
 
783
	user_data = (char __user *) (uintptr_t) args->data_ptr;
779
	user_data = to_user_ptr(args->data_ptr);
784
	remain = args->size;
780
	remain = args->size;
785
 
781
 
786
	obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
782
	obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
787
 
783
 
788
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
784
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
789
		/* If we're not in the cpu write domain, set ourself into the gtt
785
		/* If we're not in the cpu write domain, set ourself into the gtt
790
		 * write domain and manually flush cachelines (if required). This
786
		 * write domain and manually flush cachelines (if required). This
791
		 * optimizes for the case when the gpu will use the data
787
		 * optimizes for the case when the gpu will use the data
792
		 * right away and we therefore have to clflush anyway. */
788
		 * right away and we therefore have to clflush anyway. */
793
		if (obj->cache_level == I915_CACHE_NONE)
789
		if (obj->cache_level == I915_CACHE_NONE)
794
			needs_clflush_after = 1;
790
			needs_clflush_after = 1;
795
		if (obj->gtt_space) {
791
		if (obj->gtt_space) {
796
			ret = i915_gem_object_set_to_gtt_domain(obj, true);
792
			ret = i915_gem_object_set_to_gtt_domain(obj, true);
797
			if (ret)
793
			if (ret)
798
				return ret;
794
				return ret;
799
		}
795
		}
800
	}
796
	}
801
	/* Same trick applies for invalidate partially written cachelines before
797
	/* Same trick applies for invalidate partially written cachelines before
802
	 * writing.  */
798
	 * writing.  */
803
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
799
	if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
804
	    && obj->cache_level == I915_CACHE_NONE)
800
	    && obj->cache_level == I915_CACHE_NONE)
805
		needs_clflush_before = 1;
801
		needs_clflush_before = 1;
806
 
802
 
807
	ret = i915_gem_object_get_pages(obj);
803
	ret = i915_gem_object_get_pages(obj);
808
	if (ret)
804
	if (ret)
809
		return ret;
805
		return ret;
810
 
806
 
811
	i915_gem_object_pin_pages(obj);
807
	i915_gem_object_pin_pages(obj);
812
 
808
 
813
	offset = args->offset;
809
	offset = args->offset;
814
	obj->dirty = 1;
810
	obj->dirty = 1;
815
 
811
 
-
 
812
	for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
816
	for_each_sg(obj->pages->sgl, sg, obj->pages->nents, i) {
813
			 offset >> PAGE_SHIFT) {
817
		struct page *page;
814
		struct page *page = sg_page_iter_page(&sg_iter);
818
		int partial_cacheline_write;
-
 
819
 
-
 
820
		if (i < offset >> PAGE_SHIFT)
-
 
821
			continue;
815
		int partial_cacheline_write;
822
 
816
 
823
		if (remain <= 0)
817
		if (remain <= 0)
824
			break;
818
			break;
825
 
819
 
826
		/* Operation in this page
820
		/* Operation in this page
827
		 *
821
		 *
828
		 * shmem_page_offset = offset within page in shmem file
822
		 * shmem_page_offset = offset within page in shmem file
829
		 * page_length = bytes to copy for this page
823
		 * page_length = bytes to copy for this page
830
		 */
824
		 */
831
		shmem_page_offset = offset_in_page(offset);
825
		shmem_page_offset = offset_in_page(offset);
832
 
826
 
833
		page_length = remain;
827
		page_length = remain;
834
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
828
		if ((shmem_page_offset + page_length) > PAGE_SIZE)
835
			page_length = PAGE_SIZE - shmem_page_offset;
829
			page_length = PAGE_SIZE - shmem_page_offset;
836
 
830
 
837
		/* If we don't overwrite a cacheline completely we need to be
831
		/* If we don't overwrite a cacheline completely we need to be
838
		 * careful to have up-to-date data by first clflushing. Don't
832
		 * careful to have up-to-date data by first clflushing. Don't
839
		 * overcomplicate things and flush the entire patch. */
833
		 * overcomplicate things and flush the entire patch. */
840
		partial_cacheline_write = needs_clflush_before &&
834
		partial_cacheline_write = needs_clflush_before &&
841
			((shmem_page_offset | page_length)
835
			((shmem_page_offset | page_length)
842
				& (x86_clflush_size - 1));
836
				& (x86_clflush_size - 1));
843
 
-
 
844
		page = sg_page(sg);
837
 
845
		page_do_bit17_swizzling = obj_do_bit17_swizzling &&
838
		page_do_bit17_swizzling = obj_do_bit17_swizzling &&
846
			(page_to_phys(page) & (1 << 17)) != 0;
839
			(page_to_phys(page) & (1 << 17)) != 0;
847
 
840
 
848
		ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
841
		ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
849
					user_data, page_do_bit17_swizzling,
842
					user_data, page_do_bit17_swizzling,
850
					partial_cacheline_write,
843
					partial_cacheline_write,
851
					needs_clflush_after);
844
					needs_clflush_after);
852
		if (ret == 0)
845
		if (ret == 0)
853
			goto next_page;
846
			goto next_page;
854
 
847
 
855
		hit_slowpath = 1;
848
		hit_slowpath = 1;
856
		mutex_unlock(&dev->struct_mutex);
849
		mutex_unlock(&dev->struct_mutex);
857
		dbgprintf("%s need shmem_pwrite_slow\n",__FUNCTION__);
850
		dbgprintf("%s need shmem_pwrite_slow\n",__FUNCTION__);
858
 
851
 
859
//		ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
852
//		ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
860
//					user_data, page_do_bit17_swizzling,
853
//					user_data, page_do_bit17_swizzling,
861
//					partial_cacheline_write,
854
//					partial_cacheline_write,
862
//					needs_clflush_after);
855
//					needs_clflush_after);
863
 
856
 
864
		mutex_lock(&dev->struct_mutex);
857
		mutex_lock(&dev->struct_mutex);
865
 
858
 
866
next_page:
859
next_page:
867
 
860
 
868
		if (ret)
861
		if (ret)
869
			goto out;
862
			goto out;
870
 
863
 
871
		remain -= page_length;
864
		remain -= page_length;
872
		user_data += page_length;
865
		user_data += page_length;
873
		offset += page_length;
866
		offset += page_length;
874
	}
867
	}
875
 
868
 
876
out:
869
out:
877
	i915_gem_object_unpin_pages(obj);
870
	i915_gem_object_unpin_pages(obj);
878
 
871
 
879
	if (hit_slowpath) {
872
	if (hit_slowpath) {
880
		/*
873
		/*
881
		 * Fixup: Flush cpu caches in case we didn't flush the dirty
874
		 * Fixup: Flush cpu caches in case we didn't flush the dirty
882
		 * cachelines in-line while writing and the object moved
875
		 * cachelines in-line while writing and the object moved
883
		 * out of the cpu write domain while we've dropped the lock.
876
		 * out of the cpu write domain while we've dropped the lock.
884
		 */
877
		 */
885
		if (!needs_clflush_after &&
878
		if (!needs_clflush_after &&
886
		    obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
879
		    obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
887
			i915_gem_clflush_object(obj);
880
			i915_gem_clflush_object(obj);
888
			i915_gem_chipset_flush(dev);
881
			i915_gem_chipset_flush(dev);
889
		}
882
		}
890
	}
883
	}
891
 
884
 
892
	if (needs_clflush_after)
885
	if (needs_clflush_after)
893
		i915_gem_chipset_flush(dev);
886
		i915_gem_chipset_flush(dev);
894
 
887
 
895
	return ret;
888
	return ret;
896
}
889
}
897
 
890
 
898
/**
891
/**
899
 * Writes data to the object referenced by handle.
892
 * Writes data to the object referenced by handle.
900
 *
893
 *
901
 * On error, the contents of the buffer that were to be modified are undefined.
894
 * On error, the contents of the buffer that were to be modified are undefined.
902
 */
895
 */
903
int
896
int
904
i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
897
i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
905
		      struct drm_file *file)
898
		      struct drm_file *file)
906
{
899
{
907
	struct drm_i915_gem_pwrite *args = data;
900
	struct drm_i915_gem_pwrite *args = data;
908
	struct drm_i915_gem_object *obj;
901
	struct drm_i915_gem_object *obj;
909
	int ret;
902
	int ret;
910
 
903
 
911
     if(args->handle == -2)
904
     if(args->handle == -2)
912
     {
905
     {
913
        printf("%s handle %d\n", __FUNCTION__, args->handle);
906
        printf("%s handle %d\n", __FUNCTION__, args->handle);
914
        return 0;
907
        return 0;
915
     }
908
     }
916
 
909
 
917
	if (args->size == 0)
910
	if (args->size == 0)
918
		return 0;
911
		return 0;
919
 
912
 
920
	ret = i915_mutex_lock_interruptible(dev);
913
	ret = i915_mutex_lock_interruptible(dev);
921
	if (ret)
914
	if (ret)
922
		return ret;
915
		return ret;
923
 
916
 
924
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
917
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
925
	if (&obj->base == NULL) {
918
	if (&obj->base == NULL) {
926
		ret = -ENOENT;
919
		ret = -ENOENT;
927
		goto unlock;
920
		goto unlock;
928
	}
921
	}
929
 
922
 
930
	/* Bounds check destination. */
923
	/* Bounds check destination. */
931
	if (args->offset > obj->base.size ||
924
	if (args->offset > obj->base.size ||
932
	    args->size > obj->base.size - args->offset) {
925
	    args->size > obj->base.size - args->offset) {
933
		ret = -EINVAL;
926
		ret = -EINVAL;
934
		goto out;
927
		goto out;
935
	}
928
	}
936
 
929
 
937
	/* prime objects have no backing filp to GEM pread/pwrite
930
	/* prime objects have no backing filp to GEM pread/pwrite
938
	 * pages from.
931
	 * pages from.
939
	 */
932
	 */
940
	if (!obj->base.filp) {
933
	if (!obj->base.filp) {
941
		ret = -EINVAL;
934
		ret = -EINVAL;
942
		goto out;
935
		goto out;
943
	}
936
	}
944
 
937
 
945
	trace_i915_gem_object_pwrite(obj, args->offset, args->size);
938
	trace_i915_gem_object_pwrite(obj, args->offset, args->size);
946
 
939
 
947
	ret = -EFAULT;
940
	ret = -EFAULT;
948
	/* We can only do the GTT pwrite on untiled buffers, as otherwise
941
	/* We can only do the GTT pwrite on untiled buffers, as otherwise
949
	 * it would end up going through the fenced access, and we'll get
942
	 * it would end up going through the fenced access, and we'll get
950
	 * different detiling behavior between reading and writing.
943
	 * different detiling behavior between reading and writing.
951
	 * pread/pwrite currently are reading and writing from the CPU
944
	 * pread/pwrite currently are reading and writing from the CPU
952
	 * perspective, requiring manual detiling by the client.
945
	 * perspective, requiring manual detiling by the client.
953
	 */
946
	 */
954
//   if (obj->phys_obj) {
947
//   if (obj->phys_obj) {
955
//       ret = i915_gem_phys_pwrite(dev, obj, args, file);
948
//       ret = i915_gem_phys_pwrite(dev, obj, args, file);
956
//       goto out;
949
//       goto out;
957
//   }
950
//   }
958
 
951
 
959
	if (obj->cache_level == I915_CACHE_NONE &&
952
	if (obj->cache_level == I915_CACHE_NONE &&
960
	    obj->tiling_mode == I915_TILING_NONE &&
953
	    obj->tiling_mode == I915_TILING_NONE &&
961
	    obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
954
	    obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
962
		ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
955
		ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
963
		/* Note that the gtt paths might fail with non-page-backed user
956
		/* Note that the gtt paths might fail with non-page-backed user
964
		 * pointers (e.g. gtt mappings when moving data between
957
		 * pointers (e.g. gtt mappings when moving data between
965
		 * textures). Fallback to the shmem path in that case. */
958
		 * textures). Fallback to the shmem path in that case. */
966
	}
959
	}
967
 
960
 
968
	if (ret == -EFAULT || ret == -ENOSPC)
961
	if (ret == -EFAULT || ret == -ENOSPC)
969
       ret = i915_gem_shmem_pwrite(dev, obj, args, file);
962
       ret = i915_gem_shmem_pwrite(dev, obj, args, file);
970
 
963
 
971
out:
964
out:
972
	drm_gem_object_unreference(&obj->base);
965
	drm_gem_object_unreference(&obj->base);
973
unlock:
966
unlock:
974
	mutex_unlock(&dev->struct_mutex);
967
	mutex_unlock(&dev->struct_mutex);
975
	return ret;
968
	return ret;
976
}
969
}
977
 
970
 
978
int
971
int
979
i915_gem_check_wedge(struct i915_gpu_error *error,
972
i915_gem_check_wedge(struct i915_gpu_error *error,
980
		     bool interruptible)
973
		     bool interruptible)
981
{
974
{
982
	if (i915_reset_in_progress(error)) {
975
	if (i915_reset_in_progress(error)) {
983
		/* Non-interruptible callers can't handle -EAGAIN, hence return
976
		/* Non-interruptible callers can't handle -EAGAIN, hence return
984
		 * -EIO unconditionally for these. */
977
		 * -EIO unconditionally for these. */
985
		if (!interruptible)
978
		if (!interruptible)
986
			return -EIO;
979
			return -EIO;
987
 
980
 
988
		/* Recovery complete, but the reset failed ... */
981
		/* Recovery complete, but the reset failed ... */
989
		if (i915_terminally_wedged(error))
982
		if (i915_terminally_wedged(error))
990
			return -EIO;
983
			return -EIO;
991
 
984
 
992
		return -EAGAIN;
985
		return -EAGAIN;
993
	}
986
	}
994
 
987
 
995
	return 0;
988
	return 0;
996
}
989
}
997
 
990
 
998
/*
991
/*
999
 * Compare seqno against outstanding lazy request. Emit a request if they are
992
 * Compare seqno against outstanding lazy request. Emit a request if they are
1000
 * equal.
993
 * equal.
1001
 */
994
 */
1002
static int
995
static int
1003
i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
996
i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
1004
{
997
{
1005
	int ret;
998
	int ret;
1006
 
999
 
1007
	BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1000
	BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
1008
 
1001
 
1009
	ret = 0;
1002
	ret = 0;
1010
	if (seqno == ring->outstanding_lazy_request)
1003
	if (seqno == ring->outstanding_lazy_request)
1011
		ret = i915_add_request(ring, NULL, NULL);
1004
		ret = i915_add_request(ring, NULL, NULL);
1012
 
1005
 
1013
	return ret;
1006
	return ret;
1014
}
1007
}
1015
 
1008
 
1016
/**
1009
/**
1017
 * __wait_seqno - wait until execution of seqno has finished
1010
 * __wait_seqno - wait until execution of seqno has finished
1018
 * @ring: the ring expected to report seqno
1011
 * @ring: the ring expected to report seqno
1019
 * @seqno: duh!
1012
 * @seqno: duh!
1020
 * @reset_counter: reset sequence associated with the given seqno
1013
 * @reset_counter: reset sequence associated with the given seqno
1021
 * @interruptible: do an interruptible wait (normally yes)
1014
 * @interruptible: do an interruptible wait (normally yes)
1022
 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1015
 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
1023
 *
1016
 *
1024
 * Note: It is of utmost importance that the passed in seqno and reset_counter
1017
 * Note: It is of utmost importance that the passed in seqno and reset_counter
1025
 * values have been read by the caller in an smp safe manner. Where read-side
1018
 * values have been read by the caller in an smp safe manner. Where read-side
1026
 * locks are involved, it is sufficient to read the reset_counter before
1019
 * locks are involved, it is sufficient to read the reset_counter before
1027
 * unlocking the lock that protects the seqno. For lockless tricks, the
1020
 * unlocking the lock that protects the seqno. For lockless tricks, the
1028
 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1021
 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
1029
 * inserted.
1022
 * inserted.
1030
 *
1023
 *
1031
 * Returns 0 if the seqno was found within the alloted time. Else returns the
1024
 * Returns 0 if the seqno was found within the alloted time. Else returns the
1032
 * errno with remaining time filled in timeout argument.
1025
 * errno with remaining time filled in timeout argument.
1033
 */
1026
 */
1034
static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
1027
static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
1035
			unsigned reset_counter,
1028
			unsigned reset_counter,
1036
			bool interruptible, struct timespec *timeout)
1029
			bool interruptible, struct timespec *timeout)
1037
{
1030
{
1038
	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1031
	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1039
	struct timespec before, now, wait_time={1,0};
1032
	struct timespec before, now, wait_time={1,0};
1040
	unsigned long timeout_jiffies;
1033
	unsigned long timeout_jiffies;
1041
	long end;
1034
	long end;
1042
	bool wait_forever = true;
1035
	bool wait_forever = true;
1043
	int ret;
1036
	int ret;
1044
 
1037
 
1045
	if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
1038
	if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
1046
		return 0;
1039
		return 0;
1047
 
1040
 
1048
	trace_i915_gem_request_wait_begin(ring, seqno);
1041
	trace_i915_gem_request_wait_begin(ring, seqno);
1049
 
1042
 
1050
	if (timeout != NULL) {
1043
	if (timeout != NULL) {
1051
		wait_time = *timeout;
1044
		wait_time = *timeout;
1052
		wait_forever = false;
1045
		wait_forever = false;
1053
	}
1046
	}
1054
 
1047
 
1055
	timeout_jiffies = timespec_to_jiffies(&wait_time);
1048
	timeout_jiffies = timespec_to_jiffies(&wait_time);
1056
 
1049
 
1057
	if (WARN_ON(!ring->irq_get(ring)))
1050
	if (WARN_ON(!ring->irq_get(ring)))
1058
		return -ENODEV;
1051
		return -ENODEV;
1059
 
1052
 
1060
    /* Record current time in case interrupted by signal, or wedged * */
1053
    /* Record current time in case interrupted by signal, or wedged * */
1061
	getrawmonotonic(&before);
1054
	getrawmonotonic(&before);
1062
 
1055
 
1063
#define EXIT_COND \
1056
#define EXIT_COND \
1064
	(i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
1057
	(i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
1065
	 i915_reset_in_progress(&dev_priv->gpu_error) || \
1058
	 i915_reset_in_progress(&dev_priv->gpu_error) || \
1066
	 reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1059
	 reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1067
	do {
1060
	do {
1068
		if (interruptible)
1061
		if (interruptible)
1069
			end = wait_event_interruptible_timeout(ring->irq_queue,
1062
			end = wait_event_interruptible_timeout(ring->irq_queue,
1070
							       EXIT_COND,
1063
							       EXIT_COND,
1071
							       timeout_jiffies);
1064
							       timeout_jiffies);
1072
		else
1065
		else
1073
			end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1066
			end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1074
						 timeout_jiffies);
1067
						 timeout_jiffies);
1075
 
1068
 
1076
		/* We need to check whether any gpu reset happened in between
1069
		/* We need to check whether any gpu reset happened in between
1077
		 * the caller grabbing the seqno and now ... */
1070
		 * the caller grabbing the seqno and now ... */
1078
		if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1071
		if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1079
			end = -EAGAIN;
1072
			end = -EAGAIN;
1080
 
1073
 
1081
		/* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
1074
		/* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
1082
		 * gone. */
1075
		 * gone. */
1083
		ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1076
		ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1084
		if (ret)
1077
		if (ret)
1085
			end = ret;
1078
			end = ret;
1086
	} while (end == 0 && wait_forever);
1079
	} while (end == 0 && wait_forever);
1087
 
1080
 
1088
	getrawmonotonic(&now);
1081
	getrawmonotonic(&now);
1089
 
1082
 
1090
	ring->irq_put(ring);
1083
	ring->irq_put(ring);
1091
	trace_i915_gem_request_wait_end(ring, seqno);
1084
	trace_i915_gem_request_wait_end(ring, seqno);
1092
#undef EXIT_COND
1085
#undef EXIT_COND
1093
 
1086
 
1094
	if (timeout) {
1087
	if (timeout) {
1095
//       struct timespec sleep_time = timespec_sub(now, before);
1088
//       struct timespec sleep_time = timespec_sub(now, before);
1096
//       *timeout = timespec_sub(*timeout, sleep_time);
1089
//       *timeout = timespec_sub(*timeout, sleep_time);
1097
	}
1090
	}
1098
 
1091
 
1099
	switch (end) {
1092
	switch (end) {
1100
	case -EIO:
1093
	case -EIO:
1101
	case -EAGAIN: /* Wedged */
1094
	case -EAGAIN: /* Wedged */
1102
	case -ERESTARTSYS: /* Signal */
1095
	case -ERESTARTSYS: /* Signal */
1103
		return (int)end;
1096
		return (int)end;
1104
	case 0: /* Timeout */
1097
	case 0: /* Timeout */
1105
		if (timeout)
-
 
1106
			set_normalized_timespec(timeout, 0, 0);
-
 
1107
		return -ETIME;
1098
		return -ETIME;
1108
	default: /* Completed */
1099
	default: /* Completed */
1109
		WARN_ON(end < 0); /* We're not aware of other errors */
1100
		WARN_ON(end < 0); /* We're not aware of other errors */
1110
		return 0;
1101
		return 0;
1111
	}
1102
	}
1112
}
1103
}
1113
 
1104
 
1114
/**
1105
/**
1115
 * Waits for a sequence number to be signaled, and cleans up the
1106
 * Waits for a sequence number to be signaled, and cleans up the
1116
 * request and object lists appropriately for that event.
1107
 * request and object lists appropriately for that event.
1117
 */
1108
 */
1118
int
1109
int
1119
i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1110
i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1120
{
1111
{
1121
	struct drm_device *dev = ring->dev;
1112
	struct drm_device *dev = ring->dev;
1122
	struct drm_i915_private *dev_priv = dev->dev_private;
1113
	struct drm_i915_private *dev_priv = dev->dev_private;
1123
	bool interruptible = dev_priv->mm.interruptible;
1114
	bool interruptible = dev_priv->mm.interruptible;
1124
	int ret;
1115
	int ret;
1125
 
1116
 
1126
	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1117
	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1127
	BUG_ON(seqno == 0);
1118
	BUG_ON(seqno == 0);
1128
 
1119
 
1129
	ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1120
	ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1130
	if (ret)
1121
	if (ret)
1131
		return ret;
1122
		return ret;
1132
 
1123
 
1133
	ret = i915_gem_check_olr(ring, seqno);
1124
	ret = i915_gem_check_olr(ring, seqno);
1134
	if (ret)
1125
	if (ret)
1135
		return ret;
1126
		return ret;
1136
 
1127
 
1137
	return __wait_seqno(ring, seqno,
1128
	return __wait_seqno(ring, seqno,
1138
			    atomic_read(&dev_priv->gpu_error.reset_counter),
1129
			    atomic_read(&dev_priv->gpu_error.reset_counter),
1139
			    interruptible, NULL);
1130
			    interruptible, NULL);
1140
}
1131
}
1141
 
1132
 
1142
/**
1133
/**
1143
 * Ensures that all rendering to the object has completed and the object is
1134
 * Ensures that all rendering to the object has completed and the object is
1144
 * safe to unbind from the GTT or access from the CPU.
1135
 * safe to unbind from the GTT or access from the CPU.
1145
 */
1136
 */
1146
static __must_check int
1137
static __must_check int
1147
i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1138
i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1148
			       bool readonly)
1139
			       bool readonly)
1149
{
1140
{
1150
	struct intel_ring_buffer *ring = obj->ring;
1141
	struct intel_ring_buffer *ring = obj->ring;
1151
	u32 seqno;
1142
	u32 seqno;
1152
	int ret;
1143
	int ret;
1153
 
1144
 
1154
	seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1145
	seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1155
	if (seqno == 0)
1146
	if (seqno == 0)
1156
		return 0;
1147
		return 0;
1157
 
1148
 
1158
	ret = i915_wait_seqno(ring, seqno);
1149
	ret = i915_wait_seqno(ring, seqno);
1159
       if (ret)
1150
       if (ret)
1160
           return ret;
1151
           return ret;
1161
 
1152
 
1162
	i915_gem_retire_requests_ring(ring);
1153
	i915_gem_retire_requests_ring(ring);
1163
 
1154
 
1164
	/* Manually manage the write flush as we may have not yet
1155
	/* Manually manage the write flush as we may have not yet
1165
	 * retired the buffer.
1156
	 * retired the buffer.
1166
	 */
1157
	 */
1167
	if (obj->last_write_seqno &&
1158
	if (obj->last_write_seqno &&
1168
	    i915_seqno_passed(seqno, obj->last_write_seqno)) {
1159
	    i915_seqno_passed(seqno, obj->last_write_seqno)) {
1169
		obj->last_write_seqno = 0;
1160
		obj->last_write_seqno = 0;
1170
		obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1161
		obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1171
	}
1162
	}
1172
 
1163
 
1173
	return 0;
1164
	return 0;
1174
}
1165
}
1175
 
1166
 
1176
/* A nonblocking variant of the above wait. This is a highly dangerous routine
1167
/* A nonblocking variant of the above wait. This is a highly dangerous routine
1177
 * as the object state may change during this call.
1168
 * as the object state may change during this call.
1178
 */
1169
 */
1179
static __must_check int
1170
static __must_check int
1180
i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1171
i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1181
					    bool readonly)
1172
					    bool readonly)
1182
{
1173
{
1183
	struct drm_device *dev = obj->base.dev;
1174
	struct drm_device *dev = obj->base.dev;
1184
	struct drm_i915_private *dev_priv = dev->dev_private;
1175
	struct drm_i915_private *dev_priv = dev->dev_private;
1185
	struct intel_ring_buffer *ring = obj->ring;
1176
	struct intel_ring_buffer *ring = obj->ring;
1186
	unsigned reset_counter;
1177
	unsigned reset_counter;
1187
	u32 seqno;
1178
	u32 seqno;
1188
	int ret;
1179
	int ret;
1189
 
1180
 
1190
	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1181
	BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1191
	BUG_ON(!dev_priv->mm.interruptible);
1182
	BUG_ON(!dev_priv->mm.interruptible);
1192
 
1183
 
1193
	seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1184
	seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1194
	if (seqno == 0)
1185
	if (seqno == 0)
1195
		return 0;
1186
		return 0;
1196
 
1187
 
1197
	ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
1188
	ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
1198
	if (ret)
1189
	if (ret)
1199
		return ret;
1190
		return ret;
1200
 
1191
 
1201
	ret = i915_gem_check_olr(ring, seqno);
1192
	ret = i915_gem_check_olr(ring, seqno);
1202
	if (ret)
1193
	if (ret)
1203
		return ret;
1194
		return ret;
1204
 
1195
 
1205
	reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
1196
	reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
1206
	mutex_unlock(&dev->struct_mutex);
1197
	mutex_unlock(&dev->struct_mutex);
1207
	ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
1198
	ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
1208
	mutex_lock(&dev->struct_mutex);
1199
	mutex_lock(&dev->struct_mutex);
1209
 
1200
 
1210
	i915_gem_retire_requests_ring(ring);
1201
	i915_gem_retire_requests_ring(ring);
1211
 
1202
 
1212
	/* Manually manage the write flush as we may have not yet
1203
	/* Manually manage the write flush as we may have not yet
1213
	 * retired the buffer.
1204
	 * retired the buffer.
1214
	 */
1205
	 */
1215
	if (obj->last_write_seqno &&
1206
	if (obj->last_write_seqno &&
1216
	    i915_seqno_passed(seqno, obj->last_write_seqno)) {
1207
	    i915_seqno_passed(seqno, obj->last_write_seqno)) {
1217
		obj->last_write_seqno = 0;
1208
		obj->last_write_seqno = 0;
1218
		obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1209
		obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1219
	}
1210
	}
1220
 
1211
 
1221
	return ret;
1212
	return ret;
1222
}
1213
}
1223
 
1214
 
1224
/**
1215
/**
1225
 * Called when user space prepares to use an object with the CPU, either
1216
 * Called when user space prepares to use an object with the CPU, either
1226
 * through the mmap ioctl's mapping or a GTT mapping.
1217
 * through the mmap ioctl's mapping or a GTT mapping.
1227
 */
1218
 */
1228
int
1219
int
1229
i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1220
i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1230
			  struct drm_file *file)
1221
			  struct drm_file *file)
1231
{
1222
{
1232
	struct drm_i915_gem_set_domain *args = data;
1223
	struct drm_i915_gem_set_domain *args = data;
1233
	struct drm_i915_gem_object *obj;
1224
	struct drm_i915_gem_object *obj;
1234
	uint32_t read_domains = args->read_domains;
1225
	uint32_t read_domains = args->read_domains;
1235
	uint32_t write_domain = args->write_domain;
1226
	uint32_t write_domain = args->write_domain;
1236
	int ret;
1227
	int ret;
1237
 
1228
 
1238
 
1229
 
1239
     if(args->handle == -2)
1230
     if(args->handle == -2)
1240
     {
1231
     {
1241
        printf("%s handle %d\n", __FUNCTION__, args->handle);
1232
        printf("%s handle %d\n", __FUNCTION__, args->handle);
1242
        return 0;
1233
        return 0;
1243
     }
1234
     }
1244
 
1235
 
1245
	/* Only handle setting domains to types used by the CPU. */
1236
	/* Only handle setting domains to types used by the CPU. */
1246
	if (write_domain & I915_GEM_GPU_DOMAINS)
1237
	if (write_domain & I915_GEM_GPU_DOMAINS)
1247
		return -EINVAL;
1238
		return -EINVAL;
1248
 
1239
 
1249
	if (read_domains & I915_GEM_GPU_DOMAINS)
1240
	if (read_domains & I915_GEM_GPU_DOMAINS)
1250
		return -EINVAL;
1241
		return -EINVAL;
1251
 
1242
 
1252
	/* Having something in the write domain implies it's in the read
1243
	/* Having something in the write domain implies it's in the read
1253
	 * domain, and only that read domain.  Enforce that in the request.
1244
	 * domain, and only that read domain.  Enforce that in the request.
1254
	 */
1245
	 */
1255
	if (write_domain != 0 && read_domains != write_domain)
1246
	if (write_domain != 0 && read_domains != write_domain)
1256
		return -EINVAL;
1247
		return -EINVAL;
1257
 
1248
 
1258
	ret = i915_mutex_lock_interruptible(dev);
1249
	ret = i915_mutex_lock_interruptible(dev);
1259
	if (ret)
1250
	if (ret)
1260
		return ret;
1251
		return ret;
1261
 
1252
 
1262
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1253
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1263
	if (&obj->base == NULL) {
1254
	if (&obj->base == NULL) {
1264
		ret = -ENOENT;
1255
		ret = -ENOENT;
1265
		goto unlock;
1256
		goto unlock;
1266
	}
1257
	}
1267
 
1258
 
1268
	/* Try to flush the object off the GPU without holding the lock.
1259
	/* Try to flush the object off the GPU without holding the lock.
1269
	 * We will repeat the flush holding the lock in the normal manner
1260
	 * We will repeat the flush holding the lock in the normal manner
1270
	 * to catch cases where we are gazumped.
1261
	 * to catch cases where we are gazumped.
1271
	 */
1262
	 */
1272
	ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1263
	ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1273
	if (ret)
1264
	if (ret)
1274
		goto unref;
1265
		goto unref;
1275
 
1266
 
1276
	if (read_domains & I915_GEM_DOMAIN_GTT) {
1267
	if (read_domains & I915_GEM_DOMAIN_GTT) {
1277
		ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1268
		ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1278
 
1269
 
1279
		/* Silently promote "you're not bound, there was nothing to do"
1270
		/* Silently promote "you're not bound, there was nothing to do"
1280
		 * to success, since the client was just asking us to
1271
		 * to success, since the client was just asking us to
1281
		 * make sure everything was done.
1272
		 * make sure everything was done.
1282
		 */
1273
		 */
1283
		if (ret == -EINVAL)
1274
		if (ret == -EINVAL)
1284
			ret = 0;
1275
			ret = 0;
1285
	} else {
1276
	} else {
1286
		ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1277
		ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1287
	}
1278
	}
1288
 
1279
 
1289
unref:
1280
unref:
1290
	drm_gem_object_unreference(&obj->base);
1281
	drm_gem_object_unreference(&obj->base);
1291
unlock:
1282
unlock:
1292
	mutex_unlock(&dev->struct_mutex);
1283
	mutex_unlock(&dev->struct_mutex);
1293
	return ret;
1284
	return ret;
1294
}
1285
}
1295
 
1286
 
1296
 
1287
 
1297
 
1288
 
1298
 
1289
 
1299
 
1290
 
1300
 
1291
 
1301
/**
1292
/**
1302
 * Maps the contents of an object, returning the address it is mapped
1293
 * Maps the contents of an object, returning the address it is mapped
1303
 * into.
1294
 * into.
1304
 *
1295
 *
1305
 * While the mapping holds a reference on the contents of the object, it doesn't
1296
 * While the mapping holds a reference on the contents of the object, it doesn't
1306
 * imply a ref on the object itself.
1297
 * imply a ref on the object itself.
1307
 */
1298
 */
1308
int
1299
int
1309
i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1300
i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1310
		    struct drm_file *file)
1301
		    struct drm_file *file)
1311
{
1302
{
1312
	struct drm_i915_gem_mmap *args = data;
1303
	struct drm_i915_gem_mmap *args = data;
1313
	struct drm_gem_object *obj;
1304
	struct drm_gem_object *obj;
1314
	unsigned long addr = 0;
1305
	unsigned long addr = 0;
1315
 
1306
 
1316
     if(args->handle == -2)
1307
     if(args->handle == -2)
1317
     {
1308
     {
1318
        printf("%s handle %d\n", __FUNCTION__, args->handle);
1309
        printf("%s handle %d\n", __FUNCTION__, args->handle);
1319
        return 0;
1310
        return 0;
1320
     }
1311
     }
1321
 
1312
 
1322
	obj = drm_gem_object_lookup(dev, file, args->handle);
1313
	obj = drm_gem_object_lookup(dev, file, args->handle);
1323
	if (obj == NULL)
1314
	if (obj == NULL)
1324
		return -ENOENT;
1315
		return -ENOENT;
1325
	
1316
	
1326
    //dbgprintf("%s offset %lld size %lld\n",
1317
    //dbgprintf("%s offset %lld size %lld\n",
1327
//                __FUNCTION__, args->offset, args->size);
1318
//                __FUNCTION__, args->offset, args->size);
1328
	/* prime objects have no backing filp to GEM mmap
1319
	/* prime objects have no backing filp to GEM mmap
1329
	 * pages from.
1320
	 * pages from.
1330
	 */
1321
	 */
1331
	if (!obj->filp) {
1322
	if (!obj->filp) {
1332
		drm_gem_object_unreference_unlocked(obj);
1323
		drm_gem_object_unreference_unlocked(obj);
1333
		return -EINVAL;
1324
		return -EINVAL;
1334
	}
1325
	}
1335
 
1326
 
1336
    addr = vm_mmap(obj->filp, 0, args->size,
1327
    addr = vm_mmap(obj->filp, 0, args->size,
1337
              PROT_READ | PROT_WRITE, MAP_SHARED,
1328
              PROT_READ | PROT_WRITE, MAP_SHARED,
1338
              args->offset);
1329
              args->offset);
1339
	drm_gem_object_unreference_unlocked(obj);
1330
	drm_gem_object_unreference_unlocked(obj);
1340
    if (IS_ERR((void *)addr))
1331
    if (IS_ERR((void *)addr))
1341
        return addr;
1332
        return addr;
1342
 
1333
 
1343
	args->addr_ptr = (uint64_t) addr;
1334
	args->addr_ptr = (uint64_t) addr;
1344
 
1335
 
1345
    return 0;
1336
    return 0;
1346
}
1337
}
1347
 
1338
 
1348
 
1339
 
1349
 
1340
 
1350
 
1341
 
1351
 
1342
 
1352
 
1343
 
1353
 
1344
 
1354
 
1345
 
1355
 
1346
 
1356
 
1347
 
1357
 
1348
 
1358
 
1349
 
1359
 
1350
 
1360
/**
1351
/**
1361
 * i915_gem_release_mmap - remove physical page mappings
1352
 * i915_gem_release_mmap - remove physical page mappings
1362
 * @obj: obj in question
1353
 * @obj: obj in question
1363
 *
1354
 *
1364
 * Preserve the reservation of the mmapping with the DRM core code, but
1355
 * Preserve the reservation of the mmapping with the DRM core code, but
1365
 * relinquish ownership of the pages back to the system.
1356
 * relinquish ownership of the pages back to the system.
1366
 *
1357
 *
1367
 * It is vital that we remove the page mapping if we have mapped a tiled
1358
 * It is vital that we remove the page mapping if we have mapped a tiled
1368
 * object through the GTT and then lose the fence register due to
1359
 * object through the GTT and then lose the fence register due to
1369
 * resource pressure. Similarly if the object has been moved out of the
1360
 * resource pressure. Similarly if the object has been moved out of the
1370
 * aperture, than pages mapped into userspace must be revoked. Removing the
1361
 * aperture, than pages mapped into userspace must be revoked. Removing the
1371
 * mapping will then trigger a page fault on the next user access, allowing
1362
 * mapping will then trigger a page fault on the next user access, allowing
1372
 * fixup by i915_gem_fault().
1363
 * fixup by i915_gem_fault().
1373
 */
1364
 */
1374
void
1365
void
1375
i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1366
i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1376
{
1367
{
1377
	if (!obj->fault_mappable)
1368
	if (!obj->fault_mappable)
1378
		return;
1369
		return;
1379
 
1370
 
1380
	if (obj->base.dev->dev_mapping)
1371
	if (obj->base.dev->dev_mapping)
1381
//		unmap_mapping_range(obj->base.dev->dev_mapping,
1372
//		unmap_mapping_range(obj->base.dev->dev_mapping,
1382
//				    (loff_t)obj->base.map_list.hash.key<
1373
//				    (loff_t)obj->base.map_list.hash.key<
1383
//				    obj->base.size, 1);
1374
//				    obj->base.size, 1);
1384
 
1375
 
1385
	obj->fault_mappable = false;
1376
	obj->fault_mappable = false;
1386
}
1377
}
1387
 
1378
 
1388
uint32_t
1379
uint32_t
1389
i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1380
i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1390
{
1381
{
1391
	uint32_t gtt_size;
1382
	uint32_t gtt_size;
1392
 
1383
 
1393
	if (INTEL_INFO(dev)->gen >= 4 ||
1384
	if (INTEL_INFO(dev)->gen >= 4 ||
1394
	    tiling_mode == I915_TILING_NONE)
1385
	    tiling_mode == I915_TILING_NONE)
1395
		return size;
1386
		return size;
1396
 
1387
 
1397
	/* Previous chips need a power-of-two fence region when tiling */
1388
	/* Previous chips need a power-of-two fence region when tiling */
1398
	if (INTEL_INFO(dev)->gen == 3)
1389
	if (INTEL_INFO(dev)->gen == 3)
1399
		gtt_size = 1024*1024;
1390
		gtt_size = 1024*1024;
1400
	else
1391
	else
1401
		gtt_size = 512*1024;
1392
		gtt_size = 512*1024;
1402
 
1393
 
1403
	while (gtt_size < size)
1394
	while (gtt_size < size)
1404
		gtt_size <<= 1;
1395
		gtt_size <<= 1;
1405
 
1396
 
1406
	return gtt_size;
1397
	return gtt_size;
1407
}
1398
}
1408
 
1399
 
1409
/**
1400
/**
1410
 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1401
 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1411
 * @obj: object to check
1402
 * @obj: object to check
1412
 *
1403
 *
1413
 * Return the required GTT alignment for an object, taking into account
1404
 * Return the required GTT alignment for an object, taking into account
1414
 * potential fence register mapping.
1405
 * potential fence register mapping.
1415
 */
1406
 */
1416
uint32_t
1407
uint32_t
1417
i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1408
i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1418
			   int tiling_mode, bool fenced)
1409
			   int tiling_mode, bool fenced)
1419
{
1410
{
1420
	/*
1411
	/*
1421
	 * Minimum alignment is 4k (GTT page size), but might be greater
1412
	 * Minimum alignment is 4k (GTT page size), but might be greater
1422
	 * if a fence register is needed for the object.
1413
	 * if a fence register is needed for the object.
1423
	 */
1414
	 */
1424
	if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
1415
	if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
1425
	    tiling_mode == I915_TILING_NONE)
1416
	    tiling_mode == I915_TILING_NONE)
1426
		return 4096;
1417
		return 4096;
1427
 
1418
 
1428
	/*
1419
	/*
1429
	 * Previous chips need to be aligned to the size of the smallest
1420
	 * Previous chips need to be aligned to the size of the smallest
1430
	 * fence register that can contain the object.
1421
	 * fence register that can contain the object.
1431
	 */
1422
	 */
1432
	return i915_gem_get_gtt_size(dev, size, tiling_mode);
1423
	return i915_gem_get_gtt_size(dev, size, tiling_mode);
1433
}
1424
}
1434
 
1425
 
1435
/**
1426
/**
1436
 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1427
 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1437
 *					 unfenced object
1428
 *					 unfenced object
1438
 * @dev: the device
1429
 * @dev: the device
1439
 * @size: size of the object
1430
 * @size: size of the object
1440
 * @tiling_mode: tiling mode of the object
1431
 * @tiling_mode: tiling mode of the object
1441
 *
1432
 *
1442
 * Return the required GTT alignment for an object, only taking into account
1433
 * Return the required GTT alignment for an object, only taking into account
1443
 * unfenced tiled surface requirements.
1434
 * unfenced tiled surface requirements.
1444
 */
1435
 */
1445
uint32_t
1436
uint32_t
1446
i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1437
i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1447
				    uint32_t size,
1438
				    uint32_t size,
1448
				    int tiling_mode)
1439
				    int tiling_mode)
1449
{
1440
{
1450
	/*
1441
	/*
1451
	 * Minimum alignment is 4k (GTT page size) for sane hw.
1442
	 * Minimum alignment is 4k (GTT page size) for sane hw.
1452
	 */
1443
	 */
1453
	if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1444
	if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1454
	    tiling_mode == I915_TILING_NONE)
1445
	    tiling_mode == I915_TILING_NONE)
1455
		return 4096;
1446
		return 4096;
1456
 
1447
 
1457
	/* Previous hardware however needs to be aligned to a power-of-two
1448
	/* Previous hardware however needs to be aligned to a power-of-two
1458
	 * tile height. The simplest method for determining this is to reuse
1449
	 * tile height. The simplest method for determining this is to reuse
1459
	 * the power-of-tile object size.
1450
	 * the power-of-tile object size.
1460
	 */
1451
	 */
1461
	return i915_gem_get_gtt_size(dev, size, tiling_mode);
1452
	return i915_gem_get_gtt_size(dev, size, tiling_mode);
1462
}
1453
}
1463
 
1454
 
1464
int
1455
int
1465
i915_gem_mmap_gtt(struct drm_file *file,
1456
i915_gem_mmap_gtt(struct drm_file *file,
1466
          struct drm_device *dev,
1457
          struct drm_device *dev,
1467
          uint32_t handle,
1458
          uint32_t handle,
1468
          uint64_t *offset)
1459
          uint64_t *offset)
1469
{
1460
{
1470
    struct drm_i915_private *dev_priv = dev->dev_private;
1461
    struct drm_i915_private *dev_priv = dev->dev_private;
1471
    struct drm_i915_gem_object *obj;
1462
    struct drm_i915_gem_object *obj;
1472
    unsigned long pfn;
1463
    unsigned long pfn;
1473
    char *mem, *ptr;
1464
    char *mem, *ptr;
1474
    int ret;
1465
    int ret;
1475
 
1466
 
1476
    ret = i915_mutex_lock_interruptible(dev);
1467
    ret = i915_mutex_lock_interruptible(dev);
1477
    if (ret)
1468
    if (ret)
1478
        return ret;
1469
        return ret;
1479
 
1470
 
1480
    obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1471
    obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1481
    if (&obj->base == NULL) {
1472
    if (&obj->base == NULL) {
1482
        ret = -ENOENT;
1473
        ret = -ENOENT;
1483
        goto unlock;
1474
        goto unlock;
1484
    }
1475
    }
1485
 
1476
 
1486
    if (obj->base.size > dev_priv->gtt.mappable_end) {
1477
    if (obj->base.size > dev_priv->gtt.mappable_end) {
1487
        ret = -E2BIG;
1478
        ret = -E2BIG;
1488
        goto out;
1479
        goto out;
1489
    }
1480
    }
1490
 
1481
 
1491
    if (obj->madv != I915_MADV_WILLNEED) {
1482
    if (obj->madv != I915_MADV_WILLNEED) {
1492
        DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1483
        DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1493
        ret = -EINVAL;
1484
        ret = -EINVAL;
1494
        goto out;
1485
        goto out;
1495
    }
1486
    }
1496
    /* Now bind it into the GTT if needed */
1487
    /* Now bind it into the GTT if needed */
1497
    ret = i915_gem_object_pin(obj, 0, true, false);
1488
    ret = i915_gem_object_pin(obj, 0, true, false);
1498
    if (ret)
1489
    if (ret)
1499
        goto out;
1490
        goto out;
1500
 
1491
 
1501
    ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1492
    ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1502
    if (ret)
1493
    if (ret)
1503
        goto unpin;
1494
        goto unpin;
1504
 
1495
 
1505
    ret = i915_gem_object_get_fence(obj);
1496
    ret = i915_gem_object_get_fence(obj);
1506
    if (ret)
1497
    if (ret)
1507
        goto unpin;
1498
        goto unpin;
1508
 
1499
 
1509
    obj->fault_mappable = true;
1500
    obj->fault_mappable = true;
1510
 
1501
 
1511
    pfn = dev_priv->gtt.mappable_base + obj->gtt_offset;
1502
    pfn = dev_priv->gtt.mappable_base + obj->gtt_offset;
1512
 
1503
 
1513
    /* Finally, remap it using the new GTT offset */
1504
    /* Finally, remap it using the new GTT offset */
1514
 
1505
 
1515
    mem = UserAlloc(obj->base.size);
1506
    mem = UserAlloc(obj->base.size);
1516
    if(unlikely(mem == NULL))
1507
    if(unlikely(mem == NULL))
1517
    {
1508
    {
1518
        ret = -ENOMEM;
1509
        ret = -ENOMEM;
1519
        goto unpin;
1510
        goto unpin;
1520
    }
1511
    }
1521
 
1512
 
1522
    for(ptr = mem; ptr < mem + obj->base.size; ptr+= 4096, pfn+= 4096)
1513
    for(ptr = mem; ptr < mem + obj->base.size; ptr+= 4096, pfn+= 4096)
1523
        MapPage(ptr, pfn, PG_SHARED|PG_UW);
1514
        MapPage(ptr, pfn, PG_SHARED|PG_UW);
1524
 
1515
 
1525
unpin:
1516
unpin:
1526
    i915_gem_object_unpin(obj);
1517
    i915_gem_object_unpin(obj);
1527
 
1518
 
1528
 
1519
 
1529
    *offset = (u64)mem;
1520
    *offset = (u64)mem;
1530
 
1521
 
1531
out:
1522
out:
1532
    drm_gem_object_unreference(&obj->base);
1523
    drm_gem_object_unreference(&obj->base);
1533
unlock:
1524
unlock:
1534
    mutex_unlock(&dev->struct_mutex);
1525
    mutex_unlock(&dev->struct_mutex);
1535
    return ret;
1526
    return ret;
1536
}
1527
}
1537
 
1528
 
1538
/**
1529
/**
1539
 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1530
 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1540
 * @dev: DRM device
1531
 * @dev: DRM device
1541
 * @data: GTT mapping ioctl data
1532
 * @data: GTT mapping ioctl data
1542
 * @file: GEM object info
1533
 * @file: GEM object info
1543
 *
1534
 *
1544
 * Simply returns the fake offset to userspace so it can mmap it.
1535
 * Simply returns the fake offset to userspace so it can mmap it.
1545
 * The mmap call will end up in drm_gem_mmap(), which will set things
1536
 * The mmap call will end up in drm_gem_mmap(), which will set things
1546
 * up so we can get faults in the handler above.
1537
 * up so we can get faults in the handler above.
1547
 *
1538
 *
1548
 * The fault handler will take care of binding the object into the GTT
1539
 * The fault handler will take care of binding the object into the GTT
1549
 * (since it may have been evicted to make room for something), allocating
1540
 * (since it may have been evicted to make room for something), allocating
1550
 * a fence register, and mapping the appropriate aperture address into
1541
 * a fence register, and mapping the appropriate aperture address into
1551
 * userspace.
1542
 * userspace.
1552
 */
1543
 */
1553
int
1544
int
1554
i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1545
i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1555
            struct drm_file *file)
1546
            struct drm_file *file)
1556
{
1547
{
1557
    struct drm_i915_gem_mmap_gtt *args = data;
1548
    struct drm_i915_gem_mmap_gtt *args = data;
1558
 
1549
 
1559
    return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1550
    return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1560
}
1551
}
1561
 
1552
 
1562
/* Immediately discard the backing storage */
1553
/* Immediately discard the backing storage */
1563
static void
1554
static void
1564
i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1555
i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1565
{
1556
{
1566
//	struct inode *inode;
1557
//	struct inode *inode;
1567
 
1558
 
1568
//	i915_gem_object_free_mmap_offset(obj);
1559
//	i915_gem_object_free_mmap_offset(obj);
1569
 
1560
 
1570
	if (obj->base.filp == NULL)
1561
	if (obj->base.filp == NULL)
1571
		return;
1562
		return;
1572
 
1563
 
1573
	/* Our goal here is to return as much of the memory as
1564
	/* Our goal here is to return as much of the memory as
1574
	 * is possible back to the system as we are called from OOM.
1565
	 * is possible back to the system as we are called from OOM.
1575
	 * To do this we must instruct the shmfs to drop all of its
1566
	 * To do this we must instruct the shmfs to drop all of its
1576
	 * backing pages, *now*.
1567
	 * backing pages, *now*.
1577
	 */
1568
	 */
1578
//	inode = obj->base.filp->f_path.dentry->d_inode;
1569
//	inode = obj->base.filp->f_path.dentry->d_inode;
1579
//	shmem_truncate_range(inode, 0, (loff_t)-1);
1570
//	shmem_truncate_range(inode, 0, (loff_t)-1);
1580
 
1571
 
1581
	obj->madv = __I915_MADV_PURGED;
1572
	obj->madv = __I915_MADV_PURGED;
1582
}
1573
}
1583
 
1574
 
1584
static inline int
1575
static inline int
1585
i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1576
i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1586
{
1577
{
1587
	return obj->madv == I915_MADV_DONTNEED;
1578
	return obj->madv == I915_MADV_DONTNEED;
1588
}
1579
}
1589
 
1580
 
1590
static void
1581
static void
1591
i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1582
i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1592
{
1583
{
1593
	int page_count = obj->base.size / PAGE_SIZE;
-
 
1594
	struct scatterlist *sg;
1584
	struct sg_page_iter sg_iter;
1595
	int ret, i;
1585
	int ret;
1596
 
1586
 
1597
	BUG_ON(obj->madv == __I915_MADV_PURGED);
1587
	BUG_ON(obj->madv == __I915_MADV_PURGED);
1598
 
1588
 
1599
	ret = i915_gem_object_set_to_cpu_domain(obj, true);
1589
	ret = i915_gem_object_set_to_cpu_domain(obj, true);
1600
	if (ret) {
1590
	if (ret) {
1601
		/* In the event of a disaster, abandon all caches and
1591
		/* In the event of a disaster, abandon all caches and
1602
		 * hope for the best.
1592
		 * hope for the best.
1603
		 */
1593
		 */
1604
		WARN_ON(ret != -EIO);
1594
		WARN_ON(ret != -EIO);
1605
		i915_gem_clflush_object(obj);
1595
		i915_gem_clflush_object(obj);
1606
		obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1596
		obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1607
	}
1597
	}
1608
 
1598
 
1609
	if (obj->madv == I915_MADV_DONTNEED)
1599
	if (obj->madv == I915_MADV_DONTNEED)
1610
		obj->dirty = 0;
1600
		obj->dirty = 0;
1611
 
1601
 
1612
	for_each_sg(obj->pages->sgl, sg, page_count, i) {
1602
	for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
1613
		struct page *page = sg_page(sg);
1603
		struct page *page = sg_page_iter_page(&sg_iter);
1614
 
1604
 
1615
        page_cache_release(page);
1605
        page_cache_release(page);
1616
	}
1606
	}
1617
    //DRM_DEBUG_KMS("%s release %d pages\n", __FUNCTION__, page_count);
1607
    //DRM_DEBUG_KMS("%s release %d pages\n", __FUNCTION__, page_count);
1618
 
1608
 
1619
	obj->dirty = 0;
1609
	obj->dirty = 0;
1620
 
1610
 
1621
	sg_free_table(obj->pages);
1611
	sg_free_table(obj->pages);
1622
	kfree(obj->pages);
1612
	kfree(obj->pages);
1623
}
1613
}
1624
 
1614
 
1625
int
1615
int
1626
i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1616
i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1627
{
1617
{
1628
	const struct drm_i915_gem_object_ops *ops = obj->ops;
1618
	const struct drm_i915_gem_object_ops *ops = obj->ops;
1629
 
1619
 
1630
	if (obj->pages == NULL)
1620
	if (obj->pages == NULL)
1631
		return 0;
1621
		return 0;
1632
 
1622
 
1633
	BUG_ON(obj->gtt_space);
1623
	BUG_ON(obj->gtt_space);
1634
 
1624
 
1635
	if (obj->pages_pin_count)
1625
	if (obj->pages_pin_count)
1636
		return -EBUSY;
1626
		return -EBUSY;
1637
 
1627
 
1638
	/* ->put_pages might need to allocate memory for the bit17 swizzle
1628
	/* ->put_pages might need to allocate memory for the bit17 swizzle
1639
	 * array, hence protect them from being reaped by removing them from gtt
1629
	 * array, hence protect them from being reaped by removing them from gtt
1640
	 * lists early. */
1630
	 * lists early. */
1641
	list_del(&obj->gtt_list);
1631
	list_del(&obj->gtt_list);
1642
 
1632
 
1643
	ops->put_pages(obj);
1633
	ops->put_pages(obj);
1644
	obj->pages = NULL;
1634
	obj->pages = NULL;
1645
 
1635
 
1646
	if (i915_gem_object_is_purgeable(obj))
1636
	if (i915_gem_object_is_purgeable(obj))
1647
		i915_gem_object_truncate(obj);
1637
		i915_gem_object_truncate(obj);
1648
 
1638
 
1649
	return 0;
1639
	return 0;
1650
}
1640
}
1651
 
1641
 
1652
 
1642
 
1653
 
1643
 
1654
 
1644
 
1655
 
1645
 
1656
 
1646
 
1657
 
1647
 
1658
 
1648
 
1659
static int
1649
static int
1660
i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
1650
i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
1661
{
1651
{
1662
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1652
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1663
    int page_count, i;
1653
    int page_count, i;
1664
	struct address_space *mapping;
-
 
1665
	struct sg_table *st;
1654
	struct sg_table *st;
1666
	struct scatterlist *sg;
1655
	struct scatterlist *sg;
-
 
1656
	struct sg_page_iter sg_iter;
1667
	struct page *page;
1657
	struct page *page;
-
 
1658
	unsigned long last_pfn = 0;	/* suppress gcc warning */
1668
	gfp_t gfp;
1659
	gfp_t gfp;
1669
 
1660
 
1670
	/* Assert that the object is not currently in any GPU domain. As it
1661
	/* Assert that the object is not currently in any GPU domain. As it
1671
	 * wasn't in the GTT, there shouldn't be any way it could have been in
1662
	 * wasn't in the GTT, there shouldn't be any way it could have been in
1672
	 * a GPU cache
1663
	 * a GPU cache
1673
	 */
1664
	 */
1674
	BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1665
	BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1675
	BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1666
	BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1676
 
1667
 
1677
	st = kmalloc(sizeof(*st), GFP_KERNEL);
1668
	st = kmalloc(sizeof(*st), GFP_KERNEL);
1678
	if (st == NULL)
1669
	if (st == NULL)
1679
		return -ENOMEM;
1670
		return -ENOMEM;
1680
 
1671
 
1681
	page_count = obj->base.size / PAGE_SIZE;
1672
	page_count = obj->base.size / PAGE_SIZE;
1682
	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
1673
	if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
1683
		sg_free_table(st);
1674
		sg_free_table(st);
1684
		kfree(st);
1675
		kfree(st);
-
 
1676
        FAIL();
1685
		return -ENOMEM;
1677
		return -ENOMEM;
1686
	}
1678
	}
1687
 
1679
 
1688
	/* Get the list of pages out of our struct file.  They'll be pinned
1680
	/* Get the list of pages out of our struct file.  They'll be pinned
1689
	 * at this point until we release them.
1681
	 * at this point until we release them.
1690
	 *
1682
	 *
1691
	 * Fail silently without starting the shrinker
1683
	 * Fail silently without starting the shrinker
1692
	 */
1684
	 */
-
 
1685
	sg = st->sgl;
-
 
1686
	st->nents = 0;
1693
	for_each_sg(st->sgl, sg, page_count, i) {
1687
	for (i = 0; i < page_count; i++) {
1694
		page = shmem_read_mapping_page_gfp(obj->base.filp, i, gfp);
1688
		page = shmem_read_mapping_page_gfp(obj->base.filp, i, gfp);
1695
		if (IS_ERR(page)) {
1689
		if (IS_ERR(page)) {
1696
            dbgprintf("%s invalid page %p\n", __FUNCTION__, page);
1690
            dbgprintf("%s invalid page %p\n", __FUNCTION__, page);
1697
			goto err_pages;
1691
			goto err_pages;
1698
 
1692
 
1699
		}
1693
		}
-
 
1694
 
-
 
1695
		if (!i || page_to_pfn(page) != last_pfn + 1) {
-
 
1696
			if (i)
-
 
1697
				sg = sg_next(sg);
-
 
1698
			st->nents++;
1700
		sg_set_page(sg, page, PAGE_SIZE, 0);
1699
		sg_set_page(sg, page, PAGE_SIZE, 0);
-
 
1700
		} else {
-
 
1701
			sg->length += PAGE_SIZE;
-
 
1702
		}
-
 
1703
		last_pfn = page_to_pfn(page);
1701
	}
1704
	}
-
 
1705
 
1702
 
1706
		sg_mark_end(sg);
1703
	obj->pages = st;
-
 
1704
 
-
 
1705
//    DRM_DEBUG_KMS("%s alloc %d pages\n", __FUNCTION__, page_count);
1707
	obj->pages = st;
1706
 
1708
 
1707
	return 0;
1709
	return 0;
1708
 
1710
 
1709
err_pages:
1711
err_pages:
-
 
1712
	sg_mark_end(sg);
1710
	for_each_sg(st->sgl, sg, i, page_count)
1713
	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
1711
		page_cache_release(sg_page(sg));
1714
		page_cache_release(sg_page_iter_page(&sg_iter));
1712
	sg_free_table(st);
1715
	sg_free_table(st);
1713
	kfree(st);
1716
	kfree(st);
-
 
1717
    FAIL();
1714
	return PTR_ERR(page);
1718
	return PTR_ERR(page);
1715
}
1719
}
1716
 
1720
 
1717
/* Ensure that the associated pages are gathered from the backing storage
1721
/* Ensure that the associated pages are gathered from the backing storage
1718
 * and pinned into our object. i915_gem_object_get_pages() may be called
1722
 * and pinned into our object. i915_gem_object_get_pages() may be called
1719
 * multiple times before they are released by a single call to
1723
 * multiple times before they are released by a single call to
1720
 * i915_gem_object_put_pages() - once the pages are no longer referenced
1724
 * i915_gem_object_put_pages() - once the pages are no longer referenced
1721
 * either as a result of memory pressure (reaping pages under the shrinker)
1725
 * either as a result of memory pressure (reaping pages under the shrinker)
1722
 * or as the object is itself released.
1726
 * or as the object is itself released.
1723
 */
1727
 */
1724
int
1728
int
1725
i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1729
i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1726
{
1730
{
1727
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1731
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1728
	const struct drm_i915_gem_object_ops *ops = obj->ops;
1732
	const struct drm_i915_gem_object_ops *ops = obj->ops;
1729
	int ret;
1733
	int ret;
1730
 
1734
 
1731
	if (obj->pages)
1735
	if (obj->pages)
1732
		return 0;
1736
		return 0;
1733
 
1737
 
1734
	BUG_ON(obj->pages_pin_count);
1738
	BUG_ON(obj->pages_pin_count);
1735
 
1739
 
1736
	ret = ops->get_pages(obj);
1740
	ret = ops->get_pages(obj);
1737
	if (ret)
1741
	if (ret)
1738
		return ret;
1742
		return ret;
1739
 
1743
 
1740
	list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
1744
	list_add_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
1741
    return 0;
1745
    return 0;
1742
}
1746
}
1743
 
1747
 
1744
void
1748
void
1745
i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1749
i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1746
			       struct intel_ring_buffer *ring)
1750
			       struct intel_ring_buffer *ring)
1747
{
1751
{
1748
	struct drm_device *dev = obj->base.dev;
1752
	struct drm_device *dev = obj->base.dev;
1749
	struct drm_i915_private *dev_priv = dev->dev_private;
1753
	struct drm_i915_private *dev_priv = dev->dev_private;
1750
	u32 seqno = intel_ring_get_seqno(ring);
1754
	u32 seqno = intel_ring_get_seqno(ring);
1751
 
1755
 
1752
	BUG_ON(ring == NULL);
1756
	BUG_ON(ring == NULL);
1753
	obj->ring = ring;
1757
	obj->ring = ring;
1754
 
1758
 
1755
	/* Add a reference if we're newly entering the active list. */
1759
	/* Add a reference if we're newly entering the active list. */
1756
	if (!obj->active) {
1760
	if (!obj->active) {
1757
		drm_gem_object_reference(&obj->base);
1761
		drm_gem_object_reference(&obj->base);
1758
		obj->active = 1;
1762
		obj->active = 1;
1759
	}
1763
	}
1760
 
1764
 
1761
	/* Move from whatever list we were on to the tail of execution. */
1765
	/* Move from whatever list we were on to the tail of execution. */
1762
	list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1766
	list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1763
	list_move_tail(&obj->ring_list, &ring->active_list);
1767
	list_move_tail(&obj->ring_list, &ring->active_list);
1764
 
1768
 
1765
	obj->last_read_seqno = seqno;
1769
	obj->last_read_seqno = seqno;
1766
 
1770
 
1767
	if (obj->fenced_gpu_access) {
1771
	if (obj->fenced_gpu_access) {
1768
		obj->last_fenced_seqno = seqno;
1772
		obj->last_fenced_seqno = seqno;
1769
 
1773
 
1770
		/* Bump MRU to take account of the delayed flush */
1774
		/* Bump MRU to take account of the delayed flush */
1771
		if (obj->fence_reg != I915_FENCE_REG_NONE) {
1775
		if (obj->fence_reg != I915_FENCE_REG_NONE) {
1772
		struct drm_i915_fence_reg *reg;
1776
		struct drm_i915_fence_reg *reg;
1773
 
1777
 
1774
		reg = &dev_priv->fence_regs[obj->fence_reg];
1778
		reg = &dev_priv->fence_regs[obj->fence_reg];
1775
			list_move_tail(®->lru_list,
1779
			list_move_tail(®->lru_list,
1776
				       &dev_priv->mm.fence_list);
1780
				       &dev_priv->mm.fence_list);
1777
		}
1781
		}
1778
	}
1782
	}
1779
}
1783
}
1780
 
1784
 
1781
static void
1785
static void
1782
i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1786
i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1783
{
1787
{
1784
	struct drm_device *dev = obj->base.dev;
1788
	struct drm_device *dev = obj->base.dev;
1785
	struct drm_i915_private *dev_priv = dev->dev_private;
1789
	struct drm_i915_private *dev_priv = dev->dev_private;
1786
 
1790
 
1787
	BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
1791
	BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
1788
	BUG_ON(!obj->active);
1792
	BUG_ON(!obj->active);
1789
 
1793
 
1790
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1794
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1791
 
1795
 
1792
	list_del_init(&obj->ring_list);
1796
	list_del_init(&obj->ring_list);
1793
	obj->ring = NULL;
1797
	obj->ring = NULL;
1794
 
1798
 
1795
	obj->last_read_seqno = 0;
1799
	obj->last_read_seqno = 0;
1796
	obj->last_write_seqno = 0;
1800
	obj->last_write_seqno = 0;
1797
	obj->base.write_domain = 0;
1801
	obj->base.write_domain = 0;
1798
 
1802
 
1799
	obj->last_fenced_seqno = 0;
1803
	obj->last_fenced_seqno = 0;
1800
	obj->fenced_gpu_access = false;
1804
	obj->fenced_gpu_access = false;
1801
 
1805
 
1802
	obj->active = 0;
1806
	obj->active = 0;
1803
	drm_gem_object_unreference(&obj->base);
1807
	drm_gem_object_unreference(&obj->base);
1804
 
1808
 
1805
	WARN_ON(i915_verify_lists(dev));
1809
	WARN_ON(i915_verify_lists(dev));
1806
}
1810
}
1807
 
1811
 
1808
static int
1812
static int
1809
i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
1813
i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
1810
{
1814
{
1811
	struct drm_i915_private *dev_priv = dev->dev_private;
1815
	struct drm_i915_private *dev_priv = dev->dev_private;
1812
	struct intel_ring_buffer *ring;
1816
	struct intel_ring_buffer *ring;
1813
	int ret, i, j;
1817
	int ret, i, j;
1814
 
1818
 
1815
	/* Carefully retire all requests without writing to the rings */
1819
	/* Carefully retire all requests without writing to the rings */
1816
	for_each_ring(ring, dev_priv, i) {
1820
	for_each_ring(ring, dev_priv, i) {
1817
		ret = intel_ring_idle(ring);
1821
		ret = intel_ring_idle(ring);
1818
	if (ret)
1822
	if (ret)
1819
		return ret;
1823
		return ret;
1820
	}
1824
	}
1821
	i915_gem_retire_requests(dev);
1825
	i915_gem_retire_requests(dev);
1822
 
1826
 
1823
	/* Finally reset hw state */
1827
	/* Finally reset hw state */
1824
	for_each_ring(ring, dev_priv, i) {
1828
	for_each_ring(ring, dev_priv, i) {
1825
		intel_ring_init_seqno(ring, seqno);
1829
		intel_ring_init_seqno(ring, seqno);
1826
 
1830
 
1827
		for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
1831
		for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
1828
			ring->sync_seqno[j] = 0;
1832
			ring->sync_seqno[j] = 0;
1829
	}
1833
	}
1830
 
1834
 
1831
	return 0;
1835
	return 0;
1832
}
1836
}
1833
 
1837
 
1834
int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
1838
int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
1835
{
1839
{
1836
	struct drm_i915_private *dev_priv = dev->dev_private;
1840
	struct drm_i915_private *dev_priv = dev->dev_private;
1837
	int ret;
1841
	int ret;
1838
 
1842
 
1839
	if (seqno == 0)
1843
	if (seqno == 0)
1840
		return -EINVAL;
1844
		return -EINVAL;
1841
 
1845
 
1842
	/* HWS page needs to be set less than what we
1846
	/* HWS page needs to be set less than what we
1843
	 * will inject to ring
1847
	 * will inject to ring
1844
	 */
1848
	 */
1845
	ret = i915_gem_init_seqno(dev, seqno - 1);
1849
	ret = i915_gem_init_seqno(dev, seqno - 1);
1846
	if (ret)
1850
	if (ret)
1847
		return ret;
1851
		return ret;
1848
 
1852
 
1849
	/* Carefully set the last_seqno value so that wrap
1853
	/* Carefully set the last_seqno value so that wrap
1850
	 * detection still works
1854
	 * detection still works
1851
	 */
1855
	 */
1852
	dev_priv->next_seqno = seqno;
1856
	dev_priv->next_seqno = seqno;
1853
	dev_priv->last_seqno = seqno - 1;
1857
	dev_priv->last_seqno = seqno - 1;
1854
	if (dev_priv->last_seqno == 0)
1858
	if (dev_priv->last_seqno == 0)
1855
		dev_priv->last_seqno--;
1859
		dev_priv->last_seqno--;
1856
 
1860
 
1857
	return 0;
1861
	return 0;
1858
}
1862
}
1859
 
1863
 
1860
int
1864
int
1861
i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
1865
i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
1862
{
1866
{
1863
	struct drm_i915_private *dev_priv = dev->dev_private;
1867
	struct drm_i915_private *dev_priv = dev->dev_private;
1864
 
1868
 
1865
	/* reserve 0 for non-seqno */
1869
	/* reserve 0 for non-seqno */
1866
	if (dev_priv->next_seqno == 0) {
1870
	if (dev_priv->next_seqno == 0) {
1867
		int ret = i915_gem_init_seqno(dev, 0);
1871
		int ret = i915_gem_init_seqno(dev, 0);
1868
		if (ret)
1872
		if (ret)
1869
			return ret;
1873
			return ret;
1870
 
1874
 
1871
		dev_priv->next_seqno = 1;
1875
		dev_priv->next_seqno = 1;
1872
	}
1876
	}
1873
 
1877
 
1874
	*seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
1878
	*seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
1875
	return 0;
1879
	return 0;
1876
}
1880
}
1877
 
1881
 
1878
int
1882
int
1879
i915_add_request(struct intel_ring_buffer *ring,
1883
i915_add_request(struct intel_ring_buffer *ring,
1880
		 struct drm_file *file,
1884
		 struct drm_file *file,
1881
		 u32 *out_seqno)
1885
		 u32 *out_seqno)
1882
{
1886
{
1883
	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1887
	drm_i915_private_t *dev_priv = ring->dev->dev_private;
1884
	struct drm_i915_gem_request *request;
1888
	struct drm_i915_gem_request *request;
1885
	u32 request_ring_position;
1889
	u32 request_ring_position;
1886
	int was_empty;
1890
	int was_empty;
1887
	int ret;
1891
	int ret;
1888
 
1892
 
1889
	/*
1893
	/*
1890
	 * Emit any outstanding flushes - execbuf can fail to emit the flush
1894
	 * Emit any outstanding flushes - execbuf can fail to emit the flush
1891
	 * after having emitted the batchbuffer command. Hence we need to fix
1895
	 * after having emitted the batchbuffer command. Hence we need to fix
1892
	 * things up similar to emitting the lazy request. The difference here
1896
	 * things up similar to emitting the lazy request. The difference here
1893
	 * is that the flush _must_ happen before the next request, no matter
1897
	 * is that the flush _must_ happen before the next request, no matter
1894
	 * what.
1898
	 * what.
1895
	 */
1899
	 */
1896
	ret = intel_ring_flush_all_caches(ring);
1900
	ret = intel_ring_flush_all_caches(ring);
1897
	if (ret)
1901
	if (ret)
1898
		return ret;
1902
		return ret;
1899
 
1903
 
1900
	request = kmalloc(sizeof(*request), GFP_KERNEL);
1904
	request = kmalloc(sizeof(*request), GFP_KERNEL);
1901
	if (request == NULL)
1905
	if (request == NULL)
1902
		return -ENOMEM;
1906
		return -ENOMEM;
1903
 
1907
 
1904
 
1908
 
1905
	/* Record the position of the start of the request so that
1909
	/* Record the position of the start of the request so that
1906
	 * should we detect the updated seqno part-way through the
1910
	 * should we detect the updated seqno part-way through the
1907
	 * GPU processing the request, we never over-estimate the
1911
	 * GPU processing the request, we never over-estimate the
1908
	 * position of the head.
1912
	 * position of the head.
1909
	 */
1913
	 */
1910
	request_ring_position = intel_ring_get_tail(ring);
1914
	request_ring_position = intel_ring_get_tail(ring);
1911
 
1915
 
1912
	ret = ring->add_request(ring);
1916
	ret = ring->add_request(ring);
1913
	if (ret) {
1917
	if (ret) {
1914
		kfree(request);
1918
		kfree(request);
1915
	    return ret;
1919
	    return ret;
1916
	}
1920
	}
1917
 
1921
 
1918
	request->seqno = intel_ring_get_seqno(ring);
1922
	request->seqno = intel_ring_get_seqno(ring);
1919
	request->ring = ring;
1923
	request->ring = ring;
1920
	request->tail = request_ring_position;
1924
	request->tail = request_ring_position;
1921
    request->emitted_jiffies = GetTimerTicks();
1925
    request->emitted_jiffies = GetTimerTicks();
1922
	was_empty = list_empty(&ring->request_list);
1926
	was_empty = list_empty(&ring->request_list);
1923
	list_add_tail(&request->list, &ring->request_list);
1927
	list_add_tail(&request->list, &ring->request_list);
1924
	request->file_priv = NULL;
1928
	request->file_priv = NULL;
1925
 
1929
 
1926
	if (file) {
1930
	if (file) {
1927
		struct drm_i915_file_private *file_priv = file->driver_priv;
1931
		struct drm_i915_file_private *file_priv = file->driver_priv;
1928
 
1932
 
1929
		spin_lock(&file_priv->mm.lock);
1933
		spin_lock(&file_priv->mm.lock);
1930
		request->file_priv = file_priv;
1934
		request->file_priv = file_priv;
1931
		list_add_tail(&request->client_list,
1935
		list_add_tail(&request->client_list,
1932
			      &file_priv->mm.request_list);
1936
			      &file_priv->mm.request_list);
1933
		spin_unlock(&file_priv->mm.lock);
1937
		spin_unlock(&file_priv->mm.lock);
1934
	}
1938
	}
1935
 
1939
 
1936
	trace_i915_gem_request_add(ring, request->seqno);
1940
	trace_i915_gem_request_add(ring, request->seqno);
1937
	ring->outstanding_lazy_request = 0;
1941
	ring->outstanding_lazy_request = 0;
1938
 
1942
 
1939
	if (!dev_priv->mm.suspended) {
1943
	if (!dev_priv->mm.suspended) {
1940
		if (i915_enable_hangcheck) {
1944
		if (i915_enable_hangcheck) {
1941
//			mod_timer(&dev_priv->hangcheck_timer,
1945
//			mod_timer(&dev_priv->hangcheck_timer,
1942
//				  jiffies +
1946
//				  jiffies +
1943
//				  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1947
//				  msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1944
		}
1948
		}
1945
		if (was_empty) {
1949
		if (was_empty) {
1946
           queue_delayed_work(dev_priv->wq,
1950
           queue_delayed_work(dev_priv->wq,
1947
					   &dev_priv->mm.retire_work,
1951
					   &dev_priv->mm.retire_work,
1948
					   round_jiffies_up_relative(HZ));
1952
					   round_jiffies_up_relative(HZ));
1949
			intel_mark_busy(dev_priv->dev);
1953
			intel_mark_busy(dev_priv->dev);
1950
		}
1954
		}
1951
	}
1955
	}
1952
 
1956
 
1953
	if (out_seqno)
1957
	if (out_seqno)
1954
		*out_seqno = request->seqno;
1958
		*out_seqno = request->seqno;
1955
	return 0;
1959
	return 0;
1956
}
1960
}
1957
 
1961
 
1958
static inline void
1962
static inline void
1959
i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1963
i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1960
{
1964
{
1961
	struct drm_i915_file_private *file_priv = request->file_priv;
1965
	struct drm_i915_file_private *file_priv = request->file_priv;
1962
 
1966
 
1963
	if (!file_priv)
1967
	if (!file_priv)
1964
		return;
1968
		return;
1965
 
1969
 
1966
	spin_lock(&file_priv->mm.lock);
1970
	spin_lock(&file_priv->mm.lock);
1967
	if (request->file_priv) {
1971
	if (request->file_priv) {
1968
		list_del(&request->client_list);
1972
		list_del(&request->client_list);
1969
		request->file_priv = NULL;
1973
		request->file_priv = NULL;
1970
	}
1974
	}
1971
	spin_unlock(&file_priv->mm.lock);
1975
	spin_unlock(&file_priv->mm.lock);
1972
}
1976
}
1973
 
1977
 
1974
static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1978
static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1975
				      struct intel_ring_buffer *ring)
1979
				      struct intel_ring_buffer *ring)
1976
{
1980
{
1977
	while (!list_empty(&ring->request_list)) {
1981
	while (!list_empty(&ring->request_list)) {
1978
		struct drm_i915_gem_request *request;
1982
		struct drm_i915_gem_request *request;
1979
 
1983
 
1980
		request = list_first_entry(&ring->request_list,
1984
		request = list_first_entry(&ring->request_list,
1981
					   struct drm_i915_gem_request,
1985
					   struct drm_i915_gem_request,
1982
					   list);
1986
					   list);
1983
 
1987
 
1984
		list_del(&request->list);
1988
		list_del(&request->list);
1985
		i915_gem_request_remove_from_client(request);
1989
		i915_gem_request_remove_from_client(request);
1986
		kfree(request);
1990
		kfree(request);
1987
	}
1991
	}
1988
 
1992
 
1989
	while (!list_empty(&ring->active_list)) {
1993
	while (!list_empty(&ring->active_list)) {
1990
		struct drm_i915_gem_object *obj;
1994
		struct drm_i915_gem_object *obj;
1991
 
1995
 
1992
		obj = list_first_entry(&ring->active_list,
1996
		obj = list_first_entry(&ring->active_list,
1993
				       struct drm_i915_gem_object,
1997
				       struct drm_i915_gem_object,
1994
				       ring_list);
1998
				       ring_list);
1995
 
1999
 
1996
		i915_gem_object_move_to_inactive(obj);
2000
		i915_gem_object_move_to_inactive(obj);
1997
	}
2001
	}
1998
}
2002
}
1999
 
2003
 
2000
static void i915_gem_reset_fences(struct drm_device *dev)
2004
void i915_gem_restore_fences(struct drm_device *dev)
2001
{
2005
{
2002
	struct drm_i915_private *dev_priv = dev->dev_private;
2006
	struct drm_i915_private *dev_priv = dev->dev_private;
2003
	int i;
2007
	int i;
2004
 
2008
 
2005
	for (i = 0; i < dev_priv->num_fence_regs; i++) {
2009
	for (i = 0; i < dev_priv->num_fence_regs; i++) {
2006
		struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2010
		struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2007
 
-
 
2008
		i915_gem_write_fence(dev, i, NULL);
2011
		i915_gem_write_fence(dev, i, reg->obj);
2009
 
-
 
2010
		if (reg->obj)
-
 
2011
			i915_gem_object_fence_lost(reg->obj);
-
 
2012
 
-
 
2013
		reg->pin_count = 0;
-
 
2014
		reg->obj = NULL;
-
 
2015
		INIT_LIST_HEAD(®->lru_list);
-
 
2016
	}
2012
	}
2017
 
-
 
2018
	INIT_LIST_HEAD(&dev_priv->mm.fence_list);
-
 
2019
}
2013
}
2020
 
2014
 
2021
void i915_gem_reset(struct drm_device *dev)
2015
void i915_gem_reset(struct drm_device *dev)
2022
{
2016
{
2023
	struct drm_i915_private *dev_priv = dev->dev_private;
2017
	struct drm_i915_private *dev_priv = dev->dev_private;
2024
	struct drm_i915_gem_object *obj;
2018
	struct drm_i915_gem_object *obj;
2025
	struct intel_ring_buffer *ring;
2019
	struct intel_ring_buffer *ring;
2026
	int i;
2020
	int i;
2027
 
2021
 
2028
	for_each_ring(ring, dev_priv, i)
2022
	for_each_ring(ring, dev_priv, i)
2029
		i915_gem_reset_ring_lists(dev_priv, ring);
2023
		i915_gem_reset_ring_lists(dev_priv, ring);
2030
 
2024
 
2031
	/* Move everything out of the GPU domains to ensure we do any
2025
	/* Move everything out of the GPU domains to ensure we do any
2032
	 * necessary invalidation upon reuse.
2026
	 * necessary invalidation upon reuse.
2033
	 */
2027
	 */
2034
	list_for_each_entry(obj,
2028
	list_for_each_entry(obj,
2035
			    &dev_priv->mm.inactive_list,
2029
			    &dev_priv->mm.inactive_list,
2036
			    mm_list)
2030
			    mm_list)
2037
	{
2031
	{
2038
		obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
2032
		obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
2039
	}
2033
	}
2040
 
-
 
2041
	/* The fence registers are invalidated so clear them out */
2034
 
2042
	i915_gem_reset_fences(dev);
2035
	i915_gem_restore_fences(dev);
2043
}
2036
}
2044
 
2037
 
2045
/**
2038
/**
2046
 * This function clears the request list as sequence numbers are passed.
2039
 * This function clears the request list as sequence numbers are passed.
2047
 */
2040
 */
2048
void
2041
void
2049
i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
2042
i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
2050
{
2043
{
2051
	uint32_t seqno;
2044
	uint32_t seqno;
2052
 
2045
 
2053
	if (list_empty(&ring->request_list))
2046
	if (list_empty(&ring->request_list))
2054
		return;
2047
		return;
2055
 
2048
 
2056
	WARN_ON(i915_verify_lists(ring->dev));
2049
	WARN_ON(i915_verify_lists(ring->dev));
2057
 
2050
 
2058
	seqno = ring->get_seqno(ring, true);
2051
	seqno = ring->get_seqno(ring, true);
2059
 
2052
 
2060
	while (!list_empty(&ring->request_list)) {
2053
	while (!list_empty(&ring->request_list)) {
2061
		struct drm_i915_gem_request *request;
2054
		struct drm_i915_gem_request *request;
2062
 
2055
 
2063
		request = list_first_entry(&ring->request_list,
2056
		request = list_first_entry(&ring->request_list,
2064
					   struct drm_i915_gem_request,
2057
					   struct drm_i915_gem_request,
2065
					   list);
2058
					   list);
2066
 
2059
 
2067
		if (!i915_seqno_passed(seqno, request->seqno))
2060
		if (!i915_seqno_passed(seqno, request->seqno))
2068
			break;
2061
			break;
2069
 
2062
 
2070
		trace_i915_gem_request_retire(ring, request->seqno);
2063
		trace_i915_gem_request_retire(ring, request->seqno);
2071
		/* We know the GPU must have read the request to have
2064
		/* We know the GPU must have read the request to have
2072
		 * sent us the seqno + interrupt, so use the position
2065
		 * sent us the seqno + interrupt, so use the position
2073
		 * of tail of the request to update the last known position
2066
		 * of tail of the request to update the last known position
2074
		 * of the GPU head.
2067
		 * of the GPU head.
2075
		 */
2068
		 */
2076
		ring->last_retired_head = request->tail;
2069
		ring->last_retired_head = request->tail;
2077
 
2070
 
2078
		list_del(&request->list);
2071
		list_del(&request->list);
2079
		i915_gem_request_remove_from_client(request);
2072
		i915_gem_request_remove_from_client(request);
2080
		kfree(request);
2073
		kfree(request);
2081
	}
2074
	}
2082
 
2075
 
2083
	/* Move any buffers on the active list that are no longer referenced
2076
	/* Move any buffers on the active list that are no longer referenced
2084
	 * by the ringbuffer to the flushing/inactive lists as appropriate.
2077
	 * by the ringbuffer to the flushing/inactive lists as appropriate.
2085
	 */
2078
	 */
2086
	while (!list_empty(&ring->active_list)) {
2079
	while (!list_empty(&ring->active_list)) {
2087
		struct drm_i915_gem_object *obj;
2080
		struct drm_i915_gem_object *obj;
2088
 
2081
 
2089
		obj = list_first_entry(&ring->active_list,
2082
		obj = list_first_entry(&ring->active_list,
2090
				      struct drm_i915_gem_object,
2083
				      struct drm_i915_gem_object,
2091
				      ring_list);
2084
				      ring_list);
2092
 
2085
 
2093
		if (!i915_seqno_passed(seqno, obj->last_read_seqno))
2086
		if (!i915_seqno_passed(seqno, obj->last_read_seqno))
2094
			break;
2087
			break;
2095
 
2088
 
2096
			i915_gem_object_move_to_inactive(obj);
2089
			i915_gem_object_move_to_inactive(obj);
2097
	}
2090
	}
2098
 
2091
 
2099
	if (unlikely(ring->trace_irq_seqno &&
2092
	if (unlikely(ring->trace_irq_seqno &&
2100
		     i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
2093
		     i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
2101
		ring->irq_put(ring);
2094
		ring->irq_put(ring);
2102
		ring->trace_irq_seqno = 0;
2095
		ring->trace_irq_seqno = 0;
2103
	}
2096
	}
2104
 
2097
 
2105
	WARN_ON(i915_verify_lists(ring->dev));
2098
	WARN_ON(i915_verify_lists(ring->dev));
2106
}
2099
}
2107
 
2100
 
2108
void
2101
void
2109
i915_gem_retire_requests(struct drm_device *dev)
2102
i915_gem_retire_requests(struct drm_device *dev)
2110
{
2103
{
2111
	drm_i915_private_t *dev_priv = dev->dev_private;
2104
	drm_i915_private_t *dev_priv = dev->dev_private;
2112
	struct intel_ring_buffer *ring;
2105
	struct intel_ring_buffer *ring;
2113
	int i;
2106
	int i;
2114
 
2107
 
2115
	for_each_ring(ring, dev_priv, i)
2108
	for_each_ring(ring, dev_priv, i)
2116
		i915_gem_retire_requests_ring(ring);
2109
		i915_gem_retire_requests_ring(ring);
2117
}
2110
}
2118
 
2111
 
2119
static void
2112
static void
2120
i915_gem_retire_work_handler(struct work_struct *work)
2113
i915_gem_retire_work_handler(struct work_struct *work)
2121
{
2114
{
2122
	drm_i915_private_t *dev_priv;
2115
	drm_i915_private_t *dev_priv;
2123
	struct drm_device *dev;
2116
	struct drm_device *dev;
2124
	struct intel_ring_buffer *ring;
2117
	struct intel_ring_buffer *ring;
2125
	bool idle;
2118
	bool idle;
2126
	int i;
2119
	int i;
2127
 
2120
 
2128
	dev_priv = container_of(work, drm_i915_private_t,
2121
	dev_priv = container_of(work, drm_i915_private_t,
2129
				mm.retire_work.work);
2122
				mm.retire_work.work);
2130
	dev = dev_priv->dev;
2123
	dev = dev_priv->dev;
2131
 
2124
 
2132
	/* Come back later if the device is busy... */
2125
	/* Come back later if the device is busy... */
2133
	if (!mutex_trylock(&dev->struct_mutex)) {
2126
	if (!mutex_trylock(&dev->struct_mutex)) {
2134
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2127
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2135
				   round_jiffies_up_relative(HZ));
2128
				   round_jiffies_up_relative(HZ));
2136
        return;
2129
        return;
2137
	}
2130
	}
2138
 
2131
 
2139
	i915_gem_retire_requests(dev);
2132
	i915_gem_retire_requests(dev);
2140
 
2133
 
2141
	/* Send a periodic flush down the ring so we don't hold onto GEM
2134
	/* Send a periodic flush down the ring so we don't hold onto GEM
2142
	 * objects indefinitely.
2135
	 * objects indefinitely.
2143
	 */
2136
	 */
2144
	idle = true;
2137
	idle = true;
2145
	for_each_ring(ring, dev_priv, i) {
2138
	for_each_ring(ring, dev_priv, i) {
2146
		if (ring->gpu_caches_dirty)
2139
		if (ring->gpu_caches_dirty)
2147
			i915_add_request(ring, NULL, NULL);
2140
			i915_add_request(ring, NULL, NULL);
2148
 
2141
 
2149
		idle &= list_empty(&ring->request_list);
2142
		idle &= list_empty(&ring->request_list);
2150
	}
2143
	}
2151
 
2144
 
2152
   if (!dev_priv->mm.suspended && !idle)
2145
   if (!dev_priv->mm.suspended && !idle)
2153
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2146
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2154
				   round_jiffies_up_relative(HZ));
2147
				   round_jiffies_up_relative(HZ));
2155
	if (idle)
2148
	if (idle)
2156
		intel_mark_idle(dev);
2149
		intel_mark_idle(dev);
2157
 
2150
 
2158
	mutex_unlock(&dev->struct_mutex);
2151
	mutex_unlock(&dev->struct_mutex);
2159
}
2152
}
2160
 
2153
 
2161
/**
2154
/**
2162
 * Ensures that an object will eventually get non-busy by flushing any required
2155
 * Ensures that an object will eventually get non-busy by flushing any required
2163
 * write domains, emitting any outstanding lazy request and retiring and
2156
 * write domains, emitting any outstanding lazy request and retiring and
2164
 * completed requests.
2157
 * completed requests.
2165
 */
2158
 */
2166
static int
2159
static int
2167
i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2160
i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2168
{
2161
{
2169
	int ret;
2162
	int ret;
2170
 
2163
 
2171
	if (obj->active) {
2164
	if (obj->active) {
2172
		ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
2165
		ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
2173
		if (ret)
2166
		if (ret)
2174
			return ret;
2167
			return ret;
2175
 
2168
 
2176
		i915_gem_retire_requests_ring(obj->ring);
2169
		i915_gem_retire_requests_ring(obj->ring);
2177
	}
2170
	}
2178
 
2171
 
2179
	return 0;
2172
	return 0;
2180
}
2173
}
2181
 
2174
 
2182
/**
2175
/**
2183
 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2176
 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2184
 * @DRM_IOCTL_ARGS: standard ioctl arguments
2177
 * @DRM_IOCTL_ARGS: standard ioctl arguments
2185
 *
2178
 *
2186
 * Returns 0 if successful, else an error is returned with the remaining time in
2179
 * Returns 0 if successful, else an error is returned with the remaining time in
2187
 * the timeout parameter.
2180
 * the timeout parameter.
2188
 *  -ETIME: object is still busy after timeout
2181
 *  -ETIME: object is still busy after timeout
2189
 *  -ERESTARTSYS: signal interrupted the wait
2182
 *  -ERESTARTSYS: signal interrupted the wait
2190
 *  -ENONENT: object doesn't exist
2183
 *  -ENONENT: object doesn't exist
2191
 * Also possible, but rare:
2184
 * Also possible, but rare:
2192
 *  -EAGAIN: GPU wedged
2185
 *  -EAGAIN: GPU wedged
2193
 *  -ENOMEM: damn
2186
 *  -ENOMEM: damn
2194
 *  -ENODEV: Internal IRQ fail
2187
 *  -ENODEV: Internal IRQ fail
2195
 *  -E?: The add request failed
2188
 *  -E?: The add request failed
2196
 *
2189
 *
2197
 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2190
 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2198
 * non-zero timeout parameter the wait ioctl will wait for the given number of
2191
 * non-zero timeout parameter the wait ioctl will wait for the given number of
2199
 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2192
 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2200
 * without holding struct_mutex the object may become re-busied before this
2193
 * without holding struct_mutex the object may become re-busied before this
2201
 * function completes. A similar but shorter * race condition exists in the busy
2194
 * function completes. A similar but shorter * race condition exists in the busy
2202
 * ioctl
2195
 * ioctl
2203
 */
2196
 */
2204
 
2197
 
2205
 
2198
 
2206
 
2199
 
2207
 
2200
 
2208
 
2201
 
2209
 
2202
 
2210
 
2203
 
2211
 
2204
 
2212
 
2205
 
2213
 
2206
 
2214
 
2207
 
2215
 
2208
 
2216
/**
2209
/**
2217
 * i915_gem_object_sync - sync an object to a ring.
2210
 * i915_gem_object_sync - sync an object to a ring.
2218
 *
2211
 *
2219
 * @obj: object which may be in use on another ring.
2212
 * @obj: object which may be in use on another ring.
2220
 * @to: ring we wish to use the object on. May be NULL.
2213
 * @to: ring we wish to use the object on. May be NULL.
2221
 *
2214
 *
2222
 * This code is meant to abstract object synchronization with the GPU.
2215
 * This code is meant to abstract object synchronization with the GPU.
2223
 * Calling with NULL implies synchronizing the object with the CPU
2216
 * Calling with NULL implies synchronizing the object with the CPU
2224
 * rather than a particular GPU ring.
2217
 * rather than a particular GPU ring.
2225
 *
2218
 *
2226
 * Returns 0 if successful, else propagates up the lower layer error.
2219
 * Returns 0 if successful, else propagates up the lower layer error.
2227
 */
2220
 */
2228
int
2221
int
2229
i915_gem_object_sync(struct drm_i915_gem_object *obj,
2222
i915_gem_object_sync(struct drm_i915_gem_object *obj,
2230
		     struct intel_ring_buffer *to)
2223
		     struct intel_ring_buffer *to)
2231
{
2224
{
2232
	struct intel_ring_buffer *from = obj->ring;
2225
	struct intel_ring_buffer *from = obj->ring;
2233
	u32 seqno;
2226
	u32 seqno;
2234
	int ret, idx;
2227
	int ret, idx;
2235
 
2228
 
2236
	if (from == NULL || to == from)
2229
	if (from == NULL || to == from)
2237
		return 0;
2230
		return 0;
2238
 
2231
 
2239
	if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
2232
	if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
2240
		return i915_gem_object_wait_rendering(obj, false);
2233
		return i915_gem_object_wait_rendering(obj, false);
2241
 
2234
 
2242
	idx = intel_ring_sync_index(from, to);
2235
	idx = intel_ring_sync_index(from, to);
2243
 
2236
 
2244
	seqno = obj->last_read_seqno;
2237
	seqno = obj->last_read_seqno;
2245
	if (seqno <= from->sync_seqno[idx])
2238
	if (seqno <= from->sync_seqno[idx])
2246
		return 0;
2239
		return 0;
2247
 
2240
 
2248
	ret = i915_gem_check_olr(obj->ring, seqno);
2241
	ret = i915_gem_check_olr(obj->ring, seqno);
2249
	if (ret)
2242
	if (ret)
2250
		return ret;
2243
		return ret;
2251
 
2244
 
2252
	ret = to->sync_to(to, from, seqno);
2245
	ret = to->sync_to(to, from, seqno);
2253
	if (!ret)
2246
	if (!ret)
2254
		/* We use last_read_seqno because sync_to()
2247
		/* We use last_read_seqno because sync_to()
2255
		 * might have just caused seqno wrap under
2248
		 * might have just caused seqno wrap under
2256
		 * the radar.
2249
		 * the radar.
2257
		 */
2250
		 */
2258
		from->sync_seqno[idx] = obj->last_read_seqno;
2251
		from->sync_seqno[idx] = obj->last_read_seqno;
2259
 
2252
 
2260
	return ret;
2253
	return ret;
2261
}
2254
}
2262
 
2255
 
2263
static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2256
static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2264
{
2257
{
2265
	u32 old_write_domain, old_read_domains;
2258
	u32 old_write_domain, old_read_domains;
2266
 
2259
 
2267
	/* Force a pagefault for domain tracking on next user access */
2260
	/* Force a pagefault for domain tracking on next user access */
2268
//	i915_gem_release_mmap(obj);
2261
//	i915_gem_release_mmap(obj);
2269
 
2262
 
2270
	if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2263
	if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2271
		return;
2264
		return;
2272
 
2265
 
2273
	/* Wait for any direct GTT access to complete */
2266
	/* Wait for any direct GTT access to complete */
2274
	mb();
2267
	mb();
2275
 
2268
 
2276
	old_read_domains = obj->base.read_domains;
2269
	old_read_domains = obj->base.read_domains;
2277
	old_write_domain = obj->base.write_domain;
2270
	old_write_domain = obj->base.write_domain;
2278
 
2271
 
2279
	obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2272
	obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2280
	obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2273
	obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2281
 
2274
 
2282
	trace_i915_gem_object_change_domain(obj,
2275
	trace_i915_gem_object_change_domain(obj,
2283
					    old_read_domains,
2276
					    old_read_domains,
2284
					    old_write_domain);
2277
					    old_write_domain);
2285
}
2278
}
2286
 
2279
 
2287
/**
2280
/**
2288
 * Unbinds an object from the GTT aperture.
2281
 * Unbinds an object from the GTT aperture.
2289
 */
2282
 */
2290
int
2283
int
2291
i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2284
i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2292
{
2285
{
2293
	drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2286
	drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2294
	int ret;
2287
	int ret;
2295
 
2288
 
2296
    if(obj == get_fb_obj())
2289
    if(obj == get_fb_obj())
2297
        return 0;
2290
        return 0;
2298
 
2291
 
2299
	if (obj->gtt_space == NULL)
2292
	if (obj->gtt_space == NULL)
2300
		return 0;
2293
		return 0;
2301
 
2294
 
2302
	if (obj->pin_count)
2295
	if (obj->pin_count)
2303
		return -EBUSY;
2296
		return -EBUSY;
2304
 
2297
 
2305
	BUG_ON(obj->pages == NULL);
2298
	BUG_ON(obj->pages == NULL);
2306
 
2299
 
2307
	ret = i915_gem_object_finish_gpu(obj);
2300
	ret = i915_gem_object_finish_gpu(obj);
2308
	if (ret)
2301
	if (ret)
2309
		return ret;
2302
		return ret;
2310
	/* Continue on if we fail due to EIO, the GPU is hung so we
2303
	/* Continue on if we fail due to EIO, the GPU is hung so we
2311
	 * should be safe and we need to cleanup or else we might
2304
	 * should be safe and we need to cleanup or else we might
2312
	 * cause memory corruption through use-after-free.
2305
	 * cause memory corruption through use-after-free.
2313
	 */
2306
	 */
2314
 
2307
 
2315
	i915_gem_object_finish_gtt(obj);
2308
	i915_gem_object_finish_gtt(obj);
2316
 
2309
 
2317
	/* release the fence reg _after_ flushing */
2310
	/* release the fence reg _after_ flushing */
2318
	ret = i915_gem_object_put_fence(obj);
2311
	ret = i915_gem_object_put_fence(obj);
2319
	if (ret)
2312
	if (ret)
2320
		return ret;
2313
		return ret;
2321
 
2314
 
2322
	trace_i915_gem_object_unbind(obj);
2315
	trace_i915_gem_object_unbind(obj);
2323
 
2316
 
2324
	if (obj->has_global_gtt_mapping)
2317
	if (obj->has_global_gtt_mapping)
2325
        i915_gem_gtt_unbind_object(obj);
2318
        i915_gem_gtt_unbind_object(obj);
2326
	if (obj->has_aliasing_ppgtt_mapping) {
2319
	if (obj->has_aliasing_ppgtt_mapping) {
2327
		i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2320
		i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2328
		obj->has_aliasing_ppgtt_mapping = 0;
2321
		obj->has_aliasing_ppgtt_mapping = 0;
2329
	}
2322
	}
2330
	i915_gem_gtt_finish_object(obj);
2323
	i915_gem_gtt_finish_object(obj);
2331
 
2324
 
2332
	list_del(&obj->mm_list);
2325
	list_del(&obj->mm_list);
2333
	list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
2326
	list_move_tail(&obj->gtt_list, &dev_priv->mm.unbound_list);
2334
	/* Avoid an unnecessary call to unbind on rebind. */
2327
	/* Avoid an unnecessary call to unbind on rebind. */
2335
	obj->map_and_fenceable = true;
2328
	obj->map_and_fenceable = true;
2336
 
2329
 
2337
	drm_mm_put_block(obj->gtt_space);
2330
	drm_mm_put_block(obj->gtt_space);
2338
	obj->gtt_space = NULL;
2331
	obj->gtt_space = NULL;
2339
	obj->gtt_offset = 0;
2332
	obj->gtt_offset = 0;
2340
 
2333
 
2341
	return 0;
2334
	return 0;
2342
}
2335
}
2343
 
2336
 
2344
int i915_gpu_idle(struct drm_device *dev)
2337
int i915_gpu_idle(struct drm_device *dev)
2345
{
2338
{
2346
	drm_i915_private_t *dev_priv = dev->dev_private;
2339
	drm_i915_private_t *dev_priv = dev->dev_private;
2347
	struct intel_ring_buffer *ring;
2340
	struct intel_ring_buffer *ring;
2348
	int ret, i;
2341
	int ret, i;
2349
 
2342
 
2350
	/* Flush everything onto the inactive list. */
2343
	/* Flush everything onto the inactive list. */
2351
	for_each_ring(ring, dev_priv, i) {
2344
	for_each_ring(ring, dev_priv, i) {
2352
		ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2345
		ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2353
		if (ret)
2346
		if (ret)
2354
			return ret;
2347
			return ret;
2355
 
2348
 
2356
		ret = intel_ring_idle(ring);
2349
		ret = intel_ring_idle(ring);
2357
		if (ret)
2350
		if (ret)
2358
			return ret;
2351
			return ret;
2359
	}
2352
	}
2360
 
2353
 
2361
	return 0;
2354
	return 0;
2362
}
2355
}
2363
 
2356
 
2364
static void i965_write_fence_reg(struct drm_device *dev, int reg,
2357
static void i965_write_fence_reg(struct drm_device *dev, int reg,
2365
					struct drm_i915_gem_object *obj)
2358
					struct drm_i915_gem_object *obj)
2366
{
2359
{
2367
	drm_i915_private_t *dev_priv = dev->dev_private;
2360
	drm_i915_private_t *dev_priv = dev->dev_private;
2368
	int fence_reg;
2361
	int fence_reg;
2369
	int fence_pitch_shift;
2362
	int fence_pitch_shift;
2370
	uint64_t val;
2363
	uint64_t val;
2371
 
2364
 
2372
	if (INTEL_INFO(dev)->gen >= 6) {
2365
	if (INTEL_INFO(dev)->gen >= 6) {
2373
		fence_reg = FENCE_REG_SANDYBRIDGE_0;
2366
		fence_reg = FENCE_REG_SANDYBRIDGE_0;
2374
		fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
2367
		fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
2375
	} else {
2368
	} else {
2376
		fence_reg = FENCE_REG_965_0;
2369
		fence_reg = FENCE_REG_965_0;
2377
		fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
2370
		fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
2378
	}
2371
	}
2379
 
2372
 
2380
	if (obj) {
2373
	if (obj) {
2381
		u32 size = obj->gtt_space->size;
2374
		u32 size = obj->gtt_space->size;
2382
 
2375
 
2383
		val = (uint64_t)((obj->gtt_offset + size - 4096) &
2376
		val = (uint64_t)((obj->gtt_offset + size - 4096) &
2384
				 0xfffff000) << 32;
2377
				 0xfffff000) << 32;
2385
		val |= obj->gtt_offset & 0xfffff000;
2378
		val |= obj->gtt_offset & 0xfffff000;
2386
		val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
2379
		val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
2387
		if (obj->tiling_mode == I915_TILING_Y)
2380
		if (obj->tiling_mode == I915_TILING_Y)
2388
			val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2381
			val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2389
		val |= I965_FENCE_REG_VALID;
2382
		val |= I965_FENCE_REG_VALID;
2390
	} else
2383
	} else
2391
		val = 0;
2384
		val = 0;
2392
 
2385
 
2393
	fence_reg += reg * 8;
2386
	fence_reg += reg * 8;
2394
	I915_WRITE64(fence_reg, val);
2387
	I915_WRITE64(fence_reg, val);
2395
	POSTING_READ(fence_reg);
2388
	POSTING_READ(fence_reg);
2396
}
2389
}
2397
 
2390
 
2398
static void i915_write_fence_reg(struct drm_device *dev, int reg,
2391
static void i915_write_fence_reg(struct drm_device *dev, int reg,
2399
				 struct drm_i915_gem_object *obj)
2392
				 struct drm_i915_gem_object *obj)
2400
{
2393
{
2401
	drm_i915_private_t *dev_priv = dev->dev_private;
2394
	drm_i915_private_t *dev_priv = dev->dev_private;
2402
	u32 val;
2395
	u32 val;
2403
 
2396
 
2404
	if (obj) {
2397
	if (obj) {
2405
		u32 size = obj->gtt_space->size;
2398
		u32 size = obj->gtt_space->size;
2406
		int pitch_val;
2399
		int pitch_val;
2407
		int tile_width;
2400
		int tile_width;
2408
 
2401
 
2409
		WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2402
		WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2410
		     (size & -size) != size ||
2403
		     (size & -size) != size ||
2411
		     (obj->gtt_offset & (size - 1)),
2404
		     (obj->gtt_offset & (size - 1)),
2412
		     "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2405
		     "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2413
		     obj->gtt_offset, obj->map_and_fenceable, size);
2406
		     obj->gtt_offset, obj->map_and_fenceable, size);
2414
 
2407
 
2415
		if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2408
		if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2416
			tile_width = 128;
2409
			tile_width = 128;
2417
		else
2410
		else
2418
			tile_width = 512;
2411
			tile_width = 512;
2419
 
2412
 
2420
		/* Note: pitch better be a power of two tile widths */
2413
		/* Note: pitch better be a power of two tile widths */
2421
		pitch_val = obj->stride / tile_width;
2414
		pitch_val = obj->stride / tile_width;
2422
		pitch_val = ffs(pitch_val) - 1;
2415
		pitch_val = ffs(pitch_val) - 1;
2423
 
2416
 
2424
		val = obj->gtt_offset;
2417
		val = obj->gtt_offset;
2425
		if (obj->tiling_mode == I915_TILING_Y)
2418
		if (obj->tiling_mode == I915_TILING_Y)
2426
			val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2419
			val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2427
		val |= I915_FENCE_SIZE_BITS(size);
2420
		val |= I915_FENCE_SIZE_BITS(size);
2428
		val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2421
		val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2429
		val |= I830_FENCE_REG_VALID;
2422
		val |= I830_FENCE_REG_VALID;
2430
	} else
2423
	} else
2431
		val = 0;
2424
		val = 0;
2432
 
2425
 
2433
	if (reg < 8)
2426
	if (reg < 8)
2434
		reg = FENCE_REG_830_0 + reg * 4;
2427
		reg = FENCE_REG_830_0 + reg * 4;
2435
	else
2428
	else
2436
		reg = FENCE_REG_945_8 + (reg - 8) * 4;
2429
		reg = FENCE_REG_945_8 + (reg - 8) * 4;
2437
 
2430
 
2438
	I915_WRITE(reg, val);
2431
	I915_WRITE(reg, val);
2439
	POSTING_READ(reg);
2432
	POSTING_READ(reg);
2440
}
2433
}
2441
 
2434
 
2442
static void i830_write_fence_reg(struct drm_device *dev, int reg,
2435
static void i830_write_fence_reg(struct drm_device *dev, int reg,
2443
				struct drm_i915_gem_object *obj)
2436
				struct drm_i915_gem_object *obj)
2444
{
2437
{
2445
	drm_i915_private_t *dev_priv = dev->dev_private;
2438
	drm_i915_private_t *dev_priv = dev->dev_private;
2446
	uint32_t val;
2439
	uint32_t val;
2447
 
2440
 
2448
	if (obj) {
2441
	if (obj) {
2449
		u32 size = obj->gtt_space->size;
2442
		u32 size = obj->gtt_space->size;
2450
		uint32_t pitch_val;
2443
		uint32_t pitch_val;
2451
 
2444
 
2452
		WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2445
		WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2453
		     (size & -size) != size ||
2446
		     (size & -size) != size ||
2454
		     (obj->gtt_offset & (size - 1)),
2447
		     (obj->gtt_offset & (size - 1)),
2455
		     "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2448
		     "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2456
		     obj->gtt_offset, size);
2449
		     obj->gtt_offset, size);
2457
 
2450
 
2458
		pitch_val = obj->stride / 128;
2451
		pitch_val = obj->stride / 128;
2459
		pitch_val = ffs(pitch_val) - 1;
2452
		pitch_val = ffs(pitch_val) - 1;
2460
 
2453
 
2461
		val = obj->gtt_offset;
2454
		val = obj->gtt_offset;
2462
		if (obj->tiling_mode == I915_TILING_Y)
2455
		if (obj->tiling_mode == I915_TILING_Y)
2463
			val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2456
			val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2464
		val |= I830_FENCE_SIZE_BITS(size);
2457
		val |= I830_FENCE_SIZE_BITS(size);
2465
		val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2458
		val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2466
		val |= I830_FENCE_REG_VALID;
2459
		val |= I830_FENCE_REG_VALID;
2467
	} else
2460
	} else
2468
		val = 0;
2461
		val = 0;
2469
 
2462
 
2470
	I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2463
	I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2471
	POSTING_READ(FENCE_REG_830_0 + reg * 4);
2464
	POSTING_READ(FENCE_REG_830_0 + reg * 4);
2472
}
2465
}
2473
 
2466
 
2474
inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
2467
inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
2475
{
2468
{
2476
	return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
2469
	return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
2477
}
2470
}
2478
 
2471
 
2479
static void i915_gem_write_fence(struct drm_device *dev, int reg,
2472
static void i915_gem_write_fence(struct drm_device *dev, int reg,
2480
				 struct drm_i915_gem_object *obj)
2473
				 struct drm_i915_gem_object *obj)
2481
{
2474
{
2482
	struct drm_i915_private *dev_priv = dev->dev_private;
2475
	struct drm_i915_private *dev_priv = dev->dev_private;
2483
 
2476
 
2484
	/* Ensure that all CPU reads are completed before installing a fence
2477
	/* Ensure that all CPU reads are completed before installing a fence
2485
	 * and all writes before removing the fence.
2478
	 * and all writes before removing the fence.
2486
	 */
2479
	 */
2487
	if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
2480
	if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
2488
		mb();
2481
		mb();
2489
 
2482
 
2490
	switch (INTEL_INFO(dev)->gen) {
2483
	switch (INTEL_INFO(dev)->gen) {
2491
	case 7:
2484
	case 7:
2492
	case 6:
2485
	case 6:
2493
	case 5:
2486
	case 5:
2494
	case 4: i965_write_fence_reg(dev, reg, obj); break;
2487
	case 4: i965_write_fence_reg(dev, reg, obj); break;
2495
	case 3: i915_write_fence_reg(dev, reg, obj); break;
2488
	case 3: i915_write_fence_reg(dev, reg, obj); break;
2496
	case 2: i830_write_fence_reg(dev, reg, obj); break;
2489
	case 2: i830_write_fence_reg(dev, reg, obj); break;
2497
	default: BUG();
2490
	default: BUG();
2498
	}
2491
	}
2499
 
2492
 
2500
	/* And similarly be paranoid that no direct access to this region
2493
	/* And similarly be paranoid that no direct access to this region
2501
	 * is reordered to before the fence is installed.
2494
	 * is reordered to before the fence is installed.
2502
	 */
2495
	 */
2503
	if (i915_gem_object_needs_mb(obj))
2496
	if (i915_gem_object_needs_mb(obj))
2504
		mb();
2497
		mb();
2505
}
2498
}
2506
 
2499
 
2507
static inline int fence_number(struct drm_i915_private *dev_priv,
2500
static inline int fence_number(struct drm_i915_private *dev_priv,
2508
			       struct drm_i915_fence_reg *fence)
2501
			       struct drm_i915_fence_reg *fence)
2509
{
2502
{
2510
	return fence - dev_priv->fence_regs;
2503
	return fence - dev_priv->fence_regs;
2511
}
2504
}
-
 
2505
 
-
 
2506
static void i915_gem_write_fence__ipi(void *data)
-
 
2507
{
-
 
2508
    asm volatile("wbinvd");
-
 
2509
 
-
 
2510
}
2512
 
2511
 
2513
static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2512
static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2514
					 struct drm_i915_fence_reg *fence,
2513
					 struct drm_i915_fence_reg *fence,
2515
					 bool enable)
2514
					 bool enable)
2516
{
2515
{
-
 
2516
	struct drm_device *dev = obj->base.dev;
2517
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2517
	struct drm_i915_private *dev_priv = dev->dev_private;
2518
	int reg = fence_number(dev_priv, fence);
2518
	int fence_reg = fence_number(dev_priv, fence);
-
 
2519
 
-
 
2520
	/* In order to fully serialize access to the fenced region and
-
 
2521
	 * the update to the fence register we need to take extreme
-
 
2522
	 * measures on SNB+. In theory, the write to the fence register
-
 
2523
	 * flushes all memory transactions before, and coupled with the
-
 
2524
	 * mb() placed around the register write we serialise all memory
-
 
2525
	 * operations with respect to the changes in the tiler. Yet, on
-
 
2526
	 * SNB+ we need to take a step further and emit an explicit wbinvd()
-
 
2527
	 * on each processor in order to manually flush all memory
-
 
2528
	 * transactions before updating the fence register.
-
 
2529
	 */
-
 
2530
	if (HAS_LLC(obj->base.dev))
2519
 
2531
		on_each_cpu(i915_gem_write_fence__ipi, NULL, 1);
2520
	i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2532
	i915_gem_write_fence(dev, fence_reg, enable ? obj : NULL);
2521
 
2533
 
2522
	if (enable) {
2534
	if (enable) {
2523
		obj->fence_reg = reg;
2535
		obj->fence_reg = fence_reg;
2524
		fence->obj = obj;
2536
		fence->obj = obj;
2525
		list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2537
		list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2526
	} else {
2538
	} else {
2527
		obj->fence_reg = I915_FENCE_REG_NONE;
2539
		obj->fence_reg = I915_FENCE_REG_NONE;
2528
		fence->obj = NULL;
2540
		fence->obj = NULL;
2529
		list_del_init(&fence->lru_list);
2541
		list_del_init(&fence->lru_list);
2530
	}
2542
	}
2531
}
2543
}
2532
 
2544
 
2533
static int
2545
static int
2534
i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
2546
i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
2535
{
2547
{
2536
	if (obj->last_fenced_seqno) {
2548
	if (obj->last_fenced_seqno) {
2537
		int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
2549
		int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
2538
			if (ret)
2550
			if (ret)
2539
				return ret;
2551
				return ret;
2540
 
2552
 
2541
		obj->last_fenced_seqno = 0;
2553
		obj->last_fenced_seqno = 0;
2542
	}
2554
	}
2543
 
2555
 
2544
	obj->fenced_gpu_access = false;
2556
	obj->fenced_gpu_access = false;
2545
	return 0;
2557
	return 0;
2546
}
2558
}
2547
 
2559
 
2548
int
2560
int
2549
i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2561
i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2550
{
2562
{
2551
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2563
	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
-
 
2564
	struct drm_i915_fence_reg *fence;
2552
	int ret;
2565
	int ret;
2553
 
2566
 
2554
	ret = i915_gem_object_wait_fence(obj);
2567
	ret = i915_gem_object_wait_fence(obj);
2555
	if (ret)
2568
	if (ret)
2556
		return ret;
2569
		return ret;
2557
 
2570
 
2558
	if (obj->fence_reg == I915_FENCE_REG_NONE)
2571
	if (obj->fence_reg == I915_FENCE_REG_NONE)
2559
		return 0;
2572
		return 0;
2560
 
-
 
2561
	i915_gem_object_update_fence(obj,
2573
 
2562
				     &dev_priv->fence_regs[obj->fence_reg],
-
 
-
 
2574
	fence = &dev_priv->fence_regs[obj->fence_reg];
2563
				     false);
2575
 
-
 
2576
	i915_gem_object_fence_lost(obj);
2564
	i915_gem_object_fence_lost(obj);
2577
	i915_gem_object_update_fence(obj, fence, false);
2565
 
2578
 
2566
	return 0;
2579
	return 0;
2567
}
2580
}
2568
 
2581
 
2569
static struct drm_i915_fence_reg *
2582
static struct drm_i915_fence_reg *
2570
i915_find_fence_reg(struct drm_device *dev)
2583
i915_find_fence_reg(struct drm_device *dev)
2571
{
2584
{
2572
	struct drm_i915_private *dev_priv = dev->dev_private;
2585
	struct drm_i915_private *dev_priv = dev->dev_private;
2573
	struct drm_i915_fence_reg *reg, *avail;
2586
	struct drm_i915_fence_reg *reg, *avail;
2574
	int i;
2587
	int i;
2575
 
2588
 
2576
	/* First try to find a free reg */
2589
	/* First try to find a free reg */
2577
	avail = NULL;
2590
	avail = NULL;
2578
	for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2591
	for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2579
		reg = &dev_priv->fence_regs[i];
2592
		reg = &dev_priv->fence_regs[i];
2580
		if (!reg->obj)
2593
		if (!reg->obj)
2581
			return reg;
2594
			return reg;
2582
 
2595
 
2583
		if (!reg->pin_count)
2596
		if (!reg->pin_count)
2584
			avail = reg;
2597
			avail = reg;
2585
	}
2598
	}
2586
 
2599
 
2587
	if (avail == NULL)
2600
	if (avail == NULL)
2588
		return NULL;
2601
		return NULL;
2589
 
2602
 
2590
	/* None available, try to steal one or wait for a user to finish */
2603
	/* None available, try to steal one or wait for a user to finish */
2591
	list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2604
	list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2592
		if (reg->pin_count)
2605
		if (reg->pin_count)
2593
			continue;
2606
			continue;
2594
 
2607
 
2595
		return reg;
2608
		return reg;
2596
	}
2609
	}
2597
 
2610
 
2598
	return NULL;
2611
	return NULL;
2599
}
2612
}
2600
 
2613
 
2601
/**
2614
/**
2602
 * i915_gem_object_get_fence - set up fencing for an object
2615
 * i915_gem_object_get_fence - set up fencing for an object
2603
 * @obj: object to map through a fence reg
2616
 * @obj: object to map through a fence reg
2604
 *
2617
 *
2605
 * When mapping objects through the GTT, userspace wants to be able to write
2618
 * When mapping objects through the GTT, userspace wants to be able to write
2606
 * to them without having to worry about swizzling if the object is tiled.
2619
 * to them without having to worry about swizzling if the object is tiled.
2607
 * This function walks the fence regs looking for a free one for @obj,
2620
 * This function walks the fence regs looking for a free one for @obj,
2608
 * stealing one if it can't find any.
2621
 * stealing one if it can't find any.
2609
 *
2622
 *
2610
 * It then sets up the reg based on the object's properties: address, pitch
2623
 * It then sets up the reg based on the object's properties: address, pitch
2611
 * and tiling format.
2624
 * and tiling format.
2612
 *
2625
 *
2613
 * For an untiled surface, this removes any existing fence.
2626
 * For an untiled surface, this removes any existing fence.
2614
 */
2627
 */
2615
int
2628
int
2616
i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
2629
i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
2617
{
2630
{
2618
	struct drm_device *dev = obj->base.dev;
2631
	struct drm_device *dev = obj->base.dev;
2619
	struct drm_i915_private *dev_priv = dev->dev_private;
2632
	struct drm_i915_private *dev_priv = dev->dev_private;
2620
	bool enable = obj->tiling_mode != I915_TILING_NONE;
2633
	bool enable = obj->tiling_mode != I915_TILING_NONE;
2621
	struct drm_i915_fence_reg *reg;
2634
	struct drm_i915_fence_reg *reg;
2622
	int ret;
2635
	int ret;
2623
 
2636
 
2624
	/* Have we updated the tiling parameters upon the object and so
2637
	/* Have we updated the tiling parameters upon the object and so
2625
	 * will need to serialise the write to the associated fence register?
2638
	 * will need to serialise the write to the associated fence register?
2626
	 */
2639
	 */
2627
	if (obj->fence_dirty) {
2640
	if (obj->fence_dirty) {
2628
		ret = i915_gem_object_wait_fence(obj);
2641
		ret = i915_gem_object_wait_fence(obj);
2629
		if (ret)
2642
		if (ret)
2630
			return ret;
2643
			return ret;
2631
	}
2644
	}
2632
 
2645
 
2633
	/* Just update our place in the LRU if our fence is getting reused. */
2646
	/* Just update our place in the LRU if our fence is getting reused. */
2634
	if (obj->fence_reg != I915_FENCE_REG_NONE) {
2647
	if (obj->fence_reg != I915_FENCE_REG_NONE) {
2635
		reg = &dev_priv->fence_regs[obj->fence_reg];
2648
		reg = &dev_priv->fence_regs[obj->fence_reg];
2636
		if (!obj->fence_dirty) {
2649
		if (!obj->fence_dirty) {
2637
			list_move_tail(®->lru_list,
2650
			list_move_tail(®->lru_list,
2638
				       &dev_priv->mm.fence_list);
2651
				       &dev_priv->mm.fence_list);
2639
			return 0;
2652
			return 0;
2640
		}
2653
		}
2641
	} else if (enable) {
2654
	} else if (enable) {
2642
		reg = i915_find_fence_reg(dev);
2655
		reg = i915_find_fence_reg(dev);
2643
		if (reg == NULL)
2656
		if (reg == NULL)
2644
			return -EDEADLK;
2657
			return -EDEADLK;
2645
 
2658
 
2646
		if (reg->obj) {
2659
		if (reg->obj) {
2647
			struct drm_i915_gem_object *old = reg->obj;
2660
			struct drm_i915_gem_object *old = reg->obj;
2648
 
2661
 
2649
			ret = i915_gem_object_wait_fence(old);
2662
			ret = i915_gem_object_wait_fence(old);
2650
			if (ret)
2663
			if (ret)
2651
				return ret;
2664
				return ret;
2652
 
2665
 
2653
			i915_gem_object_fence_lost(old);
2666
			i915_gem_object_fence_lost(old);
2654
		}
2667
		}
2655
	} else
2668
	} else
2656
		return 0;
2669
		return 0;
2657
 
2670
 
2658
	i915_gem_object_update_fence(obj, reg, enable);
2671
	i915_gem_object_update_fence(obj, reg, enable);
2659
	obj->fence_dirty = false;
2672
	obj->fence_dirty = false;
2660
 
2673
 
2661
	return 0;
2674
	return 0;
2662
}
2675
}
2663
 
2676
 
2664
static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2677
static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2665
				     struct drm_mm_node *gtt_space,
2678
				     struct drm_mm_node *gtt_space,
2666
				     unsigned long cache_level)
2679
				     unsigned long cache_level)
2667
{
2680
{
2668
	struct drm_mm_node *other;
2681
	struct drm_mm_node *other;
2669
 
2682
 
2670
	/* On non-LLC machines we have to be careful when putting differing
2683
	/* On non-LLC machines we have to be careful when putting differing
2671
	 * types of snoopable memory together to avoid the prefetcher
2684
	 * types of snoopable memory together to avoid the prefetcher
2672
	 * crossing memory domains and dying.
2685
	 * crossing memory domains and dying.
2673
	 */
2686
	 */
2674
	if (HAS_LLC(dev))
2687
	if (HAS_LLC(dev))
2675
		return true;
2688
		return true;
2676
 
2689
 
2677
	if (gtt_space == NULL)
2690
	if (gtt_space == NULL)
2678
		return true;
2691
		return true;
2679
 
2692
 
2680
	if (list_empty(>t_space->node_list))
2693
	if (list_empty(>t_space->node_list))
2681
		return true;
2694
		return true;
2682
 
2695
 
2683
	other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2696
	other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2684
	if (other->allocated && !other->hole_follows && other->color != cache_level)
2697
	if (other->allocated && !other->hole_follows && other->color != cache_level)
2685
		return false;
2698
		return false;
2686
 
2699
 
2687
	other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2700
	other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
2688
	if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2701
	if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
2689
		return false;
2702
		return false;
2690
 
2703
 
2691
	return true;
2704
	return true;
2692
}
2705
}
2693
 
2706
 
2694
static void i915_gem_verify_gtt(struct drm_device *dev)
2707
static void i915_gem_verify_gtt(struct drm_device *dev)
2695
{
2708
{
2696
#if WATCH_GTT
2709
#if WATCH_GTT
2697
	struct drm_i915_private *dev_priv = dev->dev_private;
2710
	struct drm_i915_private *dev_priv = dev->dev_private;
2698
	struct drm_i915_gem_object *obj;
2711
	struct drm_i915_gem_object *obj;
2699
	int err = 0;
2712
	int err = 0;
2700
 
2713
 
2701
	list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2714
	list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
2702
		if (obj->gtt_space == NULL) {
2715
		if (obj->gtt_space == NULL) {
2703
			printk(KERN_ERR "object found on GTT list with no space reserved\n");
2716
			printk(KERN_ERR "object found on GTT list with no space reserved\n");
2704
			err++;
2717
			err++;
2705
			continue;
2718
			continue;
2706
		}
2719
		}
2707
 
2720
 
2708
		if (obj->cache_level != obj->gtt_space->color) {
2721
		if (obj->cache_level != obj->gtt_space->color) {
2709
			printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2722
			printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
2710
			       obj->gtt_space->start,
2723
			       obj->gtt_space->start,
2711
			       obj->gtt_space->start + obj->gtt_space->size,
2724
			       obj->gtt_space->start + obj->gtt_space->size,
2712
			       obj->cache_level,
2725
			       obj->cache_level,
2713
			       obj->gtt_space->color);
2726
			       obj->gtt_space->color);
2714
			err++;
2727
			err++;
2715
			continue;
2728
			continue;
2716
		}
2729
		}
2717
 
2730
 
2718
		if (!i915_gem_valid_gtt_space(dev,
2731
		if (!i915_gem_valid_gtt_space(dev,
2719
					      obj->gtt_space,
2732
					      obj->gtt_space,
2720
					      obj->cache_level)) {
2733
					      obj->cache_level)) {
2721
			printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2734
			printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
2722
			       obj->gtt_space->start,
2735
			       obj->gtt_space->start,
2723
			       obj->gtt_space->start + obj->gtt_space->size,
2736
			       obj->gtt_space->start + obj->gtt_space->size,
2724
			       obj->cache_level);
2737
			       obj->cache_level);
2725
			err++;
2738
			err++;
2726
			continue;
2739
			continue;
2727
		}
2740
		}
2728
	}
2741
	}
2729
 
2742
 
2730
	WARN_ON(err);
2743
	WARN_ON(err);
2731
#endif
2744
#endif
2732
}
2745
}
2733
 
2746
 
2734
/**
2747
/**
2735
 * Finds free space in the GTT aperture and binds the object there.
2748
 * Finds free space in the GTT aperture and binds the object there.
2736
 */
2749
 */
2737
static int
2750
static int
2738
i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2751
i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2739
			    unsigned alignment,
2752
			    unsigned alignment,
2740
			    bool map_and_fenceable,
2753
			    bool map_and_fenceable,
2741
			    bool nonblocking)
2754
			    bool nonblocking)
2742
{
2755
{
2743
	struct drm_device *dev = obj->base.dev;
2756
	struct drm_device *dev = obj->base.dev;
2744
	drm_i915_private_t *dev_priv = dev->dev_private;
2757
	drm_i915_private_t *dev_priv = dev->dev_private;
2745
	struct drm_mm_node *node;
2758
	struct drm_mm_node *node;
2746
	u32 size, fence_size, fence_alignment, unfenced_alignment;
2759
	u32 size, fence_size, fence_alignment, unfenced_alignment;
2747
	bool mappable, fenceable;
2760
	bool mappable, fenceable;
2748
	int ret;
2761
	int ret;
2749
 
2762
 
2750
	fence_size = i915_gem_get_gtt_size(dev,
2763
	fence_size = i915_gem_get_gtt_size(dev,
2751
					   obj->base.size,
2764
					   obj->base.size,
2752
					   obj->tiling_mode);
2765
					   obj->tiling_mode);
2753
	fence_alignment = i915_gem_get_gtt_alignment(dev,
2766
	fence_alignment = i915_gem_get_gtt_alignment(dev,
2754
						     obj->base.size,
2767
						     obj->base.size,
2755
						     obj->tiling_mode, true);
2768
						     obj->tiling_mode, true);
2756
	unfenced_alignment =
2769
	unfenced_alignment =
2757
		i915_gem_get_gtt_alignment(dev,
2770
		i915_gem_get_gtt_alignment(dev,
2758
						    obj->base.size,
2771
						    obj->base.size,
2759
						    obj->tiling_mode, false);
2772
						    obj->tiling_mode, false);
2760
 
2773
 
2761
	if (alignment == 0)
2774
	if (alignment == 0)
2762
		alignment = map_and_fenceable ? fence_alignment :
2775
		alignment = map_and_fenceable ? fence_alignment :
2763
						unfenced_alignment;
2776
						unfenced_alignment;
2764
	if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2777
	if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2765
		DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2778
		DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2766
		return -EINVAL;
2779
		return -EINVAL;
2767
	}
2780
	}
2768
 
2781
 
2769
	size = map_and_fenceable ? fence_size : obj->base.size;
2782
	size = map_and_fenceable ? fence_size : obj->base.size;
2770
 
2783
 
2771
	/* If the object is bigger than the entire aperture, reject it early
2784
	/* If the object is bigger than the entire aperture, reject it early
2772
	 * before evicting everything in a vain attempt to find space.
2785
	 * before evicting everything in a vain attempt to find space.
2773
	 */
2786
	 */
2774
	if (obj->base.size >
2787
	if (obj->base.size >
2775
	    (map_and_fenceable ? dev_priv->gtt.mappable_end : dev_priv->gtt.total)) {
2788
	    (map_and_fenceable ? dev_priv->gtt.mappable_end : dev_priv->gtt.total)) {
2776
		DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2789
		DRM_ERROR("Attempting to bind an object larger than the aperture\n");
-
 
2790
        FAIL();
2777
		return -E2BIG;
2791
		return -E2BIG;
2778
	}
2792
	}
2779
 
2793
 
2780
	ret = i915_gem_object_get_pages(obj);
2794
	ret = i915_gem_object_get_pages(obj);
2781
	if (ret)
2795
	if (ret)
2782
		return ret;
2796
		return ret;
2783
 
2797
 
2784
	i915_gem_object_pin_pages(obj);
2798
	i915_gem_object_pin_pages(obj);
2785
 
2799
 
2786
	node = kzalloc(sizeof(*node), GFP_KERNEL);
2800
	node = kzalloc(sizeof(*node), GFP_KERNEL);
2787
	if (node == NULL) {
2801
	if (node == NULL) {
2788
		i915_gem_object_unpin_pages(obj);
2802
		i915_gem_object_unpin_pages(obj);
2789
		return -ENOMEM;
2803
		return -ENOMEM;
2790
	}
2804
	}
2791
 
2805
 
2792
 search_free:
2806
 search_free:
2793
	if (map_and_fenceable)
2807
	if (map_and_fenceable)
2794
		ret = drm_mm_insert_node_in_range_generic(&dev_priv->mm.gtt_space, node,
2808
		ret = drm_mm_insert_node_in_range_generic(&dev_priv->mm.gtt_space, node,
2795
							  size, alignment, obj->cache_level,
2809
							  size, alignment, obj->cache_level,
2796
							  0, dev_priv->gtt.mappable_end);
2810
							  0, dev_priv->gtt.mappable_end);
2797
	else
2811
	else
2798
		ret = drm_mm_insert_node_generic(&dev_priv->mm.gtt_space, node,
2812
		ret = drm_mm_insert_node_generic(&dev_priv->mm.gtt_space, node,
2799
						 size, alignment, obj->cache_level);
2813
						 size, alignment, obj->cache_level);
2800
	if (ret) {
2814
	if (ret) {
2801
 
2815
 
2802
		i915_gem_object_unpin_pages(obj);
2816
		i915_gem_object_unpin_pages(obj);
2803
		kfree(node);
2817
		kfree(node);
2804
			return ret;
2818
			return ret;
2805
	}
2819
	}
2806
	if (WARN_ON(!i915_gem_valid_gtt_space(dev, node, obj->cache_level))) {
2820
	if (WARN_ON(!i915_gem_valid_gtt_space(dev, node, obj->cache_level))) {
2807
		i915_gem_object_unpin_pages(obj);
2821
		i915_gem_object_unpin_pages(obj);
2808
		drm_mm_put_block(node);
2822
		drm_mm_put_block(node);
2809
		return -EINVAL;
2823
		return -EINVAL;
2810
	}
2824
	}
2811
 
2825
 
2812
	ret = i915_gem_gtt_prepare_object(obj);
2826
	ret = i915_gem_gtt_prepare_object(obj);
2813
	if (ret) {
2827
	if (ret) {
2814
		i915_gem_object_unpin_pages(obj);
2828
		i915_gem_object_unpin_pages(obj);
2815
		drm_mm_put_block(node);
2829
		drm_mm_put_block(node);
2816
			return ret;
2830
			return ret;
2817
	}
2831
	}
2818
 
2832
 
2819
	list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
2833
	list_move_tail(&obj->gtt_list, &dev_priv->mm.bound_list);
2820
	list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2834
	list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2821
 
2835
 
2822
	obj->gtt_space = node;
2836
	obj->gtt_space = node;
2823
	obj->gtt_offset = node->start;
2837
	obj->gtt_offset = node->start;
2824
 
2838
 
2825
	fenceable =
2839
	fenceable =
2826
		node->size == fence_size &&
2840
		node->size == fence_size &&
2827
		(node->start & (fence_alignment - 1)) == 0;
2841
		(node->start & (fence_alignment - 1)) == 0;
2828
 
2842
 
2829
	mappable =
2843
	mappable =
2830
		obj->gtt_offset + obj->base.size <= dev_priv->gtt.mappable_end;
2844
		obj->gtt_offset + obj->base.size <= dev_priv->gtt.mappable_end;
2831
 
2845
 
2832
	obj->map_and_fenceable = mappable && fenceable;
2846
	obj->map_and_fenceable = mappable && fenceable;
2833
 
2847
 
2834
	i915_gem_object_unpin_pages(obj);
2848
	i915_gem_object_unpin_pages(obj);
2835
	trace_i915_gem_object_bind(obj, map_and_fenceable);
2849
	trace_i915_gem_object_bind(obj, map_and_fenceable);
2836
	i915_gem_verify_gtt(dev);
2850
	i915_gem_verify_gtt(dev);
2837
	return 0;
2851
	return 0;
2838
}
2852
}
2839
 
2853
 
2840
void
2854
void
2841
i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2855
i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2842
{
2856
{
2843
	/* If we don't have a page list set up, then we're not pinned
2857
	/* If we don't have a page list set up, then we're not pinned
2844
	 * to GPU, and we can ignore the cache flush because it'll happen
2858
	 * to GPU, and we can ignore the cache flush because it'll happen
2845
	 * again at bind time.
2859
	 * again at bind time.
2846
	 */
2860
	 */
2847
	if (obj->pages == NULL)
2861
	if (obj->pages == NULL)
2848
		return;
2862
		return;
2849
 
2863
 
2850
	/*
2864
	/*
2851
	 * Stolen memory is always coherent with the GPU as it is explicitly
2865
	 * Stolen memory is always coherent with the GPU as it is explicitly
2852
	 * marked as wc by the system, or the system is cache-coherent.
2866
	 * marked as wc by the system, or the system is cache-coherent.
2853
	 */
2867
	 */
2854
	if (obj->stolen)
2868
	if (obj->stolen)
2855
		return;
2869
		return;
2856
 
2870
 
2857
	/* If the GPU is snooping the contents of the CPU cache,
2871
	/* If the GPU is snooping the contents of the CPU cache,
2858
	 * we do not need to manually clear the CPU cache lines.  However,
2872
	 * we do not need to manually clear the CPU cache lines.  However,
2859
	 * the caches are only snooped when the render cache is
2873
	 * the caches are only snooped when the render cache is
2860
	 * flushed/invalidated.  As we always have to emit invalidations
2874
	 * flushed/invalidated.  As we always have to emit invalidations
2861
	 * and flushes when moving into and out of the RENDER domain, correct
2875
	 * and flushes when moving into and out of the RENDER domain, correct
2862
	 * snooping behaviour occurs naturally as the result of our domain
2876
	 * snooping behaviour occurs naturally as the result of our domain
2863
	 * tracking.
2877
	 * tracking.
2864
	 */
2878
	 */
2865
	if (obj->cache_level != I915_CACHE_NONE)
2879
	if (obj->cache_level != I915_CACHE_NONE)
2866
		return;
2880
		return;
2867
#if 0
2881
#if 0
2868
     if(obj->mapped != NULL)
2882
     if(obj->mapped != NULL)
2869
     {
2883
     {
2870
        uint8_t *page_virtual;
2884
        uint8_t *page_virtual;
2871
        unsigned int i;
2885
        unsigned int i;
2872
 
2886
 
2873
        page_virtual = obj->mapped;
2887
        page_virtual = obj->mapped;
2874
        asm volatile("mfence");
2888
        asm volatile("mfence");
2875
        for (i = 0; i < obj->base.size; i += x86_clflush_size)
2889
        for (i = 0; i < obj->base.size; i += x86_clflush_size)
2876
            clflush(page_virtual + i);
2890
            clflush(page_virtual + i);
2877
        asm volatile("mfence");
2891
        asm volatile("mfence");
2878
     }
2892
     }
2879
     else
2893
     else
2880
     {
2894
     {
2881
        uint8_t *page_virtual;
2895
        uint8_t *page_virtual;
2882
        unsigned int i;
2896
        unsigned int i;
2883
        page_virtual = AllocKernelSpace(obj->base.size);
2897
        page_virtual = AllocKernelSpace(obj->base.size);
2884
        if(page_virtual != NULL)
2898
        if(page_virtual != NULL)
2885
        {
2899
        {
2886
            dma_addr_t *src, *dst;
2900
            dma_addr_t *src, *dst;
2887
            u32 count;
2901
            u32 count;
2888
 
2902
 
2889
#define page_tabs  0xFDC00000      /* really dirty hack */
2903
#define page_tabs  0xFDC00000      /* really dirty hack */
2890
 
2904
 
2891
            src =  obj->pages.page;
2905
            src =  obj->pages.page;
2892
            dst =  &((dma_addr_t*)page_tabs)[(u32_t)page_virtual >> 12];
2906
            dst =  &((dma_addr_t*)page_tabs)[(u32_t)page_virtual >> 12];
2893
            count = obj->base.size/4096;
2907
            count = obj->base.size/4096;
2894
 
2908
 
2895
            while(count--)
2909
            while(count--)
2896
            {
2910
            {
2897
                *dst++ = (0xFFFFF000 & *src++) | 0x001 ;
2911
                *dst++ = (0xFFFFF000 & *src++) | 0x001 ;
2898
            };
2912
            };
2899
 
2913
 
2900
            asm volatile("mfence");
2914
            asm volatile("mfence");
2901
            for (i = 0; i < obj->base.size; i += x86_clflush_size)
2915
            for (i = 0; i < obj->base.size; i += x86_clflush_size)
2902
                clflush(page_virtual + i);
2916
                clflush(page_virtual + i);
2903
            asm volatile("mfence");
2917
            asm volatile("mfence");
2904
            FreeKernelSpace(page_virtual);
2918
            FreeKernelSpace(page_virtual);
2905
        }
2919
        }
2906
        else
2920
        else
2907
        {
2921
        {
2908
            asm volatile (
2922
            asm volatile (
2909
            "mfence         \n"
2923
            "mfence         \n"
2910
            "wbinvd         \n"                 /* this is really ugly  */
2924
            "wbinvd         \n"                 /* this is really ugly  */
2911
            "mfence");
2925
            "mfence");
2912
        }
2926
        }
2913
     }
2927
     }
2914
#endif
2928
#endif
2915
 
2929
 
2916
}
2930
}
2917
 
2931
 
2918
/** Flushes the GTT write domain for the object if it's dirty. */
2932
/** Flushes the GTT write domain for the object if it's dirty. */
2919
static void
2933
static void
2920
i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2934
i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2921
{
2935
{
2922
	uint32_t old_write_domain;
2936
	uint32_t old_write_domain;
2923
 
2937
 
2924
	if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2938
	if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2925
		return;
2939
		return;
2926
 
2940
 
2927
	/* No actual flushing is required for the GTT write domain.  Writes
2941
	/* No actual flushing is required for the GTT write domain.  Writes
2928
	 * to it immediately go to main memory as far as we know, so there's
2942
	 * to it immediately go to main memory as far as we know, so there's
2929
	 * no chipset flush.  It also doesn't land in render cache.
2943
	 * no chipset flush.  It also doesn't land in render cache.
2930
	 *
2944
	 *
2931
	 * However, we do have to enforce the order so that all writes through
2945
	 * However, we do have to enforce the order so that all writes through
2932
	 * the GTT land before any writes to the device, such as updates to
2946
	 * the GTT land before any writes to the device, such as updates to
2933
	 * the GATT itself.
2947
	 * the GATT itself.
2934
	 */
2948
	 */
2935
	wmb();
2949
	wmb();
2936
 
2950
 
2937
	old_write_domain = obj->base.write_domain;
2951
	old_write_domain = obj->base.write_domain;
2938
	obj->base.write_domain = 0;
2952
	obj->base.write_domain = 0;
2939
 
2953
 
2940
	trace_i915_gem_object_change_domain(obj,
2954
	trace_i915_gem_object_change_domain(obj,
2941
					    obj->base.read_domains,
2955
					    obj->base.read_domains,
2942
					    old_write_domain);
2956
					    old_write_domain);
2943
}
2957
}
2944
 
2958
 
2945
/** Flushes the CPU write domain for the object if it's dirty. */
2959
/** Flushes the CPU write domain for the object if it's dirty. */
2946
static void
2960
static void
2947
i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2961
i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2948
{
2962
{
2949
	uint32_t old_write_domain;
2963
	uint32_t old_write_domain;
2950
 
2964
 
2951
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2965
	if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2952
		return;
2966
		return;
2953
 
2967
 
2954
	i915_gem_clflush_object(obj);
2968
	i915_gem_clflush_object(obj);
2955
	i915_gem_chipset_flush(obj->base.dev);
2969
	i915_gem_chipset_flush(obj->base.dev);
2956
	old_write_domain = obj->base.write_domain;
2970
	old_write_domain = obj->base.write_domain;
2957
	obj->base.write_domain = 0;
2971
	obj->base.write_domain = 0;
2958
 
2972
 
2959
	trace_i915_gem_object_change_domain(obj,
2973
	trace_i915_gem_object_change_domain(obj,
2960
					    obj->base.read_domains,
2974
					    obj->base.read_domains,
2961
					    old_write_domain);
2975
					    old_write_domain);
2962
}
2976
}
2963
 
2977
 
2964
/**
2978
/**
2965
 * Moves a single object to the GTT read, and possibly write domain.
2979
 * Moves a single object to the GTT read, and possibly write domain.
2966
 *
2980
 *
2967
 * This function returns when the move is complete, including waiting on
2981
 * This function returns when the move is complete, including waiting on
2968
 * flushes to occur.
2982
 * flushes to occur.
2969
 */
2983
 */
2970
int
2984
int
2971
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2985
i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2972
{
2986
{
2973
	drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2987
	drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2974
	uint32_t old_write_domain, old_read_domains;
2988
	uint32_t old_write_domain, old_read_domains;
2975
	int ret;
2989
	int ret;
2976
 
2990
 
2977
	/* Not valid to be called on unbound objects. */
2991
	/* Not valid to be called on unbound objects. */
2978
	if (obj->gtt_space == NULL)
2992
	if (obj->gtt_space == NULL)
2979
		return -EINVAL;
2993
		return -EINVAL;
2980
 
2994
 
2981
	if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2995
	if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2982
		return 0;
2996
		return 0;
2983
 
2997
 
2984
	ret = i915_gem_object_wait_rendering(obj, !write);
2998
	ret = i915_gem_object_wait_rendering(obj, !write);
2985
		if (ret)
2999
		if (ret)
2986
			return ret;
3000
			return ret;
2987
 
3001
 
2988
	i915_gem_object_flush_cpu_write_domain(obj);
3002
	i915_gem_object_flush_cpu_write_domain(obj);
2989
 
3003
 
2990
	/* Serialise direct access to this object with the barriers for
3004
	/* Serialise direct access to this object with the barriers for
2991
	 * coherent writes from the GPU, by effectively invalidating the
3005
	 * coherent writes from the GPU, by effectively invalidating the
2992
	 * GTT domain upon first access.
3006
	 * GTT domain upon first access.
2993
	 */
3007
	 */
2994
	if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3008
	if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2995
		mb();
3009
		mb();
2996
 
3010
 
2997
	old_write_domain = obj->base.write_domain;
3011
	old_write_domain = obj->base.write_domain;
2998
	old_read_domains = obj->base.read_domains;
3012
	old_read_domains = obj->base.read_domains;
2999
 
3013
 
3000
	/* It should now be out of any other write domains, and we can update
3014
	/* It should now be out of any other write domains, and we can update
3001
	 * the domain values for our changes.
3015
	 * the domain values for our changes.
3002
	 */
3016
	 */
3003
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3017
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3004
	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3018
	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3005
	if (write) {
3019
	if (write) {
3006
		obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3020
		obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3007
		obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3021
		obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3008
		obj->dirty = 1;
3022
		obj->dirty = 1;
3009
	}
3023
	}
3010
 
3024
 
3011
	trace_i915_gem_object_change_domain(obj,
3025
	trace_i915_gem_object_change_domain(obj,
3012
					    old_read_domains,
3026
					    old_read_domains,
3013
					    old_write_domain);
3027
					    old_write_domain);
3014
 
3028
 
3015
	/* And bump the LRU for this access */
3029
	/* And bump the LRU for this access */
3016
	if (i915_gem_object_is_inactive(obj))
3030
	if (i915_gem_object_is_inactive(obj))
3017
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3031
		list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
3018
 
3032
 
3019
	return 0;
3033
	return 0;
3020
}
3034
}
3021
 
3035
 
3022
int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3036
int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3023
				    enum i915_cache_level cache_level)
3037
				    enum i915_cache_level cache_level)
3024
{
3038
{
3025
	struct drm_device *dev = obj->base.dev;
3039
	struct drm_device *dev = obj->base.dev;
3026
	drm_i915_private_t *dev_priv = dev->dev_private;
3040
	drm_i915_private_t *dev_priv = dev->dev_private;
3027
	int ret;
3041
	int ret;
3028
 
3042
 
3029
	if (obj->cache_level == cache_level)
3043
	if (obj->cache_level == cache_level)
3030
		return 0;
3044
		return 0;
3031
 
3045
 
3032
	if (obj->pin_count) {
3046
	if (obj->pin_count) {
3033
		DRM_DEBUG("can not change the cache level of pinned objects\n");
3047
		DRM_DEBUG("can not change the cache level of pinned objects\n");
3034
		return -EBUSY;
3048
		return -EBUSY;
3035
	}
3049
	}
3036
 
3050
 
3037
	if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
3051
	if (!i915_gem_valid_gtt_space(dev, obj->gtt_space, cache_level)) {
3038
		ret = i915_gem_object_unbind(obj);
3052
		ret = i915_gem_object_unbind(obj);
3039
		if (ret)
3053
		if (ret)
3040
			return ret;
3054
			return ret;
3041
	}
3055
	}
3042
 
3056
 
3043
	if (obj->gtt_space) {
3057
	if (obj->gtt_space) {
3044
		ret = i915_gem_object_finish_gpu(obj);
3058
		ret = i915_gem_object_finish_gpu(obj);
3045
		if (ret)
3059
		if (ret)
3046
			return ret;
3060
			return ret;
3047
 
3061
 
3048
		i915_gem_object_finish_gtt(obj);
3062
		i915_gem_object_finish_gtt(obj);
3049
 
3063
 
3050
		/* Before SandyBridge, you could not use tiling or fence
3064
		/* Before SandyBridge, you could not use tiling or fence
3051
		 * registers with snooped memory, so relinquish any fences
3065
		 * registers with snooped memory, so relinquish any fences
3052
		 * currently pointing to our region in the aperture.
3066
		 * currently pointing to our region in the aperture.
3053
		 */
3067
		 */
3054
		if (INTEL_INFO(dev)->gen < 6) {
3068
		if (INTEL_INFO(dev)->gen < 6) {
3055
			ret = i915_gem_object_put_fence(obj);
3069
			ret = i915_gem_object_put_fence(obj);
3056
			if (ret)
3070
			if (ret)
3057
				return ret;
3071
				return ret;
3058
		}
3072
		}
3059
 
3073
 
3060
		if (obj->has_global_gtt_mapping)
3074
		if (obj->has_global_gtt_mapping)
3061
			i915_gem_gtt_bind_object(obj, cache_level);
3075
			i915_gem_gtt_bind_object(obj, cache_level);
3062
		if (obj->has_aliasing_ppgtt_mapping)
3076
		if (obj->has_aliasing_ppgtt_mapping)
3063
			i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3077
			i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3064
					       obj, cache_level);
3078
					       obj, cache_level);
3065
 
3079
 
3066
		obj->gtt_space->color = cache_level;
3080
		obj->gtt_space->color = cache_level;
3067
	}
3081
	}
3068
 
3082
 
3069
	if (cache_level == I915_CACHE_NONE) {
3083
	if (cache_level == I915_CACHE_NONE) {
3070
		u32 old_read_domains, old_write_domain;
3084
		u32 old_read_domains, old_write_domain;
3071
 
3085
 
3072
		/* If we're coming from LLC cached, then we haven't
3086
		/* If we're coming from LLC cached, then we haven't
3073
		 * actually been tracking whether the data is in the
3087
		 * actually been tracking whether the data is in the
3074
		 * CPU cache or not, since we only allow one bit set
3088
		 * CPU cache or not, since we only allow one bit set
3075
		 * in obj->write_domain and have been skipping the clflushes.
3089
		 * in obj->write_domain and have been skipping the clflushes.
3076
		 * Just set it to the CPU cache for now.
3090
		 * Just set it to the CPU cache for now.
3077
		 */
3091
		 */
3078
		WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3092
		WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3079
		WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3093
		WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3080
 
3094
 
3081
		old_read_domains = obj->base.read_domains;
3095
		old_read_domains = obj->base.read_domains;
3082
		old_write_domain = obj->base.write_domain;
3096
		old_write_domain = obj->base.write_domain;
3083
 
3097
 
3084
		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3098
		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3085
		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3099
		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3086
 
3100
 
3087
		trace_i915_gem_object_change_domain(obj,
3101
		trace_i915_gem_object_change_domain(obj,
3088
						    old_read_domains,
3102
						    old_read_domains,
3089
						    old_write_domain);
3103
						    old_write_domain);
3090
    }
3104
    }
3091
 
3105
 
3092
	obj->cache_level = cache_level;
3106
	obj->cache_level = cache_level;
3093
	i915_gem_verify_gtt(dev);
3107
	i915_gem_verify_gtt(dev);
3094
	return 0;
3108
	return 0;
3095
}
3109
}
3096
 
3110
 
3097
int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3111
int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3098
			       struct drm_file *file)
3112
			       struct drm_file *file)
3099
{
3113
{
3100
	struct drm_i915_gem_caching *args = data;
3114
	struct drm_i915_gem_caching *args = data;
3101
	struct drm_i915_gem_object *obj;
3115
	struct drm_i915_gem_object *obj;
3102
	int ret;
3116
	int ret;
3103
 
3117
 
3104
     if(args->handle == -2)
3118
     if(args->handle == -2)
3105
     {
3119
     {
3106
        printf("%s handle %d\n", __FUNCTION__, args->handle);
3120
        printf("%s handle %d\n", __FUNCTION__, args->handle);
3107
        return 0;
3121
        return 0;
3108
     }
3122
     }
3109
 
3123
 
3110
	ret = i915_mutex_lock_interruptible(dev);
3124
	ret = i915_mutex_lock_interruptible(dev);
3111
	if (ret)
3125
	if (ret)
3112
		return ret;
3126
		return ret;
3113
 
3127
 
3114
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3128
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3115
	if (&obj->base == NULL) {
3129
	if (&obj->base == NULL) {
3116
		ret = -ENOENT;
3130
		ret = -ENOENT;
3117
		goto unlock;
3131
		goto unlock;
3118
	}
3132
	}
3119
 
3133
 
3120
	args->caching = obj->cache_level != I915_CACHE_NONE;
3134
	args->caching = obj->cache_level != I915_CACHE_NONE;
3121
 
3135
 
3122
	drm_gem_object_unreference(&obj->base);
3136
	drm_gem_object_unreference(&obj->base);
3123
unlock:
3137
unlock:
3124
	mutex_unlock(&dev->struct_mutex);
3138
	mutex_unlock(&dev->struct_mutex);
3125
	return ret;
3139
	return ret;
3126
}
3140
}
3127
 
3141
 
3128
int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3142
int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3129
			       struct drm_file *file)
3143
			       struct drm_file *file)
3130
{
3144
{
3131
	struct drm_i915_gem_caching *args = data;
3145
	struct drm_i915_gem_caching *args = data;
3132
	struct drm_i915_gem_object *obj;
3146
	struct drm_i915_gem_object *obj;
3133
	enum i915_cache_level level;
3147
	enum i915_cache_level level;
3134
	int ret;
3148
	int ret;
3135
 
3149
 
3136
     if(args->handle == -2)
3150
     if(args->handle == -2)
3137
     {
3151
     {
3138
        printf("%s handle %d\n", __FUNCTION__, args->handle);
3152
        printf("%s handle %d\n", __FUNCTION__, args->handle);
3139
        return 0;
3153
        return 0;
3140
     }
3154
     }
3141
 
3155
 
3142
	switch (args->caching) {
3156
	switch (args->caching) {
3143
	case I915_CACHING_NONE:
3157
	case I915_CACHING_NONE:
3144
		level = I915_CACHE_NONE;
3158
		level = I915_CACHE_NONE;
3145
		break;
3159
		break;
3146
	case I915_CACHING_CACHED:
3160
	case I915_CACHING_CACHED:
3147
		level = I915_CACHE_LLC;
3161
		level = I915_CACHE_LLC;
3148
		break;
3162
		break;
3149
	default:
3163
	default:
3150
		return -EINVAL;
3164
		return -EINVAL;
3151
	}
3165
	}
3152
 
3166
 
3153
	ret = i915_mutex_lock_interruptible(dev);
3167
	ret = i915_mutex_lock_interruptible(dev);
3154
	if (ret)
3168
	if (ret)
3155
		return ret;
3169
		return ret;
3156
 
3170
 
3157
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3171
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3158
	if (&obj->base == NULL) {
3172
	if (&obj->base == NULL) {
3159
		ret = -ENOENT;
3173
		ret = -ENOENT;
3160
		goto unlock;
3174
		goto unlock;
3161
	}
3175
	}
3162
 
3176
 
3163
	ret = i915_gem_object_set_cache_level(obj, level);
3177
	ret = i915_gem_object_set_cache_level(obj, level);
3164
 
3178
 
3165
	drm_gem_object_unreference(&obj->base);
3179
	drm_gem_object_unreference(&obj->base);
3166
unlock:
3180
unlock:
3167
	mutex_unlock(&dev->struct_mutex);
3181
	mutex_unlock(&dev->struct_mutex);
3168
	return ret;
3182
	return ret;
3169
}
3183
}
3170
 
3184
 
3171
/*
3185
/*
3172
 * Prepare buffer for display plane (scanout, cursors, etc).
3186
 * Prepare buffer for display plane (scanout, cursors, etc).
3173
 * Can be called from an uninterruptible phase (modesetting) and allows
3187
 * Can be called from an uninterruptible phase (modesetting) and allows
3174
 * any flushes to be pipelined (for pageflips).
3188
 * any flushes to be pipelined (for pageflips).
3175
 */
3189
 */
3176
int
3190
int
3177
i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3191
i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3178
				     u32 alignment,
3192
				     u32 alignment,
3179
				     struct intel_ring_buffer *pipelined)
3193
				     struct intel_ring_buffer *pipelined)
3180
{
3194
{
3181
	u32 old_read_domains, old_write_domain;
3195
	u32 old_read_domains, old_write_domain;
3182
	int ret;
3196
	int ret;
3183
 
3197
 
3184
	if (pipelined != obj->ring) {
3198
	if (pipelined != obj->ring) {
3185
		ret = i915_gem_object_sync(obj, pipelined);
3199
		ret = i915_gem_object_sync(obj, pipelined);
3186
	if (ret)
3200
	if (ret)
3187
		return ret;
3201
		return ret;
3188
	}
3202
	}
3189
 
3203
 
3190
	/* The display engine is not coherent with the LLC cache on gen6.  As
3204
	/* The display engine is not coherent with the LLC cache on gen6.  As
3191
	 * a result, we make sure that the pinning that is about to occur is
3205
	 * a result, we make sure that the pinning that is about to occur is
3192
	 * done with uncached PTEs. This is lowest common denominator for all
3206
	 * done with uncached PTEs. This is lowest common denominator for all
3193
	 * chipsets.
3207
	 * chipsets.
3194
	 *
3208
	 *
3195
	 * However for gen6+, we could do better by using the GFDT bit instead
3209
	 * However for gen6+, we could do better by using the GFDT bit instead
3196
	 * of uncaching, which would allow us to flush all the LLC-cached data
3210
	 * of uncaching, which would allow us to flush all the LLC-cached data
3197
	 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3211
	 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3198
	 */
3212
	 */
3199
	ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3213
	ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3200
	if (ret)
3214
	if (ret)
3201
		return ret;
3215
		return ret;
3202
 
3216
 
3203
	/* As the user may map the buffer once pinned in the display plane
3217
	/* As the user may map the buffer once pinned in the display plane
3204
	 * (e.g. libkms for the bootup splash), we have to ensure that we
3218
	 * (e.g. libkms for the bootup splash), we have to ensure that we
3205
	 * always use map_and_fenceable for all scanout buffers.
3219
	 * always use map_and_fenceable for all scanout buffers.
3206
	 */
3220
	 */
3207
	ret = i915_gem_object_pin(obj, alignment, true, false);
3221
	ret = i915_gem_object_pin(obj, alignment, true, false);
3208
	if (ret)
3222
	if (ret)
3209
		return ret;
3223
		return ret;
3210
 
3224
 
3211
	i915_gem_object_flush_cpu_write_domain(obj);
3225
	i915_gem_object_flush_cpu_write_domain(obj);
3212
 
3226
 
3213
	old_write_domain = obj->base.write_domain;
3227
	old_write_domain = obj->base.write_domain;
3214
	old_read_domains = obj->base.read_domains;
3228
	old_read_domains = obj->base.read_domains;
3215
 
3229
 
3216
	/* It should now be out of any other write domains, and we can update
3230
	/* It should now be out of any other write domains, and we can update
3217
	 * the domain values for our changes.
3231
	 * the domain values for our changes.
3218
	 */
3232
	 */
3219
	obj->base.write_domain = 0;
3233
	obj->base.write_domain = 0;
3220
	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3234
	obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3221
 
3235
 
3222
	trace_i915_gem_object_change_domain(obj,
3236
	trace_i915_gem_object_change_domain(obj,
3223
					    old_read_domains,
3237
					    old_read_domains,
3224
					    old_write_domain);
3238
					    old_write_domain);
3225
 
3239
 
3226
	return 0;
3240
	return 0;
3227
}
3241
}
3228
 
3242
 
3229
int
3243
int
3230
i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3244
i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3231
{
3245
{
3232
	int ret;
3246
	int ret;
3233
 
3247
 
3234
	if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3248
	if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3235
		return 0;
3249
		return 0;
3236
 
3250
 
3237
	ret = i915_gem_object_wait_rendering(obj, false);
3251
	ret = i915_gem_object_wait_rendering(obj, false);
3238
    if (ret)
3252
    if (ret)
3239
        return ret;
3253
        return ret;
3240
 
3254
 
3241
	/* Ensure that we invalidate the GPU's caches and TLBs. */
3255
	/* Ensure that we invalidate the GPU's caches and TLBs. */
3242
	obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3256
	obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3243
	return 0;
3257
	return 0;
3244
}
3258
}
3245
 
3259
 
3246
/**
3260
/**
3247
 * Moves a single object to the CPU read, and possibly write domain.
3261
 * Moves a single object to the CPU read, and possibly write domain.
3248
 *
3262
 *
3249
 * This function returns when the move is complete, including waiting on
3263
 * This function returns when the move is complete, including waiting on
3250
 * flushes to occur.
3264
 * flushes to occur.
3251
 */
3265
 */
3252
int
3266
int
3253
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3267
i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3254
{
3268
{
3255
	uint32_t old_write_domain, old_read_domains;
3269
	uint32_t old_write_domain, old_read_domains;
3256
	int ret;
3270
	int ret;
3257
 
3271
 
3258
	if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3272
	if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3259
		return 0;
3273
		return 0;
3260
 
3274
 
3261
	ret = i915_gem_object_wait_rendering(obj, !write);
3275
	ret = i915_gem_object_wait_rendering(obj, !write);
3262
	if (ret)
3276
	if (ret)
3263
		return ret;
3277
		return ret;
3264
 
3278
 
3265
	i915_gem_object_flush_gtt_write_domain(obj);
3279
	i915_gem_object_flush_gtt_write_domain(obj);
3266
 
3280
 
3267
	old_write_domain = obj->base.write_domain;
3281
	old_write_domain = obj->base.write_domain;
3268
	old_read_domains = obj->base.read_domains;
3282
	old_read_domains = obj->base.read_domains;
3269
 
3283
 
3270
	/* Flush the CPU cache if it's still invalid. */
3284
	/* Flush the CPU cache if it's still invalid. */
3271
	if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3285
	if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3272
		i915_gem_clflush_object(obj);
3286
		i915_gem_clflush_object(obj);
3273
 
3287
 
3274
		obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3288
		obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3275
	}
3289
	}
3276
 
3290
 
3277
	/* It should now be out of any other write domains, and we can update
3291
	/* It should now be out of any other write domains, and we can update
3278
	 * the domain values for our changes.
3292
	 * the domain values for our changes.
3279
	 */
3293
	 */
3280
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3294
	BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3281
 
3295
 
3282
	/* If we're writing through the CPU, then the GPU read domains will
3296
	/* If we're writing through the CPU, then the GPU read domains will
3283
	 * need to be invalidated at next use.
3297
	 * need to be invalidated at next use.
3284
	 */
3298
	 */
3285
	if (write) {
3299
	if (write) {
3286
		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3300
		obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3287
		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3301
		obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3288
	}
3302
	}
3289
 
3303
 
3290
	trace_i915_gem_object_change_domain(obj,
3304
	trace_i915_gem_object_change_domain(obj,
3291
					    old_read_domains,
3305
					    old_read_domains,
3292
					    old_write_domain);
3306
					    old_write_domain);
3293
 
3307
 
3294
	return 0;
3308
	return 0;
3295
}
3309
}
3296
 
3310
 
3297
/* Throttle our rendering by waiting until the ring has completed our requests
3311
/* Throttle our rendering by waiting until the ring has completed our requests
3298
 * emitted over 20 msec ago.
3312
 * emitted over 20 msec ago.
3299
 *
3313
 *
3300
 * Note that if we were to use the current jiffies each time around the loop,
3314
 * Note that if we were to use the current jiffies each time around the loop,
3301
 * we wouldn't escape the function with any frames outstanding if the time to
3315
 * we wouldn't escape the function with any frames outstanding if the time to
3302
 * render a frame was over 20ms.
3316
 * render a frame was over 20ms.
3303
 *
3317
 *
3304
 * This should get us reasonable parallelism between CPU and GPU but also
3318
 * This should get us reasonable parallelism between CPU and GPU but also
3305
 * relatively low latency when blocking on a particular request to finish.
3319
 * relatively low latency when blocking on a particular request to finish.
3306
 */
3320
 */
3307
static int
3321
static int
3308
i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3322
i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3309
{
3323
{
3310
	struct drm_i915_private *dev_priv = dev->dev_private;
3324
	struct drm_i915_private *dev_priv = dev->dev_private;
3311
	struct drm_i915_file_private *file_priv = file->driver_priv;
3325
	struct drm_i915_file_private *file_priv = file->driver_priv;
3312
	unsigned long recent_enough = GetTimerTicks() - msecs_to_jiffies(20);
3326
	unsigned long recent_enough = GetTimerTicks() - msecs_to_jiffies(20);
3313
	struct drm_i915_gem_request *request;
3327
	struct drm_i915_gem_request *request;
3314
	struct intel_ring_buffer *ring = NULL;
3328
	struct intel_ring_buffer *ring = NULL;
3315
	unsigned reset_counter;
3329
	unsigned reset_counter;
3316
	u32 seqno = 0;
3330
	u32 seqno = 0;
3317
	int ret;
3331
	int ret;
3318
 
3332
 
3319
	ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3333
	ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3320
	if (ret)
3334
	if (ret)
3321
		return ret;
3335
		return ret;
3322
 
3336
 
3323
	ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
3337
	ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
3324
	if (ret)
3338
	if (ret)
3325
		return ret;
3339
		return ret;
3326
 
3340
 
3327
	spin_lock(&file_priv->mm.lock);
3341
	spin_lock(&file_priv->mm.lock);
3328
	list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3342
	list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3329
		if (time_after_eq(request->emitted_jiffies, recent_enough))
3343
		if (time_after_eq(request->emitted_jiffies, recent_enough))
3330
			break;
3344
			break;
3331
 
3345
 
3332
		ring = request->ring;
3346
		ring = request->ring;
3333
		seqno = request->seqno;
3347
		seqno = request->seqno;
3334
	}
3348
	}
3335
	reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
3349
	reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
3336
	spin_unlock(&file_priv->mm.lock);
3350
	spin_unlock(&file_priv->mm.lock);
3337
 
3351
 
3338
	if (seqno == 0)
3352
	if (seqno == 0)
3339
		return 0;
3353
		return 0;
3340
 
3354
 
3341
	ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
3355
	ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
3342
	if (ret == 0)
3356
	if (ret == 0)
3343
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3357
		queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3344
 
3358
 
3345
	return ret;
3359
	return ret;
3346
}
3360
}
3347
 
3361
 
3348
int
3362
int
3349
i915_gem_object_pin(struct drm_i915_gem_object *obj,
3363
i915_gem_object_pin(struct drm_i915_gem_object *obj,
3350
		    uint32_t alignment,
3364
		    uint32_t alignment,
3351
		    bool map_and_fenceable,
3365
		    bool map_and_fenceable,
3352
		    bool nonblocking)
3366
		    bool nonblocking)
3353
{
3367
{
3354
	int ret;
3368
	int ret;
3355
 
3369
 
3356
	if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3370
	if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3357
		return -EBUSY;
3371
		return -EBUSY;
3358
 
3372
 
3359
	if (obj->gtt_space != NULL) {
3373
	if (obj->gtt_space != NULL) {
3360
		if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3374
		if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3361
		    (map_and_fenceable && !obj->map_and_fenceable)) {
3375
		    (map_and_fenceable && !obj->map_and_fenceable)) {
3362
			WARN(obj->pin_count,
3376
			WARN(obj->pin_count,
3363
			     "bo is already pinned with incorrect alignment:"
3377
			     "bo is already pinned with incorrect alignment:"
3364
			     " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3378
			     " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3365
			     " obj->map_and_fenceable=%d\n",
3379
			     " obj->map_and_fenceable=%d\n",
3366
			     obj->gtt_offset, alignment,
3380
			     obj->gtt_offset, alignment,
3367
			     map_and_fenceable,
3381
			     map_and_fenceable,
3368
			     obj->map_and_fenceable);
3382
			     obj->map_and_fenceable);
3369
			ret = i915_gem_object_unbind(obj);
3383
			ret = i915_gem_object_unbind(obj);
3370
			if (ret)
3384
			if (ret)
3371
				return ret;
3385
				return ret;
3372
		}
3386
		}
3373
	}
3387
	}
3374
 
3388
 
3375
	if (obj->gtt_space == NULL) {
3389
	if (obj->gtt_space == NULL) {
3376
		struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3390
		struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3377
 
3391
 
3378
		ret = i915_gem_object_bind_to_gtt(obj, alignment,
3392
		ret = i915_gem_object_bind_to_gtt(obj, alignment,
3379
						  map_and_fenceable,
3393
						  map_and_fenceable,
3380
						  nonblocking);
3394
						  nonblocking);
3381
		if (ret)
3395
		if (ret)
3382
			return ret;
3396
			return ret;
3383
 
3397
 
3384
		if (!dev_priv->mm.aliasing_ppgtt)
3398
		if (!dev_priv->mm.aliasing_ppgtt)
3385
			i915_gem_gtt_bind_object(obj, obj->cache_level);
3399
			i915_gem_gtt_bind_object(obj, obj->cache_level);
3386
	}
3400
	}
3387
 
3401
 
3388
	if (!obj->has_global_gtt_mapping && map_and_fenceable)
3402
	if (!obj->has_global_gtt_mapping && map_and_fenceable)
3389
		i915_gem_gtt_bind_object(obj, obj->cache_level);
3403
		i915_gem_gtt_bind_object(obj, obj->cache_level);
3390
 
3404
 
3391
	obj->pin_count++;
3405
	obj->pin_count++;
3392
	obj->pin_mappable |= map_and_fenceable;
3406
	obj->pin_mappable |= map_and_fenceable;
3393
 
3407
 
3394
	return 0;
3408
	return 0;
3395
}
3409
}
3396
 
3410
 
3397
void
3411
void
3398
i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3412
i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3399
{
3413
{
3400
	BUG_ON(obj->pin_count == 0);
3414
	BUG_ON(obj->pin_count == 0);
3401
	BUG_ON(obj->gtt_space == NULL);
3415
	BUG_ON(obj->gtt_space == NULL);
3402
 
3416
 
3403
	if (--obj->pin_count == 0)
3417
	if (--obj->pin_count == 0)
3404
		obj->pin_mappable = false;
3418
		obj->pin_mappable = false;
3405
}
3419
}
3406
 
3420
 
3407
int
3421
int
3408
i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3422
i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3409
		   struct drm_file *file)
3423
		   struct drm_file *file)
3410
{
3424
{
3411
	struct drm_i915_gem_pin *args = data;
3425
	struct drm_i915_gem_pin *args = data;
3412
	struct drm_i915_gem_object *obj;
3426
	struct drm_i915_gem_object *obj;
3413
	int ret;
3427
	int ret;
3414
 
3428
 
3415
     if(args->handle == -2)
3429
     if(args->handle == -2)
3416
     {
3430
     {
3417
        printf("%s handle %d\n", __FUNCTION__, args->handle);
3431
        printf("%s handle %d\n", __FUNCTION__, args->handle);
3418
        return 0;
3432
        return 0;
3419
     }
3433
     }
3420
 
3434
 
3421
	ret = i915_mutex_lock_interruptible(dev);
3435
	ret = i915_mutex_lock_interruptible(dev);
3422
	if (ret)
3436
	if (ret)
3423
		return ret;
3437
		return ret;
3424
 
3438
 
3425
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3439
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3426
	if (&obj->base == NULL) {
3440
	if (&obj->base == NULL) {
3427
		ret = -ENOENT;
3441
		ret = -ENOENT;
3428
		goto unlock;
3442
		goto unlock;
3429
	}
3443
	}
3430
 
3444
 
3431
	if (obj->madv != I915_MADV_WILLNEED) {
3445
	if (obj->madv != I915_MADV_WILLNEED) {
3432
		DRM_ERROR("Attempting to pin a purgeable buffer\n");
3446
		DRM_ERROR("Attempting to pin a purgeable buffer\n");
3433
		ret = -EINVAL;
3447
		ret = -EINVAL;
3434
		goto out;
3448
		goto out;
3435
	}
3449
	}
3436
 
3450
 
3437
	if (obj->pin_filp != NULL && obj->pin_filp != file) {
3451
	if (obj->pin_filp != NULL && obj->pin_filp != file) {
3438
		DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3452
		DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3439
			  args->handle);
3453
			  args->handle);
3440
		ret = -EINVAL;
3454
		ret = -EINVAL;
3441
		goto out;
3455
		goto out;
3442
	}
3456
	}
3443
 
3457
 
3444
	if (obj->user_pin_count == 0) {
3458
	if (obj->user_pin_count == 0) {
3445
		ret = i915_gem_object_pin(obj, args->alignment, true, false);
3459
		ret = i915_gem_object_pin(obj, args->alignment, true, false);
3446
		if (ret)
3460
		if (ret)
3447
			goto out;
3461
			goto out;
3448
	}
3462
	}
3449
 
3463
 
3450
	obj->user_pin_count++;
3464
	obj->user_pin_count++;
3451
	obj->pin_filp = file;
3465
	obj->pin_filp = file;
3452
 
3466
 
3453
	/* XXX - flush the CPU caches for pinned objects
3467
	/* XXX - flush the CPU caches for pinned objects
3454
	 * as the X server doesn't manage domains yet
3468
	 * as the X server doesn't manage domains yet
3455
	 */
3469
	 */
3456
	i915_gem_object_flush_cpu_write_domain(obj);
3470
	i915_gem_object_flush_cpu_write_domain(obj);
3457
	args->offset = obj->gtt_offset;
3471
	args->offset = obj->gtt_offset;
3458
out:
3472
out:
3459
	drm_gem_object_unreference(&obj->base);
3473
	drm_gem_object_unreference(&obj->base);
3460
unlock:
3474
unlock:
3461
	mutex_unlock(&dev->struct_mutex);
3475
	mutex_unlock(&dev->struct_mutex);
3462
	return ret;
3476
	return ret;
3463
}
3477
}
3464
 
3478
 
3465
#if 0
3479
#if 0
3466
 
3480
 
3467
int
3481
int
3468
i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3482
i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3469
		     struct drm_file *file)
3483
		     struct drm_file *file)
3470
{
3484
{
3471
	struct drm_i915_gem_pin *args = data;
3485
	struct drm_i915_gem_pin *args = data;
3472
	struct drm_i915_gem_object *obj;
3486
	struct drm_i915_gem_object *obj;
3473
	int ret;
3487
	int ret;
3474
 
3488
 
3475
	ret = i915_mutex_lock_interruptible(dev);
3489
	ret = i915_mutex_lock_interruptible(dev);
3476
	if (ret)
3490
	if (ret)
3477
		return ret;
3491
		return ret;
3478
 
3492
 
3479
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3493
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3480
	if (&obj->base == NULL) {
3494
	if (&obj->base == NULL) {
3481
		ret = -ENOENT;
3495
		ret = -ENOENT;
3482
		goto unlock;
3496
		goto unlock;
3483
	}
3497
	}
3484
 
3498
 
3485
	if (obj->pin_filp != file) {
3499
	if (obj->pin_filp != file) {
3486
		DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3500
		DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3487
			  args->handle);
3501
			  args->handle);
3488
		ret = -EINVAL;
3502
		ret = -EINVAL;
3489
		goto out;
3503
		goto out;
3490
	}
3504
	}
3491
	obj->user_pin_count--;
3505
	obj->user_pin_count--;
3492
	if (obj->user_pin_count == 0) {
3506
	if (obj->user_pin_count == 0) {
3493
		obj->pin_filp = NULL;
3507
		obj->pin_filp = NULL;
3494
		i915_gem_object_unpin(obj);
3508
		i915_gem_object_unpin(obj);
3495
	}
3509
	}
3496
 
3510
 
3497
out:
3511
out:
3498
	drm_gem_object_unreference(&obj->base);
3512
	drm_gem_object_unreference(&obj->base);
3499
unlock:
3513
unlock:
3500
	mutex_unlock(&dev->struct_mutex);
3514
	mutex_unlock(&dev->struct_mutex);
3501
	return ret;
3515
	return ret;
3502
}
3516
}
3503
 
3517
 
3504
#endif
3518
#endif
3505
 
3519
 
3506
int
3520
int
3507
i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3521
i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3508
		    struct drm_file *file)
3522
		    struct drm_file *file)
3509
{
3523
{
3510
	struct drm_i915_gem_busy *args = data;
3524
	struct drm_i915_gem_busy *args = data;
3511
	struct drm_i915_gem_object *obj;
3525
	struct drm_i915_gem_object *obj;
3512
	int ret;
3526
	int ret;
3513
 
3527
 
3514
	ret = i915_mutex_lock_interruptible(dev);
3528
	ret = i915_mutex_lock_interruptible(dev);
3515
	if (ret)
3529
	if (ret)
3516
		return ret;
3530
		return ret;
3517
 
3531
 
3518
    if(args->handle == -2)
3532
    if(args->handle == -2)
3519
    {
3533
    {
3520
        obj = get_fb_obj();
3534
        obj = get_fb_obj();
3521
        drm_gem_object_reference(&obj->base);
3535
        drm_gem_object_reference(&obj->base);
3522
    }
3536
    }
3523
    else
3537
    else
3524
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3538
	obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3525
	if (&obj->base == NULL) {
3539
	if (&obj->base == NULL) {
3526
		ret = -ENOENT;
3540
		ret = -ENOENT;
3527
		goto unlock;
3541
		goto unlock;
3528
	}
3542
	}
3529
 
3543
 
3530
	/* Count all active objects as busy, even if they are currently not used
3544
	/* Count all active objects as busy, even if they are currently not used
3531
	 * by the gpu. Users of this interface expect objects to eventually
3545
	 * by the gpu. Users of this interface expect objects to eventually
3532
	 * become non-busy without any further actions, therefore emit any
3546
	 * become non-busy without any further actions, therefore emit any
3533
	 * necessary flushes here.
3547
	 * necessary flushes here.
3534
	 */
3548
	 */
3535
	ret = i915_gem_object_flush_active(obj);
3549
	ret = i915_gem_object_flush_active(obj);
3536
 
3550
 
3537
	args->busy = obj->active;
3551
	args->busy = obj->active;
3538
	if (obj->ring) {
3552
	if (obj->ring) {
3539
		BUILD_BUG_ON(I915_NUM_RINGS > 16);
3553
		BUILD_BUG_ON(I915_NUM_RINGS > 16);
3540
		args->busy |= intel_ring_flag(obj->ring) << 16;
3554
		args->busy |= intel_ring_flag(obj->ring) << 16;
3541
	}
3555
	}
3542
 
3556
 
3543
	drm_gem_object_unreference(&obj->base);
3557
	drm_gem_object_unreference(&obj->base);
3544
unlock:
3558
unlock:
3545
	mutex_unlock(&dev->struct_mutex);
3559
	mutex_unlock(&dev->struct_mutex);
3546
	return ret;
3560
	return ret;
3547
}
3561
}
3548
 
3562
 
3549
int
3563
int
3550
i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3564
i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3551
			struct drm_file *file_priv)
3565
			struct drm_file *file_priv)
3552
{
3566
{
3553
	return i915_gem_ring_throttle(dev, file_priv);
3567
	return i915_gem_ring_throttle(dev, file_priv);
3554
}
3568
}
3555
 
3569
 
3556
#if 0
3570
#if 0
3557
 
3571
 
3558
int
3572
int
3559
i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3573
i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3560
		       struct drm_file *file_priv)
3574
		       struct drm_file *file_priv)
3561
{
3575
{
3562
	struct drm_i915_gem_madvise *args = data;
3576
	struct drm_i915_gem_madvise *args = data;
3563
	struct drm_i915_gem_object *obj;
3577
	struct drm_i915_gem_object *obj;
3564
	int ret;
3578
	int ret;
3565
 
3579
 
3566
	switch (args->madv) {
3580
	switch (args->madv) {
3567
	case I915_MADV_DONTNEED:
3581
	case I915_MADV_DONTNEED:
3568
	case I915_MADV_WILLNEED:
3582
	case I915_MADV_WILLNEED:
3569
	    break;
3583
	    break;
3570
	default:
3584
	default:
3571
	    return -EINVAL;
3585
	    return -EINVAL;
3572
	}
3586
	}
3573
 
3587
 
3574
	ret = i915_mutex_lock_interruptible(dev);
3588
	ret = i915_mutex_lock_interruptible(dev);
3575
	if (ret)
3589
	if (ret)
3576
		return ret;
3590
		return ret;
3577
 
3591
 
3578
	obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3592
	obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3579
	if (&obj->base == NULL) {
3593
	if (&obj->base == NULL) {
3580
		ret = -ENOENT;
3594
		ret = -ENOENT;
3581
		goto unlock;
3595
		goto unlock;
3582
	}
3596
	}
3583
 
3597
 
3584
	if (obj->pin_count) {
3598
	if (obj->pin_count) {
3585
		ret = -EINVAL;
3599
		ret = -EINVAL;
3586
		goto out;
3600
		goto out;
3587
	}
3601
	}
3588
 
3602
 
3589
	if (obj->madv != __I915_MADV_PURGED)
3603
	if (obj->madv != __I915_MADV_PURGED)
3590
		obj->madv = args->madv;
3604
		obj->madv = args->madv;
3591
 
3605
 
3592
	/* if the object is no longer attached, discard its backing storage */
3606
	/* if the object is no longer attached, discard its backing storage */
3593
	if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
3607
	if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
3594
		i915_gem_object_truncate(obj);
3608
		i915_gem_object_truncate(obj);
3595
 
3609
 
3596
	args->retained = obj->madv != __I915_MADV_PURGED;
3610
	args->retained = obj->madv != __I915_MADV_PURGED;
3597
 
3611
 
3598
out:
3612
out:
3599
	drm_gem_object_unreference(&obj->base);
3613
	drm_gem_object_unreference(&obj->base);
3600
unlock:
3614
unlock:
3601
	mutex_unlock(&dev->struct_mutex);
3615
	mutex_unlock(&dev->struct_mutex);
3602
	return ret;
3616
	return ret;
3603
}
3617
}
3604
#endif
3618
#endif
3605
 
3619
 
3606
void i915_gem_object_init(struct drm_i915_gem_object *obj,
3620
void i915_gem_object_init(struct drm_i915_gem_object *obj,
3607
			  const struct drm_i915_gem_object_ops *ops)
3621
			  const struct drm_i915_gem_object_ops *ops)
3608
{
3622
{
3609
	INIT_LIST_HEAD(&obj->mm_list);
3623
	INIT_LIST_HEAD(&obj->mm_list);
3610
	INIT_LIST_HEAD(&obj->gtt_list);
3624
	INIT_LIST_HEAD(&obj->gtt_list);
3611
	INIT_LIST_HEAD(&obj->ring_list);
3625
	INIT_LIST_HEAD(&obj->ring_list);
3612
	INIT_LIST_HEAD(&obj->exec_list);
3626
	INIT_LIST_HEAD(&obj->exec_list);
3613
 
3627
 
3614
	obj->ops = ops;
3628
	obj->ops = ops;
3615
 
3629
 
3616
	obj->fence_reg = I915_FENCE_REG_NONE;
3630
	obj->fence_reg = I915_FENCE_REG_NONE;
3617
	obj->madv = I915_MADV_WILLNEED;
3631
	obj->madv = I915_MADV_WILLNEED;
3618
	/* Avoid an unnecessary call to unbind on the first bind. */
3632
	/* Avoid an unnecessary call to unbind on the first bind. */
3619
	obj->map_and_fenceable = true;
3633
	obj->map_and_fenceable = true;
3620
 
3634
 
3621
	i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
3635
	i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
3622
}
3636
}
3623
 
3637
 
3624
static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
3638
static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
3625
	.get_pages = i915_gem_object_get_pages_gtt,
3639
	.get_pages = i915_gem_object_get_pages_gtt,
3626
	.put_pages = i915_gem_object_put_pages_gtt,
3640
	.put_pages = i915_gem_object_put_pages_gtt,
3627
};
3641
};
3628
 
3642
 
3629
struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3643
struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3630
						  size_t size)
3644
						  size_t size)
3631
{
3645
{
3632
	struct drm_i915_gem_object *obj;
3646
	struct drm_i915_gem_object *obj;
3633
	struct address_space *mapping;
3647
	struct address_space *mapping;
3634
	gfp_t mask;
3648
	gfp_t mask;
-
 
3649
 
-
 
3650
	obj = i915_gem_object_alloc(dev);
3635
 
3651
 
3636
	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3652
	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
-
 
3653
	if (obj == NULL)
-
 
3654
    {
3637
	if (obj == NULL)
3655
        FAIL();
-
 
3656
		return NULL;
3638
		return NULL;
3657
    };
3639
 
3658
 
3640
	if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3659
	if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3641
		kfree(obj);
3660
		kfree(obj);
-
 
3661
        FAIL();
3642
		return NULL;
3662
		return NULL;
3643
	}
3663
	}
3644
 
3664
 
3645
 
3665
 
3646
	i915_gem_object_init(obj, &i915_gem_object_ops);
3666
	i915_gem_object_init(obj, &i915_gem_object_ops);
3647
 
3667
 
3648
	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3668
	obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3649
	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3669
	obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3650
 
3670
 
3651
	if (HAS_LLC(dev)) {
3671
	if (HAS_LLC(dev)) {
3652
		/* On some devices, we can have the GPU use the LLC (the CPU
3672
		/* On some devices, we can have the GPU use the LLC (the CPU
3653
		 * cache) for about a 10% performance improvement
3673
		 * cache) for about a 10% performance improvement
3654
		 * compared to uncached.  Graphics requests other than
3674
		 * compared to uncached.  Graphics requests other than
3655
		 * display scanout are coherent with the CPU in
3675
		 * display scanout are coherent with the CPU in
3656
		 * accessing this cache.  This means in this mode we
3676
		 * accessing this cache.  This means in this mode we
3657
		 * don't need to clflush on the CPU side, and on the
3677
		 * don't need to clflush on the CPU side, and on the
3658
		 * GPU side we only need to flush internal caches to
3678
		 * GPU side we only need to flush internal caches to
3659
		 * get data visible to the CPU.
3679
		 * get data visible to the CPU.
3660
		 *
3680
		 *
3661
		 * However, we maintain the display planes as UC, and so
3681
		 * However, we maintain the display planes as UC, and so
3662
		 * need to rebind when first used as such.
3682
		 * need to rebind when first used as such.
3663
		 */
3683
		 */
3664
		obj->cache_level = I915_CACHE_LLC;
3684
		obj->cache_level = I915_CACHE_LLC;
3665
	} else
3685
	} else
3666
		obj->cache_level = I915_CACHE_NONE;
3686
		obj->cache_level = I915_CACHE_NONE;
3667
 
3687
 
3668
	return obj;
3688
	return obj;
3669
}
3689
}
3670
 
3690
 
3671
int i915_gem_init_object(struct drm_gem_object *obj)
3691
int i915_gem_init_object(struct drm_gem_object *obj)
3672
{
3692
{
3673
	BUG();
3693
	BUG();
3674
 
3694
 
3675
	return 0;
3695
	return 0;
3676
}
3696
}
3677
 
3697
 
3678
void i915_gem_free_object(struct drm_gem_object *gem_obj)
3698
void i915_gem_free_object(struct drm_gem_object *gem_obj)
3679
{
3699
{
3680
	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3700
	struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3681
	struct drm_device *dev = obj->base.dev;
3701
	struct drm_device *dev = obj->base.dev;
3682
	drm_i915_private_t *dev_priv = dev->dev_private;
3702
	drm_i915_private_t *dev_priv = dev->dev_private;
3683
 
3703
 
3684
	trace_i915_gem_object_destroy(obj);
3704
	trace_i915_gem_object_destroy(obj);
3685
 
3705
 
3686
//   if (obj->phys_obj)
3706
//   if (obj->phys_obj)
3687
//       i915_gem_detach_phys_object(dev, obj);
3707
//       i915_gem_detach_phys_object(dev, obj);
3688
//    printf("%s obj %p\n", __FUNCTION__, obj);
3708
//    printf("%s obj %p\n", __FUNCTION__, obj);
3689
 
3709
 
3690
	obj->pin_count = 0;
3710
	obj->pin_count = 0;
3691
	if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3711
	if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3692
		bool was_interruptible;
3712
		bool was_interruptible;
3693
 
3713
 
3694
		was_interruptible = dev_priv->mm.interruptible;
3714
		was_interruptible = dev_priv->mm.interruptible;
3695
		dev_priv->mm.interruptible = false;
3715
		dev_priv->mm.interruptible = false;
3696
 
3716
 
3697
		WARN_ON(i915_gem_object_unbind(obj));
3717
		WARN_ON(i915_gem_object_unbind(obj));
3698
 
3718
 
3699
		dev_priv->mm.interruptible = was_interruptible;
3719
		dev_priv->mm.interruptible = was_interruptible;
3700
	}
3720
	}
3701
 
3721
 
3702
	obj->pages_pin_count = 0;
3722
	obj->pages_pin_count = 0;
3703
	i915_gem_object_put_pages(obj);
3723
	i915_gem_object_put_pages(obj);
3704
//   i915_gem_object_free_mmap_offset(obj);
3724
//   i915_gem_object_free_mmap_offset(obj);
3705
 
3725
 
3706
	BUG_ON(obj->pages);
3726
	BUG_ON(obj->pages);
3707
 
3727
 
3708
//   if (obj->base.import_attach)
3728
//   if (obj->base.import_attach)
3709
//       drm_prime_gem_destroy(&obj->base, NULL);
3729
//       drm_prime_gem_destroy(&obj->base, NULL);
3710
 
3730
 
3711
    if(obj->base.filp != NULL)
3731
    if(obj->base.filp != NULL)
3712
    {
3732
    {
3713
//        printf("filp %p\n", obj->base.filp);
3733
//        printf("filp %p\n", obj->base.filp);
3714
        shmem_file_delete(obj->base.filp);
3734
        shmem_file_delete(obj->base.filp);
3715
    }
3735
    }
3716
 
3736
 
3717
	drm_gem_object_release(&obj->base);
3737
	drm_gem_object_release(&obj->base);
3718
	i915_gem_info_remove_obj(dev_priv, obj->base.size);
3738
	i915_gem_info_remove_obj(dev_priv, obj->base.size);
3719
 
3739
 
3720
	kfree(obj->bit_17);
3740
	kfree(obj->bit_17);
3721
	kfree(obj);
3741
	kfree(obj);
3722
}
3742
}
3723
 
3743
 
3724
#if 0
3744
#if 0
3725
int
3745
int
3726
i915_gem_idle(struct drm_device *dev)
3746
i915_gem_idle(struct drm_device *dev)
3727
{
3747
{
3728
	drm_i915_private_t *dev_priv = dev->dev_private;
3748
	drm_i915_private_t *dev_priv = dev->dev_private;
3729
	int ret;
3749
	int ret;
3730
 
3750
 
3731
	mutex_lock(&dev->struct_mutex);
3751
	mutex_lock(&dev->struct_mutex);
3732
 
3752
 
3733
	if (dev_priv->mm.suspended) {
3753
	if (dev_priv->mm.suspended) {
3734
		mutex_unlock(&dev->struct_mutex);
3754
		mutex_unlock(&dev->struct_mutex);
3735
		return 0;
3755
		return 0;
3736
	}
3756
	}
3737
 
3757
 
3738
	ret = i915_gpu_idle(dev);
3758
	ret = i915_gpu_idle(dev);
3739
	if (ret) {
3759
	if (ret) {
3740
		mutex_unlock(&dev->struct_mutex);
3760
		mutex_unlock(&dev->struct_mutex);
3741
		return ret;
3761
		return ret;
3742
	}
3762
	}
3743
	i915_gem_retire_requests(dev);
3763
	i915_gem_retire_requests(dev);
3744
 
3764
 
3745
	/* Under UMS, be paranoid and evict. */
3765
	/* Under UMS, be paranoid and evict. */
3746
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3766
	if (!drm_core_check_feature(dev, DRIVER_MODESET))
3747
		i915_gem_evict_everything(dev);
3767
		i915_gem_evict_everything(dev);
3748
 
-
 
3749
	i915_gem_reset_fences(dev);
-
 
3750
 
3768
 
3751
	/* Hack!  Don't let anybody do execbuf while we don't control the chip.
3769
	/* Hack!  Don't let anybody do execbuf while we don't control the chip.
3752
	 * We need to replace this with a semaphore, or something.
3770
	 * We need to replace this with a semaphore, or something.
3753
	 * And not confound mm.suspended!
3771
	 * And not confound mm.suspended!
3754
	 */
3772
	 */
3755
	dev_priv->mm.suspended = 1;
3773
	dev_priv->mm.suspended = 1;
3756
	del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
3774
	del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
3757
 
3775
 
3758
	i915_kernel_lost_context(dev);
3776
	i915_kernel_lost_context(dev);
3759
	i915_gem_cleanup_ringbuffer(dev);
3777
	i915_gem_cleanup_ringbuffer(dev);
3760
 
3778
 
3761
	mutex_unlock(&dev->struct_mutex);
3779
	mutex_unlock(&dev->struct_mutex);
3762
 
3780
 
3763
	/* Cancel the retire work handler, which should be idle now. */
3781
	/* Cancel the retire work handler, which should be idle now. */
3764
	cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3782
	cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3765
 
3783
 
3766
	return 0;
3784
	return 0;
3767
}
3785
}
3768
#endif
3786
#endif
3769
 
3787
 
3770
void i915_gem_l3_remap(struct drm_device *dev)
3788
void i915_gem_l3_remap(struct drm_device *dev)
3771
{
3789
{
3772
	drm_i915_private_t *dev_priv = dev->dev_private;
3790
	drm_i915_private_t *dev_priv = dev->dev_private;
3773
	u32 misccpctl;
3791
	u32 misccpctl;
3774
	int i;
3792
	int i;
3775
 
3793
 
3776
	if (!HAS_L3_GPU_CACHE(dev))
3794
	if (!HAS_L3_GPU_CACHE(dev))
3777
		return;
3795
		return;
3778
 
3796
 
3779
	if (!dev_priv->l3_parity.remap_info)
3797
	if (!dev_priv->l3_parity.remap_info)
3780
		return;
3798
		return;
3781
 
3799
 
3782
	misccpctl = I915_READ(GEN7_MISCCPCTL);
3800
	misccpctl = I915_READ(GEN7_MISCCPCTL);
3783
	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3801
	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
3784
	POSTING_READ(GEN7_MISCCPCTL);
3802
	POSTING_READ(GEN7_MISCCPCTL);
3785
 
3803
 
3786
	for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3804
	for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
3787
		u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3805
		u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
3788
		if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
3806
		if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
3789
			DRM_DEBUG("0x%x was already programmed to %x\n",
3807
			DRM_DEBUG("0x%x was already programmed to %x\n",
3790
				  GEN7_L3LOG_BASE + i, remap);
3808
				  GEN7_L3LOG_BASE + i, remap);
3791
		if (remap && !dev_priv->l3_parity.remap_info[i/4])
3809
		if (remap && !dev_priv->l3_parity.remap_info[i/4])
3792
			DRM_DEBUG_DRIVER("Clearing remapped register\n");
3810
			DRM_DEBUG_DRIVER("Clearing remapped register\n");
3793
		I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
3811
		I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
3794
	}
3812
	}
3795
 
3813
 
3796
	/* Make sure all the writes land before disabling dop clock gating */
3814
	/* Make sure all the writes land before disabling dop clock gating */
3797
	POSTING_READ(GEN7_L3LOG_BASE);
3815
	POSTING_READ(GEN7_L3LOG_BASE);
3798
 
3816
 
3799
	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3817
	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
3800
}
3818
}
3801
 
3819
 
3802
void i915_gem_init_swizzling(struct drm_device *dev)
3820
void i915_gem_init_swizzling(struct drm_device *dev)
3803
{
3821
{
3804
	drm_i915_private_t *dev_priv = dev->dev_private;
3822
	drm_i915_private_t *dev_priv = dev->dev_private;
3805
 
3823
 
3806
	if (INTEL_INFO(dev)->gen < 5 ||
3824
	if (INTEL_INFO(dev)->gen < 5 ||
3807
	    dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3825
	    dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3808
		return;
3826
		return;
3809
 
3827
 
3810
	I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3828
	I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3811
				 DISP_TILE_SURFACE_SWIZZLING);
3829
				 DISP_TILE_SURFACE_SWIZZLING);
3812
 
3830
 
3813
	if (IS_GEN5(dev))
3831
	if (IS_GEN5(dev))
3814
		return;
3832
		return;
3815
 
3833
 
3816
	I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3834
	I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3817
	if (IS_GEN6(dev))
3835
	if (IS_GEN6(dev))
3818
		I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
3836
		I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
3819
	else if (IS_GEN7(dev))
3837
	else if (IS_GEN7(dev))
3820
		I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
3838
		I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
3821
	else
3839
	else
3822
		BUG();
3840
		BUG();
3823
}
3841
}
3824
 
3842
 
3825
static bool
3843
static bool
3826
intel_enable_blt(struct drm_device *dev)
3844
intel_enable_blt(struct drm_device *dev)
3827
{
3845
{
3828
	if (!HAS_BLT(dev))
3846
	if (!HAS_BLT(dev))
3829
		return false;
3847
		return false;
3830
 
3848
 
3831
	/* The blitter was dysfunctional on early prototypes */
3849
	/* The blitter was dysfunctional on early prototypes */
3832
	if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3850
	if (IS_GEN6(dev) && dev->pdev->revision < 8) {
3833
		DRM_INFO("BLT not supported on this pre-production hardware;"
3851
		DRM_INFO("BLT not supported on this pre-production hardware;"
3834
			 " graphics performance will be degraded.\n");
3852
			 " graphics performance will be degraded.\n");
3835
		return false;
3853
		return false;
3836
	}
3854
	}
3837
 
3855
 
3838
	return true;
3856
	return true;
3839
}
3857
}
3840
 
3858
 
3841
static int i915_gem_init_rings(struct drm_device *dev)
3859
static int i915_gem_init_rings(struct drm_device *dev)
3842
{
3860
{
3843
	struct drm_i915_private *dev_priv = dev->dev_private;
3861
	struct drm_i915_private *dev_priv = dev->dev_private;
3844
	int ret;
3862
	int ret;
3845
 
3863
 
3846
	ret = intel_init_render_ring_buffer(dev);
3864
	ret = intel_init_render_ring_buffer(dev);
3847
	if (ret)
3865
	if (ret)
3848
		return ret;
3866
		return ret;
3849
 
3867
 
3850
    if (HAS_BSD(dev)) {
3868
    if (HAS_BSD(dev)) {
3851
		ret = intel_init_bsd_ring_buffer(dev);
3869
		ret = intel_init_bsd_ring_buffer(dev);
3852
		if (ret)
3870
		if (ret)
3853
			goto cleanup_render_ring;
3871
			goto cleanup_render_ring;
3854
	}
3872
	}
3855
 
3873
 
3856
	if (intel_enable_blt(dev)) {
3874
	if (intel_enable_blt(dev)) {
3857
		ret = intel_init_blt_ring_buffer(dev);
3875
		ret = intel_init_blt_ring_buffer(dev);
3858
		if (ret)
3876
		if (ret)
3859
			goto cleanup_bsd_ring;
3877
			goto cleanup_bsd_ring;
3860
	}
3878
	}
3861
 
3879
 
3862
	ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
3880
	ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
3863
	if (ret)
3881
	if (ret)
3864
		goto cleanup_blt_ring;
3882
		goto cleanup_blt_ring;
3865
 
3883
 
3866
	return 0;
3884
	return 0;
3867
 
3885
 
3868
cleanup_blt_ring:
3886
cleanup_blt_ring:
3869
	intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
3887
	intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
3870
cleanup_bsd_ring:
3888
cleanup_bsd_ring:
3871
	intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3889
	intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3872
cleanup_render_ring:
3890
cleanup_render_ring:
3873
	intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3891
	intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3874
 
3892
 
3875
	return ret;
3893
	return ret;
3876
}
3894
}
3877
 
3895
 
3878
int
3896
int
3879
i915_gem_init_hw(struct drm_device *dev)
3897
i915_gem_init_hw(struct drm_device *dev)
3880
{
3898
{
3881
	drm_i915_private_t *dev_priv = dev->dev_private;
3899
	drm_i915_private_t *dev_priv = dev->dev_private;
3882
	int ret;
3900
	int ret;
3883
 
3901
 
3884
	if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
3902
	if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
3885
		return -EIO;
3903
		return -EIO;
3886
 
3904
 
3887
	if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
3905
	if (IS_HASWELL(dev) && (I915_READ(0x120010) == 1))
3888
		I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
3906
		I915_WRITE(0x9008, I915_READ(0x9008) | 0xf0000);
-
 
3907
 
-
 
3908
	if (HAS_PCH_NOP(dev)) {
-
 
3909
		u32 temp = I915_READ(GEN7_MSG_CTL);
-
 
3910
		temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
-
 
3911
		I915_WRITE(GEN7_MSG_CTL, temp);
-
 
3912
	}
3889
 
3913
 
3890
	i915_gem_l3_remap(dev);
3914
	i915_gem_l3_remap(dev);
3891
 
3915
 
3892
	i915_gem_init_swizzling(dev);
3916
	i915_gem_init_swizzling(dev);
3893
 
3917
 
3894
	ret = i915_gem_init_rings(dev);
3918
	ret = i915_gem_init_rings(dev);
3895
	if (ret)
3919
	if (ret)
3896
		return ret;
3920
		return ret;
3897
 
3921
 
3898
	/*
3922
	/*
3899
	 * XXX: There was some w/a described somewhere suggesting loading
3923
	 * XXX: There was some w/a described somewhere suggesting loading
3900
	 * contexts before PPGTT.
3924
	 * contexts before PPGTT.
3901
	 */
3925
	 */
3902
	i915_gem_context_init(dev);
3926
	i915_gem_context_init(dev);
-
 
3927
	if (dev_priv->mm.aliasing_ppgtt) {
-
 
3928
		ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
-
 
3929
		if (ret) {
3903
	i915_gem_init_ppgtt(dev);
3930
			i915_gem_cleanup_aliasing_ppgtt(dev);
-
 
3931
			DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
-
 
3932
		}
-
 
3933
	}
3904
 
3934
 
3905
	return 0;
3935
	return 0;
3906
}
3936
}
3907
 
3937
 
3908
#define LFB_SIZE 0xC00000
3938
#define LFB_SIZE 0xC00000
3909
 
3939
 
3910
int i915_gem_init(struct drm_device *dev)
3940
int i915_gem_init(struct drm_device *dev)
3911
{
3941
{
3912
	struct drm_i915_private *dev_priv = dev->dev_private;
3942
	struct drm_i915_private *dev_priv = dev->dev_private;
3913
	int ret;
3943
	int ret;
3914
 
3944
 
3915
	mutex_lock(&dev->struct_mutex);
3945
	mutex_lock(&dev->struct_mutex);
-
 
3946
 
-
 
3947
	if (IS_VALLEYVIEW(dev)) {
-
 
3948
		/* VLVA0 (potential hack), BIOS isn't actually waking us */
-
 
3949
		I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
-
 
3950
		if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
-
 
3951
			DRM_DEBUG_DRIVER("allow wake ack timed out\n");
-
 
3952
	}
-
 
3953
 
3916
	i915_gem_init_global_gtt(dev);
3954
	i915_gem_init_global_gtt(dev);
-
 
3955
 
3917
	ret = i915_gem_init_hw(dev);
3956
	ret = i915_gem_init_hw(dev);
3918
	mutex_unlock(&dev->struct_mutex);
3957
	mutex_unlock(&dev->struct_mutex);
3919
	if (ret) {
3958
	if (ret) {
3920
		i915_gem_cleanup_aliasing_ppgtt(dev);
3959
		i915_gem_cleanup_aliasing_ppgtt(dev);
3921
		return ret;
3960
		return ret;
3922
	}
3961
	}
-
 
3962
 
3923
 
3963
 
3924
    return 0;
3964
    return 0;
3925
}
3965
}
3926
 
3966
 
3927
void
3967
void
3928
i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3968
i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3929
{
3969
{
3930
	drm_i915_private_t *dev_priv = dev->dev_private;
3970
	drm_i915_private_t *dev_priv = dev->dev_private;
3931
	struct intel_ring_buffer *ring;
3971
	struct intel_ring_buffer *ring;
3932
	int i;
3972
	int i;
3933
 
3973
 
3934
	for_each_ring(ring, dev_priv, i)
3974
	for_each_ring(ring, dev_priv, i)
3935
		intel_cleanup_ring_buffer(ring);
3975
		intel_cleanup_ring_buffer(ring);
3936
}
3976
}
3937
 
3977
 
3938
#if 0
3978
#if 0
3939
 
3979
 
3940
int
3980
int
3941
i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3981
i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3942
		       struct drm_file *file_priv)
3982
		       struct drm_file *file_priv)
3943
{
3983
{
3944
	drm_i915_private_t *dev_priv = dev->dev_private;
3984
	drm_i915_private_t *dev_priv = dev->dev_private;
3945
	int ret;
3985
	int ret;
3946
 
3986
 
3947
	if (drm_core_check_feature(dev, DRIVER_MODESET))
3987
	if (drm_core_check_feature(dev, DRIVER_MODESET))
3948
		return 0;
3988
		return 0;
3949
 
3989
 
3950
	if (i915_reset_in_progress(&dev_priv->gpu_error)) {
3990
	if (i915_reset_in_progress(&dev_priv->gpu_error)) {
3951
		DRM_ERROR("Reenabling wedged hardware, good luck\n");
3991
		DRM_ERROR("Reenabling wedged hardware, good luck\n");
3952
		atomic_set(&dev_priv->gpu_error.reset_counter, 0);
3992
		atomic_set(&dev_priv->gpu_error.reset_counter, 0);
3953
	}
3993
	}
3954
 
3994
 
3955
	mutex_lock(&dev->struct_mutex);
3995
	mutex_lock(&dev->struct_mutex);
3956
	dev_priv->mm.suspended = 0;
3996
	dev_priv->mm.suspended = 0;
3957
 
3997
 
3958
	ret = i915_gem_init_hw(dev);
3998
	ret = i915_gem_init_hw(dev);
3959
	if (ret != 0) {
3999
	if (ret != 0) {
3960
		mutex_unlock(&dev->struct_mutex);
4000
		mutex_unlock(&dev->struct_mutex);
3961
		return ret;
4001
		return ret;
3962
	}
4002
	}
3963
 
4003
 
3964
	BUG_ON(!list_empty(&dev_priv->mm.active_list));
4004
	BUG_ON(!list_empty(&dev_priv->mm.active_list));
3965
	mutex_unlock(&dev->struct_mutex);
4005
	mutex_unlock(&dev->struct_mutex);
3966
 
4006
 
3967
	ret = drm_irq_install(dev);
4007
	ret = drm_irq_install(dev);
3968
	if (ret)
4008
	if (ret)
3969
		goto cleanup_ringbuffer;
4009
		goto cleanup_ringbuffer;
3970
 
4010
 
3971
	return 0;
4011
	return 0;
3972
 
4012
 
3973
cleanup_ringbuffer:
4013
cleanup_ringbuffer:
3974
	mutex_lock(&dev->struct_mutex);
4014
	mutex_lock(&dev->struct_mutex);
3975
	i915_gem_cleanup_ringbuffer(dev);
4015
	i915_gem_cleanup_ringbuffer(dev);
3976
	dev_priv->mm.suspended = 1;
4016
	dev_priv->mm.suspended = 1;
3977
	mutex_unlock(&dev->struct_mutex);
4017
	mutex_unlock(&dev->struct_mutex);
3978
 
4018
 
3979
	return ret;
4019
	return ret;
3980
}
4020
}
3981
 
4021
 
3982
int
4022
int
3983
i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4023
i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3984
		       struct drm_file *file_priv)
4024
		       struct drm_file *file_priv)
3985
{
4025
{
3986
	if (drm_core_check_feature(dev, DRIVER_MODESET))
4026
	if (drm_core_check_feature(dev, DRIVER_MODESET))
3987
		return 0;
4027
		return 0;
3988
 
4028
 
3989
	drm_irq_uninstall(dev);
4029
	drm_irq_uninstall(dev);
3990
	return i915_gem_idle(dev);
4030
	return i915_gem_idle(dev);
3991
}
4031
}
3992
 
4032
 
3993
void
4033
void
3994
i915_gem_lastclose(struct drm_device *dev)
4034
i915_gem_lastclose(struct drm_device *dev)
3995
{
4035
{
3996
	int ret;
4036
	int ret;
3997
 
4037
 
3998
	if (drm_core_check_feature(dev, DRIVER_MODESET))
4038
	if (drm_core_check_feature(dev, DRIVER_MODESET))
3999
		return;
4039
		return;
4000
 
4040
 
4001
	ret = i915_gem_idle(dev);
4041
	ret = i915_gem_idle(dev);
4002
	if (ret)
4042
	if (ret)
4003
		DRM_ERROR("failed to idle hardware: %d\n", ret);
4043
		DRM_ERROR("failed to idle hardware: %d\n", ret);
4004
}
4044
}
4005
#endif
4045
#endif
4006
 
4046
 
4007
static void
4047
static void
4008
init_ring_lists(struct intel_ring_buffer *ring)
4048
init_ring_lists(struct intel_ring_buffer *ring)
4009
{
4049
{
4010
    INIT_LIST_HEAD(&ring->active_list);
4050
    INIT_LIST_HEAD(&ring->active_list);
4011
    INIT_LIST_HEAD(&ring->request_list);
4051
    INIT_LIST_HEAD(&ring->request_list);
4012
}
4052
}
4013
 
4053
 
4014
void
4054
void
4015
i915_gem_load(struct drm_device *dev)
4055
i915_gem_load(struct drm_device *dev)
4016
{
4056
{
4017
	drm_i915_private_t *dev_priv = dev->dev_private;
4057
	drm_i915_private_t *dev_priv = dev->dev_private;
4018
    int i;
4058
    int i;
4019
 
4059
 
4020
    INIT_LIST_HEAD(&dev_priv->mm.active_list);
4060
    INIT_LIST_HEAD(&dev_priv->mm.active_list);
4021
    INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4061
    INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4022
	INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4062
	INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4023
	INIT_LIST_HEAD(&dev_priv->mm.bound_list);
4063
	INIT_LIST_HEAD(&dev_priv->mm.bound_list);
4024
    INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4064
    INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4025
    for (i = 0; i < I915_NUM_RINGS; i++)
4065
    for (i = 0; i < I915_NUM_RINGS; i++)
4026
        init_ring_lists(&dev_priv->ring[i]);
4066
        init_ring_lists(&dev_priv->ring[i]);
4027
	for (i = 0; i < I915_MAX_NUM_FENCES; i++)
4067
	for (i = 0; i < I915_MAX_NUM_FENCES; i++)
4028
        INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4068
        INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4029
	INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4069
	INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4030
			  i915_gem_retire_work_handler);
4070
			  i915_gem_retire_work_handler);
4031
	init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
4071
	init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
4032
 
4072
 
4033
    /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4073
    /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4034
    if (IS_GEN3(dev)) {
4074
    if (IS_GEN3(dev)) {
4035
		I915_WRITE(MI_ARB_STATE,
4075
		I915_WRITE(MI_ARB_STATE,
4036
			   _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
4076
			   _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
4037
    }
4077
    }
4038
 
4078
 
4039
    dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4079
    dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
-
 
4080
 
-
 
4081
	if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
4040
 
4082
		dev_priv->num_fence_regs = 32;
4041
    if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4083
	else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4042
        dev_priv->num_fence_regs = 16;
4084
        dev_priv->num_fence_regs = 16;
4043
    else
4085
    else
4044
        dev_priv->num_fence_regs = 8;
4086
        dev_priv->num_fence_regs = 8;
4045
 
4087
 
4046
    /* Initialize fence registers to zero */
4088
    /* Initialize fence registers to zero */
-
 
4089
	INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4047
	i915_gem_reset_fences(dev);
4090
	i915_gem_restore_fences(dev);
4048
 
4091
 
4049
    i915_gem_detect_bit_6_swizzle(dev);
4092
    i915_gem_detect_bit_6_swizzle(dev);
4050
 
4093
 
4051
    dev_priv->mm.interruptible = true;
4094
    dev_priv->mm.interruptible = true;
4052
 
4095
 
4053
//    dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4096
//    dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4054
//    dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4097
//    dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4055
//    register_shrinker(&dev_priv->mm.inactive_shrinker);
4098
//    register_shrinker(&dev_priv->mm.inactive_shrinker);
4056
}
4099
}
4057
//>
4100
//>
4058
//>
4101
//>
4059
#define>
4102
#define>
4060
#define>
4103
#define>
4061
#define>
4104
#define>
4062
#define>
4105
#define>