Path: blob/21.2-virgl/src/gallium/drivers/iris/iris_bufmgr.h
4565 views
/*1* Copyright © 2017 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#ifndef IRIS_BUFMGR_H24#define IRIS_BUFMGR_H2526#include <stdbool.h>27#include <stdint.h>28#include <stdio.h>29#include <sys/types.h>30#include "c11/threads.h"31#include "util/macros.h"32#include "util/u_atomic.h"33#include "util/list.h"34#include "pipe/p_defines.h"3536struct iris_batch;37struct intel_device_info;38struct pipe_debug_callback;39struct isl_surf;4041/**42* Memory zones. When allocating a buffer, you can request that it is43* placed into a specific region of the virtual address space (PPGTT).44*45* Most buffers can go anywhere (IRIS_MEMZONE_OTHER). Some buffers are46* accessed via an offset from a base address. STATE_BASE_ADDRESS has47* a maximum 4GB size for each region, so we need to restrict those48* buffers to be within 4GB of the base. Each memory zone corresponds49* to a particular base address.50*51* We lay out the virtual address space as follows:52*53* - [0, 4K): Nothing (empty page for null address)54* - [4K, 4G): Shaders (Instruction Base Address)55* - [4G, 8G): Surfaces & Binders (Surface State Base Address, Bindless ...)56* - [8G, 12G): Dynamic (Dynamic State Base Address)57* - [12G, *): Other (everything else in the full 48-bit VMA)58*59* A special buffer for border color lives at the start of the dynamic state60* memory zone. This unfortunately has to be handled specially because the61* SAMPLER_STATE "Indirect State Pointer" field is only a 24-bit pointer.62*63* Each GL context uses a separate GEM context, which technically gives them64* each a separate VMA. However, we assign address globally, so buffers will65* have the same address in all GEM contexts. This lets us have a single BO66* field for the address, which is easy and cheap.67*/68enum iris_memory_zone {69IRIS_MEMZONE_SHADER,70IRIS_MEMZONE_BINDER,71IRIS_MEMZONE_BINDLESS,72IRIS_MEMZONE_SURFACE,73IRIS_MEMZONE_DYNAMIC,74IRIS_MEMZONE_OTHER,7576IRIS_MEMZONE_BORDER_COLOR_POOL,77};7879/* Intentionally exclude single buffer "zones" */80#define IRIS_MEMZONE_COUNT (IRIS_MEMZONE_OTHER + 1)8182#define IRIS_BINDER_SIZE (64 * 1024)83#define IRIS_MAX_BINDERS 10084#define IRIS_BINDLESS_SIZE (8 * 1024 * 1024)8586#define IRIS_MEMZONE_SHADER_START (0ull * (1ull << 32))87#define IRIS_MEMZONE_BINDER_START (1ull * (1ull << 32))88#define IRIS_MEMZONE_BINDLESS_START (IRIS_MEMZONE_BINDER_START + IRIS_MAX_BINDERS * IRIS_BINDER_SIZE)89#define IRIS_MEMZONE_SURFACE_START (IRIS_MEMZONE_BINDLESS_START + IRIS_BINDLESS_SIZE)90#define IRIS_MEMZONE_DYNAMIC_START (2ull * (1ull << 32))91#define IRIS_MEMZONE_OTHER_START (3ull * (1ull << 32))9293#define IRIS_BORDER_COLOR_POOL_ADDRESS IRIS_MEMZONE_DYNAMIC_START94#define IRIS_BORDER_COLOR_POOL_SIZE (64 * 1024)9596/**97* Classification of the various incoherent caches of the GPU into a number of98* caching domains.99*/100enum iris_domain {101/** Render color cache. */102IRIS_DOMAIN_RENDER_WRITE = 0,103/** (Hi)Z/stencil cache. */104IRIS_DOMAIN_DEPTH_WRITE,105/** Any other read-write cache. */106IRIS_DOMAIN_OTHER_WRITE,107/** Any other read-only cache. */108IRIS_DOMAIN_OTHER_READ,109/** Number of caching domains. */110NUM_IRIS_DOMAINS,111/** Not a real cache, use to opt out of the cache tracking mechanism. */112IRIS_DOMAIN_NONE = NUM_IRIS_DOMAINS113};114115/**116* Whether a caching domain is guaranteed not to write any data to memory.117*/118static inline bool119iris_domain_is_read_only(enum iris_domain access)120{121return access == IRIS_DOMAIN_OTHER_READ;122}123124enum iris_mmap_mode {125IRIS_MMAP_UC, /**< Fully uncached memory map */126IRIS_MMAP_WC, /**< Write-combining map with no caching of reads */127IRIS_MMAP_WB, /**< Write-back mapping with CPU caches enabled */128};129130struct iris_bo {131/**132* Size in bytes of the buffer object.133*134* The size may be larger than the size originally requested for the135* allocation, such as being aligned to page size.136*/137uint64_t size;138139/** Buffer manager context associated with this buffer object */140struct iris_bufmgr *bufmgr;141142/** Pre-computed hash using _mesa_hash_pointer for cache tracking sets */143uint32_t hash;144145/** The GEM handle for this buffer object. */146uint32_t gem_handle;147148/**149* Virtual address of the buffer inside the PPGTT (Per-Process Graphics150* Translation Table).151*152* Although each hardware context has its own VMA, we assign BO's to the153* same address in all contexts, for simplicity.154*/155uint64_t gtt_offset;156157/**158* If non-zero, then this bo has an aux-map translation to this address.159*/160uint64_t aux_map_address;161162/**163* The validation list index for this buffer, or -1 when not in a batch.164* Note that a single buffer may be in multiple batches (contexts), and165* this is a global field, which refers to the last batch using the BO.166* It should not be considered authoritative, but can be used to avoid a167* linear walk of the validation list in the common case by guessing that168* exec_bos[bo->index] == bo and confirming whether that's the case.169*170* XXX: this is not ideal now that we have more than one batch per context,171* XXX: as the index will flop back and forth between the render index and172* XXX: compute index...173*/174unsigned index;175176int refcount;177const char *name;178179uint64_t kflags;180181/**182* Kernel-assigned global name for this object183*184* List contains both flink named and prime fd'd objects185*/186unsigned global_name;187188time_t free_time;189190/** Mapped address for the buffer, saved across map/unmap cycles */191void *map;192193/** BO cache list */194struct list_head head;195196/** List of GEM handle exports of this buffer (bo_export) */197struct list_head exports;198199/**200* Synchronization sequence number of most recent access of this BO from201* each caching domain.202*203* Although this is a global field, use in multiple contexts should be204* safe, see iris_emit_buffer_barrier_for() for details.205*206* Also align it to 64 bits. This will make atomic operations faster on 32207* bit platforms.208*/209uint64_t last_seqnos[NUM_IRIS_DOMAINS] __attribute__ ((aligned (8)));210211/**212* Boolean of whether the GPU is definitely not accessing the buffer.213*214* This is only valid when reusable, since non-reusable215* buffers are those that have been shared with other216* processes, so we don't know their state.217*/218bool idle;219220/**221* Boolean of whether this buffer can be re-used222*/223bool reusable;224225/** Was this buffer imported from an external client? */226bool imported;227228/** Has this buffer been exported to external clients? */229bool exported;230231/**232* Boolean of whether this buffer points into user memory233*/234bool userptr;235236/** The mmap coherency mode selected at BO allocation time */237enum iris_mmap_mode mmap_mode;238239/**240* Boolean of whether this was allocated from local memory241*/242bool local;243};244245#define BO_ALLOC_ZEROED (1<<0)246#define BO_ALLOC_COHERENT (1<<1)247#define BO_ALLOC_SMEM (1<<2)248249/**250* Allocate a buffer object.251*252* Buffer objects are not necessarily initially mapped into CPU virtual253* address space or graphics device aperture. They must be mapped254* using iris_bo_map() to be used by the CPU.255*/256struct iris_bo *iris_bo_alloc(struct iris_bufmgr *bufmgr,257const char *name,258uint64_t size,259uint32_t alignment,260enum iris_memory_zone memzone,261unsigned flags);262263struct iris_bo *264iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name,265void *ptr, size_t size,266enum iris_memory_zone memzone);267268/** Takes a reference on a buffer object */269static inline void270iris_bo_reference(struct iris_bo *bo)271{272p_atomic_inc(&bo->refcount);273}274275/**276* Releases a reference on a buffer object, freeing the data if277* no references remain.278*/279void iris_bo_unreference(struct iris_bo *bo);280281#define MAP_READ PIPE_MAP_READ282#define MAP_WRITE PIPE_MAP_WRITE283#define MAP_ASYNC PIPE_MAP_UNSYNCHRONIZED284#define MAP_PERSISTENT PIPE_MAP_PERSISTENT285#define MAP_COHERENT PIPE_MAP_COHERENT286/* internal */287#define MAP_RAW (PIPE_MAP_DRV_PRV << 0)288#define MAP_INTERNAL_MASK (MAP_RAW)289290#define MAP_FLAGS (MAP_READ | MAP_WRITE | MAP_ASYNC | \291MAP_PERSISTENT | MAP_COHERENT | MAP_INTERNAL_MASK)292293/**294* Maps the buffer into userspace.295*296* This function will block waiting for any existing execution on the297* buffer to complete, first. The resulting mapping is returned.298*/299MUST_CHECK void *iris_bo_map(struct pipe_debug_callback *dbg,300struct iris_bo *bo, unsigned flags);301302/**303* Reduces the refcount on the userspace mapping of the buffer304* object.305*/306static inline int iris_bo_unmap(struct iris_bo *bo) { return 0; }307308/**309* Waits for rendering to an object by the GPU to have completed.310*311* This is not required for any access to the BO by bo_map,312* bo_subdata, etc. It is merely a way for the driver to implement313* glFinish.314*/315void iris_bo_wait_rendering(struct iris_bo *bo);316317318/**319* Unref a buffer manager instance.320*/321void iris_bufmgr_unref(struct iris_bufmgr *bufmgr);322323/**324* Create a visible name for a buffer which can be used by other apps325*326* \param buf Buffer to create a name for327* \param name Returned name328*/329int iris_bo_flink(struct iris_bo *bo, uint32_t *name);330331/**332* Is this buffer shared with external clients (imported or exported)?333*/334static inline bool335iris_bo_is_external(const struct iris_bo *bo)336{337return bo->exported || bo->imported;338}339340/**341* Mark a buffer as being shared with other external clients.342*/343void iris_bo_mark_exported(struct iris_bo *bo);344345/**346* Returns 1 if mapping the buffer for write could cause the process347* to block, due to the object being active in the GPU.348*/349int iris_bo_busy(struct iris_bo *bo);350351/**352* Specify the volatility of the buffer.353* \param bo Buffer to create a name for354* \param madv The purgeable status355*356* Use I915_MADV_DONTNEED to mark the buffer as purgeable, and it will be357* reclaimed under memory pressure. If you subsequently require the buffer,358* then you must pass I915_MADV_WILLNEED to mark the buffer as required.359*360* Returns 1 if the buffer was retained, or 0 if it was discarded whilst361* marked as I915_MADV_DONTNEED.362*/363int iris_bo_madvise(struct iris_bo *bo, int madv);364365/* drm_bacon_bufmgr_gem.c */366struct iris_bufmgr *iris_bufmgr_get_for_fd(struct intel_device_info *devinfo,367int fd, bool bo_reuse);368int iris_bufmgr_get_fd(struct iris_bufmgr *bufmgr);369370struct iris_bo *iris_bo_gem_create_from_name(struct iris_bufmgr *bufmgr,371const char *name,372unsigned handle);373374void* iris_bufmgr_get_aux_map_context(struct iris_bufmgr *bufmgr);375376int iris_bo_wait(struct iris_bo *bo, int64_t timeout_ns);377378uint32_t iris_create_hw_context(struct iris_bufmgr *bufmgr);379uint32_t iris_clone_hw_context(struct iris_bufmgr *bufmgr, uint32_t ctx_id);380381#define IRIS_CONTEXT_LOW_PRIORITY ((I915_CONTEXT_MIN_USER_PRIORITY-1)/2)382#define IRIS_CONTEXT_MEDIUM_PRIORITY (I915_CONTEXT_DEFAULT_PRIORITY)383#define IRIS_CONTEXT_HIGH_PRIORITY ((I915_CONTEXT_MAX_USER_PRIORITY+1)/2)384385int iris_hw_context_set_priority(struct iris_bufmgr *bufmgr,386uint32_t ctx_id, int priority);387388void iris_destroy_hw_context(struct iris_bufmgr *bufmgr, uint32_t ctx_id);389390int iris_gem_get_tiling(struct iris_bo *bo, uint32_t *tiling);391int iris_gem_set_tiling(struct iris_bo *bo, const struct isl_surf *surf);392393int iris_bo_export_dmabuf(struct iris_bo *bo, int *prime_fd);394struct iris_bo *iris_bo_import_dmabuf(struct iris_bufmgr *bufmgr, int prime_fd);395396/**397* Exports a bo as a GEM handle into a given DRM file descriptor398* \param bo Buffer to export399* \param drm_fd File descriptor where the new handle is created400* \param out_handle Pointer to store the new handle401*402* Returns 0 if the buffer was successfully exported, a non zero error code403* otherwise.404*/405int iris_bo_export_gem_handle_for_device(struct iris_bo *bo, int drm_fd,406uint32_t *out_handle);407408uint32_t iris_bo_export_gem_handle(struct iris_bo *bo);409410int iris_reg_read(struct iris_bufmgr *bufmgr, uint32_t offset, uint64_t *out);411412int drm_ioctl(int fd, unsigned long request, void *arg);413414/**415* Returns the BO's address relative to the appropriate base address.416*417* All of our base addresses are programmed to the start of a 4GB region,418* so simply returning the bottom 32 bits of the BO address will give us419* the offset from whatever base address corresponds to that memory region.420*/421static inline uint32_t422iris_bo_offset_from_base_address(struct iris_bo *bo)423{424/* This only works for buffers in the memory zones corresponding to a425* base address - the top, unbounded memory zone doesn't have a base.426*/427assert(bo->gtt_offset < IRIS_MEMZONE_OTHER_START);428return bo->gtt_offset;429}430431/**432* Track access of a BO from the specified caching domain and sequence number.433*434* Can be used without locking. Only the most recent access (i.e. highest435* seqno) is tracked.436*/437static inline void438iris_bo_bump_seqno(struct iris_bo *bo, uint64_t seqno,439enum iris_domain type)440{441uint64_t *const last_seqno = &bo->last_seqnos[type];442uint64_t tmp, prev_seqno = p_atomic_read(last_seqno);443444while (prev_seqno < seqno &&445prev_seqno != (tmp = p_atomic_cmpxchg(last_seqno, prev_seqno, seqno)))446prev_seqno = tmp;447}448449enum iris_memory_zone iris_memzone_for_address(uint64_t address);450451#endif /* IRIS_BUFMGR_H */452453454