1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Imagination Technologies Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * based in part on anv driver which is: 5bf215546Sopenharmony_ci * Copyright © 2015 Intel Corporation 6bf215546Sopenharmony_ci * 7bf215546Sopenharmony_ci * based in part on v3dv_cl.c which is: 8bf215546Sopenharmony_ci * Copyright © 2019 Raspberry Pi 9bf215546Sopenharmony_ci * 10bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 11bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 12bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights 13bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 15bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions: 16bf215546Sopenharmony_ci * 17bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 18bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 19bf215546Sopenharmony_ci * Software. 20bf215546Sopenharmony_ci * 21bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 24bf215546Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 25bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 27bf215546Sopenharmony_ci * SOFTWARE. 28bf215546Sopenharmony_ci */ 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include <assert.h> 31bf215546Sopenharmony_ci#include <stdbool.h> 32bf215546Sopenharmony_ci#include <stdint.h> 33bf215546Sopenharmony_ci#include <vulkan/vulkan.h> 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci#include "hwdef/rogue_hw_utils.h" 36bf215546Sopenharmony_ci#include "pvr_bo.h" 37bf215546Sopenharmony_ci#include "pvr_csb.h" 38bf215546Sopenharmony_ci#include "pvr_device_info.h" 39bf215546Sopenharmony_ci#include "pvr_private.h" 40bf215546Sopenharmony_ci#include "vk_log.h" 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci/** 43bf215546Sopenharmony_ci * \file pvr_csb.c 44bf215546Sopenharmony_ci * 45bf215546Sopenharmony_ci * \brief Contains functions to manage Control Stream Builder (csb) object. 46bf215546Sopenharmony_ci * 47bf215546Sopenharmony_ci * A csb object can be used to create a primary/main control stream, referred 48bf215546Sopenharmony_ci * as control stream hereafter, or a secondary control stream, also referred as 49bf215546Sopenharmony_ci * a sub control stream. The main difference between these is that, the control 50bf215546Sopenharmony_ci * stream is the one directly submitted to the GPU and is terminated using 51bf215546Sopenharmony_ci * STREAM_TERMINATE. Whereas, the secondary control stream can be thought of as 52bf215546Sopenharmony_ci * an independent set of commands that can be referenced by a primary control 53bf215546Sopenharmony_ci * stream to avoid duplication and is instead terminated using STREAM_RETURN, 54bf215546Sopenharmony_ci * which means the control stream parser should return to the main stream it 55bf215546Sopenharmony_ci * came from. 56bf215546Sopenharmony_ci * 57bf215546Sopenharmony_ci * Note: Sub control stream is only supported for PVR_CMD_STREAM_TYPE_GRAPHICS 58bf215546Sopenharmony_ci * type control streams. 59bf215546Sopenharmony_ci */ 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci/** 62bf215546Sopenharmony_ci * \brief Size of the individual csb buffer object. 63bf215546Sopenharmony_ci */ 64bf215546Sopenharmony_ci#define PVR_CMD_BUFFER_CSB_BO_SIZE 4096 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci/** 67bf215546Sopenharmony_ci * \brief Initializes the csb object. 68bf215546Sopenharmony_ci * 69bf215546Sopenharmony_ci * \param[in] device Logical device pointer. 70bf215546Sopenharmony_ci * \param[in] csb Control Stream Builder object to initialize. 71bf215546Sopenharmony_ci * 72bf215546Sopenharmony_ci * \sa #pvr_csb_finish() 73bf215546Sopenharmony_ci */ 74bf215546Sopenharmony_civoid pvr_csb_init(struct pvr_device *device, 75bf215546Sopenharmony_ci enum pvr_cmd_stream_type stream_type, 76bf215546Sopenharmony_ci struct pvr_csb *csb) 77bf215546Sopenharmony_ci{ 78bf215546Sopenharmony_ci csb->start = NULL; 79bf215546Sopenharmony_ci csb->next = NULL; 80bf215546Sopenharmony_ci csb->pvr_bo = NULL; 81bf215546Sopenharmony_ci csb->end = NULL; 82bf215546Sopenharmony_ci csb->device = device; 83bf215546Sopenharmony_ci csb->stream_type = stream_type; 84bf215546Sopenharmony_ci csb->status = VK_SUCCESS; 85bf215546Sopenharmony_ci list_inithead(&csb->pvr_bo_list); 86bf215546Sopenharmony_ci} 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci/** 89bf215546Sopenharmony_ci * \brief Frees the resources associated with the csb object. 90bf215546Sopenharmony_ci * 91bf215546Sopenharmony_ci * \param[in] csb Control Stream Builder object to free. 92bf215546Sopenharmony_ci * 93bf215546Sopenharmony_ci * \sa #pvr_csb_init() 94bf215546Sopenharmony_ci */ 95bf215546Sopenharmony_civoid pvr_csb_finish(struct pvr_csb *csb) 96bf215546Sopenharmony_ci{ 97bf215546Sopenharmony_ci list_for_each_entry_safe (struct pvr_bo, pvr_bo, &csb->pvr_bo_list, link) { 98bf215546Sopenharmony_ci list_del(&pvr_bo->link); 99bf215546Sopenharmony_ci pvr_bo_free(csb->device, pvr_bo); 100bf215546Sopenharmony_ci } 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci /* Leave the csb in a reset state to catch use after destroy instances */ 103bf215546Sopenharmony_ci pvr_csb_init(NULL, PVR_CMD_STREAM_TYPE_INVALID, csb); 104bf215546Sopenharmony_ci} 105bf215546Sopenharmony_ci 106bf215546Sopenharmony_ci/** 107bf215546Sopenharmony_ci * \brief Helper function to extend csb memory. 108bf215546Sopenharmony_ci * 109bf215546Sopenharmony_ci * Allocates a new buffer object and links it with the previous buffer object 110bf215546Sopenharmony_ci * using STREAM_LINK dwords and updates csb object to use the new buffer. 111bf215546Sopenharmony_ci * 112bf215546Sopenharmony_ci * To make sure that we have enough space to emit STREAM_LINK dwords in the 113bf215546Sopenharmony_ci * current buffer, a few bytes are reserved at the end, every time a buffer is 114bf215546Sopenharmony_ci * created. Every time we allocate a new buffer we fix the current buffer in use 115bf215546Sopenharmony_ci * to emit the stream link dwords. This makes sure that when 116bf215546Sopenharmony_ci * #pvr_csb_alloc_dwords() is called from #pvr_csb_emit() to add STREAM_LINK0 117bf215546Sopenharmony_ci * and STREAM_LINK1, it succeeds without trying to allocate new pages. 118bf215546Sopenharmony_ci * 119bf215546Sopenharmony_ci * \param[in] csb Control Stream Builder object to extend. 120bf215546Sopenharmony_ci * \return true on success and false otherwise. 121bf215546Sopenharmony_ci */ 122bf215546Sopenharmony_cistatic bool pvr_csb_buffer_extend(struct pvr_csb *csb) 123bf215546Sopenharmony_ci{ 124bf215546Sopenharmony_ci const uint8_t stream_link_space = (pvr_cmd_length(VDMCTRL_STREAM_LINK0) + 125bf215546Sopenharmony_ci pvr_cmd_length(VDMCTRL_STREAM_LINK1)) * 126bf215546Sopenharmony_ci 4; 127bf215546Sopenharmony_ci const uint32_t cache_line_size = 128bf215546Sopenharmony_ci rogue_get_slc_cache_line_size(&csb->device->pdevice->dev_info); 129bf215546Sopenharmony_ci struct pvr_bo *pvr_bo; 130bf215546Sopenharmony_ci VkResult result; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci /* Make sure extra space allocated for stream links is sufficient for both 133bf215546Sopenharmony_ci * stream types. 134bf215546Sopenharmony_ci */ 135bf215546Sopenharmony_ci STATIC_ASSERT((pvr_cmd_length(VDMCTRL_STREAM_LINK0) + 136bf215546Sopenharmony_ci pvr_cmd_length(VDMCTRL_STREAM_LINK1)) == 137bf215546Sopenharmony_ci (pvr_cmd_length(CDMCTRL_STREAM_LINK0) + 138bf215546Sopenharmony_ci pvr_cmd_length(CDMCTRL_STREAM_LINK1))); 139bf215546Sopenharmony_ci 140bf215546Sopenharmony_ci result = pvr_bo_alloc(csb->device, 141bf215546Sopenharmony_ci csb->device->heaps.general_heap, 142bf215546Sopenharmony_ci PVR_CMD_BUFFER_CSB_BO_SIZE, 143bf215546Sopenharmony_ci cache_line_size, 144bf215546Sopenharmony_ci PVR_BO_ALLOC_FLAG_CPU_MAPPED, 145bf215546Sopenharmony_ci &pvr_bo); 146bf215546Sopenharmony_ci if (result != VK_SUCCESS) { 147bf215546Sopenharmony_ci vk_error(csb->device, result); 148bf215546Sopenharmony_ci csb->status = result; 149bf215546Sopenharmony_ci return false; 150bf215546Sopenharmony_ci } 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci /* Chain to the old BO if this is not the first BO in csb */ 153bf215546Sopenharmony_ci if (csb->pvr_bo) { 154bf215546Sopenharmony_ci csb->end += stream_link_space; 155bf215546Sopenharmony_ci assert(csb->next + stream_link_space <= csb->end); 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci switch (csb->stream_type) { 158bf215546Sopenharmony_ci case PVR_CMD_STREAM_TYPE_GRAPHICS: 159bf215546Sopenharmony_ci pvr_csb_emit (csb, VDMCTRL_STREAM_LINK0, link) { 160bf215546Sopenharmony_ci link.link_addrmsb = pvr_bo->vma->dev_addr; 161bf215546Sopenharmony_ci } 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci pvr_csb_emit (csb, VDMCTRL_STREAM_LINK1, link) { 164bf215546Sopenharmony_ci link.link_addrlsb = pvr_bo->vma->dev_addr; 165bf215546Sopenharmony_ci } 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci break; 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_ci case PVR_CMD_STREAM_TYPE_COMPUTE: 170bf215546Sopenharmony_ci pvr_csb_emit (csb, CDMCTRL_STREAM_LINK0, link) { 171bf215546Sopenharmony_ci link.link_addrmsb = pvr_bo->vma->dev_addr; 172bf215546Sopenharmony_ci } 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci pvr_csb_emit (csb, CDMCTRL_STREAM_LINK1, link) { 175bf215546Sopenharmony_ci link.link_addrlsb = pvr_bo->vma->dev_addr; 176bf215546Sopenharmony_ci } 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ci break; 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ci default: 181bf215546Sopenharmony_ci unreachable("Unknown stream type"); 182bf215546Sopenharmony_ci break; 183bf215546Sopenharmony_ci } 184bf215546Sopenharmony_ci } 185bf215546Sopenharmony_ci 186bf215546Sopenharmony_ci csb->pvr_bo = pvr_bo; 187bf215546Sopenharmony_ci csb->start = pvr_bo->bo->map; 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci /* Reserve stream link size at the end to make sure we don't run out of 190bf215546Sopenharmony_ci * space when a stream link is required. 191bf215546Sopenharmony_ci */ 192bf215546Sopenharmony_ci csb->end = csb->start + pvr_bo->bo->size - stream_link_space; 193bf215546Sopenharmony_ci csb->next = csb->start; 194bf215546Sopenharmony_ci 195bf215546Sopenharmony_ci list_addtail(&pvr_bo->link, &csb->pvr_bo_list); 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci return true; 198bf215546Sopenharmony_ci} 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci/** 201bf215546Sopenharmony_ci * \brief Provides a chunk of memory from the current csb buffer. In cases where 202bf215546Sopenharmony_ci * the buffer is not able to fulfill the required amount of memory, 203bf215546Sopenharmony_ci * #pvr_csb_buffer_extend() is called to allocate a new buffer. Maximum size 204bf215546Sopenharmony_ci * allocable in bytes is #PVR_CMD_BUFFER_CSB_BO_SIZE - size of STREAM_LINK0 205bf215546Sopenharmony_ci * and STREAM_LINK1 dwords. 206bf215546Sopenharmony_ci * 207bf215546Sopenharmony_ci * \param[in] csb Control Stream Builder object to allocate from. 208bf215546Sopenharmony_ci * \param[in] num_dwords Number of dwords to allocate. 209bf215546Sopenharmony_ci * \return Valid host virtual address or NULL otherwise. 210bf215546Sopenharmony_ci */ 211bf215546Sopenharmony_civoid *pvr_csb_alloc_dwords(struct pvr_csb *csb, uint32_t num_dwords) 212bf215546Sopenharmony_ci{ 213bf215546Sopenharmony_ci const uint32_t required_space = num_dwords * 4; 214bf215546Sopenharmony_ci 215bf215546Sopenharmony_ci if (csb->status != VK_SUCCESS) 216bf215546Sopenharmony_ci return NULL; 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ci if (csb->next + required_space > csb->end) { 219bf215546Sopenharmony_ci bool ret = pvr_csb_buffer_extend(csb); 220bf215546Sopenharmony_ci if (!ret) 221bf215546Sopenharmony_ci return NULL; 222bf215546Sopenharmony_ci } 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci void *p = csb->next; 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci csb->next += required_space; 227bf215546Sopenharmony_ci assert(csb->next <= csb->end); 228bf215546Sopenharmony_ci 229bf215546Sopenharmony_ci return p; 230bf215546Sopenharmony_ci} 231bf215546Sopenharmony_ci 232bf215546Sopenharmony_ci/** 233bf215546Sopenharmony_ci * \brief Adds VDMCTRL_STREAM_RETURN dword into the control stream pointed by 234bf215546Sopenharmony_ci * csb object. Given a VDMCTRL_STREAM_RETURN marks the end of the sub control 235bf215546Sopenharmony_ci * stream, we return the status of the control stream as well. 236bf215546Sopenharmony_ci * 237bf215546Sopenharmony_ci * \param[in] csb Control Stream Builder object to add VDMCTRL_STREAM_RETURN to. 238bf215546Sopenharmony_ci * \return VK_SUCCESS on success, or error code otherwise. 239bf215546Sopenharmony_ci */ 240bf215546Sopenharmony_ciVkResult pvr_csb_emit_return(struct pvr_csb *csb) 241bf215546Sopenharmony_ci{ 242bf215546Sopenharmony_ci /* STREAM_RETURN is only supported by graphics control stream. */ 243bf215546Sopenharmony_ci assert(csb->stream_type == PVR_CMD_STREAM_TYPE_GRAPHICS); 244bf215546Sopenharmony_ci 245bf215546Sopenharmony_ci /* clang-format off */ 246bf215546Sopenharmony_ci pvr_csb_emit(csb, VDMCTRL_STREAM_RETURN, ret); 247bf215546Sopenharmony_ci /* clang-format on */ 248bf215546Sopenharmony_ci 249bf215546Sopenharmony_ci return csb->status; 250bf215546Sopenharmony_ci} 251bf215546Sopenharmony_ci 252bf215546Sopenharmony_ci/** 253bf215546Sopenharmony_ci * \brief Adds STREAM_TERMINATE dword into the control stream pointed by csb 254bf215546Sopenharmony_ci * object. Given a STREAM_TERMINATE marks the end of the control stream, we 255bf215546Sopenharmony_ci * return the status of the control stream as well. 256bf215546Sopenharmony_ci * 257bf215546Sopenharmony_ci * \param[in] csb Control Stream Builder object to terminate. 258bf215546Sopenharmony_ci * \return VK_SUCCESS on success, or error code otherwise. 259bf215546Sopenharmony_ci */ 260bf215546Sopenharmony_ciVkResult pvr_csb_emit_terminate(struct pvr_csb *csb) 261bf215546Sopenharmony_ci{ 262bf215546Sopenharmony_ci switch (csb->stream_type) { 263bf215546Sopenharmony_ci case PVR_CMD_STREAM_TYPE_GRAPHICS: 264bf215546Sopenharmony_ci /* clang-format off */ 265bf215546Sopenharmony_ci pvr_csb_emit(csb, VDMCTRL_STREAM_TERMINATE, terminate); 266bf215546Sopenharmony_ci /* clang-format on */ 267bf215546Sopenharmony_ci break; 268bf215546Sopenharmony_ci 269bf215546Sopenharmony_ci case PVR_CMD_STREAM_TYPE_COMPUTE: 270bf215546Sopenharmony_ci /* clang-format off */ 271bf215546Sopenharmony_ci pvr_csb_emit(csb, CDMCTRL_STREAM_TERMINATE, terminate); 272bf215546Sopenharmony_ci /* clang-format on */ 273bf215546Sopenharmony_ci break; 274bf215546Sopenharmony_ci 275bf215546Sopenharmony_ci default: 276bf215546Sopenharmony_ci unreachable("Unknown stream type"); 277bf215546Sopenharmony_ci break; 278bf215546Sopenharmony_ci } 279bf215546Sopenharmony_ci 280bf215546Sopenharmony_ci return csb->status; 281bf215546Sopenharmony_ci} 282