2 * Copyright (c) 2009 The Regents of the University of California
3 * Barret Rhoden <brho@cs.berkeley.edu>
4 * See LICENSE for details.
9 /* SMP related functions */
12 #include <ros/common.h>
13 #include <sys/queue.h>
22 #define CPU_STATE_IRQ 0
23 #define CPU_STATE_KERNEL 1
24 #define CPU_STATE_USER 2
25 #define CPU_STATE_IDLE 3
26 #define NR_CPU_STATES 4
28 static char *cpu_state_names[NR_CPU_STATES] =
38 uintptr_t stacktop; /* must be first */
39 int coreid; /* must be second */
44 /* Process management */
45 // cur_proc should be valid on all cores that are not management cores.
46 struct proc *cur_proc; /* which process context is loaded */
47 struct proc *owning_proc; /* proc owning the core / cur_ctx */
48 uint32_t owning_vcoreid; /* vcoreid of owning proc (if applicable */
49 struct user_context *cur_ctx; /* user ctx we came in on (can be 0) */
50 struct user_context actual_ctx; /* storage for cur_ctx */
51 uint32_t __ctx_depth; /* don't access directly. see trap.h. */
52 int __lock_checking_enabled;/* == 1, enables spinlock depth checking */
53 struct kthread *cur_kthread;/* tracks the running kernel context */
54 struct kthread *spare; /* useful when restarting */
55 struct timer_chain tchain; /* for the per-core alarm */
56 unsigned int lock_depth;
57 struct trace_ring traces;
59 uint64_t last_tick_cnt;
60 uint64_t state_ticks[NR_CPU_STATES];
61 /* TODO: 64b (not sure if we'll need these at all */
67 spinlock_t immed_amsg_lock;
68 struct kernel_msg_list immed_amsgs;
69 spinlock_t routine_amsg_lock;
70 struct kernel_msg_list routine_amsgs;
71 /* profiling -- opaque to all but the profiling code. */
73 }__attribute__((aligned(ARCH_CL_SIZE)));
75 /* Allows the kernel to figure out what process is running on this core. Can be
76 * used just like a pointer to a struct proc. */
77 #define current per_cpu_info[core_id()].cur_proc
78 /* Allows the kernel to figure out what *user* ctx is on this core's stack. Can
79 * be used just like a pointer to a struct user_context. Note the distinction
80 * between kernel and user contexts. The kernel always returns to its nested,
81 * interrupted contexts via iret/etc. We never do that for user contexts. */
82 #define current_ctx per_cpu_info[core_id()].cur_ctx
84 typedef struct per_cpu_info per_cpu_info_t;
85 extern per_cpu_info_t per_cpu_info[MAX_NUM_CORES];
87 /* SMP bootup functions */
89 void smp_idle(void) __attribute__((noreturn));
90 void smp_percpu_init(void); // this must be called by each core individually
91 void __arch_pcpu_init(uint32_t coreid); /* each arch has one of these */
93 void __set_cpu_state(struct per_cpu_info *pcpui, int state);
94 void reset_cpu_state_ticks(int coreid);
96 /* SMP utility functions */
97 int smp_call_function_self(isr_t handler, void *data,
98 handler_wrapper_t **wait_wrapper);
99 int smp_call_function_all(isr_t handler, void *data,
100 handler_wrapper_t **wait_wrapper);
101 int smp_call_function_single(uint32_t dest, isr_t handler, void *data,
102 handler_wrapper_t **wait_wrapper);
103 int smp_call_wait(handler_wrapper_t *wrapper);
105 /* PCPUI Trace Rings: */
106 struct pcpu_trace_event {
112 /* If you want to add a type, use the next available number, increment NR_TYPES,
113 * use your own macro, and provide a handler. Add your handler to
114 * pcpui_tr_handlers in smp.c. */
115 #define PCPUI_TR_TYPE_NULL 0
116 #define PCPUI_TR_TYPE_KMSG 1
117 #define PCPUI_TR_TYPE_LOCKS 2
118 #define PCPUI_NR_TYPES 3
120 #ifdef CONFIG_TRACE_KMSGS
122 # define pcpui_trace_kmsg(pcpui, pc) \
124 struct pcpu_trace_event *e = get_trace_slot_racy(&pcpui->traces); \
126 e->type = PCPUI_TR_TYPE_KMSG; \
133 # define pcpui_trace_kmsg(pcpui, pc)
135 #endif /* CONFIG_TRACE_KMSGS */
138 #ifdef CONFIG_TRACE_LOCKS
140 # define pcpui_trace_locks(pcpui, lock) \
142 struct pcpu_trace_event *e = get_trace_slot_overwrite(&pcpui->traces); \
144 e->type = PCPUI_TR_TYPE_LOCKS; \
145 e->arg0 = (int)tsc2usec(read_tsc()); \
146 e->arg1 = (uintptr_t)lock; \
152 # define pcpui_trace_locks(pcpui, lock)
154 #endif /* CONFIG_TRACE_LOCKS */
156 void smp_do_in_cores(const struct core_set *cset, void (*func)(void *),
159 /* Run the handlers for all events in a pcpui ring. Can run on all cores, or
160 * just one core. 'type' selects which event type is handled (0 for all). */
161 void pcpui_tr_foreach(int coreid, int type);
162 void pcpui_tr_foreach_all(int type);
163 void pcpui_tr_reset_all(void);
164 void pcpui_tr_reset_and_clear_all(void);