summaryrefslogtreecommitdiff
path: root/sys/dev/pci/drm/savage_drv.c
blob: 8ba435653fd3ebf2ad5af426b8c47ebfaa07b1a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* savage_drv.c -- Savage DRI driver
 */
/*-
 * Copyright 2005 Eric Anholt
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <anholt@FreeBSD.org>
 */

#include "drmP.h"
#include "drm.h"
#include "savage_drm.h"
#include "savage_drv.h"

int	savagedrm_probe(struct device *, void *, void *);
void	savagedrm_attach(struct device *, struct device *, void *);
int	savagedrm_detach(struct device *, int);
int	savagedrm_ioctl(struct drm_device *, u_long, caddr_t, struct drm_file *);

const static struct drm_pcidev savagedrm_pciidlist[] = {
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE3D, S3_SAVAGE3D},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE3D_M, S3_SAVAGE3D},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE4, S3_SAVAGE4},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE4_2, S3_SAVAGE4},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE_MXMV, S3_SAVAGE_MX},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE_MX, S3_SAVAGE_MX},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE_IXMV, S3_SAVAGE_MX},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SAVAGE_IX, S3_SAVAGE_MX},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_MX128, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_MX64, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_MX64C, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_IX128SDR, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_IX128DDR, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_IX64SDR, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_IX64DDR, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_IXCSDR, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_SUPERSAVAGE_IXCDDR, S3_SUPERSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_PROSAVAGE_PM133, S3_PROSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_PROSAVAGE_KM133, S3_PROSAVAGE},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_TWISTER, S3_TWISTER},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_TWISTER_K, S3_TWISTER},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_PROSAVAGE_DDR, S3_PROSAVAGEDDR},
	{PCI_VENDOR_S3, PCI_PRODUCT_S3_PROSAVAGE_DDR_K, S3_PROSAVAGEDDR},
	{0, 0, 0}
};

static const struct drm_driver_info savagedrm_driver = {
	.buf_priv_size		= sizeof(struct savagedrm_buf_priv),
	.firstopen		= savage_driver_firstopen,
	.lastclose		= savage_driver_lastclose,
	.reclaim_buffers_locked = savage_reclaim_buffers,
	.dma_ioctl		= savage_bci_buffers,

	.name			= DRIVER_NAME,
	.desc			= DRIVER_DESC,
	.date			= DRIVER_DATE,
	.major			= DRIVER_MAJOR,
	.minor			= DRIVER_MINOR,
	.patchlevel		= DRIVER_PATCHLEVEL,

	.flags			= DRIVER_AGP | DRIVER_MTRR | DRIVER_PCI_DMA |
				    DRIVER_DMA,
};

int
savagedrm_probe(struct device *parent, void *match, void *aux)
{
	return drm_pciprobe((struct pci_attach_args *)aux, savagedrm_pciidlist);
}

void
savagedrm_attach(struct device *parent, struct device *self, void *aux)
{
	drm_savage_private_t	*dev_priv = (drm_savage_private_t *)self;
	struct pci_attach_args	*pa = aux;
	struct vga_pci_bar	*bar;
	const struct drm_pcidev	*id_entry;
	unsigned long		 mmio_base;
	int			 is_agp;

	id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
	    PCI_PRODUCT(pa->pa_id), savagedrm_pciidlist);
	dev_priv->chipset = (enum savage_family)id_entry->driver_private;

	if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
		bar = vga_pci_bar_info((struct vga_pci_softc *)parent, 0);	
		if (bar == NULL) {
			printf(": can't find fb info\n");
			return;
		}
		dev_priv->fb_base = bar->base;
		dev_priv->fb_size = SAVAGE_FB_SIZE_S3;
		mmio_base = dev_priv->fb_base + dev_priv->fb_size;
		dev_priv->aperture_base = dev_priv->fb_base +
		    SAVAGE_APERTURE_OFFSET;
		/* this should always be true */
		if (bar->maxsize != 0x08000000) {
			printf(": strange pci resource len $08lx\n",
			    bar->maxsize);
			return;
		}
	} else if (dev_priv->chipset != S3_SUPERSAVAGE &&
		   dev_priv->chipset != S3_SAVAGE2000) {
		bar = vga_pci_bar_info((struct vga_pci_softc *)parent, 0);	
		if (bar == NULL) {
			printf(": can't find mmio info\n");
			return;
		}
		mmio_base = bar->base;

		bar = vga_pci_bar_info((struct vga_pci_softc *)parent, 1);	
		if (bar == NULL) {
			printf(": can't find fb info\n");
			return;
		}
		dev_priv->fb_base = bar->base;
		dev_priv->fb_size = SAVAGE_FB_SIZE_S4;
		dev_priv->aperture_base = dev_priv->fb_base +
		    SAVAGE_APERTURE_OFFSET;
		/* this should always be true */
		if (bar->maxsize != 0x08000000) {
			printf(": strange pci resource len $08lx\n",
			    bar->maxsize);
			return;
		}
	} else {
		bar = vga_pci_bar_info((struct vga_pci_softc *)parent, 0);	
		if (bar == NULL) {
			printf(": can't find mmio info\n");
			return;
		}
		mmio_base = bar->base;
		bar = vga_pci_bar_info((struct vga_pci_softc *)parent, 1);	
		if (bar == NULL) {
			printf(": can't find fb info\n");
			return;
		}
		dev_priv->fb_base = bar->base;
		dev_priv->fb_size = bar->maxsize;
		bar = vga_pci_bar_info((struct vga_pci_softc *)parent, 2);	
		if (bar == NULL) {
			printf(": can't find aperture info\n");
			return;
		}
		dev_priv->aperture_base = bar->base;
	}

	if (bus_space_map(pa->pa_memt, mmio_base, SAVAGE_MMIO_SIZE,
	    BUS_SPACE_MAP_LINEAR, &dev_priv->bsh) != 0) {
		printf(": can't map mmio space\n");
		return;
	}
	dev_priv->bst = pa->pa_memt;

	is_agp = pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP,
	    NULL, NULL);
	printf("\n");

	dev_priv->drmdev = drm_attach_pci(&savagedrm_driver, pa, is_agp, self);
}

int
savagedrm_detach(struct device *self, int flags)
{
	drm_savage_private_t	*dev_priv = (drm_savage_private_t *)self;

	if (dev_priv->drmdev != NULL) {
		config_detach(dev_priv->drmdev, flags);
		dev_priv->drmdev = NULL;
	}

	bus_space_unmap(dev_priv->bst, dev_priv->bsh, SAVAGE_MMIO_SIZE);

	return (0);
}

struct cfattach savagedrm_ca = {
	sizeof(drm_savage_private_t), savagedrm_probe, savagedrm_attach,
	savagedrm_detach
};

struct cfdriver savagedrm_cd = {
	0, "savagedrm", DV_DULL
};

int
savagedrm_ioctl(struct drm_device *dev, u_long cmd, caddr_t data,
    struct drm_file *file_priv)
{
	if (file_priv->authenticated == 1) {
		switch (cmd) {
		case DRM_IOCTL_SAVAGE_CMDBUF:
			return (savage_bci_cmdbuf(dev, data, file_priv));
		case DRM_IOCTL_SAVAGE_EVENT_EMIT:
			return (savage_bci_event_emit(dev, data, file_priv));
		case DRM_IOCTL_SAVAGE_EVENT_WAIT:
			return (savage_bci_event_wait(dev, data, file_priv));
		}
	}

	if (file_priv->master == 1) {
		switch (cmd) {
		case DRM_IOCTL_SAVAGE_INIT:
			return (savage_bci_init(dev, data, file_priv));
		}
	}
	return (EINVAL);
}