xref: /freebsd/sys/dev/agp/agp_ati.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005 Eric Anholt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * Based on reading the Linux 2.6.8.1 driver by Dave Jones.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 
42 #include <dev/agp/agppriv.h>
43 #include <dev/agp/agpreg.h>
44 #include <dev/pci/pcivar.h>
45 #include <dev/pci/pcireg.h>
46 
47 #include <vm/vm.h>
48 #include <vm/vm_extern.h>
49 #include <vm/vm_kern.h>
50 #include <vm/vm_object.h>
51 #include <vm/pmap.h>
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #include <sys/rman.h>
55 
56 MALLOC_DECLARE(M_AGP);
57 
58 #define READ4(off)	bus_space_read_4(sc->bst, sc->bsh, off)
59 #define WRITE4(off,v)	bus_space_write_4(sc->bst, sc->bsh, off, v)
60 
61 struct agp_ati_softc {
62 	struct agp_softc agp;
63 	struct resource *regs;	/* memory mapped control registers */
64 	bus_space_tag_t bst;	/* bus_space tag */
65 	bus_space_handle_t bsh;	/* bus_space handle */
66 	u_int32_t	initial_aperture; /* aperture size at startup */
67 	char		is_rs300;
68 
69 	/* The GATT */
70 	u_int32_t	ag_entries;
71 	u_int32_t      *ag_virtual;	/* virtual address of gatt */
72 	u_int32_t      *ag_vdir;	/* virtual address of page dir */
73 	vm_offset_t	ag_pdir;	/* physical address of page dir */
74 };
75 
76 static const char*
77 agp_ati_match(device_t dev)
78 {
79 	if (pci_get_class(dev) != PCIC_BRIDGE ||
80 	    pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
81 		return NULL;
82 
83 	if (agp_find_caps(dev) == 0)
84 		return NULL;
85 
86 	switch (pci_get_devid(dev)) {
87 	case 0xcab01002:
88 		return ("ATI RS100 AGP bridge");
89 	case 0xcab21002:
90 		return ("ATI RS200 AGP bridge");
91 	case 0xcbb21002:
92 		return ("ATI RS200M AGP bridge");
93 	case 0xcab31002:
94 		return ("ATI RS250 AGP bridge");
95 	case 0x58301002:
96 		return ("ATI RS300_100 AGP bridge");
97 	case 0x58311002:
98 		return ("ATI RS300_133 AGP bridge");
99 	case 0x58321002:
100 		return ("ATI RS300_166 AGP bridge");
101 	case 0x58331002:
102 		return ("ATI RS300_200 AGP bridge");
103 	}
104 
105 	return NULL;
106 }
107 
108 static int
109 agp_ati_probe(device_t dev)
110 {
111 	const char *desc;
112 
113 	desc = agp_ati_match(dev);
114 	if (desc) {
115 		device_set_desc(dev, desc);
116 		return 0;
117 	}
118 
119 	return ENXIO;
120 }
121 
122 static int
123 agp_ati_alloc_gatt(device_t dev)
124 {
125 	struct agp_ati_softc *sc = device_get_softc(dev);
126 	u_int32_t apsize = AGP_GET_APERTURE(dev);
127 	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
128 	u_int32_t apbase_offset;
129 	int i;
130 
131 	/* Alloc the GATT -- pointers to pages of AGP memory */
132 	sc->ag_entries = entries;
133 	sc->ag_virtual = kmem_alloc_attr(entries * sizeof(uint32_t),
134 	    M_NOWAIT | M_ZERO, 0, ~0, VM_MEMATTR_WRITE_COMBINING);
135 	if (sc->ag_virtual == NULL) {
136 		if (bootverbose)
137 			device_printf(dev, "GATT allocation failed\n");
138 		return ENOMEM;
139 	}
140 
141 	/* Alloc the page directory -- pointers to each page of the GATT */
142 	sc->ag_vdir = kmem_alloc_attr(AGP_PAGE_SIZE, M_NOWAIT | M_ZERO,
143 	    0, ~0, VM_MEMATTR_WRITE_COMBINING);
144 	if (sc->ag_vdir == NULL) {
145 		if (bootverbose)
146 			device_printf(dev, "pagedir allocation failed\n");
147 		kmem_free(sc->ag_virtual, entries * sizeof(uint32_t));
148 		return ENOMEM;
149 	}
150 	sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
151 
152 	apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
153 	/* Fill in the pagedir's pointers to GATT pages */
154 	for (i = 0; i < sc->ag_entries / 1024; i++) {
155 		vm_offset_t va;
156 		vm_offset_t pa;
157 
158 		va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
159 		pa = vtophys(va);
160 		sc->ag_vdir[apbase_offset + i] = pa | 1;
161 	}
162 
163 	return 0;
164 }
165 
166 static int
167 agp_ati_attach(device_t dev)
168 {
169 	struct agp_ati_softc *sc = device_get_softc(dev);
170 	int error, rid;
171 	u_int32_t temp;
172 	u_int32_t apsize_reg, agpmode_reg;
173 
174 	error = agp_generic_attach(dev);
175 	if (error)
176 		return error;
177 
178 	switch (pci_get_devid(dev)) {
179 	case 0xcab01002: /* ATI RS100 AGP bridge */
180 	case 0xcab21002: /* ATI RS200 AGP bridge */
181 	case 0xcbb21002: /* ATI RS200M AGP bridge */
182 	case 0xcab31002: /* ATI RS250 AGP bridge */
183 		sc->is_rs300 = 0;
184 		apsize_reg = ATI_RS100_APSIZE;
185 		agpmode_reg = ATI_RS100_IG_AGPMODE;
186 		break;
187 	case 0x58301002: /* ATI RS300_100 AGP bridge */
188 	case 0x58311002: /* ATI RS300_133 AGP bridge */
189 	case 0x58321002: /* ATI RS300_166 AGP bridge */
190 	case 0x58331002: /* ATI RS300_200 AGP bridge */
191 		sc->is_rs300 = 1;
192 		apsize_reg = ATI_RS300_APSIZE;
193 		agpmode_reg = ATI_RS300_IG_AGPMODE;
194 		break;
195 	default:
196 		/* Unknown chipset */
197 		return EINVAL;
198 	}
199 
200 	rid = ATI_GART_MMADDR;
201 	sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
202 	if (!sc->regs) {
203 		agp_generic_detach(dev);
204 		return ENOMEM;
205 	}
206 
207 	sc->bst = rman_get_bustag(sc->regs);
208 	sc->bsh = rman_get_bushandle(sc->regs);
209 
210 	sc->initial_aperture = AGP_GET_APERTURE(dev);
211 
212 	for (;;) {
213 		if (agp_ati_alloc_gatt(dev) == 0)
214 			break;
215 
216 		/*
217 		 * Probably contigmalloc failure. Try reducing the
218 		 * aperture so that the gatt size reduces.
219 		 */
220 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
221 			return ENOMEM;
222 	}
223 
224 	temp = pci_read_config(dev, apsize_reg, 4);
225 	pci_write_config(dev, apsize_reg, temp | 1, 4);
226 
227 	pci_write_config(dev, agpmode_reg, 0x20000, 4);
228 
229 	WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
230 
231 	temp = pci_read_config(dev, 4, 4);	/* XXX: Magic reg# */
232 	pci_write_config(dev, 4, temp | (1 << 14), 4);
233 
234 	WRITE4(ATI_GART_BASE, sc->ag_pdir);
235 
236 	AGP_FLUSH_TLB(dev);
237 
238 	return 0;
239 }
240 
241 static int
242 agp_ati_detach(device_t dev)
243 {
244 	struct agp_ati_softc *sc = device_get_softc(dev);
245 	u_int32_t apsize_reg, temp;
246 
247 	agp_free_cdev(dev);
248 
249 	if (sc->is_rs300)
250 		apsize_reg = ATI_RS300_APSIZE;
251 	else
252 		apsize_reg = ATI_RS100_APSIZE;
253 
254 	/* Clear the GATT base */
255 	WRITE4(ATI_GART_BASE, 0);
256 
257 	/* Put the aperture back the way it started. */
258 	AGP_SET_APERTURE(dev, sc->initial_aperture);
259 
260 	temp = pci_read_config(dev, apsize_reg, 4);
261 	pci_write_config(dev, apsize_reg, temp & ~1, 4);
262 
263 	kmem_free(sc->ag_vdir, AGP_PAGE_SIZE);
264 	kmem_free(sc->ag_virtual, sc->ag_entries * sizeof(uint32_t));
265 
266 	bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
267 	agp_free_res(dev);
268 
269 	return 0;
270 }
271 
272 static u_int32_t
273 agp_ati_get_aperture(device_t dev)
274 {
275 	struct agp_ati_softc *sc = device_get_softc(dev);
276 	int size_value;
277 
278 	if (sc->is_rs300)
279 		size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
280 	else
281 		size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
282 
283 	size_value = (size_value & 0x0000000e) >> 1;
284 	size_value = (32 * 1024 * 1024) << size_value;
285 
286 	return size_value;
287 }
288 
289 static int
290 agp_ati_set_aperture(device_t dev, u_int32_t aperture)
291 {
292 	struct agp_ati_softc *sc = device_get_softc(dev);
293 	int size_value;
294 	u_int32_t apsize_reg;
295 
296 	if (sc->is_rs300)
297 		apsize_reg = ATI_RS300_APSIZE;
298 	else
299 		apsize_reg = ATI_RS100_APSIZE;
300 
301 	size_value = pci_read_config(dev, apsize_reg, 4);
302 
303 	size_value &= ~0x0000000e;
304 	size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
305 
306 	pci_write_config(dev, apsize_reg, size_value, 4);
307 
308 	return 0;
309 }
310 
311 static int
312 agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
313 {
314 	struct agp_ati_softc *sc = device_get_softc(dev);
315 
316 	if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
317 		return EINVAL;
318 
319 	sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
320 
321 	return 0;
322 }
323 
324 static int
325 agp_ati_unbind_page(device_t dev, vm_offset_t offset)
326 {
327 	struct agp_ati_softc *sc = device_get_softc(dev);
328 
329 	if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
330 		return EINVAL;
331 
332 	sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
333 	return 0;
334 }
335 
336 static void
337 agp_ati_flush_tlb(device_t dev)
338 {
339 	struct agp_ati_softc *sc = device_get_softc(dev);
340 
341 	/* Set the cache invalidate bit and wait for the chipset to clear */
342 	WRITE4(ATI_GART_CACHE_CNTRL, 1);
343 	(void)READ4(ATI_GART_CACHE_CNTRL);
344 }
345 
346 static device_method_t agp_ati_methods[] = {
347 	/* Device interface */
348 	DEVMETHOD(device_probe,		agp_ati_probe),
349 	DEVMETHOD(device_attach,	agp_ati_attach),
350 	DEVMETHOD(device_detach,	agp_ati_detach),
351 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
352 	DEVMETHOD(device_suspend,	bus_generic_suspend),
353 	DEVMETHOD(device_resume,	bus_generic_resume),
354 
355 	/* AGP interface */
356 	DEVMETHOD(agp_get_aperture,	agp_ati_get_aperture),
357 	DEVMETHOD(agp_set_aperture,	agp_ati_set_aperture),
358 	DEVMETHOD(agp_bind_page,	agp_ati_bind_page),
359 	DEVMETHOD(agp_unbind_page,	agp_ati_unbind_page),
360 	DEVMETHOD(agp_flush_tlb,	agp_ati_flush_tlb),
361 	DEVMETHOD(agp_enable,		agp_generic_enable),
362 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
363 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
364 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
365 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
366 	{ 0, 0 }
367 };
368 
369 static driver_t agp_ati_driver = {
370 	"agp",
371 	agp_ati_methods,
372 	sizeof(struct agp_ati_softc),
373 };
374 
375 DRIVER_MODULE(agp_ati, hostb, agp_ati_driver, 0, 0);
376 MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
377 MODULE_DEPEND(agp_ati, pci, 1, 1, 1);
378