162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci *
362306a36Sopenharmony_ci * Copyright 2016-2022 HabanaLabs, Ltd.
462306a36Sopenharmony_ci * All Rights Reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef SECURITY_H_
962306a36Sopenharmony_ci#define SECURITY_H_
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/io-64-nonatomic-lo-hi.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistruct hl_device;
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/* special blocks */
1662306a36Sopenharmony_ci#define HL_MAX_NUM_OF_GLBL_ERR_CAUSE		10
1762306a36Sopenharmony_ci#define HL_GLBL_ERR_ADDRESS_MASK		GENMASK(11, 0)
1862306a36Sopenharmony_ci/* GLBL_ERR_ADDR register offset from the start of the block */
1962306a36Sopenharmony_ci#define HL_GLBL_ERR_ADDR_OFFSET		0xF44
2062306a36Sopenharmony_ci/* GLBL_ERR_CAUSE register offset from the start of the block */
2162306a36Sopenharmony_ci#define HL_GLBL_ERR_CAUSE_OFFSET	0xF48
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/*
2462306a36Sopenharmony_ci * struct hl_special_block_info - stores address details of a particular type of
2562306a36Sopenharmony_ci * IP block which has a SPECIAL part.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * @block_type: block type as described in every ASIC's block_types enum.
2862306a36Sopenharmony_ci * @base_addr: base address of the first block of particular type,
2962306a36Sopenharmony_ci *             e.g., address of NIC0_UMR0_0 of 'NIC_UMR' block.
3062306a36Sopenharmony_ci * @major: number of major blocks of particular type.
3162306a36Sopenharmony_ci * @minor: number of minor blocks of particular type.
3262306a36Sopenharmony_ci * @sub_minor: number of sub minor blocks of particular type.
3362306a36Sopenharmony_ci * @major_offset: address gap between 2 consecutive major blocks of particular type,
3462306a36Sopenharmony_ci *                e.g., offset between NIC0_UMR0_0 and NIC1_UMR0_0 is 0x80000.
3562306a36Sopenharmony_ci * @minor_offset: address gap between 2 consecutive minor blocks of particular type,
3662306a36Sopenharmony_ci *                e.g., offset between NIC0_UMR0_0 and NIC0_UMR1_0 is 0x20000.
3762306a36Sopenharmony_ci * @sub_minor_offset: address gap between 2 consecutive sub_minor blocks of particular
3862306a36Sopenharmony_ci *                    type, e.g., offset between NIC0_UMR0_0 and NIC0_UMR0_1 is 0x1000.
3962306a36Sopenharmony_ci *
4062306a36Sopenharmony_ci * e.g., in Gaudi2, NIC_UMR blocks can be interpreted as:
4162306a36Sopenharmony_ci * NIC<major>_UMR<minor>_<sub_minor> where major=12, minor=2, sub_minor=15.
4262306a36Sopenharmony_ci * In other words, for each of 12 major numbers (i.e 0 to 11) there are
4362306a36Sopenharmony_ci * 2 blocks with different minor numbers (i.e. 0 to 1). Again, for each minor
4462306a36Sopenharmony_ci * number there are 15 blocks with different sub_minor numbers (i.e. 0 to 14).
4562306a36Sopenharmony_ci * So different blocks are NIC0_UMR0_0, NIC0_UMR0_1, ..., NIC0_UMR1_0, ....,
4662306a36Sopenharmony_ci * NIC11_UMR1_14.
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * Struct's formatted data is located in the SOL-based auto-generated protbits headers.
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_cistruct hl_special_block_info {
5162306a36Sopenharmony_ci	int block_type;
5262306a36Sopenharmony_ci	u32 base_addr;
5362306a36Sopenharmony_ci	u32 major;
5462306a36Sopenharmony_ci	u32 minor;
5562306a36Sopenharmony_ci	u32 sub_minor;
5662306a36Sopenharmony_ci	u32 major_offset;
5762306a36Sopenharmony_ci	u32 minor_offset;
5862306a36Sopenharmony_ci	u32 sub_minor_offset;
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/*
6262306a36Sopenharmony_ci * struct hl_automated_pb_cfg - represents configurations of a particular type
6362306a36Sopenharmony_ci * of IP block which has protection bits.
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci * @addr: address details as described in hl_automation_pb_addr struct.
6662306a36Sopenharmony_ci * @prot_map: each bit corresponds to one among 32 protection configuration regs
6762306a36Sopenharmony_ci *            (e.g., SPECIAL_GLBL_PRIV). '1' means 0xffffffff and '0' means 0x0
6862306a36Sopenharmony_ci *            to be written into the corresponding protection configuration reg.
6962306a36Sopenharmony_ci *            This bit is meaningful if same bit in data_map is 0, otherwise ignored.
7062306a36Sopenharmony_ci * @data_map: each bit corresponds to one among 32 protection configuration regs
7162306a36Sopenharmony_ci *            (e.g., SPECIAL_GLBL_PRIV). '1' means corresponding protection
7262306a36Sopenharmony_ci *            configuration reg is to be written with a value in array pointed
7362306a36Sopenharmony_ci *            by 'data', otherwise the value is decided by 'prot_map'.
7462306a36Sopenharmony_ci * @data: pointer to data array which stores the config value(s) to be written
7562306a36Sopenharmony_ci *            to corresponding protection configuration reg(s).
7662306a36Sopenharmony_ci * @data_size: size of the data array.
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * Each bit of 'data_map' and 'prot_map' fields corresponds to one among 32
7962306a36Sopenharmony_ci * protection configuration registers e.g., SPECIAL GLBL PRIV regs (starting at
8062306a36Sopenharmony_ci * offset 0xE80). '1' in 'data_map' means protection configuration to be done
8162306a36Sopenharmony_ci * using configuration in data array. '0' in 'data_map" means protection
8262306a36Sopenharmony_ci * configuration to be done as per the value of corresponding bit in 'prot_map'.
8362306a36Sopenharmony_ci * '1' in 'prot_map' means the register to be programmed with 0xFFFFFFFF
8462306a36Sopenharmony_ci * (all non-protected). '0' in 'prot_map' means the register to be programmed
8562306a36Sopenharmony_ci * with 0x0 (all protected).
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * e.g., prot_map = 0x00000001, data_map = 0xC0000000 , data = {0xff, 0x12}
8862306a36Sopenharmony_ci * SPECIAL_GLBL_PRIV[0] = 0xFFFFFFFF
8962306a36Sopenharmony_ci * SPECIAL_GLBL_PRIV[1..29] = 0x0
9062306a36Sopenharmony_ci * SPECIAL_GLBL_PRIV[30] = 0xFF
9162306a36Sopenharmony_ci * SPECIAL_GLBL_PRIV[31] = 0x12
9262306a36Sopenharmony_ci */
9362306a36Sopenharmony_cistruct hl_automated_pb_cfg {
9462306a36Sopenharmony_ci	struct hl_special_block_info addr;
9562306a36Sopenharmony_ci	u32 prot_map;
9662306a36Sopenharmony_ci	u32 data_map;
9762306a36Sopenharmony_ci	const u32 *data;
9862306a36Sopenharmony_ci	u8 data_size;
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/* struct hl_special_blocks_cfg - holds special blocks cfg data.
10262306a36Sopenharmony_ci *
10362306a36Sopenharmony_ci * @priv_automated_pb_cfg: points to the main privileged PB array.
10462306a36Sopenharmony_ci * @sec_automated_pb_cfg: points to the main secured PB array.
10562306a36Sopenharmony_ci * @skip_blocks_cfg: holds arrays of block types & block ranges to be excluded.
10662306a36Sopenharmony_ci * @priv_cfg_size: size of the main privileged PB array.
10762306a36Sopenharmony_ci * @sec_cfg_size: size of the main secured PB array.
10862306a36Sopenharmony_ci * @prot_lvl_priv: indication if it's a privileged/secured PB configurations.
10962306a36Sopenharmony_ci */
11062306a36Sopenharmony_cistruct hl_special_blocks_cfg {
11162306a36Sopenharmony_ci	struct hl_automated_pb_cfg *priv_automated_pb_cfg;
11262306a36Sopenharmony_ci	struct hl_automated_pb_cfg *sec_automated_pb_cfg;
11362306a36Sopenharmony_ci	struct hl_skip_blocks_cfg *skip_blocks_cfg;
11462306a36Sopenharmony_ci	u32 priv_cfg_size;
11562306a36Sopenharmony_ci	u32 sec_cfg_size;
11662306a36Sopenharmony_ci	u8 prot_lvl_priv;
11762306a36Sopenharmony_ci};
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci/* Automated security */
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci/* struct hl_skip_blocks_cfg - holds arrays of block types & block ranges to be
12262306a36Sopenharmony_ci * excluded from special blocks configurations.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * @block_types: an array of block types NOT to be configured.
12562306a36Sopenharmony_ci * @block_types_len: len of an array of block types not to be configured.
12662306a36Sopenharmony_ci * @block_ranges: an array of block ranges not to be configured.
12762306a36Sopenharmony_ci * @block_ranges_len: len of an array of block ranges not to be configured.
12862306a36Sopenharmony_ci * @skip_block_hook: hook that will be called before initializing special blocks.
12962306a36Sopenharmony_ci */
13062306a36Sopenharmony_cistruct hl_skip_blocks_cfg {
13162306a36Sopenharmony_ci	int *block_types;
13262306a36Sopenharmony_ci	size_t block_types_len;
13362306a36Sopenharmony_ci	struct range *block_ranges;
13462306a36Sopenharmony_ci	size_t block_ranges_len;
13562306a36Sopenharmony_ci	bool (*skip_block_hook)(struct hl_device *hdev,
13662306a36Sopenharmony_ci				struct hl_special_blocks_cfg *special_blocks_cfg,
13762306a36Sopenharmony_ci				u32 blk_idx, u32 major, u32 minor, u32 sub_minor);
13862306a36Sopenharmony_ci};
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_ci/**
14162306a36Sopenharmony_ci * struct iterate_special_ctx - HW module special block iterator
14262306a36Sopenharmony_ci * @fn: function to apply to each HW module special block instance
14362306a36Sopenharmony_ci * @data: optional internal data to the function iterator
14462306a36Sopenharmony_ci */
14562306a36Sopenharmony_cistruct iterate_special_ctx {
14662306a36Sopenharmony_ci	/*
14762306a36Sopenharmony_ci	 * callback for the HW module special block iterator
14862306a36Sopenharmony_ci	 * @hdev: pointer to the habanalabs device structure
14962306a36Sopenharmony_ci	 * @block_id: block (ASIC specific definition can be dcore/hdcore)
15062306a36Sopenharmony_ci	 * @major: major block index within block_id
15162306a36Sopenharmony_ci	 * @minor: minor block index within the major block
15262306a36Sopenharmony_ci	 * @sub_minor: sub_minor block index within the minor block
15362306a36Sopenharmony_ci	 * @data: function specific data
15462306a36Sopenharmony_ci	 */
15562306a36Sopenharmony_ci	int (*fn)(struct hl_device *hdev, u32 block_id, u32 major, u32 minor,
15662306a36Sopenharmony_ci						u32 sub_minor, void *data);
15762306a36Sopenharmony_ci	void *data;
15862306a36Sopenharmony_ci};
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciint hl_iterate_special_blocks(struct hl_device *hdev, struct iterate_special_ctx *ctx);
16162306a36Sopenharmony_civoid hl_check_for_glbl_errors(struct hl_device *hdev);
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci#endif /* SECURITY_H_ */
164