agp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2015 Nouveau Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "agp.h"
  23. #ifdef __NVKM_PCI_AGP_H__
  24. #include <core/option.h>
  25. struct nvkm_device_agp_quirk {
  26. u16 hostbridge_vendor;
  27. u16 hostbridge_device;
  28. u16 chip_vendor;
  29. u16 chip_device;
  30. int mode;
  31. };
  32. static const struct nvkm_device_agp_quirk
  33. nvkm_device_agp_quirks[] = {
  34. /* VIA Apollo PRO133x / GeForce FX 5600 Ultra - fdo#20341 */
  35. { PCI_VENDOR_ID_VIA, 0x0691, PCI_VENDOR_ID_NVIDIA, 0x0311, 2 },
  36. /* SiS 761 does not support AGP cards, use PCI mode */
  37. { PCI_VENDOR_ID_SI, 0x0761, PCI_ANY_ID, PCI_ANY_ID, 0 },
  38. {},
  39. };
  40. void
  41. nvkm_agp_fini(struct nvkm_pci *pci)
  42. {
  43. if (pci->agp.acquired) {
  44. agp_backend_release(pci->agp.bridge);
  45. pci->agp.acquired = false;
  46. }
  47. }
  48. /* Ensure AGP controller is in a consistent state in case we need to
  49. * execute the VBIOS DEVINIT scripts.
  50. */
  51. void
  52. nvkm_agp_preinit(struct nvkm_pci *pci)
  53. {
  54. struct nvkm_device *device = pci->subdev.device;
  55. u32 mode = nvkm_pci_rd32(pci, 0x004c);
  56. u32 save[2];
  57. /* First of all, disable fast writes, otherwise if it's already
  58. * enabled in the AGP bridge and we disable the card's AGP
  59. * controller we might be locking ourselves out of it.
  60. */
  61. if ((mode | pci->agp.mode) & PCI_AGP_COMMAND_FW) {
  62. mode = pci->agp.mode & ~PCI_AGP_COMMAND_FW;
  63. agp_enable(pci->agp.bridge, mode);
  64. }
  65. /* clear busmaster bit, and disable AGP */
  66. save[0] = nvkm_pci_rd32(pci, 0x0004);
  67. nvkm_pci_wr32(pci, 0x0004, save[0] & ~0x00000004);
  68. nvkm_pci_wr32(pci, 0x004c, 0x00000000);
  69. /* reset PGRAPH, PFIFO and PTIMER */
  70. save[1] = nvkm_mask(device, 0x000200, 0x00011100, 0x00000000);
  71. nvkm_mask(device, 0x000200, 0x00011100, save[1]);
  72. /* and restore busmaster bit (gives effect of resetting AGP) */
  73. nvkm_pci_wr32(pci, 0x0004, save[0]);
  74. }
  75. int
  76. nvkm_agp_init(struct nvkm_pci *pci)
  77. {
  78. if (!agp_backend_acquire(pci->pdev)) {
  79. nvkm_error(&pci->subdev, "failed to acquire agp\n");
  80. return -ENODEV;
  81. }
  82. agp_enable(pci->agp.bridge, pci->agp.mode);
  83. pci->agp.acquired = true;
  84. return 0;
  85. }
  86. void
  87. nvkm_agp_dtor(struct nvkm_pci *pci)
  88. {
  89. arch_phys_wc_del(pci->agp.mtrr);
  90. }
  91. void
  92. nvkm_agp_ctor(struct nvkm_pci *pci)
  93. {
  94. const struct nvkm_device_agp_quirk *quirk = nvkm_device_agp_quirks;
  95. struct nvkm_subdev *subdev = &pci->subdev;
  96. struct nvkm_device *device = subdev->device;
  97. struct agp_kern_info info;
  98. int mode = -1;
  99. #ifdef __powerpc__
  100. /* Disable AGP by default on all PowerPC machines for now -- At
  101. * least some UniNorth-2 AGP bridges are known to be broken:
  102. * DMA from the host to the card works just fine, but writeback
  103. * from the card to the host goes straight to memory
  104. * untranslated bypassing that GATT somehow, making them quite
  105. * painful to deal with...
  106. */
  107. mode = 0;
  108. #endif
  109. mode = nvkm_longopt(device->cfgopt, "NvAGP", mode);
  110. /* acquire bridge temporarily, so that we can copy its info */
  111. if (!(pci->agp.bridge = agp_backend_acquire(pci->pdev))) {
  112. nvkm_warn(subdev, "failed to acquire agp\n");
  113. return;
  114. }
  115. agp_copy_info(pci->agp.bridge, &info);
  116. agp_backend_release(pci->agp.bridge);
  117. pci->agp.mode = info.mode;
  118. pci->agp.base = info.aper_base;
  119. pci->agp.size = info.aper_size * 1024 * 1024;
  120. pci->agp.cma = info.cant_use_aperture;
  121. pci->agp.mtrr = -1;
  122. /* determine if bridge + chipset combination needs a workaround */
  123. while (quirk->hostbridge_vendor) {
  124. if (info.device->vendor == quirk->hostbridge_vendor &&
  125. info.device->device == quirk->hostbridge_device &&
  126. (quirk->chip_vendor == (u16)PCI_ANY_ID ||
  127. pci->pdev->vendor == quirk->chip_vendor) &&
  128. (quirk->chip_device == (u16)PCI_ANY_ID ||
  129. pci->pdev->device == quirk->chip_device)) {
  130. nvkm_info(subdev, "forcing default agp mode to %dX, "
  131. "use NvAGP=<mode> to override\n",
  132. quirk->mode);
  133. mode = quirk->mode;
  134. break;
  135. }
  136. quirk++;
  137. }
  138. /* apply quirk / user-specified mode */
  139. if (mode >= 1) {
  140. if (pci->agp.mode & 0x00000008)
  141. mode /= 4; /* AGPv3 */
  142. pci->agp.mode &= ~0x00000007;
  143. pci->agp.mode |= (mode & 0x7);
  144. } else
  145. if (mode == 0) {
  146. pci->agp.bridge = NULL;
  147. return;
  148. }
  149. /* fast writes appear to be broken on nv18, they make the card
  150. * lock up randomly.
  151. */
  152. if (device->chipset == 0x18)
  153. pci->agp.mode &= ~PCI_AGP_COMMAND_FW;
  154. pci->agp.mtrr = arch_phys_wc_add(pci->agp.base, pci->agp.size);
  155. }
  156. #endif