amphion video decoder/encoder driver
Subsystems (4)
Ming Qian
maintainerv13 cover amphion video decoder/encoder driver
Hi all,
This patch series adds support for
the amphion video encoder and decoder
via the VPU block present in imx8q platforms. Currently, support for IMX8QXP and IMX8QM is included.
It features decoding for the following formats:
- H.264
- HEVC
- MPEG4
- MPEG2
- VC1
- VP8
It features encoding for the following formats:
- H.264
The driver creates a separate device node for the encoder and decoder.
This driver is dependent on vpu firmwares.
The firmwares have been submitted to linux-firmware. The firmware patch is since commit b563148fd28623f6b6ce68bb06c3dd3bd138b058:
linux-firmware: Update firmware file for Intel Bluetooth 9462 (Fri Oct 8 16:30:14 2021 +0530)
and it's available in the git repository at:
https://github.com/mingqian-0/linux-firmware.git
for you to fetch changes up to bb3eee4f99589d4910dee4c053a3a685546b5dbb:
amphion: add VPU firmwares for NXP i.MX8Q SoCs
(Tue Oct 12 15:09:57 2021 +0800)
encoder is tested with gstreamer
decoder is tested with gstreamer, but the following patches are required:
https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1379
https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1381
Changelog:
v13
- make a workaround that avoid firmware entering wfi wrongly
v12
- support reset decoder when starting a new stream
- don't append an empty last buffer, set last_buffer_dequeued
- improve the resolution change flow
- return all buffers if start_streaming fail
- fill encoder capture buffer's filed to none
- fix a bug in calculating bytesperline
v11
- fix dt_binding_check error after upgrade dtschema
- remove "default y"
- add media device
v10
- refine vpu log, remove custom logging infrastructure
- support non contiguous planes format nv12m instead of nv12
- rename V4L2_PIX_FMT_NV12_8L128 to V4L2_PIX_FMT_NV12MT_8L128
- rename V4L2_PIX_FMT_NV12_10BE_8L128 to V4L2_PIX_FMT_NV12MT_10BE_8L128
- merge two module into one
- fix kernel panic in rmmod
v9
- drop V4L2_BUF_FLAG_CODECCONFIG
- drop V4L2_EVENT_CODEC_ERROR
- drop V4L2_EVENT_SKIP - use the v4l2_buffer.sequence counter
- fix some build warnings with W=1 reported by kernel test robot
v8
- move driver from driver/media/platform/imx/vpu-8q to driver/media/platform/amphion
- rename driver name to amphion
- remove imx_vpu.h
- move the definition of V4L2_EVENT_CODEC_ERROR to videodev2.h
- move the definition of V4L2_EVENT_SKIP to videodev2.h
v7
- fix build warnings with W=1 reported by kernel test robot
v6:
- rename V4L2_PIX_FMT_NT8 to V4L2_PIX_FMT_NV12_8L128
- rename V4L2_PIX_FMT_NT10 to V4L2_PIX_FMT_NV12_10BE_8L128
v5:
- move some definition from imx_vph.h to videodev2.h
- remove some unnecessary content
- add some documentation descriptions
- pass the lateset v4l2-compliance test
v4:
- redefine the memory-region in devicetree bindings documentation
- use v4l2's mechanism to implement synchronize queuing ioctl
- remove the unnecessary mutex ioctl_sync
- don't notify source change event if the parameters are same as previously established
- add flag V4L2_FMT_FLAG_DYN_RESOLUTION to decoder's capture format
v3:
- don't make vpu device node a simple-bus
- trigger probing vpu core in the driver
- remove unnecessary vpu core index property
v2:
- fix dt bindings build error
- split driver patch into several parts to avoid exceeding bytes limit
Compliance
==========
# v4l2-compliance -d /dev/video0 v4l2-compliance 1.21.0-4859, 64 bits, 64-bit time_t v4l2-compliance SHA: 493af03f3c57 2021-10-08 17:23:11
Compliance test for amphion-vpu device /dev/video0:
Driver Info:
Driver name : amphion-vpu Card type : amphion vpu decoder Bus info : platform: amphion-vpu Driver version : 5.15.0 Capabilities : 0x84204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Detected Stateful Decoder
Media Driver Info:
Driver name : amphion-vpu Model : amphion-vpu Serial : Bus info : platform: amphion-vpu Media version : 5.15.0 Hardware revision: 0x00000000 (0) Driver version : 5.15.0 Interface Info: ID : 0x0300000c Type : V4L Video Entity Info: ID : 0x00000001 (1) Name : amphion-vpu-decoder-source Function : V4L2 I/O Pad 0x01000002 : 0: Source Link 0x02000008: to remote pad 0x1000004 of entity 'amphion-vpu-decoder-proc' (Video Decoder): Data, Enabled, Immutable
Required ioctls:
test MC information (see 'Media Driver Info' above): OK test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK
Allow for multiple opens:
test second /dev/video0 open: OK test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)
Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported) test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0
Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0
Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)
Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 3 Private Controls: 0
Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK (Not Supported) test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK (Not Supported)
test Composing: OK
test Scaling: OK (Not Supported)
Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK
Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test VIDIOC_EXPBUF: OK
test Requests: OK (Not Supported)
Total for amphion-vpu device /dev/video0: 46, Succeeded: 46, Failed: 0, Warnings: 0
# v4l2-compliance -d /dev/video1 v4l2-compliance 1.21.0-4859, 64 bits, 64-bit time_t v4l2-compliance SHA: 493af03f3c57 2021-10-08 17:23:11
Compliance test for amphion-vpu device /dev/video1:
Driver Info:
Driver name : amphion-vpu Card type : amphion vpu encoder Bus info : platform: amphion-vpu Driver version : 5.15.0 Capabilities : 0x84204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Detected Stateful Encoder
Media Driver Info:
Driver name : amphion-vpu Model : amphion-vpu Serial : Bus info : platform: amphion-vpu Media version : 5.15.0 Hardware revision: 0x00000000 (0) Driver version : 5.15.0 Interface Info: ID : 0x0300001a Type : V4L Video Entity Info: ID : 0x0000000f (15) Name : amphion-vpu-encoder-source Function : V4L2 I/O Pad 0x01000010 : 0: Source Link 0x02000016: to remote pad 0x1000012 of entity 'amphion-vpu-encoder-proc' (Video Encoder): Data, Enabled, Immutable
Required ioctls:
test MC information (see 'Media Driver Info' above): OK test VIDIOC_QUERYCAP: OK
test invalid ioctls: OK
Allow for multiple opens:
test second /dev/video1 open: OK test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)
Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported) test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0
Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0
Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)
Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK test VIDIOC_QUERYCTRL: OK
test VIDIOC_G/S_CTRL: OK
test VIDIOC_G/S/TRY_EXT_CTRLS: OK test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 20 Private Controls: 0
Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK
test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK
test Composing: OK (Not Supported)
test Scaling: OK (Not Supported)
Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK
test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK (Not Supported)
Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test VIDIOC_EXPBUF: OK
test Requests: OK (Not Supported)
Total for amphion-vpu device /dev/video1: 46, Succeeded: 46, Failed: 0, Warnings: 0
# v4l2-compliance -d /dev/media0 v4l2-compliance 1.21.0-4859, 64 bits, 64-bit time_t v4l2-compliance SHA: 493af03f3c57 2021-10-08 17:23:11
Compliance test for amphion-vpu device /dev/media0:
Media Driver Info:
Driver name : amphion-vpu Model : amphion-vpu Serial : Bus info : platform: amphion-vpu Media version : 5.15.0 Hardware revision: 0x00000000 (0) Driver version : 5.15.0
Required ioctls:
test MEDIA_IOC_DEVICE_INFO: OK
test invalid ioctls: OK
Allow for multiple opens:
test second /dev/media0 open: OK test MEDIA_IOC_DEVICE_INFO: OK
test for unlimited opens: OK
Media Controller ioctls:
test MEDIA_IOC_G_TOPOLOGY: OK
Entities: 6 Interfaces: 2 Pads: 8 Links: 8 test MEDIA_IOC_ENUM_ENTITIES/LINKS: OK test MEDIA_IOC_SETUP_LINK: OK
Total for amphion-vpu device /dev/media0: 8, Succeeded: 8, Failed: 0, Warnings: 0
Ming Qian (13):
dt-bindings: media: amphion: add amphion video codec bindings media:Add nv12mt_8l128 and nv12mt_10be_8l128 video format.
media: amphion: add amphion vpu device driver
media: amphion: add vpu core driver
media: amphion: implement vpu core communication based on mailbox
media: amphion: add vpu v4l2 m2m support
media: amphion: add v4l2 m2m vpu encoder stateful driver
media: amphion: add v4l2 m2m vpu decoder stateful driver
media: amphion: implement windsor encoder rpc interface
media: amphion: implement malone decoder rpc interface
ARM64: dts: freescale: imx8q: add imx vpu codec entries
firmware: imx: scu-pd: imx8q: add vpu mu resources
MAINTAINERS: add AMPHION VPU CODEC V4L2 driver entry
.../bindings/media/amphion,vpu.yaml | 180 ++ .../media/v4l/pixfmt-yuv-planar.rst | 15 + MAINTAINERS | 9 + .../arm64/boot/dts/freescale/imx8-ss-vpu.dtsi | 72 + arch/arm64/boot/dts/freescale/imx8qxp-mek.dts | 17 + arch/arm64/boot/dts/freescale/imx8qxp.dtsi | 24 + arch/arm64/configs/defconfig | 1 + drivers/firmware/imx/scu-pd.c | 4 + drivers/media/platform/Kconfig | 19 + drivers/media/platform/Makefile | 2 + drivers/media/platform/amphion/Makefile | 20 + drivers/media/platform/amphion/vdec.c | 1680 +++++++++++++++++ drivers/media/platform/amphion/venc.c | 1351 +++++++++++++ drivers/media/platform/amphion/vpu.h | 357 ++++ drivers/media/platform/amphion/vpu_cmds.c | 439 +++++ drivers/media/platform/amphion/vpu_cmds.h | 25 + drivers/media/platform/amphion/vpu_codec.h | 67 + drivers/media/platform/amphion/vpu_color.c | 190 ++ drivers/media/platform/amphion/vpu_core.c | 906 +++++++++ drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++ drivers/media/platform/amphion/vpu_defs.h | 186 ++ drivers/media/platform/amphion/vpu_drv.c | 265 +++ drivers/media/platform/amphion/vpu_helpers.c | 436 +++++ drivers/media/platform/amphion/vpu_helpers.h | 71 + drivers/media/platform/amphion/vpu_imx8q.c | 271 +++ drivers/media/platform/amphion/vpu_imx8q.h | 116 ++ drivers/media/platform/amphion/vpu_malone.c | 1679 ++++++++++++++++ drivers/media/platform/amphion/vpu_malone.h | 42 + drivers/media/platform/amphion/vpu_mbox.c | 124 ++ drivers/media/platform/amphion/vpu_mbox.h | 16 + drivers/media/platform/amphion/vpu_msgs.c | 414 ++++ drivers/media/platform/amphion/vpu_msgs.h | 14 + drivers/media/platform/amphion/vpu_rpc.c | 279 +++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++ drivers/media/platform/amphion/vpu_v4l2.c | 703 +++++++ drivers/media/platform/amphion/vpu_v4l2.h | 54 + drivers/media/platform/amphion/vpu_windsor.c | 1222 ++++++++++++ drivers/media/platform/amphion/vpu_windsor.h | 39 + drivers/media/v4l2-core/v4l2-ioctl.c | 2 + include/uapi/linux/videodev2.h | 2 + 41 files changed, 12287 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/amphion,vpu.yaml create mode 100644 arch/arm64/boot/dts/freescale/imx8-ss-vpu.dtsi create mode 100644 drivers/media/platform/amphion/Makefile create mode 100644 drivers/media/platform/amphion/vdec.c create mode 100644 drivers/media/platform/amphion/venc.c create mode 100644 drivers/media/platform/amphion/vpu.h create mode 100644 drivers/media/platform/amphion/vpu_cmds.c create mode 100644 drivers/media/platform/amphion/vpu_cmds.h create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_color.c create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_defs.h create mode 100644 drivers/media/platform/amphion/vpu_drv.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.h create mode 100644 drivers/media/platform/amphion/vpu_imx8q.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.h create mode 100644 drivers/media/platform/amphion/vpu_malone.c create mode 100644 drivers/media/platform/amphion/vpu_malone.h create mode 100644 drivers/media/platform/amphion/vpu_mbox.c create mode 100644 drivers/media/platform/amphion/vpu_mbox.h create mode 100644 drivers/media/platform/amphion/vpu_msgs.c create mode 100644 drivers/media/platform/amphion/vpu_msgs.h create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.h create mode 100644 drivers/media/platform/amphion/vpu_v4l2.c create mode 100644 drivers/media/platform/amphion/vpu_v4l2.h create mode 100644 drivers/media/platform/amphion/vpu_windsor.c create mode 100644 drivers/media/platform/amphion/vpu_windsor.h
base-commit: 999ed03518cb01aa9ef55c025db79567eec6268c
Signature · 1 line
2.33.0
Ming Qian
maintainerPATCH v13 1/13 dt-bindings: media: amphion: add amphion video codec bindings
Add devicetree binding documentation for amphion
Video Processing Unit IP presents on NXP i.MX8Q
--- .../bindings/media/amphion,vpu.yaml | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/amphion,vpu.yaml
Documentation/devicetree/bindings/media/amphion,vpu.yaml new file +180 -0
@@ -0,0 +1,180 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) + +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/media/amphion,vpu.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Amphion VPU codec IP + +maintainers: + - Ming Qian <[email protected]> + - Shijie Qin <[email protected]> + +description: |- + The Amphion MXC video encoder(Windsor) and decoder(Malone) accelerators present + on NXP i.MX8Q SoCs. + +properties: + $nodename: + pattern: "^vpu@[0-9a-f]+$" + + compatible: + items: + - enum: + - nxp,imx8qm-vpu + - nxp,imx8qxp-vpu + + reg: + maxItems: 1 + + power-domains: + maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 1 + + ranges: true + +patternProperties: + "^mailbox@[0-9a-f]+$": + description: + Each vpu encoder or decoder correspond a MU, which used for communication + between driver and firmware. Implement via mailbox on driver. + $ref: ../mailbox/fsl,mu.yaml# + + + "^vpu_core@[0-9a-f]+$": + description: + Each core correspond a decoder or encoder, need to configure them + separately. NXP i.MX8QM SoC has one decoder and two encoder, i.MX8QXP SoC + has one decoder and one encoder. + type: object + + properties: + compatible: + items: + - enum: + - nxp,imx8q-vpu-decoder + - nxp,imx8q-vpu-encoder + + reg: + maxItems: 1 + + power-domains: + maxItems: 1 + + mbox-names: + items: + - const: tx0 + - const: tx1 + - const: rx + + mboxes: + description: + List of phandle of 2 MU channels for tx, 1 MU channel for rx. + maxItems: 3 + + memory-region: + description: + Phandle to the reserved memory nodes to be associated with the + remoteproc device. The reserved memory nodes should be carveout nodes, + and should be defined as per the bindings in + Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt + items: + - description: region reserved for firmware image sections. + - description: region used for RPC shared memory between firmware and + driver. + + required: + - compatible + - reg + - power-domains + - mbox-names + - mboxes + - memory-region + + additionalProperties: false + +required: + - compatible + - reg + - power-domains + +additionalProperties: false + +examples: + # Device node example for i.MX8QM platform: + - | + #include <dt-bindings/firmware/imx/rsrc.h> + + vpu: vpu@2c000000 { + compatible = "nxp,imx8qm-vpu"; + ranges = <0x2c000000 0x2c000000 0x2000000>; + reg = <0x2c000000 0x1000000>; + #address-cells = <1>; + #size-cells = <1>; + power-domains = <&pd IMX_SC_R_VPU>; + + mu_m0: mailbox@2d000000 { + compatible = "fsl,imx6sx-mu"; + reg = <0x2d000000 0x20000>; + interrupts = <0 472 4>; + #mbox-cells = <2>; + power-domains = <&pd IMX_SC_R_VPU_MU_0>; + }; + + mu1_m0: mailbox@2d020000 { + compatible = "fsl,imx6sx-mu"; + reg = <0x2d020000 0x20000>; + interrupts = <0 473 4>; + #mbox-cells = <2>; + power-domains = <&pd IMX_SC_R_VPU_MU_1>; + }; + + mu2_m0: mailbox@2d040000 { + compatible = "fsl,imx6sx-mu"; + reg = <0x2d040000 0x20000>; + interrupts = <0 474 4>; + #mbox-cells = <2>; + power-domains = <&pd IMX_SC_R_VPU_MU_2>; + }; + + vpu_core0: vpu_core@2d080000 { + compatible = "nxp,imx8q-vpu-decoder"; + reg = <0x2d080000 0x10000>; + power-domains = <&pd IMX_SC_R_VPU_DEC_0>; + mbox-names = "tx0", "tx1", "rx"; + mboxes = <&mu_m0 0 0>, + <&mu_m0 0 1>, + <&mu_m0 1 0>; + memory-region = <&decoder_boot>, <&decoder_rpc>; + }; + + vpu_core1: vpu_core@2d090000 { + compatible = "nxp,imx8q-vpu-encoder"; + reg = <0x2d090000 0x10000>; + power-domains = <&pd IMX_SC_R_VPU_ENC_0>; + mbox-names = "tx0", "tx1", "rx"; + mboxes = <&mu1_m0 0 0>, + <&mu1_m0 0 1>, + <&mu1_m0 1 0>; + memory-region = <&encoder1_boot>, <&encoder1_rpc>; + }; + + vpu_core2: vpu_core@2d0a0000 { + reg = <0x2d0a0000 0x10000>; + compatible = "nxp,imx8q-vpu-encoder"; + power-domains = <&pd IMX_SC_R_VPU_ENC_1>; + mbox-names = "tx0", "tx1", "rx"; + mboxes = <&mu2_m0 0 0>, + <&mu2_m0 0 1>, + <&mu2_m0 1 0>; + memory-region = <&encoder2_boot>, <&encoder2_rpc>; + }; + }; + +...
Signature · 1 line
2.33.0
Ming Qian
maintainerPATCH v13 2/13 media:Add nv12mt_8l128 and nv12mt_10be_8l128 video format.
In reply to Ming Qiannv12mt_8l128 is 8-bit tiled nv12 format used by amphion decoder. nv12mt_10be_8l128 is 10-bit tiled format used by amphion decoder. The tile size is 8x128
--- .../userspace-api/media/v4l/pixfmt-yuv-planar.rst | 15 +++++++++++++++ drivers/media/v4l2-core/v4l2-ioctl.c | 2 ++ include/uapi/linux/videodev2.h | 2 ++ 3 files changed, 19 insertions(+)
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +15 -0
@@ -257,6 +257,8 @@ of the luma plane. .. _V4L2-PIX-FMT-NV12-4L4: .. _V4L2-PIX-FMT-NV12-16L16: .. _V4L2-PIX-FMT-NV12-32L32: +.. _V4L2_PIX_FMT_NV12MT_8L128: +.. _V4L2_PIX_FMT_NV12MT_10BE_8L128: Tiled NV12 ----------
@@ -296,6 +298,19 @@ tiles linearly in memory. The line stride and image height must be aligned to a multiple of 32. The layouts of the luma and chroma planes are identical. +``V4L2_PIX_FMT_NV12MT_8L128`` is similar to ``V4L2_PIX_FMT_NV12M`` but stores +pixel in 2D 8x128 tiles, and stores tiles linearly in memory. +The image height must be aligned to a multiple of 128. +The layouts of the luma and chroma planes are identical. + +``V4L2_PIX_FMT_NV12MT_10BE_8L128`` is similar to ``V4L2_PIX_FMT_NV12M`` but stores +10 bits pixel in 2D 8x128 tiles, and stores tiles linearly in memory. +the data is arranged at the big end. +The image height must be aligned to a multiple of 128. +The layouts of the luma and chroma planes are identical. +Note the tile size is 8bytes multiplied by 128 bytes, +it means that the low bits and high bits of one pixel may be in differnt tiles. + .. _nv12mt: .. kernel-figure:: nv12mt.svg
drivers/media/v4l2-core/v4l2-ioctl.c +2 -0
@@ -1388,6 +1388,8 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)case V4L2_META_FMT_VIVID : descr ="Vivid Metadata" ;break ;case V4L2_META_FMT_RK_ISP1_PARAMS : descr ="Rockchip ISP1 3A Parameters" ;break ;case V4L2_META_FMT_RK_ISP1_STAT_3A : descr ="Rockchip ISP1 3A Statistics" ;break ; +case V4L2_PIX_FMT_NV12MT_8L128 : descr ="NV12M (8x128 Linear)" ;break ; +case V4L2_PIX_FMT_NV12MT_10BE_8L128 : descr ="NV12M 10BE(8x128 Linear)" ;break ;default :/* Compressed formats */
include/uapi/linux/videodev2.h +2 -0
@@ -632,6 +632,8 @@ struct v4l2_pix_format {/* Tiled YUV formats, non contiguous planes */ #define V4L2_PIX_FMT_NV12MT v4l2_fourcc('T', 'M', '1', '2')/* 12 Y/CbCr 4:2:0 64x32 tiles */ #define V4L2_PIX_FMT_NV12MT_16X16 v4l2_fourcc('V', 'M', '1', '2')/* 12 Y/CbCr 4:2:0 16x16 tiles */ +#define V4L2_PIX_FMT_NV12MT_8L128 v4l2_fourcc('N', 'A', '1', '2')/* Y/CbCr 4:2:0 8x128 tiles */ +#define V4L2_PIX_FMT_NV12MT_10BE_8L128 v4l2_fourcc('N', 'T', '1', '2')/* Y/CbCr 4:2:0 10-bit 8x128 tiles */ /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */ #define V4L2_PIX_FMT_SBGGR8 v4l2_fourcc('B', 'A', '8', '1')/* 8 BGBG.. GRGR.. */
Signature · 1 line
2.33.0
Re: [PATCH v13 02/13] media:Add nv12mt_8l128 and nv12mt_10be_8l128 video format.
Quoted 27 lines · Ming Qian
nv12mt_8l128is 8-bit tiled nv12 format used by amphion decoder.nv12mt_10be_8l128is 10-bit tiled format used by amphion decoder. The tile size is 8x128Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- .../userspace-api/media/v4l/pixfmt-yuv-planar.rst | 15 +++++++++++++++ drivers/media/v4l2-core/v4l2-ioctl.c | 2 ++ include/uapi/linux/videodev2.h | 2 ++ 3 files changed, 19 insertions(+)Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +2
-0@@ -257,6 +257,8 @@ of the luma plane. .. _V4L2-PIX-FMT-NV12-4L4: .. _V4L2-PIX-FMT-NV12-16L16: .. _V4L2-PIX-FMT-NV12-32L32: +.. _V4L2_PIX_FMT_NV12MT_8L128: +.. _V4L2_PIX_FMT_NV12MT_10BE_8L128: Tiled NV12 ----------
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst tiles linearly in memory. The line stride and image height must be · line 302
Quoted 5 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +2
-0@@ -296,6 +298,19 @@ tiles linearly in memory. The line stride and image height must be aligned to a multiple of 32. The layouts of the luma and chroma planes are identical. +``V4L2_PIX_FMT_NV12MT_8L128`` is similar to ``V4L2_PIX_FMT_NV12M`` but stores +pixel in 2D 8x128 tiles, and stores tiles linearly in memory.
pixel -> pixels (note: also wrong in the text V4L2_PIX_FMT_NV12_4L4/16L16/32L32)
Shouldn't this be called V4L2_PIX_FMT_NV12M_8L128? The 'MT' suffix seems to be specific to macroblock tiles and not linear tiles.
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst tiles linearly in memory. The line stride and image height must be · line 308
Quoted 6 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +6
-0+The image height must be aligned to a multiple of 128. +The layouts of the luma and chroma planes are identical. + +``V4L2_PIX_FMT_NV12MT_10BE_8L128`` is similar to ``V4L2_PIX_FMT_NV12M`` but stores +10 bits pixel in 2D 8x128 tiles, and stores tiles linearly in memory. +the data is arranged at the big end.
at the big end -> in big endian order
I assume the 10 bit pixels are packed? So 5 bytes contain 4 10-bit pixels layout like this (for luma):
byte 0: Y0(bits 9-2)
byte 1: Y0(bits 1-0) Y1(bits 9-4)
byte 2: Y1(bits 3-0) Y2(bits 9-6)
byte 3: Y2(bits 5-0) Y3(bits 9-8)
byte 4: Y3(bits 7-0)
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst tiles linearly in memory. The line stride and image height must be · line 312
Quoted 4 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +4
-0+The image height must be aligned to a multiple of 128. +The layouts of the luma and chroma planes are identical. +Note the tile size is 8bytes multiplied by 128 bytes, +it means that the low bits and high bits of one pixel may be in differnt tiles.
differnt -> different
Quoted 9 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +1
-0+ .. _nv12mt: .. kernel-figure:: nv12mt.svgdrivers/media/v4l2-core/v4l2-ioctl.c +0
-0
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt) · line 1392
Quoted 5 lines
drivers/media/v4l2-core/v4l2-ioctl.c +2
-0@@ -1388,6 +1388,8 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)case V4L2_META_FMT_VIVID : descr ="Vivid Metadata" ;break ;case V4L2_META_FMT_RK_ISP1_PARAMS : descr ="Rockchip ISP1 3A Parameters" ;break ;case V4L2_META_FMT_RK_ISP1_STAT_3A : descr ="Rockchip ISP1 3A Statistics" ;break ; +case V4L2_PIX_FMT_NV12MT_8L128 : descr ="NV12M (8x128 Linear)" ;break ; +case V4L2_PIX_FMT_NV12MT_10BE_8L128 : descr ="NV12M 10BE(8x128 Linear)" ;break ;
"10-bit NV12M (8x128 Linear, BE)"
include/uapi/linux/videodev2.h struct v4l2_pix_format { · line 636
Quoted 13 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0default :/* Compressed formats */ include/uapi/linux/videodev2.h +2
-0@@ -632,6 +632,8 @@ struct v4l2_pix_format {/* Tiled YUV formats, non contiguous planes */ #define V4L2_PIX_FMT_NV12MT v4l2_fourcc('T', 'M', '1', '2')/* 12 Y/CbCr 4:2:0 64x32 tiles */ #define V4L2_PIX_FMT_NV12MT_16X16 v4l2_fourcc('V', 'M', '1', '2')/* 12 Y/CbCr 4:2:0 16x16 tiles */ +#define V4L2_PIX_FMT_NV12MT_8L128 v4l2_fourcc('N', 'A', '1', '2')/* Y/CbCr 4:2:0 8x128 tiles */ +#define V4L2_PIX_FMT_NV12MT_10BE_8L128 v4l2_fourcc('N', 'T', '1', '2')/* Y/CbCr 4:2:0 10-bit 8x128 tiles */
Use v4l2_fourcc_be to denote that this is a BE format.
include/uapi/linux/videodev2.h struct v4l2_pix_format {
Quoted 4 lines
include/uapi/linux/videodev2.h +0
-0/* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */ #define V4L2_PIX_FMT_SBGGR8 v4l2_fourcc('B', 'A', '8', '1')/* 8 BGBG.. GRGR.. */
Regards,
Hans
Ming Qian
maintainerRE: [EXT] Re: [PATCH v13 02/13] media:Add nv12mt_8l128 and nv12mt_10be_8l128 video format.
Quoted 48 lines
-----Original Message-----
From: Hans Verkuil [mailto:[email protected]]
Sent: Thursday, December 2, 2021 5:40 PM
To: Ming Qian <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; dl-linux-imx <[email protected]>; Aisheng Dong <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXT] Re: [PATCH v13 02/13] media:Addnv12mt_8l128andnv12mt_10be_8l128video format.Caution: EXT Email
Quoted 32 lines · Ming Qian
nv12mt_8l128is 8-bit tiled nv12 format used by amphion decoder.nv12mt_10be_8l128is 10-bit tiled format used by amphion decoder. The tile size is 8x128Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- .../userspace-api/media/v4l/pixfmt-yuv-planar.rst | 15 +++++++++++++++ drivers/media/v4l2-core/v4l2-ioctl.c | 2 ++ include/uapi/linux/videodev2.h | 2 ++ 3 files changed, 19 insertions(+)diff --git
a/Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rstb/Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rstindex3a09d93d405b..fc3baa2753ab100644Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +2
-0@@ -257,6 +257,8 @@ of the luma plane. .. _V4L2-PIX-FMT-NV12-4L4: .. _V4L2-PIX-FMT-NV12-16L16: .. _V4L2-PIX-FMT-NV12-32L32: +.. _V4L2_PIX_FMT_NV12MT_8L128: +.. _V4L2_PIX_FMT_NV12MT_10BE_8L128: Tiled NV12 ----------@@ -296,6 +298,19 @@ tiles linearly in memory. The line stride andimage height must be aligned to a multiple of 32. The layouts of the luma and chroma planes are identical.
Quoted 8 lines
Quoted 2 lines
+``V4L2_PIX_FMT_NV12MT_8L128`` is similar to ``V4L2_PIX_FMT_NV12M`` +but stores pixel in 2D 8x128 tiles, and stores tiles linearly in memory.
pixel -> pixels (note: also wrong in the text
V4L2_PIX_FMT_NV12_4L4/16L16/32L32)Shouldn't this be called
V4L2_PIX_FMT_NV12M_8L128? The 'MT' suffix seems to be specific to macroblock tiles and not linear tiles.
I'll change it, I thought the t means tiled
Quoted 20 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst tiles linearly in memory. The line stride and image height must be · line 308
Quoted 8 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +7
-0+The image height must be aligned to a multiple of 128. +The layouts of the luma and chroma planes are identical. + +``V4L2_PIX_FMT_NV12MT_10BE_8L128`` is similar to +``V4L2_PIX_FMT_NV12M`` but stores +10 bits pixel in 2D 8x128 tiles, and stores tiles linearly in memory. +the data is arranged at the big end.at the big end -> in big endian order
I assume the 10 bit pixels are packed? So 5 bytes contain 4 10-bit pixels layout like this (for luma):
byte 0: Y0(bits 9-2)
byte 1: Y0(bits 1-0) Y1(bits 9-4)
byte 2: Y1(bits 3-0) Y2(bits 9-6)
byte 3: Y2(bits 5-0) Y3(bits 9-8)
byte 4: Y3(bits 7-0)
Quoted 7 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst tiles linearly in memory. The line stride and image height must be
Quoted 4 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +4
-0+The image height must be aligned to a multiple of 128. +The layouts of the luma and chroma planes are identical. +Note the tile size is 8bytes multiplied by 128 bytes, it means that +the low bits and high bits of one pixel may be in differnt tiles.differnt -> different
Got it
Quoted 68 lines
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc
Quoted 10 lines
Documentation/userspace-api/media/v4l/pixfmt-yuv-planar.rst +1
-0+ .. _nv12mt: .. kernel-figure:: nv12mt.svgdrivers/media/v4l2-core/v4l2-ioctl.c +0
-0@@ -1388,6 +1388,8 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc*fmt)
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc · line 1388
Quoted 1 line
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0case V4L2_META_FMT_VIVID : descr ="Vivid Metadata" ;break;
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc · line 1389
Quoted 1 line
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0case V4L2_META_FMT_RK_ISP1_PARAMS: descr = "RockchipISP1 3A Parameters"; break;
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc · line 1390
Quoted 1 line
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0case V4L2_META_FMT_RK_ISP1_STAT_3A: descr = "RockchipISP1 3A Statistics"; break;
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc · line 1391
Quoted 1 line
drivers/media/v4l2-core/v4l2-ioctl.c +1
-0+ case V4L2_PIX_FMT_NV12MT_8L128: descr = "NV12M (8x128Linear)"; break;
drivers/media/v4l2-core/v4l2-ioctl.c static void v4l_fill_fmtdesc(struct v4l2_fmtdesc · line 1392
Quoted 1 line
drivers/media/v4l2-core/v4l2-ioctl.c +1
-0+ case V4L2_PIX_FMT_NV12MT_10BE_8L128: descr = "NV12M10BE(8x128 Linear)"; break;
"10-bit NV12M (8x128 Linear, BE)"
Quoted 9 lines
drivers/media/v4l2-core/v4l2-ioctl.c +0
-0default : /* Compressed formats */ diff --git
a/include/uapi/linux/videodev2.hb/include/uapi/linux/videodev2.hindexf118fe7a9f58..9443c3109928100644include/uapi/linux/videodev2.h +0
-0@@ -632,6 +632,8 @@ struct v4l2_pix_format { /* Tiled YUV formats, non contiguous planes */ #defineQuoted 4 lines
V4L2_PIX_FMT_NV12MTv4l2_fourcc('T', 'M', '1', '2')/* 12 Y/CbCr 4:2:0 64x32 tiles */ #defineV4L2_PIX_FMT_NV12MT_16X16v4l2_fourcc('V', 'M', '1', '2')/* 12 Y/CbCr 4:2:0 16x16 tiles */
+#defineV4L2_PIX_FMT_NV12MT_8L128v4l2_fourcc('N', 'A', '1', '2')/* Y/CbCr 4:2:0 8x128 tiles */
Quoted 2 lines
+#define
V4L2_PIX_FMT_NV12MT_10BE_8L128v4l2_fourcc('N', 'T', '1', +'2')/* Y/CbCr 4:2:0 10-bit 8x128 tiles */Use
v4l2_fourcc_beto denote that this is a BE format.Quoted 4 lines
/* Bayer formats - see
https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.siliconimaging.com%2FRGB%2520Bayer.htm&data=04%7C01%7Cming.qia n%40n
Quoted 1 line
xp.com%7Ce303b4acc4c7478171bc08d9b577abe4%7C686ea1d3bc2b4c6fa92 cd99c5c
Quoted 1 line
301635%7C0%7C0%7C637740347869679174%7CUnknown%7CTWFpbGZsb3 d8eyJWIjoiMC
Quoted 1 line
4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&a mp;sd
Quoted 1 line
ata=DJGu6nhkZwiJlVD7oqXG0ZUOGBMmrILtiYirX87MyEo%3D&reserved= 0 */
Quoted 3 lines
#define
V4L2_PIX_FMT_SBGGR8v4l2_fourcc('B', 'A', '8', '1')/* 8 BGBG.. GRGR.. */Regards,
Hans
Ming Qian
maintainerPATCH v13 3/13 media: amphion: add amphion vpu device driver
In reply to Ming QianThe amphion vpu codec ip contains encoder and decoder. Windsor is the encoder, it supports to encode H.264. Malone is the decoder, it features a powerful video processing unit able to decode many foramts, such as H.264, HEVC, and other foramts.
This Driver is for this IP that is based on the v4l2 mem2mem framework.
Supported SoCs are: IMX8QXP, IMX8QM
--- arch/arm64/configs/defconfig | 1 + drivers/media/platform/Kconfig | 19 ++ drivers/media/platform/Makefile | 2 + drivers/media/platform/amphion/Makefile | 20 ++ drivers/media/platform/amphion/vpu.h | 357 +++++++++++++++++++++ drivers/media/platform/amphion/vpu_defs.h | 186 +++++++++++ drivers/media/platform/amphion/vpu_drv.c | 265 +++++++++++++++ drivers/media/platform/amphion/vpu_imx8q.c | 271 ++++++++++++++++ drivers/media/platform/amphion/vpu_imx8q.h | 116 +++++++ 9 files changed, 1237 insertions(+) create mode 100644 drivers/media/platform/amphion/Makefile create mode 100644 drivers/media/platform/amphion/vpu.h create mode 100644 drivers/media/platform/amphion/vpu_defs.h create mode 100644 drivers/media/platform/amphion/vpu_drv.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.h
arch/arm64/configs/defconfig +1 -0
@@ -657,6 +657,7 @@ CONFIG_V4L_PLATFORM_DRIVERS=y CONFIG_VIDEO_RCAR_CSI2=m CONFIG_VIDEO_RCAR_VIN=m CONFIG_VIDEO_SUN6I_CSI=m +CONFIG_VIDEO_AMPHION_VPU=m CONFIG_V4L_MEM2MEM_DRIVERS=y CONFIG_VIDEO_SAMSUNG_S5P_JPEG=m CONFIG_VIDEO_SAMSUNG_S5P_MFC=m
drivers/media/platform/Kconfig +19 -0
@@ -216,6 +216,25 @@ config VIDEO_RCAR_ISP To compile this driver as a module, choose M here: the module will be called rcar-isp. +config VIDEO_AMPHION_VPU + tristate "Amphion VPU(Video Processing Unit) Codec IP" + depends on ARCH_MXC + depends on MEDIA_SUPPORT + depends on VIDEO_DEV + depends on VIDEO_V4L2 + select MEDIA_CONTROLLER + select V4L2_MEM2MEM_DEV + select VIDEOBUF2_DMA_CONTIG + select VIDEOBUF2_VMALLOC + help + Amphion VPU Codec IP contains two parts: Windsor and Malone. + Windsor is encoder that supports H.264, and Malone is decoder + that supports H.264, HEVC, and other video formats. + This is a V4L2 driver for NXP MXC 8Q video accelerator hardware. + It accelerates encoding and decoding operations on + various NXP SoCs. + To compile this driver as a module choose m here. + endif # V4L_PLATFORM_DRIVERS menuconfig V4L_MEM2MEM_DRIVERS
drivers/media/platform/Makefile +2 -0
@@ -88,3 +88,5 @@ obj-$(CONFIG_VIDEO_QCOM_VENUS) += qcom/venus/ obj-y += sunxi/ obj-$(CONFIG_VIDEO_MESON_GE2D) += meson/ge2d/ + +obj-$(CONFIG_VIDEO_AMPHION_VPU) += amphion/
drivers/media/platform/amphion/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for NXP VPU driver + +amphion-vpu-objs += vpu_drv.o \ + vpu_core.o \ + vpu_mbox.o \ + vpu_v4l2.o \ + vpu_helpers.o \ + vpu_cmds.o \ + vpu_msgs.o \ + vpu_rpc.o \ + vpu_imx8q.o \ + vpu_windsor.o \ + vpu_malone.o \ + vpu_color.o \ + vdec.o \ + venc.o \ + vpu_dbg.o + +obj-$(CONFIG_VIDEO_AMPHION_VPU) += amphion-vpu.o
drivers/media/platform/amphion/vpu.h new file +357 -0
357 lines not on this page · raw message
drivers/media/platform/amphion/vpu_defs.h new file +186 -0
186 lines not on this page · raw message
drivers/media/platform/amphion/vpu_drv.c new file +265 -0
265 lines not on this page · raw message
drivers/media/platform/amphion/vpu_imx8q.c new file +271 -0
271 lines not on this page · raw message
drivers/media/platform/amphion/vpu_imx8q.h new file +116 -0
116 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v13 03/13] media: amphion: add amphion vpu device driver
Quoted 4 lines · Ming Qian
The amphion vpu codec ip contains encoder and decoder. Windsor is the encoder, it supports to encode H.264. Malone is the decoder, it features a powerful video processing unit able to decode many foramts,
foramts -> formats
Quoted 1 line
such as H.264, HEVC, and other foramts.
ditto
Quoted 44 lines
This Driver is for this IP that is based on the v4l2 mem2mem framework.
Supported SoCs are: IMX8QXP, IMX8QM
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>Reported-by kernel test robot <[email protected]>--- arch/arm64/configs/defconfig | 1 + drivers/media/platform/Kconfig | 19 ++ drivers/media/platform/Makefile | 2 + drivers/media/platform/amphion/Makefile | 20 ++ drivers/media/platform/amphion/vpu.h | 357 +++++++++++++++++++++ drivers/media/platform/amphion/vpu_defs.h | 186 +++++++++++ drivers/media/platform/amphion/vpu_drv.c | 265 +++++++++++++++ drivers/media/platform/amphion/vpu_imx8q.c | 271 ++++++++++++++++ drivers/media/platform/amphion/vpu_imx8q.h | 116 +++++++ 9 files changed, 1237 insertions(+) create mode 100644 drivers/media/platform/amphion/Makefile create mode 100644 drivers/media/platform/amphion/vpu.h create mode 100644 drivers/media/platform/amphion/vpu_defs.h create mode 100644 drivers/media/platform/amphion/vpu_drv.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.harch/arm64/configs/defconfig +1
-0@@ -657,6 +657,7 @@ CONFIG_V4L_PLATFORM_DRIVERS=y CONFIG_VIDEO_RCAR_CSI2=m CONFIG_VIDEO_RCAR_VIN=m CONFIG_VIDEO_SUN6I_CSI=m +CONFIG_VIDEO_AMPHION_VPU=m CONFIG_V4L_MEM2MEM_DRIVERS=y CONFIG_VIDEO_SAMSUNG_S5P_JPEG=m CONFIG_VIDEO_SAMSUNG_S5P_MFC=mdrivers/media/platform/Kconfig +0
-0
drivers/media/platform/Kconfig config VIDEO_RCAR_ISP · line 220
Quoted 5 lines
drivers/media/platform/Kconfig +2
-0@@ -216,6 +216,25 @@ config VIDEO_RCAR_ISP To compile this driver as a module, choose M here: the module will be called rcar-isp. +config VIDEO_AMPHION_VPU + tristate "Amphion VPU(Video Processing Unit) Codec IP"
Add space before (
drivers/media/platform/Kconfig config VIDEO_RCAR_ISP · line 221
Quoted 1 line
drivers/media/platform/Kconfig +1
-0+ depends on ARCH_MXC
Add: || COMPILE_TEST
It should always be possible to compile test drivers, even on other architectures.
Quoted 9 lines
drivers/media/platform/Kconfig +9
-0+ depends on MEDIA_SUPPORT + depends on VIDEO_DEV + depends on VIDEO_V4L2 + select MEDIA_CONTROLLER + select V4L2_MEM2MEM_DEV + select VIDEOBUF2_DMA_CONTIG + select VIDEOBUF2_VMALLOC + help + Amphion VPU Codec IP contains two parts: Windsor and Malone.
drivers/media/platform/Kconfig config VIDEO_RCAR_ISP · line 240
Quoted 10 lines
drivers/media/platform/Kconfig +7
-0+ Windsor is encoder that supports H.264, and Malone is decoder + that supports H.264, HEVC, and other video formats. + This is a V4L2 driver for NXP MXC 8Q video accelerator hardware. + It accelerates encoding and decoding operations on + various NXP SoCs. + To compile this driver as a module choose m here. + endif # V4L_PLATFORM_DRIVERS menuconfig V4L_MEM2MEM_DRIVERS
Regards,
Hans
Ming Qian
maintainerRE: [EXT] Re: [PATCH v13 03/13] media: amphion: add amphion vpu device driver
Quoted 20 lines
-----Original Message-----
From: Hans Verkuil [mailto:[email protected]]
Sent: Thursday, December 2, 2021 5:45 PM
To: Ming Qian <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; dl-linux-imx <[email protected]>; Aisheng Dong <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXT] Re: [PATCH v13 03/13] media: amphion: add amphion vpu device driverCaution: EXT Email
Quoted 4 lines · Ming Qian
The amphion vpu codec ip contains encoder and decoder. Windsor is the encoder, it supports to encode H.264. Malone is the decoder, it features a powerful video processing unit able to decode many foramts,
Quoted 6 lines
foramts -> formats
Quoted 1 line
such as H.264, HEVC, and other foramts.
ditto
Got it, I'll fix it
Quoted 83 lines
Quoted 9 lines
This Driver is for this IP that is based on the v4l2 mem2mem framework.
Supported SoCs are: IMX8QXP, IMX8QM
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>Reported-by kernel test robot <[email protected]>Quoted 6 lines
--- arch/arm64/configs/defconfig | 1 + drivers/media/platform/Kconfig | 19 ++ drivers/media/platform/Makefile | 2 + drivers/media/platform/amphion/Makefile | 20 ++ drivers/media/platform/amphion/vpu.h | 357+++++++++++++++++++++
Quoted 29 lines
drivers/media/platform/amphion/vpu_defs.h | 186 +++++++++++ drivers/media/platform/amphion/vpu_drv.c | 265 +++++++++++++++ drivers/media/platform/amphion/vpu_imx8q.c | 271 ++++++++++++++++ drivers/media/platform/amphion/vpu_imx8q.h | 116 +++++++ 9 files changed, 1237 insertions(+) create mode 100644 drivers/media/platform/amphion/Makefile create mode 100644 drivers/media/platform/amphion/vpu.h create mode 100644 drivers/media/platform/amphion/vpu_defs.h create mode 100644 drivers/media/platform/amphion/vpu_drv.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.c create mode 100644 drivers/media/platform/amphion/vpu_imx8q.h(quoted diff) +0
-0b/arch/arm64/configs/defconfig index
f2e2b9bdd702..cc3633112f3f100644arch/arm64/configs/defconfig +0
-0@@ -657,6 +657,7 @@ CONFIG_V4L_PLATFORM_DRIVERS=y
CONFIG_VIDEO_RCAR_CSI2=mCONFIG_VIDEO_RCAR_VIN=mCONFIG_VIDEO_SUN6I_CSI=march/arm64/configs/defconfig +1
-0+CONFIG_VIDEO_AMPHION_VPU=m CONFIG_V4L_MEM2MEM_DRIVERS=y CONFIG_VIDEO_SAMSUNG_S5P_JPEG=m CONFIG_VIDEO_SAMSUNG_S5P_MFC=m(quoted diff) +0
-0b/drivers/media/platform/Kconfig index
9fbdba0fd1e7..7d4a8cd52a9e100644drivers/media/platform/Kconfig config VIDEO_RCAR_ISP · line 220
Quoted 5 lines
drivers/media/platform/Kconfig +2
-0@@ -216,6 +216,25 @@ config VIDEO_RCAR_ISP To compile this driver as a module, choose M here: the module will be called rcar-isp. +config VIDEO_AMPHION_VPU + tristate "Amphion VPU(Video Processing Unit) Codec IP"Add space before (
drivers/media/platform/Kconfig config VIDEO_RCAR_ISP · line 221
Quoted 1 line
drivers/media/platform/Kconfig +1
-0+ depends on ARCH_MXCAdd: ||
COMPILE_TESTIt should always be possible to compile test drivers, even on other architectures.
Quoted 9 lines
drivers/media/platform/Kconfig +9
-0+ depends on MEDIA_SUPPORT + depends on VIDEO_DEV + depends on VIDEO_V4L2 + select MEDIA_CONTROLLER + select V4L2_MEM2MEM_DEV + select VIDEOBUF2_DMA_CONTIG + select VIDEOBUF2_VMALLOC + help + Amphion VPU Codec IP contains two parts: Windsor and Malone.drivers/media/platform/Kconfig config VIDEO_RCAR_ISP · line 240
Quoted 10 lines
drivers/media/platform/Kconfig +7
-0+ Windsor is encoder that supports H.264, and Malone is decoder + that supports H.264, HEVC, and other video formats. + This is a V4L2 driver for NXP MXC 8Q video accelerator hardware. + It accelerates encoding and decoding operations on + various NXP SoCs. + To compile this driver as a module choose m here. + endif # V4L_PLATFORM_DRIVERS menuconfig V4L_MEM2MEM_DRIVERSRegards,
Hans
Ming Qian
maintainerPATCH v13 4/13 media: amphion: add vpu core driver
In reply to Ming QianThe vpu supports encoder and decoder.
it needs mu core to handle it.
core will run either encoder or decoder firmware.
This driver is for support the vpu core.
--- drivers/media/platform/amphion/vpu_codec.h | 67 ++ drivers/media/platform/amphion/vpu_core.c | 906 +++++++++++++++++++++ drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++++++++ drivers/media/platform/amphion/vpu_rpc.c | 279 +++++++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++++++++ 6 files changed, 2226 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.h
drivers/media/platform/amphion/vpu_codec.h new file +67 -0
@@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CODEC_H +#define _AMPHION_VPU_CODEC_H + +struct vpu_encode_params { +u32 input_format; +u32 codec_format; +u32 profile; +u32 tier; +u32 level; +struct v4l2_fract frame_rate; +u32 src_stride; +u32 src_width; +u32 src_height; +struct v4l2_rect crop; +u32 out_width; +u32 out_height; + +u32 gop_length; +u32 bframes; + +u32 rc_mode; +u32 bitrate; +u32 bitrate_min; +u32 bitrate_max; + +u32 i_frame_qp; +u32 p_frame_qp; +u32 b_frame_qp; +u32 qp_min; +u32 qp_max; +u32 qp_min_i; +u32 qp_max_i; + +struct { +u32 enable; +u32 idc; +u32 width; +u32 height; + } sar; + +struct { +u32 primaries; +u32 transfer; +u32 matrix; +u32 full_range; + } color; +}; + +struct vpu_decode_params { +u32 codec_format; +u32 output_format; +u32 b_dis_reorder; +u32 b_non_frame; +u32 frame_count; +u32 end_flag; +struct { +u32 base; +u32 size; + } udata; +}; + +#endif
drivers/media/platform/amphion/vpu_core.c new file +906 -0
906 lines not on this page · raw message
drivers/media/platform/amphion/vpu_core.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CORE_H +#define _AMPHION_VPU_CORE_H + +void csr_writel (struct vpu_core *core,u32 reg,u32 val); +u32 csr_readl (struct vpu_core *core,u32 reg); +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf); +void vpu_free_dma (struct vpu_buffer *buf); +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index); + +#endif
drivers/media/platform/amphion/vpu_dbg.c new file +495 -0
495 lines not on this page · raw message
drivers/media/platform/amphion/vpu_rpc.c new file +279 -0
279 lines not on this page · raw message
drivers/media/platform/amphion/vpu_rpc.h new file +464 -0
464 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v13 04/13] media: amphion: add vpu core driver
Quoted 2 lines · Ming Qian
The vpu supports encoder and decoder.
it needs mu core to handle it.
"mu core"? Do you mean "vpu core"? If not, then what is a "mu core"?
Regards,
Hans
Quoted 2,285 lines
core will run either encoder or decoder firmware.
This driver is for support the vpu core.
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- drivers/media/platform/amphion/vpu_codec.h | 67 ++ drivers/media/platform/amphion/vpu_core.c | 906 +++++++++++++++++++++ drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++++++++ drivers/media/platform/amphion/vpu_rpc.c | 279 +++++++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++++++++ 6 files changed, 2226 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.hdrivers/media/platform/amphion/vpu_codec.h new file +67
-0@@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CODEC_H +#define _AMPHION_VPU_CODEC_H + +struct vpu_encode_params { +u32 input_format; +u32 codec_format; +u32 profile; +u32 tier; +u32 level; +struct v4l2_fract frame_rate; +u32 src_stride; +u32 src_width; +u32 src_height; +struct v4l2_rect crop; +u32 out_width; +u32 out_height; + +u32 gop_length; +u32 bframes; + +u32 rc_mode; +u32 bitrate; +u32 bitrate_min; +u32 bitrate_max; + +u32 i_frame_qp; +u32 p_frame_qp; +u32 b_frame_qp; +u32 qp_min; +u32 qp_max; +u32 qp_min_i; +u32 qp_max_i; + +struct { +u32 enable; +u32 idc; +u32 width; +u32 height; + } sar; + +struct { +u32 primaries; +u32 transfer; +u32 matrix; +u32 full_range; + } color; +}; + +struct vpu_decode_params { +u32 codec_format; +u32 output_format; +u32 b_dis_reorder; +u32 b_non_frame; +u32 frame_count; +u32 end_flag; +struct { +u32 base; +u32 size; + } udata; +}; + +#endif drivers/media/platform/amphion/vpu_core.c new file +906
-0@@ -0,0 +1,906 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <linux/pm_domain.h> +#include <linux/firmware.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_core.h" +#include "vpu_mbox.h" +#include "vpu_msgs.h" +#include "vpu_rpc.h" +#include "vpu_cmds.h" + +void csr_writel (struct vpu_core *core,u32 reg,u32 val) +{ +writel (val, core->base + reg); +} + +u32 csr_readl (struct vpu_core *core,u32 reg) +{ +return readl (core->base + reg); +} + +static int vpu_core_load_firmware (struct vpu_core *core) +{ +const struct firmware *pfw =NULL ; +int ret =0 ; + +WARN_ON (!core || !core->res || !core->res->fwname); +if (!core->fw.virt) { +dev_err (core->dev,"firmware buffer is not ready\n" ); +return -EINVAL ; + } + + ret =request_firmware (&pfw, core->res->fwname, core->dev); +dev_dbg (core->dev,"request_firmware %s : %d\n" , core->res->fwname, ret); +if (ret) { +dev_err (core->dev,"request firmware %s failed, ret = %d\n" , + core->res->fwname, ret); +return ret; + } + +if (core->fw.length < pfw->size) { +dev_err (core->dev,"firmware buffer size want %zu, but %d\n" , + pfw->size, core->fw.length); + ret = -EINVAL ; + goto exit; + } + +memset_io (core->fw.virt,0 , core->fw.length); +memcpy (core->fw.virt, pfw->data, pfw->size); + core->fw.bytesused = pfw->size; + ret =vpu_iface_on_firmware_loaded (core); +exit: +release_firmware (pfw); + pfw =NULL ; + +return ret; +} + +static int vpu_core_boot_done (struct vpu_core *core) +{ +u32 fw_version; + + fw_version =vpu_iface_get_version (core); +dev_info (core->dev,"%s firmware version : %d.%d.%d\n" , +vpu_core_type_desc (core->type), + (fw_version >>16 ) &0xff , + (fw_version >>8 ) &0xff , + fw_version &0xff ); + core->supported_instance_count =vpu_iface_get_max_instance_count (core); +if (core->res->act_size) { +u32 count = core->act.length / core->res->act_size; + + core->supported_instance_count =min (core->supported_instance_count, count); + } + core->fw_version = fw_version; + core->state =VPU_CORE_ACTIVE ; + +return 0 ; +} + +static int vpu_core_wait_boot_done (struct vpu_core *core) +{ +int ret; + + ret =wait_for_completion_timeout (&core->cmp,VPU_TIMEOUT ); +if (!ret) { +dev_err (core->dev,"boot timeout\n" ); +return -EINVAL ; + } +return vpu_core_boot_done (core); +} + +static int vpu_core_boot (struct vpu_core *core,bool load) +{ +int ret; + +WARN_ON (!core); + +if (!core->res->standalone) +return 0 ; + +reinit_completion (&core->cmp); +if (load) { + ret =vpu_core_load_firmware (core); +if (ret) +return ret; + } + +vpu_iface_boot_core (core); +return vpu_core_wait_boot_done (core); +} + +static int vpu_core_shutdown (struct vpu_core *core) +{ +if (!core->res->standalone) +return 0 ; +return vpu_iface_shutdown_core (core); +} + +static int vpu_core_restore (struct vpu_core *core) +{ +int ret; + +if (!core->res->standalone) +return 0 ; + ret =vpu_core_sw_reset (core); +if (ret) +return ret; + +vpu_core_boot_done (core); +return vpu_iface_restore_core (core); +} + +static int __vpu_alloc_dma (struct device *dev,struct vpu_buffer *buf) +{ +gfp_t gfp =GFP_KERNEL |GFP_DMA32 ; + +WARN_ON (!dev || !buf); + +if (!buf->length) +return 0 ; + + buf->virt =dma_alloc_coherent (dev, buf->length, &buf->phys, gfp); +if (!buf->virt) +return -ENOMEM ; + + buf->dev = dev; + +return 0 ; +} + +void vpu_free_dma (struct vpu_buffer *buf) +{ +WARN_ON (!buf); + +if (!buf->virt || !buf->dev) +return ; + +dma_free_coherent (buf->dev, buf->length, buf->virt, buf->phys); + buf->virt =NULL ; + buf->phys =0 ; + buf->length =0 ; + buf->bytesused =0 ; + buf->dev =NULL ; +} + +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf) +{ +WARN_ON (!core || !buf); + +return __vpu_alloc_dma (core->dev, buf); +} + +static void vpu_core_check_hang (struct vpu_core *core) +{ +if (core->hang_mask) + core->state =VPU_CORE_HANG ; +} + +static struct vpu_core *vpu_core_find_proper_by_type (struct vpu_dev *vpu,u32 type) +{ +struct vpu_core *core =NULL ; +int request_count =INT_MAX ; +struct vpu_core *c; + +WARN_ON (!vpu); + +list_for_each_entry (c, &vpu->cores, list) { +dev_dbg (c->dev,"instance_mask = 0x%lx, state = %d\n" , + c->instance_mask, + c->state); +if (c->type != type) +continue ; +if (c->state ==VPU_CORE_DEINIT ) { + core = c; +break ; + } +vpu_core_check_hang (c); +if (c->state !=VPU_CORE_ACTIVE ) +continue ; +if (c->request_count < request_count) { + request_count = c->request_count; + core = c; + } +if (!request_count) +break ; + } + +return core; +} + +static bool vpu_core_is_exist (struct vpu_dev *vpu,struct vpu_core *core) +{ +struct vpu_core *c; + +list_for_each_entry (c, &vpu->cores, list) { +if (c == core) +return true; + } + +return false; +} + +static void vpu_core_get_vpu (struct vpu_core *core) +{ + core->vpu->get_vpu (core->vpu); +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->get_enc (core->vpu); +if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->get_dec (core->vpu); +} + +static int vpu_core_register (struct device *dev,struct vpu_core *core) +{ +struct vpu_dev *vpu =dev_get_drvdata (dev); +int ret =0 ; + +dev_dbg (core->dev,"register core %s\n" ,vpu_core_type_desc (core->type)); +if (vpu_core_is_exist (vpu, core)) +return 0 ; + + core->workqueue =alloc_workqueue ("vpu" ,WQ_UNBOUND |WQ_MEM_RECLAIM ,1 ); +if (!core->workqueue) { +dev_err (core->dev,"fail to alloc workqueue\n" ); +return -ENOMEM ; + } +INIT_WORK (&core->msg_work, vpu_msg_run_work); +INIT_DELAYED_WORK (&core->msg_delayed_work, vpu_msg_delayed_work); + core->msg_buffer_size =roundup_pow_of_two (VPU_MSG_BUFFER_SIZE ); + core->msg_buffer =vzalloc (core->msg_buffer_size); +if (!core->msg_buffer) { +dev_err (core->dev,"failed allocate buffer for fifo\n" ); + ret = -ENOMEM ; + goto error; + } + ret =kfifo_init (&core->msg_fifo, core->msg_buffer, core->msg_buffer_size); +if (ret) { +dev_err (core->dev,"failed init kfifo\n" ); + goto error; + } + +list_add_tail (&core->list, &vpu->cores); + +vpu_core_get_vpu (core); + +if (vpu_iface_get_power_state (core)) + ret =vpu_core_restore (core); +if (ret) + goto error; + +return 0 ; +error: +if (core->msg_buffer) { +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + } +if (core->workqueue) { +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } +return ret; +} + +static void vpu_core_put_vpu (struct vpu_core *core) +{ +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->put_enc (core->vpu); +if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->put_dec (core->vpu); + core->vpu->put_vpu (core->vpu); +} + +static int vpu_core_unregister (struct device *dev,struct vpu_core *core) +{ +list_del_init (&core->list); + +vpu_core_put_vpu (core); + core->vpu =NULL ; +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + +if (core->workqueue) { +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } + +return 0 ; +} + +static int vpu_core_acquire_instance (struct vpu_core *core) +{ +int id; + +WARN_ON (!core); + + id =ffz (core->instance_mask); +if (id >= core->supported_instance_count) +return -EINVAL ; + +set_bit (id, &core->instance_mask); + +return id; +} + +static void vpu_core_release_instance (struct vpu_core *core,int id) +{ +WARN_ON (!core); + +if (id <0 || id >= core->supported_instance_count) +return ; + +clear_bit (id, &core->instance_mask); +} + +struct vpu_inst *vpu_inst_get (struct vpu_inst *inst) +{ +if (!inst) +return NULL ; + +atomic_inc (&inst->ref_count); + +return inst; +} + +void vpu_inst_put (struct vpu_inst *inst) +{ +if (!inst) +return ; +if (atomic_dec_and_test (&inst->ref_count)) { +if (inst->release) + inst->release (inst); + } +} + +struct vpu_core *vpu_request_core (struct vpu_dev *vpu,enum vpu_core_type type) +{ +struct vpu_core *core =NULL ; +int ret; + +mutex_lock (&vpu->lock); + + core =vpu_core_find_proper_by_type (vpu, type); +if (!core) + goto exit; + +mutex_lock (&core->lock); +pm_runtime_get_sync (core->dev); + +if (core->state ==VPU_CORE_DEINIT ) { + ret =vpu_core_boot (core, true); +if (ret) { +pm_runtime_put_sync (core->dev); +mutex_unlock (&core->lock); + core =NULL ; + goto exit; + } + } + + core->request_count++; + +mutex_unlock (&core->lock); +exit: +mutex_unlock (&vpu->lock); + +return core; +} + +void vpu_release_core (struct vpu_core *core) +{ +if (!core) +return ; + +mutex_lock (&core->lock); +pm_runtime_put_sync (core->dev); +if (core->request_count) + core->request_count--; +mutex_unlock (&core->lock); +} + +int vpu_inst_register (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core; +int ret =0 ; + +WARN_ON (!inst || !inst->vpu); + + vpu = inst->vpu; + core = inst->core; +if (!core) { + core =vpu_request_core (vpu, inst->type); +if (!core) { +dev_err (vpu->dev,"there is no vpu core for %s\n" , +vpu_core_type_desc (inst->type)); +return -EINVAL ; + } + inst->core = core; + inst->dev =get_device (core->dev); + } + +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) + goto exit; + + ret =vpu_core_acquire_instance (core); +if (ret <0 ) + goto exit; + +vpu_trace (inst->dev,"[%d] %p\n" , ret, inst); + inst->id = ret; +list_add_tail (&inst->list, &core->instances); + ret =0 ; +if (core->res->act_size) { + inst->act.phys = core->act.phys + core->res->act_size * inst->id; + inst->act.virt = core->act.virt + core->res->act_size * inst->id; + inst->act.length = core->res->act_size; + } +vpu_inst_create_dbgfs_file (inst); +exit: +mutex_unlock (&core->lock); + +if (ret) +dev_err (core->dev,"register instance fail\n" ); +return ret; +} + +int vpu_inst_unregister (struct vpu_inst *inst) +{ +struct vpu_core *core; + +WARN_ON (!inst); + +if (!inst->core) +return 0 ; + + core = inst->core; +vpu_clear_request (inst); +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) { +vpu_inst_remove_dbgfs_file (inst); +list_del_init (&inst->list); +vpu_core_release_instance (core, inst->id); + inst->id =VPU_INST_NULL_ID ; + } +vpu_core_check_hang (core); +if (core->state ==VPU_CORE_HANG && !core->instance_mask) { +dev_info (core->dev,"reset hang core\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); + +return 0 ; +} + +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index) +{ +struct vpu_inst *inst =NULL ; +struct vpu_inst *tmp; + +mutex_lock (&core->lock); +if (!test_bit (index, &core->instance_mask)) + goto exit; +list_for_each_entry (tmp, &core->instances, list) { +if (tmp->id == index) { + inst =vpu_inst_get (tmp); +break ; + } + } +exit: +mutex_unlock (&core->lock); + +return inst; +} + +const struct vpu_core_resources *vpu_get_resource (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core =NULL ; +const struct vpu_core_resources *res =NULL ; + +if (!inst || !inst->vpu) +return NULL ; + +if (inst->core && inst->core->res) +return inst->core->res; + + vpu = inst->vpu; +mutex_lock (&vpu->lock); +list_for_each_entry (core, &vpu->cores, list) { +if (core->type == inst->type) { + res = core->res; +break ; + } + } +mutex_unlock (&vpu->lock); + +return res; +} + +static int vpu_core_parse_dt (struct vpu_core *core,struct device_node *np) +{ +struct device_node *node; +struct resource res; + +if (of_count_phandle_with_args (np,"memory-region" ,NULL ) <2 ) { +dev_err (core->dev,"need 2 memory-region for boot and rpc\n" ); +return -ENODEV ; + } + + node =of_parse_phandle (np,"memory-region" ,0 ); +if (!node) { +dev_err (core->dev,"boot-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { +dev_err (core->dev,"boot-region of_address_to_resource error\n" ); +return -EINVAL ; + } + core->fw.phys = res.start; + core->fw.length =resource_size (&res); + + node =of_parse_phandle (np,"memory-region" ,1 ); +if (!node) { +dev_err (core->dev,"rpc-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { +dev_err (core->dev,"rpc-region of_address_to_resource error\n" ); +return -EINVAL ; + } + core->rpc.phys = res.start; + core->rpc.length =resource_size (&res); + +if (core->rpc.length < core->res->rpc_size + core->res->fwlog_size) { +dev_err (core->dev,"the rpc-region <%pad, 0x%x> is not enough\n" , + &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->fw.virt =ioremap_wc (core->fw.phys, core->fw.length); + core->rpc.virt =ioremap_wc (core->rpc.phys, core->rpc.length); +memset_io (core->rpc.virt,0 , core->rpc.length); + +if (vpu_iface_check_memory_region (core, + core->rpc.phys, + core->rpc.length) !=VPU_CORE_MEMORY_UNCACHED ) { +dev_err (core->dev,"rpc region<%pad, 0x%x> isn't uncached\n" , + &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->log.phys = core->rpc.phys + core->res->rpc_size; + core->log.virt = core->rpc.virt + core->res->rpc_size; + core->log.length = core->res->fwlog_size; + core->act.phys = core->log.phys + core->log.length; + core->act.virt = core->log.virt + core->log.length; + core->act.length = core->rpc.length - core->res->rpc_size - core->log.length; + core->rpc.length = core->res->rpc_size; + +return 0 ; +} + +static int vpu_core_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core; +struct vpu_dev *vpu =dev_get_drvdata (dev->parent); +struct vpu_shared_addr *iface; +u32 iface_data_size; +int ret; + +dev_dbg (dev,"probe\n" ); +if (!vpu) +return -EINVAL ; + core =devm_kzalloc (dev,sizeof (*core),GFP_KERNEL ); +if (!core) +return -ENOMEM ; + + core->pdev = pdev; + core->dev = dev; +platform_set_drvdata (pdev, core); + core->vpu = vpu; +INIT_LIST_HEAD (&core->instances); +mutex_init (&core->lock); +mutex_init (&core->cmd_lock); +init_completion (&core->cmp); +init_waitqueue_head (&core->ack_wq); + core->state =VPU_CORE_DEINIT ; + + core->res =of_device_get_match_data (dev); +if (!core->res) +return -ENODEV ; + + core->type = core->res->type; + core->id =of_alias_get_id (dev->of_node,"vpu_core" ); +if (core->id <0 ) { +dev_err (dev,"can't get vpu core id\n" ); +return core->id; + } +dev_info (core->dev,"[%d] = %s\n" , core->id,vpu_core_type_desc (core->type)); + ret =vpu_core_parse_dt (core, dev->of_node); +if (ret) +return ret; + + core->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (core->base)) +return PTR_ERR (core->base); + +if (!vpu_iface_check_codec (core)) { +dev_err (core->dev,"is not supported\n" ); +return -EINVAL ; + } + + ret =vpu_mbox_init (core); +if (ret) +return ret; + + iface =devm_kzalloc (dev,sizeof (*iface),GFP_KERNEL ); +if (!iface) +return -ENOMEM ; + + iface_data_size =vpu_iface_get_data_size (core); +if (iface_data_size) { + iface->priv =devm_kzalloc (dev, iface_data_size,GFP_KERNEL ); +if (!iface->priv) +return -ENOMEM ; + } + + ret =vpu_iface_init (core, iface, &core->rpc, core->fw.phys); +if (ret) { +dev_err (core->dev,"init iface fail, ret = %d\n" , ret); +return ret; + } + +vpu_iface_config_system (core, vpu->res->mreg_base, vpu->base); +vpu_iface_set_log_buf (core, &core->log); + +pm_runtime_enable (dev); + ret =pm_runtime_get_sync (dev); +if (ret) { +pm_runtime_put_noidle (dev); +pm_runtime_set_suspended (dev); + goto err_runtime_disable; + } + + ret =vpu_core_register (dev->parent, core); +if (ret) + goto err_core_register; + core->parent = dev->parent; + +pm_runtime_put_sync (dev); +vpu_core_create_dbgfs_file (core); + +return 0 ; + +err_core_register: +pm_runtime_put_sync (dev); +err_runtime_disable: +pm_runtime_disable (dev); + +return ret; +} + +static int vpu_core_remove (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core =platform_get_drvdata (pdev); +int ret; + +vpu_core_remove_dbgfs_file (core); + ret =pm_runtime_get_sync (dev); +WARN_ON (ret <0 ); + +vpu_core_shutdown (core); +pm_runtime_put_sync (dev); +pm_runtime_disable (dev); + +vpu_core_unregister (core->parent, core); +iounmap (core->fw.virt); +iounmap (core->rpc.virt); +mutex_destroy (&core->lock); +mutex_destroy (&core->cmd_lock); + +return 0 ; +} + +static int __maybe_unused vpu_core_runtime_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +return vpu_mbox_request (core); +} + +static int __maybe_unused vpu_core_runtime_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +vpu_mbox_free (core); +return 0 ; +} + +static void vpu_core_cancel_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; + +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); + +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +cancel_work_sync (&inst->msg_work); +mutex_unlock (&core->lock); +} + +static void vpu_core_resume_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; +unsigned long delay =msecs_to_jiffies (10 ); + +queue_work (core->workqueue, &core->msg_work); +queue_delayed_work (core->workqueue, &core->msg_delayed_work, delay); + +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +queue_work (inst->workqueue, &inst->msg_work); +mutex_unlock (&core->lock); +} + +static int __maybe_unused vpu_core_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock); +pm_runtime_get_sync (dev); +vpu_core_get_vpu (core); +if (core->state !=VPU_CORE_SNAPSHOT ) + goto exit; + +if (!vpu_iface_get_power_state (core)) { +if (!list_empty (&core->instances)) { + ret =vpu_core_boot (core, false); +if (ret) { +dev_err (core->dev,"%s boot fail\n" , __func__); + core->state =VPU_CORE_DEINIT ; + goto exit; + } + }else { + core->state =VPU_CORE_DEINIT ; + } + }else { +if (!list_empty (&core->instances)) { + ret =vpu_core_sw_reset (core); +if (ret) { +dev_err (core->dev,"%s sw_reset fail\n" , __func__); + core->state =VPU_CORE_HANG ; + goto exit; + } + } + core->state =VPU_CORE_ACTIVE ; + } + +exit: +pm_runtime_put_sync (dev); +mutex_unlock (&core->lock); + +vpu_core_resume_work (core); +return ret; +} + +static int __maybe_unused vpu_core_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock); +if (core->state ==VPU_CORE_ACTIVE ) { +if (!list_empty (&core->instances)) { + ret =vpu_core_snapshot (core); +if (ret) { +mutex_unlock (&core->lock); +return ret; + } + } + + core->state =VPU_CORE_SNAPSHOT ; + } +mutex_unlock (&core->lock); + +vpu_core_cancel_work (core); + +mutex_lock (&core->lock); +vpu_core_put_vpu (core); +mutex_unlock (&core->lock); +return ret; +} + +static const struct dev_pm_ops vpu_core_pm_ops = { + SET_RUNTIME_PM_OPS(vpu_core_runtime_suspend, vpu_core_runtime_resume, NULL) +SET_SYSTEM_SLEEP_PM_OPS (vpu_core_suspend, vpu_core_resume) +}; + +static struct vpu_core_resources imx8q_enc = { + .type =VPU_CORE_TYPE_ENC , + .fwname ="vpu/vpu_fw_imx8_enc.bin" , + .stride =16 , + .max_width =1920 , + .max_height =1920 , + .min_width =64 , + .min_height =48 , + .step_width =2 , + .step_height =2 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .act_size =0xc0000 , + .standalone = true, +}; + +static struct vpu_core_resources imx8q_dec = { + .type =VPU_CORE_TYPE_DEC , + .fwname ="vpu/vpu_fw_imx8_dec.bin" , + .stride =256 , + .max_width =8188 , + .max_height =8188 , + .min_width =16 , + .min_height =16 , + .step_width =1 , + .step_height =1 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .standalone = true, +}; + +static const struct of_device_id vpu_core_dt_match[] = { + { .compatible ="nxp,imx8q-vpu-encoder" , .data = &imx8q_enc }, + { .compatible ="nxp,imx8q-vpu-decoder" , .data = &imx8q_dec }, + {} +}; +MODULE_DEVICE_TABLE (of, vpu_core_dt_match); + +static struct platform_driver amphion_vpu_core_driver = { + .probe = vpu_core_probe, + .remove = vpu_core_remove, + .driver = { + .name ="amphion-vpu-core" , + .of_match_table = vpu_core_dt_match, + .pm = &vpu_core_pm_ops, + }, +}; + +int __init vpu_core_driver_init (void ) +{ +return platform_driver_register (&hion_vpu_core_driver); +} + +void __exit vpu_core_driver_exit(void) +{ +platform_driver_unregister (&hion_vpu_core_driver); +}drivers/media/platform/amphion/vpu_core.h new file +15
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CORE_H +#define _AMPHION_VPU_CORE_H + +void csr_writel (struct vpu_core *core,u32 reg,u32 val); +u32 csr_readl (struct vpu_core *core,u32 reg); +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf); +void vpu_free_dma (struct vpu_buffer *buf); +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index); + +#endif drivers/media/platform/amphion/vpu_dbg.c new file +495
-0@@ -0,0 +1,495 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <media/v4l2-device.h> +#include <linux/debugfs.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_helpers.h" +#include "vpu_cmds.h" +#include "vpu_rpc.h" + +struct print_buf_desc { +u32 start_h_phy; +u32 start_h_vir; +u32 start_m; +u32 bytes; +u32 read; +u32 write; +char buffer[0 ]; +}; + +static char *vb2_stat_name[] = { + [VB2_BUF_STATE_DEQUEUED ] ="dequeued" , + [VB2_BUF_STATE_IN_REQUEST ] ="in_request" , + [VB2_BUF_STATE_PREPARING ] ="preparing" , + [VB2_BUF_STATE_QUEUED ] ="queued" , + [VB2_BUF_STATE_ACTIVE ] ="active" , + [VB2_BUF_STATE_DONE ] ="done" , + [VB2_BUF_STATE_ERROR ] ="error" , +}; + +static char *vpu_stat_name[] = { + [VPU_BUF_STATE_IDLE ] ="idle" , + [VPU_BUF_STATE_INUSE ] ="inuse" , + [VPU_BUF_STATE_DECODED ] ="decoded" , + [VPU_BUF_STATE_READY ] ="ready" , + [VPU_BUF_STATE_SKIP ] ="skip" , + [VPU_BUF_STATE_ERROR ] ="error" , +}; + +static int vpu_dbg_instance (struct seq_file *s,void *data) +{ +struct vpu_inst *inst = s->private; +char str[128 ]; +int num; +struct vb2_queue *vq; +int i; + + num =scnprintf (str,sizeof (str),"[%s]\n" ,vpu_core_type_desc (inst->type)); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"tgig = %d,pid = %d\n" , inst->tgid, inst->pid); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"state = %d\n" , inst->state); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str), +"min_buffer_out = %d, min_buffer_cap = %d\n" , + inst->min_buffer_out, inst->min_buffer_cap); +if (seq_write (s, str, num)) +return 0 ; + + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); + num =scnprintf (str,sizeof (str), +"output (%2d, %2d): fmt = %c%c%c%c %d x %d, %d;" , +vb2_is_streaming (vq), + vq->num_buffers, + inst->out_format.pixfmt, + inst->out_format.pixfmt >>8 , + inst->out_format.pixfmt >>16 , + inst->out_format.pixfmt >>24 , + inst->out_format.width, + inst->out_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->out_format.num_planes; i++) { + num =scnprintf (str,sizeof (str)," %d(%d)" , + inst->out_format.sizeimage[i], + inst->out_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + num =scnprintf (str,sizeof (str), +"capture(%2d, %2d): fmt = %c%c%c%c %d x %d, %d;" , +vb2_is_streaming (vq), + vq->num_buffers, + inst->cap_format.pixfmt, + inst->cap_format.pixfmt >>8 , + inst->cap_format.pixfmt >>16 , + inst->cap_format.pixfmt >>24 , + inst->cap_format.width, + inst->cap_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->cap_format.num_planes; i++) { + num =scnprintf (str,sizeof (str)," %d(%d)" , + inst->cap_format.sizeimage[i], + inst->cap_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + num =scnprintf (str,sizeof (str),"crop: (%d, %d) %d x %d\n" , + inst->crop.left, + inst->crop.top, + inst->crop.width, + inst->crop.height); +if (seq_write (s, str, num)) +return 0 ; + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"output [%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]); +if (seq_write (s, str, num)) +return 0 ; + } + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"capture[%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]); +if (seq_write (s, str, num)) +return 0 ; + } + + num =scnprintf (str,sizeof (str),"sequence = %d\n" , inst->sequence); +if (seq_write (s, str, num)) +return 0 ; + +if (inst->use_stream_buffer) { + num =scnprintf (str,sizeof (str),"stream_buffer = %d / %d, <%pad, 0x%x>\n" , +vpu_helper_get_used_space (inst), + inst->stream_buffer.length, + &inst->stream_buffer.phys, + inst->stream_buffer.length); +if (seq_write (s, str, num)) +return 0 ; + } + num =scnprintf (str,sizeof (str),"kfifo len = 0x%x\n" ,kfifo_len (&inst->msg_fifo)); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"flow :\n" ); +if (seq_write (s, str, num)) +return 0 ; + +mutex_lock (&inst->core->cmd_lock); +for (i =0 ; i <ARRAY_SIZE (inst->flows); i++) { +u32 idx = (inst->flow_idx + i) % (ARRAY_SIZE (inst->flows)); + +if (!inst->flows[idx]) +continue ; + num =scnprintf (str,sizeof (str),"\t[%s]0x%x\n" , + inst->flows[idx] >=VPU_MSG_ID_NOOP ?"M" :"C" , + inst->flows[idx]); +if (seq_write (s, str, num)) { +mutex_unlock (&inst->core->cmd_lock); +return 0 ; + } + } +mutex_unlock (&inst->core->cmd_lock); + + i =0 ; +while (true) { + num =call_vop (inst, get_debug_info, str,sizeof (str), i++); +if (num <=0 ) +break ; +if (seq_write (s, str, num)) +return 0 ; + } + +return 0 ; +} + +static int vpu_dbg_core (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct vpu_shared_addr *iface = core->iface; +char str[128 ]; +int num; + + num =scnprintf (str,sizeof (str),"[%s]\n" ,vpu_core_type_desc (core->type)); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"boot_region = <%pad, 0x%x>\n" , + &core->fw.phys, core->fw.length); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"rpc_region = <%pad, 0x%x> used = 0x%x\n" , + &core->rpc.phys, core->rpc.length, core->rpc.bytesused); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"fwlog_region = <%pad, 0x%x>\n" , + &core->log.phys, core->log.length); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"state = %d\n" , core->state); +if (seq_write (s, str, num)) +return 0 ; +if (core->state ==VPU_CORE_DEINIT ) +return 0 ; + num =scnprintf (str,sizeof (str),"fw version = %d.%d.%d\n" , + (core->fw_version >>16 ) &0xff , + (core->fw_version >>8 ) &0xff , + core->fw_version &0xff ); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"instances = %d/%d (0x%02lx), %d\n" , +hweight32 (core->instance_mask), + core->supported_instance_count, + core->instance_mask, + core->request_count); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"kfifo len = 0x%x\n" ,kfifo_len (&core->msg_fifo)); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str), +"cmd_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n" , + iface->cmd_desc->start, + iface->cmd_desc->end, + iface->cmd_desc->wptr, + iface->cmd_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str), +"msg_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n" , + iface->msg_desc->start, + iface->msg_desc->end, + iface->msg_desc->wptr, + iface->msg_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + +return 0 ; +} + +static int vpu_dbg_fwlog (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct print_buf_desc *print_buf; +int length; +u32 rptr; +u32 wptr; +int ret =0 ; + +if (!core->log.virt || core->state ==VPU_CORE_DEINIT ) +return 0 ; + + print_buf = core->log.virt; + rptr = print_buf->read; + wptr = print_buf->write; + +if (rptr == wptr) +return 0 ; +else if (rptr < wptr) + length = wptr - rptr; +else + length = print_buf->bytes + wptr - rptr; + +if (s->count + length >= s->size) { + s->count = s->size; +return 0 ; + } + +if (rptr + length >= print_buf->bytes) { +int num = print_buf->bytes - rptr; + +if (seq_write (s, print_buf->buffer + rptr, num)) + ret =-1 ; + length -= num; + rptr =0 ; + } + +if (length) { +if (seq_write (s, print_buf->buffer + rptr, length)) + ret =-1 ; + rptr += length; + } +if (!ret) + print_buf->read = rptr; + +return 0 ; +} + +static int vpu_dbg_inst_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_instance, inode->i_private); +} + +static ssize_t vpu_dbg_inst_write (struct file *file, +const char __user *user_buf,size_t size,loff_t *ppos) +{ +struct seq_file *s = file->private_data; +struct vpu_inst *inst = s->private; + +vpu_session_debug (inst); + +return size; +} + +static ssize_t vpu_dbg_core_write (struct file *file, +const char __user *user_buf,size_t size,loff_t *ppos) +{ +struct seq_file *s = file->private_data; +struct vpu_core *core = s->private; + +pm_runtime_get_sync (core->dev); +mutex_lock (&core->lock); +if (core->state !=VPU_CORE_DEINIT && !core->instance_mask) { +dev_info (core->dev,"reset\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); +pm_runtime_put_sync (core->dev); + +return size; +} + +static int vpu_dbg_core_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_core, inode->i_private); +} + +static int vpu_dbg_fwlog_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_fwlog, inode->i_private); +} + +static const struct file_operations vpu_dbg_inst_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_inst_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_inst_write, +}; + +static const struct file_operations vpu_dbg_core_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_core_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_core_write, +}; + +static const struct file_operations vpu_dbg_fwlog_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_fwlog_open, + .release = single_release, + .read = seq_read, +}; + +int vpu_inst_create_dbgfs_file (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!inst || !inst->core || !inst->core->vpu) +return -EINVAL ; + + vpu = inst->core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (inst->debugfs) +return 0 ; + +scnprintf (name,sizeof (name),"instance.%d.%d" , + inst->core->id, inst->id); + inst->debugfs =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0644 ), + vpu->debugfs, + inst, + &vpu_dbg_inst_fops); +if (!inst->debugfs) { +dev_err (inst->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + +return 0 ; +} + +int vpu_inst_remove_dbgfs_file (struct vpu_inst *inst) +{ +if (!inst) +return 0 ; + +debugfs_remove (inst->debugfs); + inst->debugfs =NULL ; + +return 0 ; +} + +int vpu_core_create_dbgfs_file (struct vpu_core *core) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!core || !core->vpu) +return -EINVAL ; + + vpu = core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (!core->debugfs) { +scnprintf (name,sizeof (name),"core.%d" , core->id); + core->debugfs =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0644 ), + vpu->debugfs, + core, + &vpu_dbg_core_fops); +if (!core->debugfs) { +dev_err (core->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + } +if (!core->debugfs_fwlog) { +scnprintf (name,sizeof (name),"fwlog.%d" , core->id); + core->debugfs_fwlog =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0444 ), + vpu->debugfs, + core, + &vpu_dbg_fwlog_fops); +if (!core->debugfs_fwlog) { +dev_err (core->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + } + +return 0 ; +} + +int vpu_core_remove_dbgfs_file (struct vpu_core *core) +{ +if (!core) +return 0 ; +debugfs_remove (core->debugfs); + core->debugfs =NULL ; +debugfs_remove (core->debugfs_fwlog); + core->debugfs_fwlog =NULL ; + +return 0 ; +} + +void vpu_inst_record_flow (struct vpu_inst *inst,u32 flow) +{ +if (!inst) +return ; + + inst->flows[inst->flow_idx] = flow; + inst->flow_idx = (inst->flow_idx +1 ) % (ARRAY_SIZE (inst->flows)); +}drivers/media/platform/amphion/vpu_rpc.c new file +279
-0@@ -0,0 +1,279 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/firmware/imx/ipc.h> +#include <linux/firmware/imx/svc/misc.h> +#include "vpu.h" +#include "vpu_rpc.h" +#include "vpu_imx8q.h" +#include "vpu_windsor.h" +#include "vpu_malone.h" + +u32 vpu_iface_check_memory_region (struct vpu_core *core,dma_addr_t addr,u32 size) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->check_memory_region) +return VPU_CORE_MEMORY_INVALID ; + +return ops->check_memory_region (core->fw.phys, addr, size); +} + +static u32 vpu_rpc_check_buffer_space (struct vpu_rpc_buffer_desc *desc,bool write) +{ +u32 ptr1; +u32 ptr2; +u32 size; + +WARN_ON (!desc); + + size = desc->end - desc->start; +if (write) { + ptr1 = desc->wptr; + ptr2 = desc->rptr; + }else { + ptr1 = desc->rptr; + ptr2 = desc->wptr; + } + +if (ptr1 == ptr2) { +if (!write) +return 0 ; +else +return size; + } + +return (ptr2 + size - ptr1) % size; +} + +static int vpu_rpc_send_cmd_buf (struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 *data; +u32 wptr; +u32 i; + +WARN_ON (!shared || !shared->cmd_mem_vir || !cmd); + + desc = shared->cmd_desc; + space =vpu_rpc_check_buffer_space (desc, true); +if (space < (((cmd->hdr.num +1 ) <<2 ) +16 )) { +pr_err ("Cmd Buffer is no space for [%d] %d\n" , + cmd->hdr.index, cmd->hdr.id); +return -EINVAL ; + } + wptr = desc->wptr; + data = (u32 *)(shared->cmd_mem_vir + desc->wptr - desc->start); + *data =0 ; + *data |= ((cmd->hdr.index &0xff ) <<24 ); + *data |= ((cmd->hdr.num &0xff ) <<16 ); + *data |= (cmd->hdr.id &0x3fff ); + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + +for (i =0 ; i < cmd->hdr.num; i++) { + *data = cmd->data[i]; + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + } + +/*update wptr after data is written*/ +mb (); + desc->wptr = wptr; + +return 0 ; +} + +static bool vpu_rpc_check_msg (struct vpu_shared_addr *shared) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 msgword; +u32 msgnum; + +WARN_ON (!shared || !shared->msg_desc); + + desc = shared->msg_desc; + space =vpu_rpc_check_buffer_space (desc,0 ); + space = (space >>2 ); + +if (space) { + msgword = *(u32 *)(shared->msg_mem_vir + desc->rptr - desc->start); + msgnum = (msgword &0xff0000 ) >>16 ; +if (msgnum <= space) +return true; + } + +return false; +} + +static int vpu_rpc_receive_msg_buf (struct vpu_shared_addr *shared,struct vpu_rpc_event *msg) +{ +struct vpu_rpc_buffer_desc *desc; +u32 *data; +u32 msgword; +u32 rptr; +u32 i; + +WARN_ON (!shared || !shared->msg_desc || !msg); + +if (!vpu_rpc_check_msg (shared)) +return -EINVAL ; + + desc = shared->msg_desc; + data = (u32 *)(shared->msg_mem_vir + desc->rptr - desc->start); + rptr = desc->rptr; + msgword = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + + msg->hdr.index = (msgword >>24 ) &0xff ; + msg->hdr.num = (msgword >>16 ) &0xff ; + msg->hdr.id = msgword &0x3fff ; + +if (msg->hdr.num >ARRAY_SIZE (msg->data)) { +pr_err ("msg(%d) data length(%d) is out of range\n" , + msg->hdr.id, msg->hdr.num); +return -EINVAL ; + } + +for (i =0 ; i < msg->hdr.num; i++) { + msg->data[i] = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + } + +/*update rptr after data is read*/ +mb (); + desc->rptr = rptr; + +return 0 ; +} + +struct vpu_iface_ops imx8q_rpc_ops[] = { + [VPU_CORE_TYPE_ENC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_windsor_get_data_size, + .check_memory_region = vpu_imx8q_check_memory_region, + .init_rpc = vpu_windsor_init_rpc, + .set_log_buf = vpu_windsor_set_log_buf, + .set_system_cfg = vpu_windsor_set_system_cfg, + .get_version = vpu_windsor_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .pack_cmd = vpu_windsor_pack_cmd, + .convert_msg_id = vpu_windsor_convert_msg_id, + .unpack_msg_data = vpu_windsor_unpack_msg_data, + .config_memory_resource = vpu_windsor_config_memory_resource, + .get_stream_buffer_size = vpu_windsor_get_stream_buffer_size, + .config_stream_buffer = vpu_windsor_config_stream_buffer, + .get_stream_buffer_desc = vpu_windsor_get_stream_buffer_desc, + .update_stream_buffer = vpu_windsor_update_stream_buffer, + .set_encode_params = vpu_windsor_set_encode_params, + .input_frame = vpu_windsor_input_frame, + .get_max_instance_count = vpu_windsor_get_max_instance_count, + }, + [VPU_CORE_TYPE_DEC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_malone_get_data_size, + .check_memory_region = vpu_imx8q_check_memory_region, + .init_rpc = vpu_malone_init_rpc, + .set_log_buf = vpu_malone_set_log_buf, + .set_system_cfg = vpu_malone_set_system_cfg, + .get_version = vpu_malone_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .get_stream_buffer_size = vpu_malone_get_stream_buffer_size, + .config_stream_buffer = vpu_malone_config_stream_buffer, + .set_decode_params = vpu_malone_set_decode_params, + .pack_cmd = vpu_malone_pack_cmd, + .convert_msg_id = vpu_malone_convert_msg_id, + .unpack_msg_data = vpu_malone_unpack_msg_data, + .get_stream_buffer_desc = vpu_malone_get_stream_buffer_desc, + .update_stream_buffer = vpu_malone_update_stream_buffer, + .add_scode = vpu_malone_add_scode, + .input_frame = vpu_malone_input_frame, + .pre_send_cmd = vpu_malone_pre_cmd, + .post_send_cmd = vpu_malone_post_cmd, + .init_instance = vpu_malone_init_instance, + .get_max_instance_count = vpu_malone_get_max_instance_count, + }, +}; + + +static struct vpu_iface_ops *vpu_get_iface (struct vpu_dev *vpu,enum vpu_core_type type) +{ +struct vpu_iface_ops *rpc_ops =NULL ; +u32 size =0 ; + +WARN_ON (!vpu || !vpu->res); + +switch (vpu->res->plat_type) { +case IMX8QXP : +case IMX8QM : + rpc_ops = imx8q_rpc_ops; + size =ARRAY_SIZE (imx8q_rpc_ops); +break ; +default : +return NULL ; + } + +if (type >= size) +return NULL ; + +return &rpc_ops[type]; +} + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core) +{ +WARN_ON (!core || !core->vpu); + +return vpu_get_iface (core->vpu, core->type); +} + +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst) +{ +WARN_ON (!inst || !inst->vpu); + +if (inst->core) +return vpu_core_get_iface (inst->core); + +return vpu_get_iface (inst->vpu, inst->type); +}drivers/media/platform/amphion/vpu_rpc.h new file +464
-0@@ -0,0 +1,464 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_RPC_H +#define _AMPHION_VPU_RPC_H + +#include <media/videobuf2-core.h> +#include "vpu_codec.h" + +struct vpu_rpc_buffer_desc { +u32 wptr; +u32 rptr; +u32 start; +u32 end; +}; + +struct vpu_shared_addr { +void *iface; +struct vpu_rpc_buffer_desc *cmd_desc; +void *cmd_mem_vir; +struct vpu_rpc_buffer_desc *msg_desc; +void *msg_mem_vir; + +unsigned long boot_addr; +struct vpu_core *core; +void *priv; +}; + +struct vpu_rpc_event_header { +u32 index; +u32 id; +u32 num; +}; + +struct vpu_rpc_event { +struct vpu_rpc_event_header hdr; +u32 data[128 ]; +}; + +struct vpu_iface_ops { +bool (*check_codec)(enum vpu_core_type type); +bool (*check_fmt)(enum vpu_core_type type,u32 pixelfmt); +u32 (*get_data_size)(void ); +u32 (*check_memory_region)(dma_addr_t base,dma_addr_t addr,u32 size); +int (*boot_core)(struct vpu_core *core); +int (*shutdown_core)(struct vpu_core *core); +int (*restore_core)(struct vpu_core *core); +int (*get_power_state)(struct vpu_core *core); +int (*on_firmware_loaded)(struct vpu_core *core); +void (*init_rpc)(struct vpu_shared_addr *shared, +struct vpu_buffer *rpc,dma_addr_t boot_addr); +void (*set_log_buf)(struct vpu_shared_addr *shared, +struct vpu_buffer *log); +void (*set_system_cfg)(struct vpu_shared_addr *shared, +u32 regs_base,void __iomem *regs,u32 index); +void (*set_stream_cfg)(struct vpu_shared_addr *shared,u32 index); +u32 (*get_version)(struct vpu_shared_addr *shared); +u32 (*get_max_instance_count)(struct vpu_shared_addr *shared); +int (*get_stream_buffer_size)(struct vpu_shared_addr *shared); +int (*send_cmd_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd); +int (*receive_msg_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *msg); +int (*pack_cmd)(struct vpu_rpc_event *pkt,u32 index,u32 id,void *data); +int (*convert_msg_id)(u32 msg_id); +int (*unpack_msg_data)(struct vpu_rpc_event *pkt,void *data); +int (*input_frame)(struct vpu_shared_addr *shared, +struct vpu_inst *inst,struct vb2_buffer *vb); +int (*config_memory_resource)(struct vpu_shared_addr *shared, +u32 instance, +u32 type, +u32 index, +struct vpu_buffer *buf); +int (*config_stream_buffer)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *buf); +int (*update_stream_buffer)(struct vpu_shared_addr *shared, +u32 instance,u32 ptr,bool write); +int (*get_stream_buffer_desc)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_rpc_buffer_desc *desc); +int (*set_encode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_encode_params *params,u32 update); +int (*set_decode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_decode_params *params,u32 update); +int (*add_scode)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *stream_buffer, +u32 pixelformat, +u32 scode_type); +int (*pre_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*post_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*init_instance)(struct vpu_shared_addr *shared,u32 instance); +}; + +enum { +VPU_CORE_MEMORY_INVALID =0 , +VPU_CORE_MEMORY_CACHED , +VPU_CORE_MEMORY_UNCACHED +}; + +struct vpu_rpc_region_t { +dma_addr_t start; +dma_addr_t end; +dma_addr_t type; +}; + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core); +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst); +u32 vpu_iface_check_memory_region (struct vpu_core *core,dma_addr_t addr,u32 size); + +static inline bool vpu_iface_check_codec (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->check_codec) +return ops->check_codec (core->type); + +return true; +} + +static inline bool vpu_iface_check_format (struct vpu_inst *inst,u32 pixelfmt) +{ +struct vpu_iface_ops *ops =vpu_inst_get_iface (inst); + +if (ops && ops->check_fmt) +return ops->check_fmt (inst->type, pixelfmt); + +return true; +} + +static inline int vpu_iface_boot_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->boot_core) +return ops->boot_core (core); +return 0 ; +} + +static inline int vpu_iface_get_power_state (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->get_power_state) +return ops->get_power_state (core); +return 1 ; +} + +static inline int vpu_iface_shutdown_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->shutdown_core) +return ops->shutdown_core (core); +return 0 ; +} + +static inline int vpu_iface_restore_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->restore_core) +return ops->restore_core (core); +return 0 ; +} + +static inline int vpu_iface_on_firmware_loaded (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->on_firmware_loaded) +return ops->on_firmware_loaded (core); + +return 0 ; +} + +static inline u32 vpu_iface_get_data_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_data_size) +return 0 ; + +return ops->get_data_size (); +} + +static inline int vpu_iface_init (struct vpu_core *core, +struct vpu_shared_addr *shared, +struct vpu_buffer *rpc, +dma_addr_t boot_addr) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->init_rpc) +return -EINVAL ; + + ops->init_rpc (shared, rpc, boot_addr); + core->iface = shared; + shared->core = core; +if (rpc->bytesused > rpc->length) +return -ENOSPC ; +return 0 ; +} + +static inline int vpu_iface_set_log_buf (struct vpu_core *core, +struct vpu_buffer *log) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; + +if (ops->set_log_buf) + ops->set_log_buf (core->iface, log); + +return 0 ; +} + +static inline int vpu_iface_config_system (struct vpu_core *core, +u32 regs_base,void __iomem *regs) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; +if (ops->set_system_cfg) + ops->set_system_cfg (core->iface, regs_base, regs, core->id); + +return 0 ; +} + +static inline int vpu_iface_get_stream_buffer_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_stream_buffer_size) +return 0 ; + +return ops->get_stream_buffer_size (core->iface); +} + +static inline int vpu_iface_config_stream (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops) +return -EINVAL ; +if (ops->set_stream_cfg) + ops->set_stream_cfg (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_send_cmd (struct vpu_core *core,struct vpu_rpc_event *cmd) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->send_cmd_buf) +return -EINVAL ; + +return ops->send_cmd_buf (core->iface, cmd); +} + +static inline int vpu_iface_receive_msg (struct vpu_core *core,struct vpu_rpc_event *msg) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->receive_msg_buf) +return -EINVAL ; + +return ops->receive_msg_buf (core->iface, msg); +} + +static inline int vpu_iface_pack_cmd (struct vpu_core *core, +struct vpu_rpc_event *pkt, +u32 index,u32 id,void *data) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->pack_cmd) +return -EINVAL ; +return ops->pack_cmd (pkt, index, id, data); +} + +static inline int vpu_iface_convert_msg_id (struct vpu_core *core,u32 msg_id) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->convert_msg_id) +return -EINVAL ; + +return ops->convert_msg_id (msg_id); +} + +static inline int vpu_iface_unpack_msg_data (struct vpu_core *core, +struct vpu_rpc_event *pkt,void *data) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->unpack_msg_data) +return -EINVAL ; + +return ops->unpack_msg_data (pkt, data); +} + +static inline int vpu_iface_input_frame (struct vpu_inst *inst, +struct vb2_buffer *vb) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +if (!ops || !ops->input_frame) +return -EINVAL ; + +return ops->input_frame (inst->core->iface, inst, vb); +} + +static inline int vpu_iface_config_memory_resource (struct vpu_inst *inst, +u32 type,u32 index,struct vpu_buffer *buf) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->config_memory_resource) +return -EINVAL ; + +return ops->config_memory_resource (inst->core->iface, + inst->id, + type, index, buf); +} + +static inline int vpu_iface_config_stream_buffer (struct vpu_inst *inst, +struct vpu_buffer *buf) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->config_stream_buffer) +return -EINVAL ; + +return ops->config_stream_buffer (inst->core->iface, inst->id, buf); +} + +static inline int vpu_iface_update_stream_buffer (struct vpu_inst *inst, +u32 ptr,bool write) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->update_stream_buffer) +return -EINVAL ; + +return ops->update_stream_buffer (inst->core->iface, inst->id, ptr, write); +} + +static inline int vpu_iface_get_stream_buffer_desc (struct vpu_inst *inst, +struct vpu_rpc_buffer_desc *desc) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->get_stream_buffer_desc) +return -EINVAL ; + +if (!desc) +return 0 ; + +return ops->get_stream_buffer_desc (inst->core->iface, inst->id, desc); +} + +static inline u32 vpu_iface_get_version (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_version) +return 0 ; + +return ops->get_version (core->iface); +} + +static inline u32 vpu_iface_get_max_instance_count (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_max_instance_count) +return 0 ; + +return ops->get_max_instance_count (core->iface); +} + +static inline int vpu_iface_set_encode_params (struct vpu_inst *inst, +struct vpu_encode_params *params,u32 update) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->set_encode_params) +return -EINVAL ; + +return ops->set_encode_params (inst->core->iface, inst->id, params, update); +} + +static inline int vpu_iface_set_decode_params (struct vpu_inst *inst, +struct vpu_decode_params *params,u32 update) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->set_decode_params) +return -EINVAL ; + +return ops->set_decode_params (inst->core->iface, inst->id, params, update); +} + +static inline int vpu_iface_add_scode (struct vpu_inst *inst,u32 scode_type) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->add_scode) +return -EINVAL ; + +return ops->add_scode (inst->core->iface, inst->id, + &inst->stream_buffer, + inst->out_format.pixfmt, + scode_type); +} + +static inline int vpu_iface_pre_send_cmd (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->pre_send_cmd) +return ops->pre_send_cmd (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_post_send_cmd (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->post_send_cmd) +return ops->post_send_cmd (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_init_instance (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->init_instance) +return ops->init_instance (inst->core->iface, inst->id); + +return 0 ; +} + +#endif
Ming Qian
maintainerRE: [EXT] Re: [PATCH v13 04/13] media: amphion: add vpu core driver
Quoted 14 lines
-----Original Message-----
From: Hans Verkuil [mailto:[email protected]]
Sent: Thursday, December 2, 2021 5:05 PM
To: Ming Qian <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; dl-linux-imx <[email protected]>; Aisheng Dong <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXT] Re: [PATCH v13 04/13] media: amphion: add vpu core driverCaution: EXT Email
On 30/11/2021 10:48, Ming Qian wrote:
Quoted 8 lines
Quoted 2 lines
The vpu supports encoder and decoder. it needs mu core to handle it.
"mu core"? Do you mean "vpu core"? If not, then what is a "mu core"?
Regards,
Hans
Yes, it means "vpu core". We often call it "mu" internally
Quoted 2,370 lines
Quoted 11 lines
core will run either encoder or decoder firmware.
This driver is for support the vpu core.
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- drivers/media/platform/amphion/vpu_codec.h | 67 ++ drivers/media/platform/amphion/vpu_core.c | 906+++++++++++++++++++++
Quoted 12 lines
drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++++++++ drivers/media/platform/amphion/vpu_rpc.c | 279 +++++++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++++++++ 6 files changed, 2226 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.hReview
Quoted 1 line
(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_codec.hReview
Quoted 73 lines
new file mode 100644
index 000000000000..bf8920e9f6d7drivers/media/platform/amphion/vpu_codec.h new file +67
-0@@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CODEC_H +#define _AMPHION_VPU_CODEC_H + +struct vpu_encode_params { +u32 input_format; +u32 codec_format; +u32 profile; +u32 tier; +u32 level; +struct v4l2_fract frame_rate; +u32 src_stride; +u32 src_width; +u32 src_height; +struct v4l2_rect crop; +u32 out_width; +u32 out_height; + +u32 gop_length; +u32 bframes; + +u32 rc_mode; +u32 bitrate; +u32 bitrate_min; +u32 bitrate_max; + +u32 i_frame_qp; +u32 p_frame_qp; +u32 b_frame_qp; +u32 qp_min; +u32 qp_max; +u32 qp_min_i; +u32 qp_max_i; + +struct { +u32 enable; +u32 idc; +u32 width; +u32 height; + } sar; + +struct { +u32 primaries; +u32 transfer; +u32 matrix; +u32 full_range; + } color; +}; + +struct vpu_decode_params { +u32 codec_format; +u32 output_format; +u32 b_dis_reorder; +u32 b_non_frame; +u32 frame_count; +u32 end_flag; +struct { +u32 base; +u32 size; + } udata; +}; + +#endif (quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_core.cQuoted 45 lines
new file mode 100644
index 000000000000..0dbfd1c84f75drivers/media/platform/amphion/vpu_core.c new file +40
-0@@ -0,0 +1,906 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <linux/pm_domain.h> +#include <linux/firmware.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_core.h" +#include "vpu_mbox.h" +#include "vpu_msgs.h" +#include "vpu_rpc.h" +#include "vpu_cmds.h" + +void csr_writel (struct vpu_core *core,u32 reg,u32 val) +{ +writel (val, core->base + reg); +} + +u32 csr_readl (struct vpu_core *core,u32 reg) +{ +return readl (core->base + reg); +} + +static int vpu_core_load_firmware (struct vpu_core *core) +{ +const struct firmware *pfw =NULL ;drivers/media/platform/amphion/vpu_core.c line 50
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+int ret =0 ; + +WARN_ON (!core || !core->res || !core->res->fwname); +if (!core->fw.virt) { +dev_err (core->dev,"firmware buffer is not ready\n" ); +return -EINVAL ; + } + + ret =request_firmware (&pfw, core->res->fwname, core->dev); + dev_dbg(core->dev, "request_firmware %s : %d\n",
core->res->fwname, ret);drivers/media/platform/amphion/vpu_core.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_core.c new file +8
-0+if (ret) { +dev_err (core->dev,"request firmware %s failed, ret = %d\n" , + core->res->fwname, ret); +return ret; + } + +if (core->fw.length < pfw->size) { + dev_err(core->dev, "firmware buffer size want %zu,but %d\n",
Quoted 17 lines
drivers/media/platform/amphion/vpu_core.c new file +17
-0+ pfw->size, core->fw.length); + ret = -EINVAL ; + goto exit; + } + +memset_io (core->fw.virt,0 , core->fw.length); +memcpy (core->fw.virt, pfw->data, pfw->size); + core->fw.bytesused = pfw->size; + ret =vpu_iface_on_firmware_loaded (core); +exit: +release_firmware (pfw); + pfw =NULL ; + +return ret; +} + +static int vpu_core_boot_done (struct vpu_core *core)drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+{ +u32 fw_version; + + fw_version =vpu_iface_get_version (core); +dev_info (core->dev,"%s firmware version : %d.%d.%d\n" , +vpu_core_type_desc (core->type), + (fw_version >>16 ) &0xff , + (fw_version >>8 ) &0xff , + fw_version &0xff ); + core->supported_instance_count =
vpu_iface_get_max_instance_count(core);drivers/media/platform/amphion/vpu_core.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c new file +4
-0+if (core->res->act_size) { +u32 count = core->act.length / core->res->act_size; + + core->supported_instance_count =
min(core->supported_instance_count, count);Quoted 97 lines
drivers/media/platform/amphion/vpu_core.c new file +97
-0+ } + core->fw_version = fw_version; + core->state =VPU_CORE_ACTIVE ; + +return 0 ; +} + +static int vpu_core_wait_boot_done (struct vpu_core *core) +{ +int ret; + + ret =wait_for_completion_timeout (&core->cmp,VPU_TIMEOUT ); +if (!ret) { +dev_err (core->dev,"boot timeout\n" ); +return -EINVAL ; + } +return vpu_core_boot_done (core); +} + +static int vpu_core_boot (struct vpu_core *core,bool load) +{ +int ret; + +WARN_ON (!core); + +if (!core->res->standalone) +return 0 ; + +reinit_completion (&core->cmp); +if (load) { + ret =vpu_core_load_firmware (core); +if (ret) +return ret; + } + +vpu_iface_boot_core (core); +return vpu_core_wait_boot_done (core); +} + +static int vpu_core_shutdown (struct vpu_core *core) +{ +if (!core->res->standalone) +return 0 ; +return vpu_iface_shutdown_core (core); +} + +static int vpu_core_restore (struct vpu_core *core) +{ +int ret; + +if (!core->res->standalone) +return 0 ; + ret =vpu_core_sw_reset (core); +if (ret) +return ret; + +vpu_core_boot_done (core); +return vpu_iface_restore_core (core); +} + +static int __vpu_alloc_dma (struct device *dev,struct vpu_buffer *buf) +{ +gfp_t gfp =GFP_KERNEL |GFP_DMA32 ; + +WARN_ON (!dev || !buf); + +if (!buf->length) +return 0 ; + + buf->virt =dma_alloc_coherent (dev, buf->length, &buf->phys, gfp); +if (!buf->virt) +return -ENOMEM ; + + buf->dev = dev; + +return 0 ; +} + +void vpu_free_dma (struct vpu_buffer *buf) +{ +WARN_ON (!buf); + +if (!buf->virt || !buf->dev) +return ; + +dma_free_coherent (buf->dev, buf->length, buf->virt, buf->phys); + buf->virt =NULL ; + buf->phys =0 ; + buf->length =0 ; + buf->bytesused =0 ; + buf->dev =NULL ; +} + +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf) +{ +WARN_ON (!core || !buf); +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+return __vpu_alloc_dma (core->dev, buf); +} + +static void vpu_core_check_hang (struct vpu_core *core) +{ +if (core->hang_mask) + core->state =VPU_CORE_HANG ; +} + +static struct vpu_core *vpu_core_find_proper_by_type(struct vpu_dev *vpu,u32 type)
Quoted 48 lines
drivers/media/platform/amphion/vpu_core.c new file +48
-0+{ +struct vpu_core *core =NULL ; +int request_count =INT_MAX ; +struct vpu_core *c; + +WARN_ON (!vpu); + +list_for_each_entry (c, &vpu->cores, list) { +dev_dbg (c->dev,"instance_mask = 0x%lx, state = %d\n" , + c->instance_mask, + c->state); +if (c->type != type) +continue ; +if (c->state ==VPU_CORE_DEINIT ) { + core = c; +break ; + } +vpu_core_check_hang (c); +if (c->state !=VPU_CORE_ACTIVE ) +continue ; +if (c->request_count < request_count) { + request_count = c->request_count; + core = c; + } +if (!request_count) +break ; + } + +return core; +} + +static bool vpu_core_is_exist (struct vpu_dev *vpu,struct vpu_core *core) +{ +struct vpu_core *c; + +list_for_each_entry (c, &vpu->cores, list) { +if (c == core) +return true; + } + +return false; +} + +static void vpu_core_get_vpu (struct vpu_core *core) +{ + core->vpu->get_vpu (core->vpu); +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->get_enc (core->vpu);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->get_dec (core->vpu); +} + +static int vpu_core_register (struct device *dev,struct vpu_core *core) +{ +struct vpu_dev *vpu =dev_get_drvdata (dev); +int ret =0 ; + + dev_dbg(core->dev, "register core %s\n",
vpu_core_type_desc(core->type));drivers/media/platform/amphion/vpu_core.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c new file +4
-0+if (vpu_core_is_exist (vpu, core)) +return 0 ; + + core->workqueue = alloc_workqueue("vpu", WQ_UNBOUND |
WQ_MEM_RECLAIM, 1);drivers/media/platform/amphion/vpu_core.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_core.c new file +6
-0+if (!core->workqueue) { +dev_err (core->dev,"fail to alloc workqueue\n" ); +return -ENOMEM ; + } +INIT_WORK (&core->msg_work, vpu_msg_run_work); + INIT_DELAYED_WORK(&core->msg_delayed_work,
vpu_msg_delayed_work);drivers/media/platform/amphion/vpu_core.c line 265
Quoted 1 line
drivers/media/platform/amphion/vpu_core.c new file +1
-0+ core->msg_buffer_size =
roundup_pow_of_two(VPU_MSG_BUFFER_SIZE);drivers/media/platform/amphion/vpu_core.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_core.c new file +7
-0+ core->msg_buffer =vzalloc (core->msg_buffer_size); +if (!core->msg_buffer) { +dev_err (core->dev,"failed allocate buffer for fifo\n" ); + ret = -ENOMEM ; + goto error; + } + ret = kfifo_init(&core->msg_fifo, core->msg_buffer,
core->msg_buffer_size);Quoted 91 lines
drivers/media/platform/amphion/vpu_core.c new file +91
-0+if (ret) { +dev_err (core->dev,"failed init kfifo\n" ); + goto error; + } + +list_add_tail (&core->list, &vpu->cores); + +vpu_core_get_vpu (core); + +if (vpu_iface_get_power_state (core)) + ret =vpu_core_restore (core); +if (ret) + goto error; + +return 0 ; +error: +if (core->msg_buffer) { +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + } +if (core->workqueue) { +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } +return ret; +} + +static void vpu_core_put_vpu (struct vpu_core *core) +{ +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->put_enc (core->vpu); +if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->put_dec (core->vpu); + core->vpu->put_vpu (core->vpu); +} + +static int vpu_core_unregister (struct device *dev,struct vpu_core *core) +{ +list_del_init (&core->list); + +vpu_core_put_vpu (core); + core->vpu =NULL ; +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + +if (core->workqueue) { +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } + +return 0 ; +} + +static int vpu_core_acquire_instance (struct vpu_core *core) +{ +int id; + +WARN_ON (!core); + + id =ffz (core->instance_mask); +if (id >= core->supported_instance_count) +return -EINVAL ; + +set_bit (id, &core->instance_mask); + +return id; +} + +static void vpu_core_release_instance (struct vpu_core *core,int id) +{ +WARN_ON (!core); + +if (id <0 || id >= core->supported_instance_count) +return ; + +clear_bit (id, &core->instance_mask); +} + +struct vpu_inst *vpu_inst_get (struct vpu_inst *inst) +{ +if (!inst) +return NULL ; + +atomic_inc (&inst->ref_count); + +return inst; +} + +void vpu_inst_put (struct vpu_inst *inst)drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+{ +if (!inst) +return ; +if (atomic_dec_and_test (&inst->ref_count)) { +if (inst->release) + inst->release (inst); + } +} + +struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum
vpu_core_typetype)Quoted 69 lines
drivers/media/platform/amphion/vpu_core.c new file +69
-0+{ +struct vpu_core *core =NULL ; +int ret; + +mutex_lock (&vpu->lock); + + core =vpu_core_find_proper_by_type (vpu, type); +if (!core) + goto exit; + +mutex_lock (&core->lock); +pm_runtime_get_sync (core->dev); + +if (core->state ==VPU_CORE_DEINIT ) { + ret =vpu_core_boot (core, true); +if (ret) { +pm_runtime_put_sync (core->dev); +mutex_unlock (&core->lock); + core =NULL ; + goto exit; + } + } + + core->request_count++; + +mutex_unlock (&core->lock); +exit: +mutex_unlock (&vpu->lock); + +return core; +} + +void vpu_release_core (struct vpu_core *core) +{ +if (!core) +return ; + +mutex_lock (&core->lock); +pm_runtime_put_sync (core->dev); +if (core->request_count) + core->request_count--; +mutex_unlock (&core->lock); +} + +int vpu_inst_register (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core; +int ret =0 ; + +WARN_ON (!inst || !inst->vpu); + + vpu = inst->vpu; + core = inst->core; +if (!core) { + core =vpu_request_core (vpu, inst->type); +if (!core) { +dev_err (vpu->dev,"there is no vpu core for %s\n" , +vpu_core_type_desc (inst->type)); +return -EINVAL ; + } + inst->core = core; + inst->dev =get_device (core->dev); + } + +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) + goto exit; +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+ ret =vpu_core_acquire_instance (core); +if (ret <0 ) + goto exit; + +vpu_trace (inst->dev,"[%d] %p\n" , ret, inst); + inst->id = ret; +list_add_tail (&inst->list, &core->instances); + ret =0 ; +if (core->res->act_size) { + inst->act.phys = core->act.phys + core->res->act_size *
inst->id;Quoted 85 lines
drivers/media/platform/amphion/vpu_core.c new file +85
-0+ inst->act.virt = core->act.virt + core->res->act_size * inst->id; + inst->act.length = core->res->act_size; + } +vpu_inst_create_dbgfs_file (inst); +exit: +mutex_unlock (&core->lock); + +if (ret) +dev_err (core->dev,"register instance fail\n" ); +return ret; +} + +int vpu_inst_unregister (struct vpu_inst *inst) +{ +struct vpu_core *core; + +WARN_ON (!inst); + +if (!inst->core) +return 0 ; + + core = inst->core; +vpu_clear_request (inst); +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) { +vpu_inst_remove_dbgfs_file (inst); +list_del_init (&inst->list); +vpu_core_release_instance (core, inst->id); + inst->id =VPU_INST_NULL_ID ; + } +vpu_core_check_hang (core); +if (core->state ==VPU_CORE_HANG && !core->instance_mask) { +dev_info (core->dev,"reset hang core\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); + +return 0 ; +} + +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index) +{ +struct vpu_inst *inst =NULL ; +struct vpu_inst *tmp; + +mutex_lock (&core->lock); +if (!test_bit (index, &core->instance_mask)) + goto exit; +list_for_each_entry (tmp, &core->instances, list) { +if (tmp->id == index) { + inst =vpu_inst_get (tmp); +break ; + } + } +exit: +mutex_unlock (&core->lock); + +return inst; +} + +const struct vpu_core_resources *vpu_get_resource (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core =NULL ; +const struct vpu_core_resources *res =NULL ; + +if (!inst || !inst->vpu) +return NULL ; + +if (inst->core && inst->core->res) +return inst->core->res; + + vpu = inst->vpu; +mutex_lock (&vpu->lock); +list_for_each_entry (core, &vpu->cores, list) { +if (core->type == inst->type) { + res = core->res; +break ; + } + } +mutex_unlock (&vpu->lock); +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+return res; +} + +static int vpu_core_parse_dt (struct vpu_core *core,struct device_node *np) +{ +struct device_node *node; +struct resource res; + +if (of_count_phandle_with_args (np,"memory-region" ,NULL ) <2 ) { + dev_err(core->dev, "need 2 memory-region for boot andrpc\n");
drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+return -ENODEV ; + } + + node =of_parse_phandle (np,"memory-region" ,0 ); +if (!node) { +dev_err (core->dev,"boot-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { + dev_err(core->dev, "boot-region of_address_to_resourceerror\n");
drivers/media/platform/amphion/vpu_core.c
Quoted 12 lines
drivers/media/platform/amphion/vpu_core.c new file +12
-0+return -EINVAL ; + } + core->fw.phys = res.start; + core->fw.length =resource_size (&res); + + node =of_parse_phandle (np,"memory-region" ,1 ); +if (!node) { +dev_err (core->dev,"rpc-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { + dev_err(core->dev, "rpc-region of_address_to_resourceerror\n");
drivers/media/platform/amphion/vpu_core.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_core.c new file +7
-0+return -EINVAL ; + } + core->rpc.phys = res.start; + core->rpc.length =resource_size (&res); + +if (core->rpc.length < core->res->rpc_size + core->res->fwlog_size) { + dev_err(core->dev, "the rpc-region <%pad, 0x%x> is notenough\n",
drivers/media/platform/amphion/vpu_core.c
Quoted 11 lines
drivers/media/platform/amphion/vpu_core.c new file +11
-0+ &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->fw.virt =ioremap_wc (core->fw.phys, core->fw.length); + core->rpc.virt =ioremap_wc (core->rpc.phys, core->rpc.length); +memset_io (core->rpc.virt,0 , core->rpc.length); + + if (vpu_iface_check_memory_region(core, + core->rpc.phys, + core->rpc.length) !=
VPU_CORE_MEMORY_UNCACHED) {drivers/media/platform/amphion/vpu_core.c line 588
Quoted 1 line
drivers/media/platform/amphion/vpu_core.c new file +1
-0+ dev_err(core->dev, "rpc region<%pad, 0x%x> isn'tuncached\n",
drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+ &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->log.phys = core->rpc.phys + core->res->rpc_size; + core->log.virt = core->rpc.virt + core->res->rpc_size; + core->log.length = core->res->fwlog_size; + core->act.phys = core->log.phys + core->log.length; + core->act.virt = core->log.virt + core->log.length; + core->act.length = core->rpc.length - core->res->rpc_size -
core->log.length;Quoted 33 lines
drivers/media/platform/amphion/vpu_core.c new file +33
-0+ core->rpc.length = core->res->rpc_size; + +return 0 ; +} + +static int vpu_core_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core; +struct vpu_dev *vpu =dev_get_drvdata (dev->parent); +struct vpu_shared_addr *iface; +u32 iface_data_size; +int ret; + +dev_dbg (dev,"probe\n" ); +if (!vpu) +return -EINVAL ; + core =devm_kzalloc (dev,sizeof (*core),GFP_KERNEL ); +if (!core) +return -ENOMEM ; + + core->pdev = pdev; + core->dev = dev; +platform_set_drvdata (pdev, core); + core->vpu = vpu; +INIT_LIST_HEAD (&core->instances); +mutex_init (&core->lock); +mutex_init (&core->cmd_lock); +init_completion (&core->cmp); +init_waitqueue_head (&core->ack_wq); + core->state =VPU_CORE_DEINIT ; + + core->res =of_device_get_match_data (dev);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (!core->res) +return -ENODEV ; + + core->type = core->res->type; + core->id =of_alias_get_id (dev->of_node,"vpu_core" ); +if (core->id <0 ) { +dev_err (dev,"can't get vpu core id\n" ); +return core->id; + } + dev_info(core->dev, "[%d] = %s\n", core->id,
vpu_core_type_desc(core->type));Quoted 14 lines
drivers/media/platform/amphion/vpu_core.c new file +14
-0+ ret =vpu_core_parse_dt (core, dev->of_node); +if (ret) +return ret; + + core->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (core->base)) +return PTR_ERR (core->base); + +if (!vpu_iface_check_codec (core)) { +dev_err (core->dev,"is not supported\n" ); +return -EINVAL ; + } + + ret =vpu_mbox_init (core);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (ret) +return ret; + + iface =devm_kzalloc (dev,sizeof (*iface),GFP_KERNEL ); +if (!iface) +return -ENOMEM ; + + iface_data_size =vpu_iface_get_data_size (core); +if (iface_data_size) { + iface->priv = devm_kzalloc(dev, iface_data_size,
GFP_KERNEL);Quoted 87 lines
drivers/media/platform/amphion/vpu_core.c new file +87
-0+if (!iface->priv) +return -ENOMEM ; + } + + ret =vpu_iface_init (core, iface, &core->rpc, core->fw.phys); +if (ret) { +dev_err (core->dev,"init iface fail, ret = %d\n" , ret); +return ret; + } + +vpu_iface_config_system (core, vpu->res->mreg_base, vpu->base); +vpu_iface_set_log_buf (core, &core->log); + +pm_runtime_enable (dev); + ret =pm_runtime_get_sync (dev); +if (ret) { +pm_runtime_put_noidle (dev); +pm_runtime_set_suspended (dev); + goto err_runtime_disable; + } + + ret =vpu_core_register (dev->parent, core); +if (ret) + goto err_core_register; + core->parent = dev->parent; + +pm_runtime_put_sync (dev); +vpu_core_create_dbgfs_file (core); + +return 0 ; + +err_core_register: +pm_runtime_put_sync (dev); +err_runtime_disable: +pm_runtime_disable (dev); + +return ret; +} + +static int vpu_core_remove (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core =platform_get_drvdata (pdev); +int ret; + +vpu_core_remove_dbgfs_file (core); + ret =pm_runtime_get_sync (dev); +WARN_ON (ret <0 ); + +vpu_core_shutdown (core); +pm_runtime_put_sync (dev); +pm_runtime_disable (dev); + +vpu_core_unregister (core->parent, core); +iounmap (core->fw.virt); +iounmap (core->rpc.virt); +mutex_destroy (&core->lock); +mutex_destroy (&core->cmd_lock); + +return 0 ; +} + +static int __maybe_unused vpu_core_runtime_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +return vpu_mbox_request (core); +} + +static int __maybe_unused vpu_core_runtime_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +vpu_mbox_free (core); +return 0 ; +} + +static void vpu_core_cancel_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; + +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); + +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +cancel_work_sync (&inst->msg_work);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+mutex_unlock (&core->lock); +} + +static void vpu_core_resume_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; +unsigned long delay =msecs_to_jiffies (10 ); + +queue_work (core->workqueue, &core->msg_work); + queue_delayed_work(core->workqueue, &core->msg_delayed_work,delay);
Quoted 16 lines
drivers/media/platform/amphion/vpu_core.c new file +16
-0+ +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +queue_work (inst->workqueue, &inst->msg_work); +mutex_unlock (&core->lock); +} + +static int __maybe_unused vpu_core_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+pm_runtime_get_sync (dev); +vpu_core_get_vpu (core); +if (core->state !=VPU_CORE_SNAPSHOT ) + goto exit; + +if (!vpu_iface_get_power_state (core)) { +if (!list_empty (&core->instances)) { + ret =vpu_core_boot (core, false); +if (ret) { + dev_err(core->dev, "%s boot fail\n",
__func__);drivers/media/platform/amphion/vpu_core.c
Quoted 11 lines
drivers/media/platform/amphion/vpu_core.c new file +11
-0+ core->state =VPU_CORE_DEINIT ; + goto exit; + } + }else { + core->state =VPU_CORE_DEINIT ; + } + }else { +if (!list_empty (&core->instances)) { + ret =vpu_core_sw_reset (core); +if (ret) { + dev_err(core->dev, "%s sw_reset fail\n",
__func__);Quoted 37 lines
drivers/media/platform/amphion/vpu_core.c new file +37
-0+ core->state =VPU_CORE_HANG ; + goto exit; + } + } + core->state =VPU_CORE_ACTIVE ; + } + +exit: +pm_runtime_put_sync (dev); +mutex_unlock (&core->lock); + +vpu_core_resume_work (core); +return ret; +} + +static int __maybe_unused vpu_core_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock); +if (core->state ==VPU_CORE_ACTIVE ) { +if (!list_empty (&core->instances)) { + ret =vpu_core_snapshot (core); +if (ret) { +mutex_unlock (&core->lock); +return ret; + } + } + + core->state =VPU_CORE_SNAPSHOT ; + } +mutex_unlock (&core->lock); +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+vpu_core_cancel_work (core); + +mutex_lock (&core->lock); +vpu_core_put_vpu (core); +mutex_unlock (&core->lock); +return ret; +} + +static const struct dev_pm_ops vpu_core_pm_ops = { + SET_RUNTIME_PM_OPS(vpu_core_runtime_suspend,
vpu_core_runtime_resume, NULL)Review
Quoted 61 lines
drivers/media/platform/amphion/vpu_core.c new file +60
-0+SET_SYSTEM_SLEEP_PM_OPS (vpu_core_suspend, vpu_core_resume) +}; + +static struct vpu_core_resources imx8q_enc = { + .type =VPU_CORE_TYPE_ENC , + .fwname ="vpu/vpu_fw_imx8_enc.bin" , + .stride =16 , + .max_width =1920 , + .max_height =1920 , + .min_width =64 , + .min_height =48 , + .step_width =2 , + .step_height =2 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .act_size =0xc0000 , + .standalone = true, +}; + +static struct vpu_core_resources imx8q_dec = { + .type =VPU_CORE_TYPE_DEC , + .fwname ="vpu/vpu_fw_imx8_dec.bin" , + .stride =256 , + .max_width =8188 , + .max_height =8188 , + .min_width =16 , + .min_height =16 , + .step_width =1 , + .step_height =1 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .standalone = true, +}; + +static const struct of_device_id vpu_core_dt_match[] = { + { .compatible ="nxp,imx8q-vpu-encoder" , .data = &imx8q_enc }, + { .compatible ="nxp,imx8q-vpu-decoder" , .data = &imx8q_dec }, + {} +}; +MODULE_DEVICE_TABLE (of, vpu_core_dt_match); + +static struct platform_driver amphion_vpu_core_driver = { + .probe = vpu_core_probe, + .remove = vpu_core_remove, + .driver = { + .name ="amphion-vpu-core" , + .of_match_table = vpu_core_dt_match, + .pm = &vpu_core_pm_ops, + }, +}; + +int __init vpu_core_driver_init (void ) +{ +return platform_driver_register (&hion_vpu_core_driver); +} + +void __exit vpu_core_driver_exit(void) +{ +platform_driver_unregister (&hion_vpu_core_driver); +}(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_core.hReview
Quoted 21 lines
new file mode 100644
index 000000000000..00a662997da4drivers/media/platform/amphion/vpu_core.h new file +15
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CORE_H +#define _AMPHION_VPU_CORE_H + +void csr_writel (struct vpu_core *core,u32 reg,u32 val); +u32 csr_readl (struct vpu_core *core,u32 reg); +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf); +void vpu_free_dma (struct vpu_buffer *buf); +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index); + +#endif (quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_dbg.cQuoted 54 lines
new file mode 100644
index 000000000000..2e7e11101f99drivers/media/platform/amphion/vpu_dbg.c new file +49
-0@@ -0,0 +1,495 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <media/v4l2-device.h> +#include <linux/debugfs.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_helpers.h" +#include "vpu_cmds.h" +#include "vpu_rpc.h" + +struct print_buf_desc { +u32 start_h_phy; +u32 start_h_vir; +u32 start_m; +u32 bytes; +u32 read; +u32 write; +char buffer[0 ]; +}; + +static char *vb2_stat_name[] = { + [VB2_BUF_STATE_DEQUEUED ] ="dequeued" , + [VB2_BUF_STATE_IN_REQUEST ] ="in_request" , + [VB2_BUF_STATE_PREPARING ] ="preparing" , + [VB2_BUF_STATE_QUEUED ] ="queued" , + [VB2_BUF_STATE_ACTIVE ] ="active" , + [VB2_BUF_STATE_DONE ] ="done" , + [VB2_BUF_STATE_ERROR ] ="error" , +}; + +static char *vpu_stat_name[] = { + [VPU_BUF_STATE_IDLE ] ="idle" , + [VPU_BUF_STATE_INUSE ] ="inuse" , + [VPU_BUF_STATE_DECODED ] ="decoded" , + [VPU_BUF_STATE_READY ] ="ready" , + [VPU_BUF_STATE_SKIP ] ="skip" , + [VPU_BUF_STATE_ERROR ] ="error" , +};drivers/media/platform/amphion/vpu_dbg.c line 59
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+ +static int vpu_dbg_instance (struct seq_file *s,void *data) +{ +struct vpu_inst *inst = s->private; +char str[128 ]; +int num; +struct vb2_queue *vq; +int i; + + num = scnprintf(str, sizeof(str), "[%s]\n",
vpu_core_type_desc(inst->type));drivers/media/platform/amphion/vpu_dbg.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_dbg.c new file +4
-0+if (seq_write (s, str, num)) +return 0 ; + + num = scnprintf(str, sizeof(str), "tgig = %d,pid = %d\n", inst->tgid,
inst->pid);Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"state = %d\n" , inst->state); +if (seq_write (s, str, num)) +return 0 ;drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+ num =scnprintf (str,sizeof (str), +"min_buffer_out = %d, min_buffer_cap = %d\n" , + inst->min_buffer_out, inst->min_buffer_cap); +if (seq_write (s, str, num)) +return 0 ; + + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); + num = scnprintf(str, sizeof(str), + "output (%2d, %2d): fmt = %c%c%c%c %dx %d, %d;",
Quoted 14 lines
drivers/media/platform/amphion/vpu_dbg.c new file +14
-0+vb2_is_streaming (vq), + vq->num_buffers, + inst->out_format.pixfmt, + inst->out_format.pixfmt >>8 , + inst->out_format.pixfmt >>16 , + inst->out_format.pixfmt >>24 , + inst->out_format.width, + inst->out_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->out_format.num_planes; i++) { + num = scnprintf(str, sizeof(str), " %d(%d)", + inst->out_format.sizeimage[i],drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+ inst->out_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + num = scnprintf(str, sizeof(str), + "capture(%2d, %2d): fmt = %c%c%c%c %dx %d, %d;",
Quoted 56 lines
drivers/media/platform/amphion/vpu_dbg.c new file +56
-0+vb2_is_streaming (vq), + vq->num_buffers, + inst->cap_format.pixfmt, + inst->cap_format.pixfmt >>8 , + inst->cap_format.pixfmt >>16 , + inst->cap_format.pixfmt >>24 , + inst->cap_format.width, + inst->cap_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->cap_format.num_planes; i++) { + num =scnprintf (str,sizeof (str)," %d(%d)" , + inst->cap_format.sizeimage[i], + inst->cap_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + num =scnprintf (str,sizeof (str),"crop: (%d, %d) %d x %d\n" , + inst->crop.left, + inst->crop.top, + inst->crop.width, + inst->crop.height); +if (seq_write (s, str, num)) +return 0 ; + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"output [%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]); +if (seq_write (s, str, num)) +return 0 ; + } + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"capture[%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]);drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+if (seq_write (s, str, num)) +return 0 ; + } + + num =scnprintf (str,sizeof (str),"sequence = %d\n" , inst->sequence); +if (seq_write (s, str, num)) +return 0 ; + +if (inst->use_stream_buffer) { + num = scnprintf(str, sizeof(str), "stream_buffer = %d / %d,<%pad, 0x%x>\n",
drivers/media/platform/amphion/vpu_dbg.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_dbg.c new file +8
-0+vpu_helper_get_used_space (inst), + inst->stream_buffer.length, + &inst->stream_buffer.phys, + inst->stream_buffer.length); +if (seq_write (s, str, num)) +return 0 ; + } + num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n",
kfifo_len(&inst->msg_fifo));Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"flow :\n" ); +if (seq_write (s, str, num))drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+return 0 ; + +mutex_lock (&inst->core->cmd_lock); +for (i =0 ; i <ARRAY_SIZE (inst->flows); i++) { +u32 idx = (inst->flow_idx + i) % (ARRAY_SIZE (inst->flows)); + +if (!inst->flows[idx]) +continue ; + num = scnprintf(str, sizeof(str), "\t[%s]0x%x\n", + inst->flows[idx] >= VPU_MSG_ID_NOOP ?"M" : "C",
Quoted 18 lines
drivers/media/platform/amphion/vpu_dbg.c new file +18
-0+ inst->flows[idx]); +if (seq_write (s, str, num)) { +mutex_unlock (&inst->core->cmd_lock); +return 0 ; + } + } +mutex_unlock (&inst->core->cmd_lock); + + i =0 ; +while (true) { + num =call_vop (inst, get_debug_info, str,sizeof (str), i++); +if (num <=0 ) +break ; +if (seq_write (s, str, num)) +return 0 ; + } + +return 0 ;drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+} + +static int vpu_dbg_core (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct vpu_shared_addr *iface = core->iface; +char str[128 ]; +int num; + + num = scnprintf(str, sizeof(str), "[%s]\n",
vpu_core_type_desc(core->type));drivers/media/platform/amphion/vpu_dbg.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_dbg.c new file +8
-0+if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"boot_region = <%pad, 0x%x>\n" , + &core->fw.phys, core->fw.length); +if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), "rpc_region = <%pad, 0x%x> used =0x%x\n",
drivers/media/platform/amphion/vpu_dbg.c line 228
Quoted 1 line
drivers/media/platform/amphion/vpu_dbg.c new file +1
-0+ &core->rpc.phys, core->rpc.length,
core->rpc.bytesused);Quoted 16 lines
drivers/media/platform/amphion/vpu_dbg.c new file +16
-0+if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"fwlog_region = <%pad, 0x%x>\n" , + &core->log.phys, core->log.length); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"state = %d\n" , core->state); +if (seq_write (s, str, num)) +return 0 ; +if (core->state ==VPU_CORE_DEINIT ) +return 0 ; + num =scnprintf (str,sizeof (str),"fw version = %d.%d.%d\n" , + (core->fw_version >>16 ) &0xff , + (core->fw_version >>8 ) &0xff , + core->fw_version &0xff );drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"instances = %d/%d (0x%02lx), %d\n" , +hweight32 (core->instance_mask), + core->supported_instance_count, + core->instance_mask, + core->request_count); +if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n",
kfifo_len(&core->msg_fifo));drivers/media/platform/amphion/vpu_dbg.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_dbg.c new file +4
-0+if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), + "cmd_buf:[0x%x, 0x%x], wptr = 0x%x, rptr =0x%x\n",
drivers/media/platform/amphion/vpu_dbg.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_dbg.c new file +8
-0+ iface->cmd_desc->start, + iface->cmd_desc->end, + iface->cmd_desc->wptr, + iface->cmd_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), + "msg_buf:[0x%x, 0x%x], wptr = 0x%x, rptr =0x%x\n",
Quoted 55 lines
drivers/media/platform/amphion/vpu_dbg.c new file +55
-0+ iface->msg_desc->start, + iface->msg_desc->end, + iface->msg_desc->wptr, + iface->msg_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + +return 0 ; +} + +static int vpu_dbg_fwlog (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct print_buf_desc *print_buf; +int length; +u32 rptr; +u32 wptr; +int ret =0 ; + +if (!core->log.virt || core->state ==VPU_CORE_DEINIT ) +return 0 ; + + print_buf = core->log.virt; + rptr = print_buf->read; + wptr = print_buf->write; + +if (rptr == wptr) +return 0 ; +else if (rptr < wptr) + length = wptr - rptr; +else + length = print_buf->bytes + wptr - rptr; + +if (s->count + length >= s->size) { + s->count = s->size; +return 0 ; + } + +if (rptr + length >= print_buf->bytes) { +int num = print_buf->bytes - rptr; + +if (seq_write (s, print_buf->buffer + rptr, num)) + ret =-1 ; + length -= num; + rptr =0 ; + } + +if (length) { +if (seq_write (s, print_buf->buffer + rptr, length)) + ret =-1 ; + rptr += length; + } +if (!ret) + print_buf->read = rptr; +drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+return 0 ; +} + +static int vpu_dbg_inst_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_instance, inode->i_private); +} + +static ssize_t vpu_dbg_inst_write (struct file *file, + const char __user *user_buf, size_t size, loff_t*ppos)
drivers/media/platform/amphion/vpu_dbg.c
Quoted 11 lines
drivers/media/platform/amphion/vpu_dbg.c new file +11
-0+{ +struct seq_file *s = file->private_data; +struct vpu_inst *inst = s->private; + +vpu_session_debug (inst); + +return size; +} + +static ssize_t vpu_dbg_core_write (struct file *file, + const char __user *user_buf, size_t size, loff_t*ppos)
Quoted 99 lines
drivers/media/platform/amphion/vpu_dbg.c new file +99
-0+{ +struct seq_file *s = file->private_data; +struct vpu_core *core = s->private; + +pm_runtime_get_sync (core->dev); +mutex_lock (&core->lock); +if (core->state !=VPU_CORE_DEINIT && !core->instance_mask) { +dev_info (core->dev,"reset\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); +pm_runtime_put_sync (core->dev); + +return size; +} + +static int vpu_dbg_core_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_core, inode->i_private); +} + +static int vpu_dbg_fwlog_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_fwlog, inode->i_private); +} + +static const struct file_operations vpu_dbg_inst_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_inst_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_inst_write, +}; + +static const struct file_operations vpu_dbg_core_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_core_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_core_write, +}; + +static const struct file_operations vpu_dbg_fwlog_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_fwlog_open, + .release = single_release, + .read = seq_read, +}; + +int vpu_inst_create_dbgfs_file (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!inst || !inst->core || !inst->core->vpu) +return -EINVAL ; + + vpu = inst->core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (inst->debugfs) +return 0 ; + +scnprintf (name,sizeof (name),"instance.%d.%d" , + inst->core->id, inst->id); + inst->debugfs =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0644 ), + vpu->debugfs, + inst, + &vpu_dbg_inst_fops); +if (!inst->debugfs) { +dev_err (inst->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + +return 0 ; +} + +int vpu_inst_remove_dbgfs_file (struct vpu_inst *inst) +{ +if (!inst) +return 0 ; + +debugfs_remove (inst->debugfs); + inst->debugfs =NULL ; + +return 0 ; +} + +int vpu_core_create_dbgfs_file (struct vpu_core *core) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!core || !core->vpu)drivers/media/platform/amphion/vpu_dbg.c line 472
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+return -EINVAL ; + + vpu = core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (!core->debugfs) { +scnprintf (name,sizeof (name),"core.%d" , core->id); + core->debugfs = debugfs_create_file((const char *)name, +
VERIFY_OCTAL_PERMISSIONS(0644),drivers/media/platform/amphion/vpu_dbg.c
Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+ vpu->debugfs, + core, + &vpu_dbg_core_fops); +if (!core->debugfs) { + dev_err(core->dev, "vpu create debugfs %s fail\n",name);
drivers/media/platform/amphion/vpu_dbg.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_dbg.c new file +6
-0+return -EINVAL ; + } + } +if (!core->debugfs_fwlog) { +scnprintf (name,sizeof (name),"fwlog.%d" , core->id); + core->debugfs_fwlog = debugfs_create_file((const char*)name,
drivers/media/platform/amphion/vpu_dbg.c line 463
Quoted 1 line
drivers/media/platform/amphion/vpu_dbg.c new file +1
-0+
VERIFY_OCTAL_PERMISSIONS(0444),drivers/media/platform/amphion/vpu_dbg.c
Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+ vpu->debugfs, + core, + &vpu_dbg_fwlog_fops); +if (!core->debugfs_fwlog) { + dev_err(core->dev, "vpu create debugfs %s fail\n",name);
Review
Quoted 28 lines
drivers/media/platform/amphion/vpu_dbg.c new file +27
-0+return -EINVAL ; + } + } + +return 0 ; +} + +int vpu_core_remove_dbgfs_file (struct vpu_core *core) +{ +if (!core) +return 0 ; +debugfs_remove (core->debugfs); + core->debugfs =NULL ; +debugfs_remove (core->debugfs_fwlog); + core->debugfs_fwlog =NULL ; + +return 0 ; +} + +void vpu_inst_record_flow (struct vpu_inst *inst,u32 flow) +{ +if (!inst) +return ; + + inst->flows[inst->flow_idx] = flow; + inst->flow_idx = (inst->flow_idx +1 ) % (ARRAY_SIZE (inst->flows)); +}(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_rpc.cQuoted 18 lines
new file mode 100644
index 000000000000..7b5e9177e010drivers/media/platform/amphion/vpu_rpc.c new file +13
-0@@ -0,0 +1,279 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> drivers/media/platform/amphion/vpu_rpc.c line 23
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+#include <linux/platform_device.h> +#include <linux/firmware/imx/ipc.h> +#include <linux/firmware/imx/svc/misc.h> +#include "vpu.h" +#include "vpu_rpc.h" +#include "vpu_imx8q.h" +#include "vpu_windsor.h" +#include "vpu_malone.h" + +u32 vpu_iface_check_memory_region(struct vpu_core *core, dma_addr_taddr, u32 size)
drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->check_memory_region) +return VPU_CORE_MEMORY_INVALID ; + +return ops->check_memory_region (core->fw.phys, addr, size); +} + +static u32 vpu_rpc_check_buffer_space(struct vpu_rpc_buffer_desc *desc,bool write)
Quoted 79 lines
drivers/media/platform/amphion/vpu_rpc.c new file +79
-0+{ +u32 ptr1; +u32 ptr2; +u32 size; + +WARN_ON (!desc); + + size = desc->end - desc->start; +if (write) { + ptr1 = desc->wptr; + ptr2 = desc->rptr; + }else { + ptr1 = desc->rptr; + ptr2 = desc->wptr; + } + +if (ptr1 == ptr2) { +if (!write) +return 0 ; +else +return size; + } + +return (ptr2 + size - ptr1) % size; +} + +static int vpu_rpc_send_cmd_buf (struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 *data; +u32 wptr; +u32 i; + +WARN_ON (!shared || !shared->cmd_mem_vir || !cmd); + + desc = shared->cmd_desc; + space =vpu_rpc_check_buffer_space (desc, true); +if (space < (((cmd->hdr.num +1 ) <<2 ) +16 )) { +pr_err ("Cmd Buffer is no space for [%d] %d\n" , + cmd->hdr.index, cmd->hdr.id); +return -EINVAL ; + } + wptr = desc->wptr; + data = (u32 *)(shared->cmd_mem_vir + desc->wptr - desc->start); + *data =0 ; + *data |= ((cmd->hdr.index &0xff ) <<24 ); + *data |= ((cmd->hdr.num &0xff ) <<16 ); + *data |= (cmd->hdr.id &0x3fff ); + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + +for (i =0 ; i < cmd->hdr.num; i++) { + *data = cmd->data[i]; + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + } + +/*update wptr after data is written*/ +mb (); + desc->wptr = wptr; + +return 0 ; +} + +static bool vpu_rpc_check_msg (struct vpu_shared_addr *shared) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 msgword;drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+u32 msgnum; + +WARN_ON (!shared || !shared->msg_desc); + + desc = shared->msg_desc; + space =vpu_rpc_check_buffer_space (desc,0 ); + space = (space >>2 ); + +if (space) { + msgword = *(u32 *)(shared->msg_mem_vir + desc->rptr -
desc->start);drivers/media/platform/amphion/vpu_rpc.c
Quoted 9 lines
drivers/media/platform/amphion/vpu_rpc.c new file +9
-0+ msgnum = (msgword &0xff0000 ) >>16 ; +if (msgnum <= space) +return true; + } + +return false; +} + +static int vpu_rpc_receive_msg_buf(struct vpu_shared_addr *shared, struct
vpu_rpc_event*msg)Quoted 49 lines
drivers/media/platform/amphion/vpu_rpc.c new file +49
-0+{ +struct vpu_rpc_buffer_desc *desc; +u32 *data; +u32 msgword; +u32 rptr; +u32 i; + +WARN_ON (!shared || !shared->msg_desc || !msg); + +if (!vpu_rpc_check_msg (shared)) +return -EINVAL ; + + desc = shared->msg_desc; + data = (u32 *)(shared->msg_mem_vir + desc->rptr - desc->start); + rptr = desc->rptr; + msgword = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + + msg->hdr.index = (msgword >>24 ) &0xff ; + msg->hdr.num = (msgword >>16 ) &0xff ; + msg->hdr.id = msgword &0x3fff ; + +if (msg->hdr.num >ARRAY_SIZE (msg->data)) { +pr_err ("msg(%d) data length(%d) is out of range\n" , + msg->hdr.id, msg->hdr.num); +return -EINVAL ; + } + +for (i =0 ; i < msg->hdr.num; i++) { + msg->data[i] = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + } + +/*update rptr after data is read*/ +mb (); + desc->rptr = rptr; + +return 0 ; +}drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+ +struct vpu_iface_ops imx8q_rpc_ops[] = { + [VPU_CORE_TYPE_ENC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_windsor_get_data_size, + .check_memory_region =
vpu_imx8q_check_memory_region,drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+ .init_rpc = vpu_windsor_init_rpc, + .set_log_buf = vpu_windsor_set_log_buf, + .set_system_cfg = vpu_windsor_set_system_cfg, + .get_version = vpu_windsor_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .pack_cmd = vpu_windsor_pack_cmd, + .convert_msg_id = vpu_windsor_convert_msg_id, + .unpack_msg_data = vpu_windsor_unpack_msg_data, + .config_memory_resource =
vpu_windsor_config_memory_resource,drivers/media/platform/amphion/vpu_rpc.c line 201
Quoted 1 line
drivers/media/platform/amphion/vpu_rpc.c new file +1
-0+ .get_stream_buffer_size =
vpu_windsor_get_stream_buffer_size,drivers/media/platform/amphion/vpu_rpc.c line 203
Quoted 2 lines
drivers/media/platform/amphion/vpu_rpc.c new file +2
-0+ .config_stream_buffer = vpu_windsor_config_stream_buffer, + .get_stream_buffer_desc =
vpu_windsor_get_stream_buffer_desc,drivers/media/platform/amphion/vpu_rpc.c line 204
Quoted 1 line
drivers/media/platform/amphion/vpu_rpc.c new file +1
-0+ .update_stream_buffer =
vpu_windsor_update_stream_buffer,drivers/media/platform/amphion/vpu_rpc.c
Quoted 3 lines
drivers/media/platform/amphion/vpu_rpc.c new file +3
-0+ .set_encode_params = vpu_windsor_set_encode_params, + .input_frame = vpu_windsor_input_frame, + .get_max_instance_count =
vpu_windsor_get_max_instance_count,drivers/media/platform/amphion/vpu_rpc.c
Quoted 9 lines
drivers/media/platform/amphion/vpu_rpc.c new file +9
-0+ }, + [VPU_CORE_TYPE_DEC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_malone_get_data_size, + .check_memory_region =
vpu_imx8q_check_memory_region,drivers/media/platform/amphion/vpu_rpc.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_rpc.c new file +7
-0+ .init_rpc = vpu_malone_init_rpc, + .set_log_buf = vpu_malone_set_log_buf, + .set_system_cfg = vpu_malone_set_system_cfg, + .get_version = vpu_malone_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .get_stream_buffer_size =
vpu_malone_get_stream_buffer_size,drivers/media/platform/amphion/vpu_rpc.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_rpc.c new file +6
-0+ .config_stream_buffer = vpu_malone_config_stream_buffer, + .set_decode_params = vpu_malone_set_decode_params, + .pack_cmd = vpu_malone_pack_cmd, + .convert_msg_id = vpu_malone_convert_msg_id, + .unpack_msg_data = vpu_malone_unpack_msg_data, + .get_stream_buffer_desc =
vpu_malone_get_stream_buffer_desc,drivers/media/platform/amphion/vpu_rpc.c line 230
Quoted 1 line
drivers/media/platform/amphion/vpu_rpc.c new file +1
-0+ .update_stream_buffer =
vpu_malone_update_stream_buffer,drivers/media/platform/amphion/vpu_rpc.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_rpc.c new file +6
-0+ .add_scode = vpu_malone_add_scode, + .input_frame = vpu_malone_input_frame, + .pre_send_cmd = vpu_malone_pre_cmd, + .post_send_cmd = vpu_malone_post_cmd, + .init_instance = vpu_malone_init_instance, + .get_max_instance_count =
vpu_malone_get_max_instance_count,drivers/media/platform/amphion/vpu_rpc.c line 241
Quoted 5 lines
drivers/media/platform/amphion/vpu_rpc.c new file +5
-0+ }, +}; + + +static struct vpu_iface_ops *vpu_get_iface(struct vpu_dev *vpu, enum
vpu_core_typetype)Review
Quoted 39 lines
drivers/media/platform/amphion/vpu_rpc.c new file +38
-0+{ +struct vpu_iface_ops *rpc_ops =NULL ; +u32 size =0 ; + +WARN_ON (!vpu || !vpu->res); + +switch (vpu->res->plat_type) { +case IMX8QXP : +case IMX8QM : + rpc_ops = imx8q_rpc_ops; + size =ARRAY_SIZE (imx8q_rpc_ops); +break ; +default : +return NULL ; + } + +if (type >= size) +return NULL ; + +return &rpc_ops[type]; +} + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core) +{ +WARN_ON (!core || !core->vpu); + +return vpu_get_iface (core->vpu, core->type); +} + +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst) +{ +WARN_ON (!inst || !inst->vpu); + +if (inst->core) +return vpu_core_get_iface (inst->core); + +return vpu_get_iface (inst->vpu, inst->type); +}(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_rpc.hQuoted 41 lines
new file mode 100644
index 000000000000..abe998e5a5bedrivers/media/platform/amphion/vpu_rpc.h new file +36
-0@@ -0,0 +1,464 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_RPC_H +#define _AMPHION_VPU_RPC_H + +#include <media/videobuf2-core.h> +#include "vpu_codec.h" + +struct vpu_rpc_buffer_desc { +u32 wptr; +u32 rptr; +u32 start; +u32 end; +}; + +struct vpu_shared_addr { +void *iface; +struct vpu_rpc_buffer_desc *cmd_desc; +void *cmd_mem_vir; +struct vpu_rpc_buffer_desc *msg_desc; +void *msg_mem_vir; + +unsigned long boot_addr; +struct vpu_core *core; +void *priv; +}; + +struct vpu_rpc_event_header { +u32 index; +u32 id; +u32 num; +}; +drivers/media/platform/amphion/vpu_rpc.h line 46
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+struct vpu_rpc_event { +struct vpu_rpc_event_header hdr; +u32 data[128 ]; +}; + +struct vpu_iface_ops { +bool (*check_codec)(enum vpu_core_type type); +bool (*check_fmt)(enum vpu_core_type type,u32 pixelfmt); +u32 (*get_data_size)(void ); + u32 (*check_memory_region)(dma_addr_t base, dma_addr_t addr,u32 size);
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+int (*boot_core)(struct vpu_core *core); +int (*shutdown_core)(struct vpu_core *core); +int (*restore_core)(struct vpu_core *core); +int (*get_power_state)(struct vpu_core *core); +int (*on_firmware_loaded)(struct vpu_core *core); +void (*init_rpc)(struct vpu_shared_addr *shared, +struct vpu_buffer *rpc,dma_addr_t boot_addr); +void (*set_log_buf)(struct vpu_shared_addr *shared, +struct vpu_buffer *log); + void (*set_system_cfg)(struct vpu_shared_addr *shared,drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ u32 regs_base, void __iomem *regs, u32 index); +void (*set_stream_cfg)(struct vpu_shared_addr *shared,u32 index); +u32 (*get_version)(struct vpu_shared_addr *shared); +u32 (*get_max_instance_count)(struct vpu_shared_addr *shared); +int (*get_stream_buffer_size)(struct vpu_shared_addr *shared); +int (*send_cmd_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd); +int (*receive_msg_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *msg); + int (*pack_cmd)(struct vpu_rpc_event *pkt, u32 index, u32 id, void*data);
Quoted 4 lines
drivers/media/platform/amphion/vpu_rpc.h new file +4
-0+int (*convert_msg_id)(u32 msg_id); +int (*unpack_msg_data)(struct vpu_rpc_event *pkt,void *data); +int (*input_frame)(struct vpu_shared_addr *shared, +struct vpu_inst *inst,struct vb2_buffer *vb);drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+int (*config_memory_resource)(struct vpu_shared_addr *shared, +u32 instance, +u32 type, +u32 index, +struct vpu_buffer *buf); +int (*config_stream_buffer)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *buf); +int (*update_stream_buffer)(struct vpu_shared_addr *shared, + u32 instance, u32 ptr, boolwrite);
drivers/media/platform/amphion/vpu_rpc.h
Quoted 3 lines
drivers/media/platform/amphion/vpu_rpc.h new file +3
-0+int (*get_stream_buffer_desc)(struct vpu_shared_addr *shared, +u32 instance, + struct vpu_rpc_buffer_desc*desc);
Quoted 21 lines
drivers/media/platform/amphion/vpu_rpc.h new file +21
-0+int (*set_encode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_encode_params *params,u32 update); +int (*set_decode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_decode_params *params,u32 update); +int (*add_scode)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *stream_buffer, +u32 pixelformat, +u32 scode_type); +int (*pre_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*post_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*init_instance)(struct vpu_shared_addr *shared,u32 instance); +}; + +enum { +VPU_CORE_MEMORY_INVALID =0 , +VPU_CORE_MEMORY_CACHED , +VPU_CORE_MEMORY_UNCACHED +};drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ +struct vpu_rpc_region_t { +dma_addr_t start; +dma_addr_t end; +dma_addr_t type; +}; + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core); +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst); +u32 vpu_iface_check_memory_region(struct vpu_core *core, dma_addr_taddr, u32 size);
drivers/media/platform/amphion/vpu_rpc.h
Quoted 12 lines
drivers/media/platform/amphion/vpu_rpc.h new file +12
-0+ +static inline bool vpu_iface_check_codec (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->check_codec) +return ops->check_codec (core->type); + +return true; +} + +static inline bool vpu_iface_check_format(struct vpu_inst *inst, u32pixelfmt)
Quoted 123 lines
drivers/media/platform/amphion/vpu_rpc.h new file +123
-0+{ +struct vpu_iface_ops *ops =vpu_inst_get_iface (inst); + +if (ops && ops->check_fmt) +return ops->check_fmt (inst->type, pixelfmt); + +return true; +} + +static inline int vpu_iface_boot_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->boot_core) +return ops->boot_core (core); +return 0 ; +} + +static inline int vpu_iface_get_power_state (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->get_power_state) +return ops->get_power_state (core); +return 1 ; +} + +static inline int vpu_iface_shutdown_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->shutdown_core) +return ops->shutdown_core (core); +return 0 ; +} + +static inline int vpu_iface_restore_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->restore_core) +return ops->restore_core (core); +return 0 ; +} + +static inline int vpu_iface_on_firmware_loaded (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->on_firmware_loaded) +return ops->on_firmware_loaded (core); + +return 0 ; +} + +static inline u32 vpu_iface_get_data_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_data_size) +return 0 ; + +return ops->get_data_size (); +} + +static inline int vpu_iface_init (struct vpu_core *core, +struct vpu_shared_addr *shared, +struct vpu_buffer *rpc, +dma_addr_t boot_addr) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->init_rpc) +return -EINVAL ; + + ops->init_rpc (shared, rpc, boot_addr); + core->iface = shared; + shared->core = core; +if (rpc->bytesused > rpc->length) +return -ENOSPC ; +return 0 ; +} + +static inline int vpu_iface_set_log_buf (struct vpu_core *core, +struct vpu_buffer *log) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; + +if (ops->set_log_buf) + ops->set_log_buf (core->iface, log); + +return 0 ; +} + +static inline int vpu_iface_config_system (struct vpu_core *core, +u32 regs_base,void __iomem *regs) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; +if (ops->set_system_cfg) + ops->set_system_cfg (core->iface, regs_base, regs, core->id); + +return 0 ; +} + +static inline int vpu_iface_get_stream_buffer_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_stream_buffer_size) +return 0 ; + +return ops->get_stream_buffer_size (core->iface); +} + +static inline int vpu_iface_config_stream (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core);drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ +WARN_ON (inst->id <0 ); +if (!ops) +return -EINVAL ; +if (ops->set_stream_cfg) + ops->set_stream_cfg (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_send_cmd(struct vpu_core *core, struct
vpu_rpc_event*cmd)drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->send_cmd_buf) +return -EINVAL ; + +return ops->send_cmd_buf (core->iface, cmd); +} + +static inline int vpu_iface_receive_msg(struct vpu_core *core, struct
vpu_rpc_event*msg)Quoted 11 lines
drivers/media/platform/amphion/vpu_rpc.h new file +11
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->receive_msg_buf) +return -EINVAL ; + +return ops->receive_msg_buf (core->iface, msg); +} + +static inline int vpu_iface_pack_cmd(struct vpu_core *core, + struct vpu_rpc_event *pkt,drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ u32 index, u32 id, void *data) +{ + struct vpu_iface_ops *ops = vpu_core_get_iface(core); + +if (!ops || !ops->pack_cmd) +return -EINVAL ; +return ops->pack_cmd (pkt, index, id, data); +} + +static inline int vpu_iface_convert_msg_id(struct vpu_core *core, u32
msg_id)drivers/media/platform/amphion/vpu_rpc.h
Quoted 11 lines
drivers/media/platform/amphion/vpu_rpc.h new file +11
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->convert_msg_id) +return -EINVAL ; + +return ops->convert_msg_id (msg_id); +} + +static inline int vpu_iface_unpack_msg_data (struct vpu_core *core, + struct vpu_rpc_event*pkt, void *data)
Quoted 46 lines
drivers/media/platform/amphion/vpu_rpc.h new file +46
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->unpack_msg_data) +return -EINVAL ; + +return ops->unpack_msg_data (pkt, data); +} + +static inline int vpu_iface_input_frame (struct vpu_inst *inst, +struct vb2_buffer *vb) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +if (!ops || !ops->input_frame) +return -EINVAL ; + +return ops->input_frame (inst->core->iface, inst, vb); +} + +static inline int vpu_iface_config_memory_resource (struct vpu_inst *inst, +u32 type,u32 index,struct vpu_buffer *buf) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->config_memory_resource) +return -EINVAL ; + +return ops->config_memory_resource (inst->core->iface, + inst->id, + type, index, buf); +} + +static inline int vpu_iface_config_stream_buffer (struct vpu_inst *inst, +struct vpu_buffer *buf) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->config_stream_buffer) +return -EINVAL ; + +return ops->config_stream_buffer (inst->core->iface, inst->id, buf); +} +drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+static inline int vpu_iface_update_stream_buffer (struct vpu_inst *inst, +u32 ptr,bool write) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->update_stream_buffer) +return -EINVAL ; + + return ops->update_stream_buffer(inst->core->iface, inst->id, ptr,write);
drivers/media/platform/amphion/vpu_rpc.h
Quoted 4 lines
drivers/media/platform/amphion/vpu_rpc.h new file +4
-0+} + +static inline int vpu_iface_get_stream_buffer_desc (struct vpu_inst *inst, + struct vpu_rpc_buffer_desc*desc)
Quoted 25 lines
drivers/media/platform/amphion/vpu_rpc.h new file +25
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->get_stream_buffer_desc) +return -EINVAL ; + +if (!desc) +return 0 ; + +return ops->get_stream_buffer_desc (inst->core->iface, inst->id, desc); +} + +static inline u32 vpu_iface_get_version (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_version) +return 0 ; + +return ops->get_version (core->iface); +} + +static inline u32 vpu_iface_get_max_instance_count (struct vpu_core *core) +{drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_max_instance_count) +return 0 ; + +return ops->get_max_instance_count (core->iface); +} + +static inline int vpu_iface_set_encode_params (struct vpu_inst *inst, + struct vpu_encode_params *params, u32update)
drivers/media/platform/amphion/vpu_rpc.h
Quoted 8 lines
drivers/media/platform/amphion/vpu_rpc.h new file +8
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->set_encode_params) +return -EINVAL ; + + return ops->set_encode_params(inst->core->iface, inst->id, params,update);
drivers/media/platform/amphion/vpu_rpc.h
Quoted 4 lines
drivers/media/platform/amphion/vpu_rpc.h new file +4
-0+} + +static inline int vpu_iface_set_decode_params (struct vpu_inst *inst, + struct vpu_decode_params *params, u32update)
drivers/media/platform/amphion/vpu_rpc.h
Quoted 8 lines
drivers/media/platform/amphion/vpu_rpc.h new file +8
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->set_decode_params) +return -EINVAL ; + + return ops->set_decode_params(inst->core->iface, inst->id, params,update);
Quoted 49 lines
drivers/media/platform/amphion/vpu_rpc.h new file +48
-0+} + +static inline int vpu_iface_add_scode (struct vpu_inst *inst,u32 scode_type) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->add_scode) +return -EINVAL ; + +return ops->add_scode (inst->core->iface, inst->id, + &inst->stream_buffer, + inst->out_format.pixfmt, + scode_type); +} + +static inline int vpu_iface_pre_send_cmd (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->pre_send_cmd) +return ops->pre_send_cmd (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_post_send_cmd (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->post_send_cmd) +return ops->post_send_cmd (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_init_instance (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->init_instance) +return ops->init_instance (inst->core->iface, inst->id); + +return 0 ; +} + +#endif
Ming Qian
maintainerRE: [EXT] Re: [PATCH v13 04/13] media: amphion: add vpu core driver
In reply to Hans VerkuilQuoted 14 lines
-----Original Message-----
From: Hans Verkuil [mailto:[email protected]]
Sent: Thursday, December 2, 2021 5:05 PM
To: Ming Qian <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; dl-linux-imx <[email protected]>; Aisheng Dong <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXT] Re: [PATCH v13 04/13] media: amphion: add vpu core driverCaution: EXT Email
On 30/11/2021 10:48, Ming Qian wrote:
Quoted 8 lines
Quoted 2 lines
The vpu supports encoder and decoder. it needs mu core to handle it.
"mu core"? Do you mean "vpu core"? If not, then what is a "mu core"?
Regards,
Hans
Yes, it means "vpu core", we often call it mu internally. I'm sorry that my statement caused confusion
Quoted 24 lines
Quoted 11 lines
core will run either encoder or decoder firmware.
This driver is for support the vpu core.
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- drivers/media/platform/amphion/vpu_codec.h | 67 ++ drivers/media/platform/amphion/vpu_core.c | 906+++++++++++++++++++++
Quoted 12 lines
drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++++++++ drivers/media/platform/amphion/vpu_rpc.c | 279 +++++++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++++++++ 6 files changed, 2226 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.h
Re: [PATCH v13 04/13] media: amphion: add vpu core driver
In reply to Ming QianQuoted 136 lines · Ming Qian
The vpu supports encoder and decoder.
it needs mu core to handle it.
core will run either encoder or decoder firmware.This driver is for support the vpu core.
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- drivers/media/platform/amphion/vpu_codec.h | 67 ++ drivers/media/platform/amphion/vpu_core.c | 906 +++++++++++++++++++++ drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++++++++ drivers/media/platform/amphion/vpu_rpc.c | 279 +++++++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++++++++ 6 files changed, 2226 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.hdrivers/media/platform/amphion/vpu_codec.h new file +67
-0@@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CODEC_H +#define _AMPHION_VPU_CODEC_H + +struct vpu_encode_params { +u32 input_format; +u32 codec_format; +u32 profile; +u32 tier; +u32 level; +struct v4l2_fract frame_rate; +u32 src_stride; +u32 src_width; +u32 src_height; +struct v4l2_rect crop; +u32 out_width; +u32 out_height; + +u32 gop_length; +u32 bframes; + +u32 rc_mode; +u32 bitrate; +u32 bitrate_min; +u32 bitrate_max; + +u32 i_frame_qp; +u32 p_frame_qp; +u32 b_frame_qp; +u32 qp_min; +u32 qp_max; +u32 qp_min_i; +u32 qp_max_i; + +struct { +u32 enable; +u32 idc; +u32 width; +u32 height; + } sar; + +struct { +u32 primaries; +u32 transfer; +u32 matrix; +u32 full_range; + } color; +}; + +struct vpu_decode_params { +u32 codec_format; +u32 output_format; +u32 b_dis_reorder; +u32 b_non_frame; +u32 frame_count; +u32 end_flag; +struct { +u32 base; +u32 size; + } udata; +}; + +#endif drivers/media/platform/amphion/vpu_core.c new file +33
-0@@ -0,0 +1,906 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <linux/pm_domain.h> +#include <linux/firmware.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_core.h" +#include "vpu_mbox.h" +#include "vpu_msgs.h" +#include "vpu_rpc.h" +#include "vpu_cmds.h" + +void csr_writel (struct vpu_core *core,u32 reg,u32 val) +{ +writel (val, core->base + reg); +} + +u32 csr_readl (struct vpu_core *core,u32 reg)
drivers/media/platform/amphion/vpu_core.c line 43
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+{ +return readl (core->base + reg); +} + +static int vpu_core_load_firmware (struct vpu_core *core) +{ +const struct firmware *pfw =NULL ; +int ret =0 ; + +WARN_ON (!core || !core->res || !core->res->fwname);
Either do:
if (WARN_ON(!core || !core->res || !core->res->fwname)) return -EINVAL;
or just drop it. You'll get a oops with backtrace soon enough.
Same elsewhere in this driver.
Quoted 627 lines
drivers/media/platform/amphion/vpu_core.c new file +627
-0+if (!core->fw.virt) { +dev_err (core->dev,"firmware buffer is not ready\n" ); +return -EINVAL ; + } + + ret =request_firmware (&pfw, core->res->fwname, core->dev); +dev_dbg (core->dev,"request_firmware %s : %d\n" , core->res->fwname, ret); +if (ret) { +dev_err (core->dev,"request firmware %s failed, ret = %d\n" , + core->res->fwname, ret); +return ret; + } + +if (core->fw.length < pfw->size) { +dev_err (core->dev,"firmware buffer size want %zu, but %d\n" , + pfw->size, core->fw.length); + ret = -EINVAL ; + goto exit; + } + +memset_io (core->fw.virt,0 , core->fw.length); +memcpy (core->fw.virt, pfw->data, pfw->size); + core->fw.bytesused = pfw->size; + ret =vpu_iface_on_firmware_loaded (core); +exit: +release_firmware (pfw); + pfw =NULL ; + +return ret; +} + +static int vpu_core_boot_done (struct vpu_core *core) +{ +u32 fw_version; + + fw_version =vpu_iface_get_version (core); +dev_info (core->dev,"%s firmware version : %d.%d.%d\n" , +vpu_core_type_desc (core->type), + (fw_version >>16 ) &0xff , + (fw_version >>8 ) &0xff , + fw_version &0xff ); + core->supported_instance_count =vpu_iface_get_max_instance_count (core); +if (core->res->act_size) { +u32 count = core->act.length / core->res->act_size; + + core->supported_instance_count =min (core->supported_instance_count, count); + } + core->fw_version = fw_version; + core->state =VPU_CORE_ACTIVE ; + +return 0 ; +} + +static int vpu_core_wait_boot_done (struct vpu_core *core) +{ +int ret; + + ret =wait_for_completion_timeout (&core->cmp,VPU_TIMEOUT ); +if (!ret) { +dev_err (core->dev,"boot timeout\n" ); +return -EINVAL ; + } +return vpu_core_boot_done (core); +} + +static int vpu_core_boot (struct vpu_core *core,bool load) +{ +int ret; + +WARN_ON (!core); + +if (!core->res->standalone) +return 0 ; + +reinit_completion (&core->cmp); +if (load) { + ret =vpu_core_load_firmware (core); +if (ret) +return ret; + } + +vpu_iface_boot_core (core); +return vpu_core_wait_boot_done (core); +} + +static int vpu_core_shutdown (struct vpu_core *core) +{ +if (!core->res->standalone) +return 0 ; +return vpu_iface_shutdown_core (core); +} + +static int vpu_core_restore (struct vpu_core *core) +{ +int ret; + +if (!core->res->standalone) +return 0 ; + ret =vpu_core_sw_reset (core); +if (ret) +return ret; + +vpu_core_boot_done (core); +return vpu_iface_restore_core (core); +} + +static int __vpu_alloc_dma (struct device *dev,struct vpu_buffer *buf) +{ +gfp_t gfp =GFP_KERNEL |GFP_DMA32 ; + +WARN_ON (!dev || !buf); + +if (!buf->length) +return 0 ; + + buf->virt =dma_alloc_coherent (dev, buf->length, &buf->phys, gfp); +if (!buf->virt) +return -ENOMEM ; + + buf->dev = dev; + +return 0 ; +} + +void vpu_free_dma (struct vpu_buffer *buf) +{ +WARN_ON (!buf); + +if (!buf->virt || !buf->dev) +return ; + +dma_free_coherent (buf->dev, buf->length, buf->virt, buf->phys); + buf->virt =NULL ; + buf->phys =0 ; + buf->length =0 ; + buf->bytesused =0 ; + buf->dev =NULL ; +} + +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf) +{ +WARN_ON (!core || !buf); + +return __vpu_alloc_dma (core->dev, buf); +} + +static void vpu_core_check_hang (struct vpu_core *core) +{ +if (core->hang_mask) + core->state =VPU_CORE_HANG ; +} + +static struct vpu_core *vpu_core_find_proper_by_type (struct vpu_dev *vpu,u32 type) +{ +struct vpu_core *core =NULL ; +int request_count =INT_MAX ; +struct vpu_core *c; + +WARN_ON (!vpu); + +list_for_each_entry (c, &vpu->cores, list) { +dev_dbg (c->dev,"instance_mask = 0x%lx, state = %d\n" , + c->instance_mask, + c->state); +if (c->type != type) +continue ; +if (c->state ==VPU_CORE_DEINIT ) { + core = c; +break ; + } +vpu_core_check_hang (c); +if (c->state !=VPU_CORE_ACTIVE ) +continue ; +if (c->request_count < request_count) { + request_count = c->request_count; + core = c; + } +if (!request_count) +break ; + } + +return core; +} + +static bool vpu_core_is_exist (struct vpu_dev *vpu,struct vpu_core *core) +{ +struct vpu_core *c; + +list_for_each_entry (c, &vpu->cores, list) { +if (c == core) +return true; + } + +return false; +} + +static void vpu_core_get_vpu (struct vpu_core *core) +{ + core->vpu->get_vpu (core->vpu); +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->get_enc (core->vpu); +if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->get_dec (core->vpu); +} + +static int vpu_core_register (struct device *dev,struct vpu_core *core) +{ +struct vpu_dev *vpu =dev_get_drvdata (dev); +int ret =0 ; + +dev_dbg (core->dev,"register core %s\n" ,vpu_core_type_desc (core->type)); +if (vpu_core_is_exist (vpu, core)) +return 0 ; + + core->workqueue =alloc_workqueue ("vpu" ,WQ_UNBOUND |WQ_MEM_RECLAIM ,1 ); +if (!core->workqueue) { +dev_err (core->dev,"fail to alloc workqueue\n" ); +return -ENOMEM ; + } +INIT_WORK (&core->msg_work, vpu_msg_run_work); +INIT_DELAYED_WORK (&core->msg_delayed_work, vpu_msg_delayed_work); + core->msg_buffer_size =roundup_pow_of_two (VPU_MSG_BUFFER_SIZE ); + core->msg_buffer =vzalloc (core->msg_buffer_size); +if (!core->msg_buffer) { +dev_err (core->dev,"failed allocate buffer for fifo\n" ); + ret = -ENOMEM ; + goto error; + } + ret =kfifo_init (&core->msg_fifo, core->msg_buffer, core->msg_buffer_size); +if (ret) { +dev_err (core->dev,"failed init kfifo\n" ); + goto error; + } + +list_add_tail (&core->list, &vpu->cores); + +vpu_core_get_vpu (core); + +if (vpu_iface_get_power_state (core)) + ret =vpu_core_restore (core); +if (ret) + goto error; + +return 0 ; +error: +if (core->msg_buffer) { +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + } +if (core->workqueue) { +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } +return ret; +} + +static void vpu_core_put_vpu (struct vpu_core *core) +{ +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->put_enc (core->vpu); +if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->put_dec (core->vpu); + core->vpu->put_vpu (core->vpu); +} + +static int vpu_core_unregister (struct device *dev,struct vpu_core *core) +{ +list_del_init (&core->list); + +vpu_core_put_vpu (core); + core->vpu =NULL ; +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + +if (core->workqueue) { +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } + +return 0 ; +} + +static int vpu_core_acquire_instance (struct vpu_core *core) +{ +int id; + +WARN_ON (!core); + + id =ffz (core->instance_mask); +if (id >= core->supported_instance_count) +return -EINVAL ; + +set_bit (id, &core->instance_mask); + +return id; +} + +static void vpu_core_release_instance (struct vpu_core *core,int id) +{ +WARN_ON (!core); + +if (id <0 || id >= core->supported_instance_count) +return ; + +clear_bit (id, &core->instance_mask); +} + +struct vpu_inst *vpu_inst_get (struct vpu_inst *inst) +{ +if (!inst) +return NULL ; + +atomic_inc (&inst->ref_count); + +return inst; +} + +void vpu_inst_put (struct vpu_inst *inst) +{ +if (!inst) +return ; +if (atomic_dec_and_test (&inst->ref_count)) { +if (inst->release) + inst->release (inst); + } +} + +struct vpu_core *vpu_request_core (struct vpu_dev *vpu,enum vpu_core_type type) +{ +struct vpu_core *core =NULL ; +int ret; + +mutex_lock (&vpu->lock); + + core =vpu_core_find_proper_by_type (vpu, type); +if (!core) + goto exit; + +mutex_lock (&core->lock); +pm_runtime_get_sync (core->dev); + +if (core->state ==VPU_CORE_DEINIT ) { + ret =vpu_core_boot (core, true); +if (ret) { +pm_runtime_put_sync (core->dev); +mutex_unlock (&core->lock); + core =NULL ; + goto exit; + } + } + + core->request_count++; + +mutex_unlock (&core->lock); +exit: +mutex_unlock (&vpu->lock); + +return core; +} + +void vpu_release_core (struct vpu_core *core) +{ +if (!core) +return ; + +mutex_lock (&core->lock); +pm_runtime_put_sync (core->dev); +if (core->request_count) + core->request_count--; +mutex_unlock (&core->lock); +} + +int vpu_inst_register (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core; +int ret =0 ; + +WARN_ON (!inst || !inst->vpu); + + vpu = inst->vpu; + core = inst->core; +if (!core) { + core =vpu_request_core (vpu, inst->type); +if (!core) { +dev_err (vpu->dev,"there is no vpu core for %s\n" , +vpu_core_type_desc (inst->type)); +return -EINVAL ; + } + inst->core = core; + inst->dev =get_device (core->dev); + } + +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) + goto exit; + + ret =vpu_core_acquire_instance (core); +if (ret <0 ) + goto exit; + +vpu_trace (inst->dev,"[%d] %p\n" , ret, inst); + inst->id = ret; +list_add_tail (&inst->list, &core->instances); + ret =0 ; +if (core->res->act_size) { + inst->act.phys = core->act.phys + core->res->act_size * inst->id; + inst->act.virt = core->act.virt + core->res->act_size * inst->id; + inst->act.length = core->res->act_size; + } +vpu_inst_create_dbgfs_file (inst); +exit: +mutex_unlock (&core->lock); + +if (ret) +dev_err (core->dev,"register instance fail\n" ); +return ret; +} + +int vpu_inst_unregister (struct vpu_inst *inst) +{ +struct vpu_core *core; + +WARN_ON (!inst); + +if (!inst->core) +return 0 ; + + core = inst->core; +vpu_clear_request (inst); +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) { +vpu_inst_remove_dbgfs_file (inst); +list_del_init (&inst->list); +vpu_core_release_instance (core, inst->id); + inst->id =VPU_INST_NULL_ID ; + } +vpu_core_check_hang (core); +if (core->state ==VPU_CORE_HANG && !core->instance_mask) { +dev_info (core->dev,"reset hang core\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); + +return 0 ; +} + +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index) +{ +struct vpu_inst *inst =NULL ; +struct vpu_inst *tmp; + +mutex_lock (&core->lock); +if (!test_bit (index, &core->instance_mask)) + goto exit; +list_for_each_entry (tmp, &core->instances, list) { +if (tmp->id == index) { + inst =vpu_inst_get (tmp); +break ; + } + } +exit: +mutex_unlock (&core->lock); + +return inst; +} + +const struct vpu_core_resources *vpu_get_resource (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core =NULL ; +const struct vpu_core_resources *res =NULL ; + +if (!inst || !inst->vpu) +return NULL ; + +if (inst->core && inst->core->res) +return inst->core->res; + + vpu = inst->vpu; +mutex_lock (&vpu->lock); +list_for_each_entry (core, &vpu->cores, list) { +if (core->type == inst->type) { + res = core->res; +break ; + } + } +mutex_unlock (&vpu->lock); + +return res; +} + +static int vpu_core_parse_dt (struct vpu_core *core,struct device_node *np) +{ +struct device_node *node; +struct resource res; + +if (of_count_phandle_with_args (np,"memory-region" ,NULL ) <2 ) { +dev_err (core->dev,"need 2 memory-region for boot and rpc\n" ); +return -ENODEV ; + } + + node =of_parse_phandle (np,"memory-region" ,0 ); +if (!node) { +dev_err (core->dev,"boot-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { +dev_err (core->dev,"boot-region of_address_to_resource error\n" ); +return -EINVAL ; + } + core->fw.phys = res.start; + core->fw.length =resource_size (&res); + + node =of_parse_phandle (np,"memory-region" ,1 ); +if (!node) { +dev_err (core->dev,"rpc-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { +dev_err (core->dev,"rpc-region of_address_to_resource error\n" ); +return -EINVAL ; + } + core->rpc.phys = res.start; + core->rpc.length =resource_size (&res); + +if (core->rpc.length < core->res->rpc_size + core->res->fwlog_size) { +dev_err (core->dev,"the rpc-region <%pad, 0x%x> is not enough\n" , + &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->fw.virt =ioremap_wc (core->fw.phys, core->fw.length); + core->rpc.virt =ioremap_wc (core->rpc.phys, core->rpc.length); +memset_io (core->rpc.virt,0 , core->rpc.length); + +if (vpu_iface_check_memory_region (core, + core->rpc.phys, + core->rpc.length) !=VPU_CORE_MEMORY_UNCACHED ) { +dev_err (core->dev,"rpc region<%pad, 0x%x> isn't uncached\n" , + &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->log.phys = core->rpc.phys + core->res->rpc_size; + core->log.virt = core->rpc.virt + core->res->rpc_size; + core->log.length = core->res->fwlog_size; + core->act.phys = core->log.phys + core->log.length; + core->act.virt = core->log.virt + core->log.length; + core->act.length = core->rpc.length - core->res->rpc_size - core->log.length; + core->rpc.length = core->res->rpc_size; + +return 0 ; +} + +static int vpu_core_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core; +struct vpu_dev *vpu =dev_get_drvdata (dev->parent); +struct vpu_shared_addr *iface; +u32 iface_data_size; +int ret; + +dev_dbg (dev,"probe\n" ); +if (!vpu) +return -EINVAL ; + core =devm_kzalloc (dev,sizeof (*core),GFP_KERNEL ); +if (!core) +return -ENOMEM ; + + core->pdev = pdev; + core->dev = dev; +platform_set_drvdata (pdev, core); + core->vpu = vpu; +INIT_LIST_HEAD (&core->instances); +mutex_init (&core->lock); +mutex_init (&core->cmd_lock); +init_completion (&core->cmp); +init_waitqueue_head (&core->ack_wq); + core->state =VPU_CORE_DEINIT ; + + core->res =of_device_get_match_data (dev); +if (!core->res) +return -ENODEV ; + + core->type = core->res->type; + core->id =of_alias_get_id (dev->of_node,"vpu_core" ); +if (core->id <0 ) { +dev_err (dev,"can't get vpu core id\n" ); +return core->id; + } +dev_info (core->dev,"[%d] = %s\n" , core->id,vpu_core_type_desc (core->type)); + ret =vpu_core_parse_dt (core, dev->of_node); +if (ret) +return ret; + + core->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (core->base)) +return PTR_ERR (core->base); + +if (!vpu_iface_check_codec (core)) { +dev_err (core->dev,"is not supported\n" ); +return -EINVAL ; + } + + ret =vpu_mbox_init (core); +if (ret) +return ret; + + iface =devm_kzalloc (dev,sizeof (*iface),GFP_KERNEL ); +if (!iface) +return -ENOMEM ; + + iface_data_size =vpu_iface_get_data_size (core); +if (iface_data_size) { + iface->priv =devm_kzalloc (dev, iface_data_size,GFP_KERNEL ); +if (!iface->priv) +return -ENOMEM ; + } + + ret =vpu_iface_init (core, iface, &core->rpc, core->fw.phys);
drivers/media/platform/amphion/vpu_core.c line 680
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (ret) { +dev_err (core->dev,"init iface fail, ret = %d\n" , ret); +return ret; + } + +vpu_iface_config_system (core, vpu->res->mreg_base, vpu->base); +vpu_iface_set_log_buf (core, &core->log); + +pm_runtime_enable (dev); + ret =pm_runtime_get_sync (dev);
Use pm_runtime_resume_and_get() instead and drop the pm_runtime_put_noidle() in the 'if' below. The use of pm_runtime_resume_and_get is preferred over the rather confusing pm_runtime_get_sync().
If it is used elsewhere in this series as well (I haven't checked this), then make the same changes.
Quoted 22 lines
drivers/media/platform/amphion/vpu_core.c new file +22
-0+if (ret) { +pm_runtime_put_noidle (dev); +pm_runtime_set_suspended (dev); + goto err_runtime_disable; + } + + ret =vpu_core_register (dev->parent, core); +if (ret) + goto err_core_register; + core->parent = dev->parent; + +pm_runtime_put_sync (dev); +vpu_core_create_dbgfs_file (core); + +return 0 ; + +err_core_register: +pm_runtime_put_sync (dev); +err_runtime_disable: +pm_runtime_disable (dev); + +return ret;
drivers/media/platform/amphion/vpu_core.c line 712
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+} + +static int vpu_core_remove (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core =platform_get_drvdata (pdev); +int ret; + +vpu_core_remove_dbgfs_file (core); + ret =pm_runtime_get_sync (dev);
Ah, same here.
Quoted 1,462 lines
drivers/media/platform/amphion/vpu_core.c new file +194
-0+WARN_ON (ret <0 ); + +vpu_core_shutdown (core); +pm_runtime_put_sync (dev); +pm_runtime_disable (dev); + +vpu_core_unregister (core->parent, core); +iounmap (core->fw.virt); +iounmap (core->rpc.virt); +mutex_destroy (&core->lock); +mutex_destroy (&core->cmd_lock); + +return 0 ; +} + +static int __maybe_unused vpu_core_runtime_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +return vpu_mbox_request (core); +} + +static int __maybe_unused vpu_core_runtime_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +vpu_mbox_free (core); +return 0 ; +} + +static void vpu_core_cancel_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; + +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); + +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +cancel_work_sync (&inst->msg_work); +mutex_unlock (&core->lock); +} + +static void vpu_core_resume_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; +unsigned long delay =msecs_to_jiffies (10 ); + +queue_work (core->workqueue, &core->msg_work); +queue_delayed_work (core->workqueue, &core->msg_delayed_work, delay); + +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +queue_work (inst->workqueue, &inst->msg_work); +mutex_unlock (&core->lock); +} + +static int __maybe_unused vpu_core_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock); +pm_runtime_get_sync (dev); +vpu_core_get_vpu (core); +if (core->state !=VPU_CORE_SNAPSHOT ) + goto exit; + +if (!vpu_iface_get_power_state (core)) { +if (!list_empty (&core->instances)) { + ret =vpu_core_boot (core, false); +if (ret) { +dev_err (core->dev,"%s boot fail\n" , __func__); + core->state =VPU_CORE_DEINIT ; + goto exit; + } + }else { + core->state =VPU_CORE_DEINIT ; + } + }else { +if (!list_empty (&core->instances)) { + ret =vpu_core_sw_reset (core); +if (ret) { +dev_err (core->dev,"%s sw_reset fail\n" , __func__); + core->state =VPU_CORE_HANG ; + goto exit; + } + } + core->state =VPU_CORE_ACTIVE ; + } + +exit: +pm_runtime_put_sync (dev); +mutex_unlock (&core->lock); + +vpu_core_resume_work (core); +return ret; +} + +static int __maybe_unused vpu_core_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock); +if (core->state ==VPU_CORE_ACTIVE ) { +if (!list_empty (&core->instances)) { + ret =vpu_core_snapshot (core); +if (ret) { +mutex_unlock (&core->lock); +return ret; + } + } + + core->state =VPU_CORE_SNAPSHOT ; + } +mutex_unlock (&core->lock); + +vpu_core_cancel_work (core); + +mutex_lock (&core->lock); +vpu_core_put_vpu (core); +mutex_unlock (&core->lock); +return ret; +} + +static const struct dev_pm_ops vpu_core_pm_ops = { + SET_RUNTIME_PM_OPS(vpu_core_runtime_suspend, vpu_core_runtime_resume, NULL) +SET_SYSTEM_SLEEP_PM_OPS (vpu_core_suspend, vpu_core_resume) +}; + +static struct vpu_core_resources imx8q_enc = { + .type =VPU_CORE_TYPE_ENC , + .fwname ="vpu/vpu_fw_imx8_enc.bin" , + .stride =16 , + .max_width =1920 , + .max_height =1920 , + .min_width =64 , + .min_height =48 , + .step_width =2 , + .step_height =2 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .act_size =0xc0000 , + .standalone = true, +}; + +static struct vpu_core_resources imx8q_dec = { + .type =VPU_CORE_TYPE_DEC , + .fwname ="vpu/vpu_fw_imx8_dec.bin" , + .stride =256 , + .max_width =8188 , + .max_height =8188 , + .min_width =16 , + .min_height =16 , + .step_width =1 , + .step_height =1 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .standalone = true, +}; + +static const struct of_device_id vpu_core_dt_match[] = { + { .compatible ="nxp,imx8q-vpu-encoder" , .data = &imx8q_enc }, + { .compatible ="nxp,imx8q-vpu-decoder" , .data = &imx8q_dec }, + {} +}; +MODULE_DEVICE_TABLE (of, vpu_core_dt_match); + +static struct platform_driver amphion_vpu_core_driver = { + .probe = vpu_core_probe, + .remove = vpu_core_remove, + .driver = { + .name ="amphion-vpu-core" , + .of_match_table = vpu_core_dt_match, + .pm = &vpu_core_pm_ops, + }, +}; + +int __init vpu_core_driver_init (void ) +{ +return platform_driver_register (&hion_vpu_core_driver); +} + +void __exit vpu_core_driver_exit(void) +{ +platform_driver_unregister (&hion_vpu_core_driver); +}drivers/media/platform/amphion/vpu_core.h new file +15
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CORE_H +#define _AMPHION_VPU_CORE_H + +void csr_writel (struct vpu_core *core,u32 reg,u32 val); +u32 csr_readl (struct vpu_core *core,u32 reg); +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf); +void vpu_free_dma (struct vpu_buffer *buf); +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index); + +#endif drivers/media/platform/amphion/vpu_dbg.c new file +495
-0@@ -0,0 +1,495 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <media/v4l2-device.h> +#include <linux/debugfs.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_helpers.h" +#include "vpu_cmds.h" +#include "vpu_rpc.h" + +struct print_buf_desc { +u32 start_h_phy; +u32 start_h_vir; +u32 start_m; +u32 bytes; +u32 read; +u32 write; +char buffer[0 ]; +}; + +static char *vb2_stat_name[] = { + [VB2_BUF_STATE_DEQUEUED ] ="dequeued" , + [VB2_BUF_STATE_IN_REQUEST ] ="in_request" , + [VB2_BUF_STATE_PREPARING ] ="preparing" , + [VB2_BUF_STATE_QUEUED ] ="queued" , + [VB2_BUF_STATE_ACTIVE ] ="active" , + [VB2_BUF_STATE_DONE ] ="done" , + [VB2_BUF_STATE_ERROR ] ="error" , +}; + +static char *vpu_stat_name[] = { + [VPU_BUF_STATE_IDLE ] ="idle" , + [VPU_BUF_STATE_INUSE ] ="inuse" , + [VPU_BUF_STATE_DECODED ] ="decoded" , + [VPU_BUF_STATE_READY ] ="ready" , + [VPU_BUF_STATE_SKIP ] ="skip" , + [VPU_BUF_STATE_ERROR ] ="error" , +}; + +static int vpu_dbg_instance (struct seq_file *s,void *data) +{ +struct vpu_inst *inst = s->private; +char str[128 ]; +int num; +struct vb2_queue *vq; +int i; + + num =scnprintf (str,sizeof (str),"[%s]\n" ,vpu_core_type_desc (inst->type)); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"tgig = %d,pid = %d\n" , inst->tgid, inst->pid); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"state = %d\n" , inst->state); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str), +"min_buffer_out = %d, min_buffer_cap = %d\n" , + inst->min_buffer_out, inst->min_buffer_cap); +if (seq_write (s, str, num)) +return 0 ; + + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); + num =scnprintf (str,sizeof (str), +"output (%2d, %2d): fmt = %c%c%c%c %d x %d, %d;" , +vb2_is_streaming (vq), + vq->num_buffers, + inst->out_format.pixfmt, + inst->out_format.pixfmt >>8 , + inst->out_format.pixfmt >>16 , + inst->out_format.pixfmt >>24 , + inst->out_format.width, + inst->out_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->out_format.num_planes; i++) { + num =scnprintf (str,sizeof (str)," %d(%d)" , + inst->out_format.sizeimage[i], + inst->out_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + num =scnprintf (str,sizeof (str), +"capture(%2d, %2d): fmt = %c%c%c%c %d x %d, %d;" , +vb2_is_streaming (vq), + vq->num_buffers, + inst->cap_format.pixfmt, + inst->cap_format.pixfmt >>8 , + inst->cap_format.pixfmt >>16 , + inst->cap_format.pixfmt >>24 , + inst->cap_format.width, + inst->cap_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->cap_format.num_planes; i++) { + num =scnprintf (str,sizeof (str)," %d(%d)" , + inst->cap_format.sizeimage[i], + inst->cap_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + num =scnprintf (str,sizeof (str),"crop: (%d, %d) %d x %d\n" , + inst->crop.left, + inst->crop.top, + inst->crop.width, + inst->crop.height); +if (seq_write (s, str, num)) +return 0 ; + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"output [%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]); +if (seq_write (s, str, num)) +return 0 ; + } + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"capture[%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]); +if (seq_write (s, str, num)) +return 0 ; + } + + num =scnprintf (str,sizeof (str),"sequence = %d\n" , inst->sequence); +if (seq_write (s, str, num)) +return 0 ; + +if (inst->use_stream_buffer) { + num =scnprintf (str,sizeof (str),"stream_buffer = %d / %d, <%pad, 0x%x>\n" , +vpu_helper_get_used_space (inst), + inst->stream_buffer.length, + &inst->stream_buffer.phys, + inst->stream_buffer.length); +if (seq_write (s, str, num)) +return 0 ; + } + num =scnprintf (str,sizeof (str),"kfifo len = 0x%x\n" ,kfifo_len (&inst->msg_fifo)); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"flow :\n" ); +if (seq_write (s, str, num)) +return 0 ; + +mutex_lock (&inst->core->cmd_lock); +for (i =0 ; i <ARRAY_SIZE (inst->flows); i++) { +u32 idx = (inst->flow_idx + i) % (ARRAY_SIZE (inst->flows)); + +if (!inst->flows[idx]) +continue ; + num =scnprintf (str,sizeof (str),"\t[%s]0x%x\n" , + inst->flows[idx] >=VPU_MSG_ID_NOOP ?"M" :"C" , + inst->flows[idx]); +if (seq_write (s, str, num)) { +mutex_unlock (&inst->core->cmd_lock); +return 0 ; + } + } +mutex_unlock (&inst->core->cmd_lock); + + i =0 ; +while (true) { + num =call_vop (inst, get_debug_info, str,sizeof (str), i++); +if (num <=0 ) +break ; +if (seq_write (s, str, num)) +return 0 ; + } + +return 0 ; +} + +static int vpu_dbg_core (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct vpu_shared_addr *iface = core->iface; +char str[128 ]; +int num; + + num =scnprintf (str,sizeof (str),"[%s]\n" ,vpu_core_type_desc (core->type)); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"boot_region = <%pad, 0x%x>\n" , + &core->fw.phys, core->fw.length); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"rpc_region = <%pad, 0x%x> used = 0x%x\n" , + &core->rpc.phys, core->rpc.length, core->rpc.bytesused); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"fwlog_region = <%pad, 0x%x>\n" , + &core->log.phys, core->log.length); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"state = %d\n" , core->state); +if (seq_write (s, str, num)) +return 0 ; +if (core->state ==VPU_CORE_DEINIT ) +return 0 ; + num =scnprintf (str,sizeof (str),"fw version = %d.%d.%d\n" , + (core->fw_version >>16 ) &0xff , + (core->fw_version >>8 ) &0xff , + core->fw_version &0xff ); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"instances = %d/%d (0x%02lx), %d\n" , +hweight32 (core->instance_mask), + core->supported_instance_count, + core->instance_mask, + core->request_count); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"kfifo len = 0x%x\n" ,kfifo_len (&core->msg_fifo)); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str), +"cmd_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n" , + iface->cmd_desc->start, + iface->cmd_desc->end, + iface->cmd_desc->wptr, + iface->cmd_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str), +"msg_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n" , + iface->msg_desc->start, + iface->msg_desc->end, + iface->msg_desc->wptr, + iface->msg_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + +return 0 ; +} + +static int vpu_dbg_fwlog (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct print_buf_desc *print_buf; +int length; +u32 rptr; +u32 wptr; +int ret =0 ; + +if (!core->log.virt || core->state ==VPU_CORE_DEINIT ) +return 0 ; + + print_buf = core->log.virt; + rptr = print_buf->read; + wptr = print_buf->write; + +if (rptr == wptr) +return 0 ; +else if (rptr < wptr) + length = wptr - rptr; +else + length = print_buf->bytes + wptr - rptr; + +if (s->count + length >= s->size) { + s->count = s->size; +return 0 ; + } + +if (rptr + length >= print_buf->bytes) { +int num = print_buf->bytes - rptr; + +if (seq_write (s, print_buf->buffer + rptr, num)) + ret =-1 ; + length -= num; + rptr =0 ; + } + +if (length) { +if (seq_write (s, print_buf->buffer + rptr, length)) + ret =-1 ; + rptr += length; + } +if (!ret) + print_buf->read = rptr; + +return 0 ; +} + +static int vpu_dbg_inst_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_instance, inode->i_private); +} + +static ssize_t vpu_dbg_inst_write (struct file *file, +const char __user *user_buf,size_t size,loff_t *ppos) +{ +struct seq_file *s = file->private_data; +struct vpu_inst *inst = s->private; + +vpu_session_debug (inst); + +return size; +} + +static ssize_t vpu_dbg_core_write (struct file *file, +const char __user *user_buf,size_t size,loff_t *ppos) +{ +struct seq_file *s = file->private_data; +struct vpu_core *core = s->private; + +pm_runtime_get_sync (core->dev); +mutex_lock (&core->lock); +if (core->state !=VPU_CORE_DEINIT && !core->instance_mask) { +dev_info (core->dev,"reset\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); +pm_runtime_put_sync (core->dev); + +return size; +} + +static int vpu_dbg_core_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_core, inode->i_private); +} + +static int vpu_dbg_fwlog_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_fwlog, inode->i_private); +} + +static const struct file_operations vpu_dbg_inst_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_inst_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_inst_write, +}; + +static const struct file_operations vpu_dbg_core_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_core_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_core_write, +}; + +static const struct file_operations vpu_dbg_fwlog_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_fwlog_open, + .release = single_release, + .read = seq_read, +}; + +int vpu_inst_create_dbgfs_file (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!inst || !inst->core || !inst->core->vpu) +return -EINVAL ; + + vpu = inst->core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (inst->debugfs) +return 0 ; + +scnprintf (name,sizeof (name),"instance.%d.%d" , + inst->core->id, inst->id); + inst->debugfs =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0644 ), + vpu->debugfs, + inst, + &vpu_dbg_inst_fops); +if (!inst->debugfs) { +dev_err (inst->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + +return 0 ; +} + +int vpu_inst_remove_dbgfs_file (struct vpu_inst *inst) +{ +if (!inst) +return 0 ; + +debugfs_remove (inst->debugfs); + inst->debugfs =NULL ; + +return 0 ; +} + +int vpu_core_create_dbgfs_file (struct vpu_core *core) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!core || !core->vpu) +return -EINVAL ; + + vpu = core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (!core->debugfs) { +scnprintf (name,sizeof (name),"core.%d" , core->id); + core->debugfs =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0644 ), + vpu->debugfs, + core, + &vpu_dbg_core_fops); +if (!core->debugfs) { +dev_err (core->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + } +if (!core->debugfs_fwlog) { +scnprintf (name,sizeof (name),"fwlog.%d" , core->id); + core->debugfs_fwlog =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0444 ), + vpu->debugfs, + core, + &vpu_dbg_fwlog_fops); +if (!core->debugfs_fwlog) { +dev_err (core->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + } + +return 0 ; +} + +int vpu_core_remove_dbgfs_file (struct vpu_core *core) +{ +if (!core) +return 0 ; +debugfs_remove (core->debugfs); + core->debugfs =NULL ; +debugfs_remove (core->debugfs_fwlog); + core->debugfs_fwlog =NULL ; + +return 0 ; +} + +void vpu_inst_record_flow (struct vpu_inst *inst,u32 flow) +{ +if (!inst) +return ; + + inst->flows[inst->flow_idx] = flow; + inst->flow_idx = (inst->flow_idx +1 ) % (ARRAY_SIZE (inst->flows)); +}drivers/media/platform/amphion/vpu_rpc.c new file +279
-0@@ -0,0 +1,279 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/firmware/imx/ipc.h> +#include <linux/firmware/imx/svc/misc.h> +#include "vpu.h" +#include "vpu_rpc.h" +#include "vpu_imx8q.h" +#include "vpu_windsor.h" +#include "vpu_malone.h" + +u32 vpu_iface_check_memory_region (struct vpu_core *core,dma_addr_t addr,u32 size) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->check_memory_region) +return VPU_CORE_MEMORY_INVALID ; + +return ops->check_memory_region (core->fw.phys, addr, size); +} + +static u32 vpu_rpc_check_buffer_space (struct vpu_rpc_buffer_desc *desc,bool write) +{ +u32 ptr1; +u32 ptr2; +u32 size; + +WARN_ON (!desc); + + size = desc->end - desc->start; +if (write) { + ptr1 = desc->wptr; + ptr2 = desc->rptr; + }else { + ptr1 = desc->rptr; + ptr2 = desc->wptr; + } + +if (ptr1 == ptr2) { +if (!write) +return 0 ; +else +return size; + } + +return (ptr2 + size - ptr1) % size; +} + +static int vpu_rpc_send_cmd_buf (struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 *data; +u32 wptr; +u32 i; + +WARN_ON (!shared || !shared->cmd_mem_vir || !cmd); + + desc = shared->cmd_desc; + space =vpu_rpc_check_buffer_space (desc, true); +if (space < (((cmd->hdr.num +1 ) <<2 ) +16 )) { +pr_err ("Cmd Buffer is no space for [%d] %d\n" , + cmd->hdr.index, cmd->hdr.id); +return -EINVAL ; + } + wptr = desc->wptr; + data = (u32 *)(shared->cmd_mem_vir + desc->wptr - desc->start); + *data =0 ; + *data |= ((cmd->hdr.index &0xff ) <<24 ); + *data |= ((cmd->hdr.num &0xff ) <<16 ); + *data |= (cmd->hdr.id &0x3fff ); + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + +for (i =0 ; i < cmd->hdr.num; i++) { + *data = cmd->data[i]; + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + } + +/*update wptr after data is written*/ +mb (); + desc->wptr = wptr; + +return 0 ; +} + +static bool vpu_rpc_check_msg (struct vpu_shared_addr *shared) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 msgword; +u32 msgnum; + +WARN_ON (!shared || !shared->msg_desc); + + desc = shared->msg_desc; + space =vpu_rpc_check_buffer_space (desc,0 ); + space = (space >>2 ); + +if (space) { + msgword = *(u32 *)(shared->msg_mem_vir + desc->rptr - desc->start); + msgnum = (msgword &0xff0000 ) >>16 ; +if (msgnum <= space) +return true; + } + +return false; +} + +static int vpu_rpc_receive_msg_buf (struct vpu_shared_addr *shared,struct vpu_rpc_event *msg) +{ +struct vpu_rpc_buffer_desc *desc; +u32 *data; +u32 msgword; +u32 rptr; +u32 i; + +WARN_ON (!shared || !shared->msg_desc || !msg); + +if (!vpu_rpc_check_msg (shared)) +return -EINVAL ; + + desc = shared->msg_desc; + data = (u32 *)(shared->msg_mem_vir + desc->rptr - desc->start); + rptr = desc->rptr; + msgword = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + + msg->hdr.index = (msgword >>24 ) &0xff ; + msg->hdr.num = (msgword >>16 ) &0xff ; + msg->hdr.id = msgword &0x3fff ; + +if (msg->hdr.num >ARRAY_SIZE (msg->data)) { +pr_err ("msg(%d) data length(%d) is out of range\n" , + msg->hdr.id, msg->hdr.num); +return -EINVAL ; + } + +for (i =0 ; i < msg->hdr.num; i++) { + msg->data[i] = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + } + +/*update rptr after data is read*/ +mb (); + desc->rptr = rptr; + +return 0 ; +} + +struct vpu_iface_ops imx8q_rpc_ops[] = { + [VPU_CORE_TYPE_ENC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_windsor_get_data_size, + .check_memory_region = vpu_imx8q_check_memory_region, + .init_rpc = vpu_windsor_init_rpc, + .set_log_buf = vpu_windsor_set_log_buf, + .set_system_cfg = vpu_windsor_set_system_cfg, + .get_version = vpu_windsor_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .pack_cmd = vpu_windsor_pack_cmd, + .convert_msg_id = vpu_windsor_convert_msg_id, + .unpack_msg_data = vpu_windsor_unpack_msg_data, + .config_memory_resource = vpu_windsor_config_memory_resource, + .get_stream_buffer_size = vpu_windsor_get_stream_buffer_size, + .config_stream_buffer = vpu_windsor_config_stream_buffer, + .get_stream_buffer_desc = vpu_windsor_get_stream_buffer_desc, + .update_stream_buffer = vpu_windsor_update_stream_buffer, + .set_encode_params = vpu_windsor_set_encode_params, + .input_frame = vpu_windsor_input_frame, + .get_max_instance_count = vpu_windsor_get_max_instance_count, + }, + [VPU_CORE_TYPE_DEC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_malone_get_data_size, + .check_memory_region = vpu_imx8q_check_memory_region, + .init_rpc = vpu_malone_init_rpc, + .set_log_buf = vpu_malone_set_log_buf, + .set_system_cfg = vpu_malone_set_system_cfg, + .get_version = vpu_malone_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .get_stream_buffer_size = vpu_malone_get_stream_buffer_size, + .config_stream_buffer = vpu_malone_config_stream_buffer, + .set_decode_params = vpu_malone_set_decode_params, + .pack_cmd = vpu_malone_pack_cmd, + .convert_msg_id = vpu_malone_convert_msg_id, + .unpack_msg_data = vpu_malone_unpack_msg_data, + .get_stream_buffer_desc = vpu_malone_get_stream_buffer_desc, + .update_stream_buffer = vpu_malone_update_stream_buffer, + .add_scode = vpu_malone_add_scode, + .input_frame = vpu_malone_input_frame, + .pre_send_cmd = vpu_malone_pre_cmd, + .post_send_cmd = vpu_malone_post_cmd, + .init_instance = vpu_malone_init_instance, + .get_max_instance_count = vpu_malone_get_max_instance_count, + }, +}; + + +static struct vpu_iface_ops *vpu_get_iface (struct vpu_dev *vpu,enum vpu_core_type type) +{ +struct vpu_iface_ops *rpc_ops =NULL ; +u32 size =0 ; + +WARN_ON (!vpu || !vpu->res); + +switch (vpu->res->plat_type) { +case IMX8QXP : +case IMX8QM : + rpc_ops = imx8q_rpc_ops; + size =ARRAY_SIZE (imx8q_rpc_ops); +break ; +default : +return NULL ; + } + +if (type >= size) +return NULL ; + +return &rpc_ops[type]; +} + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core) +{ +WARN_ON (!core || !core->vpu); + +return vpu_get_iface (core->vpu, core->type); +} + +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst) +{ +WARN_ON (!inst || !inst->vpu); + +if (inst->core) +return vpu_core_get_iface (inst->core); + +return vpu_get_iface (inst->vpu, inst->type); +}drivers/media/platform/amphion/vpu_rpc.h new file +454
-0@@ -0,0 +1,464 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_RPC_H +#define _AMPHION_VPU_RPC_H + +#include <media/videobuf2-core.h> +#include "vpu_codec.h" + +struct vpu_rpc_buffer_desc { +u32 wptr; +u32 rptr; +u32 start; +u32 end; +}; + +struct vpu_shared_addr { +void *iface; +struct vpu_rpc_buffer_desc *cmd_desc; +void *cmd_mem_vir; +struct vpu_rpc_buffer_desc *msg_desc; +void *msg_mem_vir; + +unsigned long boot_addr; +struct vpu_core *core; +void *priv; +}; + +struct vpu_rpc_event_header { +u32 index; +u32 id; +u32 num; +}; + +struct vpu_rpc_event { +struct vpu_rpc_event_header hdr; +u32 data[128 ]; +}; + +struct vpu_iface_ops { +bool (*check_codec)(enum vpu_core_type type); +bool (*check_fmt)(enum vpu_core_type type,u32 pixelfmt); +u32 (*get_data_size)(void ); +u32 (*check_memory_region)(dma_addr_t base,dma_addr_t addr,u32 size); +int (*boot_core)(struct vpu_core *core); +int (*shutdown_core)(struct vpu_core *core); +int (*restore_core)(struct vpu_core *core); +int (*get_power_state)(struct vpu_core *core); +int (*on_firmware_loaded)(struct vpu_core *core); +void (*init_rpc)(struct vpu_shared_addr *shared, +struct vpu_buffer *rpc,dma_addr_t boot_addr); +void (*set_log_buf)(struct vpu_shared_addr *shared, +struct vpu_buffer *log); +void (*set_system_cfg)(struct vpu_shared_addr *shared, +u32 regs_base,void __iomem *regs,u32 index); +void (*set_stream_cfg)(struct vpu_shared_addr *shared,u32 index); +u32 (*get_version)(struct vpu_shared_addr *shared); +u32 (*get_max_instance_count)(struct vpu_shared_addr *shared); +int (*get_stream_buffer_size)(struct vpu_shared_addr *shared); +int (*send_cmd_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd); +int (*receive_msg_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *msg); +int (*pack_cmd)(struct vpu_rpc_event *pkt,u32 index,u32 id,void *data); +int (*convert_msg_id)(u32 msg_id); +int (*unpack_msg_data)(struct vpu_rpc_event *pkt,void *data); +int (*input_frame)(struct vpu_shared_addr *shared, +struct vpu_inst *inst,struct vb2_buffer *vb); +int (*config_memory_resource)(struct vpu_shared_addr *shared, +u32 instance, +u32 type, +u32 index, +struct vpu_buffer *buf); +int (*config_stream_buffer)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *buf); +int (*update_stream_buffer)(struct vpu_shared_addr *shared, +u32 instance,u32 ptr,bool write); +int (*get_stream_buffer_desc)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_rpc_buffer_desc *desc); +int (*set_encode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_encode_params *params,u32 update); +int (*set_decode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_decode_params *params,u32 update); +int (*add_scode)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *stream_buffer, +u32 pixelformat, +u32 scode_type); +int (*pre_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*post_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*init_instance)(struct vpu_shared_addr *shared,u32 instance); +}; + +enum { +VPU_CORE_MEMORY_INVALID =0 , +VPU_CORE_MEMORY_CACHED , +VPU_CORE_MEMORY_UNCACHED +}; + +struct vpu_rpc_region_t { +dma_addr_t start; +dma_addr_t end; +dma_addr_t type; +}; + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core); +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst); +u32 vpu_iface_check_memory_region (struct vpu_core *core,dma_addr_t addr,u32 size); + +static inline bool vpu_iface_check_codec (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->check_codec) +return ops->check_codec (core->type); + +return true; +} + +static inline bool vpu_iface_check_format (struct vpu_inst *inst,u32 pixelfmt) +{ +struct vpu_iface_ops *ops =vpu_inst_get_iface (inst); + +if (ops && ops->check_fmt) +return ops->check_fmt (inst->type, pixelfmt); + +return true; +} + +static inline int vpu_iface_boot_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->boot_core) +return ops->boot_core (core); +return 0 ; +} + +static inline int vpu_iface_get_power_state (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->get_power_state) +return ops->get_power_state (core); +return 1 ; +} + +static inline int vpu_iface_shutdown_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->shutdown_core) +return ops->shutdown_core (core); +return 0 ; +} + +static inline int vpu_iface_restore_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->restore_core) +return ops->restore_core (core); +return 0 ; +} + +static inline int vpu_iface_on_firmware_loaded (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->on_firmware_loaded) +return ops->on_firmware_loaded (core); + +return 0 ; +} + +static inline u32 vpu_iface_get_data_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_data_size) +return 0 ; + +return ops->get_data_size (); +} + +static inline int vpu_iface_init (struct vpu_core *core, +struct vpu_shared_addr *shared, +struct vpu_buffer *rpc, +dma_addr_t boot_addr) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->init_rpc) +return -EINVAL ; + + ops->init_rpc (shared, rpc, boot_addr); + core->iface = shared; + shared->core = core; +if (rpc->bytesused > rpc->length) +return -ENOSPC ; +return 0 ; +} + +static inline int vpu_iface_set_log_buf (struct vpu_core *core, +struct vpu_buffer *log) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; + +if (ops->set_log_buf) + ops->set_log_buf (core->iface, log); + +return 0 ; +} + +static inline int vpu_iface_config_system (struct vpu_core *core, +u32 regs_base,void __iomem *regs) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; +if (ops->set_system_cfg) + ops->set_system_cfg (core->iface, regs_base, regs, core->id); + +return 0 ; +} + +static inline int vpu_iface_get_stream_buffer_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_stream_buffer_size) +return 0 ; + +return ops->get_stream_buffer_size (core->iface); +} + +static inline int vpu_iface_config_stream (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops) +return -EINVAL ; +if (ops->set_stream_cfg) + ops->set_stream_cfg (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_send_cmd (struct vpu_core *core,struct vpu_rpc_event *cmd) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->send_cmd_buf) +return -EINVAL ; + +return ops->send_cmd_buf (core->iface, cmd); +} + +static inline int vpu_iface_receive_msg (struct vpu_core *core,struct vpu_rpc_event *msg) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->receive_msg_buf) +return -EINVAL ; + +return ops->receive_msg_buf (core->iface, msg); +} + +static inline int vpu_iface_pack_cmd (struct vpu_core *core, +struct vpu_rpc_event *pkt, +u32 index,u32 id,void *data) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->pack_cmd) +return -EINVAL ; +return ops->pack_cmd (pkt, index, id, data); +} + +static inline int vpu_iface_convert_msg_id (struct vpu_core *core,u32 msg_id) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->convert_msg_id) +return -EINVAL ; + +return ops->convert_msg_id (msg_id); +} + +static inline int vpu_iface_unpack_msg_data (struct vpu_core *core, +struct vpu_rpc_event *pkt,void *data) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->unpack_msg_data) +return -EINVAL ; + +return ops->unpack_msg_data (pkt, data); +} + +static inline int vpu_iface_input_frame (struct vpu_inst *inst, +struct vb2_buffer *vb) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +if (!ops || !ops->input_frame) +return -EINVAL ; + +return ops->input_frame (inst->core->iface, inst, vb); +} + +static inline int vpu_iface_config_memory_resource (struct vpu_inst *inst, +u32 type,u32 index,struct vpu_buffer *buf) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->config_memory_resource) +return -EINVAL ; + +return ops->config_memory_resource (inst->core->iface, + inst->id, + type, index, buf); +} + +static inline int vpu_iface_config_stream_buffer (struct vpu_inst *inst, +struct vpu_buffer *buf) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->config_stream_buffer) +return -EINVAL ; + +return ops->config_stream_buffer (inst->core->iface, inst->id, buf); +} + +static inline int vpu_iface_update_stream_buffer (struct vpu_inst *inst, +u32 ptr,bool write) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->update_stream_buffer) +return -EINVAL ; + +return ops->update_stream_buffer (inst->core->iface, inst->id, ptr, write); +} + +static inline int vpu_iface_get_stream_buffer_desc (struct vpu_inst *inst, +struct vpu_rpc_buffer_desc *desc) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->get_stream_buffer_desc) +return -EINVAL ; + +if (!desc) +return 0 ; + +return ops->get_stream_buffer_desc (inst->core->iface, inst->id, desc); +} + +static inline u32 vpu_iface_get_version (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_version) +return 0 ; + +return ops->get_version (core->iface); +} + +static inline u32 vpu_iface_get_max_instance_count (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_max_instance_count) +return 0 ; + +return ops->get_max_instance_count (core->iface); +} + +static inline int vpu_iface_set_encode_params (struct vpu_inst *inst, +struct vpu_encode_params *params,u32 update) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->set_encode_params) +return -EINVAL ; + +return ops->set_encode_params (inst->core->iface, inst->id, params, update); +} + +static inline int vpu_iface_set_decode_params (struct vpu_inst *inst, +struct vpu_decode_params *params,u32 update) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->set_decode_params) +return -EINVAL ; + +return ops->set_decode_params (inst->core->iface, inst->id, params, update); +} + +static inline int vpu_iface_add_scode (struct vpu_inst *inst,u32 scode_type) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (!ops || !ops->add_scode) +return -EINVAL ; + +return ops->add_scode (inst->core->iface, inst->id, + &inst->stream_buffer, + inst->out_format.pixfmt, + scode_type); +} + +static inline int vpu_iface_pre_send_cmd (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->pre_send_cmd) +return ops->pre_send_cmd (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_post_send_cmd (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->post_send_cmd) +return ops->post_send_cmd (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_init_instance (struct vpu_inst *inst) +{
drivers/media/platform/amphion/vpu_rpc.h line 464
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core); + +WARN_ON (inst->id <0 ); +if (ops && ops->init_instance) +return ops->init_instance (inst->core->iface, inst->id); + + return 0; +} + +#endif
Regards,
Hans
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Ming Qian
maintainerRE: [EXT] Re: [PATCH v13 04/13] media: amphion: add vpu core driver
Quoted 166 lines
-----Original Message-----
From: Hans Verkuil [mailto:[email protected]]
Sent: Thursday, December 2, 2021 5:54 PM
To: Ming Qian <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; dl-linux-imx <[email protected]>; Aisheng Dong <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]
Subject: [EXT] Re: [PATCH v13 04/13] media: amphion: add vpu core driverCaution: EXT Email
Quoted 4 lines · Ming Qian
The vpu supports encoder and decoder.
it needs mu core to handle it.
core will run either encoder or decoder firmware.Quoted 8 lines
This driver is for support the vpu core.
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>--- drivers/media/platform/amphion/vpu_codec.h | 67 ++ drivers/media/platform/amphion/vpu_core.c | 906+++++++++++++++++++++
Quoted 12 lines
drivers/media/platform/amphion/vpu_core.h | 15 + drivers/media/platform/amphion/vpu_dbg.c | 495 +++++++++++ drivers/media/platform/amphion/vpu_rpc.c | 279 +++++++ drivers/media/platform/amphion/vpu_rpc.h | 464 +++++++++++ 6 files changed, 2226 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_codec.h create mode 100644 drivers/media/platform/amphion/vpu_core.c create mode 100644 drivers/media/platform/amphion/vpu_core.h create mode 100644 drivers/media/platform/amphion/vpu_dbg.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.c create mode 100644 drivers/media/platform/amphion/vpu_rpc.hReview
Quoted 1 line
(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_codec.hReview
Quoted 73 lines
new file mode 100644
index 000000000000..bf8920e9f6d7drivers/media/platform/amphion/vpu_codec.h new file +67
-0@@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CODEC_H +#define _AMPHION_VPU_CODEC_H + +struct vpu_encode_params { +u32 input_format; +u32 codec_format; +u32 profile; +u32 tier; +u32 level; +struct v4l2_fract frame_rate; +u32 src_stride; +u32 src_width; +u32 src_height; +struct v4l2_rect crop; +u32 out_width; +u32 out_height; + +u32 gop_length; +u32 bframes; + +u32 rc_mode; +u32 bitrate; +u32 bitrate_min; +u32 bitrate_max; + +u32 i_frame_qp; +u32 p_frame_qp; +u32 b_frame_qp; +u32 qp_min; +u32 qp_max; +u32 qp_min_i; +u32 qp_max_i; + +struct { +u32 enable; +u32 idc; +u32 width; +u32 height; + } sar; + +struct { +u32 primaries; +u32 transfer; +u32 matrix; +u32 full_range; + } color; +}; + +struct vpu_decode_params { +u32 codec_format; +u32 output_format; +u32 b_dis_reorder; +u32 b_non_frame; +u32 frame_count; +u32 end_flag; +struct { +u32 base; +u32 size; + } udata; +}; + +#endif (quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_core.cQuoted 38 lines
new file mode 100644
index 000000000000..0dbfd1c84f75drivers/media/platform/amphion/vpu_core.c new file +33
-0@@ -0,0 +1,906 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <linux/pm_domain.h> +#include <linux/firmware.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_core.h" +#include "vpu_mbox.h" +#include "vpu_msgs.h" +#include "vpu_rpc.h" +#include "vpu_cmds.h" + +void csr_writel (struct vpu_core *core,u32 reg,u32 val) +{ +writel (val, core->base + reg); +} + +u32 csr_readl (struct vpu_core *core,u32 reg)drivers/media/platform/amphion/vpu_core.c line 43
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+{ +return readl (core->base + reg); +} + +static int vpu_core_load_firmware (struct vpu_core *core) +{ +const struct firmware *pfw =NULL ; +int ret =0 ; + +WARN_ON (!core || !core->res || !core->res->fwname);Either do:
Quoted 6 lines
if (WARN_ON(!core || !core->res || !core->res->fwname)) return -EINVAL;or just drop it. You'll get a oops with backtrace soon enough.
Same elsewhere in this driver.
Got it, I'll check and fix them
Quoted 2,239 lines
drivers/media/platform/amphion/vpu_core.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_core.c new file +7
-0+if (!core->fw.virt) { +dev_err (core->dev,"firmware buffer is not ready\n" ); +return -EINVAL ; + } + + ret =request_firmware (&pfw, core->res->fwname, core->dev); + dev_dbg(core->dev, "request_firmware %s : %d\n",
core->res->fwname, ret);drivers/media/platform/amphion/vpu_core.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_core.c new file +8
-0+if (ret) { +dev_err (core->dev,"request firmware %s failed, ret = %d\n" , + core->res->fwname, ret); +return ret; + } + +if (core->fw.length < pfw->size) { + dev_err(core->dev, "firmware buffer size want %zu,but %d\n",
Quoted 17 lines
drivers/media/platform/amphion/vpu_core.c new file +17
-0+ pfw->size, core->fw.length); + ret = -EINVAL ; + goto exit; + } + +memset_io (core->fw.virt,0 , core->fw.length); +memcpy (core->fw.virt, pfw->data, pfw->size); + core->fw.bytesused = pfw->size; + ret =vpu_iface_on_firmware_loaded (core); +exit: +release_firmware (pfw); + pfw =NULL ; + +return ret; +} + +static int vpu_core_boot_done (struct vpu_core *core)drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+{ +u32 fw_version; + + fw_version =vpu_iface_get_version (core); +dev_info (core->dev,"%s firmware version : %d.%d.%d\n" , +vpu_core_type_desc (core->type), + (fw_version >>16 ) &0xff , + (fw_version >>8 ) &0xff , + fw_version &0xff ); + core->supported_instance_count =
vpu_iface_get_max_instance_count(core);drivers/media/platform/amphion/vpu_core.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c new file +4
-0+if (core->res->act_size) { +u32 count = core->act.length / core->res->act_size; + + core->supported_instance_count =
min(core->supported_instance_count, count);Quoted 97 lines
drivers/media/platform/amphion/vpu_core.c new file +97
-0+ } + core->fw_version = fw_version; + core->state =VPU_CORE_ACTIVE ; + +return 0 ; +} + +static int vpu_core_wait_boot_done (struct vpu_core *core) +{ +int ret; + + ret =wait_for_completion_timeout (&core->cmp,VPU_TIMEOUT ); +if (!ret) { +dev_err (core->dev,"boot timeout\n" ); +return -EINVAL ; + } +return vpu_core_boot_done (core); +} + +static int vpu_core_boot (struct vpu_core *core,bool load) +{ +int ret; + +WARN_ON (!core); + +if (!core->res->standalone) +return 0 ; + +reinit_completion (&core->cmp); +if (load) { + ret =vpu_core_load_firmware (core); +if (ret) +return ret; + } + +vpu_iface_boot_core (core); +return vpu_core_wait_boot_done (core); +} + +static int vpu_core_shutdown (struct vpu_core *core) +{ +if (!core->res->standalone) +return 0 ; +return vpu_iface_shutdown_core (core); +} + +static int vpu_core_restore (struct vpu_core *core) +{ +int ret; + +if (!core->res->standalone) +return 0 ; + ret =vpu_core_sw_reset (core); +if (ret) +return ret; + +vpu_core_boot_done (core); +return vpu_iface_restore_core (core); +} + +static int __vpu_alloc_dma (struct device *dev,struct vpu_buffer *buf) +{ +gfp_t gfp =GFP_KERNEL |GFP_DMA32 ; + +WARN_ON (!dev || !buf); + +if (!buf->length) +return 0 ; + + buf->virt =dma_alloc_coherent (dev, buf->length, &buf->phys, gfp); +if (!buf->virt) +return -ENOMEM ; + + buf->dev = dev; + +return 0 ; +} + +void vpu_free_dma (struct vpu_buffer *buf) +{ +WARN_ON (!buf); + +if (!buf->virt || !buf->dev) +return ; + +dma_free_coherent (buf->dev, buf->length, buf->virt, buf->phys); + buf->virt =NULL ; + buf->phys =0 ; + buf->length =0 ; + buf->bytesused =0 ; + buf->dev =NULL ; +} + +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf) +{ +WARN_ON (!core || !buf); +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+return __vpu_alloc_dma (core->dev, buf); +} + +static void vpu_core_check_hang (struct vpu_core *core) +{ +if (core->hang_mask) + core->state =VPU_CORE_HANG ; +} + +static struct vpu_core *vpu_core_find_proper_by_type(struct vpu_dev *vpu,u32 type)
Quoted 48 lines
drivers/media/platform/amphion/vpu_core.c new file +48
-0+{ +struct vpu_core *core =NULL ; +int request_count =INT_MAX ; +struct vpu_core *c; + +WARN_ON (!vpu); + +list_for_each_entry (c, &vpu->cores, list) { +dev_dbg (c->dev,"instance_mask = 0x%lx, state = %d\n" , + c->instance_mask, + c->state); +if (c->type != type) +continue ; +if (c->state ==VPU_CORE_DEINIT ) { + core = c; +break ; + } +vpu_core_check_hang (c); +if (c->state !=VPU_CORE_ACTIVE ) +continue ; +if (c->request_count < request_count) { + request_count = c->request_count; + core = c; + } +if (!request_count) +break ; + } + +return core; +} + +static bool vpu_core_is_exist (struct vpu_dev *vpu,struct vpu_core *core) +{ +struct vpu_core *c; + +list_for_each_entry (c, &vpu->cores, list) { +if (c == core) +return true; + } + +return false; +} + +static void vpu_core_get_vpu (struct vpu_core *core) +{ + core->vpu->get_vpu (core->vpu); +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->get_enc (core->vpu);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->get_dec (core->vpu); +} + +static int vpu_core_register (struct device *dev,struct vpu_core *core) +{ +struct vpu_dev *vpu =dev_get_drvdata (dev); +int ret =0 ; + + dev_dbg(core->dev, "register core %s\n",
vpu_core_type_desc(core->type));drivers/media/platform/amphion/vpu_core.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c new file +4
-0+if (vpu_core_is_exist (vpu, core)) +return 0 ; + + core->workqueue = alloc_workqueue("vpu", WQ_UNBOUND |
WQ_MEM_RECLAIM, 1);drivers/media/platform/amphion/vpu_core.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_core.c new file +6
-0+if (!core->workqueue) { +dev_err (core->dev,"fail to alloc workqueue\n" ); +return -ENOMEM ; + } +INIT_WORK (&core->msg_work, vpu_msg_run_work); + INIT_DELAYED_WORK(&core->msg_delayed_work,
vpu_msg_delayed_work);drivers/media/platform/amphion/vpu_core.c line 265
Quoted 1 line
drivers/media/platform/amphion/vpu_core.c new file +1
-0+ core->msg_buffer_size =
roundup_pow_of_two(VPU_MSG_BUFFER_SIZE);drivers/media/platform/amphion/vpu_core.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_core.c new file +7
-0+ core->msg_buffer =vzalloc (core->msg_buffer_size); +if (!core->msg_buffer) { +dev_err (core->dev,"failed allocate buffer for fifo\n" ); + ret = -ENOMEM ; + goto error; + } + ret = kfifo_init(&core->msg_fifo, core->msg_buffer,
core->msg_buffer_size);Quoted 91 lines
drivers/media/platform/amphion/vpu_core.c new file +91
-0+if (ret) { +dev_err (core->dev,"failed init kfifo\n" ); + goto error; + } + +list_add_tail (&core->list, &vpu->cores); + +vpu_core_get_vpu (core); + +if (vpu_iface_get_power_state (core)) + ret =vpu_core_restore (core); +if (ret) + goto error; + +return 0 ; +error: +if (core->msg_buffer) { +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + } +if (core->workqueue) { +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } +return ret; +} + +static void vpu_core_put_vpu (struct vpu_core *core) +{ +if (core->type ==VPU_CORE_TYPE_ENC ) + core->vpu->put_enc (core->vpu); +if (core->type ==VPU_CORE_TYPE_DEC ) + core->vpu->put_dec (core->vpu); + core->vpu->put_vpu (core->vpu); +} + +static int vpu_core_unregister (struct device *dev,struct vpu_core *core) +{ +list_del_init (&core->list); + +vpu_core_put_vpu (core); + core->vpu =NULL ; +vfree (core->msg_buffer); + core->msg_buffer =NULL ; + +if (core->workqueue) { +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); +destroy_workqueue (core->workqueue); + core->workqueue =NULL ; + } + +return 0 ; +} + +static int vpu_core_acquire_instance (struct vpu_core *core) +{ +int id; + +WARN_ON (!core); + + id =ffz (core->instance_mask); +if (id >= core->supported_instance_count) +return -EINVAL ; + +set_bit (id, &core->instance_mask); + +return id; +} + +static void vpu_core_release_instance (struct vpu_core *core,int id) +{ +WARN_ON (!core); + +if (id <0 || id >= core->supported_instance_count) +return ; + +clear_bit (id, &core->instance_mask); +} + +struct vpu_inst *vpu_inst_get (struct vpu_inst *inst) +{ +if (!inst) +return NULL ; + +atomic_inc (&inst->ref_count); + +return inst; +} + +void vpu_inst_put (struct vpu_inst *inst)drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+{ +if (!inst) +return ; +if (atomic_dec_and_test (&inst->ref_count)) { +if (inst->release) + inst->release (inst); + } +} + +struct vpu_core *vpu_request_core(struct vpu_dev *vpu, enum
vpu_core_typetype)Quoted 69 lines
drivers/media/platform/amphion/vpu_core.c new file +69
-0+{ +struct vpu_core *core =NULL ; +int ret; + +mutex_lock (&vpu->lock); + + core =vpu_core_find_proper_by_type (vpu, type); +if (!core) + goto exit; + +mutex_lock (&core->lock); +pm_runtime_get_sync (core->dev); + +if (core->state ==VPU_CORE_DEINIT ) { + ret =vpu_core_boot (core, true); +if (ret) { +pm_runtime_put_sync (core->dev); +mutex_unlock (&core->lock); + core =NULL ; + goto exit; + } + } + + core->request_count++; + +mutex_unlock (&core->lock); +exit: +mutex_unlock (&vpu->lock); + +return core; +} + +void vpu_release_core (struct vpu_core *core) +{ +if (!core) +return ; + +mutex_lock (&core->lock); +pm_runtime_put_sync (core->dev); +if (core->request_count) + core->request_count--; +mutex_unlock (&core->lock); +} + +int vpu_inst_register (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core; +int ret =0 ; + +WARN_ON (!inst || !inst->vpu); + + vpu = inst->vpu; + core = inst->core; +if (!core) { + core =vpu_request_core (vpu, inst->type); +if (!core) { +dev_err (vpu->dev,"there is no vpu core for %s\n" , +vpu_core_type_desc (inst->type)); +return -EINVAL ; + } + inst->core = core; + inst->dev =get_device (core->dev); + } + +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) + goto exit; +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+ ret =vpu_core_acquire_instance (core); +if (ret <0 ) + goto exit; + +vpu_trace (inst->dev,"[%d] %p\n" , ret, inst); + inst->id = ret; +list_add_tail (&inst->list, &core->instances); + ret =0 ; +if (core->res->act_size) { + inst->act.phys = core->act.phys + core->res->act_size *
inst->id;Quoted 85 lines
drivers/media/platform/amphion/vpu_core.c new file +85
-0+ inst->act.virt = core->act.virt + core->res->act_size * inst->id; + inst->act.length = core->res->act_size; + } +vpu_inst_create_dbgfs_file (inst); +exit: +mutex_unlock (&core->lock); + +if (ret) +dev_err (core->dev,"register instance fail\n" ); +return ret; +} + +int vpu_inst_unregister (struct vpu_inst *inst) +{ +struct vpu_core *core; + +WARN_ON (!inst); + +if (!inst->core) +return 0 ; + + core = inst->core; +vpu_clear_request (inst); +mutex_lock (&core->lock); +if (inst->id >=0 && inst->id < core->supported_instance_count) { +vpu_inst_remove_dbgfs_file (inst); +list_del_init (&inst->list); +vpu_core_release_instance (core, inst->id); + inst->id =VPU_INST_NULL_ID ; + } +vpu_core_check_hang (core); +if (core->state ==VPU_CORE_HANG && !core->instance_mask) { +dev_info (core->dev,"reset hang core\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); + +return 0 ; +} + +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index) +{ +struct vpu_inst *inst =NULL ; +struct vpu_inst *tmp; + +mutex_lock (&core->lock); +if (!test_bit (index, &core->instance_mask)) + goto exit; +list_for_each_entry (tmp, &core->instances, list) { +if (tmp->id == index) { + inst =vpu_inst_get (tmp); +break ; + } + } +exit: +mutex_unlock (&core->lock); + +return inst; +} + +const struct vpu_core_resources *vpu_get_resource (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +struct vpu_core *core =NULL ; +const struct vpu_core_resources *res =NULL ; + +if (!inst || !inst->vpu) +return NULL ; + +if (inst->core && inst->core->res) +return inst->core->res; + + vpu = inst->vpu; +mutex_lock (&vpu->lock); +list_for_each_entry (core, &vpu->cores, list) { +if (core->type == inst->type) { + res = core->res; +break ; + } + } +mutex_unlock (&vpu->lock); +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+return res; +} + +static int vpu_core_parse_dt (struct vpu_core *core,struct device_node *np) +{ +struct device_node *node; +struct resource res; + +if (of_count_phandle_with_args (np,"memory-region" ,NULL ) <2 ) { + dev_err(core->dev, "need 2 memory-region for boot andrpc\n");
drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+return -ENODEV ; + } + + node =of_parse_phandle (np,"memory-region" ,0 ); +if (!node) { +dev_err (core->dev,"boot-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { + dev_err(core->dev, "boot-region of_address_to_resourceerror\n");
drivers/media/platform/amphion/vpu_core.c
Quoted 12 lines
drivers/media/platform/amphion/vpu_core.c new file +12
-0+return -EINVAL ; + } + core->fw.phys = res.start; + core->fw.length =resource_size (&res); + + node =of_parse_phandle (np,"memory-region" ,1 ); +if (!node) { +dev_err (core->dev,"rpc-region of_parse_phandle error\n" ); +return -ENODEV ; + } +if (of_address_to_resource (node,0 , &res)) { + dev_err(core->dev, "rpc-region of_address_to_resourceerror\n");
drivers/media/platform/amphion/vpu_core.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_core.c new file +7
-0+return -EINVAL ; + } + core->rpc.phys = res.start; + core->rpc.length =resource_size (&res); + +if (core->rpc.length < core->res->rpc_size + core->res->fwlog_size) { + dev_err(core->dev, "the rpc-region <%pad, 0x%x> is notenough\n",
drivers/media/platform/amphion/vpu_core.c
Quoted 11 lines
drivers/media/platform/amphion/vpu_core.c new file +11
-0+ &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->fw.virt =ioremap_wc (core->fw.phys, core->fw.length); + core->rpc.virt =ioremap_wc (core->rpc.phys, core->rpc.length); +memset_io (core->rpc.virt,0 , core->rpc.length); + + if (vpu_iface_check_memory_region(core, + core->rpc.phys, + core->rpc.length) !=
VPU_CORE_MEMORY_UNCACHED) {drivers/media/platform/amphion/vpu_core.c line 588
Quoted 1 line
drivers/media/platform/amphion/vpu_core.c new file +1
-0+ dev_err(core->dev, "rpc region<%pad, 0x%x> isn'tuncached\n",
drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+ &core->rpc.phys, core->rpc.length); +return -EINVAL ; + } + + core->log.phys = core->rpc.phys + core->res->rpc_size; + core->log.virt = core->rpc.virt + core->res->rpc_size; + core->log.length = core->res->fwlog_size; + core->act.phys = core->log.phys + core->log.length; + core->act.virt = core->log.virt + core->log.length; + core->act.length = core->rpc.length - core->res->rpc_size -
core->log.length;Quoted 33 lines
drivers/media/platform/amphion/vpu_core.c new file +33
-0+ core->rpc.length = core->res->rpc_size; + +return 0 ; +} + +static int vpu_core_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core; +struct vpu_dev *vpu =dev_get_drvdata (dev->parent); +struct vpu_shared_addr *iface; +u32 iface_data_size; +int ret; + +dev_dbg (dev,"probe\n" ); +if (!vpu) +return -EINVAL ; + core =devm_kzalloc (dev,sizeof (*core),GFP_KERNEL ); +if (!core) +return -ENOMEM ; + + core->pdev = pdev; + core->dev = dev; +platform_set_drvdata (pdev, core); + core->vpu = vpu; +INIT_LIST_HEAD (&core->instances); +mutex_init (&core->lock); +mutex_init (&core->cmd_lock); +init_completion (&core->cmp); +init_waitqueue_head (&core->ack_wq); + core->state =VPU_CORE_DEINIT ; + + core->res =of_device_get_match_data (dev);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (!core->res) +return -ENODEV ; + + core->type = core->res->type; + core->id =of_alias_get_id (dev->of_node,"vpu_core" ); +if (core->id <0 ) { +dev_err (dev,"can't get vpu core id\n" ); +return core->id; + } + dev_info(core->dev, "[%d] = %s\n", core->id,
vpu_core_type_desc(core->type));Quoted 14 lines
drivers/media/platform/amphion/vpu_core.c new file +14
-0+ ret =vpu_core_parse_dt (core, dev->of_node); +if (ret) +return ret; + + core->base =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (core->base)) +return PTR_ERR (core->base); + +if (!vpu_iface_check_codec (core)) { +dev_err (core->dev,"is not supported\n" ); +return -EINVAL ; + } + + ret =vpu_mbox_init (core);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (ret) +return ret; + + iface =devm_kzalloc (dev,sizeof (*iface),GFP_KERNEL ); +if (!iface) +return -ENOMEM ; + + iface_data_size =vpu_iface_get_data_size (core); +if (iface_data_size) { + iface->priv = devm_kzalloc(dev, iface_data_size,
GFP_KERNEL);Quoted 5 lines
drivers/media/platform/amphion/vpu_core.c new file +5
-0+if (!iface->priv) +return -ENOMEM ; + } + + ret =vpu_iface_init (core, iface, &core->rpc, core->fw.phys);drivers/media/platform/amphion/vpu_core.c line 680
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+if (ret) { +dev_err (core->dev,"init iface fail, ret = %d\n" , ret); +return ret; + } + +vpu_iface_config_system (core, vpu->res->mreg_base, vpu->base); +vpu_iface_set_log_buf (core, &core->log); + +pm_runtime_enable (dev); + ret =pm_runtime_get_sync (dev);Use
pm_runtime_resume_and_get()instead and drop thepm_runtime_put_noidle()
in the 'if' below. The use ofpm_runtime_resume_and_getis preferred over the rather confusingpm_runtime_get_sync().If it is used elsewhere in this series as well (I haven't checked this), then make the same changes.
Quoted 22 lines
drivers/media/platform/amphion/vpu_core.c new file +22
-0+if (ret) { +pm_runtime_put_noidle (dev); +pm_runtime_set_suspended (dev); + goto err_runtime_disable; + } + + ret =vpu_core_register (dev->parent, core); +if (ret) + goto err_core_register; + core->parent = dev->parent; + +pm_runtime_put_sync (dev); +vpu_core_create_dbgfs_file (core); + +return 0 ; + +err_core_register: +pm_runtime_put_sync (dev); +err_runtime_disable: +pm_runtime_disable (dev); + +return ret;drivers/media/platform/amphion/vpu_core.c line 712
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+} + +static int vpu_core_remove (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct vpu_core *core =platform_get_drvdata (pdev); +int ret; + +vpu_core_remove_dbgfs_file (core); + ret =pm_runtime_get_sync (dev);Ah, same here.
Quoted 40 lines
drivers/media/platform/amphion/vpu_core.c new file +40
-0+WARN_ON (ret <0 ); + +vpu_core_shutdown (core); +pm_runtime_put_sync (dev); +pm_runtime_disable (dev); + +vpu_core_unregister (core->parent, core); +iounmap (core->fw.virt); +iounmap (core->rpc.virt); +mutex_destroy (&core->lock); +mutex_destroy (&core->cmd_lock); + +return 0 ; +} + +static int __maybe_unused vpu_core_runtime_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +return vpu_mbox_request (core); +} + +static int __maybe_unused vpu_core_runtime_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); + +vpu_mbox_free (core); +return 0 ; +} + +static void vpu_core_cancel_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; + +cancel_work_sync (&core->msg_work); +cancel_delayed_work_sync (&core->msg_delayed_work); + +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +cancel_work_sync (&inst->msg_work);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+mutex_unlock (&core->lock); +} + +static void vpu_core_resume_work (struct vpu_core *core) +{ +struct vpu_inst *inst =NULL ; +unsigned long delay =msecs_to_jiffies (10 ); + +queue_work (core->workqueue, &core->msg_work); + queue_delayed_work(core->workqueue, &core->msg_delayed_work,delay);
Quoted 16 lines
drivers/media/platform/amphion/vpu_core.c new file +16
-0+ +mutex_lock (&core->lock); +list_for_each_entry (inst, &core->instances, list) +queue_work (inst->workqueue, &inst->msg_work); +mutex_unlock (&core->lock); +} + +static int __maybe_unused vpu_core_resume (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock);drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+pm_runtime_get_sync (dev); +vpu_core_get_vpu (core); +if (core->state !=VPU_CORE_SNAPSHOT ) + goto exit; + +if (!vpu_iface_get_power_state (core)) { +if (!list_empty (&core->instances)) { + ret =vpu_core_boot (core, false); +if (ret) { + dev_err(core->dev, "%s boot fail\n",
__func__);drivers/media/platform/amphion/vpu_core.c
Quoted 11 lines
drivers/media/platform/amphion/vpu_core.c new file +11
-0+ core->state =VPU_CORE_DEINIT ; + goto exit; + } + }else { + core->state =VPU_CORE_DEINIT ; + } + }else { +if (!list_empty (&core->instances)) { + ret =vpu_core_sw_reset (core); +if (ret) { + dev_err(core->dev, "%s sw_reset fail\n",
__func__);Quoted 37 lines
drivers/media/platform/amphion/vpu_core.c new file +37
-0+ core->state =VPU_CORE_HANG ; + goto exit; + } + } + core->state =VPU_CORE_ACTIVE ; + } + +exit: +pm_runtime_put_sync (dev); +mutex_unlock (&core->lock); + +vpu_core_resume_work (core); +return ret; +} + +static int __maybe_unused vpu_core_suspend (struct device *dev) +{ +struct vpu_core *core =dev_get_drvdata (dev); +int ret =0 ; + +if (!core->res->standalone) +return 0 ; + +mutex_lock (&core->lock); +if (core->state ==VPU_CORE_ACTIVE ) { +if (!list_empty (&core->instances)) { + ret =vpu_core_snapshot (core); +if (ret) { +mutex_unlock (&core->lock); +return ret; + } + } + + core->state =VPU_CORE_SNAPSHOT ; + } +mutex_unlock (&core->lock); +drivers/media/platform/amphion/vpu_core.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_core.c new file +10
-0+vpu_core_cancel_work (core); + +mutex_lock (&core->lock); +vpu_core_put_vpu (core); +mutex_unlock (&core->lock); +return ret; +} + +static const struct dev_pm_ops vpu_core_pm_ops = { + SET_RUNTIME_PM_OPS(vpu_core_runtime_suspend,
vpu_core_runtime_resume, NULL)Review
Quoted 61 lines
drivers/media/platform/amphion/vpu_core.c new file +60
-0+SET_SYSTEM_SLEEP_PM_OPS (vpu_core_suspend, vpu_core_resume) +}; + +static struct vpu_core_resources imx8q_enc = { + .type =VPU_CORE_TYPE_ENC , + .fwname ="vpu/vpu_fw_imx8_enc.bin" , + .stride =16 , + .max_width =1920 , + .max_height =1920 , + .min_width =64 , + .min_height =48 , + .step_width =2 , + .step_height =2 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .act_size =0xc0000 , + .standalone = true, +}; + +static struct vpu_core_resources imx8q_dec = { + .type =VPU_CORE_TYPE_DEC , + .fwname ="vpu/vpu_fw_imx8_dec.bin" , + .stride =256 , + .max_width =8188 , + .max_height =8188 , + .min_width =16 , + .min_height =16 , + .step_width =1 , + .step_height =1 , + .rpc_size =0x80000 , + .fwlog_size =0x80000 , + .standalone = true, +}; + +static const struct of_device_id vpu_core_dt_match[] = { + { .compatible ="nxp,imx8q-vpu-encoder" , .data = &imx8q_enc }, + { .compatible ="nxp,imx8q-vpu-decoder" , .data = &imx8q_dec }, + {} +}; +MODULE_DEVICE_TABLE (of, vpu_core_dt_match); + +static struct platform_driver amphion_vpu_core_driver = { + .probe = vpu_core_probe, + .remove = vpu_core_remove, + .driver = { + .name ="amphion-vpu-core" , + .of_match_table = vpu_core_dt_match, + .pm = &vpu_core_pm_ops, + }, +}; + +int __init vpu_core_driver_init (void ) +{ +return platform_driver_register (&hion_vpu_core_driver); +} + +void __exit vpu_core_driver_exit(void) +{ +platform_driver_unregister (&hion_vpu_core_driver); +}(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_core.hReview
Quoted 21 lines
new file mode 100644
index 000000000000..00a662997da4drivers/media/platform/amphion/vpu_core.h new file +15
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CORE_H +#define _AMPHION_VPU_CORE_H + +void csr_writel (struct vpu_core *core,u32 reg,u32 val); +u32 csr_readl (struct vpu_core *core,u32 reg); +int vpu_alloc_dma (struct vpu_core *core,struct vpu_buffer *buf); +void vpu_free_dma (struct vpu_buffer *buf); +struct vpu_inst *vpu_core_find_instance (struct vpu_core *core,u32 index); + +#endif (quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_dbg.cQuoted 54 lines
new file mode 100644
index 000000000000..2e7e11101f99drivers/media/platform/amphion/vpu_dbg.c new file +49
-0@@ -0,0 +1,495 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/types.h> +#include <linux/pm_runtime.h> +#include <media/v4l2-device.h> +#include <linux/debugfs.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_helpers.h" +#include "vpu_cmds.h" +#include "vpu_rpc.h" + +struct print_buf_desc { +u32 start_h_phy; +u32 start_h_vir; +u32 start_m; +u32 bytes; +u32 read; +u32 write; +char buffer[0 ]; +}; + +static char *vb2_stat_name[] = { + [VB2_BUF_STATE_DEQUEUED ] ="dequeued" , + [VB2_BUF_STATE_IN_REQUEST ] ="in_request" , + [VB2_BUF_STATE_PREPARING ] ="preparing" , + [VB2_BUF_STATE_QUEUED ] ="queued" , + [VB2_BUF_STATE_ACTIVE ] ="active" , + [VB2_BUF_STATE_DONE ] ="done" , + [VB2_BUF_STATE_ERROR ] ="error" , +}; + +static char *vpu_stat_name[] = { + [VPU_BUF_STATE_IDLE ] ="idle" , + [VPU_BUF_STATE_INUSE ] ="inuse" , + [VPU_BUF_STATE_DECODED ] ="decoded" , + [VPU_BUF_STATE_READY ] ="ready" , + [VPU_BUF_STATE_SKIP ] ="skip" , + [VPU_BUF_STATE_ERROR ] ="error" , +};drivers/media/platform/amphion/vpu_dbg.c line 59
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+ +static int vpu_dbg_instance (struct seq_file *s,void *data) +{ +struct vpu_inst *inst = s->private; +char str[128 ]; +int num; +struct vb2_queue *vq; +int i; + + num = scnprintf(str, sizeof(str), "[%s]\n",
vpu_core_type_desc(inst->type));drivers/media/platform/amphion/vpu_dbg.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_dbg.c new file +4
-0+if (seq_write (s, str, num)) +return 0 ; + + num = scnprintf(str, sizeof(str), "tgig = %d,pid = %d\n", inst->tgid,
inst->pid);Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"state = %d\n" , inst->state); +if (seq_write (s, str, num)) +return 0 ;drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+ num =scnprintf (str,sizeof (str), +"min_buffer_out = %d, min_buffer_cap = %d\n" , + inst->min_buffer_out, inst->min_buffer_cap); +if (seq_write (s, str, num)) +return 0 ; + + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); + num = scnprintf(str, sizeof(str), + "output (%2d, %2d): fmt = %c%c%c%c %dx %d, %d;",
Quoted 14 lines
drivers/media/platform/amphion/vpu_dbg.c new file +14
-0+vb2_is_streaming (vq), + vq->num_buffers, + inst->out_format.pixfmt, + inst->out_format.pixfmt >>8 , + inst->out_format.pixfmt >>16 , + inst->out_format.pixfmt >>24 , + inst->out_format.width, + inst->out_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->out_format.num_planes; i++) { + num = scnprintf(str, sizeof(str), " %d(%d)", + inst->out_format.sizeimage[i],drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+ inst->out_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + num = scnprintf(str, sizeof(str), + "capture(%2d, %2d): fmt = %c%c%c%c %dx %d, %d;",
Quoted 56 lines
drivers/media/platform/amphion/vpu_dbg.c new file +56
-0+vb2_is_streaming (vq), + vq->num_buffers, + inst->cap_format.pixfmt, + inst->cap_format.pixfmt >>8 , + inst->cap_format.pixfmt >>16 , + inst->cap_format.pixfmt >>24 , + inst->cap_format.width, + inst->cap_format.height, + vq->last_buffer_dequeued); +if (seq_write (s, str, num)) +return 0 ; +for (i =0 ; i < inst->cap_format.num_planes; i++) { + num =scnprintf (str,sizeof (str)," %d(%d)" , + inst->cap_format.sizeimage[i], + inst->cap_format.bytesperline[i]); +if (seq_write (s, str, num)) +return 0 ; + } +if (seq_write (s,"\n" ,1 )) +return 0 ; + num =scnprintf (str,sizeof (str),"crop: (%d, %d) %d x %d\n" , + inst->crop.left, + inst->crop.top, + inst->crop.width, + inst->crop.height); +if (seq_write (s, str, num)) +return 0 ; + + vq =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"output [%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]); +if (seq_write (s, str, num)) +return 0 ; + } + + vq =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +for (i =0 ; i < vq->num_buffers; i++) { +struct vb2_buffer *vb = vq->bufs[i]; +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + +if (vb->state ==VB2_BUF_STATE_DEQUEUED ) +continue ; + num =scnprintf (str,sizeof (str), +"capture[%2d] state = %10s, %8s\n" , + i, vb2_stat_name[vb->state], + vpu_stat_name[vpu_buf->state]);drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+if (seq_write (s, str, num)) +return 0 ; + } + + num =scnprintf (str,sizeof (str),"sequence = %d\n" , inst->sequence); +if (seq_write (s, str, num)) +return 0 ; + +if (inst->use_stream_buffer) { + num = scnprintf(str, sizeof(str), "stream_buffer = %d / %d,<%pad, 0x%x>\n",
drivers/media/platform/amphion/vpu_dbg.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_dbg.c new file +8
-0+vpu_helper_get_used_space (inst), + inst->stream_buffer.length, + &inst->stream_buffer.phys, + inst->stream_buffer.length); +if (seq_write (s, str, num)) +return 0 ; + } + num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n",
kfifo_len(&inst->msg_fifo));Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"flow :\n" ); +if (seq_write (s, str, num))drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+return 0 ; + +mutex_lock (&inst->core->cmd_lock); +for (i =0 ; i <ARRAY_SIZE (inst->flows); i++) { +u32 idx = (inst->flow_idx + i) % (ARRAY_SIZE (inst->flows)); + +if (!inst->flows[idx]) +continue ; + num = scnprintf(str, sizeof(str), "\t[%s]0x%x\n", + inst->flows[idx] >= VPU_MSG_ID_NOOP ?"M" : "C",
Quoted 18 lines
drivers/media/platform/amphion/vpu_dbg.c new file +18
-0+ inst->flows[idx]); +if (seq_write (s, str, num)) { +mutex_unlock (&inst->core->cmd_lock); +return 0 ; + } + } +mutex_unlock (&inst->core->cmd_lock); + + i =0 ; +while (true) { + num =call_vop (inst, get_debug_info, str,sizeof (str), i++); +if (num <=0 ) +break ; +if (seq_write (s, str, num)) +return 0 ; + } + +return 0 ;drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+} + +static int vpu_dbg_core (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct vpu_shared_addr *iface = core->iface; +char str[128 ]; +int num; + + num = scnprintf(str, sizeof(str), "[%s]\n",
vpu_core_type_desc(core->type));drivers/media/platform/amphion/vpu_dbg.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_dbg.c new file +8
-0+if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"boot_region = <%pad, 0x%x>\n" , + &core->fw.phys, core->fw.length); +if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), "rpc_region = <%pad, 0x%x> used =0x%x\n",
drivers/media/platform/amphion/vpu_dbg.c line 228
Quoted 1 line
drivers/media/platform/amphion/vpu_dbg.c new file +1
-0+ &core->rpc.phys, core->rpc.length,
core->rpc.bytesused);Quoted 16 lines
drivers/media/platform/amphion/vpu_dbg.c new file +16
-0+if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"fwlog_region = <%pad, 0x%x>\n" , + &core->log.phys, core->log.length); +if (seq_write (s, str, num)) +return 0 ; + + num =scnprintf (str,sizeof (str),"state = %d\n" , core->state); +if (seq_write (s, str, num)) +return 0 ; +if (core->state ==VPU_CORE_DEINIT ) +return 0 ; + num =scnprintf (str,sizeof (str),"fw version = %d.%d.%d\n" , + (core->fw_version >>16 ) &0xff , + (core->fw_version >>8 ) &0xff , + core->fw_version &0xff );drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+if (seq_write (s, str, num)) +return 0 ; + num =scnprintf (str,sizeof (str),"instances = %d/%d (0x%02lx), %d\n" , +hweight32 (core->instance_mask), + core->supported_instance_count, + core->instance_mask, + core->request_count); +if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n",
kfifo_len(&core->msg_fifo));drivers/media/platform/amphion/vpu_dbg.c
Quoted 4 lines
drivers/media/platform/amphion/vpu_dbg.c new file +4
-0+if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), + "cmd_buf:[0x%x, 0x%x], wptr = 0x%x, rptr =0x%x\n",
drivers/media/platform/amphion/vpu_dbg.c
Quoted 8 lines
drivers/media/platform/amphion/vpu_dbg.c new file +8
-0+ iface->cmd_desc->start, + iface->cmd_desc->end, + iface->cmd_desc->wptr, + iface->cmd_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + num = scnprintf(str, sizeof(str), + "msg_buf:[0x%x, 0x%x], wptr = 0x%x, rptr =0x%x\n",
Quoted 55 lines
drivers/media/platform/amphion/vpu_dbg.c new file +55
-0+ iface->msg_desc->start, + iface->msg_desc->end, + iface->msg_desc->wptr, + iface->msg_desc->rptr); +if (seq_write (s, str, num)) +return 0 ; + +return 0 ; +} + +static int vpu_dbg_fwlog (struct seq_file *s,void *data) +{ +struct vpu_core *core = s->private; +struct print_buf_desc *print_buf; +int length; +u32 rptr; +u32 wptr; +int ret =0 ; + +if (!core->log.virt || core->state ==VPU_CORE_DEINIT ) +return 0 ; + + print_buf = core->log.virt; + rptr = print_buf->read; + wptr = print_buf->write; + +if (rptr == wptr) +return 0 ; +else if (rptr < wptr) + length = wptr - rptr; +else + length = print_buf->bytes + wptr - rptr; + +if (s->count + length >= s->size) { + s->count = s->size; +return 0 ; + } + +if (rptr + length >= print_buf->bytes) { +int num = print_buf->bytes - rptr; + +if (seq_write (s, print_buf->buffer + rptr, num)) + ret =-1 ; + length -= num; + rptr =0 ; + } + +if (length) { +if (seq_write (s, print_buf->buffer + rptr, length)) + ret =-1 ; + rptr += length; + } +if (!ret) + print_buf->read = rptr; +drivers/media/platform/amphion/vpu_dbg.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+return 0 ; +} + +static int vpu_dbg_inst_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_instance, inode->i_private); +} + +static ssize_t vpu_dbg_inst_write (struct file *file, + const char __user *user_buf, size_t size, loff_t*ppos)
drivers/media/platform/amphion/vpu_dbg.c
Quoted 11 lines
drivers/media/platform/amphion/vpu_dbg.c new file +11
-0+{ +struct seq_file *s = file->private_data; +struct vpu_inst *inst = s->private; + +vpu_session_debug (inst); + +return size; +} + +static ssize_t vpu_dbg_core_write (struct file *file, + const char __user *user_buf, size_t size, loff_t*ppos)
Quoted 99 lines
drivers/media/platform/amphion/vpu_dbg.c new file +99
-0+{ +struct seq_file *s = file->private_data; +struct vpu_core *core = s->private; + +pm_runtime_get_sync (core->dev); +mutex_lock (&core->lock); +if (core->state !=VPU_CORE_DEINIT && !core->instance_mask) { +dev_info (core->dev,"reset\n" ); +if (!vpu_core_sw_reset (core)) { + core->state =VPU_CORE_ACTIVE ; + core->hang_mask =0 ; + } + } +mutex_unlock (&core->lock); +pm_runtime_put_sync (core->dev); + +return size; +} + +static int vpu_dbg_core_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_core, inode->i_private); +} + +static int vpu_dbg_fwlog_open (struct inode *inode,struct file *filp) +{ +return single_open (filp, vpu_dbg_fwlog, inode->i_private); +} + +static const struct file_operations vpu_dbg_inst_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_inst_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_inst_write, +}; + +static const struct file_operations vpu_dbg_core_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_core_open, + .release = single_release, + .read = seq_read, + .write = vpu_dbg_core_write, +}; + +static const struct file_operations vpu_dbg_fwlog_fops = { + .owner =THIS_MODULE , + .open = vpu_dbg_fwlog_open, + .release = single_release, + .read = seq_read, +}; + +int vpu_inst_create_dbgfs_file (struct vpu_inst *inst) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!inst || !inst->core || !inst->core->vpu) +return -EINVAL ; + + vpu = inst->core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (inst->debugfs) +return 0 ; + +scnprintf (name,sizeof (name),"instance.%d.%d" , + inst->core->id, inst->id); + inst->debugfs =debugfs_create_file ((const char *)name, +VERIFY_OCTAL_PERMISSIONS (0644 ), + vpu->debugfs, + inst, + &vpu_dbg_inst_fops); +if (!inst->debugfs) { +dev_err (inst->dev,"vpu create debugfs %s fail\n" , name); +return -EINVAL ; + } + +return 0 ; +} + +int vpu_inst_remove_dbgfs_file (struct vpu_inst *inst) +{ +if (!inst) +return 0 ; + +debugfs_remove (inst->debugfs); + inst->debugfs =NULL ; + +return 0 ; +} + +int vpu_core_create_dbgfs_file (struct vpu_core *core) +{ +struct vpu_dev *vpu; +char name[64 ]; + +if (!core || !core->vpu)drivers/media/platform/amphion/vpu_dbg.c line 472
Quoted 10 lines
drivers/media/platform/amphion/vpu_dbg.c new file +10
-0+return -EINVAL ; + + vpu = core->vpu; +if (!vpu->debugfs) +return -EINVAL ; + +if (!core->debugfs) { +scnprintf (name,sizeof (name),"core.%d" , core->id); + core->debugfs = debugfs_create_file((const char *)name, +
VERIFY_OCTAL_PERMISSIONS(0644),drivers/media/platform/amphion/vpu_dbg.c
Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+ vpu->debugfs, + core, + &vpu_dbg_core_fops); +if (!core->debugfs) { + dev_err(core->dev, "vpu create debugfs %s fail\n",name);
drivers/media/platform/amphion/vpu_dbg.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_dbg.c new file +6
-0+return -EINVAL ; + } + } +if (!core->debugfs_fwlog) { +scnprintf (name,sizeof (name),"fwlog.%d" , core->id); + core->debugfs_fwlog = debugfs_create_file((const char*)name,
drivers/media/platform/amphion/vpu_dbg.c line 463
Quoted 1 line
drivers/media/platform/amphion/vpu_dbg.c new file +1
-0+
VERIFY_OCTAL_PERMISSIONS(0444),drivers/media/platform/amphion/vpu_dbg.c
Quoted 5 lines
drivers/media/platform/amphion/vpu_dbg.c new file +5
-0+ vpu->debugfs, + core, + &vpu_dbg_fwlog_fops); +if (!core->debugfs_fwlog) { + dev_err(core->dev, "vpu create debugfs %s fail\n",name);
Review
Quoted 28 lines
drivers/media/platform/amphion/vpu_dbg.c new file +27
-0+return -EINVAL ; + } + } + +return 0 ; +} + +int vpu_core_remove_dbgfs_file (struct vpu_core *core) +{ +if (!core) +return 0 ; +debugfs_remove (core->debugfs); + core->debugfs =NULL ; +debugfs_remove (core->debugfs_fwlog); + core->debugfs_fwlog =NULL ; + +return 0 ; +} + +void vpu_inst_record_flow (struct vpu_inst *inst,u32 flow) +{ +if (!inst) +return ; + + inst->flows[inst->flow_idx] = flow; + inst->flow_idx = (inst->flow_idx +1 ) % (ARRAY_SIZE (inst->flows)); +}(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_rpc.cQuoted 18 lines
new file mode 100644
index 000000000000..7b5e9177e010drivers/media/platform/amphion/vpu_rpc.c new file +13
-0@@ -0,0 +1,279 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> drivers/media/platform/amphion/vpu_rpc.c line 23
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+#include <linux/platform_device.h> +#include <linux/firmware/imx/ipc.h> +#include <linux/firmware/imx/svc/misc.h> +#include "vpu.h" +#include "vpu_rpc.h" +#include "vpu_imx8q.h" +#include "vpu_windsor.h" +#include "vpu_malone.h" + +u32 vpu_iface_check_memory_region(struct vpu_core *core, dma_addr_taddr, u32 size)
drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->check_memory_region) +return VPU_CORE_MEMORY_INVALID ; + +return ops->check_memory_region (core->fw.phys, addr, size); +} + +static u32 vpu_rpc_check_buffer_space(struct vpu_rpc_buffer_desc *desc,bool write)
Quoted 79 lines
drivers/media/platform/amphion/vpu_rpc.c new file +79
-0+{ +u32 ptr1; +u32 ptr2; +u32 size; + +WARN_ON (!desc); + + size = desc->end - desc->start; +if (write) { + ptr1 = desc->wptr; + ptr2 = desc->rptr; + }else { + ptr1 = desc->rptr; + ptr2 = desc->wptr; + } + +if (ptr1 == ptr2) { +if (!write) +return 0 ; +else +return size; + } + +return (ptr2 + size - ptr1) % size; +} + +static int vpu_rpc_send_cmd_buf (struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 *data; +u32 wptr; +u32 i; + +WARN_ON (!shared || !shared->cmd_mem_vir || !cmd); + + desc = shared->cmd_desc; + space =vpu_rpc_check_buffer_space (desc, true); +if (space < (((cmd->hdr.num +1 ) <<2 ) +16 )) { +pr_err ("Cmd Buffer is no space for [%d] %d\n" , + cmd->hdr.index, cmd->hdr.id); +return -EINVAL ; + } + wptr = desc->wptr; + data = (u32 *)(shared->cmd_mem_vir + desc->wptr - desc->start); + *data =0 ; + *data |= ((cmd->hdr.index &0xff ) <<24 ); + *data |= ((cmd->hdr.num &0xff ) <<16 ); + *data |= (cmd->hdr.id &0x3fff ); + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + +for (i =0 ; i < cmd->hdr.num; i++) { + *data = cmd->data[i]; + wptr +=4 ; + data++; +if (wptr >= desc->end) { + wptr = desc->start; + data = shared->cmd_mem_vir; + } + } + +/*update wptr after data is written*/ +mb (); + desc->wptr = wptr; + +return 0 ; +} + +static bool vpu_rpc_check_msg (struct vpu_shared_addr *shared) +{ +struct vpu_rpc_buffer_desc *desc; +u32 space =0 ; +u32 msgword;drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+u32 msgnum; + +WARN_ON (!shared || !shared->msg_desc); + + desc = shared->msg_desc; + space =vpu_rpc_check_buffer_space (desc,0 ); + space = (space >>2 ); + +if (space) { + msgword = *(u32 *)(shared->msg_mem_vir + desc->rptr -
desc->start);drivers/media/platform/amphion/vpu_rpc.c
Quoted 9 lines
drivers/media/platform/amphion/vpu_rpc.c new file +9
-0+ msgnum = (msgword &0xff0000 ) >>16 ; +if (msgnum <= space) +return true; + } + +return false; +} + +static int vpu_rpc_receive_msg_buf(struct vpu_shared_addr *shared, struct
vpu_rpc_event*msg)Quoted 49 lines
drivers/media/platform/amphion/vpu_rpc.c new file +49
-0+{ +struct vpu_rpc_buffer_desc *desc; +u32 *data; +u32 msgword; +u32 rptr; +u32 i; + +WARN_ON (!shared || !shared->msg_desc || !msg); + +if (!vpu_rpc_check_msg (shared)) +return -EINVAL ; + + desc = shared->msg_desc; + data = (u32 *)(shared->msg_mem_vir + desc->rptr - desc->start); + rptr = desc->rptr; + msgword = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + + msg->hdr.index = (msgword >>24 ) &0xff ; + msg->hdr.num = (msgword >>16 ) &0xff ; + msg->hdr.id = msgword &0x3fff ; + +if (msg->hdr.num >ARRAY_SIZE (msg->data)) { +pr_err ("msg(%d) data length(%d) is out of range\n" , + msg->hdr.id, msg->hdr.num); +return -EINVAL ; + } + +for (i =0 ; i < msg->hdr.num; i++) { + msg->data[i] = *data; + data++; + rptr +=4 ; +if (rptr >= desc->end) { + rptr = desc->start; + data = shared->msg_mem_vir; + } + } + +/*update rptr after data is read*/ +mb (); + desc->rptr = rptr; + +return 0 ; +}drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+ +struct vpu_iface_ops imx8q_rpc_ops[] = { + [VPU_CORE_TYPE_ENC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_windsor_get_data_size, + .check_memory_region =
vpu_imx8q_check_memory_region,drivers/media/platform/amphion/vpu_rpc.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.c new file +10
-0+ .init_rpc = vpu_windsor_init_rpc, + .set_log_buf = vpu_windsor_set_log_buf, + .set_system_cfg = vpu_windsor_set_system_cfg, + .get_version = vpu_windsor_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .pack_cmd = vpu_windsor_pack_cmd, + .convert_msg_id = vpu_windsor_convert_msg_id, + .unpack_msg_data = vpu_windsor_unpack_msg_data, + .config_memory_resource =
vpu_windsor_config_memory_resource,drivers/media/platform/amphion/vpu_rpc.c line 201
Quoted 1 line
drivers/media/platform/amphion/vpu_rpc.c new file +1
-0+ .get_stream_buffer_size =
vpu_windsor_get_stream_buffer_size,drivers/media/platform/amphion/vpu_rpc.c line 203
Quoted 2 lines
drivers/media/platform/amphion/vpu_rpc.c new file +2
-0+ .config_stream_buffer = vpu_windsor_config_stream_buffer, + .get_stream_buffer_desc =
vpu_windsor_get_stream_buffer_desc,drivers/media/platform/amphion/vpu_rpc.c line 204
Quoted 1 line
drivers/media/platform/amphion/vpu_rpc.c new file +1
-0+ .update_stream_buffer =
vpu_windsor_update_stream_buffer,drivers/media/platform/amphion/vpu_rpc.c
Quoted 3 lines
drivers/media/platform/amphion/vpu_rpc.c new file +3
-0+ .set_encode_params = vpu_windsor_set_encode_params, + .input_frame = vpu_windsor_input_frame, + .get_max_instance_count =
vpu_windsor_get_max_instance_count,drivers/media/platform/amphion/vpu_rpc.c
Quoted 9 lines
drivers/media/platform/amphion/vpu_rpc.c new file +9
-0+ }, + [VPU_CORE_TYPE_DEC ] = { + .check_codec = vpu_imx8q_check_codec, + .check_fmt = vpu_imx8q_check_fmt, + .boot_core = vpu_imx8q_boot_core, + .get_power_state = vpu_imx8q_get_power_state, + .on_firmware_loaded = vpu_imx8q_on_firmware_loaded, + .get_data_size = vpu_malone_get_data_size, + .check_memory_region =
vpu_imx8q_check_memory_region,drivers/media/platform/amphion/vpu_rpc.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_rpc.c new file +7
-0+ .init_rpc = vpu_malone_init_rpc, + .set_log_buf = vpu_malone_set_log_buf, + .set_system_cfg = vpu_malone_set_system_cfg, + .get_version = vpu_malone_get_version, + .send_cmd_buf = vpu_rpc_send_cmd_buf, + .receive_msg_buf = vpu_rpc_receive_msg_buf, + .get_stream_buffer_size =
vpu_malone_get_stream_buffer_size,drivers/media/platform/amphion/vpu_rpc.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_rpc.c new file +6
-0+ .config_stream_buffer = vpu_malone_config_stream_buffer, + .set_decode_params = vpu_malone_set_decode_params, + .pack_cmd = vpu_malone_pack_cmd, + .convert_msg_id = vpu_malone_convert_msg_id, + .unpack_msg_data = vpu_malone_unpack_msg_data, + .get_stream_buffer_desc =
vpu_malone_get_stream_buffer_desc,drivers/media/platform/amphion/vpu_rpc.c line 230
Quoted 1 line
drivers/media/platform/amphion/vpu_rpc.c new file +1
-0+ .update_stream_buffer =
vpu_malone_update_stream_buffer,drivers/media/platform/amphion/vpu_rpc.c
Quoted 6 lines
drivers/media/platform/amphion/vpu_rpc.c new file +6
-0+ .add_scode = vpu_malone_add_scode, + .input_frame = vpu_malone_input_frame, + .pre_send_cmd = vpu_malone_pre_cmd, + .post_send_cmd = vpu_malone_post_cmd, + .init_instance = vpu_malone_init_instance, + .get_max_instance_count =
vpu_malone_get_max_instance_count,drivers/media/platform/amphion/vpu_rpc.c line 241
Quoted 5 lines
drivers/media/platform/amphion/vpu_rpc.c new file +5
-0+ }, +}; + + +static struct vpu_iface_ops *vpu_get_iface(struct vpu_dev *vpu, enum
vpu_core_typetype)Review
Quoted 39 lines
drivers/media/platform/amphion/vpu_rpc.c new file +38
-0+{ +struct vpu_iface_ops *rpc_ops =NULL ; +u32 size =0 ; + +WARN_ON (!vpu || !vpu->res); + +switch (vpu->res->plat_type) { +case IMX8QXP : +case IMX8QM : + rpc_ops = imx8q_rpc_ops; + size =ARRAY_SIZE (imx8q_rpc_ops); +break ; +default : +return NULL ; + } + +if (type >= size) +return NULL ; + +return &rpc_ops[type]; +} + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core) +{ +WARN_ON (!core || !core->vpu); + +return vpu_get_iface (core->vpu, core->type); +} + +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst) +{ +WARN_ON (!inst || !inst->vpu); + +if (inst->core) +return vpu_core_get_iface (inst->core); + +return vpu_get_iface (inst->vpu, inst->type); +}(quoted diff) +0
-0
b/drivers/media/platform/amphion/vpu_rpc.hQuoted 41 lines
new file mode 100644
index 000000000000..abe998e5a5bedrivers/media/platform/amphion/vpu_rpc.h new file +36
-0@@ -0,0 +1,464 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_RPC_H +#define _AMPHION_VPU_RPC_H + +#include <media/videobuf2-core.h> +#include "vpu_codec.h" + +struct vpu_rpc_buffer_desc { +u32 wptr; +u32 rptr; +u32 start; +u32 end; +}; + +struct vpu_shared_addr { +void *iface; +struct vpu_rpc_buffer_desc *cmd_desc; +void *cmd_mem_vir; +struct vpu_rpc_buffer_desc *msg_desc; +void *msg_mem_vir; + +unsigned long boot_addr; +struct vpu_core *core; +void *priv; +}; + +struct vpu_rpc_event_header { +u32 index; +u32 id; +u32 num; +}; +drivers/media/platform/amphion/vpu_rpc.h line 46
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+struct vpu_rpc_event { +struct vpu_rpc_event_header hdr; +u32 data[128 ]; +}; + +struct vpu_iface_ops { +bool (*check_codec)(enum vpu_core_type type); +bool (*check_fmt)(enum vpu_core_type type,u32 pixelfmt); +u32 (*get_data_size)(void ); + u32 (*check_memory_region)(dma_addr_t base, dma_addr_t addr,u32 size);
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+int (*boot_core)(struct vpu_core *core); +int (*shutdown_core)(struct vpu_core *core); +int (*restore_core)(struct vpu_core *core); +int (*get_power_state)(struct vpu_core *core); +int (*on_firmware_loaded)(struct vpu_core *core); +void (*init_rpc)(struct vpu_shared_addr *shared, +struct vpu_buffer *rpc,dma_addr_t boot_addr); +void (*set_log_buf)(struct vpu_shared_addr *shared, +struct vpu_buffer *log); + void (*set_system_cfg)(struct vpu_shared_addr *shared,drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ u32 regs_base, void __iomem *regs, u32 index); +void (*set_stream_cfg)(struct vpu_shared_addr *shared,u32 index); +u32 (*get_version)(struct vpu_shared_addr *shared); +u32 (*get_max_instance_count)(struct vpu_shared_addr *shared); +int (*get_stream_buffer_size)(struct vpu_shared_addr *shared); +int (*send_cmd_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *cmd); +int (*receive_msg_buf)(struct vpu_shared_addr *shared, +struct vpu_rpc_event *msg); + int (*pack_cmd)(struct vpu_rpc_event *pkt, u32 index, u32 id, void*data);
Quoted 4 lines
drivers/media/platform/amphion/vpu_rpc.h new file +4
-0+int (*convert_msg_id)(u32 msg_id); +int (*unpack_msg_data)(struct vpu_rpc_event *pkt,void *data); +int (*input_frame)(struct vpu_shared_addr *shared, +struct vpu_inst *inst,struct vb2_buffer *vb);drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+int (*config_memory_resource)(struct vpu_shared_addr *shared, +u32 instance, +u32 type, +u32 index, +struct vpu_buffer *buf); +int (*config_stream_buffer)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *buf); +int (*update_stream_buffer)(struct vpu_shared_addr *shared, + u32 instance, u32 ptr, boolwrite);
drivers/media/platform/amphion/vpu_rpc.h
Quoted 3 lines
drivers/media/platform/amphion/vpu_rpc.h new file +3
-0+int (*get_stream_buffer_desc)(struct vpu_shared_addr *shared, +u32 instance, + struct vpu_rpc_buffer_desc*desc);
Quoted 21 lines
drivers/media/platform/amphion/vpu_rpc.h new file +21
-0+int (*set_encode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_encode_params *params,u32 update); +int (*set_decode_params)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_decode_params *params,u32 update); +int (*add_scode)(struct vpu_shared_addr *shared, +u32 instance, +struct vpu_buffer *stream_buffer, +u32 pixelformat, +u32 scode_type); +int (*pre_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*post_send_cmd)(struct vpu_shared_addr *shared,u32 instance); +int (*init_instance)(struct vpu_shared_addr *shared,u32 instance); +}; + +enum { +VPU_CORE_MEMORY_INVALID =0 , +VPU_CORE_MEMORY_CACHED , +VPU_CORE_MEMORY_UNCACHED +};drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ +struct vpu_rpc_region_t { +dma_addr_t start; +dma_addr_t end; +dma_addr_t type; +}; + +struct vpu_iface_ops *vpu_core_get_iface (struct vpu_core *core); +struct vpu_iface_ops *vpu_inst_get_iface (struct vpu_inst *inst); +u32 vpu_iface_check_memory_region(struct vpu_core *core, dma_addr_taddr, u32 size);
drivers/media/platform/amphion/vpu_rpc.h
Quoted 12 lines
drivers/media/platform/amphion/vpu_rpc.h new file +12
-0+ +static inline bool vpu_iface_check_codec (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->check_codec) +return ops->check_codec (core->type); + +return true; +} + +static inline bool vpu_iface_check_format(struct vpu_inst *inst, u32pixelfmt)
Quoted 123 lines
drivers/media/platform/amphion/vpu_rpc.h new file +123
-0+{ +struct vpu_iface_ops *ops =vpu_inst_get_iface (inst); + +if (ops && ops->check_fmt) +return ops->check_fmt (inst->type, pixelfmt); + +return true; +} + +static inline int vpu_iface_boot_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->boot_core) +return ops->boot_core (core); +return 0 ; +} + +static inline int vpu_iface_get_power_state (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->get_power_state) +return ops->get_power_state (core); +return 1 ; +} + +static inline int vpu_iface_shutdown_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->shutdown_core) +return ops->shutdown_core (core); +return 0 ; +} + +static inline int vpu_iface_restore_core (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->restore_core) +return ops->restore_core (core); +return 0 ; +} + +static inline int vpu_iface_on_firmware_loaded (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (ops && ops->on_firmware_loaded) +return ops->on_firmware_loaded (core); + +return 0 ; +} + +static inline u32 vpu_iface_get_data_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_data_size) +return 0 ; + +return ops->get_data_size (); +} + +static inline int vpu_iface_init (struct vpu_core *core, +struct vpu_shared_addr *shared, +struct vpu_buffer *rpc, +dma_addr_t boot_addr) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->init_rpc) +return -EINVAL ; + + ops->init_rpc (shared, rpc, boot_addr); + core->iface = shared; + shared->core = core; +if (rpc->bytesused > rpc->length) +return -ENOSPC ; +return 0 ; +} + +static inline int vpu_iface_set_log_buf (struct vpu_core *core, +struct vpu_buffer *log) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; + +if (ops->set_log_buf) + ops->set_log_buf (core->iface, log); + +return 0 ; +} + +static inline int vpu_iface_config_system (struct vpu_core *core, +u32 regs_base,void __iomem *regs) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops) +return -EINVAL ; +if (ops->set_system_cfg) + ops->set_system_cfg (core->iface, regs_base, regs, core->id); + +return 0 ; +} + +static inline int vpu_iface_get_stream_buffer_size (struct vpu_core *core) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->get_stream_buffer_size) +return 0 ; + +return ops->get_stream_buffer_size (core->iface); +} + +static inline int vpu_iface_config_stream (struct vpu_inst *inst) +{ +struct vpu_iface_ops *ops =vpu_core_get_iface (inst->core);drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ +WARN_ON (inst->id <0 ); +if (!ops) +return -EINVAL ; +if (ops->set_stream_cfg) + ops->set_stream_cfg (inst->core->iface, inst->id); +return 0 ; +} + +static inline int vpu_iface_send_cmd(struct vpu_core *core, struct
vpu_rpc_event*cmd)drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->send_cmd_buf) +return -EINVAL ; + +return ops->send_cmd_buf (core->iface, cmd); +} + +static inline int vpu_iface_receive_msg(struct vpu_core *core, struct
vpu_rpc_event*msg)Quoted 11 lines
drivers/media/platform/amphion/vpu_rpc.h new file +11
-0+{ +struct vpu_iface_ops *ops =vpu_core_get_iface (core); + +if (!ops || !ops->receive_msg_buf) +return -EINVAL ; + +return ops->receive_msg_buf (core->iface, msg); +} + +static inline int vpu_iface_pack_cmd(struct vpu_core *core, + struct vpu_rpc_event *pkt,drivers/media/platform/amphion/vpu_rpc.h
Quoted 10 lines
drivers/media/platform/amphion/vpu_rpc.h new file +10
-0+ u32 index, u32 id, void *data) +{ + struct vpu_iface_ops *ops = vpu_core_get_iface(core); + +if (!ops || !ops->pack_cmd) +return -EINVAL ; +return ops->pack_cmd (pkt, index, id, data); +} + +static inline int vpu_iface_convert_msg_id(struct vpu_core *core, u32
msg_id)184 lines not shown · raw message
Part of this message is not shown here. View the raw message.
Ming Qian
maintainerPATCH v13 5/13 media: amphion: implement vpu core communication based on mailbox
In reply to Ming Qiandriver use mailbox to communicate with vpu core. and there are a command buffer and a message buffer. driver will write commands to the command buffer, then trigger a vpu core interrupt
vpu core will write messages to the message buffer, then trigger a cpu interrupt.
--- drivers/media/platform/amphion/vpu_cmds.c | 439 ++++++++++++++++++++++ drivers/media/platform/amphion/vpu_cmds.h | 25 ++ drivers/media/platform/amphion/vpu_mbox.c | 124 ++++++ drivers/media/platform/amphion/vpu_mbox.h | 16 + drivers/media/platform/amphion/vpu_msgs.c | 414 ++++++++++++++++++++ drivers/media/platform/amphion/vpu_msgs.h | 14 + 6 files changed, 1032 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_cmds.c create mode 100644 drivers/media/platform/amphion/vpu_cmds.h create mode 100644 drivers/media/platform/amphion/vpu_mbox.c create mode 100644 drivers/media/platform/amphion/vpu_mbox.h create mode 100644 drivers/media/platform/amphion/vpu_msgs.c create mode 100644 drivers/media/platform/amphion/vpu_msgs.h
drivers/media/platform/amphion/vpu_cmds.c new file +439 -0
@@ -0,0 +1,439 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/of_address.h> +#include <linux/platform_device.h> +#include <linux/slab.h> +#include <linux/types.h> +#include <linux/delay.h> +#include <linux/vmalloc.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_cmds.h" +#include "vpu_rpc.h" +#include "vpu_mbox.h" + +struct vpu_cmd_request { +u32 request; +u32 response; +u32 handled; +}; + +struct vpu_cmd_t { +struct list_head list; +u32 id; +struct vpu_cmd_request *request; +struct vpu_rpc_event *pkt; +unsigned long key; +}; + +static struct vpu_cmd_request vpu_cmd_requests[] = { + { + .request =VPU_CMD_ID_CONFIGURE_CODEC , + .response =VPU_MSG_ID_MEM_REQUEST , + .handled =1 , + }, + { + .request =VPU_CMD_ID_START , + .response =VPU_MSG_ID_START_DONE , + .handled =0 , + }, + { + .request =VPU_CMD_ID_STOP , + .response =VPU_MSG_ID_STOP_DONE , + .handled =0 , + }, + { + .request =VPU_CMD_ID_ABORT , + .response =VPU_MSG_ID_ABORT_DONE , + .handled =0 , + }, + { + .request =VPU_CMD_ID_RST_BUF , + .response =VPU_MSG_ID_BUF_RST , + .handled =1 , + }, +}; + +static int vpu_cmd_send (struct vpu_core *core,struct vpu_rpc_event *pkt) +{ +int ret =0 ; + +WARN_ON (!core || !pkt); + + ret =vpu_iface_send_cmd (core, pkt); +if (ret) +return ret; + +/*write cmd data to cmd buffer before trigger a cmd interrupt*/ +mb (); +vpu_mbox_send_type (core,COMMAND ); + +return ret; +} + +static struct vpu_cmd_t *vpu_alloc_cmd (struct vpu_inst *inst,u32 id,void *data) +{ +struct vpu_cmd_t *cmd; +int i; +int ret; + + cmd =vzalloc (sizeof (*cmd)); +if (!cmd) +return NULL ; + + cmd->pkt =vzalloc (sizeof (*cmd->pkt)); +if (!cmd->pkt) { +vfree (cmd); +return NULL ; + } + + cmd->id = id; + ret =vpu_iface_pack_cmd (inst->core, cmd->pkt, inst->id, id, data); +if (ret) { +dev_err (inst->dev,"iface pack cmd(%d) fail\n" , id); +vfree (cmd->pkt); +vfree (cmd); +return NULL ; + } +for (i =0 ; i <ARRAY_SIZE (vpu_cmd_requests); i++) { +if (vpu_cmd_requests[i].request == id) { + cmd->request = &vpu_cmd_requests[i]; +break ; + } + } + +return cmd; +} + +static void vpu_free_cmd (struct vpu_cmd_t *cmd) +{ +if (!cmd) +return ; +if (cmd->pkt) +vfree (cmd->pkt); +vfree (cmd); +} + +static int vpu_session_process_cmd (struct vpu_inst *inst,struct vpu_cmd_t *cmd) +{ +int ret; + +if (!inst || !cmd || !cmd->pkt) +return -EINVAL ; + +dev_dbg (inst->dev,"[%d]send cmd(0x%x)\n" , inst->id, cmd->id); +vpu_iface_pre_send_cmd (inst); + ret =vpu_cmd_send (inst->core, cmd->pkt); +if (!ret) { +vpu_iface_post_send_cmd (inst); +vpu_inst_record_flow (inst, cmd->id); + }else +dev_err (inst->dev,"[%d] iface send cmd(0x%x) fail\n" , inst->id, cmd->id); + +return ret; +} + +static void vpu_process_cmd_request (struct vpu_inst *inst) +{ +struct vpu_cmd_t *cmd; +struct vpu_cmd_t *tmp; + +if (!inst || inst->pending) +return ; + +list_for_each_entry_safe (cmd, tmp, &inst->cmd_q, list) { +list_del_init (&cmd->list); +if (vpu_session_process_cmd (inst, cmd)) +dev_err (inst->dev,"[%d] process cmd(%d) fail\n" , inst->id, cmd->id); +if (cmd->request) { + inst->pending = (void *)cmd; +break ; + } +vpu_free_cmd (cmd); + } +} + +static int vpu_request_cmd (struct vpu_inst *inst,u32 id,void *data, +unsigned long *key,int *sync) +{ +struct vpu_core *core; +struct vpu_cmd_t *cmd; + +if (!inst || !inst->core) +return -EINVAL ; + + core = inst->core; + cmd =vpu_alloc_cmd (inst, id, data); +if (!cmd) +return -ENOMEM ; + +mutex_lock (&core->cmd_lock); + cmd->key = core->cmd_seq++; +if (key) + *key = cmd->key; +if (sync) + *sync = cmd->request ? true : false; +list_add_tail (&cmd->list, &inst->cmd_q); +vpu_process_cmd_request (inst); +mutex_unlock (&core->cmd_lock); + +return 0 ; +} + +static void vpu_clear_pending (struct vpu_inst *inst) +{ +if (!inst || !inst->pending) +return ; + +vpu_free_cmd (inst->pending); +wake_up_all (&inst->core->ack_wq); + inst->pending =NULL ; +} + +static bool vpu_check_response (struct vpu_cmd_t *cmd,u32 response,u32 handled) +{ +struct vpu_cmd_request *request; + +if (!cmd || !cmd->request) +return false; + + request = cmd->request; +if (request->response != response) +return false; +if (request->handled != handled) +return false; + +return true; +} + +int vpu_response_cmd (struct vpu_inst *inst,u32 response,u32 handled) +{ +struct vpu_core *core; + +if (!inst || !inst->core) +return -EINVAL ; + + core = inst->core; +mutex_lock (&core->cmd_lock); +if (vpu_check_response (inst->pending, response, handled)) +vpu_clear_pending (inst); + +vpu_process_cmd_request (inst); +mutex_unlock (&core->cmd_lock); + +return 0 ; +} + +void vpu_clear_request (struct vpu_inst *inst) +{ +struct vpu_cmd_t *cmd; +struct vpu_cmd_t *tmp; + +mutex_lock (&inst->core->cmd_lock); +if (inst->pending) +vpu_clear_pending (inst); + +list_for_each_entry_safe (cmd, tmp, &inst->cmd_q, list) { +list_del_init (&cmd->list); +vpu_free_cmd (cmd); + } +mutex_unlock (&inst->core->cmd_lock); +} + +static bool check_is_responsed (struct vpu_inst *inst,unsigned long key) +{ +struct vpu_core *core = inst->core; +struct vpu_cmd_t *cmd; +bool flag = true; + +mutex_lock (&core->cmd_lock); + cmd = inst->pending; +if (cmd && key == cmd->key) { + flag = false; + goto exit; + } +list_for_each_entry (cmd, &inst->cmd_q, list) { +if (key == cmd->key) { + flag = false; +break ; + } + } +exit: +mutex_unlock (&core->cmd_lock); + +return flag; +} + +static int sync_session_response (struct vpu_inst *inst,unsigned long key) +{ +struct vpu_core *core; + +if (!inst || !inst->core) +return -EINVAL ; + + core = inst->core; + +call_vop (inst, wait_prepare); +wait_event_timeout (core->ack_wq, +check_is_responsed (inst, key), +VPU_TIMEOUT ); +call_vop (inst, wait_finish); + +if (!check_is_responsed (inst, key)) { +dev_err (inst->dev,"[%d] sync session timeout\n" , inst->id); +set_bit (inst->id, &core->hang_mask); +mutex_lock (&inst->core->cmd_lock); +vpu_clear_pending (inst); +mutex_unlock (&inst->core->cmd_lock); +return -EINVAL ; + } + +return 0 ; +} + +static int vpu_session_send_cmd (struct vpu_inst *inst,u32 id,void *data) +{ +unsigned long key; +int sync = false; +int ret = -EINVAL ; + +WARN_ON (!inst || !inst->core || inst->id <0 ); + + ret =vpu_request_cmd (inst, id, data, &key, &sync); +if (!ret && sync) + ret =sync_session_response (inst, key); + +if (ret) +dev_err (inst->dev,"[%d] send cmd(0x%x) fail\n" , inst->id, id); + +return ret; +} + +int vpu_session_configure_codec (struct vpu_inst *inst) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_CONFIGURE_CODEC ,NULL ); +} + +int vpu_session_start (struct vpu_inst *inst) +{ +vpu_trace (inst->dev,"[%d]\n" , inst->id); + +return vpu_session_send_cmd (inst,VPU_CMD_ID_START ,NULL ); +} + +int vpu_session_stop (struct vpu_inst *inst) +{ +int ret; + +vpu_trace (inst->dev,"[%d]\n" , inst->id); + + ret =vpu_session_send_cmd (inst,VPU_CMD_ID_STOP ,NULL ); +/* workaround for a firmware bug, +* if the next command is too close after stop cmd, +* the firmware may enter wfi wrongly. +*/ +usleep_range (3000 ,5000 ); +return ret; +} + +int vpu_session_encode_frame (struct vpu_inst *inst,s64 timestamp) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_FRAME_ENCODE , ×tamp); +} + +int vpu_session_alloc_fs (struct vpu_inst *inst,struct vpu_fs_info *fs) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_FS_ALLOC , fs); +} + +int vpu_session_release_fs (struct vpu_inst *inst,struct vpu_fs_info *fs) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_FS_RELEASE , fs); +} + +int vpu_session_abort (struct vpu_inst *inst) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_ABORT ,NULL ); +} + +int vpu_session_rst_buf (struct vpu_inst *inst) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_RST_BUF ,NULL ); +} + +int vpu_session_fill_timestamp (struct vpu_inst *inst,struct vpu_ts_info *info) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_TIMESTAMP , info); +} + +int vpu_session_update_parameters (struct vpu_inst *inst,void *arg) +{ +if (inst->type &VPU_CORE_TYPE_DEC ) +vpu_iface_set_decode_params (inst, arg,1 ); +else +vpu_iface_set_encode_params (inst, arg,1 ); + +return vpu_session_send_cmd (inst,VPU_CMD_ID_UPDATE_PARAMETER , arg); +} + +int vpu_session_debug (struct vpu_inst *inst) +{ +return vpu_session_send_cmd (inst,VPU_CMD_ID_DEBUG ,NULL ); +} + +int vpu_core_snapshot (struct vpu_core *core) +{ +struct vpu_inst *inst; +int ret; + +WARN_ON (!core ||list_empty (&core->instances)); + + inst = list_first_entry(&core->instances, struct vpu_inst, list); + +reinit_completion (&core->cmp); + ret =vpu_session_send_cmd (inst,VPU_CMD_ID_SNAPSHOT ,NULL ); +if (ret) +return ret; + ret =wait_for_completion_timeout (&core->cmp,VPU_TIMEOUT ); +if (!ret) { +dev_err (core->dev,"snapshot timeout\n" ); +return -EINVAL ; + } + +return 0 ; +} + +int vpu_core_sw_reset (struct vpu_core *core) +{ +struct vpu_rpc_event pkt; +int ret; + +WARN_ON (!core); + +memset (&pkt,0 ,sizeof (pkt)); +vpu_iface_pack_cmd (core, &pkt,0 ,VPU_CMD_ID_FIRM_RESET ,NULL ); + +reinit_completion (&core->cmp); +mutex_lock (&core->cmd_lock); + ret =vpu_cmd_send (core, &pkt); +mutex_unlock (&core->cmd_lock); +if (ret) +return ret; + ret =wait_for_completion_timeout (&core->cmp,VPU_TIMEOUT ); +if (!ret) { +dev_err (core->dev,"sw reset timeout\n" ); +return -EINVAL ; + } + +return 0 ; +}
drivers/media/platform/amphion/vpu_cmds.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_CMDS_H +#define _AMPHION_VPU_CMDS_H + +int vpu_session_configure_codec (struct vpu_inst *inst); +int vpu_session_start (struct vpu_inst *inst); +int vpu_session_stop (struct vpu_inst *inst); +int vpu_session_abort (struct vpu_inst *inst); +int vpu_session_rst_buf (struct vpu_inst *inst); +int vpu_session_encode_frame (struct vpu_inst *inst,s64 timestamp); +int vpu_session_alloc_fs (struct vpu_inst *inst,struct vpu_fs_info *fs); +int vpu_session_release_fs (struct vpu_inst *inst,struct vpu_fs_info *fs); +int vpu_session_fill_timestamp (struct vpu_inst *inst,struct vpu_ts_info *info); +int vpu_session_update_parameters (struct vpu_inst *inst,void *arg); +int vpu_core_snapshot (struct vpu_core *core); +int vpu_core_sw_reset (struct vpu_core *core); +int vpu_response_cmd (struct vpu_inst *inst,u32 response,u32 handled); +void vpu_clear_request (struct vpu_inst *inst); +int vpu_session_debug (struct vpu_inst *inst); + +#endif
drivers/media/platform/amphion/vpu_mbox.c new file +124 -0
124 lines not on this page · raw message
drivers/media/platform/amphion/vpu_mbox.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_MBOX_H +#define _AMPHION_VPU_MBOX_H + +int vpu_mbox_init (struct vpu_core *core); +int vpu_mbox_request (struct vpu_core *core); +void vpu_mbox_free (struct vpu_core *core); +void vpu_mbox_send_msg (struct vpu_core *core,u32 type,u32 data); +void vpu_mbox_send_type (struct vpu_core *core,u32 type); +void vpu_mbox_enable_rx (struct vpu_dev *dev); + +#endif
drivers/media/platform/amphion/vpu_msgs.c new file +414 -0
414 lines not on this page · raw message
drivers/media/platform/amphion/vpu_msgs.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_MSGS_H +#define _AMPHION_VPU_MSGS_H + +int vpu_isr (struct vpu_core *core,u32 irq); +void vpu_inst_run_work (struct work_struct *work); +void vpu_msg_run_work (struct work_struct *work); +void vpu_msg_delayed_work (struct work_struct *work); + +#endif
Signature · 1 line
2.33.0
Ming Qian
maintainerPATCH v13 6/13 media: amphion: add vpu v4l2 m2m support
In reply to Ming Qianvpu_v4l2.c implements the v4l2 m2m driver methods. vpu_helpers.c implements the common helper functions vpu_color.c converts the v4l2 colorspace with iso
--- drivers/media/platform/amphion/vpu_color.c | 190 +++++ drivers/media/platform/amphion/vpu_helpers.c | 436 ++++++++++++ drivers/media/platform/amphion/vpu_helpers.h | 71 ++ drivers/media/platform/amphion/vpu_v4l2.c | 703 +++++++++++++++++++ drivers/media/platform/amphion/vpu_v4l2.h | 54 ++ 5 files changed, 1454 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_color.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.h create mode 100644 drivers/media/platform/amphion/vpu_v4l2.c create mode 100644 drivers/media/platform/amphion/vpu_v4l2.h
drivers/media/platform/amphion/vpu_color.c new file +190 -0
@@ -0,0 +1,190 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/delay.h> +#include <linux/types.h> +#include <media/v4l2-device.h> +#include "vpu.h" +#include "vpu_helpers.h" + +static const u8 colorprimaries[] = { +0 , +V4L2_COLORSPACE_REC709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +V4L2_COLORSPACE_470_SYSTEM_M ,/*Rec. ITU-R BT.470-6 System M*/ +V4L2_COLORSPACE_470_SYSTEM_BG ,/*Rec. ITU-R BT.470-6 System B, G*/ +V4L2_COLORSPACE_SMPTE170M ,/*SMPTE170M*/ +V4L2_COLORSPACE_SMPTE240M ,/*SMPTE240M*/ +0 ,/*Generic film*/ +V4L2_COLORSPACE_BT2020 ,/*Rec. ITU-R BT.2020-2*/ +0 ,/*SMPTE ST 428-1*/ +}; + +static const u8 colortransfers[] = { +0 , +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +0 ,/*Rec. ITU-R BT.470-6 System M*/ +0 ,/*Rec. ITU-R BT.470-6 System B, G*/ +V4L2_XFER_FUNC_709 ,/*SMPTE170M*/ +V4L2_XFER_FUNC_SMPTE240M ,/*SMPTE240M*/ +V4L2_XFER_FUNC_NONE ,/*Linear transfer characteristics*/ +0 , +0 , +0 ,/*IEC 61966-2-4*/ +0 ,/*Rec. ITU-R BT.1361-0 extended colour gamut*/ +V4L2_XFER_FUNC_SRGB ,/*IEC 61966-2-1 sRGB or sYCC*/ +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.2020-2 (10 bit system)*/ +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.2020-2 (12 bit system)*/ +V4L2_XFER_FUNC_SMPTE2084 ,/*SMPTE ST 2084*/ +0 ,/*SMPTE ST 428-1*/ +0 /*Rec. ITU-R BT.2100-0 hybrid log-gamma (HLG)*/ +}; + +static const u8 colormatrixcoefs[] = { +0 , +V4L2_YCBCR_ENC_709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +0 ,/*Title 47 Code of Federal Regulations*/ +V4L2_YCBCR_ENC_601 ,/*Rec. ITU-R BT.601-7 625*/ +V4L2_YCBCR_ENC_601 ,/*Rec. ITU-R BT.601-7 525*/ +V4L2_YCBCR_ENC_SMPTE240M ,/*SMPTE240M*/ +0 , +V4L2_YCBCR_ENC_BT2020 ,/*Rec. ITU-R BT.2020-2*/ +V4L2_YCBCR_ENC_BT2020_CONST_LUM /*Rec. ITU-R BT.2020-2 constant*/ +}; + +u32 vpu_color_cvrt_primaries_v2i (u32 primaries) +{ +return VPU_ARRAY_FIND (colorprimaries, primaries); +} + +u32 vpu_color_cvrt_primaries_i2v (u32 primaries) +{ +return VPU_ARRAY_AT (colorprimaries, primaries); +} + +u32 vpu_color_cvrt_transfers_v2i (u32 transfers) +{ +return VPU_ARRAY_FIND (colortransfers, transfers); +} + +u32 vpu_color_cvrt_transfers_i2v (u32 transfers) +{ +return VPU_ARRAY_AT (colortransfers, transfers); +} + +u32 vpu_color_cvrt_matrix_v2i (u32 matrix) +{ +return VPU_ARRAY_FIND (colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_matrix_i2v (u32 matrix) +{ +return VPU_ARRAY_AT (colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_full_range_v2i (u32 full_range) +{ +return (full_range ==V4L2_QUANTIZATION_FULL_RANGE ); +} + +u32 vpu_color_cvrt_full_range_i2v (u32 full_range) +{ +if (full_range) +return V4L2_QUANTIZATION_FULL_RANGE ; + +return V4L2_QUANTIZATION_LIM_RANGE ; +} + +int vpu_color_check_primaries (u32 primaries) +{ +return vpu_color_cvrt_primaries_v2i (primaries) ?0 : -EINVAL ; +} + +int vpu_color_check_transfers (u32 transfers) +{ +return vpu_color_cvrt_transfers_v2i (transfers) ?0 : -EINVAL ; +} + +int vpu_color_check_matrix (u32 matrix) +{ +return vpu_color_cvrt_matrix_v2i (matrix) ?0 : -EINVAL ; +} + +int vpu_color_check_full_range (u32 full_range) +{ +int ret = -EINVAL ; + +switch (full_range) { +case V4L2_QUANTIZATION_FULL_RANGE : +case V4L2_QUANTIZATION_LIM_RANGE : + ret =0 ; +break ; +default : +break ; + + } + +return ret; +} + +int vpu_color_get_default (u32 primaries, +u32 *ptransfers,u32 *pmatrix,u32 *pfull_range) +{ +u32 transfers; +u32 matrix; +u32 full_range; + +switch (primaries) { +case V4L2_COLORSPACE_REC709 : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_709 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_470_SYSTEM_M : +case V4L2_COLORSPACE_470_SYSTEM_BG : +case V4L2_COLORSPACE_SMPTE170M : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_601 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_SMPTE240M : + transfers =V4L2_XFER_FUNC_SMPTE240M ; + matrix =V4L2_YCBCR_ENC_SMPTE240M ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_BT2020 : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_BT2020 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +default : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_709 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; + } + +if (ptransfers) + *ptransfers = transfers; +if (pmatrix) + *pmatrix = matrix; +if (pfull_range) + *pfull_range = full_range; + + +return 0 ; +}
drivers/media/platform/amphion/vpu_helpers.c new file +436 -0
436 lines not on this page · raw message
drivers/media/platform/amphion/vpu_helpers.h new file +71 -0
71 lines not on this page · raw message
drivers/media/platform/amphion/vpu_v4l2.c new file +703 -0
703 lines not on this page · raw message
drivers/media/platform/amphion/vpu_v4l2.h new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.33.0
Re: [PATCH v13 06/13] media: amphion: add vpu v4l2 m2m support
Quoted 3 lines · Ming Qian
vpu_v4l2.cimplements the v4l2 m2m driver methods.vpu_helpers.cimplements the common helper functionsvpu_color.cconverts the v4l2 colorspace with iso
iso?
Quoted 44 lines
Signed-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>Reported-by kernel test robot <[email protected]>--- drivers/media/platform/amphion/vpu_color.c | 190 +++++ drivers/media/platform/amphion/vpu_helpers.c | 436 ++++++++++++ drivers/media/platform/amphion/vpu_helpers.h | 71 ++ drivers/media/platform/amphion/vpu_v4l2.c | 703 +++++++++++++++++++ drivers/media/platform/amphion/vpu_v4l2.h | 54 ++ 5 files changed, 1454 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_color.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.h create mode 100644 drivers/media/platform/amphion/vpu_v4l2.c create mode 100644 drivers/media/platform/amphion/vpu_v4l2.hdrivers/media/platform/amphion/vpu_color.c new file +20
-0@@ -0,0 +1,190 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/delay.h> +#include <linux/types.h> +#include <media/v4l2-device.h> +#include "vpu.h" +#include "vpu_helpers.h" + +static const u8 colorprimaries[] = { +0 ,
drivers/media/platform/amphion/vpu_color.c line 30
Quoted 10 lines
drivers/media/platform/amphion/vpu_color.c new file +10
-0+V4L2_COLORSPACE_REC709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +V4L2_COLORSPACE_470_SYSTEM_M ,/*Rec. ITU-R BT.470-6 System M*/ +V4L2_COLORSPACE_470_SYSTEM_BG ,/*Rec. ITU-R BT.470-6 System B, G*/ +V4L2_COLORSPACE_SMPTE170M ,/*SMPTE170M*/ +V4L2_COLORSPACE_SMPTE240M ,/*SMPTE240M*/ +0 ,/*Generic film*/ +V4L2_COLORSPACE_BT2020 ,/*Rec. ITU-R BT.2020-2*/ + 0, /*SMPTE ST 428-1*/
Add space after /* and before */
Quoted 12 lines
drivers/media/platform/amphion/vpu_color.c new file +12
-0+}; + +static const u8 colortransfers[] = { +0 , +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +0 ,/*Rec. ITU-R BT.470-6 System M*/ +0 ,/*Rec. ITU-R BT.470-6 System B, G*/ +V4L2_XFER_FUNC_709 ,/*SMPTE170M*/ +V4L2_XFER_FUNC_SMPTE240M ,/*SMPTE240M*/ +V4L2_XFER_FUNC_NONE ,/*Linear transfer characteristics*/
drivers/media/platform/amphion/vpu_color.c line 52
Quoted 10 lines
drivers/media/platform/amphion/vpu_color.c new file +10
-0+0 , +0 , +0 ,/*IEC 61966-2-4*/ +0 ,/*Rec. ITU-R BT.1361-0 extended colour gamut*/ +V4L2_XFER_FUNC_SRGB ,/*IEC 61966-2-1 sRGB or sYCC*/ +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.2020-2 (10 bit system)*/ +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.2020-2 (12 bit system)*/ +V4L2_XFER_FUNC_SMPTE2084 ,/*SMPTE ST 2084*/ +0 ,/*SMPTE ST 428-1*/ +0 /*Rec. ITU-R BT.2100-0 hybrid log-gamma (HLG)*/
Ditto here and elsewhere.
Quoted 116 lines
drivers/media/platform/amphion/vpu_color.c new file +116
-0+}; + +static const u8 colormatrixcoefs[] = { +0 , +V4L2_YCBCR_ENC_709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +0 ,/*Title 47 Code of Federal Regulations*/ +V4L2_YCBCR_ENC_601 ,/*Rec. ITU-R BT.601-7 625*/ +V4L2_YCBCR_ENC_601 ,/*Rec. ITU-R BT.601-7 525*/ +V4L2_YCBCR_ENC_SMPTE240M ,/*SMPTE240M*/ +0 , +V4L2_YCBCR_ENC_BT2020 ,/*Rec. ITU-R BT.2020-2*/ +V4L2_YCBCR_ENC_BT2020_CONST_LUM /*Rec. ITU-R BT.2020-2 constant*/ +}; + +u32 vpu_color_cvrt_primaries_v2i (u32 primaries) +{ +return VPU_ARRAY_FIND (colorprimaries, primaries); +} + +u32 vpu_color_cvrt_primaries_i2v (u32 primaries) +{ +return VPU_ARRAY_AT (colorprimaries, primaries); +} + +u32 vpu_color_cvrt_transfers_v2i (u32 transfers) +{ +return VPU_ARRAY_FIND (colortransfers, transfers); +} + +u32 vpu_color_cvrt_transfers_i2v (u32 transfers) +{ +return VPU_ARRAY_AT (colortransfers, transfers); +} + +u32 vpu_color_cvrt_matrix_v2i (u32 matrix) +{ +return VPU_ARRAY_FIND (colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_matrix_i2v (u32 matrix) +{ +return VPU_ARRAY_AT (colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_full_range_v2i (u32 full_range) +{ +return (full_range ==V4L2_QUANTIZATION_FULL_RANGE ); +} + +u32 vpu_color_cvrt_full_range_i2v (u32 full_range) +{ +if (full_range) +return V4L2_QUANTIZATION_FULL_RANGE ; + +return V4L2_QUANTIZATION_LIM_RANGE ; +} + +int vpu_color_check_primaries (u32 primaries) +{ +return vpu_color_cvrt_primaries_v2i (primaries) ?0 : -EINVAL ; +} + +int vpu_color_check_transfers (u32 transfers) +{ +return vpu_color_cvrt_transfers_v2i (transfers) ?0 : -EINVAL ; +} + +int vpu_color_check_matrix (u32 matrix) +{ +return vpu_color_cvrt_matrix_v2i (matrix) ?0 : -EINVAL ; +} + +int vpu_color_check_full_range (u32 full_range) +{ +int ret = -EINVAL ; + +switch (full_range) { +case V4L2_QUANTIZATION_FULL_RANGE : +case V4L2_QUANTIZATION_LIM_RANGE : + ret =0 ; +break ; +default : +break ; + + } + +return ret; +} + +int vpu_color_get_default (u32 primaries, +u32 *ptransfers,u32 *pmatrix,u32 *pfull_range) +{ +u32 transfers; +u32 matrix; +u32 full_range; + +switch (primaries) { +case V4L2_COLORSPACE_REC709 : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_709 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_470_SYSTEM_M : +case V4L2_COLORSPACE_470_SYSTEM_BG : +case V4L2_COLORSPACE_SMPTE170M : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_601 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_SMPTE240M : + transfers =V4L2_XFER_FUNC_SMPTE240M ; + matrix =V4L2_YCBCR_ENC_SMPTE240M ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ;
drivers/media/platform/amphion/vpu_color.c line 178
Quoted 10 lines
drivers/media/platform/amphion/vpu_color.c new file +10
-0+case V4L2_COLORSPACE_BT2020 : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_BT2020 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +default : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_709 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ;
You can use V4L2_MAP_XFER_FUNC_DEFAULT and V4L2_MAP_YCBCR_ENC_DEFAULT here.
Do you even need to provide the quantization range? Isn't it always lim range anyway?
Quoted 381 lines
drivers/media/platform/amphion/vpu_color.c new file +12
-0+ } + +if (ptransfers) + *ptransfers = transfers; +if (pmatrix) + *pmatrix = matrix; +if (pfull_range) + *pfull_range = full_range; + + +return 0 ; +}drivers/media/platform/amphion/vpu_helpers.c new file +363
-0@@ -0,0 +1,436 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_rpc.h" +#include "vpu_helpers.h" + +int vpu_helper_find_in_array_u8 (const u8 *array,u32 size,u32 x) +{ +int i; + +for (i =0 ; i < size; i++) { +if (array[i] == x) +return i; + } + +return 0 ; +} + +bool vpu_helper_check_type (struct vpu_inst *inst,u32 type) +{ +const struct vpu_format *pfmt; + +for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { +if (!vpu_iface_check_format (inst, pfmt->pixfmt)) +continue ; +if (pfmt->type == type) +return true; + } + +return false; +} + +const struct vpu_format *vpu_helper_find_format (struct vpu_inst *inst,u32 type,u32 pixelfmt) +{ +const struct vpu_format *pfmt; + +if (!inst || !inst->formats) +return NULL ; + +if (!vpu_iface_check_format (inst, pixelfmt)) +return NULL ; + +for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { +if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type)) +return pfmt; + } + +return NULL ; +} + +const struct vpu_format *vpu_helper_enum_format (struct vpu_inst *inst,u32 type,int index) +{ +const struct vpu_format *pfmt; +int i =0 ; + +if (!inst || !inst->formats) +return NULL ; + +for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { +if (!vpu_iface_check_format (inst, pfmt->pixfmt)) +continue ; + +if (pfmt->type == type) { +if (index == i) +return pfmt; + i++; + } + } + +return NULL ; +} + +u32 vpu_helper_valid_frame_width (struct vpu_inst *inst,u32 width) +{ +const struct vpu_core_resources *res; + +if (!inst) +return width; + + res =vpu_get_resource (inst); +if (!res) +return width; +if (res->max_width) + width =clamp (width, res->min_width, res->max_width); +if (res->step_width) + width =ALIGN (width, res->step_width); + +return width; +} + +u32 vpu_helper_valid_frame_height (struct vpu_inst *inst,u32 height) +{ +const struct vpu_core_resources *res; + +if (!inst) +return height; + + res =vpu_get_resource (inst); +if (!res) +return height; +if (res->max_height) + height =clamp (height, res->min_height, res->max_height); +if (res->step_height) + height =ALIGN (height, res->step_height); + +return height; +} + +static u32 get_nv12_plane_size (u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +u32 bytesperline; +u32 size =0 ; + + bytesperline =ALIGN (width, stride); +if (pbl) + bytesperline =max (bytesperline, *pbl); + height =ALIGN (height,2 ); +if (plane_no ==0 ) + size = bytesperline * height; +else if (plane_no ==1 ) + size = bytesperline * height >>1 ; +if (pbl) + *pbl = bytesperline; + +return size; +} + +static u32 get_tiled_8l128_plane_size (u32 fmt,u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +u32 ws =3 ; +u32 hs =7 ; +u32 bitdepth =8 ; +u32 bytesperline; +u32 size =0 ; + +if (interlaced) + hs++; +if (fmt ==V4L2_PIX_FMT_NV12MT_10BE_8L128 ) + bitdepth =10 ; + bytesperline =DIV_ROUND_UP (width * bitdepth,BITS_PER_BYTE ); + bytesperline =ALIGN (bytesperline,1 << ws); + bytesperline =ALIGN (bytesperline, stride); +if (pbl) + bytesperline =max (bytesperline, *pbl); + height =ALIGN (height,1 << hs); +if (plane_no ==0 ) + size = bytesperline * height; +else if (plane_no ==1 ) + size = (bytesperline *ALIGN (height,1 << (hs +1 ))) >>1 ; +if (pbl) + *pbl = bytesperline; + +return size; +} + +static u32 get_default_plane_size (u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +u32 bytesperline; +u32 size =0 ; + + bytesperline =ALIGN (width, stride); +if (pbl) + bytesperline =max (bytesperline, *pbl); +if (plane_no ==0 ) + size = bytesperline * height; +if (pbl) + *pbl = bytesperline; + +return size; +} + +u32 vpu_helper_get_plane_size (u32 fmt,u32 w,u32 h,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +switch (fmt) { +case V4L2_PIX_FMT_NV12M : +return get_nv12_plane_size (w, h, plane_no, stride, interlaced, pbl); +case V4L2_PIX_FMT_NV12MT_8L128 : +case V4L2_PIX_FMT_NV12MT_10BE_8L128 : +return get_tiled_8l128_plane_size (fmt, w, h, plane_no, stride, interlaced, pbl); +default : +return get_default_plane_size (w, h, plane_no, stride, interlaced, pbl); + } +} + +u32 vpu_helper_copy_from_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *rptr,u32 size,void *dst) +{ +u32 offset; +u32 start; +u32 end; +void *virt; + +if (!stream_buffer || !rptr || !dst) +return -EINVAL ; + +if (!size) +return 0 ; + + offset = *rptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + +if (offset < start || offset > end) +return -EINVAL ; + +if (offset + size <= end) { +memcpy (dst, virt + (offset - start), size); + }else { +memcpy (dst, virt + (offset - start), end - offset); +memcpy (dst + end - offset, virt, size + offset - end); + } + + *rptr =vpu_helper_step_walk (stream_buffer, offset, size); +return size; +} + +u32 vpu_helper_copy_to_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u32 size,void *src) +{ +u32 offset; +u32 start; +u32 end; +void *virt; + +if (!stream_buffer || !wptr || !src) +return -EINVAL ; + +if (!size) +return 0 ; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; +if (offset < start || offset > end) +return -EINVAL ; + +if (offset + size <= end) { +memcpy (virt + (offset - start), src, size); + }else { +memcpy (virt + (offset - start), src, end - offset); +memcpy (virt, src + end - offset, size + offset - end); + } + + *wptr =vpu_helper_step_walk (stream_buffer, offset, size); + +return size; +} + +u32 vpu_helper_memset_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u8 val,u32 size) +{ +u32 offset; +u32 start; +u32 end; +void *virt; + +if (!stream_buffer || !wptr) +return -EINVAL ; + +if (!size) +return 0 ; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; +if (offset < start || offset > end) +return -EINVAL ; + +if (offset + size <= end) { +memset (virt + (offset - start), val, size); + }else { +memset (virt + (offset - start), val, end - offset); +memset (virt, val, size + offset - end); + } + + offset += size; +if (offset >= end) + offset -= stream_buffer->length; + + *wptr = offset; + +return size; +} + +u32 vpu_helper_get_free_space (struct vpu_inst *inst) +{ +struct vpu_rpc_buffer_desc desc; + +if (vpu_iface_get_stream_buffer_desc (inst, &desc)) +return 0 ; + +if (desc.rptr > desc.wptr) +return desc.rptr - desc.wptr; +else if (desc.rptr < desc.wptr) +return (desc.end - desc.start + desc.rptr - desc.wptr); +else +return desc.end - desc.start; +} + +u32 vpu_helper_get_used_space (struct vpu_inst *inst) +{ +struct vpu_rpc_buffer_desc desc; + +if (vpu_iface_get_stream_buffer_desc (inst, &desc)) +return 0 ; + +if (desc.wptr > desc.rptr) +return desc.wptr - desc.rptr; +else if (desc.wptr < desc.rptr) +return (desc.end - desc.start + desc.wptr - desc.rptr); +else +return 0 ; +} + +int vpu_helper_g_volatile_ctrl (struct v4l2_ctrl *ctrl) +{ +struct vpu_inst *inst =ctrl_to_inst (ctrl); + +switch (ctrl->id) { +case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE : + ctrl->val = inst->min_buffer_cap; +break ; +case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT : + ctrl->val = inst->min_buffer_out; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +u32 vpu_helper_calc_coprime (u32 *a,u32 *b) +{ +int m = *a; +int n = *b; + +if (m ==0 ) +return n; +if (n ==0 ) +return m; + +while (n !=0 ) { +int tmp = m % n; +
drivers/media/platform/amphion/vpu_helpers.c line 373
Quoted 10 lines
drivers/media/platform/amphion/vpu_helpers.c new file +10
-0+ m = n; + n = tmp; + } + *a = (*a) / m; + *b = (*b) / m; + +return m; +} + +#define READ_BYTE (buffer, pos) (*(u8 *)((buffer)->virt + ((pos) % buffer->length)))
Add newline. Also split up the define into two lines:
#define READ_BYTE(buffer, pos) \ (*(u8 *)((buffer)->virt + ((pos) % buffer->length)))
Quoted 202 lines
drivers/media/platform/amphion/vpu_helpers.c new file +63
-0+int vpu_helper_find_startcode (struct vpu_buffer *stream_buffer, +u32 pixelformat,u32 offset,u32 bytesused) +{ +u32 start_code; +int start_code_size; +u32 val =0 ; +int i; +int ret = -EINVAL ; + +if (!stream_buffer || !stream_buffer->virt) +return -EINVAL ; + +switch (pixelformat) { +case V4L2_PIX_FMT_H264 : + start_code_size =4 ; + start_code =0x00000001 ; +break ; +default : +return 0 ; + } + +for (i =0 ; i < bytesused; i++) { + val = (val <<8 ) |READ_BYTE (stream_buffer, offset + i); +if (i < start_code_size -1 ) +continue ; +if (val == start_code) { + ret = i +1 - start_code_size; +break ; + } + } + +return ret; +} + +int vpu_find_dst_by_src (struct vpu_pair *pairs,u32 cnt,u32 src) +{ +u32 i; + +if (!pairs || !cnt) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) { +if (pairs[i].src == src) +return pairs[i].dst; + } + +return -EINVAL ; +} + +int vpu_find_src_by_dst (struct vpu_pair *pairs,u32 cnt,u32 dst) +{ +u32 i; + +if (!pairs || !cnt) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) { +if (pairs[i].dst == dst) +return pairs[i].src; + } + +return -EINVAL ; +}drivers/media/platform/amphion/vpu_helpers.h new file +71
-0@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_HELPERS_H +#define _AMPHION_VPU_HELPERS_H + +struct vpu_pair { +u32 src; +u32 dst; +}; + +#define MAKE_TIMESTAMP (s, ns) (((s32)(s) * NSEC_PER_SEC) + (ns)) +#define VPU_INVALID_TIMESTAMP MAKE_TIMESTAMP(-1, 0) +#define VPU_ARRAY_AT (array, i) (((i) < ARRAY_SIZE(array)) ? array[i] : 0) +#define VPU_ARRAY_FIND (array, x) vpu_helper_find_in_array_u8(array, ARRAY_SIZE(array), x) + +int vpu_helper_find_in_array_u8 (const u8 *array,u32 size,u32 x); +bool vpu_helper_check_type (struct vpu_inst *inst,u32 type); +const struct vpu_format *vpu_helper_find_format (struct vpu_inst *inst,u32 type,u32 pixelfmt); +const struct vpu_format *vpu_helper_enum_format (struct vpu_inst *inst,u32 type,int index); +u32 vpu_helper_valid_frame_width (struct vpu_inst *inst,u32 width); +u32 vpu_helper_valid_frame_height (struct vpu_inst *inst,u32 height); +u32 vpu_helper_get_plane_size (u32 fmt,u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl); +u32 vpu_helper_copy_from_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *rptr,u32 size,void *dst); +u32 vpu_helper_copy_to_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u32 size,void *src); +u32 vpu_helper_memset_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u8 val,u32 size); +u32 vpu_helper_get_free_space (struct vpu_inst *inst); +u32 vpu_helper_get_used_space (struct vpu_inst *inst); +int vpu_helper_g_volatile_ctrl (struct v4l2_ctrl *ctrl); +u32 vpu_helper_calc_coprime (u32 *a,u32 *b); +void vpu_helper_get_kmp_next (const u8 *pattern,int *next,int size); +int vpu_helper_kmp_search (u8 *s,int s_len,const u8 *p,int p_len,int *next); +int vpu_helper_kmp_search_in_stream_buffer (struct vpu_buffer *stream_buffer, +u32 offset,int bytesused, +const u8 *p,int p_len,int *next); +int vpu_helper_find_startcode (struct vpu_buffer *stream_buffer, +u32 pixelformat,u32 offset,u32 bytesused); + +static inline u32 vpu_helper_step_walk (struct vpu_buffer *stream_buffer,u32 pos,u32 step) +{ + pos += step; +if (pos > stream_buffer->phys + stream_buffer->length) + pos -= stream_buffer->length; + +return pos; +} + +int vpu_color_check_primaries (u32 primaries); +int vpu_color_check_transfers (u32 transfers); +int vpu_color_check_matrix (u32 matrix); +int vpu_color_check_full_range (u32 full_range); +u32 vpu_color_cvrt_primaries_v2i (u32 primaries); +u32 vpu_color_cvrt_primaries_i2v (u32 primaries); +u32 vpu_color_cvrt_transfers_v2i (u32 transfers); +u32 vpu_color_cvrt_transfers_i2v (u32 transfers); +u32 vpu_color_cvrt_matrix_v2i (u32 matrix); +u32 vpu_color_cvrt_matrix_i2v (u32 matrix); +u32 vpu_color_cvrt_full_range_v2i (u32 full_range); +u32 vpu_color_cvrt_full_range_i2v (u32 full_range); +int vpu_color_get_default (u32 primaries, +u32 *ptransfers,u32 *pmatrix,u32 *pfull_range); + +int vpu_find_dst_by_src (struct vpu_pair *pairs,u32 cnt,u32 src); +int vpu_find_src_by_dst (struct vpu_pair *pairs,u32 cnt,u32 dst); +#endif drivers/media/platform/amphion/vpu_v4l2.c new file +56
-0@@ -0,0 +1,703 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/pm_runtime.h> +#include <linux/videodev2.h> +#include <media/v4l2-device.h> +#include <media/v4l2-event.h> +#include <media/v4l2-mem2mem.h> +#include <media/v4l2-ioctl.h> +#include <media/videobuf2-v4l2.h> +#include <media/videobuf2-dma-contig.h> +#include <media/videobuf2-vmalloc.h> +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_v4l2.h" +#include "vpu_msgs.h" +#include "vpu_helpers.h" + +void vpu_inst_lock (struct vpu_inst *inst) +{ +mutex_lock (&inst->lock); +} + +void vpu_inst_unlock (struct vpu_inst *inst) +{ +mutex_unlock (&inst->lock); +} + +dma_addr_t vpu_get_vb_phy_addr (struct vb2_buffer *vb,u32 plane_no) +{ +if (plane_no >= vb->num_planes) +return 0 ; +return vb2_dma_contig_plane_dma_addr (vb, plane_no) + + vb->planes[plane_no].data_offset; +} + +unsigned int vpu_get_vb_length (struct vb2_buffer *vb,u32 plane_no) +{ +if (plane_no >= vb->num_planes) +return 0 ; +return vb2_plane_size (vb, plane_no) - vb->planes[plane_no].data_offset; +} + +void vpu_v4l2_set_error (struct vpu_inst *inst) +{ +struct vb2_queue *src_q =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +struct vb2_queue *dst_q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +
drivers/media/platform/amphion/vpu_v4l2.c line 66
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+dev_err (inst->dev,"some error occurs in codec\n" ); +if (src_q) + src_q->error =1 ; +if (dst_q) + dst_q->error =1 ; +} + +int vpu_notify_eos (struct vpu_inst *inst) +{ +const struct v4l2_event ev = {
Can be static.
Quoted 7 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +7
-0+ .id = 0, + .type = V4L2_EVENT_EOS + }; + +vpu_trace (inst->dev,"[%d]\n" , inst->id); +v4l2_event_queue_fh (&inst->fh, &ev); +
drivers/media/platform/amphion/vpu_v4l2.c line 83
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+return 0 ; +} + +int vpu_notify_source_change (struct vpu_inst *inst) +{ +const struct v4l2_event ev = { + .id =0 , + .type =V4L2_EVENT_SOURCE_CHANGE , + .u.src_change.changes =V4L2_EVENT_SRC_CH_RESOLUTION + };
Ditto.
Quoted 249 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +249
-0+ +vpu_trace (inst->dev,"[%d]\n" , inst->id); +v4l2_event_queue_fh (&inst->fh, &ev); +return 0 ; +} + +int vpu_set_last_buffer_dequeued (struct vpu_inst *inst) +{ +struct vb2_queue *q; + +if (!inst || !inst->fh.m2m_ctx) +return -EINVAL ; + + q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +if (!list_empty (&q->done_list)) +return -EINVAL ; + +vpu_trace (inst->dev,"last buffer dequeued\n" ); + q->last_buffer_dequeued = true; +wake_up (&q->done_wq); +vpu_notify_eos (inst); +return 0 ; +} + +const struct vpu_format *vpu_try_fmt_common (struct vpu_inst *inst, +struct v4l2_format *f) +{ +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +u32 type = f->type; +u32 stride =1 ; +u32 bytesperline; +u32 sizeimage; +const struct vpu_format *fmt; +const struct vpu_core_resources *res; +int i; + + fmt =vpu_helper_find_format (inst, type, pixmp->pixelformat); +if (!fmt) { + fmt =vpu_helper_enum_format (inst, type,0 ); +if (!fmt) +return NULL ; + pixmp->pixelformat = fmt->pixfmt; + } + + res =vpu_get_resource (inst); +if (res) + stride = res->stride; +if (pixmp->width) + pixmp->width =vpu_helper_valid_frame_width (inst, pixmp->width); +if (pixmp->height) + pixmp->height =vpu_helper_valid_frame_height (inst, pixmp->height); + pixmp->flags = fmt->flags; + pixmp->num_planes = fmt->num_planes; +if (pixmp->field ==V4L2_FIELD_ANY ) + pixmp->field =V4L2_FIELD_NONE ; +for (i =0 ; i < pixmp->num_planes; i++) { + bytesperline =max_t (s32, pixmp->plane_fmt[i].bytesperline,0 ); + sizeimage =vpu_helper_get_plane_size (pixmp->pixelformat, + pixmp->width, pixmp->height, i, stride, + pixmp->field ==V4L2_FIELD_INTERLACED ?1 :0 , + &bytesperline); + sizeimage =max_t (s32, pixmp->plane_fmt[i].sizeimage, sizeimage); + pixmp->plane_fmt[i].bytesperline = bytesperline; + pixmp->plane_fmt[i].sizeimage = sizeimage; + } + +return fmt; +} + +static bool vpu_check_ready (struct vpu_inst *inst,u32 type) +{ +if (!inst) +return false; +if (inst->state ==VPU_CODEC_STATE_DEINIT || inst->id <0 ) +return false; +if (!inst->ops->check_ready) +return true; +return call_vop (inst, check_ready, type); +} + +int vpu_process_output_buffer (struct vpu_inst *inst) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vpu_vb2_buffer *vpu_buf =NULL ; + +if (!inst) +return -EINVAL ; + +if (!vpu_check_ready (inst, inst->out_format.type)) +return -EINVAL ; + +v4l2_m2m_for_each_src_buf (inst->fh.m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); +if (vpu_buf->state ==VPU_BUF_STATE_IDLE ) +break ; + vpu_buf =NULL ; + } + +if (!vpu_buf) +return -EINVAL ; + +dev_dbg (inst->dev,"[%d]frame id = %d / %d\n" , + inst->id, vpu_buf->m2m_buf.vb.sequence, inst->sequence); +return call_vop (inst, process_output, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +int vpu_process_capture_buffer (struct vpu_inst *inst) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vpu_vb2_buffer *vpu_buf =NULL ; + +if (!inst) +return -EINVAL ; + +if (!vpu_check_ready (inst, inst->cap_format.type)) +return -EINVAL ; + +v4l2_m2m_for_each_dst_buf (inst->fh.m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); +if (vpu_buf->state ==VPU_BUF_STATE_IDLE ) +break ; + vpu_buf =NULL ; + } +if (!vpu_buf) +return -EINVAL ; + +return call_vop (inst, process_capture, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence (struct vpu_inst *inst, +u32 type,u32 sequence) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vb2_v4l2_buffer *vbuf =NULL ; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +v4l2_m2m_for_each_src_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->sequence == sequence) +break ; + vbuf =NULL ; + } + }else { +v4l2_m2m_for_each_dst_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->sequence == sequence) +break ; + vbuf =NULL ; + } + } + +return vbuf; +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_idx (struct vpu_inst *inst, +u32 type,u32 idx) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vb2_v4l2_buffer *vbuf =NULL ; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +v4l2_m2m_for_each_src_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->vb2_buf.index == idx) +break ; + vbuf =NULL ; + } + }else { +v4l2_m2m_for_each_dst_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->vb2_buf.index == idx) +break ; + vbuf =NULL ; + } + } + +return vbuf; +} + +int vpu_get_num_buffers (struct vpu_inst *inst,u32 type) +{ +struct vb2_queue *q; + +if (!inst || !inst->fh.m2m_ctx) +return -EINVAL ; +if (V4L2_TYPE_IS_OUTPUT (type)) + q =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +else + q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + +return q->num_buffers; +} + +static void vpu_m2m_device_run (void *priv) +{ +} + +static void vpu_m2m_job_abort (void *priv) +{ +struct vpu_inst *inst = priv; +struct v4l2_m2m_ctx *m2m_ctx = inst->fh.m2m_ctx; + +v4l2_m2m_job_finish (m2m_ctx->m2m_dev, m2m_ctx); +} + +static const struct v4l2_m2m_ops vpu_m2m_ops = { + .device_run = vpu_m2m_device_run, + .job_abort = vpu_m2m_job_abort +}; + +static int vpu_vb2_queue_setup (struct vb2_queue *vq, +unsigned int *buf_count, +unsigned int *plane_count, +unsigned int psize[], +struct device *allocators[]) +{ +struct vpu_inst *inst =vb2_get_drv_priv (vq); +struct vpu_format *cur_fmt; +int i; + + cur_fmt =vpu_get_format (inst, vq->type); + +if (*plane_count) { +if (inst->state ==VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE ) { +for (i =0 ; i < *plane_count; i++) { +if (!psize[i]) + psize[i] = cur_fmt->sizeimage[i]; + } +return 0 ; + } +if (*plane_count != cur_fmt->num_planes) +return -EINVAL ; +for (i =0 ; i < cur_fmt->num_planes; i++) { +if (psize[i] < cur_fmt->sizeimage[i]) +return -EINVAL ; + } +return 0 ; + } + + *plane_count = cur_fmt->num_planes; +for (i =0 ; i < cur_fmt->num_planes; i++) + psize[i] = cur_fmt->sizeimage[i]; + +return 0 ; +} + +static int vpu_vb2_buf_init (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb);
drivers/media/platform/amphion/vpu_v4l2.c line 342
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + + vpu_buf->state =VPU_BUF_STATE_IDLE ; + +return 0 ; +} + +static void vpu_vb2_buf_cleanup (struct vb2_buffer *vb) +{ +}
Unless this is filled in in a later patch, you can just drop this.
Quoted 125 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +125
-0+ +static int vpu_vb2_buf_prepare (struct vb2_buffer *vb) +{ +struct vpu_inst *inst =vb2_get_drv_priv (vb->vb2_queue); +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); +struct vpu_format *cur_fmt; +u32 i; + + cur_fmt =vpu_get_format (inst, vb->type); +if (vb->num_planes != cur_fmt->num_planes) +return -EINVAL ; +for (i =0 ; i < cur_fmt->num_planes; i++) { +if (vpu_get_vb_length (vb, i) < cur_fmt->sizeimage[i]) { +dev_dbg (inst->dev,"[%d] %s buf[%d] is invalid\n" , + inst->id, +vpu_type_name (vb->type), + vb->index); + vpu_buf->state =VPU_BUF_STATE_ERROR ; + } + } + +return 0 ; +} + +static void vpu_vb2_buf_finish (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_inst *inst =vb2_get_drv_priv (vb->vb2_queue); +struct vb2_queue *q = vb->vb2_queue; + +if (vbuf->flags &V4L2_BUF_FLAG_LAST ) +vpu_notify_eos (inst); + +if (list_empty (&q->done_list)) +call_vop (inst, on_queue_empty, q->type); +} + +void vpu_vb2_buffers_return (struct vpu_inst *inst, +unsigned int type,enum vb2_buffer_state state) +{ +struct vb2_v4l2_buffer *buf; + +if (!inst || !inst->fh.m2m_ctx) +return ; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +while ((buf =v4l2_m2m_src_buf_remove (inst->fh.m2m_ctx))) +v4l2_m2m_buf_done (buf, state); + }else { +while ((buf =v4l2_m2m_dst_buf_remove (inst->fh.m2m_ctx))) +v4l2_m2m_buf_done (buf, state); + } +} + +static int vpu_vb2_start_streaming (struct vb2_queue *q,unsigned int count) +{ +struct vpu_inst *inst =vb2_get_drv_priv (q); +struct vpu_format *fmt =vpu_get_format (inst, q->type); +int ret; + +vpu_inst_unlock (inst); + ret =vpu_inst_register (inst); +vpu_inst_lock (inst); +if (ret) { +vpu_vb2_buffers_return (inst, q->type,VB2_BUF_STATE_QUEUED ); +return ret; + } + +vpu_trace (inst->dev,"[%d] %s %c%c%c%c %dx%d %u(%u) %u(%u) %u(%u) %d\n" , + inst->id,vpu_type_name (q->type), + fmt->pixfmt, + fmt->pixfmt >>8 , + fmt->pixfmt >>16 , + fmt->pixfmt >>24 , + fmt->width, fmt->height, + fmt->sizeimage[0 ], fmt->bytesperline[0 ], + fmt->sizeimage[1 ], fmt->bytesperline[1 ], + fmt->sizeimage[2 ], fmt->bytesperline[2 ], + q->num_buffers); +call_vop (inst, start, q->type); +vb2_clear_last_buffer_dequeued (q); + +return 0 ; +} + +static void vpu_vb2_stop_streaming (struct vb2_queue *q) +{ +struct vpu_inst *inst =vb2_get_drv_priv (q); + +vpu_trace (inst->dev,"[%d] %s\n" , inst->id,vpu_type_name (q->type)); + +call_vop (inst, stop, q->type); +vpu_vb2_buffers_return (inst, q->type,VB2_BUF_STATE_ERROR ); +if (V4L2_TYPE_IS_OUTPUT (q->type)) + inst->sequence =0 ; +} + +static void vpu_vb2_buf_queue (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_inst *inst =vb2_get_drv_priv (vb->vb2_queue); + +if (V4L2_TYPE_IS_OUTPUT (vb->type)) { + vbuf->sequence = inst->sequence++; +if ((s64 )vb->timestamp <0 ) + vb->timestamp =VPU_INVALID_TIMESTAMP ; + } + +v4l2_m2m_buf_queue (inst->fh.m2m_ctx, vbuf); +vpu_process_output_buffer (inst); +vpu_process_capture_buffer (inst); +} + +static struct vb2_ops vpu_vb2_ops = { + .queue_setup = vpu_vb2_queue_setup, + .buf_init = vpu_vb2_buf_init, + .buf_cleanup = vpu_vb2_buf_cleanup, + .buf_prepare = vpu_vb2_buf_prepare, + .buf_finish = vpu_vb2_buf_finish, + .start_streaming = vpu_vb2_start_streaming, + .stop_streaming = vpu_vb2_stop_streaming, + .buf_queue = vpu_vb2_buf_queue, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish,
drivers/media/platform/amphion/vpu_v4l2.c line 477
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+}; + +static int vpu_m2m_queue_init (void *priv,struct vb2_queue *src_vq, +struct vb2_queue *dst_vq) +{ +struct vpu_inst *inst = priv; +int ret; + + inst->out_format.type = src_vq->type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + src_vq->io_modes =VB2_MMAP |VB2_USERPTR |VB2_DMABUF ;
I would drop VB2_USERPTR. Not desired for new drivers unless there is a really good reason. Ditto for dst_vq below.
drivers/media/platform/amphion/vpu_v4l2.c line 485
Quoted 8 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +8
-0+ src_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + src_vq->ops = &vpu_vb2_ops; + src_vq->mem_ops = &vb2_dma_contig_memops; +if (inst->type ==VPU_CORE_TYPE_DEC && inst->use_stream_buffer) + src_vq->mem_ops = &vb2_vmalloc_memops; + src_vq->drv_priv = inst; + src_vq->buf_struct_size =sizeof (struct vpu_vb2_buffer ); + src_vq->allow_zero_bytesused =1 ;
Do you need this? Unless you have a really good reason for this, I would drop this. Same for dst_vq.
Quoted 73 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +73
-0+ src_vq->min_buffers_needed =1 ; + src_vq->dev = inst->vpu->dev; + src_vq->lock = &inst->lock; + ret =vb2_queue_init (src_vq); +if (ret) +return ret; + + inst->cap_format.type = dst_vq->type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + dst_vq->io_modes =VB2_MMAP |VB2_USERPTR |VB2_DMABUF ; + dst_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + dst_vq->ops = &vpu_vb2_ops; + dst_vq->mem_ops = &vb2_dma_contig_memops; +if (inst->type ==VPU_CORE_TYPE_ENC && inst->use_stream_buffer) + dst_vq->mem_ops = &vb2_vmalloc_memops; + dst_vq->drv_priv = inst; + dst_vq->buf_struct_size =sizeof (struct vpu_vb2_buffer ); + dst_vq->allow_zero_bytesused =1 ; + dst_vq->min_buffers_needed =1 ; + dst_vq->dev = inst->vpu->dev; + dst_vq->lock = &inst->lock; + ret =vb2_queue_init (dst_vq); +if (ret) { +vb2_queue_release (src_vq); +return ret; + } + +return 0 ; +} + +static int vpu_v4l2_release (struct vpu_inst *inst) +{ +vpu_trace (inst->vpu->dev,"%p\n" , inst); + +vpu_release_core (inst->core); +put_device (inst->dev); + +if (inst->workqueue) { +cancel_work_sync (&inst->msg_work); +destroy_workqueue (inst->workqueue); + inst->workqueue =NULL ; + } +if (inst->fh.m2m_ctx) { +v4l2_m2m_ctx_release (inst->fh.m2m_ctx); + inst->fh.m2m_ctx =NULL ; + } + +v4l2_ctrl_handler_free (&inst->ctrl_handler); +mutex_destroy (&inst->lock); +v4l2_fh_del (&inst->fh); +v4l2_fh_exit (&inst->fh); + +call_vop (inst, cleanup); + +return 0 ; +} + +int vpu_v4l2_open (struct file *file,struct vpu_inst *inst) +{ +struct vpu_dev *vpu =video_drvdata (file); +struct vpu_func *func; +int ret =0 ; + +WARN_ON (!file || !inst || !inst->ops); + +if (inst->type ==VPU_CORE_TYPE_ENC ) + func = &vpu->encoder; +else + func = &vpu->decoder; + +atomic_set (&inst->ref_count,0 ); +vpu_inst_get (inst); + inst->vpu = vpu; + inst->core =vpu_request_core (vpu, inst->type);
drivers/media/platform/amphion/vpu_v4l2.c line 568
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+if (inst->core) + inst->dev =get_device (inst->core->dev); +mutex_init (&inst->lock); +INIT_LIST_HEAD (&inst->cmd_q); + inst->id =VPU_INST_NULL_ID ; + inst->release = vpu_v4l2_release; + inst->pid = current->pid; + inst->tgid = current->tgid; + inst->min_buffer_cap =2 ; + inst->min_buffer_out =2 ;
Assuming this means the minimum number of buffers needed, why is min_buffers_needed set to 1 when initializing the vb2_queue structs?
Quoted 44 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +44
-0+v4l2_fh_init (&inst->fh, func->vfd); +v4l2_fh_add (&inst->fh); + + ret =call_vop (inst, ctrl_init); +if (ret) + goto error; + + inst->fh.m2m_ctx =v4l2_m2m_ctx_init (func->m2m_dev, + inst, vpu_m2m_queue_init); +if (IS_ERR (inst->fh.m2m_ctx)) { +dev_err (vpu->dev,"v4l2_m2m_ctx_init fail\n" ); + ret =PTR_ERR (func->m2m_dev); + goto error; + } + + inst->fh.ctrl_handler = &inst->ctrl_handler; + file->private_data = &inst->fh; + inst->state =VPU_CODEC_STATE_DEINIT ; + inst->workqueue =alloc_workqueue ("vpu_inst" ,WQ_UNBOUND |WQ_MEM_RECLAIM ,1 ); +if (inst->workqueue) { +INIT_WORK (&inst->msg_work, vpu_inst_run_work); + ret =kfifo_init (&inst->msg_fifo, + inst->msg_buffer, +roundup_pow_of_two (sizeof (inst->msg_buffer))); +if (ret) { +destroy_workqueue (inst->workqueue); + inst->workqueue =NULL ; + } + } +vpu_trace (vpu->dev,"tgid = %d, pid = %d, type = %s, inst = %p\n" , + inst->tgid, inst->pid,vpu_core_type_desc (inst->type), inst); + +return 0 ; +error: +vpu_inst_put (inst); +return ret; +} + +int vpu_v4l2_close (struct file *file) +{ +struct vpu_dev *vpu =video_drvdata (file); +struct vpu_inst *inst =to_inst (file); +struct vb2_queue *src_q; +struct vb2_queue *dst_q;
drivers/media/platform/amphion/vpu_v4l2.c line 622
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+ +vpu_trace (vpu->dev,"tgid = %d, pid = %d, inst = %p\n" , + inst->tgid, inst->pid, inst); + src_q =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); + dst_q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +vpu_inst_lock (inst); +if (vb2_is_streaming (src_q)) +v4l2_m2m_streamoff (file, inst->fh.m2m_ctx, src_q->type); +if (vb2_is_streaming (dst_q)) +v4l2_m2m_streamoff (file, inst->fh.m2m_ctx, dst_q->type);
This looks very wrong. I expect a call to v4l2_m2m_ctx_release() here, and that will take care of any streaming.
Quoted 37 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +37
-0+vpu_inst_unlock (inst); + +call_vop (inst, release); +vpu_inst_unregister (inst); +vpu_inst_put (inst); + +return 0 ; +} + +int vpu_add_func (struct vpu_dev *vpu,struct vpu_func *func) +{ +struct video_device *vfd; +int ret; + +if (!vpu || !func) +return -EINVAL ; + +if (func->vfd) +return 0 ; + + vfd =video_device_alloc (); +if (!vfd) { +dev_err (vpu->dev,"alloc vpu decoder video device fail\n" ); +return -ENOMEM ; + } + vfd->release = video_device_release; + vfd->vfl_dir =VFL_DIR_M2M ; + vfd->v4l2_dev = &vpu->v4l2_dev; + vfd->device_caps =V4L2_CAP_VIDEO_M2M_MPLANE |V4L2_CAP_STREAMING ; +if (func->type ==VPU_CORE_TYPE_ENC ) { +strscpy (vfd->name,"amphion-vpu-encoder" ,sizeof (vfd->name)); + vfd->fops =venc_get_fops (); + vfd->ioctl_ops =venc_get_ioctl_ops (); + }else { +strscpy (vfd->name,"amphion-vpu-decoder" ,sizeof (vfd->name)); + vfd->fops =vdec_get_fops (); + vfd->ioctl_ops =vdec_get_ioctl_ops ();
drivers/media/platform/amphion/vpu_v4l2.c line 669
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+ } + + ret =video_register_device (vfd,VFL_TYPE_VIDEO ,-1 ); +if (ret) { +video_device_release (vfd); +return ret; + } +video_set_drvdata (vfd, vpu); + func->vfd = vfd; + func->m2m_dev =v4l2_m2m_init (&vpu_m2m_ops);
This should be done before the video_register_device to avoid creating device nodes while the device isn't fully initialized yet.
Quoted 85 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +34
-0+if (IS_ERR (func->m2m_dev)) { +dev_err (vpu->dev,"v4l2_m2m_init fail\n" ); +video_unregister_device (func->vfd); + func->vfd =NULL ; +return PTR_ERR (func->m2m_dev); + } + + ret =v4l2_m2m_register_media_controller (func->m2m_dev, func->vfd, func->function); +if (ret) { +v4l2_m2m_release (func->m2m_dev); + func->m2m_dev =NULL ; +video_unregister_device (func->vfd); + func->vfd =NULL ; +return ret; + } + +return 0 ; +} + +void vpu_remove_func (struct vpu_func *func) +{ +if (!func) +return ; + +if (func->m2m_dev) { +v4l2_m2m_unregister_media_controller (func->m2m_dev); +v4l2_m2m_release (func->m2m_dev); + func->m2m_dev =NULL ; + } +if (func->vfd) { +video_unregister_device (func->vfd); + func->vfd =NULL ; + } +}drivers/media/platform/amphion/vpu_v4l2.h new file +44
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_V4L2_H +#define _AMPHION_VPU_V4L2_H + +#include <linux/videodev2.h> + +void vpu_inst_lock (struct vpu_inst *inst); +void vpu_inst_unlock (struct vpu_inst *inst); + +int vpu_v4l2_open (struct file *file,struct vpu_inst *inst); +int vpu_v4l2_close (struct file *file); + +const struct vpu_format *vpu_try_fmt_common (struct vpu_inst *inst,struct v4l2_format *f); +int vpu_process_output_buffer (struct vpu_inst *inst); +int vpu_process_capture_buffer (struct vpu_inst *inst); +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence (struct vpu_inst *inst,u32 type,u32 sequence); +struct vb2_v4l2_buffer *vpu_find_buf_by_idx (struct vpu_inst *inst,u32 type,u32 idx); +void vpu_v4l2_set_error (struct vpu_inst *inst); +int vpu_notify_eos (struct vpu_inst *inst); +int vpu_notify_source_change (struct vpu_inst *inst); +int vpu_set_last_buffer_dequeued (struct vpu_inst *inst); +void vpu_vb2_buffers_return (struct vpu_inst *inst, +unsigned int type,enum vb2_buffer_state state); +int vpu_get_num_buffers (struct vpu_inst *inst,u32 type); + +dma_addr_t vpu_get_vb_phy_addr (struct vb2_buffer *vb,u32 plane_no); +unsigned int vpu_get_vb_length (struct vb2_buffer *vb,u32 plane_no); +static inline struct vpu_format *vpu_get_format (struct vpu_inst *inst,u32 type) +{ +if (V4L2_TYPE_IS_OUTPUT (type)) +return &inst->out_format; +else +return &inst->cap_format; +} + +static inline char *vpu_type_name (u32 type) +{ +return V4L2_TYPE_IS_OUTPUT (type) ?"output" :"capture" ; +} +
drivers/media/platform/amphion/vpu_v4l2.h line 54
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.h new file +10
-0+static inline int vpu_vb_is_codecconfig (struct vb2_v4l2_buffer *vbuf) +{ +#ifdef V4L2_BUF_FLAG_CODECCONFIG +return (vbuf->flags &V4L2_BUF_FLAG_CODECCONFIG ) ?1 :0 ; +#else +return 0 ; +#endif +} + +#endif
Regards,
Hans
Ming Qian
maintainerRE: [EXT] Re: [PATCH v13 06/13] media: amphion: add vpu v4l2 m2m support
Quoted 23 lines
Quoted 23 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +23
-0+ +int vpu_v4l2_open (struct file *file,struct vpu_inst *inst) +{ +struct vpu_dev *vpu =video_drvdata (file); +struct vpu_func *func; +int ret =0 ; + +WARN_ON (!file || !inst || !inst->ops); + +if (inst->type ==VPU_CORE_TYPE_ENC ) + func = &vpu->encoder; +else + func = &vpu->decoder; + +atomic_set (&inst->ref_count,0 ); +vpu_inst_get (inst); + inst->vpu = vpu; + inst->core =vpu_request_core (vpu, inst->type); +if (inst->core) + inst->dev =get_device (inst->core->dev); +mutex_init (&inst->lock); +INIT_LIST_HEAD (&inst->cmd_q); + inst->id =VPU_INST_NULL_ID ;
Quoted 8 lines
drivers/media/platform/amphion/vpu_v4l2.c line 568
Quoted 5 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +5
-0+ inst->release = vpu_v4l2_release; + inst->pid = current->pid; + inst->tgid = current->tgid; + inst->min_buffer_cap =2 ; + inst->min_buffer_out =2 ;Assuming this means the minimum number of buffers needed, why is
min_buffers_neededset to 1 when initializing thevb2_queuestructs?
In my opinion, the min_buffers_needed determine when vb2_start_streaming will be called, Like the following code:
if (q->queued_count >= q->min_buffers_needed) {
... ...
ret = vb2_start_streaming(q);
... ...
}I hope driver starts a vpu instance when 1 frame is queued, so I set the min_buffers_needed to 1. And the min_buffer_cap means the minimum vb2 buffer count, and it will changed according to the stream, I just set default value to 2, it will be changed after vpu parsed the stream info.
Quoted 42 lines
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +9
-0+v4l2_fh_init (&inst->fh, func->vfd); +v4l2_fh_add (&inst->fh); + + ret =call_vop (inst, ctrl_init); +if (ret) + goto error; + + inst->fh.m2m_ctx =v4l2_m2m_ctx_init (func->m2m_dev, + inst, vpu_m2m_queue_init);drivers/media/platform/amphion/vpu_v4l2.c
Quoted 10 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +10
-0+if (IS_ERR (inst->fh.m2m_ctx)) { +dev_err (vpu->dev,"v4l2_m2m_ctx_init fail\n" ); + ret =PTR_ERR (func->m2m_dev); + goto error; + } + + inst->fh.ctrl_handler = &inst->ctrl_handler; + file->private_data = &inst->fh; + inst->state =VPU_CODEC_STATE_DEINIT ; + inst->workqueue = alloc_workqueue("vpu_inst", WQ_UNBOUND |
WQ_MEM_RECLAIM, 1);drivers/media/platform/amphion/vpu_v4l2.c line 600
Quoted 5 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +5
-0+if (inst->workqueue) { +INIT_WORK (&inst->msg_work, vpu_inst_run_work); + ret = kfifo_init(&inst->msg_fifo, + inst->msg_buffer, +roundup_pow_of_two(sizeof(inst->msg_buffer)));
drivers/media/platform/amphion/vpu_v4l2.c
Quoted 7 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +7
-0+if (ret) { +destroy_workqueue (inst->workqueue); + inst->workqueue =NULL ; + } + } + vpu_trace(vpu->dev, "tgid = %d, pid = %d, type = %s, inst = %p\n", + inst->tgid, inst->pid,
vpu_core_type_desc(inst->type), inst);Quoted 7 lines
drivers/media/platform/amphion/vpu_v4l2.c new file +7
-0+ +return 0 ; +error: +vpu_inst_put (inst); +return ret; +} +
Re: [PATCH v13 06/13] media: amphion: add vpu v4l2 m2m support
In reply to Ming QianLe mardi 30 novembre 2021 à 17:48 +0800, Ming Qian a écrit :
Quoted 363 lines
vpu_v4l2.cimplements the v4l2 m2m driver methods.vpu_helpers.cimplements the common helper functionsvpu_color.cconverts the v4l2 colorspace with isoSigned-off-by Ming Qian <[email protected]>Signed-off-by Shijie Qin <[email protected]>Signed-off-by Zhou Peng <[email protected]>Reported-by kernel test robot <[email protected]>--- drivers/media/platform/amphion/vpu_color.c | 190 +++++ drivers/media/platform/amphion/vpu_helpers.c | 436 ++++++++++++ drivers/media/platform/amphion/vpu_helpers.h | 71 ++ drivers/media/platform/amphion/vpu_v4l2.c | 703 +++++++++++++++++++ drivers/media/platform/amphion/vpu_v4l2.h | 54 ++ 5 files changed, 1454 insertions(+) create mode 100644 drivers/media/platform/amphion/vpu_color.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.c create mode 100644 drivers/media/platform/amphion/vpu_helpers.h create mode 100644 drivers/media/platform/amphion/vpu_v4l2.c create mode 100644 drivers/media/platform/amphion/vpu_v4l2.hdrivers/media/platform/amphion/vpu_color.c new file +190
-0@@ -0,0 +1,190 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/device.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/slab.h> +#include <linux/delay.h> +#include <linux/types.h> +#include <media/v4l2-device.h> +#include "vpu.h" +#include "vpu_helpers.h" + +static const u8 colorprimaries[] = { +0 , +V4L2_COLORSPACE_REC709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +V4L2_COLORSPACE_470_SYSTEM_M ,/*Rec. ITU-R BT.470-6 System M*/ +V4L2_COLORSPACE_470_SYSTEM_BG ,/*Rec. ITU-R BT.470-6 System B, G*/ +V4L2_COLORSPACE_SMPTE170M ,/*SMPTE170M*/ +V4L2_COLORSPACE_SMPTE240M ,/*SMPTE240M*/ +0 ,/*Generic film*/ +V4L2_COLORSPACE_BT2020 ,/*Rec. ITU-R BT.2020-2*/ +0 ,/*SMPTE ST 428-1*/ +}; + +static const u8 colortransfers[] = { +0 , +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +0 ,/*Rec. ITU-R BT.470-6 System M*/ +0 ,/*Rec. ITU-R BT.470-6 System B, G*/ +V4L2_XFER_FUNC_709 ,/*SMPTE170M*/ +V4L2_XFER_FUNC_SMPTE240M ,/*SMPTE240M*/ +V4L2_XFER_FUNC_NONE ,/*Linear transfer characteristics*/ +0 , +0 , +0 ,/*IEC 61966-2-4*/ +0 ,/*Rec. ITU-R BT.1361-0 extended colour gamut*/ +V4L2_XFER_FUNC_SRGB ,/*IEC 61966-2-1 sRGB or sYCC*/ +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.2020-2 (10 bit system)*/ +V4L2_XFER_FUNC_709 ,/*Rec. ITU-R BT.2020-2 (12 bit system)*/ +V4L2_XFER_FUNC_SMPTE2084 ,/*SMPTE ST 2084*/ +0 ,/*SMPTE ST 428-1*/ +0 /*Rec. ITU-R BT.2100-0 hybrid log-gamma (HLG)*/ +}; + +static const u8 colormatrixcoefs[] = { +0 , +V4L2_YCBCR_ENC_709 ,/*Rec. ITU-R BT.709-6*/ +0 , +0 , +0 ,/*Title 47 Code of Federal Regulations*/ +V4L2_YCBCR_ENC_601 ,/*Rec. ITU-R BT.601-7 625*/ +V4L2_YCBCR_ENC_601 ,/*Rec. ITU-R BT.601-7 525*/ +V4L2_YCBCR_ENC_SMPTE240M ,/*SMPTE240M*/ +0 , +V4L2_YCBCR_ENC_BT2020 ,/*Rec. ITU-R BT.2020-2*/ +V4L2_YCBCR_ENC_BT2020_CONST_LUM /*Rec. ITU-R BT.2020-2 constant*/ +}; + +u32 vpu_color_cvrt_primaries_v2i (u32 primaries) +{ +return VPU_ARRAY_FIND (colorprimaries, primaries); +} + +u32 vpu_color_cvrt_primaries_i2v (u32 primaries) +{ +return VPU_ARRAY_AT (colorprimaries, primaries); +} + +u32 vpu_color_cvrt_transfers_v2i (u32 transfers) +{ +return VPU_ARRAY_FIND (colortransfers, transfers); +} + +u32 vpu_color_cvrt_transfers_i2v (u32 transfers) +{ +return VPU_ARRAY_AT (colortransfers, transfers); +} + +u32 vpu_color_cvrt_matrix_v2i (u32 matrix) +{ +return VPU_ARRAY_FIND (colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_matrix_i2v (u32 matrix) +{ +return VPU_ARRAY_AT (colormatrixcoefs, matrix); +} + +u32 vpu_color_cvrt_full_range_v2i (u32 full_range) +{ +return (full_range ==V4L2_QUANTIZATION_FULL_RANGE ); +} + +u32 vpu_color_cvrt_full_range_i2v (u32 full_range) +{ +if (full_range) +return V4L2_QUANTIZATION_FULL_RANGE ; + +return V4L2_QUANTIZATION_LIM_RANGE ; +} + +int vpu_color_check_primaries (u32 primaries) +{ +return vpu_color_cvrt_primaries_v2i (primaries) ?0 : -EINVAL ; +} + +int vpu_color_check_transfers (u32 transfers) +{ +return vpu_color_cvrt_transfers_v2i (transfers) ?0 : -EINVAL ; +} + +int vpu_color_check_matrix (u32 matrix) +{ +return vpu_color_cvrt_matrix_v2i (matrix) ?0 : -EINVAL ; +} + +int vpu_color_check_full_range (u32 full_range) +{ +int ret = -EINVAL ; + +switch (full_range) { +case V4L2_QUANTIZATION_FULL_RANGE : +case V4L2_QUANTIZATION_LIM_RANGE : + ret =0 ; +break ; +default : +break ; + + } + +return ret; +} + +int vpu_color_get_default (u32 primaries, +u32 *ptransfers,u32 *pmatrix,u32 *pfull_range) +{ +u32 transfers; +u32 matrix; +u32 full_range; + +switch (primaries) { +case V4L2_COLORSPACE_REC709 : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_709 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_470_SYSTEM_M : +case V4L2_COLORSPACE_470_SYSTEM_BG : +case V4L2_COLORSPACE_SMPTE170M : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_601 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_SMPTE240M : + transfers =V4L2_XFER_FUNC_SMPTE240M ; + matrix =V4L2_YCBCR_ENC_SMPTE240M ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +case V4L2_COLORSPACE_BT2020 : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_BT2020 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; +default : + transfers =V4L2_XFER_FUNC_709 ; + matrix =V4L2_YCBCR_ENC_709 ; + full_range =V4L2_QUANTIZATION_LIM_RANGE ; +break ; + } + +if (ptransfers) + *ptransfers = transfers; +if (pmatrix) + *pmatrix = matrix; +if (pfull_range) + *pfull_range = full_range; + + +return 0 ; +}drivers/media/platform/amphion/vpu_helpers.c new file +140
-0@@ -0,0 +1,436 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_rpc.h" +#include "vpu_helpers.h" + +int vpu_helper_find_in_array_u8 (const u8 *array,u32 size,u32 x) +{ +int i; + +for (i =0 ; i < size; i++) { +if (array[i] == x) +return i; + } + +return 0 ; +} + +bool vpu_helper_check_type (struct vpu_inst *inst,u32 type) +{ +const struct vpu_format *pfmt; + +for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { +if (!vpu_iface_check_format (inst, pfmt->pixfmt)) +continue ; +if (pfmt->type == type) +return true; + } + +return false; +} + +const struct vpu_format *vpu_helper_find_format (struct vpu_inst *inst,u32 type,u32 pixelfmt) +{ +const struct vpu_format *pfmt; + +if (!inst || !inst->formats) +return NULL ; + +if (!vpu_iface_check_format (inst, pixelfmt)) +return NULL ; + +for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { +if (pfmt->pixfmt == pixelfmt && (!type || type == pfmt->type)) +return pfmt; + } + +return NULL ; +} + +const struct vpu_format *vpu_helper_enum_format (struct vpu_inst *inst,u32 type,int index) +{ +const struct vpu_format *pfmt; +int i =0 ; + +if (!inst || !inst->formats) +return NULL ; + +for (pfmt = inst->formats; pfmt->pixfmt; pfmt++) { +if (!vpu_iface_check_format (inst, pfmt->pixfmt)) +continue ; + +if (pfmt->type == type) { +if (index == i) +return pfmt; + i++; + } + } + +return NULL ; +} + +u32 vpu_helper_valid_frame_width (struct vpu_inst *inst,u32 width) +{ +const struct vpu_core_resources *res; + +if (!inst) +return width; + + res =vpu_get_resource (inst); +if (!res) +return width; +if (res->max_width) + width =clamp (width, res->min_width, res->max_width); +if (res->step_width) + width =ALIGN (width, res->step_width); + +return width; +} + +u32 vpu_helper_valid_frame_height (struct vpu_inst *inst,u32 height) +{ +const struct vpu_core_resources *res; + +if (!inst) +return height; + + res =vpu_get_resource (inst); +if (!res) +return height; +if (res->max_height) + height =clamp (height, res->min_height, res->max_height); +if (res->step_height) + height =ALIGN (height, res->step_height); + +return height; +} + +static u32 get_nv12_plane_size (u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +u32 bytesperline; +u32 size =0 ; + + bytesperline =ALIGN (width, stride); +if (pbl) + bytesperline =max (bytesperline, *pbl); + height =ALIGN (height,2 ); +if (plane_no ==0 ) + size = bytesperline * height; +else if (plane_no ==1 ) + size = bytesperline * height >>1 ; +if (pbl) + *pbl = bytesperline; + +return size; +} + +static u32 get_tiled_8l128_plane_size(u32 fmt, u32 width, u32 height, int plane_no,
drivers/media/platform/amphion/vpu_helpers.c line 150
Quoted 10 lines
drivers/media/platform/amphion/vpu_helpers.c new file +10
-0+ u32 stride, u32 interlaced, u32 *pbl) +{ + u32 ws = 3; +u32 hs =7 ; +u32 bitdepth =8 ; +u32 bytesperline; +u32 size =0 ; + +if (interlaced) + hs++;
As discussed earlier, when producing tiled, this driver should negotiate V4L2_FIELD_ALTERNATE and ensure the sequence numbers for paired fields matches as per spec. The height being halved is common this this type of interlace output.
Note that this is a bit of a whole in GStreamer fluster integration (there is nothing to merge fields), you can probably just move NV12, which according to its frame size will be interleaved, by placing that format first in your enum_fmt implementation. Though it conflict with the intent to place first the most "native" format. I can also provide patch that will force NV12 in fluster for current mainline testing.
Quoted 1,132 lines
drivers/media/platform/amphion/vpu_helpers.c new file +286
-0+if (fmt ==V4L2_PIX_FMT_NV12MT_10BE_8L128 ) + bitdepth =10 ; + bytesperline =DIV_ROUND_UP (width * bitdepth,BITS_PER_BYTE ); + bytesperline =ALIGN (bytesperline,1 << ws); + bytesperline =ALIGN (bytesperline, stride); +if (pbl) + bytesperline =max (bytesperline, *pbl); + height =ALIGN (height,1 << hs); +if (plane_no ==0 ) + size = bytesperline * height; +else if (plane_no ==1 ) + size = (bytesperline *ALIGN (height,1 << (hs +1 ))) >>1 ; +if (pbl) + *pbl = bytesperline; + +return size; +} + +static u32 get_default_plane_size (u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +u32 bytesperline; +u32 size =0 ; + + bytesperline =ALIGN (width, stride); +if (pbl) + bytesperline =max (bytesperline, *pbl); +if (plane_no ==0 ) + size = bytesperline * height; +if (pbl) + *pbl = bytesperline; + +return size; +} + +u32 vpu_helper_get_plane_size (u32 fmt,u32 w,u32 h,int plane_no, +u32 stride,u32 interlaced,u32 *pbl) +{ +switch (fmt) { +case V4L2_PIX_FMT_NV12M : +return get_nv12_plane_size (w, h, plane_no, stride, interlaced, pbl); +case V4L2_PIX_FMT_NV12MT_8L128 : +case V4L2_PIX_FMT_NV12MT_10BE_8L128 : +return get_tiled_8l128_plane_size (fmt, w, h, plane_no, stride, interlaced, pbl); +default : +return get_default_plane_size (w, h, plane_no, stride, interlaced, pbl); + } +} + +u32 vpu_helper_copy_from_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *rptr,u32 size,void *dst) +{ +u32 offset; +u32 start; +u32 end; +void *virt; + +if (!stream_buffer || !rptr || !dst) +return -EINVAL ; + +if (!size) +return 0 ; + + offset = *rptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; + +if (offset < start || offset > end) +return -EINVAL ; + +if (offset + size <= end) { +memcpy (dst, virt + (offset - start), size); + }else { +memcpy (dst, virt + (offset - start), end - offset); +memcpy (dst + end - offset, virt, size + offset - end); + } + + *rptr =vpu_helper_step_walk (stream_buffer, offset, size); +return size; +} + +u32 vpu_helper_copy_to_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u32 size,void *src) +{ +u32 offset; +u32 start; +u32 end; +void *virt; + +if (!stream_buffer || !wptr || !src) +return -EINVAL ; + +if (!size) +return 0 ; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; +if (offset < start || offset > end) +return -EINVAL ; + +if (offset + size <= end) { +memcpy (virt + (offset - start), src, size); + }else { +memcpy (virt + (offset - start), src, end - offset); +memcpy (virt, src + end - offset, size + offset - end); + } + + *wptr =vpu_helper_step_walk (stream_buffer, offset, size); + +return size; +} + +u32 vpu_helper_memset_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u8 val,u32 size) +{ +u32 offset; +u32 start; +u32 end; +void *virt; + +if (!stream_buffer || !wptr) +return -EINVAL ; + +if (!size) +return 0 ; + + offset = *wptr; + start = stream_buffer->phys; + end = start + stream_buffer->length; + virt = stream_buffer->virt; +if (offset < start || offset > end) +return -EINVAL ; + +if (offset + size <= end) { +memset (virt + (offset - start), val, size); + }else { +memset (virt + (offset - start), val, end - offset); +memset (virt, val, size + offset - end); + } + + offset += size; +if (offset >= end) + offset -= stream_buffer->length; + + *wptr = offset; + +return size; +} + +u32 vpu_helper_get_free_space (struct vpu_inst *inst) +{ +struct vpu_rpc_buffer_desc desc; + +if (vpu_iface_get_stream_buffer_desc (inst, &desc)) +return 0 ; + +if (desc.rptr > desc.wptr) +return desc.rptr - desc.wptr; +else if (desc.rptr < desc.wptr) +return (desc.end - desc.start + desc.rptr - desc.wptr); +else +return desc.end - desc.start; +} + +u32 vpu_helper_get_used_space (struct vpu_inst *inst) +{ +struct vpu_rpc_buffer_desc desc; + +if (vpu_iface_get_stream_buffer_desc (inst, &desc)) +return 0 ; + +if (desc.wptr > desc.rptr) +return desc.wptr - desc.rptr; +else if (desc.wptr < desc.rptr) +return (desc.end - desc.start + desc.wptr - desc.rptr); +else +return 0 ; +} + +int vpu_helper_g_volatile_ctrl (struct v4l2_ctrl *ctrl) +{ +struct vpu_inst *inst =ctrl_to_inst (ctrl); + +switch (ctrl->id) { +case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE : + ctrl->val = inst->min_buffer_cap; +break ; +case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT : + ctrl->val = inst->min_buffer_out; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +u32 vpu_helper_calc_coprime (u32 *a,u32 *b) +{ +int m = *a; +int n = *b; + +if (m ==0 ) +return n; +if (n ==0 ) +return m; + +while (n !=0 ) { +int tmp = m % n; + + m = n; + n = tmp; + } + *a = (*a) / m; + *b = (*b) / m; + +return m; +} + +#define READ_BYTE (buffer, pos) (*(u8 *)((buffer)->virt + ((pos) % buffer->length))) +int vpu_helper_find_startcode (struct vpu_buffer *stream_buffer, +u32 pixelformat,u32 offset,u32 bytesused) +{ +u32 start_code; +int start_code_size; +u32 val =0 ; +int i; +int ret = -EINVAL ; + +if (!stream_buffer || !stream_buffer->virt) +return -EINVAL ; + +switch (pixelformat) { +case V4L2_PIX_FMT_H264 : + start_code_size =4 ; + start_code =0x00000001 ; +break ; +default : +return 0 ; + } + +for (i =0 ; i < bytesused; i++) { + val = (val <<8 ) |READ_BYTE (stream_buffer, offset + i); +if (i < start_code_size -1 ) +continue ; +if (val == start_code) { + ret = i +1 - start_code_size; +break ; + } + } + +return ret; +} + +int vpu_find_dst_by_src (struct vpu_pair *pairs,u32 cnt,u32 src) +{ +u32 i; + +if (!pairs || !cnt) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) { +if (pairs[i].src == src) +return pairs[i].dst; + } + +return -EINVAL ; +} + +int vpu_find_src_by_dst (struct vpu_pair *pairs,u32 cnt,u32 dst) +{ +u32 i; + +if (!pairs || !cnt) +return -EINVAL ; + +for (i =0 ; i < cnt; i++) { +if (pairs[i].dst == dst) +return pairs[i].src; + } + +return -EINVAL ; +}drivers/media/platform/amphion/vpu_helpers.h new file +71
-0@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_HELPERS_H +#define _AMPHION_VPU_HELPERS_H + +struct vpu_pair { +u32 src; +u32 dst; +}; + +#define MAKE_TIMESTAMP (s, ns) (((s32)(s) * NSEC_PER_SEC) + (ns)) +#define VPU_INVALID_TIMESTAMP MAKE_TIMESTAMP(-1, 0) +#define VPU_ARRAY_AT (array, i) (((i) < ARRAY_SIZE(array)) ? array[i] : 0) +#define VPU_ARRAY_FIND (array, x) vpu_helper_find_in_array_u8(array, ARRAY_SIZE(array), x) + +int vpu_helper_find_in_array_u8 (const u8 *array,u32 size,u32 x); +bool vpu_helper_check_type (struct vpu_inst *inst,u32 type); +const struct vpu_format *vpu_helper_find_format (struct vpu_inst *inst,u32 type,u32 pixelfmt); +const struct vpu_format *vpu_helper_enum_format (struct vpu_inst *inst,u32 type,int index); +u32 vpu_helper_valid_frame_width (struct vpu_inst *inst,u32 width); +u32 vpu_helper_valid_frame_height (struct vpu_inst *inst,u32 height); +u32 vpu_helper_get_plane_size (u32 fmt,u32 width,u32 height,int plane_no, +u32 stride,u32 interlaced,u32 *pbl); +u32 vpu_helper_copy_from_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *rptr,u32 size,void *dst); +u32 vpu_helper_copy_to_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u32 size,void *src); +u32 vpu_helper_memset_stream_buffer (struct vpu_buffer *stream_buffer, +u32 *wptr,u8 val,u32 size); +u32 vpu_helper_get_free_space (struct vpu_inst *inst); +u32 vpu_helper_get_used_space (struct vpu_inst *inst); +int vpu_helper_g_volatile_ctrl (struct v4l2_ctrl *ctrl); +u32 vpu_helper_calc_coprime (u32 *a,u32 *b); +void vpu_helper_get_kmp_next (const u8 *pattern,int *next,int size); +int vpu_helper_kmp_search (u8 *s,int s_len,const u8 *p,int p_len,int *next); +int vpu_helper_kmp_search_in_stream_buffer (struct vpu_buffer *stream_buffer, +u32 offset,int bytesused, +const u8 *p,int p_len,int *next); +int vpu_helper_find_startcode (struct vpu_buffer *stream_buffer, +u32 pixelformat,u32 offset,u32 bytesused); + +static inline u32 vpu_helper_step_walk (struct vpu_buffer *stream_buffer,u32 pos,u32 step) +{ + pos += step; +if (pos > stream_buffer->phys + stream_buffer->length) + pos -= stream_buffer->length; + +return pos; +} + +int vpu_color_check_primaries (u32 primaries); +int vpu_color_check_transfers (u32 transfers); +int vpu_color_check_matrix (u32 matrix); +int vpu_color_check_full_range (u32 full_range); +u32 vpu_color_cvrt_primaries_v2i (u32 primaries); +u32 vpu_color_cvrt_primaries_i2v (u32 primaries); +u32 vpu_color_cvrt_transfers_v2i (u32 transfers); +u32 vpu_color_cvrt_transfers_i2v (u32 transfers); +u32 vpu_color_cvrt_matrix_v2i (u32 matrix); +u32 vpu_color_cvrt_matrix_i2v (u32 matrix); +u32 vpu_color_cvrt_full_range_v2i (u32 full_range); +u32 vpu_color_cvrt_full_range_i2v (u32 full_range); +int vpu_color_get_default (u32 primaries, +u32 *ptransfers,u32 *pmatrix,u32 *pfull_range); + +int vpu_find_dst_by_src (struct vpu_pair *pairs,u32 cnt,u32 src); +int vpu_find_src_by_dst (struct vpu_pair *pairs,u32 cnt,u32 dst); +#endif drivers/media/platform/amphion/vpu_v4l2.c new file +703
-0@@ -0,0 +1,703 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/pm_runtime.h> +#include <linux/videodev2.h> +#include <media/v4l2-device.h> +#include <media/v4l2-event.h> +#include <media/v4l2-mem2mem.h> +#include <media/v4l2-ioctl.h> +#include <media/videobuf2-v4l2.h> +#include <media/videobuf2-dma-contig.h> +#include <media/videobuf2-vmalloc.h> +#include "vpu.h" +#include "vpu_core.h" +#include "vpu_v4l2.h" +#include "vpu_msgs.h" +#include "vpu_helpers.h" + +void vpu_inst_lock (struct vpu_inst *inst) +{ +mutex_lock (&inst->lock); +} + +void vpu_inst_unlock (struct vpu_inst *inst) +{ +mutex_unlock (&inst->lock); +} + +dma_addr_t vpu_get_vb_phy_addr (struct vb2_buffer *vb,u32 plane_no) +{ +if (plane_no >= vb->num_planes) +return 0 ; +return vb2_dma_contig_plane_dma_addr (vb, plane_no) + + vb->planes[plane_no].data_offset; +} + +unsigned int vpu_get_vb_length (struct vb2_buffer *vb,u32 plane_no) +{ +if (plane_no >= vb->num_planes) +return 0 ; +return vb2_plane_size (vb, plane_no) - vb->planes[plane_no].data_offset; +} + +void vpu_v4l2_set_error (struct vpu_inst *inst) +{ +struct vb2_queue *src_q =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +struct vb2_queue *dst_q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + +dev_err (inst->dev,"some error occurs in codec\n" ); +if (src_q) + src_q->error =1 ; +if (dst_q) + dst_q->error =1 ; +} + +int vpu_notify_eos (struct vpu_inst *inst) +{ +const struct v4l2_event ev = { + .id =0 , + .type =V4L2_EVENT_EOS + }; + +vpu_trace (inst->dev,"[%d]\n" , inst->id); +v4l2_event_queue_fh (&inst->fh, &ev); + +return 0 ; +} + +int vpu_notify_source_change (struct vpu_inst *inst) +{ +const struct v4l2_event ev = { + .id =0 , + .type =V4L2_EVENT_SOURCE_CHANGE , + .u.src_change.changes =V4L2_EVENT_SRC_CH_RESOLUTION + }; + +vpu_trace (inst->dev,"[%d]\n" , inst->id); +v4l2_event_queue_fh (&inst->fh, &ev); +return 0 ; +} + +int vpu_set_last_buffer_dequeued (struct vpu_inst *inst) +{ +struct vb2_queue *q; + +if (!inst || !inst->fh.m2m_ctx) +return -EINVAL ; + + q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +if (!list_empty (&q->done_list)) +return -EINVAL ; + +vpu_trace (inst->dev,"last buffer dequeued\n" ); + q->last_buffer_dequeued = true; +wake_up (&q->done_wq); +vpu_notify_eos (inst); +return 0 ; +} + +const struct vpu_format *vpu_try_fmt_common (struct vpu_inst *inst, +struct v4l2_format *f) +{ +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +u32 type = f->type; +u32 stride =1 ; +u32 bytesperline; +u32 sizeimage; +const struct vpu_format *fmt; +const struct vpu_core_resources *res; +int i; + + fmt =vpu_helper_find_format (inst, type, pixmp->pixelformat); +if (!fmt) { + fmt =vpu_helper_enum_format (inst, type,0 ); +if (!fmt) +return NULL ; + pixmp->pixelformat = fmt->pixfmt; + } + + res =vpu_get_resource (inst); +if (res) + stride = res->stride; +if (pixmp->width) + pixmp->width =vpu_helper_valid_frame_width (inst, pixmp->width); +if (pixmp->height) + pixmp->height =vpu_helper_valid_frame_height (inst, pixmp->height); + pixmp->flags = fmt->flags; + pixmp->num_planes = fmt->num_planes; +if (pixmp->field ==V4L2_FIELD_ANY ) + pixmp->field =V4L2_FIELD_NONE ; +for (i =0 ; i < pixmp->num_planes; i++) { + bytesperline =max_t (s32, pixmp->plane_fmt[i].bytesperline,0 ); + sizeimage =vpu_helper_get_plane_size (pixmp->pixelformat, + pixmp->width, pixmp->height, i, stride, + pixmp->field ==V4L2_FIELD_INTERLACED ?1 :0 , + &bytesperline); + sizeimage =max_t (s32, pixmp->plane_fmt[i].sizeimage, sizeimage); + pixmp->plane_fmt[i].bytesperline = bytesperline; + pixmp->plane_fmt[i].sizeimage = sizeimage; + } + +return fmt; +} + +static bool vpu_check_ready (struct vpu_inst *inst,u32 type) +{ +if (!inst) +return false; +if (inst->state ==VPU_CODEC_STATE_DEINIT || inst->id <0 ) +return false; +if (!inst->ops->check_ready) +return true; +return call_vop (inst, check_ready, type); +} + +int vpu_process_output_buffer (struct vpu_inst *inst) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vpu_vb2_buffer *vpu_buf =NULL ; + +if (!inst) +return -EINVAL ; + +if (!vpu_check_ready (inst, inst->out_format.type)) +return -EINVAL ; + +v4l2_m2m_for_each_src_buf (inst->fh.m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); +if (vpu_buf->state ==VPU_BUF_STATE_IDLE ) +break ; + vpu_buf =NULL ; + } + +if (!vpu_buf) +return -EINVAL ; + +dev_dbg (inst->dev,"[%d]frame id = %d / %d\n" , + inst->id, vpu_buf->m2m_buf.vb.sequence, inst->sequence); +return call_vop (inst, process_output, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +int vpu_process_capture_buffer (struct vpu_inst *inst) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vpu_vb2_buffer *vpu_buf =NULL ; + +if (!inst) +return -EINVAL ; + +if (!vpu_check_ready (inst, inst->cap_format.type)) +return -EINVAL ; + +v4l2_m2m_for_each_dst_buf (inst->fh.m2m_ctx, buf) { + vpu_buf = container_of(buf, struct vpu_vb2_buffer, m2m_buf); +if (vpu_buf->state ==VPU_BUF_STATE_IDLE ) +break ; + vpu_buf =NULL ; + } +if (!vpu_buf) +return -EINVAL ; + +return call_vop (inst, process_capture, &vpu_buf->m2m_buf.vb.vb2_buf); +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence (struct vpu_inst *inst, +u32 type,u32 sequence) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vb2_v4l2_buffer *vbuf =NULL ; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +v4l2_m2m_for_each_src_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->sequence == sequence) +break ; + vbuf =NULL ; + } + }else { +v4l2_m2m_for_each_dst_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->sequence == sequence) +break ; + vbuf =NULL ; + } + } + +return vbuf; +} + +struct vb2_v4l2_buffer *vpu_find_buf_by_idx (struct vpu_inst *inst, +u32 type,u32 idx) +{ +struct v4l2_m2m_buffer *buf =NULL ; +struct vb2_v4l2_buffer *vbuf =NULL ; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +v4l2_m2m_for_each_src_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->vb2_buf.index == idx) +break ; + vbuf =NULL ; + } + }else { +v4l2_m2m_for_each_dst_buf (inst->fh.m2m_ctx, buf) { + vbuf = &buf->vb; +if (vbuf->vb2_buf.index == idx) +break ; + vbuf =NULL ; + } + } + +return vbuf; +} + +int vpu_get_num_buffers (struct vpu_inst *inst,u32 type) +{ +struct vb2_queue *q; + +if (!inst || !inst->fh.m2m_ctx) +return -EINVAL ; +if (V4L2_TYPE_IS_OUTPUT (type)) + q =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); +else + q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); + +return q->num_buffers; +} + +static void vpu_m2m_device_run (void *priv) +{ +} + +static void vpu_m2m_job_abort (void *priv) +{ +struct vpu_inst *inst = priv; +struct v4l2_m2m_ctx *m2m_ctx = inst->fh.m2m_ctx; + +v4l2_m2m_job_finish (m2m_ctx->m2m_dev, m2m_ctx); +} + +static const struct v4l2_m2m_ops vpu_m2m_ops = { + .device_run = vpu_m2m_device_run, + .job_abort = vpu_m2m_job_abort +}; + +static int vpu_vb2_queue_setup (struct vb2_queue *vq, +unsigned int *buf_count, +unsigned int *plane_count, +unsigned int psize[], +struct device *allocators[]) +{ +struct vpu_inst *inst =vb2_get_drv_priv (vq); +struct vpu_format *cur_fmt; +int i; + + cur_fmt =vpu_get_format (inst, vq->type); + +if (*plane_count) { +if (inst->state ==VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE ) { +for (i =0 ; i < *plane_count; i++) { +if (!psize[i]) + psize[i] = cur_fmt->sizeimage[i]; + } +return 0 ; + } +if (*plane_count != cur_fmt->num_planes) +return -EINVAL ; +for (i =0 ; i < cur_fmt->num_planes; i++) { +if (psize[i] < cur_fmt->sizeimage[i]) +return -EINVAL ; + } +return 0 ; + } + + *plane_count = cur_fmt->num_planes; +for (i =0 ; i < cur_fmt->num_planes; i++) + psize[i] = cur_fmt->sizeimage[i]; + +return 0 ; +} + +static int vpu_vb2_buf_init (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); + + vpu_buf->state =VPU_BUF_STATE_IDLE ; + +return 0 ; +} + +static void vpu_vb2_buf_cleanup (struct vb2_buffer *vb) +{ +} + +static int vpu_vb2_buf_prepare (struct vb2_buffer *vb) +{ +struct vpu_inst *inst =vb2_get_drv_priv (vb->vb2_queue); +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_vb2_buffer *vpu_buf =to_vpu_vb2_buffer (vbuf); +struct vpu_format *cur_fmt; +u32 i; + + cur_fmt =vpu_get_format (inst, vb->type); +if (vb->num_planes != cur_fmt->num_planes) +return -EINVAL ; +for (i =0 ; i < cur_fmt->num_planes; i++) { +if (vpu_get_vb_length (vb, i) < cur_fmt->sizeimage[i]) { +dev_dbg (inst->dev,"[%d] %s buf[%d] is invalid\n" , + inst->id, +vpu_type_name (vb->type), + vb->index); + vpu_buf->state =VPU_BUF_STATE_ERROR ; + } + } + +return 0 ; +} + +static void vpu_vb2_buf_finish (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_inst *inst =vb2_get_drv_priv (vb->vb2_queue); +struct vb2_queue *q = vb->vb2_queue; + +if (vbuf->flags &V4L2_BUF_FLAG_LAST ) +vpu_notify_eos (inst); + +if (list_empty (&q->done_list)) +call_vop (inst, on_queue_empty, q->type); +} + +void vpu_vb2_buffers_return (struct vpu_inst *inst, +unsigned int type,enum vb2_buffer_state state) +{ +struct vb2_v4l2_buffer *buf; + +if (!inst || !inst->fh.m2m_ctx) +return ; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +while ((buf =v4l2_m2m_src_buf_remove (inst->fh.m2m_ctx))) +v4l2_m2m_buf_done (buf, state); + }else { +while ((buf =v4l2_m2m_dst_buf_remove (inst->fh.m2m_ctx))) +v4l2_m2m_buf_done (buf, state); + } +} + +static int vpu_vb2_start_streaming (struct vb2_queue *q,unsigned int count) +{ +struct vpu_inst *inst =vb2_get_drv_priv (q); +struct vpu_format *fmt =vpu_get_format (inst, q->type); +int ret; + +vpu_inst_unlock (inst); + ret =vpu_inst_register (inst); +vpu_inst_lock (inst); +if (ret) { +vpu_vb2_buffers_return (inst, q->type,VB2_BUF_STATE_QUEUED ); +return ret; + } + +vpu_trace (inst->dev,"[%d] %s %c%c%c%c %dx%d %u(%u) %u(%u) %u(%u) %d\n" , + inst->id,vpu_type_name (q->type), + fmt->pixfmt, + fmt->pixfmt >>8 , + fmt->pixfmt >>16 , + fmt->pixfmt >>24 , + fmt->width, fmt->height, + fmt->sizeimage[0 ], fmt->bytesperline[0 ], + fmt->sizeimage[1 ], fmt->bytesperline[1 ], + fmt->sizeimage[2 ], fmt->bytesperline[2 ], + q->num_buffers); +call_vop (inst, start, q->type); +vb2_clear_last_buffer_dequeued (q); + +return 0 ; +} + +static void vpu_vb2_stop_streaming (struct vb2_queue *q) +{ +struct vpu_inst *inst =vb2_get_drv_priv (q); + +vpu_trace (inst->dev,"[%d] %s\n" , inst->id,vpu_type_name (q->type)); + +call_vop (inst, stop, q->type); +vpu_vb2_buffers_return (inst, q->type,VB2_BUF_STATE_ERROR ); +if (V4L2_TYPE_IS_OUTPUT (q->type)) + inst->sequence =0 ; +} + +static void vpu_vb2_buf_queue (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct vpu_inst *inst =vb2_get_drv_priv (vb->vb2_queue); + +if (V4L2_TYPE_IS_OUTPUT (vb->type)) { + vbuf->sequence = inst->sequence++; +if ((s64 )vb->timestamp <0 ) + vb->timestamp =VPU_INVALID_TIMESTAMP ; + } + +v4l2_m2m_buf_queue (inst->fh.m2m_ctx, vbuf); +vpu_process_output_buffer (inst); +vpu_process_capture_buffer (inst); +} + +static struct vb2_ops vpu_vb2_ops = { + .queue_setup = vpu_vb2_queue_setup, + .buf_init = vpu_vb2_buf_init, + .buf_cleanup = vpu_vb2_buf_cleanup, + .buf_prepare = vpu_vb2_buf_prepare, + .buf_finish = vpu_vb2_buf_finish, + .start_streaming = vpu_vb2_start_streaming, + .stop_streaming = vpu_vb2_stop_streaming, + .buf_queue = vpu_vb2_buf_queue, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int vpu_m2m_queue_init (void *priv,struct vb2_queue *src_vq, +struct vb2_queue *dst_vq) +{ +struct vpu_inst *inst = priv; +int ret; + + inst->out_format.type = src_vq->type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + src_vq->io_modes =VB2_MMAP |VB2_USERPTR |VB2_DMABUF ; + src_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + src_vq->ops = &vpu_vb2_ops; + src_vq->mem_ops = &vb2_dma_contig_memops; +if (inst->type ==VPU_CORE_TYPE_DEC && inst->use_stream_buffer) + src_vq->mem_ops = &vb2_vmalloc_memops; + src_vq->drv_priv = inst; + src_vq->buf_struct_size =sizeof (struct vpu_vb2_buffer ); + src_vq->allow_zero_bytesused =1 ; + src_vq->min_buffers_needed =1 ; + src_vq->dev = inst->vpu->dev; + src_vq->lock = &inst->lock; + ret =vb2_queue_init (src_vq); +if (ret) +return ret; + + inst->cap_format.type = dst_vq->type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + dst_vq->io_modes =VB2_MMAP |VB2_USERPTR |VB2_DMABUF ; + dst_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + dst_vq->ops = &vpu_vb2_ops; + dst_vq->mem_ops = &vb2_dma_contig_memops; +if (inst->type ==VPU_CORE_TYPE_ENC && inst->use_stream_buffer) + dst_vq->mem_ops = &vb2_vmalloc_memops; + dst_vq->drv_priv = inst; + dst_vq->buf_struct_size =sizeof (struct vpu_vb2_buffer ); + dst_vq->allow_zero_bytesused =1 ; + dst_vq->min_buffers_needed =1 ; + dst_vq->dev = inst->vpu->dev; + dst_vq->lock = &inst->lock; + ret =vb2_queue_init (dst_vq); +if (ret) { +vb2_queue_release (src_vq); +return ret; + } + +return 0 ; +} + +static int vpu_v4l2_release (struct vpu_inst *inst) +{ +vpu_trace (inst->vpu->dev,"%p\n" , inst); + +vpu_release_core (inst->core); +put_device (inst->dev); + +if (inst->workqueue) { +cancel_work_sync (&inst->msg_work); +destroy_workqueue (inst->workqueue); + inst->workqueue =NULL ; + } +if (inst->fh.m2m_ctx) { +v4l2_m2m_ctx_release (inst->fh.m2m_ctx); + inst->fh.m2m_ctx =NULL ; + } + +v4l2_ctrl_handler_free (&inst->ctrl_handler); +mutex_destroy (&inst->lock); +v4l2_fh_del (&inst->fh); +v4l2_fh_exit (&inst->fh); + +call_vop (inst, cleanup); + +return 0 ; +} + +int vpu_v4l2_open (struct file *file,struct vpu_inst *inst) +{ +struct vpu_dev *vpu =video_drvdata (file); +struct vpu_func *func; +int ret =0 ; + +WARN_ON (!file || !inst || !inst->ops); + +if (inst->type ==VPU_CORE_TYPE_ENC ) + func = &vpu->encoder; +else + func = &vpu->decoder; + +atomic_set (&inst->ref_count,0 ); +vpu_inst_get (inst); + inst->vpu = vpu; + inst->core =vpu_request_core (vpu, inst->type); +if (inst->core) + inst->dev =get_device (inst->core->dev); +mutex_init (&inst->lock); +INIT_LIST_HEAD (&inst->cmd_q); + inst->id =VPU_INST_NULL_ID ; + inst->release = vpu_v4l2_release; + inst->pid = current->pid; + inst->tgid = current->tgid; + inst->min_buffer_cap =2 ; + inst->min_buffer_out =2 ; +v4l2_fh_init (&inst->fh, func->vfd); +v4l2_fh_add (&inst->fh); + + ret =call_vop (inst, ctrl_init); +if (ret) + goto error; + + inst->fh.m2m_ctx =v4l2_m2m_ctx_init (func->m2m_dev, + inst, vpu_m2m_queue_init); +if (IS_ERR (inst->fh.m2m_ctx)) { +dev_err (vpu->dev,"v4l2_m2m_ctx_init fail\n" ); + ret =PTR_ERR (func->m2m_dev); + goto error; + } + + inst->fh.ctrl_handler = &inst->ctrl_handler; + file->private_data = &inst->fh; + inst->state =VPU_CODEC_STATE_DEINIT ; + inst->workqueue =alloc_workqueue ("vpu_inst" ,WQ_UNBOUND |WQ_MEM_RECLAIM ,1 ); +if (inst->workqueue) { +INIT_WORK (&inst->msg_work, vpu_inst_run_work); + ret =kfifo_init (&inst->msg_fifo, + inst->msg_buffer, +roundup_pow_of_two (sizeof (inst->msg_buffer))); +if (ret) { +destroy_workqueue (inst->workqueue); + inst->workqueue =NULL ; + } + } +vpu_trace (vpu->dev,"tgid = %d, pid = %d, type = %s, inst = %p\n" , + inst->tgid, inst->pid,vpu_core_type_desc (inst->type), inst); + +return 0 ; +error: +vpu_inst_put (inst); +return ret; +} + +int vpu_v4l2_close (struct file *file) +{ +struct vpu_dev *vpu =video_drvdata (file); +struct vpu_inst *inst =to_inst (file); +struct vb2_queue *src_q; +struct vb2_queue *dst_q; + +vpu_trace (vpu->dev,"tgid = %d, pid = %d, inst = %p\n" , + inst->tgid, inst->pid, inst); + src_q =v4l2_m2m_get_src_vq (inst->fh.m2m_ctx); + dst_q =v4l2_m2m_get_dst_vq (inst->fh.m2m_ctx); +vpu_inst_lock (inst); +if (vb2_is_streaming (src_q)) +v4l2_m2m_streamoff (file, inst->fh.m2m_ctx, src_q->type); +if (vb2_is_streaming (dst_q)) +v4l2_m2m_streamoff (file, inst->fh.m2m_ctx, dst_q->type); +vpu_inst_unlock (inst); + +call_vop (inst, release); +vpu_inst_unregister (inst); +vpu_inst_put (inst); + +return 0 ; +} + +int vpu_add_func (struct vpu_dev *vpu,struct vpu_func *func) +{ +struct video_device *vfd; +int ret; + +if (!vpu || !func) +return -EINVAL ; + +if (func->vfd) +return 0 ; + + vfd =video_device_alloc (); +if (!vfd) { +dev_err (vpu->dev,"alloc vpu decoder video device fail\n" ); +return -ENOMEM ; + } + vfd->release = video_device_release; + vfd->vfl_dir =VFL_DIR_M2M ; + vfd->v4l2_dev = &vpu->v4l2_dev; + vfd->device_caps =V4L2_CAP_VIDEO_M2M_MPLANE |V4L2_CAP_STREAMING ; +if (func->type ==VPU_CORE_TYPE_ENC ) { +strscpy (vfd->name,"amphion-vpu-encoder" ,sizeof (vfd->name)); + vfd->fops =venc_get_fops (); + vfd->ioctl_ops =venc_get_ioctl_ops (); + }else { +strscpy (vfd->name,"amphion-vpu-decoder" ,sizeof (vfd->name)); + vfd->fops =vdec_get_fops (); + vfd->ioctl_ops =vdec_get_ioctl_ops (); + } + + ret =video_register_device (vfd,VFL_TYPE_VIDEO ,-1 ); +if (ret) { +video_device_release (vfd); +return ret; + } +video_set_drvdata (vfd, vpu); + func->vfd = vfd; + func->m2m_dev =v4l2_m2m_init (&vpu_m2m_ops); +if (IS_ERR (func->m2m_dev)) { +dev_err (vpu->dev,"v4l2_m2m_init fail\n" ); +video_unregister_device (func->vfd); + func->vfd =NULL ; +return PTR_ERR (func->m2m_dev); + } + + ret =v4l2_m2m_register_media_controller (func->m2m_dev, func->vfd, func->function); +if (ret) { +v4l2_m2m_release (func->m2m_dev); + func->m2m_dev =NULL ; +video_unregister_device (func->vfd); + func->vfd =NULL ; +return ret; + } + +return 0 ; +} + +void vpu_remove_func (struct vpu_func *func) +{ +if (!func) +return ; + +if (func->m2m_dev) { +v4l2_m2m_unregister_media_controller (func->m2m_dev); +v4l2_m2m_release (func->m2m_dev); + func->m2m_dev =NULL ; + } +if (func->vfd) { +video_unregister_device (func->vfd); + func->vfd =NULL ; + } +}drivers/media/platform/amphion/vpu_v4l2.h new file +54
-0@@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright 2020-2021 NXP +*/ + +#ifndef _AMPHION_VPU_V4L2_H +#define _AMPHION_VPU_V4L2_H + +#include <linux/videodev2.h> + +void vpu_inst_lock (struct vpu_inst *inst); +void vpu_inst_unlock (struct vpu_inst *inst); + +int vpu_v4l2_open (struct file *file,struct vpu_inst *inst); +int vpu_v4l2_close (struct file *file); + +const struct vpu_format *vpu_try_fmt_common (struct vpu_inst *inst,struct v4l2_format *f); +int vpu_process_output_buffer (struct vpu_inst *inst); +int vpu_process_capture_buffer (struct vpu_inst *inst); +struct vb2_v4l2_buffer *vpu_find_buf_by_sequence (struct vpu_inst *inst,u32 type,u32 sequence); +struct vb2_v4l2_buffer *vpu_find_buf_by_idx (struct vpu_inst *inst,u32 type,u32 idx); +void vpu_v4l2_set_error (struct vpu_inst *inst); +int vpu_notify_eos (struct vpu_inst *inst); +int vpu_notify_source_change (struct vpu_inst *inst); +int vpu_set_last_buffer_dequeued (struct vpu_inst *inst); +void vpu_vb2_buffers_return (struct vpu_inst *inst, +unsigned int type,enum vb2_buffer_state state); +int vpu_get_num_buffers (struct vpu_inst *inst,u32 type); + +dma_addr_t vpu_get_vb_phy_addr (struct vb2_buffer *vb,u32 plane_no); +unsigned int vpu_get_vb_length (struct vb2_buffer *vb,u32 plane_no); +static inline struct vpu_format *vpu_get_format (struct vpu_inst *inst,u32 type) +{ +if (V4L2_TYPE_IS_OUTPUT (type)) +return &inst->out_format; +else +return &inst->cap_format; +} + +static inline char *vpu_type_name (u32 type) +{ +return V4L2_TYPE_IS_OUTPUT (type) ?"output" :"capture" ; +} + +static inline int vpu_vb_is_codecconfig (struct vb2_v4l2_buffer *vbuf) +{ +#ifdef V4L2_BUF_FLAG_CODECCONFIG +return (vbuf->flags &V4L2_BUF_FLAG_CODECCONFIG ) ?1 :0 ; +#else +return 0 ; +#endif +} + +#endif
Ming Qian
maintainerPATCH v13 7/13 media: amphion: add v4l2 m2m vpu encoder stateful driver
In reply to Ming QianThis consists of video encoder implementation plus encoder controls.
--- drivers/media/platform/amphion/venc.c | 1351 +++++++++++++++++++++++++ 1 file changed, 1351 insertions(+) create mode 100644 drivers/media/platform/amphion/venc.c
drivers/media/platform/amphion/venc.c new file +1351 -0
@@ -0,0 +1,1351 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2020-2021 NXP +*/ + +#include <linux/init.h> +#include <linux/interconnect.h> +#include <linux/ioctl.h> +#include <linux/list.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/delay.h> +#include <linux/videodev2.h> +#include <linux/ktime.h> +#include <media/v4l2-device.h> +#include <media/v4l2-event.h> +#include <media/v4l2-mem2mem.h> +#include <media/v4l2-ioctl.h> +#include <media/videobuf2-v4l2.h> +#include <media/videobuf2-dma-contig.h> +#include <media/videobuf2-vmalloc.h> +#include "vpu.h" +#include "vpu_defs.h" +#include "vpu_core.h" +#include "vpu_helpers.h" +#include "vpu_v4l2.h" +#include "vpu_cmds.h" +#include "vpu_rpc.h" + +#define VENC_OUTPUT_ENABLE (1 << 0) +#define VENC_CAPTURE_ENABLE (1 << 1) +#define VENC_ENABLE_MASK (VENC_OUTPUT_ENABLE | VENC_CAPTURE_ENABLE) +#define VENC_MAX_BUF_CNT 8 + +struct venc_t { +struct vpu_encode_params params; +u32 request_key_frame; +u32 input_ready; +u32 cpb_size; +bool bitrate_change; + +struct vpu_buffer enc[VENC_MAX_BUF_CNT ]; +struct vpu_buffer ref[VENC_MAX_BUF_CNT ]; +struct vpu_buffer act[VENC_MAX_BUF_CNT ]; +struct list_head frames; +u32 frame_count; +u32 encode_count; +u32 ready_count; +u32 enable; +u32 stopped; + +u32 skipped_count; +u32 skipped_bytes; + +wait_queue_head_t wq; +}; + +struct venc_frame_t { +struct list_head list; +struct vpu_enc_pic_info info; +u32 bytesused; +s64 timestamp; +}; + +static const struct vpu_format venc_formats[] = { + { + .pixfmt =V4L2_PIX_FMT_NV12M , + .num_planes =2 , + .type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE , + }, + { + .pixfmt =V4L2_PIX_FMT_H264 , + .num_planes =1 , + .type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE , + }, + {0 ,0 ,0 ,0 }, +}; + +static int venc_querycap (struct file *file,void *fh,struct v4l2_capability *cap) +{ +strscpy (cap->driver,"amphion-vpu" ,sizeof (cap->driver)); +strscpy (cap->card,"amphion vpu encoder" ,sizeof (cap->card)); +strscpy (cap->bus_info,"platform: amphion-vpu" ,sizeof (cap->bus_info)); + +return 0 ; +} + +static int venc_enum_fmt (struct file *file,void *fh,struct v4l2_fmtdesc *f) +{ +struct vpu_inst *inst =to_inst (file); +const struct vpu_format *fmt; + +memset (f->reserved,0 ,sizeof (f->reserved)); + fmt =vpu_helper_enum_format (inst, f->type, f->index); +if (!fmt) +return -EINVAL ; + + f->pixelformat = fmt->pixfmt; + f->flags = fmt->flags; + +return 0 ; +} + +static int venc_enum_framesizes (struct file *file,void *fh,struct v4l2_frmsizeenum *fsize) +{ +struct vpu_inst *inst =to_inst (file); +const struct vpu_core_resources *res; + +if (!fsize || fsize->index) +return -EINVAL ; + +if (!vpu_helper_find_format (inst,0 , fsize->pixel_format)) +return -EINVAL ; + + res =vpu_get_resource (inst); +if (!res) +return -EINVAL ; + fsize->type =V4L2_FRMSIZE_TYPE_STEPWISE ; + fsize->stepwise.max_width = res->max_width; + fsize->stepwise.max_height = res->max_height; + fsize->stepwise.min_width = res->min_width; + fsize->stepwise.min_height = res->min_height; + fsize->stepwise.step_width = res->step_width; + fsize->stepwise.step_height = res->step_height; + +return 0 ; +} + +static int venc_enum_frameintervals (struct file *file,void *fh,struct v4l2_frmivalenum *fival) +{ +struct vpu_inst *inst =to_inst (file); +const struct vpu_core_resources *res; + +if (!fival || fival->index) +return -EINVAL ; + +if (!vpu_helper_find_format (inst,0 , fival->pixel_format)) +return -EINVAL ; + +if (!fival->width || !fival->height) +return -EINVAL ; + + res =vpu_get_resource (inst); +if (!res) +return -EINVAL ; +if (fival->width < res->min_width || + fival->width > res->max_width || + fival->height < res->min_height || + fival->height > res->max_height) +return -EINVAL ; + + fival->type =V4L2_FRMIVAL_TYPE_CONTINUOUS ; + fival->stepwise.min.numerator =1 ; + fival->stepwise.min.denominator =USHRT_MAX ; + fival->stepwise.max.numerator =USHRT_MAX ; + fival->stepwise.max.denominator =1 ; + fival->stepwise.step.numerator =1 ; + fival->stepwise.step.denominator =1 ; + +return 0 ; +} + +static int venc_g_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct vpu_inst *inst =to_inst (file); +struct venc_t *venc = inst->priv; +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +struct vpu_format *cur_fmt; +int i; + + cur_fmt =vpu_get_format (inst, f->type); + + pixmp->pixelformat = cur_fmt->pixfmt; + pixmp->num_planes = cur_fmt->num_planes; + pixmp->width = cur_fmt->width; + pixmp->height = cur_fmt->height; + pixmp->field = cur_fmt->field; + pixmp->flags = cur_fmt->flags; +for (i =0 ; i < pixmp->num_planes; i++) { + pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i]; + pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i]; + } + + f->fmt.pix_mp.colorspace = venc->params.color.primaries; + f->fmt.pix_mp.xfer_func = venc->params.color.transfer; + f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix; + f->fmt.pix_mp.quantization = venc->params.color.full_range; + +return 0 ; +} + +static int venc_try_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct vpu_inst *inst =to_inst (file); + +vpu_try_fmt_common (inst, f); + +return 0 ; +} + +static int venc_s_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct vpu_inst *inst =to_inst (file); +const struct vpu_format *fmt; +struct vpu_format *cur_fmt; +struct vb2_queue *q; +struct venc_t *venc = inst->priv; +struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; +int i; + + q =v4l2_m2m_get_vq (inst->fh.m2m_ctx, f->type); +if (!q) +return -EINVAL ; +if (vb2_is_streaming (q)) +return -EBUSY ; + + fmt =vpu_try_fmt_common (inst, f); +if (!fmt) +return -EINVAL ; + + cur_fmt =vpu_get_format (inst, f->type); + + cur_fmt->pixfmt = fmt->pixfmt; + cur_fmt->num_planes = fmt->num_planes; + cur_fmt->flags = fmt->flags; + cur_fmt->width = pix_mp->width; + cur_fmt->height = pix_mp->height; +for (i =0 ; i < fmt->num_planes; i++) { + cur_fmt->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage; + cur_fmt->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline; + } + +if (pix_mp->field !=V4L2_FIELD_ANY ) + cur_fmt->field = pix_mp->field; + +if (V4L2_TYPE_IS_OUTPUT (f->type)) { + venc->params.input_format = cur_fmt->pixfmt; + venc->params.src_stride = cur_fmt->bytesperline[0 ]; + venc->params.src_width = cur_fmt->width; + venc->params.src_height = cur_fmt->height; + venc->params.crop.left =0 ; + venc->params.crop.top =0 ; + venc->params.crop.width = cur_fmt->width; + venc->params.crop.height = cur_fmt->height; + }else { + venc->params.codec_format = cur_fmt->pixfmt; + venc->params.out_width = cur_fmt->width; + venc->params.out_height = cur_fmt->height; + } + +if (V4L2_TYPE_IS_OUTPUT (f->type)) { +if (!vpu_color_check_primaries (pix_mp->colorspace)) { + venc->params.color.primaries = pix_mp->colorspace; +vpu_color_get_default (venc->params.color.primaries, + &venc->params.color.transfer, + &venc->params.color.matrix, + &venc->params.color.full_range); + } +if (!vpu_color_check_transfers (pix_mp->xfer_func)) + venc->params.color.transfer = pix_mp->xfer_func; +if (!vpu_color_check_matrix (pix_mp->ycbcr_enc)) + venc->params.color.matrix = pix_mp->ycbcr_enc; +if (!vpu_color_check_full_range (pix_mp->quantization)) + venc->params.color.full_range = pix_mp->quantization; + } + + pix_mp->colorspace = venc->params.color.primaries; + pix_mp->xfer_func = venc->params.color.transfer; + pix_mp->ycbcr_enc = venc->params.color.matrix; + pix_mp->quantization = venc->params.color.full_range; + +return 0 ; +} + +static int venc_g_parm (struct file *file,void *fh,struct v4l2_streamparm *parm) +{ +struct vpu_inst *inst =to_inst (file); +struct venc_t *venc = inst->priv; +struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe; + +if (!parm) +return -EINVAL ; + +if (!vpu_helper_check_type (inst, parm->type)) +return -EINVAL ; + + parm->parm.capture.capability =V4L2_CAP_TIMEPERFRAME ; + parm->parm.capture.readbuffers =0 ; + timeperframe->numerator = venc->params.frame_rate.numerator; + timeperframe->denominator = venc->params.frame_rate.denominator; + +return 0 ; +} + +static int venc_s_parm (struct file *file,void *fh,struct v4l2_streamparm *parm) +{ +struct vpu_inst *inst =to_inst (file); +struct venc_t *venc = inst->priv; +struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe; + +if (!parm) +return -EINVAL ; + +if (!vpu_helper_check_type (inst, parm->type)) +return -EINVAL ; + +if (!timeperframe->numerator) + timeperframe->numerator = venc->params.frame_rate.numerator; +if (!timeperframe->denominator) + timeperframe->denominator = venc->params.frame_rate.denominator; + + venc->params.frame_rate.numerator = timeperframe->numerator; + venc->params.frame_rate.denominator = timeperframe->denominator; + +vpu_helper_calc_coprime (&venc->params.frame_rate.numerator, + &venc->params.frame_rate.denominator); + + parm->parm.capture.capability =V4L2_CAP_TIMEPERFRAME ; +memset (parm->parm.capture.reserved, +0 ,sizeof (parm->parm.capture.reserved)); + +return 0 ; +} + +static int venc_g_selection (struct file *file,void *fh,struct v4l2_selection *s) +{ +struct vpu_inst *inst =to_inst (file); +struct venc_t *venc = inst->priv; + +if (s->type !=V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type !=V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) +return -EINVAL ; + +switch (s->target) { +case V4L2_SEL_TGT_CROP_DEFAULT : +case V4L2_SEL_TGT_CROP_BOUNDS : + s->r.left =0 ; + s->r.top =0 ; + s->r.width = inst->out_format.width; + s->r.height = inst->out_format.height; +break ; +case V4L2_SEL_TGT_CROP : + s->r = venc->params.crop; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static int venc_valid_crop (struct venc_t *venc,const struct vpu_core_resources *res) +{ +struct v4l2_rect *rect =NULL ; +u32 min_width; +u32 min_height; +u32 src_width; +u32 src_height; + + rect = &venc->params.crop; + min_width = res->min_width; + min_height = res->min_height; + src_width = venc->params.src_width; + src_height = venc->params.src_height; + +if (rect->width ==0 || rect->height ==0 ) +return -EINVAL ; +if (rect->left > src_width - min_width || + rect->top > src_height - min_height) +return -EINVAL ; + + rect->width =min (rect->width, src_width - rect->left); + rect->width =max_t (u32, rect->width, min_width); + + rect->height =min (rect->height, src_height - rect->top); + rect->height =max_t (u32, rect->height, min_height); + +return 0 ; +} + +static int venc_s_selection (struct file *file,void *fh,struct v4l2_selection *s) +{ +struct vpu_inst *inst =to_inst (file); +const struct vpu_core_resources *res; +struct venc_t *venc = inst->priv; + + res =vpu_get_resource (inst); +if (!res) +return -EINVAL ; + +if (s->type !=V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type !=V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) +return -EINVAL ; +if (s->target !=V4L2_SEL_TGT_CROP ) +return -EINVAL ; + + venc->params.crop.left =ALIGN (s->r.left, res->step_width); + venc->params.crop.top =ALIGN (s->r.top, res->step_height); + venc->params.crop.width =ALIGN (s->r.width, res->step_width); + venc->params.crop.height =ALIGN (s->r.height, res->step_height); +if (venc_valid_crop (venc, res)) { + venc->params.crop.left =0 ; + venc->params.crop.top =0 ; + venc->params.crop.width = venc->params.src_width; + venc->params.crop.height = venc->params.src_height; + } + + inst->crop = venc->params.crop; + +return 0 ; +} + +static int venc_drain (struct vpu_inst *inst) +{ +struct venc_t *venc = inst->priv; +int ret; + +if (inst->state !=VPU_CODEC_STATE_DRAIN ) +return 0 ; + +if (v4l2_m2m_num_src_bufs_ready (inst->fh.m2m_ctx)) +return 0 ; + +if (!venc->input_ready) +return 0 ; + + venc->input_ready = false; +vpu_trace (inst->dev,"[%d]\n" , inst->id); + ret =vpu_session_stop (inst); +if (ret) +return ret; + inst->state =VPU_CODEC_STATE_STOP ; +wake_up_all (&venc->wq); + +return 0 ; +} + +static int venc_request_eos (struct vpu_inst *inst) +{ + inst->state =VPU_CODEC_STATE_DRAIN ; +venc_drain (inst); + +return 0 ; +} + +static int venc_encoder_cmd (struct file *file,void *fh,struct v4l2_encoder_cmd *cmd) +{ +struct vpu_inst *inst =to_inst (file); +int ret; + + ret =v4l2_m2m_ioctl_try_encoder_cmd (file, fh, cmd); +if (ret) +return ret; + +vpu_inst_lock (inst); +if (cmd->cmd ==V4L2_ENC_CMD_STOP ) { +if (inst->state ==VPU_CODEC_STATE_DEINIT ) +vpu_set_last_buffer_dequeued (inst); +else +venc_request_eos (inst); + } +vpu_inst_unlock (inst); + +return 0 ; +} + +static int venc_subscribe_event (struct v4l2_fh *fh,const struct v4l2_event_subscription *sub) +{ +switch (sub->type) { +case V4L2_EVENT_EOS : +return v4l2_event_subscribe (fh, sub,0 ,NULL ); +case V4L2_EVENT_CTRL : +return v4l2_ctrl_subscribe_event (fh, sub); +default : +return -EINVAL ; + } +} + +static const struct v4l2_ioctl_ops venc_ioctl_ops = { + .vidioc_querycap = venc_querycap, + .vidioc_enum_fmt_vid_cap = venc_enum_fmt, + .vidioc_enum_fmt_vid_out = venc_enum_fmt, + .vidioc_enum_framesizes = venc_enum_framesizes, + .vidioc_enum_frameintervals = venc_enum_frameintervals, + .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt, + .vidioc_g_fmt_vid_out_mplane = venc_g_fmt, + .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt, + .vidioc_try_fmt_vid_out_mplane = venc_try_fmt, + .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt, + .vidioc_s_fmt_vid_out_mplane = venc_s_fmt, + .vidioc_g_parm = venc_g_parm, + .vidioc_s_parm = venc_s_parm, + .vidioc_g_selection = venc_g_selection, + .vidioc_s_selection = venc_s_selection, + .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, + .vidioc_encoder_cmd = venc_encoder_cmd, + .vidioc_subscribe_event = venc_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, + .vidioc_streamon = v4l2_m2m_ioctl_streamon, + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, +}; + +static int venc_op_s_ctrl (struct v4l2_ctrl *ctrl) +{ +struct vpu_inst *inst =ctrl_to_inst (ctrl); +struct venc_t *venc = inst->priv; +int ret =0 ; + +vpu_inst_lock (inst); +switch (ctrl->id) { +case V4L2_CID_MPEG_VIDEO_H264_PROFILE : + venc->params.profile = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_LEVEL : + venc->params.level = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_BITRATE_MODE : + venc->params.rc_mode = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_BITRATE : +if (ctrl->val != venc->params.bitrate) + venc->bitrate_change = true; + venc->params.bitrate = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_GOP_SIZE : + venc->params.gop_length = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_B_FRAMES : + venc->params.bframes = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP : + venc->params.i_frame_qp = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP : + venc->params.p_frame_qp = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP : + venc->params.b_frame_qp = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME : + venc->request_key_frame =1 ; +break ; +case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE : + venc->cpb_size = ctrl->val *1024 ; +break ; +case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE : + venc->params.sar.enable = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC : + venc->params.sar.idc = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH : + venc->params.sar.width = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT : + venc->params.sar.height = ctrl->val; +break ; +case V4L2_CID_MPEG_VIDEO_HEADER_MODE : +break ; +default : + ret = -EINVAL ; +break ; + } +vpu_inst_unlock (inst); + +return ret; +} + +static const struct v4l2_ctrl_ops venc_ctrl_ops = { + .s_ctrl = venc_op_s_ctrl, + .g_volatile_ctrl = vpu_helper_g_volatile_ctrl, +}; + +static int venc_ctrl_init (struct vpu_inst *inst) +{ +struct v4l2_ctrl *ctrl; +int ret; + + ret =v4l2_ctrl_handler_init (&inst->ctrl_handler,20 ); +if (ret) +return ret; + +v4l2_ctrl_new_std_menu (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_PROFILE , +V4L2_MPEG_VIDEO_H264_PROFILE_HIGH , + ~((1 <<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE ) | + (1 <<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN ) | + (1 <<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH )), +V4L2_MPEG_VIDEO_H264_PROFILE_HIGH ); + +v4l2_ctrl_new_std_menu (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_LEVEL , +V4L2_MPEG_VIDEO_H264_LEVEL_5_1 , +0x0 , +V4L2_MPEG_VIDEO_H264_LEVEL_4_0 ); + +v4l2_ctrl_new_std_menu (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_BITRATE_MODE , +V4L2_MPEG_VIDEO_BITRATE_MODE_CBR , +0x0 , +V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ); + +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_BITRATE , +BITRATE_MIN , +BITRATE_MAX , +BITRATE_STEP , +BITRATE_DEFAULT ); + +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_GOP_SIZE ,0 , (1 <<16 ) -1 ,1 ,30 ); + +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_B_FRAMES ,0 ,4 ,1 ,0 ); + +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP ,1 ,51 ,1 ,26 ); +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP ,1 ,51 ,1 ,28 ); +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP ,1 ,51 ,1 ,30 ); +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME ,0 ,0 ,0 ,0 ); + ctrl =v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MIN_BUFFERS_FOR_CAPTURE ,1 ,32 ,1 ,2 ); +if (ctrl) + ctrl->flags |=V4L2_CTRL_FLAG_VOLATILE ; + ctrl =v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MIN_BUFFERS_FOR_OUTPUT ,1 ,32 ,1 ,2 ); +if (ctrl) + ctrl->flags |=V4L2_CTRL_FLAG_VOLATILE ; + +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE ,64 ,10240 ,1 ,1024 ); + +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE ,0 ,1 ,1 ,1 ); +v4l2_ctrl_new_std_menu (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC , +V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED , +0x0 , + V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1); +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH , +0 ,USHRT_MAX ,1 ,1 ); +v4l2_ctrl_new_std (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT , +0 ,USHRT_MAX ,1 ,1 ); +v4l2_ctrl_new_std_menu (&inst->ctrl_handler, &venc_ctrl_ops, +V4L2_CID_MPEG_VIDEO_HEADER_MODE , +V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME , + ~(1 <<V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME ), +V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME ); + + ret =v4l2_ctrl_handler_setup (&inst->ctrl_handler); +if (ret) { +dev_err (inst->dev,"[%d] setup ctrls fail, ret = %d\n" , inst->id, ret); +v4l2_ctrl_handler_free (&inst->ctrl_handler); +return ret; + } + +return 0 ; +} + +static bool venc_check_ready (struct vpu_inst *inst,unsigned int type) +{ +struct venc_t *venc = inst->priv; + +if (V4L2_TYPE_IS_OUTPUT (type)) { +if (vpu_helper_get_free_space (inst) < venc->cpb_size) +return false; +return venc->input_ready; + } + +if (list_empty (&venc->frames)) +return false; +return true; +} + +static u32 venc_get_enable_mask (u32 type) +{ +if (V4L2_TYPE_IS_OUTPUT (type)) +return VENC_OUTPUT_ENABLE ; +else +return VENC_CAPTURE_ENABLE ; +} + +static void venc_set_enable (struct venc_t *venc,u32 type,int enable) +{ +u32 mask =venc_get_enable_mask (type); + +if (enable) + venc->enable |= mask; +else + venc->enable &= ~mask; +} + +static u32 venc_get_enable (struct venc_t *venc,u32 type) +{ +return venc->enable &venc_get_enable_mask (type); +} + +static void venc_input_done (struct vpu_inst *inst) +{ +struct venc_t *venc = inst->priv; + +vpu_inst_lock (inst); + venc->input_ready = true; +vpu_process_output_buffer (inst); +if (inst->state ==VPU_CODEC_STATE_DRAIN ) +venc_drain (inst); +vpu_inst_unlock (inst); +} + +/* +* It's hardware limitation, that there may be several bytes +* redundant data at the beginning of frame. +* For android platform, the redundant data may cause cts test fail +* So driver will strip them +*/ +static int venc_precheck_encoded_frame (struct vpu_inst *inst,struct venc_frame_t *frame) +{ +struct venc_t *venc; +int skipped; + +if (!inst || !frame || !frame->bytesused) +return -EINVAL ; + + venc = inst->priv; + skipped =vpu_helper_find_startcode (&inst->stream_buffer, + inst->cap_format.pixfmt, + frame->info.wptr - inst->stream_buffer.phys, + frame->bytesused); +if (skipped >0 ) { + frame->bytesused -= skipped; + frame->info.wptr =vpu_helper_step_walk (&inst->stream_buffer, + frame->info.wptr, skipped); + venc->skipped_bytes += skipped; + venc->skipped_count++; + } + +return 0 ; +} + +static int venc_get_one_encoded_frame (struct vpu_inst *inst, +struct venc_frame_t *frame, +struct vb2_v4l2_buffer *vbuf) +{ +struct venc_t *venc = inst->priv; +struct vpu_vb2_buffer *vpu_buf; + +if (!vbuf) +return -EAGAIN ; + +if (!venc_get_enable (inst->priv, vbuf->vb2_buf.type)) { +v4l2_m2m_buf_done (vbuf,VB2_BUF_STATE_ERROR ); +return 0 ; + } + vpu_buf =to_vpu_vb2_buffer (vbuf); +if (frame->bytesused > vbuf->vb2_buf.planes[0 ].length) { +v4l2_m2m_buf_done (vbuf,VB2_BUF_STATE_ERROR ); +return -ENOMEM ; + } + +venc_precheck_encoded_frame (inst, frame); + +if (frame->bytesused) { +u32 rptr = frame->info.wptr; +void *dst =vb2_plane_vaddr (&vbuf->vb2_buf,0 ); + +vpu_helper_copy_from_stream_buffer (&inst->stream_buffer, + &rptr, frame->bytesused, dst); +vpu_iface_update_stream_buffer (inst, rptr,0 ); + } +vb2_set_plane_payload (&vbuf->vb2_buf,0 , frame->bytesused); + vbuf->sequence = frame->info.frame_id; + vbuf->vb2_buf.timestamp = frame->info.timestamp; + vbuf->field = inst->cap_format.field; + vbuf->flags |= frame->info.pic_type; + vpu_buf->state =VPU_BUF_STATE_IDLE ; +dev_dbg (inst->dev,"[%d][OUTPUT TS]%32lld\n" , inst->id, frame->info.timestamp); +v4l2_m2m_buf_done (vbuf,VB2_BUF_STATE_DONE ); + venc->ready_count++; + +if (vbuf->flags &V4L2_BUF_FLAG_KEYFRAME ) +dev_dbg (inst->dev,"[%d][%d]key frame\n" , inst->id, frame->info.frame_id); + +return 0 ; +} + +static int venc_get_encoded_frames (struct vpu_inst *inst) +{ +struct venc_t *venc; +struct venc_frame_t *frame; +struct venc_frame_t *tmp; + +if (!inst || !inst->priv) +return -EINVAL ; + + venc = inst->priv; +list_for_each_entry_safe (frame, tmp, &venc->frames, list) { +if (venc_get_one_encoded_frame (inst, frame, +v4l2_m2m_dst_buf_remove (inst->fh.m2m_ctx))) +break ; +list_del_init (&frame->list); +vfree (frame); + } + +return 0 ; +} + +static int venc_frame_encoded (struct vpu_inst *inst,void *arg) +{ +struct vpu_enc_pic_info *info = arg; +struct venc_frame_t *frame; +struct venc_t *venc; +int ret =0 ; + +if (!inst || !info) +return -EINVAL ; + venc = inst->priv; + frame =vzalloc (sizeof (*frame)); +if (!frame) +return -ENOMEM ; + +memcpy (&frame->info, info,sizeof (frame->info)); + frame->bytesused = info->frame_size; + +vpu_inst_lock (inst); +list_add_tail (&frame->list, &venc->frames); + venc->encode_count++; +venc_get_encoded_frames (inst); +vpu_inst_unlock (inst); + +return ret; +} + +static void venc_buf_done (struct vpu_inst *inst,struct vpu_frame_info *frame) +{ +struct vb2_v4l2_buffer *vbuf; +struct vpu_vb2_buffer *vpu_buf; + +if (!inst || !frame) +return ; + +vpu_inst_lock (inst); +if (!venc_get_enable (inst->priv, frame->type)) + goto exit; + vbuf =vpu_find_buf_by_sequence (inst, frame->type, frame->sequence); +if (!vbuf) { +dev_err (inst->dev,"[%d] can't find buf: type %d, sequence %d\n" , + inst->id, frame->type, frame->sequence); + goto exit; + } + + vpu_buf =to_vpu_vb2_buffer (vbuf); + vpu_buf->state =VPU_BUF_STATE_IDLE ; +if (V4L2_TYPE_IS_OUTPUT (frame->type)) +v4l2_m2m_src_buf_remove_by_buf (inst->fh.m2m_ctx, vbuf); +else +v4l2_m2m_dst_buf_remove_by_buf (inst->fh.m2m_ctx, vbuf); +v4l2_m2m_buf_done (vbuf,VB2_BUF_STATE_DONE ); +exit: +vpu_inst_unlock (inst); +} + +static void venc_set_last_buffer_dequeued (struct vpu_inst *inst) +{ +struct venc_t *venc = inst->priv; + +if (venc->stopped &&list_empty (&venc->frames)) +vpu_set_last_buffer_dequeued (inst); +} + +static void venc_stop_done (struct vpu_inst *inst) +{ +struct venc_t *venc = inst->priv; + +vpu_inst_lock (inst); + venc->stopped = true; +venc_set_last_buffer_dequeued (inst); +vpu_inst_unlock (inst); + +wake_up_all (&venc->wq); +} + +static void venc_event_notify (struct vpu_inst *inst,u32 event,void *data) +{ +} + +static void venc_release (struct vpu_inst *inst) +{ +} + +static void venc_cleanup (struct vpu_inst *inst) +{ +struct venc_t *venc; + +if (!inst) +return ; + + venc = inst->priv; +if (venc) +vfree (venc); + inst->priv =NULL ; +vfree (inst); +} + +static int venc_start_session (struct vpu_inst *inst,u32 type) +{ +struct venc_t *venc = inst->priv; +int stream_buffer_size; +int ret; + +venc_set_enable (venc, type,1 ); +if ((venc->enable &VENC_ENABLE_MASK ) !=VENC_ENABLE_MASK ) +return 0 ; + +vpu_iface_init_instance (inst); + stream_buffer_size =vpu_iface_get_stream_buffer_size (inst->core); +if (stream_buffer_size >0 ) { + inst->stream_buffer.length =max_t (u32, stream_buffer_size, venc->cpb_size *3 ); + ret =vpu_alloc_dma (inst->core, &inst->stream_buffer); +if (ret) + goto error; + + inst->use_stream_buffer = true; +vpu_iface_config_stream_buffer (inst, &inst->stream_buffer); + } + + ret =vpu_iface_set_encode_params (inst, &venc->params,0 ); +if (ret) + goto error; + ret =vpu_session_configure_codec (inst); +if (ret) + goto error; + + inst->state =VPU_CODEC_STATE_CONFIGURED ; +/*vpu_iface_config_memory_resource*/ + +/*config enc expert mode parameter*/ + ret =vpu_iface_set_encode_params (inst, &venc->params,1 ); +if (ret) + goto error; + + ret =vpu_session_start (inst); +if (ret) + goto error; + inst->state =VPU_CODEC_STATE_STARTED ; + + venc->bitrate_change = false; + venc->input_ready = true; + venc->frame_count =0 ; + venc->encode_count =0 ; + venc->ready_count =0 ; + venc->stopped = false; +vpu_process_output_buffer (inst); +if (venc->frame_count ==0 ) +dev_err (inst->dev,"[%d] there is no input when starting\n" , inst->id); + +return 0 ; +error: +venc_set_enable (venc, type,0 ); + inst->state =VPU_CODEC_STATE_DEINIT ; + +vpu_free_dma (&inst->stream_buffer); +return ret; +} + +static void venc_cleanup_mem_resource (struct vpu_inst *inst) +{ +struct venc_t *venc; +u32 i; + +WARN_ON (!inst || !inst->priv); + + venc = inst->priv; + +for (i =0 ; i <ARRAY_SIZE (venc->enc); i++) +vpu_free_dma (&venc->enc[i]); +for (i =0 ; i <ARRAY_SIZE (venc->ref); i++) +vpu_free_dma (&venc->ref[i]); +for (i =0 ; i <ARRAY_SIZE (venc->act); i++) +vpu_free_dma (&venc->act[i]); +} + +static void venc_request_mem_resource (struct vpu_inst *inst, +u32 enc_frame_size, +u32 enc_frame_num, +u32 ref_frame_size, +u32 ref_frame_num, +u32 act_frame_size, +u32 act_frame_num) +{ +struct venc_t *venc; +u32 i; +int ret; + +WARN_ON (!inst || !inst->priv || !inst->core); + + venc = inst->priv; + +if (enc_frame_num >ARRAY_SIZE (venc->enc)) { +dev_err (inst->dev,"[%d] enc num(%d) is out of range\n" , + inst->id, enc_frame_num); +return ; + } +if (ref_frame_num >ARRAY_SIZE (venc->ref)) { +dev_err (inst->dev,"[%d] ref num(%d) is out of range\n" , + inst->id, ref_frame_num); +return ; + } +if (act_frame_num >ARRAY_SIZE (venc->act)) { +dev_err (inst->dev,"[%d] act num(%d) is out of range\n" , + inst->id, act_frame_num); +return ; + } + +for (i =0 ; i < enc_frame_num; i++) { + venc->enc[i].length = enc_frame_size; + ret =vpu_alloc_dma (inst->core, &venc->enc[i]); +if (ret) { +venc_cleanup_mem_resource (inst); +return ; + } + } +for (i =0 ; i < ref_frame_num; i++) { + venc->ref[i].length = ref_frame_size; + ret =vpu_alloc_dma (inst->core, &venc->ref[i]); +if (ret) { +venc_cleanup_mem_resource (inst); +return ; + } + } +if (act_frame_num !=1 || act_frame_size > inst->act.length) { +venc_cleanup_mem_resource (inst); +return ; + } + venc->act[0 ].length = act_frame_size; + venc->act[0 ].phys = inst->act.phys; + venc->act[0 ].virt = inst->act.virt; + +for (i =0 ; i < enc_frame_num; i++) +vpu_iface_config_memory_resource (inst,MEM_RES_ENC , i, &venc->enc[i]); +for (i =0 ; i < ref_frame_num; i++) +vpu_iface_config_memory_resource (inst,MEM_RES_REF , i, &venc->ref[i]); +for (i =0 ; i < act_frame_num; i++) +vpu_iface_config_memory_resource (inst,MEM_RES_ACT , i, &venc->act[i]); +} + +static void venc_cleanup_frames (struct venc_t *venc) +{ +struct venc_frame_t *frame; +struct venc_frame_t *tmp; + +list_for_each_entry_safe (frame, tmp, &venc->frames, list) { +list_del_init (&frame->list); +vfree (frame); + } +} + +static int venc_stop_session (struct vpu_inst *inst,u32 type) +{ +struct venc_t *venc = inst->priv; + +venc_set_enable (venc, type,0 ); +if (venc->enable &VENC_ENABLE_MASK ) +return 0 ; + +if (inst->state ==VPU_CODEC_STATE_DEINIT ) +return 0 ; + +if (inst->state !=VPU_CODEC_STATE_STOP ) +venc_request_eos (inst); + +call_vop (inst, wait_prepare); +if (!wait_event_timeout (venc->wq, venc->stopped,VPU_TIMEOUT )) { +set_bit (inst->id, &inst->core->hang_mask); +vpu_session_debug (inst); + } +call_vop (inst, wait_finish); + + inst->state =VPU_CODEC_STATE_DEINIT ; +venc_cleanup_frames (inst->priv); +vpu_free_dma (&inst->stream_buffer); +venc_cleanup_mem_resource (inst); + +return 0 ; +} + +static int venc_process_output (struct vpu_inst *inst,struct vb2_buffer *vb) +{ +struct venc_t *venc = inst->priv; +struct vb2_v4l2_buffer *vbuf; +struct vpu_vb2_buffer *vpu_buf =NULL ; +u32 flags; + +if (inst->state ==VPU_CODEC_STATE_DEINIT ) +return -EINVAL ; + + vbuf =to_vb2_v4l2_buffer (vb); + vpu_buf =to_vpu_vb2_buffer (vbuf); +if (inst->state ==VPU_CODEC_STATE_STARTED ) + inst->state =VPU_CODEC_STATE_ACTIVE ; + + flags = vbuf->flags; +if (venc->request_key_frame) { + vbuf->flags |=V4L2_BUF_FLAG_KEYFRAME ; + venc->request_key_frame =0 ; + } +if (venc->bitrate_change) { +vpu_session_update_parameters (inst, &venc->params); + venc->bitrate_change = false; + } +dev_dbg (inst->dev,"[%d][INPUT TS]%32lld\n" , inst->id, vb->timestamp); +vpu_iface_input_frame (inst, vb); + vbuf->flags = flags; + venc->input_ready = false; + venc->frame_count++; + vpu_buf->state =VPU_BUF_STATE_INUSE ; + +return 0 ; +} + +static int venc_process_capture (struct vpu_inst *inst,struct vb2_buffer *vb) +{ +struct venc_t *venc; +struct venc_frame_t *frame =NULL ; +struct vb2_v4l2_buffer *vbuf; +int ret; + + venc = inst->priv; +if (list_empty (&venc->frames)) +return -EINVAL ; + + frame = list_first_entry(&venc->frames, struct venc_frame_t, list); + vbuf =to_vb2_v4l2_buffer (vb); +v4l2_m2m_dst_buf_remove_by_buf (inst->fh.m2m_ctx, vbuf); + ret =venc_get_one_encoded_frame (inst, frame, vbuf); +if (ret) +return ret; + +list_del_init (&frame->list); +vfree (frame); +return 0 ; +} + +static void venc_on_queue_empty (struct vpu_inst *inst,u32 type) +{ +struct venc_t *venc = inst->priv; + +if (V4L2_TYPE_IS_OUTPUT (type)) +return ; + +if (venc->stopped) +venc_set_last_buffer_dequeued (inst); +} + +static int venc_get_debug_info (struct vpu_inst *inst,char *str,u32 size,u32 i) +{ +struct venc_t *venc = inst->priv; +int num =-1 ; + +switch (i) { +case 0 : + num =scnprintf (str, size,"profile = %d\n" , venc->params.profile); +break ; +case 1 : + num =scnprintf (str, size,"level = %d\n" , venc->params.level); +break ; +case 2 : + num =scnprintf (str, size,"fps = %d/%d\n" , + venc->params.frame_rate.numerator, + venc->params.frame_rate.denominator); +break ; +case 3 : + num =scnprintf (str, size,"%d x %d -> %d x %d\n" , + venc->params.src_width, + venc->params.src_height, + venc->params.out_width, + venc->params.out_height); +break ; +case 4 : + num =scnprintf (str, size,"(%d, %d) %d x %d\n" , + venc->params.crop.left, + venc->params.crop.top, + venc->params.crop.width, + venc->params.crop.height); +break ; +case 5 : + num =scnprintf (str, size, +"enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n" , + venc->enable, + venc->frame_count, venc->encode_count, + venc->ready_count, + venc->stopped); +break ; +case 6 : + num =scnprintf (str, size,"gop = %d\n" , venc->params.gop_length); +break ; +case 7 : + num =scnprintf (str, size,"bframes = %d\n" , venc->params.bframes); +break ; +case 8 : + num =scnprintf (str, size,"rc: mode = %d, bitrate = %d, qp = %d\n" , + venc->params.rc_mode, + venc->params.bitrate, + venc->params.i_frame_qp); +break ; +case 9 : + num =scnprintf (str, size,"sar: enable = %d, idc = %d, %d x %d\n" , + venc->params.sar.enable, + venc->params.sar.idc, + venc->params.sar.width, + venc->params.sar.height); + +break ; +case 10 : + num =scnprintf (str, size, +"colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n" , + venc->params.color.primaries, + venc->params.color.transfer, + venc->params.color.matrix, + venc->params.color.full_range); +break ; +case 11 : + num =scnprintf (str, size,"skipped: count = %d, bytes = %d\n" , + venc->skipped_count, venc->skipped_bytes); +break ; +default : +break ; + } + +return num; +} + +static struct vpu_inst_ops venc_inst_ops = { + .ctrl_init = venc_ctrl_init, + .check_ready = venc_check_ready, + .input_done = venc_input_done, + .get_one_frame = venc_frame_encoded, + .buf_done = venc_buf_done, + .stop_done = venc_stop_done, + .event_notify = venc_event_notify, + .release = venc_release, + .cleanup = venc_cleanup, + .start = venc_start_session, + .mem_request = venc_request_mem_resource, + .stop = venc_stop_session, + .process_output = venc_process_output, + .process_capture = venc_process_capture, + .on_queue_empty = venc_on_queue_empty, + .get_debug_info = venc_get_debug_info, + .wait_prepare = vpu_inst_unlock, + .wait_finish = vpu_inst_lock, +}; + +static void venc_init (struct file *file) +{ +struct vpu_inst *inst =to_inst (file); +struct venc_t *venc; +struct v4l2_format f; +struct v4l2_streamparm parm; + + venc = inst->priv; + venc->params.qp_min =1 ; + venc->params.qp_max =51 ; + venc->params.qp_min_i =1 ; + venc->params.qp_max_i =51 ; + venc->params.bitrate_max =BITRATE_MAX ; + venc->params.bitrate_min =BITRATE_MIN ; + +memset (&f,0 ,sizeof (f)); + f.type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + f.fmt.pix_mp.pixelformat =V4L2_PIX_FMT_NV12M ; + f.fmt.pix_mp.width =1280 ; + f.fmt.pix_mp.height =720 ; + f.fmt.pix_mp.field =V4L2_FIELD_NONE ; + f.fmt.pix_mp.colorspace =V4L2_COLORSPACE_REC709 ; +venc_s_fmt (file, &inst->fh, &f); + +memset (&f,0 ,sizeof (f)); + f.type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + f.fmt.pix_mp.pixelformat =V4L2_PIX_FMT_H264 ; + f.fmt.pix_mp.width =1280 ; + f.fmt.pix_mp.height =720 ; + f.fmt.pix_mp.field =V4L2_FIELD_NONE ; +venc_s_fmt (file, &inst->fh, &f); + +memset (&parm,0 ,sizeof (parm)); + parm.type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + parm.parm.capture.timeperframe.numerator =1 ; + parm.parm.capture.timeperframe.denominator =30 ; +venc_s_parm (file, &inst->fh, &parm); +} + +static int venc_open (struct file *file) +{ +struct vpu_inst *inst; +struct venc_t *venc; +int ret; + + inst =vzalloc (sizeof (*inst)); +if (!inst) +return -ENOMEM ; + + venc =vzalloc (sizeof (*venc)); +if (!venc) { +vfree (inst); +return -ENOMEM ; + } + + inst->ops = &venc_inst_ops; + inst->formats = venc_formats; + inst->type =VPU_CORE_TYPE_ENC ; + inst->priv = venc; +INIT_LIST_HEAD (&venc->frames); +init_waitqueue_head (&venc->wq); + + ret =vpu_v4l2_open (file, inst); +if (ret) +return ret; + +venc_init (file); + +return 0 ; +} + +static const struct v4l2_file_operations venc_fops = { + .owner =THIS_MODULE , + .open = venc_open, + .release = vpu_v4l2_close, + .unlocked_ioctl = video_ioctl2, + .poll = v4l2_m2m_fop_poll, + .mmap = v4l2_m2m_fop_mmap, +}; + +const struct v4l2_ioctl_ops *venc_get_ioctl_ops (void ) +{ +return &venc_ioctl_ops; +} + +const struct v4l2_file_operations *venc_get_fops (void ) +{ +return &venc_fops; +}
Signature · 1 line
2.33.0