nouveau_nvkm_subdev_instmem_nv50.c revision 1.3
1/* $NetBSD: nouveau_nvkm_subdev_instmem_nv50.c,v 1.3 2018/08/27 06:36:48 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.3 2018/08/27 06:36:48 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#ifdef __NetBSD__ 55 bus_space_tag_t bst; 56 bus_space_handle_t bsh; 57#endif 58 void *map; 59}; 60 61static enum nvkm_memory_target 62nv50_instobj_target(struct nvkm_memory *memory) 63{ 64 return NVKM_MEM_TARGET_VRAM; 65} 66 67static u64 68nv50_instobj_addr(struct nvkm_memory *memory) 69{ 70 return nv50_instobj(memory)->mem->offset; 71} 72 73static u64 74nv50_instobj_size(struct nvkm_memory *memory) 75{ 76 return (u64)nv50_instobj(memory)->mem->size << NVKM_RAM_MM_SHIFT; 77} 78 79static void 80nv50_instobj_boot(struct nvkm_memory *memory, struct nvkm_vm *vm) 81{ 82 struct nv50_instobj *iobj = nv50_instobj(memory); 83 struct nvkm_subdev *subdev = &iobj->imem->base.subdev; 84 struct nvkm_device *device = subdev->device; 85 u64 size = nvkm_memory_size(memory); 86 void __iomem *map; 87 int ret; 88 89 iobj->map = ERR_PTR(-ENOMEM); 90 91 ret = nvkm_vm_get(vm, size, 12, NV_MEM_ACCESS_RW, &iobj->bar); 92 if (ret == 0) { 93#ifdef __NetBSD__ 94 bus_space_tag_t bst = device->func->resource_tag(device, 3); 95 bus_space_handle_t bsh; 96 int ret; 97 98 /* Yes, truncation is really intended here. */ 99 /* XXX errno NetBSD->Linux */ 100 ret = -bus_space_map(bst, (u32)iobj->bar.offset, size, 101 BUS_SPACE_MAP_LINEAR, &bsh); 102 if (ret == 0) { 103 nvkm_memory_map(memory, &iobj->bar, 0); 104 iobj->bst = bst; 105 iobj->bsh = bsh; 106 iobj->map = bus_space_vaddr(bst, bsh); 107 } else { 108 nvkm_warn(subdev, "PRAMIN bus_space_map failed: %d\n", 109 ret); 110 nvkm_vm_put(&iobj->bar); 111 } 112#else 113 map = ioremap(device->func->resource_addr(device, 3) + 114 (u32)iobj->bar.offset, size); 115 if (map) { 116 nvkm_memory_map(memory, &iobj->bar, 0); 117 iobj->map = map; 118 } else { 119 nvkm_warn(subdev, "PRAMIN ioremap failed\n"); 120 nvkm_vm_put(&iobj->bar); 121 } 122#endif 123 } else { 124 nvkm_warn(subdev, "PRAMIN exhausted\n"); 125 } 126} 127 128static void 129nv50_instobj_release(struct nvkm_memory *memory) 130{ 131 struct nv50_instmem *imem = nv50_instobj(memory)->imem; 132 spin_unlock_irqrestore(&imem->lock, imem->lock_flags); 133} 134 135static void __iomem * 136nv50_instobj_acquire(struct nvkm_memory *memory) 137{ 138 struct nv50_instobj *iobj = nv50_instobj(memory); 139 struct nv50_instmem *imem = iobj->imem; 140 struct nvkm_bar *bar = imem->base.subdev.device->bar; 141 struct nvkm_vm *vm; 142 unsigned long flags; 143 144 if (!iobj->map && (vm = nvkm_bar_kmap(bar))) 145 nvkm_memory_boot(memory, vm); 146 if (!IS_ERR_OR_NULL(iobj->map)) 147 return iobj->map; 148 149 spin_lock_irqsave(&imem->lock, flags); 150 imem->lock_flags = flags; 151 return NULL; 152} 153 154static u32 155nv50_instobj_rd32(struct nvkm_memory *memory, u64 offset) 156{ 157 struct nv50_instobj *iobj = nv50_instobj(memory); 158 struct nv50_instmem *imem = iobj->imem; 159 struct nvkm_device *device = imem->base.subdev.device; 160 u64 base = (iobj->mem->offset + offset) & 0xffffff00000ULL; 161 u64 addr = (iobj->mem->offset + offset) & 0x000000fffffULL; 162 u32 data; 163 164 if (unlikely(imem->addr != base)) { 165 nvkm_wr32(device, 0x001700, base >> 16); 166 imem->addr = base; 167 } 168 data = nvkm_rd32(device, 0x700000 + addr); 169 return data; 170} 171 172static void 173nv50_instobj_wr32(struct nvkm_memory *memory, u64 offset, u32 data) 174{ 175 struct nv50_instobj *iobj = nv50_instobj(memory); 176 struct nv50_instmem *imem = iobj->imem; 177 struct nvkm_device *device = imem->base.subdev.device; 178 u64 base = (iobj->mem->offset + offset) & 0xffffff00000ULL; 179 u64 addr = (iobj->mem->offset + offset) & 0x000000fffffULL; 180 181 if (unlikely(imem->addr != base)) { 182 nvkm_wr32(device, 0x001700, base >> 16); 183 imem->addr = base; 184 } 185 nvkm_wr32(device, 0x700000 + addr, data); 186} 187 188static void 189nv50_instobj_map(struct nvkm_memory *memory, struct nvkm_vma *vma, u64 offset) 190{ 191 struct nv50_instobj *iobj = nv50_instobj(memory); 192 nvkm_vm_map_at(vma, offset, iobj->mem); 193} 194 195static void * 196nv50_instobj_dtor(struct nvkm_memory *memory) 197{ 198 struct nv50_instobj *iobj = nv50_instobj(memory); 199 struct nvkm_ram *ram = iobj->imem->base.subdev.device->fb->ram; 200 if (!IS_ERR_OR_NULL(iobj->map)) { 201 nvkm_vm_put(&iobj->bar); 202#ifdef __NetBSD__ 203 bus_space_unmap(iobj->bst, iobj->bsh, 204 nvkm_memory_size(iobj->memory)); 205 iobj->map = NULL; 206#else 207 iounmap(iobj->map); 208#endif 209 } 210 ram->func->put(ram, &iobj->mem); 211 return iobj; 212} 213 214static const struct nvkm_memory_func 215nv50_instobj_func = { 216 .dtor = nv50_instobj_dtor, 217 .target = nv50_instobj_target, 218 .size = nv50_instobj_size, 219 .addr = nv50_instobj_addr, 220 .boot = nv50_instobj_boot, 221 .acquire = nv50_instobj_acquire, 222 .release = nv50_instobj_release, 223 .rd32 = nv50_instobj_rd32, 224 .wr32 = nv50_instobj_wr32, 225 .map = nv50_instobj_map, 226}; 227 228static int 229nv50_instobj_new(struct nvkm_instmem *base, u32 size, u32 align, bool zero, 230 struct nvkm_memory **pmemory) 231{ 232 struct nv50_instmem *imem = nv50_instmem(base); 233 struct nv50_instobj *iobj; 234 struct nvkm_ram *ram = imem->base.subdev.device->fb->ram; 235 int ret; 236 237 if (!(iobj = kzalloc(sizeof(*iobj), GFP_KERNEL))) 238 return -ENOMEM; 239 *pmemory = &iobj->memory; 240 241 nvkm_memory_ctor(&nv50_instobj_func, &iobj->memory); 242 iobj->imem = imem; 243 244 size = max((size + 4095) & ~4095, (u32)4096); 245 align = max((align + 4095) & ~4095, (u32)4096); 246 247 ret = ram->func->get(ram, size, align, 0, 0x800, &iobj->mem); 248 if (ret) 249 return ret; 250 251 iobj->mem->page_shift = 12; 252 return 0; 253} 254 255/****************************************************************************** 256 * instmem subdev implementation 257 *****************************************************************************/ 258 259static void 260nv50_instmem_fini(struct nvkm_instmem *base) 261{ 262 nv50_instmem(base)->addr = ~0ULL; 263} 264 265static const struct nvkm_instmem_func 266nv50_instmem = { 267 .fini = nv50_instmem_fini, 268 .memory_new = nv50_instobj_new, 269 .persistent = false, 270 .zero = false, 271}; 272 273int 274nv50_instmem_new(struct nvkm_device *device, int index, 275 struct nvkm_instmem **pimem) 276{ 277 struct nv50_instmem *imem; 278 279 if (!(imem = kzalloc(sizeof(*imem), GFP_KERNEL))) 280 return -ENOMEM; 281 nvkm_instmem_ctor(&nv50_instmem, device, index, &imem->base); 282 spin_lock_init(&imem->lock); 283 *pimem = &imem->base; 284 return 0; 285} 286