blob: 398c7d074f24b8b035f1bd00e3950cae70e8f9c8 [file] [log] [blame]
Eric Anholt6a9eb082008-06-03 09:27:37 -07001/**************************************************************************
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 Anholt368b3922008-09-10 13:54:34 -070037#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
Eric Anholt6a9eb082008-06-03 09:27:37 -070041#include <xf86drm.h>
Pauli Nieminen21105bc2010-03-10 13:35:59 +020042#include <xf86atomic.h>
Jesse Barnes276c07d2008-11-13 13:52:04 -080043#include <fcntl.h>
Eric Anholt6a9eb082008-06-03 09:27:37 -070044#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48#include <assert.h>
Eric Anholt6df7b072008-06-12 23:22:26 -070049#include <pthread.h>
Eric Anholt6a9eb082008-06-03 09:27:37 -070050#include <sys/ioctl.h>
51#include <sys/mman.h>
Jesse Barnes276c07d2008-11-13 13:52:04 -080052#include <sys/stat.h>
53#include <sys/types.h>
Eric Anholt6a9eb082008-06-03 09:27:37 -070054
55#include "errno.h"
Eric Anholt72abe982009-02-18 13:06:35 -080056#include "libdrm_lists.h"
Eric Anholtc4857422008-06-03 10:20:49 -070057#include "intel_bufmgr.h"
Eric Anholt738e36a2008-09-05 10:35:32 +010058#include "intel_bufmgr_priv.h"
Eric Anholtcbdd6272009-01-27 17:16:11 -080059#include "intel_chipset.h"
Eric Anholt6a9eb082008-06-03 09:27:37 -070060#include "string.h"
Eric Anholt6a9eb082008-06-03 09:27:37 -070061
62#include "i915_drm.h"
63
Eric Anholt6a9eb082008-06-03 09:27:37 -070064#define DBG(...) do { \
Eric Anholtd70d6052009-10-06 12:40:42 -070065 if (bufmgr_gem->bufmgr.debug) \
66 fprintf(stderr, __VA_ARGS__); \
Eric Anholt6a9eb082008-06-03 09:27:37 -070067} while (0)
68
Eric Anholt4b982642008-10-30 09:33:07 -070069typedef struct _drm_intel_bo_gem drm_intel_bo_gem;
Keith Packarda919ff52008-06-05 15:58:09 -070070
Eric Anholt4b982642008-10-30 09:33:07 -070071struct drm_intel_gem_bo_bucket {
Eric Anholtd70d6052009-10-06 12:40:42 -070072 drmMMListHead head;
73 unsigned long size;
Eric Anholt6a9eb082008-06-03 09:27:37 -070074};
75
Eric Anholt469655f2009-05-18 16:07:45 -070076/* 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 Anholt6a9eb082008-06-03 09:27:37 -070078 */
Eric Anholt469655f2009-05-18 16:07:45 -070079#define DRM_INTEL_GEM_BO_BUCKETS 14
Eric Anholt4b982642008-10-30 09:33:07 -070080typedef struct _drm_intel_bufmgr_gem {
Eric Anholtd70d6052009-10-06 12:40:42 -070081 drm_intel_bufmgr bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -070082
Eric Anholtd70d6052009-10-06 12:40:42 -070083 int fd;
Eric Anholt6a9eb082008-06-03 09:27:37 -070084
Eric Anholtd70d6052009-10-06 12:40:42 -070085 int max_relocs;
Eric Anholt6a9eb082008-06-03 09:27:37 -070086
Eric Anholtd70d6052009-10-06 12:40:42 -070087 pthread_mutex_t lock;
Eric Anholt6df7b072008-06-12 23:22:26 -070088
Eric Anholtd70d6052009-10-06 12:40:42 -070089 struct drm_i915_gem_exec_object *exec_objects;
Jesse Barnesb5096402009-09-15 11:02:58 -070090 struct drm_i915_gem_exec_object2 *exec2_objects;
Eric Anholtd70d6052009-10-06 12:40:42 -070091 drm_intel_bo **exec_bos;
92 int exec_size;
93 int exec_count;
Eric Anholt6a9eb082008-06-03 09:27:37 -070094
Eric Anholtd70d6052009-10-06 12:40:42 -070095 /** Array of lists of cached gem objects of power-of-two sizes */
96 struct drm_intel_gem_bo_bucket cache_bucket[DRM_INTEL_GEM_BO_BUCKETS];
Eric Anholt6a9eb082008-06-03 09:27:37 -070097
Eric Anholtd70d6052009-10-06 12:40:42 -070098 uint64_t gtt_size;
99 int available_fences;
100 int pci_device;
Eric Anholta1f9ea72010-03-02 08:49:36 -0800101 int gen;
Eric Anholtd70d6052009-10-06 12:40:42 -0700102 char bo_reuse;
Jesse Barnesb5096402009-09-15 11:02:58 -0700103 char fenced_relocs;
Eric Anholt4b982642008-10-30 09:33:07 -0700104} drm_intel_bufmgr_gem;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700105
Jesse Barnesb5096402009-09-15 11:02:58 -0700106#define DRM_INTEL_RELOC_FENCE (1<<0)
107
108typedef struct _drm_intel_reloc_target_info {
109 drm_intel_bo *bo;
110 int flags;
111} drm_intel_reloc_target;
112
Eric Anholt4b982642008-10-30 09:33:07 -0700113struct _drm_intel_bo_gem {
Eric Anholtd70d6052009-10-06 12:40:42 -0700114 drm_intel_bo bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700115
Eric Anholtd70d6052009-10-06 12:40:42 -0700116 atomic_t refcount;
117 uint32_t gem_handle;
118 const char *name;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700119
Eric Anholtd70d6052009-10-06 12:40:42 -0700120 /**
121 * Kenel-assigned global name for this object
122 */
123 unsigned int global_name;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700124
Eric Anholtd70d6052009-10-06 12:40:42 -0700125 /**
126 * Index of the buffer within the validation list while preparing a
127 * batchbuffer execution.
128 */
129 int validate_index;
Keith Packard18f091d2008-12-15 15:08:12 -0800130
Eric Anholtd70d6052009-10-06 12:40:42 -0700131 /**
132 * Current tiling mode
133 */
134 uint32_t tiling_mode;
135 uint32_t swizzle_mode;
Eric Anholt3f3c5be2009-07-09 17:49:46 -0700136
Eric Anholtd70d6052009-10-06 12:40:42 -0700137 time_t free_time;
Keith Packard329e0862008-06-05 16:05:35 -0700138
Eric Anholtd70d6052009-10-06 12:40:42 -0700139 /** Array passed to the DRM containing relocation information. */
140 struct drm_i915_gem_relocation_entry *relocs;
Jesse Barnesb5096402009-09-15 11:02:58 -0700141 /**
142 * Array of info structs corresponding to relocs[i].target_handle etc
143 */
144 drm_intel_reloc_target *reloc_target_info;
Eric Anholtd70d6052009-10-06 12:40:42 -0700145 /** Number of entries in relocs */
146 int reloc_count;
147 /** Mapped address for the buffer, saved across map/unmap cycles */
148 void *mem_virtual;
149 /** GTT virtual address for the buffer, saved across map/unmap cycles */
150 void *gtt_virtual;
Eric Anholt0e867312008-10-21 00:10:54 -0700151
Eric Anholtd70d6052009-10-06 12:40:42 -0700152 /** BO cache list */
153 drmMMListHead head;
Eric Anholt0e867312008-10-21 00:10:54 -0700154
Eric Anholtd70d6052009-10-06 12:40:42 -0700155 /**
156 * Boolean of whether this BO and its children have been included in
157 * the current drm_intel_bufmgr_check_aperture_space() total.
158 */
159 char included_in_check_aperture;
Eric Anholt0e867312008-10-21 00:10:54 -0700160
Eric Anholtd70d6052009-10-06 12:40:42 -0700161 /**
162 * Boolean of whether this buffer has been used as a relocation
163 * target and had its size accounted for, and thus can't have any
164 * further relocations added to it.
165 */
166 char used_as_reloc_target;
Keith Packard5b5ce302009-05-11 13:42:12 -0700167
Eric Anholtd70d6052009-10-06 12:40:42 -0700168 /**
Chris Wilson792fed12009-12-02 13:12:39 +0000169 * Boolean of whether we have encountered an error whilst building the relocation tree.
170 */
171 char has_error;
172
173 /**
Eric Anholtd70d6052009-10-06 12:40:42 -0700174 * Boolean of whether this buffer can be re-used
175 */
176 char reusable;
177
178 /**
179 * Size in bytes of this buffer and its relocation descendents.
180 *
181 * Used to avoid costly tree walking in
182 * drm_intel_bufmgr_check_aperture in the common case.
183 */
184 int reloc_tree_size;
185
186 /**
187 * Number of potential fence registers required by this buffer and its
188 * relocations.
189 */
190 int reloc_tree_fences;
Keith Packarda919ff52008-06-05 15:58:09 -0700191};
Eric Anholt6a9eb082008-06-03 09:27:37 -0700192
Keith Packardb13f4e12008-11-21 01:49:39 -0800193static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -0700194drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count);
Keith Packardb13f4e12008-11-21 01:49:39 -0800195
196static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -0700197drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count);
Keith Packardb13f4e12008-11-21 01:49:39 -0800198
Eric Anholt6a9eb082008-06-03 09:27:37 -0700199static int
Eric Anholtd70d6052009-10-06 12:40:42 -0700200drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
201 uint32_t * swizzle_mode);
Keith Packard18f091d2008-12-15 15:08:12 -0800202
203static int
Eric Anholtd70d6052009-10-06 12:40:42 -0700204drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
Keith Packard18f091d2008-12-15 15:08:12 -0800205 uint32_t stride);
206
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700207static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
208 time_t time);
Chris Wilson04495ee2009-10-02 04:39:22 +0100209
Eric Anholtd70d6052009-10-06 12:40:42 -0700210static void drm_intel_gem_bo_unreference(drm_intel_bo *bo);
Keith Packard18f091d2008-12-15 15:08:12 -0800211
Eric Anholtd70d6052009-10-06 12:40:42 -0700212static void drm_intel_gem_bo_free(drm_intel_bo *bo);
Chris Wilson0fb215a2009-10-02 04:31:34 +0100213
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700214static unsigned long
215drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size,
216 uint32_t *tiling_mode)
217{
218 unsigned long min_size, max_size;
219 unsigned long i;
220
221 if (*tiling_mode == I915_TILING_NONE)
222 return size;
223
224 /* 965+ just need multiples of page size for tiling */
Eric Anholta1f9ea72010-03-02 08:49:36 -0800225 if (bufmgr_gem->gen >= 4)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700226 return ROUND_UP_TO(size, 4096);
227
228 /* Older chips need powers of two, of at least 512k or 1M */
Eric Anholtacbaff22010-03-02 15:24:50 -0800229 if (bufmgr_gem->gen == 3) {
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700230 min_size = 1024*1024;
231 max_size = 128*1024*1024;
232 } else {
233 min_size = 512*1024;
234 max_size = 64*1024*1024;
235 }
236
237 if (size > max_size) {
238 *tiling_mode = I915_TILING_NONE;
239 return size;
240 }
241
242 for (i = min_size; i < size; i <<= 1)
243 ;
244
245 return i;
246}
247
248/*
249 * Round a given pitch up to the minimum required for X tiling on a
250 * given chip. We use 512 as the minimum to allow for a later tiling
251 * change.
252 */
253static unsigned long
254drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem,
255 unsigned long pitch, uint32_t tiling_mode)
256{
Eric Anholt1d4d1e62010-03-04 16:09:40 -0800257 unsigned long tile_width;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700258 unsigned long i;
259
Eric Anholt7c697b12010-03-17 10:05:55 -0700260 /* If untiled, then just align it so that we can do rendering
261 * to it with the 3D engine.
262 */
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700263 if (tiling_mode == I915_TILING_NONE)
Eric Anholt7c697b12010-03-17 10:05:55 -0700264 return ALIGN(pitch, 64);
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700265
Eric Anholt1d4d1e62010-03-04 16:09:40 -0800266 if (tiling_mode == I915_TILING_X)
267 tile_width = 512;
268 else
269 tile_width = 128;
270
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700271 /* 965 is flexible */
Eric Anholta1f9ea72010-03-02 08:49:36 -0800272 if (bufmgr_gem->gen >= 4)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700273 return ROUND_UP_TO(pitch, tile_width);
274
275 /* Pre-965 needs power of two tile width */
276 for (i = tile_width; i < pitch; i <<= 1)
277 ;
278
279 return i;
280}
281
Eric Anholt4b982642008-10-30 09:33:07 -0700282static struct drm_intel_gem_bo_bucket *
283drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem,
284 unsigned long size)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700285{
Eric Anholtd70d6052009-10-06 12:40:42 -0700286 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700287
Eric Anholtd70d6052009-10-06 12:40:42 -0700288 for (i = 0; i < DRM_INTEL_GEM_BO_BUCKETS; i++) {
289 struct drm_intel_gem_bo_bucket *bucket =
290 &bufmgr_gem->cache_bucket[i];
291 if (bucket->size >= size) {
292 return bucket;
293 }
Eric Anholt78fa5902009-07-06 11:55:28 -0700294 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700295
Eric Anholtd70d6052009-10-06 12:40:42 -0700296 return NULL;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700297}
298
Eric Anholtd70d6052009-10-06 12:40:42 -0700299static void
300drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700301{
Eric Anholtd70d6052009-10-06 12:40:42 -0700302 int i, j;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700303
Eric Anholtd70d6052009-10-06 12:40:42 -0700304 for (i = 0; i < bufmgr_gem->exec_count; i++) {
305 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
306 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700307
Eric Anholtd70d6052009-10-06 12:40:42 -0700308 if (bo_gem->relocs == NULL) {
309 DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle,
310 bo_gem->name);
311 continue;
312 }
313
314 for (j = 0; j < bo_gem->reloc_count; j++) {
Jesse Barnesb5096402009-09-15 11:02:58 -0700315 drm_intel_bo *target_bo = bo_gem->reloc_target_info[j].bo;
Eric Anholtd70d6052009-10-06 12:40:42 -0700316 drm_intel_bo_gem *target_gem =
317 (drm_intel_bo_gem *) target_bo;
318
319 DBG("%2d: %d (%s)@0x%08llx -> "
320 "%d (%s)@0x%08lx + 0x%08x\n",
321 i,
322 bo_gem->gem_handle, bo_gem->name,
323 (unsigned long long)bo_gem->relocs[j].offset,
324 target_gem->gem_handle,
325 target_gem->name,
326 target_bo->offset,
327 bo_gem->relocs[j].delta);
328 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700329 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700330}
331
Chris Wilson9fec2a82009-12-02 10:42:51 +0000332static inline void
Chris Wilson04495ee2009-10-02 04:39:22 +0100333drm_intel_gem_bo_reference(drm_intel_bo *bo)
334{
Eric Anholtd70d6052009-10-06 12:40:42 -0700335 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Chris Wilson04495ee2009-10-02 04:39:22 +0100336
Eric Anholtd70d6052009-10-06 12:40:42 -0700337 assert(atomic_read(&bo_gem->refcount) > 0);
338 atomic_inc(&bo_gem->refcount);
Chris Wilson04495ee2009-10-02 04:39:22 +0100339}
340
Eric Anholt6a9eb082008-06-03 09:27:37 -0700341/**
342 * Adds the given buffer to the list of buffers to be validated (moved into the
343 * appropriate memory type) with the next batch submission.
344 *
345 * If a buffer is validated multiple times in a batch submission, it ends up
346 * with the intersection of the memory type flags and the union of the
347 * access flags.
348 */
349static void
Eric Anholt4b982642008-10-30 09:33:07 -0700350drm_intel_add_validate_buffer(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700351{
Eric Anholtd70d6052009-10-06 12:40:42 -0700352 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
353 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
354 int index;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700355
Eric Anholtd70d6052009-10-06 12:40:42 -0700356 if (bo_gem->validate_index != -1)
357 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700358
Eric Anholtd70d6052009-10-06 12:40:42 -0700359 /* Extend the array of validation entries as necessary. */
360 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
361 int new_size = bufmgr_gem->exec_size * 2;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700362
Eric Anholtd70d6052009-10-06 12:40:42 -0700363 if (new_size == 0)
364 new_size = 5;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700365
Eric Anholtd70d6052009-10-06 12:40:42 -0700366 bufmgr_gem->exec_objects =
367 realloc(bufmgr_gem->exec_objects,
368 sizeof(*bufmgr_gem->exec_objects) * new_size);
369 bufmgr_gem->exec_bos =
370 realloc(bufmgr_gem->exec_bos,
371 sizeof(*bufmgr_gem->exec_bos) * new_size);
372 bufmgr_gem->exec_size = new_size;
373 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700374
Eric Anholtd70d6052009-10-06 12:40:42 -0700375 index = bufmgr_gem->exec_count;
376 bo_gem->validate_index = index;
377 /* Fill in array entry */
378 bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
379 bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
380 bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs;
381 bufmgr_gem->exec_objects[index].alignment = 0;
382 bufmgr_gem->exec_objects[index].offset = 0;
383 bufmgr_gem->exec_bos[index] = bo;
Eric Anholtd70d6052009-10-06 12:40:42 -0700384 bufmgr_gem->exec_count++;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700385}
386
Jesse Barnesb5096402009-09-15 11:02:58 -0700387static void
388drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
389{
390 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
391 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
392 int index;
393
Eric Anholt47102862010-03-03 10:07:27 -0800394 if (bo_gem->validate_index != -1) {
395 if (need_fence)
396 bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |=
397 EXEC_OBJECT_NEEDS_FENCE;
Jesse Barnesb5096402009-09-15 11:02:58 -0700398 return;
Eric Anholt47102862010-03-03 10:07:27 -0800399 }
Jesse Barnesb5096402009-09-15 11:02:58 -0700400
401 /* Extend the array of validation entries as necessary. */
402 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
403 int new_size = bufmgr_gem->exec_size * 2;
404
405 if (new_size == 0)
406 new_size = 5;
407
408 bufmgr_gem->exec2_objects =
409 realloc(bufmgr_gem->exec2_objects,
410 sizeof(*bufmgr_gem->exec2_objects) * new_size);
411 bufmgr_gem->exec_bos =
412 realloc(bufmgr_gem->exec_bos,
413 sizeof(*bufmgr_gem->exec_bos) * new_size);
414 bufmgr_gem->exec_size = new_size;
415 }
416
417 index = bufmgr_gem->exec_count;
418 bo_gem->validate_index = index;
419 /* Fill in array entry */
420 bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
421 bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
422 bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
423 bufmgr_gem->exec2_objects[index].alignment = 0;
424 bufmgr_gem->exec2_objects[index].offset = 0;
425 bufmgr_gem->exec_bos[index] = bo;
426 bufmgr_gem->exec2_objects[index].flags = 0;
427 bufmgr_gem->exec2_objects[index].rsvd1 = 0;
428 bufmgr_gem->exec2_objects[index].rsvd2 = 0;
429 if (need_fence) {
430 bufmgr_gem->exec2_objects[index].flags |=
431 EXEC_OBJECT_NEEDS_FENCE;
432 }
433 bufmgr_gem->exec_count++;
434}
435
Eric Anholt6a9eb082008-06-03 09:27:37 -0700436#define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
437 sizeof(uint32_t))
438
Chris Wilsone22fb792009-11-30 22:14:30 +0000439static void
440drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem,
441 drm_intel_bo_gem *bo_gem)
442{
443 int size;
444
445 assert(!bo_gem->used_as_reloc_target);
446
447 /* The older chipsets are far-less flexible in terms of tiling,
448 * and require tiled buffer to be size aligned in the aperture.
449 * This means that in the worst possible case we will need a hole
450 * twice as large as the object in order for it to fit into the
451 * aperture. Optimal packing is for wimps.
452 */
453 size = bo_gem->bo.size;
Eric Anholta1f9ea72010-03-02 08:49:36 -0800454 if (bufmgr_gem->gen < 4 && bo_gem->tiling_mode != I915_TILING_NONE)
Chris Wilsone22fb792009-11-30 22:14:30 +0000455 size *= 2;
456
457 bo_gem->reloc_tree_size = size;
458}
459
Eric Anholt6a9eb082008-06-03 09:27:37 -0700460static int
Eric Anholt4b982642008-10-30 09:33:07 -0700461drm_intel_setup_reloc_list(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700462{
Eric Anholtd70d6052009-10-06 12:40:42 -0700463 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
464 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
465 unsigned int max_relocs = bufmgr_gem->max_relocs;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700466
Eric Anholtd70d6052009-10-06 12:40:42 -0700467 if (bo->size / 4 < max_relocs)
468 max_relocs = bo->size / 4;
Eric Anholt3c9bd062009-10-05 16:35:32 -0700469
Eric Anholtd70d6052009-10-06 12:40:42 -0700470 bo_gem->relocs = malloc(max_relocs *
471 sizeof(struct drm_i915_gem_relocation_entry));
Jesse Barnesb5096402009-09-15 11:02:58 -0700472 bo_gem->reloc_target_info = malloc(max_relocs *
Chris Wilson35061732010-04-11 18:40:38 +0100473 sizeof(drm_intel_reloc_target));
Jesse Barnesb5096402009-09-15 11:02:58 -0700474 if (bo_gem->relocs == NULL || bo_gem->reloc_target_info == NULL) {
Chris Wilson792fed12009-12-02 13:12:39 +0000475 bo_gem->has_error = 1;
476
477 free (bo_gem->relocs);
478 bo_gem->relocs = NULL;
479
Jesse Barnesb5096402009-09-15 11:02:58 -0700480 free (bo_gem->reloc_target_info);
481 bo_gem->reloc_target_info = NULL;
Chris Wilson792fed12009-12-02 13:12:39 +0000482
483 return 1;
484 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700485
Eric Anholtd70d6052009-10-06 12:40:42 -0700486 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700487}
488
Eric Anholt8214a652009-08-27 18:32:07 -0700489static int
490drm_intel_gem_bo_busy(drm_intel_bo *bo)
491{
Eric Anholtd70d6052009-10-06 12:40:42 -0700492 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
493 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
494 struct drm_i915_gem_busy busy;
495 int ret;
Eric Anholt8214a652009-08-27 18:32:07 -0700496
Eric Anholtd70d6052009-10-06 12:40:42 -0700497 memset(&busy, 0, sizeof(busy));
498 busy.handle = bo_gem->gem_handle;
Eric Anholt8214a652009-08-27 18:32:07 -0700499
Chris Wilson8ffd2e12009-12-01 13:08:04 +0000500 do {
501 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
502 } while (ret == -1 && errno == EINTR);
Eric Anholt8214a652009-08-27 18:32:07 -0700503
Eric Anholtd70d6052009-10-06 12:40:42 -0700504 return (ret == 0 && busy.busy);
Eric Anholt8214a652009-08-27 18:32:07 -0700505}
506
Chris Wilson0fb215a2009-10-02 04:31:34 +0100507static int
Chris Wilson83a35b62009-11-11 13:04:38 +0000508drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem,
509 drm_intel_bo_gem *bo_gem, int state)
Chris Wilson0fb215a2009-10-02 04:31:34 +0100510{
Eric Anholtd70d6052009-10-06 12:40:42 -0700511 struct drm_i915_gem_madvise madv;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100512
Eric Anholtd70d6052009-10-06 12:40:42 -0700513 madv.handle = bo_gem->gem_handle;
514 madv.madv = state;
515 madv.retained = 1;
516 ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
Chris Wilson0fb215a2009-10-02 04:31:34 +0100517
Eric Anholtd70d6052009-10-06 12:40:42 -0700518 return madv.retained;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100519}
520
Chris Wilson83a35b62009-11-11 13:04:38 +0000521static int
522drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv)
523{
524 return drm_intel_gem_bo_madvise_internal
525 ((drm_intel_bufmgr_gem *) bo->bufmgr,
526 (drm_intel_bo_gem *) bo,
527 madv);
528}
529
Chris Wilson0fb215a2009-10-02 04:31:34 +0100530/* drop the oldest entries that have been purged by the kernel */
531static void
532drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem,
533 struct drm_intel_gem_bo_bucket *bucket)
534{
Eric Anholtd70d6052009-10-06 12:40:42 -0700535 while (!DRMLISTEMPTY(&bucket->head)) {
536 drm_intel_bo_gem *bo_gem;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100537
Eric Anholtd70d6052009-10-06 12:40:42 -0700538 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
539 bucket->head.next, head);
Chris Wilson83a35b62009-11-11 13:04:38 +0000540 if (drm_intel_gem_bo_madvise_internal
Eric Anholtd70d6052009-10-06 12:40:42 -0700541 (bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
542 break;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100543
Eric Anholtd70d6052009-10-06 12:40:42 -0700544 DRMLISTDEL(&bo_gem->head);
545 drm_intel_gem_bo_free(&bo_gem->bo);
546 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100547}
548
Eric Anholt4b982642008-10-30 09:33:07 -0700549static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700550drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr,
551 const char *name,
552 unsigned long size,
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700553 unsigned long flags)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700554{
Eric Anholtd70d6052009-10-06 12:40:42 -0700555 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
556 drm_intel_bo_gem *bo_gem;
557 unsigned int page_size = getpagesize();
558 int ret;
559 struct drm_intel_gem_bo_bucket *bucket;
560 int alloc_from_cache;
561 unsigned long bo_size;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700562 int for_render = 0;
563
564 if (flags & BO_ALLOC_FOR_RENDER)
565 for_render = 1;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700566
Eric Anholtd70d6052009-10-06 12:40:42 -0700567 /* Round the allocated size up to a power of two number of pages. */
568 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700569
Eric Anholtd70d6052009-10-06 12:40:42 -0700570 /* If we don't have caching at this size, don't actually round the
571 * allocation up.
572 */
573 if (bucket == NULL) {
574 bo_size = size;
575 if (bo_size < page_size)
576 bo_size = page_size;
Eric Anholt72abe982009-02-18 13:06:35 -0800577 } else {
Eric Anholtd70d6052009-10-06 12:40:42 -0700578 bo_size = bucket->size;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700579 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100580
Eric Anholtd70d6052009-10-06 12:40:42 -0700581 pthread_mutex_lock(&bufmgr_gem->lock);
582 /* Get a buffer out of the cache if available */
583retry:
584 alloc_from_cache = 0;
585 if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) {
586 if (for_render) {
587 /* Allocate new render-target BOs from the tail (MRU)
588 * of the list, as it will likely be hot in the GPU
589 * cache and in the aperture for us.
590 */
591 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
592 bucket->head.prev, head);
593 DRMLISTDEL(&bo_gem->head);
594 alloc_from_cache = 1;
595 } else {
596 /* For non-render-target BOs (where we're probably
597 * going to map it first thing in order to fill it
598 * with data), check if the last BO in the cache is
599 * unbusy, and only reuse in that case. Otherwise,
600 * allocating a new buffer is probably faster than
601 * waiting for the GPU to finish.
602 */
603 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
604 bucket->head.next, head);
605 if (!drm_intel_gem_bo_busy(&bo_gem->bo)) {
606 alloc_from_cache = 1;
607 DRMLISTDEL(&bo_gem->head);
608 }
609 }
610
611 if (alloc_from_cache) {
Chris Wilson83a35b62009-11-11 13:04:38 +0000612 if (!drm_intel_gem_bo_madvise_internal
Eric Anholtd70d6052009-10-06 12:40:42 -0700613 (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) {
614 drm_intel_gem_bo_free(&bo_gem->bo);
615 drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem,
616 bucket);
617 goto retry;
618 }
619 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100620 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700621 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700622
Eric Anholtd70d6052009-10-06 12:40:42 -0700623 if (!alloc_from_cache) {
624 struct drm_i915_gem_create create;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700625
Eric Anholtd70d6052009-10-06 12:40:42 -0700626 bo_gem = calloc(1, sizeof(*bo_gem));
627 if (!bo_gem)
628 return NULL;
Keith Packarda919ff52008-06-05 15:58:09 -0700629
Eric Anholtd70d6052009-10-06 12:40:42 -0700630 bo_gem->bo.size = bo_size;
631 memset(&create, 0, sizeof(create));
632 create.size = bo_size;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700633
Chris Wilson8ffd2e12009-12-01 13:08:04 +0000634 do {
635 ret = ioctl(bufmgr_gem->fd,
636 DRM_IOCTL_I915_GEM_CREATE,
637 &create);
638 } while (ret == -1 && errno == EINTR);
Eric Anholtd70d6052009-10-06 12:40:42 -0700639 bo_gem->gem_handle = create.handle;
640 bo_gem->bo.handle = bo_gem->gem_handle;
641 if (ret != 0) {
642 free(bo_gem);
643 return NULL;
644 }
645 bo_gem->bo.bufmgr = bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700646 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700647
Eric Anholtd70d6052009-10-06 12:40:42 -0700648 bo_gem->name = name;
649 atomic_set(&bo_gem->refcount, 1);
650 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -0700651 bo_gem->reloc_tree_fences = 0;
652 bo_gem->used_as_reloc_target = 0;
Chris Wilson792fed12009-12-02 13:12:39 +0000653 bo_gem->has_error = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -0700654 bo_gem->tiling_mode = I915_TILING_NONE;
655 bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
656 bo_gem->reusable = 1;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700657
Chris Wilsone22fb792009-11-30 22:14:30 +0000658 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
659
Eric Anholtd70d6052009-10-06 12:40:42 -0700660 DBG("bo_create: buf %d (%s) %ldb\n",
661 bo_gem->gem_handle, bo_gem->name, size);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700662
Eric Anholtd70d6052009-10-06 12:40:42 -0700663 return &bo_gem->bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700664}
665
Eric Anholt72abe982009-02-18 13:06:35 -0800666static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700667drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
668 const char *name,
669 unsigned long size,
670 unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -0800671{
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700672 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size,
673 BO_ALLOC_FOR_RENDER);
Eric Anholt72abe982009-02-18 13:06:35 -0800674}
675
676static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700677drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr,
678 const char *name,
679 unsigned long size,
680 unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -0800681{
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700682 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0);
683}
684
685static drm_intel_bo *
686drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
687 int x, int y, int cpp, uint32_t *tiling_mode,
688 unsigned long *pitch, unsigned long flags)
689{
690 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
691 drm_intel_bo *bo;
692 unsigned long size, stride, aligned_y = y;
693 int ret;
694
Eric Anholta0abb1b2010-03-02 15:05:30 -0800695 /* If we're tiled, our allocations are in 8 or 32-row blocks,
696 * so failure to align our height means that we won't allocate
697 * enough pages.
698 *
699 * If we're untiled, we still have to align to 2 rows high
700 * because the data port accesses 2x2 blocks even if the
701 * bottom row isn't to be rendered, so failure to align means
702 * we could walk off the end of the GTT and fault. This is
703 * documented on 965, and may be the case on older chipsets
704 * too so we try to be careful.
705 */
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700706 if (*tiling_mode == I915_TILING_NONE)
707 aligned_y = ALIGN(y, 2);
708 else if (*tiling_mode == I915_TILING_X)
709 aligned_y = ALIGN(y, 8);
710 else if (*tiling_mode == I915_TILING_Y)
711 aligned_y = ALIGN(y, 32);
712
713 stride = x * cpp;
714 stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, *tiling_mode);
715 size = stride * aligned_y;
716 size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode);
717
718 bo = drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags);
719 if (!bo)
720 return NULL;
721
722 ret = drm_intel_gem_bo_set_tiling(bo, tiling_mode, stride);
723 if (ret != 0) {
724 drm_intel_gem_bo_unreference(bo);
725 return NULL;
726 }
727
728 *pitch = stride;
729
730 return bo;
Eric Anholt72abe982009-02-18 13:06:35 -0800731}
732
Eric Anholt6a9eb082008-06-03 09:27:37 -0700733/**
Eric Anholt4b982642008-10-30 09:33:07 -0700734 * Returns a drm_intel_bo wrapping the given buffer object handle.
Eric Anholt6a9eb082008-06-03 09:27:37 -0700735 *
736 * This can be used when one application needs to pass a buffer object
737 * to another.
738 */
Eric Anholt4b982642008-10-30 09:33:07 -0700739drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700740drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
741 const char *name,
Eric Anholt4b982642008-10-30 09:33:07 -0700742 unsigned int handle)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700743{
Eric Anholtd70d6052009-10-06 12:40:42 -0700744 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
745 drm_intel_bo_gem *bo_gem;
746 int ret;
747 struct drm_gem_open open_arg;
748 struct drm_i915_gem_get_tiling get_tiling;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700749
Eric Anholtd70d6052009-10-06 12:40:42 -0700750 bo_gem = calloc(1, sizeof(*bo_gem));
751 if (!bo_gem)
752 return NULL;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700753
Eric Anholtd70d6052009-10-06 12:40:42 -0700754 memset(&open_arg, 0, sizeof(open_arg));
755 open_arg.name = handle;
Chris Wilson8ffd2e12009-12-01 13:08:04 +0000756 do {
757 ret = ioctl(bufmgr_gem->fd,
758 DRM_IOCTL_GEM_OPEN,
759 &open_arg);
760 } while (ret == -1 && errno == EINTR);
Eric Anholtd70d6052009-10-06 12:40:42 -0700761 if (ret != 0) {
762 fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
763 name, handle, strerror(errno));
764 free(bo_gem);
765 return NULL;
766 }
767 bo_gem->bo.size = open_arg.size;
768 bo_gem->bo.offset = 0;
769 bo_gem->bo.virtual = NULL;
770 bo_gem->bo.bufmgr = bufmgr;
771 bo_gem->name = name;
772 atomic_set(&bo_gem->refcount, 1);
773 bo_gem->validate_index = -1;
774 bo_gem->gem_handle = open_arg.handle;
775 bo_gem->global_name = handle;
776 bo_gem->reusable = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700777
Eric Anholtd70d6052009-10-06 12:40:42 -0700778 memset(&get_tiling, 0, sizeof(get_tiling));
779 get_tiling.handle = bo_gem->gem_handle;
780 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
781 if (ret != 0) {
782 drm_intel_gem_bo_unreference(&bo_gem->bo);
783 return NULL;
784 }
785 bo_gem->tiling_mode = get_tiling.tiling_mode;
786 bo_gem->swizzle_mode = get_tiling.swizzle_mode;
Chris Wilsone22fb792009-11-30 22:14:30 +0000787 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
Keith Packard18f091d2008-12-15 15:08:12 -0800788
Eric Anholtd70d6052009-10-06 12:40:42 -0700789 DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700790
Eric Anholtd70d6052009-10-06 12:40:42 -0700791 return &bo_gem->bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700792}
793
794static void
Eric Anholt4b982642008-10-30 09:33:07 -0700795drm_intel_gem_bo_free(drm_intel_bo *bo)
Eric Anholt500c81d2008-06-06 17:13:16 -0700796{
Eric Anholtd70d6052009-10-06 12:40:42 -0700797 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
798 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
799 struct drm_gem_close close;
800 int ret;
Eric Anholt500c81d2008-06-06 17:13:16 -0700801
Eric Anholtd70d6052009-10-06 12:40:42 -0700802 if (bo_gem->mem_virtual)
803 munmap(bo_gem->mem_virtual, bo_gem->bo.size);
804 if (bo_gem->gtt_virtual)
805 munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
Eric Anholt500c81d2008-06-06 17:13:16 -0700806
Eric Anholtd70d6052009-10-06 12:40:42 -0700807 /* Close this object */
808 memset(&close, 0, sizeof(close));
809 close.handle = bo_gem->gem_handle;
810 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
811 if (ret != 0) {
812 fprintf(stderr,
813 "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
814 bo_gem->gem_handle, bo_gem->name, strerror(errno));
815 }
816 free(bo);
Eric Anholt500c81d2008-06-06 17:13:16 -0700817}
818
Eric Anholt3f3c5be2009-07-09 17:49:46 -0700819/** Frees all cached buffers significantly older than @time. */
820static void
821drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time)
822{
Chris Wilson04495ee2009-10-02 04:39:22 +0100823 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700824
Eric Anholtd70d6052009-10-06 12:40:42 -0700825 for (i = 0; i < DRM_INTEL_GEM_BO_BUCKETS; i++) {
826 struct drm_intel_gem_bo_bucket *bucket =
827 &bufmgr_gem->cache_bucket[i];
Chris Wilson04495ee2009-10-02 04:39:22 +0100828
Eric Anholtd70d6052009-10-06 12:40:42 -0700829 while (!DRMLISTEMPTY(&bucket->head)) {
830 drm_intel_bo_gem *bo_gem;
Chris Wilson04495ee2009-10-02 04:39:22 +0100831
Eric Anholtd70d6052009-10-06 12:40:42 -0700832 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
833 bucket->head.next, head);
834 if (time - bo_gem->free_time <= 1)
835 break;
Chris Wilson04495ee2009-10-02 04:39:22 +0100836
Eric Anholtd70d6052009-10-06 12:40:42 -0700837 DRMLISTDEL(&bo_gem->head);
Chris Wilson04495ee2009-10-02 04:39:22 +0100838
Eric Anholtd70d6052009-10-06 12:40:42 -0700839 drm_intel_gem_bo_free(&bo_gem->bo);
840 }
841 }
Chris Wilson04495ee2009-10-02 04:39:22 +0100842}
843
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700844static void
845drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time)
Chris Wilson04495ee2009-10-02 04:39:22 +0100846{
Eric Anholtd70d6052009-10-06 12:40:42 -0700847 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
848 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
849 struct drm_intel_gem_bo_bucket *bucket;
850 uint32_t tiling_mode;
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700851 int i;
Chris Wilson04495ee2009-10-02 04:39:22 +0100852
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700853 /* Unreference all the target buffers */
854 for (i = 0; i < bo_gem->reloc_count; i++) {
855 drm_intel_gem_bo_unreference_locked_timed(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -0700856 reloc_target_info[i].bo,
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700857 time);
Eric Anholtd70d6052009-10-06 12:40:42 -0700858 }
Chris Wilsonb666f412009-11-30 23:07:19 +0000859 bo_gem->reloc_count = 0;
860 bo_gem->used_as_reloc_target = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -0700861
862 DBG("bo_unreference final: %d (%s)\n",
863 bo_gem->gem_handle, bo_gem->name);
864
Chris Wilson57473c72009-12-02 13:36:22 +0000865 /* release memory associated with this object */
Jesse Barnesb5096402009-09-15 11:02:58 -0700866 if (bo_gem->reloc_target_info) {
867 free(bo_gem->reloc_target_info);
868 bo_gem->reloc_target_info = NULL;
Chris Wilson57473c72009-12-02 13:36:22 +0000869 }
870 if (bo_gem->relocs) {
871 free(bo_gem->relocs);
872 bo_gem->relocs = NULL;
873 }
874
Eric Anholtd70d6052009-10-06 12:40:42 -0700875 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
876 /* Put the buffer into our internal cache for reuse if we can. */
877 tiling_mode = I915_TILING_NONE;
878 if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL &&
Chris Wilson60aa8032009-11-30 20:02:05 +0000879 drm_intel_gem_bo_set_tiling(bo, &tiling_mode, 0) == 0 &&
880 drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem,
881 I915_MADV_DONTNEED)) {
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700882 bo_gem->free_time = time;
Eric Anholtd70d6052009-10-06 12:40:42 -0700883
884 bo_gem->name = NULL;
885 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -0700886
887 DRMLISTADDTAIL(&bo_gem->head, &bucket->head);
888
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700889 drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time);
Eric Anholtd70d6052009-10-06 12:40:42 -0700890 } else {
891 drm_intel_gem_bo_free(bo);
892 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700893}
894
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700895static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
896 time_t time)
897{
898 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
899
900 assert(atomic_read(&bo_gem->refcount) > 0);
Eric Anholtd70d6052009-10-06 12:40:42 -0700901 if (atomic_dec_and_test(&bo_gem->refcount))
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700902 drm_intel_gem_bo_unreference_final(bo, time);
Eric Anholtd70d6052009-10-06 12:40:42 -0700903}
904
905static void drm_intel_gem_bo_unreference(drm_intel_bo *bo)
906{
907 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
908
909 assert(atomic_read(&bo_gem->refcount) > 0);
910 if (atomic_dec_and_test(&bo_gem->refcount)) {
911 drm_intel_bufmgr_gem *bufmgr_gem =
912 (drm_intel_bufmgr_gem *) bo->bufmgr;
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700913 struct timespec time;
914
915 clock_gettime(CLOCK_MONOTONIC, &time);
916
Eric Anholtd70d6052009-10-06 12:40:42 -0700917 pthread_mutex_lock(&bufmgr_gem->lock);
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700918 drm_intel_gem_bo_unreference_final(bo, time.tv_sec);
Eric Anholtd70d6052009-10-06 12:40:42 -0700919 pthread_mutex_unlock(&bufmgr_gem->lock);
920 }
921}
922
923static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
924{
925 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
926 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
927 struct drm_i915_gem_set_domain set_domain;
928 int ret;
929
Chris Wilsona3305b02010-05-13 08:24:28 +0100930 pthread_mutex_lock(&bufmgr_gem->lock);
931
Eric Anholtd70d6052009-10-06 12:40:42 -0700932 /* Allow recursive mapping. Mesa may recursively map buffers with
933 * nested display loops.
Carl Worthafd245d2009-04-29 14:43:55 -0700934 */
Eric Anholtd70d6052009-10-06 12:40:42 -0700935 if (!bo_gem->mem_virtual) {
936 struct drm_i915_gem_mmap mmap_arg;
Carl Worthafd245d2009-04-29 14:43:55 -0700937
Eric Anholtd70d6052009-10-06 12:40:42 -0700938 DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name);
939
940 memset(&mmap_arg, 0, sizeof(mmap_arg));
941 mmap_arg.handle = bo_gem->gem_handle;
942 mmap_arg.offset = 0;
943 mmap_arg.size = bo->size;
Chris Wilson8ffd2e12009-12-01 13:08:04 +0000944 do {
945 ret = ioctl(bufmgr_gem->fd,
946 DRM_IOCTL_I915_GEM_MMAP,
947 &mmap_arg);
948 } while (ret == -1 && errno == EINTR);
Eric Anholtd70d6052009-10-06 12:40:42 -0700949 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +0000950 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -0700951 fprintf(stderr,
952 "%s:%d: Error mapping buffer %d (%s): %s .\n",
953 __FILE__, __LINE__, bo_gem->gem_handle,
954 bo_gem->name, strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +0100955 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -0700956 return ret;
957 }
958 bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
959 }
960 DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
961 bo_gem->mem_virtual);
962 bo->virtual = bo_gem->mem_virtual;
963
964 set_domain.handle = bo_gem->gem_handle;
965 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
966 if (write_enable)
967 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
968 else
969 set_domain.write_domain = 0;
970 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +0000971 ret = ioctl(bufmgr_gem->fd,
972 DRM_IOCTL_I915_GEM_SET_DOMAIN,
Eric Anholtd70d6052009-10-06 12:40:42 -0700973 &set_domain);
974 } while (ret == -1 && errno == EINTR);
975 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +0000976 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -0700977 fprintf(stderr, "%s:%d: Error setting to CPU domain %d: %s\n",
978 __FILE__, __LINE__, bo_gem->gem_handle,
979 strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +0100980 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -0700981 return ret;
982 }
983
Chris Wilsona3305b02010-05-13 08:24:28 +0100984 pthread_mutex_unlock(&bufmgr_gem->lock);
985
Eric Anholtd70d6052009-10-06 12:40:42 -0700986 return 0;
987}
988
989int drm_intel_gem_bo_map_gtt(drm_intel_bo *bo)
990{
991 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
992 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
993 struct drm_i915_gem_set_domain set_domain;
994 int ret;
995
Chris Wilsona3305b02010-05-13 08:24:28 +0100996 pthread_mutex_lock(&bufmgr_gem->lock);
997
Eric Anholtd70d6052009-10-06 12:40:42 -0700998 /* Get a mapping of the buffer if we haven't before. */
999 if (bo_gem->gtt_virtual == NULL) {
1000 struct drm_i915_gem_mmap_gtt mmap_arg;
1001
1002 DBG("bo_map_gtt: mmap %d (%s)\n", bo_gem->gem_handle,
1003 bo_gem->name);
1004
1005 memset(&mmap_arg, 0, sizeof(mmap_arg));
1006 mmap_arg.handle = bo_gem->gem_handle;
1007
1008 /* Get the fake offset back... */
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001009 do {
1010 ret = ioctl(bufmgr_gem->fd,
1011 DRM_IOCTL_I915_GEM_MMAP_GTT,
1012 &mmap_arg);
1013 } while (ret == -1 && errno == EINTR);
Eric Anholtd70d6052009-10-06 12:40:42 -07001014 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001015 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -07001016 fprintf(stderr,
1017 "%s:%d: Error preparing buffer map %d (%s): %s .\n",
1018 __FILE__, __LINE__,
1019 bo_gem->gem_handle, bo_gem->name,
1020 strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +01001021 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001022 return ret;
1023 }
1024
1025 /* and mmap it */
1026 bo_gem->gtt_virtual = mmap(0, bo->size, PROT_READ | PROT_WRITE,
1027 MAP_SHARED, bufmgr_gem->fd,
1028 mmap_arg.offset);
1029 if (bo_gem->gtt_virtual == MAP_FAILED) {
Chris Wilson08371bc2009-12-08 22:35:24 +00001030 bo_gem->gtt_virtual = NULL;
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001031 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -07001032 fprintf(stderr,
1033 "%s:%d: Error mapping buffer %d (%s): %s .\n",
1034 __FILE__, __LINE__,
1035 bo_gem->gem_handle, bo_gem->name,
1036 strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +01001037 pthread_mutex_unlock(&bufmgr_gem->lock);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001038 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001039 }
1040 }
1041
1042 bo->virtual = bo_gem->gtt_virtual;
1043
1044 DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1045 bo_gem->gtt_virtual);
1046
1047 /* Now move it to the GTT domain so that the CPU caches are flushed */
1048 set_domain.handle = bo_gem->gem_handle;
1049 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1050 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
1051 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001052 ret = ioctl(bufmgr_gem->fd,
1053 DRM_IOCTL_I915_GEM_SET_DOMAIN,
Eric Anholtd70d6052009-10-06 12:40:42 -07001054 &set_domain);
1055 } while (ret == -1 && errno == EINTR);
1056
1057 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001058 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -07001059 fprintf(stderr, "%s:%d: Error setting domain %d: %s\n",
1060 __FILE__, __LINE__, bo_gem->gem_handle,
1061 strerror(errno));
1062 }
1063
Chris Wilsona3305b02010-05-13 08:24:28 +01001064 pthread_mutex_unlock(&bufmgr_gem->lock);
1065
Chris Wilson60aa8032009-11-30 20:02:05 +00001066 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001067}
1068
1069int drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo)
1070{
1071 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1072 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1073 int ret = 0;
1074
1075 if (bo == NULL)
1076 return 0;
1077
1078 assert(bo_gem->gtt_virtual != NULL);
1079
Chris Wilsona3305b02010-05-13 08:24:28 +01001080 pthread_mutex_lock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001081 bo->virtual = NULL;
Chris Wilsona3305b02010-05-13 08:24:28 +01001082 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001083
1084 return ret;
1085}
1086
1087static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
1088{
1089 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1090 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1091 struct drm_i915_gem_sw_finish sw_finish;
1092 int ret;
1093
1094 if (bo == NULL)
1095 return 0;
1096
1097 assert(bo_gem->mem_virtual != NULL);
1098
Chris Wilsona3305b02010-05-13 08:24:28 +01001099 pthread_mutex_lock(&bufmgr_gem->lock);
1100
Eric Anholtd70d6052009-10-06 12:40:42 -07001101 /* Cause a flush to happen if the buffer's pinned for scanout, so the
1102 * results show up in a timely manner.
1103 */
1104 sw_finish.handle = bo_gem->gem_handle;
1105 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001106 ret = ioctl(bufmgr_gem->fd,
1107 DRM_IOCTL_I915_GEM_SW_FINISH,
Eric Anholtd70d6052009-10-06 12:40:42 -07001108 &sw_finish);
1109 } while (ret == -1 && errno == EINTR);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001110 ret = ret == -1 ? -errno : 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07001111
1112 bo->virtual = NULL;
Chris Wilsona3305b02010-05-13 08:24:28 +01001113 pthread_mutex_unlock(&bufmgr_gem->lock);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001114
1115 return ret;
Carl Worthafd245d2009-04-29 14:43:55 -07001116}
1117
Eric Anholt6a9eb082008-06-03 09:27:37 -07001118static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001119drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
1120 unsigned long size, const void *data)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001121{
Eric Anholtd70d6052009-10-06 12:40:42 -07001122 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1123 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1124 struct drm_i915_gem_pwrite pwrite;
1125 int ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001126
Eric Anholtd70d6052009-10-06 12:40:42 -07001127 memset(&pwrite, 0, sizeof(pwrite));
1128 pwrite.handle = bo_gem->gem_handle;
1129 pwrite.offset = offset;
1130 pwrite.size = size;
1131 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
1132 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001133 ret = ioctl(bufmgr_gem->fd,
1134 DRM_IOCTL_I915_GEM_PWRITE,
1135 &pwrite);
Eric Anholtd70d6052009-10-06 12:40:42 -07001136 } while (ret == -1 && errno == EINTR);
1137 if (ret != 0) {
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001138 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -07001139 fprintf(stderr,
1140 "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
1141 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1142 (int)size, strerror(errno));
1143 }
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001144
1145 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001146}
1147
1148static int
1149drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
1150{
1151 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1152 struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id;
1153 int ret;
1154
1155 get_pipe_from_crtc_id.crtc_id = crtc_id;
1156 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
1157 &get_pipe_from_crtc_id);
1158 if (ret != 0) {
1159 /* We return -1 here to signal that we don't
1160 * know which pipe is associated with this crtc.
1161 * This lets the caller know that this information
1162 * isn't available; using the wrong pipe for
1163 * vblank waiting can cause the chipset to lock up
1164 */
1165 return -1;
1166 }
1167
1168 return get_pipe_from_crtc_id.pipe;
1169}
1170
1171static int
1172drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
1173 unsigned long size, void *data)
1174{
1175 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1176 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1177 struct drm_i915_gem_pread pread;
1178 int ret;
1179
1180 memset(&pread, 0, sizeof(pread));
1181 pread.handle = bo_gem->gem_handle;
1182 pread.offset = offset;
1183 pread.size = size;
1184 pread.data_ptr = (uint64_t) (uintptr_t) data;
1185 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001186 ret = ioctl(bufmgr_gem->fd,
1187 DRM_IOCTL_I915_GEM_PREAD,
1188 &pread);
Eric Anholtd70d6052009-10-06 12:40:42 -07001189 } while (ret == -1 && errno == EINTR);
1190 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001191 ret = -errno;
Eric Anholtd70d6052009-10-06 12:40:42 -07001192 fprintf(stderr,
1193 "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
1194 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1195 (int)size, strerror(errno));
1196 }
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001197
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001198 return ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001199}
1200
Eric Anholt6fb1ad72008-11-13 11:44:22 -08001201/** Waits for all GPU rendering to the object to have completed. */
Eric Anholt6a9eb082008-06-03 09:27:37 -07001202static void
Eric Anholt4b982642008-10-30 09:33:07 -07001203drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001204{
Eric Anholtd70d6052009-10-06 12:40:42 -07001205 drm_intel_gem_bo_start_gtt_access(bo, 0);
Eric Anholt6fb1ad72008-11-13 11:44:22 -08001206}
1207
1208/**
1209 * Sets the object to the GTT read and possibly write domain, used by the X
1210 * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
1211 *
1212 * In combination with drm_intel_gem_bo_pin() and manual fence management, we
1213 * can do tiled pixmaps this way.
1214 */
1215void
1216drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable)
1217{
Eric Anholtd70d6052009-10-06 12:40:42 -07001218 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1219 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1220 struct drm_i915_gem_set_domain set_domain;
1221 int ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001222
Eric Anholtd70d6052009-10-06 12:40:42 -07001223 set_domain.handle = bo_gem->gem_handle;
1224 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1225 set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0;
1226 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001227 ret = ioctl(bufmgr_gem->fd,
1228 DRM_IOCTL_I915_GEM_SET_DOMAIN,
Eric Anholtd70d6052009-10-06 12:40:42 -07001229 &set_domain);
1230 } while (ret == -1 && errno == EINTR);
1231 if (ret != 0) {
1232 fprintf(stderr,
1233 "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
1234 __FILE__, __LINE__, bo_gem->gem_handle,
1235 set_domain.read_domains, set_domain.write_domain,
1236 strerror(errno));
1237 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001238}
1239
1240static void
Eric Anholt4b982642008-10-30 09:33:07 -07001241drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001242{
Eric Anholtd70d6052009-10-06 12:40:42 -07001243 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1244 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001245
Jesse Barnesb5096402009-09-15 11:02:58 -07001246 free(bufmgr_gem->exec2_objects);
Eric Anholtd70d6052009-10-06 12:40:42 -07001247 free(bufmgr_gem->exec_objects);
1248 free(bufmgr_gem->exec_bos);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001249
Eric Anholtd70d6052009-10-06 12:40:42 -07001250 pthread_mutex_destroy(&bufmgr_gem->lock);
Eric Anholt6df7b072008-06-12 23:22:26 -07001251
Eric Anholtd70d6052009-10-06 12:40:42 -07001252 /* Free any cached buffer objects we were going to reuse */
1253 for (i = 0; i < DRM_INTEL_GEM_BO_BUCKETS; i++) {
1254 struct drm_intel_gem_bo_bucket *bucket =
1255 &bufmgr_gem->cache_bucket[i];
1256 drm_intel_bo_gem *bo_gem;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001257
Eric Anholtd70d6052009-10-06 12:40:42 -07001258 while (!DRMLISTEMPTY(&bucket->head)) {
1259 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1260 bucket->head.next, head);
1261 DRMLISTDEL(&bo_gem->head);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001262
Eric Anholtd70d6052009-10-06 12:40:42 -07001263 drm_intel_gem_bo_free(&bo_gem->bo);
1264 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001265 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001266
Eric Anholtd70d6052009-10-06 12:40:42 -07001267 free(bufmgr);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001268}
1269
1270/**
1271 * Adds the target buffer to the validation list and adds the relocation
1272 * to the reloc_buffer's relocation list.
1273 *
1274 * The relocation entry at the given offset must already contain the
1275 * precomputed relocation value, because the kernel will optimize out
1276 * the relocation entry write when the buffer hasn't moved from the
1277 * last known offset in target_bo.
1278 */
1279static int
Jesse Barnesb5096402009-09-15 11:02:58 -07001280do_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1281 drm_intel_bo *target_bo, uint32_t target_offset,
1282 uint32_t read_domains, uint32_t write_domain,
1283 int need_fence)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001284{
Eric Anholtd70d6052009-10-06 12:40:42 -07001285 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1286 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1287 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001288
Chris Wilson97077332009-12-01 23:01:34 +00001289 if (bo_gem->has_error)
Chris Wilson792fed12009-12-02 13:12:39 +00001290 return -ENOMEM;
Chris Wilson792fed12009-12-02 13:12:39 +00001291
1292 if (target_bo_gem->has_error) {
1293 bo_gem->has_error = 1;
Chris Wilson792fed12009-12-02 13:12:39 +00001294 return -ENOMEM;
1295 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001296
Jesse Barnesb5096402009-09-15 11:02:58 -07001297 if (target_bo_gem->tiling_mode == I915_TILING_NONE)
1298 need_fence = 0;
1299
1300 /* We never use HW fences for rendering on 965+ */
Eric Anholta1f9ea72010-03-02 08:49:36 -08001301 if (bufmgr_gem->gen >= 4)
Jesse Barnesb5096402009-09-15 11:02:58 -07001302 need_fence = 0;
1303
Eric Anholtd70d6052009-10-06 12:40:42 -07001304 /* Create a new relocation list if needed */
Chris Wilson97077332009-12-01 23:01:34 +00001305 if (bo_gem->relocs == NULL && drm_intel_setup_reloc_list(bo))
Chris Wilson792fed12009-12-02 13:12:39 +00001306 return -ENOMEM;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001307
Eric Anholtd70d6052009-10-06 12:40:42 -07001308 /* Check overflow */
1309 assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001310
Eric Anholtd70d6052009-10-06 12:40:42 -07001311 /* Check args */
1312 assert(offset <= bo->size - 4);
1313 assert((write_domain & (write_domain - 1)) == 0);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001314
Eric Anholtd70d6052009-10-06 12:40:42 -07001315 /* Make sure that we're not adding a reloc to something whose size has
1316 * already been accounted for.
1317 */
1318 assert(!bo_gem->used_as_reloc_target);
1319 bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size;
Eric Anholta1f9ea72010-03-02 08:49:36 -08001320 /* An object needing a fence is a tiled buffer, so it won't have
Jesse Barnesb5096402009-09-15 11:02:58 -07001321 * relocs to other buffers.
1322 */
1323 if (need_fence)
1324 target_bo_gem->reloc_tree_fences = 1;
Eric Anholtd70d6052009-10-06 12:40:42 -07001325 bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences;
Eric Anholt0e867312008-10-21 00:10:54 -07001326
Eric Anholtd70d6052009-10-06 12:40:42 -07001327 /* Flag the target to disallow further relocations in it. */
1328 target_bo_gem->used_as_reloc_target = 1;
Eric Anholt0e867312008-10-21 00:10:54 -07001329
Eric Anholtd70d6052009-10-06 12:40:42 -07001330 bo_gem->relocs[bo_gem->reloc_count].offset = offset;
1331 bo_gem->relocs[bo_gem->reloc_count].delta = target_offset;
1332 bo_gem->relocs[bo_gem->reloc_count].target_handle =
1333 target_bo_gem->gem_handle;
1334 bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
1335 bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
1336 bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001337
Jesse Barnesb5096402009-09-15 11:02:58 -07001338 bo_gem->reloc_target_info[bo_gem->reloc_count].bo = target_bo;
Eric Anholtd70d6052009-10-06 12:40:42 -07001339 drm_intel_gem_bo_reference(target_bo);
Jesse Barnesb5096402009-09-15 11:02:58 -07001340 if (need_fence)
1341 bo_gem->reloc_target_info[bo_gem->reloc_count].flags =
1342 DRM_INTEL_RELOC_FENCE;
1343 else
1344 bo_gem->reloc_target_info[bo_gem->reloc_count].flags = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001345
Eric Anholtd70d6052009-10-06 12:40:42 -07001346 bo_gem->reloc_count++;
Eric Anholt6df7b072008-06-12 23:22:26 -07001347
Eric Anholtd70d6052009-10-06 12:40:42 -07001348 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001349}
1350
Jesse Barnesb5096402009-09-15 11:02:58 -07001351static int
1352drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1353 drm_intel_bo *target_bo, uint32_t target_offset,
1354 uint32_t read_domains, uint32_t write_domain)
1355{
1356 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
1357
1358 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1359 read_domains, write_domain,
1360 !bufmgr_gem->fenced_relocs);
1361}
1362
1363static int
1364drm_intel_gem_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
1365 drm_intel_bo *target_bo,
1366 uint32_t target_offset,
1367 uint32_t read_domains, uint32_t write_domain)
1368{
1369 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1370 read_domains, write_domain, 1);
1371}
1372
Eric Anholt6a9eb082008-06-03 09:27:37 -07001373/**
1374 * Walk the tree of relocations rooted at BO and accumulate the list of
1375 * validations to be performed and update the relocation buffers with
1376 * index values into the validation list.
1377 */
1378static void
Eric Anholt4b982642008-10-30 09:33:07 -07001379drm_intel_gem_bo_process_reloc(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001380{
Eric Anholtd70d6052009-10-06 12:40:42 -07001381 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1382 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001383
Eric Anholtd70d6052009-10-06 12:40:42 -07001384 if (bo_gem->relocs == NULL)
1385 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001386
Eric Anholtd70d6052009-10-06 12:40:42 -07001387 for (i = 0; i < bo_gem->reloc_count; i++) {
Jesse Barnesb5096402009-09-15 11:02:58 -07001388 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001389
Eric Anholtd70d6052009-10-06 12:40:42 -07001390 /* Continue walking the tree depth-first. */
1391 drm_intel_gem_bo_process_reloc(target_bo);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001392
Eric Anholtd70d6052009-10-06 12:40:42 -07001393 /* Add the target to the validate list */
1394 drm_intel_add_validate_buffer(target_bo);
1395 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001396}
1397
Eric Anholt6a9eb082008-06-03 09:27:37 -07001398static void
Jesse Barnesb5096402009-09-15 11:02:58 -07001399drm_intel_gem_bo_process_reloc2(drm_intel_bo *bo)
1400{
1401 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
1402 int i;
1403
1404 if (bo_gem->relocs == NULL)
1405 return;
1406
1407 for (i = 0; i < bo_gem->reloc_count; i++) {
1408 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
1409 int need_fence;
1410
1411 /* Continue walking the tree depth-first. */
1412 drm_intel_gem_bo_process_reloc2(target_bo);
1413
1414 need_fence = (bo_gem->reloc_target_info[i].flags &
1415 DRM_INTEL_RELOC_FENCE);
1416
1417 /* Add the target to the validate list */
1418 drm_intel_add_validate_buffer2(target_bo, need_fence);
1419 }
1420}
1421
1422
1423static void
Eric Anholtd70d6052009-10-06 12:40:42 -07001424drm_intel_update_buffer_offsets(drm_intel_bufmgr_gem *bufmgr_gem)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001425{
Eric Anholtd70d6052009-10-06 12:40:42 -07001426 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001427
Eric Anholtd70d6052009-10-06 12:40:42 -07001428 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1429 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1430 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001431
Eric Anholtd70d6052009-10-06 12:40:42 -07001432 /* Update the buffer offset */
1433 if (bufmgr_gem->exec_objects[i].offset != bo->offset) {
1434 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
1435 bo_gem->gem_handle, bo_gem->name, bo->offset,
1436 (unsigned long long)bufmgr_gem->exec_objects[i].
1437 offset);
1438 bo->offset = bufmgr_gem->exec_objects[i].offset;
1439 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001440 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001441}
1442
Jesse Barnesb5096402009-09-15 11:02:58 -07001443static void
1444drm_intel_update_buffer_offsets2 (drm_intel_bufmgr_gem *bufmgr_gem)
1445{
1446 int i;
1447
1448 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1449 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1450 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
1451
1452 /* Update the buffer offset */
1453 if (bufmgr_gem->exec2_objects[i].offset != bo->offset) {
1454 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
1455 bo_gem->gem_handle, bo_gem->name, bo->offset,
1456 (unsigned long long)bufmgr_gem->exec2_objects[i].offset);
1457 bo->offset = bufmgr_gem->exec2_objects[i].offset;
1458 }
1459 }
1460}
1461
Eric Anholtf9d98be2008-09-08 08:51:40 -07001462static int
Eric Anholt4b982642008-10-30 09:33:07 -07001463drm_intel_gem_bo_exec(drm_intel_bo *bo, int used,
Eric Anholtd70d6052009-10-06 12:40:42 -07001464 drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001465{
Eric Anholtd70d6052009-10-06 12:40:42 -07001466 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
Chris Wilson792fed12009-12-02 13:12:39 +00001467 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholtd70d6052009-10-06 12:40:42 -07001468 struct drm_i915_gem_execbuffer execbuf;
1469 int ret, i;
Eric Anholtf9d98be2008-09-08 08:51:40 -07001470
Chris Wilson792fed12009-12-02 13:12:39 +00001471 if (bo_gem->has_error)
1472 return -ENOMEM;
1473
Eric Anholtd70d6052009-10-06 12:40:42 -07001474 pthread_mutex_lock(&bufmgr_gem->lock);
1475 /* Update indices and set up the validate list. */
1476 drm_intel_gem_bo_process_reloc(bo);
Eric Anholtf9d98be2008-09-08 08:51:40 -07001477
Eric Anholtd70d6052009-10-06 12:40:42 -07001478 /* Add the batch buffer to the validation list. There are no
1479 * relocations pointing to it.
1480 */
1481 drm_intel_add_validate_buffer(bo);
Eric Anholtf9d98be2008-09-08 08:51:40 -07001482
Eric Anholtd70d6052009-10-06 12:40:42 -07001483 execbuf.buffers_ptr = (uintptr_t) bufmgr_gem->exec_objects;
1484 execbuf.buffer_count = bufmgr_gem->exec_count;
1485 execbuf.batch_start_offset = 0;
1486 execbuf.batch_len = used;
1487 execbuf.cliprects_ptr = (uintptr_t) cliprects;
1488 execbuf.num_cliprects = num_cliprects;
1489 execbuf.DR1 = 0;
1490 execbuf.DR4 = DR4;
Eric Anholtf9d98be2008-09-08 08:51:40 -07001491
Eric Anholtd70d6052009-10-06 12:40:42 -07001492 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001493 ret = ioctl(bufmgr_gem->fd,
1494 DRM_IOCTL_I915_GEM_EXECBUFFER,
Eric Anholtd70d6052009-10-06 12:40:42 -07001495 &execbuf);
Chris Wilsonb73612e2009-12-02 12:58:00 +00001496 } while (ret != 0 && errno == EINTR);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001497
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001498 if (ret != 0) {
1499 ret = -errno;
1500 if (errno == ENOSPC) {
1501 fprintf(stderr,
1502 "Execbuffer fails to pin. "
1503 "Estimate: %u. Actual: %u. Available: %u\n",
1504 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
1505 bufmgr_gem->
1506 exec_count),
1507 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
1508 bufmgr_gem->
1509 exec_count),
1510 (unsigned int)bufmgr_gem->gtt_size);
1511 }
Eric Anholtd70d6052009-10-06 12:40:42 -07001512 }
1513 drm_intel_update_buffer_offsets(bufmgr_gem);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001514
Eric Anholtd70d6052009-10-06 12:40:42 -07001515 if (bufmgr_gem->bufmgr.debug)
1516 drm_intel_gem_dump_validation_list(bufmgr_gem);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001517
Eric Anholtd70d6052009-10-06 12:40:42 -07001518 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1519 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1520 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001521
Eric Anholtd70d6052009-10-06 12:40:42 -07001522 /* Disconnect the buffer from the validate list */
1523 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -07001524 bufmgr_gem->exec_bos[i] = NULL;
1525 }
1526 bufmgr_gem->exec_count = 0;
1527 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtf9d98be2008-09-08 08:51:40 -07001528
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001529 return ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001530}
1531
Keith Packard8e41ce12008-08-04 00:34:08 -07001532static int
Zou Nan hai66375fd2010-06-02 10:07:37 +08001533drm_intel_gem_bo_mrb_exec2(drm_intel_bo *bo, int used,
1534 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
1535 int ring_flag)
Jesse Barnesb5096402009-09-15 11:02:58 -07001536{
1537 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
1538 struct drm_i915_gem_execbuffer2 execbuf;
1539 int ret, i;
1540
Zou Nan hai66375fd2010-06-02 10:07:37 +08001541 if ((ring_flag != I915_EXEC_RENDER) && (ring_flag != I915_EXEC_BSD))
1542 return -EINVAL;
1543
Jesse Barnesb5096402009-09-15 11:02:58 -07001544 pthread_mutex_lock(&bufmgr_gem->lock);
1545 /* Update indices and set up the validate list. */
1546 drm_intel_gem_bo_process_reloc2(bo);
1547
1548 /* Add the batch buffer to the validation list. There are no relocations
1549 * pointing to it.
1550 */
1551 drm_intel_add_validate_buffer2(bo, 0);
1552
1553 execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec2_objects;
1554 execbuf.buffer_count = bufmgr_gem->exec_count;
1555 execbuf.batch_start_offset = 0;
1556 execbuf.batch_len = used;
1557 execbuf.cliprects_ptr = (uintptr_t)cliprects;
1558 execbuf.num_cliprects = num_cliprects;
1559 execbuf.DR1 = 0;
1560 execbuf.DR4 = DR4;
Zou Nan hai66375fd2010-06-02 10:07:37 +08001561 execbuf.flags = ring_flag;
Jesse Barnesb5096402009-09-15 11:02:58 -07001562 execbuf.rsvd1 = 0;
1563 execbuf.rsvd2 = 0;
1564
1565 do {
1566 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2,
1567 &execbuf);
Chris Wilsona4041e02010-03-07 14:15:40 +00001568 } while (ret != 0 && errno == EINTR);
Jesse Barnesb5096402009-09-15 11:02:58 -07001569
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001570 if (ret != 0) {
1571 ret = -errno;
1572 if (ret == -ENOMEM) {
1573 fprintf(stderr,
1574 "Execbuffer fails to pin. "
1575 "Estimate: %u. Actual: %u. Available: %u\n",
1576 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
1577 bufmgr_gem->exec_count),
1578 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
1579 bufmgr_gem->exec_count),
1580 (unsigned int) bufmgr_gem->gtt_size);
1581 }
Jesse Barnesb5096402009-09-15 11:02:58 -07001582 }
1583 drm_intel_update_buffer_offsets2(bufmgr_gem);
1584
1585 if (bufmgr_gem->bufmgr.debug)
1586 drm_intel_gem_dump_validation_list(bufmgr_gem);
1587
1588 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1589 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1590 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
1591
1592 /* Disconnect the buffer from the validate list */
1593 bo_gem->validate_index = -1;
1594 bufmgr_gem->exec_bos[i] = NULL;
1595 }
1596 bufmgr_gem->exec_count = 0;
1597 pthread_mutex_unlock(&bufmgr_gem->lock);
1598
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001599 return ret;
Jesse Barnesb5096402009-09-15 11:02:58 -07001600}
1601
1602static int
Zou Nan hai66375fd2010-06-02 10:07:37 +08001603drm_intel_gem_bo_exec2(drm_intel_bo *bo, int used,
1604 drm_clip_rect_t *cliprects, int num_cliprects,
1605 int DR4)
1606{
1607 return drm_intel_gem_bo_mrb_exec2(bo, used,
1608 cliprects, num_cliprects, DR4,
1609 I915_EXEC_RENDER);
1610}
1611
1612static int
Eric Anholt4b982642008-10-30 09:33:07 -07001613drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment)
Keith Packard8e41ce12008-08-04 00:34:08 -07001614{
Eric Anholtd70d6052009-10-06 12:40:42 -07001615 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1616 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1617 struct drm_i915_gem_pin pin;
1618 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001619
Eric Anholtd70d6052009-10-06 12:40:42 -07001620 memset(&pin, 0, sizeof(pin));
1621 pin.handle = bo_gem->gem_handle;
1622 pin.alignment = alignment;
Keith Packard8e41ce12008-08-04 00:34:08 -07001623
Eric Anholtd70d6052009-10-06 12:40:42 -07001624 do {
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001625 ret = ioctl(bufmgr_gem->fd,
1626 DRM_IOCTL_I915_GEM_PIN,
1627 &pin);
Eric Anholtd70d6052009-10-06 12:40:42 -07001628 } while (ret == -1 && errno == EINTR);
Eric Anholt02445ea2009-01-04 17:37:18 -08001629
Eric Anholtd70d6052009-10-06 12:40:42 -07001630 if (ret != 0)
1631 return -errno;
Keith Packard8e41ce12008-08-04 00:34:08 -07001632
Eric Anholtd70d6052009-10-06 12:40:42 -07001633 bo->offset = pin.offset;
1634 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07001635}
1636
1637static int
Eric Anholt4b982642008-10-30 09:33:07 -07001638drm_intel_gem_bo_unpin(drm_intel_bo *bo)
Keith Packard8e41ce12008-08-04 00:34:08 -07001639{
Eric Anholtd70d6052009-10-06 12:40:42 -07001640 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1641 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1642 struct drm_i915_gem_unpin unpin;
1643 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001644
Eric Anholtd70d6052009-10-06 12:40:42 -07001645 memset(&unpin, 0, sizeof(unpin));
1646 unpin.handle = bo_gem->gem_handle;
Keith Packard8e41ce12008-08-04 00:34:08 -07001647
Eric Anholtd70d6052009-10-06 12:40:42 -07001648 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
1649 if (ret != 0)
1650 return -errno;
Keith Packard8e41ce12008-08-04 00:34:08 -07001651
Eric Anholtd70d6052009-10-06 12:40:42 -07001652 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07001653}
1654
1655static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001656drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
Eric Anholt4b982642008-10-30 09:33:07 -07001657 uint32_t stride)
Keith Packard8e41ce12008-08-04 00:34:08 -07001658{
Eric Anholtd70d6052009-10-06 12:40:42 -07001659 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1660 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1661 struct drm_i915_gem_set_tiling set_tiling;
1662 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001663
Eric Anholtd70d6052009-10-06 12:40:42 -07001664 if (bo_gem->global_name == 0 && *tiling_mode == bo_gem->tiling_mode)
1665 return 0;
Keith Packard18f091d2008-12-15 15:08:12 -08001666
Eric Anholtd70d6052009-10-06 12:40:42 -07001667 memset(&set_tiling, 0, sizeof(set_tiling));
1668 set_tiling.handle = bo_gem->gem_handle;
Keith Packard8e41ce12008-08-04 00:34:08 -07001669
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001670 do {
Chris Wilson4f0f8712010-02-10 09:45:13 +00001671 set_tiling.tiling_mode = *tiling_mode;
1672 set_tiling.stride = stride;
1673
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001674 ret = ioctl(bufmgr_gem->fd,
1675 DRM_IOCTL_I915_GEM_SET_TILING,
1676 &set_tiling);
1677 } while (ret == -1 && errno == EINTR);
Chris Wilsonfcf3e612010-05-24 18:35:41 +01001678 if (ret == 0) {
1679 bo_gem->tiling_mode = set_tiling.tiling_mode;
1680 bo_gem->swizzle_mode = set_tiling.swizzle_mode;
1681 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
1682 } else
1683 ret = -errno;
Chris Wilsone22fb792009-11-30 22:14:30 +00001684
Keith Packard18f091d2008-12-15 15:08:12 -08001685 *tiling_mode = bo_gem->tiling_mode;
Chris Wilsonfcf3e612010-05-24 18:35:41 +01001686 return ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001687}
1688
1689static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001690drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
1691 uint32_t * swizzle_mode)
Keith Packard8e41ce12008-08-04 00:34:08 -07001692{
Eric Anholtd70d6052009-10-06 12:40:42 -07001693 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt99338382008-10-14 13:18:11 -07001694
Eric Anholtd70d6052009-10-06 12:40:42 -07001695 *tiling_mode = bo_gem->tiling_mode;
1696 *swizzle_mode = bo_gem->swizzle_mode;
1697 return 0;
Eric Anholt99338382008-10-14 13:18:11 -07001698}
1699
1700static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001701drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name)
Keith Packard8e41ce12008-08-04 00:34:08 -07001702{
Eric Anholtd70d6052009-10-06 12:40:42 -07001703 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1704 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1705 struct drm_gem_flink flink;
1706 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001707
Eric Anholtd70d6052009-10-06 12:40:42 -07001708 if (!bo_gem->global_name) {
1709 memset(&flink, 0, sizeof(flink));
1710 flink.handle = bo_gem->gem_handle;
1711
1712 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
1713 if (ret != 0)
1714 return -errno;
1715 bo_gem->global_name = flink.name;
1716 bo_gem->reusable = 0;
1717 }
1718
1719 *name = bo_gem->global_name;
1720 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07001721}
1722
Eric Anholt6a9eb082008-06-03 09:27:37 -07001723/**
1724 * Enables unlimited caching of buffer objects for reuse.
1725 *
1726 * This is potentially very memory expensive, as the cache at each bucket
1727 * size is only bounded by how many buffers of that size we've managed to have
1728 * in flight at once.
1729 */
1730void
Eric Anholt4b982642008-10-30 09:33:07 -07001731drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001732{
Eric Anholtd70d6052009-10-06 12:40:42 -07001733 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001734
Eric Anholtd70d6052009-10-06 12:40:42 -07001735 bufmgr_gem->bo_reuse = 1;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001736}
1737
Eric Anholt0e867312008-10-21 00:10:54 -07001738/**
Jesse Barnesb5096402009-09-15 11:02:58 -07001739 * Enable use of fenced reloc type.
1740 *
1741 * New code should enable this to avoid unnecessary fence register
1742 * allocation. If this option is not enabled, all relocs will have fence
1743 * register allocated.
1744 */
1745void
1746drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr)
1747{
Eric Anholt766fa792010-03-02 16:04:14 -08001748 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
Jesse Barnesb5096402009-09-15 11:02:58 -07001749
Eric Anholt766fa792010-03-02 16:04:14 -08001750 if (bufmgr_gem->bufmgr.bo_exec == drm_intel_gem_bo_exec2)
1751 bufmgr_gem->fenced_relocs = 1;
Jesse Barnesb5096402009-09-15 11:02:58 -07001752}
1753
1754/**
Eric Anholt0e867312008-10-21 00:10:54 -07001755 * Return the additional aperture space required by the tree of buffer objects
1756 * rooted at bo.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001757 */
1758static int
Eric Anholt4b982642008-10-30 09:33:07 -07001759drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001760{
Eric Anholtd70d6052009-10-06 12:40:42 -07001761 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1762 int i;
1763 int total = 0;
Eric Anholt0e867312008-10-21 00:10:54 -07001764
Eric Anholtd70d6052009-10-06 12:40:42 -07001765 if (bo == NULL || bo_gem->included_in_check_aperture)
1766 return 0;
Eric Anholt0e867312008-10-21 00:10:54 -07001767
Eric Anholtd70d6052009-10-06 12:40:42 -07001768 total += bo->size;
1769 bo_gem->included_in_check_aperture = 1;
Eric Anholt0e867312008-10-21 00:10:54 -07001770
Eric Anholtd70d6052009-10-06 12:40:42 -07001771 for (i = 0; i < bo_gem->reloc_count; i++)
1772 total +=
1773 drm_intel_gem_bo_get_aperture_space(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -07001774 reloc_target_info[i].bo);
Eric Anholt0e867312008-10-21 00:10:54 -07001775
Eric Anholtd70d6052009-10-06 12:40:42 -07001776 return total;
Eric Anholt0e867312008-10-21 00:10:54 -07001777}
1778
1779/**
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001780 * Count the number of buffers in this list that need a fence reg
1781 *
1782 * If the count is greater than the number of available regs, we'll have
1783 * to ask the caller to resubmit a batch with fewer tiled buffers.
1784 *
Eric Anholt9209c9a2009-01-27 16:54:11 -08001785 * This function over-counts if the same buffer is used multiple times.
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001786 */
1787static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -07001788drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count)
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001789{
Eric Anholtd70d6052009-10-06 12:40:42 -07001790 int i;
1791 unsigned int total = 0;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001792
Eric Anholtd70d6052009-10-06 12:40:42 -07001793 for (i = 0; i < count; i++) {
1794 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001795
Eric Anholtd70d6052009-10-06 12:40:42 -07001796 if (bo_gem == NULL)
1797 continue;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001798
Eric Anholtd70d6052009-10-06 12:40:42 -07001799 total += bo_gem->reloc_tree_fences;
1800 }
1801 return total;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001802}
1803
1804/**
Eric Anholt4b982642008-10-30 09:33:07 -07001805 * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
1806 * for the next drm_intel_bufmgr_check_aperture_space() call.
Eric Anholt0e867312008-10-21 00:10:54 -07001807 */
1808static void
Eric Anholt4b982642008-10-30 09:33:07 -07001809drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo)
Eric Anholt0e867312008-10-21 00:10:54 -07001810{
Eric Anholtd70d6052009-10-06 12:40:42 -07001811 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1812 int i;
Eric Anholt0e867312008-10-21 00:10:54 -07001813
Eric Anholtd70d6052009-10-06 12:40:42 -07001814 if (bo == NULL || !bo_gem->included_in_check_aperture)
1815 return;
Eric Anholt0e867312008-10-21 00:10:54 -07001816
Eric Anholtd70d6052009-10-06 12:40:42 -07001817 bo_gem->included_in_check_aperture = 0;
Eric Anholt0e867312008-10-21 00:10:54 -07001818
Eric Anholtd70d6052009-10-06 12:40:42 -07001819 for (i = 0; i < bo_gem->reloc_count; i++)
1820 drm_intel_gem_bo_clear_aperture_space_flag(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -07001821 reloc_target_info[i].bo);
Eric Anholt0e867312008-10-21 00:10:54 -07001822}
1823
1824/**
Keith Packardb13f4e12008-11-21 01:49:39 -08001825 * Return a conservative estimate for the amount of aperture required
1826 * for a collection of buffers. This may double-count some buffers.
1827 */
1828static unsigned int
1829drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count)
1830{
Eric Anholtd70d6052009-10-06 12:40:42 -07001831 int i;
1832 unsigned int total = 0;
Keith Packardb13f4e12008-11-21 01:49:39 -08001833
Eric Anholtd70d6052009-10-06 12:40:42 -07001834 for (i = 0; i < count; i++) {
1835 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
1836 if (bo_gem != NULL)
1837 total += bo_gem->reloc_tree_size;
1838 }
1839 return total;
Keith Packardb13f4e12008-11-21 01:49:39 -08001840}
1841
1842/**
1843 * Return the amount of aperture needed for a collection of buffers.
1844 * This avoids double counting any buffers, at the cost of looking
1845 * at every buffer in the set.
1846 */
1847static unsigned int
1848drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count)
1849{
Eric Anholtd70d6052009-10-06 12:40:42 -07001850 int i;
1851 unsigned int total = 0;
Keith Packardb13f4e12008-11-21 01:49:39 -08001852
Eric Anholtd70d6052009-10-06 12:40:42 -07001853 for (i = 0; i < count; i++) {
1854 total += drm_intel_gem_bo_get_aperture_space(bo_array[i]);
1855 /* For the first buffer object in the array, we get an
1856 * accurate count back for its reloc_tree size (since nothing
1857 * had been flagged as being counted yet). We can save that
1858 * value out as a more conservative reloc_tree_size that
1859 * avoids double-counting target buffers. Since the first
1860 * buffer happens to usually be the batch buffer in our
1861 * callers, this can pull us back from doing the tree
1862 * walk on every new batch emit.
1863 */
1864 if (i == 0) {
1865 drm_intel_bo_gem *bo_gem =
1866 (drm_intel_bo_gem *) bo_array[i];
1867 bo_gem->reloc_tree_size = total;
1868 }
Eric Anholt7ce8d4c2009-02-27 13:46:31 -08001869 }
Keith Packardb13f4e12008-11-21 01:49:39 -08001870
Eric Anholtd70d6052009-10-06 12:40:42 -07001871 for (i = 0; i < count; i++)
1872 drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]);
1873 return total;
Keith Packardb13f4e12008-11-21 01:49:39 -08001874}
1875
1876/**
Eric Anholt0e867312008-10-21 00:10:54 -07001877 * Return -1 if the batchbuffer should be flushed before attempting to
1878 * emit rendering referencing the buffers pointed to by bo_array.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001879 *
Eric Anholt0e867312008-10-21 00:10:54 -07001880 * This is required because if we try to emit a batchbuffer with relocations
1881 * to a tree of buffers that won't simultaneously fit in the aperture,
1882 * the rendering will return an error at a point where the software is not
1883 * prepared to recover from it.
1884 *
1885 * However, we also want to emit the batchbuffer significantly before we reach
1886 * the limit, as a series of batchbuffers each of which references buffers
1887 * covering almost all of the aperture means that at each emit we end up
1888 * waiting to evict a buffer from the last rendering, and we get synchronous
1889 * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
1890 * get better parallelism.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001891 */
1892static int
Eric Anholt4b982642008-10-30 09:33:07 -07001893drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001894{
Eric Anholtd70d6052009-10-06 12:40:42 -07001895 drm_intel_bufmgr_gem *bufmgr_gem =
1896 (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr;
1897 unsigned int total = 0;
1898 unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4;
1899 int total_fences;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001900
Eric Anholtd70d6052009-10-06 12:40:42 -07001901 /* Check for fence reg constraints if necessary */
1902 if (bufmgr_gem->available_fences) {
1903 total_fences = drm_intel_gem_total_fences(bo_array, count);
1904 if (total_fences > bufmgr_gem->available_fences)
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001905 return -ENOSPC;
Eric Anholtd70d6052009-10-06 12:40:42 -07001906 }
Eric Anholt0e867312008-10-21 00:10:54 -07001907
Eric Anholtd70d6052009-10-06 12:40:42 -07001908 total = drm_intel_gem_estimate_batch_space(bo_array, count);
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001909
Eric Anholtd70d6052009-10-06 12:40:42 -07001910 if (total > threshold)
1911 total = drm_intel_gem_compute_batch_space(bo_array, count);
Eric Anholt0e867312008-10-21 00:10:54 -07001912
Eric Anholtd70d6052009-10-06 12:40:42 -07001913 if (total > threshold) {
1914 DBG("check_space: overflowed available aperture, "
1915 "%dkb vs %dkb\n",
1916 total / 1024, (int)bufmgr_gem->gtt_size / 1024);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001917 return -ENOSPC;
Eric Anholtd70d6052009-10-06 12:40:42 -07001918 } else {
1919 DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024,
1920 (int)bufmgr_gem->gtt_size / 1024);
1921 return 0;
1922 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001923}
1924
Keith Packard5b5ce302009-05-11 13:42:12 -07001925/*
1926 * Disable buffer reuse for objects which are shared with the kernel
1927 * as scanout buffers
1928 */
1929static int
1930drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo)
1931{
Eric Anholtd70d6052009-10-06 12:40:42 -07001932 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Keith Packard5b5ce302009-05-11 13:42:12 -07001933
Eric Anholtd70d6052009-10-06 12:40:42 -07001934 bo_gem->reusable = 0;
1935 return 0;
Keith Packard5b5ce302009-05-11 13:42:12 -07001936}
1937
Eric Anholt769b1052009-10-01 19:09:26 -07001938static int
Chris Wilson07e75892010-05-11 08:54:06 +01001939drm_intel_gem_bo_is_reusable(drm_intel_bo *bo)
1940{
1941 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1942
1943 return bo_gem->reusable;
1944}
1945
1946static int
Eric Anholt66d27142009-10-20 13:20:55 -07001947_drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
Eric Anholt769b1052009-10-01 19:09:26 -07001948{
Eric Anholtd70d6052009-10-06 12:40:42 -07001949 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1950 int i;
Eric Anholt769b1052009-10-01 19:09:26 -07001951
Eric Anholtd70d6052009-10-06 12:40:42 -07001952 for (i = 0; i < bo_gem->reloc_count; i++) {
Jesse Barnesb5096402009-09-15 11:02:58 -07001953 if (bo_gem->reloc_target_info[i].bo == target_bo)
Eric Anholtd70d6052009-10-06 12:40:42 -07001954 return 1;
Jesse Barnesb5096402009-09-15 11:02:58 -07001955 if (_drm_intel_gem_bo_references(bo_gem->reloc_target_info[i].bo,
Eric Anholtd70d6052009-10-06 12:40:42 -07001956 target_bo))
1957 return 1;
1958 }
1959
Eric Anholt769b1052009-10-01 19:09:26 -07001960 return 0;
Eric Anholt769b1052009-10-01 19:09:26 -07001961}
1962
Eric Anholt66d27142009-10-20 13:20:55 -07001963/** Return true if target_bo is referenced by bo's relocation tree. */
1964static int
1965drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
1966{
1967 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
1968
1969 if (bo == NULL || target_bo == NULL)
1970 return 0;
1971 if (target_bo_gem->used_as_reloc_target)
1972 return _drm_intel_gem_bo_references(bo, target_bo);
1973 return 0;
1974}
1975
Eric Anholt769b1052009-10-01 19:09:26 -07001976/**
Eric Anholt6a9eb082008-06-03 09:27:37 -07001977 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
1978 * and manage map buffer objections.
1979 *
1980 * \param fd File descriptor of the opened DRM device.
1981 */
Eric Anholt4b982642008-10-30 09:33:07 -07001982drm_intel_bufmgr *
1983drm_intel_bufmgr_gem_init(int fd, int batch_size)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001984{
Eric Anholtd70d6052009-10-06 12:40:42 -07001985 drm_intel_bufmgr_gem *bufmgr_gem;
1986 struct drm_i915_gem_get_aperture aperture;
1987 drm_i915_getparam_t gp;
1988 int ret, i;
1989 unsigned long size;
Zou Nan hai66375fd2010-06-02 10:07:37 +08001990 int exec2 = 0, has_bsd = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001991
Eric Anholtd70d6052009-10-06 12:40:42 -07001992 bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
Dave Airlie973d8d62010-02-02 10:57:12 +10001993 if (bufmgr_gem == NULL)
1994 return NULL;
1995
Eric Anholtd70d6052009-10-06 12:40:42 -07001996 bufmgr_gem->fd = fd;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001997
Eric Anholtd70d6052009-10-06 12:40:42 -07001998 if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) {
1999 free(bufmgr_gem);
2000 return NULL;
2001 }
Eric Anholt6df7b072008-06-12 23:22:26 -07002002
Eric Anholtd70d6052009-10-06 12:40:42 -07002003 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
Eric Anholt0e867312008-10-21 00:10:54 -07002004
Eric Anholtd70d6052009-10-06 12:40:42 -07002005 if (ret == 0)
2006 bufmgr_gem->gtt_size = aperture.aper_available_size;
2007 else {
2008 fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
2009 strerror(errno));
2010 bufmgr_gem->gtt_size = 128 * 1024 * 1024;
2011 fprintf(stderr, "Assuming %dkB available aperture size.\n"
2012 "May lead to reduced performance or incorrect "
2013 "rendering.\n",
2014 (int)bufmgr_gem->gtt_size / 1024);
2015 }
Eric Anholt0e867312008-10-21 00:10:54 -07002016
Eric Anholtd70d6052009-10-06 12:40:42 -07002017 gp.param = I915_PARAM_CHIPSET_ID;
2018 gp.value = &bufmgr_gem->pci_device;
Eric Anholtcbdd6272009-01-27 17:16:11 -08002019 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2020 if (ret) {
Eric Anholtd70d6052009-10-06 12:40:42 -07002021 fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno);
2022 fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value);
Eric Anholtcbdd6272009-01-27 17:16:11 -08002023 }
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002024
Eric Anholta1f9ea72010-03-02 08:49:36 -08002025 if (IS_GEN2(bufmgr_gem))
2026 bufmgr_gem->gen = 2;
2027 else if (IS_GEN3(bufmgr_gem))
2028 bufmgr_gem->gen = 3;
2029 else if (IS_GEN4(bufmgr_gem))
2030 bufmgr_gem->gen = 4;
2031 else
2032 bufmgr_gem->gen = 6;
2033
Jesse Barnesb5096402009-09-15 11:02:58 -07002034 gp.param = I915_PARAM_HAS_EXECBUF2;
2035 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2036 if (!ret)
2037 exec2 = 1;
2038
Zou Nan hai66375fd2010-06-02 10:07:37 +08002039 gp.param = I915_PARAM_HAS_BSD;
2040 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2041 if (!ret)
2042 has_bsd = 1;
2043
Eric Anholta1f9ea72010-03-02 08:49:36 -08002044 if (bufmgr_gem->gen < 4) {
Eric Anholtd70d6052009-10-06 12:40:42 -07002045 gp.param = I915_PARAM_NUM_FENCES_AVAIL;
2046 gp.value = &bufmgr_gem->available_fences;
2047 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2048 if (ret) {
2049 fprintf(stderr, "get fences failed: %d [%d]\n", ret,
2050 errno);
2051 fprintf(stderr, "param: %d, val: %d\n", gp.param,
2052 *gp.value);
2053 bufmgr_gem->available_fences = 0;
Chris Wilsonfdcde592010-02-09 08:32:54 +00002054 } else {
2055 /* XXX The kernel reports the total number of fences,
2056 * including any that may be pinned.
2057 *
2058 * We presume that there will be at least one pinned
2059 * fence for the scanout buffer, but there may be more
2060 * than one scanout and the user may be manually
2061 * pinning buffers. Let's move to execbuffer2 and
2062 * thereby forget the insanity of using fences...
2063 */
2064 bufmgr_gem->available_fences -= 2;
2065 if (bufmgr_gem->available_fences < 0)
2066 bufmgr_gem->available_fences = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07002067 }
2068 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002069
Eric Anholtd70d6052009-10-06 12:40:42 -07002070 /* Let's go with one relocation per every 2 dwords (but round down a bit
2071 * since a power of two will mean an extra page allocation for the reloc
2072 * buffer).
2073 *
2074 * Every 4 was too few for the blender benchmark.
2075 */
2076 bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
Eric Anholt769b1052009-10-01 19:09:26 -07002077
Eric Anholtd70d6052009-10-06 12:40:42 -07002078 bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc;
2079 bufmgr_gem->bufmgr.bo_alloc_for_render =
2080 drm_intel_gem_bo_alloc_for_render;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -07002081 bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled;
Eric Anholtd70d6052009-10-06 12:40:42 -07002082 bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference;
2083 bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference;
2084 bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map;
2085 bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap;
2086 bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata;
2087 bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata;
2088 bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering;
2089 bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc;
Jesse Barnesb5096402009-09-15 11:02:58 -07002090 bufmgr_gem->bufmgr.bo_emit_reloc_fence = drm_intel_gem_bo_emit_reloc_fence;
Eric Anholtd70d6052009-10-06 12:40:42 -07002091 bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin;
2092 bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin;
2093 bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling;
2094 bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling;
2095 bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink;
Jesse Barnesb5096402009-09-15 11:02:58 -07002096 /* Use the new one if available */
Zou Nan hai66375fd2010-06-02 10:07:37 +08002097 if (exec2) {
Jesse Barnesb5096402009-09-15 11:02:58 -07002098 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec2;
Zou Nan hai66375fd2010-06-02 10:07:37 +08002099 if (has_bsd)
2100 bufmgr_gem->bufmgr.bo_mrb_exec = drm_intel_gem_bo_mrb_exec2;
2101 } else
Jesse Barnesb5096402009-09-15 11:02:58 -07002102 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec;
Eric Anholtd70d6052009-10-06 12:40:42 -07002103 bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy;
Chris Wilson83a35b62009-11-11 13:04:38 +00002104 bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise;
Eric Anholtd70d6052009-10-06 12:40:42 -07002105 bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_destroy;
2106 bufmgr_gem->bufmgr.debug = 0;
2107 bufmgr_gem->bufmgr.check_aperture_space =
2108 drm_intel_gem_check_aperture_space;
2109 bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse;
Chris Wilson07e75892010-05-11 08:54:06 +01002110 bufmgr_gem->bufmgr.bo_is_reusable = drm_intel_gem_bo_is_reusable;
Eric Anholtd70d6052009-10-06 12:40:42 -07002111 bufmgr_gem->bufmgr.get_pipe_from_crtc_id =
2112 drm_intel_gem_get_pipe_from_crtc_id;
2113 bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002114
Eric Anholtd70d6052009-10-06 12:40:42 -07002115 /* Initialize the linked lists for BO reuse cache. */
2116 for (i = 0, size = 4096; i < DRM_INTEL_GEM_BO_BUCKETS; i++, size *= 2) {
2117 DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head);
2118 bufmgr_gem->cache_bucket[i].size = size;
2119 }
2120
2121 return &bufmgr_gem->bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002122}