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
29#include "pvr_csb.h"
30#include "pvr_job_common.h"
31#include "pvr_job_context.h"
32#include "pvr_job_compute.h"
33#include "pvr_private.h"
34#include "pvr_winsys.h"
35#include "util/macros.h"
36
37static void pvr_compute_job_ws_submit_info_init(
38   struct pvr_compute_ctx *ctx,
39   struct pvr_sub_cmd_compute *sub_cmd,
40   struct vk_sync **waits,
41   uint32_t wait_count,
42   uint32_t *stage_flags,
43   struct pvr_winsys_compute_submit_info *submit_info)
44{
45   const struct pvr_compute_ctx_switch *const ctx_switch = &ctx->ctx_switch;
46   uint32_t shared_regs = sub_cmd->num_shared_regs;
47
48   submit_info->frame_num = ctx->device->global_queue_present_count;
49   submit_info->job_num = ctx->device->global_queue_job_count;
50
51   submit_info->waits = waits;
52   submit_info->wait_count = wait_count;
53   submit_info->stage_flags = stage_flags;
54
55   pvr_csb_pack (&submit_info->regs.cdm_ctx_state_base_addr,
56                 CR_CDM_CONTEXT_STATE_BASE,
57                 state) {
58      state.addr = ctx_switch->compute_state_bo->vma->dev_addr;
59   }
60
61   /* Other registers are initialized in pvr_sub_cmd_compute_job_init(). */
62   pvr_csb_pack (&submit_info->regs.cdm_resume_pds1,
63                 CR_CDM_CONTEXT_PDS1,
64                 state) {
65      /* Convert the data size from dwords to bytes. */
66      const uint32_t load_program_data_size =
67         ctx_switch->sr[0].pds.load_program.data_size * 4U;
68
69      state.pds_seq_dep = false;
70      state.usc_seq_dep = false;
71      state.target = false;
72      state.unified_size = ctx_switch->sr[0].usc.unified_size;
73      state.common_shared = true;
74      state.common_size =
75         DIV_ROUND_UP(shared_regs << 2,
76                      PVRX(CR_CDM_CONTEXT_PDS1_COMMON_SIZE_UNIT_SIZE));
77      state.temp_size = 0;
78
79      assert(load_program_data_size %
80                PVRX(CR_CDM_CONTEXT_PDS1_DATA_SIZE_UNIT_SIZE) ==
81             0);
82      state.data_size =
83         load_program_data_size / PVRX(CR_CDM_CONTEXT_PDS1_DATA_SIZE_UNIT_SIZE);
84      state.fence = false;
85   }
86}
87
88VkResult pvr_compute_job_submit(struct pvr_compute_ctx *ctx,
89                                struct pvr_sub_cmd_compute *sub_cmd,
90                                struct vk_sync **waits,
91                                uint32_t wait_count,
92                                uint32_t *stage_flags,
93                                struct vk_sync *signal_sync)
94{
95   struct pvr_device *device = ctx->device;
96
97   pvr_compute_job_ws_submit_info_init(ctx,
98                                       sub_cmd,
99                                       waits,
100                                       wait_count,
101                                       stage_flags,
102                                       &sub_cmd->submit_info);
103
104   return device->ws->ops->compute_submit(ctx->ws_ctx,
105                                          &sub_cmd->submit_info,
106                                          signal_sync);
107}
108