xref: /netbsd-src/sys/dev/pci/vga_pci.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: vga_pci.c,v 1.40 2008/03/14 22:12:08 cube Exp $	*/
2 
3 /*
4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5  * All rights reserved.
6  *
7  * Author: Chris G. Demetriou
8  *
9  * Permission to use, copy, modify and distribute this software and
10  * its documentation is hereby granted, provided that both the copyright
11  * notice and this permission notice appear in all copies of the
12  * software, derivative works or modified versions, and any portions
13  * thereof, and that both notices appear in supporting documentation.
14  *
15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18  *
19  * Carnegie Mellon requests users of this software to return to
20  *
21  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22  *  School of Computer Science
23  *  Carnegie Mellon University
24  *  Pittsburgh PA 15213-3890
25  *
26  * any improvements or extensions that they make and grant Carnegie the
27  * rights to redistribute these changes.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vga_pci.c,v 1.40 2008/03/14 22:12:08 cube Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 
39 #include <dev/pci/pcireg.h>
40 #include <dev/pci/pcivar.h>
41 #include <dev/pci/pcidevs.h>
42 #include <dev/pci/pciio.h>
43 
44 #include <dev/ic/mc6845reg.h>
45 #include <dev/ic/pcdisplayvar.h>
46 #include <dev/ic/vgareg.h>
47 #include <dev/ic/vgavar.h>
48 #include <dev/pci/vga_pcivar.h>
49 
50 #include <dev/isa/isareg.h>	/* For legacy VGA address ranges */
51 
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wscons/wsdisplayvar.h>
54 
55 #include "opt_vga.h"
56 
57 #ifdef VGA_POST
58 #include <x86/vga_post.h>
59 #endif
60 
61 #define	NBARS		6	/* number of PCI BARs */
62 
63 struct vga_bar {
64 	bus_addr_t vb_base;
65 	bus_size_t vb_size;
66 	pcireg_t vb_type;
67 	int vb_flags;
68 };
69 
70 struct vga_pci_softc {
71 	struct vga_softc sc_vga;
72 
73 	pci_chipset_tag_t sc_pc;
74 	pcitag_t sc_pcitag;
75 
76 	struct vga_bar sc_bars[NBARS];
77 	struct vga_bar sc_rom;
78 
79 #ifdef VGA_POST
80 	struct vga_post *sc_posth;
81 #endif
82 };
83 
84 static int	vga_pci_match(struct device *, struct cfdata *, void *);
85 static void	vga_pci_attach(struct device *, struct device *, void *);
86 static int	vga_pci_lookup_quirks(struct pci_attach_args *);
87 static bool	vga_pci_resume(device_t dv PMF_FN_PROTO);
88 
89 CFATTACH_DECL_NEW(vga_pci, sizeof(struct vga_pci_softc),
90     vga_pci_match, vga_pci_attach, NULL, NULL);
91 
92 static int	vga_pci_ioctl(void *, u_long, void *, int, struct lwp *);
93 static paddr_t	vga_pci_mmap(void *, off_t, int);
94 
95 static const struct vga_funcs vga_pci_funcs = {
96 	vga_pci_ioctl,
97 	vga_pci_mmap,
98 };
99 
100 static const struct {
101 	int id;
102 	int quirks;
103 } vga_pci_quirks[] = {
104 	{PCI_ID_CODE(PCI_VENDOR_SILMOTION, PCI_PRODUCT_SILMOTION_SM712),
105 	 VGA_QUIRK_NOFASTSCROLL},
106 };
107 
108 static const struct {
109 	int vid;
110 	int quirks;
111 } vga_pci_vquirks[] = {
112 	{PCI_VENDOR_ATI, VGA_QUIRK_ONEFONT},
113 };
114 
115 static int
116 vga_pci_lookup_quirks(struct pci_attach_args *pa)
117 {
118 	int i;
119 
120 	for (i = 0; i < sizeof(vga_pci_quirks) / sizeof (vga_pci_quirks[0]);
121 	     i++) {
122 		if (vga_pci_quirks[i].id == pa->pa_id)
123 			return (vga_pci_quirks[i].quirks);
124 	}
125 	for (i = 0; i < sizeof(vga_pci_vquirks) / sizeof (vga_pci_vquirks[0]);
126 	     i++) {
127 		if (vga_pci_vquirks[i].vid == PCI_VENDOR(pa->pa_id))
128 			return (vga_pci_vquirks[i].quirks);
129 	}
130 	return (0);
131 }
132 
133 static int
134 vga_pci_match(struct device *parent, struct cfdata *match,
135     void *aux)
136 {
137 	struct pci_attach_args *pa = aux;
138 	int potential;
139 
140 	potential = 0;
141 
142 	/*
143 	 * If it's prehistoric/vga or display/vga, we might match.
144 	 * For the console device, this is just a sanity check.
145 	 */
146 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_PREHISTORIC &&
147 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_PREHISTORIC_VGA)
148 		potential = 1;
149 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
150 	     PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA)
151 		potential = 1;
152 
153 	if (!potential)
154 		return (0);
155 
156 	/* check whether it is disabled by firmware */
157 	if ((pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
158 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
159 	    != (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
160 		return (0);
161 
162 	/* If it's the console, we have a winner! */
163 	if (vga_is_console(pa->pa_iot, WSDISPLAY_TYPE_PCIVGA))
164 		return (1);
165 
166 	/*
167 	 * If we might match, make sure that the card actually looks OK.
168 	 */
169 	if (!vga_common_probe(pa->pa_iot, pa->pa_memt))
170 		return (0);
171 
172 	return (1);
173 }
174 
175 static void
176 vga_pci_attach(struct device *parent, struct device *self, void *aux)
177 {
178 	struct vga_pci_softc *psc = device_private(self);
179 	struct vga_softc *sc = &psc->sc_vga;
180 	struct pci_attach_args *pa = aux;
181 	char devinfo[256];
182 	int bar, reg;
183 
184 	sc->sc_dev = self;
185 	psc->sc_pc = pa->pa_pc;
186 	psc->sc_pcitag = pa->pa_tag;
187 
188 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
189 	aprint_naive("\n");
190 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
191 	    PCI_REVISION(pa->pa_class));
192 
193 	/*
194 	 * Gather info about all the BARs.  These are used to allow
195 	 * the X server to map the VGA device.
196 	 */
197 	for (bar = 0; bar < NBARS; bar++) {
198 		reg = PCI_MAPREG_START + (bar * 4);
199 		if (!pci_mapreg_probe(psc->sc_pc, psc->sc_pcitag, reg,
200 				      &psc->sc_bars[bar].vb_type)) {
201 			/* there is no valid mapping register */
202 			continue;
203 		}
204 		if (PCI_MAPREG_TYPE(psc->sc_bars[bar].vb_type) ==
205 		    PCI_MAPREG_TYPE_IO) {
206 			/* Don't bother fetching I/O BARs. */
207 			continue;
208 		}
209 #ifndef __LP64__
210 		if (PCI_MAPREG_MEM_TYPE(psc->sc_bars[bar].vb_type) ==
211 		    PCI_MAPREG_MEM_TYPE_64BIT) {
212 			/* XXX */
213 			aprint_error_dev(self,
214 			    "WARNING: ignoring 64-bit BAR @ 0x%02x\n", reg);
215 			bar++;
216 			continue;
217 		}
218 #endif
219 		if (pci_mapreg_info(psc->sc_pc, psc->sc_pcitag, reg,
220 		     psc->sc_bars[bar].vb_type,
221 		     &psc->sc_bars[bar].vb_base,
222 		     &psc->sc_bars[bar].vb_size,
223 		     &psc->sc_bars[bar].vb_flags))
224 			aprint_error_dev(self,
225 			    "WARNING: strange BAR @ 0x%02x\n", reg);
226 	}
227 
228 	/* XXX Expansion ROM? */
229 
230 	vga_common_attach(sc, pa->pa_iot, pa->pa_memt, WSDISPLAY_TYPE_PCIVGA,
231 			  vga_pci_lookup_quirks(pa), &vga_pci_funcs);
232 
233 #ifdef VGA_POST
234 	psc->sc_posth = vga_post_init(pa->pa_bus, pa->pa_device, pa->pa_function);
235 	if (psc->sc_posth == NULL)
236 		aprint_error_dev(self, "WARNING: could not prepare POST handler\n");
237 #endif
238 
239 	/*
240 	 * XXX Do not use the generic PCI framework for now as
241 	 * XXX it would power down the device when the console
242 	 * XXX is still using it.
243 	 */
244 	if (!pmf_device_register(self, NULL, vga_pci_resume))
245 		aprint_error_dev(self, "couldn't establish power handler\n");
246 	config_found_ia(self, "drm", aux, vga_drm_print);
247 }
248 
249 static bool
250 vga_pci_resume(device_t dv PMF_FN_ARGS)
251 {
252 	struct vga_pci_softc *sc = device_private(dv);
253 
254 	vga_resume(&sc->sc_vga);
255 
256 #ifdef VGA_POST
257 	if (sc->sc_posth != NULL)
258 		vga_post_call(sc->sc_posth);
259 #endif
260 
261 	return true;
262 }
263 
264 int
265 vga_pci_cnattach(bus_space_tag_t iot, bus_space_tag_t memt,
266     pci_chipset_tag_t pc, int bus, int device,
267     int function)
268 {
269 
270 	return (vga_cnattach(iot, memt, WSDISPLAY_TYPE_PCIVGA, 0));
271 }
272 
273 int
274 vga_drm_print(void *aux, const char *pnp)
275 {
276 	if (pnp)
277 		aprint_normal("direct rendering for %s", pnp);
278 	return (UNSUPP);
279 }
280 
281 
282 static int
283 vga_pci_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
284 {
285 	struct vga_config *vc = v;
286 	struct vga_pci_softc *psc = (void *) vc->softc;
287 
288 	switch (cmd) {
289 	/* PCI config read/write passthrough. */
290 	case PCI_IOC_CFGREAD:
291 	case PCI_IOC_CFGWRITE:
292 		return (pci_devioctl(psc->sc_pc, psc->sc_pcitag,
293 		    cmd, data, flag, l));
294 
295 	default:
296 		return (EPASSTHROUGH);
297 	}
298 }
299 
300 static paddr_t
301 vga_pci_mmap(void *v, off_t offset, int prot)
302 {
303 	struct vga_config *vc = v;
304 	struct vga_pci_softc *psc = (void *) vc->softc;
305 	struct vga_bar *vb;
306 	int bar;
307 
308 	for (bar = 0; bar < NBARS; bar++) {
309 		vb = &psc->sc_bars[bar];
310 		if (vb->vb_size == 0)
311 			continue;
312 		if (offset >= vb->vb_base &&
313 		    offset < (vb->vb_base + vb->vb_size)) {
314 			/* XXX This the right thing to do with flags? */
315 			return (bus_space_mmap(vc->hdl.vh_memt, vb->vb_base,
316 			    (offset - vb->vb_base), prot, vb->vb_flags));
317 		}
318 	}
319 
320 	/* XXX Expansion ROM? */
321 
322 	/*
323 	 * Allow mmap access to the legacy ISA hole.  This is where
324 	 * the legacy video BIOS will be located, and also where
325 	 * the legacy VGA display buffer is located.
326 	 *
327 	 * XXX Security implications, here?
328 	 */
329 	if (offset >= IOM_BEGIN && offset < IOM_END)
330 		return (bus_space_mmap(vc->hdl.vh_memt, IOM_BEGIN,
331 		    (offset - IOM_BEGIN), prot, 0));
332 
333 	/* Range not found. */
334 	return (-1);
335 }
336