Add interrupt cputimer interface.
[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 #ifndef _SYS_TYPES_H_
43 #include <sys/types.h>
44 #endif
45 #ifndef _SYS_QUEUE_H_
46 #include <sys/queue.h>
47 #endif
48
49 struct intrframe;
50
51 typedef __uint32_t      sysclock_t;
52 typedef TAILQ_HEAD(systimerq, systimer) *systimerq_t;
53 typedef void (*systimer_func_t)(struct systimer *);
54 typedef void (*systimer_func2_t)(struct systimer *, struct intrframe *);
55
56 typedef struct systimer {
57     TAILQ_ENTRY(systimer)       node;
58     systimerq_t                 queue;
59     sysclock_t                  time;           /* absolute time next intr */
60     sysclock_t                  periodic;       /* if non-zero */
61     systimer_func2_t            func;
62     void                        *data;
63     int                         flags;
64     int                         freq;           /* frequency if periodic */
65     struct cputimer             *which;         /* which timer was used? */
66     struct globaldata           *gd;            /* cpu owning structure */
67 } *systimer_t;
68
69 #define SYSTF_ONQUEUE           0x0001
70 #define SYSTF_IPIRUNNING        0x0002
71 #define SYSTF_NONQUEUED         0x0004
72
73 void systimer_intr_enable(void);
74 void systimer_intr(sysclock_t *, int, struct intrframe *);
75 void systimer_add(systimer_t);
76 void systimer_del(systimer_t);
77 void systimer_init_periodic(systimer_t, void *, void *, int);
78 void systimer_init_periodic_nq(systimer_t, void *, void *, int);
79 void systimer_adjust_periodic(systimer_t, int);
80 void systimer_init_oneshot(systimer_t, void *, void *, int);
81
82 /*
83  * cputimer interface.  This provides a free-running (non-interrupt) 
84  * timebase for the system.  The cputimer
85  *
86  * These variables hold the fixed cputimer frequency, determining the
87  * granularity of cputimer_count().
88  *
89  * Note that cputimer_count() always returns a full-width wrapping counter.
90  *
91  * The 64 bit versions are used for converting count values into uS or nS
92  * as follows:
93  *
94  *      usec = (cputimer_freq64_usec * count) >> 32
95  */
96
97 struct cputimer {
98     SLIST_ENTRY(cputimer) next;
99     const char  *name;
100     int         pri;
101     int         type;
102     sysclock_t  (*count)(void);
103     sysclock_t  (*fromhz)(int freq);
104     sysclock_t  (*fromus)(int us);
105     void        (*construct)(struct cputimer *, sysclock_t);
106     void        (*destruct)(struct cputimer *);
107     sysclock_t  freq;           /* in Hz */
108     int64_t     freq64_usec;    /* in (1e6 << 32) / timer_freq */
109     int64_t     freq64_nsec;    /* in (1e9 << 32) / timer_freq */
110     sysclock_t  base;           /* (implementation dependant) */
111 };
112
113 extern struct cputimer *sys_cputimer;
114
115 #define CPUTIMER_DUMMY          0
116 #define CPUTIMER_8254_SEL1      1
117 #define CPUTIMER_8254_SEL2      2
118 #define CPUTIMER_ACPI           3
119 #define CPUTIMER_VKERNEL        4
120 #define CPUTIMER_HPET           5
121
122 #define CPUTIMER_PRI_DUMMY      -10
123 #define CPUTIMER_PRI_8254       0
124 #define CPUTIMER_PRI_ACPI       10
125 #define CPUTIMER_PRI_HPET       15
126 #define CPUTIMER_PRI_VKERNEL    20
127
128 void cputimer_select(struct cputimer *, int);
129 void cputimer_register(struct cputimer *);
130 void cputimer_deregister(struct cputimer *);
131 void cputimer_set_frequency(struct cputimer *, int);
132 sysclock_t cputimer_default_fromhz(int);
133 sysclock_t cputimer_default_fromus(int);
134 void cputimer_default_construct(struct cputimer *, sysclock_t);
135 void cputimer_default_destruct(struct cputimer *);
136
137 /*
138  * Interrupt cputimer interface.
139  *
140  * Interrupt cputimers are normally one shot timers which will
141  * generate interrupt upon expiration.
142  *
143  * initclock -- Called at SI_BOOT2_CLOCKREG, SI_ORDER_SECOND.  The
144  *              interrupt timer could deregister itself here, if it
145  *              is not the selected system interrupt cputimer.  Before
146  *              this function is called, 'enable' and 'reload' will
147  *              not be called.
148  * enable    -- Enable interrupt.  It is called by each CPU.  It is
149  *              only called once during boot.  Before this function
150  *              is called, 'reload' will not be called.
151  * reload    -- Called by each CPU when it wants to to reprogram the
152  *              one shot timer expiration time.  The reload value is
153  *              measured in sys_cputimer->freq.
154  * config    -- Setup the interrupt cputimer according to the passed
155  *              in non-interrupt cputimer.  It will be called when
156  *              sys_cputimer's frequency is changed or when sys_cputimer
157  *              itself is changed.  It is also called when this interrupt
158  *              cputimer gets registered.
159  * restart   -- Start the possibly stalled interrupt cputimer immediately.
160  *              Do fixup if necessary.
161  * pmfixup   -- Called after ACPI power management is enabled.
162  */
163 struct cputimer_intr {
164         sysclock_t      freq;
165         void            (*reload)
166                         (struct cputimer_intr *, sysclock_t);
167         void            (*enable)
168                         (struct cputimer_intr *);
169         void            (*config)
170                         (struct cputimer_intr *, const struct cputimer *);
171         void            (*restart)
172                         (struct cputimer_intr *);
173         void            (*pmfixup)
174                         (struct cputimer_intr *);
175         void            (*initclock)
176                         (struct cputimer_intr *, boolean_t);
177         SLIST_ENTRY(cputimer_intr) next;
178         const char      *name;
179         int             type;   /* CPUTIMER_INTR_ */
180         int             prio;   /* CPUTIMER_INTR_PRIO_ */
181         uint32_t        caps;   /* CPUTIMER_INTR_CAP_ */
182 };
183
184 #define CPUTIMER_INTR_8254              0
185 #define CPUTIMER_INTR_LAPIC             1
186 #define CPUTIMER_INTR_VKERNEL           2
187
188 /* NOTE: Keep the new values less than CPUTIMER_INTR_PRIO_MAX */
189 #define CPUTIMER_INTR_PRIO_8254         0
190 #define CPUTIMER_INTR_PRIO_LAPIC        10
191 #define CPUTIMER_INTR_PRIO_VKERNEL      20
192 #define CPUTIMER_INTR_PRIO_MAX          1000
193
194 #define CPUTIMER_INTR_CAP_NONE          0
195 #define CPUTIMER_INTR_CAP_PS            0x1     /* works during powersaving */
196
197 /*
198  * Interrupt cputimer implementation interfaces
199  *
200  * NOTE:
201  * cputimer_intr_deregister() is _not_ allowed to be called
202  * with the currently selected interrupt cputimer.
203  */
204 void cputimer_intr_register(struct cputimer_intr *);
205 void cputimer_intr_deregister(struct cputimer_intr *);
206 int  cputimer_intr_select(struct cputimer_intr *, int);
207
208 /*
209  * Interrupt cputimer implementation helper functions
210  *
211  * default_enable    -- NOP
212  * default_restart   -- reload(0)
213  * default_config    -- NOP
214  * default_pmfixup   -- NOP
215  * default_initclock -- NOP
216  */
217 void cputimer_intr_default_enable(struct cputimer_intr *);
218 void cputimer_intr_default_restart(struct cputimer_intr *);
219 void cputimer_intr_default_config(struct cputimer_intr *,
220                                   const struct cputimer *);
221 void cputimer_intr_default_pmfixup(struct cputimer_intr *);
222 void cputimer_intr_default_initclock(struct cputimer_intr *, boolean_t);
223
224 /*
225  * Interrupt cputimer external interfaces
226  */
227 void cputimer_intr_enable(void);
228 void cputimer_intr_pmfixup(void);
229 void cputimer_intr_config(const struct cputimer *);
230 void cputimer_intr_reload(sysclock_t);
231 void cputimer_intr_restart(void);
232 int  cputimer_intr_select_caps(uint32_t);
233
234 #endif  /* !_SYS_SYSTIMER_H_ */