195584Sanholt/* mga_drv.c -- Matrox G200/G400 driver -*- linux-c -*-
2145132Sanholt * Created: Mon Dec 13 01:56:22 1999 by jhartmann@precisioninsight.com
3145132Sanholt */
4139749Simp/*-
595584Sanholt * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
695584Sanholt * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
795584Sanholt * All Rights Reserved.
895584Sanholt *
995584Sanholt * Permission is hereby granted, free of charge, to any person obtaining a
1095584Sanholt * copy of this software and associated documentation files (the "Software"),
1195584Sanholt * to deal in the Software without restriction, including without limitation
1295584Sanholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1395584Sanholt * and/or sell copies of the Software, and to permit persons to whom the
1495584Sanholt * Software is furnished to do so, subject to the following conditions:
1595584Sanholt *
1695584Sanholt * The above copyright notice and this permission notice (including the next
1795584Sanholt * paragraph) shall be included in all copies or substantial portions of the
1895584Sanholt * Software.
1995584Sanholt *
2095584Sanholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2195584Sanholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2295584Sanholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2395584Sanholt * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2495584Sanholt * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2595584Sanholt * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2695584Sanholt * OTHER DEALINGS IN THE SOFTWARE.
2795584Sanholt *
2895584Sanholt * Authors:
2995584Sanholt *    Rickard E. (Rik) Faith <faith@valinux.com>
3095584Sanholt *    Gareth Hughes <gareth@valinux.com>
3195584Sanholt *
3295584Sanholt */
3395584Sanholt
34152909Sanholt#include <sys/cdefs.h>
35152909Sanholt__FBSDID("$FreeBSD$");
36152909Sanholt
3795584Sanholt#include "dev/drm/drmP.h"
38112015Sanholt#include "dev/drm/drm.h"
3995746Sanholt#include "dev/drm/mga_drm.h"
4095584Sanholt#include "dev/drm/mga_drv.h"
41145132Sanholt#include "dev/drm/drm_pciids.h"
4295584Sanholt
43145132Sanholt/* drv_PCI_IDs comes from drm_pciids.h, generated from drm_pciids.txt. */
44145132Sanholtstatic drm_pci_id_list_t mga_pciidlist[] = {
45145132Sanholt	mga_PCI_IDS
46145132Sanholt};
4795584Sanholt
48152909Sanholt/**
49152909Sanholt * Determine if the device really is AGP or not.
50152909Sanholt *
51152909Sanholt * In addition to the usual tests performed by \c drm_device_is_agp, this
52152909Sanholt * function detects PCI G450 cards that appear to the system exactly like
53152909Sanholt * AGP G450 cards.
54152909Sanholt *
55152909Sanholt * \param dev   The device to be tested.
56152909Sanholt *
57152909Sanholt * \returns
58152909Sanholt * If the device is a PCI G450, zero is returned.  Otherwise non-zero is
59152909Sanholt * returned.
60152909Sanholt *
61152909Sanholt * \bug
62152909Sanholt * This function needs to be filled in!  The implementation in
63152909Sanholt * linux-core/mga_drv.c shows what needs to be done.
64152909Sanholt */
65182080Srnolandstatic int mga_driver_device_is_agp(struct drm_device * dev)
66152909Sanholt{
67153579Sjhb	device_t bus;
68153579Sjhb
69152909Sanholt	/* There are PCI versions of the G450.  These cards have the
70152909Sanholt	 * same PCI ID as the AGP G450, but have an additional PCI-to-PCI
71152909Sanholt	 * bridge chip.  We detect these cards, which are not currently
72152909Sanholt	 * supported by this driver, by looking at the device ID of the
73152909Sanholt	 * bus the "card" is on.  If vendor is 0x3388 (Hint Corp) and the
74152909Sanholt	 * device is 0x0021 (HB6 Universal PCI-PCI bridge), we reject the
75152909Sanholt	 * device.
76152909Sanholt	 */
77153579Sjhb	bus = device_get_parent(device_get_parent(dev->device));
78152909Sanholt	if (pci_get_device(dev->device) == 0x0525 &&
79153579Sjhb	    pci_get_vendor(bus) == 0x3388 &&
80153579Sjhb	    pci_get_device(bus) == 0x0021)
81158683Sanholt		return DRM_IS_NOT_AGP;
82152909Sanholt	else
83158683Sanholt		return DRM_MIGHT_BE_AGP;
84152909Sanholt}
85145132Sanholt
86182080Srnolandstatic void mga_configure(struct drm_device *dev)
87145132Sanholt{
88183573Srnoland	dev->driver->driver_features =
89183573Srnoland	    DRIVER_USE_AGP | DRIVER_REQUIRE_AGP | DRIVER_USE_MTRR |
90183573Srnoland	    DRIVER_HAVE_DMA | DRIVER_HAVE_IRQ;
91145132Sanholt
92183573Srnoland	dev->driver->buf_priv_size	= sizeof(drm_mga_buf_priv_t);
93183573Srnoland	dev->driver->load		= mga_driver_load;
94183573Srnoland	dev->driver->unload		= mga_driver_unload;
95183573Srnoland	dev->driver->lastclose		= mga_driver_lastclose;
96183573Srnoland	dev->driver->get_vblank_counter	= mga_get_vblank_counter;
97183573Srnoland	dev->driver->enable_vblank	= mga_enable_vblank;
98183573Srnoland	dev->driver->disable_vblank	= mga_disable_vblank;
99183573Srnoland	dev->driver->irq_preinstall	= mga_driver_irq_preinstall;
100183573Srnoland	dev->driver->irq_postinstall	= mga_driver_irq_postinstall;
101183573Srnoland	dev->driver->irq_uninstall	= mga_driver_irq_uninstall;
102183573Srnoland	dev->driver->irq_handler	= mga_driver_irq_handler;
103183573Srnoland	dev->driver->dma_ioctl		= mga_dma_buffers;
104183573Srnoland	dev->driver->dma_quiescent	= mga_driver_dma_quiescent;
105183573Srnoland	dev->driver->device_is_agp	= mga_driver_device_is_agp;
106145132Sanholt
107183573Srnoland	dev->driver->ioctls		= mga_ioctls;
108183573Srnoland	dev->driver->max_ioctl		= mga_max_ioctl;
109145132Sanholt
110183573Srnoland	dev->driver->name		= DRIVER_NAME;
111183573Srnoland	dev->driver->desc		= DRIVER_DESC;
112183573Srnoland	dev->driver->date		= DRIVER_DATE;
113183573Srnoland	dev->driver->major		= DRIVER_MAJOR;
114183573Srnoland	dev->driver->minor		= DRIVER_MINOR;
115183573Srnoland	dev->driver->patchlevel		= DRIVER_PATCHLEVEL;
116145132Sanholt}
117145132Sanholt
118145132Sanholtstatic int
119189563Srnolandmga_probe(device_t kdev)
120145132Sanholt{
121189563Srnoland	return drm_probe(kdev, mga_pciidlist);
122145132Sanholt}
123145132Sanholt
124145132Sanholtstatic int
125189563Srnolandmga_attach(device_t kdev)
126145132Sanholt{
127189563Srnoland	struct drm_device *dev = device_get_softc(kdev);
128145132Sanholt
129183833Srnoland	dev->driver = malloc(sizeof(struct drm_driver_info), DRM_MEM_DRIVER,
130183604Srnoland	    M_WAITOK | M_ZERO);
131183604Srnoland
132145132Sanholt	mga_configure(dev);
133183573Srnoland
134189563Srnoland	return drm_attach(kdev, mga_pciidlist);
135145132Sanholt}
136145132Sanholt
137183573Srnolandstatic int
138189563Srnolandmga_detach(device_t kdev)
139183573Srnoland{
140189563Srnoland	struct drm_device *dev = device_get_softc(kdev);
141183573Srnoland	int ret;
142183573Srnoland
143189563Srnoland	ret = drm_detach(kdev);
144183573Srnoland
145183833Srnoland	free(dev->driver, DRM_MEM_DRIVER);
146183573Srnoland
147183573Srnoland	return ret;
148183573Srnoland}
149183573Srnoland
150145132Sanholtstatic device_method_t mga_methods[] = {
151145132Sanholt	/* Device interface */
152145132Sanholt	DEVMETHOD(device_probe,		mga_probe),
153145132Sanholt	DEVMETHOD(device_attach,	mga_attach),
154183573Srnoland	DEVMETHOD(device_detach,	mga_detach),
155145132Sanholt
156145132Sanholt	{ 0, 0 }
157145132Sanholt};
158145132Sanholt
159145132Sanholtstatic driver_t mga_driver = {
160145132Sanholt	"drm",
161145132Sanholt	mga_methods,
162182080Srnoland	sizeof(struct drm_device)
163145132Sanholt};
164145132Sanholt
165145132Sanholtextern devclass_t drm_devclass;
166153579SjhbDRIVER_MODULE(mga, vgapci, mga_driver, drm_devclass, 0, 0);
167145132SanholtMODULE_DEPEND(mga, drm, 1, 1, 1);
168