1/* 	$NetBSD: aapic.c,v 1.7 2008/07/09 21:07:55 joerg Exp $	*/
2
3#include <sys/cdefs.h>
4__KERNEL_RCSID(0, "$NetBSD: aapic.c,v 1.7 2008/07/09 21:07:55 joerg Exp $");
5
6#include <sys/param.h>
7#include <sys/systm.h>
8#include <sys/kernel.h>
9#include <sys/device.h>
10
11#include <dev/pci/pcireg.h>
12#include <dev/pci/pcivar.h>
13#include <dev/pci/pcidevs.h>
14
15#include <arch/x86/pci/amd8131reg.h>
16
17#include "ioapic.h"
18
19#if NIOAPIC > 0
20extern int nioapics;
21#endif
22
23static int	aapic_match(device_t, cfdata_t, void *);
24static void	aapic_attach(device_t, device_t, void *);
25
26CFATTACH_DECL_NEW(aapic, 0, aapic_match, aapic_attach, NULL, NULL);
27
28static int
29aapic_match(device_t parent, cfdata_t match, void *aux)
30{
31	struct pci_attach_args *pa = aux;
32
33	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
34	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PCIX8131_APIC)
35		return (1);
36
37	return (0);
38}
39
40static void
41aapic_attach(device_t parent, device_t self, void *aux)
42{
43	struct pci_attach_args *pa = aux;
44	char devinfo[256];
45	int bus, dev, func, rev;
46	pcitag_t tag;
47	pcireg_t reg;
48
49	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
50	rev = PCI_REVISION(pa->pa_class);
51	aprint_naive("\n");
52	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, rev);
53
54#if NIOAPIC > 0
55	if (nioapics == 0)
56		return;
57#else
58	return;
59#endif
60
61	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMD8131_IOAPIC_CTL);
62	reg |= AMD8131_IOAEN;
63	pci_conf_write(pa->pa_pc, pa->pa_tag, AMD8131_IOAPIC_CTL, reg);
64
65	pci_decompose_tag(pa->pa_pc, pa->pa_tag, &bus, &dev, &func);
66	func = 0;
67	tag = pci_make_tag(pa->pa_pc, bus, dev, func);
68	reg = pci_conf_read(pa->pa_pc, tag, AMD8131_PCIX_MISC);
69	reg &= ~AMD8131_NIOAMODE;
70	pci_conf_write(pa->pa_pc, tag, AMD8131_PCIX_MISC, reg);
71}
72