xref: /netbsd-src/sys/dev/tprof/tprof_x86.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: tprof_x86.c,v 1.1 2018/07/24 09:47:35 maxv 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.1 2018/07/24 09:47:35 maxv 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
55 tprof_x86_init(void)
56 {
57 	switch (cpu_vendor) {
58 	case CPUVENDOR_AMD:
59 		return tprof_backend_register("tprof_amd", &tprof_amd_ops,
60 		    TPROF_BACKEND_VERSION);
61 	case CPUVENDOR_INTEL:
62 		return tprof_backend_register("tprof_intel", &tprof_intel_ops,
63 		    TPROF_BACKEND_VERSION);
64 	default:
65 		return ENOTSUP;
66 	}
67 }
68 
69 static int
70 tprof_x86_fini(void)
71 {
72 	switch (cpu_vendor) {
73 	case CPUVENDOR_AMD:
74 		return tprof_backend_unregister("tprof_amd");
75 	case CPUVENDOR_INTEL:
76 		return tprof_backend_unregister("tprof_intel");
77 	default:
78 		return ENOTSUP;
79 	}
80 }
81 
82 static int
83 tprof_x86_modcmd(modcmd_t cmd, void *arg)
84 {
85 	switch (cmd) {
86 	case MODULE_CMD_INIT:
87 		return tprof_x86_init();
88 	case MODULE_CMD_FINI:
89 		return tprof_x86_fini();
90 	default:
91 		return ENOTTY;
92 	}
93 }
94