1/* SPDX-License-Identifier: GPL-2.0
2 *
3 * Copyright 2016-2022 HabanaLabs, Ltd.
4 * All Rights Reserved.
5 *
6 */
7
8#ifndef SECURITY_H_
9#define SECURITY_H_
10
11#include <linux/io-64-nonatomic-lo-hi.h>
12
13struct hl_device;
14
15/* special blocks */
16#define HL_GLBL_ERR_ADDRESS_MASK	GENMASK(11, 0)
17/* GLBL_ERR_ADDR register offset from the start of the block */
18#define HL_GLBL_ERR_ADDR_OFFSET		0xF44
19/* GLBL_ERR_CAUSE register offset from the start of the block */
20#define HL_GLBL_ERR_CAUSE_OFFSET	0xF48
21
22/*
23 * struct hl_special_block_info - stores address details of a particular type of
24 * IP block which has a SPECIAL part.
25 *
26 * @block_type: block type as described in every ASIC's block_types enum.
27 * @base_addr: base address of the first block of particular type,
28 *             e.g., address of NIC0_UMR0_0 of 'NIC_UMR' block.
29 * @major: number of major blocks of particular type.
30 * @minor: number of minor blocks of particular type.
31 * @sub_minor: number of sub minor blocks of particular type.
32 * @major_offset: address gap between 2 consecutive major blocks of particular type,
33 *                e.g., offset between NIC0_UMR0_0 and NIC1_UMR0_0 is 0x80000.
34 * @minor_offset: address gap between 2 consecutive minor blocks of particular type,
35 *                e.g., offset between NIC0_UMR0_0 and NIC0_UMR1_0 is 0x20000.
36 * @sub_minor_offset: address gap between 2 consecutive sub_minor blocks of particular
37 *                    type, e.g., offset between NIC0_UMR0_0 and NIC0_UMR0_1 is 0x1000.
38 *
39 * e.g., in Gaudi2, NIC_UMR blocks can be interpreted as:
40 * NIC<major>_UMR<minor>_<sub_minor> where major=12, minor=2, sub_minor=15.
41 * In other words, for each of 12 major numbers (i.e 0 to 11) there are
42 * 2 blocks with different minor numbers (i.e. 0 to 1). Again, for each minor
43 * number there are 15 blocks with different sub_minor numbers (i.e. 0 to 14).
44 * So different blocks are NIC0_UMR0_0, NIC0_UMR0_1, ..., NIC0_UMR1_0, ....,
45 * NIC11_UMR1_14.
46 *
47 * Struct's formatted data is located in the SOL-based auto-generated protbits headers.
48 */
49struct hl_special_block_info {
50	int block_type;
51	u32 base_addr;
52	u32 major;
53	u32 minor;
54	u32 sub_minor;
55	u32 major_offset;
56	u32 minor_offset;
57	u32 sub_minor_offset;
58};
59
60/*
61 * struct hl_automated_pb_cfg - represents configurations of a particular type
62 * of IP block which has protection bits.
63 *
64 * @addr: address details as described in hl_automation_pb_addr struct.
65 * @prot_map: each bit corresponds to one among 32 protection configuration regs
66 *            (e.g., SPECIAL_GLBL_PRIV). '1' means 0xffffffff and '0' means 0x0
67 *            to be written into the corresponding protection configuration reg.
68 *            This bit is meaningful if same bit in data_map is 0, otherwise ignored.
69 * @data_map: each bit corresponds to one among 32 protection configuration regs
70 *            (e.g., SPECIAL_GLBL_PRIV). '1' means corresponding protection
71 *            configuration reg is to be written with a value in array pointed
72 *            by 'data', otherwise the value is decided by 'prot_map'.
73 * @data: pointer to data array which stores the config value(s) to be written
74 *            to corresponding protection configuration reg(s).
75 * @data_size: size of the data array.
76 *
77 * Each bit of 'data_map' and 'prot_map' fields corresponds to one among 32
78 * protection configuration registers e.g., SPECIAL GLBL PRIV regs (starting at
79 * offset 0xE80). '1' in 'data_map' means protection configuration to be done
80 * using configuration in data array. '0' in 'data_map" means protection
81 * configuration to be done as per the value of corresponding bit in 'prot_map'.
82 * '1' in 'prot_map' means the register to be programmed with 0xFFFFFFFF
83 * (all non-protected). '0' in 'prot_map' means the register to be programmed
84 * with 0x0 (all protected).
85 *
86 * e.g., prot_map = 0x00000001, data_map = 0xC0000000 , data = {0xff, 0x12}
87 * SPECIAL_GLBL_PRIV[0] = 0xFFFFFFFF
88 * SPECIAL_GLBL_PRIV[1..29] = 0x0
89 * SPECIAL_GLBL_PRIV[30] = 0xFF
90 * SPECIAL_GLBL_PRIV[31] = 0x12
91 */
92struct hl_automated_pb_cfg {
93	struct hl_special_block_info addr;
94	u32 prot_map;
95	u32 data_map;
96	const u32 *data;
97	u8 data_size;
98};
99
100/* struct hl_special_blocks_cfg - holds special blocks cfg data.
101 *
102 * @priv_automated_pb_cfg: points to the main privileged PB array.
103 * @sec_automated_pb_cfg: points to the main secured PB array.
104 * @skip_blocks_cfg: holds arrays of block types & block ranges to be excluded.
105 * @priv_cfg_size: size of the main privileged PB array.
106 * @sec_cfg_size: size of the main secured PB array.
107 * @prot_lvl_priv: indication if it's a privileged/secured PB configurations.
108 */
109struct hl_special_blocks_cfg {
110	struct hl_automated_pb_cfg *priv_automated_pb_cfg;
111	struct hl_automated_pb_cfg *sec_automated_pb_cfg;
112	struct hl_skip_blocks_cfg *skip_blocks_cfg;
113	u32 priv_cfg_size;
114	u32 sec_cfg_size;
115	u8 prot_lvl_priv;
116};
117
118/* Automated security */
119
120/* struct hl_skip_blocks_cfg - holds arrays of block types & block ranges to be
121 * excluded from special blocks configurations.
122 *
123 * @block_types: an array of block types NOT to be configured.
124 * @block_types_len: len of an array of block types not to be configured.
125 * @block_ranges: an array of block ranges not to be configured.
126 * @block_ranges_len: len of an array of block ranges not to be configured.
127 * @skip_block_hook: hook that will be called before initializing special blocks.
128 */
129struct hl_skip_blocks_cfg {
130	int *block_types;
131	size_t block_types_len;
132	struct range *block_ranges;
133	size_t block_ranges_len;
134	bool (*skip_block_hook)(struct hl_device *hdev,
135				struct hl_special_blocks_cfg *special_blocks_cfg,
136				u32 blk_idx, u32 major, u32 minor, u32 sub_minor);
137};
138
139/**
140 * struct iterate_special_ctx - HW module special block iterator
141 * @fn: function to apply to each HW module special block instance
142 * @data: optional internal data to the function iterator
143 */
144struct iterate_special_ctx {
145	/*
146	 * callback for the HW module special block iterator
147	 * @hdev: pointer to the habanalabs device structure
148	 * @block_id: block (ASIC specific definition can be dcore/hdcore)
149	 * @major: major block index within block_id
150	 * @minor: minor block index within the major block
151	 * @sub_minor: sub_minor block index within the minor block
152	 * @data: function specific data
153	 */
154	int (*fn)(struct hl_device *hdev, u32 block_id, u32 major, u32 minor,
155						u32 sub_minor, void *data);
156	void *data;
157};
158
159int hl_iterate_special_blocks(struct hl_device *hdev, struct iterate_special_ctx *ctx);
160void hl_check_for_glbl_errors(struct hl_device *hdev);
161
162#endif /* SECURITY_H_ */
163