mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
322 lines
13 KiB
C
322 lines
13 KiB
C
/* SPDX-License-Identifier: MIT */
|
|
#ifdef NV_RS_ROM_FW_HOST_FIXTURE
|
|
#include "rom_fw_mock.h"
|
|
#else
|
|
#include <linux/atomic.h>
|
|
#include <linux/dma-mapping.h>
|
|
#include <linux/errno.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/mutex.h>
|
|
#include <linux/overflow.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/version.h>
|
|
#if defined(CONFIG_X86)
|
|
#include <asm/set_memory.h>
|
|
#endif
|
|
#include <asm/pgtable.h>
|
|
#include "pci_adapter.h"
|
|
#endif
|
|
#include "rom_fw_adapter.h"
|
|
#define ROM_FW_MAX_BYTES 0x80000UL
|
|
struct rom_fw_descriptor {
|
|
struct nv_rs_rom_fw_spec spec;
|
|
u64 token, cookie;
|
|
struct page *page;
|
|
void *cpu;
|
|
dma_addr_t dma;
|
|
unsigned int order;
|
|
size_t allocated;
|
|
u32 tag;
|
|
bool restore_wb, dma_mapped, cpu_mapped, copied, verified;
|
|
};
|
|
struct nv_rs_rom_fw {
|
|
struct nv_rs_pci *pci;
|
|
struct device *device;
|
|
struct mutex lock;
|
|
u64 mask, alignment;
|
|
struct rom_fw_descriptor *descriptor;
|
|
};
|
|
#ifdef NV_RS_ROM_FW_VM_FIXTURE
|
|
/* These hooks are absent from the production module. The guest serializes
|
|
* contexts and observes actual direct-map PTEs/PFNs, including before free. */
|
|
static unsigned int vm_wb_failures;
|
|
static struct nv_rs_rom_fw_audit vm_last_free;
|
|
void nv_rs_rom_fw_vm_fail_wb(unsigned int count) { vm_wb_failures = count; }
|
|
void nv_rs_rom_fw_vm_last_free(struct nv_rs_rom_fw_audit *out) { *out = vm_last_free; }
|
|
static int vm_audit(struct rom_fw_descriptor *d, struct nv_rs_rom_fw_audit *out)
|
|
{
|
|
unsigned int i;
|
|
const u8 *bytes = d->cpu;
|
|
if (!d->page || !d->cpu || d->allocated / PAGE_SIZE > NV_RS_ROM_FW_AUDIT_PAGES ||
|
|
(d->dma_mapped && !d->cpu_mapped)) return -EINVAL;
|
|
memset(out, 0, sizeof(*out));
|
|
out->bytes=d->spec.bytes; out->allocated=d->allocated;
|
|
out->pages=d->allocated / PAGE_SIZE;
|
|
out->hash=14695981039346656037ULL;
|
|
for (i=0;i<d->spec.bytes;i++) out->hash=(out->hash^bytes[i])*1099511628211ULL;
|
|
for (i=0;i<out->pages;i++) {
|
|
unsigned long address=(unsigned long)d->cpu+i*PAGE_SIZE;
|
|
pte_t *entry=lookup_address(address,&out->level[i]);
|
|
if (!entry) return -EIO;
|
|
out->pfn[i]=page_to_pfn(virt_to_page((void *)address));
|
|
out->pte[i]=pte_val(*entry);
|
|
}
|
|
return 0;
|
|
}
|
|
int nv_rs_rom_fw_vm_audit(struct nv_rs_rom_fw *c, u64 token, struct nv_rs_rom_fw_audit *out)
|
|
{
|
|
int error;
|
|
struct rom_fw_descriptor *d;
|
|
if (!c || !out) return -EINVAL;
|
|
mutex_lock(&c->lock);
|
|
d=c->descriptor;
|
|
error=d && token && d->token==token ? vm_audit(d,out) : -EINVAL;
|
|
mutex_unlock(&c->lock); return error;
|
|
}
|
|
#endif
|
|
static int restore_pages_wb(unsigned long address, unsigned int pages)
|
|
{
|
|
#ifdef NV_RS_ROM_FW_VM_FIXTURE
|
|
if (vm_wb_failures) { vm_wb_failures--; return -EIO; }
|
|
#endif
|
|
#if defined(CONFIG_X86) || defined(NV_RS_ROM_FW_HOST_FIXTURE)
|
|
return set_memory_wb(address,pages);
|
|
#else
|
|
(void)address; (void)pages;
|
|
return 0;
|
|
#endif
|
|
}
|
|
static atomic64_t rom_fw_next_token = ATOMIC64_INIT(1);
|
|
static u64 next_token(void)
|
|
{
|
|
s64 old = atomic64_read(&rom_fw_next_token);
|
|
for (;;) {
|
|
s64 seen;
|
|
if (old <= 0 || old == S64_MAX) return 0;
|
|
seen = atomic64_cmpxchg(&rom_fw_next_token, old, old + 1);
|
|
if (seen == old) return (u64)old;
|
|
old = seen;
|
|
}
|
|
}
|
|
static struct rom_fw_descriptor *lookup(struct nv_rs_rom_fw *c, u64 token)
|
|
{ return c->descriptor && token && c->descriptor->token == token ? c->descriptor : NULL; }
|
|
static int verify_pages(struct rom_fw_descriptor *d, u32 cache)
|
|
{
|
|
#ifdef NV_RS_ROM_FW_HOST_FIXTURE
|
|
return rom_fw_mock_verify_pages(d->page, d->cpu, d->allocated, cache);
|
|
#elif defined(CONFIG_X86)
|
|
unsigned long i, base = page_to_pfn(d->page);
|
|
unsigned long expected = cachemode2protval(cache ? _PAGE_CACHE_MODE_UC_MINUS : _PAGE_CACHE_MODE_WB) & _PAGE_CACHE_MASK;
|
|
for (i = 0; i < d->allocated / PAGE_SIZE; i++) {
|
|
unsigned int level;
|
|
unsigned long address = (unsigned long)d->cpu + i * PAGE_SIZE;
|
|
pte_t *entry = lookup_address(address, &level);
|
|
pteval_t value;
|
|
if (!entry || page_to_pfn(virt_to_page((void *)address)) != base + i) return -EIO;
|
|
value = pte_val(*entry);
|
|
if (!(value & _PAGE_PRESENT)) return -EIO;
|
|
if (level > PG_LEVEL_4K) value = protval_large_2_4k(value);
|
|
if ((value & _PAGE_CACHE_MASK) != expected) return -EIO;
|
|
}
|
|
return 0;
|
|
#else
|
|
(void)d; (void)cache;
|
|
return 0;
|
|
#endif
|
|
}
|
|
int nv_rs_rom_fw_create(struct nv_rs_pci *pci, u32 bits, u64 alignment, struct nv_rs_rom_fw **out)
|
|
{
|
|
struct nv_rs_rom_fw *c;
|
|
struct device *dev;
|
|
int error;
|
|
if (!out) return -EINVAL;
|
|
*out = NULL;
|
|
if (bits < 32 || bits > 64 || alignment < PAGE_SIZE ||
|
|
alignment > ROM_FW_MAX_BYTES || !is_power_of_2(alignment)) return -EINVAL;
|
|
if (!pci || !nv_rs_pci_get(pci)) return -ENODEV;
|
|
error = nv_rs_pci_dma_acquire(pci, bits);
|
|
if (error) { nv_rs_pci_put(pci); return error; }
|
|
dev = nv_rs_pci_device(pci);
|
|
if (!dev || !dev->dma_mask || DMA_BIT_MASK(bits) > *dev->dma_mask || nv_rs_pci_is_removed(pci)) {
|
|
nv_rs_pci_dma_release(pci); nv_rs_pci_put(pci); return -ENODEV;
|
|
}
|
|
c = kzalloc(sizeof(*c), GFP_KERNEL);
|
|
if (!c) { nv_rs_pci_dma_release(pci); nv_rs_pci_put(pci); return -ENOMEM; }
|
|
c->pci = pci; c->device = dev; c->mask = DMA_BIT_MASK(bits); c->alignment = alignment;
|
|
mutex_init(&c->lock); *out = c; return 0;
|
|
}
|
|
int nv_rs_rom_fw_close(struct nv_rs_rom_fw *c)
|
|
{
|
|
if (!c) return -EINVAL;
|
|
mutex_lock(&c->lock);
|
|
if (c->descriptor) { mutex_unlock(&c->lock); return -EBUSY; }
|
|
mutex_unlock(&c->lock);
|
|
#ifndef NV_RS_ROM_FW_HOST_FIXTURE
|
|
mutex_destroy(&c->lock);
|
|
#endif
|
|
nv_rs_pci_dma_release(c->pci); nv_rs_pci_put(c->pci); kfree(c); return 0;
|
|
}
|
|
int nv_rs_rom_fw_desc_create(struct nv_rs_rom_fw *c, const struct nv_rs_rom_fw_spec *spec, u64 *token)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
u64 identity;
|
|
if (!c || !spec || !token) return -EINVAL;
|
|
*token = 0;
|
|
if (!spec->bytes || spec->bytes > ROM_FW_MAX_BYTES || spec->alignment ||
|
|
spec->physically_contiguous != 1 || spec->reserved || spec->flags) return -EINVAL;
|
|
/* No FBMEM or WC approximation; those require the actual GPU allocator. */
|
|
if (spec->address_space != 1 || spec->cpu_cache > 1) return -EOPNOTSUPP;
|
|
mutex_lock(&c->lock);
|
|
if (nv_rs_pci_is_removed(c->pci)) { mutex_unlock(&c->lock); return -ENODEV; }
|
|
if (c->descriptor) { mutex_unlock(&c->lock); return -EBUSY; }
|
|
identity = next_token();
|
|
if (!identity) { mutex_unlock(&c->lock); return -ENOSPC; }
|
|
d = kzalloc(sizeof(*d), GFP_KERNEL);
|
|
if (!d) { mutex_unlock(&c->lock); return -ENOMEM; }
|
|
d->spec = *spec; d->token = identity; c->descriptor = d; *token = identity;
|
|
mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_tag(struct nv_rs_rom_fw *c, u64 token, u32 tag)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
if (!c) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d) { mutex_unlock(&c->lock); return -EINVAL; }
|
|
d->tag = tag; mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_allocate(struct nv_rs_rom_fw *c, u64 token)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
u64 last;
|
|
int error;
|
|
if (!c) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d || d->page) { mutex_unlock(&c->lock); return -EINVAL; }
|
|
if (nv_rs_pci_is_removed(c->pci)) { mutex_unlock(&c->lock); return -ENODEV; }
|
|
d->order = get_order(max_t(u64, d->spec.bytes, c->alignment));
|
|
#if defined(MAX_PAGE_ORDER)
|
|
/* Match stock 580.178.04 common/inc/nv-linux.h: MAX_PAGE_ORDER is inclusive (>= 6.8). */
|
|
if (d->order > MAX_PAGE_ORDER) { mutex_unlock(&c->lock); return -ENOMEM; }
|
|
#else
|
|
if (d->order >= MAX_ORDER) { mutex_unlock(&c->lock); return -ENOMEM; }
|
|
#endif
|
|
d->allocated = PAGE_SIZE << d->order;
|
|
d->page = alloc_pages(GFP_KERNEL | __GFP_ZERO, d->order);
|
|
if (!d->page) { mutex_unlock(&c->lock); return -ENOMEM; }
|
|
d->cpu = page_address(d->page);
|
|
if (!d->cpu) {
|
|
/* No cache/DMA operation has happened. Never send address0 through
|
|
* the WB retry path for a page without a direct CPU mapping. */
|
|
__free_pages(d->page, d->order);
|
|
d->page = NULL; d->allocated = 0;
|
|
mutex_unlock(&c->lock); return -ENOMEM;
|
|
}
|
|
if (d->spec.cpu_cache == 1) {
|
|
/* Failure may have changed a prefix. Retain all pages until WB restore
|
|
* succeeds; neither DMA failure nor removal may free UC pages. */
|
|
d->restore_wb = true;
|
|
#if defined(CONFIG_X86) || defined(NV_RS_ROM_FW_HOST_FIXTURE)
|
|
error = set_memory_uc((unsigned long)d->cpu, d->allocated >> PAGE_SHIFT);
|
|
#else
|
|
error = 0;
|
|
#endif
|
|
if (error) { mutex_unlock(&c->lock); return error; }
|
|
}
|
|
error = verify_pages(d, d->spec.cpu_cache);
|
|
if (error) { d->restore_wb = true; mutex_unlock(&c->lock); return error; }
|
|
d->dma = dma_map_page(c->device, d->page, 0, d->allocated, DMA_TO_DEVICE);
|
|
if (dma_mapping_error(c->device, d->dma)) { mutex_unlock(&c->lock); return -EIO; }
|
|
d->dma_mapped = true;
|
|
if (check_add_overflow((u64)d->dma, (u64)d->allocated - 1, &last) || last > c->mask ||
|
|
((u64)d->dma & (c->alignment - 1)) || nv_rs_pci_is_removed(c->pci)) {
|
|
mutex_unlock(&c->lock); return -ERANGE;
|
|
}
|
|
d->verified = true;
|
|
mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_map(struct nv_rs_rom_fw *c, u64 token, u64 offset, u64 bytes, u32 type, u32 flags, u64 *cookie)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
u64 identity;
|
|
if (!c || !cookie) return -EINVAL;
|
|
*cookie = 0;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d || !d->page || !d->verified || !d->dma_mapped || d->cpu_mapped || offset || bytes != d->spec.bytes || type != 1 || flags != 3) {
|
|
mutex_unlock(&c->lock); return -EINVAL;
|
|
}
|
|
if (nv_rs_pci_is_removed(c->pci)) { mutex_unlock(&c->lock); return -ENODEV; }
|
|
identity = next_token();
|
|
if (!identity) { mutex_unlock(&c->lock); return -ENOSPC; }
|
|
dma_sync_single_for_cpu(c->device, d->dma, d->allocated, DMA_TO_DEVICE);
|
|
d->cpu_mapped = true; d->copied = false; d->cookie = identity; *cookie = identity;
|
|
mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_copy(struct nv_rs_rom_fw *c, u64 token, u64 cookie, const u8 *source, u64 bytes)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
if (!c || !source) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d || !d->cpu_mapped || !cookie || cookie != d->cookie || bytes != d->spec.bytes) {
|
|
mutex_unlock(&c->lock); return -EINVAL;
|
|
}
|
|
if (nv_rs_pci_is_removed(c->pci)) { mutex_unlock(&c->lock); return -ENODEV; }
|
|
memcpy(d->cpu, source, bytes); d->copied = true; mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_unmap(struct nv_rs_rom_fw *c, u64 token, u64 cookie)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
if (!c) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d || !d->cpu_mapped || !cookie || cookie != d->cookie) { mutex_unlock(&c->lock); return -EINVAL; }
|
|
dma_sync_single_for_device(c->device, d->dma, d->allocated, DMA_TO_DEVICE);
|
|
dma_wmb(); d->cpu_mapped = false; d->cookie = 0; mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_free(struct nv_rs_rom_fw *c, u64 token)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
int error;
|
|
if (!c) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d || d->cpu_mapped) { mutex_unlock(&c->lock); return -EBUSY; }
|
|
d->copied = false; d->verified = false;
|
|
if (d->dma_mapped) { dma_unmap_page(c->device, d->dma, d->allocated, DMA_TO_DEVICE); d->dma_mapped = false; }
|
|
if (d->restore_wb) {
|
|
error = restore_pages_wb((unsigned long)d->cpu, d->allocated >> PAGE_SHIFT);
|
|
if (error) { mutex_unlock(&c->lock); return error; }
|
|
error = verify_pages(d, 0);
|
|
if (error) { mutex_unlock(&c->lock); return error; }
|
|
d->restore_wb = false;
|
|
}
|
|
if (d->page && verify_pages(d, 0)) { d->restore_wb = true; mutex_unlock(&c->lock); return -EIO; }
|
|
if (d->page) {
|
|
#ifdef NV_RS_ROM_FW_VM_FIXTURE
|
|
/* DMA has been unmapped; readback is CPU-owned again. */
|
|
error=vm_audit(d,&vm_last_free);
|
|
if(error) { mutex_unlock(&c->lock); return error; }
|
|
#endif
|
|
__free_pages(d->page, d->order); d->page = NULL; d->cpu = NULL;
|
|
}
|
|
d->allocated = 0; mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_destroy(struct nv_rs_rom_fw *c, u64 token)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
if (!c) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d) { mutex_unlock(&c->lock); return -EINVAL; }
|
|
if (d->page || d->dma_mapped || d->cpu_mapped || d->restore_wb) { mutex_unlock(&c->lock); return -EBUSY; }
|
|
c->descriptor = NULL; kfree(d); mutex_unlock(&c->lock); return 0;
|
|
}
|
|
int nv_rs_rom_fw_info(struct nv_rs_rom_fw *c, u64 token, struct nv_rs_rom_fw_info *out)
|
|
{
|
|
struct rom_fw_descriptor *d;
|
|
if (!c || !out) return -EINVAL;
|
|
mutex_lock(&c->lock); d = lookup(c, token);
|
|
if (!d || !d->page || !d->verified || !d->dma_mapped || d->cpu_mapped || !d->copied) { mutex_unlock(&c->lock); return -EINVAL; }
|
|
if (nv_rs_pci_is_removed(c->pci)) { mutex_unlock(&c->lock); return -ENODEV; }
|
|
*out = (struct nv_rs_rom_fw_info){.bytes=d->spec.bytes,.allocated_bytes=d->allocated,.dma_address=d->dma,
|
|
.cpu_cache=d->spec.cpu_cache,.address_space=d->spec.address_space,.physically_contiguous=1,.tag=d->tag,.copied=1};
|
|
mutex_unlock(&c->lock); return 0;
|
|
}
|