1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/malloc.h>
37#include <sys/kernel.h>
38#include <sys/module.h>
39#include <sys/bus.h>
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/proc.h>
43
44#include <dev/agp/agppriv.h>
45#include <dev/agp/agpreg.h>
46#include <dev/pci/pcivar.h>
47#include <dev/pci/pcireg.h>
48
49#include <vm/vm.h>
50#include <vm/vm_extern.h>
51#include <vm/vm_kern.h>
52#include <vm/vm_object.h>
53#include <vm/pmap.h>
54#include <machine/bus.h>
55#include <machine/resource.h>
56#include <sys/rman.h>
57
58MALLOC_DECLARE(M_AGP);
59
60#define READ4(off)	bus_space_read_4(sc->bst, sc->bsh, off)
61#define WRITE4(off,v)	bus_space_write_4(sc->bst, sc->bsh, off, v)
62
63struct agp_ati_softc {
64	struct agp_softc agp;
65	struct resource *regs;	/* memory mapped control registers */
66	bus_space_tag_t bst;	/* bus_space tag */
67	bus_space_handle_t bsh;	/* bus_space handle */
68	u_int32_t	initial_aperture; /* aperture size at startup */
69	char		is_rs300;
70
71	/* The GATT */
72	u_int32_t	ag_entries;
73	u_int32_t      *ag_virtual;	/* virtual address of gatt */
74	u_int32_t      *ag_vdir;	/* virtual address of page dir */
75	vm_offset_t	ag_pdir;	/* physical address of page dir */
76};
77
78static const char*
79agp_ati_match(device_t dev)
80{
81	if (pci_get_class(dev) != PCIC_BRIDGE ||
82	    pci_get_subclass(dev) != PCIS_BRIDGE_HOST)
83		return NULL;
84
85	if (agp_find_caps(dev) == 0)
86		return NULL;
87
88	switch (pci_get_devid(dev)) {
89	case 0xcab01002:
90		return ("ATI RS100 AGP bridge");
91	case 0xcab21002:
92		return ("ATI RS200 AGP bridge");
93	case 0xcbb21002:
94		return ("ATI RS200M AGP bridge");
95	case 0xcab31002:
96		return ("ATI RS250 AGP bridge");
97	case 0x58301002:
98		return ("ATI RS300_100 AGP bridge");
99	case 0x58311002:
100		return ("ATI RS300_133 AGP bridge");
101	case 0x58321002:
102		return ("ATI RS300_166 AGP bridge");
103	case 0x58331002:
104		return ("ATI RS300_200 AGP bridge");
105	}
106
107	return NULL;
108}
109
110static int
111agp_ati_probe(device_t dev)
112{
113	const char *desc;
114
115	desc = agp_ati_match(dev);
116	if (desc) {
117		device_set_desc(dev, desc);
118		return 0;
119	}
120
121	return ENXIO;
122}
123
124static int
125agp_ati_alloc_gatt(device_t dev)
126{
127	struct agp_ati_softc *sc = device_get_softc(dev);
128	u_int32_t apsize = AGP_GET_APERTURE(dev);
129	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
130	u_int32_t apbase_offset;
131	int i;
132
133	/* Alloc the GATT -- pointers to pages of AGP memory */
134	sc->ag_entries = entries;
135	sc->ag_virtual = (void *)kmem_alloc_attr(entries * sizeof(u_int32_t),
136	    M_NOWAIT | M_ZERO, 0, ~0, VM_MEMATTR_WRITE_COMBINING);
137	if (sc->ag_virtual == NULL) {
138		if (bootverbose)
139			device_printf(dev, "GATT allocation failed\n");
140		return ENOMEM;
141	}
142
143	/* Alloc the page directory -- pointers to each page of the GATT */
144	sc->ag_vdir = (void *)kmem_alloc_attr(AGP_PAGE_SIZE, M_NOWAIT | M_ZERO,
145	    0, ~0, VM_MEMATTR_WRITE_COMBINING);
146	if (sc->ag_vdir == NULL) {
147		if (bootverbose)
148			device_printf(dev, "pagedir allocation failed\n");
149		kmem_free((vm_offset_t)sc->ag_virtual, entries *
150		    sizeof(u_int32_t));
151		return ENOMEM;
152	}
153	sc->ag_pdir = vtophys((vm_offset_t)sc->ag_vdir);
154
155	apbase_offset = pci_read_config(dev, AGP_APBASE, 4) >> 22;
156	/* Fill in the pagedir's pointers to GATT pages */
157	for (i = 0; i < sc->ag_entries / 1024; i++) {
158		vm_offset_t va;
159		vm_offset_t pa;
160
161		va = ((vm_offset_t)sc->ag_virtual) + i * AGP_PAGE_SIZE;
162		pa = vtophys(va);
163		sc->ag_vdir[apbase_offset + i] = pa | 1;
164	}
165
166	return 0;
167}
168
169static int
170agp_ati_attach(device_t dev)
171{
172	struct agp_ati_softc *sc = device_get_softc(dev);
173	int error, rid;
174	u_int32_t temp;
175	u_int32_t apsize_reg, agpmode_reg;
176
177	error = agp_generic_attach(dev);
178	if (error)
179		return error;
180
181	switch (pci_get_devid(dev)) {
182	case 0xcab01002: /* ATI RS100 AGP bridge */
183	case 0xcab21002: /* ATI RS200 AGP bridge */
184	case 0xcbb21002: /* ATI RS200M AGP bridge */
185	case 0xcab31002: /* ATI RS250 AGP bridge */
186		sc->is_rs300 = 0;
187		apsize_reg = ATI_RS100_APSIZE;
188		agpmode_reg = ATI_RS100_IG_AGPMODE;
189		break;
190	case 0x58301002: /* ATI RS300_100 AGP bridge */
191	case 0x58311002: /* ATI RS300_133 AGP bridge */
192	case 0x58321002: /* ATI RS300_166 AGP bridge */
193	case 0x58331002: /* ATI RS300_200 AGP bridge */
194		sc->is_rs300 = 1;
195		apsize_reg = ATI_RS300_APSIZE;
196		agpmode_reg = ATI_RS300_IG_AGPMODE;
197		break;
198	default:
199		/* Unknown chipset */
200		return EINVAL;
201	}
202
203	rid = ATI_GART_MMADDR;
204	sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
205	if (!sc->regs) {
206		agp_generic_detach(dev);
207		return ENOMEM;
208	}
209
210	sc->bst = rman_get_bustag(sc->regs);
211	sc->bsh = rman_get_bushandle(sc->regs);
212
213	sc->initial_aperture = AGP_GET_APERTURE(dev);
214
215	for (;;) {
216		if (agp_ati_alloc_gatt(dev) == 0)
217			break;
218
219		/*
220		 * Probably contigmalloc failure. Try reducing the
221		 * aperture so that the gatt size reduces.
222		 */
223		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
224			return ENOMEM;
225	}
226
227	temp = pci_read_config(dev, apsize_reg, 4);
228	pci_write_config(dev, apsize_reg, temp | 1, 4);
229
230	pci_write_config(dev, agpmode_reg, 0x20000, 4);
231
232	WRITE4(ATI_GART_FEATURE_ID, 0x00060000);
233
234	temp = pci_read_config(dev, 4, 4);	/* XXX: Magic reg# */
235	pci_write_config(dev, 4, temp | (1 << 14), 4);
236
237	WRITE4(ATI_GART_BASE, sc->ag_pdir);
238
239	AGP_FLUSH_TLB(dev);
240
241	return 0;
242}
243
244static int
245agp_ati_detach(device_t dev)
246{
247	struct agp_ati_softc *sc = device_get_softc(dev);
248	u_int32_t apsize_reg, temp;
249
250	agp_free_cdev(dev);
251
252	if (sc->is_rs300)
253		apsize_reg = ATI_RS300_APSIZE;
254	else
255		apsize_reg = ATI_RS100_APSIZE;
256
257	/* Clear the GATT base */
258	WRITE4(ATI_GART_BASE, 0);
259
260	/* Put the aperture back the way it started. */
261	AGP_SET_APERTURE(dev, sc->initial_aperture);
262
263	temp = pci_read_config(dev, apsize_reg, 4);
264	pci_write_config(dev, apsize_reg, temp & ~1, 4);
265
266	kmem_free((vm_offset_t)sc->ag_vdir, AGP_PAGE_SIZE);
267	kmem_free((vm_offset_t)sc->ag_virtual, sc->ag_entries *
268	    sizeof(u_int32_t));
269
270	bus_release_resource(dev, SYS_RES_MEMORY, ATI_GART_MMADDR, sc->regs);
271	agp_free_res(dev);
272
273	return 0;
274}
275
276static u_int32_t
277agp_ati_get_aperture(device_t dev)
278{
279	struct agp_ati_softc *sc = device_get_softc(dev);
280	int size_value;
281
282	if (sc->is_rs300)
283		size_value = pci_read_config(dev, ATI_RS300_APSIZE, 4);
284	else
285		size_value = pci_read_config(dev, ATI_RS100_APSIZE, 4);
286
287	size_value = (size_value & 0x0000000e) >> 1;
288	size_value = (32 * 1024 * 1024) << size_value;
289
290	return size_value;
291}
292
293static int
294agp_ati_set_aperture(device_t dev, u_int32_t aperture)
295{
296	struct agp_ati_softc *sc = device_get_softc(dev);
297	int size_value;
298	u_int32_t apsize_reg;
299
300	if (sc->is_rs300)
301		apsize_reg = ATI_RS300_APSIZE;
302	else
303		apsize_reg = ATI_RS100_APSIZE;
304
305	size_value = pci_read_config(dev, apsize_reg, 4);
306
307	size_value &= ~0x0000000e;
308	size_value |= (ffs(aperture / (32 * 1024 * 1024)) - 1) << 1;
309
310	pci_write_config(dev, apsize_reg, size_value, 4);
311
312	return 0;
313}
314
315static int
316agp_ati_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
317{
318	struct agp_ati_softc *sc = device_get_softc(dev);
319
320	if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
321		return EINVAL;
322
323	sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
324
325	return 0;
326}
327
328static int
329agp_ati_unbind_page(device_t dev, vm_offset_t offset)
330{
331	struct agp_ati_softc *sc = device_get_softc(dev);
332
333	if (offset >= (sc->ag_entries << AGP_PAGE_SHIFT))
334		return EINVAL;
335
336	sc->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
337	return 0;
338}
339
340static void
341agp_ati_flush_tlb(device_t dev)
342{
343	struct agp_ati_softc *sc = device_get_softc(dev);
344
345	/* Set the cache invalidate bit and wait for the chipset to clear */
346	WRITE4(ATI_GART_CACHE_CNTRL, 1);
347	(void)READ4(ATI_GART_CACHE_CNTRL);
348}
349
350static device_method_t agp_ati_methods[] = {
351	/* Device interface */
352	DEVMETHOD(device_probe,		agp_ati_probe),
353	DEVMETHOD(device_attach,	agp_ati_attach),
354	DEVMETHOD(device_detach,	agp_ati_detach),
355	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
356	DEVMETHOD(device_suspend,	bus_generic_suspend),
357	DEVMETHOD(device_resume,	bus_generic_resume),
358
359	/* AGP interface */
360	DEVMETHOD(agp_get_aperture,	agp_ati_get_aperture),
361	DEVMETHOD(agp_set_aperture,	agp_ati_set_aperture),
362	DEVMETHOD(agp_bind_page,	agp_ati_bind_page),
363	DEVMETHOD(agp_unbind_page,	agp_ati_unbind_page),
364	DEVMETHOD(agp_flush_tlb,	agp_ati_flush_tlb),
365	DEVMETHOD(agp_enable,		agp_generic_enable),
366	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
367	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
368	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
369	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
370	{ 0, 0 }
371};
372
373static driver_t agp_ati_driver = {
374	"agp",
375	agp_ati_methods,
376	sizeof(struct agp_ati_softc),
377};
378
379static devclass_t agp_devclass;
380
381DRIVER_MODULE(agp_ati, hostb, agp_ati_driver, agp_devclass, 0, 0);
382MODULE_DEPEND(agp_ati, agp, 1, 1, 1);
383MODULE_DEPEND(agp_ati, pci, 1, 1, 1);
384