blob: d28430cd2ba6066ad62c118b8d13d59fa7a88793 [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
14static unsigned long
Ben Skeggscb75d972012-07-11 10:44:20 +100015get_agp_mode(struct nouveau_drm *drm, unsigned long mode)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100016{
Ben Skeggscb75d972012-07-11 10:44:20 +100017 struct nouveau_device *device = nv_device(drm->device);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100018
19 /*
20 * FW seems to be broken on nv18, it makes the card lock up
21 * randomly.
22 */
Ben Skeggscb75d972012-07-11 10:44:20 +100023 if (device->chipset == 0x18)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100024 mode &= ~PCI_AGP_COMMAND_FW;
25
26 /*
27 * AGP mode set in the command line.
28 */
29 if (nouveau_agpmode > 0) {
30 bool agpv3 = mode & 0x8;
31 int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
32
33 mode = (mode & ~0x7) | (rate & 0x7);
34 }
35
36 return mode;
37}
38
39static bool
Ben Skeggscb75d972012-07-11 10:44:20 +100040nouveau_agp_enabled(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100041{
Ben Skeggscb75d972012-07-11 10:44:20 +100042 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100043
44 if (!drm_pci_device_is_agp(dev) || !dev->agp)
45 return false;
46
Ben Skeggscb75d972012-07-11 10:44:20 +100047 if (drm->agp.stat == UNKNOWN) {
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100048 if (!nouveau_agpmode)
49 return false;
Ben Skeggscb75d972012-07-11 10:44:20 +100050 return true;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100051 }
52
Ben Skeggscb75d972012-07-11 10:44:20 +100053 return (drm->agp.stat == ENABLED);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100054}
55#endif
56
57void
Ben Skeggscb75d972012-07-11 10:44:20 +100058nouveau_agp_reset(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100059{
60#if __OS_HAS_AGP
Ben Skeggscb75d972012-07-11 10:44:20 +100061 struct nouveau_device *device = nv_device(drm->device);
62 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100063 u32 save[2];
64 int ret;
65
Ben Skeggscb75d972012-07-11 10:44:20 +100066 if (!nouveau_agp_enabled(drm))
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100067 return;
68
69 /* First of all, disable fast writes, otherwise if it's
70 * already enabled in the AGP bridge and we disable the card's
71 * AGP controller we might be locking ourselves out of it. */
Ben Skeggscb75d972012-07-11 10:44:20 +100072 if ((nv_rd32(device, NV04_PBUS_PCI_NV_19) |
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100073 dev->agp->mode) & PCI_AGP_COMMAND_FW) {
74 struct drm_agp_info info;
75 struct drm_agp_mode mode;
76
77 ret = drm_agp_info(dev, &info);
78 if (ret)
79 return;
80
Ben Skeggscb75d972012-07-11 10:44:20 +100081 mode.mode = get_agp_mode(drm, info.mode);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100082 mode.mode &= ~PCI_AGP_COMMAND_FW;
83
84 ret = drm_agp_enable(dev, mode);
85 if (ret)
86 return;
87 }
88
89
90 /* clear busmaster bit, and disable AGP */
Ben Skeggscb75d972012-07-11 10:44:20 +100091 save[0] = nv_mask(device, NV04_PBUS_PCI_NV_1, 0x00000004, 0x00000000);
92 nv_wr32(device, NV04_PBUS_PCI_NV_19, 0);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100093
94 /* reset PGRAPH, PFIFO and PTIMER */
Ben Skeggscb75d972012-07-11 10:44:20 +100095 save[1] = nv_mask(device, 0x000200, 0x00011100, 0x00000000);
96 nv_mask(device, 0x000200, 0x00011100, save[1]);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +100097
98 /* and restore bustmaster bit (gives effect of resetting AGP) */
Ben Skeggscb75d972012-07-11 10:44:20 +100099 nv_wr32(device, NV04_PBUS_PCI_NV_1, save[0]);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000100#endif
101}
102
103void
Ben Skeggscb75d972012-07-11 10:44:20 +1000104nouveau_agp_init(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 struct drm_agp_info info;
110 struct drm_agp_mode mode;
111 int ret;
112
Ben Skeggscb75d972012-07-11 10:44:20 +1000113 if (!nouveau_agp_enabled(drm))
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000114 return;
Ben Skeggscb75d972012-07-11 10:44:20 +1000115 drm->agp.stat = DISABLE;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000116
117 ret = drm_agp_acquire(dev);
118 if (ret) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000119 nv_error(device, "unable to acquire AGP: %d\n", ret);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000120 return;
121 }
122
123 ret = drm_agp_info(dev, &info);
124 if (ret) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000125 nv_error(device, "unable to get AGP info: %d\n", ret);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000126 return;
127 }
128
129 /* see agp.h for the AGPSTAT_* modes available */
Ben Skeggscb75d972012-07-11 10:44:20 +1000130 mode.mode = get_agp_mode(drm, info.mode);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000131
132 ret = drm_agp_enable(dev, mode);
133 if (ret) {
Ben Skeggscb75d972012-07-11 10:44:20 +1000134 nv_error(device, "unable to enable AGP: %d\n", ret);
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000135 return;
136 }
137
Ben Skeggscb75d972012-07-11 10:44:20 +1000138 drm->agp.stat = ENABLED;
139 drm->agp.base = info.aperture_base;
140 drm->agp.size = info.aperture_size;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000141#endif
142}
143
144void
Ben Skeggscb75d972012-07-11 10:44:20 +1000145nouveau_agp_fini(struct nouveau_drm *drm)
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000146{
147#if __OS_HAS_AGP
Ben Skeggscb75d972012-07-11 10:44:20 +1000148 struct drm_device *dev = drm->dev;
Ben Skeggsaa4cc5d22012-07-05 21:36:32 +1000149 if (dev->agp && dev->agp->acquired)
150 drm_agp_release(dev);
151#endif
152}