2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2005-10-25 20:59:12 +04:00
|
|
|
* Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2003-05-03 20:03:26 +04:00
|
|
|
#ifndef KERNEL_SMP_H
|
|
|
|
#define KERNEL_SMP_H
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-10-26 20:13:36 +04:00
|
|
|
#include <KernelExport.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2003-05-03 20:03:26 +04:00
|
|
|
struct kernel_args;
|
|
|
|
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
// intercpu messages
|
|
|
|
enum {
|
2005-12-14 20:07:37 +03:00
|
|
|
SMP_MSG_INVALIDATE_PAGE_RANGE = 0,
|
|
|
|
SMP_MSG_INVALIDATE_PAGE_LIST,
|
|
|
|
SMP_MSG_USER_INVALIDATE_PAGES,
|
|
|
|
SMP_MSG_GLOBAL_INVALIDATE_PAGES,
|
2002-07-09 16:24:59 +04:00
|
|
|
SMP_MSG_CPU_HALT,
|
2005-10-25 22:18:11 +04:00
|
|
|
SMP_MSG_CALL_FUNCTION,
|
2013-10-21 04:38:57 +04:00
|
|
|
SMP_MSG_RESCHEDULE
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2005-10-25 22:18:11 +04:00
|
|
|
SMP_MSG_FLAG_ASYNC = 0x0,
|
|
|
|
SMP_MSG_FLAG_SYNC = 0x1,
|
|
|
|
SMP_MSG_FLAG_FREE_ARG = 0x2,
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2008-10-08 01:14:24 +04:00
|
|
|
typedef uint32 cpu_mask_t;
|
|
|
|
|
2012-06-14 16:06:55 +04:00
|
|
|
typedef void (*smp_call_func)(addr_t data1, int32 currentCPU, addr_t data2, addr_t data3);
|
2005-10-25 22:18:11 +04:00
|
|
|
|
|
|
|
|
2005-10-25 20:59:12 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2011-06-14 16:41:11 +04:00
|
|
|
bool try_acquire_spinlock(spinlock* lock);
|
|
|
|
|
2004-03-15 01:54:00 +03:00
|
|
|
status_t smp_init(struct kernel_args *args);
|
|
|
|
status_t smp_per_cpu_init(struct kernel_args *args, int32 cpu);
|
2008-06-02 06:04:12 +04:00
|
|
|
status_t smp_init_post_generic_syscalls(void);
|
2010-04-30 23:32:12 +04:00
|
|
|
bool smp_trap_non_boot_cpus(int32 cpu, uint32* rendezVous);
|
2005-10-25 20:59:12 +04:00
|
|
|
void smp_wake_up_non_boot_cpus(void);
|
2013-11-06 01:32:59 +04:00
|
|
|
void smp_cpu_rendezvous(uint32 *var, int current_cpu);
|
2012-06-14 16:06:55 +04:00
|
|
|
void smp_send_ici(int32 targetCPU, int32 message, addr_t data, addr_t data2, addr_t data3,
|
2005-10-25 20:59:12 +04:00
|
|
|
void *data_ptr, uint32 flags);
|
2012-06-14 16:06:55 +04:00
|
|
|
void smp_send_multicast_ici(cpu_mask_t cpuMask, int32 message, addr_t data,
|
|
|
|
addr_t data2, addr_t data3, void *data_ptr, uint32 flags);
|
|
|
|
void smp_send_broadcast_ici(int32 message, addr_t data, addr_t data2, addr_t data3,
|
2005-10-25 20:59:12 +04:00
|
|
|
void *data_ptr, uint32 flags);
|
2009-08-01 05:53:54 +04:00
|
|
|
void smp_send_broadcast_ici_interrupts_disabled(int32 currentCPU, int32 message,
|
2012-06-14 16:06:55 +04:00
|
|
|
addr_t data, addr_t data2, addr_t data3, void *data_ptr, uint32 flags);
|
2005-10-25 20:59:12 +04:00
|
|
|
|
|
|
|
int32 smp_get_num_cpus(void);
|
|
|
|
void smp_set_num_cpus(int32 numCPUs);
|
|
|
|
int32 smp_get_current_cpu(void);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2009-08-01 05:53:54 +04:00
|
|
|
int smp_intercpu_int_handler(int32 cpu);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-10-25 20:59:12 +04:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-01-12 01:13:11 +03:00
|
|
|
|
|
|
|
// Unless spinlock debug features are enabled, try to inline
|
|
|
|
// {acquire,release}_spinlock().
|
|
|
|
#if !DEBUG_SPINLOCKS && !B_DEBUG_SPINLOCK_CONTENTION
|
|
|
|
|
2011-06-14 16:41:11 +04:00
|
|
|
|
|
|
|
static inline bool
|
|
|
|
try_acquire_spinlock_inline(spinlock* lock)
|
|
|
|
{
|
2013-11-06 01:47:18 +04:00
|
|
|
return atomic_get_and_set((int32*)lock, 1) == 0;
|
2011-06-14 16:41:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-12 01:13:11 +03:00
|
|
|
static inline void
|
|
|
|
acquire_spinlock_inline(spinlock* lock)
|
|
|
|
{
|
2011-06-14 16:41:11 +04:00
|
|
|
if (try_acquire_spinlock_inline(lock))
|
2010-01-12 01:13:11 +03:00
|
|
|
return;
|
2010-11-26 16:55:20 +03:00
|
|
|
acquire_spinlock(lock);
|
2010-01-12 01:13:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
release_spinlock_inline(spinlock* lock)
|
|
|
|
{
|
2013-11-06 01:47:18 +04:00
|
|
|
atomic_set((int32*)lock, 0);
|
2010-01-12 01:13:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-14 16:41:11 +04:00
|
|
|
#define try_acquire_spinlock(lock) try_acquire_spinlock_inline(lock)
|
|
|
|
#define acquire_spinlock(lock) acquire_spinlock_inline(lock)
|
|
|
|
#define release_spinlock(lock) release_spinlock_inline(lock)
|
2010-01-12 01:13:11 +03:00
|
|
|
|
|
|
|
#endif // !DEBUG_SPINLOCKS && !B_DEBUG_SPINLOCK_CONTENTION
|
|
|
|
|
|
|
|
|
2013-11-07 07:20:32 +04:00
|
|
|
static inline bool
|
|
|
|
try_acquire_write_spinlock_inline(rw_spinlock* lock)
|
|
|
|
{
|
|
|
|
return atomic_test_and_set(&lock->lock, 1 << 31, 0) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
acquire_write_spinlock_inline(rw_spinlock* lock)
|
|
|
|
{
|
|
|
|
if (try_acquire_write_spinlock(lock))
|
|
|
|
return;
|
|
|
|
acquire_write_spinlock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
release_write_spinlock_inline(rw_spinlock* lock)
|
|
|
|
{
|
|
|
|
atomic_set(&lock->lock, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
try_acquire_read_spinlock_inline(rw_spinlock* lock)
|
|
|
|
{
|
|
|
|
uint32 previous = atomic_add(&lock->lock, 1);
|
|
|
|
if ((previous & (1 << 31)) == 0)
|
|
|
|
return true;
|
|
|
|
atomic_test_and_set(&lock->lock, 1 << 31, previous);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
acquire_read_spinlock_inline(rw_spinlock* lock)
|
|
|
|
{
|
|
|
|
if (try_acquire_read_spinlock(lock))
|
|
|
|
return;
|
|
|
|
acquire_read_spinlock(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
release_read_spinlock_inline(rw_spinlock* lock)
|
|
|
|
{
|
|
|
|
atomic_add(&lock->lock, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define try_acquire_read_spinlock(lock) try_acquire_read_spinlock_inline(lock)
|
|
|
|
#define acquire_read_spinlock(lock) acquire_read_spinlock_inline(lock)
|
|
|
|
#define release_read_spinlock(lock) release_read_spinlock_inline(lock)
|
|
|
|
#define try_acquire_write_spinlock(lock) \
|
|
|
|
try_acquire_write_spinlock(lock)
|
|
|
|
#define acquire_write_spinlock(lock) acquire_write_spinlock_inline(lock)
|
|
|
|
#define release_write_spinlock(lock) release_write_spinlock_inline(lock)
|
|
|
|
|
|
|
|
|
2013-11-05 07:16:13 +04:00
|
|
|
static inline bool
|
|
|
|
try_acquire_write_seqlock_inline(seqlock* lock) {
|
|
|
|
bool succeed = try_acquire_spinlock(&lock->lock);
|
|
|
|
if (succeed)
|
2013-11-06 01:32:59 +04:00
|
|
|
atomic_add((int32*)&lock->count, 1);
|
2013-11-05 07:16:13 +04:00
|
|
|
return succeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
acquire_write_seqlock_inline(seqlock* lock) {
|
|
|
|
acquire_spinlock(&lock->lock);
|
2013-11-06 01:32:59 +04:00
|
|
|
atomic_add((int32*)&lock->count, 1);
|
2013-11-05 07:16:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
release_write_seqlock_inline(seqlock* lock) {
|
2013-11-06 01:32:59 +04:00
|
|
|
atomic_add((int32*)&lock->count, 1);
|
2013-11-05 07:16:13 +04:00
|
|
|
release_spinlock(&lock->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline uint32
|
|
|
|
acquire_read_seqlock_inline(seqlock* lock) {
|
2013-11-06 01:32:59 +04:00
|
|
|
return atomic_get((int32*)&lock->count);
|
2013-11-05 07:16:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline bool
|
|
|
|
release_read_seqlock_inline(seqlock* lock, uint32 count) {
|
2013-11-06 01:32:59 +04:00
|
|
|
uint32 current = atomic_get((int32*)&lock->count);
|
2013-11-05 07:16:13 +04:00
|
|
|
|
|
|
|
return count % 2 == 0 && current == count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define try_acquire_write_seqlock(lock) try_acquire_write_seqlock_inline(lock)
|
|
|
|
#define acquire_write_seqlock(lock) acquire_write_seqlock_inline(lock)
|
|
|
|
#define release_write_seqlock(lock) release_write_seqlock_inline(lock)
|
|
|
|
#define acquire_read_seqlock(lock) acquire_read_seqlock_inline(lock)
|
|
|
|
#define release_read_seqlock(lock, count) \
|
|
|
|
release_read_seqlock_inline(lock, count)
|
|
|
|
|
|
|
|
|
2003-05-03 20:03:26 +04:00
|
|
|
#endif /* KERNEL_SMP_H */
|