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