File: amd_ip_blocks_ex.c

package info (click to toggle)
intel-gpu-tools 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 63,368 kB
  • sloc: xml: 781,458; ansic: 360,567; python: 8,336; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (253 lines) | stat: -rw-r--r-- 7,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: MIT
/*
 * Copyright 2025 Advanced Micro Devices, Inc.
 */

#include <stdint.h>
#include "ioctl_wrappers.h"
#include "amd_ip_blocks.h"
#include "amd_PM4.h"
#include "amdgpu_asic_addr.h"

static void gfx_program_compute_default(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint64_t code_addr,
	uint64_t user_data0_addr,
	uint32_t rsrc1_dw,
	uint32_t rsrc2_or_tmp,
	uint32_t thr_x, uint32_t thr_y, uint32_t thr_z)
{
	base->emit(base, PACKET3(PKT3_CONTEXT_CONTROL, 1));
	base->emit(base, 0x80000000);
	base->emit(base, 0x80000000);

	base->emit(base, PACKET3(PKT3_CLEAR_STATE, 0));
	base->emit(base, 0x80000000);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_PGM_LO));
	base->emit(base, (uint32_t)(code_addr >> 8));
	base->emit(base, (uint32_t)(code_addr >> 40));

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_PGM_RSRC1));
	base->emit(base, rsrc1_dw);
	base->emit(base, rsrc2_or_tmp);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 1));
	base->emit(base, f->get_reg_offset(COMPUTE_TMPRING_SIZE));
	base->emit(base, 0x00000100);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_USER_DATA_0));
	base->emit(base, (uint32_t)user_data0_addr);
	base->emit(base, (uint32_t)(user_data0_addr >> 32));

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 1));
	base->emit(base, f->get_reg_offset(COMPUTE_RESOURCE_LIMITS));
	base->emit(base, 0);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 3));
	base->emit(base, f->get_reg_offset(COMPUTE_NUM_THREAD_X));
	base->emit(base, thr_x);
	base->emit(base, thr_y);
	base->emit(base, thr_z);
}

static void gfx_dispatch_direct_default(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint32_t gx, uint32_t gy, uint32_t gz,
	uint32_t flags)
{
	base->emit(base, PACKET3(PACKET3_DISPATCH_DIRECT, 3));
	base->emit(base, gx);
	base->emit(base, gy);
	base->emit(base, gz);
	base->emit(base, flags);
	base->emit_aligned(base, 7, GFX_COMPUTE_NOP);
}

static void gfx_write_confirm_default(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint64_t dst, uint32_t val)
{
	base->emit(base, PACKET3(PACKET3_WRITE_DATA, 3));
	base->emit(base, WRITE_DATA_DST_SEL(5) | WR_CONFIRM);
	base->emit(base, (uint32_t)dst);
	base->emit(base, (uint32_t)(dst >> 32));
	base->emit(base, val);
	base->emit_aligned(base, 7, GFX_COMPUTE_NOP);
}

static void gfx_dispatch_direct_gfx9(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint32_t gx, uint32_t gy, uint32_t gz,
	uint32_t flags_unused)
{
	gfx_dispatch_direct_default(f, base, gx, gy, gz, 0x00000000);
}

static void gfx_dispatch_direct_gfx10(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint32_t gx, uint32_t gy, uint32_t gz,
	uint32_t flags_unused)
{
	gfx_dispatch_direct_default(f, base, gx, gy, gz, 0x00000045);
}

static void gfx_program_compute_gfx11(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint64_t code, uint64_t udata0,
	uint32_t rsrc1, uint32_t rsrc2,
	uint32_t tx, uint32_t ty, uint32_t tz)
{
	base->emit(base, PACKET3(PKT3_CONTEXT_CONTROL, 1));
	base->emit(base, 0x80000000);
	base->emit(base, 0x80000000);

	base->emit(base, PACKET3(PKT3_CLEAR_STATE, 0));
	base->emit(base, 0x80000000);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_PGM_LO));
	base->emit(base, (uint32_t)(code >> 8));
	base->emit(base, (uint32_t)(code >> 40));

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_PGM_RSRC1));
	base->emit(base, rsrc1);
	base->emit(base, rsrc2);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_USER_DATA_0));
	base->emit(base, (uint32_t)udata0);
	base->emit(base, (uint32_t)(udata0 >> 32));

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 1));
	base->emit(base, f->get_reg_offset(COMPUTE_RESOURCE_LIMITS));
	base->emit(base, 0);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 3));
	base->emit(base, f->get_reg_offset(COMPUTE_NUM_THREAD_X));
	base->emit(base, tx);
	base->emit(base, ty);
	base->emit(base, tz);
}

static void gfx_program_compute_gfx12(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint64_t code, uint64_t udata0,
	uint32_t rsrc1, uint32_t rsrc2,
	uint32_t tx, uint32_t ty, uint32_t tz)
{
	base->emit(base, PACKET3(PKT3_CONTEXT_CONTROL, 1));
	base->emit(base, 0x80000000);
	base->emit(base, 0x80000000);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_PGM_LO));
	base->emit(base, (uint32_t)(code >> 8));
	base->emit(base, (uint32_t)(code >> 40));

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_PGM_RSRC1));
	base->emit(base, rsrc1);
	base->emit(base, rsrc2);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 2));
	base->emit(base, f->get_reg_offset(COMPUTE_USER_DATA_0));
	base->emit(base, (uint32_t)udata0);
	base->emit(base, (uint32_t)(udata0 >> 32));

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 1));
	base->emit(base, f->get_reg_offset(COMPUTE_RESOURCE_LIMITS));
	base->emit(base, 0);

	base->emit(base, PACKET3(PKT3_SET_SH_REG, 3));
	base->emit(base, f->get_reg_offset(COMPUTE_NUM_THREAD_X));
	base->emit(base, tx);
	base->emit(base, ty);
	base->emit(base, tz);
}


static void gfx_dispatch_direct_gfx11(
	const struct amdgpu_ip_funcs *f,
	struct amdgpu_cmd_base *base,
	uint32_t gx, uint32_t gy, uint32_t gz,
	uint32_t flags_unused)
{
	gfx_dispatch_direct_default(f, base, gx, gy, gz, 0x00000045);
}

static void gfx_emit_nops_default(struct amdgpu_cmd_base *base, uint32_t count)
{
	uint32_t i;

	for (i = 0; i < count; i++)
		base->emit(base, PACKET3(PACKET3_NOP, 0));
}

static void gfx_write_data_mem_default(
	const struct amdgpu_ip_funcs *funcs,
	struct amdgpu_cmd_base *base,
	uint32_t engine_sel,
	uint64_t dst_addr,
	uint32_t value,
	bool wr_confirm)
{
	uint32_t flags = WRITE_DATA_DST_SEL(5);	/* memory address */

	flags |= WRITE_DATA_ENGINE_SEL(engine_sel);
	if (wr_confirm)
		flags |= WR_CONFIRM;

	base->emit(base, PACKET3(PACKET3_WRITE_DATA, 3));
	base->emit(base, flags);
	base->emit(base, lower_32_bits(dst_addr));
	base->emit(base, upper_32_bits(dst_addr));
	base->emit(base, value);
}


void amd_ip_blocks_ex_init(struct amdgpu_ip_funcs *funcs)
{
	funcs->gfx_program_compute = gfx_program_compute_default;
	funcs->gfx_dispatch_direct = gfx_dispatch_direct_default;
	funcs->gfx_write_confirm = gfx_write_confirm_default;
	funcs->gfx_emit_nops = gfx_emit_nops_default;
	funcs->gfx_write_data_mem = gfx_write_data_mem_default;

	switch (funcs->family_id) {
	case AMDGPU_FAMILY_RV:
	case AMDGPU_FAMILY_NV:
	case AMDGPU_FAMILY_VGH:
		funcs->gfx_dispatch_direct = gfx_dispatch_direct_gfx9;
		break;
	case AMDGPU_FAMILY_YC:
	case AMDGPU_FAMILY_GC_10_3_6:
	case AMDGPU_FAMILY_GC_10_3_7:
		funcs->gfx_dispatch_direct = gfx_dispatch_direct_gfx10;
		break;
	case AMDGPU_FAMILY_GC_11_0_0:
	case AMDGPU_FAMILY_GC_11_0_1:
	case AMDGPU_FAMILY_GC_11_5_0:
		funcs->gfx_program_compute = gfx_program_compute_gfx11;
		funcs->gfx_dispatch_direct = gfx_dispatch_direct_gfx11;
		break;
	case AMDGPU_FAMILY_GC_12_0_0:
		funcs->gfx_program_compute =gfx_program_compute_gfx12;
		funcs->gfx_dispatch_direct = gfx_dispatch_direct_gfx11;
		break;
	default:
		break;
	}
}