1/*-
2 * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/malloc.h>
34#include <sys/socket.h>
35
36#include <sys/module.h>
37#include <sys/bus.h>
38
39#include <machine/bus.h>
40#include <machine/resource.h>
41#include <sys/rman.h>
42
43#include <vm/vm.h>
44#include <vm/vm_param.h>
45#include <vm/pmap.h>
46#include <machine/md_var.h>
47#include <machine/pc/bios.h>
48
49/*
50 * System Management BIOS Reference Specification, v2.4 Final
51 * http://www.dmtf.org/standards/published_documents/DSP0134.pdf
52 */
53
54struct smbios_softc {
55	device_t		dev;
56	struct resource *	res;
57	int			rid;
58
59	struct smbios_eps *	eps;
60};
61
62#define	RES2EPS(res)	((struct smbios_eps *)rman_get_virtual(res))
63#define	ADDR2EPS(addr)  ((struct smbios_eps *)BIOS_PADDRTOVADDR(addr))
64
65static devclass_t	smbios_devclass;
66
67static void	smbios_identify	(driver_t *, device_t);
68static int	smbios_probe	(device_t);
69static int	smbios_attach	(device_t);
70static int	smbios_detach	(device_t);
71static int	smbios_modevent	(module_t, int, void *);
72
73static int	smbios_cksum	(struct smbios_eps *);
74
75static void
76smbios_identify (driver_t *driver, device_t parent)
77{
78	device_t child;
79	u_int32_t addr;
80	int length;
81	int rid;
82
83	if (!device_is_alive(parent))
84		return;
85
86	addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN,
87			      SMBIOS_STEP, SMBIOS_OFF);
88	if (addr != 0) {
89		rid = 0;
90		length = ADDR2EPS(addr)->length;
91
92		if (length != 0x1f) {
93			u_int8_t major, minor;
94
95			major = ADDR2EPS(addr)->major_version;
96			minor = ADDR2EPS(addr)->minor_version;
97
98			/* SMBIOS v2.1 implementation might use 0x1e. */
99			if (length == 0x1e && major == 2 && minor == 1)
100				length = 0x1f;
101			else
102				return;
103		}
104
105		child = BUS_ADD_CHILD(parent, 5, "smbios", -1);
106		device_set_driver(child, driver);
107		bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
108		device_set_desc(child, "System Management BIOS");
109	}
110
111	return;
112}
113
114static int
115smbios_probe (device_t dev)
116{
117	struct resource *res;
118	int rid;
119	int error;
120
121	error = 0;
122	rid = 0;
123	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
124	if (res == NULL) {
125		device_printf(dev, "Unable to allocate memory resource.\n");
126		error = ENOMEM;
127		goto bad;
128	}
129
130	if (smbios_cksum(RES2EPS(res))) {
131		device_printf(dev, "SMBIOS checksum failed.\n");
132		error = ENXIO;
133		goto bad;
134	}
135
136bad:
137	if (res)
138		bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
139	return (error);
140}
141
142static int
143smbios_attach (device_t dev)
144{
145	struct smbios_softc *sc;
146	int error;
147
148	sc = device_get_softc(dev);
149	error = 0;
150
151	sc->dev = dev;
152	sc->rid = 0;
153	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
154		RF_ACTIVE);
155	if (sc->res == NULL) {
156		device_printf(dev, "Unable to allocate memory resource.\n");
157		error = ENOMEM;
158		goto bad;
159	}
160	sc->eps = RES2EPS(sc->res);
161
162	device_printf(dev, "Version: %u.%u",
163	    sc->eps->major_version, sc->eps->minor_version);
164	if (bcd2bin(sc->eps->BCD_revision))
165		printf(", BCD Revision: %u.%u",
166			bcd2bin(sc->eps->BCD_revision >> 4),
167			bcd2bin(sc->eps->BCD_revision & 0x0f));
168	printf("\n");
169
170	return (0);
171bad:
172	if (sc->res)
173		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
174	return (error);
175}
176
177static int
178smbios_detach (device_t dev)
179{
180	struct smbios_softc *sc;
181
182	sc = device_get_softc(dev);
183
184	if (sc->res)
185		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
186
187	return (0);
188}
189
190static int
191smbios_modevent (mod, what, arg)
192        module_t        mod;
193        int             what;
194        void *          arg;
195{
196	device_t *	devs;
197	int		count;
198	int		i;
199
200	switch (what) {
201	case MOD_LOAD:
202		break;
203	case MOD_UNLOAD:
204		devclass_get_devices(smbios_devclass, &devs, &count);
205		for (i = 0; i < count; i++) {
206			device_delete_child(device_get_parent(devs[i]), devs[i]);
207		}
208		free(devs, M_TEMP);
209		break;
210	default:
211		break;
212	}
213
214	return (0);
215}
216
217static device_method_t smbios_methods[] = {
218	/* Device interface */
219	DEVMETHOD(device_identify,      smbios_identify),
220	DEVMETHOD(device_probe,         smbios_probe),
221	DEVMETHOD(device_attach,        smbios_attach),
222	DEVMETHOD(device_detach,        smbios_detach),
223	{ 0, 0 }
224};
225
226static driver_t smbios_driver = {
227	"smbios",
228	smbios_methods,
229	sizeof(struct smbios_softc),
230};
231
232DRIVER_MODULE(smbios, nexus, smbios_driver, smbios_devclass, smbios_modevent, 0);
233MODULE_VERSION(smbios, 1);
234
235static int
236smbios_cksum (struct smbios_eps *e)
237{
238	u_int8_t *ptr;
239	u_int8_t cksum;
240	int i;
241
242	ptr = (u_int8_t *)e;
243	cksum = 0;
244	for (i = 0; i < e->length; i++) {
245		cksum += ptr[i];
246	}
247
248	return (cksum);
249}
250