blob: 2723e21b478274115b81bef637c57f46698c1da3 [file] [log] [blame]
Eric Anholt6a9eb082008-06-03 09:27:37 -07001/**************************************************************************
2 *
3 * Copyright © 2007 Red Hat Inc.
Eric Anholtc9ce2ed2012-03-09 16:08:23 -08004 * Copyright © 2007-2012 Intel Corporation
Eric Anholt6a9eb082008-06-03 09:27:37 -07005 * 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>
Jesse Barnes276c07d2008-11-13 13:52:04 -080051#include <sys/stat.h>
52#include <sys/types.h>
Eric Anholt2c2bdb32011-10-21 16:53:16 -070053#include <stdbool.h>
Eric Anholt6a9eb082008-06-03 09:27:37 -070054
55#include "errno.h"
David Shao7d42b492012-11-10 00:24:56 -050056#ifndef ETIME
57#define ETIME ETIMEDOUT
58#endif
Emil Velikov42465fe2015-04-05 15:51:59 +010059#include "libdrm_macros.h"
Eric Anholt72abe982009-02-18 13:06:35 -080060#include "libdrm_lists.h"
Eric Anholtc4857422008-06-03 10:20:49 -070061#include "intel_bufmgr.h"
Eric Anholt738e36a2008-09-05 10:35:32 +010062#include "intel_bufmgr_priv.h"
Eric Anholtcbdd6272009-01-27 17:16:11 -080063#include "intel_chipset.h"
Eric Anholt6a9eb082008-06-03 09:27:37 -070064#include "string.h"
Eric Anholt6a9eb082008-06-03 09:27:37 -070065
66#include "i915_drm.h"
67
Chris Wilson90b23cc2012-02-09 10:23:10 +000068#ifdef HAVE_VALGRIND
69#include <valgrind.h>
70#include <memcheck.h>
71#define VG(x) x
72#else
73#define VG(x)
74#endif
75
Daniel Vettereb7a5b62015-02-11 11:59:52 +010076#define memclear(s) memset(&s, 0, sizeof(s))
Chris Wilson90b23cc2012-02-09 10:23:10 +000077
Eric Anholt6a9eb082008-06-03 09:27:37 -070078#define DBG(...) do { \
Eric Anholtd70d6052009-10-06 12:40:42 -070079 if (bufmgr_gem->bufmgr.debug) \
80 fprintf(stderr, __VA_ARGS__); \
Eric Anholt6a9eb082008-06-03 09:27:37 -070081} while (0)
82
Eric Anholt0ec768e2010-06-04 17:09:11 -070083#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
Anuj Phogat5c68f9f2015-04-10 17:20:55 -070084#define MAX2(A, B) ((A) > (B) ? (A) : (B))
Eric Anholt0ec768e2010-06-04 17:09:11 -070085
Eric Anholt4b982642008-10-30 09:33:07 -070086typedef struct _drm_intel_bo_gem drm_intel_bo_gem;
Keith Packarda919ff52008-06-05 15:58:09 -070087
Eric Anholt4b982642008-10-30 09:33:07 -070088struct drm_intel_gem_bo_bucket {
Eric Anholtd70d6052009-10-06 12:40:42 -070089 drmMMListHead head;
90 unsigned long size;
Eric Anholt6a9eb082008-06-03 09:27:37 -070091};
92
Eric Anholt4b982642008-10-30 09:33:07 -070093typedef struct _drm_intel_bufmgr_gem {
Eric Anholtd70d6052009-10-06 12:40:42 -070094 drm_intel_bufmgr bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -070095
Lionel Landwerlin743af592014-09-12 13:48:36 +010096 atomic_t refcount;
97
Eric Anholtd70d6052009-10-06 12:40:42 -070098 int fd;
Eric Anholt6a9eb082008-06-03 09:27:37 -070099
Eric Anholtd70d6052009-10-06 12:40:42 -0700100 int max_relocs;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700101
Eric Anholtd70d6052009-10-06 12:40:42 -0700102 pthread_mutex_t lock;
Eric Anholt6df7b072008-06-12 23:22:26 -0700103
Eric Anholtd70d6052009-10-06 12:40:42 -0700104 struct drm_i915_gem_exec_object *exec_objects;
Jesse Barnesb5096402009-09-15 11:02:58 -0700105 struct drm_i915_gem_exec_object2 *exec2_objects;
Eric Anholtd70d6052009-10-06 12:40:42 -0700106 drm_intel_bo **exec_bos;
107 int exec_size;
108 int exec_count;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700109
Eric Anholtd70d6052009-10-06 12:40:42 -0700110 /** Array of lists of cached gem objects of power-of-two sizes */
Eric Anholt0ec768e2010-06-04 17:09:11 -0700111 struct drm_intel_gem_bo_bucket cache_bucket[14 * 4];
112 int num_buckets;
Chris Wilsonf16b4162010-06-21 15:21:48 +0100113 time_t time;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700114
Lionel Landwerlin743af592014-09-12 13:48:36 +0100115 drmMMListHead managers;
116
Chris Wilson36d49392011-02-14 09:39:06 +0000117 drmMMListHead named;
Chris Wilsone4b60f22011-12-05 21:29:05 +0000118 drmMMListHead vma_cache;
Chris Wilsondd9a5b42011-12-06 13:12:37 +0000119 int vma_count, vma_open, vma_max;
Chris Wilson36d49392011-02-14 09:39:06 +0000120
Eric Anholtd70d6052009-10-06 12:40:42 -0700121 uint64_t gtt_size;
122 int available_fences;
123 int pci_device;
Eric Anholta1f9ea72010-03-02 08:49:36 -0800124 int gen;
Chris Wilson36245772010-10-29 10:49:54 +0100125 unsigned int has_bsd : 1;
126 unsigned int has_blt : 1;
127 unsigned int has_relaxed_fencing : 1;
Eugeni Dodonov151cdcf2012-01-17 15:20:19 -0200128 unsigned int has_llc : 1;
Ben Widawsky971c0802012-06-05 11:30:48 -0700129 unsigned int has_wait_timeout : 1;
Chris Wilson36245772010-10-29 10:49:54 +0100130 unsigned int bo_reuse : 1;
Kenneth Graunke6e642db2011-10-11 14:38:34 -0700131 unsigned int no_exec : 1;
Xiang, Haihao01199992012-11-14 12:46:39 +0800132 unsigned int has_vebox : 1;
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700133 bool fenced_relocs;
Eric Anholt4db16a92011-10-11 15:59:03 -0700134
Tvrtko Ursulin30921482015-04-17 11:57:28 +0100135 struct {
136 void *ptr;
137 uint32_t handle;
138 } userptr_active;
139
Eric Anholt4b982642008-10-30 09:33:07 -0700140} drm_intel_bufmgr_gem;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700141
Jesse Barnesb5096402009-09-15 11:02:58 -0700142#define DRM_INTEL_RELOC_FENCE (1<<0)
143
144typedef struct _drm_intel_reloc_target_info {
145 drm_intel_bo *bo;
146 int flags;
147} drm_intel_reloc_target;
148
Eric Anholt4b982642008-10-30 09:33:07 -0700149struct _drm_intel_bo_gem {
Eric Anholtd70d6052009-10-06 12:40:42 -0700150 drm_intel_bo bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700151
Eric Anholtd70d6052009-10-06 12:40:42 -0700152 atomic_t refcount;
153 uint32_t gem_handle;
154 const char *name;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700155
Eric Anholtd70d6052009-10-06 12:40:42 -0700156 /**
157 * Kenel-assigned global name for this object
Keith Packardc3d96892013-11-22 05:31:01 -0800158 *
159 * List contains both flink named and prime fd'd objects
Eric Anholtd70d6052009-10-06 12:40:42 -0700160 */
161 unsigned int global_name;
Chris Wilson36d49392011-02-14 09:39:06 +0000162 drmMMListHead name_list;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700163
Eric Anholtd70d6052009-10-06 12:40:42 -0700164 /**
165 * Index of the buffer within the validation list while preparing a
166 * batchbuffer execution.
167 */
168 int validate_index;
Keith Packard18f091d2008-12-15 15:08:12 -0800169
Eric Anholtd70d6052009-10-06 12:40:42 -0700170 /**
171 * Current tiling mode
172 */
173 uint32_t tiling_mode;
174 uint32_t swizzle_mode;
Chris Wilson056aa9b2010-06-21 14:31:29 +0100175 unsigned long stride;
Eric Anholt3f3c5be2009-07-09 17:49:46 -0700176
Eric Anholtd70d6052009-10-06 12:40:42 -0700177 time_t free_time;
Keith Packard329e0862008-06-05 16:05:35 -0700178
Eric Anholtd70d6052009-10-06 12:40:42 -0700179 /** Array passed to the DRM containing relocation information. */
180 struct drm_i915_gem_relocation_entry *relocs;
Jesse Barnesb5096402009-09-15 11:02:58 -0700181 /**
182 * Array of info structs corresponding to relocs[i].target_handle etc
183 */
184 drm_intel_reloc_target *reloc_target_info;
Eric Anholtd70d6052009-10-06 12:40:42 -0700185 /** Number of entries in relocs */
186 int reloc_count;
187 /** Mapped address for the buffer, saved across map/unmap cycles */
188 void *mem_virtual;
189 /** GTT virtual address for the buffer, saved across map/unmap cycles */
190 void *gtt_virtual;
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +0100191 /**
192 * Virtual address of the buffer allocated by user, used for userptr
193 * objects only.
194 */
195 void *user_virtual;
Chris Wilsonc549a772011-12-05 10:14:34 +0000196 int map_count;
Chris Wilsone4b60f22011-12-05 21:29:05 +0000197 drmMMListHead vma_list;
Eric Anholt0e867312008-10-21 00:10:54 -0700198
Eric Anholtd70d6052009-10-06 12:40:42 -0700199 /** BO cache list */
200 drmMMListHead head;
Eric Anholt0e867312008-10-21 00:10:54 -0700201
Eric Anholtd70d6052009-10-06 12:40:42 -0700202 /**
203 * Boolean of whether this BO and its children have been included in
204 * the current drm_intel_bufmgr_check_aperture_space() total.
205 */
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700206 bool included_in_check_aperture;
Eric Anholt0e867312008-10-21 00:10:54 -0700207
Eric Anholtd70d6052009-10-06 12:40:42 -0700208 /**
209 * Boolean of whether this buffer has been used as a relocation
210 * target and had its size accounted for, and thus can't have any
211 * further relocations added to it.
212 */
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700213 bool used_as_reloc_target;
Keith Packard5b5ce302009-05-11 13:42:12 -0700214
Eric Anholtd70d6052009-10-06 12:40:42 -0700215 /**
Chris Wilson792fed12009-12-02 13:12:39 +0000216 * Boolean of whether we have encountered an error whilst building the relocation tree.
217 */
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700218 bool has_error;
Chris Wilson792fed12009-12-02 13:12:39 +0000219
220 /**
Eric Anholtd70d6052009-10-06 12:40:42 -0700221 * Boolean of whether this buffer can be re-used
222 */
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700223 bool reusable;
Eric Anholtd70d6052009-10-06 12:40:42 -0700224
225 /**
Eric Anholt02f93c22014-01-15 00:38:39 -0800226 * Boolean of whether the GPU is definitely not accessing the buffer.
227 *
228 * This is only valid when reusable, since non-reusable
229 * buffers are those that have been shared wth other
230 * processes, so we don't know their state.
231 */
232 bool idle;
233
234 /**
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +0100235 * Boolean of whether this buffer was allocated with userptr
236 */
237 bool is_userptr;
238
239 /**
Eric Anholtd70d6052009-10-06 12:40:42 -0700240 * Size in bytes of this buffer and its relocation descendents.
241 *
242 * Used to avoid costly tree walking in
243 * drm_intel_bufmgr_check_aperture in the common case.
244 */
245 int reloc_tree_size;
246
247 /**
248 * Number of potential fence registers required by this buffer and its
249 * relocations.
250 */
251 int reloc_tree_fences;
Eric Anholt4cb01ee2011-10-28 13:12:16 -0700252
253 /** Flags that we may need to do the SW_FINSIH ioctl on unmap. */
254 bool mapped_cpu_write;
Keith Packarda919ff52008-06-05 15:58:09 -0700255};
Eric Anholt6a9eb082008-06-03 09:27:37 -0700256
Keith Packardb13f4e12008-11-21 01:49:39 -0800257static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -0700258drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count);
Keith Packardb13f4e12008-11-21 01:49:39 -0800259
260static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -0700261drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count);
Keith Packardb13f4e12008-11-21 01:49:39 -0800262
Eric Anholt6a9eb082008-06-03 09:27:37 -0700263static int
Eric Anholtd70d6052009-10-06 12:40:42 -0700264drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
265 uint32_t * swizzle_mode);
Keith Packard18f091d2008-12-15 15:08:12 -0800266
267static int
Chris Wilson1db22ff2010-06-21 14:27:23 +0100268drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
269 uint32_t tiling_mode,
270 uint32_t stride);
Keith Packard18f091d2008-12-15 15:08:12 -0800271
Eric Anholt0d7ad7e2009-10-20 14:19:38 -0700272static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
273 time_t time);
Chris Wilson04495ee2009-10-02 04:39:22 +0100274
Eric Anholtd70d6052009-10-06 12:40:42 -0700275static void drm_intel_gem_bo_unreference(drm_intel_bo *bo);
Keith Packard18f091d2008-12-15 15:08:12 -0800276
Eric Anholtd70d6052009-10-06 12:40:42 -0700277static void drm_intel_gem_bo_free(drm_intel_bo *bo);
Chris Wilson0fb215a2009-10-02 04:31:34 +0100278
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700279static unsigned long
280drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size,
281 uint32_t *tiling_mode)
282{
283 unsigned long min_size, max_size;
284 unsigned long i;
285
286 if (*tiling_mode == I915_TILING_NONE)
287 return size;
288
289 /* 965+ just need multiples of page size for tiling */
Eric Anholta1f9ea72010-03-02 08:49:36 -0800290 if (bufmgr_gem->gen >= 4)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700291 return ROUND_UP_TO(size, 4096);
292
293 /* Older chips need powers of two, of at least 512k or 1M */
Eric Anholtacbaff22010-03-02 15:24:50 -0800294 if (bufmgr_gem->gen == 3) {
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700295 min_size = 1024*1024;
296 max_size = 128*1024*1024;
297 } else {
298 min_size = 512*1024;
299 max_size = 64*1024*1024;
300 }
301
302 if (size > max_size) {
303 *tiling_mode = I915_TILING_NONE;
304 return size;
305 }
306
Chris Wilson36245772010-10-29 10:49:54 +0100307 /* Do we need to allocate every page for the fence? */
308 if (bufmgr_gem->has_relaxed_fencing)
309 return ROUND_UP_TO(size, 4096);
310
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700311 for (i = min_size; i < size; i <<= 1)
312 ;
313
314 return i;
315}
316
317/*
318 * Round a given pitch up to the minimum required for X tiling on a
319 * given chip. We use 512 as the minimum to allow for a later tiling
320 * change.
321 */
322static unsigned long
323drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem,
Chris Wilson726210f2010-06-24 11:38:00 +0100324 unsigned long pitch, uint32_t *tiling_mode)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700325{
Eric Anholt1d4d1e62010-03-04 16:09:40 -0800326 unsigned long tile_width;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700327 unsigned long i;
328
Eric Anholt7c697b12010-03-17 10:05:55 -0700329 /* If untiled, then just align it so that we can do rendering
330 * to it with the 3D engine.
331 */
Chris Wilson726210f2010-06-24 11:38:00 +0100332 if (*tiling_mode == I915_TILING_NONE)
Eric Anholt7c697b12010-03-17 10:05:55 -0700333 return ALIGN(pitch, 64);
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700334
Daniel Vetter194aa1b2011-09-22 22:20:53 +0200335 if (*tiling_mode == I915_TILING_X
Eric Anholt078bc5b2011-12-20 13:10:36 -0800336 || (IS_915(bufmgr_gem->pci_device)
337 && *tiling_mode == I915_TILING_Y))
Eric Anholt1d4d1e62010-03-04 16:09:40 -0800338 tile_width = 512;
339 else
340 tile_width = 128;
341
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700342 /* 965 is flexible */
Eric Anholta1f9ea72010-03-02 08:49:36 -0800343 if (bufmgr_gem->gen >= 4)
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700344 return ROUND_UP_TO(pitch, tile_width);
345
Chris Wilson726210f2010-06-24 11:38:00 +0100346 /* The older hardware has a maximum pitch of 8192 with tiled
347 * surfaces, so fallback to untiled if it's too large.
348 */
349 if (pitch > 8192) {
350 *tiling_mode = I915_TILING_NONE;
351 return ALIGN(pitch, 64);
352 }
353
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700354 /* Pre-965 needs power of two tile width */
355 for (i = tile_width; i < pitch; i <<= 1)
356 ;
357
358 return i;
359}
360
Eric Anholt4b982642008-10-30 09:33:07 -0700361static struct drm_intel_gem_bo_bucket *
362drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem,
363 unsigned long size)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700364{
Eric Anholtd70d6052009-10-06 12:40:42 -0700365 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700366
Eric Anholt0ec768e2010-06-04 17:09:11 -0700367 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
Eric Anholtd70d6052009-10-06 12:40:42 -0700368 struct drm_intel_gem_bo_bucket *bucket =
369 &bufmgr_gem->cache_bucket[i];
370 if (bucket->size >= size) {
371 return bucket;
372 }
Eric Anholt78fa5902009-07-06 11:55:28 -0700373 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700374
Eric Anholtd70d6052009-10-06 12:40:42 -0700375 return NULL;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700376}
377
Eric Anholtd70d6052009-10-06 12:40:42 -0700378static void
379drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700380{
Eric Anholtd70d6052009-10-06 12:40:42 -0700381 int i, j;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700382
Eric Anholtd70d6052009-10-06 12:40:42 -0700383 for (i = 0; i < bufmgr_gem->exec_count; i++) {
384 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
385 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700386
Eric Anholtd70d6052009-10-06 12:40:42 -0700387 if (bo_gem->relocs == NULL) {
388 DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle,
389 bo_gem->name);
390 continue;
391 }
392
393 for (j = 0; j < bo_gem->reloc_count; j++) {
Jesse Barnesb5096402009-09-15 11:02:58 -0700394 drm_intel_bo *target_bo = bo_gem->reloc_target_info[j].bo;
Eric Anholtd70d6052009-10-06 12:40:42 -0700395 drm_intel_bo_gem *target_gem =
396 (drm_intel_bo_gem *) target_bo;
397
398 DBG("%2d: %d (%s)@0x%08llx -> "
399 "%d (%s)@0x%08lx + 0x%08x\n",
400 i,
401 bo_gem->gem_handle, bo_gem->name,
402 (unsigned long long)bo_gem->relocs[j].offset,
403 target_gem->gem_handle,
404 target_gem->name,
Kenneth Graunkeedf17db2014-01-13 14:14:36 -0800405 target_bo->offset64,
Eric Anholtd70d6052009-10-06 12:40:42 -0700406 bo_gem->relocs[j].delta);
407 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700408 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700409}
410
Chris Wilson9fec2a82009-12-02 10:42:51 +0000411static inline void
Chris Wilson04495ee2009-10-02 04:39:22 +0100412drm_intel_gem_bo_reference(drm_intel_bo *bo)
413{
Eric Anholtd70d6052009-10-06 12:40:42 -0700414 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Chris Wilson04495ee2009-10-02 04:39:22 +0100415
Eric Anholtd70d6052009-10-06 12:40:42 -0700416 atomic_inc(&bo_gem->refcount);
Chris Wilson04495ee2009-10-02 04:39:22 +0100417}
418
Eric Anholt6a9eb082008-06-03 09:27:37 -0700419/**
420 * Adds the given buffer to the list of buffers to be validated (moved into the
421 * appropriate memory type) with the next batch submission.
422 *
423 * If a buffer is validated multiple times in a batch submission, it ends up
424 * with the intersection of the memory type flags and the union of the
425 * access flags.
426 */
427static void
Eric Anholt4b982642008-10-30 09:33:07 -0700428drm_intel_add_validate_buffer(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700429{
Eric Anholtd70d6052009-10-06 12:40:42 -0700430 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
431 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
432 int index;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700433
Eric Anholtd70d6052009-10-06 12:40:42 -0700434 if (bo_gem->validate_index != -1)
435 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700436
Eric Anholtd70d6052009-10-06 12:40:42 -0700437 /* Extend the array of validation entries as necessary. */
438 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
439 int new_size = bufmgr_gem->exec_size * 2;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700440
Eric Anholtd70d6052009-10-06 12:40:42 -0700441 if (new_size == 0)
442 new_size = 5;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700443
Eric Anholtd70d6052009-10-06 12:40:42 -0700444 bufmgr_gem->exec_objects =
445 realloc(bufmgr_gem->exec_objects,
446 sizeof(*bufmgr_gem->exec_objects) * new_size);
447 bufmgr_gem->exec_bos =
448 realloc(bufmgr_gem->exec_bos,
449 sizeof(*bufmgr_gem->exec_bos) * new_size);
450 bufmgr_gem->exec_size = new_size;
451 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700452
Eric Anholtd70d6052009-10-06 12:40:42 -0700453 index = bufmgr_gem->exec_count;
454 bo_gem->validate_index = index;
455 /* Fill in array entry */
456 bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
457 bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
458 bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs;
Anuj Phogat5ba34e12015-04-10 17:20:56 -0700459 bufmgr_gem->exec_objects[index].alignment = bo->align;
Eric Anholtd70d6052009-10-06 12:40:42 -0700460 bufmgr_gem->exec_objects[index].offset = 0;
461 bufmgr_gem->exec_bos[index] = bo;
Eric Anholtd70d6052009-10-06 12:40:42 -0700462 bufmgr_gem->exec_count++;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700463}
464
Jesse Barnesb5096402009-09-15 11:02:58 -0700465static void
466drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
467{
468 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
469 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
470 int index;
471
Eric Anholt47102862010-03-03 10:07:27 -0800472 if (bo_gem->validate_index != -1) {
473 if (need_fence)
474 bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |=
475 EXEC_OBJECT_NEEDS_FENCE;
Jesse Barnesb5096402009-09-15 11:02:58 -0700476 return;
Eric Anholt47102862010-03-03 10:07:27 -0800477 }
Jesse Barnesb5096402009-09-15 11:02:58 -0700478
479 /* Extend the array of validation entries as necessary. */
480 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
481 int new_size = bufmgr_gem->exec_size * 2;
482
483 if (new_size == 0)
484 new_size = 5;
485
486 bufmgr_gem->exec2_objects =
487 realloc(bufmgr_gem->exec2_objects,
488 sizeof(*bufmgr_gem->exec2_objects) * new_size);
489 bufmgr_gem->exec_bos =
490 realloc(bufmgr_gem->exec_bos,
491 sizeof(*bufmgr_gem->exec_bos) * new_size);
492 bufmgr_gem->exec_size = new_size;
493 }
494
495 index = bufmgr_gem->exec_count;
496 bo_gem->validate_index = index;
497 /* Fill in array entry */
498 bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
499 bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
500 bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
Anuj Phogat5ba34e12015-04-10 17:20:56 -0700501 bufmgr_gem->exec2_objects[index].alignment = bo->align;
Jesse Barnesb5096402009-09-15 11:02:58 -0700502 bufmgr_gem->exec2_objects[index].offset = 0;
503 bufmgr_gem->exec_bos[index] = bo;
504 bufmgr_gem->exec2_objects[index].flags = 0;
505 bufmgr_gem->exec2_objects[index].rsvd1 = 0;
506 bufmgr_gem->exec2_objects[index].rsvd2 = 0;
507 if (need_fence) {
508 bufmgr_gem->exec2_objects[index].flags |=
509 EXEC_OBJECT_NEEDS_FENCE;
510 }
511 bufmgr_gem->exec_count++;
512}
513
Eric Anholt6a9eb082008-06-03 09:27:37 -0700514#define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
515 sizeof(uint32_t))
516
Chris Wilsone22fb792009-11-30 22:14:30 +0000517static void
518drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem,
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700519 drm_intel_bo_gem *bo_gem,
520 unsigned int alignment)
Chris Wilsone22fb792009-11-30 22:14:30 +0000521{
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700522 unsigned int size;
Chris Wilsone22fb792009-11-30 22:14:30 +0000523
524 assert(!bo_gem->used_as_reloc_target);
525
526 /* The older chipsets are far-less flexible in terms of tiling,
527 * and require tiled buffer to be size aligned in the aperture.
528 * This means that in the worst possible case we will need a hole
529 * twice as large as the object in order for it to fit into the
530 * aperture. Optimal packing is for wimps.
531 */
532 size = bo_gem->bo.size;
Chris Wilson51b89502010-11-22 09:50:06 +0000533 if (bufmgr_gem->gen < 4 && bo_gem->tiling_mode != I915_TILING_NONE) {
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700534 unsigned int min_size;
Chris Wilson51b89502010-11-22 09:50:06 +0000535
536 if (bufmgr_gem->has_relaxed_fencing) {
537 if (bufmgr_gem->gen == 3)
538 min_size = 1024*1024;
539 else
540 min_size = 512*1024;
541
542 while (min_size < size)
543 min_size *= 2;
544 } else
545 min_size = size;
546
547 /* Account for worst-case alignment. */
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700548 alignment = MAX2(alignment, min_size);
Chris Wilson51b89502010-11-22 09:50:06 +0000549 }
Chris Wilsone22fb792009-11-30 22:14:30 +0000550
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700551 bo_gem->reloc_tree_size = size + alignment;
Chris Wilsone22fb792009-11-30 22:14:30 +0000552}
553
Eric Anholt6a9eb082008-06-03 09:27:37 -0700554static int
Eric Anholt4b982642008-10-30 09:33:07 -0700555drm_intel_setup_reloc_list(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700556{
Eric Anholtd70d6052009-10-06 12:40:42 -0700557 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
558 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
559 unsigned int max_relocs = bufmgr_gem->max_relocs;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700560
Eric Anholtd70d6052009-10-06 12:40:42 -0700561 if (bo->size / 4 < max_relocs)
562 max_relocs = bo->size / 4;
Eric Anholt3c9bd062009-10-05 16:35:32 -0700563
Eric Anholtd70d6052009-10-06 12:40:42 -0700564 bo_gem->relocs = malloc(max_relocs *
565 sizeof(struct drm_i915_gem_relocation_entry));
Jesse Barnesb5096402009-09-15 11:02:58 -0700566 bo_gem->reloc_target_info = malloc(max_relocs *
Chris Wilson35061732010-04-11 18:40:38 +0100567 sizeof(drm_intel_reloc_target));
Jesse Barnesb5096402009-09-15 11:02:58 -0700568 if (bo_gem->relocs == NULL || bo_gem->reloc_target_info == NULL) {
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700569 bo_gem->has_error = true;
Chris Wilson792fed12009-12-02 13:12:39 +0000570
571 free (bo_gem->relocs);
572 bo_gem->relocs = NULL;
573
Jesse Barnesb5096402009-09-15 11:02:58 -0700574 free (bo_gem->reloc_target_info);
575 bo_gem->reloc_target_info = NULL;
Chris Wilson792fed12009-12-02 13:12:39 +0000576
577 return 1;
578 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700579
Eric Anholtd70d6052009-10-06 12:40:42 -0700580 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700581}
582
Eric Anholt8214a652009-08-27 18:32:07 -0700583static int
584drm_intel_gem_bo_busy(drm_intel_bo *bo)
585{
Eric Anholtd70d6052009-10-06 12:40:42 -0700586 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
587 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
588 struct drm_i915_gem_busy busy;
589 int ret;
Eric Anholt8214a652009-08-27 18:32:07 -0700590
Eric Anholt02f93c22014-01-15 00:38:39 -0800591 if (bo_gem->reusable && bo_gem->idle)
592 return false;
593
Daniel Vettereb7a5b62015-02-11 11:59:52 +0100594 memclear(busy);
Eric Anholtd70d6052009-10-06 12:40:42 -0700595 busy.handle = bo_gem->gem_handle;
Eric Anholt8214a652009-08-27 18:32:07 -0700596
Chris Wilson62997222010-09-25 21:32:59 +0100597 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
Eric Anholt02f93c22014-01-15 00:38:39 -0800598 if (ret == 0) {
599 bo_gem->idle = !busy.busy;
600 return busy.busy;
601 } else {
602 return false;
603 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700604 return (ret == 0 && busy.busy);
Eric Anholt8214a652009-08-27 18:32:07 -0700605}
606
Chris Wilson0fb215a2009-10-02 04:31:34 +0100607static int
Chris Wilson83a35b62009-11-11 13:04:38 +0000608drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem,
609 drm_intel_bo_gem *bo_gem, int state)
Chris Wilson0fb215a2009-10-02 04:31:34 +0100610{
Eric Anholtd70d6052009-10-06 12:40:42 -0700611 struct drm_i915_gem_madvise madv;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100612
Daniel Vettereb7a5b62015-02-11 11:59:52 +0100613 memclear(madv);
Eric Anholtd70d6052009-10-06 12:40:42 -0700614 madv.handle = bo_gem->gem_handle;
615 madv.madv = state;
616 madv.retained = 1;
Chris Wilson62997222010-09-25 21:32:59 +0100617 drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
Chris Wilson0fb215a2009-10-02 04:31:34 +0100618
Eric Anholtd70d6052009-10-06 12:40:42 -0700619 return madv.retained;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100620}
621
Chris Wilson83a35b62009-11-11 13:04:38 +0000622static int
623drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv)
624{
625 return drm_intel_gem_bo_madvise_internal
626 ((drm_intel_bufmgr_gem *) bo->bufmgr,
627 (drm_intel_bo_gem *) bo,
628 madv);
629}
630
Chris Wilson0fb215a2009-10-02 04:31:34 +0100631/* drop the oldest entries that have been purged by the kernel */
632static void
633drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem,
634 struct drm_intel_gem_bo_bucket *bucket)
635{
Eric Anholtd70d6052009-10-06 12:40:42 -0700636 while (!DRMLISTEMPTY(&bucket->head)) {
637 drm_intel_bo_gem *bo_gem;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100638
Eric Anholtd70d6052009-10-06 12:40:42 -0700639 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
640 bucket->head.next, head);
Chris Wilson83a35b62009-11-11 13:04:38 +0000641 if (drm_intel_gem_bo_madvise_internal
Eric Anholtd70d6052009-10-06 12:40:42 -0700642 (bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
643 break;
Chris Wilson0fb215a2009-10-02 04:31:34 +0100644
Eric Anholtd70d6052009-10-06 12:40:42 -0700645 DRMLISTDEL(&bo_gem->head);
646 drm_intel_gem_bo_free(&bo_gem->bo);
647 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100648}
649
Eric Anholt4b982642008-10-30 09:33:07 -0700650static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700651drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr,
652 const char *name,
653 unsigned long size,
Chris Wilson1db22ff2010-06-21 14:27:23 +0100654 unsigned long flags,
655 uint32_t tiling_mode,
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700656 unsigned long stride,
657 unsigned int alignment)
Eric Anholt6a9eb082008-06-03 09:27:37 -0700658{
Eric Anholtd70d6052009-10-06 12:40:42 -0700659 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
660 drm_intel_bo_gem *bo_gem;
661 unsigned int page_size = getpagesize();
662 int ret;
663 struct drm_intel_gem_bo_bucket *bucket;
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700664 bool alloc_from_cache;
Eric Anholtd70d6052009-10-06 12:40:42 -0700665 unsigned long bo_size;
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700666 bool for_render = false;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700667
668 if (flags & BO_ALLOC_FOR_RENDER)
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700669 for_render = true;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700670
Eric Anholtd70d6052009-10-06 12:40:42 -0700671 /* Round the allocated size up to a power of two number of pages. */
672 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700673
Eric Anholtd70d6052009-10-06 12:40:42 -0700674 /* If we don't have caching at this size, don't actually round the
675 * allocation up.
676 */
677 if (bucket == NULL) {
678 bo_size = size;
679 if (bo_size < page_size)
680 bo_size = page_size;
Eric Anholt72abe982009-02-18 13:06:35 -0800681 } else {
Eric Anholtd70d6052009-10-06 12:40:42 -0700682 bo_size = bucket->size;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700683 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100684
Eric Anholtd70d6052009-10-06 12:40:42 -0700685 pthread_mutex_lock(&bufmgr_gem->lock);
686 /* Get a buffer out of the cache if available */
687retry:
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700688 alloc_from_cache = false;
Eric Anholtd70d6052009-10-06 12:40:42 -0700689 if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) {
690 if (for_render) {
691 /* Allocate new render-target BOs from the tail (MRU)
692 * of the list, as it will likely be hot in the GPU
693 * cache and in the aperture for us.
694 */
695 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
696 bucket->head.prev, head);
697 DRMLISTDEL(&bo_gem->head);
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700698 alloc_from_cache = true;
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700699 bo_gem->bo.align = alignment;
Eric Anholtd70d6052009-10-06 12:40:42 -0700700 } else {
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700701 assert(alignment == 0);
Eric Anholtd70d6052009-10-06 12:40:42 -0700702 /* For non-render-target BOs (where we're probably
703 * going to map it first thing in order to fill it
704 * with data), check if the last BO in the cache is
705 * unbusy, and only reuse in that case. Otherwise,
706 * allocating a new buffer is probably faster than
707 * waiting for the GPU to finish.
708 */
709 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
710 bucket->head.next, head);
711 if (!drm_intel_gem_bo_busy(&bo_gem->bo)) {
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700712 alloc_from_cache = true;
Eric Anholtd70d6052009-10-06 12:40:42 -0700713 DRMLISTDEL(&bo_gem->head);
714 }
715 }
716
717 if (alloc_from_cache) {
Chris Wilson83a35b62009-11-11 13:04:38 +0000718 if (!drm_intel_gem_bo_madvise_internal
Eric Anholtd70d6052009-10-06 12:40:42 -0700719 (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) {
720 drm_intel_gem_bo_free(&bo_gem->bo);
721 drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem,
722 bucket);
723 goto retry;
724 }
Chris Wilson1db22ff2010-06-21 14:27:23 +0100725
726 if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
727 tiling_mode,
728 stride)) {
729 drm_intel_gem_bo_free(&bo_gem->bo);
730 goto retry;
731 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700732 }
Chris Wilson0fb215a2009-10-02 04:31:34 +0100733 }
Eric Anholtd70d6052009-10-06 12:40:42 -0700734 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700735
Eric Anholtd70d6052009-10-06 12:40:42 -0700736 if (!alloc_from_cache) {
737 struct drm_i915_gem_create create;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700738
Eric Anholtd70d6052009-10-06 12:40:42 -0700739 bo_gem = calloc(1, sizeof(*bo_gem));
740 if (!bo_gem)
741 return NULL;
Keith Packarda919ff52008-06-05 15:58:09 -0700742
Eric Anholtd70d6052009-10-06 12:40:42 -0700743 bo_gem->bo.size = bo_size;
Chris Wilson90b23cc2012-02-09 10:23:10 +0000744
Daniel Vettereb7a5b62015-02-11 11:59:52 +0100745 memclear(create);
Eric Anholtd70d6052009-10-06 12:40:42 -0700746 create.size = bo_size;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700747
Chris Wilson62997222010-09-25 21:32:59 +0100748 ret = drmIoctl(bufmgr_gem->fd,
749 DRM_IOCTL_I915_GEM_CREATE,
750 &create);
Eric Anholtd70d6052009-10-06 12:40:42 -0700751 bo_gem->gem_handle = create.handle;
752 bo_gem->bo.handle = bo_gem->gem_handle;
753 if (ret != 0) {
754 free(bo_gem);
755 return NULL;
756 }
757 bo_gem->bo.bufmgr = bufmgr;
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700758 bo_gem->bo.align = alignment;
Chris Wilson1db22ff2010-06-21 14:27:23 +0100759
760 bo_gem->tiling_mode = I915_TILING_NONE;
761 bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
762 bo_gem->stride = 0;
763
Thomas Meyer4f44ecc2014-11-07 19:43:04 +0100764 /* drm_intel_gem_bo_free calls DRMLISTDEL() for an uninitialized
765 list (vma_list), so better set the list head here */
766 DRMINITLISTHEAD(&bo_gem->name_list);
767 DRMINITLISTHEAD(&bo_gem->vma_list);
Chris Wilson1db22ff2010-06-21 14:27:23 +0100768 if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
769 tiling_mode,
770 stride)) {
771 drm_intel_gem_bo_free(&bo_gem->bo);
772 return NULL;
773 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700774 }
Eric Anholt6a9eb082008-06-03 09:27:37 -0700775
Eric Anholtd70d6052009-10-06 12:40:42 -0700776 bo_gem->name = name;
777 atomic_set(&bo_gem->refcount, 1);
778 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -0700779 bo_gem->reloc_tree_fences = 0;
Eric Anholt2c2bdb32011-10-21 16:53:16 -0700780 bo_gem->used_as_reloc_target = false;
781 bo_gem->has_error = false;
782 bo_gem->reusable = true;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700783
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700784 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, alignment);
Chris Wilsone22fb792009-11-30 22:14:30 +0000785
Eric Anholtd70d6052009-10-06 12:40:42 -0700786 DBG("bo_create: buf %d (%s) %ldb\n",
787 bo_gem->gem_handle, bo_gem->name, size);
Eric Anholt6a9eb082008-06-03 09:27:37 -0700788
Eric Anholtd70d6052009-10-06 12:40:42 -0700789 return &bo_gem->bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -0700790}
791
Eric Anholt72abe982009-02-18 13:06:35 -0800792static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700793drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
794 const char *name,
795 unsigned long size,
796 unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -0800797{
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700798 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size,
Chris Wilson1db22ff2010-06-21 14:27:23 +0100799 BO_ALLOC_FOR_RENDER,
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700800 I915_TILING_NONE, 0,
801 alignment);
Eric Anholt72abe982009-02-18 13:06:35 -0800802}
803
804static drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -0700805drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr,
806 const char *name,
807 unsigned long size,
808 unsigned int alignment)
Eric Anholt72abe982009-02-18 13:06:35 -0800809{
Chris Wilson1db22ff2010-06-21 14:27:23 +0100810 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0,
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700811 I915_TILING_NONE, 0, 0);
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700812}
813
814static drm_intel_bo *
815drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
816 int x, int y, int cpp, uint32_t *tiling_mode,
817 unsigned long *pitch, unsigned long flags)
818{
819 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
Chris Wilsone65caeb2010-06-09 10:08:41 +0100820 unsigned long size, stride;
821 uint32_t tiling;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700822
Chris Wilsone65caeb2010-06-09 10:08:41 +0100823 do {
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100824 unsigned long aligned_y, height_alignment;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700825
Chris Wilsone65caeb2010-06-09 10:08:41 +0100826 tiling = *tiling_mode;
827
828 /* If we're tiled, our allocations are in 8 or 32-row blocks,
829 * so failure to align our height means that we won't allocate
830 * enough pages.
831 *
832 * If we're untiled, we still have to align to 2 rows high
833 * because the data port accesses 2x2 blocks even if the
834 * bottom row isn't to be rendered, so failure to align means
835 * we could walk off the end of the GTT and fault. This is
836 * documented on 965, and may be the case on older chipsets
837 * too so we try to be careful.
838 */
839 aligned_y = y;
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100840 height_alignment = 2;
841
Eric Anholt078bc5b2011-12-20 13:10:36 -0800842 if ((bufmgr_gem->gen == 2) && tiling != I915_TILING_NONE)
Daniel Vetter06ebbf72011-03-26 15:04:04 +0100843 height_alignment = 16;
Daniel Vetter194aa1b2011-09-22 22:20:53 +0200844 else if (tiling == I915_TILING_X
Eric Anholt078bc5b2011-12-20 13:10:36 -0800845 || (IS_915(bufmgr_gem->pci_device)
846 && tiling == I915_TILING_Y))
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100847 height_alignment = 8;
Chris Wilsone65caeb2010-06-09 10:08:41 +0100848 else if (tiling == I915_TILING_Y)
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100849 height_alignment = 32;
Daniel Vetter9a71ed92011-02-22 18:53:56 +0100850 aligned_y = ALIGN(y, height_alignment);
Chris Wilsone65caeb2010-06-09 10:08:41 +0100851
852 stride = x * cpp;
Chris Wilson726210f2010-06-24 11:38:00 +0100853 stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, tiling_mode);
Chris Wilsone65caeb2010-06-09 10:08:41 +0100854 size = stride * aligned_y;
855 size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode);
856 } while (*tiling_mode != tiling);
Chris Wilson6ea2bda2010-06-22 13:03:52 +0100857 *pitch = stride;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -0700858
Chris Wilson6ea2bda2010-06-22 13:03:52 +0100859 if (tiling == I915_TILING_NONE)
Chris Wilson5eec2862010-06-21 14:20:56 +0100860 stride = 0;
861
Chris Wilson6ea2bda2010-06-22 13:03:52 +0100862 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags,
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700863 tiling, stride, 0);
Eric Anholt72abe982009-02-18 13:06:35 -0800864}
865
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +0100866static drm_intel_bo *
867drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
868 const char *name,
869 void *addr,
870 uint32_t tiling_mode,
871 uint32_t stride,
872 unsigned long size,
873 unsigned long flags)
874{
875 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
876 drm_intel_bo_gem *bo_gem;
877 int ret;
878 struct drm_i915_gem_userptr userptr;
879
880 /* Tiling with userptr surfaces is not supported
881 * on all hardware so refuse it for time being.
882 */
883 if (tiling_mode != I915_TILING_NONE)
884 return NULL;
885
886 bo_gem = calloc(1, sizeof(*bo_gem));
887 if (!bo_gem)
888 return NULL;
889
890 bo_gem->bo.size = size;
891
Daniel Vettereb7a5b62015-02-11 11:59:52 +0100892 memclear(userptr);
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +0100893 userptr.user_ptr = (__u64)((unsigned long)addr);
894 userptr.user_size = size;
895 userptr.flags = flags;
896
897 ret = drmIoctl(bufmgr_gem->fd,
898 DRM_IOCTL_I915_GEM_USERPTR,
899 &userptr);
900 if (ret != 0) {
901 DBG("bo_create_userptr: "
902 "ioctl failed with user ptr %p size 0x%lx, "
903 "user flags 0x%lx\n", addr, size, flags);
904 free(bo_gem);
905 return NULL;
906 }
907
908 bo_gem->gem_handle = userptr.handle;
909 bo_gem->bo.handle = bo_gem->gem_handle;
910 bo_gem->bo.bufmgr = bufmgr;
911 bo_gem->is_userptr = true;
912 bo_gem->bo.virtual = addr;
913 /* Save the address provided by user */
914 bo_gem->user_virtual = addr;
915 bo_gem->tiling_mode = I915_TILING_NONE;
916 bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
917 bo_gem->stride = 0;
918
919 DRMINITLISTHEAD(&bo_gem->name_list);
920 DRMINITLISTHEAD(&bo_gem->vma_list);
921
922 bo_gem->name = name;
923 atomic_set(&bo_gem->refcount, 1);
924 bo_gem->validate_index = -1;
925 bo_gem->reloc_tree_fences = 0;
926 bo_gem->used_as_reloc_target = false;
927 bo_gem->has_error = false;
928 bo_gem->reusable = false;
929
Anuj Phogat5c68f9f2015-04-10 17:20:55 -0700930 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +0100931
932 DBG("bo_create_userptr: "
933 "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
934 addr, bo_gem->gem_handle, bo_gem->name,
935 size, stride, tiling_mode);
936
937 return &bo_gem->bo;
938}
939
Chris Wilson32258e42014-11-04 14:26:49 +0000940static bool
941has_userptr(drm_intel_bufmgr_gem *bufmgr_gem)
942{
943 int ret;
944 void *ptr;
945 long pgsz;
946 struct drm_i915_gem_userptr userptr;
Chris Wilson32258e42014-11-04 14:26:49 +0000947
948 pgsz = sysconf(_SC_PAGESIZE);
949 assert(pgsz > 0);
950
951 ret = posix_memalign(&ptr, pgsz, pgsz);
952 if (ret) {
953 DBG("Failed to get a page (%ld) for userptr detection!\n",
954 pgsz);
955 return false;
956 }
957
958 memclear(userptr);
959 userptr.user_ptr = (__u64)(unsigned long)ptr;
960 userptr.user_size = pgsz;
961
962retry:
963 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
964 if (ret) {
965 if (errno == ENODEV && userptr.flags == 0) {
966 userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
967 goto retry;
968 }
969 free(ptr);
970 return false;
971 }
972
Tvrtko Ursulin30921482015-04-17 11:57:28 +0100973 /* We don't release the userptr bo here as we want to keep the
974 * kernel mm tracking alive for our lifetime. The first time we
975 * create a userptr object the kernel has to install a mmu_notifer
976 * which is a heavyweight operation (e.g. it requires taking all
977 * mm_locks and stop_machine()).
978 */
979
980 bufmgr_gem->userptr_active.ptr = ptr;
981 bufmgr_gem->userptr_active.handle = userptr.handle;
Chris Wilson32258e42014-11-04 14:26:49 +0000982
983 return true;
984}
985
986static drm_intel_bo *
987check_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
988 const char *name,
989 void *addr,
990 uint32_t tiling_mode,
991 uint32_t stride,
992 unsigned long size,
993 unsigned long flags)
994{
995 if (has_userptr((drm_intel_bufmgr_gem *)bufmgr))
996 bufmgr->bo_alloc_userptr = drm_intel_gem_bo_alloc_userptr;
997 else
998 bufmgr->bo_alloc_userptr = NULL;
999
1000 return drm_intel_bo_alloc_userptr(bufmgr, name, addr,
1001 tiling_mode, stride, size, flags);
1002}
1003
Eric Anholt6a9eb082008-06-03 09:27:37 -07001004/**
Eric Anholt4b982642008-10-30 09:33:07 -07001005 * Returns a drm_intel_bo wrapping the given buffer object handle.
Eric Anholt6a9eb082008-06-03 09:27:37 -07001006 *
1007 * This can be used when one application needs to pass a buffer object
1008 * to another.
1009 */
Emil Velikov0f8da822015-03-31 22:32:11 +01001010drm_intel_bo *
Eric Anholtd70d6052009-10-06 12:40:42 -07001011drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
1012 const char *name,
Eric Anholt4b982642008-10-30 09:33:07 -07001013 unsigned int handle)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001014{
Eric Anholtd70d6052009-10-06 12:40:42 -07001015 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1016 drm_intel_bo_gem *bo_gem;
1017 int ret;
1018 struct drm_gem_open open_arg;
1019 struct drm_i915_gem_get_tiling get_tiling;
Chris Wilson36d49392011-02-14 09:39:06 +00001020 drmMMListHead *list;
1021
1022 /* At the moment most applications only have a few named bo.
1023 * For instance, in a DRI client only the render buffers passed
1024 * between X and the client are named. And since X returns the
1025 * alternating names for the front/back buffer a linear search
1026 * provides a sufficiently fast match.
1027 */
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001028 pthread_mutex_lock(&bufmgr_gem->lock);
Chris Wilson36d49392011-02-14 09:39:06 +00001029 for (list = bufmgr_gem->named.next;
1030 list != &bufmgr_gem->named;
1031 list = list->next) {
1032 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
1033 if (bo_gem->global_name == handle) {
1034 drm_intel_gem_bo_reference(&bo_gem->bo);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001035 pthread_mutex_unlock(&bufmgr_gem->lock);
Chris Wilson36d49392011-02-14 09:39:06 +00001036 return &bo_gem->bo;
1037 }
1038 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001039
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001040 memclear(open_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001041 open_arg.name = handle;
Chris Wilson62997222010-09-25 21:32:59 +01001042 ret = drmIoctl(bufmgr_gem->fd,
1043 DRM_IOCTL_GEM_OPEN,
1044 &open_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001045 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001046 DBG("Couldn't reference %s handle 0x%08x: %s\n",
1047 name, handle, strerror(errno));
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001048 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001049 return NULL;
1050 }
Keith Packardc3d96892013-11-22 05:31:01 -08001051 /* Now see if someone has used a prime handle to get this
1052 * object from the kernel before by looking through the list
1053 * again for a matching gem_handle
1054 */
1055 for (list = bufmgr_gem->named.next;
1056 list != &bufmgr_gem->named;
1057 list = list->next) {
1058 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
1059 if (bo_gem->gem_handle == open_arg.handle) {
1060 drm_intel_gem_bo_reference(&bo_gem->bo);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001061 pthread_mutex_unlock(&bufmgr_gem->lock);
Keith Packardc3d96892013-11-22 05:31:01 -08001062 return &bo_gem->bo;
1063 }
1064 }
1065
1066 bo_gem = calloc(1, sizeof(*bo_gem));
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001067 if (!bo_gem) {
1068 pthread_mutex_unlock(&bufmgr_gem->lock);
Keith Packardc3d96892013-11-22 05:31:01 -08001069 return NULL;
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001070 }
Keith Packardc3d96892013-11-22 05:31:01 -08001071
Eric Anholtd70d6052009-10-06 12:40:42 -07001072 bo_gem->bo.size = open_arg.size;
1073 bo_gem->bo.offset = 0;
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08001074 bo_gem->bo.offset64 = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07001075 bo_gem->bo.virtual = NULL;
1076 bo_gem->bo.bufmgr = bufmgr;
1077 bo_gem->name = name;
1078 atomic_set(&bo_gem->refcount, 1);
1079 bo_gem->validate_index = -1;
1080 bo_gem->gem_handle = open_arg.handle;
Chris Wilson53581b62011-02-14 09:27:05 +00001081 bo_gem->bo.handle = open_arg.handle;
Eric Anholtd70d6052009-10-06 12:40:42 -07001082 bo_gem->global_name = handle;
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001083 bo_gem->reusable = false;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001084
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001085 memclear(get_tiling);
Eric Anholtd70d6052009-10-06 12:40:42 -07001086 get_tiling.handle = bo_gem->gem_handle;
Chris Wilson62997222010-09-25 21:32:59 +01001087 ret = drmIoctl(bufmgr_gem->fd,
1088 DRM_IOCTL_I915_GEM_GET_TILING,
1089 &get_tiling);
Eric Anholtd70d6052009-10-06 12:40:42 -07001090 if (ret != 0) {
1091 drm_intel_gem_bo_unreference(&bo_gem->bo);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001092 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001093 return NULL;
1094 }
1095 bo_gem->tiling_mode = get_tiling.tiling_mode;
1096 bo_gem->swizzle_mode = get_tiling.swizzle_mode;
Chris Wilson056aa9b2010-06-21 14:31:29 +01001097 /* XXX stride is unknown */
Anuj Phogat5c68f9f2015-04-10 17:20:55 -07001098 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
Keith Packard18f091d2008-12-15 15:08:12 -08001099
Chris Wilsone4b60f22011-12-05 21:29:05 +00001100 DRMINITLISTHEAD(&bo_gem->vma_list);
Chris Wilson36d49392011-02-14 09:39:06 +00001101 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04001102 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001103 DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001104
Eric Anholtd70d6052009-10-06 12:40:42 -07001105 return &bo_gem->bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001106}
1107
1108static void
Eric Anholt4b982642008-10-30 09:33:07 -07001109drm_intel_gem_bo_free(drm_intel_bo *bo)
Eric Anholt500c81d2008-06-06 17:13:16 -07001110{
Eric Anholtd70d6052009-10-06 12:40:42 -07001111 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1112 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1113 struct drm_gem_close close;
1114 int ret;
Eric Anholt500c81d2008-06-06 17:13:16 -07001115
Chris Wilsone4b60f22011-12-05 21:29:05 +00001116 DRMLISTDEL(&bo_gem->vma_list);
1117 if (bo_gem->mem_virtual) {
Chris Wilson90b23cc2012-02-09 10:23:10 +00001118 VG(VALGRIND_FREELIKE_BLOCK(bo_gem->mem_virtual, 0));
Emil Velikov537b1ca2014-09-07 19:47:06 +01001119 drm_munmap(bo_gem->mem_virtual, bo_gem->bo.size);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001120 bufmgr_gem->vma_count--;
1121 }
1122 if (bo_gem->gtt_virtual) {
Emil Velikov537b1ca2014-09-07 19:47:06 +01001123 drm_munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001124 bufmgr_gem->vma_count--;
1125 }
1126
Eric Anholtd70d6052009-10-06 12:40:42 -07001127 /* Close this object */
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001128 memclear(close);
Eric Anholtd70d6052009-10-06 12:40:42 -07001129 close.handle = bo_gem->gem_handle;
Chris Wilson62997222010-09-25 21:32:59 +01001130 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
Eric Anholtd70d6052009-10-06 12:40:42 -07001131 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001132 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
1133 bo_gem->gem_handle, bo_gem->name, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001134 }
1135 free(bo);
Eric Anholt500c81d2008-06-06 17:13:16 -07001136}
1137
Chris Wilson23eeb7e2012-02-09 10:29:22 +00001138static void
1139drm_intel_gem_bo_mark_mmaps_incoherent(drm_intel_bo *bo)
1140{
1141#if HAVE_VALGRIND
1142 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1143
1144 if (bo_gem->mem_virtual)
1145 VALGRIND_MAKE_MEM_NOACCESS(bo_gem->mem_virtual, bo->size);
1146
1147 if (bo_gem->gtt_virtual)
1148 VALGRIND_MAKE_MEM_NOACCESS(bo_gem->gtt_virtual, bo->size);
1149#endif
1150}
1151
Eric Anholt3f3c5be2009-07-09 17:49:46 -07001152/** Frees all cached buffers significantly older than @time. */
1153static void
1154drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time)
1155{
Chris Wilson04495ee2009-10-02 04:39:22 +01001156 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001157
Chris Wilsonf16b4162010-06-21 15:21:48 +01001158 if (bufmgr_gem->time == time)
1159 return;
1160
Eric Anholt0ec768e2010-06-04 17:09:11 -07001161 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
Eric Anholtd70d6052009-10-06 12:40:42 -07001162 struct drm_intel_gem_bo_bucket *bucket =
1163 &bufmgr_gem->cache_bucket[i];
Chris Wilson04495ee2009-10-02 04:39:22 +01001164
Eric Anholtd70d6052009-10-06 12:40:42 -07001165 while (!DRMLISTEMPTY(&bucket->head)) {
1166 drm_intel_bo_gem *bo_gem;
Chris Wilson04495ee2009-10-02 04:39:22 +01001167
Eric Anholtd70d6052009-10-06 12:40:42 -07001168 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1169 bucket->head.next, head);
1170 if (time - bo_gem->free_time <= 1)
1171 break;
Chris Wilson04495ee2009-10-02 04:39:22 +01001172
Eric Anholtd70d6052009-10-06 12:40:42 -07001173 DRMLISTDEL(&bo_gem->head);
Chris Wilson04495ee2009-10-02 04:39:22 +01001174
Eric Anholtd70d6052009-10-06 12:40:42 -07001175 drm_intel_gem_bo_free(&bo_gem->bo);
1176 }
1177 }
Chris Wilsonf16b4162010-06-21 15:21:48 +01001178
1179 bufmgr_gem->time = time;
Chris Wilson04495ee2009-10-02 04:39:22 +01001180}
1181
Chris Wilsone4b60f22011-12-05 21:29:05 +00001182static void drm_intel_gem_bo_purge_vma_cache(drm_intel_bufmgr_gem *bufmgr_gem)
1183{
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001184 int limit;
1185
1186 DBG("%s: cached=%d, open=%d, limit=%d\n", __FUNCTION__,
1187 bufmgr_gem->vma_count, bufmgr_gem->vma_open, bufmgr_gem->vma_max);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001188
1189 if (bufmgr_gem->vma_max < 0)
1190 return;
1191
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001192 /* We may need to evict a few entries in order to create new mmaps */
1193 limit = bufmgr_gem->vma_max - 2*bufmgr_gem->vma_open;
1194 if (limit < 0)
1195 limit = 0;
1196
1197 while (bufmgr_gem->vma_count > limit) {
Chris Wilsone4b60f22011-12-05 21:29:05 +00001198 drm_intel_bo_gem *bo_gem;
1199
1200 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1201 bufmgr_gem->vma_cache.next,
1202 vma_list);
1203 assert(bo_gem->map_count == 0);
Chris Wilson0ab22512011-12-14 08:20:10 +00001204 DRMLISTDELINIT(&bo_gem->vma_list);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001205
1206 if (bo_gem->mem_virtual) {
Emil Velikov537b1ca2014-09-07 19:47:06 +01001207 drm_munmap(bo_gem->mem_virtual, bo_gem->bo.size);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001208 bo_gem->mem_virtual = NULL;
1209 bufmgr_gem->vma_count--;
1210 }
1211 if (bo_gem->gtt_virtual) {
Emil Velikov537b1ca2014-09-07 19:47:06 +01001212 drm_munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001213 bo_gem->gtt_virtual = NULL;
1214 bufmgr_gem->vma_count--;
1215 }
1216 }
1217}
1218
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001219static void drm_intel_gem_bo_close_vma(drm_intel_bufmgr_gem *bufmgr_gem,
1220 drm_intel_bo_gem *bo_gem)
Chris Wilsone4b60f22011-12-05 21:29:05 +00001221{
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001222 bufmgr_gem->vma_open--;
Chris Wilsone4b60f22011-12-05 21:29:05 +00001223 DRMLISTADDTAIL(&bo_gem->vma_list, &bufmgr_gem->vma_cache);
1224 if (bo_gem->mem_virtual)
1225 bufmgr_gem->vma_count++;
1226 if (bo_gem->gtt_virtual)
1227 bufmgr_gem->vma_count++;
1228 drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
1229}
1230
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001231static void drm_intel_gem_bo_open_vma(drm_intel_bufmgr_gem *bufmgr_gem,
1232 drm_intel_bo_gem *bo_gem)
Chris Wilsone4b60f22011-12-05 21:29:05 +00001233{
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001234 bufmgr_gem->vma_open++;
Chris Wilsone4b60f22011-12-05 21:29:05 +00001235 DRMLISTDEL(&bo_gem->vma_list);
1236 if (bo_gem->mem_virtual)
1237 bufmgr_gem->vma_count--;
1238 if (bo_gem->gtt_virtual)
1239 bufmgr_gem->vma_count--;
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001240 drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001241}
1242
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001243static void
1244drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time)
Chris Wilson04495ee2009-10-02 04:39:22 +01001245{
Eric Anholtd70d6052009-10-06 12:40:42 -07001246 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1247 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1248 struct drm_intel_gem_bo_bucket *bucket;
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001249 int i;
Chris Wilson04495ee2009-10-02 04:39:22 +01001250
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001251 /* Unreference all the target buffers */
1252 for (i = 0; i < bo_gem->reloc_count; i++) {
Eric Anholt4f7704a2010-06-10 08:58:08 -07001253 if (bo_gem->reloc_target_info[i].bo != bo) {
1254 drm_intel_gem_bo_unreference_locked_timed(bo_gem->
1255 reloc_target_info[i].bo,
1256 time);
1257 }
Eric Anholtd70d6052009-10-06 12:40:42 -07001258 }
Chris Wilsonb666f412009-11-30 23:07:19 +00001259 bo_gem->reloc_count = 0;
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001260 bo_gem->used_as_reloc_target = false;
Eric Anholtd70d6052009-10-06 12:40:42 -07001261
1262 DBG("bo_unreference final: %d (%s)\n",
1263 bo_gem->gem_handle, bo_gem->name);
1264
Chris Wilson57473c72009-12-02 13:36:22 +00001265 /* release memory associated with this object */
Jesse Barnesb5096402009-09-15 11:02:58 -07001266 if (bo_gem->reloc_target_info) {
1267 free(bo_gem->reloc_target_info);
1268 bo_gem->reloc_target_info = NULL;
Chris Wilson57473c72009-12-02 13:36:22 +00001269 }
1270 if (bo_gem->relocs) {
1271 free(bo_gem->relocs);
1272 bo_gem->relocs = NULL;
1273 }
1274
Chris Wilson5c5332b2011-12-05 10:39:49 +00001275 /* Clear any left-over mappings */
1276 if (bo_gem->map_count) {
1277 DBG("bo freed with non-zero map-count %d\n", bo_gem->map_count);
1278 bo_gem->map_count = 0;
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001279 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
Chris Wilson23eeb7e2012-02-09 10:29:22 +00001280 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
Chris Wilson5c5332b2011-12-05 10:39:49 +00001281 }
Chris Wilson5c5332b2011-12-05 10:39:49 +00001282
Chris Wilson36d49392011-02-14 09:39:06 +00001283 DRMLISTDEL(&bo_gem->name_list);
1284
Eric Anholtd70d6052009-10-06 12:40:42 -07001285 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
1286 /* Put the buffer into our internal cache for reuse if we can. */
Eric Anholtd70d6052009-10-06 12:40:42 -07001287 if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL &&
Chris Wilson60aa8032009-11-30 20:02:05 +00001288 drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem,
1289 I915_MADV_DONTNEED)) {
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001290 bo_gem->free_time = time;
Eric Anholtd70d6052009-10-06 12:40:42 -07001291
1292 bo_gem->name = NULL;
1293 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -07001294
1295 DRMLISTADDTAIL(&bo_gem->head, &bucket->head);
Eric Anholtd70d6052009-10-06 12:40:42 -07001296 } else {
1297 drm_intel_gem_bo_free(bo);
1298 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001299}
1300
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001301static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
1302 time_t time)
1303{
1304 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1305
1306 assert(atomic_read(&bo_gem->refcount) > 0);
Eric Anholtd70d6052009-10-06 12:40:42 -07001307 if (atomic_dec_and_test(&bo_gem->refcount))
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001308 drm_intel_gem_bo_unreference_final(bo, time);
Eric Anholtd70d6052009-10-06 12:40:42 -07001309}
1310
1311static void drm_intel_gem_bo_unreference(drm_intel_bo *bo)
1312{
1313 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1314
1315 assert(atomic_read(&bo_gem->refcount) > 0);
Lionel Landwerlin88025ad2014-09-12 13:48:37 +01001316
1317 if (atomic_add_unless(&bo_gem->refcount, -1, 1)) {
Eric Anholtd70d6052009-10-06 12:40:42 -07001318 drm_intel_bufmgr_gem *bufmgr_gem =
1319 (drm_intel_bufmgr_gem *) bo->bufmgr;
Eric Anholt0d7ad7e2009-10-20 14:19:38 -07001320 struct timespec time;
1321
1322 clock_gettime(CLOCK_MONOTONIC, &time);
1323
Eric Anholtd70d6052009-10-06 12:40:42 -07001324 pthread_mutex_lock(&bufmgr_gem->lock);
Lionel Landwerlin88025ad2014-09-12 13:48:37 +01001325
1326 if (atomic_dec_and_test(&bo_gem->refcount)) {
1327 drm_intel_gem_bo_unreference_final(bo, time.tv_sec);
1328 drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time.tv_sec);
1329 }
1330
Eric Anholtd70d6052009-10-06 12:40:42 -07001331 pthread_mutex_unlock(&bufmgr_gem->lock);
1332 }
1333}
1334
1335static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
1336{
1337 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1338 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1339 struct drm_i915_gem_set_domain set_domain;
1340 int ret;
1341
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01001342 if (bo_gem->is_userptr) {
1343 /* Return the same user ptr */
1344 bo->virtual = bo_gem->user_virtual;
1345 return 0;
1346 }
1347
Chris Wilsona3305b02010-05-13 08:24:28 +01001348 pthread_mutex_lock(&bufmgr_gem->lock);
1349
Chris Wilsone4b60f22011-12-05 21:29:05 +00001350 if (bo_gem->map_count++ == 0)
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001351 drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001352
Eric Anholtd70d6052009-10-06 12:40:42 -07001353 if (!bo_gem->mem_virtual) {
1354 struct drm_i915_gem_mmap mmap_arg;
Carl Worthafd245d2009-04-29 14:43:55 -07001355
Chris Wilson015286f2011-12-11 17:35:06 +00001356 DBG("bo_map: %d (%s), map_count=%d\n",
1357 bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
Eric Anholtd70d6052009-10-06 12:40:42 -07001358
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001359 memclear(mmap_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001360 mmap_arg.handle = bo_gem->gem_handle;
Eric Anholtd70d6052009-10-06 12:40:42 -07001361 mmap_arg.size = bo->size;
Chris Wilson62997222010-09-25 21:32:59 +01001362 ret = drmIoctl(bufmgr_gem->fd,
1363 DRM_IOCTL_I915_GEM_MMAP,
1364 &mmap_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001365 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001366 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001367 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1368 __FILE__, __LINE__, bo_gem->gem_handle,
1369 bo_gem->name, strerror(errno));
Chris Wilsone4b60f22011-12-05 21:29:05 +00001370 if (--bo_gem->map_count == 0)
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001371 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
Chris Wilsona3305b02010-05-13 08:24:28 +01001372 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07001373 return ret;
1374 }
Chris Wilson90b23cc2012-02-09 10:23:10 +00001375 VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg.addr_ptr, mmap_arg.size, 0, 1));
Eric Anholtd70d6052009-10-06 12:40:42 -07001376 bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
1377 }
1378 DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1379 bo_gem->mem_virtual);
1380 bo->virtual = bo_gem->mem_virtual;
1381
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001382 memclear(set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001383 set_domain.handle = bo_gem->gem_handle;
1384 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
1385 if (write_enable)
1386 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
1387 else
1388 set_domain.write_domain = 0;
Chris Wilson62997222010-09-25 21:32:59 +01001389 ret = drmIoctl(bufmgr_gem->fd,
1390 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1391 &set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001392 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001393 DBG("%s:%d: Error setting to CPU domain %d: %s\n",
1394 __FILE__, __LINE__, bo_gem->gem_handle,
1395 strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001396 }
1397
Eric Anholt4cb01ee2011-10-28 13:12:16 -07001398 if (write_enable)
1399 bo_gem->mapped_cpu_write = true;
1400
Chris Wilson23eeb7e2012-02-09 10:29:22 +00001401 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1402 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->mem_virtual, bo->size));
Chris Wilsona3305b02010-05-13 08:24:28 +01001403 pthread_mutex_unlock(&bufmgr_gem->lock);
1404
Eric Anholtd70d6052009-10-06 12:40:42 -07001405 return 0;
1406}
1407
Eric Anholt99c73372012-02-10 04:12:15 -08001408static int
1409map_gtt(drm_intel_bo *bo)
Eric Anholtd70d6052009-10-06 12:40:42 -07001410{
1411 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1412 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholtd70d6052009-10-06 12:40:42 -07001413 int ret;
1414
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01001415 if (bo_gem->is_userptr)
1416 return -EINVAL;
1417
Chris Wilsone4b60f22011-12-05 21:29:05 +00001418 if (bo_gem->map_count++ == 0)
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001419 drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
Chris Wilsone4b60f22011-12-05 21:29:05 +00001420
Eric Anholtd70d6052009-10-06 12:40:42 -07001421 /* Get a mapping of the buffer if we haven't before. */
1422 if (bo_gem->gtt_virtual == NULL) {
1423 struct drm_i915_gem_mmap_gtt mmap_arg;
1424
Chris Wilson015286f2011-12-11 17:35:06 +00001425 DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
1426 bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
Eric Anholtd70d6052009-10-06 12:40:42 -07001427
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001428 memclear(mmap_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001429 mmap_arg.handle = bo_gem->gem_handle;
1430
1431 /* Get the fake offset back... */
Chris Wilson62997222010-09-25 21:32:59 +01001432 ret = drmIoctl(bufmgr_gem->fd,
1433 DRM_IOCTL_I915_GEM_MMAP_GTT,
1434 &mmap_arg);
Eric Anholtd70d6052009-10-06 12:40:42 -07001435 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001436 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001437 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
1438 __FILE__, __LINE__,
1439 bo_gem->gem_handle, bo_gem->name,
1440 strerror(errno));
Chris Wilsonc5f0ed12011-12-13 10:30:54 +00001441 if (--bo_gem->map_count == 0)
1442 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
Eric Anholtd70d6052009-10-06 12:40:42 -07001443 return ret;
1444 }
1445
1446 /* and mmap it */
Emil Velikov537b1ca2014-09-07 19:47:06 +01001447 bo_gem->gtt_virtual = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
1448 MAP_SHARED, bufmgr_gem->fd,
1449 mmap_arg.offset);
Eric Anholtd70d6052009-10-06 12:40:42 -07001450 if (bo_gem->gtt_virtual == MAP_FAILED) {
Chris Wilson08371bc2009-12-08 22:35:24 +00001451 bo_gem->gtt_virtual = NULL;
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001452 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001453 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1454 __FILE__, __LINE__,
1455 bo_gem->gem_handle, bo_gem->name,
1456 strerror(errno));
Chris Wilsone4b60f22011-12-05 21:29:05 +00001457 if (--bo_gem->map_count == 0)
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001458 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001459 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001460 }
1461 }
1462
1463 bo->virtual = bo_gem->gtt_virtual;
1464
1465 DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1466 bo_gem->gtt_virtual);
1467
Eric Anholt99c73372012-02-10 04:12:15 -08001468 return 0;
1469}
1470
Emil Velikov0f8da822015-03-31 22:32:11 +01001471int
Maarten Lankhorst07fead42014-07-31 15:07:27 +02001472drm_intel_gem_bo_map_gtt(drm_intel_bo *bo)
Eric Anholt99c73372012-02-10 04:12:15 -08001473{
1474 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1475 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1476 struct drm_i915_gem_set_domain set_domain;
1477 int ret;
1478
1479 pthread_mutex_lock(&bufmgr_gem->lock);
1480
1481 ret = map_gtt(bo);
1482 if (ret) {
1483 pthread_mutex_unlock(&bufmgr_gem->lock);
1484 return ret;
1485 }
1486
1487 /* Now move it to the GTT domain so that the GPU and CPU
1488 * caches are flushed and the GPU isn't actively using the
1489 * buffer.
1490 *
1491 * The pagefault handler does this domain change for us when
1492 * it has unbound the BO from the GTT, but it's up to us to
1493 * tell it when we're about to use things if we had done
1494 * rendering and it still happens to be bound to the GTT.
1495 */
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001496 memclear(set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001497 set_domain.handle = bo_gem->gem_handle;
1498 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1499 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
Chris Wilson62997222010-09-25 21:32:59 +01001500 ret = drmIoctl(bufmgr_gem->fd,
1501 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1502 &set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001503 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001504 DBG("%s:%d: Error setting domain %d: %s\n",
1505 __FILE__, __LINE__, bo_gem->gem_handle,
1506 strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001507 }
1508
Chris Wilson23eeb7e2012-02-09 10:29:22 +00001509 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1510 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->gtt_virtual, bo->size));
Chris Wilsona3305b02010-05-13 08:24:28 +01001511 pthread_mutex_unlock(&bufmgr_gem->lock);
1512
Chris Wilsonc3ddfea2010-06-29 20:12:44 +01001513 return 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07001514}
1515
Eric Anholt99c73372012-02-10 04:12:15 -08001516/**
1517 * Performs a mapping of the buffer object like the normal GTT
1518 * mapping, but avoids waiting for the GPU to be done reading from or
1519 * rendering to the buffer.
1520 *
1521 * This is used in the implementation of GL_ARB_map_buffer_range: The
1522 * user asks to create a buffer, then does a mapping, fills some
1523 * space, runs a drawing command, then asks to map it again without
1524 * synchronizing because it guarantees that it won't write over the
1525 * data that the GPU is busy using (or, more specifically, that if it
1526 * does write over the data, it acknowledges that rendering is
1527 * undefined).
1528 */
1529
Emil Velikov0f8da822015-03-31 22:32:11 +01001530int
Maarten Lankhorst07fead42014-07-31 15:07:27 +02001531drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
Eric Anholt99c73372012-02-10 04:12:15 -08001532{
1533 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
Ben Widawsky743372e2013-12-26 16:30:09 -08001534#ifdef HAVE_VALGRIND
Chia-I Wufea54082013-07-10 10:49:59 +08001535 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Ben Widawsky743372e2013-12-26 16:30:09 -08001536#endif
Eric Anholt99c73372012-02-10 04:12:15 -08001537 int ret;
1538
1539 /* If the CPU cache isn't coherent with the GTT, then use a
1540 * regular synchronized mapping. The problem is that we don't
1541 * track where the buffer was last used on the CPU side in
1542 * terms of drm_intel_bo_map vs drm_intel_gem_bo_map_gtt, so
1543 * we would potentially corrupt the buffer even when the user
1544 * does reasonable things.
1545 */
1546 if (!bufmgr_gem->has_llc)
1547 return drm_intel_gem_bo_map_gtt(bo);
1548
1549 pthread_mutex_lock(&bufmgr_gem->lock);
Chia-I Wufea54082013-07-10 10:49:59 +08001550
Eric Anholt99c73372012-02-10 04:12:15 -08001551 ret = map_gtt(bo);
Chia-I Wufea54082013-07-10 10:49:59 +08001552 if (ret == 0) {
1553 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1554 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->gtt_virtual, bo->size));
1555 }
1556
Eric Anholt99c73372012-02-10 04:12:15 -08001557 pthread_mutex_unlock(&bufmgr_gem->lock);
1558
1559 return ret;
1560}
1561
Eric Anholtd70d6052009-10-06 12:40:42 -07001562static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
1563{
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01001564 drm_intel_bufmgr_gem *bufmgr_gem;
Eric Anholtd70d6052009-10-06 12:40:42 -07001565 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt4cb01ee2011-10-28 13:12:16 -07001566 int ret = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07001567
1568 if (bo == NULL)
1569 return 0;
1570
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01001571 if (bo_gem->is_userptr)
1572 return 0;
1573
1574 bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1575
Chris Wilsona3305b02010-05-13 08:24:28 +01001576 pthread_mutex_lock(&bufmgr_gem->lock);
1577
Chris Wilson015286f2011-12-11 17:35:06 +00001578 if (bo_gem->map_count <= 0) {
1579 DBG("attempted to unmap an unmapped bo\n");
1580 pthread_mutex_unlock(&bufmgr_gem->lock);
1581 /* Preserve the old behaviour of just treating this as a
1582 * no-op rather than reporting the error.
1583 */
1584 return 0;
1585 }
Chris Wilsone4b60f22011-12-05 21:29:05 +00001586
Eric Anholt4cb01ee2011-10-28 13:12:16 -07001587 if (bo_gem->mapped_cpu_write) {
Chris Wilson90b23cc2012-02-09 10:23:10 +00001588 struct drm_i915_gem_sw_finish sw_finish;
1589
Eric Anholt4cb01ee2011-10-28 13:12:16 -07001590 /* Cause a flush to happen if the buffer's pinned for
1591 * scanout, so the results show up in a timely manner.
1592 * Unlike GTT set domains, this only does work if the
1593 * buffer should be scanout-related.
1594 */
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001595 memclear(sw_finish);
Eric Anholt4cb01ee2011-10-28 13:12:16 -07001596 sw_finish.handle = bo_gem->gem_handle;
1597 ret = drmIoctl(bufmgr_gem->fd,
1598 DRM_IOCTL_I915_GEM_SW_FINISH,
1599 &sw_finish);
1600 ret = ret == -1 ? -errno : 0;
1601
1602 bo_gem->mapped_cpu_write = false;
1603 }
Eric Anholtd70d6052009-10-06 12:40:42 -07001604
Chris Wilsonc549a772011-12-05 10:14:34 +00001605 /* We need to unmap after every innovation as we cannot track
1606 * an open vma for every bo as that will exhaasut the system
1607 * limits and cause later failures.
1608 */
1609 if (--bo_gem->map_count == 0) {
Chris Wilsondd9a5b42011-12-06 13:12:37 +00001610 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
Chris Wilson23eeb7e2012-02-09 10:29:22 +00001611 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
Chris Wilsonc549a772011-12-05 10:14:34 +00001612 bo->virtual = NULL;
1613 }
Chris Wilsona3305b02010-05-13 08:24:28 +01001614 pthread_mutex_unlock(&bufmgr_gem->lock);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001615
1616 return ret;
Carl Worthafd245d2009-04-29 14:43:55 -07001617}
1618
Emil Velikov0f8da822015-03-31 22:32:11 +01001619int
Maarten Lankhorst07fead42014-07-31 15:07:27 +02001620drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo)
Eric Anholtd0ae6832011-10-28 13:13:08 -07001621{
1622 return drm_intel_gem_bo_unmap(bo);
1623}
1624
Eric Anholt6a9eb082008-06-03 09:27:37 -07001625static int
Eric Anholtd70d6052009-10-06 12:40:42 -07001626drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
1627 unsigned long size, const void *data)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001628{
Eric Anholtd70d6052009-10-06 12:40:42 -07001629 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1630 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1631 struct drm_i915_gem_pwrite pwrite;
1632 int ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001633
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01001634 if (bo_gem->is_userptr)
1635 return -EINVAL;
1636
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001637 memclear(pwrite);
Eric Anholtd70d6052009-10-06 12:40:42 -07001638 pwrite.handle = bo_gem->gem_handle;
1639 pwrite.offset = offset;
1640 pwrite.size = size;
1641 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
Chris Wilson62997222010-09-25 21:32:59 +01001642 ret = drmIoctl(bufmgr_gem->fd,
1643 DRM_IOCTL_I915_GEM_PWRITE,
1644 &pwrite);
Eric Anholtd70d6052009-10-06 12:40:42 -07001645 if (ret != 0) {
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001646 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001647 DBG("%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
1648 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1649 (int)size, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001650 }
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001651
1652 return ret;
Eric Anholtd70d6052009-10-06 12:40:42 -07001653}
1654
1655static int
1656drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
1657{
1658 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1659 struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id;
1660 int ret;
1661
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001662 memclear(get_pipe_from_crtc_id);
Eric Anholtd70d6052009-10-06 12:40:42 -07001663 get_pipe_from_crtc_id.crtc_id = crtc_id;
Chris Wilson62997222010-09-25 21:32:59 +01001664 ret = drmIoctl(bufmgr_gem->fd,
1665 DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
1666 &get_pipe_from_crtc_id);
Eric Anholtd70d6052009-10-06 12:40:42 -07001667 if (ret != 0) {
1668 /* We return -1 here to signal that we don't
1669 * know which pipe is associated with this crtc.
1670 * This lets the caller know that this information
1671 * isn't available; using the wrong pipe for
1672 * vblank waiting can cause the chipset to lock up
1673 */
1674 return -1;
1675 }
1676
1677 return get_pipe_from_crtc_id.pipe;
1678}
1679
1680static int
1681drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
1682 unsigned long size, void *data)
1683{
1684 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1685 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1686 struct drm_i915_gem_pread pread;
1687 int ret;
1688
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01001689 if (bo_gem->is_userptr)
1690 return -EINVAL;
1691
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001692 memclear(pread);
Eric Anholtd70d6052009-10-06 12:40:42 -07001693 pread.handle = bo_gem->gem_handle;
1694 pread.offset = offset;
1695 pread.size = size;
1696 pread.data_ptr = (uint64_t) (uintptr_t) data;
Chris Wilson62997222010-09-25 21:32:59 +01001697 ret = drmIoctl(bufmgr_gem->fd,
1698 DRM_IOCTL_I915_GEM_PREAD,
1699 &pread);
Eric Anholtd70d6052009-10-06 12:40:42 -07001700 if (ret != 0) {
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001701 ret = -errno;
Chris Wilson96214862010-10-01 16:50:09 +01001702 DBG("%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
1703 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1704 (int)size, strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001705 }
Chris Wilson3e21e3b2010-03-04 21:17:48 +00001706
Chris Wilsonacb4aa62009-12-02 12:40:26 +00001707 return ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001708}
1709
Eric Anholt877b2ce2010-11-09 13:51:45 -08001710/** Waits for all GPU rendering with the object to have completed. */
Eric Anholt6a9eb082008-06-03 09:27:37 -07001711static void
Eric Anholt4b982642008-10-30 09:33:07 -07001712drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001713{
Eric Anholt877b2ce2010-11-09 13:51:45 -08001714 drm_intel_gem_bo_start_gtt_access(bo, 1);
Eric Anholt6fb1ad72008-11-13 11:44:22 -08001715}
1716
1717/**
Ben Widawsky971c0802012-06-05 11:30:48 -07001718 * Waits on a BO for the given amount of time.
1719 *
1720 * @bo: buffer object to wait for
1721 * @timeout_ns: amount of time to wait in nanoseconds.
Daniel Vetterfcff9e22015-03-06 18:56:57 +01001722 * If value is less than 0, an infinite wait will occur.
Ben Widawsky971c0802012-06-05 11:30:48 -07001723 *
Daniel Vetterfcff9e22015-03-06 18:56:57 +01001724 * Returns 0 if the wait was successful ie. the last batch referencing the
1725 * object has completed within the allotted time. Otherwise some negative return
1726 * value describes the error. Of particular interest is -ETIME when the wait has
1727 * failed to yield the desired result.
Ben Widawsky971c0802012-06-05 11:30:48 -07001728 *
1729 * Similar to drm_intel_gem_bo_wait_rendering except a timeout parameter allows
1730 * the operation to give up after a certain amount of time. Another subtle
1731 * difference is the internal locking semantics are different (this variant does
1732 * not hold the lock for the duration of the wait). This makes the wait subject
1733 * to a larger userspace race window.
1734 *
1735 * The implementation shall wait until the object is no longer actively
1736 * referenced within a batch buffer at the time of the call. The wait will
1737 * not guarantee that the buffer is re-issued via another thread, or an flinked
1738 * handle. Userspace must make sure this race does not occur if such precision
1739 * is important.
Daniel Vetterfcff9e22015-03-06 18:56:57 +01001740 *
1741 * Note that some kernels have broken the inifite wait for negative values
1742 * promise, upgrade to latest stable kernels if this is the case.
Ben Widawsky971c0802012-06-05 11:30:48 -07001743 */
Emil Velikov0f8da822015-03-31 22:32:11 +01001744int
Maarten Lankhorst07fead42014-07-31 15:07:27 +02001745drm_intel_gem_bo_wait(drm_intel_bo *bo, int64_t timeout_ns)
Ben Widawsky971c0802012-06-05 11:30:48 -07001746{
1747 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1748 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1749 struct drm_i915_gem_wait wait;
1750 int ret;
1751
1752 if (!bufmgr_gem->has_wait_timeout) {
1753 DBG("%s:%d: Timed wait is not supported. Falling back to "
1754 "infinite wait\n", __FILE__, __LINE__);
1755 if (timeout_ns) {
1756 drm_intel_gem_bo_wait_rendering(bo);
1757 return 0;
1758 } else {
1759 return drm_intel_gem_bo_busy(bo) ? -ETIME : 0;
1760 }
1761 }
1762
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001763 memclear(wait);
Ben Widawsky971c0802012-06-05 11:30:48 -07001764 wait.bo_handle = bo_gem->gem_handle;
1765 wait.timeout_ns = timeout_ns;
Ben Widawsky971c0802012-06-05 11:30:48 -07001766 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
1767 if (ret == -1)
1768 return -errno;
1769
1770 return ret;
1771}
1772
1773/**
Eric Anholt6fb1ad72008-11-13 11:44:22 -08001774 * Sets the object to the GTT read and possibly write domain, used by the X
1775 * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
1776 *
1777 * In combination with drm_intel_gem_bo_pin() and manual fence management, we
1778 * can do tiled pixmaps this way.
1779 */
Emil Velikov0f8da822015-03-31 22:32:11 +01001780void
Eric Anholt6fb1ad72008-11-13 11:44:22 -08001781drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable)
1782{
Eric Anholtd70d6052009-10-06 12:40:42 -07001783 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1784 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1785 struct drm_i915_gem_set_domain set_domain;
1786 int ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001787
Daniel Vettereb7a5b62015-02-11 11:59:52 +01001788 memclear(set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001789 set_domain.handle = bo_gem->gem_handle;
1790 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1791 set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0;
Chris Wilson62997222010-09-25 21:32:59 +01001792 ret = drmIoctl(bufmgr_gem->fd,
1793 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1794 &set_domain);
Eric Anholtd70d6052009-10-06 12:40:42 -07001795 if (ret != 0) {
Chris Wilson96214862010-10-01 16:50:09 +01001796 DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
1797 __FILE__, __LINE__, bo_gem->gem_handle,
1798 set_domain.read_domains, set_domain.write_domain,
1799 strerror(errno));
Eric Anholtd70d6052009-10-06 12:40:42 -07001800 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001801}
1802
1803static void
Eric Anholt4b982642008-10-30 09:33:07 -07001804drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001805{
Eric Anholtd70d6052009-10-06 12:40:42 -07001806 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
Tvrtko Ursulin30921482015-04-17 11:57:28 +01001807 struct drm_gem_close close_bo;
1808 int i, ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001809
Jesse Barnesb5096402009-09-15 11:02:58 -07001810 free(bufmgr_gem->exec2_objects);
Eric Anholtd70d6052009-10-06 12:40:42 -07001811 free(bufmgr_gem->exec_objects);
1812 free(bufmgr_gem->exec_bos);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001813
Eric Anholtd70d6052009-10-06 12:40:42 -07001814 pthread_mutex_destroy(&bufmgr_gem->lock);
Eric Anholt6df7b072008-06-12 23:22:26 -07001815
Eric Anholtd70d6052009-10-06 12:40:42 -07001816 /* Free any cached buffer objects we were going to reuse */
Eric Anholt0ec768e2010-06-04 17:09:11 -07001817 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
Eric Anholtd70d6052009-10-06 12:40:42 -07001818 struct drm_intel_gem_bo_bucket *bucket =
1819 &bufmgr_gem->cache_bucket[i];
1820 drm_intel_bo_gem *bo_gem;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001821
Eric Anholtd70d6052009-10-06 12:40:42 -07001822 while (!DRMLISTEMPTY(&bucket->head)) {
1823 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1824 bucket->head.next, head);
1825 DRMLISTDEL(&bo_gem->head);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001826
Eric Anholtd70d6052009-10-06 12:40:42 -07001827 drm_intel_gem_bo_free(&bo_gem->bo);
1828 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001829 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001830
Tvrtko Ursulin30921482015-04-17 11:57:28 +01001831 /* Release userptr bo kept hanging around for optimisation. */
1832 if (bufmgr_gem->userptr_active.ptr) {
1833 memclear(close_bo);
1834 close_bo.handle = bufmgr_gem->userptr_active.handle;
1835 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
1836 free(bufmgr_gem->userptr_active.ptr);
1837 if (ret)
1838 fprintf(stderr,
1839 "Failed to release test userptr object! (%d) "
1840 "i915 kernel driver may not be sane!\n", errno);
1841 }
1842
Eric Anholtd70d6052009-10-06 12:40:42 -07001843 free(bufmgr);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001844}
1845
1846/**
1847 * Adds the target buffer to the validation list and adds the relocation
1848 * to the reloc_buffer's relocation list.
1849 *
1850 * The relocation entry at the given offset must already contain the
1851 * precomputed relocation value, because the kernel will optimize out
1852 * the relocation entry write when the buffer hasn't moved from the
1853 * last known offset in target_bo.
1854 */
1855static int
Jesse Barnesb5096402009-09-15 11:02:58 -07001856do_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1857 drm_intel_bo *target_bo, uint32_t target_offset,
1858 uint32_t read_domains, uint32_t write_domain,
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001859 bool need_fence)
Eric Anholt6a9eb082008-06-03 09:27:37 -07001860{
Eric Anholtd70d6052009-10-06 12:40:42 -07001861 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1862 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1863 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001864 bool fenced_command;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001865
Chris Wilson97077332009-12-01 23:01:34 +00001866 if (bo_gem->has_error)
Chris Wilson792fed12009-12-02 13:12:39 +00001867 return -ENOMEM;
Chris Wilson792fed12009-12-02 13:12:39 +00001868
1869 if (target_bo_gem->has_error) {
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001870 bo_gem->has_error = true;
Chris Wilson792fed12009-12-02 13:12:39 +00001871 return -ENOMEM;
1872 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07001873
Jesse Barnesb5096402009-09-15 11:02:58 -07001874 /* We never use HW fences for rendering on 965+ */
Eric Anholta1f9ea72010-03-02 08:49:36 -08001875 if (bufmgr_gem->gen >= 4)
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001876 need_fence = false;
Jesse Barnesb5096402009-09-15 11:02:58 -07001877
Chris Wilson537703f2010-12-07 20:34:22 +00001878 fenced_command = need_fence;
1879 if (target_bo_gem->tiling_mode == I915_TILING_NONE)
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001880 need_fence = false;
Chris Wilson537703f2010-12-07 20:34:22 +00001881
Eric Anholtd70d6052009-10-06 12:40:42 -07001882 /* Create a new relocation list if needed */
Chris Wilson97077332009-12-01 23:01:34 +00001883 if (bo_gem->relocs == NULL && drm_intel_setup_reloc_list(bo))
Chris Wilson792fed12009-12-02 13:12:39 +00001884 return -ENOMEM;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001885
Eric Anholtd70d6052009-10-06 12:40:42 -07001886 /* Check overflow */
1887 assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001888
Eric Anholtd70d6052009-10-06 12:40:42 -07001889 /* Check args */
1890 assert(offset <= bo->size - 4);
1891 assert((write_domain & (write_domain - 1)) == 0);
Eric Anholt6a9eb082008-06-03 09:27:37 -07001892
Chris Wilsonec65f8d2013-05-08 16:30:44 +01001893 /* An object needing a fence is a tiled buffer, so it won't have
1894 * relocs to other buffers.
1895 */
1896 if (need_fence) {
1897 assert(target_bo_gem->reloc_count == 0);
1898 target_bo_gem->reloc_tree_fences = 1;
1899 }
1900
Eric Anholtd70d6052009-10-06 12:40:42 -07001901 /* Make sure that we're not adding a reloc to something whose size has
1902 * already been accounted for.
1903 */
1904 assert(!bo_gem->used_as_reloc_target);
Eric Anholtf1791372010-06-07 14:22:36 -07001905 if (target_bo_gem != bo_gem) {
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001906 target_bo_gem->used_as_reloc_target = true;
Eric Anholtf1791372010-06-07 14:22:36 -07001907 bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size;
Chris Wilsonec65f8d2013-05-08 16:30:44 +01001908 bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences;
Eric Anholtf1791372010-06-07 14:22:36 -07001909 }
Eric Anholt0e867312008-10-21 00:10:54 -07001910
Eric Anholtd70d6052009-10-06 12:40:42 -07001911 bo_gem->relocs[bo_gem->reloc_count].offset = offset;
1912 bo_gem->relocs[bo_gem->reloc_count].delta = target_offset;
1913 bo_gem->relocs[bo_gem->reloc_count].target_handle =
1914 target_bo_gem->gem_handle;
1915 bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
1916 bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08001917 bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset64;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001918
Jesse Barnesb5096402009-09-15 11:02:58 -07001919 bo_gem->reloc_target_info[bo_gem->reloc_count].bo = target_bo;
Eric Anholt4f7704a2010-06-10 08:58:08 -07001920 if (target_bo != bo)
1921 drm_intel_gem_bo_reference(target_bo);
Chris Wilsonaf3d2822010-12-03 10:48:12 +00001922 if (fenced_command)
Jesse Barnesb5096402009-09-15 11:02:58 -07001923 bo_gem->reloc_target_info[bo_gem->reloc_count].flags =
1924 DRM_INTEL_RELOC_FENCE;
1925 else
1926 bo_gem->reloc_target_info[bo_gem->reloc_count].flags = 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001927
Eric Anholtd70d6052009-10-06 12:40:42 -07001928 bo_gem->reloc_count++;
Eric Anholt6df7b072008-06-12 23:22:26 -07001929
Eric Anholtd70d6052009-10-06 12:40:42 -07001930 return 0;
Eric Anholt6a9eb082008-06-03 09:27:37 -07001931}
1932
Jesse Barnesb5096402009-09-15 11:02:58 -07001933static int
1934drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1935 drm_intel_bo *target_bo, uint32_t target_offset,
1936 uint32_t read_domains, uint32_t write_domain)
1937{
1938 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
1939
1940 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1941 read_domains, write_domain,
1942 !bufmgr_gem->fenced_relocs);
1943}
1944
1945static int
1946drm_intel_gem_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
1947 drm_intel_bo *target_bo,
1948 uint32_t target_offset,
1949 uint32_t read_domains, uint32_t write_domain)
1950{
1951 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
Eric Anholt2c2bdb32011-10-21 16:53:16 -07001952 read_domains, write_domain, true);
Jesse Barnesb5096402009-09-15 11:02:58 -07001953}
1954
Emil Velikov0f8da822015-03-31 22:32:11 +01001955int
Eric Anholt515cea62011-10-21 18:48:20 -07001956drm_intel_gem_bo_get_reloc_count(drm_intel_bo *bo)
1957{
1958 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1959
1960 return bo_gem->reloc_count;
1961}
1962
1963/**
1964 * Removes existing relocation entries in the BO after "start".
1965 *
1966 * This allows a user to avoid a two-step process for state setup with
1967 * counting up all the buffer objects and doing a
1968 * drm_intel_bufmgr_check_aperture_space() before emitting any of the
1969 * relocations for the state setup. Instead, save the state of the
1970 * batchbuffer including drm_intel_gem_get_reloc_count(), emit all the
1971 * state, and then check if it still fits in the aperture.
1972 *
1973 * Any further drm_intel_bufmgr_check_aperture_space() queries
1974 * involving this buffer in the tree are undefined after this call.
1975 */
Emil Velikov0f8da822015-03-31 22:32:11 +01001976void
Eric Anholt515cea62011-10-21 18:48:20 -07001977drm_intel_gem_bo_clear_relocs(drm_intel_bo *bo, int start)
1978{
Lionel Landwerlin86b37c62014-09-12 13:48:38 +01001979 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
Eric Anholt515cea62011-10-21 18:48:20 -07001980 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1981 int i;
1982 struct timespec time;
1983
1984 clock_gettime(CLOCK_MONOTONIC, &time);
1985
1986 assert(bo_gem->reloc_count >= start);
Lionel Landwerlin86b37c62014-09-12 13:48:38 +01001987
Eric Anholt515cea62011-10-21 18:48:20 -07001988 /* Unreference the cleared target buffers */
Lionel Landwerlin86b37c62014-09-12 13:48:38 +01001989 pthread_mutex_lock(&bufmgr_gem->lock);
1990
Eric Anholt515cea62011-10-21 18:48:20 -07001991 for (i = start; i < bo_gem->reloc_count; i++) {
Chris Wilsonfdda9702013-01-11 00:55:12 +00001992 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) bo_gem->reloc_target_info[i].bo;
1993 if (&target_bo_gem->bo != bo) {
1994 bo_gem->reloc_tree_fences -= target_bo_gem->reloc_tree_fences;
1995 drm_intel_gem_bo_unreference_locked_timed(&target_bo_gem->bo,
Eric Anholt515cea62011-10-21 18:48:20 -07001996 time.tv_sec);
1997 }
1998 }
1999 bo_gem->reloc_count = start;
Lionel Landwerlin86b37c62014-09-12 13:48:38 +01002000
2001 pthread_mutex_unlock(&bufmgr_gem->lock);
2002
Eric Anholt515cea62011-10-21 18:48:20 -07002003}
2004
Eric Anholt6a9eb082008-06-03 09:27:37 -07002005/**
2006 * Walk the tree of relocations rooted at BO and accumulate the list of
2007 * validations to be performed and update the relocation buffers with
2008 * index values into the validation list.
2009 */
2010static void
Eric Anholt4b982642008-10-30 09:33:07 -07002011drm_intel_gem_bo_process_reloc(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002012{
Eric Anholtd70d6052009-10-06 12:40:42 -07002013 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2014 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002015
Eric Anholtd70d6052009-10-06 12:40:42 -07002016 if (bo_gem->relocs == NULL)
2017 return;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002018
Eric Anholtd70d6052009-10-06 12:40:42 -07002019 for (i = 0; i < bo_gem->reloc_count; i++) {
Jesse Barnesb5096402009-09-15 11:02:58 -07002020 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002021
Eric Anholtf1791372010-06-07 14:22:36 -07002022 if (target_bo == bo)
2023 continue;
2024
Chris Wilson23eeb7e2012-02-09 10:29:22 +00002025 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
2026
Eric Anholtd70d6052009-10-06 12:40:42 -07002027 /* Continue walking the tree depth-first. */
2028 drm_intel_gem_bo_process_reloc(target_bo);
Eric Anholt6a9eb082008-06-03 09:27:37 -07002029
Eric Anholtd70d6052009-10-06 12:40:42 -07002030 /* Add the target to the validate list */
2031 drm_intel_add_validate_buffer(target_bo);
2032 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002033}
2034
Eric Anholt6a9eb082008-06-03 09:27:37 -07002035static void
Jesse Barnesb5096402009-09-15 11:02:58 -07002036drm_intel_gem_bo_process_reloc2(drm_intel_bo *bo)
2037{
2038 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2039 int i;
2040
2041 if (bo_gem->relocs == NULL)
2042 return;
2043
2044 for (i = 0; i < bo_gem->reloc_count; i++) {
2045 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
2046 int need_fence;
2047
Eric Anholtf1791372010-06-07 14:22:36 -07002048 if (target_bo == bo)
2049 continue;
2050
Chris Wilson23eeb7e2012-02-09 10:29:22 +00002051 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
2052
Jesse Barnesb5096402009-09-15 11:02:58 -07002053 /* Continue walking the tree depth-first. */
2054 drm_intel_gem_bo_process_reloc2(target_bo);
2055
2056 need_fence = (bo_gem->reloc_target_info[i].flags &
2057 DRM_INTEL_RELOC_FENCE);
2058
2059 /* Add the target to the validate list */
2060 drm_intel_add_validate_buffer2(target_bo, need_fence);
2061 }
2062}
2063
2064
2065static void
Eric Anholtd70d6052009-10-06 12:40:42 -07002066drm_intel_update_buffer_offsets(drm_intel_bufmgr_gem *bufmgr_gem)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002067{
Eric Anholtd70d6052009-10-06 12:40:42 -07002068 int i;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002069
Eric Anholtd70d6052009-10-06 12:40:42 -07002070 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2071 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2072 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002073
Eric Anholtd70d6052009-10-06 12:40:42 -07002074 /* Update the buffer offset */
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002075 if (bufmgr_gem->exec_objects[i].offset != bo->offset64) {
Eric Anholtd70d6052009-10-06 12:40:42 -07002076 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002077 bo_gem->gem_handle, bo_gem->name, bo->offset64,
Eric Anholtd70d6052009-10-06 12:40:42 -07002078 (unsigned long long)bufmgr_gem->exec_objects[i].
2079 offset);
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002080 bo->offset64 = bufmgr_gem->exec_objects[i].offset;
Eric Anholtd70d6052009-10-06 12:40:42 -07002081 bo->offset = bufmgr_gem->exec_objects[i].offset;
2082 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002083 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002084}
2085
Jesse Barnesb5096402009-09-15 11:02:58 -07002086static void
2087drm_intel_update_buffer_offsets2 (drm_intel_bufmgr_gem *bufmgr_gem)
2088{
2089 int i;
2090
2091 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2092 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2093 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2094
2095 /* Update the buffer offset */
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002096 if (bufmgr_gem->exec2_objects[i].offset != bo->offset64) {
Jesse Barnesb5096402009-09-15 11:02:58 -07002097 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002098 bo_gem->gem_handle, bo_gem->name, bo->offset64,
Jesse Barnesb5096402009-09-15 11:02:58 -07002099 (unsigned long long)bufmgr_gem->exec2_objects[i].offset);
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002100 bo->offset64 = bufmgr_gem->exec2_objects[i].offset;
Jesse Barnesb5096402009-09-15 11:02:58 -07002101 bo->offset = bufmgr_gem->exec2_objects[i].offset;
2102 }
2103 }
2104}
2105
Emil Velikov0f8da822015-03-31 22:32:11 +01002106void
Eric Anholt4db16a92011-10-11 15:59:03 -07002107drm_intel_gem_bo_aub_dump_bmp(drm_intel_bo *bo,
2108 int x1, int y1, int width, int height,
2109 enum aub_dump_bmp_format format,
2110 int pitch, int offset)
2111{
Eric Anholt4db16a92011-10-11 15:59:03 -07002112}
2113
Eric Anholtf9d98be2008-09-08 08:51:40 -07002114static int
Eric Anholt4b982642008-10-30 09:33:07 -07002115drm_intel_gem_bo_exec(drm_intel_bo *bo, int used,
Eric Anholtd70d6052009-10-06 12:40:42 -07002116 drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002117{
Eric Anholtd70d6052009-10-06 12:40:42 -07002118 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
Chris Wilson792fed12009-12-02 13:12:39 +00002119 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholtd70d6052009-10-06 12:40:42 -07002120 struct drm_i915_gem_execbuffer execbuf;
2121 int ret, i;
Eric Anholtf9d98be2008-09-08 08:51:40 -07002122
Chris Wilson792fed12009-12-02 13:12:39 +00002123 if (bo_gem->has_error)
2124 return -ENOMEM;
2125
Eric Anholtd70d6052009-10-06 12:40:42 -07002126 pthread_mutex_lock(&bufmgr_gem->lock);
2127 /* Update indices and set up the validate list. */
2128 drm_intel_gem_bo_process_reloc(bo);
Eric Anholtf9d98be2008-09-08 08:51:40 -07002129
Eric Anholtd70d6052009-10-06 12:40:42 -07002130 /* Add the batch buffer to the validation list. There are no
2131 * relocations pointing to it.
2132 */
2133 drm_intel_add_validate_buffer(bo);
Eric Anholtf9d98be2008-09-08 08:51:40 -07002134
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002135 memclear(execbuf);
Eric Anholtd70d6052009-10-06 12:40:42 -07002136 execbuf.buffers_ptr = (uintptr_t) bufmgr_gem->exec_objects;
2137 execbuf.buffer_count = bufmgr_gem->exec_count;
2138 execbuf.batch_start_offset = 0;
2139 execbuf.batch_len = used;
2140 execbuf.cliprects_ptr = (uintptr_t) cliprects;
2141 execbuf.num_cliprects = num_cliprects;
2142 execbuf.DR1 = 0;
2143 execbuf.DR4 = DR4;
Eric Anholtf9d98be2008-09-08 08:51:40 -07002144
Chris Wilson62997222010-09-25 21:32:59 +01002145 ret = drmIoctl(bufmgr_gem->fd,
2146 DRM_IOCTL_I915_GEM_EXECBUFFER,
2147 &execbuf);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00002148 if (ret != 0) {
2149 ret = -errno;
2150 if (errno == ENOSPC) {
Chris Wilson96214862010-10-01 16:50:09 +01002151 DBG("Execbuffer fails to pin. "
2152 "Estimate: %u. Actual: %u. Available: %u\n",
2153 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
2154 bufmgr_gem->
2155 exec_count),
2156 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
2157 bufmgr_gem->
2158 exec_count),
2159 (unsigned int)bufmgr_gem->gtt_size);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00002160 }
Eric Anholtd70d6052009-10-06 12:40:42 -07002161 }
2162 drm_intel_update_buffer_offsets(bufmgr_gem);
Eric Anholt6a9eb082008-06-03 09:27:37 -07002163
Eric Anholtd70d6052009-10-06 12:40:42 -07002164 if (bufmgr_gem->bufmgr.debug)
2165 drm_intel_gem_dump_validation_list(bufmgr_gem);
Eric Anholt6a9eb082008-06-03 09:27:37 -07002166
Eric Anholtd70d6052009-10-06 12:40:42 -07002167 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2168 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2169 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002170
Eric Anholt02f93c22014-01-15 00:38:39 -08002171 bo_gem->idle = false;
2172
Eric Anholtd70d6052009-10-06 12:40:42 -07002173 /* Disconnect the buffer from the validate list */
2174 bo_gem->validate_index = -1;
Eric Anholtd70d6052009-10-06 12:40:42 -07002175 bufmgr_gem->exec_bos[i] = NULL;
2176 }
2177 bufmgr_gem->exec_count = 0;
2178 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtf9d98be2008-09-08 08:51:40 -07002179
Chris Wilsonacb4aa62009-12-02 12:40:26 +00002180 return ret;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002181}
2182
Keith Packard8e41ce12008-08-04 00:34:08 -07002183static int
Ben Widawsky3ed38712012-03-18 18:28:28 -07002184do_exec2(drm_intel_bo *bo, int used, drm_intel_context *ctx,
2185 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
2186 unsigned int flags)
Jesse Barnesb5096402009-09-15 11:02:58 -07002187{
2188 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
2189 struct drm_i915_gem_execbuffer2 execbuf;
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002190 int ret = 0;
2191 int i;
Jesse Barnesb5096402009-09-15 11:02:58 -07002192
Chris Wilson0184bb12010-12-19 13:01:15 +00002193 switch (flags & 0x7) {
Chris Wilson057fab32010-10-26 11:35:11 +01002194 default:
Zou Nan hai66375fd2010-06-02 10:07:37 +08002195 return -EINVAL;
Chris Wilson057fab32010-10-26 11:35:11 +01002196 case I915_EXEC_BLT:
2197 if (!bufmgr_gem->has_blt)
2198 return -EINVAL;
2199 break;
2200 case I915_EXEC_BSD:
2201 if (!bufmgr_gem->has_bsd)
2202 return -EINVAL;
2203 break;
Xiang, Haihao01199992012-11-14 12:46:39 +08002204 case I915_EXEC_VEBOX:
2205 if (!bufmgr_gem->has_vebox)
2206 return -EINVAL;
2207 break;
Chris Wilson057fab32010-10-26 11:35:11 +01002208 case I915_EXEC_RENDER:
2209 case I915_EXEC_DEFAULT:
2210 break;
2211 }
Zou Nan hai66375fd2010-06-02 10:07:37 +08002212
Jesse Barnesb5096402009-09-15 11:02:58 -07002213 pthread_mutex_lock(&bufmgr_gem->lock);
2214 /* Update indices and set up the validate list. */
2215 drm_intel_gem_bo_process_reloc2(bo);
2216
2217 /* Add the batch buffer to the validation list. There are no relocations
2218 * pointing to it.
2219 */
2220 drm_intel_add_validate_buffer2(bo, 0);
2221
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002222 memclear(execbuf);
Jesse Barnesb5096402009-09-15 11:02:58 -07002223 execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec2_objects;
2224 execbuf.buffer_count = bufmgr_gem->exec_count;
2225 execbuf.batch_start_offset = 0;
2226 execbuf.batch_len = used;
2227 execbuf.cliprects_ptr = (uintptr_t)cliprects;
2228 execbuf.num_cliprects = num_cliprects;
2229 execbuf.DR1 = 0;
2230 execbuf.DR4 = DR4;
Chris Wilson0184bb12010-12-19 13:01:15 +00002231 execbuf.flags = flags;
Ben Widawsky3ed38712012-03-18 18:28:28 -07002232 if (ctx == NULL)
2233 i915_execbuffer2_set_context_id(execbuf, 0);
2234 else
2235 i915_execbuffer2_set_context_id(execbuf, ctx->ctx_id);
Jesse Barnesb5096402009-09-15 11:02:58 -07002236 execbuf.rsvd2 = 0;
2237
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002238 if (bufmgr_gem->no_exec)
2239 goto skip_execution;
2240
Chris Wilson62997222010-09-25 21:32:59 +01002241 ret = drmIoctl(bufmgr_gem->fd,
2242 DRM_IOCTL_I915_GEM_EXECBUFFER2,
2243 &execbuf);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00002244 if (ret != 0) {
2245 ret = -errno;
Chris Wilson13e82702010-06-21 15:38:06 +01002246 if (ret == -ENOSPC) {
Chris Wilson96214862010-10-01 16:50:09 +01002247 DBG("Execbuffer fails to pin. "
2248 "Estimate: %u. Actual: %u. Available: %u\n",
2249 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
2250 bufmgr_gem->exec_count),
2251 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
2252 bufmgr_gem->exec_count),
2253 (unsigned int) bufmgr_gem->gtt_size);
Chris Wilson3e21e3b2010-03-04 21:17:48 +00002254 }
Jesse Barnesb5096402009-09-15 11:02:58 -07002255 }
2256 drm_intel_update_buffer_offsets2(bufmgr_gem);
2257
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002258skip_execution:
Jesse Barnesb5096402009-09-15 11:02:58 -07002259 if (bufmgr_gem->bufmgr.debug)
2260 drm_intel_gem_dump_validation_list(bufmgr_gem);
2261
2262 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2263 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2264 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2265
Eric Anholt02f93c22014-01-15 00:38:39 -08002266 bo_gem->idle = false;
2267
Jesse Barnesb5096402009-09-15 11:02:58 -07002268 /* Disconnect the buffer from the validate list */
2269 bo_gem->validate_index = -1;
2270 bufmgr_gem->exec_bos[i] = NULL;
2271 }
2272 bufmgr_gem->exec_count = 0;
2273 pthread_mutex_unlock(&bufmgr_gem->lock);
2274
Chris Wilson3e21e3b2010-03-04 21:17:48 +00002275 return ret;
Jesse Barnesb5096402009-09-15 11:02:58 -07002276}
2277
2278static int
Zou Nan hai66375fd2010-06-02 10:07:37 +08002279drm_intel_gem_bo_exec2(drm_intel_bo *bo, int used,
2280 drm_clip_rect_t *cliprects, int num_cliprects,
2281 int DR4)
2282{
Ben Widawsky3ed38712012-03-18 18:28:28 -07002283 return do_exec2(bo, used, NULL, cliprects, num_cliprects, DR4,
2284 I915_EXEC_RENDER);
2285}
2286
2287static int
2288drm_intel_gem_bo_mrb_exec2(drm_intel_bo *bo, int used,
2289 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
2290 unsigned int flags)
2291{
2292 return do_exec2(bo, used, NULL, cliprects, num_cliprects, DR4,
2293 flags);
2294}
2295
Emil Velikov0f8da822015-03-31 22:32:11 +01002296int
Ben Widawsky3ed38712012-03-18 18:28:28 -07002297drm_intel_gem_bo_context_exec(drm_intel_bo *bo, drm_intel_context *ctx,
2298 int used, unsigned int flags)
2299{
2300 return do_exec2(bo, used, ctx, NULL, 0, 0, flags);
Zou Nan hai66375fd2010-06-02 10:07:37 +08002301}
2302
2303static int
Eric Anholt4b982642008-10-30 09:33:07 -07002304drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment)
Keith Packard8e41ce12008-08-04 00:34:08 -07002305{
Eric Anholtd70d6052009-10-06 12:40:42 -07002306 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2307 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2308 struct drm_i915_gem_pin pin;
2309 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07002310
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002311 memclear(pin);
Eric Anholtd70d6052009-10-06 12:40:42 -07002312 pin.handle = bo_gem->gem_handle;
2313 pin.alignment = alignment;
Keith Packard8e41ce12008-08-04 00:34:08 -07002314
Chris Wilson62997222010-09-25 21:32:59 +01002315 ret = drmIoctl(bufmgr_gem->fd,
2316 DRM_IOCTL_I915_GEM_PIN,
2317 &pin);
Eric Anholtd70d6052009-10-06 12:40:42 -07002318 if (ret != 0)
2319 return -errno;
Keith Packard8e41ce12008-08-04 00:34:08 -07002320
Kenneth Graunkeedf17db2014-01-13 14:14:36 -08002321 bo->offset64 = pin.offset;
Eric Anholtd70d6052009-10-06 12:40:42 -07002322 bo->offset = pin.offset;
2323 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07002324}
2325
2326static int
Eric Anholt4b982642008-10-30 09:33:07 -07002327drm_intel_gem_bo_unpin(drm_intel_bo *bo)
Keith Packard8e41ce12008-08-04 00:34:08 -07002328{
Eric Anholtd70d6052009-10-06 12:40:42 -07002329 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2330 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2331 struct drm_i915_gem_unpin unpin;
2332 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07002333
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002334 memclear(unpin);
Eric Anholtd70d6052009-10-06 12:40:42 -07002335 unpin.handle = bo_gem->gem_handle;
Keith Packard8e41ce12008-08-04 00:34:08 -07002336
Chris Wilson62997222010-09-25 21:32:59 +01002337 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
Eric Anholtd70d6052009-10-06 12:40:42 -07002338 if (ret != 0)
2339 return -errno;
Keith Packard8e41ce12008-08-04 00:34:08 -07002340
Eric Anholtd70d6052009-10-06 12:40:42 -07002341 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07002342}
2343
2344static int
Chris Wilson1db22ff2010-06-21 14:27:23 +01002345drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
2346 uint32_t tiling_mode,
2347 uint32_t stride)
Keith Packard8e41ce12008-08-04 00:34:08 -07002348{
Eric Anholtd70d6052009-10-06 12:40:42 -07002349 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2350 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2351 struct drm_i915_gem_set_tiling set_tiling;
2352 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07002353
Chris Wilsonaba35022010-06-22 13:00:22 +01002354 if (bo_gem->global_name == 0 &&
2355 tiling_mode == bo_gem->tiling_mode &&
Chris Wilson056aa9b2010-06-21 14:31:29 +01002356 stride == bo_gem->stride)
Eric Anholtd70d6052009-10-06 12:40:42 -07002357 return 0;
Keith Packard18f091d2008-12-15 15:08:12 -08002358
Eric Anholtd70d6052009-10-06 12:40:42 -07002359 memset(&set_tiling, 0, sizeof(set_tiling));
Chris Wilson8ffd2e12009-12-01 13:08:04 +00002360 do {
Chris Wilson62997222010-09-25 21:32:59 +01002361 /* set_tiling is slightly broken and overwrites the
2362 * input on the error path, so we have to open code
2363 * rmIoctl.
2364 */
Chris Wilson1db22ff2010-06-21 14:27:23 +01002365 set_tiling.handle = bo_gem->gem_handle;
2366 set_tiling.tiling_mode = tiling_mode;
Chris Wilson4f0f8712010-02-10 09:45:13 +00002367 set_tiling.stride = stride;
2368
Chris Wilson8ffd2e12009-12-01 13:08:04 +00002369 ret = ioctl(bufmgr_gem->fd,
2370 DRM_IOCTL_I915_GEM_SET_TILING,
2371 &set_tiling);
Chris Wilson62997222010-09-25 21:32:59 +01002372 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
Chris Wilson1db22ff2010-06-21 14:27:23 +01002373 if (ret == -1)
2374 return -errno;
2375
2376 bo_gem->tiling_mode = set_tiling.tiling_mode;
2377 bo_gem->swizzle_mode = set_tiling.swizzle_mode;
Chris Wilsonaba35022010-06-22 13:00:22 +01002378 bo_gem->stride = set_tiling.stride;
Chris Wilson1db22ff2010-06-21 14:27:23 +01002379 return 0;
2380}
2381
2382static int
2383drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
2384 uint32_t stride)
2385{
2386 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2387 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2388 int ret;
2389
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01002390 /* Tiling with userptr surfaces is not supported
2391 * on all hardware so refuse it for time being.
2392 */
2393 if (bo_gem->is_userptr)
2394 return -EINVAL;
2395
Chris Wilsoncd34cbe2010-06-22 11:07:26 +01002396 /* Linear buffers have no stride. By ensuring that we only ever use
2397 * stride 0 with linear buffers, we simplify our code.
2398 */
Chris Wilsonc7bbaca2010-06-22 11:15:56 +01002399 if (*tiling_mode == I915_TILING_NONE)
Chris Wilsoncd34cbe2010-06-22 11:07:26 +01002400 stride = 0;
2401
Chris Wilson1db22ff2010-06-21 14:27:23 +01002402 ret = drm_intel_gem_bo_set_tiling_internal(bo, *tiling_mode, stride);
2403 if (ret == 0)
Anuj Phogat5c68f9f2015-04-10 17:20:55 -07002404 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
Chris Wilsone22fb792009-11-30 22:14:30 +00002405
Keith Packard18f091d2008-12-15 15:08:12 -08002406 *tiling_mode = bo_gem->tiling_mode;
Chris Wilsonfcf3e612010-05-24 18:35:41 +01002407 return ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07002408}
2409
2410static int
Eric Anholtd70d6052009-10-06 12:40:42 -07002411drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
2412 uint32_t * swizzle_mode)
Keith Packard8e41ce12008-08-04 00:34:08 -07002413{
Eric Anholtd70d6052009-10-06 12:40:42 -07002414 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholt99338382008-10-14 13:18:11 -07002415
Eric Anholtd70d6052009-10-06 12:40:42 -07002416 *tiling_mode = bo_gem->tiling_mode;
2417 *swizzle_mode = bo_gem->swizzle_mode;
2418 return 0;
Eric Anholt99338382008-10-14 13:18:11 -07002419}
2420
Emil Velikov0f8da822015-03-31 22:32:11 +01002421drm_intel_bo *
Dave Airlieff65de92012-07-15 00:22:46 +00002422drm_intel_bo_gem_create_from_prime(drm_intel_bufmgr *bufmgr, int prime_fd, int size)
2423{
2424 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
2425 int ret;
2426 uint32_t handle;
2427 drm_intel_bo_gem *bo_gem;
2428 struct drm_i915_gem_get_tiling get_tiling;
Keith Packardc3d96892013-11-22 05:31:01 -08002429 drmMMListHead *list;
Dave Airlieff65de92012-07-15 00:22:46 +00002430
Rafał Sapałacf40cf02015-07-24 11:22:34 +02002431 pthread_mutex_lock(&bufmgr_gem->lock);
Dave Airlieff65de92012-07-15 00:22:46 +00002432 ret = drmPrimeFDToHandle(bufmgr_gem->fd, prime_fd, &handle);
Rafał Sapałacf40cf02015-07-24 11:22:34 +02002433 if (ret) {
2434 DBG("create_from_prime: failed to obtain handle from fd: %s\n", strerror(errno));
2435 pthread_mutex_unlock(&bufmgr_gem->lock);
2436 return NULL;
2437 }
Keith Packardc3d96892013-11-22 05:31:01 -08002438
2439 /*
2440 * See if the kernel has already returned this buffer to us. Just as
2441 * for named buffers, we must not create two bo's pointing at the same
2442 * kernel object
2443 */
2444 for (list = bufmgr_gem->named.next;
2445 list != &bufmgr_gem->named;
2446 list = list->next) {
2447 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
2448 if (bo_gem->gem_handle == handle) {
2449 drm_intel_gem_bo_reference(&bo_gem->bo);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002450 pthread_mutex_unlock(&bufmgr_gem->lock);
Keith Packardc3d96892013-11-22 05:31:01 -08002451 return &bo_gem->bo;
2452 }
2453 }
2454
Dave Airlieff65de92012-07-15 00:22:46 +00002455 bo_gem = calloc(1, sizeof(*bo_gem));
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002456 if (!bo_gem) {
2457 pthread_mutex_unlock(&bufmgr_gem->lock);
Dave Airlieff65de92012-07-15 00:22:46 +00002458 return NULL;
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002459 }
Kristian Høgsberg9c52c3d2013-10-10 14:40:58 -07002460 /* Determine size of bo. The fd-to-handle ioctl really should
2461 * return the size, but it doesn't. If we have kernel 3.12 or
2462 * later, we can lseek on the prime fd to get the size. Older
2463 * kernels will just fail, in which case we fall back to the
2464 * provided (estimated or guess size). */
2465 ret = lseek(prime_fd, 0, SEEK_END);
2466 if (ret != -1)
2467 bo_gem->bo.size = ret;
2468 else
2469 bo_gem->bo.size = size;
2470
Dave Airlieff65de92012-07-15 00:22:46 +00002471 bo_gem->bo.handle = handle;
2472 bo_gem->bo.bufmgr = bufmgr;
2473
2474 bo_gem->gem_handle = handle;
2475
2476 atomic_set(&bo_gem->refcount, 1);
2477
2478 bo_gem->name = "prime";
2479 bo_gem->validate_index = -1;
2480 bo_gem->reloc_tree_fences = 0;
2481 bo_gem->used_as_reloc_target = false;
2482 bo_gem->has_error = false;
2483 bo_gem->reusable = false;
2484
Dave Airlieff65de92012-07-15 00:22:46 +00002485 DRMINITLISTHEAD(&bo_gem->vma_list);
Keith Packardc3d96892013-11-22 05:31:01 -08002486 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002487 pthread_mutex_unlock(&bufmgr_gem->lock);
Dave Airlieff65de92012-07-15 00:22:46 +00002488
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002489 memclear(get_tiling);
Dave Airlieff65de92012-07-15 00:22:46 +00002490 get_tiling.handle = bo_gem->gem_handle;
2491 ret = drmIoctl(bufmgr_gem->fd,
2492 DRM_IOCTL_I915_GEM_GET_TILING,
2493 &get_tiling);
2494 if (ret != 0) {
Rafał Sapałacf40cf02015-07-24 11:22:34 +02002495 DBG("create_from_prime: failed to get tiling: %s\n", strerror(errno));
Dave Airlieff65de92012-07-15 00:22:46 +00002496 drm_intel_gem_bo_unreference(&bo_gem->bo);
2497 return NULL;
2498 }
2499 bo_gem->tiling_mode = get_tiling.tiling_mode;
2500 bo_gem->swizzle_mode = get_tiling.swizzle_mode;
2501 /* XXX stride is unknown */
Anuj Phogat5c68f9f2015-04-10 17:20:55 -07002502 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
Dave Airlieff65de92012-07-15 00:22:46 +00002503
2504 return &bo_gem->bo;
2505}
2506
Emil Velikov0f8da822015-03-31 22:32:11 +01002507int
Dave Airlieff65de92012-07-15 00:22:46 +00002508drm_intel_bo_gem_export_to_prime(drm_intel_bo *bo, int *prime_fd)
2509{
2510 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2511 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2512
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002513 pthread_mutex_lock(&bufmgr_gem->lock);
Keith Packardc3d96892013-11-22 05:31:01 -08002514 if (DRMLISTEMPTY(&bo_gem->name_list))
2515 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002516 pthread_mutex_unlock(&bufmgr_gem->lock);
Keith Packardc3d96892013-11-22 05:31:01 -08002517
Kristian Høgsberg1b7ce582012-09-14 16:35:19 -04002518 if (drmPrimeHandleToFD(bufmgr_gem->fd, bo_gem->gem_handle,
2519 DRM_CLOEXEC, prime_fd) != 0)
2520 return -errno;
2521
2522 bo_gem->reusable = false;
2523
2524 return 0;
Dave Airlieff65de92012-07-15 00:22:46 +00002525}
2526
Eric Anholt99338382008-10-14 13:18:11 -07002527static int
Eric Anholtd70d6052009-10-06 12:40:42 -07002528drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name)
Keith Packard8e41ce12008-08-04 00:34:08 -07002529{
Eric Anholtd70d6052009-10-06 12:40:42 -07002530 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2531 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Eric Anholtd70d6052009-10-06 12:40:42 -07002532 int ret;
Keith Packard8e41ce12008-08-04 00:34:08 -07002533
Eric Anholtd70d6052009-10-06 12:40:42 -07002534 if (!bo_gem->global_name) {
Chris Wilson90b23cc2012-02-09 10:23:10 +00002535 struct drm_gem_flink flink;
2536
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002537 memclear(flink);
Eric Anholtd70d6052009-10-06 12:40:42 -07002538 flink.handle = bo_gem->gem_handle;
2539
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002540 pthread_mutex_lock(&bufmgr_gem->lock);
2541
Chris Wilson62997222010-09-25 21:32:59 +01002542 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002543 if (ret != 0) {
2544 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07002545 return -errno;
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002546 }
Chris Wilson90b23cc2012-02-09 10:23:10 +00002547
Eric Anholtd70d6052009-10-06 12:40:42 -07002548 bo_gem->global_name = flink.name;
Eric Anholt2c2bdb32011-10-21 16:53:16 -07002549 bo_gem->reusable = false;
Chris Wilson36d49392011-02-14 09:39:06 +00002550
Keith Packardc3d96892013-11-22 05:31:01 -08002551 if (DRMLISTEMPTY(&bo_gem->name_list))
2552 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
Rafal Sapala0fa1dbf2014-08-05 14:51:38 -04002553 pthread_mutex_unlock(&bufmgr_gem->lock);
Eric Anholtd70d6052009-10-06 12:40:42 -07002554 }
2555
2556 *name = bo_gem->global_name;
2557 return 0;
Keith Packard8e41ce12008-08-04 00:34:08 -07002558}
2559
Eric Anholt6a9eb082008-06-03 09:27:37 -07002560/**
2561 * Enables unlimited caching of buffer objects for reuse.
2562 *
2563 * This is potentially very memory expensive, as the cache at each bucket
2564 * size is only bounded by how many buffers of that size we've managed to have
2565 * in flight at once.
2566 */
Emil Velikov0f8da822015-03-31 22:32:11 +01002567void
Eric Anholt4b982642008-10-30 09:33:07 -07002568drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002569{
Eric Anholtd70d6052009-10-06 12:40:42 -07002570 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002571
Eric Anholt2c2bdb32011-10-21 16:53:16 -07002572 bufmgr_gem->bo_reuse = true;
Eric Anholt6a9eb082008-06-03 09:27:37 -07002573}
2574
Eric Anholt0e867312008-10-21 00:10:54 -07002575/**
Jesse Barnesb5096402009-09-15 11:02:58 -07002576 * Enable use of fenced reloc type.
2577 *
2578 * New code should enable this to avoid unnecessary fence register
2579 * allocation. If this option is not enabled, all relocs will have fence
2580 * register allocated.
2581 */
Emil Velikov0f8da822015-03-31 22:32:11 +01002582void
Jesse Barnesb5096402009-09-15 11:02:58 -07002583drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr)
2584{
Eric Anholt766fa792010-03-02 16:04:14 -08002585 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
Jesse Barnesb5096402009-09-15 11:02:58 -07002586
Eric Anholt766fa792010-03-02 16:04:14 -08002587 if (bufmgr_gem->bufmgr.bo_exec == drm_intel_gem_bo_exec2)
Eric Anholt2c2bdb32011-10-21 16:53:16 -07002588 bufmgr_gem->fenced_relocs = true;
Jesse Barnesb5096402009-09-15 11:02:58 -07002589}
2590
2591/**
Eric Anholt0e867312008-10-21 00:10:54 -07002592 * Return the additional aperture space required by the tree of buffer objects
2593 * rooted at bo.
Eric Anholt6a9eb082008-06-03 09:27:37 -07002594 */
2595static int
Eric Anholt4b982642008-10-30 09:33:07 -07002596drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002597{
Eric Anholtd70d6052009-10-06 12:40:42 -07002598 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2599 int i;
2600 int total = 0;
Eric Anholt0e867312008-10-21 00:10:54 -07002601
Eric Anholtd70d6052009-10-06 12:40:42 -07002602 if (bo == NULL || bo_gem->included_in_check_aperture)
2603 return 0;
Eric Anholt0e867312008-10-21 00:10:54 -07002604
Eric Anholtd70d6052009-10-06 12:40:42 -07002605 total += bo->size;
Eric Anholt2c2bdb32011-10-21 16:53:16 -07002606 bo_gem->included_in_check_aperture = true;
Eric Anholt0e867312008-10-21 00:10:54 -07002607
Eric Anholtd70d6052009-10-06 12:40:42 -07002608 for (i = 0; i < bo_gem->reloc_count; i++)
2609 total +=
2610 drm_intel_gem_bo_get_aperture_space(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -07002611 reloc_target_info[i].bo);
Eric Anholt0e867312008-10-21 00:10:54 -07002612
Eric Anholtd70d6052009-10-06 12:40:42 -07002613 return total;
Eric Anholt0e867312008-10-21 00:10:54 -07002614}
2615
2616/**
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002617 * Count the number of buffers in this list that need a fence reg
2618 *
2619 * If the count is greater than the number of available regs, we'll have
2620 * to ask the caller to resubmit a batch with fewer tiled buffers.
2621 *
Eric Anholt9209c9a2009-01-27 16:54:11 -08002622 * This function over-counts if the same buffer is used multiple times.
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002623 */
2624static unsigned int
Eric Anholtd70d6052009-10-06 12:40:42 -07002625drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count)
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002626{
Eric Anholtd70d6052009-10-06 12:40:42 -07002627 int i;
2628 unsigned int total = 0;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002629
Eric Anholtd70d6052009-10-06 12:40:42 -07002630 for (i = 0; i < count; i++) {
2631 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002632
Eric Anholtd70d6052009-10-06 12:40:42 -07002633 if (bo_gem == NULL)
2634 continue;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002635
Eric Anholtd70d6052009-10-06 12:40:42 -07002636 total += bo_gem->reloc_tree_fences;
2637 }
2638 return total;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002639}
2640
2641/**
Eric Anholt4b982642008-10-30 09:33:07 -07002642 * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
2643 * for the next drm_intel_bufmgr_check_aperture_space() call.
Eric Anholt0e867312008-10-21 00:10:54 -07002644 */
2645static void
Eric Anholt4b982642008-10-30 09:33:07 -07002646drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo)
Eric Anholt0e867312008-10-21 00:10:54 -07002647{
Eric Anholtd70d6052009-10-06 12:40:42 -07002648 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2649 int i;
Eric Anholt0e867312008-10-21 00:10:54 -07002650
Eric Anholtd70d6052009-10-06 12:40:42 -07002651 if (bo == NULL || !bo_gem->included_in_check_aperture)
2652 return;
Eric Anholt0e867312008-10-21 00:10:54 -07002653
Eric Anholt2c2bdb32011-10-21 16:53:16 -07002654 bo_gem->included_in_check_aperture = false;
Eric Anholt0e867312008-10-21 00:10:54 -07002655
Eric Anholtd70d6052009-10-06 12:40:42 -07002656 for (i = 0; i < bo_gem->reloc_count; i++)
2657 drm_intel_gem_bo_clear_aperture_space_flag(bo_gem->
Jesse Barnesb5096402009-09-15 11:02:58 -07002658 reloc_target_info[i].bo);
Eric Anholt0e867312008-10-21 00:10:54 -07002659}
2660
2661/**
Keith Packardb13f4e12008-11-21 01:49:39 -08002662 * Return a conservative estimate for the amount of aperture required
2663 * for a collection of buffers. This may double-count some buffers.
2664 */
2665static unsigned int
2666drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count)
2667{
Eric Anholtd70d6052009-10-06 12:40:42 -07002668 int i;
2669 unsigned int total = 0;
Keith Packardb13f4e12008-11-21 01:49:39 -08002670
Eric Anholtd70d6052009-10-06 12:40:42 -07002671 for (i = 0; i < count; i++) {
2672 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
2673 if (bo_gem != NULL)
2674 total += bo_gem->reloc_tree_size;
2675 }
2676 return total;
Keith Packardb13f4e12008-11-21 01:49:39 -08002677}
2678
2679/**
2680 * Return the amount of aperture needed for a collection of buffers.
2681 * This avoids double counting any buffers, at the cost of looking
2682 * at every buffer in the set.
2683 */
2684static unsigned int
2685drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count)
2686{
Eric Anholtd70d6052009-10-06 12:40:42 -07002687 int i;
2688 unsigned int total = 0;
Keith Packardb13f4e12008-11-21 01:49:39 -08002689
Eric Anholtd70d6052009-10-06 12:40:42 -07002690 for (i = 0; i < count; i++) {
2691 total += drm_intel_gem_bo_get_aperture_space(bo_array[i]);
2692 /* For the first buffer object in the array, we get an
2693 * accurate count back for its reloc_tree size (since nothing
2694 * had been flagged as being counted yet). We can save that
2695 * value out as a more conservative reloc_tree_size that
2696 * avoids double-counting target buffers. Since the first
2697 * buffer happens to usually be the batch buffer in our
2698 * callers, this can pull us back from doing the tree
2699 * walk on every new batch emit.
2700 */
2701 if (i == 0) {
2702 drm_intel_bo_gem *bo_gem =
2703 (drm_intel_bo_gem *) bo_array[i];
2704 bo_gem->reloc_tree_size = total;
2705 }
Eric Anholt7ce8d4c2009-02-27 13:46:31 -08002706 }
Keith Packardb13f4e12008-11-21 01:49:39 -08002707
Eric Anholtd70d6052009-10-06 12:40:42 -07002708 for (i = 0; i < count; i++)
2709 drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]);
2710 return total;
Keith Packardb13f4e12008-11-21 01:49:39 -08002711}
2712
2713/**
Eric Anholt0e867312008-10-21 00:10:54 -07002714 * Return -1 if the batchbuffer should be flushed before attempting to
2715 * emit rendering referencing the buffers pointed to by bo_array.
Eric Anholt6a9eb082008-06-03 09:27:37 -07002716 *
Eric Anholt0e867312008-10-21 00:10:54 -07002717 * This is required because if we try to emit a batchbuffer with relocations
2718 * to a tree of buffers that won't simultaneously fit in the aperture,
2719 * the rendering will return an error at a point where the software is not
2720 * prepared to recover from it.
2721 *
2722 * However, we also want to emit the batchbuffer significantly before we reach
2723 * the limit, as a series of batchbuffers each of which references buffers
2724 * covering almost all of the aperture means that at each emit we end up
2725 * waiting to evict a buffer from the last rendering, and we get synchronous
2726 * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
2727 * get better parallelism.
Eric Anholt6a9eb082008-06-03 09:27:37 -07002728 */
2729static int
Eric Anholt4b982642008-10-30 09:33:07 -07002730drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count)
Eric Anholt6a9eb082008-06-03 09:27:37 -07002731{
Eric Anholtd70d6052009-10-06 12:40:42 -07002732 drm_intel_bufmgr_gem *bufmgr_gem =
2733 (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr;
2734 unsigned int total = 0;
2735 unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4;
2736 int total_fences;
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002737
Eric Anholtd70d6052009-10-06 12:40:42 -07002738 /* Check for fence reg constraints if necessary */
2739 if (bufmgr_gem->available_fences) {
2740 total_fences = drm_intel_gem_total_fences(bo_array, count);
2741 if (total_fences > bufmgr_gem->available_fences)
Chris Wilsonacb4aa62009-12-02 12:40:26 +00002742 return -ENOSPC;
Eric Anholtd70d6052009-10-06 12:40:42 -07002743 }
Eric Anholt0e867312008-10-21 00:10:54 -07002744
Eric Anholtd70d6052009-10-06 12:40:42 -07002745 total = drm_intel_gem_estimate_batch_space(bo_array, count);
Jesse Barnes2fa5f282009-01-23 14:13:45 -08002746
Eric Anholtd70d6052009-10-06 12:40:42 -07002747 if (total > threshold)
2748 total = drm_intel_gem_compute_batch_space(bo_array, count);
Eric Anholt0e867312008-10-21 00:10:54 -07002749
Eric Anholtd70d6052009-10-06 12:40:42 -07002750 if (total > threshold) {
2751 DBG("check_space: overflowed available aperture, "
2752 "%dkb vs %dkb\n",
2753 total / 1024, (int)bufmgr_gem->gtt_size / 1024);
Chris Wilsonacb4aa62009-12-02 12:40:26 +00002754 return -ENOSPC;
Eric Anholtd70d6052009-10-06 12:40:42 -07002755 } else {
2756 DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024,
2757 (int)bufmgr_gem->gtt_size / 1024);
2758 return 0;
2759 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07002760}
2761
Keith Packard5b5ce302009-05-11 13:42:12 -07002762/*
2763 * Disable buffer reuse for objects which are shared with the kernel
2764 * as scanout buffers
2765 */
2766static int
2767drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo)
2768{
Eric Anholtd70d6052009-10-06 12:40:42 -07002769 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
Keith Packard5b5ce302009-05-11 13:42:12 -07002770
Eric Anholt2c2bdb32011-10-21 16:53:16 -07002771 bo_gem->reusable = false;
Eric Anholtd70d6052009-10-06 12:40:42 -07002772 return 0;
Keith Packard5b5ce302009-05-11 13:42:12 -07002773}
2774
Eric Anholt769b1052009-10-01 19:09:26 -07002775static int
Chris Wilson07e75892010-05-11 08:54:06 +01002776drm_intel_gem_bo_is_reusable(drm_intel_bo *bo)
2777{
2778 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2779
2780 return bo_gem->reusable;
2781}
2782
2783static int
Eric Anholt66d27142009-10-20 13:20:55 -07002784_drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
Eric Anholt769b1052009-10-01 19:09:26 -07002785{
Eric Anholtd70d6052009-10-06 12:40:42 -07002786 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2787 int i;
Eric Anholt769b1052009-10-01 19:09:26 -07002788
Eric Anholtd70d6052009-10-06 12:40:42 -07002789 for (i = 0; i < bo_gem->reloc_count; i++) {
Jesse Barnesb5096402009-09-15 11:02:58 -07002790 if (bo_gem->reloc_target_info[i].bo == target_bo)
Eric Anholtd70d6052009-10-06 12:40:42 -07002791 return 1;
Eric Anholt4f7704a2010-06-10 08:58:08 -07002792 if (bo == bo_gem->reloc_target_info[i].bo)
2793 continue;
Jesse Barnesb5096402009-09-15 11:02:58 -07002794 if (_drm_intel_gem_bo_references(bo_gem->reloc_target_info[i].bo,
Eric Anholtd70d6052009-10-06 12:40:42 -07002795 target_bo))
2796 return 1;
2797 }
2798
Eric Anholt769b1052009-10-01 19:09:26 -07002799 return 0;
Eric Anholt769b1052009-10-01 19:09:26 -07002800}
2801
Eric Anholt66d27142009-10-20 13:20:55 -07002802/** Return true if target_bo is referenced by bo's relocation tree. */
2803static int
2804drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
2805{
2806 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
2807
2808 if (bo == NULL || target_bo == NULL)
2809 return 0;
2810 if (target_bo_gem->used_as_reloc_target)
2811 return _drm_intel_gem_bo_references(bo, target_bo);
2812 return 0;
2813}
2814
Eric Anholt0ec768e2010-06-04 17:09:11 -07002815static void
2816add_bucket(drm_intel_bufmgr_gem *bufmgr_gem, int size)
2817{
2818 unsigned int i = bufmgr_gem->num_buckets;
2819
2820 assert(i < ARRAY_SIZE(bufmgr_gem->cache_bucket));
2821
2822 DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head);
2823 bufmgr_gem->cache_bucket[i].size = size;
2824 bufmgr_gem->num_buckets++;
2825}
2826
2827static void
2828init_cache_buckets(drm_intel_bufmgr_gem *bufmgr_gem)
2829{
2830 unsigned long size, cache_max_size = 64 * 1024 * 1024;
2831
2832 /* OK, so power of two buckets was too wasteful of memory.
2833 * Give 3 other sizes between each power of two, to hopefully
2834 * cover things accurately enough. (The alternative is
2835 * probably to just go for exact matching of sizes, and assume
2836 * that for things like composited window resize the tiled
2837 * width/height alignment and rounding of sizes to pages will
2838 * get us useful cache hit rates anyway)
2839 */
2840 add_bucket(bufmgr_gem, 4096);
2841 add_bucket(bufmgr_gem, 4096 * 2);
2842 add_bucket(bufmgr_gem, 4096 * 3);
2843
2844 /* Initialize the linked lists for BO reuse cache. */
2845 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
2846 add_bucket(bufmgr_gem, size);
2847
2848 add_bucket(bufmgr_gem, size + size * 1 / 4);
2849 add_bucket(bufmgr_gem, size + size * 2 / 4);
2850 add_bucket(bufmgr_gem, size + size * 3 / 4);
2851 }
2852}
2853
Emil Velikov0f8da822015-03-31 22:32:11 +01002854void
Chris Wilsone4b60f22011-12-05 21:29:05 +00002855drm_intel_bufmgr_gem_set_vma_cache_size(drm_intel_bufmgr *bufmgr, int limit)
2856{
2857 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
2858
2859 bufmgr_gem->vma_max = limit;
2860
2861 drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
2862}
2863
Eric Anholt769b1052009-10-01 19:09:26 -07002864/**
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002865 * Get the PCI ID for the device. This can be overridden by setting the
2866 * INTEL_DEVID_OVERRIDE environment variable to the desired ID.
2867 */
2868static int
2869get_pci_device_id(drm_intel_bufmgr_gem *bufmgr_gem)
2870{
2871 char *devid_override;
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002872 int devid = 0;
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002873 int ret;
2874 drm_i915_getparam_t gp;
2875
2876 if (geteuid() == getuid()) {
2877 devid_override = getenv("INTEL_DEVID_OVERRIDE");
2878 if (devid_override) {
2879 bufmgr_gem->no_exec = true;
2880 return strtod(devid_override, NULL);
2881 }
2882 }
2883
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002884 memclear(gp);
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002885 gp.param = I915_PARAM_CHIPSET_ID;
2886 gp.value = &devid;
2887 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
2888 if (ret) {
2889 fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno);
2890 fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value);
2891 }
2892 return devid;
2893}
2894
Emil Velikov0f8da822015-03-31 22:32:11 +01002895int
Kenneth Graunke6e642db2011-10-11 14:38:34 -07002896drm_intel_bufmgr_gem_get_devid(drm_intel_bufmgr *bufmgr)
2897{
2898 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
2899
2900 return bufmgr_gem->pci_device;
2901}
2902
2903/**
Damien Lespiaufbd106a2013-02-20 12:11:49 +00002904 * Sets the AUB filename.
2905 *
2906 * This function has to be called before drm_intel_bufmgr_gem_set_aub_dump()
2907 * for it to have any effect.
2908 */
Emil Velikov0f8da822015-03-31 22:32:11 +01002909void
Damien Lespiaufbd106a2013-02-20 12:11:49 +00002910drm_intel_bufmgr_gem_set_aub_filename(drm_intel_bufmgr *bufmgr,
2911 const char *filename)
2912{
Damien Lespiaufbd106a2013-02-20 12:11:49 +00002913}
2914
2915/**
Eric Anholt4db16a92011-10-11 15:59:03 -07002916 * Sets up AUB dumping.
2917 *
2918 * This is a trace file format that can be used with the simulator.
2919 * Packets are emitted in a format somewhat like GPU command packets.
2920 * You can set up a GTT and upload your objects into the referenced
2921 * space, then send off batchbuffers and get BMPs out the other end.
2922 */
Emil Velikov0f8da822015-03-31 22:32:11 +01002923void
Eric Anholt4db16a92011-10-11 15:59:03 -07002924drm_intel_bufmgr_gem_set_aub_dump(drm_intel_bufmgr *bufmgr, int enable)
2925{
Kristian Høgsberg Kristensencd2f91e2015-07-31 10:47:50 -07002926 fprintf(stderr, "libdrm aub dumping is deprecated.\n\n"
2927 "Use intel_aubdump from intel-gpu-tools instead. Install intel-gpu-tools,\n"
2928 "then run (for example)\n\n"
2929 "\t$ intel_aubdump --output=trace.aub glxgears -geometry 500x500\n\n"
2930 "See the intel_aubdump man page for more details.\n");
Eric Anholt4db16a92011-10-11 15:59:03 -07002931}
2932
Emil Velikov0f8da822015-03-31 22:32:11 +01002933drm_intel_context *
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002934drm_intel_gem_context_create(drm_intel_bufmgr *bufmgr)
2935{
2936 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
2937 struct drm_i915_gem_context_create create;
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002938 drm_intel_context *context = NULL;
Damien Lespiauc10b08d2012-07-26 17:50:09 +01002939 int ret;
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002940
Ben Widawsky3d34fe22013-12-26 16:37:00 -08002941 context = calloc(1, sizeof(*context));
2942 if (!context)
2943 return NULL;
2944
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002945 memclear(create);
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002946 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
2947 if (ret != 0) {
Kenneth Graunke992e2af2012-07-12 13:41:11 -07002948 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n",
2949 strerror(errno));
Ben Widawsky3d34fe22013-12-26 16:37:00 -08002950 free(context);
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002951 return NULL;
2952 }
2953
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002954 context->ctx_id = create.ctx_id;
2955 context->bufmgr = bufmgr;
2956
2957 return context;
2958}
2959
Emil Velikov0f8da822015-03-31 22:32:11 +01002960void
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002961drm_intel_gem_context_destroy(drm_intel_context *ctx)
2962{
2963 drm_intel_bufmgr_gem *bufmgr_gem;
2964 struct drm_i915_gem_context_destroy destroy;
2965 int ret;
2966
2967 if (ctx == NULL)
2968 return;
2969
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002970 memclear(destroy);
Kenneth Graunkea9412fa2012-08-12 13:33:05 -07002971
Ben Widawskyf7210fa2012-01-13 11:31:52 -08002972 bufmgr_gem = (drm_intel_bufmgr_gem *)ctx->bufmgr;
2973 destroy.ctx_id = ctx->ctx_id;
2974 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY,
2975 &destroy);
2976 if (ret != 0)
2977 fprintf(stderr, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
2978 strerror(errno));
2979
2980 free(ctx);
2981}
2982
Emil Velikov0f8da822015-03-31 22:32:11 +01002983int
Ian Romanick5a41b022013-11-15 10:24:43 -08002984drm_intel_get_reset_stats(drm_intel_context *ctx,
2985 uint32_t *reset_count,
2986 uint32_t *active,
2987 uint32_t *pending)
2988{
2989 drm_intel_bufmgr_gem *bufmgr_gem;
2990 struct drm_i915_reset_stats stats;
2991 int ret;
2992
2993 if (ctx == NULL)
2994 return -EINVAL;
2995
Daniel Vettereb7a5b62015-02-11 11:59:52 +01002996 memclear(stats);
Ian Romanick5a41b022013-11-15 10:24:43 -08002997
2998 bufmgr_gem = (drm_intel_bufmgr_gem *)ctx->bufmgr;
2999 stats.ctx_id = ctx->ctx_id;
3000 ret = drmIoctl(bufmgr_gem->fd,
3001 DRM_IOCTL_I915_GET_RESET_STATS,
3002 &stats);
3003 if (ret == 0) {
3004 if (reset_count != NULL)
3005 *reset_count = stats.reset_count;
3006
3007 if (active != NULL)
3008 *active = stats.batch_active;
3009
3010 if (pending != NULL)
3011 *pending = stats.batch_pending;
3012 }
3013
3014 return ret;
3015}
3016
Emil Velikov0f8da822015-03-31 22:32:11 +01003017int
Eric Anholt2607dad2012-08-01 16:43:16 -07003018drm_intel_reg_read(drm_intel_bufmgr *bufmgr,
3019 uint32_t offset,
3020 uint64_t *result)
3021{
3022 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3023 struct drm_i915_reg_read reg_read;
3024 int ret;
3025
Daniel Vettereb7a5b62015-02-11 11:59:52 +01003026 memclear(reg_read);
Eric Anholt2607dad2012-08-01 16:43:16 -07003027 reg_read.offset = offset;
3028
3029 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_REG_READ, &reg_read);
3030
3031 *result = reg_read.val;
3032 return ret;
3033}
3034
Emil Velikov0f8da822015-03-31 22:32:11 +01003035int
Jeff McGeed556e062015-03-09 16:13:03 -07003036drm_intel_get_subslice_total(int fd, unsigned int *subslice_total)
3037{
3038 drm_i915_getparam_t gp;
3039 int ret;
3040
3041 memclear(gp);
3042 gp.value = (int*)subslice_total;
3043 gp.param = I915_PARAM_SUBSLICE_TOTAL;
3044 ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
3045 if (ret)
3046 return -errno;
3047
3048 return 0;
3049}
3050
Emil Velikov0f8da822015-03-31 22:32:11 +01003051int
Jeff McGeed556e062015-03-09 16:13:03 -07003052drm_intel_get_eu_total(int fd, unsigned int *eu_total)
3053{
3054 drm_i915_getparam_t gp;
3055 int ret;
3056
3057 memclear(gp);
3058 gp.value = (int*)eu_total;
3059 gp.param = I915_PARAM_EU_TOTAL;
3060 ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
3061 if (ret)
3062 return -errno;
3063
3064 return 0;
3065}
Ben Widawskyf7210fa2012-01-13 11:31:52 -08003066
Eric Anholt4db16a92011-10-11 15:59:03 -07003067/**
Paul Berryda02f722012-05-04 12:41:00 -07003068 * Annotate the given bo for use in aub dumping.
3069 *
3070 * \param annotations is an array of drm_intel_aub_annotation objects
3071 * describing the type of data in various sections of the bo. Each
3072 * element of the array specifies the type and subtype of a section of
3073 * the bo, and the past-the-end offset of that section. The elements
3074 * of \c annotations must be sorted so that ending_offset is
3075 * increasing.
3076 *
3077 * \param count is the number of elements in the \c annotations array.
3078 * If \c count is zero, then \c annotations will not be dereferenced.
3079 *
3080 * Annotations are copied into a private data structure, so caller may
3081 * re-use the memory pointed to by \c annotations after the call
3082 * returns.
3083 *
3084 * Annotations are stored for the lifetime of the bo; to reset to the
3085 * default state (no annotations), call this function with a \c count
3086 * of zero.
3087 */
Emil Velikov0f8da822015-03-31 22:32:11 +01003088void
Paul Berryda02f722012-05-04 12:41:00 -07003089drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
3090 drm_intel_aub_annotation *annotations,
3091 unsigned count)
3092{
Paul Berryda02f722012-05-04 12:41:00 -07003093}
3094
Lionel Landwerlin743af592014-09-12 13:48:36 +01003095static pthread_mutex_t bufmgr_list_mutex = PTHREAD_MUTEX_INITIALIZER;
3096static drmMMListHead bufmgr_list = { &bufmgr_list, &bufmgr_list };
3097
3098static drm_intel_bufmgr_gem *
3099drm_intel_bufmgr_gem_find(int fd)
3100{
3101 drm_intel_bufmgr_gem *bufmgr_gem;
3102
3103 DRMLISTFOREACHENTRY(bufmgr_gem, &bufmgr_list, managers) {
3104 if (bufmgr_gem->fd == fd) {
3105 atomic_inc(&bufmgr_gem->refcount);
3106 return bufmgr_gem;
3107 }
3108 }
3109
3110 return NULL;
3111}
3112
3113static void
3114drm_intel_bufmgr_gem_unref(drm_intel_bufmgr *bufmgr)
3115{
3116 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3117
3118 if (atomic_add_unless(&bufmgr_gem->refcount, -1, 1)) {
3119 pthread_mutex_lock(&bufmgr_list_mutex);
3120
3121 if (atomic_dec_and_test(&bufmgr_gem->refcount)) {
3122 DRMLISTDEL(&bufmgr_gem->managers);
3123 drm_intel_bufmgr_gem_destroy(bufmgr);
3124 }
3125
3126 pthread_mutex_unlock(&bufmgr_list_mutex);
3127 }
3128}
3129
Paul Berryda02f722012-05-04 12:41:00 -07003130/**
Eric Anholt6a9eb082008-06-03 09:27:37 -07003131 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
3132 * and manage map buffer objections.
3133 *
3134 * \param fd File descriptor of the opened DRM device.
3135 */
Emil Velikov0f8da822015-03-31 22:32:11 +01003136drm_intel_bufmgr *
Eric Anholt4b982642008-10-30 09:33:07 -07003137drm_intel_bufmgr_gem_init(int fd, int batch_size)
Eric Anholt6a9eb082008-06-03 09:27:37 -07003138{
Eric Anholtd70d6052009-10-06 12:40:42 -07003139 drm_intel_bufmgr_gem *bufmgr_gem;
3140 struct drm_i915_gem_get_aperture aperture;
3141 drm_i915_getparam_t gp;
Daniel Vetter630dd262011-09-22 22:20:09 +02003142 int ret, tmp;
Eric Anholt2c2bdb32011-10-21 16:53:16 -07003143 bool exec2 = false;
Eric Anholt6a9eb082008-06-03 09:27:37 -07003144
Lionel Landwerlin743af592014-09-12 13:48:36 +01003145 pthread_mutex_lock(&bufmgr_list_mutex);
3146
3147 bufmgr_gem = drm_intel_bufmgr_gem_find(fd);
3148 if (bufmgr_gem)
3149 goto exit;
3150
Eric Anholtd70d6052009-10-06 12:40:42 -07003151 bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
Dave Airlie973d8d62010-02-02 10:57:12 +10003152 if (bufmgr_gem == NULL)
Lionel Landwerlin743af592014-09-12 13:48:36 +01003153 goto exit;
Dave Airlie973d8d62010-02-02 10:57:12 +10003154
Eric Anholtd70d6052009-10-06 12:40:42 -07003155 bufmgr_gem->fd = fd;
Lionel Landwerlin743af592014-09-12 13:48:36 +01003156 atomic_set(&bufmgr_gem->refcount, 1);
Eric Anholt6a9eb082008-06-03 09:27:37 -07003157
Eric Anholtd70d6052009-10-06 12:40:42 -07003158 if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) {
3159 free(bufmgr_gem);
Lionel Landwerlin743af592014-09-12 13:48:36 +01003160 bufmgr_gem = NULL;
3161 goto exit;
Eric Anholtd70d6052009-10-06 12:40:42 -07003162 }
Eric Anholt6df7b072008-06-12 23:22:26 -07003163
Daniel Vettereb7a5b62015-02-11 11:59:52 +01003164 memclear(aperture);
Chris Wilson62997222010-09-25 21:32:59 +01003165 ret = drmIoctl(bufmgr_gem->fd,
3166 DRM_IOCTL_I915_GEM_GET_APERTURE,
3167 &aperture);
Eric Anholt0e867312008-10-21 00:10:54 -07003168
Eric Anholtd70d6052009-10-06 12:40:42 -07003169 if (ret == 0)
3170 bufmgr_gem->gtt_size = aperture.aper_available_size;
3171 else {
3172 fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
3173 strerror(errno));
3174 bufmgr_gem->gtt_size = 128 * 1024 * 1024;
3175 fprintf(stderr, "Assuming %dkB available aperture size.\n"
3176 "May lead to reduced performance or incorrect "
3177 "rendering.\n",
3178 (int)bufmgr_gem->gtt_size / 1024);
3179 }
Eric Anholt0e867312008-10-21 00:10:54 -07003180
Kenneth Graunke6e642db2011-10-11 14:38:34 -07003181 bufmgr_gem->pci_device = get_pci_device_id(bufmgr_gem);
Jesse Barnes2fa5f282009-01-23 14:13:45 -08003182
Eric Anholt078bc5b2011-12-20 13:10:36 -08003183 if (IS_GEN2(bufmgr_gem->pci_device))
Eric Anholta1f9ea72010-03-02 08:49:36 -08003184 bufmgr_gem->gen = 2;
Eric Anholt078bc5b2011-12-20 13:10:36 -08003185 else if (IS_GEN3(bufmgr_gem->pci_device))
Eric Anholta1f9ea72010-03-02 08:49:36 -08003186 bufmgr_gem->gen = 3;
Eric Anholt078bc5b2011-12-20 13:10:36 -08003187 else if (IS_GEN4(bufmgr_gem->pci_device))
Eric Anholta1f9ea72010-03-02 08:49:36 -08003188 bufmgr_gem->gen = 4;
Chad Versace592ac672012-01-27 10:02:16 -08003189 else if (IS_GEN5(bufmgr_gem->pci_device))
3190 bufmgr_gem->gen = 5;
3191 else if (IS_GEN6(bufmgr_gem->pci_device))
Eric Anholta1f9ea72010-03-02 08:49:36 -08003192 bufmgr_gem->gen = 6;
Chad Versace592ac672012-01-27 10:02:16 -08003193 else if (IS_GEN7(bufmgr_gem->pci_device))
Chris Wilson9a2b57d2012-07-25 16:28:59 +01003194 bufmgr_gem->gen = 7;
Ben Widawsky5b348f32013-02-13 16:09:33 +00003195 else if (IS_GEN8(bufmgr_gem->pci_device))
3196 bufmgr_gem->gen = 8;
Damien Lespiauf1e15d12013-02-13 16:09:37 +00003197 else if (IS_GEN9(bufmgr_gem->pci_device))
3198 bufmgr_gem->gen = 9;
Chris Wilson9a2b57d2012-07-25 16:28:59 +01003199 else {
3200 free(bufmgr_gem);
Lionel Landwerlin743af592014-09-12 13:48:36 +01003201 bufmgr_gem = NULL;
3202 goto exit;
Chris Wilson9a2b57d2012-07-25 16:28:59 +01003203 }
Eric Anholta1f9ea72010-03-02 08:49:36 -08003204
Eric Anholt078bc5b2011-12-20 13:10:36 -08003205 if (IS_GEN3(bufmgr_gem->pci_device) &&
3206 bufmgr_gem->gtt_size > 256*1024*1024) {
Daniel Vetter36cff1c2011-12-04 12:51:45 +01003207 /* The unmappable part of gtt on gen 3 (i.e. above 256MB) can't
3208 * be used for tiled blits. To simplify the accounting, just
3209 * substract the unmappable part (fixed to 256MB on all known
3210 * gen3 devices) if the kernel advertises it. */
3211 bufmgr_gem->gtt_size -= 256*1024*1024;
3212 }
3213
Daniel Vettereb7a5b62015-02-11 11:59:52 +01003214 memclear(gp);
Daniel Vetter630dd262011-09-22 22:20:09 +02003215 gp.value = &tmp;
3216
Jesse Barnesb5096402009-09-15 11:02:58 -07003217 gp.param = I915_PARAM_HAS_EXECBUF2;
Chris Wilson62997222010-09-25 21:32:59 +01003218 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Jesse Barnesb5096402009-09-15 11:02:58 -07003219 if (!ret)
Eric Anholt2c2bdb32011-10-21 16:53:16 -07003220 exec2 = true;
Jesse Barnesb5096402009-09-15 11:02:58 -07003221
Zou Nan hai66375fd2010-06-02 10:07:37 +08003222 gp.param = I915_PARAM_HAS_BSD;
Chris Wilson62997222010-09-25 21:32:59 +01003223 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Chris Wilson057fab32010-10-26 11:35:11 +01003224 bufmgr_gem->has_bsd = ret == 0;
3225
3226 gp.param = I915_PARAM_HAS_BLT;
3227 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3228 bufmgr_gem->has_blt = ret == 0;
Zou Nan hai66375fd2010-06-02 10:07:37 +08003229
Chris Wilson36245772010-10-29 10:49:54 +01003230 gp.param = I915_PARAM_HAS_RELAXED_FENCING;
3231 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3232 bufmgr_gem->has_relaxed_fencing = ret == 0;
3233
Chris Wilson32258e42014-11-04 14:26:49 +00003234 bufmgr_gem->bufmgr.bo_alloc_userptr = check_bo_alloc_userptr;
Tvrtko Ursulinae8edc72014-06-19 15:52:03 +01003235
Ben Widawsky971c0802012-06-05 11:30:48 -07003236 gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
3237 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3238 bufmgr_gem->has_wait_timeout = ret == 0;
3239
Eugeni Dodonov151cdcf2012-01-17 15:20:19 -02003240 gp.param = I915_PARAM_HAS_LLC;
3241 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Eric Anholt3a888482012-02-27 17:26:05 -08003242 if (ret != 0) {
Eugeni Dodonov151cdcf2012-01-17 15:20:19 -02003243 /* Kernel does not supports HAS_LLC query, fallback to GPU
3244 * generation detection and assume that we have LLC on GEN6/7
3245 */
3246 bufmgr_gem->has_llc = (IS_GEN6(bufmgr_gem->pci_device) |
3247 IS_GEN7(bufmgr_gem->pci_device));
3248 } else
Chris Wilson75830a02012-10-07 10:05:19 +01003249 bufmgr_gem->has_llc = *gp.value;
Eugeni Dodonov151cdcf2012-01-17 15:20:19 -02003250
Xiang, Haihao01199992012-11-14 12:46:39 +08003251 gp.param = I915_PARAM_HAS_VEBOX;
3252 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3253 bufmgr_gem->has_vebox = (ret == 0) & (*gp.value > 0);
3254
Eric Anholta1f9ea72010-03-02 08:49:36 -08003255 if (bufmgr_gem->gen < 4) {
Eric Anholtd70d6052009-10-06 12:40:42 -07003256 gp.param = I915_PARAM_NUM_FENCES_AVAIL;
3257 gp.value = &bufmgr_gem->available_fences;
Chris Wilson62997222010-09-25 21:32:59 +01003258 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
Eric Anholtd70d6052009-10-06 12:40:42 -07003259 if (ret) {
3260 fprintf(stderr, "get fences failed: %d [%d]\n", ret,
3261 errno);
3262 fprintf(stderr, "param: %d, val: %d\n", gp.param,
3263 *gp.value);
3264 bufmgr_gem->available_fences = 0;
Chris Wilsonfdcde592010-02-09 08:32:54 +00003265 } else {
3266 /* XXX The kernel reports the total number of fences,
3267 * including any that may be pinned.
3268 *
3269 * We presume that there will be at least one pinned
3270 * fence for the scanout buffer, but there may be more
3271 * than one scanout and the user may be manually
3272 * pinning buffers. Let's move to execbuffer2 and
3273 * thereby forget the insanity of using fences...
3274 */
3275 bufmgr_gem->available_fences -= 2;
3276 if (bufmgr_gem->available_fences < 0)
3277 bufmgr_gem->available_fences = 0;
Eric Anholtd70d6052009-10-06 12:40:42 -07003278 }
3279 }
Eric Anholt6a9eb082008-06-03 09:27:37 -07003280
Eric Anholtd70d6052009-10-06 12:40:42 -07003281 /* Let's go with one relocation per every 2 dwords (but round down a bit
3282 * since a power of two will mean an extra page allocation for the reloc
3283 * buffer).
3284 *
3285 * Every 4 was too few for the blender benchmark.
3286 */
3287 bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
Eric Anholt769b1052009-10-01 19:09:26 -07003288
Eric Anholtd70d6052009-10-06 12:40:42 -07003289 bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc;
3290 bufmgr_gem->bufmgr.bo_alloc_for_render =
3291 drm_intel_gem_bo_alloc_for_render;
Jesse Barnes3a7dfcd2009-10-06 14:34:06 -07003292 bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled;
Eric Anholtd70d6052009-10-06 12:40:42 -07003293 bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference;
3294 bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference;
3295 bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map;
3296 bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap;
3297 bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata;
3298 bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata;
3299 bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering;
3300 bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc;
Jesse Barnesb5096402009-09-15 11:02:58 -07003301 bufmgr_gem->bufmgr.bo_emit_reloc_fence = drm_intel_gem_bo_emit_reloc_fence;
Eric Anholtd70d6052009-10-06 12:40:42 -07003302 bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin;
3303 bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin;
3304 bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling;
3305 bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling;
3306 bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink;
Jesse Barnesb5096402009-09-15 11:02:58 -07003307 /* Use the new one if available */
Zou Nan hai66375fd2010-06-02 10:07:37 +08003308 if (exec2) {
Jesse Barnesb5096402009-09-15 11:02:58 -07003309 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec2;
Albert Damen49447a92010-11-07 15:54:32 +01003310 bufmgr_gem->bufmgr.bo_mrb_exec = drm_intel_gem_bo_mrb_exec2;
Zou Nan hai66375fd2010-06-02 10:07:37 +08003311 } else
Jesse Barnesb5096402009-09-15 11:02:58 -07003312 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec;
Eric Anholtd70d6052009-10-06 12:40:42 -07003313 bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy;
Chris Wilson83a35b62009-11-11 13:04:38 +00003314 bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise;
Lionel Landwerlin743af592014-09-12 13:48:36 +01003315 bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_unref;
Eric Anholtd70d6052009-10-06 12:40:42 -07003316 bufmgr_gem->bufmgr.debug = 0;
3317 bufmgr_gem->bufmgr.check_aperture_space =
3318 drm_intel_gem_check_aperture_space;
3319 bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse;
Chris Wilson07e75892010-05-11 08:54:06 +01003320 bufmgr_gem->bufmgr.bo_is_reusable = drm_intel_gem_bo_is_reusable;
Eric Anholtd70d6052009-10-06 12:40:42 -07003321 bufmgr_gem->bufmgr.get_pipe_from_crtc_id =
3322 drm_intel_gem_get_pipe_from_crtc_id;
3323 bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references;
Eric Anholt6a9eb082008-06-03 09:27:37 -07003324
Chris Wilson36d49392011-02-14 09:39:06 +00003325 DRMINITLISTHEAD(&bufmgr_gem->named);
Eric Anholt0ec768e2010-06-04 17:09:11 -07003326 init_cache_buckets(bufmgr_gem);
Eric Anholtd70d6052009-10-06 12:40:42 -07003327
Chris Wilsone4b60f22011-12-05 21:29:05 +00003328 DRMINITLISTHEAD(&bufmgr_gem->vma_cache);
3329 bufmgr_gem->vma_max = -1; /* unlimited by default */
3330
Lionel Landwerlin743af592014-09-12 13:48:36 +01003331 DRMLISTADD(&bufmgr_gem->managers, &bufmgr_list);
3332
3333exit:
3334 pthread_mutex_unlock(&bufmgr_list_mutex);
3335
3336 return bufmgr_gem != NULL ? &bufmgr_gem->bufmgr : NULL;
Eric Anholt6a9eb082008-06-03 09:27:37 -07003337}