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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
/* SPDX-License-Identifier: MIT
*
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
*/
#include "priv.h"
#include <linux/elf.h>
#include <linux/crc32.h>
#include <subdev/fb.h>
#include <subdev/fsp.h>
#include <rm/r570/nvrm/gsp.h>
#include <nvhw/drf.h>
#include <nvhw/ref/gh100/dev_falcon_v4.h>
#include <nvhw/ref/gh100/dev_riscv_pri.h>
int
gh100_gsp_fini(struct nvkm_gsp *gsp, bool suspend)
{
struct nvkm_falcon *falcon = &gsp->falcon;
int ret, time = 4000;
/* Shutdown RM. */
ret = r535_gsp_fini(gsp, suspend);
if (ret && suspend)
return ret;
/* Wait for RISC-V to halt. */
do {
u32 data = nvkm_falcon_rd32(falcon, falcon->addr2 + NV_PRISCV_RISCV_CPUCTL);
if (NVVAL_GET(data, NV_PRISCV, RISCV_CPUCTL, HALTED))
return 0;
usleep_range(1000, 2000);
} while(time--);
return -ETIMEDOUT;
}
static bool
gh100_gsp_lockdown_released(struct nvkm_gsp *gsp, u32 *mbox0)
{
u32 data;
/* Wait for GSP access via BAR0 to be allowed. */
*mbox0 = nvkm_falcon_rd32(&gsp->falcon, NV_PFALCON_FALCON_MAILBOX0);
if (*mbox0 && (*mbox0 & 0xffffff00) == 0xbadf4100)
return false;
/* Check if an error code has been reported. */
if (*mbox0) {
u32 mbox1 = nvkm_falcon_rd32(&gsp->falcon, NV_PFALCON_FALCON_MAILBOX1);
/* Any value that's not GSP_FMC_BOOT_PARAMS addr is an error. */
if ((((u64)mbox1 << 32) | *mbox0) != gsp->fmc.args.addr)
return true;
}
/* Check if lockdown has been released. */
data = nvkm_falcon_rd32(&gsp->falcon, NV_PFALCON_FALCON_HWCFG2);
return !NVVAL_GET(data, NV_PFALCON, FALCON_HWCFG2, RISCV_BR_PRIV_LOCKDOWN);
}
int
gh100_gsp_init(struct nvkm_gsp *gsp)
{
struct nvkm_subdev *subdev = &gsp->subdev;
struct nvkm_device *device = subdev->device;
const bool resume = gsp->sr.meta.data != NULL;
struct nvkm_gsp_mem *meta;
GSP_FMC_BOOT_PARAMS *args;
int ret, time = 4000;
u32 rsvd_size;
u32 mbox0;
if (!resume) {
ret = nvkm_gsp_mem_ctor(gsp, sizeof(*args), &gsp->fmc.args);
if (ret)
return ret;
meta = &gsp->wpr_meta;
} else {
gsp->rm->api->gsp->set_rmargs(gsp, true);
meta = &gsp->sr.meta;
}
args = gsp->fmc.args.data;
args->bootGspRmParams.gspRmDescOffset = meta->addr;
args->bootGspRmParams.gspRmDescSize = meta->size;
args->bootGspRmParams.target = GSP_DMA_TARGET_COHERENT_SYSTEM;
args->bootGspRmParams.bIsGspRmBoot = 1;
args->gspRmParams.target = GSP_DMA_TARGET_NONCOHERENT_SYSTEM;
args->gspRmParams.bootArgsOffset = gsp->libos.addr;
rsvd_size = gsp->fb.heap.size;
if (gsp->rm->wpr->rsvd_size_pmu)
rsvd_size = ALIGN(rsvd_size + gsp->rm->wpr->rsvd_size_pmu, 0x200000);
ret = nvkm_fsp_boot_gsp_fmc(device->fsp, gsp->fmc.args.addr, rsvd_size, resume,
gsp->fmc.fw.addr, gsp->fmc.hash, gsp->fmc.pkey, gsp->fmc.sig);
if (ret)
return ret;
do {
if (gh100_gsp_lockdown_released(gsp, &mbox0))
break;
usleep_range(1000, 2000);
} while(time--);
if (time < 0) {
nvkm_error(subdev, "GSP-FMC boot timed out\n");
return -ETIMEDOUT;
}
if (mbox0) {
nvkm_error(subdev, "GSP-FMC boot failed (mbox: 0x%08x)\n", mbox0);
return -EIO;
}
return r535_gsp_init(gsp);
}
static int
gh100_gsp_wpr_meta_init(struct nvkm_gsp *gsp)
{
GspFwWprMeta *meta;
int ret;
ret = nvkm_gsp_mem_ctor(gsp, sizeof(*meta), &gsp->wpr_meta);
if (ret)
return ret;
gsp->fb.size = nvkm_fb_vidmem_size(gsp->subdev.device);
gsp->fb.bios.vga_workspace.size = 128 * 1024;
gsp->fb.heap.size = gsp->rm->wpr->heap_size_non_wpr;
meta = gsp->wpr_meta.data;
meta->magic = GSP_FW_WPR_META_MAGIC;
meta->revision = GSP_FW_WPR_META_REVISION;
meta->sizeOfRadix3Elf = gsp->fw.len;
meta->sysmemAddrOfRadix3Elf = gsp->radix3.lvl0.addr;
meta->sizeOfBootloader = gsp->boot.fw.size;
meta->sysmemAddrOfBootloader = gsp->boot.fw.addr;
meta->bootloaderCodeOffset = gsp->boot.code_offset;
meta->bootloaderDataOffset = gsp->boot.data_offset;
meta->bootloaderManifestOffset = gsp->boot.manifest_offset;
meta->sysmemAddrOfSignature = gsp->sig.addr;
meta->sizeOfSignature = gsp->sig.size;
meta->nonWprHeapSize = gsp->fb.heap.size;
meta->gspFwHeapSize = tu102_gsp_wpr_heap_size(gsp);
meta->frtsSize = 0x100000;
meta->vgaWorkspaceSize = gsp->fb.bios.vga_workspace.size;
meta->pmuReservedSize = gsp->rm->wpr->rsvd_size_pmu;
return 0;
}
/* The sh_flags value for the binary blobs in the ELF image */
#define FMC_SHF_FLAGS (SHF_MASKPROC | SHF_MASKOS | SHF_OS_NONCONFORMING | SHF_ALLOC)
#define ELF_HDR_SIZE ((u8)sizeof(struct elf32_hdr))
#define ELF_SHDR_SIZE ((u8)sizeof(struct elf32_shdr))
/* The FMC ELF header must be exactly this */
static const u8 elf_header[] = {
0x7f, 'E', 'L', 'F', 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, /* e_type, e_machine, e_version */
0, 0, 0, 0, 0, 0, 0, 0, /* e_entry, e_phoff */
ELF_HDR_SIZE, 0, 0, 0, 0, 0, 0, 0, /* e_shoff, e_flags */
ELF_HDR_SIZE, 0, 0, 0, /* e_ehsize, e_phentsize */
0, 0, ELF_SHDR_SIZE, 0, /* e_phnum, e_shentsize */
6, 0, 1, 0, /* e_shnum, e_shstrndx */
};
/**
* elf_validate_sections - validate each section in the FMC ELF image
* @elf: ELF image
* @length: size of the entire ELF image
*/
static bool
elf_validate_sections(const void *elf, size_t length)
{
const struct elf32_hdr *ehdr = elf;
const struct elf32_shdr *shdr = elf + ehdr->e_shoff;
/* The offset of the first section */
Elf32_Off section_begin = ehdr->e_shoff + ehdr->e_shnum * ehdr->e_shentsize;
if (section_begin > length)
return false;
/* The first section header is the null section, so skip it */
for (unsigned int i = 1; i < ehdr->e_shnum; i++) {
if (i == ehdr->e_shstrndx) {
if (shdr[i].sh_type != SHT_STRTAB)
return false;
if (shdr[i].sh_flags != SHF_STRINGS)
return false;
} else {
if (shdr[i].sh_type != SHT_PROGBITS)
return false;
if (shdr[i].sh_flags != FMC_SHF_FLAGS)
return false;
}
/* Ensure that each section is inside the image */
if (shdr[i].sh_offset < section_begin ||
(u64)shdr[i].sh_offset + shdr[i].sh_size > length)
return false;
/* Non-zero sh_info is a CRC */
if (shdr[i].sh_info) {
/* The kernel's CRC32 needs a pre- and post-xor to match standard CRCs */
u32 crc32 = crc32_le(~0, elf + shdr[i].sh_offset, shdr[i].sh_size) ^ ~0;
if (shdr[i].sh_info != crc32)
return false;
}
}
return true;
}
/**
* elf_section - return a pointer to the data for a given section
* @elf: ELF image
* @name: section name to search for
* @len: pointer to returned length of found section
*/
static const void *
elf_section(const void *elf, const char *name, unsigned int *len)
{
const struct elf32_hdr *ehdr = elf;
const struct elf32_shdr *shdr = elf + ehdr->e_shoff;
const char *names = elf + shdr[ehdr->e_shstrndx].sh_offset;
for (unsigned int i = 1; i < ehdr->e_shnum; i++) {
if (!strcmp(&names[shdr[i].sh_name], name)) {
*len = shdr[i].sh_size;
return elf + shdr[i].sh_offset;
}
}
return NULL;
}
int
gh100_gsp_oneinit(struct nvkm_gsp *gsp)
{
struct nvkm_subdev *subdev = &gsp->subdev;
struct nvkm_device *device = subdev->device;
struct nvkm_fsp *fsp = device->fsp;
const void *fw = gsp->fws.fmc->data;
const void *hash, *sig, *pkey, *img;
unsigned int img_len = 0, hash_len = 0, pkey_len = 0, sig_len = 0;
int ret;
if (gsp->fws.fmc->size < ELF_HDR_SIZE ||
memcmp(fw, elf_header, sizeof(elf_header)) ||
!elf_validate_sections(fw, gsp->fws.fmc->size)) {
nvkm_error(subdev, "fmc firmware image is invalid\n");
return -ENODATA;
}
hash = elf_section(fw, "hash", &hash_len);
sig = elf_section(fw, "signature", &sig_len);
pkey = elf_section(fw, "publickey", &pkey_len);
img = elf_section(fw, "image", &img_len);
if (!hash || !sig || !pkey || !img) {
nvkm_error(subdev, "fmc firmware image is invalid\n");
return -ENODATA;
}
if (!nvkm_fsp_verify_gsp_fmc(fsp, hash_len, pkey_len, sig_len))
return -EINVAL;
/* Load GSP-FMC FW into memory. */
ret = nvkm_gsp_mem_ctor(gsp, img_len, &gsp->fmc.fw);
if (ret)
return ret;
memcpy(gsp->fmc.fw.data, img, img_len);
gsp->fmc.hash = kmemdup(hash, hash_len, GFP_KERNEL);
gsp->fmc.pkey = kmemdup(pkey, pkey_len, GFP_KERNEL);
gsp->fmc.sig = kmemdup(sig, sig_len, GFP_KERNEL);
if (!gsp->fmc.hash || !gsp->fmc.pkey || !gsp->fmc.sig)
return -ENOMEM;
ret = r535_gsp_oneinit(gsp);
if (ret)
return ret;
return gh100_gsp_wpr_meta_init(gsp);
}
static const struct nvkm_gsp_func
gh100_gsp = {
.flcn = &ga102_gsp_flcn,
.sig_section = ".fwsignature_gh100",
.dtor = r535_gsp_dtor,
.oneinit = gh100_gsp_oneinit,
.init = gh100_gsp_init,
.fini = gh100_gsp_fini,
.rm.gpu = &gh100_gpu,
};
int
gh100_gsp_load(struct nvkm_gsp *gsp, int ver, const struct nvkm_gsp_fwif *fwif)
{
int ret;
ret = tu102_gsp_load_rm(gsp, fwif);
if (ret)
goto done;
ret = nvkm_gsp_load_fw(gsp, "fmc", fwif->ver, &gsp->fws.fmc);
done:
if (ret)
nvkm_gsp_dtor_fws(gsp);
return ret;
}
static struct nvkm_gsp_fwif
gh100_gsps[] = {
{ 0, gh100_gsp_load, &gh100_gsp, &r570_rm_gh100, "570.144", true },
{}
};
int
gh100_gsp_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
struct nvkm_gsp **pgsp)
{
return nvkm_gsp_new_(gh100_gsps, device, type, inst, pgsp);
}
NVKM_GSP_FIRMWARE_FMC(gh100, 570.144);
|