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