1ae06c70bSJeff Kirsher /* SPDX-License-Identifier: GPL-2.0 */
251dce24bSJeff Kirsher /* Copyright(c) 2013 - 2018 Intel Corporation. */
356a62fc8SJesse Brandeburg 
456a62fc8SJesse Brandeburg #ifndef _I40E_HMC_H_
556a62fc8SJesse Brandeburg #define _I40E_HMC_H_
656a62fc8SJesse Brandeburg 
7*e77220eeSIvan Vecera #include "i40e_alloc.h"
8*e77220eeSIvan Vecera #include "i40e_io.h"
9*e77220eeSIvan Vecera #include "i40e_register.h"
10*e77220eeSIvan Vecera 
1156a62fc8SJesse Brandeburg #define I40E_HMC_MAX_BP_COUNT 512
1256a62fc8SJesse Brandeburg 
1356a62fc8SJesse Brandeburg /* forward-declare the HW struct for the compiler */
1456a62fc8SJesse Brandeburg struct i40e_hw;
1556a62fc8SJesse Brandeburg 
1656a62fc8SJesse Brandeburg #define I40E_HMC_INFO_SIGNATURE		0x484D5347 /* HMSG */
1756a62fc8SJesse Brandeburg #define I40E_HMC_PD_CNT_IN_SD		512
1856a62fc8SJesse Brandeburg #define I40E_HMC_DIRECT_BP_SIZE		0x200000 /* 2M */
1956a62fc8SJesse Brandeburg #define I40E_HMC_PAGED_BP_SIZE		4096
2056a62fc8SJesse Brandeburg #define I40E_HMC_PD_BP_BUF_ALIGNMENT	4096
2156a62fc8SJesse Brandeburg 
2256a62fc8SJesse Brandeburg struct i40e_hmc_obj_info {
2356a62fc8SJesse Brandeburg 	u64 base;	/* base addr in FPM */
2456a62fc8SJesse Brandeburg 	u32 max_cnt;	/* max count available for this hmc func */
2556a62fc8SJesse Brandeburg 	u32 cnt;	/* count of objects driver actually wants to create */
2656a62fc8SJesse Brandeburg 	u64 size;	/* size in bytes of one object */
2756a62fc8SJesse Brandeburg };
2856a62fc8SJesse Brandeburg 
2956a62fc8SJesse Brandeburg enum i40e_sd_entry_type {
3056a62fc8SJesse Brandeburg 	I40E_SD_TYPE_INVALID = 0,
3156a62fc8SJesse Brandeburg 	I40E_SD_TYPE_PAGED   = 1,
3256a62fc8SJesse Brandeburg 	I40E_SD_TYPE_DIRECT  = 2
3356a62fc8SJesse Brandeburg };
3456a62fc8SJesse Brandeburg 
3556a62fc8SJesse Brandeburg struct i40e_hmc_bp {
3656a62fc8SJesse Brandeburg 	enum i40e_sd_entry_type entry_type;
3756a62fc8SJesse Brandeburg 	struct i40e_dma_mem addr; /* populate to be used by hw */
3856a62fc8SJesse Brandeburg 	u32 sd_pd_index;
3956a62fc8SJesse Brandeburg 	u32 ref_cnt;
4056a62fc8SJesse Brandeburg };
4156a62fc8SJesse Brandeburg 
4256a62fc8SJesse Brandeburg struct i40e_hmc_pd_entry {
4356a62fc8SJesse Brandeburg 	struct i40e_hmc_bp bp;
4456a62fc8SJesse Brandeburg 	u32 sd_index;
453bbf0faaSFaisal Latif 	bool rsrc_pg;
4656a62fc8SJesse Brandeburg 	bool valid;
4756a62fc8SJesse Brandeburg };
4856a62fc8SJesse Brandeburg 
4956a62fc8SJesse Brandeburg struct i40e_hmc_pd_table {
5056a62fc8SJesse Brandeburg 	struct i40e_dma_mem pd_page_addr; /* populate to be used by hw */
5156a62fc8SJesse Brandeburg 	struct i40e_hmc_pd_entry  *pd_entry; /* [512] for sw book keeping */
5256a62fc8SJesse Brandeburg 	struct i40e_virt_mem pd_entry_virt_mem; /* virt mem for pd_entry */
5356a62fc8SJesse Brandeburg 
5456a62fc8SJesse Brandeburg 	u32 ref_cnt;
5556a62fc8SJesse Brandeburg 	u32 sd_index;
5656a62fc8SJesse Brandeburg };
5756a62fc8SJesse Brandeburg 
5856a62fc8SJesse Brandeburg struct i40e_hmc_sd_entry {
5956a62fc8SJesse Brandeburg 	enum i40e_sd_entry_type entry_type;
6056a62fc8SJesse Brandeburg 	bool valid;
6156a62fc8SJesse Brandeburg 
6256a62fc8SJesse Brandeburg 	union {
6356a62fc8SJesse Brandeburg 		struct i40e_hmc_pd_table pd_table;
6456a62fc8SJesse Brandeburg 		struct i40e_hmc_bp bp;
6556a62fc8SJesse Brandeburg 	} u;
6656a62fc8SJesse Brandeburg };
6756a62fc8SJesse Brandeburg 
6856a62fc8SJesse Brandeburg struct i40e_hmc_sd_table {
6956a62fc8SJesse Brandeburg 	struct i40e_virt_mem addr; /* used to track sd_entry allocations */
7056a62fc8SJesse Brandeburg 	u32 sd_cnt;
7156a62fc8SJesse Brandeburg 	u32 ref_cnt;
7256a62fc8SJesse Brandeburg 	struct i40e_hmc_sd_entry *sd_entry; /* (sd_cnt*512) entries max */
7356a62fc8SJesse Brandeburg };
7456a62fc8SJesse Brandeburg 
7556a62fc8SJesse Brandeburg struct i40e_hmc_info {
7656a62fc8SJesse Brandeburg 	u32 signature;
7756a62fc8SJesse Brandeburg 	/* equals to pci func num for PF and dynamically allocated for VFs */
7856a62fc8SJesse Brandeburg 	u8 hmc_fn_id;
7956a62fc8SJesse Brandeburg 	u16 first_sd_index; /* index of the first available SD */
8056a62fc8SJesse Brandeburg 
8156a62fc8SJesse Brandeburg 	/* hmc objects */
8256a62fc8SJesse Brandeburg 	struct i40e_hmc_obj_info *hmc_obj;
8356a62fc8SJesse Brandeburg 	struct i40e_virt_mem hmc_obj_virt_mem;
8456a62fc8SJesse Brandeburg 	struct i40e_hmc_sd_table sd_table;
8556a62fc8SJesse Brandeburg };
8656a62fc8SJesse Brandeburg 
8756a62fc8SJesse Brandeburg #define I40E_INC_SD_REFCNT(sd_table)	((sd_table)->ref_cnt++)
8856a62fc8SJesse Brandeburg #define I40E_INC_PD_REFCNT(pd_table)	((pd_table)->ref_cnt++)
8956a62fc8SJesse Brandeburg #define I40E_INC_BP_REFCNT(bp)		((bp)->ref_cnt++)
9056a62fc8SJesse Brandeburg 
9156a62fc8SJesse Brandeburg #define I40E_DEC_SD_REFCNT(sd_table)	((sd_table)->ref_cnt--)
9256a62fc8SJesse Brandeburg #define I40E_DEC_PD_REFCNT(pd_table)	((pd_table)->ref_cnt--)
9356a62fc8SJesse Brandeburg #define I40E_DEC_BP_REFCNT(bp)		((bp)->ref_cnt--)
9456a62fc8SJesse Brandeburg 
9556a62fc8SJesse Brandeburg /**
9656a62fc8SJesse Brandeburg  * I40E_SET_PF_SD_ENTRY - marks the sd entry as valid in the hardware
9756a62fc8SJesse Brandeburg  * @hw: pointer to our hw struct
9856a62fc8SJesse Brandeburg  * @pa: pointer to physical address
9956a62fc8SJesse Brandeburg  * @sd_index: segment descriptor index
10056a62fc8SJesse Brandeburg  * @type: if sd entry is direct or paged
10156a62fc8SJesse Brandeburg  **/
10256a62fc8SJesse Brandeburg #define I40E_SET_PF_SD_ENTRY(hw, pa, sd_index, type)			\
10356a62fc8SJesse Brandeburg {									\
10456a62fc8SJesse Brandeburg 	u32 val1, val2, val3;						\
10556a62fc8SJesse Brandeburg 	val1 = (u32)(upper_32_bits(pa));				\
10656a62fc8SJesse Brandeburg 	val2 = (u32)(pa) | (I40E_HMC_MAX_BP_COUNT <<			\
10756a62fc8SJesse Brandeburg 		 I40E_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |		\
10856a62fc8SJesse Brandeburg 		((((type) == I40E_SD_TYPE_PAGED) ? 0 : 1) <<		\
10956a62fc8SJesse Brandeburg 		I40E_PFHMC_SDDATALOW_PMSDTYPE_SHIFT) |			\
11041a1d04bSJesse Brandeburg 		BIT(I40E_PFHMC_SDDATALOW_PMSDVALID_SHIFT);		\
11141a1d04bSJesse Brandeburg 	val3 = (sd_index) | BIT_ULL(I40E_PFHMC_SDCMD_PMSDWR_SHIFT);	\
11256a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATAHIGH, val1);			\
11356a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATALOW, val2);				\
11456a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDCMD, val3);				\
11556a62fc8SJesse Brandeburg }
11656a62fc8SJesse Brandeburg 
11756a62fc8SJesse Brandeburg /**
11856a62fc8SJesse Brandeburg  * I40E_CLEAR_PF_SD_ENTRY - marks the sd entry as invalid in the hardware
11956a62fc8SJesse Brandeburg  * @hw: pointer to our hw struct
12056a62fc8SJesse Brandeburg  * @sd_index: segment descriptor index
12156a62fc8SJesse Brandeburg  * @type: if sd entry is direct or paged
12256a62fc8SJesse Brandeburg  **/
12356a62fc8SJesse Brandeburg #define I40E_CLEAR_PF_SD_ENTRY(hw, sd_index, type)			\
12456a62fc8SJesse Brandeburg {									\
12556a62fc8SJesse Brandeburg 	u32 val2, val3;							\
12656a62fc8SJesse Brandeburg 	val2 = (I40E_HMC_MAX_BP_COUNT <<				\
12756a62fc8SJesse Brandeburg 		I40E_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |		\
12856a62fc8SJesse Brandeburg 		((((type) == I40E_SD_TYPE_PAGED) ? 0 : 1) <<		\
12956a62fc8SJesse Brandeburg 		I40E_PFHMC_SDDATALOW_PMSDTYPE_SHIFT);			\
13041a1d04bSJesse Brandeburg 	val3 = (sd_index) | BIT_ULL(I40E_PFHMC_SDCMD_PMSDWR_SHIFT);	\
13156a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATAHIGH, 0);				\
13256a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATALOW, val2);				\
13356a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDCMD, val3);				\
13456a62fc8SJesse Brandeburg }
13556a62fc8SJesse Brandeburg 
13656a62fc8SJesse Brandeburg /**
13756a62fc8SJesse Brandeburg  * I40E_INVALIDATE_PF_HMC_PD - Invalidates the pd cache in the hardware
13856a62fc8SJesse Brandeburg  * @hw: pointer to our hw struct
13956a62fc8SJesse Brandeburg  * @sd_idx: segment descriptor index
14056a62fc8SJesse Brandeburg  * @pd_idx: page descriptor index
14156a62fc8SJesse Brandeburg  **/
14256a62fc8SJesse Brandeburg #define I40E_INVALIDATE_PF_HMC_PD(hw, sd_idx, pd_idx)			\
14356a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_PDINV,					\
14456a62fc8SJesse Brandeburg 	    (((sd_idx) << I40E_PFHMC_PDINV_PMSDIDX_SHIFT) |		\
14556a62fc8SJesse Brandeburg 	     ((pd_idx) << I40E_PFHMC_PDINV_PMPDIDX_SHIFT)))
14656a62fc8SJesse Brandeburg 
14756a62fc8SJesse Brandeburg /**
14856a62fc8SJesse Brandeburg  * I40E_FIND_SD_INDEX_LIMIT - finds segment descriptor index limit
14956a62fc8SJesse Brandeburg  * @hmc_info: pointer to the HMC configuration information structure
15056a62fc8SJesse Brandeburg  * @type: type of HMC resources we're searching
15156a62fc8SJesse Brandeburg  * @index: starting index for the object
15256a62fc8SJesse Brandeburg  * @cnt: number of objects we're trying to create
15356a62fc8SJesse Brandeburg  * @sd_idx: pointer to return index of the segment descriptor in question
15456a62fc8SJesse Brandeburg  * @sd_limit: pointer to return the maximum number of segment descriptors
15556a62fc8SJesse Brandeburg  *
15656a62fc8SJesse Brandeburg  * This function calculates the segment descriptor index and index limit
15756a62fc8SJesse Brandeburg  * for the resource defined by i40e_hmc_rsrc_type.
15856a62fc8SJesse Brandeburg  **/
15956a62fc8SJesse Brandeburg #define I40E_FIND_SD_INDEX_LIMIT(hmc_info, type, index, cnt, sd_idx, sd_limit)\
16056a62fc8SJesse Brandeburg {									\
16156a62fc8SJesse Brandeburg 	u64 fpm_addr, fpm_limit;					\
16256a62fc8SJesse Brandeburg 	fpm_addr = (hmc_info)->hmc_obj[(type)].base +			\
16356a62fc8SJesse Brandeburg 		   (hmc_info)->hmc_obj[(type)].size * (index);		\
16456a62fc8SJesse Brandeburg 	fpm_limit = fpm_addr + (hmc_info)->hmc_obj[(type)].size * (cnt);\
16556a62fc8SJesse Brandeburg 	*(sd_idx) = (u32)(fpm_addr / I40E_HMC_DIRECT_BP_SIZE);		\
16656a62fc8SJesse Brandeburg 	*(sd_limit) = (u32)((fpm_limit - 1) / I40E_HMC_DIRECT_BP_SIZE);	\
16756a62fc8SJesse Brandeburg 	/* add one more to the limit to correct our range */		\
16856a62fc8SJesse Brandeburg 	*(sd_limit) += 1;						\
16956a62fc8SJesse Brandeburg }
17056a62fc8SJesse Brandeburg 
17156a62fc8SJesse Brandeburg /**
17256a62fc8SJesse Brandeburg  * I40E_FIND_PD_INDEX_LIMIT - finds page descriptor index limit
17356a62fc8SJesse Brandeburg  * @hmc_info: pointer to the HMC configuration information struct
17456a62fc8SJesse Brandeburg  * @type: HMC resource type we're examining
17556a62fc8SJesse Brandeburg  * @idx: starting index for the object
17656a62fc8SJesse Brandeburg  * @cnt: number of objects we're trying to create
17756a62fc8SJesse Brandeburg  * @pd_index: pointer to return page descriptor index
17856a62fc8SJesse Brandeburg  * @pd_limit: pointer to return page descriptor index limit
17956a62fc8SJesse Brandeburg  *
18056a62fc8SJesse Brandeburg  * Calculates the page descriptor index and index limit for the resource
18156a62fc8SJesse Brandeburg  * defined by i40e_hmc_rsrc_type.
18256a62fc8SJesse Brandeburg  **/
18356a62fc8SJesse Brandeburg #define I40E_FIND_PD_INDEX_LIMIT(hmc_info, type, idx, cnt, pd_index, pd_limit)\
18456a62fc8SJesse Brandeburg {									\
18556a62fc8SJesse Brandeburg 	u64 fpm_adr, fpm_limit;						\
18656a62fc8SJesse Brandeburg 	fpm_adr = (hmc_info)->hmc_obj[(type)].base +			\
18756a62fc8SJesse Brandeburg 		  (hmc_info)->hmc_obj[(type)].size * (idx);		\
18856a62fc8SJesse Brandeburg 	fpm_limit = fpm_adr + (hmc_info)->hmc_obj[(type)].size * (cnt);	\
18956a62fc8SJesse Brandeburg 	*(pd_index) = (u32)(fpm_adr / I40E_HMC_PAGED_BP_SIZE);		\
19056a62fc8SJesse Brandeburg 	*(pd_limit) = (u32)((fpm_limit - 1) / I40E_HMC_PAGED_BP_SIZE);	\
19156a62fc8SJesse Brandeburg 	/* add one more to the limit to correct our range */		\
19256a62fc8SJesse Brandeburg 	*(pd_limit) += 1;						\
19356a62fc8SJesse Brandeburg }
1945180ff13SJan Sokolowski 
1955180ff13SJan Sokolowski int i40e_add_sd_table_entry(struct i40e_hw *hw,
19656a62fc8SJesse Brandeburg 			    struct i40e_hmc_info *hmc_info,
19756a62fc8SJesse Brandeburg 			    u32 sd_index,
19856a62fc8SJesse Brandeburg 			    enum i40e_sd_entry_type type,
19956a62fc8SJesse Brandeburg 			    u64 direct_mode_sz);
2005180ff13SJan Sokolowski int i40e_add_pd_table_entry(struct i40e_hw *hw,
20156a62fc8SJesse Brandeburg 			    struct i40e_hmc_info *hmc_info,
2023bbf0faaSFaisal Latif 			    u32 pd_index,
2033bbf0faaSFaisal Latif 			    struct i40e_dma_mem *rsrc_pg);
2045180ff13SJan Sokolowski int i40e_remove_pd_bp(struct i40e_hw *hw,
20556a62fc8SJesse Brandeburg 		      struct i40e_hmc_info *hmc_info,
206467d729aSAnjali Singhai Jain 		      u32 idx);
2075180ff13SJan Sokolowski int i40e_prep_remove_sd_bp(struct i40e_hmc_info *hmc_info,
20856a62fc8SJesse Brandeburg 			   u32 idx);
2095180ff13SJan Sokolowski int i40e_remove_sd_bp_new(struct i40e_hw *hw,
21056a62fc8SJesse Brandeburg 			  struct i40e_hmc_info *hmc_info,
21156a62fc8SJesse Brandeburg 			  u32 idx, bool is_pf);
2125180ff13SJan Sokolowski int i40e_prep_remove_pd_page(struct i40e_hmc_info *hmc_info,
21356a62fc8SJesse Brandeburg 			     u32 idx);
2145180ff13SJan Sokolowski int i40e_remove_pd_page_new(struct i40e_hw *hw,
21556a62fc8SJesse Brandeburg 			    struct i40e_hmc_info *hmc_info,
21656a62fc8SJesse Brandeburg 			    u32 idx, bool is_pf);
21756a62fc8SJesse Brandeburg 
21856a62fc8SJesse Brandeburg #endif /* _I40E_HMC_H_ */
219