Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 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 | |
| 26 | #include <drm/ttm/ttm_bo_driver.h> |
| 27 | |
| 28 | int |
| 29 | nouveau_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 | |
| 36 | void |
| 37 | nouveau_mem_fini(struct nouveau_mem *mem) |
| 38 | { |
Ben Skeggs | 3a314f7 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 39 | nvkm_vm_put(&mem->vma[1]); |
| 40 | nvkm_vm_put(&mem->vma[0]); |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int |
| 44 | nouveau_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 Skeggs | bd275f1 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 63 | mem->_mem->memory = &mem->memory; |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
Ben Skeggs | 1de3377 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 67 | #include <subdev/fb/nv50.h> |
| 68 | |
| 69 | struct nvkm_vram { |
| 70 | struct nvkm_memory memory; |
| 71 | struct nvkm_ram *ram; |
| 72 | u8 page; |
| 73 | struct nvkm_mm_node *mn; |
| 74 | }; |
| 75 | |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 76 | int |
| 77 | nouveau_mem_vram(struct ttm_mem_reg *reg, bool contig, u8 page) |
| 78 | { |
| 79 | struct nouveau_mem *mem = nouveau_mem(reg); |
Ben Skeggs | 7b86566 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 80 | struct nouveau_cli *cli = mem->cli; |
| 81 | struct nvkm_device *device = nvxx_device(&cli->device); |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 82 | u64 size = ALIGN(reg->num_pages << PAGE_SHIFT, 1 << page); |
Ben Skeggs | 1de3377 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 83 | u8 type; |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 84 | int ret; |
| 85 | |
| 86 | mem->mem.page = page; |
Ben Skeggs | 1de3377 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 87 | 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 Skeggs | 1de3377 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 105 | mem->_mem->mem = ((struct nvkm_vram *)mem->_mem->memory)->mn; |
| 106 | mem->_mem->memtype = (mem->comp << 7) | mem->kind; |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 107 | |
| 108 | reg->start = mem->_mem->offset >> PAGE_SHIFT; |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | nouveau_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 Skeggs | bd275f1 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 121 | static enum nvkm_memory_target |
| 122 | nouveau_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 | |
| 130 | static u8 |
| 131 | nouveau_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 | |
| 137 | static u64 |
| 138 | nouveau_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 | |
| 144 | static const struct nvkm_memory_func |
| 145 | nouveau_mem_memory = { |
| 146 | .target = nouveau_mem_memory_target, |
| 147 | .page = nouveau_mem_memory_page, |
| 148 | .size = nouveau_mem_memory_size, |
| 149 | }; |
| 150 | |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 151 | int |
| 152 | nouveau_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 Skeggs | bd275f1 | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 162 | nvkm_memory_ctor(&nouveau_mem_memory, &mem->memory); |
Ben Skeggs | 9ce523c | 2017-11-01 03:56:19 +1000 | [diff] [blame] | 163 | |
| 164 | reg->mm_node = mem; |
| 165 | return 0; |
| 166 | } |