summaryrefslogtreecommitdiff
path: root/usr/src/uts/i86pc/io/pci/pci_kstats.c
blob: a387ba1ff8543701bdba9743e46f754ea03e4125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *	Kstat support for X86 PCI driver
 */

#include <sys/conf.h>
#include <sys/mach_intr.h>
#include <sys/psm.h>
#include <sys/clock.h>
#include <sys/apic.h>
#include <io/pci/pci_var.h>

typedef struct pci_kstat_private {
	ddi_intr_handle_impl_t	*hdlp;
	dev_info_t		*rootnex_dip;
} pci_kstat_private_t;

static struct {
	kstat_named_t ihks_name;
	kstat_named_t ihks_type;
	kstat_named_t ihks_cpu;
	kstat_named_t ihks_pil;
	kstat_named_t ihks_time;
	kstat_named_t ihks_ino;
	kstat_named_t ihks_cookie;
	kstat_named_t ihks_devpath;
	kstat_named_t ihks_buspath;
} pci_ks_template = {
	{ "name",	KSTAT_DATA_CHAR },
	{ "type",	KSTAT_DATA_CHAR },
	{ "cpu",	KSTAT_DATA_UINT64 },
	{ "pil",	KSTAT_DATA_UINT64 },
	{ "time",	KSTAT_DATA_UINT64 },
	{ "ino",	KSTAT_DATA_UINT64 },
	{ "cookie",	KSTAT_DATA_UINT64 },
	{ "devpath",	KSTAT_DATA_STRING },
	{ "buspath",	KSTAT_DATA_STRING },
};

static char ih_devpath[MAXPATHLEN];
static char ih_buspath[MAXPATHLEN];
static uint32_t pci_ks_inst;
static kmutex_t pci_ks_template_lock;

/*ARGSUSED*/
static int
pci_ih_ks_update(kstat_t *ksp, int rw)
{
	pci_kstat_private_t *private_data =
	    (pci_kstat_private_t *)ksp->ks_private;
	dev_info_t *rootnex_dip = private_data->rootnex_dip;
	ddi_intr_handle_impl_t *ih_p = private_data->hdlp;
	dev_info_t *dip = ih_p->ih_dip;
	int maxlen = sizeof (pci_ks_template.ihks_name.value.c);
	apic_get_intr_t	intrinfo;

	(void) snprintf(pci_ks_template.ihks_name.value.c, maxlen, "%s%d",
	    ddi_driver_name(dip), ddi_get_instance(dip));
	(void) ddi_pathname(dip, ih_devpath);
	(void) ddi_pathname(rootnex_dip, ih_buspath);
	kstat_named_setstr(&pci_ks_template.ihks_devpath, ih_devpath);
	kstat_named_setstr(&pci_ks_template.ihks_buspath, ih_buspath);

	/*
	 * ih_p->ih_vector really has an IRQ.  Ask pci_get_intr_from_vecirq to
	 * return a vector since that's what PCItool will require intrd to use.
	 *
	 * PCItool will change the CPU routing of the IRQ that vector maps to.
	 *
	 * Note that although possibly multiple vectors can map to an IRQ, the
	 * vector returned below will always be the same for a given IRQ
	 * specified, and so all kstats for a given IRQ will report the same
	 * value for the ino field.
	 *
	 * ---
	 *
	 * Check for the enabled state, since kstats are set up when the ISR is
	 * added, not enabled.  There may be a period where interrupts are not
	 * enabled even though they may have been added.
	 *
	 * It is also possible that the vector is for a dummy interrupt.
	 * pci_get_intr_from_vecirq will return failure in this case.
	 */
	intrinfo.avgi_cpu_id = 0; /* In case pci_get_intr_from_vecirq fails */
	intrinfo.avgi_req_flags = PSMGI_REQ_CPUID | PSMGI_REQ_VECTOR;
	if ((ih_p->ih_state != DDI_IHDL_STATE_ENABLE) ||
	    (pci_get_intr_from_vecirq(&intrinfo,  ih_p->ih_vector, IS_IRQ) !=
	    DDI_SUCCESS) ||
	    (intrinfo.avgi_cpu_id & PSMGI_CPU_FLAGS)) {

		(void) strcpy(pci_ks_template.ihks_type.value.c, "disabled");
		pci_ks_template.ihks_pil.value.ui64 = 0;
		pci_ks_template.ihks_time.value.ui64 = 0;
		pci_ks_template.ihks_cookie.value.ui64 = 0;
		pci_ks_template.ihks_cpu.value.ui64 = 0;
		pci_ks_template.ihks_ino.value.ui64 = 0;

		/* Interrupt is user-bound.  Remove kstat. */
		if (intrinfo.avgi_cpu_id & PSMGI_CPU_FLAGS)
			(void) taskq_dispatch(system_taskq,
			    (void (*)(void *))pci_kstat_delete, ksp, TQ_SLEEP);

		return (0);
	}

	/*
	 * Interrupt is valid (not a dummy), not user-bound to a specific cpu,
	 * and enabled.  Update kstat fields.
	 */
	switch (ih_p->ih_type) {
	case DDI_INTR_TYPE_MSI:
		(void) strcpy(pci_ks_template.ihks_type.value.c, "msi");
		break;
	case DDI_INTR_TYPE_MSIX:
		(void) strcpy(pci_ks_template.ihks_type.value.c, "msix");
		break;
	default:
		(void) strcpy(pci_ks_template.ihks_type.value.c, "fixed");
		break;
	}
	pci_ks_template.ihks_pil.value.ui64 = ih_p->ih_pri;
	pci_ks_template.ihks_time.value.ui64 =
	    ((ihdl_plat_t *)ih_p->ih_private)->ip_ticks;
	tsc_scalehrtime((hrtime_t *)&pci_ks_template.ihks_time.value.ui64);
	pci_ks_template.ihks_cookie.value.ui64 = ih_p->ih_vector;
	/* CPU won't be user bound at this point. */
	pci_ks_template.ihks_cpu.value.ui64 = intrinfo.avgi_cpu_id;
	pci_ks_template.ihks_ino.value.ui64 = intrinfo.avgi_vector;

	return (0);
}


void pci_kstat_create(kstat_t **kspp, dev_info_t *rootnex_dip,
    ddi_intr_handle_impl_t *hdlp)
{
	pci_kstat_private_t *private_data;

	*kspp = kstat_create("pci_intrs", atomic_inc_32_nv(&pci_ks_inst),
	    _MODULE_NAME, "interrupts", KSTAT_TYPE_NAMED,
	    sizeof (pci_ks_template) / sizeof (kstat_named_t),
	    KSTAT_FLAG_VIRTUAL);
	if (*kspp != NULL) {

		private_data =
		    kmem_zalloc(sizeof (pci_kstat_private_t), KM_SLEEP);
		private_data->hdlp = hdlp;
		private_data->rootnex_dip = rootnex_dip;

		(*kspp)->ks_private = private_data;
		(*kspp)->ks_data_size += MAXPATHLEN * 2;
		(*kspp)->ks_lock = &pci_ks_template_lock;
		(*kspp)->ks_data = &pci_ks_template;
		(*kspp)->ks_update = pci_ih_ks_update;
		kstat_install(*kspp);
	}
}


/*
 * This function is invoked in two ways:
 * - Thru taskq thread via pci_ih_ks_update to remove kstats for user-bound
 *   interrupts.
 * - From the REMISR introp when an interrupt is being removed.
 */
void
pci_kstat_delete(kstat_t *ksp)
{
	pci_kstat_private_t *kstat_private;
	ddi_intr_handle_impl_t *hdlp;

	if (ksp) {
		kstat_private = ksp->ks_private;
		hdlp = kstat_private->hdlp;
		((ihdl_plat_t *)hdlp->ih_private)->ip_ksp = NULL;

		/*
		 * Delete the kstat before removing the private pointer, to
		 * prevent a kstat update from coming after private is freed.
		 */
		kstat_delete(ksp);

		kmem_free(kstat_private, sizeof (pci_kstat_private_t));
	}
}