xref: /openbsd-src/sys/dev/pci/agp_apple.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: agp_apple.c,v 1.5 2014/03/29 18:09:30 guenther Exp $	*/
2 
3 /*
4  * Copyright (c) 2012 Martin Pieuchot <mpi@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/malloc.h>
22 #include <sys/device.h>
23 #include <sys/rwlock.h>
24 
25 #include <dev/pci/pcivar.h>
26 #include <dev/pci/pcireg.h>
27 #include <dev/pci/pcidevs.h>
28 #include <dev/pci/agpvar.h>
29 #include <dev/pci/agpreg.h>
30 
31 #include <machine/bus.h>
32 
33 #define M (1024 * 1024)
34 
35 struct agp_apple_softc {
36 	struct device		 dev;
37 	struct agp_softc	*agpdev;
38 	struct agp_gatt		*gatt;
39 	pci_chipset_tag_t	 asc_pc;
40 	pcitag_t		 asc_tag;
41 	bus_addr_t		 asc_apaddr;
42 	bus_size_t		 asc_apsize;
43 	int			 asc_flags;
44 #define	AGP_APPLE_ISU3 (1 << 0)
45 };
46 
47 int	agp_apple_match(struct device *, void *, void *);
48 void	agp_apple_attach(struct device *, struct device *, void *);
49 bus_size_t agp_apple_get_aperture(void *);
50 int	agp_apple_set_aperture(void *sc, bus_size_t);
51 void	agp_apple_bind_page(void *, bus_addr_t, paddr_t, int);
52 void	agp_apple_unbind_page(void *, bus_addr_t);
53 void	agp_apple_flush_tlb(void *);
54 
55 const struct cfattach appleagp_ca = {
56 	sizeof(struct agp_apple_softc), agp_apple_match, agp_apple_attach,
57 };
58 
59 struct cfdriver appleagp_cd = {
60 	NULL, "appleagp", DV_DULL
61 };
62 
63 const struct agp_methods agp_apple_methods = {
64 	agp_apple_bind_page,
65 	agp_apple_unbind_page,
66 	agp_apple_flush_tlb,
67 };
68 
69 int
70 agp_apple_match(struct device *parent, void *match, void *aux)
71 {
72 	struct agp_attach_args *aa = aux;
73 	struct pci_attach_args *pa = aa->aa_pa;
74 
75 	if (agpbus_probe(aa) != 1 || PCI_VENDOR(pa->pa_id) != PCI_VENDOR_APPLE)
76 		return (0);
77 
78 	switch (PCI_PRODUCT(pa->pa_id)) {
79 		case PCI_PRODUCT_APPLE_UNINORTH_AGP:
80 		case PCI_PRODUCT_APPLE_PANGEA_AGP:
81 		case PCI_PRODUCT_APPLE_UNINORTH2_AGP:
82 		case PCI_PRODUCT_APPLE_UNINORTH_AGP3:
83 		case PCI_PRODUCT_APPLE_INTREPID2_AGP:
84 		case PCI_PRODUCT_APPLE_U3_AGP:
85 		case PCI_PRODUCT_APPLE_U3L_AGP:
86 		case PCI_PRODUCT_APPLE_K2_AGP:
87 			return (1);
88 	}
89 
90 	return (0);
91 }
92 
93 void
94 agp_apple_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct agp_apple_softc *asc = (struct agp_apple_softc *)self;
97 	struct agp_attach_args *aa = aux;
98 	struct pci_attach_args *pa = aa->aa_pa;
99 	struct agp_gatt *gatt;
100 
101 	asc->asc_tag = pa->pa_tag;
102 	asc->asc_pc = pa->pa_pc;
103 
104 	switch (PCI_PRODUCT(pa->pa_id)) {
105 		case PCI_PRODUCT_APPLE_U3_AGP:
106 		case PCI_PRODUCT_APPLE_U3L_AGP:
107 		case PCI_PRODUCT_APPLE_K2_AGP:
108 			asc->asc_flags |= AGP_APPLE_ISU3;
109 			break;
110 		default:
111 			break;
112 	}
113 
114 	/*
115 	 * XXX It looks like UniNorth GART only accepts an aperture
116 	 * base address of 0x00 certainly because it does not perform
117 	 * any physical-to-physical address translation.
118 	 */
119 	asc->asc_apaddr = 0x00;
120 	pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_APBASE,
121 	    asc->asc_apaddr);
122 
123 	/*
124 	 * There's no way to read the aperture size but all UniNorth
125 	 * chips seem to support an aperture of 256M (and 512M for U3).
126 	 */
127 	asc->asc_apsize = 256 * M;
128 	for (;;) {
129 		gatt = agp_alloc_gatt(pa->pa_dmat, asc->asc_apsize);
130 		if (gatt != NULL)
131 			break;
132 
133 		asc->asc_apsize /= 2;
134 	}
135 	asc->gatt = gatt;
136 
137 	if (agp_apple_set_aperture(asc, asc->asc_apsize)) {
138 		printf("failed to set aperture\n");
139 		return;
140 	}
141 
142 	agp_apple_flush_tlb(asc);
143 
144 	asc->agpdev = (struct agp_softc *)agp_attach_bus(pa, &agp_apple_methods,
145 	    asc->asc_apaddr, asc->asc_apsize, &asc->dev);
146 }
147 
148 bus_size_t
149 agp_apple_get_aperture(void *dev)
150 {
151 	struct agp_apple_softc *asc = dev;
152 
153 	return (asc->asc_apsize);
154 }
155 
156 int
157 agp_apple_set_aperture(void *dev, bus_size_t aperture)
158 {
159 	struct agp_apple_softc *asc = dev;
160 
161 	if (aperture % (4 * M) || aperture < (4 * M) || aperture > (256 * M))
162 		return (EINVAL);
163 
164 	pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_ATTBASE,
165 	    (asc->gatt->ag_physical & 0xfffff000) | (aperture >> 22));
166 
167 	return (0);
168 }
169 
170 #define flushd(p) __asm volatile("dcbst 0,%0; sync" ::"r"(p) : "memory")
171 
172 void
173 agp_apple_bind_page(void *v, bus_addr_t off, paddr_t pa, int flags)
174 {
175 	struct agp_apple_softc *asc = v;
176 	uint32_t entry;
177 
178 	if (off >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
179 		return;
180 
181 	if (asc->asc_flags & AGP_APPLE_ISU3)
182 		entry = (pa >> PAGE_SHIFT | 0x80000000);
183 	else
184 		entry = htole32(pa | 0x01);
185 
186 	asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT] = entry;
187 
188 	flushd(&asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT]);
189 }
190 
191 void
192 agp_apple_unbind_page(void *v, bus_size_t off)
193 {
194 	struct agp_apple_softc *asc = v;
195 
196 	if (off >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
197 		return;
198 
199 	asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT] = 0;
200 
201 	flushd(&asc->gatt->ag_virtual[off >> AGP_PAGE_SHIFT]);
202 }
203 
204 #undef flushd
205 
206 void
207 agp_apple_flush_tlb(void *v)
208 {
209 	struct agp_apple_softc *asc = v;
210 
211 	if (asc->asc_flags & AGP_APPLE_ISU3) {
212 		pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL,
213 		    AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_PERFRD |
214 		    AGP_APPLE_GART_INVALIDATE);
215 		pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL,
216 		    AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_PERFRD);
217 	} else {
218 		pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL,
219 		    AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_INVALIDATE);
220 		pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL,
221 		    AGP_APPLE_GART_ENABLE);
222 		pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL,
223 		    AGP_APPLE_GART_ENABLE | AGP_APPLE_GART_2XRESET);
224 		pci_conf_write(asc->asc_pc, asc->asc_tag, AGP_APPLE_GARTCTRL,
225 		    AGP_APPLE_GART_ENABLE);
226 	}
227 }
228