blob: 51666daddb948fd3650f6c14dd7d90963d8ce5dd [file] [log] [blame]
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +10001#include <linux/module.h>
2
Ben Skeggscb75d972012-07-11 10:44:20 +10003#include <core/device.h>
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +10004
Ben Skeggscb75d972012-07-11 10:44:20 +10005#include "nouveau_drm.h"
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +10006#include "nouveau_agp.h"
Ben Skeggscb75d972012-07-11 10:44:20 +10007#include "nouveau_reg.h"
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +10008
9#if __OS_HAS_AGP
10MODULE_PARM_DESC(agpmode, "AGP mode (0 to disable AGP)");
11static int nouveau_agpmode = -1;
12module_param_named(agpmode, nouveau_agpmode, int, 0400);
13
Ilia Mirkinfd34381b2013-10-27 11:54:09 -040014struct nouveau_agpmode_quirk {
15 u16 hostbridge_vendor;
16 u16 hostbridge_device;
17 u16 chip_vendor;
18 u16 chip_device;
19 int mode;
20};
21
22static struct nouveau_agpmode_quirk nouveau_agpmode_quirk_list[] = {
23 /* VIA Apollo PRO133x / GeForce FX 5600 Ultra, max agpmode 2, fdo #20341 */
24 { PCI_VENDOR_ID_VIA, 0x0691, PCI_VENDOR_ID_NVIDIA, 0x0311, 2 },
25
26 {},
27};
28
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100029static unsigned long
Ilia Mirkinfd34381b2013-10-27 11:54:09 -040030get_agp_mode(struct nouveau_drm *drm, const struct drm_agp_info *info)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100031{
Ben Skeggscb75d972012-07-11 10:44:20 +100032 struct nouveau_device *device = nv_device(drm->device);
Ilia Mirkinfd34381b2013-10-27 11:54:09 -040033 struct nouveau_agpmode_quirk *quirk = nouveau_agpmode_quirk_list;
34 int agpmode = nouveau_agpmode;
35 unsigned long mode = info->mode;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100036
37 /*
38 * FW seems to be broken on nv18, it makes the card lock up
39 * randomly.
40 */
Ben Skeggscb75d972012-07-11 10:44:20 +100041 if (device->chipset == 0x18)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100042 mode &= ~PCI_AGP_COMMAND_FW;
43
44 /*
Ilia Mirkinfd34381b2013-10-27 11:54:09 -040045 * Go through the quirks list and adjust the agpmode accordingly.
46 */
47 while (agpmode == -1 && quirk->hostbridge_vendor) {
48 if (info->id_vendor == quirk->hostbridge_vendor &&
49 info->id_device == quirk->hostbridge_device &&
50 device->pdev->vendor == quirk->chip_vendor &&
51 device->pdev->device == quirk->chip_device) {
52 agpmode = quirk->mode;
53 nv_info(device, "Forcing agp mode to %dX. Use agpmode to override.\n",
54 agpmode);
55 break;
56 }
57 ++quirk;
58 }
59
60 /*
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100061 * AGP mode set in the command line.
62 */
Ilia Mirkinfd34381b2013-10-27 11:54:09 -040063 if (agpmode > 0) {
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100064 bool agpv3 = mode & 0x8;
Ilia Mirkinfd34381b2013-10-27 11:54:09 -040065 int rate = agpv3 ? agpmode / 4 : agpmode;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100066
67 mode = (mode & ~0x7) | (rate & 0x7);
68 }
69
70 return mode;
71}
72
73static bool
Ben Skeggscb75d972012-07-11 10:44:20 +100074nouveau_agp_enabled(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100075{
Ben Skeggscb75d972012-07-11 10:44:20 +100076 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100077
Alexandre Courbot420b9462014-02-17 15:17:26 +090078 if (!dev->pdev || !drm_pci_device_is_agp(dev) || !dev->agp)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100079 return false;
80
Ben Skeggscb75d972012-07-11 10:44:20 +100081 if (drm->agp.stat == UNKNOWN) {
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100082 if (!nouveau_agpmode)
83 return false;
Francisco Jerez650e1202013-02-26 02:33:11 +010084#ifdef __powerpc__
85 /* Disable AGP by default on all PowerPC machines for
86 * now -- At least some UniNorth-2 AGP bridges are
87 * known to be broken: DMA from the host to the card
88 * works just fine, but writeback from the card to the
89 * host goes straight to memory untranslated bypassing
90 * the GATT somehow, making them quite painful to deal
91 * with...
92 */
93 if (nouveau_agpmode == -1)
94 return false;
95#endif
Ben Skeggscb75d972012-07-11 10:44:20 +100096 return true;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100097 }
98
Ben Skeggscb75d972012-07-11 10:44:20 +100099 return (drm->agp.stat == ENABLED);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000100}
101#endif
102
103void
Ben Skeggscb75d972012-07-11 10:44:20 +1000104nouveau_agp_reset(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000105{
106#if __OS_HAS_AGP
Ben Skeggscb75d972012-07-11 10:44:20 +1000107 struct nouveau_device *device = nv_device(drm->device);
108 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000109 u32 save[2];
110 int ret;
111
Ben Skeggscb75d972012-07-11 10:44:20 +1000112 if (!nouveau_agp_enabled(drm))
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000113 return;
114
115 /* First of all, disable fast writes, otherwise if it's
116 * already enabled in the AGP bridge and we disable the card's
117 * AGP controller we might be locking ourselves out of it. */
Ben Skeggscb75d972012-07-11 10:44:20 +1000118 if ((nv_rd32(device, NV04_PBUS_PCI_NV_19) |
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000119 dev->agp->mode) & PCI_AGP_COMMAND_FW) {
120 struct drm_agp_info info;
121 struct drm_agp_mode mode;
122
123 ret = drm_agp_info(dev, &info);
124 if (ret)
125 return;
126
Ilia Mirkinfd34381b2013-10-27 11:54:09 -0400127 mode.mode = get_agp_mode(drm, &info);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000128 mode.mode &= ~PCI_AGP_COMMAND_FW;
129
130 ret = drm_agp_enable(dev, mode);
131 if (ret)
132 return;
133 }
134
135
136 /* clear busmaster bit, and disable AGP */
Ben Skeggscb75d972012-07-11 10:44:20 +1000137 save[0] = nv_mask(device, NV04_PBUS_PCI_NV_1, 0x00000004, 0x00000000);
138 nv_wr32(device, NV04_PBUS_PCI_NV_19, 0);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000139
140 /* reset PGRAPH, PFIFO and PTIMER */
Ben Skeggscb75d972012-07-11 10:44:20 +1000141 save[1] = nv_mask(device, 0x000200, 0x00011100, 0x00000000);
142 nv_mask(device, 0x000200, 0x00011100, save[1]);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000143
144 /* and restore bustmaster bit (gives effect of resetting AGP) */
Ben Skeggscb75d972012-07-11 10:44:20 +1000145 nv_wr32(device, NV04_PBUS_PCI_NV_1, save[0]);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000146#endif
147}
148
149void
Ben Skeggscb75d972012-07-11 10:44:20 +1000150nouveau_agp_init(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000151{
152#if __OS_HAS_AGP
Ben Skeggscb75d972012-07-11 10:44:20 +1000153 struct nouveau_device *device = nv_device(drm->device);
154 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000155 struct drm_agp_info info;
156 struct drm_agp_mode mode;
157 int ret;
158
Ben Skeggscb75d972012-07-11 10:44:20 +1000159 if (!nouveau_agp_enabled(drm))
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000160 return;
Ben Skeggscb75d972012-07-11 10:44:20 +1000161 drm->agp.stat = DISABLE;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000162
163 ret = drm_agp_acquire(dev);
164 if (ret) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000165 nv_error(device, "unable to acquire AGP: %d\n", ret);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000166 return;
167 }
168
169 ret = drm_agp_info(dev, &info);
170 if (ret) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000171 nv_error(device, "unable to get AGP info: %d\n", ret);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000172 return;
173 }
174
175 /* see agp.h for the AGPSTAT_* modes available */
Ilia Mirkinfd34381b2013-10-27 11:54:09 -0400176 mode.mode = get_agp_mode(drm, &info);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000177
178 ret = drm_agp_enable(dev, mode);
179 if (ret) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000180 nv_error(device, "unable to enable AGP: %d\n", ret);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000181 return;
182 }
183
Ben Skeggscb75d972012-07-11 10:44:20 +1000184 drm->agp.stat = ENABLED;
185 drm->agp.base = info.aperture_base;
186 drm->agp.size = info.aperture_size;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000187#endif
188}
189
190void
Ben Skeggscb75d972012-07-11 10:44:20 +1000191nouveau_agp_fini(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000192{
193#if __OS_HAS_AGP
Ben Skeggscb75d972012-07-11 10:44:20 +1000194 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000195 if (dev->agp && dev->agp->acquired)
196 drm_agp_release(dev);
197#endif
198}