| 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 |
|
| 85 |
static inline VkResult pvr_mmap(void *addr, |
| 86 |
const size_t len, |
| 87 |
const int prot, |
| 88 |
int flags, |
| 89 |
const int fd, |
| 90 |
const off_t offset, |
| 91 |
void **const map_out) |
| 92 |
{ |
| 93 |
if (addr) |
| 94 |
flags |= MAP_FIXED; |
| 95 |
|
| 96 |
void *const map = mmap(addr, len, prot, flags, fd, offset); |
| 97 |
if (map == MAP_FAILED) { |
| 98 |
const int err = errno; |
| 99 |
return vk_errorf(NULL, |
| 100 |
VK_ERROR_MEMORY_MAP_FAILED, |
| 101 |
"mmap failed (errno %d: %s)", |
| 102 |
err, |
| 103 |
strerror(err)); |
| 104 |
} |
| 105 |
|
| 106 |
*map_out = map; |
| 107 |
|
| 108 |
return VK_SUCCESS; |
| 109 |
} |
| 110 |
|
| 111 |
static inline VkResult |
| 112 |
pvr_munmap(void *const addr, const size_t len, bool reserve) |
| 113 |
{ |
| 114 |
if (reserve) { |
| 115 |
void *ret = mmap(addr, |
| 116 |
len, |
| 117 |
PROT_NONE, |
| 118 |
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, |
| 119 |
-1, |
| 120 |
0); |
| 121 |
|
| 122 |
if (ret == MAP_FAILED) { |
| 123 |
const int err = errno; |
| 124 |
return vk_errorf(NULL, |
| 125 |
VK_ERROR_UNKNOWN, |
| 126 |
"mmap(reserve) failed (errno %d: %s)", |
| 127 |
err, |
| 128 |
strerror(err)); |
| 129 |
} |
| 130 |
|
| 131 |
return VK_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
const int ret = munmap(addr, len); |
| 135 |
if (ret) { |
| 136 |
const int err = errno; |
| 137 |
return vk_errorf(NULL, |
| 138 |
VK_ERROR_UNKNOWN, |
| 139 |
"munmap failed (errno %d: %s)", |
| 140 |
err, |
| 141 |
strerror(err)); |
| 142 |
} |
| 143 |
|
| 144 |
return VK_SUCCESS; |
| 145 |
} |
| 146 |
|
| 147 |
#define pvr_ioctlf(fd, request, arg, error, fmt, args...) \ |
| 148 |
({ \ |
| 149 |
VkResult _result = VK_SUCCESS; \ |
| 150 |
int _ret; \ |
| 151 |
\ |
| 152 |
_ret = drmIoctl(fd, request, arg); \ |
| 153 |
if (_ret) { \ |
| 154 |
_ret = errno; \ |
| 155 |
_result = vk_errorf(NULL, \ |
| 156 |
error, \ |
| 157 |
fmt " (errno %d: %s)", \ |
| 158 |
##args, \ |
| 159 |
_ret, \ |
| 160 |
strerror(_ret)); \ |
| 161 |
} \ |
| 162 |
\ |
| 163 |
_result; \ |
| 164 |
}) |
| 165 |
|
| 166 |
#define pvr_ioctl(fd, request, arg, error) \ |
| 167 |
pvr_ioctlf(fd, request, arg, error, "ioctl " #request " failed") |
| 168 |
|
| 169 |
#endif /* PVR_WINSYS_HELPER_H */ |