1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2019 Pengutronix, Michael Tretter <kernel@pengutronix.de> 4 * 5 * Allegro DVT video encoder driver 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/clk.h> 10 #include <linux/firmware.h> 11 #include <linux/gcd.h> 12 #include <linux/interrupt.h> 13 #include <linux/io.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> 19 #include <linux/of.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> 33 34 #include "allegro-mail.h" 35 #include "nal-h264.h" 36 #include "nal-hevc.h" 37 38 /* 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. 42 */ 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 49 50 #define ALLEGRO_FRAMERATE_DEFAULT ((struct v4l2_fract) { 30, 1 }) 51 52 #define ALLEGRO_GOP_SIZE_DEFAULT 25 53 #define ALLEGRO_GOP_SIZE_MAX 1000 54 55 /* 56 * MCU Control Registers 57 * 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. 62 */ 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 72 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 77 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) 83 84 #define AXI_ADDR_OFFSET_IP 0x0208 85 86 /* 87 * The MCU accesses the system memory with a 2G offset compared to CPU 88 * physical addresses. 89 */ 90 #define MCU_CACHE_OFFSET SZ_2G 91 92 /* 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. 96 */ 97 #define ENCODER_STREAM_OFFSET SZ_128 98 99 #define SIZE_MACROBLOCK 16 100 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 106 107 /* 108 * This control allows applications to explicitly disable the encoder buffer. 109 * This value is Allegro specific. 110 */ 111 #define V4L2_CID_USER_ALLEGRO_ENCODER_BUFFER (V4L2_CID_USER_ALLEGRO_BASE + 0) 112 113 static int debug; 114 module_param(debug, int, 0644); 115 MODULE_PARM_DESC(debug, "Debug level (0-2)"); 116 117 struct allegro_buffer { 118 void *vaddr; 119 dma_addr_t paddr; 120 size_t size; 121 struct list_head head; 122 }; 123 124 struct allegro_dev; 125 struct allegro_channel; 126 127 struct allegro_mbox { 128 struct allegro_dev *dev; 129 unsigned int head; 130 unsigned int tail; 131 unsigned int data; 132 size_t size; 133 /* protect mailbox from simultaneous accesses */ 134 struct mutex lock; 135 }; 136 137 struct allegro_encoder_buffer { 138 unsigned int size; 139 unsigned int color_depth; 140 unsigned int num_cores; 141 unsigned int clk_rate; 142 }; 143 144 struct allegro_dev { 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; 149 150 /* mutex protecting vb2_queue structure */ 151 struct mutex lock; 152 153 struct regmap *regmap; 154 struct regmap *sram; 155 struct regmap *settings; 156 157 struct clk *clk_core; 158 struct clk *clk_mcu; 159 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; 165 166 struct completion init_complete; 167 bool initialized; 168 169 /* The mailbox interface */ 170 struct allegro_mbox *mbox_command; 171 struct allegro_mbox *mbox_status; 172 173 /* 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. 177 */ 178 unsigned long channel_user_ids; 179 struct list_head channels; 180 }; 181 182 static const struct regmap_config allegro_regmap_config = { 183 .name = "regmap", 184 .reg_bits = 32, 185 .val_bits = 32, 186 .reg_stride = 4, 187 .max_register = 0xfff, 188 .cache_type = REGCACHE_NONE, 189 }; 190 191 static const struct regmap_config allegro_sram_config = { 192 .name = "sram", 193 .reg_bits = 32, 194 .val_bits = 32, 195 .reg_stride = 4, 196 .max_register = 0x7fff, 197 .cache_type = REGCACHE_NONE, 198 }; 199 200 struct allegro_channel { 201 struct allegro_dev *dev; 202 struct v4l2_fh fh; 203 struct v4l2_ctrl_handler ctrl_handler; 204 205 unsigned int width; 206 unsigned int height; 207 unsigned int stride; 208 struct v4l2_fract framerate; 209 210 enum v4l2_colorspace colorspace; 211 enum v4l2_ycbcr_encoding ycbcr_enc; 212 enum v4l2_quantization quantization; 213 enum v4l2_xfer_func xfer_func; 214 215 u32 pixelformat; 216 unsigned int sizeimage_raw; 217 unsigned int osequence; 218 219 u32 codec; 220 unsigned int sizeimage_encoded; 221 unsigned int csequence; 222 223 bool frame_rc_enable; 224 unsigned int bitrate; 225 unsigned int bitrate_peak; 226 227 struct allegro_buffer config_blob; 228 229 unsigned int log2_max_frame_num; 230 bool temporal_mvp_enable; 231 232 bool enable_loop_filter_across_tiles; 233 bool enable_loop_filter_across_slices; 234 bool enable_deblocking_filter_override; 235 bool enable_reordering; 236 bool dbf_ovr_en; 237 238 unsigned int num_ref_idx_l0; 239 unsigned int num_ref_idx_l1; 240 241 /* Maximum range for motion estimation */ 242 int b_hrz_me_range; 243 int b_vrt_me_range; 244 int p_hrz_me_range; 245 int p_vrt_me_range; 246 /* Size limits of coding unit */ 247 int min_cu_size; 248 int max_cu_size; 249 /* Size limits of transform unit */ 250 int min_tu_size; 251 int max_tu_size; 252 int max_transfo_depth_intra; 253 int max_transfo_depth_inter; 254 255 struct v4l2_ctrl *mpeg_video_h264_profile; 256 struct v4l2_ctrl *mpeg_video_h264_level; 257 struct v4l2_ctrl *mpeg_video_h264_i_frame_qp; 258 struct v4l2_ctrl *mpeg_video_h264_max_qp; 259 struct v4l2_ctrl *mpeg_video_h264_min_qp; 260 struct v4l2_ctrl *mpeg_video_h264_p_frame_qp; 261 struct v4l2_ctrl *mpeg_video_h264_b_frame_qp; 262 263 struct v4l2_ctrl *mpeg_video_hevc_profile; 264 struct v4l2_ctrl *mpeg_video_hevc_level; 265 struct v4l2_ctrl *mpeg_video_hevc_tier; 266 struct v4l2_ctrl *mpeg_video_hevc_i_frame_qp; 267 struct v4l2_ctrl *mpeg_video_hevc_max_qp; 268 struct v4l2_ctrl *mpeg_video_hevc_min_qp; 269 struct v4l2_ctrl *mpeg_video_hevc_p_frame_qp; 270 struct v4l2_ctrl *mpeg_video_hevc_b_frame_qp; 271 272 struct v4l2_ctrl *mpeg_video_frame_rc_enable; 273 struct { /* video bitrate mode control cluster */ 274 struct v4l2_ctrl *mpeg_video_bitrate_mode; 275 struct v4l2_ctrl *mpeg_video_bitrate; 276 struct v4l2_ctrl *mpeg_video_bitrate_peak; 277 }; 278 struct v4l2_ctrl *mpeg_video_cpb_size; 279 struct v4l2_ctrl *mpeg_video_gop_size; 280 281 struct v4l2_ctrl *encoder_buffer; 282 283 /* user_id is used to identify the channel during CREATE_CHANNEL */ 284 /* not sure, what to set here and if this is actually required */ 285 int user_id; 286 /* channel_id is set by the mcu and used by all later commands */ 287 int mcu_channel_id; 288 289 struct list_head buffers_reference; 290 struct list_head buffers_intermediate; 291 292 struct list_head source_shadow_list; 293 struct list_head stream_shadow_list; 294 /* protect shadow lists of buffers passed to firmware */ 295 struct mutex shadow_list_lock; 296 297 struct list_head list; 298 struct completion completion; 299 300 unsigned int error; 301 }; 302 303 static inline struct allegro_channel *file_to_channel(struct file *filp) 304 { 305 return container_of(file_to_v4l2_fh(filp), struct allegro_channel, fh); 306 } 307 308 static inline int 309 allegro_channel_get_i_frame_qp(struct allegro_channel *channel) 310 { 311 if (channel->codec == V4L2_PIX_FMT_HEVC) 312 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_i_frame_qp); 313 else 314 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_i_frame_qp); 315 } 316 317 static inline int 318 allegro_channel_get_p_frame_qp(struct allegro_channel *channel) 319 { 320 if (channel->codec == V4L2_PIX_FMT_HEVC) 321 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_p_frame_qp); 322 else 323 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_p_frame_qp); 324 } 325 326 static inline int 327 allegro_channel_get_b_frame_qp(struct allegro_channel *channel) 328 { 329 if (channel->codec == V4L2_PIX_FMT_HEVC) 330 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_b_frame_qp); 331 else 332 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_b_frame_qp); 333 } 334 335 static inline int 336 allegro_channel_get_min_qp(struct allegro_channel *channel) 337 { 338 if (channel->codec == V4L2_PIX_FMT_HEVC) 339 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_min_qp); 340 else 341 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_min_qp); 342 } 343 344 static inline int 345 allegro_channel_get_max_qp(struct allegro_channel *channel) 346 { 347 if (channel->codec == V4L2_PIX_FMT_HEVC) 348 return v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_max_qp); 349 else 350 return v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_max_qp); 351 } 352 353 struct allegro_m2m_buffer { 354 struct v4l2_m2m_buffer buf; 355 struct list_head head; 356 }; 357 358 #define to_allegro_m2m_buffer(__buf) \ 359 container_of(__buf, struct allegro_m2m_buffer, buf) 360 361 struct fw_info { 362 unsigned int id; 363 unsigned int id_codec; 364 char *version; 365 unsigned int mailbox_cmd; 366 unsigned int mailbox_status; 367 size_t mailbox_size; 368 enum mcu_msg_version mailbox_version; 369 size_t suballocator_size; 370 }; 371 372 static const struct fw_info supported_firmware[] = { 373 { 374 .id = 18296, 375 .id_codec = 96272, 376 .version = "v2018.2", 377 .mailbox_cmd = 0x7800, 378 .mailbox_status = 0x7c00, 379 .mailbox_size = 0x400 - 0x8, 380 .mailbox_version = MCU_MSG_VERSION_2018_2, 381 .suballocator_size = SZ_16M, 382 }, { 383 .id = 14680, 384 .id_codec = 126572, 385 .version = "v2019.2", 386 .mailbox_cmd = 0x7000, 387 .mailbox_status = 0x7800, 388 .mailbox_size = 0x800 - 0x8, 389 .mailbox_version = MCU_MSG_VERSION_2019_2, 390 .suballocator_size = SZ_32M, 391 }, 392 }; 393 394 static inline u32 to_mcu_addr(struct allegro_dev *dev, dma_addr_t phys) 395 { 396 if (upper_32_bits(phys) || (lower_32_bits(phys) & MCU_CACHE_OFFSET)) 397 v4l2_warn(&dev->v4l2_dev, 398 "address %pad is outside mcu window\n", &phys); 399 400 return lower_32_bits(phys) | MCU_CACHE_OFFSET; 401 } 402 403 static inline u32 to_mcu_size(struct allegro_dev *dev, size_t size) 404 { 405 return lower_32_bits(size); 406 } 407 408 static inline u32 to_codec_addr(struct allegro_dev *dev, dma_addr_t phys) 409 { 410 if (upper_32_bits(phys)) 411 v4l2_warn(&dev->v4l2_dev, 412 "address %pad cannot be used by codec\n", &phys); 413 414 return lower_32_bits(phys); 415 } 416 417 static inline u64 ptr_to_u64(const void *ptr) 418 { 419 return (uintptr_t)ptr; 420 } 421 422 /* Helper functions for channel and user operations */ 423 424 static unsigned long allegro_next_user_id(struct allegro_dev *dev) 425 { 426 if (dev->channel_user_ids == ~0UL) 427 return -EBUSY; 428 429 return ffz(dev->channel_user_ids); 430 } 431 432 static struct allegro_channel * 433 allegro_find_channel_by_user_id(struct allegro_dev *dev, 434 unsigned int user_id) 435 { 436 struct allegro_channel *channel; 437 438 list_for_each_entry(channel, &dev->channels, list) { 439 if (channel->user_id == user_id) 440 return channel; 441 } 442 443 return ERR_PTR(-EINVAL); 444 } 445 446 static struct allegro_channel * 447 allegro_find_channel_by_channel_id(struct allegro_dev *dev, 448 unsigned int channel_id) 449 { 450 struct allegro_channel *channel; 451 452 list_for_each_entry(channel, &dev->channels, list) { 453 if (channel->mcu_channel_id == channel_id) 454 return channel; 455 } 456 457 return ERR_PTR(-EINVAL); 458 } 459 460 static inline bool channel_exists(struct allegro_channel *channel) 461 { 462 return channel->mcu_channel_id != -1; 463 } 464 465 #define AL_ERROR 0x80 466 #define AL_ERR_INIT_FAILED 0x81 467 #define AL_ERR_NO_FRAME_DECODED 0x82 468 #define AL_ERR_RESOLUTION_CHANGE 0x85 469 #define AL_ERR_NO_MEMORY 0x87 470 #define AL_ERR_STREAM_OVERFLOW 0x88 471 #define AL_ERR_TOO_MANY_SLICES 0x89 472 #define AL_ERR_BUF_NOT_READY 0x8c 473 #define AL_ERR_NO_CHANNEL_AVAILABLE 0x8d 474 #define AL_ERR_RESOURCE_UNAVAILABLE 0x8e 475 #define AL_ERR_NOT_ENOUGH_CORES 0x8f 476 #define AL_ERR_REQUEST_MALFORMED 0x90 477 #define AL_ERR_CMD_NOT_ALLOWED 0x91 478 #define AL_ERR_INVALID_CMD_VALUE 0x92 479 480 static inline const char *allegro_err_to_string(unsigned int err) 481 { 482 switch (err) { 483 case AL_ERR_INIT_FAILED: 484 return "initialization failed"; 485 case AL_ERR_NO_FRAME_DECODED: 486 return "no frame decoded"; 487 case AL_ERR_RESOLUTION_CHANGE: 488 return "resolution change"; 489 case AL_ERR_NO_MEMORY: 490 return "out of memory"; 491 case AL_ERR_STREAM_OVERFLOW: 492 return "stream buffer overflow"; 493 case AL_ERR_TOO_MANY_SLICES: 494 return "too many slices"; 495 case AL_ERR_BUF_NOT_READY: 496 return "buffer not ready"; 497 case AL_ERR_NO_CHANNEL_AVAILABLE: 498 return "no channel available"; 499 case AL_ERR_RESOURCE_UNAVAILABLE: 500 return "resource unavailable"; 501 case AL_ERR_NOT_ENOUGH_CORES: 502 return "not enough cores"; 503 case AL_ERR_REQUEST_MALFORMED: 504 return "request malformed"; 505 case AL_ERR_CMD_NOT_ALLOWED: 506 return "command not allowed"; 507 case AL_ERR_INVALID_CMD_VALUE: 508 return "invalid command value"; 509 case AL_ERROR: 510 default: 511 return "unknown error"; 512 } 513 } 514 515 static unsigned int estimate_stream_size(unsigned int width, 516 unsigned int height) 517 { 518 unsigned int offset = ENCODER_STREAM_OFFSET; 519 unsigned int num_blocks = DIV_ROUND_UP(width, SIZE_MACROBLOCK) * 520 DIV_ROUND_UP(height, SIZE_MACROBLOCK); 521 unsigned int pcm_size = SZ_256; 522 unsigned int partition_table = SZ_256; 523 524 return round_up(offset + num_blocks * pcm_size + partition_table, 32); 525 } 526 527 static enum v4l2_mpeg_video_h264_level 528 select_minimum_h264_level(unsigned int width, unsigned int height) 529 { 530 unsigned int pic_width_in_mb = DIV_ROUND_UP(width, SIZE_MACROBLOCK); 531 unsigned int frame_height_in_mb = DIV_ROUND_UP(height, SIZE_MACROBLOCK); 532 unsigned int frame_size_in_mb = pic_width_in_mb * frame_height_in_mb; 533 enum v4l2_mpeg_video_h264_level level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0; 534 535 /* 536 * The level limits are specified in Rec. ITU-T H.264 Annex A.3.1 and 537 * also specify limits regarding bit rate and CBP size. Only approximate 538 * the levels using the frame size. 539 * 540 * Level 5.1 allows up to 4k video resolution. 541 */ 542 if (frame_size_in_mb <= 99) 543 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_0; 544 else if (frame_size_in_mb <= 396) 545 level = V4L2_MPEG_VIDEO_H264_LEVEL_1_1; 546 else if (frame_size_in_mb <= 792) 547 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_1; 548 else if (frame_size_in_mb <= 1620) 549 level = V4L2_MPEG_VIDEO_H264_LEVEL_2_2; 550 else if (frame_size_in_mb <= 3600) 551 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_1; 552 else if (frame_size_in_mb <= 5120) 553 level = V4L2_MPEG_VIDEO_H264_LEVEL_3_2; 554 else if (frame_size_in_mb <= 8192) 555 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0; 556 else if (frame_size_in_mb <= 8704) 557 level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2; 558 else if (frame_size_in_mb <= 22080) 559 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_0; 560 else 561 level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1; 562 563 return level; 564 } 565 566 static unsigned int h264_maximum_bitrate(enum v4l2_mpeg_video_h264_level level) 567 { 568 switch (level) { 569 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0: 570 return 64000; 571 case V4L2_MPEG_VIDEO_H264_LEVEL_1B: 572 return 128000; 573 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1: 574 return 192000; 575 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2: 576 return 384000; 577 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3: 578 return 768000; 579 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0: 580 return 2000000; 581 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1: 582 return 4000000; 583 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2: 584 return 4000000; 585 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0: 586 return 10000000; 587 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1: 588 return 14000000; 589 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2: 590 return 20000000; 591 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0: 592 return 20000000; 593 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1: 594 return 50000000; 595 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2: 596 return 50000000; 597 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0: 598 return 135000000; 599 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1: 600 default: 601 return 240000000; 602 } 603 } 604 605 static unsigned int h264_maximum_cpb_size(enum v4l2_mpeg_video_h264_level level) 606 { 607 switch (level) { 608 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0: 609 return 175; 610 case V4L2_MPEG_VIDEO_H264_LEVEL_1B: 611 return 350; 612 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1: 613 return 500; 614 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2: 615 return 1000; 616 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3: 617 return 2000; 618 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0: 619 return 2000; 620 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1: 621 return 4000; 622 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2: 623 return 4000; 624 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0: 625 return 10000; 626 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1: 627 return 14000; 628 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2: 629 return 20000; 630 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0: 631 return 25000; 632 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1: 633 return 62500; 634 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2: 635 return 62500; 636 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0: 637 return 135000; 638 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1: 639 default: 640 return 240000; 641 } 642 } 643 644 static enum v4l2_mpeg_video_hevc_level 645 select_minimum_hevc_level(unsigned int width, unsigned int height) 646 { 647 unsigned int luma_picture_size = width * height; 648 enum v4l2_mpeg_video_hevc_level level; 649 650 if (luma_picture_size <= 36864) 651 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_1; 652 else if (luma_picture_size <= 122880) 653 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_2; 654 else if (luma_picture_size <= 245760) 655 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1; 656 else if (luma_picture_size <= 552960) 657 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_3; 658 else if (luma_picture_size <= 983040) 659 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1; 660 else if (luma_picture_size <= 2228224) 661 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_4; 662 else if (luma_picture_size <= 8912896) 663 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_5; 664 else 665 level = V4L2_MPEG_VIDEO_HEVC_LEVEL_6; 666 667 return level; 668 } 669 670 static unsigned int hevc_maximum_bitrate(enum v4l2_mpeg_video_hevc_level level) 671 { 672 /* 673 * See Rec. ITU-T H.265 v5 (02/2018), A.4.2 Profile-specific level 674 * limits for the video profiles. 675 */ 676 switch (level) { 677 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1: 678 return 128; 679 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2: 680 return 1500; 681 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1: 682 return 3000; 683 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3: 684 return 6000; 685 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1: 686 return 10000; 687 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4: 688 return 12000; 689 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1: 690 return 20000; 691 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5: 692 return 25000; 693 default: 694 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1: 695 return 40000; 696 } 697 } 698 699 static unsigned int hevc_maximum_cpb_size(enum v4l2_mpeg_video_hevc_level level) 700 { 701 switch (level) { 702 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1: 703 return 350; 704 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2: 705 return 1500; 706 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1: 707 return 3000; 708 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3: 709 return 6000; 710 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1: 711 return 10000; 712 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4: 713 return 12000; 714 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1: 715 return 20000; 716 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5: 717 return 25000; 718 default: 719 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1: 720 return 40000; 721 } 722 } 723 724 static const struct fw_info * 725 allegro_get_firmware_info(struct allegro_dev *dev, 726 const struct firmware *fw, 727 const struct firmware *fw_codec) 728 { 729 int i; 730 unsigned int id = fw->size; 731 unsigned int id_codec = fw_codec->size; 732 733 for (i = 0; i < ARRAY_SIZE(supported_firmware); i++) 734 if (supported_firmware[i].id == id && 735 supported_firmware[i].id_codec == id_codec) 736 return &supported_firmware[i]; 737 738 return NULL; 739 } 740 741 /* 742 * Buffers that are used internally by the MCU. 743 */ 744 745 static int allegro_alloc_buffer(struct allegro_dev *dev, 746 struct allegro_buffer *buffer, size_t size) 747 { 748 buffer->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, 749 &buffer->paddr, GFP_KERNEL); 750 if (!buffer->vaddr) 751 return -ENOMEM; 752 buffer->size = size; 753 754 return 0; 755 } 756 757 static void allegro_free_buffer(struct allegro_dev *dev, 758 struct allegro_buffer *buffer) 759 { 760 if (buffer->vaddr) { 761 dma_free_coherent(&dev->plat_dev->dev, buffer->size, 762 buffer->vaddr, buffer->paddr); 763 buffer->vaddr = NULL; 764 buffer->size = 0; 765 } 766 } 767 768 /* 769 * Mailbox interface to send messages to the MCU. 770 */ 771 772 static void allegro_mcu_interrupt(struct allegro_dev *dev); 773 static void allegro_handle_message(struct allegro_dev *dev, 774 union mcu_msg_response *msg); 775 776 static struct allegro_mbox *allegro_mbox_init(struct allegro_dev *dev, 777 unsigned int base, size_t size) 778 { 779 struct allegro_mbox *mbox; 780 781 mbox = devm_kmalloc(&dev->plat_dev->dev, sizeof(*mbox), GFP_KERNEL); 782 if (!mbox) 783 return ERR_PTR(-ENOMEM); 784 785 mbox->dev = dev; 786 787 mbox->head = base; 788 mbox->tail = base + 0x4; 789 mbox->data = base + 0x8; 790 mbox->size = size; 791 mutex_init(&mbox->lock); 792 793 regmap_write(dev->sram, mbox->head, 0); 794 regmap_write(dev->sram, mbox->tail, 0); 795 796 return mbox; 797 } 798 799 static int allegro_mbox_write(struct allegro_mbox *mbox, 800 const u32 *src, size_t size) 801 { 802 struct regmap *sram = mbox->dev->sram; 803 unsigned int tail; 804 size_t size_no_wrap; 805 int err = 0; 806 int stride = regmap_get_reg_stride(sram); 807 808 if (!src) 809 return -EINVAL; 810 811 if (size > mbox->size) 812 return -EINVAL; 813 814 mutex_lock(&mbox->lock); 815 regmap_read(sram, mbox->tail, &tail); 816 if (tail > mbox->size) { 817 err = -EIO; 818 goto out; 819 } 820 size_no_wrap = min(size, mbox->size - (size_t)tail); 821 regmap_bulk_write(sram, mbox->data + tail, 822 src, size_no_wrap / stride); 823 regmap_bulk_write(sram, mbox->data, 824 src + (size_no_wrap / sizeof(*src)), 825 (size - size_no_wrap) / stride); 826 regmap_write(sram, mbox->tail, (tail + size) % mbox->size); 827 828 out: 829 mutex_unlock(&mbox->lock); 830 831 return err; 832 } 833 834 static ssize_t allegro_mbox_read(struct allegro_mbox *mbox, 835 u32 *dst, size_t nbyte) 836 { 837 struct { 838 u16 length; 839 u16 type; 840 } __attribute__ ((__packed__)) *header; 841 struct regmap *sram = mbox->dev->sram; 842 unsigned int head; 843 ssize_t size; 844 size_t body_no_wrap; 845 int stride = regmap_get_reg_stride(sram); 846 847 regmap_read(sram, mbox->head, &head); 848 if (head > mbox->size) 849 return -EIO; 850 851 /* Assume that the header does not wrap. */ 852 regmap_bulk_read(sram, mbox->data + head, 853 dst, sizeof(*header) / stride); 854 header = (void *)dst; 855 size = header->length + sizeof(*header); 856 if (size > mbox->size || size & 0x3) 857 return -EIO; 858 if (size > nbyte) 859 return -EINVAL; 860 861 /* 862 * The message might wrap within the mailbox. If the message does not 863 * wrap, the first read will read the entire message, otherwise the 864 * first read will read message until the end of the mailbox and the 865 * second read will read the remaining bytes from the beginning of the 866 * mailbox. 867 * 868 * Skip the header, as was already read to get the size of the body. 869 */ 870 body_no_wrap = min((size_t)header->length, 871 (size_t)(mbox->size - (head + sizeof(*header)))); 872 regmap_bulk_read(sram, mbox->data + head + sizeof(*header), 873 dst + (sizeof(*header) / sizeof(*dst)), 874 body_no_wrap / stride); 875 regmap_bulk_read(sram, mbox->data, 876 dst + (sizeof(*header) + body_no_wrap) / sizeof(*dst), 877 (header->length - body_no_wrap) / stride); 878 879 regmap_write(sram, mbox->head, (head + size) % mbox->size); 880 881 return size; 882 } 883 884 /** 885 * allegro_mbox_send() - Send a message via the mailbox 886 * @mbox: the mailbox which is used to send the message 887 * @msg: the message to send 888 */ 889 static int allegro_mbox_send(struct allegro_mbox *mbox, void *msg) 890 { 891 struct allegro_dev *dev = mbox->dev; 892 ssize_t size; 893 int err; 894 u32 *tmp; 895 896 tmp = kzalloc(mbox->size, GFP_KERNEL); 897 if (!tmp) { 898 err = -ENOMEM; 899 goto out; 900 } 901 902 size = allegro_encode_mail(tmp, msg); 903 904 err = allegro_mbox_write(mbox, tmp, size); 905 kfree(tmp); 906 if (err) 907 goto out; 908 909 allegro_mcu_interrupt(dev); 910 911 out: 912 return err; 913 } 914 915 /** 916 * allegro_mbox_notify() - Notify the mailbox about a new message 917 * @mbox: The allegro_mbox to notify 918 */ 919 static void allegro_mbox_notify(struct allegro_mbox *mbox) 920 { 921 struct allegro_dev *dev = mbox->dev; 922 union mcu_msg_response *msg; 923 ssize_t size; 924 u32 *tmp; 925 int err; 926 927 msg = kmalloc(sizeof(*msg), GFP_KERNEL); 928 if (!msg) 929 return; 930 931 msg->header.version = dev->fw_info->mailbox_version; 932 933 tmp = kmalloc(mbox->size, GFP_KERNEL); 934 if (!tmp) 935 goto out; 936 937 size = allegro_mbox_read(mbox, tmp, mbox->size); 938 if (size < 0) 939 goto out; 940 941 err = allegro_decode_mail(msg, tmp); 942 if (err) 943 goto out; 944 945 allegro_handle_message(dev, msg); 946 947 out: 948 kfree(tmp); 949 kfree(msg); 950 } 951 952 static int allegro_encoder_buffer_init(struct allegro_dev *dev, 953 struct allegro_encoder_buffer *buffer) 954 { 955 int err; 956 struct regmap *settings = dev->settings; 957 unsigned int supports_10_bit; 958 unsigned int memory_depth; 959 unsigned int num_cores; 960 unsigned int color_depth; 961 unsigned long clk_rate; 962 963 /* We don't support the encoder buffer pre Firmware version 2019.2 */ 964 if (dev->fw_info->mailbox_version < MCU_MSG_VERSION_2019_2) 965 return -ENODEV; 966 967 if (!settings) 968 return -EINVAL; 969 970 err = regmap_read(settings, VCU_ENC_COLOR_DEPTH, &supports_10_bit); 971 if (err < 0) 972 return err; 973 err = regmap_read(settings, VCU_MEMORY_DEPTH, &memory_depth); 974 if (err < 0) 975 return err; 976 err = regmap_read(settings, VCU_NUM_CORE, &num_cores); 977 if (err < 0) 978 return err; 979 980 clk_rate = clk_get_rate(dev->clk_core); 981 if (clk_rate == 0) 982 return -EINVAL; 983 984 color_depth = supports_10_bit ? 10 : 8; 985 /* The firmware expects the encoder buffer size in bits. */ 986 buffer->size = color_depth * 32 * memory_depth; 987 buffer->color_depth = color_depth; 988 buffer->num_cores = num_cores; 989 buffer->clk_rate = clk_rate; 990 991 v4l2_dbg(1, debug, &dev->v4l2_dev, 992 "using %d bits encoder buffer with %d-bit color depth\n", 993 buffer->size, color_depth); 994 995 return 0; 996 } 997 998 static void allegro_mcu_send_init(struct allegro_dev *dev, 999 dma_addr_t suballoc_dma, size_t suballoc_size) 1000 { 1001 struct mcu_msg_init_request msg; 1002 1003 memset(&msg, 0, sizeof(msg)); 1004 1005 msg.header.type = MCU_MSG_TYPE_INIT; 1006 msg.header.version = dev->fw_info->mailbox_version; 1007 1008 msg.suballoc_dma = to_mcu_addr(dev, suballoc_dma); 1009 msg.suballoc_size = to_mcu_size(dev, suballoc_size); 1010 1011 if (dev->has_encoder_buffer) { 1012 msg.encoder_buffer_size = dev->encoder_buffer.size; 1013 msg.encoder_buffer_color_depth = dev->encoder_buffer.color_depth; 1014 msg.num_cores = dev->encoder_buffer.num_cores; 1015 msg.clk_rate = dev->encoder_buffer.clk_rate; 1016 } else { 1017 msg.encoder_buffer_size = -1; 1018 msg.encoder_buffer_color_depth = -1; 1019 msg.num_cores = -1; 1020 msg.clk_rate = -1; 1021 } 1022 1023 allegro_mbox_send(dev->mbox_command, &msg); 1024 } 1025 1026 static u32 v4l2_pixelformat_to_mcu_format(u32 pixelformat) 1027 { 1028 switch (pixelformat) { 1029 case V4L2_PIX_FMT_NV12: 1030 /* AL_420_8BITS: 0x100 -> NV12, 0x88 -> 8 bit */ 1031 return 0x100 | 0x88; 1032 default: 1033 return -EINVAL; 1034 } 1035 } 1036 1037 static u32 v4l2_colorspace_to_mcu_colorspace(enum v4l2_colorspace colorspace) 1038 { 1039 switch (colorspace) { 1040 case V4L2_COLORSPACE_REC709: 1041 return 2; 1042 case V4L2_COLORSPACE_SMPTE170M: 1043 return 3; 1044 case V4L2_COLORSPACE_SMPTE240M: 1045 return 4; 1046 case V4L2_COLORSPACE_SRGB: 1047 return 7; 1048 default: 1049 /* UNKNOWN */ 1050 return 0; 1051 } 1052 } 1053 1054 static u8 v4l2_profile_to_mcu_profile(enum v4l2_mpeg_video_h264_profile profile) 1055 { 1056 switch (profile) { 1057 case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE: 1058 default: 1059 return 66; 1060 } 1061 } 1062 1063 static u16 v4l2_level_to_mcu_level(enum v4l2_mpeg_video_h264_level level) 1064 { 1065 switch (level) { 1066 case V4L2_MPEG_VIDEO_H264_LEVEL_1_0: 1067 return 10; 1068 case V4L2_MPEG_VIDEO_H264_LEVEL_1_1: 1069 return 11; 1070 case V4L2_MPEG_VIDEO_H264_LEVEL_1_2: 1071 return 12; 1072 case V4L2_MPEG_VIDEO_H264_LEVEL_1_3: 1073 return 13; 1074 case V4L2_MPEG_VIDEO_H264_LEVEL_2_0: 1075 return 20; 1076 case V4L2_MPEG_VIDEO_H264_LEVEL_2_1: 1077 return 21; 1078 case V4L2_MPEG_VIDEO_H264_LEVEL_2_2: 1079 return 22; 1080 case V4L2_MPEG_VIDEO_H264_LEVEL_3_0: 1081 return 30; 1082 case V4L2_MPEG_VIDEO_H264_LEVEL_3_1: 1083 return 31; 1084 case V4L2_MPEG_VIDEO_H264_LEVEL_3_2: 1085 return 32; 1086 case V4L2_MPEG_VIDEO_H264_LEVEL_4_0: 1087 return 40; 1088 case V4L2_MPEG_VIDEO_H264_LEVEL_4_1: 1089 return 41; 1090 case V4L2_MPEG_VIDEO_H264_LEVEL_4_2: 1091 return 42; 1092 case V4L2_MPEG_VIDEO_H264_LEVEL_5_0: 1093 return 50; 1094 case V4L2_MPEG_VIDEO_H264_LEVEL_5_1: 1095 default: 1096 return 51; 1097 } 1098 } 1099 1100 static u8 hevc_profile_to_mcu_profile(enum v4l2_mpeg_video_hevc_profile profile) 1101 { 1102 switch (profile) { 1103 default: 1104 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN: 1105 return 1; 1106 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10: 1107 return 2; 1108 case V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE: 1109 return 3; 1110 } 1111 } 1112 1113 static u16 hevc_level_to_mcu_level(enum v4l2_mpeg_video_hevc_level level) 1114 { 1115 switch (level) { 1116 case V4L2_MPEG_VIDEO_HEVC_LEVEL_1: 1117 return 10; 1118 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2: 1119 return 20; 1120 case V4L2_MPEG_VIDEO_HEVC_LEVEL_2_1: 1121 return 21; 1122 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3: 1123 return 30; 1124 case V4L2_MPEG_VIDEO_HEVC_LEVEL_3_1: 1125 return 31; 1126 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4: 1127 return 40; 1128 case V4L2_MPEG_VIDEO_HEVC_LEVEL_4_1: 1129 return 41; 1130 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5: 1131 return 50; 1132 default: 1133 case V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1: 1134 return 51; 1135 } 1136 } 1137 1138 static u8 hevc_tier_to_mcu_tier(enum v4l2_mpeg_video_hevc_tier tier) 1139 { 1140 switch (tier) { 1141 default: 1142 case V4L2_MPEG_VIDEO_HEVC_TIER_MAIN: 1143 return 0; 1144 case V4L2_MPEG_VIDEO_HEVC_TIER_HIGH: 1145 return 1; 1146 } 1147 } 1148 1149 static u32 1150 v4l2_bitrate_mode_to_mcu_mode(enum v4l2_mpeg_video_bitrate_mode mode) 1151 { 1152 switch (mode) { 1153 case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR: 1154 return 2; 1155 case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR: 1156 default: 1157 return 1; 1158 } 1159 } 1160 1161 static u32 v4l2_cpb_size_to_mcu(unsigned int cpb_size, unsigned int bitrate) 1162 { 1163 unsigned int cpb_size_kbit; 1164 unsigned int bitrate_kbps; 1165 1166 /* 1167 * The mcu expects the CPB size in units of a 90 kHz clock, but the 1168 * channel follows the V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE and stores 1169 * the CPB size in kilobytes. 1170 */ 1171 cpb_size_kbit = cpb_size * BITS_PER_BYTE; 1172 bitrate_kbps = bitrate / 1000; 1173 1174 return (cpb_size_kbit * 90000) / bitrate_kbps; 1175 } 1176 1177 static s16 get_qp_delta(int minuend, int subtrahend) 1178 { 1179 if (minuend == subtrahend) 1180 return -1; 1181 else 1182 return minuend - subtrahend; 1183 } 1184 1185 static u32 allegro_channel_get_entropy_mode(struct allegro_channel *channel) 1186 { 1187 #define ALLEGRO_ENTROPY_MODE_CAVLC 0 1188 #define ALLEGRO_ENTROPY_MODE_CABAC 1 1189 1190 /* HEVC always uses CABAC, but this has to be explicitly set */ 1191 if (channel->codec == V4L2_PIX_FMT_HEVC) 1192 return ALLEGRO_ENTROPY_MODE_CABAC; 1193 1194 return ALLEGRO_ENTROPY_MODE_CAVLC; 1195 } 1196 1197 static int fill_create_channel_param(struct allegro_channel *channel, 1198 struct create_channel_param *param) 1199 { 1200 int i_frame_qp = allegro_channel_get_i_frame_qp(channel); 1201 int p_frame_qp = allegro_channel_get_p_frame_qp(channel); 1202 int b_frame_qp = allegro_channel_get_b_frame_qp(channel); 1203 int bitrate_mode = v4l2_ctrl_g_ctrl(channel->mpeg_video_bitrate_mode); 1204 unsigned int cpb_size = v4l2_ctrl_g_ctrl(channel->mpeg_video_cpb_size); 1205 1206 param->width = channel->width; 1207 param->height = channel->height; 1208 param->format = v4l2_pixelformat_to_mcu_format(channel->pixelformat); 1209 param->colorspace = 1210 v4l2_colorspace_to_mcu_colorspace(channel->colorspace); 1211 param->src_mode = 0x0; 1212 1213 param->codec = channel->codec; 1214 if (channel->codec == V4L2_PIX_FMT_H264) { 1215 enum v4l2_mpeg_video_h264_profile profile; 1216 enum v4l2_mpeg_video_h264_level level; 1217 1218 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_profile); 1219 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_level); 1220 1221 param->profile = v4l2_profile_to_mcu_profile(profile); 1222 param->constraint_set_flags = BIT(1); 1223 param->level = v4l2_level_to_mcu_level(level); 1224 } else { 1225 enum v4l2_mpeg_video_hevc_profile profile; 1226 enum v4l2_mpeg_video_hevc_level level; 1227 enum v4l2_mpeg_video_hevc_tier tier; 1228 1229 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_profile); 1230 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level); 1231 tier = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_tier); 1232 1233 param->profile = hevc_profile_to_mcu_profile(profile); 1234 param->level = hevc_level_to_mcu_level(level); 1235 param->tier = hevc_tier_to_mcu_tier(tier); 1236 } 1237 1238 param->log2_max_poc = LOG2_MAX_PIC_ORDER_CNT; 1239 param->log2_max_frame_num = channel->log2_max_frame_num; 1240 param->temporal_mvp_enable = channel->temporal_mvp_enable; 1241 1242 param->dbf_ovr_en = channel->dbf_ovr_en; 1243 param->override_lf = channel->enable_deblocking_filter_override; 1244 param->enable_reordering = channel->enable_reordering; 1245 param->entropy_mode = allegro_channel_get_entropy_mode(channel); 1246 param->rdo_cost_mode = 1; 1247 param->custom_lda = 1; 1248 param->lf = 1; 1249 param->lf_x_tile = channel->enable_loop_filter_across_tiles; 1250 param->lf_x_slice = channel->enable_loop_filter_across_slices; 1251 1252 param->src_bit_depth = 8; 1253 1254 param->beta_offset = BETA_OFFSET_DIV_2; 1255 param->tc_offset = TC_OFFSET_DIV_2; 1256 param->num_slices = 1; 1257 param->me_range[0] = channel->b_hrz_me_range; 1258 param->me_range[1] = channel->b_vrt_me_range; 1259 param->me_range[2] = channel->p_hrz_me_range; 1260 param->me_range[3] = channel->p_vrt_me_range; 1261 param->max_cu_size = channel->max_cu_size; 1262 param->min_cu_size = channel->min_cu_size; 1263 param->max_tu_size = channel->max_tu_size; 1264 param->min_tu_size = channel->min_tu_size; 1265 param->max_transfo_depth_intra = channel->max_transfo_depth_intra; 1266 param->max_transfo_depth_inter = channel->max_transfo_depth_inter; 1267 1268 param->encoder_buffer_enabled = v4l2_ctrl_g_ctrl(channel->encoder_buffer); 1269 param->encoder_buffer_offset = 0; 1270 1271 param->rate_control_mode = channel->frame_rc_enable ? 1272 v4l2_bitrate_mode_to_mcu_mode(bitrate_mode) : 0; 1273 1274 param->cpb_size = v4l2_cpb_size_to_mcu(cpb_size, channel->bitrate_peak); 1275 /* Shall be ]0;cpb_size in 90 kHz units]. Use maximum value. */ 1276 param->initial_rem_delay = param->cpb_size; 1277 param->framerate = DIV_ROUND_UP(channel->framerate.numerator, 1278 channel->framerate.denominator); 1279 param->clk_ratio = channel->framerate.denominator == 1001 ? 1001 : 1000; 1280 param->target_bitrate = channel->bitrate; 1281 param->max_bitrate = channel->bitrate_peak; 1282 param->initial_qp = i_frame_qp; 1283 param->min_qp = allegro_channel_get_min_qp(channel); 1284 param->max_qp = allegro_channel_get_max_qp(channel); 1285 param->ip_delta = get_qp_delta(i_frame_qp, p_frame_qp); 1286 param->pb_delta = get_qp_delta(p_frame_qp, b_frame_qp); 1287 param->golden_ref = 0; 1288 param->golden_delta = 2; 1289 param->golden_ref_frequency = 10; 1290 param->rate_control_option = 0x00000000; 1291 1292 param->num_pixel = channel->width + channel->height; 1293 param->max_psnr = 4200; 1294 param->max_pixel_value = 255; 1295 1296 param->gop_ctrl_mode = 0x00000002; 1297 param->freq_idr = v4l2_ctrl_g_ctrl(channel->mpeg_video_gop_size); 1298 param->freq_lt = 0; 1299 param->gdr_mode = 0x00000000; 1300 param->gop_length = v4l2_ctrl_g_ctrl(channel->mpeg_video_gop_size); 1301 param->subframe_latency = 0x00000000; 1302 1303 param->lda_factors[0] = 51; 1304 param->lda_factors[1] = 90; 1305 param->lda_factors[2] = 151; 1306 param->lda_factors[3] = 151; 1307 param->lda_factors[4] = 151; 1308 param->lda_factors[5] = 151; 1309 1310 param->max_num_merge_cand = 5; 1311 1312 return 0; 1313 } 1314 1315 static int allegro_mcu_send_create_channel(struct allegro_dev *dev, 1316 struct allegro_channel *channel) 1317 { 1318 struct mcu_msg_create_channel msg; 1319 struct allegro_buffer *blob = &channel->config_blob; 1320 struct create_channel_param param; 1321 size_t size; 1322 1323 memset(¶m, 0, sizeof(param)); 1324 fill_create_channel_param(channel, ¶m); 1325 allegro_alloc_buffer(dev, blob, sizeof(struct create_channel_param)); 1326 param.version = dev->fw_info->mailbox_version; 1327 size = allegro_encode_config_blob(blob->vaddr, ¶m); 1328 1329 memset(&msg, 0, sizeof(msg)); 1330 1331 msg.header.type = MCU_MSG_TYPE_CREATE_CHANNEL; 1332 msg.header.version = dev->fw_info->mailbox_version; 1333 1334 msg.user_id = channel->user_id; 1335 1336 msg.blob = blob->vaddr; 1337 msg.blob_size = size; 1338 msg.blob_mcu_addr = to_mcu_addr(dev, blob->paddr); 1339 1340 allegro_mbox_send(dev->mbox_command, &msg); 1341 1342 return 0; 1343 } 1344 1345 static int allegro_mcu_send_destroy_channel(struct allegro_dev *dev, 1346 struct allegro_channel *channel) 1347 { 1348 struct mcu_msg_destroy_channel msg; 1349 1350 memset(&msg, 0, sizeof(msg)); 1351 1352 msg.header.type = MCU_MSG_TYPE_DESTROY_CHANNEL; 1353 msg.header.version = dev->fw_info->mailbox_version; 1354 1355 msg.channel_id = channel->mcu_channel_id; 1356 1357 allegro_mbox_send(dev->mbox_command, &msg); 1358 1359 return 0; 1360 } 1361 1362 static int allegro_mcu_send_put_stream_buffer(struct allegro_dev *dev, 1363 struct allegro_channel *channel, 1364 dma_addr_t paddr, 1365 unsigned long size, 1366 u64 dst_handle) 1367 { 1368 struct mcu_msg_put_stream_buffer msg; 1369 1370 memset(&msg, 0, sizeof(msg)); 1371 1372 msg.header.type = MCU_MSG_TYPE_PUT_STREAM_BUFFER; 1373 msg.header.version = dev->fw_info->mailbox_version; 1374 1375 msg.channel_id = channel->mcu_channel_id; 1376 msg.dma_addr = to_codec_addr(dev, paddr); 1377 msg.mcu_addr = to_mcu_addr(dev, paddr); 1378 msg.size = size; 1379 msg.offset = ENCODER_STREAM_OFFSET; 1380 /* copied to mcu_msg_encode_frame_response */ 1381 msg.dst_handle = dst_handle; 1382 1383 allegro_mbox_send(dev->mbox_command, &msg); 1384 1385 return 0; 1386 } 1387 1388 static int allegro_mcu_send_encode_frame(struct allegro_dev *dev, 1389 struct allegro_channel *channel, 1390 dma_addr_t src_y, dma_addr_t src_uv, 1391 u64 src_handle) 1392 { 1393 struct mcu_msg_encode_frame msg; 1394 bool use_encoder_buffer = v4l2_ctrl_g_ctrl(channel->encoder_buffer); 1395 1396 memset(&msg, 0, sizeof(msg)); 1397 1398 msg.header.type = MCU_MSG_TYPE_ENCODE_FRAME; 1399 msg.header.version = dev->fw_info->mailbox_version; 1400 1401 msg.channel_id = channel->mcu_channel_id; 1402 msg.encoding_options = AL_OPT_FORCE_LOAD; 1403 if (use_encoder_buffer) 1404 msg.encoding_options |= AL_OPT_USE_L2; 1405 msg.pps_qp = 26; /* qp are relative to 26 */ 1406 msg.user_param = 0; /* copied to mcu_msg_encode_frame_response */ 1407 /* src_handle is copied to mcu_msg_encode_frame_response */ 1408 msg.src_handle = src_handle; 1409 msg.src_y = to_codec_addr(dev, src_y); 1410 msg.src_uv = to_codec_addr(dev, src_uv); 1411 msg.stride = channel->stride; 1412 1413 allegro_mbox_send(dev->mbox_command, &msg); 1414 1415 return 0; 1416 } 1417 1418 static int allegro_mcu_wait_for_init_timeout(struct allegro_dev *dev, 1419 unsigned long timeout_ms) 1420 { 1421 unsigned long time_left; 1422 1423 time_left = wait_for_completion_timeout(&dev->init_complete, 1424 msecs_to_jiffies(timeout_ms)); 1425 if (time_left == 0) 1426 return -ETIMEDOUT; 1427 1428 reinit_completion(&dev->init_complete); 1429 return 0; 1430 } 1431 1432 static int allegro_mcu_push_buffer_internal(struct allegro_channel *channel, 1433 enum mcu_msg_type type) 1434 { 1435 struct allegro_dev *dev = channel->dev; 1436 struct mcu_msg_push_buffers_internal *msg; 1437 struct mcu_msg_push_buffers_internal_buffer *buffer; 1438 unsigned int num_buffers = 0; 1439 size_t size; 1440 struct allegro_buffer *al_buffer; 1441 struct list_head *list; 1442 int err; 1443 1444 switch (type) { 1445 case MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE: 1446 list = &channel->buffers_reference; 1447 break; 1448 case MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE: 1449 list = &channel->buffers_intermediate; 1450 break; 1451 default: 1452 return -EINVAL; 1453 } 1454 1455 list_for_each_entry(al_buffer, list, head) 1456 num_buffers++; 1457 size = struct_size(msg, buffer, num_buffers); 1458 1459 msg = kmalloc(size, GFP_KERNEL); 1460 if (!msg) 1461 return -ENOMEM; 1462 1463 msg->header.type = type; 1464 msg->header.version = dev->fw_info->mailbox_version; 1465 1466 msg->channel_id = channel->mcu_channel_id; 1467 msg->num_buffers = num_buffers; 1468 1469 buffer = msg->buffer; 1470 list_for_each_entry(al_buffer, list, head) { 1471 buffer->dma_addr = to_codec_addr(dev, al_buffer->paddr); 1472 buffer->mcu_addr = to_mcu_addr(dev, al_buffer->paddr); 1473 buffer->size = to_mcu_size(dev, al_buffer->size); 1474 buffer++; 1475 } 1476 1477 err = allegro_mbox_send(dev->mbox_command, msg); 1478 1479 kfree(msg); 1480 return err; 1481 } 1482 1483 static int allegro_mcu_push_buffer_intermediate(struct allegro_channel *channel) 1484 { 1485 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_INTERMEDIATE; 1486 1487 return allegro_mcu_push_buffer_internal(channel, type); 1488 } 1489 1490 static int allegro_mcu_push_buffer_reference(struct allegro_channel *channel) 1491 { 1492 enum mcu_msg_type type = MCU_MSG_TYPE_PUSH_BUFFER_REFERENCE; 1493 1494 return allegro_mcu_push_buffer_internal(channel, type); 1495 } 1496 1497 static int allocate_buffers_internal(struct allegro_channel *channel, 1498 struct list_head *list, 1499 size_t n, size_t size) 1500 { 1501 struct allegro_dev *dev = channel->dev; 1502 unsigned int i; 1503 int err; 1504 struct allegro_buffer *buffer, *tmp; 1505 1506 for (i = 0; i < n; i++) { 1507 buffer = kmalloc(sizeof(*buffer), GFP_KERNEL); 1508 if (!buffer) { 1509 err = -ENOMEM; 1510 goto err; 1511 } 1512 INIT_LIST_HEAD(&buffer->head); 1513 1514 err = allegro_alloc_buffer(dev, buffer, size); 1515 if (err) { 1516 kfree(buffer); 1517 goto err; 1518 } 1519 list_add(&buffer->head, list); 1520 } 1521 1522 return 0; 1523 1524 err: 1525 list_for_each_entry_safe(buffer, tmp, list, head) { 1526 list_del(&buffer->head); 1527 allegro_free_buffer(dev, buffer); 1528 kfree(buffer); 1529 } 1530 return err; 1531 } 1532 1533 static void destroy_buffers_internal(struct allegro_channel *channel, 1534 struct list_head *list) 1535 { 1536 struct allegro_dev *dev = channel->dev; 1537 struct allegro_buffer *buffer, *tmp; 1538 1539 list_for_each_entry_safe(buffer, tmp, list, head) { 1540 list_del(&buffer->head); 1541 allegro_free_buffer(dev, buffer); 1542 kfree(buffer); 1543 } 1544 } 1545 1546 static void destroy_reference_buffers(struct allegro_channel *channel) 1547 { 1548 return destroy_buffers_internal(channel, &channel->buffers_reference); 1549 } 1550 1551 static void destroy_intermediate_buffers(struct allegro_channel *channel) 1552 { 1553 return destroy_buffers_internal(channel, 1554 &channel->buffers_intermediate); 1555 } 1556 1557 static int allocate_intermediate_buffers(struct allegro_channel *channel, 1558 size_t n, size_t size) 1559 { 1560 return allocate_buffers_internal(channel, 1561 &channel->buffers_intermediate, 1562 n, size); 1563 } 1564 1565 static int allocate_reference_buffers(struct allegro_channel *channel, 1566 size_t n, size_t size) 1567 { 1568 return allocate_buffers_internal(channel, 1569 &channel->buffers_reference, 1570 n, PAGE_ALIGN(size)); 1571 } 1572 1573 static ssize_t allegro_h264_write_sps(struct allegro_channel *channel, 1574 void *dest, size_t n) 1575 { 1576 struct allegro_dev *dev = channel->dev; 1577 struct nal_h264_sps *sps; 1578 ssize_t size; 1579 unsigned int size_mb = SIZE_MACROBLOCK; 1580 /* Calculation of crop units in Rec. ITU-T H.264 (04/2017) p. 76 */ 1581 unsigned int crop_unit_x = 2; 1582 unsigned int crop_unit_y = 2; 1583 enum v4l2_mpeg_video_h264_profile profile; 1584 enum v4l2_mpeg_video_h264_level level; 1585 unsigned int cpb_size; 1586 unsigned int cpb_size_scale; 1587 1588 sps = kzalloc(sizeof(*sps), GFP_KERNEL); 1589 if (!sps) 1590 return -ENOMEM; 1591 1592 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_profile); 1593 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_level); 1594 1595 sps->profile_idc = nal_h264_profile(profile); 1596 sps->constraint_set0_flag = 0; 1597 sps->constraint_set1_flag = 1; 1598 sps->constraint_set2_flag = 0; 1599 sps->constraint_set3_flag = 0; 1600 sps->constraint_set4_flag = 0; 1601 sps->constraint_set5_flag = 0; 1602 sps->level_idc = nal_h264_level(level); 1603 sps->seq_parameter_set_id = 0; 1604 sps->log2_max_frame_num_minus4 = LOG2_MAX_FRAME_NUM - 4; 1605 sps->pic_order_cnt_type = 0; 1606 sps->log2_max_pic_order_cnt_lsb_minus4 = LOG2_MAX_PIC_ORDER_CNT - 4; 1607 sps->max_num_ref_frames = 3; 1608 sps->gaps_in_frame_num_value_allowed_flag = 0; 1609 sps->pic_width_in_mbs_minus1 = 1610 DIV_ROUND_UP(channel->width, size_mb) - 1; 1611 sps->pic_height_in_map_units_minus1 = 1612 DIV_ROUND_UP(channel->height, size_mb) - 1; 1613 sps->frame_mbs_only_flag = 1; 1614 sps->mb_adaptive_frame_field_flag = 0; 1615 sps->direct_8x8_inference_flag = 1; 1616 sps->frame_cropping_flag = 1617 (channel->width % size_mb) || (channel->height % size_mb); 1618 if (sps->frame_cropping_flag) { 1619 sps->crop_left = 0; 1620 sps->crop_right = (round_up(channel->width, size_mb) - channel->width) / crop_unit_x; 1621 sps->crop_top = 0; 1622 sps->crop_bottom = (round_up(channel->height, size_mb) - channel->height) / crop_unit_y; 1623 } 1624 sps->vui_parameters_present_flag = 1; 1625 sps->vui.aspect_ratio_info_present_flag = 0; 1626 sps->vui.overscan_info_present_flag = 0; 1627 1628 sps->vui.video_signal_type_present_flag = 1; 1629 sps->vui.video_format = 5; /* unspecified */ 1630 sps->vui.video_full_range_flag = nal_h264_full_range(channel->quantization); 1631 sps->vui.colour_description_present_flag = 1; 1632 sps->vui.colour_primaries = nal_h264_color_primaries(channel->colorspace); 1633 sps->vui.transfer_characteristics = 1634 nal_h264_transfer_characteristics(channel->colorspace, channel->xfer_func); 1635 sps->vui.matrix_coefficients = 1636 nal_h264_matrix_coeffs(channel->colorspace, channel->ycbcr_enc); 1637 1638 sps->vui.chroma_loc_info_present_flag = 1; 1639 sps->vui.chroma_sample_loc_type_top_field = 0; 1640 sps->vui.chroma_sample_loc_type_bottom_field = 0; 1641 1642 sps->vui.timing_info_present_flag = 1; 1643 sps->vui.num_units_in_tick = channel->framerate.denominator; 1644 sps->vui.time_scale = 2 * channel->framerate.numerator; 1645 1646 sps->vui.fixed_frame_rate_flag = 1; 1647 sps->vui.nal_hrd_parameters_present_flag = 0; 1648 sps->vui.vcl_hrd_parameters_present_flag = 1; 1649 sps->vui.vcl_hrd_parameters.cpb_cnt_minus1 = 0; 1650 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-53 */ 1651 sps->vui.vcl_hrd_parameters.bit_rate_scale = 1652 ffs(channel->bitrate_peak) - 6; 1653 sps->vui.vcl_hrd_parameters.bit_rate_value_minus1[0] = 1654 channel->bitrate_peak / (1 << (6 + sps->vui.vcl_hrd_parameters.bit_rate_scale)) - 1; 1655 /* See Rec. ITU-T H.264 (04/2017) p. 410 E-54 */ 1656 cpb_size = v4l2_ctrl_g_ctrl(channel->mpeg_video_cpb_size); 1657 cpb_size_scale = ffs(cpb_size) - 4; 1658 sps->vui.vcl_hrd_parameters.cpb_size_scale = cpb_size_scale; 1659 sps->vui.vcl_hrd_parameters.cpb_size_value_minus1[0] = 1660 (cpb_size * 1000) / (1 << (4 + cpb_size_scale)) - 1; 1661 sps->vui.vcl_hrd_parameters.cbr_flag[0] = 1662 !v4l2_ctrl_g_ctrl(channel->mpeg_video_frame_rc_enable); 1663 sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 = 31; 1664 sps->vui.vcl_hrd_parameters.cpb_removal_delay_length_minus1 = 31; 1665 sps->vui.vcl_hrd_parameters.dpb_output_delay_length_minus1 = 31; 1666 sps->vui.vcl_hrd_parameters.time_offset_length = 0; 1667 sps->vui.low_delay_hrd_flag = 0; 1668 sps->vui.pic_struct_present_flag = 1; 1669 sps->vui.bitstream_restriction_flag = 0; 1670 1671 size = nal_h264_write_sps(&dev->plat_dev->dev, dest, n, sps); 1672 1673 kfree(sps); 1674 1675 return size; 1676 } 1677 1678 static ssize_t allegro_h264_write_pps(struct allegro_channel *channel, 1679 void *dest, size_t n) 1680 { 1681 struct allegro_dev *dev = channel->dev; 1682 struct nal_h264_pps *pps; 1683 ssize_t size; 1684 1685 pps = kzalloc(sizeof(*pps), GFP_KERNEL); 1686 if (!pps) 1687 return -ENOMEM; 1688 1689 pps->pic_parameter_set_id = 0; 1690 pps->seq_parameter_set_id = 0; 1691 pps->entropy_coding_mode_flag = 0; 1692 pps->bottom_field_pic_order_in_frame_present_flag = 0; 1693 pps->num_slice_groups_minus1 = 0; 1694 pps->num_ref_idx_l0_default_active_minus1 = channel->num_ref_idx_l0 - 1; 1695 pps->num_ref_idx_l1_default_active_minus1 = channel->num_ref_idx_l1 - 1; 1696 pps->weighted_pred_flag = 0; 1697 pps->weighted_bipred_idc = 0; 1698 pps->pic_init_qp_minus26 = 0; 1699 pps->pic_init_qs_minus26 = 0; 1700 pps->chroma_qp_index_offset = 0; 1701 pps->deblocking_filter_control_present_flag = 1; 1702 pps->constrained_intra_pred_flag = 0; 1703 pps->redundant_pic_cnt_present_flag = 0; 1704 pps->transform_8x8_mode_flag = 0; 1705 pps->pic_scaling_matrix_present_flag = 0; 1706 pps->second_chroma_qp_index_offset = 0; 1707 1708 size = nal_h264_write_pps(&dev->plat_dev->dev, dest, n, pps); 1709 1710 kfree(pps); 1711 1712 return size; 1713 } 1714 1715 static void allegro_channel_eos_event(struct allegro_channel *channel) 1716 { 1717 const struct v4l2_event eos_event = { 1718 .type = V4L2_EVENT_EOS 1719 }; 1720 1721 v4l2_event_queue_fh(&channel->fh, &eos_event); 1722 } 1723 1724 static ssize_t allegro_hevc_write_vps(struct allegro_channel *channel, 1725 void *dest, size_t n) 1726 { 1727 struct allegro_dev *dev = channel->dev; 1728 struct nal_hevc_vps *vps; 1729 struct nal_hevc_profile_tier_level *ptl; 1730 ssize_t size; 1731 unsigned int num_ref_frames = channel->num_ref_idx_l0; 1732 s32 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_profile); 1733 s32 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level); 1734 s32 tier = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_tier); 1735 1736 vps = kzalloc(sizeof(*vps), GFP_KERNEL); 1737 if (!vps) 1738 return -ENOMEM; 1739 1740 vps->base_layer_internal_flag = 1; 1741 vps->base_layer_available_flag = 1; 1742 vps->temporal_id_nesting_flag = 1; 1743 1744 ptl = &vps->profile_tier_level; 1745 ptl->general_profile_idc = nal_hevc_profile(profile); 1746 ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1; 1747 ptl->general_tier_flag = nal_hevc_tier(tier); 1748 ptl->general_progressive_source_flag = 1; 1749 ptl->general_frame_only_constraint_flag = 1; 1750 ptl->general_level_idc = nal_hevc_level(level); 1751 1752 vps->sub_layer_ordering_info_present_flag = 0; 1753 vps->max_dec_pic_buffering_minus1[0] = num_ref_frames; 1754 vps->max_num_reorder_pics[0] = num_ref_frames; 1755 1756 size = nal_hevc_write_vps(&dev->plat_dev->dev, dest, n, vps); 1757 1758 kfree(vps); 1759 1760 return size; 1761 } 1762 1763 static ssize_t allegro_hevc_write_sps(struct allegro_channel *channel, 1764 void *dest, size_t n) 1765 { 1766 struct allegro_dev *dev = channel->dev; 1767 struct nal_hevc_sps *sps; 1768 struct nal_hevc_profile_tier_level *ptl; 1769 struct nal_hevc_vui_parameters *vui; 1770 struct nal_hevc_hrd_parameters *hrd; 1771 ssize_t size; 1772 unsigned int cpb_size; 1773 unsigned int num_ref_frames = channel->num_ref_idx_l0; 1774 s32 profile = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_profile); 1775 s32 level = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level); 1776 s32 tier = v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_tier); 1777 1778 sps = kzalloc(sizeof(*sps), GFP_KERNEL); 1779 if (!sps) 1780 return -ENOMEM; 1781 1782 sps->temporal_id_nesting_flag = 1; 1783 1784 ptl = &sps->profile_tier_level; 1785 ptl->general_profile_idc = nal_hevc_profile(profile); 1786 ptl->general_profile_compatibility_flag[ptl->general_profile_idc] = 1; 1787 ptl->general_tier_flag = nal_hevc_tier(tier); 1788 ptl->general_progressive_source_flag = 1; 1789 ptl->general_frame_only_constraint_flag = 1; 1790 ptl->general_level_idc = nal_hevc_level(level); 1791 1792 sps->seq_parameter_set_id = 0; 1793 sps->chroma_format_idc = 1; /* Only 4:2:0 sampling supported */ 1794 sps->pic_width_in_luma_samples = round_up(channel->width, 8); 1795 sps->pic_height_in_luma_samples = round_up(channel->height, 8); 1796 sps->conf_win_right_offset = 1797 sps->pic_width_in_luma_samples - channel->width; 1798 sps->conf_win_bottom_offset = 1799 sps->pic_height_in_luma_samples - channel->height; 1800 sps->conformance_window_flag = 1801 sps->conf_win_right_offset || sps->conf_win_bottom_offset; 1802 1803 sps->log2_max_pic_order_cnt_lsb_minus4 = LOG2_MAX_PIC_ORDER_CNT - 4; 1804 1805 sps->sub_layer_ordering_info_present_flag = 1; 1806 sps->max_dec_pic_buffering_minus1[0] = num_ref_frames; 1807 sps->max_num_reorder_pics[0] = num_ref_frames; 1808 1809 sps->log2_min_luma_coding_block_size_minus3 = 1810 channel->min_cu_size - 3; 1811 sps->log2_diff_max_min_luma_coding_block_size = 1812 channel->max_cu_size - channel->min_cu_size; 1813 sps->log2_min_luma_transform_block_size_minus2 = 1814 channel->min_tu_size - 2; 1815 sps->log2_diff_max_min_luma_transform_block_size = 1816 channel->max_tu_size - channel->min_tu_size; 1817 sps->max_transform_hierarchy_depth_intra = 1818 channel->max_transfo_depth_intra; 1819 sps->max_transform_hierarchy_depth_inter = 1820 channel->max_transfo_depth_inter; 1821 1822 sps->sps_temporal_mvp_enabled_flag = channel->temporal_mvp_enable; 1823 sps->strong_intra_smoothing_enabled_flag = channel->max_cu_size > 4; 1824 1825 sps->vui_parameters_present_flag = 1; 1826 vui = &sps->vui; 1827 1828 vui->video_signal_type_present_flag = 1; 1829 vui->video_format = 5; /* unspecified */ 1830 vui->video_full_range_flag = nal_hevc_full_range(channel->quantization); 1831 vui->colour_description_present_flag = 1; 1832 vui->colour_primaries = nal_hevc_color_primaries(channel->colorspace); 1833 vui->transfer_characteristics = nal_hevc_transfer_characteristics(channel->colorspace, 1834 channel->xfer_func); 1835 vui->matrix_coeffs = nal_hevc_matrix_coeffs(channel->colorspace, channel->ycbcr_enc); 1836 1837 vui->chroma_loc_info_present_flag = 1; 1838 vui->chroma_sample_loc_type_top_field = 0; 1839 vui->chroma_sample_loc_type_bottom_field = 0; 1840 1841 vui->vui_timing_info_present_flag = 1; 1842 vui->vui_num_units_in_tick = channel->framerate.denominator; 1843 vui->vui_time_scale = channel->framerate.numerator; 1844 1845 vui->bitstream_restriction_flag = 1; 1846 vui->motion_vectors_over_pic_boundaries_flag = 1; 1847 vui->restricted_ref_pic_lists_flag = 1; 1848 vui->log2_max_mv_length_horizontal = 15; 1849 vui->log2_max_mv_length_vertical = 15; 1850 1851 vui->vui_hrd_parameters_present_flag = 1; 1852 hrd = &vui->nal_hrd_parameters; 1853 hrd->vcl_hrd_parameters_present_flag = 1; 1854 1855 hrd->initial_cpb_removal_delay_length_minus1 = 31; 1856 hrd->au_cpb_removal_delay_length_minus1 = 30; 1857 hrd->dpb_output_delay_length_minus1 = 30; 1858 1859 hrd->bit_rate_scale = ffs(channel->bitrate_peak) - 6; 1860 hrd->vcl_hrd[0].bit_rate_value_minus1[0] = 1861 (channel->bitrate_peak >> (6 + hrd->bit_rate_scale)) - 1; 1862 1863 cpb_size = v4l2_ctrl_g_ctrl(channel->mpeg_video_cpb_size) * 1000; 1864 hrd->cpb_size_scale = ffs(cpb_size) - 4; 1865 hrd->vcl_hrd[0].cpb_size_value_minus1[0] = (cpb_size >> (4 + hrd->cpb_size_scale)) - 1; 1866 1867 hrd->vcl_hrd[0].cbr_flag[0] = !v4l2_ctrl_g_ctrl(channel->mpeg_video_frame_rc_enable); 1868 1869 size = nal_hevc_write_sps(&dev->plat_dev->dev, dest, n, sps); 1870 1871 kfree(sps); 1872 1873 return size; 1874 } 1875 1876 static ssize_t allegro_hevc_write_pps(struct allegro_channel *channel, 1877 struct mcu_msg_encode_frame_response *msg, 1878 void *dest, size_t n) 1879 { 1880 struct allegro_dev *dev = channel->dev; 1881 struct nal_hevc_pps *pps; 1882 ssize_t size; 1883 int i; 1884 1885 pps = kzalloc(sizeof(*pps), GFP_KERNEL); 1886 if (!pps) 1887 return -ENOMEM; 1888 1889 pps->pps_pic_parameter_set_id = 0; 1890 pps->pps_seq_parameter_set_id = 0; 1891 1892 if (msg->num_column > 1 || msg->num_row > 1) { 1893 pps->tiles_enabled_flag = 1; 1894 pps->num_tile_columns_minus1 = msg->num_column - 1; 1895 pps->num_tile_rows_minus1 = msg->num_row - 1; 1896 1897 for (i = 0; i < msg->num_column; i++) 1898 pps->column_width_minus1[i] = msg->tile_width[i] - 1; 1899 1900 for (i = 0; i < msg->num_row; i++) 1901 pps->row_height_minus1[i] = msg->tile_height[i] - 1; 1902 } 1903 1904 pps->loop_filter_across_tiles_enabled_flag = 1905 channel->enable_loop_filter_across_tiles; 1906 pps->pps_loop_filter_across_slices_enabled_flag = 1907 channel->enable_loop_filter_across_slices; 1908 pps->deblocking_filter_control_present_flag = 1; 1909 pps->deblocking_filter_override_enabled_flag = 1910 channel->enable_deblocking_filter_override; 1911 pps->pps_beta_offset_div2 = BETA_OFFSET_DIV_2; 1912 pps->pps_tc_offset_div2 = TC_OFFSET_DIV_2; 1913 1914 pps->lists_modification_present_flag = channel->enable_reordering; 1915 1916 size = nal_hevc_write_pps(&dev->plat_dev->dev, dest, n, pps); 1917 1918 kfree(pps); 1919 1920 return size; 1921 } 1922 1923 static u64 allegro_put_buffer(struct allegro_channel *channel, 1924 struct list_head *list, 1925 struct vb2_v4l2_buffer *buffer) 1926 { 1927 struct v4l2_m2m_buffer *b = container_of(buffer, 1928 struct v4l2_m2m_buffer, vb); 1929 struct allegro_m2m_buffer *shadow = to_allegro_m2m_buffer(b); 1930 1931 mutex_lock(&channel->shadow_list_lock); 1932 list_add_tail(&shadow->head, list); 1933 mutex_unlock(&channel->shadow_list_lock); 1934 1935 return ptr_to_u64(buffer); 1936 } 1937 1938 static struct vb2_v4l2_buffer * 1939 allegro_get_buffer(struct allegro_channel *channel, 1940 struct list_head *list, u64 handle) 1941 { 1942 struct allegro_m2m_buffer *shadow, *tmp; 1943 struct vb2_v4l2_buffer *buffer = NULL; 1944 1945 mutex_lock(&channel->shadow_list_lock); 1946 list_for_each_entry_safe(shadow, tmp, list, head) { 1947 if (handle == ptr_to_u64(&shadow->buf.vb)) { 1948 buffer = &shadow->buf.vb; 1949 list_del_init(&shadow->head); 1950 break; 1951 } 1952 } 1953 mutex_unlock(&channel->shadow_list_lock); 1954 1955 return buffer; 1956 } 1957 1958 static void allegro_channel_finish_frame(struct allegro_channel *channel, 1959 struct mcu_msg_encode_frame_response *msg) 1960 { 1961 struct allegro_dev *dev = channel->dev; 1962 struct vb2_v4l2_buffer *src_buf; 1963 struct vb2_v4l2_buffer *dst_buf; 1964 struct { 1965 u32 offset; 1966 u32 size; 1967 } *partition; 1968 enum vb2_buffer_state state = VB2_BUF_STATE_ERROR; 1969 char *curr; 1970 ssize_t len; 1971 ssize_t free; 1972 1973 src_buf = allegro_get_buffer(channel, &channel->source_shadow_list, 1974 msg->src_handle); 1975 if (!src_buf) 1976 v4l2_warn(&dev->v4l2_dev, 1977 "channel %d: invalid source buffer\n", 1978 channel->mcu_channel_id); 1979 1980 dst_buf = allegro_get_buffer(channel, &channel->stream_shadow_list, 1981 msg->dst_handle); 1982 if (!dst_buf) 1983 v4l2_warn(&dev->v4l2_dev, 1984 "channel %d: invalid stream buffer\n", 1985 channel->mcu_channel_id); 1986 1987 if (!src_buf || !dst_buf) 1988 goto err; 1989 1990 if (v4l2_m2m_is_last_draining_src_buf(channel->fh.m2m_ctx, src_buf)) { 1991 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 1992 allegro_channel_eos_event(channel); 1993 v4l2_m2m_mark_stopped(channel->fh.m2m_ctx); 1994 } 1995 1996 dst_buf->sequence = channel->csequence++; 1997 1998 if (msg->error_code & AL_ERROR) { 1999 v4l2_err(&dev->v4l2_dev, 2000 "channel %d: failed to encode frame: %s (%x)\n", 2001 channel->mcu_channel_id, 2002 allegro_err_to_string(msg->error_code), 2003 msg->error_code); 2004 goto err; 2005 } 2006 2007 if (msg->partition_table_size != 1) { 2008 v4l2_warn(&dev->v4l2_dev, 2009 "channel %d: only handling first partition table entry (%d entries)\n", 2010 channel->mcu_channel_id, msg->partition_table_size); 2011 } 2012 2013 if (msg->partition_table_offset + 2014 msg->partition_table_size * sizeof(*partition) > 2015 vb2_plane_size(&dst_buf->vb2_buf, 0)) { 2016 v4l2_err(&dev->v4l2_dev, 2017 "channel %d: partition table outside of dst_buf\n", 2018 channel->mcu_channel_id); 2019 goto err; 2020 } 2021 2022 partition = 2023 vb2_plane_vaddr(&dst_buf->vb2_buf, 0) + msg->partition_table_offset; 2024 if (partition->offset + partition->size > 2025 vb2_plane_size(&dst_buf->vb2_buf, 0)) { 2026 v4l2_err(&dev->v4l2_dev, 2027 "channel %d: encoded frame is outside of dst_buf (offset 0x%x, size 0x%x)\n", 2028 channel->mcu_channel_id, partition->offset, 2029 partition->size); 2030 goto err; 2031 } 2032 2033 v4l2_dbg(2, debug, &dev->v4l2_dev, 2034 "channel %d: encoded frame of size %d is at offset 0x%x\n", 2035 channel->mcu_channel_id, partition->size, partition->offset); 2036 2037 /* 2038 * The payload must include the data before the partition offset, 2039 * because we will put the sps and pps data there. 2040 */ 2041 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 2042 partition->offset + partition->size); 2043 2044 curr = vb2_plane_vaddr(&dst_buf->vb2_buf, 0); 2045 free = partition->offset; 2046 2047 if (channel->codec == V4L2_PIX_FMT_HEVC && msg->is_idr) { 2048 len = allegro_hevc_write_vps(channel, curr, free); 2049 if (len < 0) { 2050 v4l2_err(&dev->v4l2_dev, 2051 "not enough space for video parameter set: %zd left\n", 2052 free); 2053 goto err; 2054 } 2055 curr += len; 2056 free -= len; 2057 v4l2_dbg(1, debug, &dev->v4l2_dev, 2058 "channel %d: wrote %zd byte VPS nal unit\n", 2059 channel->mcu_channel_id, len); 2060 } 2061 2062 if (msg->is_idr) { 2063 if (channel->codec == V4L2_PIX_FMT_H264) 2064 len = allegro_h264_write_sps(channel, curr, free); 2065 else 2066 len = allegro_hevc_write_sps(channel, curr, free); 2067 if (len < 0) { 2068 v4l2_err(&dev->v4l2_dev, 2069 "not enough space for sequence parameter set: %zd left\n", 2070 free); 2071 goto err; 2072 } 2073 curr += len; 2074 free -= len; 2075 v4l2_dbg(1, debug, &dev->v4l2_dev, 2076 "channel %d: wrote %zd byte SPS nal unit\n", 2077 channel->mcu_channel_id, len); 2078 } 2079 2080 if (msg->slice_type == AL_ENC_SLICE_TYPE_I) { 2081 if (channel->codec == V4L2_PIX_FMT_H264) 2082 len = allegro_h264_write_pps(channel, curr, free); 2083 else 2084 len = allegro_hevc_write_pps(channel, msg, curr, free); 2085 if (len < 0) { 2086 v4l2_err(&dev->v4l2_dev, 2087 "not enough space for picture parameter set: %zd left\n", 2088 free); 2089 goto err; 2090 } 2091 curr += len; 2092 free -= len; 2093 v4l2_dbg(1, debug, &dev->v4l2_dev, 2094 "channel %d: wrote %zd byte PPS nal unit\n", 2095 channel->mcu_channel_id, len); 2096 } 2097 2098 if (msg->slice_type != AL_ENC_SLICE_TYPE_I && !msg->is_idr) { 2099 dst_buf->vb2_buf.planes[0].data_offset = free; 2100 free = 0; 2101 } else { 2102 if (channel->codec == V4L2_PIX_FMT_H264) 2103 len = nal_h264_write_filler(&dev->plat_dev->dev, curr, free); 2104 else 2105 len = nal_hevc_write_filler(&dev->plat_dev->dev, curr, free); 2106 if (len < 0) { 2107 v4l2_err(&dev->v4l2_dev, 2108 "failed to write %zd filler data\n", free); 2109 goto err; 2110 } 2111 curr += len; 2112 free -= len; 2113 v4l2_dbg(2, debug, &dev->v4l2_dev, 2114 "channel %d: wrote %zd bytes filler nal unit\n", 2115 channel->mcu_channel_id, len); 2116 } 2117 2118 if (free != 0) { 2119 v4l2_err(&dev->v4l2_dev, 2120 "non-VCL NAL units do not fill space until VCL NAL unit: %zd bytes left\n", 2121 free); 2122 goto err; 2123 } 2124 2125 state = VB2_BUF_STATE_DONE; 2126 2127 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, false); 2128 if (msg->is_idr) 2129 dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME; 2130 else 2131 dst_buf->flags |= V4L2_BUF_FLAG_PFRAME; 2132 2133 v4l2_dbg(1, debug, &dev->v4l2_dev, 2134 "channel %d: encoded frame #%03d (%s%s, QP %d, %d bytes)\n", 2135 channel->mcu_channel_id, 2136 dst_buf->sequence, 2137 msg->is_idr ? "IDR, " : "", 2138 msg->slice_type == AL_ENC_SLICE_TYPE_I ? "I slice" : 2139 msg->slice_type == AL_ENC_SLICE_TYPE_P ? "P slice" : "unknown", 2140 msg->qp, partition->size); 2141 2142 err: 2143 if (src_buf) 2144 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); 2145 2146 if (dst_buf) 2147 v4l2_m2m_buf_done(dst_buf, state); 2148 } 2149 2150 static int allegro_handle_init(struct allegro_dev *dev, 2151 struct mcu_msg_init_response *msg) 2152 { 2153 complete(&dev->init_complete); 2154 2155 return 0; 2156 } 2157 2158 static int 2159 allegro_handle_create_channel(struct allegro_dev *dev, 2160 struct mcu_msg_create_channel_response *msg) 2161 { 2162 struct allegro_channel *channel; 2163 int err = 0; 2164 struct create_channel_param param; 2165 2166 channel = allegro_find_channel_by_user_id(dev, msg->user_id); 2167 if (IS_ERR(channel)) { 2168 v4l2_warn(&dev->v4l2_dev, 2169 "received %s for unknown user %d\n", 2170 msg_type_name(msg->header.type), 2171 msg->user_id); 2172 return -EINVAL; 2173 } 2174 2175 if (msg->error_code) { 2176 v4l2_err(&dev->v4l2_dev, 2177 "user %d: mcu failed to create channel: %s (%x)\n", 2178 channel->user_id, 2179 allegro_err_to_string(msg->error_code), 2180 msg->error_code); 2181 err = -EIO; 2182 goto out; 2183 } 2184 2185 channel->mcu_channel_id = msg->channel_id; 2186 v4l2_dbg(1, debug, &dev->v4l2_dev, 2187 "user %d: channel has channel id %d\n", 2188 channel->user_id, channel->mcu_channel_id); 2189 2190 err = allegro_decode_config_blob(¶m, msg, channel->config_blob.vaddr); 2191 allegro_free_buffer(channel->dev, &channel->config_blob); 2192 if (err) 2193 goto out; 2194 2195 channel->num_ref_idx_l0 = param.num_ref_idx_l0; 2196 channel->num_ref_idx_l1 = param.num_ref_idx_l1; 2197 2198 v4l2_dbg(1, debug, &dev->v4l2_dev, 2199 "channel %d: intermediate buffers: %d x %d bytes\n", 2200 channel->mcu_channel_id, 2201 msg->int_buffers_count, msg->int_buffers_size); 2202 err = allocate_intermediate_buffers(channel, msg->int_buffers_count, 2203 msg->int_buffers_size); 2204 if (err) { 2205 v4l2_err(&dev->v4l2_dev, 2206 "channel %d: failed to allocate intermediate buffers\n", 2207 channel->mcu_channel_id); 2208 goto out; 2209 } 2210 err = allegro_mcu_push_buffer_intermediate(channel); 2211 if (err) 2212 goto out; 2213 2214 v4l2_dbg(1, debug, &dev->v4l2_dev, 2215 "channel %d: reference buffers: %d x %d bytes\n", 2216 channel->mcu_channel_id, 2217 msg->rec_buffers_count, msg->rec_buffers_size); 2218 err = allocate_reference_buffers(channel, msg->rec_buffers_count, 2219 msg->rec_buffers_size); 2220 if (err) { 2221 v4l2_err(&dev->v4l2_dev, 2222 "channel %d: failed to allocate reference buffers\n", 2223 channel->mcu_channel_id); 2224 goto out; 2225 } 2226 err = allegro_mcu_push_buffer_reference(channel); 2227 if (err) 2228 goto out; 2229 2230 out: 2231 channel->error = err; 2232 complete(&channel->completion); 2233 2234 /* Handled successfully, error is passed via channel->error */ 2235 return 0; 2236 } 2237 2238 static int 2239 allegro_handle_destroy_channel(struct allegro_dev *dev, 2240 struct mcu_msg_destroy_channel_response *msg) 2241 { 2242 struct allegro_channel *channel; 2243 2244 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id); 2245 if (IS_ERR(channel)) { 2246 v4l2_err(&dev->v4l2_dev, 2247 "received %s for unknown channel %d\n", 2248 msg_type_name(msg->header.type), 2249 msg->channel_id); 2250 return -EINVAL; 2251 } 2252 2253 v4l2_dbg(2, debug, &dev->v4l2_dev, 2254 "user %d: vcu destroyed channel %d\n", 2255 channel->user_id, channel->mcu_channel_id); 2256 complete(&channel->completion); 2257 2258 return 0; 2259 } 2260 2261 static int 2262 allegro_handle_encode_frame(struct allegro_dev *dev, 2263 struct mcu_msg_encode_frame_response *msg) 2264 { 2265 struct allegro_channel *channel; 2266 2267 channel = allegro_find_channel_by_channel_id(dev, msg->channel_id); 2268 if (IS_ERR(channel)) { 2269 v4l2_err(&dev->v4l2_dev, 2270 "received %s for unknown channel %d\n", 2271 msg_type_name(msg->header.type), 2272 msg->channel_id); 2273 return -EINVAL; 2274 } 2275 2276 allegro_channel_finish_frame(channel, msg); 2277 2278 return 0; 2279 } 2280 2281 static void allegro_handle_message(struct allegro_dev *dev, 2282 union mcu_msg_response *msg) 2283 { 2284 switch (msg->header.type) { 2285 case MCU_MSG_TYPE_INIT: 2286 allegro_handle_init(dev, &msg->init); 2287 break; 2288 case MCU_MSG_TYPE_CREATE_CHANNEL: 2289 allegro_handle_create_channel(dev, &msg->create_channel); 2290 break; 2291 case MCU_MSG_TYPE_DESTROY_CHANNEL: 2292 allegro_handle_destroy_channel(dev, &msg->destroy_channel); 2293 break; 2294 case MCU_MSG_TYPE_ENCODE_FRAME: 2295 allegro_handle_encode_frame(dev, &msg->encode_frame); 2296 break; 2297 default: 2298 v4l2_warn(&dev->v4l2_dev, 2299 "%s: unknown message %s\n", 2300 __func__, msg_type_name(msg->header.type)); 2301 break; 2302 } 2303 } 2304 2305 static irqreturn_t allegro_hardirq(int irq, void *data) 2306 { 2307 struct allegro_dev *dev = data; 2308 unsigned int status; 2309 2310 regmap_read(dev->regmap, AL5_ITC_CPU_IRQ_STA, &status); 2311 if (!(status & AL5_ITC_CPU_IRQ_STA_TRIGGERED)) 2312 return IRQ_NONE; 2313 2314 regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_CLR, status); 2315 2316 return IRQ_WAKE_THREAD; 2317 } 2318 2319 static irqreturn_t allegro_irq_thread(int irq, void *data) 2320 { 2321 struct allegro_dev *dev = data; 2322 2323 /* 2324 * The firmware is initialized after the mailbox is setup. We further 2325 * check the AL5_ITC_CPU_IRQ_STA register, if the firmware actually 2326 * triggered the interrupt. Although this should not happen, make sure 2327 * that we ignore interrupts, if the mailbox is not initialized. 2328 */ 2329 if (!dev->mbox_status) 2330 return IRQ_NONE; 2331 2332 allegro_mbox_notify(dev->mbox_status); 2333 2334 return IRQ_HANDLED; 2335 } 2336 2337 static void allegro_copy_firmware(struct allegro_dev *dev, 2338 const u8 * const buf, size_t size) 2339 { 2340 int err = 0; 2341 2342 v4l2_dbg(1, debug, &dev->v4l2_dev, 2343 "copy mcu firmware (%zu B) to SRAM\n", size); 2344 err = regmap_bulk_write(dev->sram, 0x0, buf, size / 4); 2345 if (err) 2346 v4l2_err(&dev->v4l2_dev, 2347 "failed to copy firmware: %d\n", err); 2348 } 2349 2350 static void allegro_copy_fw_codec(struct allegro_dev *dev, 2351 const u8 * const buf, size_t size) 2352 { 2353 int err; 2354 dma_addr_t icache_offset, dcache_offset; 2355 2356 /* 2357 * The downstream allocates 600 KB for the codec firmware to have some 2358 * extra space for "possible extensions." My tests were fine with 2359 * allocating just enough memory for the actual firmware, but I am not 2360 * sure that the firmware really does not use the remaining space. 2361 */ 2362 err = allegro_alloc_buffer(dev, &dev->firmware, size); 2363 if (err) { 2364 v4l2_err(&dev->v4l2_dev, 2365 "failed to allocate %zu bytes for firmware\n", size); 2366 return; 2367 } 2368 2369 v4l2_dbg(1, debug, &dev->v4l2_dev, 2370 "copy codec firmware (%zd B) to phys %pad\n", 2371 size, &dev->firmware.paddr); 2372 memcpy(dev->firmware.vaddr, buf, size); 2373 2374 regmap_write(dev->regmap, AXI_ADDR_OFFSET_IP, 2375 upper_32_bits(dev->firmware.paddr)); 2376 2377 icache_offset = dev->firmware.paddr - MCU_CACHE_OFFSET; 2378 v4l2_dbg(2, debug, &dev->v4l2_dev, 2379 "icache_offset: msb = 0x%x, lsb = 0x%x\n", 2380 upper_32_bits(icache_offset), lower_32_bits(icache_offset)); 2381 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_MSB, 2382 upper_32_bits(icache_offset)); 2383 regmap_write(dev->regmap, AL5_ICACHE_ADDR_OFFSET_LSB, 2384 lower_32_bits(icache_offset)); 2385 2386 dcache_offset = 2387 (dev->firmware.paddr & 0xffffffff00000000ULL) - MCU_CACHE_OFFSET; 2388 v4l2_dbg(2, debug, &dev->v4l2_dev, 2389 "dcache_offset: msb = 0x%x, lsb = 0x%x\n", 2390 upper_32_bits(dcache_offset), lower_32_bits(dcache_offset)); 2391 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_MSB, 2392 upper_32_bits(dcache_offset)); 2393 regmap_write(dev->regmap, AL5_DCACHE_ADDR_OFFSET_LSB, 2394 lower_32_bits(dcache_offset)); 2395 } 2396 2397 static void allegro_free_fw_codec(struct allegro_dev *dev) 2398 { 2399 allegro_free_buffer(dev, &dev->firmware); 2400 } 2401 2402 /* 2403 * Control functions for the MCU 2404 */ 2405 2406 static int allegro_mcu_enable_interrupts(struct allegro_dev *dev) 2407 { 2408 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, BIT(0)); 2409 } 2410 2411 static int allegro_mcu_disable_interrupts(struct allegro_dev *dev) 2412 { 2413 return regmap_write(dev->regmap, AL5_ITC_CPU_IRQ_MSK, 0); 2414 } 2415 2416 static int allegro_mcu_wait_for_sleep(struct allegro_dev *dev) 2417 { 2418 unsigned long timeout; 2419 unsigned int status; 2420 2421 timeout = jiffies + msecs_to_jiffies(100); 2422 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 && 2423 status != AL5_MCU_STA_SLEEP) { 2424 if (time_after(jiffies, timeout)) 2425 return -ETIMEDOUT; 2426 cpu_relax(); 2427 } 2428 2429 return 0; 2430 } 2431 2432 static int allegro_mcu_start(struct allegro_dev *dev) 2433 { 2434 unsigned long timeout; 2435 unsigned int status; 2436 int err; 2437 2438 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, BIT(0)); 2439 if (err) 2440 return err; 2441 2442 timeout = jiffies + msecs_to_jiffies(100); 2443 while (regmap_read(dev->regmap, AL5_MCU_STA, &status) == 0 && 2444 status == AL5_MCU_STA_SLEEP) { 2445 if (time_after(jiffies, timeout)) 2446 return -ETIMEDOUT; 2447 cpu_relax(); 2448 } 2449 2450 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0); 2451 if (err) 2452 return err; 2453 2454 return 0; 2455 } 2456 2457 static int allegro_mcu_reset(struct allegro_dev *dev) 2458 { 2459 int err; 2460 2461 /* 2462 * Ensure that the AL5_MCU_WAKEUP bit is set to 0 otherwise the mcu 2463 * does not go to sleep after the reset. 2464 */ 2465 err = regmap_write(dev->regmap, AL5_MCU_WAKEUP, 0); 2466 if (err) 2467 return err; 2468 2469 err = regmap_write(dev->regmap, 2470 AL5_MCU_RESET_MODE, AL5_MCU_RESET_MODE_SLEEP); 2471 if (err < 0) 2472 return err; 2473 2474 err = regmap_write(dev->regmap, AL5_MCU_RESET, AL5_MCU_RESET_SOFT); 2475 if (err < 0) 2476 return err; 2477 2478 return allegro_mcu_wait_for_sleep(dev); 2479 } 2480 2481 static void allegro_mcu_interrupt(struct allegro_dev *dev) 2482 { 2483 regmap_write(dev->regmap, AL5_MCU_INTERRUPT, BIT(0)); 2484 } 2485 2486 static void allegro_destroy_channel(struct allegro_channel *channel) 2487 { 2488 struct allegro_dev *dev = channel->dev; 2489 unsigned long time_left; 2490 2491 if (channel_exists(channel)) { 2492 reinit_completion(&channel->completion); 2493 allegro_mcu_send_destroy_channel(dev, channel); 2494 time_left = wait_for_completion_timeout(&channel->completion, 2495 msecs_to_jiffies(5000)); 2496 if (time_left == 0) 2497 v4l2_warn(&dev->v4l2_dev, 2498 "channel %d: timeout while destroying\n", 2499 channel->mcu_channel_id); 2500 2501 channel->mcu_channel_id = -1; 2502 } 2503 2504 destroy_intermediate_buffers(channel); 2505 destroy_reference_buffers(channel); 2506 2507 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, false); 2508 v4l2_ctrl_grab(channel->mpeg_video_h264_level, false); 2509 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, false); 2510 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, false); 2511 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, false); 2512 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, false); 2513 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, false); 2514 2515 v4l2_ctrl_grab(channel->mpeg_video_hevc_profile, false); 2516 v4l2_ctrl_grab(channel->mpeg_video_hevc_level, false); 2517 v4l2_ctrl_grab(channel->mpeg_video_hevc_tier, false); 2518 v4l2_ctrl_grab(channel->mpeg_video_hevc_i_frame_qp, false); 2519 v4l2_ctrl_grab(channel->mpeg_video_hevc_max_qp, false); 2520 v4l2_ctrl_grab(channel->mpeg_video_hevc_min_qp, false); 2521 v4l2_ctrl_grab(channel->mpeg_video_hevc_p_frame_qp, false); 2522 v4l2_ctrl_grab(channel->mpeg_video_hevc_b_frame_qp, false); 2523 2524 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, false); 2525 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, false); 2526 v4l2_ctrl_grab(channel->mpeg_video_bitrate, false); 2527 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, false); 2528 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, false); 2529 v4l2_ctrl_grab(channel->mpeg_video_gop_size, false); 2530 2531 v4l2_ctrl_grab(channel->encoder_buffer, false); 2532 2533 if (channel->user_id != -1) { 2534 clear_bit(channel->user_id, &dev->channel_user_ids); 2535 channel->user_id = -1; 2536 } 2537 } 2538 2539 /* 2540 * Create the MCU channel 2541 * 2542 * After the channel has been created, the picture size, format, colorspace 2543 * and framerate are fixed. Also the codec, profile, bitrate, etc. cannot be 2544 * changed anymore. 2545 * 2546 * The channel can be created only once. The MCU will accept source buffers 2547 * and stream buffers only after a channel has been created. 2548 */ 2549 static int allegro_create_channel(struct allegro_channel *channel) 2550 { 2551 struct allegro_dev *dev = channel->dev; 2552 unsigned long time_left; 2553 2554 if (channel_exists(channel)) { 2555 v4l2_warn(&dev->v4l2_dev, 2556 "channel already exists\n"); 2557 return 0; 2558 } 2559 2560 channel->user_id = allegro_next_user_id(dev); 2561 if (channel->user_id < 0) { 2562 v4l2_err(&dev->v4l2_dev, 2563 "no free channels available\n"); 2564 return -EBUSY; 2565 } 2566 set_bit(channel->user_id, &dev->channel_user_ids); 2567 2568 v4l2_dbg(1, debug, &dev->v4l2_dev, 2569 "user %d: creating channel (%4.4s, %dx%d@%d)\n", 2570 channel->user_id, 2571 (char *)&channel->codec, channel->width, channel->height, 2572 DIV_ROUND_UP(channel->framerate.numerator, 2573 channel->framerate.denominator)); 2574 2575 v4l2_ctrl_grab(channel->mpeg_video_h264_profile, true); 2576 v4l2_ctrl_grab(channel->mpeg_video_h264_level, true); 2577 v4l2_ctrl_grab(channel->mpeg_video_h264_i_frame_qp, true); 2578 v4l2_ctrl_grab(channel->mpeg_video_h264_max_qp, true); 2579 v4l2_ctrl_grab(channel->mpeg_video_h264_min_qp, true); 2580 v4l2_ctrl_grab(channel->mpeg_video_h264_p_frame_qp, true); 2581 v4l2_ctrl_grab(channel->mpeg_video_h264_b_frame_qp, true); 2582 2583 v4l2_ctrl_grab(channel->mpeg_video_hevc_profile, true); 2584 v4l2_ctrl_grab(channel->mpeg_video_hevc_level, true); 2585 v4l2_ctrl_grab(channel->mpeg_video_hevc_tier, true); 2586 v4l2_ctrl_grab(channel->mpeg_video_hevc_i_frame_qp, true); 2587 v4l2_ctrl_grab(channel->mpeg_video_hevc_max_qp, true); 2588 v4l2_ctrl_grab(channel->mpeg_video_hevc_min_qp, true); 2589 v4l2_ctrl_grab(channel->mpeg_video_hevc_p_frame_qp, true); 2590 v4l2_ctrl_grab(channel->mpeg_video_hevc_b_frame_qp, true); 2591 2592 v4l2_ctrl_grab(channel->mpeg_video_frame_rc_enable, true); 2593 v4l2_ctrl_grab(channel->mpeg_video_bitrate_mode, true); 2594 v4l2_ctrl_grab(channel->mpeg_video_bitrate, true); 2595 v4l2_ctrl_grab(channel->mpeg_video_bitrate_peak, true); 2596 v4l2_ctrl_grab(channel->mpeg_video_cpb_size, true); 2597 v4l2_ctrl_grab(channel->mpeg_video_gop_size, true); 2598 2599 v4l2_ctrl_grab(channel->encoder_buffer, true); 2600 2601 reinit_completion(&channel->completion); 2602 allegro_mcu_send_create_channel(dev, channel); 2603 time_left = wait_for_completion_timeout(&channel->completion, 2604 msecs_to_jiffies(5000)); 2605 if (time_left == 0) 2606 channel->error = -ETIMEDOUT; 2607 if (channel->error) 2608 goto err; 2609 2610 v4l2_dbg(1, debug, &dev->v4l2_dev, 2611 "channel %d: accepting buffers\n", 2612 channel->mcu_channel_id); 2613 2614 return 0; 2615 2616 err: 2617 allegro_destroy_channel(channel); 2618 2619 return channel->error; 2620 } 2621 2622 /** 2623 * allegro_channel_adjust() - Adjust channel parameters to current format 2624 * @channel: the channel to adjust 2625 * 2626 * Various parameters of a channel and their limits depend on the currently 2627 * set format. Adjust the parameters after a format change in one go. 2628 */ 2629 static void allegro_channel_adjust(struct allegro_channel *channel) 2630 { 2631 struct allegro_dev *dev = channel->dev; 2632 u32 codec = channel->codec; 2633 struct v4l2_ctrl *ctrl; 2634 s64 min; 2635 s64 max; 2636 2637 channel->sizeimage_encoded = 2638 estimate_stream_size(channel->width, channel->height); 2639 2640 if (codec == V4L2_PIX_FMT_H264) { 2641 ctrl = channel->mpeg_video_h264_level; 2642 min = select_minimum_h264_level(channel->width, channel->height); 2643 } else { 2644 ctrl = channel->mpeg_video_hevc_level; 2645 min = select_minimum_hevc_level(channel->width, channel->height); 2646 } 2647 if (ctrl->minimum > min) 2648 v4l2_dbg(1, debug, &dev->v4l2_dev, 2649 "%s.minimum: %lld -> %lld\n", 2650 v4l2_ctrl_get_name(ctrl->id), ctrl->minimum, min); 2651 v4l2_ctrl_lock(ctrl); 2652 __v4l2_ctrl_modify_range(ctrl, min, ctrl->maximum, 2653 ctrl->step, ctrl->default_value); 2654 v4l2_ctrl_unlock(ctrl); 2655 2656 ctrl = channel->mpeg_video_bitrate; 2657 if (codec == V4L2_PIX_FMT_H264) 2658 max = h264_maximum_bitrate(v4l2_ctrl_g_ctrl(channel->mpeg_video_h264_level)); 2659 else 2660 max = hevc_maximum_bitrate(v4l2_ctrl_g_ctrl(channel->mpeg_video_hevc_level)); 2661 if (ctrl->maximum < max) 2662 v4l2_dbg(1, debug, &dev->v4l2_dev, 2663 "%s: maximum: %lld -> %lld\n", 2664 v4l2_ctrl_get_name(ctrl->id), ctrl->maximum, max); 2665 v4l2_ctrl_lock(ctrl); 2666 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, 2667 ctrl->step, ctrl->default_value); 2668 v4l2_ctrl_unlock(ctrl); 2669 2670 ctrl = channel->mpeg_video_bitrate_peak; 2671 v4l2_ctrl_lock(ctrl); 2672 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, 2673 ctrl->step, ctrl->default_value); 2674 v4l2_ctrl_unlock(ctrl); 2675 2676 v4l2_ctrl_activate(channel->mpeg_video_h264_profile, 2677 codec == V4L2_PIX_FMT_H264); 2678 v4l2_ctrl_activate(channel->mpeg_video_h264_level, 2679 codec == V4L2_PIX_FMT_H264); 2680 v4l2_ctrl_activate(channel->mpeg_video_h264_i_frame_qp, 2681 codec == V4L2_PIX_FMT_H264); 2682 v4l2_ctrl_activate(channel->mpeg_video_h264_max_qp, 2683 codec == V4L2_PIX_FMT_H264); 2684 v4l2_ctrl_activate(channel->mpeg_video_h264_min_qp, 2685 codec == V4L2_PIX_FMT_H264); 2686 v4l2_ctrl_activate(channel->mpeg_video_h264_p_frame_qp, 2687 codec == V4L2_PIX_FMT_H264); 2688 v4l2_ctrl_activate(channel->mpeg_video_h264_b_frame_qp, 2689 codec == V4L2_PIX_FMT_H264); 2690 2691 v4l2_ctrl_activate(channel->mpeg_video_hevc_profile, 2692 codec == V4L2_PIX_FMT_HEVC); 2693 v4l2_ctrl_activate(channel->mpeg_video_hevc_level, 2694 codec == V4L2_PIX_FMT_HEVC); 2695 v4l2_ctrl_activate(channel->mpeg_video_hevc_tier, 2696 codec == V4L2_PIX_FMT_HEVC); 2697 v4l2_ctrl_activate(channel->mpeg_video_hevc_i_frame_qp, 2698 codec == V4L2_PIX_FMT_HEVC); 2699 v4l2_ctrl_activate(channel->mpeg_video_hevc_max_qp, 2700 codec == V4L2_PIX_FMT_HEVC); 2701 v4l2_ctrl_activate(channel->mpeg_video_hevc_min_qp, 2702 codec == V4L2_PIX_FMT_HEVC); 2703 v4l2_ctrl_activate(channel->mpeg_video_hevc_p_frame_qp, 2704 codec == V4L2_PIX_FMT_HEVC); 2705 v4l2_ctrl_activate(channel->mpeg_video_hevc_b_frame_qp, 2706 codec == V4L2_PIX_FMT_HEVC); 2707 2708 if (codec == V4L2_PIX_FMT_H264) 2709 channel->log2_max_frame_num = LOG2_MAX_FRAME_NUM; 2710 channel->temporal_mvp_enable = true; 2711 channel->dbf_ovr_en = (codec == V4L2_PIX_FMT_H264); 2712 channel->enable_deblocking_filter_override = (codec == V4L2_PIX_FMT_HEVC); 2713 channel->enable_reordering = (codec == V4L2_PIX_FMT_HEVC); 2714 channel->enable_loop_filter_across_tiles = true; 2715 channel->enable_loop_filter_across_slices = true; 2716 2717 if (codec == V4L2_PIX_FMT_H264) { 2718 channel->b_hrz_me_range = 8; 2719 channel->b_vrt_me_range = 8; 2720 channel->p_hrz_me_range = 16; 2721 channel->p_vrt_me_range = 16; 2722 channel->max_cu_size = ilog2(16); 2723 channel->min_cu_size = ilog2(8); 2724 channel->max_tu_size = ilog2(4); 2725 channel->min_tu_size = ilog2(4); 2726 } else { 2727 channel->b_hrz_me_range = 16; 2728 channel->b_vrt_me_range = 16; 2729 channel->p_hrz_me_range = 32; 2730 channel->p_vrt_me_range = 32; 2731 channel->max_cu_size = ilog2(32); 2732 channel->min_cu_size = ilog2(8); 2733 channel->max_tu_size = ilog2(32); 2734 channel->min_tu_size = ilog2(4); 2735 } 2736 channel->max_transfo_depth_intra = 1; 2737 channel->max_transfo_depth_inter = 1; 2738 } 2739 2740 static void allegro_set_default_params(struct allegro_channel *channel) 2741 { 2742 channel->width = ALLEGRO_WIDTH_DEFAULT; 2743 channel->height = ALLEGRO_HEIGHT_DEFAULT; 2744 channel->stride = round_up(channel->width, 32); 2745 channel->framerate = ALLEGRO_FRAMERATE_DEFAULT; 2746 2747 channel->colorspace = V4L2_COLORSPACE_REC709; 2748 channel->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 2749 channel->quantization = V4L2_QUANTIZATION_DEFAULT; 2750 channel->xfer_func = V4L2_XFER_FUNC_DEFAULT; 2751 2752 channel->pixelformat = V4L2_PIX_FMT_NV12; 2753 channel->sizeimage_raw = channel->stride * channel->height * 3 / 2; 2754 2755 channel->codec = V4L2_PIX_FMT_H264; 2756 } 2757 2758 static int allegro_queue_setup(struct vb2_queue *vq, 2759 unsigned int *nbuffers, unsigned int *nplanes, 2760 unsigned int sizes[], 2761 struct device *alloc_devs[]) 2762 { 2763 struct allegro_channel *channel = vb2_get_drv_priv(vq); 2764 struct allegro_dev *dev = channel->dev; 2765 2766 v4l2_dbg(2, debug, &dev->v4l2_dev, 2767 "%s: queue setup[%s]: nplanes = %d\n", 2768 V4L2_TYPE_IS_OUTPUT(vq->type) ? "output" : "capture", 2769 *nplanes == 0 ? "REQBUFS" : "CREATE_BUFS", *nplanes); 2770 2771 if (*nplanes != 0) { 2772 if (V4L2_TYPE_IS_OUTPUT(vq->type)) { 2773 if (sizes[0] < channel->sizeimage_raw) 2774 return -EINVAL; 2775 } else { 2776 if (sizes[0] < channel->sizeimage_encoded) 2777 return -EINVAL; 2778 } 2779 } else { 2780 *nplanes = 1; 2781 if (V4L2_TYPE_IS_OUTPUT(vq->type)) 2782 sizes[0] = channel->sizeimage_raw; 2783 else 2784 sizes[0] = channel->sizeimage_encoded; 2785 } 2786 2787 return 0; 2788 } 2789 2790 static int allegro_buf_prepare(struct vb2_buffer *vb) 2791 { 2792 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 2793 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue); 2794 struct allegro_dev *dev = channel->dev; 2795 2796 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { 2797 if (vbuf->field == V4L2_FIELD_ANY) 2798 vbuf->field = V4L2_FIELD_NONE; 2799 if (vbuf->field != V4L2_FIELD_NONE) { 2800 v4l2_err(&dev->v4l2_dev, 2801 "channel %d: unsupported field\n", 2802 channel->mcu_channel_id); 2803 return -EINVAL; 2804 } 2805 } 2806 2807 return 0; 2808 } 2809 2810 static void allegro_buf_queue(struct vb2_buffer *vb) 2811 { 2812 struct allegro_channel *channel = vb2_get_drv_priv(vb->vb2_queue); 2813 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 2814 struct vb2_queue *q = vb->vb2_queue; 2815 2816 if (V4L2_TYPE_IS_CAPTURE(q->type) && 2817 vb2_is_streaming(q) && 2818 v4l2_m2m_dst_buf_is_last(channel->fh.m2m_ctx)) { 2819 unsigned int i; 2820 2821 for (i = 0; i < vb->num_planes; i++) 2822 vb2_set_plane_payload(vb, i, 0); 2823 2824 vbuf->field = V4L2_FIELD_NONE; 2825 vbuf->sequence = channel->csequence++; 2826 2827 v4l2_m2m_last_buffer_done(channel->fh.m2m_ctx, vbuf); 2828 allegro_channel_eos_event(channel); 2829 return; 2830 } 2831 2832 v4l2_m2m_buf_queue(channel->fh.m2m_ctx, vbuf); 2833 } 2834 2835 static int allegro_start_streaming(struct vb2_queue *q, unsigned int count) 2836 { 2837 struct allegro_channel *channel = vb2_get_drv_priv(q); 2838 struct allegro_dev *dev = channel->dev; 2839 2840 v4l2_dbg(2, debug, &dev->v4l2_dev, 2841 "%s: start streaming\n", 2842 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture"); 2843 2844 v4l2_m2m_update_start_streaming_state(channel->fh.m2m_ctx, q); 2845 2846 if (V4L2_TYPE_IS_OUTPUT(q->type)) 2847 channel->osequence = 0; 2848 else 2849 channel->csequence = 0; 2850 2851 return 0; 2852 } 2853 2854 static void allegro_stop_streaming(struct vb2_queue *q) 2855 { 2856 struct allegro_channel *channel = vb2_get_drv_priv(q); 2857 struct allegro_dev *dev = channel->dev; 2858 struct vb2_v4l2_buffer *buffer; 2859 struct allegro_m2m_buffer *shadow, *tmp; 2860 2861 v4l2_dbg(2, debug, &dev->v4l2_dev, 2862 "%s: stop streaming\n", 2863 V4L2_TYPE_IS_OUTPUT(q->type) ? "output" : "capture"); 2864 2865 if (V4L2_TYPE_IS_OUTPUT(q->type)) { 2866 mutex_lock(&channel->shadow_list_lock); 2867 list_for_each_entry_safe(shadow, tmp, 2868 &channel->source_shadow_list, head) { 2869 list_del(&shadow->head); 2870 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR); 2871 } 2872 mutex_unlock(&channel->shadow_list_lock); 2873 2874 while ((buffer = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx))) 2875 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR); 2876 } else { 2877 mutex_lock(&channel->shadow_list_lock); 2878 list_for_each_entry_safe(shadow, tmp, 2879 &channel->stream_shadow_list, head) { 2880 list_del(&shadow->head); 2881 v4l2_m2m_buf_done(&shadow->buf.vb, VB2_BUF_STATE_ERROR); 2882 } 2883 mutex_unlock(&channel->shadow_list_lock); 2884 2885 allegro_destroy_channel(channel); 2886 while ((buffer = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx))) 2887 v4l2_m2m_buf_done(buffer, VB2_BUF_STATE_ERROR); 2888 } 2889 2890 v4l2_m2m_update_stop_streaming_state(channel->fh.m2m_ctx, q); 2891 2892 if (V4L2_TYPE_IS_OUTPUT(q->type) && 2893 v4l2_m2m_has_stopped(channel->fh.m2m_ctx)) 2894 allegro_channel_eos_event(channel); 2895 } 2896 2897 static const struct vb2_ops allegro_queue_ops = { 2898 .queue_setup = allegro_queue_setup, 2899 .buf_prepare = allegro_buf_prepare, 2900 .buf_queue = allegro_buf_queue, 2901 .start_streaming = allegro_start_streaming, 2902 .stop_streaming = allegro_stop_streaming, 2903 }; 2904 2905 static int allegro_queue_init(void *priv, 2906 struct vb2_queue *src_vq, 2907 struct vb2_queue *dst_vq) 2908 { 2909 int err; 2910 struct allegro_channel *channel = priv; 2911 2912 src_vq->dev = &channel->dev->plat_dev->dev; 2913 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 2914 src_vq->io_modes = VB2_DMABUF | VB2_MMAP; 2915 src_vq->mem_ops = &vb2_dma_contig_memops; 2916 src_vq->drv_priv = channel; 2917 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 2918 src_vq->ops = &allegro_queue_ops; 2919 src_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer); 2920 src_vq->lock = &channel->dev->lock; 2921 err = vb2_queue_init(src_vq); 2922 if (err) 2923 return err; 2924 2925 dst_vq->dev = &channel->dev->plat_dev->dev; 2926 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2927 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; 2928 dst_vq->mem_ops = &vb2_dma_contig_memops; 2929 dst_vq->drv_priv = channel; 2930 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 2931 dst_vq->ops = &allegro_queue_ops; 2932 dst_vq->buf_struct_size = sizeof(struct allegro_m2m_buffer); 2933 dst_vq->lock = &channel->dev->lock; 2934 err = vb2_queue_init(dst_vq); 2935 if (err) 2936 return err; 2937 2938 return 0; 2939 } 2940 2941 static int allegro_clamp_qp(struct allegro_channel *channel, 2942 struct v4l2_ctrl *ctrl) 2943 { 2944 struct v4l2_ctrl *next_ctrl; 2945 2946 if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP) 2947 next_ctrl = channel->mpeg_video_h264_p_frame_qp; 2948 else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP) 2949 next_ctrl = channel->mpeg_video_h264_b_frame_qp; 2950 else 2951 return 0; 2952 2953 /* Modify range automatically updates the value */ 2954 __v4l2_ctrl_modify_range(next_ctrl, ctrl->val, 51, 1, ctrl->val); 2955 2956 return allegro_clamp_qp(channel, next_ctrl); 2957 } 2958 2959 static int allegro_clamp_bitrate(struct allegro_channel *channel, 2960 struct v4l2_ctrl *ctrl) 2961 { 2962 struct v4l2_ctrl *ctrl_bitrate = channel->mpeg_video_bitrate; 2963 struct v4l2_ctrl *ctrl_bitrate_peak = channel->mpeg_video_bitrate_peak; 2964 2965 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR && 2966 ctrl_bitrate_peak->val < ctrl_bitrate->val) 2967 ctrl_bitrate_peak->val = ctrl_bitrate->val; 2968 2969 return 0; 2970 } 2971 2972 static int allegro_try_ctrl(struct v4l2_ctrl *ctrl) 2973 { 2974 struct allegro_channel *channel = container_of(ctrl->handler, 2975 struct allegro_channel, 2976 ctrl_handler); 2977 2978 switch (ctrl->id) { 2979 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 2980 allegro_clamp_bitrate(channel, ctrl); 2981 break; 2982 case V4L2_CID_USER_ALLEGRO_ENCODER_BUFFER: 2983 if (!channel->dev->has_encoder_buffer) 2984 ctrl->val = 0; 2985 break; 2986 } 2987 2988 return 0; 2989 } 2990 2991 static int allegro_s_ctrl(struct v4l2_ctrl *ctrl) 2992 { 2993 struct allegro_channel *channel = container_of(ctrl->handler, 2994 struct allegro_channel, 2995 ctrl_handler); 2996 struct allegro_dev *dev = channel->dev; 2997 2998 v4l2_dbg(1, debug, &dev->v4l2_dev, 2999 "s_ctrl: %s = %d\n", v4l2_ctrl_get_name(ctrl->id), ctrl->val); 3000 3001 switch (ctrl->id) { 3002 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: 3003 channel->frame_rc_enable = ctrl->val; 3004 break; 3005 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 3006 channel->bitrate = channel->mpeg_video_bitrate->val; 3007 channel->bitrate_peak = channel->mpeg_video_bitrate_peak->val; 3008 v4l2_ctrl_activate(channel->mpeg_video_bitrate_peak, 3009 ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR); 3010 break; 3011 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: 3012 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: 3013 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: 3014 allegro_clamp_qp(channel, ctrl); 3015 break; 3016 } 3017 3018 return 0; 3019 } 3020 3021 static const struct v4l2_ctrl_ops allegro_ctrl_ops = { 3022 .try_ctrl = allegro_try_ctrl, 3023 .s_ctrl = allegro_s_ctrl, 3024 }; 3025 3026 static const struct v4l2_ctrl_config allegro_encoder_buffer_ctrl_config = { 3027 .id = V4L2_CID_USER_ALLEGRO_ENCODER_BUFFER, 3028 .name = "Encoder Buffer Enable", 3029 .type = V4L2_CTRL_TYPE_BOOLEAN, 3030 .min = 0, 3031 .max = 1, 3032 .step = 1, 3033 .def = 1, 3034 }; 3035 3036 static int allegro_open(struct file *file) 3037 { 3038 struct video_device *vdev = video_devdata(file); 3039 struct allegro_dev *dev = video_get_drvdata(vdev); 3040 struct allegro_channel *channel = NULL; 3041 struct v4l2_ctrl_handler *handler; 3042 u64 mask; 3043 int ret; 3044 unsigned int bitrate_max; 3045 unsigned int bitrate_def; 3046 unsigned int cpb_size_max; 3047 unsigned int cpb_size_def; 3048 3049 channel = kzalloc(sizeof(*channel), GFP_KERNEL); 3050 if (!channel) 3051 return -ENOMEM; 3052 3053 v4l2_fh_init(&channel->fh, vdev); 3054 3055 init_completion(&channel->completion); 3056 INIT_LIST_HEAD(&channel->source_shadow_list); 3057 INIT_LIST_HEAD(&channel->stream_shadow_list); 3058 mutex_init(&channel->shadow_list_lock); 3059 3060 channel->dev = dev; 3061 3062 allegro_set_default_params(channel); 3063 3064 handler = &channel->ctrl_handler; 3065 v4l2_ctrl_handler_init(handler, 0); 3066 channel->mpeg_video_h264_profile = v4l2_ctrl_new_std_menu(handler, 3067 &allegro_ctrl_ops, 3068 V4L2_CID_MPEG_VIDEO_H264_PROFILE, 3069 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0, 3070 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE); 3071 mask = 1 << V4L2_MPEG_VIDEO_H264_LEVEL_1B; 3072 channel->mpeg_video_h264_level = v4l2_ctrl_new_std_menu(handler, 3073 &allegro_ctrl_ops, 3074 V4L2_CID_MPEG_VIDEO_H264_LEVEL, 3075 V4L2_MPEG_VIDEO_H264_LEVEL_5_1, mask, 3076 V4L2_MPEG_VIDEO_H264_LEVEL_5_1); 3077 channel->mpeg_video_h264_i_frame_qp = 3078 v4l2_ctrl_new_std(handler, 3079 &allegro_ctrl_ops, 3080 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 3081 0, 51, 1, 30); 3082 channel->mpeg_video_h264_max_qp = 3083 v4l2_ctrl_new_std(handler, 3084 &allegro_ctrl_ops, 3085 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 3086 0, 51, 1, 51); 3087 channel->mpeg_video_h264_min_qp = 3088 v4l2_ctrl_new_std(handler, 3089 &allegro_ctrl_ops, 3090 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 3091 0, 51, 1, 0); 3092 channel->mpeg_video_h264_p_frame_qp = 3093 v4l2_ctrl_new_std(handler, 3094 &allegro_ctrl_ops, 3095 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 3096 0, 51, 1, 30); 3097 channel->mpeg_video_h264_b_frame_qp = 3098 v4l2_ctrl_new_std(handler, 3099 &allegro_ctrl_ops, 3100 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 3101 0, 51, 1, 30); 3102 3103 channel->mpeg_video_hevc_profile = 3104 v4l2_ctrl_new_std_menu(handler, 3105 &allegro_ctrl_ops, 3106 V4L2_CID_MPEG_VIDEO_HEVC_PROFILE, 3107 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN, 0x0, 3108 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN); 3109 channel->mpeg_video_hevc_level = 3110 v4l2_ctrl_new_std_menu(handler, 3111 &allegro_ctrl_ops, 3112 V4L2_CID_MPEG_VIDEO_HEVC_LEVEL, 3113 V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1, 0x0, 3114 V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1); 3115 channel->mpeg_video_hevc_tier = 3116 v4l2_ctrl_new_std_menu(handler, 3117 &allegro_ctrl_ops, 3118 V4L2_CID_MPEG_VIDEO_HEVC_TIER, 3119 V4L2_MPEG_VIDEO_HEVC_TIER_HIGH, 0x0, 3120 V4L2_MPEG_VIDEO_HEVC_TIER_MAIN); 3121 channel->mpeg_video_hevc_i_frame_qp = 3122 v4l2_ctrl_new_std(handler, 3123 &allegro_ctrl_ops, 3124 V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP, 3125 0, 51, 1, 30); 3126 channel->mpeg_video_hevc_max_qp = 3127 v4l2_ctrl_new_std(handler, 3128 &allegro_ctrl_ops, 3129 V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP, 3130 0, 51, 1, 51); 3131 channel->mpeg_video_hevc_min_qp = 3132 v4l2_ctrl_new_std(handler, 3133 &allegro_ctrl_ops, 3134 V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, 3135 0, 51, 1, 0); 3136 channel->mpeg_video_hevc_p_frame_qp = 3137 v4l2_ctrl_new_std(handler, 3138 &allegro_ctrl_ops, 3139 V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP, 3140 0, 51, 1, 30); 3141 channel->mpeg_video_hevc_b_frame_qp = 3142 v4l2_ctrl_new_std(handler, 3143 &allegro_ctrl_ops, 3144 V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP, 3145 0, 51, 1, 30); 3146 3147 channel->mpeg_video_frame_rc_enable = 3148 v4l2_ctrl_new_std(handler, 3149 &allegro_ctrl_ops, 3150 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 3151 false, 0x1, 3152 true, false); 3153 channel->mpeg_video_bitrate_mode = v4l2_ctrl_new_std_menu(handler, 3154 &allegro_ctrl_ops, 3155 V4L2_CID_MPEG_VIDEO_BITRATE_MODE, 3156 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0, 3157 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); 3158 3159 if (channel->codec == V4L2_PIX_FMT_H264) { 3160 bitrate_max = h264_maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1); 3161 bitrate_def = h264_maximum_bitrate(V4L2_MPEG_VIDEO_H264_LEVEL_5_1); 3162 cpb_size_max = h264_maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1); 3163 cpb_size_def = h264_maximum_cpb_size(V4L2_MPEG_VIDEO_H264_LEVEL_5_1); 3164 } else { 3165 bitrate_max = hevc_maximum_bitrate(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1); 3166 bitrate_def = hevc_maximum_bitrate(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1); 3167 cpb_size_max = hevc_maximum_cpb_size(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1); 3168 cpb_size_def = hevc_maximum_cpb_size(V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1); 3169 } 3170 channel->mpeg_video_bitrate = v4l2_ctrl_new_std(handler, 3171 &allegro_ctrl_ops, 3172 V4L2_CID_MPEG_VIDEO_BITRATE, 3173 0, bitrate_max, 1, bitrate_def); 3174 channel->mpeg_video_bitrate_peak = v4l2_ctrl_new_std(handler, 3175 &allegro_ctrl_ops, 3176 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, 3177 0, bitrate_max, 1, bitrate_def); 3178 channel->mpeg_video_cpb_size = v4l2_ctrl_new_std(handler, 3179 &allegro_ctrl_ops, 3180 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 3181 0, cpb_size_max, 1, cpb_size_def); 3182 channel->mpeg_video_gop_size = v4l2_ctrl_new_std(handler, 3183 &allegro_ctrl_ops, 3184 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 3185 0, ALLEGRO_GOP_SIZE_MAX, 3186 1, ALLEGRO_GOP_SIZE_DEFAULT); 3187 channel->encoder_buffer = v4l2_ctrl_new_custom(handler, 3188 &allegro_encoder_buffer_ctrl_config, NULL); 3189 v4l2_ctrl_new_std(handler, 3190 &allegro_ctrl_ops, 3191 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 3192 1, 32, 3193 1, 1); 3194 if (handler->error != 0) { 3195 ret = handler->error; 3196 goto error; 3197 } 3198 3199 channel->fh.ctrl_handler = handler; 3200 3201 v4l2_ctrl_cluster(3, &channel->mpeg_video_bitrate_mode); 3202 3203 v4l2_ctrl_handler_setup(handler); 3204 3205 channel->mcu_channel_id = -1; 3206 channel->user_id = -1; 3207 3208 INIT_LIST_HEAD(&channel->buffers_reference); 3209 INIT_LIST_HEAD(&channel->buffers_intermediate); 3210 3211 channel->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, channel, 3212 allegro_queue_init); 3213 3214 if (IS_ERR(channel->fh.m2m_ctx)) { 3215 ret = PTR_ERR(channel->fh.m2m_ctx); 3216 goto error; 3217 } 3218 3219 list_add(&channel->list, &dev->channels); 3220 v4l2_fh_add(&channel->fh, file); 3221 3222 allegro_channel_adjust(channel); 3223 3224 return 0; 3225 3226 error: 3227 v4l2_ctrl_handler_free(handler); 3228 kfree(channel); 3229 return ret; 3230 } 3231 3232 static int allegro_release(struct file *file) 3233 { 3234 struct allegro_channel *channel = file_to_channel(file); 3235 3236 v4l2_m2m_ctx_release(channel->fh.m2m_ctx); 3237 3238 list_del(&channel->list); 3239 3240 v4l2_ctrl_handler_free(&channel->ctrl_handler); 3241 3242 v4l2_fh_del(&channel->fh, file); 3243 v4l2_fh_exit(&channel->fh); 3244 3245 kfree(channel); 3246 3247 return 0; 3248 } 3249 3250 static int allegro_querycap(struct file *file, void *fh, 3251 struct v4l2_capability *cap) 3252 { 3253 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 3254 strscpy(cap->card, "Allegro DVT Video Encoder", sizeof(cap->card)); 3255 3256 return 0; 3257 } 3258 3259 static int allegro_enum_fmt_vid(struct file *file, void *fh, 3260 struct v4l2_fmtdesc *f) 3261 { 3262 switch (f->type) { 3263 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 3264 if (f->index >= 1) 3265 return -EINVAL; 3266 f->pixelformat = V4L2_PIX_FMT_NV12; 3267 break; 3268 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 3269 if (f->index >= 2) 3270 return -EINVAL; 3271 if (f->index == 0) 3272 f->pixelformat = V4L2_PIX_FMT_H264; 3273 if (f->index == 1) 3274 f->pixelformat = V4L2_PIX_FMT_HEVC; 3275 break; 3276 default: 3277 return -EINVAL; 3278 } 3279 return 0; 3280 } 3281 3282 static int allegro_g_fmt_vid_cap(struct file *file, void *fh, 3283 struct v4l2_format *f) 3284 { 3285 struct allegro_channel *channel = file_to_channel(file); 3286 3287 f->fmt.pix.field = V4L2_FIELD_NONE; 3288 f->fmt.pix.width = channel->width; 3289 f->fmt.pix.height = channel->height; 3290 3291 f->fmt.pix.colorspace = channel->colorspace; 3292 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc; 3293 f->fmt.pix.quantization = channel->quantization; 3294 f->fmt.pix.xfer_func = channel->xfer_func; 3295 3296 f->fmt.pix.pixelformat = channel->codec; 3297 f->fmt.pix.bytesperline = 0; 3298 f->fmt.pix.sizeimage = channel->sizeimage_encoded; 3299 3300 return 0; 3301 } 3302 3303 static int allegro_try_fmt_vid_cap(struct file *file, void *fh, 3304 struct v4l2_format *f) 3305 { 3306 f->fmt.pix.field = V4L2_FIELD_NONE; 3307 3308 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width, 3309 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX); 3310 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height, 3311 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX); 3312 3313 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_HEVC && 3314 f->fmt.pix.pixelformat != V4L2_PIX_FMT_H264) 3315 f->fmt.pix.pixelformat = V4L2_PIX_FMT_H264; 3316 3317 f->fmt.pix.bytesperline = 0; 3318 f->fmt.pix.sizeimage = 3319 estimate_stream_size(f->fmt.pix.width, f->fmt.pix.height); 3320 3321 return 0; 3322 } 3323 3324 static int allegro_s_fmt_vid_cap(struct file *file, void *fh, 3325 struct v4l2_format *f) 3326 { 3327 struct allegro_channel *channel = file_to_channel(file); 3328 struct vb2_queue *vq; 3329 int err; 3330 3331 err = allegro_try_fmt_vid_cap(file, fh, f); 3332 if (err) 3333 return err; 3334 3335 vq = v4l2_m2m_get_vq(channel->fh.m2m_ctx, f->type); 3336 if (!vq) 3337 return -EINVAL; 3338 if (vb2_is_busy(vq)) 3339 return -EBUSY; 3340 3341 channel->codec = f->fmt.pix.pixelformat; 3342 3343 allegro_channel_adjust(channel); 3344 3345 return 0; 3346 } 3347 3348 static int allegro_g_fmt_vid_out(struct file *file, void *fh, 3349 struct v4l2_format *f) 3350 { 3351 struct allegro_channel *channel = file_to_channel(file); 3352 3353 f->fmt.pix.field = V4L2_FIELD_NONE; 3354 3355 f->fmt.pix.width = channel->width; 3356 f->fmt.pix.height = channel->height; 3357 3358 f->fmt.pix.colorspace = channel->colorspace; 3359 f->fmt.pix.ycbcr_enc = channel->ycbcr_enc; 3360 f->fmt.pix.quantization = channel->quantization; 3361 f->fmt.pix.xfer_func = channel->xfer_func; 3362 3363 f->fmt.pix.pixelformat = channel->pixelformat; 3364 f->fmt.pix.bytesperline = channel->stride; 3365 f->fmt.pix.sizeimage = channel->sizeimage_raw; 3366 3367 return 0; 3368 } 3369 3370 static int allegro_try_fmt_vid_out(struct file *file, void *fh, 3371 struct v4l2_format *f) 3372 { 3373 f->fmt.pix.field = V4L2_FIELD_NONE; 3374 3375 /* 3376 * The firmware of the Allegro codec handles the padding internally 3377 * and expects the visual frame size when configuring a channel. 3378 * Therefore, unlike other encoder drivers, this driver does not round 3379 * up the width and height to macroblock alignment and does not 3380 * implement the selection api. 3381 */ 3382 f->fmt.pix.width = clamp_t(__u32, f->fmt.pix.width, 3383 ALLEGRO_WIDTH_MIN, ALLEGRO_WIDTH_MAX); 3384 f->fmt.pix.height = clamp_t(__u32, f->fmt.pix.height, 3385 ALLEGRO_HEIGHT_MIN, ALLEGRO_HEIGHT_MAX); 3386 3387 f->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12; 3388 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 32); 3389 f->fmt.pix.sizeimage = 3390 f->fmt.pix.bytesperline * f->fmt.pix.height * 3 / 2; 3391 3392 return 0; 3393 } 3394 3395 static int allegro_s_fmt_vid_out(struct file *file, void *fh, 3396 struct v4l2_format *f) 3397 { 3398 struct allegro_channel *channel = file_to_channel(file); 3399 int err; 3400 3401 err = allegro_try_fmt_vid_out(file, fh, f); 3402 if (err) 3403 return err; 3404 3405 channel->width = f->fmt.pix.width; 3406 channel->height = f->fmt.pix.height; 3407 channel->stride = f->fmt.pix.bytesperline; 3408 channel->sizeimage_raw = f->fmt.pix.sizeimage; 3409 3410 channel->colorspace = f->fmt.pix.colorspace; 3411 channel->ycbcr_enc = f->fmt.pix.ycbcr_enc; 3412 channel->quantization = f->fmt.pix.quantization; 3413 channel->xfer_func = f->fmt.pix.xfer_func; 3414 3415 allegro_channel_adjust(channel); 3416 3417 return 0; 3418 } 3419 3420 static int allegro_channel_cmd_stop(struct allegro_channel *channel) 3421 { 3422 if (v4l2_m2m_has_stopped(channel->fh.m2m_ctx)) 3423 allegro_channel_eos_event(channel); 3424 3425 return 0; 3426 } 3427 3428 static int allegro_channel_cmd_start(struct allegro_channel *channel) 3429 { 3430 if (v4l2_m2m_has_stopped(channel->fh.m2m_ctx)) 3431 vb2_clear_last_buffer_dequeued(&channel->fh.m2m_ctx->cap_q_ctx.q); 3432 3433 return 0; 3434 } 3435 3436 static int allegro_encoder_cmd(struct file *file, void *fh, 3437 struct v4l2_encoder_cmd *cmd) 3438 { 3439 struct allegro_channel *channel = file_to_channel(file); 3440 int err; 3441 3442 err = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd); 3443 if (err) 3444 return err; 3445 3446 err = v4l2_m2m_ioctl_encoder_cmd(file, fh, cmd); 3447 if (err) 3448 return err; 3449 3450 if (cmd->cmd == V4L2_ENC_CMD_STOP) 3451 err = allegro_channel_cmd_stop(channel); 3452 3453 if (cmd->cmd == V4L2_ENC_CMD_START) 3454 err = allegro_channel_cmd_start(channel); 3455 3456 return err; 3457 } 3458 3459 static int allegro_enum_framesizes(struct file *file, void *fh, 3460 struct v4l2_frmsizeenum *fsize) 3461 { 3462 switch (fsize->pixel_format) { 3463 case V4L2_PIX_FMT_HEVC: 3464 case V4L2_PIX_FMT_H264: 3465 case V4L2_PIX_FMT_NV12: 3466 break; 3467 default: 3468 return -EINVAL; 3469 } 3470 3471 if (fsize->index) 3472 return -EINVAL; 3473 3474 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; 3475 fsize->stepwise.min_width = ALLEGRO_WIDTH_MIN; 3476 fsize->stepwise.max_width = ALLEGRO_WIDTH_MAX; 3477 fsize->stepwise.step_width = 1; 3478 fsize->stepwise.min_height = ALLEGRO_HEIGHT_MIN; 3479 fsize->stepwise.max_height = ALLEGRO_HEIGHT_MAX; 3480 fsize->stepwise.step_height = 1; 3481 3482 return 0; 3483 } 3484 3485 static int allegro_ioctl_streamon(struct file *file, void *priv, 3486 enum v4l2_buf_type type) 3487 { 3488 struct allegro_channel *channel = file_to_channel(file); 3489 int err; 3490 3491 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 3492 err = allegro_create_channel(channel); 3493 if (err) 3494 return err; 3495 } 3496 3497 return v4l2_m2m_streamon(file, channel->fh.m2m_ctx, type); 3498 } 3499 3500 static int allegro_g_parm(struct file *file, void *fh, 3501 struct v4l2_streamparm *a) 3502 { 3503 struct allegro_channel *channel = file_to_channel(file); 3504 struct v4l2_fract *timeperframe; 3505 3506 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 3507 return -EINVAL; 3508 3509 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 3510 timeperframe = &a->parm.output.timeperframe; 3511 timeperframe->numerator = channel->framerate.denominator; 3512 timeperframe->denominator = channel->framerate.numerator; 3513 3514 return 0; 3515 } 3516 3517 static int allegro_s_parm(struct file *file, void *fh, 3518 struct v4l2_streamparm *a) 3519 { 3520 struct allegro_channel *channel = file_to_channel(file); 3521 struct v4l2_fract *timeperframe; 3522 int div; 3523 3524 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 3525 return -EINVAL; 3526 3527 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 3528 timeperframe = &a->parm.output.timeperframe; 3529 3530 if (timeperframe->numerator == 0 || timeperframe->denominator == 0) 3531 return allegro_g_parm(file, fh, a); 3532 3533 div = gcd(timeperframe->denominator, timeperframe->numerator); 3534 channel->framerate.numerator = timeperframe->denominator / div; 3535 channel->framerate.denominator = timeperframe->numerator / div; 3536 3537 return 0; 3538 } 3539 3540 static int allegro_subscribe_event(struct v4l2_fh *fh, 3541 const struct v4l2_event_subscription *sub) 3542 { 3543 switch (sub->type) { 3544 case V4L2_EVENT_EOS: 3545 return v4l2_event_subscribe(fh, sub, 0, NULL); 3546 default: 3547 return v4l2_ctrl_subscribe_event(fh, sub); 3548 } 3549 } 3550 3551 static const struct v4l2_ioctl_ops allegro_ioctl_ops = { 3552 .vidioc_querycap = allegro_querycap, 3553 .vidioc_enum_fmt_vid_cap = allegro_enum_fmt_vid, 3554 .vidioc_enum_fmt_vid_out = allegro_enum_fmt_vid, 3555 .vidioc_g_fmt_vid_cap = allegro_g_fmt_vid_cap, 3556 .vidioc_try_fmt_vid_cap = allegro_try_fmt_vid_cap, 3557 .vidioc_s_fmt_vid_cap = allegro_s_fmt_vid_cap, 3558 .vidioc_g_fmt_vid_out = allegro_g_fmt_vid_out, 3559 .vidioc_try_fmt_vid_out = allegro_try_fmt_vid_out, 3560 .vidioc_s_fmt_vid_out = allegro_s_fmt_vid_out, 3561 3562 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 3563 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 3564 3565 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 3566 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 3567 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 3568 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 3569 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 3570 3571 .vidioc_streamon = allegro_ioctl_streamon, 3572 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 3573 3574 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 3575 .vidioc_encoder_cmd = allegro_encoder_cmd, 3576 .vidioc_enum_framesizes = allegro_enum_framesizes, 3577 3578 .vidioc_g_parm = allegro_g_parm, 3579 .vidioc_s_parm = allegro_s_parm, 3580 3581 .vidioc_subscribe_event = allegro_subscribe_event, 3582 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 3583 }; 3584 3585 static const struct v4l2_file_operations allegro_fops = { 3586 .owner = THIS_MODULE, 3587 .open = allegro_open, 3588 .release = allegro_release, 3589 .poll = v4l2_m2m_fop_poll, 3590 .unlocked_ioctl = video_ioctl2, 3591 .mmap = v4l2_m2m_fop_mmap, 3592 }; 3593 3594 static int allegro_register_device(struct allegro_dev *dev) 3595 { 3596 struct video_device *video_dev = &dev->video_dev; 3597 3598 strscpy(video_dev->name, "allegro", sizeof(video_dev->name)); 3599 video_dev->fops = &allegro_fops; 3600 video_dev->ioctl_ops = &allegro_ioctl_ops; 3601 video_dev->release = video_device_release_empty; 3602 video_dev->lock = &dev->lock; 3603 video_dev->v4l2_dev = &dev->v4l2_dev; 3604 video_dev->vfl_dir = VFL_DIR_M2M; 3605 video_dev->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; 3606 video_set_drvdata(video_dev, dev); 3607 3608 return video_register_device(video_dev, VFL_TYPE_VIDEO, 0); 3609 } 3610 3611 static void allegro_device_run(void *priv) 3612 { 3613 struct allegro_channel *channel = priv; 3614 struct allegro_dev *dev = channel->dev; 3615 struct vb2_v4l2_buffer *src_buf; 3616 struct vb2_v4l2_buffer *dst_buf; 3617 dma_addr_t src_y; 3618 dma_addr_t src_uv; 3619 dma_addr_t dst_addr; 3620 unsigned long dst_size; 3621 u64 src_handle; 3622 u64 dst_handle; 3623 3624 dst_buf = v4l2_m2m_dst_buf_remove(channel->fh.m2m_ctx); 3625 dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0); 3626 dst_size = vb2_plane_size(&dst_buf->vb2_buf, 0); 3627 dst_handle = allegro_put_buffer(channel, &channel->stream_shadow_list, 3628 dst_buf); 3629 allegro_mcu_send_put_stream_buffer(dev, channel, dst_addr, dst_size, 3630 dst_handle); 3631 3632 src_buf = v4l2_m2m_src_buf_remove(channel->fh.m2m_ctx); 3633 src_buf->sequence = channel->osequence++; 3634 src_y = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0); 3635 src_uv = src_y + (channel->stride * channel->height); 3636 src_handle = allegro_put_buffer(channel, &channel->source_shadow_list, 3637 src_buf); 3638 allegro_mcu_send_encode_frame(dev, channel, src_y, src_uv, src_handle); 3639 3640 v4l2_m2m_job_finish(dev->m2m_dev, channel->fh.m2m_ctx); 3641 } 3642 3643 static const struct v4l2_m2m_ops allegro_m2m_ops = { 3644 .device_run = allegro_device_run, 3645 }; 3646 3647 static int allegro_mcu_hw_init(struct allegro_dev *dev, 3648 const struct fw_info *info) 3649 { 3650 int err; 3651 3652 dev->mbox_command = allegro_mbox_init(dev, info->mailbox_cmd, 3653 info->mailbox_size); 3654 dev->mbox_status = allegro_mbox_init(dev, info->mailbox_status, 3655 info->mailbox_size); 3656 if (IS_ERR(dev->mbox_command) || IS_ERR(dev->mbox_status)) { 3657 v4l2_err(&dev->v4l2_dev, 3658 "failed to initialize mailboxes\n"); 3659 return -EIO; 3660 } 3661 3662 err = allegro_encoder_buffer_init(dev, &dev->encoder_buffer); 3663 dev->has_encoder_buffer = (err == 0); 3664 if (!dev->has_encoder_buffer) 3665 v4l2_info(&dev->v4l2_dev, "encoder buffer not available\n"); 3666 3667 allegro_mcu_enable_interrupts(dev); 3668 3669 /* The mcu sends INIT after reset. */ 3670 allegro_mcu_start(dev); 3671 err = allegro_mcu_wait_for_init_timeout(dev, 5000); 3672 if (err < 0) { 3673 v4l2_err(&dev->v4l2_dev, 3674 "mcu did not send INIT after reset\n"); 3675 err = -EIO; 3676 goto err_disable_interrupts; 3677 } 3678 3679 err = allegro_alloc_buffer(dev, &dev->suballocator, 3680 info->suballocator_size); 3681 if (err) { 3682 v4l2_err(&dev->v4l2_dev, 3683 "failed to allocate %zu bytes for suballocator\n", 3684 info->suballocator_size); 3685 goto err_reset_mcu; 3686 } 3687 3688 allegro_mcu_send_init(dev, dev->suballocator.paddr, 3689 dev->suballocator.size); 3690 err = allegro_mcu_wait_for_init_timeout(dev, 5000); 3691 if (err < 0) { 3692 v4l2_err(&dev->v4l2_dev, 3693 "mcu failed to configure sub-allocator\n"); 3694 err = -EIO; 3695 goto err_free_suballocator; 3696 } 3697 3698 return 0; 3699 3700 err_free_suballocator: 3701 allegro_free_buffer(dev, &dev->suballocator); 3702 err_reset_mcu: 3703 allegro_mcu_reset(dev); 3704 err_disable_interrupts: 3705 allegro_mcu_disable_interrupts(dev); 3706 3707 return err; 3708 } 3709 3710 static int allegro_mcu_hw_deinit(struct allegro_dev *dev) 3711 { 3712 int err; 3713 3714 err = allegro_mcu_reset(dev); 3715 if (err) 3716 v4l2_warn(&dev->v4l2_dev, 3717 "mcu failed to enter sleep state\n"); 3718 3719 err = allegro_mcu_disable_interrupts(dev); 3720 if (err) 3721 v4l2_warn(&dev->v4l2_dev, 3722 "failed to disable interrupts\n"); 3723 3724 allegro_free_buffer(dev, &dev->suballocator); 3725 3726 return 0; 3727 } 3728 3729 static void allegro_fw_callback(const struct firmware *fw, void *context) 3730 { 3731 struct allegro_dev *dev = context; 3732 const char *fw_codec_name = "al5e.fw"; 3733 const struct firmware *fw_codec; 3734 int err; 3735 3736 if (!fw) 3737 return; 3738 3739 v4l2_dbg(1, debug, &dev->v4l2_dev, 3740 "requesting codec firmware '%s'\n", fw_codec_name); 3741 err = request_firmware(&fw_codec, fw_codec_name, &dev->plat_dev->dev); 3742 if (err) 3743 goto err_release_firmware; 3744 3745 dev->fw_info = allegro_get_firmware_info(dev, fw, fw_codec); 3746 if (!dev->fw_info) { 3747 v4l2_err(&dev->v4l2_dev, "firmware is not supported\n"); 3748 goto err_release_firmware_codec; 3749 } 3750 3751 v4l2_info(&dev->v4l2_dev, 3752 "using mcu firmware version '%s'\n", dev->fw_info->version); 3753 3754 pm_runtime_enable(&dev->plat_dev->dev); 3755 err = pm_runtime_resume_and_get(&dev->plat_dev->dev); 3756 if (err) 3757 goto err_release_firmware_codec; 3758 3759 /* Ensure that the mcu is sleeping at the reset vector */ 3760 err = allegro_mcu_reset(dev); 3761 if (err) { 3762 v4l2_err(&dev->v4l2_dev, "failed to reset mcu\n"); 3763 goto err_suspend; 3764 } 3765 3766 allegro_copy_firmware(dev, fw->data, fw->size); 3767 allegro_copy_fw_codec(dev, fw_codec->data, fw_codec->size); 3768 3769 err = allegro_mcu_hw_init(dev, dev->fw_info); 3770 if (err) { 3771 v4l2_err(&dev->v4l2_dev, "failed to initialize mcu\n"); 3772 goto err_free_fw_codec; 3773 } 3774 3775 dev->m2m_dev = v4l2_m2m_init(&allegro_m2m_ops); 3776 if (IS_ERR(dev->m2m_dev)) { 3777 v4l2_err(&dev->v4l2_dev, "failed to init mem2mem device\n"); 3778 goto err_mcu_hw_deinit; 3779 } 3780 3781 err = allegro_register_device(dev); 3782 if (err) { 3783 v4l2_err(&dev->v4l2_dev, "failed to register video device\n"); 3784 goto err_m2m_release; 3785 } 3786 3787 v4l2_dbg(1, debug, &dev->v4l2_dev, 3788 "allegro codec registered as /dev/video%d\n", 3789 dev->video_dev.num); 3790 3791 dev->initialized = true; 3792 3793 release_firmware(fw_codec); 3794 release_firmware(fw); 3795 3796 return; 3797 3798 err_m2m_release: 3799 v4l2_m2m_release(dev->m2m_dev); 3800 dev->m2m_dev = NULL; 3801 err_mcu_hw_deinit: 3802 allegro_mcu_hw_deinit(dev); 3803 err_free_fw_codec: 3804 allegro_free_fw_codec(dev); 3805 err_suspend: 3806 pm_runtime_put(&dev->plat_dev->dev); 3807 pm_runtime_disable(&dev->plat_dev->dev); 3808 err_release_firmware_codec: 3809 release_firmware(fw_codec); 3810 err_release_firmware: 3811 release_firmware(fw); 3812 } 3813 3814 static int allegro_firmware_request_nowait(struct allegro_dev *dev) 3815 { 3816 const char *fw = "al5e_b.fw"; 3817 3818 v4l2_dbg(1, debug, &dev->v4l2_dev, 3819 "requesting firmware '%s'\n", fw); 3820 return request_firmware_nowait(THIS_MODULE, true, fw, 3821 &dev->plat_dev->dev, GFP_KERNEL, dev, 3822 allegro_fw_callback); 3823 } 3824 3825 static int allegro_probe(struct platform_device *pdev) 3826 { 3827 struct allegro_dev *dev; 3828 struct resource *res, *sram_res; 3829 int ret; 3830 int irq; 3831 void __iomem *regs, *sram_regs; 3832 3833 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 3834 if (!dev) 3835 return -ENOMEM; 3836 dev->plat_dev = pdev; 3837 init_completion(&dev->init_complete); 3838 INIT_LIST_HEAD(&dev->channels); 3839 3840 mutex_init(&dev->lock); 3841 3842 dev->initialized = false; 3843 3844 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs"); 3845 if (!res) { 3846 dev_err(&pdev->dev, 3847 "regs resource missing from device tree\n"); 3848 return -EINVAL; 3849 } 3850 regs = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 3851 if (!regs) { 3852 dev_err(&pdev->dev, "failed to map registers\n"); 3853 return -ENOMEM; 3854 } 3855 dev->regmap = devm_regmap_init_mmio(&pdev->dev, regs, 3856 &allegro_regmap_config); 3857 if (IS_ERR(dev->regmap)) { 3858 dev_err(&pdev->dev, "failed to init regmap\n"); 3859 return PTR_ERR(dev->regmap); 3860 } 3861 3862 sram_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram"); 3863 if (!sram_res) { 3864 dev_err(&pdev->dev, 3865 "sram resource missing from device tree\n"); 3866 return -EINVAL; 3867 } 3868 sram_regs = devm_ioremap(&pdev->dev, 3869 sram_res->start, 3870 resource_size(sram_res)); 3871 if (!sram_regs) { 3872 dev_err(&pdev->dev, "failed to map sram\n"); 3873 return -ENOMEM; 3874 } 3875 dev->sram = devm_regmap_init_mmio(&pdev->dev, sram_regs, 3876 &allegro_sram_config); 3877 if (IS_ERR(dev->sram)) { 3878 dev_err(&pdev->dev, "failed to init sram\n"); 3879 return PTR_ERR(dev->sram); 3880 } 3881 3882 dev->settings = syscon_regmap_lookup_by_compatible("xlnx,vcu-settings"); 3883 if (IS_ERR(dev->settings)) 3884 dev_warn(&pdev->dev, "failed to open settings\n"); 3885 3886 dev->clk_core = devm_clk_get(&pdev->dev, "core_clk"); 3887 if (IS_ERR(dev->clk_core)) 3888 return PTR_ERR(dev->clk_core); 3889 3890 dev->clk_mcu = devm_clk_get(&pdev->dev, "mcu_clk"); 3891 if (IS_ERR(dev->clk_mcu)) 3892 return PTR_ERR(dev->clk_mcu); 3893 3894 irq = platform_get_irq(pdev, 0); 3895 if (irq < 0) 3896 return irq; 3897 ret = devm_request_threaded_irq(&pdev->dev, irq, 3898 allegro_hardirq, 3899 allegro_irq_thread, 3900 IRQF_SHARED, dev_name(&pdev->dev), dev); 3901 if (ret < 0) { 3902 dev_err(&pdev->dev, "failed to request irq: %d\n", ret); 3903 return ret; 3904 } 3905 3906 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 3907 if (ret) 3908 return ret; 3909 3910 platform_set_drvdata(pdev, dev); 3911 3912 ret = allegro_firmware_request_nowait(dev); 3913 if (ret < 0) { 3914 v4l2_err(&dev->v4l2_dev, 3915 "failed to request firmware: %d\n", ret); 3916 v4l2_device_unregister(&dev->v4l2_dev); 3917 return ret; 3918 } 3919 3920 return 0; 3921 } 3922 3923 static void allegro_remove(struct platform_device *pdev) 3924 { 3925 struct allegro_dev *dev = platform_get_drvdata(pdev); 3926 3927 if (dev->initialized) { 3928 video_unregister_device(&dev->video_dev); 3929 if (dev->m2m_dev) 3930 v4l2_m2m_release(dev->m2m_dev); 3931 allegro_mcu_hw_deinit(dev); 3932 allegro_free_fw_codec(dev); 3933 } 3934 3935 pm_runtime_put(&dev->plat_dev->dev); 3936 pm_runtime_disable(&dev->plat_dev->dev); 3937 3938 v4l2_device_unregister(&dev->v4l2_dev); 3939 } 3940 3941 static int allegro_runtime_resume(struct device *device) 3942 { 3943 struct allegro_dev *dev = dev_get_drvdata(device); 3944 struct regmap *settings = dev->settings; 3945 unsigned int clk_mcu; 3946 unsigned int clk_core; 3947 int err; 3948 3949 if (!settings) 3950 return -EINVAL; 3951 3952 #define MHZ_TO_HZ(freq) ((freq) * 1000 * 1000) 3953 3954 err = regmap_read(settings, VCU_CORE_CLK, &clk_core); 3955 if (err < 0) 3956 return err; 3957 err = clk_set_rate(dev->clk_core, MHZ_TO_HZ(clk_core)); 3958 if (err < 0) 3959 return err; 3960 err = clk_prepare_enable(dev->clk_core); 3961 if (err) 3962 return err; 3963 3964 err = regmap_read(settings, VCU_MCU_CLK, &clk_mcu); 3965 if (err < 0) 3966 goto disable_clk_core; 3967 err = clk_set_rate(dev->clk_mcu, MHZ_TO_HZ(clk_mcu)); 3968 if (err < 0) 3969 goto disable_clk_core; 3970 err = clk_prepare_enable(dev->clk_mcu); 3971 if (err) 3972 goto disable_clk_core; 3973 3974 #undef MHZ_TO_HZ 3975 3976 return 0; 3977 3978 disable_clk_core: 3979 clk_disable_unprepare(dev->clk_core); 3980 3981 return err; 3982 } 3983 3984 static int allegro_runtime_suspend(struct device *device) 3985 { 3986 struct allegro_dev *dev = dev_get_drvdata(device); 3987 3988 clk_disable_unprepare(dev->clk_mcu); 3989 clk_disable_unprepare(dev->clk_core); 3990 3991 return 0; 3992 } 3993 3994 static const struct of_device_id allegro_dt_ids[] = { 3995 { .compatible = "allegro,al5e-1.1" }, 3996 { /* sentinel */ } 3997 }; 3998 3999 MODULE_DEVICE_TABLE(of, allegro_dt_ids); 4000 4001 static const struct dev_pm_ops allegro_pm_ops = { 4002 .runtime_resume = allegro_runtime_resume, 4003 .runtime_suspend = allegro_runtime_suspend, 4004 }; 4005 4006 static struct platform_driver allegro_driver = { 4007 .probe = allegro_probe, 4008 .remove = allegro_remove, 4009 .driver = { 4010 .name = "allegro", 4011 .of_match_table = allegro_dt_ids, 4012 .pm = &allegro_pm_ops, 4013 }, 4014 }; 4015 4016 module_platform_driver(allegro_driver); 4017 4018 MODULE_LICENSE("GPL"); 4019 MODULE_AUTHOR("Michael Tretter <kernel@pengutronix.de>"); 4020 MODULE_DESCRIPTION("Allegro DVT encoder driver"); 4021