xref: /netbsd-src/sys/dev/pci/agp_ali.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: agp_ali.c,v 1.14 2008/04/13 13:07:31 njoly 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_ali.c,v 1.3 2001/07/05 21:28:46 jhb Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: agp_ali.c,v 1.14 2008/04/13 13:07:31 njoly Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/conf.h>
40 #include <sys/device.h>
41 #include <sys/agpio.h>
42 
43 #include <uvm/uvm_extern.h>
44 
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/agpvar.h>
48 #include <dev/pci/agpreg.h>
49 
50 #include <sys/bus.h>
51 
52 struct agp_ali_softc {
53 	struct agp_softc agp;
54 	u_int32_t	initial_aperture; /* aperture size at startup */
55 	struct agp_gatt *gatt;
56 };
57 
58 static u_int32_t agp_ali_get_aperture(struct agp_softc *);
59 static int agp_ali_set_aperture(struct agp_softc *sc, u_int32_t);
60 static int agp_ali_bind_page(struct agp_softc *, off_t, bus_addr_t);
61 static int agp_ali_unbind_page(struct agp_softc *, off_t);
62 static void agp_ali_flush_tlb(struct agp_softc *);
63 
64 
65 static struct agp_methods agp_ali_methods = {
66 	agp_ali_get_aperture,
67 	agp_ali_set_aperture,
68 	agp_ali_bind_page,
69 	agp_ali_unbind_page,
70 	agp_ali_flush_tlb,
71 	agp_generic_enable,
72 	agp_generic_alloc_memory,
73 	agp_generic_free_memory,
74 	agp_generic_bind_memory,
75 	agp_generic_unbind_memory,
76 };
77 
78 int
79 agp_ali_attach(struct device *parent, struct device *self, void *aux)
80 {
81 	struct agp_softc *sc = (struct agp_softc *)self;
82 	struct agp_ali_softc *asc;
83 	struct pci_attach_args *pa = aux;
84 	struct agp_gatt *gatt;
85 	pcireg_t reg;
86 
87 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT);
88 	if (asc == NULL) {
89 		aprint_error(": failed to allocate softc\n");
90 		return ENOMEM;
91 	}
92 	sc->as_chipc = asc;
93 	sc->as_methods = &agp_ali_methods;
94 
95 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
96 		aprint_error(": failed to map aperture\n");
97 		free(asc, M_AGP);
98 		return ENXIO;
99 	}
100 
101 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
102 	    NULL);
103 
104 	asc->initial_aperture = agp_ali_get_aperture(sc);
105 
106 	for (;;) {
107 		gatt = agp_alloc_gatt(sc);
108 		if (gatt != NULL)
109 			break;
110 
111 		/*
112 		 * Probably contigmalloc failure. Try reducing the
113 		 * aperture so that the gatt size reduces.
114 		 */
115 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
116 			agp_generic_detach(sc);
117 			aprint_error(": failed to set aperture\n");
118 			return ENOMEM;
119 		}
120 	}
121 	asc->gatt = gatt;
122 
123 	/* Install the gatt. */
124 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_ALI_ATTBASE);
125 	reg = (reg & 0xfff) | (gatt->ag_physical & ~0xfff);
126 	pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_ALI_ATTBASE, reg);
127 
128 	/* Enable the TLB. */
129 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_ALI_TLBCTRL);
130 	reg = (reg & ~0xff) | 0x10;
131 	pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_ALI_TLBCTRL, reg);
132 
133 	return 0;
134 }
135 
136 #if 0
137 static int
138 agp_ali_detach(struct agp_softc *sc)
139 {
140 	int error;
141 	pcireg_t reg;
142 	struct agp_ali_softc *asc = sc->as_chipc;
143 
144 	error = agp_generic_detach(sc);
145 	if (error)
146 		return error;
147 
148 	/* Disable the TLB.. */
149 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL);
150 	reg &= ~0xff;
151 	reg |= 0x90;
152 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL, reg);
153 
154 	/* Put the aperture back the way it started. */
155 	AGP_SET_APERTURE(sc, asc->initial_aperture);
156 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE);
157 	reg &= 0xf;
158 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE, reg);
159 
160 	agp_free_gatt(sc, asc->gatt);
161 	return 0;
162 }
163 #endif
164 
165 #define M 1024*1024
166 
167 static const u_int32_t agp_ali_table[] = {
168 	0,			/* 0 - invalid */
169 	1,			/* 1 - invalid */
170 	2,			/* 2 - invalid */
171 	4*M,			/* 3 */
172 	8*M,			/* 4 */
173 	0,			/* 5 - Reserved */
174 	16*M,			/* 6 */
175 	32*M,			/* 7 */
176 	64*M,			/* 8 */
177 	128*M,			/* 9 */
178 	256*M,			/* 10 */
179 };
180 #define agp_ali_table_size (sizeof(agp_ali_table) / sizeof(agp_ali_table[0]))
181 
182 static u_int32_t
183 agp_ali_get_aperture(struct agp_softc *sc)
184 {
185 	int i;
186 
187 	/*
188 	 * The aperture size is derived from the low bits of attbase.
189 	 * I'm not sure this is correct..
190 	 */
191 	i = (int)pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE) & 0xf;
192 	if (i >= agp_ali_table_size)
193 		return 0;
194 	return agp_ali_table[i];
195 }
196 
197 static int
198 agp_ali_set_aperture(struct agp_softc *sc, u_int32_t aperture)
199 {
200 	int i;
201 	pcireg_t reg;
202 
203 	if (aperture & (aperture - 1) || aperture < 1*M)
204 		return EINVAL;
205 
206 	for (i = 0; i < agp_ali_table_size; i++)
207 		if (agp_ali_table[i] == aperture)
208 			break;
209 	if (i == agp_ali_table_size)
210 		return EINVAL;
211 
212 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE);
213 	reg &= ~0xf;
214 	reg |= i;
215 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_ATTBASE, reg);
216 	return 0;
217 }
218 
219 static int
220 agp_ali_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
221 {
222 	struct agp_ali_softc *asc = sc->as_chipc;
223 
224 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
225 		return EINVAL;
226 
227 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
228 	return 0;
229 }
230 
231 static int
232 agp_ali_unbind_page(struct agp_softc *sc, off_t offset)
233 {
234 	struct agp_ali_softc *asc = sc->as_chipc;
235 
236 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
237 		return EINVAL;
238 
239 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
240 	return 0;
241 }
242 
243 static void
244 agp_ali_flush_tlb(struct agp_softc *sc)
245 {
246 	pcireg_t reg;
247 
248 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL);
249 	reg &= ~0xff;
250 	reg |= 0x90;
251 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL, reg);
252 	reg &= ~0xff;
253 	reg |= 0x10;
254 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_ALI_TLBCTRL, reg);
255 }
256