101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014 Broadcom
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg */
2301e04c3fSmrg
247ec681f3Smrg#ifndef V3D_BUFMGR_H
257ec681f3Smrg#define V3D_BUFMGR_H
2601e04c3fSmrg
2701e04c3fSmrg#include <stdint.h>
2801e04c3fSmrg#include "util/u_hash_table.h"
2901e04c3fSmrg#include "util/u_inlines.h"
3001e04c3fSmrg#include "util/list.h"
3101e04c3fSmrg#include "v3d_screen.h"
3201e04c3fSmrg
3301e04c3fSmrgstruct v3d_context;
3401e04c3fSmrg
3501e04c3fSmrgstruct v3d_bo {
3601e04c3fSmrg        struct pipe_reference reference;
3701e04c3fSmrg        struct v3d_screen *screen;
3801e04c3fSmrg        void *map;
3901e04c3fSmrg        const char *name;
4001e04c3fSmrg        uint32_t handle;
4101e04c3fSmrg        uint32_t size;
4201e04c3fSmrg
4301e04c3fSmrg        /* Address of the BO in our page tables. */
4401e04c3fSmrg        uint32_t offset;
4501e04c3fSmrg
4601e04c3fSmrg        /** Entry in the linked list of buffers freed, by age. */
4701e04c3fSmrg        struct list_head time_list;
4801e04c3fSmrg        /** Entry in the per-page-count linked list of buffers freed (by age). */
4901e04c3fSmrg        struct list_head size_list;
5001e04c3fSmrg        /** Approximate second when the bo was freed. */
5101e04c3fSmrg        time_t free_time;
5201e04c3fSmrg        /**
5301e04c3fSmrg         * Whether only our process has a reference to the BO (meaning that
5401e04c3fSmrg         * it's safe to reuse it in the BO cache).
5501e04c3fSmrg         */
5601e04c3fSmrg        bool private;
5701e04c3fSmrg};
5801e04c3fSmrg
5901e04c3fSmrgstruct v3d_bo *v3d_bo_alloc(struct v3d_screen *screen, uint32_t size,
6001e04c3fSmrg                            const char *name);
6101e04c3fSmrgvoid v3d_bo_last_unreference(struct v3d_bo *bo);
6201e04c3fSmrgvoid v3d_bo_last_unreference_locked_timed(struct v3d_bo *bo, time_t time);
639f464c52Smayastruct v3d_bo *v3d_bo_open_name(struct v3d_screen *screen, uint32_t name);
649f464c52Smayastruct v3d_bo *v3d_bo_open_dmabuf(struct v3d_screen *screen, int fd);
6501e04c3fSmrgbool v3d_bo_flink(struct v3d_bo *bo, uint32_t *name);
6601e04c3fSmrgint v3d_bo_get_dmabuf(struct v3d_bo *bo);
6701e04c3fSmrg
6801e04c3fSmrgstatic inline void
6901e04c3fSmrgv3d_bo_set_reference(struct v3d_bo **old_bo, struct v3d_bo *new_bo)
7001e04c3fSmrg{
7101e04c3fSmrg        if (pipe_reference(&(*old_bo)->reference, &new_bo->reference))
7201e04c3fSmrg                v3d_bo_last_unreference(*old_bo);
7301e04c3fSmrg        *old_bo = new_bo;
7401e04c3fSmrg}
7501e04c3fSmrg
7601e04c3fSmrgstatic inline struct v3d_bo *
7701e04c3fSmrgv3d_bo_reference(struct v3d_bo *bo)
7801e04c3fSmrg{
7901e04c3fSmrg        pipe_reference(NULL, &bo->reference);
8001e04c3fSmrg        return bo;
8101e04c3fSmrg}
8201e04c3fSmrg
8301e04c3fSmrgstatic inline void
8401e04c3fSmrgv3d_bo_unreference(struct v3d_bo **bo)
8501e04c3fSmrg{
8601e04c3fSmrg        struct v3d_screen *screen;
8701e04c3fSmrg        if (!*bo)
8801e04c3fSmrg                return;
8901e04c3fSmrg
9001e04c3fSmrg        if ((*bo)->private) {
9101e04c3fSmrg                /* Avoid the mutex for private BOs */
9201e04c3fSmrg                if (pipe_reference(&(*bo)->reference, NULL))
9301e04c3fSmrg                        v3d_bo_last_unreference(*bo);
9401e04c3fSmrg        } else {
9501e04c3fSmrg                screen = (*bo)->screen;
9601e04c3fSmrg                mtx_lock(&screen->bo_handles_mutex);
9701e04c3fSmrg
9801e04c3fSmrg                if (pipe_reference(&(*bo)->reference, NULL)) {
997ec681f3Smrg                        _mesa_hash_table_remove_key(screen->bo_handles,
10001e04c3fSmrg                                               (void *)(uintptr_t)(*bo)->handle);
10101e04c3fSmrg                        v3d_bo_last_unreference(*bo);
10201e04c3fSmrg                }
10301e04c3fSmrg
10401e04c3fSmrg                mtx_unlock(&screen->bo_handles_mutex);
10501e04c3fSmrg        }
10601e04c3fSmrg
10701e04c3fSmrg        *bo = NULL;
10801e04c3fSmrg}
10901e04c3fSmrg
11001e04c3fSmrgstatic inline void
11101e04c3fSmrgv3d_bo_unreference_locked_timed(struct v3d_bo **bo, time_t time)
11201e04c3fSmrg{
11301e04c3fSmrg        if (!*bo)
11401e04c3fSmrg                return;
11501e04c3fSmrg
11601e04c3fSmrg        if (pipe_reference(&(*bo)->reference, NULL))
11701e04c3fSmrg                v3d_bo_last_unreference_locked_timed(*bo, time);
11801e04c3fSmrg        *bo = NULL;
11901e04c3fSmrg}
12001e04c3fSmrg
12101e04c3fSmrgvoid *
12201e04c3fSmrgv3d_bo_map(struct v3d_bo *bo);
12301e04c3fSmrg
12401e04c3fSmrgvoid *
12501e04c3fSmrgv3d_bo_map_unsynchronized(struct v3d_bo *bo);
12601e04c3fSmrg
12701e04c3fSmrgbool
12801e04c3fSmrgv3d_bo_wait(struct v3d_bo *bo, uint64_t timeout_ns, const char *reason);
12901e04c3fSmrg
13001e04c3fSmrgbool
13101e04c3fSmrgv3d_wait_seqno(struct v3d_screen *screen, uint64_t seqno, uint64_t timeout_ns,
13201e04c3fSmrg               const char *reason);
13301e04c3fSmrg
13401e04c3fSmrgvoid
13501e04c3fSmrgv3d_bufmgr_destroy(struct pipe_screen *pscreen);
13601e04c3fSmrg
1377ec681f3Smrg#endif /* V3D_BUFMGR_H */
13801e04c3fSmrg
139