systimer/cputimer: Add {systimer,cputimer}_intr_enable()
[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 *info);
54 typedef void (*systimer_func2_t)(struct systimer *info, struct intrframe *frame);
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 void systimer_changed(void);
82
83 /*
84  * cputimer interface.  This provides a free-running (non-interrupt) 
85  * timebase for the system.  The cputimer
86  *
87  * These variables hold the fixed cputimer frequency, determining the
88  * granularity of cputimer_count().  
89  *
90  * The 64 bit versions are used for converting count values into uS or nS
91  * as follows:
92  *
93  *      usec = (cputimer_freq64_usec * count) >> 32
94  */
95
96 struct cputimer {
97     SLIST_ENTRY(cputimer) next;
98     const char  *name;
99     int         pri;
100     int         type;
101     sysclock_t  (*count)(void);
102     sysclock_t  (*fromhz)(int freq);
103     sysclock_t  (*fromus)(int us);
104     void        (*construct)(struct cputimer *cputimer, sysclock_t oldclock);
105     void        (*destruct)(struct cputimer *cputimer);
106     sysclock_t  freq;           /* in Hz */
107     int64_t     freq64_usec;    /* in (1e6 << 32) / timer_freq */
108     int64_t     freq64_nsec;    /* in (1e9 << 32) / timer_freq */
109     sysclock_t  base;           /* (implementation dependant) */
110 };
111
112 extern struct cputimer *sys_cputimer;
113
114 #define CPUTIMER_DUMMY          0
115 #define CPUTIMER_8254_SEL1      1
116 #define CPUTIMER_8254_SEL2      2
117 #define CPUTIMER_ACPI           3
118 #define CPUTIMER_VKERNEL        4
119 #define CPUTIMER_HPET           5
120
121 #define CPUTIMER_PRI_DUMMY      -10
122 #define CPUTIMER_PRI_8254       0
123 #define CPUTIMER_PRI_ACPI       10
124 #define CPUTIMER_PRI_HPET       15
125 #define CPUTIMER_PRI_VKERNEL    20
126
127 /*
128  * note that cputimer_count() always returns a full-width wrapping counter.
129  */
130 void cputimer_select(struct cputimer *, int);
131 void cputimer_register(struct cputimer *);
132 void cputimer_deregister(struct cputimer *);
133 void cputimer_set_frequency(struct cputimer *, int);
134 sysclock_t cputimer_default_fromhz(int);
135 sysclock_t cputimer_default_fromus(int);
136 void cputimer_default_construct(struct cputimer *, sysclock_t);
137 void cputimer_default_destruct(struct cputimer *);
138
139 void cputimer_intr_enable(void);
140 void cputimer_intr_config(struct cputimer *);
141 void cputimer_intr_reload(sysclock_t);
142
143 #endif
144