bcc.h
9.31 KB
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
/*
* Copyright (c) 2017, Cobham Gaisler AB
* 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER 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.
*/
#ifndef __BCC_BCC_H_
#define __BCC_BCC_H_
#include <stddef.h>
#include <stdint.h>
enum {
BCC_OK,
BCC_NOT_AVAILABLE,
BCC_FAIL
};
/*
* Enable interrupt based timer service.
*
* The function installs a tick interrupt handler which maintains local time
* using timer hardware. This makes C library / POSIX time functions not
* limited to hardware constraints anymore.
*
* NOTE: Epoch changes to the point in time when bcc_timer_tick_init_period() is
* called.
* NOTE: This function should be called only once.
* NOTE: The function assumes that the timer (global) scaler is set to 1 MHz.
*
* usec_per_tick: Requested timer tick period: number of microseconds per tick.
*
* return:
* - BCC_OK: Success
* - BCC_FAIL: Failed to enable interrupt based timer service, or already
* enabled.
* - BCC_NOT_AVAILABLE: Hardware or resource not available
*/
int bcc_timer_tick_init_period(
uint32_t usec_per_tick
);
/* Enable interrupt based timer service with 10 millisecond tick period. */
static inline int bcc_timer_tick_init(void);
/* Return number of microseconds since some point in time. */
uint32_t bcc_timer_get_us(void);
/*
* Register interrupt handler
*
* The function in parameter handler is registered as an interrupt handler for
* the given interrupt source. The handler is called with arg and source as
* arguments.
*
* Interrupt source is not enabled by this function. bcc_int_unmask() can be
* used to enable it.
*
* Multiple interrupt handlers can be registered for the same interrupt number.
* They are dispatched at interrupt in the same order as registered.
*
* A handler registered with this function should be unregistered with
* bcc_isr_unregister().
*
* source: SPARC interrupt number 1-15 or extended interrupt number 16-31.
* handler: Pointer to software routine to execute when the interrupt triggers.
* arg: Passed as parameter to handler.
*
* return:
* - NULL: Indicates failed to install handler.
* - Pointer: Pointer to ISR handler context. Should not be dereferenced by user.
* Used as input to bcc_isr_unregister().
*
* NOTE: This function may call malloc().
*/
void *bcc_isr_register(
int source,
void (*handler)(
void *arg,
int source
),
void *arg
);
/*
* Unregister interrupt handler
*
* It is only allowed to unregister an interrupt handler which has previously
* been registered with bcc_isr_register().
*
* Interrupt source is not disabled by this function. The function
* bcc_int_mask() can be used to disable it.
*
* isr_ctx: ISR handler context returned by bcc_isr_register()
*
* return:
* - BCC_OK: Handler successfully unregistered.
* - BCC_FAIL: Failed to unregister handler.
*
* NOTE: This function may call free().
*/
int bcc_isr_unregister(
void *isr_ctx
);
/*
* Interrupt node for registering interrupt handler
*
* This structure is used for registering interrupt handlers with
* bcc_isr_register_node() and bcc_isr_unregister_node(). An application shall
* not use it if bcc_isr_register() and bcc_isr_unregister() is used.
*/
struct bcc_isr_node {
/* BCC private data (do not touch) */
void *__private;
/* Interrupt source number */
int source;
/* User ISR handler */
void (*handler)(
void *arg,
int source
);
/* Passed as parameter to handler */
void *arg;
};
/*
* Register interrupt handler, non-allocating
*
* This function is similar to bcc_isr_register() with the difference that the
* user is responsible for memory management. It will never call malloc().
* Instead the caller has to provide a pointer to a preallocated and
* initialized ISR node of type struct bcc_isr node.
*
* The memory pointed to by isr_node shall be considered owned exclusively by
* the run-time between the call to bcc_isr_register_node() and a future
* bcc_isr_unregister_node(). It means that the memory must be available for
* this time and must not be modified by the application. The memory pointed to
* by isr_node must be writable.
*
* This function should be used to install interrupt handlers in applications
* which want full control over memory allocation.
*
* isr_node: Pointer to user initialized ISR node. The fields source, handler
* and optionally the arg shall be initialized by the caller.
*
* return:
* - BCC_OK: Handler installed successfully.
* - BCC_FAIL: Failed to install handler.
*/
int bcc_isr_register_node(
struct bcc_isr_node *isr_node
);
/*
* Unregister interrupt handler, non-allocating
*
* This function is similar to bcc_isr_unregister() with the difference that
* the user is responsible for memory management. It is only allowed to
* unregister an interrupt handler which has previously been registered with
* bcc_isr_register_node().
*
* isr_node: Same as input parameter to bcc_isr_register_node().
*
* return:
* - BCC_OK: Handler successfully unregistered.
* - BCC_FAIL: Failed to unregister handler.
*/
int bcc_isr_unregister_node(
const struct bcc_isr_node *isr_node
);
/*
* Disable all maskable interrupts and return the previous interrupt
* enable/disable state
*
* A matching bcc_int_enable() with the return value as parameter must be
* called to exit the interrupt disabled state. It is allowed to do nested
* calls to bcc_int_disable(), and if so the same number of bcc_int_enable()
* must be called.
*
* This function modifies the MSTATUS.MIE field. Interrupt controller is
* not touched.
*
* return: Previous interrupt level (used when calling bcc_int_enable().
*/
static inline int bcc_int_disable(void);
/*
* Return to a previous interrupt enable/disable state
*
* The plevel parameter is the return value from a previous call to
* bcc_int_disable(). At return, interrupts may be enabled or disabled
* depending on plevel.
*
* This function modifies the MSTATUS.MIE field. Interrupt controller is
* not touched.
*
* plevel: The interrupt protection level to set. Must be the return value from
* the most recent call to bcc_int_disable().
*/
static inline void bcc_int_enable(int plevel);
/*
* Mask (disable) an interrupt source on the current CPU.
*
* return:
* - BCC_OK: success
* - BCC_NOT_AVAILABLE: device not available
*/
int bcc_int_mask(int source);
/*
* Unmask (enable) an interrupt source on the current CPU.
*
* return:
* - BCC_OK: success
* - BCC_NOT_AVAILABLE: device not available
*/
int bcc_int_unmask(int source);
/*
* Get ID of the current processor.
*
* The first processor in the system has ID 0.
*
* return: ID of the current processor
*/
int bcc_get_cpuid(void);
/*
* Set 64-bit words to zero
*
* This function sets n 64-bit words to zero, starting at address dst. All
* writes are performed with the SPARC V8 std instruction.
*
* dst: Start address of area to set to zero. Must be aligned to a 64-bit word.
* n: Number of 64-bit words to set to zero.
*/
void bcc_dwzero(uint64_t *dst, size_t n);
/*
* Get number of processors in the system
*
* NOTE: This function will return -1 if the run-time is not aware of the
* interrupt controller.
*
* return: Number of processor in the system or -1 if unknown.
*/
int bcc_get_cpu_count(void);
/*
* Start a processor
*
* cpuid: The processor to start (0 .. (get_cpu_count()-1))
* return:
* - BCC_OK: success
* - BCC_NOT_AVAILABLE: processor or device not available
*/
int bcc_start_processor(int cpuid);
struct bcc_startinfo {
void (*pc)(int mhartid);
uintptr_t sp;
};
extern struct bcc_startinfo __bcc_startinfo[16];
/*
* Start a processor
*
* The CPU will start executing with pc and sp in
* __bcc_cpu_start[cpuid].
*
* cpuid 0 is the boot cpu and can not be used as parameter.
*
* cpuid: The processor to start (1 .. (get_cpu_count()-1))
* return:
* - BCC_OK: success
* - BCC_NOT_AVAILABLE: processor or device not available
*/
int bcc_start_processor(int cpuid);
/* Bring in implementation of inline functions. */
#include "bcc_inline.h"
#endif