Rev 4397 | Rev 4501 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4397 | Rev 4403 | ||
---|---|---|---|
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 | #include |
35 | #include |
36 | #include |
36 | #include |
37 | #include |
37 | #include |
38 | 38 | ||
39 | #ifdef HAVE_VALGRIND |
39 | #ifdef HAVE_VALGRIND |
40 | #include |
40 | #include |
41 | #include |
41 | #include |
42 | #endif |
42 | #endif |
43 | 43 | ||
44 | #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM |
44 | #ifdef HAVE_STRUCT_SYSINFO_TOTALRAM |
45 | #include |
45 | #include |
46 | #endif |
46 | #endif |
47 | 47 | ||
48 | #include "sna_cpuid.h" |
48 | #include "sna_cpuid.h" |
49 | 49 | ||
50 | 50 | ||
51 | static struct kgem_bo * |
51 | static struct kgem_bo * |
52 | search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags); |
52 | search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags); |
53 | 53 | ||
54 | static struct kgem_bo * |
54 | static struct kgem_bo * |
55 | search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags); |
55 | search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags); |
56 | 56 | ||
57 | #define DBG_NO_HW 0 |
57 | #define DBG_NO_HW 0 |
58 | #define DBG_NO_TILING 0 |
58 | #define DBG_NO_TILING 0 |
59 | #define DBG_NO_CACHE 0 |
59 | #define DBG_NO_CACHE 0 |
60 | #define DBG_NO_CACHE_LEVEL 0 |
60 | #define DBG_NO_CACHE_LEVEL 0 |
61 | #define DBG_NO_CPU 0 |
61 | #define DBG_NO_CPU 0 |
62 | #define DBG_NO_CREATE2 1 |
62 | #define DBG_NO_CREATE2 1 |
63 | #define DBG_NO_USERPTR 0 |
63 | #define DBG_NO_USERPTR 0 |
64 | #define DBG_NO_UNSYNCHRONIZED_USERPTR 0 |
64 | #define DBG_NO_UNSYNCHRONIZED_USERPTR 0 |
65 | #define DBG_NO_LLC 0 |
65 | #define DBG_NO_LLC 0 |
66 | #define DBG_NO_SEMAPHORES 0 |
66 | #define DBG_NO_SEMAPHORES 0 |
67 | #define DBG_NO_MADV 1 |
67 | #define DBG_NO_MADV 1 |
68 | #define DBG_NO_UPLOAD_CACHE 0 |
68 | #define DBG_NO_UPLOAD_CACHE 0 |
69 | #define DBG_NO_UPLOAD_ACTIVE 0 |
69 | #define DBG_NO_UPLOAD_ACTIVE 0 |
70 | #define DBG_NO_MAP_UPLOAD 0 |
70 | #define DBG_NO_MAP_UPLOAD 0 |
71 | #define DBG_NO_RELAXED_FENCING 0 |
71 | #define DBG_NO_RELAXED_FENCING 0 |
72 | #define DBG_NO_SECURE_BATCHES 0 |
72 | #define DBG_NO_SECURE_BATCHES 0 |
73 | #define DBG_NO_PINNED_BATCHES 0 |
73 | #define DBG_NO_PINNED_BATCHES 0 |
74 | #define DBG_NO_FAST_RELOC 0 |
74 | #define DBG_NO_FAST_RELOC 0 |
75 | #define DBG_NO_HANDLE_LUT 1 |
75 | #define DBG_NO_HANDLE_LUT 1 |
76 | #define DBG_NO_WT 0 |
76 | #define DBG_NO_WT 0 |
77 | #define DBG_DUMP 0 |
77 | #define DBG_DUMP 0 |
78 | 78 | ||
79 | #define FORCE_MMAP_SYNC 0 /* ((1 << DOMAIN_CPU) | (1 << DOMAIN_GTT)) */ |
79 | #define FORCE_MMAP_SYNC 0 /* ((1 << DOMAIN_CPU) | (1 << DOMAIN_GTT)) */ |
80 | 80 | ||
81 | #ifndef DEBUG_SYNC |
81 | #ifndef DEBUG_SYNC |
82 | #define DEBUG_SYNC 0 |
82 | #define DEBUG_SYNC 0 |
83 | #endif |
83 | #endif |
84 | 84 | ||
85 | 85 | ||
86 | #if 0 |
86 | #if 0 |
87 | #define ASSERT_IDLE(kgem__, handle__) assert(!__kgem_busy(kgem__, handle__)) |
87 | #define ASSERT_IDLE(kgem__, handle__) assert(!__kgem_busy(kgem__, handle__)) |
88 | #define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__) assert(!(expect__) || !__kgem_busy(kgem__, handle__)) |
88 | #define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__) assert(!(expect__) || !__kgem_busy(kgem__, handle__)) |
89 | #else |
89 | #else |
90 | #define ASSERT_IDLE(kgem__, handle__) |
90 | #define ASSERT_IDLE(kgem__, handle__) |
91 | #define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__) |
91 | #define ASSERT_MAYBE_IDLE(kgem__, handle__, expect__) |
92 | #endif |
92 | #endif |
93 | 93 | ||
94 | /* Worst case seems to be 965gm where we cannot write within a cacheline that |
94 | /* Worst case seems to be 965gm where we cannot write within a cacheline that |
95 | * is being simultaneously being read by the GPU, or within the sampler |
95 | * is being simultaneously being read by the GPU, or within the sampler |
96 | * prefetch. In general, the chipsets seem to have a requirement that sampler |
96 | * prefetch. In general, the chipsets seem to have a requirement that sampler |
97 | * offsets be aligned to a cacheline (64 bytes). |
97 | * offsets be aligned to a cacheline (64 bytes). |
98 | */ |
98 | */ |
99 | #define UPLOAD_ALIGNMENT 128 |
99 | #define UPLOAD_ALIGNMENT 128 |
100 | 100 | ||
101 | #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) |
101 | #define PAGE_ALIGN(x) ALIGN(x, PAGE_SIZE) |
102 | #define NUM_PAGES(x) (((x) + PAGE_SIZE-1) / PAGE_SIZE) |
102 | #define NUM_PAGES(x) (((x) + PAGE_SIZE-1) / PAGE_SIZE) |
103 | 103 | ||
104 | #define MAX_GTT_VMA_CACHE 512 |
104 | #define MAX_GTT_VMA_CACHE 512 |
105 | #define MAX_CPU_VMA_CACHE INT16_MAX |
105 | #define MAX_CPU_VMA_CACHE INT16_MAX |
106 | #define MAP_PRESERVE_TIME 10 |
106 | #define MAP_PRESERVE_TIME 10 |
107 | 107 | ||
108 | #define MAKE_CPU_MAP(ptr) ((void*)((uintptr_t)(ptr) | 1)) |
108 | #define MAKE_CPU_MAP(ptr) ((void*)((uintptr_t)(ptr) | 1)) |
109 | #define MAKE_USER_MAP(ptr) ((void*)((uintptr_t)(ptr) | 3)) |
109 | #define MAKE_USER_MAP(ptr) ((void*)((uintptr_t)(ptr) | 3)) |
110 | #define IS_USER_MAP(ptr) ((uintptr_t)(ptr) & 2) |
110 | #define IS_USER_MAP(ptr) ((uintptr_t)(ptr) & 2) |
111 | #define __MAP_TYPE(ptr) ((uintptr_t)(ptr) & 3) |
111 | #define __MAP_TYPE(ptr) ((uintptr_t)(ptr) & 3) |
112 | 112 | ||
113 | #define MAKE_REQUEST(rq, ring) ((struct kgem_request *)((uintptr_t)(rq) | (ring))) |
113 | #define MAKE_REQUEST(rq, ring) ((struct kgem_request *)((uintptr_t)(rq) | (ring))) |
114 | 114 | ||
115 | #define LOCAL_I915_PARAM_HAS_BLT 11 |
115 | #define LOCAL_I915_PARAM_HAS_BLT 11 |
116 | #define LOCAL_I915_PARAM_HAS_RELAXED_FENCING 12 |
116 | #define LOCAL_I915_PARAM_HAS_RELAXED_FENCING 12 |
117 | #define LOCAL_I915_PARAM_HAS_RELAXED_DELTA 15 |
117 | #define LOCAL_I915_PARAM_HAS_RELAXED_DELTA 15 |
118 | #define LOCAL_I915_PARAM_HAS_SEMAPHORES 20 |
118 | #define LOCAL_I915_PARAM_HAS_SEMAPHORES 20 |
119 | #define LOCAL_I915_PARAM_HAS_SECURE_BATCHES 23 |
119 | #define LOCAL_I915_PARAM_HAS_SECURE_BATCHES 23 |
120 | #define LOCAL_I915_PARAM_HAS_PINNED_BATCHES 24 |
120 | #define LOCAL_I915_PARAM_HAS_PINNED_BATCHES 24 |
121 | #define LOCAL_I915_PARAM_HAS_NO_RELOC 25 |
121 | #define LOCAL_I915_PARAM_HAS_NO_RELOC 25 |
122 | #define LOCAL_I915_PARAM_HAS_HANDLE_LUT 26 |
122 | #define LOCAL_I915_PARAM_HAS_HANDLE_LUT 26 |
123 | #define LOCAL_I915_PARAM_HAS_WT 27 |
123 | #define LOCAL_I915_PARAM_HAS_WT 27 |
124 | 124 | ||
125 | #define LOCAL_I915_EXEC_IS_PINNED (1<<10) |
125 | #define LOCAL_I915_EXEC_IS_PINNED (1<<10) |
126 | #define LOCAL_I915_EXEC_NO_RELOC (1<<11) |
126 | #define LOCAL_I915_EXEC_NO_RELOC (1<<11) |
127 | #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12) |
127 | #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12) |
128 | struct local_i915_gem_userptr { |
128 | struct local_i915_gem_userptr { |
129 | uint64_t user_ptr; |
129 | uint64_t user_ptr; |
130 | uint64_t user_size; |
130 | uint64_t user_size; |
131 | uint32_t flags; |
131 | uint32_t flags; |
132 | #define I915_USERPTR_READ_ONLY (1<<0) |
132 | #define I915_USERPTR_READ_ONLY (1<<0) |
133 | #define I915_USERPTR_UNSYNCHRONIZED (1<<31) |
133 | #define I915_USERPTR_UNSYNCHRONIZED (1<<31) |
134 | uint32_t handle; |
134 | uint32_t handle; |
135 | }; |
135 | }; |
136 | 136 | ||
137 | #define UNCACHED 0 |
137 | #define UNCACHED 0 |
138 | #define SNOOPED 1 |
138 | #define SNOOPED 1 |
139 | #define DISPLAY 2 |
139 | #define DISPLAY 2 |
140 | 140 | ||
141 | struct local_i915_gem_caching { |
141 | struct local_i915_gem_caching { |
142 | uint32_t handle; |
142 | uint32_t handle; |
143 | uint32_t caching; |
143 | uint32_t caching; |
144 | }; |
144 | }; |
145 | 145 | ||
146 | #define LOCAL_IOCTL_I915_GEM_SET_CACHING SRV_I915_GEM_SET_CACHING |
146 | #define LOCAL_IOCTL_I915_GEM_SET_CACHING SRV_I915_GEM_SET_CACHING |
147 | 147 | ||
148 | struct local_fbinfo { |
148 | struct local_fbinfo { |
149 | int width; |
149 | int width; |
150 | int height; |
150 | int height; |
151 | int pitch; |
151 | int pitch; |
152 | int tiling; |
152 | int tiling; |
153 | }; |
153 | }; |
154 | 154 | ||
155 | struct kgem_buffer { |
155 | struct kgem_buffer { |
156 | struct kgem_bo base; |
156 | struct kgem_bo base; |
157 | void *mem; |
157 | void *mem; |
158 | uint32_t used; |
158 | uint32_t used; |
159 | uint32_t need_io : 1; |
159 | uint32_t need_io : 1; |
160 | uint32_t write : 2; |
160 | uint32_t write : 2; |
161 | uint32_t mmapped : 1; |
161 | uint32_t mmapped : 1; |
162 | }; |
162 | }; |
163 | 163 | ||
164 | static struct kgem_bo *__kgem_freed_bo; |
164 | static struct kgem_bo *__kgem_freed_bo; |
165 | static struct kgem_request *__kgem_freed_request; |
165 | static struct kgem_request *__kgem_freed_request; |
166 | static struct drm_i915_gem_exec_object2 _kgem_dummy_exec; |
166 | static struct drm_i915_gem_exec_object2 _kgem_dummy_exec; |
167 | 167 | ||
168 | static inline int bytes(struct kgem_bo *bo) |
168 | static inline int bytes(struct kgem_bo *bo) |
169 | { |
169 | { |
170 | return __kgem_bo_size(bo); |
170 | return __kgem_bo_size(bo); |
171 | } |
171 | } |
172 | 172 | ||
173 | #define bucket(B) (B)->size.pages.bucket |
173 | #define bucket(B) (B)->size.pages.bucket |
174 | #define num_pages(B) (B)->size.pages.count |
174 | #define num_pages(B) (B)->size.pages.count |
175 | 175 | ||
176 | #ifdef DEBUG_MEMORY |
176 | #ifdef DEBUG_MEMORY |
177 | static void debug_alloc(struct kgem *kgem, size_t size) |
177 | static void debug_alloc(struct kgem *kgem, size_t size) |
178 | { |
178 | { |
179 | kgem->debug_memory.bo_allocs++; |
179 | kgem->debug_memory.bo_allocs++; |
180 | kgem->debug_memory.bo_bytes += size; |
180 | kgem->debug_memory.bo_bytes += size; |
181 | } |
181 | } |
182 | static void debug_alloc__bo(struct kgem *kgem, struct kgem_bo *bo) |
182 | static void debug_alloc__bo(struct kgem *kgem, struct kgem_bo *bo) |
183 | { |
183 | { |
184 | debug_alloc(kgem, bytes(bo)); |
184 | debug_alloc(kgem, bytes(bo)); |
185 | } |
185 | } |
186 | #else |
186 | #else |
187 | #define debug_alloc(k, b) |
187 | #define debug_alloc(k, b) |
188 | #define debug_alloc__bo(k, b) |
188 | #define debug_alloc__bo(k, b) |
189 | #endif |
189 | #endif |
190 | 190 | ||
191 | #ifndef NDEBUG |
191 | #ifndef NDEBUG |
192 | static void assert_tiling(struct kgem *kgem, struct kgem_bo *bo) |
192 | static void assert_tiling(struct kgem *kgem, struct kgem_bo *bo) |
193 | { |
193 | { |
194 | struct drm_i915_gem_get_tiling tiling; |
194 | struct drm_i915_gem_get_tiling tiling; |
195 | 195 | ||
196 | assert(bo); |
196 | assert(bo); |
197 | 197 | ||
198 | VG_CLEAR(tiling); |
198 | VG_CLEAR(tiling); |
199 | tiling.handle = bo->handle; |
199 | tiling.handle = bo->handle; |
200 | tiling.tiling_mode = -1; |
200 | tiling.tiling_mode = -1; |
201 | (void)drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_GET_TILING, &tiling); |
201 | (void)drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_GET_TILING, &tiling); |
202 | assert(tiling.tiling_mode == bo->tiling); |
202 | assert(tiling.tiling_mode == bo->tiling); |
203 | } |
203 | } |
204 | #else |
204 | #else |
205 | #define assert_tiling(kgem, bo) |
205 | #define assert_tiling(kgem, bo) |
206 | #endif |
206 | #endif |
207 | 207 | ||
208 | static void kgem_sna_reset(struct kgem *kgem) |
208 | static void kgem_sna_reset(struct kgem *kgem) |
209 | { |
209 | { |
210 | struct sna *sna = container_of(kgem, struct sna, kgem); |
210 | struct sna *sna = container_of(kgem, struct sna, kgem); |
211 | 211 | ||
212 | sna->render.reset(sna); |
212 | sna->render.reset(sna); |
213 | sna->blt_state.fill_bo = 0; |
213 | sna->blt_state.fill_bo = 0; |
214 | } |
214 | } |
215 | 215 | ||
216 | static void kgem_sna_flush(struct kgem *kgem) |
216 | static void kgem_sna_flush(struct kgem *kgem) |
217 | { |
217 | { |
218 | struct sna *sna = container_of(kgem, struct sna, kgem); |
218 | struct sna *sna = container_of(kgem, struct sna, kgem); |
219 | 219 | ||
220 | sna->render.flush(sna); |
220 | sna->render.flush(sna); |
221 | 221 | ||
222 | // if (sna->render.solid_cache.dirty) |
222 | // if (sna->render.solid_cache.dirty) |
223 | // sna_render_flush_solid(sna); |
223 | // sna_render_flush_solid(sna); |
224 | } |
224 | } |
225 | 225 | ||
226 | static bool gem_set_tiling(int fd, uint32_t handle, int tiling, int stride) |
226 | static bool gem_set_tiling(int fd, uint32_t handle, int tiling, int stride) |
227 | { |
227 | { |
228 | struct drm_i915_gem_set_tiling set_tiling; |
228 | struct drm_i915_gem_set_tiling set_tiling; |
229 | int ret; |
229 | int ret; |
230 | 230 | ||
231 | if (DBG_NO_TILING) |
231 | if (DBG_NO_TILING) |
232 | return false; |
232 | return false; |
233 | 233 | ||
234 | VG_CLEAR(set_tiling); |
234 | VG_CLEAR(set_tiling); |
235 | do { |
235 | do { |
236 | set_tiling.handle = handle; |
236 | set_tiling.handle = handle; |
237 | set_tiling.tiling_mode = tiling; |
237 | set_tiling.tiling_mode = tiling; |
238 | set_tiling.stride = stride; |
238 | set_tiling.stride = stride; |
239 | 239 | ||
240 | ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling); |
240 | ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling); |
241 | } while (ret != 0); |
241 | } while (ret != 0); |
242 | return ret == 0; |
242 | return ret == 0; |
243 | } |
243 | } |
244 | 244 | ||
245 | static bool gem_set_caching(int fd, uint32_t handle, int caching) |
245 | static bool gem_set_caching(int fd, uint32_t handle, int caching) |
246 | { |
246 | { |
247 | struct local_i915_gem_caching arg; |
247 | struct local_i915_gem_caching arg; |
248 | 248 | ||
249 | VG_CLEAR(arg); |
249 | VG_CLEAR(arg); |
250 | arg.handle = handle; |
250 | arg.handle = handle; |
251 | arg.caching = caching; |
251 | arg.caching = caching; |
252 | return drmIoctl(fd, LOCAL_IOCTL_I915_GEM_SET_CACHING, &arg) == 0; |
252 | return drmIoctl(fd, LOCAL_IOCTL_I915_GEM_SET_CACHING, &arg) == 0; |
253 | } |
253 | } |
254 | 254 | ||
255 | 255 | ||
256 | 256 | ||
257 | 257 | ||
258 | 258 | ||
259 | static bool __kgem_throttle_retire(struct kgem *kgem, unsigned flags) |
259 | static bool __kgem_throttle_retire(struct kgem *kgem, unsigned flags) |
260 | { |
260 | { |
261 | if (flags & CREATE_NO_RETIRE) { |
261 | if (flags & CREATE_NO_RETIRE) { |
262 | DBG(("%s: not retiring per-request\n", __FUNCTION__)); |
262 | DBG(("%s: not retiring per-request\n", __FUNCTION__)); |
263 | return false; |
263 | return false; |
264 | } |
264 | } |
265 | 265 | ||
266 | if (!kgem->need_retire) { |
266 | if (!kgem->need_retire) { |
267 | DBG(("%s: nothing to retire\n", __FUNCTION__)); |
267 | DBG(("%s: nothing to retire\n", __FUNCTION__)); |
268 | return false; |
268 | return false; |
269 | } |
269 | } |
270 | 270 | ||
271 | if (kgem_retire(kgem)) |
271 | if (kgem_retire(kgem)) |
272 | return true; |
272 | return true; |
273 | 273 | ||
274 | if (flags & CREATE_NO_THROTTLE || !kgem->need_throttle) { |
274 | if (flags & CREATE_NO_THROTTLE || !kgem->need_throttle) { |
275 | DBG(("%s: not throttling\n", __FUNCTION__)); |
275 | DBG(("%s: not throttling\n", __FUNCTION__)); |
276 | return false; |
276 | return false; |
277 | } |
277 | } |
278 | 278 | ||
279 | kgem_throttle(kgem); |
279 | kgem_throttle(kgem); |
280 | return kgem_retire(kgem); |
280 | return kgem_retire(kgem); |
281 | } |
281 | } |
282 | 282 | ||
283 | static void *__kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo) |
283 | static void *__kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo) |
284 | { |
284 | { |
285 | struct drm_i915_gem_mmap_gtt mmap_arg; |
285 | struct drm_i915_gem_mmap_gtt mmap_arg; |
286 | void *ptr; |
286 | void *ptr; |
287 | 287 | ||
288 | DBG(("%s(handle=%d, size=%d)\n", __FUNCTION__, |
288 | DBG(("%s(handle=%d, size=%d)\n", __FUNCTION__, |
289 | bo->handle, bytes(bo))); |
289 | bo->handle, bytes(bo))); |
290 | assert(bo->proxy == NULL); |
290 | assert(bo->proxy == NULL); |
291 | assert(!bo->snoop); |
291 | assert(!bo->snoop); |
292 | assert(kgem_bo_can_map(kgem, bo)); |
292 | assert(kgem_bo_can_map(kgem, bo)); |
293 | 293 | ||
294 | retry_gtt: |
294 | retry_gtt: |
295 | VG_CLEAR(mmap_arg); |
295 | VG_CLEAR(mmap_arg); |
296 | mmap_arg.handle = bo->handle; |
296 | mmap_arg.handle = bo->handle; |
297 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg)) { |
297 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg)) { |
298 | 298 | ||
299 | (void)__kgem_throttle_retire(kgem, 0); |
299 | (void)__kgem_throttle_retire(kgem, 0); |
300 | if (kgem_expire_cache(kgem)) |
300 | if (kgem_expire_cache(kgem)) |
301 | goto retry_gtt; |
301 | goto retry_gtt; |
302 | 302 | ||
303 | if (kgem->need_expire) { |
303 | if (kgem->need_expire) { |
304 | kgem_cleanup_cache(kgem); |
304 | kgem_cleanup_cache(kgem); |
305 | goto retry_gtt; |
305 | goto retry_gtt; |
306 | } |
306 | } |
307 | 307 | ||
308 | printf("%s: failed to retrieve GTT offset for handle=%d\n", |
308 | printf("%s: failed to retrieve GTT offset for handle=%d\n", |
309 | __FUNCTION__, bo->handle); |
309 | __FUNCTION__, bo->handle); |
310 | return NULL; |
310 | return NULL; |
311 | } |
311 | } |
312 | 312 | ||
313 | retry_mmap: |
313 | retry_mmap: |
314 | ptr = (void*)(int)mmap_arg.offset; |
314 | ptr = (void*)(int)mmap_arg.offset; |
315 | if (ptr == NULL) { |
315 | if (ptr == NULL) { |
316 | ErrorF("%s: failed to mmap handle=%d, %d bytes, into GTT domain\n", |
316 | ErrorF("%s: failed to mmap handle=%d, %d bytes, into GTT domain\n", |
317 | __FUNCTION__, bo->handle, bytes(bo)); |
317 | __FUNCTION__, bo->handle, bytes(bo)); |
318 | ptr = NULL; |
318 | ptr = NULL; |
319 | } |
319 | } |
320 | 320 | ||
321 | return ptr; |
321 | return ptr; |
322 | } |
322 | } |
323 | 323 | ||
324 | static int __gem_write(int fd, uint32_t handle, |
324 | static int __gem_write(int fd, uint32_t handle, |
325 | int offset, int length, |
325 | int offset, int length, |
326 | const void *src) |
326 | const void *src) |
327 | { |
327 | { |
328 | struct drm_i915_gem_pwrite pwrite; |
328 | struct drm_i915_gem_pwrite pwrite; |
329 | 329 | ||
330 | DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__, |
330 | DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__, |
331 | handle, offset, length)); |
331 | handle, offset, length)); |
332 | 332 | ||
333 | VG_CLEAR(pwrite); |
333 | VG_CLEAR(pwrite); |
334 | pwrite.handle = handle; |
334 | pwrite.handle = handle; |
335 | pwrite.offset = offset; |
335 | pwrite.offset = offset; |
336 | pwrite.size = length; |
336 | pwrite.size = length; |
337 | pwrite.data_ptr = (uintptr_t)src; |
337 | pwrite.data_ptr = (uintptr_t)src; |
338 | return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); |
338 | return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); |
339 | } |
339 | } |
340 | 340 | ||
341 | static int gem_write(int fd, uint32_t handle, |
341 | static int gem_write(int fd, uint32_t handle, |
342 | int offset, int length, |
342 | int offset, int length, |
343 | const void *src) |
343 | const void *src) |
344 | { |
344 | { |
345 | struct drm_i915_gem_pwrite pwrite; |
345 | struct drm_i915_gem_pwrite pwrite; |
346 | 346 | ||
347 | DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__, |
347 | DBG(("%s(handle=%d, offset=%d, len=%d)\n", __FUNCTION__, |
348 | handle, offset, length)); |
348 | handle, offset, length)); |
349 | 349 | ||
350 | VG_CLEAR(pwrite); |
350 | VG_CLEAR(pwrite); |
351 | pwrite.handle = handle; |
351 | pwrite.handle = handle; |
352 | /* align the transfer to cachelines; fortuitously this is safe! */ |
352 | /* align the transfer to cachelines; fortuitously this is safe! */ |
353 | if ((offset | length) & 63) { |
353 | if ((offset | length) & 63) { |
354 | pwrite.offset = offset & ~63; |
354 | pwrite.offset = offset & ~63; |
355 | pwrite.size = ALIGN(offset+length, 64) - pwrite.offset; |
355 | pwrite.size = ALIGN(offset+length, 64) - pwrite.offset; |
356 | pwrite.data_ptr = (uintptr_t)src + pwrite.offset - offset; |
356 | pwrite.data_ptr = (uintptr_t)src + pwrite.offset - offset; |
357 | } else { |
357 | } else { |
358 | pwrite.offset = offset; |
358 | pwrite.offset = offset; |
359 | pwrite.size = length; |
359 | pwrite.size = length; |
360 | pwrite.data_ptr = (uintptr_t)src; |
360 | pwrite.data_ptr = (uintptr_t)src; |
361 | } |
361 | } |
362 | return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); |
362 | return drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite); |
363 | } |
363 | } |
364 | 364 | ||
365 | 365 | ||
366 | bool __kgem_busy(struct kgem *kgem, int handle) |
366 | bool __kgem_busy(struct kgem *kgem, int handle) |
367 | { |
367 | { |
368 | struct drm_i915_gem_busy busy; |
368 | struct drm_i915_gem_busy busy; |
369 | 369 | ||
370 | VG_CLEAR(busy); |
370 | VG_CLEAR(busy); |
371 | busy.handle = handle; |
371 | busy.handle = handle; |
372 | busy.busy = !kgem->wedged; |
372 | busy.busy = !kgem->wedged; |
373 | (void)drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); |
373 | (void)drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); |
374 | DBG(("%s: handle=%d, busy=%d, wedged=%d\n", |
374 | DBG(("%s: handle=%d, busy=%d, wedged=%d\n", |
375 | __FUNCTION__, handle, busy.busy, kgem->wedged)); |
375 | __FUNCTION__, handle, busy.busy, kgem->wedged)); |
376 | 376 | ||
377 | return busy.busy; |
377 | return busy.busy; |
378 | } |
378 | } |
379 | 379 | ||
380 | static void kgem_bo_retire(struct kgem *kgem, struct kgem_bo *bo) |
380 | static void kgem_bo_retire(struct kgem *kgem, struct kgem_bo *bo) |
381 | { |
381 | { |
382 | DBG(("%s: retiring bo handle=%d (needed flush? %d), rq? %d [busy?=%d]\n", |
382 | DBG(("%s: retiring bo handle=%d (needed flush? %d), rq? %d [busy?=%d]\n", |
383 | __FUNCTION__, bo->handle, bo->needs_flush, bo->rq != NULL, |
383 | __FUNCTION__, bo->handle, bo->needs_flush, bo->rq != NULL, |
384 | __kgem_busy(kgem, bo->handle))); |
384 | __kgem_busy(kgem, bo->handle))); |
385 | assert(bo->exec == NULL); |
385 | assert(bo->exec == NULL); |
386 | assert(list_is_empty(&bo->vma)); |
386 | assert(list_is_empty(&bo->vma)); |
387 | 387 | ||
388 | if (bo->rq) { |
388 | if (bo->rq) { |
389 | if (!__kgem_busy(kgem, bo->handle)) { |
389 | if (!__kgem_busy(kgem, bo->handle)) { |
390 | __kgem_bo_clear_busy(bo); |
390 | __kgem_bo_clear_busy(bo); |
391 | kgem_retire(kgem); |
391 | kgem_retire(kgem); |
392 | } |
392 | } |
393 | } else { |
393 | } else { |
394 | assert(!bo->needs_flush); |
394 | assert(!bo->needs_flush); |
395 | ASSERT_IDLE(kgem, bo->handle); |
395 | ASSERT_IDLE(kgem, bo->handle); |
396 | } |
396 | } |
397 | } |
397 | } |
398 | 398 | ||
399 | bool kgem_bo_write(struct kgem *kgem, struct kgem_bo *bo, |
399 | bool kgem_bo_write(struct kgem *kgem, struct kgem_bo *bo, |
400 | const void *data, int length) |
400 | const void *data, int length) |
401 | { |
401 | { |
402 | assert(bo->refcnt); |
402 | assert(bo->refcnt); |
403 | assert(!bo->purged); |
403 | assert(!bo->purged); |
404 | assert(bo->proxy == NULL); |
404 | assert(bo->proxy == NULL); |
405 | ASSERT_IDLE(kgem, bo->handle); |
405 | ASSERT_IDLE(kgem, bo->handle); |
406 | 406 | ||
407 | assert(length <= bytes(bo)); |
407 | assert(length <= bytes(bo)); |
408 | if (gem_write(kgem->fd, bo->handle, 0, length, data)) |
408 | if (gem_write(kgem->fd, bo->handle, 0, length, data)) |
409 | return false; |
409 | return false; |
410 | 410 | ||
411 | DBG(("%s: flush=%d, domain=%d\n", __FUNCTION__, bo->flush, bo->domain)); |
411 | DBG(("%s: flush=%d, domain=%d\n", __FUNCTION__, bo->flush, bo->domain)); |
412 | if (bo->exec == NULL) { |
412 | if (bo->exec == NULL) { |
413 | kgem_bo_retire(kgem, bo); |
413 | kgem_bo_retire(kgem, bo); |
414 | bo->domain = DOMAIN_NONE; |
414 | bo->domain = DOMAIN_NONE; |
415 | } |
415 | } |
416 | bo->gtt_dirty = true; |
416 | bo->gtt_dirty = true; |
417 | return true; |
417 | return true; |
418 | } |
418 | } |
419 | 419 | ||
420 | static uint32_t gem_create(int fd, int num_pages) |
420 | static uint32_t gem_create(int fd, int num_pages) |
421 | { |
421 | { |
422 | struct drm_i915_gem_create create; |
422 | struct drm_i915_gem_create create; |
423 | 423 | ||
424 | VG_CLEAR(create); |
424 | VG_CLEAR(create); |
425 | create.handle = 0; |
425 | create.handle = 0; |
426 | create.size = PAGE_SIZE * num_pages; |
426 | create.size = PAGE_SIZE * num_pages; |
427 | (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); |
427 | (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create); |
428 | 428 | ||
429 | return create.handle; |
429 | return create.handle; |
430 | } |
430 | } |
431 | 431 | ||
432 | static bool |
432 | static bool |
433 | kgem_bo_set_purgeable(struct kgem *kgem, struct kgem_bo *bo) |
433 | kgem_bo_set_purgeable(struct kgem *kgem, struct kgem_bo *bo) |
434 | { |
434 | { |
435 | #if DBG_NO_MADV |
435 | #if DBG_NO_MADV |
436 | return true; |
436 | return true; |
437 | #else |
437 | #else |
438 | struct drm_i915_gem_madvise madv; |
438 | struct drm_i915_gem_madvise madv; |
439 | 439 | ||
440 | assert(bo->exec == NULL); |
440 | assert(bo->exec == NULL); |
441 | assert(!bo->purged); |
441 | assert(!bo->purged); |
442 | 442 | ||
443 | VG_CLEAR(madv); |
443 | VG_CLEAR(madv); |
444 | madv.handle = bo->handle; |
444 | madv.handle = bo->handle; |
445 | madv.madv = I915_MADV_DONTNEED; |
445 | madv.madv = I915_MADV_DONTNEED; |
446 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) { |
446 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) { |
447 | bo->purged = 1; |
447 | bo->purged = 1; |
448 | kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU; |
448 | kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU; |
449 | return madv.retained; |
449 | return madv.retained; |
450 | } |
450 | } |
451 | 451 | ||
452 | return true; |
452 | return true; |
453 | #endif |
453 | #endif |
454 | } |
454 | } |
455 | 455 | ||
456 | static bool |
456 | static bool |
457 | kgem_bo_is_retained(struct kgem *kgem, struct kgem_bo *bo) |
457 | kgem_bo_is_retained(struct kgem *kgem, struct kgem_bo *bo) |
458 | { |
458 | { |
459 | #if DBG_NO_MADV |
459 | #if DBG_NO_MADV |
460 | return true; |
460 | return true; |
461 | #else |
461 | #else |
462 | struct drm_i915_gem_madvise madv; |
462 | struct drm_i915_gem_madvise madv; |
463 | 463 | ||
464 | if (!bo->purged) |
464 | if (!bo->purged) |
465 | return true; |
465 | return true; |
466 | 466 | ||
467 | VG_CLEAR(madv); |
467 | VG_CLEAR(madv); |
468 | madv.handle = bo->handle; |
468 | madv.handle = bo->handle; |
469 | madv.madv = I915_MADV_DONTNEED; |
469 | madv.madv = I915_MADV_DONTNEED; |
470 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) |
470 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) |
471 | return madv.retained; |
471 | return madv.retained; |
472 | 472 | ||
473 | return false; |
473 | return false; |
474 | #endif |
474 | #endif |
475 | } |
475 | } |
476 | 476 | ||
477 | static bool |
477 | static bool |
478 | kgem_bo_clear_purgeable(struct kgem *kgem, struct kgem_bo *bo) |
478 | kgem_bo_clear_purgeable(struct kgem *kgem, struct kgem_bo *bo) |
479 | { |
479 | { |
480 | #if DBG_NO_MADV |
480 | #if DBG_NO_MADV |
481 | return true; |
481 | return true; |
482 | #else |
482 | #else |
483 | struct drm_i915_gem_madvise madv; |
483 | struct drm_i915_gem_madvise madv; |
484 | 484 | ||
485 | assert(bo->purged); |
485 | assert(bo->purged); |
486 | 486 | ||
487 | VG_CLEAR(madv); |
487 | VG_CLEAR(madv); |
488 | madv.handle = bo->handle; |
488 | madv.handle = bo->handle; |
489 | madv.madv = I915_MADV_WILLNEED; |
489 | madv.madv = I915_MADV_WILLNEED; |
490 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) { |
490 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv) == 0) { |
491 | bo->purged = !madv.retained; |
491 | bo->purged = !madv.retained; |
492 | kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU; |
492 | kgem->need_purge |= !madv.retained && bo->domain == DOMAIN_GPU; |
493 | return madv.retained; |
493 | return madv.retained; |
494 | } |
494 | } |
495 | 495 | ||
496 | return false; |
496 | return false; |
497 | #endif |
497 | #endif |
498 | } |
498 | } |
499 | 499 | ||
500 | static void gem_close(int fd, uint32_t handle) |
500 | static void gem_close(int fd, uint32_t handle) |
501 | { |
501 | { |
502 | struct drm_gem_close close; |
502 | struct drm_gem_close close; |
503 | 503 | ||
504 | VG_CLEAR(close); |
504 | VG_CLEAR(close); |
505 | close.handle = handle; |
505 | close.handle = handle; |
506 | (void)drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close); |
506 | (void)drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close); |
507 | } |
507 | } |
508 | 508 | ||
509 | constant inline static unsigned long __fls(unsigned long word) |
509 | constant inline static unsigned long __fls(unsigned long word) |
510 | { |
510 | { |
511 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86__) || defined(__x86_64__)) |
511 | #if defined(__GNUC__) && (defined(__i386__) || defined(__x86__) || defined(__x86_64__)) |
512 | asm("bsr %1,%0" |
512 | asm("bsr %1,%0" |
513 | : "=r" (word) |
513 | : "=r" (word) |
514 | : "rm" (word)); |
514 | : "rm" (word)); |
515 | return word; |
515 | return word; |
516 | #else |
516 | #else |
517 | unsigned int v = 0; |
517 | unsigned int v = 0; |
518 | 518 | ||
519 | while (word >>= 1) |
519 | while (word >>= 1) |
520 | v++; |
520 | v++; |
521 | 521 | ||
522 | return v; |
522 | return v; |
523 | #endif |
523 | #endif |
524 | } |
524 | } |
525 | 525 | ||
526 | constant inline static int cache_bucket(int num_pages) |
526 | constant inline static int cache_bucket(int num_pages) |
527 | { |
527 | { |
528 | return __fls(num_pages); |
528 | return __fls(num_pages); |
529 | } |
529 | } |
530 | 530 | ||
531 | static struct kgem_bo *__kgem_bo_init(struct kgem_bo *bo, |
531 | static struct kgem_bo *__kgem_bo_init(struct kgem_bo *bo, |
532 | int handle, int num_pages) |
532 | int handle, int num_pages) |
533 | { |
533 | { |
534 | assert(num_pages); |
534 | assert(num_pages); |
535 | memset(bo, 0, sizeof(*bo)); |
535 | memset(bo, 0, sizeof(*bo)); |
536 | 536 | ||
537 | bo->refcnt = 1; |
537 | bo->refcnt = 1; |
538 | bo->handle = handle; |
538 | bo->handle = handle; |
539 | bo->target_handle = -1; |
539 | bo->target_handle = -1; |
540 | num_pages(bo) = num_pages; |
540 | num_pages(bo) = num_pages; |
541 | bucket(bo) = cache_bucket(num_pages); |
541 | bucket(bo) = cache_bucket(num_pages); |
542 | bo->reusable = true; |
542 | bo->reusable = true; |
543 | bo->domain = DOMAIN_CPU; |
543 | bo->domain = DOMAIN_CPU; |
544 | list_init(&bo->request); |
544 | list_init(&bo->request); |
545 | list_init(&bo->list); |
545 | list_init(&bo->list); |
546 | list_init(&bo->vma); |
546 | list_init(&bo->vma); |
547 | 547 | ||
548 | return bo; |
548 | return bo; |
549 | } |
549 | } |
550 | 550 | ||
551 | static struct kgem_bo *__kgem_bo_alloc(int handle, int num_pages) |
551 | static struct kgem_bo *__kgem_bo_alloc(int handle, int num_pages) |
552 | { |
552 | { |
553 | struct kgem_bo *bo; |
553 | struct kgem_bo *bo; |
554 | 554 | ||
555 | if (__kgem_freed_bo) { |
555 | if (__kgem_freed_bo) { |
556 | bo = __kgem_freed_bo; |
556 | bo = __kgem_freed_bo; |
557 | __kgem_freed_bo = *(struct kgem_bo **)bo; |
557 | __kgem_freed_bo = *(struct kgem_bo **)bo; |
558 | } else { |
558 | } else { |
559 | bo = malloc(sizeof(*bo)); |
559 | bo = malloc(sizeof(*bo)); |
560 | if (bo == NULL) |
560 | if (bo == NULL) |
561 | return NULL; |
561 | return NULL; |
562 | } |
562 | } |
563 | 563 | ||
564 | return __kgem_bo_init(bo, handle, num_pages); |
564 | return __kgem_bo_init(bo, handle, num_pages); |
565 | } |
565 | } |
566 | 566 | ||
567 | static struct kgem_request *__kgem_request_alloc(struct kgem *kgem) |
567 | static struct kgem_request *__kgem_request_alloc(struct kgem *kgem) |
568 | { |
568 | { |
569 | struct kgem_request *rq; |
569 | struct kgem_request *rq; |
570 | 570 | ||
571 | rq = __kgem_freed_request; |
571 | rq = __kgem_freed_request; |
572 | if (rq) { |
572 | if (rq) { |
573 | __kgem_freed_request = *(struct kgem_request **)rq; |
573 | __kgem_freed_request = *(struct kgem_request **)rq; |
574 | } else { |
574 | } else { |
575 | rq = malloc(sizeof(*rq)); |
575 | rq = malloc(sizeof(*rq)); |
576 | if (rq == NULL) |
576 | if (rq == NULL) |
577 | rq = &kgem->static_request; |
577 | rq = &kgem->static_request; |
578 | } |
578 | } |
579 | 579 | ||
580 | list_init(&rq->buffers); |
580 | list_init(&rq->buffers); |
581 | rq->bo = NULL; |
581 | rq->bo = NULL; |
582 | rq->ring = 0; |
582 | rq->ring = 0; |
583 | 583 | ||
584 | return rq; |
584 | return rq; |
585 | } |
585 | } |
586 | 586 | ||
587 | static void __kgem_request_free(struct kgem_request *rq) |
587 | static void __kgem_request_free(struct kgem_request *rq) |
588 | { |
588 | { |
589 | _list_del(&rq->list); |
589 | _list_del(&rq->list); |
590 | *(struct kgem_request **)rq = __kgem_freed_request; |
590 | *(struct kgem_request **)rq = __kgem_freed_request; |
591 | __kgem_freed_request = rq; |
591 | __kgem_freed_request = rq; |
592 | } |
592 | } |
593 | 593 | ||
594 | static struct list *inactive(struct kgem *kgem, int num_pages) |
594 | static struct list *inactive(struct kgem *kgem, int num_pages) |
595 | { |
595 | { |
596 | assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE); |
596 | assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE); |
597 | assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS); |
597 | assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS); |
598 | return &kgem->inactive[cache_bucket(num_pages)]; |
598 | return &kgem->inactive[cache_bucket(num_pages)]; |
599 | } |
599 | } |
600 | 600 | ||
601 | static struct list *active(struct kgem *kgem, int num_pages, int tiling) |
601 | static struct list *active(struct kgem *kgem, int num_pages, int tiling) |
602 | { |
602 | { |
603 | assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE); |
603 | assert(num_pages < MAX_CACHE_SIZE / PAGE_SIZE); |
604 | assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS); |
604 | assert(cache_bucket(num_pages) < NUM_CACHE_BUCKETS); |
605 | return &kgem->active[cache_bucket(num_pages)][tiling]; |
605 | return &kgem->active[cache_bucket(num_pages)][tiling]; |
606 | } |
606 | } |
607 | 607 | ||
608 | static size_t |
608 | static size_t |
609 | agp_aperture_size(struct pci_device *dev, unsigned gen) |
609 | agp_aperture_size(struct pci_device *dev, unsigned gen) |
610 | { |
610 | { |
611 | /* XXX assume that only future chipsets are unknown and follow |
611 | /* XXX assume that only future chipsets are unknown and follow |
612 | * the post gen2 PCI layout. |
612 | * the post gen2 PCI layout. |
613 | */ |
613 | */ |
614 | return 0; |
614 | return 0; |
615 | } |
615 | } |
616 | 616 | ||
617 | static size_t |
617 | static size_t |
618 | total_ram_size(void) |
618 | total_ram_size(void) |
619 | { |
619 | { |
620 | uint32_t data[9]; |
620 | uint32_t data[9]; |
621 | size_t size = 0; |
621 | size_t size = 0; |
622 | 622 | ||
623 | asm volatile("int $0x40" |
623 | asm volatile("int $0x40" |
624 | : "=a" (size) |
624 | : "=a" (size) |
625 | : "a" (18),"b"(20), "c" (data) |
625 | : "a" (18),"b"(20), "c" (data) |
626 | : "memory"); |
626 | : "memory"); |
627 | 627 | ||
628 | return size != -1 ? size : 0; |
628 | return size != -1 ? size : 0; |
629 | } |
629 | } |
630 | 630 | ||
631 | static unsigned |
631 | static unsigned |
632 | cpu_cache_size__cpuid4(void) |
632 | cpu_cache_size__cpuid4(void) |
633 | { |
633 | { |
634 | /* Deterministic Cache Parmaeters (Function 04h)": |
634 | /* Deterministic Cache Parmaeters (Function 04h)": |
635 | * When EAX is initialized to a value of 4, the CPUID instruction |
635 | * When EAX is initialized to a value of 4, the CPUID instruction |
636 | * returns deterministic cache information in the EAX, EBX, ECX |
636 | * returns deterministic cache information in the EAX, EBX, ECX |
637 | * and EDX registers. This function requires ECX be initialized |
637 | * and EDX registers. This function requires ECX be initialized |
638 | * with an index which indicates which cache to return information |
638 | * with an index which indicates which cache to return information |
639 | * about. The OS is expected to call this function (CPUID.4) with |
639 | * about. The OS is expected to call this function (CPUID.4) with |
640 | * ECX = 0, 1, 2, until EAX[4:0] == 0, indicating no more caches. |
640 | * ECX = 0, 1, 2, until EAX[4:0] == 0, indicating no more caches. |
641 | * The order in which the caches are returned is not specified |
641 | * The order in which the caches are returned is not specified |
642 | * and may change at Intel's discretion. |
642 | * and may change at Intel's discretion. |
643 | * |
643 | * |
644 | * Calculating the Cache Size in bytes: |
644 | * Calculating the Cache Size in bytes: |
645 | * = (Ways +1) * (Partitions +1) * (Line Size +1) * (Sets +1) |
645 | * = (Ways +1) * (Partitions +1) * (Line Size +1) * (Sets +1) |
646 | */ |
646 | */ |
647 | 647 | ||
648 | unsigned int eax, ebx, ecx, edx; |
648 | unsigned int eax, ebx, ecx, edx; |
649 | unsigned int llc_size = 0; |
649 | unsigned int llc_size = 0; |
650 | int cnt = 0; |
650 | int cnt = 0; |
651 | 651 | ||
652 | if (__get_cpuid_max(BASIC_CPUID, NULL) < 4) |
652 | if (__get_cpuid_max(BASIC_CPUID, NULL) < 4) |
653 | return 0; |
653 | return 0; |
654 | 654 | ||
655 | do { |
655 | do { |
656 | unsigned associativity, line_partitions, line_size, sets; |
656 | unsigned associativity, line_partitions, line_size, sets; |
657 | 657 | ||
658 | __cpuid_count(4, cnt++, eax, ebx, ecx, edx); |
658 | __cpuid_count(4, cnt++, eax, ebx, ecx, edx); |
659 | 659 | ||
660 | if ((eax & 0x1f) == 0) |
660 | if ((eax & 0x1f) == 0) |
661 | break; |
661 | break; |
662 | 662 | ||
663 | associativity = ((ebx >> 22) & 0x3ff) + 1; |
663 | associativity = ((ebx >> 22) & 0x3ff) + 1; |
664 | line_partitions = ((ebx >> 12) & 0x3ff) + 1; |
664 | line_partitions = ((ebx >> 12) & 0x3ff) + 1; |
665 | line_size = (ebx & 0xfff) + 1; |
665 | line_size = (ebx & 0xfff) + 1; |
666 | sets = ecx + 1; |
666 | sets = ecx + 1; |
667 | 667 | ||
668 | llc_size = associativity * line_partitions * line_size * sets; |
668 | llc_size = associativity * line_partitions * line_size * sets; |
669 | } while (1); |
669 | } while (1); |
670 | 670 | ||
671 | return llc_size; |
671 | return llc_size; |
672 | } |
672 | } |
673 | 673 | ||
674 | static int gem_param(struct kgem *kgem, int name) |
674 | static int gem_param(struct kgem *kgem, int name) |
675 | { |
675 | { |
676 | drm_i915_getparam_t gp; |
676 | drm_i915_getparam_t gp; |
677 | int v = -1; /* No param uses the sign bit, reserve it for errors */ |
677 | int v = -1; /* No param uses the sign bit, reserve it for errors */ |
678 | 678 | ||
679 | VG_CLEAR(gp); |
679 | VG_CLEAR(gp); |
680 | gp.param = name; |
680 | gp.param = name; |
681 | gp.value = &v; |
681 | gp.value = &v; |
682 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GETPARAM, &gp)) |
682 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GETPARAM, &gp)) |
683 | return -1; |
683 | return -1; |
684 | 684 | ||
685 | VG(VALGRIND_MAKE_MEM_DEFINED(&v, sizeof(v))); |
685 | VG(VALGRIND_MAKE_MEM_DEFINED(&v, sizeof(v))); |
686 | return v; |
686 | return v; |
687 | } |
687 | } |
688 | 688 | ||
689 | static bool test_has_execbuffer2(struct kgem *kgem) |
689 | static bool test_has_execbuffer2(struct kgem *kgem) |
690 | { |
690 | { |
691 | return 1; |
691 | return 1; |
692 | } |
692 | } |
693 | 693 | ||
694 | static bool test_has_no_reloc(struct kgem *kgem) |
694 | static bool test_has_no_reloc(struct kgem *kgem) |
695 | { |
695 | { |
696 | if (DBG_NO_FAST_RELOC) |
696 | if (DBG_NO_FAST_RELOC) |
697 | return false; |
697 | return false; |
698 | 698 | ||
699 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_NO_RELOC) > 0; |
699 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_NO_RELOC) > 0; |
700 | } |
700 | } |
701 | 701 | ||
702 | static bool test_has_handle_lut(struct kgem *kgem) |
702 | static bool test_has_handle_lut(struct kgem *kgem) |
703 | { |
703 | { |
704 | if (DBG_NO_HANDLE_LUT) |
704 | if (DBG_NO_HANDLE_LUT) |
705 | return false; |
705 | return false; |
706 | 706 | ||
707 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_HANDLE_LUT) > 0; |
707 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_HANDLE_LUT) > 0; |
708 | } |
708 | } |
709 | 709 | ||
710 | static bool test_has_wt(struct kgem *kgem) |
710 | static bool test_has_wt(struct kgem *kgem) |
711 | { |
711 | { |
712 | if (DBG_NO_WT) |
712 | if (DBG_NO_WT) |
713 | return false; |
713 | return false; |
714 | 714 | ||
715 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_WT) > 0; |
715 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_WT) > 0; |
716 | } |
716 | } |
717 | 717 | ||
718 | static bool test_has_semaphores_enabled(struct kgem *kgem) |
718 | static bool test_has_semaphores_enabled(struct kgem *kgem) |
719 | { |
719 | { |
720 | bool detected = false; |
720 | bool detected = false; |
721 | int ret; |
721 | int ret; |
722 | 722 | ||
723 | if (DBG_NO_SEMAPHORES) |
723 | if (DBG_NO_SEMAPHORES) |
724 | return false; |
724 | return false; |
725 | 725 | ||
726 | ret = gem_param(kgem, LOCAL_I915_PARAM_HAS_SEMAPHORES); |
726 | ret = gem_param(kgem, LOCAL_I915_PARAM_HAS_SEMAPHORES); |
727 | if (ret != -1) |
727 | if (ret != -1) |
728 | return ret > 0; |
728 | return ret > 0; |
729 | 729 | ||
730 | return detected; |
730 | return detected; |
731 | } |
731 | } |
732 | 732 | ||
733 | static bool __kgem_throttle(struct kgem *kgem) |
733 | static bool __kgem_throttle(struct kgem *kgem) |
734 | { |
734 | { |
735 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_THROTTLE, NULL) == 0) |
735 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_THROTTLE, NULL) == 0) |
736 | return false; |
736 | return false; |
737 | 737 | ||
738 | return errno == EIO; |
738 | return errno == EIO; |
739 | } |
739 | } |
740 | 740 | ||
741 | static bool is_hw_supported(struct kgem *kgem, |
741 | static bool is_hw_supported(struct kgem *kgem, |
742 | struct pci_device *dev) |
742 | struct pci_device *dev) |
743 | { |
743 | { |
744 | if (DBG_NO_HW) |
744 | if (DBG_NO_HW) |
745 | return false; |
745 | return false; |
746 | 746 | ||
747 | if (!test_has_execbuffer2(kgem)) |
747 | if (!test_has_execbuffer2(kgem)) |
748 | return false; |
748 | return false; |
749 | 749 | ||
750 | if (kgem->gen == (unsigned)-1) /* unknown chipset, assume future gen */ |
750 | if (kgem->gen == (unsigned)-1) /* unknown chipset, assume future gen */ |
751 | return kgem->has_blt; |
751 | return kgem->has_blt; |
752 | 752 | ||
753 | /* Although pre-855gm the GMCH is fubar, it works mostly. So |
753 | /* Although pre-855gm the GMCH is fubar, it works mostly. So |
754 | * let the user decide through "NoAccel" whether or not to risk |
754 | * let the user decide through "NoAccel" whether or not to risk |
755 | * hw acceleration. |
755 | * hw acceleration. |
756 | */ |
756 | */ |
757 | 757 | ||
758 | if (kgem->gen == 060 && dev->revision < 8) { |
758 | if (kgem->gen == 060 && dev->revision < 8) { |
759 | /* pre-production SNB with dysfunctional BLT */ |
759 | /* pre-production SNB with dysfunctional BLT */ |
760 | return false; |
760 | return false; |
761 | } |
761 | } |
762 | 762 | ||
763 | if (kgem->gen >= 060) /* Only if the kernel supports the BLT ring */ |
763 | if (kgem->gen >= 060) /* Only if the kernel supports the BLT ring */ |
764 | return kgem->has_blt; |
764 | return kgem->has_blt; |
765 | 765 | ||
766 | return true; |
766 | return true; |
767 | } |
767 | } |
768 | 768 | ||
769 | static bool test_has_relaxed_fencing(struct kgem *kgem) |
769 | static bool test_has_relaxed_fencing(struct kgem *kgem) |
770 | { |
770 | { |
771 | if (kgem->gen < 040) { |
771 | if (kgem->gen < 040) { |
772 | if (DBG_NO_RELAXED_FENCING) |
772 | if (DBG_NO_RELAXED_FENCING) |
773 | return false; |
773 | return false; |
774 | 774 | ||
775 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_FENCING) > 0; |
775 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_FENCING) > 0; |
776 | } else |
776 | } else |
777 | return true; |
777 | return true; |
778 | } |
778 | } |
779 | 779 | ||
780 | static bool test_has_llc(struct kgem *kgem) |
780 | static bool test_has_llc(struct kgem *kgem) |
781 | { |
781 | { |
782 | int has_llc = -1; |
782 | int has_llc = -1; |
783 | 783 | ||
784 | if (DBG_NO_LLC) |
784 | if (DBG_NO_LLC) |
785 | return false; |
785 | return false; |
786 | 786 | ||
787 | #if defined(I915_PARAM_HAS_LLC) /* Expected in libdrm-2.4.31 */ |
787 | #if defined(I915_PARAM_HAS_LLC) /* Expected in libdrm-2.4.31 */ |
788 | has_llc = gem_param(kgem, I915_PARAM_HAS_LLC); |
788 | has_llc = gem_param(kgem, I915_PARAM_HAS_LLC); |
789 | #endif |
789 | #endif |
790 | if (has_llc == -1) { |
790 | if (has_llc == -1) { |
791 | DBG(("%s: no kernel/drm support for HAS_LLC, assuming support for LLC based on GPU generation\n", __FUNCTION__)); |
791 | DBG(("%s: no kernel/drm support for HAS_LLC, assuming support for LLC based on GPU generation\n", __FUNCTION__)); |
792 | has_llc = kgem->gen >= 060; |
792 | has_llc = kgem->gen >= 060; |
793 | } |
793 | } |
794 | 794 | ||
795 | return has_llc; |
795 | return has_llc; |
796 | } |
796 | } |
797 | 797 | ||
798 | static bool test_has_caching(struct kgem *kgem) |
798 | static bool test_has_caching(struct kgem *kgem) |
799 | { |
799 | { |
800 | uint32_t handle; |
800 | uint32_t handle; |
801 | bool ret; |
801 | bool ret; |
802 | 802 | ||
803 | if (DBG_NO_CACHE_LEVEL) |
803 | if (DBG_NO_CACHE_LEVEL) |
804 | return false; |
804 | return false; |
805 | 805 | ||
806 | /* Incoherent blt and sampler hangs the GPU */ |
806 | /* Incoherent blt and sampler hangs the GPU */ |
807 | if (kgem->gen == 040) |
807 | if (kgem->gen == 040) |
808 | return false; |
808 | return false; |
809 | 809 | ||
810 | handle = gem_create(kgem->fd, 1); |
810 | handle = gem_create(kgem->fd, 1); |
811 | if (handle == 0) |
811 | if (handle == 0) |
812 | return false; |
812 | return false; |
813 | 813 | ||
814 | ret = gem_set_caching(kgem->fd, handle, UNCACHED); |
814 | ret = gem_set_caching(kgem->fd, handle, UNCACHED); |
815 | gem_close(kgem->fd, handle); |
815 | gem_close(kgem->fd, handle); |
816 | return ret; |
816 | return ret; |
817 | } |
817 | } |
818 | 818 | ||
819 | static bool test_has_userptr(struct kgem *kgem) |
819 | static bool test_has_userptr(struct kgem *kgem) |
820 | { |
820 | { |
821 | #if defined(USE_USERPTR) |
821 | #if defined(USE_USERPTR) |
822 | uint32_t handle; |
822 | uint32_t handle; |
823 | void *ptr; |
823 | void *ptr; |
824 | 824 | ||
825 | if (DBG_NO_USERPTR) |
825 | if (DBG_NO_USERPTR) |
826 | return false; |
826 | return false; |
827 | 827 | ||
828 | /* Incoherent blt and sampler hangs the GPU */ |
828 | /* Incoherent blt and sampler hangs the GPU */ |
829 | if (kgem->gen == 040) |
829 | if (kgem->gen == 040) |
830 | return false; |
830 | return false; |
831 | 831 | ||
832 | if (posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE)) |
832 | if (posix_memalign(&ptr, PAGE_SIZE, PAGE_SIZE)) |
833 | return false; |
833 | return false; |
834 | 834 | ||
835 | handle = gem_userptr(kgem->fd, ptr, PAGE_SIZE, false); |
835 | handle = gem_userptr(kgem->fd, ptr, PAGE_SIZE, false); |
836 | gem_close(kgem->fd, handle); |
836 | gem_close(kgem->fd, handle); |
837 | free(ptr); |
837 | free(ptr); |
838 | 838 | ||
839 | return handle != 0; |
839 | return handle != 0; |
840 | #else |
840 | #else |
841 | return false; |
841 | return false; |
842 | #endif |
842 | #endif |
843 | } |
843 | } |
844 | 844 | ||
845 | static bool test_has_create2(struct kgem *kgem) |
845 | static bool test_has_create2(struct kgem *kgem) |
846 | { |
846 | { |
847 | #if defined(USE_CREATE2) |
847 | #if defined(USE_CREATE2) |
848 | struct local_i915_gem_create2 args; |
848 | struct local_i915_gem_create2 args; |
849 | 849 | ||
850 | if (DBG_NO_CREATE2) |
850 | if (DBG_NO_CREATE2) |
851 | return false; |
851 | return false; |
852 | 852 | ||
853 | memset(&args, 0, sizeof(args)); |
853 | memset(&args, 0, sizeof(args)); |
854 | args.size = PAGE_SIZE; |
854 | args.size = PAGE_SIZE; |
855 | args.caching = DISPLAY; |
855 | args.caching = DISPLAY; |
856 | if (drmIoctl(kgem->fd, LOCAL_IOCTL_I915_GEM_CREATE2, &args) == 0) |
856 | if (drmIoctl(kgem->fd, LOCAL_IOCTL_I915_GEM_CREATE2, &args) == 0) |
857 | gem_close(kgem->fd, args.handle); |
857 | gem_close(kgem->fd, args.handle); |
858 | 858 | ||
859 | return args.handle != 0; |
859 | return args.handle != 0; |
860 | #else |
860 | #else |
861 | return false; |
861 | return false; |
862 | #endif |
862 | #endif |
863 | } |
863 | } |
864 | 864 | ||
865 | static bool test_has_secure_batches(struct kgem *kgem) |
865 | static bool test_has_secure_batches(struct kgem *kgem) |
866 | { |
866 | { |
867 | if (DBG_NO_SECURE_BATCHES) |
867 | if (DBG_NO_SECURE_BATCHES) |
868 | return false; |
868 | return false; |
869 | 869 | ||
870 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_SECURE_BATCHES) > 0; |
870 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_SECURE_BATCHES) > 0; |
871 | } |
871 | } |
872 | 872 | ||
873 | static bool test_has_pinned_batches(struct kgem *kgem) |
873 | static bool test_has_pinned_batches(struct kgem *kgem) |
874 | { |
874 | { |
875 | if (DBG_NO_PINNED_BATCHES) |
875 | if (DBG_NO_PINNED_BATCHES) |
876 | return false; |
876 | return false; |
877 | 877 | ||
878 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_PINNED_BATCHES) > 0; |
878 | return gem_param(kgem, LOCAL_I915_PARAM_HAS_PINNED_BATCHES) > 0; |
879 | } |
879 | } |
880 | 880 | ||
881 | 881 | ||
882 | static bool kgem_init_pinned_batches(struct kgem *kgem) |
882 | static bool kgem_init_pinned_batches(struct kgem *kgem) |
883 | { |
883 | { |
884 | int count[2] = { 2, 2 }; |
884 | int count[2] = { 2, 2 }; |
885 | int size[2] = { 1, 2 }; |
885 | int size[2] = { 1, 2 }; |
886 | int n, i; |
886 | int n, i; |
887 | 887 | ||
888 | if (kgem->wedged) |
888 | if (kgem->wedged) |
889 | return true; |
889 | return true; |
890 | 890 | ||
891 | for (n = 0; n < ARRAY_SIZE(count); n++) { |
891 | for (n = 0; n < ARRAY_SIZE(count); n++) { |
892 | for (i = 0; i < count[n]; i++) { |
892 | for (i = 0; i < count[n]; i++) { |
893 | struct drm_i915_gem_pin pin; |
893 | struct drm_i915_gem_pin pin; |
894 | struct kgem_bo *bo; |
894 | struct kgem_bo *bo; |
895 | 895 | ||
896 | VG_CLEAR(pin); |
896 | VG_CLEAR(pin); |
897 | 897 | ||
898 | pin.handle = gem_create(kgem->fd, size[n]); |
898 | pin.handle = gem_create(kgem->fd, size[n]); |
899 | if (pin.handle == 0) |
899 | if (pin.handle == 0) |
900 | goto err; |
900 | goto err; |
901 | 901 | ||
902 | DBG(("%s: new handle=%d, num_pages=%d\n", |
902 | DBG(("%s: new handle=%d, num_pages=%d\n", |
903 | __FUNCTION__, pin.handle, size[n])); |
903 | __FUNCTION__, pin.handle, size[n])); |
904 | 904 | ||
905 | bo = __kgem_bo_alloc(pin.handle, size[n]); |
905 | bo = __kgem_bo_alloc(pin.handle, size[n]); |
906 | if (bo == NULL) { |
906 | if (bo == NULL) { |
907 | gem_close(kgem->fd, pin.handle); |
907 | gem_close(kgem->fd, pin.handle); |
908 | goto err; |
908 | goto err; |
909 | } |
909 | } |
910 | 910 | ||
911 | pin.alignment = 0; |
911 | pin.alignment = 0; |
912 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_PIN, &pin)) { |
912 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_PIN, &pin)) { |
913 | gem_close(kgem->fd, pin.handle); |
913 | gem_close(kgem->fd, pin.handle); |
914 | goto err; |
914 | goto err; |
915 | } |
915 | } |
916 | bo->presumed_offset = pin.offset; |
916 | bo->presumed_offset = pin.offset; |
917 | debug_alloc__bo(kgem, bo); |
917 | debug_alloc__bo(kgem, bo); |
918 | list_add(&bo->list, &kgem->pinned_batches[n]); |
918 | list_add(&bo->list, &kgem->pinned_batches[n]); |
919 | } |
919 | } |
920 | } |
920 | } |
921 | 921 | ||
922 | return true; |
922 | return true; |
923 | 923 | ||
924 | err: |
924 | err: |
925 | for (n = 0; n < ARRAY_SIZE(kgem->pinned_batches); n++) { |
925 | for (n = 0; n < ARRAY_SIZE(kgem->pinned_batches); n++) { |
926 | while (!list_is_empty(&kgem->pinned_batches[n])) { |
926 | while (!list_is_empty(&kgem->pinned_batches[n])) { |
927 | kgem_bo_destroy(kgem, |
927 | kgem_bo_destroy(kgem, |
928 | list_first_entry(&kgem->pinned_batches[n], |
928 | list_first_entry(&kgem->pinned_batches[n], |
929 | struct kgem_bo, list)); |
929 | struct kgem_bo, list)); |
930 | } |
930 | } |
931 | } |
931 | } |
932 | 932 | ||
933 | /* For simplicity populate the lists with a single unpinned bo */ |
933 | /* For simplicity populate the lists with a single unpinned bo */ |
934 | for (n = 0; n < ARRAY_SIZE(count); n++) { |
934 | for (n = 0; n < ARRAY_SIZE(count); n++) { |
935 | struct kgem_bo *bo; |
935 | struct kgem_bo *bo; |
936 | uint32_t handle; |
936 | uint32_t handle; |
937 | 937 | ||
938 | handle = gem_create(kgem->fd, size[n]); |
938 | handle = gem_create(kgem->fd, size[n]); |
939 | if (handle == 0) |
939 | if (handle == 0) |
940 | break; |
940 | break; |
941 | 941 | ||
942 | bo = __kgem_bo_alloc(handle, size[n]); |
942 | bo = __kgem_bo_alloc(handle, size[n]); |
943 | if (bo == NULL) { |
943 | if (bo == NULL) { |
944 | gem_close(kgem->fd, handle); |
944 | gem_close(kgem->fd, handle); |
945 | break; |
945 | break; |
946 | } |
946 | } |
947 | 947 | ||
948 | debug_alloc__bo(kgem, bo); |
948 | debug_alloc__bo(kgem, bo); |
949 | list_add(&bo->list, &kgem->pinned_batches[n]); |
949 | list_add(&bo->list, &kgem->pinned_batches[n]); |
950 | } |
950 | } |
951 | return false; |
951 | return false; |
952 | } |
952 | } |
953 | 953 | ||
954 | void kgem_init(struct kgem *kgem, int fd, struct pci_device *dev, unsigned gen) |
954 | void kgem_init(struct kgem *kgem, int fd, struct pci_device *dev, unsigned gen) |
955 | { |
955 | { |
956 | struct drm_i915_gem_get_aperture aperture; |
956 | struct drm_i915_gem_get_aperture aperture; |
957 | size_t totalram; |
957 | size_t totalram; |
958 | unsigned half_gpu_max; |
958 | unsigned half_gpu_max; |
959 | unsigned int i, j; |
959 | unsigned int i, j; |
960 | 960 | ||
961 | DBG(("%s: fd=%d, gen=%d\n", __FUNCTION__, fd, gen)); |
961 | DBG(("%s: fd=%d, gen=%d\n", __FUNCTION__, fd, gen)); |
962 | 962 | ||
963 | memset(kgem, 0, sizeof(*kgem)); |
963 | memset(kgem, 0, sizeof(*kgem)); |
964 | 964 | ||
965 | kgem->fd = fd; |
965 | kgem->fd = fd; |
966 | kgem->gen = gen; |
966 | kgem->gen = gen; |
967 | 967 | ||
968 | list_init(&kgem->requests[0]); |
968 | list_init(&kgem->requests[0]); |
969 | list_init(&kgem->requests[1]); |
969 | list_init(&kgem->requests[1]); |
970 | list_init(&kgem->batch_buffers); |
970 | list_init(&kgem->batch_buffers); |
971 | list_init(&kgem->active_buffers); |
971 | list_init(&kgem->active_buffers); |
972 | list_init(&kgem->flushing); |
972 | list_init(&kgem->flushing); |
973 | list_init(&kgem->large); |
973 | list_init(&kgem->large); |
974 | list_init(&kgem->large_inactive); |
974 | list_init(&kgem->large_inactive); |
975 | list_init(&kgem->snoop); |
975 | list_init(&kgem->snoop); |
976 | list_init(&kgem->scanout); |
976 | list_init(&kgem->scanout); |
977 | for (i = 0; i < ARRAY_SIZE(kgem->pinned_batches); i++) |
977 | for (i = 0; i < ARRAY_SIZE(kgem->pinned_batches); i++) |
978 | list_init(&kgem->pinned_batches[i]); |
978 | list_init(&kgem->pinned_batches[i]); |
979 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) |
979 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) |
980 | list_init(&kgem->inactive[i]); |
980 | list_init(&kgem->inactive[i]); |
981 | for (i = 0; i < ARRAY_SIZE(kgem->active); i++) { |
981 | for (i = 0; i < ARRAY_SIZE(kgem->active); i++) { |
982 | for (j = 0; j < ARRAY_SIZE(kgem->active[i]); j++) |
982 | for (j = 0; j < ARRAY_SIZE(kgem->active[i]); j++) |
983 | list_init(&kgem->active[i][j]); |
983 | list_init(&kgem->active[i][j]); |
984 | } |
984 | } |
985 | for (i = 0; i < ARRAY_SIZE(kgem->vma); i++) { |
985 | for (i = 0; i < ARRAY_SIZE(kgem->vma); i++) { |
986 | for (j = 0; j < ARRAY_SIZE(kgem->vma[i].inactive); j++) |
986 | for (j = 0; j < ARRAY_SIZE(kgem->vma[i].inactive); j++) |
987 | list_init(&kgem->vma[i].inactive[j]); |
987 | list_init(&kgem->vma[i].inactive[j]); |
988 | } |
988 | } |
989 | kgem->vma[MAP_GTT].count = -MAX_GTT_VMA_CACHE; |
989 | kgem->vma[MAP_GTT].count = -MAX_GTT_VMA_CACHE; |
990 | kgem->vma[MAP_CPU].count = -MAX_CPU_VMA_CACHE; |
990 | kgem->vma[MAP_CPU].count = -MAX_CPU_VMA_CACHE; |
991 | 991 | ||
992 | kgem->has_blt = gem_param(kgem, LOCAL_I915_PARAM_HAS_BLT) > 0; |
992 | kgem->has_blt = gem_param(kgem, LOCAL_I915_PARAM_HAS_BLT) > 0; |
993 | DBG(("%s: has BLT ring? %d\n", __FUNCTION__, |
993 | DBG(("%s: has BLT ring? %d\n", __FUNCTION__, |
994 | kgem->has_blt)); |
994 | kgem->has_blt)); |
995 | 995 | ||
996 | kgem->has_relaxed_delta = |
996 | kgem->has_relaxed_delta = |
997 | gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_DELTA) > 0; |
997 | gem_param(kgem, LOCAL_I915_PARAM_HAS_RELAXED_DELTA) > 0; |
998 | DBG(("%s: has relaxed delta? %d\n", __FUNCTION__, |
998 | DBG(("%s: has relaxed delta? %d\n", __FUNCTION__, |
999 | kgem->has_relaxed_delta)); |
999 | kgem->has_relaxed_delta)); |
1000 | 1000 | ||
1001 | kgem->has_relaxed_fencing = test_has_relaxed_fencing(kgem); |
1001 | kgem->has_relaxed_fencing = test_has_relaxed_fencing(kgem); |
1002 | DBG(("%s: has relaxed fencing? %d\n", __FUNCTION__, |
1002 | DBG(("%s: has relaxed fencing? %d\n", __FUNCTION__, |
1003 | kgem->has_relaxed_fencing)); |
1003 | kgem->has_relaxed_fencing)); |
1004 | 1004 | ||
1005 | kgem->has_llc = test_has_llc(kgem); |
1005 | kgem->has_llc = test_has_llc(kgem); |
1006 | DBG(("%s: has shared last-level-cache? %d\n", __FUNCTION__, |
1006 | DBG(("%s: has shared last-level-cache? %d\n", __FUNCTION__, |
1007 | kgem->has_llc)); |
1007 | kgem->has_llc)); |
1008 | 1008 | ||
1009 | kgem->has_wt = test_has_wt(kgem); |
1009 | kgem->has_wt = test_has_wt(kgem); |
1010 | DBG(("%s: has write-through caching for scanouts? %d\n", __FUNCTION__, |
1010 | DBG(("%s: has write-through caching for scanouts? %d\n", __FUNCTION__, |
1011 | kgem->has_wt)); |
1011 | kgem->has_wt)); |
1012 | 1012 | ||
1013 | kgem->has_caching = test_has_caching(kgem); |
1013 | kgem->has_caching = test_has_caching(kgem); |
1014 | DBG(("%s: has set-cache-level? %d\n", __FUNCTION__, |
1014 | DBG(("%s: has set-cache-level? %d\n", __FUNCTION__, |
1015 | kgem->has_caching)); |
1015 | kgem->has_caching)); |
1016 | 1016 | ||
1017 | kgem->has_userptr = test_has_userptr(kgem); |
1017 | kgem->has_userptr = test_has_userptr(kgem); |
1018 | DBG(("%s: has userptr? %d\n", __FUNCTION__, |
1018 | DBG(("%s: has userptr? %d\n", __FUNCTION__, |
1019 | kgem->has_userptr)); |
1019 | kgem->has_userptr)); |
1020 | 1020 | ||
1021 | kgem->has_create2 = test_has_create2(kgem); |
1021 | kgem->has_create2 = test_has_create2(kgem); |
1022 | kgem->has_create2 = 0; |
1022 | kgem->has_create2 = 0; |
1023 | DBG(("%s: has create2? %d\n", __FUNCTION__, |
1023 | DBG(("%s: has create2? %d\n", __FUNCTION__, |
1024 | kgem->has_create2)); |
1024 | kgem->has_create2)); |
1025 | 1025 | ||
1026 | kgem->has_no_reloc = test_has_no_reloc(kgem); |
1026 | kgem->has_no_reloc = test_has_no_reloc(kgem); |
1027 | DBG(("%s: has no-reloc? %d\n", __FUNCTION__, |
1027 | DBG(("%s: has no-reloc? %d\n", __FUNCTION__, |
1028 | kgem->has_no_reloc)); |
1028 | kgem->has_no_reloc)); |
1029 | 1029 | ||
1030 | kgem->has_handle_lut = test_has_handle_lut(kgem); |
1030 | kgem->has_handle_lut = test_has_handle_lut(kgem); |
1031 | kgem->has_handle_lut = 0; |
1031 | kgem->has_handle_lut = 0; |
1032 | DBG(("%s: has handle-lut? %d\n", __FUNCTION__, |
1032 | DBG(("%s: has handle-lut? %d\n", __FUNCTION__, |
1033 | kgem->has_handle_lut)); |
1033 | kgem->has_handle_lut)); |
1034 | 1034 | ||
1035 | kgem->has_semaphores = false; |
1035 | kgem->has_semaphores = false; |
1036 | if (kgem->has_blt && test_has_semaphores_enabled(kgem)) |
1036 | if (kgem->has_blt && test_has_semaphores_enabled(kgem)) |
1037 | kgem->has_semaphores = true; |
1037 | kgem->has_semaphores = true; |
1038 | DBG(("%s: semaphores enabled? %d\n", __FUNCTION__, |
1038 | DBG(("%s: semaphores enabled? %d\n", __FUNCTION__, |
1039 | kgem->has_semaphores)); |
1039 | kgem->has_semaphores)); |
1040 | 1040 | ||
1041 | kgem->can_blt_cpu = gen >= 030; |
1041 | kgem->can_blt_cpu = gen >= 030; |
1042 | DBG(("%s: can blt to cpu? %d\n", __FUNCTION__, |
1042 | DBG(("%s: can blt to cpu? %d\n", __FUNCTION__, |
1043 | kgem->can_blt_cpu)); |
1043 | kgem->can_blt_cpu)); |
1044 | 1044 | ||
1045 | kgem->has_secure_batches = test_has_secure_batches(kgem); |
1045 | kgem->has_secure_batches = test_has_secure_batches(kgem); |
1046 | DBG(("%s: can use privileged batchbuffers? %d\n", __FUNCTION__, |
1046 | DBG(("%s: can use privileged batchbuffers? %d\n", __FUNCTION__, |
1047 | kgem->has_secure_batches)); |
1047 | kgem->has_secure_batches)); |
1048 | 1048 | ||
1049 | kgem->has_pinned_batches = test_has_pinned_batches(kgem); |
1049 | kgem->has_pinned_batches = test_has_pinned_batches(kgem); |
1050 | DBG(("%s: can use pinned batchbuffers (to avoid CS w/a)? %d\n", __FUNCTION__, |
1050 | DBG(("%s: can use pinned batchbuffers (to avoid CS w/a)? %d\n", __FUNCTION__, |
1051 | kgem->has_pinned_batches)); |
1051 | kgem->has_pinned_batches)); |
1052 | 1052 | ||
1053 | if (!is_hw_supported(kgem, dev)) { |
1053 | if (!is_hw_supported(kgem, dev)) { |
1054 | printf("Detected unsupported/dysfunctional hardware, disabling acceleration.\n"); |
1054 | printf("Detected unsupported/dysfunctional hardware, disabling acceleration.\n"); |
1055 | kgem->wedged = 1; |
1055 | kgem->wedged = 1; |
1056 | } else if (__kgem_throttle(kgem)) { |
1056 | } else if (__kgem_throttle(kgem)) { |
1057 | printf("Detected a hung GPU, disabling acceleration.\n"); |
1057 | printf("Detected a hung GPU, disabling acceleration.\n"); |
1058 | kgem->wedged = 1; |
1058 | kgem->wedged = 1; |
1059 | } |
1059 | } |
1060 | 1060 | ||
1061 | kgem->batch_size = ARRAY_SIZE(kgem->batch); |
1061 | kgem->batch_size = ARRAY_SIZE(kgem->batch); |
1062 | if (gen == 020 && !kgem->has_pinned_batches) |
1062 | if (gen == 020 && !kgem->has_pinned_batches) |
1063 | /* Limited to what we can pin */ |
1063 | /* Limited to what we can pin */ |
1064 | kgem->batch_size = 4*1024; |
1064 | kgem->batch_size = 4*1024; |
1065 | if (gen == 022) |
1065 | if (gen == 022) |
1066 | /* 865g cannot handle a batch spanning multiple pages */ |
1066 | /* 865g cannot handle a batch spanning multiple pages */ |
1067 | kgem->batch_size = PAGE_SIZE / sizeof(uint32_t); |
1067 | kgem->batch_size = PAGE_SIZE / sizeof(uint32_t); |
1068 | if ((gen >> 3) == 7) |
1068 | if ((gen >> 3) == 7) |
1069 | kgem->batch_size = 16*1024; |
1069 | kgem->batch_size = 16*1024; |
1070 | if (!kgem->has_relaxed_delta && kgem->batch_size > 4*1024) |
1070 | if (!kgem->has_relaxed_delta && kgem->batch_size > 4*1024) |
1071 | kgem->batch_size = 4*1024; |
1071 | kgem->batch_size = 4*1024; |
1072 | 1072 | ||
1073 | if (!kgem_init_pinned_batches(kgem) && gen == 020) { |
1073 | if (!kgem_init_pinned_batches(kgem) && gen == 020) { |
1074 | printf("Unable to reserve memory for GPU, disabling acceleration.\n"); |
1074 | printf("Unable to reserve memory for GPU, disabling acceleration.\n"); |
1075 | kgem->wedged = 1; |
1075 | kgem->wedged = 1; |
1076 | } |
1076 | } |
1077 | 1077 | ||
1078 | DBG(("%s: maximum batch size? %d\n", __FUNCTION__, |
1078 | DBG(("%s: maximum batch size? %d\n", __FUNCTION__, |
1079 | kgem->batch_size)); |
1079 | kgem->batch_size)); |
1080 | 1080 | ||
1081 | kgem->min_alignment = 4; |
1081 | kgem->min_alignment = 16; |
1082 | if (gen < 040) |
1082 | if (gen < 040) |
1083 | kgem->min_alignment = 64; |
1083 | kgem->min_alignment = 64; |
1084 | 1084 | ||
1085 | kgem->half_cpu_cache_pages = cpu_cache_size() >> 13; |
1085 | kgem->half_cpu_cache_pages = cpu_cache_size() >> 13; |
1086 | DBG(("%s: last-level cache size: %d bytes, threshold in pages: %d\n", |
1086 | DBG(("%s: last-level cache size: %d bytes, threshold in pages: %d\n", |
1087 | __FUNCTION__, cpu_cache_size(), kgem->half_cpu_cache_pages)); |
1087 | __FUNCTION__, cpu_cache_size(), kgem->half_cpu_cache_pages)); |
1088 | 1088 | ||
1089 | kgem->next_request = __kgem_request_alloc(kgem); |
1089 | kgem->next_request = __kgem_request_alloc(kgem); |
1090 | 1090 | ||
1091 | DBG(("%s: cpu bo enabled %d: llc? %d, set-cache-level? %d, userptr? %d\n", __FUNCTION__, |
1091 | DBG(("%s: cpu bo enabled %d: llc? %d, set-cache-level? %d, userptr? %d\n", __FUNCTION__, |
1092 | !DBG_NO_CPU && (kgem->has_llc | kgem->has_userptr | kgem->has_caching), |
1092 | !DBG_NO_CPU && (kgem->has_llc | kgem->has_userptr | kgem->has_caching), |
1093 | kgem->has_llc, kgem->has_caching, kgem->has_userptr)); |
1093 | kgem->has_llc, kgem->has_caching, kgem->has_userptr)); |
1094 | 1094 | ||
1095 | VG_CLEAR(aperture); |
1095 | VG_CLEAR(aperture); |
1096 | aperture.aper_size = 0; |
1096 | aperture.aper_size = 0; |
1097 | (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture); |
1097 | (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture); |
1098 | if (aperture.aper_size == 0) |
1098 | if (aperture.aper_size == 0) |
1099 | aperture.aper_size = 64*1024*1024; |
1099 | aperture.aper_size = 64*1024*1024; |
1100 | 1100 | ||
1101 | DBG(("%s: aperture size %lld, available now %lld\n", |
1101 | DBG(("%s: aperture size %lld, available now %lld\n", |
1102 | __FUNCTION__, |
1102 | __FUNCTION__, |
1103 | (long long)aperture.aper_size, |
1103 | (long long)aperture.aper_size, |
1104 | (long long)aperture.aper_available_size)); |
1104 | (long long)aperture.aper_available_size)); |
1105 | 1105 | ||
1106 | kgem->aperture_total = aperture.aper_size; |
1106 | kgem->aperture_total = aperture.aper_size; |
1107 | kgem->aperture_high = aperture.aper_size * 3/4; |
1107 | kgem->aperture_high = aperture.aper_size * 3/4; |
1108 | kgem->aperture_low = aperture.aper_size * 1/3; |
1108 | kgem->aperture_low = aperture.aper_size * 1/3; |
1109 | if (gen < 033) { |
1109 | if (gen < 033) { |
1110 | /* Severe alignment penalties */ |
1110 | /* Severe alignment penalties */ |
1111 | kgem->aperture_high /= 2; |
1111 | kgem->aperture_high /= 2; |
1112 | kgem->aperture_low /= 2; |
1112 | kgem->aperture_low /= 2; |
1113 | } |
1113 | } |
1114 | DBG(("%s: aperture low=%d [%d], high=%d [%d]\n", __FUNCTION__, |
1114 | DBG(("%s: aperture low=%d [%d], high=%d [%d]\n", __FUNCTION__, |
1115 | kgem->aperture_low, kgem->aperture_low / (1024*1024), |
1115 | kgem->aperture_low, kgem->aperture_low / (1024*1024), |
1116 | kgem->aperture_high, kgem->aperture_high / (1024*1024))); |
1116 | kgem->aperture_high, kgem->aperture_high / (1024*1024))); |
1117 | 1117 | ||
1118 | kgem->aperture_mappable = agp_aperture_size(dev, gen); |
1118 | kgem->aperture_mappable = agp_aperture_size(dev, gen); |
1119 | if (kgem->aperture_mappable == 0 || |
1119 | if (kgem->aperture_mappable == 0 || |
1120 | kgem->aperture_mappable > aperture.aper_size) |
1120 | kgem->aperture_mappable > aperture.aper_size) |
1121 | kgem->aperture_mappable = aperture.aper_size; |
1121 | kgem->aperture_mappable = aperture.aper_size; |
1122 | DBG(("%s: aperture mappable=%d [%d MiB]\n", __FUNCTION__, |
1122 | DBG(("%s: aperture mappable=%d [%d MiB]\n", __FUNCTION__, |
1123 | kgem->aperture_mappable, kgem->aperture_mappable / (1024*1024))); |
1123 | kgem->aperture_mappable, kgem->aperture_mappable / (1024*1024))); |
1124 | 1124 | ||
1125 | kgem->buffer_size = 64 * 1024; |
1125 | kgem->buffer_size = 64 * 1024; |
1126 | while (kgem->buffer_size < kgem->aperture_mappable >> 10) |
1126 | while (kgem->buffer_size < kgem->aperture_mappable >> 10) |
1127 | kgem->buffer_size *= 2; |
1127 | kgem->buffer_size *= 2; |
1128 | if (kgem->buffer_size >> 12 > kgem->half_cpu_cache_pages) |
1128 | if (kgem->buffer_size >> 12 > kgem->half_cpu_cache_pages) |
1129 | kgem->buffer_size = kgem->half_cpu_cache_pages << 12; |
1129 | kgem->buffer_size = kgem->half_cpu_cache_pages << 12; |
1130 | kgem->buffer_size = 1 << __fls(kgem->buffer_size); |
1130 | kgem->buffer_size = 1 << __fls(kgem->buffer_size); |
1131 | DBG(("%s: buffer size=%d [%d KiB]\n", __FUNCTION__, |
1131 | DBG(("%s: buffer size=%d [%d KiB]\n", __FUNCTION__, |
1132 | kgem->buffer_size, kgem->buffer_size / 1024)); |
1132 | kgem->buffer_size, kgem->buffer_size / 1024)); |
1133 | assert(kgem->buffer_size); |
1133 | assert(kgem->buffer_size); |
1134 | 1134 | ||
1135 | kgem->max_object_size = 3 * (kgem->aperture_high >> 12) << 10; |
1135 | kgem->max_object_size = 3 * (kgem->aperture_high >> 12) << 10; |
1136 | kgem->max_gpu_size = kgem->max_object_size; |
1136 | kgem->max_gpu_size = kgem->max_object_size; |
1137 | if (!kgem->has_llc && kgem->max_gpu_size > MAX_CACHE_SIZE) |
1137 | if (!kgem->has_llc && kgem->max_gpu_size > MAX_CACHE_SIZE) |
1138 | kgem->max_gpu_size = MAX_CACHE_SIZE; |
1138 | kgem->max_gpu_size = MAX_CACHE_SIZE; |
1139 | 1139 | ||
1140 | totalram = total_ram_size(); |
1140 | totalram = total_ram_size(); |
1141 | if (totalram == 0) { |
1141 | if (totalram == 0) { |
1142 | DBG(("%s: total ram size unknown, assuming maximum of total aperture\n", |
1142 | DBG(("%s: total ram size unknown, assuming maximum of total aperture\n", |
1143 | __FUNCTION__)); |
1143 | __FUNCTION__)); |
1144 | totalram = kgem->aperture_total; |
1144 | totalram = kgem->aperture_total; |
1145 | } |
1145 | } |
1146 | DBG(("%s: total ram=%ld\n", __FUNCTION__, (long)totalram)); |
1146 | DBG(("%s: total ram=%ld\n", __FUNCTION__, (long)totalram)); |
1147 | if (kgem->max_object_size > totalram / 2) |
1147 | if (kgem->max_object_size > totalram / 2) |
1148 | kgem->max_object_size = totalram / 2; |
1148 | kgem->max_object_size = totalram / 2; |
1149 | if (kgem->max_gpu_size > totalram / 4) |
1149 | if (kgem->max_gpu_size > totalram / 4) |
1150 | kgem->max_gpu_size = totalram / 4; |
1150 | kgem->max_gpu_size = totalram / 4; |
1151 | 1151 | ||
1152 | kgem->max_cpu_size = kgem->max_object_size; |
1152 | kgem->max_cpu_size = kgem->max_object_size; |
1153 | 1153 | ||
1154 | half_gpu_max = kgem->max_gpu_size / 2; |
1154 | half_gpu_max = kgem->max_gpu_size / 2; |
1155 | kgem->max_copy_tile_size = (MAX_CACHE_SIZE + 1)/2; |
1155 | kgem->max_copy_tile_size = (MAX_CACHE_SIZE + 1)/2; |
1156 | if (kgem->max_copy_tile_size > half_gpu_max) |
1156 | if (kgem->max_copy_tile_size > half_gpu_max) |
1157 | kgem->max_copy_tile_size = half_gpu_max; |
1157 | kgem->max_copy_tile_size = half_gpu_max; |
1158 | 1158 | ||
1159 | if (kgem->has_llc) |
1159 | if (kgem->has_llc) |
1160 | kgem->max_upload_tile_size = kgem->max_copy_tile_size; |
1160 | kgem->max_upload_tile_size = kgem->max_copy_tile_size; |
1161 | else |
1161 | else |
1162 | kgem->max_upload_tile_size = kgem->aperture_mappable / 4; |
1162 | kgem->max_upload_tile_size = kgem->aperture_mappable / 4; |
1163 | if (kgem->max_upload_tile_size > half_gpu_max) |
1163 | if (kgem->max_upload_tile_size > half_gpu_max) |
1164 | kgem->max_upload_tile_size = half_gpu_max; |
1164 | kgem->max_upload_tile_size = half_gpu_max; |
1165 | if (kgem->max_upload_tile_size > kgem->aperture_high/2) |
1165 | if (kgem->max_upload_tile_size > kgem->aperture_high/2) |
1166 | kgem->max_upload_tile_size = kgem->aperture_high/2; |
1166 | kgem->max_upload_tile_size = kgem->aperture_high/2; |
1167 | if (kgem->max_upload_tile_size > kgem->aperture_low) |
1167 | if (kgem->max_upload_tile_size > kgem->aperture_low) |
1168 | kgem->max_upload_tile_size = kgem->aperture_low; |
1168 | kgem->max_upload_tile_size = kgem->aperture_low; |
1169 | if (kgem->max_upload_tile_size < 16*PAGE_SIZE) |
1169 | if (kgem->max_upload_tile_size < 16*PAGE_SIZE) |
1170 | kgem->max_upload_tile_size = 16*PAGE_SIZE; |
1170 | kgem->max_upload_tile_size = 16*PAGE_SIZE; |
1171 | 1171 | ||
1172 | kgem->large_object_size = MAX_CACHE_SIZE; |
1172 | kgem->large_object_size = MAX_CACHE_SIZE; |
1173 | if (kgem->large_object_size > half_gpu_max) |
1173 | if (kgem->large_object_size > half_gpu_max) |
1174 | kgem->large_object_size = half_gpu_max; |
1174 | kgem->large_object_size = half_gpu_max; |
1175 | if (kgem->max_copy_tile_size > kgem->aperture_high/2) |
1175 | if (kgem->max_copy_tile_size > kgem->aperture_high/2) |
1176 | kgem->max_copy_tile_size = kgem->aperture_high/2; |
1176 | kgem->max_copy_tile_size = kgem->aperture_high/2; |
1177 | if (kgem->max_copy_tile_size > kgem->aperture_low) |
1177 | if (kgem->max_copy_tile_size > kgem->aperture_low) |
1178 | kgem->max_copy_tile_size = kgem->aperture_low; |
1178 | kgem->max_copy_tile_size = kgem->aperture_low; |
1179 | if (kgem->max_copy_tile_size < 16*PAGE_SIZE) |
1179 | if (kgem->max_copy_tile_size < 16*PAGE_SIZE) |
1180 | kgem->max_copy_tile_size = 16*PAGE_SIZE; |
1180 | kgem->max_copy_tile_size = 16*PAGE_SIZE; |
1181 | 1181 | ||
1182 | if (kgem->has_llc | kgem->has_caching | kgem->has_userptr) { |
1182 | if (kgem->has_llc | kgem->has_caching | kgem->has_userptr) { |
1183 | if (kgem->large_object_size > kgem->max_cpu_size) |
1183 | if (kgem->large_object_size > kgem->max_cpu_size) |
1184 | kgem->large_object_size = kgem->max_cpu_size; |
1184 | kgem->large_object_size = kgem->max_cpu_size; |
1185 | } else |
1185 | } else |
1186 | kgem->max_cpu_size = 0; |
1186 | kgem->max_cpu_size = 0; |
1187 | if (DBG_NO_CPU) |
1187 | if (DBG_NO_CPU) |
1188 | kgem->max_cpu_size = 0; |
1188 | kgem->max_cpu_size = 0; |
1189 | 1189 | ||
1190 | DBG(("%s: maximum object size=%d\n", |
1190 | DBG(("%s: maximum object size=%d\n", |
1191 | __FUNCTION__, kgem->max_object_size)); |
1191 | __FUNCTION__, kgem->max_object_size)); |
1192 | DBG(("%s: large object thresold=%d\n", |
1192 | DBG(("%s: large object thresold=%d\n", |
1193 | __FUNCTION__, kgem->large_object_size)); |
1193 | __FUNCTION__, kgem->large_object_size)); |
1194 | DBG(("%s: max object sizes (gpu=%d, cpu=%d, tile upload=%d, copy=%d)\n", |
1194 | DBG(("%s: max object sizes (gpu=%d, cpu=%d, tile upload=%d, copy=%d)\n", |
1195 | __FUNCTION__, |
1195 | __FUNCTION__, |
1196 | kgem->max_gpu_size, kgem->max_cpu_size, |
1196 | kgem->max_gpu_size, kgem->max_cpu_size, |
1197 | kgem->max_upload_tile_size, kgem->max_copy_tile_size)); |
1197 | kgem->max_upload_tile_size, kgem->max_copy_tile_size)); |
1198 | 1198 | ||
1199 | /* Convert the aperture thresholds to pages */ |
1199 | /* Convert the aperture thresholds to pages */ |
1200 | kgem->aperture_low /= PAGE_SIZE; |
1200 | kgem->aperture_low /= PAGE_SIZE; |
1201 | kgem->aperture_high /= PAGE_SIZE; |
1201 | kgem->aperture_high /= PAGE_SIZE; |
1202 | 1202 | ||
1203 | kgem->fence_max = gem_param(kgem, I915_PARAM_NUM_FENCES_AVAIL) - 2; |
1203 | kgem->fence_max = gem_param(kgem, I915_PARAM_NUM_FENCES_AVAIL) - 2; |
1204 | if ((int)kgem->fence_max < 0) |
1204 | if ((int)kgem->fence_max < 0) |
1205 | kgem->fence_max = 5; /* minimum safe value for all hw */ |
1205 | kgem->fence_max = 5; /* minimum safe value for all hw */ |
1206 | DBG(("%s: max fences=%d\n", __FUNCTION__, kgem->fence_max)); |
1206 | DBG(("%s: max fences=%d\n", __FUNCTION__, kgem->fence_max)); |
1207 | 1207 | ||
1208 | kgem->batch_flags_base = 0; |
1208 | kgem->batch_flags_base = 0; |
1209 | if (kgem->has_no_reloc) |
1209 | if (kgem->has_no_reloc) |
1210 | kgem->batch_flags_base |= LOCAL_I915_EXEC_NO_RELOC; |
1210 | kgem->batch_flags_base |= LOCAL_I915_EXEC_NO_RELOC; |
1211 | if (kgem->has_handle_lut) |
1211 | if (kgem->has_handle_lut) |
1212 | kgem->batch_flags_base |= LOCAL_I915_EXEC_HANDLE_LUT; |
1212 | kgem->batch_flags_base |= LOCAL_I915_EXEC_HANDLE_LUT; |
1213 | if (kgem->has_pinned_batches) |
1213 | if (kgem->has_pinned_batches) |
1214 | kgem->batch_flags_base |= LOCAL_I915_EXEC_IS_PINNED; |
1214 | kgem->batch_flags_base |= LOCAL_I915_EXEC_IS_PINNED; |
1215 | } |
1215 | } |
1216 | 1216 | ||
1217 | /* XXX hopefully a good approximation */ |
1217 | /* XXX hopefully a good approximation */ |
1218 | uint32_t kgem_get_unique_id(struct kgem *kgem) |
1218 | uint32_t kgem_get_unique_id(struct kgem *kgem) |
1219 | { |
1219 | { |
1220 | uint32_t id; |
1220 | uint32_t id; |
1221 | id = ++kgem->unique_id; |
1221 | id = ++kgem->unique_id; |
1222 | if (id == 0) |
1222 | if (id == 0) |
1223 | id = ++kgem->unique_id; |
1223 | id = ++kgem->unique_id; |
1224 | return id; |
1224 | return id; |
1225 | } |
1225 | } |
1226 | 1226 | ||
1227 | inline static uint32_t kgem_pitch_alignment(struct kgem *kgem, unsigned flags) |
1227 | inline static uint32_t kgem_pitch_alignment(struct kgem *kgem, unsigned flags) |
1228 | { |
1228 | { |
1229 | if (flags & CREATE_PRIME) |
1229 | if (flags & CREATE_PRIME) |
1230 | return 256; |
1230 | return 256; |
1231 | if (flags & CREATE_SCANOUT) |
1231 | if (flags & CREATE_SCANOUT) |
1232 | return 64; |
1232 | return 64; |
1233 | return kgem->min_alignment; |
1233 | return kgem->min_alignment; |
1234 | } |
1234 | } |
1235 | 1235 | ||
1236 | void kgem_get_tile_size(struct kgem *kgem, int tiling, |
1236 | void kgem_get_tile_size(struct kgem *kgem, int tiling, |
1237 | int *tile_width, int *tile_height, int *tile_size) |
1237 | int *tile_width, int *tile_height, int *tile_size) |
1238 | { |
1238 | { |
1239 | if (kgem->gen <= 030) { |
1239 | if (kgem->gen <= 030) { |
1240 | if (tiling) { |
1240 | if (tiling) { |
1241 | if (kgem->gen < 030) { |
1241 | if (kgem->gen < 030) { |
1242 | *tile_width = 128; |
1242 | *tile_width = 128; |
1243 | *tile_height = 16; |
1243 | *tile_height = 16; |
1244 | *tile_size = 2048; |
1244 | *tile_size = 2048; |
1245 | } else { |
1245 | } else { |
1246 | *tile_width = 512; |
1246 | *tile_width = 512; |
1247 | *tile_height = 8; |
1247 | *tile_height = 8; |
1248 | *tile_size = 4096; |
1248 | *tile_size = 4096; |
1249 | } |
1249 | } |
1250 | } else { |
1250 | } else { |
1251 | *tile_width = 1; |
1251 | *tile_width = 1; |
1252 | *tile_height = 1; |
1252 | *tile_height = 1; |
1253 | *tile_size = 1; |
1253 | *tile_size = 1; |
1254 | } |
1254 | } |
1255 | } else switch (tiling) { |
1255 | } else switch (tiling) { |
1256 | default: |
1256 | default: |
1257 | case I915_TILING_NONE: |
1257 | case I915_TILING_NONE: |
1258 | *tile_width = 1; |
1258 | *tile_width = 1; |
1259 | *tile_height = 1; |
1259 | *tile_height = 1; |
1260 | *tile_size = 1; |
1260 | *tile_size = 1; |
1261 | break; |
1261 | break; |
1262 | case I915_TILING_X: |
1262 | case I915_TILING_X: |
1263 | *tile_width = 512; |
1263 | *tile_width = 512; |
1264 | *tile_height = 8; |
1264 | *tile_height = 8; |
1265 | *tile_size = 4096; |
1265 | *tile_size = 4096; |
1266 | break; |
1266 | break; |
1267 | case I915_TILING_Y: |
1267 | case I915_TILING_Y: |
1268 | *tile_width = 128; |
1268 | *tile_width = 128; |
1269 | *tile_height = 32; |
1269 | *tile_height = 32; |
1270 | *tile_size = 4096; |
1270 | *tile_size = 4096; |
1271 | break; |
1271 | break; |
1272 | } |
1272 | } |
1273 | } |
1273 | } |
1274 | 1274 | ||
1275 | uint32_t kgem_surface_size(struct kgem *kgem, |
1275 | uint32_t kgem_surface_size(struct kgem *kgem, |
1276 | bool relaxed_fencing, |
1276 | bool relaxed_fencing, |
1277 | unsigned flags, |
1277 | unsigned flags, |
1278 | uint32_t width, |
1278 | uint32_t width, |
1279 | uint32_t height, |
1279 | uint32_t height, |
1280 | uint32_t bpp, |
1280 | uint32_t bpp, |
1281 | uint32_t tiling, |
1281 | uint32_t tiling, |
1282 | uint32_t *pitch) |
1282 | uint32_t *pitch) |
1283 | { |
1283 | { |
1284 | uint32_t tile_width, tile_height; |
1284 | uint32_t tile_width, tile_height; |
1285 | uint32_t size; |
1285 | uint32_t size; |
1286 | 1286 | ||
1287 | assert(width <= MAXSHORT); |
1287 | assert(width <= MAXSHORT); |
1288 | assert(height <= MAXSHORT); |
1288 | assert(height <= MAXSHORT); |
1289 | assert(bpp >= 8); |
1289 | assert(bpp >= 8); |
1290 | 1290 | ||
1291 | if (kgem->gen <= 030) { |
1291 | if (kgem->gen <= 030) { |
1292 | if (tiling) { |
1292 | if (tiling) { |
1293 | if (kgem->gen < 030) { |
1293 | if (kgem->gen < 030) { |
1294 | tile_width = 128; |
1294 | tile_width = 128; |
1295 | tile_height = 32; |
1295 | tile_height = 32; |
1296 | } else { |
1296 | } else { |
1297 | tile_width = 512; |
1297 | tile_width = 512; |
1298 | tile_height = 16; |
1298 | tile_height = 16; |
1299 | } |
1299 | } |
1300 | } else { |
1300 | } else { |
1301 | tile_width = 2 * bpp >> 3; |
1301 | tile_width = 2 * bpp >> 3; |
1302 | tile_width = ALIGN(tile_width, |
1302 | tile_width = ALIGN(tile_width, |
1303 | kgem_pitch_alignment(kgem, flags)); |
1303 | kgem_pitch_alignment(kgem, flags)); |
1304 | tile_height = 2; |
1304 | tile_height = 2; |
1305 | } |
1305 | } |
1306 | } else switch (tiling) { |
1306 | } else switch (tiling) { |
1307 | default: |
1307 | default: |
1308 | case I915_TILING_NONE: |
1308 | case I915_TILING_NONE: |
1309 | tile_width = 2 * bpp >> 3; |
1309 | tile_width = 2 * bpp >> 3; |
1310 | tile_width = ALIGN(tile_width, |
1310 | tile_width = ALIGN(tile_width, |
1311 | kgem_pitch_alignment(kgem, flags)); |
1311 | kgem_pitch_alignment(kgem, flags)); |
1312 | tile_height = 2; |
1312 | tile_height = 2; |
1313 | break; |
1313 | break; |
1314 | 1314 | ||
1315 | /* XXX align to an even tile row */ |
1315 | /* XXX align to an even tile row */ |
1316 | case I915_TILING_X: |
1316 | case I915_TILING_X: |
1317 | tile_width = 512; |
1317 | tile_width = 512; |
1318 | tile_height = 16; |
1318 | tile_height = 16; |
1319 | break; |
1319 | break; |
1320 | case I915_TILING_Y: |
1320 | case I915_TILING_Y: |
1321 | tile_width = 128; |
1321 | tile_width = 128; |
1322 | tile_height = 64; |
1322 | tile_height = 64; |
1323 | break; |
1323 | break; |
1324 | } |
1324 | } |
1325 | 1325 | ||
1326 | *pitch = ALIGN(width * bpp / 8, tile_width); |
1326 | *pitch = ALIGN(width * bpp / 8, tile_width); |
1327 | height = ALIGN(height, tile_height); |
1327 | height = ALIGN(height, tile_height); |
1328 | if (kgem->gen >= 040) |
1328 | if (kgem->gen >= 040) |
1329 | return PAGE_ALIGN(*pitch * height); |
1329 | return PAGE_ALIGN(*pitch * height); |
1330 | 1330 | ||
1331 | /* If it is too wide for the blitter, don't even bother. */ |
1331 | /* If it is too wide for the blitter, don't even bother. */ |
1332 | if (tiling != I915_TILING_NONE) { |
1332 | if (tiling != I915_TILING_NONE) { |
1333 | if (*pitch > 8192) |
1333 | if (*pitch > 8192) |
1334 | return 0; |
1334 | return 0; |
1335 | 1335 | ||
1336 | for (size = tile_width; size < *pitch; size <<= 1) |
1336 | for (size = tile_width; size < *pitch; size <<= 1) |
1337 | ; |
1337 | ; |
1338 | *pitch = size; |
1338 | *pitch = size; |
1339 | } else { |
1339 | } else { |
1340 | if (*pitch >= 32768) |
1340 | if (*pitch >= 32768) |
1341 | return 0; |
1341 | return 0; |
1342 | } |
1342 | } |
1343 | 1343 | ||
1344 | size = *pitch * height; |
1344 | size = *pitch * height; |
1345 | if (relaxed_fencing || tiling == I915_TILING_NONE) |
1345 | if (relaxed_fencing || tiling == I915_TILING_NONE) |
1346 | return PAGE_ALIGN(size); |
1346 | return PAGE_ALIGN(size); |
1347 | 1347 | ||
1348 | /* We need to allocate a pot fence region for a tiled buffer. */ |
1348 | /* We need to allocate a pot fence region for a tiled buffer. */ |
1349 | if (kgem->gen < 030) |
1349 | if (kgem->gen < 030) |
1350 | tile_width = 512 * 1024; |
1350 | tile_width = 512 * 1024; |
1351 | else |
1351 | else |
1352 | tile_width = 1024 * 1024; |
1352 | tile_width = 1024 * 1024; |
1353 | while (tile_width < size) |
1353 | while (tile_width < size) |
1354 | tile_width *= 2; |
1354 | tile_width *= 2; |
1355 | return tile_width; |
1355 | return tile_width; |
1356 | } |
1356 | } |
1357 | 1357 | ||
1358 | static uint32_t kgem_aligned_height(struct kgem *kgem, |
1358 | static uint32_t kgem_aligned_height(struct kgem *kgem, |
1359 | uint32_t height, uint32_t tiling) |
1359 | uint32_t height, uint32_t tiling) |
1360 | { |
1360 | { |
1361 | uint32_t tile_height; |
1361 | uint32_t tile_height; |
1362 | 1362 | ||
1363 | if (kgem->gen <= 030) { |
1363 | if (kgem->gen <= 030) { |
1364 | tile_height = tiling ? kgem->gen < 030 ? 32 : 16 : 1; |
1364 | tile_height = tiling ? kgem->gen < 030 ? 32 : 16 : 1; |
1365 | } else switch (tiling) { |
1365 | } else switch (tiling) { |
1366 | /* XXX align to an even tile row */ |
1366 | /* XXX align to an even tile row */ |
1367 | default: |
1367 | default: |
1368 | case I915_TILING_NONE: |
1368 | case I915_TILING_NONE: |
1369 | tile_height = 1; |
1369 | tile_height = 1; |
1370 | break; |
1370 | break; |
1371 | case I915_TILING_X: |
1371 | case I915_TILING_X: |
1372 | tile_height = 16; |
1372 | tile_height = 16; |
1373 | break; |
1373 | break; |
1374 | case I915_TILING_Y: |
1374 | case I915_TILING_Y: |
1375 | tile_height = 64; |
1375 | tile_height = 64; |
1376 | break; |
1376 | break; |
1377 | } |
1377 | } |
1378 | 1378 | ||
1379 | return ALIGN(height, tile_height); |
1379 | return ALIGN(height, tile_height); |
1380 | } |
1380 | } |
1381 | 1381 | ||
1382 | static struct drm_i915_gem_exec_object2 * |
1382 | static struct drm_i915_gem_exec_object2 * |
1383 | kgem_add_handle(struct kgem *kgem, struct kgem_bo *bo) |
1383 | kgem_add_handle(struct kgem *kgem, struct kgem_bo *bo) |
1384 | { |
1384 | { |
1385 | struct drm_i915_gem_exec_object2 *exec; |
1385 | struct drm_i915_gem_exec_object2 *exec; |
1386 | 1386 | ||
1387 | DBG(("%s: handle=%d, index=%d\n", |
1387 | DBG(("%s: handle=%d, index=%d\n", |
1388 | __FUNCTION__, bo->handle, kgem->nexec)); |
1388 | __FUNCTION__, bo->handle, kgem->nexec)); |
1389 | 1389 | ||
1390 | assert(kgem->nexec < ARRAY_SIZE(kgem->exec)); |
1390 | assert(kgem->nexec < ARRAY_SIZE(kgem->exec)); |
1391 | bo->target_handle = kgem->has_handle_lut ? kgem->nexec : bo->handle; |
1391 | bo->target_handle = kgem->has_handle_lut ? kgem->nexec : bo->handle; |
1392 | exec = memset(&kgem->exec[kgem->nexec++], 0, sizeof(*exec)); |
1392 | exec = memset(&kgem->exec[kgem->nexec++], 0, sizeof(*exec)); |
1393 | exec->handle = bo->handle; |
1393 | exec->handle = bo->handle; |
1394 | exec->offset = bo->presumed_offset; |
1394 | exec->offset = bo->presumed_offset; |
1395 | 1395 | ||
1396 | kgem->aperture += num_pages(bo); |
1396 | kgem->aperture += num_pages(bo); |
1397 | 1397 | ||
1398 | return exec; |
1398 | return exec; |
1399 | } |
1399 | } |
1400 | 1400 | ||
1401 | static void kgem_add_bo(struct kgem *kgem, struct kgem_bo *bo) |
1401 | static void kgem_add_bo(struct kgem *kgem, struct kgem_bo *bo) |
1402 | { |
1402 | { |
1403 | bo->exec = kgem_add_handle(kgem, bo); |
1403 | bo->exec = kgem_add_handle(kgem, bo); |
1404 | bo->rq = MAKE_REQUEST(kgem->next_request, kgem->ring); |
1404 | bo->rq = MAKE_REQUEST(kgem->next_request, kgem->ring); |
1405 | 1405 | ||
1406 | list_move_tail(&bo->request, &kgem->next_request->buffers); |
1406 | list_move_tail(&bo->request, &kgem->next_request->buffers); |
1407 | 1407 | ||
1408 | /* XXX is it worth working around gcc here? */ |
1408 | /* XXX is it worth working around gcc here? */ |
1409 | kgem->flush |= bo->flush; |
1409 | kgem->flush |= bo->flush; |
1410 | } |
1410 | } |
1411 | 1411 | ||
1412 | static uint32_t kgem_end_batch(struct kgem *kgem) |
1412 | static uint32_t kgem_end_batch(struct kgem *kgem) |
1413 | { |
1413 | { |
1414 | kgem->batch[kgem->nbatch++] = MI_BATCH_BUFFER_END; |
1414 | kgem->batch[kgem->nbatch++] = MI_BATCH_BUFFER_END; |
1415 | if (kgem->nbatch & 1) |
1415 | if (kgem->nbatch & 1) |
1416 | kgem->batch[kgem->nbatch++] = MI_NOOP; |
1416 | kgem->batch[kgem->nbatch++] = MI_NOOP; |
1417 | 1417 | ||
1418 | return kgem->nbatch; |
1418 | return kgem->nbatch; |
1419 | } |
1419 | } |
1420 | 1420 | ||
1421 | static void kgem_fixup_self_relocs(struct kgem *kgem, struct kgem_bo *bo) |
1421 | static void kgem_fixup_self_relocs(struct kgem *kgem, struct kgem_bo *bo) |
1422 | { |
1422 | { |
1423 | int n; |
1423 | int n; |
1424 | 1424 | ||
1425 | assert(kgem->nreloc__self <= 256); |
1425 | assert(kgem->nreloc__self <= 256); |
1426 | if (kgem->nreloc__self == 0) |
1426 | if (kgem->nreloc__self == 0) |
1427 | return; |
1427 | return; |
1428 | 1428 | ||
1429 | for (n = 0; n < kgem->nreloc__self; n++) { |
1429 | for (n = 0; n < kgem->nreloc__self; n++) { |
1430 | int i = kgem->reloc__self[n]; |
1430 | int i = kgem->reloc__self[n]; |
1431 | assert(kgem->reloc[i].target_handle == ~0U); |
1431 | assert(kgem->reloc[i].target_handle == ~0U); |
1432 | kgem->reloc[i].target_handle = bo->target_handle; |
1432 | kgem->reloc[i].target_handle = bo->target_handle; |
1433 | kgem->reloc[i].presumed_offset = bo->presumed_offset; |
1433 | kgem->reloc[i].presumed_offset = bo->presumed_offset; |
1434 | kgem->batch[kgem->reloc[i].offset/sizeof(kgem->batch[0])] = |
1434 | kgem->batch[kgem->reloc[i].offset/sizeof(kgem->batch[0])] = |
1435 | kgem->reloc[i].delta + bo->presumed_offset; |
1435 | kgem->reloc[i].delta + bo->presumed_offset; |
1436 | } |
1436 | } |
1437 | 1437 | ||
1438 | if (n == 256) { |
1438 | if (n == 256) { |
1439 | for (n = kgem->reloc__self[255]; n < kgem->nreloc; n++) { |
1439 | for (n = kgem->reloc__self[255]; n < kgem->nreloc; n++) { |
1440 | if (kgem->reloc[n].target_handle == ~0U) { |
1440 | if (kgem->reloc[n].target_handle == ~0U) { |
1441 | kgem->reloc[n].target_handle = bo->target_handle; |
1441 | kgem->reloc[n].target_handle = bo->target_handle; |
1442 | kgem->reloc[n].presumed_offset = bo->presumed_offset; |
1442 | kgem->reloc[n].presumed_offset = bo->presumed_offset; |
1443 | kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] = |
1443 | kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] = |
1444 | kgem->reloc[n].delta + bo->presumed_offset; |
1444 | kgem->reloc[n].delta + bo->presumed_offset; |
1445 | } |
1445 | } |
1446 | } |
1446 | } |
1447 | 1447 | ||
1448 | } |
1448 | } |
1449 | 1449 | ||
1450 | } |
1450 | } |
1451 | 1451 | ||
1452 | static void kgem_bo_binding_free(struct kgem *kgem, struct kgem_bo *bo) |
1452 | static void kgem_bo_binding_free(struct kgem *kgem, struct kgem_bo *bo) |
1453 | { |
1453 | { |
1454 | struct kgem_bo_binding *b; |
1454 | struct kgem_bo_binding *b; |
1455 | 1455 | ||
1456 | b = bo->binding.next; |
1456 | b = bo->binding.next; |
1457 | while (b) { |
1457 | while (b) { |
1458 | struct kgem_bo_binding *next = b->next; |
1458 | struct kgem_bo_binding *next = b->next; |
1459 | free (b); |
1459 | free (b); |
1460 | b = next; |
1460 | b = next; |
1461 | } |
1461 | } |
1462 | } |
1462 | } |
1463 | 1463 | ||
1464 | static void kgem_bo_release_map(struct kgem *kgem, struct kgem_bo *bo) |
1464 | static void kgem_bo_release_map(struct kgem *kgem, struct kgem_bo *bo) |
1465 | { |
1465 | { |
1466 | int type = IS_CPU_MAP(bo->map); |
1466 | int type = IS_CPU_MAP(bo->map); |
1467 | 1467 | ||
1468 | assert(!IS_USER_MAP(bo->map)); |
1468 | assert(!IS_USER_MAP(bo->map)); |
1469 | 1469 | ||
1470 | DBG(("%s: releasing %s vma for handle=%d, count=%d\n", |
1470 | DBG(("%s: releasing %s vma for handle=%d, count=%d\n", |
1471 | __FUNCTION__, type ? "CPU" : "GTT", |
1471 | __FUNCTION__, type ? "CPU" : "GTT", |
1472 | bo->handle, kgem->vma[type].count)); |
1472 | bo->handle, kgem->vma[type].count)); |
1473 | 1473 | ||
1474 | VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo))); |
1474 | VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo))); |
1475 | user_free(MAP(bo->map)); |
1475 | user_free(MAP(bo->map)); |
1476 | bo->map = NULL; |
1476 | bo->map = NULL; |
1477 | 1477 | ||
1478 | if (!list_is_empty(&bo->vma)) { |
1478 | if (!list_is_empty(&bo->vma)) { |
1479 | list_del(&bo->vma); |
1479 | list_del(&bo->vma); |
1480 | kgem->vma[type].count--; |
1480 | kgem->vma[type].count--; |
1481 | } |
1481 | } |
1482 | } |
1482 | } |
1483 | 1483 | ||
1484 | static void kgem_bo_free(struct kgem *kgem, struct kgem_bo *bo) |
1484 | static void kgem_bo_free(struct kgem *kgem, struct kgem_bo *bo) |
1485 | { |
1485 | { |
1486 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
1486 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
1487 | assert(bo->refcnt == 0); |
1487 | assert(bo->refcnt == 0); |
1488 | assert(bo->proxy == NULL); |
1488 | assert(bo->proxy == NULL); |
1489 | assert(bo->exec == NULL); |
1489 | assert(bo->exec == NULL); |
1490 | assert(!bo->snoop || bo->rq == NULL); |
1490 | assert(!bo->snoop || bo->rq == NULL); |
1491 | 1491 | ||
1492 | #ifdef DEBUG_MEMORY |
1492 | #ifdef DEBUG_MEMORY |
1493 | kgem->debug_memory.bo_allocs--; |
1493 | kgem->debug_memory.bo_allocs--; |
1494 | kgem->debug_memory.bo_bytes -= bytes(bo); |
1494 | kgem->debug_memory.bo_bytes -= bytes(bo); |
1495 | #endif |
1495 | #endif |
1496 | 1496 | ||
1497 | kgem_bo_binding_free(kgem, bo); |
1497 | kgem_bo_binding_free(kgem, bo); |
1498 | 1498 | ||
1499 | if (IS_USER_MAP(bo->map)) { |
1499 | if (IS_USER_MAP(bo->map)) { |
1500 | assert(bo->rq == NULL); |
1500 | assert(bo->rq == NULL); |
1501 | assert(!__kgem_busy(kgem, bo->handle)); |
1501 | assert(!__kgem_busy(kgem, bo->handle)); |
1502 | assert(MAP(bo->map) != bo || bo->io || bo->flush); |
1502 | assert(MAP(bo->map) != bo || bo->io || bo->flush); |
1503 | if (!(bo->io || bo->flush)) { |
1503 | if (!(bo->io || bo->flush)) { |
1504 | DBG(("%s: freeing snooped base\n", __FUNCTION__)); |
1504 | DBG(("%s: freeing snooped base\n", __FUNCTION__)); |
1505 | assert(bo != MAP(bo->map)); |
1505 | assert(bo != MAP(bo->map)); |
1506 | free(MAP(bo->map)); |
1506 | free(MAP(bo->map)); |
1507 | } |
1507 | } |
1508 | bo->map = NULL; |
1508 | bo->map = NULL; |
1509 | } |
1509 | } |
1510 | if (bo->map) |
1510 | if (bo->map) |
1511 | kgem_bo_release_map(kgem, bo); |
1511 | kgem_bo_release_map(kgem, bo); |
1512 | assert(list_is_empty(&bo->vma)); |
1512 | assert(list_is_empty(&bo->vma)); |
1513 | assert(bo->map == NULL); |
1513 | assert(bo->map == NULL); |
1514 | 1514 | ||
1515 | _list_del(&bo->list); |
1515 | _list_del(&bo->list); |
1516 | _list_del(&bo->request); |
1516 | _list_del(&bo->request); |
1517 | gem_close(kgem->fd, bo->handle); |
1517 | gem_close(kgem->fd, bo->handle); |
1518 | 1518 | ||
1519 | if (!bo->io) { |
1519 | if (!bo->io) { |
1520 | *(struct kgem_bo **)bo = __kgem_freed_bo; |
1520 | *(struct kgem_bo **)bo = __kgem_freed_bo; |
1521 | __kgem_freed_bo = bo; |
1521 | __kgem_freed_bo = bo; |
1522 | } else |
1522 | } else |
1523 | free(bo); |
1523 | free(bo); |
1524 | } |
1524 | } |
1525 | 1525 | ||
1526 | inline static void kgem_bo_move_to_inactive(struct kgem *kgem, |
1526 | inline static void kgem_bo_move_to_inactive(struct kgem *kgem, |
1527 | struct kgem_bo *bo) |
1527 | struct kgem_bo *bo) |
1528 | { |
1528 | { |
1529 | DBG(("%s: moving handle=%d to inactive\n", __FUNCTION__, bo->handle)); |
1529 | DBG(("%s: moving handle=%d to inactive\n", __FUNCTION__, bo->handle)); |
1530 | 1530 | ||
1531 | assert(bo->refcnt == 0); |
1531 | assert(bo->refcnt == 0); |
1532 | assert(bo->reusable); |
1532 | assert(bo->reusable); |
1533 | assert(bo->rq == NULL); |
1533 | assert(bo->rq == NULL); |
1534 | assert(bo->exec == NULL); |
1534 | assert(bo->exec == NULL); |
1535 | assert(bo->domain != DOMAIN_GPU); |
1535 | assert(bo->domain != DOMAIN_GPU); |
1536 | assert(!bo->proxy); |
1536 | assert(!bo->proxy); |
1537 | assert(!bo->io); |
1537 | assert(!bo->io); |
1538 | assert(!bo->scanout); |
1538 | assert(!bo->scanout); |
1539 | assert(!bo->snoop); |
1539 | assert(!bo->snoop); |
1540 | assert(!bo->flush); |
1540 | assert(!bo->flush); |
1541 | assert(!bo->needs_flush); |
1541 | assert(!bo->needs_flush); |
1542 | assert(list_is_empty(&bo->vma)); |
1542 | assert(list_is_empty(&bo->vma)); |
1543 | assert_tiling(kgem, bo); |
1543 | assert_tiling(kgem, bo); |
1544 | ASSERT_IDLE(kgem, bo->handle); |
1544 | ASSERT_IDLE(kgem, bo->handle); |
1545 | 1545 | ||
1546 | kgem->need_expire = true; |
1546 | kgem->need_expire = true; |
1547 | 1547 | ||
1548 | if (bucket(bo) >= NUM_CACHE_BUCKETS) { |
1548 | if (bucket(bo) >= NUM_CACHE_BUCKETS) { |
1549 | list_move(&bo->list, &kgem->large_inactive); |
1549 | list_move(&bo->list, &kgem->large_inactive); |
1550 | return; |
1550 | return; |
1551 | } |
1551 | } |
1552 | 1552 | ||
1553 | assert(bo->flush == false); |
1553 | assert(bo->flush == false); |
1554 | list_move(&bo->list, &kgem->inactive[bucket(bo)]); |
1554 | list_move(&bo->list, &kgem->inactive[bucket(bo)]); |
1555 | if (bo->map) { |
1555 | if (bo->map) { |
1556 | int type = IS_CPU_MAP(bo->map); |
1556 | int type = IS_CPU_MAP(bo->map); |
1557 | if (bucket(bo) >= NUM_CACHE_BUCKETS || |
1557 | if (bucket(bo) >= NUM_CACHE_BUCKETS || |
1558 | (!type && !__kgem_bo_is_mappable(kgem, bo))) { |
1558 | (!type && !__kgem_bo_is_mappable(kgem, bo))) { |
1559 | // munmap(MAP(bo->map), bytes(bo)); |
1559 | // munmap(MAP(bo->map), bytes(bo)); |
1560 | bo->map = NULL; |
1560 | bo->map = NULL; |
1561 | } |
1561 | } |
1562 | if (bo->map) { |
1562 | if (bo->map) { |
1563 | list_add(&bo->vma, &kgem->vma[type].inactive[bucket(bo)]); |
1563 | list_add(&bo->vma, &kgem->vma[type].inactive[bucket(bo)]); |
1564 | kgem->vma[type].count++; |
1564 | kgem->vma[type].count++; |
1565 | } |
1565 | } |
1566 | } |
1566 | } |
1567 | } |
1567 | } |
1568 | 1568 | ||
1569 | static struct kgem_bo *kgem_bo_replace_io(struct kgem_bo *bo) |
1569 | static struct kgem_bo *kgem_bo_replace_io(struct kgem_bo *bo) |
1570 | { |
1570 | { |
1571 | struct kgem_bo *base; |
1571 | struct kgem_bo *base; |
1572 | 1572 | ||
1573 | if (!bo->io) |
1573 | if (!bo->io) |
1574 | return bo; |
1574 | return bo; |
1575 | 1575 | ||
1576 | assert(!bo->snoop); |
1576 | assert(!bo->snoop); |
1577 | base = malloc(sizeof(*base)); |
1577 | base = malloc(sizeof(*base)); |
1578 | if (base) { |
1578 | if (base) { |
1579 | DBG(("%s: transferring io handle=%d to bo\n", |
1579 | DBG(("%s: transferring io handle=%d to bo\n", |
1580 | __FUNCTION__, bo->handle)); |
1580 | __FUNCTION__, bo->handle)); |
1581 | /* transfer the handle to a minimum bo */ |
1581 | /* transfer the handle to a minimum bo */ |
1582 | memcpy(base, bo, sizeof(*base)); |
1582 | memcpy(base, bo, sizeof(*base)); |
1583 | base->io = false; |
1583 | base->io = false; |
1584 | list_init(&base->list); |
1584 | list_init(&base->list); |
1585 | list_replace(&bo->request, &base->request); |
1585 | list_replace(&bo->request, &base->request); |
1586 | list_replace(&bo->vma, &base->vma); |
1586 | list_replace(&bo->vma, &base->vma); |
1587 | free(bo); |
1587 | free(bo); |
1588 | bo = base; |
1588 | bo = base; |
1589 | } else |
1589 | } else |
1590 | bo->reusable = false; |
1590 | bo->reusable = false; |
1591 | 1591 | ||
1592 | return bo; |
1592 | return bo; |
1593 | } |
1593 | } |
1594 | 1594 | ||
1595 | inline static void kgem_bo_remove_from_inactive(struct kgem *kgem, |
1595 | inline static void kgem_bo_remove_from_inactive(struct kgem *kgem, |
1596 | struct kgem_bo *bo) |
1596 | struct kgem_bo *bo) |
1597 | { |
1597 | { |
1598 | DBG(("%s: removing handle=%d from inactive\n", __FUNCTION__, bo->handle)); |
1598 | DBG(("%s: removing handle=%d from inactive\n", __FUNCTION__, bo->handle)); |
1599 | 1599 | ||
1600 | list_del(&bo->list); |
1600 | list_del(&bo->list); |
1601 | assert(bo->rq == NULL); |
1601 | assert(bo->rq == NULL); |
1602 | assert(bo->exec == NULL); |
1602 | assert(bo->exec == NULL); |
1603 | if (bo->map) { |
1603 | if (bo->map) { |
1604 | assert(!list_is_empty(&bo->vma)); |
1604 | assert(!list_is_empty(&bo->vma)); |
1605 | list_del(&bo->vma); |
1605 | list_del(&bo->vma); |
1606 | kgem->vma[IS_CPU_MAP(bo->map)].count--; |
1606 | kgem->vma[IS_CPU_MAP(bo->map)].count--; |
1607 | } |
1607 | } |
1608 | } |
1608 | } |
1609 | 1609 | ||
1610 | inline static void kgem_bo_remove_from_active(struct kgem *kgem, |
1610 | inline static void kgem_bo_remove_from_active(struct kgem *kgem, |
1611 | struct kgem_bo *bo) |
1611 | struct kgem_bo *bo) |
1612 | { |
1612 | { |
1613 | DBG(("%s: removing handle=%d from active\n", __FUNCTION__, bo->handle)); |
1613 | DBG(("%s: removing handle=%d from active\n", __FUNCTION__, bo->handle)); |
1614 | 1614 | ||
1615 | list_del(&bo->list); |
1615 | list_del(&bo->list); |
1616 | assert(bo->rq != NULL); |
1616 | assert(bo->rq != NULL); |
1617 | if (bo->rq == (void *)kgem) |
1617 | if (bo->rq == (void *)kgem) |
1618 | list_del(&bo->request); |
1618 | list_del(&bo->request); |
1619 | assert(list_is_empty(&bo->vma)); |
1619 | assert(list_is_empty(&bo->vma)); |
1620 | } |
1620 | } |
1621 | 1621 | ||
1622 | static void _kgem_bo_delete_buffer(struct kgem *kgem, struct kgem_bo *bo) |
1622 | static void _kgem_bo_delete_buffer(struct kgem *kgem, struct kgem_bo *bo) |
1623 | { |
1623 | { |
1624 | struct kgem_buffer *io = (struct kgem_buffer *)bo->proxy; |
1624 | struct kgem_buffer *io = (struct kgem_buffer *)bo->proxy; |
1625 | 1625 | ||
1626 | DBG(("%s: size=%d, offset=%d, parent used=%d\n", |
1626 | DBG(("%s: size=%d, offset=%d, parent used=%d\n", |
1627 | __FUNCTION__, bo->size.bytes, bo->delta, io->used)); |
1627 | __FUNCTION__, bo->size.bytes, bo->delta, io->used)); |
1628 | 1628 | ||
1629 | if (ALIGN(bo->delta + bo->size.bytes, UPLOAD_ALIGNMENT) == io->used) |
1629 | if (ALIGN(bo->delta + bo->size.bytes, UPLOAD_ALIGNMENT) == io->used) |
1630 | io->used = bo->delta; |
1630 | io->used = bo->delta; |
1631 | } |
1631 | } |
1632 | 1632 | ||
1633 | static void kgem_bo_move_to_scanout(struct kgem *kgem, struct kgem_bo *bo) |
1633 | static void kgem_bo_move_to_scanout(struct kgem *kgem, struct kgem_bo *bo) |
1634 | { |
1634 | { |
1635 | assert(bo->refcnt == 0); |
1635 | assert(bo->refcnt == 0); |
1636 | assert(bo->scanout); |
1636 | assert(bo->scanout); |
1637 | assert(bo->delta); |
1637 | assert(bo->delta); |
1638 | assert(!bo->flush); |
1638 | assert(!bo->flush); |
1639 | assert(!bo->snoop); |
1639 | assert(!bo->snoop); |
1640 | assert(!bo->io); |
1640 | assert(!bo->io); |
1641 | 1641 | ||
1642 | if (bo->purged) { |
1642 | if (bo->purged) { |
1643 | DBG(("%s: discarding purged scanout - external name?\n", |
1643 | DBG(("%s: discarding purged scanout - external name?\n", |
1644 | __FUNCTION__)); |
1644 | __FUNCTION__)); |
1645 | kgem_bo_free(kgem, bo); |
1645 | kgem_bo_free(kgem, bo); |
1646 | return; |
1646 | return; |
1647 | } |
1647 | } |
1648 | 1648 | ||
1649 | DBG(("%s: moving %d [fb %d] to scanout cache, active? %d\n", |
1649 | DBG(("%s: moving %d [fb %d] to scanout cache, active? %d\n", |
1650 | __FUNCTION__, bo->handle, bo->delta, bo->rq != NULL)); |
1650 | __FUNCTION__, bo->handle, bo->delta, bo->rq != NULL)); |
1651 | if (bo->rq) |
1651 | if (bo->rq) |
1652 | list_move_tail(&bo->list, &kgem->scanout); |
1652 | list_move_tail(&bo->list, &kgem->scanout); |
1653 | else |
1653 | else |
1654 | list_move(&bo->list, &kgem->scanout); |
1654 | list_move(&bo->list, &kgem->scanout); |
1655 | } |
1655 | } |
1656 | 1656 | ||
1657 | static void kgem_bo_move_to_snoop(struct kgem *kgem, struct kgem_bo *bo) |
1657 | static void kgem_bo_move_to_snoop(struct kgem *kgem, struct kgem_bo *bo) |
1658 | { |
1658 | { |
1659 | assert(bo->reusable); |
1659 | assert(bo->reusable); |
1660 | assert(!bo->flush); |
1660 | assert(!bo->flush); |
1661 | assert(!bo->needs_flush); |
1661 | assert(!bo->needs_flush); |
1662 | assert(bo->refcnt == 0); |
1662 | assert(bo->refcnt == 0); |
1663 | assert(bo->exec == NULL); |
1663 | assert(bo->exec == NULL); |
1664 | 1664 | ||
1665 | if (num_pages(bo) > kgem->max_cpu_size >> 13) { |
1665 | if (num_pages(bo) > kgem->max_cpu_size >> 13) { |
1666 | DBG(("%s handle=%d discarding large CPU buffer (%d >%d pages)\n", |
1666 | DBG(("%s handle=%d discarding large CPU buffer (%d >%d pages)\n", |
1667 | __FUNCTION__, bo->handle, num_pages(bo), kgem->max_cpu_size >> 13)); |
1667 | __FUNCTION__, bo->handle, num_pages(bo), kgem->max_cpu_size >> 13)); |
1668 | kgem_bo_free(kgem, bo); |
1668 | kgem_bo_free(kgem, bo); |
1669 | return; |
1669 | return; |
1670 | } |
1670 | } |
1671 | 1671 | ||
1672 | assert(bo->tiling == I915_TILING_NONE); |
1672 | assert(bo->tiling == I915_TILING_NONE); |
1673 | assert(bo->rq == NULL); |
1673 | assert(bo->rq == NULL); |
1674 | 1674 | ||
1675 | DBG(("%s: moving %d to snoop cachee\n", __FUNCTION__, bo->handle)); |
1675 | DBG(("%s: moving %d to snoop cachee\n", __FUNCTION__, bo->handle)); |
1676 | list_add(&bo->list, &kgem->snoop); |
1676 | list_add(&bo->list, &kgem->snoop); |
1677 | } |
1677 | } |
1678 | 1678 | ||
1679 | static struct kgem_bo * |
1679 | static struct kgem_bo * |
1680 | search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags) |
1680 | search_snoop_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags) |
1681 | { |
1681 | { |
1682 | struct kgem_bo *bo, *first = NULL; |
1682 | struct kgem_bo *bo, *first = NULL; |
1683 | 1683 | ||
1684 | DBG(("%s: num_pages=%d, flags=%x\n", __FUNCTION__, num_pages, flags)); |
1684 | DBG(("%s: num_pages=%d, flags=%x\n", __FUNCTION__, num_pages, flags)); |
1685 | 1685 | ||
1686 | if ((kgem->has_caching | kgem->has_userptr) == 0) |
1686 | if ((kgem->has_caching | kgem->has_userptr) == 0) |
1687 | return NULL; |
1687 | return NULL; |
1688 | 1688 | ||
1689 | if (list_is_empty(&kgem->snoop)) { |
1689 | if (list_is_empty(&kgem->snoop)) { |
1690 | DBG(("%s: inactive and cache empty\n", __FUNCTION__)); |
1690 | DBG(("%s: inactive and cache empty\n", __FUNCTION__)); |
1691 | if (!__kgem_throttle_retire(kgem, flags)) { |
1691 | if (!__kgem_throttle_retire(kgem, flags)) { |
1692 | DBG(("%s: nothing retired\n", __FUNCTION__)); |
1692 | DBG(("%s: nothing retired\n", __FUNCTION__)); |
1693 | return NULL; |
1693 | return NULL; |
1694 | } |
1694 | } |
1695 | } |
1695 | } |
1696 | 1696 | ||
1697 | list_for_each_entry(bo, &kgem->snoop, list) { |
1697 | list_for_each_entry(bo, &kgem->snoop, list) { |
1698 | assert(bo->refcnt == 0); |
1698 | assert(bo->refcnt == 0); |
1699 | assert(bo->snoop); |
1699 | assert(bo->snoop); |
1700 | assert(!bo->scanout); |
1700 | assert(!bo->scanout); |
1701 | assert(!bo->purged); |
1701 | assert(!bo->purged); |
1702 | assert(bo->proxy == NULL); |
1702 | assert(bo->proxy == NULL); |
1703 | assert(bo->tiling == I915_TILING_NONE); |
1703 | assert(bo->tiling == I915_TILING_NONE); |
1704 | assert(bo->rq == NULL); |
1704 | assert(bo->rq == NULL); |
1705 | assert(bo->exec == NULL); |
1705 | assert(bo->exec == NULL); |
1706 | 1706 | ||
1707 | if (num_pages > num_pages(bo)) |
1707 | if (num_pages > num_pages(bo)) |
1708 | continue; |
1708 | continue; |
1709 | 1709 | ||
1710 | if (num_pages(bo) > 2*num_pages) { |
1710 | if (num_pages(bo) > 2*num_pages) { |
1711 | if (first == NULL) |
1711 | if (first == NULL) |
1712 | first = bo; |
1712 | first = bo; |
1713 | continue; |
1713 | continue; |
1714 | } |
1714 | } |
1715 | 1715 | ||
1716 | list_del(&bo->list); |
1716 | list_del(&bo->list); |
1717 | bo->pitch = 0; |
1717 | bo->pitch = 0; |
1718 | bo->delta = 0; |
1718 | bo->delta = 0; |
1719 | 1719 | ||
1720 | DBG((" %s: found handle=%d (num_pages=%d) in snoop cache\n", |
1720 | DBG((" %s: found handle=%d (num_pages=%d) in snoop cache\n", |
1721 | __FUNCTION__, bo->handle, num_pages(bo))); |
1721 | __FUNCTION__, bo->handle, num_pages(bo))); |
1722 | return bo; |
1722 | return bo; |
1723 | } |
1723 | } |
1724 | 1724 | ||
1725 | if (first) { |
1725 | if (first) { |
1726 | list_del(&first->list); |
1726 | list_del(&first->list); |
1727 | first->pitch = 0; |
1727 | first->pitch = 0; |
1728 | first->delta = 0; |
1728 | first->delta = 0; |
1729 | 1729 | ||
1730 | DBG((" %s: found handle=%d (num_pages=%d) in snoop cache\n", |
1730 | DBG((" %s: found handle=%d (num_pages=%d) in snoop cache\n", |
1731 | __FUNCTION__, first->handle, num_pages(first))); |
1731 | __FUNCTION__, first->handle, num_pages(first))); |
1732 | return first; |
1732 | return first; |
1733 | } |
1733 | } |
1734 | 1734 | ||
1735 | return NULL; |
1735 | return NULL; |
1736 | } |
1736 | } |
1737 | 1737 | ||
1738 | void kgem_bo_undo(struct kgem *kgem, struct kgem_bo *bo) |
1738 | void kgem_bo_undo(struct kgem *kgem, struct kgem_bo *bo) |
1739 | { |
1739 | { |
1740 | if (kgem->nexec != 1 || bo->exec == NULL) |
1740 | if (kgem->nexec != 1 || bo->exec == NULL) |
1741 | return; |
1741 | return; |
1742 | 1742 | ||
1743 | DBG(("%s: only handle in batch, discarding last operations for handle=%d\n", |
1743 | DBG(("%s: only handle in batch, discarding last operations for handle=%d\n", |
1744 | __FUNCTION__, bo->handle)); |
1744 | __FUNCTION__, bo->handle)); |
1745 | 1745 | ||
1746 | assert(bo->exec == &kgem->exec[0]); |
1746 | assert(bo->exec == &kgem->exec[0]); |
1747 | assert(kgem->exec[0].handle == bo->handle); |
1747 | assert(kgem->exec[0].handle == bo->handle); |
1748 | assert(RQ(bo->rq) == kgem->next_request); |
1748 | assert(RQ(bo->rq) == kgem->next_request); |
1749 | 1749 | ||
1750 | bo->refcnt++; |
1750 | bo->refcnt++; |
1751 | kgem_reset(kgem); |
1751 | kgem_reset(kgem); |
1752 | bo->refcnt--; |
1752 | bo->refcnt--; |
1753 | } |
1753 | } |
1754 | 1754 | ||
1755 | static void __kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo) |
1755 | static void __kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo) |
1756 | { |
1756 | { |
1757 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
1757 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
1758 | 1758 | ||
1759 | assert(list_is_empty(&bo->list)); |
1759 | assert(list_is_empty(&bo->list)); |
1760 | assert(bo->refcnt == 0); |
1760 | assert(bo->refcnt == 0); |
1761 | assert(!bo->purged || !bo->reusable); |
1761 | assert(!bo->purged || !bo->reusable); |
1762 | assert(bo->proxy == NULL); |
1762 | assert(bo->proxy == NULL); |
1763 | assert_tiling(kgem, bo); |
1763 | assert_tiling(kgem, bo); |
1764 | 1764 | ||
1765 | bo->binding.offset = 0; |
1765 | bo->binding.offset = 0; |
1766 | 1766 | ||
1767 | if (DBG_NO_CACHE) |
1767 | if (DBG_NO_CACHE) |
1768 | goto destroy; |
1768 | goto destroy; |
1769 | 1769 | ||
1770 | if (bo->snoop && !bo->flush) { |
1770 | if (bo->snoop && !bo->flush) { |
1771 | DBG(("%s: handle=%d is snooped\n", __FUNCTION__, bo->handle)); |
1771 | DBG(("%s: handle=%d is snooped\n", __FUNCTION__, bo->handle)); |
1772 | assert(bo->reusable); |
1772 | assert(bo->reusable); |
1773 | assert(list_is_empty(&bo->list)); |
1773 | assert(list_is_empty(&bo->list)); |
1774 | if (bo->exec == NULL && bo->rq && !__kgem_busy(kgem, bo->handle)) |
1774 | if (bo->exec == NULL && bo->rq && !__kgem_busy(kgem, bo->handle)) |
1775 | __kgem_bo_clear_busy(bo); |
1775 | __kgem_bo_clear_busy(bo); |
1776 | if (bo->rq == NULL) |
1776 | if (bo->rq == NULL) |
1777 | kgem_bo_move_to_snoop(kgem, bo); |
1777 | kgem_bo_move_to_snoop(kgem, bo); |
1778 | return; |
1778 | return; |
1779 | } |
1779 | } |
1780 | if (!IS_USER_MAP(bo->map)) |
1780 | if (!IS_USER_MAP(bo->map)) |
1781 | bo->flush = false; |
1781 | bo->flush = false; |
1782 | 1782 | ||
1783 | if (bo->scanout) { |
1783 | if (bo->scanout) { |
1784 | kgem_bo_move_to_scanout(kgem, bo); |
1784 | kgem_bo_move_to_scanout(kgem, bo); |
1785 | return; |
1785 | return; |
1786 | } |
1786 | } |
1787 | 1787 | ||
1788 | if (bo->io) |
1788 | if (bo->io) |
1789 | bo = kgem_bo_replace_io(bo); |
1789 | bo = kgem_bo_replace_io(bo); |
1790 | if (!bo->reusable) { |
1790 | if (!bo->reusable) { |
1791 | DBG(("%s: handle=%d, not reusable\n", |
1791 | DBG(("%s: handle=%d, not reusable\n", |
1792 | __FUNCTION__, bo->handle)); |
1792 | __FUNCTION__, bo->handle)); |
1793 | goto destroy; |
1793 | goto destroy; |
1794 | } |
1794 | } |
1795 | 1795 | ||
1796 | if (!kgem->has_llc && IS_CPU_MAP(bo->map) && bo->domain != DOMAIN_CPU) |
1796 | if (!kgem->has_llc && IS_CPU_MAP(bo->map) && bo->domain != DOMAIN_CPU) |
1797 | kgem_bo_release_map(kgem, bo); |
1797 | kgem_bo_release_map(kgem, bo); |
1798 | 1798 | ||
1799 | assert(list_is_empty(&bo->vma)); |
1799 | assert(list_is_empty(&bo->vma)); |
1800 | assert(list_is_empty(&bo->list)); |
1800 | assert(list_is_empty(&bo->list)); |
1801 | assert(bo->flush == false); |
1801 | assert(bo->flush == false); |
1802 | assert(bo->snoop == false); |
1802 | assert(bo->snoop == false); |
1803 | assert(bo->io == false); |
1803 | assert(bo->io == false); |
1804 | assert(bo->scanout == false); |
1804 | assert(bo->scanout == false); |
1805 | 1805 | ||
1806 | kgem_bo_undo(kgem, bo); |
1806 | kgem_bo_undo(kgem, bo); |
1807 | assert(bo->refcnt == 0); |
1807 | assert(bo->refcnt == 0); |
1808 | 1808 | ||
1809 | if (bo->rq && bo->exec == NULL && !__kgem_busy(kgem, bo->handle)) |
1809 | if (bo->rq && bo->exec == NULL && !__kgem_busy(kgem, bo->handle)) |
1810 | __kgem_bo_clear_busy(bo); |
1810 | __kgem_bo_clear_busy(bo); |
1811 | 1811 | ||
1812 | if (bo->rq) { |
1812 | if (bo->rq) { |
1813 | struct list *cache; |
1813 | struct list *cache; |
1814 | 1814 | ||
1815 | DBG(("%s: handle=%d -> active\n", __FUNCTION__, bo->handle)); |
1815 | DBG(("%s: handle=%d -> active\n", __FUNCTION__, bo->handle)); |
1816 | if (bucket(bo) < NUM_CACHE_BUCKETS) |
1816 | if (bucket(bo) < NUM_CACHE_BUCKETS) |
1817 | cache = &kgem->active[bucket(bo)][bo->tiling]; |
1817 | cache = &kgem->active[bucket(bo)][bo->tiling]; |
1818 | else |
1818 | else |
1819 | cache = &kgem->large; |
1819 | cache = &kgem->large; |
1820 | list_add(&bo->list, cache); |
1820 | list_add(&bo->list, cache); |
1821 | return; |
1821 | return; |
1822 | } |
1822 | } |
1823 | 1823 | ||
1824 | assert(bo->exec == NULL); |
1824 | assert(bo->exec == NULL); |
1825 | assert(list_is_empty(&bo->request)); |
1825 | assert(list_is_empty(&bo->request)); |
1826 | 1826 | ||
1827 | if (!IS_CPU_MAP(bo->map)) { |
1827 | if (!IS_CPU_MAP(bo->map)) { |
1828 | if (!kgem_bo_set_purgeable(kgem, bo)) |
1828 | if (!kgem_bo_set_purgeable(kgem, bo)) |
1829 | goto destroy; |
1829 | goto destroy; |
1830 | 1830 | ||
1831 | if (!kgem->has_llc && bo->domain == DOMAIN_CPU) |
1831 | if (!kgem->has_llc && bo->domain == DOMAIN_CPU) |
1832 | goto destroy; |
1832 | goto destroy; |
1833 | 1833 | ||
1834 | DBG(("%s: handle=%d, purged\n", |
1834 | DBG(("%s: handle=%d, purged\n", |
1835 | __FUNCTION__, bo->handle)); |
1835 | __FUNCTION__, bo->handle)); |
1836 | } |
1836 | } |
1837 | 1837 | ||
1838 | kgem_bo_move_to_inactive(kgem, bo); |
1838 | kgem_bo_move_to_inactive(kgem, bo); |
1839 | return; |
1839 | return; |
1840 | 1840 | ||
1841 | destroy: |
1841 | destroy: |
1842 | if (!bo->exec) |
1842 | if (!bo->exec) |
1843 | kgem_bo_free(kgem, bo); |
1843 | kgem_bo_free(kgem, bo); |
1844 | } |
1844 | } |
1845 | 1845 | ||
1846 | static void kgem_bo_unref(struct kgem *kgem, struct kgem_bo *bo) |
1846 | static void kgem_bo_unref(struct kgem *kgem, struct kgem_bo *bo) |
1847 | { |
1847 | { |
1848 | assert(bo->refcnt); |
1848 | assert(bo->refcnt); |
1849 | if (--bo->refcnt == 0) |
1849 | if (--bo->refcnt == 0) |
1850 | __kgem_bo_destroy(kgem, bo); |
1850 | __kgem_bo_destroy(kgem, bo); |
1851 | } |
1851 | } |
1852 | 1852 | ||
1853 | static void kgem_buffer_release(struct kgem *kgem, struct kgem_buffer *bo) |
1853 | static void kgem_buffer_release(struct kgem *kgem, struct kgem_buffer *bo) |
1854 | { |
1854 | { |
1855 | while (!list_is_empty(&bo->base.vma)) { |
1855 | while (!list_is_empty(&bo->base.vma)) { |
1856 | struct kgem_bo *cached; |
1856 | struct kgem_bo *cached; |
1857 | 1857 | ||
1858 | cached = list_first_entry(&bo->base.vma, struct kgem_bo, vma); |
1858 | cached = list_first_entry(&bo->base.vma, struct kgem_bo, vma); |
1859 | assert(cached->proxy == &bo->base); |
1859 | assert(cached->proxy == &bo->base); |
1860 | list_del(&cached->vma); |
1860 | list_del(&cached->vma); |
1861 | 1861 | ||
1862 | assert(*(struct kgem_bo **)cached->map == cached); |
1862 | assert(*(struct kgem_bo **)cached->map == cached); |
1863 | *(struct kgem_bo **)cached->map = NULL; |
1863 | *(struct kgem_bo **)cached->map = NULL; |
1864 | cached->map = NULL; |
1864 | cached->map = NULL; |
1865 | 1865 | ||
1866 | kgem_bo_destroy(kgem, cached); |
1866 | kgem_bo_destroy(kgem, cached); |
1867 | } |
1867 | } |
1868 | } |
1868 | } |
1869 | 1869 | ||
1870 | static bool kgem_retire__buffers(struct kgem *kgem) |
1870 | static bool kgem_retire__buffers(struct kgem *kgem) |
1871 | { |
1871 | { |
1872 | bool retired = false; |
1872 | bool retired = false; |
1873 | 1873 | ||
1874 | while (!list_is_empty(&kgem->active_buffers)) { |
1874 | while (!list_is_empty(&kgem->active_buffers)) { |
1875 | struct kgem_buffer *bo = |
1875 | struct kgem_buffer *bo = |
1876 | list_last_entry(&kgem->active_buffers, |
1876 | list_last_entry(&kgem->active_buffers, |
1877 | struct kgem_buffer, |
1877 | struct kgem_buffer, |
1878 | base.list); |
1878 | base.list); |
1879 | 1879 | ||
1880 | if (bo->base.rq) |
1880 | if (bo->base.rq) |
1881 | break; |
1881 | break; |
1882 | 1882 | ||
1883 | DBG(("%s: releasing upload cache for handle=%d? %d\n", |
1883 | DBG(("%s: releasing upload cache for handle=%d? %d\n", |
1884 | __FUNCTION__, bo->base.handle, !list_is_empty(&bo->base.vma))); |
1884 | __FUNCTION__, bo->base.handle, !list_is_empty(&bo->base.vma))); |
1885 | list_del(&bo->base.list); |
1885 | list_del(&bo->base.list); |
1886 | kgem_buffer_release(kgem, bo); |
1886 | kgem_buffer_release(kgem, bo); |
1887 | kgem_bo_unref(kgem, &bo->base); |
1887 | kgem_bo_unref(kgem, &bo->base); |
1888 | retired = true; |
1888 | retired = true; |
1889 | } |
1889 | } |
1890 | 1890 | ||
1891 | return retired; |
1891 | return retired; |
1892 | } |
1892 | } |
1893 | 1893 | ||
1894 | static bool kgem_retire__flushing(struct kgem *kgem) |
1894 | static bool kgem_retire__flushing(struct kgem *kgem) |
1895 | { |
1895 | { |
1896 | struct kgem_bo *bo, *next; |
1896 | struct kgem_bo *bo, *next; |
1897 | bool retired = false; |
1897 | bool retired = false; |
1898 | 1898 | ||
1899 | list_for_each_entry_safe(bo, next, &kgem->flushing, request) { |
1899 | list_for_each_entry_safe(bo, next, &kgem->flushing, request) { |
1900 | assert(bo->rq == (void *)kgem); |
1900 | assert(bo->rq == (void *)kgem); |
1901 | assert(bo->exec == NULL); |
1901 | assert(bo->exec == NULL); |
1902 | 1902 | ||
1903 | if (__kgem_busy(kgem, bo->handle)) |
1903 | if (__kgem_busy(kgem, bo->handle)) |
1904 | break; |
1904 | break; |
1905 | 1905 | ||
1906 | __kgem_bo_clear_busy(bo); |
1906 | __kgem_bo_clear_busy(bo); |
1907 | 1907 | ||
1908 | if (bo->refcnt) |
1908 | if (bo->refcnt) |
1909 | continue; |
1909 | continue; |
1910 | 1910 | ||
1911 | if (bo->snoop) { |
1911 | if (bo->snoop) { |
1912 | kgem_bo_move_to_snoop(kgem, bo); |
1912 | kgem_bo_move_to_snoop(kgem, bo); |
1913 | } else if (bo->scanout) { |
1913 | } else if (bo->scanout) { |
1914 | kgem_bo_move_to_scanout(kgem, bo); |
1914 | kgem_bo_move_to_scanout(kgem, bo); |
1915 | } else if ((bo = kgem_bo_replace_io(bo))->reusable && |
1915 | } else if ((bo = kgem_bo_replace_io(bo))->reusable && |
1916 | kgem_bo_set_purgeable(kgem, bo)) { |
1916 | kgem_bo_set_purgeable(kgem, bo)) { |
1917 | kgem_bo_move_to_inactive(kgem, bo); |
1917 | kgem_bo_move_to_inactive(kgem, bo); |
1918 | retired = true; |
1918 | retired = true; |
1919 | } else |
1919 | } else |
1920 | kgem_bo_free(kgem, bo); |
1920 | kgem_bo_free(kgem, bo); |
1921 | } |
1921 | } |
1922 | #if HAS_DEBUG_FULL |
1922 | #if HAS_DEBUG_FULL |
1923 | { |
1923 | { |
1924 | int count = 0; |
1924 | int count = 0; |
1925 | list_for_each_entry(bo, &kgem->flushing, request) |
1925 | list_for_each_entry(bo, &kgem->flushing, request) |
1926 | count++; |
1926 | count++; |
1927 | ErrorF("%s: %d bo on flushing list\n", __FUNCTION__, count); |
1927 | ErrorF("%s: %d bo on flushing list\n", __FUNCTION__, count); |
1928 | } |
1928 | } |
1929 | #endif |
1929 | #endif |
1930 | 1930 | ||
1931 | kgem->need_retire |= !list_is_empty(&kgem->flushing); |
1931 | kgem->need_retire |= !list_is_empty(&kgem->flushing); |
1932 | 1932 | ||
1933 | return retired; |
1933 | return retired; |
1934 | } |
1934 | } |
1935 | 1935 | ||
1936 | 1936 | ||
1937 | static bool __kgem_retire_rq(struct kgem *kgem, struct kgem_request *rq) |
1937 | static bool __kgem_retire_rq(struct kgem *kgem, struct kgem_request *rq) |
1938 | { |
1938 | { |
1939 | bool retired = false; |
1939 | bool retired = false; |
1940 | 1940 | ||
1941 | DBG(("%s: request %d complete\n", |
1941 | DBG(("%s: request %d complete\n", |
1942 | __FUNCTION__, rq->bo->handle)); |
1942 | __FUNCTION__, rq->bo->handle)); |
1943 | 1943 | ||
1944 | while (!list_is_empty(&rq->buffers)) { |
1944 | while (!list_is_empty(&rq->buffers)) { |
1945 | struct kgem_bo *bo; |
1945 | struct kgem_bo *bo; |
1946 | 1946 | ||
1947 | bo = list_first_entry(&rq->buffers, |
1947 | bo = list_first_entry(&rq->buffers, |
1948 | struct kgem_bo, |
1948 | struct kgem_bo, |
1949 | request); |
1949 | request); |
1950 | 1950 | ||
1951 | assert(RQ(bo->rq) == rq); |
1951 | assert(RQ(bo->rq) == rq); |
1952 | assert(bo->exec == NULL); |
1952 | assert(bo->exec == NULL); |
1953 | assert(bo->domain == DOMAIN_GPU || bo->domain == DOMAIN_NONE); |
1953 | assert(bo->domain == DOMAIN_GPU || bo->domain == DOMAIN_NONE); |
1954 | 1954 | ||
1955 | list_del(&bo->request); |
1955 | list_del(&bo->request); |
1956 | 1956 | ||
1957 | if (bo->needs_flush) |
1957 | if (bo->needs_flush) |
1958 | bo->needs_flush = __kgem_busy(kgem, bo->handle); |
1958 | bo->needs_flush = __kgem_busy(kgem, bo->handle); |
1959 | if (bo->needs_flush) { |
1959 | if (bo->needs_flush) { |
1960 | DBG(("%s: moving %d to flushing\n", |
1960 | DBG(("%s: moving %d to flushing\n", |
1961 | __FUNCTION__, bo->handle)); |
1961 | __FUNCTION__, bo->handle)); |
1962 | list_add(&bo->request, &kgem->flushing); |
1962 | list_add(&bo->request, &kgem->flushing); |
1963 | bo->rq = (void *)kgem; |
1963 | bo->rq = (void *)kgem; |
1964 | continue; |
1964 | continue; |
1965 | } |
1965 | } |
1966 | 1966 | ||
1967 | bo->domain = DOMAIN_NONE; |
1967 | bo->domain = DOMAIN_NONE; |
1968 | bo->rq = NULL; |
1968 | bo->rq = NULL; |
1969 | if (bo->refcnt) |
1969 | if (bo->refcnt) |
1970 | continue; |
1970 | continue; |
1971 | 1971 | ||
1972 | if (bo->snoop) { |
1972 | if (bo->snoop) { |
1973 | kgem_bo_move_to_snoop(kgem, bo); |
1973 | kgem_bo_move_to_snoop(kgem, bo); |
1974 | } else if (bo->scanout) { |
1974 | } else if (bo->scanout) { |
1975 | kgem_bo_move_to_scanout(kgem, bo); |
1975 | kgem_bo_move_to_scanout(kgem, bo); |
1976 | } else if ((bo = kgem_bo_replace_io(bo))->reusable && |
1976 | } else if ((bo = kgem_bo_replace_io(bo))->reusable && |
1977 | kgem_bo_set_purgeable(kgem, bo)) { |
1977 | kgem_bo_set_purgeable(kgem, bo)) { |
1978 | kgem_bo_move_to_inactive(kgem, bo); |
1978 | kgem_bo_move_to_inactive(kgem, bo); |
1979 | retired = true; |
1979 | retired = true; |
1980 | } else { |
1980 | } else { |
1981 | DBG(("%s: closing %d\n", |
1981 | DBG(("%s: closing %d\n", |
1982 | __FUNCTION__, bo->handle)); |
1982 | __FUNCTION__, bo->handle)); |
1983 | kgem_bo_free(kgem, bo); |
1983 | kgem_bo_free(kgem, bo); |
1984 | } |
1984 | } |
1985 | } |
1985 | } |
1986 | 1986 | ||
1987 | assert(rq->bo->rq == NULL); |
1987 | assert(rq->bo->rq == NULL); |
1988 | assert(list_is_empty(&rq->bo->request)); |
1988 | assert(list_is_empty(&rq->bo->request)); |
1989 | 1989 | ||
1990 | if (--rq->bo->refcnt == 0) { |
1990 | if (--rq->bo->refcnt == 0) { |
1991 | if (kgem_bo_set_purgeable(kgem, rq->bo)) { |
1991 | if (kgem_bo_set_purgeable(kgem, rq->bo)) { |
1992 | kgem_bo_move_to_inactive(kgem, rq->bo); |
1992 | kgem_bo_move_to_inactive(kgem, rq->bo); |
1993 | retired = true; |
1993 | retired = true; |
1994 | } else { |
1994 | } else { |
1995 | DBG(("%s: closing %d\n", |
1995 | DBG(("%s: closing %d\n", |
1996 | __FUNCTION__, rq->bo->handle)); |
1996 | __FUNCTION__, rq->bo->handle)); |
1997 | kgem_bo_free(kgem, rq->bo); |
1997 | kgem_bo_free(kgem, rq->bo); |
1998 | } |
1998 | } |
1999 | } |
1999 | } |
2000 | 2000 | ||
2001 | __kgem_request_free(rq); |
2001 | __kgem_request_free(rq); |
2002 | return retired; |
2002 | return retired; |
2003 | } |
2003 | } |
2004 | 2004 | ||
2005 | static bool kgem_retire__requests_ring(struct kgem *kgem, int ring) |
2005 | static bool kgem_retire__requests_ring(struct kgem *kgem, int ring) |
2006 | { |
2006 | { |
2007 | bool retired = false; |
2007 | bool retired = false; |
2008 | 2008 | ||
2009 | while (!list_is_empty(&kgem->requests[ring])) { |
2009 | while (!list_is_empty(&kgem->requests[ring])) { |
2010 | struct kgem_request *rq; |
2010 | struct kgem_request *rq; |
2011 | 2011 | ||
2012 | rq = list_first_entry(&kgem->requests[ring], |
2012 | rq = list_first_entry(&kgem->requests[ring], |
2013 | struct kgem_request, |
2013 | struct kgem_request, |
2014 | list); |
2014 | list); |
2015 | if (__kgem_busy(kgem, rq->bo->handle)) |
2015 | if (__kgem_busy(kgem, rq->bo->handle)) |
2016 | break; |
2016 | break; |
2017 | 2017 | ||
2018 | retired |= __kgem_retire_rq(kgem, rq); |
2018 | retired |= __kgem_retire_rq(kgem, rq); |
2019 | } |
2019 | } |
2020 | 2020 | ||
2021 | #if HAS_DEBUG_FULL |
2021 | #if HAS_DEBUG_FULL |
2022 | { |
2022 | { |
2023 | struct kgem_bo *bo; |
2023 | struct kgem_bo *bo; |
2024 | int count = 0; |
2024 | int count = 0; |
2025 | 2025 | ||
2026 | list_for_each_entry(bo, &kgem->requests[ring], request) |
2026 | list_for_each_entry(bo, &kgem->requests[ring], request) |
2027 | count++; |
2027 | count++; |
2028 | 2028 | ||
2029 | bo = NULL; |
2029 | bo = NULL; |
2030 | if (!list_is_empty(&kgem->requests[ring])) |
2030 | if (!list_is_empty(&kgem->requests[ring])) |
2031 | bo = list_first_entry(&kgem->requests[ring], |
2031 | bo = list_first_entry(&kgem->requests[ring], |
2032 | struct kgem_request, |
2032 | struct kgem_request, |
2033 | list)->bo; |
2033 | list)->bo; |
2034 | 2034 | ||
2035 | ErrorF("%s: ring=%d, %d outstanding requests, oldest=%d\n", |
2035 | ErrorF("%s: ring=%d, %d outstanding requests, oldest=%d\n", |
2036 | __FUNCTION__, ring, count, bo ? bo->handle : 0); |
2036 | __FUNCTION__, ring, count, bo ? bo->handle : 0); |
2037 | } |
2037 | } |
2038 | #endif |
2038 | #endif |
2039 | 2039 | ||
2040 | return retired; |
2040 | return retired; |
2041 | } |
2041 | } |
2042 | 2042 | ||
2043 | static bool kgem_retire__requests(struct kgem *kgem) |
2043 | static bool kgem_retire__requests(struct kgem *kgem) |
2044 | { |
2044 | { |
2045 | bool retired = false; |
2045 | bool retired = false; |
2046 | int n; |
2046 | int n; |
2047 | 2047 | ||
2048 | for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) { |
2048 | for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) { |
2049 | retired |= kgem_retire__requests_ring(kgem, n); |
2049 | retired |= kgem_retire__requests_ring(kgem, n); |
2050 | kgem->need_retire |= !list_is_empty(&kgem->requests[n]); |
2050 | kgem->need_retire |= !list_is_empty(&kgem->requests[n]); |
2051 | } |
2051 | } |
2052 | 2052 | ||
2053 | return retired; |
2053 | return retired; |
2054 | } |
2054 | } |
2055 | 2055 | ||
2056 | bool kgem_retire(struct kgem *kgem) |
2056 | bool kgem_retire(struct kgem *kgem) |
2057 | { |
2057 | { |
2058 | bool retired = false; |
2058 | bool retired = false; |
2059 | 2059 | ||
2060 | DBG(("%s\n", __FUNCTION__)); |
2060 | DBG(("%s\n", __FUNCTION__)); |
2061 | 2061 | ||
2062 | kgem->need_retire = false; |
2062 | kgem->need_retire = false; |
2063 | 2063 | ||
2064 | retired |= kgem_retire__flushing(kgem); |
2064 | retired |= kgem_retire__flushing(kgem); |
2065 | retired |= kgem_retire__requests(kgem); |
2065 | retired |= kgem_retire__requests(kgem); |
2066 | retired |= kgem_retire__buffers(kgem); |
2066 | retired |= kgem_retire__buffers(kgem); |
2067 | 2067 | ||
2068 | DBG(("%s -- retired=%d, need_retire=%d\n", |
2068 | DBG(("%s -- retired=%d, need_retire=%d\n", |
2069 | __FUNCTION__, retired, kgem->need_retire)); |
2069 | __FUNCTION__, retired, kgem->need_retire)); |
2070 | 2070 | ||
2071 | kgem->retire(kgem); |
2071 | kgem->retire(kgem); |
2072 | 2072 | ||
2073 | return retired; |
2073 | return retired; |
2074 | } |
2074 | } |
2075 | 2075 | ||
2076 | bool __kgem_ring_is_idle(struct kgem *kgem, int ring) |
2076 | bool __kgem_ring_is_idle(struct kgem *kgem, int ring) |
2077 | { |
2077 | { |
2078 | struct kgem_request *rq; |
2078 | struct kgem_request *rq; |
2079 | 2079 | ||
2080 | assert(!list_is_empty(&kgem->requests[ring])); |
2080 | assert(!list_is_empty(&kgem->requests[ring])); |
2081 | 2081 | ||
2082 | rq = list_last_entry(&kgem->requests[ring], |
2082 | rq = list_last_entry(&kgem->requests[ring], |
2083 | struct kgem_request, list); |
2083 | struct kgem_request, list); |
2084 | if (__kgem_busy(kgem, rq->bo->handle)) { |
2084 | if (__kgem_busy(kgem, rq->bo->handle)) { |
2085 | DBG(("%s: last requests handle=%d still busy\n", |
2085 | DBG(("%s: last requests handle=%d still busy\n", |
2086 | __FUNCTION__, rq->bo->handle)); |
2086 | __FUNCTION__, rq->bo->handle)); |
2087 | return false; |
2087 | return false; |
2088 | } |
2088 | } |
2089 | 2089 | ||
2090 | DBG(("%s: ring=%d idle (handle=%d)\n", |
2090 | DBG(("%s: ring=%d idle (handle=%d)\n", |
2091 | __FUNCTION__, ring, rq->bo->handle)); |
2091 | __FUNCTION__, ring, rq->bo->handle)); |
2092 | 2092 | ||
2093 | kgem_retire__requests_ring(kgem, ring); |
2093 | kgem_retire__requests_ring(kgem, ring); |
2094 | assert(list_is_empty(&kgem->requests[ring])); |
2094 | assert(list_is_empty(&kgem->requests[ring])); |
2095 | return true; |
2095 | return true; |
2096 | } |
2096 | } |
2097 | 2097 | ||
2098 | static void kgem_commit(struct kgem *kgem) |
2098 | static void kgem_commit(struct kgem *kgem) |
2099 | { |
2099 | { |
2100 | struct kgem_request *rq = kgem->next_request; |
2100 | struct kgem_request *rq = kgem->next_request; |
2101 | struct kgem_bo *bo, *next; |
2101 | struct kgem_bo *bo, *next; |
2102 | 2102 | ||
2103 | list_for_each_entry_safe(bo, next, &rq->buffers, request) { |
2103 | list_for_each_entry_safe(bo, next, &rq->buffers, request) { |
2104 | assert(next->request.prev == &bo->request); |
2104 | assert(next->request.prev == &bo->request); |
2105 | 2105 | ||
2106 | DBG(("%s: release handle=%d (proxy? %d), dirty? %d flush? %d, snoop? %d -> offset=%x\n", |
2106 | DBG(("%s: release handle=%d (proxy? %d), dirty? %d flush? %d, snoop? %d -> offset=%x\n", |
2107 | __FUNCTION__, bo->handle, bo->proxy != NULL, |
2107 | __FUNCTION__, bo->handle, bo->proxy != NULL, |
2108 | bo->gpu_dirty, bo->needs_flush, bo->snoop, |
2108 | bo->gpu_dirty, bo->needs_flush, bo->snoop, |
2109 | (unsigned)bo->exec->offset)); |
2109 | (unsigned)bo->exec->offset)); |
2110 | 2110 | ||
2111 | assert(bo->exec); |
2111 | assert(bo->exec); |
2112 | assert(bo->proxy == NULL || bo->exec == &_kgem_dummy_exec); |
2112 | assert(bo->proxy == NULL || bo->exec == &_kgem_dummy_exec); |
2113 | assert(RQ(bo->rq) == rq || (RQ(bo->proxy->rq) == rq)); |
2113 | assert(RQ(bo->rq) == rq || (RQ(bo->proxy->rq) == rq)); |
2114 | 2114 | ||
2115 | bo->presumed_offset = bo->exec->offset; |
2115 | bo->presumed_offset = bo->exec->offset; |
2116 | bo->exec = NULL; |
2116 | bo->exec = NULL; |
2117 | bo->target_handle = -1; |
2117 | bo->target_handle = -1; |
2118 | 2118 | ||
2119 | if (!bo->refcnt && !bo->reusable) { |
2119 | if (!bo->refcnt && !bo->reusable) { |
2120 | assert(!bo->snoop); |
2120 | assert(!bo->snoop); |
2121 | kgem_bo_free(kgem, bo); |
2121 | kgem_bo_free(kgem, bo); |
2122 | continue; |
2122 | continue; |
2123 | } |
2123 | } |
2124 | 2124 | ||
2125 | bo->binding.offset = 0; |
2125 | bo->binding.offset = 0; |
2126 | bo->domain = DOMAIN_GPU; |
2126 | bo->domain = DOMAIN_GPU; |
2127 | bo->gpu_dirty = false; |
2127 | bo->gpu_dirty = false; |
2128 | 2128 | ||
2129 | if (bo->proxy) { |
2129 | if (bo->proxy) { |
2130 | /* proxies are not used for domain tracking */ |
2130 | /* proxies are not used for domain tracking */ |
2131 | bo->exec = NULL; |
2131 | bo->exec = NULL; |
2132 | __kgem_bo_clear_busy(bo); |
2132 | __kgem_bo_clear_busy(bo); |
2133 | } |
2133 | } |
2134 | 2134 | ||
2135 | kgem->scanout_busy |= bo->scanout; |
2135 | kgem->scanout_busy |= bo->scanout; |
2136 | } |
2136 | } |
2137 | 2137 | ||
2138 | if (rq == &kgem->static_request) { |
2138 | if (rq == &kgem->static_request) { |
2139 | struct drm_i915_gem_set_domain set_domain; |
2139 | struct drm_i915_gem_set_domain set_domain; |
2140 | 2140 | ||
2141 | DBG(("%s: syncing due to allocation failure\n", __FUNCTION__)); |
2141 | DBG(("%s: syncing due to allocation failure\n", __FUNCTION__)); |
2142 | 2142 | ||
2143 | VG_CLEAR(set_domain); |
2143 | VG_CLEAR(set_domain); |
2144 | set_domain.handle = rq->bo->handle; |
2144 | set_domain.handle = rq->bo->handle; |
2145 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2145 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2146 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2146 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2147 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) { |
2147 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) { |
2148 | DBG(("%s: sync: GPU hang detected\n", __FUNCTION__)); |
2148 | DBG(("%s: sync: GPU hang detected\n", __FUNCTION__)); |
2149 | kgem_throttle(kgem); |
2149 | kgem_throttle(kgem); |
2150 | } |
2150 | } |
2151 | 2151 | ||
2152 | kgem_retire(kgem); |
2152 | kgem_retire(kgem); |
2153 | assert(list_is_empty(&rq->buffers)); |
2153 | assert(list_is_empty(&rq->buffers)); |
2154 | 2154 | ||
2155 | assert(rq->bo->map == NULL); |
2155 | assert(rq->bo->map == NULL); |
2156 | gem_close(kgem->fd, rq->bo->handle); |
2156 | gem_close(kgem->fd, rq->bo->handle); |
2157 | kgem_cleanup_cache(kgem); |
2157 | kgem_cleanup_cache(kgem); |
2158 | } else { |
2158 | } else { |
2159 | list_add_tail(&rq->list, &kgem->requests[rq->ring]); |
2159 | list_add_tail(&rq->list, &kgem->requests[rq->ring]); |
2160 | kgem->need_throttle = kgem->need_retire = 1; |
2160 | kgem->need_throttle = kgem->need_retire = 1; |
2161 | } |
2161 | } |
2162 | 2162 | ||
2163 | kgem->next_request = NULL; |
2163 | kgem->next_request = NULL; |
2164 | } |
2164 | } |
2165 | 2165 | ||
2166 | static void kgem_close_list(struct kgem *kgem, struct list *head) |
2166 | static void kgem_close_list(struct kgem *kgem, struct list *head) |
2167 | { |
2167 | { |
2168 | while (!list_is_empty(head)) |
2168 | while (!list_is_empty(head)) |
2169 | kgem_bo_free(kgem, list_first_entry(head, struct kgem_bo, list)); |
2169 | kgem_bo_free(kgem, list_first_entry(head, struct kgem_bo, list)); |
2170 | } |
2170 | } |
2171 | 2171 | ||
2172 | static void kgem_close_inactive(struct kgem *kgem) |
2172 | static void kgem_close_inactive(struct kgem *kgem) |
2173 | { |
2173 | { |
2174 | unsigned int i; |
2174 | unsigned int i; |
2175 | 2175 | ||
2176 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) |
2176 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) |
2177 | kgem_close_list(kgem, &kgem->inactive[i]); |
2177 | kgem_close_list(kgem, &kgem->inactive[i]); |
2178 | } |
2178 | } |
2179 | 2179 | ||
2180 | static void kgem_finish_buffers(struct kgem *kgem) |
2180 | static void kgem_finish_buffers(struct kgem *kgem) |
2181 | { |
2181 | { |
2182 | struct kgem_buffer *bo, *next; |
2182 | struct kgem_buffer *bo, *next; |
2183 | 2183 | ||
2184 | list_for_each_entry_safe(bo, next, &kgem->batch_buffers, base.list) { |
2184 | list_for_each_entry_safe(bo, next, &kgem->batch_buffers, base.list) { |
2185 | DBG(("%s: buffer handle=%d, used=%d, exec?=%d, write=%d, mmapped=%s\n", |
2185 | DBG(("%s: buffer handle=%d, used=%d, exec?=%d, write=%d, mmapped=%s\n", |
2186 | __FUNCTION__, bo->base.handle, bo->used, bo->base.exec!=NULL, |
2186 | __FUNCTION__, bo->base.handle, bo->used, bo->base.exec!=NULL, |
2187 | bo->write, bo->mmapped ? IS_CPU_MAP(bo->base.map) ? "cpu" : "gtt" : "no")); |
2187 | bo->write, bo->mmapped ? IS_CPU_MAP(bo->base.map) ? "cpu" : "gtt" : "no")); |
2188 | 2188 | ||
2189 | assert(next->base.list.prev == &bo->base.list); |
2189 | assert(next->base.list.prev == &bo->base.list); |
2190 | assert(bo->base.io); |
2190 | assert(bo->base.io); |
2191 | assert(bo->base.refcnt >= 1); |
2191 | assert(bo->base.refcnt >= 1); |
2192 | 2192 | ||
2193 | if (!bo->base.exec) { |
2193 | if (!bo->base.exec) { |
2194 | DBG(("%s: skipping unattached handle=%d, used=%d\n", |
2194 | DBG(("%s: skipping unattached handle=%d, used=%d\n", |
2195 | __FUNCTION__, bo->base.handle, bo->used)); |
2195 | __FUNCTION__, bo->base.handle, bo->used)); |
2196 | continue; |
2196 | continue; |
2197 | } |
2197 | } |
2198 | 2198 | ||
2199 | if (!bo->write) { |
2199 | if (!bo->write) { |
2200 | assert(bo->base.exec || bo->base.refcnt > 1); |
2200 | assert(bo->base.exec || bo->base.refcnt > 1); |
2201 | goto decouple; |
2201 | goto decouple; |
2202 | } |
2202 | } |
2203 | 2203 | ||
2204 | if (bo->mmapped) { |
2204 | if (bo->mmapped) { |
2205 | int used; |
2205 | int used; |
2206 | 2206 | ||
2207 | assert(!bo->need_io); |
2207 | assert(!bo->need_io); |
2208 | 2208 | ||
2209 | used = ALIGN(bo->used, PAGE_SIZE); |
2209 | used = ALIGN(bo->used, PAGE_SIZE); |
2210 | if (!DBG_NO_UPLOAD_ACTIVE && |
2210 | if (!DBG_NO_UPLOAD_ACTIVE && |
2211 | used + PAGE_SIZE <= bytes(&bo->base) && |
2211 | used + PAGE_SIZE <= bytes(&bo->base) && |
2212 | (kgem->has_llc || !IS_CPU_MAP(bo->base.map) || bo->base.snoop)) { |
2212 | (kgem->has_llc || !IS_CPU_MAP(bo->base.map) || bo->base.snoop)) { |
2213 | DBG(("%s: retaining upload buffer (%d/%d)\n", |
2213 | DBG(("%s: retaining upload buffer (%d/%d)\n", |
2214 | __FUNCTION__, bo->used, bytes(&bo->base))); |
2214 | __FUNCTION__, bo->used, bytes(&bo->base))); |
2215 | bo->used = used; |
2215 | bo->used = used; |
2216 | list_move(&bo->base.list, |
2216 | list_move(&bo->base.list, |
2217 | &kgem->active_buffers); |
2217 | &kgem->active_buffers); |
2218 | continue; |
2218 | continue; |
2219 | } |
2219 | } |
2220 | DBG(("%s: discarding mmapped buffer, used=%d, map type=%d\n", |
2220 | DBG(("%s: discarding mmapped buffer, used=%d, map type=%d\n", |
2221 | __FUNCTION__, bo->used, (int)__MAP_TYPE(bo->base.map))); |
2221 | __FUNCTION__, bo->used, (int)__MAP_TYPE(bo->base.map))); |
2222 | goto decouple; |
2222 | goto decouple; |
2223 | } |
2223 | } |
2224 | 2224 | ||
2225 | if (!bo->used) { |
2225 | if (!bo->used) { |
2226 | /* Unless we replace the handle in the execbuffer, |
2226 | /* Unless we replace the handle in the execbuffer, |
2227 | * then this bo will become active. So decouple it |
2227 | * then this bo will become active. So decouple it |
2228 | * from the buffer list and track it in the normal |
2228 | * from the buffer list and track it in the normal |
2229 | * manner. |
2229 | * manner. |
2230 | */ |
2230 | */ |
2231 | goto decouple; |
2231 | goto decouple; |
2232 | } |
2232 | } |
2233 | 2233 | ||
2234 | assert(bo->need_io); |
2234 | assert(bo->need_io); |
2235 | assert(bo->base.rq == MAKE_REQUEST(kgem->next_request, kgem->ring)); |
2235 | assert(bo->base.rq == MAKE_REQUEST(kgem->next_request, kgem->ring)); |
2236 | assert(bo->base.domain != DOMAIN_GPU); |
2236 | assert(bo->base.domain != DOMAIN_GPU); |
2237 | 2237 | ||
2238 | if (bo->base.refcnt == 1 && |
2238 | if (bo->base.refcnt == 1 && |
2239 | bo->base.size.pages.count > 1 && |
2239 | bo->base.size.pages.count > 1 && |
2240 | bo->used < bytes(&bo->base) / 2) { |
2240 | bo->used < bytes(&bo->base) / 2) { |
2241 | struct kgem_bo *shrink; |
2241 | struct kgem_bo *shrink; |
2242 | unsigned alloc = NUM_PAGES(bo->used); |
2242 | unsigned alloc = NUM_PAGES(bo->used); |
2243 | 2243 | ||
2244 | shrink = search_snoop_cache(kgem, alloc, |
2244 | shrink = search_snoop_cache(kgem, alloc, |
2245 | CREATE_INACTIVE | CREATE_NO_RETIRE); |
2245 | CREATE_INACTIVE | CREATE_NO_RETIRE); |
2246 | if (shrink) { |
2246 | if (shrink) { |
2247 | void *map; |
2247 | void *map; |
2248 | int n; |
2248 | int n; |
2249 | 2249 | ||
2250 | DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n", |
2250 | DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n", |
2251 | __FUNCTION__, |
2251 | __FUNCTION__, |
2252 | bo->used, bytes(&bo->base), bytes(shrink), |
2252 | bo->used, bytes(&bo->base), bytes(shrink), |
2253 | bo->base.handle, shrink->handle)); |
2253 | bo->base.handle, shrink->handle)); |
2254 | 2254 | ||
2255 | assert(bo->used <= bytes(shrink)); |
2255 | assert(bo->used <= bytes(shrink)); |
2256 | map = kgem_bo_map__cpu(kgem, shrink); |
2256 | map = kgem_bo_map__cpu(kgem, shrink); |
2257 | if (map) { |
2257 | if (map) { |
2258 | kgem_bo_sync__cpu(kgem, shrink); |
2258 | kgem_bo_sync__cpu(kgem, shrink); |
2259 | memcpy(map, bo->mem, bo->used); |
2259 | memcpy(map, bo->mem, bo->used); |
2260 | 2260 | ||
2261 | shrink->target_handle = |
2261 | shrink->target_handle = |
2262 | kgem->has_handle_lut ? bo->base.target_handle : shrink->handle; |
2262 | kgem->has_handle_lut ? bo->base.target_handle : shrink->handle; |
2263 | for (n = 0; n < kgem->nreloc; n++) { |
2263 | for (n = 0; n < kgem->nreloc; n++) { |
2264 | if (kgem->reloc[n].target_handle == bo->base.target_handle) { |
2264 | if (kgem->reloc[n].target_handle == bo->base.target_handle) { |
2265 | kgem->reloc[n].target_handle = shrink->target_handle; |
2265 | kgem->reloc[n].target_handle = shrink->target_handle; |
2266 | kgem->reloc[n].presumed_offset = shrink->presumed_offset; |
2266 | kgem->reloc[n].presumed_offset = shrink->presumed_offset; |
2267 | kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] = |
2267 | kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] = |
2268 | kgem->reloc[n].delta + shrink->presumed_offset; |
2268 | kgem->reloc[n].delta + shrink->presumed_offset; |
2269 | } |
2269 | } |
2270 | } |
2270 | } |
2271 | 2271 | ||
2272 | bo->base.exec->handle = shrink->handle; |
2272 | bo->base.exec->handle = shrink->handle; |
2273 | bo->base.exec->offset = shrink->presumed_offset; |
2273 | bo->base.exec->offset = shrink->presumed_offset; |
2274 | shrink->exec = bo->base.exec; |
2274 | shrink->exec = bo->base.exec; |
2275 | shrink->rq = bo->base.rq; |
2275 | shrink->rq = bo->base.rq; |
2276 | list_replace(&bo->base.request, |
2276 | list_replace(&bo->base.request, |
2277 | &shrink->request); |
2277 | &shrink->request); |
2278 | list_init(&bo->base.request); |
2278 | list_init(&bo->base.request); |
2279 | shrink->needs_flush = bo->base.gpu_dirty; |
2279 | shrink->needs_flush = bo->base.gpu_dirty; |
2280 | 2280 | ||
2281 | bo->base.exec = NULL; |
2281 | bo->base.exec = NULL; |
2282 | bo->base.rq = NULL; |
2282 | bo->base.rq = NULL; |
2283 | bo->base.gpu_dirty = false; |
2283 | bo->base.gpu_dirty = false; |
2284 | bo->base.needs_flush = false; |
2284 | bo->base.needs_flush = false; |
2285 | bo->used = 0; |
2285 | bo->used = 0; |
2286 | 2286 | ||
2287 | goto decouple; |
2287 | goto decouple; |
2288 | } |
2288 | } |
2289 | 2289 | ||
2290 | __kgem_bo_destroy(kgem, shrink); |
2290 | __kgem_bo_destroy(kgem, shrink); |
2291 | } |
2291 | } |
2292 | 2292 | ||
2293 | shrink = search_linear_cache(kgem, alloc, |
2293 | shrink = search_linear_cache(kgem, alloc, |
2294 | CREATE_INACTIVE | CREATE_NO_RETIRE); |
2294 | CREATE_INACTIVE | CREATE_NO_RETIRE); |
2295 | if (shrink) { |
2295 | if (shrink) { |
2296 | int n; |
2296 | int n; |
2297 | 2297 | ||
2298 | DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n", |
2298 | DBG(("%s: used=%d, shrinking %d to %d, handle %d to %d\n", |
2299 | __FUNCTION__, |
2299 | __FUNCTION__, |
2300 | bo->used, bytes(&bo->base), bytes(shrink), |
2300 | bo->used, bytes(&bo->base), bytes(shrink), |
2301 | bo->base.handle, shrink->handle)); |
2301 | bo->base.handle, shrink->handle)); |
2302 | 2302 | ||
2303 | assert(bo->used <= bytes(shrink)); |
2303 | assert(bo->used <= bytes(shrink)); |
2304 | if (gem_write(kgem->fd, shrink->handle, |
2304 | if (gem_write(kgem->fd, shrink->handle, |
2305 | 0, bo->used, bo->mem) == 0) { |
2305 | 0, bo->used, bo->mem) == 0) { |
2306 | shrink->target_handle = |
2306 | shrink->target_handle = |
2307 | kgem->has_handle_lut ? bo->base.target_handle : shrink->handle; |
2307 | kgem->has_handle_lut ? bo->base.target_handle : shrink->handle; |
2308 | for (n = 0; n < kgem->nreloc; n++) { |
2308 | for (n = 0; n < kgem->nreloc; n++) { |
2309 | if (kgem->reloc[n].target_handle == bo->base.target_handle) { |
2309 | if (kgem->reloc[n].target_handle == bo->base.target_handle) { |
2310 | kgem->reloc[n].target_handle = shrink->target_handle; |
2310 | kgem->reloc[n].target_handle = shrink->target_handle; |
2311 | kgem->reloc[n].presumed_offset = shrink->presumed_offset; |
2311 | kgem->reloc[n].presumed_offset = shrink->presumed_offset; |
2312 | kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] = |
2312 | kgem->batch[kgem->reloc[n].offset/sizeof(kgem->batch[0])] = |
2313 | kgem->reloc[n].delta + shrink->presumed_offset; |
2313 | kgem->reloc[n].delta + shrink->presumed_offset; |
2314 | } |
2314 | } |
2315 | } |
2315 | } |
2316 | 2316 | ||
2317 | bo->base.exec->handle = shrink->handle; |
2317 | bo->base.exec->handle = shrink->handle; |
2318 | bo->base.exec->offset = shrink->presumed_offset; |
2318 | bo->base.exec->offset = shrink->presumed_offset; |
2319 | shrink->exec = bo->base.exec; |
2319 | shrink->exec = bo->base.exec; |
2320 | shrink->rq = bo->base.rq; |
2320 | shrink->rq = bo->base.rq; |
2321 | list_replace(&bo->base.request, |
2321 | list_replace(&bo->base.request, |
2322 | &shrink->request); |
2322 | &shrink->request); |
2323 | list_init(&bo->base.request); |
2323 | list_init(&bo->base.request); |
2324 | shrink->needs_flush = bo->base.gpu_dirty; |
2324 | shrink->needs_flush = bo->base.gpu_dirty; |
2325 | 2325 | ||
2326 | bo->base.exec = NULL; |
2326 | bo->base.exec = NULL; |
2327 | bo->base.rq = NULL; |
2327 | bo->base.rq = NULL; |
2328 | bo->base.gpu_dirty = false; |
2328 | bo->base.gpu_dirty = false; |
2329 | bo->base.needs_flush = false; |
2329 | bo->base.needs_flush = false; |
2330 | bo->used = 0; |
2330 | bo->used = 0; |
2331 | 2331 | ||
2332 | goto decouple; |
2332 | goto decouple; |
2333 | } |
2333 | } |
2334 | 2334 | ||
2335 | __kgem_bo_destroy(kgem, shrink); |
2335 | __kgem_bo_destroy(kgem, shrink); |
2336 | } |
2336 | } |
2337 | } |
2337 | } |
2338 | 2338 | ||
2339 | DBG(("%s: handle=%d, uploading %d/%d\n", |
2339 | DBG(("%s: handle=%d, uploading %d/%d\n", |
2340 | __FUNCTION__, bo->base.handle, bo->used, bytes(&bo->base))); |
2340 | __FUNCTION__, bo->base.handle, bo->used, bytes(&bo->base))); |
2341 | ASSERT_IDLE(kgem, bo->base.handle); |
2341 | ASSERT_IDLE(kgem, bo->base.handle); |
2342 | assert(bo->used <= bytes(&bo->base)); |
2342 | assert(bo->used <= bytes(&bo->base)); |
2343 | gem_write(kgem->fd, bo->base.handle, |
2343 | gem_write(kgem->fd, bo->base.handle, |
2344 | 0, bo->used, bo->mem); |
2344 | 0, bo->used, bo->mem); |
2345 | bo->need_io = 0; |
2345 | bo->need_io = 0; |
2346 | 2346 | ||
2347 | decouple: |
2347 | decouple: |
2348 | DBG(("%s: releasing handle=%d\n", |
2348 | DBG(("%s: releasing handle=%d\n", |
2349 | __FUNCTION__, bo->base.handle)); |
2349 | __FUNCTION__, bo->base.handle)); |
2350 | list_del(&bo->base.list); |
2350 | list_del(&bo->base.list); |
2351 | kgem_bo_unref(kgem, &bo->base); |
2351 | kgem_bo_unref(kgem, &bo->base); |
2352 | } |
2352 | } |
2353 | } |
2353 | } |
2354 | 2354 | ||
2355 | static void kgem_cleanup(struct kgem *kgem) |
2355 | static void kgem_cleanup(struct kgem *kgem) |
2356 | { |
2356 | { |
2357 | int n; |
2357 | int n; |
2358 | 2358 | ||
2359 | for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) { |
2359 | for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) { |
2360 | while (!list_is_empty(&kgem->requests[n])) { |
2360 | while (!list_is_empty(&kgem->requests[n])) { |
2361 | struct kgem_request *rq; |
2361 | struct kgem_request *rq; |
2362 | 2362 | ||
2363 | rq = list_first_entry(&kgem->requests[n], |
2363 | rq = list_first_entry(&kgem->requests[n], |
2364 | struct kgem_request, |
2364 | struct kgem_request, |
2365 | list); |
2365 | list); |
2366 | while (!list_is_empty(&rq->buffers)) { |
2366 | while (!list_is_empty(&rq->buffers)) { |
2367 | struct kgem_bo *bo; |
2367 | struct kgem_bo *bo; |
2368 | 2368 | ||
2369 | bo = list_first_entry(&rq->buffers, |
2369 | bo = list_first_entry(&rq->buffers, |
2370 | struct kgem_bo, |
2370 | struct kgem_bo, |
2371 | request); |
2371 | request); |
2372 | 2372 | ||
2373 | bo->exec = NULL; |
2373 | bo->exec = NULL; |
2374 | bo->gpu_dirty = false; |
2374 | bo->gpu_dirty = false; |
2375 | __kgem_bo_clear_busy(bo); |
2375 | __kgem_bo_clear_busy(bo); |
2376 | if (bo->refcnt == 0) |
2376 | if (bo->refcnt == 0) |
2377 | kgem_bo_free(kgem, bo); |
2377 | kgem_bo_free(kgem, bo); |
2378 | } |
2378 | } |
2379 | 2379 | ||
2380 | __kgem_request_free(rq); |
2380 | __kgem_request_free(rq); |
2381 | } |
2381 | } |
2382 | } |
2382 | } |
2383 | 2383 | ||
2384 | kgem_close_inactive(kgem); |
2384 | kgem_close_inactive(kgem); |
2385 | } |
2385 | } |
2386 | 2386 | ||
2387 | static int kgem_batch_write(struct kgem *kgem, uint32_t handle, uint32_t size) |
2387 | static int kgem_batch_write(struct kgem *kgem, uint32_t handle, uint32_t size) |
2388 | { |
2388 | { |
2389 | int ret; |
2389 | int ret; |
2390 | 2390 | ||
2391 | ASSERT_IDLE(kgem, handle); |
2391 | ASSERT_IDLE(kgem, handle); |
2392 | 2392 | ||
2393 | /* If there is no surface data, just upload the batch */ |
2393 | /* If there is no surface data, just upload the batch */ |
2394 | if (kgem->surface == kgem->batch_size) |
2394 | if (kgem->surface == kgem->batch_size) |
2395 | return gem_write(kgem->fd, handle, |
2395 | return gem_write(kgem->fd, handle, |
2396 | 0, sizeof(uint32_t)*kgem->nbatch, |
2396 | 0, sizeof(uint32_t)*kgem->nbatch, |
2397 | kgem->batch); |
2397 | kgem->batch); |
2398 | 2398 | ||
2399 | /* Are the batch pages conjoint with the surface pages? */ |
2399 | /* Are the batch pages conjoint with the surface pages? */ |
2400 | if (kgem->surface < kgem->nbatch + PAGE_SIZE/sizeof(uint32_t)) { |
2400 | if (kgem->surface < kgem->nbatch + PAGE_SIZE/sizeof(uint32_t)) { |
2401 | assert(size == PAGE_ALIGN(kgem->batch_size*sizeof(uint32_t))); |
2401 | assert(size == PAGE_ALIGN(kgem->batch_size*sizeof(uint32_t))); |
2402 | return gem_write(kgem->fd, handle, |
2402 | return gem_write(kgem->fd, handle, |
2403 | 0, kgem->batch_size*sizeof(uint32_t), |
2403 | 0, kgem->batch_size*sizeof(uint32_t), |
2404 | kgem->batch); |
2404 | kgem->batch); |
2405 | } |
2405 | } |
2406 | 2406 | ||
2407 | /* Disjoint surface/batch, upload separately */ |
2407 | /* Disjoint surface/batch, upload separately */ |
2408 | ret = gem_write(kgem->fd, handle, |
2408 | ret = gem_write(kgem->fd, handle, |
2409 | 0, sizeof(uint32_t)*kgem->nbatch, |
2409 | 0, sizeof(uint32_t)*kgem->nbatch, |
2410 | kgem->batch); |
2410 | kgem->batch); |
2411 | if (ret) |
2411 | if (ret) |
2412 | return ret; |
2412 | return ret; |
2413 | 2413 | ||
2414 | ret = PAGE_ALIGN(sizeof(uint32_t) * kgem->batch_size); |
2414 | ret = PAGE_ALIGN(sizeof(uint32_t) * kgem->batch_size); |
2415 | ret -= sizeof(uint32_t) * kgem->surface; |
2415 | ret -= sizeof(uint32_t) * kgem->surface; |
2416 | assert(size-ret >= kgem->nbatch*sizeof(uint32_t)); |
2416 | assert(size-ret >= kgem->nbatch*sizeof(uint32_t)); |
2417 | return __gem_write(kgem->fd, handle, |
2417 | return __gem_write(kgem->fd, handle, |
2418 | size - ret, (kgem->batch_size - kgem->surface)*sizeof(uint32_t), |
2418 | size - ret, (kgem->batch_size - kgem->surface)*sizeof(uint32_t), |
2419 | kgem->batch + kgem->surface); |
2419 | kgem->batch + kgem->surface); |
2420 | } |
2420 | } |
2421 | 2421 | ||
2422 | void kgem_reset(struct kgem *kgem) |
2422 | void kgem_reset(struct kgem *kgem) |
2423 | { |
2423 | { |
2424 | if (kgem->next_request) { |
2424 | if (kgem->next_request) { |
2425 | struct kgem_request *rq = kgem->next_request; |
2425 | struct kgem_request *rq = kgem->next_request; |
2426 | 2426 | ||
2427 | while (!list_is_empty(&rq->buffers)) { |
2427 | while (!list_is_empty(&rq->buffers)) { |
2428 | struct kgem_bo *bo = |
2428 | struct kgem_bo *bo = |
2429 | list_first_entry(&rq->buffers, |
2429 | list_first_entry(&rq->buffers, |
2430 | struct kgem_bo, |
2430 | struct kgem_bo, |
2431 | request); |
2431 | request); |
2432 | list_del(&bo->request); |
2432 | list_del(&bo->request); |
2433 | 2433 | ||
2434 | assert(RQ(bo->rq) == rq); |
2434 | assert(RQ(bo->rq) == rq); |
2435 | 2435 | ||
2436 | bo->binding.offset = 0; |
2436 | bo->binding.offset = 0; |
2437 | bo->exec = NULL; |
2437 | bo->exec = NULL; |
2438 | bo->target_handle = -1; |
2438 | bo->target_handle = -1; |
2439 | bo->gpu_dirty = false; |
2439 | bo->gpu_dirty = false; |
2440 | 2440 | ||
2441 | if (bo->needs_flush && __kgem_busy(kgem, bo->handle)) { |
2441 | if (bo->needs_flush && __kgem_busy(kgem, bo->handle)) { |
2442 | assert(bo->domain == DOMAIN_GPU || bo->domain == DOMAIN_NONE); |
2442 | assert(bo->domain == DOMAIN_GPU || bo->domain == DOMAIN_NONE); |
2443 | list_add(&bo->request, &kgem->flushing); |
2443 | list_add(&bo->request, &kgem->flushing); |
2444 | bo->rq = (void *)kgem; |
2444 | bo->rq = (void *)kgem; |
2445 | } else |
2445 | } else |
2446 | __kgem_bo_clear_busy(bo); |
2446 | __kgem_bo_clear_busy(bo); |
2447 | 2447 | ||
2448 | if (bo->refcnt || bo->rq) |
2448 | if (bo->refcnt || bo->rq) |
2449 | continue; |
2449 | continue; |
2450 | 2450 | ||
2451 | if (bo->snoop) { |
2451 | if (bo->snoop) { |
2452 | kgem_bo_move_to_snoop(kgem, bo); |
2452 | kgem_bo_move_to_snoop(kgem, bo); |
2453 | } else if (bo->scanout) { |
2453 | } else if (bo->scanout) { |
2454 | kgem_bo_move_to_scanout(kgem, bo); |
2454 | kgem_bo_move_to_scanout(kgem, bo); |
2455 | } else if ((bo = kgem_bo_replace_io(bo))->reusable && |
2455 | } else if ((bo = kgem_bo_replace_io(bo))->reusable && |
2456 | kgem_bo_set_purgeable(kgem, bo)) { |
2456 | kgem_bo_set_purgeable(kgem, bo)) { |
2457 | kgem_bo_move_to_inactive(kgem, bo); |
2457 | kgem_bo_move_to_inactive(kgem, bo); |
2458 | } else { |
2458 | } else { |
2459 | DBG(("%s: closing %d\n", |
2459 | DBG(("%s: closing %d\n", |
2460 | __FUNCTION__, bo->handle)); |
2460 | __FUNCTION__, bo->handle)); |
2461 | kgem_bo_free(kgem, bo); |
2461 | kgem_bo_free(kgem, bo); |
2462 | } |
2462 | } |
2463 | } |
2463 | } |
2464 | 2464 | ||
2465 | if (rq != &kgem->static_request) { |
2465 | if (rq != &kgem->static_request) { |
2466 | list_init(&rq->list); |
2466 | list_init(&rq->list); |
2467 | __kgem_request_free(rq); |
2467 | __kgem_request_free(rq); |
2468 | } |
2468 | } |
2469 | } |
2469 | } |
2470 | 2470 | ||
2471 | kgem->nfence = 0; |
2471 | kgem->nfence = 0; |
2472 | kgem->nexec = 0; |
2472 | kgem->nexec = 0; |
2473 | kgem->nreloc = 0; |
2473 | kgem->nreloc = 0; |
2474 | kgem->nreloc__self = 0; |
2474 | kgem->nreloc__self = 0; |
2475 | kgem->aperture = 0; |
2475 | kgem->aperture = 0; |
2476 | kgem->aperture_fenced = 0; |
2476 | kgem->aperture_fenced = 0; |
2477 | kgem->nbatch = 0; |
2477 | kgem->nbatch = 0; |
2478 | kgem->surface = kgem->batch_size; |
2478 | kgem->surface = kgem->batch_size; |
2479 | kgem->mode = KGEM_NONE; |
2479 | kgem->mode = KGEM_NONE; |
2480 | kgem->flush = 0; |
2480 | kgem->flush = 0; |
2481 | kgem->batch_flags = kgem->batch_flags_base; |
2481 | kgem->batch_flags = kgem->batch_flags_base; |
2482 | 2482 | ||
2483 | kgem->next_request = __kgem_request_alloc(kgem); |
2483 | kgem->next_request = __kgem_request_alloc(kgem); |
2484 | 2484 | ||
2485 | kgem_sna_reset(kgem); |
2485 | kgem_sna_reset(kgem); |
2486 | } |
2486 | } |
2487 | 2487 | ||
2488 | static int compact_batch_surface(struct kgem *kgem) |
2488 | static int compact_batch_surface(struct kgem *kgem) |
2489 | { |
2489 | { |
2490 | int size, shrink, n; |
2490 | int size, shrink, n; |
2491 | 2491 | ||
2492 | if (!kgem->has_relaxed_delta) |
2492 | if (!kgem->has_relaxed_delta) |
2493 | return kgem->batch_size; |
2493 | return kgem->batch_size; |
2494 | 2494 | ||
2495 | /* See if we can pack the contents into one or two pages */ |
2495 | /* See if we can pack the contents into one or two pages */ |
2496 | n = ALIGN(kgem->batch_size, 1024); |
2496 | n = ALIGN(kgem->batch_size, 1024); |
2497 | size = n - kgem->surface + kgem->nbatch; |
2497 | size = n - kgem->surface + kgem->nbatch; |
2498 | size = ALIGN(size, 1024); |
2498 | size = ALIGN(size, 1024); |
2499 | 2499 | ||
2500 | shrink = n - size; |
2500 | shrink = n - size; |
2501 | if (shrink) { |
2501 | if (shrink) { |
2502 | DBG(("shrinking from %d to %d\n", kgem->batch_size, size)); |
2502 | DBG(("shrinking from %d to %d\n", kgem->batch_size, size)); |
2503 | 2503 | ||
2504 | shrink *= sizeof(uint32_t); |
2504 | shrink *= sizeof(uint32_t); |
2505 | for (n = 0; n < kgem->nreloc; n++) { |
2505 | for (n = 0; n < kgem->nreloc; n++) { |
2506 | if (kgem->reloc[n].read_domains == I915_GEM_DOMAIN_INSTRUCTION && |
2506 | if (kgem->reloc[n].read_domains == I915_GEM_DOMAIN_INSTRUCTION && |
2507 | kgem->reloc[n].target_handle == ~0U) |
2507 | kgem->reloc[n].target_handle == ~0U) |
2508 | kgem->reloc[n].delta -= shrink; |
2508 | kgem->reloc[n].delta -= shrink; |
2509 | 2509 | ||
2510 | if (kgem->reloc[n].offset >= sizeof(uint32_t)*kgem->nbatch) |
2510 | if (kgem->reloc[n].offset >= sizeof(uint32_t)*kgem->nbatch) |
2511 | kgem->reloc[n].offset -= shrink; |
2511 | kgem->reloc[n].offset -= shrink; |
2512 | } |
2512 | } |
2513 | } |
2513 | } |
2514 | 2514 | ||
2515 | return size * sizeof(uint32_t); |
2515 | return size * sizeof(uint32_t); |
2516 | } |
2516 | } |
2517 | 2517 | ||
2518 | static struct kgem_bo * |
2518 | static struct kgem_bo * |
2519 | kgem_create_batch(struct kgem *kgem, int size) |
2519 | kgem_create_batch(struct kgem *kgem, int size) |
2520 | { |
2520 | { |
2521 | struct drm_i915_gem_set_domain set_domain; |
2521 | struct drm_i915_gem_set_domain set_domain; |
2522 | struct kgem_bo *bo; |
2522 | struct kgem_bo *bo; |
2523 | 2523 | ||
2524 | if (size <= 4096) { |
2524 | if (size <= 4096) { |
2525 | bo = list_first_entry(&kgem->pinned_batches[0], |
2525 | bo = list_first_entry(&kgem->pinned_batches[0], |
2526 | struct kgem_bo, |
2526 | struct kgem_bo, |
2527 | list); |
2527 | list); |
2528 | if (!bo->rq) { |
2528 | if (!bo->rq) { |
2529 | out_4096: |
2529 | out_4096: |
2530 | list_move_tail(&bo->list, &kgem->pinned_batches[0]); |
2530 | list_move_tail(&bo->list, &kgem->pinned_batches[0]); |
2531 | return kgem_bo_reference(bo); |
2531 | return kgem_bo_reference(bo); |
2532 | } |
2532 | } |
2533 | 2533 | ||
2534 | if (!__kgem_busy(kgem, bo->handle)) { |
2534 | if (!__kgem_busy(kgem, bo->handle)) { |
2535 | assert(RQ(bo->rq)->bo == bo); |
2535 | assert(RQ(bo->rq)->bo == bo); |
2536 | __kgem_retire_rq(kgem, RQ(bo->rq)); |
2536 | __kgem_retire_rq(kgem, RQ(bo->rq)); |
2537 | goto out_4096; |
2537 | goto out_4096; |
2538 | } |
2538 | } |
2539 | } |
2539 | } |
2540 | 2540 | ||
2541 | if (size <= 16384) { |
2541 | if (size <= 16384) { |
2542 | bo = list_first_entry(&kgem->pinned_batches[1], |
2542 | bo = list_first_entry(&kgem->pinned_batches[1], |
2543 | struct kgem_bo, |
2543 | struct kgem_bo, |
2544 | list); |
2544 | list); |
2545 | if (!bo->rq) { |
2545 | if (!bo->rq) { |
2546 | out_16384: |
2546 | out_16384: |
2547 | list_move_tail(&bo->list, &kgem->pinned_batches[1]); |
2547 | list_move_tail(&bo->list, &kgem->pinned_batches[1]); |
2548 | return kgem_bo_reference(bo); |
2548 | return kgem_bo_reference(bo); |
2549 | } |
2549 | } |
2550 | 2550 | ||
2551 | if (!__kgem_busy(kgem, bo->handle)) { |
2551 | if (!__kgem_busy(kgem, bo->handle)) { |
2552 | assert(RQ(bo->rq)->bo == bo); |
2552 | assert(RQ(bo->rq)->bo == bo); |
2553 | __kgem_retire_rq(kgem, RQ(bo->rq)); |
2553 | __kgem_retire_rq(kgem, RQ(bo->rq)); |
2554 | goto out_16384; |
2554 | goto out_16384; |
2555 | } |
2555 | } |
2556 | } |
2556 | } |
2557 | 2557 | ||
2558 | if (kgem->gen == 020 && !kgem->has_pinned_batches) { |
2558 | if (kgem->gen == 020 && !kgem->has_pinned_batches) { |
2559 | assert(size <= 16384); |
2559 | assert(size <= 16384); |
2560 | 2560 | ||
2561 | bo = list_first_entry(&kgem->pinned_batches[size > 4096], |
2561 | bo = list_first_entry(&kgem->pinned_batches[size > 4096], |
2562 | struct kgem_bo, |
2562 | struct kgem_bo, |
2563 | list); |
2563 | list); |
2564 | list_move_tail(&bo->list, &kgem->pinned_batches[size > 4096]); |
2564 | list_move_tail(&bo->list, &kgem->pinned_batches[size > 4096]); |
2565 | 2565 | ||
2566 | DBG(("%s: syncing due to busy batches\n", __FUNCTION__)); |
2566 | DBG(("%s: syncing due to busy batches\n", __FUNCTION__)); |
2567 | 2567 | ||
2568 | VG_CLEAR(set_domain); |
2568 | VG_CLEAR(set_domain); |
2569 | set_domain.handle = bo->handle; |
2569 | set_domain.handle = bo->handle; |
2570 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2570 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2571 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2571 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2572 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) { |
2572 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) { |
2573 | DBG(("%s: sync: GPU hang detected\n", __FUNCTION__)); |
2573 | DBG(("%s: sync: GPU hang detected\n", __FUNCTION__)); |
2574 | kgem_throttle(kgem); |
2574 | kgem_throttle(kgem); |
2575 | return NULL; |
2575 | return NULL; |
2576 | } |
2576 | } |
2577 | 2577 | ||
2578 | kgem_retire(kgem); |
2578 | kgem_retire(kgem); |
2579 | assert(bo->rq == NULL); |
2579 | assert(bo->rq == NULL); |
2580 | return kgem_bo_reference(bo); |
2580 | return kgem_bo_reference(bo); |
2581 | } |
2581 | } |
2582 | 2582 | ||
2583 | return kgem_create_linear(kgem, size, CREATE_NO_THROTTLE); |
2583 | return kgem_create_linear(kgem, size, CREATE_NO_THROTTLE); |
2584 | } |
2584 | } |
2585 | 2585 | ||
2586 | void _kgem_submit(struct kgem *kgem) |
2586 | void _kgem_submit(struct kgem *kgem) |
2587 | { |
2587 | { |
2588 | struct kgem_request *rq; |
2588 | struct kgem_request *rq; |
2589 | uint32_t batch_end; |
2589 | uint32_t batch_end; |
2590 | int size; |
2590 | int size; |
2591 | 2591 | ||
2592 | assert(!DBG_NO_HW); |
2592 | assert(!DBG_NO_HW); |
2593 | assert(!kgem->wedged); |
2593 | assert(!kgem->wedged); |
2594 | 2594 | ||
2595 | assert(kgem->nbatch); |
2595 | assert(kgem->nbatch); |
2596 | assert(kgem->nbatch <= KGEM_BATCH_SIZE(kgem)); |
2596 | assert(kgem->nbatch <= KGEM_BATCH_SIZE(kgem)); |
2597 | assert(kgem->nbatch <= kgem->surface); |
2597 | assert(kgem->nbatch <= kgem->surface); |
2598 | 2598 | ||
2599 | batch_end = kgem_end_batch(kgem); |
2599 | batch_end = kgem_end_batch(kgem); |
2600 | kgem_sna_flush(kgem); |
2600 | kgem_sna_flush(kgem); |
2601 | 2601 | ||
2602 | DBG(("batch[%d/%d, flags=%x]: %d %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d\n", |
2602 | DBG(("batch[%d/%d, flags=%x]: %d %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d\n", |
2603 | kgem->mode, kgem->ring, kgem->batch_flags, |
2603 | kgem->mode, kgem->ring, kgem->batch_flags, |
2604 | batch_end, kgem->nbatch, kgem->surface, kgem->batch_size, |
2604 | batch_end, kgem->nbatch, kgem->surface, kgem->batch_size, |
2605 | kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture)); |
2605 | kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture)); |
2606 | 2606 | ||
2607 | assert(kgem->nbatch <= kgem->batch_size); |
2607 | assert(kgem->nbatch <= kgem->batch_size); |
2608 | assert(kgem->nbatch <= kgem->surface); |
2608 | assert(kgem->nbatch <= kgem->surface); |
2609 | assert(kgem->nreloc <= ARRAY_SIZE(kgem->reloc)); |
2609 | assert(kgem->nreloc <= ARRAY_SIZE(kgem->reloc)); |
2610 | assert(kgem->nexec < ARRAY_SIZE(kgem->exec)); |
2610 | assert(kgem->nexec < ARRAY_SIZE(kgem->exec)); |
2611 | assert(kgem->nfence <= kgem->fence_max); |
2611 | assert(kgem->nfence <= kgem->fence_max); |
2612 | 2612 | ||
2613 | kgem_finish_buffers(kgem); |
2613 | kgem_finish_buffers(kgem); |
2614 | 2614 | ||
2615 | #if SHOW_BATCH |
2615 | #if SHOW_BATCH |
2616 | __kgem_batch_debug(kgem, batch_end); |
2616 | __kgem_batch_debug(kgem, batch_end); |
2617 | #endif |
2617 | #endif |
2618 | 2618 | ||
2619 | rq = kgem->next_request; |
2619 | rq = kgem->next_request; |
2620 | if (kgem->surface != kgem->batch_size) |
2620 | if (kgem->surface != kgem->batch_size) |
2621 | size = compact_batch_surface(kgem); |
2621 | size = compact_batch_surface(kgem); |
2622 | else |
2622 | else |
2623 | size = kgem->nbatch * sizeof(kgem->batch[0]); |
2623 | size = kgem->nbatch * sizeof(kgem->batch[0]); |
2624 | rq->bo = kgem_create_batch(kgem, size); |
2624 | rq->bo = kgem_create_batch(kgem, size); |
2625 | if (rq->bo) { |
2625 | if (rq->bo) { |
2626 | uint32_t handle = rq->bo->handle; |
2626 | uint32_t handle = rq->bo->handle; |
2627 | int i; |
2627 | int i; |
2628 | 2628 | ||
2629 | assert(!rq->bo->needs_flush); |
2629 | assert(!rq->bo->needs_flush); |
2630 | 2630 | ||
2631 | i = kgem->nexec++; |
2631 | i = kgem->nexec++; |
2632 | kgem->exec[i].handle = handle; |
2632 | kgem->exec[i].handle = handle; |
2633 | kgem->exec[i].relocation_count = kgem->nreloc; |
2633 | kgem->exec[i].relocation_count = kgem->nreloc; |
2634 | kgem->exec[i].relocs_ptr = (uintptr_t)kgem->reloc; |
2634 | kgem->exec[i].relocs_ptr = (uintptr_t)kgem->reloc; |
2635 | kgem->exec[i].alignment = 0; |
2635 | kgem->exec[i].alignment = 0; |
2636 | kgem->exec[i].offset = rq->bo->presumed_offset; |
2636 | kgem->exec[i].offset = rq->bo->presumed_offset; |
2637 | kgem->exec[i].flags = 0; |
2637 | kgem->exec[i].flags = 0; |
2638 | kgem->exec[i].rsvd1 = 0; |
2638 | kgem->exec[i].rsvd1 = 0; |
2639 | kgem->exec[i].rsvd2 = 0; |
2639 | kgem->exec[i].rsvd2 = 0; |
2640 | 2640 | ||
2641 | rq->bo->target_handle = kgem->has_handle_lut ? i : handle; |
2641 | rq->bo->target_handle = kgem->has_handle_lut ? i : handle; |
2642 | rq->bo->exec = &kgem->exec[i]; |
2642 | rq->bo->exec = &kgem->exec[i]; |
2643 | rq->bo->rq = MAKE_REQUEST(rq, kgem->ring); /* useful sanity check */ |
2643 | rq->bo->rq = MAKE_REQUEST(rq, kgem->ring); /* useful sanity check */ |
2644 | list_add(&rq->bo->request, &rq->buffers); |
2644 | list_add(&rq->bo->request, &rq->buffers); |
2645 | rq->ring = kgem->ring == KGEM_BLT; |
2645 | rq->ring = kgem->ring == KGEM_BLT; |
2646 | 2646 | ||
2647 | kgem_fixup_self_relocs(kgem, rq->bo); |
2647 | kgem_fixup_self_relocs(kgem, rq->bo); |
2648 | 2648 | ||
2649 | if (kgem_batch_write(kgem, handle, size) == 0) { |
2649 | if (kgem_batch_write(kgem, handle, size) == 0) { |
2650 | struct drm_i915_gem_execbuffer2 execbuf; |
2650 | struct drm_i915_gem_execbuffer2 execbuf; |
2651 | int ret, retry = 3; |
2651 | int ret, retry = 3; |
2652 | 2652 | ||
2653 | memset(&execbuf, 0, sizeof(execbuf)); |
2653 | memset(&execbuf, 0, sizeof(execbuf)); |
2654 | execbuf.buffers_ptr = (uintptr_t)kgem->exec; |
2654 | execbuf.buffers_ptr = (uintptr_t)kgem->exec; |
2655 | execbuf.buffer_count = kgem->nexec; |
2655 | execbuf.buffer_count = kgem->nexec; |
2656 | execbuf.batch_len = batch_end*sizeof(uint32_t); |
2656 | execbuf.batch_len = batch_end*sizeof(uint32_t); |
2657 | execbuf.flags = kgem->ring | kgem->batch_flags; |
2657 | execbuf.flags = kgem->ring | kgem->batch_flags; |
2658 | 2658 | ||
2659 | if (DEBUG_DUMP) |
2659 | if (DEBUG_DUMP) |
2660 | { |
2660 | { |
2661 | int fd = open("/tmp1/1/batchbuffer.bin", O_CREAT|O_WRONLY|O_BINARY); |
2661 | int fd = open("/tmp1/1/batchbuffer.bin", O_CREAT|O_WRONLY|O_BINARY); |
2662 | if (fd != -1) { |
2662 | if (fd != -1) { |
2663 | write(fd, kgem->batch, size); |
2663 | write(fd, kgem->batch, size); |
2664 | close(fd); |
2664 | close(fd); |
2665 | } |
2665 | } |
2666 | else printf("SNA: failed to write batchbuffer\n"); |
2666 | else printf("SNA: failed to write batchbuffer\n"); |
2667 | asm volatile("int3"); |
2667 | asm volatile("int3"); |
2668 | } |
2668 | } |
2669 | 2669 | ||
2670 | ret = drmIoctl(kgem->fd, |
2670 | ret = drmIoctl(kgem->fd, |
2671 | DRM_IOCTL_I915_GEM_EXECBUFFER2, |
2671 | DRM_IOCTL_I915_GEM_EXECBUFFER2, |
2672 | &execbuf); |
2672 | &execbuf); |
2673 | while (ret == -1 && errno == EBUSY && retry--) { |
2673 | while (ret == -1 && errno == EBUSY && retry--) { |
2674 | __kgem_throttle(kgem); |
2674 | __kgem_throttle(kgem); |
2675 | ret = drmIoctl(kgem->fd, |
2675 | ret = drmIoctl(kgem->fd, |
2676 | DRM_IOCTL_I915_GEM_EXECBUFFER2, |
2676 | DRM_IOCTL_I915_GEM_EXECBUFFER2, |
2677 | &execbuf); |
2677 | &execbuf); |
2678 | } |
2678 | } |
2679 | if (DEBUG_SYNC && ret == 0) { |
2679 | if (DEBUG_SYNC && ret == 0) { |
2680 | struct drm_i915_gem_set_domain set_domain; |
2680 | struct drm_i915_gem_set_domain set_domain; |
2681 | 2681 | ||
2682 | VG_CLEAR(set_domain); |
2682 | VG_CLEAR(set_domain); |
2683 | set_domain.handle = handle; |
2683 | set_domain.handle = handle; |
2684 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2684 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2685 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2685 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2686 | 2686 | ||
2687 | ret = drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain); |
2687 | ret = drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain); |
2688 | } |
2688 | } |
2689 | if (ret == -1) { |
2689 | if (ret == -1) { |
2690 | DBG(("%s: GPU hang detected [%d]\n", |
2690 | DBG(("%s: GPU hang detected [%d]\n", |
2691 | __FUNCTION__, errno)); |
2691 | __FUNCTION__, errno)); |
2692 | kgem_throttle(kgem); |
2692 | kgem_throttle(kgem); |
2693 | kgem->wedged = true; |
2693 | kgem->wedged = true; |
2694 | 2694 | ||
2695 | #if 0 |
2695 | #if 0 |
2696 | ret = errno; |
2696 | ret = errno; |
2697 | ErrorF("batch[%d/%d]: %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d: errno=%d\n", |
2697 | ErrorF("batch[%d/%d]: %d %d %d, nreloc=%d, nexec=%d, nfence=%d, aperture=%d: errno=%d\n", |
2698 | kgem->mode, kgem->ring, batch_end, kgem->nbatch, kgem->surface, |
2698 | kgem->mode, kgem->ring, batch_end, kgem->nbatch, kgem->surface, |
2699 | kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture, errno); |
2699 | kgem->nreloc, kgem->nexec, kgem->nfence, kgem->aperture, errno); |
2700 | 2700 | ||
2701 | for (i = 0; i < kgem->nexec; i++) { |
2701 | for (i = 0; i < kgem->nexec; i++) { |
2702 | struct kgem_bo *bo, *found = NULL; |
2702 | struct kgem_bo *bo, *found = NULL; |
2703 | 2703 | ||
2704 | list_for_each_entry(bo, &kgem->next_request->buffers, request) { |
2704 | list_for_each_entry(bo, &kgem->next_request->buffers, request) { |
2705 | if (bo->handle == kgem->exec[i].handle) { |
2705 | if (bo->handle == kgem->exec[i].handle) { |
2706 | found = bo; |
2706 | found = bo; |
2707 | break; |
2707 | break; |
2708 | } |
2708 | } |
2709 | } |
2709 | } |
2710 | ErrorF("exec[%d] = handle:%d, presumed offset: %x, size: %d, tiling %d, fenced %d, snooped %d, deleted %d\n", |
2710 | ErrorF("exec[%d] = handle:%d, presumed offset: %x, size: %d, tiling %d, fenced %d, snooped %d, deleted %d\n", |
2711 | i, |
2711 | i, |
2712 | kgem->exec[i].handle, |
2712 | kgem->exec[i].handle, |
2713 | (int)kgem->exec[i].offset, |
2713 | (int)kgem->exec[i].offset, |
2714 | found ? kgem_bo_size(found) : -1, |
2714 | found ? kgem_bo_size(found) : -1, |
2715 | found ? found->tiling : -1, |
2715 | found ? found->tiling : -1, |
2716 | (int)(kgem->exec[i].flags & EXEC_OBJECT_NEEDS_FENCE), |
2716 | (int)(kgem->exec[i].flags & EXEC_OBJECT_NEEDS_FENCE), |
2717 | found ? found->snoop : -1, |
2717 | found ? found->snoop : -1, |
2718 | found ? found->purged : -1); |
2718 | found ? found->purged : -1); |
2719 | } |
2719 | } |
2720 | for (i = 0; i < kgem->nreloc; i++) { |
2720 | for (i = 0; i < kgem->nreloc; i++) { |
2721 | ErrorF("reloc[%d] = pos:%d, target:%d, delta:%d, read:%x, write:%x, offset:%x\n", |
2721 | ErrorF("reloc[%d] = pos:%d, target:%d, delta:%d, read:%x, write:%x, offset:%x\n", |
2722 | i, |
2722 | i, |
2723 | (int)kgem->reloc[i].offset, |
2723 | (int)kgem->reloc[i].offset, |
2724 | kgem->reloc[i].target_handle, |
2724 | kgem->reloc[i].target_handle, |
2725 | kgem->reloc[i].delta, |
2725 | kgem->reloc[i].delta, |
2726 | kgem->reloc[i].read_domains, |
2726 | kgem->reloc[i].read_domains, |
2727 | kgem->reloc[i].write_domain, |
2727 | kgem->reloc[i].write_domain, |
2728 | (int)kgem->reloc[i].presumed_offset); |
2728 | (int)kgem->reloc[i].presumed_offset); |
2729 | } |
2729 | } |
2730 | 2730 | ||
2731 | if (DEBUG_SYNC) { |
2731 | if (DEBUG_SYNC) { |
2732 | int fd = open("/tmp/batchbuffer", O_WRONLY | O_CREAT | O_APPEND, 0666); |
2732 | int fd = open("/tmp/batchbuffer", O_WRONLY | O_CREAT | O_APPEND, 0666); |
2733 | if (fd != -1) { |
2733 | if (fd != -1) { |
2734 | write(fd, kgem->batch, batch_end*sizeof(uint32_t)); |
2734 | write(fd, kgem->batch, batch_end*sizeof(uint32_t)); |
2735 | close(fd); |
2735 | close(fd); |
2736 | } |
2736 | } |
2737 | 2737 | ||
2738 | FatalError("SNA: failed to submit batchbuffer, errno=%d\n", ret); |
2738 | FatalError("SNA: failed to submit batchbuffer, errno=%d\n", ret); |
2739 | } |
2739 | } |
2740 | #endif |
2740 | #endif |
2741 | } |
2741 | } |
2742 | } |
2742 | } |
2743 | 2743 | ||
2744 | kgem_commit(kgem); |
2744 | kgem_commit(kgem); |
2745 | } |
2745 | } |
2746 | if (kgem->wedged) |
2746 | if (kgem->wedged) |
2747 | kgem_cleanup(kgem); |
2747 | kgem_cleanup(kgem); |
2748 | 2748 | ||
2749 | kgem_reset(kgem); |
2749 | kgem_reset(kgem); |
2750 | 2750 | ||
2751 | assert(kgem->next_request != NULL); |
2751 | assert(kgem->next_request != NULL); |
2752 | } |
2752 | } |
2753 | 2753 | ||
2754 | void kgem_throttle(struct kgem *kgem) |
2754 | void kgem_throttle(struct kgem *kgem) |
2755 | { |
2755 | { |
2756 | kgem->need_throttle = 0; |
2756 | kgem->need_throttle = 0; |
2757 | if (kgem->wedged) |
2757 | if (kgem->wedged) |
2758 | return; |
2758 | return; |
2759 | 2759 | ||
2760 | kgem->wedged = __kgem_throttle(kgem); |
2760 | kgem->wedged = __kgem_throttle(kgem); |
2761 | if (kgem->wedged) { |
2761 | if (kgem->wedged) { |
2762 | printf("Detected a hung GPU, disabling acceleration.\n"); |
2762 | printf("Detected a hung GPU, disabling acceleration.\n"); |
2763 | printf("When reporting this, please include i915_error_state from debugfs and the full dmesg.\n"); |
2763 | printf("When reporting this, please include i915_error_state from debugfs and the full dmesg.\n"); |
2764 | } |
2764 | } |
2765 | } |
2765 | } |
2766 | 2766 | ||
2767 | void kgem_purge_cache(struct kgem *kgem) |
2767 | void kgem_purge_cache(struct kgem *kgem) |
2768 | { |
2768 | { |
2769 | struct kgem_bo *bo, *next; |
2769 | struct kgem_bo *bo, *next; |
2770 | int i; |
2770 | int i; |
2771 | 2771 | ||
2772 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2772 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2773 | list_for_each_entry_safe(bo, next, &kgem->inactive[i], list) { |
2773 | list_for_each_entry_safe(bo, next, &kgem->inactive[i], list) { |
2774 | if (!kgem_bo_is_retained(kgem, bo)) { |
2774 | if (!kgem_bo_is_retained(kgem, bo)) { |
2775 | DBG(("%s: purging %d\n", |
2775 | DBG(("%s: purging %d\n", |
2776 | __FUNCTION__, bo->handle)); |
2776 | __FUNCTION__, bo->handle)); |
2777 | kgem_bo_free(kgem, bo); |
2777 | kgem_bo_free(kgem, bo); |
2778 | } |
2778 | } |
2779 | } |
2779 | } |
2780 | } |
2780 | } |
2781 | 2781 | ||
2782 | kgem->need_purge = false; |
2782 | kgem->need_purge = false; |
2783 | } |
2783 | } |
2784 | 2784 | ||
2785 | 2785 | ||
2786 | void kgem_clean_large_cache(struct kgem *kgem) |
2786 | void kgem_clean_large_cache(struct kgem *kgem) |
2787 | { |
2787 | { |
2788 | while (!list_is_empty(&kgem->large_inactive)) { |
2788 | while (!list_is_empty(&kgem->large_inactive)) { |
2789 | kgem_bo_free(kgem, |
2789 | kgem_bo_free(kgem, |
2790 | list_first_entry(&kgem->large_inactive, |
2790 | list_first_entry(&kgem->large_inactive, |
2791 | struct kgem_bo, list)); |
2791 | struct kgem_bo, list)); |
2792 | 2792 | ||
2793 | } |
2793 | } |
2794 | } |
2794 | } |
2795 | 2795 | ||
2796 | bool kgem_expire_cache(struct kgem *kgem) |
2796 | bool kgem_expire_cache(struct kgem *kgem) |
2797 | { |
2797 | { |
2798 | time_t now, expire; |
2798 | time_t now, expire; |
2799 | struct kgem_bo *bo; |
2799 | struct kgem_bo *bo; |
2800 | unsigned int size = 0, count = 0; |
2800 | unsigned int size = 0, count = 0; |
2801 | bool idle; |
2801 | bool idle; |
2802 | unsigned int i; |
2802 | unsigned int i; |
2803 | 2803 | ||
2804 | time(&now); |
2804 | time(&now); |
2805 | 2805 | ||
2806 | while (__kgem_freed_bo) { |
2806 | while (__kgem_freed_bo) { |
2807 | bo = __kgem_freed_bo; |
2807 | bo = __kgem_freed_bo; |
2808 | __kgem_freed_bo = *(struct kgem_bo **)bo; |
2808 | __kgem_freed_bo = *(struct kgem_bo **)bo; |
2809 | free(bo); |
2809 | free(bo); |
2810 | } |
2810 | } |
2811 | 2811 | ||
2812 | while (__kgem_freed_request) { |
2812 | while (__kgem_freed_request) { |
2813 | struct kgem_request *rq = __kgem_freed_request; |
2813 | struct kgem_request *rq = __kgem_freed_request; |
2814 | __kgem_freed_request = *(struct kgem_request **)rq; |
2814 | __kgem_freed_request = *(struct kgem_request **)rq; |
2815 | free(rq); |
2815 | free(rq); |
2816 | } |
2816 | } |
2817 | 2817 | ||
2818 | kgem_clean_large_cache(kgem); |
2818 | kgem_clean_large_cache(kgem); |
2819 | 2819 | ||
2820 | expire = 0; |
2820 | expire = 0; |
2821 | list_for_each_entry(bo, &kgem->snoop, list) { |
2821 | list_for_each_entry(bo, &kgem->snoop, list) { |
2822 | if (bo->delta) { |
2822 | if (bo->delta) { |
2823 | expire = now - MAX_INACTIVE_TIME/2; |
2823 | expire = now - MAX_INACTIVE_TIME/2; |
2824 | break; |
2824 | break; |
2825 | } |
2825 | } |
2826 | 2826 | ||
2827 | bo->delta = now; |
2827 | bo->delta = now; |
2828 | } |
2828 | } |
2829 | if (expire) { |
2829 | if (expire) { |
2830 | while (!list_is_empty(&kgem->snoop)) { |
2830 | while (!list_is_empty(&kgem->snoop)) { |
2831 | bo = list_last_entry(&kgem->snoop, struct kgem_bo, list); |
2831 | bo = list_last_entry(&kgem->snoop, struct kgem_bo, list); |
2832 | 2832 | ||
2833 | if (bo->delta > expire) |
2833 | if (bo->delta > expire) |
2834 | break; |
2834 | break; |
2835 | 2835 | ||
2836 | kgem_bo_free(kgem, bo); |
2836 | kgem_bo_free(kgem, bo); |
2837 | } |
2837 | } |
2838 | } |
2838 | } |
2839 | #ifdef DEBUG_MEMORY |
2839 | #ifdef DEBUG_MEMORY |
2840 | { |
2840 | { |
2841 | long snoop_size = 0; |
2841 | long snoop_size = 0; |
2842 | int snoop_count = 0; |
2842 | int snoop_count = 0; |
2843 | list_for_each_entry(bo, &kgem->snoop, list) |
2843 | list_for_each_entry(bo, &kgem->snoop, list) |
2844 | snoop_count++, snoop_size += bytes(bo); |
2844 | snoop_count++, snoop_size += bytes(bo); |
2845 | ErrorF("%s: still allocated %d bo, %ld bytes, in snoop cache\n", |
2845 | ErrorF("%s: still allocated %d bo, %ld bytes, in snoop cache\n", |
2846 | __FUNCTION__, snoop_count, snoop_size); |
2846 | __FUNCTION__, snoop_count, snoop_size); |
2847 | } |
2847 | } |
2848 | #endif |
2848 | #endif |
2849 | 2849 | ||
2850 | kgem_retire(kgem); |
2850 | kgem_retire(kgem); |
2851 | if (kgem->wedged) |
2851 | if (kgem->wedged) |
2852 | kgem_cleanup(kgem); |
2852 | kgem_cleanup(kgem); |
2853 | 2853 | ||
2854 | kgem->expire(kgem); |
2854 | kgem->expire(kgem); |
2855 | 2855 | ||
2856 | if (kgem->need_purge) |
2856 | if (kgem->need_purge) |
2857 | kgem_purge_cache(kgem); |
2857 | kgem_purge_cache(kgem); |
2858 | 2858 | ||
2859 | expire = 0; |
2859 | expire = 0; |
2860 | 2860 | ||
2861 | idle = !kgem->need_retire; |
2861 | idle = !kgem->need_retire; |
2862 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2862 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2863 | idle &= list_is_empty(&kgem->inactive[i]); |
2863 | idle &= list_is_empty(&kgem->inactive[i]); |
2864 | list_for_each_entry(bo, &kgem->inactive[i], list) { |
2864 | list_for_each_entry(bo, &kgem->inactive[i], list) { |
2865 | if (bo->delta) { |
2865 | if (bo->delta) { |
2866 | expire = now - MAX_INACTIVE_TIME; |
2866 | expire = now - MAX_INACTIVE_TIME; |
2867 | break; |
2867 | break; |
2868 | } |
2868 | } |
2869 | 2869 | ||
2870 | bo->delta = now; |
2870 | bo->delta = now; |
2871 | } |
2871 | } |
2872 | } |
2872 | } |
2873 | if (idle) { |
2873 | if (idle) { |
2874 | DBG(("%s: idle\n", __FUNCTION__)); |
2874 | DBG(("%s: idle\n", __FUNCTION__)); |
2875 | kgem->need_expire = false; |
2875 | kgem->need_expire = false; |
2876 | return false; |
2876 | return false; |
2877 | } |
2877 | } |
2878 | if (expire == 0) |
2878 | if (expire == 0) |
2879 | return true; |
2879 | return true; |
2880 | 2880 | ||
2881 | idle = !kgem->need_retire; |
2881 | idle = !kgem->need_retire; |
2882 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2882 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2883 | struct list preserve; |
2883 | struct list preserve; |
2884 | 2884 | ||
2885 | list_init(&preserve); |
2885 | list_init(&preserve); |
2886 | while (!list_is_empty(&kgem->inactive[i])) { |
2886 | while (!list_is_empty(&kgem->inactive[i])) { |
2887 | bo = list_last_entry(&kgem->inactive[i], |
2887 | bo = list_last_entry(&kgem->inactive[i], |
2888 | struct kgem_bo, list); |
2888 | struct kgem_bo, list); |
2889 | 2889 | ||
2890 | if (bo->delta > expire) { |
2890 | if (bo->delta > expire) { |
2891 | idle = false; |
2891 | idle = false; |
2892 | break; |
2892 | break; |
2893 | } |
2893 | } |
2894 | 2894 | ||
2895 | if (bo->map && bo->delta + MAP_PRESERVE_TIME > expire) { |
2895 | if (bo->map && bo->delta + MAP_PRESERVE_TIME > expire) { |
2896 | idle = false; |
2896 | idle = false; |
2897 | list_move_tail(&bo->list, &preserve); |
2897 | list_move_tail(&bo->list, &preserve); |
2898 | } else { |
2898 | } else { |
2899 | count++; |
2899 | count++; |
2900 | size += bytes(bo); |
2900 | size += bytes(bo); |
2901 | kgem_bo_free(kgem, bo); |
2901 | kgem_bo_free(kgem, bo); |
2902 | DBG(("%s: expiring %d\n", |
2902 | DBG(("%s: expiring %d\n", |
2903 | __FUNCTION__, bo->handle)); |
2903 | __FUNCTION__, bo->handle)); |
2904 | } |
2904 | } |
2905 | } |
2905 | } |
2906 | if (!list_is_empty(&preserve)) { |
2906 | if (!list_is_empty(&preserve)) { |
2907 | preserve.prev->next = kgem->inactive[i].next; |
2907 | preserve.prev->next = kgem->inactive[i].next; |
2908 | kgem->inactive[i].next->prev = preserve.prev; |
2908 | kgem->inactive[i].next->prev = preserve.prev; |
2909 | kgem->inactive[i].next = preserve.next; |
2909 | kgem->inactive[i].next = preserve.next; |
2910 | preserve.next->prev = &kgem->inactive[i]; |
2910 | preserve.next->prev = &kgem->inactive[i]; |
2911 | } |
2911 | } |
2912 | } |
2912 | } |
2913 | 2913 | ||
2914 | #ifdef DEBUG_MEMORY |
2914 | #ifdef DEBUG_MEMORY |
2915 | { |
2915 | { |
2916 | long inactive_size = 0; |
2916 | long inactive_size = 0; |
2917 | int inactive_count = 0; |
2917 | int inactive_count = 0; |
2918 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) |
2918 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) |
2919 | list_for_each_entry(bo, &kgem->inactive[i], list) |
2919 | list_for_each_entry(bo, &kgem->inactive[i], list) |
2920 | inactive_count++, inactive_size += bytes(bo); |
2920 | inactive_count++, inactive_size += bytes(bo); |
2921 | ErrorF("%s: still allocated %d bo, %ld bytes, in inactive cache\n", |
2921 | ErrorF("%s: still allocated %d bo, %ld bytes, in inactive cache\n", |
2922 | __FUNCTION__, inactive_count, inactive_size); |
2922 | __FUNCTION__, inactive_count, inactive_size); |
2923 | } |
2923 | } |
2924 | #endif |
2924 | #endif |
2925 | 2925 | ||
2926 | DBG(("%s: expired %d objects, %d bytes, idle? %d\n", |
2926 | DBG(("%s: expired %d objects, %d bytes, idle? %d\n", |
2927 | __FUNCTION__, count, size, idle)); |
2927 | __FUNCTION__, count, size, idle)); |
2928 | 2928 | ||
2929 | kgem->need_expire = !idle; |
2929 | kgem->need_expire = !idle; |
2930 | return !idle; |
2930 | return !idle; |
2931 | (void)count; |
2931 | (void)count; |
2932 | (void)size; |
2932 | (void)size; |
2933 | } |
2933 | } |
2934 | 2934 | ||
2935 | void kgem_cleanup_cache(struct kgem *kgem) |
2935 | void kgem_cleanup_cache(struct kgem *kgem) |
2936 | { |
2936 | { |
2937 | unsigned int i; |
2937 | unsigned int i; |
2938 | int n; |
2938 | int n; |
2939 | 2939 | ||
2940 | /* sync to the most recent request */ |
2940 | /* sync to the most recent request */ |
2941 | for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) { |
2941 | for (n = 0; n < ARRAY_SIZE(kgem->requests); n++) { |
2942 | if (!list_is_empty(&kgem->requests[n])) { |
2942 | if (!list_is_empty(&kgem->requests[n])) { |
2943 | struct kgem_request *rq; |
2943 | struct kgem_request *rq; |
2944 | struct drm_i915_gem_set_domain set_domain; |
2944 | struct drm_i915_gem_set_domain set_domain; |
2945 | 2945 | ||
2946 | rq = list_first_entry(&kgem->requests[n], |
2946 | rq = list_first_entry(&kgem->requests[n], |
2947 | struct kgem_request, |
2947 | struct kgem_request, |
2948 | list); |
2948 | list); |
2949 | 2949 | ||
2950 | DBG(("%s: sync on cleanup\n", __FUNCTION__)); |
2950 | DBG(("%s: sync on cleanup\n", __FUNCTION__)); |
2951 | 2951 | ||
2952 | VG_CLEAR(set_domain); |
2952 | VG_CLEAR(set_domain); |
2953 | set_domain.handle = rq->bo->handle; |
2953 | set_domain.handle = rq->bo->handle; |
2954 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2954 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
2955 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2955 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
2956 | (void)drmIoctl(kgem->fd, |
2956 | (void)drmIoctl(kgem->fd, |
2957 | DRM_IOCTL_I915_GEM_SET_DOMAIN, |
2957 | DRM_IOCTL_I915_GEM_SET_DOMAIN, |
2958 | &set_domain); |
2958 | &set_domain); |
2959 | } |
2959 | } |
2960 | } |
2960 | } |
2961 | 2961 | ||
2962 | kgem_retire(kgem); |
2962 | kgem_retire(kgem); |
2963 | kgem_cleanup(kgem); |
2963 | kgem_cleanup(kgem); |
2964 | 2964 | ||
2965 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2965 | for (i = 0; i < ARRAY_SIZE(kgem->inactive); i++) { |
2966 | while (!list_is_empty(&kgem->inactive[i])) |
2966 | while (!list_is_empty(&kgem->inactive[i])) |
2967 | kgem_bo_free(kgem, |
2967 | kgem_bo_free(kgem, |
2968 | list_last_entry(&kgem->inactive[i], |
2968 | list_last_entry(&kgem->inactive[i], |
2969 | struct kgem_bo, list)); |
2969 | struct kgem_bo, list)); |
2970 | } |
2970 | } |
2971 | 2971 | ||
2972 | kgem_clean_large_cache(kgem); |
2972 | kgem_clean_large_cache(kgem); |
2973 | 2973 | ||
2974 | while (!list_is_empty(&kgem->snoop)) |
2974 | while (!list_is_empty(&kgem->snoop)) |
2975 | kgem_bo_free(kgem, |
2975 | kgem_bo_free(kgem, |
2976 | list_last_entry(&kgem->snoop, |
2976 | list_last_entry(&kgem->snoop, |
2977 | struct kgem_bo, list)); |
2977 | struct kgem_bo, list)); |
2978 | 2978 | ||
2979 | while (__kgem_freed_bo) { |
2979 | while (__kgem_freed_bo) { |
2980 | struct kgem_bo *bo = __kgem_freed_bo; |
2980 | struct kgem_bo *bo = __kgem_freed_bo; |
2981 | __kgem_freed_bo = *(struct kgem_bo **)bo; |
2981 | __kgem_freed_bo = *(struct kgem_bo **)bo; |
2982 | free(bo); |
2982 | free(bo); |
2983 | } |
2983 | } |
2984 | 2984 | ||
2985 | kgem->need_purge = false; |
2985 | kgem->need_purge = false; |
2986 | kgem->need_expire = false; |
2986 | kgem->need_expire = false; |
2987 | } |
2987 | } |
2988 | 2988 | ||
2989 | static struct kgem_bo * |
2989 | static struct kgem_bo * |
2990 | search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags) |
2990 | search_linear_cache(struct kgem *kgem, unsigned int num_pages, unsigned flags) |
2991 | { |
2991 | { |
2992 | struct kgem_bo *bo, *first = NULL; |
2992 | struct kgem_bo *bo, *first = NULL; |
2993 | bool use_active = (flags & CREATE_INACTIVE) == 0; |
2993 | bool use_active = (flags & CREATE_INACTIVE) == 0; |
2994 | struct list *cache; |
2994 | struct list *cache; |
2995 | 2995 | ||
2996 | DBG(("%s: num_pages=%d, flags=%x, use_active? %d, use_large=%d [max=%d]\n", |
2996 | DBG(("%s: num_pages=%d, flags=%x, use_active? %d, use_large=%d [max=%d]\n", |
2997 | __FUNCTION__, num_pages, flags, use_active, |
2997 | __FUNCTION__, num_pages, flags, use_active, |
2998 | num_pages >= MAX_CACHE_SIZE / PAGE_SIZE, |
2998 | num_pages >= MAX_CACHE_SIZE / PAGE_SIZE, |
2999 | MAX_CACHE_SIZE / PAGE_SIZE)); |
2999 | MAX_CACHE_SIZE / PAGE_SIZE)); |
3000 | 3000 | ||
3001 | assert(num_pages); |
3001 | assert(num_pages); |
3002 | 3002 | ||
3003 | if (num_pages >= MAX_CACHE_SIZE / PAGE_SIZE) { |
3003 | if (num_pages >= MAX_CACHE_SIZE / PAGE_SIZE) { |
3004 | DBG(("%s: searching large buffers\n", __FUNCTION__)); |
3004 | DBG(("%s: searching large buffers\n", __FUNCTION__)); |
3005 | retry_large: |
3005 | retry_large: |
3006 | cache = use_active ? &kgem->large : &kgem->large_inactive; |
3006 | cache = use_active ? &kgem->large : &kgem->large_inactive; |
3007 | list_for_each_entry_safe(bo, first, cache, list) { |
3007 | list_for_each_entry_safe(bo, first, cache, list) { |
3008 | assert(bo->refcnt == 0); |
3008 | assert(bo->refcnt == 0); |
3009 | assert(bo->reusable); |
3009 | assert(bo->reusable); |
3010 | assert(!bo->scanout); |
3010 | assert(!bo->scanout); |
3011 | 3011 | ||
3012 | if (num_pages > num_pages(bo)) |
3012 | if (num_pages > num_pages(bo)) |
3013 | goto discard; |
3013 | goto discard; |
3014 | 3014 | ||
3015 | if (bo->tiling != I915_TILING_NONE) { |
3015 | if (bo->tiling != I915_TILING_NONE) { |
3016 | if (use_active) |
3016 | if (use_active) |
3017 | goto discard; |
3017 | goto discard; |
3018 | 3018 | ||
3019 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3019 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3020 | I915_TILING_NONE, 0)) |
3020 | I915_TILING_NONE, 0)) |
3021 | goto discard; |
3021 | goto discard; |
3022 | 3022 | ||
3023 | bo->tiling = I915_TILING_NONE; |
3023 | bo->tiling = I915_TILING_NONE; |
3024 | bo->pitch = 0; |
3024 | bo->pitch = 0; |
3025 | } |
3025 | } |
3026 | 3026 | ||
3027 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) |
3027 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) |
3028 | goto discard; |
3028 | goto discard; |
3029 | 3029 | ||
3030 | list_del(&bo->list); |
3030 | list_del(&bo->list); |
3031 | if (bo->rq == (void *)kgem) |
3031 | if (bo->rq == (void *)kgem) |
3032 | list_del(&bo->request); |
3032 | list_del(&bo->request); |
3033 | 3033 | ||
3034 | bo->delta = 0; |
3034 | bo->delta = 0; |
3035 | assert_tiling(kgem, bo); |
3035 | assert_tiling(kgem, bo); |
3036 | return bo; |
3036 | return bo; |
3037 | 3037 | ||
3038 | discard: |
3038 | discard: |
3039 | if (!use_active) |
3039 | if (!use_active) |
3040 | kgem_bo_free(kgem, bo); |
3040 | kgem_bo_free(kgem, bo); |
3041 | } |
3041 | } |
3042 | 3042 | ||
3043 | if (use_active) { |
3043 | if (use_active) { |
3044 | use_active = false; |
3044 | use_active = false; |
3045 | goto retry_large; |
3045 | goto retry_large; |
3046 | } |
3046 | } |
3047 | 3047 | ||
3048 | if (__kgem_throttle_retire(kgem, flags)) |
3048 | if (__kgem_throttle_retire(kgem, flags)) |
3049 | goto retry_large; |
3049 | goto retry_large; |
3050 | 3050 | ||
3051 | return NULL; |
3051 | return NULL; |
3052 | } |
3052 | } |
3053 | 3053 | ||
3054 | if (!use_active && list_is_empty(inactive(kgem, num_pages))) { |
3054 | if (!use_active && list_is_empty(inactive(kgem, num_pages))) { |
3055 | DBG(("%s: inactive and cache bucket empty\n", |
3055 | DBG(("%s: inactive and cache bucket empty\n", |
3056 | __FUNCTION__)); |
3056 | __FUNCTION__)); |
3057 | 3057 | ||
3058 | if (flags & CREATE_NO_RETIRE) { |
3058 | if (flags & CREATE_NO_RETIRE) { |
3059 | DBG(("%s: can not retire\n", __FUNCTION__)); |
3059 | DBG(("%s: can not retire\n", __FUNCTION__)); |
3060 | return NULL; |
3060 | return NULL; |
3061 | } |
3061 | } |
3062 | 3062 | ||
3063 | if (list_is_empty(active(kgem, num_pages, I915_TILING_NONE))) { |
3063 | if (list_is_empty(active(kgem, num_pages, I915_TILING_NONE))) { |
3064 | DBG(("%s: active cache bucket empty\n", __FUNCTION__)); |
3064 | DBG(("%s: active cache bucket empty\n", __FUNCTION__)); |
3065 | return NULL; |
3065 | return NULL; |
3066 | } |
3066 | } |
3067 | 3067 | ||
3068 | if (!__kgem_throttle_retire(kgem, flags)) { |
3068 | if (!__kgem_throttle_retire(kgem, flags)) { |
3069 | DBG(("%s: nothing retired\n", __FUNCTION__)); |
3069 | DBG(("%s: nothing retired\n", __FUNCTION__)); |
3070 | return NULL; |
3070 | return NULL; |
3071 | } |
3071 | } |
3072 | 3072 | ||
3073 | if (list_is_empty(inactive(kgem, num_pages))) { |
3073 | if (list_is_empty(inactive(kgem, num_pages))) { |
3074 | DBG(("%s: active cache bucket still empty after retire\n", |
3074 | DBG(("%s: active cache bucket still empty after retire\n", |
3075 | __FUNCTION__)); |
3075 | __FUNCTION__)); |
3076 | return NULL; |
3076 | return NULL; |
3077 | } |
3077 | } |
3078 | } |
3078 | } |
3079 | 3079 | ||
3080 | if (!use_active && flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3080 | if (!use_active && flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3081 | int for_cpu = !!(flags & CREATE_CPU_MAP); |
3081 | int for_cpu = !!(flags & CREATE_CPU_MAP); |
3082 | DBG(("%s: searching for inactive %s map\n", |
3082 | DBG(("%s: searching for inactive %s map\n", |
3083 | __FUNCTION__, for_cpu ? "cpu" : "gtt")); |
3083 | __FUNCTION__, for_cpu ? "cpu" : "gtt")); |
3084 | cache = &kgem->vma[for_cpu].inactive[cache_bucket(num_pages)]; |
3084 | cache = &kgem->vma[for_cpu].inactive[cache_bucket(num_pages)]; |
3085 | list_for_each_entry(bo, cache, vma) { |
3085 | list_for_each_entry(bo, cache, vma) { |
3086 | assert(IS_CPU_MAP(bo->map) == for_cpu); |
3086 | assert(IS_CPU_MAP(bo->map) == for_cpu); |
3087 | assert(bucket(bo) == cache_bucket(num_pages)); |
3087 | assert(bucket(bo) == cache_bucket(num_pages)); |
3088 | assert(bo->proxy == NULL); |
3088 | assert(bo->proxy == NULL); |
3089 | assert(bo->rq == NULL); |
3089 | assert(bo->rq == NULL); |
3090 | assert(bo->exec == NULL); |
3090 | assert(bo->exec == NULL); |
3091 | assert(!bo->scanout); |
3091 | assert(!bo->scanout); |
3092 | 3092 | ||
3093 | if (num_pages > num_pages(bo)) { |
3093 | if (num_pages > num_pages(bo)) { |
3094 | DBG(("inactive too small: %d < %d\n", |
3094 | DBG(("inactive too small: %d < %d\n", |
3095 | num_pages(bo), num_pages)); |
3095 | num_pages(bo), num_pages)); |
3096 | continue; |
3096 | continue; |
3097 | } |
3097 | } |
3098 | 3098 | ||
3099 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3099 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3100 | kgem_bo_free(kgem, bo); |
3100 | kgem_bo_free(kgem, bo); |
3101 | break; |
3101 | break; |
3102 | } |
3102 | } |
3103 | 3103 | ||
3104 | if (I915_TILING_NONE != bo->tiling && |
3104 | if (I915_TILING_NONE != bo->tiling && |
3105 | !gem_set_tiling(kgem->fd, bo->handle, |
3105 | !gem_set_tiling(kgem->fd, bo->handle, |
3106 | I915_TILING_NONE, 0)) |
3106 | I915_TILING_NONE, 0)) |
3107 | continue; |
3107 | continue; |
3108 | 3108 | ||
3109 | kgem_bo_remove_from_inactive(kgem, bo); |
3109 | kgem_bo_remove_from_inactive(kgem, bo); |
3110 | 3110 | ||
3111 | bo->tiling = I915_TILING_NONE; |
3111 | bo->tiling = I915_TILING_NONE; |
3112 | bo->pitch = 0; |
3112 | bo->pitch = 0; |
3113 | bo->delta = 0; |
3113 | bo->delta = 0; |
3114 | DBG((" %s: found handle=%d (num_pages=%d) in linear vma cache\n", |
3114 | DBG((" %s: found handle=%d (num_pages=%d) in linear vma cache\n", |
3115 | __FUNCTION__, bo->handle, num_pages(bo))); |
3115 | __FUNCTION__, bo->handle, num_pages(bo))); |
3116 | assert(use_active || bo->domain != DOMAIN_GPU); |
3116 | assert(use_active || bo->domain != DOMAIN_GPU); |
3117 | assert(!bo->needs_flush); |
3117 | assert(!bo->needs_flush); |
3118 | assert_tiling(kgem, bo); |
3118 | assert_tiling(kgem, bo); |
3119 | ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active); |
3119 | ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active); |
3120 | return bo; |
3120 | return bo; |
3121 | } |
3121 | } |
3122 | 3122 | ||
3123 | if (flags & CREATE_EXACT) |
3123 | if (flags & CREATE_EXACT) |
3124 | return NULL; |
3124 | return NULL; |
3125 | 3125 | ||
3126 | if (flags & CREATE_CPU_MAP && !kgem->has_llc) |
3126 | if (flags & CREATE_CPU_MAP && !kgem->has_llc) |
3127 | return NULL; |
3127 | return NULL; |
3128 | } |
3128 | } |
3129 | 3129 | ||
3130 | cache = use_active ? active(kgem, num_pages, I915_TILING_NONE) : inactive(kgem, num_pages); |
3130 | cache = use_active ? active(kgem, num_pages, I915_TILING_NONE) : inactive(kgem, num_pages); |
3131 | list_for_each_entry(bo, cache, list) { |
3131 | list_for_each_entry(bo, cache, list) { |
3132 | assert(bo->refcnt == 0); |
3132 | assert(bo->refcnt == 0); |
3133 | assert(bo->reusable); |
3133 | assert(bo->reusable); |
3134 | assert(!!bo->rq == !!use_active); |
3134 | assert(!!bo->rq == !!use_active); |
3135 | assert(bo->proxy == NULL); |
3135 | assert(bo->proxy == NULL); |
3136 | assert(!bo->scanout); |
3136 | assert(!bo->scanout); |
3137 | 3137 | ||
3138 | if (num_pages > num_pages(bo)) |
3138 | if (num_pages > num_pages(bo)) |
3139 | continue; |
3139 | continue; |
3140 | 3140 | ||
3141 | if (use_active && |
3141 | if (use_active && |
3142 | kgem->gen <= 040 && |
3142 | kgem->gen <= 040 && |
3143 | bo->tiling != I915_TILING_NONE) |
3143 | bo->tiling != I915_TILING_NONE) |
3144 | continue; |
3144 | continue; |
3145 | 3145 | ||
3146 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3146 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3147 | kgem_bo_free(kgem, bo); |
3147 | kgem_bo_free(kgem, bo); |
3148 | break; |
3148 | break; |
3149 | } |
3149 | } |
3150 | 3150 | ||
3151 | if (I915_TILING_NONE != bo->tiling) { |
3151 | if (I915_TILING_NONE != bo->tiling) { |
3152 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) |
3152 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) |
3153 | continue; |
3153 | continue; |
3154 | 3154 | ||
3155 | if (first) |
3155 | if (first) |
3156 | continue; |
3156 | continue; |
3157 | 3157 | ||
3158 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3158 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3159 | I915_TILING_NONE, 0)) |
3159 | I915_TILING_NONE, 0)) |
3160 | continue; |
3160 | continue; |
3161 | 3161 | ||
3162 | bo->tiling = I915_TILING_NONE; |
3162 | bo->tiling = I915_TILING_NONE; |
3163 | bo->pitch = 0; |
3163 | bo->pitch = 0; |
3164 | } |
3164 | } |
3165 | 3165 | ||
3166 | if (bo->map) { |
3166 | if (bo->map) { |
3167 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3167 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3168 | int for_cpu = !!(flags & CREATE_CPU_MAP); |
3168 | int for_cpu = !!(flags & CREATE_CPU_MAP); |
3169 | if (IS_CPU_MAP(bo->map) != for_cpu) { |
3169 | if (IS_CPU_MAP(bo->map) != for_cpu) { |
3170 | if (first != NULL) |
3170 | if (first != NULL) |
3171 | break; |
3171 | break; |
3172 | 3172 | ||
3173 | first = bo; |
3173 | first = bo; |
3174 | continue; |
3174 | continue; |
3175 | } |
3175 | } |
3176 | } else { |
3176 | } else { |
3177 | if (first != NULL) |
3177 | if (first != NULL) |
3178 | break; |
3178 | break; |
3179 | 3179 | ||
3180 | first = bo; |
3180 | first = bo; |
3181 | continue; |
3181 | continue; |
3182 | } |
3182 | } |
3183 | } else { |
3183 | } else { |
3184 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3184 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3185 | if (first != NULL) |
3185 | if (first != NULL) |
3186 | break; |
3186 | break; |
3187 | 3187 | ||
3188 | first = bo; |
3188 | first = bo; |
3189 | continue; |
3189 | continue; |
3190 | } |
3190 | } |
3191 | } |
3191 | } |
3192 | 3192 | ||
3193 | if (use_active) |
3193 | if (use_active) |
3194 | kgem_bo_remove_from_active(kgem, bo); |
3194 | kgem_bo_remove_from_active(kgem, bo); |
3195 | else |
3195 | else |
3196 | kgem_bo_remove_from_inactive(kgem, bo); |
3196 | kgem_bo_remove_from_inactive(kgem, bo); |
3197 | 3197 | ||
3198 | assert(bo->tiling == I915_TILING_NONE); |
3198 | assert(bo->tiling == I915_TILING_NONE); |
3199 | bo->pitch = 0; |
3199 | bo->pitch = 0; |
3200 | bo->delta = 0; |
3200 | bo->delta = 0; |
3201 | DBG((" %s: found handle=%d (num_pages=%d) in linear %s cache\n", |
3201 | DBG((" %s: found handle=%d (num_pages=%d) in linear %s cache\n", |
3202 | __FUNCTION__, bo->handle, num_pages(bo), |
3202 | __FUNCTION__, bo->handle, num_pages(bo), |
3203 | use_active ? "active" : "inactive")); |
3203 | use_active ? "active" : "inactive")); |
3204 | assert(list_is_empty(&bo->list)); |
3204 | assert(list_is_empty(&bo->list)); |
3205 | assert(use_active || bo->domain != DOMAIN_GPU); |
3205 | assert(use_active || bo->domain != DOMAIN_GPU); |
3206 | assert(!bo->needs_flush || use_active); |
3206 | assert(!bo->needs_flush || use_active); |
3207 | assert_tiling(kgem, bo); |
3207 | assert_tiling(kgem, bo); |
3208 | ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active); |
3208 | ASSERT_MAYBE_IDLE(kgem, bo->handle, !use_active); |
3209 | return bo; |
3209 | return bo; |
3210 | } |
3210 | } |
3211 | 3211 | ||
3212 | if (first) { |
3212 | if (first) { |
3213 | assert(first->tiling == I915_TILING_NONE); |
3213 | assert(first->tiling == I915_TILING_NONE); |
3214 | 3214 | ||
3215 | if (use_active) |
3215 | if (use_active) |
3216 | kgem_bo_remove_from_active(kgem, first); |
3216 | kgem_bo_remove_from_active(kgem, first); |
3217 | else |
3217 | else |
3218 | kgem_bo_remove_from_inactive(kgem, first); |
3218 | kgem_bo_remove_from_inactive(kgem, first); |
3219 | 3219 | ||
3220 | first->pitch = 0; |
3220 | first->pitch = 0; |
3221 | first->delta = 0; |
3221 | first->delta = 0; |
3222 | DBG((" %s: found handle=%d (near-miss) (num_pages=%d) in linear %s cache\n", |
3222 | DBG((" %s: found handle=%d (near-miss) (num_pages=%d) in linear %s cache\n", |
3223 | __FUNCTION__, first->handle, num_pages(first), |
3223 | __FUNCTION__, first->handle, num_pages(first), |
3224 | use_active ? "active" : "inactive")); |
3224 | use_active ? "active" : "inactive")); |
3225 | assert(list_is_empty(&first->list)); |
3225 | assert(list_is_empty(&first->list)); |
3226 | assert(use_active || first->domain != DOMAIN_GPU); |
3226 | assert(use_active || first->domain != DOMAIN_GPU); |
3227 | assert(!first->needs_flush || use_active); |
3227 | assert(!first->needs_flush || use_active); |
3228 | ASSERT_MAYBE_IDLE(kgem, first->handle, !use_active); |
3228 | ASSERT_MAYBE_IDLE(kgem, first->handle, !use_active); |
3229 | return first; |
3229 | return first; |
3230 | } |
3230 | } |
3231 | 3231 | ||
3232 | return NULL; |
3232 | return NULL; |
3233 | } |
3233 | } |
3234 | 3234 | ||
3235 | 3235 | ||
3236 | struct kgem_bo *kgem_create_linear(struct kgem *kgem, int size, unsigned flags) |
3236 | struct kgem_bo *kgem_create_linear(struct kgem *kgem, int size, unsigned flags) |
3237 | { |
3237 | { |
3238 | struct kgem_bo *bo; |
3238 | struct kgem_bo *bo; |
3239 | uint32_t handle; |
3239 | uint32_t handle; |
3240 | 3240 | ||
3241 | DBG(("%s(%d)\n", __FUNCTION__, size)); |
3241 | DBG(("%s(%d)\n", __FUNCTION__, size)); |
3242 | assert(size); |
3242 | assert(size); |
3243 | 3243 | ||
3244 | if (flags & CREATE_GTT_MAP && kgem->has_llc) { |
3244 | if (flags & CREATE_GTT_MAP && kgem->has_llc) { |
3245 | flags &= ~CREATE_GTT_MAP; |
3245 | flags &= ~CREATE_GTT_MAP; |
3246 | flags |= CREATE_CPU_MAP; |
3246 | flags |= CREATE_CPU_MAP; |
3247 | } |
3247 | } |
3248 | 3248 | ||
3249 | size = NUM_PAGES(size); |
3249 | size = NUM_PAGES(size); |
3250 | bo = search_linear_cache(kgem, size, CREATE_INACTIVE | flags); |
3250 | bo = search_linear_cache(kgem, size, CREATE_INACTIVE | flags); |
3251 | if (bo) { |
3251 | if (bo) { |
3252 | assert(bo->domain != DOMAIN_GPU); |
3252 | assert(bo->domain != DOMAIN_GPU); |
3253 | ASSERT_IDLE(kgem, bo->handle); |
3253 | ASSERT_IDLE(kgem, bo->handle); |
3254 | bo->refcnt = 1; |
3254 | bo->refcnt = 1; |
3255 | return bo; |
3255 | return bo; |
3256 | } |
3256 | } |
3257 | 3257 | ||
3258 | if (flags & CREATE_CACHED) |
3258 | if (flags & CREATE_CACHED) |
3259 | return NULL; |
3259 | return NULL; |
3260 | 3260 | ||
3261 | handle = gem_create(kgem->fd, size); |
3261 | handle = gem_create(kgem->fd, size); |
3262 | if (handle == 0) |
3262 | if (handle == 0) |
3263 | return NULL; |
3263 | return NULL; |
3264 | 3264 | ||
3265 | DBG(("%s: new handle=%d, num_pages=%d\n", __FUNCTION__, handle, size)); |
3265 | DBG(("%s: new handle=%d, num_pages=%d\n", __FUNCTION__, handle, size)); |
3266 | bo = __kgem_bo_alloc(handle, size); |
3266 | bo = __kgem_bo_alloc(handle, size); |
3267 | if (bo == NULL) { |
3267 | if (bo == NULL) { |
3268 | gem_close(kgem->fd, handle); |
3268 | gem_close(kgem->fd, handle); |
3269 | return NULL; |
3269 | return NULL; |
3270 | } |
3270 | } |
3271 | 3271 | ||
3272 | debug_alloc__bo(kgem, bo); |
3272 | debug_alloc__bo(kgem, bo); |
3273 | return bo; |
3273 | return bo; |
3274 | } |
3274 | } |
3275 | 3275 | ||
3276 | inline int kgem_bo_fenced_size(struct kgem *kgem, struct kgem_bo *bo) |
3276 | inline int kgem_bo_fenced_size(struct kgem *kgem, struct kgem_bo *bo) |
3277 | { |
3277 | { |
3278 | unsigned int size; |
3278 | unsigned int size; |
3279 | 3279 | ||
3280 | assert(bo->tiling); |
3280 | assert(bo->tiling); |
3281 | assert_tiling(kgem, bo); |
3281 | assert_tiling(kgem, bo); |
3282 | assert(kgem->gen < 040); |
3282 | assert(kgem->gen < 040); |
3283 | 3283 | ||
3284 | if (kgem->gen < 030) |
3284 | if (kgem->gen < 030) |
3285 | size = 512 * 1024; |
3285 | size = 512 * 1024; |
3286 | else |
3286 | else |
3287 | size = 1024 * 1024; |
3287 | size = 1024 * 1024; |
3288 | while (size < bytes(bo)) |
3288 | while (size < bytes(bo)) |
3289 | size *= 2; |
3289 | size *= 2; |
3290 | 3290 | ||
3291 | return size; |
3291 | return size; |
3292 | } |
3292 | } |
3293 | 3293 | ||
3294 | struct kgem_bo *kgem_create_2d(struct kgem *kgem, |
3294 | struct kgem_bo *kgem_create_2d(struct kgem *kgem, |
3295 | int width, |
3295 | int width, |
3296 | int height, |
3296 | int height, |
3297 | int bpp, |
3297 | int bpp, |
3298 | int tiling, |
3298 | int tiling, |
3299 | uint32_t flags) |
3299 | uint32_t flags) |
3300 | { |
3300 | { |
3301 | struct list *cache; |
3301 | struct list *cache; |
3302 | struct kgem_bo *bo; |
3302 | struct kgem_bo *bo; |
3303 | uint32_t pitch, tiled_height, size; |
3303 | uint32_t pitch, tiled_height, size; |
3304 | uint32_t handle; |
3304 | uint32_t handle; |
3305 | int i, bucket, retry; |
3305 | int i, bucket, retry; |
3306 | bool exact = flags & (CREATE_EXACT | CREATE_SCANOUT); |
3306 | bool exact = flags & (CREATE_EXACT | CREATE_SCANOUT); |
3307 | 3307 | ||
3308 | if (tiling < 0) |
3308 | if (tiling < 0) |
3309 | exact = true, tiling = -tiling; |
3309 | exact = true, tiling = -tiling; |
3310 | 3310 | ||
3311 | 3311 | ||
3312 | 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__, |
3312 | 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__, |
3313 | width, height, bpp, tiling, exact, |
3313 | width, height, bpp, tiling, exact, |
3314 | !!(flags & CREATE_INACTIVE), |
3314 | !!(flags & CREATE_INACTIVE), |
3315 | !!(flags & CREATE_CPU_MAP), |
3315 | !!(flags & CREATE_CPU_MAP), |
3316 | !!(flags & CREATE_GTT_MAP), |
3316 | !!(flags & CREATE_GTT_MAP), |
3317 | !!(flags & CREATE_SCANOUT), |
3317 | !!(flags & CREATE_SCANOUT), |
3318 | !!(flags & CREATE_PRIME), |
3318 | !!(flags & CREATE_PRIME), |
3319 | !!(flags & CREATE_TEMPORARY))); |
3319 | !!(flags & CREATE_TEMPORARY))); |
3320 | 3320 | ||
3321 | size = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags, |
3321 | size = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags, |
3322 | width, height, bpp, tiling, &pitch); |
3322 | width, height, bpp, tiling, &pitch); |
3323 | assert(size && size <= kgem->max_object_size); |
3323 | assert(size && size <= kgem->max_object_size); |
3324 | size /= PAGE_SIZE; |
3324 | size /= PAGE_SIZE; |
3325 | bucket = cache_bucket(size); |
3325 | bucket = cache_bucket(size); |
3326 | 3326 | ||
3327 | if (flags & CREATE_SCANOUT) { |
3327 | if (flags & CREATE_SCANOUT) { |
3328 | struct kgem_bo *last = NULL; |
3328 | struct kgem_bo *last = NULL; |
3329 | 3329 | ||
3330 | list_for_each_entry_reverse(bo, &kgem->scanout, list) { |
3330 | list_for_each_entry_reverse(bo, &kgem->scanout, list) { |
3331 | assert(bo->scanout); |
3331 | assert(bo->scanout); |
3332 | assert(bo->delta); |
3332 | assert(bo->delta); |
3333 | assert(!bo->flush); |
3333 | assert(!bo->flush); |
3334 | assert_tiling(kgem, bo); |
3334 | assert_tiling(kgem, bo); |
3335 | 3335 | ||
3336 | if (size > num_pages(bo) || num_pages(bo) > 2*size) |
3336 | if (size > num_pages(bo) || num_pages(bo) > 2*size) |
3337 | continue; |
3337 | continue; |
3338 | 3338 | ||
3339 | if (bo->tiling != tiling || |
3339 | if (bo->tiling != tiling || |
3340 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3340 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3341 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3341 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3342 | tiling, pitch)) |
3342 | tiling, pitch)) |
3343 | continue; |
3343 | continue; |
3344 | 3344 | ||
3345 | bo->tiling = tiling; |
3345 | bo->tiling = tiling; |
3346 | bo->pitch = pitch; |
3346 | bo->pitch = pitch; |
3347 | } |
3347 | } |
3348 | 3348 | ||
3349 | if (flags & CREATE_INACTIVE && bo->rq) { |
3349 | if (flags & CREATE_INACTIVE && bo->rq) { |
3350 | last = bo; |
3350 | last = bo; |
3351 | continue; |
3351 | continue; |
3352 | } |
3352 | } |
3353 | 3353 | ||
3354 | list_del(&bo->list); |
3354 | list_del(&bo->list); |
3355 | 3355 | ||
3356 | bo->unique_id = kgem_get_unique_id(kgem); |
3356 | bo->unique_id = kgem_get_unique_id(kgem); |
3357 | DBG((" 1:from scanout: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3357 | DBG((" 1:from scanout: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3358 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3358 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3359 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3359 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3360 | assert_tiling(kgem, bo); |
3360 | assert_tiling(kgem, bo); |
3361 | bo->refcnt = 1; |
3361 | bo->refcnt = 1; |
3362 | return bo; |
3362 | return bo; |
3363 | } |
3363 | } |
3364 | 3364 | ||
3365 | if (last) { |
3365 | if (last) { |
3366 | list_del(&last->list); |
3366 | list_del(&last->list); |
3367 | 3367 | ||
3368 | last->unique_id = kgem_get_unique_id(kgem); |
3368 | last->unique_id = kgem_get_unique_id(kgem); |
3369 | DBG((" 1:from scanout: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3369 | DBG((" 1:from scanout: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3370 | last->pitch, last->tiling, last->handle, last->unique_id)); |
3370 | last->pitch, last->tiling, last->handle, last->unique_id)); |
3371 | assert(last->pitch*kgem_aligned_height(kgem, height, last->tiling) <= kgem_bo_size(last)); |
3371 | assert(last->pitch*kgem_aligned_height(kgem, height, last->tiling) <= kgem_bo_size(last)); |
3372 | assert_tiling(kgem, last); |
3372 | assert_tiling(kgem, last); |
3373 | last->refcnt = 1; |
3373 | last->refcnt = 1; |
3374 | return last; |
3374 | return last; |
3375 | } |
3375 | } |
3376 | 3376 | ||
3377 | bo = NULL; //__kgem_bo_create_as_display(kgem, size, tiling, pitch); |
3377 | bo = NULL; //__kgem_bo_create_as_display(kgem, size, tiling, pitch); |
3378 | if (bo) |
3378 | if (bo) |
3379 | return bo; |
3379 | return bo; |
3380 | } |
3380 | } |
3381 | 3381 | ||
3382 | if (bucket >= NUM_CACHE_BUCKETS) { |
3382 | if (bucket >= NUM_CACHE_BUCKETS) { |
3383 | DBG(("%s: large bo num pages=%d, bucket=%d\n", |
3383 | DBG(("%s: large bo num pages=%d, bucket=%d\n", |
3384 | __FUNCTION__, size, bucket)); |
3384 | __FUNCTION__, size, bucket)); |
3385 | 3385 | ||
3386 | if (flags & CREATE_INACTIVE) |
3386 | if (flags & CREATE_INACTIVE) |
3387 | goto large_inactive; |
3387 | goto large_inactive; |
3388 | 3388 | ||
3389 | tiled_height = kgem_aligned_height(kgem, height, tiling); |
3389 | tiled_height = kgem_aligned_height(kgem, height, tiling); |
3390 | 3390 | ||
3391 | list_for_each_entry(bo, &kgem->large, list) { |
3391 | list_for_each_entry(bo, &kgem->large, list) { |
3392 | assert(!bo->purged); |
3392 | assert(!bo->purged); |
3393 | assert(!bo->scanout); |
3393 | assert(!bo->scanout); |
3394 | assert(bo->refcnt == 0); |
3394 | assert(bo->refcnt == 0); |
3395 | assert(bo->reusable); |
3395 | assert(bo->reusable); |
3396 | assert_tiling(kgem, bo); |
3396 | assert_tiling(kgem, bo); |
3397 | 3397 | ||
3398 | if (kgem->gen < 040) { |
3398 | if (kgem->gen < 040) { |
3399 | if (bo->pitch < pitch) { |
3399 | if (bo->pitch < pitch) { |
3400 | DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n", |
3400 | DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n", |
3401 | bo->tiling, tiling, |
3401 | bo->tiling, tiling, |
3402 | bo->pitch, pitch)); |
3402 | bo->pitch, pitch)); |
3403 | continue; |
3403 | continue; |
3404 | } |
3404 | } |
3405 | 3405 | ||
3406 | if (bo->pitch * tiled_height > bytes(bo)) |
3406 | if (bo->pitch * tiled_height > bytes(bo)) |
3407 | continue; |
3407 | continue; |
3408 | } else { |
3408 | } else { |
3409 | if (num_pages(bo) < size) |
3409 | if (num_pages(bo) < size) |
3410 | continue; |
3410 | continue; |
3411 | 3411 | ||
3412 | if (bo->pitch != pitch || bo->tiling != tiling) { |
3412 | if (bo->pitch != pitch || bo->tiling != tiling) { |
3413 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3413 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3414 | tiling, pitch)) |
3414 | tiling, pitch)) |
3415 | continue; |
3415 | continue; |
3416 | 3416 | ||
3417 | bo->pitch = pitch; |
3417 | bo->pitch = pitch; |
3418 | bo->tiling = tiling; |
3418 | bo->tiling = tiling; |
3419 | } |
3419 | } |
3420 | } |
3420 | } |
3421 | 3421 | ||
3422 | kgem_bo_remove_from_active(kgem, bo); |
3422 | kgem_bo_remove_from_active(kgem, bo); |
3423 | 3423 | ||
3424 | bo->unique_id = kgem_get_unique_id(kgem); |
3424 | bo->unique_id = kgem_get_unique_id(kgem); |
3425 | bo->delta = 0; |
3425 | bo->delta = 0; |
3426 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3426 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3427 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3427 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3428 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3428 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3429 | assert_tiling(kgem, bo); |
3429 | assert_tiling(kgem, bo); |
3430 | bo->refcnt = 1; |
3430 | bo->refcnt = 1; |
3431 | bo->flush = true; |
3431 | bo->flush = true; |
3432 | return bo; |
3432 | return bo; |
3433 | } |
3433 | } |
3434 | 3434 | ||
3435 | large_inactive: |
3435 | large_inactive: |
3436 | __kgem_throttle_retire(kgem, flags); |
3436 | __kgem_throttle_retire(kgem, flags); |
3437 | list_for_each_entry(bo, &kgem->large_inactive, list) { |
3437 | list_for_each_entry(bo, &kgem->large_inactive, list) { |
3438 | assert(bo->refcnt == 0); |
3438 | assert(bo->refcnt == 0); |
3439 | assert(bo->reusable); |
3439 | assert(bo->reusable); |
3440 | assert(!bo->scanout); |
3440 | assert(!bo->scanout); |
3441 | assert_tiling(kgem, bo); |
3441 | assert_tiling(kgem, bo); |
3442 | 3442 | ||
3443 | if (size > num_pages(bo)) |
3443 | if (size > num_pages(bo)) |
3444 | continue; |
3444 | continue; |
3445 | 3445 | ||
3446 | if (bo->tiling != tiling || |
3446 | if (bo->tiling != tiling || |
3447 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3447 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3448 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3448 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3449 | tiling, pitch)) |
3449 | tiling, pitch)) |
3450 | continue; |
3450 | continue; |
3451 | 3451 | ||
3452 | bo->tiling = tiling; |
3452 | bo->tiling = tiling; |
3453 | bo->pitch = pitch; |
3453 | bo->pitch = pitch; |
3454 | } |
3454 | } |
3455 | 3455 | ||
3456 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3456 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3457 | kgem_bo_free(kgem, bo); |
3457 | kgem_bo_free(kgem, bo); |
3458 | break; |
3458 | break; |
3459 | } |
3459 | } |
3460 | 3460 | ||
3461 | list_del(&bo->list); |
3461 | list_del(&bo->list); |
3462 | 3462 | ||
3463 | assert(bo->domain != DOMAIN_GPU); |
3463 | assert(bo->domain != DOMAIN_GPU); |
3464 | bo->unique_id = kgem_get_unique_id(kgem); |
3464 | bo->unique_id = kgem_get_unique_id(kgem); |
3465 | bo->pitch = pitch; |
3465 | bo->pitch = pitch; |
3466 | bo->delta = 0; |
3466 | bo->delta = 0; |
3467 | DBG((" 1:from large inactive: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3467 | DBG((" 1:from large inactive: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3468 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3468 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3469 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3469 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3470 | assert_tiling(kgem, bo); |
3470 | assert_tiling(kgem, bo); |
3471 | bo->refcnt = 1; |
3471 | bo->refcnt = 1; |
3472 | return bo; |
3472 | return bo; |
3473 | } |
3473 | } |
3474 | 3474 | ||
3475 | goto create; |
3475 | goto create; |
3476 | } |
3476 | } |
3477 | 3477 | ||
3478 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3478 | if (flags & (CREATE_CPU_MAP | CREATE_GTT_MAP)) { |
3479 | int for_cpu = !!(flags & CREATE_CPU_MAP); |
3479 | int for_cpu = !!(flags & CREATE_CPU_MAP); |
3480 | if (kgem->has_llc && tiling == I915_TILING_NONE) |
3480 | if (kgem->has_llc && tiling == I915_TILING_NONE) |
3481 | for_cpu = 1; |
3481 | for_cpu = 1; |
3482 | /* We presume that we will need to upload to this bo, |
3482 | /* We presume that we will need to upload to this bo, |
3483 | * and so would prefer to have an active VMA. |
3483 | * and so would prefer to have an active VMA. |
3484 | */ |
3484 | */ |
3485 | cache = &kgem->vma[for_cpu].inactive[bucket]; |
3485 | cache = &kgem->vma[for_cpu].inactive[bucket]; |
3486 | do { |
3486 | do { |
3487 | list_for_each_entry(bo, cache, vma) { |
3487 | list_for_each_entry(bo, cache, vma) { |
3488 | assert(bucket(bo) == bucket); |
3488 | assert(bucket(bo) == bucket); |
3489 | assert(bo->refcnt == 0); |
3489 | assert(bo->refcnt == 0); |
3490 | assert(!bo->scanout); |
3490 | assert(!bo->scanout); |
3491 | assert(bo->map); |
3491 | assert(bo->map); |
3492 | assert(IS_CPU_MAP(bo->map) == for_cpu); |
3492 | assert(IS_CPU_MAP(bo->map) == for_cpu); |
3493 | assert(bo->rq == NULL); |
3493 | assert(bo->rq == NULL); |
3494 | assert(list_is_empty(&bo->request)); |
3494 | assert(list_is_empty(&bo->request)); |
3495 | assert(bo->flush == false); |
3495 | assert(bo->flush == false); |
3496 | assert_tiling(kgem, bo); |
3496 | assert_tiling(kgem, bo); |
3497 | 3497 | ||
3498 | if (size > num_pages(bo)) { |
3498 | if (size > num_pages(bo)) { |
3499 | DBG(("inactive too small: %d < %d\n", |
3499 | DBG(("inactive too small: %d < %d\n", |
3500 | num_pages(bo), size)); |
3500 | num_pages(bo), size)); |
3501 | continue; |
3501 | continue; |
3502 | } |
3502 | } |
3503 | 3503 | ||
3504 | if (bo->tiling != tiling || |
3504 | if (bo->tiling != tiling || |
3505 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3505 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3506 | DBG(("inactive vma with wrong tiling: %d < %d\n", |
3506 | DBG(("inactive vma with wrong tiling: %d < %d\n", |
3507 | bo->tiling, tiling)); |
3507 | bo->tiling, tiling)); |
3508 | continue; |
3508 | continue; |
3509 | } |
3509 | } |
3510 | 3510 | ||
3511 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3511 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3512 | kgem_bo_free(kgem, bo); |
3512 | kgem_bo_free(kgem, bo); |
3513 | break; |
3513 | break; |
3514 | } |
3514 | } |
3515 | 3515 | ||
3516 | assert(bo->tiling == tiling); |
3516 | assert(bo->tiling == tiling); |
3517 | bo->pitch = pitch; |
3517 | bo->pitch = pitch; |
3518 | bo->delta = 0; |
3518 | bo->delta = 0; |
3519 | bo->unique_id = kgem_get_unique_id(kgem); |
3519 | bo->unique_id = kgem_get_unique_id(kgem); |
3520 | bo->domain = DOMAIN_NONE; |
3520 | bo->domain = DOMAIN_NONE; |
3521 | 3521 | ||
3522 | kgem_bo_remove_from_inactive(kgem, bo); |
3522 | kgem_bo_remove_from_inactive(kgem, bo); |
3523 | 3523 | ||
3524 | DBG((" from inactive vma: pitch=%d, tiling=%d: handle=%d, id=%d\n", |
3524 | DBG((" from inactive vma: pitch=%d, tiling=%d: handle=%d, id=%d\n", |
3525 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3525 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3526 | assert(bo->reusable); |
3526 | assert(bo->reusable); |
3527 | assert(bo->domain != DOMAIN_GPU); |
3527 | assert(bo->domain != DOMAIN_GPU); |
3528 | ASSERT_IDLE(kgem, bo->handle); |
3528 | ASSERT_IDLE(kgem, bo->handle); |
3529 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3529 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3530 | assert_tiling(kgem, bo); |
3530 | assert_tiling(kgem, bo); |
3531 | bo->refcnt = 1; |
3531 | bo->refcnt = 1; |
3532 | return bo; |
3532 | return bo; |
3533 | } |
3533 | } |
3534 | } while (!list_is_empty(cache) && |
3534 | } while (!list_is_empty(cache) && |
3535 | __kgem_throttle_retire(kgem, flags)); |
3535 | __kgem_throttle_retire(kgem, flags)); |
3536 | 3536 | ||
3537 | if (flags & CREATE_CPU_MAP && !kgem->has_llc) { |
3537 | if (flags & CREATE_CPU_MAP && !kgem->has_llc) { |
3538 | if (list_is_empty(&kgem->active[bucket][tiling]) && |
3538 | if (list_is_empty(&kgem->active[bucket][tiling]) && |
3539 | list_is_empty(&kgem->inactive[bucket])) |
3539 | list_is_empty(&kgem->inactive[bucket])) |
3540 | flags &= ~CREATE_CACHED; |
3540 | flags &= ~CREATE_CACHED; |
3541 | 3541 | ||
3542 | goto create; |
3542 | goto create; |
3543 | } |
3543 | } |
3544 | } |
3544 | } |
3545 | 3545 | ||
3546 | if (flags & CREATE_INACTIVE) |
3546 | if (flags & CREATE_INACTIVE) |
3547 | goto skip_active_search; |
3547 | goto skip_active_search; |
3548 | 3548 | ||
3549 | /* Best active match */ |
3549 | /* Best active match */ |
3550 | retry = NUM_CACHE_BUCKETS - bucket; |
3550 | retry = NUM_CACHE_BUCKETS - bucket; |
3551 | if (retry > 3 && (flags & CREATE_TEMPORARY) == 0) |
3551 | if (retry > 3 && (flags & CREATE_TEMPORARY) == 0) |
3552 | retry = 3; |
3552 | retry = 3; |
3553 | search_again: |
3553 | search_again: |
3554 | assert(bucket < NUM_CACHE_BUCKETS); |
3554 | assert(bucket < NUM_CACHE_BUCKETS); |
3555 | cache = &kgem->active[bucket][tiling]; |
3555 | cache = &kgem->active[bucket][tiling]; |
3556 | if (tiling) { |
3556 | if (tiling) { |
3557 | tiled_height = kgem_aligned_height(kgem, height, tiling); |
3557 | tiled_height = kgem_aligned_height(kgem, height, tiling); |
3558 | list_for_each_entry(bo, cache, list) { |
3558 | list_for_each_entry(bo, cache, list) { |
3559 | assert(!bo->purged); |
3559 | assert(!bo->purged); |
3560 | assert(bo->refcnt == 0); |
3560 | assert(bo->refcnt == 0); |
3561 | assert(bucket(bo) == bucket); |
3561 | assert(bucket(bo) == bucket); |
3562 | assert(bo->reusable); |
3562 | assert(bo->reusable); |
3563 | assert(bo->tiling == tiling); |
3563 | assert(bo->tiling == tiling); |
3564 | assert(bo->flush == false); |
3564 | assert(bo->flush == false); |
3565 | assert(!bo->scanout); |
3565 | assert(!bo->scanout); |
3566 | assert_tiling(kgem, bo); |
3566 | assert_tiling(kgem, bo); |
3567 | 3567 | ||
3568 | if (kgem->gen < 040) { |
3568 | if (kgem->gen < 040) { |
3569 | if (bo->pitch < pitch) { |
3569 | if (bo->pitch < pitch) { |
3570 | DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n", |
3570 | DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n", |
3571 | bo->tiling, tiling, |
3571 | bo->tiling, tiling, |
3572 | bo->pitch, pitch)); |
3572 | bo->pitch, pitch)); |
3573 | continue; |
3573 | continue; |
3574 | } |
3574 | } |
3575 | 3575 | ||
3576 | if (bo->pitch * tiled_height > bytes(bo)) |
3576 | if (bo->pitch * tiled_height > bytes(bo)) |
3577 | continue; |
3577 | continue; |
3578 | } else { |
3578 | } else { |
3579 | if (num_pages(bo) < size) |
3579 | if (num_pages(bo) < size) |
3580 | continue; |
3580 | continue; |
3581 | 3581 | ||
3582 | if (bo->pitch != pitch) { |
3582 | if (bo->pitch != pitch) { |
3583 | if (!gem_set_tiling(kgem->fd, |
3583 | if (!gem_set_tiling(kgem->fd, |
3584 | bo->handle, |
3584 | bo->handle, |
3585 | tiling, pitch)) |
3585 | tiling, pitch)) |
3586 | continue; |
3586 | continue; |
3587 | 3587 | ||
3588 | bo->pitch = pitch; |
3588 | bo->pitch = pitch; |
3589 | } |
3589 | } |
3590 | } |
3590 | } |
3591 | 3591 | ||
3592 | kgem_bo_remove_from_active(kgem, bo); |
3592 | kgem_bo_remove_from_active(kgem, bo); |
3593 | 3593 | ||
3594 | bo->unique_id = kgem_get_unique_id(kgem); |
3594 | bo->unique_id = kgem_get_unique_id(kgem); |
3595 | bo->delta = 0; |
3595 | bo->delta = 0; |
3596 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3596 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3597 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3597 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3598 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3598 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3599 | assert_tiling(kgem, bo); |
3599 | assert_tiling(kgem, bo); |
3600 | bo->refcnt = 1; |
3600 | bo->refcnt = 1; |
3601 | return bo; |
3601 | return bo; |
3602 | } |
3602 | } |
3603 | } else { |
3603 | } else { |
3604 | list_for_each_entry(bo, cache, list) { |
3604 | list_for_each_entry(bo, cache, list) { |
3605 | assert(bucket(bo) == bucket); |
3605 | assert(bucket(bo) == bucket); |
3606 | assert(!bo->purged); |
3606 | assert(!bo->purged); |
3607 | assert(bo->refcnt == 0); |
3607 | assert(bo->refcnt == 0); |
3608 | assert(bo->reusable); |
3608 | assert(bo->reusable); |
3609 | assert(!bo->scanout); |
3609 | assert(!bo->scanout); |
3610 | assert(bo->tiling == tiling); |
3610 | assert(bo->tiling == tiling); |
3611 | assert(bo->flush == false); |
3611 | assert(bo->flush == false); |
3612 | assert_tiling(kgem, bo); |
3612 | assert_tiling(kgem, bo); |
3613 | 3613 | ||
3614 | if (num_pages(bo) < size) |
3614 | if (num_pages(bo) < size) |
3615 | continue; |
3615 | continue; |
3616 | 3616 | ||
3617 | kgem_bo_remove_from_active(kgem, bo); |
3617 | kgem_bo_remove_from_active(kgem, bo); |
3618 | 3618 | ||
3619 | bo->pitch = pitch; |
3619 | bo->pitch = pitch; |
3620 | bo->unique_id = kgem_get_unique_id(kgem); |
3620 | bo->unique_id = kgem_get_unique_id(kgem); |
3621 | bo->delta = 0; |
3621 | bo->delta = 0; |
3622 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3622 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3623 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3623 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3624 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3624 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3625 | assert_tiling(kgem, bo); |
3625 | assert_tiling(kgem, bo); |
3626 | bo->refcnt = 1; |
3626 | bo->refcnt = 1; |
3627 | return bo; |
3627 | return bo; |
3628 | } |
3628 | } |
3629 | } |
3629 | } |
3630 | 3630 | ||
3631 | if (--retry && exact) { |
3631 | if (--retry && exact) { |
3632 | if (kgem->gen >= 040) { |
3632 | if (kgem->gen >= 040) { |
3633 | for (i = I915_TILING_NONE; i <= I915_TILING_Y; i++) { |
3633 | for (i = I915_TILING_NONE; i <= I915_TILING_Y; i++) { |
3634 | if (i == tiling) |
3634 | if (i == tiling) |
3635 | continue; |
3635 | continue; |
3636 | 3636 | ||
3637 | cache = &kgem->active[bucket][i]; |
3637 | cache = &kgem->active[bucket][i]; |
3638 | list_for_each_entry(bo, cache, list) { |
3638 | list_for_each_entry(bo, cache, list) { |
3639 | assert(!bo->purged); |
3639 | assert(!bo->purged); |
3640 | assert(bo->refcnt == 0); |
3640 | assert(bo->refcnt == 0); |
3641 | assert(bo->reusable); |
3641 | assert(bo->reusable); |
3642 | assert(!bo->scanout); |
3642 | assert(!bo->scanout); |
3643 | assert(bo->flush == false); |
3643 | assert(bo->flush == false); |
3644 | assert_tiling(kgem, bo); |
3644 | assert_tiling(kgem, bo); |
3645 | 3645 | ||
3646 | if (num_pages(bo) < size) |
3646 | if (num_pages(bo) < size) |
3647 | continue; |
3647 | continue; |
3648 | 3648 | ||
3649 | if (!gem_set_tiling(kgem->fd, |
3649 | if (!gem_set_tiling(kgem->fd, |
3650 | bo->handle, |
3650 | bo->handle, |
3651 | tiling, pitch)) |
3651 | tiling, pitch)) |
3652 | continue; |
3652 | continue; |
3653 | 3653 | ||
3654 | kgem_bo_remove_from_active(kgem, bo); |
3654 | kgem_bo_remove_from_active(kgem, bo); |
3655 | 3655 | ||
3656 | bo->unique_id = kgem_get_unique_id(kgem); |
3656 | bo->unique_id = kgem_get_unique_id(kgem); |
3657 | bo->pitch = pitch; |
3657 | bo->pitch = pitch; |
3658 | bo->tiling = tiling; |
3658 | bo->tiling = tiling; |
3659 | bo->delta = 0; |
3659 | bo->delta = 0; |
3660 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3660 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3661 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3661 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3662 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3662 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3663 | assert_tiling(kgem, bo); |
3663 | assert_tiling(kgem, bo); |
3664 | bo->refcnt = 1; |
3664 | bo->refcnt = 1; |
3665 | return bo; |
3665 | return bo; |
3666 | } |
3666 | } |
3667 | } |
3667 | } |
3668 | } |
3668 | } |
3669 | 3669 | ||
3670 | bucket++; |
3670 | bucket++; |
3671 | goto search_again; |
3671 | goto search_again; |
3672 | } |
3672 | } |
3673 | 3673 | ||
3674 | if (!exact) { /* allow an active near-miss? */ |
3674 | if (!exact) { /* allow an active near-miss? */ |
3675 | i = tiling; |
3675 | i = tiling; |
3676 | while (--i >= 0) { |
3676 | while (--i >= 0) { |
3677 | tiled_height = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags, |
3677 | tiled_height = kgem_surface_size(kgem, kgem->has_relaxed_fencing, flags, |
3678 | width, height, bpp, tiling, &pitch); |
3678 | width, height, bpp, tiling, &pitch); |
3679 | cache = active(kgem, tiled_height / PAGE_SIZE, i); |
3679 | cache = active(kgem, tiled_height / PAGE_SIZE, i); |
3680 | tiled_height = kgem_aligned_height(kgem, height, i); |
3680 | tiled_height = kgem_aligned_height(kgem, height, i); |
3681 | list_for_each_entry(bo, cache, list) { |
3681 | list_for_each_entry(bo, cache, list) { |
3682 | assert(!bo->purged); |
3682 | assert(!bo->purged); |
3683 | assert(bo->refcnt == 0); |
3683 | assert(bo->refcnt == 0); |
3684 | assert(bo->reusable); |
3684 | assert(bo->reusable); |
3685 | assert(!bo->scanout); |
3685 | assert(!bo->scanout); |
3686 | assert(bo->flush == false); |
3686 | assert(bo->flush == false); |
3687 | assert_tiling(kgem, bo); |
3687 | assert_tiling(kgem, bo); |
3688 | 3688 | ||
3689 | if (bo->tiling) { |
3689 | if (bo->tiling) { |
3690 | if (bo->pitch < pitch) { |
3690 | if (bo->pitch < pitch) { |
3691 | DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n", |
3691 | DBG(("tiled and pitch too small: tiling=%d, (want %d), pitch=%d, need %d\n", |
3692 | bo->tiling, tiling, |
3692 | bo->tiling, tiling, |
3693 | bo->pitch, pitch)); |
3693 | bo->pitch, pitch)); |
3694 | continue; |
3694 | continue; |
3695 | } |
3695 | } |
3696 | } else |
3696 | } else |
3697 | bo->pitch = pitch; |
3697 | bo->pitch = pitch; |
3698 | 3698 | ||
3699 | if (bo->pitch * tiled_height > bytes(bo)) |
3699 | if (bo->pitch * tiled_height > bytes(bo)) |
3700 | continue; |
3700 | continue; |
3701 | 3701 | ||
3702 | kgem_bo_remove_from_active(kgem, bo); |
3702 | kgem_bo_remove_from_active(kgem, bo); |
3703 | 3703 | ||
3704 | bo->unique_id = kgem_get_unique_id(kgem); |
3704 | bo->unique_id = kgem_get_unique_id(kgem); |
3705 | bo->delta = 0; |
3705 | bo->delta = 0; |
3706 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3706 | DBG((" 1:from active: pitch=%d, tiling=%d, handle=%d, id=%d\n", |
3707 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3707 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3708 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3708 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3709 | assert_tiling(kgem, bo); |
3709 | assert_tiling(kgem, bo); |
3710 | bo->refcnt = 1; |
3710 | bo->refcnt = 1; |
3711 | return bo; |
3711 | return bo; |
3712 | } |
3712 | } |
3713 | } |
3713 | } |
3714 | } |
3714 | } |
3715 | 3715 | ||
3716 | skip_active_search: |
3716 | skip_active_search: |
3717 | bucket = cache_bucket(size); |
3717 | bucket = cache_bucket(size); |
3718 | retry = NUM_CACHE_BUCKETS - bucket; |
3718 | retry = NUM_CACHE_BUCKETS - bucket; |
3719 | if (retry > 3) |
3719 | if (retry > 3) |
3720 | retry = 3; |
3720 | retry = 3; |
3721 | search_inactive: |
3721 | search_inactive: |
3722 | /* Now just look for a close match and prefer any currently active */ |
3722 | /* Now just look for a close match and prefer any currently active */ |
3723 | assert(bucket < NUM_CACHE_BUCKETS); |
3723 | assert(bucket < NUM_CACHE_BUCKETS); |
3724 | cache = &kgem->inactive[bucket]; |
3724 | cache = &kgem->inactive[bucket]; |
3725 | list_for_each_entry(bo, cache, list) { |
3725 | list_for_each_entry(bo, cache, list) { |
3726 | assert(bucket(bo) == bucket); |
3726 | assert(bucket(bo) == bucket); |
3727 | assert(bo->reusable); |
3727 | assert(bo->reusable); |
3728 | assert(!bo->scanout); |
3728 | assert(!bo->scanout); |
3729 | assert(bo->flush == false); |
3729 | assert(bo->flush == false); |
3730 | assert_tiling(kgem, bo); |
3730 | assert_tiling(kgem, bo); |
3731 | 3731 | ||
3732 | if (size > num_pages(bo)) { |
3732 | if (size > num_pages(bo)) { |
3733 | DBG(("inactive too small: %d < %d\n", |
3733 | DBG(("inactive too small: %d < %d\n", |
3734 | num_pages(bo), size)); |
3734 | num_pages(bo), size)); |
3735 | continue; |
3735 | continue; |
3736 | } |
3736 | } |
3737 | 3737 | ||
3738 | if (bo->tiling != tiling || |
3738 | if (bo->tiling != tiling || |
3739 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3739 | (tiling != I915_TILING_NONE && bo->pitch != pitch)) { |
3740 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3740 | if (!gem_set_tiling(kgem->fd, bo->handle, |
3741 | tiling, pitch)) |
3741 | tiling, pitch)) |
3742 | continue; |
3742 | continue; |
3743 | 3743 | ||
3744 | if (bo->map) |
3744 | if (bo->map) |
3745 | kgem_bo_release_map(kgem, bo); |
3745 | kgem_bo_release_map(kgem, bo); |
3746 | } |
3746 | } |
3747 | 3747 | ||
3748 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3748 | if (bo->purged && !kgem_bo_clear_purgeable(kgem, bo)) { |
3749 | kgem_bo_free(kgem, bo); |
3749 | kgem_bo_free(kgem, bo); |
3750 | break; |
3750 | break; |
3751 | } |
3751 | } |
3752 | 3752 | ||
3753 | kgem_bo_remove_from_inactive(kgem, bo); |
3753 | kgem_bo_remove_from_inactive(kgem, bo); |
3754 | 3754 | ||
3755 | bo->pitch = pitch; |
3755 | bo->pitch = pitch; |
3756 | bo->tiling = tiling; |
3756 | bo->tiling = tiling; |
3757 | 3757 | ||
3758 | bo->delta = 0; |
3758 | bo->delta = 0; |
3759 | bo->unique_id = kgem_get_unique_id(kgem); |
3759 | bo->unique_id = kgem_get_unique_id(kgem); |
3760 | assert(bo->pitch); |
3760 | assert(bo->pitch); |
3761 | DBG((" from inactive: pitch=%d, tiling=%d: handle=%d, id=%d\n", |
3761 | DBG((" from inactive: pitch=%d, tiling=%d: handle=%d, id=%d\n", |
3762 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3762 | bo->pitch, bo->tiling, bo->handle, bo->unique_id)); |
3763 | assert(bo->refcnt == 0); |
3763 | assert(bo->refcnt == 0); |
3764 | assert(bo->reusable); |
3764 | assert(bo->reusable); |
3765 | assert((flags & CREATE_INACTIVE) == 0 || bo->domain != DOMAIN_GPU); |
3765 | assert((flags & CREATE_INACTIVE) == 0 || bo->domain != DOMAIN_GPU); |
3766 | ASSERT_MAYBE_IDLE(kgem, bo->handle, flags & CREATE_INACTIVE); |
3766 | ASSERT_MAYBE_IDLE(kgem, bo->handle, flags & CREATE_INACTIVE); |
3767 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3767 | assert(bo->pitch*kgem_aligned_height(kgem, height, bo->tiling) <= kgem_bo_size(bo)); |
3768 | assert_tiling(kgem, bo); |
3768 | assert_tiling(kgem, bo); |
3769 | bo->refcnt = 1; |
3769 | bo->refcnt = 1; |
3770 | return bo; |
3770 | return bo; |
3771 | } |
3771 | } |
3772 | 3772 | ||
3773 | if (flags & CREATE_INACTIVE && |
3773 | if (flags & CREATE_INACTIVE && |
3774 | !list_is_empty(&kgem->active[bucket][tiling]) && |
3774 | !list_is_empty(&kgem->active[bucket][tiling]) && |
3775 | __kgem_throttle_retire(kgem, flags)) { |
3775 | __kgem_throttle_retire(kgem, flags)) { |
3776 | flags &= ~CREATE_INACTIVE; |
3776 | flags &= ~CREATE_INACTIVE; |
3777 | goto search_inactive; |
3777 | goto search_inactive; |
3778 | } |
3778 | } |
3779 | 3779 | ||
3780 | if (--retry) { |
3780 | if (--retry) { |
3781 | bucket++; |
3781 | bucket++; |
3782 | flags &= ~CREATE_INACTIVE; |
3782 | flags &= ~CREATE_INACTIVE; |
3783 | goto search_inactive; |
3783 | goto search_inactive; |
3784 | } |
3784 | } |
3785 | 3785 | ||
3786 | create: |
3786 | create: |
3787 | if (flags & CREATE_CACHED) |
3787 | if (flags & CREATE_CACHED) |
3788 | return NULL; |
3788 | return NULL; |
3789 | 3789 | ||
3790 | if (bucket >= NUM_CACHE_BUCKETS) |
3790 | if (bucket >= NUM_CACHE_BUCKETS) |
3791 | size = ALIGN(size, 1024); |
3791 | size = ALIGN(size, 1024); |
3792 | handle = gem_create(kgem->fd, size); |
3792 | handle = gem_create(kgem->fd, size); |
3793 | if (handle == 0) |
3793 | if (handle == 0) |
3794 | return NULL; |
3794 | return NULL; |
3795 | 3795 | ||
3796 | bo = __kgem_bo_alloc(handle, size); |
3796 | bo = __kgem_bo_alloc(handle, size); |
3797 | if (!bo) { |
3797 | if (!bo) { |
3798 | gem_close(kgem->fd, handle); |
3798 | gem_close(kgem->fd, handle); |
3799 | return NULL; |
3799 | return NULL; |
3800 | } |
3800 | } |
3801 | 3801 | ||
3802 | if (bucket >= NUM_CACHE_BUCKETS) { |
3802 | if (bucket >= NUM_CACHE_BUCKETS) { |
3803 | DBG(("%s: marking large bo for automatic flushing\n", |
3803 | DBG(("%s: marking large bo for automatic flushing\n", |
3804 | __FUNCTION__)); |
3804 | __FUNCTION__)); |
3805 | bo->flush = true; |
3805 | bo->flush = true; |
3806 | } |
3806 | } |
3807 | 3807 | ||
3808 | bo->unique_id = kgem_get_unique_id(kgem); |
3808 | bo->unique_id = kgem_get_unique_id(kgem); |
3809 | if (tiling == I915_TILING_NONE || |
3809 | if (tiling == I915_TILING_NONE || |
3810 | gem_set_tiling(kgem->fd, handle, tiling, pitch)) { |
3810 | gem_set_tiling(kgem->fd, handle, tiling, pitch)) { |
3811 | bo->tiling = tiling; |
3811 | bo->tiling = tiling; |
3812 | bo->pitch = pitch; |
3812 | bo->pitch = pitch; |
3813 | } else { |
3813 | } else { |
3814 | if (flags & CREATE_EXACT) { |
3814 | if (flags & CREATE_EXACT) { |
3815 | if (bo->pitch != pitch || bo->tiling != tiling) { |
3815 | if (bo->pitch != pitch || bo->tiling != tiling) { |
3816 | kgem_bo_free(kgem, bo); |
3816 | kgem_bo_free(kgem, bo); |
3817 | return NULL; |
3817 | return NULL; |
3818 | } |
3818 | } |
3819 | } |
3819 | } |
3820 | } |
3820 | } |
3821 | 3821 | ||
3822 | assert(bytes(bo) >= bo->pitch * kgem_aligned_height(kgem, height, bo->tiling)); |
3822 | assert(bytes(bo) >= bo->pitch * kgem_aligned_height(kgem, height, bo->tiling)); |
3823 | assert_tiling(kgem, bo); |
3823 | assert_tiling(kgem, bo); |
3824 | 3824 | ||
3825 | debug_alloc__bo(kgem, bo); |
3825 | debug_alloc__bo(kgem, bo); |
3826 | 3826 | ||
3827 | DBG((" new pitch=%d, tiling=%d, handle=%d, id=%d, num_pages=%d [%d], bucket=%d\n", |
3827 | DBG((" new pitch=%d, tiling=%d, handle=%d, id=%d, num_pages=%d [%d], bucket=%d\n", |
3828 | bo->pitch, bo->tiling, bo->handle, bo->unique_id, |
3828 | bo->pitch, bo->tiling, bo->handle, bo->unique_id, |
3829 | size, num_pages(bo), bucket(bo))); |
3829 | size, num_pages(bo), bucket(bo))); |
3830 | return bo; |
3830 | return bo; |
3831 | } |
3831 | } |
3832 | 3832 | ||
3833 | #if 0 |
3833 | #if 0 |
3834 | struct kgem_bo *kgem_create_cpu_2d(struct kgem *kgem, |
3834 | struct kgem_bo *kgem_create_cpu_2d(struct kgem *kgem, |
3835 | int width, |
3835 | int width, |
3836 | int height, |
3836 | int height, |
3837 | int bpp, |
3837 | int bpp, |
3838 | uint32_t flags) |
3838 | uint32_t flags) |
3839 | { |
3839 | { |
3840 | struct kgem_bo *bo; |
3840 | struct kgem_bo *bo; |
3841 | int stride, size; |
3841 | int stride, size; |
3842 | 3842 | ||
3843 | if (DBG_NO_CPU) |
3843 | if (DBG_NO_CPU) |
3844 | return NULL; |
3844 | return NULL; |
3845 | 3845 | ||
3846 | DBG(("%s(%dx%d, bpp=%d)\n", __FUNCTION__, width, height, bpp)); |
3846 | DBG(("%s(%dx%d, bpp=%d)\n", __FUNCTION__, width, height, bpp)); |
3847 | 3847 | ||
3848 | if (kgem->has_llc) { |
3848 | if (kgem->has_llc) { |
3849 | bo = kgem_create_2d(kgem, width, height, bpp, |
3849 | bo = kgem_create_2d(kgem, width, height, bpp, |
3850 | I915_TILING_NONE, flags); |
3850 | I915_TILING_NONE, flags); |
3851 | if (bo == NULL) |
3851 | if (bo == NULL) |
3852 | return bo; |
3852 | return bo; |
3853 | 3853 | ||
3854 | assert(bo->tiling == I915_TILING_NONE); |
3854 | assert(bo->tiling == I915_TILING_NONE); |
3855 | assert_tiling(kgem, bo); |
3855 | assert_tiling(kgem, bo); |
3856 | 3856 | ||
3857 | if (kgem_bo_map__cpu(kgem, bo) == NULL) { |
3857 | if (kgem_bo_map__cpu(kgem, bo) == NULL) { |
3858 | kgem_bo_destroy(kgem, bo); |
3858 | kgem_bo_destroy(kgem, bo); |
3859 | return NULL; |
3859 | return NULL; |
3860 | } |
3860 | } |
3861 | 3861 | ||
3862 | return bo; |
3862 | return bo; |
3863 | } |
3863 | } |
3864 | 3864 | ||
3865 | assert(width > 0 && height > 0); |
3865 | assert(width > 0 && height > 0); |
3866 | stride = ALIGN(width, 2) * bpp >> 3; |
3866 | stride = ALIGN(width, 2) * bpp >> 3; |
3867 | stride = ALIGN(stride, 4); |
3867 | stride = ALIGN(stride, 4); |
3868 | size = stride * ALIGN(height, 2); |
3868 | size = stride * ALIGN(height, 2); |
3869 | assert(size >= PAGE_SIZE); |
3869 | assert(size >= PAGE_SIZE); |
3870 | 3870 | ||
3871 | DBG(("%s: %dx%d, %d bpp, stride=%d\n", |
3871 | DBG(("%s: %dx%d, %d bpp, stride=%d\n", |
3872 | __FUNCTION__, width, height, bpp, stride)); |
3872 | __FUNCTION__, width, height, bpp, stride)); |
3873 | 3873 | ||
3874 | bo = search_snoop_cache(kgem, NUM_PAGES(size), 0); |
3874 | bo = search_snoop_cache(kgem, NUM_PAGES(size), 0); |
3875 | if (bo) { |
3875 | if (bo) { |
3876 | assert(bo->tiling == I915_TILING_NONE); |
3876 | assert(bo->tiling == I915_TILING_NONE); |
3877 | assert_tiling(kgem, bo); |
3877 | assert_tiling(kgem, bo); |
3878 | assert(bo->snoop); |
3878 | assert(bo->snoop); |
3879 | bo->refcnt = 1; |
3879 | bo->refcnt = 1; |
3880 | bo->pitch = stride; |
3880 | bo->pitch = stride; |
3881 | bo->unique_id = kgem_get_unique_id(kgem); |
3881 | bo->unique_id = kgem_get_unique_id(kgem); |
3882 | return bo; |
3882 | return bo; |
3883 | } |
3883 | } |
3884 | 3884 | ||
3885 | if (kgem->has_caching) { |
3885 | if (kgem->has_caching) { |
3886 | bo = kgem_create_linear(kgem, size, flags); |
3886 | bo = kgem_create_linear(kgem, size, flags); |
3887 | if (bo == NULL) |
3887 | if (bo == NULL) |
3888 | return NULL; |
3888 | return NULL; |
3889 | 3889 | ||
3890 | assert(bo->tiling == I915_TILING_NONE); |
3890 | assert(bo->tiling == I915_TILING_NONE); |
3891 | assert_tiling(kgem, bo); |
3891 | assert_tiling(kgem, bo); |
3892 | 3892 | ||
3893 | if (!gem_set_caching(kgem->fd, bo->handle, SNOOPED)) { |
3893 | if (!gem_set_caching(kgem->fd, bo->handle, SNOOPED)) { |
3894 | kgem_bo_destroy(kgem, bo); |
3894 | kgem_bo_destroy(kgem, bo); |
3895 | return NULL; |
3895 | return NULL; |
3896 | } |
3896 | } |
3897 | bo->snoop = true; |
3897 | bo->snoop = true; |
3898 | 3898 | ||
3899 | if (kgem_bo_map__cpu(kgem, bo) == NULL) { |
3899 | if (kgem_bo_map__cpu(kgem, bo) == NULL) { |
3900 | kgem_bo_destroy(kgem, bo); |
3900 | kgem_bo_destroy(kgem, bo); |
3901 | return NULL; |
3901 | return NULL; |
3902 | } |
3902 | } |
3903 | 3903 | ||
3904 | bo->pitch = stride; |
3904 | bo->pitch = stride; |
3905 | bo->unique_id = kgem_get_unique_id(kgem); |
3905 | bo->unique_id = kgem_get_unique_id(kgem); |
3906 | return bo; |
3906 | return bo; |
3907 | } |
3907 | } |
3908 | 3908 | ||
3909 | if (kgem->has_userptr) { |
3909 | if (kgem->has_userptr) { |
3910 | void *ptr; |
3910 | void *ptr; |
3911 | 3911 | ||
3912 | /* XXX */ |
3912 | /* XXX */ |
3913 | //if (posix_memalign(&ptr, 64, ALIGN(size, 64))) |
3913 | //if (posix_memalign(&ptr, 64, ALIGN(size, 64))) |
3914 | if (posix_memalign(&ptr, PAGE_SIZE, ALIGN(size, PAGE_SIZE))) |
3914 | if (posix_memalign(&ptr, PAGE_SIZE, ALIGN(size, PAGE_SIZE))) |
3915 | return NULL; |
3915 | return NULL; |
3916 | 3916 | ||
3917 | bo = kgem_create_map(kgem, ptr, size, false); |
3917 | bo = kgem_create_map(kgem, ptr, size, false); |
3918 | if (bo == NULL) { |
3918 | if (bo == NULL) { |
3919 | free(ptr); |
3919 | free(ptr); |
3920 | return NULL; |
3920 | return NULL; |
3921 | } |
3921 | } |
3922 | 3922 | ||
3923 | bo->pitch = stride; |
3923 | bo->pitch = stride; |
3924 | bo->unique_id = kgem_get_unique_id(kgem); |
3924 | bo->unique_id = kgem_get_unique_id(kgem); |
3925 | return bo; |
3925 | return bo; |
3926 | } |
3926 | } |
3927 | 3927 | ||
3928 | return NULL; |
3928 | return NULL; |
3929 | } |
3929 | } |
3930 | #endif |
3930 | #endif |
3931 | 3931 | ||
3932 | void _kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo) |
3932 | void _kgem_bo_destroy(struct kgem *kgem, struct kgem_bo *bo) |
3933 | { |
3933 | { |
3934 | DBG(("%s: handle=%d, proxy? %d\n", |
3934 | DBG(("%s: handle=%d, proxy? %d\n", |
3935 | __FUNCTION__, bo->handle, bo->proxy != NULL)); |
3935 | __FUNCTION__, bo->handle, bo->proxy != NULL)); |
3936 | 3936 | ||
3937 | if (bo->proxy) { |
3937 | if (bo->proxy) { |
3938 | _list_del(&bo->vma); |
3938 | _list_del(&bo->vma); |
3939 | _list_del(&bo->request); |
3939 | _list_del(&bo->request); |
3940 | if (bo->io && bo->exec == NULL) |
3940 | if (bo->io && bo->exec == NULL) |
3941 | _kgem_bo_delete_buffer(kgem, bo); |
3941 | _kgem_bo_delete_buffer(kgem, bo); |
3942 | kgem_bo_unref(kgem, bo->proxy); |
3942 | kgem_bo_unref(kgem, bo->proxy); |
3943 | kgem_bo_binding_free(kgem, bo); |
3943 | kgem_bo_binding_free(kgem, bo); |
3944 | free(bo); |
3944 | free(bo); |
3945 | return; |
3945 | return; |
3946 | } |
3946 | } |
3947 | 3947 | ||
3948 | __kgem_bo_destroy(kgem, bo); |
3948 | __kgem_bo_destroy(kgem, bo); |
3949 | } |
3949 | } |
3950 | 3950 | ||
3951 | static void __kgem_flush(struct kgem *kgem, struct kgem_bo *bo) |
3951 | static void __kgem_flush(struct kgem *kgem, struct kgem_bo *bo) |
3952 | { |
3952 | { |
3953 | assert(bo->rq); |
3953 | assert(bo->rq); |
3954 | assert(bo->exec == NULL); |
3954 | assert(bo->exec == NULL); |
3955 | assert(bo->needs_flush); |
3955 | assert(bo->needs_flush); |
3956 | 3956 | ||
3957 | /* The kernel will emit a flush *and* update its own flushing lists. */ |
3957 | /* The kernel will emit a flush *and* update its own flushing lists. */ |
3958 | if (!__kgem_busy(kgem, bo->handle)) |
3958 | if (!__kgem_busy(kgem, bo->handle)) |
3959 | __kgem_bo_clear_busy(bo); |
3959 | __kgem_bo_clear_busy(bo); |
3960 | 3960 | ||
3961 | DBG(("%s: handle=%d, busy?=%d\n", |
3961 | DBG(("%s: handle=%d, busy?=%d\n", |
3962 | __FUNCTION__, bo->handle, bo->rq != NULL)); |
3962 | __FUNCTION__, bo->handle, bo->rq != NULL)); |
3963 | } |
3963 | } |
3964 | 3964 | ||
3965 | void kgem_scanout_flush(struct kgem *kgem, struct kgem_bo *bo) |
3965 | void kgem_scanout_flush(struct kgem *kgem, struct kgem_bo *bo) |
3966 | { |
3966 | { |
3967 | kgem_bo_submit(kgem, bo); |
3967 | kgem_bo_submit(kgem, bo); |
3968 | if (!bo->needs_flush) |
3968 | if (!bo->needs_flush) |
3969 | return; |
3969 | return; |
3970 | 3970 | ||
3971 | /* If the kernel fails to emit the flush, then it will be forced when |
3971 | /* If the kernel fails to emit the flush, then it will be forced when |
3972 | * we assume direct access. And as the usual failure is EIO, we do |
3972 | * we assume direct access. And as the usual failure is EIO, we do |
3973 | * not actually care. |
3973 | * not actually care. |
3974 | */ |
3974 | */ |
3975 | assert(bo->exec == NULL); |
3975 | assert(bo->exec == NULL); |
3976 | if (bo->rq) |
3976 | if (bo->rq) |
3977 | __kgem_flush(kgem, bo); |
3977 | __kgem_flush(kgem, bo); |
3978 | 3978 | ||
3979 | /* Whatever actually happens, we can regard the GTT write domain |
3979 | /* Whatever actually happens, we can regard the GTT write domain |
3980 | * as being flushed. |
3980 | * as being flushed. |
3981 | */ |
3981 | */ |
3982 | bo->gtt_dirty = false; |
3982 | bo->gtt_dirty = false; |
3983 | bo->needs_flush = false; |
3983 | bo->needs_flush = false; |
3984 | bo->domain = DOMAIN_NONE; |
3984 | bo->domain = DOMAIN_NONE; |
3985 | } |
3985 | } |
3986 | 3986 | ||
3987 | inline static bool needs_semaphore(struct kgem *kgem, struct kgem_bo *bo) |
3987 | inline static bool needs_semaphore(struct kgem *kgem, struct kgem_bo *bo) |
3988 | { |
3988 | { |
3989 | return kgem->nreloc && bo->rq && RQ_RING(bo->rq) != kgem->ring; |
3989 | return kgem->nreloc && bo->rq && RQ_RING(bo->rq) != kgem->ring; |
3990 | } |
3990 | } |
3991 | 3991 | ||
3992 | bool kgem_check_bo(struct kgem *kgem, ...) |
3992 | bool kgem_check_bo(struct kgem *kgem, ...) |
3993 | { |
3993 | { |
3994 | va_list ap; |
3994 | va_list ap; |
3995 | struct kgem_bo *bo; |
3995 | struct kgem_bo *bo; |
3996 | int num_exec = 0; |
3996 | int num_exec = 0; |
3997 | int num_pages = 0; |
3997 | int num_pages = 0; |
3998 | bool flush = false; |
3998 | bool flush = false; |
3999 | 3999 | ||
4000 | va_start(ap, kgem); |
4000 | va_start(ap, kgem); |
4001 | while ((bo = va_arg(ap, struct kgem_bo *))) { |
4001 | while ((bo = va_arg(ap, struct kgem_bo *))) { |
4002 | while (bo->proxy) |
4002 | while (bo->proxy) |
4003 | bo = bo->proxy; |
4003 | bo = bo->proxy; |
4004 | if (bo->exec) |
4004 | if (bo->exec) |
4005 | continue; |
4005 | continue; |
4006 | 4006 | ||
4007 | if (needs_semaphore(kgem, bo)) |
4007 | if (needs_semaphore(kgem, bo)) |
4008 | return false; |
4008 | return false; |
4009 | 4009 | ||
4010 | num_pages += num_pages(bo); |
4010 | num_pages += num_pages(bo); |
4011 | num_exec++; |
4011 | num_exec++; |
4012 | 4012 | ||
4013 | flush |= bo->flush; |
4013 | flush |= bo->flush; |
4014 | } |
4014 | } |
4015 | va_end(ap); |
4015 | va_end(ap); |
4016 | 4016 | ||
4017 | DBG(("%s: num_pages=+%d, num_exec=+%d\n", |
4017 | DBG(("%s: num_pages=+%d, num_exec=+%d\n", |
4018 | __FUNCTION__, num_pages, num_exec)); |
4018 | __FUNCTION__, num_pages, num_exec)); |
4019 | 4019 | ||
4020 | if (!num_pages) |
4020 | if (!num_pages) |
4021 | return true; |
4021 | return true; |
4022 | 4022 | ||
4023 | if (kgem_flush(kgem, flush)) |
4023 | if (kgem_flush(kgem, flush)) |
4024 | return false; |
4024 | return false; |
4025 | 4025 | ||
4026 | if (kgem->aperture > kgem->aperture_low && |
4026 | if (kgem->aperture > kgem->aperture_low && |
4027 | kgem_ring_is_idle(kgem, kgem->ring)) { |
4027 | kgem_ring_is_idle(kgem, kgem->ring)) { |
4028 | DBG(("%s: current aperture usage (%d) is greater than low water mark (%d)\n", |
4028 | DBG(("%s: current aperture usage (%d) is greater than low water mark (%d)\n", |
4029 | __FUNCTION__, kgem->aperture, kgem->aperture_low)); |
4029 | __FUNCTION__, kgem->aperture, kgem->aperture_low)); |
4030 | return false; |
4030 | return false; |
4031 | } |
4031 | } |
4032 | 4032 | ||
4033 | if (num_pages + kgem->aperture > kgem->aperture_high) { |
4033 | if (num_pages + kgem->aperture > kgem->aperture_high) { |
4034 | DBG(("%s: final aperture usage (%d) is greater than high water mark (%d)\n", |
4034 | DBG(("%s: final aperture usage (%d) is greater than high water mark (%d)\n", |
4035 | __FUNCTION__, num_pages + kgem->aperture, kgem->aperture_high)); |
4035 | __FUNCTION__, num_pages + kgem->aperture, kgem->aperture_high)); |
4036 | return false; |
4036 | return false; |
4037 | } |
4037 | } |
4038 | 4038 | ||
4039 | if (kgem->nexec + num_exec >= KGEM_EXEC_SIZE(kgem)) { |
4039 | if (kgem->nexec + num_exec >= KGEM_EXEC_SIZE(kgem)) { |
4040 | DBG(("%s: out of exec slots (%d + %d / %d)\n", __FUNCTION__, |
4040 | DBG(("%s: out of exec slots (%d + %d / %d)\n", __FUNCTION__, |
4041 | kgem->nexec, num_exec, KGEM_EXEC_SIZE(kgem))); |
4041 | kgem->nexec, num_exec, KGEM_EXEC_SIZE(kgem))); |
4042 | return false; |
4042 | return false; |
4043 | } |
4043 | } |
4044 | 4044 | ||
4045 | return true; |
4045 | return true; |
4046 | } |
4046 | } |
4047 | 4047 | ||
4048 | 4048 | ||
4049 | 4049 | ||
4050 | 4050 | ||
4051 | 4051 | ||
4052 | 4052 | ||
4053 | 4053 | ||
4054 | 4054 | ||
4055 | 4055 | ||
4056 | 4056 | ||
4057 | 4057 | ||
4058 | 4058 | ||
4059 | 4059 | ||
4060 | 4060 | ||
4061 | 4061 | ||
4062 | 4062 | ||
4063 | 4063 | ||
4064 | 4064 | ||
4065 | 4065 | ||
4066 | 4066 | ||
4067 | 4067 | ||
4068 | 4068 | ||
4069 | 4069 | ||
4070 | 4070 | ||
4071 | 4071 | ||
4072 | 4072 | ||
4073 | 4073 | ||
4074 | 4074 | ||
4075 | 4075 | ||
4076 | 4076 | ||
4077 | uint32_t kgem_add_reloc(struct kgem *kgem, |
4077 | uint32_t kgem_add_reloc(struct kgem *kgem, |
4078 | uint32_t pos, |
4078 | uint32_t pos, |
4079 | struct kgem_bo *bo, |
4079 | struct kgem_bo *bo, |
4080 | uint32_t read_write_domain, |
4080 | uint32_t read_write_domain, |
4081 | uint32_t delta) |
4081 | uint32_t delta) |
4082 | { |
4082 | { |
4083 | int index; |
4083 | int index; |
4084 | 4084 | ||
4085 | DBG(("%s: handle=%d, pos=%d, delta=%d, domains=%08x\n", |
4085 | DBG(("%s: handle=%d, pos=%d, delta=%d, domains=%08x\n", |
4086 | __FUNCTION__, bo ? bo->handle : 0, pos, delta, read_write_domain)); |
4086 | __FUNCTION__, bo ? bo->handle : 0, pos, delta, read_write_domain)); |
4087 | 4087 | ||
4088 | assert((read_write_domain & 0x7fff) == 0 || bo != NULL); |
4088 | assert((read_write_domain & 0x7fff) == 0 || bo != NULL); |
4089 | 4089 | ||
4090 | if( bo != NULL && bo->handle == -2) |
4090 | if( bo != NULL && bo->handle == -2) |
4091 | { |
4091 | { |
4092 | if (bo->exec == NULL) |
4092 | if (bo->exec == NULL) |
4093 | kgem_add_bo(kgem, bo); |
4093 | kgem_add_bo(kgem, bo); |
4094 | 4094 | ||
4095 | if (read_write_domain & 0x7fff && !bo->gpu_dirty) { |
4095 | if (read_write_domain & 0x7fff && !bo->gpu_dirty) { |
4096 | __kgem_bo_mark_dirty(bo); |
4096 | __kgem_bo_mark_dirty(bo); |
4097 | } |
4097 | } |
4098 | return 0; |
4098 | return 0; |
4099 | }; |
4099 | }; |
4100 | 4100 | ||
4101 | index = kgem->nreloc++; |
4101 | index = kgem->nreloc++; |
4102 | assert(index < ARRAY_SIZE(kgem->reloc)); |
4102 | assert(index < ARRAY_SIZE(kgem->reloc)); |
4103 | kgem->reloc[index].offset = pos * sizeof(kgem->batch[0]); |
4103 | kgem->reloc[index].offset = pos * sizeof(kgem->batch[0]); |
4104 | if (bo) { |
4104 | if (bo) { |
4105 | assert(bo->refcnt); |
4105 | assert(bo->refcnt); |
4106 | while (bo->proxy) { |
4106 | while (bo->proxy) { |
4107 | DBG(("%s: adding proxy [delta=%d] for handle=%d\n", |
4107 | DBG(("%s: adding proxy [delta=%d] for handle=%d\n", |
4108 | __FUNCTION__, bo->delta, bo->handle)); |
4108 | __FUNCTION__, bo->delta, bo->handle)); |
4109 | delta += bo->delta; |
4109 | delta += bo->delta; |
4110 | assert(bo->handle == bo->proxy->handle); |
4110 | assert(bo->handle == bo->proxy->handle); |
4111 | /* need to release the cache upon batch submit */ |
4111 | /* need to release the cache upon batch submit */ |
4112 | if (bo->exec == NULL) { |
4112 | if (bo->exec == NULL) { |
4113 | list_move_tail(&bo->request, |
4113 | list_move_tail(&bo->request, |
4114 | &kgem->next_request->buffers); |
4114 | &kgem->next_request->buffers); |
4115 | bo->rq = MAKE_REQUEST(kgem->next_request, |
4115 | bo->rq = MAKE_REQUEST(kgem->next_request, |
4116 | kgem->ring); |
4116 | kgem->ring); |
4117 | bo->exec = &_kgem_dummy_exec; |
4117 | bo->exec = &_kgem_dummy_exec; |
4118 | } |
4118 | } |
4119 | 4119 | ||
4120 | if (read_write_domain & 0x7fff && !bo->gpu_dirty) |
4120 | if (read_write_domain & 0x7fff && !bo->gpu_dirty) |
4121 | __kgem_bo_mark_dirty(bo); |
4121 | __kgem_bo_mark_dirty(bo); |
4122 | 4122 | ||
4123 | bo = bo->proxy; |
4123 | bo = bo->proxy; |
4124 | assert(bo->refcnt); |
4124 | assert(bo->refcnt); |
4125 | } |
4125 | } |
4126 | assert(bo->refcnt); |
4126 | assert(bo->refcnt); |
4127 | 4127 | ||
4128 | if (bo->exec == NULL) |
4128 | if (bo->exec == NULL) |
4129 | kgem_add_bo(kgem, bo); |
4129 | kgem_add_bo(kgem, bo); |
4130 | assert(bo->rq == MAKE_REQUEST(kgem->next_request, kgem->ring)); |
4130 | assert(bo->rq == MAKE_REQUEST(kgem->next_request, kgem->ring)); |
4131 | assert(RQ_RING(bo->rq) == kgem->ring); |
4131 | assert(RQ_RING(bo->rq) == kgem->ring); |
4132 | 4132 | ||
4133 | if (kgem->gen < 040 && read_write_domain & KGEM_RELOC_FENCED) { |
4133 | if (kgem->gen < 040 && read_write_domain & KGEM_RELOC_FENCED) { |
4134 | if (bo->tiling && |
4134 | if (bo->tiling && |
4135 | (bo->exec->flags & EXEC_OBJECT_NEEDS_FENCE) == 0) { |
4135 | (bo->exec->flags & EXEC_OBJECT_NEEDS_FENCE) == 0) { |
4136 | assert(kgem->nfence < kgem->fence_max); |
4136 | assert(kgem->nfence < kgem->fence_max); |
4137 | kgem->aperture_fenced += |
4137 | kgem->aperture_fenced += |
4138 | kgem_bo_fenced_size(kgem, bo); |
4138 | kgem_bo_fenced_size(kgem, bo); |
4139 | kgem->nfence++; |
4139 | kgem->nfence++; |
4140 | } |
4140 | } |
4141 | bo->exec->flags |= EXEC_OBJECT_NEEDS_FENCE; |
4141 | bo->exec->flags |= EXEC_OBJECT_NEEDS_FENCE; |
4142 | } |
4142 | } |
4143 | 4143 | ||
4144 | kgem->reloc[index].delta = delta; |
4144 | kgem->reloc[index].delta = delta; |
4145 | kgem->reloc[index].target_handle = bo->target_handle; |
4145 | kgem->reloc[index].target_handle = bo->target_handle; |
4146 | kgem->reloc[index].presumed_offset = bo->presumed_offset; |
4146 | kgem->reloc[index].presumed_offset = bo->presumed_offset; |
4147 | 4147 | ||
4148 | if (read_write_domain & 0x7fff && !bo->gpu_dirty) { |
4148 | if (read_write_domain & 0x7fff && !bo->gpu_dirty) { |
4149 | assert(!bo->snoop || kgem->can_blt_cpu); |
4149 | assert(!bo->snoop || kgem->can_blt_cpu); |
4150 | __kgem_bo_mark_dirty(bo); |
4150 | __kgem_bo_mark_dirty(bo); |
4151 | } |
4151 | } |
4152 | 4152 | ||
4153 | delta += bo->presumed_offset; |
4153 | delta += bo->presumed_offset; |
4154 | } else { |
4154 | } else { |
4155 | kgem->reloc[index].delta = delta; |
4155 | kgem->reloc[index].delta = delta; |
4156 | kgem->reloc[index].target_handle = ~0U; |
4156 | kgem->reloc[index].target_handle = ~0U; |
4157 | kgem->reloc[index].presumed_offset = 0; |
4157 | kgem->reloc[index].presumed_offset = 0; |
4158 | if (kgem->nreloc__self < 256) |
4158 | if (kgem->nreloc__self < 256) |
4159 | kgem->reloc__self[kgem->nreloc__self++] = index; |
4159 | kgem->reloc__self[kgem->nreloc__self++] = index; |
4160 | } |
4160 | } |
4161 | kgem->reloc[index].read_domains = read_write_domain >> 16; |
4161 | kgem->reloc[index].read_domains = read_write_domain >> 16; |
4162 | kgem->reloc[index].write_domain = read_write_domain & 0x7fff; |
4162 | kgem->reloc[index].write_domain = read_write_domain & 0x7fff; |
4163 | 4163 | ||
4164 | return delta; |
4164 | return delta; |
4165 | } |
4165 | } |
4166 | 4166 | ||
4167 | static void kgem_trim_vma_cache(struct kgem *kgem, int type, int bucket) |
4167 | static void kgem_trim_vma_cache(struct kgem *kgem, int type, int bucket) |
4168 | { |
4168 | { |
4169 | int i, j; |
4169 | int i, j; |
4170 | 4170 | ||
4171 | DBG(("%s: type=%d, count=%d (bucket: %d)\n", |
4171 | DBG(("%s: type=%d, count=%d (bucket: %d)\n", |
4172 | __FUNCTION__, type, kgem->vma[type].count, bucket)); |
4172 | __FUNCTION__, type, kgem->vma[type].count, bucket)); |
4173 | if (kgem->vma[type].count <= 0) |
4173 | if (kgem->vma[type].count <= 0) |
4174 | return; |
4174 | return; |
4175 | 4175 | ||
4176 | if (kgem->need_purge) |
4176 | if (kgem->need_purge) |
4177 | kgem_purge_cache(kgem); |
4177 | kgem_purge_cache(kgem); |
4178 | 4178 | ||
4179 | /* vma are limited on a per-process basis to around 64k. |
4179 | /* vma are limited on a per-process basis to around 64k. |
4180 | * This includes all malloc arenas as well as other file |
4180 | * This includes all malloc arenas as well as other file |
4181 | * mappings. In order to be fair and not hog the cache, |
4181 | * mappings. In order to be fair and not hog the cache, |
4182 | * and more importantly not to exhaust that limit and to |
4182 | * and more importantly not to exhaust that limit and to |
4183 | * start failing mappings, we keep our own number of open |
4183 | * start failing mappings, we keep our own number of open |
4184 | * vma to within a conservative value. |
4184 | * vma to within a conservative value. |
4185 | */ |
4185 | */ |
4186 | i = 0; |
4186 | i = 0; |
4187 | while (kgem->vma[type].count > 0) { |
4187 | while (kgem->vma[type].count > 0) { |
4188 | struct kgem_bo *bo = NULL; |
4188 | struct kgem_bo *bo = NULL; |
4189 | 4189 | ||
4190 | for (j = 0; |
4190 | for (j = 0; |
4191 | bo == NULL && j < ARRAY_SIZE(kgem->vma[type].inactive); |
4191 | bo == NULL && j < ARRAY_SIZE(kgem->vma[type].inactive); |
4192 | j++) { |
4192 | j++) { |
4193 | struct list *head = &kgem->vma[type].inactive[i++%ARRAY_SIZE(kgem->vma[type].inactive)]; |
4193 | struct list *head = &kgem->vma[type].inactive[i++%ARRAY_SIZE(kgem->vma[type].inactive)]; |
4194 | if (!list_is_empty(head)) |
4194 | if (!list_is_empty(head)) |
4195 | bo = list_last_entry(head, struct kgem_bo, vma); |
4195 | bo = list_last_entry(head, struct kgem_bo, vma); |
4196 | } |
4196 | } |
4197 | if (bo == NULL) |
4197 | if (bo == NULL) |
4198 | break; |
4198 | break; |
4199 | 4199 | ||
4200 | DBG(("%s: discarding inactive %s vma cache for %d\n", |
4200 | DBG(("%s: discarding inactive %s vma cache for %d\n", |
4201 | __FUNCTION__, |
4201 | __FUNCTION__, |
4202 | IS_CPU_MAP(bo->map) ? "CPU" : "GTT", bo->handle)); |
4202 | IS_CPU_MAP(bo->map) ? "CPU" : "GTT", bo->handle)); |
4203 | assert(IS_CPU_MAP(bo->map) == type); |
4203 | assert(IS_CPU_MAP(bo->map) == type); |
4204 | assert(bo->map); |
4204 | assert(bo->map); |
4205 | assert(bo->rq == NULL); |
4205 | assert(bo->rq == NULL); |
4206 | 4206 | ||
4207 | VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo))); |
4207 | VG(if (type) VALGRIND_MAKE_MEM_NOACCESS(MAP(bo->map), bytes(bo))); |
4208 | // munmap(MAP(bo->map), bytes(bo)); |
4208 | // munmap(MAP(bo->map), bytes(bo)); |
4209 | bo->map = NULL; |
4209 | bo->map = NULL; |
4210 | list_del(&bo->vma); |
4210 | list_del(&bo->vma); |
4211 | kgem->vma[type].count--; |
4211 | kgem->vma[type].count--; |
4212 | 4212 | ||
4213 | if (!bo->purged && !kgem_bo_set_purgeable(kgem, bo)) { |
4213 | if (!bo->purged && !kgem_bo_set_purgeable(kgem, bo)) { |
4214 | DBG(("%s: freeing unpurgeable old mapping\n", |
4214 | DBG(("%s: freeing unpurgeable old mapping\n", |
4215 | __FUNCTION__)); |
4215 | __FUNCTION__)); |
4216 | kgem_bo_free(kgem, bo); |
4216 | kgem_bo_free(kgem, bo); |
4217 | } |
4217 | } |
4218 | } |
4218 | } |
4219 | } |
4219 | } |
4220 | 4220 | ||
4221 | void *kgem_bo_map__async(struct kgem *kgem, struct kgem_bo *bo) |
4221 | void *kgem_bo_map__async(struct kgem *kgem, struct kgem_bo *bo) |
4222 | { |
4222 | { |
4223 | void *ptr; |
4223 | void *ptr; |
4224 | 4224 | ||
4225 | DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__, |
4225 | DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__, |
4226 | bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain)); |
4226 | bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain)); |
4227 | 4227 | ||
4228 | assert(bo->proxy == NULL); |
4228 | assert(bo->proxy == NULL); |
4229 | assert(list_is_empty(&bo->list)); |
4229 | assert(list_is_empty(&bo->list)); |
4230 | assert(!IS_USER_MAP(bo->map)); |
4230 | assert(!IS_USER_MAP(bo->map)); |
4231 | assert_tiling(kgem, bo); |
4231 | assert_tiling(kgem, bo); |
4232 | 4232 | ||
4233 | if (bo->tiling == I915_TILING_NONE && !bo->scanout && kgem->has_llc) { |
4233 | if (bo->tiling == I915_TILING_NONE && !bo->scanout && kgem->has_llc) { |
4234 | DBG(("%s: converting request for GTT map into CPU map\n", |
4234 | DBG(("%s: converting request for GTT map into CPU map\n", |
4235 | __FUNCTION__)); |
4235 | __FUNCTION__)); |
4236 | return kgem_bo_map__cpu(kgem, bo); |
4236 | return kgem_bo_map__cpu(kgem, bo); |
4237 | } |
4237 | } |
4238 | 4238 | ||
4239 | if (IS_CPU_MAP(bo->map)) |
4239 | if (IS_CPU_MAP(bo->map)) |
4240 | kgem_bo_release_map(kgem, bo); |
4240 | kgem_bo_release_map(kgem, bo); |
4241 | 4241 | ||
4242 | ptr = bo->map; |
4242 | ptr = bo->map; |
4243 | if (ptr == NULL) { |
4243 | if (ptr == NULL) { |
4244 | assert(kgem_bo_size(bo) <= kgem->aperture_mappable / 2); |
4244 | assert(kgem_bo_size(bo) <= kgem->aperture_mappable / 2); |
4245 | 4245 | ||
4246 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4246 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4247 | 4247 | ||
4248 | ptr = __kgem_bo_map__gtt(kgem, bo); |
4248 | ptr = __kgem_bo_map__gtt(kgem, bo); |
4249 | if (ptr == NULL) |
4249 | if (ptr == NULL) |
4250 | return NULL; |
4250 | return NULL; |
4251 | 4251 | ||
4252 | /* Cache this mapping to avoid the overhead of an |
4252 | /* Cache this mapping to avoid the overhead of an |
4253 | * excruciatingly slow GTT pagefault. This is more an |
4253 | * excruciatingly slow GTT pagefault. This is more an |
4254 | * issue with compositing managers which need to frequently |
4254 | * issue with compositing managers which need to frequently |
4255 | * flush CPU damage to their GPU bo. |
4255 | * flush CPU damage to their GPU bo. |
4256 | */ |
4256 | */ |
4257 | bo->map = ptr; |
4257 | bo->map = ptr; |
4258 | DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4258 | DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4259 | } |
4259 | } |
4260 | 4260 | ||
4261 | return ptr; |
4261 | return ptr; |
4262 | } |
4262 | } |
4263 | 4263 | ||
4264 | void *kgem_bo_map(struct kgem *kgem, struct kgem_bo *bo) |
4264 | void *kgem_bo_map(struct kgem *kgem, struct kgem_bo *bo) |
4265 | { |
4265 | { |
4266 | void *ptr; |
4266 | void *ptr; |
4267 | 4267 | ||
4268 | DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__, |
4268 | DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__, |
4269 | bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain)); |
4269 | bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain)); |
4270 | 4270 | ||
4271 | assert(bo->proxy == NULL); |
4271 | assert(bo->proxy == NULL); |
4272 | assert(list_is_empty(&bo->list)); |
4272 | assert(list_is_empty(&bo->list)); |
4273 | assert(!IS_USER_MAP(bo->map)); |
4273 | assert(!IS_USER_MAP(bo->map)); |
4274 | assert(bo->exec == NULL); |
4274 | assert(bo->exec == NULL); |
4275 | assert_tiling(kgem, bo); |
4275 | assert_tiling(kgem, bo); |
4276 | 4276 | ||
4277 | if (bo->tiling == I915_TILING_NONE && !bo->scanout && |
4277 | if (bo->tiling == I915_TILING_NONE && !bo->scanout && |
4278 | (kgem->has_llc || bo->domain == DOMAIN_CPU)) { |
4278 | (kgem->has_llc || bo->domain == DOMAIN_CPU)) { |
4279 | DBG(("%s: converting request for GTT map into CPU map\n", |
4279 | DBG(("%s: converting request for GTT map into CPU map\n", |
4280 | __FUNCTION__)); |
4280 | __FUNCTION__)); |
4281 | ptr = kgem_bo_map__cpu(kgem, bo); |
4281 | ptr = kgem_bo_map__cpu(kgem, bo); |
4282 | if (ptr) |
4282 | if (ptr) |
4283 | kgem_bo_sync__cpu(kgem, bo); |
4283 | kgem_bo_sync__cpu(kgem, bo); |
4284 | return ptr; |
4284 | return ptr; |
4285 | } |
4285 | } |
4286 | 4286 | ||
4287 | if (IS_CPU_MAP(bo->map)) |
4287 | if (IS_CPU_MAP(bo->map)) |
4288 | kgem_bo_release_map(kgem, bo); |
4288 | kgem_bo_release_map(kgem, bo); |
4289 | 4289 | ||
4290 | ptr = bo->map; |
4290 | ptr = bo->map; |
4291 | if (ptr == NULL) { |
4291 | if (ptr == NULL) { |
4292 | assert(kgem_bo_size(bo) <= kgem->aperture_mappable / 2); |
4292 | assert(kgem_bo_size(bo) <= kgem->aperture_mappable / 2); |
4293 | assert(kgem->gen != 021 || bo->tiling != I915_TILING_Y); |
4293 | assert(kgem->gen != 021 || bo->tiling != I915_TILING_Y); |
4294 | 4294 | ||
4295 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4295 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4296 | 4296 | ||
4297 | ptr = __kgem_bo_map__gtt(kgem, bo); |
4297 | ptr = __kgem_bo_map__gtt(kgem, bo); |
4298 | if (ptr == NULL) |
4298 | if (ptr == NULL) |
4299 | return NULL; |
4299 | return NULL; |
4300 | 4300 | ||
4301 | /* Cache this mapping to avoid the overhead of an |
4301 | /* Cache this mapping to avoid the overhead of an |
4302 | * excruciatingly slow GTT pagefault. This is more an |
4302 | * excruciatingly slow GTT pagefault. This is more an |
4303 | * issue with compositing managers which need to frequently |
4303 | * issue with compositing managers which need to frequently |
4304 | * flush CPU damage to their GPU bo. |
4304 | * flush CPU damage to their GPU bo. |
4305 | */ |
4305 | */ |
4306 | bo->map = ptr; |
4306 | bo->map = ptr; |
4307 | DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4307 | DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4308 | } |
4308 | } |
4309 | 4309 | ||
4310 | if (bo->domain != DOMAIN_GTT || FORCE_MMAP_SYNC & (1 << DOMAIN_GTT)) { |
4310 | if (bo->domain != DOMAIN_GTT || FORCE_MMAP_SYNC & (1 << DOMAIN_GTT)) { |
4311 | struct drm_i915_gem_set_domain set_domain; |
4311 | struct drm_i915_gem_set_domain set_domain; |
4312 | 4312 | ||
4313 | DBG(("%s: sync: needs_flush? %d, domain? %d, busy? %d\n", __FUNCTION__, |
4313 | DBG(("%s: sync: needs_flush? %d, domain? %d, busy? %d\n", __FUNCTION__, |
4314 | bo->needs_flush, bo->domain, __kgem_busy(kgem, bo->handle))); |
4314 | bo->needs_flush, bo->domain, __kgem_busy(kgem, bo->handle))); |
4315 | 4315 | ||
4316 | /* XXX use PROT_READ to avoid the write flush? */ |
4316 | /* XXX use PROT_READ to avoid the write flush? */ |
4317 | 4317 | ||
4318 | VG_CLEAR(set_domain); |
4318 | VG_CLEAR(set_domain); |
4319 | set_domain.handle = bo->handle; |
4319 | set_domain.handle = bo->handle; |
4320 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
4320 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
4321 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
4321 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
4322 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) { |
4322 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) { |
4323 | kgem_bo_retire(kgem, bo); |
4323 | kgem_bo_retire(kgem, bo); |
4324 | bo->domain = DOMAIN_GTT; |
4324 | bo->domain = DOMAIN_GTT; |
4325 | bo->gtt_dirty = true; |
4325 | bo->gtt_dirty = true; |
4326 | } |
4326 | } |
4327 | } |
4327 | } |
4328 | 4328 | ||
4329 | return ptr; |
4329 | return ptr; |
4330 | } |
4330 | } |
4331 | 4331 | ||
4332 | void *kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo) |
4332 | void *kgem_bo_map__gtt(struct kgem *kgem, struct kgem_bo *bo) |
4333 | { |
4333 | { |
4334 | void *ptr; |
4334 | void *ptr; |
4335 | 4335 | ||
4336 | DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__, |
4336 | DBG(("%s: handle=%d, offset=%d, tiling=%d, map=%p, domain=%d\n", __FUNCTION__, |
4337 | bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain)); |
4337 | bo->handle, bo->presumed_offset, bo->tiling, bo->map, bo->domain)); |
4338 | 4338 | ||
4339 | assert(bo->exec == NULL); |
4339 | assert(bo->exec == NULL); |
4340 | assert(list_is_empty(&bo->list)); |
4340 | assert(list_is_empty(&bo->list)); |
4341 | assert(!IS_USER_MAP(bo->map)); |
4341 | assert(!IS_USER_MAP(bo->map)); |
4342 | assert_tiling(kgem, bo); |
4342 | assert_tiling(kgem, bo); |
4343 | 4343 | ||
4344 | if (IS_CPU_MAP(bo->map)) |
4344 | if (IS_CPU_MAP(bo->map)) |
4345 | kgem_bo_release_map(kgem, bo); |
4345 | kgem_bo_release_map(kgem, bo); |
4346 | 4346 | ||
4347 | ptr = bo->map; |
4347 | ptr = bo->map; |
4348 | if (ptr == NULL) { |
4348 | if (ptr == NULL) { |
4349 | assert(bytes(bo) <= kgem->aperture_mappable / 4); |
4349 | assert(bytes(bo) <= kgem->aperture_mappable / 4); |
4350 | 4350 | ||
4351 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4351 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4352 | 4352 | ||
4353 | ptr = __kgem_bo_map__gtt(kgem, bo); |
4353 | ptr = __kgem_bo_map__gtt(kgem, bo); |
4354 | if (ptr == NULL) |
4354 | if (ptr == NULL) |
4355 | return NULL; |
4355 | return NULL; |
4356 | 4356 | ||
4357 | /* Cache this mapping to avoid the overhead of an |
4357 | /* Cache this mapping to avoid the overhead of an |
4358 | * excruciatingly slow GTT pagefault. This is more an |
4358 | * excruciatingly slow GTT pagefault. This is more an |
4359 | * issue with compositing managers which need to frequently |
4359 | * issue with compositing managers which need to frequently |
4360 | * flush CPU damage to their GPU bo. |
4360 | * flush CPU damage to their GPU bo. |
4361 | */ |
4361 | */ |
4362 | bo->map = ptr; |
4362 | bo->map = ptr; |
4363 | DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4363 | DBG(("%s: caching GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4364 | } |
4364 | } |
4365 | 4365 | ||
4366 | return ptr; |
4366 | return ptr; |
4367 | } |
4367 | } |
4368 | 4368 | ||
4369 | void *kgem_bo_map__debug(struct kgem *kgem, struct kgem_bo *bo) |
4369 | void *kgem_bo_map__debug(struct kgem *kgem, struct kgem_bo *bo) |
4370 | { |
4370 | { |
4371 | if (bo->map) |
4371 | if (bo->map) |
4372 | return MAP(bo->map); |
4372 | return MAP(bo->map); |
4373 | 4373 | ||
4374 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4374 | kgem_trim_vma_cache(kgem, MAP_GTT, bucket(bo)); |
4375 | return bo->map = __kgem_bo_map__gtt(kgem, bo); |
4375 | return bo->map = __kgem_bo_map__gtt(kgem, bo); |
4376 | } |
4376 | } |
4377 | 4377 | ||
4378 | void *kgem_bo_map__cpu(struct kgem *kgem, struct kgem_bo *bo) |
4378 | void *kgem_bo_map__cpu(struct kgem *kgem, struct kgem_bo *bo) |
4379 | { |
4379 | { |
4380 | struct drm_i915_gem_mmap mmap_arg; |
4380 | struct drm_i915_gem_mmap mmap_arg; |
4381 | 4381 | ||
4382 | DBG(("%s(handle=%d, size=%d, mapped? %d)\n", |
4382 | DBG(("%s(handle=%d, size=%d, mapped? %d)\n", |
4383 | __FUNCTION__, bo->handle, bytes(bo), (int)__MAP_TYPE(bo->map))); |
4383 | __FUNCTION__, bo->handle, bytes(bo), (int)__MAP_TYPE(bo->map))); |
4384 | assert(!bo->purged); |
4384 | assert(!bo->purged); |
4385 | assert(list_is_empty(&bo->list)); |
4385 | assert(list_is_empty(&bo->list)); |
4386 | assert(bo->proxy == NULL); |
4386 | assert(bo->proxy == NULL); |
4387 | 4387 | ||
4388 | if (IS_CPU_MAP(bo->map)) |
4388 | if (IS_CPU_MAP(bo->map)) |
4389 | return MAP(bo->map); |
4389 | return MAP(bo->map); |
4390 | 4390 | ||
4391 | if (bo->map) |
4391 | if (bo->map) |
4392 | kgem_bo_release_map(kgem, bo); |
4392 | kgem_bo_release_map(kgem, bo); |
4393 | 4393 | ||
4394 | kgem_trim_vma_cache(kgem, MAP_CPU, bucket(bo)); |
4394 | kgem_trim_vma_cache(kgem, MAP_CPU, bucket(bo)); |
4395 | 4395 | ||
4396 | retry: |
4396 | retry: |
4397 | VG_CLEAR(mmap_arg); |
4397 | VG_CLEAR(mmap_arg); |
4398 | mmap_arg.handle = bo->handle; |
4398 | mmap_arg.handle = bo->handle; |
4399 | mmap_arg.offset = 0; |
4399 | mmap_arg.offset = 0; |
4400 | mmap_arg.size = bytes(bo); |
4400 | mmap_arg.size = bytes(bo); |
4401 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg)) { |
4401 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg)) { |
4402 | 4402 | ||
4403 | if (__kgem_throttle_retire(kgem, 0)) |
4403 | if (__kgem_throttle_retire(kgem, 0)) |
4404 | goto retry; |
4404 | goto retry; |
4405 | 4405 | ||
4406 | if (kgem->need_expire) { |
4406 | if (kgem->need_expire) { |
4407 | kgem_cleanup_cache(kgem); |
4407 | kgem_cleanup_cache(kgem); |
4408 | goto retry; |
4408 | goto retry; |
4409 | } |
4409 | } |
4410 | 4410 | ||
4411 | ErrorF("%s: failed to mmap handle=%d, %d bytes, into CPU domain\n", |
4411 | ErrorF("%s: failed to mmap handle=%d, %d bytes, into CPU domain\n", |
4412 | __FUNCTION__, bo->handle, bytes(bo)); |
4412 | __FUNCTION__, bo->handle, bytes(bo)); |
4413 | return NULL; |
4413 | return NULL; |
4414 | } |
4414 | } |
4415 | 4415 | ||
4416 | VG(VALGRIND_MAKE_MEM_DEFINED(mmap_arg.addr_ptr, bytes(bo))); |
4416 | VG(VALGRIND_MAKE_MEM_DEFINED(mmap_arg.addr_ptr, bytes(bo))); |
4417 | 4417 | ||
4418 | DBG(("%s: caching CPU vma for %d\n", __FUNCTION__, bo->handle)); |
4418 | DBG(("%s: caching CPU vma for %d\n", __FUNCTION__, bo->handle)); |
4419 | bo->map = MAKE_CPU_MAP(mmap_arg.addr_ptr); |
4419 | bo->map = MAKE_CPU_MAP(mmap_arg.addr_ptr); |
4420 | return (void *)(uintptr_t)mmap_arg.addr_ptr; |
4420 | return (void *)(uintptr_t)mmap_arg.addr_ptr; |
4421 | } |
4421 | } |
4422 | 4422 | ||
4423 | void *__kgem_bo_map__cpu(struct kgem *kgem, struct kgem_bo *bo) |
4423 | void *__kgem_bo_map__cpu(struct kgem *kgem, struct kgem_bo *bo) |
4424 | { |
4424 | { |
4425 | struct drm_i915_gem_mmap mmap_arg; |
4425 | struct drm_i915_gem_mmap mmap_arg; |
4426 | 4426 | ||
4427 | DBG(("%s(handle=%d, size=%d, mapped? %d)\n", |
4427 | DBG(("%s(handle=%d, size=%d, mapped? %d)\n", |
4428 | __FUNCTION__, bo->handle, bytes(bo), (int)__MAP_TYPE(bo->map))); |
4428 | __FUNCTION__, bo->handle, bytes(bo), (int)__MAP_TYPE(bo->map))); |
4429 | assert(bo->refcnt); |
4429 | assert(bo->refcnt); |
4430 | assert(!bo->purged); |
4430 | assert(!bo->purged); |
4431 | assert(list_is_empty(&bo->list)); |
4431 | assert(list_is_empty(&bo->list)); |
4432 | assert(bo->proxy == NULL); |
4432 | assert(bo->proxy == NULL); |
4433 | 4433 | ||
4434 | if (IS_CPU_MAP(bo->map)) |
4434 | if (IS_CPU_MAP(bo->map)) |
4435 | return MAP(bo->map); |
4435 | return MAP(bo->map); |
4436 | 4436 | ||
4437 | retry: |
4437 | retry: |
4438 | VG_CLEAR(mmap_arg); |
4438 | VG_CLEAR(mmap_arg); |
4439 | mmap_arg.handle = bo->handle; |
4439 | mmap_arg.handle = bo->handle; |
4440 | mmap_arg.offset = 0; |
4440 | mmap_arg.offset = 0; |
4441 | mmap_arg.size = bytes(bo); |
4441 | mmap_arg.size = bytes(bo); |
4442 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg)) { |
4442 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg)) { |
4443 | int err = errno; |
4443 | int err = errno; |
4444 | 4444 | ||
4445 | assert(err != EINVAL); |
4445 | assert(err != EINVAL); |
4446 | 4446 | ||
4447 | if (__kgem_throttle_retire(kgem, 0)) |
4447 | if (__kgem_throttle_retire(kgem, 0)) |
4448 | goto retry; |
4448 | goto retry; |
4449 | 4449 | ||
4450 | if (kgem->need_expire) { |
4450 | if (kgem->need_expire) { |
4451 | kgem_cleanup_cache(kgem); |
4451 | kgem_cleanup_cache(kgem); |
4452 | goto retry; |
4452 | goto retry; |
4453 | } |
4453 | } |
4454 | 4454 | ||
4455 | ErrorF("%s: failed to mmap handle=%d, %d bytes, into CPU domain: %d\n", |
4455 | ErrorF("%s: failed to mmap handle=%d, %d bytes, into CPU domain: %d\n", |
4456 | __FUNCTION__, bo->handle, bytes(bo), err); |
4456 | __FUNCTION__, bo->handle, bytes(bo), err); |
4457 | return NULL; |
4457 | return NULL; |
4458 | } |
4458 | } |
4459 | 4459 | ||
4460 | VG(VALGRIND_MAKE_MEM_DEFINED(mmap_arg.addr_ptr, bytes(bo))); |
4460 | VG(VALGRIND_MAKE_MEM_DEFINED(mmap_arg.addr_ptr, bytes(bo))); |
4461 | if (bo->map && bo->domain == DOMAIN_CPU) { |
4461 | if (bo->map && bo->domain == DOMAIN_CPU) { |
4462 | DBG(("%s: discarding GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4462 | DBG(("%s: discarding GTT vma for %d\n", __FUNCTION__, bo->handle)); |
4463 | kgem_bo_release_map(kgem, bo); |
4463 | kgem_bo_release_map(kgem, bo); |
4464 | } |
4464 | } |
4465 | if (bo->map == NULL) { |
4465 | if (bo->map == NULL) { |
4466 | DBG(("%s: caching CPU vma for %d\n", __FUNCTION__, bo->handle)); |
4466 | DBG(("%s: caching CPU vma for %d\n", __FUNCTION__, bo->handle)); |
4467 | bo->map = MAKE_CPU_MAP(mmap_arg.addr_ptr); |
4467 | bo->map = MAKE_CPU_MAP(mmap_arg.addr_ptr); |
4468 | } |
4468 | } |
4469 | return (void *)(uintptr_t)mmap_arg.addr_ptr; |
4469 | return (void *)(uintptr_t)mmap_arg.addr_ptr; |
4470 | } |
4470 | } |
4471 | void kgem_bo_sync__cpu(struct kgem *kgem, struct kgem_bo *bo) |
4471 | void kgem_bo_sync__cpu(struct kgem *kgem, struct kgem_bo *bo) |
4472 | { |
4472 | { |
4473 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
4473 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
4474 | assert(!bo->scanout); |
4474 | assert(!bo->scanout); |
4475 | kgem_bo_submit(kgem, bo); |
4475 | kgem_bo_submit(kgem, bo); |
4476 | 4476 | ||
4477 | /* SHM pixmaps use proxies for subpage offsets */ |
4477 | /* SHM pixmaps use proxies for subpage offsets */ |
4478 | assert(!bo->purged); |
4478 | assert(!bo->purged); |
4479 | while (bo->proxy) |
4479 | while (bo->proxy) |
4480 | bo = bo->proxy; |
4480 | bo = bo->proxy; |
4481 | assert(!bo->purged); |
4481 | assert(!bo->purged); |
4482 | 4482 | ||
4483 | if (bo->domain != DOMAIN_CPU || FORCE_MMAP_SYNC & (1 << DOMAIN_CPU)) { |
4483 | if (bo->domain != DOMAIN_CPU || FORCE_MMAP_SYNC & (1 << DOMAIN_CPU)) { |
4484 | struct drm_i915_gem_set_domain set_domain; |
4484 | struct drm_i915_gem_set_domain set_domain; |
4485 | 4485 | ||
4486 | DBG(("%s: SYNC: handle=%d, needs_flush? %d, domain? %d, busy? %d\n", |
4486 | DBG(("%s: SYNC: handle=%d, needs_flush? %d, domain? %d, busy? %d\n", |
4487 | __FUNCTION__, bo->handle, |
4487 | __FUNCTION__, bo->handle, |
4488 | bo->needs_flush, bo->domain, |
4488 | bo->needs_flush, bo->domain, |
4489 | __kgem_busy(kgem, bo->handle))); |
4489 | __kgem_busy(kgem, bo->handle))); |
4490 | 4490 | ||
4491 | VG_CLEAR(set_domain); |
4491 | VG_CLEAR(set_domain); |
4492 | set_domain.handle = bo->handle; |
4492 | set_domain.handle = bo->handle; |
4493 | set_domain.read_domains = I915_GEM_DOMAIN_CPU; |
4493 | set_domain.read_domains = I915_GEM_DOMAIN_CPU; |
4494 | set_domain.write_domain = I915_GEM_DOMAIN_CPU; |
4494 | set_domain.write_domain = I915_GEM_DOMAIN_CPU; |
4495 | 4495 | ||
4496 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) { |
4496 | if (drmIoctl(kgem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain) == 0) { |
4497 | kgem_bo_retire(kgem, bo); |
4497 | kgem_bo_retire(kgem, bo); |
4498 | bo->domain = DOMAIN_CPU; |
4498 | bo->domain = DOMAIN_CPU; |
4499 | } |
4499 | } |
4500 | } |
4500 | } |
4501 | } |
4501 | } |
4502 | 4502 | ||
4503 | void kgem_clear_dirty(struct kgem *kgem) |
4503 | void kgem_clear_dirty(struct kgem *kgem) |
4504 | { |
4504 | { |
4505 | struct list * const buffers = &kgem->next_request->buffers; |
4505 | struct list * const buffers = &kgem->next_request->buffers; |
4506 | struct kgem_bo *bo; |
4506 | struct kgem_bo *bo; |
4507 | 4507 | ||
4508 | list_for_each_entry(bo, buffers, request) { |
4508 | list_for_each_entry(bo, buffers, request) { |
4509 | if (!bo->gpu_dirty) |
4509 | if (!bo->gpu_dirty) |
4510 | break; |
4510 | break; |
4511 | 4511 | ||
4512 | bo->gpu_dirty = false; |
4512 | bo->gpu_dirty = false; |
4513 | } |
4513 | } |
4514 | } |
4514 | } |
4515 | 4515 | ||
4516 | struct kgem_bo *kgem_create_proxy(struct kgem *kgem, |
4516 | struct kgem_bo *kgem_create_proxy(struct kgem *kgem, |
4517 | struct kgem_bo *target, |
4517 | struct kgem_bo *target, |
4518 | int offset, int length) |
4518 | int offset, int length) |
4519 | { |
4519 | { |
4520 | struct kgem_bo *bo; |
4520 | struct kgem_bo *bo; |
4521 | 4521 | ||
4522 | DBG(("%s: target handle=%d [proxy? %d], offset=%d, length=%d, io=%d\n", |
4522 | DBG(("%s: target handle=%d [proxy? %d], offset=%d, length=%d, io=%d\n", |
4523 | __FUNCTION__, target->handle, target->proxy ? target->proxy->delta : -1, |
4523 | __FUNCTION__, target->handle, target->proxy ? target->proxy->delta : -1, |
4524 | offset, length, target->io)); |
4524 | offset, length, target->io)); |
4525 | 4525 | ||
4526 | bo = __kgem_bo_alloc(target->handle, length); |
4526 | bo = __kgem_bo_alloc(target->handle, length); |
4527 | if (bo == NULL) |
4527 | if (bo == NULL) |
4528 | return NULL; |
4528 | return NULL; |
4529 | 4529 | ||
4530 | bo->unique_id = kgem_get_unique_id(kgem); |
4530 | bo->unique_id = kgem_get_unique_id(kgem); |
4531 | bo->reusable = false; |
4531 | bo->reusable = false; |
4532 | bo->size.bytes = length; |
4532 | bo->size.bytes = length; |
4533 | 4533 | ||
4534 | bo->io = target->io && target->proxy == NULL; |
4534 | bo->io = target->io && target->proxy == NULL; |
4535 | bo->gpu_dirty = target->gpu_dirty; |
4535 | bo->gpu_dirty = target->gpu_dirty; |
4536 | bo->tiling = target->tiling; |
4536 | bo->tiling = target->tiling; |
4537 | bo->pitch = target->pitch; |
4537 | bo->pitch = target->pitch; |
4538 | bo->flush = target->flush; |
4538 | bo->flush = target->flush; |
4539 | bo->snoop = target->snoop; |
4539 | bo->snoop = target->snoop; |
4540 | 4540 | ||
4541 | assert(!bo->scanout); |
4541 | assert(!bo->scanout); |
4542 | bo->proxy = kgem_bo_reference(target); |
4542 | bo->proxy = kgem_bo_reference(target); |
4543 | bo->delta = offset; |
4543 | bo->delta = offset; |
4544 | 4544 | ||
4545 | if (target->exec) { |
4545 | if (target->exec) { |
4546 | list_move_tail(&bo->request, &kgem->next_request->buffers); |
4546 | list_move_tail(&bo->request, &kgem->next_request->buffers); |
4547 | bo->exec = &_kgem_dummy_exec; |
4547 | bo->exec = &_kgem_dummy_exec; |
4548 | } |
4548 | } |
4549 | bo->rq = target->rq; |
4549 | bo->rq = target->rq; |
4550 | 4550 | ||
4551 | return bo; |
4551 | return bo; |
4552 | } |
4552 | } |
4553 | 4553 | ||
4554 | #if 0 |
4554 | #if 0 |
4555 | static struct kgem_buffer * |
4555 | static struct kgem_buffer * |
4556 | buffer_alloc(void) |
4556 | buffer_alloc(void) |
4557 | { |
4557 | { |
4558 | struct kgem_buffer *bo; |
4558 | struct kgem_buffer *bo; |
4559 | 4559 | ||
4560 | bo = malloc(sizeof(*bo)); |
4560 | bo = malloc(sizeof(*bo)); |
4561 | if (bo == NULL) |
4561 | if (bo == NULL) |
4562 | return NULL; |
4562 | return NULL; |
4563 | 4563 | ||
4564 | bo->mem = NULL; |
4564 | bo->mem = NULL; |
4565 | bo->need_io = false; |
4565 | bo->need_io = false; |
4566 | bo->mmapped = true; |
4566 | bo->mmapped = true; |
4567 | 4567 | ||
4568 | return bo; |
4568 | return bo; |
4569 | } |
4569 | } |
4570 | 4570 | ||
4571 | static struct kgem_buffer * |
4571 | static struct kgem_buffer * |
4572 | buffer_alloc_with_data(int num_pages) |
4572 | buffer_alloc_with_data(int num_pages) |
4573 | { |
4573 | { |
4574 | struct kgem_buffer *bo; |
4574 | struct kgem_buffer *bo; |
4575 | 4575 | ||
4576 | bo = malloc(sizeof(*bo) + 2*UPLOAD_ALIGNMENT + num_pages * PAGE_SIZE); |
4576 | bo = malloc(sizeof(*bo) + 2*UPLOAD_ALIGNMENT + num_pages * PAGE_SIZE); |
4577 | if (bo == NULL) |
4577 | if (bo == NULL) |
4578 | return NULL; |
4578 | return NULL; |
4579 | 4579 | ||
4580 | bo->mem = (void *)ALIGN((uintptr_t)bo + sizeof(*bo), UPLOAD_ALIGNMENT); |
4580 | bo->mem = (void *)ALIGN((uintptr_t)bo + sizeof(*bo), UPLOAD_ALIGNMENT); |
4581 | bo->mmapped = false; |
4581 | bo->mmapped = false; |
4582 | return bo; |
4582 | return bo; |
4583 | } |
4583 | } |
4584 | 4584 | ||
4585 | static inline bool |
4585 | static inline bool |
4586 | use_snoopable_buffer(struct kgem *kgem, uint32_t flags) |
4586 | use_snoopable_buffer(struct kgem *kgem, uint32_t flags) |
4587 | { |
4587 | { |
4588 | if ((flags & KGEM_BUFFER_WRITE) == 0) |
4588 | if ((flags & KGEM_BUFFER_WRITE) == 0) |
4589 | return kgem->gen >= 030; |
4589 | return kgem->gen >= 030; |
4590 | 4590 | ||
4591 | return true; |
4591 | return true; |
4592 | } |
4592 | } |
4593 | 4593 | ||
4594 | static void |
4594 | static void |
4595 | init_buffer_from_bo(struct kgem_buffer *bo, struct kgem_bo *old) |
4595 | init_buffer_from_bo(struct kgem_buffer *bo, struct kgem_bo *old) |
4596 | { |
4596 | { |
4597 | DBG(("%s: reusing handle=%d for buffer\n", |
4597 | DBG(("%s: reusing handle=%d for buffer\n", |
4598 | __FUNCTION__, old->handle)); |
4598 | __FUNCTION__, old->handle)); |
4599 | 4599 | ||
4600 | assert(old->proxy == NULL); |
4600 | assert(old->proxy == NULL); |
4601 | 4601 | ||
4602 | memcpy(&bo->base, old, sizeof(*old)); |
4602 | memcpy(&bo->base, old, sizeof(*old)); |
4603 | if (old->rq) |
4603 | if (old->rq) |
4604 | list_replace(&old->request, &bo->base.request); |
4604 | list_replace(&old->request, &bo->base.request); |
4605 | else |
4605 | else |
4606 | list_init(&bo->base.request); |
4606 | list_init(&bo->base.request); |
4607 | list_replace(&old->vma, &bo->base.vma); |
4607 | list_replace(&old->vma, &bo->base.vma); |
4608 | list_init(&bo->base.list); |
4608 | list_init(&bo->base.list); |
4609 | free(old); |
4609 | free(old); |
4610 | 4610 | ||
4611 | assert(bo->base.tiling == I915_TILING_NONE); |
4611 | assert(bo->base.tiling == I915_TILING_NONE); |
4612 | 4612 | ||
4613 | bo->base.refcnt = 1; |
4613 | bo->base.refcnt = 1; |
4614 | } |
4614 | } |
4615 | 4615 | ||
4616 | static struct kgem_buffer * |
4616 | static struct kgem_buffer * |
4617 | search_snoopable_buffer(struct kgem *kgem, unsigned alloc) |
4617 | search_snoopable_buffer(struct kgem *kgem, unsigned alloc) |
4618 | { |
4618 | { |
4619 | struct kgem_buffer *bo; |
4619 | struct kgem_buffer *bo; |
4620 | struct kgem_bo *old; |
4620 | struct kgem_bo *old; |
4621 | 4621 | ||
4622 | old = search_snoop_cache(kgem, alloc, 0); |
4622 | old = search_snoop_cache(kgem, alloc, 0); |
4623 | if (old) { |
4623 | if (old) { |
4624 | if (!old->io) { |
4624 | if (!old->io) { |
4625 | bo = buffer_alloc(); |
4625 | bo = buffer_alloc(); |
4626 | if (bo == NULL) |
4626 | if (bo == NULL) |
4627 | return NULL; |
4627 | return NULL; |
4628 | 4628 | ||
4629 | init_buffer_from_bo(bo, old); |
4629 | init_buffer_from_bo(bo, old); |
4630 | } else { |
4630 | } else { |
4631 | bo = (struct kgem_buffer *)old; |
4631 | bo = (struct kgem_buffer *)old; |
4632 | bo->base.refcnt = 1; |
4632 | bo->base.refcnt = 1; |
4633 | } |
4633 | } |
4634 | 4634 | ||
4635 | DBG(("%s: created CPU handle=%d for buffer, size %d\n", |
4635 | DBG(("%s: created CPU handle=%d for buffer, size %d\n", |
4636 | __FUNCTION__, bo->base.handle, num_pages(&bo->base))); |
4636 | __FUNCTION__, bo->base.handle, num_pages(&bo->base))); |
4637 | 4637 | ||
4638 | assert(bo->base.snoop); |
4638 | assert(bo->base.snoop); |
4639 | assert(bo->base.tiling == I915_TILING_NONE); |
4639 | assert(bo->base.tiling == I915_TILING_NONE); |
4640 | assert(num_pages(&bo->base) >= alloc); |
4640 | assert(num_pages(&bo->base) >= alloc); |
4641 | assert(bo->mmapped == true); |
4641 | assert(bo->mmapped == true); |
4642 | assert(bo->need_io == false); |
4642 | assert(bo->need_io == false); |
4643 | 4643 | ||
4644 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4644 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4645 | if (bo->mem == NULL) { |
4645 | if (bo->mem == NULL) { |
4646 | bo->base.refcnt = 0; |
4646 | bo->base.refcnt = 0; |
4647 | kgem_bo_free(kgem, &bo->base); |
4647 | kgem_bo_free(kgem, &bo->base); |
4648 | bo = NULL; |
4648 | bo = NULL; |
4649 | } |
4649 | } |
4650 | 4650 | ||
4651 | return bo; |
4651 | return bo; |
4652 | } |
4652 | } |
4653 | 4653 | ||
4654 | return NULL; |
4654 | return NULL; |
4655 | } |
4655 | } |
4656 | 4656 | ||
4657 | static struct kgem_buffer * |
4657 | static struct kgem_buffer * |
4658 | create_snoopable_buffer(struct kgem *kgem, unsigned alloc) |
4658 | create_snoopable_buffer(struct kgem *kgem, unsigned alloc) |
4659 | { |
4659 | { |
4660 | struct kgem_buffer *bo; |
4660 | struct kgem_buffer *bo; |
4661 | uint32_t handle; |
4661 | uint32_t handle; |
4662 | 4662 | ||
4663 | if (kgem->has_llc) { |
4663 | if (kgem->has_llc) { |
4664 | struct kgem_bo *old; |
4664 | struct kgem_bo *old; |
4665 | 4665 | ||
4666 | bo = buffer_alloc(); |
4666 | bo = buffer_alloc(); |
4667 | if (bo == NULL) |
4667 | if (bo == NULL) |
4668 | return NULL; |
4668 | return NULL; |
4669 | 4669 | ||
4670 | old = search_linear_cache(kgem, alloc, |
4670 | old = search_linear_cache(kgem, alloc, |
4671 | CREATE_INACTIVE | CREATE_CPU_MAP | CREATE_EXACT); |
4671 | CREATE_INACTIVE | CREATE_CPU_MAP | CREATE_EXACT); |
4672 | if (old) { |
4672 | if (old) { |
4673 | init_buffer_from_bo(bo, old); |
4673 | init_buffer_from_bo(bo, old); |
4674 | } else { |
4674 | } else { |
4675 | handle = gem_create(kgem->fd, alloc); |
4675 | handle = gem_create(kgem->fd, alloc); |
4676 | if (handle == 0) { |
4676 | if (handle == 0) { |
4677 | free(bo); |
4677 | free(bo); |
4678 | return NULL; |
4678 | return NULL; |
4679 | } |
4679 | } |
4680 | 4680 | ||
4681 | debug_alloc(kgem, alloc); |
4681 | debug_alloc(kgem, alloc); |
4682 | __kgem_bo_init(&bo->base, handle, alloc); |
4682 | __kgem_bo_init(&bo->base, handle, alloc); |
4683 | DBG(("%s: created CPU (LLC) handle=%d for buffer, size %d\n", |
4683 | DBG(("%s: created CPU (LLC) handle=%d for buffer, size %d\n", |
4684 | __FUNCTION__, bo->base.handle, alloc)); |
4684 | __FUNCTION__, bo->base.handle, alloc)); |
4685 | } |
4685 | } |
4686 | 4686 | ||
4687 | assert(bo->base.refcnt == 1); |
4687 | assert(bo->base.refcnt == 1); |
4688 | assert(bo->mmapped == true); |
4688 | assert(bo->mmapped == true); |
4689 | assert(bo->need_io == false); |
4689 | assert(bo->need_io == false); |
4690 | 4690 | ||
4691 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4691 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4692 | if (bo->mem != NULL) |
4692 | if (bo->mem != NULL) |
4693 | return bo; |
4693 | return bo; |
4694 | 4694 | ||
4695 | bo->base.refcnt = 0; /* for valgrind */ |
4695 | bo->base.refcnt = 0; /* for valgrind */ |
4696 | kgem_bo_free(kgem, &bo->base); |
4696 | kgem_bo_free(kgem, &bo->base); |
4697 | } |
4697 | } |
4698 | 4698 | ||
4699 | if (kgem->has_caching) { |
4699 | if (kgem->has_caching) { |
4700 | struct kgem_bo *old; |
4700 | struct kgem_bo *old; |
4701 | 4701 | ||
4702 | bo = buffer_alloc(); |
4702 | bo = buffer_alloc(); |
4703 | if (bo == NULL) |
4703 | if (bo == NULL) |
4704 | return NULL; |
4704 | return NULL; |
4705 | 4705 | ||
4706 | old = search_linear_cache(kgem, alloc, |
4706 | old = search_linear_cache(kgem, alloc, |
4707 | CREATE_INACTIVE | CREATE_CPU_MAP | CREATE_EXACT); |
4707 | CREATE_INACTIVE | CREATE_CPU_MAP | CREATE_EXACT); |
4708 | if (old) { |
4708 | if (old) { |
4709 | init_buffer_from_bo(bo, old); |
4709 | init_buffer_from_bo(bo, old); |
4710 | } else { |
4710 | } else { |
4711 | handle = gem_create(kgem->fd, alloc); |
4711 | handle = gem_create(kgem->fd, alloc); |
4712 | if (handle == 0) { |
4712 | if (handle == 0) { |
4713 | free(bo); |
4713 | free(bo); |
4714 | return NULL; |
4714 | return NULL; |
4715 | } |
4715 | } |
4716 | 4716 | ||
4717 | debug_alloc(kgem, alloc); |
4717 | debug_alloc(kgem, alloc); |
4718 | __kgem_bo_init(&bo->base, handle, alloc); |
4718 | __kgem_bo_init(&bo->base, handle, alloc); |
4719 | DBG(("%s: created CPU handle=%d for buffer, size %d\n", |
4719 | DBG(("%s: created CPU handle=%d for buffer, size %d\n", |
4720 | __FUNCTION__, bo->base.handle, alloc)); |
4720 | __FUNCTION__, bo->base.handle, alloc)); |
4721 | } |
4721 | } |
4722 | 4722 | ||
4723 | assert(bo->base.refcnt == 1); |
4723 | assert(bo->base.refcnt == 1); |
4724 | assert(bo->mmapped == true); |
4724 | assert(bo->mmapped == true); |
4725 | assert(bo->need_io == false); |
4725 | assert(bo->need_io == false); |
4726 | 4726 | ||
4727 | if (!gem_set_caching(kgem->fd, bo->base.handle, SNOOPED)) |
4727 | if (!gem_set_caching(kgem->fd, bo->base.handle, SNOOPED)) |
4728 | goto free_caching; |
4728 | goto free_caching; |
4729 | 4729 | ||
4730 | bo->base.snoop = true; |
4730 | bo->base.snoop = true; |
4731 | 4731 | ||
4732 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4732 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4733 | if (bo->mem == NULL) |
4733 | if (bo->mem == NULL) |
4734 | goto free_caching; |
4734 | goto free_caching; |
4735 | 4735 | ||
4736 | return bo; |
4736 | return bo; |
4737 | 4737 | ||
4738 | free_caching: |
4738 | free_caching: |
4739 | bo->base.refcnt = 0; /* for valgrind */ |
4739 | bo->base.refcnt = 0; /* for valgrind */ |
4740 | kgem_bo_free(kgem, &bo->base); |
4740 | kgem_bo_free(kgem, &bo->base); |
4741 | } |
4741 | } |
4742 | 4742 | ||
4743 | if (kgem->has_userptr) { |
4743 | if (kgem->has_userptr) { |
4744 | bo = buffer_alloc(); |
4744 | bo = buffer_alloc(); |
4745 | if (bo == NULL) |
4745 | if (bo == NULL) |
4746 | return NULL; |
4746 | return NULL; |
4747 | 4747 | ||
4748 | //if (posix_memalign(&ptr, 64, ALIGN(size, 64))) |
4748 | //if (posix_memalign(&ptr, 64, ALIGN(size, 64))) |
4749 | if (posix_memalign(&bo->mem, PAGE_SIZE, alloc * PAGE_SIZE)) { |
4749 | if (posix_memalign(&bo->mem, PAGE_SIZE, alloc * PAGE_SIZE)) { |
4750 | free(bo); |
4750 | free(bo); |
4751 | return NULL; |
4751 | return NULL; |
4752 | } |
4752 | } |
4753 | 4753 | ||
4754 | handle = gem_userptr(kgem->fd, bo->mem, alloc * PAGE_SIZE, false); |
4754 | handle = gem_userptr(kgem->fd, bo->mem, alloc * PAGE_SIZE, false); |
4755 | if (handle == 0) { |
4755 | if (handle == 0) { |
4756 | free(bo->mem); |
4756 | free(bo->mem); |
4757 | free(bo); |
4757 | free(bo); |
4758 | return NULL; |
4758 | return NULL; |
4759 | } |
4759 | } |
4760 | 4760 | ||
4761 | debug_alloc(kgem, alloc); |
4761 | debug_alloc(kgem, alloc); |
4762 | __kgem_bo_init(&bo->base, handle, alloc); |
4762 | __kgem_bo_init(&bo->base, handle, alloc); |
4763 | DBG(("%s: created snoop handle=%d for buffer\n", |
4763 | DBG(("%s: created snoop handle=%d for buffer\n", |
4764 | __FUNCTION__, bo->base.handle)); |
4764 | __FUNCTION__, bo->base.handle)); |
4765 | 4765 | ||
4766 | assert(bo->mmapped == true); |
4766 | assert(bo->mmapped == true); |
4767 | assert(bo->need_io == false); |
4767 | assert(bo->need_io == false); |
4768 | 4768 | ||
4769 | bo->base.refcnt = 1; |
4769 | bo->base.refcnt = 1; |
4770 | bo->base.snoop = true; |
4770 | bo->base.snoop = true; |
4771 | bo->base.map = MAKE_USER_MAP(bo->mem); |
4771 | bo->base.map = MAKE_USER_MAP(bo->mem); |
4772 | 4772 | ||
4773 | return bo; |
4773 | return bo; |
4774 | } |
4774 | } |
4775 | 4775 | ||
4776 | return NULL; |
4776 | return NULL; |
4777 | } |
4777 | } |
4778 | 4778 | ||
4779 | struct kgem_bo *kgem_create_buffer(struct kgem *kgem, |
4779 | struct kgem_bo *kgem_create_buffer(struct kgem *kgem, |
4780 | uint32_t size, uint32_t flags, |
4780 | uint32_t size, uint32_t flags, |
4781 | void **ret) |
4781 | void **ret) |
4782 | { |
4782 | { |
4783 | struct kgem_buffer *bo; |
4783 | struct kgem_buffer *bo; |
4784 | unsigned offset, alloc; |
4784 | unsigned offset, alloc; |
4785 | struct kgem_bo *old; |
4785 | struct kgem_bo *old; |
4786 | 4786 | ||
4787 | DBG(("%s: size=%d, flags=%x [write?=%d, inplace?=%d, last?=%d]\n", |
4787 | DBG(("%s: size=%d, flags=%x [write?=%d, inplace?=%d, last?=%d]\n", |
4788 | __FUNCTION__, size, flags, |
4788 | __FUNCTION__, size, flags, |
4789 | !!(flags & KGEM_BUFFER_WRITE), |
4789 | !!(flags & KGEM_BUFFER_WRITE), |
4790 | !!(flags & KGEM_BUFFER_INPLACE), |
4790 | !!(flags & KGEM_BUFFER_INPLACE), |
4791 | !!(flags & KGEM_BUFFER_LAST))); |
4791 | !!(flags & KGEM_BUFFER_LAST))); |
4792 | assert(size); |
4792 | assert(size); |
4793 | /* we should never be asked to create anything TOO large */ |
4793 | /* we should never be asked to create anything TOO large */ |
4794 | assert(size <= kgem->max_object_size); |
4794 | assert(size <= kgem->max_object_size); |
4795 | 4795 | ||
4796 | #if !DBG_NO_UPLOAD_CACHE |
4796 | #if !DBG_NO_UPLOAD_CACHE |
4797 | list_for_each_entry(bo, &kgem->batch_buffers, base.list) { |
4797 | list_for_each_entry(bo, &kgem->batch_buffers, base.list) { |
4798 | assert(bo->base.io); |
4798 | assert(bo->base.io); |
4799 | assert(bo->base.refcnt >= 1); |
4799 | assert(bo->base.refcnt >= 1); |
4800 | 4800 | ||
4801 | /* We can reuse any write buffer which we can fit */ |
4801 | /* We can reuse any write buffer which we can fit */ |
4802 | if (flags == KGEM_BUFFER_LAST && |
4802 | if (flags == KGEM_BUFFER_LAST && |
4803 | bo->write == KGEM_BUFFER_WRITE && |
4803 | bo->write == KGEM_BUFFER_WRITE && |
4804 | bo->base.refcnt == 1 && !bo->mmapped && |
4804 | bo->base.refcnt == 1 && !bo->mmapped && |
4805 | size <= bytes(&bo->base)) { |
4805 | size <= bytes(&bo->base)) { |
4806 | DBG(("%s: reusing write buffer for read of %d bytes? used=%d, total=%d\n", |
4806 | DBG(("%s: reusing write buffer for read of %d bytes? used=%d, total=%d\n", |
4807 | __FUNCTION__, size, bo->used, bytes(&bo->base))); |
4807 | __FUNCTION__, size, bo->used, bytes(&bo->base))); |
4808 | gem_write(kgem->fd, bo->base.handle, |
4808 | gem_write(kgem->fd, bo->base.handle, |
4809 | 0, bo->used, bo->mem); |
4809 | 0, bo->used, bo->mem); |
4810 | kgem_buffer_release(kgem, bo); |
4810 | kgem_buffer_release(kgem, bo); |
4811 | bo->need_io = 0; |
4811 | bo->need_io = 0; |
4812 | bo->write = 0; |
4812 | bo->write = 0; |
4813 | offset = 0; |
4813 | offset = 0; |
4814 | bo->used = size; |
4814 | bo->used = size; |
4815 | goto done; |
4815 | goto done; |
4816 | } |
4816 | } |
4817 | 4817 | ||
4818 | if (flags & KGEM_BUFFER_WRITE) { |
4818 | if (flags & KGEM_BUFFER_WRITE) { |
4819 | if ((bo->write & KGEM_BUFFER_WRITE) == 0 || |
4819 | if ((bo->write & KGEM_BUFFER_WRITE) == 0 || |
4820 | (((bo->write & ~flags) & KGEM_BUFFER_INPLACE) && |
4820 | (((bo->write & ~flags) & KGEM_BUFFER_INPLACE) && |
4821 | !bo->base.snoop)) { |
4821 | !bo->base.snoop)) { |
4822 | DBG(("%s: skip write %x buffer, need %x\n", |
4822 | DBG(("%s: skip write %x buffer, need %x\n", |
4823 | __FUNCTION__, bo->write, flags)); |
4823 | __FUNCTION__, bo->write, flags)); |
4824 | continue; |
4824 | continue; |
4825 | } |
4825 | } |
4826 | assert(bo->mmapped || bo->need_io); |
4826 | assert(bo->mmapped || bo->need_io); |
4827 | } else { |
4827 | } else { |
4828 | if (bo->write & KGEM_BUFFER_WRITE) { |
4828 | if (bo->write & KGEM_BUFFER_WRITE) { |
4829 | DBG(("%s: skip write %x buffer, need %x\n", |
4829 | DBG(("%s: skip write %x buffer, need %x\n", |
4830 | __FUNCTION__, bo->write, flags)); |
4830 | __FUNCTION__, bo->write, flags)); |
4831 | continue; |
4831 | continue; |
4832 | } |
4832 | } |
4833 | } |
4833 | } |
4834 | 4834 | ||
4835 | if (bo->used + size <= bytes(&bo->base)) { |
4835 | if (bo->used + size <= bytes(&bo->base)) { |
4836 | DBG(("%s: reusing buffer? used=%d + size=%d, total=%d\n", |
4836 | DBG(("%s: reusing buffer? used=%d + size=%d, total=%d\n", |
4837 | __FUNCTION__, bo->used, size, bytes(&bo->base))); |
4837 | __FUNCTION__, bo->used, size, bytes(&bo->base))); |
4838 | offset = bo->used; |
4838 | offset = bo->used; |
4839 | bo->used += size; |
4839 | bo->used += size; |
4840 | goto done; |
4840 | goto done; |
4841 | } |
4841 | } |
4842 | } |
4842 | } |
4843 | 4843 | ||
4844 | if (flags & KGEM_BUFFER_WRITE) { |
4844 | if (flags & KGEM_BUFFER_WRITE) { |
4845 | list_for_each_entry(bo, &kgem->active_buffers, base.list) { |
4845 | list_for_each_entry(bo, &kgem->active_buffers, base.list) { |
4846 | assert(bo->base.io); |
4846 | assert(bo->base.io); |
4847 | assert(bo->base.refcnt >= 1); |
4847 | assert(bo->base.refcnt >= 1); |
4848 | assert(bo->mmapped); |
4848 | assert(bo->mmapped); |
4849 | assert(!IS_CPU_MAP(bo->base.map) || kgem->has_llc || bo->base.snoop); |
4849 | assert(!IS_CPU_MAP(bo->base.map) || kgem->has_llc || bo->base.snoop); |
4850 | 4850 | ||
4851 | if (!kgem->has_llc && (bo->write & ~flags) & KGEM_BUFFER_INPLACE) { |
4851 | if (!kgem->has_llc && (bo->write & ~flags) & KGEM_BUFFER_INPLACE) { |
4852 | DBG(("%s: skip write %x buffer, need %x\n", |
4852 | DBG(("%s: skip write %x buffer, need %x\n", |
4853 | __FUNCTION__, bo->write, flags)); |
4853 | __FUNCTION__, bo->write, flags)); |
4854 | continue; |
4854 | continue; |
4855 | } |
4855 | } |
4856 | 4856 | ||
4857 | if (bo->used + size <= bytes(&bo->base)) { |
4857 | if (bo->used + size <= bytes(&bo->base)) { |
4858 | DBG(("%s: reusing buffer? used=%d + size=%d, total=%d\n", |
4858 | DBG(("%s: reusing buffer? used=%d + size=%d, total=%d\n", |
4859 | __FUNCTION__, bo->used, size, bytes(&bo->base))); |
4859 | __FUNCTION__, bo->used, size, bytes(&bo->base))); |
4860 | offset = bo->used; |
4860 | offset = bo->used; |
4861 | bo->used += size; |
4861 | bo->used += size; |
4862 | list_move(&bo->base.list, &kgem->batch_buffers); |
4862 | list_move(&bo->base.list, &kgem->batch_buffers); |
4863 | goto done; |
4863 | goto done; |
4864 | } |
4864 | } |
4865 | } |
4865 | } |
4866 | } |
4866 | } |
4867 | #endif |
4867 | #endif |
4868 | 4868 | ||
4869 | #if !DBG_NO_MAP_UPLOAD |
4869 | #if !DBG_NO_MAP_UPLOAD |
4870 | /* Be a little more generous and hope to hold fewer mmappings */ |
4870 | /* Be a little more generous and hope to hold fewer mmappings */ |
4871 | alloc = ALIGN(2*size, kgem->buffer_size); |
4871 | alloc = ALIGN(2*size, kgem->buffer_size); |
4872 | if (alloc > MAX_CACHE_SIZE) |
4872 | if (alloc > MAX_CACHE_SIZE) |
4873 | alloc = ALIGN(size, kgem->buffer_size); |
4873 | alloc = ALIGN(size, kgem->buffer_size); |
4874 | if (alloc > MAX_CACHE_SIZE) |
4874 | if (alloc > MAX_CACHE_SIZE) |
4875 | alloc = PAGE_ALIGN(size); |
4875 | alloc = PAGE_ALIGN(size); |
4876 | assert(alloc); |
4876 | assert(alloc); |
4877 | 4877 | ||
4878 | if (alloc > kgem->aperture_mappable / 4) |
4878 | if (alloc > kgem->aperture_mappable / 4) |
4879 | flags &= ~KGEM_BUFFER_INPLACE; |
4879 | flags &= ~KGEM_BUFFER_INPLACE; |
4880 | alloc /= PAGE_SIZE; |
4880 | alloc /= PAGE_SIZE; |
4881 | 4881 | ||
4882 | if (kgem->has_llc && |
4882 | if (kgem->has_llc && |
4883 | (flags & KGEM_BUFFER_WRITE_INPLACE) != KGEM_BUFFER_WRITE_INPLACE) { |
4883 | (flags & KGEM_BUFFER_WRITE_INPLACE) != KGEM_BUFFER_WRITE_INPLACE) { |
4884 | bo = buffer_alloc(); |
4884 | bo = buffer_alloc(); |
4885 | if (bo == NULL) |
4885 | if (bo == NULL) |
4886 | goto skip_llc; |
4886 | goto skip_llc; |
4887 | 4887 | ||
4888 | old = NULL; |
4888 | old = NULL; |
4889 | if ((flags & KGEM_BUFFER_WRITE) == 0) |
4889 | if ((flags & KGEM_BUFFER_WRITE) == 0) |
4890 | old = search_linear_cache(kgem, alloc, CREATE_CPU_MAP); |
4890 | old = search_linear_cache(kgem, alloc, CREATE_CPU_MAP); |
4891 | if (old == NULL) |
4891 | if (old == NULL) |
4892 | old = search_linear_cache(kgem, alloc, CREATE_INACTIVE | CREATE_CPU_MAP); |
4892 | old = search_linear_cache(kgem, alloc, CREATE_INACTIVE | CREATE_CPU_MAP); |
4893 | if (old == NULL) |
4893 | if (old == NULL) |
4894 | old = search_linear_cache(kgem, NUM_PAGES(size), CREATE_INACTIVE | CREATE_CPU_MAP); |
4894 | old = search_linear_cache(kgem, NUM_PAGES(size), CREATE_INACTIVE | CREATE_CPU_MAP); |
4895 | if (old) { |
4895 | if (old) { |
4896 | DBG(("%s: found LLC handle=%d for buffer\n", |
4896 | DBG(("%s: found LLC handle=%d for buffer\n", |
4897 | __FUNCTION__, old->handle)); |
4897 | __FUNCTION__, old->handle)); |
4898 | 4898 | ||
4899 | init_buffer_from_bo(bo, old); |
4899 | init_buffer_from_bo(bo, old); |
4900 | } else { |
4900 | } else { |
4901 | uint32_t handle = gem_create(kgem->fd, alloc); |
4901 | uint32_t handle = gem_create(kgem->fd, alloc); |
4902 | if (handle == 0) { |
4902 | if (handle == 0) { |
4903 | free(bo); |
4903 | free(bo); |
4904 | goto skip_llc; |
4904 | goto skip_llc; |
4905 | } |
4905 | } |
4906 | __kgem_bo_init(&bo->base, handle, alloc); |
4906 | __kgem_bo_init(&bo->base, handle, alloc); |
4907 | DBG(("%s: created LLC handle=%d for buffer\n", |
4907 | DBG(("%s: created LLC handle=%d for buffer\n", |
4908 | __FUNCTION__, bo->base.handle)); |
4908 | __FUNCTION__, bo->base.handle)); |
4909 | 4909 | ||
4910 | debug_alloc(kgem, alloc); |
4910 | debug_alloc(kgem, alloc); |
4911 | } |
4911 | } |
4912 | 4912 | ||
4913 | assert(bo->mmapped); |
4913 | assert(bo->mmapped); |
4914 | assert(!bo->need_io); |
4914 | assert(!bo->need_io); |
4915 | 4915 | ||
4916 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4916 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
4917 | if (bo->mem) { |
4917 | if (bo->mem) { |
4918 | if (flags & KGEM_BUFFER_WRITE) |
4918 | if (flags & KGEM_BUFFER_WRITE) |
4919 | kgem_bo_sync__cpu(kgem, &bo->base); |
4919 | kgem_bo_sync__cpu(kgem, &bo->base); |
4920 | flags &= ~KGEM_BUFFER_INPLACE; |
4920 | flags &= ~KGEM_BUFFER_INPLACE; |
4921 | goto init; |
4921 | goto init; |
4922 | } else { |
4922 | } else { |
4923 | bo->base.refcnt = 0; /* for valgrind */ |
4923 | bo->base.refcnt = 0; /* for valgrind */ |
4924 | kgem_bo_free(kgem, &bo->base); |
4924 | kgem_bo_free(kgem, &bo->base); |
4925 | } |
4925 | } |
4926 | } |
4926 | } |
4927 | skip_llc: |
4927 | skip_llc: |
4928 | 4928 | ||
4929 | if ((flags & KGEM_BUFFER_WRITE_INPLACE) == KGEM_BUFFER_WRITE_INPLACE) { |
4929 | if ((flags & KGEM_BUFFER_WRITE_INPLACE) == KGEM_BUFFER_WRITE_INPLACE) { |
4930 | /* The issue with using a GTT upload buffer is that we may |
4930 | /* The issue with using a GTT upload buffer is that we may |
4931 | * cause eviction-stalls in order to free up some GTT space. |
4931 | * cause eviction-stalls in order to free up some GTT space. |
4932 | * An is-mappable? ioctl could help us detect when we are |
4932 | * An is-mappable? ioctl could help us detect when we are |
4933 | * about to block, or some per-page magic in the kernel. |
4933 | * about to block, or some per-page magic in the kernel. |
4934 | * |
4934 | * |
4935 | * XXX This is especially noticeable on memory constrained |
4935 | * XXX This is especially noticeable on memory constrained |
4936 | * devices like gen2 or with relatively slow gpu like i3. |
4936 | * devices like gen2 or with relatively slow gpu like i3. |
4937 | */ |
4937 | */ |
4938 | DBG(("%s: searching for an inactive GTT map for upload\n", |
4938 | DBG(("%s: searching for an inactive GTT map for upload\n", |
4939 | __FUNCTION__)); |
4939 | __FUNCTION__)); |
4940 | old = search_linear_cache(kgem, alloc, |
4940 | old = search_linear_cache(kgem, alloc, |
4941 | CREATE_EXACT | CREATE_INACTIVE | CREATE_GTT_MAP); |
4941 | CREATE_EXACT | CREATE_INACTIVE | CREATE_GTT_MAP); |
4942 | #if HAVE_I915_GEM_BUFFER_INFO |
4942 | #if HAVE_I915_GEM_BUFFER_INFO |
4943 | if (old) { |
4943 | if (old) { |
4944 | struct drm_i915_gem_buffer_info info; |
4944 | struct drm_i915_gem_buffer_info info; |
4945 | 4945 | ||
4946 | /* An example of such a non-blocking ioctl might work */ |
4946 | /* An example of such a non-blocking ioctl might work */ |
4947 | 4947 | ||
4948 | VG_CLEAR(info); |
4948 | VG_CLEAR(info); |
4949 | info.handle = handle; |
4949 | info.handle = handle; |
4950 | if (drmIoctl(kgem->fd, |
4950 | if (drmIoctl(kgem->fd, |
4951 | DRM_IOCTL_I915_GEM_BUFFER_INFO, |
4951 | DRM_IOCTL_I915_GEM_BUFFER_INFO, |
4952 | &fino) == 0) { |
4952 | &fino) == 0) { |
4953 | old->presumed_offset = info.addr; |
4953 | old->presumed_offset = info.addr; |
4954 | if ((info.flags & I915_GEM_MAPPABLE) == 0) { |
4954 | if ((info.flags & I915_GEM_MAPPABLE) == 0) { |
4955 | kgem_bo_move_to_inactive(kgem, old); |
4955 | kgem_bo_move_to_inactive(kgem, old); |
4956 | old = NULL; |
4956 | old = NULL; |
4957 | } |
4957 | } |
4958 | } |
4958 | } |
4959 | } |
4959 | } |
4960 | #endif |
4960 | #endif |
4961 | if (old == NULL) |
4961 | if (old == NULL) |
4962 | old = search_linear_cache(kgem, NUM_PAGES(size), |
4962 | old = search_linear_cache(kgem, NUM_PAGES(size), |
4963 | CREATE_EXACT | CREATE_INACTIVE | CREATE_GTT_MAP); |
4963 | CREATE_EXACT | CREATE_INACTIVE | CREATE_GTT_MAP); |
4964 | if (old == NULL) { |
4964 | if (old == NULL) { |
4965 | old = search_linear_cache(kgem, alloc, CREATE_INACTIVE); |
4965 | old = search_linear_cache(kgem, alloc, CREATE_INACTIVE); |
4966 | if (old && !__kgem_bo_is_mappable(kgem, old)) { |
4966 | if (old && !__kgem_bo_is_mappable(kgem, old)) { |
4967 | _kgem_bo_destroy(kgem, old); |
4967 | _kgem_bo_destroy(kgem, old); |
4968 | old = NULL; |
4968 | old = NULL; |
4969 | } |
4969 | } |
4970 | } |
4970 | } |
4971 | if (old) { |
4971 | if (old) { |
4972 | DBG(("%s: reusing handle=%d for buffer\n", |
4972 | DBG(("%s: reusing handle=%d for buffer\n", |
4973 | __FUNCTION__, old->handle)); |
4973 | __FUNCTION__, old->handle)); |
4974 | assert(__kgem_bo_is_mappable(kgem, old)); |
4974 | assert(__kgem_bo_is_mappable(kgem, old)); |
4975 | assert(!old->snoop); |
4975 | assert(!old->snoop); |
4976 | assert(old->rq == NULL); |
4976 | assert(old->rq == NULL); |
4977 | 4977 | ||
4978 | bo = buffer_alloc(); |
4978 | bo = buffer_alloc(); |
4979 | if (bo == NULL) |
4979 | if (bo == NULL) |
4980 | return NULL; |
4980 | return NULL; |
4981 | 4981 | ||
4982 | init_buffer_from_bo(bo, old); |
4982 | init_buffer_from_bo(bo, old); |
4983 | assert(num_pages(&bo->base) >= NUM_PAGES(size)); |
4983 | assert(num_pages(&bo->base) >= NUM_PAGES(size)); |
4984 | 4984 | ||
4985 | assert(bo->mmapped); |
4985 | assert(bo->mmapped); |
4986 | assert(bo->base.refcnt == 1); |
4986 | assert(bo->base.refcnt == 1); |
4987 | 4987 | ||
4988 | bo->mem = kgem_bo_map(kgem, &bo->base); |
4988 | bo->mem = kgem_bo_map(kgem, &bo->base); |
4989 | if (bo->mem) { |
4989 | if (bo->mem) { |
4990 | if (IS_CPU_MAP(bo->base.map)) |
4990 | if (IS_CPU_MAP(bo->base.map)) |
4991 | flags &= ~KGEM_BUFFER_INPLACE; |
4991 | flags &= ~KGEM_BUFFER_INPLACE; |
4992 | goto init; |
4992 | goto init; |
4993 | } else { |
4993 | } else { |
4994 | bo->base.refcnt = 0; |
4994 | bo->base.refcnt = 0; |
4995 | kgem_bo_free(kgem, &bo->base); |
4995 | kgem_bo_free(kgem, &bo->base); |
4996 | } |
4996 | } |
4997 | } |
4997 | } |
4998 | } |
4998 | } |
4999 | #else |
4999 | #else |
5000 | flags &= ~KGEM_BUFFER_INPLACE; |
5000 | flags &= ~KGEM_BUFFER_INPLACE; |
5001 | #endif |
5001 | #endif |
5002 | /* Be more parsimonious with pwrite/pread/cacheable buffers */ |
5002 | /* Be more parsimonious with pwrite/pread/cacheable buffers */ |
5003 | if ((flags & KGEM_BUFFER_INPLACE) == 0) |
5003 | if ((flags & KGEM_BUFFER_INPLACE) == 0) |
5004 | alloc = NUM_PAGES(size); |
5004 | alloc = NUM_PAGES(size); |
5005 | 5005 | ||
5006 | if (use_snoopable_buffer(kgem, flags)) { |
5006 | if (use_snoopable_buffer(kgem, flags)) { |
5007 | bo = search_snoopable_buffer(kgem, alloc); |
5007 | bo = search_snoopable_buffer(kgem, alloc); |
5008 | if (bo) { |
5008 | if (bo) { |
5009 | if (flags & KGEM_BUFFER_WRITE) |
5009 | if (flags & KGEM_BUFFER_WRITE) |
5010 | kgem_bo_sync__cpu(kgem, &bo->base); |
5010 | kgem_bo_sync__cpu(kgem, &bo->base); |
5011 | flags &= ~KGEM_BUFFER_INPLACE; |
5011 | flags &= ~KGEM_BUFFER_INPLACE; |
5012 | goto init; |
5012 | goto init; |
5013 | } |
5013 | } |
5014 | 5014 | ||
5015 | if ((flags & KGEM_BUFFER_INPLACE) == 0) { |
5015 | if ((flags & KGEM_BUFFER_INPLACE) == 0) { |
5016 | bo = create_snoopable_buffer(kgem, alloc); |
5016 | bo = create_snoopable_buffer(kgem, alloc); |
5017 | if (bo) |
5017 | if (bo) |
5018 | goto init; |
5018 | goto init; |
5019 | } |
5019 | } |
5020 | } |
5020 | } |
5021 | 5021 | ||
5022 | flags &= ~KGEM_BUFFER_INPLACE; |
5022 | flags &= ~KGEM_BUFFER_INPLACE; |
5023 | 5023 | ||
5024 | old = NULL; |
5024 | old = NULL; |
5025 | if ((flags & KGEM_BUFFER_WRITE) == 0) |
5025 | if ((flags & KGEM_BUFFER_WRITE) == 0) |
5026 | old = search_linear_cache(kgem, alloc, 0); |
5026 | old = search_linear_cache(kgem, alloc, 0); |
5027 | if (old == NULL) |
5027 | if (old == NULL) |
5028 | old = search_linear_cache(kgem, alloc, CREATE_INACTIVE); |
5028 | old = search_linear_cache(kgem, alloc, CREATE_INACTIVE); |
5029 | if (old) { |
5029 | if (old) { |
5030 | DBG(("%s: reusing ordinary handle %d for io\n", |
5030 | DBG(("%s: reusing ordinary handle %d for io\n", |
5031 | __FUNCTION__, old->handle)); |
5031 | __FUNCTION__, old->handle)); |
5032 | bo = buffer_alloc_with_data(num_pages(old)); |
5032 | bo = buffer_alloc_with_data(num_pages(old)); |
5033 | if (bo == NULL) |
5033 | if (bo == NULL) |
5034 | return NULL; |
5034 | return NULL; |
5035 | 5035 | ||
5036 | init_buffer_from_bo(bo, old); |
5036 | init_buffer_from_bo(bo, old); |
5037 | bo->need_io = flags & KGEM_BUFFER_WRITE; |
5037 | bo->need_io = flags & KGEM_BUFFER_WRITE; |
5038 | } else { |
5038 | } else { |
5039 | unsigned hint; |
5039 | unsigned hint; |
5040 | 5040 | ||
5041 | if (use_snoopable_buffer(kgem, flags)) { |
5041 | if (use_snoopable_buffer(kgem, flags)) { |
5042 | bo = create_snoopable_buffer(kgem, alloc); |
5042 | bo = create_snoopable_buffer(kgem, alloc); |
5043 | if (bo) |
5043 | if (bo) |
5044 | goto init; |
5044 | goto init; |
5045 | } |
5045 | } |
5046 | 5046 | ||
5047 | bo = buffer_alloc(); |
5047 | bo = buffer_alloc(); |
5048 | if (bo == NULL) |
5048 | if (bo == NULL) |
5049 | return NULL; |
5049 | return NULL; |
5050 | 5050 | ||
5051 | hint = CREATE_INACTIVE; |
5051 | hint = CREATE_INACTIVE; |
5052 | if (flags & KGEM_BUFFER_WRITE) |
5052 | if (flags & KGEM_BUFFER_WRITE) |
5053 | hint |= CREATE_CPU_MAP; |
5053 | hint |= CREATE_CPU_MAP; |
5054 | old = search_linear_cache(kgem, alloc, hint); |
5054 | old = search_linear_cache(kgem, alloc, hint); |
5055 | if (old) { |
5055 | if (old) { |
5056 | DBG(("%s: reusing handle=%d for buffer\n", |
5056 | DBG(("%s: reusing handle=%d for buffer\n", |
5057 | __FUNCTION__, old->handle)); |
5057 | __FUNCTION__, old->handle)); |
5058 | 5058 | ||
5059 | init_buffer_from_bo(bo, old); |
5059 | init_buffer_from_bo(bo, old); |
5060 | } else { |
5060 | } else { |
5061 | uint32_t handle = gem_create(kgem->fd, alloc); |
5061 | uint32_t handle = gem_create(kgem->fd, alloc); |
5062 | if (handle == 0) { |
5062 | if (handle == 0) { |
5063 | free(bo); |
5063 | free(bo); |
5064 | return NULL; |
5064 | return NULL; |
5065 | } |
5065 | } |
5066 | 5066 | ||
5067 | DBG(("%s: created handle=%d for buffer\n", |
5067 | DBG(("%s: created handle=%d for buffer\n", |
5068 | __FUNCTION__, handle)); |
5068 | __FUNCTION__, handle)); |
5069 | 5069 | ||
5070 | __kgem_bo_init(&bo->base, handle, alloc); |
5070 | __kgem_bo_init(&bo->base, handle, alloc); |
5071 | debug_alloc(kgem, alloc * PAGE_SIZE); |
5071 | debug_alloc(kgem, alloc * PAGE_SIZE); |
5072 | } |
5072 | } |
5073 | 5073 | ||
5074 | assert(bo->mmapped); |
5074 | assert(bo->mmapped); |
5075 | assert(!bo->need_io); |
5075 | assert(!bo->need_io); |
5076 | assert(bo->base.refcnt == 1); |
5076 | assert(bo->base.refcnt == 1); |
5077 | 5077 | ||
5078 | if (flags & KGEM_BUFFER_WRITE) { |
5078 | if (flags & KGEM_BUFFER_WRITE) { |
5079 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
5079 | bo->mem = kgem_bo_map__cpu(kgem, &bo->base); |
5080 | if (bo->mem != NULL) { |
5080 | if (bo->mem != NULL) { |
5081 | kgem_bo_sync__cpu(kgem, &bo->base); |
5081 | kgem_bo_sync__cpu(kgem, &bo->base); |
5082 | goto init; |
5082 | goto init; |
5083 | } |
5083 | } |
5084 | } |
5084 | } |
5085 | 5085 | ||
5086 | DBG(("%s: failing back to new pwrite buffer\n", __FUNCTION__)); |
5086 | DBG(("%s: failing back to new pwrite buffer\n", __FUNCTION__)); |
5087 | old = &bo->base; |
5087 | old = &bo->base; |
5088 | bo = buffer_alloc_with_data(num_pages(old)); |
5088 | bo = buffer_alloc_with_data(num_pages(old)); |
5089 | if (bo == NULL) { |
5089 | if (bo == NULL) { |
5090 | old->refcnt= 0; |
5090 | old->refcnt= 0; |
5091 | kgem_bo_free(kgem, old); |
5091 | kgem_bo_free(kgem, old); |
5092 | return NULL; |
5092 | return NULL; |
5093 | } |
5093 | } |
5094 | 5094 | ||
5095 | init_buffer_from_bo(bo, old); |
5095 | init_buffer_from_bo(bo, old); |
5096 | 5096 | ||
5097 | assert(bo->mem); |
5097 | assert(bo->mem); |
5098 | assert(!bo->mmapped); |
5098 | assert(!bo->mmapped); |
5099 | assert(bo->base.refcnt == 1); |
5099 | assert(bo->base.refcnt == 1); |
5100 | 5100 | ||
5101 | bo->need_io = flags & KGEM_BUFFER_WRITE; |
5101 | bo->need_io = flags & KGEM_BUFFER_WRITE; |
5102 | } |
5102 | } |
5103 | init: |
5103 | init: |
5104 | bo->base.io = true; |
5104 | bo->base.io = true; |
5105 | assert(bo->base.refcnt == 1); |
5105 | assert(bo->base.refcnt == 1); |
5106 | assert(num_pages(&bo->base) >= NUM_PAGES(size)); |
5106 | assert(num_pages(&bo->base) >= NUM_PAGES(size)); |
5107 | assert(!bo->need_io || !bo->base.needs_flush); |
5107 | assert(!bo->need_io || !bo->base.needs_flush); |
5108 | assert(!bo->need_io || bo->base.domain != DOMAIN_GPU); |
5108 | assert(!bo->need_io || bo->base.domain != DOMAIN_GPU); |
5109 | assert(bo->mem); |
5109 | assert(bo->mem); |
5110 | assert(!bo->mmapped || bo->base.map != NULL); |
5110 | assert(!bo->mmapped || bo->base.map != NULL); |
5111 | 5111 | ||
5112 | bo->used = size; |
5112 | bo->used = size; |
5113 | bo->write = flags & KGEM_BUFFER_WRITE_INPLACE; |
5113 | bo->write = flags & KGEM_BUFFER_WRITE_INPLACE; |
5114 | offset = 0; |
5114 | offset = 0; |
5115 | 5115 | ||
5116 | assert(list_is_empty(&bo->base.list)); |
5116 | assert(list_is_empty(&bo->base.list)); |
5117 | list_add(&bo->base.list, &kgem->batch_buffers); |
5117 | list_add(&bo->base.list, &kgem->batch_buffers); |
5118 | 5118 | ||
5119 | DBG(("%s(pages=%d [%d]) new handle=%d, used=%d, write=%d\n", |
5119 | DBG(("%s(pages=%d [%d]) new handle=%d, used=%d, write=%d\n", |
5120 | __FUNCTION__, num_pages(&bo->base), alloc, bo->base.handle, bo->used, bo->write)); |
5120 | __FUNCTION__, num_pages(&bo->base), alloc, bo->base.handle, bo->used, bo->write)); |
5121 | 5121 | ||
5122 | done: |
5122 | done: |
5123 | bo->used = ALIGN(bo->used, UPLOAD_ALIGNMENT); |
5123 | bo->used = ALIGN(bo->used, UPLOAD_ALIGNMENT); |
5124 | assert(bo->mem); |
5124 | assert(bo->mem); |
5125 | *ret = (char *)bo->mem + offset; |
5125 | *ret = (char *)bo->mem + offset; |
5126 | return kgem_create_proxy(kgem, &bo->base, offset, size); |
5126 | return kgem_create_proxy(kgem, &bo->base, offset, size); |
5127 | } |
5127 | } |
5128 | 5128 | ||
5129 | bool kgem_buffer_is_inplace(struct kgem_bo *_bo) |
5129 | bool kgem_buffer_is_inplace(struct kgem_bo *_bo) |
5130 | { |
5130 | { |
5131 | struct kgem_buffer *bo = (struct kgem_buffer *)_bo->proxy; |
5131 | struct kgem_buffer *bo = (struct kgem_buffer *)_bo->proxy; |
5132 | return bo->write & KGEM_BUFFER_WRITE_INPLACE; |
5132 | return bo->write & KGEM_BUFFER_WRITE_INPLACE; |
5133 | } |
5133 | } |
5134 | 5134 | ||
5135 | struct kgem_bo *kgem_create_buffer_2d(struct kgem *kgem, |
5135 | struct kgem_bo *kgem_create_buffer_2d(struct kgem *kgem, |
5136 | int width, int height, int bpp, |
5136 | int width, int height, int bpp, |
5137 | uint32_t flags, |
5137 | uint32_t flags, |
5138 | void **ret) |
5138 | void **ret) |
5139 | { |
5139 | { |
5140 | struct kgem_bo *bo; |
5140 | struct kgem_bo *bo; |
5141 | int stride; |
5141 | int stride; |
5142 | 5142 | ||
5143 | assert(width > 0 && height > 0); |
5143 | assert(width > 0 && height > 0); |
5144 | assert(ret != NULL); |
5144 | assert(ret != NULL); |
5145 | stride = ALIGN(width, 2) * bpp >> 3; |
5145 | stride = ALIGN(width, 2) * bpp >> 3; |
5146 | stride = ALIGN(stride, 4); |
5146 | stride = ALIGN(stride, 4); |
5147 | 5147 | ||
5148 | DBG(("%s: %dx%d, %d bpp, stride=%d\n", |
5148 | DBG(("%s: %dx%d, %d bpp, stride=%d\n", |
5149 | __FUNCTION__, width, height, bpp, stride)); |
5149 | __FUNCTION__, width, height, bpp, stride)); |
5150 | 5150 | ||
5151 | bo = kgem_create_buffer(kgem, stride * ALIGN(height, 2), flags, ret); |
5151 | bo = kgem_create_buffer(kgem, stride * ALIGN(height, 2), flags, ret); |
5152 | if (bo == NULL) { |
5152 | if (bo == NULL) { |
5153 | DBG(("%s: allocation failure for upload buffer\n", |
5153 | DBG(("%s: allocation failure for upload buffer\n", |
5154 | __FUNCTION__)); |
5154 | __FUNCTION__)); |
5155 | return NULL; |
5155 | return NULL; |
5156 | } |
5156 | } |
5157 | assert(*ret != NULL); |
5157 | assert(*ret != NULL); |
5158 | assert(bo->proxy != NULL); |
5158 | assert(bo->proxy != NULL); |
5159 | 5159 | ||
5160 | if (height & 1) { |
5160 | if (height & 1) { |
5161 | struct kgem_buffer *io = (struct kgem_buffer *)bo->proxy; |
5161 | struct kgem_buffer *io = (struct kgem_buffer *)bo->proxy; |
5162 | int min; |
5162 | int min; |
5163 | 5163 | ||
5164 | assert(io->used); |
5164 | assert(io->used); |
5165 | 5165 | ||
5166 | /* Having padded this surface to ensure that accesses to |
5166 | /* Having padded this surface to ensure that accesses to |
5167 | * the last pair of rows is valid, remove the padding so |
5167 | * the last pair of rows is valid, remove the padding so |
5168 | * that it can be allocated to other pixmaps. |
5168 | * that it can be allocated to other pixmaps. |
5169 | */ |
5169 | */ |
5170 | min = bo->delta + height * stride; |
5170 | min = bo->delta + height * stride; |
5171 | min = ALIGN(min, UPLOAD_ALIGNMENT); |
5171 | min = ALIGN(min, UPLOAD_ALIGNMENT); |
5172 | if (io->used != min) { |
5172 | if (io->used != min) { |
5173 | DBG(("%s: trimming buffer from %d to %d\n", |
5173 | DBG(("%s: trimming buffer from %d to %d\n", |
5174 | __FUNCTION__, io->used, min)); |
5174 | __FUNCTION__, io->used, min)); |
5175 | io->used = min; |
5175 | io->used = min; |
5176 | } |
5176 | } |
5177 | bo->size.bytes -= stride; |
5177 | bo->size.bytes -= stride; |
5178 | } |
5178 | } |
5179 | 5179 | ||
5180 | bo->map = MAKE_CPU_MAP(*ret); |
5180 | bo->map = MAKE_CPU_MAP(*ret); |
5181 | bo->pitch = stride; |
5181 | bo->pitch = stride; |
5182 | bo->unique_id = kgem_get_unique_id(kgem); |
5182 | bo->unique_id = kgem_get_unique_id(kgem); |
5183 | return bo; |
5183 | return bo; |
5184 | } |
5184 | } |
5185 | 5185 | ||
5186 | struct kgem_bo *kgem_upload_source_image(struct kgem *kgem, |
5186 | struct kgem_bo *kgem_upload_source_image(struct kgem *kgem, |
5187 | const void *data, |
5187 | const void *data, |
5188 | const BoxRec *box, |
5188 | const BoxRec *box, |
5189 | int stride, int bpp) |
5189 | int stride, int bpp) |
5190 | { |
5190 | { |
5191 | int width = box->x2 - box->x1; |
5191 | int width = box->x2 - box->x1; |
5192 | int height = box->y2 - box->y1; |
5192 | int height = box->y2 - box->y1; |
5193 | struct kgem_bo *bo; |
5193 | struct kgem_bo *bo; |
5194 | void *dst; |
5194 | void *dst; |
5195 | 5195 | ||
5196 | if (!kgem_can_create_2d(kgem, width, height, bpp)) |
5196 | if (!kgem_can_create_2d(kgem, width, height, bpp)) |
5197 | return NULL; |
5197 | return NULL; |
5198 | 5198 | ||
5199 | DBG(("%s : (%d, %d), (%d, %d), stride=%d, bpp=%d\n", |
5199 | DBG(("%s : (%d, %d), (%d, %d), stride=%d, bpp=%d\n", |
5200 | __FUNCTION__, box->x1, box->y1, box->x2, box->y2, stride, bpp)); |
5200 | __FUNCTION__, box->x1, box->y1, box->x2, box->y2, stride, bpp)); |
5201 | 5201 | ||
5202 | assert(data); |
5202 | assert(data); |
5203 | assert(width > 0); |
5203 | assert(width > 0); |
5204 | assert(height > 0); |
5204 | assert(height > 0); |
5205 | assert(stride); |
5205 | assert(stride); |
5206 | assert(bpp); |
5206 | assert(bpp); |
5207 | 5207 | ||
5208 | bo = kgem_create_buffer_2d(kgem, |
5208 | bo = kgem_create_buffer_2d(kgem, |
5209 | width, height, bpp, |
5209 | width, height, bpp, |
5210 | KGEM_BUFFER_WRITE_INPLACE, &dst); |
5210 | KGEM_BUFFER_WRITE_INPLACE, &dst); |
5211 | if (bo) |
5211 | if (bo) |
5212 | memcpy_blt(data, dst, bpp, |
5212 | memcpy_blt(data, dst, bpp, |
5213 | stride, bo->pitch, |
5213 | stride, bo->pitch, |
5214 | box->x1, box->y1, |
5214 | box->x1, box->y1, |
5215 | 0, 0, |
5215 | 0, 0, |
5216 | width, height); |
5216 | width, height); |
5217 | 5217 | ||
5218 | return bo; |
5218 | return bo; |
5219 | } |
5219 | } |
5220 | 5220 | ||
5221 | void kgem_proxy_bo_attach(struct kgem_bo *bo, |
5221 | void kgem_proxy_bo_attach(struct kgem_bo *bo, |
5222 | struct kgem_bo **ptr) |
5222 | struct kgem_bo **ptr) |
5223 | { |
5223 | { |
5224 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
5224 | DBG(("%s: handle=%d\n", __FUNCTION__, bo->handle)); |
5225 | assert(bo->map == NULL || IS_CPU_MAP(bo->map)); |
5225 | assert(bo->map == NULL || IS_CPU_MAP(bo->map)); |
5226 | assert(bo->proxy); |
5226 | assert(bo->proxy); |
5227 | list_add(&bo->vma, &bo->proxy->vma); |
5227 | list_add(&bo->vma, &bo->proxy->vma); |
5228 | bo->map = ptr; |
5228 | bo->map = ptr; |
5229 | *ptr = kgem_bo_reference(bo); |
5229 | *ptr = kgem_bo_reference(bo); |
5230 | } |
5230 | } |
5231 | 5231 | ||
5232 | void kgem_buffer_read_sync(struct kgem *kgem, struct kgem_bo *_bo) |
5232 | void kgem_buffer_read_sync(struct kgem *kgem, struct kgem_bo *_bo) |
5233 | { |
5233 | { |
5234 | struct kgem_buffer *bo; |
5234 | struct kgem_buffer *bo; |
5235 | uint32_t offset = _bo->delta, length = _bo->size.bytes; |
5235 | uint32_t offset = _bo->delta, length = _bo->size.bytes; |
5236 | 5236 | ||
5237 | /* We expect the caller to have already submitted the batch */ |
5237 | /* We expect the caller to have already submitted the batch */ |
5238 | assert(_bo->io); |
5238 | assert(_bo->io); |
5239 | assert(_bo->exec == NULL); |
5239 | assert(_bo->exec == NULL); |
5240 | assert(_bo->rq == NULL); |
5240 | assert(_bo->rq == NULL); |
5241 | assert(_bo->proxy); |
5241 | assert(_bo->proxy); |
5242 | 5242 | ||
5243 | _bo = _bo->proxy; |
5243 | _bo = _bo->proxy; |
5244 | assert(_bo->proxy == NULL); |
5244 | assert(_bo->proxy == NULL); |
5245 | assert(_bo->exec == NULL); |
5245 | assert(_bo->exec == NULL); |
5246 | 5246 | ||
5247 | bo = (struct kgem_buffer *)_bo; |
5247 | bo = (struct kgem_buffer *)_bo; |
5248 | 5248 | ||
5249 | DBG(("%s(offset=%d, length=%d, snooped=%d)\n", __FUNCTION__, |
5249 | DBG(("%s(offset=%d, length=%d, snooped=%d)\n", __FUNCTION__, |
5250 | offset, length, bo->base.snoop)); |
5250 | offset, length, bo->base.snoop)); |
5251 | 5251 | ||
5252 | if (bo->mmapped) { |
5252 | if (bo->mmapped) { |
5253 | struct drm_i915_gem_set_domain set_domain; |
5253 | struct drm_i915_gem_set_domain set_domain; |
5254 | 5254 | ||
5255 | DBG(("%s: sync: needs_flush? %d, domain? %d, busy? %d\n", |
5255 | DBG(("%s: sync: needs_flush? %d, domain? %d, busy? %d\n", |
5256 | __FUNCTION__, |
5256 | __FUNCTION__, |
5257 | bo->base.needs_flush, |
5257 | bo->base.needs_flush, |
5258 | bo->base.domain, |
5258 | bo->base.domain, |
5259 | __kgem_busy(kgem, bo->base.handle))); |
5259 | __kgem_busy(kgem, bo->base.handle))); |
5260 | 5260 | ||
5261 | assert(!IS_CPU_MAP(bo->base.map) || bo->base.snoop || kgem->has_llc); |
5261 | assert(!IS_CPU_MAP(bo->base.map) || bo->base.snoop || kgem->has_llc); |
5262 | 5262 | ||
5263 | VG_CLEAR(set_domain); |
5263 | VG_CLEAR(set_domain); |
5264 | set_domain.handle = bo->base.handle; |
5264 | set_domain.handle = bo->base.handle; |
5265 | set_domain.write_domain = 0; |
5265 | set_domain.write_domain = 0; |
5266 | set_domain.read_domains = |
5266 | set_domain.read_domains = |
5267 | IS_CPU_MAP(bo->base.map) ? I915_GEM_DOMAIN_CPU : I915_GEM_DOMAIN_GTT; |
5267 | IS_CPU_MAP(bo->base.map) ? I915_GEM_DOMAIN_CPU : I915_GEM_DOMAIN_GTT; |
5268 | 5268 | ||
5269 | if (drmIoctl(kgem->fd, |
5269 | if (drmIoctl(kgem->fd, |
5270 | DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) |
5270 | DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain)) |
5271 | return; |
5271 | return; |
5272 | } else { |
5272 | } else { |
5273 | if (gem_read(kgem->fd, |
5273 | if (gem_read(kgem->fd, |
5274 | bo->base.handle, (char *)bo->mem+offset, |
5274 | bo->base.handle, (char *)bo->mem+offset, |
5275 | offset, length)) |
5275 | offset, length)) |
5276 | return; |
5276 | return; |
5277 | } |
5277 | } |
5278 | kgem_bo_retire(kgem, &bo->base); |
5278 | kgem_bo_retire(kgem, &bo->base); |
5279 | bo->base.domain = DOMAIN_NONE; |
5279 | bo->base.domain = DOMAIN_NONE; |
5280 | } |
5280 | } |
5281 | #endif |
5281 | #endif |
5282 | 5282 | ||
5283 | uint32_t kgem_bo_get_binding(struct kgem_bo *bo, uint32_t format) |
5283 | uint32_t kgem_bo_get_binding(struct kgem_bo *bo, uint32_t format) |
5284 | { |
5284 | { |
5285 | struct kgem_bo_binding *b; |
5285 | struct kgem_bo_binding *b; |
5286 | 5286 | ||
5287 | for (b = &bo->binding; b && b->offset; b = b->next) |
5287 | for (b = &bo->binding; b && b->offset; b = b->next) |
5288 | if (format == b->format) |
5288 | if (format == b->format) |
5289 | return b->offset; |
5289 | return b->offset; |
5290 | 5290 | ||
5291 | return 0; |
5291 | return 0; |
5292 | } |
5292 | } |
5293 | 5293 | ||
5294 | void kgem_bo_set_binding(struct kgem_bo *bo, uint32_t format, uint16_t offset) |
5294 | void kgem_bo_set_binding(struct kgem_bo *bo, uint32_t format, uint16_t offset) |
5295 | { |
5295 | { |
5296 | struct kgem_bo_binding *b; |
5296 | struct kgem_bo_binding *b; |
5297 | 5297 | ||
5298 | for (b = &bo->binding; b; b = b->next) { |
5298 | for (b = &bo->binding; b; b = b->next) { |
5299 | if (b->offset) |
5299 | if (b->offset) |
5300 | continue; |
5300 | continue; |
5301 | 5301 | ||
5302 | b->offset = offset; |
5302 | b->offset = offset; |
5303 | b->format = format; |
5303 | b->format = format; |
5304 | 5304 | ||
5305 | if (b->next) |
5305 | if (b->next) |
5306 | b->next->offset = 0; |
5306 | b->next->offset = 0; |
5307 | 5307 | ||
5308 | return; |
5308 | return; |
5309 | } |
5309 | } |
5310 | 5310 | ||
5311 | b = malloc(sizeof(*b)); |
5311 | b = malloc(sizeof(*b)); |
5312 | if (b) { |
5312 | if (b) { |
5313 | b->next = bo->binding.next; |
5313 | b->next = bo->binding.next; |
5314 | b->format = format; |
5314 | b->format = format; |
5315 | b->offset = offset; |
5315 | b->offset = offset; |
5316 | bo->binding.next = b; |
5316 | bo->binding.next = b; |
5317 | } |
5317 | } |
5318 | } |
5318 | } |
5319 | 5319 | ||
5320 | int kgem_init_fb(struct kgem *kgem, struct sna_fb *fb) |
5320 | int kgem_init_fb(struct kgem *kgem, struct sna_fb *fb) |
5321 | { |
5321 | { |
5322 | struct kgem_bo *bo; |
5322 | struct kgem_bo *bo; |
5323 | struct drm_gem_open open_arg; |
5323 | struct drm_gem_open open_arg; |
5324 | struct drm_i915_gem_get_tiling get_tiling; |
5324 | struct drm_i915_gem_get_tiling get_tiling; |
5325 | 5325 | ||
5326 | size_t size; |
5326 | size_t size; |
5327 | int ret; |
5327 | int ret; |
5328 | 5328 | ||
5329 | ret = drmIoctl(kgem->fd, SRV_FBINFO, fb); |
5329 | ret = drmIoctl(kgem->fd, SRV_FBINFO, fb); |
5330 | if( ret != 0 ) |
5330 | if( ret != 0 ) |
5331 | return 0; |
5331 | return 0; |
5332 | 5332 | ||
5333 | open_arg.name = fb->name; |
5333 | open_arg.name = fb->name; |
5334 | ret = drmIoctl(kgem->fd, DRM_IOCTL_GEM_OPEN, &open_arg); |
5334 | ret = drmIoctl(kgem->fd, DRM_IOCTL_GEM_OPEN, &open_arg); |
5335 | if (ret != 0) { |
5335 | if (ret != 0) { |
5336 | printf("Couldn't reference %s handle 0x%08x\n", |
5336 | printf("Couldn't reference %s handle 0x%08x\n", |
5337 | fb->name, fb->name); |
5337 | fb->name, fb->name); |
5338 | return NULL; |
5338 | return NULL; |
5339 | } |
5339 | } |
5340 | size = open_arg.size / PAGE_SIZE; |
5340 | size = open_arg.size / PAGE_SIZE; |
5341 | 5341 | ||
5342 | bo = __kgem_bo_alloc(open_arg.handle, size); |
5342 | bo = __kgem_bo_alloc(open_arg.handle, size); |
5343 | if (!bo) { |
5343 | if (!bo) { |
5344 | return 0; |
5344 | return 0; |
5345 | } |
5345 | } |
5346 | 5346 | ||
5347 | get_tiling.handle = bo->handle; |
5347 | get_tiling.handle = bo->handle; |
5348 | ret = drmIoctl(kgem->fd,DRM_IOCTL_I915_GEM_GET_TILING,&get_tiling); |
5348 | ret = drmIoctl(kgem->fd,DRM_IOCTL_I915_GEM_GET_TILING,&get_tiling); |
5349 | if (ret != 0) { |
5349 | if (ret != 0) { |
5350 | printf("%s: couldn't get tiling for handle %d\n", __FUNCTION__, bo->handle); |
5350 | printf("%s: couldn't get tiling for handle %d\n", __FUNCTION__, bo->handle); |
5351 | // drm_intel_gem_bo_unreference(&bo_gem->bo); |
5351 | // drm_intel_gem_bo_unreference(&bo_gem->bo); |
5352 | return 0; |
5352 | return 0; |
5353 | } |
5353 | } |
5354 | 5354 | ||
5355 | bo->domain = DOMAIN_GTT; |
5355 | bo->domain = DOMAIN_GTT; |
5356 | bo->unique_id = kgem_get_unique_id(kgem); |
5356 | bo->unique_id = kgem_get_unique_id(kgem); |
5357 | bo->pitch = fb->pitch; |
5357 | bo->pitch = fb->pitch; |
5358 | bo->tiling = get_tiling.tiling_mode; |
5358 | bo->tiling = get_tiling.tiling_mode; |
5359 | bo->scanout = 1; |
5359 | bo->scanout = 1; |
5360 | fb->fb_bo = bo; |
5360 | fb->fb_bo = bo; |
5361 | 5361 | ||
5362 | printf("fb handle %d w: %d h: %d pitch %d tilng %d bo %p\n", |
5362 | printf("fb handle %d w: %d h: %d pitch %d tilng %d bo %p\n", |
5363 | bo->handle, fb->width, fb->height, fb->pitch, fb->tiling, fb->fb_bo); |
5363 | bo->handle, fb->width, fb->height, fb->pitch, fb->tiling, fb->fb_bo); |
5364 | 5364 | ||
5365 | return 1; |
5365 | return 1; |
5366 | }; |
5366 | }; |
5367 | 5367 | ||
5368 | 5368 | ||
5369 | int kgem_update_fb(struct kgem *kgem, struct sna_fb *fb) |
5369 | int kgem_update_fb(struct kgem *kgem, struct sna_fb *fb) |
5370 | { |
5370 | { |
5371 | struct kgem_bo *bo; |
5371 | struct kgem_bo *bo; |
5372 | size_t size; |
5372 | size_t size; |
5373 | int ret; |
5373 | int ret; |
5374 | 5374 | ||
5375 | bo = fb->fb_bo; |
5375 | bo = fb->fb_bo; |
5376 | 5376 | ||
5377 | ret = drmIoctl(kgem->fd, SRV_FBINFO, fb); |
5377 | ret = drmIoctl(kgem->fd, SRV_FBINFO, fb); |
5378 | if( ret != 0 ) |
5378 | if( ret != 0 ) |
5379 | return 0; |
5379 | return 0; |
5380 | 5380 | ||
5381 | fb->fb_bo = bo; |
5381 | fb->fb_bo = bo; |
5382 | 5382 | ||
5383 | size = fb->pitch * fb->height / PAGE_SIZE; |
5383 | size = fb->pitch * fb->height / PAGE_SIZE; |
5384 | 5384 | ||
5385 | if((size != bo->size.pages.count) || |
5385 | if((size != bo->size.pages.count) || |
5386 | (fb->pitch != bo->pitch)) |
5386 | (fb->pitch != bo->pitch)) |
5387 | { |
5387 | { |
5388 | bo->size.pages.count = size; |
5388 | bo->size.pages.count = size; |
5389 | bo->pitch = fb->pitch; |
5389 | bo->pitch = fb->pitch; |
5390 | 5390 | ||
5391 | printf("fb width %d height %d pitch %d bo %p\n", |
5391 | printf("fb width %d height %d pitch %d bo %p\n", |
5392 | fb->width, fb->height, fb->pitch, fb->fb_bo); |
5392 | fb->width, fb->height, fb->pitch, fb->fb_bo); |
5393 | 5393 | ||
5394 | return 1; |
5394 | return 1; |
5395 | } |
5395 | } |
5396 | 5396 | ||
5397 | return 0; |
5397 | return 0; |
5398 | }; |
5398 | }; |
5399 | 5399 | ||
5400 | void sna_bo_destroy(struct kgem *kgem, struct kgem_bo *bo) |
5400 | void sna_bo_destroy(struct kgem *kgem, struct kgem_bo *bo) |
5401 | { |
5401 | { |
5402 | kgem_bo_destroy(kgem, bo); |
5402 | kgem_bo_destroy(kgem, bo); |
5403 | kgem_bo_free(kgem, bo); |
5403 | kgem_bo_free(kgem, bo); |
5404 | } |
5404 | } |
5405 | 5405 | ||
5406 | 5406 | ||
5407 | void kgem_close_batches(struct kgem *kgem) |
5407 | void kgem_close_batches(struct kgem *kgem) |
5408 | { |
5408 | { |
5409 | int n; |
5409 | int n; |
5410 | for (n = 0; n < ARRAY_SIZE(kgem->pinned_batches); n++) { |
5410 | for (n = 0; n < ARRAY_SIZE(kgem->pinned_batches); n++) { |
5411 | while (!list_is_empty(&kgem->pinned_batches[n])) |
5411 | while (!list_is_empty(&kgem->pinned_batches[n])) |
5412 | { |
5412 | { |
5413 | struct kgem_bo *bo = |
5413 | struct kgem_bo *bo = |
5414 | list_first_entry(&kgem->pinned_batches[n], |
5414 | list_first_entry(&kgem->pinned_batches[n], |
5415 | struct kgem_bo, list); |
5415 | struct kgem_bo, list); |
5416 | list_del(&bo->list); |
5416 | list_del(&bo->list); |
5417 | kgem_bo_destroy(kgem,bo); |
5417 | kgem_bo_destroy(kgem,bo); |
5418 | } |
5418 | } |
5419 | } |
5419 | } |
5420 | }; |
5420 | }; |
5421 | 5421 | ||
5422 | struct kgem_bo *kgem_bo_from_handle(struct kgem *kgem, int handle, |
5422 | struct kgem_bo *kgem_bo_from_handle(struct kgem *kgem, int handle, |
5423 | int pitch, int height) |
5423 | int pitch, int height) |
5424 | { |
5424 | { |
5425 | struct kgem_bo *bo; |
5425 | struct kgem_bo *bo; |
5426 | int size; |
5426 | int size; |
5427 | 5427 | ||
5428 | size = pitch * height / PAGE_SIZE; |
5428 | size = pitch * height / PAGE_SIZE; |
5429 | 5429 | ||
5430 | bo = __kgem_bo_alloc(handle, size); |
5430 | bo = __kgem_bo_alloc(handle, size); |
5431 | if(bo == NULL) |
5431 | if(bo == NULL) |
5432 | return NULL; |
5432 | return NULL; |
5433 | 5433 | ||
5434 | bo->domain = DOMAIN_GTT; |
5434 | bo->domain = DOMAIN_GTT; |
5435 | bo->unique_id = kgem_get_unique_id(kgem); |
5435 | bo->unique_id = kgem_get_unique_id(kgem); |
5436 | bo->pitch = pitch; |
5436 | bo->pitch = pitch; |
5437 | bo->tiling = I915_TILING_X; |
5437 | bo->tiling = I915_TILING_X; |
5438 | bo->scanout = 0; |
5438 | bo->scanout = 0; |
5439 | 5439 | ||
5440 | return bo; |
5440 | return bo; |
5441 | }>=>=>=>=>><>=>><>=>=>>=>>>>>=>>>=>>=>>=>=>>=>>>>>=>>>=>=>>>>=>=>=>>>>>=>>>>>>>>>>=>>=>=>=>=>=>=>=>=>>>>=>>=>>=>>=>>>>>>=>>>=>>>=><=>>>=>=>=>>=>>>>><>><>><>>>>>>>>>>>>>>>>>>>>>=>31) |
5441 | }>=>=>=>=>><>=>><>=>=>>=>>>>>=>>>=>>=>>=>=>>=>>>>>=>>>=>=>>>>=>=>=>>>>>=>>>>>>>>>>=>>=>=>=>=>=>=>=>=>>>>=>>=>>=>>=>>>>>>=>>>=>>>=><=>>>=>=>=>>=>>>>><>><>><>>>>>>>>>>>>>>>>>>>>>=>31) |
5442 | ><31) |
5442 | ><31) |
5443 | >0) |
5443 | >0) |
5444 | #define><0) |
5444 | #define><0) |
5445 | #define>12) |
5445 | #define>12) |
5446 | struct><12) |
5446 | struct><12) |
5447 | struct>11) |
5447 | struct>11) |
5448 | #define><11) |
5448 | #define><11) |
5449 | #define>10) |
5449 | #define>10) |
5450 | #define><10) |
5450 | #define><10) |
5451 | #define>><>><> |
5451 | #define>><>><> |