octeon_pmc.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/11/sys/mips/cavium/octeon_pmc.c 330897 2018-03-14 03:19:51Z eadler $
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/mips/cavium/octeon_pmc.c 330897 2018-03-14 03:19:51Z eadler $");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37#include <sys/interrupt.h>
38#include <sys/kernel.h>
39#include <sys/module.h>
40#include <sys/rman.h>
41#include <sys/malloc.h>
42#include <sys/smp.h>
43#include <sys/pmc.h>
44#include <sys/pmckern.h>
45
46#include <machine/bus.h>
47#include <machine/intr_machdep.h>
48
49#include <contrib/octeon-sdk/cvmx.h>
50#include <mips/cavium/octeon_irq.h>
51
52struct octeon_pmc_softc {
53	struct rman irq_rman;
54	struct resource *octeon_pmc_irq;
55};
56
57static void		octeon_pmc_identify(driver_t *, device_t);
58static int		octeon_pmc_probe(device_t);
59static int		octeon_pmc_attach(device_t);
60static int		octeon_pmc_intr(void *);
61
62static void
63octeon_pmc_identify(driver_t *drv, device_t parent)
64{
65	if (octeon_has_feature(OCTEON_FEATURE_USB))
66		BUS_ADD_CHILD(parent, 0, "pmc", 0);
67}
68
69static int
70octeon_pmc_probe(device_t dev)
71{
72	if (device_get_unit(dev) != 0)
73		return (ENXIO);
74
75	device_set_desc(dev, "Cavium Octeon Performance Counters");
76	return (BUS_PROBE_NOWILDCARD);
77}
78
79static int
80octeon_pmc_attach(device_t dev)
81{
82	struct octeon_pmc_softc *sc;
83	int error;
84	int rid;
85
86	sc = device_get_softc(dev);
87
88	rid = 0;
89	sc->octeon_pmc_irq = bus_alloc_resource(dev,
90	    SYS_RES_IRQ, &rid, OCTEON_PMC_IRQ,
91	    OCTEON_PMC_IRQ, 1, RF_ACTIVE);
92
93	if (sc->octeon_pmc_irq == NULL) {
94		device_printf(dev, "could not allocate irq%d\n", OCTEON_PMC_IRQ);
95		return (ENXIO);
96	}
97
98	error = bus_setup_intr(dev, sc->octeon_pmc_irq,
99	    INTR_TYPE_MISC, octeon_pmc_intr, NULL, sc, NULL);
100	if (error != 0) {
101		device_printf(dev, "bus_setup_intr failed: %d\n", error);
102		return (error);
103	}
104
105	return (0);
106}
107
108static int
109octeon_pmc_intr(void *arg)
110{
111	struct trapframe *tf = PCPU_GET(curthread)->td_intr_frame;
112
113	if (pmc_intr)
114		(*pmc_intr)(PCPU_GET(cpuid), tf);
115
116	return (FILTER_HANDLED);
117}
118
119static device_method_t octeon_pmc_methods[] = {
120	DEVMETHOD(device_identify,	octeon_pmc_identify),
121	DEVMETHOD(device_probe,		octeon_pmc_probe),
122	DEVMETHOD(device_attach,	octeon_pmc_attach),
123	{ 0, 0 }
124};
125
126static driver_t octeon_pmc_driver = {
127	"pmc",
128	octeon_pmc_methods,
129	sizeof(struct octeon_pmc_softc),
130};
131static devclass_t octeon_pmc_devclass;
132DRIVER_MODULE(octeon_pmc, nexus, octeon_pmc_driver, octeon_pmc_devclass, 0, 0);
133