blob: 17c6efb0ebec223fe36683adbbea4dbcb66427c7 [file] [log] [blame]
Ben Skeggs9ce523c2017-11-01 03:56:19 +10001/*
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
26#include <drm/ttm/ttm_bo_driver.h>
27
28int
29nouveau_mem_map(struct nouveau_mem *mem,
30 struct nvkm_vmm *vmm, struct nvkm_vma *vma)
31{
32 nvkm_vm_map(vma, mem->_mem);
33 return 0;
34}
35
36void
37nouveau_mem_fini(struct nouveau_mem *mem)
38{
Ben Skeggs3a314f72017-11-01 03:56:19 +100039 nvkm_vm_put(&mem->vma[1]);
40 nvkm_vm_put(&mem->vma[0]);
Ben Skeggs9ce523c2017-11-01 03:56:19 +100041}
42
43int
44nouveau_mem_host(struct ttm_mem_reg *reg, struct ttm_dma_tt *tt)
45{
46 struct nouveau_mem *mem = nouveau_mem(reg);
47 struct nouveau_cli *cli = mem->cli;
48
49 if (mem->kind && cli->device.info.chipset == 0x50)
50 mem->comp = mem->kind = 0;
51 if (mem->comp) {
52 if (cli->device.info.chipset >= 0xc0)
53 mem->kind = gf100_pte_storage_type_map[mem->kind];
54 mem->comp = 0;
55 }
56
57 mem->__mem.size = (reg->num_pages << PAGE_SHIFT) >> 12;
58 mem->__mem.memtype = (mem->comp << 7) | mem->kind;
59 if (tt->ttm.sg) mem->__mem.sg = tt->ttm.sg;
60 else mem->__mem.pages = tt->dma_address;
61 mem->_mem = &mem->__mem;
62 mem->mem.page = 12;
Ben Skeggsbd275f12017-11-01 03:56:19 +100063 mem->_mem->memory = &mem->memory;
Ben Skeggs9ce523c2017-11-01 03:56:19 +100064 return 0;
65}
66
Ben Skeggs1de33772017-11-01 03:56:19 +100067#include <subdev/fb/nv50.h>
68
69struct nvkm_vram {
70 struct nvkm_memory memory;
71 struct nvkm_ram *ram;
72 u8 page;
73 struct nvkm_mm_node *mn;
74};
75
Ben Skeggs9ce523c2017-11-01 03:56:19 +100076int
77nouveau_mem_vram(struct ttm_mem_reg *reg, bool contig, u8 page)
78{
79 struct nouveau_mem *mem = nouveau_mem(reg);
Ben Skeggs7b865662017-11-01 03:56:19 +100080 struct nouveau_cli *cli = mem->cli;
81 struct nvkm_device *device = nvxx_device(&cli->device);
Ben Skeggs9ce523c2017-11-01 03:56:19 +100082 u64 size = ALIGN(reg->num_pages << PAGE_SHIFT, 1 << page);
Ben Skeggs1de33772017-11-01 03:56:19 +100083 u8 type;
Ben Skeggs9ce523c2017-11-01 03:56:19 +100084 int ret;
85
86 mem->mem.page = page;
Ben Skeggs1de33772017-11-01 03:56:19 +100087 mem->_mem = &mem->__mem;
88
89 if (cli->device.info.chipset < 0xc0) {
90 type = nv50_fb_memtype[mem->kind];
91 } else {
92 if (!mem->comp)
93 mem->kind = gf100_pte_storage_type_map[mem->kind];
94 mem->comp = 0;
95 type = 0x01;
96 }
97
98 ret = nvkm_ram_get(device, NVKM_RAM_MM_NORMAL, type, page, size,
99 contig, false, &mem->_mem->memory);
100 if (ret)
101 return ret;
102
103 mem->_mem->size = size >> NVKM_RAM_MM_SHIFT;
104 mem->_mem->offset = nvkm_memory_addr(mem->_mem->memory);
Ben Skeggs1de33772017-11-01 03:56:19 +1000105 mem->_mem->mem = ((struct nvkm_vram *)mem->_mem->memory)->mn;
106 mem->_mem->memtype = (mem->comp << 7) | mem->kind;
Ben Skeggs9ce523c2017-11-01 03:56:19 +1000107
108 reg->start = mem->_mem->offset >> PAGE_SHIFT;
109 return ret;
110}
111
112void
113nouveau_mem_del(struct ttm_mem_reg *reg)
114{
115 struct nouveau_mem *mem = nouveau_mem(reg);
116 nouveau_mem_fini(mem);
117 kfree(reg->mm_node);
118 reg->mm_node = NULL;
119}
120
Ben Skeggsbd275f12017-11-01 03:56:19 +1000121static enum nvkm_memory_target
122nouveau_mem_memory_target(struct nvkm_memory *memory)
123{
124 struct nouveau_mem *mem = container_of(memory, typeof(*mem), memory);
125 if (mem->_mem->mem)
126 return NVKM_MEM_TARGET_VRAM;
127 return NVKM_MEM_TARGET_HOST;
128};
129
130static u8
131nouveau_mem_memory_page(struct nvkm_memory *memory)
132{
133 struct nouveau_mem *mem = container_of(memory, typeof(*mem), memory);
134 return mem->mem.page;
135};
136
137static u64
138nouveau_mem_memory_size(struct nvkm_memory *memory)
139{
140 struct nouveau_mem *mem = container_of(memory, typeof(*mem), memory);
141 return mem->_mem->size << 12;
142}
143
144static const struct nvkm_memory_func
145nouveau_mem_memory = {
146 .target = nouveau_mem_memory_target,
147 .page = nouveau_mem_memory_page,
148 .size = nouveau_mem_memory_size,
149};
150
Ben Skeggs9ce523c2017-11-01 03:56:19 +1000151int
152nouveau_mem_new(struct nouveau_cli *cli, u8 kind, u8 comp,
153 struct ttm_mem_reg *reg)
154{
155 struct nouveau_mem *mem;
156
157 if (!(mem = kzalloc(sizeof(*mem), GFP_KERNEL)))
158 return -ENOMEM;
159 mem->cli = cli;
160 mem->kind = kind;
161 mem->comp = comp;
Ben Skeggsbd275f12017-11-01 03:56:19 +1000162 nvkm_memory_ctor(&nouveau_mem_memory, &mem->memory);
Ben Skeggs9ce523c2017-11-01 03:56:19 +1000163
164 reg->mm_node = mem;
165 return 0;
166}