2 * Copyright (c) 2003-2009 RMI Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.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>
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];
56 static __inline__ void
57 pic_send_perfmon_ipi(int cpu)
59 xlr_reg_t *mmio = xlr_io_mmio(XLR_IO_PIC_OFFSET);
64 pid = (cpu >> 2) & 0x7;
65 ipi = (pid << 20) | (tid << 16) | IPI_PERFMON;
67 mtx_lock_spin(&xlr_pic_lock);
68 xlr_write_reg(mmio, PIC_IPI, ipi);
69 mtx_unlock_spin(&xlr_pic_lock);
76 xlr_perfmon_clockhandler(void)
84 if (xlr_perfmon_ticks++ >= (*xlr_cpu_sampling_interval) / (XLR_PIC_HZ / (hz * 1024))) {
87 *xlr_perfmon_timer_loc += *xlr_cpu_sampling_interval;
88 xlr_perfmon_ticks = 0;
89 xlr_perfmon_sampler(NULL);
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 ==
96 pic_send_perfmon_ipi(cpu);
105 xlr_perfmon_start(void)
109 size = (NCORES * sizeof(*xlr_shared_config_area)) +
110 sizeof(*xlr_perfmon_timer_loc) +
111 sizeof(*xlr_cpu_sampling_interval);
113 xlr_shared_config_area = malloc(size, M_TEMP, M_WAITOK);
114 if (!xlr_shared_config_area) {
118 xlr_perfmon_timer_loc = (uint32_t *) (xlr_shared_config_area + NCORES);
119 xlr_cpu_sampling_interval = (uint32_t *) (xlr_perfmon_timer_loc + 1);
121 *xlr_cpu_sampling_interval = DEFAULT_CPU_SAMPLING_INTERVAL;
122 *xlr_perfmon_timer_loc = 0;
123 xlr_perfmon_ticks = 0;
125 xlr_perfmon_init_cpu(NULL);
127 smp_call_function(xlr_perfmon_init_cpu, NULL,
128 PCPU_GET(other_cpus) & 0x11111111);
130 xlr_perfmon_started = 1;
135 xlr_perfmon_stop(void)
137 xlr_perfmon_started = 0;
138 free(xlr_shared_config_area, M_TEMP);
139 xlr_shared_config_area = NULL;
143 sysctl_xlr_perfmon_start_stop(SYSCTL_HANDLER_ARGS)
145 int error, val = xlr_perfmon_started;
147 error = sysctl_handle_int(oidp, &val, 0, req);
148 if (error != 0 || req->newptr == NULL)
151 if (!xlr_perfmon_started && val != 0)
153 else if (xlr_perfmon_started && val == 0)
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");