0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include "nouveau_vmm.h"
0023 #include "nouveau_drv.h"
0024 #include "nouveau_bo.h"
0025 #include "nouveau_svm.h"
0026 #include "nouveau_mem.h"
0027
0028 void
0029 nouveau_vma_unmap(struct nouveau_vma *vma)
0030 {
0031 if (vma->mem) {
0032 nvif_vmm_unmap(&vma->vmm->vmm, vma->addr);
0033 vma->mem = NULL;
0034 }
0035 }
0036
0037 int
0038 nouveau_vma_map(struct nouveau_vma *vma, struct nouveau_mem *mem)
0039 {
0040 struct nvif_vma tmp = { .addr = vma->addr };
0041 int ret = nouveau_mem_map(mem, &vma->vmm->vmm, &tmp);
0042 if (ret)
0043 return ret;
0044 vma->mem = mem;
0045 return 0;
0046 }
0047
0048 struct nouveau_vma *
0049 nouveau_vma_find(struct nouveau_bo *nvbo, struct nouveau_vmm *vmm)
0050 {
0051 struct nouveau_vma *vma;
0052
0053 list_for_each_entry(vma, &nvbo->vma_list, head) {
0054 if (vma->vmm == vmm)
0055 return vma;
0056 }
0057
0058 return NULL;
0059 }
0060
0061 void
0062 nouveau_vma_del(struct nouveau_vma **pvma)
0063 {
0064 struct nouveau_vma *vma = *pvma;
0065 if (vma && --vma->refs <= 0) {
0066 if (likely(vma->addr != ~0ULL)) {
0067 struct nvif_vma tmp = { .addr = vma->addr, .size = 1 };
0068 nvif_vmm_put(&vma->vmm->vmm, &tmp);
0069 }
0070 list_del(&vma->head);
0071 kfree(*pvma);
0072 }
0073 *pvma = NULL;
0074 }
0075
0076 int
0077 nouveau_vma_new(struct nouveau_bo *nvbo, struct nouveau_vmm *vmm,
0078 struct nouveau_vma **pvma)
0079 {
0080 struct nouveau_mem *mem = nouveau_mem(nvbo->bo.resource);
0081 struct nouveau_vma *vma;
0082 struct nvif_vma tmp;
0083 int ret;
0084
0085 if ((vma = *pvma = nouveau_vma_find(nvbo, vmm))) {
0086 vma->refs++;
0087 return 0;
0088 }
0089
0090 if (!(vma = *pvma = kmalloc(sizeof(*vma), GFP_KERNEL)))
0091 return -ENOMEM;
0092 vma->vmm = vmm;
0093 vma->refs = 1;
0094 vma->addr = ~0ULL;
0095 vma->mem = NULL;
0096 vma->fence = NULL;
0097 list_add_tail(&vma->head, &nvbo->vma_list);
0098
0099 if (nvbo->bo.resource->mem_type != TTM_PL_SYSTEM &&
0100 mem->mem.page == nvbo->page) {
0101 ret = nvif_vmm_get(&vmm->vmm, LAZY, false, mem->mem.page, 0,
0102 mem->mem.size, &tmp);
0103 if (ret)
0104 goto done;
0105
0106 vma->addr = tmp.addr;
0107 ret = nouveau_vma_map(vma, mem);
0108 } else {
0109 ret = nvif_vmm_get(&vmm->vmm, PTES, false, mem->mem.page, 0,
0110 mem->mem.size, &tmp);
0111 vma->addr = tmp.addr;
0112 }
0113
0114 done:
0115 if (ret)
0116 nouveau_vma_del(pvma);
0117 return ret;
0118 }
0119
0120 void
0121 nouveau_vmm_fini(struct nouveau_vmm *vmm)
0122 {
0123 nouveau_svmm_fini(&vmm->svmm);
0124 nvif_vmm_dtor(&vmm->vmm);
0125 vmm->cli = NULL;
0126 }
0127
0128 int
0129 nouveau_vmm_init(struct nouveau_cli *cli, s32 oclass, struct nouveau_vmm *vmm)
0130 {
0131 int ret = nvif_vmm_ctor(&cli->mmu, "drmVmm", oclass, false, PAGE_SIZE,
0132 0, NULL, 0, &vmm->vmm);
0133 if (ret)
0134 return ret;
0135
0136 vmm->cli = cli;
0137 return 0;
0138 }