1 // SPDX-License-Identifier: GPL-2.0
5 * Allegro DVT video encoder driver
8 #include <linux/bits.h>
10 #include <linux/firmware.h>
11 #include <linux/gcd.h>
12 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/log2.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/mfd/syscon/xlnx-vcu.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/sizes.h>
24 #include <linux/slab.h>
25 #include <linux/videodev2.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-event.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/v4l2-mem2mem.h>
31 #include <media/videobuf2-dma-contig.h>
32 #include <media/videobuf2-v4l2.h>
34 #include "allegro-mail.h"
39 * Support up to 4k video streams. The hardware actually supports higher
40 * resolutions, which are specified in PG252 June 6, 2018 (H.264/H.265 Video
41 * Codec Unit v1.1) Chapter 3.
43 #define ALLEGRO_WIDTH_MIN 128
44 #define ALLEGRO_WIDTH_DEFAULT 1920
45 #define ALLEGRO_WIDTH_MAX 3840
46 #define ALLEGRO_HEIGHT_MIN 64
47 #define ALLEGRO_HEIGHT_DEFAULT 1080
48 #define ALLEGRO_HEIGHT_MAX 2160
50 #define ALLEGRO_FRAMERATE_DEFAULT ((struct v4l2_fract) { 30, 1 })
52 #define ALLEGRO_GOP_SIZE_DEFAULT 25
53 #define ALLEGRO_GOP_SIZE_MAX 1000
56 * MCU Control Registers
58 * The Zynq UltraScale+ Devices Register Reference documents the registers
59 * with an offset of 0x9000, which equals the size of the SRAM and one page
60 * gap. The driver handles SRAM and registers separately and, therefore, is
61 * oblivious of the offset.
63 #define AL5_MCU_RESET 0x0000
64 #define AL5_MCU_RESET_SOFT BIT(0)
65 #define AL5_MCU_RESET_REGS BIT(1)
66 #define AL5_MCU_RESET_MODE 0x0004
67 #define AL5_MCU_RESET_MODE_SLEEP BIT(0)
68 #define AL5_MCU_RESET_MODE_HALT BIT(1)
69 #define AL5_MCU_STA 0x0008
70 #define AL5_MCU_STA_SLEEP BIT(0)
71 #define AL5_MCU_WAKEUP 0x000c
73 #define AL5_ICACHE_ADDR_OFFSET_MSB 0x0010
74 #define AL5_ICACHE_ADDR_OFFSET_LSB 0x0014
75 #define AL5_DCACHE_ADDR_OFFSET_MSB 0x0018
76 #define AL5_DCACHE_ADDR_OFFSET_LSB 0x001c
78 #define AL5_MCU_INTERRUPT 0x0100
79 #define AL5_ITC_CPU_IRQ_MSK 0x0104
80 #define AL5_ITC_CPU_IRQ_CLR 0x0108
81 #define AL5_ITC_CPU_IRQ_STA 0x010C
82 #define AL5_ITC_CPU_IRQ_STA_TRIGGERED BIT(0)
84 #define AXI_ADDR_OFFSET_IP 0x0208
87 * The MCU accesses the system memory with a 2G offset compared to CPU
90 #define MCU_CACHE_OFFSET SZ_2G
93 * The driver needs to reserve some space at the beginning of capture buffers,
94 * because it needs to write SPS/PPS NAL units. The encoder writes the actual
95 * frame data after the offset.
97 #define ENCODER_STREAM_OFFSET SZ_128
99 #define SIZE_MACROBLOCK 16
101 /* Encoding options */
102 #define LOG2_MAX_FRAME_NUM 4
103 #define LOG2_MAX_PIC_ORDER_CNT 10
104 #define BETA_OFFSET_DIV_2 -1
105 #define TC_OFFSET_DIV_2 -1
108 * This control allows applications to explicitly disable the encoder buffer.
109 * This value is Allegro specific.
111 #define V4L2_CID_USER_ALLEGRO_ENCODER_BUFFER (V4L2_CID_USER_ALLEGRO_BASE + 0)
114 module_param(debug, int, 0644);
115 MODULE_PARM_DESC(debug, "Debug level (0-2)");
117 struct allegro_buffer {
121 struct list_head head;
125 struct allegro_channel;
127 struct allegro_mbox {
128 struct allegro_dev *dev;
133 /* protect mailbox from simultaneous accesses */
137 struct allegro_encoder_buffer {
139 unsigned int color_depth;
140 unsigned int num_cores;
141 unsigned int clk_rate;
145 struct v4l2_device v4l2_dev;
146 struct video_device video_dev;
147 struct v4l2_m2m_dev *m2m_dev;
148 struct platform_device *plat_dev;
150 /* mutex protecting vb2_queue structure */
153 struct regmap *regmap;
155 struct regmap *settings;
157 struct clk *clk_core;
160 const struct fw_info *fw_info;
161 struct allegro_buffer firmware;
162 struct allegro_buffer suballocator;
163 bool has_encoder_buffer;
164 struct allegro_encoder_buffer encoder_buffer;
166 struct completion init_complete;
169 /* The mailbox interface */
170 struct allegro_mbox *mbox_command;
171 struct allegro_mbox *mbox_status;
174 * The downstream driver limits the users to 64 users, thus I can use
175 * a bitfield for the user_ids that are in use. See also user_id in
176 * struct allegro_channel.
178 unsigned long channel_user_ids;
179 struct list_head channels;
182 static const struct regmap_config allegro_regmap_config = {
187 .max_register = 0xfff,
188 .cache_type = REGCACHE_NONE,
191 static const struct regmap_config allegro_sram_config = {
196 .max_register = 0x7fff,
197 .cache_type = REGCACHE_NONE,
200 #define fh_to_channel(__fh) container_of(__fh, struct allegro_channel, fh)
202 struct allegro_channel {
203 struct allegro_dev *dev;
205 struct v4l2_ctrl_handler ctrl_handler;
210 struct v4l2_fract framerate;
212 enum v4l2_colorspace colorspace;
213 enum v4l2_ycbcr_encoding ycbcr_enc;
214 enum v4l2_quantization quantization;
215 enum v4l2_xfer_func xfer_func;
218 unsigned int sizeimage_raw;
219 unsigned int osequence;
222 unsigned int sizeimage_encoded;
223 unsigned int csequence;
225 bool frame_rc_enable;
226 unsigned int bitrate;
227 unsigned int bitrate_peak;
229 struct allegro_buffer config_blob;
231 unsigned int log2_max_frame_num;
232 bool temporal_mvp_enable;
234 bool enable_loop_filter_across_tiles;
235 bool enable_loop_filter_across_slices;
236 bool enable_deblocking_filter_override;
237 bool enable_reordering;
240 unsigned int num_ref_idx_l0;
241 unsigned int num_ref_idx_l1;
243 /* Maximum range for motion estimation */
248 /* Size limits of coding unit */
251 /* Size limits of transform unit */
254 int max_transfo_depth_intra;
255 int max_transfo_depth_inter;
257 struct v4l2_ctrl *mpeg_video_h264_profile;
258 struct v4l2_ctrl *mpeg_video_h264_level;
259 struct v4l2_ctrl *mpeg_video_h264_i_frame_qp;
260 struct v4l2_ctrl *mpeg_video_h264_max_qp;
261 struct v4l2_ctrl *mpeg_video_h264_min_qp;
262 struct v4l2_ctrl *mpeg_video_h264_p_frame_qp;
263 struct v4l2_ctrl *mpeg_video_h264_b_frame_qp;
265 struct v4l2_ctrl *mpeg_video_hevc_profile;
266 struct v4l2_ctrl *mpeg_video_hevc_level;
267 struct v4l2_ctrl *mpeg_video_hevc_tier;
268 struct v4l2_ctrl *mpeg_video_hevc_i_frame_qp;
269 struct v4l2_ctrl *mpeg_video_hevc_max_qp;
270 struct v4l2_ctrl *mpeg_video_hevc_min_qp;
271 struct v4l2_ctrl *mpeg_video_hevc_p_frame_qp;
272 struct v4l2_ctrl *mpeg_video_hevc_b_frame_qp;
274 struct v4l2_ctrl *mpeg_video_frame_rc_enable;
275 struct { /* video bitrate mode control cluster */
276 struct v4l2_ctrl *mpeg_video_bitrate_mode;
277 struct v4l2_ctrl *mpeg_video_bitrate;
278 struct v4l2_ctrl *mpeg_video_bitrate_peak;
280 struct v4l2_ctrl *mpeg_video_cpb_size;
281 struct v4l2_ctrl *mpeg_video_gop_size;
283 struct v4l2_ctrl *encoder_buffer;
285 /* user_id is used to identify the channel during CREATE_CHANNEL */
286 /* not sure, what to set here and if this is actually required */
288 /* channel_id is set by the mcu and used by all later commands */
291 struct list_head buffers_reference;
292 struct list_head buffers_intermediate;
294 struct list_head source_shadow_list;
295 struct list_head stream_shadow_list;
296 /* protect shadow lists of buffers passed to firmware */
297 struct mutex shadow_list_lock;
299 struct list_head list;
300 struct completion completion;
306 allegro_channel_get_i_frame_qp(struct allegro_channel *channel)
308 if (channel->codec == V4L2_PIX_FMT_HEVC)
309 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_i_frame_qp);
311 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_i_frame_qp);
315 allegro_channel_get_p_frame_qp(struct allegro_channel *channel)
317 if (channel->codec == V4L2_PIX_FMT_HEVC)
318 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_p_frame_qp);
320 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_p_frame_qp);
324 allegro_channel_get_b_frame_qp(struct allegro_channel *channel)
326 if (channel->codec == V4L2_PIX_FMT_HEVC)
327 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_b_frame_qp);
329 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_b_frame_qp);
333 allegro_channel_get_min_qp(struct allegro_channel *channel)
335 if (channel->codec == V4L2_PIX_FMT_HEVC)
336 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_min_qp);
338 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_min_qp);
342 allegro_channel_get_max_qp(struct allegro_channel *channel)
344 if (channel->codec == V4L2_PIX_FMT_HEVC)
345 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_max_qp);
347 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_max_qp);
350 struct allegro_m2m_buffer {
351 struct v4l2_m2m_buffer buf;
352 struct list_head head;
355 #define to_allegro_m2m_buffer(__buf) \
356 container_of(__buf, struct allegro_m2m_buffer, buf)
360 unsigned int id_codec;
362 unsigned int mailbox_cmd;
363 unsigned int mailbox_status;
365 enum mcu_msg_version mailbox_version;
366 size_t suballocator_size;
369 static const struct fw_info supported_firmware[] = {
373 .version = "v2018.2",
374 .mailbox_cmd = 0x7800,
375 .mailbox_status = 0x7c00,
376 .mailbox_size = 0x400 - 0x8,
377 .mailbox_version = MCU_MSG_VERSION_2018_2,
378 .suballocator_size = SZ_16M,
382 .version = "v2019.2",
383 .mailbox_cmd = 0x7000,
384 .mailbox_status = 0x7800,
385 .mailbox_size = 0x800 - 0x8,
386 .mailbox_version = MCU_MSG_VERSION_2019_2,
387 .suballocator_size = SZ_32M,
391 static inline u32 to_mcu_addr(struct allegro_dev *dev, dma_addr_t phys)
393 if (upper_32_bits(phys) || (lower_32_bits(phys) & MCU_CACHE_OFFSET))
394 v4l2_warn(&dev->v4l2_dev,
395 "address %pad is outside mcu window\n", &phys);
397 return lower_32_bits(phys) | MCU_CACHE_OFFSET;
400 static inline u32 to_mcu_size(struct allegro_dev *dev, size_t size)
402 return lower_32_bits(size);
405 static inline u32 to_codec_addr(struct allegro_dev *dev, dma_addr_t phys)
407 if (upper_32_bits(phys))
408 v4l2_warn(&dev->v4l2_dev,
409 "address %pad cannot be used by codec\n", &phys);
411 return lower_32_bits(phys);
414 static inline u64 ptr_to_u64(const void *ptr)
416 return (uintptr_t)ptr;
419 /* Helper functions for channel and user operations */
421 static unsigned long allegro_next_user_id(struct allegro_dev *dev)
423 if (dev->channel_user_ids == ~0UL)
426 return ffz(dev->channel_user_ids);
429 static struct allegro_channel *
430 allegro_find_channel_by_user_id(struct allegro_dev *dev,
431 unsigned int user_id)
433 struct allegro_channel *channel;
435 list_for_each_entry(channel, &dev->channels, list) {
436 if (channel->user_id == user_id)
440 return ERR_PTR(-EINVAL);
443 static struct allegro_channel *
444 allegro_find_channel_by_channel_id(struct allegro_dev *dev,
445 unsigned int channel_id)
447 struct allegro_channel *channel;
449 list_for_each_entry(channel, &dev->channels, list) {
450 if (channel->mcu_channel_id == channel_id)
454 return ERR_PTR(-EINVAL);
457 static inline bool channel_exists(struct allegro_channel *channel)
459 return channel->mcu_channel_id != -1;
462 #define AL_ERROR 0x80
463 #define AL_ERR_INIT_FAILED 0x81
464 #define AL_ERR_NO_FRAME_DECODED 0x82
465 #define AL_ERR_RESOLUTION_CHANGE 0x85
466 #define AL_ERR_NO_MEMORY 0x87
467 #define AL_ERR_STREAM_OVERFLOW 0x88
468 #define AL_ERR_TOO_MANY_SLICES 0x89
469 #define AL_ERR_BUF_NOT_READY 0x8c
470 #define AL_ERR_NO_CHANNEL_AVAILABLE 0x8d
471 #define AL_ERR_RESOURCE_UNAVAILABLE 0x8e
472 #define AL_ERR_NOT_ENOUGH_CORES 0x8f
473 #define AL_ERR_REQUEST_MALFORMED 0x90
474 #define AL_ERR_CMD_NOT_ALLOWED 0x91
475 #define AL_ERR_INVALID_CMD_VALUE 0x92
477 static inline const char *allegro_err_to_string(unsigned int err)
480 case AL_ERR_INIT_FAILED:
481 return "initialization failed";
482 case AL_ERR_NO_FRAME_DECODED:
483 return "no frame decoded";
484 case AL_ERR_RESOLUTION_CHANGE:
485 return "resolution change";
486 case AL_ERR_NO_MEMORY:
487 return "out of memory";
488 case AL_ERR_STREAM_OVERFLOW:
489 return "stream buffer overflow";
490 case AL_ERR_TOO_MANY_SLICES:
491 return "too many slices";
492 case AL_ERR_BUF_NOT_READY:
493 return "buffer not ready";
494 case AL_ERR_NO_CHANNEL_AVAILABLE:
495 return "no channel available";
496 case AL_ERR_RESOURCE_UNAVAILABLE:
497 return "resource unavailable";
498 case AL_ERR_NOT_ENOUGH_CORES:
499 return "not enough cores";
500 case AL_ERR_REQUEST_MALFORMED:
501 return "request malformed";
502 case AL_ERR_CMD_NOT_ALLOWED:
503 return "command not allowed";
504 case AL_ERR_INVALID_CMD_VALUE:
505 return "invalid command value";
508 return "unknown error";
512 static unsigned int estimate_stream_size(unsigned int width,
515 unsigned int offset = ENCODER_STREAM_OFFSET;
516 unsigned int num_blocks = DIV_ROUND_UP(width, SIZE_MACROBLOCK) *
517 DIV_ROUND_UP(height, SIZE_MACROBLOCK);
518 unsigned int pcm_size = SZ_256;
519 unsigned int partition_table = SZ_256;
521 return round_up(offset + num_blocks * pcm_size + partition_table, 32);
524 static enum v4l2_mpeg_video_h264_level
525 select_minimum_h264_level(unsigned int width, unsigned int height)
527 unsigned int pic_width_in_mb = DIV_ROUND_UP(width, SIZE_MACROBLOCK);
528 unsigned int frame_height_in_mb = DIV_ROUND_UP(height, SIZE_MACROBLOCK);
529 unsigned int frame_size_in_mb = pic_width_in_mb * frame_height_in_mb;
530 enum v4l2_mpeg_video_h264_level level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
533 * The level limits are specified in Rec. ITU-T H.264 Annex A.3.1 and
534 * also specify limits regarding bit rate and CBP size. Only approximate
535 * the levels using the frame size.
537 * Level 5.1 allows up to 4k video resolution.
539 if (frame_size_in_mb <= 99)
540 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
541 else if (frame_size_in_mb <= 396)
542 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
543 else if (frame_size_in_mb <= 792)
544 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
545 else if (frame_size_in_mb <= 1620)
546 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
547 else if (frame_size_in_mb <= 3600)
548 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
549 else if (frame_size_in_mb <= 5120)
550 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
551 else if (frame_size_in_mb <= 8192)
552 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
553 else if (frame_size_in_mb <= 8704)
554 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
555 else if (frame_size_in_mb <= 22080)
556 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
558 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
563 static unsigned int h264_maximum_bitrate(enum v4l2_mpeg_video_h264_level level)
566 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
568 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
570 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
572 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
574 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
576 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
578 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
580 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
582 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
584 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
586 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
588 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
590 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
592 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
594 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
596 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
602 static unsigned int h264_maximum_cpb_size(enum v4l2_mpeg_video_h264_level level)
605 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
607 case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
609 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
611 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
613 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
615 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
617 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
619 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
621 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
623 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
625 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
627 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
629 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
631 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
633 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
635 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
641 static enum v4l2_mpeg_video_hevc_level
642 select_minimum_hevc_level(unsigned int width, unsigned int height)
644 unsigned int luma_picture_size = width * height;
645 enum v4l2_mpeg_video_hevc_level level;
647 if (luma_picture_size <= 36864)
648 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_1;
649 else if (luma_picture_size <= 122880)
650 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_2;
651 else if (luma_picture_size <= 245760)
652 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1;
653 else if (luma_picture_size <= 552960)
654 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_3;
655 else if (luma_picture_size <= 983040)
656 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1;
657 else if (luma_picture_size <= 2228224)
658 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_4;
659 else if (luma_picture_size <= 8912896)
660 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_5;
662 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_6;
667 static unsigned int hevc_maximum_bitrate(enum v4l2_mpeg_video_hevc_level level)
670 * See Rec. ITU-T H.265 v5 (02/2018), A.4.2 Profile-specific level
671 * limits for the video profiles.
674 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
676 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
678 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
680 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
682 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
684 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
686 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
688 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
691 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
696 static unsigned int hevc_maximum_cpb_size(enum v4l2_mpeg_video_hevc_level level)
699 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
701 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
703 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
705 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
707 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
709 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
711 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
713 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
716 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
721 static const struct fw_info *
722 allegro_get_firmware_info(struct allegro_dev *dev,
723 const struct firmware *fw,
724 const struct firmware *fw_codec)
727 unsigned int id = fw->size;
728 unsigned int id_codec = fw_codec->size;
730 for (i = 0; i < ARRAY_SIZE(supported_firmware); i++)
731 if (supported_firmware[i].id == id &&
732 supported_firmware[i].id_codec == id_codec)
733 return &supported_firmware[i];
739 * Buffers that are used internally by the MCU.
742 static int allegro_alloc_buffer(struct allegro_dev *dev,
743 struct allegro_buffer *buffer, size_t size)
745 buffer->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size,
746 &buffer->paddr, GFP_KERNEL);
754 static void allegro_free_buffer(struct allegro_dev *dev,
755 struct allegro_buffer *buffer)
758 dma_free_coherent(&dev->plat_dev->dev, buffer->size,
759 buffer->vaddr, buffer->paddr);
760 buffer->vaddr = NULL;
766 * Mailbox interface to send messages to the MCU.
769 static void allegro_mcu_interrupt(struct allegro_dev *dev);
770 static void allegro_handle_message(struct allegro_dev *dev,
771 union mcu_msg_response *msg);
773 static struct allegro_mbox *allegro_mbox_init(struct allegro_dev *dev,
774 unsigned int base, size_t size)
776 struct allegro_mbox *mbox;
778 mbox = devm_kmalloc(&dev->plat_dev->dev, sizeof(*mbox), GFP_KERNEL);
780 return ERR_PTR(-ENOMEM);
785 mbox->tail = base + 0x4;
786 mbox->data = base + 0x8;
788 mutex_init(&mbox->lock);
790 regmap_write(dev->sram, mbox->head, 0);
791 regmap_write(dev->sram, mbox->tail, 0);
796 static int allegro_mbox_write(struct allegro_mbox *mbox,
797 const u32 *src, size_t size)
799 struct regmap *sram = mbox->dev->sram;
803 int stride = regmap_get_reg_stride(sram);
808 if (size > mbox->size)
811 mutex_lock(&mbox->lock);
812 regmap_read(sram, mbox->tail, &tail);
813 if (tail > mbox->size) {
817 size_no_wrap = min(size, mbox->size - (size_t)tail);
818 regmap_bulk_write(sram, mbox->data + tail,
819 src, size_no_wrap / stride);
820 regmap_bulk_write(sram, mbox->data,
821 src + (size_no_wrap / sizeof(*src)),
822 (size - size_no_wrap) / stride);
823 regmap_write(sram, mbox->tail, (tail + size) % mbox->size);
826 mutex_unlock(&mbox->lock);
831 static ssize_t allegro_mbox_read(struct allegro_mbox *mbox,
832 u32 *dst, size_t nbyte)
837 } __attribute__ ((__packed__)) *header;
838 struct regmap *sram = mbox->dev->sram;
842 int stride = regmap_get_reg_stride(sram);
844 regmap_read(sram, mbox->head, &head);
845 if (head > mbox->size)
848 /* Assume that the header does not wrap. */
849 regmap_bulk_read(sram, mbox->data + head,
850 dst, sizeof(*header) / stride);
851 header = (void *)dst;
852 size = header->length + sizeof(*header);
853 if (size > mbox->size || size & 0x3)
859 * The message might wrap within the mailbox. If the message does not
860 * wrap, the first read will read the entire message, otherwise the
861 * first read will read message until the end of the mailbox and the
862 * second read will read the remaining bytes from the beginning of the
865 * Skip the header, as was already read to get the size of the body.
867 body_no_wrap = min((size_t)header->length,
868 (size_t)(mbox->size - (head + sizeof(*header))));
869 regmap_bulk_read(sram, mbox->data + head + sizeof(*header),
870 dst + (sizeof(*header) / sizeof(*dst)),
871 body_no_wrap / stride);
872 regmap_bulk_read(sram, mbox->data,
873 dst + (sizeof(*header) + body_no_wrap) / sizeof(*dst),
874 (header->length - body_no_wrap) / stride);
876 regmap_write(sram, mbox->head, (head + size) % mbox->size);
882 * allegro_mbox_send() - Send a message via the mailbox
883 * @mbox: the mailbox which is used to send the message
884 * @msg: the message to send
886 static int allegro_mbox_send(struct allegro_mbox *mbox, void *msg)
888 struct allegro_dev *dev = mbox->dev;
893 tmp = kzalloc(mbox->size, GFP_KERNEL);
899 size = allegro_encode_mail(tmp, msg);
901 err = allegro_mbox_write(mbox, tmp, size);
906 allegro_mcu_interrupt(dev);
913 * allegro_mbox_notify() - Notify the mailbox about a new message
914 * @mbox: The allegro_mbox to notify
916 static void allegro_mbox_notify(struct allegro_mbox *mbox)
918 struct allegro_dev *dev = mbox->dev;
919 union mcu_msg_response *msg;
924 msg = kmalloc(sizeof(*msg), GFP_KERNEL);
928 msg->header.version = dev->fw_info->mailbox_version;
930 tmp = kmalloc(mbox->size, GFP_KERNEL);
934 size = allegro_mbox_read(mbox, tmp, mbox->size);
938 err = allegro_decode_mail(msg, tmp);
942 allegro_handle_message(dev, msg);
949 static int allegro_encoder_buffer_init(struct allegro_dev *dev,
950 struct allegro_encoder_buffer *buffer)
953 struct regmap *settings = dev->settings;
954 unsigned int supports_10_bit;
955 unsigned int memory_depth;
956 unsigned int num_cores;
957 unsigned int color_depth;
958 unsigned long clk_rate;
960 /* We don't support the encoder buffer pre Firmware version 2019.2 */
961 if (dev->fw_info->mailbox_version < MCU_MSG_VERSION_2019_2)
967 err = regmap_read(settings, VCU_ENC_COLOR_DEPTH, &supports_10_bit);
970 err = regmap_read(settings, VCU_MEMORY_DEPTH, &memory_depth);
973 err = regmap_read(settings, VCU_NUM_CORE, &num_cores);
977 clk_rate = clk_get_rate(dev->clk_core);
981 color_depth = supports_10_bit ? 10 : 8;
982 /* The firmware expects the encoder buffer size in bits. */
983 buffer->size = color_depth * 32 * memory_depth;
984 buffer->color_depth = color_depth;
985 buffer->num_cores = num_cores;
986 buffer->clk_rate = clk_rate;
988 v4l2_dbg(1, debug, &dev->v4l2_dev,
989 "using %d bits encoder buffer with %d-bit color depth\n",
990 buffer->size, color_depth);
995 static void allegro_mcu_send_init(struct allegro_dev *dev,
996 dma_addr_t suballoc_dma, size_t suballoc_size)
998 struct mcu_msg_init_request msg;
1000 memset(&msg, 0, sizeof(msg));
1002 msg.header.type = MCU_MSG_TYPE_INIT;
1003 msg.header.version = dev->fw_info->mailbox_version;
1005 msg.suballoc_dma = to_mcu_addr(dev, suballoc_dma);
1006 msg.suballoc_size = to_mcu_size(dev, suballoc_size);
1008 if (dev->has_encoder_buffer) {
1009 msg.encoder_buffer_size = dev->encoder_buffer.size;
1010 msg.encoder_buffer_color_depth = dev->encoder_buffer.color_depth;
1011 msg.num_cores = dev->encoder_buffer.num_cores;
1012 msg.clk_rate = dev->encoder_buffer.clk_rate;
1014 msg.encoder_buffer_size = -1;
1015 msg.encoder_buffer_color_depth = -1;
1020 allegro_mbox_send(dev->mbox_command, &msg);
1023 static u32 v4l2_pixelformat_to_mcu_format(u32 pixelformat)
1025 switch (pixelformat) {
1026 case V4L2_PIX_FMT_NV12:
1027 /* AL_420_8BITS: 0x100 -> NV12, 0x88 -> 8 bit */
1028 return 0x100 | 0x88;
1034 static u32 v4l2_colorspace_to_mcu_colorspace(enum v4l2_colorspace colorspace)
1036 switch (colorspace) {
1037 case V4L2_COLORSPACE_REC709:
1039 case V4L2_COLORSPACE_SMPTE170M:
1041 case V4L2_COLORSPACE_SMPTE240M:
1043 case V4L2_COLORSPACE_SRGB:
1051 static u8 v4l2_profile_to_mcu_profile(enum v4l2_mpeg_video_h264_profile profile)
1054 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
1060 static u16 v4l2_level_to_mcu_level(enum v4l2_mpeg_video_h264_level level)
1063 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
1065 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
1067 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
1069 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
1071 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
1073 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
1075 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
1077 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
1079 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
1081 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
1083 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
1085 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1:
1087 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
1089 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
1091 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
1097 static u8 hevc_profile_to_mcu_profile(enum v4l2_mpeg_video_hevc_profile profile)
1101 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN:
1103 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10:
1105 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE:
1110 static u16 hevc_level_to_mcu_level(enum v4l2_mpeg_video_hevc_level level)
1113 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1:
1115 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2:
1117 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1:
1119 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3:
1121 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1:
1123 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4:
1125 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1:
1127 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5:
1130 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1:
1135 static u8 hevc_tier_to_mcu_tier(enum v4l2_mpeg_video_hevc_tier tier)
1139 case V4L2_MPEG_VIDEO_HEVC_TIER_MAIN:
1141 case V4L2_MPEG_VIDEO_HEVC_TIER_HIGH:
1147 v4l2_bitrate_mode_to_mcu_mode(enum v4l2_mpeg_video_bitrate_mode mode)
1150 case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
1152 case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
1158 static u32 v4l2_cpb_size_to_mcu(unsigned int cpb_size, unsigned int bitrate)
1160 unsigned int cpb_size_kbit;
1161 unsigned int bitrate_kbps;
1164 * The mcu expects the CPB size in units of a 90 kHz clock, but the
1165 * channel follows the V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE and stores
1166 * the CPB size in kilobytes.
1168 cpb_size_kbit = cpb_size * BITS_PER_BYTE;
1169 bitrate_kbps = bitrate / 1000;
1171 return (cpb_size_kbit * 90000) / bitrate_kbps;
1174 static s16 get_qp_delta(int minuend, int subtrahend)
1176 if (minuend == subtrahend)
1179 return minuend - subtrahend;
1182 static u32 allegro_channel_get_entropy_mode(struct allegro_channel *channel)
1184 #define ALLEGRO_ENTROPY_MODE_CAVLC 0
1185 #define ALLEGRO_ENTROPY_MODE_CABAC 1
1187 /* HEVC always uses CABAC, but this has to be explicitly set */
1188 if (channel->codec == V4L2_PIX_FMT_HEVC)
1189 return ALLEGRO_ENTROPY_MODE_CABAC;
1191 return ALLEGRO_ENTROPY_MODE_CAVLC;
1194 static int fill_create_channel_param(struct allegro_channel *channel,
1195 struct create_channel_param *param)
1197 int i_frame_qp = allegro_channel_get_i_frame_qp(channel);
1198 int p_frame_qp = allegro_channel_get_p_frame_qp(channel);
1199 int b_frame_qp = allegro_channel_get_b_frame_qp(channel);
1200 int bitrate_mode = v4l2_ctrl_g_ctrl(channel->mpeg_video_bitrate_mode);
1201 unsigned int cpb_size = v4l2_ctrl_g_ctrl(channel->mpeg_video_cpb_size);
1203 param->width = channel->width;
1204 param->height = channel->height;
1205 param->format = v4l2_pixelformat_to_mcu_format(channel->pixelformat);
1207 v4l2_colorspace_to_mcu_colorspace(channel->colorspace);
1208 param->src_mode = 0x0;
1210 param->codec = channel->codec;
1211 if (channel->codec == V4L2_PIX_FMT_H264) {
1212 enum v4l2_mpeg_video_h264_profile profile;
1213 enum v4l2_mpeg_video_h264_level level;
1215 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_profile);
1216 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_level);
1218 param->profile = v4l2_profile_to_mcu_profile(profile);
1219 param->constraint_set_flags = BIT(1);
1220 param->level = v4l2_level_to_mcu_level(level);
1222 enum v4l2_mpeg_video_hevc_profile profile;
1223 enum v4l2_mpeg_video_hevc_level level;
1224 enum v4l2_mpeg_video_hevc_tier tier;
1226 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_profile);
1227 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level);
1228 tier = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_tier);
1230 param->profile = hevc_profile_to_mcu_profile(profile);
1231 param->level = hevc_level_to_mcu_level(level);
1232 param->tier = hevc_tier_to_mcu_tier(tier);
1235 param->log2_max_poc = LOG2_MAX_PIC_ORDER_CNT;
1236 param->log2_max_frame_num = channel->log2_max_frame_num;
1237 param->temporal_mvp_enable = channel->temporal_mvp_enable;
1239 param->dbf_ovr_en = channel->dbf_ovr_en;
1240 param->override_lf = channel->enable_deblocking_filter_override;
1241 param->enable_reordering = channel->enable_reordering;
1242 param->entropy_mode = allegro_channel_get_entropy_mode(channel);
1243 param->rdo_cost_mode = 1;
1244 param->custom_lda = 1;
1246 param->lf_x_tile = channel->enable_loop_filter_across_tiles;
1247 param->lf_x_slice = channel->enable_loop_filter_across_slices;
1249 param->src_bit_depth = 8;
1251 param->beta_offset = BETA_OFFSET_DIV_2;
1252 param->tc_offset = TC_OFFSET_DIV_2;
1253 param->num_slices = 1;
1254 param->me_range[0] = channel->b_hrz_me_range;
1255 param->me_range[1] = channel->b_vrt_me_range;
1256 param->me_range[2] = channel->p_hrz_me_range;
1257 param->me_range[3] = channel->p_vrt_me_range;
1258 param->max_cu_size = channel->max_cu_size;
1259 param->min_cu_size = channel->min_cu_size;
1260 param->max_tu_size = channel->max_tu_size;
1261 param->min_tu_size = channel->min_tu_size;
1262 param->max_transfo_depth_intra = channel->max_transfo_depth_intra;
1263 param->max_transfo_depth_inter = channel->max_transfo_depth_inter;
1265 param->encoder_buffer_enabled = v4l2_ctrl_g_ctrl(channel->encoder_buffer);
1266 param->encoder_buffer_offset = 0;
1268 param->rate_control_mode = channel->frame_rc_enable ?
1269 v4l2_bitrate_mode_to_mcu_mode(bitrate_mode) : 0;
1271 param->cpb_size = v4l2_cpb_size_to_mcu(cpb_size, channel->bitrate_peak);
1272 /* Shall be ]0;cpb_size in 90 kHz units]. Use maximum value. */
1273 param->initial_rem_delay = param->cpb_size;
1274 param->framerate = DIV_ROUND_UP(channel->framerate.numerator,
1275 channel->framerate.denominator);
1276 param->clk_ratio = channel->framerate.denominator == 1001 ? 1001 : 1000;
1277 param->target_bitrate = channel->bitrate;
1278 param->max_bitrate = channel->bitrate_peak;
1279 param->initial_qp = i_frame_qp;
1280 param->min_qp = allegro_channel_get_min_qp(channel);
1281 param->max_qp = allegro_channel_get_max_qp(channel);
1282 param->ip_delta = get_qp_delta(i_frame_qp, p_frame_qp);
1283 param->pb_delta = get_qp_delta(p_frame_qp, b_frame_qp);
1284 param->golden_ref = 0;
1285 param->golden_delta = 2;
1286 param->golden_ref_frequency = 10;
1287 param->rate_control_option = 0x00000000;
1289 param->num_pixel = channel->width + channel->height;
1290 param->max_psnr = 4200;
1291 param->max_pixel_value = 255;
1293 param->gop_ctrl_mode = 0x00000002;
1294 param->freq_idr = v4l2_ctrl_g_ctrl(channel->mpeg_video_gop_size);
1296 param->gdr_mode = 0x00000000;
1297 param->gop_length = v4l2_ctrl_g_ctrl(channel->mpeg_video_gop_size);
1298 param->subframe_latency = 0x00000000;
1300 param->lda_factors[0] = 51;
1301 param->lda_factors[1] = 90;
1302 param->lda_factors[2] = 151;
1303 param->lda_factors[3] = 151;
1304 param->lda_factors[4] = 151;
1305 param->lda_factors[5] = 151;
1307 param->max_num_merge_cand = 5;
1312 static int allegro_mcu_send_create_channel(struct allegro_dev *dev,
1313 struct allegro_channel *channel)
1315 struct mcu_msg_create_channel msg;
1316 struct allegro_buffer *blob = &channel->config_blob;
1317 struct create_channel_param param;
1320 memset(¶m, 0, sizeof(param));
1321 fill_create_channel_param(channel, ¶m);
1322 allegro_alloc_buffer(dev, blob, sizeof(struct create_channel_param));
1323 param.version = dev->fw_info->mailbox_version;
1324 size = allegro_encode_config_blob(blob->vaddr, ¶m);
1326 memset(&msg, 0, sizeof(msg));
1328 msg.header.type = MCU_MSG_TYPE_CREATE_CHANNEL;
1329 msg.header.version = dev->fw_info->mailbox_version;
1331 msg.user_id = channel->user_id;
1333 msg.blob = blob->vaddr;
1334 msg.blob_size = size;
1335 msg.blob_mcu_addr = to_mcu_addr(dev, blob->paddr);
1337 allegro_mbox_send(dev->mbox_command, &msg);
1342 static int allegro_mcu_send_destroy_channel(struct allegro_dev *dev,
1343 struct allegro_channel *channel)
1345 struct mcu_msg_destroy_channel msg;
1347 memset(&msg, 0, sizeof(msg));
1349 msg.header.type = MCU_MSG_TYPE_DESTROY_CHANNEL;
1350 msg.header.version = dev->fw_info->mailbox_version;
1352 msg.channel_id = channel->mcu_channel_id;
1354 allegro_mbox_send(dev->mbox_command, &msg);
1359 static int allegro_mcu_send_put_stream_buffer(struct allegro_dev *dev,
1360 struct allegro_channel *channel,
1365 struct mcu_msg_put_stream_buffer msg;
1367 memset(&msg, 0, sizeof(msg));
1369 msg.header.type = MCU_MSG_TYPE_PUT_STREAM_BUFFER;
1370 msg.header.version = dev->fw_info->mailbox_version;
1372 msg.channel_id = channel->mcu_channel_id;
1373 msg.dma_addr = to_codec_addr(dev, paddr);
1374 msg.mcu_addr = to_mcu_addr(dev, paddr);
1376 msg.offset = ENCODER_STREAM_OFFSET;
1377 /* copied to mcu_msg_encode_frame_response */
1378 msg.dst_handle = dst_handle;
1380 allegro_mbox_send(dev->mbox_command, &msg);
1385 static int allegro_mcu_send_encode_frame(struct allegro_dev *dev,
1386 struct allegro_channel *channel,
1387 dma_addr_t src_y, dma_addr_t src_uv,
1390 struct mcu_msg_encode_frame msg;
1391 bool use_encoder_buffer = v4l2_ctrl_g_ctrl(channel->encoder_buffer);
1393 memset(&msg, 0, sizeof(msg));
1395 msg.header.type = MCU_MSG_TYPE_ENCODE_FRAME;
1396 msg.header.version = dev->fw_info->mailbox_version;
1398 msg.channel_id = channel->mcu_channel_id;
1399 msg.encoding_options = AL_OPT_FORCE_LOAD;
1400 if (use_encoder_buffer)
1401 msg.encoding_options |= AL_OPT_USE_L2;
1402 msg.pps_qp = 26; /* qp are relative to 26 */
1403 msg.user_param = 0; /* copied to mcu_msg_encode_frame_response */
1404 /* src_handle is copied to mcu_msg_encode_frame_response */
1405 msg.src_handle = src_handle;
1406 msg.src_y = to_codec_addr(dev, src_y);
1407 msg.src_uv = to_codec_addr(dev, src_uv);
1408 msg.stride = channel->stride;
1410 allegro_mbox_send(dev->mbox_command, &msg);
1415 static int allegro_mcu_wait_for_init_timeout(struct allegro_dev *dev,
1416 unsigned long timeout_ms)
1418 unsigned long time_left;
1420 time_left = wait_for_completion_timeout(&dev->init_complete,
1421 msecs_to_jiffies(timeout_ms));
1425 reinit_completion(&dev->init_complete);
1429 static int allegro_mcu_push_buffer_internal(struct allegro_channel *channel,
1430 enum mcu_msg_type type)
1432 struct allegro_dev *dev = channel->dev;
1433 struct mcu_msg_push_buffers_internal *msg;
1434 struct mcu_msg_push_buffers_internal_buffer *buffer;
1435 unsigned int num_buffers = 0;
1437 struct allegro_buffer *al_buffer;
1438 struct list_head *list;
1442 case MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE:
1443 list = &channel->buffers_reference;
1445 case MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE:
1446 list = &channel->buffers_intermediate;
1452 list_for_each_entry(al_buffer, list, head)
1454 size = struct_size(msg, buffer, num_buffers);
1456 msg = kmalloc(size, GFP_KERNEL);
1460 msg->header.type = type;
1461 msg->header.version = dev->fw_info->mailbox_version;
1463 msg->channel_id = channel->mcu_channel_id;
1464 msg->num_buffers = num_buffers;
1466 buffer = msg->buffer;
1467 list_for_each_entry(al_buffer, list, head) {
1468 buffer->dma_addr = to_codec_addr(dev, al_buffer->paddr);
1469 buffer->mcu_addr = to_mcu_addr(dev, al_buffer->paddr);
1470 buffer->size = to_mcu_size(dev, al_buffer->size);
1474 err = allegro_mbox_send(dev->mbox_command, msg);
1480 static int allegro_mcu_push_buffer_intermediate(struct allegro_channel *channel)
1482 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE;
1484 return allegro_mcu_push_buffer_internal(channel, type);
1487 static int allegro_mcu_push_buffer_reference(struct allegro_channel *channel)
1489 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE;
1491 return allegro_mcu_push_buffer_internal(channel, type);
1494 static int allocate_buffers_internal(struct allegro_channel *channel,
1495 struct list_head *list,
1496 size_t n, size_t size)
1498 struct allegro_dev *dev = channel->dev;
1501 struct allegro_buffer *buffer, *tmp;
1503 for (i = 0; i < n; i++) {
1504 buffer = kmalloc(sizeof(*buffer), GFP_KERNEL);
1509 INIT_LIST_HEAD(&buffer->head);
1511 err = allegro_alloc_buffer(dev, buffer, size);
1516 list_add(&buffer->head, list);
1522 list_for_each_entry_safe(buffer, tmp, list, head) {
1523 list_del(&buffer->head);
1524 allegro_free_buffer(dev, buffer);
1530 static void destroy_buffers_internal(struct allegro_channel *channel,
1531 struct list_head *list)
1533 struct allegro_dev *dev = channel->dev;
1534 struct allegro_buffer *buffer, *tmp;
1536 list_for_each_entry_safe(buffer, tmp, list, head) {
1537 list_del(&buffer->head);
1538 allegro_free_buffer(dev, buffer);
1543 static void destroy_reference_buffers(struct allegro_channel *channel)
1545 return destroy_buffers_internal(channel, &channel->buffers_reference);
1548 static void destroy_intermediate_buffers(struct allegro_channel *channel)
1550 return destroy_buffers_internal(channel,
1551 &channel->buffers_intermediate);
1554 static int allocate_intermediate_buffers(struct allegro_channel *channel,
1555 size_t n, size_t size)
1557 return allocate_buffers_internal(channel,
1558 &channel->buffers_intermediate,
1562 static int allocate_reference_buffers(struct allegro_channel *channel,
1563 size_t n, size_t size)
1565 return allocate_buffers_internal(channel,
1566 &channel->buffers_reference,
1567 n, PAGE_ALIGN(size));
1570 static ssize_t allegro_h264_write_sps(struct allegro_channel *channel,
1571 void *dest, size_t n)
1573 struct allegro_dev *dev = channel->dev;
1574 struct nal_h264_sps *sps;
1576 unsigned int size_mb = SIZE_MACROBLOCK;
1577 /* Calculation of crop units in Rec. ITU-T H.264 (04/2017) p. 76 */
1578 unsigned int crop_unit_x = 2;
1579 unsigned int crop_unit_y = 2;
1580 enum v4l2_mpeg_video_h264_profile profile;
1581 enum v4l2_mpeg_video_h264_level level;
1582 unsigned int cpb_size;
1583 unsigned int cpb_size_scale;
1585 sps = kzalloc(sizeof(*sps), GFP_KERNEL);
1589 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_profile);
1590 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_level);
1592 sps->profile_idc = nal_h264_profile(profile);
1593 sps->constraint_set0_flag = 0;
1594 sps->constraint_set1_flag = 1;
1595 sps->constraint_set2_flag = 0;
1596 sps->constraint_set3_flag = 0;
1597 sps->constraint_set4_flag = 0;
1598 sps->constraint_set5_flag = 0;
1599 sps->level_idc = nal_h264_level(level);
1600 sps->seq_parameter_set_id = 0;
1601 sps->log2_max_frame_num_minus4 = LOG2_MAX_FRAME_NUM - 4;
1602 sps->pic_order_cnt_type = 0;
1603 sps->log2_max_pic_order_cnt_lsb_minus4 = LOG2_MAX_PIC_ORDER_CNT - 4;
1604 sps->max_num_ref_frames = 3;
1605 sps->gaps_in_frame_num_value_allowed_flag = 0;
1606 sps->pic_width_in_mbs_minus1 =
1607 DIV_ROUND_UP(channel->width, size_mb) - 1;
1608 sps->pic_height_in_map_units_minus1 =
1609 DIV_ROUND_UP(channel->height, size_mb) - 1;
1610 sps->frame_mbs_only_flag = 1;
1611 sps->mb_adaptive_frame_field_flag = 0;
1612 sps->direct_8x8_inference_flag = 1;
1613 sps->frame_cropping_flag =
1614 (channel->width % size_mb) || (channel->height % size_mb);
1615 if (sps->frame_cropping_flag) {
1617 sps->crop_right = (round_up(channel->width, size_mb) - channel->width) / crop_unit_x;
1619 sps->crop_bottom = (round_up(channel->height, size_mb) - channel->height) / crop_unit_y;
1621 sps->vui_parameters_present_flag = 1;
1622 sps->vui.aspect_ratio_info_present_flag = 0;
1623 sps->vui.overscan_info_present_flag = 0;
1625 sps->vui.video_signal_type_present_flag = 1;
1626 sps->vui.video_format = 5; /* unspecified */
1627 sps->vui.video_full_range_flag = nal_h264_full_range(channel->quantization);
1628 sps->vui.colour_description_present_flag = 1;
1629 sps->vui.colour_primaries = nal_h264_color_primaries(channel->colorspace);
1630 sps->vui.transfer_characteristics =
1631 nal_h264_transfer_characteristics(channel->colorspace, channel->xfer_func);
1632 sps->vui.matrix_coefficients =
1633 nal_h264_matrix_coeffs(channel->colorspace, channel->ycbcr_enc);
1635 sps->vui.chroma_loc_info_present_flag = 1;
1636 sps->vui.chroma_sample_loc_type_top_field = 0;
1637 sps->vui.chroma_sample_loc_type_bottom_field = 0;
1639 sps->vui.timing_info_present_flag = 1;
1640 sps->vui.num_units_in_tick = channel->framerate.denominator;
1641 sps->vui.time_scale = 2 * channel->framerate.numerator;
1643 sps->vui.fixed_frame_rate_flag = 1;
1644 sps->vui.nal_hrd_parameters_present_flag = 0;
1645 sps->vui.vcl_hrd_parameters_present_flag = 1;
1646 sps->vui.vcl_hrd_parameters.cpb_cnt_minus1 = 0;
1647 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-53 */
1648 sps->vui.vcl_hrd_parameters.bit_rate_scale =
1649 ffs(channel->bitrate_peak) - 6;
1650 sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] =
1651 channel->bitrate_peak / (1 << (6 + sps->vui.vcl_hrd_parameters.bit_rate_scale)) - 1;
1652 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-54 */
1653 cpb_size = v4l2_ctrl_g_ctrl(channel->mpeg_video_cpb_size);
1654 cpb_size_scale = ffs(cpb_size) - 4;
1655 sps->vui.vcl_hrd_parameters.cpb_size_scale = cpb_size_scale;
1656 sps->vui.vcl_hrd_parameters.cpb_size_value_minus1[0] =
1657 (cpb_size * 1000) / (1 << (4 + cpb_size_scale)) - 1;
1658 sps->vui.vcl_hrd_parameters.cbr_flag[0] =
1659 !v4l2_ctrl_g_ctrl(channel->mpeg_video_frame_rc_enable);
1660 sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 31;
1661 sps->vui.vcl_hrd_parameters.cpb_removal_delay_length_minus1 = 31;
1662 sps->vui.vcl_hrd_parameters.dpb_output_delay_length_minus1 = 31;
1663 sps->vui.vcl_hrd_parameters.time_offset_length = 0;
1664 sps->vui.low_delay_hrd_flag = 0;
1665 sps->vui.pic_struct_present_flag = 1;
1666 sps->vui.bitstream_restriction_flag = 0;
1668 size = nal_h264_write_sps(&dev->plat_dev->dev, dest, n, sps);
1675 static ssize_t allegro_h264_write_pps(struct allegro_channel *channel,
1676 void *dest, size_t n)
1678 struct allegro_dev *dev = channel->dev;
1679 struct nal_h264_pps *pps;
1682 pps = kzalloc(sizeof(*pps), GFP_KERNEL);
1686 pps->pic_parameter_set_id = 0;
1687 pps->seq_parameter_set_id = 0;
1688 pps->entropy_coding_mode_flag = 0;
1689 pps->bottom_field_pic_order_in_frame_present_flag = 0;
1690 pps->num_slice_groups_minus1 = 0;
1691 pps->num_ref_idx_l0_default_active_minus1 = channel->num_ref_idx_l0 - 1;
1692 pps->num_ref_idx_l1_default_active_minus1 = channel->num_ref_idx_l1 - 1;
1693 pps->weighted_pred_flag = 0;
1694 pps->weighted_bipred_idc = 0;
1695 pps->pic_init_qp_minus26 = 0;
1696 pps->pic_init_qs_minus26 = 0;
1697 pps->chroma_qp_index_offset = 0;
1698 pps->deblocking_filter_control_present_flag = 1;
1699 pps->constrained_intra_pred_flag = 0;
1700 pps->redundant_pic_cnt_present_flag = 0;
1701 pps->transform_8x8_mode_flag = 0;
1702 pps->pic_scaling_matrix_present_flag = 0;
1703 pps->second_chroma_qp_index_offset = 0;
1705 size = nal_h264_write_pps(&dev->plat_dev->dev, dest, n, pps);
1712 static void allegro_channel_eos_event(struct allegro_channel *channel)
1714 const struct v4l2_event eos_event = {
1715 .type = V4L2_EVENT_EOS
1718 v4l2_event_queue_fh(&channel->fh, &eos_event);
1721 static ssize_t allegro_hevc_write_vps(struct allegro_channel *channel,
1722 void *dest, size_t n)
1724 struct allegro_dev *dev = channel->dev;
1725 struct nal_hevc_vps *vps;
1726 struct nal_hevc_profile_tier_level *ptl;
1728 unsigned int num_ref_frames = channel->num_ref_idx_l0;
1729 s32 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_profile);
1730 s32 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level);
1731 s32 tier = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_tier);
1733 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
1737 vps->base_layer_internal_flag = 1;
1738 vps->base_layer_available_flag = 1;
1739 vps->temporal_id_nesting_flag = 1;
1741 ptl = &vps->profile_tier_level;
1742 ptl->general_profile_idc = nal_hevc_profile(profile);
1743 ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1;
1744 ptl->general_tier_flag = nal_hevc_tier(tier);
1745 ptl->general_progressive_source_flag = 1;
1746 ptl->general_frame_only_constraint_flag = 1;
1747 ptl->general_level_idc = nal_hevc_level(level);
1749 vps->sub_layer_ordering_info_present_flag = 0;
1750 vps->max_dec_pic_buffering_minus1[0] = num_ref_frames;
1751 vps->max_num_reorder_pics[0] = num_ref_frames;
1753 size = nal_hevc_write_vps(&dev->plat_dev->dev, dest, n, vps);
1760 static ssize_t allegro_hevc_write_sps(struct allegro_channel *channel,
1761 void *dest, size_t n)
1763 struct allegro_dev *dev = channel->dev;
1764 struct nal_hevc_sps *sps;
1765 struct nal_hevc_profile_tier_level *ptl;
1766 struct nal_hevc_vui_parameters *vui;
1767 struct nal_hevc_hrd_parameters *hrd;
1769 unsigned int cpb_size;
1770 unsigned int num_ref_frames = channel->num_ref_idx_l0;
1771 s32 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_profile);
1772 s32 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level);
1773 s32 tier = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_tier);
1775 sps = kzalloc(sizeof(*sps), GFP_KERNEL);
1779 sps->temporal_id_nesting_flag = 1;
1781 ptl = &sps->profile_tier_level;
1782 ptl->general_profile_idc = nal_hevc_profile(profile);
1783 ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1;
1784 ptl->general_tier_flag = nal_hevc_tier(tier);
1785 ptl->general_progressive_source_flag = 1;
1786 ptl->general_frame_only_constraint_flag = 1;
1787 ptl->general_level_idc = nal_hevc_level(level);
1789 sps->seq_parameter_set_id = 0;
1790 sps->chroma_format_idc = 1; /* Only 4:2:0 sampling supported */
1791 sps->pic_width_in_luma_samples = round_up(channel->width, 8);
1792 sps->pic_height_in_luma_samples = round_up(channel->height, 8);
1793 sps->conf_win_right_offset =
1794 sps->pic_width_in_luma_samples - channel->width;
1795 sps->conf_win_bottom_offset =
1796 sps->pic_height_in_luma_samples - channel->height;
1797 sps->conformance_window_flag =
1798 sps->conf_win_right_offset || sps->conf_win_bottom_offset;
1800 sps->log2_max_pic_order_cnt_lsb_minus4 = LOG2_MAX_PIC_ORDER_CNT - 4;
1802 sps->sub_layer_ordering_info_present_flag = 1;
1803 sps->max_dec_pic_buffering_minus1[0] = num_ref_frames;
1804 sps->max_num_reorder_pics[0] = num_ref_frames;
1806 sps->log2_min_luma_coding_block_size_minus3 =
1807 channel->min_cu_size - 3;
1808 sps->log2_diff_max_min_luma_coding_block_size =
1809 channel->max_cu_size - channel->min_cu_size;
1810 sps->log2_min_luma_transform_block_size_minus2 =
1811 channel->min_tu_size - 2;
1812 sps->log2_diff_max_min_luma_transform_block_size =
1813 channel->max_tu_size - channel->min_tu_size;
1814 sps->max_transform_hierarchy_depth_intra =
1815 channel->max_transfo_depth_intra;
1816 sps->max_transform_hierarchy_depth_inter =
1817 channel->max_transfo_depth_inter;
1819 sps->sps_temporal_mvp_enabled_flag = channel->temporal_mvp_enable;
1820 sps->strong_intra_smoothing_enabled_flag = channel->max_cu_size > 4;
1822 sps->vui_parameters_present_flag = 1;
1825 vui->video_signal_type_present_flag = 1;
1826 vui->video_format = 5; /* unspecified */
1827 vui->video_full_range_flag = nal_hevc_full_range(channel->quantization);
1828 vui->colour_description_present_flag = 1;
1829 vui->colour_primaries = nal_hevc_color_primaries(channel->colorspace);
1830 vui->transfer_characteristics = nal_hevc_transfer_characteristics(channel->colorspace,
1831 channel->xfer_func);
1832 vui->matrix_coeffs = nal_hevc_matrix_coeffs(channel->colorspace, channel->ycbcr_enc);
1834 vui->chroma_loc_info_present_flag = 1;
1835 vui->chroma_sample_loc_type_top_field = 0;
1836 vui->chroma_sample_loc_type_bottom_field = 0;
1838 vui->vui_timing_info_present_flag = 1;
1839 vui->vui_num_units_in_tick = channel->framerate.denominator;
1840 vui->vui_time_scale = channel->framerate.numerator;
1842 vui->bitstream_restriction_flag = 1;
1843 vui->motion_vectors_over_pic_boundaries_flag = 1;
1844 vui->restricted_ref_pic_lists_flag = 1;
1845 vui->log2_max_mv_length_horizontal = 15;
1846 vui->log2_max_mv_length_vertical = 15;
1848 vui->vui_hrd_parameters_present_flag = 1;
1849 hrd = &vui->nal_hrd_parameters;
1850 hrd->vcl_hrd_parameters_present_flag = 1;
1852 hrd->initial_cpb_removal_delay_length_minus1 = 31;
1853 hrd->au_cpb_removal_delay_length_minus1 = 30;
1854 hrd->dpb_output_delay_length_minus1 = 30;
1856 hrd->bit_rate_scale = ffs(channel->bitrate_peak) - 6;
1857 hrd->vcl_hrd[0].bit_rate_value_minus1[0] =
1858 (channel->bitrate_peak >> (6 + hrd->bit_rate_scale)) - 1;
1860 cpb_size = v4l2_ctrl_g_ctrl(channel->mpeg_video_cpb_size) * 1000;
1861 hrd->cpb_size_scale = ffs(cpb_size) - 4;
1862 hrd->vcl_hrd[0].cpb_size_value_minus1[0] = (cpb_size >> (4 + hrd->cpb_size_scale)) - 1;
1864 hrd->vcl_hrd[0].cbr_flag[0] = !v4l2_ctrl_g_ctrl(channel->mpeg_video_frame_rc_enable);
1866 size = nal_hevc_write_sps(&dev->plat_dev->dev, dest, n, sps);
1873 static ssize_t allegro_hevc_write_pps(struct allegro_channel *channel,
1874 struct mcu_msg_encode_frame_response *msg,
1875 void *dest, size_t n)
1877 struct allegro_dev *dev = channel->dev;
1878 struct nal_hevc_pps *pps;
1882 pps = kzalloc(sizeof(*pps), GFP_KERNEL);
1886 pps->pps_pic_parameter_set_id = 0;
1887 pps->pps_seq_parameter_set_id = 0;
1889 if (msg->num_column > 1 || msg->num_row > 1) {
1890 pps->tiles_enabled_flag = 1;
1891 pps->num_tile_columns_minus1 = msg->num_column - 1;
1892 pps->num_tile_rows_minus1 = msg->num_row - 1;
1894 for (i = 0; i < msg->num_column; i++)
1895 pps->column_width_minus1[i] = msg->tile_width[i] - 1;
1897 for (i = 0; i < msg->num_row; i++)
1898 pps->row_height_minus1[i] = msg->tile_height[i] - 1;
1901 pps->loop_filter_across_tiles_enabled_flag =
1902 channel->enable_loop_filter_across_tiles;
1903 pps->pps_loop_filter_across_slices_enabled_flag =
1904 channel->enable_loop_filter_across_slices;
1905 pps->deblocking_filter_control_present_flag = 1;
1906 pps->deblocking_filter_override_enabled_flag =
1907 channel->enable_deblocking_filter_override;
1908 pps->pps_beta_offset_div2 = BETA_OFFSET_DIV_2;
1909 pps->pps_tc_offset_div2 = TC_OFFSET_DIV_2;
1911 pps->lists_modification_present_flag = channel->enable_reordering;
1913 size = nal_hevc_write_pps(&dev->plat_dev->dev, dest, n, pps);
1920 static u64 allegro_put_buffer(struct allegro_channel *channel,
1921 struct list_head *list,
1922 struct vb2_v4l2_buffer *buffer)
1924 struct v4l2_m2m_buffer *b = container_of(buffer,
1925 struct v4l2_m2m_buffer, vb);
1926 struct allegro_m2m_buffer *shadow = to_allegro_m2m_buffer(b);
1928 mutex_lock(&channel->shadow_list_lock);
1929 list_add_tail(&shadow->head, list);
1930 mutex_unlock(&channel->shadow_list_lock);
1932 return ptr_to_u64(buffer);
1935 static struct vb2_v4l2_buffer *
1936 allegro_get_buffer(struct allegro_channel *channel,
1937 struct list_head *list, u64 handle)
1939 struct allegro_m2m_buffer *shadow, *tmp;
1940 struct vb2_v4l2_buffer *buffer = NULL;
1942 mutex_lock(&channel->shadow_list_lock);
1943 list_for_each_entry_safe(shadow, tmp, list, head) {
1944 if (handle == ptr_to_u64(&shadow->buf.vb)) {
1945 buffer = &shadow->buf.vb;
1946 list_del_init(&shadow->head);
1950 mutex_unlock(&channel->shadow_list_lock);
1955 static void allegro_channel_finish_frame(struct allegro_channel *channel,
1956 struct mcu_msg_encode_frame_response *msg)
1958 struct allegro_dev *dev = channel->dev;
1959 struct vb2_v4l2_buffer *src_buf;
1960 struct vb2_v4l2_buffer *dst_buf;
1965 enum vb2_buffer_state state = VB2_BUF_STATE_ERROR;
1970 src_buf = allegro_get_buffer(channel, &channel->source_shadow_list,
1973 v4l2_warn(&dev->v4l2_dev,
1974 "channel %d: invalid source buffer\n",
1975 channel->mcu_channel_id);
1977 dst_buf = allegro_get_buffer(channel, &channel->stream_shadow_list,
1980 v4l2_warn(&dev->v4l2_dev,
1981 "channel %d: invalid stream buffer\n",
1982 channel->mcu_channel_id);
1984 if (!src_buf || !dst_buf)
1987 if (v4l2_m2m_is_last_draining_src_buf(channel->fh.m2m_ctx, src_buf)) {
1988 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
1989 allegro_channel_eos_event(channel);
1990 v4l2_m2m_mark_stopped(channel->fh.m2m_ctx);
1993 dst_buf->sequence = channel->csequence++;
1995 if (msg->error_code & AL_ERROR) {
1996 v4l2_err(&dev->v4l2_dev,
1997 "channel %d: failed to encode frame: %s (%x)\n",
1998 channel->mcu_channel_id,
1999 allegro_err_to_string(msg->error_code),
2004 if (msg->partition_table_size != 1) {
2005 v4l2_warn(&dev->v4l2_dev,
2006 "channel %d: only handling first partition table entry (%d entries)\n",
2007 channel->mcu_channel_id, msg->partition_table_size);
2010 if (msg->partition_table_offset +
2011 msg->partition_table_size * sizeof(*partition) >
2012 vb2_plane_size(&dst_buf->vb2_buf, 0)) {
2013 v4l2_err(&dev->v4l2_dev,
2014 "channel %d: partition table outside of dst_buf\n",
2015 channel->mcu_channel_id);
2020 vb2_plane_vaddr(&dst_buf->vb2_buf, 0) + msg->partition_table_offset;
2021 if (partition->offset + partition->size >
2022 vb2_plane_size(&dst_buf->vb2_buf, 0)) {
2023 v4l2_err(&dev->v4l2_dev,
2024 "channel %d: encoded frame is outside of dst_buf (offset 0x%x, size 0x%x)\n",
2025 channel->mcu_channel_id, partition->offset,
2030 v4l2_dbg(2, debug, &dev->v4l2_dev,
2031 "channel %d: encoded frame of size %d is at offset 0x%x\n",
2032 channel->mcu_channel_id, partition->size, partition->offset);
2035 * The payload must include the data before the partition offset,
2036 * because we will put the sps and pps data there.
2038 vb2_set_plane_payload(&dst_buf->vb2_buf, 0,
2039 partition->offset + partition->size);
2041 curr = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
2042 free = partition->offset;
2044 if (channel->codec == V4L2_PIX_FMT_HEVC && msg->is_idr) {
2045 len = allegro_hevc_write_vps(channel, curr, free);
2047 v4l2_err(&dev->v4l2_dev,
2048 "not enough space for video parameter set: %zd left\n",
2054 v4l2_dbg(1, debug, &dev->v4l2_dev,
2055 "channel %d: wrote %zd byte VPS nal unit\n",
2056 channel->mcu_channel_id, len);
2060 if (channel->codec == V4L2_PIX_FMT_H264)
2061 len = allegro_h264_write_sps(channel, curr, free);
2063 len = allegro_hevc_write_sps(channel, curr, free);
2065 v4l2_err(&dev->v4l2_dev,
2066 "not enough space for sequence parameter set: %zd left\n",
2072 v4l2_dbg(1, debug, &dev->v4l2_dev,
2073 "channel %d: wrote %zd byte SPS nal unit\n",
2074 channel->mcu_channel_id, len);
2077 if (msg->slice_type == AL_ENC_SLICE_TYPE_I) {
2078 if (channel->codec == V4L2_PIX_FMT_H264)
2079 len = allegro_h264_write_pps(channel, curr, free);
2081 len = allegro_hevc_write_pps(channel, msg, curr, free);
2083 v4l2_err(&dev->v4l2_dev,
2084 "not enough space for picture parameter set: %zd left\n",
2090 v4l2_dbg(1, debug, &dev->v4l2_dev,
2091 "channel %d: wrote %zd byte PPS nal unit\n",
2092 channel->mcu_channel_id, len);
2095 if (msg->slice_type != AL_ENC_SLICE_TYPE_I && !msg->is_idr) {
2096 dst_buf->vb2_buf.planes[0].data_offset = free;
2099 if (channel->codec == V4L2_PIX_FMT_H264)
2100 len = nal_h264_write_filler(&dev->plat_dev->dev, curr, free);
2102 len = nal_hevc_write_filler(&dev->plat_dev->dev, curr, free);
2104 v4l2_err(&dev->v4l2_dev,
2105 "failed to write %zd filler data\n", free);
2110 v4l2_dbg(2, debug, &dev->v4l2_dev,
2111 "channel %d: wrote %zd bytes filler nal unit\n",
2112 channel->mcu_channel_id, len);
2116 v4l2_err(&dev->v4l2_dev,
2117 "non-VCL NAL units do not fill space until VCL NAL unit: %zd bytes left\n",
2122 state = VB2_BUF_STATE_DONE;
2124 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false);
2126 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
2128 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME;
2130 v4l2_dbg(1, debug, &dev->v4l2_dev,
2131 "channel %d: encoded frame #%03d (%s%s, QP %d, %d bytes)\n",
2132 channel->mcu_channel_id,
2134 msg->is_idr ? "IDR, " : "",
2135 msg->slice_type == AL_ENC_SLICE_TYPE_I ? "I slice" :
2136 msg->slice_type == AL_ENC_SLICE_TYPE_P ? "P slice" : "unknown",
2137 msg->qp, partition->size);
2141 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
2144 v4l2_m2m_buf_done(dst_buf, state);
2147 static int allegro_handle_init(struct allegro_dev *dev,
2148 struct mcu_msg_init_response *msg)
2150 complete(&dev->init_complete);
2156 allegro_handle_create_channel(struct allegro_dev *dev,
2157 struct mcu_msg_create_channel_response *msg)
2159 struct allegro_channel *channel;
2161 struct create_channel_param param;
2163 channel = allegro_find_channel_by_user_id(dev, msg->user_id);
2164 if (IS_ERR(channel)) {
2165 v4l2_warn(&dev->v4l2_dev,
2166 "received %s for unknown user %d\n",
2167 msg_type_name(msg->header.type),
2172 if (msg->error_code) {
2173 v4l2_err(&dev->v4l2_dev,
2174 "user %d: mcu failed to create channel: %s (%x)\n",
2176 allegro_err_to_string(msg->error_code),
2182 channel->mcu_channel_id = msg->channel_id;
2183 v4l2_dbg(1, debug, &dev->v4l2_dev,
2184 "user %d: channel has channel id %d\n",
2185 channel->user_id, channel->mcu_channel_id);
2187 err = allegro_decode_config_blob(¶m, msg, channel->config_blob.vaddr);
2188 allegro_free_buffer(channel->dev, &channel->config_blob);
2192 channel->num_ref_idx_l0 = param.num_ref_idx_l0;
2193 channel->num_ref_idx_l1 = param.num_ref_idx_l1;
2195 v4l2_dbg(1, debug, &dev->v4l2_dev,
2196 "channel %d: intermediate buffers: %d x %d bytes\n",
2197 channel->mcu_channel_id,
2198 msg->int_buffers_count, msg->int_buffers_size);
2199 err = allocate_intermediate_buffers(channel, msg->int_buffers_count,
2200 msg->int_buffers_size);
2202 v4l2_err(&dev->v4l2_dev,
2203 "channel %d: failed to allocate intermediate buffers\n",
2204 channel->mcu_channel_id);
2207 err = allegro_mcu_push_buffer_intermediate(channel);
2211 v4l2_dbg(1, debug, &dev->v4l2_dev,
2212 "channel %d: reference buffers: %d x %d bytes\n",
2213 channel->mcu_channel_id,
2214 msg->rec_buffers_count, msg->rec_buffers_size);
2215 err = allocate_reference_buffers(channel, msg->rec_buffers_count,
2216 msg->rec_buffers_size);
2218 v4l2_err(&dev->v4l2_dev,
2219 "channel %d: failed to allocate reference buffers\n",
2220 channel->mcu_channel_id);
2223 err = allegro_mcu_push_buffer_reference(channel);
2228 channel->error = err;
2229 complete(&channel->completion);
2231 /* Handled successfully, error is passed via channel->error */
2236 allegro_handle_destroy_channel(struct allegro_dev *dev,
2237 struct mcu_msg_destroy_channel_response *msg)
2239 struct allegro_channel *channel;
2241 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
2242 if (IS_ERR(channel)) {
2243 v4l2_err(&dev->v4l2_dev,
2244 "received %s for unknown channel %d\n",
2245 msg_type_name(msg->header.type),
2250 v4l2_dbg(2, debug, &dev->v4l2_dev,
2251 "user %d: vcu destroyed channel %d\n",
2252 channel->user_id, channel->mcu_channel_id);
2253 complete(&channel->completion);
2259 allegro_handle_encode_frame(struct allegro_dev *dev,
2260 struct mcu_msg_encode_frame_response *msg)
2262 struct allegro_channel *channel;
2264 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id);
2265 if (IS_ERR(channel)) {
2266 v4l2_err(&dev->v4l2_dev,
2267 "received %s for unknown channel %d\n",
2268 msg_type_name(msg->header.type),
2273 allegro_channel_finish_frame(channel, msg);
2278 static void allegro_handle_message(struct allegro_dev *dev,
2279 union mcu_msg_response *msg)
2281 switch (msg->header.type) {
2282 case MCU_MSG_TYPE_INIT:
2283 allegro_handle_init(dev, &msg->init);
2285 case MCU_MSG_TYPE_CREATE_CHANNEL:
2286 allegro_handle_create_channel(dev, &msg->create_channel);
2288 case MCU_MSG_TYPE_DESTROY_CHANNEL:
2289 allegro_handle_destroy_channel(dev, &msg->destroy_channel);
2291 case MCU_MSG_TYPE_ENCODE_FRAME:
2292 allegro_handle_encode_frame(dev, &msg->encode_frame);
2295 v4l2_warn(&dev->v4l2_dev,
2296 "%s: unknown message %s\n",
2297 __func__, msg_type_name(msg->header.type));
2302 static irqreturn_t allegro_hardirq(int irq, void *data)
2304 struct allegro_dev *dev = data;
2305 unsigned int status;
2307 regmap_read(dev->regmap, AL5_ITC_CPU_IRQ_STA, &status);
2308 if (!(status & AL5_ITC_CPU_IRQ_STA_TRIGGERED))
2311 regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_CLR, status);
2313 return IRQ_WAKE_THREAD;
2316 static irqreturn_t allegro_irq_thread(int irq, void *data)
2318 struct allegro_dev *dev = data;
2321 * The firmware is initialized after the mailbox is setup. We further
2322 * check the AL5_ITC_CPU_IRQ_STA register, if the firmware actually
2323 * triggered the interrupt. Although this should not happen, make sure
2324 * that we ignore interrupts, if the mailbox is not initialized.
2326 if (!dev->mbox_status)
2329 allegro_mbox_notify(dev->mbox_status);
2334 static void allegro_copy_firmware(struct allegro_dev *dev,
2335 const u8 * const buf, size_t size)
2339 v4l2_dbg(1, debug, &dev->v4l2_dev,
2340 "copy mcu firmware (%zu B) to SRAM\n", size);
2341 err = regmap_bulk_write(dev->sram, 0x0, buf, size / 4);
2343 v4l2_err(&dev->v4l2_dev,
2344 "failed to copy firmware: %d\n", err);
2347 static void allegro_copy_fw_codec(struct allegro_dev *dev,
2348 const u8 * const buf, size_t size)
2351 dma_addr_t icache_offset, dcache_offset;
2354 * The downstream allocates 600 KB for the codec firmware to have some
2355 * extra space for "possible extensions." My tests were fine with
2356 * allocating just enough memory for the actual firmware, but I am not
2357 * sure that the firmware really does not use the remaining space.
2359 err = allegro_alloc_buffer(dev, &dev->firmware, size);
2361 v4l2_err(&dev->v4l2_dev,
2362 "failed to allocate %zu bytes for firmware\n", size);
2366 v4l2_dbg(1, debug, &dev->v4l2_dev,
2367 "copy codec firmware (%zd B) to phys %pad\n",
2368 size, &dev->firmware.paddr);
2369 memcpy(dev->firmware.vaddr, buf, size);
2371 regmap_write(dev->regmap, AXI_ADDR_OFFSET_IP,
2372 upper_32_bits(dev->firmware.paddr));
2374 icache_offset = dev->firmware.paddr - MCU_CACHE_OFFSET;
2375 v4l2_dbg(2, debug, &dev->v4l2_dev,
2376 "icache_offset: msb = 0x%x, lsb = 0x%x\n",
2377 upper_32_bits(icache_offset), lower_32_bits(icache_offset));
2378 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_MSB,
2379 upper_32_bits(icache_offset));
2380 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_LSB,
2381 lower_32_bits(icache_offset));
2384 (dev->firmware.paddr & 0xffffffff00000000ULL) - MCU_CACHE_OFFSET;
2385 v4l2_dbg(2, debug, &dev->v4l2_dev,
2386 "dcache_offset: msb = 0x%x, lsb = 0x%x\n",
2387 upper_32_bits(dcache_offset), lower_32_bits(dcache_offset));
2388 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_MSB,
2389 upper_32_bits(dcache_offset));
2390 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_LSB,
2391 lower_32_bits(dcache_offset));
2394 static void allegro_free_fw_codec(struct allegro_dev *dev)
2396 allegro_free_buffer(dev, &dev->firmware);
2400 * Control functions for the MCU
2403 static int allegro_mcu_enable_interrupts(struct allegro_dev *dev)
2405 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, BIT(0));
2408 static int allegro_mcu_disable_interrupts(struct allegro_dev *dev)
2410 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, 0);
2413 static int allegro_mcu_wait_for_sleep(struct allegro_dev *dev)
2415 unsigned long timeout;
2416 unsigned int status;
2418 timeout = jiffies + msecs_to_jiffies(100);
2419 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
2420 status != AL5_MCU_STA_SLEEP) {
2421 if (time_after(jiffies, timeout))
2429 static int allegro_mcu_start(struct allegro_dev *dev)
2431 unsigned long timeout;
2432 unsigned int status;
2435 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, BIT(0));
2439 timeout = jiffies + msecs_to_jiffies(100);
2440 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 &&
2441 status == AL5_MCU_STA_SLEEP) {
2442 if (time_after(jiffies, timeout))
2447 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
2454 static int allegro_mcu_reset(struct allegro_dev *dev)
2459 * Ensure that the AL5_MCU_WAKEUP bit is set to 0 otherwise the mcu
2460 * does not go to sleep after the reset.
2462 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0);
2466 err = regmap_write(dev->regmap,
2467 AL5_MCU_RESET_MODE, AL5_MCU_RESET_MODE_SLEEP);
2471 err = regmap_write(dev->regmap, AL5_MCU_RESET, AL5_MCU_RESET_SOFT);
2475 return allegro_mcu_wait_for_sleep(dev);
2478 static void allegro_mcu_interrupt(struct allegro_dev *dev)
2480 regmap_write(dev->regmap, AL5_MCU_INTERRUPT, BIT(0));
2483 static void allegro_destroy_channel(struct allegro_channel *channel)
2485 struct allegro_dev *dev = channel->dev;
2486 unsigned long time_left;
2488 if (channel_exists(channel)) {
2489 reinit_completion(&channel->completion);
2490 allegro_mcu_send_destroy_channel(dev, channel);
2491 time_left = wait_for_completion_timeout(&channel->completion,
2492 msecs_to_jiffies(5000));
2494 v4l2_warn(&dev->v4l2_dev,
2495 "channel %d: timeout while destroying\n",
2496 channel->mcu_channel_id);
2498 channel->mcu_channel_id = -1;
2501 destroy_intermediate_buffers(channel);
2502 destroy_reference_buffers(channel);
2504 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, false);
2505 v4l2_ctrl_grab(channel->mpeg_video_h264_level, false);
2506 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, false);
2507 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, false);
2508 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, false);
2509 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, false);
2510 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, false);
2512 v4l2_ctrl_grab(channel->mpeg_video_hevc_profile, false);
2513 v4l2_ctrl_grab(channel->mpeg_video_hevc_level, false);
2514 v4l2_ctrl_grab(channel->mpeg_video_hevc_tier, false);
2515 v4l2_ctrl_grab(channel->mpeg_video_hevc_i_frame_qp, false);
2516 v4l2_ctrl_grab(channel->mpeg_video_hevc_max_qp, false);
2517 v4l2_ctrl_grab(channel->mpeg_video_hevc_min_qp, false);
2518 v4l2_ctrl_grab(channel->mpeg_video_hevc_p_frame_qp, false);
2519 v4l2_ctrl_grab(channel->mpeg_video_hevc_b_frame_qp, false);
2521 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, false);
2522 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, false);
2523 v4l2_ctrl_grab(channel->mpeg_video_bitrate, false);
2524 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, false);
2525 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, false);
2526 v4l2_ctrl_grab(channel->mpeg_video_gop_size, false);
2528 v4l2_ctrl_grab(channel->encoder_buffer, false);
2530 if (channel->user_id != -1) {
2531 clear_bit(channel->user_id, &dev->channel_user_ids);
2532 channel->user_id = -1;
2537 * Create the MCU channel
2539 * After the channel has been created, the picture size, format, colorspace
2540 * and framerate are fixed. Also the codec, profile, bitrate, etc. cannot be
2543 * The channel can be created only once. The MCU will accept source buffers
2544 * and stream buffers only after a channel has been created.
2546 static int allegro_create_channel(struct allegro_channel *channel)
2548 struct allegro_dev *dev = channel->dev;
2549 unsigned long time_left;
2551 if (channel_exists(channel)) {
2552 v4l2_warn(&dev->v4l2_dev,
2553 "channel already exists\n");
2557 channel->user_id = allegro_next_user_id(dev);
2558 if (channel->user_id < 0) {
2559 v4l2_err(&dev->v4l2_dev,
2560 "no free channels available\n");
2563 set_bit(channel->user_id, &dev->channel_user_ids);
2565 v4l2_dbg(1, debug, &dev->v4l2_dev,
2566 "user %d: creating channel (%4.4s, %dx%d@%d)\n",
2568 (char *)&channel->codec, channel->width, channel->height,
2569 DIV_ROUND_UP(channel->framerate.numerator,
2570 channel->framerate.denominator));
2572 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, true);
2573 v4l2_ctrl_grab(channel->mpeg_video_h264_level, true);
2574 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, true);
2575 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, true);
2576 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, true);
2577 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, true);
2578 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, true);
2580 v4l2_ctrl_grab(channel->mpeg_video_hevc_profile, true);
2581 v4l2_ctrl_grab(channel->mpeg_video_hevc_level, true);
2582 v4l2_ctrl_grab(channel->mpeg_video_hevc_tier, true);
2583 v4l2_ctrl_grab(channel->mpeg_video_hevc_i_frame_qp, true);
2584 v4l2_ctrl_grab(channel->mpeg_video_hevc_max_qp, true);
2585 v4l2_ctrl_grab(channel->mpeg_video_hevc_min_qp, true);
2586 v4l2_ctrl_grab(channel->mpeg_video_hevc_p_frame_qp, true);
2587 v4l2_ctrl_grab(channel->mpeg_video_hevc_b_frame_qp, true);
2589 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, true);
2590 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, true);
2591 v4l2_ctrl_grab(channel->mpeg_video_bitrate, true);
2592 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, true);
2593 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, true);
2594 v4l2_ctrl_grab(channel->mpeg_video_gop_size, true);
2596 v4l2_ctrl_grab(channel->encoder_buffer, true);
2598 reinit_completion(&channel->completion);
2599 allegro_mcu_send_create_channel(dev, channel);
2600 time_left = wait_for_completion_timeout(&channel->completion,
2601 msecs_to_jiffies(5000));
2603 channel->error = -ETIMEDOUT;
2607 v4l2_dbg(1, debug, &dev->v4l2_dev,
2608 "channel %d: accepting buffers\n",
2609 channel->mcu_channel_id);
2614 allegro_destroy_channel(channel);
2616 return channel->error;
2620 * allegro_channel_adjust() - Adjust channel parameters to current format
2621 * @channel: the channel to adjust
2623 * Various parameters of a channel and their limits depend on the currently
2624 * set format. Adjust the parameters after a format change in one go.
2626 static void allegro_channel_adjust(struct allegro_channel *channel)
2628 struct allegro_dev *dev = channel->dev;
2629 u32 codec = channel->codec;
2630 struct v4l2_ctrl *ctrl;
2634 channel->sizeimage_encoded =
2635 estimate_stream_size(channel->width, channel->height);
2637 if (codec == V4L2_PIX_FMT_H264) {
2638 ctrl = channel->mpeg_video_h264_level;
2639 min = select_minimum_h264_level(channel->width, channel->height);
2641 ctrl = channel->mpeg_video_hevc_level;
2642 min = select_minimum_hevc_level(channel->width, channel->height);
2644 if (ctrl->minimum > min)
2645 v4l2_dbg(1, debug, &dev->v4l2_dev,
2646 "%s.minimum: %lld -> %lld\n",
2647 v4l2_ctrl_get_name(ctrl->id), ctrl->minimum, min);
2648 v4l2_ctrl_lock(ctrl);
2649 __v4l2_ctrl_modify_range(ctrl, min, ctrl->maximum,
2650 ctrl->step, ctrl->default_value);
2651 v4l2_ctrl_unlock(ctrl);
2653 ctrl = channel->mpeg_video_bitrate;
2654 if (codec == V4L2_PIX_FMT_H264)
2655 max = h264_maximum_bitrate(v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_level));
2657 max = hevc_maximum_bitrate(v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level));
2658 if (ctrl->maximum < max)
2659 v4l2_dbg(1, debug, &dev->v4l2_dev,
2660 "%s: maximum: %lld -> %lld\n",
2661 v4l2_ctrl_get_name(ctrl->id), ctrl->maximum, max);
2662 v4l2_ctrl_lock(ctrl);
2663 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max,
2664 ctrl->step, ctrl->default_value);
2665 v4l2_ctrl_unlock(ctrl);
2667 ctrl = channel->mpeg_video_bitrate_peak;
2668 v4l2_ctrl_lock(ctrl);
2669 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max,
2670 ctrl->step, ctrl->default_value);
2671 v4l2_ctrl_unlock(ctrl);
2673 v4l2_ctrl_activate(channel->mpeg_video_h264_profile,
2674 codec == V4L2_PIX_FMT_H264);
2675 v4l2_ctrl_activate(channel->mpeg_video_h264_level,
2676 codec == V4L2_PIX_FMT_H264);
2677 v4l2_ctrl_activate(channel->mpeg_video_h264_i_frame_qp,
2678 codec == V4L2_PIX_FMT_H264);
2679 v4l2_ctrl_activate(channel->mpeg_video_h264_max_qp,
2680 codec == V4L2_PIX_FMT_H264);
2681 v4l2_ctrl_activate(channel->mpeg_video_h264_min_qp,
2682 codec == V4L2_PIX_FMT_H264);
2683 v4l2_ctrl_activate(channel->mpeg_video_h264_p_frame_qp,
2684 codec == V4L2_PIX_FMT_H264);
2685 v4l2_ctrl_activate(channel->mpeg_video_h264_b_frame_qp,
2686 codec == V4L2_PIX_FMT_H264);
2688 v4l2_ctrl_activate(channel->mpeg_video_hevc_profile,
2689 codec == V4L2_PIX_FMT_HEVC);
2690 v4l2_ctrl_activate(channel->mpeg_video_hevc_level,
2691 codec == V4L2_PIX_FMT_HEVC);
2692 v4l2_ctrl_activate(channel->mpeg_video_hevc_tier,
2693 codec == V4L2_PIX_FMT_HEVC);
2694 v4l2_ctrl_activate(channel->mpeg_video_hevc_i_frame_qp,
2695 codec == V4L2_PIX_FMT_HEVC);
2696 v4l2_ctrl_activate(channel->mpeg_video_hevc_max_qp,
2697 codec == V4L2_PIX_FMT_HEVC);
2698 v4l2_ctrl_activate(channel->mpeg_video_hevc_min_qp,
2699 codec == V4L2_PIX_FMT_HEVC);
2700 v4l2_ctrl_activate(channel->mpeg_video_hevc_p_frame_qp,
2701 codec == V4L2_PIX_FMT_HEVC);
2702 v4l2_ctrl_activate(channel->mpeg_video_hevc_b_frame_qp,
2703 codec == V4L2_PIX_FMT_HEVC);
2705 if (codec == V4L2_PIX_FMT_H264)
2706 channel->log2_max_frame_num = LOG2_MAX_FRAME_NUM;
2707 channel->temporal_mvp_enable = true;
2708 channel->dbf_ovr_en = (codec == V4L2_PIX_FMT_H264);
2709 channel->enable_deblocking_filter_override = (codec == V4L2_PIX_FMT_HEVC);
2710 channel->enable_reordering = (codec == V4L2_PIX_FMT_HEVC);
2711 channel->enable_loop_filter_across_tiles = true;
2712 channel->enable_loop_filter_across_slices = true;
2714 if (codec == V4L2_PIX_FMT_H264) {
2715 channel->b_hrz_me_range = 8;
2716 channel->b_vrt_me_range = 8;
2717 channel->p_hrz_me_range = 16;
2718 channel->p_vrt_me_range = 16;
2719 channel->max_cu_size = ilog2(16);
2720 channel->min_cu_size = ilog2(8);
2721 channel->max_tu_size = ilog2(4);
2722 channel->min_tu_size = ilog2(4);
2724 channel->b_hrz_me_range = 16;
2725 channel->b_vrt_me_range = 16;
2726 channel->p_hrz_me_range = 32;
2727 channel->p_vrt_me_range = 32;
2728 channel->max_cu_size = ilog2(32);
2729 channel->min_cu_size = ilog2(8);
2730 channel->max_tu_size = ilog2(32);
2731 channel->min_tu_size = ilog2(4);
2733 channel->max_transfo_depth_intra = 1;
2734 channel->max_transfo_depth_inter = 1;
2737 static void allegro_set_default_params(struct allegro_channel *channel)
2739 channel->width = ALLEGRO_WIDTH_DEFAULT;
2740 channel->height = ALLEGRO_HEIGHT_DEFAULT;
2741 channel->stride = round_up(channel->width, 32);
2742 channel->framerate = ALLEGRO_FRAMERATE_DEFAULT;
2744 channel->colorspace = V4L2_COLORSPACE_REC709;
2745 channel->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
2746 channel->quantization = V4L2_QUANTIZATION_DEFAULT;
2747 channel->xfer_func = V4L2_XFER_FUNC_DEFAULT;
2749 channel->pixelformat = V4L2_PIX_FMT_NV12;
2750 channel->sizeimage_raw = channel->stride * channel->height * 3 / 2;
2752 channel->codec = V4L2_PIX_FMT_H264;
2755 static int allegro_queue_setup(struct vb2_queue *vq,
2756 unsigned int *nbuffers, unsigned int *nplanes,
2757 unsigned int sizes[],
2758 struct device *alloc_devs[])
2760 struct allegro_channel *channel = vb2_get_drv_priv(vq);
2761 struct allegro_dev *dev = channel->dev;
2763 v4l2_dbg(2, debug, &dev->v4l2_dev,
2764 "%s: queue setup[%s]: nplanes = %d\n",
2765 V4L2_TYPE_IS_OUTPUT(vq->type) ? "output" : "capture",
2766 *nplanes == 0 ? "REQBUFS" : "CREATE_BUFS", *nplanes);
2768 if (*nplanes != 0) {
2769 if (V4L2_TYPE_IS_OUTPUT(vq->type)) {
2770 if (sizes[0] < channel->sizeimage_raw)
2773 if (sizes[0] < channel->sizeimage_encoded)
2778 if (V4L2_TYPE_IS_OUTPUT(vq->type))
2779 sizes[0] = channel->sizeimage_raw;
2781 sizes[0] = channel->sizeimage_encoded;
2787 static int allegro_buf_prepare(struct vb2_buffer *vb)
2789 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2790 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2791 struct allegro_dev *dev = channel->dev;
2793 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2794 if (vbuf->field == V4L2_FIELD_ANY)
2795 vbuf->field = V4L2_FIELD_NONE;
2796 if (vbuf->field != V4L2_FIELD_NONE) {
2797 v4l2_err(&dev->v4l2_dev,
2798 "channel %d: unsupported field\n",
2799 channel->mcu_channel_id);
2807 static void allegro_buf_queue(struct vb2_buffer *vb)
2809 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue);
2810 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
2811 struct vb2_queue *q = vb->vb2_queue;
2813 if (V4L2_TYPE_IS_CAPTURE(q->type) &&
2814 vb2_is_streaming(q) &&
2815 v4l2_m2m_dst_buf_is_last(channel->fh.m2m_ctx)) {
2818 for (i = 0; i < vb->num_planes; i++)
2819 vb2_set_plane_payload(vb, i, 0);
2821 vbuf->field = V4L2_FIELD_NONE;
2822 vbuf->sequence = channel->csequence++;
2824 v4l2_m2m_last_buffer_done(channel->fh.m2m_ctx, vbuf);
2825 allegro_channel_eos_event(channel);
2829 v4l2_m2m_buf_queue(channel->fh.m2m_ctx, vbuf);
2832 static int allegro_start_streaming(struct vb2_queue *q, unsigned int count)
2834 struct allegro_channel *channel = vb2_get_drv_priv(q);
2835 struct allegro_dev *dev = channel->dev;
2837 v4l2_dbg(2, debug, &dev->v4l2_dev,
2838 "%s: start streaming\n",
2839 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2841 v4l2_m2m_update_start_streaming_state(channel->fh.m2m_ctx, q);
2843 if (V4L2_TYPE_IS_OUTPUT(q->type))
2844 channel->osequence = 0;
2846 channel->csequence = 0;
2851 static void allegro_stop_streaming(struct vb2_queue *q)
2853 struct allegro_channel *channel = vb2_get_drv_priv(q);
2854 struct allegro_dev *dev = channel->dev;
2855 struct vb2_v4l2_buffer *buffer;
2856 struct allegro_m2m_buffer *shadow, *tmp;
2858 v4l2_dbg(2, debug, &dev->v4l2_dev,
2859 "%s: stop streaming\n",
2860 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture");
2862 if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2863 mutex_lock(&channel->shadow_list_lock);
2864 list_for_each_entry_safe(shadow, tmp,
2865 &channel->source_shadow_list, head) {
2866 list_del(&shadow->head);
2867 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR);
2869 mutex_unlock(&channel->shadow_list_lock);
2871 while ((buffer = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx)))
2872 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2874 mutex_lock(&channel->shadow_list_lock);
2875 list_for_each_entry_safe(shadow, tmp,
2876 &channel->stream_shadow_list, head) {
2877 list_del(&shadow->head);
2878 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR);
2880 mutex_unlock(&channel->shadow_list_lock);
2882 allegro_destroy_channel(channel);
2883 while ((buffer = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx)))
2884 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR);
2887 v4l2_m2m_update_stop_streaming_state(channel->fh.m2m_ctx, q);
2889 if (V4L2_TYPE_IS_OUTPUT(q->type) &&
2890 v4l2_m2m_has_stopped(channel->fh.m2m_ctx))
2891 allegro_channel_eos_event(channel);
2894 static const struct vb2_ops allegro_queue_ops = {
2895 .queue_setup = allegro_queue_setup,
2896 .buf_prepare = allegro_buf_prepare,
2897 .buf_queue = allegro_buf_queue,
2898 .start_streaming = allegro_start_streaming,
2899 .stop_streaming = allegro_stop_streaming,
2902 static int allegro_queue_init(void *priv,
2903 struct vb2_queue *src_vq,
2904 struct vb2_queue *dst_vq)
2907 struct allegro_channel *channel = priv;
2909 src_vq->dev = &channel->dev->plat_dev->dev;
2910 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
2911 src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2912 src_vq->mem_ops = &vb2_dma_contig_memops;
2913 src_vq->drv_priv = channel;
2914 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2915 src_vq->ops = &allegro_queue_ops;
2916 src_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer);
2917 src_vq->lock = &channel->dev->lock;
2918 err = vb2_queue_init(src_vq);
2922 dst_vq->dev = &channel->dev->plat_dev->dev;
2923 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2924 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
2925 dst_vq->mem_ops = &vb2_dma_contig_memops;
2926 dst_vq->drv_priv = channel;
2927 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2928 dst_vq->ops = &allegro_queue_ops;
2929 dst_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer);
2930 dst_vq->lock = &channel->dev->lock;
2931 err = vb2_queue_init(dst_vq);
2938 static int allegro_clamp_qp(struct allegro_channel *channel,
2939 struct v4l2_ctrl *ctrl)
2941 struct v4l2_ctrl *next_ctrl;
2943 if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP)
2944 next_ctrl = channel->mpeg_video_h264_p_frame_qp;
2945 else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP)
2946 next_ctrl = channel->mpeg_video_h264_b_frame_qp;
2950 /* Modify range automatically updates the value */
2951 __v4l2_ctrl_modify_range(next_ctrl, ctrl->val, 51, 1, ctrl->val);
2953 return allegro_clamp_qp(channel, next_ctrl);
2956 static int allegro_clamp_bitrate(struct allegro_channel *channel,
2957 struct v4l2_ctrl *ctrl)
2959 struct v4l2_ctrl *ctrl_bitrate = channel->mpeg_video_bitrate;
2960 struct v4l2_ctrl *ctrl_bitrate_peak = channel->mpeg_video_bitrate_peak;
2962 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
2963 ctrl_bitrate_peak->val < ctrl_bitrate->val)
2964 ctrl_bitrate_peak->val = ctrl_bitrate->val;
2969 static int allegro_try_ctrl(struct v4l2_ctrl *ctrl)
2971 struct allegro_channel *channel = container_of(ctrl->handler,
2972 struct allegro_channel,
2976 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
2977 allegro_clamp_bitrate(channel, ctrl);
2979 case V4L2_CID_USER_ALLEGRO_ENCODER_BUFFER:
2980 if (!channel->dev->has_encoder_buffer)
2988 static int allegro_s_ctrl(struct v4l2_ctrl *ctrl)
2990 struct allegro_channel *channel = container_of(ctrl->handler,
2991 struct allegro_channel,
2993 struct allegro_dev *dev = channel->dev;
2995 v4l2_dbg(1, debug, &dev->v4l2_dev,
2996 "s_ctrl: %s = %d\n", v4l2_ctrl_get_name(ctrl->id), ctrl->val);
2999 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
3000 channel->frame_rc_enable = ctrl->val;
3002 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
3003 channel->bitrate = channel->mpeg_video_bitrate->val;
3004 channel->bitrate_peak = channel->mpeg_video_bitrate_peak->val;
3005 v4l2_ctrl_activate(channel->mpeg_video_bitrate_peak,
3006 ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
3008 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
3009 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
3010 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
3011 allegro_clamp_qp(channel, ctrl);
3018 static const struct v4l2_ctrl_ops allegro_ctrl_ops = {
3019 .try_ctrl = allegro_try_ctrl,
3020 .s_ctrl = allegro_s_ctrl,
3023 static const struct v4l2_ctrl_config allegro_encoder_buffer_ctrl_config = {
3024 .id = V4L2_CID_USER_ALLEGRO_ENCODER_BUFFER,
3025 .name = "Encoder Buffer Enable",
3026 .type = V4L2_CTRL_TYPE_BOOLEAN,
3033 static int allegro_open(struct file *file)
3035 struct video_device *vdev = video_devdata(file);
3036 struct allegro_dev *dev = video_get_drvdata(vdev);
3037 struct allegro_channel *channel = NULL;
3038 struct v4l2_ctrl_handler *handler;
3041 unsigned int bitrate_max;
3042 unsigned int bitrate_def;
3043 unsigned int cpb_size_max;
3044 unsigned int cpb_size_def;
3046 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
3050 v4l2_fh_init(&channel->fh, vdev);
3052 init_completion(&channel->completion);
3053 INIT_LIST_HEAD(&channel->source_shadow_list);
3054 INIT_LIST_HEAD(&channel->stream_shadow_list);
3055 mutex_init(&channel->shadow_list_lock);
3059 allegro_set_default_params(channel);
3061 handler = &channel->ctrl_handler;
3062 v4l2_ctrl_handler_init(handler, 0);
3063 channel->mpeg_video_h264_profile = v4l2_ctrl_new_std_menu(handler,
3065 V4L2_CID_MPEG_VIDEO_H264_PROFILE,
3066 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0,
3067 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE);
3068 mask = 1 << V4L2_MPEG_VIDEO_H264_LEVEL_1B;
3069 channel->mpeg_video_h264_level = v4l2_ctrl_new_std_menu(handler,
3071 V4L2_CID_MPEG_VIDEO_H264_LEVEL,
3072 V4L2_MPEG_VIDEO_H264_LEVEL_5_1, mask,
3073 V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
3074 channel->mpeg_video_h264_i_frame_qp =
3075 v4l2_ctrl_new_std(handler,
3077 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP,
3079 channel->mpeg_video_h264_max_qp =
3080 v4l2_ctrl_new_std(handler,
3082 V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
3084 channel->mpeg_video_h264_min_qp =
3085 v4l2_ctrl_new_std(handler,
3087 V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
3089 channel->mpeg_video_h264_p_frame_qp =
3090 v4l2_ctrl_new_std(handler,
3092 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP,
3094 channel->mpeg_video_h264_b_frame_qp =
3095 v4l2_ctrl_new_std(handler,
3097 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
3100 channel->mpeg_video_hevc_profile =
3101 v4l2_ctrl_new_std_menu(handler,
3103 V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
3104 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN, 0x0,
3105 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN);
3106 channel->mpeg_video_hevc_level =
3107 v4l2_ctrl_new_std_menu(handler,
3109 V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
3110 V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1, 0x0,
3111 V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1);
3112 channel->mpeg_video_hevc_tier =
3113 v4l2_ctrl_new_std_menu(handler,
3115 V4L2_CID_MPEG_VIDEO_HEVC_TIER,
3116 V4L2_MPEG_VIDEO_HEVC_TIER_HIGH, 0x0,
3117 V4L2_MPEG_VIDEO_HEVC_TIER_MAIN);
3118 channel->mpeg_video_hevc_i_frame_qp =
3119 v4l2_ctrl_new_std(handler,
3121 V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP,
3123 channel->mpeg_video_hevc_max_qp =
3124 v4l2_ctrl_new_std(handler,
3126 V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP,
3128 channel->mpeg_video_hevc_min_qp =
3129 v4l2_ctrl_new_std(handler,
3131 V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP,
3133 channel->mpeg_video_hevc_p_frame_qp =
3134 v4l2_ctrl_new_std(handler,
3136 V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
3138 channel->mpeg_video_hevc_b_frame_qp =
3139 v4l2_ctrl_new_std(handler,
3141 V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP,
3144 channel->mpeg_video_frame_rc_enable =
3145 v4l2_ctrl_new_std(handler,
3147 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
3150 channel->mpeg_video_bitrate_mode = v4l2_ctrl_new_std_menu(handler,
3152 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
3153 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
3154 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
3156 if (channel->codec == V4L2_PIX_FMT_H264) {
3157 bitrate_max = h264_maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
3158 bitrate_def = h264_maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
3159 cpb_size_max = h264_maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
3160 cpb_size_def = h264_maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1);
3162 bitrate_max = hevc_maximum_bitrate(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1);
3163 bitrate_def = hevc_maximum_bitrate(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1);
3164 cpb_size_max = hevc_maximum_cpb_size(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1);
3165 cpb_size_def = hevc_maximum_cpb_size(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1);
3167 channel->mpeg_video_bitrate = v4l2_ctrl_new_std(handler,
3169 V4L2_CID_MPEG_VIDEO_BITRATE,
3170 0, bitrate_max, 1, bitrate_def);
3171 channel->mpeg_video_bitrate_peak = v4l2_ctrl_new_std(handler,
3173 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
3174 0, bitrate_max, 1, bitrate_def);
3175 channel->mpeg_video_cpb_size = v4l2_ctrl_new_std(handler,
3177 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
3178 0, cpb_size_max, 1, cpb_size_def);
3179 channel->mpeg_video_gop_size = v4l2_ctrl_new_std(handler,
3181 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
3182 0, ALLEGRO_GOP_SIZE_MAX,
3183 1, ALLEGRO_GOP_SIZE_DEFAULT);
3184 channel->encoder_buffer = v4l2_ctrl_new_custom(handler,
3185 &allegro_encoder_buffer_ctrl_config, NULL);
3186 v4l2_ctrl_new_std(handler,
3188 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
3191 if (handler->error != 0) {
3192 ret = handler->error;
3196 channel->fh.ctrl_handler = handler;
3198 v4l2_ctrl_cluster(3, &channel->mpeg_video_bitrate_mode);
3200 v4l2_ctrl_handler_setup(handler);
3202 channel->mcu_channel_id = -1;
3203 channel->user_id = -1;
3205 INIT_LIST_HEAD(&channel->buffers_reference);
3206 INIT_LIST_HEAD(&channel->buffers_intermediate);
3208 channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel,
3209 allegro_queue_init);
3211 if (IS_ERR(channel->fh.m2m_ctx)) {
3212 ret = PTR_ERR(channel->fh.m2m_ctx);
3216 list_add(&channel->list, &dev->channels);
3217 file->private_data = &channel->fh;
3218 v4l2_fh_add(&channel->fh);
3220 allegro_channel_adjust(channel);
3225 v4l2_ctrl_handler_free(handler);
3230 static int allegro_release(struct file *file)
3232 struct allegro_channel *channel = fh_to_channel(file->private_data);
3234 v4l2_m2m_ctx_release(channel->fh.m2m_ctx);
3236 list_del(&channel->list);
3238 v4l2_ctrl_handler_free(&channel->ctrl_handler);
3240 v4l2_fh_del(&channel->fh);
3241 v4l2_fh_exit(&channel->fh);
3248 static int allegro_querycap(struct file *file, void *fh,
3249 struct v4l2_capability *cap)
3251 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
3252 strscpy(cap->card, "Allegro DVT Video Encoder", sizeof(cap->card));
3257 static int allegro_enum_fmt_vid(struct file *file, void *fh,
3258 struct v4l2_fmtdesc *f)
3261 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
3264 f->pixelformat = V4L2_PIX_FMT_NV12;
3266 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
3270 f->pixelformat = V4L2_PIX_FMT_H264;
3272 f->pixelformat = V4L2_PIX_FMT_HEVC;
3280 static int allegro_g_fmt_vid_cap(struct file *file, void *fh,
3281 struct v4l2_format *f)
3283 struct allegro_channel *channel = fh_to_channel(fh);
3285 f->fmt.pix.field = V4L2_FIELD_NONE;
3286 f->fmt.pix.width = channel->width;
3287 f->fmt.pix.height = channel->height;
3289 f->fmt.pix.colorspace = channel->colorspace;
3290 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
3291 f->fmt.pix.quantization = channel->quantization;
3292 f->fmt.pix.xfer_func = channel->xfer_func;
3294 f->fmt.pix.pixelformat = channel->codec;
3295 f->fmt.pix.bytesperline = 0;
3296 f->fmt.pix.sizeimage = channel->sizeimage_encoded;
3301 static int allegro_try_fmt_vid_cap(struct file *file, void *fh,
3302 struct v4l2_format *f)
3304 f->fmt.pix.field = V4L2_FIELD_NONE;
3306 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
3307 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
3308 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
3309 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
3311 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_HEVC &&
3312 f->fmt.pix.pixelformat != V4L2_PIX_FMT_H264)
3313 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
3315 f->fmt.pix.bytesperline = 0;
3316 f->fmt.pix.sizeimage =
3317 estimate_stream_size(f->fmt.pix.width, f->fmt.pix.height);
3322 static int allegro_s_fmt_vid_cap(struct file *file, void *fh,
3323 struct v4l2_format *f)
3325 struct allegro_channel *channel = fh_to_channel(fh);
3326 struct vb2_queue *vq;
3329 err = allegro_try_fmt_vid_cap(file, fh, f);
3333 vq = v4l2_m2m_get_vq(channel->fh.m2m_ctx, f->type);
3336 if (vb2_is_busy(vq))
3339 channel->codec = f->fmt.pix.pixelformat;
3341 allegro_channel_adjust(channel);
3346 static int allegro_g_fmt_vid_out(struct file *file, void *fh,
3347 struct v4l2_format *f)
3349 struct allegro_channel *channel = fh_to_channel(fh);
3351 f->fmt.pix.field = V4L2_FIELD_NONE;
3353 f->fmt.pix.width = channel->width;
3354 f->fmt.pix.height = channel->height;
3356 f->fmt.pix.colorspace = channel->colorspace;
3357 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc;
3358 f->fmt.pix.quantization = channel->quantization;
3359 f->fmt.pix.xfer_func = channel->xfer_func;
3361 f->fmt.pix.pixelformat = channel->pixelformat;
3362 f->fmt.pix.bytesperline = channel->stride;
3363 f->fmt.pix.sizeimage = channel->sizeimage_raw;
3368 static int allegro_try_fmt_vid_out(struct file *file, void *fh,
3369 struct v4l2_format *f)
3371 f->fmt.pix.field = V4L2_FIELD_NONE;
3374 * The firmware of the Allegro codec handles the padding internally
3375 * and expects the visual frame size when configuring a channel.
3376 * Therefore, unlike other encoder drivers, this driver does not round
3377 * up the width and height to macroblock alignment and does not
3378 * implement the selection api.
3380 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width,
3381 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX);
3382 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height,
3383 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX);
3385 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
3386 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 32);
3387 f->fmt.pix.sizeimage =
3388 f->fmt.pix.bytesperline * f->fmt.pix.height * 3 / 2;
3393 static int allegro_s_fmt_vid_out(struct file *file, void *fh,
3394 struct v4l2_format *f)
3396 struct allegro_channel *channel = fh_to_channel(fh);
3399 err = allegro_try_fmt_vid_out(file, fh, f);
3403 channel->width = f->fmt.pix.width;
3404 channel->height = f->fmt.pix.height;
3405 channel->stride = f->fmt.pix.bytesperline;
3406 channel->sizeimage_raw = f->fmt.pix.sizeimage;
3408 channel->colorspace = f->fmt.pix.colorspace;
3409 channel->ycbcr_enc = f->fmt.pix.ycbcr_enc;
3410 channel->quantization = f->fmt.pix.quantization;
3411 channel->xfer_func = f->fmt.pix.xfer_func;
3413 allegro_channel_adjust(channel);
3418 static int allegro_channel_cmd_stop(struct allegro_channel *channel)
3420 if (v4l2_m2m_has_stopped(channel->fh.m2m_ctx))
3421 allegro_channel_eos_event(channel);
3426 static int allegro_channel_cmd_start(struct allegro_channel *channel)
3428 if (v4l2_m2m_has_stopped(channel->fh.m2m_ctx))
3429 vb2_clear_last_buffer_dequeued(&channel->fh.m2m_ctx->cap_q_ctx.q);
3434 static int allegro_encoder_cmd(struct file *file, void *fh,
3435 struct v4l2_encoder_cmd *cmd)
3437 struct allegro_channel *channel = fh_to_channel(fh);
3440 err = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd);
3444 err = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd);
3448 if (cmd->cmd == V4L2_ENC_CMD_STOP)
3449 err = allegro_channel_cmd_stop(channel);
3451 if (cmd->cmd == V4L2_ENC_CMD_START)
3452 err = allegro_channel_cmd_start(channel);
3457 static int allegro_enum_framesizes(struct file *file, void *fh,
3458 struct v4l2_frmsizeenum *fsize)
3460 switch (fsize->pixel_format) {
3461 case V4L2_PIX_FMT_HEVC:
3462 case V4L2_PIX_FMT_H264:
3463 case V4L2_PIX_FMT_NV12:
3472 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
3473 fsize->stepwise.min_width = ALLEGRO_WIDTH_MIN;
3474 fsize->stepwise.max_width = ALLEGRO_WIDTH_MAX;
3475 fsize->stepwise.step_width = 1;
3476 fsize->stepwise.min_height = ALLEGRO_HEIGHT_MIN;
3477 fsize->stepwise.max_height = ALLEGRO_HEIGHT_MAX;
3478 fsize->stepwise.step_height = 1;
3483 static int allegro_ioctl_streamon(struct file *file, void *priv,
3484 enum v4l2_buf_type type)
3486 struct v4l2_fh *fh = file->private_data;
3487 struct allegro_channel *channel = fh_to_channel(fh);
3490 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
3491 err = allegro_create_channel(channel);
3496 return v4l2_m2m_streamon(file, fh->m2m_ctx, type);
3499 static int allegro_g_parm(struct file *file, void *fh,
3500 struct v4l2_streamparm *a)
3502 struct allegro_channel *channel = fh_to_channel(fh);
3503 struct v4l2_fract *timeperframe;
3505 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
3508 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
3509 timeperframe = &a->parm.output.timeperframe;
3510 timeperframe->numerator = channel->framerate.denominator;
3511 timeperframe->denominator = channel->framerate.numerator;
3516 static int allegro_s_parm(struct file *file, void *fh,
3517 struct v4l2_streamparm *a)
3519 struct allegro_channel *channel = fh_to_channel(fh);
3520 struct v4l2_fract *timeperframe;
3523 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
3526 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
3527 timeperframe = &a->parm.output.timeperframe;
3529 if (timeperframe->numerator == 0 || timeperframe->denominator == 0)
3530 return allegro_g_parm(file, fh, a);
3532 div = gcd(timeperframe->denominator, timeperframe->numerator);
3533 channel->framerate.numerator = timeperframe->denominator / div;
3534 channel->framerate.denominator = timeperframe->numerator / div;
3539 static int allegro_subscribe_event(struct v4l2_fh *fh,
3540 const struct v4l2_event_subscription *sub)
3542 switch (sub->type) {
3543 case V4L2_EVENT_EOS:
3544 return v4l2_event_subscribe(fh, sub, 0, NULL);
3546 return v4l2_ctrl_subscribe_event(fh, sub);
3550 static const struct v4l2_ioctl_ops allegro_ioctl_ops = {
3551 .vidioc_querycap = allegro_querycap,
3552 .vidioc_enum_fmt_vid_cap = allegro_enum_fmt_vid,
3553 .vidioc_enum_fmt_vid_out = allegro_enum_fmt_vid,
3554 .vidioc_g_fmt_vid_cap = allegro_g_fmt_vid_cap,
3555 .vidioc_try_fmt_vid_cap = allegro_try_fmt_vid_cap,
3556 .vidioc_s_fmt_vid_cap = allegro_s_fmt_vid_cap,
3557 .vidioc_g_fmt_vid_out = allegro_g_fmt_vid_out,
3558 .vidioc_try_fmt_vid_out = allegro_try_fmt_vid_out,
3559 .vidioc_s_fmt_vid_out = allegro_s_fmt_vid_out,
3561 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
3562 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
3564 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
3565 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
3566 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
3567 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
3568 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
3570 .vidioc_streamon = allegro_ioctl_streamon,
3571 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
3573 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd,
3574 .vidioc_encoder_cmd = allegro_encoder_cmd,
3575 .vidioc_enum_framesizes = allegro_enum_framesizes,
3577 .vidioc_g_parm = allegro_g_parm,
3578 .vidioc_s_parm = allegro_s_parm,
3580 .vidioc_subscribe_event = allegro_subscribe_event,
3581 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
3584 static const struct v4l2_file_operations allegro_fops = {
3585 .owner = THIS_MODULE,
3586 .open = allegro_open,
3587 .release = allegro_release,
3588 .poll = v4l2_m2m_fop_poll,
3589 .unlocked_ioctl = video_ioctl2,
3590 .mmap = v4l2_m2m_fop_mmap,
3593 static int allegro_register_device(struct allegro_dev *dev)
3595 struct video_device *video_dev = &dev->video_dev;
3597 strscpy(video_dev->name, "allegro", sizeof(video_dev->name));
3598 video_dev->fops = &allegro_fops;
3599 video_dev->ioctl_ops = &allegro_ioctl_ops;
3600 video_dev->release = video_device_release_empty;
3601 video_dev->lock = &dev->lock;
3602 video_dev->v4l2_dev = &dev->v4l2_dev;
3603 video_dev->vfl_dir = VFL_DIR_M2M;
3604 video_dev->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
3605 video_set_drvdata(video_dev, dev);
3607 return video_register_device(video_dev, VFL_TYPE_VIDEO, 0);
3610 static void allegro_device_run(void *priv)
3612 struct allegro_channel *channel = priv;
3613 struct allegro_dev *dev = channel->dev;
3614 struct vb2_v4l2_buffer *src_buf;
3615 struct vb2_v4l2_buffer *dst_buf;
3618 dma_addr_t dst_addr;
3619 unsigned long dst_size;
3623 dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx);
3624 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
3625 dst_size = vb2_plane_size(&dst_buf->vb2_buf, 0);
3626 dst_handle = allegro_put_buffer(channel, &channel->stream_shadow_list,
3628 allegro_mcu_send_put_stream_buffer(dev, channel, dst_addr, dst_size,
3631 src_buf = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx);
3632 src_buf->sequence = channel->osequence++;
3633 src_y = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
3634 src_uv = src_y + (channel->stride * channel->height);
3635 src_handle = allegro_put_buffer(channel, &channel->source_shadow_list,
3637 allegro_mcu_send_encode_frame(dev, channel, src_y, src_uv, src_handle);
3639 v4l2_m2m_job_finish(dev->m2m_dev, channel->fh.m2m_ctx);
3642 static const struct v4l2_m2m_ops allegro_m2m_ops = {
3643 .device_run = allegro_device_run,
3646 static int allegro_mcu_hw_init(struct allegro_dev *dev,
3647 const struct fw_info *info)
3651 dev->mbox_command = allegro_mbox_init(dev, info->mailbox_cmd,
3652 info->mailbox_size);
3653 dev->mbox_status = allegro_mbox_init(dev, info->mailbox_status,
3654 info->mailbox_size);
3655 if (IS_ERR(dev->mbox_command) || IS_ERR(dev->mbox_status)) {
3656 v4l2_err(&dev->v4l2_dev,
3657 "failed to initialize mailboxes\n");
3661 err = allegro_encoder_buffer_init(dev, &dev->encoder_buffer);
3662 dev->has_encoder_buffer = (err == 0);
3663 if (!dev->has_encoder_buffer)
3664 v4l2_info(&dev->v4l2_dev, "encoder buffer not available\n");
3666 allegro_mcu_enable_interrupts(dev);
3668 /* The mcu sends INIT after reset. */
3669 allegro_mcu_start(dev);
3670 err = allegro_mcu_wait_for_init_timeout(dev, 5000);
3672 v4l2_err(&dev->v4l2_dev,
3673 "mcu did not send INIT after reset\n");
3675 goto err_disable_interrupts;
3678 err = allegro_alloc_buffer(dev, &dev->suballocator,
3679 info->suballocator_size);
3681 v4l2_err(&dev->v4l2_dev,
3682 "failed to allocate %zu bytes for suballocator\n",
3683 info->suballocator_size);
3687 allegro_mcu_send_init(dev, dev->suballocator.paddr,
3688 dev->suballocator.size);
3689 err = allegro_mcu_wait_for_init_timeout(dev, 5000);
3691 v4l2_err(&dev->v4l2_dev,
3692 "mcu failed to configure sub-allocator\n");
3694 goto err_free_suballocator;
3699 err_free_suballocator:
3700 allegro_free_buffer(dev, &dev->suballocator);
3702 allegro_mcu_reset(dev);
3703 err_disable_interrupts:
3704 allegro_mcu_disable_interrupts(dev);
3709 static int allegro_mcu_hw_deinit(struct allegro_dev *dev)
3713 err = allegro_mcu_reset(dev);
3715 v4l2_warn(&dev->v4l2_dev,
3716 "mcu failed to enter sleep state\n");
3718 err = allegro_mcu_disable_interrupts(dev);
3720 v4l2_warn(&dev->v4l2_dev,
3721 "failed to disable interrupts\n");
3723 allegro_free_buffer(dev, &dev->suballocator);
3728 static void allegro_fw_callback(const struct firmware *fw, void *context)
3730 struct allegro_dev *dev = context;
3731 const char *fw_codec_name = "al5e.fw";
3732 const struct firmware *fw_codec;
3738 v4l2_dbg(1, debug, &dev->v4l2_dev,
3739 "requesting codec firmware '%s'\n", fw_codec_name);
3740 err = request_firmware(&fw_codec, fw_codec_name, &dev->plat_dev->dev);
3742 goto err_release_firmware;
3744 dev->fw_info = allegro_get_firmware_info(dev, fw, fw_codec);
3745 if (!dev->fw_info) {
3746 v4l2_err(&dev->v4l2_dev, "firmware is not supported\n");
3747 goto err_release_firmware_codec;
3750 v4l2_info(&dev->v4l2_dev,
3751 "using mcu firmware version '%s'\n", dev->fw_info->version);
3753 pm_runtime_enable(&dev->plat_dev->dev);
3754 err = pm_runtime_resume_and_get(&dev->plat_dev->dev);
3756 goto err_release_firmware_codec;
3758 /* Ensure that the mcu is sleeping at the reset vector */
3759 err = allegro_mcu_reset(dev);
3761 v4l2_err(&dev->v4l2_dev, "failed to reset mcu\n");
3765 allegro_copy_firmware(dev, fw->data, fw->size);
3766 allegro_copy_fw_codec(dev, fw_codec->data, fw_codec->size);
3768 err = allegro_mcu_hw_init(dev, dev->fw_info);
3770 v4l2_err(&dev->v4l2_dev, "failed to initialize mcu\n");
3771 goto err_free_fw_codec;
3774 dev->m2m_dev = v4l2_m2m_init(&allegro_m2m_ops);
3775 if (IS_ERR(dev->m2m_dev)) {
3776 v4l2_err(&dev->v4l2_dev, "failed to init mem2mem device\n");
3777 goto err_mcu_hw_deinit;
3780 err = allegro_register_device(dev);
3782 v4l2_err(&dev->v4l2_dev, "failed to register video device\n");
3783 goto err_m2m_release;
3786 v4l2_dbg(1, debug, &dev->v4l2_dev,
3787 "allegro codec registered as /dev/video%d\n",
3788 dev->video_dev.num);
3790 dev->initialized = true;
3792 release_firmware(fw_codec);
3793 release_firmware(fw);
3798 v4l2_m2m_release(dev->m2m_dev);
3799 dev->m2m_dev = NULL;
3801 allegro_mcu_hw_deinit(dev);
3803 allegro_free_fw_codec(dev);
3805 pm_runtime_put(&dev->plat_dev->dev);
3806 pm_runtime_disable(&dev->plat_dev->dev);
3807 err_release_firmware_codec:
3808 release_firmware(fw_codec);
3809 err_release_firmware:
3810 release_firmware(fw);
3813 static int allegro_firmware_request_nowait(struct allegro_dev *dev)
3815 const char *fw = "al5e_b.fw";
3817 v4l2_dbg(1, debug, &dev->v4l2_dev,
3818 "requesting firmware '%s'\n", fw);
3819 return request_firmware_nowait(THIS_MODULE, true, fw,
3820 &dev->plat_dev->dev, GFP_KERNEL, dev,
3821 allegro_fw_callback);
3824 static int allegro_probe(struct platform_device *pdev)
3826 struct allegro_dev *dev;
3827 struct resource *res, *sram_res;
3830 void __iomem *regs, *sram_regs;
3832 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
3835 dev->plat_dev = pdev;
3836 init_completion(&dev->init_complete);
3837 INIT_LIST_HEAD(&dev->channels);
3839 mutex_init(&dev->lock);
3841 dev->initialized = false;
3843 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
3846 "regs resource missing from device tree\n");
3849 regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
3851 dev_err(&pdev->dev, "failed to map registers\n");
3854 dev->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
3855 &allegro_regmap_config);
3856 if (IS_ERR(dev->regmap)) {
3857 dev_err(&pdev->dev, "failed to init regmap\n");
3858 return PTR_ERR(dev->regmap);
3861 sram_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
3864 "sram resource missing from device tree\n");
3867 sram_regs = devm_ioremap(&pdev->dev,
3869 resource_size(sram_res));
3871 dev_err(&pdev->dev, "failed to map sram\n");
3874 dev->sram = devm_regmap_init_mmio(&pdev->dev, sram_regs,
3875 &allegro_sram_config);
3876 if (IS_ERR(dev->sram)) {
3877 dev_err(&pdev->dev, "failed to init sram\n");
3878 return PTR_ERR(dev->sram);
3881 dev->settings = syscon_regmap_lookup_by_compatible("xlnx,vcu-settings");
3882 if (IS_ERR(dev->settings))
3883 dev_warn(&pdev->dev, "failed to open settings\n");
3885 dev->clk_core = devm_clk_get(&pdev->dev, "core_clk");
3886 if (IS_ERR(dev->clk_core))
3887 return PTR_ERR(dev->clk_core);
3889 dev->clk_mcu = devm_clk_get(&pdev->dev, "mcu_clk");
3890 if (IS_ERR(dev->clk_mcu))
3891 return PTR_ERR(dev->clk_mcu);
3893 irq = platform_get_irq(pdev, 0);
3896 ret = devm_request_threaded_irq(&pdev->dev, irq,
3899 IRQF_SHARED, dev_name(&pdev->dev), dev);
3901 dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
3905 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
3909 platform_set_drvdata(pdev, dev);
3911 ret = allegro_firmware_request_nowait(dev);
3913 v4l2_err(&dev->v4l2_dev,
3914 "failed to request firmware: %d\n", ret);
3921 static void allegro_remove(struct platform_device *pdev)
3923 struct allegro_dev *dev = platform_get_drvdata(pdev);
3925 if (dev->initialized) {
3926 video_unregister_device(&dev->video_dev);
3928 v4l2_m2m_release(dev->m2m_dev);
3929 allegro_mcu_hw_deinit(dev);
3930 allegro_free_fw_codec(dev);
3933 pm_runtime_put(&dev->plat_dev->dev);
3934 pm_runtime_disable(&dev->plat_dev->dev);
3936 v4l2_device_unregister(&dev->v4l2_dev);
3939 static int allegro_runtime_resume(struct device *device)
3941 struct allegro_dev *dev = dev_get_drvdata(device);
3942 struct regmap *settings = dev->settings;
3943 unsigned int clk_mcu;
3944 unsigned int clk_core;
3950 #define MHZ_TO_HZ(freq) ((freq) * 1000 * 1000)
3952 err = regmap_read(settings, VCU_CORE_CLK, &clk_core);
3955 err = clk_set_rate(dev->clk_core, MHZ_TO_HZ(clk_core));
3958 err = clk_prepare_enable(dev->clk_core);
3962 err = regmap_read(settings, VCU_MCU_CLK, &clk_mcu);
3964 goto disable_clk_core;
3965 err = clk_set_rate(dev->clk_mcu, MHZ_TO_HZ(clk_mcu));
3967 goto disable_clk_core;
3968 err = clk_prepare_enable(dev->clk_mcu);
3970 goto disable_clk_core;
3977 clk_disable_unprepare(dev->clk_core);
3982 static int allegro_runtime_suspend(struct device *device)
3984 struct allegro_dev *dev = dev_get_drvdata(device);
3986 clk_disable_unprepare(dev->clk_mcu);
3987 clk_disable_unprepare(dev->clk_core);
3992 static const struct of_device_id allegro_dt_ids[] = {
3993 { .compatible = "allegro,al5e-1.1" },
3997 MODULE_DEVICE_TABLE(of, allegro_dt_ids);
3999 static const struct dev_pm_ops allegro_pm_ops = {
4000 .runtime_resume = allegro_runtime_resume,
4001 .runtime_suspend = allegro_runtime_suspend,
4004 static struct platform_driver allegro_driver = {
4005 .probe = allegro_probe,
4006 .remove = allegro_remove,
4009 .of_match_table = allegro_dt_ids,
4010 .pm = &allegro_pm_ops,
4014 module_platform_driver(allegro_driver);
4016 MODULE_LICENSE("GPL");
4018 MODULE_DESCRIPTION("Allegro DVT encoder driver");