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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/*
* 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/types.h>
#include <sys/kstat.h>
#include "n2piupc_acc.h"
#include "n2piupc_tables.h"
#include "n2piupc.h"
#include "n2piupc_biterr.h"
#define PIC_STR_LEN 5 /* Size of a PICx name string. */
static int n2piupc_create_name_kstat(n2piu_grp_t *grp);
static void n2piupc_delete_name_kstats(kstat_t **name_kstats_pp,
int num_kstats);
static kstat_t *n2piupc_create_cntr_kstat(char *name, int dev_inst,
int (*update)(kstat_t *, int), n2piu_ksinfo_t *ksinfop, int num_pics);
static int n2piupc_kstat_update(kstat_t *ksp, int rw);
static kstat_t *n2piupc_create_picN_kstat(char *mod_name, int pic,
uint64_t mask, int num_ev, n2piu_event_t *ev_array);
static int n2piupc_write(n2piupc_t *n2piupc_p, int regid, uint64_t data);
/*
* One-time initialization for this module.
*/
int
n2piupc_kstat_init()
{
n2piu_grp_t **grp_pp;
n2piu_grp_t *grp_p;
N2PIUPC_DBG2("n2piupc: kstat_init: enter\n");
/*
* Initialize the name kstats for each group, drawing upon the table
* for values.
*/
for (grp_pp = leaf_grps; *grp_pp != NULL; grp_pp++) {
grp_p = *grp_pp;
N2PIUPC_DBG2("Setting up group for %s\n", grp_p->grp_name);
/* Create basic pic event-type pair. */
grp_p->name_kstats_pp = kmem_zalloc((grp_p->num_counters *
sizeof (kstat_t)), KM_SLEEP);
if (n2piupc_create_name_kstat(grp_p) != DDI_SUCCESS) {
n2piupc_kstat_fini();
N2PIUPC_DBG1("n2piupc: init: failure exit\n");
return (DDI_FAILURE);
}
}
N2PIUPC_DBG2("n2piupc: kstat_init: success exit\n");
return (DDI_SUCCESS);
}
/*
* Per-instance initialization for this module.
*/
int
n2piupc_kstat_attach(n2piupc_t *n2piupc_p)
{
n2piu_grp_t **grp_pp;
n2piu_grp_t *grp_p;
n2piu_ksinfo_t *ksinfo_p;
int i;
N2PIUPC_DBG2("n2piupc: kstat_attach %d: enter\n",
ddi_get_instance(n2piupc_p->n2piupc_dip));
/* Initialize biterr module. Save opaque result. */
if (n2piupc_biterr_attach(&n2piupc_p->n2piupc_biterr_p) != DDI_SUCCESS)
goto err;
/* Set up kstats for each group. */
for (i = 0, grp_pp = leaf_grps; *grp_pp != NULL; i++, grp_pp++) {
grp_p = *grp_pp;
/*
* ksinfo_p keeps all info needed by n2piupc_kstat_update,
* which is fired off asynchronously on demand by the kstat
* framework.
*/
ksinfo_p = (n2piu_ksinfo_t *)kmem_zalloc(
sizeof (n2piu_ksinfo_t), KM_SLEEP);
ksinfo_p->n2piupc_p = n2piupc_p;
ksinfo_p->grp_p = grp_p;
/* Also save in state structure, for later cleanup. */
n2piupc_p->n2piupc_ksinfo_p[i] = ksinfo_p;
/* Create counter kstats */
ksinfo_p->cntr_ksp = n2piupc_create_cntr_kstat(grp_p->grp_name,
ddi_get_instance(n2piupc_p->n2piupc_dip),
n2piupc_kstat_update, ksinfo_p, grp_p->num_counters);
if (ksinfo_p->cntr_ksp == NULL)
goto err;
}
/*
* Special treatment for bit err registers: enable them so they start
* counting now.
*/
if (n2piupc_write(n2piupc_p, leaf_grps[BIT_ERR_GRP]->regsel_p->regoff,
BTERR_CTR_ENABLE) != SUCCESS) {
goto err;
}
N2PIUPC_DBG2("n2piupc: kstat_attach: success exit\n");
return (DDI_SUCCESS);
err:
n2piupc_kstat_detach(n2piupc_p);
N2PIUPC_DBG2("n2piupc: kstat_attach: failure exit\n");
return (DDI_FAILURE);
}
/*
* Create the name kstats for each group.
*/
static int
n2piupc_create_name_kstat(n2piu_grp_t *grp_p)
{
int i;
for (i = 0; i < grp_p->num_counters; i++) {
grp_p->name_kstats_pp[i] = n2piupc_create_picN_kstat(
grp_p->grp_name, i,
grp_p->regsel_p->fields_p[i].event_offset,
grp_p->regsel_p->fields_p[i].num_events,
grp_p->regsel_p->fields_p[i].events_p);
if (grp_p->name_kstats_pp[i] == NULL)
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* Create the picN kstat. Returns a pointer to the
* kstat which the driver must store to allow it
* to be deleted when necessary.
*/
static kstat_t *
n2piupc_create_picN_kstat(char *mod_name, int pic, uint64_t ev_offset,
int num_ev, n2piu_event_t *ev_array)
{
int event;
char pic_name[PIC_STR_LEN];
kstat_t *picN_ksp = NULL;
struct kstat_named *pic_named_data;
(void) snprintf(pic_name, PIC_STR_LEN, "pic%1d", pic);
if ((picN_ksp = kstat_create(mod_name, 0, pic_name,
"bus", KSTAT_TYPE_NAMED, num_ev, NULL)) == NULL) {
cmn_err(CE_WARN, "%s %s : kstat create failed",
mod_name, pic_name);
return (NULL);
}
/* NOTE: Number of events is assumed to always be non-zero. */
pic_named_data = (struct kstat_named *)picN_ksp->ks_data;
/*
* Fill up data section of the kstat
* Write event names and their associated pcr masks.
* num_ev - 1 is because CLEAR_PIC is added separately.
*/
for (event = 0; event < num_ev - 1; event++) {
pic_named_data[event].value.ui64 =
ev_array[event].value << ev_offset;
kstat_named_init(&pic_named_data[event],
ev_array[event].name, KSTAT_DATA_UINT64);
}
/*
* add the clear_pic entry
*/
pic_named_data[event].value.ui64 =
(uint64_t)~(ev_array[event].value << ev_offset);
kstat_named_init(&pic_named_data[event], ev_array[event].name,
KSTAT_DATA_UINT64);
kstat_install(picN_ksp);
return (picN_ksp);
}
/*
* Create the "counters" kstat.
*/
static kstat_t *
n2piupc_create_cntr_kstat(char *name, int dev_inst,
int (*update)(kstat_t *, int), n2piu_ksinfo_t *ksinfop, int num_pics)
{
int i;
char pic_str[PIC_STR_LEN];
struct kstat *counters_ksp;
struct kstat_named *counters_named_data;
N2PIUPC_DBG2("n2piupc_create_cntr_kstat: name: %s instance: %d\n",
name, dev_inst);
/*
* Size of kstat is num_pics + 1. extra one for pcr.
*/
if ((counters_ksp = kstat_create(name, dev_inst, "counters", "bus",
KSTAT_TYPE_NAMED, num_pics + 1, KSTAT_FLAG_WRITABLE)) == NULL) {
cmn_err(CE_WARN, "%s%d: kstat_create for %s counters failed",
NAMEINST(ksinfop->n2piupc_p->n2piupc_dip), name);
return (NULL);
}
counters_named_data = (struct kstat_named *)(counters_ksp->ks_data);
kstat_named_init(&counters_named_data[0], "pcr", KSTAT_DATA_UINT64);
for (i = 0; i < num_pics; i++) {
(void) snprintf(pic_str, PIC_STR_LEN, "pic%1d", i);
kstat_named_init(&counters_named_data[i+1], pic_str,
KSTAT_DATA_UINT64);
}
/*
* Store the reg type and other info. in the kstat's private field
* so that they are available to the update function.
*/
counters_ksp->ks_private = (void *)ksinfop;
counters_ksp->ks_update = update;
kstat_install(counters_ksp);
return (counters_ksp);
}
/* Higher-level register write, hides SW abstractions. */
static int
n2piupc_write(n2piupc_t *n2piupc_p, int regid, uint64_t data)
{
int rval = SUCCESS;
switch (regid) {
case SW_N2PIU_BITERR_SEL:
case SW_N2PIU_BITERR_CLR:
rval = n2piupc_biterr_write(n2piupc_p, regid, data);
break;
default:
if (n2piupc_set_perfreg(n2piupc_p->n2piupc_handle,
regid, data) != H_EOK)
rval = EIO;
break;
}
N2PIUPC_DBG1("n2piupc_write: status:%d\n", rval);
return (rval);
}
/* Higher-level register read, hides SW abstractions. */
static int
n2piupc_read(n2piupc_t *n2piupc_p, int regid, uint64_t *data)
{
int rval = SUCCESS;
N2PIUPC_DBG2("n2piupc_read enter: regid:%d\n", regid);
/* This "register" is a layered SW-implemented reg. */
switch (regid) {
case SW_N2PIU_BITERR_CNT1_DATA:
case SW_N2PIU_BITERR_CNT2_DATA:
case SW_N2PIU_BITERR_SEL:
rval = n2piupc_biterr_read(n2piupc_p, regid, data);
break;
default:
if (n2piupc_get_perfreg(n2piupc_p->n2piupc_handle,
regid, data) != H_EOK)
rval = EIO;
break;
}
N2PIUPC_DBG1("n2piupc_read exit: data:0x%lx, status:%d\n", *data,
rval);
return (rval);
}
/*
* Program a performance counter.
*
* reggroup is which type of counter.
* counter is the counter number.
* event is the event to program for that counter.
*/
static int
n2piupc_perfcnt_program(n2piupc_t *n2piupc_p, n2piu_grp_t *grp_p,
uint64_t new_events)
{
uint64_t old_events;
int rval = SUCCESS;
uint64_t event_mask;
int counter;
N2PIUPC_DBG1(
"n2piupc_perfcnt_program enter: new_events:0x%" PRIx64 "\n",
new_events);
if ((rval = n2piupc_read(n2piupc_p, grp_p->regsel_p->regoff,
&old_events)) != SUCCESS) {
N2PIUPC_DBG1(
"Read of old event data failed, select reg offset:%ld\n",
grp_p->regsel_p->regoff);
goto done_pgm;
}
N2PIUPC_DBG1(" old_events:0x%" PRIx64 "\n", old_events);
for (counter = 0; counter < grp_p->num_counters; counter++) {
if (grp_p->counters_p[counter].zero_regoff == NO_REGISTER)
continue;
event_mask = grp_p->regsel_p->fields_p[counter].event_mask <<
grp_p->regsel_p->fields_p[counter].event_offset;
N2PIUPC_DBG1(
"grp:%s, counter:%d, zero_regoff:0x%lx, "
"event_mask:0x%" PRIx64 ", old&mask:0x%lx, "
"new&mask:0x%lx\n",
grp_p->grp_name, counter,
grp_p->counters_p[counter].zero_regoff,
event_mask, old_events & event_mask,
new_events & event_mask);
if ((old_events & event_mask) ==
(new_events & event_mask))
continue;
N2PIUPC_DBG1("Zeroing counter %d\n", counter);
if ((rval = n2piupc_write(n2piupc_p,
grp_p->counters_p[counter].zero_regoff,
grp_p->counters_p[counter].zero_value)) != SUCCESS)
goto done_pgm;
}
if (old_events != new_events) {
N2PIUPC_DBG1("old != new, setting event reg %ld to 0x%lx\n",
grp_p->regsel_p->regoff, new_events);
if ((rval = n2piupc_write(n2piupc_p, grp_p->regsel_p->regoff,
new_events)) != SUCCESS) {
N2PIUPC_DBG1(
"Write of new event data failed, "
"select reg offset: %ld\n",
grp_p->regsel_p->regoff);
goto done_pgm;
}
}
done_pgm:
N2PIUPC_DBG1("n2piupc_perfcnt_program: returning status %d.\n", rval);
return (rval);
}
/*
* kstat update function. Handles reads/writes
* from/to kstat.
*/
static int
n2piupc_kstat_update(kstat_t *ksp, int rw)
{
struct kstat_named *data_p;
int counter;
n2piu_ksinfo_t *ksinfop = ksp->ks_private;
n2piu_grp_t *grp_p = ksinfop->grp_p;
n2piupc_t *n2piupc_p = ksinfop->n2piupc_p;
data_p = (struct kstat_named *)ksp->ks_data;
if (rw == KSTAT_WRITE) {
N2PIUPC_DBG2("n2piupc_kstat_update: wr %ld\n",
data_p[0].value.ui64);
/*
* Fields without programmable events won't be zeroed as
* n2piupc_perfcnt_program is what zeros them.
*/
/* This group has programmable events. */
if (grp_p->regsel_p->regoff != NO_REGISTER) {
N2PIUPC_DBG2("write: regoff has valid register\n");
if (n2piupc_perfcnt_program(n2piupc_p, grp_p,
data_p[0].value.ui64) != SUCCESS)
return (EIO);
}
} else { /* Read the event register and all of the counters. */
/* This group has programmable events. */
if (grp_p->regsel_p->regoff != NO_REGISTER) {
N2PIUPC_DBG2("read: regoff has valid register\n");
if (n2piupc_read(n2piupc_p, grp_p->regsel_p->regoff,
&data_p[0].value.ui64) != SUCCESS)
return (EIO);
} else
data_p[0].value.ui64 = 0ull;
N2PIUPC_DBG2("n2piupc_kstat_update: rd event %ld",
data_p[0].value.ui64);
for (counter = 0; counter < grp_p->num_counters; counter++) {
if (n2piupc_read(n2piupc_p,
grp_p->counters_p[counter].regoff,
&data_p[counter + 1].value.ui64) != SUCCESS)
return (EIO);
N2PIUPC_DBG2("cntr%d, off:0x%lx, val:0x%ld", counter,
grp_p->counters_p[counter].regoff,
data_p[counter + 1].value.ui64);
}
}
return (SUCCESS);
}
void
n2piupc_kstat_fini()
{
n2piu_grp_t **grp_pp;
n2piu_grp_t *grp_p;
int j;
N2PIUPC_DBG2("n2piupc_kstat_fini called\n");
for (j = 0, grp_pp = leaf_grps; *grp_pp != NULL; j++, grp_pp++) {
grp_p = *grp_pp;
if (grp_p->name_kstats_pp != NULL) {
n2piupc_delete_name_kstats(grp_p->name_kstats_pp,
grp_p->num_counters);
kmem_free(grp_p->name_kstats_pp,
grp_p->num_counters * sizeof (kstat_t));
grp_p->name_kstats_pp = NULL;
}
}
}
static void
n2piupc_delete_name_kstats(kstat_t **name_kstats_pp, int num_kstats)
{
int i;
if (name_kstats_pp != NULL) {
for (i = 0; i < num_kstats; i++) {
if (name_kstats_pp[i] != NULL)
kstat_delete(name_kstats_pp[i]);
}
}
}
void
n2piupc_kstat_detach(n2piupc_t *n2piupc_p)
{
n2piu_grp_t **grp_pp;
int i;
N2PIUPC_DBG2("n2piupc_kstat_detach called\n");
for (i = 0, grp_pp = leaf_grps; *grp_pp != NULL; i++, grp_pp++) {
if (n2piupc_p->n2piupc_ksinfo_p[i] != NULL) {
if (n2piupc_p->n2piupc_ksinfo_p[i]->cntr_ksp != NULL)
kstat_delete(
n2piupc_p->n2piupc_ksinfo_p[i]->cntr_ksp);
kmem_free(n2piupc_p->n2piupc_ksinfo_p[i],
sizeof (n2piu_ksinfo_t));
}
}
n2piupc_biterr_detach(n2piupc_p->n2piupc_biterr_p);
}
|