blob: f5ab0a6f37c599e338c956a47eb40ed10d724d79 [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 Anholt0ec768e2010-06-04 17:09:11 -070069#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
70
Eric Anholt4b982642008-10-30 09:33:07 -070071typedef struct _drm_intel_bo_gem drm_intel_bo_gem;
Keith Packarda919ff52008-06-05 15:58:09 -070072
Eric Anholt4b982642008-10-30 09:33:07 -070073struct drm_intel_gem_bo_bucket {
Eric Anholtd70d6052009-10-06 12:40:42 -070074 drmMMListHead head;
75 unsigned long size;
Eric Anholt6a9eb082008-06-03 09:27:37 -070076};
77
Eric Anholt4b982642008-10-30 09:33:07 -070078typedef struct _drm_intel_bufmgr_gem {
Eric Anholtd70d6052009-10-06 12:40:42 -070079 drm_intel_bufmgr bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -070080
Eric Anholtd70d6052009-10-06 12:40:42 -070081 int fd;
Eric Anholt6a9eb082008-06-03 09:27:37 -070082
Eric Anholtd70d6052009-10-06 12:40:42 -070083 int max_relocs;
Eric Anholt6a9eb082008-06-03 09:27:37 -070084
Eric Anholtd70d6052009-10-06 12:40:42 -070085 pthread_mutex_t lock;
Eric Anholt6df7b072008-06-12 23:22:26 -070086
Eric Anholtd70d6052009-10-06 12:40:42 -070087 struct drm_i915_gem_exec_object *exec_objects;
Jesse Barnesb5096402009-09-15 11:02:58 -070088 struct drm_i915_gem_exec_object2 *exec2_objects;
Eric Anholtd70d6052009-10-06 12:40:42 -070089 drm_intel_bo **exec_bos;
90 int exec_size;
91 int exec_count;
Eric Anholt6a9eb082008-06-03 09:27:37 -070092
Eric Anholtd70d6052009-10-06 12:40:42 -070093 /** Array of lists of cached gem objects of power-of-two sizes */
Eric Anholt0ec768e2010-06-04 17:09:11 -070094 struct drm_intel_gem_bo_bucket cache_bucket[14 * 4];
95 int num_buckets;
Chris Wilsonf16b4162010-06-21 15:21:48 +010096 time_t time;
Eric Anholt6a9eb082008-06-03 09:27:37 -070097
Chris Wilson36d49392011-02-14 09:39:06 +000098 drmMMListHead named;
99
Eric Anholtd70d6052009-10-06 12:40:42 -0700100 uint64_t gtt_size;
101 int available_fences;
102 int pci_device;
Eric Anholta1f9ea72010-03-02 08:49:36 -0800103 int gen;
Chris Wilson36245772010-10-29 10:49:54 +0100104 unsigned int has_bsd : 1;
105 unsigned int has_blt : 1;
106 unsigned int has_relaxed_fencing : 1;
107 unsigned int bo_reuse : 1;
Jesse Barnesb5096402009-09-15 11:02:58 -0700108 char fenced_relocs;
Eric Anholt4b982642008-10-30 09:33:07 -0700109} drm_intel_bufmgr_gem;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700110
Jesse Barnesb5096402009-09-15 11:02:58 -0700111#define DRM_INTEL_RELOC_FENCE (1<<0)
112
113typedef struct _drm_intel_reloc_target_info {
114 drm_intel_bo *bo;
115 int flags;
116} drm_intel_reloc_target;
117
Eric Anholt4b982642008-10-30 09:33:07 -0700118struct _drm_intel_bo_gem {
Eric Anholtd70d6052009-10-06 12:40:42 -0700119 drm_intel_bo bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700120
Eric Anholtd70d6052009-10-06 12:40:42 -0700121 atomic_t refcount;
122 uint32_t gem_handle;
123 const char *name;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700124
Eric Anholtd70d6052009-10-06 12:40:42 -0700125 /**
126 * Kenel-assigned global name for this object
127 */
128 unsigned int global_name;
Chris Wilson36d49392011-02-14 09:39:06 +0000129 drmMMListHead name_list;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700130
Eric Anholtd70d6052009-10-06 12:40:42 -0700131 /**
132 * Index of the buffer within the validation list while preparing a
133 * batchbuffer execution.
134 */
135 int validate_index;
Keith Packard18f091d2008-12-15 15:08:12 -0800136
Eric Anholtd70d6052009-10-06 12:40:42 -0700137 /**
138 * Current tiling mode
139 */
140 uint32_t tiling_mode;
141 uint32_t swizzle_mode;
Chris Wilson056aa9b2010-06-21 14:31:29 +0100142 unsigned long stride;
Eric Anholt3f3c5be2009-07-09 17:49:46 -0700143
Eric Anholtd70d6052009-10-06 12:40:42 -0700144 time_t free_time;
Keith Packard329e0862008-06-05 16:05:35 -0700145
Eric Anholtd70d6052009-10-06 12:40:42 -0700146 /** Array passed to the DRM containing relocation information. */
147 struct drm_i915_gem_relocation_entry *relocs;
Jesse Barnesb5096402009-09-15 11:02:58 -0700148 /**
149 * Array of info structs corresponding to relocs[i].target_handle etc
150 */
151 drm_intel_reloc_target *reloc_target_info;
Eric Anholtd70d6052009-10-06 12:40:42 -0700152 /** Number of entries in relocs */
153 int reloc_count;
154 /** Mapped address for the buffer, saved across map/unmap cycles */
155 void *mem_virtual;
156 /** GTT virtual address for the buffer, saved across map/unmap cycles */
157 void *gtt_virtual;
Eric Anholt0e867312008-10-21 00:10:54 -0700158
Eric Anholtd70d6052009-10-06 12:40:42 -0700159 /** BO cache list */
160 drmMMListHead head;
Eric Anholt0e867312008-10-21 00:10:54 -0700161
Eric Anholtd70d6052009-10-06 12:40:42 -0700162 /**
163 * Boolean of whether this BO and its children have been included in
164 * the current drm_intel_bufmgr_check_aperture_space() total.
165 */
166 char included_in_check_aperture;
Eric Anholt0e867312008-10-21 00:10:54 -0700167
Eric Anholtd70d6052009-10-06 12:40:42 -0700168 /**
169 * Boolean of whether this buffer has been used as a relocation
170 * target and had its size accounted for, and thus can't have any
171 * further relocations added to it.
172 */
173 char used_as_reloc_target;
Keith Packard5b5ce302009-05-11 13:42:12 -0700174
Eric Anholtd70d6052009-10-06 12:40:42 -0700175 /**
Chris Wilson792fed12009-12-02 13:12:39 +0000176 * Boolean of whether we have encountered an error whilst building the relocation tree.
177 */
178 char has_error;
179
180 /**
Eric Anholtd70d6052009-10-06 12:40:42 -0700181 * Boolean of whether this buffer can be re-used
182 */
183 char reusable;
184
185 /**
186 * Size in bytes of this buffer and its relocation descendents.
187 *
188 * Used to avoid costly tree walking in
189 * drm_intel_bufmgr_check_aperture in the common case.
190 */
191 int reloc_tree_size;
192
193 /**
194 * Number of potential fence registers required by this buffer and its
195 * relocations.
196 */
197 int reloc_tree_fences;
Keith Packarda919ff52008-06-05 15:58:09 -0700198};
Eric Anholt6a9eb082008-06-03 09:27:37 -0700199
Keith Packardb13f4e12008-11-21 01:49:39 -0800200static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -0700201drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count);
Keith Packardb13f4e12008-11-21 01:49:39 -0800202
203static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -0700204drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count);
Keith Packardb13f4e12008-11-21 01:49:39 -0800205
Eric Anholt6a9eb082008-06-03 09:27:37 -0700206static int
Eric Anholtd70d6052009-10-06 12:40:42 -0700207drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
208 uint32_t * swizzle_mode);
Keith Packard18f091d2008-12-15 15:08:12 -0800209
210static int
Chris Wilson1db22ff2010-06-21 14:27:23 +0100211drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
212 uint32_t tiling_mode,
213 uint32_t stride);
Keith Packard18f091d2008-12-15 15:08:12 -0800214
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700215static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
216 time_t time);
Chris Wilson04495ee2009-10-02 04:39:22 +0100217
Eric Anholtd70d6052009-10-06 12:40:42 -0700218static void drm_intel_gem_bo_unreference(drm_intel_bo *bo);
Keith Packard18f091d2008-12-15 15:08:12 -0800219
Eric Anholtd70d6052009-10-06 12:40:42 -0700220static void drm_intel_gem_bo_free(drm_intel_bo *bo);
Chris Wilson0fb215a2009-10-02 04:31:34 +0100221
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700222static unsigned long
223drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size,
224 uint32_t *tiling_mode)
225{
226 unsigned long min_size, max_size;
227 unsigned long i;
228
229 if (*tiling_mode == I915_TILING_NONE)
230 return size;
231
232 /* 965+ just need multiples of page size for tiling */
Eric Anholta1f9ea72010-03-02 08:49:36 -0800233 if (bufmgr_gem->gen >= 4)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700234 return ROUND_UP_TO(size, 4096);
235
236 /* Older chips need powers of two, of at least 512k or 1M */
Eric Anholtacbaff22010-03-02 15:24:50 -0800237 if (bufmgr_gem->gen == 3) {
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700238 min_size = 1024*1024;
239 max_size = 128*1024*1024;
240 } else {
241 min_size = 512*1024;
242 max_size = 64*1024*1024;
243 }
244
245 if (size > max_size) {
246 *tiling_mode = I915_TILING_NONE;
247 return size;
248 }
249
Chris Wilson36245772010-10-29 10:49:54 +0100250 /* Do we need to allocate every page for the fence? */
251 if (bufmgr_gem->has_relaxed_fencing)
252 return ROUND_UP_TO(size, 4096);
253
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700254 for (i = min_size; i < size; i <<= 1)
255 ;
256
257 return i;
258}
259
260/*
261 * Round a given pitch up to the minimum required for X tiling on a
262 * given chip. We use 512 as the minimum to allow for a later tiling
263 * change.
264 */
265static unsigned long
266drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem,
Chris Wilson726210f2010-06-24 11:38:00 +0100267 unsigned long pitch, uint32_t *tiling_mode)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700268{
Eric Anholt1d4d1e62010-03-04 16:09:40 -0800269 unsigned long tile_width;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700270 unsigned long i;
271
Eric Anholt7c697b12010-03-17 10:05:55 -0700272 /* If untiled, then just align it so that we can do rendering
273 * to it with the 3D engine.
274 */
Chris Wilson726210f2010-06-24 11:38:00 +0100275 if (*tiling_mode == I915_TILING_NONE)
Eric Anholt7c697b12010-03-17 10:05:55 -0700276 return ALIGN(pitch, 64);
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700277
Chris Wilson726210f2010-06-24 11:38:00 +0100278 if (*tiling_mode == I915_TILING_X)
Eric Anholt1d4d1e62010-03-04 16:09:40 -0800279 tile_width = 512;
280 else
281 tile_width = 128;
282
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700283 /* 965 is flexible */
Eric Anholta1f9ea72010-03-02 08:49:36 -0800284 if (bufmgr_gem->gen >= 4)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700285 return ROUND_UP_TO(pitch, tile_width);
286
Chris Wilson726210f2010-06-24 11:38:00 +0100287 /* The older hardware has a maximum pitch of 8192 with tiled
288 * surfaces, so fallback to untiled if it's too large.
289 */
290 if (pitch > 8192) {
291 *tiling_mode = I915_TILING_NONE;
292 return ALIGN(pitch, 64);
293 }
294
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700295 /* Pre-965 needs power of two tile width */
296 for (i = tile_width; i < pitch; i <<= 1)
297 ;
298
299 return i;
300}
301
Eric Anholt4b982642008-10-30 09:33:07 -0700302static struct drm_intel_gem_bo_bucket *
303drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem,
304 unsigned long size)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700305{
Eric Anholtd70d6052009-10-06 12:40:42 -0700306 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700307
Eric Anholt0ec768e2010-06-04 17:09:11 -0700308 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
Eric Anholtd70d6052009-10-06 12:40:42 -0700309 struct drm_intel_gem_bo_bucket *bucket =
310 &bufmgr_gem->cache_bucket[i];
311 if (bucket->size >= size) {
312 return bucket;
313 }
Eric Anholt78fa5902009-07-06 11:55:28 -0700314 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700315
Eric Anholtd70d6052009-10-06 12:40:42 -0700316 return NULL;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700317}
318
Eric Anholtd70d6052009-10-06 12:40:42 -0700319static void
320drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700321{
Eric Anholtd70d6052009-10-06 12:40:42 -0700322 int i, j;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700323
Eric Anholtd70d6052009-10-06 12:40:42 -0700324 for (i = 0; i < bufmgr_gem->exec_count; i++) {
325 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
326 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700327
Eric Anholtd70d6052009-10-06 12:40:42 -0700328 if (bo_gem->relocs == NULL) {
329 DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle,
330 bo_gem->name);
331 continue;
332 }
333
334 for (j = 0; j < bo_gem->reloc_count; j++) {
Jesse Barnesb5096402009-09-15 11:02:58 -0700335 drm_intel_bo *target_bo = bo_gem->reloc_target_info[j].bo;
Eric Anholtd70d6052009-10-06 12:40:42 -0700336 drm_intel_bo_gem *target_gem =
337 (drm_intel_bo_gem *) target_bo;
338
339 DBG("%2d: %d (%s)@0x%08llx -> "
340 "%d (%s)@0x%08lx + 0x%08x\n",
341 i,
342 bo_gem->gem_handle, bo_gem->name,
343 (unsigned long long)bo_gem->relocs[j].offset,
344 target_gem->gem_handle,
345 target_gem->name,
346 target_bo->offset,
347 bo_gem->relocs[j].delta);
348 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700349 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700350}
351
Chris Wilson9fec2a82009-12-02 10:42:51 +0000352static inline void
Chris Wilson04495ee2009-10-02 04:39:22 +0100353drm_intel_gem_bo_reference(drm_intel_bo *bo)
354{
Eric Anholtd70d6052009-10-06 12:40:42 -0700355 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Chris Wilson04495ee2009-10-02 04:39:22 +0100356
Eric Anholtd70d6052009-10-06 12:40:42 -0700357 atomic_inc(&bo_gem->refcount);
Chris Wilson04495ee2009-10-02 04:39:22 +0100358}
359
Eric Anholt6a9eb082008-06-03 09:27:37 -0700360/**
361 * Adds the given buffer to the list of buffers to be validated (moved into the
362 * appropriate memory type) with the next batch submission.
363 *
364 * If a buffer is validated multiple times in a batch submission, it ends up
365 * with the intersection of the memory type flags and the union of the
366 * access flags.
367 */
368static void
Eric Anholt4b982642008-10-30 09:33:07 -0700369drm_intel_add_validate_buffer(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700370{
Eric Anholtd70d6052009-10-06 12:40:42 -0700371 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
372 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
373 int index;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700374
Eric Anholtd70d6052009-10-06 12:40:42 -0700375 if (bo_gem->validate_index != -1)
376 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700377
Eric Anholtd70d6052009-10-06 12:40:42 -0700378 /* Extend the array of validation entries as necessary. */
379 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
380 int new_size = bufmgr_gem->exec_size * 2;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700381
Eric Anholtd70d6052009-10-06 12:40:42 -0700382 if (new_size == 0)
383 new_size = 5;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700384
Eric Anholtd70d6052009-10-06 12:40:42 -0700385 bufmgr_gem->exec_objects =
386 realloc(bufmgr_gem->exec_objects,
387 sizeof(*bufmgr_gem->exec_objects) * new_size);
388 bufmgr_gem->exec_bos =
389 realloc(bufmgr_gem->exec_bos,
390 sizeof(*bufmgr_gem->exec_bos) * new_size);
391 bufmgr_gem->exec_size = new_size;
392 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700393
Eric Anholtd70d6052009-10-06 12:40:42 -0700394 index = bufmgr_gem->exec_count;
395 bo_gem->validate_index = index;
396 /* Fill in array entry */
397 bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
398 bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
399 bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs;
400 bufmgr_gem->exec_objects[index].alignment = 0;
401 bufmgr_gem->exec_objects[index].offset = 0;
402 bufmgr_gem->exec_bos[index] = bo;
Eric Anholtd70d6052009-10-06 12:40:42 -0700403 bufmgr_gem->exec_count++;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700404}
405
Jesse Barnesb5096402009-09-15 11:02:58 -0700406static void
407drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
408{
409 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
410 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
411 int index;
412
Eric Anholt47102862010-03-03 10:07:27 -0800413 if (bo_gem->validate_index != -1) {
414 if (need_fence)
415 bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |=
416 EXEC_OBJECT_NEEDS_FENCE;
Jesse Barnesb5096402009-09-15 11:02:58 -0700417 return;
Eric Anholt47102862010-03-03 10:07:27 -0800418 }
Jesse Barnesb5096402009-09-15 11:02:58 -0700419
420 /* Extend the array of validation entries as necessary. */
421 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
422 int new_size = bufmgr_gem->exec_size * 2;
423
424 if (new_size == 0)
425 new_size = 5;
426
427 bufmgr_gem->exec2_objects =
428 realloc(bufmgr_gem->exec2_objects,
429 sizeof(*bufmgr_gem->exec2_objects) * new_size);
430 bufmgr_gem->exec_bos =
431 realloc(bufmgr_gem->exec_bos,
432 sizeof(*bufmgr_gem->exec_bos) * new_size);
433 bufmgr_gem->exec_size = new_size;
434 }
435
436 index = bufmgr_gem->exec_count;
437 bo_gem->validate_index = index;
438 /* Fill in array entry */
439 bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
440 bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
441 bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
442 bufmgr_gem->exec2_objects[index].alignment = 0;
443 bufmgr_gem->exec2_objects[index].offset = 0;
444 bufmgr_gem->exec_bos[index] = bo;
445 bufmgr_gem->exec2_objects[index].flags = 0;
446 bufmgr_gem->exec2_objects[index].rsvd1 = 0;
447 bufmgr_gem->exec2_objects[index].rsvd2 = 0;
448 if (need_fence) {
449 bufmgr_gem->exec2_objects[index].flags |=
450 EXEC_OBJECT_NEEDS_FENCE;
451 }
452 bufmgr_gem->exec_count++;
453}
454
Eric Anholt6a9eb082008-06-03 09:27:37 -0700455#define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
456 sizeof(uint32_t))
457
Chris Wilsone22fb792009-11-30 22:14:30 +0000458static void
459drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem,
460 drm_intel_bo_gem *bo_gem)
461{
462 int size;
463
464 assert(!bo_gem->used_as_reloc_target);
465
466 /* The older chipsets are far-less flexible in terms of tiling,
467 * and require tiled buffer to be size aligned in the aperture.
468 * This means that in the worst possible case we will need a hole
469 * twice as large as the object in order for it to fit into the
470 * aperture. Optimal packing is for wimps.
471 */
472 size = bo_gem->bo.size;
Chris Wilson51b89502010-11-22 09:50:06 +0000473 if (bufmgr_gem->gen < 4 && bo_gem->tiling_mode != I915_TILING_NONE) {
474 int min_size;
475
476 if (bufmgr_gem->has_relaxed_fencing) {
477 if (bufmgr_gem->gen == 3)
478 min_size = 1024*1024;
479 else
480 min_size = 512*1024;
481
482 while (min_size < size)
483 min_size *= 2;
484 } else
485 min_size = size;
486
487 /* Account for worst-case alignment. */
488 size = 2 * min_size;
489 }
Chris Wilsone22fb792009-11-30 22:14:30 +0000490
491 bo_gem->reloc_tree_size = size;
492}
493
Eric Anholt6a9eb082008-06-03 09:27:37 -0700494static int
Eric Anholt4b982642008-10-30 09:33:07 -0700495drm_intel_setup_reloc_list(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700496{
Eric Anholtd70d6052009-10-06 12:40:42 -0700497 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
498 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
499 unsigned int max_relocs = bufmgr_gem->max_relocs;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700500
Eric Anholtd70d6052009-10-06 12:40:42 -0700501 if (bo->size / 4 < max_relocs)
502 max_relocs = bo->size / 4;
Eric Anholt3c9bd062009-10-05 16:35:32 -0700503
Eric Anholtd70d6052009-10-06 12:40:42 -0700504 bo_gem->relocs = malloc(max_relocs *
505 sizeof(struct drm_i915_gem_relocation_entry));
Jesse Barnesb5096402009-09-15 11:02:58 -0700506 bo_gem->reloc_target_info = malloc(max_relocs *
Chris Wilson35061732010-04-11 18:40:38 +0100507 sizeof(drm_intel_reloc_target));
Jesse Barnesb5096402009-09-15 11:02:58 -0700508 if (bo_gem->relocs == NULL || bo_gem->reloc_target_info == NULL) {
Chris Wilson792fed12009-12-02 13:12:39 +0000509 bo_gem->has_error = 1;
510
511 free (bo_gem->relocs);
512 bo_gem->relocs = NULL;
513
Jesse Barnesb5096402009-09-15 11:02:58 -0700514 free (bo_gem->reloc_target_info);
515 bo_gem->reloc_target_info = NULL;
Chris Wilson792fed12009-12-02 13:12:39 +0000516
517 return 1;
518 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700519
Eric Anholtd70d6052009-10-06 12:40:42 -0700520 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700521}
522
Eric Anholt8214a652009-08-27 18:32:07 -0700523static int
524drm_intel_gem_bo_busy(drm_intel_bo *bo)
525{
Eric Anholtd70d6052009-10-06 12:40:42 -0700526 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
527 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
528 struct drm_i915_gem_busy busy;
529 int ret;
Eric Anholt8214a652009-08-27 18:32:07 -0700530
Eric Anholtd70d6052009-10-06 12:40:42 -0700531 memset(&busy, 0, sizeof(busy));
532 busy.handle = bo_gem->gem_handle;
Eric Anholt8214a652009-08-27 18:32:07 -0700533
Chris Wilson62997222010-09-25 21:32:59 +0100534 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
Eric Anholt8214a652009-08-27 18:32:07 -0700535
Eric Anholtd70d6052009-10-06 12:40:42 -0700536 return (ret == 0 && busy.busy);
Eric Anholt8214a652009-08-27 18:32:07 -0700537}
538
Chris Wilson0fb215a2009-10-02 04:31:34 +0100539static int
Chris Wilson83a35b62009-11-11 13:04:38 +0000540drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem,
541 drm_intel_bo_gem *bo_gem, int state)
Chris Wilson0fb215a2009-10-02 04:31:34 +0100542{
Eric Anholtd70d6052009-10-06 12:40:42 -0700543 struct drm_i915_gem_madvise madv;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100544
Eric Anholtd70d6052009-10-06 12:40:42 -0700545 madv.handle = bo_gem->gem_handle;
546 madv.madv = state;
547 madv.retained = 1;
Chris Wilson62997222010-09-25 21:32:59 +0100548 drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
Chris Wilson0fb215a2009-10-02 04:31:34 +0100549
Eric Anholtd70d6052009-10-06 12:40:42 -0700550 return madv.retained;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100551}
552
Chris Wilson83a35b62009-11-11 13:04:38 +0000553static int
554drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv)
555{
556 return drm_intel_gem_bo_madvise_internal
557 ((drm_intel_bufmgr_gem *) bo->bufmgr,
558 (drm_intel_bo_gem *) bo,
559 madv);
560}
561
Chris Wilson0fb215a2009-10-02 04:31:34 +0100562/* drop the oldest entries that have been purged by the kernel */
563static void
564drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem,
565 struct drm_intel_gem_bo_bucket *bucket)
566{
Eric Anholtd70d6052009-10-06 12:40:42 -0700567 while (!DRMLISTEMPTY(&bucket->head)) {
568 drm_intel_bo_gem *bo_gem;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100569
Eric Anholtd70d6052009-10-06 12:40:42 -0700570 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
571 bucket->head.next, head);
Chris Wilson83a35b62009-11-11 13:04:38 +0000572 if (drm_intel_gem_bo_madvise_internal
Eric Anholtd70d6052009-10-06 12:40:42 -0700573 (bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
574 break;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100575
Eric Anholtd70d6052009-10-06 12:40:42 -0700576 DRMLISTDEL(&bo_gem->head);
577 drm_intel_gem_bo_free(&bo_gem->bo);
578 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100579}
580
Eric Anholt4b982642008-10-30 09:33:07 -0700581static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700582drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr,
583 const char *name,
584 unsigned long size,
Chris Wilson1db22ff2010-06-21 14:27:23 +0100585 unsigned long flags,
586 uint32_t tiling_mode,
587 unsigned long stride)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700588{
Eric Anholtd70d6052009-10-06 12:40:42 -0700589 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
590 drm_intel_bo_gem *bo_gem;
591 unsigned int page_size = getpagesize();
592 int ret;
593 struct drm_intel_gem_bo_bucket *bucket;
594 int alloc_from_cache;
595 unsigned long bo_size;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700596 int for_render = 0;
597
598 if (flags & BO_ALLOC_FOR_RENDER)
599 for_render = 1;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700600
Eric Anholtd70d6052009-10-06 12:40:42 -0700601 /* Round the allocated size up to a power of two number of pages. */
602 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700603
Eric Anholtd70d6052009-10-06 12:40:42 -0700604 /* If we don't have caching at this size, don't actually round the
605 * allocation up.
606 */
607 if (bucket == NULL) {
608 bo_size = size;
609 if (bo_size < page_size)
610 bo_size = page_size;
Eric Anholt72abe982009-02-18 13:06:35 -0800611 } else {
Eric Anholtd70d6052009-10-06 12:40:42 -0700612 bo_size = bucket->size;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700613 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100614
Eric Anholtd70d6052009-10-06 12:40:42 -0700615 pthread_mutex_lock(&bufmgr_gem->lock);
616 /* Get a buffer out of the cache if available */
617retry:
618 alloc_from_cache = 0;
619 if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) {
620 if (for_render) {
621 /* Allocate new render-target BOs from the tail (MRU)
622 * of the list, as it will likely be hot in the GPU
623 * cache and in the aperture for us.
624 */
625 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
626 bucket->head.prev, head);
627 DRMLISTDEL(&bo_gem->head);
628 alloc_from_cache = 1;
629 } else {
630 /* For non-render-target BOs (where we're probably
631 * going to map it first thing in order to fill it
632 * with data), check if the last BO in the cache is
633 * unbusy, and only reuse in that case. Otherwise,
634 * allocating a new buffer is probably faster than
635 * waiting for the GPU to finish.
636 */
637 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
638 bucket->head.next, head);
639 if (!drm_intel_gem_bo_busy(&bo_gem->bo)) {
640 alloc_from_cache = 1;
641 DRMLISTDEL(&bo_gem->head);
642 }
643 }
644
645 if (alloc_from_cache) {
Chris Wilson83a35b62009-11-11 13:04:38 +0000646 if (!drm_intel_gem_bo_madvise_internal
Eric Anholtd70d6052009-10-06 12:40:42 -0700647 (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) {
648 drm_intel_gem_bo_free(&bo_gem->bo);
649 drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem,
650 bucket);
651 goto retry;
652 }
Chris Wilson1db22ff2010-06-21 14:27:23 +0100653
654 if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
655 tiling_mode,
656 stride)) {
657 drm_intel_gem_bo_free(&bo_gem->bo);
658 goto retry;
659 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700660 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100661 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700662 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700663
Eric Anholtd70d6052009-10-06 12:40:42 -0700664 if (!alloc_from_cache) {
665 struct drm_i915_gem_create create;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700666
Eric Anholtd70d6052009-10-06 12:40:42 -0700667 bo_gem = calloc(1, sizeof(*bo_gem));
668 if (!bo_gem)
669 return NULL;
Keith Packarda919ff52008-06-05 15:58:09 -0700670
Eric Anholtd70d6052009-10-06 12:40:42 -0700671 bo_gem->bo.size = bo_size;
672 memset(&create, 0, sizeof(create));
673 create.size = bo_size;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700674
Chris Wilson62997222010-09-25 21:32:59 +0100675 ret = drmIoctl(bufmgr_gem->fd,
676 DRM_IOCTL_I915_GEM_CREATE,
677 &create);
Eric Anholtd70d6052009-10-06 12:40:42 -0700678 bo_gem->gem_handle = create.handle;
679 bo_gem->bo.handle = bo_gem->gem_handle;
680 if (ret != 0) {
681 free(bo_gem);
682 return NULL;
683 }
684 bo_gem->bo.bufmgr = bufmgr;
Chris Wilson1db22ff2010-06-21 14:27:23 +0100685
686 bo_gem->tiling_mode = I915_TILING_NONE;
687 bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
688 bo_gem->stride = 0;
689
690 if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
691 tiling_mode,
692 stride)) {
693 drm_intel_gem_bo_free(&bo_gem->bo);
694 return NULL;
695 }
Chris Wilson36d49392011-02-14 09:39:06 +0000696
697 DRMINITLISTHEAD(&bo_gem->name_list);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700698 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700699
Eric Anholtd70d6052009-10-06 12:40:42 -0700700 bo_gem->name = name;
701 atomic_set(&bo_gem->refcount, 1);
702 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -0700703 bo_gem->reloc_tree_fences = 0;
704 bo_gem->used_as_reloc_target = 0;
Chris Wilson792fed12009-12-02 13:12:39 +0000705 bo_gem->has_error = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -0700706 bo_gem->reusable = 1;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700707
Chris Wilsone22fb792009-11-30 22:14:30 +0000708 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
709
Eric Anholtd70d6052009-10-06 12:40:42 -0700710 DBG("bo_create: buf %d (%s) %ldb\n",
711 bo_gem->gem_handle, bo_gem->name, size);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700712
Eric Anholtd70d6052009-10-06 12:40:42 -0700713 return &bo_gem->bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700714}
715
Eric Anholt72abe982009-02-18 13:06:35 -0800716static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700717drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
718 const char *name,
719 unsigned long size,
720 unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -0800721{
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700722 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size,
Chris Wilson1db22ff2010-06-21 14:27:23 +0100723 BO_ALLOC_FOR_RENDER,
724 I915_TILING_NONE, 0);
Eric Anholt72abe982009-02-18 13:06:35 -0800725}
726
727static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700728drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr,
729 const char *name,
730 unsigned long size,
731 unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -0800732{
Chris Wilson1db22ff2010-06-21 14:27:23 +0100733 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0,
734 I915_TILING_NONE, 0);
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700735}
736
737static drm_intel_bo *
738drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
739 int x, int y, int cpp, uint32_t *tiling_mode,
740 unsigned long *pitch, unsigned long flags)
741{
742 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
Chris Wilsone65caeb2010-06-09 10:08:41 +0100743 unsigned long size, stride;
744 uint32_t tiling;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700745
Chris Wilsone65caeb2010-06-09 10:08:41 +0100746 do {
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100747 unsigned long aligned_y, height_alignment;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700748
Chris Wilsone65caeb2010-06-09 10:08:41 +0100749 tiling = *tiling_mode;
750
751 /* If we're tiled, our allocations are in 8 or 32-row blocks,
752 * so failure to align our height means that we won't allocate
753 * enough pages.
754 *
755 * If we're untiled, we still have to align to 2 rows high
756 * because the data port accesses 2x2 blocks even if the
757 * bottom row isn't to be rendered, so failure to align means
758 * we could walk off the end of the GTT and fault. This is
759 * documented on 965, and may be the case on older chipsets
760 * too so we try to be careful.
761 */
762 aligned_y = y;
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100763 height_alignment = 2;
764
765 if (tiling == I915_TILING_X)
766 height_alignment = 8;
Chris Wilsone65caeb2010-06-09 10:08:41 +0100767 else if (tiling == I915_TILING_Y)
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100768 height_alignment = 32;
769 /* i8xx has a interleaved 2-row tile layout */
Daniel Vettere6018c22011-02-22 19:11:07 +0100770 if (IS_GEN2(bufmgr_gem) && tiling != I915_TILING_NONE)
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100771 height_alignment *= 2;
772 aligned_y = ALIGN(y, height_alignment);
Chris Wilsone65caeb2010-06-09 10:08:41 +0100773
774 stride = x * cpp;
Chris Wilson726210f2010-06-24 11:38:00 +0100775 stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, tiling_mode);
Chris Wilsone65caeb2010-06-09 10:08:41 +0100776 size = stride * aligned_y;
777 size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode);
778 } while (*tiling_mode != tiling);
Chris Wilson6ea2bda2010-06-22 13:03:52 +0100779 *pitch = stride;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700780
Chris Wilson6ea2bda2010-06-22 13:03:52 +0100781 if (tiling == I915_TILING_NONE)
Chris Wilson5eec2862010-06-21 14:20:56 +0100782 stride = 0;
783
Chris Wilson6ea2bda2010-06-22 13:03:52 +0100784 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags,
785 tiling, stride);
Eric Anholt72abe982009-02-18 13:06:35 -0800786}
787
Eric Anholt6a9eb082008-06-03 09:27:37 -0700788/**
Eric Anholt4b982642008-10-30 09:33:07 -0700789 * Returns a drm_intel_bo wrapping the given buffer object handle.
Eric Anholt6a9eb082008-06-03 09:27:37 -0700790 *
791 * This can be used when one application needs to pass a buffer object
792 * to another.
793 */
Eric Anholt4b982642008-10-30 09:33:07 -0700794drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700795drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
796 const char *name,
Eric Anholt4b982642008-10-30 09:33:07 -0700797 unsigned int handle)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700798{
Eric Anholtd70d6052009-10-06 12:40:42 -0700799 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
800 drm_intel_bo_gem *bo_gem;
801 int ret;
802 struct drm_gem_open open_arg;
803 struct drm_i915_gem_get_tiling get_tiling;
Chris Wilson36d49392011-02-14 09:39:06 +0000804 drmMMListHead *list;
805
806 /* At the moment most applications only have a few named bo.
807 * For instance, in a DRI client only the render buffers passed
808 * between X and the client are named. And since X returns the
809 * alternating names for the front/back buffer a linear search
810 * provides a sufficiently fast match.
811 */
812 for (list = bufmgr_gem->named.next;
813 list != &bufmgr_gem->named;
814 list = list->next) {
815 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
816 if (bo_gem->global_name == handle) {
817 drm_intel_gem_bo_reference(&bo_gem->bo);
818 return &bo_gem->bo;
819 }
820 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700821
Eric Anholtd70d6052009-10-06 12:40:42 -0700822 bo_gem = calloc(1, sizeof(*bo_gem));
823 if (!bo_gem)
824 return NULL;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700825
Eric Anholtd70d6052009-10-06 12:40:42 -0700826 memset(&open_arg, 0, sizeof(open_arg));
827 open_arg.name = handle;
Chris Wilson62997222010-09-25 21:32:59 +0100828 ret = drmIoctl(bufmgr_gem->fd,
829 DRM_IOCTL_GEM_OPEN,
830 &open_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -0700831 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +0100832 DBG("Couldn't reference %s handle 0x%08x: %s\n",
833 name, handle, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -0700834 free(bo_gem);
835 return NULL;
836 }
837 bo_gem->bo.size = open_arg.size;
838 bo_gem->bo.offset = 0;
839 bo_gem->bo.virtual = NULL;
840 bo_gem->bo.bufmgr = bufmgr;
841 bo_gem->name = name;
842 atomic_set(&bo_gem->refcount, 1);
843 bo_gem->validate_index = -1;
844 bo_gem->gem_handle = open_arg.handle;
Chris Wilson53581b62011-02-14 09:27:05 +0000845 bo_gem->bo.handle = open_arg.handle;
Eric Anholtd70d6052009-10-06 12:40:42 -0700846 bo_gem->global_name = handle;
847 bo_gem->reusable = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700848
Eric Anholtd70d6052009-10-06 12:40:42 -0700849 memset(&get_tiling, 0, sizeof(get_tiling));
850 get_tiling.handle = bo_gem->gem_handle;
Chris Wilson62997222010-09-25 21:32:59 +0100851 ret = drmIoctl(bufmgr_gem->fd,
852 DRM_IOCTL_I915_GEM_GET_TILING,
853 &get_tiling);
Eric Anholtd70d6052009-10-06 12:40:42 -0700854 if (ret != 0) {
855 drm_intel_gem_bo_unreference(&bo_gem->bo);
856 return NULL;
857 }
858 bo_gem->tiling_mode = get_tiling.tiling_mode;
859 bo_gem->swizzle_mode = get_tiling.swizzle_mode;
Chris Wilson056aa9b2010-06-21 14:31:29 +0100860 /* XXX stride is unknown */
Chris Wilsone22fb792009-11-30 22:14:30 +0000861 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
Keith Packard18f091d2008-12-15 15:08:12 -0800862
Chris Wilson36d49392011-02-14 09:39:06 +0000863 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
Eric Anholtd70d6052009-10-06 12:40:42 -0700864 DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700865
Eric Anholtd70d6052009-10-06 12:40:42 -0700866 return &bo_gem->bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700867}
868
869static void
Eric Anholt4b982642008-10-30 09:33:07 -0700870drm_intel_gem_bo_free(drm_intel_bo *bo)
Eric Anholt500c81d2008-06-06 17:13:16 -0700871{
Eric Anholtd70d6052009-10-06 12:40:42 -0700872 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
873 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
874 struct drm_gem_close close;
875 int ret;
Eric Anholt500c81d2008-06-06 17:13:16 -0700876
Eric Anholtd70d6052009-10-06 12:40:42 -0700877 if (bo_gem->mem_virtual)
878 munmap(bo_gem->mem_virtual, bo_gem->bo.size);
879 if (bo_gem->gtt_virtual)
880 munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
Eric Anholt500c81d2008-06-06 17:13:16 -0700881
Eric Anholtd70d6052009-10-06 12:40:42 -0700882 /* Close this object */
883 memset(&close, 0, sizeof(close));
884 close.handle = bo_gem->gem_handle;
Chris Wilson62997222010-09-25 21:32:59 +0100885 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
Eric Anholtd70d6052009-10-06 12:40:42 -0700886 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +0100887 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
888 bo_gem->gem_handle, bo_gem->name, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -0700889 }
890 free(bo);
Eric Anholt500c81d2008-06-06 17:13:16 -0700891}
892
Eric Anholt3f3c5be2009-07-09 17:49:46 -0700893/** Frees all cached buffers significantly older than @time. */
894static void
895drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time)
896{
Chris Wilson04495ee2009-10-02 04:39:22 +0100897 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700898
Chris Wilsonf16b4162010-06-21 15:21:48 +0100899 if (bufmgr_gem->time == time)
900 return;
901
Eric Anholt0ec768e2010-06-04 17:09:11 -0700902 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
Eric Anholtd70d6052009-10-06 12:40:42 -0700903 struct drm_intel_gem_bo_bucket *bucket =
904 &bufmgr_gem->cache_bucket[i];
Chris Wilson04495ee2009-10-02 04:39:22 +0100905
Eric Anholtd70d6052009-10-06 12:40:42 -0700906 while (!DRMLISTEMPTY(&bucket->head)) {
907 drm_intel_bo_gem *bo_gem;
Chris Wilson04495ee2009-10-02 04:39:22 +0100908
Eric Anholtd70d6052009-10-06 12:40:42 -0700909 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
910 bucket->head.next, head);
911 if (time - bo_gem->free_time <= 1)
912 break;
Chris Wilson04495ee2009-10-02 04:39:22 +0100913
Eric Anholtd70d6052009-10-06 12:40:42 -0700914 DRMLISTDEL(&bo_gem->head);
Chris Wilson04495ee2009-10-02 04:39:22 +0100915
Eric Anholtd70d6052009-10-06 12:40:42 -0700916 drm_intel_gem_bo_free(&bo_gem->bo);
917 }
918 }
Chris Wilsonf16b4162010-06-21 15:21:48 +0100919
920 bufmgr_gem->time = time;
Chris Wilson04495ee2009-10-02 04:39:22 +0100921}
922
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700923static void
924drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time)
Chris Wilson04495ee2009-10-02 04:39:22 +0100925{
Eric Anholtd70d6052009-10-06 12:40:42 -0700926 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
927 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
928 struct drm_intel_gem_bo_bucket *bucket;
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700929 int i;
Chris Wilson04495ee2009-10-02 04:39:22 +0100930
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700931 /* Unreference all the target buffers */
932 for (i = 0; i < bo_gem->reloc_count; i++) {
Eric Anholt4f7704a2010-06-10 08:58:08 -0700933 if (bo_gem->reloc_target_info[i].bo != bo) {
934 drm_intel_gem_bo_unreference_locked_timed(bo_gem->
935 reloc_target_info[i].bo,
936 time);
937 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700938 }
Chris Wilsonb666f412009-11-30 23:07:19 +0000939 bo_gem->reloc_count = 0;
940 bo_gem->used_as_reloc_target = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -0700941
942 DBG("bo_unreference final: %d (%s)\n",
943 bo_gem->gem_handle, bo_gem->name);
944
Chris Wilson57473c72009-12-02 13:36:22 +0000945 /* release memory associated with this object */
Jesse Barnesb5096402009-09-15 11:02:58 -0700946 if (bo_gem->reloc_target_info) {
947 free(bo_gem->reloc_target_info);
948 bo_gem->reloc_target_info = NULL;
Chris Wilson57473c72009-12-02 13:36:22 +0000949 }
950 if (bo_gem->relocs) {
951 free(bo_gem->relocs);
952 bo_gem->relocs = NULL;
953 }
954
Chris Wilson36d49392011-02-14 09:39:06 +0000955 DRMLISTDEL(&bo_gem->name_list);
956
Eric Anholtd70d6052009-10-06 12:40:42 -0700957 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
958 /* Put the buffer into our internal cache for reuse if we can. */
Eric Anholtd70d6052009-10-06 12:40:42 -0700959 if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL &&
Chris Wilson60aa8032009-11-30 20:02:05 +0000960 drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem,
961 I915_MADV_DONTNEED)) {
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700962 bo_gem->free_time = time;
Eric Anholtd70d6052009-10-06 12:40:42 -0700963
964 bo_gem->name = NULL;
965 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -0700966
967 DRMLISTADDTAIL(&bo_gem->head, &bucket->head);
Eric Anholtd70d6052009-10-06 12:40:42 -0700968 } else {
969 drm_intel_gem_bo_free(bo);
970 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700971}
972
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700973static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
974 time_t time)
975{
976 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
977
978 assert(atomic_read(&bo_gem->refcount) > 0);
Eric Anholtd70d6052009-10-06 12:40:42 -0700979 if (atomic_dec_and_test(&bo_gem->refcount))
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700980 drm_intel_gem_bo_unreference_final(bo, time);
Eric Anholtd70d6052009-10-06 12:40:42 -0700981}
982
983static void drm_intel_gem_bo_unreference(drm_intel_bo *bo)
984{
985 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
986
987 assert(atomic_read(&bo_gem->refcount) > 0);
988 if (atomic_dec_and_test(&bo_gem->refcount)) {
989 drm_intel_bufmgr_gem *bufmgr_gem =
990 (drm_intel_bufmgr_gem *) bo->bufmgr;
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700991 struct timespec time;
992
993 clock_gettime(CLOCK_MONOTONIC, &time);
994
Eric Anholtd70d6052009-10-06 12:40:42 -0700995 pthread_mutex_lock(&bufmgr_gem->lock);
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700996 drm_intel_gem_bo_unreference_final(bo, time.tv_sec);
Chris Wilsonf16b4162010-06-21 15:21:48 +0100997 drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time.tv_sec);
Eric Anholtd70d6052009-10-06 12:40:42 -0700998 pthread_mutex_unlock(&bufmgr_gem->lock);
999 }
1000}
1001
1002static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
1003{
1004 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1005 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1006 struct drm_i915_gem_set_domain set_domain;
1007 int ret;
1008
Chris Wilsona3305b02010-05-13 08:24:28 +01001009 pthread_mutex_lock(&bufmgr_gem->lock);
1010
Eric Anholtd70d6052009-10-06 12:40:42 -07001011 /* Allow recursive mapping. Mesa may recursively map buffers with
1012 * nested display loops.
Carl Worthafd245d2009-04-29 14:43:55 -07001013 */
Eric Anholtd70d6052009-10-06 12:40:42 -07001014 if (!bo_gem->mem_virtual) {
1015 struct drm_i915_gem_mmap mmap_arg;
Carl Worthafd245d2009-04-29 14:43:55 -07001016
Eric Anholtd70d6052009-10-06 12:40:42 -07001017 DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name);
1018
1019 memset(&mmap_arg, 0, sizeof(mmap_arg));
1020 mmap_arg.handle = bo_gem->gem_handle;
1021 mmap_arg.offset = 0;
1022 mmap_arg.size = bo->size;
Chris Wilson62997222010-09-25 21:32:59 +01001023 ret = drmIoctl(bufmgr_gem->fd,
1024 DRM_IOCTL_I915_GEM_MMAP,
1025 &mmap_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001026 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001027 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001028 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1029 __FILE__, __LINE__, bo_gem->gem_handle,
1030 bo_gem->name, strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +01001031 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001032 return ret;
1033 }
1034 bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
1035 }
1036 DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1037 bo_gem->mem_virtual);
1038 bo->virtual = bo_gem->mem_virtual;
1039
1040 set_domain.handle = bo_gem->gem_handle;
1041 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
1042 if (write_enable)
1043 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
1044 else
1045 set_domain.write_domain = 0;
Chris Wilson62997222010-09-25 21:32:59 +01001046 ret = drmIoctl(bufmgr_gem->fd,
1047 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1048 &set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001049 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001050 DBG("%s:%d: Error setting to CPU domain %d: %s\n",
1051 __FILE__, __LINE__, bo_gem->gem_handle,
1052 strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001053 }
1054
Chris Wilsona3305b02010-05-13 08:24:28 +01001055 pthread_mutex_unlock(&bufmgr_gem->lock);
1056
Eric Anholtd70d6052009-10-06 12:40:42 -07001057 return 0;
1058}
1059
1060int drm_intel_gem_bo_map_gtt(drm_intel_bo *bo)
1061{
1062 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1063 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1064 struct drm_i915_gem_set_domain set_domain;
1065 int ret;
1066
Chris Wilsona3305b02010-05-13 08:24:28 +01001067 pthread_mutex_lock(&bufmgr_gem->lock);
1068
Eric Anholtd70d6052009-10-06 12:40:42 -07001069 /* Get a mapping of the buffer if we haven't before. */
1070 if (bo_gem->gtt_virtual == NULL) {
1071 struct drm_i915_gem_mmap_gtt mmap_arg;
1072
1073 DBG("bo_map_gtt: mmap %d (%s)\n", bo_gem->gem_handle,
1074 bo_gem->name);
1075
1076 memset(&mmap_arg, 0, sizeof(mmap_arg));
1077 mmap_arg.handle = bo_gem->gem_handle;
1078
1079 /* Get the fake offset back... */
Chris Wilson62997222010-09-25 21:32:59 +01001080 ret = drmIoctl(bufmgr_gem->fd,
1081 DRM_IOCTL_I915_GEM_MMAP_GTT,
1082 &mmap_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001083 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001084 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001085 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
1086 __FILE__, __LINE__,
1087 bo_gem->gem_handle, bo_gem->name,
1088 strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +01001089 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001090 return ret;
1091 }
1092
1093 /* and mmap it */
1094 bo_gem->gtt_virtual = mmap(0, bo->size, PROT_READ | PROT_WRITE,
1095 MAP_SHARED, bufmgr_gem->fd,
1096 mmap_arg.offset);
1097 if (bo_gem->gtt_virtual == MAP_FAILED) {
Chris Wilson08371bc2009-12-08 22:35:24 +00001098 bo_gem->gtt_virtual = NULL;
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001099 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001100 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1101 __FILE__, __LINE__,
1102 bo_gem->gem_handle, bo_gem->name,
1103 strerror(errno));
Chris Wilsona3305b02010-05-13 08:24:28 +01001104 pthread_mutex_unlock(&bufmgr_gem->lock);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001105 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001106 }
1107 }
1108
1109 bo->virtual = bo_gem->gtt_virtual;
1110
1111 DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1112 bo_gem->gtt_virtual);
1113
1114 /* Now move it to the GTT domain so that the CPU caches are flushed */
1115 set_domain.handle = bo_gem->gem_handle;
1116 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1117 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilson62997222010-09-25 21:32:59 +01001118 ret = drmIoctl(bufmgr_gem->fd,
1119 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1120 &set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001121 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001122 DBG("%s:%d: Error setting domain %d: %s\n",
1123 __FILE__, __LINE__, bo_gem->gem_handle,
1124 strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001125 }
1126
Chris Wilsona3305b02010-05-13 08:24:28 +01001127 pthread_mutex_unlock(&bufmgr_gem->lock);
1128
Chris Wilsonc3ddfea2010-06-29 20:12:44 +01001129 return 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07001130}
1131
1132int drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo)
1133{
1134 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
Eric Anholtd70d6052009-10-06 12:40:42 -07001135 int ret = 0;
1136
1137 if (bo == NULL)
1138 return 0;
1139
Chris Wilsona3305b02010-05-13 08:24:28 +01001140 pthread_mutex_lock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001141 bo->virtual = NULL;
Chris Wilsona3305b02010-05-13 08:24:28 +01001142 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001143
1144 return ret;
1145}
1146
1147static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
1148{
1149 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1150 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1151 struct drm_i915_gem_sw_finish sw_finish;
1152 int ret;
1153
1154 if (bo == NULL)
1155 return 0;
1156
Chris Wilsona3305b02010-05-13 08:24:28 +01001157 pthread_mutex_lock(&bufmgr_gem->lock);
1158
Eric Anholtd70d6052009-10-06 12:40:42 -07001159 /* Cause a flush to happen if the buffer's pinned for scanout, so the
1160 * results show up in a timely manner.
1161 */
1162 sw_finish.handle = bo_gem->gem_handle;
Chris Wilson62997222010-09-25 21:32:59 +01001163 ret = drmIoctl(bufmgr_gem->fd,
1164 DRM_IOCTL_I915_GEM_SW_FINISH,
1165 &sw_finish);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001166 ret = ret == -1 ? -errno : 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07001167
1168 bo->virtual = NULL;
Chris Wilsona3305b02010-05-13 08:24:28 +01001169 pthread_mutex_unlock(&bufmgr_gem->lock);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001170
1171 return ret;
Carl Worthafd245d2009-04-29 14:43:55 -07001172}
1173
Eric Anholt6a9eb082008-06-03 09:27:37 -07001174static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001175drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
1176 unsigned long size, const void *data)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001177{
Eric Anholtd70d6052009-10-06 12:40:42 -07001178 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1179 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1180 struct drm_i915_gem_pwrite pwrite;
1181 int ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001182
Eric Anholtd70d6052009-10-06 12:40:42 -07001183 memset(&pwrite, 0, sizeof(pwrite));
1184 pwrite.handle = bo_gem->gem_handle;
1185 pwrite.offset = offset;
1186 pwrite.size = size;
1187 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
Chris Wilson62997222010-09-25 21:32:59 +01001188 ret = drmIoctl(bufmgr_gem->fd,
1189 DRM_IOCTL_I915_GEM_PWRITE,
1190 &pwrite);
Eric Anholtd70d6052009-10-06 12:40:42 -07001191 if (ret != 0) {
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001192 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001193 DBG("%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
1194 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1195 (int)size, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001196 }
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001197
1198 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001199}
1200
1201static int
1202drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
1203{
1204 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1205 struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id;
1206 int ret;
1207
1208 get_pipe_from_crtc_id.crtc_id = crtc_id;
Chris Wilson62997222010-09-25 21:32:59 +01001209 ret = drmIoctl(bufmgr_gem->fd,
1210 DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
1211 &get_pipe_from_crtc_id);
Eric Anholtd70d6052009-10-06 12:40:42 -07001212 if (ret != 0) {
1213 /* We return -1 here to signal that we don't
1214 * know which pipe is associated with this crtc.
1215 * This lets the caller know that this information
1216 * isn't available; using the wrong pipe for
1217 * vblank waiting can cause the chipset to lock up
1218 */
1219 return -1;
1220 }
1221
1222 return get_pipe_from_crtc_id.pipe;
1223}
1224
1225static int
1226drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
1227 unsigned long size, void *data)
1228{
1229 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1230 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1231 struct drm_i915_gem_pread pread;
1232 int ret;
1233
1234 memset(&pread, 0, sizeof(pread));
1235 pread.handle = bo_gem->gem_handle;
1236 pread.offset = offset;
1237 pread.size = size;
1238 pread.data_ptr = (uint64_t) (uintptr_t) data;
Chris Wilson62997222010-09-25 21:32:59 +01001239 ret = drmIoctl(bufmgr_gem->fd,
1240 DRM_IOCTL_I915_GEM_PREAD,
1241 &pread);
Eric Anholtd70d6052009-10-06 12:40:42 -07001242 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001243 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001244 DBG("%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
1245 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1246 (int)size, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001247 }
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001248
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001249 return ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001250}
1251
Eric Anholt877b2ce2010-11-09 13:51:45 -08001252/** Waits for all GPU rendering with the object to have completed. */
Eric Anholt6a9eb082008-06-03 09:27:37 -07001253static void
Eric Anholt4b982642008-10-30 09:33:07 -07001254drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001255{
Eric Anholt877b2ce2010-11-09 13:51:45 -08001256 drm_intel_gem_bo_start_gtt_access(bo, 1);
Eric Anholt6fb1ad72008-11-13 11:44:22 -08001257}
1258
1259/**
1260 * Sets the object to the GTT read and possibly write domain, used by the X
1261 * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
1262 *
1263 * In combination with drm_intel_gem_bo_pin() and manual fence management, we
1264 * can do tiled pixmaps this way.
1265 */
1266void
1267drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable)
1268{
Eric Anholtd70d6052009-10-06 12:40:42 -07001269 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1270 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1271 struct drm_i915_gem_set_domain set_domain;
1272 int ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001273
Eric Anholtd70d6052009-10-06 12:40:42 -07001274 set_domain.handle = bo_gem->gem_handle;
1275 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1276 set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0;
Chris Wilson62997222010-09-25 21:32:59 +01001277 ret = drmIoctl(bufmgr_gem->fd,
1278 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1279 &set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001280 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001281 DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
1282 __FILE__, __LINE__, bo_gem->gem_handle,
1283 set_domain.read_domains, set_domain.write_domain,
1284 strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001285 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001286}
1287
1288static void
Eric Anholt4b982642008-10-30 09:33:07 -07001289drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001290{
Eric Anholtd70d6052009-10-06 12:40:42 -07001291 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1292 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001293
Jesse Barnesb5096402009-09-15 11:02:58 -07001294 free(bufmgr_gem->exec2_objects);
Eric Anholtd70d6052009-10-06 12:40:42 -07001295 free(bufmgr_gem->exec_objects);
1296 free(bufmgr_gem->exec_bos);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001297
Eric Anholtd70d6052009-10-06 12:40:42 -07001298 pthread_mutex_destroy(&bufmgr_gem->lock);
Eric Anholt6df7b072008-06-12 23:22:26 -07001299
Eric Anholtd70d6052009-10-06 12:40:42 -07001300 /* Free any cached buffer objects we were going to reuse */
Eric Anholt0ec768e2010-06-04 17:09:11 -07001301 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
Eric Anholtd70d6052009-10-06 12:40:42 -07001302 struct drm_intel_gem_bo_bucket *bucket =
1303 &bufmgr_gem->cache_bucket[i];
1304 drm_intel_bo_gem *bo_gem;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001305
Eric Anholtd70d6052009-10-06 12:40:42 -07001306 while (!DRMLISTEMPTY(&bucket->head)) {
1307 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1308 bucket->head.next, head);
1309 DRMLISTDEL(&bo_gem->head);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001310
Eric Anholtd70d6052009-10-06 12:40:42 -07001311 drm_intel_gem_bo_free(&bo_gem->bo);
1312 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001313 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001314
Eric Anholtd70d6052009-10-06 12:40:42 -07001315 free(bufmgr);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001316}
1317
1318/**
1319 * Adds the target buffer to the validation list and adds the relocation
1320 * to the reloc_buffer's relocation list.
1321 *
1322 * The relocation entry at the given offset must already contain the
1323 * precomputed relocation value, because the kernel will optimize out
1324 * the relocation entry write when the buffer hasn't moved from the
1325 * last known offset in target_bo.
1326 */
1327static int
Jesse Barnesb5096402009-09-15 11:02:58 -07001328do_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1329 drm_intel_bo *target_bo, uint32_t target_offset,
1330 uint32_t read_domains, uint32_t write_domain,
1331 int need_fence)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001332{
Eric Anholtd70d6052009-10-06 12:40:42 -07001333 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1334 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1335 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
Chris Wilson537703f2010-12-07 20:34:22 +00001336 int fenced_command;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001337
Chris Wilson97077332009-12-01 23:01:34 +00001338 if (bo_gem->has_error)
Chris Wilson792fed12009-12-02 13:12:39 +00001339 return -ENOMEM;
Chris Wilson792fed12009-12-02 13:12:39 +00001340
1341 if (target_bo_gem->has_error) {
1342 bo_gem->has_error = 1;
Chris Wilson792fed12009-12-02 13:12:39 +00001343 return -ENOMEM;
1344 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001345
Jesse Barnesb5096402009-09-15 11:02:58 -07001346 /* We never use HW fences for rendering on 965+ */
Eric Anholta1f9ea72010-03-02 08:49:36 -08001347 if (bufmgr_gem->gen >= 4)
Jesse Barnesb5096402009-09-15 11:02:58 -07001348 need_fence = 0;
1349
Chris Wilson537703f2010-12-07 20:34:22 +00001350 fenced_command = need_fence;
1351 if (target_bo_gem->tiling_mode == I915_TILING_NONE)
1352 need_fence = 0;
1353
Eric Anholtd70d6052009-10-06 12:40:42 -07001354 /* Create a new relocation list if needed */
Chris Wilson97077332009-12-01 23:01:34 +00001355 if (bo_gem->relocs == NULL && drm_intel_setup_reloc_list(bo))
Chris Wilson792fed12009-12-02 13:12:39 +00001356 return -ENOMEM;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001357
Eric Anholtd70d6052009-10-06 12:40:42 -07001358 /* Check overflow */
1359 assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001360
Eric Anholtd70d6052009-10-06 12:40:42 -07001361 /* Check args */
1362 assert(offset <= bo->size - 4);
1363 assert((write_domain & (write_domain - 1)) == 0);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001364
Eric Anholtd70d6052009-10-06 12:40:42 -07001365 /* Make sure that we're not adding a reloc to something whose size has
1366 * already been accounted for.
1367 */
1368 assert(!bo_gem->used_as_reloc_target);
Eric Anholtf1791372010-06-07 14:22:36 -07001369 if (target_bo_gem != bo_gem) {
1370 target_bo_gem->used_as_reloc_target = 1;
1371 bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size;
1372 }
Eric Anholta1f9ea72010-03-02 08:49:36 -08001373 /* An object needing a fence is a tiled buffer, so it won't have
Jesse Barnesb5096402009-09-15 11:02:58 -07001374 * relocs to other buffers.
1375 */
1376 if (need_fence)
1377 target_bo_gem->reloc_tree_fences = 1;
Eric Anholtd70d6052009-10-06 12:40:42 -07001378 bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences;
Eric Anholt0e867312008-10-21 00:10:54 -07001379
Eric Anholtd70d6052009-10-06 12:40:42 -07001380 bo_gem->relocs[bo_gem->reloc_count].offset = offset;
1381 bo_gem->relocs[bo_gem->reloc_count].delta = target_offset;
1382 bo_gem->relocs[bo_gem->reloc_count].target_handle =
1383 target_bo_gem->gem_handle;
1384 bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
1385 bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
1386 bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001387
Jesse Barnesb5096402009-09-15 11:02:58 -07001388 bo_gem->reloc_target_info[bo_gem->reloc_count].bo = target_bo;
Eric Anholt4f7704a2010-06-10 08:58:08 -07001389 if (target_bo != bo)
1390 drm_intel_gem_bo_reference(target_bo);
Chris Wilsonaf3d2822010-12-03 10:48:12 +00001391 if (fenced_command)
Jesse Barnesb5096402009-09-15 11:02:58 -07001392 bo_gem->reloc_target_info[bo_gem->reloc_count].flags =
1393 DRM_INTEL_RELOC_FENCE;
1394 else
1395 bo_gem->reloc_target_info[bo_gem->reloc_count].flags = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001396
Eric Anholtd70d6052009-10-06 12:40:42 -07001397 bo_gem->reloc_count++;
Eric Anholt6df7b072008-06-12 23:22:26 -07001398
Eric Anholtd70d6052009-10-06 12:40:42 -07001399 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001400}
1401
Jesse Barnesb5096402009-09-15 11:02:58 -07001402static int
1403drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1404 drm_intel_bo *target_bo, uint32_t target_offset,
1405 uint32_t read_domains, uint32_t write_domain)
1406{
1407 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
1408
1409 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1410 read_domains, write_domain,
1411 !bufmgr_gem->fenced_relocs);
1412}
1413
1414static int
1415drm_intel_gem_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
1416 drm_intel_bo *target_bo,
1417 uint32_t target_offset,
1418 uint32_t read_domains, uint32_t write_domain)
1419{
1420 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1421 read_domains, write_domain, 1);
1422}
1423
Eric Anholt6a9eb082008-06-03 09:27:37 -07001424/**
1425 * Walk the tree of relocations rooted at BO and accumulate the list of
1426 * validations to be performed and update the relocation buffers with
1427 * index values into the validation list.
1428 */
1429static void
Eric Anholt4b982642008-10-30 09:33:07 -07001430drm_intel_gem_bo_process_reloc(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001431{
Eric Anholtd70d6052009-10-06 12:40:42 -07001432 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1433 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001434
Eric Anholtd70d6052009-10-06 12:40:42 -07001435 if (bo_gem->relocs == NULL)
1436 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001437
Eric Anholtd70d6052009-10-06 12:40:42 -07001438 for (i = 0; i < bo_gem->reloc_count; i++) {
Jesse Barnesb5096402009-09-15 11:02:58 -07001439 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001440
Eric Anholtf1791372010-06-07 14:22:36 -07001441 if (target_bo == bo)
1442 continue;
1443
Eric Anholtd70d6052009-10-06 12:40:42 -07001444 /* Continue walking the tree depth-first. */
1445 drm_intel_gem_bo_process_reloc(target_bo);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001446
Eric Anholtd70d6052009-10-06 12:40:42 -07001447 /* Add the target to the validate list */
1448 drm_intel_add_validate_buffer(target_bo);
1449 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001450}
1451
Eric Anholt6a9eb082008-06-03 09:27:37 -07001452static void
Jesse Barnesb5096402009-09-15 11:02:58 -07001453drm_intel_gem_bo_process_reloc2(drm_intel_bo *bo)
1454{
1455 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
1456 int i;
1457
1458 if (bo_gem->relocs == NULL)
1459 return;
1460
1461 for (i = 0; i < bo_gem->reloc_count; i++) {
1462 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
1463 int need_fence;
1464
Eric Anholtf1791372010-06-07 14:22:36 -07001465 if (target_bo == bo)
1466 continue;
1467
Jesse Barnesb5096402009-09-15 11:02:58 -07001468 /* Continue walking the tree depth-first. */
1469 drm_intel_gem_bo_process_reloc2(target_bo);
1470
1471 need_fence = (bo_gem->reloc_target_info[i].flags &
1472 DRM_INTEL_RELOC_FENCE);
1473
1474 /* Add the target to the validate list */
1475 drm_intel_add_validate_buffer2(target_bo, need_fence);
1476 }
1477}
1478
1479
1480static void
Eric Anholtd70d6052009-10-06 12:40:42 -07001481drm_intel_update_buffer_offsets(drm_intel_bufmgr_gem *bufmgr_gem)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001482{
Eric Anholtd70d6052009-10-06 12:40:42 -07001483 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001484
Eric Anholtd70d6052009-10-06 12:40:42 -07001485 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1486 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1487 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001488
Eric Anholtd70d6052009-10-06 12:40:42 -07001489 /* Update the buffer offset */
1490 if (bufmgr_gem->exec_objects[i].offset != bo->offset) {
1491 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
1492 bo_gem->gem_handle, bo_gem->name, bo->offset,
1493 (unsigned long long)bufmgr_gem->exec_objects[i].
1494 offset);
1495 bo->offset = bufmgr_gem->exec_objects[i].offset;
1496 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001497 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001498}
1499
Jesse Barnesb5096402009-09-15 11:02:58 -07001500static void
1501drm_intel_update_buffer_offsets2 (drm_intel_bufmgr_gem *bufmgr_gem)
1502{
1503 int i;
1504
1505 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1506 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1507 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
1508
1509 /* Update the buffer offset */
1510 if (bufmgr_gem->exec2_objects[i].offset != bo->offset) {
1511 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
1512 bo_gem->gem_handle, bo_gem->name, bo->offset,
1513 (unsigned long long)bufmgr_gem->exec2_objects[i].offset);
1514 bo->offset = bufmgr_gem->exec2_objects[i].offset;
1515 }
1516 }
1517}
1518
Eric Anholtf9d98be2008-09-08 08:51:40 -07001519static int
Eric Anholt4b982642008-10-30 09:33:07 -07001520drm_intel_gem_bo_exec(drm_intel_bo *bo, int used,
Eric Anholtd70d6052009-10-06 12:40:42 -07001521 drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001522{
Eric Anholtd70d6052009-10-06 12:40:42 -07001523 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
Chris Wilson792fed12009-12-02 13:12:39 +00001524 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholtd70d6052009-10-06 12:40:42 -07001525 struct drm_i915_gem_execbuffer execbuf;
1526 int ret, i;
Eric Anholtf9d98be2008-09-08 08:51:40 -07001527
Chris Wilson792fed12009-12-02 13:12:39 +00001528 if (bo_gem->has_error)
1529 return -ENOMEM;
1530
Eric Anholtd70d6052009-10-06 12:40:42 -07001531 pthread_mutex_lock(&bufmgr_gem->lock);
1532 /* Update indices and set up the validate list. */
1533 drm_intel_gem_bo_process_reloc(bo);
Eric Anholtf9d98be2008-09-08 08:51:40 -07001534
Eric Anholtd70d6052009-10-06 12:40:42 -07001535 /* Add the batch buffer to the validation list. There are no
1536 * relocations pointing to it.
1537 */
1538 drm_intel_add_validate_buffer(bo);
Eric Anholtf9d98be2008-09-08 08:51:40 -07001539
Eric Anholtd70d6052009-10-06 12:40:42 -07001540 execbuf.buffers_ptr = (uintptr_t) bufmgr_gem->exec_objects;
1541 execbuf.buffer_count = bufmgr_gem->exec_count;
1542 execbuf.batch_start_offset = 0;
1543 execbuf.batch_len = used;
1544 execbuf.cliprects_ptr = (uintptr_t) cliprects;
1545 execbuf.num_cliprects = num_cliprects;
1546 execbuf.DR1 = 0;
1547 execbuf.DR4 = DR4;
Eric Anholtf9d98be2008-09-08 08:51:40 -07001548
Chris Wilson62997222010-09-25 21:32:59 +01001549 ret = drmIoctl(bufmgr_gem->fd,
1550 DRM_IOCTL_I915_GEM_EXECBUFFER,
1551 &execbuf);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001552 if (ret != 0) {
1553 ret = -errno;
1554 if (errno == ENOSPC) {
Chris Wilson96214862010-10-01 16:50:09 +01001555 DBG("Execbuffer fails to pin. "
1556 "Estimate: %u. Actual: %u. Available: %u\n",
1557 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
1558 bufmgr_gem->
1559 exec_count),
1560 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
1561 bufmgr_gem->
1562 exec_count),
1563 (unsigned int)bufmgr_gem->gtt_size);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001564 }
Eric Anholtd70d6052009-10-06 12:40:42 -07001565 }
1566 drm_intel_update_buffer_offsets(bufmgr_gem);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001567
Eric Anholtd70d6052009-10-06 12:40:42 -07001568 if (bufmgr_gem->bufmgr.debug)
1569 drm_intel_gem_dump_validation_list(bufmgr_gem);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001570
Eric Anholtd70d6052009-10-06 12:40:42 -07001571 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1572 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1573 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001574
Eric Anholtd70d6052009-10-06 12:40:42 -07001575 /* Disconnect the buffer from the validate list */
1576 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -07001577 bufmgr_gem->exec_bos[i] = NULL;
1578 }
1579 bufmgr_gem->exec_count = 0;
1580 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtf9d98be2008-09-08 08:51:40 -07001581
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001582 return ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001583}
1584
Keith Packard8e41ce12008-08-04 00:34:08 -07001585static int
Zou Nan hai66375fd2010-06-02 10:07:37 +08001586drm_intel_gem_bo_mrb_exec2(drm_intel_bo *bo, int used,
1587 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
Chris Wilson0184bb12010-12-19 13:01:15 +00001588 unsigned int flags)
Jesse Barnesb5096402009-09-15 11:02:58 -07001589{
1590 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
1591 struct drm_i915_gem_execbuffer2 execbuf;
1592 int ret, i;
1593
Chris Wilson0184bb12010-12-19 13:01:15 +00001594 switch (flags & 0x7) {
Chris Wilson057fab32010-10-26 11:35:11 +01001595 default:
Zou Nan hai66375fd2010-06-02 10:07:37 +08001596 return -EINVAL;
Chris Wilson057fab32010-10-26 11:35:11 +01001597 case I915_EXEC_BLT:
1598 if (!bufmgr_gem->has_blt)
1599 return -EINVAL;
1600 break;
1601 case I915_EXEC_BSD:
1602 if (!bufmgr_gem->has_bsd)
1603 return -EINVAL;
1604 break;
1605 case I915_EXEC_RENDER:
1606 case I915_EXEC_DEFAULT:
1607 break;
1608 }
Zou Nan hai66375fd2010-06-02 10:07:37 +08001609
Jesse Barnesb5096402009-09-15 11:02:58 -07001610 pthread_mutex_lock(&bufmgr_gem->lock);
1611 /* Update indices and set up the validate list. */
1612 drm_intel_gem_bo_process_reloc2(bo);
1613
1614 /* Add the batch buffer to the validation list. There are no relocations
1615 * pointing to it.
1616 */
1617 drm_intel_add_validate_buffer2(bo, 0);
1618
1619 execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec2_objects;
1620 execbuf.buffer_count = bufmgr_gem->exec_count;
1621 execbuf.batch_start_offset = 0;
1622 execbuf.batch_len = used;
1623 execbuf.cliprects_ptr = (uintptr_t)cliprects;
1624 execbuf.num_cliprects = num_cliprects;
1625 execbuf.DR1 = 0;
1626 execbuf.DR4 = DR4;
Chris Wilson0184bb12010-12-19 13:01:15 +00001627 execbuf.flags = flags;
Jesse Barnesb5096402009-09-15 11:02:58 -07001628 execbuf.rsvd1 = 0;
1629 execbuf.rsvd2 = 0;
1630
Chris Wilson62997222010-09-25 21:32:59 +01001631 ret = drmIoctl(bufmgr_gem->fd,
1632 DRM_IOCTL_I915_GEM_EXECBUFFER2,
1633 &execbuf);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001634 if (ret != 0) {
1635 ret = -errno;
Chris Wilson13e82702010-06-21 15:38:06 +01001636 if (ret == -ENOSPC) {
Chris Wilson96214862010-10-01 16:50:09 +01001637 DBG("Execbuffer fails to pin. "
1638 "Estimate: %u. Actual: %u. Available: %u\n",
1639 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
1640 bufmgr_gem->exec_count),
1641 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
1642 bufmgr_gem->exec_count),
1643 (unsigned int) bufmgr_gem->gtt_size);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001644 }
Jesse Barnesb5096402009-09-15 11:02:58 -07001645 }
1646 drm_intel_update_buffer_offsets2(bufmgr_gem);
1647
1648 if (bufmgr_gem->bufmgr.debug)
1649 drm_intel_gem_dump_validation_list(bufmgr_gem);
1650
1651 for (i = 0; i < bufmgr_gem->exec_count; i++) {
1652 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
1653 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
1654
1655 /* Disconnect the buffer from the validate list */
1656 bo_gem->validate_index = -1;
1657 bufmgr_gem->exec_bos[i] = NULL;
1658 }
1659 bufmgr_gem->exec_count = 0;
1660 pthread_mutex_unlock(&bufmgr_gem->lock);
1661
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001662 return ret;
Jesse Barnesb5096402009-09-15 11:02:58 -07001663}
1664
1665static int
Zou Nan hai66375fd2010-06-02 10:07:37 +08001666drm_intel_gem_bo_exec2(drm_intel_bo *bo, int used,
1667 drm_clip_rect_t *cliprects, int num_cliprects,
1668 int DR4)
1669{
1670 return drm_intel_gem_bo_mrb_exec2(bo, used,
1671 cliprects, num_cliprects, DR4,
1672 I915_EXEC_RENDER);
1673}
1674
1675static int
Eric Anholt4b982642008-10-30 09:33:07 -07001676drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment)
Keith Packard8e41ce12008-08-04 00:34:08 -07001677{
Eric Anholtd70d6052009-10-06 12:40:42 -07001678 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1679 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1680 struct drm_i915_gem_pin pin;
1681 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001682
Eric Anholtd70d6052009-10-06 12:40:42 -07001683 memset(&pin, 0, sizeof(pin));
1684 pin.handle = bo_gem->gem_handle;
1685 pin.alignment = alignment;
Keith Packard8e41ce12008-08-04 00:34:08 -07001686
Chris Wilson62997222010-09-25 21:32:59 +01001687 ret = drmIoctl(bufmgr_gem->fd,
1688 DRM_IOCTL_I915_GEM_PIN,
1689 &pin);
Eric Anholtd70d6052009-10-06 12:40:42 -07001690 if (ret != 0)
1691 return -errno;
Keith Packard8e41ce12008-08-04 00:34:08 -07001692
Eric Anholtd70d6052009-10-06 12:40:42 -07001693 bo->offset = pin.offset;
1694 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07001695}
1696
1697static int
Eric Anholt4b982642008-10-30 09:33:07 -07001698drm_intel_gem_bo_unpin(drm_intel_bo *bo)
Keith Packard8e41ce12008-08-04 00:34:08 -07001699{
Eric Anholtd70d6052009-10-06 12:40:42 -07001700 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1701 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1702 struct drm_i915_gem_unpin unpin;
1703 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001704
Eric Anholtd70d6052009-10-06 12:40:42 -07001705 memset(&unpin, 0, sizeof(unpin));
1706 unpin.handle = bo_gem->gem_handle;
Keith Packard8e41ce12008-08-04 00:34:08 -07001707
Chris Wilson62997222010-09-25 21:32:59 +01001708 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
Eric Anholtd70d6052009-10-06 12:40:42 -07001709 if (ret != 0)
1710 return -errno;
Keith Packard8e41ce12008-08-04 00:34:08 -07001711
Eric Anholtd70d6052009-10-06 12:40:42 -07001712 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07001713}
1714
1715static int
Chris Wilson1db22ff2010-06-21 14:27:23 +01001716drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
1717 uint32_t tiling_mode,
1718 uint32_t stride)
Keith Packard8e41ce12008-08-04 00:34:08 -07001719{
Eric Anholtd70d6052009-10-06 12:40:42 -07001720 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1721 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1722 struct drm_i915_gem_set_tiling set_tiling;
1723 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001724
Chris Wilsonaba35022010-06-22 13:00:22 +01001725 if (bo_gem->global_name == 0 &&
1726 tiling_mode == bo_gem->tiling_mode &&
Chris Wilson056aa9b2010-06-21 14:31:29 +01001727 stride == bo_gem->stride)
Eric Anholtd70d6052009-10-06 12:40:42 -07001728 return 0;
Keith Packard18f091d2008-12-15 15:08:12 -08001729
Eric Anholtd70d6052009-10-06 12:40:42 -07001730 memset(&set_tiling, 0, sizeof(set_tiling));
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001731 do {
Chris Wilson62997222010-09-25 21:32:59 +01001732 /* set_tiling is slightly broken and overwrites the
1733 * input on the error path, so we have to open code
1734 * rmIoctl.
1735 */
Chris Wilson1db22ff2010-06-21 14:27:23 +01001736 set_tiling.handle = bo_gem->gem_handle;
1737 set_tiling.tiling_mode = tiling_mode;
Chris Wilson4f0f8712010-02-10 09:45:13 +00001738 set_tiling.stride = stride;
1739
Chris Wilson8ffd2e12009-12-01 13:08:04 +00001740 ret = ioctl(bufmgr_gem->fd,
1741 DRM_IOCTL_I915_GEM_SET_TILING,
1742 &set_tiling);
Chris Wilson62997222010-09-25 21:32:59 +01001743 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
Chris Wilson1db22ff2010-06-21 14:27:23 +01001744 if (ret == -1)
1745 return -errno;
1746
1747 bo_gem->tiling_mode = set_tiling.tiling_mode;
1748 bo_gem->swizzle_mode = set_tiling.swizzle_mode;
Chris Wilsonaba35022010-06-22 13:00:22 +01001749 bo_gem->stride = set_tiling.stride;
Chris Wilson1db22ff2010-06-21 14:27:23 +01001750 return 0;
1751}
1752
1753static int
1754drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
1755 uint32_t stride)
1756{
1757 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1758 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1759 int ret;
1760
Chris Wilsoncd34cbe2010-06-22 11:07:26 +01001761 /* Linear buffers have no stride. By ensuring that we only ever use
1762 * stride 0 with linear buffers, we simplify our code.
1763 */
Chris Wilsonc7bbaca2010-06-22 11:15:56 +01001764 if (*tiling_mode == I915_TILING_NONE)
Chris Wilsoncd34cbe2010-06-22 11:07:26 +01001765 stride = 0;
1766
Chris Wilson1db22ff2010-06-21 14:27:23 +01001767 ret = drm_intel_gem_bo_set_tiling_internal(bo, *tiling_mode, stride);
1768 if (ret == 0)
Chris Wilsonfcf3e612010-05-24 18:35:41 +01001769 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
Chris Wilsone22fb792009-11-30 22:14:30 +00001770
Keith Packard18f091d2008-12-15 15:08:12 -08001771 *tiling_mode = bo_gem->tiling_mode;
Chris Wilsonfcf3e612010-05-24 18:35:41 +01001772 return ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001773}
1774
1775static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001776drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
1777 uint32_t * swizzle_mode)
Keith Packard8e41ce12008-08-04 00:34:08 -07001778{
Eric Anholtd70d6052009-10-06 12:40:42 -07001779 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt99338382008-10-14 13:18:11 -07001780
Eric Anholtd70d6052009-10-06 12:40:42 -07001781 *tiling_mode = bo_gem->tiling_mode;
1782 *swizzle_mode = bo_gem->swizzle_mode;
1783 return 0;
Eric Anholt99338382008-10-14 13:18:11 -07001784}
1785
1786static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001787drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name)
Keith Packard8e41ce12008-08-04 00:34:08 -07001788{
Eric Anholtd70d6052009-10-06 12:40:42 -07001789 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1790 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1791 struct drm_gem_flink flink;
1792 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07001793
Eric Anholtd70d6052009-10-06 12:40:42 -07001794 if (!bo_gem->global_name) {
1795 memset(&flink, 0, sizeof(flink));
1796 flink.handle = bo_gem->gem_handle;
1797
Chris Wilson62997222010-09-25 21:32:59 +01001798 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
Eric Anholtd70d6052009-10-06 12:40:42 -07001799 if (ret != 0)
1800 return -errno;
1801 bo_gem->global_name = flink.name;
1802 bo_gem->reusable = 0;
Chris Wilson36d49392011-02-14 09:39:06 +00001803
1804 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
Eric Anholtd70d6052009-10-06 12:40:42 -07001805 }
1806
1807 *name = bo_gem->global_name;
1808 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07001809}
1810
Eric Anholt6a9eb082008-06-03 09:27:37 -07001811/**
1812 * Enables unlimited caching of buffer objects for reuse.
1813 *
1814 * This is potentially very memory expensive, as the cache at each bucket
1815 * size is only bounded by how many buffers of that size we've managed to have
1816 * in flight at once.
1817 */
1818void
Eric Anholt4b982642008-10-30 09:33:07 -07001819drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001820{
Eric Anholtd70d6052009-10-06 12:40:42 -07001821 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001822
Eric Anholtd70d6052009-10-06 12:40:42 -07001823 bufmgr_gem->bo_reuse = 1;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001824}
1825
Eric Anholt0e867312008-10-21 00:10:54 -07001826/**
Jesse Barnesb5096402009-09-15 11:02:58 -07001827 * Enable use of fenced reloc type.
1828 *
1829 * New code should enable this to avoid unnecessary fence register
1830 * allocation. If this option is not enabled, all relocs will have fence
1831 * register allocated.
1832 */
1833void
1834drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr)
1835{
Eric Anholt766fa792010-03-02 16:04:14 -08001836 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
Jesse Barnesb5096402009-09-15 11:02:58 -07001837
Eric Anholt766fa792010-03-02 16:04:14 -08001838 if (bufmgr_gem->bufmgr.bo_exec == drm_intel_gem_bo_exec2)
1839 bufmgr_gem->fenced_relocs = 1;
Jesse Barnesb5096402009-09-15 11:02:58 -07001840}
1841
1842/**
Eric Anholt0e867312008-10-21 00:10:54 -07001843 * Return the additional aperture space required by the tree of buffer objects
1844 * rooted at bo.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001845 */
1846static int
Eric Anholt4b982642008-10-30 09:33:07 -07001847drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001848{
Eric Anholtd70d6052009-10-06 12:40:42 -07001849 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1850 int i;
1851 int total = 0;
Eric Anholt0e867312008-10-21 00:10:54 -07001852
Eric Anholtd70d6052009-10-06 12:40:42 -07001853 if (bo == NULL || bo_gem->included_in_check_aperture)
1854 return 0;
Eric Anholt0e867312008-10-21 00:10:54 -07001855
Eric Anholtd70d6052009-10-06 12:40:42 -07001856 total += bo->size;
1857 bo_gem->included_in_check_aperture = 1;
Eric Anholt0e867312008-10-21 00:10:54 -07001858
Eric Anholtd70d6052009-10-06 12:40:42 -07001859 for (i = 0; i < bo_gem->reloc_count; i++)
1860 total +=
1861 drm_intel_gem_bo_get_aperture_space(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -07001862 reloc_target_info[i].bo);
Eric Anholt0e867312008-10-21 00:10:54 -07001863
Eric Anholtd70d6052009-10-06 12:40:42 -07001864 return total;
Eric Anholt0e867312008-10-21 00:10:54 -07001865}
1866
1867/**
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001868 * Count the number of buffers in this list that need a fence reg
1869 *
1870 * If the count is greater than the number of available regs, we'll have
1871 * to ask the caller to resubmit a batch with fewer tiled buffers.
1872 *
Eric Anholt9209c9a2009-01-27 16:54:11 -08001873 * This function over-counts if the same buffer is used multiple times.
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001874 */
1875static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -07001876drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count)
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001877{
Eric Anholtd70d6052009-10-06 12:40:42 -07001878 int i;
1879 unsigned int total = 0;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001880
Eric Anholtd70d6052009-10-06 12:40:42 -07001881 for (i = 0; i < count; i++) {
1882 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001883
Eric Anholtd70d6052009-10-06 12:40:42 -07001884 if (bo_gem == NULL)
1885 continue;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001886
Eric Anholtd70d6052009-10-06 12:40:42 -07001887 total += bo_gem->reloc_tree_fences;
1888 }
1889 return total;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001890}
1891
1892/**
Eric Anholt4b982642008-10-30 09:33:07 -07001893 * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
1894 * for the next drm_intel_bufmgr_check_aperture_space() call.
Eric Anholt0e867312008-10-21 00:10:54 -07001895 */
1896static void
Eric Anholt4b982642008-10-30 09:33:07 -07001897drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo)
Eric Anholt0e867312008-10-21 00:10:54 -07001898{
Eric Anholtd70d6052009-10-06 12:40:42 -07001899 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1900 int i;
Eric Anholt0e867312008-10-21 00:10:54 -07001901
Eric Anholtd70d6052009-10-06 12:40:42 -07001902 if (bo == NULL || !bo_gem->included_in_check_aperture)
1903 return;
Eric Anholt0e867312008-10-21 00:10:54 -07001904
Eric Anholtd70d6052009-10-06 12:40:42 -07001905 bo_gem->included_in_check_aperture = 0;
Eric Anholt0e867312008-10-21 00:10:54 -07001906
Eric Anholtd70d6052009-10-06 12:40:42 -07001907 for (i = 0; i < bo_gem->reloc_count; i++)
1908 drm_intel_gem_bo_clear_aperture_space_flag(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -07001909 reloc_target_info[i].bo);
Eric Anholt0e867312008-10-21 00:10:54 -07001910}
1911
1912/**
Keith Packardb13f4e12008-11-21 01:49:39 -08001913 * Return a conservative estimate for the amount of aperture required
1914 * for a collection of buffers. This may double-count some buffers.
1915 */
1916static unsigned int
1917drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count)
1918{
Eric Anholtd70d6052009-10-06 12:40:42 -07001919 int i;
1920 unsigned int total = 0;
Keith Packardb13f4e12008-11-21 01:49:39 -08001921
Eric Anholtd70d6052009-10-06 12:40:42 -07001922 for (i = 0; i < count; i++) {
1923 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
1924 if (bo_gem != NULL)
1925 total += bo_gem->reloc_tree_size;
1926 }
1927 return total;
Keith Packardb13f4e12008-11-21 01:49:39 -08001928}
1929
1930/**
1931 * Return the amount of aperture needed for a collection of buffers.
1932 * This avoids double counting any buffers, at the cost of looking
1933 * at every buffer in the set.
1934 */
1935static unsigned int
1936drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count)
1937{
Eric Anholtd70d6052009-10-06 12:40:42 -07001938 int i;
1939 unsigned int total = 0;
Keith Packardb13f4e12008-11-21 01:49:39 -08001940
Eric Anholtd70d6052009-10-06 12:40:42 -07001941 for (i = 0; i < count; i++) {
1942 total += drm_intel_gem_bo_get_aperture_space(bo_array[i]);
1943 /* For the first buffer object in the array, we get an
1944 * accurate count back for its reloc_tree size (since nothing
1945 * had been flagged as being counted yet). We can save that
1946 * value out as a more conservative reloc_tree_size that
1947 * avoids double-counting target buffers. Since the first
1948 * buffer happens to usually be the batch buffer in our
1949 * callers, this can pull us back from doing the tree
1950 * walk on every new batch emit.
1951 */
1952 if (i == 0) {
1953 drm_intel_bo_gem *bo_gem =
1954 (drm_intel_bo_gem *) bo_array[i];
1955 bo_gem->reloc_tree_size = total;
1956 }
Eric Anholt7ce8d4c2009-02-27 13:46:31 -08001957 }
Keith Packardb13f4e12008-11-21 01:49:39 -08001958
Eric Anholtd70d6052009-10-06 12:40:42 -07001959 for (i = 0; i < count; i++)
1960 drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]);
1961 return total;
Keith Packardb13f4e12008-11-21 01:49:39 -08001962}
1963
1964/**
Eric Anholt0e867312008-10-21 00:10:54 -07001965 * Return -1 if the batchbuffer should be flushed before attempting to
1966 * emit rendering referencing the buffers pointed to by bo_array.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001967 *
Eric Anholt0e867312008-10-21 00:10:54 -07001968 * This is required because if we try to emit a batchbuffer with relocations
1969 * to a tree of buffers that won't simultaneously fit in the aperture,
1970 * the rendering will return an error at a point where the software is not
1971 * prepared to recover from it.
1972 *
1973 * However, we also want to emit the batchbuffer significantly before we reach
1974 * the limit, as a series of batchbuffers each of which references buffers
1975 * covering almost all of the aperture means that at each emit we end up
1976 * waiting to evict a buffer from the last rendering, and we get synchronous
1977 * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
1978 * get better parallelism.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001979 */
1980static int
Eric Anholt4b982642008-10-30 09:33:07 -07001981drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001982{
Eric Anholtd70d6052009-10-06 12:40:42 -07001983 drm_intel_bufmgr_gem *bufmgr_gem =
1984 (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr;
1985 unsigned int total = 0;
1986 unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4;
1987 int total_fences;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001988
Eric Anholtd70d6052009-10-06 12:40:42 -07001989 /* Check for fence reg constraints if necessary */
1990 if (bufmgr_gem->available_fences) {
1991 total_fences = drm_intel_gem_total_fences(bo_array, count);
1992 if (total_fences > bufmgr_gem->available_fences)
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001993 return -ENOSPC;
Eric Anholtd70d6052009-10-06 12:40:42 -07001994 }
Eric Anholt0e867312008-10-21 00:10:54 -07001995
Eric Anholtd70d6052009-10-06 12:40:42 -07001996 total = drm_intel_gem_estimate_batch_space(bo_array, count);
Jesse Barnes2fa5f282009-01-23 14:13:45 -08001997
Eric Anholtd70d6052009-10-06 12:40:42 -07001998 if (total > threshold)
1999 total = drm_intel_gem_compute_batch_space(bo_array, count);
Eric Anholt0e867312008-10-21 00:10:54 -07002000
Eric Anholtd70d6052009-10-06 12:40:42 -07002001 if (total > threshold) {
2002 DBG("check_space: overflowed available aperture, "
2003 "%dkb vs %dkb\n",
2004 total / 1024, (int)bufmgr_gem->gtt_size / 1024);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00002005 return -ENOSPC;
Eric Anholtd70d6052009-10-06 12:40:42 -07002006 } else {
2007 DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024,
2008 (int)bufmgr_gem->gtt_size / 1024);
2009 return 0;
2010 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002011}
2012
Keith Packard5b5ce302009-05-11 13:42:12 -07002013/*
2014 * Disable buffer reuse for objects which are shared with the kernel
2015 * as scanout buffers
2016 */
2017static int
2018drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo)
2019{
Eric Anholtd70d6052009-10-06 12:40:42 -07002020 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Keith Packard5b5ce302009-05-11 13:42:12 -07002021
Eric Anholtd70d6052009-10-06 12:40:42 -07002022 bo_gem->reusable = 0;
2023 return 0;
Keith Packard5b5ce302009-05-11 13:42:12 -07002024}
2025
Eric Anholt769b1052009-10-01 19:09:26 -07002026static int
Chris Wilson07e75892010-05-11 08:54:06 +01002027drm_intel_gem_bo_is_reusable(drm_intel_bo *bo)
2028{
2029 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2030
2031 return bo_gem->reusable;
2032}
2033
2034static int
Eric Anholt66d27142009-10-20 13:20:55 -07002035_drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
Eric Anholt769b1052009-10-01 19:09:26 -07002036{
Eric Anholtd70d6052009-10-06 12:40:42 -07002037 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2038 int i;
Eric Anholt769b1052009-10-01 19:09:26 -07002039
Eric Anholtd70d6052009-10-06 12:40:42 -07002040 for (i = 0; i < bo_gem->reloc_count; i++) {
Jesse Barnesb5096402009-09-15 11:02:58 -07002041 if (bo_gem->reloc_target_info[i].bo == target_bo)
Eric Anholtd70d6052009-10-06 12:40:42 -07002042 return 1;
Eric Anholt4f7704a2010-06-10 08:58:08 -07002043 if (bo == bo_gem->reloc_target_info[i].bo)
2044 continue;
Jesse Barnesb5096402009-09-15 11:02:58 -07002045 if (_drm_intel_gem_bo_references(bo_gem->reloc_target_info[i].bo,
Eric Anholtd70d6052009-10-06 12:40:42 -07002046 target_bo))
2047 return 1;
2048 }
2049
Eric Anholt769b1052009-10-01 19:09:26 -07002050 return 0;
Eric Anholt769b1052009-10-01 19:09:26 -07002051}
2052
Eric Anholt66d27142009-10-20 13:20:55 -07002053/** Return true if target_bo is referenced by bo's relocation tree. */
2054static int
2055drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
2056{
2057 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
2058
2059 if (bo == NULL || target_bo == NULL)
2060 return 0;
2061 if (target_bo_gem->used_as_reloc_target)
2062 return _drm_intel_gem_bo_references(bo, target_bo);
2063 return 0;
2064}
2065
Eric Anholt0ec768e2010-06-04 17:09:11 -07002066static void
2067add_bucket(drm_intel_bufmgr_gem *bufmgr_gem, int size)
2068{
2069 unsigned int i = bufmgr_gem->num_buckets;
2070
2071 assert(i < ARRAY_SIZE(bufmgr_gem->cache_bucket));
2072
2073 DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head);
2074 bufmgr_gem->cache_bucket[i].size = size;
2075 bufmgr_gem->num_buckets++;
2076}
2077
2078static void
2079init_cache_buckets(drm_intel_bufmgr_gem *bufmgr_gem)
2080{
2081 unsigned long size, cache_max_size = 64 * 1024 * 1024;
2082
2083 /* OK, so power of two buckets was too wasteful of memory.
2084 * Give 3 other sizes between each power of two, to hopefully
2085 * cover things accurately enough. (The alternative is
2086 * probably to just go for exact matching of sizes, and assume
2087 * that for things like composited window resize the tiled
2088 * width/height alignment and rounding of sizes to pages will
2089 * get us useful cache hit rates anyway)
2090 */
2091 add_bucket(bufmgr_gem, 4096);
2092 add_bucket(bufmgr_gem, 4096 * 2);
2093 add_bucket(bufmgr_gem, 4096 * 3);
2094
2095 /* Initialize the linked lists for BO reuse cache. */
2096 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
2097 add_bucket(bufmgr_gem, size);
2098
2099 add_bucket(bufmgr_gem, size + size * 1 / 4);
2100 add_bucket(bufmgr_gem, size + size * 2 / 4);
2101 add_bucket(bufmgr_gem, size + size * 3 / 4);
2102 }
2103}
2104
Eric Anholt769b1052009-10-01 19:09:26 -07002105/**
Eric Anholt6a9eb082008-06-03 09:27:37 -07002106 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
2107 * and manage map buffer objections.
2108 *
2109 * \param fd File descriptor of the opened DRM device.
2110 */
Eric Anholt4b982642008-10-30 09:33:07 -07002111drm_intel_bufmgr *
2112drm_intel_bufmgr_gem_init(int fd, int batch_size)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002113{
Eric Anholtd70d6052009-10-06 12:40:42 -07002114 drm_intel_bufmgr_gem *bufmgr_gem;
2115 struct drm_i915_gem_get_aperture aperture;
2116 drm_i915_getparam_t gp;
Eric Anholt0ec768e2010-06-04 17:09:11 -07002117 int ret;
Chris Wilson057fab32010-10-26 11:35:11 +01002118 int exec2 = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002119
Eric Anholtd70d6052009-10-06 12:40:42 -07002120 bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
Dave Airlie973d8d62010-02-02 10:57:12 +10002121 if (bufmgr_gem == NULL)
2122 return NULL;
2123
Eric Anholtd70d6052009-10-06 12:40:42 -07002124 bufmgr_gem->fd = fd;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002125
Eric Anholtd70d6052009-10-06 12:40:42 -07002126 if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) {
2127 free(bufmgr_gem);
2128 return NULL;
2129 }
Eric Anholt6df7b072008-06-12 23:22:26 -07002130
Chris Wilson62997222010-09-25 21:32:59 +01002131 ret = drmIoctl(bufmgr_gem->fd,
2132 DRM_IOCTL_I915_GEM_GET_APERTURE,
2133 &aperture);
Eric Anholt0e867312008-10-21 00:10:54 -07002134
Eric Anholtd70d6052009-10-06 12:40:42 -07002135 if (ret == 0)
2136 bufmgr_gem->gtt_size = aperture.aper_available_size;
2137 else {
2138 fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
2139 strerror(errno));
2140 bufmgr_gem->gtt_size = 128 * 1024 * 1024;
2141 fprintf(stderr, "Assuming %dkB available aperture size.\n"
2142 "May lead to reduced performance or incorrect "
2143 "rendering.\n",
2144 (int)bufmgr_gem->gtt_size / 1024);
2145 }
Eric Anholt0e867312008-10-21 00:10:54 -07002146
Eric Anholtd70d6052009-10-06 12:40:42 -07002147 gp.param = I915_PARAM_CHIPSET_ID;
2148 gp.value = &bufmgr_gem->pci_device;
Chris Wilson62997222010-09-25 21:32:59 +01002149 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Eric Anholtcbdd6272009-01-27 17:16:11 -08002150 if (ret) {
Eric Anholtd70d6052009-10-06 12:40:42 -07002151 fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno);
2152 fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value);
Eric Anholtcbdd6272009-01-27 17:16:11 -08002153 }
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002154
Eric Anholta1f9ea72010-03-02 08:49:36 -08002155 if (IS_GEN2(bufmgr_gem))
2156 bufmgr_gem->gen = 2;
2157 else if (IS_GEN3(bufmgr_gem))
2158 bufmgr_gem->gen = 3;
2159 else if (IS_GEN4(bufmgr_gem))
2160 bufmgr_gem->gen = 4;
2161 else
2162 bufmgr_gem->gen = 6;
2163
Jesse Barnesb5096402009-09-15 11:02:58 -07002164 gp.param = I915_PARAM_HAS_EXECBUF2;
Chris Wilson62997222010-09-25 21:32:59 +01002165 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Jesse Barnesb5096402009-09-15 11:02:58 -07002166 if (!ret)
2167 exec2 = 1;
2168
Zou Nan hai66375fd2010-06-02 10:07:37 +08002169 gp.param = I915_PARAM_HAS_BSD;
Chris Wilson62997222010-09-25 21:32:59 +01002170 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Chris Wilson057fab32010-10-26 11:35:11 +01002171 bufmgr_gem->has_bsd = ret == 0;
2172
2173 gp.param = I915_PARAM_HAS_BLT;
2174 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2175 bufmgr_gem->has_blt = ret == 0;
Zou Nan hai66375fd2010-06-02 10:07:37 +08002176
Chris Wilson36245772010-10-29 10:49:54 +01002177 gp.param = I915_PARAM_HAS_RELAXED_FENCING;
2178 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2179 bufmgr_gem->has_relaxed_fencing = ret == 0;
2180
Eric Anholta1f9ea72010-03-02 08:49:36 -08002181 if (bufmgr_gem->gen < 4) {
Eric Anholtd70d6052009-10-06 12:40:42 -07002182 gp.param = I915_PARAM_NUM_FENCES_AVAIL;
2183 gp.value = &bufmgr_gem->available_fences;
Chris Wilson62997222010-09-25 21:32:59 +01002184 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Eric Anholtd70d6052009-10-06 12:40:42 -07002185 if (ret) {
2186 fprintf(stderr, "get fences failed: %d [%d]\n", ret,
2187 errno);
2188 fprintf(stderr, "param: %d, val: %d\n", gp.param,
2189 *gp.value);
2190 bufmgr_gem->available_fences = 0;
Chris Wilsonfdcde592010-02-09 08:32:54 +00002191 } else {
2192 /* XXX The kernel reports the total number of fences,
2193 * including any that may be pinned.
2194 *
2195 * We presume that there will be at least one pinned
2196 * fence for the scanout buffer, but there may be more
2197 * than one scanout and the user may be manually
2198 * pinning buffers. Let's move to execbuffer2 and
2199 * thereby forget the insanity of using fences...
2200 */
2201 bufmgr_gem->available_fences -= 2;
2202 if (bufmgr_gem->available_fences < 0)
2203 bufmgr_gem->available_fences = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07002204 }
2205 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002206
Eric Anholtd70d6052009-10-06 12:40:42 -07002207 /* Let's go with one relocation per every 2 dwords (but round down a bit
2208 * since a power of two will mean an extra page allocation for the reloc
2209 * buffer).
2210 *
2211 * Every 4 was too few for the blender benchmark.
2212 */
2213 bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
Eric Anholt769b1052009-10-01 19:09:26 -07002214
Eric Anholtd70d6052009-10-06 12:40:42 -07002215 bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc;
2216 bufmgr_gem->bufmgr.bo_alloc_for_render =
2217 drm_intel_gem_bo_alloc_for_render;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -07002218 bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled;
Eric Anholtd70d6052009-10-06 12:40:42 -07002219 bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference;
2220 bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference;
2221 bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map;
2222 bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap;
2223 bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata;
2224 bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata;
2225 bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering;
2226 bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc;
Jesse Barnesb5096402009-09-15 11:02:58 -07002227 bufmgr_gem->bufmgr.bo_emit_reloc_fence = drm_intel_gem_bo_emit_reloc_fence;
Eric Anholtd70d6052009-10-06 12:40:42 -07002228 bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin;
2229 bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin;
2230 bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling;
2231 bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling;
2232 bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink;
Jesse Barnesb5096402009-09-15 11:02:58 -07002233 /* Use the new one if available */
Zou Nan hai66375fd2010-06-02 10:07:37 +08002234 if (exec2) {
Jesse Barnesb5096402009-09-15 11:02:58 -07002235 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec2;
Albert Damen49447a92010-11-07 15:54:32 +01002236 bufmgr_gem->bufmgr.bo_mrb_exec = drm_intel_gem_bo_mrb_exec2;
Zou Nan hai66375fd2010-06-02 10:07:37 +08002237 } else
Jesse Barnesb5096402009-09-15 11:02:58 -07002238 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec;
Eric Anholtd70d6052009-10-06 12:40:42 -07002239 bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy;
Chris Wilson83a35b62009-11-11 13:04:38 +00002240 bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise;
Eric Anholtd70d6052009-10-06 12:40:42 -07002241 bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_destroy;
2242 bufmgr_gem->bufmgr.debug = 0;
2243 bufmgr_gem->bufmgr.check_aperture_space =
2244 drm_intel_gem_check_aperture_space;
2245 bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse;
Chris Wilson07e75892010-05-11 08:54:06 +01002246 bufmgr_gem->bufmgr.bo_is_reusable = drm_intel_gem_bo_is_reusable;
Eric Anholtd70d6052009-10-06 12:40:42 -07002247 bufmgr_gem->bufmgr.get_pipe_from_crtc_id =
2248 drm_intel_gem_get_pipe_from_crtc_id;
2249 bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002250
Chris Wilson36d49392011-02-14 09:39:06 +00002251 DRMINITLISTHEAD(&bufmgr_gem->named);
Eric Anholt0ec768e2010-06-04 17:09:11 -07002252 init_cache_buckets(bufmgr_gem);
Eric Anholtd70d6052009-10-06 12:40:42 -07002253
2254 return &bufmgr_gem->bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002255}