Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions Documentation/devicetree/bindings/media/apple,t8010-ave.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
%YAML 1.2
---
$id: http://devicetree.org/schemas/media/apple,t8010-ave.yaml#
$schema: http://devicetree.org/meta-schemas/core.yaml#

title: Apple A10 video encoder (AVE2)

maintainers:
- Paul Scalise <paulscalise1@users.noreply.github.com>

description:
The A10's H.264 and HEVC video encoder. Apple's firmware runs on a 32-bit
ARM CPU of the block's own and drives the encoder core. The host loads the
firmware, talks to it through a mailbox in the CPU's control block, and
maps its memory through the block's DART.

properties:
compatible:
const: apple,t8010-ave

reg:
items:
- description: the CPU's control block, with the host side of the
mailbox
- description: the translation unit in front of the DART
- description: the PMGR page with the encoder pipes' power states,
which the firmware gets mapped through the DART

reg-names:
items:
- const: cpu
- const: smmu
- const: pmgr

interrupts:
maxItems: 1

power-domains:
maxItems: 5

power-domain-names:
items:
- const: sys
- const: pipe4
- const: pipe5
- const: me0
- const: me1

iommus:
maxItems: 1

firmware-name:
maxItems: 1

required:
- compatible
- reg
- reg-names
- interrupts
- power-domains
- power-domain-names
- iommus
- firmware-name

additionalProperties: false

examples:
- |
#include <dt-bindings/interrupt-controller/apple-aic.h>
#include <dt-bindings/interrupt-controller/irq.h>

soc {
#address-cells = <2>;
#size-cells = <2>;

video-encoder@207c80000 {
compatible = "apple,t8010-ave";
reg = <0x2 0x07c80000 0x0 0x80000>,
<0x2 0x07c20000 0x0 0x4000>,
<0x2 0x0e088000 0x0 0x4000>;
reg-names = "cpu", "smmu", "pmgr";
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 181 IRQ_TYPE_LEVEL_HIGH>;
power-domains = <&ps_venc_sys>, <&ps_venc_pipe4>, <&ps_venc_pipe5>,
<&ps_venc_me0>, <&ps_venc_me1>;
power-domain-names = "sys", "pipe4", "pipe5", "me0", "me1";
iommus = <&dart_ave 0>;
firmware-name = "apple/ave-t8010.bin";
};
};
32 changes: 32 additions & 0 deletions arch/arm64/boot/dts/apple/t8010.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,38 @@
iommus = <&dart_jpeg1 0>;
};

dart_ave: iommu@207c30000 {
compatible = "apple,t8010-dart", "apple,s5l8960x-dart";
reg = <0x2 0x07c30000 0x0 0x4000>;
#iommu-cells = <1>;
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 179 IRQ_TYPE_LEVEL_HIGH>;
power-domains = <&ps_venc_sys>;
};

/*
* The video encoder: Apple's firmware on a 32-bit ARM CPU of its
* own drives the encoder core, which the AP never touches. "cpu"
* is the CPU's control block, with the AP's side of the mailbox at
* +0x40000, "smmu" the unit in front of dart-ave, and "pmgr" the
* PMGR page with the pipes' power states, which the firmware gets
* mapped through the DART.
*/
ave: video-encoder@207c80000 {
compatible = "apple,t8010-ave";
reg = <0x2 0x07c80000 0x0 0x80000>,
<0x2 0x07c20000 0x0 0x4000>,
<0x2 0x0e088000 0x0 0x4000>;
reg-names = "cpu", "smmu", "pmgr";
interrupt-parent = <&aic>;
interrupts = <AIC_IRQ 181 IRQ_TYPE_LEVEL_HIGH>;
power-domains = <&ps_venc_sys>, <&ps_venc_pipe4>, <&ps_venc_pipe5>,
<&ps_venc_me0>, <&ps_venc_me1>;
power-domain-names = "sys", "pipe4", "pipe5", "me0", "me1";
iommus = <&dart_ave 0>;
firmware-name = "apple/ave-t8010.bin";
};

sio: dma-controller@20ae00000 {
compatible = "apple,t8010-sio";
reg = <0x2 0x0ae00000 0x0 0x10000>,
Expand Down
1 change: 1 addition & 0 deletions arch/arm64/configs/orchard_apple16k_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ CONFIG_MEDIA_TEST_SUPPORT=y
CONFIG_VIDEO_APPLE_T8010_ISP=m
CONFIG_VIDEO_APPLE_VXD=m
CONFIG_VIDEO_APPLE_JPEG=m
CONFIG_VIDEO_APPLE_AVE=m
CONFIG_DRM=y
CONFIG_DRM_APPLE_ADBE=y
CONFIG_DRM_ADP=y
Expand Down
1 change: 1 addition & 0 deletions drivers/media/platform/apple/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ comment "Apple media platform drivers"
source "drivers/media/platform/apple/isp/Kconfig"
source "drivers/media/platform/apple/vxd/Kconfig"
source "drivers/media/platform/apple/jpeg/Kconfig"
source "drivers/media/platform/apple/ave/Kconfig"
1 change: 1 addition & 0 deletions drivers/media/platform/apple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
obj-y += isp/
obj-y += vxd/
obj-y += jpeg/
obj-y += ave/
14 changes: 14 additions & 0 deletions drivers/media/platform/apple/ave/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0-only
config VIDEO_APPLE_AVE
tristate "Apple T8010 video encoder (AVE2)"
depends on VIDEO_DEV && IOMMU_API
depends on ARCH_APPLE || COMPILE_TEST
select VIDEOBUF2_DMA_CONTIG
select V4L2_MEM2MEM_DEV
help
V4L2 stateful H.264 and HEVC encoder for the Apple A10's video
encoder, whose firmware (apple/ave-t8010.bin) runs on a CPU of its
own.

To compile this driver as a module, choose M here: the module will be
called apple-ave.
3 changes: 3 additions & 0 deletions drivers/media/platform/apple/ave/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_VIDEO_APPLE_AVE) += apple-ave.o
apple-ave-y := ave_drv.o ave_hw.o ave_v4l2.o
151 changes: 151 additions & 0 deletions drivers/media/platform/apple/ave/ave.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
/*
* Apple T8010 video encoder (AVE2): a V4L2 stateful encoder, H.264 and HEVC
* from NV12.
*/
#ifndef AVE_H
#define AVE_H

#include <linux/interrupt.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/spinlock.h>
#include <linux/wait.h>
#include <linux/workqueue.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-v4l2.h>

#define AVE_MIN_W 192 /* the firmware's H.264 limits */
#define AVE_MIN_H 96
#define AVE_MIN_W_HEVC 160 /* and HEVC's (iOS's checks) */
#define AVE_MIN_H_HEVC 64
#define AVE_MAX_W 4096 /* iOS: H.264 and HEVC 4K30, 1080p60 */
#define AVE_MAX_H 2304
#define AVE_STRIDE_ALIGN 64 /* source lines */
#define AVE_MAX_STRIDE 8192
#define AVE_TIMEOUT_MS 2000 /* one picture */
#define AVE_MAX_B 2 /* B pictures between two anchors */
#define AVE_MAX_INFLIGHT 2 /* pictures the firmware has at once */

/* what a picture is coded as */
enum ave_pic_type {
AVE_PIC_IDR,
AVE_PIC_P,
AVE_PIC_B, /* not a reference */
};

struct ave_hw;

struct ave_dev {
struct device *dev;
struct v4l2_device v4l2;
struct video_device vdev;
struct v4l2_m2m_dev *m2m;
struct mutex lock; /* the video device's ioctls */
struct ave_hw *hw; /* ave_hw.c's state */
spinlock_t irq_lock; /* running */
struct ave_ctx *running; /* the context whose pictures are encoding */
struct mutex run_lock; /* posting pictures and completing them */
struct delayed_work watchdog;
};

/* what a stream is encoded with: latched from the formats and controls when it starts */
struct ave_params {
u32 codec; /* V4L2_PIX_FMT_H264 or _HEVC */
u32 width, height; /* the source frame */
u32 stride; /* its bytes per line (both planes) */
bool src_ten; /* P010 (10-bit) rather than NV12; HEVC only */
struct v4l2_rect crop; /* what is coded as visible */
u32 fps_num, fps_den; /* frames per second: num / den */
u32 rc_mode; /* V4L2_MPEG_VIDEO_BITRATE_MODE_* */
u32 bitrate; /* bits per second (VBR, CBR) */
u32 bitrate_peak; /* VBR peak */
u32 qp_i, qp_p, qp_b; /* constant QP (CQ) */
u32 qp_min, qp_max;
bool aq; /* adaptive quantization: a QP per 16x16 (not with CQ) */
u32 gop; /* frames from IDR to IDR (0: only the first) */
u32 bframes; /* B pictures between anchors (0..AVE_MAX_B) */
u32 slice_mbs; /* at most this many macroblocks a slice (0: one slice) */
u32 profile, level; /* V4L2 enums of the codec */
u32 tier; /* HEVC: V4L2_MPEG_VIDEO_HEVC_TIER_* */
bool cabac; /* H.264 entropy coding */
bool headers_separate; /* SPS/PPS (VPS) in a buffer of their own */
bool headers_every_idr; /* SPS/PPS (VPS) before every IDR picture */
/* colorimetry for the VUI */
u8 colorspace, xfer_func, ycbcr_enc, quantization;
};

struct ave_ctx {
struct v4l2_fh fh;
struct ave_dev *ave;
struct v4l2_ctrl_handler ctrls;
struct v4l2_ctrl *ctrl_bitrate;
struct v4l2_ctrl *ctrl_min_out; /* MIN_BUFFERS_FOR_OUTPUT: 1 + B pictures */
struct v4l2_pix_format_mplane src_fmt; /* OUTPUT: raw NV12 */
struct v4l2_pix_format_mplane dst_fmt; /* CAPTURE: H.264 / HEVC */
struct v4l2_rect crop; /* the visible part of the source */
u32 fps_num, fps_den; /* S_PARM: frames per second num / den */
struct ave_params params;
void *session; /* ave_hw.c's per-stream state, NULL when stopped */
u32 out_seq, cap_seq;
u32 frame_num; /* pictures posted since the stream started */
bool force_idr; /* FORCE_KEY_FRAME, or the source restarted */
bool headers_sent; /* the separate header buffer went out */
bool cur_headers; /* the job in flight writes only the headers */
/*
* Coding order. Source frames are taken in groups: the group's last
* frame (its anchor, I or P) is coded first, then the frames before
* it as B pictures, @held in display order. @disp numbers the frames
* in display order from the last IDR picture.
*/
spinlock_t sched_lock; /* the fields below: job_ready() reads them */
struct vb2_v4l2_buffer *held[AVE_MAX_B];
unsigned int n_held, next_held;
u32 disp; /* of the next frame not in a group yet */
u32 held_disp; /* of held[0] */
/*
* Pictures in flight: their source frames in @flight, in coding order
* (they stay queued until done, so draining works as for one); their
* capture buffers are the first @posted of that queue. The mem2mem
* job is held until the oldest is done.
*/
struct vb2_v4l2_buffer *flight[AVE_MAX_INFLIGHT];
unsigned int posted;
u32 drain_seq; /* the sequence of the last frame before ENC_CMD_STOP */
bool job_held;
wait_queue_head_t idle; /* posted went to 0 */
};

static inline struct ave_ctx *ave_fh_to_ctx(struct v4l2_fh *fh)
{
return container_of(fh, struct ave_ctx, fh);
}

/*
* The hardware side (ave_hw.c). ave_hw_encode() starts one picture into the
* capture buffer and returns; the firmware may have up to
* ave_hw_max_inflight() of them, finishes them in order, and each answer
* ends the oldest with ave_frame_done(). The session calls sleep; they are
* serialised by the mem2mem core (one job at a time), run_lock and ave->lock.
*/
int ave_hw_init(struct ave_dev *ave, struct platform_device *pdev);
void ave_hw_remove(struct ave_dev *ave);
void ave_hw_suspend(struct ave_dev *ave);
int ave_hw_session_start(struct ave_ctx *ctx);
void ave_hw_session_stop(struct ave_ctx *ctx);
int ave_hw_headers(struct ave_ctx *ctx, struct vb2_v4l2_buffer *dst, u32 *bytes);
int ave_hw_encode(struct ave_ctx *ctx, struct vb2_v4l2_buffer *src,
struct vb2_v4l2_buffer *dst, enum ave_pic_type type, u32 disp);
unsigned int ave_hw_max_inflight(const struct ave_ctx *ctx);
int ave_hw_set_bitrate(struct ave_ctx *ctx, u32 bitrate);
void ave_hw_timeout(struct ave_dev *ave);

/* a picture is done: its bytes in the capture buffer (ok false: failed) */
void ave_frame_done(struct ave_dev *ave, u32 bytes, enum ave_pic_type type, bool ok);

int ave_v4l2_register(struct ave_dev *ave);
void ave_v4l2_unregister(struct ave_dev *ave);

#endif
Loading
Loading