1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020 Marvell International Ltd.
4  */
5 
6 #ifndef _CVMX_GLOBAL_RESOURCES_T_
7 #define _CVMX_GLOBAL_RESOURCES_T_
8 
9 #define CVMX_GLOBAL_RESOURCES_DATA_NAME "cvmx-global-resources"
10 
11 /*In macros below abbreviation GR stands for global resources. */
12 #define CVMX_GR_TAG_INVALID                                                                        \
13 	cvmx_get_gr_tag('i', 'n', 'v', 'a', 'l', 'i', 'd', '.', '.', '.', '.', '.', '.', '.', '.', \
14 			'.')
15 /*Tag for pko que table range. */
16 #define CVMX_GR_TAG_PKO_QUEUES                                                                     \
17 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'p', 'k', 'o', '_', 'q', 'u', 'e', 'u', 's', '.', '.', \
18 			'.')
19 /*Tag for a pko internal ports range */
20 #define CVMX_GR_TAG_PKO_IPORTS                                                                     \
21 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'p', 'k', 'o', '_', 'i', 'p', 'o', 'r', 't', '.', '.', \
22 			'.')
23 #define CVMX_GR_TAG_FPA                                                                            \
24 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'f', 'p', 'a', '.', '.', '.', '.', '.', '.', '.', '.', \
25 			'.')
26 #define CVMX_GR_TAG_FAU                                                                            \
27 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'f', 'a', 'u', '.', '.', '.', '.', '.', '.', '.', '.', \
28 			'.')
29 #define CVMX_GR_TAG_SSO_GRP(n)                                                                     \
30 	cvmx_get_gr_tag('c', 'v', 'm', '_', 's', 's', 'o', '_', '0', (n) + '0', '.', '.', '.',     \
31 			'.', '.', '.');
32 #define CVMX_GR_TAG_TIM(n)                                                                         \
33 	cvmx_get_gr_tag('c', 'v', 'm', '_', 't', 'i', 'm', '_', (n) + '0', '.', '.', '.', '.',     \
34 			'.', '.', '.')
35 #define CVMX_GR_TAG_CLUSTERS(x)                                                                    \
36 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'c', 'l', 'u', 's', 't', 'e', 'r', '_', (x + '0'),     \
37 			'.', '.', '.')
38 #define CVMX_GR_TAG_CLUSTER_GRP(x)                                                                 \
39 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'c', 'l', 'g', 'r', 'p', '_', (x + '0'), '.', '.',     \
40 			'.', '.', '.')
41 #define CVMX_GR_TAG_STYLE(x)                                                                       \
42 	cvmx_get_gr_tag('c', 'v', 'm', '_', 's', 't', 'y', 'l', 'e', '_', (x + '0'), '.', '.',     \
43 			'.', '.', '.')
44 #define CVMX_GR_TAG_QPG_ENTRY(x)                                                                   \
45 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'q', 'p', 'g', 'e', 't', '_', (x + '0'), '.', '.',     \
46 			'.', '.', '.')
47 #define CVMX_GR_TAG_BPID(x)                                                                        \
48 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'b', 'p', 'i', 'd', 's', '_', (x + '0'), '.', '.',     \
49 			'.', '.', '.')
50 #define CVMX_GR_TAG_MTAG_IDX(x)                                                                    \
51 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'm', 't', 'a', 'g', 'x', '_', (x + '0'), '.', '.',     \
52 			'.', '.', '.')
53 #define CVMX_GR_TAG_PCAM(x, y, z)                                                                  \
54 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'p', 'c', 'a', 'm', '_', (x + '0'), (y + '0'),         \
55 			(z + '0'), '.', '.', '.', '.')
56 
57 #define CVMX_GR_TAG_CIU3_IDT(_n)                                                                   \
58 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'c', 'i', 'u', '3', '_', ((_n) + '0'), '_', 'i', 'd',  \
59 			't', '.', '.')
60 
61 /* Allocation of the 512 SW INTSTs (in the  12 bit SW INTSN space) */
62 #define CVMX_GR_TAG_CIU3_SWINTSN(_n)                                                               \
63 	cvmx_get_gr_tag('c', 'v', 'm', '_', 'c', 'i', 'u', '3', '_', ((_n) + '0'), '_', 's', 'w',  \
64 			'i', 's', 'n')
65 
66 #define TAG_INIT_PART(A, B, C, D, E, F, G, H)                                                      \
67 	((((u64)(A) & 0xff) << 56) | (((u64)(B) & 0xff) << 48) | (((u64)(C) & 0xff) << 40) |             \
68 	 (((u64)(D) & 0xff) << 32) | (((u64)(E) & 0xff) << 24) | (((u64)(F) & 0xff) << 16) |             \
69 	 (((u64)(G) & 0xff) << 8) | (((u64)(H) & 0xff)))
70 
71 struct global_resource_tag {
72 	u64 lo;
73 	u64 hi;
74 };
75 
76 enum cvmx_resource_err { CVMX_RESOURCE_ALLOC_FAILED = -1, CVMX_RESOURCE_ALREADY_RESERVED = -2 };
77 
78 /*
79  * @INTERNAL
80  * Creates a tag from the specified characters.
81  */
cvmx_get_gr_tag(char a,char b,char c,char d,char e,char f,char g,char h,char i,char j,char k,char l,char m,char n,char o,char p)82 static inline struct global_resource_tag cvmx_get_gr_tag(char a, char b, char c, char d, char e,
83 							 char f, char g, char h, char i, char j,
84 							 char k, char l, char m, char n, char o,
85 							 char p)
86 {
87 	struct global_resource_tag tag;
88 
89 	tag.lo = TAG_INIT_PART(a, b, c, d, e, f, g, h);
90 	tag.hi = TAG_INIT_PART(i, j, k, l, m, n, o, p);
91 	return tag;
92 }
93 
cvmx_gr_same_tag(struct global_resource_tag gr1,struct global_resource_tag gr2)94 static inline int cvmx_gr_same_tag(struct global_resource_tag gr1, struct global_resource_tag gr2)
95 {
96 	return (gr1.hi == gr2.hi) && (gr1.lo == gr2.lo);
97 }
98 
99 /*
100  * @INTERNAL
101  * Creates a global resource range that can hold the specified number of
102  * elements
103  * @param tag is the tag of the range. The taga is created using the method
104  * cvmx_get_gr_tag()
105  * @param nelements is the number of elements to be held in the resource range.
106  */
107 int cvmx_create_global_resource_range(struct global_resource_tag tag, int nelements);
108 
109 /*
110  * @INTERNAL
111  * Allocate nelements in the global resource range with the specified tag. It
112  * is assumed that prior
113  * to calling this the global resource range has already been created using
114  * cvmx_create_global_resource_range().
115  * @param tag is the tag of the global resource range.
116  * @param nelements is the number of elements to be allocated.
117  * @param owner is a 64 bit number that identifes the owner of this range.
118  * @aligment specifes the required alignment of the returned base number.
119  * @return returns the base of the allocated range. -1 return value indicates
120  * failure.
121  */
122 int cvmx_allocate_global_resource_range(struct global_resource_tag tag, u64 owner, int nelements,
123 					int alignment);
124 
125 /*
126  * @INTERNAL
127  * Allocate nelements in the global resource range with the specified tag.
128  * The elements allocated need not be contiguous. It is assumed that prior to
129  * calling this the global resource range has already
130  * been created using cvmx_create_global_resource_range().
131  * @param tag is the tag of the global resource range.
132  * @param nelements is the number of elements to be allocated.
133  * @param owner is a 64 bit number that identifes the owner of the allocated
134  * elements.
135  * @param allocated_elements returns indexs of the allocated entries.
136  * @return returns 0 on success and -1 on failure.
137  */
138 int cvmx_resource_alloc_many(struct global_resource_tag tag, u64 owner, int nelements,
139 			     int allocated_elements[]);
140 int cvmx_resource_alloc_reverse(struct global_resource_tag, u64 owner);
141 /*
142  * @INTERNAL
143  * Reserve nelements starting from base in the global resource range with the
144  * specified tag.
145  * It is assumed that prior to calling this the global resource range has
146  * already been created using cvmx_create_global_resource_range().
147  * @param tag is the tag of the global resource range.
148  * @param nelements is the number of elements to be allocated.
149  * @param owner is a 64 bit number that identifes the owner of this range.
150  * @base specifies the base start of nelements.
151  * @return returns the base of the allocated range. -1 return value indicates
152  * failure.
153  */
154 int cvmx_reserve_global_resource_range(struct global_resource_tag tag, u64 owner, int base,
155 				       int nelements);
156 /*
157  * @INTERNAL
158  * Free nelements starting at base in the global resource range with the
159  * specified tag.
160  * @param tag is the tag of the global resource range.
161  * @param base is the base number
162  * @param nelements is the number of elements that are to be freed.
163  * @return returns 0 if successful and -1 on failure.
164  */
165 int cvmx_free_global_resource_range_with_base(struct global_resource_tag tag, int base,
166 					      int nelements);
167 
168 /*
169  * @INTERNAL
170  * Free nelements with the bases specified in bases[] with the
171  * specified tag.
172  * @param tag is the tag of the global resource range.
173  * @param bases is an array containing the bases to be freed.
174  * @param nelements is the number of elements that are to be freed.
175  * @return returns 0 if successful and -1 on failure.
176  */
177 int cvmx_free_global_resource_range_multiple(struct global_resource_tag tag, int bases[],
178 					     int nelements);
179 /*
180  * @INTERNAL
181  * Free elements from the specified owner in the global resource range with the
182  * specified tag.
183  * @param tag is the tag of the global resource range.
184  * @param owner is the owner of resources that are to be freed.
185  * @return returns 0 if successful and -1 on failure.
186  */
187 int cvmx_free_global_resource_range_with_owner(struct global_resource_tag tag, int owner);
188 
189 /*
190  * @INTERNAL
191  * Frees all the global resources that have been created.
192  * For use only from the bootloader, when it shutdown and boots up the
193  * application or kernel.
194  */
195 int free_global_resources(void);
196 
197 u64 cvmx_get_global_resource_owner(struct global_resource_tag tag, int base);
198 /*
199  * @INTERNAL
200  * Shows the global resource range with the specified tag. Use mainly for debug.
201  */
202 void cvmx_show_global_resource_range(struct global_resource_tag tag);
203 
204 /*
205  * @INTERNAL
206  * Shows all the global resources. Used mainly for debug.
207  */
208 void cvmx_global_resources_show(void);
209 
210 u64 cvmx_allocate_app_id(void);
211 u64 cvmx_get_app_id(void);
212 
213 #endif
214