]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/rmi/msgring.h
sys: further adoption of SPDX licensing ID tags.
[FreeBSD/FreeBSD.git] / sys / mips / rmi / msgring.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2003-2009 RMI Corporation
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
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  * 3. Neither the name of RMI Corporation, nor the names of its contributors,
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * RMI_BSD
32  * $FreeBSD$
33  */
34 #ifndef _RMI_MSGRING_H_
35 #define _RMI_MSGRING_H_
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40
41 #include <machine/cpuregs.h>
42 #include <machine/cpufunc.h>
43 #include <mips/rmi/rmi_mips_exts.h>
44
45 #define MSGRNG_TX_BUF_REG       0
46 #define MSGRNG_RX_BUF_REG       1
47 #define MSGRNG_MSG_STATUS_REG   2
48 #define MSGRNG_MSG_CONFIG_REG   3
49 #define MSGRNG_MSG_BUCKSIZE_REG 4
50
51 #define MSGRNG_CC_0_REG         16
52 #define MSGRNG_CC_1_REG         17
53 #define MSGRNG_CC_2_REG         18
54 #define MSGRNG_CC_3_REG         19
55 #define MSGRNG_CC_4_REG         20
56 #define MSGRNG_CC_5_REG         21
57 #define MSGRNG_CC_6_REG         22
58 #define MSGRNG_CC_7_REG         23
59 #define MSGRNG_CC_8_REG         24
60 #define MSGRNG_CC_9_REG         25
61 #define MSGRNG_CC_10_REG        26
62 #define MSGRNG_CC_11_REG        27
63 #define MSGRNG_CC_12_REG        28
64 #define MSGRNG_CC_13_REG        29
65 #define MSGRNG_CC_14_REG        30
66 #define MSGRNG_CC_15_REG        31
67
68 /* Station IDs */
69 #define MSGRNG_STNID_CPU0       0x00
70 #define MSGRNG_STNID_CPU1       0x08
71 #define MSGRNG_STNID_CPU2       0x10
72 #define MSGRNG_STNID_CPU3       0x18
73 #define MSGRNG_STNID_CPU4       0x20
74 #define MSGRNG_STNID_CPU5       0x28
75 #define MSGRNG_STNID_CPU6       0x30
76 #define MSGRNG_STNID_CPU7       0x38
77 #define MSGRNG_STNID_XGS0_TX    64
78 #define MSGRNG_STNID_XMAC0_00_TX        64
79 #define MSGRNG_STNID_XMAC0_01_TX        65
80 #define MSGRNG_STNID_XMAC0_02_TX        66
81 #define MSGRNG_STNID_XMAC0_03_TX        67
82 #define MSGRNG_STNID_XMAC0_04_TX        68
83 #define MSGRNG_STNID_XMAC0_05_TX        69
84 #define MSGRNG_STNID_XMAC0_06_TX        70
85 #define MSGRNG_STNID_XMAC0_07_TX        71
86 #define MSGRNG_STNID_XMAC0_08_TX        72
87 #define MSGRNG_STNID_XMAC0_09_TX        73
88 #define MSGRNG_STNID_XMAC0_10_TX        74
89 #define MSGRNG_STNID_XMAC0_11_TX        75
90 #define MSGRNG_STNID_XMAC0_12_TX        76
91 #define MSGRNG_STNID_XMAC0_13_TX        77
92 #define MSGRNG_STNID_XMAC0_14_TX        78
93 #define MSGRNG_STNID_XMAC0_15_TX        79
94
95 #define MSGRNG_STNID_XGS1_TX            80
96 #define MSGRNG_STNID_XMAC1_00_TX        80
97 #define MSGRNG_STNID_XMAC1_01_TX        81
98 #define MSGRNG_STNID_XMAC1_02_TX        82
99 #define MSGRNG_STNID_XMAC1_03_TX        83
100 #define MSGRNG_STNID_XMAC1_04_TX        84
101 #define MSGRNG_STNID_XMAC1_05_TX        85
102 #define MSGRNG_STNID_XMAC1_06_TX        86
103 #define MSGRNG_STNID_XMAC1_07_TX        87
104 #define MSGRNG_STNID_XMAC1_08_TX        88
105 #define MSGRNG_STNID_XMAC1_09_TX        89
106 #define MSGRNG_STNID_XMAC1_10_TX        90
107 #define MSGRNG_STNID_XMAC1_11_TX        91
108 #define MSGRNG_STNID_XMAC1_12_TX        92
109 #define MSGRNG_STNID_XMAC1_13_TX        93
110 #define MSGRNG_STNID_XMAC1_14_TX        94
111 #define MSGRNG_STNID_XMAC1_15_TX        95
112
113 #define MSGRNG_STNID_GMAC               96
114 #define MSGRNG_STNID_GMACJFR_0          96
115 #define MSGRNG_STNID_GMACRFR_0          97
116 #define MSGRNG_STNID_GMACTX0            98
117 #define MSGRNG_STNID_GMACTX1            99
118 #define MSGRNG_STNID_GMACTX2            100
119 #define MSGRNG_STNID_GMACTX3            101
120 #define MSGRNG_STNID_GMACJFR_1          102
121 #define MSGRNG_STNID_GMACRFR_1          103
122
123 #define MSGRNG_STNID_DMA                104
124 #define MSGRNG_STNID_DMA_0              104
125 #define MSGRNG_STNID_DMA_1              105
126 #define MSGRNG_STNID_DMA_2              106
127 #define MSGRNG_STNID_DMA_3              107
128
129 #define MSGRNG_STNID_XGS0FR             112
130 #define MSGRNG_STNID_XMAC0JFR           112
131 #define MSGRNG_STNID_XMAC0RFR           113
132
133 #define MSGRNG_STNID_XGS1FR             114
134 #define MSGRNG_STNID_XMAC1JFR           114
135 #define MSGRNG_STNID_XMAC1RFR           115
136 #define MSGRNG_STNID_SEC                120
137 #define MSGRNG_STNID_SEC0               120
138 #define MSGRNG_STNID_SEC1               121
139 #define MSGRNG_STNID_SEC2               122
140 #define MSGRNG_STNID_SEC3               123
141 #define MSGRNG_STNID_PK0                124
142 #define MSGRNG_STNID_SEC_RSA            124
143 #define MSGRNG_STNID_SEC_RSVD0          125
144 #define MSGRNG_STNID_SEC_RSVD1          126
145 #define MSGRNG_STNID_SEC_RSVD2          127
146
147 #define MSGRNG_STNID_GMAC1              80
148 #define MSGRNG_STNID_GMAC1_FR_0         81
149 #define MSGRNG_STNID_GMAC1_TX0          82
150 #define MSGRNG_STNID_GMAC1_TX1          83
151 #define MSGRNG_STNID_GMAC1_TX2          84
152 #define MSGRNG_STNID_GMAC1_TX3          85
153 #define MSGRNG_STNID_GMAC1_FR_1         87
154 #define MSGRNG_STNID_GMAC0              96
155 #define MSGRNG_STNID_GMAC0_FR_0         97
156 #define MSGRNG_STNID_GMAC0_TX0          98
157 #define MSGRNG_STNID_GMAC0_TX1          99
158 #define MSGRNG_STNID_GMAC0_TX2          100
159 #define MSGRNG_STNID_GMAC0_TX3          101
160 #define MSGRNG_STNID_GMAC0_FR_1         103
161 #define MSGRNG_STNID_CMP_0              108
162 #define MSGRNG_STNID_CMP_1              109
163 #define MSGRNG_STNID_CMP_2              110
164 #define MSGRNG_STNID_CMP_3              111
165 #define MSGRNG_STNID_PCIE_0             116
166 #define MSGRNG_STNID_PCIE_1             117
167 #define MSGRNG_STNID_PCIE_2             118
168 #define MSGRNG_STNID_PCIE_3             119
169 #define MSGRNG_STNID_XLS_PK0            121
170
171 #define MSGRNG_CODE_MAC         0
172 #define MSGRNG_CODE_XGMAC       2
173 #define MSGRNG_CODE_SEC         0
174 #define MSGRNG_CODE_BOOT_WAKEUP 200
175 #define MSGRNG_CODE_SPI4        3
176
177 #define msgrng_read_status()    read_c2_register32(MSGRNG_MSG_STATUS_REG, 0)
178 #define msgrng_read_config()    read_c2_register32(MSGRNG_MSG_CONFIG_REG, 0)
179 #define msgrng_write_config(v)  write_c2_register32(MSGRNG_MSG_CONFIG_REG, 0, v)
180 #define msgrng_read_bucksize(b) read_c2_register32(MSGRNG_MSG_BUCKSIZE_REG, b)
181 #define msgrng_write_bucksize(b, v)     write_c2_register32(MSGRNG_MSG_BUCKSIZE_REG, b, v)
182 #define msgrng_read_cc(r, s)    read_c2_register32(r, s)
183 #define msgrng_write_cc(r, v, s)        write_c2_register32(r, s, v)
184
185 #define msgrng_load_rx_msg0()   read_c2_register64(MSGRNG_RX_BUF_REG, 0)
186 #define msgrng_load_rx_msg1()   read_c2_register64(MSGRNG_RX_BUF_REG, 1)
187 #define msgrng_load_rx_msg2()   read_c2_register64(MSGRNG_RX_BUF_REG, 2)
188 #define msgrng_load_rx_msg3()   read_c2_register64(MSGRNG_RX_BUF_REG, 3)
189
190 #define msgrng_load_tx_msg0(v)  write_c2_register64(MSGRNG_TX_BUF_REG, 0, v)
191 #define msgrng_load_tx_msg1(v)  write_c2_register64(MSGRNG_TX_BUF_REG, 1, v)
192 #define msgrng_load_tx_msg2(v)  write_c2_register64(MSGRNG_TX_BUF_REG, 2, v)
193 #define msgrng_load_tx_msg3(v)  write_c2_register64(MSGRNG_TX_BUF_REG, 3, v)
194
195 static __inline void 
196 msgrng_send(unsigned int stid)
197 {
198         __asm__ volatile (
199             ".set       push\n"
200             ".set       noreorder\n"
201             "move       $8, %0\n"
202             "c2         0x80001\n"      /* msgsnd $8 */
203             ".set       pop\n"
204             :: "r" (stid): "$8"
205         );
206 }
207
208 static __inline void 
209 msgrng_receive(unsigned int pri)
210 {
211         __asm__ volatile (
212             ".set       push\n"
213             ".set       noreorder\n"
214             "move       $8, %0\n"
215             "c2         0x80002\n"    /* msgld $8 */
216             ".set       pop\n"
217             :: "r" (pri): "$8"
218         );
219 }
220
221 static __inline void 
222 msgrng_wait(unsigned int mask)
223 {
224         __asm__ volatile (
225             ".set       push\n"
226             ".set       noreorder\n"
227             "move       $8, %0\n"
228             "c2         0x80003\n"    /* msgwait $8 */
229             ".set       pop\n"
230             :: "r" (mask): "$8"
231         );
232 }
233
234 static __inline uint32_t
235 msgrng_access_enable(void)
236 {
237         uint32_t sr = mips_rd_status();
238
239         mips_wr_status((sr & ~MIPS_SR_INT_IE) | MIPS_SR_COP_2_BIT);
240         return (sr);
241 }
242
243 static __inline void
244 msgrng_restore(uint32_t sr)
245 {
246
247         mips_wr_status(sr);
248 }
249
250 struct msgrng_msg {
251         uint64_t msg0;
252         uint64_t msg1;
253         uint64_t msg2;
254         uint64_t msg3;
255 };
256
257 static __inline int
258 message_send(unsigned int size, unsigned int code,
259     unsigned int stid, struct msgrng_msg *msg)
260 {
261         unsigned int dest = 0;
262         unsigned long long status = 0;
263         int i = 0;
264
265         /* 
266          * Make sure that all the writes pending at the cpu are flushed.
267          * Any writes pending on CPU will not be see by devices. L1/L2
268          * caches are coherent with IO, so no cache flush needed.
269          */ 
270         __asm __volatile ("sync");
271
272         /* Load TX message buffers */
273         msgrng_load_tx_msg0(msg->msg0);
274         msgrng_load_tx_msg1(msg->msg1);
275         msgrng_load_tx_msg2(msg->msg2);
276         msgrng_load_tx_msg3(msg->msg3);
277         dest = ((size - 1) << 16) | (code << 8) | stid;
278
279         /*
280          * Retry a few times on credit fail, this should be a 
281          * transient condition, unless there is a configuration
282          * failure, or the receiver is stuck.
283          */
284         for (i = 0; i < 8; i++) {
285                 msgrng_send(dest);
286                 status = msgrng_read_status();
287                 KASSERT((status & 0x2) == 0, ("Send pending fail!"));
288                 if ((status & 0x4) == 0)
289                         return (0);
290         }
291
292         /* If there is a credit failure, return error */
293         return (status & 0x06);
294 }
295
296 static __inline int 
297 message_receive(int bucket, int *size, int *code, int *stid,
298     struct msgrng_msg *msg)
299 {
300         uint32_t status = 0, tmp = 0;
301        
302         msgrng_receive(bucket); 
303
304         /* wait for load pending to clear */
305         do {
306            status = msgrng_read_status();
307         } while ((status & 0x08) != 0);
308
309         /* receive error bits */
310         tmp = status & 0x30;
311         if (tmp != 0)
312                 return (tmp);
313
314         *size = ((status & 0xc0) >> 6) + 1;
315         *code = (status & 0xff00) >> 8;
316         *stid = (status & 0x7f0000) >> 16;
317         msg->msg0 = msgrng_load_rx_msg0();
318         msg->msg1 = msgrng_load_rx_msg1();
319         msg->msg2 = msgrng_load_rx_msg2();
320         msg->msg3 = msgrng_load_rx_msg3();
321         return (0);
322 }
323
324 #define MSGRNG_STN_RX_QSIZE     256
325 #define MSGRNG_NSTATIONS        128
326 #define MSGRNG_CORE_NBUCKETS    8
327
328 struct stn_cc {
329         unsigned short counters[16][8];
330 };
331
332 struct bucket_size {
333         unsigned short bucket[MSGRNG_NSTATIONS];
334 };
335
336 extern struct bucket_size bucket_sizes;
337
338 extern struct stn_cc cc_table_cpu_0;
339 extern struct stn_cc cc_table_cpu_1;
340 extern struct stn_cc cc_table_cpu_2;
341 extern struct stn_cc cc_table_cpu_3;
342 extern struct stn_cc cc_table_cpu_4;
343 extern struct stn_cc cc_table_cpu_5;
344 extern struct stn_cc cc_table_cpu_6;
345 extern struct stn_cc cc_table_cpu_7;
346 extern struct stn_cc cc_table_xgs_0;
347 extern struct stn_cc cc_table_xgs_1;
348 extern struct stn_cc cc_table_gmac;
349 extern struct stn_cc cc_table_dma;
350 extern struct stn_cc cc_table_sec;
351
352 extern struct bucket_size xls_bucket_sizes;
353
354 extern struct stn_cc xls_cc_table_cpu_0;
355 extern struct stn_cc xls_cc_table_cpu_1;
356 extern struct stn_cc xls_cc_table_cpu_2;
357 extern struct stn_cc xls_cc_table_cpu_3;
358 extern struct stn_cc xls_cc_table_gmac0;
359 extern struct stn_cc xls_cc_table_gmac1;
360 extern struct stn_cc xls_cc_table_cmp;
361 extern struct stn_cc xls_cc_table_pcie;
362 extern struct stn_cc xls_cc_table_dma;
363 extern struct stn_cc xls_cc_table_sec;
364
365 typedef void (*msgring_handler)(int, int, int, int, struct msgrng_msg *, void *);
366 int register_msgring_handler(int startb, int endb, msgring_handler action,
367                     void *arg);
368 uint32_t xlr_msgring_handler(uint8_t bucket_mask, uint32_t max_messages);
369 void xlr_msgring_cpu_init(void);
370 void xlr_msgring_config(void);
371
372 #endif