nouveau_vmm.c 3.3 KB

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