]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_mem.c
drm/nouveau: allocate vram with nvkm_ram_get()
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / nouveau / nouveau_mem.c
CommitLineData
9ce523cc
BS
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_mem.h"
23#include "nouveau_drv.h"
24#include "nouveau_bo.h"
25
7b865663
BS
26#include <subdev/ltc.h>
27
9ce523cc
BS
28#include <drm/ttm/ttm_bo_driver.h>
29
30int
31nouveau_mem_map(struct nouveau_mem *mem,
32 struct nvkm_vmm *vmm, struct nvkm_vma *vma)
33{
34 nvkm_vm_map(vma, mem->_mem);
35 return 0;
36}
37
38void
39nouveau_mem_fini(struct nouveau_mem *mem)
40{
41 if (mem->vma[1].node) {
42 nvkm_vm_unmap(&mem->vma[1]);
43 nvkm_vm_put(&mem->vma[1]);
44 }
45 if (mem->vma[0].node) {
46 nvkm_vm_unmap(&mem->vma[0]);
47 nvkm_vm_put(&mem->vma[0]);
48 }
7b865663
BS
49 nvkm_memory_tags_put(&mem->memory, nvxx_device(&mem->cli->device),
50 &mem->tags);
9ce523cc
BS
51}
52
53int
54nouveau_mem_host(struct ttm_mem_reg *reg, struct ttm_dma_tt *tt)
55{
56 struct nouveau_mem *mem = nouveau_mem(reg);
57 struct nouveau_cli *cli = mem->cli;
58
59 if (mem->kind && cli->device.info.chipset == 0x50)
60 mem->comp = mem->kind = 0;
61 if (mem->comp) {
62 if (cli->device.info.chipset >= 0xc0)
63 mem->kind = gf100_pte_storage_type_map[mem->kind];
64 mem->comp = 0;
65 }
66
67 mem->__mem.size = (reg->num_pages << PAGE_SHIFT) >> 12;
68 mem->__mem.memtype = (mem->comp << 7) | mem->kind;
69 if (tt->ttm.sg) mem->__mem.sg = tt->ttm.sg;
70 else mem->__mem.pages = tt->dma_address;
71 mem->_mem = &mem->__mem;
72 mem->mem.page = 12;
bd275f1d 73 mem->_mem->memory = &mem->memory;
9ce523cc
BS
74 return 0;
75}
76
1de33775
BS
77#include <subdev/fb/nv50.h>
78
79struct nvkm_vram {
80 struct nvkm_memory memory;
81 struct nvkm_ram *ram;
82 u8 page;
83 struct nvkm_mm_node *mn;
84};
85
9ce523cc
BS
86int
87nouveau_mem_vram(struct ttm_mem_reg *reg, bool contig, u8 page)
88{
89 struct nouveau_mem *mem = nouveau_mem(reg);
7b865663
BS
90 struct nouveau_cli *cli = mem->cli;
91 struct nvkm_device *device = nvxx_device(&cli->device);
9ce523cc 92 u64 size = ALIGN(reg->num_pages << PAGE_SHIFT, 1 << page);
1de33775 93 u8 type;
9ce523cc
BS
94 int ret;
95
96 mem->mem.page = page;
1de33775
BS
97 mem->_mem = &mem->__mem;
98
99 if (cli->device.info.chipset < 0xc0) {
100 type = nv50_fb_memtype[mem->kind];
101 } else {
102 if (!mem->comp)
103 mem->kind = gf100_pte_storage_type_map[mem->kind];
104 mem->comp = 0;
105 type = 0x01;
106 }
107
108 ret = nvkm_ram_get(device, NVKM_RAM_MM_NORMAL, type, page, size,
109 contig, false, &mem->_mem->memory);
110 if (ret)
111 return ret;
112
113 mem->_mem->size = size >> NVKM_RAM_MM_SHIFT;
114 mem->_mem->offset = nvkm_memory_addr(mem->_mem->memory);
9ce523cc 115
7b865663
BS
116 if (cli->device.info.chipset < 0xc0 && mem->comp) {
117 if (page == 16) {
118 ret = nvkm_memory_tags_get(mem->_mem->memory, device,
119 size >> page, NULL,
120 &mem->tags);
121 WARN_ON(ret);
122 }
123 if (!mem->tags || !mem->tags->mn)
124 mem->comp = 0;
125 } else
126 if (cli->device.info.chipset >= 0xc0 &&
127 gf100_pte_storage_type_map[mem->kind] != mem->kind) {
128 if (page == 17) {
129 ret = nvkm_memory_tags_get(mem->_mem->memory, device,
130 size >> page,
131 nvkm_ltc_tags_clear,
132 &mem->tags);
133 WARN_ON(ret);
134 }
135 if (!mem->tags || !mem->tags->mn)
136 mem->kind = gf100_pte_storage_type_map[mem->kind];
137 }
138
7b865663
BS
139 if (mem->tags && mem->tags->mn)
140 mem->_mem->tag = mem->tags->mn;
1de33775
BS
141 mem->_mem->mem = ((struct nvkm_vram *)mem->_mem->memory)->mn;
142 mem->_mem->memtype = (mem->comp << 7) | mem->kind;
9ce523cc
BS
143
144 reg->start = mem->_mem->offset >> PAGE_SHIFT;
145 return ret;
146}
147
148void
149nouveau_mem_del(struct ttm_mem_reg *reg)
150{
151 struct nouveau_mem *mem = nouveau_mem(reg);
152 nouveau_mem_fini(mem);
153 kfree(reg->mm_node);
154 reg->mm_node = NULL;
155}
156
bd275f1d
BS
157static enum nvkm_memory_target
158nouveau_mem_memory_target(struct nvkm_memory *memory)
159{
160 struct nouveau_mem *mem = container_of(memory, typeof(*mem), memory);
161 if (mem->_mem->mem)
162 return NVKM_MEM_TARGET_VRAM;
163 return NVKM_MEM_TARGET_HOST;
164};
165
166static u8
167nouveau_mem_memory_page(struct nvkm_memory *memory)
168{
169 struct nouveau_mem *mem = container_of(memory, typeof(*mem), memory);
170 return mem->mem.page;
171};
172
173static u64
174nouveau_mem_memory_size(struct nvkm_memory *memory)
175{
176 struct nouveau_mem *mem = container_of(memory, typeof(*mem), memory);
177 return mem->_mem->size << 12;
178}
179
180static const struct nvkm_memory_func
181nouveau_mem_memory = {
182 .target = nouveau_mem_memory_target,
183 .page = nouveau_mem_memory_page,
184 .size = nouveau_mem_memory_size,
185};
186
9ce523cc
BS
187int
188nouveau_mem_new(struct nouveau_cli *cli, u8 kind, u8 comp,
189 struct ttm_mem_reg *reg)
190{
191 struct nouveau_mem *mem;
192
193 if (!(mem = kzalloc(sizeof(*mem), GFP_KERNEL)))
194 return -ENOMEM;
195 mem->cli = cli;
196 mem->kind = kind;
197 mem->comp = comp;
bd275f1d 198 nvkm_memory_ctor(&nouveau_mem_memory, &mem->memory);
9ce523cc
BS
199
200 reg->mm_node = mem;
201 return 0;
202}