nv50_vm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_vm.h"
  27. void
  28. nv50_vm_map_pgt(struct nouveau_gpuobj *pgd, u32 type, u32 pde,
  29. struct nouveau_gpuobj *pgt)
  30. {
  31. struct drm_nouveau_private *dev_priv = pgd->dev->dev_private;
  32. u32 coverage = (pgt->size >> 3) << type;
  33. u64 phys;
  34. phys = pgt->vinst;
  35. phys |= 0x01; /* present */
  36. phys |= (type == 12) ? 0x02 : 0x00; /* 4KiB pages */
  37. if (dev_priv->vram_sys_base) {
  38. phys += dev_priv->vram_sys_base;
  39. phys |= 0x30;
  40. }
  41. if (coverage <= 32 * 1024 * 1024)
  42. phys |= 0x60;
  43. else if (coverage <= 64 * 1024 * 1024)
  44. phys |= 0x40;
  45. else if (coverage < 128 * 1024 * 1024)
  46. phys |= 0x20;
  47. nv_wo32(pgd, (pde * 8) + 0, lower_32_bits(phys));
  48. nv_wo32(pgd, (pde * 8) + 4, upper_32_bits(phys));
  49. }
  50. void
  51. nv50_vm_unmap_pgt(struct nouveau_gpuobj *pgd, u32 pde)
  52. {
  53. nv_wo32(pgd, (pde * 8) + 0, 0x00000000);
  54. nv_wo32(pgd, (pde * 8) + 4, 0xdeadcafe);
  55. }
  56. static inline u64
  57. nv50_vm_addr(struct nouveau_vma *vma, struct nouveau_gpuobj *pgt,
  58. u64 phys, u32 memtype, u32 target)
  59. {
  60. struct drm_nouveau_private *dev_priv = pgt->dev->dev_private;
  61. phys |= 1; /* present */
  62. phys |= (u64)memtype << 40;
  63. /* IGPs don't have real VRAM, re-target to stolen system memory */
  64. if (target == 0 && dev_priv->vram_sys_base) {
  65. phys += dev_priv->vram_sys_base;
  66. target = 3;
  67. }
  68. phys |= target << 4;
  69. if (vma->access & NV_MEM_ACCESS_SYS)
  70. phys |= (1 << 6);
  71. if (!(vma->access & NV_MEM_ACCESS_WO))
  72. phys |= (1 << 3);
  73. return phys;
  74. }
  75. void
  76. nv50_vm_map(struct nouveau_vma *vma, struct nouveau_gpuobj *pgt,
  77. struct nouveau_vram *mem, u32 pte, u32 cnt, u64 phys)
  78. {
  79. u32 block;
  80. int i;
  81. phys = nv50_vm_addr(vma, pgt, phys, mem->memtype, 0);
  82. pte <<= 3;
  83. cnt <<= 3;
  84. while (cnt) {
  85. u32 offset_h = upper_32_bits(phys);
  86. u32 offset_l = lower_32_bits(phys);
  87. for (i = 7; i >= 0; i--) {
  88. block = 1 << (i + 3);
  89. if (cnt >= block && !(pte & (block - 1)))
  90. break;
  91. }
  92. offset_l |= (i << 7);
  93. phys += block << (vma->node->type - 3);
  94. cnt -= block;
  95. while (block) {
  96. nv_wo32(pgt, pte + 0, offset_l);
  97. nv_wo32(pgt, pte + 4, offset_h);
  98. pte += 8;
  99. block -= 8;
  100. }
  101. }
  102. }
  103. void
  104. nv50_vm_map_sg(struct nouveau_vma *vma, struct nouveau_gpuobj *pgt,
  105. u32 pte, dma_addr_t *list, u32 cnt)
  106. {
  107. pte <<= 3;
  108. while (cnt--) {
  109. u64 phys = nv50_vm_addr(vma, pgt, (u64)*list++, 0, 2);
  110. nv_wo32(pgt, pte + 0, lower_32_bits(phys));
  111. nv_wo32(pgt, pte + 4, upper_32_bits(phys));
  112. pte += 8;
  113. }
  114. }
  115. void
  116. nv50_vm_unmap(struct nouveau_gpuobj *pgt, u32 pte, u32 cnt)
  117. {
  118. pte <<= 3;
  119. while (cnt--) {
  120. nv_wo32(pgt, pte + 0, 0x00000000);
  121. nv_wo32(pgt, pte + 4, 0x00000000);
  122. pte += 8;
  123. }
  124. }
  125. void
  126. nv50_vm_flush(struct nouveau_vm *vm)
  127. {
  128. struct drm_nouveau_private *dev_priv = vm->dev->dev_private;
  129. struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
  130. struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
  131. struct nouveau_pgraph_engine *pgraph = &dev_priv->engine.graph;
  132. struct nouveau_crypt_engine *pcrypt = &dev_priv->engine.crypt;
  133. pinstmem->flush(vm->dev);
  134. /* BAR */
  135. if (vm != dev_priv->chan_vm) {
  136. nv50_vm_flush_engine(vm->dev, 6);
  137. return;
  138. }
  139. pfifo->tlb_flush(vm->dev);
  140. if (atomic_read(&vm->pgraph_refs))
  141. pgraph->tlb_flush(vm->dev);
  142. if (atomic_read(&vm->pcrypt_refs))
  143. pcrypt->tlb_flush(vm->dev);
  144. }
  145. void
  146. nv50_vm_flush_engine(struct drm_device *dev, int engine)
  147. {
  148. nv_wr32(dev, 0x100c80, (engine << 16) | 1);
  149. if (!nv_wait(dev, 0x100c80, 0x00000001, 0x00000000))
  150. NV_ERROR(dev, "vm flush timeout: engine %d\n", engine);
  151. }