1 /* $NetBSD: nouveau_nvkm_subdev_bar_gf100.c,v 1.3 2021/12/18 23:45:38 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_bar_gf100.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $"); 28 29 #include "gf100.h" 30 31 #include <core/memory.h> 32 #include <core/option.h> 33 #include <subdev/fb.h> 34 #include <subdev/mmu.h> 35 36 struct nvkm_vmm * 37 gf100_bar_bar1_vmm(struct nvkm_bar *base) 38 { 39 return gf100_bar(base)->bar[1].vmm; 40 } 41 42 void 43 gf100_bar_bar1_wait(struct nvkm_bar *base) 44 { 45 /* NFI why it's twice. */ 46 nvkm_bar_flush(base); 47 nvkm_bar_flush(base); 48 } 49 50 void 51 gf100_bar_bar1_fini(struct nvkm_bar *bar) 52 { 53 nvkm_mask(bar->subdev.device, 0x001704, 0x80000000, 0x00000000); 54 } 55 56 void 57 gf100_bar_bar1_init(struct nvkm_bar *base) 58 { 59 struct nvkm_device *device = base->subdev.device; 60 struct gf100_bar *bar = gf100_bar(base); 61 const u32 addr = nvkm_memory_addr(bar->bar[1].inst) >> 12; 62 nvkm_wr32(device, 0x001704, 0x80000000 | addr); 63 } 64 65 struct nvkm_vmm * 66 gf100_bar_bar2_vmm(struct nvkm_bar *base) 67 { 68 return gf100_bar(base)->bar[0].vmm; 69 } 70 71 void 72 gf100_bar_bar2_fini(struct nvkm_bar *bar) 73 { 74 nvkm_mask(bar->subdev.device, 0x001714, 0x80000000, 0x00000000); 75 } 76 77 void 78 gf100_bar_bar2_init(struct nvkm_bar *base) 79 { 80 struct nvkm_device *device = base->subdev.device; 81 struct gf100_bar *bar = gf100_bar(base); 82 u32 addr = nvkm_memory_addr(bar->bar[0].inst) >> 12; 83 if (bar->bar2_halve) 84 addr |= 0x40000000; 85 nvkm_wr32(device, 0x001714, 0x80000000 | addr); 86 } 87 88 static int 89 gf100_bar_oneinit_bar(struct gf100_bar *bar, struct gf100_barN *bar_vm, 90 struct lock_class_key *key, int bar_nr) 91 { 92 struct nvkm_device *device = bar->base.subdev.device; 93 resource_size_t bar_len; 94 int ret; 95 96 ret = nvkm_memory_new(device, NVKM_MEM_TARGET_INST, 0x1000, 0, false, 97 &bar_vm->inst); 98 if (ret) 99 return ret; 100 101 bar_len = device->func->resource_size(device, bar_nr); 102 if (!bar_len) 103 return -ENOMEM; 104 if (bar_nr == 3 && bar->bar2_halve) 105 bar_len >>= 1; 106 107 ret = nvkm_vmm_new(device, 0, bar_len, NULL, 0, key, 108 (bar_nr == 3) ? "bar2" : "bar1", &bar_vm->vmm); 109 if (ret) 110 return ret; 111 112 atomic_inc(&bar_vm->vmm->engref[NVKM_SUBDEV_BAR]); 113 bar_vm->vmm->debug = bar->base.subdev.debug; 114 115 /* 116 * Bootstrap page table lookup. 117 */ 118 if (bar_nr == 3) { 119 ret = nvkm_vmm_boot(bar_vm->vmm); 120 if (ret) 121 return ret; 122 } 123 124 return nvkm_vmm_join(bar_vm->vmm, bar_vm->inst); 125 } 126 127 int 128 gf100_bar_oneinit(struct nvkm_bar *base) 129 { 130 static struct lock_class_key bar1_lock; 131 static struct lock_class_key bar2_lock; 132 struct gf100_bar *bar = gf100_bar(base); 133 int ret; 134 135 /* BAR2 */ 136 if (bar->base.func->bar2.init) { 137 ret = gf100_bar_oneinit_bar(bar, &bar->bar[0], &bar2_lock, 3); 138 if (ret) 139 return ret; 140 141 bar->base.subdev.oneinit = true; 142 nvkm_bar_bar2_init(bar->base.subdev.device); 143 } 144 145 /* BAR1 */ 146 ret = gf100_bar_oneinit_bar(bar, &bar->bar[1], &bar1_lock, 1); 147 if (ret) 148 return ret; 149 150 return 0; 151 } 152 153 void * 154 gf100_bar_dtor(struct nvkm_bar *base) 155 { 156 struct gf100_bar *bar = gf100_bar(base); 157 158 nvkm_vmm_part(bar->bar[1].vmm, bar->bar[1].inst); 159 nvkm_vmm_unref(&bar->bar[1].vmm); 160 nvkm_memory_unref(&bar->bar[1].inst); 161 162 nvkm_vmm_part(bar->bar[0].vmm, bar->bar[0].inst); 163 nvkm_vmm_unref(&bar->bar[0].vmm); 164 nvkm_memory_unref(&bar->bar[0].inst); 165 return bar; 166 } 167 168 int 169 gf100_bar_new_(const struct nvkm_bar_func *func, struct nvkm_device *device, 170 int index, struct nvkm_bar **pbar) 171 { 172 struct gf100_bar *bar; 173 if (!(bar = kzalloc(sizeof(*bar), GFP_KERNEL))) 174 return -ENOMEM; 175 nvkm_bar_ctor(func, device, index, &bar->base); 176 bar->bar2_halve = nvkm_boolopt(device->cfgopt, "NvBar2Halve", false); 177 *pbar = &bar->base; 178 return 0; 179 } 180 181 static const struct nvkm_bar_func 182 gf100_bar_func = { 183 .dtor = gf100_bar_dtor, 184 .oneinit = gf100_bar_oneinit, 185 .bar1.init = gf100_bar_bar1_init, 186 .bar1.fini = gf100_bar_bar1_fini, 187 .bar1.wait = gf100_bar_bar1_wait, 188 .bar1.vmm = gf100_bar_bar1_vmm, 189 .bar2.init = gf100_bar_bar2_init, 190 .bar2.fini = gf100_bar_bar2_fini, 191 .bar2.wait = gf100_bar_bar1_wait, 192 .bar2.vmm = gf100_bar_bar2_vmm, 193 .flush = g84_bar_flush, 194 }; 195 196 int 197 gf100_bar_new(struct nvkm_device *device, int index, struct nvkm_bar **pbar) 198 { 199 return gf100_bar_new_(&gf100_bar_func, device, index, pbar); 200 } 201