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