Subversion Repositories Kolibri OS

Rev

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

Rev 3258 Rev 3263
1
/*
1
/*
2
 * Copyright (c) 2011 Intel Corporation
2
 * Copyright (c) 2011 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 FROM,
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
 * SOFTWARE.
21
 * SOFTWARE.
22
 *
22
 *
23
 * Authors:
23
 * Authors:
24
 *    Chris Wilson 
24
 *    Chris Wilson 
25
 *
25
 *
26
 */
26
 */
27
 
27
 
28
#ifdef HAVE_CONFIG_H
28
#ifdef HAVE_CONFIG_H
29
#include "config.h"
29
#include "config.h"
30
#endif
30
#endif
31
 
31
 
32
#include "sna.h"
32
#include "sna.h"
33
#include "sna_reg.h"
33
#include "sna_reg.h"
34
 
34
 
35
 
35
 
36
unsigned int cpu_cache_size();
36
unsigned int cpu_cache_size();
37
 
37
 
38
static struct kgem_bo *
38
static struct kgem_bo *
39
search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags);
39
search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags);
40
 
40
 
41
static struct kgem_bo *
41
static struct kgem_bo *
42
search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags);
42
search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags);
43
 
43
 
44
#define DBG_NO_HW 0
44
#define DBG_NO_HW 0
45
#define DBG_NO_TILING 1
45
#define DBG_NO_TILING 1
46
#define DBG_NO_CACHE 0
46
#define DBG_NO_CACHE 0
47
#define DBG_NO_CACHE_LEVEL 0
47
#define DBG_NO_CACHE_LEVEL 0
48
#define DBG_NO_CPU 0
48
#define DBG_NO_CPU 0
49
#define DBG_NO_USERPTR 0
49
#define DBG_NO_USERPTR 0
50
#define DBG_NO_LLC 0
50
#define DBG_NO_LLC 0
51
#define DBG_NO_SEMAPHORES 0
51
#define DBG_NO_SEMAPHORES 0
52
#define DBG_NO_MADV 1
52
#define DBG_NO_MADV 1
53
#define DBG_NO_UPLOAD_CACHE 0
53
#define DBG_NO_UPLOAD_CACHE 0
54
#define DBG_NO_UPLOAD_ACTIVE 0
54
#define DBG_NO_UPLOAD_ACTIVE 0
55
#define DBG_NO_MAP_UPLOAD 0
55
#define DBG_NO_MAP_UPLOAD 0
56
#define DBG_NO_RELAXED_FENCING 0
56
#define DBG_NO_RELAXED_FENCING 0
57
#define DBG_NO_SECURE_BATCHES 0
57
#define DBG_NO_SECURE_BATCHES 0
58
#define DBG_NO_PINNED_BATCHES 0
58
#define DBG_NO_PINNED_BATCHES 0
59
#define DBG_NO_FAST_RELOC 0
59
#define DBG_NO_FAST_RELOC 0
60
#define DBG_NO_HANDLE_LUT 0
60
#define DBG_NO_HANDLE_LUT 0
61
#define DBG_DUMP 0
61
#define DBG_DUMP 0
62
 
62
 
63
#ifndef DEBUG_SYNC
63
#ifndef DEBUG_SYNC
64
#define DEBUG_SYNC 0
64
#define DEBUG_SYNC 0
65
#endif
65
#endif
66
 
66
 
67
#define SHOW_BATCH 0
67
#define SHOW_BATCH 0
68
 
68
 
69
#if 0
69
#if 0
70
#define ASSERT_IDLE(kgem__, handle__) assert(!__kgem_busy(kgem__, handle__))
70
#define ASSERT_IDLE(kgem__, handle__) assert(!__kgem_busy(kgem__, handle__))
71
#define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__) assert(!(expect__) || !__kgem_busy(kgem__, handle__))
71
#define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__) assert(!(expect__) || !__kgem_busy(kgem__, handle__))
72
#else
72
#else
73
#define ASSERT_IDLE(kgem__, handle__)
73
#define ASSERT_IDLE(kgem__, handle__)
74
#define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__)
74
#define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__)
75
#endif
75
#endif
76
 
76
 
77
/* Worst case seems to be 965gm where we cannot write within a cacheline that
77
/* Worst case seems to be 965gm where we cannot write within a cacheline that
78
 * is being simultaneously being read by the GPU, or within the sampler
78
 * is being simultaneously being read by the GPU, or within the sampler
79
 * prefetch. In general, the chipsets seem to have a requirement that sampler
79
 * prefetch. In general, the chipsets seem to have a requirement that sampler
80
 * offsets be aligned to a cacheline (64 bytes).
80
 * offsets be aligned to a cacheline (64 bytes).
81
 */
81
 */
82
#define UPLOAD_ALIGNMENT 128
82
#define UPLOAD_ALIGNMENT 128
83
 
83
 
84
#define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE)
84
#define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE)
85
#define NUM_PAGES(x) (((x) + PAGE_SIZE-1) / PAGE_SIZE)
85
#define NUM_PAGES(x) (((x) + PAGE_SIZE-1) / PAGE_SIZE)
86
 
86
 
87
#define MAX_GTT_VMA_CACHE 512
87
#define MAX_GTT_VMA_CACHE 512
88
#define MAX_CPU_VMA_CACHE INT16_MAX
88
#define MAX_CPU_VMA_CACHE INT16_MAX
89
#define MAP_PRESERVE_TIME 10
89
#define MAP_PRESERVE_TIME 10
90
 
90
 
91
#define MAP(ptr) ((void*)((uintptr_t)(ptr) & ~3))
91
#define MAP(ptr) ((void*)((uintptr_t)(ptr) & ~3))
92
#define MAKE_CPU_MAP(ptr) ((void*)((uintptr_t)(ptr) | 1))
92
#define MAKE_CPU_MAP(ptr) ((void*)((uintptr_t)(ptr) | 1))
93
#define MAKE_USER_MAP(ptr) ((void*)((uintptr_t)(ptr) | 3))
93
#define MAKE_USER_MAP(ptr) ((void*)((uintptr_t)(ptr) | 3))
94
#define IS_USER_MAP(ptr) ((uintptr_t)(ptr) & 2)
94
#define IS_USER_MAP(ptr) ((uintptr_t)(ptr) & 2)
95
#define __MAP_TYPE(ptr) ((uintptr_t)(ptr) & 3)
95
#define __MAP_TYPE(ptr) ((uintptr_t)(ptr) & 3)
96
 
96
 
97
#define MAKE_REQUEST(rq, ring) ((struct kgem_request *)((uintptr_t)(rq) | (ring)))
97
#define MAKE_REQUEST(rq, ring) ((struct kgem_request *)((uintptr_t)(rq) | (ring)))
98
 
98
 
99
#define LOCAL_I915_PARAM_HAS_BLT		        11
99
#define LOCAL_I915_PARAM_HAS_BLT		        11
100
#define LOCAL_I915_PARAM_HAS_RELAXED_FENCING	12
100
#define LOCAL_I915_PARAM_HAS_RELAXED_FENCING	12
101
#define LOCAL_I915_PARAM_HAS_RELAXED_DELTA	    15
101
#define LOCAL_I915_PARAM_HAS_RELAXED_DELTA	    15
102
#define LOCAL_I915_PARAM_HAS_SEMAPHORES		    20
102
#define LOCAL_I915_PARAM_HAS_SEMAPHORES		    20
103
#define LOCAL_I915_PARAM_HAS_SECURE_BATCHES	    23
103
#define LOCAL_I915_PARAM_HAS_SECURE_BATCHES	    23
104
#define LOCAL_I915_PARAM_HAS_PINNED_BATCHES	    24
104
#define LOCAL_I915_PARAM_HAS_PINNED_BATCHES	    24
105
#define LOCAL_I915_PARAM_HAS_NO_RELOC		    25
105
#define LOCAL_I915_PARAM_HAS_NO_RELOC		    25
106
#define LOCAL_I915_PARAM_HAS_HANDLE_LUT		    26
106
#define LOCAL_I915_PARAM_HAS_HANDLE_LUT		    26
107
 
107
 
108
#define LOCAL_I915_EXEC_IS_PINNED		(1<<10)
108
#define LOCAL_I915_EXEC_IS_PINNED		(1<<10)
109
#define LOCAL_I915_EXEC_NO_RELOC		(1<<11)
109
#define LOCAL_I915_EXEC_NO_RELOC		(1<<11)
110
#define LOCAL_I915_EXEC_HANDLE_LUT		(1<<12)
110
#define LOCAL_I915_EXEC_HANDLE_LUT		(1<<12)
-
 
111
struct local_i915_gem_userptr {
-
 
112
	uint64_t user_ptr;
-
 
113
	uint32_t user_size;
-
 
114
	uint32_t flags;
-
 
115
#define I915_USERPTR_READ_ONLY (1<<0)
-
 
116
#define I915_USERPTR_UNSYNCHRONIZED (1<<31)
-
 
117
	uint32_t handle;
-
 
118
};
-
 
119
 
111
#define UNCACHED	0
120
#define UNCACHED	0
112
#define SNOOPED		1
121
#define SNOOPED		1
113
 
122
 
114
struct local_i915_gem_cacheing {
123
struct local_i915_gem_cacheing {
115
	uint32_t handle;
124
	uint32_t handle;
116
	uint32_t cacheing;
125
	uint32_t cacheing;
117
};
126
};
118
 
127
 
119
#define LOCAL_IOCTL_I915_GEM_SET_CACHEING SRV_I915_GEM_SET_CACHEING
128
#define LOCAL_IOCTL_I915_GEM_SET_CACHEING SRV_I915_GEM_SET_CACHEING
-
 
129
 
-
 
130
struct local_fbinfo {
-
 
131
	int width;
-
 
132
	int height;
-
 
133
	int pitch;
-
 
134
	int tiling;
-
 
135
};
120
 
136
 
121
struct kgem_buffer {
137
struct kgem_buffer {
122
	struct kgem_bo base;
138
	struct kgem_bo base;
123
	void *mem;
139
	void *mem;
124
	uint32_t used;
140
	uint32_t used;
125
	uint32_t need_io : 1;
141
	uint32_t need_io : 1;
126
	uint32_t write : 2;
142
	uint32_t write : 2;
127
	uint32_t mmapped : 1;
143
	uint32_t mmapped : 1;
128
};
144
};
129
 
145
 
130
static struct kgem_bo *__kgem_freed_bo;
146
static struct kgem_bo *__kgem_freed_bo;
131
static struct kgem_request *__kgem_freed_request;
147
static struct kgem_request *__kgem_freed_request;
132
static struct drm_i915_gem_exec_object2 _kgem_dummy_exec;
148
static struct drm_i915_gem_exec_object2 _kgem_dummy_exec;
133
 
149
 
134
static inline int bytes(struct kgem_bo *bo)
150
static inline int bytes(struct kgem_bo *bo)
135
{
151
{
136
	return __kgem_bo_size(bo);
152
	return __kgem_bo_size(bo);
137
}
153
}
138
 
154
 
139
#define bucket(B) (B)->size.pages.bucket
155
#define bucket(B) (B)->size.pages.bucket
140
#define num_pages(B) (B)->size.pages.count
156
#define num_pages(B) (B)->size.pages.count
141
 
157
 
142
#ifdef DEBUG_MEMORY
158
#ifdef DEBUG_MEMORY
143
static void debug_alloc(struct kgem *kgem, size_t size)
159
static void debug_alloc(struct kgem *kgem, size_t size)
144
{
160
{
145
	kgem->debug_memory.bo_allocs++;
161
	kgem->debug_memory.bo_allocs++;
146
	kgem->debug_memory.bo_bytes += size;
162
	kgem->debug_memory.bo_bytes += size;
147
}
163
}
148
static void debug_alloc__bo(struct kgem *kgem, struct kgem_bo *bo)
164
static void debug_alloc__bo(struct kgem *kgem, struct kgem_bo *bo)
149
{
165
{
150
	debug_alloc(kgem, bytes(bo));
166
	debug_alloc(kgem, bytes(bo));
151
}
167
}
152
#else
168
#else
153
#define debug_alloc(k, b)
169
#define debug_alloc(k, b)
154
#define debug_alloc__bo(k, b)
170
#define debug_alloc__bo(k, b)
155
#endif
171
#endif
156
 
172
 
157
static void kgem_sna_reset(struct kgem *kgem)
173
static void kgem_sna_reset(struct kgem *kgem)
158
{
174
{
159
	struct sna *sna = container_of(kgem, struct sna, kgem);
175
	struct sna *sna = container_of(kgem, struct sna, kgem);
160
 
176
 
161
	sna->render.reset(sna);
177
	sna->render.reset(sna);
162
	sna->blt_state.fill_bo = 0;
178
	sna->blt_state.fill_bo = 0;
163
}
179
}
164
 
180
 
165
static void kgem_sna_flush(struct kgem *kgem)
181
static void kgem_sna_flush(struct kgem *kgem)
166
{
182
{
167
	struct sna *sna = container_of(kgem, struct sna, kgem);
183
	struct sna *sna = container_of(kgem, struct sna, kgem);
168
 
184
 
169
	sna->render.flush(sna);
185
	sna->render.flush(sna);
170
 
186
 
171
//	if (sna->render.solid_cache.dirty)
187
//	if (sna->render.solid_cache.dirty)
172
//		sna_render_flush_solid(sna);
188
//		sna_render_flush_solid(sna);
173
}
189
}
174
 
190
 
175
static bool gem_set_tiling(int fd, uint32_t handle, int tiling, int stride)
191
static bool gem_set_tiling(int fd, uint32_t handle, int tiling, int stride)
176
{
192
{
177
	struct drm_i915_gem_set_tiling set_tiling;
193
	struct drm_i915_gem_set_tiling set_tiling;
178
	int ret;
194
	int ret;
179
 
195
 
180
	if (DBG_NO_TILING)
196
	if (DBG_NO_TILING)
181
		return false;
197
		return false;
182
/*
198
/*
183
	VG_CLEAR(set_tiling);
199
	VG_CLEAR(set_tiling);
184
	do {
200
	do {
185
		set_tiling.handle = handle;
201
		set_tiling.handle = handle;
186
		set_tiling.tiling_mode = tiling;
202
		set_tiling.tiling_mode = tiling;
187
		set_tiling.stride = stride;
203
		set_tiling.stride = stride;
188
 
204
 
189
		ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
205
		ret = ioctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
190
	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
206
	} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
191
*/	
207
*/	
192
	return ret == 0;
208
	return false;//ret == 0;
193
}
209
}
194
 
210
 
195
static bool gem_set_cacheing(int fd, uint32_t handle, int cacheing)
211
static bool gem_set_cacheing(int fd, uint32_t handle, int cacheing)
196
{
212
{
197
	struct local_i915_gem_cacheing arg;
213
	struct local_i915_gem_cacheing arg;
198
 
214
 
199
	VG_CLEAR(arg);
215
	VG_CLEAR(arg);
200
	arg.handle = handle;
216
	arg.handle = handle;
201
	arg.cacheing = cacheing;
217
	arg.cacheing = cacheing;
202
	return drmIoctl(fd, LOCAL_IOCTL_I915_GEM_SET_CACHEING, &arg) == 0;
218
	return drmIoctl(fd, LOCAL_IOCTL_I915_GEM_SET_CACHEING, &arg) == 0;
203
}
219
}
204
	
220
	
205
	
221
	
206
 
222
 
207
	
223
	
208
 
224
 
209
static bool __kgem_throttle_retire(struct kgem *kgem, unsigned flags)
225
static bool __kgem_throttle_retire(struct kgem *kgem, unsigned flags)
210
{
226
{
211
	if (flags & CREATE_NO_RETIRE) {
227
	if (flags & CREATE_NO_RETIRE) {
212
		DBG(("%s: not retiring per-request\n", __FUNCTION__));
228
		DBG(("%s: not retiring per-request\n", __FUNCTION__));
213
		return false;
229
		return false;
214
	}
230
	}
215
 
231
 
216
	if (!kgem->need_retire) {
232
	if (!kgem->need_retire) {
217
		DBG(("%s: nothing to retire\n", __FUNCTION__));
233
		DBG(("%s: nothing to retire\n", __FUNCTION__));
218
		return false;
234
		return false;
219
	}
235
	}
220
 
236
 
221
	if (kgem_retire(kgem))
237
	if (kgem_retire(kgem))
222
		return true;
238
		return true;
223
 
239
 
224
	if (flags & CREATE_NO_THROTTLE || !kgem->need_throttle) {
240
	if (flags & CREATE_NO_THROTTLE || !kgem->need_throttle) {
225
		DBG(("%s: not throttling\n", __FUNCTION__));
241
		DBG(("%s: not throttling\n", __FUNCTION__));
226
		return false;
242
		return false;
227
	}
243
	}
228
 
244
 
229
	kgem_throttle(kgem);
245
	kgem_throttle(kgem);
230
	return kgem_retire(kgem);
246
	return kgem_retire(kgem);
231
}
247
}
232
 
248
 
233
static void *__kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo)
249
static void *__kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo)
234
{
250
{
235
	struct drm_i915_gem_mmap_gtt mmap_arg;
251
	struct drm_i915_gem_mmap_gtt mmap_arg;
236
	void *ptr;
252
	void *ptr;
237
 
253
 
238
	DBG(("%s(handle=%d, size=%d)\n", __FUNCTION__,
254
	DBG(("%s(handle=%d, size=%d)\n", __FUNCTION__,
239
	     bo->handle, bytes(bo)));
255
	     bo->handle, bytes(bo)));
240
	assert(bo->proxy == NULL);
256
	assert(bo->proxy == NULL);
241
 
257
 
242
retry_gtt:
258
retry_gtt:
243
	VG_CLEAR(mmap_arg);
259
	VG_CLEAR(mmap_arg);
244
	mmap_arg.handle = bo->handle;
260
	mmap_arg.handle = bo->handle;
245
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg)) {
261
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg)) {
246
		printf("%s: failed to retrieve GTT offset for handle=%d: %d\n",
262
		printf("%s: failed to retrieve GTT offset for handle=%d: %d\n",
247
		       __FUNCTION__, bo->handle, 0);
263
		       __FUNCTION__, bo->handle, 0);
248
		(void)__kgem_throttle_retire(kgem, 0);
264
		(void)__kgem_throttle_retire(kgem, 0);
249
		if (kgem_expire_cache(kgem))
265
		if (kgem_expire_cache(kgem))
250
			goto retry_gtt;
266
			goto retry_gtt;
251
 
267
 
252
		if (kgem->need_expire) {
268
		if (kgem->need_expire) {
253
			kgem_cleanup_cache(kgem);
269
			kgem_cleanup_cache(kgem);
254
			goto retry_gtt;
270
			goto retry_gtt;
255
		}
271
		}
256
 
272
 
257
		return NULL;
273
		return NULL;
258
	}
274
	}
259
 
275
 
260
retry_mmap:
276
retry_mmap:
261
//	ptr = mmap(0, bytes(bo), PROT_READ | PROT_WRITE, MAP_SHARED,
277
//	ptr = mmap(0, bytes(bo), PROT_READ | PROT_WRITE, MAP_SHARED,
262
//		   kgem->fd, mmap_arg.offset);
278
//		   kgem->fd, mmap_arg.offset);
263
	if (ptr == 0) {
279
//	if (ptr == 0) {
264
		printf("%s: failed to mmap %d, %d bytes, into GTT domain: %d\n",
280
		printf("%s: failed to mmap %d, %d bytes, into GTT domain: %d\n",
265
		       __FUNCTION__, bo->handle, bytes(bo), 0);
281
		       __FUNCTION__, bo->handle, bytes(bo), 0);
266
		if (__kgem_throttle_retire(kgem, 0))
282
//		if (__kgem_throttle_retire(kgem, 0))
267
			goto retry_mmap;
283
//			goto retry_mmap;
268
 
284
 
269
		if (kgem->need_expire) {
285
//		if (kgem->need_expire) {
270
			kgem_cleanup_cache(kgem);
286
//			kgem_cleanup_cache(kgem);
271
			goto retry_mmap;
287
//			goto retry_mmap;
272
		}
288
//		}
273
 
289
 
274
		ptr = NULL;
290
		ptr = NULL;
275
	}
291
//	}
276
 
292
 
277
	return ptr;
293
	return ptr;
278
}
294
}
279
 
295
 
280
static int __gem_write(int fd, uint32_t handle,
296
static int __gem_write(int fd, uint32_t handle,
281
		       int offset, int length,
297
		       int offset, int length,
282
		       const void *src)
298
		       const void *src)
283
{
299
{
284
	struct drm_i915_gem_pwrite pwrite;
300
	struct drm_i915_gem_pwrite pwrite;
285
 
301
 
286
	DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__,
302
	DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__,
287
	     handle, offset, length));
303
	     handle, offset, length));
288
 
304
 
289
	VG_CLEAR(pwrite);
305
	VG_CLEAR(pwrite);
290
	pwrite.handle = handle;
306
	pwrite.handle = handle;
291
	pwrite.offset = offset;
307
	pwrite.offset = offset;
292
	pwrite.size = length;
308
	pwrite.size = length;
293
	pwrite.data_ptr = (uintptr_t)src;
309
	pwrite.data_ptr = (uintptr_t)src;
294
	return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
310
	return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
295
}
311
}
296
 
312
 
297
static int gem_write(int fd, uint32_t handle,
313
static int gem_write(int fd, uint32_t handle,
298
		     int offset, int length,
314
		     int offset, int length,
299
		     const void *src)
315
		     const void *src)
300
{
316
{
301
	struct drm_i915_gem_pwrite pwrite;
317
	struct drm_i915_gem_pwrite pwrite;
302
 
318
 
303
	DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__,
319
	DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__,
304
	     handle, offset, length));
320
	     handle, offset, length));
305
 
321
 
306
	VG_CLEAR(pwrite);
322
	VG_CLEAR(pwrite);
307
	pwrite.handle = handle;
323
	pwrite.handle = handle;
308
	/* align the transfer to cachelines; fortuitously this is safe! */
324
	/* align the transfer to cachelines; fortuitously this is safe! */
309
	if ((offset | length) & 63) {
325
	if ((offset | length) & 63) {
310
		pwrite.offset = offset & ~63;
326
		pwrite.offset = offset & ~63;
311
		pwrite.size = ALIGN(offset+length, 64) - pwrite.offset;
327
		pwrite.size = ALIGN(offset+length, 64) - pwrite.offset;
312
		pwrite.data_ptr = (uintptr_t)src + pwrite.offset - offset;
328
		pwrite.data_ptr = (uintptr_t)src + pwrite.offset - offset;
313
	} else {
329
	} else {
314
		pwrite.offset = offset;
330
		pwrite.offset = offset;
315
		pwrite.size = length;
331
		pwrite.size = length;
316
		pwrite.data_ptr = (uintptr_t)src;
332
		pwrite.data_ptr = (uintptr_t)src;
317
	}
333
	}
318
	return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
334
	return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
319
}
335
}
320
	
336
	
321
 
337
 
322
bool __kgem_busy(struct kgem *kgem, int handle)
338
bool __kgem_busy(struct kgem *kgem, int handle)
323
{
339
{
324
	struct drm_i915_gem_busy busy;
340
	struct drm_i915_gem_busy busy;
325
    
341
    
326
	VG_CLEAR(busy);
342
	VG_CLEAR(busy);
327
	busy.handle = handle;
343
	busy.handle = handle;
328
	busy.busy = !kgem->wedged;
344
	busy.busy = !kgem->wedged;
329
	(void)drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
345
	(void)drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
330
	DBG(("%s: handle=%d, busy=%d, wedged=%d\n",
346
	DBG(("%s: handle=%d, busy=%d, wedged=%d\n",
331
	     __FUNCTION__, handle, busy.busy, kgem->wedged));
347
	     __FUNCTION__, handle, busy.busy, kgem->wedged));
332
 
348
 
333
	return busy.busy;
349
	return busy.busy;
334
}
350
}
335
 
351
 
336
static void kgem_bo_retire(struct kgem *kgem, struct kgem_bo *bo)
352
static void kgem_bo_retire(struct kgem *kgem, struct kgem_bo *bo)
337
{
353
{
338
	DBG(("%s: retiring bo handle=%d (needed flush? %d), rq? %d [busy?=%d]\n",
354
	DBG(("%s: retiring bo handle=%d (needed flush? %d), rq? %d [busy?=%d]\n",
339
	     __FUNCTION__, bo->handle, bo->needs_flush, bo->rq != NULL,
355
	     __FUNCTION__, bo->handle, bo->needs_flush, bo->rq != NULL,
340
	     __kgem_busy(kgem, bo->handle)));
356
	     __kgem_busy(kgem, bo->handle)));
341
	assert(bo->exec == NULL);
357
	assert(bo->exec == NULL);
342
	assert(list_is_empty(&bo->vma));
358
	assert(list_is_empty(&bo->vma));
343
 
359
 
344
	if (bo->rq) {
360
	if (bo->rq) {
345
		if (!__kgem_busy(kgem, bo->handle)) {
361
		if (!__kgem_busy(kgem, bo->handle)) {
346
			__kgem_bo_clear_busy(bo);
362
			__kgem_bo_clear_busy(bo);
347
			kgem_retire(kgem);
363
			kgem_retire(kgem);
348
		}
364
		}
349
	} else {
365
	} else {
350
		assert(!bo->needs_flush);
366
		assert(!bo->needs_flush);
351
		ASSERT_IDLE(kgem, bo->handle);
367
		ASSERT_IDLE(kgem, bo->handle);
352
	}
368
	}
353
}
369
}
354
 
370
 
355
bool kgem_bo_write(struct kgem *kgem, struct kgem_bo *bo,
371
bool kgem_bo_write(struct kgem *kgem, struct kgem_bo *bo,
356
		   const void *data, int length)
372
		   const void *data, int length)
357
{
373
{
358
	assert(bo->refcnt);
374
	assert(bo->refcnt);
359
	assert(!bo->purged);
375
	assert(!bo->purged);
360
	assert(bo->proxy == NULL);
376
	assert(bo->proxy == NULL);
361
	ASSERT_IDLE(kgem, bo->handle);
377
	ASSERT_IDLE(kgem, bo->handle);
362
 
378
 
363
	assert(length <= bytes(bo));
379
	assert(length <= bytes(bo));
364
	if (gem_write(kgem->fd, bo->handle, 0, length, data))
380
	if (gem_write(kgem->fd, bo->handle, 0, length, data))
365
		return false;
381
		return false;
366
 
382
 
367
	DBG(("%s: flush=%d, domain=%d\n", __FUNCTION__, bo->flush, bo->domain));
383
	DBG(("%s: flush=%d, domain=%d\n", __FUNCTION__, bo->flush, bo->domain));
368
	if (bo->exec == NULL) {
384
	if (bo->exec == NULL) {
369
		kgem_bo_retire(kgem, bo);
385
		kgem_bo_retire(kgem, bo);
370
		bo->domain = DOMAIN_NONE;
386
		bo->domain = DOMAIN_NONE;
371
	}
387
	}
372
	return true;
388
	return true;
373
}
389
}
374
 
390
 
375
static uint32_t gem_create(int fd, int num_pages)
391
static uint32_t gem_create(int fd, int num_pages)
376
{
392
{
377
	struct drm_i915_gem_create create;
393
	struct drm_i915_gem_create create;
378
 
394
 
379
	VG_CLEAR(create);
395
	VG_CLEAR(create);
380
	create.handle = 0;
396
	create.handle = 0;
381
	create.size = PAGE_SIZE * num_pages;
397
	create.size = PAGE_SIZE * num_pages;
382
	(void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
398
	(void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
383
 
399
 
384
	return create.handle;
400
	return create.handle;
385
}
401
}
386
 
402
 
387
static bool
403
static bool
388
kgem_bo_set_purgeable(struct kgem *kgem, struct kgem_bo *bo)
404
kgem_bo_set_purgeable(struct kgem *kgem, struct kgem_bo *bo)
389
{
405
{
390
#if DBG_NO_MADV
406
#if DBG_NO_MADV
391
	return true;
407
	return true;
392
#else
408
#else
393
	struct drm_i915_gem_madvise madv;
409
	struct drm_i915_gem_madvise madv;
394
 
410
 
395
	assert(bo->exec == NULL);
411
	assert(bo->exec == NULL);
396
	assert(!bo->purged);
412
	assert(!bo->purged);
397
 
413
 
398
	VG_CLEAR(madv);
414
	VG_CLEAR(madv);
399
	madv.handle = bo->handle;
415
	madv.handle = bo->handle;
400
	madv.madv = I915_MADV_DONTNEED;
416
	madv.madv = I915_MADV_DONTNEED;
401
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) {
417
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) {
402
		bo->purged = 1;
418
		bo->purged = 1;
403
		kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU;
419
		kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU;
404
		return madv.retained;
420
		return madv.retained;
405
	}
421
	}
406
 
422
 
407
	return true;
423
	return true;
408
#endif
424
#endif
409
}
425
}
410
 
426
 
411
static bool
427
static bool
412
kgem_bo_is_retained(struct kgem *kgem, struct kgem_bo *bo)
428
kgem_bo_is_retained(struct kgem *kgem, struct kgem_bo *bo)
413
{
429
{
414
#if DBG_NO_MADV
430
#if DBG_NO_MADV
415
	return true;
431
	return true;
416
#else
432
#else
417
	struct drm_i915_gem_madvise madv;
433
	struct drm_i915_gem_madvise madv;
418
 
434
 
419
	if (!bo->purged)
435
	if (!bo->purged)
420
		return true;
436
		return true;
421
 
437
 
422
	VG_CLEAR(madv);
438
	VG_CLEAR(madv);
423
	madv.handle = bo->handle;
439
	madv.handle = bo->handle;
424
	madv.madv = I915_MADV_DONTNEED;
440
	madv.madv = I915_MADV_DONTNEED;
425
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0)
441
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0)
426
		return madv.retained;
442
		return madv.retained;
427
 
443
 
428
	return false;
444
	return false;
429
#endif
445
#endif
430
}
446
}
431
 
447
 
432
static bool
448
static bool
433
kgem_bo_clear_purgeable(struct kgem *kgem, struct kgem_bo *bo)
449
kgem_bo_clear_purgeable(struct kgem *kgem, struct kgem_bo *bo)
434
{
450
{
435
#if DBG_NO_MADV
451
#if DBG_NO_MADV
436
	return true;
452
	return true;
437
#else
453
#else
438
	struct drm_i915_gem_madvise madv;
454
	struct drm_i915_gem_madvise madv;
439
 
455
 
440
	assert(bo->purged);
456
	assert(bo->purged);
441
 
457
 
442
	VG_CLEAR(madv);
458
	VG_CLEAR(madv);
443
	madv.handle = bo->handle;
459
	madv.handle = bo->handle;
444
	madv.madv = I915_MADV_WILLNEED;
460
	madv.madv = I915_MADV_WILLNEED;
445
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) {
461
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) {
446
		bo->purged = !madv.retained;
462
		bo->purged = !madv.retained;
447
		kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU;
463
		kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU;
448
		return madv.retained;
464
		return madv.retained;
449
	}
465
	}
450
 
466
 
451
	return false;
467
	return false;
452
#endif
468
#endif
453
}
469
}
454
 
470
 
455
static void gem_close(int fd, uint32_t handle)
471
static void gem_close(int fd, uint32_t handle)
456
{
472
{
457
	struct drm_gem_close close;
473
	struct drm_gem_close close;
458
 
474
 
459
	VG_CLEAR(close);
475
	VG_CLEAR(close);
460
	close.handle = handle;
476
	close.handle = handle;
461
	(void)drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
477
	(void)drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close);
462
}
478
}
463
 
479
 
464
constant inline static unsigned long __fls(unsigned long word)
480
constant inline static unsigned long __fls(unsigned long word)
465
{
481
{
466
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86__) || defined(__x86_64__))
482
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86__) || defined(__x86_64__))
467
	asm("bsr %1,%0"
483
	asm("bsr %1,%0"
468
	    : "=r" (word)
484
	    : "=r" (word)
469
	    : "rm" (word));
485
	    : "rm" (word));
470
	return word;
486
	return word;
471
#else
487
#else
472
	unsigned int v = 0;
488
	unsigned int v = 0;
473
 
489
 
474
	while (word >>= 1)
490
	while (word >>= 1)
475
		v++;
491
		v++;
476
 
492
 
477
	return v;
493
	return v;
478
#endif
494
#endif
479
}
495
}
480
 
496
 
481
constant inline static int cache_bucket(int num_pages)
497
constant inline static int cache_bucket(int num_pages)
482
{
498
{
483
	return __fls(num_pages);
499
	return __fls(num_pages);
484
}
500
}
485
 
501
 
486
static struct kgem_bo *__kgem_bo_init(struct kgem_bo *bo,
502
static struct kgem_bo *__kgem_bo_init(struct kgem_bo *bo,
487
				      int handle, int num_pages)
503
				      int handle, int num_pages)
488
{
504
{
489
	assert(num_pages);
505
	assert(num_pages);
490
	memset(bo, 0, sizeof(*bo));
506
	memset(bo, 0, sizeof(*bo));
491
 
507
 
492
	bo->refcnt = 1;
508
	bo->refcnt = 1;
493
	bo->handle = handle;
509
	bo->handle = handle;
494
	bo->target_handle = -1;
510
	bo->target_handle = -1;
495
	num_pages(bo) = num_pages;
511
	num_pages(bo) = num_pages;
496
	bucket(bo) = cache_bucket(num_pages);
512
	bucket(bo) = cache_bucket(num_pages);
497
	bo->reusable = true;
513
	bo->reusable = true;
498
	bo->domain = DOMAIN_CPU;
514
	bo->domain = DOMAIN_CPU;
499
	list_init(&bo->request);
515
	list_init(&bo->request);
500
	list_init(&bo->list);
516
	list_init(&bo->list);
501
	list_init(&bo->vma);
517
	list_init(&bo->vma);
502
 
518
 
503
	return bo;
519
	return bo;
504
}
520
}
505
 
521
 
506
static struct kgem_bo *__kgem_bo_alloc(int handle, int num_pages)
522
static struct kgem_bo *__kgem_bo_alloc(int handle, int num_pages)
507
{
523
{
508
	struct kgem_bo *bo;
524
	struct kgem_bo *bo;
509
 
525
 
510
	if (__kgem_freed_bo) {
526
	if (__kgem_freed_bo) {
511
		bo = __kgem_freed_bo;
527
		bo = __kgem_freed_bo;
512
		__kgem_freed_bo = *(struct kgem_bo **)bo;
528
		__kgem_freed_bo = *(struct kgem_bo **)bo;
513
	} else {
529
	} else {
514
		bo = malloc(sizeof(*bo));
530
		bo = malloc(sizeof(*bo));
515
		if (bo == NULL)
531
		if (bo == NULL)
516
			return NULL;
532
			return NULL;
517
	}
533
	}
518
 
534
 
519
	return __kgem_bo_init(bo, handle, num_pages);
535
	return __kgem_bo_init(bo, handle, num_pages);
520
}
536
}
521
 
537
 
522
static struct kgem_request *__kgem_request_alloc(struct kgem *kgem)
538
static struct kgem_request *__kgem_request_alloc(struct kgem *kgem)
523
{
539
{
524
	struct kgem_request *rq;
540
	struct kgem_request *rq;
525
 
541
 
526
	rq = __kgem_freed_request;
542
	rq = __kgem_freed_request;
527
	if (rq) {
543
	if (rq) {
528
		__kgem_freed_request = *(struct kgem_request **)rq;
544
		__kgem_freed_request = *(struct kgem_request **)rq;
529
	} else {
545
	} else {
530
		rq = malloc(sizeof(*rq));
546
		rq = malloc(sizeof(*rq));
531
		if (rq == NULL)
547
		if (rq == NULL)
532
			rq = &kgem->static_request;
548
			rq = &kgem->static_request;
533
	}
549
	}
534
 
550
 
535
	list_init(&rq->buffers);
551
	list_init(&rq->buffers);
536
	rq->bo = NULL;
552
	rq->bo = NULL;
537
	rq->ring = 0;
553
	rq->ring = 0;
538
 
554
 
539
	return rq;
555
	return rq;
540
}
556
}
541
 
557
 
542
static void __kgem_request_free(struct kgem_request *rq)
558
static void __kgem_request_free(struct kgem_request *rq)
543
{
559
{
544
	_list_del(&rq->list);
560
	_list_del(&rq->list);
545
	*(struct kgem_request **)rq = __kgem_freed_request;
561
	*(struct kgem_request **)rq = __kgem_freed_request;
546
	__kgem_freed_request = rq;
562
	__kgem_freed_request = rq;
547
}
563
}
548
 
564
 
549
static struct list *inactive(struct kgem *kgem, int num_pages)
565
static struct list *inactive(struct kgem *kgem, int num_pages)
550
{
566
{
551
	assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE);
567
	assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE);
552
	assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS);
568
	assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS);
553
	return &kgem->inactive[cache_bucket(num_pages)];
569
	return &kgem->inactive[cache_bucket(num_pages)];
554
}
570
}
555
 
571
 
556
static struct list *active(struct kgem *kgem, int num_pages, int tiling)
572
static struct list *active(struct kgem *kgem, int num_pages, int tiling)
557
{
573
{
558
	assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE);
574
	assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE);
559
	assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS);
575
	assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS);
560
	return &kgem->active[cache_bucket(num_pages)][tiling];
576
	return &kgem->active[cache_bucket(num_pages)][tiling];
561
}
577
}
562
 
578
 
563
static size_t
579
static size_t
564
agp_aperture_size(struct pci_device *dev, unsigned gen)
580
agp_aperture_size(struct pci_device *dev, unsigned gen)
565
{
581
{
566
	/* XXX assume that only future chipsets are unknown and follow
582
	/* XXX assume that only future chipsets are unknown and follow
567
	 * the post gen2 PCI layout.
583
	 * the post gen2 PCI layout.
568
	 */
584
	 */
569
//	return dev->regions[gen < 030 ? 0 : 2].size;
585
//	return dev->regions[gen < 030 ? 0 : 2].size;
570
 
586
 
571
    return 0;
587
    return 0;
572
}
588
}
573
 
589
 
574
static size_t
590
static size_t
575
total_ram_size(void)
591
total_ram_size(void)
576
{
592
{
577
    uint32_t  data[9];
593
    uint32_t  data[9];
578
    size_t    size = 0;
594
    size_t    size = 0;
579
    
595
    
580
    asm volatile("int $0x40"
596
    asm volatile("int $0x40"
581
        : "=a" (size)
597
        : "=a" (size)
582
        : "a" (18),"b"(20), "c" (data)
598
        : "a" (18),"b"(20), "c" (data)
583
        : "memory");
599
        : "memory");
584
    
600
    
585
    return size != -1 ? size : 0;
601
    return size != -1 ? size : 0;
586
}
602
}
587
 
603
 
588
static int gem_param(struct kgem *kgem, int name)
604
static int gem_param(struct kgem *kgem, int name)
589
{
605
{
590
    drm_i915_getparam_t gp;
606
    drm_i915_getparam_t gp;
591
    int v = -1; /* No param uses the sign bit, reserve it for errors */
607
    int v = -1; /* No param uses the sign bit, reserve it for errors */
592
 
608
 
593
    VG_CLEAR(gp);
609
    VG_CLEAR(gp);
594
    gp.param = name;
610
    gp.param = name;
595
    gp.value = &v;
611
    gp.value = &v;
596
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GETPARAM, &gp))
612
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GETPARAM, &gp))
597
        return -1;
613
        return -1;
598
 
614
 
599
    VG(VALGRIND_MAKE_MEM_DEFINED(&v, sizeof(v)));
615
    VG(VALGRIND_MAKE_MEM_DEFINED(&v, sizeof(v)));
600
    return v;
616
    return v;
601
}
617
}
602
 
618
 
603
static bool test_has_execbuffer2(struct kgem *kgem)
619
static bool test_has_execbuffer2(struct kgem *kgem)
604
{
620
{
605
	return 1;
621
	return 1;
606
}
622
}
607
 
623
 
608
static bool test_has_no_reloc(struct kgem *kgem)
624
static bool test_has_no_reloc(struct kgem *kgem)
609
{
625
{
610
	if (DBG_NO_FAST_RELOC)
626
	if (DBG_NO_FAST_RELOC)
611
		return false;
627
		return false;
612
 
628
 
613
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_NO_RELOC) > 0;
629
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_NO_RELOC) > 0;
614
}
630
}
615
 
631
 
616
static bool test_has_handle_lut(struct kgem *kgem)
632
static bool test_has_handle_lut(struct kgem *kgem)
617
{
633
{
618
	if (DBG_NO_HANDLE_LUT)
634
	if (DBG_NO_HANDLE_LUT)
619
		return false;
635
		return false;
620
 
636
 
621
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_HANDLE_LUT) > 0;
637
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_HANDLE_LUT) > 0;
622
}
638
}
623
 
639
 
624
static bool test_has_semaphores_enabled(struct kgem *kgem)
640
static bool test_has_semaphores_enabled(struct kgem *kgem)
625
{
641
{
626
	FILE *file;
642
	FILE *file;
627
	bool detected = false;
643
	bool detected = false;
628
	int ret;
644
	int ret;
629
 
645
 
630
	if (DBG_NO_SEMAPHORES)
646
	if (DBG_NO_SEMAPHORES)
631
		return false;
647
		return false;
632
 
648
 
633
	ret = gem_param(kgem, LOCAL_I915_PARAM_HAS_SEMAPHORES);
649
	ret = gem_param(kgem, LOCAL_I915_PARAM_HAS_SEMAPHORES);
634
	if (ret != -1)
650
	if (ret != -1)
635
		return ret > 0;
651
		return ret > 0;
636
 
652
 
637
	return detected;
653
	return detected;
638
}
654
}
639
 
655
 
640
static bool __kgem_throttle(struct kgem *kgem)
656
static bool __kgem_throttle(struct kgem *kgem)
641
{
657
{
642
//	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_THROTTLE, NULL) == 0)
658
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_THROTTLE, NULL) == 0)
643
		return false;
659
		return false;
644
 
660
 
645
//	return errno == EIO;
661
	return errno == EIO;
646
}
662
}
647
 
663
 
648
static bool is_hw_supported(struct kgem *kgem,
664
static bool is_hw_supported(struct kgem *kgem,
649
			    struct pci_device *dev)
665
			    struct pci_device *dev)
650
{
666
{
651
	if (DBG_NO_HW)
667
	if (DBG_NO_HW)
652
		return false;
668
		return false;
653
 
669
 
654
	if (!test_has_execbuffer2(kgem))
670
	if (!test_has_execbuffer2(kgem))
655
		return false;
671
		return false;
656
 
672
 
657
	if (kgem->gen == (unsigned)-1) /* unknown chipset, assume future gen */
673
	if (kgem->gen == (unsigned)-1) /* unknown chipset, assume future gen */
658
		return kgem->has_blt;
674
		return kgem->has_blt;
659
 
675
 
660
	/* Although pre-855gm the GMCH is fubar, it works mostly. So
676
	/* Although pre-855gm the GMCH is fubar, it works mostly. So
661
	 * let the user decide through "NoAccel" whether or not to risk
677
	 * let the user decide through "NoAccel" whether or not to risk
662
	 * hw acceleration.
678
	 * hw acceleration.
663
	 */
679
	 */
664
 
680
 
665
	if (kgem->gen == 060 && dev->revision < 8) {
681
	if (kgem->gen == 060 && dev->revision < 8) {
666
		/* pre-production SNB with dysfunctional BLT */
682
		/* pre-production SNB with dysfunctional BLT */
667
		return false;
683
		return false;
668
	}
684
	}
669
 
685
 
670
	if (kgem->gen >= 060) /* Only if the kernel supports the BLT ring */
686
	if (kgem->gen >= 060) /* Only if the kernel supports the BLT ring */
671
		return kgem->has_blt;
687
		return kgem->has_blt;
672
 
688
 
673
	return true;
689
	return true;
674
}
690
}
675
 
691
 
676
static bool test_has_relaxed_fencing(struct kgem *kgem)
692
static bool test_has_relaxed_fencing(struct kgem *kgem)
677
{
693
{
678
	if (kgem->gen < 040) {
694
	if (kgem->gen < 040) {
679
		if (DBG_NO_RELAXED_FENCING)
695
		if (DBG_NO_RELAXED_FENCING)
680
			return false;
696
			return false;
681
 
697
 
682
		return gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_FENCING) > 0;
698
		return gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_FENCING) > 0;
683
	} else
699
	} else
684
		return true;
700
		return true;
685
}
701
}
686
 
702
 
687
static bool test_has_llc(struct kgem *kgem)
703
static bool test_has_llc(struct kgem *kgem)
688
{
704
{
689
	int has_llc = -1;
705
	int has_llc = -1;
690
 
706
 
691
	if (DBG_NO_LLC)
707
	if (DBG_NO_LLC)
692
		return false;
708
		return false;
693
 
709
 
694
#if defined(I915_PARAM_HAS_LLC) /* Expected in libdrm-2.4.31 */
710
#if defined(I915_PARAM_HAS_LLC) /* Expected in libdrm-2.4.31 */
695
	has_llc = gem_param(kgem, I915_PARAM_HAS_LLC);
711
	has_llc = gem_param(kgem, I915_PARAM_HAS_LLC);
696
#endif
712
#endif
697
	if (has_llc == -1) {
713
	if (has_llc == -1) {
698
		DBG(("%s: no kernel/drm support for HAS_LLC, assuming support for LLC based on GPU generation\n", __FUNCTION__));
714
		DBG(("%s: no kernel/drm support for HAS_LLC, assuming support for LLC based on GPU generation\n", __FUNCTION__));
699
		has_llc = kgem->gen >= 060;
715
		has_llc = kgem->gen >= 060;
700
	}
716
	}
701
 
717
 
702
	return has_llc;
718
	return has_llc;
703
}
719
}
704
 
720
 
705
static bool test_has_cacheing(struct kgem *kgem)
721
static bool test_has_cacheing(struct kgem *kgem)
706
{
722
{
707
	uint32_t handle;
723
	uint32_t handle;
708
	bool ret;
724
	bool ret;
709
 
725
 
710
	if (DBG_NO_CACHE_LEVEL)
726
	if (DBG_NO_CACHE_LEVEL)
711
		return false;
727
		return false;
712
 
728
 
713
	/* Incoherent blt and sampler hangs the GPU */
729
	/* Incoherent blt and sampler hangs the GPU */
714
	if (kgem->gen == 040)
730
	if (kgem->gen == 040)
715
		return false;
731
		return false;
716
 
732
 
717
	handle = gem_create(kgem->fd, 1);
733
	handle = gem_create(kgem->fd, 1);
718
	if (handle == 0)
734
	if (handle == 0)
719
		return false;
735
		return false;
720
 
736
 
721
	ret = gem_set_cacheing(kgem->fd, handle, UNCACHED);
737
	ret = gem_set_cacheing(kgem->fd, handle, UNCACHED);
722
	gem_close(kgem->fd, handle);
738
	gem_close(kgem->fd, handle);
723
	return ret;
739
	return ret;
724
}
740
}
725
 
741
 
726
static bool test_has_userptr(struct kgem *kgem)
742
static bool test_has_userptr(struct kgem *kgem)
727
{
743
{
728
#if defined(USE_USERPTR)
744
#if defined(USE_USERPTR)
729
	uint32_t handle;
745
	uint32_t handle;
730
	void *ptr;
746
	void *ptr;
731
 
747
 
732
	if (DBG_NO_USERPTR)
748
	if (DBG_NO_USERPTR)
733
		return false;
749
		return false;
734
 
750
 
735
	/* Incoherent blt and sampler hangs the GPU */
751
	/* Incoherent blt and sampler hangs the GPU */
736
	if (kgem->gen == 040)
752
	if (kgem->gen == 040)
737
		return false;
753
		return false;
738
 
754
 
739
	ptr = malloc(PAGE_SIZE);
755
	ptr = malloc(PAGE_SIZE);
740
	handle = gem_userptr(kgem->fd, ptr, PAGE_SIZE, false);
756
	handle = gem_userptr(kgem->fd, ptr, PAGE_SIZE, false);
741
	gem_close(kgem->fd, handle);
757
	gem_close(kgem->fd, handle);
742
	free(ptr);
758
	free(ptr);
743
 
759
 
744
	return handle != 0;
760
	return handle != 0;
745
#else
761
#else
746
	return false;
762
	return false;
747
#endif
763
#endif
748
}
764
}
749
 
765
 
750
static bool test_has_secure_batches(struct kgem *kgem)
766
static bool test_has_secure_batches(struct kgem *kgem)
751
{
767
{
752
	if (DBG_NO_SECURE_BATCHES)
768
	if (DBG_NO_SECURE_BATCHES)
753
		return false;
769
		return false;
754
 
770
 
755
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_SECURE_BATCHES) > 0;
771
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_SECURE_BATCHES) > 0;
756
}
772
}
757
 
773
 
758
static bool test_has_pinned_batches(struct kgem *kgem)
774
static bool test_has_pinned_batches(struct kgem *kgem)
759
{
775
{
760
	if (DBG_NO_PINNED_BATCHES)
776
	if (DBG_NO_PINNED_BATCHES)
761
		return false;
777
		return false;
762
 
778
 
763
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_PINNED_BATCHES) > 0;
779
	return gem_param(kgem, LOCAL_I915_PARAM_HAS_PINNED_BATCHES) > 0;
764
}
780
}
765
 
781
 
766
 
782
 
767
static bool kgem_init_pinned_batches(struct kgem *kgem)
783
static bool kgem_init_pinned_batches(struct kgem *kgem)
768
{
784
{
769
	int count[2] = { 4, 2 };
785
	int count[2] = { 4, 2 };
770
	int size[2] = { 1, 4 };
786
	int size[2] = { 1, 4 };
771
	int n, i;
787
	int n, i;
772
 
788
 
773
	if (kgem->wedged)
789
	if (kgem->wedged)
774
		return true;
790
		return true;
775
 
791
 
776
	for (n = 0; n < ARRAY_SIZE(count); n++) {
792
	for (n = 0; n < ARRAY_SIZE(count); n++) {
777
		for (i = 0; i < count[n]; i++) {
793
		for (i = 0; i < count[n]; i++) {
778
			struct drm_i915_gem_pin pin;
794
			struct drm_i915_gem_pin pin;
779
			struct kgem_bo *bo;
795
			struct kgem_bo *bo;
780
 
796
 
781
			VG_CLEAR(pin);
797
			VG_CLEAR(pin);
782
 
798
 
783
			pin.handle = gem_create(kgem->fd, size[n]);
799
			pin.handle = gem_create(kgem->fd, size[n]);
784
			if (pin.handle == 0)
800
			if (pin.handle == 0)
785
				goto err;
801
				goto err;
786
 
802
 
787
			DBG(("%s: new handle=%d, num_pages=%d\n",
803
			DBG(("%s: new handle=%d, num_pages=%d\n",
788
			     __FUNCTION__, pin.handle, size[n]));
804
			     __FUNCTION__, pin.handle, size[n]));
789
 
805
 
790
			bo = __kgem_bo_alloc(pin.handle, size[n]);
806
			bo = __kgem_bo_alloc(pin.handle, size[n]);
791
			if (bo == NULL) {
807
			if (bo == NULL) {
792
				gem_close(kgem->fd, pin.handle);
808
				gem_close(kgem->fd, pin.handle);
793
				goto err;
809
				goto err;
794
			}
810
			}
795
 
811
 
796
			pin.alignment = 0;
812
			pin.alignment = 0;
797
			if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
813
			if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_PIN, &pin)) {
798
				gem_close(kgem->fd, pin.handle);
814
				gem_close(kgem->fd, pin.handle);
799
				goto err;
815
				goto err;
800
			}
816
			}
801
			bo->presumed_offset = pin.offset;
817
			bo->presumed_offset = pin.offset;
802
			debug_alloc__bo(kgem, bo);
818
			debug_alloc__bo(kgem, bo);
803
			list_add(&bo->list, &kgem->pinned_batches[n]);
819
			list_add(&bo->list, &kgem->pinned_batches[n]);
804
		}
820
		}
805
	}
821
	}
806
 
822
 
807
	return true;
823
	return true;
808
 
824
 
809
err:
825
err:
810
	for (n = 0; n < ARRAY_SIZE(kgem->pinned_batches); n++) {
826
	for (n = 0; n < ARRAY_SIZE(kgem->pinned_batches); n++) {
811
		while (!list_is_empty(&kgem->pinned_batches[n])) {
827
		while (!list_is_empty(&kgem->pinned_batches[n])) {
812
			kgem_bo_destroy(kgem,
828
			kgem_bo_destroy(kgem,
813
					list_first_entry(&kgem->pinned_batches[n],
829
					list_first_entry(&kgem->pinned_batches[n],
814
							 struct kgem_bo, list));
830
							 struct kgem_bo, list));
815
		}
831
		}
816
	}
832
	}
817
 
833
 
818
	/* For simplicity populate the lists with a single unpinned bo */
834
	/* For simplicity populate the lists with a single unpinned bo */
819
	for (n = 0; n < ARRAY_SIZE(count); n++) {
835
	for (n = 0; n < ARRAY_SIZE(count); n++) {
820
		struct kgem_bo *bo;
836
		struct kgem_bo *bo;
821
		uint32_t handle;
837
		uint32_t handle;
822
 
838
 
823
		handle = gem_create(kgem->fd, size[n]);
839
		handle = gem_create(kgem->fd, size[n]);
824
		if (handle == 0)
840
		if (handle == 0)
825
			break;
841
			break;
826
 
842
 
827
		bo = __kgem_bo_alloc(handle, size[n]);
843
		bo = __kgem_bo_alloc(handle, size[n]);
828
		if (bo == NULL) {
844
		if (bo == NULL) {
829
			gem_close(kgem->fd, handle);
845
			gem_close(kgem->fd, handle);
830
			break;
846
			break;
831
		}
847
		}
832
 
848
 
833
		debug_alloc__bo(kgem, bo);
849
		debug_alloc__bo(kgem, bo);
834
		list_add(&bo->list, &kgem->pinned_batches[n]);
850
		list_add(&bo->list, &kgem->pinned_batches[n]);
835
	}
851
	}
836
	return false;
852
	return false;
837
}
853
}
838
 
854
 
839
void kgem_init(struct kgem *kgem, int fd, struct pci_device *dev, unsigned gen)
855
void kgem_init(struct kgem *kgem, int fd, struct pci_device *dev, unsigned gen)
840
{
856
{
841
    struct drm_i915_gem_get_aperture aperture;
857
    struct drm_i915_gem_get_aperture aperture;
842
    size_t totalram;
858
    size_t totalram;
843
    unsigned half_gpu_max;
859
    unsigned half_gpu_max;
844
    unsigned int i, j;
860
    unsigned int i, j;
845
 
861
 
846
    DBG(("%s: fd=%d, gen=%d\n", __FUNCTION__, fd, gen));
862
    DBG(("%s: fd=%d, gen=%d\n", __FUNCTION__, fd, gen));
847
 
863
 
848
    memset(kgem, 0, sizeof(*kgem));
864
    memset(kgem, 0, sizeof(*kgem));
849
 
865
 
850
    kgem->fd = fd;
866
    kgem->fd = fd;
851
    kgem->gen = gen;
867
    kgem->gen = gen;
852
 
868
 
853
    list_init(&kgem->requests[0]);
869
    list_init(&kgem->requests[0]);
854
    list_init(&kgem->requests[1]);
870
    list_init(&kgem->requests[1]);
855
    list_init(&kgem->batch_buffers);
871
    list_init(&kgem->batch_buffers);
856
    list_init(&kgem->active_buffers);
872
    list_init(&kgem->active_buffers);
857
    list_init(&kgem->flushing);
873
    list_init(&kgem->flushing);
858
    list_init(&kgem->large);
874
    list_init(&kgem->large);
859
    list_init(&kgem->large_inactive);
875
    list_init(&kgem->large_inactive);
860
    list_init(&kgem->snoop);
876
    list_init(&kgem->snoop);
861
    list_init(&kgem->scanout);
877
    list_init(&kgem->scanout);
862
    for (i = 0; i < ARRAY_SIZE(kgem->pinned_batches); i++)
878
    for (i = 0; i < ARRAY_SIZE(kgem->pinned_batches); i++)
863
        list_init(&kgem->pinned_batches[i]);
879
        list_init(&kgem->pinned_batches[i]);
864
    for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++)
880
    for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++)
865
        list_init(&kgem->inactive[i]);
881
        list_init(&kgem->inactive[i]);
866
    for (i = 0; i < ARRAY_SIZE(kgem->active); i++) {
882
    for (i = 0; i < ARRAY_SIZE(kgem->active); i++) {
867
        for (j = 0; j < ARRAY_SIZE(kgem->active[i]); j++)
883
        for (j = 0; j < ARRAY_SIZE(kgem->active[i]); j++)
868
            list_init(&kgem->active[i][j]);
884
            list_init(&kgem->active[i][j]);
869
    }
885
    }
870
    for (i = 0; i < ARRAY_SIZE(kgem->vma); i++) {
886
    for (i = 0; i < ARRAY_SIZE(kgem->vma); i++) {
871
        for (j = 0; j < ARRAY_SIZE(kgem->vma[i].inactive); j++)
887
        for (j = 0; j < ARRAY_SIZE(kgem->vma[i].inactive); j++)
872
            list_init(&kgem->vma[i].inactive[j]);
888
            list_init(&kgem->vma[i].inactive[j]);
873
    }
889
    }
874
    kgem->vma[MAP_GTT].count = -MAX_GTT_VMA_CACHE;
890
    kgem->vma[MAP_GTT].count = -MAX_GTT_VMA_CACHE;
875
    kgem->vma[MAP_CPU].count = -MAX_CPU_VMA_CACHE;
891
    kgem->vma[MAP_CPU].count = -MAX_CPU_VMA_CACHE;
876
 
892
 
877
    kgem->has_blt = gem_param(kgem, LOCAL_I915_PARAM_HAS_BLT) > 0;
893
    kgem->has_blt = gem_param(kgem, LOCAL_I915_PARAM_HAS_BLT) > 0;
878
    DBG(("%s: has BLT ring? %d\n", __FUNCTION__,
894
    DBG(("%s: has BLT ring? %d\n", __FUNCTION__,
879
         kgem->has_blt));
895
         kgem->has_blt));
880
 
896
 
881
    kgem->has_relaxed_delta =
897
    kgem->has_relaxed_delta =
882
        gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_DELTA) > 0;
898
        gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_DELTA) > 0;
883
    DBG(("%s: has relaxed delta? %d\n", __FUNCTION__,
899
    DBG(("%s: has relaxed delta? %d\n", __FUNCTION__,
884
         kgem->has_relaxed_delta));
900
         kgem->has_relaxed_delta));
885
 
901
 
886
    kgem->has_relaxed_fencing = test_has_relaxed_fencing(kgem);
902
    kgem->has_relaxed_fencing = test_has_relaxed_fencing(kgem);
887
    DBG(("%s: has relaxed fencing? %d\n", __FUNCTION__,
903
    DBG(("%s: has relaxed fencing? %d\n", __FUNCTION__,
888
         kgem->has_relaxed_fencing));
904
         kgem->has_relaxed_fencing));
889
 
905
 
890
    kgem->has_llc = test_has_llc(kgem);
906
    kgem->has_llc = test_has_llc(kgem);
891
    DBG(("%s: has shared last-level-cache? %d\n", __FUNCTION__,
907
    DBG(("%s: has shared last-level-cache? %d\n", __FUNCTION__,
892
         kgem->has_llc));
908
         kgem->has_llc));
893
 
909
 
894
    kgem->has_cacheing = test_has_cacheing(kgem);
910
    kgem->has_cacheing = test_has_cacheing(kgem);
895
    DBG(("%s: has set-cache-level? %d\n", __FUNCTION__,
911
    DBG(("%s: has set-cache-level? %d\n", __FUNCTION__,
896
         kgem->has_cacheing));
912
         kgem->has_cacheing));
897
 
913
 
898
    kgem->has_userptr = test_has_userptr(kgem);
914
    kgem->has_userptr = test_has_userptr(kgem);
899
    DBG(("%s: has userptr? %d\n", __FUNCTION__,
915
    DBG(("%s: has userptr? %d\n", __FUNCTION__,
900
         kgem->has_userptr));
916
         kgem->has_userptr));
901
 
917
 
902
    kgem->has_no_reloc = test_has_no_reloc(kgem);
918
    kgem->has_no_reloc = test_has_no_reloc(kgem);
903
    DBG(("%s: has no-reloc? %d\n", __FUNCTION__,
919
    DBG(("%s: has no-reloc? %d\n", __FUNCTION__,
904
         kgem->has_no_reloc));
920
         kgem->has_no_reloc));
905
 
921
 
906
    kgem->has_handle_lut = test_has_handle_lut(kgem);
922
    kgem->has_handle_lut = test_has_handle_lut(kgem);
907
    DBG(("%s: has handle-lut? %d\n", __FUNCTION__,
923
    DBG(("%s: has handle-lut? %d\n", __FUNCTION__,
908
         kgem->has_handle_lut));
924
         kgem->has_handle_lut));
909
 
925
 
910
    kgem->has_semaphores = false;
926
    kgem->has_semaphores = false;
911
    if (kgem->has_blt && test_has_semaphores_enabled(kgem))
927
    if (kgem->has_blt && test_has_semaphores_enabled(kgem))
912
        kgem->has_semaphores = true;
928
        kgem->has_semaphores = true;
913
    DBG(("%s: semaphores enabled? %d\n", __FUNCTION__,
929
    DBG(("%s: semaphores enabled? %d\n", __FUNCTION__,
914
         kgem->has_semaphores));
930
         kgem->has_semaphores));
915
 
931
 
916
    kgem->can_blt_cpu = gen >= 030;
932
    kgem->can_blt_cpu = gen >= 030;
917
    DBG(("%s: can blt to cpu? %d\n", __FUNCTION__,
933
    DBG(("%s: can blt to cpu? %d\n", __FUNCTION__,
918
         kgem->can_blt_cpu));
934
         kgem->can_blt_cpu));
919
 
935
 
920
    kgem->has_secure_batches = test_has_secure_batches(kgem);
936
    kgem->has_secure_batches = test_has_secure_batches(kgem);
921
    DBG(("%s: can use privileged batchbuffers? %d\n", __FUNCTION__,
937
    DBG(("%s: can use privileged batchbuffers? %d\n", __FUNCTION__,
922
         kgem->has_secure_batches));
938
         kgem->has_secure_batches));
923
 
939
 
924
    kgem->has_pinned_batches = test_has_pinned_batches(kgem);
940
    kgem->has_pinned_batches = test_has_pinned_batches(kgem);
925
    DBG(("%s: can use pinned batchbuffers (to avoid CS w/a)? %d\n", __FUNCTION__,
941
    DBG(("%s: can use pinned batchbuffers (to avoid CS w/a)? %d\n", __FUNCTION__,
926
         kgem->has_pinned_batches));
942
         kgem->has_pinned_batches));
927
 
943
 
928
    if (!is_hw_supported(kgem, dev)) {
944
    if (!is_hw_supported(kgem, dev)) {
929
        printf("Detected unsupported/dysfunctional hardware, disabling acceleration.\n");
945
        printf("Detected unsupported/dysfunctional hardware, disabling acceleration.\n");
930
        kgem->wedged = 1;
946
        kgem->wedged = 1;
931
    } else if (__kgem_throttle(kgem)) {
947
    } else if (__kgem_throttle(kgem)) {
932
        printf("Detected a hung GPU, disabling acceleration.\n");
948
        printf("Detected a hung GPU, disabling acceleration.\n");
933
        kgem->wedged = 1;
949
        kgem->wedged = 1;
934
    }
950
    }
935
 
951
 
936
    kgem->batch_size = ARRAY_SIZE(kgem->batch);
952
    kgem->batch_size = ARRAY_SIZE(kgem->batch);
937
    if (gen == 020 && !kgem->has_pinned_batches)
953
    if (gen == 020 && !kgem->has_pinned_batches)
938
        /* Limited to what we can pin */
954
        /* Limited to what we can pin */
939
        kgem->batch_size = 4*1024;
955
        kgem->batch_size = 4*1024;
940
    if (gen == 022)
956
    if (gen == 022)
941
        /* 865g cannot handle a batch spanning multiple pages */
957
        /* 865g cannot handle a batch spanning multiple pages */
942
        kgem->batch_size = PAGE_SIZE / sizeof(uint32_t);
958
        kgem->batch_size = PAGE_SIZE / sizeof(uint32_t);
943
    if ((gen >> 3) == 7)
959
    if ((gen >> 3) == 7)
944
        kgem->batch_size = 16*1024;
960
        kgem->batch_size = 16*1024;
945
    if (!kgem->has_relaxed_delta && kgem->batch_size > 4*1024)
961
    if (!kgem->has_relaxed_delta && kgem->batch_size > 4*1024)
946
        kgem->batch_size = 4*1024;
962
        kgem->batch_size = 4*1024;
947
 
963
 
948
    if (!kgem_init_pinned_batches(kgem) && gen == 020) {
964
    if (!kgem_init_pinned_batches(kgem) && gen == 020) {
949
        printf("Unable to reserve memory for GPU, disabling acceleration.\n");
965
        printf("Unable to reserve memory for GPU, disabling acceleration.\n");
950
        kgem->wedged = 1;
966
        kgem->wedged = 1;
951
    }
967
    }
952
 
968
 
953
    DBG(("%s: maximum batch size? %d\n", __FUNCTION__,
969
    DBG(("%s: maximum batch size? %d\n", __FUNCTION__,
954
         kgem->batch_size));
970
         kgem->batch_size));
955
 
971
 
956
    kgem->min_alignment = 4;
972
    kgem->min_alignment = 4;
957
    if (gen < 040)
973
    if (gen < 040)
958
        kgem->min_alignment = 64;
974
        kgem->min_alignment = 64;
959
 
975
 
960
    kgem->half_cpu_cache_pages = cpu_cache_size() >> 13;
976
    kgem->half_cpu_cache_pages = cpu_cache_size() >> 13;
961
    DBG(("%s: half cpu cache %d pages\n", __FUNCTION__,
977
    DBG(("%s: half cpu cache %d pages\n", __FUNCTION__,
962
         kgem->half_cpu_cache_pages));
978
         kgem->half_cpu_cache_pages));
963
 
979
 
964
    kgem->next_request = __kgem_request_alloc(kgem);
980
    kgem->next_request = __kgem_request_alloc(kgem);
965
 
981
 
966
    DBG(("%s: cpu bo enabled %d: llc? %d, set-cache-level? %d, userptr? %d\n", __FUNCTION__,
982
    DBG(("%s: cpu bo enabled %d: llc? %d, set-cache-level? %d, userptr? %d\n", __FUNCTION__,
967
         !DBG_NO_CPU && (kgem->has_llc | kgem->has_userptr | kgem->has_cacheing),
983
         !DBG_NO_CPU && (kgem->has_llc | kgem->has_userptr | kgem->has_cacheing),
968
         kgem->has_llc, kgem->has_cacheing, kgem->has_userptr));
984
         kgem->has_llc, kgem->has_cacheing, kgem->has_userptr));
969
 
985
 
970
    VG_CLEAR(aperture);
986
    VG_CLEAR(aperture);
971
    aperture.aper_size = 0;
987
    aperture.aper_size = 0;
972
	(void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
988
	(void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
973
    if (aperture.aper_size == 0)
989
    if (aperture.aper_size == 0)
974
        aperture.aper_size = 64*1024*1024;
990
        aperture.aper_size = 64*1024*1024;
975
 
991
 
976
    DBG(("%s: aperture size %lld, available now %lld\n",
992
    DBG(("%s: aperture size %lld, available now %lld\n",
977
         __FUNCTION__,
993
         __FUNCTION__,
978
         (long long)aperture.aper_size,
994
         (long long)aperture.aper_size,
979
         (long long)aperture.aper_available_size));
995
         (long long)aperture.aper_available_size));
980
 
996
 
981
    kgem->aperture_total = aperture.aper_size;
997
    kgem->aperture_total = aperture.aper_size;
982
    kgem->aperture_high = aperture.aper_size * 3/4;
998
    kgem->aperture_high = aperture.aper_size * 3/4;
983
    kgem->aperture_low = aperture.aper_size * 1/3;
999
    kgem->aperture_low = aperture.aper_size * 1/3;
984
    if (gen < 033) {
1000
    if (gen < 033) {
985
        /* Severe alignment penalties */
1001
        /* Severe alignment penalties */
986
        kgem->aperture_high /= 2;
1002
        kgem->aperture_high /= 2;
987
        kgem->aperture_low /= 2;
1003
        kgem->aperture_low /= 2;
988
    }
1004
    }
989
    DBG(("%s: aperture low=%d [%d], high=%d [%d]\n", __FUNCTION__,
1005
    DBG(("%s: aperture low=%d [%d], high=%d [%d]\n", __FUNCTION__,
990
         kgem->aperture_low, kgem->aperture_low / (1024*1024),
1006
         kgem->aperture_low, kgem->aperture_low / (1024*1024),
991
         kgem->aperture_high, kgem->aperture_high / (1024*1024)));
1007
         kgem->aperture_high, kgem->aperture_high / (1024*1024)));
992
 
1008
 
993
    kgem->aperture_mappable = agp_aperture_size(dev, gen);
1009
    kgem->aperture_mappable = agp_aperture_size(dev, gen);
994
    if (kgem->aperture_mappable == 0 ||
1010
    if (kgem->aperture_mappable == 0 ||
995
        kgem->aperture_mappable > aperture.aper_size)
1011
        kgem->aperture_mappable > aperture.aper_size)
996
        kgem->aperture_mappable = aperture.aper_size;
1012
        kgem->aperture_mappable = aperture.aper_size;
997
    DBG(("%s: aperture mappable=%d [%d MiB]\n", __FUNCTION__,
1013
    DBG(("%s: aperture mappable=%d [%d MiB]\n", __FUNCTION__,
998
         kgem->aperture_mappable, kgem->aperture_mappable / (1024*1024)));
1014
         kgem->aperture_mappable, kgem->aperture_mappable / (1024*1024)));
999
 
1015
 
1000
    kgem->buffer_size = 64 * 1024;
1016
    kgem->buffer_size = 64 * 1024;
1001
    while (kgem->buffer_size < kgem->aperture_mappable >> 10)
1017
    while (kgem->buffer_size < kgem->aperture_mappable >> 10)
1002
        kgem->buffer_size *= 2;
1018
        kgem->buffer_size *= 2;
1003
    if (kgem->buffer_size >> 12 > kgem->half_cpu_cache_pages)
1019
    if (kgem->buffer_size >> 12 > kgem->half_cpu_cache_pages)
1004
        kgem->buffer_size = kgem->half_cpu_cache_pages << 12;
1020
        kgem->buffer_size = kgem->half_cpu_cache_pages << 12;
1005
    DBG(("%s: buffer size=%d [%d KiB]\n", __FUNCTION__,
1021
    DBG(("%s: buffer size=%d [%d KiB]\n", __FUNCTION__,
1006
         kgem->buffer_size, kgem->buffer_size / 1024));
1022
         kgem->buffer_size, kgem->buffer_size / 1024));
1007
 
1023
 
1008
    kgem->max_object_size = 3 * (kgem->aperture_high >> 12) << 10;
1024
    kgem->max_object_size = 3 * (kgem->aperture_high >> 12) << 10;
1009
    kgem->max_gpu_size = kgem->max_object_size;
1025
    kgem->max_gpu_size = kgem->max_object_size;
1010
    if (!kgem->has_llc)
1026
    if (!kgem->has_llc)
1011
        kgem->max_gpu_size = MAX_CACHE_SIZE;
1027
        kgem->max_gpu_size = MAX_CACHE_SIZE;
1012
 
1028
 
1013
    totalram = total_ram_size();
1029
    totalram = total_ram_size();
1014
    if (totalram == 0) {
1030
    if (totalram == 0) {
1015
        DBG(("%s: total ram size unknown, assuming maximum of total aperture\n",
1031
        DBG(("%s: total ram size unknown, assuming maximum of total aperture\n",
1016
             __FUNCTION__));
1032
             __FUNCTION__));
1017
        totalram = kgem->aperture_total;
1033
        totalram = kgem->aperture_total;
1018
    }
1034
    }
1019
    DBG(("%s: total ram=%u\n", __FUNCTION__, totalram));
1035
    DBG(("%s: total ram=%u\n", __FUNCTION__, totalram));
1020
    if (kgem->max_object_size > totalram / 2)
1036
    if (kgem->max_object_size > totalram / 2)
1021
        kgem->max_object_size = totalram / 2;
1037
        kgem->max_object_size = totalram / 2;
1022
    if (kgem->max_gpu_size > totalram / 4)
1038
    if (kgem->max_gpu_size > totalram / 4)
1023
        kgem->max_gpu_size = totalram / 4;
1039
        kgem->max_gpu_size = totalram / 4;
1024
 
1040
 
1025
    kgem->max_cpu_size = kgem->max_object_size;
1041
    kgem->max_cpu_size = kgem->max_object_size;
1026
 
1042
 
1027
    half_gpu_max = kgem->max_gpu_size / 2;
1043
    half_gpu_max = kgem->max_gpu_size / 2;
1028
    kgem->max_copy_tile_size = (MAX_CACHE_SIZE + 1)/2;
1044
    kgem->max_copy_tile_size = (MAX_CACHE_SIZE + 1)/2;
1029
    if (kgem->max_copy_tile_size > half_gpu_max)
1045
    if (kgem->max_copy_tile_size > half_gpu_max)
1030
        kgem->max_copy_tile_size = half_gpu_max;
1046
        kgem->max_copy_tile_size = half_gpu_max;
1031
 
1047
 
1032
    if (kgem->has_llc)
1048
    if (kgem->has_llc)
1033
        kgem->max_upload_tile_size = kgem->max_copy_tile_size;
1049
        kgem->max_upload_tile_size = kgem->max_copy_tile_size;
1034
    else
1050
    else
1035
        kgem->max_upload_tile_size = kgem->aperture_mappable / 4;
1051
        kgem->max_upload_tile_size = kgem->aperture_mappable / 4;
1036
    if (kgem->max_upload_tile_size > half_gpu_max)
1052
    if (kgem->max_upload_tile_size > half_gpu_max)
1037
        kgem->max_upload_tile_size = half_gpu_max;
1053
        kgem->max_upload_tile_size = half_gpu_max;
1038
 
1054
 
1039
    kgem->large_object_size = MAX_CACHE_SIZE;
1055
    kgem->large_object_size = MAX_CACHE_SIZE;
1040
    if (kgem->large_object_size > kgem->max_gpu_size)
1056
    if (kgem->large_object_size > kgem->max_gpu_size)
1041
        kgem->large_object_size = kgem->max_gpu_size;
1057
        kgem->large_object_size = kgem->max_gpu_size;
1042
 
1058
 
1043
    if (kgem->has_llc | kgem->has_cacheing | kgem->has_userptr) {
1059
    if (kgem->has_llc | kgem->has_cacheing | kgem->has_userptr) {
1044
        if (kgem->large_object_size > kgem->max_cpu_size)
1060
        if (kgem->large_object_size > kgem->max_cpu_size)
1045
            kgem->large_object_size = kgem->max_cpu_size;
1061
            kgem->large_object_size = kgem->max_cpu_size;
1046
    } else
1062
    } else
1047
        kgem->max_cpu_size = 0;
1063
        kgem->max_cpu_size = 0;
1048
    if (DBG_NO_CPU)
1064
    if (DBG_NO_CPU)
1049
        kgem->max_cpu_size = 0;
1065
        kgem->max_cpu_size = 0;
1050
 
1066
 
1051
    DBG(("%s: maximum object size=%d\n",
1067
    DBG(("%s: maximum object size=%d\n",
1052
         __FUNCTION__, kgem->max_object_size));
1068
         __FUNCTION__, kgem->max_object_size));
1053
    DBG(("%s: large object thresold=%d\n",
1069
    DBG(("%s: large object thresold=%d\n",
1054
         __FUNCTION__, kgem->large_object_size));
1070
         __FUNCTION__, kgem->large_object_size));
1055
    DBG(("%s: max object sizes (gpu=%d, cpu=%d, tile upload=%d, copy=%d)\n",
1071
    DBG(("%s: max object sizes (gpu=%d, cpu=%d, tile upload=%d, copy=%d)\n",
1056
         __FUNCTION__,
1072
         __FUNCTION__,
1057
         kgem->max_gpu_size, kgem->max_cpu_size,
1073
         kgem->max_gpu_size, kgem->max_cpu_size,
1058
         kgem->max_upload_tile_size, kgem->max_copy_tile_size));
1074
         kgem->max_upload_tile_size, kgem->max_copy_tile_size));
1059
 
1075
 
1060
    /* Convert the aperture thresholds to pages */
1076
    /* Convert the aperture thresholds to pages */
1061
    kgem->aperture_low /= PAGE_SIZE;
1077
    kgem->aperture_low /= PAGE_SIZE;
1062
    kgem->aperture_high /= PAGE_SIZE;
1078
    kgem->aperture_high /= PAGE_SIZE;
1063
 
1079
 
1064
    kgem->fence_max = gem_param(kgem, I915_PARAM_NUM_FENCES_AVAIL) - 2;
1080
    kgem->fence_max = gem_param(kgem, I915_PARAM_NUM_FENCES_AVAIL) - 2;
1065
    if ((int)kgem->fence_max < 0)
1081
    if ((int)kgem->fence_max < 0)
1066
        kgem->fence_max = 5; /* minimum safe value for all hw */
1082
        kgem->fence_max = 5; /* minimum safe value for all hw */
1067
    DBG(("%s: max fences=%d\n", __FUNCTION__, kgem->fence_max));
1083
    DBG(("%s: max fences=%d\n", __FUNCTION__, kgem->fence_max));
1068
 
1084
 
1069
    kgem->batch_flags_base = 0;
1085
    kgem->batch_flags_base = 0;
1070
    if (kgem->has_no_reloc)
1086
    if (kgem->has_no_reloc)
1071
        kgem->batch_flags_base |= LOCAL_I915_EXEC_NO_RELOC;
1087
        kgem->batch_flags_base |= LOCAL_I915_EXEC_NO_RELOC;
1072
    if (kgem->has_handle_lut)
1088
    if (kgem->has_handle_lut)
1073
        kgem->batch_flags_base |= LOCAL_I915_EXEC_HANDLE_LUT;
1089
        kgem->batch_flags_base |= LOCAL_I915_EXEC_HANDLE_LUT;
1074
    if (kgem->has_pinned_batches)
1090
    if (kgem->has_pinned_batches)
1075
        kgem->batch_flags_base |= LOCAL_I915_EXEC_IS_PINNED;
1091
        kgem->batch_flags_base |= LOCAL_I915_EXEC_IS_PINNED;
-
 
1092
}
-
 
1093
 
-
 
1094
/* XXX hopefully a good approximation */
-
 
1095
static uint32_t kgem_get_unique_id(struct kgem *kgem)
-
 
1096
{
-
 
1097
	uint32_t id;
-
 
1098
	id = ++kgem->unique_id;
-
 
1099
	if (id == 0)
-
 
1100
		id = ++kgem->unique_id;
-
 
1101
	return id;
-
 
1102
}
-
 
1103
 
-
 
1104
inline static uint32_t kgem_pitch_alignment(struct kgem *kgem, unsigned flags)
-
 
1105
{
-
 
1106
	if (flags & CREATE_PRIME)
-
 
1107
		return 256;
-
 
1108
	if (flags & CREATE_SCANOUT)
-
 
1109
		return 64;
-
 
1110
	return kgem->min_alignment;
-
 
1111
}
-
 
1112
 
-
 
1113
static uint32_t kgem_untiled_pitch(struct kgem *kgem,
-
 
1114
				   uint32_t width, uint32_t bpp,
-
 
1115
				   unsigned flags)
-
 
1116
{
-
 
1117
	width = ALIGN(width, 2) * bpp >> 3;
-
 
1118
	return ALIGN(width, kgem_pitch_alignment(kgem, flags));
-
 
1119
}
-
 
1120
static uint32_t kgem_surface_size(struct kgem *kgem,
-
 
1121
				  bool relaxed_fencing,
-
 
1122
				  unsigned flags,
-
 
1123
				  uint32_t width,
-
 
1124
				  uint32_t height,
-
 
1125
				  uint32_t bpp,
-
 
1126
				  uint32_t tiling,
-
 
1127
				  uint32_t *pitch)
-
 
1128
{
-
 
1129
	uint32_t tile_width, tile_height;
-
 
1130
	uint32_t size;
-
 
1131
 
-
 
1132
	assert(width <= MAXSHORT);
-
 
1133
	assert(height <= MAXSHORT);
-
 
1134
 
-
 
1135
	if (kgem->gen <= 030) {
-
 
1136
		if (tiling) {
-
 
1137
			if (kgem->gen < 030) {
-
 
1138
				tile_width = 128;
-
 
1139
				tile_height = 32;
-
 
1140
			} else {
-
 
1141
				tile_width = 512;
-
 
1142
				tile_height = 16;
-
 
1143
			}
-
 
1144
		} else {
-
 
1145
			tile_width = 2 * bpp >> 3;
-
 
1146
			tile_width = ALIGN(tile_width,
-
 
1147
					   kgem_pitch_alignment(kgem, flags));
-
 
1148
			tile_height = 2;
-
 
1149
		}
-
 
1150
	} else switch (tiling) {
-
 
1151
	default:
-
 
1152
	case I915_TILING_NONE:
-
 
1153
		tile_width = 2 * bpp >> 3;
-
 
1154
		tile_width = ALIGN(tile_width,
-
 
1155
				   kgem_pitch_alignment(kgem, flags));
-
 
1156
		tile_height = 2;
-
 
1157
		break;
-
 
1158
 
-
 
1159
		/* XXX align to an even tile row */
-
 
1160
	case I915_TILING_X:
-
 
1161
		tile_width = 512;
-
 
1162
		tile_height = 16;
-
 
1163
		break;
-
 
1164
	case I915_TILING_Y:
-
 
1165
		tile_width = 128;
-
 
1166
		tile_height = 64;
-
 
1167
		break;
-
 
1168
	}
-
 
1169
 
-
 
1170
	*pitch = ALIGN(width * bpp / 8, tile_width);
-
 
1171
	height = ALIGN(height, tile_height);
-
 
1172
	if (kgem->gen >= 040)
-
 
1173
		return PAGE_ALIGN(*pitch * height);
-
 
1174
 
-
 
1175
	/* If it is too wide for the blitter, don't even bother.  */
-
 
1176
	if (tiling != I915_TILING_NONE) {
-
 
1177
		if (*pitch > 8192)
-
 
1178
			return 0;
-
 
1179
 
-
 
1180
		for (size = tile_width; size < *pitch; size <<= 1)
-
 
1181
			;
-
 
1182
		*pitch = size;
-
 
1183
	} else {
-
 
1184
		if (*pitch >= 32768)
-
 
1185
			return 0;
-
 
1186
	}
-
 
1187
 
-
 
1188
	size = *pitch * height;
-
 
1189
	if (relaxed_fencing || tiling == I915_TILING_NONE)
-
 
1190
		return PAGE_ALIGN(size);
-
 
1191
 
-
 
1192
	/*  We need to allocate a pot fence region for a tiled buffer. */
-
 
1193
	if (kgem->gen < 030)
-
 
1194
		tile_width = 512 * 1024;
-
 
1195
	else
-
 
1196
		tile_width = 1024 * 1024;
-
 
1197
	while (tile_width < size)
-
 
1198
		tile_width *= 2;
-
 
1199
	return tile_width;
-
 
1200
}
-
 
1201
 
-
 
1202
static uint32_t kgem_aligned_height(struct kgem *kgem,
-
 
1203
				    uint32_t height, uint32_t tiling)
-
 
1204
{
-
 
1205
	uint32_t tile_height;
-
 
1206
 
-
 
1207
	if (kgem->gen <= 030) {
-
 
1208
		tile_height = tiling ? kgem->gen < 030 ? 32 : 16 : 1;
-
 
1209
	} else switch (tiling) {
-
 
1210
		/* XXX align to an even tile row */
-
 
1211
	default:
-
 
1212
	case I915_TILING_NONE:
-
 
1213
		tile_height = 1;
-
 
1214
		break;
-
 
1215
	case I915_TILING_X:
-
 
1216
		tile_height = 16;
-
 
1217
		break;
-
 
1218
	case I915_TILING_Y:
-
 
1219
		tile_height = 64;
-
 
1220
		break;
-
 
1221
	}
-
 
1222
 
1076
 
1223
	return ALIGN(height, tile_height);
1077
}
1224
}
1078
 
1225
 
1079
static struct drm_i915_gem_exec_object2 *
1226
static struct drm_i915_gem_exec_object2 *
1080
kgem_add_handle(struct kgem *kgem, struct kgem_bo *bo)
1227
kgem_add_handle(struct kgem *kgem, struct kgem_bo *bo)
1081
{
1228
{
1082
	struct drm_i915_gem_exec_object2 *exec;
1229
	struct drm_i915_gem_exec_object2 *exec;
1083
 
1230
 
1084
	DBG(("%s: handle=%d, index=%d\n",
1231
	DBG(("%s: handle=%d, index=%d\n",
1085
	     __FUNCTION__, bo->handle, kgem->nexec));
1232
	     __FUNCTION__, bo->handle, kgem->nexec));
1086
 
1233
 
1087
	assert(kgem->nexec < ARRAY_SIZE(kgem->exec));
1234
	assert(kgem->nexec < ARRAY_SIZE(kgem->exec));
1088
	bo->target_handle = kgem->has_handle_lut ? kgem->nexec : bo->handle;
1235
	bo->target_handle = kgem->has_handle_lut ? kgem->nexec : bo->handle;
1089
	exec = memset(&kgem->exec[kgem->nexec++], 0, sizeof(*exec));
1236
	exec = memset(&kgem->exec[kgem->nexec++], 0, sizeof(*exec));
1090
	exec->handle = bo->handle;
1237
	exec->handle = bo->handle;
1091
	exec->offset = bo->presumed_offset;
1238
	exec->offset = bo->presumed_offset;
1092
 
1239
 
1093
	kgem->aperture += num_pages(bo);
1240
	kgem->aperture += num_pages(bo);
1094
 
1241
 
1095
	return exec;
1242
	return exec;
1096
}
1243
}
1097
 
1244
 
1098
static void kgem_add_bo(struct kgem *kgem, struct kgem_bo *bo)
1245
static void kgem_add_bo(struct kgem *kgem, struct kgem_bo *bo)
1099
{
1246
{
1100
	bo->exec = kgem_add_handle(kgem, bo);
1247
	bo->exec = kgem_add_handle(kgem, bo);
1101
	bo->rq = MAKE_REQUEST(kgem->next_request, kgem->ring);
1248
	bo->rq = MAKE_REQUEST(kgem->next_request, kgem->ring);
1102
 
1249
 
1103
	list_move_tail(&bo->request, &kgem->next_request->buffers);
1250
	list_move_tail(&bo->request, &kgem->next_request->buffers);
1104
 
1251
 
1105
	/* XXX is it worth working around gcc here? */
1252
	/* XXX is it worth working around gcc here? */
1106
	kgem->flush |= bo->flush;
1253
	kgem->flush |= bo->flush;
1107
}
1254
}
1108
 
1255
 
1109
static uint32_t kgem_end_batch(struct kgem *kgem)
1256
static uint32_t kgem_end_batch(struct kgem *kgem)
1110
{
1257
{
1111
	kgem->batch[kgem->nbatch++] = MI_BATCH_BUFFER_END;
1258
	kgem->batch[kgem->nbatch++] = MI_BATCH_BUFFER_END;
1112
	if (kgem->nbatch & 1)
1259
	if (kgem->nbatch & 1)
1113
		kgem->batch[kgem->nbatch++] = MI_NOOP;
1260
		kgem->batch[kgem->nbatch++] = MI_NOOP;
1114
 
1261
 
1115
	return kgem->nbatch;
1262
	return kgem->nbatch;
1116
}
1263
}
1117
 
1264
 
1118
static void kgem_fixup_self_relocs(struct kgem *kgem, struct kgem_bo *bo)
1265
static void kgem_fixup_self_relocs(struct kgem *kgem, struct kgem_bo *bo)
1119
{
1266
{
1120
	int n;
1267
	int n;
1121
 
1268
 
1122
	if (kgem->nreloc__self == 0)
1269
	if (kgem->nreloc__self == 0)
1123
		return;
1270
		return;
1124
 
1271
 
1125
	for (n = 0; n < kgem->nreloc__self; n++) {
1272
	for (n = 0; n < kgem->nreloc__self; n++) {
1126
		int i = kgem->reloc__self[n];
1273
		int i = kgem->reloc__self[n];
1127
		assert(kgem->reloc[i].target_handle == ~0U);
1274
		assert(kgem->reloc[i].target_handle == ~0U);
1128
		kgem->reloc[i].target_handle = bo->target_handle;
1275
		kgem->reloc[i].target_handle = bo->target_handle;
1129
		kgem->reloc[i].presumed_offset = bo->presumed_offset;
1276
		kgem->reloc[i].presumed_offset = bo->presumed_offset;
1130
		kgem->batch[kgem->reloc[i].offset/sizeof(kgem->batch[0])] =
1277
		kgem->batch[kgem->reloc[i].offset/sizeof(kgem->batch[0])] =
1131
			kgem->reloc[i].delta + bo->presumed_offset;
1278
			kgem->reloc[i].delta + bo->presumed_offset;
1132
	}
1279
	}
1133
 
1280
 
1134
	if (n == 256) {
1281
	if (n == 256) {
1135
		for (n = kgem->reloc__self[255]; n < kgem->nreloc; n++) {
1282
		for (n = kgem->reloc__self[255]; n < kgem->nreloc; n++) {
1136
			if (kgem->reloc[n].target_handle == ~0U) {
1283
			if (kgem->reloc[n].target_handle == ~0U) {
1137
				kgem->reloc[n].target_handle = bo->target_handle;
1284
				kgem->reloc[n].target_handle = bo->target_handle;
1138
				kgem->reloc[n].presumed_offset = bo->presumed_offset;
1285
				kgem->reloc[n].presumed_offset = bo->presumed_offset;
1139
				kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] =
1286
				kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] =
1140
					kgem->reloc[n].delta + bo->presumed_offset;
1287
					kgem->reloc[n].delta + bo->presumed_offset;
1141
			}
1288
			}
1142
		}
1289
		}
1143
 
1290
 
1144
	}
1291
	}
1145
 
1292
 
1146
}
1293
}
1147
 
1294
 
1148
static void kgem_bo_binding_free(struct kgem *kgem, struct kgem_bo *bo)
1295
static void kgem_bo_binding_free(struct kgem *kgem, struct kgem_bo *bo)
1149
{
1296
{
1150
	struct kgem_bo_binding *b;
1297
	struct kgem_bo_binding *b;
1151
 
1298
 
1152
	b = bo->binding.next;
1299
	b = bo->binding.next;
1153
	while (b) {
1300
	while (b) {
1154
		struct kgem_bo_binding *next = b->next;
1301
		struct kgem_bo_binding *next = b->next;
1155
		free (b);
1302
		free (b);
1156
		b = next;
1303
		b = next;
1157
	}
1304
	}
1158
}
1305
}
1159
 
1306
 
1160
static void kgem_bo_release_map(struct kgem *kgem, struct kgem_bo *bo)
1307
static void kgem_bo_release_map(struct kgem *kgem, struct kgem_bo *bo)
1161
{
1308
{
1162
	int type = IS_CPU_MAP(bo->map);
1309
	int type = IS_CPU_MAP(bo->map);
1163
 
1310
 
1164
	assert(!IS_USER_MAP(bo->map));
1311
	assert(!IS_USER_MAP(bo->map));
1165
 
1312
 
1166
	DBG(("%s: releasing %s vma for handle=%d, count=%d\n",
1313
	DBG(("%s: releasing %s vma for handle=%d, count=%d\n",
1167
	     __FUNCTION__, type ? "CPU" : "GTT",
1314
	     __FUNCTION__, type ? "CPU" : "GTT",
1168
	     bo->handle, kgem->vma[type].count));
1315
	     bo->handle, kgem->vma[type].count));
1169
 
1316
 
1170
	VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo)));
1317
	VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo)));
1171
//	munmap(MAP(bo->map), bytes(bo));
1318
//	munmap(MAP(bo->map), bytes(bo));
1172
	bo->map = NULL;
1319
	bo->map = NULL;
1173
 
1320
 
1174
	if (!list_is_empty(&bo->vma)) {
1321
	if (!list_is_empty(&bo->vma)) {
1175
		list_del(&bo->vma);
1322
		list_del(&bo->vma);
1176
		kgem->vma[type].count--;
1323
		kgem->vma[type].count--;
1177
	}
1324
	}
1178
}
1325
}
1179
 
1326
 
1180
static void kgem_bo_free(struct kgem *kgem, struct kgem_bo *bo)
1327
static void kgem_bo_free(struct kgem *kgem, struct kgem_bo *bo)
1181
{
1328
{
1182
	DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle));
1329
	DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle));
1183
	assert(bo->refcnt == 0);
1330
	assert(bo->refcnt == 0);
1184
	assert(bo->exec == NULL);
1331
	assert(bo->exec == NULL);
1185
	assert(!bo->snoop || bo->rq == NULL);
1332
	assert(!bo->snoop || bo->rq == NULL);
1186
 
1333
 
1187
#ifdef DEBUG_MEMORY
1334
#ifdef DEBUG_MEMORY
1188
	kgem->debug_memory.bo_allocs--;
1335
	kgem->debug_memory.bo_allocs--;
1189
	kgem->debug_memory.bo_bytes -= bytes(bo);
1336
	kgem->debug_memory.bo_bytes -= bytes(bo);
1190
#endif
1337
#endif
1191
 
1338
 
1192
	kgem_bo_binding_free(kgem, bo);
1339
	kgem_bo_binding_free(kgem, bo);
1193
 
1340
 
1194
	if (IS_USER_MAP(bo->map)) {
1341
	if (IS_USER_MAP(bo->map)) {
1195
		assert(bo->rq == NULL);
1342
		assert(bo->rq == NULL);
1196
		assert(MAP(bo->map) != bo || bo->io);
1343
		assert(MAP(bo->map) != bo || bo->io);
1197
		if (bo != MAP(bo->map)) {
1344
		if (bo != MAP(bo->map)) {
1198
			DBG(("%s: freeing snooped base\n", __FUNCTION__));
1345
			DBG(("%s: freeing snooped base\n", __FUNCTION__));
1199
			free(MAP(bo->map));
1346
			free(MAP(bo->map));
1200
		}
1347
		}
1201
		bo->map = NULL;
1348
		bo->map = NULL;
1202
	}
1349
	}
1203
	if (bo->map)
1350
	if (bo->map)
1204
		kgem_bo_release_map(kgem, bo);
1351
		kgem_bo_release_map(kgem, bo);
1205
	assert(list_is_empty(&bo->vma));
1352
	assert(list_is_empty(&bo->vma));
1206
 
1353
 
1207
	_list_del(&bo->list);
1354
	_list_del(&bo->list);
1208
	_list_del(&bo->request);
1355
	_list_del(&bo->request);
1209
	gem_close(kgem->fd, bo->handle);
1356
	gem_close(kgem->fd, bo->handle);
1210
 
1357
 
1211
	if (!bo->io) {
1358
	if (!bo->io) {
1212
		*(struct kgem_bo **)bo = __kgem_freed_bo;
1359
		*(struct kgem_bo **)bo = __kgem_freed_bo;
1213
		__kgem_freed_bo = bo;
1360
		__kgem_freed_bo = bo;
1214
	} else
1361
	} else
1215
		free(bo);
1362
		free(bo);
1216
}
1363
}
1217
 
1364
 
1218
inline static void kgem_bo_move_to_inactive(struct kgem *kgem,
1365
inline static void kgem_bo_move_to_inactive(struct kgem *kgem,
1219
					    struct kgem_bo *bo)
1366
					    struct kgem_bo *bo)
1220
{
1367
{
1221
	DBG(("%s: moving handle=%d to inactive\n", __FUNCTION__, bo->handle));
1368
	DBG(("%s: moving handle=%d to inactive\n", __FUNCTION__, bo->handle));
1222
 
1369
 
1223
	assert(bo->refcnt == 0);
1370
	assert(bo->refcnt == 0);
1224
	assert(bo->reusable);
1371
	assert(bo->reusable);
1225
	assert(bo->rq == NULL);
1372
	assert(bo->rq == NULL);
1226
	assert(bo->exec == NULL);
1373
	assert(bo->exec == NULL);
1227
	assert(bo->domain != DOMAIN_GPU);
1374
	assert(bo->domain != DOMAIN_GPU);
1228
	assert(!bo->proxy);
1375
	assert(!bo->proxy);
1229
	assert(!bo->io);
1376
	assert(!bo->io);
1230
	assert(!bo->scanout);
1377
	assert(!bo->scanout);
1231
	assert(!bo->needs_flush);
1378
	assert(!bo->needs_flush);
1232
	assert(list_is_empty(&bo->vma));
1379
	assert(list_is_empty(&bo->vma));
1233
	ASSERT_IDLE(kgem, bo->handle);
1380
	ASSERT_IDLE(kgem, bo->handle);
1234
 
1381
 
1235
	kgem->need_expire = true;
1382
	kgem->need_expire = true;
1236
 
1383
 
1237
	if (bucket(bo) >= NUM_CACHE_BUCKETS) {
1384
	if (bucket(bo) >= NUM_CACHE_BUCKETS) {
1238
		list_move(&bo->list, &kgem->large_inactive);
1385
		list_move(&bo->list, &kgem->large_inactive);
1239
		return;
1386
		return;
1240
	}
1387
	}
1241
 
1388
 
1242
	assert(bo->flush == false);
1389
	assert(bo->flush == false);
1243
	list_move(&bo->list, &kgem->inactive[bucket(bo)]);
1390
	list_move(&bo->list, &kgem->inactive[bucket(bo)]);
1244
	if (bo->map) {
1391
	if (bo->map) {
1245
		int type = IS_CPU_MAP(bo->map);
1392
		int type = IS_CPU_MAP(bo->map);
1246
		if (bucket(bo) >= NUM_CACHE_BUCKETS ||
1393
		if (bucket(bo) >= NUM_CACHE_BUCKETS ||
1247
		    (!type && !__kgem_bo_is_mappable(kgem, bo))) {
1394
		    (!type && !__kgem_bo_is_mappable(kgem, bo))) {
1248
//			munmap(MAP(bo->map), bytes(bo));
1395
//			munmap(MAP(bo->map), bytes(bo));
1249
			bo->map = NULL;
1396
			bo->map = NULL;
1250
		}
1397
		}
1251
		if (bo->map) {
1398
		if (bo->map) {
1252
			list_add(&bo->vma, &kgem->vma[type].inactive[bucket(bo)]);
1399
			list_add(&bo->vma, &kgem->vma[type].inactive[bucket(bo)]);
1253
			kgem->vma[type].count++;
1400
			kgem->vma[type].count++;
1254
		}
1401
		}
1255
	}
1402
	}
1256
}
1403
}
1257
 
1404
 
1258
static struct kgem_bo *kgem_bo_replace_io(struct kgem_bo *bo)
1405
static struct kgem_bo *kgem_bo_replace_io(struct kgem_bo *bo)
1259
{
1406
{
1260
	struct kgem_bo *base;
1407
	struct kgem_bo *base;
1261
 
1408
 
1262
	if (!bo->io)
1409
	if (!bo->io)
1263
		return bo;
1410
		return bo;
1264
 
1411
 
1265
	assert(!bo->snoop);
1412
	assert(!bo->snoop);
1266
	base = malloc(sizeof(*base));
1413
	base = malloc(sizeof(*base));
1267
	if (base) {
1414
	if (base) {
1268
		DBG(("%s: transferring io handle=%d to bo\n",
1415
		DBG(("%s: transferring io handle=%d to bo\n",
1269
		     __FUNCTION__, bo->handle));
1416
		     __FUNCTION__, bo->handle));
1270
		/* transfer the handle to a minimum bo */
1417
		/* transfer the handle to a minimum bo */
1271
		memcpy(base, bo, sizeof(*base));
1418
		memcpy(base, bo, sizeof(*base));
1272
		base->io = false;
1419
		base->io = false;
1273
		list_init(&base->list);
1420
		list_init(&base->list);
1274
		list_replace(&bo->request, &base->request);
1421
		list_replace(&bo->request, &base->request);
1275
		list_replace(&bo->vma, &base->vma);
1422
		list_replace(&bo->vma, &base->vma);
1276
		free(bo);
1423
		free(bo);
1277
		bo = base;
1424
		bo = base;
1278
	} else
1425
	} else
1279
		bo->reusable = false;
1426
		bo->reusable = false;
1280
 
1427
 
1281
	return bo;
1428
	return bo;
1282
}
1429
}
1283
 
1430
 
1284
inline static void kgem_bo_remove_from_inactive(struct kgem *kgem,
1431
inline static void kgem_bo_remove_from_inactive(struct kgem *kgem,
1285
						struct kgem_bo *bo)
1432
						struct kgem_bo *bo)
1286
{
1433
{
1287
	DBG(("%s: removing handle=%d from inactive\n", __FUNCTION__, bo->handle));
1434
	DBG(("%s: removing handle=%d from inactive\n", __FUNCTION__, bo->handle));
1288
 
1435
 
1289
	list_del(&bo->list);
1436
	list_del(&bo->list);
1290
	assert(bo->rq == NULL);
1437
	assert(bo->rq == NULL);
1291
	assert(bo->exec == NULL);
1438
	assert(bo->exec == NULL);
1292
	if (bo->map) {
1439
	if (bo->map) {
1293
		assert(!list_is_empty(&bo->vma));
1440
		assert(!list_is_empty(&bo->vma));
1294
		list_del(&bo->vma);
1441
		list_del(&bo->vma);
1295
		kgem->vma[IS_CPU_MAP(bo->map)].count--;
1442
		kgem->vma[IS_CPU_MAP(bo->map)].count--;
1296
	}
1443
	}
1297
}
1444
}
1298
 
1445
 
1299
inline static void kgem_bo_remove_from_active(struct kgem *kgem,
1446
inline static void kgem_bo_remove_from_active(struct kgem *kgem,
1300
					      struct kgem_bo *bo)
1447
					      struct kgem_bo *bo)
1301
{
1448
{
1302
	DBG(("%s: removing handle=%d from active\n", __FUNCTION__, bo->handle));
1449
	DBG(("%s: removing handle=%d from active\n", __FUNCTION__, bo->handle));
1303
 
1450
 
1304
	list_del(&bo->list);
1451
	list_del(&bo->list);
1305
	assert(bo->rq != NULL);
1452
	assert(bo->rq != NULL);
1306
	if (bo->rq == (void *)kgem)
1453
	if (bo->rq == (void *)kgem)
1307
		list_del(&bo->request);
1454
		list_del(&bo->request);
1308
	assert(list_is_empty(&bo->vma));
1455
	assert(list_is_empty(&bo->vma));
1309
}
1456
}
1310
 
1457
 
1311
static void kgem_bo_clear_scanout(struct kgem *kgem, struct kgem_bo *bo)
1458
static void kgem_bo_clear_scanout(struct kgem *kgem, struct kgem_bo *bo)
1312
{
1459
{
1313
	assert(bo->scanout);
1460
	assert(bo->scanout);
1314
	assert(!bo->refcnt);
1461
	assert(!bo->refcnt);
1315
	assert(bo->exec == NULL);
1462
	assert(bo->exec == NULL);
1316
	assert(bo->proxy == NULL);
1463
	assert(bo->proxy == NULL);
1317
 
1464
 
1318
	DBG(("%s: handle=%d, fb=%d (reusable=%d)\n",
1465
	DBG(("%s: handle=%d, fb=%d (reusable=%d)\n",
1319
	     __FUNCTION__, bo->handle, bo->delta, bo->reusable));
1466
	     __FUNCTION__, bo->handle, bo->delta, bo->reusable));
1320
	if (bo->delta) {
1467
	if (bo->delta) {
1321
		/* XXX will leak if we are not DRM_MASTER. *shrug* */
1468
		/* XXX will leak if we are not DRM_MASTER. *shrug* */
1322
//		drmModeRmFB(kgem->fd, bo->delta);
1469
//		drmModeRmFB(kgem->fd, bo->delta);
1323
		bo->delta = 0;
1470
		bo->delta = 0;
1324
	}
1471
	}
1325
 
1472
 
1326
	bo->scanout = false;
1473
	bo->scanout = false;
1327
	bo->flush = false;
1474
	bo->flush = false;
1328
	bo->reusable = true;
1475
	bo->reusable = true;
1329
 
1476
 
1330
	if (kgem->has_llc &&
1477
	if (kgem->has_llc &&
1331
	    !gem_set_cacheing(kgem->fd, bo->handle, SNOOPED))
1478
	    !gem_set_cacheing(kgem->fd, bo->handle, SNOOPED))
1332
		bo->reusable = false;
1479
		bo->reusable = false;
1333
}
1480
}
1334
 
1481
 
1335
static void _kgem_bo_delete_buffer(struct kgem *kgem, struct kgem_bo *bo)
1482
static void _kgem_bo_delete_buffer(struct kgem *kgem, struct kgem_bo *bo)
1336
{
1483
{
1337
	struct kgem_buffer *io = (struct kgem_buffer *)bo->proxy;
1484
	struct kgem_buffer *io = (struct kgem_buffer *)bo->proxy;
1338
 
1485
 
1339
	DBG(("%s: size=%d, offset=%d, parent used=%d\n",
1486
	DBG(("%s: size=%d, offset=%d, parent used=%d\n",
1340
	     __FUNCTION__, bo->size.bytes, bo->delta, io->used));
1487
	     __FUNCTION__, bo->size.bytes, bo->delta, io->used));
1341
 
1488
 
1342
	if (ALIGN(bo->delta + bo->size.bytes, UPLOAD_ALIGNMENT) == io->used)
1489
	if (ALIGN(bo->delta + bo->size.bytes, UPLOAD_ALIGNMENT) == io->used)
1343
		io->used = bo->delta;
1490
		io->used = bo->delta;
1344
}
1491
}
1345
 
1492
 
1346
static void kgem_bo_move_to_scanout(struct kgem *kgem, struct kgem_bo *bo)
1493
static void kgem_bo_move_to_scanout(struct kgem *kgem, struct kgem_bo *bo)
1347
{
1494
{
1348
	assert(bo->refcnt == 0);
1495
	assert(bo->refcnt == 0);
1349
	assert(bo->scanout);
1496
	assert(bo->scanout);
1350
	assert(bo->delta);
1497
	assert(bo->delta);
1351
	assert(!bo->snoop);
1498
	assert(!bo->snoop);
1352
	assert(!bo->io);
1499
	assert(!bo->io);
1353
 
1500
 
1354
	DBG(("%s: moving %d [fb %d] to scanout cache, active? %d\n",
1501
	DBG(("%s: moving %d [fb %d] to scanout cache, active? %d\n",
1355
	     __FUNCTION__, bo->handle, bo->delta, bo->rq != NULL));
1502
	     __FUNCTION__, bo->handle, bo->delta, bo->rq != NULL));
1356
	if (bo->rq)
1503
	if (bo->rq)
1357
		list_move_tail(&bo->list, &kgem->scanout);
1504
		list_move_tail(&bo->list, &kgem->scanout);
1358
	else
1505
	else
1359
	list_move(&bo->list, &kgem->scanout);
1506
	list_move(&bo->list, &kgem->scanout);
1360
}
1507
}
1361
 
1508
 
1362
static void kgem_bo_move_to_snoop(struct kgem *kgem, struct kgem_bo *bo)
1509
static void kgem_bo_move_to_snoop(struct kgem *kgem, struct kgem_bo *bo)
1363
{
1510
{
1364
	assert(bo->refcnt == 0);
1511
	assert(bo->refcnt == 0);
1365
	assert(bo->exec == NULL);
1512
	assert(bo->exec == NULL);
1366
 
1513
 
1367
	if (num_pages(bo) > kgem->max_cpu_size >> 13) {
1514
	if (num_pages(bo) > kgem->max_cpu_size >> 13) {
1368
		DBG(("%s handle=%d discarding large CPU buffer (%d >%d pages)\n",
1515
		DBG(("%s handle=%d discarding large CPU buffer (%d >%d pages)\n",
1369
		     __FUNCTION__, bo->handle, num_pages(bo), kgem->max_cpu_size >> 13));
1516
		     __FUNCTION__, bo->handle, num_pages(bo), kgem->max_cpu_size >> 13));
1370
		kgem_bo_free(kgem, bo);
1517
		kgem_bo_free(kgem, bo);
1371
		return;
1518
		return;
1372
	}
1519
	}
1373
 
1520
 
1374
	assert(bo->tiling == I915_TILING_NONE);
1521
	assert(bo->tiling == I915_TILING_NONE);
1375
	assert(bo->rq == NULL);
1522
	assert(bo->rq == NULL);
1376
 
1523
 
1377
	DBG(("%s: moving %d to snoop cachee\n", __FUNCTION__, bo->handle));
1524
	DBG(("%s: moving %d to snoop cachee\n", __FUNCTION__, bo->handle));
1378
	list_add(&bo->list, &kgem->snoop);
1525
	list_add(&bo->list, &kgem->snoop);
1379
}
1526
}
1380
 
1527
 
1381
static struct kgem_bo *
1528
static struct kgem_bo *
1382
search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags)
1529
search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags)
1383
{
1530
{
1384
	struct kgem_bo *bo, *first = NULL;
1531
	struct kgem_bo *bo, *first = NULL;
1385
 
1532
 
1386
	DBG(("%s: num_pages=%d, flags=%x\n", __FUNCTION__, num_pages, flags));
1533
	DBG(("%s: num_pages=%d, flags=%x\n", __FUNCTION__, num_pages, flags));
1387
 
1534
 
1388
	if ((kgem->has_cacheing | kgem->has_userptr) == 0)
1535
	if ((kgem->has_cacheing | kgem->has_userptr) == 0)
1389
		return NULL;
1536
		return NULL;
1390
 
1537
 
1391
	if (list_is_empty(&kgem->snoop)) {
1538
	if (list_is_empty(&kgem->snoop)) {
1392
		DBG(("%s: inactive and cache empty\n", __FUNCTION__));
1539
		DBG(("%s: inactive and cache empty\n", __FUNCTION__));
1393
		if (!__kgem_throttle_retire(kgem, flags)) {
1540
		if (!__kgem_throttle_retire(kgem, flags)) {
1394
			DBG(("%s: nothing retired\n", __FUNCTION__));
1541
			DBG(("%s: nothing retired\n", __FUNCTION__));
1395
			return NULL;
1542
			return NULL;
1396
		}
1543
		}
1397
	}
1544
	}
1398
 
1545
 
1399
	list_for_each_entry(bo, &kgem->snoop, list) {
1546
	list_for_each_entry(bo, &kgem->snoop, list) {
1400
		assert(bo->refcnt == 0);
1547
		assert(bo->refcnt == 0);
1401
		assert(bo->snoop);
1548
		assert(bo->snoop);
1402
		assert(!bo->scanout);
1549
		assert(!bo->scanout);
1403
		assert(bo->proxy == NULL);
1550
		assert(bo->proxy == NULL);
1404
		assert(bo->tiling == I915_TILING_NONE);
1551
		assert(bo->tiling == I915_TILING_NONE);
1405
		assert(bo->rq == NULL);
1552
		assert(bo->rq == NULL);
1406
		assert(bo->exec == NULL);
1553
		assert(bo->exec == NULL);
1407
 
1554
 
1408
		if (num_pages > num_pages(bo))
1555
		if (num_pages > num_pages(bo))
1409
			continue;
1556
			continue;
1410
 
1557
 
1411
		if (num_pages(bo) > 2*num_pages) {
1558
		if (num_pages(bo) > 2*num_pages) {
1412
			if (first == NULL)
1559
			if (first == NULL)
1413
				first = bo;
1560
				first = bo;
1414
			continue;
1561
			continue;
1415
		}
1562
		}
1416
 
1563
 
1417
		list_del(&bo->list);
1564
		list_del(&bo->list);
1418
		bo->pitch = 0;
1565
		bo->pitch = 0;
1419
		bo->delta = 0;
1566
		bo->delta = 0;
1420
 
1567
 
1421
		DBG(("  %s: found handle=%d (num_pages=%d) in snoop cache\n",
1568
		DBG(("  %s: found handle=%d (num_pages=%d) in snoop cache\n",
1422
		     __FUNCTION__, bo->handle, num_pages(bo)));
1569
		     __FUNCTION__, bo->handle, num_pages(bo)));
1423
		return bo;
1570
		return bo;
1424
	}
1571
	}
1425
 
1572
 
1426
	if (first) {
1573
	if (first) {
1427
		list_del(&first->list);
1574
		list_del(&first->list);
1428
		first->pitch = 0;
1575
		first->pitch = 0;
1429
		first->delta = 0;
1576
		first->delta = 0;
1430
 
1577
 
1431
		DBG(("  %s: found handle=%d (num_pages=%d) in snoop cache\n",
1578
		DBG(("  %s: found handle=%d (num_pages=%d) in snoop cache\n",
1432
		     __FUNCTION__, first->handle, num_pages(first)));
1579
		     __FUNCTION__, first->handle, num_pages(first)));
1433
		return first;
1580
		return first;
1434
	}
1581
	}
1435
 
1582
 
1436
	return NULL;
1583
	return NULL;
1437
}
1584
}
1438
 
1585
 
1439
static void __kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo)
1586
static void __kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo)
1440
{
1587
{
1441
	DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle));
1588
	DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle));
1442
 
1589
 
1443
	assert(list_is_empty(&bo->list));
1590
	assert(list_is_empty(&bo->list));
1444
	assert(bo->refcnt == 0);
1591
	assert(bo->refcnt == 0);
1445
	assert(!bo->purged);
1592
	assert(!bo->purged);
1446
	assert(bo->proxy == NULL);
1593
	assert(bo->proxy == NULL);
1447
 
1594
 
1448
	bo->binding.offset = 0;
1595
	bo->binding.offset = 0;
1449
 
1596
 
1450
	if (DBG_NO_CACHE)
1597
	if (DBG_NO_CACHE)
1451
		goto destroy;
1598
		goto destroy;
1452
 
1599
 
1453
	if (bo->snoop && !bo->flush) {
1600
	if (bo->snoop && !bo->flush) {
1454
		DBG(("%s: handle=%d is snooped\n", __FUNCTION__, bo->handle));
1601
		DBG(("%s: handle=%d is snooped\n", __FUNCTION__, bo->handle));
1455
		assert(!bo->flush);
1602
		assert(!bo->flush);
1456
		assert(list_is_empty(&bo->list));
1603
		assert(list_is_empty(&bo->list));
1457
		if (bo->exec == NULL && bo->rq && !__kgem_busy(kgem, bo->handle))
1604
		if (bo->exec == NULL && bo->rq && !__kgem_busy(kgem, bo->handle))
1458
			__kgem_bo_clear_busy(bo);
1605
			__kgem_bo_clear_busy(bo);
1459
		if (bo->rq == NULL) {
1606
		if (bo->rq == NULL) {
1460
			assert(!bo->needs_flush);
1607
			assert(!bo->needs_flush);
1461
			kgem_bo_move_to_snoop(kgem, bo);
1608
			kgem_bo_move_to_snoop(kgem, bo);
1462
		}
1609
		}
1463
		return;
1610
		return;
1464
	}
1611
	}
1465
 
1612
 
1466
	if (bo->scanout) {
1613
	if (bo->scanout) {
1467
		kgem_bo_move_to_scanout(kgem, bo);
1614
		kgem_bo_move_to_scanout(kgem, bo);
1468
		return;
1615
		return;
1469
	}
1616
	}
1470
 
1617
 
1471
	if (bo->io)
1618
	if (bo->io)
1472
		bo = kgem_bo_replace_io(bo);
1619
		bo = kgem_bo_replace_io(bo);
1473
	if (!bo->reusable) {
1620
	if (!bo->reusable) {
1474
		DBG(("%s: handle=%d, not reusable\n",
1621
		DBG(("%s: handle=%d, not reusable\n",
1475
		     __FUNCTION__, bo->handle));
1622
		     __FUNCTION__, bo->handle));
1476
		goto destroy;
1623
		goto destroy;
1477
	}
1624
	}
1478
 
1625
 
1479
	if (!kgem->has_llc && IS_CPU_MAP(bo->map) && bo->domain != DOMAIN_CPU)
1626
	if (!kgem->has_llc && IS_CPU_MAP(bo->map) && bo->domain != DOMAIN_CPU)
1480
		kgem_bo_release_map(kgem, bo);
1627
		kgem_bo_release_map(kgem, bo);
1481
 
1628
 
1482
	assert(list_is_empty(&bo->vma));
1629
	assert(list_is_empty(&bo->vma));
1483
	assert(list_is_empty(&bo->list));
1630
	assert(list_is_empty(&bo->list));
1484
	assert(bo->snoop == false);
1631
	assert(bo->snoop == false);
1485
	assert(bo->io == false);
1632
	assert(bo->io == false);
1486
	assert(bo->scanout == false);
1633
	assert(bo->scanout == false);
1487
 
1634
 
1488
	if (bo->exec && kgem->nexec == 1) {
1635
	if (bo->exec && kgem->nexec == 1) {
1489
		DBG(("%s: only handle in batch, discarding last operations\n",
1636
		DBG(("%s: only handle in batch, discarding last operations\n",
1490
		     __FUNCTION__));
1637
		     __FUNCTION__));
1491
		assert(bo->exec == &kgem->exec[0]);
1638
		assert(bo->exec == &kgem->exec[0]);
1492
		assert(kgem->exec[0].handle == bo->handle);
1639
		assert(kgem->exec[0].handle == bo->handle);
1493
		assert(RQ(bo->rq) == kgem->next_request);
1640
		assert(RQ(bo->rq) == kgem->next_request);
1494
		bo->refcnt = 1;
1641
		bo->refcnt = 1;
1495
		kgem_reset(kgem);
1642
		kgem_reset(kgem);
1496
		bo->refcnt = 0;
1643
		bo->refcnt = 0;
1497
	}
1644
	}
1498
 
1645
 
1499
	if (bo->rq && bo->exec == NULL && !__kgem_busy(kgem, bo->handle))
1646
	if (bo->rq && bo->exec == NULL && !__kgem_busy(kgem, bo->handle))
1500
		__kgem_bo_clear_busy(bo);
1647
		__kgem_bo_clear_busy(bo);
1501
 
1648
 
1502
	if (bo->rq) {
1649
	if (bo->rq) {
1503
		struct list *cache;
1650
		struct list *cache;
1504
 
1651
 
1505
		DBG(("%s: handle=%d -> active\n", __FUNCTION__, bo->handle));
1652
		DBG(("%s: handle=%d -> active\n", __FUNCTION__, bo->handle));
1506
		if (bucket(bo) < NUM_CACHE_BUCKETS)
1653
		if (bucket(bo) < NUM_CACHE_BUCKETS)
1507
			cache = &kgem->active[bucket(bo)][bo->tiling];
1654
			cache = &kgem->active[bucket(bo)][bo->tiling];
1508
		else
1655
		else
1509
			cache = &kgem->large;
1656
			cache = &kgem->large;
1510
		list_add(&bo->list, cache);
1657
		list_add(&bo->list, cache);
1511
		return;
1658
		return;
1512
	}
1659
	}
1513
 
1660
 
1514
	assert(bo->exec == NULL);
1661
	assert(bo->exec == NULL);
1515
	assert(list_is_empty(&bo->request));
1662
	assert(list_is_empty(&bo->request));
1516
 
1663
 
1517
	if (!IS_CPU_MAP(bo->map)) {
1664
	if (!IS_CPU_MAP(bo->map)) {
1518
		if (!kgem_bo_set_purgeable(kgem, bo))
1665
		if (!kgem_bo_set_purgeable(kgem, bo))
1519
			goto destroy;
1666
			goto destroy;
1520
 
1667
 
1521
		if (!kgem->has_llc && bo->domain == DOMAIN_CPU)
1668
		if (!kgem->has_llc && bo->domain == DOMAIN_CPU)
1522
			goto destroy;
1669
			goto destroy;
1523
 
1670
 
1524
		DBG(("%s: handle=%d, purged\n",
1671
		DBG(("%s: handle=%d, purged\n",
1525
		     __FUNCTION__, bo->handle));
1672
		     __FUNCTION__, bo->handle));
1526
	}
1673
	}
1527
 
1674
 
1528
	kgem_bo_move_to_inactive(kgem, bo);
1675
	kgem_bo_move_to_inactive(kgem, bo);
1529
	return;
1676
	return;
1530
 
1677
 
1531
destroy:
1678
destroy:
1532
	if (!bo->exec)
1679
	if (!bo->exec)
1533
		kgem_bo_free(kgem, bo);
1680
		kgem_bo_free(kgem, bo);
1534
}
1681
}
1535
 
1682
 
1536
static void kgem_bo_unref(struct kgem *kgem, struct kgem_bo *bo)
1683
static void kgem_bo_unref(struct kgem *kgem, struct kgem_bo *bo)
1537
{
1684
{
1538
	assert(bo->refcnt);
1685
	assert(bo->refcnt);
1539
	if (--bo->refcnt == 0)
1686
	if (--bo->refcnt == 0)
1540
		__kgem_bo_destroy(kgem, bo);
1687
		__kgem_bo_destroy(kgem, bo);
1541
}
1688
}
1542
 
1689
 
1543
static void kgem_buffer_release(struct kgem *kgem, struct kgem_buffer *bo)
1690
static void kgem_buffer_release(struct kgem *kgem, struct kgem_buffer *bo)
1544
{
1691
{
1545
	while (!list_is_empty(&bo->base.vma)) {
1692
	while (!list_is_empty(&bo->base.vma)) {
1546
		struct kgem_bo *cached;
1693
		struct kgem_bo *cached;
1547
 
1694
 
1548
		cached = list_first_entry(&bo->base.vma, struct kgem_bo, vma);
1695
		cached = list_first_entry(&bo->base.vma, struct kgem_bo, vma);
1549
		assert(cached->proxy == &bo->base);
1696
		assert(cached->proxy == &bo->base);
1550
		list_del(&cached->vma);
1697
		list_del(&cached->vma);
1551
 
1698
 
1552
		assert(*(struct kgem_bo **)cached->map == cached);
1699
		assert(*(struct kgem_bo **)cached->map == cached);
1553
		*(struct kgem_bo **)cached->map = NULL;
1700
		*(struct kgem_bo **)cached->map = NULL;
1554
		cached->map = NULL;
1701
		cached->map = NULL;
1555
 
1702
 
1556
		kgem_bo_destroy(kgem, cached);
1703
		kgem_bo_destroy(kgem, cached);
1557
	}
1704
	}
1558
}
1705
}
1559
 
1706
 
1560
static bool kgem_retire__buffers(struct kgem *kgem)
1707
static bool kgem_retire__buffers(struct kgem *kgem)
1561
{
1708
{
1562
	bool retired = false;
1709
	bool retired = false;
1563
 
1710
 
1564
	while (!list_is_empty(&kgem->active_buffers)) {
1711
	while (!list_is_empty(&kgem->active_buffers)) {
1565
		struct kgem_buffer *bo =
1712
		struct kgem_buffer *bo =
1566
			list_last_entry(&kgem->active_buffers,
1713
			list_last_entry(&kgem->active_buffers,
1567
					struct kgem_buffer,
1714
					struct kgem_buffer,
1568
					base.list);
1715
					base.list);
1569
 
1716
 
1570
		if (bo->base.rq)
1717
		if (bo->base.rq)
1571
			break;
1718
			break;
1572
 
1719
 
1573
		DBG(("%s: releasing upload cache for handle=%d? %d\n",
1720
		DBG(("%s: releasing upload cache for handle=%d? %d\n",
1574
		     __FUNCTION__, bo->base.handle, !list_is_empty(&bo->base.vma)));
1721
		     __FUNCTION__, bo->base.handle, !list_is_empty(&bo->base.vma)));
1575
		list_del(&bo->base.list);
1722
		list_del(&bo->base.list);
1576
		kgem_buffer_release(kgem, bo);
1723
		kgem_buffer_release(kgem, bo);
1577
		kgem_bo_unref(kgem, &bo->base);
1724
		kgem_bo_unref(kgem, &bo->base);
1578
		retired = true;
1725
		retired = true;
1579
	}
1726
	}
1580
 
1727
 
1581
	return retired;
1728
	return retired;
1582
}
1729
}
1583
 
1730
 
1584
static bool kgem_retire__flushing(struct kgem *kgem)
1731
static bool kgem_retire__flushing(struct kgem *kgem)
1585
{
1732
{
1586
	struct kgem_bo *bo, *next;
1733
	struct kgem_bo *bo, *next;
1587
	bool retired = false;
1734
	bool retired = false;
1588
 
1735
 
1589
	list_for_each_entry_safe(bo, next, &kgem->flushing, request) {
1736
	list_for_each_entry_safe(bo, next, &kgem->flushing, request) {
1590
		assert(bo->rq == (void *)kgem);
1737
		assert(bo->rq == (void *)kgem);
1591
		assert(bo->exec == NULL);
1738
		assert(bo->exec == NULL);
1592
 
1739
 
1593
		if (__kgem_busy(kgem, bo->handle))
1740
		if (__kgem_busy(kgem, bo->handle))
1594
			break;
1741
			break;
1595
 
1742
 
1596
		__kgem_bo_clear_busy(bo);
1743
		__kgem_bo_clear_busy(bo);
1597
 
1744
 
1598
		if (bo->refcnt)
1745
		if (bo->refcnt)
1599
			continue;
1746
			continue;
1600
 
1747
 
1601
		if (bo->snoop) {
1748
		if (bo->snoop) {
1602
			kgem_bo_move_to_snoop(kgem, bo);
1749
			kgem_bo_move_to_snoop(kgem, bo);
1603
		} else if (bo->scanout) {
1750
		} else if (bo->scanout) {
1604
			kgem_bo_move_to_scanout(kgem, bo);
1751
			kgem_bo_move_to_scanout(kgem, bo);
1605
		} else if ((bo = kgem_bo_replace_io(bo))->reusable &&
1752
		} else if ((bo = kgem_bo_replace_io(bo))->reusable &&
1606
			   kgem_bo_set_purgeable(kgem, bo)) {
1753
			   kgem_bo_set_purgeable(kgem, bo)) {
1607
			kgem_bo_move_to_inactive(kgem, bo);
1754
			kgem_bo_move_to_inactive(kgem, bo);
1608
			retired = true;
1755
			retired = true;
1609
		} else
1756
		} else
1610
			kgem_bo_free(kgem, bo);
1757
			kgem_bo_free(kgem, bo);
1611
	}
1758
	}
1612
#if HAS_DEBUG_FULL
1759
#if HAS_DEBUG_FULL
1613
	{
1760
	{
1614
		int count = 0;
1761
		int count = 0;
1615
		list_for_each_entry(bo, &kgem->flushing, request)
1762
		list_for_each_entry(bo, &kgem->flushing, request)
1616
			count++;
1763
			count++;
1617
		printf("%s: %d bo on flushing list\n", __FUNCTION__, count);
1764
		printf("%s: %d bo on flushing list\n", __FUNCTION__, count);
1618
	}
1765
	}
1619
#endif
1766
#endif
1620
 
1767
 
1621
	kgem->need_retire |= !list_is_empty(&kgem->flushing);
1768
	kgem->need_retire |= !list_is_empty(&kgem->flushing);
1622
 
1769
 
1623
	return retired;
1770
	return retired;
1624
}
1771
}
1625
 
1772
 
1626
 
1773
 
1627
static bool __kgem_retire_rq(struct kgem *kgem, struct kgem_request *rq)
1774
static bool __kgem_retire_rq(struct kgem *kgem, struct kgem_request *rq)
1628
{
1775
{
1629
	bool retired = false;
1776
	bool retired = false;
1630
 
1777
 
1631
	DBG(("%s: request %d complete\n",
1778
	DBG(("%s: request %d complete\n",
1632
	     __FUNCTION__, rq->bo->handle));
1779
	     __FUNCTION__, rq->bo->handle));
1633
 
1780
 
1634
	while (!list_is_empty(&rq->buffers)) {
1781
	while (!list_is_empty(&rq->buffers)) {
1635
		struct kgem_bo *bo;
1782
		struct kgem_bo *bo;
1636
 
1783
 
1637
		bo = list_first_entry(&rq->buffers,
1784
		bo = list_first_entry(&rq->buffers,
1638
				      struct kgem_bo,
1785
				      struct kgem_bo,
1639
				      request);
1786
				      request);
1640
 
1787
 
1641
		assert(RQ(bo->rq) == rq);
1788
		assert(RQ(bo->rq) == rq);
1642
		assert(bo->exec == NULL);
1789
		assert(bo->exec == NULL);
1643
		assert(bo->domain == DOMAIN_GPU || bo->domain == DOMAIN_NONE);
1790
		assert(bo->domain == DOMAIN_GPU || bo->domain == DOMAIN_NONE);
1644
 
1791
 
1645
		list_del(&bo->request);
1792
		list_del(&bo->request);
1646
 
1793
 
1647
		if (bo->needs_flush)
1794
		if (bo->needs_flush)
1648
			bo->needs_flush = __kgem_busy(kgem, bo->handle);
1795
			bo->needs_flush = __kgem_busy(kgem, bo->handle);
1649
		if (bo->needs_flush) {
1796
		if (bo->needs_flush) {
1650
			DBG(("%s: moving %d to flushing\n",
1797
			DBG(("%s: moving %d to flushing\n",
1651
			     __FUNCTION__, bo->handle));
1798
			     __FUNCTION__, bo->handle));
1652
			list_add(&bo->request, &kgem->flushing);
1799
			list_add(&bo->request, &kgem->flushing);
1653
			bo->rq = (void *)kgem;
1800
			bo->rq = (void *)kgem;
1654
			continue;
1801
			continue;
1655
		}
1802
		}
1656
 
1803
 
1657
		bo->domain = DOMAIN_NONE;
1804
		bo->domain = DOMAIN_NONE;
1658
		bo->rq = NULL;
1805
		bo->rq = NULL;
1659
		if (bo->refcnt)
1806
		if (bo->refcnt)
1660
			continue;
1807
			continue;
1661
 
1808
 
1662
		if (bo->snoop) {
1809
		if (bo->snoop) {
1663
			kgem_bo_move_to_snoop(kgem, bo);
1810
			kgem_bo_move_to_snoop(kgem, bo);
1664
		} else if (bo->scanout) {
1811
		} else if (bo->scanout) {
1665
			kgem_bo_move_to_scanout(kgem, bo);
1812
			kgem_bo_move_to_scanout(kgem, bo);
1666
		} else if ((bo = kgem_bo_replace_io(bo))->reusable &&
1813
		} else if ((bo = kgem_bo_replace_io(bo))->reusable &&
1667
			   kgem_bo_set_purgeable(kgem, bo)) {
1814
			   kgem_bo_set_purgeable(kgem, bo)) {
1668
			kgem_bo_move_to_inactive(kgem, bo);
1815
			kgem_bo_move_to_inactive(kgem, bo);
1669
			retired = true;
1816
			retired = true;
1670
		} else {
1817
		} else {
1671
			DBG(("%s: closing %d\n",
1818
			DBG(("%s: closing %d\n",
1672
			     __FUNCTION__, bo->handle));
1819
			     __FUNCTION__, bo->handle));
1673
			kgem_bo_free(kgem, bo);
1820
			kgem_bo_free(kgem, bo);
1674
		}
1821
		}
1675
	}
1822
	}
1676
 
1823
 
1677
	assert(rq->bo->rq == NULL);
1824
	assert(rq->bo->rq == NULL);
1678
	assert(list_is_empty(&rq->bo->request));
1825
	assert(list_is_empty(&rq->bo->request));
1679
 
1826
 
1680
	if (--rq->bo->refcnt == 0) {
1827
	if (--rq->bo->refcnt == 0) {
1681
		if (kgem_bo_set_purgeable(kgem, rq->bo)) {
1828
		if (kgem_bo_set_purgeable(kgem, rq->bo)) {
1682
			kgem_bo_move_to_inactive(kgem, rq->bo);
1829
			kgem_bo_move_to_inactive(kgem, rq->bo);
1683
			retired = true;
1830
			retired = true;
1684
		} else {
1831
		} else {
1685
			DBG(("%s: closing %d\n",
1832
			DBG(("%s: closing %d\n",
1686
			     __FUNCTION__, rq->bo->handle));
1833
			     __FUNCTION__, rq->bo->handle));
1687
			kgem_bo_free(kgem, rq->bo);
1834
			kgem_bo_free(kgem, rq->bo);
1688
		}
1835
		}
1689
	}
1836
	}
1690
 
1837
 
1691
	__kgem_request_free(rq);
1838
	__kgem_request_free(rq);
1692
	return retired;
1839
	return retired;
1693
}
1840
}
1694
 
1841
 
1695
static bool kgem_retire__requests_ring(struct kgem *kgem, int ring)
1842
static bool kgem_retire__requests_ring(struct kgem *kgem, int ring)
1696
{
1843
{
1697
	bool retired = false;
1844
	bool retired = false;
1698
 
1845
 
1699
	while (!list_is_empty(&kgem->requests[ring])) {
1846
	while (!list_is_empty(&kgem->requests[ring])) {
1700
		struct kgem_request *rq;
1847
		struct kgem_request *rq;
1701
 
1848
 
1702
		rq = list_first_entry(&kgem->requests[ring],
1849
		rq = list_first_entry(&kgem->requests[ring],
1703
				      struct kgem_request,
1850
				      struct kgem_request,
1704
				      list);
1851
				      list);
1705
		if (__kgem_busy(kgem, rq->bo->handle))
1852
		if (__kgem_busy(kgem, rq->bo->handle))
1706
			break;
1853
			break;
1707
 
1854
 
1708
		retired |= __kgem_retire_rq(kgem, rq);
1855
		retired |= __kgem_retire_rq(kgem, rq);
1709
	}
1856
	}
1710
 
1857
 
1711
#if HAS_DEBUG_FULL
1858
#if HAS_DEBUG_FULL
1712
	{
1859
	{
1713
		struct kgem_bo *bo;
1860
		struct kgem_bo *bo;
1714
		int count = 0;
1861
		int count = 0;
1715
 
1862
 
1716
		list_for_each_entry(bo, &kgem->requests[ring], request)
1863
		list_for_each_entry(bo, &kgem->requests[ring], request)
1717
			count++;
1864
			count++;
1718
 
1865
 
1719
		bo = NULL;
1866
		bo = NULL;
1720
		if (!list_is_empty(&kgem->requests[ring]))
1867
		if (!list_is_empty(&kgem->requests[ring]))
1721
			bo = list_first_entry(&kgem->requests[ring],
1868
			bo = list_first_entry(&kgem->requests[ring],
1722
					      struct kgem_request,
1869
					      struct kgem_request,
1723
					      list)->bo;
1870
					      list)->bo;
1724
 
1871
 
1725
		printf("%s: ring=%d, %d outstanding requests, oldest=%d\n",
1872
		printf("%s: ring=%d, %d outstanding requests, oldest=%d\n",
1726
		       __FUNCTION__, ring, count, bo ? bo->handle : 0);
1873
		       __FUNCTION__, ring, count, bo ? bo->handle : 0);
1727
	}
1874
	}
1728
#endif
1875
#endif
1729
 
1876
 
1730
	return retired;
1877
	return retired;
1731
}
1878
}
1732
 
1879
 
1733
static bool kgem_retire__requests(struct kgem *kgem)
1880
static bool kgem_retire__requests(struct kgem *kgem)
1734
{
1881
{
1735
	bool retired = false;
1882
	bool retired = false;
1736
	int n;
1883
	int n;
1737
 
1884
 
1738
	for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) {
1885
	for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) {
1739
		retired |= kgem_retire__requests_ring(kgem, n);
1886
		retired |= kgem_retire__requests_ring(kgem, n);
1740
		kgem->need_retire |= !list_is_empty(&kgem->requests[n]);
1887
		kgem->need_retire |= !list_is_empty(&kgem->requests[n]);
1741
	}
1888
	}
1742
 
1889
 
1743
	return retired;
1890
	return retired;
1744
}
1891
}
1745
 
1892
 
1746
bool kgem_retire(struct kgem *kgem)
1893
bool kgem_retire(struct kgem *kgem)
1747
{
1894
{
1748
	bool retired = false;
1895
	bool retired = false;
1749
 
1896
 
1750
	DBG(("%s\n", __FUNCTION__));
1897
	DBG(("%s\n", __FUNCTION__));
1751
 
1898
 
1752
	kgem->need_retire = false;
1899
	kgem->need_retire = false;
1753
 
1900
 
1754
	retired |= kgem_retire__flushing(kgem);
1901
	retired |= kgem_retire__flushing(kgem);
1755
	retired |= kgem_retire__requests(kgem);
1902
	retired |= kgem_retire__requests(kgem);
1756
	retired |= kgem_retire__buffers(kgem);
1903
	retired |= kgem_retire__buffers(kgem);
1757
 
1904
 
1758
	DBG(("%s -- retired=%d, need_retire=%d\n",
1905
	DBG(("%s -- retired=%d, need_retire=%d\n",
1759
	     __FUNCTION__, retired, kgem->need_retire));
1906
	     __FUNCTION__, retired, kgem->need_retire));
1760
 
1907
 
1761
	kgem->retire(kgem);
1908
	kgem->retire(kgem);
1762
 
1909
 
1763
	return retired;
1910
	return retired;
1764
}
1911
}
-
 
1912
 
-
 
1913
bool __kgem_ring_is_idle(struct kgem *kgem, int ring)
-
 
1914
{
-
 
1915
	struct kgem_request *rq;
-
 
1916
 
-
 
1917
	assert(!list_is_empty(&kgem->requests[ring]));
-
 
1918
 
-
 
1919
	rq = list_last_entry(&kgem->requests[ring],
-
 
1920
			     struct kgem_request, list);
-
 
1921
	if (__kgem_busy(kgem, rq->bo->handle)) {
-
 
1922
		DBG(("%s: last requests handle=%d still busy\n",
-
 
1923
		     __FUNCTION__, rq->bo->handle));
-
 
1924
		return false;
-
 
1925
	}
-
 
1926
 
-
 
1927
	DBG(("%s: ring=%d idle (handle=%d)\n",
1765
 
1928
	     __FUNCTION__, ring, rq->bo->handle));
1766
 
1929
 
1767
 
1930
	kgem_retire__requests_ring(kgem, ring);
1768
 
1931
	assert(list_is_empty(&kgem->requests[ring]));
1769
 
1932
	return true;
1770
 
1933
}
1771
 
1934
 
1772
static void kgem_commit(struct kgem *kgem)
1935
static void kgem_commit(struct kgem *kgem)
1773
{
1936
{
1774
	struct kgem_request *rq = kgem->next_request;
1937
	struct kgem_request *rq = kgem->next_request;
1775
	struct kgem_bo *bo, *next;
1938
	struct kgem_bo *bo, *next;
1776
 
1939
 
1777
	list_for_each_entry_safe(bo, next, &rq->buffers, request) {
1940
	list_for_each_entry_safe(bo, next, &rq->buffers, request) {
1778
		assert(next->request.prev == &bo->request);
1941
		assert(next->request.prev == &bo->request);
1779
 
1942
 
1780
		DBG(("%s: release handle=%d (proxy? %d), dirty? %d flush? %d, snoop? %d -> offset=%x\n",
1943
		DBG(("%s: release handle=%d (proxy? %d), dirty? %d flush? %d, snoop? %d -> offset=%x\n",
1781
		     __FUNCTION__, bo->handle, bo->proxy != NULL,
1944
		     __FUNCTION__, bo->handle, bo->proxy != NULL,
1782
		     bo->dirty, bo->needs_flush, bo->snoop,
1945
		     bo->dirty, bo->needs_flush, bo->snoop,
1783
		     (unsigned)bo->exec->offset));
1946
		     (unsigned)bo->exec->offset));
1784
 
1947
 
1785
		assert(!bo->purged);
1948
		assert(!bo->purged);
1786
		assert(bo->exec);
1949
		assert(bo->exec);
1787
		assert(bo->proxy == NULL || bo->exec == &_kgem_dummy_exec);
1950
		assert(bo->proxy == NULL || bo->exec == &_kgem_dummy_exec);
1788
		assert(RQ(bo->rq) == rq || (RQ(bo->proxy->rq) == rq));
1951
		assert(RQ(bo->rq) == rq || (RQ(bo->proxy->rq) == rq));
1789
 
1952
 
1790
		bo->presumed_offset = bo->exec->offset;
1953
		bo->presumed_offset = bo->exec->offset;
1791
		bo->exec = NULL;
1954
		bo->exec = NULL;
1792
		bo->target_handle = -1;
1955
		bo->target_handle = -1;
1793
 
1956
 
1794
		if (!bo->refcnt && !bo->reusable) {
1957
		if (!bo->refcnt && !bo->reusable) {
1795
			assert(!bo->snoop);
1958
			assert(!bo->snoop);
1796
			kgem_bo_free(kgem, bo);
1959
			kgem_bo_free(kgem, bo);
1797
			continue;
1960
			continue;
1798
		}
1961
		}
1799
 
1962
 
1800
		bo->binding.offset = 0;
1963
		bo->binding.offset = 0;
1801
		bo->domain = DOMAIN_GPU;
1964
		bo->domain = DOMAIN_GPU;
1802
		bo->dirty = false;
1965
		bo->dirty = false;
1803
 
1966
 
1804
		if (bo->proxy) {
1967
		if (bo->proxy) {
1805
			/* proxies are not used for domain tracking */
1968
			/* proxies are not used for domain tracking */
1806
			bo->exec = NULL;
1969
			bo->exec = NULL;
1807
			__kgem_bo_clear_busy(bo);
1970
			__kgem_bo_clear_busy(bo);
1808
		}
1971
		}
1809
 
1972
 
1810
		kgem->scanout_busy |= bo->scanout;
1973
		kgem->scanout_busy |= bo->scanout;
1811
	}
1974
	}
1812
 
1975
 
1813
	if (rq == &kgem->static_request) {
1976
	if (rq == &kgem->static_request) {
1814
		struct drm_i915_gem_set_domain set_domain;
1977
		struct drm_i915_gem_set_domain set_domain;
1815
 
1978
 
1816
		DBG(("%s: syncing due to allocation failure\n", __FUNCTION__));
1979
		DBG(("%s: syncing due to allocation failure\n", __FUNCTION__));
1817
 
1980
 
1818
		VG_CLEAR(set_domain);
1981
		VG_CLEAR(set_domain);
1819
		set_domain.handle = rq->bo->handle;
1982
		set_domain.handle = rq->bo->handle;
1820
		set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1983
		set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1821
		set_domain.write_domain = I915_GEM_DOMAIN_GTT;
1984
		set_domain.write_domain = I915_GEM_DOMAIN_GTT;
1822
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) {
1985
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) {
1823
			DBG(("%s: sync: GPU hang detected\n", __FUNCTION__));
1986
			DBG(("%s: sync: GPU hang detected\n", __FUNCTION__));
1824
			kgem_throttle(kgem);
1987
			kgem_throttle(kgem);
1825
		}
1988
		}
1826
 
1989
 
1827
		kgem_retire(kgem);
1990
		kgem_retire(kgem);
1828
		assert(list_is_empty(&rq->buffers));
1991
		assert(list_is_empty(&rq->buffers));
1829
 
1992
 
1830
		gem_close(kgem->fd, rq->bo->handle);
1993
		gem_close(kgem->fd, rq->bo->handle);
1831
		kgem_cleanup_cache(kgem);
1994
		kgem_cleanup_cache(kgem);
1832
	} else {
1995
	} else {
1833
		list_add_tail(&rq->list, &kgem->requests[rq->ring]);
1996
		list_add_tail(&rq->list, &kgem->requests[rq->ring]);
1834
		kgem->need_throttle = kgem->need_retire = 1;
1997
		kgem->need_throttle = kgem->need_retire = 1;
1835
	}
1998
	}
1836
 
1999
 
1837
	kgem->next_request = NULL;
2000
	kgem->next_request = NULL;
1838
}
2001
}
1839
 
2002
 
1840
static void kgem_close_list(struct kgem *kgem, struct list *head)
2003
static void kgem_close_list(struct kgem *kgem, struct list *head)
1841
{
2004
{
1842
	while (!list_is_empty(head))
2005
	while (!list_is_empty(head))
1843
		kgem_bo_free(kgem, list_first_entry(head, struct kgem_bo, list));
2006
		kgem_bo_free(kgem, list_first_entry(head, struct kgem_bo, list));
1844
}
2007
}
1845
 
2008
 
1846
static void kgem_close_inactive(struct kgem *kgem)
2009
static void kgem_close_inactive(struct kgem *kgem)
1847
{
2010
{
1848
	unsigned int i;
2011
	unsigned int i;
1849
 
2012
 
1850
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++)
2013
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++)
1851
		kgem_close_list(kgem, &kgem->inactive[i]);
2014
		kgem_close_list(kgem, &kgem->inactive[i]);
1852
}
2015
}
1853
 
2016
 
1854
static void kgem_finish_buffers(struct kgem *kgem)
2017
static void kgem_finish_buffers(struct kgem *kgem)
1855
{
2018
{
1856
	struct kgem_buffer *bo, *next;
2019
	struct kgem_buffer *bo, *next;
1857
 
2020
 
1858
	list_for_each_entry_safe(bo, next, &kgem->batch_buffers, base.list) {
2021
	list_for_each_entry_safe(bo, next, &kgem->batch_buffers, base.list) {
1859
		DBG(("%s: buffer handle=%d, used=%d, exec?=%d, write=%d, mmapped=%d\n",
2022
		DBG(("%s: buffer handle=%d, used=%d, exec?=%d, write=%d, mmapped=%d\n",
1860
		     __FUNCTION__, bo->base.handle, bo->used, bo->base.exec!=NULL,
2023
		     __FUNCTION__, bo->base.handle, bo->used, bo->base.exec!=NULL,
1861
		     bo->write, bo->mmapped));
2024
		     bo->write, bo->mmapped));
1862
 
2025
 
1863
		assert(next->base.list.prev == &bo->base.list);
2026
		assert(next->base.list.prev == &bo->base.list);
1864
		assert(bo->base.io);
2027
		assert(bo->base.io);
1865
		assert(bo->base.refcnt >= 1);
2028
		assert(bo->base.refcnt >= 1);
1866
 
2029
 
1867
		if (!bo->base.exec) {
2030
		if (!bo->base.exec) {
1868
			DBG(("%s: skipping unattached handle=%d, used=%d\n",
2031
			DBG(("%s: skipping unattached handle=%d, used=%d\n",
1869
			     __FUNCTION__, bo->base.handle, bo->used));
2032
			     __FUNCTION__, bo->base.handle, bo->used));
1870
			continue;
2033
			continue;
1871
		}
2034
		}
1872
 
2035
 
1873
		if (!bo->write) {
2036
		if (!bo->write) {
1874
			assert(bo->base.exec || bo->base.refcnt > 1);
2037
			assert(bo->base.exec || bo->base.refcnt > 1);
1875
			goto decouple;
2038
			goto decouple;
1876
		}
2039
		}
1877
 
2040
 
1878
		if (bo->mmapped) {
2041
		if (bo->mmapped) {
1879
			int used;
2042
			int used;
1880
 
2043
 
1881
			assert(!bo->need_io);
2044
			assert(!bo->need_io);
1882
 
2045
 
1883
			used = ALIGN(bo->used, PAGE_SIZE);
2046
			used = ALIGN(bo->used, PAGE_SIZE);
1884
			if (!DBG_NO_UPLOAD_ACTIVE &&
2047
			if (!DBG_NO_UPLOAD_ACTIVE &&
1885
			    used + PAGE_SIZE <= bytes(&bo->base) &&
2048
			    used + PAGE_SIZE <= bytes(&bo->base) &&
1886
			    (kgem->has_llc || !IS_CPU_MAP(bo->base.map) || bo->base.snoop)) {
2049
			    (kgem->has_llc || !IS_CPU_MAP(bo->base.map) || bo->base.snoop)) {
1887
				DBG(("%s: retaining upload buffer (%d/%d)\n",
2050
				DBG(("%s: retaining upload buffer (%d/%d)\n",
1888
				     __FUNCTION__, bo->used, bytes(&bo->base)));
2051
				     __FUNCTION__, bo->used, bytes(&bo->base)));
1889
				bo->used = used;
2052
				bo->used = used;
1890
				list_move(&bo->base.list,
2053
				list_move(&bo->base.list,
1891
					  &kgem->active_buffers);
2054
					  &kgem->active_buffers);
1892
				continue;
2055
				continue;
1893
			}
2056
			}
1894
			DBG(("%s: discarding mmapped buffer, used=%d, map type=%d\n",
2057
			DBG(("%s: discarding mmapped buffer, used=%d, map type=%d\n",
1895
			     __FUNCTION__, bo->used, (int)__MAP_TYPE(bo->base.map)));
2058
			     __FUNCTION__, bo->used, (int)__MAP_TYPE(bo->base.map)));
1896
			goto decouple;
2059
			goto decouple;
1897
		}
2060
		}
1898
 
2061
 
1899
		if (!bo->used) {
2062
		if (!bo->used) {
1900
			/* Unless we replace the handle in the execbuffer,
2063
			/* Unless we replace the handle in the execbuffer,
1901
			 * then this bo will become active. So decouple it
2064
			 * then this bo will become active. So decouple it
1902
			 * from the buffer list and track it in the normal
2065
			 * from the buffer list and track it in the normal
1903
			 * manner.
2066
			 * manner.
1904
			 */
2067
			 */
1905
			goto decouple;
2068
			goto decouple;
1906
		}
2069
		}
1907
 
2070
 
1908
		assert(bo->need_io);
2071
		assert(bo->need_io);
1909
		assert(bo->base.rq == MAKE_REQUEST(kgem->next_request, kgem->ring));
2072
		assert(bo->base.rq == MAKE_REQUEST(kgem->next_request, kgem->ring));
1910
		assert(bo->base.domain != DOMAIN_GPU);
2073
		assert(bo->base.domain != DOMAIN_GPU);
1911
 
2074
 
1912
		if (bo->base.refcnt == 1 &&
2075
		if (bo->base.refcnt == 1 &&
1913
		    bo->base.size.pages.count > 1 &&
2076
		    bo->base.size.pages.count > 1 &&
1914
		    bo->used < bytes(&bo->base) / 2) {
2077
		    bo->used < bytes(&bo->base) / 2) {
1915
			struct kgem_bo *shrink;
2078
			struct kgem_bo *shrink;
1916
			unsigned alloc = NUM_PAGES(bo->used);
2079
			unsigned alloc = NUM_PAGES(bo->used);
1917
 
2080
 
1918
			shrink = search_snoop_cache(kgem, alloc,
2081
			shrink = search_snoop_cache(kgem, alloc,
1919
						    CREATE_INACTIVE | CREATE_NO_RETIRE);
2082
						    CREATE_INACTIVE | CREATE_NO_RETIRE);
1920
			if (shrink) {
2083
			if (shrink) {
1921
				void *map;
2084
				void *map;
1922
				int n;
2085
				int n;
1923
 
2086
 
1924
				DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n",
2087
				DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n",
1925
				     __FUNCTION__,
2088
				     __FUNCTION__,
1926
				     bo->used, bytes(&bo->base), bytes(shrink),
2089
				     bo->used, bytes(&bo->base), bytes(shrink),
1927
				     bo->base.handle, shrink->handle));
2090
				     bo->base.handle, shrink->handle));
1928
 
2091
 
1929
				assert(bo->used <= bytes(shrink));
2092
				assert(bo->used <= bytes(shrink));
1930
				map = kgem_bo_map__cpu(kgem, shrink);
2093
				map = kgem_bo_map__cpu(kgem, shrink);
1931
				if (map) {
2094
				if (map) {
1932
					kgem_bo_sync__cpu(kgem, shrink);
2095
					kgem_bo_sync__cpu(kgem, shrink);
1933
					memcpy(map, bo->mem, bo->used);
2096
					memcpy(map, bo->mem, bo->used);
1934
 
2097
 
1935
					shrink->target_handle =
2098
					shrink->target_handle =
1936
						kgem->has_handle_lut ? bo->base.target_handle : shrink->handle;
2099
						kgem->has_handle_lut ? bo->base.target_handle : shrink->handle;
1937
					for (n = 0; n < kgem->nreloc; n++) {
2100
					for (n = 0; n < kgem->nreloc; n++) {
1938
						if (kgem->reloc[n].target_handle == bo->base.target_handle) {
2101
						if (kgem->reloc[n].target_handle == bo->base.target_handle) {
1939
							kgem->reloc[n].target_handle = shrink->target_handle;
2102
							kgem->reloc[n].target_handle = shrink->target_handle;
1940
							kgem->reloc[n].presumed_offset = shrink->presumed_offset;
2103
							kgem->reloc[n].presumed_offset = shrink->presumed_offset;
1941
							kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] =
2104
							kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] =
1942
								kgem->reloc[n].delta + shrink->presumed_offset;
2105
								kgem->reloc[n].delta + shrink->presumed_offset;
1943
						}
2106
						}
1944
					}
2107
					}
1945
 
2108
 
1946
					bo->base.exec->handle = shrink->handle;
2109
					bo->base.exec->handle = shrink->handle;
1947
					bo->base.exec->offset = shrink->presumed_offset;
2110
					bo->base.exec->offset = shrink->presumed_offset;
1948
					shrink->exec = bo->base.exec;
2111
					shrink->exec = bo->base.exec;
1949
					shrink->rq = bo->base.rq;
2112
					shrink->rq = bo->base.rq;
1950
					list_replace(&bo->base.request,
2113
					list_replace(&bo->base.request,
1951
						     &shrink->request);
2114
						     &shrink->request);
1952
					list_init(&bo->base.request);
2115
					list_init(&bo->base.request);
1953
					shrink->needs_flush = bo->base.dirty;
2116
					shrink->needs_flush = bo->base.dirty;
1954
 
2117
 
1955
					bo->base.exec = NULL;
2118
					bo->base.exec = NULL;
1956
					bo->base.rq = NULL;
2119
					bo->base.rq = NULL;
1957
					bo->base.dirty = false;
2120
					bo->base.dirty = false;
1958
					bo->base.needs_flush = false;
2121
					bo->base.needs_flush = false;
1959
					bo->used = 0;
2122
					bo->used = 0;
1960
 
2123
 
1961
					goto decouple;
2124
					goto decouple;
1962
				}
2125
				}
1963
 
2126
 
1964
				__kgem_bo_destroy(kgem, shrink);
2127
				__kgem_bo_destroy(kgem, shrink);
1965
			}
2128
			}
1966
 
2129
 
1967
			shrink = search_linear_cache(kgem, alloc,
2130
			shrink = search_linear_cache(kgem, alloc,
1968
						     CREATE_INACTIVE | CREATE_NO_RETIRE);
2131
						     CREATE_INACTIVE | CREATE_NO_RETIRE);
1969
			if (shrink) {
2132
			if (shrink) {
1970
				int n;
2133
				int n;
1971
 
2134
 
1972
				DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n",
2135
				DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n",
1973
				     __FUNCTION__,
2136
				     __FUNCTION__,
1974
				     bo->used, bytes(&bo->base), bytes(shrink),
2137
				     bo->used, bytes(&bo->base), bytes(shrink),
1975
				     bo->base.handle, shrink->handle));
2138
				     bo->base.handle, shrink->handle));
1976
 
2139
 
1977
				assert(bo->used <= bytes(shrink));
2140
				assert(bo->used <= bytes(shrink));
1978
				if (gem_write(kgem->fd, shrink->handle,
2141
				if (gem_write(kgem->fd, shrink->handle,
1979
					      0, bo->used, bo->mem) == 0) {
2142
					      0, bo->used, bo->mem) == 0) {
1980
					shrink->target_handle =
2143
					shrink->target_handle =
1981
						kgem->has_handle_lut ? bo->base.target_handle : shrink->handle;
2144
						kgem->has_handle_lut ? bo->base.target_handle : shrink->handle;
1982
					for (n = 0; n < kgem->nreloc; n++) {
2145
					for (n = 0; n < kgem->nreloc; n++) {
1983
						if (kgem->reloc[n].target_handle == bo->base.target_handle) {
2146
						if (kgem->reloc[n].target_handle == bo->base.target_handle) {
1984
							kgem->reloc[n].target_handle = shrink->target_handle;
2147
							kgem->reloc[n].target_handle = shrink->target_handle;
1985
							kgem->reloc[n].presumed_offset = shrink->presumed_offset;
2148
							kgem->reloc[n].presumed_offset = shrink->presumed_offset;
1986
							kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] =
2149
							kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] =
1987
								kgem->reloc[n].delta + shrink->presumed_offset;
2150
								kgem->reloc[n].delta + shrink->presumed_offset;
1988
						}
2151
						}
1989
					}
2152
					}
1990
 
2153
 
1991
					bo->base.exec->handle = shrink->handle;
2154
					bo->base.exec->handle = shrink->handle;
1992
					bo->base.exec->offset = shrink->presumed_offset;
2155
					bo->base.exec->offset = shrink->presumed_offset;
1993
					shrink->exec = bo->base.exec;
2156
					shrink->exec = bo->base.exec;
1994
					shrink->rq = bo->base.rq;
2157
					shrink->rq = bo->base.rq;
1995
					list_replace(&bo->base.request,
2158
					list_replace(&bo->base.request,
1996
						     &shrink->request);
2159
						     &shrink->request);
1997
					list_init(&bo->base.request);
2160
					list_init(&bo->base.request);
1998
					shrink->needs_flush = bo->base.dirty;
2161
					shrink->needs_flush = bo->base.dirty;
1999
 
2162
 
2000
					bo->base.exec = NULL;
2163
					bo->base.exec = NULL;
2001
					bo->base.rq = NULL;
2164
					bo->base.rq = NULL;
2002
					bo->base.dirty = false;
2165
					bo->base.dirty = false;
2003
					bo->base.needs_flush = false;
2166
					bo->base.needs_flush = false;
2004
					bo->used = 0;
2167
					bo->used = 0;
2005
 
2168
 
2006
					goto decouple;
2169
					goto decouple;
2007
				}
2170
				}
2008
 
2171
 
2009
				__kgem_bo_destroy(kgem, shrink);
2172
				__kgem_bo_destroy(kgem, shrink);
2010
			}
2173
			}
2011
		}
2174
		}
2012
 
2175
 
2013
		DBG(("%s: handle=%d, uploading %d/%d\n",
2176
		DBG(("%s: handle=%d, uploading %d/%d\n",
2014
		     __FUNCTION__, bo->base.handle, bo->used, bytes(&bo->base)));
2177
		     __FUNCTION__, bo->base.handle, bo->used, bytes(&bo->base)));
2015
		ASSERT_IDLE(kgem, bo->base.handle);
2178
		ASSERT_IDLE(kgem, bo->base.handle);
2016
		assert(bo->used <= bytes(&bo->base));
2179
		assert(bo->used <= bytes(&bo->base));
2017
		gem_write(kgem->fd, bo->base.handle,
2180
		gem_write(kgem->fd, bo->base.handle,
2018
			  0, bo->used, bo->mem);
2181
			  0, bo->used, bo->mem);
2019
		bo->need_io = 0;
2182
		bo->need_io = 0;
2020
 
2183
 
2021
decouple:
2184
decouple:
2022
		DBG(("%s: releasing handle=%d\n",
2185
		DBG(("%s: releasing handle=%d\n",
2023
		     __FUNCTION__, bo->base.handle));
2186
		     __FUNCTION__, bo->base.handle));
2024
		list_del(&bo->base.list);
2187
		list_del(&bo->base.list);
2025
		kgem_bo_unref(kgem, &bo->base);
2188
		kgem_bo_unref(kgem, &bo->base);
2026
	}
2189
	}
2027
}
2190
}
2028
 
2191
 
2029
static void kgem_cleanup(struct kgem *kgem)
2192
static void kgem_cleanup(struct kgem *kgem)
2030
{
2193
{
2031
	int n;
2194
	int n;
2032
 
2195
 
2033
	for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) {
2196
	for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) {
2034
		while (!list_is_empty(&kgem->requests[n])) {
2197
		while (!list_is_empty(&kgem->requests[n])) {
2035
			struct kgem_request *rq;
2198
			struct kgem_request *rq;
2036
 
2199
 
2037
			rq = list_first_entry(&kgem->requests[n],
2200
			rq = list_first_entry(&kgem->requests[n],
2038
					      struct kgem_request,
2201
					      struct kgem_request,
2039
					      list);
2202
					      list);
2040
			while (!list_is_empty(&rq->buffers)) {
2203
			while (!list_is_empty(&rq->buffers)) {
2041
				struct kgem_bo *bo;
2204
				struct kgem_bo *bo;
2042
 
2205
 
2043
				bo = list_first_entry(&rq->buffers,
2206
				bo = list_first_entry(&rq->buffers,
2044
						      struct kgem_bo,
2207
						      struct kgem_bo,
2045
						      request);
2208
						      request);
2046
 
2209
 
2047
				bo->exec = NULL;
2210
				bo->exec = NULL;
2048
				bo->dirty = false;
2211
				bo->dirty = false;
2049
				__kgem_bo_clear_busy(bo);
2212
				__kgem_bo_clear_busy(bo);
2050
				if (bo->refcnt == 0)
2213
				if (bo->refcnt == 0)
2051
					kgem_bo_free(kgem, bo);
2214
					kgem_bo_free(kgem, bo);
2052
			}
2215
			}
2053
 
2216
 
2054
			__kgem_request_free(rq);
2217
			__kgem_request_free(rq);
2055
		}
2218
		}
2056
	}
2219
	}
2057
 
2220
 
2058
	kgem_close_inactive(kgem);
2221
	kgem_close_inactive(kgem);
2059
}
2222
}
2060
 
2223
 
2061
static int kgem_batch_write(struct kgem *kgem, uint32_t handle, uint32_t size)
2224
static int kgem_batch_write(struct kgem *kgem, uint32_t handle, uint32_t size)
2062
{
2225
{
2063
	int ret;
2226
	int ret;
2064
 
2227
 
2065
	ASSERT_IDLE(kgem, handle);
2228
	ASSERT_IDLE(kgem, handle);
2066
 
2229
 
2067
	/* If there is no surface data, just upload the batch */
2230
	/* If there is no surface data, just upload the batch */
2068
	if (kgem->surface == kgem->batch_size)
2231
	if (kgem->surface == kgem->batch_size)
2069
		return gem_write(kgem->fd, handle,
2232
		return gem_write(kgem->fd, handle,
2070
				 0, sizeof(uint32_t)*kgem->nbatch,
2233
				 0, sizeof(uint32_t)*kgem->nbatch,
2071
				 kgem->batch);
2234
				 kgem->batch);
2072
 
2235
 
2073
	/* Are the batch pages conjoint with the surface pages? */
2236
	/* Are the batch pages conjoint with the surface pages? */
2074
	if (kgem->surface < kgem->nbatch + PAGE_SIZE/sizeof(uint32_t)) {
2237
	if (kgem->surface < kgem->nbatch + PAGE_SIZE/sizeof(uint32_t)) {
2075
		assert(size == PAGE_ALIGN(kgem->batch_size*sizeof(uint32_t)));
2238
		assert(size == PAGE_ALIGN(kgem->batch_size*sizeof(uint32_t)));
2076
		return gem_write(kgem->fd, handle,
2239
		return gem_write(kgem->fd, handle,
2077
				 0, kgem->batch_size*sizeof(uint32_t),
2240
				 0, kgem->batch_size*sizeof(uint32_t),
2078
				 kgem->batch);
2241
				 kgem->batch);
2079
	}
2242
	}
2080
 
2243
 
2081
	/* Disjoint surface/batch, upload separately */
2244
	/* Disjoint surface/batch, upload separately */
2082
	ret = gem_write(kgem->fd, handle,
2245
	ret = gem_write(kgem->fd, handle,
2083
			0, sizeof(uint32_t)*kgem->nbatch,
2246
			0, sizeof(uint32_t)*kgem->nbatch,
2084
			kgem->batch);
2247
			kgem->batch);
2085
	if (ret)
2248
	if (ret)
2086
		return ret;
2249
		return ret;
2087
 
2250
 
2088
	ret = PAGE_ALIGN(sizeof(uint32_t) * kgem->batch_size);
2251
	ret = PAGE_ALIGN(sizeof(uint32_t) * kgem->batch_size);
2089
	ret -= sizeof(uint32_t) * kgem->surface;
2252
	ret -= sizeof(uint32_t) * kgem->surface;
2090
	assert(size-ret >= kgem->nbatch*sizeof(uint32_t));
2253
	assert(size-ret >= kgem->nbatch*sizeof(uint32_t));
2091
	return __gem_write(kgem->fd, handle,
2254
	return __gem_write(kgem->fd, handle,
2092
			size - ret, (kgem->batch_size - kgem->surface)*sizeof(uint32_t),
2255
			size - ret, (kgem->batch_size - kgem->surface)*sizeof(uint32_t),
2093
			kgem->batch + kgem->surface);
2256
			kgem->batch + kgem->surface);
2094
}
2257
}
2095
 
2258
 
2096
void kgem_reset(struct kgem *kgem)
2259
void kgem_reset(struct kgem *kgem)
2097
{
2260
{
2098
	if (kgem->next_request) {
2261
	if (kgem->next_request) {
2099
		struct kgem_request *rq = kgem->next_request;
2262
		struct kgem_request *rq = kgem->next_request;
2100
 
2263
 
2101
		while (!list_is_empty(&rq->buffers)) {
2264
		while (!list_is_empty(&rq->buffers)) {
2102
			struct kgem_bo *bo =
2265
			struct kgem_bo *bo =
2103
				list_first_entry(&rq->buffers,
2266
				list_first_entry(&rq->buffers,
2104
						 struct kgem_bo,
2267
						 struct kgem_bo,
2105
						 request);
2268
						 request);
2106
			list_del(&bo->request);
2269
			list_del(&bo->request);
2107
 
2270
 
2108
			assert(RQ(bo->rq) == rq);
2271
			assert(RQ(bo->rq) == rq);
2109
 
2272
 
2110
			bo->binding.offset = 0;
2273
			bo->binding.offset = 0;
2111
			bo->exec = NULL;
2274
			bo->exec = NULL;
2112
			bo->target_handle = -1;
2275
			bo->target_handle = -1;
2113
			bo->dirty = false;
2276
			bo->dirty = false;
2114
 
2277
 
2115
			if (bo->needs_flush && __kgem_busy(kgem, bo->handle)) {
2278
			if (bo->needs_flush && __kgem_busy(kgem, bo->handle)) {
2116
				list_add(&bo->request, &kgem->flushing);
2279
				list_add(&bo->request, &kgem->flushing);
2117
				bo->rq = (void *)kgem;
2280
				bo->rq = (void *)kgem;
2118
			} else
2281
			} else
2119
				__kgem_bo_clear_busy(bo);
2282
				__kgem_bo_clear_busy(bo);
2120
 
2283
 
2121
			if (!bo->refcnt && !bo->reusable) {
2284
			if (!bo->refcnt && !bo->reusable) {
2122
				assert(!bo->snoop);
2285
				assert(!bo->snoop);
2123
				DBG(("%s: discarding handle=%d\n",
2286
				DBG(("%s: discarding handle=%d\n",
2124
				     __FUNCTION__, bo->handle));
2287
				     __FUNCTION__, bo->handle));
2125
				kgem_bo_free(kgem, bo);
2288
				kgem_bo_free(kgem, bo);
2126
			}
2289
			}
2127
		}
2290
		}
2128
 
2291
 
2129
		if (rq != &kgem->static_request) {
2292
		if (rq != &kgem->static_request) {
2130
			list_init(&rq->list);
2293
			list_init(&rq->list);
2131
			__kgem_request_free(rq);
2294
			__kgem_request_free(rq);
2132
		}
2295
		}
2133
	}
2296
	}
2134
 
2297
 
2135
	kgem->nfence = 0;
2298
	kgem->nfence = 0;
2136
	kgem->nexec = 0;
2299
	kgem->nexec = 0;
2137
	kgem->nreloc = 0;
2300
	kgem->nreloc = 0;
2138
	kgem->nreloc__self = 0;
2301
	kgem->nreloc__self = 0;
2139
	kgem->aperture = 0;
2302
	kgem->aperture = 0;
2140
	kgem->aperture_fenced = 0;
2303
	kgem->aperture_fenced = 0;
2141
	kgem->nbatch = 0;
2304
	kgem->nbatch = 0;
2142
	kgem->surface = kgem->batch_size;
2305
	kgem->surface = kgem->batch_size;
2143
	kgem->mode = KGEM_NONE;
2306
	kgem->mode = KGEM_NONE;
2144
	kgem->flush = 0;
2307
	kgem->flush = 0;
2145
	kgem->batch_flags = kgem->batch_flags_base;
2308
	kgem->batch_flags = kgem->batch_flags_base;
2146
 
2309
 
2147
	kgem->next_request = __kgem_request_alloc(kgem);
2310
	kgem->next_request = __kgem_request_alloc(kgem);
2148
 
2311
 
2149
	kgem_sna_reset(kgem);
2312
	kgem_sna_reset(kgem);
2150
}
2313
}
2151
 
2314
 
2152
static int compact_batch_surface(struct kgem *kgem)
2315
static int compact_batch_surface(struct kgem *kgem)
2153
{
2316
{
2154
	int size, shrink, n;
2317
	int size, shrink, n;
2155
 
2318
 
2156
	if (!kgem->has_relaxed_delta)
2319
	if (!kgem->has_relaxed_delta)
2157
		return kgem->batch_size;
2320
		return kgem->batch_size;
2158
 
2321
 
2159
	/* See if we can pack the contents into one or two pages */
2322
	/* See if we can pack the contents into one or two pages */
2160
	n = ALIGN(kgem->batch_size, 1024);
2323
	n = ALIGN(kgem->batch_size, 1024);
2161
	size = n - kgem->surface + kgem->nbatch;
2324
	size = n - kgem->surface + kgem->nbatch;
2162
	size = ALIGN(size, 1024);
2325
	size = ALIGN(size, 1024);
2163
 
2326
 
2164
	shrink = n - size;
2327
	shrink = n - size;
2165
	if (shrink) {
2328
	if (shrink) {
2166
		DBG(("shrinking from %d to %d\n", kgem->batch_size, size));
2329
		DBG(("shrinking from %d to %d\n", kgem->batch_size, size));
2167
 
2330
 
2168
		shrink *= sizeof(uint32_t);
2331
		shrink *= sizeof(uint32_t);
2169
		for (n = 0; n < kgem->nreloc; n++) {
2332
		for (n = 0; n < kgem->nreloc; n++) {
2170
			if (kgem->reloc[n].read_domains == I915_GEM_DOMAIN_INSTRUCTION &&
2333
			if (kgem->reloc[n].read_domains == I915_GEM_DOMAIN_INSTRUCTION &&
2171
			    kgem->reloc[n].target_handle == ~0U)
2334
			    kgem->reloc[n].target_handle == ~0U)
2172
				kgem->reloc[n].delta -= shrink;
2335
				kgem->reloc[n].delta -= shrink;
2173
 
2336
 
2174
			if (kgem->reloc[n].offset >= sizeof(uint32_t)*kgem->nbatch)
2337
			if (kgem->reloc[n].offset >= sizeof(uint32_t)*kgem->nbatch)
2175
				kgem->reloc[n].offset -= shrink;
2338
				kgem->reloc[n].offset -= shrink;
2176
		}
2339
		}
2177
	}
2340
	}
2178
 
2341
 
2179
	return size * sizeof(uint32_t);
2342
	return size * sizeof(uint32_t);
2180
}
2343
}
2181
 
2344
 
2182
static struct kgem_bo *
2345
static struct kgem_bo *
2183
kgem_create_batch(struct kgem *kgem, int size)
2346
kgem_create_batch(struct kgem *kgem, int size)
2184
{
2347
{
2185
	struct drm_i915_gem_set_domain set_domain;
2348
	struct drm_i915_gem_set_domain set_domain;
2186
	struct kgem_bo *bo;
2349
	struct kgem_bo *bo;
2187
 
2350
 
2188
	if (size <= 4096) {
2351
	if (size <= 4096) {
2189
		bo = list_first_entry(&kgem->pinned_batches[0],
2352
		bo = list_first_entry(&kgem->pinned_batches[0],
2190
				      struct kgem_bo,
2353
				      struct kgem_bo,
2191
				      list);
2354
				      list);
2192
		if (!bo->rq) {
2355
		if (!bo->rq) {
2193
out_4096:
2356
out_4096:
2194
			list_move_tail(&bo->list, &kgem->pinned_batches[0]);
2357
			list_move_tail(&bo->list, &kgem->pinned_batches[0]);
2195
			return kgem_bo_reference(bo);
2358
			return kgem_bo_reference(bo);
2196
		}
2359
		}
2197
 
2360
 
2198
		if (!__kgem_busy(kgem, bo->handle)) {
2361
		if (!__kgem_busy(kgem, bo->handle)) {
2199
			assert(RQ(bo->rq)->bo == bo);
2362
			assert(RQ(bo->rq)->bo == bo);
2200
			__kgem_retire_rq(kgem, RQ(bo->rq));
2363
			__kgem_retire_rq(kgem, RQ(bo->rq));
2201
			goto out_4096;
2364
			goto out_4096;
2202
		}
2365
		}
2203
	}
2366
	}
2204
 
2367
 
2205
	if (size <= 16384) {
2368
	if (size <= 16384) {
2206
		bo = list_first_entry(&kgem->pinned_batches[1],
2369
		bo = list_first_entry(&kgem->pinned_batches[1],
2207
				      struct kgem_bo,
2370
				      struct kgem_bo,
2208
				      list);
2371
				      list);
2209
		if (!bo->rq) {
2372
		if (!bo->rq) {
2210
out_16384:
2373
out_16384:
2211
			list_move_tail(&bo->list, &kgem->pinned_batches[1]);
2374
			list_move_tail(&bo->list, &kgem->pinned_batches[1]);
2212
			return kgem_bo_reference(bo);
2375
			return kgem_bo_reference(bo);
2213
		}
2376
		}
2214
 
2377
 
2215
		if (!__kgem_busy(kgem, bo->handle)) {
2378
		if (!__kgem_busy(kgem, bo->handle)) {
2216
			assert(RQ(bo->rq)->bo == bo);
2379
			assert(RQ(bo->rq)->bo == bo);
2217
			__kgem_retire_rq(kgem, RQ(bo->rq));
2380
			__kgem_retire_rq(kgem, RQ(bo->rq));
2218
			goto out_16384;
2381
			goto out_16384;
2219
		}
2382
		}
2220
	}
2383
	}
2221
 
2384
 
2222
	if (kgem->gen == 020 && !kgem->has_pinned_batches) {
2385
	if (kgem->gen == 020 && !kgem->has_pinned_batches) {
2223
		assert(size <= 16384);
2386
		assert(size <= 16384);
2224
 
2387
 
2225
		bo = list_first_entry(&kgem->pinned_batches[size > 4096],
2388
		bo = list_first_entry(&kgem->pinned_batches[size > 4096],
2226
				      struct kgem_bo,
2389
				      struct kgem_bo,
2227
				      list);
2390
				      list);
2228
		list_move_tail(&bo->list, &kgem->pinned_batches[size > 4096]);
2391
		list_move_tail(&bo->list, &kgem->pinned_batches[size > 4096]);
2229
 
2392
 
2230
		DBG(("%s: syncing due to busy batches\n", __FUNCTION__));
2393
		DBG(("%s: syncing due to busy batches\n", __FUNCTION__));
2231
 
2394
 
2232
		VG_CLEAR(set_domain);
2395
		VG_CLEAR(set_domain);
2233
		set_domain.handle = bo->handle;
2396
		set_domain.handle = bo->handle;
2234
		set_domain.read_domains = I915_GEM_DOMAIN_GTT;
2397
		set_domain.read_domains = I915_GEM_DOMAIN_GTT;
2235
		set_domain.write_domain = I915_GEM_DOMAIN_GTT;
2398
		set_domain.write_domain = I915_GEM_DOMAIN_GTT;
2236
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) {
2399
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) {
2237
			DBG(("%s: sync: GPU hang detected\n", __FUNCTION__));
2400
			DBG(("%s: sync: GPU hang detected\n", __FUNCTION__));
2238
			kgem_throttle(kgem);
2401
			kgem_throttle(kgem);
2239
			return NULL;
2402
			return NULL;
2240
		}
2403
		}
2241
 
2404
 
2242
		kgem_retire(kgem);
2405
		kgem_retire(kgem);
2243
		assert(bo->rq == NULL);
2406
		assert(bo->rq == NULL);
2244
		return kgem_bo_reference(bo);
2407
		return kgem_bo_reference(bo);
2245
	}
2408
	}
2246
 
2409
 
2247
	return kgem_create_linear(kgem, size, CREATE_NO_THROTTLE);
2410
	return kgem_create_linear(kgem, size, CREATE_NO_THROTTLE);
2248
}
2411
}
2249
 
2412
 
2250
void _kgem_submit(struct kgem *kgem)
2413
void _kgem_submit(struct kgem *kgem)
2251
{
2414
{
2252
	struct kgem_request *rq;
2415
	struct kgem_request *rq;
2253
	uint32_t batch_end;
2416
	uint32_t batch_end;
2254
	int size;
2417
	int size;
2255
 
2418
 
2256
	assert(!DBG_NO_HW);
2419
	assert(!DBG_NO_HW);
2257
	assert(!kgem->wedged);
2420
	assert(!kgem->wedged);
2258
 
2421
 
2259
	assert(kgem->nbatch);
2422
	assert(kgem->nbatch);
2260
	assert(kgem->nbatch <= KGEM_BATCH_SIZE(kgem));
2423
	assert(kgem->nbatch <= KGEM_BATCH_SIZE(kgem));
2261
	assert(kgem->nbatch <= kgem->surface);
2424
	assert(kgem->nbatch <= kgem->surface);
2262
 
2425
 
2263
	batch_end = kgem_end_batch(kgem);
2426
	batch_end = kgem_end_batch(kgem);
2264
	kgem_sna_flush(kgem);
2427
	kgem_sna_flush(kgem);
2265
 
2428
 
2266
	DBG(("batch[%d/%d]: %d %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d\n",
2429
	DBG(("batch[%d/%d]: %d %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d\n",
2267
	     kgem->mode, kgem->ring, batch_end, kgem->nbatch, kgem->surface, kgem->batch_size,
2430
	     kgem->mode, kgem->ring, batch_end, kgem->nbatch, kgem->surface, kgem->batch_size,
2268
	     kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture));
2431
	     kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture));
2269
 
2432
 
2270
	assert(kgem->nbatch <= kgem->batch_size);
2433
	assert(kgem->nbatch <= kgem->batch_size);
2271
	assert(kgem->nbatch <= kgem->surface);
2434
	assert(kgem->nbatch <= kgem->surface);
2272
	assert(kgem->nreloc <= ARRAY_SIZE(kgem->reloc));
2435
	assert(kgem->nreloc <= ARRAY_SIZE(kgem->reloc));
2273
	assert(kgem->nexec < ARRAY_SIZE(kgem->exec));
2436
	assert(kgem->nexec < ARRAY_SIZE(kgem->exec));
2274
	assert(kgem->nfence <= kgem->fence_max);
2437
	assert(kgem->nfence <= kgem->fence_max);
2275
 
2438
 
2276
	kgem_finish_buffers(kgem);
2439
	kgem_finish_buffers(kgem);
2277
 
2440
 
2278
#if SHOW_BATCH
2441
#if SHOW_BATCH
2279
	__kgem_batch_debug(kgem, batch_end);
2442
	__kgem_batch_debug(kgem, batch_end);
2280
#endif
2443
#endif
2281
 
2444
 
2282
	rq = kgem->next_request;
2445
	rq = kgem->next_request;
2283
	if (kgem->surface != kgem->batch_size)
2446
	if (kgem->surface != kgem->batch_size)
2284
		size = compact_batch_surface(kgem);
2447
		size = compact_batch_surface(kgem);
2285
	else
2448
	else
2286
		size = kgem->nbatch * sizeof(kgem->batch[0]);
2449
		size = kgem->nbatch * sizeof(kgem->batch[0]);
2287
	rq->bo = kgem_create_batch(kgem, size);
2450
	rq->bo = kgem_create_batch(kgem, size);
2288
	if (rq->bo) {
2451
	if (rq->bo) {
2289
		uint32_t handle = rq->bo->handle;
2452
		uint32_t handle = rq->bo->handle;
2290
		int i;
2453
		int i;
2291
 
2454
 
2292
		assert(!rq->bo->needs_flush);
2455
		assert(!rq->bo->needs_flush);
2293
 
2456
 
2294
		i = kgem->nexec++;
2457
		i = kgem->nexec++;
2295
		kgem->exec[i].handle = handle;
2458
		kgem->exec[i].handle = handle;
2296
		kgem->exec[i].relocation_count = kgem->nreloc;
2459
		kgem->exec[i].relocation_count = kgem->nreloc;
2297
		kgem->exec[i].relocs_ptr = (uintptr_t)kgem->reloc;
2460
		kgem->exec[i].relocs_ptr = (uintptr_t)kgem->reloc;
2298
		kgem->exec[i].alignment = 0;
2461
		kgem->exec[i].alignment = 0;
2299
		kgem->exec[i].offset = rq->bo->presumed_offset;
2462
		kgem->exec[i].offset = rq->bo->presumed_offset;
2300
		kgem->exec[i].flags = 0;
2463
		kgem->exec[i].flags = 0;
2301
		kgem->exec[i].rsvd1 = 0;
2464
		kgem->exec[i].rsvd1 = 0;
2302
		kgem->exec[i].rsvd2 = 0;
2465
		kgem->exec[i].rsvd2 = 0;
2303
 
2466
 
2304
		rq->bo->target_handle = kgem->has_handle_lut ? i : handle;
2467
		rq->bo->target_handle = kgem->has_handle_lut ? i : handle;
2305
		rq->bo->exec = &kgem->exec[i];
2468
		rq->bo->exec = &kgem->exec[i];
2306
		rq->bo->rq = MAKE_REQUEST(rq, kgem->ring); /* useful sanity check */
2469
		rq->bo->rq = MAKE_REQUEST(rq, kgem->ring); /* useful sanity check */
2307
		list_add(&rq->bo->request, &rq->buffers);
2470
		list_add(&rq->bo->request, &rq->buffers);
2308
		rq->ring = kgem->ring == KGEM_BLT;
2471
		rq->ring = kgem->ring == KGEM_BLT;
2309
 
2472
 
2310
		kgem_fixup_self_relocs(kgem, rq->bo);
2473
		kgem_fixup_self_relocs(kgem, rq->bo);
2311
 
2474
 
2312
		if (kgem_batch_write(kgem, handle, size) == 0) {
2475
		if (kgem_batch_write(kgem, handle, size) == 0) {
2313
			struct drm_i915_gem_execbuffer2 execbuf;
2476
			struct drm_i915_gem_execbuffer2 execbuf;
2314
			int ret, retry = 3;
2477
			int ret, retry = 3;
2315
 
2478
 
2316
			VG_CLEAR(execbuf);
2479
			VG_CLEAR(execbuf);
2317
			execbuf.buffers_ptr = (uintptr_t)kgem->exec;
2480
			execbuf.buffers_ptr = (uintptr_t)kgem->exec;
2318
			execbuf.buffer_count = kgem->nexec;
2481
			execbuf.buffer_count = kgem->nexec;
2319
			execbuf.batch_start_offset = 0;
2482
			execbuf.batch_start_offset = 0;
2320
			execbuf.batch_len = batch_end*sizeof(uint32_t);
2483
			execbuf.batch_len = batch_end*sizeof(uint32_t);
2321
			execbuf.cliprects_ptr = 0;
2484
			execbuf.cliprects_ptr = 0;
2322
			execbuf.num_cliprects = 0;
2485
			execbuf.num_cliprects = 0;
2323
			execbuf.DR1 = 0;
2486
			execbuf.DR1 = 0;
2324
			execbuf.DR4 = 0;
2487
			execbuf.DR4 = 0;
2325
			execbuf.flags = kgem->ring | kgem->batch_flags;
2488
			execbuf.flags = kgem->ring | kgem->batch_flags;
2326
			execbuf.rsvd1 = 0;
2489
			execbuf.rsvd1 = 0;
2327
			execbuf.rsvd2 = 0;
2490
			execbuf.rsvd2 = 0;
2328
 
2491
 
2329
 
2492
 
2330
 
2493
 
2331
//			ret = drmIoctl(kgem->fd,
2494
			ret = drmIoctl(kgem->fd,
2332
//				       DRM_IOCTL_I915_GEM_EXECBUFFER2,
2495
				       DRM_IOCTL_I915_GEM_EXECBUFFER2,
2333
//				       &execbuf);
2496
				       &execbuf);
2334
//			while (ret == -1 && errno == EBUSY && retry--) {
2497
			while (ret == -1 && errno == EBUSY && retry--) {
2335
//				__kgem_throttle(kgem);
2498
				__kgem_throttle(kgem);
2336
//				ret = drmIoctl(kgem->fd,
2499
				ret = drmIoctl(kgem->fd,
2337
//					       DRM_IOCTL_I915_GEM_EXECBUFFER2,
2500
					       DRM_IOCTL_I915_GEM_EXECBUFFER2,
2338
//					       &execbuf);
2501
					       &execbuf);
2339
//			}
2502
			}
2340
			if (DEBUG_SYNC && ret == 0) {
2503
			if (DEBUG_SYNC && ret == 0) {
2341
				struct drm_i915_gem_set_domain set_domain;
2504
				struct drm_i915_gem_set_domain set_domain;
2342
 
2505
 
2343
				VG_CLEAR(set_domain);
2506
				VG_CLEAR(set_domain);
2344
				set_domain.handle = handle;
2507
				set_domain.handle = handle;
2345
				set_domain.read_domains = I915_GEM_DOMAIN_GTT;
2508
				set_domain.read_domains = I915_GEM_DOMAIN_GTT;
2346
				set_domain.write_domain = I915_GEM_DOMAIN_GTT;
2509
				set_domain.write_domain = I915_GEM_DOMAIN_GTT;
2347
 
2510
 
2348
				ret = drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
2511
				ret = drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
2349
			}
2512
			}
2350
			if (ret == -1) {
2513
			if (ret == -1) {
2351
//				DBG(("%s: GPU hang detected [%d]\n",
2514
//				DBG(("%s: GPU hang detected [%d]\n",
2352
//				     __FUNCTION__, errno));
2515
//				     __FUNCTION__, errno));
2353
				kgem_throttle(kgem);
2516
				kgem_throttle(kgem);
2354
				kgem->wedged = true;
2517
				kgem->wedged = true;
2355
 
2518
 
2356
#if 0
2519
#if 0
2357
				ret = errno;
2520
				ret = errno;
2358
				ErrorF("batch[%d/%d]: %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d: errno=%d\n",
2521
				ErrorF("batch[%d/%d]: %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d: errno=%d\n",
2359
				       kgem->mode, kgem->ring, batch_end, kgem->nbatch, kgem->surface,
2522
				       kgem->mode, kgem->ring, batch_end, kgem->nbatch, kgem->surface,
2360
				       kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture, errno);
2523
				       kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture, errno);
2361
 
2524
 
2362
				for (i = 0; i < kgem->nexec; i++) {
2525
				for (i = 0; i < kgem->nexec; i++) {
2363
					struct kgem_bo *bo, *found = NULL;
2526
					struct kgem_bo *bo, *found = NULL;
2364
 
2527
 
2365
					list_for_each_entry(bo, &kgem->next_request->buffers, request) {
2528
					list_for_each_entry(bo, &kgem->next_request->buffers, request) {
2366
						if (bo->handle == kgem->exec[i].handle) {
2529
						if (bo->handle == kgem->exec[i].handle) {
2367
							found = bo;
2530
							found = bo;
2368
							break;
2531
							break;
2369
						}
2532
						}
2370
					}
2533
					}
2371
					ErrorF("exec[%d] = handle:%d, presumed offset: %x, size: %d, tiling %d, fenced %d, snooped %d, deleted %d\n",
2534
					ErrorF("exec[%d] = handle:%d, presumed offset: %x, size: %d, tiling %d, fenced %d, snooped %d, deleted %d\n",
2372
					       i,
2535
					       i,
2373
					       kgem->exec[i].handle,
2536
					       kgem->exec[i].handle,
2374
					       (int)kgem->exec[i].offset,
2537
					       (int)kgem->exec[i].offset,
2375
					       found ? kgem_bo_size(found) : -1,
2538
					       found ? kgem_bo_size(found) : -1,
2376
					       found ? found->tiling : -1,
2539
					       found ? found->tiling : -1,
2377
					       (int)(kgem->exec[i].flags & EXEC_OBJECT_NEEDS_FENCE),
2540
					       (int)(kgem->exec[i].flags & EXEC_OBJECT_NEEDS_FENCE),
2378
					       found ? found->snoop : -1,
2541
					       found ? found->snoop : -1,
2379
					       found ? found->purged : -1);
2542
					       found ? found->purged : -1);
2380
				}
2543
				}
2381
				for (i = 0; i < kgem->nreloc; i++) {
2544
				for (i = 0; i < kgem->nreloc; i++) {
2382
					ErrorF("reloc[%d] = pos:%d, target:%d, delta:%d, read:%x, write:%x, offset:%x\n",
2545
					ErrorF("reloc[%d] = pos:%d, target:%d, delta:%d, read:%x, write:%x, offset:%x\n",
2383
					       i,
2546
					       i,
2384
					       (int)kgem->reloc[i].offset,
2547
					       (int)kgem->reloc[i].offset,
2385
					       kgem->reloc[i].target_handle,
2548
					       kgem->reloc[i].target_handle,
2386
					       kgem->reloc[i].delta,
2549
					       kgem->reloc[i].delta,
2387
					       kgem->reloc[i].read_domains,
2550
					       kgem->reloc[i].read_domains,
2388
					       kgem->reloc[i].write_domain,
2551
					       kgem->reloc[i].write_domain,
2389
					       (int)kgem->reloc[i].presumed_offset);
2552
					       (int)kgem->reloc[i].presumed_offset);
2390
				}
2553
				}
2391
 
2554
 
2392
				if (DEBUG_SYNC) {
2555
				if (DEBUG_SYNC) {
2393
					int fd = open("/tmp/batchbuffer", O_WRONLY | O_CREAT | O_APPEND, 0666);
2556
					int fd = open("/tmp/batchbuffer", O_WRONLY | O_CREAT | O_APPEND, 0666);
2394
					if (fd != -1) {
2557
					if (fd != -1) {
2395
						write(fd, kgem->batch, batch_end*sizeof(uint32_t));
2558
						write(fd, kgem->batch, batch_end*sizeof(uint32_t));
2396
						close(fd);
2559
						close(fd);
2397
					}
2560
					}
2398
 
2561
 
2399
					FatalError("SNA: failed to submit batchbuffer, errno=%d\n", ret);
2562
					FatalError("SNA: failed to submit batchbuffer, errno=%d\n", ret);
2400
				}
2563
				}
2401
#endif
2564
#endif
2402
			}
2565
			}
2403
		}
2566
		}
2404
 
2567
 
2405
		kgem_commit(kgem);
2568
		kgem_commit(kgem);
2406
	}
2569
	}
2407
	if (kgem->wedged)
2570
	if (kgem->wedged)
2408
		kgem_cleanup(kgem);
2571
		kgem_cleanup(kgem);
2409
 
2572
 
2410
	kgem_reset(kgem);
2573
	kgem_reset(kgem);
2411
 
2574
 
2412
	assert(kgem->next_request != NULL);
2575
	assert(kgem->next_request != NULL);
2413
}
2576
}
2414
 
2577
 
2415
void kgem_throttle(struct kgem *kgem)
2578
void kgem_throttle(struct kgem *kgem)
2416
{
2579
{
2417
	kgem->need_throttle = 0;
2580
	kgem->need_throttle = 0;
2418
	if (kgem->wedged)
2581
	if (kgem->wedged)
2419
		return;
2582
		return;
2420
 
2583
 
2421
	kgem->wedged = __kgem_throttle(kgem);
2584
	kgem->wedged = __kgem_throttle(kgem);
2422
	if (kgem->wedged) {
2585
	if (kgem->wedged) {
2423
		printf("Detected a hung GPU, disabling acceleration.\n");
2586
		printf("Detected a hung GPU, disabling acceleration.\n");
2424
		printf("When reporting this, please include i915_error_state from debugfs and the full dmesg.\n");
2587
		printf("When reporting this, please include i915_error_state from debugfs and the full dmesg.\n");
2425
	}
2588
	}
2426
}
2589
}
2427
 
2590
 
2428
void kgem_purge_cache(struct kgem *kgem)
2591
void kgem_purge_cache(struct kgem *kgem)
2429
{
2592
{
2430
	struct kgem_bo *bo, *next;
2593
	struct kgem_bo *bo, *next;
2431
	int i;
2594
	int i;
2432
 
2595
 
2433
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2596
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2434
		list_for_each_entry_safe(bo, next, &kgem->inactive[i], list) {
2597
		list_for_each_entry_safe(bo, next, &kgem->inactive[i], list) {
2435
			if (!kgem_bo_is_retained(kgem, bo)) {
2598
			if (!kgem_bo_is_retained(kgem, bo)) {
2436
				DBG(("%s: purging %d\n",
2599
				DBG(("%s: purging %d\n",
2437
				     __FUNCTION__, bo->handle));
2600
				     __FUNCTION__, bo->handle));
2438
				kgem_bo_free(kgem, bo);
2601
				kgem_bo_free(kgem, bo);
2439
			}
2602
			}
2440
		}
2603
		}
2441
	}
2604
	}
2442
 
2605
 
2443
	kgem->need_purge = false;
2606
	kgem->need_purge = false;
2444
}
2607
}
2445
 
2608
 
2446
bool kgem_expire_cache(struct kgem *kgem)
2609
bool kgem_expire_cache(struct kgem *kgem)
2447
{
2610
{
2448
	time_t now, expire;
2611
	time_t now, expire;
2449
	struct kgem_bo *bo;
2612
	struct kgem_bo *bo;
2450
	unsigned int size = 0, count = 0;
2613
	unsigned int size = 0, count = 0;
2451
	bool idle;
2614
	bool idle;
2452
	unsigned int i;
2615
	unsigned int i;
2453
 
2616
 
2454
	time(&now);
2617
	time(&now);
2455
 
2618
 
2456
	while (__kgem_freed_bo) {
2619
	while (__kgem_freed_bo) {
2457
		bo = __kgem_freed_bo;
2620
		bo = __kgem_freed_bo;
2458
		__kgem_freed_bo = *(struct kgem_bo **)bo;
2621
		__kgem_freed_bo = *(struct kgem_bo **)bo;
2459
		free(bo);
2622
		free(bo);
2460
	}
2623
	}
2461
 
2624
 
2462
	while (__kgem_freed_request) {
2625
	while (__kgem_freed_request) {
2463
		struct kgem_request *rq = __kgem_freed_request;
2626
		struct kgem_request *rq = __kgem_freed_request;
2464
		__kgem_freed_request = *(struct kgem_request **)rq;
2627
		__kgem_freed_request = *(struct kgem_request **)rq;
2465
		free(rq);
2628
		free(rq);
2466
	}
2629
	}
2467
 
2630
 
2468
	while (!list_is_empty(&kgem->large_inactive)) {
2631
	while (!list_is_empty(&kgem->large_inactive)) {
2469
		kgem_bo_free(kgem,
2632
		kgem_bo_free(kgem,
2470
			     list_first_entry(&kgem->large_inactive,
2633
			     list_first_entry(&kgem->large_inactive,
2471
					      struct kgem_bo, list));
2634
					      struct kgem_bo, list));
2472
 
2635
 
2473
	}
2636
	}
2474
 
2637
 
2475
	while (!list_is_empty(&kgem->scanout)) {
2638
	while (!list_is_empty(&kgem->scanout)) {
2476
		bo = list_first_entry(&kgem->scanout, struct kgem_bo, list);
2639
		bo = list_first_entry(&kgem->scanout, struct kgem_bo, list);
2477
		if (__kgem_busy(kgem, bo->handle))
2640
		if (__kgem_busy(kgem, bo->handle))
2478
			break;
2641
			break;
2479
 
2642
 
2480
		list_del(&bo->list);
2643
		list_del(&bo->list);
2481
		kgem_bo_clear_scanout(kgem, bo);
2644
		kgem_bo_clear_scanout(kgem, bo);
2482
		__kgem_bo_destroy(kgem, bo);
2645
		__kgem_bo_destroy(kgem, bo);
2483
	}
2646
	}
2484
 
2647
 
2485
	expire = 0;
2648
	expire = 0;
2486
	list_for_each_entry(bo, &kgem->snoop, list) {
2649
	list_for_each_entry(bo, &kgem->snoop, list) {
2487
		if (bo->delta) {
2650
		if (bo->delta) {
2488
			expire = now - MAX_INACTIVE_TIME/2;
2651
			expire = now - MAX_INACTIVE_TIME/2;
2489
			break;
2652
			break;
2490
		}
2653
		}
2491
 
2654
 
2492
		bo->delta = now;
2655
		bo->delta = now;
2493
	}
2656
	}
2494
	if (expire) {
2657
	if (expire) {
2495
		while (!list_is_empty(&kgem->snoop)) {
2658
		while (!list_is_empty(&kgem->snoop)) {
2496
			bo = list_last_entry(&kgem->snoop, struct kgem_bo, list);
2659
			bo = list_last_entry(&kgem->snoop, struct kgem_bo, list);
2497
 
2660
 
2498
			if (bo->delta > expire)
2661
			if (bo->delta > expire)
2499
				break;
2662
				break;
2500
 
2663
 
2501
			kgem_bo_free(kgem, bo);
2664
			kgem_bo_free(kgem, bo);
2502
		}
2665
		}
2503
	}
2666
	}
2504
#ifdef DEBUG_MEMORY
2667
#ifdef DEBUG_MEMORY
2505
	{
2668
	{
2506
		long snoop_size = 0;
2669
		long snoop_size = 0;
2507
		int snoop_count = 0;
2670
		int snoop_count = 0;
2508
		list_for_each_entry(bo, &kgem->snoop, list)
2671
		list_for_each_entry(bo, &kgem->snoop, list)
2509
			snoop_count++, snoop_size += bytes(bo);
2672
			snoop_count++, snoop_size += bytes(bo);
2510
		ErrorF("%s: still allocated %d bo, %ld bytes, in snoop cache\n",
2673
		ErrorF("%s: still allocated %d bo, %ld bytes, in snoop cache\n",
2511
		       __FUNCTION__, snoop_count, snoop_size);
2674
		       __FUNCTION__, snoop_count, snoop_size);
2512
	}
2675
	}
2513
#endif
2676
#endif
2514
 
2677
 
2515
	kgem_retire(kgem);
2678
	kgem_retire(kgem);
2516
	if (kgem->wedged)
2679
	if (kgem->wedged)
2517
		kgem_cleanup(kgem);
2680
		kgem_cleanup(kgem);
2518
 
2681
 
2519
	kgem->expire(kgem);
2682
	kgem->expire(kgem);
2520
 
2683
 
2521
	if (kgem->need_purge)
2684
	if (kgem->need_purge)
2522
		kgem_purge_cache(kgem);
2685
		kgem_purge_cache(kgem);
2523
 
2686
 
2524
	expire = 0;
2687
	expire = 0;
2525
 
2688
 
2526
	idle = !kgem->need_retire;
2689
	idle = !kgem->need_retire;
2527
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2690
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2528
		idle &= list_is_empty(&kgem->inactive[i]);
2691
		idle &= list_is_empty(&kgem->inactive[i]);
2529
		list_for_each_entry(bo, &kgem->inactive[i], list) {
2692
		list_for_each_entry(bo, &kgem->inactive[i], list) {
2530
			if (bo->delta) {
2693
			if (bo->delta) {
2531
				expire = now - MAX_INACTIVE_TIME;
2694
				expire = now - MAX_INACTIVE_TIME;
2532
				break;
2695
				break;
2533
			}
2696
			}
2534
 
2697
 
2535
			bo->delta = now;
2698
			bo->delta = now;
2536
		}
2699
		}
2537
	}
2700
	}
2538
	if (idle) {
2701
	if (idle) {
2539
		DBG(("%s: idle\n", __FUNCTION__));
2702
		DBG(("%s: idle\n", __FUNCTION__));
2540
		kgem->need_expire = false;
2703
		kgem->need_expire = false;
2541
		return false;
2704
		return false;
2542
	}
2705
	}
2543
	if (expire == 0)
2706
	if (expire == 0)
2544
		return true;
2707
		return true;
2545
 
2708
 
2546
	idle = !kgem->need_retire;
2709
	idle = !kgem->need_retire;
2547
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2710
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2548
		struct list preserve;
2711
		struct list preserve;
2549
 
2712
 
2550
		list_init(&preserve);
2713
		list_init(&preserve);
2551
		while (!list_is_empty(&kgem->inactive[i])) {
2714
		while (!list_is_empty(&kgem->inactive[i])) {
2552
			bo = list_last_entry(&kgem->inactive[i],
2715
			bo = list_last_entry(&kgem->inactive[i],
2553
					     struct kgem_bo, list);
2716
					     struct kgem_bo, list);
2554
 
2717
 
2555
			if (bo->delta > expire) {
2718
			if (bo->delta > expire) {
2556
				idle = false;
2719
				idle = false;
2557
				break;
2720
				break;
2558
			}
2721
			}
2559
 
2722
 
2560
			if (bo->map && bo->delta + MAP_PRESERVE_TIME > expire) {
2723
			if (bo->map && bo->delta + MAP_PRESERVE_TIME > expire) {
2561
				idle = false;
2724
				idle = false;
2562
				list_move_tail(&bo->list, &preserve);
2725
				list_move_tail(&bo->list, &preserve);
2563
			} else {
2726
			} else {
2564
				count++;
2727
				count++;
2565
				size += bytes(bo);
2728
				size += bytes(bo);
2566
				kgem_bo_free(kgem, bo);
2729
				kgem_bo_free(kgem, bo);
2567
				DBG(("%s: expiring %d\n",
2730
				DBG(("%s: expiring %d\n",
2568
				     __FUNCTION__, bo->handle));
2731
				     __FUNCTION__, bo->handle));
2569
			}
2732
			}
2570
		}
2733
		}
2571
		if (!list_is_empty(&preserve)) {
2734
		if (!list_is_empty(&preserve)) {
2572
			preserve.prev->next = kgem->inactive[i].next;
2735
			preserve.prev->next = kgem->inactive[i].next;
2573
			kgem->inactive[i].next->prev = preserve.prev;
2736
			kgem->inactive[i].next->prev = preserve.prev;
2574
			kgem->inactive[i].next = preserve.next;
2737
			kgem->inactive[i].next = preserve.next;
2575
			preserve.next->prev = &kgem->inactive[i];
2738
			preserve.next->prev = &kgem->inactive[i];
2576
		}
2739
		}
2577
	}
2740
	}
2578
 
2741
 
2579
#ifdef DEBUG_MEMORY
2742
#ifdef DEBUG_MEMORY
2580
	{
2743
	{
2581
		long inactive_size = 0;
2744
		long inactive_size = 0;
2582
		int inactive_count = 0;
2745
		int inactive_count = 0;
2583
		for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++)
2746
		for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++)
2584
			list_for_each_entry(bo, &kgem->inactive[i], list)
2747
			list_for_each_entry(bo, &kgem->inactive[i], list)
2585
				inactive_count++, inactive_size += bytes(bo);
2748
				inactive_count++, inactive_size += bytes(bo);
2586
		ErrorF("%s: still allocated %d bo, %ld bytes, in inactive cache\n",
2749
		ErrorF("%s: still allocated %d bo, %ld bytes, in inactive cache\n",
2587
		       __FUNCTION__, inactive_count, inactive_size);
2750
		       __FUNCTION__, inactive_count, inactive_size);
2588
	}
2751
	}
2589
#endif
2752
#endif
2590
 
2753
 
2591
	DBG(("%s: expired %d objects, %d bytes, idle? %d\n",
2754
	DBG(("%s: expired %d objects, %d bytes, idle? %d\n",
2592
	     __FUNCTION__, count, size, idle));
2755
	     __FUNCTION__, count, size, idle));
2593
 
2756
 
2594
	kgem->need_expire = !idle;
2757
	kgem->need_expire = !idle;
2595
	return !idle;
2758
	return !idle;
2596
	(void)count;
2759
	(void)count;
2597
	(void)size;
2760
	(void)size;
2598
}
2761
}
2599
 
2762
 
2600
void kgem_cleanup_cache(struct kgem *kgem)
2763
void kgem_cleanup_cache(struct kgem *kgem)
2601
{
2764
{
2602
	unsigned int i;
2765
	unsigned int i;
2603
	int n;
2766
	int n;
2604
 
2767
 
2605
	/* sync to the most recent request */
2768
	/* sync to the most recent request */
2606
	for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) {
2769
	for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) {
2607
		if (!list_is_empty(&kgem->requests[n])) {
2770
		if (!list_is_empty(&kgem->requests[n])) {
2608
			struct kgem_request *rq;
2771
			struct kgem_request *rq;
2609
			struct drm_i915_gem_set_domain set_domain;
2772
			struct drm_i915_gem_set_domain set_domain;
2610
 
2773
 
2611
			rq = list_first_entry(&kgem->requests[n],
2774
			rq = list_first_entry(&kgem->requests[n],
2612
					      struct kgem_request,
2775
					      struct kgem_request,
2613
					      list);
2776
					      list);
2614
 
2777
 
2615
			DBG(("%s: sync on cleanup\n", __FUNCTION__));
2778
			DBG(("%s: sync on cleanup\n", __FUNCTION__));
2616
 
2779
 
2617
			VG_CLEAR(set_domain);
2780
			VG_CLEAR(set_domain);
2618
			set_domain.handle = rq->bo->handle;
2781
			set_domain.handle = rq->bo->handle;
2619
			set_domain.read_domains = I915_GEM_DOMAIN_GTT;
2782
			set_domain.read_domains = I915_GEM_DOMAIN_GTT;
2620
			set_domain.write_domain = I915_GEM_DOMAIN_GTT;
2783
			set_domain.write_domain = I915_GEM_DOMAIN_GTT;
2621
			(void)drmIoctl(kgem->fd,
2784
			(void)drmIoctl(kgem->fd,
2622
				       DRM_IOCTL_I915_GEM_SET_DOMAIN,
2785
				       DRM_IOCTL_I915_GEM_SET_DOMAIN,
2623
				       &set_domain);
2786
				       &set_domain);
2624
		}
2787
		}
2625
	}
2788
	}
2626
 
2789
 
2627
	kgem_retire(kgem);
2790
	kgem_retire(kgem);
2628
	kgem_cleanup(kgem);
2791
	kgem_cleanup(kgem);
2629
 
2792
 
2630
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2793
	for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) {
2631
		while (!list_is_empty(&kgem->inactive[i]))
2794
		while (!list_is_empty(&kgem->inactive[i]))
2632
			kgem_bo_free(kgem,
2795
			kgem_bo_free(kgem,
2633
				     list_last_entry(&kgem->inactive[i],
2796
				     list_last_entry(&kgem->inactive[i],
2634
						     struct kgem_bo, list));
2797
						     struct kgem_bo, list));
2635
	}
2798
	}
2636
 
2799
 
2637
	while (!list_is_empty(&kgem->snoop))
2800
	while (!list_is_empty(&kgem->snoop))
2638
		kgem_bo_free(kgem,
2801
		kgem_bo_free(kgem,
2639
			     list_last_entry(&kgem->snoop,
2802
			     list_last_entry(&kgem->snoop,
2640
					     struct kgem_bo, list));
2803
					     struct kgem_bo, list));
2641
 
2804
 
2642
	while (__kgem_freed_bo) {
2805
	while (__kgem_freed_bo) {
2643
		struct kgem_bo *bo = __kgem_freed_bo;
2806
		struct kgem_bo *bo = __kgem_freed_bo;
2644
		__kgem_freed_bo = *(struct kgem_bo **)bo;
2807
		__kgem_freed_bo = *(struct kgem_bo **)bo;
2645
		free(bo);
2808
		free(bo);
2646
	}
2809
	}
2647
 
2810
 
2648
	kgem->need_purge = false;
2811
	kgem->need_purge = false;
2649
	kgem->need_expire = false;
2812
	kgem->need_expire = false;
2650
}
2813
}
2651
 
2814
 
2652
static struct kgem_bo *
2815
static struct kgem_bo *
2653
search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags)
2816
search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags)
2654
{
2817
{
2655
	struct kgem_bo *bo, *first = NULL;
2818
	struct kgem_bo *bo, *first = NULL;
2656
	bool use_active = (flags & CREATE_INACTIVE) == 0;
2819
	bool use_active = (flags & CREATE_INACTIVE) == 0;
2657
	struct list *cache;
2820
	struct list *cache;
2658
 
2821
 
2659
	DBG(("%s: num_pages=%d, flags=%x, use_active? %d\n",
2822
	DBG(("%s: num_pages=%d, flags=%x, use_active? %d\n",
2660
	     __FUNCTION__, num_pages, flags, use_active));
2823
	     __FUNCTION__, num_pages, flags, use_active));
2661
 
2824
 
2662
	if (num_pages >= MAX_CACHE_SIZE / PAGE_SIZE)
2825
	if (num_pages >= MAX_CACHE_SIZE / PAGE_SIZE)
2663
		return NULL;
2826
		return NULL;
2664
 
2827
 
2665
	if (!use_active && list_is_empty(inactive(kgem, num_pages))) {
2828
	if (!use_active && list_is_empty(inactive(kgem, num_pages))) {
2666
		DBG(("%s: inactive and cache bucket empty\n",
2829
		DBG(("%s: inactive and cache bucket empty\n",
2667
		     __FUNCTION__));
2830
		     __FUNCTION__));
2668
 
2831
 
2669
		if (flags & CREATE_NO_RETIRE) {
2832
		if (flags & CREATE_NO_RETIRE) {
2670
			DBG(("%s: can not retire\n", __FUNCTION__));
2833
			DBG(("%s: can not retire\n", __FUNCTION__));
2671
			return NULL;
2834
			return NULL;
2672
		}
2835
		}
2673
 
2836
 
2674
		if (list_is_empty(active(kgem, num_pages, I915_TILING_NONE))) {
2837
		if (list_is_empty(active(kgem, num_pages, I915_TILING_NONE))) {
2675
			DBG(("%s: active cache bucket empty\n", __FUNCTION__));
2838
			DBG(("%s: active cache bucket empty\n", __FUNCTION__));
2676
			return NULL;
2839
			return NULL;
2677
		}
2840
		}
2678
 
2841
 
2679
		if (!__kgem_throttle_retire(kgem, flags)) {
2842
		if (!__kgem_throttle_retire(kgem, flags)) {
2680
			DBG(("%s: nothing retired\n", __FUNCTION__));
2843
			DBG(("%s: nothing retired\n", __FUNCTION__));
2681
			return NULL;
2844
			return NULL;
2682
		}
2845
		}
2683
 
2846
 
2684
		if (list_is_empty(inactive(kgem, num_pages))) {
2847
		if (list_is_empty(inactive(kgem, num_pages))) {
2685
			DBG(("%s: active cache bucket still empty after retire\n",
2848
			DBG(("%s: active cache bucket still empty after retire\n",
2686
			     __FUNCTION__));
2849
			     __FUNCTION__));
2687
			return NULL;
2850
			return NULL;
2688
		}
2851
		}
2689
	}
2852
	}
2690
 
2853
 
2691
	if (!use_active && flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
2854
	if (!use_active && flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
2692
		int for_cpu = !!(flags & CREATE_CPU_MAP);
2855
		int for_cpu = !!(flags & CREATE_CPU_MAP);
2693
		DBG(("%s: searching for inactive %s map\n",
2856
		DBG(("%s: searching for inactive %s map\n",
2694
		     __FUNCTION__, for_cpu ? "cpu" : "gtt"));
2857
		     __FUNCTION__, for_cpu ? "cpu" : "gtt"));
2695
		cache = &kgem->vma[for_cpu].inactive[cache_bucket(num_pages)];
2858
		cache = &kgem->vma[for_cpu].inactive[cache_bucket(num_pages)];
2696
		list_for_each_entry(bo, cache, vma) {
2859
		list_for_each_entry(bo, cache, vma) {
2697
			assert(IS_CPU_MAP(bo->map) == for_cpu);
2860
			assert(IS_CPU_MAP(bo->map) == for_cpu);
2698
			assert(bucket(bo) == cache_bucket(num_pages));
2861
			assert(bucket(bo) == cache_bucket(num_pages));
2699
			assert(bo->proxy == NULL);
2862
			assert(bo->proxy == NULL);
2700
			assert(bo->rq == NULL);
2863
			assert(bo->rq == NULL);
2701
			assert(bo->exec == NULL);
2864
			assert(bo->exec == NULL);
2702
			assert(!bo->scanout);
2865
			assert(!bo->scanout);
2703
 
2866
 
2704
			if (num_pages > num_pages(bo)) {
2867
			if (num_pages > num_pages(bo)) {
2705
				DBG(("inactive too small: %d < %d\n",
2868
				DBG(("inactive too small: %d < %d\n",
2706
				     num_pages(bo), num_pages));
2869
				     num_pages(bo), num_pages));
2707
				continue;
2870
				continue;
2708
			}
2871
			}
2709
 
2872
 
2710
			if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
2873
			if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
2711
				kgem_bo_free(kgem, bo);
2874
				kgem_bo_free(kgem, bo);
2712
				break;
2875
				break;
2713
			}
2876
			}
2714
 
2877
 
2715
			if (I915_TILING_NONE != bo->tiling &&
2878
			if (I915_TILING_NONE != bo->tiling &&
2716
			    !gem_set_tiling(kgem->fd, bo->handle,
2879
			    !gem_set_tiling(kgem->fd, bo->handle,
2717
					    I915_TILING_NONE, 0))
2880
					    I915_TILING_NONE, 0))
2718
				continue;
2881
				continue;
2719
 
2882
 
2720
			kgem_bo_remove_from_inactive(kgem, bo);
2883
			kgem_bo_remove_from_inactive(kgem, bo);
2721
 
2884
 
2722
			bo->tiling = I915_TILING_NONE;
2885
			bo->tiling = I915_TILING_NONE;
2723
			bo->pitch = 0;
2886
			bo->pitch = 0;
2724
			bo->delta = 0;
2887
			bo->delta = 0;
2725
			DBG(("  %s: found handle=%d (num_pages=%d) in linear vma cache\n",
2888
			DBG(("  %s: found handle=%d (num_pages=%d) in linear vma cache\n",
2726
			     __FUNCTION__, bo->handle, num_pages(bo)));
2889
			     __FUNCTION__, bo->handle, num_pages(bo)));
2727
			assert(use_active || bo->domain != DOMAIN_GPU);
2890
			assert(use_active || bo->domain != DOMAIN_GPU);
2728
			assert(!bo->needs_flush);
2891
			assert(!bo->needs_flush);
2729
			ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active);
2892
			ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active);
2730
			return bo;
2893
			return bo;
2731
		}
2894
		}
2732
 
2895
 
2733
		if (flags & CREATE_EXACT)
2896
		if (flags & CREATE_EXACT)
2734
			return NULL;
2897
			return NULL;
2735
 
2898
 
2736
		if (flags & CREATE_CPU_MAP && !kgem->has_llc)
2899
		if (flags & CREATE_CPU_MAP && !kgem->has_llc)
2737
			return NULL;
2900
			return NULL;
2738
	}
2901
	}
2739
 
2902
 
2740
	cache = use_active ? active(kgem, num_pages, I915_TILING_NONE) : inactive(kgem, num_pages);
2903
	cache = use_active ? active(kgem, num_pages, I915_TILING_NONE) : inactive(kgem, num_pages);
2741
	list_for_each_entry(bo, cache, list) {
2904
	list_for_each_entry(bo, cache, list) {
2742
		assert(bo->refcnt == 0);
2905
		assert(bo->refcnt == 0);
2743
		assert(bo->reusable);
2906
		assert(bo->reusable);
2744
		assert(!!bo->rq == !!use_active);
2907
		assert(!!bo->rq == !!use_active);
2745
		assert(bo->proxy == NULL);
2908
		assert(bo->proxy == NULL);
2746
		assert(!bo->scanout);
2909
		assert(!bo->scanout);
2747
 
2910
 
2748
		if (num_pages > num_pages(bo))
2911
		if (num_pages > num_pages(bo))
2749
			continue;
2912
			continue;
2750
 
2913
 
2751
		if (use_active &&
2914
		if (use_active &&
2752
		    kgem->gen <= 040 &&
2915
		    kgem->gen <= 040 &&
2753
		    bo->tiling != I915_TILING_NONE)
2916
		    bo->tiling != I915_TILING_NONE)
2754
			continue;
2917
			continue;
2755
 
2918
 
2756
		if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
2919
		if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
2757
			kgem_bo_free(kgem, bo);
2920
			kgem_bo_free(kgem, bo);
2758
			break;
2921
			break;
2759
		}
2922
		}
2760
 
2923
 
2761
		if (I915_TILING_NONE != bo->tiling) {
2924
		if (I915_TILING_NONE != bo->tiling) {
2762
			if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP))
2925
			if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP))
2763
				continue;
2926
				continue;
2764
 
2927
 
2765
			if (first)
2928
			if (first)
2766
				continue;
2929
				continue;
2767
 
2930
 
2768
			if (!gem_set_tiling(kgem->fd, bo->handle,
2931
			if (!gem_set_tiling(kgem->fd, bo->handle,
2769
					    I915_TILING_NONE, 0))
2932
					    I915_TILING_NONE, 0))
2770
				continue;
2933
				continue;
2771
 
2934
 
2772
			bo->tiling = I915_TILING_NONE;
2935
			bo->tiling = I915_TILING_NONE;
2773
			bo->pitch = 0;
2936
			bo->pitch = 0;
2774
		}
2937
		}
2775
 
2938
 
2776
		if (bo->map) {
2939
		if (bo->map) {
2777
			if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
2940
			if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
2778
				int for_cpu = !!(flags & CREATE_CPU_MAP);
2941
				int for_cpu = !!(flags & CREATE_CPU_MAP);
2779
				if (IS_CPU_MAP(bo->map) != for_cpu) {
2942
				if (IS_CPU_MAP(bo->map) != for_cpu) {
2780
					if (first != NULL)
2943
					if (first != NULL)
2781
						break;
2944
						break;
2782
 
2945
 
2783
					first = bo;
2946
					first = bo;
2784
					continue;
2947
					continue;
2785
				}
2948
				}
2786
			} else {
2949
			} else {
2787
				if (first != NULL)
2950
				if (first != NULL)
2788
					break;
2951
					break;
2789
 
2952
 
2790
				first = bo;
2953
				first = bo;
2791
				continue;
2954
				continue;
2792
			}
2955
			}
2793
		} else {
2956
		} else {
2794
			if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
2957
			if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
2795
				if (first != NULL)
2958
				if (first != NULL)
2796
					break;
2959
					break;
2797
 
2960
 
2798
				first = bo;
2961
				first = bo;
2799
				continue;
2962
				continue;
2800
			}
2963
			}
2801
		}
2964
		}
2802
 
2965
 
2803
		if (use_active)
2966
		if (use_active)
2804
			kgem_bo_remove_from_active(kgem, bo);
2967
			kgem_bo_remove_from_active(kgem, bo);
2805
		else
2968
		else
2806
			kgem_bo_remove_from_inactive(kgem, bo);
2969
			kgem_bo_remove_from_inactive(kgem, bo);
2807
 
2970
 
2808
		assert(bo->tiling == I915_TILING_NONE);
2971
		assert(bo->tiling == I915_TILING_NONE);
2809
		bo->pitch = 0;
2972
		bo->pitch = 0;
2810
		bo->delta = 0;
2973
		bo->delta = 0;
2811
		DBG(("  %s: found handle=%d (num_pages=%d) in linear %s cache\n",
2974
		DBG(("  %s: found handle=%d (num_pages=%d) in linear %s cache\n",
2812
		     __FUNCTION__, bo->handle, num_pages(bo),
2975
		     __FUNCTION__, bo->handle, num_pages(bo),
2813
		     use_active ? "active" : "inactive"));
2976
		     use_active ? "active" : "inactive"));
2814
		assert(list_is_empty(&bo->list));
2977
		assert(list_is_empty(&bo->list));
2815
		assert(use_active || bo->domain != DOMAIN_GPU);
2978
		assert(use_active || bo->domain != DOMAIN_GPU);
2816
		assert(!bo->needs_flush || use_active);
2979
		assert(!bo->needs_flush || use_active);
2817
		ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active);
2980
		ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active);
2818
		return bo;
2981
		return bo;
2819
	}
2982
	}
2820
 
2983
 
2821
	if (first) {
2984
	if (first) {
2822
		assert(first->tiling == I915_TILING_NONE);
2985
		assert(first->tiling == I915_TILING_NONE);
2823
 
2986
 
2824
		if (use_active)
2987
		if (use_active)
2825
			kgem_bo_remove_from_active(kgem, first);
2988
			kgem_bo_remove_from_active(kgem, first);
2826
		else
2989
		else
2827
			kgem_bo_remove_from_inactive(kgem, first);
2990
			kgem_bo_remove_from_inactive(kgem, first);
2828
 
2991
 
2829
		first->pitch = 0;
2992
		first->pitch = 0;
2830
		first->delta = 0;
2993
		first->delta = 0;
2831
		DBG(("  %s: found handle=%d (near-miss) (num_pages=%d) in linear %s cache\n",
2994
		DBG(("  %s: found handle=%d (near-miss) (num_pages=%d) in linear %s cache\n",
2832
		     __FUNCTION__, first->handle, num_pages(first),
2995
		     __FUNCTION__, first->handle, num_pages(first),
2833
		     use_active ? "active" : "inactive"));
2996
		     use_active ? "active" : "inactive"));
2834
		assert(list_is_empty(&first->list));
2997
		assert(list_is_empty(&first->list));
2835
		assert(use_active || first->domain != DOMAIN_GPU);
2998
		assert(use_active || first->domain != DOMAIN_GPU);
2836
		assert(!first->needs_flush || use_active);
2999
		assert(!first->needs_flush || use_active);
2837
		ASSERT_MAYBE_IDLE(kgem, first->handle, !use_active);
3000
		ASSERT_MAYBE_IDLE(kgem, first->handle, !use_active);
2838
		return first;
3001
		return first;
2839
	}
3002
	}
2840
 
3003
 
2841
	return NULL;
3004
	return NULL;
2842
}
3005
}
2843
 
3006
 
2844
 
3007
 
2845
struct kgem_bo *kgem_create_linear(struct kgem *kgem, int size, unsigned flags)
3008
struct kgem_bo *kgem_create_linear(struct kgem *kgem, int size, unsigned flags)
2846
{
3009
{
2847
	struct kgem_bo *bo;
3010
	struct kgem_bo *bo;
2848
	uint32_t handle;
3011
	uint32_t handle;
2849
 
3012
 
2850
	DBG(("%s(%d)\n", __FUNCTION__, size));
3013
	DBG(("%s(%d)\n", __FUNCTION__, size));
2851
 
3014
 
2852
	if (flags & CREATE_GTT_MAP && kgem->has_llc) {
3015
	if (flags & CREATE_GTT_MAP && kgem->has_llc) {
2853
		flags &= ~CREATE_GTT_MAP;
3016
		flags &= ~CREATE_GTT_MAP;
2854
		flags |= CREATE_CPU_MAP;
3017
		flags |= CREATE_CPU_MAP;
2855
	}
3018
	}
2856
 
3019
 
2857
	size = (size + PAGE_SIZE - 1) / PAGE_SIZE;
3020
	size = (size + PAGE_SIZE - 1) / PAGE_SIZE;
2858
	bo = search_linear_cache(kgem, size, CREATE_INACTIVE | flags);
3021
	bo = search_linear_cache(kgem, size, CREATE_INACTIVE | flags);
2859
	if (bo) {
3022
	if (bo) {
2860
		assert(bo->domain != DOMAIN_GPU);
3023
		assert(bo->domain != DOMAIN_GPU);
2861
		ASSERT_IDLE(kgem, bo->handle);
3024
		ASSERT_IDLE(kgem, bo->handle);
2862
		bo->refcnt = 1;
3025
		bo->refcnt = 1;
2863
		return bo;
3026
		return bo;
2864
	}
3027
	}
2865
 
3028
 
2866
	if (flags & CREATE_CACHED)
3029
	if (flags & CREATE_CACHED)
2867
		return NULL;
3030
		return NULL;
2868
 
3031
 
2869
	handle = gem_create(kgem->fd, size);
3032
	handle = gem_create(kgem->fd, size);
2870
	if (handle == 0)
3033
	if (handle == 0)
2871
		return NULL;
3034
		return NULL;
2872
 
3035
 
2873
	DBG(("%s: new handle=%d, num_pages=%d\n", __FUNCTION__, handle, size));
3036
	DBG(("%s: new handle=%d, num_pages=%d\n", __FUNCTION__, handle, size));
2874
	bo = __kgem_bo_alloc(handle, size);
3037
	bo = __kgem_bo_alloc(handle, size);
2875
	if (bo == NULL) {
3038
	if (bo == NULL) {
2876
		gem_close(kgem->fd, handle);
3039
		gem_close(kgem->fd, handle);
2877
		return NULL;
3040
		return NULL;
2878
	}
3041
	}
2879
 
3042
 
2880
	debug_alloc__bo(kgem, bo);
3043
	debug_alloc__bo(kgem, bo);
2881
	return bo;
3044
	return bo;
2882
}
3045
}
2883
 
3046
 
2884
inline int kgem_bo_fenced_size(struct kgem *kgem, struct kgem_bo *bo)
3047
inline int kgem_bo_fenced_size(struct kgem *kgem, struct kgem_bo *bo)
2885
{
3048
{
2886
	unsigned int size;
3049
	unsigned int size;
2887
 
3050
 
2888
	assert(bo->tiling);
3051
	assert(bo->tiling);
2889
	assert(kgem->gen < 040);
3052
	assert(kgem->gen < 040);
2890
 
3053
 
2891
	if (kgem->gen < 030)
3054
	if (kgem->gen < 030)
2892
		size = 512 * 1024;
3055
		size = 512 * 1024;
2893
	else
3056
	else
2894
		size = 1024 * 1024;
3057
		size = 1024 * 1024;
2895
	while (size < bytes(bo))
3058
	while (size < bytes(bo))
2896
		size *= 2;
3059
		size *= 2;
2897
 
3060
 
2898
	return size;
3061
	return size;
2899
}
3062
}
2900
 
-
 
2901
#if 0
-
 
2902
 
3063
 
2903
struct kgem_bo *kgem_create_2d(struct kgem *kgem,
3064
struct kgem_bo *kgem_create_2d(struct kgem *kgem,
2904
			       int width,
3065
			       int width,
2905
			       int height,
3066
			       int height,
2906
			       int bpp,
3067
			       int bpp,
2907
			       int tiling,
3068
			       int tiling,
2908
			       uint32_t flags)
3069
			       uint32_t flags)
2909
{
3070
{
2910
	struct list *cache;
3071
	struct list *cache;
2911
	struct kgem_bo *bo;
3072
	struct kgem_bo *bo;
2912
	uint32_t pitch, untiled_pitch, tiled_height, size;
3073
	uint32_t pitch, untiled_pitch, tiled_height, size;
2913
	uint32_t handle;
3074
	uint32_t handle;
2914
	int i, bucket, retry;
3075
	int i, bucket, retry;
2915
 
3076
 
2916
	if (tiling < 0)
3077
	if (tiling < 0)
2917
		tiling = -tiling, flags |= CREATE_EXACT;
3078
		tiling = -tiling, flags |= CREATE_EXACT;
2918
 
3079
 
2919
	DBG(("%s(%dx%d, bpp=%d, tiling=%d, exact=%d, inactive=%d, cpu-mapping=%d, gtt-mapping=%d, scanout?=%d, prime?=%d, temp?=%d)\n", __FUNCTION__,
3080
	DBG(("%s(%dx%d, bpp=%d, tiling=%d, exact=%d, inactive=%d, cpu-mapping=%d, gtt-mapping=%d, scanout?=%d, prime?=%d, temp?=%d)\n", __FUNCTION__,
2920
	     width, height, bpp, tiling,
3081
	     width, height, bpp, tiling,
2921
	     !!(flags & CREATE_EXACT),
3082
	     !!(flags & CREATE_EXACT),
2922
	     !!(flags & CREATE_INACTIVE),
3083
	     !!(flags & CREATE_INACTIVE),
2923
	     !!(flags & CREATE_CPU_MAP),
3084
	     !!(flags & CREATE_CPU_MAP),
2924
	     !!(flags & CREATE_GTT_MAP),
3085
	     !!(flags & CREATE_GTT_MAP),
2925
	     !!(flags & CREATE_SCANOUT),
3086
	     !!(flags & CREATE_SCANOUT),
2926
	     !!(flags & CREATE_PRIME),
3087
	     !!(flags & CREATE_PRIME),
2927
	     !!(flags & CREATE_TEMPORARY)));
3088
	     !!(flags & CREATE_TEMPORARY)));
2928
 
3089
 
2929
	size = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags,
3090
	size = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags,
2930
				 width, height, bpp, tiling, &pitch);
3091
				 width, height, bpp, tiling, &pitch);
2931
	assert(size && size <= kgem->max_object_size);
3092
	assert(size && size <= kgem->max_object_size);
2932
	size /= PAGE_SIZE;
3093
	size /= PAGE_SIZE;
2933
	bucket = cache_bucket(size);
3094
	bucket = cache_bucket(size);
2934
 
3095
 
2935
	if (flags & CREATE_SCANOUT) {
3096
	if (flags & CREATE_SCANOUT) {
2936
		assert((flags & CREATE_INACTIVE) == 0);
3097
		assert((flags & CREATE_INACTIVE) == 0);
2937
		list_for_each_entry_reverse(bo, &kgem->scanout, list) {
3098
		list_for_each_entry_reverse(bo, &kgem->scanout, list) {
2938
			assert(bo->scanout);
3099
			assert(bo->scanout);
2939
			assert(bo->delta);
3100
			assert(bo->delta);
2940
			assert(!bo->purged);
3101
			assert(!bo->purged);
2941
 
3102
 
2942
			if (size > num_pages(bo) || num_pages(bo) > 2*size)
3103
			if (size > num_pages(bo) || num_pages(bo) > 2*size)
2943
				continue;
3104
				continue;
2944
 
3105
 
2945
			if (bo->tiling != tiling ||
3106
			if (bo->tiling != tiling ||
2946
			    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3107
			    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
2947
				if (!gem_set_tiling(kgem->fd, bo->handle,
3108
				if (!gem_set_tiling(kgem->fd, bo->handle,
2948
						    tiling, pitch))
3109
						    tiling, pitch))
2949
					continue;
3110
					continue;
2950
 
3111
 
2951
				bo->tiling = tiling;
3112
				bo->tiling = tiling;
2952
				bo->pitch = pitch;
3113
				bo->pitch = pitch;
2953
			}
3114
			}
2954
 
3115
 
2955
			list_del(&bo->list);
3116
			list_del(&bo->list);
2956
 
3117
 
2957
			bo->unique_id = kgem_get_unique_id(kgem);
3118
			bo->unique_id = kgem_get_unique_id(kgem);
2958
			DBG(("  1:from scanout: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3119
			DBG(("  1:from scanout: pitch=%d, tiling=%d, handle=%d, id=%d\n",
2959
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3120
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
2960
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3121
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
2961
			bo->refcnt = 1;
3122
			bo->refcnt = 1;
2962
			return bo;
3123
			return bo;
2963
		}
3124
		}
2964
	}
3125
	}
2965
 
3126
 
2966
	if (bucket >= NUM_CACHE_BUCKETS) {
3127
	if (bucket >= NUM_CACHE_BUCKETS) {
2967
		DBG(("%s: large bo num pages=%d, bucket=%d\n",
3128
		DBG(("%s: large bo num pages=%d, bucket=%d\n",
2968
		     __FUNCTION__, size, bucket));
3129
		     __FUNCTION__, size, bucket));
2969
 
3130
 
2970
		if (flags & CREATE_INACTIVE)
3131
		if (flags & CREATE_INACTIVE)
2971
			goto large_inactive;
3132
			goto large_inactive;
2972
 
3133
 
2973
		tiled_height = kgem_aligned_height(kgem, height, tiling);
3134
		tiled_height = kgem_aligned_height(kgem, height, tiling);
2974
		untiled_pitch = kgem_untiled_pitch(kgem, width, bpp, flags);
3135
		untiled_pitch = kgem_untiled_pitch(kgem, width, bpp, flags);
2975
 
3136
 
2976
		list_for_each_entry(bo, &kgem->large, list) {
3137
		list_for_each_entry(bo, &kgem->large, list) {
2977
			assert(!bo->purged);
3138
			assert(!bo->purged);
2978
			assert(!bo->scanout);
3139
			assert(!bo->scanout);
2979
			assert(bo->refcnt == 0);
3140
			assert(bo->refcnt == 0);
2980
			assert(bo->reusable);
3141
			assert(bo->reusable);
2981
			assert(bo->flush == true);
3142
			assert(bo->flush == true);
2982
 
3143
 
2983
			if (kgem->gen < 040) {
3144
			if (kgem->gen < 040) {
2984
				if (bo->pitch < pitch) {
3145
				if (bo->pitch < pitch) {
2985
					DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n",
3146
					DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n",
2986
					     bo->tiling, tiling,
3147
					     bo->tiling, tiling,
2987
					     bo->pitch, pitch));
3148
					     bo->pitch, pitch));
2988
					continue;
3149
					continue;
2989
				}
3150
				}
2990
 
3151
 
2991
				if (bo->pitch * tiled_height > bytes(bo))
3152
				if (bo->pitch * tiled_height > bytes(bo))
2992
					continue;
3153
					continue;
2993
			} else {
3154
			} else {
2994
				if (num_pages(bo) < size)
3155
				if (num_pages(bo) < size)
2995
					continue;
3156
					continue;
2996
 
3157
 
2997
				if (bo->pitch != pitch || bo->tiling != tiling) {
3158
				if (bo->pitch != pitch || bo->tiling != tiling) {
2998
					if (!gem_set_tiling(kgem->fd, bo->handle,
3159
					if (!gem_set_tiling(kgem->fd, bo->handle,
2999
							    tiling, pitch))
3160
							    tiling, pitch))
3000
						continue;
3161
						continue;
3001
 
3162
 
3002
					bo->pitch = pitch;
3163
					bo->pitch = pitch;
3003
					bo->tiling = tiling;
3164
					bo->tiling = tiling;
3004
				}
3165
				}
3005
			}
3166
			}
3006
 
3167
 
3007
			kgem_bo_remove_from_active(kgem, bo);
3168
			kgem_bo_remove_from_active(kgem, bo);
3008
 
3169
 
3009
			bo->unique_id = kgem_get_unique_id(kgem);
3170
			bo->unique_id = kgem_get_unique_id(kgem);
3010
			bo->delta = 0;
3171
			bo->delta = 0;
3011
			DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3172
			DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3012
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3173
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3013
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3174
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3014
			bo->refcnt = 1;
3175
			bo->refcnt = 1;
3015
			return bo;
3176
			return bo;
3016
		}
3177
		}
3017
 
3178
 
3018
large_inactive:
3179
large_inactive:
3019
		list_for_each_entry(bo, &kgem->large_inactive, list) {
3180
		list_for_each_entry(bo, &kgem->large_inactive, list) {
3020
			assert(bo->refcnt == 0);
3181
			assert(bo->refcnt == 0);
3021
			assert(bo->reusable);
3182
			assert(bo->reusable);
3022
			assert(!bo->scanout);
3183
			assert(!bo->scanout);
3023
 
3184
 
3024
			if (size > num_pages(bo))
3185
			if (size > num_pages(bo))
3025
				continue;
3186
				continue;
3026
 
3187
 
3027
			if (bo->tiling != tiling ||
3188
			if (bo->tiling != tiling ||
3028
			    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3189
			    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3029
				if (!gem_set_tiling(kgem->fd, bo->handle,
3190
				if (!gem_set_tiling(kgem->fd, bo->handle,
3030
						    tiling, pitch))
3191
						    tiling, pitch))
3031
					continue;
3192
					continue;
3032
 
3193
 
3033
				bo->tiling = tiling;
3194
				bo->tiling = tiling;
3034
				bo->pitch = pitch;
3195
				bo->pitch = pitch;
3035
			}
3196
			}
3036
 
3197
 
3037
			if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
3198
			if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
3038
				kgem_bo_free(kgem, bo);
3199
				kgem_bo_free(kgem, bo);
3039
				break;
3200
				break;
3040
			}
3201
			}
3041
 
3202
 
3042
			list_del(&bo->list);
3203
			list_del(&bo->list);
3043
 
3204
 
3044
			bo->unique_id = kgem_get_unique_id(kgem);
3205
			bo->unique_id = kgem_get_unique_id(kgem);
3045
			bo->pitch = pitch;
3206
			bo->pitch = pitch;
3046
			bo->delta = 0;
3207
			bo->delta = 0;
3047
			DBG(("  1:from large inactive: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3208
			DBG(("  1:from large inactive: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3048
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3209
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3049
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3210
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3050
			bo->refcnt = 1;
3211
			bo->refcnt = 1;
3051
			return bo;
3212
			return bo;
3052
		}
3213
		}
3053
 
3214
 
3054
		goto create;
3215
		goto create;
3055
	}
3216
	}
3056
 
3217
 
3057
	if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
3218
	if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) {
3058
		int for_cpu = !!(flags & CREATE_CPU_MAP);
3219
		int for_cpu = !!(flags & CREATE_CPU_MAP);
3059
		if (kgem->has_llc && tiling == I915_TILING_NONE)
3220
		if (kgem->has_llc && tiling == I915_TILING_NONE)
3060
			for_cpu = 1;
3221
			for_cpu = 1;
3061
		/* We presume that we will need to upload to this bo,
3222
		/* We presume that we will need to upload to this bo,
3062
		 * and so would prefer to have an active VMA.
3223
		 * and so would prefer to have an active VMA.
3063
		 */
3224
		 */
3064
		cache = &kgem->vma[for_cpu].inactive[bucket];
3225
		cache = &kgem->vma[for_cpu].inactive[bucket];
3065
		do {
3226
		do {
3066
			list_for_each_entry(bo, cache, vma) {
3227
			list_for_each_entry(bo, cache, vma) {
3067
				assert(bucket(bo) == bucket);
3228
				assert(bucket(bo) == bucket);
3068
				assert(bo->refcnt == 0);
3229
				assert(bo->refcnt == 0);
3069
				assert(!bo->scanout);
3230
				assert(!bo->scanout);
3070
				assert(bo->map);
3231
				assert(bo->map);
3071
				assert(IS_CPU_MAP(bo->map) == for_cpu);
3232
				assert(IS_CPU_MAP(bo->map) == for_cpu);
3072
				assert(bo->rq == NULL);
3233
				assert(bo->rq == NULL);
3073
				assert(list_is_empty(&bo->request));
3234
				assert(list_is_empty(&bo->request));
3074
				assert(bo->flush == false);
3235
				assert(bo->flush == false);
3075
 
3236
 
3076
				if (size > num_pages(bo)) {
3237
				if (size > num_pages(bo)) {
3077
					DBG(("inactive too small: %d < %d\n",
3238
					DBG(("inactive too small: %d < %d\n",
3078
					     num_pages(bo), size));
3239
					     num_pages(bo), size));
3079
					continue;
3240
					continue;
3080
				}
3241
				}
3081
 
3242
 
3082
				if (bo->tiling != tiling ||
3243
				if (bo->tiling != tiling ||
3083
				    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3244
				    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3084
					DBG(("inactive vma with wrong tiling: %d < %d\n",
3245
					DBG(("inactive vma with wrong tiling: %d < %d\n",
3085
					     bo->tiling, tiling));
3246
					     bo->tiling, tiling));
3086
					continue;
3247
					continue;
3087
				}
3248
				}
3088
 
3249
 
3089
				if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
3250
				if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
3090
					kgem_bo_free(kgem, bo);
3251
					kgem_bo_free(kgem, bo);
3091
					break;
3252
					break;
3092
				}
3253
				}
3093
 
3254
 
3094
				bo->pitch = pitch;
3255
				bo->pitch = pitch;
3095
				bo->delta = 0;
3256
				bo->delta = 0;
3096
				bo->unique_id = kgem_get_unique_id(kgem);
3257
				bo->unique_id = kgem_get_unique_id(kgem);
3097
 
3258
 
3098
				kgem_bo_remove_from_inactive(kgem, bo);
3259
				kgem_bo_remove_from_inactive(kgem, bo);
3099
 
3260
 
3100
				DBG(("  from inactive vma: pitch=%d, tiling=%d: handle=%d, id=%d\n",
3261
				DBG(("  from inactive vma: pitch=%d, tiling=%d: handle=%d, id=%d\n",
3101
				     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3262
				     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3102
				assert(bo->reusable);
3263
				assert(bo->reusable);
3103
				assert(bo->domain != DOMAIN_GPU);
3264
				assert(bo->domain != DOMAIN_GPU);
3104
				ASSERT_IDLE(kgem, bo->handle);
3265
				ASSERT_IDLE(kgem, bo->handle);
3105
				assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3266
				assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3106
				bo->refcnt = 1;
3267
				bo->refcnt = 1;
3107
				return bo;
3268
				return bo;
3108
			}
3269
			}
3109
		} while (!list_is_empty(cache) &&
3270
		} while (!list_is_empty(cache) &&
3110
			 __kgem_throttle_retire(kgem, flags));
3271
			 __kgem_throttle_retire(kgem, flags));
3111
 
3272
 
3112
		if (flags & CREATE_CPU_MAP && !kgem->has_llc)
3273
		if (flags & CREATE_CPU_MAP && !kgem->has_llc)
3113
			goto create;
3274
			goto create;
3114
	}
3275
	}
3115
 
3276
 
3116
	if (flags & CREATE_INACTIVE)
3277
	if (flags & CREATE_INACTIVE)
3117
		goto skip_active_search;
3278
		goto skip_active_search;
3118
 
3279
 
3119
	/* Best active match */
3280
	/* Best active match */
3120
	retry = NUM_CACHE_BUCKETS - bucket;
3281
	retry = NUM_CACHE_BUCKETS - bucket;
3121
	if (retry > 3 && (flags & CREATE_TEMPORARY) == 0)
3282
	if (retry > 3 && (flags & CREATE_TEMPORARY) == 0)
3122
		retry = 3;
3283
		retry = 3;
3123
search_again:
3284
search_again:
3124
	assert(bucket < NUM_CACHE_BUCKETS);
3285
	assert(bucket < NUM_CACHE_BUCKETS);
3125
	cache = &kgem->active[bucket][tiling];
3286
	cache = &kgem->active[bucket][tiling];
3126
	if (tiling) {
3287
	if (tiling) {
3127
		tiled_height = kgem_aligned_height(kgem, height, tiling);
3288
		tiled_height = kgem_aligned_height(kgem, height, tiling);
3128
		list_for_each_entry(bo, cache, list) {
3289
		list_for_each_entry(bo, cache, list) {
3129
			assert(!bo->purged);
3290
			assert(!bo->purged);
3130
			assert(bo->refcnt == 0);
3291
			assert(bo->refcnt == 0);
3131
			assert(bucket(bo) == bucket);
3292
			assert(bucket(bo) == bucket);
3132
			assert(bo->reusable);
3293
			assert(bo->reusable);
3133
			assert(bo->tiling == tiling);
3294
			assert(bo->tiling == tiling);
3134
			assert(bo->flush == false);
3295
			assert(bo->flush == false);
3135
			assert(!bo->scanout);
3296
			assert(!bo->scanout);
3136
 
3297
 
3137
			if (kgem->gen < 040) {
3298
			if (kgem->gen < 040) {
3138
				if (bo->pitch < pitch) {
3299
				if (bo->pitch < pitch) {
3139
					DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n",
3300
					DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n",
3140
					     bo->tiling, tiling,
3301
					     bo->tiling, tiling,
3141
					     bo->pitch, pitch));
3302
					     bo->pitch, pitch));
3142
					continue;
3303
					continue;
3143
				}
3304
				}
3144
 
3305
 
3145
				if (bo->pitch * tiled_height > bytes(bo))
3306
				if (bo->pitch * tiled_height > bytes(bo))
3146
					continue;
3307
					continue;
3147
			} else {
3308
			} else {
3148
				if (num_pages(bo) < size)
3309
				if (num_pages(bo) < size)
3149
					continue;
3310
					continue;
3150
 
3311
 
3151
				if (bo->pitch != pitch) {
3312
				if (bo->pitch != pitch) {
3152
					if (!gem_set_tiling(kgem->fd,
3313
					if (!gem_set_tiling(kgem->fd,
3153
							    bo->handle,
3314
							    bo->handle,
3154
							    tiling, pitch))
3315
							    tiling, pitch))
3155
						continue;
3316
						continue;
3156
 
3317
 
3157
					bo->pitch = pitch;
3318
					bo->pitch = pitch;
3158
				}
3319
				}
3159
			}
3320
			}
3160
 
3321
 
3161
			kgem_bo_remove_from_active(kgem, bo);
3322
			kgem_bo_remove_from_active(kgem, bo);
3162
 
3323
 
3163
			bo->unique_id = kgem_get_unique_id(kgem);
3324
			bo->unique_id = kgem_get_unique_id(kgem);
3164
			bo->delta = 0;
3325
			bo->delta = 0;
3165
			DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3326
			DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3166
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3327
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3167
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3328
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3168
			bo->refcnt = 1;
3329
			bo->refcnt = 1;
3169
			return bo;
3330
			return bo;
3170
		}
3331
		}
3171
	} else {
3332
	} else {
3172
		list_for_each_entry(bo, cache, list) {
3333
		list_for_each_entry(bo, cache, list) {
3173
			assert(bucket(bo) == bucket);
3334
			assert(bucket(bo) == bucket);
3174
			assert(!bo->purged);
3335
			assert(!bo->purged);
3175
			assert(bo->refcnt == 0);
3336
			assert(bo->refcnt == 0);
3176
			assert(bo->reusable);
3337
			assert(bo->reusable);
3177
			assert(!bo->scanout);
3338
			assert(!bo->scanout);
3178
			assert(bo->tiling == tiling);
3339
			assert(bo->tiling == tiling);
3179
			assert(bo->flush == false);
3340
			assert(bo->flush == false);
3180
 
3341
 
3181
			if (num_pages(bo) < size)
3342
			if (num_pages(bo) < size)
3182
				continue;
3343
				continue;
3183
 
3344
 
3184
			kgem_bo_remove_from_active(kgem, bo);
3345
			kgem_bo_remove_from_active(kgem, bo);
3185
 
3346
 
3186
			bo->pitch = pitch;
3347
			bo->pitch = pitch;
3187
			bo->unique_id = kgem_get_unique_id(kgem);
3348
			bo->unique_id = kgem_get_unique_id(kgem);
3188
			bo->delta = 0;
3349
			bo->delta = 0;
3189
			DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3350
			DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3190
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3351
			     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3191
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3352
			assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3192
			bo->refcnt = 1;
3353
			bo->refcnt = 1;
3193
			return bo;
3354
			return bo;
3194
		}
3355
		}
3195
	}
3356
	}
3196
 
3357
 
3197
	if (--retry && flags & CREATE_EXACT) {
3358
	if (--retry && flags & CREATE_EXACT) {
3198
		if (kgem->gen >= 040) {
3359
		if (kgem->gen >= 040) {
3199
			for (i = I915_TILING_NONE; i <= I915_TILING_Y; i++) {
3360
			for (i = I915_TILING_NONE; i <= I915_TILING_Y; i++) {
3200
				if (i == tiling)
3361
				if (i == tiling)
3201
					continue;
3362
					continue;
3202
 
3363
 
3203
				cache = &kgem->active[bucket][i];
3364
				cache = &kgem->active[bucket][i];
3204
				list_for_each_entry(bo, cache, list) {
3365
				list_for_each_entry(bo, cache, list) {
3205
					assert(!bo->purged);
3366
					assert(!bo->purged);
3206
					assert(bo->refcnt == 0);
3367
					assert(bo->refcnt == 0);
3207
					assert(bo->reusable);
3368
					assert(bo->reusable);
3208
					assert(!bo->scanout);
3369
					assert(!bo->scanout);
3209
					assert(bo->flush == false);
3370
					assert(bo->flush == false);
3210
 
3371
 
3211
					if (num_pages(bo) < size)
3372
					if (num_pages(bo) < size)
3212
						continue;
3373
						continue;
3213
 
3374
 
3214
					if (!gem_set_tiling(kgem->fd,
3375
					if (!gem_set_tiling(kgem->fd,
3215
							    bo->handle,
3376
							    bo->handle,
3216
							    tiling, pitch))
3377
							    tiling, pitch))
3217
						continue;
3378
						continue;
3218
 
3379
 
3219
					kgem_bo_remove_from_active(kgem, bo);
3380
					kgem_bo_remove_from_active(kgem, bo);
3220
 
3381
 
3221
					bo->unique_id = kgem_get_unique_id(kgem);
3382
					bo->unique_id = kgem_get_unique_id(kgem);
3222
					bo->pitch = pitch;
3383
					bo->pitch = pitch;
3223
					bo->tiling = tiling;
3384
					bo->tiling = tiling;
3224
					bo->delta = 0;
3385
					bo->delta = 0;
3225
					DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3386
					DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3226
					     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3387
					     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3227
					assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3388
					assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3228
					bo->refcnt = 1;
3389
					bo->refcnt = 1;
3229
					return bo;
3390
					return bo;
3230
				}
3391
				}
3231
			}
3392
			}
3232
		}
3393
		}
3233
 
3394
 
3234
		bucket++;
3395
		bucket++;
3235
		goto search_again;
3396
		goto search_again;
3236
	}
3397
	}
3237
 
3398
 
3238
	if ((flags & CREATE_EXACT) == 0) { /* allow an active near-miss? */
3399
	if ((flags & CREATE_EXACT) == 0) { /* allow an active near-miss? */
3239
		untiled_pitch = kgem_untiled_pitch(kgem, width, bpp, flags);
3400
		untiled_pitch = kgem_untiled_pitch(kgem, width, bpp, flags);
3240
		i = tiling;
3401
		i = tiling;
3241
		while (--i >= 0) {
3402
		while (--i >= 0) {
3242
			tiled_height = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags,
3403
			tiled_height = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags,
3243
							 width, height, bpp, tiling, &pitch);
3404
							 width, height, bpp, tiling, &pitch);
3244
			cache = active(kgem, tiled_height / PAGE_SIZE, i);
3405
			cache = active(kgem, tiled_height / PAGE_SIZE, i);
3245
			tiled_height = kgem_aligned_height(kgem, height, i);
3406
			tiled_height = kgem_aligned_height(kgem, height, i);
3246
			list_for_each_entry(bo, cache, list) {
3407
			list_for_each_entry(bo, cache, list) {
3247
				assert(!bo->purged);
3408
				assert(!bo->purged);
3248
				assert(bo->refcnt == 0);
3409
				assert(bo->refcnt == 0);
3249
				assert(bo->reusable);
3410
				assert(bo->reusable);
3250
				assert(!bo->scanout);
3411
				assert(!bo->scanout);
3251
				assert(bo->flush == false);
3412
				assert(bo->flush == false);
3252
 
3413
 
3253
				if (bo->tiling) {
3414
				if (bo->tiling) {
3254
					if (bo->pitch < pitch) {
3415
					if (bo->pitch < pitch) {
3255
						DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n",
3416
						DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n",
3256
						     bo->tiling, tiling,
3417
						     bo->tiling, tiling,
3257
						     bo->pitch, pitch));
3418
						     bo->pitch, pitch));
3258
						continue;
3419
						continue;
3259
					}
3420
					}
3260
				} else
3421
				} else
3261
					bo->pitch = untiled_pitch;
3422
					bo->pitch = untiled_pitch;
3262
 
3423
 
3263
				if (bo->pitch * tiled_height > bytes(bo))
3424
				if (bo->pitch * tiled_height > bytes(bo))
3264
					continue;
3425
					continue;
3265
 
3426
 
3266
				kgem_bo_remove_from_active(kgem, bo);
3427
				kgem_bo_remove_from_active(kgem, bo);
3267
 
3428
 
3268
				bo->unique_id = kgem_get_unique_id(kgem);
3429
				bo->unique_id = kgem_get_unique_id(kgem);
3269
				bo->delta = 0;
3430
				bo->delta = 0;
3270
				DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3431
				DBG(("  1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n",
3271
				     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3432
				     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3272
				assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3433
				assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3273
				bo->refcnt = 1;
3434
				bo->refcnt = 1;
3274
				return bo;
3435
				return bo;
3275
			}
3436
			}
3276
		}
3437
		}
3277
	}
3438
	}
3278
 
3439
 
3279
skip_active_search:
3440
skip_active_search:
3280
	bucket = cache_bucket(size);
3441
	bucket = cache_bucket(size);
3281
	retry = NUM_CACHE_BUCKETS - bucket;
3442
	retry = NUM_CACHE_BUCKETS - bucket;
3282
	if (retry > 3)
3443
	if (retry > 3)
3283
		retry = 3;
3444
		retry = 3;
3284
search_inactive:
3445
search_inactive:
3285
	/* Now just look for a close match and prefer any currently active */
3446
	/* Now just look for a close match and prefer any currently active */
3286
	assert(bucket < NUM_CACHE_BUCKETS);
3447
	assert(bucket < NUM_CACHE_BUCKETS);
3287
	cache = &kgem->inactive[bucket];
3448
	cache = &kgem->inactive[bucket];
3288
	list_for_each_entry(bo, cache, list) {
3449
	list_for_each_entry(bo, cache, list) {
3289
		assert(bucket(bo) == bucket);
3450
		assert(bucket(bo) == bucket);
3290
		assert(bo->reusable);
3451
		assert(bo->reusable);
3291
		assert(!bo->scanout);
3452
		assert(!bo->scanout);
3292
		assert(bo->flush == false);
3453
		assert(bo->flush == false);
3293
 
3454
 
3294
		if (size > num_pages(bo)) {
3455
		if (size > num_pages(bo)) {
3295
			DBG(("inactive too small: %d < %d\n",
3456
			DBG(("inactive too small: %d < %d\n",
3296
			     num_pages(bo), size));
3457
			     num_pages(bo), size));
3297
			continue;
3458
			continue;
3298
		}
3459
		}
3299
 
3460
 
3300
		if (bo->tiling != tiling ||
3461
		if (bo->tiling != tiling ||
3301
		    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3462
		    (tiling != I915_TILING_NONE && bo->pitch != pitch)) {
3302
			if (!gem_set_tiling(kgem->fd, bo->handle,
3463
			if (!gem_set_tiling(kgem->fd, bo->handle,
3303
					    tiling, pitch))
3464
					    tiling, pitch))
3304
				continue;
3465
				continue;
3305
 
3466
 
3306
			if (bo->map)
3467
			if (bo->map)
3307
				kgem_bo_release_map(kgem, bo);
3468
				kgem_bo_release_map(kgem, bo);
3308
		}
3469
		}
3309
 
3470
 
3310
		if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
3471
		if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) {
3311
			kgem_bo_free(kgem, bo);
3472
			kgem_bo_free(kgem, bo);
3312
			break;
3473
			break;
3313
		}
3474
		}
3314
 
3475
 
3315
		kgem_bo_remove_from_inactive(kgem, bo);
3476
		kgem_bo_remove_from_inactive(kgem, bo);
3316
 
3477
 
3317
		bo->pitch = pitch;
3478
		bo->pitch = pitch;
3318
		bo->tiling = tiling;
3479
		bo->tiling = tiling;
3319
 
3480
 
3320
		bo->delta = 0;
3481
		bo->delta = 0;
3321
		bo->unique_id = kgem_get_unique_id(kgem);
3482
		bo->unique_id = kgem_get_unique_id(kgem);
3322
		assert(bo->pitch);
3483
		assert(bo->pitch);
3323
		DBG(("  from inactive: pitch=%d, tiling=%d: handle=%d, id=%d\n",
3484
		DBG(("  from inactive: pitch=%d, tiling=%d: handle=%d, id=%d\n",
3324
		     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3485
		     bo->pitch, bo->tiling, bo->handle, bo->unique_id));
3325
		assert(bo->refcnt == 0);
3486
		assert(bo->refcnt == 0);
3326
		assert(bo->reusable);
3487
		assert(bo->reusable);
3327
		assert((flags & CREATE_INACTIVE) == 0 || bo->domain != DOMAIN_GPU);
3488
		assert((flags & CREATE_INACTIVE) == 0 || bo->domain != DOMAIN_GPU);
3328
		ASSERT_MAYBE_IDLE(kgem, bo->handle, flags & CREATE_INACTIVE);
3489
		ASSERT_MAYBE_IDLE(kgem, bo->handle, flags & CREATE_INACTIVE);
3329
		assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3490
		assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo));
3330
		bo->refcnt = 1;
3491
		bo->refcnt = 1;
3331
		return bo;
3492
		return bo;
3332
	}
3493
	}
3333
 
3494
 
3334
	if (flags & CREATE_INACTIVE &&
3495
	if (flags & CREATE_INACTIVE &&
3335
	    !list_is_empty(&kgem->active[bucket][tiling]) &&
3496
	    !list_is_empty(&kgem->active[bucket][tiling]) &&
3336
	    __kgem_throttle_retire(kgem, flags)) {
3497
	    __kgem_throttle_retire(kgem, flags)) {
3337
		flags &= ~CREATE_INACTIVE;
3498
		flags &= ~CREATE_INACTIVE;
3338
		goto search_inactive;
3499
		goto search_inactive;
3339
	}
3500
	}
3340
 
3501
 
3341
	if (--retry) {
3502
	if (--retry) {
3342
		bucket++;
3503
		bucket++;
3343
		flags &= ~CREATE_INACTIVE;
3504
		flags &= ~CREATE_INACTIVE;
3344
		goto search_inactive;
3505
		goto search_inactive;
3345
	}
3506
	}
3346
 
3507
 
3347
create:
3508
create:
3348
	if (bucket >= NUM_CACHE_BUCKETS)
3509
	if (bucket >= NUM_CACHE_BUCKETS)
3349
		size = ALIGN(size, 1024);
3510
		size = ALIGN(size, 1024);
3350
	handle = gem_create(kgem->fd, size);
3511
	handle = gem_create(kgem->fd, size);
3351
	if (handle == 0)
3512
	if (handle == 0)
3352
		return NULL;
3513
		return NULL;
3353
 
3514
 
3354
	bo = __kgem_bo_alloc(handle, size);
3515
	bo = __kgem_bo_alloc(handle, size);
3355
	if (!bo) {
3516
	if (!bo) {
3356
		gem_close(kgem->fd, handle);
3517
		gem_close(kgem->fd, handle);
3357
		return NULL;
3518
		return NULL;
3358
	}
3519
	}
3359
 
3520
 
3360
	bo->domain = DOMAIN_CPU;
3521
	bo->domain = DOMAIN_CPU;
3361
	bo->unique_id = kgem_get_unique_id(kgem);
3522
	bo->unique_id = kgem_get_unique_id(kgem);
3362
	bo->pitch = pitch;
3523
	bo->pitch = pitch;
3363
	if (tiling != I915_TILING_NONE &&
3524
	if (tiling != I915_TILING_NONE &&
3364
	    gem_set_tiling(kgem->fd, handle, tiling, pitch))
3525
	    gem_set_tiling(kgem->fd, handle, tiling, pitch))
3365
		bo->tiling = tiling;
3526
		bo->tiling = tiling;
3366
	if (bucket >= NUM_CACHE_BUCKETS) {
3527
	if (bucket >= NUM_CACHE_BUCKETS) {
3367
		DBG(("%s: marking large bo for automatic flushing\n",
3528
		DBG(("%s: marking large bo for automatic flushing\n",
3368
		     __FUNCTION__));
3529
		     __FUNCTION__));
3369
		bo->flush = true;
3530
		bo->flush = true;
3370
	}
3531
	}
3371
 
3532
 
3372
	assert(bytes(bo) >= bo->pitch * kgem_aligned_height(kgem, height, bo->tiling));
3533
	assert(bytes(bo) >= bo->pitch * kgem_aligned_height(kgem, height, bo->tiling));
3373
 
3534
 
3374
	debug_alloc__bo(kgem, bo);
3535
	debug_alloc__bo(kgem, bo);
3375
 
3536
 
3376
	DBG(("  new pitch=%d, tiling=%d, handle=%d, id=%d, num_pages=%d [%d], bucket=%d\n",
3537
	DBG(("  new pitch=%d, tiling=%d, handle=%d, id=%d, num_pages=%d [%d], bucket=%d\n",
3377
	     bo->pitch, bo->tiling, bo->handle, bo->unique_id,
3538
	     bo->pitch, bo->tiling, bo->handle, bo->unique_id,
3378
	     size, num_pages(bo), bucket(bo)));
3539
	     size, num_pages(bo), bucket(bo)));
3379
	return bo;
3540
	return bo;
3380
}
3541
}
-
 
3542
 
3381
 
3543
#if 0
3382
struct kgem_bo *kgem_create_cpu_2d(struct kgem *kgem,
3544
struct kgem_bo *kgem_create_cpu_2d(struct kgem *kgem,
3383
				   int width,
3545
				   int width,
3384
				   int height,
3546
				   int height,
3385
				   int bpp,
3547
				   int bpp,
3386
				   uint32_t flags)
3548
				   uint32_t flags)
3387
{
3549
{
3388
	struct kgem_bo *bo;
3550
	struct kgem_bo *bo;
3389
	int stride, size;
3551
	int stride, size;
3390
 
3552
 
3391
	if (DBG_NO_CPU)
3553
	if (DBG_NO_CPU)
3392
		return NULL;
3554
		return NULL;
3393
 
3555
 
3394
	DBG(("%s(%dx%d, bpp=%d)\n", __FUNCTION__, width, height, bpp));
3556
	DBG(("%s(%dx%d, bpp=%d)\n", __FUNCTION__, width, height, bpp));
3395
 
3557
 
3396
	if (kgem->has_llc) {
3558
	if (kgem->has_llc) {
3397
		bo = kgem_create_2d(kgem, width, height, bpp,
3559
		bo = kgem_create_2d(kgem, width, height, bpp,
3398
				    I915_TILING_NONE, flags);
3560
				    I915_TILING_NONE, flags);
3399
		if (bo == NULL)
3561
		if (bo == NULL)
3400
			return bo;
3562
			return bo;
3401
 
3563
 
3402
		assert(bo->tiling == I915_TILING_NONE);
3564
		assert(bo->tiling == I915_TILING_NONE);
3403
 
3565
 
3404
		if (kgem_bo_map__cpu(kgem, bo) == NULL) {
3566
		if (kgem_bo_map__cpu(kgem, bo) == NULL) {
3405
			kgem_bo_destroy(kgem, bo);
3567
			kgem_bo_destroy(kgem, bo);
3406
			return NULL;
3568
			return NULL;
3407
		}
3569
		}
3408
 
3570
 
3409
		return bo;
3571
		return bo;
3410
	}
3572
	}
3411
 
3573
 
3412
	assert(width > 0 && height > 0);
3574
	assert(width > 0 && height > 0);
3413
	stride = ALIGN(width, 2) * bpp >> 3;
3575
	stride = ALIGN(width, 2) * bpp >> 3;
3414
	stride = ALIGN(stride, 4);
3576
	stride = ALIGN(stride, 4);
3415
	size = stride * ALIGN(height, 2);
3577
	size = stride * ALIGN(height, 2);
3416
	assert(size >= PAGE_SIZE);
3578
	assert(size >= PAGE_SIZE);
3417
 
3579
 
3418
	DBG(("%s: %dx%d, %d bpp, stride=%d\n",
3580
	DBG(("%s: %dx%d, %d bpp, stride=%d\n",
3419
	     __FUNCTION__, width, height, bpp, stride));
3581
	     __FUNCTION__, width, height, bpp, stride));
3420
 
3582
 
3421
	bo = search_snoop_cache(kgem, NUM_PAGES(size), 0);
3583
	bo = search_snoop_cache(kgem, NUM_PAGES(size), 0);
3422
	if (bo) {
3584
	if (bo) {
3423
		assert(bo->tiling == I915_TILING_NONE);
3585
		assert(bo->tiling == I915_TILING_NONE);
3424
		assert(bo->snoop);
3586
		assert(bo->snoop);
3425
		bo->refcnt = 1;
3587
		bo->refcnt = 1;
3426
		bo->pitch = stride;
3588
		bo->pitch = stride;
3427
		bo->unique_id = kgem_get_unique_id(kgem);
3589
		bo->unique_id = kgem_get_unique_id(kgem);
3428
		return bo;
3590
		return bo;
3429
	}
3591
	}
3430
 
3592
 
3431
	if (kgem->has_cacheing) {
3593
	if (kgem->has_cacheing) {
3432
		bo = kgem_create_linear(kgem, size, flags);
3594
		bo = kgem_create_linear(kgem, size, flags);
3433
		if (bo == NULL)
3595
		if (bo == NULL)
3434
			return NULL;
3596
			return NULL;
3435
 
3597
 
3436
		assert(bo->tiling == I915_TILING_NONE);
3598
		assert(bo->tiling == I915_TILING_NONE);
3437
 
3599
 
3438
		if (!gem_set_cacheing(kgem->fd, bo->handle, SNOOPED)) {
3600
		if (!gem_set_cacheing(kgem->fd, bo->handle, SNOOPED)) {
3439
			kgem_bo_destroy(kgem, bo);
3601
			kgem_bo_destroy(kgem, bo);
3440
			return NULL;
3602
			return NULL;
3441
		}
3603
		}
3442
		bo->snoop = true;
3604
		bo->snoop = true;
3443
 
3605
 
3444
		if (kgem_bo_map__cpu(kgem, bo) == NULL) {
3606
		if (kgem_bo_map__cpu(kgem, bo) == NULL) {
3445
			kgem_bo_destroy(kgem, bo);
3607
			kgem_bo_destroy(kgem, bo);
3446
			return NULL;
3608
			return NULL;
3447
		}
3609
		}
3448
 
3610
 
3449
		bo->pitch = stride;
3611
		bo->pitch = stride;
3450
		bo->unique_id = kgem_get_unique_id(kgem);
3612
		bo->unique_id = kgem_get_unique_id(kgem);
3451
		return bo;
3613
		return bo;
3452
	}
3614
	}
3453
 
3615
 
3454
	if (kgem->has_userptr) {
3616
	if (kgem->has_userptr) {
3455
		void *ptr;
3617
		void *ptr;
3456
 
3618
 
3457
		/* XXX */
3619
		/* XXX */
3458
		//if (posix_memalign(&ptr, 64, ALIGN(size, 64)))
3620
		//if (posix_memalign(&ptr, 64, ALIGN(size, 64)))
3459
		if (posix_memalign(&ptr, PAGE_SIZE, ALIGN(size, PAGE_SIZE)))
3621
		if (posix_memalign(&ptr, PAGE_SIZE, ALIGN(size, PAGE_SIZE)))
3460
			return NULL;
3622
			return NULL;
3461
 
3623
 
3462
		bo = kgem_create_map(kgem, ptr, size, false);
3624
		bo = kgem_create_map(kgem, ptr, size, false);
3463
		if (bo == NULL) {
3625
		if (bo == NULL) {
3464
			free(ptr);
3626
			free(ptr);
3465
			return NULL;
3627
			return NULL;
3466
		}
3628
		}
3467
 
3629
 
3468
		bo->map = MAKE_USER_MAP(ptr);
3630
		bo->map = MAKE_USER_MAP(ptr);
3469
		bo->pitch = stride;
3631
		bo->pitch = stride;
3470
		bo->unique_id = kgem_get_unique_id(kgem);
3632
		bo->unique_id = kgem_get_unique_id(kgem);
3471
		return bo;
3633
		return bo;
3472
	}
3634
	}
3473
 
3635
 
3474
		return NULL;
3636
		return NULL;
3475
}
3637
}
3476
 
3638
 
3477
 
3639
 
3478
#endif
3640
#endif
3479
 
3641
 
3480
 
3642
 
3481
void _kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo)
3643
void _kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo)
3482
{
3644
{
3483
	DBG(("%s: handle=%d, proxy? %d\n",
3645
	DBG(("%s: handle=%d, proxy? %d\n",
3484
	     __FUNCTION__, bo->handle, bo->proxy != NULL));
3646
	     __FUNCTION__, bo->handle, bo->proxy != NULL));
3485
 
3647
 
3486
	if (bo->proxy) {
3648
	if (bo->proxy) {
3487
		_list_del(&bo->vma);
3649
		_list_del(&bo->vma);
3488
		_list_del(&bo->request);
3650
		_list_del(&bo->request);
3489
		if (bo->io && bo->exec == NULL)
3651
		if (bo->io && bo->exec == NULL)
3490
			_kgem_bo_delete_buffer(kgem, bo);
3652
			_kgem_bo_delete_buffer(kgem, bo);
3491
		kgem_bo_unref(kgem, bo->proxy);
3653
		kgem_bo_unref(kgem, bo->proxy);
3492
		kgem_bo_binding_free(kgem, bo);
3654
		kgem_bo_binding_free(kgem, bo);
3493
		free(bo);
3655
		free(bo);
3494
		return;
3656
		return;
3495
		}
3657
		}
3496
 
3658
 
3497
	__kgem_bo_destroy(kgem, bo);
3659
	__kgem_bo_destroy(kgem, bo);
3498
}
3660
}
-
 
3661
 
-
 
3662
void __kgem_flush(struct kgem *kgem, struct kgem_bo *bo)
-
 
3663
{
-
 
3664
	assert(bo->rq);
-
 
3665
	assert(bo->exec == NULL);
-
 
3666
	assert(bo->needs_flush);
-
 
3667
 
-
 
3668
	/* The kernel will emit a flush *and* update its own flushing lists. */
-
 
3669
	if (!__kgem_busy(kgem, bo->handle))
-
 
3670
		__kgem_bo_clear_busy(bo);
-
 
3671
 
-
 
3672
	DBG(("%s: handle=%d, busy?=%d\n",
-
 
3673
	     __FUNCTION__, bo->handle, bo->rq != NULL));
-
 
3674
}
-
 
3675
 
-
 
3676
inline static bool needs_semaphore(struct kgem *kgem, struct kgem_bo *bo)
-
 
3677
{
-
 
3678
	return kgem->nreloc && bo->rq && RQ_RING(bo->rq) != kgem->ring;
-
 
3679
}
-
 
3680
 
-
 
3681
bool kgem_check_bo(struct kgem *kgem, ...)
-
 
3682
{
-
 
3683
	va_list ap;
-
 
3684
	struct kgem_bo *bo;
-
 
3685
	int num_exec = 0;
-
 
3686
	int num_pages = 0;
-
 
3687
	bool flush = false;
-
 
3688
 
-
 
3689
	va_start(ap, kgem);
-
 
3690
	while ((bo = va_arg(ap, struct kgem_bo *))) {
-
 
3691
		while (bo->proxy)
-
 
3692
			bo = bo->proxy;
-
 
3693
		if (bo->exec)
-
 
3694
			continue;
-
 
3695
 
-
 
3696
		if (needs_semaphore(kgem, bo))
-
 
3697
			return false;
-
 
3698
 
-
 
3699
		num_pages += num_pages(bo);
-
 
3700
		num_exec++;
-
 
3701
 
-
 
3702
		flush |= bo->flush;
-
 
3703
	}
-
 
3704
	va_end(ap);
-
 
3705
 
-
 
3706
	DBG(("%s: num_pages=+%d, num_exec=+%d\n",
-
 
3707
	     __FUNCTION__, num_pages, num_exec));
-
 
3708
 
-
 
3709
	if (!num_pages)
-
 
3710
		return true;
-
 
3711
 
-
 
3712
	if (kgem_flush(kgem, flush))
-
 
3713
		return false;
-
 
3714
 
-
 
3715
	if (kgem->aperture > kgem->aperture_low &&
-
 
3716
	    kgem_ring_is_idle(kgem, kgem->ring)) {
-
 
3717
		DBG(("%s: current aperture usage (%d) is greater than low water mark (%d)\n",
-
 
3718
		     __FUNCTION__, kgem->aperture, kgem->aperture_low));
-
 
3719
		return false;
-
 
3720
	}
-
 
3721
 
-
 
3722
	if (num_pages + kgem->aperture > kgem->aperture_high) {
-
 
3723
		DBG(("%s: final aperture usage (%d) is greater than high water mark (%d)\n",
-
 
3724
		     __FUNCTION__, num_pages + kgem->aperture, kgem->aperture_high));
-
 
3725
		return false;
-
 
3726
	}
-
 
3727
 
-
 
3728
	if (kgem->nexec + num_exec >= KGEM_EXEC_SIZE(kgem)) {
-
 
3729
		DBG(("%s: out of exec slots (%d + %d / %d)\n", __FUNCTION__,
3499
 
3730
		     kgem->nexec, num_exec, KGEM_EXEC_SIZE(kgem)));
3500
 
3731
		return false;
3501
 
3732
	}
3502
 
3733
 
3503
 
3734
	return true;
3504
 
3735
}
3505
 
3736
 
3506
 
3737
 
3507
 
3738
 
3508
 
3739
 
3509
 
3740
 
3510
 
3741
 
3511
 
3742
 
3512
 
3743
 
3513
 
3744
 
3514
 
3745
 
3515
 
3746
 
3516
 
3747
 
3517
 
3748
 
3518
 
3749
 
3519
 
3750
 
3520
 
3751
 
3521
 
3752
 
3522
 
3753
 
3523
 
3754
 
3524
 
3755
 
3525
 
3756
 
3526
 
3757
 
3527
 
3758
 
3528
 
3759
 
3529
 
3760
 
3530
 
3761
 
3531
 
3762
 
3532
 
3763
 
3533
 
3764
 
3534
 
3765
 
3535
uint32_t kgem_add_reloc(struct kgem *kgem,
3766
uint32_t kgem_add_reloc(struct kgem *kgem,
3536
			uint32_t pos,
3767
			uint32_t pos,
3537
			struct kgem_bo *bo,
3768
			struct kgem_bo *bo,
3538
			uint32_t read_write_domain,
3769
			uint32_t read_write_domain,
3539
			uint32_t delta)
3770
			uint32_t delta)
3540
{
3771
{
3541
	int index;
3772
	int index;
3542
 
3773
 
3543
	DBG(("%s: handle=%d, pos=%d, delta=%d, domains=%08x\n",
3774
	DBG(("%s: handle=%d, pos=%d, delta=%d, domains=%08x\n",
3544
	     __FUNCTION__, bo ? bo->handle : 0, pos, delta, read_write_domain));
3775
	     __FUNCTION__, bo ? bo->handle : 0, pos, delta, read_write_domain));
3545
 
3776
 
3546
	assert((read_write_domain & 0x7fff) == 0 || bo != NULL);
3777
	assert((read_write_domain & 0x7fff) == 0 || bo != NULL);
-
 
3778
 
-
 
3779
//    if( bo != NULL && bo->handle == -1)
-
 
3780
//        return 0;
3547
 
3781
        
3548
	index = kgem->nreloc++;
3782
	index = kgem->nreloc++;
3549
	assert(index < ARRAY_SIZE(kgem->reloc));
3783
	assert(index < ARRAY_SIZE(kgem->reloc));
3550
	kgem->reloc[index].offset = pos * sizeof(kgem->batch[0]);
3784
	kgem->reloc[index].offset = pos * sizeof(kgem->batch[0]);
3551
	if (bo) {
3785
	if (bo) {
3552
		assert(bo->refcnt);
3786
		assert(bo->refcnt);
3553
		assert(!bo->purged);
3787
		assert(!bo->purged);
3554
 
3788
 
3555
		while (bo->proxy) {
3789
		while (bo->proxy) {
3556
			DBG(("%s: adding proxy [delta=%d] for handle=%d\n",
3790
			DBG(("%s: adding proxy [delta=%d] for handle=%d\n",
3557
			     __FUNCTION__, bo->delta, bo->handle));
3791
			     __FUNCTION__, bo->delta, bo->handle));
3558
			delta += bo->delta;
3792
			delta += bo->delta;
3559
			assert(bo->handle == bo->proxy->handle);
3793
			assert(bo->handle == bo->proxy->handle);
3560
			/* need to release the cache upon batch submit */
3794
			/* need to release the cache upon batch submit */
3561
			if (bo->exec == NULL) {
3795
			if (bo->exec == NULL) {
3562
				list_move_tail(&bo->request,
3796
				list_move_tail(&bo->request,
3563
					       &kgem->next_request->buffers);
3797
					       &kgem->next_request->buffers);
3564
				bo->rq = MAKE_REQUEST(kgem->next_request,
3798
				bo->rq = MAKE_REQUEST(kgem->next_request,
3565
						      kgem->ring);
3799
						      kgem->ring);
3566
				bo->exec = &_kgem_dummy_exec;
3800
				bo->exec = &_kgem_dummy_exec;
3567
		}
3801
		}
3568
 
3802
 
3569
			if (read_write_domain & 0x7fff && !bo->dirty)
3803
			if (read_write_domain & 0x7fff && !bo->dirty)
3570
				__kgem_bo_mark_dirty(bo);
3804
				__kgem_bo_mark_dirty(bo);
3571
 
3805
 
3572
			bo = bo->proxy;
3806
			bo = bo->proxy;
3573
			assert(bo->refcnt);
3807
			assert(bo->refcnt);
3574
			assert(!bo->purged);
3808
			assert(!bo->purged);
3575
		}
3809
		}
3576
 
3810
 
3577
		if (bo->exec == NULL)
3811
		if (bo->exec == NULL)
3578
			kgem_add_bo(kgem, bo);
3812
			kgem_add_bo(kgem, bo);
3579
		assert(bo->rq == MAKE_REQUEST(kgem->next_request, kgem->ring));
3813
		assert(bo->rq == MAKE_REQUEST(kgem->next_request, kgem->ring));
3580
		assert(RQ_RING(bo->rq) == kgem->ring);
3814
		assert(RQ_RING(bo->rq) == kgem->ring);
3581
 
3815
 
3582
		if (kgem->gen < 040 && read_write_domain & KGEM_RELOC_FENCED) {
3816
		if (kgem->gen < 040 && read_write_domain & KGEM_RELOC_FENCED) {
3583
			if (bo->tiling &&
3817
			if (bo->tiling &&
3584
			    (bo->exec->flags & EXEC_OBJECT_NEEDS_FENCE) == 0) {
3818
			    (bo->exec->flags & EXEC_OBJECT_NEEDS_FENCE) == 0) {
3585
				assert(kgem->nfence < kgem->fence_max);
3819
				assert(kgem->nfence < kgem->fence_max);
3586
				kgem->aperture_fenced +=
3820
				kgem->aperture_fenced +=
3587
					kgem_bo_fenced_size(kgem, bo);
3821
					kgem_bo_fenced_size(kgem, bo);
3588
				kgem->nfence++;
3822
				kgem->nfence++;
3589
			}
3823
			}
3590
			bo->exec->flags |= EXEC_OBJECT_NEEDS_FENCE;
3824
			bo->exec->flags |= EXEC_OBJECT_NEEDS_FENCE;
3591
		}
3825
		}
3592
 
3826
 
3593
		kgem->reloc[index].delta = delta;
3827
		kgem->reloc[index].delta = delta;
3594
		kgem->reloc[index].target_handle = bo->target_handle;
3828
		kgem->reloc[index].target_handle = bo->target_handle;
3595
		kgem->reloc[index].presumed_offset = bo->presumed_offset;
3829
		kgem->reloc[index].presumed_offset = bo->presumed_offset;
3596
 
3830
 
3597
		if (read_write_domain & 0x7fff && !bo->dirty) {
3831
		if (read_write_domain & 0x7fff && !bo->dirty) {
3598
			assert(!bo->snoop || kgem->can_blt_cpu);
3832
			assert(!bo->snoop || kgem->can_blt_cpu);
3599
			__kgem_bo_mark_dirty(bo);
3833
			__kgem_bo_mark_dirty(bo);
3600
		}
3834
		}
3601
 
3835
 
3602
		delta += bo->presumed_offset;
3836
		delta += bo->presumed_offset;
3603
	} else {
3837
	} else {
3604
		kgem->reloc[index].delta = delta;
3838
		kgem->reloc[index].delta = delta;
3605
		kgem->reloc[index].target_handle = ~0U;
3839
		kgem->reloc[index].target_handle = ~0U;
3606
		kgem->reloc[index].presumed_offset = 0;
3840
		kgem->reloc[index].presumed_offset = 0;
3607
		if (kgem->nreloc__self < 256)
3841
		if (kgem->nreloc__self < 256)
3608
			kgem->reloc__self[kgem->nreloc__self++] = index;
3842
			kgem->reloc__self[kgem->nreloc__self++] = index;
3609
		}
3843
		}
3610
	kgem->reloc[index].read_domains = read_write_domain >> 16;
3844
	kgem->reloc[index].read_domains = read_write_domain >> 16;
3611
	kgem->reloc[index].write_domain = read_write_domain & 0x7fff;
3845
	kgem->reloc[index].write_domain = read_write_domain & 0x7fff;
3612
 
3846
 
3613
	return delta;
3847
	return delta;
3614
}
3848
}
3615
 
3849
 
3616
static void kgem_trim_vma_cache(struct kgem *kgem, int type, int bucket)
3850
static void kgem_trim_vma_cache(struct kgem *kgem, int type, int bucket)
3617
{
3851
{
3618
	int i, j;
3852
	int i, j;
3619
 
3853
 
3620
	DBG(("%s: type=%d, count=%d (bucket: %d)\n",
3854
	DBG(("%s: type=%d, count=%d (bucket: %d)\n",
3621
	     __FUNCTION__, type, kgem->vma[type].count, bucket));
3855
	     __FUNCTION__, type, kgem->vma[type].count, bucket));
3622
	if (kgem->vma[type].count <= 0)
3856
	if (kgem->vma[type].count <= 0)
3623
	       return;
3857
	       return;
3624
 
3858
 
3625
	if (kgem->need_purge)
3859
	if (kgem->need_purge)
3626
		kgem_purge_cache(kgem);
3860
		kgem_purge_cache(kgem);
3627
 
3861
 
3628
	/* vma are limited on a per-process basis to around 64k.
3862
	/* vma are limited on a per-process basis to around 64k.
3629
	 * This includes all malloc arenas as well as other file
3863
	 * This includes all malloc arenas as well as other file
3630
	 * mappings. In order to be fair and not hog the cache,
3864
	 * mappings. In order to be fair and not hog the cache,
3631
	 * and more importantly not to exhaust that limit and to
3865
	 * and more importantly not to exhaust that limit and to
3632
	 * start failing mappings, we keep our own number of open
3866
	 * start failing mappings, we keep our own number of open
3633
	 * vma to within a conservative value.
3867
	 * vma to within a conservative value.
3634
	 */
3868
	 */
3635
	i = 0;
3869
	i = 0;
3636
	while (kgem->vma[type].count > 0) {
3870
	while (kgem->vma[type].count > 0) {
3637
		struct kgem_bo *bo = NULL;
3871
		struct kgem_bo *bo = NULL;
3638
 
3872
 
3639
		for (j = 0;
3873
		for (j = 0;
3640
		     bo == NULL && j < ARRAY_SIZE(kgem->vma[type].inactive);
3874
		     bo == NULL && j < ARRAY_SIZE(kgem->vma[type].inactive);
3641
		     j++) {
3875
		     j++) {
3642
			struct list *head = &kgem->vma[type].inactive[i++%ARRAY_SIZE(kgem->vma[type].inactive)];
3876
			struct list *head = &kgem->vma[type].inactive[i++%ARRAY_SIZE(kgem->vma[type].inactive)];
3643
			if (!list_is_empty(head))
3877
			if (!list_is_empty(head))
3644
				bo = list_last_entry(head, struct kgem_bo, vma);
3878
				bo = list_last_entry(head, struct kgem_bo, vma);
3645
	}
3879
	}
3646
		if (bo == NULL)
3880
		if (bo == NULL)
3647
			break;
3881
			break;
3648
 
3882
 
3649
		DBG(("%s: discarding inactive %s vma cache for %d\n",
3883
		DBG(("%s: discarding inactive %s vma cache for %d\n",
3650
		     __FUNCTION__,
3884
		     __FUNCTION__,
3651
		     IS_CPU_MAP(bo->map) ? "CPU" : "GTT", bo->handle));
3885
		     IS_CPU_MAP(bo->map) ? "CPU" : "GTT", bo->handle));
3652
		assert(IS_CPU_MAP(bo->map) == type);
3886
		assert(IS_CPU_MAP(bo->map) == type);
3653
		assert(bo->map);
3887
		assert(bo->map);
3654
			assert(bo->rq == NULL);
3888
			assert(bo->rq == NULL);
3655
 
3889
 
3656
		VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo)));
3890
		VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo)));
3657
//		munmap(MAP(bo->map), bytes(bo));
3891
//		munmap(MAP(bo->map), bytes(bo));
3658
		bo->map = NULL;
3892
		bo->map = NULL;
3659
		list_del(&bo->vma);
3893
		list_del(&bo->vma);
3660
		kgem->vma[type].count--;
3894
		kgem->vma[type].count--;
3661
 
3895
 
3662
		if (!bo->purged && !kgem_bo_set_purgeable(kgem, bo)) {
3896
		if (!bo->purged && !kgem_bo_set_purgeable(kgem, bo)) {
3663
			DBG(("%s: freeing unpurgeable old mapping\n",
3897
			DBG(("%s: freeing unpurgeable old mapping\n",
3664
			     __FUNCTION__));
3898
			     __FUNCTION__));
3665
				kgem_bo_free(kgem, bo);
3899
				kgem_bo_free(kgem, bo);
3666
			}
3900
			}
3667
	}
3901
	}
3668
}
3902
}
3669
 
3903
 
3670
 
3904
 
3671
void *kgem_bo_map(struct kgem *kgem, struct kgem_bo *bo)
3905
void *kgem_bo_map(struct kgem *kgem, struct kgem_bo *bo)
3672
{
3906
{
3673
	void *ptr;
3907
	void *ptr;
3674
 
3908
 
3675
	DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__,
3909
	DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__,
3676
	     bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain));
3910
	     bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain));
3677
 
3911
 
3678
	assert(!bo->purged);
3912
	assert(!bo->purged);
3679
	assert(bo->proxy == NULL);
3913
	assert(bo->proxy == NULL);
3680
	assert(list_is_empty(&bo->list));
3914
	assert(list_is_empty(&bo->list));
3681
	assert(bo->exec == NULL);
3915
	assert(bo->exec == NULL);
3682
 
3916
 
3683
	if (bo->tiling == I915_TILING_NONE && !bo->scanout &&
3917
	if (bo->tiling == I915_TILING_NONE && !bo->scanout &&
3684
	    (kgem->has_llc || bo->domain == DOMAIN_CPU)) {
3918
	    (kgem->has_llc || bo->domain == DOMAIN_CPU)) {
3685
		DBG(("%s: converting request for GTT map into CPU map\n",
3919
		DBG(("%s: converting request for GTT map into CPU map\n",
3686
		     __FUNCTION__));
3920
		     __FUNCTION__));
3687
		ptr = kgem_bo_map__cpu(kgem, bo);
3921
		ptr = kgem_bo_map__cpu(kgem, bo);
3688
		kgem_bo_sync__cpu(kgem, bo);
3922
		kgem_bo_sync__cpu(kgem, bo);
3689
		return ptr;
3923
		return ptr;
3690
	}
3924
	}
3691
 
3925
 
3692
	if (IS_CPU_MAP(bo->map))
3926
	if (IS_CPU_MAP(bo->map))
3693
		kgem_bo_release_map(kgem, bo);
3927
		kgem_bo_release_map(kgem, bo);
3694
 
3928
 
3695
	ptr = bo->map;
3929
	ptr = bo->map;
3696
	if (ptr == NULL) {
3930
	if (ptr == NULL) {
3697
		assert(kgem_bo_size(bo) <= kgem->aperture_mappable / 2);
3931
		assert(kgem_bo_size(bo) <= kgem->aperture_mappable / 2);
3698
		assert(kgem->gen != 021 || bo->tiling != I915_TILING_Y);
3932
		assert(kgem->gen != 021 || bo->tiling != I915_TILING_Y);
3699
 
3933
 
3700
		kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo));
3934
		kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo));
3701
 
3935
 
3702
		ptr = __kgem_bo_map__gtt(kgem, bo);
3936
		ptr = __kgem_bo_map__gtt(kgem, bo);
3703
		if (ptr == NULL)
3937
		if (ptr == NULL)
3704
			return NULL;
3938
			return NULL;
3705
 
3939
 
3706
		/* Cache this mapping to avoid the overhead of an
3940
		/* Cache this mapping to avoid the overhead of an
3707
		 * excruciatingly slow GTT pagefault. This is more an
3941
		 * excruciatingly slow GTT pagefault. This is more an
3708
		 * issue with compositing managers which need to frequently
3942
		 * issue with compositing managers which need to frequently
3709
		 * flush CPU damage to their GPU bo.
3943
		 * flush CPU damage to their GPU bo.
3710
		 */
3944
		 */
3711
		bo->map = ptr;
3945
		bo->map = ptr;
3712
		DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle));
3946
		DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle));
3713
		}
3947
		}
3714
 
3948
 
3715
	if (bo->domain != DOMAIN_GTT) {
3949
	if (bo->domain != DOMAIN_GTT) {
3716
		struct drm_i915_gem_set_domain set_domain;
3950
		struct drm_i915_gem_set_domain set_domain;
3717
 
3951
 
3718
		DBG(("%s: sync: needs_flush? %d, domain? %d, busy? %d\n", __FUNCTION__,
3952
		DBG(("%s: sync: needs_flush? %d, domain? %d, busy? %d\n", __FUNCTION__,
3719
		     bo->needs_flush, bo->domain, __kgem_busy(kgem, bo->handle)));
3953
		     bo->needs_flush, bo->domain, __kgem_busy(kgem, bo->handle)));
3720
 
3954
 
3721
		/* XXX use PROT_READ to avoid the write flush? */
3955
		/* XXX use PROT_READ to avoid the write flush? */
3722
 
3956
 
3723
		VG_CLEAR(set_domain);
3957
		VG_CLEAR(set_domain);
3724
		set_domain.handle = bo->handle;
3958
		set_domain.handle = bo->handle;
3725
		set_domain.read_domains = I915_GEM_DOMAIN_GTT;
3959
		set_domain.read_domains = I915_GEM_DOMAIN_GTT;
3726
		set_domain.write_domain = I915_GEM_DOMAIN_GTT;
3960
		set_domain.write_domain = I915_GEM_DOMAIN_GTT;
3727
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) {
3961
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) {
3728
			kgem_bo_retire(kgem, bo);
3962
			kgem_bo_retire(kgem, bo);
3729
			bo->domain = DOMAIN_GTT;
3963
			bo->domain = DOMAIN_GTT;
3730
		}
3964
		}
3731
		}
3965
		}
3732
 
3966
 
3733
	return ptr;
3967
	return ptr;
3734
}
3968
}
3735
 
3969
 
3736
void *kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo)
3970
void *kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo)
3737
{
3971
{
3738
	void *ptr;
3972
	void *ptr;
3739
 
3973
 
3740
	DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__,
3974
	DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__,
3741
	     bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain));
3975
	     bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain));
3742
 
3976
 
3743
	assert(!bo->purged);
3977
	assert(!bo->purged);
3744
	assert(bo->exec == NULL);
3978
	assert(bo->exec == NULL);
3745
	assert(list_is_empty(&bo->list));
3979
	assert(list_is_empty(&bo->list));
3746
 
3980
 
3747
	if (IS_CPU_MAP(bo->map))
3981
	if (IS_CPU_MAP(bo->map))
3748
		kgem_bo_release_map(kgem, bo);
3982
		kgem_bo_release_map(kgem, bo);
3749
 
3983
 
3750
	ptr = bo->map;
3984
	ptr = bo->map;
3751
	if (ptr == NULL) {
3985
	if (ptr == NULL) {
3752
		assert(bytes(bo) <= kgem->aperture_mappable / 4);
3986
		assert(bytes(bo) <= kgem->aperture_mappable / 4);
3753
 
3987
 
3754
		kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo));
3988
		kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo));
3755
 
3989
 
3756
		ptr = __kgem_bo_map__gtt(kgem, bo);
3990
		ptr = __kgem_bo_map__gtt(kgem, bo);
3757
		if (ptr == NULL)
3991
		if (ptr == NULL)
3758
			return NULL;
3992
			return NULL;
3759
 
3993
 
3760
		/* Cache this mapping to avoid the overhead of an
3994
		/* Cache this mapping to avoid the overhead of an
3761
		 * excruciatingly slow GTT pagefault. This is more an
3995
		 * excruciatingly slow GTT pagefault. This is more an
3762
		 * issue with compositing managers which need to frequently
3996
		 * issue with compositing managers which need to frequently
3763
		 * flush CPU damage to their GPU bo.
3997
		 * flush CPU damage to their GPU bo.
3764
		 */
3998
		 */
3765
		bo->map = ptr;
3999
		bo->map = ptr;
3766
		DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle));
4000
		DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle));
3767
	}
4001
	}
3768
 
4002
 
3769
	return ptr;
4003
	return ptr;
3770
}
4004
}
3771
 
4005
 
3772
 
4006
 
3773
void *kgem_bo_map__cpu(struct kgem *kgem, struct kgem_bo *bo)
4007
void *kgem_bo_map__cpu(struct kgem *kgem, struct kgem_bo *bo)
3774
{
4008
{
3775
	struct drm_i915_gem_mmap mmap_arg;
4009
	struct drm_i915_gem_mmap mmap_arg;
3776
 
4010
 
3777
	DBG(("%s(handle=%d, size=%d, mapped? %d)\n",
4011
	DBG(("%s(handle=%d, size=%d, mapped? %d)\n",
3778
	     __FUNCTION__, bo->handle, bytes(bo), (int)__MAP_TYPE(bo->map)));
4012
	     __FUNCTION__, bo->handle, bytes(bo), (int)__MAP_TYPE(bo->map)));
3779
	assert(!bo->purged);
4013
	assert(!bo->purged);
3780
	assert(list_is_empty(&bo->list));
4014
	assert(list_is_empty(&bo->list));
3781
	assert(!bo->scanout);
4015
	assert(!bo->scanout);
3782
	assert(bo->proxy == NULL);
4016
	assert(bo->proxy == NULL);
3783
 
4017
 
3784
	if (IS_CPU_MAP(bo->map))
4018
	if (IS_CPU_MAP(bo->map))
3785
		return MAP(bo->map);
4019
		return MAP(bo->map);
3786
 
4020
 
3787
	if (bo->map)
4021
	if (bo->map)
3788
		kgem_bo_release_map(kgem, bo);
4022
		kgem_bo_release_map(kgem, bo);
3789
 
4023
 
3790
	kgem_trim_vma_cache(kgem, MAP_CPU, bucket(bo));
4024
	kgem_trim_vma_cache(kgem, MAP_CPU, bucket(bo));
3791
 
4025
 
3792
retry:
4026
retry:
3793
	VG_CLEAR(mmap_arg);
4027
	VG_CLEAR(mmap_arg);
3794
	mmap_arg.handle = bo->handle;
4028
	mmap_arg.handle = bo->handle;
3795
	mmap_arg.offset = 0;
4029
	mmap_arg.offset = 0;
3796
	mmap_arg.size = bytes(bo);
4030
	mmap_arg.size = bytes(bo);
3797
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg)) {
4031
	if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg)) {
3798
		printf("%s: failed to mmap %d, %d bytes, into CPU domain: %d\n",
4032
		printf("%s: failed to mmap %d, %d bytes, into CPU domain: %d\n",
3799
		       __FUNCTION__, bo->handle, bytes(bo), 0);
4033
		       __FUNCTION__, bo->handle, bytes(bo), 0);
3800
		if (__kgem_throttle_retire(kgem, 0))
4034
		if (__kgem_throttle_retire(kgem, 0))
3801
			goto retry;
4035
			goto retry;
3802
 
4036
 
3803
		if (kgem->need_expire) {
4037
		if (kgem->need_expire) {
3804
			kgem_cleanup_cache(kgem);
4038
			kgem_cleanup_cache(kgem);
3805
			goto retry;
4039
			goto retry;
3806
		}
4040
		}
3807
 
4041
 
3808
		return NULL;
4042
		return NULL;
3809
	}
4043
	}
3810
 
4044
 
3811
	VG(VALGRIND_MAKE_MEM_DEFINED(mmap_arg.addr_ptr, bytes(bo)));
4045
	VG(VALGRIND_MAKE_MEM_DEFINED(mmap_arg.addr_ptr, bytes(bo)));
3812
 
4046
 
3813
	DBG(("%s: caching CPU vma for %d\n", __FUNCTION__, bo->handle));
4047
	DBG(("%s: caching CPU vma for %d\n", __FUNCTION__, bo->handle));
3814
	bo->map = MAKE_CPU_MAP(mmap_arg.addr_ptr);
4048
	bo->map = MAKE_CPU_MAP(mmap_arg.addr_ptr);
3815
	return (void *)(uintptr_t)mmap_arg.addr_ptr;
4049
	return (void *)(uintptr_t)mmap_arg.addr_ptr;
3816
}
4050
}
3817
 
4051
 
3818
void kgem_bo_sync__cpu(struct kgem *kgem, struct kgem_bo *bo)
4052
void kgem_bo_sync__cpu(struct kgem *kgem, struct kgem_bo *bo)
3819
{
4053
{
3820
	assert(bo->proxy == NULL);
4054
	assert(bo->proxy == NULL);
3821
	kgem_bo_submit(kgem, bo);
4055
	kgem_bo_submit(kgem, bo);
3822
 
4056
 
3823
	if (bo->domain != DOMAIN_CPU) {
4057
	if (bo->domain != DOMAIN_CPU) {
3824
		struct drm_i915_gem_set_domain set_domain;
4058
		struct drm_i915_gem_set_domain set_domain;
3825
 
4059
 
3826
		DBG(("%s: SYNC: needs_flush? %d, domain? %d, busy? %d\n", __FUNCTION__,
4060
		DBG(("%s: SYNC: needs_flush? %d, domain? %d, busy? %d\n", __FUNCTION__,
3827
		     bo->needs_flush, bo->domain, __kgem_busy(kgem, bo->handle)));
4061
		     bo->needs_flush, bo->domain, __kgem_busy(kgem, bo->handle)));
3828
 
4062
 
3829
		VG_CLEAR(set_domain);
4063
		VG_CLEAR(set_domain);
3830
		set_domain.handle = bo->handle;
4064
		set_domain.handle = bo->handle;
3831
		set_domain.read_domains = I915_GEM_DOMAIN_CPU;
4065
		set_domain.read_domains = I915_GEM_DOMAIN_CPU;
3832
		set_domain.write_domain = I915_GEM_DOMAIN_CPU;
4066
		set_domain.write_domain = I915_GEM_DOMAIN_CPU;
3833
 
4067
 
3834
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) {
4068
		if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) {
3835
			kgem_bo_retire(kgem, bo);
4069
			kgem_bo_retire(kgem, bo);
3836
			bo->domain = DOMAIN_CPU;
4070
			bo->domain = DOMAIN_CPU;
3837
		}
4071
		}
3838
	}
4072
	}
3839
}
4073
}
3840
 
4074
 
3841
void kgem_clear_dirty(struct kgem *kgem)
4075
void kgem_clear_dirty(struct kgem *kgem)
3842
{
4076
{
3843
	struct list * const buffers = &kgem->next_request->buffers;
4077
	struct list * const buffers = &kgem->next_request->buffers;
3844
	struct kgem_bo *bo;
4078
	struct kgem_bo *bo;
3845
 
4079
 
3846
	list_for_each_entry(bo, buffers, request) {
4080
	list_for_each_entry(bo, buffers, request) {
3847
		if (!bo->dirty)
4081
		if (!bo->dirty)
3848
			break;
4082
			break;
3849
 
4083
 
3850
		bo->dirty = false;
4084
		bo->dirty = false;
3851
	}
4085
	}
3852
}
4086
}
-
 
4087
 
-
 
4088
struct kgem_bo *kgem_create_proxy(struct kgem *kgem,
-
 
4089
				  struct kgem_bo *target,
-
 
4090
				  int offset, int length)
-
 
4091
{
-
 
4092
	struct kgem_bo *bo;
-
 
4093
 
-
 
4094
	DBG(("%s: target handle=%d [proxy? %d], offset=%d, length=%d, io=%d\n",
-
 
4095
	     __FUNCTION__, target->handle, target->proxy ? target->proxy->delta : -1,
-
 
4096
	     offset, length, target->io));
-
 
4097
 
-
 
4098
	bo = __kgem_bo_alloc(target->handle, length);
-
 
4099
	if (bo == NULL)
-
 
4100
		return NULL;
-
 
4101
 
-
 
4102
	bo->unique_id = kgem_get_unique_id(kgem);
-
 
4103
	bo->reusable = false;
-
 
4104
	bo->size.bytes = length;
-
 
4105
 
-
 
4106
	bo->io = target->io && target->proxy == NULL;
-
 
4107
	bo->dirty = target->dirty;
-
 
4108
	bo->tiling = target->tiling;
-
 
4109
	bo->pitch = target->pitch;
-
 
4110
 
-
 
4111
	assert(!bo->scanout);
-
 
4112
	bo->proxy = kgem_bo_reference(target);
-
 
4113
	bo->delta = offset;
-
 
4114
 
-
 
4115
	if (target->exec) {
-
 
4116
		list_move_tail(&bo->request, &kgem->next_request->buffers);
-
 
4117
		bo->exec = &_kgem_dummy_exec;
-
 
4118
	}
-
 
4119
	bo->rq = target->rq;
-
 
4120
 
-
 
4121
	return bo;
-
 
4122
}
3853
 
4123
 
3854
uint32_t kgem_bo_get_binding(struct kgem_bo *bo, uint32_t format)
4124
uint32_t kgem_bo_get_binding(struct kgem_bo *bo, uint32_t format)
3855
{
4125
{
3856
	struct kgem_bo_binding *b;
4126
	struct kgem_bo_binding *b;
3857
 
4127
 
3858
	for (b = &bo->binding; b && b->offset; b = b->next)
4128
	for (b = &bo->binding; b && b->offset; b = b->next)
3859
		if (format == b->format)
4129
		if (format == b->format)
3860
			return b->offset;
4130
			return b->offset;
3861
 
4131
 
3862
	return 0;
4132
	return 0;
3863
}
4133
}
3864
 
4134
 
3865
void kgem_bo_set_binding(struct kgem_bo *bo, uint32_t format, uint16_t offset)
4135
void kgem_bo_set_binding(struct kgem_bo *bo, uint32_t format, uint16_t offset)
3866
{
4136
{
3867
	struct kgem_bo_binding *b;
4137
	struct kgem_bo_binding *b;
3868
 
4138
 
3869
	for (b = &bo->binding; b; b = b->next) {
4139
	for (b = &bo->binding; b; b = b->next) {
3870
		if (b->offset)
4140
		if (b->offset)
3871
			continue;
4141
			continue;
3872
 
4142
 
3873
		b->offset = offset;
4143
		b->offset = offset;
3874
		b->format = format;
4144
		b->format = format;
3875
 
4145
 
3876
		if (b->next)
4146
		if (b->next)
3877
			b->next->offset = 0;
4147
			b->next->offset = 0;
3878
 
4148
 
3879
		return;
4149
		return;
3880
	}
4150
	}
3881
 
4151
 
3882
	b = malloc(sizeof(*b));
4152
	b = malloc(sizeof(*b));
3883
	if (b) {
4153
	if (b) {
3884
		b->next = bo->binding.next;
4154
		b->next = bo->binding.next;
3885
		b->format = format;
4155
		b->format = format;
3886
		b->offset = offset;
4156
		b->offset = offset;
3887
		bo->binding.next = b;
4157
		bo->binding.next = b;
3888
	}
4158
	}
3889
}
4159
}
-
 
4160
 
-
 
4161
 
-
 
4162
int kgem_init_fb(struct kgem *kgem, struct sna_fb *fb)
-
 
4163
{
-
 
4164
    struct kgem_bo *bo;
-
 
4165
    size_t size;
-
 
4166
    int ret;
-
 
4167
 
-
 
4168
	ret = drmIoctl(kgem->fd, SRV_FBINFO, fb);
-
 
4169
	if( ret != 0 )
-
 
4170
	    return 0;
-
 
4171
    
-
 
4172
    size = fb->pitch * fb->height / PAGE_SIZE;
-
 
4173
     
-
 
4174
  	bo = __kgem_bo_alloc(-2, size);
-
 
4175
	if (!bo) {
-
 
4176
		return 0;
-
 
4177
	}
-
 
4178
 
-
 
4179
	bo->domain    = DOMAIN_GTT;
-
 
4180
	bo->unique_id = kgem_get_unique_id(kgem);
-
 
4181
	bo->pitch     = fb->pitch;
-
 
4182
    bo->tiling    = I915_TILING_NONE;
-
 
4183
    bo->scanout   = 1;
-
 
4184
	fb->fb_bo     = bo;    
-
 
4185
 
-
 
4186
    printf("fb width %d height %d pitch %d bo %p\n",
-
 
4187
            fb->width, fb->height, fb->pitch, fb->fb_bo);
-
 
4188
            
-
 
4189
    return 1;
-
 
4190
};
-
 
4191
>
3890
#define>
4192
>
3891
#define>
4193
#define>
3892
#define>
4194
#define>
3893
#define>
4195
struct>
3894
#define>
4196
struct>
3895
#define>
4197
#define>
-
 
4198
#define>
-
 
4199
#define>
-
 
4200
#define>