nouveau_agp.c 4.5 KB

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