xref: /gfx-drm/usr/src/uts/common/io/drm/drm_kstat.c (revision e95fe278)
1 /*
2  * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /*
25  * Copyright (c) 2012 Intel Corporation.  All rights reserved.
26  */
27 
28 #include "drmP.h"
29 #include <sys/kstat.h>
30 #include <sys/ddi.h>
31 #include <sys/sunddi.h>
32 #include <sys/sunldi.h>
33 
34 static char *drmkstat_name[] = {
35 	"opens",
36 	"closes",
37 	"IOCTLs",
38 	"locks",
39 	"unlocks",
40 	NULL
41 };
42 
43 static int
drm_kstat_update(kstat_t * ksp,int flag)44 drm_kstat_update(kstat_t *ksp, int flag)
45 {
46 	struct drm_device *sc;
47 	kstat_named_t *knp;
48 	int tmp;
49 
50 	if (flag != KSTAT_READ)
51 		return (EACCES);
52 
53 	sc = ksp->ks_private;
54 	knp = ksp->ks_data;
55 
56 	for (tmp = 1; tmp < 6; tmp++) {
57 		(knp++)->value.ui32 = sc->counts[tmp];
58 	}
59 
60 	return (0);
61 }
62 
63 int
drm_init_kstats(struct drm_device * sc)64 drm_init_kstats(struct drm_device *sc)
65 {
66 	int instance;
67 	kstat_t *ksp;
68 	kstat_named_t *knp;
69 	char *np;
70 	char **aknp;
71 
72 	instance = ddi_get_instance(sc->devinfo);
73 	aknp = drmkstat_name;
74 	ksp = kstat_create("drm", instance, "drminfo", "drm",
75 	    KSTAT_TYPE_NAMED, sizeof (drmkstat_name)/sizeof (char *) - 1,
76 	    KSTAT_FLAG_PERSISTENT);
77 	if (ksp == NULL)
78 		return (0);
79 
80 	ksp->ks_private = sc;
81 	ksp->ks_update = drm_kstat_update;
82 	for (knp = ksp->ks_data; (np = (*aknp)) != NULL; knp++, aknp++) {
83 		kstat_named_init(knp, np, KSTAT_DATA_UINT32);
84 	}
85 	kstat_install(ksp);
86 
87 	sc->asoft_ksp = ksp;
88 
89 	return (0);
90 }
91 
92 void
drm_fini_kstats(struct drm_device * sc)93 drm_fini_kstats(struct drm_device *sc)
94 {
95 	if (sc->asoft_ksp)
96 		kstat_delete(sc->asoft_ksp);
97 	else
98 		cmn_err(CE_WARN, "attempt to delete null kstat");
99 }
100