Subversion Repositories Kolibri OS

Rev

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

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