18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
28c2ecf20Sopenharmony_ci/* Copyright (C) 2016-2018 Netronome Systems, Inc. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#ifndef __NFP_BPF_H__
58c2ecf20Sopenharmony_ci#define __NFP_BPF_H__ 1
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/bitfield.h>
88c2ecf20Sopenharmony_ci#include <linux/bpf.h>
98c2ecf20Sopenharmony_ci#include <linux/bpf_verifier.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/list.h>
128c2ecf20Sopenharmony_ci#include <linux/rhashtable.h>
138c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
148c2ecf20Sopenharmony_ci#include <linux/types.h>
158c2ecf20Sopenharmony_ci#include <linux/wait.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "../ccm.h"
188c2ecf20Sopenharmony_ci#include "../nfp_asm.h"
198c2ecf20Sopenharmony_ci#include "fw.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define cmsg_warn(bpf, msg...)	nn_dp_warn(&(bpf)->app->ctrl->dp, msg)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* For relocation logic use up-most byte of branch instruction as scratch
248c2ecf20Sopenharmony_ci * area.  Remember to clear this before sending instructions to HW!
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci#define OP_RELO_TYPE	0xff00000000000000ULL
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cienum nfp_relo_type {
298c2ecf20Sopenharmony_ci	RELO_NONE = 0,
308c2ecf20Sopenharmony_ci	/* standard internal jumps */
318c2ecf20Sopenharmony_ci	RELO_BR_REL,
328c2ecf20Sopenharmony_ci	/* internal jumps to parts of the outro */
338c2ecf20Sopenharmony_ci	RELO_BR_GO_OUT,
348c2ecf20Sopenharmony_ci	RELO_BR_GO_ABORT,
358c2ecf20Sopenharmony_ci	RELO_BR_GO_CALL_PUSH_REGS,
368c2ecf20Sopenharmony_ci	RELO_BR_GO_CALL_POP_REGS,
378c2ecf20Sopenharmony_ci	/* external jumps to fixed addresses */
388c2ecf20Sopenharmony_ci	RELO_BR_NEXT_PKT,
398c2ecf20Sopenharmony_ci	RELO_BR_HELPER,
408c2ecf20Sopenharmony_ci	/* immediate relocation against load address */
418c2ecf20Sopenharmony_ci	RELO_IMMED_REL,
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* To make absolute relocated branches (branches other than RELO_BR_REL)
458c2ecf20Sopenharmony_ci * distinguishable in user space dumps from normal jumps, add a large offset
468c2ecf20Sopenharmony_ci * to them.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci#define BR_OFF_RELO		15000
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cienum static_regs {
518c2ecf20Sopenharmony_ci	STATIC_REG_IMMA		= 20, /* Bank AB */
528c2ecf20Sopenharmony_ci	STATIC_REG_IMM		= 21, /* Bank AB */
538c2ecf20Sopenharmony_ci	STATIC_REG_STACK	= 22, /* Bank A */
548c2ecf20Sopenharmony_ci	STATIC_REG_PKT_LEN	= 22, /* Bank B */
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cienum pkt_vec {
588c2ecf20Sopenharmony_ci	PKT_VEC_PKT_LEN		= 0,
598c2ecf20Sopenharmony_ci	PKT_VEC_PKT_PTR		= 2,
608c2ecf20Sopenharmony_ci	PKT_VEC_QSEL_SET	= 4,
618c2ecf20Sopenharmony_ci	PKT_VEC_QSEL_VAL	= 6,
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define PKT_VEL_QSEL_SET_BIT	4
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define pv_len(np)	reg_lm(1, PKT_VEC_PKT_LEN)
678c2ecf20Sopenharmony_ci#define pv_ctm_ptr(np)	reg_lm(1, PKT_VEC_PKT_PTR)
688c2ecf20Sopenharmony_ci#define pv_qsel_set(np)	reg_lm(1, PKT_VEC_QSEL_SET)
698c2ecf20Sopenharmony_ci#define pv_qsel_val(np)	reg_lm(1, PKT_VEC_QSEL_VAL)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define stack_reg(np)	reg_a(STATIC_REG_STACK)
728c2ecf20Sopenharmony_ci#define stack_imm(np)	imm_b(np)
738c2ecf20Sopenharmony_ci#define plen_reg(np)	reg_b(STATIC_REG_PKT_LEN)
748c2ecf20Sopenharmony_ci#define pptr_reg(np)	pv_ctm_ptr(np)
758c2ecf20Sopenharmony_ci#define imm_a(np)	reg_a(STATIC_REG_IMM)
768c2ecf20Sopenharmony_ci#define imm_b(np)	reg_b(STATIC_REG_IMM)
778c2ecf20Sopenharmony_ci#define imma_a(np)	reg_a(STATIC_REG_IMMA)
788c2ecf20Sopenharmony_ci#define imma_b(np)	reg_b(STATIC_REG_IMMA)
798c2ecf20Sopenharmony_ci#define imm_both(np)	reg_both(STATIC_REG_IMM)
808c2ecf20Sopenharmony_ci#define ret_reg(np)	imm_a(np)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define NFP_BPF_ABI_FLAGS	reg_imm(0)
838c2ecf20Sopenharmony_ci#define   NFP_BPF_ABI_FLAG_MARK	1
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/**
868c2ecf20Sopenharmony_ci * struct nfp_app_bpf - bpf app priv structure
878c2ecf20Sopenharmony_ci * @app:		backpointer to the app
888c2ecf20Sopenharmony_ci * @ccm:		common control message handler data
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * @bpf_dev:		BPF offload device handle
918c2ecf20Sopenharmony_ci *
928c2ecf20Sopenharmony_ci * @cmsg_key_sz:	size of key in cmsg element array
938c2ecf20Sopenharmony_ci * @cmsg_val_sz:	size of value in cmsg element array
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci * @map_list:		list of offloaded maps
968c2ecf20Sopenharmony_ci * @maps_in_use:	number of currently offloaded maps
978c2ecf20Sopenharmony_ci * @map_elems_in_use:	number of elements allocated to offloaded maps
988c2ecf20Sopenharmony_ci *
998c2ecf20Sopenharmony_ci * @maps_neutral:	hash table of offload-neutral maps (on pointer)
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * @abi_version:	global BPF ABI version
1028c2ecf20Sopenharmony_ci * @cmsg_cache_cnt:	number of entries to read for caching
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * @adjust_head:	adjust head capability
1058c2ecf20Sopenharmony_ci * @adjust_head.flags:		extra flags for adjust head
1068c2ecf20Sopenharmony_ci * @adjust_head.off_min:	minimal packet offset within buffer required
1078c2ecf20Sopenharmony_ci * @adjust_head.off_max:	maximum packet offset within buffer required
1088c2ecf20Sopenharmony_ci * @adjust_head.guaranteed_sub:	negative adjustment guaranteed possible
1098c2ecf20Sopenharmony_ci * @adjust_head.guaranteed_add:	positive adjustment guaranteed possible
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * @maps:		map capability
1128c2ecf20Sopenharmony_ci * @maps.types:			supported map types
1138c2ecf20Sopenharmony_ci * @maps.max_maps:		max number of maps supported
1148c2ecf20Sopenharmony_ci * @maps.max_elems:		max number of entries in each map
1158c2ecf20Sopenharmony_ci * @maps.max_key_sz:		max size of map key
1168c2ecf20Sopenharmony_ci * @maps.max_val_sz:		max size of map value
1178c2ecf20Sopenharmony_ci * @maps.max_elem_sz:		max size of map entry (key + value)
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * @helpers:		helper addressess for various calls
1208c2ecf20Sopenharmony_ci * @helpers.map_lookup:		map lookup helper address
1218c2ecf20Sopenharmony_ci * @helpers.map_update:		map update helper address
1228c2ecf20Sopenharmony_ci * @helpers.map_delete:		map delete helper address
1238c2ecf20Sopenharmony_ci * @helpers.perf_event_output:	output perf event to a ring buffer
1248c2ecf20Sopenharmony_ci *
1258c2ecf20Sopenharmony_ci * @pseudo_random:	FW initialized the pseudo-random machinery (CSRs)
1268c2ecf20Sopenharmony_ci * @queue_select:	BPF can set the RX queue ID in packet vector
1278c2ecf20Sopenharmony_ci * @adjust_tail:	BPF can simply trunc packet size for adjust tail
1288c2ecf20Sopenharmony_ci * @cmsg_multi_ent:	FW can pack multiple map entries in a single cmsg
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_cistruct nfp_app_bpf {
1318c2ecf20Sopenharmony_ci	struct nfp_app *app;
1328c2ecf20Sopenharmony_ci	struct nfp_ccm ccm;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	struct bpf_offload_dev *bpf_dev;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	unsigned int cmsg_key_sz;
1378c2ecf20Sopenharmony_ci	unsigned int cmsg_val_sz;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	unsigned int cmsg_cache_cnt;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	struct list_head map_list;
1428c2ecf20Sopenharmony_ci	unsigned int maps_in_use;
1438c2ecf20Sopenharmony_ci	unsigned int map_elems_in_use;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	struct rhashtable maps_neutral;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	u32 abi_version;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	struct nfp_bpf_cap_adjust_head {
1508c2ecf20Sopenharmony_ci		u32 flags;
1518c2ecf20Sopenharmony_ci		int off_min;
1528c2ecf20Sopenharmony_ci		int off_max;
1538c2ecf20Sopenharmony_ci		int guaranteed_sub;
1548c2ecf20Sopenharmony_ci		int guaranteed_add;
1558c2ecf20Sopenharmony_ci	} adjust_head;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	struct {
1588c2ecf20Sopenharmony_ci		u32 types;
1598c2ecf20Sopenharmony_ci		u32 max_maps;
1608c2ecf20Sopenharmony_ci		u32 max_elems;
1618c2ecf20Sopenharmony_ci		u32 max_key_sz;
1628c2ecf20Sopenharmony_ci		u32 max_val_sz;
1638c2ecf20Sopenharmony_ci		u32 max_elem_sz;
1648c2ecf20Sopenharmony_ci	} maps;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	struct {
1678c2ecf20Sopenharmony_ci		u32 map_lookup;
1688c2ecf20Sopenharmony_ci		u32 map_update;
1698c2ecf20Sopenharmony_ci		u32 map_delete;
1708c2ecf20Sopenharmony_ci		u32 perf_event_output;
1718c2ecf20Sopenharmony_ci	} helpers;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	bool pseudo_random;
1748c2ecf20Sopenharmony_ci	bool queue_select;
1758c2ecf20Sopenharmony_ci	bool adjust_tail;
1768c2ecf20Sopenharmony_ci	bool cmsg_multi_ent;
1778c2ecf20Sopenharmony_ci};
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cienum nfp_bpf_map_use {
1808c2ecf20Sopenharmony_ci	NFP_MAP_UNUSED = 0,
1818c2ecf20Sopenharmony_ci	NFP_MAP_USE_READ,
1828c2ecf20Sopenharmony_ci	NFP_MAP_USE_WRITE,
1838c2ecf20Sopenharmony_ci	NFP_MAP_USE_ATOMIC_CNT,
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistruct nfp_bpf_map_word {
1878c2ecf20Sopenharmony_ci	unsigned char type		:4;
1888c2ecf20Sopenharmony_ci	unsigned char non_zero_update	:1;
1898c2ecf20Sopenharmony_ci};
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define NFP_BPF_MAP_CACHE_CNT		4U
1928c2ecf20Sopenharmony_ci#define NFP_BPF_MAP_CACHE_TIME_NS	(250 * 1000)
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/**
1958c2ecf20Sopenharmony_ci * struct nfp_bpf_map - private per-map data attached to BPF maps for offload
1968c2ecf20Sopenharmony_ci * @offmap:	pointer to the offloaded BPF map
1978c2ecf20Sopenharmony_ci * @bpf:	back pointer to bpf app private structure
1988c2ecf20Sopenharmony_ci * @tid:	table id identifying map on datapath
1998c2ecf20Sopenharmony_ci *
2008c2ecf20Sopenharmony_ci * @cache_lock:	protects @cache_blockers, @cache_to, @cache
2018c2ecf20Sopenharmony_ci * @cache_blockers:	number of ops in flight which block caching
2028c2ecf20Sopenharmony_ci * @cache_gen:	counter incremented by every blocker on exit
2038c2ecf20Sopenharmony_ci * @cache_to:	time when cache will no longer be valid (ns)
2048c2ecf20Sopenharmony_ci * @cache:	skb with cached response
2058c2ecf20Sopenharmony_ci *
2068c2ecf20Sopenharmony_ci * @l:		link on the nfp_app_bpf->map_list list
2078c2ecf20Sopenharmony_ci * @use_map:	map of how the value is used (in 4B chunks)
2088c2ecf20Sopenharmony_ci */
2098c2ecf20Sopenharmony_cistruct nfp_bpf_map {
2108c2ecf20Sopenharmony_ci	struct bpf_offloaded_map *offmap;
2118c2ecf20Sopenharmony_ci	struct nfp_app_bpf *bpf;
2128c2ecf20Sopenharmony_ci	u32 tid;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	spinlock_t cache_lock;
2158c2ecf20Sopenharmony_ci	u32 cache_blockers;
2168c2ecf20Sopenharmony_ci	u32 cache_gen;
2178c2ecf20Sopenharmony_ci	u64 cache_to;
2188c2ecf20Sopenharmony_ci	struct sk_buff *cache;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	struct list_head l;
2218c2ecf20Sopenharmony_ci	struct nfp_bpf_map_word use_map[];
2228c2ecf20Sopenharmony_ci};
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistruct nfp_bpf_neutral_map {
2258c2ecf20Sopenharmony_ci	struct rhash_head l;
2268c2ecf20Sopenharmony_ci	struct bpf_map *ptr;
2278c2ecf20Sopenharmony_ci	u32 map_id;
2288c2ecf20Sopenharmony_ci	u32 count;
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciextern const struct rhashtable_params nfp_bpf_maps_neutral_params;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistruct nfp_prog;
2348c2ecf20Sopenharmony_cistruct nfp_insn_meta;
2358c2ecf20Sopenharmony_citypedef int (*instr_cb_t)(struct nfp_prog *, struct nfp_insn_meta *);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci#define nfp_prog_first_meta(nfp_prog)					\
2388c2ecf20Sopenharmony_ci	list_first_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
2398c2ecf20Sopenharmony_ci#define nfp_prog_last_meta(nfp_prog)					\
2408c2ecf20Sopenharmony_ci	list_last_entry(&(nfp_prog)->insns, struct nfp_insn_meta, l)
2418c2ecf20Sopenharmony_ci#define nfp_meta_next(meta)	list_next_entry(meta, l)
2428c2ecf20Sopenharmony_ci#define nfp_meta_prev(meta)	list_prev_entry(meta, l)
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci/**
2458c2ecf20Sopenharmony_ci * struct nfp_bpf_reg_state - register state for calls
2468c2ecf20Sopenharmony_ci * @reg: BPF register state from latest path
2478c2ecf20Sopenharmony_ci * @var_off: for stack arg - changes stack offset on different paths
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_cistruct nfp_bpf_reg_state {
2508c2ecf20Sopenharmony_ci	struct bpf_reg_state reg;
2518c2ecf20Sopenharmony_ci	bool var_off;
2528c2ecf20Sopenharmony_ci};
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#define FLAG_INSN_IS_JUMP_DST			BIT(0)
2558c2ecf20Sopenharmony_ci#define FLAG_INSN_IS_SUBPROG_START		BIT(1)
2568c2ecf20Sopenharmony_ci#define FLAG_INSN_PTR_CALLER_STACK_FRAME	BIT(2)
2578c2ecf20Sopenharmony_ci/* Instruction is pointless, noop even on its own */
2588c2ecf20Sopenharmony_ci#define FLAG_INSN_SKIP_NOOP			BIT(3)
2598c2ecf20Sopenharmony_ci/* Instruction is optimized out based on preceding instructions */
2608c2ecf20Sopenharmony_ci#define FLAG_INSN_SKIP_PREC_DEPENDENT		BIT(4)
2618c2ecf20Sopenharmony_ci/* Instruction is optimized by the verifier */
2628c2ecf20Sopenharmony_ci#define FLAG_INSN_SKIP_VERIFIER_OPT		BIT(5)
2638c2ecf20Sopenharmony_ci/* Instruction needs to zero extend to high 32-bit */
2648c2ecf20Sopenharmony_ci#define FLAG_INSN_DO_ZEXT			BIT(6)
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci#define FLAG_INSN_SKIP_MASK		(FLAG_INSN_SKIP_NOOP | \
2678c2ecf20Sopenharmony_ci					 FLAG_INSN_SKIP_PREC_DEPENDENT | \
2688c2ecf20Sopenharmony_ci					 FLAG_INSN_SKIP_VERIFIER_OPT)
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/**
2718c2ecf20Sopenharmony_ci * struct nfp_insn_meta - BPF instruction wrapper
2728c2ecf20Sopenharmony_ci * @insn: BPF instruction
2738c2ecf20Sopenharmony_ci * @ptr: pointer type for memory operations
2748c2ecf20Sopenharmony_ci * @ldst_gather_len: memcpy length gathered from load/store sequence
2758c2ecf20Sopenharmony_ci * @paired_st: the paired store insn at the head of the sequence
2768c2ecf20Sopenharmony_ci * @ptr_not_const: pointer is not always constant
2778c2ecf20Sopenharmony_ci * @pkt_cache: packet data cache information
2788c2ecf20Sopenharmony_ci * @pkt_cache.range_start: start offset for associated packet data cache
2798c2ecf20Sopenharmony_ci * @pkt_cache.range_end: end offset for associated packet data cache
2808c2ecf20Sopenharmony_ci * @pkt_cache.do_init: this read needs to initialize packet data cache
2818c2ecf20Sopenharmony_ci * @xadd_over_16bit: 16bit immediate is not guaranteed
2828c2ecf20Sopenharmony_ci * @xadd_maybe_16bit: 16bit immediate is possible
2838c2ecf20Sopenharmony_ci * @jmp_dst: destination info for jump instructions
2848c2ecf20Sopenharmony_ci * @jump_neg_op: jump instruction has inverted immediate, use ADD instead of SUB
2858c2ecf20Sopenharmony_ci * @num_insns_after_br: number of insns following a branch jump, used for fixup
2868c2ecf20Sopenharmony_ci * @func_id: function id for call instructions
2878c2ecf20Sopenharmony_ci * @arg1: arg1 for call instructions
2888c2ecf20Sopenharmony_ci * @arg2: arg2 for call instructions
2898c2ecf20Sopenharmony_ci * @umin_src: copy of core verifier umin_value for src opearnd.
2908c2ecf20Sopenharmony_ci * @umax_src: copy of core verifier umax_value for src operand.
2918c2ecf20Sopenharmony_ci * @umin_dst: copy of core verifier umin_value for dst opearnd.
2928c2ecf20Sopenharmony_ci * @umax_dst: copy of core verifier umax_value for dst operand.
2938c2ecf20Sopenharmony_ci * @off: index of first generated machine instruction (in nfp_prog.prog)
2948c2ecf20Sopenharmony_ci * @n: eBPF instruction number
2958c2ecf20Sopenharmony_ci * @flags: eBPF instruction extra optimization flags
2968c2ecf20Sopenharmony_ci * @subprog_idx: index of subprogram to which the instruction belongs
2978c2ecf20Sopenharmony_ci * @double_cb: callback for second part of the instruction
2988c2ecf20Sopenharmony_ci * @l: link on nfp_prog->insns list
2998c2ecf20Sopenharmony_ci */
3008c2ecf20Sopenharmony_cistruct nfp_insn_meta {
3018c2ecf20Sopenharmony_ci	struct bpf_insn insn;
3028c2ecf20Sopenharmony_ci	union {
3038c2ecf20Sopenharmony_ci		/* pointer ops (ld/st/xadd) */
3048c2ecf20Sopenharmony_ci		struct {
3058c2ecf20Sopenharmony_ci			struct bpf_reg_state ptr;
3068c2ecf20Sopenharmony_ci			struct bpf_insn *paired_st;
3078c2ecf20Sopenharmony_ci			s16 ldst_gather_len;
3088c2ecf20Sopenharmony_ci			bool ptr_not_const;
3098c2ecf20Sopenharmony_ci			struct {
3108c2ecf20Sopenharmony_ci				s16 range_start;
3118c2ecf20Sopenharmony_ci				s16 range_end;
3128c2ecf20Sopenharmony_ci				bool do_init;
3138c2ecf20Sopenharmony_ci			} pkt_cache;
3148c2ecf20Sopenharmony_ci			bool xadd_over_16bit;
3158c2ecf20Sopenharmony_ci			bool xadd_maybe_16bit;
3168c2ecf20Sopenharmony_ci		};
3178c2ecf20Sopenharmony_ci		/* jump */
3188c2ecf20Sopenharmony_ci		struct {
3198c2ecf20Sopenharmony_ci			struct nfp_insn_meta *jmp_dst;
3208c2ecf20Sopenharmony_ci			bool jump_neg_op;
3218c2ecf20Sopenharmony_ci			u32 num_insns_after_br; /* only for BPF-to-BPF calls */
3228c2ecf20Sopenharmony_ci		};
3238c2ecf20Sopenharmony_ci		/* function calls */
3248c2ecf20Sopenharmony_ci		struct {
3258c2ecf20Sopenharmony_ci			u32 func_id;
3268c2ecf20Sopenharmony_ci			struct bpf_reg_state arg1;
3278c2ecf20Sopenharmony_ci			struct nfp_bpf_reg_state arg2;
3288c2ecf20Sopenharmony_ci		};
3298c2ecf20Sopenharmony_ci		/* We are interested in range info for operands of ALU
3308c2ecf20Sopenharmony_ci		 * operations. For example, shift amount, multiplicand and
3318c2ecf20Sopenharmony_ci		 * multiplier etc.
3328c2ecf20Sopenharmony_ci		 */
3338c2ecf20Sopenharmony_ci		struct {
3348c2ecf20Sopenharmony_ci			u64 umin_src;
3358c2ecf20Sopenharmony_ci			u64 umax_src;
3368c2ecf20Sopenharmony_ci			u64 umin_dst;
3378c2ecf20Sopenharmony_ci			u64 umax_dst;
3388c2ecf20Sopenharmony_ci		};
3398c2ecf20Sopenharmony_ci	};
3408c2ecf20Sopenharmony_ci	unsigned int off;
3418c2ecf20Sopenharmony_ci	unsigned short n;
3428c2ecf20Sopenharmony_ci	unsigned short flags;
3438c2ecf20Sopenharmony_ci	unsigned short subprog_idx;
3448c2ecf20Sopenharmony_ci	instr_cb_t double_cb;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	struct list_head l;
3478c2ecf20Sopenharmony_ci};
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci#define BPF_SIZE_MASK	0x18
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic inline u8 mbpf_class(const struct nfp_insn_meta *meta)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	return BPF_CLASS(meta->insn.code);
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic inline u8 mbpf_src(const struct nfp_insn_meta *meta)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	return BPF_SRC(meta->insn.code);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic inline u8 mbpf_op(const struct nfp_insn_meta *meta)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	return BPF_OP(meta->insn.code);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic inline u8 mbpf_mode(const struct nfp_insn_meta *meta)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	return BPF_MODE(meta->insn.code);
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic inline bool is_mbpf_alu(const struct nfp_insn_meta *meta)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	return mbpf_class(meta) == BPF_ALU64 || mbpf_class(meta) == BPF_ALU;
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic inline bool is_mbpf_load(const struct nfp_insn_meta *meta)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_LDX | BPF_MEM);
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic inline bool is_mbpf_jmp32(const struct nfp_insn_meta *meta)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	return mbpf_class(meta) == BPF_JMP32;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic inline bool is_mbpf_jmp64(const struct nfp_insn_meta *meta)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	return mbpf_class(meta) == BPF_JMP;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic inline bool is_mbpf_jmp(const struct nfp_insn_meta *meta)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	return is_mbpf_jmp32(meta) || is_mbpf_jmp64(meta);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic inline bool is_mbpf_store(const struct nfp_insn_meta *meta)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_MEM);
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic inline bool is_mbpf_load_pkt(const struct nfp_insn_meta *meta)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	return is_mbpf_load(meta) && meta->ptr.type == PTR_TO_PACKET;
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic inline bool is_mbpf_store_pkt(const struct nfp_insn_meta *meta)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	return is_mbpf_store(meta) && meta->ptr.type == PTR_TO_PACKET;
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic inline bool is_mbpf_classic_load(const struct nfp_insn_meta *meta)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	u8 code = meta->insn.code;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	return BPF_CLASS(code) == BPF_LD &&
4168c2ecf20Sopenharmony_ci	       (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND);
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic inline bool is_mbpf_classic_store(const struct nfp_insn_meta *meta)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	u8 code = meta->insn.code;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	return BPF_CLASS(code) == BPF_ST && BPF_MODE(code) == BPF_MEM;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic inline bool is_mbpf_classic_store_pkt(const struct nfp_insn_meta *meta)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	return is_mbpf_classic_store(meta) && meta->ptr.type == PTR_TO_PACKET;
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic inline bool is_mbpf_xadd(const struct nfp_insn_meta *meta)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	return (meta->insn.code & ~BPF_SIZE_MASK) == (BPF_STX | BPF_XADD);
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic inline bool is_mbpf_mul(const struct nfp_insn_meta *meta)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_MUL;
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic inline bool is_mbpf_div(const struct nfp_insn_meta *meta)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	return is_mbpf_alu(meta) && mbpf_op(meta) == BPF_DIV;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic inline bool is_mbpf_cond_jump(const struct nfp_insn_meta *meta)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	u8 op;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (is_mbpf_jmp32(meta))
4518c2ecf20Sopenharmony_ci		return true;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	if (!is_mbpf_jmp64(meta))
4548c2ecf20Sopenharmony_ci		return false;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	op = mbpf_op(meta);
4578c2ecf20Sopenharmony_ci	return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic inline bool is_mbpf_helper_call(const struct nfp_insn_meta *meta)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	struct bpf_insn insn = meta->insn;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	return insn.code == (BPF_JMP | BPF_CALL) &&
4658c2ecf20Sopenharmony_ci		insn.src_reg != BPF_PSEUDO_CALL;
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic inline bool is_mbpf_pseudo_call(const struct nfp_insn_meta *meta)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	struct bpf_insn insn = meta->insn;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	return insn.code == (BPF_JMP | BPF_CALL) &&
4738c2ecf20Sopenharmony_ci		insn.src_reg == BPF_PSEUDO_CALL;
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci#define STACK_FRAME_ALIGN 64
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci/**
4798c2ecf20Sopenharmony_ci * struct nfp_bpf_subprog_info - nfp BPF sub-program (a.k.a. function) info
4808c2ecf20Sopenharmony_ci * @stack_depth:	maximum stack depth used by this sub-program
4818c2ecf20Sopenharmony_ci * @needs_reg_push:	whether sub-program uses callee-saved registers
4828c2ecf20Sopenharmony_ci */
4838c2ecf20Sopenharmony_cistruct nfp_bpf_subprog_info {
4848c2ecf20Sopenharmony_ci	u16 stack_depth;
4858c2ecf20Sopenharmony_ci	u8 needs_reg_push : 1;
4868c2ecf20Sopenharmony_ci};
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci/**
4898c2ecf20Sopenharmony_ci * struct nfp_prog - nfp BPF program
4908c2ecf20Sopenharmony_ci * @bpf: backpointer to the bpf app priv structure
4918c2ecf20Sopenharmony_ci * @prog: machine code
4928c2ecf20Sopenharmony_ci * @prog_len: number of valid instructions in @prog array
4938c2ecf20Sopenharmony_ci * @__prog_alloc_len: alloc size of @prog array
4948c2ecf20Sopenharmony_ci * @stack_size: total amount of stack used
4958c2ecf20Sopenharmony_ci * @verifier_meta: temporary storage for verifier's insn meta
4968c2ecf20Sopenharmony_ci * @type: BPF program type
4978c2ecf20Sopenharmony_ci * @last_bpf_off: address of the last instruction translated from BPF
4988c2ecf20Sopenharmony_ci * @tgt_out: jump target for normal exit
4998c2ecf20Sopenharmony_ci * @tgt_abort: jump target for abort (e.g. access outside of packet buffer)
5008c2ecf20Sopenharmony_ci * @tgt_call_push_regs: jump target for subroutine for saving R6~R9 to stack
5018c2ecf20Sopenharmony_ci * @tgt_call_pop_regs: jump target for subroutine used for restoring R6~R9
5028c2ecf20Sopenharmony_ci * @n_translated: number of successfully translated instructions (for errors)
5038c2ecf20Sopenharmony_ci * @error: error code if something went wrong
5048c2ecf20Sopenharmony_ci * @stack_frame_depth: max stack depth for current frame
5058c2ecf20Sopenharmony_ci * @adjust_head_location: if program has single adjust head call - the insn no.
5068c2ecf20Sopenharmony_ci * @map_records_cnt: the number of map pointers recorded for this prog
5078c2ecf20Sopenharmony_ci * @subprog_cnt: number of sub-programs, including main function
5088c2ecf20Sopenharmony_ci * @map_records: the map record pointers from bpf->maps_neutral
5098c2ecf20Sopenharmony_ci * @subprog: pointer to an array of objects holding info about sub-programs
5108c2ecf20Sopenharmony_ci * @n_insns: number of instructions on @insns list
5118c2ecf20Sopenharmony_ci * @insns: list of BPF instruction wrappers (struct nfp_insn_meta)
5128c2ecf20Sopenharmony_ci */
5138c2ecf20Sopenharmony_cistruct nfp_prog {
5148c2ecf20Sopenharmony_ci	struct nfp_app_bpf *bpf;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	u64 *prog;
5178c2ecf20Sopenharmony_ci	unsigned int prog_len;
5188c2ecf20Sopenharmony_ci	unsigned int __prog_alloc_len;
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	unsigned int stack_size;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	struct nfp_insn_meta *verifier_meta;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	enum bpf_prog_type type;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	unsigned int last_bpf_off;
5278c2ecf20Sopenharmony_ci	unsigned int tgt_out;
5288c2ecf20Sopenharmony_ci	unsigned int tgt_abort;
5298c2ecf20Sopenharmony_ci	unsigned int tgt_call_push_regs;
5308c2ecf20Sopenharmony_ci	unsigned int tgt_call_pop_regs;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	unsigned int n_translated;
5338c2ecf20Sopenharmony_ci	int error;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	unsigned int stack_frame_depth;
5368c2ecf20Sopenharmony_ci	unsigned int adjust_head_location;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	unsigned int map_records_cnt;
5398c2ecf20Sopenharmony_ci	unsigned int subprog_cnt;
5408c2ecf20Sopenharmony_ci	struct nfp_bpf_neutral_map **map_records;
5418c2ecf20Sopenharmony_ci	struct nfp_bpf_subprog_info *subprog;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	unsigned int n_insns;
5448c2ecf20Sopenharmony_ci	struct list_head insns;
5458c2ecf20Sopenharmony_ci};
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci/**
5488c2ecf20Sopenharmony_ci * struct nfp_bpf_vnic - per-vNIC BPF priv structure
5498c2ecf20Sopenharmony_ci * @tc_prog:	currently loaded cls_bpf program
5508c2ecf20Sopenharmony_ci * @start_off:	address of the first instruction in the memory
5518c2ecf20Sopenharmony_ci * @tgt_done:	jump target to get the next packet
5528c2ecf20Sopenharmony_ci */
5538c2ecf20Sopenharmony_cistruct nfp_bpf_vnic {
5548c2ecf20Sopenharmony_ci	struct bpf_prog *tc_prog;
5558c2ecf20Sopenharmony_ci	unsigned int start_off;
5568c2ecf20Sopenharmony_ci	unsigned int tgt_done;
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cibool nfp_is_subprog_start(struct nfp_insn_meta *meta);
5608c2ecf20Sopenharmony_civoid nfp_bpf_jit_prepare(struct nfp_prog *nfp_prog);
5618c2ecf20Sopenharmony_ciint nfp_bpf_jit(struct nfp_prog *prog);
5628c2ecf20Sopenharmony_cibool nfp_bpf_supported_opcode(u8 code);
5638c2ecf20Sopenharmony_cibool nfp_bpf_offload_check_mtu(struct nfp_net *nn, struct bpf_prog *prog,
5648c2ecf20Sopenharmony_ci			       unsigned int mtu);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ciint nfp_verify_insn(struct bpf_verifier_env *env, int insn_idx,
5678c2ecf20Sopenharmony_ci		    int prev_insn_idx);
5688c2ecf20Sopenharmony_ciint nfp_bpf_finalize(struct bpf_verifier_env *env);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ciint nfp_bpf_opt_replace_insn(struct bpf_verifier_env *env, u32 off,
5718c2ecf20Sopenharmony_ci			     struct bpf_insn *insn);
5728c2ecf20Sopenharmony_ciint nfp_bpf_opt_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ciextern const struct bpf_prog_offload_ops nfp_bpf_dev_ops;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistruct netdev_bpf;
5778c2ecf20Sopenharmony_cistruct nfp_app;
5788c2ecf20Sopenharmony_cistruct nfp_net;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ciint nfp_ndo_bpf(struct nfp_app *app, struct nfp_net *nn,
5818c2ecf20Sopenharmony_ci		struct netdev_bpf *bpf);
5828c2ecf20Sopenharmony_ciint nfp_net_bpf_offload(struct nfp_net *nn, struct bpf_prog *prog,
5838c2ecf20Sopenharmony_ci			bool old_prog, struct netlink_ext_ack *extack);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistruct nfp_insn_meta *
5868c2ecf20Sopenharmony_cinfp_bpf_goto_meta(struct nfp_prog *nfp_prog, struct nfp_insn_meta *meta,
5878c2ecf20Sopenharmony_ci		  unsigned int insn_idx);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_civoid *nfp_bpf_relo_for_vnic(struct nfp_prog *nfp_prog, struct nfp_bpf_vnic *bv);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ciunsigned int nfp_bpf_ctrl_cmsg_min_mtu(struct nfp_app_bpf *bpf);
5928c2ecf20Sopenharmony_ciunsigned int nfp_bpf_ctrl_cmsg_mtu(struct nfp_app_bpf *bpf);
5938c2ecf20Sopenharmony_ciunsigned int nfp_bpf_ctrl_cmsg_cache_cnt(struct nfp_app_bpf *bpf);
5948c2ecf20Sopenharmony_cilong long int
5958c2ecf20Sopenharmony_cinfp_bpf_ctrl_alloc_map(struct nfp_app_bpf *bpf, struct bpf_map *map);
5968c2ecf20Sopenharmony_civoid
5978c2ecf20Sopenharmony_cinfp_bpf_ctrl_free_map(struct nfp_app_bpf *bpf, struct nfp_bpf_map *nfp_map);
5988c2ecf20Sopenharmony_ciint nfp_bpf_ctrl_getfirst_entry(struct bpf_offloaded_map *offmap,
5998c2ecf20Sopenharmony_ci				void *next_key);
6008c2ecf20Sopenharmony_ciint nfp_bpf_ctrl_update_entry(struct bpf_offloaded_map *offmap,
6018c2ecf20Sopenharmony_ci			      void *key, void *value, u64 flags);
6028c2ecf20Sopenharmony_ciint nfp_bpf_ctrl_del_entry(struct bpf_offloaded_map *offmap, void *key);
6038c2ecf20Sopenharmony_ciint nfp_bpf_ctrl_lookup_entry(struct bpf_offloaded_map *offmap,
6048c2ecf20Sopenharmony_ci			      void *key, void *value);
6058c2ecf20Sopenharmony_ciint nfp_bpf_ctrl_getnext_entry(struct bpf_offloaded_map *offmap,
6068c2ecf20Sopenharmony_ci			       void *key, void *next_key);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ciint nfp_bpf_event_output(struct nfp_app_bpf *bpf, const void *data,
6098c2ecf20Sopenharmony_ci			 unsigned int len);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_civoid nfp_bpf_ctrl_msg_rx(struct nfp_app *app, struct sk_buff *skb);
6128c2ecf20Sopenharmony_civoid
6138c2ecf20Sopenharmony_cinfp_bpf_ctrl_msg_rx_raw(struct nfp_app *app, const void *data,
6148c2ecf20Sopenharmony_ci			unsigned int len);
6158c2ecf20Sopenharmony_ci#endif
616