Home | History | Annotate | Line # | Download | only in riscv
clock_machdep.c revision 1.6
      1  1.6  skrll /*	$NetBSD: clock_machdep.c,v 1.6 2023/07/26 06:13:44 skrll Exp $	*/
      2  1.3  skrll 
      3  1.1   matt /*-
      4  1.1   matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1   matt  * All rights reserved.
      6  1.1   matt  *
      7  1.1   matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   matt  * by Matt Thomas of 3am Software Foundry.
      9  1.1   matt  *
     10  1.1   matt  * Redistribution and use in source and binary forms, with or without
     11  1.1   matt  * modification, are permitted provided that the following conditions
     12  1.1   matt  * are met:
     13  1.1   matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1   matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1   matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   matt  *    documentation and/or other materials provided with the distribution.
     18  1.1   matt  *
     19  1.1   matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   matt  */
     31  1.1   matt 
     32  1.1   matt #include <sys/cdefs.h>
     33  1.1   matt 
     34  1.6  skrll __RCSID("$NetBSD: clock_machdep.c,v 1.6 2023/07/26 06:13:44 skrll Exp $");
     35  1.1   matt 
     36  1.1   matt #include <sys/param.h>
     37  1.4  skrll #include <sys/cpu.h>
     38  1.6  skrll #include <sys/device.h>
     39  1.1   matt #include <sys/systm.h>
     40  1.4  skrll #include <sys/timetc.h>
     41  1.4  skrll 
     42  1.4  skrll #include <machine/machdep.h>
     43  1.4  skrll #include <machine/sbi.h>
     44  1.4  skrll #include <machine/sysreg.h>
     45  1.4  skrll 
     46  1.4  skrll static void (*_riscv_timer_init)(void) = riscv_timer_init;
     47  1.4  skrll 
     48  1.4  skrll static uint32_t timer_frequency;
     49  1.4  skrll static uint32_t	timer_ticks_per_hz;
     50  1.4  skrll 
     51  1.4  skrll static u_int
     52  1.4  skrll timer_get_timecount(struct timecounter *tc)
     53  1.4  skrll {
     54  1.4  skrll 	return csr_time_read();
     55  1.4  skrll }
     56  1.4  skrll 
     57  1.4  skrll static struct timecounter tc =  {
     58  1.4  skrll 	.tc_get_timecount = timer_get_timecount,
     59  1.4  skrll 	.tc_counter_mask = ~0u,
     60  1.4  skrll 	.tc_name = "riscv",
     61  1.4  skrll 	.tc_quality = 100,
     62  1.4  skrll };
     63  1.4  skrll 
     64  1.4  skrll 
     65  1.4  skrll void
     66  1.4  skrll riscv_timer_frequency_set(uint32_t freq)
     67  1.4  skrll {
     68  1.4  skrll 	timer_frequency = freq;
     69  1.4  skrll 	timer_ticks_per_hz = freq / hz;
     70  1.4  skrll }
     71  1.4  skrll 
     72  1.5  skrll uint32_t
     73  1.5  skrll riscv_timer_frequency_get(void)
     74  1.5  skrll {
     75  1.5  skrll 	return timer_frequency;
     76  1.5  skrll }
     77  1.4  skrll 
     78  1.4  skrll void
     79  1.4  skrll riscv_timer_register(void (*timerfn)(void))
     80  1.4  skrll {
     81  1.4  skrll        if (_riscv_timer_init != NULL) {
     82  1.4  skrll #ifdef DIAGNOSTIC
     83  1.4  skrll                aprint_verbose("%s: timer already registered\n", __func__);
     84  1.4  skrll #endif
     85  1.4  skrll        }
     86  1.4  skrll 	_riscv_timer_init = timerfn;
     87  1.4  skrll }
     88  1.4  skrll 
     89  1.5  skrll void
     90  1.4  skrll riscv_timer_init(void)
     91  1.4  skrll {
     92  1.4  skrll 	struct cpu_info * const ci = curcpu();
     93  1.4  skrll 
     94  1.6  skrll 	evcnt_attach_dynamic(&ci->ci_ev_timer, EVCNT_TYPE_INTR,
     95  1.6  skrll 	    NULL, device_xname(ci->ci_dev), "timer");
     96  1.6  skrll 
     97  1.4  skrll 	ci->ci_lastintr = csr_time_read();
     98  1.5  skrll 	uint64_t next = ci->ci_lastintr + timer_ticks_per_hz;
     99  1.4  skrll 	ci->ci_lastintr_scheduled = next;
    100  1.4  skrll 
    101  1.4  skrll 	sbi_set_timer(next);		/* schedule next timer interrupt */
    102  1.4  skrll 	csr_sie_set(SIE_STIE);		/* enable supervisor timer intr */
    103  1.4  skrll 
    104  1.5  skrll 	if (cpu_index(ci) == 0) {
    105  1.5  skrll 		tc.tc_frequency = timer_frequency;
    106  1.5  skrll 		tc_init(&tc);
    107  1.5  skrll 	}
    108  1.4  skrll }
    109  1.4  skrll 
    110  1.4  skrll 
    111  1.4  skrll int
    112  1.4  skrll riscv_timer_intr(void *arg)
    113  1.4  skrll {
    114  1.4  skrll 	struct cpu_info * const ci = curcpu();
    115  1.4  skrll 	struct clockframe * const cf = arg;
    116  1.4  skrll 
    117  1.4  skrll 	csr_sip_clear(SIP_STIP);	/* clean pending interrupt status */
    118  1.4  skrll 
    119  1.4  skrll 	const uint64_t now = csr_time_read();
    120  1.4  skrll 
    121  1.4  skrll 	ci->ci_lastintr = now;
    122  1.4  skrll 	ci->ci_ev_timer.ev_count++;
    123  1.4  skrll 
    124  1.4  skrll 	ci->ci_lastintr_scheduled += timer_ticks_per_hz;
    125  1.4  skrll 	sbi_set_timer(ci->ci_lastintr_scheduled);
    126  1.4  skrll 
    127  1.4  skrll 	hardclock(cf);
    128  1.4  skrll 
    129  1.4  skrll 	return 1;
    130  1.4  skrll }
    131  1.4  skrll 
    132  1.1   matt 
    133  1.1   matt void
    134  1.1   matt cpu_initclocks(void)
    135  1.1   matt {
    136  1.4  skrll 	if (_riscv_timer_init == NULL)
    137  1.4  skrll 		panic("cpu_initclocks: no timer registered");
    138  1.4  skrll 	_riscv_timer_init();
    139  1.1   matt }
    140  1.1   matt 
    141  1.4  skrll 
    142  1.1   matt void
    143  1.2  skrll setstatclockrate(int newhz)
    144  1.1   matt {
    145  1.1   matt }
    146