Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright © 2007 Red Hat Inc. |
| 4 | * Copyright © 2007 Intel Corporation |
| 5 | * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA |
| 6 | * All Rights Reserved. |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | * copy of this software and associated documentation files (the |
| 10 | * "Software"), to deal in the Software without restriction, including |
| 11 | * without limitation the rights to use, copy, modify, merge, publish, |
| 12 | * distribute, sub license, and/or sell copies of the Software, and to |
| 13 | * permit persons to whom the Software is furnished to do so, subject to |
| 14 | * the following conditions: |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 21 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 22 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | * |
| 24 | * The above copyright notice and this permission notice (including the |
| 25 | * next paragraph) shall be included in all copies or substantial portions |
| 26 | * of the Software. |
| 27 | * |
| 28 | * |
| 29 | **************************************************************************/ |
| 30 | /* |
| 31 | * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com> |
| 32 | * Keith Whitwell <keithw-at-tungstengraphics-dot-com> |
| 33 | * Eric Anholt <eric@anholt.net> |
| 34 | * Dave Airlie <airlied@linux.ie> |
| 35 | */ |
| 36 | |
Eric Anholt | 368b392 | 2008-09-10 13:54:34 -0700 | [diff] [blame] | 37 | #ifdef HAVE_CONFIG_H |
| 38 | #include "config.h" |
| 39 | #endif |
| 40 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 41 | #include <xf86drm.h> |
Jesse Barnes | 276c07d | 2008-11-13 13:52:04 -0800 | [diff] [blame] | 42 | #include <fcntl.h> |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <unistd.h> |
| 47 | #include <assert.h> |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 48 | #include <pthread.h> |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 49 | #include <sys/ioctl.h> |
| 50 | #include <sys/mman.h> |
Jesse Barnes | 276c07d | 2008-11-13 13:52:04 -0800 | [diff] [blame] | 51 | #include <sys/stat.h> |
| 52 | #include <sys/types.h> |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 53 | |
| 54 | #include "errno.h" |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 55 | #include "libdrm_lists.h" |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 56 | #include "intel_atomic.h" |
Eric Anholt | c485742 | 2008-06-03 10:20:49 -0700 | [diff] [blame] | 57 | #include "intel_bufmgr.h" |
Eric Anholt | 738e36a | 2008-09-05 10:35:32 +0100 | [diff] [blame] | 58 | #include "intel_bufmgr_priv.h" |
Eric Anholt | cbdd627 | 2009-01-27 17:16:11 -0800 | [diff] [blame] | 59 | #include "intel_chipset.h" |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 60 | #include "string.h" |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 61 | |
| 62 | #include "i915_drm.h" |
| 63 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 64 | #define DBG(...) do { \ |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 65 | if (bufmgr_gem->bufmgr.debug) \ |
| 66 | fprintf(stderr, __VA_ARGS__); \ |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 67 | } while (0) |
| 68 | |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 69 | typedef struct _drm_intel_bo_gem drm_intel_bo_gem; |
Keith Packard | a919ff5 | 2008-06-05 15:58:09 -0700 | [diff] [blame] | 70 | |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 71 | struct drm_intel_gem_bo_bucket { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 72 | drmMMListHead head; |
| 73 | unsigned long size; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Eric Anholt | 469655f | 2009-05-18 16:07:45 -0700 | [diff] [blame] | 76 | /* Only cache objects up to 64MB. Bigger than that, and the rounding of the |
| 77 | * size makes many operations fail that wouldn't otherwise. |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 78 | */ |
Eric Anholt | 469655f | 2009-05-18 16:07:45 -0700 | [diff] [blame] | 79 | #define DRM_INTEL_GEM_BO_BUCKETS 14 |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 80 | typedef struct _drm_intel_bufmgr_gem { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 81 | drm_intel_bufmgr bufmgr; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 82 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 83 | int fd; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 84 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 85 | int max_relocs; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 86 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 87 | pthread_mutex_t lock; |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 88 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 89 | struct drm_i915_gem_exec_object *exec_objects; |
| 90 | drm_intel_bo **exec_bos; |
| 91 | int exec_size; |
| 92 | int exec_count; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 93 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 94 | /** Array of lists of cached gem objects of power-of-two sizes */ |
| 95 | struct drm_intel_gem_bo_bucket cache_bucket[DRM_INTEL_GEM_BO_BUCKETS]; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 96 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 97 | uint64_t gtt_size; |
| 98 | int available_fences; |
| 99 | int pci_device; |
| 100 | char bo_reuse; |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 101 | } drm_intel_bufmgr_gem; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 102 | |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 103 | struct _drm_intel_bo_gem { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 104 | drm_intel_bo bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 105 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 106 | atomic_t refcount; |
| 107 | uint32_t gem_handle; |
| 108 | const char *name; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 109 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Kenel-assigned global name for this object |
| 112 | */ |
| 113 | unsigned int global_name; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 114 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Index of the buffer within the validation list while preparing a |
| 117 | * batchbuffer execution. |
| 118 | */ |
| 119 | int validate_index; |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 120 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Current tiling mode |
| 123 | */ |
| 124 | uint32_t tiling_mode; |
| 125 | uint32_t swizzle_mode; |
Eric Anholt | 3f3c5be | 2009-07-09 17:49:46 -0700 | [diff] [blame] | 126 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 127 | time_t free_time; |
Keith Packard | 329e086 | 2008-06-05 16:05:35 -0700 | [diff] [blame] | 128 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 129 | /** Array passed to the DRM containing relocation information. */ |
| 130 | struct drm_i915_gem_relocation_entry *relocs; |
| 131 | /** Array of bos corresponding to relocs[i].target_handle */ |
| 132 | drm_intel_bo **reloc_target_bo; |
| 133 | /** Number of entries in relocs */ |
| 134 | int reloc_count; |
| 135 | /** Mapped address for the buffer, saved across map/unmap cycles */ |
| 136 | void *mem_virtual; |
| 137 | /** GTT virtual address for the buffer, saved across map/unmap cycles */ |
| 138 | void *gtt_virtual; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 139 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 140 | /** BO cache list */ |
| 141 | drmMMListHead head; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 142 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 143 | /** |
| 144 | * Boolean of whether this BO and its children have been included in |
| 145 | * the current drm_intel_bufmgr_check_aperture_space() total. |
| 146 | */ |
| 147 | char included_in_check_aperture; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 148 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Boolean of whether this buffer has been used as a relocation |
| 151 | * target and had its size accounted for, and thus can't have any |
| 152 | * further relocations added to it. |
| 153 | */ |
| 154 | char used_as_reloc_target; |
Keith Packard | 5b5ce30 | 2009-05-11 13:42:12 -0700 | [diff] [blame] | 155 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Boolean of whether this buffer can be re-used |
| 158 | */ |
| 159 | char reusable; |
| 160 | |
| 161 | /** |
| 162 | * Size in bytes of this buffer and its relocation descendents. |
| 163 | * |
| 164 | * Used to avoid costly tree walking in |
| 165 | * drm_intel_bufmgr_check_aperture in the common case. |
| 166 | */ |
| 167 | int reloc_tree_size; |
| 168 | |
| 169 | /** |
| 170 | * Number of potential fence registers required by this buffer and its |
| 171 | * relocations. |
| 172 | */ |
| 173 | int reloc_tree_fences; |
Keith Packard | a919ff5 | 2008-06-05 15:58:09 -0700 | [diff] [blame] | 174 | }; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 175 | |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 176 | static unsigned int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 177 | drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count); |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 178 | |
| 179 | static unsigned int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 180 | drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count); |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 181 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 182 | static int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 183 | drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode, |
| 184 | uint32_t * swizzle_mode); |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 185 | |
| 186 | static int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 187 | drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode, |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 188 | uint32_t stride); |
| 189 | |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 190 | static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo, |
| 191 | time_t time); |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 192 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 193 | static void drm_intel_gem_bo_unreference(drm_intel_bo *bo); |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 194 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 195 | static void drm_intel_gem_bo_free(drm_intel_bo *bo); |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 196 | |
Jesse Barnes | 3a7dfcd | 2009-10-06 14:34:06 -0700 | [diff] [blame] | 197 | static unsigned long |
| 198 | drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size, |
| 199 | uint32_t *tiling_mode) |
| 200 | { |
| 201 | unsigned long min_size, max_size; |
| 202 | unsigned long i; |
| 203 | |
| 204 | if (*tiling_mode == I915_TILING_NONE) |
| 205 | return size; |
| 206 | |
| 207 | /* 965+ just need multiples of page size for tiling */ |
| 208 | if (IS_I965G(bufmgr_gem)) |
| 209 | return ROUND_UP_TO(size, 4096); |
| 210 | |
| 211 | /* Older chips need powers of two, of at least 512k or 1M */ |
| 212 | if (IS_I9XX(bufmgr_gem)) { |
| 213 | min_size = 1024*1024; |
| 214 | max_size = 128*1024*1024; |
| 215 | } else { |
| 216 | min_size = 512*1024; |
| 217 | max_size = 64*1024*1024; |
| 218 | } |
| 219 | |
| 220 | if (size > max_size) { |
| 221 | *tiling_mode = I915_TILING_NONE; |
| 222 | return size; |
| 223 | } |
| 224 | |
| 225 | for (i = min_size; i < size; i <<= 1) |
| 226 | ; |
| 227 | |
| 228 | return i; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Round a given pitch up to the minimum required for X tiling on a |
| 233 | * given chip. We use 512 as the minimum to allow for a later tiling |
| 234 | * change. |
| 235 | */ |
| 236 | static unsigned long |
| 237 | drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem, |
| 238 | unsigned long pitch, uint32_t tiling_mode) |
| 239 | { |
| 240 | unsigned long tile_width = 512; |
| 241 | unsigned long i; |
| 242 | |
| 243 | if (tiling_mode == I915_TILING_NONE) |
| 244 | return ROUND_UP_TO(pitch, tile_width); |
| 245 | |
| 246 | /* 965 is flexible */ |
| 247 | if (IS_I965G(bufmgr_gem)) |
| 248 | return ROUND_UP_TO(pitch, tile_width); |
| 249 | |
| 250 | /* Pre-965 needs power of two tile width */ |
| 251 | for (i = tile_width; i < pitch; i <<= 1) |
| 252 | ; |
| 253 | |
| 254 | return i; |
| 255 | } |
| 256 | |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 257 | static struct drm_intel_gem_bo_bucket * |
| 258 | drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem, |
| 259 | unsigned long size) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 260 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 261 | int i; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 262 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 263 | for (i = 0; i < DRM_INTEL_GEM_BO_BUCKETS; i++) { |
| 264 | struct drm_intel_gem_bo_bucket *bucket = |
| 265 | &bufmgr_gem->cache_bucket[i]; |
| 266 | if (bucket->size >= size) { |
| 267 | return bucket; |
| 268 | } |
Eric Anholt | 78fa590 | 2009-07-06 11:55:28 -0700 | [diff] [blame] | 269 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 270 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 271 | return NULL; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 274 | static void |
| 275 | drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 276 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 277 | int i, j; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 278 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 279 | for (i = 0; i < bufmgr_gem->exec_count; i++) { |
| 280 | drm_intel_bo *bo = bufmgr_gem->exec_bos[i]; |
| 281 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 282 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 283 | if (bo_gem->relocs == NULL) { |
| 284 | DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle, |
| 285 | bo_gem->name); |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | for (j = 0; j < bo_gem->reloc_count; j++) { |
| 290 | drm_intel_bo *target_bo = bo_gem->reloc_target_bo[j]; |
| 291 | drm_intel_bo_gem *target_gem = |
| 292 | (drm_intel_bo_gem *) target_bo; |
| 293 | |
| 294 | DBG("%2d: %d (%s)@0x%08llx -> " |
| 295 | "%d (%s)@0x%08lx + 0x%08x\n", |
| 296 | i, |
| 297 | bo_gem->gem_handle, bo_gem->name, |
| 298 | (unsigned long long)bo_gem->relocs[j].offset, |
| 299 | target_gem->gem_handle, |
| 300 | target_gem->name, |
| 301 | target_bo->offset, |
| 302 | bo_gem->relocs[j].delta); |
| 303 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 304 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 307 | static void |
| 308 | drm_intel_gem_bo_reference(drm_intel_bo *bo) |
| 309 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 310 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 311 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 312 | assert(atomic_read(&bo_gem->refcount) > 0); |
| 313 | atomic_inc(&bo_gem->refcount); |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Adds the given buffer to the list of buffers to be validated (moved into the |
| 318 | * appropriate memory type) with the next batch submission. |
| 319 | * |
| 320 | * If a buffer is validated multiple times in a batch submission, it ends up |
| 321 | * with the intersection of the memory type flags and the union of the |
| 322 | * access flags. |
| 323 | */ |
| 324 | static void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 325 | drm_intel_add_validate_buffer(drm_intel_bo *bo) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 326 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 327 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 328 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 329 | int index; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 330 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 331 | if (bo_gem->validate_index != -1) |
| 332 | return; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 333 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 334 | /* Extend the array of validation entries as necessary. */ |
| 335 | if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) { |
| 336 | int new_size = bufmgr_gem->exec_size * 2; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 337 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 338 | if (new_size == 0) |
| 339 | new_size = 5; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 340 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 341 | bufmgr_gem->exec_objects = |
| 342 | realloc(bufmgr_gem->exec_objects, |
| 343 | sizeof(*bufmgr_gem->exec_objects) * new_size); |
| 344 | bufmgr_gem->exec_bos = |
| 345 | realloc(bufmgr_gem->exec_bos, |
| 346 | sizeof(*bufmgr_gem->exec_bos) * new_size); |
| 347 | bufmgr_gem->exec_size = new_size; |
| 348 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 349 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 350 | index = bufmgr_gem->exec_count; |
| 351 | bo_gem->validate_index = index; |
| 352 | /* Fill in array entry */ |
| 353 | bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle; |
| 354 | bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count; |
| 355 | bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs; |
| 356 | bufmgr_gem->exec_objects[index].alignment = 0; |
| 357 | bufmgr_gem->exec_objects[index].offset = 0; |
| 358 | bufmgr_gem->exec_bos[index] = bo; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 359 | bufmgr_gem->exec_count++; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 362 | #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \ |
| 363 | sizeof(uint32_t)) |
| 364 | |
Chris Wilson | e22fb79 | 2009-11-30 22:14:30 +0000 | [diff] [blame] | 365 | static void |
| 366 | drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem, |
| 367 | drm_intel_bo_gem *bo_gem) |
| 368 | { |
| 369 | int size; |
| 370 | |
| 371 | assert(!bo_gem->used_as_reloc_target); |
| 372 | |
| 373 | /* The older chipsets are far-less flexible in terms of tiling, |
| 374 | * and require tiled buffer to be size aligned in the aperture. |
| 375 | * This means that in the worst possible case we will need a hole |
| 376 | * twice as large as the object in order for it to fit into the |
| 377 | * aperture. Optimal packing is for wimps. |
| 378 | */ |
| 379 | size = bo_gem->bo.size; |
| 380 | if (!IS_I965G(bufmgr_gem) && bo_gem->tiling_mode != I915_TILING_NONE) |
| 381 | size *= 2; |
| 382 | |
| 383 | bo_gem->reloc_tree_size = size; |
| 384 | } |
| 385 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 386 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 387 | drm_intel_setup_reloc_list(drm_intel_bo *bo) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 388 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 389 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 390 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 391 | unsigned int max_relocs = bufmgr_gem->max_relocs; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 392 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 393 | if (bo->size / 4 < max_relocs) |
| 394 | max_relocs = bo->size / 4; |
Eric Anholt | 3c9bd06 | 2009-10-05 16:35:32 -0700 | [diff] [blame] | 395 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 396 | bo_gem->relocs = malloc(max_relocs * |
| 397 | sizeof(struct drm_i915_gem_relocation_entry)); |
| 398 | bo_gem->reloc_target_bo = malloc(max_relocs * sizeof(drm_intel_bo *)); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 399 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 400 | return 0; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Eric Anholt | 8214a65 | 2009-08-27 18:32:07 -0700 | [diff] [blame] | 403 | static int |
| 404 | drm_intel_gem_bo_busy(drm_intel_bo *bo) |
| 405 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 406 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 407 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 408 | struct drm_i915_gem_busy busy; |
| 409 | int ret; |
Eric Anholt | 8214a65 | 2009-08-27 18:32:07 -0700 | [diff] [blame] | 410 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 411 | memset(&busy, 0, sizeof(busy)); |
| 412 | busy.handle = bo_gem->gem_handle; |
Eric Anholt | 8214a65 | 2009-08-27 18:32:07 -0700 | [diff] [blame] | 413 | |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 414 | do { |
| 415 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy); |
| 416 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | 8214a65 | 2009-08-27 18:32:07 -0700 | [diff] [blame] | 417 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 418 | return (ret == 0 && busy.busy); |
Eric Anholt | 8214a65 | 2009-08-27 18:32:07 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 421 | static int |
Chris Wilson | 83a35b6 | 2009-11-11 13:04:38 +0000 | [diff] [blame] | 422 | drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem, |
| 423 | drm_intel_bo_gem *bo_gem, int state) |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 424 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 425 | struct drm_i915_gem_madvise madv; |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 426 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 427 | madv.handle = bo_gem->gem_handle; |
| 428 | madv.madv = state; |
| 429 | madv.retained = 1; |
| 430 | ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv); |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 431 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 432 | return madv.retained; |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Chris Wilson | 83a35b6 | 2009-11-11 13:04:38 +0000 | [diff] [blame] | 435 | static int |
| 436 | drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv) |
| 437 | { |
| 438 | return drm_intel_gem_bo_madvise_internal |
| 439 | ((drm_intel_bufmgr_gem *) bo->bufmgr, |
| 440 | (drm_intel_bo_gem *) bo, |
| 441 | madv); |
| 442 | } |
| 443 | |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 444 | /* drop the oldest entries that have been purged by the kernel */ |
| 445 | static void |
| 446 | drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem, |
| 447 | struct drm_intel_gem_bo_bucket *bucket) |
| 448 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 449 | while (!DRMLISTEMPTY(&bucket->head)) { |
| 450 | drm_intel_bo_gem *bo_gem; |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 451 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 452 | bo_gem = DRMLISTENTRY(drm_intel_bo_gem, |
| 453 | bucket->head.next, head); |
Chris Wilson | 83a35b6 | 2009-11-11 13:04:38 +0000 | [diff] [blame] | 454 | if (drm_intel_gem_bo_madvise_internal |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 455 | (bufmgr_gem, bo_gem, I915_MADV_DONTNEED)) |
| 456 | break; |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 457 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 458 | DRMLISTDEL(&bo_gem->head); |
| 459 | drm_intel_gem_bo_free(&bo_gem->bo); |
| 460 | } |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 463 | static drm_intel_bo * |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 464 | drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr, |
| 465 | const char *name, |
| 466 | unsigned long size, |
Jesse Barnes | 3a7dfcd | 2009-10-06 14:34:06 -0700 | [diff] [blame] | 467 | unsigned long flags) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 468 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 469 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr; |
| 470 | drm_intel_bo_gem *bo_gem; |
| 471 | unsigned int page_size = getpagesize(); |
| 472 | int ret; |
| 473 | struct drm_intel_gem_bo_bucket *bucket; |
| 474 | int alloc_from_cache; |
| 475 | unsigned long bo_size; |
Jesse Barnes | 3a7dfcd | 2009-10-06 14:34:06 -0700 | [diff] [blame] | 476 | int for_render = 0; |
| 477 | |
| 478 | if (flags & BO_ALLOC_FOR_RENDER) |
| 479 | for_render = 1; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 480 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 481 | /* Round the allocated size up to a power of two number of pages. */ |
| 482 | bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 483 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 484 | /* If we don't have caching at this size, don't actually round the |
| 485 | * allocation up. |
| 486 | */ |
| 487 | if (bucket == NULL) { |
| 488 | bo_size = size; |
| 489 | if (bo_size < page_size) |
| 490 | bo_size = page_size; |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 491 | } else { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 492 | bo_size = bucket->size; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 493 | } |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 494 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 495 | pthread_mutex_lock(&bufmgr_gem->lock); |
| 496 | /* Get a buffer out of the cache if available */ |
| 497 | retry: |
| 498 | alloc_from_cache = 0; |
| 499 | if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) { |
| 500 | if (for_render) { |
| 501 | /* Allocate new render-target BOs from the tail (MRU) |
| 502 | * of the list, as it will likely be hot in the GPU |
| 503 | * cache and in the aperture for us. |
| 504 | */ |
| 505 | bo_gem = DRMLISTENTRY(drm_intel_bo_gem, |
| 506 | bucket->head.prev, head); |
| 507 | DRMLISTDEL(&bo_gem->head); |
| 508 | alloc_from_cache = 1; |
| 509 | } else { |
| 510 | /* For non-render-target BOs (where we're probably |
| 511 | * going to map it first thing in order to fill it |
| 512 | * with data), check if the last BO in the cache is |
| 513 | * unbusy, and only reuse in that case. Otherwise, |
| 514 | * allocating a new buffer is probably faster than |
| 515 | * waiting for the GPU to finish. |
| 516 | */ |
| 517 | bo_gem = DRMLISTENTRY(drm_intel_bo_gem, |
| 518 | bucket->head.next, head); |
| 519 | if (!drm_intel_gem_bo_busy(&bo_gem->bo)) { |
| 520 | alloc_from_cache = 1; |
| 521 | DRMLISTDEL(&bo_gem->head); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (alloc_from_cache) { |
Chris Wilson | 83a35b6 | 2009-11-11 13:04:38 +0000 | [diff] [blame] | 526 | if (!drm_intel_gem_bo_madvise_internal |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 527 | (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) { |
| 528 | drm_intel_gem_bo_free(&bo_gem->bo); |
| 529 | drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem, |
| 530 | bucket); |
| 531 | goto retry; |
| 532 | } |
| 533 | } |
Chris Wilson | 0fb215a | 2009-10-02 04:31:34 +0100 | [diff] [blame] | 534 | } |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 535 | pthread_mutex_unlock(&bufmgr_gem->lock); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 536 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 537 | if (!alloc_from_cache) { |
| 538 | struct drm_i915_gem_create create; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 539 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 540 | bo_gem = calloc(1, sizeof(*bo_gem)); |
| 541 | if (!bo_gem) |
| 542 | return NULL; |
Keith Packard | a919ff5 | 2008-06-05 15:58:09 -0700 | [diff] [blame] | 543 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 544 | bo_gem->bo.size = bo_size; |
| 545 | memset(&create, 0, sizeof(create)); |
| 546 | create.size = bo_size; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 547 | |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 548 | do { |
| 549 | ret = ioctl(bufmgr_gem->fd, |
| 550 | DRM_IOCTL_I915_GEM_CREATE, |
| 551 | &create); |
| 552 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 553 | bo_gem->gem_handle = create.handle; |
| 554 | bo_gem->bo.handle = bo_gem->gem_handle; |
| 555 | if (ret != 0) { |
| 556 | free(bo_gem); |
| 557 | return NULL; |
| 558 | } |
| 559 | bo_gem->bo.bufmgr = bufmgr; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 560 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 561 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 562 | bo_gem->name = name; |
| 563 | atomic_set(&bo_gem->refcount, 1); |
| 564 | bo_gem->validate_index = -1; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 565 | bo_gem->reloc_tree_fences = 0; |
| 566 | bo_gem->used_as_reloc_target = 0; |
| 567 | bo_gem->tiling_mode = I915_TILING_NONE; |
| 568 | bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; |
| 569 | bo_gem->reusable = 1; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 570 | |
Chris Wilson | e22fb79 | 2009-11-30 22:14:30 +0000 | [diff] [blame] | 571 | drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem); |
| 572 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 573 | DBG("bo_create: buf %d (%s) %ldb\n", |
| 574 | bo_gem->gem_handle, bo_gem->name, size); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 575 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 576 | return &bo_gem->bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 579 | static drm_intel_bo * |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 580 | drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr, |
| 581 | const char *name, |
| 582 | unsigned long size, |
| 583 | unsigned int alignment) |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 584 | { |
Jesse Barnes | 3a7dfcd | 2009-10-06 14:34:06 -0700 | [diff] [blame] | 585 | return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, |
| 586 | BO_ALLOC_FOR_RENDER); |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static drm_intel_bo * |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 590 | drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr, |
| 591 | const char *name, |
| 592 | unsigned long size, |
| 593 | unsigned int alignment) |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 594 | { |
Jesse Barnes | 3a7dfcd | 2009-10-06 14:34:06 -0700 | [diff] [blame] | 595 | return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0); |
| 596 | } |
| 597 | |
| 598 | static drm_intel_bo * |
| 599 | drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, |
| 600 | int x, int y, int cpp, uint32_t *tiling_mode, |
| 601 | unsigned long *pitch, unsigned long flags) |
| 602 | { |
| 603 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr; |
| 604 | drm_intel_bo *bo; |
| 605 | unsigned long size, stride, aligned_y = y; |
| 606 | int ret; |
| 607 | |
| 608 | if (*tiling_mode == I915_TILING_NONE) |
| 609 | aligned_y = ALIGN(y, 2); |
| 610 | else if (*tiling_mode == I915_TILING_X) |
| 611 | aligned_y = ALIGN(y, 8); |
| 612 | else if (*tiling_mode == I915_TILING_Y) |
| 613 | aligned_y = ALIGN(y, 32); |
| 614 | |
| 615 | stride = x * cpp; |
| 616 | stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, *tiling_mode); |
| 617 | size = stride * aligned_y; |
| 618 | size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode); |
| 619 | |
| 620 | bo = drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags); |
| 621 | if (!bo) |
| 622 | return NULL; |
| 623 | |
| 624 | ret = drm_intel_gem_bo_set_tiling(bo, tiling_mode, stride); |
| 625 | if (ret != 0) { |
| 626 | drm_intel_gem_bo_unreference(bo); |
| 627 | return NULL; |
| 628 | } |
| 629 | |
| 630 | *pitch = stride; |
| 631 | |
| 632 | return bo; |
Eric Anholt | 72abe98 | 2009-02-18 13:06:35 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 635 | /** |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 636 | * Returns a drm_intel_bo wrapping the given buffer object handle. |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 637 | * |
| 638 | * This can be used when one application needs to pass a buffer object |
| 639 | * to another. |
| 640 | */ |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 641 | drm_intel_bo * |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 642 | drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr, |
| 643 | const char *name, |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 644 | unsigned int handle) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 645 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 646 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr; |
| 647 | drm_intel_bo_gem *bo_gem; |
| 648 | int ret; |
| 649 | struct drm_gem_open open_arg; |
| 650 | struct drm_i915_gem_get_tiling get_tiling; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 651 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 652 | bo_gem = calloc(1, sizeof(*bo_gem)); |
| 653 | if (!bo_gem) |
| 654 | return NULL; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 655 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 656 | memset(&open_arg, 0, sizeof(open_arg)); |
| 657 | open_arg.name = handle; |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 658 | do { |
| 659 | ret = ioctl(bufmgr_gem->fd, |
| 660 | DRM_IOCTL_GEM_OPEN, |
| 661 | &open_arg); |
| 662 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 663 | if (ret != 0) { |
| 664 | fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n", |
| 665 | name, handle, strerror(errno)); |
| 666 | free(bo_gem); |
| 667 | return NULL; |
| 668 | } |
| 669 | bo_gem->bo.size = open_arg.size; |
| 670 | bo_gem->bo.offset = 0; |
| 671 | bo_gem->bo.virtual = NULL; |
| 672 | bo_gem->bo.bufmgr = bufmgr; |
| 673 | bo_gem->name = name; |
| 674 | atomic_set(&bo_gem->refcount, 1); |
| 675 | bo_gem->validate_index = -1; |
| 676 | bo_gem->gem_handle = open_arg.handle; |
| 677 | bo_gem->global_name = handle; |
| 678 | bo_gem->reusable = 0; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 679 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 680 | memset(&get_tiling, 0, sizeof(get_tiling)); |
| 681 | get_tiling.handle = bo_gem->gem_handle; |
| 682 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling); |
| 683 | if (ret != 0) { |
| 684 | drm_intel_gem_bo_unreference(&bo_gem->bo); |
| 685 | return NULL; |
| 686 | } |
| 687 | bo_gem->tiling_mode = get_tiling.tiling_mode; |
| 688 | bo_gem->swizzle_mode = get_tiling.swizzle_mode; |
| 689 | if (bo_gem->tiling_mode == I915_TILING_NONE) |
| 690 | bo_gem->reloc_tree_fences = 0; |
| 691 | else |
| 692 | bo_gem->reloc_tree_fences = 1; |
Chris Wilson | e22fb79 | 2009-11-30 22:14:30 +0000 | [diff] [blame] | 693 | drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem); |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 694 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 695 | DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 696 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 697 | return &bo_gem->bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 701 | drm_intel_gem_bo_free(drm_intel_bo *bo) |
Eric Anholt | 500c81d | 2008-06-06 17:13:16 -0700 | [diff] [blame] | 702 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 703 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 704 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 705 | struct drm_gem_close close; |
| 706 | int ret; |
Eric Anholt | 500c81d | 2008-06-06 17:13:16 -0700 | [diff] [blame] | 707 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 708 | if (bo_gem->mem_virtual) |
| 709 | munmap(bo_gem->mem_virtual, bo_gem->bo.size); |
| 710 | if (bo_gem->gtt_virtual) |
| 711 | munmap(bo_gem->gtt_virtual, bo_gem->bo.size); |
Eric Anholt | 500c81d | 2008-06-06 17:13:16 -0700 | [diff] [blame] | 712 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 713 | free(bo_gem->reloc_target_bo); |
| 714 | free(bo_gem->relocs); |
Eric Anholt | 12d9b7c | 2009-10-02 11:11:31 -0700 | [diff] [blame] | 715 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 716 | /* Close this object */ |
| 717 | memset(&close, 0, sizeof(close)); |
| 718 | close.handle = bo_gem->gem_handle; |
| 719 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close); |
| 720 | if (ret != 0) { |
| 721 | fprintf(stderr, |
| 722 | "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n", |
| 723 | bo_gem->gem_handle, bo_gem->name, strerror(errno)); |
| 724 | } |
| 725 | free(bo); |
Eric Anholt | 500c81d | 2008-06-06 17:13:16 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Eric Anholt | 3f3c5be | 2009-07-09 17:49:46 -0700 | [diff] [blame] | 728 | /** Frees all cached buffers significantly older than @time. */ |
| 729 | static void |
| 730 | drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time) |
| 731 | { |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 732 | int i; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 733 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 734 | for (i = 0; i < DRM_INTEL_GEM_BO_BUCKETS; i++) { |
| 735 | struct drm_intel_gem_bo_bucket *bucket = |
| 736 | &bufmgr_gem->cache_bucket[i]; |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 737 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 738 | while (!DRMLISTEMPTY(&bucket->head)) { |
| 739 | drm_intel_bo_gem *bo_gem; |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 740 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 741 | bo_gem = DRMLISTENTRY(drm_intel_bo_gem, |
| 742 | bucket->head.next, head); |
| 743 | if (time - bo_gem->free_time <= 1) |
| 744 | break; |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 745 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 746 | DRMLISTDEL(&bo_gem->head); |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 747 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 748 | drm_intel_gem_bo_free(&bo_gem->bo); |
| 749 | } |
| 750 | } |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 753 | static void |
| 754 | drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time) |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 755 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 756 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 757 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 758 | struct drm_intel_gem_bo_bucket *bucket; |
| 759 | uint32_t tiling_mode; |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 760 | int i; |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 761 | |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 762 | /* Unreference all the target buffers */ |
| 763 | for (i = 0; i < bo_gem->reloc_count; i++) { |
| 764 | drm_intel_gem_bo_unreference_locked_timed(bo_gem-> |
| 765 | reloc_target_bo[i], |
| 766 | time); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 767 | } |
Chris Wilson | b666f41 | 2009-11-30 23:07:19 +0000 | [diff] [blame] | 768 | bo_gem->reloc_count = 0; |
| 769 | bo_gem->used_as_reloc_target = 0; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 770 | |
| 771 | DBG("bo_unreference final: %d (%s)\n", |
| 772 | bo_gem->gem_handle, bo_gem->name); |
| 773 | |
| 774 | bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size); |
| 775 | /* Put the buffer into our internal cache for reuse if we can. */ |
| 776 | tiling_mode = I915_TILING_NONE; |
| 777 | if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL && |
Chris Wilson | 60aa803 | 2009-11-30 20:02:05 +0000 | [diff] [blame] | 778 | drm_intel_gem_bo_set_tiling(bo, &tiling_mode, 0) == 0 && |
| 779 | drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem, |
| 780 | I915_MADV_DONTNEED)) { |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 781 | bo_gem->free_time = time; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 782 | |
| 783 | bo_gem->name = NULL; |
| 784 | bo_gem->validate_index = -1; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 785 | |
| 786 | DRMLISTADDTAIL(&bo_gem->head, &bucket->head); |
| 787 | |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 788 | drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 789 | } else { |
| 790 | drm_intel_gem_bo_free(bo); |
| 791 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 794 | static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo, |
| 795 | time_t time) |
| 796 | { |
| 797 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 798 | |
| 799 | assert(atomic_read(&bo_gem->refcount) > 0); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 800 | if (atomic_dec_and_test(&bo_gem->refcount)) |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 801 | drm_intel_gem_bo_unreference_final(bo, time); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static void drm_intel_gem_bo_unreference(drm_intel_bo *bo) |
| 805 | { |
| 806 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 807 | |
| 808 | assert(atomic_read(&bo_gem->refcount) > 0); |
| 809 | if (atomic_dec_and_test(&bo_gem->refcount)) { |
| 810 | drm_intel_bufmgr_gem *bufmgr_gem = |
| 811 | (drm_intel_bufmgr_gem *) bo->bufmgr; |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 812 | struct timespec time; |
| 813 | |
| 814 | clock_gettime(CLOCK_MONOTONIC, &time); |
| 815 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 816 | pthread_mutex_lock(&bufmgr_gem->lock); |
Eric Anholt | 0d7ad7e | 2009-10-20 14:19:38 -0700 | [diff] [blame] | 817 | drm_intel_gem_bo_unreference_final(bo, time.tv_sec); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 818 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable) |
| 823 | { |
| 824 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 825 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 826 | struct drm_i915_gem_set_domain set_domain; |
| 827 | int ret; |
| 828 | |
Chris Wilson | 04495ee | 2009-10-02 04:39:22 +0100 | [diff] [blame] | 829 | pthread_mutex_lock(&bufmgr_gem->lock); |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 830 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 831 | /* Allow recursive mapping. Mesa may recursively map buffers with |
| 832 | * nested display loops. |
Carl Worth | afd245d | 2009-04-29 14:43:55 -0700 | [diff] [blame] | 833 | */ |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 834 | if (!bo_gem->mem_virtual) { |
| 835 | struct drm_i915_gem_mmap mmap_arg; |
Carl Worth | afd245d | 2009-04-29 14:43:55 -0700 | [diff] [blame] | 836 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 837 | DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name); |
| 838 | |
| 839 | memset(&mmap_arg, 0, sizeof(mmap_arg)); |
| 840 | mmap_arg.handle = bo_gem->gem_handle; |
| 841 | mmap_arg.offset = 0; |
| 842 | mmap_arg.size = bo->size; |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 843 | do { |
| 844 | ret = ioctl(bufmgr_gem->fd, |
| 845 | DRM_IOCTL_I915_GEM_MMAP, |
| 846 | &mmap_arg); |
| 847 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 848 | if (ret != 0) { |
| 849 | fprintf(stderr, |
| 850 | "%s:%d: Error mapping buffer %d (%s): %s .\n", |
| 851 | __FILE__, __LINE__, bo_gem->gem_handle, |
| 852 | bo_gem->name, strerror(errno)); |
| 853 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 854 | return ret; |
| 855 | } |
| 856 | bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr; |
| 857 | } |
| 858 | DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, |
| 859 | bo_gem->mem_virtual); |
| 860 | bo->virtual = bo_gem->mem_virtual; |
| 861 | |
| 862 | set_domain.handle = bo_gem->gem_handle; |
| 863 | set_domain.read_domains = I915_GEM_DOMAIN_CPU; |
| 864 | if (write_enable) |
| 865 | set_domain.write_domain = I915_GEM_DOMAIN_CPU; |
| 866 | else |
| 867 | set_domain.write_domain = 0; |
| 868 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 869 | ret = ioctl(bufmgr_gem->fd, |
| 870 | DRM_IOCTL_I915_GEM_SET_DOMAIN, |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 871 | &set_domain); |
| 872 | } while (ret == -1 && errno == EINTR); |
| 873 | if (ret != 0) { |
| 874 | fprintf(stderr, "%s:%d: Error setting to CPU domain %d: %s\n", |
| 875 | __FILE__, __LINE__, bo_gem->gem_handle, |
| 876 | strerror(errno)); |
| 877 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | int drm_intel_gem_bo_map_gtt(drm_intel_bo *bo) |
| 887 | { |
| 888 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 889 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 890 | struct drm_i915_gem_set_domain set_domain; |
| 891 | int ret; |
| 892 | |
| 893 | pthread_mutex_lock(&bufmgr_gem->lock); |
| 894 | |
| 895 | /* Get a mapping of the buffer if we haven't before. */ |
| 896 | if (bo_gem->gtt_virtual == NULL) { |
| 897 | struct drm_i915_gem_mmap_gtt mmap_arg; |
| 898 | |
| 899 | DBG("bo_map_gtt: mmap %d (%s)\n", bo_gem->gem_handle, |
| 900 | bo_gem->name); |
| 901 | |
| 902 | memset(&mmap_arg, 0, sizeof(mmap_arg)); |
| 903 | mmap_arg.handle = bo_gem->gem_handle; |
| 904 | |
| 905 | /* Get the fake offset back... */ |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 906 | do { |
| 907 | ret = ioctl(bufmgr_gem->fd, |
| 908 | DRM_IOCTL_I915_GEM_MMAP_GTT, |
| 909 | &mmap_arg); |
| 910 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 911 | if (ret != 0) { |
| 912 | fprintf(stderr, |
| 913 | "%s:%d: Error preparing buffer map %d (%s): %s .\n", |
| 914 | __FILE__, __LINE__, |
| 915 | bo_gem->gem_handle, bo_gem->name, |
| 916 | strerror(errno)); |
| 917 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | /* and mmap it */ |
| 922 | bo_gem->gtt_virtual = mmap(0, bo->size, PROT_READ | PROT_WRITE, |
| 923 | MAP_SHARED, bufmgr_gem->fd, |
| 924 | mmap_arg.offset); |
| 925 | if (bo_gem->gtt_virtual == MAP_FAILED) { |
| 926 | fprintf(stderr, |
| 927 | "%s:%d: Error mapping buffer %d (%s): %s .\n", |
| 928 | __FILE__, __LINE__, |
| 929 | bo_gem->gem_handle, bo_gem->name, |
| 930 | strerror(errno)); |
| 931 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 932 | return errno; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | bo->virtual = bo_gem->gtt_virtual; |
| 937 | |
| 938 | DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, |
| 939 | bo_gem->gtt_virtual); |
| 940 | |
| 941 | /* Now move it to the GTT domain so that the CPU caches are flushed */ |
| 942 | set_domain.handle = bo_gem->gem_handle; |
| 943 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
| 944 | set_domain.write_domain = I915_GEM_DOMAIN_GTT; |
| 945 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 946 | ret = ioctl(bufmgr_gem->fd, |
| 947 | DRM_IOCTL_I915_GEM_SET_DOMAIN, |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 948 | &set_domain); |
| 949 | } while (ret == -1 && errno == EINTR); |
| 950 | |
| 951 | if (ret != 0) { |
| 952 | fprintf(stderr, "%s:%d: Error setting domain %d: %s\n", |
| 953 | __FILE__, __LINE__, bo_gem->gem_handle, |
| 954 | strerror(errno)); |
| 955 | } |
| 956 | |
| 957 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 958 | |
Chris Wilson | 60aa803 | 2009-11-30 20:02:05 +0000 | [diff] [blame] | 959 | return ret; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | int drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo) |
| 963 | { |
| 964 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 965 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 966 | int ret = 0; |
| 967 | |
| 968 | if (bo == NULL) |
| 969 | return 0; |
| 970 | |
| 971 | assert(bo_gem->gtt_virtual != NULL); |
| 972 | |
| 973 | pthread_mutex_lock(&bufmgr_gem->lock); |
| 974 | bo->virtual = NULL; |
| 975 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 976 | |
| 977 | return ret; |
| 978 | } |
| 979 | |
| 980 | static int drm_intel_gem_bo_unmap(drm_intel_bo *bo) |
| 981 | { |
| 982 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 983 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 984 | struct drm_i915_gem_sw_finish sw_finish; |
| 985 | int ret; |
| 986 | |
| 987 | if (bo == NULL) |
| 988 | return 0; |
| 989 | |
| 990 | assert(bo_gem->mem_virtual != NULL); |
| 991 | |
| 992 | pthread_mutex_lock(&bufmgr_gem->lock); |
| 993 | |
| 994 | /* Cause a flush to happen if the buffer's pinned for scanout, so the |
| 995 | * results show up in a timely manner. |
| 996 | */ |
| 997 | sw_finish.handle = bo_gem->gem_handle; |
| 998 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 999 | ret = ioctl(bufmgr_gem->fd, |
| 1000 | DRM_IOCTL_I915_GEM_SW_FINISH, |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1001 | &sw_finish); |
| 1002 | } while (ret == -1 && errno == EINTR); |
| 1003 | |
| 1004 | bo->virtual = NULL; |
| 1005 | pthread_mutex_unlock(&bufmgr_gem->lock); |
| 1006 | return 0; |
Carl Worth | afd245d | 2009-04-29 14:43:55 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1009 | static int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1010 | drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset, |
| 1011 | unsigned long size, const void *data) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1012 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1013 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1014 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1015 | struct drm_i915_gem_pwrite pwrite; |
| 1016 | int ret; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1017 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1018 | memset(&pwrite, 0, sizeof(pwrite)); |
| 1019 | pwrite.handle = bo_gem->gem_handle; |
| 1020 | pwrite.offset = offset; |
| 1021 | pwrite.size = size; |
| 1022 | pwrite.data_ptr = (uint64_t) (uintptr_t) data; |
| 1023 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 1024 | ret = ioctl(bufmgr_gem->fd, |
| 1025 | DRM_IOCTL_I915_GEM_PWRITE, |
| 1026 | &pwrite); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1027 | } while (ret == -1 && errno == EINTR); |
| 1028 | if (ret != 0) { |
| 1029 | fprintf(stderr, |
| 1030 | "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n", |
| 1031 | __FILE__, __LINE__, bo_gem->gem_handle, (int)offset, |
| 1032 | (int)size, strerror(errno)); |
| 1033 | } |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | static int |
| 1038 | drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id) |
| 1039 | { |
| 1040 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr; |
| 1041 | struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id; |
| 1042 | int ret; |
| 1043 | |
| 1044 | get_pipe_from_crtc_id.crtc_id = crtc_id; |
| 1045 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID, |
| 1046 | &get_pipe_from_crtc_id); |
| 1047 | if (ret != 0) { |
| 1048 | /* We return -1 here to signal that we don't |
| 1049 | * know which pipe is associated with this crtc. |
| 1050 | * This lets the caller know that this information |
| 1051 | * isn't available; using the wrong pipe for |
| 1052 | * vblank waiting can cause the chipset to lock up |
| 1053 | */ |
| 1054 | return -1; |
| 1055 | } |
| 1056 | |
| 1057 | return get_pipe_from_crtc_id.pipe; |
| 1058 | } |
| 1059 | |
| 1060 | static int |
| 1061 | drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset, |
| 1062 | unsigned long size, void *data) |
| 1063 | { |
| 1064 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1065 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1066 | struct drm_i915_gem_pread pread; |
| 1067 | int ret; |
| 1068 | |
| 1069 | memset(&pread, 0, sizeof(pread)); |
| 1070 | pread.handle = bo_gem->gem_handle; |
| 1071 | pread.offset = offset; |
| 1072 | pread.size = size; |
| 1073 | pread.data_ptr = (uint64_t) (uintptr_t) data; |
| 1074 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 1075 | ret = ioctl(bufmgr_gem->fd, |
| 1076 | DRM_IOCTL_I915_GEM_PREAD, |
| 1077 | &pread); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1078 | } while (ret == -1 && errno == EINTR); |
| 1079 | if (ret != 0) { |
| 1080 | fprintf(stderr, |
| 1081 | "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n", |
| 1082 | __FILE__, __LINE__, bo_gem->gem_handle, (int)offset, |
| 1083 | (int)size, strerror(errno)); |
| 1084 | } |
| 1085 | return 0; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Eric Anholt | 6fb1ad7 | 2008-11-13 11:44:22 -0800 | [diff] [blame] | 1088 | /** Waits for all GPU rendering to the object to have completed. */ |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1089 | static void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1090 | drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1091 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1092 | drm_intel_gem_bo_start_gtt_access(bo, 0); |
Eric Anholt | 6fb1ad7 | 2008-11-13 11:44:22 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Sets the object to the GTT read and possibly write domain, used by the X |
| 1097 | * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt(). |
| 1098 | * |
| 1099 | * In combination with drm_intel_gem_bo_pin() and manual fence management, we |
| 1100 | * can do tiled pixmaps this way. |
| 1101 | */ |
| 1102 | void |
| 1103 | drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable) |
| 1104 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1105 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1106 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1107 | struct drm_i915_gem_set_domain set_domain; |
| 1108 | int ret; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1109 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1110 | set_domain.handle = bo_gem->gem_handle; |
| 1111 | set_domain.read_domains = I915_GEM_DOMAIN_GTT; |
| 1112 | set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0; |
| 1113 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 1114 | ret = ioctl(bufmgr_gem->fd, |
| 1115 | DRM_IOCTL_I915_GEM_SET_DOMAIN, |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1116 | &set_domain); |
| 1117 | } while (ret == -1 && errno == EINTR); |
| 1118 | if (ret != 0) { |
| 1119 | fprintf(stderr, |
| 1120 | "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n", |
| 1121 | __FILE__, __LINE__, bo_gem->gem_handle, |
| 1122 | set_domain.read_domains, set_domain.write_domain, |
| 1123 | strerror(errno)); |
| 1124 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1128 | drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1129 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1130 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr; |
| 1131 | int i; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1132 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1133 | free(bufmgr_gem->exec_objects); |
| 1134 | free(bufmgr_gem->exec_bos); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1135 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1136 | pthread_mutex_destroy(&bufmgr_gem->lock); |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 1137 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1138 | /* Free any cached buffer objects we were going to reuse */ |
| 1139 | for (i = 0; i < DRM_INTEL_GEM_BO_BUCKETS; i++) { |
| 1140 | struct drm_intel_gem_bo_bucket *bucket = |
| 1141 | &bufmgr_gem->cache_bucket[i]; |
| 1142 | drm_intel_bo_gem *bo_gem; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1143 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1144 | while (!DRMLISTEMPTY(&bucket->head)) { |
| 1145 | bo_gem = DRMLISTENTRY(drm_intel_bo_gem, |
| 1146 | bucket->head.next, head); |
| 1147 | DRMLISTDEL(&bo_gem->head); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1148 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1149 | drm_intel_gem_bo_free(&bo_gem->bo); |
| 1150 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1151 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1152 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1153 | free(bufmgr); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * Adds the target buffer to the validation list and adds the relocation |
| 1158 | * to the reloc_buffer's relocation list. |
| 1159 | * |
| 1160 | * The relocation entry at the given offset must already contain the |
| 1161 | * precomputed relocation value, because the kernel will optimize out |
| 1162 | * the relocation entry write when the buffer hasn't moved from the |
| 1163 | * last known offset in target_bo. |
| 1164 | */ |
| 1165 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1166 | drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset, |
| 1167 | drm_intel_bo *target_bo, uint32_t target_offset, |
| 1168 | uint32_t read_domains, uint32_t write_domain) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1169 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1170 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1171 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1172 | drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1173 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1174 | pthread_mutex_lock(&bufmgr_gem->lock); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1175 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1176 | /* Create a new relocation list if needed */ |
| 1177 | if (bo_gem->relocs == NULL) |
| 1178 | drm_intel_setup_reloc_list(bo); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1179 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1180 | /* Check overflow */ |
| 1181 | assert(bo_gem->reloc_count < bufmgr_gem->max_relocs); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1182 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1183 | /* Check args */ |
| 1184 | assert(offset <= bo->size - 4); |
| 1185 | assert((write_domain & (write_domain - 1)) == 0); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1186 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1187 | /* Make sure that we're not adding a reloc to something whose size has |
| 1188 | * already been accounted for. |
| 1189 | */ |
| 1190 | assert(!bo_gem->used_as_reloc_target); |
| 1191 | bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size; |
| 1192 | bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1193 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1194 | /* Flag the target to disallow further relocations in it. */ |
| 1195 | target_bo_gem->used_as_reloc_target = 1; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1196 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1197 | bo_gem->relocs[bo_gem->reloc_count].offset = offset; |
| 1198 | bo_gem->relocs[bo_gem->reloc_count].delta = target_offset; |
| 1199 | bo_gem->relocs[bo_gem->reloc_count].target_handle = |
| 1200 | target_bo_gem->gem_handle; |
| 1201 | bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains; |
| 1202 | bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain; |
| 1203 | bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1204 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1205 | bo_gem->reloc_target_bo[bo_gem->reloc_count] = target_bo; |
| 1206 | drm_intel_gem_bo_reference(target_bo); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1207 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1208 | bo_gem->reloc_count++; |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 1209 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1210 | pthread_mutex_unlock(&bufmgr_gem->lock); |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 1211 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1212 | return 0; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * Walk the tree of relocations rooted at BO and accumulate the list of |
| 1217 | * validations to be performed and update the relocation buffers with |
| 1218 | * index values into the validation list. |
| 1219 | */ |
| 1220 | static void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1221 | drm_intel_gem_bo_process_reloc(drm_intel_bo *bo) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1222 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1223 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1224 | int i; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1225 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1226 | if (bo_gem->relocs == NULL) |
| 1227 | return; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1228 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1229 | for (i = 0; i < bo_gem->reloc_count; i++) { |
| 1230 | drm_intel_bo *target_bo = bo_gem->reloc_target_bo[i]; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1231 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1232 | /* Continue walking the tree depth-first. */ |
| 1233 | drm_intel_gem_bo_process_reloc(target_bo); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1234 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1235 | /* Add the target to the validate list */ |
| 1236 | drm_intel_add_validate_buffer(target_bo); |
| 1237 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1240 | static void |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1241 | drm_intel_update_buffer_offsets(drm_intel_bufmgr_gem *bufmgr_gem) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1242 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1243 | int i; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1244 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1245 | for (i = 0; i < bufmgr_gem->exec_count; i++) { |
| 1246 | drm_intel_bo *bo = bufmgr_gem->exec_bos[i]; |
| 1247 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1248 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1249 | /* Update the buffer offset */ |
| 1250 | if (bufmgr_gem->exec_objects[i].offset != bo->offset) { |
| 1251 | DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n", |
| 1252 | bo_gem->gem_handle, bo_gem->name, bo->offset, |
| 1253 | (unsigned long long)bufmgr_gem->exec_objects[i]. |
| 1254 | offset); |
| 1255 | bo->offset = bufmgr_gem->exec_objects[i].offset; |
| 1256 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1257 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Eric Anholt | f9d98be | 2008-09-08 08:51:40 -0700 | [diff] [blame] | 1260 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1261 | drm_intel_gem_bo_exec(drm_intel_bo *bo, int used, |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1262 | drm_clip_rect_t * cliprects, int num_cliprects, int DR4) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1263 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1264 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1265 | struct drm_i915_gem_execbuffer execbuf; |
| 1266 | int ret, i; |
Eric Anholt | f9d98be | 2008-09-08 08:51:40 -0700 | [diff] [blame] | 1267 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1268 | pthread_mutex_lock(&bufmgr_gem->lock); |
| 1269 | /* Update indices and set up the validate list. */ |
| 1270 | drm_intel_gem_bo_process_reloc(bo); |
Eric Anholt | f9d98be | 2008-09-08 08:51:40 -0700 | [diff] [blame] | 1271 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1272 | /* Add the batch buffer to the validation list. There are no |
| 1273 | * relocations pointing to it. |
| 1274 | */ |
| 1275 | drm_intel_add_validate_buffer(bo); |
Eric Anholt | f9d98be | 2008-09-08 08:51:40 -0700 | [diff] [blame] | 1276 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1277 | execbuf.buffers_ptr = (uintptr_t) bufmgr_gem->exec_objects; |
| 1278 | execbuf.buffer_count = bufmgr_gem->exec_count; |
| 1279 | execbuf.batch_start_offset = 0; |
| 1280 | execbuf.batch_len = used; |
| 1281 | execbuf.cliprects_ptr = (uintptr_t) cliprects; |
| 1282 | execbuf.num_cliprects = num_cliprects; |
| 1283 | execbuf.DR1 = 0; |
| 1284 | execbuf.DR4 = DR4; |
Eric Anholt | f9d98be | 2008-09-08 08:51:40 -0700 | [diff] [blame] | 1285 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1286 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 1287 | ret = ioctl(bufmgr_gem->fd, |
| 1288 | DRM_IOCTL_I915_GEM_EXECBUFFER, |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1289 | &execbuf); |
| 1290 | } while (ret != 0 && errno == EAGAIN); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1291 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1292 | if (ret != 0 && errno == ENOMEM) { |
| 1293 | fprintf(stderr, |
| 1294 | "Execbuffer fails to pin. " |
| 1295 | "Estimate: %u. Actual: %u. Available: %u\n", |
| 1296 | drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos, |
| 1297 | bufmgr_gem-> |
| 1298 | exec_count), |
| 1299 | drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos, |
| 1300 | bufmgr_gem-> |
| 1301 | exec_count), |
| 1302 | (unsigned int)bufmgr_gem->gtt_size); |
| 1303 | } |
| 1304 | drm_intel_update_buffer_offsets(bufmgr_gem); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1305 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1306 | if (bufmgr_gem->bufmgr.debug) |
| 1307 | drm_intel_gem_dump_validation_list(bufmgr_gem); |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1308 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1309 | for (i = 0; i < bufmgr_gem->exec_count; i++) { |
| 1310 | drm_intel_bo *bo = bufmgr_gem->exec_bos[i]; |
| 1311 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1312 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1313 | /* Disconnect the buffer from the validate list */ |
| 1314 | bo_gem->validate_index = -1; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1315 | bufmgr_gem->exec_bos[i] = NULL; |
| 1316 | } |
| 1317 | bufmgr_gem->exec_count = 0; |
| 1318 | pthread_mutex_unlock(&bufmgr_gem->lock); |
Eric Anholt | f9d98be | 2008-09-08 08:51:40 -0700 | [diff] [blame] | 1319 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1320 | return 0; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1323 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1324 | drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment) |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1325 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1326 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1327 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1328 | struct drm_i915_gem_pin pin; |
| 1329 | int ret; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1330 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1331 | memset(&pin, 0, sizeof(pin)); |
| 1332 | pin.handle = bo_gem->gem_handle; |
| 1333 | pin.alignment = alignment; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1334 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1335 | do { |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 1336 | ret = ioctl(bufmgr_gem->fd, |
| 1337 | DRM_IOCTL_I915_GEM_PIN, |
| 1338 | &pin); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1339 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | 02445ea | 2009-01-04 17:37:18 -0800 | [diff] [blame] | 1340 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1341 | if (ret != 0) |
| 1342 | return -errno; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1343 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1344 | bo->offset = pin.offset; |
| 1345 | return 0; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1349 | drm_intel_gem_bo_unpin(drm_intel_bo *bo) |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1350 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1351 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1352 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1353 | struct drm_i915_gem_unpin unpin; |
| 1354 | int ret; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1355 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1356 | memset(&unpin, 0, sizeof(unpin)); |
| 1357 | unpin.handle = bo_gem->gem_handle; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1358 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1359 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin); |
| 1360 | if (ret != 0) |
| 1361 | return -errno; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1362 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1363 | return 0; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | static int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1367 | drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode, |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1368 | uint32_t stride) |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1369 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1370 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1371 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1372 | struct drm_i915_gem_set_tiling set_tiling; |
| 1373 | int ret; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1374 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1375 | if (bo_gem->global_name == 0 && *tiling_mode == bo_gem->tiling_mode) |
| 1376 | return 0; |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 1377 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1378 | /* If we're going from non-tiling to tiling, bump fence count */ |
| 1379 | if (bo_gem->tiling_mode == I915_TILING_NONE) |
| 1380 | bo_gem->reloc_tree_fences++; |
Eric Anholt | 9209c9a | 2009-01-27 16:54:11 -0800 | [diff] [blame] | 1381 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1382 | memset(&set_tiling, 0, sizeof(set_tiling)); |
| 1383 | set_tiling.handle = bo_gem->gem_handle; |
| 1384 | set_tiling.tiling_mode = *tiling_mode; |
| 1385 | set_tiling.stride = stride; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1386 | |
Chris Wilson | 8ffd2e1 | 2009-12-01 13:08:04 +0000 | [diff] [blame] | 1387 | do { |
| 1388 | ret = ioctl(bufmgr_gem->fd, |
| 1389 | DRM_IOCTL_I915_GEM_SET_TILING, |
| 1390 | &set_tiling); |
| 1391 | } while (ret == -1 && errno == EINTR); |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1392 | if (ret != 0) { |
| 1393 | *tiling_mode = bo_gem->tiling_mode; |
| 1394 | return -errno; |
| 1395 | } |
| 1396 | bo_gem->tiling_mode = set_tiling.tiling_mode; |
| 1397 | bo_gem->swizzle_mode = set_tiling.swizzle_mode; |
| 1398 | |
| 1399 | /* If we're going from tiling to non-tiling, drop fence count */ |
| 1400 | if (bo_gem->tiling_mode == I915_TILING_NONE) |
| 1401 | bo_gem->reloc_tree_fences--; |
| 1402 | |
Chris Wilson | e22fb79 | 2009-11-30 22:14:30 +0000 | [diff] [blame] | 1403 | drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem); |
| 1404 | |
Keith Packard | 18f091d | 2008-12-15 15:08:12 -0800 | [diff] [blame] | 1405 | *tiling_mode = bo_gem->tiling_mode; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1406 | return 0; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | static int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1410 | drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode, |
| 1411 | uint32_t * swizzle_mode) |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1412 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1413 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
Eric Anholt | 9933838 | 2008-10-14 13:18:11 -0700 | [diff] [blame] | 1414 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1415 | *tiling_mode = bo_gem->tiling_mode; |
| 1416 | *swizzle_mode = bo_gem->swizzle_mode; |
| 1417 | return 0; |
Eric Anholt | 9933838 | 2008-10-14 13:18:11 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1421 | drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name) |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1422 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1423 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr; |
| 1424 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1425 | struct drm_gem_flink flink; |
| 1426 | int ret; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1427 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1428 | if (!bo_gem->global_name) { |
| 1429 | memset(&flink, 0, sizeof(flink)); |
| 1430 | flink.handle = bo_gem->gem_handle; |
| 1431 | |
| 1432 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink); |
| 1433 | if (ret != 0) |
| 1434 | return -errno; |
| 1435 | bo_gem->global_name = flink.name; |
| 1436 | bo_gem->reusable = 0; |
| 1437 | } |
| 1438 | |
| 1439 | *name = bo_gem->global_name; |
| 1440 | return 0; |
Keith Packard | 8e41ce1 | 2008-08-04 00:34:08 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1443 | /** |
| 1444 | * Enables unlimited caching of buffer objects for reuse. |
| 1445 | * |
| 1446 | * This is potentially very memory expensive, as the cache at each bucket |
| 1447 | * size is only bounded by how many buffers of that size we've managed to have |
| 1448 | * in flight at once. |
| 1449 | */ |
| 1450 | void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1451 | drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1452 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1453 | drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1454 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1455 | bufmgr_gem->bo_reuse = 1; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1458 | /** |
| 1459 | * Return the additional aperture space required by the tree of buffer objects |
| 1460 | * rooted at bo. |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1461 | */ |
| 1462 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1463 | drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1464 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1465 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1466 | int i; |
| 1467 | int total = 0; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1468 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1469 | if (bo == NULL || bo_gem->included_in_check_aperture) |
| 1470 | return 0; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1471 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1472 | total += bo->size; |
| 1473 | bo_gem->included_in_check_aperture = 1; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1474 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1475 | for (i = 0; i < bo_gem->reloc_count; i++) |
| 1476 | total += |
| 1477 | drm_intel_gem_bo_get_aperture_space(bo_gem-> |
| 1478 | reloc_target_bo[i]); |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1479 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1480 | return total; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | /** |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1484 | * Count the number of buffers in this list that need a fence reg |
| 1485 | * |
| 1486 | * If the count is greater than the number of available regs, we'll have |
| 1487 | * to ask the caller to resubmit a batch with fewer tiled buffers. |
| 1488 | * |
Eric Anholt | 9209c9a | 2009-01-27 16:54:11 -0800 | [diff] [blame] | 1489 | * This function over-counts if the same buffer is used multiple times. |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1490 | */ |
| 1491 | static unsigned int |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1492 | drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count) |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1493 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1494 | int i; |
| 1495 | unsigned int total = 0; |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1496 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1497 | for (i = 0; i < count; i++) { |
| 1498 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i]; |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1499 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1500 | if (bo_gem == NULL) |
| 1501 | continue; |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1502 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1503 | total += bo_gem->reloc_tree_fences; |
| 1504 | } |
| 1505 | return total; |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | /** |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1509 | * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready |
| 1510 | * for the next drm_intel_bufmgr_check_aperture_space() call. |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1511 | */ |
| 1512 | static void |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1513 | drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo) |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1514 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1515 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1516 | int i; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1517 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1518 | if (bo == NULL || !bo_gem->included_in_check_aperture) |
| 1519 | return; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1520 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1521 | bo_gem->included_in_check_aperture = 0; |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1522 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1523 | for (i = 0; i < bo_gem->reloc_count; i++) |
| 1524 | drm_intel_gem_bo_clear_aperture_space_flag(bo_gem-> |
| 1525 | reloc_target_bo[i]); |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /** |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 1529 | * Return a conservative estimate for the amount of aperture required |
| 1530 | * for a collection of buffers. This may double-count some buffers. |
| 1531 | */ |
| 1532 | static unsigned int |
| 1533 | drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count) |
| 1534 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1535 | int i; |
| 1536 | unsigned int total = 0; |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 1537 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1538 | for (i = 0; i < count; i++) { |
| 1539 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i]; |
| 1540 | if (bo_gem != NULL) |
| 1541 | total += bo_gem->reloc_tree_size; |
| 1542 | } |
| 1543 | return total; |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * Return the amount of aperture needed for a collection of buffers. |
| 1548 | * This avoids double counting any buffers, at the cost of looking |
| 1549 | * at every buffer in the set. |
| 1550 | */ |
| 1551 | static unsigned int |
| 1552 | drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count) |
| 1553 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1554 | int i; |
| 1555 | unsigned int total = 0; |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 1556 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1557 | for (i = 0; i < count; i++) { |
| 1558 | total += drm_intel_gem_bo_get_aperture_space(bo_array[i]); |
| 1559 | /* For the first buffer object in the array, we get an |
| 1560 | * accurate count back for its reloc_tree size (since nothing |
| 1561 | * had been flagged as being counted yet). We can save that |
| 1562 | * value out as a more conservative reloc_tree_size that |
| 1563 | * avoids double-counting target buffers. Since the first |
| 1564 | * buffer happens to usually be the batch buffer in our |
| 1565 | * callers, this can pull us back from doing the tree |
| 1566 | * walk on every new batch emit. |
| 1567 | */ |
| 1568 | if (i == 0) { |
| 1569 | drm_intel_bo_gem *bo_gem = |
| 1570 | (drm_intel_bo_gem *) bo_array[i]; |
| 1571 | bo_gem->reloc_tree_size = total; |
| 1572 | } |
Eric Anholt | 7ce8d4c | 2009-02-27 13:46:31 -0800 | [diff] [blame] | 1573 | } |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 1574 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1575 | for (i = 0; i < count; i++) |
| 1576 | drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]); |
| 1577 | return total; |
Keith Packard | b13f4e1 | 2008-11-21 01:49:39 -0800 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /** |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1581 | * Return -1 if the batchbuffer should be flushed before attempting to |
| 1582 | * emit rendering referencing the buffers pointed to by bo_array. |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1583 | * |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1584 | * This is required because if we try to emit a batchbuffer with relocations |
| 1585 | * to a tree of buffers that won't simultaneously fit in the aperture, |
| 1586 | * the rendering will return an error at a point where the software is not |
| 1587 | * prepared to recover from it. |
| 1588 | * |
| 1589 | * However, we also want to emit the batchbuffer significantly before we reach |
| 1590 | * the limit, as a series of batchbuffers each of which references buffers |
| 1591 | * covering almost all of the aperture means that at each emit we end up |
| 1592 | * waiting to evict a buffer from the last rendering, and we get synchronous |
| 1593 | * performance. By emitting smaller batchbuffers, we eat some CPU overhead to |
| 1594 | * get better parallelism. |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1595 | */ |
| 1596 | static int |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1597 | drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1598 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1599 | drm_intel_bufmgr_gem *bufmgr_gem = |
| 1600 | (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr; |
| 1601 | unsigned int total = 0; |
| 1602 | unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4; |
| 1603 | int total_fences; |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1604 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1605 | /* Check for fence reg constraints if necessary */ |
| 1606 | if (bufmgr_gem->available_fences) { |
| 1607 | total_fences = drm_intel_gem_total_fences(bo_array, count); |
| 1608 | if (total_fences > bufmgr_gem->available_fences) |
| 1609 | return -1; |
| 1610 | } |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1611 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1612 | total = drm_intel_gem_estimate_batch_space(bo_array, count); |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1613 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1614 | if (total > threshold) |
| 1615 | total = drm_intel_gem_compute_batch_space(bo_array, count); |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1616 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1617 | if (total > threshold) { |
| 1618 | DBG("check_space: overflowed available aperture, " |
| 1619 | "%dkb vs %dkb\n", |
| 1620 | total / 1024, (int)bufmgr_gem->gtt_size / 1024); |
| 1621 | return -1; |
| 1622 | } else { |
| 1623 | DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024, |
| 1624 | (int)bufmgr_gem->gtt_size / 1024); |
| 1625 | return 0; |
| 1626 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Keith Packard | 5b5ce30 | 2009-05-11 13:42:12 -0700 | [diff] [blame] | 1629 | /* |
| 1630 | * Disable buffer reuse for objects which are shared with the kernel |
| 1631 | * as scanout buffers |
| 1632 | */ |
| 1633 | static int |
| 1634 | drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo) |
| 1635 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1636 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
Keith Packard | 5b5ce30 | 2009-05-11 13:42:12 -0700 | [diff] [blame] | 1637 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1638 | bo_gem->reusable = 0; |
| 1639 | return 0; |
Keith Packard | 5b5ce30 | 2009-05-11 13:42:12 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1642 | static int |
Eric Anholt | 66d2714 | 2009-10-20 13:20:55 -0700 | [diff] [blame] | 1643 | _drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo) |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1644 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1645 | drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo; |
| 1646 | int i; |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1647 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1648 | for (i = 0; i < bo_gem->reloc_count; i++) { |
| 1649 | if (bo_gem->reloc_target_bo[i] == target_bo) |
| 1650 | return 1; |
Eric Anholt | 66d2714 | 2009-10-20 13:20:55 -0700 | [diff] [blame] | 1651 | if (_drm_intel_gem_bo_references(bo_gem->reloc_target_bo[i], |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1652 | target_bo)) |
| 1653 | return 1; |
| 1654 | } |
| 1655 | |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1656 | return 0; |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Eric Anholt | 66d2714 | 2009-10-20 13:20:55 -0700 | [diff] [blame] | 1659 | /** Return true if target_bo is referenced by bo's relocation tree. */ |
| 1660 | static int |
| 1661 | drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo) |
| 1662 | { |
| 1663 | drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo; |
| 1664 | |
| 1665 | if (bo == NULL || target_bo == NULL) |
| 1666 | return 0; |
| 1667 | if (target_bo_gem->used_as_reloc_target) |
| 1668 | return _drm_intel_gem_bo_references(bo, target_bo); |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1672 | /** |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1673 | * Initializes the GEM buffer manager, which uses the kernel to allocate, map, |
| 1674 | * and manage map buffer objections. |
| 1675 | * |
| 1676 | * \param fd File descriptor of the opened DRM device. |
| 1677 | */ |
Eric Anholt | 4b98264 | 2008-10-30 09:33:07 -0700 | [diff] [blame] | 1678 | drm_intel_bufmgr * |
| 1679 | drm_intel_bufmgr_gem_init(int fd, int batch_size) |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1680 | { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1681 | drm_intel_bufmgr_gem *bufmgr_gem; |
| 1682 | struct drm_i915_gem_get_aperture aperture; |
| 1683 | drm_i915_getparam_t gp; |
| 1684 | int ret, i; |
| 1685 | unsigned long size; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1686 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1687 | bufmgr_gem = calloc(1, sizeof(*bufmgr_gem)); |
| 1688 | bufmgr_gem->fd = fd; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1689 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1690 | if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) { |
| 1691 | free(bufmgr_gem); |
| 1692 | return NULL; |
| 1693 | } |
Eric Anholt | 6df7b07 | 2008-06-12 23:22:26 -0700 | [diff] [blame] | 1694 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1695 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture); |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1696 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1697 | if (ret == 0) |
| 1698 | bufmgr_gem->gtt_size = aperture.aper_available_size; |
| 1699 | else { |
| 1700 | fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n", |
| 1701 | strerror(errno)); |
| 1702 | bufmgr_gem->gtt_size = 128 * 1024 * 1024; |
| 1703 | fprintf(stderr, "Assuming %dkB available aperture size.\n" |
| 1704 | "May lead to reduced performance or incorrect " |
| 1705 | "rendering.\n", |
| 1706 | (int)bufmgr_gem->gtt_size / 1024); |
| 1707 | } |
Eric Anholt | 0e86731 | 2008-10-21 00:10:54 -0700 | [diff] [blame] | 1708 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1709 | gp.param = I915_PARAM_CHIPSET_ID; |
| 1710 | gp.value = &bufmgr_gem->pci_device; |
Eric Anholt | cbdd627 | 2009-01-27 17:16:11 -0800 | [diff] [blame] | 1711 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp); |
| 1712 | if (ret) { |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1713 | fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno); |
| 1714 | fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value); |
Eric Anholt | cbdd627 | 2009-01-27 17:16:11 -0800 | [diff] [blame] | 1715 | } |
Jesse Barnes | 2fa5f28 | 2009-01-23 14:13:45 -0800 | [diff] [blame] | 1716 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1717 | if (!IS_I965G(bufmgr_gem)) { |
| 1718 | gp.param = I915_PARAM_NUM_FENCES_AVAIL; |
| 1719 | gp.value = &bufmgr_gem->available_fences; |
| 1720 | ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp); |
| 1721 | if (ret) { |
| 1722 | fprintf(stderr, "get fences failed: %d [%d]\n", ret, |
| 1723 | errno); |
| 1724 | fprintf(stderr, "param: %d, val: %d\n", gp.param, |
| 1725 | *gp.value); |
| 1726 | bufmgr_gem->available_fences = 0; |
| 1727 | } |
| 1728 | } |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1729 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1730 | /* Let's go with one relocation per every 2 dwords (but round down a bit |
| 1731 | * since a power of two will mean an extra page allocation for the reloc |
| 1732 | * buffer). |
| 1733 | * |
| 1734 | * Every 4 was too few for the blender benchmark. |
| 1735 | */ |
| 1736 | bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2; |
Eric Anholt | 769b105 | 2009-10-01 19:09:26 -0700 | [diff] [blame] | 1737 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1738 | bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc; |
| 1739 | bufmgr_gem->bufmgr.bo_alloc_for_render = |
| 1740 | drm_intel_gem_bo_alloc_for_render; |
Jesse Barnes | 3a7dfcd | 2009-10-06 14:34:06 -0700 | [diff] [blame] | 1741 | bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1742 | bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference; |
| 1743 | bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference; |
| 1744 | bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map; |
| 1745 | bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap; |
| 1746 | bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata; |
| 1747 | bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata; |
| 1748 | bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering; |
| 1749 | bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc; |
| 1750 | bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin; |
| 1751 | bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin; |
| 1752 | bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling; |
| 1753 | bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling; |
| 1754 | bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink; |
| 1755 | bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec; |
| 1756 | bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy; |
Chris Wilson | 83a35b6 | 2009-11-11 13:04:38 +0000 | [diff] [blame] | 1757 | bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise; |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1758 | bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_destroy; |
| 1759 | bufmgr_gem->bufmgr.debug = 0; |
| 1760 | bufmgr_gem->bufmgr.check_aperture_space = |
| 1761 | drm_intel_gem_check_aperture_space; |
| 1762 | bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse; |
| 1763 | bufmgr_gem->bufmgr.get_pipe_from_crtc_id = |
| 1764 | drm_intel_gem_get_pipe_from_crtc_id; |
| 1765 | bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1766 | |
Eric Anholt | d70d605 | 2009-10-06 12:40:42 -0700 | [diff] [blame] | 1767 | /* Initialize the linked lists for BO reuse cache. */ |
| 1768 | for (i = 0, size = 4096; i < DRM_INTEL_GEM_BO_BUCKETS; i++, size *= 2) { |
| 1769 | DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head); |
| 1770 | bufmgr_gem->cache_bucket[i].size = size; |
| 1771 | } |
| 1772 | |
| 1773 | return &bufmgr_gem->bufmgr; |
Eric Anholt | 6a9eb08 | 2008-06-03 09:27:37 -0700 | [diff] [blame] | 1774 | } |