tdfx_pci.c revision 191056
1214501Srpaulo/*-
2214501Srpaulo * Copyright (c) 2000-2001 by Coleman Kane <cokane@FreeBSD.org>
3214501Srpaulo * All rights reserved.
4214501Srpaulo *
5252726Srpaulo * Redistribution and use in source and binary forms, with or without
6252726Srpaulo * modification, are permitted provided that the following conditions
7214501Srpaulo * are met:
8214501Srpaulo * 1. Redistributions of source code must retain the above copyright
9214501Srpaulo *    notice, this list of conditions and the following disclaimer.
10214501Srpaulo * 2. Redistributions in binary form must reproduce the above copyright
11214501Srpaulo *    notice, this list of conditions and the following disclaimer in the
12214501Srpaulo *    documentation and/or other materials provided with the distribution.
13214501Srpaulo * 3. All advertising materials mentioning features or use of this software
14214501Srpaulo *    must display the following acknowledgement:
15214501Srpaulo *      This product includes software developed by Gardner Buchanan.
16214501Srpaulo * 4. The name of Gardner Buchanan may not be used to endorse or promote
17214501Srpaulo *    products derived from this software without specific prior written
18214501Srpaulo *    permission.
19214501Srpaulo *
20214501Srpaulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21214501Srpaulo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22214501Srpaulo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23214501Srpaulo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24214501Srpaulo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25214501Srpaulo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26214501Srpaulo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27214501Srpaulo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28214501Srpaulo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29214501Srpaulo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30214501Srpaulo */
31214501Srpaulo
32214501Srpaulo#include <sys/cdefs.h>
33214501Srpaulo__FBSDID("$FreeBSD: head/sys/dev/tdfx/tdfx_pci.c 191056 2009-04-14 11:57:26Z ed $");
34214501Srpaulo
35214501Srpaulo/* 3dfx driver for FreeBSD 4.x - Finished 11 May 2000, 12:25AM ET
36214501Srpaulo *
37214501Srpaulo * Copyright (C) 2000-2001, by Coleman Kane <cokane@FreeBSD.org>,
38214501Srpaulo * based upon the 3dfx driver written for linux, by Daryll Straus, Jon Taylor,
39214501Srpaulo * and Jens Axboe, located at http://linux.3dfx.com.
40214501Srpaulo */
41214501Srpaulo
42214501Srpaulo#include <sys/param.h>
43214501Srpaulo
44214501Srpaulo#include <sys/bus.h>
45214501Srpaulo#include <sys/cdefs.h>
46214501Srpaulo#include <sys/conf.h>
47214501Srpaulo#include <sys/fcntl.h>
48214501Srpaulo#include <sys/file.h>
49214501Srpaulo#include <sys/filedesc.h>
50214501Srpaulo#include <sys/filio.h>
51214501Srpaulo#include <sys/ioccom.h>
52214501Srpaulo#include <sys/kernel.h>
53214501Srpaulo#include <sys/module.h>
54214501Srpaulo#include <sys/malloc.h>
55214501Srpaulo#include <sys/mman.h>
56214501Srpaulo#include <sys/signalvar.h>
57214501Srpaulo#include <sys/systm.h>
58214501Srpaulo#include <sys/uio.h>
59214501Srpaulo
60214501Srpaulo#include <dev/pci/pcivar.h>
61214501Srpaulo#include <dev/pci/pcireg.h>
62214501Srpaulo
63214501Srpaulo#include <vm/vm.h>
64214501Srpaulo#include <vm/vm_kern.h>
65214501Srpaulo#include <vm/pmap.h>
66214501Srpaulo#include <vm/vm_extern.h>
67214501Srpaulo
68214501Srpaulo/* rman.h depends on machine/bus.h */
69214501Srpaulo#include <machine/resource.h>
70214501Srpaulo#include <machine/bus.h>
71214501Srpaulo#include <sys/rman.h>
72214501Srpaulo
73214501Srpaulo#include <dev/tdfx/tdfx_io.h>
74214501Srpaulo#include <dev/tdfx/tdfx_vars.h>
75214501Srpaulo#include <dev/tdfx/tdfx_pci.h>
76214501Srpaulo
77214501Srpaulo
78214501Srpaulostatic devclass_t tdfx_devclass;
79214501Srpaulo
80214501Srpaulo
81214501Srpaulostatic int tdfx_count = 0;
82214501Srpaulo
83214501Srpaulo
84214501Srpaulo/* Set up the boot probe/attach routines */
85214501Srpaulostatic device_method_t tdfx_methods[] = {
86214501Srpaulo	DEVMETHOD(device_probe,		tdfx_probe),
87214501Srpaulo	DEVMETHOD(device_attach,	tdfx_attach),
88214501Srpaulo	DEVMETHOD(device_detach,	tdfx_detach),
89214501Srpaulo	DEVMETHOD(device_shutdown,	tdfx_shutdown),
90214501Srpaulo	{ 0, 0 }
91214501Srpaulo};
92214501Srpaulo
93214501SrpauloMALLOC_DEFINE(M_TDFX,"tdfx_driver","3DFX Graphics[/2D]/3D Accelerator(s)");
94214501Srpaulo
95214501Srpaulo/* Char. Dev. file operations structure */
96214501Srpaulostatic struct cdevsw tdfx_cdev = {
97214501Srpaulo	.d_version =	D_VERSION,
98214501Srpaulo	.d_flags =	D_NEEDGIANT,
99214501Srpaulo	.d_open =	tdfx_open,
100214501Srpaulo	.d_close =	tdfx_close,
101214501Srpaulo	.d_ioctl =	tdfx_ioctl,
102214501Srpaulo	.d_mmap =	tdfx_mmap,
103214501Srpaulo	.d_name =	"tdfx",
104214501Srpaulo};
105214501Srpaulo
106214501Srpaulostatic int
107214501Srpaulotdfx_probe(device_t dev)
108214501Srpaulo{
109214501Srpaulo	/*
110214501Srpaulo	 * probe routine called on kernel boot to register supported devices. We get
111214501Srpaulo	 * a device structure to work with, and we can test the VENDOR/DEVICE IDs to
112214501Srpaulo	 * see if this PCI device is one that we support. Return BUS_PRROBE_DEFAULT
113214501Srpaulo	 * if yes, ENXIO if not.
114214501Srpaulo	 */
115214501Srpaulo	switch(pci_get_devid(dev)) {
116214501Srpaulo	case PCI_DEVICE_ALLIANCE_AT3D:
117214501Srpaulo		device_set_desc(dev, "ProMotion At3D 3D Accelerator");
118214501Srpaulo		return BUS_PROBE_DEFAULT;
119214501Srpaulo	case PCI_DEVICE_3DFX_VOODOO2:
120214501Srpaulo		device_set_desc(dev, "3DFX Voodoo II 3D Accelerator");
121214501Srpaulo		return BUS_PROBE_DEFAULT;
122214501Srpaulo	/*case PCI_DEVICE_3DFX_BANSHEE:
123214501Srpaulo		device_set_desc(dev, "3DFX Voodoo Banshee 2D/3D Graphics Accelerator");
124214501Srpaulo		return BUS_PROBE_DEFAULT;
125214501Srpaulo	case PCI_DEVICE_3DFX_VOODOO3:
126214501Srpaulo		device_set_desc(dev, "3DFX Voodoo3 2D/3D Graphics Accelerator");
127214501Srpaulo		return BUS_PROBE_DEFAULT;*/
128214501Srpaulo	case PCI_DEVICE_3DFX_VOODOO1:
129214501Srpaulo		device_set_desc(dev, "3DFX Voodoo Graphics 3D Accelerator");
130214501Srpaulo		return BUS_PROBE_DEFAULT;
131214501Srpaulo	};
132214501Srpaulo
133214501Srpaulo	return ENXIO;
134214501Srpaulo}
135214501Srpaulo
136214501Srpaulostatic int
137214501Srpaulotdfx_attach(device_t dev) {
138214501Srpaulo	/*
139214501Srpaulo	 * The attach routine is called after the probe routine successfully says it
140214501Srpaulo	 * supports a given card. We now proceed to initialize this card for use with
141214501Srpaulo	 * the system. I want to map the device memory for userland allocation and
142214501Srpaulo	 * fill an information structure with information on this card. I'd also like
143214501Srpaulo	 * to set Write Combining with the MTRR code so that we can hopefully speed
144214501Srpaulo	 * up memory writes. The last thing is to register the character device
145214501Srpaulo	 * interface to the card, so we can open it from /dev/3dfxN, where N is a
146214501Srpaulo	 * small, whole number.
147214501Srpaulo	 */
148214501Srpaulo	struct tdfx_softc *tdfx_info;
149214501Srpaulo	u_long	val;
150214501Srpaulo	/* rid value tells bus_alloc_resource where to find the addresses of ports or
151214501Srpaulo	 * of memory ranges in the PCI config space*/
152214501Srpaulo	int rid = PCIR_BAR(0);
153214501Srpaulo
154214501Srpaulo	/* Increment the card counter (for the ioctl code) */
155214501Srpaulo	tdfx_count++;
156214501Srpaulo
157214501Srpaulo 	/* Enable MemMap on Voodoo */
158214501Srpaulo	val = pci_read_config(dev, PCIR_COMMAND, 2);
159214501Srpaulo	val |= (PCIM_CMD_MEMEN);
160214501Srpaulo	pci_write_config(dev, PCIR_COMMAND, val, 2);
161214501Srpaulo	val = pci_read_config(dev, PCIR_COMMAND, 2);
162214501Srpaulo
163214501Srpaulo	/* Fill the soft config struct with info about this device*/
164214501Srpaulo	tdfx_info = device_get_softc(dev);
165214501Srpaulo	tdfx_info->dev = dev;
166214501Srpaulo	tdfx_info->vendor = pci_get_vendor(dev);
167214501Srpaulo	tdfx_info->type = pci_get_devid(dev) >> 16;
168214501Srpaulo	tdfx_info->bus = pci_get_bus(dev);
169214501Srpaulo	tdfx_info->dv = pci_get_slot(dev);
170214501Srpaulo	tdfx_info->curFile = NULL;
171214501Srpaulo
172214501Srpaulo	/*
173214501Srpaulo	 *	Get the Memory Location from the PCI Config, mask out lower word, since
174214501Srpaulo	 * the config space register is only one word long (this is nicer than a
175214501Srpaulo	 * bitshift).
176214501Srpaulo	 */
177214501Srpaulo	tdfx_info->addr0 = (pci_read_config(dev, 0x10, 4) & 0xffff0000);
178214501Srpaulo#ifdef DEBUG
179214501Srpaulo	device_printf(dev, "Base0 @ 0x%x\n", tdfx_info->addr0);
180214501Srpaulo#endif
181214501Srpaulo	/* Notify the VM that we will be mapping some memory later */
182214501Srpaulo	tdfx_info->memrange = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
183214501Srpaulo		&rid, RF_ACTIVE | RF_SHAREABLE);
184214501Srpaulo	if(tdfx_info->memrange == NULL) {
185214501Srpaulo#ifdef DEBUG
186214501Srpaulo		device_printf(dev, "Error mapping mem, won't be able to use mmap()\n");
187214501Srpaulo#endif
188214501Srpaulo		tdfx_info->memrid = 0;
189214501Srpaulo	}
190214501Srpaulo	else {
191214501Srpaulo		tdfx_info->memrid = rid;
192214501Srpaulo#ifdef DEBUG
193214501Srpaulo		device_printf(dev, "Mapped to: 0x%x\n",
194214501Srpaulo				(unsigned int)rman_get_start(tdfx_info->memrange));
195214501Srpaulo#endif
196214501Srpaulo	}
197214501Srpaulo
198214501Srpaulo	/* Setup for Voodoo3 and Banshee, PIO and an extram Memrange */
199214501Srpaulo	if(pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3 ||
200214501Srpaulo		pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE) {
201214501Srpaulo		rid = 0x14;	/* 2nd mem map */
202214501Srpaulo		tdfx_info->addr1 = (pci_read_config(dev, 0x14, 4) & 0xffff0000);
203214501Srpaulo#ifdef DEBUG
204214501Srpaulo		device_printf(dev, "Base1 @ 0x%x\n", tdfx_info->addr1);
205214501Srpaulo#endif
206214501Srpaulo		tdfx_info->memrange2 = bus_alloc_resource_any(dev,
207214501Srpaulo			SYS_RES_MEMORY, &rid, RF_ACTIVE | RF_SHAREABLE);
208214501Srpaulo		if(tdfx_info->memrange2 == NULL) {
209214501Srpaulo#ifdef DEBUG
210214501Srpaulo			device_printf(dev, "Mem1 couldn't be allocated, glide may not work.");
211214501Srpaulo#endif
212214501Srpaulo			tdfx_info->memrid2 = 0;
213214501Srpaulo		}
214214501Srpaulo		else {
215214501Srpaulo			tdfx_info->memrid2 = rid;
216214501Srpaulo		}
217214501Srpaulo		/* Now to map the PIO stuff */
218214501Srpaulo		rid = PCIR_IOBASE0_2;
219214501Srpaulo		tdfx_info->pio0 = pci_read_config(dev, 0x2c, 2);
220214501Srpaulo		tdfx_info->pio0max = pci_read_config(dev, 0x30, 2) + tdfx_info->pio0;
221214501Srpaulo		tdfx_info->piorange = bus_alloc_resource_any(dev,
222214501Srpaulo			SYS_RES_IOPORT, &rid, RF_ACTIVE | RF_SHAREABLE);
223214501Srpaulo		if(tdfx_info->piorange == NULL) {
224214501Srpaulo#ifdef DEBUG
225214501Srpaulo			device_printf(dev, "Couldn't map PIO range.");
226214501Srpaulo#endif
227214501Srpaulo			tdfx_info->piorid = 0;
228214501Srpaulo		}
229214501Srpaulo		else {
230214501Srpaulo			tdfx_info->piorid = rid;
231214501Srpaulo		}
232214501Srpaulo	} else {
233214501Srpaulo	  tdfx_info->addr1 = 0;
234214501Srpaulo	  tdfx_info->memrange2 = NULL;
235214501Srpaulo	  tdfx_info->piorange = NULL;
236214501Srpaulo	}
237214501Srpaulo
238214501Srpaulo	/*
239214501Srpaulo	 *	Set Writecombining, or at least Uncacheable for the memory region, if we
240214501Srpaulo	 * are able to
241214501Srpaulo	 */
242214501Srpaulo
243214501Srpaulo	if(tdfx_setmtrr(dev) != 0) {
244214501Srpaulo#ifdef DEBUG
245214501Srpaulo		device_printf(dev, "Some weird error setting MTRRs");
246214501Srpaulo#endif
247214501Srpaulo		return -1;
248214501Srpaulo	}
249214501Srpaulo
250214501Srpaulo	/*
251214501Srpaulo	 * make_dev registers the cdev to access the 3dfx card from /dev
252214501Srpaulo	 *	use hex here for the dev num, simply to provide better support if > 10
253214501Srpaulo	 * voodoo cards, for the mad. The user must set the link, or use MAKEDEV.
254214501Srpaulo	 * Why would we want that many voodoo cards anyhow?
255214501Srpaulo	 */
256214501Srpaulo	tdfx_info->devt = make_dev(&tdfx_cdev, device_get_unit(dev),
257214501Srpaulo		UID_ROOT, GID_WHEEL, 0600, "3dfx%x", device_get_unit(dev));
258214501Srpaulo	tdfx_info->devt->si_drv1 = tdfx_info;
259214501Srpaulo
260214501Srpaulo	return 0;
261214501Srpaulo}
262214501Srpaulo
263214501Srpaulostatic int
264214501Srpaulotdfx_detach(device_t dev) {
265214501Srpaulo	struct tdfx_softc* tdfx_info;
266214501Srpaulo	int retval;
267214501Srpaulo	tdfx_info = device_get_softc(dev);
268214501Srpaulo
269214501Srpaulo	/* Delete allocated resource, of course */
270214501Srpaulo	bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid,
271214501Srpaulo			tdfx_info->memrange);
272214501Srpaulo
273214501Srpaulo	/* Release extended Voodoo3/Banshee resources */
274214501Srpaulo	if(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE ||
275214501Srpaulo			pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) {
276214501Srpaulo		if(tdfx_info->memrange2 != NULL)
277214501Srpaulo			bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid2,
278214501Srpaulo				tdfx_info->memrange);
279214501Srpaulo	/*	if(tdfx_info->piorange != NULL)
280214501Srpaulo			bus_release_resource(dev, SYS_RES_IOPORT, tdfx_info->piorid,
281214501Srpaulo				tdfx_info->piorange);*/
282214501Srpaulo	}
283214501Srpaulo
284214501Srpaulo	/* Though it is safe to leave the WRCOMB support since the
285214501Srpaulo		mem driver checks for it, we should remove it in order
286214501Srpaulo		to free an MTRR for another device */
287214501Srpaulo	retval = tdfx_clrmtrr(dev);
288214501Srpaulo#ifdef DEBUG
289214501Srpaulo	if(retval != 0)
290214501Srpaulo		printf("tdfx: For some reason, I couldn't clear the mtrr\n");
291214501Srpaulo#endif
292214501Srpaulo	/* Remove device entry when it can no longer be accessed */
293214501Srpaulo   destroy_dev(tdfx_info->devt);
294214501Srpaulo	return(0);
295214501Srpaulo}
296214501Srpaulo
297214501Srpaulostatic int
298214501Srpaulotdfx_shutdown(device_t dev) {
299214501Srpaulo#ifdef DEBUG
300214501Srpaulo	device_printf(dev, "tdfx: Device Shutdown\n");
301214501Srpaulo#endif
302214501Srpaulo	return 0;
303214501Srpaulo}
304214501Srpaulo
305214501Srpaulostatic int
306214501Srpaulotdfx_clrmtrr(device_t dev) {
307214501Srpaulo	/* This function removes the MTRR set by the attach call, so it can be used
308214501Srpaulo	 * in the future by other drivers.
309214501Srpaulo	 */
310214501Srpaulo	int retval, act;
311214501Srpaulo	struct tdfx_softc *tdfx_info = device_get_softc(dev);
312214501Srpaulo
313214501Srpaulo	act = MEMRANGE_SET_REMOVE;
314214501Srpaulo	retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
315214501Srpaulo	return retval;
316214501Srpaulo}
317214501Srpaulo
318214501Srpaulostatic int
319214501Srpaulotdfx_setmtrr(device_t dev) {
320214501Srpaulo	/*
321214501Srpaulo	 * This is the MTRR setting function for the 3dfx card. It is called from
322214501Srpaulo	 * tdfx_attach. If we can't set the MTRR properly, it's not the end of the
323214501Srpaulo	 * world. We can still continue, just with slightly (very slightly) degraded
324	 * performance.
325	 */
326	int retval = 0, act;
327	struct tdfx_softc *tdfx_info = device_get_softc(dev);
328
329	/* The older Voodoo cards have a shorter memrange than the newer ones */
330	if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1) || (pci_get_devid(dev) ==
331			PCI_DEVICE_3DFX_VOODOO2)) {
332		tdfx_info->mrdesc.mr_len = 0x400000;
333
334		/* The memory descriptor is described as the top 15 bits of the real
335			address */
336		tdfx_info->mrdesc.mr_base = tdfx_info->addr0 & 0xfffe0000;
337	}
338	else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) ||
339			(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE)) {
340		tdfx_info->mrdesc.mr_len = 0x1000000;
341		/* The Voodoo3 and Banshee LFB is the second memory address */
342		/* The memory descriptor is described as the top 15 bits of the real
343			address */
344		tdfx_info->mrdesc.mr_base = tdfx_info->addr1 & 0xfffe0000;
345	}
346	else
347		 return 0;
348	/*
349    *	The Alliance Pro Motion AT3D was not mentioned in the linux
350	 * driver as far as MTRR support goes, so I just won't put the
351	 * code in here for it. This is where it should go, though.
352	 */
353
354	/* Firstly, try to set write combining */
355	tdfx_info->mrdesc.mr_flags = MDF_WRITECOMBINE;
356	bcopy("tdfx", &tdfx_info->mrdesc.mr_owner, 4);
357	act = MEMRANGE_SET_UPDATE;
358	retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
359
360	if(retval == 0) {
361#ifdef DEBUG
362		device_printf(dev, "MTRR Set Correctly for tdfx\n");
363#endif
364	} else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO2) ||
365		(pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1)) {
366		/* if, for some reason we can't set the WRCOMB range with the V1/V2, we
367		 * can still possibly use the UNCACHEABLE region for it instead, and help
368		 * out in a small way */
369		tdfx_info->mrdesc.mr_flags = MDF_UNCACHEABLE;
370		/* This length of 1000h was taken from the linux device driver... */
371		tdfx_info->mrdesc.mr_len = 0x1000;
372
373		/*
374		 * If, for some reason, we can't set the MTRR (N/A?) we may still continue
375		 */
376#ifdef DEBUG
377		device_printf(dev, "MTRR Set Type Uncacheable %x\n",
378		    (u_int32_t)tdfx_info->mrdesc.mr_base);
379#endif
380	}
381#ifdef DEBUG
382	else {
383		device_printf(dev, "Couldn't Set MTRR\n");
384		return 0;
385	}
386#endif
387	return 0;
388}
389
390static int
391tdfx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
392{
393	/*
394	 *	The open cdev method handles open(2) calls to /dev/3dfx[n]
395	 * We can pretty much allow any opening of the device.
396	 */
397	struct tdfx_softc *tdfx_info = dev->si_drv1;
398	if(tdfx_info->busy != 0) return EBUSY;
399#ifdef	DEBUG
400	printf("3dfx: Opened by #%d\n", td->td_proc->p_pid);
401#endif
402	/* Set the driver as busy */
403	tdfx_info->busy++;
404	return 0;
405}
406
407static int
408tdfx_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
409{
410	/*
411	 *	The close cdev method handles close(2) calls to /dev/3dfx[n]
412	 * We'll always want to close the device when it's called.
413	 */
414	struct tdfx_softc *tdfx_info = dev->si_drv1;
415	if(tdfx_info->busy == 0) return EBADF;
416	tdfx_info->busy = 0;
417#ifdef	DEBUG
418	printf("Closed by #%d\n", td->td_proc->p_pid);
419#endif
420	return 0;
421}
422
423static int
424tdfx_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
425{
426	/*
427	 * mmap(2) is called by a user process to request that an area of memory
428	 * associated with this device be mapped for the process to work with. Nprot
429	 * holds the protections requested, PROT_READ, PROT_WRITE, or both.
430	 */
431
432	/**** OLD GET CONFIG ****/
433	/* struct tdfx_softc* tdfx_info; */
434
435	/* Get the configuration for our card XXX*/
436	/*tdfx_info = dev->si_drv1; */
437	/************************/
438
439	struct tdfx_softc* tdfx_info[2];
440
441	tdfx_info[0] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 0);
442
443	/* If, for some reason, its not configured, we bail out */
444	if(tdfx_info[0] == NULL) {
445#ifdef	DEBUG
446	   printf("tdfx: tdfx_info (softc) is NULL\n");
447#endif
448	   return -1;
449	}
450
451	/* We must stay within the bound of our address space */
452	if((offset & 0xff000000) == tdfx_info[0]->addr0) {
453		offset &= 0xffffff;
454		*paddr = rman_get_start(tdfx_info[0]->memrange) + offset;
455		return 0;
456	}
457
458	if(tdfx_count > 1) {
459		tdfx_info[1] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 1);
460		if((offset & 0xff000000) == tdfx_info[1]->addr0) {
461			offset &= 0xffffff;
462			*paddr = rman_get_start(tdfx_info[1]->memrange) +
463			    offset;
464			return 0;
465		}
466	}
467
468	/* See if the Banshee/V3 LFB is being requested */
469	/*if(tdfx_info->memrange2 != NULL && (offset & 0xff000000) ==
470			tdfx_info->addr1) {
471	  	offset &= 0xffffff;
472		return atop(rman_get_start(tdfx_info[1]->memrange2) + offset);
473	}*/ /* VoodooNG code */
474
475	/* The ret call */
476	/* atop -> address to page
477	 * rman_get_start, get the (struct resource*)->r_start member,
478	 * the mapping base address.
479	 */
480	return -1;
481}
482
483static int
484tdfx_query_boards(void) {
485	/*
486    *	This returns the number of installed tdfx cards, we have been keeping
487	 * count, look at tdfx_attach
488	 */
489	return tdfx_count;
490}
491
492static int
493tdfx_query_fetch(u_int cmd, struct tdfx_pio_data *piod)
494{
495	/* XXX Comment this later, after careful inspection and spring cleaning :) */
496	/* Various return values 8bit-32bit */
497	u_int8_t  ret_byte;
498	u_int16_t ret_word;
499	u_int32_t ret_dword;
500	struct tdfx_softc* tdfx_info = NULL;
501
502	/* This one depend on the tdfx_* structs being properly initialized */
503
504	/*piod->device &= 0xf;*/
505	if((piod == NULL) ||(tdfx_count <= piod->device) ||
506			(piod->device < 0)) {
507#ifdef DEBUG
508		printf("tdfx: Bad device or internal struct in tdfx_query_fetch\n");
509#endif
510		return -EINVAL;
511	}
512
513	tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
514			piod->device);
515
516	if(tdfx_info == NULL) return -ENXIO;
517
518	/* We must restrict the size reads from the port, since to high or low of a
519	 * size witll result in wrong data being passed, and that's bad */
520	/* A few of these were pulled during the attach phase */
521	switch(piod->port) {
522		case PCI_VENDOR_ID_FREEBSD:
523			if(piod->size != 2) return -EINVAL;
524			copyout(&tdfx_info->vendor, piod->value, piod->size);
525			return 0;
526		case PCI_DEVICE_ID_FREEBSD:
527			if(piod->size != 2) return -EINVAL;
528			copyout(&tdfx_info->type, piod->value, piod->size);
529			return 0;
530		case PCI_BASE_ADDRESS_0_FREEBSD:
531			if(piod->size != 4) return -EINVAL;
532			copyout(&tdfx_info->addr0, piod->value, piod->size);
533			return 0;
534		case PCI_BASE_ADDRESS_1_FREEBSD:
535			if(piod->size != 4) return -EINVAL;
536			copyout(&tdfx_info->addr1, piod->value, piod->size);
537			return 0;
538		case PCI_PRIBUS_FREEBSD:
539			if(piod->size != 1) return -EINVAL;
540			break;
541		case PCI_IOBASE_0_FREEBSD:
542			if(piod->size != 2) return -EINVAL;
543			break;
544		case PCI_IOLIMIT_0_FREEBSD:
545			if(piod->size != 2) return -EINVAL;
546			break;
547		case SST1_PCI_SPECIAL1_FREEBSD:
548			if(piod->size != 4) return -EINVAL;
549			break;
550		case PCI_REVISION_ID_FREEBSD:
551			if(piod->size != 1) return -EINVAL;
552			break;
553		case SST1_PCI_SPECIAL4_FREEBSD:
554			if(piod->size != 4) return -EINVAL;
555			break;
556		default:
557			return -EINVAL;
558	}
559
560
561	/* Read the value and return */
562	switch(piod->size) {
563		case 1:
564			ret_byte = pci_read_config(tdfx_info[piod->device].dev,
565					piod->port, 1);
566			copyout(&ret_byte, piod->value, 1);
567			break;
568		case 2:
569			ret_word = pci_read_config(tdfx_info[piod->device].dev,
570					piod->port, 2);
571			copyout(&ret_word, piod->value, 2);
572			break;
573		case 4:
574			ret_dword = pci_read_config(tdfx_info[piod->device].dev,
575					piod->port, 4);
576			copyout(&ret_dword, piod->value, 4);
577			break;
578		default:
579			return -EINVAL;
580	}
581	return 0;
582}
583
584static int
585tdfx_query_update(u_int cmd, struct tdfx_pio_data *piod)
586{
587	/* XXX Comment this later, after careful inspection and spring cleaning :) */
588	/* Return vals */
589	u_int8_t  ret_byte;
590	u_int16_t ret_word;
591	u_int32_t ret_dword;
592
593	/* Port vals, mask */
594	u_int32_t retval, preval, mask;
595	struct tdfx_softc* tdfx_info = NULL;
596
597
598	if((piod == NULL) || (piod->device >= (tdfx_count &
599					0xf))) {
600#ifdef DEBUG
601		printf("tdfx: Bad struct or device in tdfx_query_update\n");
602#endif
603		return -EINVAL;
604	}
605
606	tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
607			piod->device);
608	if(tdfx_info == NULL) return -ENXIO;
609	/* Code below this line in the fuction was taken from the
610	 * Linux driver and converted for freebsd. */
611
612	/* Check the size for all the ports, to make sure stuff doesn't get messed up
613	 * by poorly written clients */
614
615	switch(piod->port) {
616		case PCI_COMMAND_FREEBSD:
617			if(piod->size != 2) return -EINVAL;
618			break;
619		case SST1_PCI_SPECIAL1_FREEBSD:
620			if(piod->size != 4) return -EINVAL;
621			break;
622		case SST1_PCI_SPECIAL2_FREEBSD:
623			if(piod->size != 4) return -EINVAL;
624			break;
625		case SST1_PCI_SPECIAL3_FREEBSD:
626			if(piod->size != 4) return -EINVAL;
627			break;
628		case SST1_PCI_SPECIAL4_FREEBSD:
629			if(piod->size != 4) return -EINVAL;
630			break;
631		default:
632			return -EINVAL;
633	}
634	/* Read the current value */
635	retval = pci_read_config(tdfx_info->dev, piod->port & ~3, 4);
636
637	/* These set up a mask to use, since apparently they wanted to write 4 bytes
638	 * at once to the ports */
639	switch (piod->size) {
640		case 1:
641			copyin(piod->value, &ret_byte, 1);
642			preval = ret_byte << (8 * (piod->port & 0x3));
643			mask = 0xff << (8 * (piod->port & 0x3));
644			break;
645		case 2:
646			copyin(piod->value, &ret_word, 2);
647			preval = ret_word << (8 * (piod->port & 0x3));
648			mask = 0xffff << (8 * (piod->port & 0x3));
649			break;
650		case 4:
651			copyin(piod->value, &ret_dword, 4);
652			preval = ret_dword;
653			mask = ~0;
654			break;
655		default:
656			return -EINVAL;
657	}
658	/* Finally, combine the values and write it to the port */
659	retval = (retval & ~mask) | preval;
660	pci_write_config(tdfx_info->dev, piod->port & ~3, retval, 4);
661
662	return 0;
663}
664
665/* For both of these, I added a variable named workport of type u_int so
666 * that I could eliminate the warning about my data type size. The
667 * applications expect the port to be of type short, so I needed to change
668 * this within the function */
669static int
670tdfx_do_pio_rd(struct tdfx_pio_data *piod)
671{
672	/* Return val */
673	u_int8_t  ret_byte;
674	u_int 	 workport;
675	struct tdfx_softc *tdfx_info =
676		(struct tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
677
678	/* Restricts the access of ports other than those we use */
679	if(((piod->port != VGA_INPUT_STATUS_1C) || (piod->port != SC_INDEX) ||
680		(piod->port != SC_DATA) || (piod->port != VGA_MISC_OUTPUT_READ)) &&
681		(piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
682		return -EPERM;
683
684	/* All VGA STATUS REGS are byte registers, size should never be > 1 */
685	if(piod->size != 1) {
686		return -EINVAL;
687	}
688
689	/* Write the data to the intended port */
690	workport = piod->port;
691	ret_byte = inb(workport);
692	copyout(&ret_byte, piod->value, sizeof(u_int8_t));
693	return 0;
694}
695
696static int
697tdfx_do_pio_wt(struct tdfx_pio_data *piod)
698{
699	/* return val */
700	u_int8_t  ret_byte;
701	u_int		 workport;
702	struct tdfx_softc *tdfx_info = (struct
703			tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
704	/* Replace old switch w/ massive if(...) */
705	/* Restricts the access of ports other than those we use */
706	if(((piod->port != SC_INDEX) && (piod->port != SC_DATA) &&
707		(piod->port != VGA_MISC_OUTPUT_READ)) /* Can't write VGA_ST_1C */ &&
708		(piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
709		return -EPERM;
710
711	/* All VGA STATUS REGS are byte registers, size should never be > 1 */
712	if(piod->size != 1) {
713		return -EINVAL;
714	}
715
716	/* Write the data to the intended port */
717	copyin(piod->value, &ret_byte, sizeof(u_int8_t));
718	workport = piod->port;
719	outb(workport, ret_byte);
720	return 0;
721}
722
723static int
724tdfx_do_query(u_int cmd, struct tdfx_pio_data *piod)
725{
726	/* There are three sub-commands to the query 0x33 */
727	switch(_IOC_NR(cmd)) {
728		case 2:
729			return tdfx_query_boards();
730			break;
731		case 3:
732			return tdfx_query_fetch(cmd, piod);
733			break;
734		case 4:
735			return tdfx_query_update(cmd, piod);
736			break;
737		default:
738			/* In case we are thrown a bogus sub-command! */
739#ifdef DEBUG
740			printf("Bad Sub-cmd: 0x%x\n", _IOC_NR(cmd));
741#endif
742			return -EINVAL;
743	}
744}
745
746static int
747tdfx_do_pio(u_int cmd, struct tdfx_pio_data *piod)
748{
749	/* Two types of PIO, INPUT and OUTPUT, as the name suggests */
750	switch(_IOC_DIR(cmd)) {
751		case IOCV_OUT:
752			return tdfx_do_pio_rd(piod);
753			break;
754		case IOCV_IN:
755			return tdfx_do_pio_wt(piod);
756			break;
757		default:
758			return -EINVAL;
759	}
760}
761
762/* Calls to ioctl(2) eventually end up here. Unhandled ioctls return an ENXIO,
763 * normally, you would read in the data pointed to by data, then write your
764 * output to it. The ioctl *should* normally return zero if everything is
765 * alright, but 3dfx didn't make it that way...
766 *
767 * For all of the ioctl code, in the event of a real error,
768 * we return -Exxxx rather than simply Exxxx. The reason for this
769 * is that the ioctls actually RET information back to the program
770 * sometimes, rather than filling it in the passed structure. We
771 * want to distinguish errors from useful data, and maintain compatibility.
772 *
773 * There is this portion of the proc struct called p_retval[], we can store a
774 * return value in td->td_retval[0] and place the return value if it is positive
775 * in there, then we can return 0 (good). If the return value is negative, we
776 * can return -retval and the error should be properly handled.
777 */
778static int
779tdfx_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
780{
781	int retval = 0;
782	struct tdfx_pio_data *piod = (struct tdfx_pio_data*)data;
783#ifdef	DEBUG
784	printf("IOCTL'd by #%d, cmd: 0x%x, data: %p\n", td->td_proc->p_pid, (u_int32_t)cmd,
785			piod);
786#endif
787	switch(_IOC_TYPE(cmd)) {
788		/* Return the real error if negative, or simply stick the valid return
789		 * in td->td_retval */
790	case 0x33:
791			/* The '3'(0x33) type IOCTL is for querying the installed cards */
792			if((retval = tdfx_do_query(cmd, piod)) > 0) td->td_retval[0] = retval;
793			else return -retval;
794			break;
795		case 0:
796			/* The 0 type IOCTL is for programmed I/O methods */
797			if((tdfx_do_pio(cmd, piod)) > 0) td->td_retval[0] = retval;
798			else return -retval;
799			break;
800		default:
801			/* Technically, we won't reach this from linux emu, but when glide
802			 * finally gets ported, watch out! */
803#ifdef DEBUG
804			printf("Bad IOCTL from #%d\n", td->td_proc->p_pid);
805#endif
806			return ENXIO;
807	}
808
809	return 0;
810}
811
812/* This is the device driver struct. This is sent to the driver subsystem to
813 * register the method structure and the info strcut space for this particular
814 * instance of the driver.
815 */
816static driver_t tdfx_driver = {
817	"tdfx",
818	tdfx_methods,
819	sizeof(struct tdfx_softc),
820};
821
822/* Tell Mr. Kernel about us! */
823DRIVER_MODULE(tdfx, pci, tdfx_driver, tdfx_devclass, 0, 0);
824MODULE_DEPEND(tdfx, mem, 1, 1, 1);
825MODULE_VERSION(tdfx, 1);
826