1/*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifdef __i386__
31#include "opt_eisa.h"
32#include "opt_mca.h"
33#endif
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: releng/10.3/sys/x86/x86/legacy.c 233707 2012-03-30 19:10:14Z jhb $");
36
37/*
38 * This code implements a system driver for legacy systems that do not
39 * support ACPI or when ACPI support is not present in the kernel.
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/bus.h>
45#include <sys/cpu.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/module.h>
49#include <machine/bus.h>
50#include <sys/pcpu.h>
51#include <sys/rman.h>
52#include <sys/smp.h>
53
54#ifdef DEV_MCA
55#include <i386/bios/mca_machdep.h>
56#endif
57
58#include <machine/clock.h>
59#include <machine/resource.h>
60#include <x86/legacyvar.h>
61
62static MALLOC_DEFINE(M_LEGACYDEV, "legacydrv", "legacy system device");
63struct legacy_device {
64	int	lg_pcibus;
65	int	lg_pcislot;
66	int	lg_pcifunc;
67};
68
69#define DEVTOAT(dev)	((struct legacy_device *)device_get_ivars(dev))
70
71static	int legacy_probe(device_t);
72static	int legacy_attach(device_t);
73static	int legacy_print_child(device_t, device_t);
74static device_t legacy_add_child(device_t bus, u_int order, const char *name,
75				int unit);
76static	int legacy_read_ivar(device_t, device_t, int, uintptr_t *);
77static	int legacy_write_ivar(device_t, device_t, int, uintptr_t);
78
79static device_method_t legacy_methods[] = {
80	/* Device interface */
81	DEVMETHOD(device_probe,		legacy_probe),
82	DEVMETHOD(device_attach,	legacy_attach),
83	DEVMETHOD(device_detach,	bus_generic_detach),
84	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
85	DEVMETHOD(device_suspend,	bus_generic_suspend),
86	DEVMETHOD(device_resume,	bus_generic_resume),
87
88	/* Bus interface */
89	DEVMETHOD(bus_print_child,	legacy_print_child),
90	DEVMETHOD(bus_add_child,	legacy_add_child),
91	DEVMETHOD(bus_read_ivar,	legacy_read_ivar),
92	DEVMETHOD(bus_write_ivar,	legacy_write_ivar),
93	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
94	DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
95	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
96	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
97	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
98	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
99	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
100
101	{ 0, 0 }
102};
103
104static driver_t legacy_driver = {
105	"legacy",
106	legacy_methods,
107	1,			/* no softc */
108};
109static devclass_t legacy_devclass;
110
111DRIVER_MODULE(legacy, nexus, legacy_driver, legacy_devclass, 0, 0);
112
113static int
114legacy_probe(device_t dev)
115{
116
117	device_set_desc(dev, "legacy system");
118	device_quiet(dev);
119	return (0);
120}
121
122static int
123legacy_attach(device_t dev)
124{
125	device_t child;
126
127	/*
128	 * Let our child drivers identify any child devices that they
129	 * can find.  Once that is done attach any devices that we
130	 * found.
131	 */
132	bus_generic_probe(dev);
133	bus_generic_attach(dev);
134
135	/*
136	 * If we didn't see EISA or ISA on a pci bridge, create some
137	 * connection points now so they show up "on motherboard".
138	 */
139#ifdef DEV_EISA
140	if (!devclass_get_device(devclass_find("eisa"), 0)) {
141		child = BUS_ADD_CHILD(dev, 0, "eisa", 0);
142		if (child == NULL)
143			panic("legacy_attach eisa");
144		device_probe_and_attach(child);
145	}
146#endif
147#ifdef DEV_MCA
148	if (MCA_system && !devclass_get_device(devclass_find("mca"), 0)) {
149        	child = BUS_ADD_CHILD(dev, 0, "mca", 0);
150        	if (child == 0)
151                	panic("legacy_probe mca");
152		device_probe_and_attach(child);
153	}
154#endif
155	if (!devclass_get_device(devclass_find("isa"), 0)) {
156		child = BUS_ADD_CHILD(dev, 0, "isa", 0);
157		if (child == NULL)
158			panic("legacy_attach isa");
159		device_probe_and_attach(child);
160	}
161
162	return 0;
163}
164
165static int
166legacy_print_child(device_t bus, device_t child)
167{
168	struct legacy_device *atdev = DEVTOAT(child);
169	int retval = 0;
170
171	retval += bus_print_child_header(bus, child);
172	if (atdev->lg_pcibus != -1)
173		retval += printf(" pcibus %d", atdev->lg_pcibus);
174	retval += printf(" on motherboard\n");	/* XXX "motherboard", ick */
175
176	return (retval);
177}
178
179static device_t
180legacy_add_child(device_t bus, u_int order, const char *name, int unit)
181{
182	device_t child;
183	struct legacy_device *atdev;
184
185	atdev = malloc(sizeof(struct legacy_device), M_LEGACYDEV,
186	    M_NOWAIT | M_ZERO);
187	if (atdev == NULL)
188		return(NULL);
189	atdev->lg_pcibus = -1;
190	atdev->lg_pcislot = -1;
191	atdev->lg_pcifunc = -1;
192
193	child = device_add_child_ordered(bus, order, name, unit);
194	if (child == NULL)
195		free(atdev, M_LEGACYDEV);
196	else
197		/* should we free this in legacy_child_detached? */
198		device_set_ivars(child, atdev);
199
200	return (child);
201}
202
203static int
204legacy_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
205{
206	struct legacy_device *atdev = DEVTOAT(child);
207
208	switch (which) {
209	case LEGACY_IVAR_PCIDOMAIN:
210		*result = 0;
211		break;
212	case LEGACY_IVAR_PCIBUS:
213		*result = atdev->lg_pcibus;
214		break;
215	case LEGACY_IVAR_PCISLOT:
216		*result = atdev->lg_pcislot;
217		break;
218	case LEGACY_IVAR_PCIFUNC:
219		*result = atdev->lg_pcifunc;
220		break;
221	default:
222		return ENOENT;
223	}
224	return 0;
225}
226
227
228static int
229legacy_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
230{
231	struct legacy_device *atdev = DEVTOAT(child);
232
233	switch (which) {
234	case LEGACY_IVAR_PCIDOMAIN:
235		return EINVAL;
236	case LEGACY_IVAR_PCIBUS:
237		atdev->lg_pcibus = value;
238		break;
239	case LEGACY_IVAR_PCISLOT:
240		atdev->lg_pcislot = value;
241		break;
242	case LEGACY_IVAR_PCIFUNC:
243		atdev->lg_pcifunc = value;
244		break;
245	default:
246		return ENOENT;
247	}
248	return 0;
249}
250
251/*
252 * Legacy CPU attachment when ACPI is not available.  Drivers like
253 * cpufreq(4) hang off this.
254 */
255static void	cpu_identify(driver_t *driver, device_t parent);
256static int	cpu_read_ivar(device_t dev, device_t child, int index,
257		    uintptr_t *result);
258static device_t cpu_add_child(device_t bus, u_int order, const char *name,
259		    int unit);
260static struct resource_list *cpu_get_rlist(device_t dev, device_t child);
261
262struct cpu_device {
263	struct resource_list cd_rl;
264	struct pcpu *cd_pcpu;
265};
266
267static device_method_t cpu_methods[] = {
268	/* Device interface */
269	DEVMETHOD(device_identify,	cpu_identify),
270	DEVMETHOD(device_probe,		bus_generic_probe),
271	DEVMETHOD(device_attach,	bus_generic_attach),
272	DEVMETHOD(device_detach,	bus_generic_detach),
273	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
274	DEVMETHOD(device_suspend,	bus_generic_suspend),
275	DEVMETHOD(device_resume,	bus_generic_resume),
276
277	/* Bus interface */
278	DEVMETHOD(bus_add_child,	cpu_add_child),
279	DEVMETHOD(bus_read_ivar,	cpu_read_ivar),
280	DEVMETHOD(bus_get_resource_list, cpu_get_rlist),
281	DEVMETHOD(bus_get_resource,	bus_generic_rl_get_resource),
282	DEVMETHOD(bus_set_resource,	bus_generic_rl_set_resource),
283	DEVMETHOD(bus_alloc_resource,	bus_generic_rl_alloc_resource),
284	DEVMETHOD(bus_release_resource,	bus_generic_rl_release_resource),
285	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
286	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
287	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
288	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
289
290	DEVMETHOD_END
291};
292
293static driver_t cpu_driver = {
294	"cpu",
295	cpu_methods,
296	1,		/* no softc */
297};
298static devclass_t cpu_devclass;
299DRIVER_MODULE(cpu, legacy, cpu_driver, cpu_devclass, 0, 0);
300
301static void
302cpu_identify(driver_t *driver, device_t parent)
303{
304	device_t child;
305	int i;
306
307	/*
308	 * Attach a cpuX device for each CPU.  We use an order of 150
309	 * so that these devices are attached after the Host-PCI
310	 * bridges (which are added at order 100).
311	 */
312	CPU_FOREACH(i) {
313		child = BUS_ADD_CHILD(parent, 150, "cpu", i);
314		if (child == NULL)
315			panic("legacy_attach cpu");
316	}
317}
318
319static device_t
320cpu_add_child(device_t bus, u_int order, const char *name, int unit)
321{
322	struct cpu_device *cd;
323	device_t child;
324	struct pcpu *pc;
325
326	if ((cd = malloc(sizeof(*cd), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL)
327		return (NULL);
328
329	resource_list_init(&cd->cd_rl);
330	pc = pcpu_find(device_get_unit(bus));
331	cd->cd_pcpu = pc;
332
333	child = device_add_child_ordered(bus, order, name, unit);
334	if (child != NULL) {
335		pc->pc_device = child;
336		device_set_ivars(child, cd);
337	} else
338		free(cd, M_DEVBUF);
339	return (child);
340}
341
342static struct resource_list *
343cpu_get_rlist(device_t dev, device_t child)
344{
345	struct cpu_device *cpdev;
346
347	cpdev = device_get_ivars(child);
348	return (&cpdev->cd_rl);
349}
350
351static int
352cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
353{
354	struct cpu_device *cpdev;
355
356	switch (index) {
357	case CPU_IVAR_PCPU:
358		cpdev = device_get_ivars(child);
359		*result = (uintptr_t)cpdev->cd_pcpu;
360		break;
361	case CPU_IVAR_NOMINAL_MHZ:
362		if (tsc_is_invariant) {
363			*result = (uintptr_t)(atomic_load_acq_64(&tsc_freq) /
364			    1000000);
365			break;
366		}
367		/* FALLTHROUGH */
368	default:
369		return (ENOENT);
370	}
371	return (0);
372}
373