1/*
2 * Copyright (c) 2000-2001 by Coleman Kane <cokane@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Gardner Buchanan.
16 * 4. The name of Gardner Buchanan may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $FreeBSD: head/sys/dev/tdfx/tdfx_pci.c 111752 2003-03-02 18:50:21Z phk $
32 */
33
34/* 3dfx driver for FreeBSD 4.x - Finished 11 May 2000, 12:25AM ET
35 *
36 * Copyright (C) 2000-2001, by Coleman Kane <cokane@FreeBSD.org>,
37 * based upon the 3dfx driver written for linux, by Daryll Straus, Jon Taylor,
38 * and Jens Axboe, located at http://linux.3dfx.com.
39 */
40
41#include <sys/param.h>
42
43#include <sys/bus.h>
44#include <sys/cdefs.h>
45#include <sys/conf.h>
46#include <sys/fcntl.h>
47#include <sys/file.h>
48#include <sys/filedesc.h>
49#include <sys/filio.h>
50#include <sys/ioccom.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/mman.h>
54#include <sys/signalvar.h>
55#include <sys/systm.h>
56#include <sys/uio.h>
57
58#include <pci/pcivar.h>
59#include <pci/pcireg.h>
60
61#include <vm/vm.h>
62#include <vm/vm_kern.h>
63#include <vm/pmap.h>
64#include <vm/vm_extern.h>
65
66/* rman.h depends on machine/bus.h */
67#include <machine/resource.h>
68#include <machine/bus.h>
69#include <sys/rman.h>
70
71/* This must come first */
72#include "opt_tdfx.h"
73#ifdef TDFX_LINUX
74#include <dev/tdfx/tdfx_linux.h>
75#endif
76
77#include <dev/tdfx/tdfx_io.h>
78#include <dev/tdfx/tdfx_vars.h>
79#include <dev/tdfx/tdfx_pci.h>
80
81
82static devclass_t tdfx_devclass;
83
84
85static int tdfx_count = 0;
86
87
88/* Set up the boot probe/attach routines */
89static device_method_t tdfx_methods[] = {
90 DEVMETHOD(device_probe, tdfx_probe),
91 DEVMETHOD(device_attach, tdfx_attach),
92 DEVMETHOD(device_detach, tdfx_detach),
93 DEVMETHOD(device_shutdown, tdfx_shutdown),
94 { 0, 0 }
95};
96
97MALLOC_DEFINE(M_TDFX,"TDFX Driver","3DFX Graphics[/2D]/3D Accelerator(s)");
98
99#ifdef TDFX_LINUX
100MODULE_DEPEND(tdfx, linux, 1, 1, 1);
101LINUX_IOCTL_SET(tdfx, LINUX_IOCTL_TDFX_MIN, LINUX_IOCTL_TDFX_MAX);
102#endif
103
104/* Char. Dev. file operations structure */
105static struct cdevsw tdfx_cdev = {
106 /* open */ tdfx_open,
107 /* close */ tdfx_close,
108 /* read */ noread,
109 /* write */ nowrite,
110 /* ioctl */ tdfx_ioctl,
111 /* poll */ nopoll,
112 /* mmap */ tdfx_mmap,
113 /* strategy */ nostrategy,
114 /* name */ "tdfx",
115 /* maj */ CDEV_MAJOR,
116 /* dump */ nodump,
117 /* psize */ nopsize,
118 /* flags */ 0,
119};
120
121static int
122tdfx_probe(device_t dev)
123{
124 /*
125 * probe routine called on kernel boot to register supported devices. We get
126 * a device structure to work with, and we can test the VENDOR/DEVICE IDs to
127 * see if this PCI device is one that we support. Return 0 if yes, ENXIO if
128 * not.
129 */
130 switch(pci_get_devid(dev)) {
131 case PCI_DEVICE_ALLIANCE_AT3D:
132 device_set_desc(dev, "ProMotion At3D 3D Accelerator");
133 return 0;
134 case PCI_DEVICE_3DFX_VOODOO2:
135 device_set_desc(dev, "3DFX Voodoo II 3D Accelerator");
136 return 0;
137 /*case PCI_DEVICE_3DFX_BANSHEE:
138 device_set_desc(dev, "3DFX Voodoo Banshee 2D/3D Graphics Accelerator");
139 return 0;
140 case PCI_DEVICE_3DFX_VOODOO3:
141 device_set_desc(dev, "3DFX Voodoo3 2D/3D Graphics Accelerator");
142 return 0;*/
143 case PCI_DEVICE_3DFX_VOODOO1:
144 device_set_desc(dev, "3DFX Voodoo Graphics 3D Accelerator");
145 return 0;;
146 };
147
148 return ENXIO;
149}
150
151static int
152tdfx_attach(device_t dev) {
153 /*
154 * The attach routine is called after the probe routine successfully says it
155 * supports a given card. We now proceed to initialize this card for use with
156 * the system. I want to map the device memory for userland allocation and
157 * fill an information structure with information on this card. I'd also like
158 * to set Write Combining with the MTRR code so that we can hopefully speed
159 * up memory writes. The last thing is to register the character device
160 * interface to the card, so we can open it from /dev/3dfxN, where N is a
161 * small, whole number.
162 */
163 struct tdfx_softc *tdfx_info;
164 u_long val;
165 /* rid value tells bus_alloc_resource where to find the addresses of ports or
166 * of memory ranges in the PCI config space*/
167 int rid = PCIR_MAPS;
168
169 /* Increment the card counter (for the ioctl code) */
170 tdfx_count++;
171
172 /* Enable MemMap on Voodoo */
173 val = pci_read_config(dev, PCIR_COMMAND, 2);
174 val |= (PCIM_CMD_MEMEN);
175 pci_write_config(dev, PCIR_COMMAND, val, 2);
176 val = pci_read_config(dev, PCIR_COMMAND, 2);
177
178 /* Fill the soft config struct with info about this device*/
179 tdfx_info = device_get_softc(dev);
180 tdfx_info->dev = dev;
181 tdfx_info->vendor = pci_get_vendor(dev);
182 tdfx_info->type = pci_get_devid(dev) >> 16;
183 tdfx_info->bus = pci_get_bus(dev);
184 tdfx_info->dv = pci_get_slot(dev);
185 tdfx_info->curFile = NULL;
186
187 /*
188 * Get the Memory Location from the PCI Config, mask out lower word, since
189 * the config space register is only one word long (this is nicer than a
190 * bitshift).
191 */
192 tdfx_info->addr0 = (pci_read_config(dev, 0x10, 4) & 0xffff0000);
193#ifdef DEBUG
194 device_printf(dev, "Base0 @ 0x%x\n", tdfx_info->addr0);
195#endif
196 /* Notify the VM that we will be mapping some memory later */
197 tdfx_info->memrange = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 1,
198 RF_ACTIVE | RF_SHAREABLE);
199 if(tdfx_info->memrange == NULL) {
200#ifdef DEBUG
201 device_printf(dev, "Error mapping mem, won't be able to use mmap()\n");
202#endif
203 tdfx_info->memrid = 0;
204 }
205 else {
206 tdfx_info->memrid = rid;
207#ifdef DEBUG
208 device_printf(dev, "Mapped to: 0x%x\n",
209 (unsigned int)rman_get_start(tdfx_info->memrange));
210#endif
211 }
212
213 /* Setup for Voodoo3 and Banshee, PIO and an extram Memrange */
214 if(pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3 ||
215 pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE) {
216 rid = 0x14; /* 2nd mem map */
217 tdfx_info->addr1 = (pci_read_config(dev, 0x14, 4) & 0xffff0000);
218#ifdef DEBUG
219 device_printf(dev, "Base1 @ 0x%x\n", tdfx_info->addr1);
220#endif
221 tdfx_info->memrange2 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
222 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
223 if(tdfx_info->memrange2 == NULL) {
224#ifdef DEBUG
225 device_printf(dev, "Mem1 couldn't be allocated, glide may not work.");
226#endif
227 tdfx_info->memrid2 = 0;
228 }
229 else {
230 tdfx_info->memrid2 = rid;
231 }
232 /* Now to map the PIO stuff */
233 rid = PCIR_IOBASE0_2;
234 tdfx_info->pio0 = pci_read_config(dev, 0x2c, 2);
235 tdfx_info->pio0max = pci_read_config(dev, 0x30, 2) + tdfx_info->pio0;
236 tdfx_info->piorange = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
237 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
238 if(tdfx_info->piorange == NULL) {
239#ifdef DEBUG
240 device_printf(dev, "Couldn't map PIO range.");
241#endif
242 tdfx_info->piorid = 0;
243 }
244 else {
245 tdfx_info->piorid = rid;
246 }
247 } else {
248 tdfx_info->addr1 = 0;
249 tdfx_info->memrange2 = NULL;
250 tdfx_info->piorange = NULL;
251 }
252
253 /*
254 * Set Writecombining, or at least Uncacheable for the memory region, if we
255 * are able to
256 */
257
258 if(tdfx_setmtrr(dev) != 0) {
259#ifdef DEBUG
260 device_printf(dev, "Some weird error setting MTRRs");
261#endif
262 return -1;
263 }
264
265 /*
266 * make_dev registers the cdev to access the 3dfx card from /dev
267 * use hex here for the dev num, simply to provide better support if > 10
268 * voodoo cards, for the mad. The user must set the link, or use MAKEDEV.
269 * Why would we want that many voodoo cards anyhow?
270 */
271 tdfx_info->devt = make_dev(&tdfx_cdev, device_get_unit(dev),
272 UID_ROOT, GID_WHEEL, 0600, "3dfx%x", device_get_unit(dev));
273
274 return 0;
275}
276
277static int
278tdfx_detach(device_t dev) {
279 struct tdfx_softc* tdfx_info;
280 int retval;
281 tdfx_info = device_get_softc(dev);
282
283 /* Delete allocated resource, of course */
284 bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid,
285 tdfx_info->memrange);
286
287 /* Release extended Voodoo3/Banshee resources */
288 if(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE ||
289 pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) {
290 if(tdfx_info->memrange2 != NULL)
291 bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid2,
292 tdfx_info->memrange);
293 /* if(tdfx_info->piorange != NULL)
294 bus_release_resource(dev, SYS_RES_IOPORT, tdfx_info->piorid,
295 tdfx_info->piorange);*/
296 }
297
298 /* Though it is safe to leave the WRCOMB support since the
299 mem driver checks for it, we should remove it in order
300 to free an MTRR for another device */
301 retval = tdfx_clrmtrr(dev);
302#ifdef DEBUG
303 if(retval != 0)
304 printf("tdfx: For some reason, I couldn't clear the mtrr\n");
305#endif
306 /* Remove device entry when it can no longer be accessed */
307 destroy_dev(tdfx_info->devt);
308 return(0);
309}
310
311static int
312tdfx_shutdown(device_t dev) {
313#ifdef DEBUG
314 device_printf(dev, "tdfx: Device Shutdown\n");
315#endif
316 return 0;
317}
318
319static int
320tdfx_clrmtrr(device_t dev) {
321 /* This function removes the MTRR set by the attach call, so it can be used
322 * in the future by other drivers.
323 */
324 int retval, act;
325 struct tdfx_softc *tdfx_info = device_get_softc(dev);
326
327 act = MEMRANGE_SET_REMOVE;
328 retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
329 return retval;
330}
331
332static int
333tdfx_setmtrr(device_t dev) {
334 /*
335 * This is the MTRR setting function for the 3dfx card. It is called from
336 * tdfx_attach. If we can't set the MTRR properly, it's not the end of the
337 * world. We can still continue, just with slightly (very slightly) degraded
338 * performance.
339 */
340 int retval = 0, act;
341 struct tdfx_softc *tdfx_info = device_get_softc(dev);
342
343 /* The older Voodoo cards have a shorter memrange than the newer ones */
344 if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1) || (pci_get_devid(dev) ==
345 PCI_DEVICE_3DFX_VOODOO2)) {
346 tdfx_info->mrdesc.mr_len = 0x400000;
347
348 /* The memory descriptor is described as the top 15 bits of the real
349 address */
350 tdfx_info->mrdesc.mr_base = tdfx_info->addr0 & 0xfffe0000;
351 }
352 else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) ||
353 (pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE)) {
354 tdfx_info->mrdesc.mr_len = 0x1000000;
355 /* The Voodoo3 and Banshee LFB is the second memory address */
356 /* The memory descriptor is described as the top 15 bits of the real
357 address */
358 tdfx_info->mrdesc.mr_base = tdfx_info->addr1 & 0xfffe0000;
359 }
360 else
361 return 0;
362 /*
363 * The Alliance Pro Motion AT3D was not mentioned in the linux
364 * driver as far as MTRR support goes, so I just won't put the
365 * code in here for it. This is where it should go, though.
366 */
367
368 /* Firstly, try to set write combining */
369 tdfx_info->mrdesc.mr_flags = MDF_WRITECOMBINE;
370 bcopy("tdfx", &tdfx_info->mrdesc.mr_owner, 4);
371 act = MEMRANGE_SET_UPDATE;
372 retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
373
374 if(retval == 0) {
375#ifdef DEBUG
376 device_printf(dev, "MTRR Set Correctly for tdfx\n");
377#endif
378 } else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO2) ||
379 (pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1)) {
380 /* if, for some reason we can't set the WRCOMB range with the V1/V2, we
381 * can still possibly use the UNCACHEABLE region for it instead, and help
382 * out in a small way */
383 tdfx_info->mrdesc.mr_flags = MDF_UNCACHEABLE;
384 /* This length of 1000h was taken from the linux device driver... */
385 tdfx_info->mrdesc.mr_len = 0x1000;
386
387 /*
388 * If, for some reason, we can't set the MTRR (N/A?) we may still continue
389 */
390#ifdef DEBUG
391 if(retval == 0) {
392 device_printf(dev, "MTRR Set Type Uncacheable %x\n",
393 (u_int32_t)tdfx_info->mrdesc.mr_base);
394 } else {
395 device_printf(dev, "Couldn't Set MTRR\n");
396 }
397#endif
398 }
399#ifdef DEBUG
400 else {
401 device_printf(dev, "Couldn't Set MTRR\n");
402 return 0;
403 }
404#endif
405 return 0;
406}
407
408static int
409tdfx_open(dev_t dev, int flags, int fmt, struct thread *td)
410{
411 /*
412 * The open cdev method handles open(2) calls to /dev/3dfx[n]
413 * We can pretty much allow any opening of the device.
414 */
415 struct tdfx_softc *tdfx_info = devclass_get_softc(tdfx_devclass,
416 UNIT(minor(dev)));
417 if(tdfx_info->busy != 0) return EBUSY;
418#ifdef DEBUG
419 printf("3dfx: Opened by #%d\n", td->td_proc->p_pid);
420#endif
421 /* Set the driver as busy */
422 tdfx_info->busy++;
423 return 0;
424}
425
426static int
427tdfx_close(dev_t dev, int fflag, int devtype, struct thread *td)
428{
429 /*
430 * The close cdev method handles close(2) calls to /dev/3dfx[n]
431 * We'll always want to close the device when it's called.
432 */
433 struct tdfx_softc *tdfx_info = devclass_get_softc(tdfx_devclass,
434 UNIT(minor(dev)));
435 if(tdfx_info->busy == 0) return EBADF;
436 tdfx_info->busy = 0;
437#ifdef DEBUG
438 printf("Closed by #%d\n", td->td_proc->p_pid);
439#endif
440 return 0;
441}
442
443static int
444tdfx_mmap(dev_t dev, vm_offset_t offset, vm_offset_t *paddr, int nprot)
445{
446 /*
447 * mmap(2) is called by a user process to request that an area of memory
448 * associated with this device be mapped for the process to work with. Nprot
449 * holds the protections requested, PROT_READ, PROT_WRITE, or both.
450 */
451
452 /**** OLD GET CONFIG ****/
453 /* struct tdfx_softc* tdfx_info; */
454
455 /* Get the configuration for our card XXX*/
456 /*tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
457 UNIT(minor(dev)));*/
458 /************************/
459
460 struct tdfx_softc* tdfx_info[2];
461
462 tdfx_info[0] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 0);
463
464 /* If, for some reason, its not configured, we bail out */
465 if(tdfx_info[0] == NULL) {
466#ifdef DEBUG
467 printf("tdfx: tdfx_info (softc) is NULL\n");
468#endif
469 return -1;
470 }
471
472 /* We must stay within the bound of our address space */
473 if((offset & 0xff000000) == tdfx_info[0]->addr0) {
474 offset &= 0xffffff;
475 *paddr = rman_get_start(tdfx_info[0]->memrange) + offset;
476 return 0;
477 }
478
479 if(tdfx_count > 1) {
480 tdfx_info[1] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 1);
481 if((offset & 0xff000000) == tdfx_info[1]->addr0) {
482 offset &= 0xffffff;
483 *paddr = rman_get_start(tdfx_info[1]->memrange) +
484 offset;
485 return 0;
486 }
487 }
488
489 /* See if the Banshee/V3 LFB is being requested */
490 /*if(tdfx_info->memrange2 != NULL && (offset & 0xff000000) ==
491 tdfx_info->addr1) {
492 offset &= 0xffffff;
493 return atop(rman_get_start(tdfx_info[1]->memrange2) + offset);
494 }*/ /* VoodooNG code */
495
496 /* The ret call */
497 /* atop -> address to page
498 * rman_get_start, get the (struct resource*)->r_start member,
499 * the mapping base address.
500 */
501 return -1;
502}
503
504static int
505tdfx_query_boards(void) {
506 /*
507 * This returns the number of installed tdfx cards, we have been keeping
508 * count, look at tdfx_attach
509 */
510 return tdfx_count;
511}
512
513static int
514tdfx_query_fetch(u_int cmd, struct tdfx_pio_data *piod)
515{
516 /* XXX Comment this later, after careful inspection and spring cleaning :) */
517 /* Various return values 8bit-32bit */
518 u_int8_t ret_byte;
519 u_int16_t ret_word;
520 u_int32_t ret_dword;
521 struct tdfx_softc* tdfx_info = NULL;
522
523 /* This one depend on the tdfx_* structs being properly initialized */
524
525 /*piod->device &= 0xf;*/
526 if((piod == NULL) ||(tdfx_count <= piod->device) ||
527 (piod->device < 0)) {
528#ifdef DEBUG
529 printf("tdfx: Bad device or internal struct in tdfx_query_fetch\n");
530#endif
531 return -EINVAL;
532 }
533
534 tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
535 piod->device);
536
537 if(tdfx_info == NULL) return -ENXIO;
538
539 /* We must restrict the size reads from the port, since to high or low of a
540 * size witll result in wrong data being passed, and that's bad */
541 /* A few of these were pulled during the attach phase */
542 switch(piod->port) {
543 case PCI_VENDOR_ID_FREEBSD:
544 if(piod->size != 2) return -EINVAL;
545 copyout(&tdfx_info->vendor, piod->value, piod->size);
546 return 0;
547 case PCI_DEVICE_ID_FREEBSD:
548 if(piod->size != 2) return -EINVAL;
549 copyout(&tdfx_info->type, piod->value, piod->size);
550 return 0;
551 case PCI_BASE_ADDRESS_0_FREEBSD:
552 if(piod->size != 4) return -EINVAL;
553 copyout(&tdfx_info->addr0, piod->value, piod->size);
554 return 0;
555 case PCI_BASE_ADDRESS_1_FREEBSD:
556 if(piod->size != 4) return -EINVAL;
557 copyout(&tdfx_info->addr1, piod->value, piod->size);
558 return 0;
559 case PCI_PRIBUS_FREEBSD:
560 if(piod->size != 1) return -EINVAL;
561 break;
562 case PCI_IOBASE_0_FREEBSD:
563 if(piod->size != 2) return -EINVAL;
564 break;
565 case PCI_IOLIMIT_0_FREEBSD:
566 if(piod->size != 2) return -EINVAL;
567 break;
568 case SST1_PCI_SPECIAL1_FREEBSD:
569 if(piod->size != 4) return -EINVAL;
570 break;
571 case PCI_REVISION_ID_FREEBSD:
572 if(piod->size != 1) return -EINVAL;
573 break;
574 case SST1_PCI_SPECIAL4_FREEBSD:
575 if(piod->size != 4) return -EINVAL;
576 break;
577 default:
578 return -EINVAL;
579 }
580
581
582 /* Read the value and return */
583 switch(piod->size) {
584 case 1:
585 ret_byte = pci_read_config(tdfx_info[piod->device].dev,
586 piod->port, 1);
587 copyout(&ret_byte, piod->value, 1);
588 break;
589 case 2:
590 ret_word = pci_read_config(tdfx_info[piod->device].dev,
591 piod->port, 2);
592 copyout(&ret_word, piod->value, 2);
593 break;
594 case 4:
595 ret_dword = pci_read_config(tdfx_info[piod->device].dev,
596 piod->port, 4);
597 copyout(&ret_dword, piod->value, 4);
598 break;
599 default:
600 return -EINVAL;
601 }
602 return 0;
603}
604
605static int
606tdfx_query_update(u_int cmd, struct tdfx_pio_data *piod)
607{
608 /* XXX Comment this later, after careful inspection and spring cleaning :) */
609 /* Return vals */
610 u_int8_t ret_byte;
611 u_int16_t ret_word;
612 u_int32_t ret_dword;
613
614 /* Port vals, mask */
615 u_int32_t retval, preval, mask;
616 struct tdfx_softc* tdfx_info = NULL;
617
618
619 if((piod == NULL) || (piod->device >= (tdfx_count &
620 0xf))) {
621#ifdef DEBUG
622 printf("tdfx: Bad struct or device in tdfx_query_update\n");
623#endif
624 return -EINVAL;
625 }
626
627 tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
628 piod->device);
629 if(tdfx_info == NULL) return -ENXIO;
630 /* Code below this line in the fuction was taken from the
631 * Linux driver and converted for freebsd. */
632
633 /* Check the size for all the ports, to make sure stuff doesn't get messed up
634 * by poorly written clients */
635
636 switch(piod->port) {
637 case PCI_COMMAND_FREEBSD:
638 if(piod->size != 2) return -EINVAL;
639 break;
640 case SST1_PCI_SPECIAL1_FREEBSD:
641 if(piod->size != 4) return -EINVAL;
642 break;
643 case SST1_PCI_SPECIAL2_FREEBSD:
644 if(piod->size != 4) return -EINVAL;
645 break;
646 case SST1_PCI_SPECIAL3_FREEBSD:
647 if(piod->size != 4) return -EINVAL;
648 break;
649 case SST1_PCI_SPECIAL4_FREEBSD:
650 if(piod->size != 4) return -EINVAL;
651 break;
652 default:
653 return -EINVAL;
654 }
655 /* Read the current value */
656 retval = pci_read_config(tdfx_info->dev, piod->port & ~3, 4);
657
658 /* These set up a mask to use, since apparently they wanted to write 4 bytes
659 * at once to the ports */
660 switch (piod->size) {
661 case 1:
662 copyin(piod->value, &ret_byte, 1);
663 preval = ret_byte << (8 * (piod->port & 0x3));
664 mask = 0xff << (8 * (piod->port & 0x3));
665 break;
666 case 2:
667 copyin(piod->value, &ret_word, 2);
668 preval = ret_word << (8 * (piod->port & 0x3));
669 mask = 0xffff << (8 * (piod->port & 0x3));
670 break;
671 case 4:
672 copyin(piod->value, &ret_dword, 4);
673 preval = ret_dword;
674 mask = ~0;
675 break;
676 default:
677 return -EINVAL;
678 }
679 /* Finally, combine the values and write it to the port */
680 retval = (retval & ~mask) | preval;
681 pci_write_config(tdfx_info->dev, piod->port & ~3, retval, 4);
682
683 return 0;
684}
685
686/* For both of these, I added a variable named workport of type u_int so
687 * that I could eliminate the warning about my data type size. The
688 * applications expect the port to be of type short, so I needed to change
689 * this within the function */
690static int
691tdfx_do_pio_rd(struct tdfx_pio_data *piod)
692{
693 /* Return val */
694 u_int8_t ret_byte;
695 u_int workport;
696 struct tdfx_softc *tdfx_info =
697 (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
698
699 /* Restricts the access of ports other than those we use */
700 if(((piod->port != VGA_INPUT_STATUS_1C) || (piod->port != SC_INDEX) ||
701 (piod->port != SC_DATA) || (piod->port != VGA_MISC_OUTPUT_READ)) &&
702 (piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
703 return -EPERM;
704
705 /* All VGA STATUS REGS are byte registers, size should never be > 1 */
706 if(piod->size != 1) {
707 return -EINVAL;
708 }
709
710 /* Write the data to the intended port */
711 workport = piod->port;
712 ret_byte = inb(workport);
713 copyout(&ret_byte, piod->value, sizeof(u_int8_t));
714 return 0;
715}
716
717static int
718tdfx_do_pio_wt(struct tdfx_pio_data *piod)
719{
720 /* return val */
721 u_int8_t ret_byte;
722 u_int workport;
723 struct tdfx_softc *tdfx_info = (struct
724 tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
725 /* Replace old switch w/ massive if(...) */
726 /* Restricts the access of ports other than those we use */
727 if(((piod->port != SC_INDEX) && (piod->port != SC_DATA) &&
728 (piod->port != VGA_MISC_OUTPUT_READ)) /* Can't write VGA_ST_1C */ &&
729 (piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
730 return -EPERM;
731
732 /* All VGA STATUS REGS are byte registers, size should never be > 1 */
733 if(piod->size != 1) {
734 return -EINVAL;
735 }
736
737 /* Write the data to the intended port */
738 copyin(piod->value, &ret_byte, sizeof(u_int8_t));
739 workport = piod->port;
740 outb(workport, ret_byte);
741 return 0;
742}
743
744static int
745tdfx_do_query(u_int cmd, struct tdfx_pio_data *piod)
746{
747 /* There are three sub-commands to the query 0x33 */
748 switch(_IOC_NR(cmd)) {
749 case 2:
750 return tdfx_query_boards();
751 break;
752 case 3:
753 return tdfx_query_fetch(cmd, piod);
754 break;
755 case 4:
756 return tdfx_query_update(cmd, piod);
757 break;
758 default:
759 /* In case we are thrown a bogus sub-command! */
760#ifdef DEBUG
761 printf("Bad Sub-cmd: 0x%x\n", _IOC_NR(cmd));
762#endif
763 return -EINVAL;
764 };
765}
766
767static int
768tdfx_do_pio(u_int cmd, struct tdfx_pio_data *piod)
769{
770 /* Two types of PIO, INPUT and OUTPUT, as the name suggests */
771 switch(_IOC_DIR(cmd)) {
772 case IOCV_OUT:
773 return tdfx_do_pio_rd(piod);
774 break;
775 case IOCV_IN:
776 return tdfx_do_pio_wt(piod);
777 break;
778 default:
779 return -EINVAL;
780 };
781}
782
783/* Calls to ioctl(2) eventually end up here. Unhandled ioctls return an ENXIO,
784 * normally, you would read in the data pointed to by data, then write your
785 * output to it. The ioctl *should* normally return zero if everything is
786 * alright, but 3dfx didn't make it that way...
787 *
788 * For all of the ioctl code, in the event of a real error,
789 * we return -Exxxx rather than simply Exxxx. The reason for this
790 * is that the ioctls actually RET information back to the program
791 * sometimes, rather than filling it in the passed structure. We
792 * want to distinguish errors from useful data, and maintain compatibility.
793 *
794 * There is this portion of the proc struct called p_retval[], we can store a
795 * return value in td->td_retval[0] and place the return value if it is positive
796 * in there, then we can return 0 (good). If the return value is negative, we
797 * can return -retval and the error should be properly handled.
798 */
799static int
800tdfx_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
801{
802 int retval = 0;
803 struct tdfx_pio_data *piod = (struct tdfx_pio_data*)data;
804#ifdef DEBUG
805 printf("IOCTL'd by #%d, cmd: 0x%x, data: %p\n", td->td_proc->p_pid, (u_int32_t)cmd,
806 piod);
807#endif
808 switch(_IOC_TYPE(cmd)) {
809 /* Return the real error if negative, or simply stick the valid return
810 * in td->td_retval */
811 case 0x33:
812 /* The '3'(0x33) type IOCTL is for querying the installed cards */
813 if((retval = tdfx_do_query(cmd, piod)) > 0) td->td_retval[0] = retval;
814 else return -retval;
815 break;
816 case 0:
817 /* The 0 type IOCTL is for programmed I/O methods */
818 if((tdfx_do_pio(cmd, piod)) > 0) td->td_retval[0] = retval;
819 else return -retval;
820 break;
821 default:
822 /* Technically, we won't reach this from linux emu, but when glide
823 * finally gets ported, watch out! */
824#ifdef DEBUG
825 printf("Bad IOCTL from #%d\n", td->td_proc->p_pid);
826#endif
827 return ENXIO;
828 }
829
830 return 0;
831}
832
833#ifdef TDFX_LINUX
834/*
835 * Linux emulation IOCTL for /dev/tdfx
836 */
837static int
838linux_ioctl_tdfx(struct thread *td, struct linux_ioctl_args* args)
839{
840 int error = 0;
841 u_long cmd = args->cmd & 0xffff;
842
843 /* The structure passed to ioctl has two shorts, one int
844 and one void*. */
845 char d_pio[2*sizeof(short) + sizeof(int) + sizeof(void*)];
846
847 struct file *fp;
848
849 if ((error = fget(td, args->fd, &fp)) != 0)
850 return (error);
851 /* We simply copy the data and send it right to ioctl */
852 copyin((caddr_t)args->arg, &d_pio, sizeof(d_pio));
853 error = fo_ioctl(fp, cmd, (caddr_t)&d_pio, td->td_ucred, td);
854 fdrop(fp, td);
855 return error;
856}
857#endif /* TDFX_LINUX */
858
859
860/* This is the device driver struct. This is sent to the driver subsystem to
861 * register the method structure and the info strcut space for this particular
862 * instance of the driver.
863 */
864static driver_t tdfx_driver = {
865 "tdfx",
866 tdfx_methods,
867 sizeof(struct tdfx_softc),
868};
869
870/* Tell Mr. Kernel about us! */
871DRIVER_MODULE(tdfx, pci, tdfx_driver, tdfx_devclass, 0, 0);
2 * Copyright (c) 2000-2001 by Coleman Kane <cokane@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Gardner Buchanan.
16 * 4. The name of Gardner Buchanan may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * $FreeBSD: head/sys/dev/tdfx/tdfx_pci.c 111752 2003-03-02 18:50:21Z phk $
32 */
33
34/* 3dfx driver for FreeBSD 4.x - Finished 11 May 2000, 12:25AM ET
35 *
36 * Copyright (C) 2000-2001, by Coleman Kane <cokane@FreeBSD.org>,
37 * based upon the 3dfx driver written for linux, by Daryll Straus, Jon Taylor,
38 * and Jens Axboe, located at http://linux.3dfx.com.
39 */
40
41#include <sys/param.h>
42
43#include <sys/bus.h>
44#include <sys/cdefs.h>
45#include <sys/conf.h>
46#include <sys/fcntl.h>
47#include <sys/file.h>
48#include <sys/filedesc.h>
49#include <sys/filio.h>
50#include <sys/ioccom.h>
51#include <sys/kernel.h>
52#include <sys/malloc.h>
53#include <sys/mman.h>
54#include <sys/signalvar.h>
55#include <sys/systm.h>
56#include <sys/uio.h>
57
58#include <pci/pcivar.h>
59#include <pci/pcireg.h>
60
61#include <vm/vm.h>
62#include <vm/vm_kern.h>
63#include <vm/pmap.h>
64#include <vm/vm_extern.h>
65
66/* rman.h depends on machine/bus.h */
67#include <machine/resource.h>
68#include <machine/bus.h>
69#include <sys/rman.h>
70
71/* This must come first */
72#include "opt_tdfx.h"
73#ifdef TDFX_LINUX
74#include <dev/tdfx/tdfx_linux.h>
75#endif
76
77#include <dev/tdfx/tdfx_io.h>
78#include <dev/tdfx/tdfx_vars.h>
79#include <dev/tdfx/tdfx_pci.h>
80
81
82static devclass_t tdfx_devclass;
83
84
85static int tdfx_count = 0;
86
87
88/* Set up the boot probe/attach routines */
89static device_method_t tdfx_methods[] = {
90 DEVMETHOD(device_probe, tdfx_probe),
91 DEVMETHOD(device_attach, tdfx_attach),
92 DEVMETHOD(device_detach, tdfx_detach),
93 DEVMETHOD(device_shutdown, tdfx_shutdown),
94 { 0, 0 }
95};
96
97MALLOC_DEFINE(M_TDFX,"TDFX Driver","3DFX Graphics[/2D]/3D Accelerator(s)");
98
99#ifdef TDFX_LINUX
100MODULE_DEPEND(tdfx, linux, 1, 1, 1);
101LINUX_IOCTL_SET(tdfx, LINUX_IOCTL_TDFX_MIN, LINUX_IOCTL_TDFX_MAX);
102#endif
103
104/* Char. Dev. file operations structure */
105static struct cdevsw tdfx_cdev = {
106 /* open */ tdfx_open,
107 /* close */ tdfx_close,
108 /* read */ noread,
109 /* write */ nowrite,
110 /* ioctl */ tdfx_ioctl,
111 /* poll */ nopoll,
112 /* mmap */ tdfx_mmap,
113 /* strategy */ nostrategy,
114 /* name */ "tdfx",
115 /* maj */ CDEV_MAJOR,
116 /* dump */ nodump,
117 /* psize */ nopsize,
118 /* flags */ 0,
119};
120
121static int
122tdfx_probe(device_t dev)
123{
124 /*
125 * probe routine called on kernel boot to register supported devices. We get
126 * a device structure to work with, and we can test the VENDOR/DEVICE IDs to
127 * see if this PCI device is one that we support. Return 0 if yes, ENXIO if
128 * not.
129 */
130 switch(pci_get_devid(dev)) {
131 case PCI_DEVICE_ALLIANCE_AT3D:
132 device_set_desc(dev, "ProMotion At3D 3D Accelerator");
133 return 0;
134 case PCI_DEVICE_3DFX_VOODOO2:
135 device_set_desc(dev, "3DFX Voodoo II 3D Accelerator");
136 return 0;
137 /*case PCI_DEVICE_3DFX_BANSHEE:
138 device_set_desc(dev, "3DFX Voodoo Banshee 2D/3D Graphics Accelerator");
139 return 0;
140 case PCI_DEVICE_3DFX_VOODOO3:
141 device_set_desc(dev, "3DFX Voodoo3 2D/3D Graphics Accelerator");
142 return 0;*/
143 case PCI_DEVICE_3DFX_VOODOO1:
144 device_set_desc(dev, "3DFX Voodoo Graphics 3D Accelerator");
145 return 0;;
146 };
147
148 return ENXIO;
149}
150
151static int
152tdfx_attach(device_t dev) {
153 /*
154 * The attach routine is called after the probe routine successfully says it
155 * supports a given card. We now proceed to initialize this card for use with
156 * the system. I want to map the device memory for userland allocation and
157 * fill an information structure with information on this card. I'd also like
158 * to set Write Combining with the MTRR code so that we can hopefully speed
159 * up memory writes. The last thing is to register the character device
160 * interface to the card, so we can open it from /dev/3dfxN, where N is a
161 * small, whole number.
162 */
163 struct tdfx_softc *tdfx_info;
164 u_long val;
165 /* rid value tells bus_alloc_resource where to find the addresses of ports or
166 * of memory ranges in the PCI config space*/
167 int rid = PCIR_MAPS;
168
169 /* Increment the card counter (for the ioctl code) */
170 tdfx_count++;
171
172 /* Enable MemMap on Voodoo */
173 val = pci_read_config(dev, PCIR_COMMAND, 2);
174 val |= (PCIM_CMD_MEMEN);
175 pci_write_config(dev, PCIR_COMMAND, val, 2);
176 val = pci_read_config(dev, PCIR_COMMAND, 2);
177
178 /* Fill the soft config struct with info about this device*/
179 tdfx_info = device_get_softc(dev);
180 tdfx_info->dev = dev;
181 tdfx_info->vendor = pci_get_vendor(dev);
182 tdfx_info->type = pci_get_devid(dev) >> 16;
183 tdfx_info->bus = pci_get_bus(dev);
184 tdfx_info->dv = pci_get_slot(dev);
185 tdfx_info->curFile = NULL;
186
187 /*
188 * Get the Memory Location from the PCI Config, mask out lower word, since
189 * the config space register is only one word long (this is nicer than a
190 * bitshift).
191 */
192 tdfx_info->addr0 = (pci_read_config(dev, 0x10, 4) & 0xffff0000);
193#ifdef DEBUG
194 device_printf(dev, "Base0 @ 0x%x\n", tdfx_info->addr0);
195#endif
196 /* Notify the VM that we will be mapping some memory later */
197 tdfx_info->memrange = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 1,
198 RF_ACTIVE | RF_SHAREABLE);
199 if(tdfx_info->memrange == NULL) {
200#ifdef DEBUG
201 device_printf(dev, "Error mapping mem, won't be able to use mmap()\n");
202#endif
203 tdfx_info->memrid = 0;
204 }
205 else {
206 tdfx_info->memrid = rid;
207#ifdef DEBUG
208 device_printf(dev, "Mapped to: 0x%x\n",
209 (unsigned int)rman_get_start(tdfx_info->memrange));
210#endif
211 }
212
213 /* Setup for Voodoo3 and Banshee, PIO and an extram Memrange */
214 if(pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3 ||
215 pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE) {
216 rid = 0x14; /* 2nd mem map */
217 tdfx_info->addr1 = (pci_read_config(dev, 0x14, 4) & 0xffff0000);
218#ifdef DEBUG
219 device_printf(dev, "Base1 @ 0x%x\n", tdfx_info->addr1);
220#endif
221 tdfx_info->memrange2 = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
222 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
223 if(tdfx_info->memrange2 == NULL) {
224#ifdef DEBUG
225 device_printf(dev, "Mem1 couldn't be allocated, glide may not work.");
226#endif
227 tdfx_info->memrid2 = 0;
228 }
229 else {
230 tdfx_info->memrid2 = rid;
231 }
232 /* Now to map the PIO stuff */
233 rid = PCIR_IOBASE0_2;
234 tdfx_info->pio0 = pci_read_config(dev, 0x2c, 2);
235 tdfx_info->pio0max = pci_read_config(dev, 0x30, 2) + tdfx_info->pio0;
236 tdfx_info->piorange = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
237 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
238 if(tdfx_info->piorange == NULL) {
239#ifdef DEBUG
240 device_printf(dev, "Couldn't map PIO range.");
241#endif
242 tdfx_info->piorid = 0;
243 }
244 else {
245 tdfx_info->piorid = rid;
246 }
247 } else {
248 tdfx_info->addr1 = 0;
249 tdfx_info->memrange2 = NULL;
250 tdfx_info->piorange = NULL;
251 }
252
253 /*
254 * Set Writecombining, or at least Uncacheable for the memory region, if we
255 * are able to
256 */
257
258 if(tdfx_setmtrr(dev) != 0) {
259#ifdef DEBUG
260 device_printf(dev, "Some weird error setting MTRRs");
261#endif
262 return -1;
263 }
264
265 /*
266 * make_dev registers the cdev to access the 3dfx card from /dev
267 * use hex here for the dev num, simply to provide better support if > 10
268 * voodoo cards, for the mad. The user must set the link, or use MAKEDEV.
269 * Why would we want that many voodoo cards anyhow?
270 */
271 tdfx_info->devt = make_dev(&tdfx_cdev, device_get_unit(dev),
272 UID_ROOT, GID_WHEEL, 0600, "3dfx%x", device_get_unit(dev));
273
274 return 0;
275}
276
277static int
278tdfx_detach(device_t dev) {
279 struct tdfx_softc* tdfx_info;
280 int retval;
281 tdfx_info = device_get_softc(dev);
282
283 /* Delete allocated resource, of course */
284 bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid,
285 tdfx_info->memrange);
286
287 /* Release extended Voodoo3/Banshee resources */
288 if(pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE ||
289 pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) {
290 if(tdfx_info->memrange2 != NULL)
291 bus_release_resource(dev, SYS_RES_MEMORY, tdfx_info->memrid2,
292 tdfx_info->memrange);
293 /* if(tdfx_info->piorange != NULL)
294 bus_release_resource(dev, SYS_RES_IOPORT, tdfx_info->piorid,
295 tdfx_info->piorange);*/
296 }
297
298 /* Though it is safe to leave the WRCOMB support since the
299 mem driver checks for it, we should remove it in order
300 to free an MTRR for another device */
301 retval = tdfx_clrmtrr(dev);
302#ifdef DEBUG
303 if(retval != 0)
304 printf("tdfx: For some reason, I couldn't clear the mtrr\n");
305#endif
306 /* Remove device entry when it can no longer be accessed */
307 destroy_dev(tdfx_info->devt);
308 return(0);
309}
310
311static int
312tdfx_shutdown(device_t dev) {
313#ifdef DEBUG
314 device_printf(dev, "tdfx: Device Shutdown\n");
315#endif
316 return 0;
317}
318
319static int
320tdfx_clrmtrr(device_t dev) {
321 /* This function removes the MTRR set by the attach call, so it can be used
322 * in the future by other drivers.
323 */
324 int retval, act;
325 struct tdfx_softc *tdfx_info = device_get_softc(dev);
326
327 act = MEMRANGE_SET_REMOVE;
328 retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
329 return retval;
330}
331
332static int
333tdfx_setmtrr(device_t dev) {
334 /*
335 * This is the MTRR setting function for the 3dfx card. It is called from
336 * tdfx_attach. If we can't set the MTRR properly, it's not the end of the
337 * world. We can still continue, just with slightly (very slightly) degraded
338 * performance.
339 */
340 int retval = 0, act;
341 struct tdfx_softc *tdfx_info = device_get_softc(dev);
342
343 /* The older Voodoo cards have a shorter memrange than the newer ones */
344 if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1) || (pci_get_devid(dev) ==
345 PCI_DEVICE_3DFX_VOODOO2)) {
346 tdfx_info->mrdesc.mr_len = 0x400000;
347
348 /* The memory descriptor is described as the top 15 bits of the real
349 address */
350 tdfx_info->mrdesc.mr_base = tdfx_info->addr0 & 0xfffe0000;
351 }
352 else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO3) ||
353 (pci_get_devid(dev) == PCI_DEVICE_3DFX_BANSHEE)) {
354 tdfx_info->mrdesc.mr_len = 0x1000000;
355 /* The Voodoo3 and Banshee LFB is the second memory address */
356 /* The memory descriptor is described as the top 15 bits of the real
357 address */
358 tdfx_info->mrdesc.mr_base = tdfx_info->addr1 & 0xfffe0000;
359 }
360 else
361 return 0;
362 /*
363 * The Alliance Pro Motion AT3D was not mentioned in the linux
364 * driver as far as MTRR support goes, so I just won't put the
365 * code in here for it. This is where it should go, though.
366 */
367
368 /* Firstly, try to set write combining */
369 tdfx_info->mrdesc.mr_flags = MDF_WRITECOMBINE;
370 bcopy("tdfx", &tdfx_info->mrdesc.mr_owner, 4);
371 act = MEMRANGE_SET_UPDATE;
372 retval = mem_range_attr_set(&tdfx_info->mrdesc, &act);
373
374 if(retval == 0) {
375#ifdef DEBUG
376 device_printf(dev, "MTRR Set Correctly for tdfx\n");
377#endif
378 } else if((pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO2) ||
379 (pci_get_devid(dev) == PCI_DEVICE_3DFX_VOODOO1)) {
380 /* if, for some reason we can't set the WRCOMB range with the V1/V2, we
381 * can still possibly use the UNCACHEABLE region for it instead, and help
382 * out in a small way */
383 tdfx_info->mrdesc.mr_flags = MDF_UNCACHEABLE;
384 /* This length of 1000h was taken from the linux device driver... */
385 tdfx_info->mrdesc.mr_len = 0x1000;
386
387 /*
388 * If, for some reason, we can't set the MTRR (N/A?) we may still continue
389 */
390#ifdef DEBUG
391 if(retval == 0) {
392 device_printf(dev, "MTRR Set Type Uncacheable %x\n",
393 (u_int32_t)tdfx_info->mrdesc.mr_base);
394 } else {
395 device_printf(dev, "Couldn't Set MTRR\n");
396 }
397#endif
398 }
399#ifdef DEBUG
400 else {
401 device_printf(dev, "Couldn't Set MTRR\n");
402 return 0;
403 }
404#endif
405 return 0;
406}
407
408static int
409tdfx_open(dev_t dev, int flags, int fmt, struct thread *td)
410{
411 /*
412 * The open cdev method handles open(2) calls to /dev/3dfx[n]
413 * We can pretty much allow any opening of the device.
414 */
415 struct tdfx_softc *tdfx_info = devclass_get_softc(tdfx_devclass,
416 UNIT(minor(dev)));
417 if(tdfx_info->busy != 0) return EBUSY;
418#ifdef DEBUG
419 printf("3dfx: Opened by #%d\n", td->td_proc->p_pid);
420#endif
421 /* Set the driver as busy */
422 tdfx_info->busy++;
423 return 0;
424}
425
426static int
427tdfx_close(dev_t dev, int fflag, int devtype, struct thread *td)
428{
429 /*
430 * The close cdev method handles close(2) calls to /dev/3dfx[n]
431 * We'll always want to close the device when it's called.
432 */
433 struct tdfx_softc *tdfx_info = devclass_get_softc(tdfx_devclass,
434 UNIT(minor(dev)));
435 if(tdfx_info->busy == 0) return EBADF;
436 tdfx_info->busy = 0;
437#ifdef DEBUG
438 printf("Closed by #%d\n", td->td_proc->p_pid);
439#endif
440 return 0;
441}
442
443static int
444tdfx_mmap(dev_t dev, vm_offset_t offset, vm_offset_t *paddr, int nprot)
445{
446 /*
447 * mmap(2) is called by a user process to request that an area of memory
448 * associated with this device be mapped for the process to work with. Nprot
449 * holds the protections requested, PROT_READ, PROT_WRITE, or both.
450 */
451
452 /**** OLD GET CONFIG ****/
453 /* struct tdfx_softc* tdfx_info; */
454
455 /* Get the configuration for our card XXX*/
456 /*tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
457 UNIT(minor(dev)));*/
458 /************************/
459
460 struct tdfx_softc* tdfx_info[2];
461
462 tdfx_info[0] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 0);
463
464 /* If, for some reason, its not configured, we bail out */
465 if(tdfx_info[0] == NULL) {
466#ifdef DEBUG
467 printf("tdfx: tdfx_info (softc) is NULL\n");
468#endif
469 return -1;
470 }
471
472 /* We must stay within the bound of our address space */
473 if((offset & 0xff000000) == tdfx_info[0]->addr0) {
474 offset &= 0xffffff;
475 *paddr = rman_get_start(tdfx_info[0]->memrange) + offset;
476 return 0;
477 }
478
479 if(tdfx_count > 1) {
480 tdfx_info[1] = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, 1);
481 if((offset & 0xff000000) == tdfx_info[1]->addr0) {
482 offset &= 0xffffff;
483 *paddr = rman_get_start(tdfx_info[1]->memrange) +
484 offset;
485 return 0;
486 }
487 }
488
489 /* See if the Banshee/V3 LFB is being requested */
490 /*if(tdfx_info->memrange2 != NULL && (offset & 0xff000000) ==
491 tdfx_info->addr1) {
492 offset &= 0xffffff;
493 return atop(rman_get_start(tdfx_info[1]->memrange2) + offset);
494 }*/ /* VoodooNG code */
495
496 /* The ret call */
497 /* atop -> address to page
498 * rman_get_start, get the (struct resource*)->r_start member,
499 * the mapping base address.
500 */
501 return -1;
502}
503
504static int
505tdfx_query_boards(void) {
506 /*
507 * This returns the number of installed tdfx cards, we have been keeping
508 * count, look at tdfx_attach
509 */
510 return tdfx_count;
511}
512
513static int
514tdfx_query_fetch(u_int cmd, struct tdfx_pio_data *piod)
515{
516 /* XXX Comment this later, after careful inspection and spring cleaning :) */
517 /* Various return values 8bit-32bit */
518 u_int8_t ret_byte;
519 u_int16_t ret_word;
520 u_int32_t ret_dword;
521 struct tdfx_softc* tdfx_info = NULL;
522
523 /* This one depend on the tdfx_* structs being properly initialized */
524
525 /*piod->device &= 0xf;*/
526 if((piod == NULL) ||(tdfx_count <= piod->device) ||
527 (piod->device < 0)) {
528#ifdef DEBUG
529 printf("tdfx: Bad device or internal struct in tdfx_query_fetch\n");
530#endif
531 return -EINVAL;
532 }
533
534 tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
535 piod->device);
536
537 if(tdfx_info == NULL) return -ENXIO;
538
539 /* We must restrict the size reads from the port, since to high or low of a
540 * size witll result in wrong data being passed, and that's bad */
541 /* A few of these were pulled during the attach phase */
542 switch(piod->port) {
543 case PCI_VENDOR_ID_FREEBSD:
544 if(piod->size != 2) return -EINVAL;
545 copyout(&tdfx_info->vendor, piod->value, piod->size);
546 return 0;
547 case PCI_DEVICE_ID_FREEBSD:
548 if(piod->size != 2) return -EINVAL;
549 copyout(&tdfx_info->type, piod->value, piod->size);
550 return 0;
551 case PCI_BASE_ADDRESS_0_FREEBSD:
552 if(piod->size != 4) return -EINVAL;
553 copyout(&tdfx_info->addr0, piod->value, piod->size);
554 return 0;
555 case PCI_BASE_ADDRESS_1_FREEBSD:
556 if(piod->size != 4) return -EINVAL;
557 copyout(&tdfx_info->addr1, piod->value, piod->size);
558 return 0;
559 case PCI_PRIBUS_FREEBSD:
560 if(piod->size != 1) return -EINVAL;
561 break;
562 case PCI_IOBASE_0_FREEBSD:
563 if(piod->size != 2) return -EINVAL;
564 break;
565 case PCI_IOLIMIT_0_FREEBSD:
566 if(piod->size != 2) return -EINVAL;
567 break;
568 case SST1_PCI_SPECIAL1_FREEBSD:
569 if(piod->size != 4) return -EINVAL;
570 break;
571 case PCI_REVISION_ID_FREEBSD:
572 if(piod->size != 1) return -EINVAL;
573 break;
574 case SST1_PCI_SPECIAL4_FREEBSD:
575 if(piod->size != 4) return -EINVAL;
576 break;
577 default:
578 return -EINVAL;
579 }
580
581
582 /* Read the value and return */
583 switch(piod->size) {
584 case 1:
585 ret_byte = pci_read_config(tdfx_info[piod->device].dev,
586 piod->port, 1);
587 copyout(&ret_byte, piod->value, 1);
588 break;
589 case 2:
590 ret_word = pci_read_config(tdfx_info[piod->device].dev,
591 piod->port, 2);
592 copyout(&ret_word, piod->value, 2);
593 break;
594 case 4:
595 ret_dword = pci_read_config(tdfx_info[piod->device].dev,
596 piod->port, 4);
597 copyout(&ret_dword, piod->value, 4);
598 break;
599 default:
600 return -EINVAL;
601 }
602 return 0;
603}
604
605static int
606tdfx_query_update(u_int cmd, struct tdfx_pio_data *piod)
607{
608 /* XXX Comment this later, after careful inspection and spring cleaning :) */
609 /* Return vals */
610 u_int8_t ret_byte;
611 u_int16_t ret_word;
612 u_int32_t ret_dword;
613
614 /* Port vals, mask */
615 u_int32_t retval, preval, mask;
616 struct tdfx_softc* tdfx_info = NULL;
617
618
619 if((piod == NULL) || (piod->device >= (tdfx_count &
620 0xf))) {
621#ifdef DEBUG
622 printf("tdfx: Bad struct or device in tdfx_query_update\n");
623#endif
624 return -EINVAL;
625 }
626
627 tdfx_info = (struct tdfx_softc*)devclass_get_softc(tdfx_devclass,
628 piod->device);
629 if(tdfx_info == NULL) return -ENXIO;
630 /* Code below this line in the fuction was taken from the
631 * Linux driver and converted for freebsd. */
632
633 /* Check the size for all the ports, to make sure stuff doesn't get messed up
634 * by poorly written clients */
635
636 switch(piod->port) {
637 case PCI_COMMAND_FREEBSD:
638 if(piod->size != 2) return -EINVAL;
639 break;
640 case SST1_PCI_SPECIAL1_FREEBSD:
641 if(piod->size != 4) return -EINVAL;
642 break;
643 case SST1_PCI_SPECIAL2_FREEBSD:
644 if(piod->size != 4) return -EINVAL;
645 break;
646 case SST1_PCI_SPECIAL3_FREEBSD:
647 if(piod->size != 4) return -EINVAL;
648 break;
649 case SST1_PCI_SPECIAL4_FREEBSD:
650 if(piod->size != 4) return -EINVAL;
651 break;
652 default:
653 return -EINVAL;
654 }
655 /* Read the current value */
656 retval = pci_read_config(tdfx_info->dev, piod->port & ~3, 4);
657
658 /* These set up a mask to use, since apparently they wanted to write 4 bytes
659 * at once to the ports */
660 switch (piod->size) {
661 case 1:
662 copyin(piod->value, &ret_byte, 1);
663 preval = ret_byte << (8 * (piod->port & 0x3));
664 mask = 0xff << (8 * (piod->port & 0x3));
665 break;
666 case 2:
667 copyin(piod->value, &ret_word, 2);
668 preval = ret_word << (8 * (piod->port & 0x3));
669 mask = 0xffff << (8 * (piod->port & 0x3));
670 break;
671 case 4:
672 copyin(piod->value, &ret_dword, 4);
673 preval = ret_dword;
674 mask = ~0;
675 break;
676 default:
677 return -EINVAL;
678 }
679 /* Finally, combine the values and write it to the port */
680 retval = (retval & ~mask) | preval;
681 pci_write_config(tdfx_info->dev, piod->port & ~3, retval, 4);
682
683 return 0;
684}
685
686/* For both of these, I added a variable named workport of type u_int so
687 * that I could eliminate the warning about my data type size. The
688 * applications expect the port to be of type short, so I needed to change
689 * this within the function */
690static int
691tdfx_do_pio_rd(struct tdfx_pio_data *piod)
692{
693 /* Return val */
694 u_int8_t ret_byte;
695 u_int workport;
696 struct tdfx_softc *tdfx_info =
697 (struct tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
698
699 /* Restricts the access of ports other than those we use */
700 if(((piod->port != VGA_INPUT_STATUS_1C) || (piod->port != SC_INDEX) ||
701 (piod->port != SC_DATA) || (piod->port != VGA_MISC_OUTPUT_READ)) &&
702 (piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
703 return -EPERM;
704
705 /* All VGA STATUS REGS are byte registers, size should never be > 1 */
706 if(piod->size != 1) {
707 return -EINVAL;
708 }
709
710 /* Write the data to the intended port */
711 workport = piod->port;
712 ret_byte = inb(workport);
713 copyout(&ret_byte, piod->value, sizeof(u_int8_t));
714 return 0;
715}
716
717static int
718tdfx_do_pio_wt(struct tdfx_pio_data *piod)
719{
720 /* return val */
721 u_int8_t ret_byte;
722 u_int workport;
723 struct tdfx_softc *tdfx_info = (struct
724 tdfx_softc*)devclass_get_softc(tdfx_devclass, piod->device);
725 /* Replace old switch w/ massive if(...) */
726 /* Restricts the access of ports other than those we use */
727 if(((piod->port != SC_INDEX) && (piod->port != SC_DATA) &&
728 (piod->port != VGA_MISC_OUTPUT_READ)) /* Can't write VGA_ST_1C */ &&
729 (piod->port < tdfx_info->pio0) && (piod->port > tdfx_info->pio0max))
730 return -EPERM;
731
732 /* All VGA STATUS REGS are byte registers, size should never be > 1 */
733 if(piod->size != 1) {
734 return -EINVAL;
735 }
736
737 /* Write the data to the intended port */
738 copyin(piod->value, &ret_byte, sizeof(u_int8_t));
739 workport = piod->port;
740 outb(workport, ret_byte);
741 return 0;
742}
743
744static int
745tdfx_do_query(u_int cmd, struct tdfx_pio_data *piod)
746{
747 /* There are three sub-commands to the query 0x33 */
748 switch(_IOC_NR(cmd)) {
749 case 2:
750 return tdfx_query_boards();
751 break;
752 case 3:
753 return tdfx_query_fetch(cmd, piod);
754 break;
755 case 4:
756 return tdfx_query_update(cmd, piod);
757 break;
758 default:
759 /* In case we are thrown a bogus sub-command! */
760#ifdef DEBUG
761 printf("Bad Sub-cmd: 0x%x\n", _IOC_NR(cmd));
762#endif
763 return -EINVAL;
764 };
765}
766
767static int
768tdfx_do_pio(u_int cmd, struct tdfx_pio_data *piod)
769{
770 /* Two types of PIO, INPUT and OUTPUT, as the name suggests */
771 switch(_IOC_DIR(cmd)) {
772 case IOCV_OUT:
773 return tdfx_do_pio_rd(piod);
774 break;
775 case IOCV_IN:
776 return tdfx_do_pio_wt(piod);
777 break;
778 default:
779 return -EINVAL;
780 };
781}
782
783/* Calls to ioctl(2) eventually end up here. Unhandled ioctls return an ENXIO,
784 * normally, you would read in the data pointed to by data, then write your
785 * output to it. The ioctl *should* normally return zero if everything is
786 * alright, but 3dfx didn't make it that way...
787 *
788 * For all of the ioctl code, in the event of a real error,
789 * we return -Exxxx rather than simply Exxxx. The reason for this
790 * is that the ioctls actually RET information back to the program
791 * sometimes, rather than filling it in the passed structure. We
792 * want to distinguish errors from useful data, and maintain compatibility.
793 *
794 * There is this portion of the proc struct called p_retval[], we can store a
795 * return value in td->td_retval[0] and place the return value if it is positive
796 * in there, then we can return 0 (good). If the return value is negative, we
797 * can return -retval and the error should be properly handled.
798 */
799static int
800tdfx_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
801{
802 int retval = 0;
803 struct tdfx_pio_data *piod = (struct tdfx_pio_data*)data;
804#ifdef DEBUG
805 printf("IOCTL'd by #%d, cmd: 0x%x, data: %p\n", td->td_proc->p_pid, (u_int32_t)cmd,
806 piod);
807#endif
808 switch(_IOC_TYPE(cmd)) {
809 /* Return the real error if negative, or simply stick the valid return
810 * in td->td_retval */
811 case 0x33:
812 /* The '3'(0x33) type IOCTL is for querying the installed cards */
813 if((retval = tdfx_do_query(cmd, piod)) > 0) td->td_retval[0] = retval;
814 else return -retval;
815 break;
816 case 0:
817 /* The 0 type IOCTL is for programmed I/O methods */
818 if((tdfx_do_pio(cmd, piod)) > 0) td->td_retval[0] = retval;
819 else return -retval;
820 break;
821 default:
822 /* Technically, we won't reach this from linux emu, but when glide
823 * finally gets ported, watch out! */
824#ifdef DEBUG
825 printf("Bad IOCTL from #%d\n", td->td_proc->p_pid);
826#endif
827 return ENXIO;
828 }
829
830 return 0;
831}
832
833#ifdef TDFX_LINUX
834/*
835 * Linux emulation IOCTL for /dev/tdfx
836 */
837static int
838linux_ioctl_tdfx(struct thread *td, struct linux_ioctl_args* args)
839{
840 int error = 0;
841 u_long cmd = args->cmd & 0xffff;
842
843 /* The structure passed to ioctl has two shorts, one int
844 and one void*. */
845 char d_pio[2*sizeof(short) + sizeof(int) + sizeof(void*)];
846
847 struct file *fp;
848
849 if ((error = fget(td, args->fd, &fp)) != 0)
850 return (error);
851 /* We simply copy the data and send it right to ioctl */
852 copyin((caddr_t)args->arg, &d_pio, sizeof(d_pio));
853 error = fo_ioctl(fp, cmd, (caddr_t)&d_pio, td->td_ucred, td);
854 fdrop(fp, td);
855 return error;
856}
857#endif /* TDFX_LINUX */
858
859
860/* This is the device driver struct. This is sent to the driver subsystem to
861 * register the method structure and the info strcut space for this particular
862 * instance of the driver.
863 */
864static driver_t tdfx_driver = {
865 "tdfx",
866 tdfx_methods,
867 sizeof(struct tdfx_softc),
868};
869
870/* Tell Mr. Kernel about us! */
871DRIVER_MODULE(tdfx, pci, tdfx_driver, tdfx_devclass, 0, 0);