Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / agp / agp_x86_64.c
1 /*-
2  * Copyright (c) 2004, 2005 Jung-uk Kim <jkim@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/agp/agp_amd64.c,v 1.16 2007/11/12 21:51:36 jhb Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/proc.h>
37
38 #include <bus/pci/pcivar.h>
39 #include <bus/pci/pcireg.h>
40 #include "agppriv.h"
41 #include "agpreg.h"
42
43 #include <vm/vm.h>
44 #include <vm/vm_object.h>
45 #include <vm/pmap.h>
46 #include <sys/rman.h>
47
48 /* XXX */
49 extern void pci_cfgregwrite(int, int, int, int, uint32_t, int);
50 extern uint32_t pci_cfgregread(int, int, int, int, int);
51
52 static void agp_amd64_apbase_fixup(device_t);
53
54 static void agp_amd64_uli_init(device_t);
55 static int agp_amd64_uli_set_aperture(device_t, uint32_t);
56
57 static int agp_amd64_nvidia_match(uint16_t);
58 static void agp_amd64_nvidia_init(device_t);
59 static int agp_amd64_nvidia_set_aperture(device_t, uint32_t);
60
61 static int agp_amd64_via_match(void);
62 static void agp_amd64_via_init(device_t);
63 static int agp_amd64_via_set_aperture(device_t, uint32_t);
64
65 MALLOC_DECLARE(M_AGP);
66
67 #define AMD64_MAX_MCTRL         8
68
69 struct agp_amd64_softc {
70         struct agp_softc        agp;
71         uint32_t                initial_aperture;
72         struct agp_gatt         *gatt;
73         uint32_t                apbase;
74         int                     mctrl[AMD64_MAX_MCTRL];
75         int                     n_mctrl;
76         int                     via_agp;
77 };
78
79 static const char*
80 agp_amd64_match(device_t dev)
81 {
82         if (pci_get_class(dev) != PCIC_BRIDGE ||
83             pci_get_subclass(dev) != PCIS_BRIDGE_HOST ||
84             agp_find_caps(dev) == 0)
85                 return (NULL);
86
87         switch (pci_get_devid(dev)) {
88         case 0x74541022:
89                 return ("AMD 8151 AGP graphics tunnel");
90         case 0x07551039:
91                 return ("SiS 755 host to AGP bridge");
92         case 0x07601039:
93                 return ("SiS 760 host to AGP bridge");
94         case 0x168910b9:
95                 return ("ULi M1689 AGP Controller");
96         case 0x00d110de:
97                 if (agp_amd64_nvidia_match(0x00d2))
98                         return (NULL);
99                 return ("NVIDIA nForce3 AGP Controller");
100         case 0x00e110de:
101                 if (agp_amd64_nvidia_match(0x00e2))
102                         return (NULL);
103                 return ("NVIDIA nForce3-250 AGP Controller");
104         case 0x02041106:
105                 return ("VIA 8380 host to PCI bridge");
106         case 0x02381106:
107                 return ("VIA 3238 host to PCI bridge");
108         case 0x02821106:
109                 return ("VIA K8T800Pro host to PCI bridge");
110         case 0x31881106:
111                 return ("VIA 8385 host to PCI bridge");
112         };
113
114         return (NULL);
115 }
116
117 static int
118 agp_amd64_nvidia_match(uint16_t devid)
119 {
120         /* XXX nForce3 requires secondary AGP bridge at 0:11:0. */
121         if (pci_cfgregread(0, 11, 0, PCIR_CLASS, 1) != PCIC_BRIDGE ||
122             pci_cfgregread(0, 11, 0, PCIR_SUBCLASS, 1) != PCIS_BRIDGE_PCI ||
123             pci_cfgregread(0, 11, 0, PCIR_VENDOR, 2) != 0x10de ||
124             pci_cfgregread(0, 11, 0, PCIR_DEVICE, 2) != devid)
125                 return (ENXIO);
126
127         return (0);
128 }
129
130 static int
131 agp_amd64_via_match(void)
132 {
133         /* XXX Some VIA bridge requires secondary AGP bridge at 0:1:0. */
134         if (pci_cfgregread(0, 1, 0, PCIR_CLASS, 1) != PCIC_BRIDGE ||
135             pci_cfgregread(0, 1, 0, PCIR_SUBCLASS, 1) != PCIS_BRIDGE_PCI ||
136             pci_cfgregread(0, 1, 0, PCIR_VENDOR, 2) != 0x1106 ||
137             pci_cfgregread(0, 1, 0, PCIR_DEVICE, 2) != 0xb188 ||
138             (pci_cfgregread(0, 1, 0, AGP_VIA_AGPSEL, 1) & 2))
139                 return (0);
140
141         return (1);
142 }
143
144 static int
145 agp_amd64_probe(device_t dev)
146 {
147         const char *desc;
148
149         if (resource_disabled("agp", device_get_unit(dev)))
150                 return (ENXIO);
151         if ((desc = agp_amd64_match(dev))) {
152                 device_set_desc(dev, desc);
153                 return (BUS_PROBE_DEFAULT);
154         }
155
156         return (ENXIO);
157 }
158
159 static int
160 agp_amd64_attach(device_t dev)
161 {
162         struct agp_amd64_softc *sc = device_get_softc(dev);
163         struct agp_gatt *gatt;
164         int i, n, error;
165
166         for (i = 0, n = 0; i < PCI_SLOTMAX && n < AMD64_MAX_MCTRL; i++)
167                 if (pci_cfgregread(0, i, 3, 0, 4) == 0x11031022) {
168                         sc->mctrl[n] = i;
169                         n++;
170                 }
171
172         if (n == 0)
173                 return (ENXIO);
174
175         sc->n_mctrl = n;
176
177         if (bootverbose)
178                 device_printf(dev, "%d Miscellaneous Control unit(s) found.\n",
179                     sc->n_mctrl);
180
181         if ((error = agp_generic_attach(dev)))
182                 return (error);
183
184         sc->initial_aperture = AGP_GET_APERTURE(dev);
185         if (sc->initial_aperture == 0) {
186                 device_printf(dev, "bad initial aperture size, disabling\n");
187                 return ENXIO;
188         }
189
190         for (;;) {
191                 gatt = agp_alloc_gatt(dev);
192                 if (gatt)
193                         break;
194
195                 /*
196                  * Probably contigmalloc failure. Try reducing the
197                  * aperture so that the gatt size reduces.
198                  */
199                 if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2)) {
200                         agp_generic_detach(dev);
201                         return (ENOMEM);
202                 }
203         }
204         sc->gatt = gatt;
205
206         switch (pci_get_vendor(dev)) {
207         case 0x10b9:    /* ULi */
208                 agp_amd64_uli_init(dev);
209                 if (agp_amd64_uli_set_aperture(dev, sc->initial_aperture))
210                         return (ENXIO);
211                 break;
212
213         case 0x10de:    /* nVidia */
214                 agp_amd64_nvidia_init(dev);
215                 if (agp_amd64_nvidia_set_aperture(dev, sc->initial_aperture))
216                         return (ENXIO);
217                 break;
218
219         case 0x1106:    /* VIA */
220                 sc->via_agp = agp_amd64_via_match();
221                 if (sc->via_agp) {
222                         agp_amd64_via_init(dev);
223                         if (agp_amd64_via_set_aperture(dev,
224                             sc->initial_aperture))
225                                 return (ENXIO);
226                 }
227                 break;
228         }
229
230         /* Install the gatt and enable aperture. */
231         for (i = 0; i < sc->n_mctrl; i++) {
232                 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_ATTBASE,
233                     (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK,
234                     4);
235                 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL,
236                     (pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) |
237                     AGP_AMD64_APCTRL_GARTEN) &
238                     ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO),
239                     4);
240         }
241
242         agp_flush_cache();
243
244         return (0);
245 }
246
247 static int
248 agp_amd64_detach(device_t dev)
249 {
250         struct agp_amd64_softc *sc = device_get_softc(dev);
251         int i;
252
253         agp_free_cdev(dev);
254
255         for (i = 0; i < sc->n_mctrl; i++)
256                 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL,
257                     pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_APCTRL, 4) &
258                     ~AGP_AMD64_APCTRL_GARTEN, 4);
259
260         AGP_SET_APERTURE(dev, sc->initial_aperture);
261         agp_free_gatt(sc->gatt);
262         agp_free_res(dev);
263
264         return (0);
265 }
266
267 static uint32_t agp_amd64_table[] = {
268         0x02000000,     /*   32 MB */
269         0x04000000,     /*   64 MB */
270         0x08000000,     /*  128 MB */
271         0x10000000,     /*  256 MB */
272         0x20000000,     /*  512 MB */
273         0x40000000,     /* 1024 MB */
274         0x80000000,     /* 2048 MB */
275 };
276
277 #define AGP_AMD64_TABLE_SIZE NELEM(agp_amd64_table)
278
279 static uint32_t
280 agp_amd64_get_aperture(device_t dev)
281 {
282         struct agp_amd64_softc *sc = device_get_softc(dev);
283         uint32_t i;
284
285         i = (pci_cfgregread(0, sc->mctrl[0], 3, AGP_AMD64_APCTRL, 4) &
286                 AGP_AMD64_APCTRL_SIZE_MASK) >> 1;
287
288         if (i >= AGP_AMD64_TABLE_SIZE)
289                 return (0);
290
291         return (agp_amd64_table[i]);
292 }
293
294 static int
295 agp_amd64_set_aperture(device_t dev, uint32_t aperture)
296 {
297         struct agp_amd64_softc *sc = device_get_softc(dev);
298         uint32_t i;
299         int j;
300
301         for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++)
302                 if (agp_amd64_table[i] == aperture)
303                         break;
304         if (i >= AGP_AMD64_TABLE_SIZE)
305                 return (EINVAL);
306
307         for (j = 0; j < sc->n_mctrl; j++)
308                 pci_cfgregwrite(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL,
309                     (pci_cfgregread(0, sc->mctrl[j], 3, AGP_AMD64_APCTRL, 4) &
310                     ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1), 4);
311
312         switch (pci_get_vendor(dev)) {
313         case 0x10b9:    /* ULi */
314                 return (agp_amd64_uli_set_aperture(dev, aperture));
315                 break;
316
317         case 0x10de:    /* nVidia */
318                 return (agp_amd64_nvidia_set_aperture(dev, aperture));
319                 break;
320
321         case 0x1106:    /* VIA */
322                 if (sc->via_agp)
323                         return (agp_amd64_via_set_aperture(dev, aperture));
324                 break;
325         }
326
327         return (0);
328 }
329
330 static int
331 agp_amd64_bind_page(device_t dev, int offset, vm_offset_t physical)
332 {
333         struct agp_amd64_softc *sc = device_get_softc(dev);
334
335         if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
336                 return (EINVAL);
337
338         sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] =
339             (physical & 0xfffff000) | ((physical >> 28) & 0x00000ff0) | 3;
340
341         return (0);
342 }
343
344 static int
345 agp_amd64_unbind_page(device_t dev, int offset)
346 {
347         struct agp_amd64_softc *sc = device_get_softc(dev);
348
349         if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
350                 return (EINVAL);
351
352         sc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
353
354         return (0);
355 }
356
357 static void
358 agp_amd64_flush_tlb(device_t dev)
359 {
360         struct agp_amd64_softc *sc = device_get_softc(dev);
361         int i;
362
363         for (i = 0; i < sc->n_mctrl; i++)
364                 pci_cfgregwrite(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL,
365                     pci_cfgregread(0, sc->mctrl[i], 3, AGP_AMD64_CACHECTRL, 4) |
366                     AGP_AMD64_CACHECTRL_INVGART, 4);
367 }
368
369 static void
370 agp_amd64_apbase_fixup(device_t dev)
371 {
372         struct agp_amd64_softc *sc = device_get_softc(dev);
373         uint32_t apbase;
374         int i;
375
376         sc->apbase = rman_get_start(sc->agp.as_aperture);
377         apbase = (sc->apbase >> 25) & AGP_AMD64_APBASE_MASK;
378         for (i = 0; i < sc->n_mctrl; i++)
379                 pci_cfgregwrite(0, sc->mctrl[i], 3,
380                     AGP_AMD64_APBASE, apbase, 4);
381 }
382
383 static void
384 agp_amd64_uli_init(device_t dev)
385 {
386         struct agp_amd64_softc *sc = device_get_softc(dev);
387
388         agp_amd64_apbase_fixup(dev);
389         pci_write_config(dev, AGP_AMD64_ULI_APBASE,
390             (pci_read_config(dev, AGP_AMD64_ULI_APBASE, 4) & 0x0000000f) |
391             sc->apbase, 4);
392         pci_write_config(dev, AGP_AMD64_ULI_HTT_FEATURE, sc->apbase, 4);
393 }
394
395 static int
396 agp_amd64_uli_set_aperture(device_t dev, uint32_t aperture)
397 {
398         struct agp_amd64_softc *sc = device_get_softc(dev);
399
400         switch (aperture) {
401         case 0x02000000:        /*  32 MB */
402         case 0x04000000:        /*  64 MB */
403         case 0x08000000:        /* 128 MB */
404         case 0x10000000:        /* 256 MB */
405                 break;
406         default:
407                 return (EINVAL);
408         }
409
410         pci_write_config(dev, AGP_AMD64_ULI_ENU_SCR,
411             sc->apbase + aperture - 1, 4);
412
413         return (0);
414 }
415
416 static void
417 agp_amd64_nvidia_init(device_t dev)
418 {
419         struct agp_amd64_softc *sc = device_get_softc(dev);
420
421         agp_amd64_apbase_fixup(dev);
422         pci_write_config(dev, AGP_AMD64_NVIDIA_0_APBASE,
423             (pci_read_config(dev, AGP_AMD64_NVIDIA_0_APBASE, 4) & 0x0000000f) |
424             sc->apbase, 4);
425         pci_cfgregwrite(0, 11, 0, AGP_AMD64_NVIDIA_1_APBASE1, sc->apbase, 4);
426         pci_cfgregwrite(0, 11, 0, AGP_AMD64_NVIDIA_1_APBASE2, sc->apbase, 4);
427 }
428
429 static int
430 agp_amd64_nvidia_set_aperture(device_t dev, uint32_t aperture)
431 {
432         struct agp_amd64_softc *sc = device_get_softc(dev);
433         uint32_t apsize;
434
435         switch (aperture) {
436         case 0x02000000:        apsize = 0x0f;  break;  /*  32 MB */
437         case 0x04000000:        apsize = 0x0e;  break;  /*  64 MB */
438         case 0x08000000:        apsize = 0x0c;  break;  /* 128 MB */
439         case 0x10000000:        apsize = 0x08;  break;  /* 256 MB */
440         case 0x20000000:        apsize = 0x00;  break;  /* 512 MB */
441         default:
442                 return (EINVAL);
443         }
444
445         pci_cfgregwrite(0, 11, 0, AGP_AMD64_NVIDIA_1_APSIZE,
446             (pci_cfgregread(0, 11, 0, AGP_AMD64_NVIDIA_1_APSIZE, 4) &
447             0xfffffff0) | apsize, 4);
448         pci_cfgregwrite(0, 11, 0, AGP_AMD64_NVIDIA_1_APLIMIT1,
449             sc->apbase + aperture - 1, 4);
450         pci_cfgregwrite(0, 11, 0, AGP_AMD64_NVIDIA_1_APLIMIT2,
451             sc->apbase + aperture - 1, 4);
452
453         return (0);
454 }
455
456 static void
457 agp_amd64_via_init(device_t dev)
458 {
459         struct agp_amd64_softc *sc = device_get_softc(dev);
460
461         agp_amd64_apbase_fixup(dev);
462         pci_cfgregwrite(0, 1, 0, AGP3_VIA_ATTBASE, sc->gatt->ag_physical, 4);
463         pci_cfgregwrite(0, 1, 0, AGP3_VIA_GARTCTRL,
464             pci_cfgregread(0, 1, 0, AGP3_VIA_ATTBASE, 4) | 0x180, 4);
465 }
466
467 static int
468 agp_amd64_via_set_aperture(device_t dev, uint32_t aperture)
469 {
470         uint32_t apsize;
471
472         apsize = ((aperture - 1) >> 20) ^ 0xff;
473         if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
474                 return (EINVAL);
475         pci_cfgregwrite(0, 1, 0, AGP3_VIA_APSIZE, apsize, 1);
476
477         return (0);
478 }
479
480 static device_method_t agp_amd64_methods[] = {
481         /* Device interface */
482         DEVMETHOD(device_probe,         agp_amd64_probe),
483         DEVMETHOD(device_attach,        agp_amd64_attach),
484         DEVMETHOD(device_detach,        agp_amd64_detach),
485         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
486         DEVMETHOD(device_suspend,       bus_generic_suspend),
487         DEVMETHOD(device_resume,        bus_generic_resume),
488
489         /* AGP interface */
490         DEVMETHOD(agp_get_aperture,     agp_amd64_get_aperture),
491         DEVMETHOD(agp_set_aperture,     agp_amd64_set_aperture),
492         DEVMETHOD(agp_bind_page,        agp_amd64_bind_page),
493         DEVMETHOD(agp_unbind_page,      agp_amd64_unbind_page),
494         DEVMETHOD(agp_flush_tlb,        agp_amd64_flush_tlb),
495         DEVMETHOD(agp_enable,           agp_generic_enable),
496         DEVMETHOD(agp_alloc_memory,     agp_generic_alloc_memory),
497         DEVMETHOD(agp_free_memory,      agp_generic_free_memory),
498         DEVMETHOD(agp_bind_memory,      agp_generic_bind_memory),
499         DEVMETHOD(agp_unbind_memory,    agp_generic_unbind_memory),
500
501         { 0, 0 }
502 };
503
504 static driver_t agp_amd64_driver = {
505         "agp",
506         agp_amd64_methods,
507         sizeof(struct agp_amd64_softc),
508 };
509
510 static devclass_t agp_devclass;
511
512 DRIVER_MODULE(agp_amd64, pci, agp_amd64_driver, agp_devclass, NULL, NULL);
513 MODULE_DEPEND(agp_amd64, agp, 1, 1, 1);
514 MODULE_DEPEND(agp_amd64, pci, 1, 1, 1);