Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | /* * Copyright 2023 Red Hat Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #include "priv.h" #include <subdev/gsp.h> #include <nvhw/drf.h> #include <nvrm/nvtypes.h> #include <nvrm/535.113.01/common/sdk/nvidia/inc/class/cl84a0.h> #include <nvrm/535.113.01/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080internal.h> #include <nvrm/535.113.01/common/sdk/nvidia/inc/nvos.h> #include <nvrm/535.113.01/nvidia/generated/g_fbsr_nvoc.h> #include <nvrm/535.113.01/nvidia/generated/g_rpc-structures.h> #include <nvrm/535.113.01/nvidia/kernel/inc/vgpu/rpc_global_enums.h> struct fbsr_item { const char *type; u64 addr; u64 size; struct list_head head; }; struct fbsr { struct list_head items; u64 size; int regions; struct nvkm_gsp_client client; struct nvkm_gsp_device device; u64 hmemory; u64 sys_offset; }; static int fbsr_memlist(struct nvkm_gsp_device *device, u32 handle, enum nvkm_memory_target aper, u64 phys, u64 size, struct sg_table *sgt, struct nvkm_gsp_object *object) { struct nvkm_gsp_client *client = device->object.client; struct nvkm_gsp *gsp = client->gsp; const u32 pages = size / GSP_PAGE_SIZE; rpc_alloc_memory_v13_01 *rpc; int ret; rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_ALLOC_MEMORY, sizeof(*rpc) + pages * sizeof(rpc->pteDesc.pte_pde[0])); if (IS_ERR(rpc)) return PTR_ERR(rpc); rpc->hClient = client->object.handle; rpc->hDevice = device->object.handle; rpc->hMemory = handle; if (aper == NVKM_MEM_TARGET_HOST) { rpc->hClass = NV01_MEMORY_LIST_SYSTEM; rpc->flags = NVDEF(NVOS02, FLAGS, PHYSICALITY, NONCONTIGUOUS) | NVDEF(NVOS02, FLAGS, LOCATION, PCI) | NVDEF(NVOS02, FLAGS, MAPPING, NO_MAP); } else { rpc->hClass = NV01_MEMORY_LIST_FBMEM; rpc->flags = NVDEF(NVOS02, FLAGS, PHYSICALITY, CONTIGUOUS) | NVDEF(NVOS02, FLAGS, LOCATION, VIDMEM) | NVDEF(NVOS02, FLAGS, MAPPING, NO_MAP); rpc->format = 6; /* NV_MMU_PTE_KIND_GENERIC_MEMORY */ } rpc->pteAdjust = 0; rpc->length = size; rpc->pageCount = pages; rpc->pteDesc.idr = 0; rpc->pteDesc.reserved1 = 0; rpc->pteDesc.length = pages; if (sgt) { struct scatterlist *sgl; int pte = 0, idx; for_each_sgtable_dma_sg(sgt, sgl, idx) { for (int i = 0; i < sg_dma_len(sgl) / GSP_PAGE_SIZE; i++) rpc->pteDesc.pte_pde[pte++].pte = (sg_dma_address(sgl) >> 12) + i; } } else { for (int i = 0; i < pages; i++) rpc->pteDesc.pte_pde[i].pte = (phys >> 12) + i; } ret = nvkm_gsp_rpc_wr(gsp, rpc, true); if (ret) return ret; object->client = device->object.client; object->parent = &device->object; object->handle = handle; return 0; } static int fbsr_send(struct fbsr *fbsr, struct fbsr_item *item) { NV2080_CTRL_INTERNAL_FBSR_SEND_REGION_INFO_PARAMS *ctrl; struct nvkm_gsp *gsp = fbsr->client.gsp; struct nvkm_gsp_object memlist; int ret; ret = fbsr_memlist(&fbsr->device, fbsr->hmemory, NVKM_MEM_TARGET_VRAM, item->addr, item->size, NULL, &memlist); if (ret) return ret; ctrl = nvkm_gsp_rm_ctrl_get(&gsp->internal.device.subdevice, NV2080_CTRL_CMD_INTERNAL_FBSR_SEND_REGION_INFO, sizeof(*ctrl)); if (IS_ERR(ctrl)) { ret = PTR_ERR(ctrl); goto done; } ctrl->fbsrType = FBSR_TYPE_DMA; ctrl->hClient = fbsr->client.object.handle; ctrl->hVidMem = fbsr->hmemory++; ctrl->vidOffset = 0; ctrl->sysOffset = fbsr->sys_offset; ctrl->size = item->size; ret = nvkm_gsp_rm_ctrl_wr(&gsp->internal.device.subdevice, ctrl); done: nvkm_gsp_rm_free(&memlist); if (ret) return ret; fbsr->sys_offset += item->size; return 0; } static int fbsr_init(struct fbsr *fbsr, struct sg_table *sgt, u64 items_size) { NV2080_CTRL_INTERNAL_FBSR_INIT_PARAMS *ctrl; struct nvkm_gsp *gsp = fbsr->client.gsp; struct nvkm_gsp_object memlist; int ret; ret = fbsr_memlist(&fbsr->device, fbsr->hmemory, NVKM_MEM_TARGET_HOST, 0, fbsr->size, sgt, &memlist); if (ret) return ret; ctrl = nvkm_gsp_rm_ctrl_get(&gsp->internal.device.subdevice, NV2080_CTRL_CMD_INTERNAL_FBSR_INIT, sizeof(*ctrl)); if (IS_ERR(ctrl)) return PTR_ERR(ctrl); ctrl->fbsrType = FBSR_TYPE_DMA; ctrl->numRegions = fbsr->regions; ctrl->hClient = fbsr->client.object.handle; ctrl->hSysMem = fbsr->hmemory++; ctrl->gspFbAllocsSysOffset = items_size; ret = nvkm_gsp_rm_ctrl_wr(&gsp->internal.device.subdevice, ctrl); if (ret) return ret; nvkm_gsp_rm_free(&memlist); return 0; } static bool fbsr_vram(struct fbsr *fbsr, const char *type, u64 addr, u64 size) { struct fbsr_item *item; if (!(item = kzalloc(sizeof(*item), GFP_KERNEL))) return false; item->type = type; item->addr = addr; item->size = size; list_add_tail(&item->head, &fbsr->items); return true; } static bool fbsr_inst(struct fbsr *fbsr, const char *type, struct nvkm_memory *memory) { return fbsr_vram(fbsr, type, nvkm_memory_addr(memory), nvkm_memory_size(memory)); } static void r535_instmem_resume(struct nvkm_instmem *imem) { /* RM has restored VRAM contents already, so just need to free the sysmem buffer. */ if (imem->rm.fbsr_valid) { nvkm_gsp_sg_free(imem->subdev.device, &imem->rm.fbsr); imem->rm.fbsr_valid = false; } } static int r535_instmem_suspend(struct nvkm_instmem *imem) { struct nvkm_subdev *subdev = &imem->subdev; struct nvkm_device *device = subdev->device; struct nvkm_gsp *gsp = device->gsp; struct nvkm_instobj *iobj; struct fbsr fbsr = {}; struct fbsr_item *item, *temp; u64 items_size; int ret; INIT_LIST_HEAD(&fbsr.items); fbsr.hmemory = 0xcaf00003; /* Create a list of all regions we need RM to save during suspend. */ list_for_each_entry(iobj, &imem->list, head) { if (iobj->preserve) { if (!fbsr_inst(&fbsr, "inst", &iobj->memory)) return -ENOMEM; } } list_for_each_entry(iobj, &imem->boot, head) { if (!fbsr_inst(&fbsr, "boot", &iobj->memory)) return -ENOMEM; } if (!fbsr_vram(&fbsr, "gsp-non-wpr", gsp->fb.heap.addr, gsp->fb.heap.size)) return -ENOMEM; /* Determine memory requirements. */ list_for_each_entry(item, &fbsr.items, head) { nvkm_debug(subdev, "fbsr: %016llx %016llx %s\n", item->addr, item->size, item->type); fbsr.size += item->size; fbsr.regions++; } items_size = fbsr.size; nvkm_debug(subdev, "fbsr: %d regions (0x%llx bytes)\n", fbsr.regions, items_size); fbsr.size += gsp->fb.rsvd_size; fbsr.size += gsp->fb.bios.vga_workspace.size; nvkm_debug(subdev, "fbsr: size: 0x%llx bytes\n", fbsr.size); ret = nvkm_gsp_sg(gsp->subdev.device, fbsr.size, &imem->rm.fbsr); if (ret) goto done; /* Tell RM about the sysmem which will hold VRAM contents across suspend. */ ret = nvkm_gsp_client_device_ctor(gsp, &fbsr.client, &fbsr.device); if (ret) goto done_sgt; ret = fbsr_init(&fbsr, &imem->rm.fbsr, items_size); if (WARN_ON(ret)) goto done_sgt; /* Send VRAM regions that need saving. */ list_for_each_entry(item, &fbsr.items, head) { ret = fbsr_send(&fbsr, item); if (WARN_ON(ret)) goto done_sgt; } imem->rm.fbsr_valid = true; /* Cleanup everything except the sysmem backup, which will be removed after resume. */ done_sgt: if (ret) /* ... unless we failed already. */ nvkm_gsp_sg_free(device, &imem->rm.fbsr); done: list_for_each_entry_safe(item, temp, &fbsr.items, head) { list_del(&item->head); kfree(item); } nvkm_gsp_device_dtor(&fbsr.device); nvkm_gsp_client_dtor(&fbsr.client); return ret; } static void * r535_instmem_dtor(struct nvkm_instmem *imem) { kfree(imem->func); return imem; } int r535_instmem_new(const struct nvkm_instmem_func *hw, struct nvkm_device *device, enum nvkm_subdev_type type, int inst, struct nvkm_instmem **pinstmem) { struct nvkm_instmem_func *rm; int ret; if (!(rm = kzalloc(sizeof(*rm), GFP_KERNEL))) return -ENOMEM; rm->dtor = r535_instmem_dtor; rm->fini = hw->fini; rm->suspend = r535_instmem_suspend; rm->resume = r535_instmem_resume; rm->memory_new = hw->memory_new; rm->memory_wrap = hw->memory_wrap; rm->zero = false; ret = nv50_instmem_new_(rm, device, type, inst, pinstmem); if (ret) kfree(rm); return ret; } |