summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4/io/fpc/fpc-impl.c
blob: 64896cf87ead7a47fc8ae2188dfa91a2b89b8b8e (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/sunddi.h>
#include <sys/disp.h>
#include <fpc.h>
#include <fpc-impl.h>

#define	BOUNDS_CHECK_FAILS(arg, max)	((arg < 0) && (arg >= max))

static int this_node = 0;
node_data_t node_data[NUM_LEAVES];

int fpc_debug = 0;

static int counters_per_type[MAX_REG_TYPES] = {
	NUM_JBC_COUNTERS,
	NUM_IMU_COUNTERS,
	NUM_MMU_COUNTERS,
	NUM_TLU_COUNTERS,
	NUM_LPU_COUNTERS
};

static int first_reg_of_type[MAX_REG_TYPES];

static uint64_t event_field_mask[NUM_TOTAL_COUNTERS] = {
	JBC_PIC0_EVT_MASK,		/* JBC counter 0 */
	JBC_PIC1_EVT_MASK,		/* JBC counter 1 */
	IMU_PIC0_EVT_MASK,		/* IMU counter 0 */
	IMU_PIC1_EVT_MASK,		/* IMU counter 1 */
	MMU_PIC0_EVT_MASK,		/* MMU counter 0 */
	MMU_PIC1_EVT_MASK,		/* MMU counter 1 */
	TLU_PIC0_EVT_MASK,		/* TLU counter 0 */
	TLU_PIC1_EVT_MASK,		/* TLU counter 1 */
	TLU_PIC2_EVT_MASK,		/* TLU counter 2 */
	LPU_PIC0_EVT_MASK,		/* LPU counter 1 */
	LPU_PIC1_EVT_MASK		/* LPU counter 2 */
};

/* Offsets of the fields shown in event_field_masks. */
static int event_field_offset[NUM_TOTAL_COUNTERS] = {
	PIC0_EVT_SEL_SHIFT,		/* JBC counter 0 */
	PIC1_EVT_SEL_SHIFT,		/* JBC counter 1 */
	PIC0_EVT_SEL_SHIFT,		/* IMU counter 0 */
	PIC1_EVT_SEL_SHIFT,		/* IMU counter 1 */
	PIC0_EVT_SEL_SHIFT,		/* MMU counter 0 */
	PIC1_EVT_SEL_SHIFT,		/* MMU counter 1 */
	PIC0_EVT_SEL_SHIFT,		/* TLU counter 0 */
	PIC1_EVT_SEL_SHIFT,		/* TLU counter 1 */
	PIC2_EVT_SEL_SHIFT,		/* TLU counter 2 */
	PIC0_EVT_SEL_SHIFT,		/* LPU counter 1 */
	PIC2_EVT_SEL_SHIFT		/* LPU counter 2 */
};

/* For determining platform suitability at _init time. */
int
fpc_init_platform_check()
{
	return (fpc_platform_check());
}

/*ARGSUSED*/
void
fpc_common_node_setup(dev_info_t *dip, int *index_p)
{
	char pathname[MAXPATHLEN];

	(void) ddi_pathname(dip, pathname);
	node_data[this_node].name =
	    kmem_zalloc(strlen(pathname)+1, KM_SLEEP);
	(void) strcpy(node_data[this_node].name, pathname);
	mutex_init(&node_data[this_node].mutex, NULL, MUTEX_DRIVER, NULL);
	*index_p = this_node++;
}

int
fpc_perfcnt_module_init(dev_info_t *fpc_dip, int *avail)
{
	int i;
	dev_info_t *dip;

	*avail = 0;

	for (i = 1; i < MAX_REG_TYPES; i++) {
		first_reg_of_type[i] =
		    first_reg_of_type[i-1] + counters_per_type[i-1];
	}

	/*
	 * Look thru first level of device tree only.
	 * Assume there can be no more than NUM_LEAVES nodes in the system.
	 */
	dip = ddi_root_node();
	for (dip = ddi_get_child(dip);
	    ((dip != NULL) && (this_node < NUM_LEAVES));
	    dip = ddi_get_next_sibling(dip)) {
		if (fpc_platform_node_init(dip, avail) != SUCCESS)
			return (DDI_FAILURE);
	}

	return ((*avail) ? fpc_platform_module_init(fpc_dip) : DDI_FAILURE);
}

int
fpc_perfcnt_module_fini(dev_info_t *dip)
{
	int i;

	for (i = 0; i < NUM_LEAVES; i++) {
		fpc_platform_node_fini(node_data[i].plat_data_p);
		if (node_data[i].name != NULL) {
			kmem_free(node_data[i].name,
			    strlen(node_data[i].name) + 1);
			mutex_destroy(&node_data[i].mutex);
		}
	}

	fpc_platform_module_fini(dip);
	return (DDI_SUCCESS);
}

char
*fpc_get_dev_name_by_number(int index)
{
	return (node_data[index].name);
}

void *
fpc_get_platform_data_by_number(int index)
{
	return (node_data[index].plat_data_p);
}


int
fpc_set_platform_data_by_number(int index, void *data_p)
{
	node_data[index].plat_data_p = data_p;
	return (SUCCESS);
}


static int
fpc_get_mutex_by_number(int index, kmutex_t **mutex_pp)
{
	*mutex_pp = &node_data[index].mutex;
	return (SUCCESS);
}


static int
fpc_get_counter_reg_index(fire_perfcnt_t regtype, int counter)
{
	FPC_DBG1(
	    "fpc_get_counter_reg_index: regtype:%d, counter:%d, bounds:%d\n",
	    regtype, counter, counters_per_type[regtype]);
	if (BOUNDS_CHECK_FAILS(counter, counters_per_type[regtype]))
		return (-1);
	FPC_DBG1("returning: %d\n", first_reg_of_type[regtype] + counter);
	return (first_reg_of_type[regtype] + counter);
}


/*
 * Program a performance counter.
 *
 * reggroup is which type of counter.
 * counter is the counter number.
 * event is the event to program for that counter.
 */
int
fpc_perfcnt_program(int devnum, fire_perfcnt_t reggroup,
    uint64_t new_events)
{
	int counter_index;
	fire_perfreg_handle_t firehdl;
	kmutex_t *mutex_p;
	uint64_t old_events;
	int rval = SUCCESS;
	uint64_t zero = 0ull;
	int num_counters, counter;

	FPC_DBG1("fpc_perfcnt_program enter:\n");
	FPC_DBG1("  devnum:%d, reggroup:%d, new_events:0x%" PRIx64 "\n",
	    devnum, reggroup, new_events);

	if ((firehdl = fpc_get_perfreg_handle(devnum)) ==
	    (fire_perfreg_handle_t)-1)
		return (EIO);

	num_counters = counters_per_type[reggroup];

	if (fpc_get_mutex_by_number(devnum, &mutex_p) != DDI_SUCCESS) {
		(void) fpc_free_counter_handle(firehdl);
		return (EIO);
	}

	mutex_enter(mutex_p);

	if ((rval = fpc_event_io(firehdl, reggroup, &old_events, IS_READ)) !=
	    SUCCESS) {
		FPC_DBG1("Read of old event data failed, group:%d\n", reggroup);
		goto done_pgm;
	}

	for (counter = 0; counter < num_counters; counter++) {

		counter_index = fpc_get_counter_reg_index(reggroup, counter);

		if ((old_events & event_field_mask[counter_index]) ==
		    (new_events & event_field_mask[counter_index]))
			continue;

		FPC_DBG1("Zeroing counter %d\n", counter_index);
		if ((rval = fpc_counter_io(firehdl, reggroup, counter_index,
		    &zero, IS_WRITE)) != SUCCESS)
			goto done_pgm;
	}

	if (old_events != new_events) {
		if ((rval =
		    fpc_event_io(firehdl, reggroup, &new_events, IS_WRITE)) !=
		    SUCCESS) {
			FPC_DBG1("Write of new event data failed, group:%d\n",
			    reggroup);
			goto done_pgm;
		}
	}
done_pgm:
	mutex_exit(mutex_p);
	(void) fpc_free_counter_handle(firehdl);
	return (rval);
}


/*
 * Read a performance counter.
 *
 * reggroup is which type of counter.
 * event_p returns the event programmed for that counter.
 * values returns the counter values.
 */
int
fpc_perfcnt_read(int devnum, fire_perfcnt_t reggroup,
    uint64_t *event_p, uint64_t values[NUM_MAX_COUNTERS])
{
	fire_perfreg_handle_t firehdl;
	int counter_index;
	kmutex_t *mutex_p;
	int rval;
	int num_counters, counter;

	FPC_DBG1("fpc_perfcnt_read: devnum:%d\n", devnum);
	num_counters = counters_per_type[reggroup];

	if ((firehdl = fpc_get_perfreg_handle(devnum)) ==
	    (fire_perfreg_handle_t)-1)
		return (EIO);

	if (fpc_get_mutex_by_number(devnum, &mutex_p) != DDI_SUCCESS)
		return (EIO);

	mutex_enter(mutex_p);

	if ((rval = fpc_event_io(firehdl, reggroup, event_p, IS_READ)) !=
	    SUCCESS)
		goto done_read;

	for (counter = 0; counter < num_counters; counter++) {
		counter_index = fpc_get_counter_reg_index(reggroup, counter);

		if ((rval = fpc_counter_io(firehdl, reggroup, counter_index,
		    &values[counter], IS_READ)) != SUCCESS)
			goto done_read;

		FPC_DBG1("Read_counter %d / %d, status:%d, value returned:0x%"
		    PRIx64 "\n", reggroup, counter, rval, values[counter]);
	}

done_read:
	mutex_exit(mutex_p);
	(void) fpc_free_counter_handle(firehdl);
	return (rval);
}