1/* $NetBSD: nouveau_agp.c,v 1.3 2015/04/03 01:09:42 riastradh Exp $ */
2
3#include <sys/cdefs.h>
4__KERNEL_RCSID(0, "$NetBSD: nouveau_agp.c,v 1.3 2015/04/03 01:09:42 riastradh Exp $");
5
6#include <linux/module.h>
7#include <linux/moduleparam.h>
8
9#include <core/device.h>
10
11#include "nouveau_drm.h"
12#include "nouveau_agp.h"
13#include "nouveau_reg.h"
14
15#if __OS_HAS_AGP
16MODULE_PARM_DESC(agpmode, "AGP mode (0 to disable AGP)");
17static int nouveau_agpmode = -1;
18module_param_named(agpmode, nouveau_agpmode, int, 0400);
19
20struct nouveau_agpmode_quirk {
21 u16 hostbridge_vendor;
22 u16 hostbridge_device;
23 u16 chip_vendor;
24 u16 chip_device;
25 int mode;
26};
27
28static struct nouveau_agpmode_quirk nouveau_agpmode_quirk_list[] = {
29 /* VIA Apollo PRO133x / GeForce FX 5600 Ultra, max agpmode 2, fdo #20341 */
30 { PCI_VENDOR_ID_VIA, 0x0691, PCI_VENDOR_ID_NVIDIA, 0x0311, 2 },
31
32 {},
33};
34
35static unsigned long
36get_agp_mode(struct nouveau_drm *drm, const struct drm_agp_info *info)
37{
38 struct nouveau_device *device = nv_device(drm->device);
39 struct nouveau_agpmode_quirk *quirk = nouveau_agpmode_quirk_list;
40 int agpmode = nouveau_agpmode;
41 unsigned long mode = info->mode;
42
43 /*
44 * FW seems to be broken on nv18, it makes the card lock up
45 * randomly.
46 */
47 if (device->chipset == 0x18)
48 mode &= ~PCI_AGP_COMMAND_FW;
49
50 /*
51 * Go through the quirks list and adjust the agpmode accordingly.
52 */
53 while (agpmode == -1 && quirk->hostbridge_vendor) {
54 if (info->id_vendor == quirk->hostbridge_vendor &&
55 info->id_device == quirk->hostbridge_device &&
56 device->pdev->vendor == quirk->chip_vendor &&
57 device->pdev->device == quirk->chip_device) {
58 agpmode = quirk->mode;
59 nv_info(device, "Forcing agp mode to %dX. Use agpmode to override.\n",
60 agpmode);
61 break;
62 }
63 ++quirk;
64 }
65
66 /*
67 * AGP mode set in the command line.
68 */
69 if (agpmode > 0) {
70 bool agpv3 = mode & 0x8;
71 int rate = agpv3 ? agpmode / 4 : agpmode;
72
73 mode = (mode & ~0x7) | (rate & 0x7);
74 }
75
76 return mode;
77}
78
79static bool
80nouveau_agp_enabled(struct nouveau_drm *drm)
81{
82 struct drm_device *dev = drm->dev;
83
84 if (!dev->pdev || !drm_pci_device_is_agp(dev) || !dev->agp)
85 return false;
86
87 if (drm->agp.stat == UNKNOWN) {
88 if (!nouveau_agpmode)
89 return false;
90#ifdef __powerpc__
91 /* Disable AGP by default on all PowerPC machines for
92 * now -- At least some UniNorth-2 AGP bridges are
93 * known to be broken: DMA from the host to the card
94 * works just fine, but writeback from the card to the
95 * host goes straight to memory untranslated bypassing
96 * the GATT somehow, making them quite painful to deal
97 * with...
98 */
99 if (nouveau_agpmode == -1)
100 return false;
101#endif
102 return true;
103 }
104
105 return (drm->agp.stat == ENABLED);
106}
107#endif
108
109void
110nouveau_agp_reset(struct nouveau_drm *drm)
111{
112#if __OS_HAS_AGP
113 struct nouveau_device *device = nv_device(drm->device);
114 struct drm_device *dev = drm->dev;
115 u32 save[2];
116 int ret;
117
118 if (!nouveau_agp_enabled(drm))
119 return;
120
121 /* First of all, disable fast writes, otherwise if it's
122 * already enabled in the AGP bridge and we disable the card's
123 * AGP controller we might be locking ourselves out of it. */
124 if ((nv_rd32(device, NV04_PBUS_PCI_NV_19) |
125 dev->agp->mode) & PCI_AGP_COMMAND_FW) {
126 struct drm_agp_info info;
127 struct drm_agp_mode mode;
128
129 ret = drm_agp_info(dev, &info);
130 if (ret)
131 return;
132
133 mode.mode = get_agp_mode(drm, &info);
134 mode.mode &= ~PCI_AGP_COMMAND_FW;
135
136 ret = drm_agp_enable(dev, mode);
137 if (ret)
138 return;
139 }
140
141
142 /* clear busmaster bit, and disable AGP */
143 save[0] = nv_mask(device, NV04_PBUS_PCI_NV_1, 0x00000004, 0x00000000);
144 nv_wr32(device, NV04_PBUS_PCI_NV_19, 0);
145
146 /* reset PGRAPH, PFIFO and PTIMER */
147 save[1] = nv_mask(device, 0x000200, 0x00011100, 0x00000000);
148 nv_mask(device, 0x000200, 0x00011100, save[1]);
149
150 /* and restore bustmaster bit (gives effect of resetting AGP) */
151 nv_wr32(device, NV04_PBUS_PCI_NV_1, save[0]);
152#endif
153}
154
155void
156nouveau_agp_init(struct nouveau_drm *drm)
157{
158#if __OS_HAS_AGP
159 struct nouveau_device *device = nv_device(drm->device);
160 struct drm_device *dev = drm->dev;
161 struct drm_agp_info info;
162 struct drm_agp_mode mode;
163 int ret;
164
165 if (!nouveau_agp_enabled(drm))
166 return;
167 drm->agp.stat = DISABLE;
168
169 ret = drm_agp_acquire(dev);
170 if (ret) {
171 nv_error(device, "unable to acquire AGP: %d\n", ret);
172 return;
173 }
174
175 ret = drm_agp_info(dev, &info);
176 if (ret) {
177 nv_error(device, "unable to get AGP info: %d\n", ret);
178 return;
179 }
180
181 /* see agp.h for the AGPSTAT_* modes available */
182 mode.mode = get_agp_mode(drm, &info);
183
184 ret = drm_agp_enable(dev, mode);
185 if (ret) {
186 nv_error(device, "unable to enable AGP: %d\n", ret);
187 return;
188 }
189
190 drm->agp.stat = ENABLED;
191 drm->agp.base = info.aperture_base;
192 drm->agp.size = info.aperture_size;
193#ifdef __NetBSD__
194 pmap_pv_track(drm->agp.base, drm->agp.size);
195#endif
196#endif
197}
198
199void
200nouveau_agp_fini(struct nouveau_drm *drm)
201{
202#if __OS_HAS_AGP
203 struct drm_device *dev = drm->dev;
204 if (dev->agp && dev->agp->acquired) {
205#ifdef __NetBSD__
206 pmap_pv_untrack(drm->agp.base, drm->agp.size);
207#endif
208 drm_agp_release(dev);
209 }
210#endif
211}
212