vmm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <nvif/vmm.h>
  23. #include <nvif/mem.h>
  24. #include <nvif/if000c.h>
  25. int
  26. nvif_vmm_unmap(struct nvif_vmm *vmm, u64 addr)
  27. {
  28. return nvif_object_mthd(&vmm->object, NVIF_VMM_V0_UNMAP,
  29. &(struct nvif_vmm_unmap_v0) { .addr = addr },
  30. sizeof(struct nvif_vmm_unmap_v0));
  31. }
  32. int
  33. nvif_vmm_map(struct nvif_vmm *vmm, u64 addr, u64 size, void *argv, u32 argc,
  34. struct nvif_mem *mem, u64 offset)
  35. {
  36. struct nvif_vmm_map_v0 *args;
  37. u8 stack[48];
  38. int ret;
  39. if (sizeof(*args) + argc > sizeof(stack)) {
  40. if (!(args = kmalloc(sizeof(*args) + argc, GFP_KERNEL)))
  41. return -ENOMEM;
  42. } else {
  43. args = (void *)stack;
  44. }
  45. args->version = 0;
  46. args->addr = addr;
  47. args->size = size;
  48. args->memory = nvif_handle(&mem->object);
  49. args->offset = offset;
  50. memcpy(args->data, argv, argc);
  51. ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_MAP,
  52. args, sizeof(*args) + argc);
  53. if (args != (void *)stack)
  54. kfree(args);
  55. return ret;
  56. }
  57. void
  58. nvif_vmm_put(struct nvif_vmm *vmm, struct nvif_vma *vma)
  59. {
  60. if (vma->size) {
  61. WARN_ON(nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PUT,
  62. &(struct nvif_vmm_put_v0) {
  63. .addr = vma->addr,
  64. }, sizeof(struct nvif_vmm_put_v0)));
  65. vma->size = 0;
  66. }
  67. }
  68. int
  69. nvif_vmm_get(struct nvif_vmm *vmm, enum nvif_vmm_get type, bool sparse,
  70. u8 page, u8 align, u64 size, struct nvif_vma *vma)
  71. {
  72. struct nvif_vmm_get_v0 args;
  73. int ret;
  74. args.version = vma->size = 0;
  75. args.sparse = sparse;
  76. args.page = page;
  77. args.align = align;
  78. args.size = size;
  79. switch (type) {
  80. case ADDR: args.type = NVIF_VMM_GET_V0_ADDR; break;
  81. case PTES: args.type = NVIF_VMM_GET_V0_PTES; break;
  82. case LAZY: args.type = NVIF_VMM_GET_V0_LAZY; break;
  83. default:
  84. WARN_ON(1);
  85. return -EINVAL;
  86. }
  87. ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_GET,
  88. &args, sizeof(args));
  89. if (ret == 0) {
  90. vma->addr = args.addr;
  91. vma->size = args.size;
  92. }
  93. return ret;
  94. }
  95. void
  96. nvif_vmm_fini(struct nvif_vmm *vmm)
  97. {
  98. kfree(vmm->page);
  99. nvif_object_fini(&vmm->object);
  100. }
  101. int
  102. nvif_vmm_init(struct nvif_mmu *mmu, s32 oclass, u64 addr, u64 size,
  103. void *argv, u32 argc, struct nvif_vmm *vmm)
  104. {
  105. struct nvif_vmm_v0 *args;
  106. u32 argn = sizeof(*args) + argc;
  107. int ret = -ENOSYS, i;
  108. vmm->object.client = NULL;
  109. vmm->page = NULL;
  110. if (!(args = kmalloc(argn, GFP_KERNEL)))
  111. return -ENOMEM;
  112. args->version = 0;
  113. args->addr = addr;
  114. args->size = size;
  115. memcpy(args->data, argv, argc);
  116. ret = nvif_object_init(&mmu->object, 0, oclass, args, argn,
  117. &vmm->object);
  118. if (ret)
  119. goto done;
  120. vmm->start = args->addr;
  121. vmm->limit = args->size;
  122. vmm->page_nr = args->page_nr;
  123. vmm->page = kmalloc_array(vmm->page_nr, sizeof(*vmm->page),
  124. GFP_KERNEL);
  125. if (!vmm->page) {
  126. ret = -ENOMEM;
  127. goto done;
  128. }
  129. for (i = 0; i < vmm->page_nr; i++) {
  130. struct nvif_vmm_page_v0 args = { .index = i };
  131. ret = nvif_object_mthd(&vmm->object, NVIF_VMM_V0_PAGE,
  132. &args, sizeof(args));
  133. if (ret)
  134. break;
  135. vmm->page[i].shift = args.shift;
  136. vmm->page[i].sparse = args.sparse;
  137. vmm->page[i].vram = args.vram;
  138. vmm->page[i].host = args.host;
  139. vmm->page[i].comp = args.comp;
  140. }
  141. done:
  142. if (ret)
  143. nvif_vmm_fini(vmm);
  144. kfree(args);
  145. return ret;
  146. }