Subversion Repositories Kolibri OS

Rev

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

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