xref: /openbsd-src/sys/dev/pci/agp_ali.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: agp_ali.c,v 1.5 2007/11/26 15:35:15 deraadt Exp $	*/
2 /*	$NetBSD: agp_ali.c,v 1.2 2001/09/15 00:25:00 thorpej Exp $	*/
3 
4 
5 /*-
6  * Copyright (c) 2000 Doug Rabson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	$FreeBSD: src/sys/pci/agp_ali.c,v 1.3 2001/07/05 21:28:46 jhb Exp $
31  */
32 
33 
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/lock.h>
43 #include <sys/agpio.h>
44 
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/vga_pcivar.h>
48 #include <dev/pci/agpvar.h>
49 #include <dev/pci/agpreg.h>
50 
51 #include <machine/bus.h>
52 
53 struct agp_ali_softc {
54 	u_int32_t	initial_aperture; /* aperture size at startup */
55 	struct agp_gatt *gatt;
56 };
57 
58 u_int32_t agp_ali_get_aperture(struct agp_softc *);
59 int	agp_ali_set_aperture(struct agp_softc *sc, u_int32_t);
60 int	agp_ali_bind_page(struct agp_softc *, off_t, bus_addr_t);
61 int	agp_ali_unbind_page(struct agp_softc *, off_t);
62 void	agp_ali_flush_tlb(struct agp_softc *);
63 
64 struct agp_methods agp_ali_methods = {
65 	agp_ali_get_aperture,
66 	agp_ali_set_aperture,
67 	agp_ali_bind_page,
68 	agp_ali_unbind_page,
69 	agp_ali_flush_tlb,
70 	agp_generic_enable,
71 	agp_generic_alloc_memory,
72 	agp_generic_free_memory,
73 	agp_generic_bind_memory,
74 	agp_generic_unbind_memory,
75 };
76 
77 int
78 agp_ali_attach(struct agp_softc *sc, struct pci_attach_args *pa)
79 {
80 	struct agp_ali_softc *asc;
81 	struct agp_gatt *gatt;
82 	pcireg_t reg;
83 
84 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT);
85 	if (asc == NULL) {
86 		printf("failed to allocate softc\n");
87 		return (ENOMEM);
88 	}
89 	sc->sc_chipc = asc;
90 	sc->sc_methods = &agp_ali_methods;
91 
92 	if (agp_map_aperture(pa, sc, AGP_APBASE, PCI_MAPREG_TYPE_MEM) != 0) {
93 		printf("failed to map aperture\n");
94 		free(asc, M_AGP);
95 		return (ENXIO);
96 	}
97 
98 	asc->initial_aperture = agp_ali_get_aperture(sc);
99 
100 	for (;;) {
101 		gatt = agp_alloc_gatt(sc);
102 		if (gatt != NULL)
103 			break;
104 
105 		/*
106 		 * Probably contigmalloc failure. Try reducing the
107 		 * aperture so that the gatt size reduces.
108 		 */
109 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
110 			agp_generic_detach(sc);
111 			printf("failed to set aperture\n");
112 			return (ENOMEM);
113 		}
114 	}
115 	asc->gatt = gatt;
116 
117 	/* Install the gatt. */
118 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, AGP_ALI_ATTBASE);
119 	reg = (reg & 0xff) | gatt->ag_physical;
120 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_ATTBASE, reg);
121 
122 	/* Enable the TLB. */
123 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL);
124 	reg = (reg & ~0xff) | 0x10;
125 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL, reg);
126 
127 	return (0);
128 }
129 
130 #if 0
131 int
132 agp_ali_detach(struct agp_softc *sc)
133 {
134 	int error;
135 	pcireg_t reg;
136 	struct agp_ali_softc *asc = sc->sc_chipc;
137 
138 	error = agp_generic_detach(sc);
139 	if (error)
140 		return (error);
141 
142 	/* Disable the TLB.. */
143 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL);
144 	reg &= ~0xff;
145 	reg |= 0x90;
146 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL, reg);
147 
148 	/* Put the aperture back the way it started. */
149 	AGP_SET_APERTURE(sc, asc->initial_aperture);
150 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, AGP_ALI_ATTBASE);
151 	reg &= 0xff;
152 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_ATTBASE, reg);
153 
154 	agp_free_gatt(sc, asc->gatt);
155 	return (0);
156 }
157 #endif
158 
159 #define M 1024*1024
160 
161 static const u_int32_t agp_ali_table[] = {
162 	0,			/* 0 - invalid */
163 	1,			/* 1 - invalid */
164 	2,			/* 2 - invalid */
165 	4*M,			/* 3 - invalid */
166 	8*M,			/* 4 - invalid */
167 	0,			/* 5 - invalid */
168 	16*M,			/* 6 - invalid */
169 	32*M,			/* 7 - invalid */
170 	64*M,			/* 8 - invalid */
171 	128*M,			/* 9 - invalid */
172 	256*M,			/* 10 - invalid */
173 };
174 #define agp_ali_table_size (sizeof(agp_ali_table) / sizeof(agp_ali_table[0]))
175 
176 u_int32_t
177 agp_ali_get_aperture(struct agp_softc *sc)
178 {
179 	int i;
180 
181 	/*
182 	 * The aperture size is derived from the low bits of attbase.
183 	 * I'm not sure this is correct..
184 	 */
185 	i = (int)pci_conf_read(sc->sc_pc, sc->sc_pcitag,
186 	    AGP_ALI_ATTBASE) & 0xff;
187 	if (i >= agp_ali_table_size)
188 		return (0);
189 	return (agp_ali_table[i]);
190 }
191 
192 int
193 agp_ali_set_aperture(struct agp_softc *sc, u_int32_t aperture)
194 {
195 	int i;
196 	pcireg_t reg;
197 
198 	for (i = 0; i < agp_ali_table_size; i++)
199 		if (agp_ali_table[i] == aperture)
200 			break;
201 	if (i == agp_ali_table_size)
202 		return (EINVAL);
203 
204 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, AGP_ALI_ATTBASE);
205 	reg &= ~0xff;
206 	reg |= i;
207 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_ATTBASE, reg);
208 	return (0);
209 }
210 
211 int
212 agp_ali_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
213 {
214 	struct agp_ali_softc *asc = sc->sc_chipc;
215 
216 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
217 		return (EINVAL);
218 
219 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical;
220 	return (0);
221 }
222 
223 int
224 agp_ali_unbind_page(struct agp_softc *sc, off_t offset)
225 {
226 	struct agp_ali_softc *asc = sc->sc_chipc;
227 
228 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
229 		return (EINVAL);
230 
231 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
232 	return (0);
233 }
234 
235 void
236 agp_ali_flush_tlb(struct agp_softc *sc)
237 {
238 	pcireg_t reg;
239 
240 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL);
241 	reg &= ~0xff;
242 	reg |= 0x90;
243 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL, reg);
244 	reg &= ~0xff;
245 	reg |= 0x10;
246 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, AGP_ALI_TLBCTRL, reg);
247 }
248 
249