1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 2020-2021 NXP
6 #ifndef _AMPHION_VPU_RPC_H
7 #define _AMPHION_VPU_RPC_H
9 #include <media/videobuf2-core.h>
10 #include "vpu_codec.h"
12 struct vpu_rpc_buffer_desc {
19 struct vpu_shared_addr {
21 struct vpu_rpc_buffer_desc *cmd_desc;
23 struct vpu_rpc_buffer_desc *msg_desc;
26 unsigned long boot_addr;
27 struct vpu_core *core;
31 struct vpu_rpc_event_header {
37 struct vpu_rpc_event {
38 struct vpu_rpc_event_header hdr;
42 struct vpu_iface_ops {
43 bool (*check_codec)(enum vpu_core_type type);
44 bool (*check_fmt)(enum vpu_core_type type, u32 pixelfmt);
45 u32 (*get_data_size)(void);
46 int (*check_memory_region)(dma_addr_t base, dma_addr_t addr, u32 size);
47 int (*boot_core)(struct vpu_core *core);
48 int (*shutdown_core)(struct vpu_core *core);
49 int (*restore_core)(struct vpu_core *core);
50 int (*get_power_state)(struct vpu_core *core);
51 int (*on_firmware_loaded)(struct vpu_core *core);
52 void (*init_rpc)(struct vpu_shared_addr *shared,
53 struct vpu_buffer *rpc, dma_addr_t boot_addr);
54 void (*set_log_buf)(struct vpu_shared_addr *shared,
55 struct vpu_buffer *log);
56 void (*set_system_cfg)(struct vpu_shared_addr *shared,
57 u32 regs_base, void __iomem *regs, u32 index);
58 void (*set_stream_cfg)(struct vpu_shared_addr *shared, u32 index);
59 u32 (*get_version)(struct vpu_shared_addr *shared);
60 u32 (*get_max_instance_count)(struct vpu_shared_addr *shared);
61 int (*get_stream_buffer_size)(struct vpu_shared_addr *shared);
62 int (*send_cmd_buf)(struct vpu_shared_addr *shared,
63 struct vpu_rpc_event *cmd);
64 int (*receive_msg_buf)(struct vpu_shared_addr *shared,
65 struct vpu_rpc_event *msg);
66 int (*pack_cmd)(struct vpu_rpc_event *pkt, u32 index, u32 id, void *data);
67 int (*convert_msg_id)(u32 msg_id);
68 int (*unpack_msg_data)(struct vpu_rpc_event *pkt, void *data);
69 int (*input_frame)(struct vpu_shared_addr *shared,
70 struct vpu_inst *inst, struct vb2_buffer *vb);
71 int (*config_memory_resource)(struct vpu_shared_addr *shared,
75 struct vpu_buffer *buf);
76 int (*config_stream_buffer)(struct vpu_shared_addr *shared,
78 struct vpu_buffer *buf);
79 int (*update_stream_buffer)(struct vpu_shared_addr *shared,
80 u32 instance, u32 ptr, bool write);
81 int (*get_stream_buffer_desc)(struct vpu_shared_addr *shared,
83 struct vpu_rpc_buffer_desc *desc);
84 int (*set_encode_params)(struct vpu_shared_addr *shared,
86 struct vpu_encode_params *params,
88 int (*set_decode_params)(struct vpu_shared_addr *shared,
90 struct vpu_decode_params *params,
92 int (*add_scode)(struct vpu_shared_addr *shared,
94 struct vpu_buffer *stream_buffer,
97 int (*pre_send_cmd)(struct vpu_shared_addr *shared, u32 instance);
98 int (*post_send_cmd)(struct vpu_shared_addr *shared, u32 instance);
99 int (*init_instance)(struct vpu_shared_addr *shared, u32 instance);
103 VPU_CORE_MEMORY_INVALID = 0,
104 VPU_CORE_MEMORY_CACHED,
105 VPU_CORE_MEMORY_UNCACHED
108 struct vpu_rpc_region_t {
114 struct vpu_iface_ops *vpu_core_get_iface(struct vpu_core *core);
115 struct vpu_iface_ops *vpu_inst_get_iface(struct vpu_inst *inst);
116 int vpu_iface_check_memory_region(struct vpu_core *core, dma_addr_t addr, u32 size);
118 static inline bool vpu_iface_check_codec(struct vpu_core *core)
120 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
122 if (ops && ops->check_codec)
123 return ops->check_codec(core->type);
128 static inline bool vpu_iface_check_format(struct vpu_inst *inst, u32 pixelfmt)
130 struct vpu_iface_ops *ops = vpu_inst_get_iface(inst);
132 if (ops && ops->check_fmt)
133 return ops->check_fmt(inst->type, pixelfmt);
138 static inline int vpu_iface_boot_core(struct vpu_core *core)
140 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
142 if (ops && ops->boot_core)
143 return ops->boot_core(core);
147 static inline int vpu_iface_get_power_state(struct vpu_core *core)
149 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
151 if (ops && ops->get_power_state)
152 return ops->get_power_state(core);
156 static inline int vpu_iface_shutdown_core(struct vpu_core *core)
158 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
160 if (ops && ops->shutdown_core)
161 return ops->shutdown_core(core);
165 static inline int vpu_iface_restore_core(struct vpu_core *core)
167 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
169 if (ops && ops->restore_core)
170 return ops->restore_core(core);
174 static inline int vpu_iface_on_firmware_loaded(struct vpu_core *core)
176 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
178 if (ops && ops->on_firmware_loaded)
179 return ops->on_firmware_loaded(core);
184 static inline u32 vpu_iface_get_data_size(struct vpu_core *core)
186 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
188 if (!ops || !ops->get_data_size)
191 return ops->get_data_size();
194 static inline int vpu_iface_init(struct vpu_core *core,
195 struct vpu_shared_addr *shared,
196 struct vpu_buffer *rpc,
197 dma_addr_t boot_addr)
199 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
201 if (!ops || !ops->init_rpc)
204 ops->init_rpc(shared, rpc, boot_addr);
205 core->iface = shared;
207 if (rpc->bytesused > rpc->length)
212 static inline int vpu_iface_set_log_buf(struct vpu_core *core,
213 struct vpu_buffer *log)
215 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
220 if (ops->set_log_buf)
221 ops->set_log_buf(core->iface, log);
226 static inline int vpu_iface_config_system(struct vpu_core *core, u32 regs_base, void __iomem *regs)
228 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
232 if (ops->set_system_cfg)
233 ops->set_system_cfg(core->iface, regs_base, regs, core->id);
238 static inline int vpu_iface_get_stream_buffer_size(struct vpu_core *core)
240 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
242 if (!ops || !ops->get_stream_buffer_size)
245 return ops->get_stream_buffer_size(core->iface);
248 static inline int vpu_iface_config_stream(struct vpu_inst *inst)
250 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
252 if (!ops || inst->id < 0)
254 if (ops->set_stream_cfg)
255 ops->set_stream_cfg(inst->core->iface, inst->id);
259 static inline int vpu_iface_send_cmd(struct vpu_core *core, struct vpu_rpc_event *cmd)
261 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
263 if (!ops || !ops->send_cmd_buf)
266 return ops->send_cmd_buf(core->iface, cmd);
269 static inline int vpu_iface_receive_msg(struct vpu_core *core, struct vpu_rpc_event *msg)
271 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
273 if (!ops || !ops->receive_msg_buf)
276 return ops->receive_msg_buf(core->iface, msg);
279 static inline int vpu_iface_pack_cmd(struct vpu_core *core,
280 struct vpu_rpc_event *pkt,
281 u32 index, u32 id, void *data)
283 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
285 if (!ops || !ops->pack_cmd)
287 return ops->pack_cmd(pkt, index, id, data);
290 static inline int vpu_iface_convert_msg_id(struct vpu_core *core, u32 msg_id)
292 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
294 if (!ops || !ops->convert_msg_id)
297 return ops->convert_msg_id(msg_id);
300 static inline int vpu_iface_unpack_msg_data(struct vpu_core *core,
301 struct vpu_rpc_event *pkt, void *data)
303 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
305 if (!ops || !ops->unpack_msg_data)
308 return ops->unpack_msg_data(pkt, data);
311 static inline int vpu_iface_input_frame(struct vpu_inst *inst,
312 struct vb2_buffer *vb)
314 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
316 if (!ops || !ops->input_frame)
319 return ops->input_frame(inst->core->iface, inst, vb);
322 static inline int vpu_iface_config_memory_resource(struct vpu_inst *inst,
325 struct vpu_buffer *buf)
327 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
329 if (!ops || !ops->config_memory_resource || inst->id < 0)
332 return ops->config_memory_resource(inst->core->iface,
337 static inline int vpu_iface_config_stream_buffer(struct vpu_inst *inst,
338 struct vpu_buffer *buf)
340 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
342 if (!ops || !ops->config_stream_buffer || inst->id < 0)
345 if ((buf->phys % 4) || (buf->length % 4))
347 if (buf->phys + buf->length > (u64)UINT_MAX)
350 return ops->config_stream_buffer(inst->core->iface, inst->id, buf);
353 static inline int vpu_iface_update_stream_buffer(struct vpu_inst *inst,
356 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
358 if (!ops || !ops->update_stream_buffer || inst->id < 0)
361 return ops->update_stream_buffer(inst->core->iface, inst->id, ptr, write);
364 static inline int vpu_iface_get_stream_buffer_desc(struct vpu_inst *inst,
365 struct vpu_rpc_buffer_desc *desc)
367 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
369 if (!ops || !ops->get_stream_buffer_desc || inst->id < 0)
375 return ops->get_stream_buffer_desc(inst->core->iface, inst->id, desc);
378 static inline u32 vpu_iface_get_version(struct vpu_core *core)
380 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
382 if (!ops || !ops->get_version)
385 return ops->get_version(core->iface);
388 static inline u32 vpu_iface_get_max_instance_count(struct vpu_core *core)
390 struct vpu_iface_ops *ops = vpu_core_get_iface(core);
392 if (!ops || !ops->get_max_instance_count)
395 return ops->get_max_instance_count(core->iface);
398 static inline int vpu_iface_set_encode_params(struct vpu_inst *inst,
399 struct vpu_encode_params *params, u32 update)
401 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
403 if (!ops || !ops->set_encode_params || inst->id < 0)
406 return ops->set_encode_params(inst->core->iface, inst->id, params, update);
409 static inline int vpu_iface_set_decode_params(struct vpu_inst *inst,
410 struct vpu_decode_params *params, u32 update)
412 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
414 if (!ops || !ops->set_decode_params || inst->id < 0)
417 return ops->set_decode_params(inst->core->iface, inst->id, params, update);
420 static inline int vpu_iface_add_scode(struct vpu_inst *inst, u32 scode_type)
422 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
424 if (!ops || !ops->add_scode || inst->id < 0)
427 return ops->add_scode(inst->core->iface, inst->id,
428 &inst->stream_buffer,
429 inst->out_format.pixfmt,
433 static inline int vpu_iface_pre_send_cmd(struct vpu_inst *inst)
435 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
437 if (ops && ops->pre_send_cmd && inst->id >= 0)
438 return ops->pre_send_cmd(inst->core->iface, inst->id);
442 static inline int vpu_iface_post_send_cmd(struct vpu_inst *inst)
444 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
446 if (ops && ops->post_send_cmd && inst->id >= 0)
447 return ops->post_send_cmd(inst->core->iface, inst->id);
451 static inline int vpu_iface_init_instance(struct vpu_inst *inst)
453 struct vpu_iface_ops *ops = vpu_core_get_iface(inst->core);
455 if (ops && ops->init_instance && inst->id >= 0)
456 return ops->init_instance(inst->core->iface, inst->id);