1/*
2 * Copyright 2002-2006, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Copyright 2002, Travis Geiselbrecht. All rights reserved.
6 * Distributed under the terms of the NewOS License.
7 */
8#ifndef _KERNEL_CPU_H
9#define _KERNEL_CPU_H
10
11
12#include <setjmp.h>
13
14#include <int.h>
15#include <smp.h>
16#include <timer.h>
17#include <arch/cpu.h>
18
19#include <scheduler.h>
20
21
22struct kernel_args;
23
24namespace BKernel {
25	struct Thread;
26}
27
28using BKernel::Thread;
29
30
31typedef enum cpu_topology_level {
32	CPU_TOPOLOGY_SMT,
33	CPU_TOPOLOGY_CORE,
34	CPU_TOPOLOGY_PACKAGE,
35	//
36	CPU_TOPOLOGY_LEVELS
37} cpu_topology_level;
38
39typedef struct cpu_topology_node {
40	cpu_topology_level	level;
41
42	int					id;
43
44	cpu_topology_node**	children;
45	int					children_count;
46} cpu_topology_node;
47
48
49/* CPU local data structure */
50
51typedef struct CACHE_LINE_ALIGN cpu_ent {
52	int				cpu_num;
53
54	// thread.c: used to force a reschedule at quantum expiration time
55	bool			preempted;
56	timer			quantum_timer;
57
58	// keeping track of CPU activity
59	seqlock			active_time_lock;
60	bigtime_t		active_time;
61	bigtime_t		irq_time;
62	bigtime_t		interrupt_time;
63	bigtime_t		last_kernel_time;
64	bigtime_t		last_user_time;
65
66	int32			ici_counter;
67
68	// used in the kernel debugger
69	addr_t			fault_handler;
70	addr_t			fault_handler_stack_pointer;
71	jmp_buf			fault_jump_buffer;
72
73	Thread*			running_thread;
74	Thread*			previous_thread;
75	bool			invoke_scheduler;
76	bool			disabled;
77
78	// CPU topology information
79	int				topology_id[CPU_TOPOLOGY_LEVELS];
80	int				cache_id[CPU_MAX_CACHE_LEVEL];
81
82	// IRQs assigned to this CPU
83	struct list		irqs;
84	spinlock		irqs_lock;
85
86	// arch-specific stuff
87	arch_cpu_info 	arch;
88} cpu_ent;
89
90
91extern cpu_ent gCPU[];
92extern uint32 gCPUCacheLevelCount;
93
94
95#ifdef __cplusplus
96extern "C" {
97#endif
98
99status_t cpu_preboot_init_percpu(struct kernel_args *args, int curr_cpu);
100status_t cpu_init(struct kernel_args *args);
101status_t cpu_init_percpu(struct kernel_args *ka, int curr_cpu);
102status_t cpu_init_post_vm(struct kernel_args *args);
103status_t cpu_init_post_modules(struct kernel_args *args);
104bigtime_t cpu_get_active_time(int32 cpu);
105uint64 cpu_frequency(int32 cpu);
106
107cpu_ent *get_cpu_struct(void);
108extern inline cpu_ent *get_cpu_struct(void) { return &gCPU[smp_get_current_cpu()]; }
109
110status_t cpu_build_topology_tree(void);
111const cpu_topology_node* get_cpu_topology(void);
112
113void cpu_set_scheduler_mode(enum scheduler_mode mode);
114
115status_t increase_cpu_performance(int delta);
116status_t decrease_cpu_performance(int delta);
117
118void cpu_idle(void);
119void cpu_wait(int32* variable, int32 test);
120
121
122static inline void
123cpu_pause(void)
124{
125	arch_cpu_pause();
126}
127
128
129void _user_clear_caches(void *address, size_t length, uint32 flags);
130bool _user_cpu_enabled(int32 cpu);
131status_t _user_set_cpu_enabled(int32 cpu, bool enabled);
132
133#ifdef __cplusplus
134}
135#endif
136
137#endif	/* _KERNEL_CPU_H */
138