New upstream version 18.02
[deb_dpdk.git] / drivers / net / avf / base / avf_hmc.h
1 /*******************************************************************************
2
3 Copyright (c) 2013 - 2015, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #ifndef _AVF_HMC_H_
35 #define _AVF_HMC_H_
36
37 #define AVF_HMC_MAX_BP_COUNT 512
38
39 /* forward-declare the HW struct for the compiler */
40 struct avf_hw;
41
42 #define AVF_HMC_INFO_SIGNATURE          0x484D5347 /* HMSG */
43 #define AVF_HMC_PD_CNT_IN_SD            512
44 #define AVF_HMC_DIRECT_BP_SIZE          0x200000 /* 2M */
45 #define AVF_HMC_PAGED_BP_SIZE           4096
46 #define AVF_HMC_PD_BP_BUF_ALIGNMENT     4096
47 #define AVF_FIRST_VF_FPM_ID             16
48
49 struct avf_hmc_obj_info {
50         u64 base;       /* base addr in FPM */
51         u32 max_cnt;    /* max count available for this hmc func */
52         u32 cnt;        /* count of objects driver actually wants to create */
53         u64 size;       /* size in bytes of one object */
54 };
55
56 enum avf_sd_entry_type {
57         AVF_SD_TYPE_INVALID = 0,
58         AVF_SD_TYPE_PAGED   = 1,
59         AVF_SD_TYPE_DIRECT  = 2
60 };
61
62 struct avf_hmc_bp {
63         enum avf_sd_entry_type entry_type;
64         struct avf_dma_mem addr; /* populate to be used by hw */
65         u32 sd_pd_index;
66         u32 ref_cnt;
67 };
68
69 struct avf_hmc_pd_entry {
70         struct avf_hmc_bp bp;
71         u32 sd_index;
72         bool rsrc_pg;
73         bool valid;
74 };
75
76 struct avf_hmc_pd_table {
77         struct avf_dma_mem pd_page_addr; /* populate to be used by hw */
78         struct avf_hmc_pd_entry  *pd_entry; /* [512] for sw book keeping */
79         struct avf_virt_mem pd_entry_virt_mem; /* virt mem for pd_entry */
80
81         u32 ref_cnt;
82         u32 sd_index;
83 };
84
85 struct avf_hmc_sd_entry {
86         enum avf_sd_entry_type entry_type;
87         bool valid;
88
89         union {
90                 struct avf_hmc_pd_table pd_table;
91                 struct avf_hmc_bp bp;
92         } u;
93 };
94
95 struct avf_hmc_sd_table {
96         struct avf_virt_mem addr; /* used to track sd_entry allocations */
97         u32 sd_cnt;
98         u32 ref_cnt;
99         struct avf_hmc_sd_entry *sd_entry; /* (sd_cnt*512) entries max */
100 };
101
102 struct avf_hmc_info {
103         u32 signature;
104         /* equals to pci func num for PF and dynamically allocated for VFs */
105         u8 hmc_fn_id;
106         u16 first_sd_index; /* index of the first available SD */
107
108         /* hmc objects */
109         struct avf_hmc_obj_info *hmc_obj;
110         struct avf_virt_mem hmc_obj_virt_mem;
111         struct avf_hmc_sd_table sd_table;
112 };
113
114 #define AVF_INC_SD_REFCNT(sd_table)     ((sd_table)->ref_cnt++)
115 #define AVF_INC_PD_REFCNT(pd_table)     ((pd_table)->ref_cnt++)
116 #define AVF_INC_BP_REFCNT(bp)           ((bp)->ref_cnt++)
117
118 #define AVF_DEC_SD_REFCNT(sd_table)     ((sd_table)->ref_cnt--)
119 #define AVF_DEC_PD_REFCNT(pd_table)     ((pd_table)->ref_cnt--)
120 #define AVF_DEC_BP_REFCNT(bp)           ((bp)->ref_cnt--)
121
122 /**
123  * AVF_SET_PF_SD_ENTRY - marks the sd entry as valid in the hardware
124  * @hw: pointer to our hw struct
125  * @pa: pointer to physical address
126  * @sd_index: segment descriptor index
127  * @type: if sd entry is direct or paged
128  **/
129 #define AVF_SET_PF_SD_ENTRY(hw, pa, sd_index, type)                     \
130 {                                                                       \
131         u32 val1, val2, val3;                                           \
132         val1 = (u32)(AVF_HI_DWORD(pa));                         \
133         val2 = (u32)(pa) | (AVF_HMC_MAX_BP_COUNT <<                     \
134                  AVF_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |               \
135                 ((((type) == AVF_SD_TYPE_PAGED) ? 0 : 1) <<             \
136                 AVF_PFHMC_SDDATALOW_PMSDTYPE_SHIFT) |                   \
137                 BIT(AVF_PFHMC_SDDATALOW_PMSDVALID_SHIFT);               \
138         val3 = (sd_index) | BIT_ULL(AVF_PFHMC_SDCMD_PMSDWR_SHIFT);      \
139         wr32((hw), AVF_PFHMC_SDDATAHIGH, val1);                 \
140         wr32((hw), AVF_PFHMC_SDDATALOW, val2);                          \
141         wr32((hw), AVF_PFHMC_SDCMD, val3);                              \
142 }
143
144 /**
145  * AVF_CLEAR_PF_SD_ENTRY - marks the sd entry as invalid in the hardware
146  * @hw: pointer to our hw struct
147  * @sd_index: segment descriptor index
148  * @type: if sd entry is direct or paged
149  **/
150 #define AVF_CLEAR_PF_SD_ENTRY(hw, sd_index, type)                       \
151 {                                                                       \
152         u32 val2, val3;                                                 \
153         val2 = (AVF_HMC_MAX_BP_COUNT <<                         \
154                 AVF_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |                \
155                 ((((type) == AVF_SD_TYPE_PAGED) ? 0 : 1) <<             \
156                 AVF_PFHMC_SDDATALOW_PMSDTYPE_SHIFT);                    \
157         val3 = (sd_index) | BIT_ULL(AVF_PFHMC_SDCMD_PMSDWR_SHIFT);      \
158         wr32((hw), AVF_PFHMC_SDDATAHIGH, 0);                            \
159         wr32((hw), AVF_PFHMC_SDDATALOW, val2);                          \
160         wr32((hw), AVF_PFHMC_SDCMD, val3);                              \
161 }
162
163 /**
164  * AVF_INVALIDATE_PF_HMC_PD - Invalidates the pd cache in the hardware
165  * @hw: pointer to our hw struct
166  * @sd_idx: segment descriptor index
167  * @pd_idx: page descriptor index
168  **/
169 #define AVF_INVALIDATE_PF_HMC_PD(hw, sd_idx, pd_idx)                    \
170         wr32((hw), AVF_PFHMC_PDINV,                                     \
171             (((sd_idx) << AVF_PFHMC_PDINV_PMSDIDX_SHIFT) |              \
172              ((pd_idx) << AVF_PFHMC_PDINV_PMPDIDX_SHIFT)))
173
174 /**
175  * AVF_FIND_SD_INDEX_LIMIT - finds segment descriptor index limit
176  * @hmc_info: pointer to the HMC configuration information structure
177  * @type: type of HMC resources we're searching
178  * @index: starting index for the object
179  * @cnt: number of objects we're trying to create
180  * @sd_idx: pointer to return index of the segment descriptor in question
181  * @sd_limit: pointer to return the maximum number of segment descriptors
182  *
183  * This function calculates the segment descriptor index and index limit
184  * for the resource defined by avf_hmc_rsrc_type.
185  **/
186 #define AVF_FIND_SD_INDEX_LIMIT(hmc_info, type, index, cnt, sd_idx, sd_limit)\
187 {                                                                       \
188         u64 fpm_addr, fpm_limit;                                        \
189         fpm_addr = (hmc_info)->hmc_obj[(type)].base +                   \
190                    (hmc_info)->hmc_obj[(type)].size * (index);          \
191         fpm_limit = fpm_addr + (hmc_info)->hmc_obj[(type)].size * (cnt);\
192         *(sd_idx) = (u32)(fpm_addr / AVF_HMC_DIRECT_BP_SIZE);           \
193         *(sd_limit) = (u32)((fpm_limit - 1) / AVF_HMC_DIRECT_BP_SIZE);  \
194         /* add one more to the limit to correct our range */            \
195         *(sd_limit) += 1;                                               \
196 }
197
198 /**
199  * AVF_FIND_PD_INDEX_LIMIT - finds page descriptor index limit
200  * @hmc_info: pointer to the HMC configuration information struct
201  * @type: HMC resource type we're examining
202  * @idx: starting index for the object
203  * @cnt: number of objects we're trying to create
204  * @pd_index: pointer to return page descriptor index
205  * @pd_limit: pointer to return page descriptor index limit
206  *
207  * Calculates the page descriptor index and index limit for the resource
208  * defined by avf_hmc_rsrc_type.
209  **/
210 #define AVF_FIND_PD_INDEX_LIMIT(hmc_info, type, idx, cnt, pd_index, pd_limit)\
211 {                                                                       \
212         u64 fpm_adr, fpm_limit;                                         \
213         fpm_adr = (hmc_info)->hmc_obj[(type)].base +                    \
214                   (hmc_info)->hmc_obj[(type)].size * (idx);             \
215         fpm_limit = fpm_adr + (hmc_info)->hmc_obj[(type)].size * (cnt); \
216         *(pd_index) = (u32)(fpm_adr / AVF_HMC_PAGED_BP_SIZE);           \
217         *(pd_limit) = (u32)((fpm_limit - 1) / AVF_HMC_PAGED_BP_SIZE);   \
218         /* add one more to the limit to correct our range */            \
219         *(pd_limit) += 1;                                               \
220 }
221 enum avf_status_code avf_add_sd_table_entry(struct avf_hw *hw,
222                                               struct avf_hmc_info *hmc_info,
223                                               u32 sd_index,
224                                               enum avf_sd_entry_type type,
225                                               u64 direct_mode_sz);
226
227 enum avf_status_code avf_add_pd_table_entry(struct avf_hw *hw,
228                                               struct avf_hmc_info *hmc_info,
229                                               u32 pd_index,
230                                               struct avf_dma_mem *rsrc_pg);
231 enum avf_status_code avf_remove_pd_bp(struct avf_hw *hw,
232                                         struct avf_hmc_info *hmc_info,
233                                         u32 idx);
234 enum avf_status_code avf_prep_remove_sd_bp(struct avf_hmc_info *hmc_info,
235                                              u32 idx);
236 enum avf_status_code avf_remove_sd_bp_new(struct avf_hw *hw,
237                                             struct avf_hmc_info *hmc_info,
238                                             u32 idx, bool is_pf);
239 enum avf_status_code avf_prep_remove_pd_page(struct avf_hmc_info *hmc_info,
240                                                u32 idx);
241 enum avf_status_code avf_remove_pd_page_new(struct avf_hw *hw,
242                                               struct avf_hmc_info *hmc_info,
243                                               u32 idx, bool is_pf);
244
245 #endif /* _AVF_HMC_H_ */