| 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 |
#include <assert.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdint.h> |
| 27 |
#include <vulkan/vulkan.h> |
| 28 |
#include <xf86drm.h> |
| 29 |
|
| 30 |
#include "pco/pco_data.h" |
| 31 |
#include "pco_uscgen_programs.h" |
| 32 |
#include "pvr_pass.h" |
| 33 |
#include "pvr_pds.h" |
| 34 |
#include "pvr_types.h" |
| 35 |
#include "pvr_winsys.h" |
| 36 |
#include "pvr_winsys_helper.h" |
| 37 |
#include "pvr_ycbcr.h" |
| 38 |
#include "util/u_atomic.h" |
| 39 |
#include "vk_log.h" |
| 40 |
|
| 41 |
VkResult pvr_winsys_helper_display_buffer_create(struct pvr_winsys *const ws, |
| 42 |
uint64_t size, |
| 43 |
uint32_t *const handle_out) |
| 44 |
{ |
| 45 |
struct drm_mode_create_dumb args = { |
| 46 |
.width = size, |
| 47 |
.height = 1, |
| 48 |
.bpp = 8, |
| 49 |
}; |
| 50 |
VkResult result; |
| 51 |
|
| 52 |
result = pvr_ioctl(ws->display_fd, |
| 53 |
DRM_IOCTL_MODE_CREATE_DUMB, |
| 54 |
&args, |
| 55 |
VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 56 |
if (result != VK_SUCCESS) |
| 57 |
return result; |
| 58 |
|
| 59 |
*handle_out = args.handle; |
| 60 |
|
| 61 |
return VK_SUCCESS; |
| 62 |
} |
| 63 |
|
| 64 |
VkResult pvr_winsys_helper_display_buffer_destroy(struct pvr_winsys *ws, |
| 65 |
uint32_t handle) |
| 66 |
{ |
| 67 |
struct drm_mode_destroy_dumb args = { |
| 68 |
.handle = handle, |
| 69 |
}; |
| 70 |
|
| 71 |
return pvr_ioctl(ws->display_fd, |
| 72 |
DRM_IOCTL_MODE_DESTROY_DUMB, |
| 73 |
&args, |
| 74 |
VK_ERROR_UNKNOWN); |
| 75 |
} |
| 76 |
|
| 77 |
bool pvr_winsys_helper_winsys_heap_finish(struct pvr_winsys_heap *const heap) |
| 78 |
{ |
| 79 |
if (p_atomic_read(&heap->ref_count) != 0) |
| 80 |
return false; |
| 81 |
|
| 82 |
pthread_mutex_destroy(&heap->lock); |
| 83 |
util_vma_heap_finish(&heap->vma_heap); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
VkResult pvr_winsys_helper_heap_alloc(struct pvr_winsys_heap *const heap, |
| 89 |
uint64_t size, |
| 90 |
uint64_t alignment, |
| 91 |
struct pvr_winsys_vma *const vma_out) |
| 92 |
{ |
| 93 |
struct pvr_winsys_vma vma = { |
| 94 |
.heap = heap, |
| 95 |
}; |
| 96 |
|
| 97 |
assert(util_is_power_of_two_nonzero64(alignment)); |
| 98 |
|
| 99 |
/* pvr_srv_winsys_buffer_create() page aligns the size. We must do the same |
| 100 |
* here to ensure enough heap space is allocated to be able to map the |
| 101 |
* buffer to the GPU. |
| 102 |
* We have to do this for the powervr kernel mode driver as well, as it |
| 103 |
* returns a page aligned size when allocating buffers. |
| 104 |
*/ |
| 105 |
alignment = MAX2(alignment, heap->page_size); |
| 106 |
|
| 107 |
size = ALIGN_POT(size, alignment); |
| 108 |
vma.size = size; |
| 109 |
|
| 110 |
pthread_mutex_lock(&heap->lock); |
| 111 |
vma.dev_addr = |
| 112 |
PVR_DEV_ADDR(util_vma_heap_alloc(&heap->vma_heap, size, heap->page_size)); |
| 113 |
pthread_mutex_unlock(&heap->lock); |
| 114 |
|
| 115 |
if (!vma.dev_addr.addr) |
| 116 |
return vk_error(NULL, VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 117 |
|
| 118 |
p_atomic_inc(&heap->ref_count); |
| 119 |
|
| 120 |
*vma_out = vma; |
| 121 |
|
| 122 |
return VK_SUCCESS; |
| 123 |
} |
| 124 |
|
| 125 |
void pvr_winsys_helper_heap_free(struct pvr_winsys_vma *const vma) |
| 126 |
{ |
| 127 |
struct pvr_winsys_heap *const heap = vma->heap; |
| 128 |
|
| 129 |
/* A vma with an existing device mapping should not be freed. */ |
| 130 |
assert(!vma->bo); |
| 131 |
|
| 132 |
pthread_mutex_lock(&heap->lock); |
| 133 |
util_vma_heap_free(&heap->vma_heap, vma->dev_addr.addr, vma->size); |
| 134 |
pthread_mutex_unlock(&heap->lock); |
| 135 |
|
| 136 |
p_atomic_dec(&heap->ref_count); |
| 137 |
} |
| 138 |
|
| 139 |
/* Note: the function assumes the heap allocation in the carveout memory area |
| 140 |
* can be freed with the regular heap allocation free function. The free |
| 141 |
* function gets called on mapping failure. |
| 142 |
*/ |
| 143 |
static VkResult |
| 144 |
pvr_buffer_create_and_map(struct pvr_winsys *const ws, |
| 145 |
heap_alloc_carveout_func heap_alloc_carveout, |
| 146 |
struct pvr_winsys_heap *heap, |
| 147 |
pvr_dev_addr_t dev_addr, |
| 148 |
uint64_t size, |
| 149 |
uint64_t alignment, |
| 150 |
struct pvr_winsys_vma **const vma_out) |
| 151 |
{ |
| 152 |
struct pvr_winsys_vma *vma; |
| 153 |
struct pvr_winsys_bo *bo; |
| 154 |
VkResult result; |
| 155 |
|
| 156 |
/* Address should not be NULL, this function is used to allocate and map |
| 157 |
* carveout addresses and is only supposed to be used internally. |
| 158 |
*/ |
| 159 |
assert(dev_addr.addr); |
| 160 |
|
| 161 |
result = ws->ops->buffer_create(ws, |
| 162 |
size, |
| 163 |
alignment, |
| 164 |
PVR_WINSYS_BO_TYPE_GPU, |
| 165 |
PVR_WINSYS_BO_FLAG_CPU_ACCESS, |
| 166 |
&bo); |
| 167 |
if (result != VK_SUCCESS) |
| 168 |
goto err_out; |
| 169 |
|
| 170 |
result = heap_alloc_carveout(heap, dev_addr, size, alignment, &vma); |
| 171 |
if (result != VK_SUCCESS) |
| 172 |
goto err_pvr_winsys_buffer_destroy; |
| 173 |
|
| 174 |
result = ws->ops->vma_map(vma, bo, 0, size, NULL); |
| 175 |
if (result != VK_SUCCESS) |
| 176 |
goto err_pvr_winsys_heap_free; |
| 177 |
|
| 178 |
/* Note this won't destroy bo as its being used by VMA, once vma is |
| 179 |
* unmapped, bo will be destroyed automatically. |
| 180 |
*/ |
| 181 |
ws->ops->buffer_destroy(bo); |
| 182 |
|
| 183 |
*vma_out = vma; |
| 184 |
|
| 185 |
return VK_SUCCESS; |
| 186 |
|
| 187 |
err_pvr_winsys_heap_free: |
| 188 |
ws->ops->heap_free(vma); |
| 189 |
|
| 190 |
err_pvr_winsys_buffer_destroy: |
| 191 |
ws->ops->buffer_destroy(bo); |
| 192 |
|
| 193 |
err_out: |
| 194 |
return result; |
| 195 |
} |
| 196 |
|
| 197 |
static void inline pvr_buffer_destroy_and_unmap(struct pvr_winsys_vma *vma) |
| 198 |
{ |
| 199 |
const struct pvr_winsys *const ws = vma->heap->ws; |
| 200 |
|
| 201 |
/* Buffer object associated with the vma will be automatically destroyed |
| 202 |
* once vma is unmapped. |
| 203 |
*/ |
| 204 |
ws->ops->vma_unmap(vma); |
| 205 |
ws->ops->heap_free(vma); |
| 206 |
} |
| 207 |
|
| 208 |
VkResult pvr_winsys_helper_allocate_static_memory( |
| 209 |
struct pvr_winsys *const ws, |
| 210 |
heap_alloc_carveout_func heap_alloc_carveout, |
| 211 |
struct pvr_winsys_heap *const general_heap, |
| 212 |
struct pvr_winsys_heap *const pds_heap, |
| 213 |
struct pvr_winsys_heap *const usc_heap, |
| 214 |
struct pvr_winsys_vma **const general_vma_out, |
| 215 |
struct pvr_winsys_vma **const pds_vma_out, |
| 216 |
struct pvr_winsys_vma **const usc_vma_out) |
| 217 |
{ |
| 218 |
struct pvr_winsys_vma *general_vma; |
| 219 |
struct pvr_winsys_vma *pds_vma; |
| 220 |
struct pvr_winsys_vma *usc_vma; |
| 221 |
VkResult result; |
| 222 |
|
| 223 |
result = pvr_buffer_create_and_map(ws, |
| 224 |
heap_alloc_carveout, |
| 225 |
general_heap, |
| 226 |
general_heap->static_data_carveout_addr, |
| 227 |
general_heap->static_data_carveout_size, |
| 228 |
general_heap->page_size, |
| 229 |
&general_vma); |
| 230 |
if (result != VK_SUCCESS) |
| 231 |
goto err_out; |
| 232 |
|
| 233 |
result = pvr_buffer_create_and_map(ws, |
| 234 |
heap_alloc_carveout, |
| 235 |
pds_heap, |
| 236 |
pds_heap->static_data_carveout_addr, |
| 237 |
pds_heap->static_data_carveout_size, |
| 238 |
pds_heap->page_size, |
| 239 |
&pds_vma); |
| 240 |
if (result != VK_SUCCESS) |
| 241 |
goto err_pvr_buffer_destroy_and_unmap_general; |
| 242 |
|
| 243 |
result = pvr_buffer_create_and_map(ws, |
| 244 |
heap_alloc_carveout, |
| 245 |
usc_heap, |
| 246 |
usc_heap->static_data_carveout_addr, |
| 247 |
pds_heap->static_data_carveout_size, |
| 248 |
usc_heap->page_size, |
| 249 |
&usc_vma); |
| 250 |
if (result != VK_SUCCESS) |
| 251 |
goto err_pvr_buffer_destroy_and_unmap_pds; |
| 252 |
|
| 253 |
*general_vma_out = general_vma; |
| 254 |
*pds_vma_out = pds_vma; |
| 255 |
*usc_vma_out = usc_vma; |
| 256 |
|
| 257 |
return VK_SUCCESS; |
| 258 |
|
| 259 |
err_pvr_buffer_destroy_and_unmap_pds: |
| 260 |
pvr_buffer_destroy_and_unmap(pds_vma); |
| 261 |
|
| 262 |
err_pvr_buffer_destroy_and_unmap_general: |
| 263 |
pvr_buffer_destroy_and_unmap(general_vma); |
| 264 |
|
| 265 |
err_out: |
| 266 |
return result; |
| 267 |
} |
| 268 |
|
| 269 |
void pvr_winsys_helper_free_static_memory( |
| 270 |
struct pvr_winsys_vma *const general_vma, |
| 271 |
struct pvr_winsys_vma *const pds_vma, |
| 272 |
struct pvr_winsys_vma *const usc_vma) |
| 273 |
{ |
| 274 |
pvr_buffer_destroy_and_unmap(usc_vma); |
| 275 |
pvr_buffer_destroy_and_unmap(pds_vma); |
| 276 |
pvr_buffer_destroy_and_unmap(general_vma); |
| 277 |
} |
| 278 |
|
| 279 |
static uint32_t get_doutu_sample_rate(void); |
| 280 |
|
| 281 |
static void pvr_setup_static_vdm_sync(uint8_t *const pds_ptr, |
| 282 |
uint64_t pds_sync_offset_in_bytes, |
| 283 |
uint8_t *const usc_ptr, |
| 284 |
uint64_t usc_sync_offset_in_bytes) |
| 285 |
{ |
| 286 |
struct pvr_pds_kickusc_program ppp_state_update_program = { 0 }; |
| 287 |
const pco_precomp_data *precomp_data = |
| 288 |
(pco_precomp_data *)pco_usclib_common[VS_NOP_COMMON]; |
| 289 |
|
| 290 |
memcpy(usc_ptr + usc_sync_offset_in_bytes, |
| 291 |
precomp_data->binary, |
| 292 |
precomp_data->size_dwords * sizeof(uint32_t)); |
| 293 |
|
| 294 |
pvr_pds_setup_doutu(&ppp_state_update_program.usc_task_control, |
| 295 |
usc_sync_offset_in_bytes, |
| 296 |
precomp_data->temps, |
| 297 |
get_doutu_sample_rate(), |
| 298 |
false); |
| 299 |
|
| 300 |
pvr_pds_kick_usc(&ppp_state_update_program, |
| 301 |
(uint32_t *)&pds_ptr[pds_sync_offset_in_bytes], |
| 302 |
0, |
| 303 |
false, |
| 304 |
PDS_GENERATE_CODEDATA_SEGMENTS); |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
pvr_setup_static_pixel_event_program(uint8_t *const pds_ptr, |
| 309 |
uint64_t pds_eot_offset_in_bytes) |
| 310 |
{ |
| 311 |
struct pvr_pds_event_program pixel_event_program = { 0 }; |
| 312 |
|
| 313 |
pvr_pds_generate_pixel_event(&pixel_event_program, |
| 314 |
(uint32_t *)&pds_ptr[pds_eot_offset_in_bytes], |
| 315 |
PDS_GENERATE_CODE_SEGMENT, |
| 316 |
NULL); |
| 317 |
} |
| 318 |
|
| 319 |
VkResult |
| 320 |
pvr_winsys_helper_fill_static_memory(struct pvr_winsys *const ws, |
| 321 |
struct pvr_winsys_vma *const general_vma, |
| 322 |
struct pvr_winsys_vma *const pds_vma, |
| 323 |
struct pvr_winsys_vma *const usc_vma) |
| 324 |
{ |
| 325 |
VkResult result; |
| 326 |
|
| 327 |
result = ws->ops->buffer_map(general_vma->bo, NULL); |
| 328 |
if (result != VK_SUCCESS) |
| 329 |
goto err_out; |
| 330 |
|
| 331 |
result = ws->ops->buffer_map(pds_vma->bo, NULL); |
| 332 |
if (result != VK_SUCCESS) |
| 333 |
goto err_pvr_srv_winsys_buffer_unmap_general; |
| 334 |
|
| 335 |
result = ws->ops->buffer_map(usc_vma->bo, NULL); |
| 336 |
if (result != VK_SUCCESS) |
| 337 |
goto err_pvr_srv_winsys_buffer_unmap_pds; |
| 338 |
|
| 339 |
pvr_setup_static_vdm_sync(pds_vma->bo->map, |
| 340 |
pds_vma->heap->static_data_offsets.vdm_sync, |
| 341 |
usc_vma->bo->map, |
| 342 |
usc_vma->heap->static_data_offsets.vdm_sync); |
| 343 |
|
| 344 |
pvr_setup_static_pixel_event_program(pds_vma->bo->map, |
| 345 |
pds_vma->heap->static_data_offsets.eot); |
| 346 |
|
| 347 |
pvr_setup_static_yuv_csc_table( |
| 348 |
general_vma->bo->map, |
| 349 |
general_vma->heap->static_data_offsets.yuv_csc); |
| 350 |
|
| 351 |
ws->ops->buffer_unmap(usc_vma->bo, false); |
| 352 |
ws->ops->buffer_unmap(pds_vma->bo, false); |
| 353 |
ws->ops->buffer_unmap(general_vma->bo, false); |
| 354 |
|
| 355 |
return VK_SUCCESS; |
| 356 |
|
| 357 |
err_pvr_srv_winsys_buffer_unmap_pds: |
| 358 |
ws->ops->buffer_unmap(pds_vma->bo, false); |
| 359 |
|
| 360 |
err_pvr_srv_winsys_buffer_unmap_general: |
| 361 |
ws->ops->buffer_unmap(general_vma->bo, false); |
| 362 |
|
| 363 |
err_out: |
| 364 |
return result; |
| 365 |
} |
| 366 |
|
| 367 |
/* Leave this at the very end, to avoid leakage of HW-defs here */ |
| 368 |
#define PVR_BUILD_ARCH_ROGUE |
| 369 |
#include "pvr_csb.h" |
| 370 |
|
| 371 |
static uint32_t get_doutu_sample_rate(void) |
| 372 |
{ |
| 373 |
return ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE; |
| 374 |
} |