Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2017 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
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 }