nv41.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2012 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
  23. */
  24. #include "nv04.h"
  25. #include <core/gpuobj.h>
  26. #include <core/option.h>
  27. #include <subdev/timer.h>
  28. #define NV41_GART_SIZE (512 * 1024 * 1024)
  29. #define NV41_GART_PAGE ( 4 * 1024)
  30. /*******************************************************************************
  31. * VM map/unmap callbacks
  32. ******************************************************************************/
  33. static void
  34. nv41_vm_map_sg(struct nvkm_vma *vma, struct nvkm_memory *pgt,
  35. struct nvkm_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
  36. {
  37. pte = pte * 4;
  38. nvkm_kmap(pgt);
  39. while (cnt) {
  40. u32 page = PAGE_SIZE / NV41_GART_PAGE;
  41. u64 phys = (u64)*list++;
  42. while (cnt && page--) {
  43. nvkm_wo32(pgt, pte, (phys >> 7) | 1);
  44. phys += NV41_GART_PAGE;
  45. pte += 4;
  46. cnt -= 1;
  47. }
  48. }
  49. nvkm_done(pgt);
  50. }
  51. static void
  52. nv41_vm_unmap(struct nvkm_vma *vma, struct nvkm_memory *pgt, u32 pte, u32 cnt)
  53. {
  54. pte = pte * 4;
  55. nvkm_kmap(pgt);
  56. while (cnt--) {
  57. nvkm_wo32(pgt, pte, 0x00000000);
  58. pte += 4;
  59. }
  60. nvkm_done(pgt);
  61. }
  62. static void
  63. nv41_vm_flush(struct nvkm_vm *vm)
  64. {
  65. struct nv04_mmu *mmu = nv04_mmu(vm->mmu);
  66. struct nvkm_device *device = mmu->base.subdev.device;
  67. mutex_lock(&mmu->base.subdev.mutex);
  68. nvkm_wr32(device, 0x100810, 0x00000022);
  69. nvkm_msec(device, 2000,
  70. if (nvkm_rd32(device, 0x100810) & 0x00000020)
  71. break;
  72. );
  73. nvkm_wr32(device, 0x100810, 0x00000000);
  74. mutex_unlock(&mmu->base.subdev.mutex);
  75. }
  76. /*******************************************************************************
  77. * MMU subdev
  78. ******************************************************************************/
  79. static int
  80. nv41_mmu_oneinit(struct nvkm_mmu *base)
  81. {
  82. struct nv04_mmu *mmu = nv04_mmu(base);
  83. struct nvkm_device *device = mmu->base.subdev.device;
  84. int ret;
  85. ret = nvkm_vm_create(&mmu->base, 0, NV41_GART_SIZE, 0, 4096, NULL,
  86. &mmu->vm);
  87. if (ret)
  88. return ret;
  89. ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
  90. (NV41_GART_SIZE / NV41_GART_PAGE) * 4, 16, true,
  91. &mmu->vm->pgt[0].mem[0]);
  92. mmu->vm->pgt[0].refcount[0] = 1;
  93. return ret;
  94. }
  95. static void
  96. nv41_mmu_init(struct nvkm_mmu *base)
  97. {
  98. struct nv04_mmu *mmu = nv04_mmu(base);
  99. struct nvkm_device *device = mmu->base.subdev.device;
  100. struct nvkm_memory *dma = mmu->vm->pgt[0].mem[0];
  101. nvkm_wr32(device, 0x100800, 0x00000002 | nvkm_memory_addr(dma));
  102. nvkm_mask(device, 0x10008c, 0x00000100, 0x00000100);
  103. nvkm_wr32(device, 0x100820, 0x00000000);
  104. }
  105. static const struct nvkm_mmu_func
  106. nv41_mmu = {
  107. .dtor = nv04_mmu_dtor,
  108. .oneinit = nv41_mmu_oneinit,
  109. .init = nv41_mmu_init,
  110. .limit = NV41_GART_SIZE,
  111. .dma_bits = 39,
  112. .pgt_bits = 32 - 12,
  113. .spg_shift = 12,
  114. .lpg_shift = 12,
  115. .map_sg = nv41_vm_map_sg,
  116. .unmap = nv41_vm_unmap,
  117. .flush = nv41_vm_flush,
  118. };
  119. int
  120. nv41_mmu_new(struct nvkm_device *device, int index, struct nvkm_mmu **pmmu)
  121. {
  122. if (device->type == NVKM_DEVICE_AGP ||
  123. !nvkm_boolopt(device->cfgopt, "NvPCIE", true))
  124. return nv04_mmu_new(device, index, pmmu);
  125. return nv04_mmu_new_(&nv41_mmu, device, index, pmmu);
  126. }