Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / sys / systimer.h
1 /*
2  * SYS/SYSTIMER.H
3  * 
4  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
5  * 
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  * 
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  * 
36  * $DragonFly: src/sys/sys/systimer.h,v 1.13 2007/04/30 06:57:36 dillon Exp $
37  */
38
39 #ifndef _SYS_SYSTIMER_H_
40 #define _SYS_SYSTIMER_H_
41
42 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
43
44 #ifndef _SYS_TYPES_H_
45 #include <sys/types.h>
46 #endif
47 #ifndef _SYS_QUEUE_H_
48 #include <sys/queue.h>
49 #endif
50
51 /* XXX fix sys/kinfo.h */
52 #ifndef __BOOLEAN_T_DEFINED__
53 #define __BOOLEAN_T_DEFINED__
54 typedef __boolean_t     boolean_t;
55 #endif
56
57 struct intrframe;
58
59 typedef __uint32_t      sysclock_t;
60 typedef TAILQ_HEAD(systimerq, systimer) *systimerq_t;
61 typedef void (*systimer_func_t)(struct systimer *, int, struct intrframe *);
62
63 typedef struct systimer {
64     TAILQ_ENTRY(systimer)       node;
65     systimerq_t                 queue;
66     sysclock_t                  time;           /* absolute time next intr */
67     sysclock_t                  periodic;       /* if non-zero */
68     systimer_func_t             func;
69     void                        *data;
70     int                         flags;
71     int                         freq;           /* frequency if periodic */
72     struct cputimer             *which;         /* which timer was used? */
73     struct globaldata           *gd;            /* cpu owning structure */
74 } *systimer_t;
75
76 #define SYSTF_ONQUEUE           0x0001
77 #define SYSTF_IPIRUNNING        0x0002
78 #define SYSTF_NONQUEUED         0x0004
79
80 void systimer_intr_enable(void);
81 void systimer_intr(sysclock_t *, int, struct intrframe *);
82 void systimer_add(systimer_t);
83 void systimer_del(systimer_t);
84 void systimer_init_periodic(systimer_t, systimer_func_t, void *, int);
85 void systimer_init_periodic_nq(systimer_t, systimer_func_t, void *, int);
86 void systimer_adjust_periodic(systimer_t, int);
87 void systimer_init_oneshot(systimer_t, systimer_func_t, void *, int);
88
89 /*
90  * cputimer interface.  This provides a free-running (non-interrupt) 
91  * timebase for the system.  The cputimer
92  *
93  * These variables hold the fixed cputimer frequency, determining the
94  * granularity of cputimer_count().
95  *
96  * Note that cputimer_count() always returns a full-width wrapping counter.
97  *
98  * The 64 bit versions are used for converting count values into uS or nS
99  * as follows:
100  *
101  *      usec = (cputimer_freq64_usec * count) >> 32
102  */
103
104 struct cputimer {
105     SLIST_ENTRY(cputimer) next;
106     const char  *name;
107     int         pri;
108     int         type;
109     sysclock_t  (*count)(void);
110     sysclock_t  (*fromhz)(int freq);
111     sysclock_t  (*fromus)(int us);
112     void        (*construct)(struct cputimer *, sysclock_t);
113     void        (*destruct)(struct cputimer *);
114     sysclock_t  freq;           /* in Hz */
115     int64_t     freq64_usec;    /* in (1e6 << 32) / timer_freq */
116     int64_t     freq64_nsec;    /* in (1e9 << 32) / timer_freq */
117     sysclock_t  base;           /* (implementation dependant) */
118 };
119
120 extern struct cputimer *sys_cputimer;
121
122 #define CPUTIMER_DUMMY          0
123 #define CPUTIMER_8254_SEL1      1
124 #define CPUTIMER_8254_SEL2      2
125 #define CPUTIMER_ACPI           3
126 #define CPUTIMER_VKERNEL        4
127 #define CPUTIMER_HPET           5
128 #define CPUTIMER_GEODE          6
129 #define CPUTIMER_CS5536         7
130
131 #define CPUTIMER_PRI_DUMMY      -10
132 #define CPUTIMER_PRI_8254       0
133 #define CPUTIMER_PRI_ACPI       10
134 #define CPUTIMER_PRI_HPET       15
135 #define CPUTIMER_PRI_CS5536     17
136 #define CPUTIMER_PRI_GEODE      18
137 #define CPUTIMER_PRI_VKERNEL    20
138
139 void cputimer_select(struct cputimer *, int);
140 void cputimer_register(struct cputimer *);
141 void cputimer_deregister(struct cputimer *);
142 void cputimer_set_frequency(struct cputimer *, int);
143 sysclock_t cputimer_default_fromhz(int);
144 sysclock_t cputimer_default_fromus(int);
145 void cputimer_default_construct(struct cputimer *, sysclock_t);
146 void cputimer_default_destruct(struct cputimer *);
147
148 /*
149  * Interrupt cputimer interface.
150  *
151  * Interrupt cputimers are normally one shot timers which will
152  * generate interrupt upon expiration.
153  *
154  * initclock -- Called at SI_BOOT2_CLOCKREG, SI_ORDER_SECOND.  The
155  *              interrupt timer could deregister itself here, if it
156  *              is not the selected system interrupt cputimer.  Before
157  *              this function is called, 'enable' and 'reload' will
158  *              not be called.
159  * enable    -- Enable interrupt.  It is called by each CPU.  It is
160  *              only called once during boot.  Before this function
161  *              is called, 'reload' will not be called.
162  * reload    -- Called by each CPU when it wants to to reprogram the
163  *              one shot timer expiration time.  The reload value is
164  *              measured in sys_cputimer->freq.
165  * config    -- Setup the interrupt cputimer according to the passed
166  *              in non-interrupt cputimer.  It will be called when
167  *              sys_cputimer's frequency is changed or when sys_cputimer
168  *              itself is changed.  It is also called when this interrupt
169  *              cputimer gets registered.
170  * restart   -- Start the possibly stalled interrupt cputimer immediately.
171  *              Do fixup if necessary.
172  * pmfixup   -- Called after ACPI power management is enabled.
173  */
174 struct cputimer_intr {
175         sysclock_t      freq;
176         void            (*reload)
177                         (struct cputimer_intr *, sysclock_t);
178         void            (*enable)
179                         (struct cputimer_intr *);
180         void            (*config)
181                         (struct cputimer_intr *, const struct cputimer *);
182         void            (*restart)
183                         (struct cputimer_intr *);
184         void            (*pmfixup)
185                         (struct cputimer_intr *);
186         void            (*initclock)
187                         (struct cputimer_intr *, boolean_t);
188         SLIST_ENTRY(cputimer_intr) next;
189         const char      *name;
190         int             type;   /* CPUTIMER_INTR_ */
191         int             prio;   /* CPUTIMER_INTR_PRIO_ */
192         uint32_t        caps;   /* CPUTIMER_INTR_CAP_ */
193 };
194
195 #define CPUTIMER_INTR_8254              0
196 #define CPUTIMER_INTR_LAPIC             1
197 #define CPUTIMER_INTR_VKERNEL           2
198
199 /* NOTE: Keep the new values less than CPUTIMER_INTR_PRIO_MAX */
200 #define CPUTIMER_INTR_PRIO_8254         0
201 #define CPUTIMER_INTR_PRIO_LAPIC        10
202 #define CPUTIMER_INTR_PRIO_VKERNEL      20
203 #define CPUTIMER_INTR_PRIO_MAX          1000
204
205 #define CPUTIMER_INTR_CAP_NONE          0
206 #define CPUTIMER_INTR_CAP_PS            0x1     /* works during powersaving */
207
208 /*
209  * Interrupt cputimer implementation interfaces
210  *
211  * NOTE:
212  * cputimer_intr_deregister() is _not_ allowed to be called
213  * with the currently selected interrupt cputimer.
214  */
215 void cputimer_intr_register(struct cputimer_intr *);
216 void cputimer_intr_deregister(struct cputimer_intr *);
217 int  cputimer_intr_select(struct cputimer_intr *, int);
218
219 /*
220  * Interrupt cputimer implementation helper functions
221  *
222  * default_enable    -- NOP
223  * default_restart   -- reload(0)
224  * default_config    -- NOP
225  * default_pmfixup   -- NOP
226  * default_initclock -- NOP
227  */
228 void cputimer_intr_default_enable(struct cputimer_intr *);
229 void cputimer_intr_default_restart(struct cputimer_intr *);
230 void cputimer_intr_default_config(struct cputimer_intr *,
231                                   const struct cputimer *);
232 void cputimer_intr_default_pmfixup(struct cputimer_intr *);
233 void cputimer_intr_default_initclock(struct cputimer_intr *, boolean_t);
234
235 /*
236  * Interrupt cputimer external interfaces
237  */
238 void cputimer_intr_enable(void);
239 void cputimer_intr_pmfixup(void);
240 void cputimer_intr_config(const struct cputimer *);
241 void cputimer_intr_reload(sysclock_t);
242 void cputimer_intr_restart(void);
243 int  cputimer_intr_select_caps(uint32_t);
244
245 #endif  /* _KERNEL || _KERNEL_STRUCTURES */
246
247 #endif  /* !_SYS_SYSTIMER_H_ */