nouveau_vmm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2017 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. #include "nouveau_vmm.h"
  23. #include "nouveau_drv.h"
  24. #include "nouveau_bo.h"
  25. #include "nouveau_svm.h"
  26. #include "nouveau_mem.h"
  27. void
  28. nouveau_vma_unmap(struct nouveau_vma *vma)
  29. {
  30. if (vma->mem) {
  31. nvif_vmm_unmap(&vma->vmm->vmm, vma->addr);
  32. vma->mem = NULL;
  33. }
  34. }
  35. int
  36. nouveau_vma_map(struct nouveau_vma *vma, struct nouveau_mem *mem)
  37. {
  38. struct nvif_vma tmp = { .addr = vma->addr };
  39. int ret = nouveau_mem_map(mem, &vma->vmm->vmm, &tmp);
  40. if (ret)
  41. return ret;
  42. vma->mem = mem;
  43. return 0;
  44. }
  45. struct nouveau_vma *
  46. nouveau_vma_find(struct nouveau_bo *nvbo, struct nouveau_vmm *vmm)
  47. {
  48. struct nouveau_vma *vma;
  49. list_for_each_entry(vma, &nvbo->vma_list, head) {
  50. if (vma->vmm == vmm)
  51. return vma;
  52. }
  53. return NULL;
  54. }
  55. void
  56. nouveau_vma_del(struct nouveau_vma **pvma)
  57. {
  58. struct nouveau_vma *vma = *pvma;
  59. if (vma && --vma->refs <= 0) {
  60. if (likely(vma->addr != ~0ULL)) {
  61. struct nvif_vma tmp = { .addr = vma->addr, .size = 1 };
  62. nvif_vmm_put(&vma->vmm->vmm, &tmp);
  63. }
  64. list_del(&vma->head);
  65. kfree(*pvma);
  66. }
  67. *pvma = NULL;
  68. }
  69. int
  70. nouveau_vma_new(struct nouveau_bo *nvbo, struct nouveau_vmm *vmm,
  71. struct nouveau_vma **pvma)
  72. {
  73. struct nouveau_mem *mem = nouveau_mem(&nvbo->bo.mem);
  74. struct nouveau_vma *vma;
  75. struct nvif_vma tmp;
  76. int ret;
  77. if ((vma = *pvma = nouveau_vma_find(nvbo, vmm))) {
  78. vma->refs++;
  79. return 0;
  80. }
  81. if (!(vma = *pvma = kmalloc(sizeof(*vma), GFP_KERNEL)))
  82. return -ENOMEM;
  83. vma->vmm = vmm;
  84. vma->refs = 1;
  85. vma->addr = ~0ULL;
  86. vma->mem = NULL;
  87. vma->fence = NULL;
  88. list_add_tail(&vma->head, &nvbo->vma_list);
  89. if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM &&
  90. mem->mem.page == nvbo->page) {
  91. ret = nvif_vmm_get(&vmm->vmm, LAZY, false, mem->mem.page, 0,
  92. mem->mem.size, &tmp);
  93. if (ret)
  94. goto done;
  95. vma->addr = tmp.addr;
  96. ret = nouveau_vma_map(vma, mem);
  97. } else {
  98. ret = nvif_vmm_get(&vmm->vmm, PTES, false, mem->mem.page, 0,
  99. mem->mem.size, &tmp);
  100. vma->addr = tmp.addr;
  101. }
  102. done:
  103. if (ret)
  104. nouveau_vma_del(pvma);
  105. return ret;
  106. }
  107. void
  108. nouveau_vmm_fini(struct nouveau_vmm *vmm)
  109. {
  110. nouveau_svmm_fini(&vmm->svmm);
  111. nvif_vmm_dtor(&vmm->vmm);
  112. vmm->cli = NULL;
  113. }
  114. int
  115. nouveau_vmm_init(struct nouveau_cli *cli, s32 oclass, struct nouveau_vmm *vmm)
  116. {
  117. int ret = nvif_vmm_ctor(&cli->mmu, "drmVmm", oclass, false, PAGE_SIZE,
  118. 0, NULL, 0, &vmm->vmm);
  119. if (ret)
  120. return ret;
  121. vmm->cli = cli;
  122. return 0;
  123. }