xref: /freebsd-13-stable/sys/dev/agp/agp_ati.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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*
agp_ati_match(device_t dev)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
agp_ati_probe(device_t dev)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
agp_ati_alloc_gatt(device_t dev)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 = (void *)kmem_alloc_attr(entries * sizeof(u_int32_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 = (void *)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((vm_offset_t)sc->ag_virtual, entries *
148 		    sizeof(u_int32_t));
149 		return ENOMEM;
150 	}
151 	sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
152 
153 	apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
154 	/* Fill in the pagedir's pointers to GATT pages */
155 	for (i = 0; i < sc->ag_entries / 1024; i++) {
156 		vm_offset_t va;
157 		vm_offset_t pa;
158 
159 		va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
160 		pa = vtophys(va);
161 		sc->ag_vdir[apbase_offset + i] = pa | 1;
162 	}
163 
164 	return 0;
165 }
166 
167 static int
agp_ati_attach(device_t dev)168 agp_ati_attach(device_t dev)
169 {
170 	struct agp_ati_softc *sc = device_get_softc(dev);
171 	int error, rid;
172 	u_int32_t temp;
173 	u_int32_t apsize_reg, agpmode_reg;
174 
175 	error = agp_generic_attach(dev);
176 	if (error)
177 		return error;
178 
179 	switch (pci_get_devid(dev)) {
180 	case 0xcab01002: /* ATI RS100 AGP bridge */
181 	case 0xcab21002: /* ATI RS200 AGP bridge */
182 	case 0xcbb21002: /* ATI RS200M AGP bridge */
183 	case 0xcab31002: /* ATI RS250 AGP bridge */
184 		sc->is_rs300 = 0;
185 		apsize_reg = ATI_RS100_APSIZE;
186 		agpmode_reg = ATI_RS100_IG_AGPMODE;
187 		break;
188 	case 0x58301002: /* ATI RS300_100 AGP bridge */
189 	case 0x58311002: /* ATI RS300_133 AGP bridge */
190 	case 0x58321002: /* ATI RS300_166 AGP bridge */
191 	case 0x58331002: /* ATI RS300_200 AGP bridge */
192 		sc->is_rs300 = 1;
193 		apsize_reg = ATI_RS300_APSIZE;
194 		agpmode_reg = ATI_RS300_IG_AGPMODE;
195 		break;
196 	default:
197 		/* Unknown chipset */
198 		return EINVAL;
199 	}
200 
201 	rid = ATI_GART_MMADDR;
202 	sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
203 	if (!sc->regs) {
204 		agp_generic_detach(dev);
205 		return ENOMEM;
206 	}
207 
208 	sc->bst = rman_get_bustag(sc->regs);
209 	sc->bsh = rman_get_bushandle(sc->regs);
210 
211 	sc->initial_aperture = AGP_GET_APERTURE(dev);
212 
213 	for (;;) {
214 		if (agp_ati_alloc_gatt(dev) == 0)
215 			break;
216 
217 		/*
218 		 * Probably contigmalloc failure. Try reducing the
219 		 * aperture so that the gatt size reduces.
220 		 */
221 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
222 			return ENOMEM;
223 	}
224 
225 	temp = pci_read_config(dev, apsize_reg, 4);
226 	pci_write_config(dev, apsize_reg, temp | 1, 4);
227 
228 	pci_write_config(dev, agpmode_reg, 0x20000, 4);
229 
230 	WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
231 
232 	temp = pci_read_config(dev, 4, 4);	/* XXX: Magic reg# */
233 	pci_write_config(dev, 4, temp | (1 << 14), 4);
234 
235 	WRITE4(ATI_GART_BASE, sc->ag_pdir);
236 
237 	AGP_FLUSH_TLB(dev);
238 
239 	return 0;
240 }
241 
242 static int
agp_ati_detach(device_t dev)243 agp_ati_detach(device_t dev)
244 {
245 	struct agp_ati_softc *sc = device_get_softc(dev);
246 	u_int32_t apsize_reg, temp;
247 
248 	agp_free_cdev(dev);
249 
250 	if (sc->is_rs300)
251 		apsize_reg = ATI_RS300_APSIZE;
252 	else
253 		apsize_reg = ATI_RS100_APSIZE;
254 
255 	/* Clear the GATT base */
256 	WRITE4(ATI_GART_BASE, 0);
257 
258 	/* Put the aperture back the way it started. */
259 	AGP_SET_APERTURE(dev, sc->initial_aperture);
260 
261 	temp = pci_read_config(dev, apsize_reg, 4);
262 	pci_write_config(dev, apsize_reg, temp & ~1, 4);
263 
264 	kmem_free((vm_offset_t)sc->ag_vdir, AGP_PAGE_SIZE);
265 	kmem_free((vm_offset_t)sc->ag_virtual, sc->ag_entries *
266 	    sizeof(u_int32_t));
267 
268 	bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
269 	agp_free_res(dev);
270 
271 	return 0;
272 }
273 
274 static u_int32_t
agp_ati_get_aperture(device_t dev)275 agp_ati_get_aperture(device_t dev)
276 {
277 	struct agp_ati_softc *sc = device_get_softc(dev);
278 	int size_value;
279 
280 	if (sc->is_rs300)
281 		size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
282 	else
283 		size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
284 
285 	size_value = (size_value & 0x0000000e) >> 1;
286 	size_value = (32 * 1024 * 1024) << size_value;
287 
288 	return size_value;
289 }
290 
291 static int
agp_ati_set_aperture(device_t dev,u_int32_t aperture)292 agp_ati_set_aperture(device_t dev, u_int32_t aperture)
293 {
294 	struct agp_ati_softc *sc = device_get_softc(dev);
295 	int size_value;
296 	u_int32_t apsize_reg;
297 
298 	if (sc->is_rs300)
299 		apsize_reg = ATI_RS300_APSIZE;
300 	else
301 		apsize_reg = ATI_RS100_APSIZE;
302 
303 	size_value = pci_read_config(dev, apsize_reg, 4);
304 
305 	size_value &= ~0x0000000e;
306 	size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
307 
308 	pci_write_config(dev, apsize_reg, size_value, 4);
309 
310 	return 0;
311 }
312 
313 static int
agp_ati_bind_page(device_t dev,vm_offset_t offset,vm_offset_t physical)314 agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
315 {
316 	struct agp_ati_softc *sc = device_get_softc(dev);
317 
318 	if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
319 		return EINVAL;
320 
321 	sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
322 
323 	return 0;
324 }
325 
326 static int
agp_ati_unbind_page(device_t dev,vm_offset_t offset)327 agp_ati_unbind_page(device_t dev, vm_offset_t offset)
328 {
329 	struct agp_ati_softc *sc = device_get_softc(dev);
330 
331 	if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
332 		return EINVAL;
333 
334 	sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
335 	return 0;
336 }
337 
338 static void
agp_ati_flush_tlb(device_t dev)339 agp_ati_flush_tlb(device_t dev)
340 {
341 	struct agp_ati_softc *sc = device_get_softc(dev);
342 
343 	/* Set the cache invalidate bit and wait for the chipset to clear */
344 	WRITE4(ATI_GART_CACHE_CNTRL, 1);
345 	(void)READ4(ATI_GART_CACHE_CNTRL);
346 }
347 
348 static device_method_t agp_ati_methods[] = {
349 	/* Device interface */
350 	DEVMETHOD(device_probe,		agp_ati_probe),
351 	DEVMETHOD(device_attach,	agp_ati_attach),
352 	DEVMETHOD(device_detach,	agp_ati_detach),
353 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
354 	DEVMETHOD(device_suspend,	bus_generic_suspend),
355 	DEVMETHOD(device_resume,	bus_generic_resume),
356 
357 	/* AGP interface */
358 	DEVMETHOD(agp_get_aperture,	agp_ati_get_aperture),
359 	DEVMETHOD(agp_set_aperture,	agp_ati_set_aperture),
360 	DEVMETHOD(agp_bind_page,	agp_ati_bind_page),
361 	DEVMETHOD(agp_unbind_page,	agp_ati_unbind_page),
362 	DEVMETHOD(agp_flush_tlb,	agp_ati_flush_tlb),
363 	DEVMETHOD(agp_enable,		agp_generic_enable),
364 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
365 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
366 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
367 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
368 	{ 0, 0 }
369 };
370 
371 static driver_t agp_ati_driver = {
372 	"agp",
373 	agp_ati_methods,
374 	sizeof(struct agp_ati_softc),
375 };
376 
377 static devclass_t agp_devclass;
378 
379 DRIVER_MODULE(agp_ati, hostb, agp_ati_driver, agp_devclass, 0, 0);
380 MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
381 MODULE_DEPEND(agp_ati, pci, 1, 1, 1);
382