1/* mga_drv.c -- Matrox G200/G400 driver -*- linux-c -*-
2 * Created: Mon Dec 13 01:56:22 1999 by jhartmann@precisioninsight.com
3 */
4/*-
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 *    Rickard E. (Rik) Faith <faith@valinux.com>
30 *    Gareth Hughes <gareth@valinux.com>
31 *
32 */
33
34#include "drmP.h"
35#include "drm.h"
36#include "mga_drm.h"
37#include "mga_drv.h"
38#include "drm_pciids.h"
39
40#ifdef __NetBSD__
41#include <dev/pci/pcivar.h>
42#include <dev/pci/pcidevs.h>
43#endif
44
45/* drv_PCI_IDs comes from drm_pciids.h, generated from drm_pciids.txt. */
46static drm_pci_id_list_t mga_pciidlist[] = {
47	mga_PCI_IDS
48};
49
50#ifdef __NetBSD__
51static int mgadev_match(const struct pci_attach_args *pa);
52static int
53mgadev_match(const struct pci_attach_args *pa)
54{
55
56	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_HINT &&
57	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_HINT_HB1)
58		return 1;
59	return 0;
60}
61#endif
62
63
64/**
65 * Determine if the device really is AGP or not.
66 *
67 * In addition to the usual tests performed by \c drm_device_is_agp, this
68 * function detects PCI G450 cards that appear to the system exactly like
69 * AGP G450 cards.
70 *
71 * \param dev   The device to be tested.
72 *
73 * \returns
74 * If the device is a PCI G450, zero is returned.  Otherwise non-zero is
75 * returned.
76 *
77 * \bug
78 * This function needs to be filled in!  The implementation in
79 * linux-core/mga_drv.c shows what needs to be done.
80 */
81static int mga_driver_device_is_agp(struct drm_device * dev)
82{
83	/* There are PCI versions of the G450.  These cards have the
84	 * same PCI ID as the AGP G450, but have an additional PCI-to-PCI
85	 * bridge chip.  We detect these cards, which are not currently
86	 * supported by this driver, by looking at the device ID of the
87	 * bus the "card" is on.  If vendor is 0x3388 (Hint Corp) and the
88	 * device is 0x0021 (HB6 Universal PCI-PCI bridge), we reject the
89	 * device.
90	 */
91#if defined(__FreeBSD__)
92	device_t bus;
93
94#if __FreeBSD_version >= 700010
95	bus = device_get_parent(device_get_parent(dev->device));
96#else
97	bus = device_get_parent(dev->device);
98#endif
99	if (pci_get_device(dev->device) == 0x0525 &&
100	    pci_get_vendor(bus) == 0x3388 &&
101	    pci_get_device(bus) == 0x0021)
102#else
103	struct pci_attach_args pa;
104
105	if (PCI_PRODUCT(dev->pa.pa_id) == PCI_PRODUCT_MATROX_G400_AGP &&
106	    pci_find_device(&pa, mgadev_match))
107#endif
108		return DRM_IS_NOT_AGP;
109	else
110		return DRM_MIGHT_BE_AGP;
111}
112
113static void mga_configure(struct drm_device *dev)
114{
115	dev->driver->driver_features =
116	    DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | DRIVER_USE_MTRR |
117	    DRIVER_HAVE_DMA | DRIVER_HAVE_IRQ;
118
119	dev->driver->buf_priv_size	= sizeof(drm_mga_buf_priv_t);
120	dev->driver->load		= mga_driver_load;
121	dev->driver->unload		= mga_driver_unload;
122	dev->driver->lastclose		= mga_driver_lastclose;
123	dev->driver->get_vblank_counter	= mga_get_vblank_counter;
124	dev->driver->enable_vblank	= mga_enable_vblank;
125	dev->driver->disable_vblank	= mga_disable_vblank;
126	dev->driver->irq_preinstall	= mga_driver_irq_preinstall;
127	dev->driver->irq_postinstall	= mga_driver_irq_postinstall;
128	dev->driver->irq_uninstall	= mga_driver_irq_uninstall;
129	dev->driver->irq_handler	= mga_driver_irq_handler;
130	dev->driver->dma_ioctl		= mga_dma_buffers;
131	dev->driver->dma_quiescent	= mga_driver_dma_quiescent;
132	dev->driver->device_is_agp	= mga_driver_device_is_agp;
133
134	dev->driver->ioctls		= mga_ioctls;
135	dev->driver->max_ioctl		= mga_max_ioctl;
136
137	dev->driver->name		= DRIVER_NAME;
138	dev->driver->desc		= DRIVER_DESC;
139	dev->driver->date		= DRIVER_DATE;
140	dev->driver->major		= DRIVER_MAJOR;
141	dev->driver->minor		= DRIVER_MINOR;
142	dev->driver->patchlevel		= DRIVER_PATCHLEVEL;
143}
144
145#if defined(__FreeBSD__)
146
147static int
148mga_probe(device_t kdev)
149{
150	return drm_probe(kdev, mga_pciidlist);
151}
152
153static int
154mga_attach(device_t kdev)
155{
156	struct drm_device *dev = device_get_softc(kdev);
157
158	dev->driver = malloc(sizeof(struct drm_driver_info), DRM_MEM_DRIVER,
159	    M_WAITOK | M_ZERO);
160
161	mga_configure(dev);
162
163	return drm_attach(kdev, mga_pciidlist);
164}
165
166static int
167mga_detach(device_t kdev)
168{
169	struct drm_device *dev = device_get_softc(kdev);
170	int ret;
171
172	ret = drm_detach(kdev);
173
174	free(dev->driver, DRM_MEM_DRIVER);
175
176	return ret;
177}
178
179static device_method_t mga_methods[] = {
180	/* Device interface */
181	DEVMETHOD(device_probe,		mga_probe),
182	DEVMETHOD(device_attach,	mga_attach),
183	DEVMETHOD(device_detach,	mga_detach),
184
185	{ 0, 0 }
186};
187
188static driver_t mga_driver = {
189	"drm",
190	mga_methods,
191	sizeof(struct drm_device)
192};
193
194extern devclass_t drm_devclass;
195#if __FreeBSD_version >= 700010
196DRIVER_MODULE(mga, vgapci, mga_driver, drm_devclass, 0, 0);
197#else
198DRIVER_MODULE(mga, pci, mga_driver, drm_devclass, 0, 0);
199#endif
200MODULE_DEPEND(mga, drm, 1, 1, 1);
201
202#elif   defined(__NetBSD__)
203
204static int
205mgadrm_probe(device_t parent, cfdata_t match, void *aux)
206{
207	struct pci_attach_args *pa = aux;
208
209	return drm_probe(pa, mga_pciidlist);
210}
211
212static void
213mgadrm_attach(device_t parent, device_t self, void *aux)
214{
215	struct pci_attach_args *pa = aux;
216	struct drm_device *dev = device_private(self);
217
218	dev->driver = malloc(sizeof(struct drm_driver_info), DRM_MEM_DRIVER,
219	    M_WAITOK | M_ZERO);
220
221	mga_configure(dev);
222
223	drm_attach(self, pa, mga_pciidlist);
224}
225
226CFATTACH_DECL_NEW(mgadrm, sizeof(struct drm_device),
227    mgadrm_probe, mgadrm_attach, drm_detach, NULL);
228
229MODULE(MODULE_CLASS_DRIVER, mgadrm, "drm");
230
231#ifdef _MODULE
232#include "ioconf.c"
233#endif
234
235static int
236mgadrm_modcmd(modcmd_t cmd, void *arg)
237{
238	int error = 0;
239
240	switch (cmd) {
241	case MODULE_CMD_INIT:
242#ifdef _MODULE
243		error = config_init_component(cfdriver_ioconf_mgadrm,
244		    cfattach_ioconf_mgadrm, cfdata_ioconf_mgadrm);
245#endif
246		break;
247	case MODULE_CMD_FINI:
248#ifdef _MODULE
249		error = config_fini_component(cfdriver_ioconf_mgadrm,
250		    cfattach_ioconf_mgadrm, cfdata_ioconf_mgadrm);
251#endif
252		break;
253	default:
254		return ENOTTY;
255	}
256
257	return error;
258}
259
260#endif
261