diff --git a/drivers/media/platform/apple/avd/avd-av1.c b/drivers/media/platform/apple/avd/avd-av1.c index 1d8c6d2f2b798a..f129b7d3e4bef5 100644 --- a/drivers/media/platform/apple/avd/avd-av1.c +++ b/drivers/media/platform/apple/avd/avd-av1.c @@ -16,10 +16,6 @@ * Tomasz Figa */ -#include "linux/v4l2-controls.h" -#include -#include - #include #include "avd.h" @@ -60,10 +56,14 @@ }) #define AV1_REF_SCALE_SHIFT 14 +#define SUPERRES_SCALE_BITS 14 +#define SUPERRES_EXTRA_BITS 8 +#define SUPERRES_SCALE_MASK ((1 << 14) - 1) #define AV1_CODEC_MODE_INTRABC(v) FIELD_PREP(BIT(28), !!(v)) #define AV1_HDR_JNT_COMP(v) FIELD_PREP(BIT(0), !!(v)) +#define AV1_HDR_ORDER_HINT_BITS(v) FIELD_PREP(GENMASK(3, 1), v) #define AV1_HDR_ORDER_HINT(v) FIELD_PREP(BIT(4), !!(v)) #define AV1_HDR_DUAL_FILTER(v) FIELD_PREP(BIT(5), !!(v)) #define AV1_HDR_MASKED_COMPOUND(v) FIELD_PREP(BIT(6), !!(v)) @@ -124,6 +124,12 @@ #define AV1_QP_BASE_IDX(v) FIELD_PREP(GENMASK(28, 21), v) #define AV1_QP_DELTA_RES(v) FIELD_PREP(GENMASK(30, 29), v) #define AV1_QP_PRESENT(v) FIELD_PREP(BIT(31), !!(v)) +#define AV1_QP_V_DC(v) FIELD_PREP(GENMASK(26, 20), v) +#define AV1_QP_V_AC(v) FIELD_PREP(GENMASK(19, 13), v) +#define AV1_QP_QMATRIX(v) FIELD_PREP(BIT(12), !!(v)) +#define AV1_QP_QM_Y(v) FIELD_PREP(GENMASK(11, 8), v) +#define AV1_QP_QM_U(v) FIELD_PREP(GENMASK(7, 4), v) +#define AV1_QP_QM_V(v) FIELD_PREP(GENMASK(3, 0), v) #define AV1_GM_VALID(v) FIELD_PREP(BIT(30), !!(v)) #define AV1_GM_TYPE(v) FIELD_PREP(GENMASK(31, 30), v) @@ -154,7 +160,6 @@ #define AV1_LF_REF2(v) FIELD_PREP(GENMASK(13, 7), v) #define AV1_LF_REF3(v) FIELD_PREP(GENMASK(6, 0), v) -#define AV1_LF_LV(v) FIELD_PREP(GENMASK(31, 14), v) #define AV1_LF_MODE0(v) FIELD_PREP(GENMASK(13, 7), v) #define AV1_LF_MODE1(v) FIELD_PREP(GENMASK(6, 0), v) @@ -174,27 +179,35 @@ #define AV1_LR_TYPE1(v) FIELD_PREP(GENMASK(9, 8), v) #define AV1_LR_TYPE2(v) FIELD_PREP(GENMASK(7, 6), v) -#define AV1_LR_UNIT0(v) FIELD_PREP(GENMASK(1, 0), v) +#define AV1_LR_UNIT2(v) FIELD_PREP(GENMASK(1, 0), v) #define AV1_LR_UNIT1(v) FIELD_PREP(GENMASK(3, 2), v) -#define AV1_LR_UNIT2(v) FIELD_PREP(GENMASK(5, 4), v) +#define AV1_LR_UNIT0(v) FIELD_PREP(GENMASK(5, 4), v) #define AV1_REF_ORDER_HINT(v) FIELD_PREP(GENMASK(23, 0), v) #define AV1_REF_SCALE_X(v) FIELD_PREP(GENMASK(31, 16), v) #define AV1_REF_SCALE_Y(v) FIELD_PREP(GENMASK(15, 0), v) -#define AVD_CDFS_SIZE (sizeof(struct avd_av1_cdfs)) +#define AV1_SUPERRES_USE(v) FIELD_PREP(BIT(31), !!(v)) +#define AV1_SUPERRES_DENOM(v) FIELD_PREP(GENMASK(30, 28), v) +/* im gonna asume 16 like the others */ +#define AV1_SUPERRES_UPSCALE(v) FIELD_PREP(GENMASK(16, 0), v) + +#define AV1_UPSCALE_STEPX(v) FIELD_PREP(GENMASK(13, 0), v) +#define AV1_UPSCALE_UPSCX(v) FIELD_PREP(GENMASK(31, 14), v) -#define AVD_AV1_TLB_OFFSET(dst, tlb) \ - ((dst) - ALIGN(tlb, AVD_ALIGN)) -#define AVD_AV1_CDFS_OFFSET(dst, tlb) \ - (AVD_AV1_TLB_OFFSET(dst, tlb) - ALIGN(AVD_CDFS_SIZE, AVD_ALIGN)) +#define AVD_CDFS_SIZE sizeof(struct avd_av1_cdfs) + +#define AVD_AV1_COLOR_OFFSET(dst, cl) \ + ((dst) - ALIGN(cl, AVD_ALIGN)) +#define AVD_AV1_CDFS_OFFSET(dst, cl) \ + (AVD_AV1_COLOR_OFFSET(dst, cl) - ALIGN(AVD_CDFS_SIZE, AVD_ALIGN)) struct avd_av1_run { struct avd_run base; struct { dma_addr_t probs_out; - dma_addr_t priv_tlb; + dma_addr_t color; } addresses; const struct v4l2_ctrl_av1_sequence *seq; @@ -208,14 +221,21 @@ struct avd_av1_ctx { struct { struct avd_buf inst; struct avd_buf pipe_state; - struct avd_buf unk[2]; struct avd_buf probs; + struct avd_buf seg; + struct avd_buf above_info; + struct avd_buf rf_above_info; + struct avd_buf az_above; + struct avd_buf ip_above; + struct avd_buf lf_above; + struct avd_buf lf_above_info; + struct avd_buf lf_left; + struct avd_buf lf_left_info; + struct avd_buf sr_left; + struct avd_buf rf_left; + struct avd_buf rf_left_info; + struct avd_buf mv_above_info; } bufs; - struct { - struct avd_buf tile_col[4]; - struct avd_buf tile_row[4]; - struct avd_buf ref; - } scratch; }; /* @@ -316,7 +336,6 @@ static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run) struct avd_av1_ctx *av1_ctx = ctx->priv; const struct v4l2_ctrl_av1_frame *frame = run->frame; const struct v4l2_av1_global_motion *gm = &frame->global_motion; - struct avd_dev *avd = ctx->dev; int i, ref_idx; struct avd_decoded_buffer *dst, *ref; bool intrabc = !!(frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC); @@ -328,7 +347,7 @@ static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run) dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); push(0, "ref_cnst0"); - pusha(av1_ctx->scratch.ref.addr, "unk_ref_buf", 0); + pusha(av1_ctx->bufs.mv_above_info.addr, "mv_above_info", 0); for (i = 0; i < 4; i++) push(0, "ref_cnst1"); @@ -350,15 +369,17 @@ static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run) int shift = (gm->flags[ref_idx] & V4L2_AV1_GLOBAL_MOTION_FLAG_IS_TRANSLATION) ? - WARPEDMODEL_PREC_BITS - 3 : - 10 /* why? */; + (WARPEDMODEL_PREC_BITS - + 2) - (frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_HIGH_PRECISION_MV ? + 1 : + 0) : + 10; push(AV1_GM_TYPE(gm->type[ref_idx]) | AV1_GM_PARAM0(gm->params[ref_idx][0] >> shift) | AV1_GM_PARAM1(gm->params[ref_idx][1] >> shift), "ref_gm_mv"); - /* TODO: precison or something, does not always fit */ push(AV1_GM_VALID(!(V4L2_AV1_GLOBAL_MOTION_IS_INVALID( ref_idx) & gm->invalid)) | @@ -367,12 +388,8 @@ static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run) AV1_GM_PARAM1(AV1_DIV_ROUND_UP_POW2_SIGNED( gm->params[ref_idx][3], 1)), "ref_gm_param"); - /* TODO: this does not quite fit */ - push(AV1_GM_PARAM1(gm->params[ref_idx][5] / 2) | - AV1_GM_PARAM0(AV1_DIV_ROUND_UP_POW2_SIGNED( - gm->params[ref_idx][4] - - gm->params[ref_idx][3], - 2)), + push(AV1_GM_PARAM1(gm->params[ref_idx][5] >> 1) | + AV1_GM_PARAM0(gm->params[ref_idx][4] >> 1), "ref_gm_unk2"); avd_av1_dec_get_shear_params(&gm->params[ref_idx][0], &alpha, &beta, &gamma, @@ -402,7 +419,7 @@ static void set_refs(struct avd_ctx *ctx, struct avd_av1_run *run) push(AV1_REF_SCALE_X(x_scale) | AV1_REF_SCALE_Y(y_scale), "ref_scale"); - push_comp(avd, ctx, addr, ref->comp.offsets); + push_comp(ctx, addr, ref->comp.offsets); } } @@ -516,7 +533,6 @@ static void set_ref_hints(struct avd_ctx *ctx, struct avd_av1_run *run, u8 *selected_refs) { const struct v4l2_ctrl_av1_frame *frame = run->frame; - struct avd_dev *avd = ctx->dev; int i, ref_idx; struct avd_decoded_buffer *dst, *ref; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); @@ -554,7 +570,6 @@ static void set_ref_hints(struct avd_ctx *ctx, struct avd_av1_run *run, static void set_ref_hdr(struct avd_ctx *ctx, struct avd_av1_run *run) { const struct v4l2_ctrl_av1_frame *frame = run->frame; - struct avd_dev *avd = ctx->dev; u8 ref_slots[7] = { 1, 2, 3, 4, 5, 6, 7 }; int sign_bias = 0; int ref_slot = 0; @@ -588,6 +603,27 @@ static void set_ref_hdr(struct avd_ctx *ctx, struct avd_av1_run *run) "reference_select"); } +/* 7.16. Upscaling process */ +static void calc_upscale(int frame_width, int upscaled_width, int sub_x, + int *step_x, int *initial_subpel_x) +{ + int downscaled_plane_w, upscaled_plane_w, err; + + downscaled_plane_w = AV1_DIV_ROUND_UP_POW2(frame_width, sub_x); + upscaled_plane_w = AV1_DIV_ROUND_UP_POW2(upscaled_width, sub_x); + *step_x = ((downscaled_plane_w << SUPERRES_SCALE_BITS) + + (upscaled_plane_w / 2)) / + upscaled_plane_w; + err = (upscaled_plane_w * *step_x) - + (downscaled_plane_w << SUPERRES_SCALE_BITS); + *initial_subpel_x = (-((upscaled_plane_w - downscaled_plane_w) + << (SUPERRES_SCALE_BITS - 1)) + + upscaled_plane_w / 2) / + upscaled_plane_w + + (1 << (SUPERRES_EXTRA_BITS - 1)) - err / 2; + *initial_subpel_x &= SUPERRES_SCALE_MASK; +} + static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) { struct avd_av1_ctx *av1_ctx = ctx->priv; @@ -596,9 +632,9 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) const struct v4l2_av1_loop_restoration *lr = &frame->loop_restoration; const struct v4l2_av1_cdef *cdef = &frame->cdef; const struct v4l2_av1_segmentation *seg = &frame->segmentation; - struct avd_dev *avd = ctx->dev; u32 bytesperline; int i, segid, segval, ref_idx; + int step_x, initial_subpel_x; u8 selected_refs[3] = { 0, 0, 0 }; bool coded_lossless = frame->tx_mode == V4L2_AV1_TX_MODE_ONLY_4X4; bool intrabc = !!(frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC); @@ -613,10 +649,6 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) dma_addr_t ref_addr; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); - push(AVD_OP_EXEC | - AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == 4) | - AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx), - "vp_start"); push(AVD_OP_HDR | AVD_OP_HDR_FLAG_DECOMP(ctx->decomp) | AVD_OP_HDR_FLAG_INTRA(intra_only && !intrabc) | AVD_OP_HDR_CONST | AVD_OP_HDR_FLAG_PIPE_STATE(1), @@ -646,6 +678,7 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) AV1_HDR_DUAL_FILTER( seq->flags & V4L2_AV1_SEQUENCE_FLAG_ENABLE_DUAL_FILTER) | + AV1_HDR_ORDER_HINT_BITS((seq->order_hint_bits - 1)) | AV1_HDR_ORDER_HINT( seq->flags & V4L2_AV1_SEQUENCE_FLAG_ENABLE_ORDER_HINT) | @@ -666,17 +699,7 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK) | AV1_HDR_SCREEN_CONTENT_TOOLS( frame->flags & - V4L2_AV1_FRAME_FLAG_ALLOW_SCREEN_CONTENT_TOOLS) | - - /* TODO: this seems like a coincidence */ - !!!(seq->flags & V4L2_AV1_SEQUENCE_FLAG_ENABLE_ORDER_HINT) - << 1 | - !!(seq->flags & - V4L2_AV1_SEQUENCE_FLAG_ENABLE_WARPED_MOTION) - << 2 | - !!(seq->flags & - V4L2_AV1_SEQUENCE_FLAG_ENABLE_MASKED_COMPOUND) - << 3, + V4L2_AV1_FRAME_FLAG_ALLOW_SCREEN_CONTENT_TOOLS), "hdr_common"); push(AV1_FLAGS_TX_MODE_LARGEST(frame->tx_mode == @@ -714,7 +737,6 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) AV1_FLAGS_SEG_TEMPORAL_UPDATE( frame->segmentation.flags & V4L2_AV1_SEGMENTATION_FLAG_TEMPORAL_UPDATE) | - /* is this false? its always true if buf0 is set */ AV1_FLAGS_SEG_UPDATE_MAP( frame->segmentation.flags & V4L2_AV1_SEGMENTATION_FLAG_UPDATE_MAP) | @@ -788,14 +810,11 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) pusha(run->addresses.probs_out, "probs_out", 0); pusha(av1_ctx->bufs.probs.addr, "probs", 1); - - pusha(av1_ctx->scratch.tile_col[0].addr, "col", 0); - - /* TODO */ - pusha((dma_addr_t)0, "", 0); - pusha((dma_addr_t)0, "", 1); - - pusha(run->addresses.priv_tlb, "cur_ref_addr", 0); + pusha(av1_ctx->bufs.above_info.addr, "col", 0); + pusha(av1_ctx->bufs.seg.addr, "seg", 0); + /* make it obvious if its used */ + pusha((dma_addr_t)0xb0b0b0b0b0b0, "", 1); + pusha(run->addresses.color, "w_color", 0); for (i = 0; i < 3; i++) { if (selected_refs[i] < V4L2_AV1_REF_LAST_FRAME) { @@ -809,11 +828,11 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) ref_addr = vb2_dma_contig_plane_dma_addr( &ref->base.vb.vb2_buf, 0) + - AVD_AV1_TLB_OFFSET( + AVD_AV1_COLOR_OFFSET( ref->base.vb.vb2_buf.planes[0].length, - ref->av1.priv_tlb_size); + ref->av1.color_size); - pusha(ref_addr, "ref", ref_idx); + pusha(ref_addr, "r_color", ref_idx); } } @@ -826,10 +845,15 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) AV1_QP_U_AC(frame->quantization.delta_q_u_ac), "qp_base_q_idx"); - /* TODO: test294 test35 */ - push(0, "unk"); + push(AV1_QP_V_DC(frame->quantization.delta_q_v_dc) | + AV1_QP_V_AC(frame->quantization.delta_q_v_ac) | + AV1_QP_QMATRIX(frame->quantization.flags & + V4L2_AV1_QUANTIZATION_FLAG_USING_QMATRIX) | + AV1_QP_QM_Y(frame->quantization.qm_y) | + AV1_QP_QM_U(frame->quantization.qm_u) | + AV1_QP_QM_V(frame->quantization.qm_v), + "qp_qm"); - /* TODO */ push(AV1_LF_DELTA_ENABLED(frame->loop_filter.flags & V4L2_AV1_LOOP_FILTER_FLAG_DELTA_ENABLED) | AV1_LF_DELTA_PRESENT( @@ -839,14 +863,14 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) frame->loop_filter.flags & V4L2_AV1_LOOP_FILTER_FLAG_DELTA_LF_MULTI) | AV1_LF_DELTA_RES(frame->loop_filter.delta_lf_res) | - + AV1_LF_SHARPNESS(frame->loop_filter.sharpness) | AV1_LF_LV0(frame->loop_filter.level[0]) | AV1_LF_LV1(frame->loop_filter.level[1]) | AV1_LF_LV2(frame->loop_filter.level[2]) | AV1_LF_LV3(frame->loop_filter.level[3]), "lf_update"); - push(AV1_LF_SHARPNESS(frame->loop_filter.sharpness) | - AV1_LF_REF0(frame->loop_filter.ref_deltas[0]) | + + push(AV1_LF_REF0(frame->loop_filter.ref_deltas[0]) | AV1_LF_REF1(frame->loop_filter.ref_deltas[1]) | AV1_LF_REF2(frame->loop_filter.ref_deltas[2]) | AV1_LF_REF3(frame->loop_filter.ref_deltas[3]), @@ -857,8 +881,10 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) AV1_LF_REF2(frame->loop_filter.ref_deltas[6]) | AV1_LF_REF3(frame->loop_filter.ref_deltas[7]), "lf_ref_deltas"); - /* TODO */ - push(0, "lf_unk"); + + push(AV1_LF_MODE0(frame->loop_filter.mode_deltas[0]) | + AV1_LF_MODE1(frame->loop_filter.mode_deltas[1]), + "lf_mode"); #define AVD_AV1_SEC_FIXUP(i) ((i) == 4 ? 3 : i) @@ -881,16 +907,23 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) #undef AV1_CDEF_PACK_ONE #undef AV1_CDEF_PACK - push((frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES << 31) | - /* TODO: this is wrong, has something to do with superres */ - (frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES ? - (frame->superres_denom - 1) << 28 : - 0) | - (frame->upscaled_width - 1), - "upscaled_width"); - /* something superres related? test35 */ - push(0x200000, "flag_unk0"); - push(0x200000, "flag_unk1"); + push(AV1_SUPERRES_USE(frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES) | + AV1_SUPERRES_DENOM( + frame->flags & V4L2_AV1_FRAME_FLAG_USE_SUPERRES ? + (frame->superres_denom - 1) : + 0) | + AV1_SUPERRES_UPSCALE(frame->upscaled_width - 1), + "superres"); + + calc_upscale(frame->frame_width_minus_1 + 1, frame->upscaled_width, 0, + &step_x, &initial_subpel_x); + push(AV1_UPSCALE_UPSCX(initial_subpel_x) | AV1_UPSCALE_STEPX(step_x), + "upscale_p0"); + calc_upscale(frame->frame_width_minus_1 + 1, frame->upscaled_width, + !!(seq->flags & V4L2_AV1_SEQUENCE_FLAG_SUBSAMPLING_X), + &step_x, &initial_subpel_x); + push(AV1_UPSCALE_UPSCX(initial_subpel_x) | AV1_UPSCALE_STEPX(step_x), + "upscale_p1"); u8 restoration_unit_size[V4L2_AV1_NUM_PLANES_MAX] = { 3, 3, 3 }; @@ -915,24 +948,23 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) push(0, ""); push(0, ""); pusha(av1_ctx->bufs.pipe_state.addr, "pipe_state", 0); - pusha(av1_ctx->scratch.tile_col[1].addr, "col", 1); - pusha(av1_ctx->scratch.tile_col[2].addr, "col", 2); - pusha(av1_ctx->scratch.tile_col[3].addr, "col", 3); - - pusha(av1_ctx->scratch.tile_row[0].addr, "row", 0); - pusha(av1_ctx->scratch.tile_row[1].addr, "row", 1); - pusha(0, "unk", 0); - pusha(av1_ctx->bufs.unk[0].addr, "unk", 0); - pusha(av1_ctx->scratch.tile_row[2].addr, "row", 2); - pusha(av1_ctx->bufs.unk[1].addr, "unk", 1); - pusha(av1_ctx->scratch.tile_row[3].addr, "row", 3); + pusha(av1_ctx->bufs.ip_above.addr, "ip_above", 0); + pusha(av1_ctx->bufs.lf_above.addr, "lf_above", 2); + pusha(av1_ctx->bufs.lf_above_info.addr, "lf_above_info", 3); + pusha(av1_ctx->bufs.lf_left.addr, "lf_left", 0); + pusha(av1_ctx->bufs.lf_left_info.addr, "lf_left_info", 1); + /* make it obvious if its used */ + pusha((dma_addr_t)0xa0a0a0a0a0a0, "", 0); + pusha(av1_ctx->bufs.rf_above_info.addr, "rf_above_info", 0); + pusha(av1_ctx->bufs.sr_left.addr, "sr_left", 2); + pusha(av1_ctx->bufs.rf_left_info.addr, "rf_left_info", 1); + pusha(av1_ctx->bufs.rf_left.addr, "rf_left", 3); push(0, "mark_section"); - push_comp(avd, ctx, run->base.comp_out, ctx->comp.offsets); + push_comp(ctx, run->base.comp_out, ctx->comp.offsets); - push(0, ""); - push(0, "mark_section"); + pusha((u64)0, "packed_fmt_scratch", 0); /* ignored if decomp is disabled */ bytesperline = ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; @@ -943,8 +975,8 @@ static void set_header(struct avd_ctx *ctx, struct avd_av1_run *run) push(0, "mark_section"); - push(AVD_HDR_HEIGHT(frame->frame_height_minus_1) | - AVD_HDR_WIDTH(frame->frame_width_minus_1), + push(AVD_HDR_HEIGHT(frame->render_height_minus_1 + 1) | + AVD_HDR_WIDTH(frame->render_width_minus_1 + 1), "height_width_3"); if (!intra_only || intrabc) set_refs(ctx, run); @@ -957,9 +989,7 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_av1_run *run) const struct v4l2_ctrl_av1_sequence *seq = run->seq; const struct v4l2_ctrl_av1_tile_group_entry *tile_group; const struct v4l2_av1_tile_info *tile_info = &frame->tile_info; - bool is_last; - struct avd_dev *avd = ctx->dev; - int row, col, sb_row, sb_col; + int row, col, sb_row, sb_col, tile_id; int sb_shift = seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 : 4; @@ -968,9 +998,8 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_av1_run *run) for (row = 0; row < tile_info->tile_rows; row++) { for (col = 0; col < tile_info->tile_cols; col++) { - is_last = col == tile_info->tile_cols - 1 && - row == tile_info->tile_rows - 1; - int tile_id = row * tile_info->tile_cols + col; + ctx->job.num++; + tile_id = row * tile_info->tile_cols + col; tile_group = &run->tile_group[tile_id]; push(AVD_OP_CODED_DATA | @@ -1013,8 +1042,6 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_av1_run *run) tile_info->width_in_sbs_minus_1 [tile_group->tile_col]), "tile_op_end"); - push(AVD_OP_EXEC | AVD_OP_EXEC_FLAG_END(is_last), - "submit"); #ifdef DEBUG_INST pr_info("\n"); #endif @@ -1048,7 +1075,7 @@ static void avd_av1_set_prob(struct avd_ctx *ctx, struct avd_av1_run *run) vb2_plane_vaddr(&ref->base.vb.vb2_buf, 0) + AVD_AV1_CDFS_OFFSET( ref->base.vb.vb2_buf.planes[0].length, - ref->av1.priv_tlb_size), + ref->av1.color_size), sizeof(struct avd_av1_cdfs)); } @@ -1058,11 +1085,11 @@ static void avd_av1_set_prob(struct avd_ctx *ctx, struct avd_av1_run *run) */ memcpy(vb2_plane_vaddr(&dst->base.vb.vb2_buf, 0) + AVD_AV1_CDFS_OFFSET(dst->base.vb.vb2_buf.planes[0].length, - dst->av1.priv_tlb_size), + dst->av1.color_size), av1_ctx->bufs.probs.cpu, sizeof(struct avd_av1_cdfs)); } -static int avd_priv_tlb_size(int h, int w) +static int avd_color_size(int h, int w) { /* * in reality its dependent on quality @@ -1083,9 +1110,8 @@ static void update_dec_buf_info(struct avd_decoded_buffer *buf, buf->av1.frame_type = frame->frame_type; buf->av1.intrabc = frame->flags & V4L2_AV1_FRAME_FLAG_ALLOW_INTRABC; - buf->av1.priv_tlb_size = - avd_priv_tlb_size(frame->frame_width_minus_1 + 1, - frame->frame_height_minus_1 + 1); + buf->av1.color_size = avd_color_size(frame->frame_width_minus_1 + 1, + frame->frame_height_minus_1 + 1); for (i = 0; i < V4L2_AV1_TOTAL_REFS_PER_FRAME; i++) buf->av1.order_hints[i] = frame->order_hints[i]; @@ -1124,67 +1150,81 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run) run->grain = ctrl->p_cur.p; dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; - tlb_len = avd_priv_tlb_size(run->frame->frame_width_minus_1 + 1, - run->frame->frame_height_minus_1 + 1); + tlb_len = avd_color_size(run->frame->frame_width_minus_1 + 1, + run->frame->frame_height_minus_1 + 1); - run->addresses.priv_tlb = - run->base.y_out + AVD_AV1_TLB_OFFSET(dst_len, tlb_len); + run->addresses.color = + run->base.y_out + AVD_AV1_COLOR_OFFSET(dst_len, tlb_len); run->addresses.probs_out = run->base.y_out + AVD_AV1_CDFS_OFFSET(dst_len, tlb_len); return 0; } -static int avd_av1_alloc_scratch(struct avd_ctx *ctx, struct avd_av1_run *run) +static int avd_av1_alloc_work_bufs(struct avd_ctx *ctx, struct avd_av1_run *run) { struct avd_dev *avd = ctx->dev; struct avd_av1_ctx *av1_ctx = ctx->priv; const struct v4l2_ctrl_av1_frame *frame = run->frame; const struct v4l2_ctrl_av1_sequence *seq = run->seq; const struct v4l2_av1_tile_info *tile_info = &frame->tile_info; - int ret, max_sb_col = 0, max_sb_row = 0, i, sb_cols = 0, sb; + int ret, max_sb_col = 0, max_sb_row = 0, i, sb_cols1 = 0, sb_cols2 = 0, + sb; int sb_shift = seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 1 : 0; int w = frame->frame_width_minus_1 + 1; + int h = frame->frame_height_minus_1 + 1; int bit_depth = seq->bit_depth; for (i = 0; i < tile_info->tile_cols; i++) { sb = tile_info->width_in_sbs_minus_1[i] + 1; max_sb_col = sb > max_sb_col ? sb : max_sb_col; - sb_cols += ALIGN((sb << sb_shift) * 44, 128); + sb_cols1 += ALIGN((sb << sb_shift) * 44, 128); + /* wrong alignment? */ + sb_cols2 += ALIGN((sb << sb_shift) * 12, 128); } max_sb_col <<= sb_shift; - ret = avd_buf_alloc(avd, &av1_ctx->scratch.ref, max_sb_col * 80); + ret = avd_buf_alloc(avd, &av1_ctx->bufs.mv_above_info, max_sb_col * 80); if (ret) return ret; /* first frame this is always much smaller */ - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_col[0], - (max_sb_col * 400)); + ret = avd_buf_alloc(avd, &av1_ctx->bufs.above_info, max_sb_col * 400); if (ret) return ret; - /* first frame this is always 0 */ - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_col[3], sb_cols); + ret = avd_buf_alloc(avd, &av1_ctx->bufs.lf_above_info, sb_cols1); if (ret) return ret; - /* i think this is the metadata buffer to the one below */ - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_col[1], - (max_sb_col * bit_depth * 22)); + ret = avd_buf_alloc(avd, &av1_ctx->bufs.ip_above, + max_sb_col * bit_depth * 22); if (ret) return ret; - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_col[2], + ret = avd_buf_alloc(avd, &av1_ctx->bufs.lf_above, ALIGN(ALIGN(w, 8) * bit_depth * 2 + tile_info->tile_cols * bit_depth * 16, 128)); if (ret) return ret; + ret = avd_buf_alloc(avd, &av1_ctx->bufs.rf_above_info, sb_cols2); + if (ret) + return ret; + + if (frame->segmentation.flags & V4L2_AV1_SEGMENTATION_FLAG_UPDATE_MAP) { + ret = avd_buf_alloc( + avd, &av1_ctx->bufs.seg, + (ALIGN(h, 64 << sb_shift) * ALIGN(w, 64 << sb_shift)) / + 128 * 3); + if (ret) + return ret; + } + /* this is not a mistake, but im not sure why */ if (tile_info->tile_cols > 1) { for (i = 0; i < tile_info->tile_rows; i++) { @@ -1194,12 +1234,8 @@ static int avd_av1_alloc_scratch(struct avd_ctx *ctx, struct avd_av1_run *run) max_sb_row = max_sb_row << sb_shift; - /* - * metadata buffer maybe? At least for row[0], they all seem to have - * some kinda alignment to 24 so could be for them all - */ - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_row[1], - (max_sb_row * 72)); + ret = avd_buf_alloc(avd, &av1_ctx->bufs.lf_left_info, + max_sb_row * 72); if (ret) return ret; @@ -1213,22 +1249,27 @@ static int avd_av1_alloc_scratch(struct avd_ctx *ctx, struct avd_av1_run *run) * I counted blocks as luma or chroma with padding until it * changes to the other */ - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_row[0], + ret = avd_buf_alloc(avd, &av1_ctx->bufs.lf_left, (max_sb_row * 72 + 1) * 2 * bit_depth); if (ret) return ret; - ret = avd_buf_alloc(avd, &av1_ctx->scratch.tile_row[2], + ret = avd_buf_alloc(avd, &av1_ctx->bufs.sr_left, max_sb_row * bit_depth * (53 + 52)); if (ret) return ret; ret = avd_buf_alloc( - avd, &av1_ctx->scratch.tile_row[3], + avd, &av1_ctx->bufs.rf_left, max_sb_row * bit_depth * (192 + 96 + (bit_depth > 8 ? 12 : 0))); if (ret) return ret; + + ret = avd_buf_alloc(avd, &av1_ctx->bufs.rf_left_info, + max_sb_row * 24); + if (ret) + return ret; } return 0; @@ -1236,9 +1277,7 @@ static int avd_av1_alloc_scratch(struct avd_ctx *ctx, struct avd_av1_run *run) static int avd_av1_run(struct avd_ctx *ctx) { - struct avd_dev *avd = ctx->dev; struct avd_av1_run run; - struct avd_av1_ctx *av1_ctx; struct avd_decoded_buffer *dst; int ret; @@ -1248,46 +1287,32 @@ static int avd_av1_run(struct avd_ctx *ctx) return ret; } - av1_ctx = ctx->priv; + ret = avd_init_job(ctx, AVD_CODEC_AV1, + run.frame->tile_info.tile_cols * + run.frame->tile_info.tile_rows + + 1); + if (ret) { + avd_run_postamble(ctx, &run.base); + return ret; + } - ret = alloc_slots(avd, ctx, AVD_CODEC_AV1); + ret = avd_av1_alloc_work_bufs(ctx, &run); if (ret) { - dev_err(avd->dev, "no free slots: %d", ret); + avd_run_postamble(ctx, &run.base); return ret; } dst = vb2_to_avd_decoded_buf(&run.base.bufs.dst->vb2_buf); update_dec_buf_info(dst, run.seq, run.frame); - schedule_delayed_work(&ctx->watchdog_work, msecs_to_jiffies(2000)); - - avd->variant->configure_stream(avd, av1_ctx->bufs.inst.addr, - ctx->fifo_idx, ctx->vp_slot); - avd_av1_set_prob(ctx, &run); - avd_av1_alloc_scratch(ctx, &run); set_header(ctx, &run); set_tiles(ctx, &run); avd_run_postamble(ctx, &run.base); - return 0; -} - -static void avd_av1_dealloc_scratch(struct avd_ctx *ctx) -{ - struct avd_dev *avd = ctx->dev; - struct avd_av1_ctx *av1_ctx = ctx->priv; - int i; - - for (i = 0; i < 4; i++) - avd_buf_free(avd, &av1_ctx->scratch.tile_col[i]); - - for (i = 0; i < 4; i++) - avd_buf_free(avd, &av1_ctx->scratch.tile_row[i]); - - avd_buf_free(avd, &av1_ctx->scratch.ref); + return avd_submit_job(ctx); } static int avd_av1_alloc_bufs(struct avd_ctx *ctx) @@ -1310,15 +1335,34 @@ static int avd_av1_alloc_bufs(struct avd_ctx *ctx) if (ret) return ret; - ret = avd_buf_alloc(avd, &av1_ctx->bufs.unk[0], 1024); - if (ret) - return ret; + return 0; +} - ret = avd_buf_alloc(avd, &av1_ctx->bufs.unk[1], 1024); - if (ret) - return ret; +static void avd_av1_stop(struct avd_ctx *ctx) +{ + struct avd_av1_ctx *av1_ctx = ctx->priv; + struct avd_dev *avd = ctx->dev; - return 0; + avd_buf_free(avd, &av1_ctx->bufs.above_info); + avd_buf_free(avd, &av1_ctx->bufs.rf_above_info); + avd_buf_free(avd, &av1_ctx->bufs.az_above); + avd_buf_free(avd, &av1_ctx->bufs.ip_above); + avd_buf_free(avd, &av1_ctx->bufs.lf_above); + avd_buf_free(avd, &av1_ctx->bufs.lf_above_info); + avd_buf_free(avd, &av1_ctx->bufs.lf_left); + avd_buf_free(avd, &av1_ctx->bufs.lf_left_info); + avd_buf_free(avd, &av1_ctx->bufs.sr_left); + avd_buf_free(avd, &av1_ctx->bufs.rf_left); + avd_buf_free(avd, &av1_ctx->bufs.rf_left_info); + avd_buf_free(avd, &av1_ctx->bufs.seg); + avd_buf_free(avd, &av1_ctx->bufs.mv_above_info); + + avd_buf_free(avd, &av1_ctx->bufs.pipe_state); + avd_buf_free(avd, &av1_ctx->bufs.inst); + avd_buf_free(avd, &av1_ctx->bufs.probs); + + kfree(av1_ctx); + ctx->priv = NULL; } static int avd_av1_start(struct avd_ctx *ctx) @@ -1338,28 +1382,10 @@ static int avd_av1_start(struct avd_ctx *ctx) return 0; err_free_ctx: - kfree(av1_ctx); - ctx->priv = NULL; + avd_av1_stop(ctx); return ret; } -static void avd_av1_stop(struct avd_ctx *ctx) -{ - struct avd_av1_ctx *av1_ctx = ctx->priv; - struct avd_dev *avd = ctx->dev; - - avd_av1_dealloc_scratch(ctx); - - avd_buf_free(avd, &av1_ctx->bufs.pipe_state); - avd_buf_free(avd, &av1_ctx->bufs.inst); - avd_buf_free(avd, &av1_ctx->bufs.probs); - - for (int i = 0; i < 2; i++) - avd_buf_free(avd, &av1_ctx->bufs.unk[i]); - - kfree(av1_ctx); -} - static enum avd_image_fmt avd_av1_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { @@ -1394,37 +1420,19 @@ static void avd_av1_submit(struct avd_ctx *ctx) AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), avd->ctrl + avd->variant->submit_offset); -#ifdef DEBUG_INST - pr_info("%8lx | %s %2d\n", - AVD_OP_EXEC | - AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == - 4) | - AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | - AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), - "submit", 0); -#endif for (int i = 0; i < av1_ctx->submit_num - 1; i++) { -#ifdef DEBUG_INST - pr_info("%8lx | %s %2d\n", - AVD_OP_EXEC | AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | - AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), - "submit", i + 1); -#endif writel(AVD_OP_EXEC | AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), avd->ctrl + avd->variant->submit_offset); } -#ifdef DEBUG_INST - pr_info("\n"); -#endif } static void avd_av1_adjust_decoded_fmt(struct avd_ctx *ctx, struct v4l2_pix_format_mplane *pix_mp) { pix_mp->plane_fmt[0].sizeimage += ALIGN(AVD_CDFS_SIZE, AVD_ALIGN); - pix_mp->plane_fmt[0].sizeimage += ALIGN( - avd_priv_tlb_size(pix_mp->width, pix_mp->height), AVD_ALIGN); + pix_mp->plane_fmt[0].sizeimage += + ALIGN(avd_color_size(pix_mp->width, pix_mp->height), AVD_ALIGN); } static int avd_av1_validate_sequence(struct avd_ctx *ctx, diff --git a/drivers/media/platform/apple/avd/avd-drv.c b/drivers/media/platform/apple/avd/avd-drv.c index 487d478b00f397..e5ad2cd249a293 100644 --- a/drivers/media/platform/apple/avd/avd-drv.c +++ b/drivers/media/platform/apple/avd/avd-drv.c @@ -14,11 +14,13 @@ #include #include #include +#include #include #include #include "avd.h" +#include "avd-inst.h" #include "avd-regs.h" static void calc_tile_meta(u32 w, u32 h, u32 bpb, u32 tile_dim, @@ -54,9 +56,11 @@ void fill_comp(struct avd_comp *comp, enum avd_image_fmt image_fmt, u32 width, /* y has 32x32 tiles and 32 bytes of metadata per tile */ calc_tile_meta(width, height, bit_depth, 32, 32, &y, &y_meta); - /* uv has 16x16 tiles and 8 bytes of metadata per tile */ - calc_tile_meta(width / 2, height / 2, bit_depth * 2, 16, 8, &uv, - &uv_meta); + /* uv has 16x16 tiles and 8 bytes of metadata per tile; 4:2:2 chroma is full height */ + calc_tile_meta(width / 2, + (image_fmt == AVD_IMG_FMT_422_8BIT || + image_fmt == AVD_IMG_FMT_422_10BIT) ? height : height / 2, + bit_depth * 2, 16, 8, &uv, &uv_meta); /* output like DCP driver expects */ comp->offsets[0] = y; @@ -67,46 +71,22 @@ void fill_comp(struct avd_comp *comp, enum avd_image_fmt image_fmt, u32 width, comp->size = y_meta + y + uv_meta + uv; } - -int alloc_slots(struct avd_dev *avd, struct avd_ctx *ctx, enum avd_codec codec) { - u32 free; - u32 offset = 0; - for (int i = 0; i < codec; i++) - offset += avd->variant->vp_slots[i]; - - free = find_next_zero_bit(&avd->vp_slots, - offset + avd->variant->vp_slots[codec], - offset); - - if (free >= offset + avd->variant->vp_slots[codec]) - return -ENOMEM; - - set_bit(free, &avd->vp_slots); - ctx->vp_slot = free; - - ctx->fifo_idx = find_first_zero_bit(&avd->inst_fifo_slots, - avd->variant->fifo_slots); - - if (WARN_ON(ctx->fifo_idx >= avd->variant->fifo_slots)) { - clear_bit(free, &avd->vp_slots); - return -ENOMEM; - } - set_bit(ctx->fifo_idx, &avd->inst_fifo_slots); - - return 0; -} - int avd_buf_alloc(struct avd_dev *avd, struct avd_buf *buf, size_t size) { - if (!buf->cpu && size < buf->size) - return 0; - else if (buf->cpu) - avd_buf_free(avd, buf); + /* Keep size equal to the payload length used by the H.264 slice path. */ + avd_buf_free(avd, buf); + + if (size <= 0) + return -ENOMEM; buf->size = size; buf->cpu = dma_alloc_coherent(avd->dev, buf->size, &buf->addr, GFP_KERNEL); - return buf->cpu ? 0 : -ENOMEM; + if (!buf->cpu) { + memset(buf, 0, sizeof(*buf)); + return -ENOMEM; + } + return 0; } void avd_buf_free(struct avd_dev *avd, struct avd_buf *buf) @@ -134,12 +114,109 @@ avd_get_ref_buf(struct avd_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp) return vb2_to_avd_decoded_buf(buf); } +static int avd_wait_submission_queue(struct avd_ctx *ctx, int vp) +{ + struct avd_dev *avd = ctx->dev; + u32 max = readl_relaxed(avd->ctrl + + avd->variant->submit_queue_max_offset + vp * 4); + u32 cur = readl_relaxed( + avd->ctrl + avd->variant->submit_queue_status_offset + vp * 4); + + if (cur == max) { + dev_err(avd->dev, "instruction que full! %d/%d", cur, max); + return 1; + } + + if (cur >= max / 2) { + /* TODO: to high? low? Has weird side effects??? */ + usleep_range(500, 650); + } + return 0; +} + +int avd_init_job(struct avd_ctx *ctx, enum avd_codec codec, size_t segments) +{ + int ret = 0; + struct avd_job *job = &ctx->job; + + job->codec = codec; + job->num = 0; + /* A table that was built but never submitted still belongs to us. */ + kvfree(job->segments); + job->segments = kvcalloc(segments, sizeof(*job->segments), GFP_KERNEL); + if (!job->segments) + ret = -ENOMEM; + return ret; +} + +int avd_submit_job(struct avd_ctx *ctx) +{ + struct avd_dev *avd = ctx->dev; + struct avd_job *sub = &ctx->job; + struct avd_segment *segments, *seg; + unsigned long flags; + size_t num; + int i, idx = 0, vp = 0; + void __iomem *reg; + u32 exec_mask = sub->codec == AVD_CODEC_VP9 && + avd->variant->revision == 3 ? + AVD_OP_EXEC_REV3_VP9_MASK : + 0; + u32 exec_rev_flag = + AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == 4) | + AVD_OP_EXEC_FLAG_START_REV3(avd->variant->revision == 3); + + ctx->fifo_idx = 0; + for (i = 0; i < sub->codec; i++) + vp += avd->variant->vp_slots[i]; + + /* + * avd_device_run() keeps this job from being finished (and the next one + * from starting) until it returns, and arms the watchdog itself once the + * job belongs to the hardware. Still walk a local copy of the table. + */ + segments = sub->segments; + num = sub->num; + if (WARN_ON_ONCE(!segments)) + return -EINVAL; + + avd->variant->configure_stream(avd, ctx->inst.addr, ctx->fifo_idx, vp); + reg = avd->ctrl + avd->variant->vp_slot_offset + vp * 4; + + /* the first segment is always the header (needs special handling) */ + + writel(AVD_OP_EXEC | exec_mask | exec_rev_flag | + AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx), + reg); + seg = &segments[idx++]; + for (i = 0; i < seg->num; i++) + writel(seg->instructions[i], reg); + for (; idx <= num; idx++) { + seg = &segments[idx]; + for (i = 0; i < seg->num; i++) + writel(seg->instructions[i], reg); + if (avd_wait_submission_queue(ctx, vp)) + break; + if (idx == num) { + /* A completion from here on belongs to this job. */ + spin_lock_irqsave(&avd->job_lock, flags); + avd->job_end_sent = true; + spin_unlock_irqrestore(&avd->job_lock, flags); + } + writel(AVD_OP_EXEC | exec_mask | + AVD_OP_EXEC_FLAG_END(idx == num), + reg); + } + + if (sub->segments == segments) + sub->segments = NULL; + kvfree(segments); + return 0; +} static int avd_reset(struct avd_dev *avd) { int ret = 0; - avd->vp_slots = 0; - avd->inst_fifo_slots = 0; ret = pm_runtime_resume_and_get(avd->dev); if (ret < 0) @@ -167,6 +244,8 @@ static void avd_watchdog_func(struct work_struct *work) { struct avd_dev *avd; struct avd_ctx *ctx; + unsigned long flags; + bool claimed; int ret; ctx = container_of(to_delayed_work(work), struct avd_ctx, watchdog_work); @@ -175,11 +254,23 @@ static void avd_watchdog_func(struct work_struct *work) avd = ctx->dev; - dev_err(avd->dev, "Frame processing timed out! Vp: %d (%02d)", - ctx->vp_slot, ctx->fifo_idx); + /* + * Time out only the job this watchdog was armed for, and only while the + * hardware still owns it. Otherwise that job already finished, and + * resetting the hardware would break whichever job is running now. + */ + spin_lock_irqsave(&avd->job_lock, flags); + claimed = avd->job_ctx == ctx && avd->job_seq == ctx->wd_seq && + avd->job_state == AVD_JOB_SUBMITTED; + if (claimed) + avd->job_state = AVD_JOB_FINISHING; + spin_unlock_irqrestore(&avd->job_lock, flags); + dev_dbg(avd->dev, "avd-job: watchdog seq %llu: %s\n", ctx->wd_seq, + claimed ? "timeout" : "stale, ignored"); + if (!claimed) + return; - free_vp_slot(avd, ctx); - free_inst_slot(avd, ctx); + dev_err(avd->dev, "Frame processing timed out!"); writel(0, avd->mbox + AVD_REG_MBOX_IRQ_ENABLE); ret = avd_reset(avd); @@ -195,6 +286,8 @@ static irqreturn_t avd_irq_handler(int irq, void *data) struct avd_ctx *ctx = v4l2_m2m_get_curr_priv(avd->m2m_dev); enum vb2_buffer_state state; + unsigned long flags; + bool finish = false, stale = false; u32 status; if (!ctx) return IRQ_HANDLED; @@ -212,24 +305,44 @@ static irqreturn_t avd_irq_handler(int irq, void *data) if (status & 0x1000) { /* pp is done ! we are done */ state = VB2_BUF_STATE_DONE; - - free_inst_slot(avd, ctx); } else if (status & 0x100) { - free_vp_slot(avd, ctx); /* a vp is done, kick the pp and hope for the best */ if(ctx->coded_fmt_desc->ops->submit) ctx->coded_fmt_desc->ops->submit(ctx); - goto done; } else { - dev_err(avd->dev, "H%d %02d error", status, ctx->fifo_idx); + dev_err(avd->dev, "H%d error", status); /* let watchdog handle */ goto done; } - /* if the watchdog_work has run the work has already been submitted */ - if (cancel_delayed_work(&ctx->watchdog_work)) + /* + * Finish only a job the hardware owns. While avd_device_run() is still + * setting up or submitting, a completion is left over from an older job + * unless this job's END command already went out; then remember it and + * let avd_device_run() finish the job when it returns. + */ + spin_lock_irqsave(&avd->job_lock, flags); + if (avd->job_ctx != ctx) { + stale = true; + } else if (avd->job_state == AVD_JOB_SUBMITTED) { + avd->job_state = AVD_JOB_FINISHING; + finish = true; + } else if (avd->job_state == AVD_JOB_RUNNING && avd->job_end_sent) { + avd->job_pending = true; + avd->job_pending_result = state; + } else { + stale = true; + } + spin_unlock_irqrestore(&avd->job_lock, flags); + dev_dbg(avd->dev, "avd-job: irq %#x: %s\n", status, + finish ? "finish" : stale ? "stale, ignored" : "pending"); + if (stale) + dev_warn_ratelimited(avd->dev, "ignoring stale completion"); + if (finish) { + cancel_delayed_work(&ctx->watchdog_work); avd_job_finish(ctx, state); + } done: return IRQ_HANDLED; @@ -240,20 +353,74 @@ static void avd_device_run(void *priv) struct avd_ctx *ctx = priv; struct avd_dev *avd = ctx->dev; const struct avd_coded_fmt_desc *desc = ctx->coded_fmt_desc; + enum vb2_buffer_state result = VB2_BUF_STATE_ERROR; + const char *outcome = "already finished"; + unsigned long flags; + bool finish = false; + u64 seq; int ret; if (WARN_ON(!desc)) return; + /* + * IRQ completion cannot wait for an already running watchdog. Drain it + * here, before reusing wd_seq, so an old callback cannot time out this + * context's next job. device_run is sleepable; hold no job_lock here. + */ + cancel_delayed_work_sync(&ctx->watchdog_work); + ret = pm_runtime_resume_and_get(avd->dev); if (ret < 0) { avd_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR); return; } + /* + * While the job is RUNNING neither completion IRQs nor the watchdog may + * finish it: v4l2-mem2mem would start the next job on a kworker while + * this one still builds and submits ctx->job. + */ + spin_lock_irqsave(&avd->job_lock, flags); + seq = ++avd->job_seq; + avd->job_ctx = ctx; + avd->job_state = AVD_JOB_RUNNING; + avd->job_end_sent = false; + avd->job_pending = false; + spin_unlock_irqrestore(&avd->job_lock, flags); + dev_dbg(avd->dev, "avd-job: run seq %llu\n", seq); + ret = desc->ops->run(ctx); - if (ret) + if (ret) { + dev_dbg(avd->dev, "avd-job: run seq %llu failed: %d\n", seq, ret); avd_job_finish(ctx, VB2_BUF_STATE_ERROR); + return; + } + + /* + * The run either finished the job itself (held capture buffer) or handed + * it to the hardware: arm the watchdog, or finish right away if the + * completion already arrived. + */ + spin_lock_irqsave(&avd->job_lock, flags); + if (avd->job_seq == seq && avd->job_state == AVD_JOB_RUNNING) { + if (avd->job_pending) { + avd->job_state = AVD_JOB_FINISHING; + result = avd->job_pending_result; + finish = true; + outcome = "completed during submission"; + } else { + avd->job_state = AVD_JOB_SUBMITTED; + ctx->wd_seq = seq; + schedule_delayed_work(&ctx->watchdog_work, msecs_to_jiffies(2000)); + outcome = "submitted"; + } + } + spin_unlock_irqrestore(&avd->job_lock, flags); + dev_dbg(avd->dev, "avd-job: run seq %llu: %s\n", seq, outcome); + + if (finish) + avd_job_finish(ctx, result); } static int avd_queue_init(void *priv, struct vb2_queue *src_vq, @@ -275,6 +442,7 @@ static int avd_queue_init(void *priv, struct vb2_queue *src_vq, src_vq->lock = &ctx->dev->vdev_lock; src_vq->dev = ctx->dev->v4l2_dev.dev; src_vq->supports_requests = true; + src_vq->allow_cache_hints = true; ret = vb2_queue_init(src_vq); if (ret) @@ -291,6 +459,7 @@ static int avd_queue_init(void *priv, struct vb2_queue *src_vq, dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; dst_vq->lock = &ctx->dev->vdev_lock; dst_vq->dev = ctx->dev->v4l2_dev.dev; + dst_vq->allow_cache_hints = true; return vb2_queue_init(dst_vq); } @@ -307,6 +476,10 @@ static int avd_open(struct file *filp) ctx->dev = avd; + ret = avd_buf_alloc(avd, &ctx->inst, fifo_size()); + if (ret) + goto err_free_ctx; + INIT_DELAYED_WORK(&ctx->watchdog_work, avd_watchdog_func); avd_reset_coded_fmt(ctx); @@ -332,6 +505,7 @@ static int avd_open(struct file *filp) v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); err_free_ctx: + avd_buf_free(avd, &ctx->inst); kfree(ctx); return ret; } @@ -342,8 +516,12 @@ static int avd_release(struct file *filp) v4l2_fh_del(&ctx->fh, filp); v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); + /* The job has finished; its watchdog must not run on the freed ctx. */ + cancel_delayed_work_sync(&ctx->watchdog_work); v4l2_ctrl_handler_free(&ctx->ctrl_hdl); v4l2_fh_exit(&ctx->fh); + avd_buf_free(ctx->dev, &ctx->inst); + kvfree(ctx->job.segments); kfree(ctx); return 0; @@ -641,6 +819,7 @@ static int avd_probe(struct platform_device *pdev) avd->pdev = pdev; mutex_init(&avd->vdev_lock); + spin_lock_init(&avd->job_lock); match = of_match_node(avd_of_match, pdev->dev.of_node); avd->variant = match->data; diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index 1c8938a05a9a43..27880698a810a0 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -13,9 +13,7 @@ * Tomasz Figa */ -#include "linux/dev_printk.h" -#include -#include +#include #include #include @@ -31,6 +29,9 @@ #define H264_TRANSFORM_8X8_MODE(v) FIELD_PREP(BIT(7), !!(v)) +/* not a hardware constraint */ +#define MAX_SLICES 4096 + struct avd_h264_run { struct avd_run base; @@ -43,7 +44,7 @@ struct avd_h264_run { const struct v4l2_ctrl_h264_pred_weights *pred_weights; struct run_addr { - dma_addr_t sps; + dma_addr_t mv_color; } addresses; s32 cur_poc; @@ -52,16 +53,25 @@ struct avd_h264_run { /* state */ struct avd_h264_ctx { + bool monochrome; struct avd_h264_reflists { struct v4l2_h264_reference p[V4L2_H264_REF_LIST_LEN]; struct v4l2_h264_reference b0[V4L2_H264_REF_LIST_LEN]; struct v4l2_h264_reference b1[V4L2_H264_REF_LIST_LEN]; } reflists; + struct avd_buf slices[MAX_SLICES]; + struct avd_buf *active_slice; + size_t slice_num; + struct avd_h264_bufs { - struct avd_buf pps_tile[5]; struct avd_buf inst; struct avd_buf pipe_state; + struct avd_buf above_info; + struct avd_buf lf_above_info; + struct avd_buf lf_above; + struct avd_buf ip_above; + struct avd_buf mv_above_info; } bufs; }; @@ -81,7 +91,7 @@ static const u32 default_8x8_inter[] = { 0x191b1c1e, 0x1b1c1e20, 0x1c1e2021, 0x1e202123, }; -static inline u32 sps_size(u32 w, u32 h) +static inline u32 mv_color_size(u32 w, u32 h) { return (DIV_ROUND_UP(w, 16) + 1) * (DIV_ROUND_UP(h, 16) + 1) * 64; } @@ -93,15 +103,14 @@ static void stream_refs(struct avd_ctx *ctx, struct avd_h264_run *run) const struct v4l2_ctrl_h264_decode_params *decode = run->decode_params; const struct v4l2_h264_dpb_entry *dpb = decode->dpb; struct avd_h264_ctx *h264_ctx = ctx->priv; - struct avd_dev *avd = ctx->dev; struct avd_decoded_buffer *dst, *ref; dma_addr_t addr; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); push(0, ""); - pusha(h264_ctx->bufs.pps_tile[4].addr, "hdr_9c_pps_tile_addr_lsb8", 7); - pusha(run->addresses.sps, "hdr_bc_sps_tile_addr_lsb8", 0); + pusha(h264_ctx->bufs.mv_above_info.addr, "mv_above_info", 7); + pusha(run->addresses.mv_color, "mv_color", 0); push(0, ""); push(0, ""); @@ -125,7 +134,7 @@ static void stream_refs(struct avd_ctx *ctx, struct avd_h264_run *run) dpb[i].top_field_order_cnt), "hdr_d0_ref_hdr"); - push_comp(avd, ctx, addr, ctx->comp.offsets); + push_comp(ctx, addr, ctx->comp.offsets); } } @@ -134,7 +143,6 @@ static void stream_scaling(struct avd_ctx *ctx, struct avd_h264_run *run) const struct v4l2_ctrl_h264_pps *pps = run->pps; const struct v4l2_ctrl_h264_scaling_matrix *scaling = run->scaling_matrix; - struct avd_dev *avd = ctx->dev; push(H264_SCL_DIMS, "hdr_4c_pic_scaling_list_dims"); @@ -196,11 +204,6 @@ static void stream_hdr(struct avd_ctx *ctx, struct avd_h264_run *run) u32 width = (sps->pic_width_in_mbs_minus1 + 1) * 16; u32 height = (sps->pic_height_in_map_units_minus1 + 1) * 16; - push(AVD_OP_EXEC | AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | - AVD_OP_EXEC_FLAG_START_REV3(avd->variant->revision == 3) | - AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == 4), - "inst_fifo_start"); - push(AVD_OP_HDR | AVD_OP_HDR_FLAG_DECOMP(ctx->decomp) | AVD_OP_HDR_FLAG_INTRA( decode->flags & @@ -260,7 +263,7 @@ static void stream_hdr(struct avd_ctx *ctx, struct avd_h264_run *run) if (avd->variant->revision == 3) push(0, "zero"); - pusha(h264_ctx->bufs.pps_tile[0].addr, "hdr_9c_pps_tile_addr_lsb8", 0); + pusha(h264_ctx->bufs.above_info.addr, "hdr_9c_pps_tile_addr_lsb8", 0); push(0, ""); push(0, ""); @@ -270,12 +273,12 @@ static void stream_hdr(struct avd_ctx *ctx, struct avd_h264_run *run) else if (!(avd->variant->quirks & AVD_QUIRK_NO_PIPE_STATE)) pusha(h264_ctx->bufs.pipe_state.addr, "pipe_state", 0); - pusha(h264_ctx->bufs.pps_tile[1].addr, "hdr_9c_pps_tile_addr_lsb8", 1); - pusha(h264_ctx->bufs.pps_tile[2].addr, "hdr_9c_pps_tile_addr_lsb8", 2); - pusha(h264_ctx->bufs.pps_tile[3].addr, "hdr_9c_pps_tile_addr_lsb8", 3); + pusha(h264_ctx->bufs.ip_above.addr, "ip_above", 1); + pusha(h264_ctx->bufs.lf_above.addr, "lf_above", 2); + pusha(h264_ctx->bufs.lf_above_info.addr, "lf_above_info", 3); push(0, ""); - push_comp(avd, ctx, run->base.comp_out, ctx->comp.offsets); + push_comp(ctx, run->base.comp_out, ctx->comp.offsets); bytesperline = ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; if (avd->variant->quirks & AVD_QUIRK_LSR) @@ -308,7 +311,6 @@ static void stream_weights(struct avd_ctx *ctx, struct avd_h264_run *run) const struct v4l2_ctrl_h264_pred_weights *weights = run->pred_weights; const struct v4l2_ctrl_h264_pps *pps = run->pps; const struct v4l2_ctrl_h264_slice_params *sl = run->slice_params; - struct avd_dev *avd = ctx->dev; bool pred_weight_req = V4L2_H264_CTRL_PRED_WEIGHTS_REQUIRED(pps, sl); bool default_weights = pps->weighted_bipred_idc == 2 && @@ -386,18 +388,17 @@ static void stream_weights(struct avd_ctx *ctx, struct avd_h264_run *run) } } -static u32 stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) +static void stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) { const struct v4l2_ctrl_h264_decode_params *decode = run->decode_params; const struct v4l2_ctrl_h264_pps *pps = run->pps; const struct v4l2_ctrl_h264_sps *sps = run->sps; const struct v4l2_ctrl_h264_slice_params *sl = run->slice_params; - struct avd_dev *avd = ctx->dev; - struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); - u32 payload_len = vb2_get_plane_payload(&src->vb2_buf, 0); + struct avd_h264_ctx *h264_ctx = ctx->priv; + u32 payload_len = h264_ctx->active_slice->size; bool en_mode = (pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE) == 0; - const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); + const u8 *data = h264_ctx->active_slice->cpu; u32 min_off = (sl->header_bit_size + (en_mode ? 0 : 7)) / 8; @@ -406,14 +407,14 @@ static u32 stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) u32 off = 2; u32 bytes_read = 2; - while (bytes_read < min_off) { + while (bytes_read < min_off && off < payload_len) { if (data[off - 2] != 0x00 || data[off - 1] != 0x00 || data[off] != 0x03) bytes_read++; off++; } - dma_addr_t slc_a84 = run->base.coded_in + off; + dma_addr_t slc_a84 = h264_ctx->active_slice->addr + off; push(AVD_OP_CODED_DATA | AVD_OP_CODED_DATA_BIT_OFF( @@ -498,23 +499,15 @@ static u32 stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) &ctx->fh.m2m_ctx->cap_q_ctx.q, decode->dpb[sl->ref_pic_list1[0].index].reference_ts); - dma_addr_t sps_tile_addr = + dma_addr_t mv_color_addr = vb ? vb2_dma_contig_plane_dma_addr(vb, 0) + (vb->planes[0].length - - sps_size(fmt_width(ctx), - fmt_height(ctx))) : - run->addresses.sps; + mv_color_size(fmt_width(ctx), + fmt_height(ctx))) : + run->addresses.mv_color; - pusha(sps_tile_addr, "slc_a78_sps_tile_addr2_lsb8", 0); + pusha(mv_color_addr, "slc_a78_sps_tile_addr2_lsb8", 0); } - - /* only submit if this is the last slice */ - push(AVD_OP_EXEC | - AVD_OP_EXEC_FLAG_END(!( - src->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)), - "cm3_cmd_inst_fifo_end"); - - return payload_len - off; } static int avd_h264_alloc_bufs(struct avd_ctx *ctx) @@ -530,62 +523,42 @@ static int avd_h264_alloc_bufs(struct avd_ctx *ctx) ret = avd_buf_alloc(dev, &h264_ctx->bufs.inst, fifo_size()); if (ret) { - dev_err(dev->dev, "inst alloc failed\n"); return ret; } if (!(dev->variant->quirks & AVD_QUIRK_NO_PIPE_STATE)) { ret = avd_buf_alloc(dev, &h264_ctx->bufs.pipe_state, 0x200); if (ret) { - dev_err(dev->dev, "pipe state alloc failed\n"); return ret; } } mb = DIV_ROUND_UP(w, 16); - ret = avd_buf_alloc(dev, &h264_ctx->bufs.pps_tile[0], mb * 20); + ret = avd_buf_alloc(dev, &h264_ctx->bufs.above_info, mb * 20); if (ret) return ret; - ret = avd_buf_alloc(dev, &h264_ctx->bufs.pps_tile[1], - bit_depth * 4 * mb); + ret = avd_buf_alloc(dev, &h264_ctx->bufs.ip_above, bit_depth * 4 * mb); if (ret) return ret; - ret = avd_buf_alloc(dev, &h264_ctx->bufs.pps_tile[2], + ret = avd_buf_alloc(dev, &h264_ctx->bufs.lf_above, bit_depth * 4 * 4 * mb); if (ret) return ret; - ret = avd_buf_alloc(dev, &h264_ctx->bufs.pps_tile[3], 32 * mb); + ret = avd_buf_alloc(dev, &h264_ctx->bufs.lf_above_info, 32 * mb); if (ret) return ret; - ret = avd_buf_alloc(dev, &h264_ctx->bufs.pps_tile[4], 32 * mb); + ret = avd_buf_alloc(dev, &h264_ctx->bufs.mv_above_info, 32 * mb); if (ret) return ret; return 0; } -static void avd_h264_free_bufs(struct avd_ctx *ctx) -{ - struct avd_h264_ctx *h264_ctx = ctx->priv; - struct avd_dev *dev = ctx->dev; - - if (!h264_ctx) - return; - - avd_buf_free(dev, &h264_ctx->bufs.pipe_state); - avd_buf_free(dev, &h264_ctx->bufs.inst); - - for (int i = 0; i < 5; i++) - avd_buf_free(dev, &h264_ctx->bufs.pps_tile[i]); - - kfree(h264_ctx); -} - static int avd_h264_validate_pps(struct avd_ctx *ctx, const struct v4l2_ctrl_h264_pps *pps) { @@ -606,6 +579,12 @@ static int avd_h264_validate_sps(struct avd_ctx *ctx, if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8) /* Luma and chroma bit depth mismatch */ return -EINVAL; + if (sps->bit_depth_luma_minus8 != 0 && sps->bit_depth_luma_minus8 != 2) + /* Only 8 and 10 bit have capture formats */ + return -EINVAL; + if (sps->chroma_format_idc == 2 && sps->bit_depth_luma_minus8 == 2) + /* 4:2:2 10 bit would need P210; P010 is too small for its chroma */ + return -EINVAL; if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)) /* no interlaced support */ return -EINVAL; @@ -613,6 +592,30 @@ static int avd_h264_validate_sps(struct avd_ctx *ctx, return 0; } +static void avd_h264_stop(struct avd_ctx *ctx) +{ + struct avd_h264_ctx *h264_ctx = ctx->priv; + struct avd_dev *dev = ctx->dev; + int i; + + if (!h264_ctx) + return; + + avd_buf_free(dev, &h264_ctx->bufs.pipe_state); + avd_buf_free(dev, &h264_ctx->bufs.inst); + avd_buf_free(dev, &h264_ctx->bufs.above_info); + avd_buf_free(dev, &h264_ctx->bufs.lf_above_info); + avd_buf_free(dev, &h264_ctx->bufs.lf_above); + avd_buf_free(dev, &h264_ctx->bufs.ip_above); + avd_buf_free(dev, &h264_ctx->bufs.mv_above_info); + + for (i = 0; i < h264_ctx->slice_num; i++) + avd_buf_free(dev, &h264_ctx->slices[i]); + + kfree(h264_ctx); + ctx->priv = NULL; +} + static int avd_h264_start(struct avd_ctx *ctx) { struct avd_h264_ctx *h264_ctx; @@ -640,24 +643,14 @@ static int avd_h264_start(struct avd_ctx *ctx) return 0; err_free_ctx: - kfree(h264_ctx); - ctx->priv = NULL; + avd_h264_stop(ctx); return ret; } -static void avd_h264_stop(struct avd_ctx *ctx) -{ - avd_h264_free_bufs(ctx); - - /* needed for all so automatic? */ - free_vp_slot(ctx->dev, ctx); - free_inst_slot(ctx->dev, ctx); -} - static void avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) { struct v4l2_ctrl *ctrl; - u32 dst_len, sps_len; + u32 dst_len, mv_color_len; ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_DECODE_PARAMS); @@ -680,21 +673,87 @@ static void avd_h264_run_preamble(struct avd_ctx *ctx, struct avd_h264_run *run) dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; - sps_len = sps_size(fmt_width(ctx), fmt_height(ctx)); + mv_color_len = mv_color_size(fmt_width(ctx), fmt_height(ctx)); + + run->addresses.mv_color = run->base.y_out + (dst_len - mv_color_len); +} + +/* + * Check that every reference in the slice's ref lists points at a DPB entry + * that is VALID. stream_refs() only pushes a reference header (and its RVRA + * addresses) for VALID entries and the slice command indexes those by DPB + * index, so a reference to any other entry makes the VP dereference an + * uninitialised reference slot: that ends in a DART translation fault, a + * frame that never completes and a watchdog reset of the VP, which also + * disturbs the other contexts. It happens whenever a client starts decoding + * on a non-IDR frame (joining a live RTSP stream mid-GOP): ffmpeg's + * v4l2request hwaccel leaves the index of a missing reference at 0 and the + * DPB is empty. Refuse such slices before anything is pushed; the client gets + * the capture buffer back with V4L2_BUF_FLAG_ERROR, as for corrupt input. + */ +static bool avd_h264_refs_valid(const struct avd_h264_run *run) +{ + const struct v4l2_ctrl_h264_slice_params *sl = run->slice_params; + const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb; + const struct v4l2_h264_reference *lists[2] = { + sl->ref_pic_list0, sl->ref_pic_list1 + }; + u32 num[2] = { + sl->num_ref_idx_l0_active_minus1 + 1, + sl->num_ref_idx_l1_active_minus1 + 1 + }; + int nlists; + + if (sl->slice_type == V4L2_H264_SLICE_TYPE_P) + nlists = 1; + else if (sl->slice_type == V4L2_H264_SLICE_TYPE_B) + nlists = 2; + else + return true; + + for (int l = 0; l < nlists; l++) { + if (num[l] > V4L2_H264_REF_LIST_LEN) + return false; + for (u32 i = 0; i < num[l]; i++) { + u8 idx = lists[l][i].index; + + if (idx >= V4L2_H264_NUM_DPB_ENTRIES || + !(dpb[idx].flags & V4L2_H264_DPB_ENTRY_FLAG_VALID)) + return false; + } + } - run->addresses.sps = run->base.y_out + (dst_len - sps_len); + return true; } static int avd_h264_run(struct avd_ctx *ctx) { - struct avd_dev *avd = ctx->dev; struct avd_h264_ctx *h264_ctx = ctx->priv; struct v4l2_h264_reflist_builder reflist_builder; struct avd_h264_run run; - u32 slice_size, slice_parsed, reg; int ret; avd_h264_run_preamble(ctx, &run); + if (h264_ctx->slice_num >= MAX_SLICES) { + dev_err_ratelimited(ctx->dev->dev, + "slice_num > %d, stream was rejected!", + MAX_SLICES); + avd_run_postamble(ctx, &run.base); + return -EINVAL; + } + + struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); + u32 payload_len = vb2_get_plane_payload(&src->vb2_buf, 0); + const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); + h264_ctx->active_slice = &h264_ctx->slices[h264_ctx->slice_num]; + ret = avd_buf_alloc(ctx->dev, h264_ctx->active_slice, payload_len); + if (ret) { + avd_run_postamble(ctx, &run.base); + return ret; + } + memcpy(h264_ctx->active_slice->cpu, data, payload_len); + h264_ctx->slice_num++; + h264_ctx->monochrome = run.sps->chroma_format_idc == 0; /* Build the P/B{0,1} ref lists. */ v4l2_h264_init_reflist_builder(&reflist_builder, run.decode_params, @@ -709,54 +768,60 @@ static int avd_h264_run(struct avd_ctx *ctx) avd_run_postamble(ctx, &run.base); + if (!avd_h264_refs_valid(&run)) { + dev_dbg_ratelimited(ctx->dev->dev, + "slice references an invalid DPB entry, dropping frame\n"); + return -EINVAL; + } + if (is_new_frame(run.slice_params)) { - ret = alloc_slots(avd, ctx, AVD_CODEC_H264); - if (ret) { - dev_err_ratelimited(avd->dev, "no free slots: %d", ret); + /* the header segment plus one per slice */ + ret = avd_init_job(ctx, AVD_CODEC_H264, MAX_SLICES + 1); + if (ret) return ret; - } - avd->variant->configure_stream(ctx->dev, - h264_ctx->bufs.inst.addr, - ctx->fifo_idx, ctx->vp_slot); stream_hdr(ctx, &run); } - if (ctx->vp_slot == VP_SLOT_NONE) { - /* Only happens if its a multi slice frame and there was an error */ - dev_err_ratelimited(avd->dev, "no assigned VP slots: %04lx", - avd->vp_slots); - return -ENOMEM; - } - - schedule_delayed_work(&ctx->watchdog_work, msecs_to_jiffies(2000)); + if (!ctx->job.segments) + return -EINVAL; - slice_size = stream_slice(ctx, &run); + ctx->job.num++; + stream_slice(ctx, &run); if (run.base.bufs.src->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF) { - if (avd->variant->revision == 3) - reg = (0x18 | ctx->vp_slot << 12); - else - reg = (0x1018 | ctx->vp_slot << 8); + avd_job_finish(ctx, VB2_BUF_STATE_DONE); + return 0; + } - /* seems to be take ~ slice_size / 16 us */ - ret = readl_poll_timeout( - avd->ctrl + reg, slice_parsed, - slice_parsed >= round_down(slice_size, 8), 5, 1000); + return avd_submit_job(ctx); +} - if (ret) { - dev_err(avd->dev, - "VP%d: timed out (%02d)! size: %08x parsed: %08x", - ctx->vp_slot, ctx->fifo_idx, slice_size, - slice_parsed); - avd_status(avd, ctx->vp_slot); - return 0; +static void avd_h264_done(struct avd_ctx *ctx, struct vb2_v4l2_buffer *src_buf, + struct vb2_v4l2_buffer *dst_buf, + enum vb2_buffer_state result) +{ + struct avd_dev *avd = ctx->dev; + struct avd_h264_ctx *h264_ctx = ctx->priv; + int i; + + if (!(src_buf->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF)) { + for (i = 0; i < h264_ctx->slice_num; i++) + avd_buf_free(avd, &h264_ctx->slices[i]); + h264_ctx->slice_num = 0; + + /* 4:0:0 has no chroma to decode; the picture is grey */ + if (h264_ctx->monochrome && dst_buf && result == VB2_BUF_STATE_DONE) { + const struct v4l2_pix_format_mplane *pix = &ctx->decoded_fmt.fmt.pix_mp; + struct avd_decoded_buffer *dst = + vb2_to_avd_decoded_buf(&dst_buf->vb2_buf); + + /* vb2 must return cache ownership before the CPU writes. */ + dst->grey_chroma_offset = + (size_t)pix->plane_fmt[0].bytesperline * pix->height; + dst->grey_chroma_10bit = + ctx->image_fmt == AVD_IMG_FMT_420_10BIT; } - - if (cancel_delayed_work(&ctx->watchdog_work)) - avd_job_finish(ctx, VB2_BUF_STATE_DONE); } - - return 0; } static enum avd_image_fmt avd_h264_get_image_fmt(struct avd_ctx *ctx, @@ -786,7 +851,7 @@ static void avd_h264_adjust_decoded_fmt(struct avd_ctx *ctx, struct v4l2_pix_format_mplane *pix_mp) { pix_mp->plane_fmt[0].sizeimage += - sps_size(pix_mp->width, pix_mp->height); + mv_color_size(pix_mp->width, pix_mp->height); } static int avd_h264_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) @@ -802,9 +867,13 @@ static int avd_h264_try_ctrl(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) static void avd_h264_submit(struct avd_ctx *ctx) { writel_relaxed( - 0x2b000000 | - (ctx->dev->variant->revision == 3 ? 0x100 : 0x200) | - (ctx->fifo_idx << 4) | ctx->dev->variant->fifo_slots, + AVD_OP_EXEC | + AVD_OP_EXEC_FLAG_START_REV4( + ctx->dev->variant->revision == 4) | + AVD_OP_EXEC_FLAG_START_REV3( + ctx->dev->variant->revision == 3) | + AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | + AVD_OP_EXEC_FIFO_MASK(ctx->dev->variant->fifo_slots), ctx->dev->ctrl + ctx->dev->variant->submit_offset); } @@ -812,6 +881,7 @@ const struct avd_coded_fmt_ops avd_h264_fmt_ops = { .adjust_decoded_fmt = avd_h264_adjust_decoded_fmt, .start = avd_h264_start, .stop = avd_h264_stop, + .done = avd_h264_done, .run = avd_h264_run, .submit = avd_h264_submit, .try_ctrl = avd_h264_try_ctrl, diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 4f47c5124bef7c..97880fc3016dc9 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -13,19 +13,11 @@ * Tomasz Figa */ -#include "asm-generic/errno-base.h" -#include "linux/dev_printk.h" -#include -#include - -#include #include #include "avd.h" #include "avd-inst.h" -#define PPS_NUM 9 - #define NEW_TILE_ID BIT(0) #define NEW_SLICE BIT(1) @@ -52,7 +44,7 @@ #define HEVC_SCL_DIMS 0x127ffff -static inline u32 sps_size(u32 w, u32 h) +static inline u32 mv_color_size(u32 w, u32 h) { /* this will waste some memory when max cu size != 64 */ return DIV_ROUND_UP(w, 64) * DIV_ROUND_UP(h, 64) * 256; @@ -80,7 +72,7 @@ struct avd_hevc_run { int num_slices; struct run_addr { - dma_addr_t sps; + dma_addr_t mv_color; } addresses; struct avd_hevc_tile_info tile_info; @@ -90,7 +82,14 @@ struct avd_hevc_ctx { struct v4l2_ctrl_hevc_scaling_matrix scaling_matrix_cache; struct avd_h264_bufs { - struct avd_buf pps_tile[9]; + struct avd_buf mv_above_info; + struct avd_buf az_above; + struct avd_buf ip_above; + struct avd_buf lf_above; + struct avd_buf lf_above_info; + struct avd_buf lf_left; + struct avd_buf lf_left_info; + struct avd_buf sw_left; struct avd_buf inst; struct avd_buf pipe_state; } bufs; @@ -104,15 +103,13 @@ static void stream_refs(struct avd_ctx *ctx, struct avd_hevc_run *run) const struct v4l2_ctrl_hevc_slice_params *sl = &run->sl[0]; const struct v4l2_hevc_dpb_entry *dpb; struct avd_hevc_ctx *hevc_ctx = ctx->priv; - struct avd_dev *avd = ctx->dev; struct avd_decoded_buffer *dst, *ref_buf; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); push(0, ""); - pusha(hevc_ctx->bufs.pps_tile[1].addr, "hdr_9c_pps_tile_addr_lsb8", 7); - pusha(run->addresses.sps, "hdr_bc_sps_tile_addr_lsb8", - sl->slice_pic_order_cnt); + pusha(hevc_ctx->bufs.mv_above_info.addr, "mv_above_info", 7); + pusha(run->addresses.mv_color, "mv_color", 0); push(0, ""); push(0, ""); @@ -137,14 +134,13 @@ static void stream_refs(struct avd_ctx *ctx, struct avd_hevc_run *run) dpb->pic_order_cnt_val), "hdr_d0_ref_hdr"); - push_comp(avd, ctx, comp_addr, ref_buf->comp.offsets); + push_comp(ctx, comp_addr, ref_buf->comp.offsets); } } static void set_scaling_lists(struct avd_ctx *ctx, struct avd_hevc_run *run) { const struct v4l2_ctrl_hevc_scaling_matrix *s = run->scaling_matrix; - struct avd_dev *avd = ctx->dev; int i, j, k; u8 (*dc_16x16)[3] = (u8(*)[3])s->scaling_list_dc_coef_16x16; @@ -220,7 +216,6 @@ static void hevc_set_flags(struct avd_ctx *ctx, struct avd_hevc_run *run) const struct v4l2_ctrl_hevc_decode_params *decode = run->decode; const struct v4l2_ctrl_hevc_sps *sps = run->sps; const struct v4l2_ctrl_hevc_pps *pps = run->pps; - struct avd_dev *avd = ctx->dev; u32 log2_ctb_size = ((sps->log2_min_luma_coding_block_size_minus3) + sps->log2_diff_max_min_luma_coding_block_size); @@ -294,7 +289,6 @@ static void hevc_set_flags(struct avd_ctx *ctx, struct avd_hevc_run *run) static void set_header(struct avd_ctx *ctx, struct avd_hevc_run *run) { const struct v4l2_ctrl_hevc_sps *sps = run->sps; - const struct v4l2_ctrl_hevc_pps *pps = run->pps; struct avd_dev *avd = ctx->dev; struct avd_hevc_ctx *hevc_ctx = ctx->priv; u32 bytesperline; @@ -303,11 +297,6 @@ static void set_header(struct avd_ctx *ctx, struct avd_hevc_run *run) bool is_intra = run->sl[0].slice_type == V4L2_HEVC_SLICE_TYPE_I; - push(AVD_OP_EXEC | AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | - AVD_OP_EXEC_FLAG_START_REV3(avd->variant->revision == 3) | - AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == 4), - "inst_fifo_start"); - push(AVD_OP_HDR | AVD_OP_HDR_FLAG_DECOMP(ctx->decomp) | AVD_OP_HDR_FLAG_INTRA(is_intra) | AVD_OP_HDR_CONST | AVD_OP_HDR_FLAG_PIPE_STATE( @@ -365,35 +354,19 @@ static void set_header(struct avd_ctx *ctx, struct avd_hevc_run *run) else if (!(avd->variant->quirks & AVD_QUIRK_NO_PIPE_STATE)) pusha(hevc_ctx->bufs.pipe_state.addr, "pipe_state", 0); - pusha(hevc_ctx->bufs.pps_tile[0].addr, "hdr_dc_pps_tile_addr_lsb8", 0); - pusha(hevc_ctx->bufs.pps_tile[2].addr, "hdr_dc_pps_tile_addr_lsb8", 1); - pusha(hevc_ctx->bufs.pps_tile[3].addr, "hdr_dc_pps_tile_addr_lsb8", 2); - - if (pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED) { - pusha(hevc_ctx->bufs.pps_tile[4].addr, - "hdr_dc_pps_tile_addr_lsb8", 3); - pusha(hevc_ctx->bufs.pps_tile[5].addr, - "hdr_dc_pps_tile_addr_lsb8", 4); - pusha(hevc_ctx->bufs.pps_tile[6].addr, - "hdr_dc_pps_tile_addr_lsb8", 8); - pusha(hevc_ctx->bufs.pps_tile[7].addr, - "hdr_dc_pps_tile_addr_lsb8", 9); - } else { - pusha(0, "", 3); - pusha(0, "", 4); - pusha(hevc_ctx->bufs.pps_tile[8].addr, - "hdr_dc_pps_tile_addr_lsb8", 8); - pusha(0, "", 9); - } + pusha(hevc_ctx->bufs.ip_above.addr, "ip_above", 0); + pusha(hevc_ctx->bufs.lf_above.addr, "lf_above", 1); + pusha(hevc_ctx->bufs.lf_above_info.addr, "lf_above_info", 2); + pusha(hevc_ctx->bufs.lf_left.addr, "lf_left", 3); + pusha(hevc_ctx->bufs.lf_left_info.addr, "lf_left_info", 4); + pusha(hevc_ctx->bufs.az_above.addr, "az_above", 8); + pusha(hevc_ctx->bufs.sw_left.addr, "sw_left", 9); push(0, ""); - push_comp(avd, ctx, run->base.comp_out, ctx->comp.offsets); - - push(0, "cm3_mark_end_section"); + push_comp(ctx, run->base.comp_out, ctx->comp.offsets); - if (!(avd->variant->quirks & AVD_QUIRK_LSR)) - push(0, "cm3_mark_end_section"); + pusha((u64)0, "packed_fmt_scratch", 0); bytesperline = ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; if (avd->variant->quirks & AVD_QUIRK_LSR) @@ -423,7 +396,6 @@ static void stream_weights(struct avd_ctx *ctx, struct avd_hevc_run *run, u8 chroma_log2_weight_denom; const struct v4l2_ctrl_hevc_pps *pps = run->pps; const struct v4l2_hevc_pred_weight_table *pred = &sl->pred_weight_table; - struct avd_dev *avd = ctx->dev; bool has_luma_weights = ((pps->flags & V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED) && sl->slice_type == V4L2_HEVC_SLICE_TYPE_P) || @@ -523,7 +495,6 @@ static void stream_slice_dqtblk(struct avd_ctx *ctx, struct avd_hevc_run *run, { const struct v4l2_ctrl_hevc_pps *pps = run->pps; const struct v4l2_ctrl_hevc_sps *sps = run->sps; - struct avd_dev *avd = ctx->dev; push(AVD_OP_QP | AVD_OP_QP_VAL(pps->init_qp_minus26 + 26 + @@ -591,7 +562,6 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, bool is_first) { const struct v4l2_ctrl_hevc_decode_params *decode = run->decode; - struct avd_dev *avd = ctx->dev; struct avd_decoded_buffer *dst, *ref; bool ref_valid; const u8 *ref_list; @@ -648,12 +618,12 @@ static void stream_slice_mv(struct avd_ctx *ctx, struct avd_hevc_run *run, "slc_a8c_cmd_ref_type"); if (ref_valid) { - dma_addr_t sps_tile_addr = + dma_addr_t mv_color_addr = vb2_dma_contig_plane_dma_addr(&ref->base.vb.vb2_buf, 0) + (ref->base.vb.planes[0].length - - sps_size(fmt_width(ctx), fmt_height(ctx))); - pusha(sps_tile_addr, "slc_bd4_sps_tile_addr2_lsb8", + mv_color_size(fmt_width(ctx), fmt_height(ctx))); + pusha(mv_color_addr, "slc_bd4_sps_tile_addr2_lsb8", decode->dpb[ref_list[sl->collocated_ref_idx]] .pic_order_cnt_val); } @@ -663,7 +633,6 @@ static void set_slice(struct avd_ctx *ctx, struct avd_hevc_run *run, const struct v4l2_ctrl_hevc_slice_params *sl, u32 size, u32 offset, u32 flags) { - struct avd_dev *avd = ctx->dev; dma_addr_t slc_addr = run->base.coded_in + offset + sl->data_byte_offset; push(AVD_OP_CODED_DATA | flags | AVD_OP_CODED_DATA_ADDR(slc_addr >> 32), @@ -676,11 +645,10 @@ static int submit_slice_segment(struct avd_ctx *ctx, struct avd_hevc_run *run, const struct v4l2_ctrl_hevc_slice_params *sl, int row, int col, u32 col_bd[23], u32 row_bd[23], u32 pic_in_cts_width, - u32 pic_in_cts_height, bool is_last, - bool first_slice, bool hflip, bool vflip, - u32 coded_flags, u32 last_tile_block) + u32 pic_in_cts_height, bool first_slice, + bool hflip, bool vflip, u32 coded_flags, + u32 last_tile_block) { - struct avd_dev *avd = ctx->dev; const struct v4l2_ctrl_hevc_pps *pps = run->pps; u32 tb_x, tb_y, tile_block, tile_boundary; @@ -735,9 +703,6 @@ static int submit_slice_segment(struct avd_ctx *ctx, struct avd_hevc_run *run, (coded_flags & NEW_SLICE ? tile_block : tile_boundary), "cm3_set_mv_xy"); - push(AVD_OP_EXEC | AVD_OP_EXEC_FLAG_END(is_last), - "cm3_cmd_inst_fifo_end"); - return last_tile_block; } @@ -846,30 +811,6 @@ static void compute_tile_ids(struct avd_hevc_run *run, u32 pic_in_ctbs_width, x]] = tile_idx; } -static int avd_wait_submission_queue(struct avd_ctx *ctx) -{ - struct avd_dev *avd = ctx->dev; - u32 max = readl_relaxed(avd->ctrl + - avd->variant->submit_queue_max_offset + - (ctx->vp_slot) * 4); - u32 cur = readl_relaxed(avd->ctrl + - avd->variant->submit_queue_status_offset + - (ctx->vp_slot) * 4); - - /* pr_info("%d/%d\n", cur, max); */ - - if (cur == max) { - dev_err(avd->dev, "instruction que full! %d/%d", cur, max); - return 1; - } - - if (cur >= max / 2) { - /* TODO: to high? low? Has weird side effects??? */ - usleep_range(100, 150); - } - return 0; -} - struct sl_ctx { u32 ctx_col; u32 ctx_row; @@ -878,14 +819,14 @@ struct sl_ctx { }; /* TODO */ -static void stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) +static int stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) { const struct v4l2_ctrl_hevc_sps *sps = run->sps; const struct v4l2_ctrl_hevc_pps *pps = run->pps; struct avd_hevc_ctx *hevc_ctx = ctx->priv; const struct v4l2_ctrl_hevc_slice_params *sl; struct avd_hevc_tile_info *tile_info = &run->tile_info; - bool tiles_enabled, is_last, first_slice, first_segment; + bool tiles_enabled, first_slice, first_segment; bool hflip, vflip; int slice_segment_offset, entry_point_idx = 0, pos = 0, offset = 0; int row, col, i, s, to; @@ -919,18 +860,19 @@ static void stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) to = tiles_enabled ? sl->num_entry_point_offsets + 1 : 1; if (tiles_enabled && sl->num_entry_point_offsets + entry_point_idx > - run->num_entry_point_offsets + 1) { + run->num_entry_point_offsets) { dev_err(ctx->dev->dev, - "to few entry points! has: %d, needs > %d", + "too few entry points: has %d, needs %d", run->num_entry_point_offsets, sl->num_entry_point_offsets + entry_point_idx); - return; + return -EINVAL; } for (i = 0; i < to; i++) { - is_last = i == to - 1 && s == run->num_slices - 1; first_segment = i == 0; first_slice = s == 0; + ctx->job.num++; + if (tiles_enabled && to > 1) { if (i < sl->num_entry_point_offsets) { size = run->entry_point_offsets @@ -1012,21 +954,20 @@ static void stream_slices(struct avd_ctx *ctx, struct avd_hevc_run *run) last_tile_block = submit_slice_segment( ctx, run, sl, row, col, tile_info->col_bd, tile_info->row_bd, pic_in_ctbs_width, - pic_in_ctbs_height, is_last, first_slice, hflip, - vflip, slice_flag, last_tile_block); + pic_in_ctbs_height, first_slice, hflip, vflip, + slice_flag, last_tile_block); if (slice_flag & NEW_TILE_ID) pos++; slice_segment_offset += new_offset; - - if (avd_wait_submission_queue(ctx)) - return; } offset += sl->bit_size / 8; } hevc_ctx->submit_num = pos; + + return 0; } static void update_dec_buf_info(struct avd_decoded_buffer *buf, @@ -1039,7 +980,7 @@ static void avd_hevc_adjust_decoded_fmt(struct avd_ctx *ctx, struct v4l2_pix_format_mplane *pix_mp) { pix_mp->plane_fmt[0].sizeimage += - sps_size(pix_mp->width, pix_mp->height); + mv_color_size(pix_mp->width, pix_mp->height); } static enum avd_image_fmt avd_hevc_get_image_fmt(struct avd_ctx *ctx, @@ -1076,6 +1017,15 @@ static int avd_hevc_validate_sps(struct avd_ctx *ctx, if (sps->pic_width_in_luma_samples > ctx->coded_fmt.fmt.pix_mp.width || sps->pic_height_in_luma_samples > ctx->coded_fmt.fmt.pix_mp.height) return -EINVAL; + if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8) + /* Luma and chroma bit depth mismatch: the firmware faults (H0 error) */ + return -EINVAL; + if (sps->bit_depth_luma_minus8 != 0 && sps->bit_depth_luma_minus8 != 2) + /* Only 8 and 10 bit have capture formats */ + return -EINVAL; + if (sps->chroma_format_idc == 2 && sps->bit_depth_luma_minus8 == 2) + /* 4:2:2 10 bit would need P210, which is not offered */ + return -EINVAL; return 0; } @@ -1097,6 +1047,29 @@ static int avd_hevc_alloc_bufs(struct avd_ctx *ctx) return 0; } +static void avd_hevc_stop(struct avd_ctx *ctx) +{ + struct avd_hevc_ctx *hevc_ctx = ctx->priv; + struct avd_dev *avd = ctx->dev; + + if (!hevc_ctx) + return; + + avd_buf_free(avd, &hevc_ctx->bufs.pipe_state); + avd_buf_free(avd, &hevc_ctx->bufs.inst); + avd_buf_free(avd, &hevc_ctx->bufs.mv_above_info); + avd_buf_free(avd, &hevc_ctx->bufs.az_above); + avd_buf_free(avd, &hevc_ctx->bufs.ip_above); + avd_buf_free(avd, &hevc_ctx->bufs.lf_above); + avd_buf_free(avd, &hevc_ctx->bufs.lf_above_info); + avd_buf_free(avd, &hevc_ctx->bufs.lf_left); + avd_buf_free(avd, &hevc_ctx->bufs.lf_left_info); + avd_buf_free(avd, &hevc_ctx->bufs.sw_left); + + kfree(hevc_ctx); + ctx->priv = NULL; +} + static int avd_hevc_start(struct avd_ctx *ctx) { struct avd_hevc_ctx *hevc_ctx; @@ -1114,8 +1087,7 @@ static int avd_hevc_start(struct avd_ctx *ctx) return 0; err_free_ctx: - kfree(hevc_ctx); - ctx->priv = NULL; + avd_hevc_stop(ctx); return ret; } @@ -1154,52 +1126,51 @@ static int avd_hevc_alloc_scratch(struct avd_ctx *ctx, struct avd_hevc_run *run) tile_info->row_height[i] : max_row; - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[0], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.ip_above, ((max_col * max_cu_width) / 4) * bit_depth); if (ret) return ret; - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[1], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.mv_above_info, ((max_col * max_cu_width) / 16) * 20); if (ret) return ret; - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[2], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.lf_above, DIV_ROUND_UP(w, 16) * bit_depth * (10 + 6) + (cols - 1) * 256); if (ret) return ret; - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[3], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.lf_above_info, DIV_ROUND_UP(w + 7, 16) * 36 + cols * 128); if (ret) return ret; if (pps->flags & V4L2_HEVC_PPS_FLAG_TILES_ENABLED) { - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[4], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.lf_left, ((max_row * max_cu_width) / 4) * 36 + 144 /* why? */); if (ret) return ret; - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[5], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.lf_left_info, ((max_row * max_cu_width) / 4) * 9); if (ret) return ret; - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[6], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.az_above, /* not sure if its cols or rows */ DIV_ROUND_UP(w, 64) * 144 + (cols - 1) * 128); if (ret) return ret; - /* 5 or 3 is info? */ - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[7], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.sw_left, ((max_row * max_cu_width) / 4) * 216); if (ret) return ret; } else { - ret = avd_buf_alloc(avd, &hevc_ctx->bufs.pps_tile[8], + ret = avd_buf_alloc(avd, &hevc_ctx->bufs.az_above, DIV_ROUND_UP(w + 7, 16) * 4 * bit_depth); if (ret) return ret; @@ -1285,46 +1256,27 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) return 0; } -static void avd_hevc_dealloc_scratch(struct avd_ctx *ctx) -{ - struct avd_hevc_ctx *hevc_ctx = ctx->priv; - struct avd_dev *avd = ctx->dev; - - for (int i = 0; i < PPS_NUM; i++) - avd_buf_free(avd, &hevc_ctx->bufs.pps_tile[i]); -} - -static void avd_hevc_stop(struct avd_ctx *ctx) +static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) { - struct avd_hevc_ctx *hevc_ctx = ctx->priv; - struct avd_dev *avd = ctx->dev; - - if (!hevc_ctx) - return; - - avd_buf_free(avd, &hevc_ctx->bufs.pipe_state); - avd_buf_free(avd, &hevc_ctx->bufs.inst); - - avd_hevc_dealloc_scratch(ctx); - - free_vp_slot(avd, ctx); - free_inst_slot(avd, ctx); + struct v4l2_ctrl *ctrl, *sl, *ep; + u32 dst_len, mv_color_len; - kfree(hevc_ctx); -} + if (!v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS) || + !v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS)) + return -EINVAL; -static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) -{ - struct v4l2_ctrl *ctrl; - u32 dst_len, sps_len; + /* + * Apply the request's controls before reading them. Until then the + * element counts of the dynamic arrays belong to the previous request + * (elems) or to whoever set controls last (new_elems), and setting a + * larger array can move their storage. + */ + avd_run_preamble(ctx, &run->base); ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_DECODE_PARAMS); run->decode = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_HEVC_SLICE_PARAMS); - run->sl = ctrl ? ctrl->p_cur.p : NULL; - run->num_slices = ctrl ? ctrl->new_elems : 0; ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SPS); run->sps = ctrl ? ctrl->p_cur.p : NULL; ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_PPS); @@ -1333,70 +1285,78 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) V4L2_CID_STATELESS_HEVC_SCALING_MATRIX); run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL; - ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, - V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS); - run->entry_point_offsets = ctrl ? ctrl->p_cur.p : NULL; - if (!run->entry_point_offsets) - return -EINVAL; - run->num_entry_point_offsets = ctrl ? ctrl->new_elems : 0; + sl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_SLICE_PARAMS); + ep = v4l2_ctrl_find(&ctx->ctrl_hdl, + V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS); - avd_run_preamble(ctx, &run->base); + /* + * Setting a larger dynamic array on a queued request reallocates the + * array's storage, which this run would otherwise keep using. Copy what + * the run needs while holding the control lock. + */ + v4l2_ctrl_lock(sl); + run->num_slices = sl->elems; + run->sl = kmemdup(sl->p_cur.p, sl->elems * sl->elem_size, GFP_KERNEL); + run->num_entry_point_offsets = ep->elems; + run->entry_point_offsets = + kmemdup(ep->p_cur.p, ep->elems * ep->elem_size, GFP_KERNEL); + v4l2_ctrl_unlock(sl); + if (!run->sl || !run->entry_point_offsets) { + kfree(run->sl); + kfree(run->entry_point_offsets); + avd_run_postamble(ctx, &run->base); + return -ENOMEM; + } dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; - sps_len = sps_size(fmt_width(ctx), fmt_height(ctx)); + mv_color_len = mv_color_size(fmt_width(ctx), fmt_height(ctx)); - run->addresses.sps = run->base.y_out + (dst_len - sps_len); + run->addresses.mv_color = run->base.y_out + (dst_len - mv_color_len); return 0; } static int avd_hevc_run(struct avd_ctx *ctx) { - struct avd_dev *avd = ctx->dev; - struct avd_hevc_run run; - struct avd_hevc_ctx *hevc_ctx; + struct avd_hevc_run run = {}; struct avd_decoded_buffer *dst; int ret; ret = avd_hevc_run_preamble(ctx, &run); - if (ret) { - dev_err(avd->dev, "avd_hevc_run_preamble: failed %d", ret); + if (ret) return ret; - } - hevc_ctx = ctx->priv; dst = vb2_to_avd_decoded_buf(&run.base.bufs.dst->vb2_buf); update_dec_buf_info(dst, &run.sl[0]); - ret = alloc_slots(avd, ctx, AVD_CODEC_HEVC); - if (ret) { - dev_err(avd->dev, "no free slots: %d", ret); - return ret; - } - ret = avd_hevc_compute_tiles(ctx, &run); if (ret) - goto done; + goto complete; ret = avd_hevc_alloc_scratch(ctx, &run); if (ret) - goto done; + goto complete; - /* pr_info("VP%d: start\n", ctx->vp_slot); */ - avd->variant->configure_stream(avd, hevc_ctx->bufs.inst.addr, - ctx->fifo_idx, ctx->vp_slot); - /* avd_status(avd, ctx->vp_slot); */ - set_header(ctx, &run); + ret = avd_init_job(ctx, AVD_CODEC_HEVC, + run.num_slices + run.num_entry_point_offsets + 1); + if (ret) + goto complete; - schedule_delayed_work(&ctx->watchdog_work, msecs_to_jiffies(2000)); - stream_slices(ctx, &run); - /* avd_status(avd, ctx->vp_slot); */ + set_header(ctx, &run); + ret = stream_slices(ctx, &run); avd_run_postamble(ctx, &run.base); + if (!ret) + ret = avd_submit_job(ctx); + goto done; - ret = 0; +complete: + /* The request was applied; complete it on every way out. */ + avd_run_postamble(ctx, &run.base); done: kfree(run.tile_info.ctb_addr_rs_to_ts); kfree(run.tile_info.tile_ids); + kfree(run.sl); + kfree(run.entry_point_offsets); return ret; } @@ -1413,12 +1373,17 @@ static void avd_hevc_submit(struct avd_ctx *ctx) struct avd_hevc_ctx *hevc_ctx = ctx->priv; struct avd_dev *avd = ctx->dev; - for (int i = 0; i < hevc_ctx->submit_num; i++) { - writel(0x2b000000 | - (i == 0 ? (avd->variant->revision == 3 ? 0x100 : - 0x200) : - 0) | - (ctx->fifo_idx << 4) | avd->variant->fifo_slots, + writel(AVD_OP_EXEC | + AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == + 4) | + AVD_OP_EXEC_FLAG_START_REV3(avd->variant->revision == + 3) | + AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | + AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), + avd->ctrl + avd->variant->submit_offset); + for (int i = 0; i < hevc_ctx->submit_num - 1; i++) { + writel(AVD_OP_EXEC | AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | + AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), avd->ctrl + avd->variant->submit_offset); } } diff --git a/drivers/media/platform/apple/avd/avd-inst.h b/drivers/media/platform/apple/avd/avd-inst.h index db741db572e77c..e9b9ef908b8d55 100644 --- a/drivers/media/platform/apple/avd/avd-inst.h +++ b/drivers/media/platform/apple/avd/avd-inst.h @@ -17,6 +17,18 @@ #define AVD_OP_HDR FIELD_PREP(GENMASK(31, 20), 0x2db) #define AVD_OP_HDR_CONST FIELD_PREP(GENMASK(10, 0), 0x2e0) + +/* + * only for 10 bit. + * output in packed p010 / nv15 kinda format + * 10 bits per component. With groups of tree packed into 4 bytes (little + * endian order) + * P 3 2 1 + * [2:10:10:10] + * + * additionally, av1 and vp9 need an extra scratch buffer if this is set + */ +#define AVD_OP_HDR_FLAG_PACKED(v) FIELD_PREP(BIT(10), !!(v)) /* decompress pixel data */ #define AVD_OP_HDR_FLAG_DECOMP(v) FIELD_PREP(BIT(12), !!(v)) #define AVD_OP_HDR_FLAG_INTRA(v) FIELD_PREP(BIT(13), !!(v)) @@ -153,30 +165,31 @@ static inline bool boolify(u32 v) return !!(v); } -static inline void push(struct avd_dev *avd, struct avd_ctx *ctx, u32 inst) +static inline void push(struct avd_ctx *ctx, u32 inst) { - writel(inst, avd->ctrl + avd->variant->vp_slot_offset + ctx->vp_slot * 4); + struct avd_job *job = &ctx->job; + struct avd_segment *seg = &job->segments[job->num]; + seg->instructions[seg->num++] = inst; } -static inline void push_address(struct avd_dev *avd, struct avd_ctx *ctx, - dma_addr_t addr) +static inline void push_address(struct avd_ctx *ctx, dma_addr_t addr) { - if (avd->variant->quirks & AVD_QUIRK_LSR) { - push(avd, ctx, (addr >> 8)); + if (ctx->dev->variant->quirks & AVD_QUIRK_LSR) { + push(ctx, (addr >> 8)); } else { - push(avd, ctx, (u32)(addr & 0xffffffff)); - push(avd, ctx, (u32)(addr >> 32)); + push(ctx, (u32)(addr & 0xffffffff)); + push(ctx, (u32)(addr >> 32)); } } -static inline void push_comp(struct avd_dev *avd, struct avd_ctx *ctx, - dma_addr_t addr, u32 offsets[4]) +static inline void push_comp(struct avd_ctx *ctx, dma_addr_t addr, + u32 offsets[4]) { - if (avd->variant->quirks & AVD_QUIRK_LSR) { + if (ctx->dev->variant->quirks & AVD_QUIRK_LSR) { for (int i = 0; i < 4; i++) - push(avd, ctx, (addr + offsets[i]) >> 7); + push(ctx, (addr + offsets[i]) >> 7); } else { for (int i = 0; i < 4; i++) - push_address(avd, ctx, (addr + offsets[i])); + push_address(ctx, (addr + offsets[i])); } } @@ -184,11 +197,11 @@ static inline void push_comp(struct avd_dev *avd, struct avd_ctx *ctx, #define push(inst, name) \ do { \ dev_info(ctx->dev->dev, "%8x | %s", (inst), name); \ - push(avd, ctx, inst); \ + push(ctx, inst); \ } while (0) #else -#define push(inst, name) push(avd, ctx, inst) +#define push(inst, name) push(ctx, inst) #endif #ifdef DEBUG_INST_ADDR @@ -198,11 +211,11 @@ static inline void push_comp(struct avd_dev *avd, struct avd_ctx *ctx, name, i); \ dev_info(ctx->dev->dev, "%8llx | %s[%d] (high)", (inst) >> 32, \ name, i); \ - push_address(avd, ctx, inst); \ + push_address(ctx, inst); \ } while (0) #else -#define pusha(inst, name, i) push_address(avd, ctx, inst) +#define pusha(inst, name, i) push_address(ctx, inst) #endif #endif /* AVD_INST_H_ */ diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 7d4b3e138cf994..9ab137883478a7 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -823,13 +823,46 @@ static int avd_buf_prepare(struct vb2_buffer *vb) * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets * it to buffer length). */ - if (V4L2_TYPE_IS_CAPTURE(vq->type)) + if (V4L2_TYPE_IS_CAPTURE(vq->type)) { + vb2_to_avd_decoded_buf(vb)->grey_chroma_offset = 0; vb2_set_plane_payload(vb, 0, f->fmt.pix_mp.plane_fmt[0].sizeimage); + } return 0; } +static void avd_buf_finish(struct vb2_buffer *vb) +{ + struct avd_decoded_buffer *buf; + size_t luma; + u8 *y; + + if (!V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) + return; + + buf = vb2_to_avd_decoded_buf(vb); + luma = buf->grey_chroma_offset; + buf->grey_chroma_offset = 0; + if (!luma || vb->state != VB2_BUF_STATE_DONE) + return; + + /* VB2 has synchronized the capture buffer for CPU access. */ + y = vb2_plane_vaddr(vb, 0); + if (!y) + return; + + if (buf->grey_chroma_10bit) { + /* P010: 16-bit little-endian samples, 512 << 6. */ + for (size_t i = 0; i + 1 < luma / 2; i += 2) { + y[luma + i] = 0x00; + y[luma + i + 1] = 0x80; + } + } else { + memset(y + luma, 0x80, luma / 2); + } +} + static void avd_buf_queue(struct vb2_buffer *vb) { struct avd_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); @@ -857,22 +890,31 @@ static int avd_start_streaming(struct vb2_queue *q, unsigned int count) { struct avd_ctx *ctx = vb2_get_drv_priv(q); const struct avd_coded_fmt_desc *desc; + struct vb2_v4l2_buffer *vbuf; int ret; if (V4L2_TYPE_IS_CAPTURE(q->type)) return 0; desc = ctx->coded_fmt_desc; - if (WARN_ON(!desc)) - return -EINVAL; + if (WARN_ON(!desc)) { + ret = -EINVAL; + goto err_return_buffers; + } if (desc->ops->start) { ret = desc->ops->start(ctx); if (ret) - return ret; + goto err_return_buffers; } return 0; + +err_return_buffers: + /* Keep requests queued for a later STREAMON retry. */ + while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) + v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED); + return ret; } static void avd_queue_cleanup(struct vb2_queue *vq, u32 state) @@ -908,6 +950,10 @@ static void avd_stop_streaming(struct vb2_queue *q) if (desc->ops->stop) desc->ops->stop(ctx); + + /* Drop a job table left over from a frame that never finished. */ + kvfree(ctx->job.segments); + ctx->job.segments = NULL; } avd_queue_cleanup(q, VB2_BUF_STATE_ERROR); @@ -916,6 +962,7 @@ static void avd_stop_streaming(struct vb2_queue *q) const struct vb2_ops avd_queue_ops = { .queue_setup = avd_queue_setup, .buf_prepare = avd_buf_prepare, + .buf_finish = avd_buf_finish, .buf_queue = avd_buf_queue, .buf_out_validate = avd_buf_out_validate, .buf_request_complete = avd_buf_request_complete, @@ -925,6 +972,17 @@ const struct vb2_ops avd_queue_ops = { void avd_job_finish_no_pm(struct avd_ctx *ctx, enum vb2_buffer_state result) { + struct avd_dev *avd = ctx->dev; + unsigned long flags; + + spin_lock_irqsave(&avd->job_lock, flags); + if (avd->job_ctx == ctx) { + avd->job_state = AVD_JOB_IDLE; + avd->job_ctx = NULL; + avd->job_pending = false; + } + spin_unlock_irqrestore(&avd->job_lock, flags); + if (ctx->coded_fmt_desc->ops->done) { struct vb2_v4l2_buffer *src_buf, *dst_buf; diff --git a/drivers/media/platform/apple/avd/avd-vp9.c b/drivers/media/platform/apple/avd/avd-vp9.c index 869c7257c5960e..4cdcb6594c4d27 100644 --- a/drivers/media/platform/apple/avd/avd-vp9.c +++ b/drivers/media/platform/apple/avd/avd-vp9.c @@ -14,9 +14,7 @@ * Alpha Lin */ -#include "linux/v4l2-controls.h" #include -#include #include #include @@ -39,9 +37,39 @@ #define VP9_LF_MODE0(v) FIELD_PREP(GENMASK(13, 7), v) #define VP9_LF_MODE1(v) FIELD_PREP(GENMASK(6, 0), v) -#define VP9_FEAT_LVL_ALT_Q(v) FIELD_PREP(GENMASK(21, 12), v) +#define VP9_FEAT_LVL_ALT_Q_EN(v) FIELD_PREP(BIT(21), !!(v)) +#define VP9_FEAT_LVL_ALT_Q(v) FIELD_PREP(GENMASK(20, 12), v) +#define VP9_FEAT_LVL_ALT_L_EN(v) FIELD_PREP(BIT(11), !!(v)) +#define VP9_FEAT_LVL_ALT_L(v) FIELD_PREP(GENMASK(10, 4), v) +#define VP9_FEAT_LVL_REF_FRAME_EN(v) FIELD_PREP(BIT(3), !!(v)) +#define VP9_FEAT_LVL_REF_FRAME(v) FIELD_PREP(GENMASK(2, 1), v) +#define VP9_FEAT_LVL_SKIP_EN(v) FIELD_PREP(BIT(0), !!(v)) +#define VP9_FEAT_LVL_SKIP(v) + +#define VP9_REF_SEL_LAST(v) FIELD_PREP(GENMASK(2, 0), v) +#define VP9_REF_BIAS_LAST(v) FIELD_PREP(BIT(3), !!(v)) +#define VP9_REF_SEL_GOLDEN(v) FIELD_PREP(GENMASK(6, 4), v) +#define VP9_REF_BIAS_GOLDEN(v) FIELD_PREP(BIT(7), !!(v)) +#define VP9_REF_SEL_ALT(v) FIELD_PREP(GENMASK(10, 8), v) +#define VP9_REF_BIAS_ALT(v) FIELD_PREP(BIT(11), !!(v)) +#define VP9_REFERENCE_MODE(v) FIELD_PREP(GENMASK(13, 12), v) +#define VP9_PARALLEL_DEC(v) FIELD_PREP(BIT(14), !!(v)) +#define VP9_REFRESH_CTX(v) FIELD_PREP(BIT(15), !!(v)) +#define VP9_INTERP_FILTER(v) FIELD_PREP(GENMASK(18, 16), v) +#define VP9_HIGH_PREC_MV(v) FIELD_PREP(BIT(19), !!(v)) +#define VP9_ERR_RES(v) FIELD_PREP(BIT(20), !!(v)) +#define VP9_HAS_REF(v) FIELD_PREP(BIT(21), !!(v)) +#define VP9_SEG_ABS(v) FIELD_PREP(BIT(22), !!(v)) +#define VP9_SEG_UDATA_TEMP(v) FIELD_PREP(BIT(23), !!(v)) +#define VP9_SEG_UPDATE_MAP(v) FIELD_PREP(BIT(24), !!(v)) +#define VP9_SEG_ENABLED(v) FIELD_PREP(BIT(25), !!(v)) +#define VP9_SEG_RESET(v) FIELD_PREP(BIT(26), !!(v)) #define VP9_MAX_TILE_COLS (1 << 4) +#define VP9_REF_SCALE_SHIFT 14 +#define VP9_LAST_FRAME 1 +#define VP9_GOLDEN_FRAME 2 +#define VP9_ALTREF_FRAME 3 struct avd_vp9_seg_probs { u8 tree_probs[7]; @@ -149,13 +177,13 @@ struct avd_vp9_ctx { struct v4l2_vp9_frame_context frame_context[4]; struct avd_vp9_bufs { struct avd_buf inst; - /* only affect tiles */ - struct avd_buf tiles[3]; - /* just a guess (this name exists in tunables) */ + struct avd_buf az_left; struct avd_buf above_info; - /* if above is true, state and color are left */ - struct avd_buf state; - struct avd_buf color[2]; + struct avd_buf ip_above; + struct avd_buf lf_above; + struct avd_buf lf_left_info; + struct avd_buf lf_left; + struct avd_buf color; struct avd_buf seg; struct avd_buf pipe_state; struct avd_buf counts; @@ -169,9 +197,9 @@ struct avd_vp9_ctx { static void set_refs(struct avd_ctx *ctx, struct avd_vp9_run *run) { const struct v4l2_ctrl_vp9_frame *frame = run->decode_params; - struct avd_dev *avd = ctx->dev; struct avd_decoded_buffer *dst, *ref_buf[4]; dma_addr_t addr; + int xscale, yscale; dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); @@ -189,63 +217,89 @@ static void set_refs(struct avd_ctx *ctx, struct avd_vp9_run *run) &ref_buf[i]->base.vb.vb2_buf, 0) + ref_buf[i]->comp.start_offset; - /* TODO */ push(AVD_REF_FLAG_CONST, "hdr_9c_ref_100"); push(AVD_HDR_HEIGHT(ref_buf[i]->vp9.height - 1) | AVD_HDR_WIDTH(ref_buf[i]->vp9.width - 1), "hdr_70_ref_height_width"); - push(0x40004000, "hdr_7c_ref_align"); - push_comp(avd, ctx, addr, ref_buf[i]->comp.offsets); + xscale = (ref_buf[i]->vp9.width << VP9_REF_SCALE_SHIFT) / + dst->vp9.width; + yscale = (ref_buf[i]->vp9.height << VP9_REF_SCALE_SHIFT) / + dst->vp9.height; + push(AVD_HDR_HEIGHT(xscale) | AVD_HDR_WIDTH(yscale), + "ref_scale"); + + push_comp(ctx, addr, ref_buf[i]->comp.offsets); } } -/* TODO */ static u32 make_flags1(struct avd_ctx *ctx, struct avd_vp9_run *run) { struct avd_vp9_ctx *vp9_ctx = ctx->priv; const struct v4l2_ctrl_vp9_frame *frame = run->decode_params; - bool has_ref = boolify( + struct avd_decoded_buffer *dst, *last, *golden, *alt; + + dst = vb2_to_avd_decoded_buf(&run->base.bufs.dst->vb2_buf); + last = avd_get_ref_buf(ctx, &dst->base.vb, frame->last_frame_ts); + golden = avd_get_ref_buf(ctx, &dst->base.vb, frame->golden_frame_ts); + alt = avd_get_ref_buf(ctx, &dst->base.vb, frame->alt_frame_ts); + + /* + * i have only seen V4L2_VP9_SIGN_BIAS_ALT, im simply guessing the + * same applies for last and golden + */ + u32 flags = VP9_REF_SEL_LAST(VP9_LAST_FRAME); + flags |= VP9_REF_BIAS_LAST(frame->ref_frame_sign_bias & + V4L2_VP9_SIGN_BIAS_LAST); + flags |= VP9_REF_SEL_GOLDEN(golden == last ? VP9_LAST_FRAME : + VP9_GOLDEN_FRAME); + flags |= VP9_REF_BIAS_GOLDEN(frame->ref_frame_sign_bias & + V4L2_VP9_SIGN_BIAS_GOLDEN); + flags |= VP9_REF_SEL_ALT(alt != golden ? VP9_ALTREF_FRAME : + alt != last ? VP9_GOLDEN_FRAME : + VP9_LAST_FRAME); + flags |= VP9_REF_BIAS_ALT(frame->ref_frame_sign_bias & + V4L2_VP9_SIGN_BIAS_ALT); + + flags |= VP9_REFERENCE_MODE(frame->reference_mode); + + flags |= VP9_PARALLEL_DEC(frame->flags & + V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE); + flags |= VP9_REFRESH_CTX(frame->flags & + V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX); + flags |= VP9_INTERP_FILTER(frame->interpolation_filter); + flags |= VP9_HIGH_PREC_MV(frame->flags & + V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV); + + flags |= + VP9_ERR_RES(frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT); + + flags |= VP9_SEG_ABS(frame->seg.flags & + V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE); + flags |= VP9_SEG_UDATA_TEMP( + frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA && + frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE); + flags |= VP9_SEG_UPDATE_MAP(frame->seg.flags & + V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP); + flags |= VP9_SEG_ENABLED(frame->seg.flags & + V4L2_VP9_SEGMENTATION_FLAG_ENABLED); + + flags |= VP9_HAS_REF( + !(frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) && + !(frame->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) && vp9_ctx->last.valid && - !(vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME) && - vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_SHOW_FRAME); - - u32 flags = - BIT(0) | - boolify(frame->flags & V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE) - << 14 | - !boolify(frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT) - << 15 | - boolify(frame->flags & V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV) - << 19; - - if (!(frame->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME)) { - flags |= frame->interpolation_filter << 16; - if (!(frame->flags & V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT)) - flags |= has_ref << 21; - else - flags |= BIT(20); - } + vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_SHOW_FRAME && + !(vp9_ctx->last.flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME)); + + flags |= VP9_SEG_RESET( + frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED && + (frame->flags & V4L2_VP9_FRAME_FLAG_KEY_FRAME || + (vp9_ctx->last.valid && + !(vp9_ctx->last.seg.flags & + V4L2_VP9_SEGMENTATION_FLAG_ENABLED) && + vp9_ctx->last.flags & (V4L2_VP9_FRAME_FLAG_KEY_FRAME | + V4L2_VP9_FRAME_FLAG_INTRA_ONLY)))); - flags |= frame->ref_frame_sign_bias << 7; - /* this seems wrong */ - flags |= !(frame->golden_frame_ts == 0) << 10; - flags |= !(frame->ref_frame_sign_bias == 0) << 11; - - flags |= frame->reference_mode << 12; - - flags |= !!(frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP) - << 24 | - !!(frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_ENABLED) - << 25; - /* what?? */ - if (frame->seg.flags & V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA) { - if (frame->seg.flags & - V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE) - flags |= BIT(23); - else if (!vp9_ctx->last.valid) - flags |= BIT(26); - } return flags; } @@ -254,19 +308,30 @@ static u32 seg_features(struct avd_ctx *ctx, struct avd_vp9_run *run, { struct avd_vp9_ctx *vp9_ctx = ctx->priv; const struct v4l2_vp9_segmentation *seg = &vp9_ctx->cur.seg; - s16 feature_val = 0; + u32 feature_val = 0; int feature_id = 0; - u32 enabled = 0; feature_id = V4L2_VP9_SEG_LVL_ALT_Q; - if (v4l2_vp9_seg_feat_enabled(seg->feature_enabled, feature_id, segid)) - feature_val = seg->feature_data[segid][feature_id]; + feature_val |= VP9_FEAT_LVL_ALT_Q_EN(v4l2_vp9_seg_feat_enabled( + seg->feature_enabled, feature_id, segid)); + feature_val |= VP9_FEAT_LVL_ALT_Q(seg->feature_data[segid][feature_id]); + + feature_id = V4L2_VP9_SEG_LVL_ALT_L; + feature_val |= VP9_FEAT_LVL_ALT_L_EN(v4l2_vp9_seg_feat_enabled( + seg->feature_enabled, feature_id, segid)); + feature_val |= VP9_FEAT_LVL_ALT_L(seg->feature_data[segid][feature_id]); + + feature_id = V4L2_VP9_SEG_LVL_REF_FRAME; + feature_val |= VP9_FEAT_LVL_REF_FRAME_EN(v4l2_vp9_seg_feat_enabled( + seg->feature_enabled, feature_id, segid)); + feature_val |= + VP9_FEAT_LVL_REF_FRAME(seg->feature_data[segid][feature_id]); feature_id = V4L2_VP9_SEG_LVL_SKIP; - if (v4l2_vp9_seg_feat_enabled(seg->feature_enabled, feature_id, segid)) - enabled |= 1; + feature_val |= VP9_FEAT_LVL_SKIP_EN(v4l2_vp9_seg_feat_enabled( + seg->feature_enabled, feature_id, segid)); - return VP9_FEAT_LVL_ALT_Q(feature_val) | enabled; + return feature_val; } static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) @@ -281,14 +346,6 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) bool intra_only = !!(frame->flags & (V4L2_VP9_FRAME_FLAG_KEY_FRAME | V4L2_VP9_FRAME_FLAG_INTRA_ONLY)); - push(AVD_OP_EXEC | - AVD_OP_EXEC_FLAG_START_REV3(avd->variant->revision == 3) | - AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == 4) | - (avd->variant->revision == 3 ? AVD_OP_EXEC_REV3_VP9_MASK : - 0) | - AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx), - "inst_fifo_start"); - push(AVD_OP_HDR | AVD_OP_HDR_FLAG_DECOMP(ctx->decomp) | AVD_OP_HDR_FLAG_INTRA(intra_only) | AVD_OP_HDR_CONST | AVD_OP_HDR_FLAG_PIPE_STATE( @@ -324,18 +381,13 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) push(0, ""); push(0, ""); - pusha(vp9_ctx->bufs.counts.addr, "frame_counts_addr", 0); - pusha(vp9_ctx->bufs.probs.addr, "hdr_104_probs_addr_lsb8", 0); - - /* always used */ - pusha(vp9_ctx->bufs.state.addr, "hdr_118_pps0_tile_addr_lsb8", 0); - - /* read / write segment buffers */ - pusha(vp9_ctx->bufs.seg.addr, "hdr_108_pps1_tile_addr_lsb8", 1); - pusha(vp9_ctx->bufs.seg.addr, "hdr_108_pps1_tile_addr_lsb8", 2); - /* ping pong buffers, not on intra frames (how apple uses them) */ - pusha(vp9_ctx->bufs.above_info.addr, "hdr_110_pps2_tile_addr_lsb8", 3); - pusha(vp9_ctx->bufs.above_info.addr, "hdr_110_pps2_tile_addr_lsb8", 4); + pusha(vp9_ctx->bufs.counts.addr, "counts", 0); + pusha(vp9_ctx->bufs.probs.addr, "probs", 0); + pusha(vp9_ctx->bufs.above_info.addr, "above_info", 0); + pusha(vp9_ctx->bufs.seg.addr, "seg", 1); + pusha(vp9_ctx->bufs.seg.addr, "seg", 2); + pusha(vp9_ctx->bufs.color.addr, "color", 3); + pusha(vp9_ctx->bufs.color.addr, "color", 4); push(VP9_Q_IDX(frame->quant.base_q_idx) | VP9_Q_DC_Y(frame->quant.delta_q_y_dc) | @@ -369,23 +421,19 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) if (!(avd->variant->quirks & AVD_QUIRK_NO_PIPE_STATE)) pusha(vp9_ctx->bufs.pipe_state.addr, "pipe_state", 0); - pusha(vp9_ctx->bufs.color[0].addr, "hdr_e8_sps0_tile_addr_lsb8", 0); - pusha(vp9_ctx->bufs.color[1].addr, "hdr_e8_sps0_tile_addr_lsb8", 0); - + pusha(vp9_ctx->bufs.ip_above.addr, "ip_above", 0); + pusha(vp9_ctx->bufs.lf_above.addr, "lf_above", 0); + /* no lf_above_info? */ pusha((u64)0, "hdr_e8_sps0_tile_addr_lsb8", 0); - - /* not fatal */ - pusha(vp9_ctx->bufs.tiles[0].addr, "hdr_e8_sps0_tile_addr_lsb8", 0); - pusha(vp9_ctx->bufs.tiles[1].addr, "hdr_e8_sps0_tile_addr_lsb8", 0); - /* fatal if missing / wrong */ - pusha(vp9_ctx->bufs.tiles[2].addr, "hdr_e8_sps0_tile_addr_lsb8", 0); + pusha(vp9_ctx->bufs.lf_left.addr, "lf_left", 0); + pusha(vp9_ctx->bufs.lf_left_info.addr, "lf_left_info", 0); + pusha(vp9_ctx->bufs.az_left.addr, "az_left", 0); push(0, ""); - push_comp(avd, ctx, run->base.comp_out, ctx->comp.offsets); + push_comp(ctx, run->base.comp_out, ctx->comp.offsets); - /* confusing */ - pusha((u64)0, "hdr_f4_sps1_tile_addr_lsb8", 2); + pusha((u64)0, "packed_fmt_scratch", 0); bytesperline = ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].bytesperline; if (avd->variant->quirks & AVD_QUIRK_LSR) @@ -407,11 +455,9 @@ static void set_header(struct avd_ctx *ctx, struct avd_vp9_run *run) static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) { const struct v4l2_ctrl_vp9_frame *frame = run->decode_params; - struct avd_dev *avd = ctx->dev; struct avd_vp9_ctx *vp9_ctx = ctx->priv; struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); - bool is_last; u32 offset = frame->uncompressed_header_size + frame->compressed_header_size; @@ -426,8 +472,7 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) for (int row = 0; row < num_tile_rows; row++) for (int col = 0; col < num_tile_cols; col++) { - is_last = row == num_tile_rows - 1 && - col == num_tile_cols - 1; + ctx->job.num++; if (row == num_tile_rows - 1 && col == num_tile_cols - 1) { tile_size = size; @@ -459,11 +504,7 @@ static void set_tiles(struct avd_ctx *ctx, struct avd_vp9_run *run) AVD_SL_DIM_END_X(((col + 1) * sb_64_cols) / num_tile_cols - 1), "til_ac0_tile_dims"); - push(AVD_OP_EXEC | AVD_OP_EXEC_FLAG_END(is_last) | - (avd->variant->revision == 3 ? - AVD_OP_EXEC_REV3_VP9_MASK : - 0), - "cm3_cmd_inst_fifo_end"); + offset += tile_size; size -= tile_size; vp9_ctx->submit_num++; @@ -642,46 +683,42 @@ static int avd_vp9_alloc_bufs(struct avd_ctx *ctx) if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.above_info, + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.color, DIV_ROUND_UP(w, 16) * DIV_ROUND_UP(h, 64) * 144); if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.color[0], + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.ip_above, DIV_ROUND_UP(w, 16) * 4 * bit_depth + (VP9_MAX_TILE_COLS - 1) * 128); if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.color[1], + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.lf_above, DIV_ROUND_UP(w, 8) * 16 * bit_depth); if (ret) return ret; - /* - * randomly needs more space when resizing? - * maybe it does not need it? - */ ret = avd_buf_alloc(avd, &vp9_ctx->bufs.seg, DIV_ROUND_UP(w, 8) * 24); if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.tiles[0], + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.lf_left, DIV_ROUND_UP(h, 8) * 16 * bit_depth); if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.tiles[1], + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.lf_left_info, DIV_ROUND_UP(h, 64) * 16); if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.tiles[2], + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.az_left, bit_depth * 36 * DIV_ROUND_UP(h, 8) + bit_depth * 18 * DIV_ROUND_UP(h, 16)); if (ret) return ret; - ret = avd_buf_alloc(avd, &vp9_ctx->bufs.state, + ret = avd_buf_alloc(avd, &vp9_ctx->bufs.above_info, DIV_ROUND_UP(w, 64) * 288 + (VP9_MAX_TILE_COLS - 1) * 128); if (ret) @@ -731,7 +768,6 @@ static int avd_vp9_run_preamble(struct avd_ctx *ctx, struct avd_vp9_run *run) static int avd_vp9_run(struct avd_ctx *ctx) { - struct avd_dev *avd = ctx->dev; struct avd_vp9_run run; struct avd_vp9_ctx *vp9_ctx; struct avd_decoded_buffer *dst; @@ -743,6 +779,16 @@ static int avd_vp9_run(struct avd_ctx *ctx) return ret; } + ret = avd_init_job( + ctx, AVD_CODEC_VP9, + (1 << run.decode_params->tile_rows_log2) * + (1 << run.decode_params->tile_cols_log2) + + 1); + if (ret) { + avd_run_postamble(ctx, &run.base); + return ret; + } + init_probs(ctx, &run); vp9_ctx = ctx->priv; @@ -750,24 +796,12 @@ static int avd_vp9_run(struct avd_ctx *ctx) update_dec_buf_info(dst, run.decode_params); update_ctx_cur_info(vp9_ctx, dst, run.decode_params); - ret = alloc_slots(avd, ctx, AVD_CODEC_VP9); - if (ret) { - dev_err(avd->dev, "no free slots: %d", ret); - return ret; - } - - schedule_delayed_work(&ctx->watchdog_work, msecs_to_jiffies(2000)); - - avd->variant->configure_stream(avd, vp9_ctx->bufs.inst.addr, - ctx->fifo_idx, ctx->vp_slot); - set_header(ctx, &run); - vp9_ctx->submit_num = 0; set_tiles(ctx, &run); avd_run_postamble(ctx, &run.base); - return 0; + return avd_submit_job(ctx); } #define copy_tx_and_skip(p1, p2) \ @@ -947,6 +981,28 @@ avd_init_v4l2_vp9_count_tbl(struct avd_ctx *ctx) } } +static void avd_vp9_stop(struct avd_ctx *ctx) +{ + struct avd_vp9_ctx *vp9_ctx = ctx->priv; + struct avd_dev *avd = ctx->dev; + + avd_buf_free(avd, &vp9_ctx->bufs.pipe_state); + avd_buf_free(avd, &vp9_ctx->bufs.inst); + avd_buf_free(avd, &vp9_ctx->bufs.probs); + avd_buf_free(avd, &vp9_ctx->bufs.counts); + avd_buf_free(avd, &vp9_ctx->bufs.seg); + avd_buf_free(avd, &vp9_ctx->bufs.color); + avd_buf_free(avd, &vp9_ctx->bufs.ip_above); + avd_buf_free(avd, &vp9_ctx->bufs.lf_above); + avd_buf_free(avd, &vp9_ctx->bufs.above_info); + avd_buf_free(avd, &vp9_ctx->bufs.az_left); + avd_buf_free(avd, &vp9_ctx->bufs.lf_left_info); + avd_buf_free(avd, &vp9_ctx->bufs.lf_left); + + kfree(vp9_ctx); + ctx->priv = NULL; +} + static int avd_vp9_start(struct avd_ctx *ctx) { struct avd_vp9_ctx *vp9_ctx; @@ -966,31 +1022,10 @@ static int avd_vp9_start(struct avd_ctx *ctx) return 0; err_free_ctx: - kfree(vp9_ctx); - ctx->priv = NULL; + avd_vp9_stop(ctx); return ret; } -static void avd_vp9_stop(struct avd_ctx *ctx) -{ - struct avd_vp9_ctx *vp9_ctx = ctx->priv; - struct avd_dev *avd = ctx->dev; - - avd_buf_free(avd, &vp9_ctx->bufs.pipe_state); - avd_buf_free(avd, &vp9_ctx->bufs.inst); - avd_buf_free(avd, &vp9_ctx->bufs.probs); - avd_buf_free(avd, &vp9_ctx->bufs.counts); - avd_buf_free(avd, &vp9_ctx->bufs.seg); - avd_buf_free(avd, &vp9_ctx->bufs.above_info); - avd_buf_free(avd, &vp9_ctx->bufs.color[0]); - avd_buf_free(avd, &vp9_ctx->bufs.color[1]); - avd_buf_free(avd, &vp9_ctx->bufs.state); - for (int i = 0; i < 3; i++) - avd_buf_free(avd, &vp9_ctx->bufs.tiles[i]); - - kfree(vp9_ctx); -} - static enum avd_image_fmt avd_vp9_get_image_fmt(struct avd_ctx *ctx, struct v4l2_ctrl *ctrl) { @@ -1018,15 +1053,22 @@ static void avd_vp9_submit(struct avd_ctx *ctx) { struct avd_vp9_ctx *vp9_ctx = ctx->priv; struct avd_dev *avd = ctx->dev; - u32 submit_mask = ctx->dev->variant->revision == 3 ? 0xfff000 : 0; - - writel(0x2b000000 | submit_mask | - (avd->variant->revision == 3 ? 0x100 : 0x200) | - (ctx->fifo_idx << 4) | avd->variant->fifo_slots, + u32 submit_mask = ctx->dev->variant->revision == 3 ? + AVD_OP_EXEC_REV3_VP9_MASK : + 0; + + writel(AVD_OP_EXEC | submit_mask | + AVD_OP_EXEC_FLAG_START_REV4(avd->variant->revision == + 4) | + AVD_OP_EXEC_FLAG_START_REV3(avd->variant->revision == + 3) | + AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | + AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), avd->ctrl + avd->variant->submit_offset); for (int i = 0; i < vp9_ctx->submit_num - 1; i++) - writel(0x2b000000 | submit_mask | (ctx->fifo_idx << 4) | - avd->variant->fifo_slots, + writel(AVD_OP_EXEC | submit_mask | + AVD_OP_EXEC_FIFO_IDX(ctx->fifo_idx) | + AVD_OP_EXEC_FIFO_MASK(avd->variant->fifo_slots), avd->ctrl + avd->variant->submit_offset); } diff --git a/drivers/media/platform/apple/avd/avd.h b/drivers/media/platform/apple/avd/avd.h index 92252934cb0d5a..38c98f94a7f74c 100644 --- a/drivers/media/platform/apple/avd/avd.h +++ b/drivers/media/platform/apple/avd/avd.h @@ -14,10 +14,10 @@ #ifndef AVD_H_ #define AVD_H_ -#include "linux/bitmap.h" #include #include #include +#include #include #include @@ -36,9 +36,14 @@ #define VP_SLOT_NONE 255 #define INST_FIFO_SLOT_NONE 255 -/* AVD needs most addresses to be aligned to 256 */ +/* + * AVD needs most addresses to be aligned to 256 + * the only exception are the compressed buffers, they are aligned to 128 + * instead + */ #define AVD_ALIGN 256 - +/* hevc, with a b slice where all references are active and weights are sent */ +#define AVD_MAX_INST 512 struct avd_ctx; struct avd_dev; @@ -89,7 +94,7 @@ struct avd_av1_decoded_buffer_info { u32 order_hints[V4L2_AV1_TOTAL_REFS_PER_FRAME]; u8 ref_frame_idx[V4L2_AV1_REFS_PER_FRAME]; bool intrabc; - size_t priv_tlb_size; + size_t color_size; }; struct avd_hevc_decoded_buffer_info { @@ -97,7 +102,6 @@ struct avd_hevc_decoded_buffer_info { bool is_intra; }; - struct avd_comp { u32 size; /* offset to start of compressed data */ @@ -111,13 +115,15 @@ struct avd_decoded_buffer { struct v4l2_m2m_buffer base; struct avd_comp comp; + /* Pending CPU fill, retained with the buffer until dequeue. */ + size_t grey_chroma_offset; + bool grey_chroma_10bit; union { struct avd_vp9_decoded_buffer_info vp9; struct avd_hevc_decoded_buffer_info hevc; struct avd_av1_decoded_buffer_info av1; }; - }; static inline struct avd_decoded_buffer * @@ -184,6 +190,14 @@ struct avd_variant { unsigned int quirks; }; +/* Lifecycle of the one m2m job the device runs at a time. */ +enum avd_job_state { + AVD_JOB_IDLE, + AVD_JOB_RUNNING, /* inside avd_device_run() */ + AVD_JOB_SUBMITTED, /* handed to the hardware, watchdog armed */ + AVD_JOB_FINISHING, /* claimed by the IRQ handler or the watchdog */ +}; + struct avd_dev { struct device *dev; struct v4l2_device v4l2_dev; @@ -203,14 +217,37 @@ struct avd_dev { struct mutex vdev_lock; - struct reset_control *rstc; + /* The current job; see avd_device_run(). Protected by job_lock. */ + spinlock_t job_lock; + enum avd_job_state job_state; + struct avd_ctx *job_ctx; + u64 job_seq; + bool job_end_sent; + bool job_pending; + enum vb2_buffer_state job_pending_result; - unsigned long vp_slots; - unsigned long inst_fifo_slots; + struct reset_control *rstc; const struct avd_variant *variant; }; +struct avd_segment { + size_t num; + u32 instructions[AVD_MAX_INST]; +}; + +struct avd_job { + enum avd_codec codec; + size_t num; + struct avd_segment *segments; +}; + +struct avd_buf { + void *cpu; + dma_addr_t addr; + size_t size; +}; + struct avd_ctx { struct v4l2_fh fh; struct avd_dev *dev; @@ -224,20 +261,19 @@ struct avd_ctx { bool decomp; struct delayed_work watchdog_work; + /* job_seq of the job watchdog_work was armed for */ + u64 wd_seq; void *priv; struct avd_comp comp; - - u8 fifo_idx; - u8 vp_slot; + int fifo_idx; + struct avd_job job; + struct avd_buf inst; }; -struct avd_buf { - void *cpu; - dma_addr_t addr; - size_t size; -}; +int avd_init_job(struct avd_ctx *ctx, enum avd_codec codec, size_t segments); +int avd_submit_job(struct avd_ctx *ctx); int avd_buf_alloc(struct avd_dev *avd, struct avd_buf *buf, size_t size); void avd_buf_free(struct avd_dev *avd, struct avd_buf *buf); @@ -272,19 +308,6 @@ static inline u32 fmt_width(struct avd_ctx *ctx) void fill_comp(struct avd_comp *comp, enum avd_image_fmt image_fmt, u32 width, u32 height); -int alloc_slots(struct avd_dev *avd, struct avd_ctx *ctx, enum avd_codec codec); - -static inline void free_vp_slot(struct avd_dev *avd, struct avd_ctx *ctx) -{ - clear_bit(ctx->vp_slot, &avd->vp_slots); - ctx->vp_slot = VP_SLOT_NONE; -} - -static inline void free_inst_slot(struct avd_dev *avd, struct avd_ctx *ctx) -{ - clear_bit(ctx->fifo_idx, &avd->inst_fifo_slots); - ctx->fifo_idx = INST_FIFO_SLOT_NONE; -} static inline struct avd_ctx *file_to_ctx(struct file *filp) {