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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/* Copyright (c) 2023 Imagination Technologies Ltd. */
#include "pvr_device.h"
#include "pvr_rogue_fwif_stream.h"
#include "pvr_stream.h"
#include <linux/align.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <uapi/drm/pvr_drm.h>
static __always_inline bool
stream_def_is_supported(struct pvr_device *pvr_dev, const struct pvr_stream_def *stream_def)
{
if (stream_def->feature == PVR_FEATURE_NONE)
return true;
if (!(stream_def->feature & PVR_FEATURE_NOT) &&
pvr_device_has_feature(pvr_dev, stream_def->feature)) {
return true;
}
if ((stream_def->feature & PVR_FEATURE_NOT) &&
!pvr_device_has_feature(pvr_dev, stream_def->feature & ~PVR_FEATURE_NOT)) {
return true;
}
return false;
}
static int
pvr_stream_get_data(u8 *stream, u32 *stream_offset, u32 stream_size, u32 data_size, u32 align_size,
void *dest)
{
*stream_offset = ALIGN(*stream_offset, align_size);
if ((*stream_offset + data_size) > stream_size)
return -EINVAL;
memcpy(dest, stream + *stream_offset, data_size);
(*stream_offset) += data_size;
return 0;
}
/**
* pvr_stream_process_1() - Process a single stream and fill destination structure
* @pvr_dev: Device pointer.
* @stream_def: Stream definition.
* @nr_entries: Number of entries in &stream_def.
* @stream: Pointer to stream.
* @stream_offset: Starting offset within stream.
* @stream_size: Size of input stream, in bytes.
* @dest: Pointer to destination structure.
* @dest_size: Size of destination structure.
* @stream_offset_out: Pointer to variable to write updated stream offset to. May be NULL.
*
* Returns:
* * 0 on success, or
* * -%EINVAL on malformed stream.
*/
static int
pvr_stream_process_1(struct pvr_device *pvr_dev, const struct pvr_stream_def *stream_def,
u32 nr_entries, u8 *stream, u32 stream_offset, u32 stream_size,
u8 *dest, u32 dest_size, u32 *stream_offset_out)
{
int err = 0;
u32 i;
for (i = 0; i < nr_entries; i++) {
if (stream_def[i].offset >= dest_size) {
err = -EINVAL;
break;
}
if (!stream_def_is_supported(pvr_dev, &stream_def[i]))
continue;
switch (stream_def[i].size) {
case PVR_STREAM_SIZE_8:
err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u8),
sizeof(u8), dest + stream_def[i].offset);
if (err)
return err;
break;
case PVR_STREAM_SIZE_16:
err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u16),
sizeof(u16), dest + stream_def[i].offset);
if (err)
return err;
break;
case PVR_STREAM_SIZE_32:
err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u32),
sizeof(u32), dest + stream_def[i].offset);
if (err)
return err;
break;
case PVR_STREAM_SIZE_64:
err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u64),
sizeof(u64), dest + stream_def[i].offset);
if (err)
return err;
break;
case PVR_STREAM_SIZE_ARRAY:
err = pvr_stream_get_data(stream, &stream_offset, stream_size,
stream_def[i].array_size, sizeof(u64),
dest + stream_def[i].offset);
if (err)
return err;
break;
}
}
if (stream_offset_out)
*stream_offset_out = stream_offset;
return err;
}
static int
pvr_stream_process_ext_stream(struct pvr_device *pvr_dev,
const struct pvr_stream_cmd_defs *cmd_defs, void *ext_stream,
u32 stream_offset, u32 ext_stream_size, void *dest)
{
u32 musthave_masks[PVR_STREAM_EXTHDR_TYPE_MAX];
u32 ext_header;
int err = 0;
u32 i;
/* Copy "must have" mask from device. We clear this as we process the stream. */
memcpy(musthave_masks, pvr_dev->stream_musthave_quirks[cmd_defs->type],
sizeof(musthave_masks));
do {
const struct pvr_stream_ext_header *header;
u32 type;
u32 data;
err = pvr_stream_get_data(ext_stream, &stream_offset, ext_stream_size, sizeof(u32),
sizeof(ext_header), &ext_header);
if (err)
return err;
type = (ext_header & PVR_STREAM_EXTHDR_TYPE_MASK) >> PVR_STREAM_EXTHDR_TYPE_SHIFT;
data = ext_header & PVR_STREAM_EXTHDR_DATA_MASK;
if (type >= cmd_defs->ext_nr_headers)
return -EINVAL;
header = &cmd_defs->ext_headers[type];
if (data & ~header->valid_mask)
return -EINVAL;
musthave_masks[type] &= ~data;
for (i = 0; i < header->ext_streams_num; i++) {
const struct pvr_stream_ext_def *ext_def = &header->ext_streams[i];
if (!(ext_header & ext_def->header_mask))
continue;
if (!pvr_device_has_uapi_quirk(pvr_dev, ext_def->quirk))
return -EINVAL;
err = pvr_stream_process_1(pvr_dev, ext_def->stream, ext_def->stream_len,
ext_stream, stream_offset,
ext_stream_size, dest,
cmd_defs->dest_size, &stream_offset);
if (err)
return err;
}
} while (ext_header & PVR_STREAM_EXTHDR_CONTINUATION);
/*
* Verify that "must have" mask is now zero. If it isn't then one of the "must have" quirks
* for this command was not present.
*/
for (i = 0; i < cmd_defs->ext_nr_headers; i++) {
if (musthave_masks[i])
return -EINVAL;
}
return 0;
}
/**
* pvr_stream_process() - Build FW structure from stream
* @pvr_dev: Device pointer.
* @cmd_defs: Stream definition.
* @stream: Pointer to command stream.
* @stream_size: Size of command stream, in bytes.
* @dest_out: Pointer to destination buffer.
*
* Caller is responsible for freeing the output structure.
*
* Returns:
* * 0 on success,
* * -%ENOMEM on out of memory, or
* * -%EINVAL on malformed stream.
*/
int
pvr_stream_process(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs *cmd_defs,
void *stream, u32 stream_size, void *dest_out)
{
u32 stream_offset = 0;
u32 main_stream_len;
u32 padding;
int err;
if (!stream || !stream_size)
return -EINVAL;
err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u32),
sizeof(u32), &main_stream_len);
if (err)
return err;
/*
* u32 after stream length is padding to ensure u64 alignment, but may be used for expansion
* in the future. Verify it's zero.
*/
err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u32),
sizeof(u32), &padding);
if (err)
return err;
if (main_stream_len < stream_offset || main_stream_len > stream_size || padding)
return -EINVAL;
err = pvr_stream_process_1(pvr_dev, cmd_defs->main_stream, cmd_defs->main_stream_len,
stream, stream_offset, main_stream_len, dest_out,
cmd_defs->dest_size, &stream_offset);
if (err)
return err;
if (stream_offset < stream_size) {
err = pvr_stream_process_ext_stream(pvr_dev, cmd_defs, stream, stream_offset,
stream_size, dest_out);
if (err)
return err;
} else {
u32 i;
/*
* If we don't have an extension stream then there must not be any "must have"
* quirks for this command.
*/
for (i = 0; i < cmd_defs->ext_nr_headers; i++) {
if (pvr_dev->stream_musthave_quirks[cmd_defs->type][i])
return -EINVAL;
}
}
return 0;
}
/**
* pvr_stream_create_musthave_masks() - Create "must have" masks for streams based on current device
* quirks
* @pvr_dev: Device pointer.
*/
void
pvr_stream_create_musthave_masks(struct pvr_device *pvr_dev)
{
memset(pvr_dev->stream_musthave_quirks, 0, sizeof(pvr_dev->stream_musthave_quirks));
if (pvr_device_has_uapi_quirk(pvr_dev, 47217))
pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_FRAG][0] |=
PVR_STREAM_EXTHDR_FRAG0_BRN47217;
if (pvr_device_has_uapi_quirk(pvr_dev, 49927)) {
pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_GEOM][0] |=
PVR_STREAM_EXTHDR_GEOM0_BRN49927;
pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_FRAG][0] |=
PVR_STREAM_EXTHDR_FRAG0_BRN49927;
pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_COMPUTE][0] |=
PVR_STREAM_EXTHDR_COMPUTE0_BRN49927;
}
}
|