nv50.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2010 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 "priv.h"
  25. #include <core/gpuobj.h>
  26. #include <subdev/fb.h>
  27. #include <subdev/timer.h>
  28. #include <engine/gr.h>
  29. static void
  30. nv50_vm_map_pgt(struct nvkm_gpuobj *pgd, u32 pde, struct nvkm_memory *pgt[2])
  31. {
  32. u64 phys = 0xdeadcafe00000000ULL;
  33. u32 coverage = 0;
  34. if (pgt[0]) {
  35. /* present, 4KiB pages */
  36. phys = 0x00000003 | nvkm_memory_addr(pgt[0]);
  37. coverage = (nvkm_memory_size(pgt[0]) >> 3) << 12;
  38. } else
  39. if (pgt[1]) {
  40. /* present, 64KiB pages */
  41. phys = 0x00000001 | nvkm_memory_addr(pgt[1]);
  42. coverage = (nvkm_memory_size(pgt[1]) >> 3) << 16;
  43. }
  44. if (phys & 1) {
  45. if (coverage <= 32 * 1024 * 1024)
  46. phys |= 0x60;
  47. else if (coverage <= 64 * 1024 * 1024)
  48. phys |= 0x40;
  49. else if (coverage <= 128 * 1024 * 1024)
  50. phys |= 0x20;
  51. }
  52. nvkm_kmap(pgd);
  53. nvkm_wo32(pgd, (pde * 8) + 0, lower_32_bits(phys));
  54. nvkm_wo32(pgd, (pde * 8) + 4, upper_32_bits(phys));
  55. nvkm_done(pgd);
  56. }
  57. static inline u64
  58. vm_addr(struct nvkm_vma *vma, u64 phys, u32 memtype, u32 target)
  59. {
  60. phys |= 1; /* present */
  61. phys |= (u64)memtype << 40;
  62. phys |= target << 4;
  63. if (vma->access & NV_MEM_ACCESS_SYS)
  64. phys |= (1 << 6);
  65. if (!(vma->access & NV_MEM_ACCESS_WO))
  66. phys |= (1 << 3);
  67. return phys;
  68. }
  69. static void
  70. nv50_vm_map(struct nvkm_vma *vma, struct nvkm_memory *pgt,
  71. struct nvkm_mem *mem, u32 pte, u32 cnt, u64 phys, u64 delta)
  72. {
  73. struct nvkm_ram *ram = vma->vm->mmu->subdev.device->fb->ram;
  74. u32 comp = (mem->memtype & 0x180) >> 7;
  75. u32 block, target;
  76. int i;
  77. /* IGPs don't have real VRAM, re-target to stolen system memory */
  78. target = 0;
  79. if (ram->stolen) {
  80. phys += ram->stolen;
  81. target = 3;
  82. }
  83. phys = vm_addr(vma, phys, mem->memtype, target);
  84. pte <<= 3;
  85. cnt <<= 3;
  86. nvkm_kmap(pgt);
  87. while (cnt) {
  88. u32 offset_h = upper_32_bits(phys);
  89. u32 offset_l = lower_32_bits(phys);
  90. for (i = 7; i >= 0; i--) {
  91. block = 1 << (i + 3);
  92. if (cnt >= block && !(pte & (block - 1)))
  93. break;
  94. }
  95. offset_l |= (i << 7);
  96. phys += block << (vma->node->type - 3);
  97. cnt -= block;
  98. if (comp) {
  99. u32 tag = mem->tag->offset + ((delta >> 16) * comp);
  100. offset_h |= (tag << 17);
  101. delta += block << (vma->node->type - 3);
  102. }
  103. while (block) {
  104. nvkm_wo32(pgt, pte + 0, offset_l);
  105. nvkm_wo32(pgt, pte + 4, offset_h);
  106. pte += 8;
  107. block -= 8;
  108. }
  109. }
  110. nvkm_done(pgt);
  111. }
  112. static void
  113. nv50_vm_map_sg(struct nvkm_vma *vma, struct nvkm_memory *pgt,
  114. struct nvkm_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
  115. {
  116. u32 target = (vma->access & NV_MEM_ACCESS_NOSNOOP) ? 3 : 2;
  117. pte <<= 3;
  118. nvkm_kmap(pgt);
  119. while (cnt--) {
  120. u64 phys = vm_addr(vma, (u64)*list++, mem->memtype, target);
  121. nvkm_wo32(pgt, pte + 0, lower_32_bits(phys));
  122. nvkm_wo32(pgt, pte + 4, upper_32_bits(phys));
  123. pte += 8;
  124. }
  125. nvkm_done(pgt);
  126. }
  127. static void
  128. nv50_vm_unmap(struct nvkm_vma *vma, struct nvkm_memory *pgt, u32 pte, u32 cnt)
  129. {
  130. pte <<= 3;
  131. nvkm_kmap(pgt);
  132. while (cnt--) {
  133. nvkm_wo32(pgt, pte + 0, 0x00000000);
  134. nvkm_wo32(pgt, pte + 4, 0x00000000);
  135. pte += 8;
  136. }
  137. nvkm_done(pgt);
  138. }
  139. static void
  140. nv50_vm_flush(struct nvkm_vm *vm)
  141. {
  142. struct nvkm_mmu *mmu = vm->mmu;
  143. struct nvkm_subdev *subdev = &mmu->subdev;
  144. struct nvkm_device *device = subdev->device;
  145. int i, vme;
  146. mutex_lock(&subdev->mutex);
  147. for (i = 0; i < NVKM_SUBDEV_NR; i++) {
  148. if (!atomic_read(&vm->engref[i]))
  149. continue;
  150. /* unfortunate hw bug workaround... */
  151. if (i == NVKM_ENGINE_GR && device->gr) {
  152. int ret = nvkm_gr_tlb_flush(device->gr);
  153. if (ret != -ENODEV)
  154. continue;
  155. }
  156. switch (i) {
  157. case NVKM_ENGINE_GR : vme = 0x00; break;
  158. case NVKM_ENGINE_VP :
  159. case NVKM_ENGINE_MSPDEC: vme = 0x01; break;
  160. case NVKM_SUBDEV_BAR : vme = 0x06; break;
  161. case NVKM_ENGINE_MSPPP :
  162. case NVKM_ENGINE_MPEG : vme = 0x08; break;
  163. case NVKM_ENGINE_BSP :
  164. case NVKM_ENGINE_MSVLD : vme = 0x09; break;
  165. case NVKM_ENGINE_CIPHER:
  166. case NVKM_ENGINE_SEC : vme = 0x0a; break;
  167. case NVKM_ENGINE_CE0 : vme = 0x0d; break;
  168. default:
  169. continue;
  170. }
  171. nvkm_wr32(device, 0x100c80, (vme << 16) | 1);
  172. if (nvkm_msec(device, 2000,
  173. if (!(nvkm_rd32(device, 0x100c80) & 0x00000001))
  174. break;
  175. ) < 0)
  176. nvkm_error(subdev, "vm flush timeout: engine %d\n", vme);
  177. }
  178. mutex_unlock(&subdev->mutex);
  179. }
  180. static int
  181. nv50_vm_create(struct nvkm_mmu *mmu, u64 offset, u64 length, u64 mm_offset,
  182. struct lock_class_key *key, struct nvkm_vm **pvm)
  183. {
  184. u32 block = (1 << (mmu->func->pgt_bits + 12));
  185. if (block > length)
  186. block = length;
  187. return nvkm_vm_create(mmu, offset, length, mm_offset, block, key, pvm);
  188. }
  189. static const struct nvkm_mmu_func
  190. nv50_mmu = {
  191. .limit = (1ULL << 40),
  192. .dma_bits = 40,
  193. .pgt_bits = 29 - 12,
  194. .spg_shift = 12,
  195. .lpg_shift = 16,
  196. .create = nv50_vm_create,
  197. .map_pgt = nv50_vm_map_pgt,
  198. .map = nv50_vm_map,
  199. .map_sg = nv50_vm_map_sg,
  200. .unmap = nv50_vm_unmap,
  201. .flush = nv50_vm_flush,
  202. };
  203. int
  204. nv50_mmu_new(struct nvkm_device *device, int index, struct nvkm_mmu **pmmu)
  205. {
  206. return nvkm_mmu_new_(&nv50_mmu, device, index, pmmu);
  207. }