1 #ifndef CYGONCE_KERNEL_SMP_HXX
2 #define CYGONCE_KERNEL_SMP_HXX
4 //==========================================================================
10 //==========================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //==========================================================================
44 //#####DESCRIPTIONBEGIN####
49 // Purpose: Kernel SMP support
50 // Description: If SMP support is configured into the kernel, then this file
51 // translates HAL defined macros into C and C++ classes and methods
52 // that can be called from the rest of the kernel. If SMP is not
53 // configured in, then the same classes and methods are defined here
54 // to operate correctly in a single CPU configuration.
56 // Usage: #include <cyg/kernel/smp.hxx>
58 //####DESCRIPTIONEND####
60 //==========================================================================
62 #include <cyg/kernel/ktypes.h>
63 #include <cyg/infra/cyg_ass.h> // assertion macros
65 #include <cyg/hal/hal_intr.h> // HAL_DISABLE_INTERRUPTS() etc.
67 #include <cyg/kernel/instrmnt.h>
69 //==========================================================================
71 #if defined(CYGPKG_KERNEL_SMP_SUPPORT) && (CYGPKG_HAL_SMP_SUPPORT)
73 //==========================================================================
74 // SMP support is included
76 #define CYG_KERNEL_SMP_ENABLED
78 // -------------------------------------------------------------------------
81 #include <cyg/hal/hal_smp.h>
83 // -------------------------------------------------------------------------
85 // These all just map straight through to the HAL.
87 #define CYGNUM_KERNEL_CPU_MAX HAL_SMP_CPU_MAX
89 #define CYG_KERNEL_CPU_COUNT() HAL_SMP_CPU_COUNT()
91 #define CYG_KERNEL_CPU_THIS() HAL_SMP_CPU_THIS()
93 #define CYG_KERNEL_CPU_NONE HAL_SMP_CPU_NONE
95 // -------------------------------------------------------------------------
98 #define CYG_KERNEL_CPU_START( __cpu ) HAL_SMP_CPU_START( __cpu )
100 #define CYG_KERNEL_CPU_RESCHEDULE_INTERRUPT( __cpu, __wait ) \
101 HAL_SMP_CPU_RESCHEDULE_INTERRUPT( __cpu, __wait )
103 #define CYG_KERNEL_CPU_TIMESLICE_INTERRUPT( __cpu, __wait ) \
104 HAL_SMP_CPU_TIMESLICE_INTERRUPT( __cpu, __wait )
106 // -------------------------------------------------------------------------
107 // Scheduler lock default implementation.
109 // This implementation should serve for most targets. However, some
110 // targets may have hardware or other features that make simple
111 // spinlocks impossible, or allow us to implement the scheduler lock
112 // in a more efficient manner. If that is the case then the HAL will
113 // implement these macros itself.
115 #ifndef HAL_SMP_SCHEDLOCK_DATA_TYPE
117 #define HAL_SMP_SCHEDLOCK_DATA_TYPE struct hal_smp_schedlock_data_type
119 struct hal_smp_schedlock_data_type {
120 HAL_SPINLOCK_TYPE spinlock;
121 volatile HAL_SMP_CPU_TYPE holder;
124 #define HAL_SMP_SCHEDLOCK_INIT( __lock, __data ) \
128 HAL_SPINLOCK_CLEAR(__data.spinlock); \
129 HAL_SPINLOCK_SPIN(__data.spinlock); \
130 __data.holder = HAL_SMP_CPU_THIS(); \
135 #define HAL_SMP_SCHEDLOCK_INC( __lock, __data ) \
138 CYG_INTERRUPT_STATE __state; \
139 HAL_DISABLE_INTERRUPTS(__state); \
140 if( __data.holder == HAL_SMP_CPU_THIS() ) \
144 CYG_INSTRUMENT_SMP(LOCK_WAIT,CYG_KERNEL_CPU_THIS(),0); \
145 HAL_SPINLOCK_SPIN(__data.spinlock); \
146 __data.holder = HAL_SMP_CPU_THIS(); \
148 CYG_INSTRUMENT_SMP(LOCK_GOT,CYG_KERNEL_CPU_THIS(),0); \
150 HAL_RESTORE_INTERRUPTS(__state); \
154 #define HAL_SMP_SCHEDLOCK_ZERO( __lock, __data ) \
157 CYG_INTERRUPT_STATE __state; \
158 HAL_DISABLE_INTERRUPTS(__state); \
159 CYG_ASSERT( __data.holder == HAL_SMP_CPU_THIS(), "Zeroing schedlock not owned by me!"); \
161 __data.holder = HAL_SMP_CPU_NONE; \
162 HAL_SPINLOCK_CLEAR(__data.spinlock); \
163 HAL_RESTORE_INTERRUPTS(__state); \
167 #define HAL_SMP_SCHEDLOCK_SET( __lock, __data, __new ) \
170 CYG_ASSERT( __data.holder == HAL_SMP_CPU_THIS(), "Setting schedlock not owned by me!"); \
177 // -------------------------------------------------------------------------
179 // This class supplies a C++ wrapper for the HAL spinlock API.
183 #ifdef HAL_SPINLOCK_SPIN
187 HAL_SPINLOCK_TYPE lock;
191 // Constructor, initialize the lock to clear
192 Cyg_SpinLock() { lock = HAL_SPINLOCK_INIT_CLEAR; };
196 // CYG_ASSERT( !test(), "spinlock still claimed");
202 HAL_SPINLOCK_SPIN(lock);
208 HAL_SPINLOCK_CLEAR(lock);
211 // Try to claim the lock. Return true if successful, false if not.
215 HAL_SPINLOCK_TRY(lock,testval);
219 // Test the current value of the lock
223 HAL_SPINLOCK_TEST(lock, testval);
228 // The following two member functions are only necessary if the
229 // spinlock is to be used in an ISR.
231 // Claim the spinlock, but also mask this CPU's interrupts while
233 void spin_intsave(CYG_INTERRUPT_STATE *state)
235 CYG_INTERRUPT_STATE s;
236 HAL_DISABLE_INTERRUPTS(s);
241 // Clear the lock, and restore the interrupt state saved in
243 void clear_intsave(CYG_INTERRUPT_STATE state)
246 HAL_RESTORE_INTERRUPTS(state);
252 // -------------------------------------------------------------------------
253 // Scheduler lock class
254 // This uses the scheduler lock API defined by the HAL, or the defaults
257 class Cyg_Scheduler_SchedLock
259 static volatile cyg_ucount32 sched_lock // lock counter
260 CYGBLD_ATTRIB_ASM_ALIAS( cyg_scheduler_sched_lock )
261 CYGBLD_ANNOTATE_VARIABLE_SCHED
264 static HAL_SMP_SCHEDLOCK_DATA_TYPE lock_data
265 CYGBLD_ANNOTATE_VARIABLE_SCHED;
269 Cyg_Scheduler_SchedLock()
271 HAL_SMP_SCHEDLOCK_INIT( sched_lock, lock_data );
274 // Increment the scheduler lock. If this takes the lock from zero
275 // to one then this code must also do whatever is necessary to
276 // serialize CPUs through the scheduler.
277 static void inc_sched_lock()
279 CYG_INSTRUMENT_SMP(LOCK_INC,CYG_KERNEL_CPU_THIS(),0);
280 HAL_SMP_SCHEDLOCK_INC( sched_lock, lock_data );
283 // Zero the scheduler lock. This will release the CPU serializing
284 // lock and allow another CPU in.
285 static void zero_sched_lock()
287 CYG_INSTRUMENT_SMP(LOCK_ZERO,CYG_KERNEL_CPU_THIS(),0);
288 CYG_ASSERT( sched_lock != 0, "Scheduler lock already zero");
289 HAL_SMP_SCHEDLOCK_ZERO( sched_lock, lock_data );
292 // Set the scheduler lock to a non-zero value. Both the scheduler
293 // lock and the new value must be non-zero.
294 static void set_sched_lock(cyg_uint32 new_lock)
296 CYG_INSTRUMENT_SMP(LOCK_SET,CYG_KERNEL_CPU_THIS(),new_lock);
297 CYG_ASSERT( new_lock > 0, "New scheduler lock value == 0");
298 CYG_ASSERT( sched_lock > 0, "Scheduler lock == 0");
299 HAL_SMP_SCHEDLOCK_SET( sched_lock, lock_data, new_lock );
302 static cyg_ucount32 get_sched_lock()
308 #define CYGIMP_KERNEL_SCHED_LOCK_DEFINITIONS \
309 volatile cyg_ucount32 Cyg_Scheduler_SchedLock::sched_lock = 1; \
310 HAL_SMP_SCHEDLOCK_DATA_TYPE Cyg_Scheduler_SchedLock::lock_data;
312 #endif // __cplusplus
314 // -------------------------------------------------------------------------
316 #else // defined(CYGSEM_KERNEL_SMP_SUPPORT) && (CYGSEM_HAL_SMP_SUPPORT)
318 //==========================================================================
319 // SMP support is NOT included.
321 #undef CYG_KERNEL_SMP_ENABLED
323 // -------------------------------------------------------------------------
325 // Supply a set of values that describe a single CPU system.
327 #ifndef HAL_SMP_CPU_TYPE
328 #define HAL_SMP_CPU_TYPE cyg_uint32
331 #define CYGNUM_KERNEL_CPU_MAX 1
333 #define CYG_KERNEL_CPU_COUNT() 1
335 #define CYG_KERNEL_CPU_THIS() 0
337 #define CYG_KERNEL_CPU_NONE -1
339 #define CYG_KERNEL_CPU_LOWPRI() CYG_KERNEL_CPU_THIS()
341 // -------------------------------------------------------------------------
343 // This single CPU version simply goes through the motions of setting
344 // and clearing the lock variable for debugging purposes.
350 volatile cyg_uint32 lock;
354 // Constructor, initialize the lock to clear
355 Cyg_SpinLock() { lock = 0; };
359 CYG_ASSERT( lock == 0, "spinlock still claimed");
362 // Spin on the lock. In this case we just set it to 1 and proceed.
365 CYG_ASSERT( lock == 0, "spinlock already claimed!");
369 // Clear the lock. Again, just set the value.
372 CYG_ASSERT( lock != 0, "spinlock already cleared!");
376 // Try to claim the lock. Return true if successful, false if not.
379 if( lock ) return false;
380 else { lock = 1; return true; }
383 // Test the current value of the lock
384 cyg_bool test() { return lock; };
387 // The following two member functions are only necessary if the
388 // spinlock is to be used in an ISR.
390 // Claim the spinlock, but also mask this CPU's interrupts while
392 void spin_intsave(CYG_INTERRUPT_STATE *state)
394 CYG_INTERRUPT_STATE s;
395 HAL_DISABLE_INTERRUPTS(s);
400 // Clear the lock, and restore the interrupt state saved in
402 void clear_intsave(CYG_INTERRUPT_STATE state)
405 HAL_RESTORE_INTERRUPTS(state);
410 // -------------------------------------------------------------------------
411 // Scheduler lock class
413 class Cyg_Scheduler_SchedLock
415 static volatile cyg_ucount32 sched_lock // lock counter
416 CYGBLD_ATTRIB_ASM_ALIAS( cyg_scheduler_sched_lock )
417 CYGBLD_ANNOTATE_VARIABLE_SCHED
420 // For non-SMP versions, the code here does the basic and obvious things.
423 Cyg_Scheduler_SchedLock()
428 // Increment the scheduler lock, possibly taking it from zero to
430 static void inc_sched_lock()
435 static void zero_sched_lock()
437 CYG_ASSERT( sched_lock != 0, "Scheduler lock already zero");
441 // Set the scheduler lock to a non-zero value. Both the scheduler
442 // lock and the new value must be non-zero.
443 static void set_sched_lock(cyg_uint32 new_lock)
445 CYG_ASSERT( new_lock > 0, "New scheduler lock value == 0");
446 CYG_ASSERT( sched_lock > 0, "Scheduler lock == 0");
447 sched_lock = new_lock;
450 static cyg_ucount32 get_sched_lock()
456 #define CYGIMP_KERNEL_SCHED_LOCK_DEFINITIONS \
457 volatile cyg_ucount32 Cyg_Scheduler_SchedLock::sched_lock = 1;
459 #endif // __cplusplus
461 #endif // defined(CYGSEM_KERNEL_SMP_SUPPORT) && (CYGSEM_HAL_SMP_SUPPORT)
463 // -------------------------------------------------------------------------
464 #endif // ifndef CYGONCE_KERNEL_SMP_HXX