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 #include "panfrost-job.h"
32
33 #include "pan_pool.h"
34
35 #include "kmod/pan_kmod.h"
36
37 /* Flags for allocated memory */
38
39 /* This memory region is executable */
40 #define PAN_BO_EXECUTE (1 << 0)
41
42 /* This memory region should be lazily allocated and grow-on-page-fault. Must
43 * be used in conjunction with INVISIBLE */
44 #define PAN_BO_GROWABLE (1 << 1)
45
46 /* This memory region should not be mapped to the CPU */
47 #define PAN_BO_INVISIBLE (1 << 2)
48
49 /* This region may not be used immediately and will not mmap on allocate
50 * (semantically distinct from INVISIBLE, which cannot never be mmaped) */
51 #define PAN_BO_DELAY_MMAP (1 << 3)
52
53 /* BO is shared across processes (imported or exported) and therefore cannot be
54 * cached locally */
55 #define PAN_BO_SHARED (1 << 4)
56
57 /* BO might be exported at some point. PAN_BO_SHAREABLE does not imply
58 * PAN_BO_SHARED if the BO has not been exported yet */
59 #define PAN_BO_SHAREABLE (1 << 5)
60
61 /* GPU access flags */
62
63 /* BO is either shared (can be accessed by more than one GPU batch) or private
64 * (reserved by a specific GPU job). */
65 #define PAN_BO_ACCESS_PRIVATE (0 << 0)
66 #define PAN_BO_ACCESS_SHARED (1 << 0)
67
68 /* BO is being read/written by the GPU */
69 #define PAN_BO_ACCESS_READ (1 << 1)
70 #define PAN_BO_ACCESS_WRITE (1 << 2)
71 #define PAN_BO_ACCESS_RW (PAN_BO_ACCESS_READ | PAN_BO_ACCESS_WRITE)
72
73 /* BO is accessed by the vertex/tiler job. */
74 #define PAN_BO_ACCESS_VERTEX_TILER (1 << 3)
75
76 /* BO is accessed by the fragment job. */
77 #define PAN_BO_ACCESS_FRAGMENT (1 << 4)
78
79 typedef uint8_t pan_bo_access;
80
81 struct panfrost_device;
82
83 struct panfrost_bo {
84 /* Must be first for casting */
85 struct list_head bucket_link;
86
87 /* Used to link the BO to the BO cache LRU list. */
88 struct list_head lru_link;
89
90 /* Store the time this BO was use last, so the BO cache logic can evict
91 * stale BOs.
92 */
93 time_t last_used;
94
95 /* Atomic reference count */
96 int32_t refcnt;
97
98 /* Kernel representation of a buffer object. */
99 struct pan_kmod_bo *kmod_bo;
100
101 struct panfrost_device *dev;
102
103 /* Mapping for the entire object (all levels) */
104 struct panfrost_ptr ptr;
105
106 uint32_t flags;
107
108 /* Combination of PAN_BO_ACCESS_{READ,WRITE} flags encoding pending
109 * GPU accesses to this BO. Useful to avoid calling the WAIT_BO ioctl
110 * when the BO is idle.
111 */
112 uint32_t gpu_access;
113
114 /* Human readable description of the BO for debugging. */
115 const char *label;
116 };
117
118 static inline size_t
panfrost_bo_size(struct panfrost_bo * bo)119 panfrost_bo_size(struct panfrost_bo *bo)
120 {
121 return bo->kmod_bo->size;
122 }
123
124 static inline size_t
panfrost_bo_handle(struct panfrost_bo * bo)125 panfrost_bo_handle(struct panfrost_bo *bo)
126 {
127 return bo->kmod_bo->handle;
128 }
129
130 struct panfrost_bo *panfrost_bo_from_kmod_bo(struct panfrost_device *dev,
131 struct pan_kmod_bo *kmod_bo);
132
133 bool panfrost_bo_wait(struct panfrost_bo *bo, int64_t timeout_ns,
134 bool wait_readers);
135 void panfrost_bo_reference(struct panfrost_bo *bo);
136 void panfrost_bo_unreference(struct panfrost_bo *bo);
137 struct panfrost_bo *panfrost_bo_create(struct panfrost_device *dev, size_t size,
138 uint32_t flags, const char *label);
139 void panfrost_bo_mmap(struct panfrost_bo *bo);
140 struct panfrost_bo *panfrost_bo_import(struct panfrost_device *dev, int fd);
141 int panfrost_bo_export(struct panfrost_bo *bo);
142 void panfrost_bo_cache_evict_all(struct panfrost_device *dev);
143
144 #endif /* __PAN_BO_H__ */
145