1 /*
2 * © Copyright 2019 Alyssa Rosenzweig
3 * © Copyright 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #ifndef __PAN_BO_H__
27 #define __PAN_BO_H__
28
29 #include <time.h>
30 #include "util/list.h"
31
32 #include "pan_pool.h"
33
34 #include "kmod/pan_kmod.h"
35
36 /* Flags for allocated memory */
37
38 /* This memory region is executable */
39 #define PAN_BO_EXECUTE (1 << 0)
40
41 /* This memory region should be lazily allocated and grow-on-page-fault. Must
42 * be used in conjunction with INVISIBLE */
43 #define PAN_BO_GROWABLE (1 << 1)
44
45 /* This memory region should not be mapped to the CPU */
46 #define PAN_BO_INVISIBLE (1 << 2)
47
48 /* This region may not be used immediately and will not mmap on allocate
49 * (semantically distinct from INVISIBLE, which cannot never be mmaped) */
50 #define PAN_BO_DELAY_MMAP (1 << 3)
51
52 /* BO is shared across processes (imported or exported) and therefore cannot be
53 * cached locally */
54 #define PAN_BO_SHARED (1 << 4)
55
56 /* BO might be exported at some point. PAN_BO_SHAREABLE does not imply
57 * PAN_BO_SHARED if the BO has not been exported yet */
58 #define PAN_BO_SHAREABLE (1 << 5)
59
60 /* GPU access flags */
61
62 /* BO is either shared (can be accessed by more than one GPU batch) or private
63 * (reserved by a specific GPU job). */
64 #define PAN_BO_ACCESS_PRIVATE (0 << 0)
65 #define PAN_BO_ACCESS_SHARED (1 << 0)
66
67 /* BO is being read/written by the GPU */
68 #define PAN_BO_ACCESS_READ (1 << 1)
69 #define PAN_BO_ACCESS_WRITE (1 << 2)
70 #define PAN_BO_ACCESS_RW (PAN_BO_ACCESS_READ | PAN_BO_ACCESS_WRITE)
71
72 /* BO is accessed by the vertex/tiler job. */
73 #define PAN_BO_ACCESS_VERTEX_TILER (1 << 3)
74
75 /* BO is accessed by the fragment job. */
76 #define PAN_BO_ACCESS_FRAGMENT (1 << 4)
77
78 typedef uint8_t pan_bo_access;
79
80 struct panfrost_device;
81
82 struct panfrost_bo {
83 /* Must be first for casting */
84 struct list_head bucket_link;
85
86 /* Used to link the BO to the BO cache LRU list. */
87 struct list_head lru_link;
88
89 /* Store the time this BO was use last, so the BO cache logic can evict
90 * stale BOs.
91 */
92 time_t last_used;
93
94 /* Atomic reference count */
95 int32_t refcnt;
96
97 /* Kernel representation of a buffer object. */
98 struct pan_kmod_bo *kmod_bo;
99
100 struct panfrost_device *dev;
101
102 /* Mapping for the entire object (all levels) */
103 struct panfrost_ptr ptr;
104
105 uint32_t flags;
106
107 /* Combination of PAN_BO_ACCESS_{READ,WRITE} flags encoding pending
108 * GPU accesses to this BO. Useful to avoid calling the WAIT_BO ioctl
109 * when the BO is idle.
110 */
111 uint32_t gpu_access;
112
113 /* Human readable description of the BO for debugging. */
114 const char *label;
115 };
116
117 static inline size_t
panfrost_bo_size(struct panfrost_bo * bo)118 panfrost_bo_size(struct panfrost_bo *bo)
119 {
120 return bo->kmod_bo->size;
121 }
122
123 static inline size_t
panfrost_bo_handle(struct panfrost_bo * bo)124 panfrost_bo_handle(struct panfrost_bo *bo)
125 {
126 return bo->kmod_bo->handle;
127 }
128
129 struct panfrost_bo *panfrost_bo_from_kmod_bo(struct panfrost_device *dev,
130 struct pan_kmod_bo *kmod_bo);
131
132 bool panfrost_bo_wait(struct panfrost_bo *bo, int64_t timeout_ns,
133 bool wait_readers);
134 void panfrost_bo_reference(struct panfrost_bo *bo);
135 void panfrost_bo_unreference(struct panfrost_bo *bo);
136 struct panfrost_bo *panfrost_bo_create(struct panfrost_device *dev, size_t size,
137 uint32_t flags, const char *label);
138 int panfrost_bo_mmap(struct panfrost_bo *bo);
139 struct panfrost_bo *panfrost_bo_import(struct panfrost_device *dev, int fd);
140 int panfrost_bo_export(struct panfrost_bo *bo);
141 void panfrost_bo_cache_evict_all(struct panfrost_device *dev);
142
143 #endif /* __PAN_BO_H__ */
144