1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * 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 THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifndef PVR_WINSYS_HELPER_H
25 #define PVR_WINSYS_HELPER_H
26
27 #include <errno.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <sys/mman.h>
33 #include <sys/types.h>
34 #include <xf86drm.h>
35
36 #include "pvr_types.h"
37 #include "vk_log.h"
38
39 struct pvr_winsys;
40 struct pvr_winsys_heap;
41 struct pvr_winsys_static_data_offsets;
42 struct pvr_winsys_vma;
43
44 typedef VkResult (*const heap_alloc_carveout_func)(
45 struct pvr_winsys_heap *const heap,
46 const pvr_dev_addr_t carveout_dev_addr,
47 uint64_t size,
48 uint64_t alignment,
49 struct pvr_winsys_vma **vma_out);
50
51 VkResult pvr_winsys_helper_display_buffer_create(struct pvr_winsys *ws,
52 uint64_t size,
53 uint32_t *const handle_out);
54 VkResult pvr_winsys_helper_display_buffer_destroy(struct pvr_winsys *ws,
55 uint32_t handle);
56
57 bool pvr_winsys_helper_winsys_heap_finish(struct pvr_winsys_heap *const heap);
58
59 VkResult pvr_winsys_helper_heap_alloc(struct pvr_winsys_heap *const heap,
60 uint64_t size,
61 uint64_t alignment,
62 struct pvr_winsys_vma *const vma);
63 void pvr_winsys_helper_heap_free(struct pvr_winsys_vma *const vma);
64
65 VkResult pvr_winsys_helper_allocate_static_memory(
66 struct pvr_winsys *const ws,
67 heap_alloc_carveout_func heap_alloc_carveout,
68 struct pvr_winsys_heap *const general_heap,
69 struct pvr_winsys_heap *const pds_heap,
70 struct pvr_winsys_heap *const usc_heap,
71 struct pvr_winsys_vma **const general_vma_out,
72 struct pvr_winsys_vma **const pds_vma_out,
73 struct pvr_winsys_vma **const usc_vma_out);
74 void pvr_winsys_helper_free_static_memory(
75 struct pvr_winsys_vma *const general_vma,
76 struct pvr_winsys_vma *const pds_vma,
77 struct pvr_winsys_vma *const usc_vma);
78
79 VkResult
80 pvr_winsys_helper_fill_static_memory(struct pvr_winsys *const ws,
81 struct pvr_winsys_vma *const general_vma,
82 struct pvr_winsys_vma *const pds_vma,
83 struct pvr_winsys_vma *const usc_vma);
84
pvr_mmap(const size_t len,const int prot,const int flags,const int fd,const off_t offset,void ** const map_out)85 static inline VkResult pvr_mmap(const size_t len,
86 const int prot,
87 const int flags,
88 const int fd,
89 const off_t offset,
90 void **const map_out)
91 {
92 void *const map = mmap(NULL, len, prot, flags, fd, offset);
93 if (map == MAP_FAILED) {
94 const int err = errno;
95 return vk_errorf(NULL,
96 VK_ERROR_MEMORY_MAP_FAILED,
97 "mmap failed (errno %d: %s)",
98 err,
99 strerror(err));
100 }
101
102 *map_out = map;
103
104 return VK_SUCCESS;
105 }
106
pvr_munmap(void * const addr,const size_t len)107 static inline VkResult pvr_munmap(void *const addr, const size_t len)
108 {
109 const int ret = munmap(addr, len);
110 if (ret) {
111 const int err = errno;
112 return vk_errorf(NULL,
113 VK_ERROR_UNKNOWN,
114 "munmap failed (errno %d: %s)",
115 err,
116 strerror(err));
117 }
118
119 return VK_SUCCESS;
120 }
121
122 #define pvr_ioctlf(fd, request, arg, error, fmt, args...) \
123 ({ \
124 VkResult _result = VK_SUCCESS; \
125 int _ret; \
126 \
127 _ret = drmIoctl(fd, request, arg); \
128 if (_ret) { \
129 _ret = errno; \
130 _result = vk_errorf(NULL, \
131 error, \
132 fmt " (errno %d: %s)", \
133 ##args, \
134 _ret, \
135 strerror(_ret)); \
136 } \
137 \
138 _result; \
139 })
140
141 #define pvr_ioctl(fd, request, arg, error) \
142 pvr_ioctlf(fd, request, arg, error, "ioctl " #request " failed")
143
144 #endif /* PVR_WINSYS_HELPER_H */
145