nouveau_agp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <linux/module.h>
  2. #include <core/device.h>
  3. #include "nouveau_drm.h"
  4. #include "nouveau_agp.h"
  5. #include "nouveau_reg.h"
  6. #if __OS_HAS_AGP
  7. MODULE_PARM_DESC(agpmode, "AGP mode (0 to disable AGP)");
  8. static int nouveau_agpmode = -1;
  9. module_param_named(agpmode, nouveau_agpmode, int, 0400);
  10. static unsigned long
  11. get_agp_mode(struct nouveau_drm *drm, unsigned long mode)
  12. {
  13. struct nouveau_device *device = nv_device(drm->device);
  14. /*
  15. * FW seems to be broken on nv18, it makes the card lock up
  16. * randomly.
  17. */
  18. if (device->chipset == 0x18)
  19. mode &= ~PCI_AGP_COMMAND_FW;
  20. /*
  21. * AGP mode set in the command line.
  22. */
  23. if (nouveau_agpmode > 0) {
  24. bool agpv3 = mode & 0x8;
  25. int rate = agpv3 ? nouveau_agpmode / 4 : nouveau_agpmode;
  26. mode = (mode & ~0x7) | (rate & 0x7);
  27. }
  28. return mode;
  29. }
  30. static bool
  31. nouveau_agp_enabled(struct nouveau_drm *drm)
  32. {
  33. struct drm_device *dev = drm->dev;
  34. if (!drm_pci_device_is_agp(dev) || !dev->agp)
  35. return false;
  36. if (drm->agp.stat == UNKNOWN) {
  37. if (!nouveau_agpmode)
  38. return false;
  39. #ifdef __powerpc__
  40. /* Disable AGP by default on all PowerPC machines for
  41. * now -- At least some UniNorth-2 AGP bridges are
  42. * known to be broken: DMA from the host to the card
  43. * works just fine, but writeback from the card to the
  44. * host goes straight to memory untranslated bypassing
  45. * the GATT somehow, making them quite painful to deal
  46. * with...
  47. */
  48. if (nouveau_agpmode == -1)
  49. return false;
  50. #endif
  51. return true;
  52. }
  53. return (drm->agp.stat == ENABLED);
  54. }
  55. #endif
  56. void
  57. nouveau_agp_reset(struct nouveau_drm *drm)
  58. {
  59. #if __OS_HAS_AGP
  60. struct nouveau_device *device = nv_device(drm->device);
  61. struct drm_device *dev = drm->dev;
  62. u32 save[2];
  63. int ret;
  64. if (!nouveau_agp_enabled(drm))
  65. return;
  66. /* First of all, disable fast writes, otherwise if it's
  67. * already enabled in the AGP bridge and we disable the card's
  68. * AGP controller we might be locking ourselves out of it. */
  69. if ((nv_rd32(device, NV04_PBUS_PCI_NV_19) |
  70. dev->agp->mode) & PCI_AGP_COMMAND_FW) {
  71. struct drm_agp_info info;
  72. struct drm_agp_mode mode;
  73. ret = drm_agp_info(dev, &info);
  74. if (ret)
  75. return;
  76. mode.mode = get_agp_mode(drm, info.mode);
  77. mode.mode &= ~PCI_AGP_COMMAND_FW;
  78. ret = drm_agp_enable(dev, mode);
  79. if (ret)
  80. return;
  81. }
  82. /* clear busmaster bit, and disable AGP */
  83. save[0] = nv_mask(device, NV04_PBUS_PCI_NV_1, 0x00000004, 0x00000000);
  84. nv_wr32(device, NV04_PBUS_PCI_NV_19, 0);
  85. /* reset PGRAPH, PFIFO and PTIMER */
  86. save[1] = nv_mask(device, 0x000200, 0x00011100, 0x00000000);
  87. nv_mask(device, 0x000200, 0x00011100, save[1]);
  88. /* and restore bustmaster bit (gives effect of resetting AGP) */
  89. nv_wr32(device, NV04_PBUS_PCI_NV_1, save[0]);
  90. #endif
  91. }
  92. void
  93. nouveau_agp_init(struct nouveau_drm *drm)
  94. {
  95. #if __OS_HAS_AGP
  96. struct nouveau_device *device = nv_device(drm->device);
  97. struct drm_device *dev = drm->dev;
  98. struct drm_agp_info info;
  99. struct drm_agp_mode mode;
  100. int ret;
  101. if (!nouveau_agp_enabled(drm))
  102. return;
  103. drm->agp.stat = DISABLE;
  104. ret = drm_agp_acquire(dev);
  105. if (ret) {
  106. nv_error(device, "unable to acquire AGP: %d\n", ret);
  107. return;
  108. }
  109. ret = drm_agp_info(dev, &info);
  110. if (ret) {
  111. nv_error(device, "unable to get AGP info: %d\n", ret);
  112. return;
  113. }
  114. /* see agp.h for the AGPSTAT_* modes available */
  115. mode.mode = get_agp_mode(drm, info.mode);
  116. ret = drm_agp_enable(dev, mode);
  117. if (ret) {
  118. nv_error(device, "unable to enable AGP: %d\n", ret);
  119. return;
  120. }
  121. drm->agp.stat = ENABLED;
  122. drm->agp.base = info.aperture_base;
  123. drm->agp.size = info.aperture_size;
  124. #endif
  125. }
  126. void
  127. nouveau_agp_fini(struct nouveau_drm *drm)
  128. {
  129. #if __OS_HAS_AGP
  130. struct drm_device *dev = drm->dev;
  131. if (dev->agp && dev->agp->acquired)
  132. drm_agp_release(dev);
  133. #endif
  134. }