base.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2015 Red Hat Inc.
  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. * Authors: Ben Skeggs <bskeggs@redhat.com>
  23. */
  24. #include "priv.h"
  25. #include "agp.h"
  26. #include <core/option.h>
  27. #include <core/pci.h>
  28. #include <subdev/mc.h>
  29. u32
  30. nvkm_pci_rd32(struct nvkm_pci *pci, u16 addr)
  31. {
  32. return pci->func->rd32(pci, addr);
  33. }
  34. void
  35. nvkm_pci_wr08(struct nvkm_pci *pci, u16 addr, u8 data)
  36. {
  37. pci->func->wr08(pci, addr, data);
  38. }
  39. void
  40. nvkm_pci_wr32(struct nvkm_pci *pci, u16 addr, u32 data)
  41. {
  42. pci->func->wr32(pci, addr, data);
  43. }
  44. u32
  45. nvkm_pci_mask(struct nvkm_pci *pci, u16 addr, u32 mask, u32 value)
  46. {
  47. u32 data = pci->func->rd32(pci, addr);
  48. pci->func->wr32(pci, addr, (data & ~mask) | value);
  49. return data;
  50. }
  51. void
  52. nvkm_pci_rom_shadow(struct nvkm_pci *pci, bool shadow)
  53. {
  54. u32 data = nvkm_pci_rd32(pci, 0x0050);
  55. if (shadow)
  56. data |= 0x00000001;
  57. else
  58. data &= ~0x00000001;
  59. nvkm_pci_wr32(pci, 0x0050, data);
  60. }
  61. static irqreturn_t
  62. nvkm_pci_intr(int irq, void *arg)
  63. {
  64. struct nvkm_pci *pci = arg;
  65. struct nvkm_device *device = pci->subdev.device;
  66. bool handled = false;
  67. nvkm_mc_intr_unarm(device);
  68. if (pci->msi)
  69. pci->func->msi_rearm(pci);
  70. nvkm_mc_intr(device, &handled);
  71. nvkm_mc_intr_rearm(device);
  72. return handled ? IRQ_HANDLED : IRQ_NONE;
  73. }
  74. static int
  75. nvkm_pci_fini(struct nvkm_subdev *subdev, bool suspend)
  76. {
  77. struct nvkm_pci *pci = nvkm_pci(subdev);
  78. if (pci->irq >= 0) {
  79. free_irq(pci->irq, pci);
  80. pci->irq = -1;
  81. };
  82. if (pci->agp.bridge)
  83. nvkm_agp_fini(pci);
  84. return 0;
  85. }
  86. static int
  87. nvkm_pci_preinit(struct nvkm_subdev *subdev)
  88. {
  89. struct nvkm_pci *pci = nvkm_pci(subdev);
  90. if (pci->agp.bridge)
  91. nvkm_agp_preinit(pci);
  92. return 0;
  93. }
  94. static int
  95. nvkm_pci_oneinit(struct nvkm_subdev *subdev)
  96. {
  97. struct nvkm_pci *pci = nvkm_pci(subdev);
  98. if (pci_is_pcie(pci->pdev))
  99. return nvkm_pcie_oneinit(pci);
  100. return 0;
  101. }
  102. static int
  103. nvkm_pci_init(struct nvkm_subdev *subdev)
  104. {
  105. struct nvkm_pci *pci = nvkm_pci(subdev);
  106. struct pci_dev *pdev = pci->pdev;
  107. int ret;
  108. if (pci->agp.bridge) {
  109. ret = nvkm_agp_init(pci);
  110. if (ret)
  111. return ret;
  112. } else if (pci_is_pcie(pci->pdev)) {
  113. nvkm_pcie_init(pci);
  114. }
  115. if (pci->func->init)
  116. pci->func->init(pci);
  117. ret = request_irq(pdev->irq, nvkm_pci_intr, IRQF_SHARED, "nvkm", pci);
  118. if (ret)
  119. return ret;
  120. pci->irq = pdev->irq;
  121. return ret;
  122. }
  123. static void *
  124. nvkm_pci_dtor(struct nvkm_subdev *subdev)
  125. {
  126. struct nvkm_pci *pci = nvkm_pci(subdev);
  127. nvkm_agp_dtor(pci);
  128. if (pci->msi)
  129. pci_disable_msi(pci->pdev);
  130. return nvkm_pci(subdev);
  131. }
  132. static const struct nvkm_subdev_func
  133. nvkm_pci_func = {
  134. .dtor = nvkm_pci_dtor,
  135. .oneinit = nvkm_pci_oneinit,
  136. .preinit = nvkm_pci_preinit,
  137. .init = nvkm_pci_init,
  138. .fini = nvkm_pci_fini,
  139. };
  140. int
  141. nvkm_pci_new_(const struct nvkm_pci_func *func, struct nvkm_device *device,
  142. int index, struct nvkm_pci **ppci)
  143. {
  144. struct nvkm_pci *pci;
  145. if (!(pci = *ppci = kzalloc(sizeof(**ppci), GFP_KERNEL)))
  146. return -ENOMEM;
  147. nvkm_subdev_ctor(&nvkm_pci_func, device, index, &pci->subdev);
  148. pci->func = func;
  149. pci->pdev = device->func->pci(device)->pdev;
  150. pci->irq = -1;
  151. pci->pcie.speed = -1;
  152. pci->pcie.width = -1;
  153. if (device->type == NVKM_DEVICE_AGP)
  154. nvkm_agp_ctor(pci);
  155. switch (pci->pdev->device & 0x0ff0) {
  156. case 0x00f0:
  157. case 0x02e0:
  158. /* BR02? NFI how these would be handled yet exactly */
  159. break;
  160. default:
  161. switch (device->chipset) {
  162. case 0xaa:
  163. /* reported broken, nv also disable it */
  164. break;
  165. default:
  166. pci->msi = true;
  167. break;
  168. }
  169. }
  170. pci->msi = nvkm_boolopt(device->cfgopt, "NvMSI", pci->msi);
  171. if (pci->msi && func->msi_rearm) {
  172. pci->msi = pci_enable_msi(pci->pdev) == 0;
  173. if (pci->msi)
  174. nvkm_debug(&pci->subdev, "MSI enabled\n");
  175. } else {
  176. pci->msi = false;
  177. }
  178. return 0;
  179. }