diff --git a/Documentation/devicetree/bindings/media/samsung,s5p-mfc.yaml b/Documentation/devicetree/bindings/media/samsung,s5p-mfc.yaml index b46cc78070..41f05d7ce4 100644 --- a/Documentation/devicetree/bindings/media/samsung,s5p-mfc.yaml +++ b/Documentation/devicetree/bindings/media/samsung,s5p-mfc.yaml @@ -18,6 +18,7 @@ properties: compatible: oneOf: - enum: + - google,gs201-mfc # Google GS201 (MFC v15) - samsung,exynos5433-mfc # Exynos5433 - samsung,mfc-v5 # Exynos4 - samsung,mfc-v6 # Exynos5 @@ -164,6 +165,7 @@ allOf: compatible: contains: enum: + - google,gs201-mfc - tesla,fsd-mfc then: properties: diff --git a/arch/arm64/boot/dts/exynos/google/gs201-panther.dts b/arch/arm64/boot/dts/exynos/google/gs201-panther.dts index 25594305a5..9986f4a90b 100644 --- a/arch/arm64/boot/dts/exynos/google/gs201-panther.dts +++ b/arch/arm64/boot/dts/exynos/google/gs201-panther.dts @@ -125,6 +125,24 @@ reg = <0x0 0xfac00000 (1080 * 2400 * 4)>; no-map; }; + + /* + * MFC firmware, contexts and frames. The MFC takes 32-bit + * addresses, its System MMU is in bypass and the firmware + * rejects any buffer below its own base, so they all come from + * one contiguous pool below 4 GiB, firmware first. Reusable, + * so the pages serve movable allocations while no video is + * decoded. 256 MiB holds the firmware and a 4K stream decoded + * to single-buffer NV12. + */ + mfc_pool: mfc-pool { + compatible = "shared-dma-pool"; + reusable; + size = <0x10000000>; + /* read with the root's #address-cells (2), not #size-cells */ + alignment = <0x0 0x200000>; + alloc-ranges = <0x0 0x80000000 0x80000000>; + }; }; }; @@ -246,6 +264,16 @@ status = "okay"; }; +/* + * Left disabled until its two System MMUs are described: in bypass, the + * closed firmware parsing untrusted video can reach all memory below + * 4 GiB. omarchy-mobile's device/panther overlay enables it for owners who + * want hardware video. + */ +&mfc { + memory-region = <&mfc_pool>; +}; + &usbdrd31_phy { status = "okay"; }; diff --git a/arch/arm64/boot/dts/exynos/google/gs201.dtsi b/arch/arm64/boot/dts/exynos/google/gs201.dtsi index 4f804ecbb4..11f0d479e5 100644 --- a/arch/arm64/boot/dts/exynos/google/gs201.dtsi +++ b/arch/arm64/boot/dts/exynos/google/gs201.dtsi @@ -2013,6 +2013,26 @@ }; }; + /* + * MFC v15.6 video codec. Its two System MMUs (0x1c870000, + * 0x1c8a0000) are left in bypass and CMU_MFC is not described + * yet, so the firmware and its buffers need memory below 4 GiB + * from the board's memory-region, and the block runs on the + * power domain the bootloader leaves on. Its rate (and voltage) + * is the ACPM firmware's MFC DVFS domain. CMU_TOP's + * gout_cmu_mfc_mfc gate feeds the block too and stays on only + * because the boards boot with clk_ignore_unused: without that + * flag the first register access would hit an unclocked block. + */ + mfc: video-codec@1c8d0000 { + compatible = "google,gs201-mfc"; + reg = <0x1c8d0000 0x10000>; + interrupts = ; + clocks = <&acpm_ipc GS201_CLK_ACPM_DVFS_MFC>; + clock-names = "mfc"; + status = "disabled"; + }; + gpu: gpu@28000000 { compatible = "google,gs201-mali", "arm,mali-valhall-csf"; reg = <0x28000000 0x480000>; diff --git a/drivers/media/platform/samsung/s5p-mfc/regs-mfc-v15.h b/drivers/media/platform/samsung/s5p-mfc/regs-mfc-v15.h new file mode 100644 index 0000000000..84d756a3fa --- /dev/null +++ b/drivers/media/platform/samsung/s5p-mfc/regs-mfc-v15.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Register definition file for Samsung MFC V15.x Interface (FIMV) driver + * + * MFC v15.6 is found in the Google GS201 SoC. Its register map is the v8+ + * map used by v12; only the differences used by this driver are listed here. + */ + +#ifndef _REGS_MFC_V15_H +#define _REGS_MFC_V15_H + +#include +#include "regs-mfc-v12.h" + +/* Firmware base for the non-secure firmware; 0x0054 is the secure one */ +#define S5P_FIMV_RISC_NONSECURE_BASE_ADDR_V15 0x0068 +#define S5P_FIMV_MFC_VERSION_V15 0xF028 +#define S5P_FIMV_FW_STATUS_INFO_V15 0xF0A4 +#define S5P_FIMV_FW_STATUS_READY_V15 BIT(0) + +/* Dynamic DPB */ +#define S5P_FIMV_D_NAL_START_OPTIONS_V15 0xF5AC +#define S5P_FIMV_D_AVAILABLE_DPB_FLAG_UPPER_V15 0xF5B8 +#define S5P_FIMV_D_DYNAMIC_DPB_FLAG_UPPER_V15 0xF5D4 +#define S5P_FIMV_D_DYNAMIC_DPB_FLAG_LOWER_V15 0xF5D8 +#define S5P_FIMV_D_USED_DPB_FLAG_UPPER_V15 0xF720 +#define S5P_FIMV_D_USED_DPB_FLAG_LOWER_V15 0xF724 +#define S5P_FIMV_D_INIT_BUF_OPT_DYNAMIC_DPB_V15 BIT(3) + +/* MFCv15 variant defines */ +#define MFC_VERSION_V15 0xF0 +#define MFC_HW_VERSION_V15 0x15060000 +#define MFC_NUM_PORTS_V15 1 +#define MAX_FW_SIZE_V15 (SZ_1M) +#define MAX_CPB_SIZE_V15 (7 * SZ_1M) +#define MFC_OTHER_DEC_CTX_BUF_SIZE_V15 (60 * SZ_1K) +#define MFC_HEVC_ENC_CTX_BUF_SIZE_V15 (50 * SZ_1K) +#define MFC_OTHER_ENC_CTX_BUF_SIZE_V15 (50 * SZ_1K) + +/* + * The firmware rejects every buffer below its own base address, so the + * firmware and all the driver's private buffers share one region with the + * firmware at its start. + */ +#define MFC_MEM_SIZE_V15 (64 * SZ_1M) + +#endif /*_REGS_MFC_V15_H*/ diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc.c b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc.c index 32eb402d43..d3ac63b53b 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc.c +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc.c @@ -356,6 +356,10 @@ static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err) ctx->luma_size); vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, ctx->chroma_size); + /* NV12 in one buffer: plane 0 holds the whole frame */ + if (ctx->dst_fmt->num_planes == 1) + vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, + s5p_mfc_dec_nv12_size(ctx)); clear_bit(dst_buf->b->vb2_buf.index, &ctx->dec_dst_flag); @@ -381,6 +385,9 @@ static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx, & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK; dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev) & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK; + /* DPBs the firmware still holds (references or not yet displayed) */ + if (dev->dynamic_dpb) + ctx->dpb_used = readl(dev->mfc_regs->d_used_dpb_flag_lower); res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev) & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK) >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT; @@ -540,13 +547,34 @@ static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx, if (FW_HAS_E_MIN_SCRATCH_BUF(dev)) ctx->scratch_buf_size = s5p_mfc_hw_call(dev->mfc_ops, get_min_scratch_buf_size, dev); - if (ctx->img_width == 0 || ctx->img_height == 0) + if (ctx->img_width == 0 || ctx->img_height == 0) { ctx->state = MFCINST_ERROR; - else + } else { + static const struct v4l2_event ev_src_ch = { + .type = V4L2_EVENT_SOURCE_CHANGE, + .u.src_change.changes = + V4L2_EVENT_SRC_CH_RESOLUTION, + }; + ctx->state = MFCINST_HEAD_PARSED; + /* + * The stateful decoder interface (dev-decoder.rst, + * "Initialization"): the client learns the stream's + * format from this event, not by calling G_FMT on + * CAPTURE in a loop. + */ + v4l2_event_queue_fh(&ctx->fh, &ev_src_ch); + } + /* + * The buffer the header came in may hold the first frame too. + * Keep it for decoding unless the header used it up: on MFC + * v15 for every codec, as the first HEVC IRAP picture and the + * first VP8/VP9 key frame share the header's buffer. + */ if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC || - ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) && + ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC || + IS_MFCV15(dev)) && !list_empty(&ctx->src_queue)) { struct s5p_mfc_buf *src_buf; @@ -1219,10 +1247,28 @@ static int s5p_mfc_configure_common_memory(struct s5p_mfc_dev *mfc_dev) { struct device *dev = &mfc_dev->plat_dev->dev; unsigned long mem_size = SZ_4M; + int ret; if (IS_ENABLED(CONFIG_DMA_CMA) || exynos_is_iommu_available(dev)) mem_size = SZ_8M; + if (IS_MFCV15(mfc_dev)) { + /* + * The firmware sees physical addresses through 32-bit + * registers and rejects buffers below its own base, so all of + * the private buffers (codec buffers included) live in this + * region, after the firmware. An optional "memory-region" + * also serves the vb2 buffers from the same pool. + */ + mem_size = MFC_MEM_SIZE_V15; + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); + if (ret) + return ret; + ret = of_reserved_mem_device_init(dev); + if (ret && ret != -ENODEV) + return ret; + } + if (mfc_mem_size) mem_size = memparse(mfc_mem_size, NULL); @@ -1234,6 +1280,8 @@ static int s5p_mfc_configure_common_memory(struct s5p_mfc_dev *mfc_dev) &mfc_dev->mem_base, GFP_KERNEL); if (!mfc_dev->mem_virt) { bitmap_free(mfc_dev->mem_bitmap); + if (IS_MFCV15(mfc_dev)) + of_reserved_mem_device_release(dev); dev_err(dev, "failed to preallocate %ld MiB for the firmware and context buffers\n", (mem_size / SZ_1M)); return -ENOMEM; @@ -1274,6 +1322,8 @@ static void s5p_mfc_unconfigure_common_memory(struct s5p_mfc_dev *mfc_dev) mfc_dev->mem_base); bitmap_free(mfc_dev->mem_bitmap); vb2_dma_contig_clear_max_seg_size(dev); + if (IS_MFCV15(mfc_dev)) + of_reserved_mem_device_release(dev); } static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev) @@ -1322,6 +1372,7 @@ static int s5p_mfc_probe(struct platform_device *pdev) dev_err(&pdev->dev, "Failed to get device MFC hardware variant information\n"); return -ENOENT; } + dev->dynamic_dpb = IS_MFCV15(dev); dev->regs_base = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(dev->regs_base)) @@ -1419,6 +1470,18 @@ static int s5p_mfc_probe(struct platform_device *pdev) v4l2_info(&dev->v4l2_dev, "decoder registered as /dev/video%d\n", dev->vfd_dec->num); + /* + * The v15 encoder's registers differ from v12's (0xF770/0xF774 clash + * with the decoder's) and nothing here programs them yet: no encoder + * node rather than one that hangs the firmware. + */ + if (IS_MFCV15(dev)) { + video_device_release(dev->vfd_enc); + dev->vfd_enc = NULL; + pr_debug("%s--\n", __func__); + return 0; + } + ret = video_register_device(dev->vfd_enc, VFL_TYPE_VIDEO, 0); if (ret) { v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); @@ -1705,6 +1768,35 @@ static struct s5p_mfc_variant mfc_drvdata_v12 = { .num_clocks = 1, }; +/* Google's gs201 sizes (mfc_ctx_buf_size in its exynos-mfc driver) */ +static const struct s5p_mfc_buf_size_v6 mfc_buf_size_v15 = { + .dev_ctx = MFC_CTX_BUF_SIZE_V12, + .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V12, + .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V15, + .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V12, + .hevc_enc_ctx = MFC_HEVC_ENC_CTX_BUF_SIZE_V15, + .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V15, +}; + +static const struct s5p_mfc_buf_size buf_size_v15 = { + .fw = MAX_FW_SIZE_V15, + .cpb = MAX_CPB_SIZE_V15, + .priv = &mfc_buf_size_v15, +}; + +static const struct s5p_mfc_variant mfc_drvdata_v15 = { + .version = MFC_VERSION_V15, + .version_bit = MFC_V15_BIT, + .port_num = MFC_NUM_PORTS_V15, + .buf_size = &buf_size_v15, + .fw_name[0] = "s5p-mfc-v15.fw", + .clk_names = {"mfc"}, + .num_clocks = 1, + .clocks_optional = true, + /* The top MFC rate of Google's gs201 tables (mfc_freqs) */ + .active_clk_rate = 666000000, +}; + static const struct of_device_id exynos_mfc_match[] = { { .compatible = "samsung,mfc-v5", @@ -1730,6 +1822,9 @@ static const struct of_device_id exynos_mfc_match[] = { }, { .compatible = "tesla,fsd-mfc", .data = &mfc_drvdata_v12, + }, { + .compatible = "google,gs201-mfc", + .data = &mfc_drvdata_v15, }, {}, }; diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_common.h b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_common.h index 58dc176808..8abf5104e0 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_common.h +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_common.h @@ -19,7 +19,7 @@ #include #include #include "regs-mfc.h" -#include "regs-mfc-v12.h" +#include "regs-mfc-v15.h" #define S5P_MFC_NAME "s5p-mfc" @@ -199,6 +199,7 @@ struct s5p_mfc_pm { struct clk *clocks[MFC_MAX_CLOCKS]; int num_clocks; bool use_clock_gating; + unsigned long idle_clk_rate; struct device *device; }; @@ -225,6 +226,9 @@ struct s5p_mfc_buf_size { const void *priv; }; +struct s5p_mfc_ctx; +size_t s5p_mfc_dec_nv12_size(const struct s5p_mfc_ctx *ctx); + struct s5p_mfc_variant { unsigned int version; unsigned int port_num; @@ -234,6 +238,8 @@ struct s5p_mfc_variant { const char *clk_names[MFC_MAX_CLOCKS]; int num_clocks; bool use_clock_gating; + bool clocks_optional; + unsigned long active_clk_rate; }; /** @@ -300,6 +306,7 @@ struct s5p_mfc_priv_buf { * @fw_get_done: flag set when request_firmware() is complete and * copied into fw_buf * @risc_on: flag indicates RISC is on or off + * @dynamic_dpb: decoder hands one DPB to the firmware per frame (MFCv15) * */ struct s5p_mfc_dev { @@ -347,6 +354,7 @@ struct s5p_mfc_dev { enum s5p_mfc_fw_ver fw_ver; bool fw_get_done; bool risc_on; /* indicates if RISC is on or off */ + bool dynamic_dpb; }; /* @@ -591,6 +599,7 @@ struct s5p_mfc_codec_ops { * @dst_bufs_cnt: number of allocated destination buffers * @sequence: counter for the sequence number for v4l2 * @dec_dst_flag: flags for buffers queued in the hardware + * @dpb_used: DPBs still held by the firmware (dynamic DPB, MFCv15) * @dec_src_buf_size: size of the buffer for source buffers in decoding * @codec_mode: number of codec mode used by MFC hw * @slice_interface: slice interface flag @@ -684,6 +693,7 @@ struct s5p_mfc_ctx { unsigned int sequence; unsigned long dec_dst_flag; + unsigned long dpb_used; size_t dec_src_buf_size; /* Control values */ @@ -790,6 +800,7 @@ void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq); #define IS_MFCV8_PLUS(dev) ((dev)->variant->version >= 0x80) #define IS_MFCV10_PLUS(dev) ((dev)->variant->version >= 0xA0) #define IS_MFCV12(dev) ((dev)->variant->version >= 0xC0) +#define IS_MFCV15(dev) ((dev)->variant->version >= 0xF0) #define FW_HAS_E_MIN_SCRATCH_BUF(dev) (IS_MFCV10_PLUS(dev)) #define MFC_V5_BIT BIT(0) @@ -798,14 +809,16 @@ void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq); #define MFC_V8_BIT BIT(3) #define MFC_V10_BIT BIT(5) #define MFC_V12_BIT BIT(7) +#define MFC_V15_BIT BIT(8) #define MFC_V5PLUS_BITS (MFC_V5_BIT | MFC_V6_BIT | MFC_V7_BIT | \ - MFC_V8_BIT | MFC_V10_BIT | MFC_V12_BIT) + MFC_V8_BIT | MFC_V10_BIT | MFC_V12_BIT | \ + MFC_V15_BIT) #define MFC_V6PLUS_BITS (MFC_V6_BIT | MFC_V7_BIT | MFC_V8_BIT | \ - MFC_V10_BIT | MFC_V12_BIT) + MFC_V10_BIT | MFC_V12_BIT | MFC_V15_BIT) #define MFC_V7PLUS_BITS (MFC_V7_BIT | MFC_V8_BIT | MFC_V10_BIT | \ - MFC_V12_BIT) + MFC_V12_BIT | MFC_V15_BIT) -#define MFC_V10PLUS_BITS (MFC_V10_BIT | MFC_V12_BIT) +#define MFC_V10PLUS_BITS (MFC_V10_BIT | MFC_V12_BIT | MFC_V15_BIT) #endif /* S5P_MFC_COMMON_H_ */ diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_ctrl.c b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_ctrl.c index 625d77b2be..b722090701 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_ctrl.c +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_ctrl.c @@ -178,7 +178,13 @@ int s5p_mfc_reset(struct s5p_mfc_dev *dev) static inline void s5p_mfc_init_memctrl(struct s5p_mfc_dev *dev) { - if (IS_MFCV6_PLUS(dev)) { + if (IS_MFCV15(dev)) { + /* 0x0054 is the secure firmware base on MFCv15 */ + mfc_write(dev, dev->dma_base[BANK_L_CTX], + S5P_FIMV_RISC_NONSECURE_BASE_ADDR_V15); + mfc_debug(2, "Base Address : %pad\n", + &dev->dma_base[BANK_L_CTX]); + } else if (IS_MFCV6_PLUS(dev)) { mfc_write(dev, dev->dma_base[BANK_L_CTX], S5P_FIMV_RISC_BASE_ADDRESS_V6); mfc_debug(2, "Base Address : %pad\n", @@ -207,6 +213,34 @@ static inline void s5p_mfc_clear_cmds(struct s5p_mfc_dev *dev) } } +/* + * MFCv15: check that the IP is the one the driver knows and wait until the + * firmware accepts commands, as the vendor driver does before SYS_INIT. + */ +static int s5p_mfc_v15_check_fw(struct s5p_mfc_dev *dev) +{ + unsigned long timeout; + u32 ver; + + ver = mfc_read(dev, S5P_FIMV_MFC_VERSION_V15); + if (ver != MFC_HW_VERSION_V15) { + mfc_err("Unsupported MFC IP version 0x%08x (expected 0x%08x)\n", + ver, MFC_HW_VERSION_V15); + return -ENODEV; + } + + timeout = jiffies + msecs_to_jiffies(MFC_BW_TIMEOUT); + while (!(mfc_read(dev, S5P_FIMV_FW_STATUS_INFO_V15) & + S5P_FIMV_FW_STATUS_READY_V15)) { + if (time_after(jiffies, timeout)) { + mfc_err("Timeout waiting for the firmware status\n"); + return -EIO; + } + usleep_range(100, 200); + } + return 0; +} + /* Initialize hardware */ int s5p_mfc_init_hw(struct s5p_mfc_dev *dev) { @@ -235,6 +269,10 @@ int s5p_mfc_init_hw(struct s5p_mfc_dev *dev) s5p_mfc_clear_cmds(dev); /* 3. Release reset signal to the RISC */ s5p_mfc_clean_dev_int_flags(dev); + /* MFCv15 needs the clock ungated before the RISC starts */ + if (IS_MFCV15(dev)) + mfc_write(dev, 0x0, S5P_FIMV_MFC_CLOCK_OFF_V10); + if (IS_MFCV6_PLUS(dev)) { dev->risc_on = 1; mfc_write(dev, 0x1, S5P_FIMV_RISC_ON_V6); @@ -242,7 +280,7 @@ int s5p_mfc_init_hw(struct s5p_mfc_dev *dev) else mfc_write(dev, 0x3ff, S5P_FIMV_SW_RESET); - if (IS_MFCV10_PLUS(dev)) + if (IS_MFCV10_PLUS(dev) && !IS_MFCV15(dev)) mfc_write(dev, 0x0, S5P_FIMV_MFC_CLOCK_OFF_V10); mfc_debug(2, "Will now wait for completion of firmware transfer\n"); @@ -253,6 +291,14 @@ int s5p_mfc_init_hw(struct s5p_mfc_dev *dev) return -EIO; } s5p_mfc_clean_dev_int_flags(dev); + if (IS_MFCV15(dev)) { + ret = s5p_mfc_v15_check_fw(dev); + if (ret) { + s5p_mfc_reset(dev); + s5p_mfc_clock_off(dev); + return ret; + } + } /* 4. Initialize firmware */ ret = s5p_mfc_hw_call(dev->mfc_cmds, sys_init_cmd, dev); if (ret) { diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_dec.c b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_dec.c index afd28beabf..b7615ba8d0 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_dec.c +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_dec.c @@ -61,14 +61,21 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_NONE, .type = MFC_FMT_RAW, .num_planes = 3, - .versions = MFC_V12_BIT, + .versions = MFC_V12_BIT | MFC_V15_BIT, }, { .fourcc = V4L2_PIX_FMT_YVU420M, .codec_mode = S5P_MFC_CODEC_NONE, .type = MFC_FMT_RAW, .num_planes = 3, - .versions = MFC_V12_BIT + .versions = MFC_V12_BIT | MFC_V15_BIT + }, + { + .fourcc = V4L2_PIX_FMT_NV12, + .codec_mode = S5P_FIMV_CODEC_NONE, + .type = MFC_FMT_RAW, + .num_planes = 1, + .versions = MFC_V15_BIT, }, { .fourcc = V4L2_PIX_FMT_H264, @@ -84,7 +91,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_H264_MVC_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V6PLUS_BITS, + .versions = (MFC_V6PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM, }, @@ -93,7 +100,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_H263_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, }, { @@ -101,7 +108,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_MPEG2_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM, }, @@ -110,7 +117,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_MPEG2_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM, }, @@ -119,7 +126,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_MPEG4_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM, }, @@ -128,7 +135,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_MPEG4_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, }, { @@ -136,7 +143,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_VC1_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, }, { @@ -144,7 +151,7 @@ static const struct s5p_mfc_fmt formats[] = { .codec_mode = S5P_MFC_CODEC_VC1RCV_DEC, .type = MFC_FMT_DEC, .num_planes = 1, - .versions = MFC_V5PLUS_BITS, + .versions = (MFC_V5PLUS_BITS & ~MFC_V15_BIT), .flags = V4L2_FMT_FLAG_DYN_RESOLUTION, }, { @@ -327,6 +334,7 @@ static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f, if (i == ARRAY_SIZE(formats)) return -EINVAL; f->pixelformat = formats[i].fourcc; + f->flags = formats[i].flags; return 0; } @@ -342,6 +350,36 @@ static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, return vidioc_enum_fmt(file, f, true); } +/* + * Coded sizes on MFC v15: up to H.264 level 5.2's largest frame. Without + * this, Chromium assumes 1920x1088 and decodes anything bigger in software. + */ +static int vidioc_enum_framesizes(struct file *file, void *priv, + struct v4l2_frmsizeenum *fsize) +{ + struct s5p_mfc_dev *dev = video_drvdata(file); + const struct s5p_mfc_fmt *fmt; + struct v4l2_format f = { + .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, + .fmt.pix_mp.pixelformat = fsize->pixel_format, + }; + + if (!IS_MFCV15(dev) || fsize->index) + return -EINVAL; + fmt = find_format(&f, MFC_FMT_DEC); + if (!fmt || !(dev->variant->version_bit & fmt->versions)) + return -EINVAL; + + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; + fsize->stepwise.min_width = 32; + fsize->stepwise.max_width = 4096; + fsize->stepwise.step_width = 2; + fsize->stepwise.min_height = 32; + fsize->stepwise.max_height = 2304; + fsize->stepwise.step_height = 2; + return 0; +} + /* Get format */ static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f) { @@ -377,6 +415,9 @@ static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f) pix_mp->plane_fmt[0].sizeimage = ctx->luma_size; pix_mp->plane_fmt[1].bytesperline = ctx->stride[1]; pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size; + /* NV12 in one buffer: the chroma follows the luma */ + if (ctx->dst_fmt->num_planes == 1) + pix_mp->plane_fmt[0].sizeimage = s5p_mfc_dec_nv12_size(ctx); if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YUV420M || ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YVU420M) { pix_mp->plane_fmt[2].bytesperline = ctx->stride[2]; @@ -452,7 +493,13 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f) pix_mp = &f->fmt.pix_mp; if (ret) return ret; - if (vb2_is_streaming(&ctx->vq_src) || vb2_is_streaming(&ctx->vq_dst)) { + /* + * Each queue's format is fixed only while that queue streams: the + * stateful decoder interface has the client set the CAPTURE format + * after the source change event, with OUTPUT still streaming. + */ + if (vb2_is_streaming(f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ? + &ctx->vq_dst : &ctx->vq_src)) { v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__); ret = -EBUSY; goto out; @@ -460,6 +507,13 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f) if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { /* dst_fmt is validated by call to vidioc_try_fmt */ ctx->dst_fmt = find_format(f, MFC_FMT_RAW); + /* + * After the header was parsed the plane sizes were worked out + * for the old format; NV12 in one buffer lays them out + * differently, so work them out again. + */ + if (ctx->state == MFCINST_HEAD_PARSED) + s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx); ret = 0; goto out; } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { @@ -476,6 +530,7 @@ static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f) ctx->dec_src_buf_size = buf_size->cpb; else ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage; + pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size; pix_mp->plane_fmt[0].bytesperline = 0; ctx->state = MFCINST_INIT; ret = 0; @@ -913,6 +968,7 @@ static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = { .vidioc_querycap = vidioc_querycap, .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, + .vidioc_enum_framesizes = vidioc_enum_framesizes, .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt, .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt, .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt, @@ -961,12 +1017,19 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq, mfc_err("invalid format\n"); return -EINVAL; } - *plane_count = format->comp_planes; + *plane_count = format->mem_planes; /* Setup buffer count */ if (*buf_count < ctx->pb_count) *buf_count = ctx->pb_count; - if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB) + /* + * With dynamic DPB the firmware can hold all of its minimum + * at once; leave some for the client to display. + */ + if (dev->dynamic_dpb && *buf_count < ctx->pb_count + 4) + *buf_count = ctx->pb_count + 4; + if (!dev->dynamic_dpb && + *buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB) *buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB; if (*buf_count > MFC_MAX_BUFFERS) *buf_count = MFC_MAX_BUFFERS; @@ -981,6 +1044,8 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq, vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { psize[0] = ctx->luma_size; psize[1] = ctx->chroma_size; + if (ctx->dst_fmt->num_planes == 1) + psize[0] = s5p_mfc_dec_nv12_size(ctx); if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YUV420M || ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YVU420M) psize[2] = ctx->chroma_size_1; @@ -1002,6 +1067,32 @@ static int s5p_mfc_queue_setup(struct vb2_queue *vq, return 0; } +/* + * MFCv15 firmware rejects any buffer below its own base address. The private + * buffers are allocated after it, but the vb2 buffers come from the generic + * DMA allocator and nothing guarantees their position unless the device has + * its own "memory-region". Warn so that a failure can be traced to this. + */ +static void s5p_mfc_check_buf_addr_v15(struct s5p_mfc_ctx *ctx, + struct vb2_buffer *vb, unsigned int planes) +{ + struct s5p_mfc_dev *dev = ctx->dev; + dma_addr_t addr; + unsigned int i; + + if (!IS_MFCV15(dev)) + return; + + for (i = 0; i < planes; i++) { + addr = vb2_dma_contig_plane_dma_addr(vb, i); + if (addr < dev->fw_buf.dma) + dev_warn(&dev->plat_dev->dev, + "%s buffer %u plane %u at %pad is below the firmware base %pad, the MFC will reject it\n", + V4L2_TYPE_IS_OUTPUT(vb->type) ? "OUTPUT" : "CAPTURE", + vb->index, i, &addr, &dev->fw_buf.dma); + } +} + static int s5p_mfc_buf_init(struct vb2_buffer *vb) { struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); @@ -1019,8 +1110,10 @@ static int s5p_mfc_buf_init(struct vb2_buffer *vb) return -EINVAL; } } - if (vb2_plane_size(vb, 0) < ctx->luma_size || - vb2_plane_size(vb, 1) < ctx->chroma_size) { + if (ctx->dst_fmt->num_planes == 1 ? + vb2_plane_size(vb, 0) < s5p_mfc_dec_nv12_size(ctx) : + vb2_plane_size(vb, 0) < ctx->luma_size || + vb2_plane_size(vb, 1) < ctx->chroma_size) { mfc_err("Plane buffer (CAPTURE) is too small\n"); return -EINVAL; } @@ -1031,11 +1124,16 @@ static int s5p_mfc_buf_init(struct vb2_buffer *vb) return -EINVAL; } } + s5p_mfc_check_buf_addr_v15(ctx, vb, ctx->dst_fmt->num_planes); i = vb->index; ctx->dst_bufs[i].b = vbuf; ctx->dst_bufs[i].cookie.raw.luma = vb2_dma_contig_plane_dma_addr(vb, 0); - ctx->dst_bufs[i].cookie.raw.chroma = + if (ctx->dst_fmt->num_planes == 1) + ctx->dst_bufs[i].cookie.raw.chroma = + ctx->dst_bufs[i].cookie.raw.luma + ctx->luma_size; + else + ctx->dst_bufs[i].cookie.raw.chroma = vb2_dma_contig_plane_dma_addr(vb, 1); if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YUV420M || ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YVU420M) { @@ -1054,6 +1152,7 @@ static int s5p_mfc_buf_init(struct vb2_buffer *vb) return -EINVAL; } + s5p_mfc_check_buf_addr_v15(ctx, vb, 1); i = vb->index; ctx->src_bufs[i].b = vbuf; ctx->src_bufs[i].cookie.stream = @@ -1094,6 +1193,14 @@ static void s5p_mfc_stop_streaming(struct vb2_queue *q) ctx->state == MFCINST_RUNNING) && dev->curr_ctx == ctx->num && dev->hw_lock) { ctx->state = MFCINST_ABORT; + /* + * Take the context off the run queue. Otherwise the interrupt + * handler of the frame being decoded picks it again in + * try_run, which clears the context's interrupt flags before + * finding nothing to run in MFCINST_ABORT, and the wait below + * misses that FRAME_DONE and times out. + */ + clear_work_bit(ctx); spin_unlock_irqrestore(&dev->irqlock, flags); s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0); @@ -1106,6 +1213,7 @@ static void s5p_mfc_stop_streaming(struct vb2_queue *q) ctx->dst_queue_cnt = 0; ctx->dpb_flush_flag = 1; ctx->dec_dst_flag = 0; + ctx->dpb_used = 0; if (IS_MFCV6_PLUS(dev) && (ctx->state == MFCINST_RUNNING)) { ctx->state = MFCINST_FLUSH; set_work_bit_irqsave(ctx); diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr.c b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr.c index b25ad2a371..a576026776 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr.c +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr.c @@ -35,7 +35,7 @@ int s5p_mfc_alloc_priv_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx, struct s5p_mfc_priv_buf *b) { unsigned int bits = dev->mem_size >> PAGE_SHIFT; - unsigned int count = b->size >> PAGE_SHIFT; + unsigned int count = DIV_ROUND_UP(b->size, PAGE_SIZE); unsigned int align = (SZ_64K >> PAGE_SHIFT) - 1; unsigned int start, offset; @@ -78,6 +78,13 @@ int s5p_mfc_alloc_generic_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx, { struct device *mem_dev = dev->mem_dev[mem_ctx]; + /* + * MFCv15 firmware rejects buffers below its base address, so the codec + * buffers come from the same region as the firmware. + */ + if (IS_MFCV15(dev)) + return s5p_mfc_alloc_priv_buf(dev, mem_ctx, b); + mfc_debug(3, "Allocating generic buf: %zu\n", b->size); b->ctx = mem_ctx; @@ -97,7 +104,7 @@ void s5p_mfc_release_priv_buf(struct s5p_mfc_dev *dev, { if (dev->mem_virt) { unsigned int start = (b->dma - dev->mem_base) >> PAGE_SHIFT; - unsigned int count = b->size >> PAGE_SHIFT; + unsigned int count = DIV_ROUND_UP(b->size, PAGE_SIZE); bitmap_clear(dev->mem_bitmap, start, count); } else { @@ -114,6 +121,12 @@ void s5p_mfc_release_generic_buf(struct s5p_mfc_dev *dev, struct s5p_mfc_priv_buf *b) { struct device *mem_dev = dev->mem_dev[b->ctx]; + + if (IS_MFCV15(dev)) { + if (b->virt) + s5p_mfc_release_priv_buf(dev, b); + return; + } dma_free_coherent(mem_dev, b->size, b->virt, b->dma); b->virt = NULL; b->dma = 0; diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr_v6.c b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr_v6.c index 98f8292b31..3f1e6e694f 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr_v6.c +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr_v6.c @@ -483,6 +483,12 @@ static int calc_plane(int width, int height) (mbY * S5P_FIMV_NUM_PIXELS_IN_MB_ROW_V6); } +/* Bytes of a one-buffer NV12 frame: both planes and the firmware's margin */ +size_t s5p_mfc_dec_nv12_size(const struct s5p_mfc_ctx *ctx) +{ + return ctx->luma_size + ctx->chroma_size + S5P_FIMV_D_ALIGN_PLANE_SIZE_V8; +} + static void s5p_mfc_dec_calc_dpb_size_v6(struct s5p_mfc_ctx *ctx) { struct s5p_mfc_dev *dev = ctx->dev; @@ -494,6 +500,18 @@ static void s5p_mfc_dec_calc_dpb_size_v6(struct s5p_mfc_ctx *ctx) ctx->img_height, ctx->buf_width, ctx->buf_height); switch (ctx->dst_fmt->fourcc) { + case V4L2_PIX_FMT_NV12: + /* + * One buffer, laid out as V4L2 NV12: the chroma starts right + * after the luma's bytesperline * height, so no padding goes + * between the planes (as Google's NV12N: 64 byte stride, + * height aligned to 16). + */ + ctx->stride[0] = ALIGN(ctx->img_width, 64); + ctx->stride[1] = ctx->stride[0]; + ctx->luma_size = ctx->stride[0] * ctx->buf_height; + ctx->chroma_size = ctx->stride[1] * ctx->buf_height / 2; + break; case V4L2_PIX_FMT_NV12M: case V4L2_PIX_FMT_NV21M: ctx->stride[0] = ALIGN(ctx->img_width, S5P_FIMV_NV12MT_HALIGN_V6); @@ -512,7 +530,7 @@ static void s5p_mfc_dec_calc_dpb_size_v6(struct s5p_mfc_ctx *ctx) break; } - if (IS_MFCV8_PLUS(ctx->dev)) { + if (IS_MFCV8_PLUS(ctx->dev) && ctx->dst_fmt->num_planes > 1) { /* MFCv8 needs additional 64 bytes for luma,chroma dpb*/ ctx->luma_size += S5P_FIMV_D_ALIGN_PLANE_SIZE_V8; ctx->chroma_size += S5P_FIMV_D_ALIGN_PLANE_SIZE_V8; @@ -603,6 +621,22 @@ static int s5p_mfc_set_dec_stream_buffer_v6(struct s5p_mfc_ctx *ctx, return 0; } +/* + * DPB plane sizes. For NV12 in one buffer the firmware still wants its 64 + * bytes after each plane: the chroma starts right after the luma, as V4L2 + * NV12 has it, so the luma's margin is the chroma's first bytes, and the + * buffer ends with the chroma's margin. + */ +static void s5p_mfc_set_dec_dpb_size(struct s5p_mfc_ctx *ctx) +{ + const struct s5p_mfc_regs *mfc_regs = ctx->dev->mfc_regs; + unsigned int pad = ctx->dst_fmt->num_planes == 1 ? + S5P_FIMV_D_ALIGN_PLANE_SIZE_V8 : 0; + + writel(ctx->luma_size + pad, mfc_regs->d_first_plane_dpb_size); + writel(ctx->chroma_size + pad, mfc_regs->d_second_plane_dpb_size); +} + /* Set decoding frame buffer */ static int s5p_mfc_set_dec_frame_buffer_v6(struct s5p_mfc_ctx *ctx) { @@ -622,8 +656,7 @@ static int s5p_mfc_set_dec_frame_buffer_v6(struct s5p_mfc_ctx *ctx) mfc_debug(2, "Setting display delay to %d\n", ctx->display_delay); writel(ctx->total_dpb_count, mfc_regs->d_num_dpb); - writel(ctx->luma_size, mfc_regs->d_first_plane_dpb_size); - writel(ctx->chroma_size, mfc_regs->d_second_plane_dpb_size); + s5p_mfc_set_dec_dpb_size(ctx); if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YUV420M || ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YVU420M) writel(ctx->chroma_size_1, mfc_regs->d_third_plane_dpb_size); @@ -654,7 +687,18 @@ static int s5p_mfc_set_dec_frame_buffer_v6(struct s5p_mfc_ctx *ctx) mfc_debug(2, "Frame size: %d ch: %d mv: %d\n", frame_size, frame_size_ch, frame_size_mv); - for (i = 0; i < ctx->total_dpb_count; i++) { + /* + * Dynamic DPB: the DPB addresses are given one per frame at + * FRAME_START (s5p_mfc_set_dynamic_dpb_v15()), not here. + */ + if (dev->dynamic_dpb) { + writel(readl(mfc_regs->d_init_buffer_options) | + S5P_FIMV_D_INIT_BUF_OPT_DYNAMIC_DPB_V15, + mfc_regs->d_init_buffer_options); + ctx->dpb_used = 0; + } + + for (i = 0; !dev->dynamic_dpb && i < ctx->total_dpb_count; i++) { /* Bank2 */ mfc_debug(2, "Luma %d: %zx\n", i, ctx->dst_bufs[i].cookie.raw.luma); @@ -1826,6 +1870,72 @@ static inline void s5p_mfc_set_flush(struct s5p_mfc_ctx *ctx, int flush) } } +/* + * Dynamic DPB (MFCv15): pick a capture buffer queued in the driver that the + * firmware does not hold any more, as the vendor driver does + * (mfc_search_for_dpb()). If the firmware holds all of them it releases one + * by displaying it, so any queued buffer may be given then. + */ +static struct s5p_mfc_buf *s5p_mfc_search_dynamic_dpb_v15(struct s5p_mfc_ctx *ctx) +{ + struct s5p_mfc_buf *buf; + + list_for_each_entry(buf, &ctx->dst_queue, list) + if (!(ctx->dpb_used & BIT(buf->b->vb2_buf.index))) + return buf; + + if (!list_empty(&ctx->dst_queue) && + hweight_long(ctx->dpb_used) >= ctx->total_dpb_count) + return list_first_entry(&ctx->dst_queue, struct s5p_mfc_buf, + list); + return NULL; +} + +static void s5p_mfc_set_dynamic_dpb_v15(struct s5p_mfc_ctx *ctx) +{ + struct s5p_mfc_dev *dev = ctx->dev; + const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs; + struct s5p_mfc_buf *buf; + u32 set = 0; + int i; + + /* + * The plane sizes and strides set at INIT_BUFFERS don't always last + * until the next frame (under Chromium they read back as 0 at the + * second one), and the firmware then fails the frame with + * INSUFFICIENT_DPB_SIZE. Set them again for every frame. + */ + s5p_mfc_set_dec_dpb_size(ctx); + writel(ctx->stride[0], mfc_regs->d_first_plane_dpb_stride_size); + writel(ctx->stride[1], mfc_regs->d_second_plane_dpb_stride_size); + if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YUV420M || + ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YVU420M) { + writel(ctx->chroma_size_1, mfc_regs->d_third_plane_dpb_size); + writel(ctx->stride[2], mfc_regs->d_third_plane_dpb_stride_size); + } + + buf = s5p_mfc_search_dynamic_dpb_v15(ctx); + if (buf) { + i = buf->b->vb2_buf.index; + set = BIT(i); + writel(buf->cookie.raw.luma, mfc_regs->d_first_plane_dpb + i * 4); + writel(buf->cookie.raw.chroma, + mfc_regs->d_second_plane_dpb + i * 4); + if (ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YUV420M || + ctx->dst_fmt->fourcc == V4L2_PIX_FMT_YVU420M) + writel(buf->cookie.raw.chroma_1, + mfc_regs->d_third_plane_dpb + i * 4); + mfc_debug(2, "Dynamic DPB %d: %zx (used %#lx)\n", i, + buf->cookie.raw.luma, ctx->dpb_used); + } + + writel(0, mfc_regs->d_nal_start_options); + writel(set, mfc_regs->d_dynamic_dpb_flag_lower); + writel(0, mfc_regs->d_dynamic_dpb_flag_upper); + writel(set, mfc_regs->d_available_dpb_flag_lower); + writel(0, mfc_regs->d_available_dpb_flag_upper); +} + /* Decode a single frame */ static int s5p_mfc_decode_one_frame_v6(struct s5p_mfc_ctx *ctx, enum s5p_mfc_decode_arg last_frame) @@ -1833,7 +1943,10 @@ static int s5p_mfc_decode_one_frame_v6(struct s5p_mfc_ctx *ctx, struct s5p_mfc_dev *dev = ctx->dev; const struct s5p_mfc_regs *mfc_regs = dev->mfc_regs; - writel(ctx->dec_dst_flag, mfc_regs->d_available_dpb_flag_lower); + if (dev->dynamic_dpb) + s5p_mfc_set_dynamic_dpb_v15(ctx); + else + writel(ctx->dec_dst_flag, mfc_regs->d_available_dpb_flag_lower); writel(ctx->slice_interface & 0x1, mfc_regs->d_slice_if_enable); writel(ctx->inst_no, mfc_regs->instance_id); @@ -1969,6 +2082,10 @@ static inline int s5p_mfc_run_dec_frame(struct s5p_mfc_ctx *ctx) mfc_debug(2, "No src buffers.\n"); return -EAGAIN; } + if (dev->dynamic_dpb && !s5p_mfc_search_dynamic_dpb_v15(ctx)) { + mfc_debug(2, "No free DPB (used %#lx).\n", ctx->dpb_used); + return -EAGAIN; + } /* Get the next source buffer */ temp_vb = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list); temp_vb->flags |= MFC_BUF_FLAG_USED; @@ -2656,6 +2773,17 @@ const struct s5p_mfc_regs *s5p_mfc_init_regs_v6_plus(struct s5p_mfc_dev *dev) R(e_hevc_lf_tc_offset_div2, S5P_FIMV_E_HEVC_LF_TC_OFFSET_DIV2_V10); R(e_hevc_nal_control, S5P_FIMV_E_HEVC_NAL_CONTROL_V10); + if (!IS_MFCV15(dev)) + goto done; + + /* Dynamic DPB registers of MFC v15 */ + R(d_nal_start_options, S5P_FIMV_D_NAL_START_OPTIONS_V15); + R(d_available_dpb_flag_upper, S5P_FIMV_D_AVAILABLE_DPB_FLAG_UPPER_V15); + R(d_dynamic_dpb_flag_upper, S5P_FIMV_D_DYNAMIC_DPB_FLAG_UPPER_V15); + R(d_dynamic_dpb_flag_lower, S5P_FIMV_D_DYNAMIC_DPB_FLAG_LOWER_V15); + R(d_used_dpb_flag_upper, S5P_FIMV_D_USED_DPB_FLAG_UPPER_V15); + R(d_used_dpb_flag_lower, S5P_FIMV_D_USED_DPB_FLAG_LOWER_V15); + done: return &mfc_regs; #undef S5P_MFC_REG_ADDR diff --git a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_pm.c b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_pm.c index ae42414083..47107b16c2 100644 --- a/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_pm.c +++ b/drivers/media/platform/samsung/s5p-mfc/s5p_mfc_pm.c @@ -26,7 +26,12 @@ int s5p_mfc_init_pm(struct s5p_mfc_dev *dev) /* clock control */ for (i = 0; i < pm->num_clocks; i++) { - pm->clocks[i] = devm_clk_get(pm->device, pm->clk_names[i]); + if (dev->variant->clocks_optional) + pm->clocks[i] = devm_clk_get_optional(pm->device, + pm->clk_names[i]); + else + pm->clocks[i] = devm_clk_get(pm->device, + pm->clk_names[i]); if (IS_ERR(pm->clocks[i])) { /* additional clocks are optional */ if (i && PTR_ERR(pm->clocks[i]) == -ENOENT) { @@ -79,6 +84,20 @@ int s5p_mfc_power_on(struct s5p_mfc_dev *dev) } } + /* + * Run at the variant's rate while the MFC is in use. The bootloader + * leaves the GS201 MFC at 100 MHz, a quarter of what 4K needs. + */ + if (dev->variant->active_clk_rate && dev->pm.clocks[0]) { + dev->pm.idle_clk_rate = clk_get_rate(dev->pm.clocks[0]); + ret = clk_set_rate(dev->pm.clocks[0], + dev->variant->active_clk_rate); + if (ret) + mfc_err("Failed to set the MFC clock to %lu Hz: %d\n", + dev->variant->active_clk_rate, ret); + ret = 0; + } + /* prepare for software clock gating */ clk_disable(dev->pm.clock_gate); @@ -97,6 +116,11 @@ int s5p_mfc_power_off(struct s5p_mfc_dev *dev) /* finish software clock gating */ clk_enable(dev->pm.clock_gate); + if (dev->pm.idle_clk_rate) { + clk_set_rate(dev->pm.clocks[0], dev->pm.idle_clk_rate); + dev->pm.idle_clk_rate = 0; + } + for (i = 0; i < dev->pm.num_clocks; i++) clk_disable_unprepare(dev->pm.clocks[i]);