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