xref: /netbsd-src/external/cddl/osnet/dev/cyclic/arm/cyclic_machdep.c (revision ba2539a9805a0544ff82c0003cc02fe1eee5603d)
1 /*	$NetBSD: cyclic_machdep.c,v 1.2 2018/05/28 21:05:02 chs Exp $	*/
2 
3 /*
4  * Copyright 2006-2008 John Birrell <jb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: head/sys/cddl/dev/cyclic/i386/cyclic_machdep.c 222813 2011-06-07 08:46:13Z attilio $
28  *
29  */
30 
31 #include <arm/armreg.h>
32 
33 static void enable(cyb_arg_t);
34 static void disable(cyb_arg_t);
35 static void reprogram(cyb_arg_t, hrtime_t);
36 static void xcall(cyb_arg_t, cpu_t *, cyc_func_t, void *);
37 
38 static cyc_backend_t	be	= {
39 	NULL,		/* cyb_configure */
40 	NULL,		/* cyb_unconfigure */
41 	enable,
42 	disable,
43 	reprogram,
44 	xcall,
45 	NULL		/* cyb_arg_t cyb_arg */
46 };
47 
48 static void
cyclic_ap_start(void * dummy)49 cyclic_ap_start(void *dummy)
50 {
51 	/* Initialise the rest of the CPUs. */
52 	cyclic_mp_init();
53 }
54 
55 SYSINIT(cyclic_ap_start, SI_SUB_SMP, SI_ORDER_ANY, cyclic_ap_start, NULL);
56 
57 /*
58  *  Machine dependent cyclic subsystem initialisation.
59  */
60 static void
cyclic_machdep_init(void)61 cyclic_machdep_init(void)
62 {
63 	/* Register the cyclic backend. */
64 	cyclic_init(&be);
65 #ifdef __NetBSD__
66 	cyclic_ap_start(NULL);
67 #endif
68 }
69 
70 static void
cyclic_machdep_uninit(void)71 cyclic_machdep_uninit(void)
72 {
73 	int i;
74 
75 	for (i = 0; i <= mp_maxid; i++)
76 		/* Reset the cyclic clock callback hook. */
77 		cyclic_clock_func[i] = NULL;
78 
79 	/* De-register the cyclic backend. */
80 	cyclic_uninit();
81 }
82 
83 static hrtime_t exp_due[MAXCPU];
84 
85 /*
86  * This function is the one registered by the machine dependent
87  * initialiser as the callback for high speed timer events.
88  */
89 static void
cyclic_clock(struct clockframe * frame)90 cyclic_clock(struct clockframe *frame)
91 {
92 	cpu_t *c = &solaris_cpu[cpu_number()];
93 
94 	if (c->cpu_cyclic != NULL && gethrtime() >= exp_due[cpu_number()]) {
95 		if (TRAPF_USERMODE(frame)) {
96 			c->cpu_profile_pc = 0;
97 			c->cpu_profile_upc = TRAPF_PC(frame);
98 		} else {
99 			c->cpu_profile_pc = TRAPF_PC(frame);
100 			c->cpu_profile_upc = 0;
101 		}
102 
103 		c->cpu_intr_actv = 1;
104 
105 		/* Fire any timers that are due. */
106 		cyclic_fire(c);
107 
108 		c->cpu_intr_actv = 0;
109 	}
110 }
111 
enable(cyb_arg_t arg)112 static void enable(cyb_arg_t arg)
113 {
114 	/* Register the cyclic clock callback function. */
115 	cyclic_clock_func[cpu_number()] = cyclic_clock;
116 }
117 
disable(cyb_arg_t arg)118 static void disable(cyb_arg_t arg)
119 {
120 	/* Reset the cyclic clock callback function. */
121 	cyclic_clock_func[cpu_number()] = NULL;
122 }
123 
reprogram(cyb_arg_t arg,hrtime_t exp)124 static void reprogram(cyb_arg_t arg, hrtime_t exp)
125 {
126 	exp_due[cpu_number()] = exp;
127 }
128 
129 #ifdef __NetBSD__
xcall_func(void * arg0,void * arg1)130 static void xcall_func(void *arg0, void *arg1)
131 {
132 	cyc_func_t func;
133 
134 	func = arg0;
135 	(*func)(arg1);
136 }
137 #endif
138 
xcall(cyb_arg_t arg,cpu_t * c,cyc_func_t func,void * param)139 static void xcall(cyb_arg_t arg, cpu_t *c, cyc_func_t func, void *param)
140 {
141 #ifdef __NetBSD__
142 	uint64_t xc;
143 
144 	xc = xc_unicast(XC_HIGHPRI, xcall_func, func, param, cpu_lookup(c->cpuid));
145 	xc_wait(xc);
146 #else
147 	smp_rendezvous_cpus((cpumask_t) (1 << c->cpuid),
148 	    smp_no_rendevous_barrier, func, smp_no_rendevous_barrier, param);
149 #endif
150 }
151