summaryrefslogtreecommitdiffstats
path: root/sys/mips/nlm/hal/cop2.h
blob: 062cd9df44ab509fc7322147ef2f045a74aae507 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*-
 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 * NETLOGIC_BSD */

#ifndef __NLM_COP2_H__
#define __NLM_COP2_H__

#define XLP_COP2_TX_BUF_REG		0
#define XLP_COP2_RX_BUF_REG		1
#define XLP_COP2_TXMSGSTATUS_REG	2
#define XLP_COP2_RXMSGSTATUS_REG	3
#define XLP_COP2_MSGSTATUS1_REG		4
#define XLP_COP2_MSGCONFIG_REG		5
#define XLP_COP2_MSGCONFIG1_REG		6

#define CROSSTHR_POPQ_EN		0x01
#define VC0_POPQ_EN			0x02
#define VC1_POPQ_EN			0x04
#define VC2_POPQ_EN			0x08
#define VC3_POPQ_EN			0x10
#define ALL_VC_POPQ_EN			0x1E
#define ALL_VC_CT_POPQ_EN		0x1F

struct nlm_fmn_msg {
	uint64_t msg[4];
};

#define NLM_DEFINE_COP2_ACCESSORS32(name, reg, sel)		\
static inline uint32_t nlm_read_c2_##name(void)			\
{								\
	uint32_t __rv;						\
	__asm__ __volatile__ (					\
	".set	push\n"						\
	".set	noreorder\n"					\
	".set	mips64\n"					\
	"mfc2	%0, $%1, %2\n"					\
	".set	pop\n"						\
	: "=r" (__rv)						\
	: "i" (reg), "i" (sel)					\
	);							\
	return __rv;						\
}								\
								\
static inline void nlm_write_c2_##name(uint32_t val)		\
{								\
	__asm__ __volatile__(					\
	".set	push\n"						\
	".set	noreorder\n"					\
	".set	mips64\n"					\
	"mtc2	%0, $%1, %2\n"					\
	".set	pop\n"						\
	:: "r" (val), "i" (reg), "i" (sel)			\
	);							\
} struct __hack

#if (__mips == 64)
#define NLM_DEFINE_COP2_ACCESSORS64(name, reg, sel)		\
static inline uint64_t nlm_read_c2_##name(void)			\
{								\
	uint64_t __rv;						\
	__asm__ __volatile__ (					\
	".set	push\n"						\
	".set	noreorder\n"					\
	".set	mips64\n"					\
	"dmfc2	%0, $%1, %2\n"					\
	".set	pop\n"						\
	: "=r" (__rv)						\
	: "i" (reg), "i" (sel) );				\
	return __rv;						\
}								\
								\
static inline void nlm_write_c2_##name(uint64_t val)		\
{								\
	__asm__ __volatile__ (					\
	".set	push\n"						\
	".set	noreorder\n"					\
	".set	mips64\n"					\
	"dmtc2	%0, $%1, %2\n"					\
	".set	pop\n"						\
	:: "r" (val), "i" (reg), "i" (sel) );			\
} struct __hack

#else

#define NLM_DEFINE_COP2_ACCESSORS64(name, reg, sel)		\
static inline uint64_t nlm_read_c2_##name(void)			\
{								\
	uint32_t __high, __low;					\
	__asm__ __volatile__ (					\
	".set	push\n"						\
	".set	noreorder\n"					\
	".set	mips64\n"					\
	"dmfc2	$8, $%2, %3\n"					\
	"dsra32	%0, $8, 0\n"					\
	"sll	%1, $8, 0\n"					\
	".set	pop\n"						\
	: "=r"(__high), "=r"(__low)				\
	: "i"(reg), "i"(sel)					\
	: "$8" );						\
								\
	return (((uint64_t)__high << 32) | __low);		\
}								\
								\
static inline void nlm_write_c2_##name(uint64_t val)		\
{								\
       uint32_t __high = val >> 32;				\
       uint32_t __low = val & 0xffffffff;			\
	__asm__ __volatile__ (					\
	".set	push\n"						\
	".set	noreorder\n"					\
	".set	mips64\n"					\
	"dsll32	$8, %1, 0\n"					\
	"dsll32	$9, %0, 0\n"					\
	"dsrl32	$8, $8, 0\n"					\
	"or	$8, $8, $9\n"					\
	"dmtc2	$8, $%2, %3\n"					\
	".set	pop\n"						\
	:: "r"(__high), "r"(__low),  "i"(reg), "i"(sel)		\
	:"$8", "$9");						\
} struct __hack

#endif

NLM_DEFINE_COP2_ACCESSORS64(txbuf0, XLP_COP2_TX_BUF_REG, 0);
NLM_DEFINE_COP2_ACCESSORS64(txbuf1, XLP_COP2_TX_BUF_REG, 1);
NLM_DEFINE_COP2_ACCESSORS64(txbuf2, XLP_COP2_TX_BUF_REG, 2);
NLM_DEFINE_COP2_ACCESSORS64(txbuf3, XLP_COP2_TX_BUF_REG, 3);

NLM_DEFINE_COP2_ACCESSORS64(rxbuf0, XLP_COP2_RX_BUF_REG, 0);
NLM_DEFINE_COP2_ACCESSORS64(rxbuf1, XLP_COP2_RX_BUF_REG, 1);
NLM_DEFINE_COP2_ACCESSORS64(rxbuf2, XLP_COP2_RX_BUF_REG, 2);
NLM_DEFINE_COP2_ACCESSORS64(rxbuf3, XLP_COP2_RX_BUF_REG, 3);

NLM_DEFINE_COP2_ACCESSORS32(txmsgstatus, XLP_COP2_TXMSGSTATUS_REG, 0);
NLM_DEFINE_COP2_ACCESSORS32(rxmsgstatus, XLP_COP2_RXMSGSTATUS_REG, 0);
NLM_DEFINE_COP2_ACCESSORS32(msgstatus1, XLP_COP2_MSGSTATUS1_REG, 0);
NLM_DEFINE_COP2_ACCESSORS32(msgconfig, XLP_COP2_MSGCONFIG_REG, 0);
NLM_DEFINE_COP2_ACCESSORS32(msgconfig1, XLP_COP2_MSGCONFIG1_REG, 0);

/* successful completion returns 1, else 0 */
static __inline__ int nlm_msgsend(int val)
{
	int result;
	__asm__ volatile (
		".set push			\n"
		".set noreorder			\n"
		".set mips64			\n"
		"move	$8, %1			\n"
		"sync				\n"
		"/* msgsnds	$9, $8 */	\n"
		".word	0x4a084801		\n"
		"move	%0, $9			\n"
		".set pop			\n"
		: "=r" (result)
		: "r" (val)
		: "$8", "$9"
	);
	return result;
}

static __inline__ int nlm_msgld(int vc)
{
	int val;
	__asm__ volatile (
		".set push			\n"
		".set noreorder			\n"
		".set mips64			\n"
		"move	$8, %1			\n"
		"/* msgld	$9, $8 */	\n"
		".word 0x4a084802		\n"
		"move	%0, $9			\n"
		".set pop			\n"
		: "=r" (val)
		: "r" (vc)
		: "$8", "$9"
	);
	return val;
}

static __inline__ void nlm_msgwait(int vc)
{
	__asm__ volatile (
		".set push			\n"
		".set noreorder			\n"
		".set mips64			\n"
		"move	$8, %0			\n"
		"/* msgwait	$8 */		\n"
		".word 0x4a080003		\n"
		".set pop			\n"
		:: "r" (vc)
		: "$8"
	);
}

/* TODO this is not needed in n32 and n64 */
static __inline uint32_t
nlm_fmn_saveflags(void)
{
	uint32_t sr = mips_rd_status();

	mips_wr_status((sr & ~MIPS_SR_INT_IE) | MIPS_SR_COP_2_BIT);
	return (sr);
}

static __inline void
nlm_fmn_restoreflags(uint32_t sr)
{

	mips_wr_status(sr);
}

static __inline__ int nlm_fmn_msgsend(int dstid, int size, int swcode,
		struct nlm_fmn_msg *m)
{
	uint32_t flags, status;
	int rv;

	size -= 1;
	flags = nlm_fmn_saveflags();
	switch(size) {
		case 3: nlm_write_c2_txbuf3(m->msg[3]);
		case 2: nlm_write_c2_txbuf2(m->msg[2]);
		case 1: nlm_write_c2_txbuf1(m->msg[1]);
		case 0: nlm_write_c2_txbuf0(m->msg[0]);
	}

	dstid |= ((swcode << 24) | (size << 16));
	status = nlm_msgsend(dstid);
	rv = !status;
	if (rv != 0)
		rv = nlm_read_c2_txmsgstatus();
	nlm_fmn_restoreflags(flags);

	return (rv);
}

static __inline__ int nlm_fmn_msgrcv(int vc, int *srcid, int *size, int *code,
    struct nlm_fmn_msg *m)
{
	uint32_t status;
	uint32_t msg_status, flags;
	int tmp_sz, rv;

	flags = nlm_fmn_saveflags();
	status = nlm_msgld(vc); /* will return 0, if error */
	rv = !status;
	if (rv == 0) {
		msg_status = nlm_read_c2_rxmsgstatus();
		*size = ((msg_status >> 26) & 0x3) + 1;
		*code = (msg_status >> 18) & 0xff;
		*srcid = (msg_status >> 4) & 0xfff;
		tmp_sz = *size - 1;
		switch(tmp_sz) {
			case 3: m->msg[3] = nlm_read_c2_rxbuf3();
			case 2: m->msg[2] = nlm_read_c2_rxbuf2();
			case 1: m->msg[1] = nlm_read_c2_rxbuf1();
			case 0: m->msg[0] = nlm_read_c2_rxbuf0();
		}
	}
	nlm_fmn_restoreflags(flags);

	return rv;
}

/**
 * nlm_fmn_cpu_init() initializes the per-h/w thread cop2 w.r.t the following
 * configuration parameters. It needs to be individually setup on each
 * hardware thread.
 *
 * int_vec - interrupt vector getting placed into msgconfig reg
 * ctpe - cross thread message pop enable. When set to 1, the thread (h/w cpu)
 *        associated where this cop2 register is setup, can pop messages
 *        intended for any other thread in the same core.
 * v0pe - VC0 pop message request mode enable. When set to 1, the thread
 * 	  can send pop requests to vc0.
 * v1pe - VC1 pop message request mode enable. When set to 1, the thread
 * 	  can send pop requests to vc1.
 * v2pe - VC2 pop message request mode enable. When set to 1, the thread
 * 	  can send pop requests to vc2.
 * v3pe - VC3 pop message request mode enable. When set to 1, the thread
 * 	  can send pop requests to vc3.
 */
static __inline__ void nlm_fmn_cpu_init(int int_vec, int ctpe, int v0pe,
    int v1pe, int v2pe, int v3pe)
{
	uint32_t val = nlm_read_c2_msgconfig();

	/* Note: in XLP PRM 0.8.1, the int_vec bits are un-documented
	 * in msgconfig register of cop2.
	 * As per chip/cpu RTL, [16:20] bits consist of int_vec.
	 */
	val |= ((int_vec & 0x1f) << 16) |
		((v3pe & 0x1) << 4) |
		((v2pe & 0x1) << 3) |
		((v1pe & 0x1) << 2) |
		((v0pe & 0x1) << 1) |
		(ctpe & 0x1);

	nlm_write_c2_msgconfig(val);
}
#endif
OpenPOWER on IntegriCloud