summaryrefslogtreecommitdiff
path: root/drivers/media/platform/amphion/vpu_rpc.h
blob: 7eb6f01e6ab5d1c9cf00b78e5985e0ed625556fa (plain)
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Copyright 2020-2021 NXP
 */

#ifndef _AMPHION_VPU_RPC_H
#define _AMPHION_VPU_RPC_H

#include <media/videobuf2-core.h>
#include "vpu_codec.h"

struct vpu_rpc_buffer_desc {
	u32 wptr;
	u32 rptr;
	u32 start;
	u32 end;
};

struct vpu_shared_addr {
	void *iface;
	struct vpu_rpc_buffer_desc *cmd_desc;
	void *cmd_mem_vir;
	struct vpu_rpc_buffer_desc *msg_desc;
	void *msg_mem_vir;

	unsigned long boot_addr;
	struct vpu_core *core;
	void *priv;
};

struct vpu_rpc_event_header {
	u32 index;
	u32 id;
	u32 num;
};

struct vpu_rpc_event {
	struct vpu_rpc_event_header hdr;
	u32 data[128];
};

struct vpu_iface_ops {
	bool (*check_codec)(enum vpu_core_type type);
	bool (*check_fmt)(enum vpu_core_type type, u32 pixelfmt);
	u32 (*get_data_size)(void);
	int (*check_memory_region)(dma_addr_t base, dma_addr_t addr, u32 size);
	int (*boot_core)(struct vpu_core *core);
	int (*shutdown_core)(struct vpu_core *core);
	int (*restore_core)(struct vpu_core *core);
	int (*get_power_state)(struct vpu_core *core);
	int (*on_firmware_loaded)(struct vpu_core *core);
	void (*init_rpc)(struct vpu_shared_addr *shared,
			 struct vpu_buffer *rpc, dma_addr_t boot_addr);
	void (*set_log_buf)(struct vpu_shared_addr *shared,
			    struct vpu_buffer *log);
	void (*set_system_cfg)(struct vpu_shared_addr *shared,
			       u32 regs_base, void __iomem *regs, u32 index);
	void (*set_stream_cfg)(struct vpu_shared_addr *shared, u32 index);
	u32 (*get_version)(struct vpu_shared_addr *shared);
	u32 (*get_max_instance_count)(struct vpu_shared_addr *shared);
	int (*get_stream_buffer_size)(struct vpu_shared_addr *shared);
	int (*send_cmd_buf)(struct vpu_shared_addr *shared,
			    struct vpu_rpc_event *cmd);
	int (*receive_msg_buf)(struct vpu_shared_addr *shared,
			       struct vpu_rpc_event *msg);
	int (*pack_cmd)(struct vpu_rpc_event *pkt, u32 index, u32 id, void *data);
	int (*convert_msg_id)(u32 msg_id);
	int (*unpack_msg_data)(struct vpu_rpc_event *pkt, void *data);
	int (*input_frame)(struct vpu_shared_addr *shared,
			   struct vpu_inst *inst, struct vb2_buffer *vb);
	int (*config_memory_resource)(struct vpu_shared_addr *shared,
				      u32 instance,
				      u32 type,
				      u32 index,
				      struct vpu_buffer *buf);
	int (*config_stream_buffer)(struct vpu_shared_addr *shared,
				    u32 instance,
				    struct vpu_buffer *buf);
	int (*update_stream_buffer)(struct vpu_shared_addr *shared,
				    u32 instance, u32 ptr, bool write);
	int (*get_stream_buffer_desc)(struct vpu_shared_addr *shared,
				      u32 instance,
				      struct vpu_rpc_buffer_desc *desc);
	int (*set_encode_params)(struct vpu_shared_addr *shared,
				 u32 instance,
				 struct vpu_encode_params *params,
				 u32 update);
	int (*set_decode_params)(struct vpu_shared_addr *shared,
				 u32 instance,
				 struct vpu_decode_params *params,
				 u32 update);
	int (*add_scode)(struct vpu_shared_addr *shared,
			 u32 instance,
			 struct vpu_buffer *stream_buffer,
			 u32 pixelformat,
			 u32 scode_type);
	int (*pre_send_cmd)(struct vpu_shared_addr *shared, u32 instance);
	int (*post_send_cmd)(struct vpu_shared_addr *shared, u32 instance);
	int (*init_instance)(struct vpu_shared_addr *shared, u32 instance);
};

enum {
	VPU_CORE_MEMORY_INVALID = 0,
	VPU_CORE_MEMORY_CACHED,
	VPU_CORE_MEMORY_UNCACHED
};

struct vpu_rpc_region_t {
	dma_addr_t start;
	dma_addr_t end;
	dma_addr_t type;
};

struct vpu_iface_ops *vpu_core_get_iface(struct vpu_core *core);
struct vpu_iface_ops *vpu_inst_get_iface(struct vpu_inst *inst);
int vpu_iface_check_memory_region(struct vpu_core *core, dma_addr_t addr, u32 size);

static inline bool vpu_iface_check_codec(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (ops && ops->check_codec)
		return ops->check_codec(core->type);

	return true;
}

static inline bool vpu_iface_check_format(struct vpu_inst *inst, u32 pixelfmt)
{
	struct vpu_iface_ops *ops = vpu_inst_get_iface(inst);

	if (ops && ops->check_fmt)
		return ops->check_fmt(inst->type, pixelfmt);

	return true;
}

static inline int vpu_iface_boot_core(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (ops && ops->boot_core)
		return ops->boot_core(core);
	return 0;
}

static inline int vpu_iface_get_power_state(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (ops && ops->get_power_state)
		return ops->get_power_state(core);
	return 1;
}

static inline int vpu_iface_shutdown_core(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (ops && ops->shutdown_core)
		return ops->shutdown_core(core);
	return 0;
}

static inline int vpu_iface_restore_core(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (ops && ops->restore_core)
		return ops->restore_core(core);
	return 0;
}

static inline int vpu_iface_on_firmware_loaded(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (ops && ops->on_firmware_loaded)
		return ops->on_firmware_loaded(core);

	return 0;
}

static inline u32 vpu_iface_get_data_size(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->get_data_size)
		return 0;

	return ops->get_data_size();
}

static inline int vpu_iface_init(struct vpu_core *core,
				 struct vpu_shared_addr *shared,
				 struct vpu_buffer *rpc,
				 dma_addr_t boot_addr)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->init_rpc)
		return -EINVAL;

	ops->init_rpc(shared, rpc, boot_addr);
	core->iface = shared;
	shared->core = core;
	if (rpc->bytesused > rpc->length)
		return -ENOSPC;
	return 0;
}

static inline int vpu_iface_set_log_buf(struct vpu_core *core,
					struct vpu_buffer *log)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops)
		return -EINVAL;

	if (ops->set_log_buf)
		ops->set_log_buf(core->iface, log);

	return 0;
}

static inline int vpu_iface_config_system(struct vpu_core *core, u32 regs_base, void __iomem *regs)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops)
		return -EINVAL;
	if (ops->set_system_cfg)
		ops->set_system_cfg(core->iface, regs_base, regs, core->id);

	return 0;
}

static inline int vpu_iface_get_stream_buffer_size(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->get_stream_buffer_size)
		return 0;

	return ops->get_stream_buffer_size(core->iface);
}

static inline int vpu_iface_config_stream(struct vpu_inst *inst)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || inst->id < 0)
		return -EINVAL;
	if (ops->set_stream_cfg)
		ops->set_stream_cfg(inst->core->iface, inst->id);
	return 0;
}

static inline int vpu_iface_send_cmd(struct vpu_core *core, struct vpu_rpc_event *cmd)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->send_cmd_buf)
		return -EINVAL;

	return ops->send_cmd_buf(core->iface, cmd);
}

static inline int vpu_iface_receive_msg(struct vpu_core *core, struct vpu_rpc_event *msg)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->receive_msg_buf)
		return -EINVAL;

	return ops->receive_msg_buf(core->iface, msg);
}

static inline int vpu_iface_pack_cmd(struct vpu_core *core,
				     struct vpu_rpc_event *pkt,
				     u32 index, u32 id, void *data)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->pack_cmd)
		return -EINVAL;
	return ops->pack_cmd(pkt, index, id, data);
}

static inline int vpu_iface_convert_msg_id(struct vpu_core *core, u32 msg_id)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->convert_msg_id)
		return -EINVAL;

	return ops->convert_msg_id(msg_id);
}

static inline int vpu_iface_unpack_msg_data(struct vpu_core *core,
					    struct vpu_rpc_event *pkt, void *data)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->unpack_msg_data)
		return -EINVAL;

	return ops->unpack_msg_data(pkt, data);
}

static inline int vpu_iface_input_frame(struct vpu_inst *inst,
					struct vb2_buffer *vb)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
	int ret;

	if (!ops || !ops->input_frame)
		return -EINVAL;

	ret = ops->input_frame(inst->core->iface, inst, vb);
	if (ret < 0)
		return ret;
	inst->total_input_count++;
	return ret;
}

static inline int vpu_iface_config_memory_resource(struct vpu_inst *inst,
						   u32 type,
						   u32 index,
						   struct vpu_buffer *buf)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->config_memory_resource || inst->id < 0)
		return -EINVAL;

	return ops->config_memory_resource(inst->core->iface,
					inst->id,
					type, index, buf);
}

static inline int vpu_iface_config_stream_buffer(struct vpu_inst *inst,
						 struct vpu_buffer *buf)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->config_stream_buffer || inst->id < 0)
		return -EINVAL;

	if ((buf->phys % 4) || (buf->length % 4))
		return -EINVAL;
	if (buf->phys + buf->length > (u64)UINT_MAX)
		return -EINVAL;

	return ops->config_stream_buffer(inst->core->iface, inst->id, buf);
}

static inline int vpu_iface_update_stream_buffer(struct vpu_inst *inst,
						 u32 ptr, bool write)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->update_stream_buffer || inst->id < 0)
		return -EINVAL;

	return ops->update_stream_buffer(inst->core->iface, inst->id, ptr, write);
}

static inline int vpu_iface_get_stream_buffer_desc(struct vpu_inst *inst,
						   struct vpu_rpc_buffer_desc *desc)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->get_stream_buffer_desc || inst->id < 0)
		return -EINVAL;

	if (!desc)
		return 0;

	return ops->get_stream_buffer_desc(inst->core->iface, inst->id, desc);
}

static inline u32 vpu_iface_get_version(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->get_version)
		return 0;

	return ops->get_version(core->iface);
}

static inline u32 vpu_iface_get_max_instance_count(struct vpu_core *core)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(core);

	if (!ops || !ops->get_max_instance_count)
		return 0;

	return ops->get_max_instance_count(core->iface);
}

static inline int vpu_iface_set_encode_params(struct vpu_inst *inst,
					      struct vpu_encode_params *params, u32 update)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->set_encode_params || inst->id < 0)
		return -EINVAL;

	return ops->set_encode_params(inst->core->iface, inst->id, params, update);
}

static inline int vpu_iface_set_decode_params(struct vpu_inst *inst,
					      struct vpu_decode_params *params, u32 update)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->set_decode_params  || inst->id < 0)
		return -EINVAL;

	return ops->set_decode_params(inst->core->iface, inst->id, params, update);
}

static inline int vpu_iface_add_scode(struct vpu_inst *inst, u32 scode_type)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (!ops || !ops->add_scode  || inst->id < 0)
		return -EINVAL;

	return ops->add_scode(inst->core->iface, inst->id,
				&inst->stream_buffer,
				inst->out_format.pixfmt,
				scode_type);
}

static inline int vpu_iface_pre_send_cmd(struct vpu_inst *inst)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (ops && ops->pre_send_cmd && inst->id >= 0)
		return ops->pre_send_cmd(inst->core->iface, inst->id);
	return 0;
}

static inline int vpu_iface_post_send_cmd(struct vpu_inst *inst)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (ops && ops->post_send_cmd && inst->id >= 0)
		return ops->post_send_cmd(inst->core->iface, inst->id);
	return 0;
}

static inline int vpu_iface_init_instance(struct vpu_inst *inst)
{
	struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);

	if (ops && ops->init_instance && inst->id >= 0)
		return ops->init_instance(inst->core->iface, inst->id);

	return 0;
}

#endif