1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2014 Broadcom 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifndef V3D_BUFMGR_H 25bf215546Sopenharmony_ci#define V3D_BUFMGR_H 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ci#include <stdint.h> 28bf215546Sopenharmony_ci#include "util/u_hash_table.h" 29bf215546Sopenharmony_ci#include "util/u_inlines.h" 30bf215546Sopenharmony_ci#include "util/list.h" 31bf215546Sopenharmony_ci#include "v3d_screen.h" 32bf215546Sopenharmony_ci 33bf215546Sopenharmony_cistruct v3d_context; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cistruct v3d_bo { 36bf215546Sopenharmony_ci struct pipe_reference reference; 37bf215546Sopenharmony_ci struct v3d_screen *screen; 38bf215546Sopenharmony_ci void *map; 39bf215546Sopenharmony_ci const char *name; 40bf215546Sopenharmony_ci uint32_t handle; 41bf215546Sopenharmony_ci uint32_t size; 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_ci /* Address of the BO in our page tables. */ 44bf215546Sopenharmony_ci uint32_t offset; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci /** Entry in the linked list of buffers freed, by age. */ 47bf215546Sopenharmony_ci struct list_head time_list; 48bf215546Sopenharmony_ci /** Entry in the per-page-count linked list of buffers freed (by age). */ 49bf215546Sopenharmony_ci struct list_head size_list; 50bf215546Sopenharmony_ci /** Approximate second when the bo was freed. */ 51bf215546Sopenharmony_ci time_t free_time; 52bf215546Sopenharmony_ci /** 53bf215546Sopenharmony_ci * Whether only our process has a reference to the BO (meaning that 54bf215546Sopenharmony_ci * it's safe to reuse it in the BO cache). 55bf215546Sopenharmony_ci */ 56bf215546Sopenharmony_ci bool private; 57bf215546Sopenharmony_ci}; 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_cistruct v3d_bo *v3d_bo_alloc(struct v3d_screen *screen, uint32_t size, 60bf215546Sopenharmony_ci const char *name); 61bf215546Sopenharmony_civoid v3d_bo_last_unreference(struct v3d_bo *bo); 62bf215546Sopenharmony_civoid v3d_bo_last_unreference_locked_timed(struct v3d_bo *bo, time_t time); 63bf215546Sopenharmony_cistruct v3d_bo *v3d_bo_open_name(struct v3d_screen *screen, uint32_t name); 64bf215546Sopenharmony_cistruct v3d_bo *v3d_bo_open_dmabuf(struct v3d_screen *screen, int fd); 65bf215546Sopenharmony_cibool v3d_bo_flink(struct v3d_bo *bo, uint32_t *name); 66bf215546Sopenharmony_ciint v3d_bo_get_dmabuf(struct v3d_bo *bo); 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_cistatic inline void 69bf215546Sopenharmony_civ3d_bo_set_reference(struct v3d_bo **old_bo, struct v3d_bo *new_bo) 70bf215546Sopenharmony_ci{ 71bf215546Sopenharmony_ci if (pipe_reference(&(*old_bo)->reference, &new_bo->reference)) 72bf215546Sopenharmony_ci v3d_bo_last_unreference(*old_bo); 73bf215546Sopenharmony_ci *old_bo = new_bo; 74bf215546Sopenharmony_ci} 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_cistatic inline struct v3d_bo * 77bf215546Sopenharmony_civ3d_bo_reference(struct v3d_bo *bo) 78bf215546Sopenharmony_ci{ 79bf215546Sopenharmony_ci pipe_reference(NULL, &bo->reference); 80bf215546Sopenharmony_ci return bo; 81bf215546Sopenharmony_ci} 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_cistatic inline void 84bf215546Sopenharmony_civ3d_bo_unreference(struct v3d_bo **bo) 85bf215546Sopenharmony_ci{ 86bf215546Sopenharmony_ci struct v3d_screen *screen; 87bf215546Sopenharmony_ci if (!*bo) 88bf215546Sopenharmony_ci return; 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci if ((*bo)->private) { 91bf215546Sopenharmony_ci /* Avoid the mutex for private BOs */ 92bf215546Sopenharmony_ci if (pipe_reference(&(*bo)->reference, NULL)) 93bf215546Sopenharmony_ci v3d_bo_last_unreference(*bo); 94bf215546Sopenharmony_ci } else { 95bf215546Sopenharmony_ci screen = (*bo)->screen; 96bf215546Sopenharmony_ci mtx_lock(&screen->bo_handles_mutex); 97bf215546Sopenharmony_ci 98bf215546Sopenharmony_ci if (pipe_reference(&(*bo)->reference, NULL)) { 99bf215546Sopenharmony_ci _mesa_hash_table_remove_key(screen->bo_handles, 100bf215546Sopenharmony_ci (void *)(uintptr_t)(*bo)->handle); 101bf215546Sopenharmony_ci v3d_bo_last_unreference(*bo); 102bf215546Sopenharmony_ci } 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci mtx_unlock(&screen->bo_handles_mutex); 105bf215546Sopenharmony_ci } 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci *bo = NULL; 108bf215546Sopenharmony_ci} 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_cistatic inline void 111bf215546Sopenharmony_civ3d_bo_unreference_locked_timed(struct v3d_bo **bo, time_t time) 112bf215546Sopenharmony_ci{ 113bf215546Sopenharmony_ci if (!*bo) 114bf215546Sopenharmony_ci return; 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_ci if (pipe_reference(&(*bo)->reference, NULL)) 117bf215546Sopenharmony_ci v3d_bo_last_unreference_locked_timed(*bo, time); 118bf215546Sopenharmony_ci *bo = NULL; 119bf215546Sopenharmony_ci} 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_civoid * 122bf215546Sopenharmony_civ3d_bo_map(struct v3d_bo *bo); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_civoid * 125bf215546Sopenharmony_civ3d_bo_map_unsynchronized(struct v3d_bo *bo); 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_cibool 128bf215546Sopenharmony_civ3d_bo_wait(struct v3d_bo *bo, uint64_t timeout_ns, const char *reason); 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_cibool 131bf215546Sopenharmony_civ3d_wait_seqno(struct v3d_screen *screen, uint64_t seqno, uint64_t timeout_ns, 132bf215546Sopenharmony_ci const char *reason); 133bf215546Sopenharmony_ci 134bf215546Sopenharmony_civoid 135bf215546Sopenharmony_civ3d_bufmgr_destroy(struct pipe_screen *pscreen); 136bf215546Sopenharmony_ci 137bf215546Sopenharmony_ci#endif /* V3D_BUFMGR_H */ 138bf215546Sopenharmony_ci 139