xref: /netbsd-src/sys/dev/pci/agp_via.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: agp_via.c,v 1.14 2007/10/19 12:00:39 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Doug Rabson
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  *	$FreeBSD: src/sys/pci/agp_via.c,v 1.3 2001/07/05 21:28:47 jhb Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: agp_via.c,v 1.14 2007/10/19 12:00:39 ad Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/proc.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/agpio.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/agpvar.h>
49 #include <dev/pci/agpreg.h>
50 #include <dev/pci/pcidevs.h>
51 
52 #include <sys/bus.h>
53 
54 static u_int32_t agp_via_get_aperture(struct agp_softc *);
55 static int agp_via_set_aperture(struct agp_softc *, u_int32_t);
56 static int agp_via_bind_page(struct agp_softc *, off_t, bus_addr_t);
57 static int agp_via_unbind_page(struct agp_softc *, off_t);
58 static void agp_via_flush_tlb(struct agp_softc *);
59 
60 static struct agp_methods agp_via_methods = {
61 	agp_via_get_aperture,
62 	agp_via_set_aperture,
63 	agp_via_bind_page,
64 	agp_via_unbind_page,
65 	agp_via_flush_tlb,
66 	agp_generic_enable,
67 	agp_generic_alloc_memory,
68 	agp_generic_free_memory,
69 	agp_generic_bind_memory,
70 	agp_generic_unbind_memory,
71 };
72 
73 struct agp_via_softc {
74 	u_int32_t	initial_aperture; /* aperture size at startup */
75 	struct agp_gatt *gatt;
76 	int		*regs;
77 };
78 
79 #define REG_GARTCTRL	0
80 #define REG_APSIZE	1
81 #define REG_ATTBASE	2
82 
83 static int via_v2_regs[] =
84 	{ AGP_VIA_GARTCTRL, AGP_VIA_APSIZE, AGP_VIA_ATTBASE };
85 static int via_v3_regs[] =
86 	{ AGP3_VIA_GARTCTRL, AGP3_VIA_APSIZE, AGP3_VIA_ATTBASE };
87 
88 int
89 agp_via_attach(struct device *parent, struct device *self, void *aux)
90 {
91 	struct pci_attach_args *pa = aux;
92 	struct agp_softc *sc = (void *)self;
93 	struct agp_via_softc *asc;
94 	struct agp_gatt *gatt;
95 	pcireg_t agpsel, capval;
96 
97 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
98 	if (asc == NULL) {
99 		aprint_error(": can't allocate chipset-specific softc\n");
100 		return ENOMEM;
101 	}
102 	sc->as_chipc = asc;
103 	sc->as_methods = &agp_via_methods;
104 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
105 	    &capval);
106 
107 	if (PCI_CAP_AGP_MAJOR(capval) >= 3) {
108 		agpsel = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_VIA_AGPSEL);
109 		if ((agpsel & (1 << 1)) == 0) {
110 			asc->regs = via_v3_regs;
111 			printf(" (v3)");
112 		} else {
113 			asc->regs = via_v2_regs;
114 			printf(" (v2 compat mode)");
115 		}
116 	} else {
117 		asc->regs = via_v2_regs;
118 		printf(" (v2)");
119 	}
120 
121 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
122 		aprint_error(": can't map aperture\n");
123 		free(asc, M_AGP);
124 		return ENXIO;
125 	}
126 
127 	asc->initial_aperture = AGP_GET_APERTURE(sc);
128 
129 	for (;;) {
130 		gatt = agp_alloc_gatt(sc);
131 		if (gatt)
132 			break;
133 
134 		/*
135 		 * Probably contigmalloc failure. Try reducing the
136 		 * aperture so that the gatt size reduces.
137 		 */
138 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
139 			agp_generic_detach(sc);
140 			aprint_error(": can't set aperture size\n");
141 			return ENOMEM;
142 		}
143 	}
144 	asc->gatt = gatt;
145 
146 	if (asc->regs == via_v2_regs) {
147 		/* Install the gatt. */
148 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_ATTBASE],
149 				 gatt->ag_physical | 3);
150 		/* Enable the aperture. */
151 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_GARTCTRL],
152 				 0x0000000f);
153 	} else {
154 		pcireg_t gartctrl;
155 		/* Install the gatt. */
156 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_ATTBASE],
157 				 gatt->ag_physical);
158 		/* Enable the aperture. */
159 		gartctrl = pci_conf_read(pa->pa_pc, pa->pa_tag,
160 				 asc->regs[REG_ATTBASE]);
161 		pci_conf_write(pa->pa_pc, pa->pa_tag, asc->regs[REG_GARTCTRL],
162 				 gartctrl | (3 << 7));
163 	}
164 
165 	return 0;
166 }
167 
168 #if 0
169 static int
170 agp_via_detach(struct agp_softc *sc)
171 {
172 	struct agp_via_softc *asc = sc->as_chipc;
173 	int error;
174 
175 	error = agp_generic_detach(sc);
176 	if (error)
177 		return error;
178 
179 	pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL], 0);
180 	pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_ATTBASE], 0);
181 	AGP_SET_APERTURE(sc, asc->initial_aperture);
182 	agp_free_gatt(sc, asc->gatt);
183 
184 	return 0;
185 }
186 #endif
187 
188 static u_int32_t
189 agp_via_get_aperture(struct agp_softc *sc)
190 {
191 	struct agp_via_softc *asc = sc->as_chipc;
192 	u_int32_t apsize;
193 
194 	apsize = pci_conf_read(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE])
195 				& 0x1f;
196 
197 	/*
198 	 * The size is determined by the number of low bits of
199 	 * register APBASE which are forced to zero. The low 20 bits
200 	 * are always forced to zero and each zero bit in the apsize
201 	 * field just read forces the corresponding bit in the 27:20
202 	 * to be zero. We calculate the aperture size accordingly.
203 	 */
204 	return (((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1;
205 }
206 
207 static int
208 agp_via_set_aperture(struct agp_softc *sc, u_int32_t aperture)
209 {
210 	struct agp_via_softc *asc = sc->as_chipc;
211 	u_int32_t apsize;
212 	pcireg_t reg;
213 
214 	/*
215 	 * Reverse the magic from get_aperture.
216 	 */
217 	apsize = ((aperture - 1) >> 20) ^ 0xff;
218 
219 	/*
220 	 * Double check for sanity.
221 	 */
222 	if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
223 		return EINVAL;
224 
225 	reg = pci_conf_read(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE]);
226 	reg &= ~0xff;
227 	reg |= apsize;
228 	pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_APSIZE], reg);
229 
230 	return 0;
231 }
232 
233 static int
234 agp_via_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
235 {
236 	struct agp_via_softc *asc = sc->as_chipc;
237 
238 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
239 		return EINVAL;
240 
241 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
242 	return 0;
243 }
244 
245 static int
246 agp_via_unbind_page(struct agp_softc *sc, off_t offset)
247 {
248 	struct agp_via_softc *asc = sc->as_chipc;
249 
250 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
251 		return EINVAL;
252 
253 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
254 	return 0;
255 }
256 
257 static void
258 agp_via_flush_tlb(struct agp_softc *sc)
259 {
260 	struct agp_via_softc *asc = sc->as_chipc;
261 	pcireg_t gartctrl;
262 
263 	if (asc->regs == via_v2_regs) {
264 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
265 				0x8f);
266 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
267 				0x0f);
268 	} else {
269 		gartctrl = pci_conf_read(sc->as_pc, sc->as_tag,
270 					 asc->regs[REG_GARTCTRL]);
271 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
272 			       gartctrl & ~(1 << 7));
273 		pci_conf_write(sc->as_pc, sc->as_tag, asc->regs[REG_GARTCTRL],
274 			       gartctrl);
275 	}
276 }
277