1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/async.h>
31*0Sstevel@tonic-gate #include <sys/sunddi.h>
32*0Sstevel@tonic-gate #include <sys/sunndi.h>
33*0Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
34*0Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
35*0Sstevel@tonic-gate #include <sys/machsystm.h> /* lddphys() */
36*0Sstevel@tonic-gate #include <sys/kstat.h>
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate /*LINTLIBRARY*/
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate static kstat_t *pci_create_picN_kstat(char *, int, int, int,
41*0Sstevel@tonic-gate pci_kev_mask_t *);
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate void
pci_kstat_create(pci_t * pci_p)44*0Sstevel@tonic-gate pci_kstat_create(pci_t *pci_p)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate pci_common_t *cmn_p = pci_p->pci_common_p;
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gate if (cmn_p->pci_common_attachcnt == 0)
49*0Sstevel@tonic-gate pci_add_upstream_kstat(pci_p);
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate pci_add_pci_kstat(pci_p);
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate void
pci_kstat_destroy(pci_t * pci_p)55*0Sstevel@tonic-gate pci_kstat_destroy(pci_t *pci_p)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate pci_common_t *cmn_p = pci_p->pci_common_p;
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate pci_rem_pci_kstat(pci_p);
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate if (cmn_p->pci_common_attachcnt == 0)
62*0Sstevel@tonic-gate pci_rem_upstream_kstat(pci_p);
63*0Sstevel@tonic-gate }
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate void
pci_create_name_kstat(char * name,pci_ksinfo_t * pp,pci_kev_mask_t * ev)66*0Sstevel@tonic-gate pci_create_name_kstat(char *name, pci_ksinfo_t *pp, pci_kev_mask_t *ev)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate int i;
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate for (i = 0; i < NUM_OF_PICS; i++) {
71*0Sstevel@tonic-gate pp->pic_name_ksp[i] = pci_create_picN_kstat(name,
72*0Sstevel@tonic-gate i, pp->pic_shift[i], pp->pic_no_evs, ev);
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate if (pp->pic_name_ksp[i] == NULL) {
75*0Sstevel@tonic-gate cmn_err(CE_WARN, "pci: unable to create name kstat");
76*0Sstevel@tonic-gate }
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate void
pci_delete_name_kstat(pci_ksinfo_t * pp)81*0Sstevel@tonic-gate pci_delete_name_kstat(pci_ksinfo_t *pp)
82*0Sstevel@tonic-gate {
83*0Sstevel@tonic-gate int i;
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate if (pp != NULL) {
86*0Sstevel@tonic-gate for (i = 0; i < NUM_OF_PICS; i++) {
87*0Sstevel@tonic-gate if (pp->pic_name_ksp[i] != NULL)
88*0Sstevel@tonic-gate kstat_delete(pp->pic_name_ksp[i]);
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate * Create the picN kstat. Returns a pointer to the
95*0Sstevel@tonic-gate * kstat which the driver must store to allow it
96*0Sstevel@tonic-gate * to be deleted when necessary.
97*0Sstevel@tonic-gate */
98*0Sstevel@tonic-gate static kstat_t *
pci_create_picN_kstat(char * mod_name,int pic,int pic_shift,int num_ev,pci_kev_mask_t * ev_array)99*0Sstevel@tonic-gate pci_create_picN_kstat(char *mod_name, int pic, int pic_shift,
100*0Sstevel@tonic-gate int num_ev, pci_kev_mask_t *ev_array)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate struct kstat_named *pic_named_data;
103*0Sstevel@tonic-gate int inst = 0;
104*0Sstevel@tonic-gate int event;
105*0Sstevel@tonic-gate char pic_name[30];
106*0Sstevel@tonic-gate kstat_t *picN_ksp = NULL;
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate (void) sprintf(pic_name, "pic%d", pic);
109*0Sstevel@tonic-gate if ((picN_ksp = kstat_create(mod_name, inst, pic_name,
110*0Sstevel@tonic-gate "bus", KSTAT_TYPE_NAMED, num_ev, NULL)) == NULL) {
111*0Sstevel@tonic-gate cmn_err(CE_WARN, "%s %s : kstat create failed",
112*0Sstevel@tonic-gate mod_name, pic_name);
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate * It is up to the calling function to delete any kstats
116*0Sstevel@tonic-gate * that may have been created already. We just
117*0Sstevel@tonic-gate * return NULL to indicate an error has occured.
118*0Sstevel@tonic-gate */
119*0Sstevel@tonic-gate return (NULL);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate pic_named_data = (struct kstat_named *)
123*0Sstevel@tonic-gate picN_ksp->ks_data;
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate * Write event names and their associated pcr masks. The
127*0Sstevel@tonic-gate * last entry in the array (clear_pic) is added seperately
128*0Sstevel@tonic-gate * below as the pic value must be inverted.
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate for (event = 0; event < num_ev - 1; event++) {
131*0Sstevel@tonic-gate pic_named_data[event].value.ui64 =
132*0Sstevel@tonic-gate (ev_array[event].pcr_mask << pic_shift);
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate kstat_named_init(&pic_named_data[event],
135*0Sstevel@tonic-gate ev_array[event].event_name,
136*0Sstevel@tonic-gate KSTAT_DATA_UINT64);
137*0Sstevel@tonic-gate }
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate /*
140*0Sstevel@tonic-gate * add the clear_pic entry.
141*0Sstevel@tonic-gate */
142*0Sstevel@tonic-gate pic_named_data[event].value.ui64 =
143*0Sstevel@tonic-gate (uint64_t)~(ev_array[event].pcr_mask << pic_shift);
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate kstat_named_init(&pic_named_data[event],
146*0Sstevel@tonic-gate ev_array[event].event_name,
147*0Sstevel@tonic-gate KSTAT_DATA_UINT64);
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate kstat_install(picN_ksp);
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate return (picN_ksp);
152*0Sstevel@tonic-gate }
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate /*
155*0Sstevel@tonic-gate * Create the "counters" kstat.
156*0Sstevel@tonic-gate */
pci_create_cntr_kstat(pci_t * pci_p,char * name,int num_pics,int (* update)(kstat_t *,int),void * cntr_addr_p)157*0Sstevel@tonic-gate kstat_t *pci_create_cntr_kstat(pci_t *pci_p, char *name,
158*0Sstevel@tonic-gate int num_pics, int (*update)(kstat_t *, int),
159*0Sstevel@tonic-gate void *cntr_addr_p)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate struct kstat *counters_ksp;
162*0Sstevel@tonic-gate struct kstat_named *counters_named_data;
163*0Sstevel@tonic-gate dev_info_t *dip = pci_p->pci_dip;
164*0Sstevel@tonic-gate char *drv_name = (char *)ddi_driver_name(dip);
165*0Sstevel@tonic-gate int drv_instance = ddi_get_instance(dip);
166*0Sstevel@tonic-gate char pic_str[10];
167*0Sstevel@tonic-gate int i;
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate /*
170*0Sstevel@tonic-gate * Size of kstat is num_pics + 1 as it
171*0Sstevel@tonic-gate * also contains the %pcr
172*0Sstevel@tonic-gate */
173*0Sstevel@tonic-gate if ((counters_ksp = kstat_create(name, drv_instance,
174*0Sstevel@tonic-gate "counters", "bus", KSTAT_TYPE_NAMED,
175*0Sstevel@tonic-gate num_pics + 1,
176*0Sstevel@tonic-gate KSTAT_FLAG_WRITABLE)) == NULL) {
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d counters kstat_create failed",
179*0Sstevel@tonic-gate drv_name, drv_instance);
180*0Sstevel@tonic-gate return (NULL);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate counters_named_data =
184*0Sstevel@tonic-gate (struct kstat_named *)(counters_ksp->ks_data);
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate /* initialize the named kstats */
187*0Sstevel@tonic-gate kstat_named_init(&counters_named_data[0],
188*0Sstevel@tonic-gate "pcr", KSTAT_DATA_UINT64);
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate for (i = 0; i < num_pics; i++) {
191*0Sstevel@tonic-gate (void) sprintf(pic_str, "pic%d", i);
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate kstat_named_init(&counters_named_data[i+1],
194*0Sstevel@tonic-gate pic_str, KSTAT_DATA_UINT64);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate /*
198*0Sstevel@tonic-gate * Store the register offset's in the kstat's
199*0Sstevel@tonic-gate * private field so that they are available
200*0Sstevel@tonic-gate * to the update function.
201*0Sstevel@tonic-gate */
202*0Sstevel@tonic-gate counters_ksp->ks_private = (void *)cntr_addr_p;
203*0Sstevel@tonic-gate counters_ksp->ks_update = update;
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate kstat_install(counters_ksp);
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate return (counters_ksp);
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate /*
211*0Sstevel@tonic-gate * kstat update function. Handles reads/writes
212*0Sstevel@tonic-gate * from/to kstat.
213*0Sstevel@tonic-gate */
214*0Sstevel@tonic-gate int
pci_cntr_kstat_update(kstat_t * ksp,int rw)215*0Sstevel@tonic-gate pci_cntr_kstat_update(kstat_t *ksp, int rw)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate struct kstat_named *data_p;
218*0Sstevel@tonic-gate pci_cntr_addr_t *cntr_addr_p = ksp->ks_private;
219*0Sstevel@tonic-gate uint64_t pic;
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate data_p = (struct kstat_named *)ksp->ks_data;
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate if (rw == KSTAT_WRITE) {
224*0Sstevel@tonic-gate *cntr_addr_p->pcr_addr = data_p[0].value.ui64;
225*0Sstevel@tonic-gate return (0);
226*0Sstevel@tonic-gate } else {
227*0Sstevel@tonic-gate pic = *cntr_addr_p->pic_addr;
228*0Sstevel@tonic-gate data_p[0].value.ui64 = *cntr_addr_p->pcr_addr;
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate /* pic0 : lo 32 bits */
231*0Sstevel@tonic-gate data_p[1].value.ui64 = (pic <<32) >> 32;
232*0Sstevel@tonic-gate /* pic1 : hi 32 bits */
233*0Sstevel@tonic-gate data_p[2].value.ui64 = pic >> 32;
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate return (0);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate * kstat update function using physical addresses.
240*0Sstevel@tonic-gate */
241*0Sstevel@tonic-gate int
pci_cntr_kstat_pa_update(kstat_t * ksp,int rw)242*0Sstevel@tonic-gate pci_cntr_kstat_pa_update(kstat_t *ksp, int rw)
243*0Sstevel@tonic-gate {
244*0Sstevel@tonic-gate struct kstat_named *data_p;
245*0Sstevel@tonic-gate pci_cntr_pa_t *cntr_pa_p = (pci_cntr_pa_t *)ksp->ks_private;
246*0Sstevel@tonic-gate uint64_t pic;
247*0Sstevel@tonic-gate
248*0Sstevel@tonic-gate data_p = (struct kstat_named *)ksp->ks_data;
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate if (rw == KSTAT_WRITE) {
251*0Sstevel@tonic-gate stdphysio(cntr_pa_p->pcr_pa, data_p[0].value.ui64);
252*0Sstevel@tonic-gate return (0);
253*0Sstevel@tonic-gate } else {
254*0Sstevel@tonic-gate pic = lddphysio(cntr_pa_p->pic_pa);
255*0Sstevel@tonic-gate data_p[0].value.ui64 = lddphysio(cntr_pa_p->pcr_pa);
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate /* pic0 : lo 32 bits */
258*0Sstevel@tonic-gate data_p[1].value.ui64 = (pic << 32) >> 32;
259*0Sstevel@tonic-gate /* pic1 : hi 32 bits */
260*0Sstevel@tonic-gate data_p[2].value.ui64 = pic >> 32;
261*0Sstevel@tonic-gate }
262*0Sstevel@tonic-gate return (0);
263*0Sstevel@tonic-gate }
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate * Matched with pci_add_upstream_kstat()
268*0Sstevel@tonic-gate */
269*0Sstevel@tonic-gate void
pci_rem_upstream_kstat(pci_t * pci_p)270*0Sstevel@tonic-gate pci_rem_upstream_kstat(pci_t *pci_p)
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate pci_common_t *cmn_p = pci_p->pci_common_p;
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate if (cmn_p->pci_common_uksp != NULL)
275*0Sstevel@tonic-gate kstat_delete(cmn_p->pci_common_uksp);
276*0Sstevel@tonic-gate cmn_p->pci_common_uksp = NULL;
277*0Sstevel@tonic-gate }
278