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$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
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
60static void
61octeon_pmc_identify(driver_t *drv, device_t parent)
62{
63	if (octeon_has_feature(OCTEON_FEATURE_USB))
64		BUS_ADD_CHILD(parent, 0, "pmc", 0);
65}
66
67static int
68octeon_pmc_probe(device_t dev)
69{
70	if (device_get_unit(dev) != 0)
71		return (ENXIO);
72
73	device_set_desc(dev, "Cavium Octeon Performance Counters");
74	return (BUS_PROBE_NOWILDCARD);
75}
76
77static int
78octeon_pmc_attach(device_t dev)
79{
80	struct octeon_pmc_softc *sc;
81	int error;
82	int rid;
83
84	sc = device_get_softc(dev);
85
86	rid = 0;
87	sc->octeon_pmc_irq = bus_alloc_resource(dev,
88	    SYS_RES_IRQ, &rid, OCTEON_PMC_IRQ,
89	    OCTEON_PMC_IRQ, 1, RF_ACTIVE);
90
91	if (sc->octeon_pmc_irq == NULL) {
92		device_printf(dev, "could not allocate irq%d\n", OCTEON_PMC_IRQ);
93		return (ENXIO);
94	}
95
96	error = bus_setup_intr(dev, sc->octeon_pmc_irq,
97	    INTR_TYPE_MISC, octeon_pmc_intr, NULL, sc, NULL);
98	if (error != 0) {
99		device_printf(dev, "bus_setup_intr failed: %d\n", error);
100		return (error);
101	}
102
103	return (0);
104}
105
106static int
107octeon_pmc_intr(void *arg)
108{
109	struct trapframe *tf = PCPU_GET(curthread)->td_intr_frame;
110
111	if (pmc_intr)
112		(*pmc_intr)(PCPU_GET(cpuid), tf);
113
114	return (FILTER_HANDLED);
115}
116
117static device_method_t octeon_pmc_methods[] = {
118	DEVMETHOD(device_identify,	octeon_pmc_identify),
119	DEVMETHOD(device_probe,		octeon_pmc_probe),
120	DEVMETHOD(device_attach,	octeon_pmc_attach),
121	{ 0, 0 }
122};
123
124static driver_t octeon_pmc_driver = {
125	"pmc",
126	octeon_pmc_methods,
127	sizeof(struct octeon_pmc_softc),
128};
129static devclass_t octeon_pmc_devclass;
130DRIVER_MODULE(octeon_pmc, nexus, octeon_pmc_driver, octeon_pmc_devclass, 0, 0);
131