nv04.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #define NV04_PDMA_SIZE (128 * 1024 * 1024)
  27. #define NV04_PDMA_PAGE ( 4 * 1024)
  28. /*******************************************************************************
  29. * VM map/unmap callbacks
  30. ******************************************************************************/
  31. static void
  32. nv04_vm_map_sg(struct nvkm_vma *vma, struct nvkm_memory *pgt,
  33. struct nvkm_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
  34. {
  35. pte = 0x00008 + (pte * 4);
  36. nvkm_kmap(pgt);
  37. while (cnt) {
  38. u32 page = PAGE_SIZE / NV04_PDMA_PAGE;
  39. u32 phys = (u32)*list++;
  40. while (cnt && page--) {
  41. nvkm_wo32(pgt, pte, phys | 3);
  42. phys += NV04_PDMA_PAGE;
  43. pte += 4;
  44. cnt -= 1;
  45. }
  46. }
  47. nvkm_done(pgt);
  48. }
  49. static void
  50. nv04_vm_unmap(struct nvkm_vma *vma, struct nvkm_memory *pgt, u32 pte, u32 cnt)
  51. {
  52. pte = 0x00008 + (pte * 4);
  53. nvkm_kmap(pgt);
  54. while (cnt--) {
  55. nvkm_wo32(pgt, pte, 0x00000000);
  56. pte += 4;
  57. }
  58. nvkm_done(pgt);
  59. }
  60. static void
  61. nv04_vm_flush(struct nvkm_vm *vm)
  62. {
  63. }
  64. /*******************************************************************************
  65. * MMU subdev
  66. ******************************************************************************/
  67. static int
  68. nv04_mmu_oneinit(struct nvkm_mmu *base)
  69. {
  70. struct nv04_mmu *mmu = nv04_mmu(base);
  71. struct nvkm_device *device = mmu->base.subdev.device;
  72. struct nvkm_memory *dma;
  73. int ret;
  74. ret = nvkm_vm_create(&mmu->base, 0, NV04_PDMA_SIZE, 0, 4096, NULL,
  75. &mmu->vm);
  76. if (ret)
  77. return ret;
  78. ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST,
  79. (NV04_PDMA_SIZE / NV04_PDMA_PAGE) * 4 + 8,
  80. 16, true, &dma);
  81. mmu->vm->pgt[0].mem[0] = dma;
  82. mmu->vm->pgt[0].refcount[0] = 1;
  83. if (ret)
  84. return ret;
  85. nvkm_kmap(dma);
  86. nvkm_wo32(dma, 0x00000, 0x0002103d); /* PCI, RW, PT, !LN */
  87. nvkm_wo32(dma, 0x00004, NV04_PDMA_SIZE - 1);
  88. nvkm_done(dma);
  89. return 0;
  90. }
  91. void *
  92. nv04_mmu_dtor(struct nvkm_mmu *base)
  93. {
  94. struct nv04_mmu *mmu = nv04_mmu(base);
  95. struct nvkm_device *device = mmu->base.subdev.device;
  96. if (mmu->vm) {
  97. nvkm_memory_del(&mmu->vm->pgt[0].mem[0]);
  98. nvkm_vm_ref(NULL, &mmu->vm, NULL);
  99. }
  100. if (mmu->nullp) {
  101. dma_free_coherent(device->dev, 16 * 1024,
  102. mmu->nullp, mmu->null);
  103. }
  104. return mmu;
  105. }
  106. int
  107. nv04_mmu_new_(const struct nvkm_mmu_func *func, struct nvkm_device *device,
  108. int index, struct nvkm_mmu **pmmu)
  109. {
  110. struct nv04_mmu *mmu;
  111. if (!(mmu = kzalloc(sizeof(*mmu), GFP_KERNEL)))
  112. return -ENOMEM;
  113. *pmmu = &mmu->base;
  114. nvkm_mmu_ctor(func, device, index, &mmu->base);
  115. return 0;
  116. }
  117. const struct nvkm_mmu_func
  118. nv04_mmu = {
  119. .oneinit = nv04_mmu_oneinit,
  120. .dtor = nv04_mmu_dtor,
  121. .limit = NV04_PDMA_SIZE,
  122. .dma_bits = 32,
  123. .pgt_bits = 32 - 12,
  124. .spg_shift = 12,
  125. .lpg_shift = 12,
  126. .map_sg = nv04_vm_map_sg,
  127. .unmap = nv04_vm_unmap,
  128. .flush = nv04_vm_flush,
  129. };
  130. int
  131. nv04_mmu_new(struct nvkm_device *device, int index, struct nvkm_mmu **pmmu)
  132. {
  133. return nv04_mmu_new_(&nv04_mmu, device, index, pmmu);
  134. }