vga_pci.c revision 1.8 1 1.8 lukem /* $NetBSD: vga_pci.c,v 1.8 2001/11/13 07:48:49 lukem Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1 drochner * All rights reserved.
6 1.1 drochner *
7 1.1 drochner * Author: Chris G. Demetriou
8 1.1 drochner *
9 1.1 drochner * Permission to use, copy, modify and distribute this software and
10 1.1 drochner * its documentation is hereby granted, provided that both the copyright
11 1.1 drochner * notice and this permission notice appear in all copies of the
12 1.1 drochner * software, derivative works or modified versions, and any portions
13 1.1 drochner * thereof, and that both notices appear in supporting documentation.
14 1.1 drochner *
15 1.1 drochner * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1 drochner * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 drochner * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1 drochner *
19 1.1 drochner * Carnegie Mellon requests users of this software to return to
20 1.1 drochner *
21 1.1 drochner * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 drochner * School of Computer Science
23 1.1 drochner * Carnegie Mellon University
24 1.1 drochner * Pittsburgh PA 15213-3890
25 1.1 drochner *
26 1.1 drochner * any improvements or extensions that they make and grant Carnegie the
27 1.1 drochner * rights to redistribute these changes.
28 1.1 drochner */
29 1.8 lukem
30 1.8 lukem #include <sys/cdefs.h>
31 1.8 lukem __KERNEL_RCSID(0, "$NetBSD: vga_pci.c,v 1.8 2001/11/13 07:48:49 lukem Exp $");
32 1.1 drochner
33 1.1 drochner #include <sys/param.h>
34 1.1 drochner #include <sys/systm.h>
35 1.1 drochner #include <sys/kernel.h>
36 1.1 drochner #include <sys/device.h>
37 1.1 drochner #include <sys/malloc.h>
38 1.1 drochner
39 1.1 drochner #include <dev/pci/pcireg.h>
40 1.1 drochner #include <dev/pci/pcivar.h>
41 1.1 drochner #include <dev/pci/pcidevs.h>
42 1.6 thorpej #include <dev/pci/pciio.h>
43 1.1 drochner
44 1.2 drochner #include <dev/ic/mc6845reg.h>
45 1.2 drochner #include <dev/ic/pcdisplayvar.h>
46 1.1 drochner #include <dev/ic/vgareg.h>
47 1.1 drochner #include <dev/ic/vgavar.h>
48 1.1 drochner #include <dev/pci/vga_pcivar.h>
49 1.1 drochner
50 1.7 thorpej #include <dev/isa/isareg.h> /* For legacy VGA address ranges */
51 1.7 thorpej
52 1.1 drochner #include <dev/wscons/wsconsio.h>
53 1.1 drochner #include <dev/wscons/wsdisplayvar.h>
54 1.1 drochner
55 1.7 thorpej #define NBARS 6 /* number of PCI BARs */
56 1.7 thorpej
57 1.7 thorpej struct vga_bar {
58 1.7 thorpej bus_addr_t vb_base;
59 1.7 thorpej bus_size_t vb_size;
60 1.7 thorpej pcireg_t vb_type;
61 1.7 thorpej int vb_flags;
62 1.7 thorpej };
63 1.7 thorpej
64 1.1 drochner struct vga_pci_softc {
65 1.5 thorpej struct vga_softc sc_vga;
66 1.5 thorpej
67 1.6 thorpej pci_chipset_tag_t sc_pc;
68 1.5 thorpej pcitag_t sc_pcitag;
69 1.7 thorpej
70 1.7 thorpej struct vga_bar sc_bars[NBARS];
71 1.7 thorpej struct vga_bar sc_rom;
72 1.1 drochner };
73 1.1 drochner
74 1.1 drochner int vga_pci_match __P((struct device *, struct cfdata *, void *));
75 1.1 drochner void vga_pci_attach __P((struct device *, struct device *, void *));
76 1.1 drochner
77 1.1 drochner struct cfattach vga_pci_ca = {
78 1.1 drochner sizeof(struct vga_pci_softc), vga_pci_match, vga_pci_attach,
79 1.1 drochner };
80 1.1 drochner
81 1.6 thorpej int vga_pci_ioctl(void *, u_long, caddr_t, int, struct proc *);
82 1.6 thorpej paddr_t vga_pci_mmap(void *, off_t, int);
83 1.6 thorpej
84 1.6 thorpej const struct vga_funcs vga_pci_funcs = {
85 1.6 thorpej vga_pci_ioctl,
86 1.6 thorpej vga_pci_mmap,
87 1.6 thorpej };
88 1.6 thorpej
89 1.1 drochner int
90 1.1 drochner vga_pci_match(parent, match, aux)
91 1.1 drochner struct device *parent;
92 1.1 drochner struct cfdata *match;
93 1.1 drochner void *aux;
94 1.1 drochner {
95 1.1 drochner struct pci_attach_args *pa = aux;
96 1.1 drochner int potential;
97 1.1 drochner
98 1.1 drochner potential = 0;
99 1.1 drochner
100 1.1 drochner /*
101 1.1 drochner * If it's prehistoric/vga or display/vga, we might match.
102 1.1 drochner * For the console device, this is jut a sanity check.
103 1.1 drochner */
104 1.1 drochner if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
105 1.1 drochner PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
106 1.1 drochner potential = 1;
107 1.1 drochner if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
108 1.1 drochner PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
109 1.1 drochner potential = 1;
110 1.1 drochner
111 1.1 drochner if (!potential)
112 1.1 drochner return (0);
113 1.1 drochner
114 1.1 drochner /* check whether it is disabled by firmware */
115 1.1 drochner if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
116 1.1 drochner & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
117 1.1 drochner != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
118 1.1 drochner return (0);
119 1.1 drochner
120 1.1 drochner /* If it's the console, we have a winner! */
121 1.1 drochner if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
122 1.1 drochner return (1);
123 1.1 drochner
124 1.1 drochner /*
125 1.1 drochner * If we might match, make sure that the card actually looks OK.
126 1.1 drochner */
127 1.1 drochner if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
128 1.1 drochner return (0);
129 1.1 drochner
130 1.1 drochner return (1);
131 1.1 drochner }
132 1.1 drochner
133 1.1 drochner void
134 1.1 drochner vga_pci_attach(parent, self, aux)
135 1.1 drochner struct device *parent, *self;
136 1.1 drochner void *aux;
137 1.1 drochner {
138 1.5 thorpej struct vga_pci_softc *psc = (void *) self;
139 1.5 thorpej struct vga_softc *sc = &psc->sc_vga;
140 1.1 drochner struct pci_attach_args *pa = aux;
141 1.1 drochner char devinfo[256];
142 1.7 thorpej int bar, reg;
143 1.1 drochner
144 1.6 thorpej psc->sc_pc = pa->pa_pc;
145 1.5 thorpej psc->sc_pcitag = pa->pa_tag;
146 1.1 drochner
147 1.1 drochner pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
148 1.1 drochner printf(": %s (rev. 0x%02x)\n", devinfo,
149 1.1 drochner PCI_REVISION(pa->pa_class));
150 1.1 drochner
151 1.7 thorpej /*
152 1.7 thorpej * Gather info about all the BARs. These are used to allow
153 1.7 thorpej * the X server to map the VGA device.
154 1.7 thorpej */
155 1.7 thorpej for (bar = 0; bar < NBARS; bar++) {
156 1.7 thorpej reg = PCI_MAPREG_START + (bar * 4);
157 1.7 thorpej psc->sc_bars[bar].vb_type = pci_mapreg_type(psc->sc_pc,
158 1.7 thorpej psc->sc_pcitag, reg);
159 1.7 thorpej if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
160 1.7 thorpej PCI_MAPREG_TYPE_IO) {
161 1.7 thorpej /* Don't bother fetching I/O BARs. */
162 1.7 thorpej continue;
163 1.7 thorpej }
164 1.7 thorpej if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
165 1.7 thorpej PCI_MAPREG_MEM_TYPE_64BIT) {
166 1.7 thorpej /* XXX */
167 1.7 thorpej printf("%s: WARNING: ignoring 64-bit BAR @ 0x%02x\n",
168 1.7 thorpej sc->sc_dev.dv_xname, reg);
169 1.7 thorpej continue;
170 1.7 thorpej }
171 1.7 thorpej /* Ignore errors (unimplemented BARs). */
172 1.7 thorpej (void) pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
173 1.7 thorpej psc->sc_bars[bar].vb_type,
174 1.7 thorpej &psc->sc_bars[bar].vb_base,
175 1.7 thorpej &psc->sc_bars[bar].vb_size,
176 1.7 thorpej &psc->sc_bars[bar].vb_flags);
177 1.7 thorpej }
178 1.7 thorpej
179 1.7 thorpej /* XXX Expansion ROM? */
180 1.7 thorpej
181 1.5 thorpej vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
182 1.6 thorpej &vga_pci_funcs);
183 1.1 drochner }
184 1.1 drochner
185 1.1 drochner int
186 1.1 drochner vga_pci_cnattach(iot, memt, pc, bus, device, function)
187 1.1 drochner bus_space_tag_t iot, memt;
188 1.1 drochner pci_chipset_tag_t pc;
189 1.1 drochner int bus, device, function;
190 1.1 drochner {
191 1.1 drochner return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
192 1.6 thorpej }
193 1.6 thorpej
194 1.6 thorpej int
195 1.6 thorpej vga_pci_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
196 1.6 thorpej {
197 1.6 thorpej struct vga_config *vc = v;
198 1.6 thorpej struct vga_pci_softc *psc = (void *) vc->softc;
199 1.6 thorpej
200 1.6 thorpej switch (cmd) {
201 1.6 thorpej /* PCI config read/write passthrough. */
202 1.6 thorpej case PCI_IOC_CFGREAD:
203 1.6 thorpej case PCI_IOC_CFGWRITE:
204 1.6 thorpej return (pci_devioctl(psc->sc_pc, psc->sc_pcitag,
205 1.6 thorpej cmd, data, flag, p));
206 1.6 thorpej
207 1.6 thorpej default:
208 1.6 thorpej return (ENOTTY);
209 1.6 thorpej }
210 1.6 thorpej }
211 1.6 thorpej
212 1.6 thorpej paddr_t
213 1.6 thorpej vga_pci_mmap(void *v, off_t offset, int prot)
214 1.6 thorpej {
215 1.7 thorpej struct vga_config *vc = v;
216 1.7 thorpej struct vga_pci_softc *psc = (void *) vc->softc;
217 1.7 thorpej struct vga_bar *vb;
218 1.7 thorpej int bar;
219 1.7 thorpej
220 1.7 thorpej for (bar = 0; bar < NBARS; bar++) {
221 1.7 thorpej vb = &psc->sc_bars[bar];
222 1.7 thorpej if (vb->vb_size == 0)
223 1.7 thorpej continue;
224 1.7 thorpej if (offset >= vb->vb_base &&
225 1.7 thorpej offset < (vb->vb_base + vb->vb_size)) {
226 1.7 thorpej /* XXX This the right thing to do with flags? */
227 1.7 thorpej return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
228 1.7 thorpej (offset - vb->vb_base), prot, vb->vb_flags));
229 1.7 thorpej }
230 1.7 thorpej }
231 1.7 thorpej
232 1.7 thorpej /* XXX Expansion ROM? */
233 1.7 thorpej
234 1.7 thorpej /*
235 1.7 thorpej * Allow mmap access to the legacy ISA hole. This is where
236 1.7 thorpej * the legacy video BIOS will be located, and also where
237 1.7 thorpej * the legacy VGA display buffer is located.
238 1.7 thorpej *
239 1.7 thorpej * XXX Security implications, here?
240 1.7 thorpej */
241 1.7 thorpej if (offset >= IOM_BEGIN && offset < IOM_END)
242 1.7 thorpej return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
243 1.7 thorpej (offset - IOM_BEGIN), prot, 0));
244 1.6 thorpej
245 1.7 thorpej /* Range not found. */
246 1.6 thorpej return (-1);
247 1.1 drochner }
248