amdsmn.c revision 323184
1/*-
2 * Copyright (c) 2017 Conrad Meyer <cem@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 ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*
28 * Driver for the AMD Family 17h CPU System Management Network.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/dev/amdsmn/amdsmn.c 323184 2017-09-05 15:13:41Z cem $");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/conf.h>
37#include <sys/lock.h>
38#include <sys/kernel.h>
39#include <sys/module.h>
40#include <sys/mutex.h>
41#include <sys/sysctl.h>
42#include <sys/systm.h>
43
44#include <machine/cpufunc.h>
45#include <machine/md_var.h>
46#include <machine/specialreg.h>
47
48#include <dev/pci/pcivar.h>
49#include <x86/pci_cfgreg.h>
50
51#include <dev/amdsmn/amdsmn.h>
52
53#define	SMN_ADDR_REG	0x60
54#define	SMN_DATA_REG	0x64
55
56struct amdsmn_softc {
57	struct mtx smn_lock;
58};
59
60static struct pciid {
61	uint32_t	device_id;
62} amdsmn_ids[] = {
63	{ 0x14501022 },
64};
65
66/*
67 * Device methods.
68 */
69static void 	amdsmn_identify(driver_t *driver, device_t parent);
70static int	amdsmn_probe(device_t dev);
71static int	amdsmn_attach(device_t dev);
72static int	amdsmn_detach(device_t dev);
73
74static device_method_t amdsmn_methods[] = {
75	/* Device interface */
76	DEVMETHOD(device_identify,	amdsmn_identify),
77	DEVMETHOD(device_probe,		amdsmn_probe),
78	DEVMETHOD(device_attach,	amdsmn_attach),
79	DEVMETHOD(device_detach,	amdsmn_detach),
80	DEVMETHOD_END
81};
82
83static driver_t amdsmn_driver = {
84	"amdsmn",
85	amdsmn_methods,
86	sizeof(struct amdsmn_softc),
87};
88
89static devclass_t amdsmn_devclass;
90DRIVER_MODULE(amdsmn, hostb, amdsmn_driver, amdsmn_devclass, NULL, NULL);
91MODULE_VERSION(amdsmn, 1);
92
93static void
94amdsmn_identify(driver_t *driver, device_t parent)
95{
96	device_t child;
97	uint32_t devid;
98	size_t i;
99
100	/* Make sure we're not being doubly invoked. */
101	if (device_find_child(parent, "amdsmn", -1) != NULL)
102		return;
103
104	devid = pci_get_devid(parent);
105	for (i = 0; i < nitems(amdsmn_ids); i++)
106		if (amdsmn_ids[i].device_id == devid)
107			break;
108
109	if (i >= nitems(amdsmn_ids))
110		return;
111
112	child = device_add_child(parent, "amdsmn", -1);
113	if (child == NULL)
114		device_printf(parent, "add amdsmn child failed\n");
115}
116
117static int
118amdsmn_probe(device_t dev)
119{
120	uint32_t family;
121
122	if (resource_disabled("amdsmn", 0))
123		return (ENXIO);
124
125	family = CPUID_TO_FAMILY(cpu_id);
126
127	switch (family) {
128	case 0x17:
129		break;
130	default:
131		return (ENXIO);
132	}
133	device_set_desc(dev, "AMD Family 17h System Management Network");
134
135	return (BUS_PROBE_GENERIC);
136}
137
138static int
139amdsmn_attach(device_t dev)
140{
141	struct amdsmn_softc *sc = device_get_softc(dev);
142
143	mtx_init(&sc->smn_lock, "SMN mtx", "SMN", MTX_DEF);
144	return (0);
145}
146
147int
148amdsmn_detach(device_t dev)
149{
150	struct amdsmn_softc *sc = device_get_softc(dev);
151
152	mtx_destroy(&sc->smn_lock);
153	return (0);
154}
155
156int
157amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
158{
159	struct amdsmn_softc *sc = device_get_softc(dev);
160	device_t parent;
161
162	parent = device_get_parent(dev);
163
164	mtx_lock(&sc->smn_lock);
165	pci_write_config(parent, SMN_ADDR_REG, addr, 4);
166	*value = pci_read_config(parent, SMN_DATA_REG, 4);
167	mtx_unlock(&sc->smn_lock);
168
169	return (0);
170}
171
172int
173amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
174{
175	struct amdsmn_softc *sc = device_get_softc(dev);
176	device_t parent;
177
178	parent = device_get_parent(dev);
179
180	mtx_lock(&sc->smn_lock);
181	pci_write_config(parent, SMN_ADDR_REG, addr, 4);
182	pci_write_config(parent, SMN_DATA_REG, value, 4);
183	mtx_unlock(&sc->smn_lock);
184
185	return (0);
186}
187