xref: /netbsd-src/sys/dev/tprof/tprof_x86.c (revision caba1a6f4751146dacb61d492a3ac7cedc54f91f)
1*caba1a6fSryo /*	$NetBSD: tprof_x86.c,v 1.2 2022/12/01 00:32:52 ryo Exp $	*/
2ba9c3305Smaxv 
3ba9c3305Smaxv /*
4ba9c3305Smaxv  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5ba9c3305Smaxv  * All rights reserved.
6ba9c3305Smaxv  *
7ba9c3305Smaxv  * This code is derived from software contributed to The NetBSD Foundation
8ba9c3305Smaxv  * by Maxime Villard.
9ba9c3305Smaxv  *
10ba9c3305Smaxv  * Redistribution and use in source and binary forms, with or without
11ba9c3305Smaxv  * modification, are permitted provided that the following conditions
12ba9c3305Smaxv  * are met:
13ba9c3305Smaxv  * 1. Redistributions of source code must retain the above copyright
14ba9c3305Smaxv  *    notice, this list of conditions and the following disclaimer.
15ba9c3305Smaxv  * 2. Redistributions in binary form must reproduce the above copyright
16ba9c3305Smaxv  *    notice, this list of conditions and the following disclaimer in the
17ba9c3305Smaxv  *    documentation and/or other materials provided with the distribution.
18ba9c3305Smaxv  *
19ba9c3305Smaxv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ba9c3305Smaxv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ba9c3305Smaxv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ba9c3305Smaxv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ba9c3305Smaxv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ba9c3305Smaxv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ba9c3305Smaxv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ba9c3305Smaxv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ba9c3305Smaxv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ba9c3305Smaxv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ba9c3305Smaxv  * POSSIBILITY OF SUCH DAMAGE.
30ba9c3305Smaxv  */
31ba9c3305Smaxv 
32ba9c3305Smaxv #include <sys/cdefs.h>
33*caba1a6fSryo __KERNEL_RCSID(0, "$NetBSD: tprof_x86.c,v 1.2 2022/12/01 00:32:52 ryo Exp $");
34ba9c3305Smaxv 
35ba9c3305Smaxv #include <sys/param.h>
36ba9c3305Smaxv #include <sys/systm.h>
37ba9c3305Smaxv #include <sys/device.h>
38ba9c3305Smaxv #include <sys/kernel.h>
39ba9c3305Smaxv #include <sys/module.h>
40ba9c3305Smaxv 
41ba9c3305Smaxv #include <sys/cpu.h>
42ba9c3305Smaxv 
43ba9c3305Smaxv #include <dev/tprof/tprof.h>
44ba9c3305Smaxv 
45ba9c3305Smaxv #include <machine/cpufunc.h>
46ba9c3305Smaxv #include <machine/cputypes.h>	/* CPUVENDOR_* */
47ba9c3305Smaxv #include <machine/cpuvar.h>	/* cpu_vendor */
48ba9c3305Smaxv 
49ba9c3305Smaxv MODULE(MODULE_CLASS_DRIVER, tprof_x86, "tprof");
50ba9c3305Smaxv 
51ba9c3305Smaxv extern const tprof_backend_ops_t tprof_amd_ops;
52ba9c3305Smaxv extern const tprof_backend_ops_t tprof_intel_ops;
53ba9c3305Smaxv 
54ba9c3305Smaxv static int
tprof_x86_init(void)55ba9c3305Smaxv tprof_x86_init(void)
56ba9c3305Smaxv {
57*caba1a6fSryo 	const tprof_backend_ops_t *ops;
58*caba1a6fSryo 	const char *name;
59*caba1a6fSryo 	int ncounters;
60*caba1a6fSryo 
61ba9c3305Smaxv 	switch (cpu_vendor) {
62ba9c3305Smaxv 	case CPUVENDOR_AMD:
63*caba1a6fSryo 		name = "tprof_amd";
64*caba1a6fSryo 		ops = &tprof_amd_ops;
65*caba1a6fSryo 		break;
66ba9c3305Smaxv 	case CPUVENDOR_INTEL:
67*caba1a6fSryo 		name = "tprof_intel";
68*caba1a6fSryo 		ops = &tprof_intel_ops;
69*caba1a6fSryo 		break;
70ba9c3305Smaxv 	default:
71ba9c3305Smaxv 		return ENOTSUP;
72ba9c3305Smaxv 	}
73*caba1a6fSryo 
74*caba1a6fSryo 	ncounters = ops->tbo_ncounters();
75*caba1a6fSryo 	if (ncounters == 0)
76*caba1a6fSryo 		return ENOTSUP;
77*caba1a6fSryo 
78*caba1a6fSryo 	return tprof_backend_register(name, ops, TPROF_BACKEND_VERSION);
79ba9c3305Smaxv }
80ba9c3305Smaxv 
81ba9c3305Smaxv static int
tprof_x86_fini(void)82ba9c3305Smaxv tprof_x86_fini(void)
83ba9c3305Smaxv {
84ba9c3305Smaxv 	switch (cpu_vendor) {
85ba9c3305Smaxv 	case CPUVENDOR_AMD:
86ba9c3305Smaxv 		return tprof_backend_unregister("tprof_amd");
87ba9c3305Smaxv 	case CPUVENDOR_INTEL:
88ba9c3305Smaxv 		return tprof_backend_unregister("tprof_intel");
89ba9c3305Smaxv 	default:
90ba9c3305Smaxv 		return ENOTSUP;
91ba9c3305Smaxv 	}
92ba9c3305Smaxv }
93ba9c3305Smaxv 
94ba9c3305Smaxv static int
tprof_x86_modcmd(modcmd_t cmd,void * arg)95ba9c3305Smaxv tprof_x86_modcmd(modcmd_t cmd, void *arg)
96ba9c3305Smaxv {
97ba9c3305Smaxv 	switch (cmd) {
98ba9c3305Smaxv 	case MODULE_CMD_INIT:
99ba9c3305Smaxv 		return tprof_x86_init();
100ba9c3305Smaxv 	case MODULE_CMD_FINI:
101ba9c3305Smaxv 		return tprof_x86_fini();
102ba9c3305Smaxv 	default:
103ba9c3305Smaxv 		return ENOTTY;
104ba9c3305Smaxv 	}
105ba9c3305Smaxv }
106