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