source: trunk/hal/oldies/hal_cpu.h @ 48

Last change on this file since 48 was 1, checked in by alain, 7 years ago

First import

File size: 26.9 KB
Line 
1/*
2 * hal-cpu.h - CPU related Hardware Abstraction Layer
3 *
4 * Authors   Ghassan Almaless (2008,2009,2010,2011,2012)
5 *           Alain Greiner    (2016)
6 *
7 * Copyright (c)  UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#ifndef  _HAL_CPU_H_
26#define  _HAL_CPU_H_
27
28#include <types.h>
29#include <context.h>
30
31/*****************************************************************************************
32 * All CPU specific implementations must define the functions and macros
33 * specified in this CPU Hardware Abstraction Layer.
34 ****************************************************************************************/
35
36#define CPU_USR_MODE    // CPU in user mode with interrupts enabled
37#define CPU_SYS_MODE    // CPU in kernel mode with interrupts disabled
38
39
40//////////////////////////////////////////////////////////////////////////////////////////
41//////////////////////////////////////////////////////////////////////////////////////////
42//       CPU context operations / implementation in the cpu/***/hal_context.
43//////////////////////////////////////////////////////////////////////////////////////////
44//////////////////////////////////////////////////////////////////////////////////////////
45
46/*****************************************************************************************
47 * This function initializes a CPU execution context from informations contained
48 * in a thread descriptor
49 * @ ctx     : pointer on the hal_context_t structure.
50 * @ thread  : pointer on the thread descriptor.
51 ****************************************************************************************/
52extern void hal_context_init( cpu_context_t * ctx, 
53                              thread_t      * thread );
54
55/*****************************************************************************************
56 * This function sets a new value in the thread slot of the calling CPU context.
57 * @ ctx     : pointer on the hal_context_t structure.
58 * @ thread  : pointer on the thread.
59 ****************************************************************************************/
60extern void hal_context_set_thread( cpu_context_t * ctx,
61                                    thread_t      * thread );
62
63/*****************************************************************************************
64 * This function sets a new value in the cp2_ptpr slot of the calling CPU context.
65 * @ ctx     : pointer on the hal_context_t structure.
66 * @ ppm     : pointer on the (source) physical memory manager.
67 ****************************************************************************************/
68extern void hal_context_set_pmm( cpu_context_t * ctx,
69                                 pmm_t         * pmm );
70
71/*****************************************************************************************
72 * This function sets a new value in the sigreturn_func slot of the calling CPU context.
73 * @ ctx     : pointer on the hal_context_t structure.
74 * @ func    : function pointer.
75 ****************************************************************************************/
76extern void hal_context_set_sigreturn( cpu_context_t * ctx,
77                                       void          * func );
78
79/*****************************************************************************************
80 * This function should be used to launch a new thread.
81 * It loads the calling CPU registers from the values contained in the context argument,
82 *  and jumps to the address defined in the entry_func slot, using an eret.
83 * @ ctx     : pointer on the hal_context_t structure.
84 ****************************************************************************************/
85extern void hal_context_load( cpu_context_t * ctx);
86
87/*****************************************************************************************
88 * This function saves the calling CPU registers to the CPU context argument.
89 * @ ctx     : pointer on the hal_context_t structure.
90 ****************************************************************************************/
91extern void hal_context_save( cpu_context_t * ctx );
92
93/*****************************************************************************************
94 * This function restores the CPU registers from the CPU context argument.
95 * @ ctx     : pointer on the hal_context_t structure.
96 ****************************************************************************************/
97extern void hal_context_restore( cpu_context_t * ctx,
98                                 uint32_t        val );
99
100/*****************************************************************************************
101 * This function initializes an uzone structure.
102 * @ uzone    : pointer on the structure to initialise.
103 ****************************************************************************************/
104extern void hal_uzone_init( cpu_uzone_t * uzone );
105
106/*****************************************************************************************
107 * This function copies the content of a source uzone to a destination uzone.
108 * @ dst      : pointer on the destination structure.
109 * @ src      : pointer on the source structure.
110 ****************************************************************************************/
111extern void hal_uzone_dup( cpu_uzone_t * dst,
112                           hal_uzone_t * src );
113
114/*****************************************************************************************
115 * This function save the fpu registers in the thread uzone.
116 ****************************************************************************************/
117extern void hal_fpu_context_save( cpu_uzone_t * uzone );
118
119/*****************************************************************************************
120 * This function restore the fpu registers from the thread uzone.
121 ****************************************************************************************/
122extern void hal_fpu_context_restore( cpu_uzone_t * uzone );
123
124/*****************************************************************************************
125 * This function TODO ??? [AG]
126 ****************************************************************************************/
127extern error_t hal_uzone_getattr( cpu_uzone_t      * uzone,
128                                  hal_uzone_attr_t * attr);
129
130/*****************************************************************************************
131 * This function TODO ??? [AG]
132 ****************************************************************************************/
133extern error_t hal_uzone_setattr( cpu_uzone_t      * uzone,
134                                  hal_uzone_attr_t * attr);
135
136/*****************************************************************************************
137 * This function TODO ??? [AG]
138 ****************************************************************************************/
139extern void hal_signal_notify( thread_t * this,
140                               void     * handler,
141                               uint32_t   sig );
142
143////////////////////////////////////////////////////////////////////////////////////////////
144////////////////////////////////////////////////////////////////////////////////////////////
145//      CPU special registers access / implementation in cpu/***/special.c
146////////////////////////////////////////////////////////////////////////////////////////////
147////////////////////////////////////////////////////////////////////////////////////////////
148
149/*****************************************************************************************
150 * This function returns the global core identifier from the calling core register.
151 ****************************************************************************************/
152extern inline gid_t hal_get_gid();
153
154/*****************************************************************************************
155 * This function returns the content of the calling core cycles counter register.
156 ****************************************************************************************/
157extern inline uint32_t hal_time_stamp();
158
159/*****************************************************************************************
160 * This function returns the current thread pointer from the calling core register.
161 ****************************************************************************************/
162extern inline thread_t * hal_get_current_thread();
163
164/*****************************************************************************************
165 * This function registers a thread pointer in the calling CPU register.
166 ****************************************************************************************/
167extern inline void hal_set_current_thread(thread_t * thread);
168
169/*****************************************************************************************
170 * This function writes into the proper CPU register to enable the floating point unit.
171 ****************************************************************************************/
172extern inline void hal_fpu_enable();
173
174/*****************************************************************************************
175 * This function writes into the proper CPU register to disable the floating point unit.
176 ****************************************************************************************/
177extern inline void hal_fpu_disable();
178
179/*****************************************************************************************
180 * This function returns the current value of stack pointer from CPU register.
181 ****************************************************************************************/
182extern inline uint32_t hal_get_stack();
183
184/*****************************************************************************************
185 * This function registers a new value in the CPU stack pointer and returns previous one.
186 ****************************************************************************************/
187extern inline uint32_t hal_set_stack( void * new_val );
188
189
190//////////////////////////////////////////////////////////////////////////////////////////
191//////////////////////////////////////////////////////////////////////////////////////////
192//       IRQ disable / restore operations / implementation in cpu/***/irqmask.c
193//////////////////////////////////////////////////////////////////////////////////////////
194//////////////////////////////////////////////////////////////////////////////////////////
195
196/*****************************************************************************************
197 * This function disables all IRQs, and saves the CPU SR state if required.
198 * TODO : Warning this function discards the CU3 access bit
199 * @ old    : address of buffer to save the SR (no save if NULL).
200 ****************************************************************************************/
201extern inline void hal_disable_all_irq( uint32_t * old );
202
203/*****************************************************************************************
204 * This function enables all IRQs, and saves the CPU SR state if required.
205 * @ old    : address of buffer to save the SR (no save if NULL).
206 ****************************************************************************************/
207extern inline void hal_enable_all_irq( uint32_t * old );
208
209/*****************************************************************************************
210 * This function returns true if all IRQs are disabled.
211 ****************************************************************************************/
212extern inline bool_t hal_all_irq_disabled();
213
214/*****************************************************************************************
215 * This function restores a previously SCPU SR state.
216 * @ old    : value to be written in CPU SR register
217 ****************************************************************************************/
218extern inline void hal_restore_irq( uint32_t old );
219
220//////////////////////////////////////////////////////////////////////////////////////////
221//////////////////////////////////////////////////////////////////////////////////////////
222//            Atomic Operations / Implementation in cpu/***/atomic.c
223//
224// Implementation of atomic read-then-write operations depends on the CPU instruction set.
225//////////////////////////////////////////////////////////////////////////////////////////
226//////////////////////////////////////////////////////////////////////////////////////////
227
228/*****************************************************************************************
229 * This blocking function makes an atomic "and" between a 32 bits mask, and a 32 bits
230 * unsigned int shared variable, returning only when atomic operation is successful.
231 * @ ptr     : pointer on the shared variable
232 * @ val     : mask value
233 ****************************************************************************************/
234extern inline void hal_atomic_and( uint32_t * ptr,
235                                   uint32_t   val );
236
237/*****************************************************************************************
238 * This blocking function makes an atomic "or" between a 32 bits mask, and a 32 bits
239 * unsigned int shared variable, returning only when atomic operation is successful.
240 * @ ptr     : pointer on the shared variable
241 * @ val     : mask value
242 ****************************************************************************************/
243extern inline void hal_atomic_or( uint32_t * ptr,
244                                  uint32_t   val );
245
246/*****************************************************************************************
247 * This blocking function atomically adds a positive or negative value to a 32 bits
248 * unsigned int shared variable, returning only when atomic add is successful.
249 * @ ptr     : pointer on the shared variable
250 * @ val     : signed value to add
251 * @ return shared variable value before add
252 ****************************************************************************************/
253extern inline uint32_t hal_atomic_add( uint32_t * ptr,
254                                       int32_t    val );
255
256/*****************************************************************************************
257 * This blocking function atomically increments a 32 bits unsigned int shared variable,
258 * returning only when atomic increment is successful.
259 * @ ptr     : pointer on the shared variable
260 * @ return shared variable value before increment
261 ****************************************************************************************/
262extern inline uint32_t hal_atomic_inc( uint32_t * ptr );
263
264/*****************************************************************************************
265 * This blocking function atomically decrements a 32 bits unsigned int shared variable,
266 * returning only when atomic decrement is successful.
267 * @ ptr     : pointer on the shared variable
268 * @ return shared variable value before decrement
269 ****************************************************************************************/
270extern inline uint32_t hal_atomic_dec( uint32_t * ptr );
271
272/*****************************************************************************************
273 * This non blocking function makes an atomic Compare-And-Swap on a 32 bits unsigned int
274 * shared variable, returning a Boolean to indicate both success and atomicity.
275 * @ ptr     : pointer on the shared variable
276 * @ old     : expected value for the shared variable
277 * @ new     : value to be writen if success
278 * @ return true if (current == old) and (access is atomic)
279 ****************************************************************************************/
280extern inline bool_t hal_atomic_cas( uint32_t * ptr,
281                                     uint32_t   old,
282                                     uint32_t   new );
283
284/*****************************************************************************************
285 * This non blocking function makes an atomic Test-And-Set on a 32 bits unsigned int
286 * shared variable, returning a Boolean to indicate both success and atomicity.
287 * @ ptr     : pointer on the shared variable
288 * @ val     : value to be writen if success
289 * @ return true if (current == 0) and (access is atomic)
290 ****************************************************************************************/
291extern inline bool_t hal_atomic_test_set( uint32_t * ptr, 
292                                          uint32_t   val );
293
294//////////////////////////////////////////////////////////////////////////////////////////
295//////////////////////////////////////////////////////////////////////////////////////////
296//        L1 cache related operations / Implementation in the cpu/***/cache.c
297//////////////////////////////////////////////////////////////////////////////////////////
298//////////////////////////////////////////////////////////////////////////////////////////
299
300/*****************************************************************************************
301 * This function makes an uncachable read to a 32 bits variable in local memory.
302 * @ ptr     : pointer on the variable
303 * @ returns the value
304 ****************************************************************************************/
305extern inline uint32_t hal_uncached_read( uint32_t * ptr );
306
307/*****************************************************************************************
308 * This function invalidates the cache line containing a given address.
309 * @ ptr     : address in local memory
310 ****************************************************************************************/
311extern inline void hal_invalid_dcache_line( void * ptr );
312
313/*****************************************************************************************
314 * This blocking function flushes the write buffer to synchronize all pending writes.
315 ****************************************************************************************/
316extern inline void hal_wbflush();
317
318/*****************************************************************************************
319 * This forbids code reordering by the compiler.
320 ****************************************************************************************/
321extern inline void hal_rdbar();
322
323/*****************************************************************************************
324 * This function forces the calling core in idle-low-power mode.
325 ****************************************************************************************/
326extern inline void hal_core_sleep();
327
328//////////////////////////////////////////////////////////////////////////////////////////
329//////////////////////////////////////////////////////////////////////////////////////////
330//          Remote accesses / implementation in cpu/***/remote.c
331//
332// Kernel accesses to local memory bank and peripherals can use normal C pointers.
333// kernel accesses to remote memory banks or peripherals must use the following
334// dedicated functions, that can have implementations depending on architectures.
335/////////////////////////////////////////////////////////////////////////////////////////
336//////////////////////////////////////////////////////////////////////////////////////////
337
338/*****************************************************************************************
339 * This function writes a single byte in a remote cluster.
340 * @ xp      : extended pointer to remote cluster
341 * @ data    : value to be written
342 ****************************************************************************************/
343extern inline void hal_remote_sb( xptr_t xp,
344                                  char   data );
345
346/*****************************************************************************************
347 * This function writes an aligned 32 bits word in a remote cluster.
348 * @ xp      : extended pointer to remote cluster
349 * @ data    : value to be written
350 ****************************************************************************************/
351extern inline void hal_remote_sw( xptr_t   xp,
352                                  uint32_t data );
353
354/*****************************************************************************************
355 * This function writes an aligned 64 bits word in a remote cluster.
356 * @ xp      : extended pointer to remote cluster
357 * @ data    : value to be written
358 ****************************************************************************************/
359extern inline void hal_remote_swd( xptr_t   xp,
360                                   uint64_t data );
361
362/*****************************************************************************************
363 * This function writes a pointer (32 or 64 bits) in a remote cluster.
364 * @ xp      : extended pointer to remote cluster
365 * @ pt      : value to be written
366 ****************************************************************************************/
367extern inline void hal_remote_spt( xptr_t   xp,
368                                   void   * pt );
369
370/*****************************************************************************************
371 * This function reads a single byte in a remote cluster.
372 * @ xp      : extended pointer to remote data
373 * @ return read value
374 ****************************************************************************************/
375extern inline char hal_remote_lb( xptr_t  xp );
376
377/*****************************************************************************************
378 * This function reads an aligned 32 bits word in a remote cluster.
379 * @ xp      : extended pointer to remote data
380 * @ return read value
381 ****************************************************************************************/
382extern inline uint32_t hal_remote_lw( xptr_t  xp )
383
384/*****************************************************************************************
385 * This function reads an aligned 64 bits word in a remote cluster.
386 * @ xp      : extended pointer to remote data
387 * @ return read value
388 ****************************************************************************************/
389extern inline uint64_t hal_remote_lwd( xptr_t  xp )
390
391/*****************************************************************************************
392 * This function reads a poiter (32 or 64 bits) in a remote cluster.
393 * @ xp      : extended pointer to remote data
394 * @ return read value
395 ****************************************************************************************/
396extern inline void * hal_remote_lpt( xptr_t  xp );
397
398/*****************************************************************************************
399 * This function reads an uncachable 32 bits word in a remote cluster.
400 * @ xp      : extended pointer to remote data
401 * @ return read value
402 ****************************************************************************************/
403extern inline uint32_t hal_remote_lw_unc( xptr_t  xp );
404
405/*****************************************************************************************
406 * This function makes an atomic Compare-And-Swap in a remote cluster.
407 * @ xp      : extended pointer to remote data
408 * @ old     : expected value
409 * @ new     : new value to be written
410 * @ return true if success / return false if failure
411 ****************************************************************************************/
412extern inline bool_t hal_remote_atomic_cas( xptr_t   xp,
413                                            uint32_t old,
414                                            uint32_t new );
415
416/*****************************************************************************************
417 * This function adds atomically an increment to the current value of
418 * a 32 bits integer in a remote cluster.
419 * @ xp      : extended pointer to remote data
420 * @ incr    : increment value.
421 * @ return old value (before increment) of the remote integer
422 ****************************************************************************************/
423extern inline uint32_t hal_remote_atomic_add( xptr_t    xp, 
424                                              uint32_t  incr );
425
426/*****************************************************************************************
427 * This non blocking function tries to make an atomic increment to the current
428 * value of a 32 bits integer in a remote cluster.
429 * @ xp      : extended pointer to remote data
430 * @ incr    : increment value.
431 * @ old     : local buffer address for the read value (before increment)
432 * @ return true if success / return false if failure
433 ****************************************************************************************/
434extern inline error_t hal_remote_atomic_try_add( xptr_t     xp,
435                                                 uint32_t   incr,
436                                                 uint32_t * old );
437
438/*****************************************************************************************
439 * This function makes a memcpy from a source remote buffer in kernel space to another
440 * destination remote buffer in kernel space.
441 * @ dst     : extended pointer to destination buffer
442 * @ src     : extended pointer to source buffer
443 * @ size    : number of bytes to move
444 ****************************************************************************************/
445extern inline void hal_remote_memcpy( xptr_t   dst,
446                                                      xptr_t   src, 
447                                      size_t   size );
448
449////////////////////////////////////////////////////////////////////////////////////////////
450////////////////////////////////////////////////////////////////////////////////////////////
451//           User space access / implementation in cpu/***/uspace.c
452//
453// When moving data between user space and kernel space, the user address is always
454// a virtual address, but the kernel address can be a physical address, on some
455// architectures. Therefore, data transfers must use the following functions.
456////////////////////////////////////////////////////////////////////////////////////////////
457////////////////////////////////////////////////////////////////////////////////////////////
458
459/*****************************************************************************************
460 * This function tranfers a data buffer from the user space to the kernel space.
461 * @ kdst     : destination address in kernel space
462 * @ usrc     : source address in user space
463 * @ size     : number of bytes to transfer.
464 ****************************************************************************************/
465extern void hal_copy_from_uspace( void     * kdst,
466                                  void     * usrc,
467                                  uint32_t   size );
468
469/*****************************************************************************************
470 * This function tranfers a data buffer from the kernel space to the user space.
471 * @ udst     : destination address in user space.
472 * @ ksrc     : source address in kernel space.
473 * @ size     : number of bytes to transfer.
474 ****************************************************************************************/
475extern void hal_copy_to_uspace( void     * udst,
476                                void     * ksrc,
477                                uint32_t   size );
478
479#endif  /* _HAL_CPU_H_ */
Note: See TracBrowser for help on using the repository browser.