Subversion Repositories Kolibri OS

Rev

Rev 6937 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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