i386: Allow UP kernel to use LAPIC timer and I/O APIC
[dragonfly.git] / sys / platform / pc32 / i386 / nexus.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/i386/i386/nexus.c,v 1.26.2.10 2003/02/22 13:16:45 imp Exp $
30 */
31
32/*
33 * This code implements a `root nexus' for Intel Architecture
34 * machines. The function of the root nexus is to serve as an
35 * attachment point for both processors and buses, and to manage
36 * resources which are common to all of them. In particular,
37 * this code implements the core resource managers for interrupt
38 * requests, DMA requests (which rightfully should be a part of the
39 * ISA code but it's easier to do it here for now), I/O port addresses,
40 * and I/O memory address space.
41 */
42
984263bc
MD
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/bus.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/module.h>
984263bc 49#include <sys/rman.h>
1b505979 50#include <sys/interrupt.h>
bef055ab 51#include <sys/machintr.h>
984263bc
MD
52
53#include <machine/vmparam.h>
54#include <vm/vm.h>
55#include <vm/pmap.h>
56#include <machine/pmap.h>
57
2899c8b1 58#include <machine/nexusvar.h>
984263bc 59#include <machine/smp.h>
fa6eddaf 60#include <machine_base/apic/ioapic_abi.h>
ed4d621d 61#include <machine_base/apic/ioapic.h>
984263bc 62
47ce2ca6
AP
63#include <bus/pci/pcivar.h>
64#include <bus/pci/pcireg.h>
65#include <bus/pci/pcibus.h>
66#include <bus/pci/pci_cfgreg.h>
67#include <bus/pci/pcib_private.h>
68
69#include "pcib_if.h"
70
984263bc
MD
71static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
72struct nexus_device {
73 struct resource_list nx_resources;
2899c8b1 74 int nx_pcibus;
984263bc
MD
75};
76
77#define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
78
79static struct rman irq_rman, drq_rman, port_rman, mem_rman;
80
81static int nexus_probe(device_t);
82static int nexus_attach(device_t);
83static int nexus_print_all_resources(device_t dev);
84static int nexus_print_child(device_t, device_t);
2581072f
MD
85static device_t nexus_add_child(device_t bus, device_t parent, int order,
86 const char *name, int unit);
984263bc
MD
87static struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
88 u_long, u_long, u_long, u_int);
2899c8b1
JS
89static int nexus_read_ivar(device_t, device_t, int, uintptr_t *);
90static int nexus_write_ivar(device_t, device_t, int, uintptr_t);
984263bc
MD
91static int nexus_activate_resource(device_t, device_t, int, int,
92 struct resource *);
93static int nexus_deactivate_resource(device_t, device_t, int, int,
94 struct resource *);
95static int nexus_release_resource(device_t, device_t, int, int,
96 struct resource *);
bef055ab
SZ
97static int nexus_config_intr(device_t, device_t, int, enum intr_trigger,
98 enum intr_polarity);
984263bc 99static int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
e9cb6d99
MD
100 void (*)(void *), void *,
101 void **, lwkt_serialize_t);
984263bc
MD
102static int nexus_teardown_intr(device_t, device_t, struct resource *,
103 void *);
104static int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
105static int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
106static void nexus_delete_resource(device_t, device_t, int, int);
107
39b5d600
MD
108/*
109 * The device_identify method will cause nexus to automatically associate
110 * and attach to the root bus.
111 */
984263bc
MD
112static device_method_t nexus_methods[] = {
113 /* Device interface */
39b5d600 114 DEVMETHOD(device_identify, bus_generic_identify),
984263bc
MD
115 DEVMETHOD(device_probe, nexus_probe),
116 DEVMETHOD(device_attach, nexus_attach),
117 DEVMETHOD(device_detach, bus_generic_detach),
118 DEVMETHOD(device_shutdown, bus_generic_shutdown),
119 DEVMETHOD(device_suspend, bus_generic_suspend),
120 DEVMETHOD(device_resume, bus_generic_resume),
121
122 /* Bus interface */
123 DEVMETHOD(bus_print_child, nexus_print_child),
124 DEVMETHOD(bus_add_child, nexus_add_child),
2899c8b1
JS
125 DEVMETHOD(bus_read_ivar, nexus_read_ivar),
126 DEVMETHOD(bus_write_ivar, nexus_write_ivar),
984263bc
MD
127 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
128 DEVMETHOD(bus_release_resource, nexus_release_resource),
129 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
130 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
bef055ab 131 DEVMETHOD(bus_config_intr, nexus_config_intr),
984263bc
MD
132 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
133 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
134 DEVMETHOD(bus_set_resource, nexus_set_resource),
135 DEVMETHOD(bus_get_resource, nexus_get_resource),
136 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
137
138 { 0, 0 }
139};
140
141static driver_t nexus_driver = {
142 "nexus",
143 nexus_methods,
144 1, /* no softc */
145};
146static devclass_t nexus_devclass;
147
148DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
149
150static int
151nexus_probe(device_t dev)
152{
984263bc
MD
153 device_quiet(dev); /* suppress attach message for neatness */
154
155 /*
156 * IRQ's are on the mainboard on old systems, but on the ISA part
157 * of PCI->ISA bridges. There would be multiple sets of IRQs on
158 * multi-ISA-bus systems. PCI interrupts are routed to the ISA
159 * component, so in a way, PCI can be a partial child of an ISA bus(!).
160 * APIC interrupts are global though.
161 * In the non-APIC case, disallow the use of IRQ 2.
162 */
163 irq_rman.rm_start = 0;
164 irq_rman.rm_type = RMAN_ARRAY;
165 irq_rman.rm_descr = "Interrupt request lines";
30c5f287 166
1e7aaefa
SZ
167 if (ioapic_enable) {
168 irq_rman.rm_end = APIC_INTMAPSIZE - 1;
169 if (rman_init(&irq_rman)
170 || rman_manage_region(&irq_rman,
171 irq_rman.rm_start, irq_rman.rm_end))
172 panic("nexus_probe irq_rman");
173 } else {
174 irq_rman.rm_end = 15;
175 if (rman_init(&irq_rman)
176 || rman_manage_region(&irq_rman, irq_rman.rm_start, 1)
177 || rman_manage_region(&irq_rman, 3, irq_rman.rm_end))
178 panic("nexus_probe irq_rman");
179 }
180
984263bc
MD
181 /*
182 * ISA DMA on PCI systems is implemented in the ISA part of each
183 * PCI->ISA bridge and the channels can be duplicated if there are
184 * multiple bridges. (eg: laptops with docking stations)
185 */
186 drq_rman.rm_start = 0;
984263bc 187 drq_rman.rm_end = 7;
984263bc
MD
188 drq_rman.rm_type = RMAN_ARRAY;
189 drq_rman.rm_descr = "DMA request lines";
190 /* XXX drq 0 not available on some machines */
191 if (rman_init(&drq_rman)
192 || rman_manage_region(&drq_rman,
193 drq_rman.rm_start, drq_rman.rm_end))
194 panic("nexus_probe drq_rman");
195
196 /*
197 * However, IO ports and Memory truely are global at this level,
198 * as are APIC interrupts (however many IO APICS there turn out
199 * to be on large systems..)
200 */
201 port_rman.rm_start = 0;
202 port_rman.rm_end = 0xffff;
203 port_rman.rm_type = RMAN_ARRAY;
204 port_rman.rm_descr = "I/O ports";
205 if (rman_init(&port_rman)
206 || rman_manage_region(&port_rman, 0, 0xffff))
207 panic("nexus_probe port_rman");
208
209 mem_rman.rm_start = 0;
210 mem_rman.rm_end = ~0u;
211 mem_rman.rm_type = RMAN_ARRAY;
212 mem_rman.rm_descr = "I/O memory addresses";
213 if (rman_init(&mem_rman)
214 || rman_manage_region(&mem_rman, 0, ~0))
215 panic("nexus_probe mem_rman");
216
217 return bus_generic_probe(dev);
218}
219
220static int
221nexus_attach(device_t dev)
222{
223 device_t child;
224
225 /*
41a01a4d
MD
226 * First, let our child driver's identify any child devices that
227 * they can find. Once that is done attach any devices that we
228 * found.
984263bc 229 */
41a01a4d
MD
230#if 0 /* FUTURE */
231 bus_generic_probe(dev);
232#endif
984263bc 233 bus_generic_attach(dev);
41a01a4d 234
984263bc
MD
235 /*
236 * And if we didn't see EISA or ISA on a pci bridge, create some
237 * connection points now so they show up "on motherboard".
238 */
239 if (!devclass_get_device(devclass_find("eisa"), 0)) {
2581072f 240 child = BUS_ADD_CHILD(dev, dev, 0, "eisa", 0);
984263bc
MD
241 if (child == NULL)
242 panic("nexus_attach eisa");
243 device_probe_and_attach(child);
244 }
984263bc 245 if (!devclass_get_device(devclass_find("isa"), 0)) {
2581072f 246 child = BUS_ADD_CHILD(dev, dev, 0, "isa", 0);
984263bc
MD
247 if (child == NULL)
248 panic("nexus_attach isa");
249 device_probe_and_attach(child);
250 }
251
252 return 0;
253}
254
255static int
256nexus_print_all_resources(device_t dev)
257{
258 struct nexus_device *ndev = DEVTONX(dev);
259 struct resource_list *rl = &ndev->nx_resources;
260 int retval = 0;
261
2899c8b1 262 if (SLIST_FIRST(rl) || ndev->nx_pcibus != -1)
26be20a0 263 retval += kprintf(" at");
984263bc
MD
264
265 retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
266 retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
267 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
268
269 return retval;
270}
271
272static int
273nexus_print_child(device_t bus, device_t child)
274{
2899c8b1 275 struct nexus_device *ndev = DEVTONX(child);
984263bc
MD
276 int retval = 0;
277
278 retval += bus_print_child_header(bus, child);
279 retval += nexus_print_all_resources(child);
2899c8b1 280 if (ndev->nx_pcibus != -1)
26be20a0
SW
281 retval += kprintf(" pcibus %d", ndev->nx_pcibus);
282 retval += kprintf(" on motherboard\n");
984263bc
MD
283
284 return (retval);
285}
286
287static device_t
2581072f
MD
288nexus_add_child(device_t bus, device_t parent, int order,
289 const char *name, int unit)
984263bc
MD
290{
291 device_t child;
292 struct nexus_device *ndev;
293
efda3bd0 294 ndev = kmalloc(sizeof(struct nexus_device), M_NEXUSDEV, M_INTWAIT|M_ZERO);
984263bc
MD
295 if (!ndev)
296 return(0);
297 resource_list_init(&ndev->nx_resources);
2899c8b1 298 ndev->nx_pcibus = -1;
984263bc 299
2581072f 300 child = device_add_child_ordered(parent, order, name, unit);
984263bc
MD
301
302 /* should we free this in nexus_child_detached? */
303 device_set_ivars(child, ndev);
304
305 return(child);
306}
307
2899c8b1
JS
308static int
309nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
310{
311 struct nexus_device *ndev = DEVTONX(child);
312
313 switch (which) {
314 case NEXUS_IVAR_PCIBUS:
315 *result = ndev->nx_pcibus;
316 break;
317 default:
318 return ENOENT;
319 }
320 return 0;
321}
322
323static int
324nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
325{
326 struct nexus_device *ndev = DEVTONX(child);
327
328 switch (which) {
329 case NEXUS_IVAR_PCIBUS:
330 ndev->nx_pcibus = value;
331 break;
332 default:
333 return ENOENT;
334 }
335 return 0;
336}
337
984263bc
MD
338/*
339 * Allocate a resource on behalf of child. NB: child is usually going to be a
340 * child of one of our descendants, not a direct child of nexus0.
341 * (Exceptions include npx.)
342 */
343static struct resource *
344nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
345 u_long start, u_long end, u_long count, u_int flags)
346{
347 struct nexus_device *ndev = DEVTONX(child);
348 struct resource *rv;
349 struct resource_list_entry *rle;
350 struct rman *rm;
351 int needactivate = flags & RF_ACTIVE;
352
353 /*
354 * If this is an allocation of the "default" range for a given RID, and
355 * we know what the resources for this device are (ie. they aren't maintained
356 * by a child bus), then work out the start/end values.
357 */
358 if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
359 if (ndev == NULL)
360 return(NULL);
361 rle = resource_list_find(&ndev->nx_resources, type, *rid);
362 if (rle == NULL)
363 return(NULL);
364 start = rle->start;
365 end = rle->end;
366 count = rle->count;
367 }
368
369 flags &= ~RF_ACTIVE;
370
371 switch (type) {
372 case SYS_RES_IRQ:
373 rm = &irq_rman;
374 break;
375
376 case SYS_RES_DRQ:
377 rm = &drq_rman;
378 break;
379
380 case SYS_RES_IOPORT:
381 rm = &port_rman;
382 break;
383
384 case SYS_RES_MEMORY:
385 rm = &mem_rman;
386 break;
387
388 default:
389 return 0;
390 }
391
392 rv = rman_reserve_resource(rm, start, end, count, flags, child);
393 if (rv == 0)
394 return 0;
395
396 if (type == SYS_RES_MEMORY) {
397 rman_set_bustag(rv, I386_BUS_SPACE_MEM);
398 } else if (type == SYS_RES_IOPORT) {
399 rman_set_bustag(rv, I386_BUS_SPACE_IO);
984263bc 400 rman_set_bushandle(rv, rv->r_start);
984263bc
MD
401 }
402
984263bc
MD
403 if (needactivate) {
404 if (bus_activate_resource(child, type, *rid, rv)) {
984263bc
MD
405 rman_release_resource(rv);
406 return 0;
407 }
408 }
409
410 return rv;
411}
412
413static int
414nexus_activate_resource(device_t bus, device_t child, int type, int rid,
415 struct resource *r)
416{
417 /*
418 * If this is a memory resource, map it into the kernel.
419 */
420 if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
421 caddr_t vaddr = 0;
422
778931ae 423 if (rman_get_end(r) < 1024 * 1024) {
984263bc
MD
424 /*
425 * The first 1Mb is mapped at KERNBASE.
426 */
778931ae 427 vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
984263bc
MD
428 } else {
429 u_int32_t paddr;
430 u_int32_t psize;
431 u_int32_t poffs;
432
778931ae
JS
433 paddr = rman_get_start(r);
434 psize = rman_get_size(r);
984263bc
MD
435
436 poffs = paddr - trunc_page(paddr);
437 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
438 }
439 rman_set_virtual(r, vaddr);
984263bc
MD
440 /* IBM-PC: the type of bus_space_handle_t is u_int */
441 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
984263bc
MD
442 }
443 return (rman_activate_resource(r));
444}
445
446static int
447nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
448 struct resource *r)
449{
450 /*
451 * If this is a memory resource, unmap it.
452 */
778931ae
JS
453 if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) &&
454 (rman_get_end(r) >= 1024 * 1024)) {
984263bc
MD
455 u_int32_t psize;
456
778931ae 457 psize = rman_get_size(r);
984263bc
MD
458 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
459 }
460
461 return (rman_deactivate_resource(r));
462}
463
464static int
465nexus_release_resource(device_t bus, device_t child, int type, int rid,
466 struct resource *r)
467{
778931ae 468 if (rman_get_flags(r) & RF_ACTIVE) {
984263bc
MD
469 int error = bus_deactivate_resource(child, type, rid, r);
470 if (error)
471 return error;
472 }
984263bc
MD
473 return (rman_release_resource(r));
474}
475
bef055ab
SZ
476static int
477nexus_config_intr(device_t bus, device_t chile, int irq,
478 enum intr_trigger trig, enum intr_polarity pola)
479{
480 machintr_intr_config(irq, trig, pola);
481 return 0;
482}
483
984263bc
MD
484/*
485 * Currently this uses the really grody interface from kern/kern_intr.c
486 * (which really doesn't belong in kern/anything.c). Eventually, all of
487 * the code in kern_intr.c and machdep_intr.c should get moved here, since
488 * this is going to be the official interface.
489 */
490static int
491nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
e9cb6d99
MD
492 int flags, void (*ihand)(void *), void *arg,
493 void **cookiep, lwkt_serialize_t serializer)
984263bc 494{
984263bc
MD
495 int error, icflags;
496
497 /* somebody tried to setup an irq that failed to allocate! */
498 if (irq == NULL)
499 panic("nexus_setup_intr: NULL irq resource!");
500
501 *cookiep = 0;
2581072f
MD
502 icflags = flags;
503 if ((irq->r_flags & RF_SHAREABLE) == 0)
504 icflags |= INTR_EXCL;
984263bc 505
984263bc
MD
506 /*
507 * We depend here on rman_activate_resource() being idempotent.
508 */
509 error = rman_activate_resource(irq);
510 if (error)
511 return (error);
512
477d3c1c
MD
513 /*
514 * XXX cast the interrupt handler function to an inthand2_t. The
515 * difference is that an additional frame argument is passed which
516 * we do not currently want to expose the BUS subsystem to.
517 */
9d522d14
MD
518 *cookiep = register_int(irq->r_start, (inthand2_t *)ihand, arg,
519 device_get_nameunit(child), serializer,
520 icflags);
984263bc 521 if (*cookiep == NULL)
9d522d14 522 error = EINVAL;
984263bc
MD
523 return (error);
524}
525
526static int
527nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
528{
9d522d14
MD
529 if (ih) {
530 unregister_int(ih);
531 return (0);
532 }
533 return(-1);
984263bc
MD
534}
535
536static int
537nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
538{
539 struct nexus_device *ndev = DEVTONX(child);
540 struct resource_list *rl = &ndev->nx_resources;
541
542 /* XXX this should return a success/failure indicator */
543 resource_list_add(rl, type, rid, start, start + count - 1, count);
544 return(0);
545}
546
547static int
548nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
549{
550 struct nexus_device *ndev = DEVTONX(child);
551 struct resource_list *rl = &ndev->nx_resources;
552 struct resource_list_entry *rle;
553
554 rle = resource_list_find(rl, type, rid);
555 device_printf(child, "type %d rid %d startp %p countp %p - got %p\n",
556 type, rid, startp, countp, rle);
557 if (!rle)
558 return(ENOENT);
559 if (startp)
560 *startp = rle->start;
561 if (countp)
562 *countp = rle->count;
563 return(0);
564}
565
566static void
567nexus_delete_resource(device_t dev, device_t child, int type, int rid)
568{
569 struct nexus_device *ndev = DEVTONX(child);
570 struct resource_list *rl = &ndev->nx_resources;
571
572 resource_list_delete(rl, type, rid);
573}
9713df24 574