vga_pci.c revision 1.4.6.1 1 1.4.6.1 fvdl /* $NetBSD: vga_pci.c,v 1.4.6.1 2001/10/01 12:46:04 fvdl 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.1 drochner
30 1.1 drochner #include <sys/param.h>
31 1.1 drochner #include <sys/systm.h>
32 1.1 drochner #include <sys/kernel.h>
33 1.1 drochner #include <sys/device.h>
34 1.1 drochner #include <sys/malloc.h>
35 1.1 drochner
36 1.1 drochner #include <dev/pci/pcireg.h>
37 1.1 drochner #include <dev/pci/pcivar.h>
38 1.1 drochner #include <dev/pci/pcidevs.h>
39 1.4.6.1 fvdl #include <dev/pci/pciio.h>
40 1.1 drochner
41 1.2 drochner #include <dev/ic/mc6845reg.h>
42 1.2 drochner #include <dev/ic/pcdisplayvar.h>
43 1.1 drochner #include <dev/ic/vgareg.h>
44 1.1 drochner #include <dev/ic/vgavar.h>
45 1.1 drochner #include <dev/pci/vga_pcivar.h>
46 1.1 drochner
47 1.4.6.1 fvdl #include <dev/isa/isareg.h> /* For legacy VGA address ranges */
48 1.4.6.1 fvdl
49 1.1 drochner #include <dev/wscons/wsconsio.h>
50 1.1 drochner #include <dev/wscons/wsdisplayvar.h>
51 1.1 drochner
52 1.4.6.1 fvdl #define NBARS 6 /* number of PCI BARs */
53 1.4.6.1 fvdl
54 1.4.6.1 fvdl struct vga_bar {
55 1.4.6.1 fvdl bus_addr_t vb_base;
56 1.4.6.1 fvdl bus_size_t vb_size;
57 1.4.6.1 fvdl pcireg_t vb_type;
58 1.4.6.1 fvdl int vb_flags;
59 1.4.6.1 fvdl };
60 1.4.6.1 fvdl
61 1.1 drochner struct vga_pci_softc {
62 1.4.6.1 fvdl struct vga_softc sc_vga;
63 1.4.6.1 fvdl
64 1.4.6.1 fvdl pci_chipset_tag_t sc_pc;
65 1.4.6.1 fvdl pcitag_t sc_pcitag;
66 1.4.6.1 fvdl
67 1.4.6.1 fvdl struct vga_bar sc_bars[NBARS];
68 1.4.6.1 fvdl struct vga_bar sc_rom;
69 1.1 drochner };
70 1.1 drochner
71 1.1 drochner int vga_pci_match __P((struct device *, struct cfdata *, void *));
72 1.1 drochner void vga_pci_attach __P((struct device *, struct device *, void *));
73 1.1 drochner
74 1.1 drochner struct cfattach vga_pci_ca = {
75 1.1 drochner sizeof(struct vga_pci_softc), vga_pci_match, vga_pci_attach,
76 1.1 drochner };
77 1.1 drochner
78 1.4.6.1 fvdl int vga_pci_ioctl(void *, u_long, caddr_t, int, struct proc *);
79 1.4.6.1 fvdl paddr_t vga_pci_mmap(void *, off_t, int);
80 1.4.6.1 fvdl
81 1.4.6.1 fvdl const struct vga_funcs vga_pci_funcs = {
82 1.4.6.1 fvdl vga_pci_ioctl,
83 1.4.6.1 fvdl vga_pci_mmap,
84 1.4.6.1 fvdl };
85 1.4.6.1 fvdl
86 1.1 drochner int
87 1.1 drochner vga_pci_match(parent, match, aux)
88 1.1 drochner struct device *parent;
89 1.1 drochner struct cfdata *match;
90 1.1 drochner void *aux;
91 1.1 drochner {
92 1.1 drochner struct pci_attach_args *pa = aux;
93 1.1 drochner int potential;
94 1.1 drochner
95 1.1 drochner potential = 0;
96 1.1 drochner
97 1.1 drochner /*
98 1.1 drochner * If it's prehistoric/vga or display/vga, we might match.
99 1.1 drochner * For the console device, this is jut a sanity check.
100 1.1 drochner */
101 1.1 drochner if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
102 1.1 drochner PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
103 1.1 drochner potential = 1;
104 1.1 drochner if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
105 1.1 drochner PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
106 1.1 drochner potential = 1;
107 1.1 drochner
108 1.1 drochner if (!potential)
109 1.1 drochner return (0);
110 1.1 drochner
111 1.1 drochner /* check whether it is disabled by firmware */
112 1.1 drochner if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
113 1.1 drochner & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
114 1.1 drochner != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
115 1.1 drochner return (0);
116 1.1 drochner
117 1.1 drochner /* If it's the console, we have a winner! */
118 1.1 drochner if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
119 1.1 drochner return (1);
120 1.1 drochner
121 1.1 drochner /*
122 1.1 drochner * If we might match, make sure that the card actually looks OK.
123 1.1 drochner */
124 1.1 drochner if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
125 1.1 drochner return (0);
126 1.1 drochner
127 1.1 drochner return (1);
128 1.1 drochner }
129 1.1 drochner
130 1.1 drochner void
131 1.1 drochner vga_pci_attach(parent, self, aux)
132 1.1 drochner struct device *parent, *self;
133 1.1 drochner void *aux;
134 1.1 drochner {
135 1.4.6.1 fvdl struct vga_pci_softc *psc = (void *) self;
136 1.4.6.1 fvdl struct vga_softc *sc = &psc->sc_vga;
137 1.1 drochner struct pci_attach_args *pa = aux;
138 1.1 drochner char devinfo[256];
139 1.4.6.1 fvdl int bar, reg;
140 1.1 drochner
141 1.4.6.1 fvdl psc->sc_pc = pa->pa_pc;
142 1.4.6.1 fvdl psc->sc_pcitag = pa->pa_tag;
143 1.1 drochner
144 1.1 drochner pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
145 1.1 drochner printf(": %s (rev. 0x%02x)\n", devinfo,
146 1.1 drochner PCI_REVISION(pa->pa_class));
147 1.1 drochner
148 1.4.6.1 fvdl /*
149 1.4.6.1 fvdl * Gather info about all the BARs. These are used to allow
150 1.4.6.1 fvdl * the X server to map the VGA device.
151 1.4.6.1 fvdl */
152 1.4.6.1 fvdl for (bar = 0; bar < NBARS; bar++) {
153 1.4.6.1 fvdl reg = PCI_MAPREG_START + (bar * 4);
154 1.4.6.1 fvdl psc->sc_bars[bar].vb_type = pci_mapreg_type(psc->sc_pc,
155 1.4.6.1 fvdl psc->sc_pcitag, reg);
156 1.4.6.1 fvdl if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
157 1.4.6.1 fvdl PCI_MAPREG_TYPE_IO) {
158 1.4.6.1 fvdl /* Don't bother fetching I/O BARs. */
159 1.4.6.1 fvdl continue;
160 1.4.6.1 fvdl }
161 1.4.6.1 fvdl if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
162 1.4.6.1 fvdl PCI_MAPREG_MEM_TYPE_64BIT) {
163 1.4.6.1 fvdl /* XXX */
164 1.4.6.1 fvdl printf("%s: WARNING: ignoring 64-bit BAR @ 0x%02x\n",
165 1.4.6.1 fvdl sc->sc_dev.dv_xname, reg);
166 1.4.6.1 fvdl continue;
167 1.4.6.1 fvdl }
168 1.4.6.1 fvdl /* Ignore errors (unimplemented BARs). */
169 1.4.6.1 fvdl (void) pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
170 1.4.6.1 fvdl psc->sc_bars[bar].vb_type,
171 1.4.6.1 fvdl &psc->sc_bars[bar].vb_base,
172 1.4.6.1 fvdl &psc->sc_bars[bar].vb_size,
173 1.4.6.1 fvdl &psc->sc_bars[bar].vb_flags);
174 1.4.6.1 fvdl }
175 1.4.6.1 fvdl
176 1.4.6.1 fvdl /* XXX Expansion ROM? */
177 1.4.6.1 fvdl
178 1.4.6.1 fvdl vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
179 1.4.6.1 fvdl &vga_pci_funcs);
180 1.1 drochner }
181 1.1 drochner
182 1.1 drochner int
183 1.1 drochner vga_pci_cnattach(iot, memt, pc, bus, device, function)
184 1.1 drochner bus_space_tag_t iot, memt;
185 1.1 drochner pci_chipset_tag_t pc;
186 1.1 drochner int bus, device, function;
187 1.1 drochner {
188 1.1 drochner return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
189 1.4.6.1 fvdl }
190 1.4.6.1 fvdl
191 1.4.6.1 fvdl int
192 1.4.6.1 fvdl vga_pci_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
193 1.4.6.1 fvdl {
194 1.4.6.1 fvdl struct vga_config *vc = v;
195 1.4.6.1 fvdl struct vga_pci_softc *psc = (void *) vc->softc;
196 1.4.6.1 fvdl
197 1.4.6.1 fvdl switch (cmd) {
198 1.4.6.1 fvdl /* PCI config read/write passthrough. */
199 1.4.6.1 fvdl case PCI_IOC_CFGREAD:
200 1.4.6.1 fvdl case PCI_IOC_CFGWRITE:
201 1.4.6.1 fvdl return (pci_devioctl(psc->sc_pc, psc->sc_pcitag,
202 1.4.6.1 fvdl cmd, data, flag, p));
203 1.4.6.1 fvdl
204 1.4.6.1 fvdl default:
205 1.4.6.1 fvdl return (ENOTTY);
206 1.4.6.1 fvdl }
207 1.4.6.1 fvdl }
208 1.4.6.1 fvdl
209 1.4.6.1 fvdl paddr_t
210 1.4.6.1 fvdl vga_pci_mmap(void *v, off_t offset, int prot)
211 1.4.6.1 fvdl {
212 1.4.6.1 fvdl struct vga_config *vc = v;
213 1.4.6.1 fvdl struct vga_pci_softc *psc = (void *) vc->softc;
214 1.4.6.1 fvdl struct vga_bar *vb;
215 1.4.6.1 fvdl int bar;
216 1.4.6.1 fvdl
217 1.4.6.1 fvdl for (bar = 0; bar < NBARS; bar++) {
218 1.4.6.1 fvdl vb = &psc->sc_bars[bar];
219 1.4.6.1 fvdl if (vb->vb_size == 0)
220 1.4.6.1 fvdl continue;
221 1.4.6.1 fvdl if (offset >= vb->vb_base &&
222 1.4.6.1 fvdl offset < (vb->vb_base + vb->vb_size)) {
223 1.4.6.1 fvdl /* XXX This the right thing to do with flags? */
224 1.4.6.1 fvdl return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
225 1.4.6.1 fvdl (offset - vb->vb_base), prot, vb->vb_flags));
226 1.4.6.1 fvdl }
227 1.4.6.1 fvdl }
228 1.4.6.1 fvdl
229 1.4.6.1 fvdl /* XXX Expansion ROM? */
230 1.4.6.1 fvdl
231 1.4.6.1 fvdl /*
232 1.4.6.1 fvdl * Allow mmap access to the legacy ISA hole. This is where
233 1.4.6.1 fvdl * the legacy video BIOS will be located, and also where
234 1.4.6.1 fvdl * the legacy VGA display buffer is located.
235 1.4.6.1 fvdl *
236 1.4.6.1 fvdl * XXX Security implications, here?
237 1.4.6.1 fvdl */
238 1.4.6.1 fvdl if (offset >= IOM_BEGIN && offset < IOM_END)
239 1.4.6.1 fvdl return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
240 1.4.6.1 fvdl (offset - IOM_BEGIN), prot, 0));
241 1.4.6.1 fvdl
242 1.4.6.1 fvdl /* Range not found. */
243 1.4.6.1 fvdl return (-1);
244 1.1 drochner }
245