]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/timer.c
MFV r319744,r319745: 8269 dtrace stddev aggregation is normalized incorrectly
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / timer.c
1 /*-
2  * Copyright (c) 2015-2016 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
5  * Portions of this software were developed by SRI International and the
6  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Portions of this software were developed by the University of Cambridge
10  * Computer Laboratory as part of the CTSRD Project, with support from the
11  * UK Higher Education Innovation Fund (HEIF).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 /*
36  * RISC-V Timer
37  */
38
39 #include "opt_platform.h"
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/kernel.h>
48 #include <sys/module.h>
49 #include <sys/malloc.h>
50 #include <sys/rman.h>
51 #include <sys/timeet.h>
52 #include <sys/timetc.h>
53 #include <sys/watchdog.h>
54
55 #include <sys/proc.h>
56
57 #include <machine/bus.h>
58 #include <machine/cpu.h>
59 #include <machine/intr.h>
60 #include <machine/asm.h>
61 #include <machine/trap.h>
62 #include <machine/sbi.h>
63
64 #include <dev/fdt/fdt_common.h>
65 #include <dev/ofw/openfirm.h>
66 #include <dev/ofw/ofw_bus.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68
69 #define DEFAULT_FREQ    1000000
70
71 #define TIMER_COUNTS            0x00
72 #define TIMER_MTIMECMP(cpu)     (cpu * 8)
73
74 #define READ8(_sc, _reg)        \
75         bus_space_read_8(_sc->bst, _sc->bsh, _reg)
76 #define WRITE8(_sc, _reg, _val) \
77         bus_space_write_8(_sc->bst, _sc->bsh, _reg, _val)
78
79 struct riscv_tmr_softc {
80         struct resource         *res[3];
81         bus_space_tag_t         bst;
82         bus_space_handle_t      bsh;
83         bus_space_tag_t         bst_timecmp;
84         bus_space_handle_t      bsh_timecmp;
85         void                    *ih;
86         uint32_t                clkfreq;
87         struct eventtimer       et;
88 };
89
90 static struct riscv_tmr_softc *riscv_tmr_sc = NULL;
91
92 static struct resource_spec timer_spec[] = {
93         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
94         { SYS_RES_MEMORY,       1,      RF_ACTIVE },
95         { SYS_RES_IRQ,          0,      RF_ACTIVE },
96         { -1, 0 }
97 };
98
99 static timecounter_get_t riscv_tmr_get_timecount;
100
101 static struct timecounter riscv_tmr_timecount = {
102         .tc_name           = "RISC-V Timecounter",
103         .tc_get_timecount  = riscv_tmr_get_timecount,
104         .tc_poll_pps       = NULL,
105         .tc_counter_mask   = ~0u,
106         .tc_frequency      = 0,
107         .tc_quality        = 1000,
108 };
109
110 static long
111 get_counts(struct riscv_tmr_softc *sc)
112 {
113         uint64_t counts;
114
115         counts = READ8(sc, TIMER_COUNTS);
116
117         return (counts);
118 }
119
120 static unsigned
121 riscv_tmr_get_timecount(struct timecounter *tc)
122 {
123         struct riscv_tmr_softc *sc;
124
125         sc = tc->tc_priv;
126
127         return (get_counts(sc));
128 }
129
130 static int
131 riscv_tmr_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
132 {
133         struct riscv_tmr_softc *sc;
134         uint64_t counts;
135         int cpu;
136
137         sc = (struct riscv_tmr_softc *)et->et_priv;
138
139         if (first != 0) {
140                 counts = ((uint32_t)et->et_frequency * first) >> 32;
141                 counts += READ8(sc, TIMER_COUNTS);
142                 cpu = PCPU_GET(cpuid);
143                 bus_space_write_8(sc->bst_timecmp, sc->bsh_timecmp,
144                     TIMER_MTIMECMP(cpu), counts);
145                 csr_set(sie, SIE_STIE);
146                 sbi_set_timer(counts);
147
148                 return (0);
149         }
150
151         return (EINVAL);
152
153 }
154
155 static int
156 riscv_tmr_stop(struct eventtimer *et)
157 {
158         struct riscv_tmr_softc *sc;
159
160         sc = (struct riscv_tmr_softc *)et->et_priv;
161
162         /* TODO */
163
164         return (0);
165 }
166
167 static int
168 riscv_tmr_intr(void *arg)
169 {
170         struct riscv_tmr_softc *sc;
171
172         sc = (struct riscv_tmr_softc *)arg;
173
174         csr_clear(sip, SIP_STIP);
175
176         if (sc->et.et_active)
177                 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
178
179         return (FILTER_HANDLED);
180 }
181
182 static int
183 riscv_tmr_fdt_probe(device_t dev)
184 {
185
186         if (!ofw_bus_status_okay(dev))
187                 return (ENXIO);
188
189         if (ofw_bus_is_compatible(dev, "riscv,timer")) {
190                 device_set_desc(dev, "RISC-V Timer");
191                 return (BUS_PROBE_DEFAULT);
192         }
193
194         return (ENXIO);
195 }
196
197 static int
198 riscv_tmr_attach(device_t dev)
199 {
200         struct riscv_tmr_softc *sc;
201         phandle_t node;
202         pcell_t clock;
203         int error;
204
205         sc = device_get_softc(dev);
206         if (riscv_tmr_sc)
207                 return (ENXIO);
208
209         /* Get the base clock frequency */
210         node = ofw_bus_get_node(dev);
211         if (node > 0) {
212                 error = OF_getprop(node, "clock-frequency", &clock,
213                     sizeof(clock));
214                 if (error > 0) {
215                         sc->clkfreq = fdt32_to_cpu(clock);
216                 }
217         }
218
219         if (sc->clkfreq == 0)
220                 sc->clkfreq = DEFAULT_FREQ;
221
222         if (sc->clkfreq == 0) {
223                 device_printf(dev, "No clock frequency specified\n");
224                 return (ENXIO);
225         }
226
227         if (bus_alloc_resources(dev, timer_spec, sc->res)) {
228                 device_printf(dev, "could not allocate resources\n");
229                 return (ENXIO);
230         }
231
232         /* Memory interface */
233         sc->bst = rman_get_bustag(sc->res[0]);
234         sc->bsh = rman_get_bushandle(sc->res[0]);
235         sc->bst_timecmp = rman_get_bustag(sc->res[1]);
236         sc->bsh_timecmp = rman_get_bushandle(sc->res[1]);
237
238         riscv_tmr_sc = sc;
239
240         /* Setup IRQs handler */
241         error = bus_setup_intr(dev, sc->res[2], INTR_TYPE_CLK,
242             riscv_tmr_intr, NULL, sc, &sc->ih);
243         if (error) {
244                 device_printf(dev, "Unable to alloc int resource.\n");
245                 return (ENXIO);
246         }
247
248         riscv_tmr_timecount.tc_frequency = sc->clkfreq;
249         riscv_tmr_timecount.tc_priv = sc;
250         tc_init(&riscv_tmr_timecount);
251
252         sc->et.et_name = "RISC-V Eventtimer";
253         sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
254         sc->et.et_quality = 1000;
255
256         sc->et.et_frequency = sc->clkfreq;
257         sc->et.et_min_period = (0x00000002LLU << 32) / sc->et.et_frequency;
258         sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
259         sc->et.et_start = riscv_tmr_start;
260         sc->et.et_stop = riscv_tmr_stop;
261         sc->et.et_priv = sc;
262         et_register(&sc->et);
263
264         return (0);
265 }
266
267 static device_method_t riscv_tmr_fdt_methods[] = {
268         DEVMETHOD(device_probe,         riscv_tmr_fdt_probe),
269         DEVMETHOD(device_attach,        riscv_tmr_attach),
270         { 0, 0 }
271 };
272
273 static driver_t riscv_tmr_fdt_driver = {
274         "timer",
275         riscv_tmr_fdt_methods,
276         sizeof(struct riscv_tmr_softc),
277 };
278
279 static devclass_t riscv_tmr_fdt_devclass;
280
281 EARLY_DRIVER_MODULE(timer, simplebus, riscv_tmr_fdt_driver, riscv_tmr_fdt_devclass,
282     0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
283 EARLY_DRIVER_MODULE(timer, ofwbus, riscv_tmr_fdt_driver, riscv_tmr_fdt_devclass,
284     0, 0, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
285
286 void
287 DELAY(int usec)
288 {
289         int64_t counts, counts_per_usec;
290         uint64_t first, last;
291
292         /*
293          * Check the timers are setup, if not just
294          * use a for loop for the meantime
295          */
296         if (riscv_tmr_sc == NULL) {
297                 for (; usec > 0; usec--)
298                         for (counts = 200; counts > 0; counts--)
299                                 /*
300                                  * Prevent the compiler from optimizing
301                                  * out the loop
302                                  */
303                                 cpufunc_nullop();
304                 return;
305         }
306
307         /* Get the number of times to count */
308         counts_per_usec = ((riscv_tmr_timecount.tc_frequency / 1000000) + 1);
309
310         /*
311          * Clamp the timeout at a maximum value (about 32 seconds with
312          * a 66MHz clock). *Nobody* should be delay()ing for anywhere
313          * near that length of time and if they are, they should be hung
314          * out to dry.
315          */
316         if (usec >= (0x80000000U / counts_per_usec))
317                 counts = (0x80000000U / counts_per_usec) - 1;
318         else
319                 counts = usec * counts_per_usec;
320
321         first = get_counts(riscv_tmr_sc);
322
323         while (counts > 0) {
324                 last = get_counts(riscv_tmr_sc);
325                 counts -= (int64_t)(last - first);
326                 first = last;
327         }
328 }