Path: blob/21.2-virgl/src/gallium/drivers/vc4/vc4_bufmgr.h
4570 views
/*1* Copyright © 2014 Broadcom2*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 VC4_BUFMGR_H24#define VC4_BUFMGR_H2526#include <stdint.h>27#include "util/u_hash_table.h"28#include "util/u_inlines.h"29#include "vc4_qir.h"3031struct vc4_context;3233struct vc4_bo {34struct pipe_reference reference;35struct vc4_screen *screen;36void *map;37const char *name;38uint32_t handle;39uint32_t size;4041/* This will be read/written by multiple threads without a lock -- you42* should take a snapshot and use it to see if you happen to be in the43* CL's handles at this position, to make most lookups O(1). It's44* volatile to make sure that the compiler doesn't emit multiple loads45* from the address, which would make the lookup racy.46*/47volatile uint32_t last_hindex;4849/** Entry in the linked list of buffers freed, by age. */50struct list_head time_list;51/** Entry in the per-page-count linked list of buffers freed (by age). */52struct list_head size_list;53/** Approximate second when the bo was freed. */54time_t free_time;55/**56* Whether only our process has a reference to the BO (meaning that57* it's safe to reuse it in the BO cache).58*/59bool private;60};6162struct vc4_bo *vc4_bo_alloc(struct vc4_screen *screen, uint32_t size,63const char *name);64struct vc4_bo *vc4_bo_alloc_shader(struct vc4_screen *screen, const void *data,65uint32_t size);66void vc4_bo_last_unreference(struct vc4_bo *bo);67void vc4_bo_last_unreference_locked_timed(struct vc4_bo *bo, time_t time);68struct vc4_bo *vc4_bo_open_name(struct vc4_screen *screen, uint32_t name);69struct vc4_bo *vc4_bo_open_dmabuf(struct vc4_screen *screen, int fd);70bool vc4_bo_flink(struct vc4_bo *bo, uint32_t *name);71int vc4_bo_get_dmabuf(struct vc4_bo *bo);7273void vc4_bo_debug_describe(char* buf, const struct vc4_bo *ptr);74static inline struct vc4_bo *75vc4_bo_reference(struct vc4_bo *bo)76{77pipe_reference_described(NULL, &bo->reference,78(debug_reference_descriptor)79vc4_bo_debug_describe);80return bo;81}8283static inline void84vc4_bo_unreference(struct vc4_bo **bo)85{86struct vc4_screen *screen;87if (!*bo)88return;8990if ((*bo)->private) {91/* Avoid the mutex for private BOs */92if (pipe_reference_described(&(*bo)->reference, NULL,93(debug_reference_descriptor)94vc4_bo_debug_describe)) {95vc4_bo_last_unreference(*bo);96}97} else {98screen = (*bo)->screen;99mtx_lock(&screen->bo_handles_mutex);100101if (pipe_reference_described(&(*bo)->reference, NULL,102(debug_reference_descriptor)103vc4_bo_debug_describe)) {104_mesa_hash_table_remove_key(screen->bo_handles,105(void *)(uintptr_t)(*bo)->handle);106vc4_bo_last_unreference(*bo);107}108109mtx_unlock(&screen->bo_handles_mutex);110}111112*bo = NULL;113}114115static inline void116vc4_bo_unreference_locked_timed(struct vc4_bo **bo, time_t time)117{118if (!*bo)119return;120121if (pipe_reference_described(&(*bo)->reference, NULL,122(debug_reference_descriptor)123vc4_bo_debug_describe)) {124vc4_bo_last_unreference_locked_timed(*bo, time);125}126*bo = NULL;127}128129void *130vc4_bo_map(struct vc4_bo *bo);131132void *133vc4_bo_map_unsynchronized(struct vc4_bo *bo);134135bool136vc4_bo_wait(struct vc4_bo *bo, uint64_t timeout_ns, const char *reason);137138bool139vc4_wait_seqno(struct vc4_screen *screen, uint64_t seqno, uint64_t timeout_ns,140const char *reason);141142void143vc4_bo_label(struct vc4_screen *screen, struct vc4_bo *bo, const char *fmt, ...);144145void146vc4_bufmgr_destroy(struct pipe_screen *pscreen);147148#endif /* VC4_BUFMGR_H */149150151152