1/* 2 * Copyright © 2017 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#ifndef CROCUS_BUFMGR_H 25#define CROCUS_BUFMGR_H 26 27#include <stdbool.h> 28#include <stdint.h> 29#include <stdio.h> 30#include <sys/types.h> 31#include "util/macros.h" 32#include "util/u_atomic.h" 33#include "util/list.h" 34#include "pipe/p_defines.h" 35 36struct crocus_batch; 37struct intel_device_info; 38struct pipe_debug_callback; 39 40#define CROCUS_BINDER_SIZE (64 * 1024) 41#define CROCUS_MAX_BINDERS 100 42 43struct crocus_bo { 44 /** 45 * Size in bytes of the buffer object. 46 * 47 * The size may be larger than the size originally requested for the 48 * allocation, such as being aligned to page size. 49 */ 50 uint64_t size; 51 52 /** Buffer manager context associated with this buffer object */ 53 struct crocus_bufmgr *bufmgr; 54 55 /** The GEM handle for this buffer object. */ 56 uint32_t gem_handle; 57 58 /** 59 * Virtual address of the buffer inside the PPGTT (Per-Process Graphics 60 * Translation Table). 61 * 62 * Although each hardware context has its own VMA, we assign BO's to the 63 * same address in all contexts, for simplicity. 64 */ 65 uint64_t gtt_offset; 66 67 /** 68 * The validation list index for this buffer, or -1 when not in a batch. 69 * Note that a single buffer may be in multiple batches (contexts), and 70 * this is a global field, which refers to the last batch using the BO. 71 * It should not be considered authoritative, but can be used to avoid a 72 * linear walk of the validation list in the common case by guessing that 73 * exec_bos[bo->index] == bo and confirming whether that's the case. 74 * 75 * XXX: this is not ideal now that we have more than one batch per context, 76 * XXX: as the index will flop back and forth between the render index and 77 * XXX: compute index... 78 */ 79 unsigned index; 80 81 /** 82 * Boolean of whether the GPU is definitely not accessing the buffer. 83 * 84 * This is only valid when reusable, since non-reusable 85 * buffers are those that have been shared with other 86 * processes, so we don't know their state. 87 */ 88 bool idle; 89 90 int refcount; 91 const char *name; 92 93 uint64_t kflags; 94 95 /** 96 * Kenel-assigned global name for this object 97 * 98 * List contains both flink named and prime fd'd objects 99 */ 100 unsigned global_name; 101 102 /** 103 * Current tiling mode 104 */ 105 uint32_t tiling_mode; 106 uint32_t swizzle_mode; 107 uint32_t stride; 108 109 time_t free_time; 110 111 /** Mapped address for the buffer, saved across map/unmap cycles */ 112 void *map_cpu; 113 /** GTT virtual address for the buffer, saved across map/unmap cycles */ 114 void *map_gtt; 115 /** WC CPU address for the buffer, saved across map/unmap cycles */ 116 void *map_wc; 117 118 /** BO cache list */ 119 struct list_head head; 120 121 /** List of GEM handle exports of this buffer (bo_export) */ 122 struct list_head exports; 123 124 /** 125 * Boolean of whether this buffer can be re-used 126 */ 127 bool reusable; 128 129 /** 130 * Boolean of whether this buffer has been shared with an external client. 131 */ 132 bool external; 133 134 /** 135 * Boolean of whether this buffer is cache coherent 136 */ 137 bool cache_coherent; 138 139 /** 140 * Boolean of whether this buffer points into user memory 141 */ 142 bool userptr; 143 144 /** Pre-computed hash using _mesa_hash_pointer for cache tracking sets */ 145 uint32_t hash; 146}; 147 148#define BO_ALLOC_ZEROED (1 << 0) 149#define BO_ALLOC_COHERENT (1 << 1) 150 151/** 152 * Allocate a buffer object. 153 * 154 * Buffer objects are not necessarily initially mapped into CPU virtual 155 * address space or graphics device aperture. They must be mapped 156 * using crocus_bo_map() to be used by the CPU. 157 */ 158struct crocus_bo *crocus_bo_alloc(struct crocus_bufmgr *bufmgr, 159 const char *name, uint64_t size); 160 161/** 162 * Allocate a tiled buffer object. 163 * 164 * Alignment for tiled objects is set automatically; the 'flags' 165 * argument provides a hint about how the object will be used initially. 166 * 167 * Valid tiling formats are: 168 * I915_TILING_NONE 169 * I915_TILING_X 170 * I915_TILING_Y 171 */ 172struct crocus_bo *crocus_bo_alloc_tiled(struct crocus_bufmgr *bufmgr, 173 const char *name, uint64_t size, 174 uint32_t alignment, 175 uint32_t tiling_mode, uint32_t pitch, 176 unsigned flags); 177 178struct crocus_bo *crocus_bo_create_userptr(struct crocus_bufmgr *bufmgr, 179 const char *name, void *ptr, 180 size_t size); 181 182/** Takes a reference on a buffer object */ 183static inline void 184crocus_bo_reference(struct crocus_bo *bo) 185{ 186 p_atomic_inc(&bo->refcount); 187} 188 189static inline int 190atomic_add_unless(int *v, int add, int unless) 191{ 192 int c, old; 193 c = p_atomic_read(v); 194 while (c != unless && (old = p_atomic_cmpxchg(v, c, c + add)) != c) 195 c = old; 196 return c == unless; 197} 198 199void __crocus_bo_unreference(struct crocus_bo *bo); 200 201/** 202 * Releases a reference on a buffer object, freeing the data if 203 * no references remain. 204 */ 205static inline void crocus_bo_unreference(struct crocus_bo *bo) 206{ 207 if (bo == NULL) 208 return; 209 210 assert(p_atomic_read(&bo->refcount) > 0); 211 212 if (atomic_add_unless(&bo->refcount, -1, 1)) { 213 __crocus_bo_unreference(bo); 214 } 215} 216 217#define MAP_READ PIPE_MAP_READ 218#define MAP_WRITE PIPE_MAP_WRITE 219#define MAP_ASYNC PIPE_MAP_UNSYNCHRONIZED 220#define MAP_PERSISTENT PIPE_MAP_PERSISTENT 221#define MAP_COHERENT PIPE_MAP_COHERENT 222/* internal */ 223#define MAP_INTERNAL_MASK (0xff << 24) 224#define MAP_RAW (0x01 << 24) 225 226#define MAP_FLAGS (MAP_READ | MAP_WRITE | MAP_ASYNC | \ 227 MAP_PERSISTENT | MAP_COHERENT | MAP_INTERNAL_MASK) 228 229/** 230 * Maps the buffer into userspace. 231 * 232 * This function will block waiting for any existing execution on the 233 * buffer to complete, first. The resulting mapping is returned. 234 */ 235MUST_CHECK void *crocus_bo_map(struct pipe_debug_callback *dbg, 236 struct crocus_bo *bo, unsigned flags); 237 238/** 239 * Reduces the refcount on the userspace mapping of the buffer 240 * object. 241 */ 242static inline int crocus_bo_unmap(struct crocus_bo *bo) { return 0; } 243 244/** 245 * Waits for rendering to an object by the GPU to have completed. 246 * 247 * This is not required for any access to the BO by bo_map, 248 * bo_subdata, etc. It is merely a way for the driver to implement 249 * glFinish. 250 */ 251void crocus_bo_wait_rendering(struct crocus_bo *bo); 252 253/** 254 * Unref a buffer manager instance. 255 */ 256void crocus_bufmgr_unref(struct crocus_bufmgr *bufmgr); 257 258/** 259 * Get the current tiling (and resulting swizzling) mode for the bo. 260 * 261 * \param buf Buffer to get tiling mode for 262 * \param tiling_mode returned tiling mode 263 * \param swizzle_mode returned swizzling mode 264 */ 265int crocus_bo_get_tiling(struct crocus_bo *bo, uint32_t *tiling_mode, 266 uint32_t *swizzle_mode); 267 268/** 269 * Create a visible name for a buffer which can be used by other apps 270 * 271 * \param buf Buffer to create a name for 272 * \param name Returned name 273 */ 274int crocus_bo_flink(struct crocus_bo *bo, uint32_t *name); 275 276/** 277 * Is this buffer shared with external clients (exported)? 278 */ 279static inline bool 280crocus_bo_is_external(const struct crocus_bo *bo) 281{ 282 return bo->external; 283} 284 285/** 286 * Returns 1 if mapping the buffer for write could cause the process 287 * to block, due to the object being active in the GPU. 288 */ 289int crocus_bo_busy(struct crocus_bo *bo); 290 291/** 292 * Specify the volatility of the buffer. 293 * \param bo Buffer to create a name for 294 * \param madv The purgeable status 295 * 296 * Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be 297 * reclaimed under memory pressure. If you subsequently require the buffer, 298 * then you must pass I915_MADV_WILLNEED to mark the buffer as required. 299 * 300 * Returns 1 if the buffer was retained, or 0 if it was discarded whilst 301 * marked as I915_MADV_DONTNEED. 302 */ 303int crocus_bo_madvise(struct crocus_bo *bo, int madv); 304 305struct crocus_bufmgr * 306crocus_bufmgr_get_for_fd(struct intel_device_info *devinfo, int fd, 307 bool bo_reuse); 308int crocus_bufmgr_get_fd(struct crocus_bufmgr *bufmgr); 309 310struct crocus_bo *crocus_bo_gem_create_from_name(struct crocus_bufmgr *bufmgr, 311 const char *name, 312 unsigned handle); 313 314int crocus_bo_wait(struct crocus_bo *bo, int64_t timeout_ns); 315 316uint32_t crocus_create_hw_context(struct crocus_bufmgr *bufmgr); 317uint32_t crocus_clone_hw_context(struct crocus_bufmgr *bufmgr, uint32_t ctx_id); 318 319#define CROCUS_CONTEXT_LOW_PRIORITY ((I915_CONTEXT_MIN_USER_PRIORITY - 1) / 2) 320#define CROCUS_CONTEXT_MEDIUM_PRIORITY (I915_CONTEXT_DEFAULT_PRIORITY) 321#define CROCUS_CONTEXT_HIGH_PRIORITY ((I915_CONTEXT_MAX_USER_PRIORITY + 1) / 2) 322 323int crocus_hw_context_set_priority(struct crocus_bufmgr *bufmgr, 324 uint32_t ctx_id, int priority); 325 326void crocus_destroy_hw_context(struct crocus_bufmgr *bufmgr, uint32_t ctx_id); 327 328int crocus_bo_export_dmabuf(struct crocus_bo *bo, int *prime_fd); 329struct crocus_bo *crocus_bo_import_dmabuf(struct crocus_bufmgr *bufmgr, 330 int prime_fd, uint64_t modifier); 331struct crocus_bo *crocus_bo_import_dmabuf_no_mods(struct crocus_bufmgr *bufmgr, 332 int prime_fd); 333 334/** 335 * Exports a bo as a GEM handle into a given DRM file descriptor 336 * \param bo Buffer to export 337 * \param drm_fd File descriptor where the new handle is created 338 * \param out_handle Pointer to store the new handle 339 * 340 * Returns 0 if the buffer was successfully exported, a non zero error code 341 * otherwise. 342 */ 343int crocus_bo_export_gem_handle_for_device(struct crocus_bo *bo, int drm_fd, 344 uint32_t *out_handle); 345 346uint32_t crocus_bo_export_gem_handle(struct crocus_bo *bo); 347 348int crocus_reg_read(struct crocus_bufmgr *bufmgr, uint32_t offset, 349 uint64_t *out); 350 351int drm_ioctl(int fd, unsigned long request, void *arg); 352 353#endif /* CROCUS_BUFMGR_H */ 354