1/*
2 * Copyright © 2014 Broadcom
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 V3D_BUFMGR_H
25#define V3D_BUFMGR_H
26
27#include <stdint.h>
28#include "util/u_hash_table.h"
29#include "util/u_inlines.h"
30#include "util/list.h"
31#include "v3d_screen.h"
32
33struct v3d_context;
34
35struct v3d_bo {
36        struct pipe_reference reference;
37        struct v3d_screen *screen;
38        void *map;
39        const char *name;
40        uint32_t handle;
41        uint32_t size;
42
43        /* Address of the BO in our page tables. */
44        uint32_t offset;
45
46        /** Entry in the linked list of buffers freed, by age. */
47        struct list_head time_list;
48        /** Entry in the per-page-count linked list of buffers freed (by age). */
49        struct list_head size_list;
50        /** Approximate second when the bo was freed. */
51        time_t free_time;
52        /**
53         * Whether only our process has a reference to the BO (meaning that
54         * it's safe to reuse it in the BO cache).
55         */
56        bool private;
57};
58
59struct v3d_bo *v3d_bo_alloc(struct v3d_screen *screen, uint32_t size,
60                            const char *name);
61void v3d_bo_last_unreference(struct v3d_bo *bo);
62void v3d_bo_last_unreference_locked_timed(struct v3d_bo *bo, time_t time);
63struct v3d_bo *v3d_bo_open_name(struct v3d_screen *screen, uint32_t name);
64struct v3d_bo *v3d_bo_open_dmabuf(struct v3d_screen *screen, int fd);
65bool v3d_bo_flink(struct v3d_bo *bo, uint32_t *name);
66int v3d_bo_get_dmabuf(struct v3d_bo *bo);
67
68static inline void
69v3d_bo_set_reference(struct v3d_bo **old_bo, struct v3d_bo *new_bo)
70{
71        if (pipe_reference(&(*old_bo)->reference, &new_bo->reference))
72                v3d_bo_last_unreference(*old_bo);
73        *old_bo = new_bo;
74}
75
76static inline struct v3d_bo *
77v3d_bo_reference(struct v3d_bo *bo)
78{
79        pipe_reference(NULL, &bo->reference);
80        return bo;
81}
82
83static inline void
84v3d_bo_unreference(struct v3d_bo **bo)
85{
86        struct v3d_screen *screen;
87        if (!*bo)
88                return;
89
90        if ((*bo)->private) {
91                /* Avoid the mutex for private BOs */
92                if (pipe_reference(&(*bo)->reference, NULL))
93                        v3d_bo_last_unreference(*bo);
94        } else {
95                screen = (*bo)->screen;
96                mtx_lock(&screen->bo_handles_mutex);
97
98                if (pipe_reference(&(*bo)->reference, NULL)) {
99                        _mesa_hash_table_remove_key(screen->bo_handles,
100                                               (void *)(uintptr_t)(*bo)->handle);
101                        v3d_bo_last_unreference(*bo);
102                }
103
104                mtx_unlock(&screen->bo_handles_mutex);
105        }
106
107        *bo = NULL;
108}
109
110static inline void
111v3d_bo_unreference_locked_timed(struct v3d_bo **bo, time_t time)
112{
113        if (!*bo)
114                return;
115
116        if (pipe_reference(&(*bo)->reference, NULL))
117                v3d_bo_last_unreference_locked_timed(*bo, time);
118        *bo = NULL;
119}
120
121void *
122v3d_bo_map(struct v3d_bo *bo);
123
124void *
125v3d_bo_map_unsynchronized(struct v3d_bo *bo);
126
127bool
128v3d_bo_wait(struct v3d_bo *bo, uint64_t timeout_ns, const char *reason);
129
130bool
131v3d_wait_seqno(struct v3d_screen *screen, uint64_t seqno, uint64_t timeout_ns,
132               const char *reason);
133
134void
135v3d_bufmgr_destroy(struct pipe_screen *pscreen);
136
137#endif /* V3D_BUFMGR_H */
138
139