#
# kernel/cipher — the Cipher (crypto) engine, for chips built by the generic
# branch of kernel/Kbuild rather than by their own <chip>.kbuild.
#
# hi3516cv500 builds this module from its own kbuild and does not come through
# here. This file exists for hi3516ev200 (and so hi3516ev300) and for
# gk7205v200 (and so gk7205v300, gk7202v300, gk7605v100), whose firmware has
# never packaged a cipher module at all: the vendor prebuilt from the
# Hi3516EV200 SDK cannot load against an OpenIPC rootfs, and there was no
# source build to fall back on.
#
# ITS OWN TREE, like every other chip under kernel/cipher/. Reusing cv500's
# source with -DCHIP_TYPE_hi3516ev200 was tried first and is a worse idea than
# it looks: cv500's tree is a later SDK vintage (the 2019 rewrite), the two
# differ by some 30k lines, and it carries no drv_osal_hi3516ev200.h at all.
# The tree here is the one from Hi3516EV200_SDK_V1.0.1.2 that was built and
# checked on an hi3516ev300 -- 600 differential AES-CTR cases byte-identical
# to mbedTLS -- so it is the version known to drive this silicon correctly.
# Only include/ and src/ are vendored; the SDK's sample/, buildin/ and patch/
# trees are not built here.
#
# WHAT IT IS FOR. AES-128-CTR at SRTP packet sizes, where a userspace streamer
# would otherwise do it on the CPU, the key ladder the recorder's chip-bound
# mode wraps clip keys with, and a TRNG. Read the next paragraph before
# assuming the first of those is a win.
#
# WHAT IS NOT BUILT, and why the object list below is shorter than the vendor's
# build.mak. The SDK tree is a whole crypto stack, and a kernel module gets no
# dead-code elimination: `ld -r` takes every object named here, reachable or
# not, so an unused subsystem is not free, it is shipped. Two of them were,
# and together they were half the module -- 39 KB of RSA (the IFEP hardware
# driver plus a software mbedTLS bignum/RSA/ASN.1 fallback compiled into the
# kernel) and 14 KB of hash (SHA-1 through SHA-512, HMAC, SM3). Neither has a
# caller: majestic is the only thing that opens /dev/cipher on these boards and
# it uses symc, klad and the TRNG -- its cipher_abi.h says why not the other
# two, the engine's HMAC-SHA1 being five times slower than mbedTLS and its RSA
# being a once-per-clip operation userspace already does. Also gone are the
# _v100 cores and the ext_* extensions, which this part's CHIP_TYPE compiles
# down to empty objects -- no bytes, but no reason to name them either.
#
# Measured, same toolchain and kernel, gk7205v200: open_cipher.ko as installed
# (INSTALL_MOD_STRIP=1) goes from 172,328 to 96,188 bytes, -44.2%, and about
# 23 KB off an xz squashfs rootfs. For scale, gk7205v300_lite failed CI on
# 2026-09-06 four kilobytes over its 5120 KB partition.
#
# The vendor sources stay in the tree untouched; drivers/kapi_hash_rsa_absent.c
# supplies the twelve entry points kapi_dispatch.c needs, and only those twelve
# -- nothing else in the module reaches into either subsystem. Restoring one is
# putting its objects back on this list and taking that file off, not a merge.
# hi3516cv500 is trimmed the same way from its own kbuild, with its own copy of
# that file: the two trees are different SDK vintages and the kapi signatures
# differ, so the boundary was re-checked there rather than assumed from here.
# The other chips that name cipher objects build no cipher module (av100,
# cv200, cv100) or build a different, small driver (hi3520dv200's V2-era
# hi_cipher, hi3519dv500's V5 security_subsys) with none of this weight.
#
# WHAT IT IS WORTH DEPENDS ON HOW IT IS DRIVEN, and this file used to claim
# more than the evidence carried. Per 1100-byte packet the engine costs half
# the CPU of mbedTLS -- 86.4 us against 42.6 on a gk7205v200, 87.6 against
# 42.0 on an hi3516ev300 -- because the driver sleeps on its completion
# interrupt instead of spinning. That figure is taken with the streamer
# stopped, and it does not survive a busy camera: on the lab gk7205v200 with
# one WebRTC viewer at 4 Mbit/s the whole-camera saving is zero, 19.76% of a
# core against 19.68%. The user time the engine saves comes back as system
# time almost exactly, because these kernels have no IRQ_TIME_ACCOUNTING and
# the completion interrupt is billed to whichever thread was running -- with
# the streamer stopped, the idle task, which is why an idle measurement never
# sees it.
#
# THE COST IS THE TRIP, NOT THE CIPHER: two ioctls, a DMA allocation, a
# hardware start and a sleep for every packet, against about 44 us of actual
# AES. Amortising that over a burst is what makes the engine pay, and it is
# a change of its own -- this one builds the module, correct and available,
# and leaves what loads it to the change that makes loading it worthwhile.
# So nothing in a Goke image modprobes it yet, and that is the state this
# commit intends rather than an oversight.
#
# gk7205v200 IS THE SAME SILICON and builds the same tree. Not "the same
# generation on paper", which is what this file used to say and what kept it
# off the Goke branch: the cipher block is at 0x10050000 on both, behind the
# same CRG word at 0x120101A0, on the same SPI 34, and gk7205v200.dtsi's node
# is byte-for-byte hi3516ev200.dtsi's but for one string -- "goke,cipher"
# where HiSilicon says "hisilicon,hisi-cipher". That single difference is the
# whole port, and it lives in cipher_initdevice.c's match table, spelled
# through the PLATFORM_NAME/HISI_PRX pair that kernel/compat/compat.h already
# keeps for exactly this.
#
# The "different userspace" was never an objection either. Nothing in a Goke
# rootfs opens /dev/cipher: majestic talks to the ioctls directly rather than
# through libhi_cipher.so, so the consumer is the same binary on both
# families and the ABI it expects is this module's, not Goke's.
#
# CHECKED ON THE SILICON, lab gk7205v200 (imx307, kernel 4.9.37): the module
# loads, /dev/cipher appears, the version register reads the same 0x2018121
# the HiSilicon part does, and 600 differential AES-CTR cases against the
# rootfs's own mbedTLS 2.25 -- 50 each at lengths 1 through 2048 -- come back
# byte-identical with nothing written past the requested length. A WebRTC
# session driven from a browser carried 1080p20 with /proc/interrupts showing
# 499 cipher completions a second against 500 packets, so the engine really
# was doing every packet rather than quietly falling back.
#

ifneq ($(filter $(CHIPARCH),hi3516ev200 gk7205v200),)

EV200_CIPHER = cipher/hi3516ev200/src

$(PREFIX)cipher-objs := \
	$(EV200_CIPHER)/drv/cipher_initdevice.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/core/drv_symc_v200.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/core/drv_trng_v200.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/core/drv_lib.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/crypto/cryp_symc.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/crypto/cryp_trng.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/kapi_symc.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/kapi_trng.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/kapi_dispatch.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/drivers/kapi_hash_rsa_absent.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/osal/drv_osal_init_linux.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/osal/drv_osal_sys_linux.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/compat/hi_drv_compat.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/compat/drv_klad.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/compat/hal_efuse.o \
	$(EV200_CIPHER)/drv/cipher_v1.0/compat/hal_otp.o

# Self-contained: hi_type.h is vendored into the tree above rather than
# borrowed from include/hi3516cv500, which looks interchangeable and is not --
# HI_PHYS_ADDR_T is signed there and unsigned here. Borrowing it also let
# include/hi3516cv500/hi_types.h shadow this tree's own, which is where the
# HI_HANDLE_* macros live.
ccflags-y += -I$(src)/cipher/hi3516ev200/include
ccflags-y += -I$(src)/cipher/hi3516ev200/src/drv/cipher_v1.0/osal/include
ccflags-y += -I$(src)/cipher/hi3516ev200/src/drv/cipher_v1.0/drivers/core/include
ccflags-y += -I$(src)/cipher/hi3516ev200/src/drv/cipher_v1.0/drivers/crypto/include
ccflags-y += -I$(src)/cipher/hi3516ev200/src/drv/cipher_v1.0/drivers/extend/include
ccflags-y += -I$(src)/cipher/hi3516ev200/src/drv/cipher_v1.0/compat
# hi3516ev200 here names the silicon and the SDK the tree came from, not the
# CHIPARCH being built: on gk7205v200 this is still the part it selects
# drv_osal_hi3516ev200.h for, and still the right one.
ccflags-y += -DCHIP_TYPE_hi3516ev200 -DAMP_NONSECURE_VERSION

obj-m += $(PREFIX)cipher.o

endif
