]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/gpu/drm/nouveau/nouveau_vmm.c
drm/nouveau: allocate vmm object for every client
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nouveau_vmm.c
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
27 void
28 nouveau_vma_unmap(struct nouveau_vma *vma)
29 {
30 if (vma->mem) {
31 nvkm_vm_unmap(&vma->_vma);
32 vma->mem = NULL;
33 }
34 }
35
36 int
37 nouveau_vma_map(struct nouveau_vma *vma, struct nouveau_mem *mem)
38 {
39 int ret = nouveau_mem_map(mem, vma->vmm->vm, &vma->_vma);
40 if (ret)
41 return ret;
42 vma->mem = mem;
43 return 0;
44 }
45
46 struct nouveau_vma *
47 nouveau_vma_find(struct nouveau_bo *nvbo, struct nouveau_vmm *vmm)
48 {
49 struct nouveau_vma *vma;
50
51 list_for_each_entry(vma, &nvbo->vma_list, head) {
52 if (vma->vmm == vmm)
53 return vma;
54 }
55
56 return NULL;
57 }
58
59 void
60 nouveau_vma_del(struct nouveau_vma **pvma)
61 {
62 struct nouveau_vma *vma = *pvma;
63 if (vma && --vma->refs <= 0) {
64 if (likely(vma->addr != ~0ULL))
65 nvkm_vm_put(&vma->_vma);
66 list_del(&vma->head);
67 *pvma = NULL;
68 kfree(*pvma);
69 }
70 }
71
72 int
73 nouveau_vma_new(struct nouveau_bo *nvbo, struct nouveau_vmm *vmm,
74 struct nouveau_vma **pvma)
75 {
76 struct nouveau_mem *mem = nouveau_mem(&nvbo->bo.mem);
77 struct nouveau_vma *vma;
78 int ret;
79
80 if ((vma = *pvma = nouveau_vma_find(nvbo, vmm))) {
81 vma->refs++;
82 return 0;
83 }
84
85 if (!(vma = *pvma = kmalloc(sizeof(*vma), GFP_KERNEL)))
86 return -ENOMEM;
87 vma->vmm = vmm;
88 vma->refs = 1;
89 vma->addr = ~0ULL;
90 vma->mem = NULL;
91 list_add_tail(&vma->head, &nvbo->vma_list);
92
93 if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM &&
94 mem->mem.page == nvbo->page) {
95 ret = nvkm_vm_get(vmm->vm, mem->_mem->size << 12, mem->mem.page,
96 NV_MEM_ACCESS_RW, &vma->_vma);
97 if (ret)
98 goto done;
99
100 vma->addr = vma->_vma.offset;
101 ret = nouveau_vma_map(vma, mem);
102 } else {
103 ret = nvkm_vm_get(vmm->vm, mem->_mem->size << 12, mem->mem.page,
104 NV_MEM_ACCESS_RW, &vma->_vma);
105 vma->addr = vma->_vma.offset;
106 }
107
108 done:
109 if (ret)
110 nouveau_vma_del(pvma);
111 return ret;
112 }
113
114 void
115 nouveau_vmm_fini(struct nouveau_vmm *vmm)
116 {
117 nvif_vmm_fini(&vmm->vmm);
118 vmm->cli = NULL;
119 }
120
121 int
122 nouveau_vmm_init(struct nouveau_cli *cli, s32 oclass, struct nouveau_vmm *vmm)
123 {
124 int ret = nvif_vmm_init(&cli->mmu, oclass, PAGE_SIZE, 0, NULL, 0,
125 &vmm->vmm);
126 if (ret)
127 return ret;
128
129 vmm->cli = cli;
130 vmm->vm = nvkm_uvmm(vmm->vmm.object.priv)->vmm;
131 return 0;
132 }