nouveau_nvkm_subdev_instmem_nv50.c revision 1.1
1/* $NetBSD: nouveau_nvkm_subdev_instmem_nv50.c,v 1.1 2018/08/27 01:34:56 riastradh Exp $ */ 2 3/* 4 * Copyright 2012 Red Hat Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Ben Skeggs 25 */ 26#include <sys/cdefs.h> 27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_instmem_nv50.c,v 1.1 2018/08/27 01:34:56 riastradh Exp $"); 28 29#define nv50_instmem(p) container_of((p), struct nv50_instmem, base) 30#include "priv.h" 31 32#include <core/memory.h> 33#include <subdev/bar.h> 34#include <subdev/fb.h> 35#include <subdev/mmu.h> 36 37struct nv50_instmem { 38 struct nvkm_instmem base; 39 unsigned long lock_flags; 40 spinlock_t lock; 41 u64 addr; 42}; 43 44/****************************************************************************** 45 * instmem object implementation 46 *****************************************************************************/ 47#define nv50_instobj(p) container_of((p), struct nv50_instobj, memory) 48 49struct nv50_instobj { 50 struct nvkm_memory memory; 51 struct nv50_instmem *imem; 52 struct nvkm_mem *mem; 53 struct nvkm_vma bar; 54 void *map; 55}; 56 57static enum nvkm_memory_target 58nv50_instobj_target(struct nvkm_memory *memory) 59{ 60 return NVKM_MEM_TARGET_VRAM; 61} 62 63static u64 64nv50_instobj_addr(struct nvkm_memory *memory) 65{ 66 return nv50_instobj(memory)->mem->offset; 67} 68 69static u64 70nv50_instobj_size(struct nvkm_memory *memory) 71{ 72 return (u64)nv50_instobj(memory)->mem->size << NVKM_RAM_MM_SHIFT; 73} 74 75static void 76nv50_instobj_boot(struct nvkm_memory *memory, struct nvkm_vm *vm) 77{ 78 struct nv50_instobj *iobj = nv50_instobj(memory); 79 struct nvkm_subdev *subdev = &iobj->imem->base.subdev; 80 struct nvkm_device *device = subdev->device; 81 u64 size = nvkm_memory_size(memory); 82 void __iomem *map; 83 int ret; 84 85 iobj->map = ERR_PTR(-ENOMEM); 86 87 ret = nvkm_vm_get(vm, size, 12, NV_MEM_ACCESS_RW, &iobj->bar); 88 if (ret == 0) { 89 map = ioremap(device->func->resource_addr(device, 3) + 90 (u32)iobj->bar.offset, size); 91 if (map) { 92 nvkm_memory_map(memory, &iobj->bar, 0); 93 iobj->map = map; 94 } else { 95 nvkm_warn(subdev, "PRAMIN ioremap failed\n"); 96 nvkm_vm_put(&iobj->bar); 97 } 98 } else { 99 nvkm_warn(subdev, "PRAMIN exhausted\n"); 100 } 101} 102 103static void 104nv50_instobj_release(struct nvkm_memory *memory) 105{ 106 struct nv50_instmem *imem = nv50_instobj(memory)->imem; 107 spin_unlock_irqrestore(&imem->lock, imem->lock_flags); 108} 109 110static void __iomem * 111nv50_instobj_acquire(struct nvkm_memory *memory) 112{ 113 struct nv50_instobj *iobj = nv50_instobj(memory); 114 struct nv50_instmem *imem = iobj->imem; 115 struct nvkm_bar *bar = imem->base.subdev.device->bar; 116 struct nvkm_vm *vm; 117 unsigned long flags; 118 119 if (!iobj->map && (vm = nvkm_bar_kmap(bar))) 120 nvkm_memory_boot(memory, vm); 121 if (!IS_ERR_OR_NULL(iobj->map)) 122 return iobj->map; 123 124 spin_lock_irqsave(&imem->lock, flags); 125 imem->lock_flags = flags; 126 return NULL; 127} 128 129static u32 130nv50_instobj_rd32(struct nvkm_memory *memory, u64 offset) 131{ 132 struct nv50_instobj *iobj = nv50_instobj(memory); 133 struct nv50_instmem *imem = iobj->imem; 134 struct nvkm_device *device = imem->base.subdev.device; 135 u64 base = (iobj->mem->offset + offset) & 0xffffff00000ULL; 136 u64 addr = (iobj->mem->offset + offset) & 0x000000fffffULL; 137 u32 data; 138 139 if (unlikely(imem->addr != base)) { 140 nvkm_wr32(device, 0x001700, base >> 16); 141 imem->addr = base; 142 } 143 data = nvkm_rd32(device, 0x700000 + addr); 144 return data; 145} 146 147static void 148nv50_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data) 149{ 150 struct nv50_instobj *iobj = nv50_instobj(memory); 151 struct nv50_instmem *imem = iobj->imem; 152 struct nvkm_device *device = imem->base.subdev.device; 153 u64 base = (iobj->mem->offset + offset) & 0xffffff00000ULL; 154 u64 addr = (iobj->mem->offset + offset) & 0x000000fffffULL; 155 156 if (unlikely(imem->addr != base)) { 157 nvkm_wr32(device, 0x001700, base >> 16); 158 imem->addr = base; 159 } 160 nvkm_wr32(device, 0x700000 + addr, data); 161} 162 163static void 164nv50_instobj_map(struct nvkm_memory *memory, struct nvkm_vma *vma, u64 offset) 165{ 166 struct nv50_instobj *iobj = nv50_instobj(memory); 167 nvkm_vm_map_at(vma, offset, iobj->mem); 168} 169 170static void * 171nv50_instobj_dtor(struct nvkm_memory *memory) 172{ 173 struct nv50_instobj *iobj = nv50_instobj(memory); 174 struct nvkm_ram *ram = iobj->imem->base.subdev.device->fb->ram; 175 if (!IS_ERR_OR_NULL(iobj->map)) { 176 nvkm_vm_put(&iobj->bar); 177 iounmap(iobj->map); 178 } 179 ram->func->put(ram, &iobj->mem); 180 return iobj; 181} 182 183static const struct nvkm_memory_func 184nv50_instobj_func = { 185 .dtor = nv50_instobj_dtor, 186 .target = nv50_instobj_target, 187 .size = nv50_instobj_size, 188 .addr = nv50_instobj_addr, 189 .boot = nv50_instobj_boot, 190 .acquire = nv50_instobj_acquire, 191 .release = nv50_instobj_release, 192 .rd32 = nv50_instobj_rd32, 193 .wr32 = nv50_instobj_wr32, 194 .map = nv50_instobj_map, 195}; 196 197static int 198nv50_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero, 199 struct nvkm_memory **pmemory) 200{ 201 struct nv50_instmem *imem = nv50_instmem(base); 202 struct nv50_instobj *iobj; 203 struct nvkm_ram *ram = imem->base.subdev.device->fb->ram; 204 int ret; 205 206 if (!(iobj = kzalloc(sizeof(*iobj), GFP_KERNEL))) 207 return -ENOMEM; 208 *pmemory = &iobj->memory; 209 210 nvkm_memory_ctor(&nv50_instobj_func, &iobj->memory); 211 iobj->imem = imem; 212 213 size = max((size + 4095) & ~4095, (u32)4096); 214 align = max((align + 4095) & ~4095, (u32)4096); 215 216 ret = ram->func->get(ram, size, align, 0, 0x800, &iobj->mem); 217 if (ret) 218 return ret; 219 220 iobj->mem->page_shift = 12; 221 return 0; 222} 223 224/****************************************************************************** 225 * instmem subdev implementation 226 *****************************************************************************/ 227 228static void 229nv50_instmem_fini(struct nvkm_instmem *base) 230{ 231 nv50_instmem(base)->addr = ~0ULL; 232} 233 234static const struct nvkm_instmem_func 235nv50_instmem = { 236 .fini = nv50_instmem_fini, 237 .memory_new = nv50_instobj_new, 238 .persistent = false, 239 .zero = false, 240}; 241 242int 243nv50_instmem_new(struct nvkm_device *device, int index, 244 struct nvkm_instmem **pimem) 245{ 246 struct nv50_instmem *imem; 247 248 if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL))) 249 return -ENOMEM; 250 nvkm_instmem_ctor(&nv50_instmem, device, index, &imem->base); 251 spin_lock_init(&imem->lock); 252 *pimem = &imem->base; 253 return 0; 254} 255