forked from epicsdeb/rtems-libbspext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memProbe.c
407 lines (353 loc) · 10.6 KB
/
memProbe.c
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* $Id: memProbe.c,v 1.21 2010/02/12 18:19:32 strauman Exp $ */
/* Address Probing for Powerpc - RTEMS
*
* Author: Till Straumann <[email protected]>
*
* S. Kate Feng <[email protected]> ported it to the MVME5500.(4/2004)
*
*/
/*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
#include "bspExt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bsp.h>
#include <rtems/bspIo.h>
#include <assert.h>
#include <libcpu/spr.h>
#include <libcpu/cpuIdent.h>
#define SRR1_TEA_EXC (1<<(31-13))
#define SRR1_MCP_EXC (1<<(31-12))
SPR_RW(HID1)
SPR_RW(HID0)
typedef struct EH_NodeRec_ {
BspExtEHandler h;
void *d;
struct EH_NodeRec_ *n;
} EH_NodeRec, *EH_Node;
static EH_Node anchor = 0;
static exception_handler_t origHandler=0;
/* lowlevel memory probing routines conforming to
* the PPC SVR4 ABI.
* Note: r3, i.e. the return value is modified
* (implicitely) by the exeption handler
* the routines return the fault address
* if an exception was detected, hence
* the first argument should be set e.g.
* to 0 when calling the probe routine.
*/
typedef void * (*MemProber)(void *rval, void *from, void *to);
void * memProbeByte(void *rval, void *from, void *to);
void * memProbeShort(void *rval, void *from, void *to);
void * memProbeLong(void *rval, void *from, void *to);
void * memProbeEnd(void);
unsigned long _BSP_clear_hostbridge_errors(int,int)
__attribute__((weak, alias("_bspExtMemProbe_dummy_clear_hostbridge_errors")));
unsigned long
_bspExtMemProbe_dummy_clear_hostbridge_errors(int enableMCP, int quiet)
{
return enableMCP ? -1 : 0;
}
extern int
_bspExtCatchBreakpoint(BSP_Exception_frame *fp);
static void
bspExtExceptionHandler(BSP_Exception_frame* excPtr)
{
void *nip = (void*)excPtr->EXC_SRR0;
int caughtBr;
/* is this a memProbe? */
if (nip>=(void*)memProbeByte && nip<(void*)memProbeEnd) {
/* well, we assume that the branch
* instructions between memProbeByte and memProbeLong
* are safe and dont fault...
*/
/* flag the fault */
excPtr->GPR3=(unsigned)nip;
excPtr->EXC_SRR0 +=4; /* skip the faulting instruction */
/* NOTE DAR is not valid at this point because some boards
* raise a machine check which doesn't set DAR...
*/
/* clear MCP condition */
if ( (SRR1_TEA_EXC|SRR1_MCP_EXC) & excPtr->EXC_SRR1 ) {
unsigned status;
status = _BSP_clear_hostbridge_errors(1,1);
if ( bspExtVerbosity > 1 ) {
printk("Machine check [%s]; host bridge status 0x%08x\n",
excPtr->EXC_SRR1 & SRR1_TEA_EXC ? "TEA" : "MCP",
status);
}
}
return;
} else if ((caughtBr=_bspExtCatchBreakpoint(excPtr))) {
switch (caughtBr) {
default: /* should never get here */
case -1: /* usr handler wants us to panic */
break;
case 1: /* phase 1; dabr deinstalled, complete data access */
case 2: /* phase 2; stopped after completing faulting instruction */
return;
}
}
/* Work the list of registered handlers */
{
EH_Node p;
for ( p = anchor; p; p=p->n ) {
if ( 0 == (*p->h)(excPtr, p->d) )
return;
}
}
/* resort to BSP handler */
origHandler(excPtr);
}
static int useMcp = 0;
void
_bspExtMemProbeInit(void)
{
rtems_interrupt_level level;
int mcp_hid = -1;
/* Find out which HID register holds EMCP (if any) */
switch ( get_ppc_cpu_type() ) {
case PPC_7455: case PPC_7457:
mcp_hid = 1;
break;
case PPC_7400:
case PPC_750:
case PPC_604: case PPC_604e: case PPC_604r:
case PPC_603: case PPC_603e: case PPC_603ev:
case PPC_8260: /* same as 8260: case PPC_8240: */ case PPC_8245:
mcp_hid = 0;
break;
default:
mcp_hid = -1; /* none or unknown; disable MCP support */
break;
}
if ( mcp_hid >= 0 ) {
/* try to clear pending errors and enable
* MCP in HID0 on success
*/
/* clear pending errors */
_BSP_clear_hostbridge_errors(0,1);
/* see if there are still errors pending */
if ( _BSP_clear_hostbridge_errors(0,1) ) {
fprintf(stderr,"Warning: unable to clear pending hostbridge errors; leaving MCP disabled\n");
fprintf(stderr," proper operation of address probing not guaranteed\n");
} else {
/* enable MCP at the hostbridge */
if (_BSP_clear_hostbridge_errors(1,1) !=-1) {
useMcp = 1;
if ( 1 == mcp_hid ) {
_write_HID1( _read_HID1() | HID0_EMCP );/* enable MCP */
} else if ( 0 == mcp_hid ) {
_write_HID0( _read_HID0() | HID0_EMCP );/* enable MCP */
} else {
BSP_panic(__FILE__" bad value for mcp_hid");
}
}
}
}
if ( !useMcp ) {
fprintf(stderr,"libbspExt - Warning: it seems that MCP support is not available on your CPU\n");
fprintf(stderr," or not implemented by your board. Address probing must\n");
fprintf(stderr," be performed in polling mode with interrupts disabled\n");
}
/* switch exception handlers */
rtems_interrupt_disable(level);
/* install an exception handler to catch
* protection violations
*/
origHandler=globalExceptHdl;
globalExceptHdl = bspExtExceptionHandler;
/* make sure handler is swapped */
__asm__ __volatile__ ("sync");
rtems_interrupt_enable(level);
}
rtems_status_code
bspExtMemProbe(void *addr, int write, int size, void *pval)
{
rtems_status_code rval=RTEMS_SUCCESSFUL;
rtems_interrupt_level flags=0;
unsigned long buf;
MemProber probe;
void *faultAddr;
/* lazy init (not strictly thread safe!) */
if (!origHandler) bspExtInit();
if (bspExtExceptionHandler!=globalExceptHdl) {
fprintf(stderr,"bspExtMemProbe: Someone changed the exception handler - refuse to probe\n");
return -1;
}
if (bspExtVerbosity) {
fprintf(stderr,"Warning: bspExtMemProbe kills real-time performance.\n");
if ( !useMcp ) {
fprintf(stderr," Your BSP does or can not use MCP exceptions - we\n");
fprintf(stderr," must probe with INTERRUPTS DISABLED !!!\n");
}
fprintf(stderr," use only during driver initialization\n\n");
fprintf(stderr," clear the 'bspExtVerbosity' variable to silence this warning\n");
}
/* validate arguments and compute jump table index */
switch (size) {
case sizeof(char): probe=memProbeByte; break;
case sizeof(short): probe=memProbeShort; break;
case sizeof(long): probe=memProbeLong; break;
default: return RTEMS_INVALID_SIZE;
}
/* use a buffer to make sure we don't end up
* probing 'pval'...
*/
if (write && pval)
memcpy(&buf, pval, size);
if ( ! useMcp ) {
/* MCP exceptions are not enabled; use
* exclusive access to host bridge status
* - data access exceptions still work...
*/
rtems_interrupt_disable(flags);
_BSP_clear_hostbridge_errors(0,1);
}
if (write) {
if ((faultAddr=probe(0,&buf,addr)))
rval=RTEMS_INVALID_ADDRESS;
} else {
if ((faultAddr=probe(0,addr,&buf))) {
rval=RTEMS_INVALID_ADDRESS;
/* pass them the fault address back */
}
}
if ( ! useMcp ) {
/* MCP exceptions are not enabled; use
* exclusive access to host bridge status
*/
if (!faultAddr)
rval = _BSP_clear_hostbridge_errors(0,1);
rtems_interrupt_enable(flags);
}
if (!write && pval) {
memcpy(pval, faultAddr ? (void*)&faultAddr : &buf, size);
}
return rval;
}
/*
* Register an exception handler at the head (where > 0) or tail
* (where <=0).
*
* RETURNS: 0 on success, -1 on failure (handler already installed
* or no memory).
*
*/
int
bspExtInstallEHandler(BspExtEHandler h, void *usrData, int where)
{
EH_Node n=0, *pp;
unsigned long flags;
int rval = -1;
if ( !(n=malloc(sizeof(*n))) )
return -1;
bspExtLock();
for ( pp=&anchor; *pp; pp=&(*pp)->n ) {
if ( (*pp)->h == h && (*pp)->d == usrData )
goto bail;
}
/* pp now points to the tail */
n->h = h;
n->d = usrData;
if ( where ) {
/* reset to head */
pp = &anchor;
}
rtems_interrupt_disable(flags);
n->n = *pp;
*pp = n;
rtems_interrupt_enable(flags);
n = 0;
rval = 0;
bail:
bspExtUnlock();
free(n);
return rval;
}
/* Remove an exception handler
*
* RETURNS: 0 on success, nonzero if handler/usrData pair not found
*/
int bspExtRemoveEHandler(BspExtEHandler h, void *usrData)
{
EH_Node p = 0, *pp;
unsigned long flags;
bspExtLock();
for ( pp = &anchor; *pp; pp = &(*pp)->n ) {
if ( (*pp)->h == h && (*pp)->d == usrData ) {
rtems_interrupt_disable(flags);
p = *pp;
*pp = p->n;
rtems_interrupt_enable(flags);
break;
}
}
bspExtUnlock();
free(p);
return 0 == p;
}
__asm__(
" .text\n"
" .GLOBAL memProbeByte, memProbeShort, memProbeLong, memProbeEnd\n"
"memProbeByte: \n"
" lbz %r4, 0(%r4) \n"
" stb %r4, 0(%r5) \n"
" b 1f \n"
"memProbeShort: \n"
" lhz %r4, 0(%r4) \n"
" sth %r4, 0(%r5) \n"
" b 1f \n"
"memProbeLong: \n"
" lwz %r4, 0(%r4) \n"
" stw %r4, 0(%r5) \n"
"1: sync \n"
" nop \n" /* could take some time until MCP is asserted */
" nop \n"
" nop \n"
" nop \n"
" nop \n"
" nop \n"
" nop \n"
" nop \n"
" sync \n"
"memProbeEnd: \n"
" blr \n"
);