]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/mips/rmi/perfmon_kern.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / mips / rmi / perfmon_kern.c
1 /*-
2  * Copyright (c) 2003-2009 RMI Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of RMI Corporation, nor the names of its contributors,
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * RMI_BSD */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/pcpu.h>
36 #include <sys/proc.h>
37 #include <sys/smp.h>
38 #include <sys/sysctl.h>
39 #include <machine/smp.h>
40 #include <mips/rmi/perfmon.h>
41 #include <mips/rmi/pic.h>
42 #include <sys/mutex.h>
43 #include <mips/rmi/clock.h>
44
45
46 int xlr_perfmon_started = 0;
47 struct perf_area *xlr_shared_config_area = NULL;
48 uint32_t *xlr_perfmon_timer_loc;
49 uint32_t *xlr_cpu_sampling_interval;
50 uint32_t xlr_perfmon_kernel_version = 1;        /* Future use */
51 uint32_t xlr_perfmon_ticks;
52 extern int mips_cpu_online_mask;
53 extern uint32_t cpu_ltop_map[MAXCPU];
54
55 #ifdef SMP
56 static __inline__ void 
57 pic_send_perfmon_ipi(int cpu)
58 {
59         xlr_reg_t *mmio = xlr_io_mmio(XLR_IO_PIC_OFFSET);
60         int tid, pid;
61         uint32_t ipi;
62
63         tid = cpu & 0x3;
64         pid = (cpu >> 2) & 0x7;
65         ipi = (pid << 20) | (tid << 16) | IPI_PERFMON;
66
67         mtx_lock_spin(&xlr_pic_lock);
68         xlr_write_reg(mmio, PIC_IPI, ipi);
69         mtx_unlock_spin(&xlr_pic_lock);
70 }
71
72 #endif
73
74
75 void
76 xlr_perfmon_clockhandler(void)
77 {
78 #ifdef SMP
79         int cpu;
80         int i;
81
82 #endif
83
84         if (xlr_perfmon_ticks++ >= (*xlr_cpu_sampling_interval) / (XLR_PIC_HZ / (hz * 1024))) {
85
86                 /* update timer */
87                 *xlr_perfmon_timer_loc += *xlr_cpu_sampling_interval;
88                 xlr_perfmon_ticks = 0;
89                 xlr_perfmon_sampler(NULL);
90 #ifdef SMP
91                 for (i = 0; i < NCPUS; i = i + NTHREADS) {      /* oly thread 0 */
92                         cpu = cpu_ltop_map[i];
93                         if ((mips_cpu_online_mask & (1 << i)) &&
94                             xlr_shared_config_area[cpu / NTHREADS].perf_config.magic ==
95                             PERFMON_ACTIVE_MAGIC)
96                                 pic_send_perfmon_ipi(cpu);
97                 }
98
99 #endif
100
101         }
102 }
103
104 static void
105 xlr_perfmon_start(void)
106 {
107         size_t size;
108
109         size = (NCORES * sizeof(*xlr_shared_config_area)) +
110             sizeof(*xlr_perfmon_timer_loc) +
111             sizeof(*xlr_cpu_sampling_interval);
112
113         xlr_shared_config_area = malloc(size, M_TEMP, M_WAITOK);
114         if (!xlr_shared_config_area) {
115                 /* ERROR */
116                 return;
117         }
118         xlr_perfmon_timer_loc = (uint32_t *) (xlr_shared_config_area + NCORES);
119         xlr_cpu_sampling_interval = (uint32_t *) (xlr_perfmon_timer_loc + 1);
120
121         *xlr_cpu_sampling_interval = DEFAULT_CPU_SAMPLING_INTERVAL;
122         *xlr_perfmon_timer_loc = 0;
123         xlr_perfmon_ticks = 0;
124
125         xlr_perfmon_init_cpu(NULL);
126 #ifdef SMP
127         smp_call_function(xlr_perfmon_init_cpu, NULL,
128             PCPU_GET(other_cpus) & 0x11111111);
129 #endif
130         xlr_perfmon_started = 1;
131
132 }
133
134 static void
135 xlr_perfmon_stop(void)
136 {
137         xlr_perfmon_started = 0;
138         free(xlr_shared_config_area, M_TEMP);
139         xlr_shared_config_area = NULL;
140 }
141
142 static int
143 sysctl_xlr_perfmon_start_stop(SYSCTL_HANDLER_ARGS)
144 {
145         int error, val = xlr_perfmon_started;
146
147         error = sysctl_handle_int(oidp, &val, 0, req);
148         if (error != 0 || req->newptr == NULL)
149                 return (error);
150
151         if (!xlr_perfmon_started && val != 0)
152                 xlr_perfmon_start();
153         else if (xlr_perfmon_started && val == 0)
154                 xlr_perfmon_stop();
155
156         return (0);
157 }
158
159
160 SYSCTL_NODE(_debug, OID_AUTO, xlrperf, CTLFLAG_RW, NULL, "XLR PERF Nodes");
161 SYSCTL_PROC(_debug_xlrperf, OID_AUTO, start, CTLTYPE_INT | CTLFLAG_RW,
162     &xlr_perfmon_started, 0, sysctl_xlr_perfmon_start_stop, "I", "set/unset to start/stop "
163     "performance monitoring");