17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 2000 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate * All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/async.h>
297c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
317c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
327c478bd9Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
337c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> /* lddphys() */
347c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate static kstat_t *pci_create_picN_kstat(char *, int, int, int,
397c478bd9Sstevel@tonic-gate pci_kev_mask_t *);
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate void
pci_kstat_create(pci_t * pci_p)427c478bd9Sstevel@tonic-gate pci_kstat_create(pci_t *pci_p)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate pci_common_t *cmn_p = pci_p->pci_common_p;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate if (cmn_p->pci_common_attachcnt == 0)
477c478bd9Sstevel@tonic-gate pci_add_upstream_kstat(pci_p);
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate pci_add_pci_kstat(pci_p);
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate void
pci_kstat_destroy(pci_t * pci_p)537c478bd9Sstevel@tonic-gate pci_kstat_destroy(pci_t *pci_p)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate pci_common_t *cmn_p = pci_p->pci_common_p;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate pci_rem_pci_kstat(pci_p);
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate if (cmn_p->pci_common_attachcnt == 0)
607c478bd9Sstevel@tonic-gate pci_rem_upstream_kstat(pci_p);
617c478bd9Sstevel@tonic-gate }
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate void
pci_create_name_kstat(char * name,pci_ksinfo_t * pp,pci_kev_mask_t * ev)647c478bd9Sstevel@tonic-gate pci_create_name_kstat(char *name, pci_ksinfo_t *pp, pci_kev_mask_t *ev)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate int i;
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate for (i = 0; i < NUM_OF_PICS; i++) {
697c478bd9Sstevel@tonic-gate pp->pic_name_ksp[i] = pci_create_picN_kstat(name,
707c478bd9Sstevel@tonic-gate i, pp->pic_shift[i], pp->pic_no_evs, ev);
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate if (pp->pic_name_ksp[i] == NULL) {
737c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "pci: unable to create name kstat");
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate void
pci_delete_name_kstat(pci_ksinfo_t * pp)797c478bd9Sstevel@tonic-gate pci_delete_name_kstat(pci_ksinfo_t *pp)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate int i;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if (pp != NULL) {
847c478bd9Sstevel@tonic-gate for (i = 0; i < NUM_OF_PICS; i++) {
857c478bd9Sstevel@tonic-gate if (pp->pic_name_ksp[i] != NULL)
867c478bd9Sstevel@tonic-gate kstat_delete(pp->pic_name_ksp[i]);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate * Create the picN kstat. Returns a pointer to the
937c478bd9Sstevel@tonic-gate * kstat which the driver must store to allow it
947c478bd9Sstevel@tonic-gate * to be deleted when necessary.
957c478bd9Sstevel@tonic-gate */
967c478bd9Sstevel@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)977c478bd9Sstevel@tonic-gate pci_create_picN_kstat(char *mod_name, int pic, int pic_shift,
987c478bd9Sstevel@tonic-gate int num_ev, pci_kev_mask_t *ev_array)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate struct kstat_named *pic_named_data;
1017c478bd9Sstevel@tonic-gate int inst = 0;
1027c478bd9Sstevel@tonic-gate int event;
1037c478bd9Sstevel@tonic-gate char pic_name[30];
1047c478bd9Sstevel@tonic-gate kstat_t *picN_ksp = NULL;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate (void) sprintf(pic_name, "pic%d", pic);
1077c478bd9Sstevel@tonic-gate if ((picN_ksp = kstat_create(mod_name, inst, pic_name,
108*ae121a14SToomas Soome "bus", KSTAT_TYPE_NAMED, num_ev, 0)) == NULL) {
1097c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s %s : kstat create failed",
1107c478bd9Sstevel@tonic-gate mod_name, pic_name);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate * It is up to the calling function to delete any kstats
1147c478bd9Sstevel@tonic-gate * that may have been created already. We just
1157c478bd9Sstevel@tonic-gate * return NULL to indicate an error has occured.
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate return (NULL);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
120*ae121a14SToomas Soome pic_named_data = (struct kstat_named *)picN_ksp->ks_data;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Write event names and their associated pcr masks. The
1247c478bd9Sstevel@tonic-gate * last entry in the array (clear_pic) is added seperately
1257c478bd9Sstevel@tonic-gate * below as the pic value must be inverted.
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate for (event = 0; event < num_ev - 1; event++) {
1287c478bd9Sstevel@tonic-gate pic_named_data[event].value.ui64 =
1297c478bd9Sstevel@tonic-gate (ev_array[event].pcr_mask << pic_shift);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate kstat_named_init(&pic_named_data[event],
132*ae121a14SToomas Soome ev_array[event].event_name, KSTAT_DATA_UINT64);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate * add the clear_pic entry.
1377c478bd9Sstevel@tonic-gate */
1387c478bd9Sstevel@tonic-gate pic_named_data[event].value.ui64 =
1397c478bd9Sstevel@tonic-gate (uint64_t)~(ev_array[event].pcr_mask << pic_shift);
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate kstat_named_init(&pic_named_data[event],
142*ae121a14SToomas Soome ev_array[event].event_name, KSTAT_DATA_UINT64);
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate kstat_install(picN_ksp);
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate return (picN_ksp);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * Create the "counters" kstat.
1517c478bd9Sstevel@tonic-gate */
pci_create_cntr_kstat(pci_t * pci_p,char * name,int num_pics,int (* update)(kstat_t *,int),void * cntr_addr_p)1527c478bd9Sstevel@tonic-gate kstat_t *pci_create_cntr_kstat(pci_t *pci_p, char *name,
1537c478bd9Sstevel@tonic-gate int num_pics, int (*update)(kstat_t *, int),
1547c478bd9Sstevel@tonic-gate void *cntr_addr_p)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate struct kstat *counters_ksp;
1577c478bd9Sstevel@tonic-gate struct kstat_named *counters_named_data;
1587c478bd9Sstevel@tonic-gate dev_info_t *dip = pci_p->pci_dip;
1597c478bd9Sstevel@tonic-gate char *drv_name = (char *)ddi_driver_name(dip);
1607c478bd9Sstevel@tonic-gate int drv_instance = ddi_get_instance(dip);
1617c478bd9Sstevel@tonic-gate char pic_str[10];
1627c478bd9Sstevel@tonic-gate int i;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * Size of kstat is num_pics + 1 as it
1667c478bd9Sstevel@tonic-gate * also contains the %pcr
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate if ((counters_ksp = kstat_create(name, drv_instance,
169*ae121a14SToomas Soome "counters", "bus", KSTAT_TYPE_NAMED, num_pics + 1,
1707c478bd9Sstevel@tonic-gate KSTAT_FLAG_WRITABLE)) == NULL) {
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d counters kstat_create failed",
1737c478bd9Sstevel@tonic-gate drv_name, drv_instance);
1747c478bd9Sstevel@tonic-gate return (NULL);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate counters_named_data =
1787c478bd9Sstevel@tonic-gate (struct kstat_named *)(counters_ksp->ks_data);
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate /* initialize the named kstats */
1817c478bd9Sstevel@tonic-gate kstat_named_init(&counters_named_data[0],
1827c478bd9Sstevel@tonic-gate "pcr", KSTAT_DATA_UINT64);
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate for (i = 0; i < num_pics; i++) {
1857c478bd9Sstevel@tonic-gate (void) sprintf(pic_str, "pic%d", i);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate kstat_named_init(&counters_named_data[i+1],
1887c478bd9Sstevel@tonic-gate pic_str, KSTAT_DATA_UINT64);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Store the register offset's in the kstat's
1937c478bd9Sstevel@tonic-gate * private field so that they are available
1947c478bd9Sstevel@tonic-gate * to the update function.
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate counters_ksp->ks_private = (void *)cntr_addr_p;
1977c478bd9Sstevel@tonic-gate counters_ksp->ks_update = update;
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate kstat_install(counters_ksp);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate return (counters_ksp);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate * kstat update function. Handles reads/writes
2067c478bd9Sstevel@tonic-gate * from/to kstat.
2077c478bd9Sstevel@tonic-gate */
2087c478bd9Sstevel@tonic-gate int
pci_cntr_kstat_update(kstat_t * ksp,int rw)2097c478bd9Sstevel@tonic-gate pci_cntr_kstat_update(kstat_t *ksp, int rw)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate struct kstat_named *data_p;
2127c478bd9Sstevel@tonic-gate pci_cntr_addr_t *cntr_addr_p = ksp->ks_private;
2137c478bd9Sstevel@tonic-gate uint64_t pic;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate data_p = (struct kstat_named *)ksp->ks_data;
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) {
2187c478bd9Sstevel@tonic-gate *cntr_addr_p->pcr_addr = data_p[0].value.ui64;
2197c478bd9Sstevel@tonic-gate return (0);
2207c478bd9Sstevel@tonic-gate } else {
2217c478bd9Sstevel@tonic-gate pic = *cntr_addr_p->pic_addr;
2227c478bd9Sstevel@tonic-gate data_p[0].value.ui64 = *cntr_addr_p->pcr_addr;
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /* pic0 : lo 32 bits */
2257c478bd9Sstevel@tonic-gate data_p[1].value.ui64 = (pic <<32) >> 32;
2267c478bd9Sstevel@tonic-gate /* pic1 : hi 32 bits */
2277c478bd9Sstevel@tonic-gate data_p[2].value.ui64 = pic >> 32;
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate return (0);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * kstat update function using physical addresses.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate int
pci_cntr_kstat_pa_update(kstat_t * ksp,int rw)2367c478bd9Sstevel@tonic-gate pci_cntr_kstat_pa_update(kstat_t *ksp, int rw)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate struct kstat_named *data_p;
2397c478bd9Sstevel@tonic-gate pci_cntr_pa_t *cntr_pa_p = (pci_cntr_pa_t *)ksp->ks_private;
2407c478bd9Sstevel@tonic-gate uint64_t pic;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate data_p = (struct kstat_named *)ksp->ks_data;
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) {
2457c478bd9Sstevel@tonic-gate stdphysio(cntr_pa_p->pcr_pa, data_p[0].value.ui64);
2467c478bd9Sstevel@tonic-gate return (0);
2477c478bd9Sstevel@tonic-gate } else {
2487c478bd9Sstevel@tonic-gate pic = lddphysio(cntr_pa_p->pic_pa);
2497c478bd9Sstevel@tonic-gate data_p[0].value.ui64 = lddphysio(cntr_pa_p->pcr_pa);
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /* pic0 : lo 32 bits */
2527c478bd9Sstevel@tonic-gate data_p[1].value.ui64 = (pic << 32) >> 32;
2537c478bd9Sstevel@tonic-gate /* pic1 : hi 32 bits */
2547c478bd9Sstevel@tonic-gate data_p[2].value.ui64 = pic >> 32;
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate * Matched with pci_add_upstream_kstat()
2627c478bd9Sstevel@tonic-gate */
2637c478bd9Sstevel@tonic-gate void
pci_rem_upstream_kstat(pci_t * pci_p)2647c478bd9Sstevel@tonic-gate pci_rem_upstream_kstat(pci_t *pci_p)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate pci_common_t *cmn_p = pci_p->pci_common_p;
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate if (cmn_p->pci_common_uksp != NULL)
2697c478bd9Sstevel@tonic-gate kstat_delete(cmn_p->pci_common_uksp);
2707c478bd9Sstevel@tonic-gate cmn_p->pci_common_uksp = NULL;
2717c478bd9Sstevel@tonic-gate }
272