Files
nvdriver/kernel-open/nvidia/pci_adapter.c

1046 lines
37 KiB
C

// SPDX-License-Identifier: MIT
/* Linux ownership/access adapter. GPU algorithms and ordering live in Rust. */
#ifdef NV_RS_PCI_HOST_FIXTURE
#include "pci_mock.h"
#else
#include <linux/pci.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/version.h>
#include <generated/utsrelease.h>
#include <linux/bitops.h>
#include <linux/string.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/kref.h>
#include <linux/atomic.h>
#include <linux/dma-mapping.h>
#include <linux/pm_runtime.h>
#include <linux/vgaarb.h>
#ifdef NV_RS_HAVE_VIDEO_IS_PRIMARY_DEVICE
#include <asm/video.h>
#endif
#endif
#include "pci_adapter.h"
#if (!defined(NV_RS_PCI_HOST_FIXTURE) || defined(NV_RS_PCI_TEST_REBAR)) && !defined(NV_RS_PCI_TEST_EDU)
static bool enable_resizable_bar = true;
#ifndef NV_RS_PCI_HOST_FIXTURE
module_param(enable_resizable_bar, bool, 0444);
MODULE_PARM_DESC(enable_resizable_bar, "Automatically resize PCIe BAR1 to the largest supported ReBAR aperture (default: 1; skipped on primary boot display or framebuffer-claimed BAR1)");
#endif
static bool nv_rs_pci_is_primary_display(struct pci_dev *pdev)
{
#if defined(NV_RS_HAVE_VIDEO_IS_PRIMARY_DEVICE) && IS_ENABLED(CONFIG_VIDEO)
if (video_is_primary_device(&pdev->dev))
return true;
#endif
return vga_default_device() == pdev;
}
#endif
struct nv_rs_pci {
struct pci_dev *pdev;
void __iomem *bar0;
resource_size_t length;
void __iomem *bar2;
resource_size_t bar2_length;
void __iomem *bar1;
resource_size_t bar1_length;
void __iomem *bar1_wc;
resource_size_t bar1_wc_length;
struct mutex access;
raw_spinlock_t timer_irq;
raw_spinlock_t fifo_irq;
raw_spinlock_t fifo_context_irq;
raw_spinlock_t pmu_mutex_irq;
raw_spinlock_t pmu_dmem_irq;
struct kref refs;
bool removed;
struct nv_rs_pci *next_retained;
bool devinit_dma_retained;
bool devinit_dma_drained;
unsigned int dma_bits;
unsigned int dma_users;
bool dma_widen_warned;
#ifndef NV_RS_PCI_HOST_FIXTURE
atomic_t pm_resuming;
#endif
};
static int nv_rs_pci_quiesce_dma_locked(struct nv_rs_pci *p);
/* A failed Devinit owns at most one entry per PCI function. Keeping the
* registry on the old lease requires no allocation on the failure path. */
static DEFINE_MUTEX(retained_lock);
static struct nv_rs_pci *retained_pci;
static void nv_rs_pci_release(struct kref *ref)
{
struct nv_rs_pci *p = container_of(ref, struct nv_rs_pci, refs);
bool retained = p->devinit_dma_retained;
/* Retained DMA was drained or disconnected. A new driver may own MASTER. */
if (!retained)
pci_clear_master(p->pdev);
if (p->bar2) pci_iounmap(p->pdev, p->bar2);
if (p->bar1_wc) iounmap(p->bar1_wc);
if (p->bar1) pci_iounmap(p->pdev, p->bar1);
pci_iounmap(p->pdev, p->bar0);
pci_release_region(p->pdev, 0);
pci_disable_device(p->pdev);
if (retained) {
struct nv_rs_pci **link;
mutex_lock(&retained_lock);
for (link = &retained_pci; *link; link = &(*link)->next_retained) {
if (*link == p) {
*link = p->next_retained;
break;
}
}
mutex_unlock(&retained_lock);
dev_info(&p->pdev->dev, "Devinit DMA cleanup complete; BAR0 released\n");
}
pci_dev_put(p->pdev);
kfree(p);
if (retained)
module_put(THIS_MODULE);
}
bool nv_rs_pci_get(struct nv_rs_pci *p)
{
return p && kref_get_unless_zero(&p->refs);
}
void nv_rs_pci_put(struct nv_rs_pci *p)
{
if (p) kref_put(&p->refs, nv_rs_pci_release);
}
struct device *nv_rs_pci_device(struct nv_rs_pci *p)
{
return p ? &p->pdev->dev : NULL;
}
void *nv_rs_pci_pdev(struct nv_rs_pci *p)
{
return p ? (void *)p->pdev : NULL;
}
bool nv_rs_pci_is_removed(struct nv_rs_pci *p)
{
return !p || READ_ONCE(p->removed) || pci_dev_is_disconnected(p->pdev);
}
int nv_rs_pci_bar0_info(struct nv_rs_pci *p,u64 *mapped,u64 *physical,u64 *bytes)
{
if (!p || !mapped || !physical || !bytes) return -EINVAL;
if (nv_rs_pci_is_removed(p)) return -ENODEV;
*mapped=(u64)(unsigned long)p->bar0;
*physical=pci_resource_start(p->pdev,0);
*bytes=p->length;
return 0;
}
#ifndef NV_RS_PCI_HOST_FIXTURE
void nv_rs_pci_pm_resume_enter(struct nv_rs_pci *p)
{
if (p)
atomic_inc(&p->pm_resuming);
}
void nv_rs_pci_pm_resume_leave(struct nv_rs_pci *p)
{
if (p)
atomic_dec(&p->pm_resuming);
}
/* Temporary access reference for a sleepable adapter operation. The caller
* pins this PCI owner and drains its operation before removal. A PCI resume
* may sleep, so it runs outside the BAR-access mutex. When called from the
* runtime kthread spawned synchronously inside a PM/RTD3 resume callback
* (`pm_resuming > 0`), take a non-blocking refcount so `pm_runtime_resume_and_get`
* cannot deadlock waiting for the in-progress `RPM_RESUMING` callback. */
int nv_rs_pci_power_get(struct nv_rs_pci *p)
{
struct device *dev;
int error = 0;
if (nv_rs_pci_is_removed(p))
return -ENODEV;
dev = &p->pdev->dev;
if (pm_runtime_enabled(dev) && atomic_read(&p->pm_resuming) == 0) {
error = pm_runtime_resume_and_get(dev);
if (error)
return error;
} else {
pm_runtime_get_noresume(dev);
}
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p))
error = -ENODEV;
else if (READ_ONCE(p->pdev->current_state) != PCI_D0)
error = -EIO;
mutex_unlock(&p->access);
if (error)
pm_runtime_put_noidle(dev);
return error;
}
void nv_rs_pci_power_put(struct nv_rs_pci *p)
{
/* A synchronous operation must not invoke an idle callback while it holds
* the GPU operation mutex. The lifecycle owner controls idle scheduling. */
pm_runtime_put_noidle(&p->pdev->dev);
}
#endif
void nv_rs_pci_mark_removed(struct nv_rs_pci *p)
{
if (!p) return;
mutex_lock(&p->access);
WRITE_ONCE(p->removed, true);
/* Record quiescence while this binding still owns the function. */
if (p->devinit_dma_retained && !p->devinit_dma_drained &&
!pci_dev_is_disconnected(p->pdev)) {
int error = nv_rs_pci_quiesce_dma_locked(p);
WRITE_ONCE(p->devinit_dma_drained, !error);
if (error)
dev_warn(&p->pdev->dev, "Devinit DMA drain failed (%d); see GP104 recovery in docs/INSTALL.md\n", error);
}
mutex_unlock(&p->access);
}
/* Probe holds the device lock and the PCI core has resumed the function.
* Give an earlier binding its drain opportunity before taking BAR0 or
* changing the device's resources. Its worker still owns DMA teardown. */
static int nv_rs_pci_recover_retained(struct pci_dev *pdev)
{
struct nv_rs_pci *old;
int error = 0;
mutex_lock(&retained_lock);
for (old = retained_pci; old; old = old->next_retained) {
if (old->pdev == pdev) {
if (!nv_rs_pci_get(old)) {
mutex_unlock(&retained_lock);
return -EAGAIN;
}
break;
}
}
mutex_unlock(&retained_lock);
if (!old)
return 0;
mutex_lock(&old->access);
if (!old->removed)
error = -EBUSY;
else if (!old->devinit_dma_drained && !pci_dev_is_disconnected(pdev)) {
error = nv_rs_pci_quiesce_dma_locked(old);
if (!error)
WRITE_ONCE(old->devinit_dma_drained, true);
}
mutex_unlock(&old->access);
nv_rs_pci_put(old);
return error ? error : -EAGAIN;
}
int nv_rs_pci_open(struct pci_dev *pdev, struct nv_rs_pci **out)
{
struct nv_rs_pci *p;
int error;
if (!out) return -EINVAL;
*out = NULL;
/* Refuse to steal hardware from another driver or open unclaimed BARs. */
if (!pdev || !pdev->driver || strcmp(pdev->driver->name, "nvidia_rs"))
return -EPERM;
#ifdef NV_RS_PCI_TEST_EDU
/* Guest-test build only. Production never admits this emulated device. */
if (pdev->vendor != 0x1234 || pdev->device != 0x11e8) return -ENODEV;
#else
if (pdev->vendor != 0x10de || (pdev->class >> 16) != PCI_BASE_CLASS_DISPLAY ||
(((pdev->class >> 8) & 0xff) != 0 && ((pdev->class >> 8) & 0xff) != 2))
return -ENODEV;
#endif
error = nv_rs_pci_recover_retained(pdev);
if (error) return error;
if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
!pci_resource_start(pdev, 0) || pci_resource_len(pdev, 0) < 0xa04)
return -ENXIO;
#if (!defined(NV_RS_PCI_HOST_FIXTURE) || defined(NV_RS_PCI_TEST_REBAR)) && !defined(NV_RS_PCI_TEST_EDU)
/* Unlock Resizable BAR1 automatically (enabled by default, no GeForce/product-tier gate).
* Mirrors Linux PCI ReBAR expansion before pci_enable_device_mem() / BAR1 mapping,
* but refuses to disable PCI_COMMAND_MEMORY or release BAR1 when the GPU is the primary
* boot display device or a child resource (e.g. simpledrm/efifb framebuffer) claims BAR1/BAR3. */
if (enable_resizable_bar) {
const int bar1 = 1;
const int bar3 = 3;
resource_size_t bar1_len = pci_resource_len(pdev, bar1);
unsigned long sizes = bar1_len ? pci_rebar_get_possible_sizes(pdev, bar1) : 0;
if (sizes != 0) {
int requested = fls(sizes) - 1;
int old_size = pci_rebar_bytes_to_size(bar1_len);
struct pci_host_bridge *host = pci_find_host_bridge(pdev->bus);
if (old_size >= 0 && requested > old_size && (!host || !host->preserve_config)) {
if (nv_rs_pci_is_primary_display(pdev) ||
pdev->resource[bar1].child != NULL ||
pdev->resource[bar3].child != NULL) {
dev_info(&pdev->dev,
"nvdriver: skipping BAR1 ReBAR resize on active primary/framebuffer GPU\n");
} else {
u16 cmd = 0;
int r;
bool release_bar3 = pci_resource_len(pdev, bar3) != 0 &&
!(pci_resource_flags(pdev, bar3) & IORESOURCE_UNSET);
bool resized = false;
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
pci_write_config_word(pdev, PCI_COMMAND, cmd & ~PCI_COMMAND_MEMORY);
pci_release_resource(pdev, bar1);
if (release_bar3)
pci_release_resource(pdev, bar3);
for (;;) {
#if defined(NV_RS_PCI_RESIZE_EXCLUDE_BARS) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
r = pci_resize_resource(pdev, bar1, requested, 0);
#else
r = pci_resize_resource(pdev, bar1, requested);
#endif
if (r == 0) {
resized = (requested != old_size);
break;
}
if (r == -ENOSPC && requested > old_size) {
clear_bit(requested, &sizes);
if (sizes == 0)
break;
requested = fls(sizes) - 1;
if (requested <= old_size)
break;
continue;
}
break;
}
pci_assign_unassigned_bus_resources(pdev->bus);
if ((pci_resource_flags(pdev, bar1) & IORESOURCE_UNSET) ||
(release_bar3 && (pci_resource_flags(pdev, bar3) & IORESOURCE_UNSET))) {
if (resized) {
#if defined(NV_RS_PCI_RESIZE_EXCLUDE_BARS) || LINUX_VERSION_CODE >= KERNEL_VERSION(6, 19, 0)
r = pci_resize_resource(pdev, bar1, old_size, 0);
#else
r = pci_resize_resource(pdev, bar1, old_size);
#endif
if (r != 0)
dev_warn(&pdev->dev,
"nvdriver: failed to restore original BAR1 size (%d)\n", r);
pci_assign_unassigned_bus_resources(pdev->bus);
}
if (pci_resource_flags(pdev, bar1) & IORESOURCE_UNSET)
dev_warn(&pdev->dev,
"nvdriver: BAR1 remained unassigned after ReBAR resize attempt\n");
}
pci_write_config_word(pdev, PCI_COMMAND, cmd);
}
}
}
}
#endif
p = kzalloc(sizeof(*p), GFP_KERNEL);
if (!p) return -ENOMEM;
p->pdev = pci_dev_get(pdev);
error = pci_enable_device_mem(pdev);
if (error) goto fail_reference;
error = pci_request_region(pdev, 0, "nvidia_rs");
if (error) goto fail_enable;
p->length = pci_resource_len(pdev, 0);
p->bar0 = pci_iomap(pdev, 0, 0);
if (!p->bar0) { error = -ENOMEM; goto fail_region; }
/* Matches original probe's conservative initial addressability. A chip/HAL
* must request a proven wider mask before wider DMA addresses are used. */
error = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
if (error) goto fail_mapping;
p->dma_bits = 32;
/* as the original probe (620.73.04 nv-pci.c:1277): DMA segments of a scatterlist may be as long as the mapping needs */
dma_set_max_seg_size(&pdev->dev, U32_MAX);
mutex_init(&p->access);
raw_spin_lock_init(&p->timer_irq);
raw_spin_lock_init(&p->fifo_irq);
raw_spin_lock_init(&p->fifo_context_irq);
raw_spin_lock_init(&p->pmu_mutex_irq);
raw_spin_lock_init(&p->pmu_dmem_irq);
kref_init(&p->refs);
*out = p;
return 0;
fail_mapping:
pci_iounmap(pdev, p->bar0);
fail_region:
pci_release_region(pdev, 0);
fail_enable:
pci_disable_device(pdev);
fail_reference:
pci_dev_put(p->pdev);
kfree(p);
return error;
}
int nv_rs_pci_negotiate_dma(struct nv_rs_pci *p, unsigned int bits)
{
int error;
if (!p || bits < 32 || bits > 64) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (p->dma_users && bits != p->dma_bits) error = -EBUSY;
else {
error = dma_set_mask_and_coherent(&p->pdev->dev, DMA_BIT_MASK(bits));
if (!error) p->dma_bits = bits;
}
mutex_unlock(&p->access);
return error;
}
int nv_rs_pci_dma_acquire(struct nv_rs_pci *p, unsigned int bits)
{
int error = 0;
if (!p || bits < 32 || bits > 64) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (p->dma_users == UINT_MAX) error = -EOVERFLOW;
else if (p->dma_users && p->dma_bits != bits) error = -EBUSY;
else {
if (p->dma_bits != bits)
error = dma_set_mask_and_coherent(&p->pdev->dev, DMA_BIT_MASK(bits));
if (!error) { p->dma_bits = bits; p->dma_users++; }
}
mutex_unlock(&p->access);
return error;
}
/* Raises the device's streaming DMA mask (dma_map_page and friends) while DMA leases exist. The coherent mask and the
* lease width (dma_bits) stay as they are: every DMA context and coherent allocation made under the narrower width,
* and every later 32-bit lease, keep working, and a streaming address handed out under the old mask stays valid under
* the wider one. A GPU's probe keeps a 32-bit lease for its lifetime; with the streaming mask left at 32 bits every
* client page above 4 GiB that the DMA API maps for the GPU is bounced through swiotlb (the GPU's writes never reach
* the page the CPU reads), or behind a translating IOMMU takes its IOVA from the 4 GiB below the mask until that runs
* out. Never lowers the mask. Says so in the kernel log when it raises it, and once per device when it cannot (the
* per-page callers have no way to report it). */
int nv_rs_pci_widen_dma(struct nv_rs_pci *p, unsigned int bits)
{
int error = 0;
if (!p || bits < 32 || bits > 64) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (dma_get_mask(&p->pdev->dev) < DMA_BIT_MASK(bits)) {
error = dma_set_mask(&p->pdev->dev, DMA_BIT_MASK(bits));
if (!error)
dev_info(&p->pdev->dev,
"streaming DMA mask raised to %u bits (DMA leases and the coherent mask stay at %u)\n",
bits, p->dma_bits);
else if (!p->dma_widen_warned) {
p->dma_widen_warned = true;
dev_warn(&p->pdev->dev,
"streaming DMA mask not raised to %u bits (error %d): client pages stay limited to the old mask "
"(swiotlb bounce buffers, or a small IOVA space behind an IOMMU)\n", bits, error);
}
}
mutex_unlock(&p->access);
return error;
}
void nv_rs_pci_dma_release(struct nv_rs_pci *p)
{
if (!p) return;
mutex_lock(&p->access);
if (!WARN_ON_ONCE(!p->dma_users)) p->dma_users--;
mutex_unlock(&p->access);
}
int nv_rs_pci_bus_master(struct nv_rs_pci *p, bool enable)
{
int error = 0;
if (!p) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (enable) pci_set_master(p->pdev);
else pci_clear_master(p->pdev);
mutex_unlock(&p->access);
return error;
}
static int nv_rs_pci_quiesce_dma_locked(struct nv_rs_pci *p)
{
u16 command, status;
if (!pci_is_pcie(p->pdev))
return -ENODEV;
pci_clear_master(p->pdev);
if (pci_read_config_word(p->pdev, PCI_COMMAND, &command) ||
command == U16_MAX || (command & PCI_COMMAND_MASTER))
return -EIO;
if (!pci_wait_for_pending_transaction(p->pdev))
return -ETIMEDOUT;
if (pcie_capability_read_word(p->pdev, PCI_EXP_DEVSTA, &status) ||
status == U16_MAX || (status & PCI_EXP_DEVSTA_TRPND))
return -EIO;
return 0;
}
int nv_rs_pci_quiesce_dma(struct nv_rs_pci *p)
{
int error;
if (!p)
return -EINVAL;
mutex_lock(&p->access);
error = nv_rs_pci_is_removed(p) ? -ENODEV : nv_rs_pci_quiesce_dma_locked(p);
mutex_unlock(&p->access);
return error;
}
void nv_rs_pci_retain_devinit_dma(struct nv_rs_pci *p)
{
/* Called under the same GPU operation lock that serializes removal. */
if (p->devinit_dma_retained)
return;
/* The first-open GPU lease already pins this module. Transfer an extra
* pin to cleanup so rmmod reports busy before module_exit can wait. */
__module_get(THIS_MODULE);
p->devinit_dma_retained = true;
mutex_lock(&retained_lock);
p->next_retained = retained_pci;
retained_pci = p;
mutex_unlock(&retained_lock);
dev_warn(&p->pdev->dev, "Devinit DMA retained; unbind to drain before module unload\n");
}
int nv_rs_pci_drain_removed_dma(struct nv_rs_pci *p)
{
int error = -EBUSY;
if (!READ_ONCE(p->removed))
return -EBUSY;
if (READ_ONCE(p->devinit_dma_drained) || pci_dev_is_disconnected(p->pdev))
return 0;
/* remove() can run under the device lock. Never wait for it here.
* Retry only on the unbound function, excluding probe and removal. */
if (device_trylock(&p->pdev->dev)) {
if (!p->pdev->driver) {
mutex_lock(&p->access);
error = nv_rs_pci_quiesce_dma_locked(p);
if (!error)
WRITE_ONCE(p->devinit_dma_drained, true);
mutex_unlock(&p->access);
}
device_unlock(&p->pdev->dev);
}
return error;
}
int nv_rs_pci_function_present(struct nv_rs_pci *p, u32 domain, u32 bus,
u32 slot, u32 function, bool *present)
{
struct pci_dev *target;
int error = 0;
if (!present) return -EINVAL;
*present = false;
/* Linux takes a signed int domain, an 8-bit bus and PCI_DEVFN(slot,fn).
* Validate before casts/macros: malformed addresses must never alias a BDF. */
if (!p || domain > INT_MAX || bus > 255 || slot > 31 || function > 7)
return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else {
target = pci_get_domain_bus_and_slot((int)domain, bus,
PCI_DEVFN(slot, function));
if (target) {
*present = !pci_dev_is_disconnected(target);
pci_dev_put(target);
}
}
mutex_unlock(&p->access);
return error;
}
int nv_rs_pci_read_config32(struct nv_rs_pci *p, u32 domain, u32 bus,
u32 slot, u32 function, u32 offset, u32 *value)
{
struct pci_dev *target;
int error = 0;
u32 result = 0;
if (!value) return -EINVAL;
*value = 0;
if (!p || domain > INT_MAX || bus > 255 || slot > 31 || function > 7 ||
(offset & 3)) return -EINVAL;
if (offset > 0xffc) return -ERANGE;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) { error = -ENODEV; goto unlock; }
target = pci_get_domain_bus_and_slot((int)domain, bus, PCI_DEVFN(slot, function));
if (!target) { error = -ENOENT; goto unlock; }
if (pci_dev_is_disconnected(target)) error = -ENODEV;
else if (pci_read_config_dword(target, offset, &result) != PCIBIOS_SUCCESSFUL)
error = -EIO;
else *value = result;
pci_dev_put(target);
unlock:
mutex_unlock(&p->access);
return error;
}
static int nv_rs_pci_access(struct nv_rs_pci *p, u64 offset, u32 *value, bool write)
{
int error = 0;
if (!p || !value || (offset & 3)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (p->length < 4 || offset > p->length - 4) error = -ERANGE;
else if (write) writel(*value, p->bar0 + offset);
else *value = readl(p->bar0 + offset);
mutex_unlock(&p->access);
return error;
}
/* Direct bring-up only: map the first `bytes` of the BAR2 aperture (PCI
* resource 3 on these GPUs). Uncached, not write-combined, so no flush is
* needed after a store. The mapping is dropped by unmap or with the lease. */
int nv_rs_pci_bar2_map(struct nv_rs_pci *p, u64 bytes)
{
int error = 0;
if (!p || !bytes || (bytes & 0xfff)) return -EINVAL;
#ifdef NV_RS_PCI_TEST_EDU
return -ENODEV;
#else
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (p->bar2) error = -EBUSY;
else if (!(pci_resource_flags(p->pdev, 3) & IORESOURCE_MEM) ||
!pci_resource_start(p->pdev, 3) || pci_resource_len(p->pdev, 3) < bytes)
error = -ENXIO;
else {
p->bar2 = pci_iomap(p->pdev, 3, bytes);
if (!p->bar2) error = -ENOMEM;
else p->bar2_length = bytes;
}
mutex_unlock(&p->access);
return error;
#endif
}
void nv_rs_pci_bar2_unmap(struct nv_rs_pci *p)
{
if (!p) return;
mutex_lock(&p->access);
if (p->bar2) pci_iounmap(p->pdev, p->bar2);
p->bar2 = NULL;
p->bar2_length = 0;
mutex_unlock(&p->access);
}
static int nv_rs_pci_bar2_access(struct nv_rs_pci *p, u64 offset, u32 *value, bool write)
{
int error = 0;
if (!p || !value || (offset & 3)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (!p->bar2) error = -ENXIO;
else if (p->bar2_length < 4 || offset > p->bar2_length - 4) error = -ERANGE;
else if (write) writel(*value, p->bar2 + offset);
else *value = readl(p->bar2 + offset);
mutex_unlock(&p->access);
return error;
}
int nv_rs_pci_bar2_read32(struct nv_rs_pci *p, u64 offset, u32 *value)
{
return nv_rs_pci_bar2_access(p, offset, value, false);
}
int nv_rs_pci_bar2_write32(struct nv_rs_pci *p, u64 offset, u32 value)
{
return nv_rs_pci_bar2_access(p, offset, &value, true);
}
/* Direct bring-up only: the BAR1 aperture (PCI resource 1 on these GPUs).
* `bytes` reports its PCI size. `map` maps `bytes` of it from `offset`,
* uncached like the BAR2 window; accesses are relative to that window. The
* mapping is dropped by unmap or with the lease. */
int nv_rs_pci_bar1_bytes(struct nv_rs_pci *p, u64 *bytes)
{
if (!p || !bytes) return -EINVAL;
#ifdef NV_RS_PCI_TEST_EDU
return -ENODEV;
#else
if (nv_rs_pci_is_removed(p)) return -ENODEV;
if (!(pci_resource_flags(p->pdev, 1) & IORESOURCE_MEM) || !pci_resource_start(p->pdev, 1))
return -ENXIO;
*bytes = pci_resource_len(p->pdev, 1);
return 0;
#endif
}
int nv_rs_pci_bar1_map(struct nv_rs_pci *p, u64 offset, u64 bytes)
{
int error = 0;
if (!p || !bytes || ((offset | bytes) & 0xfff)) return -EINVAL;
#ifdef NV_RS_PCI_TEST_EDU
return -ENODEV;
#else
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (p->bar1) error = -EBUSY;
else if (!(pci_resource_flags(p->pdev, 1) & IORESOURCE_MEM) ||
!pci_resource_start(p->pdev, 1) || pci_resource_len(p->pdev, 1) < bytes ||
offset > pci_resource_len(p->pdev, 1) - bytes)
error = -ENXIO;
else {
p->bar1 = pci_iomap_range(p->pdev, 1, offset, bytes);
if (!p->bar1) error = -ENOMEM;
else p->bar1_length = bytes;
}
mutex_unlock(&p->access);
return error;
#endif
}
void nv_rs_pci_bar1_unmap(struct nv_rs_pci *p)
{
if (!p) return;
mutex_lock(&p->access);
if (p->bar1) pci_iounmap(p->pdev, p->bar1);
p->bar1 = NULL;
p->bar1_length = 0;
mutex_unlock(&p->access);
}
static int nv_rs_pci_bar1_access(struct nv_rs_pci *p, u64 offset, u32 *value, bool write)
{
int error = 0;
if (!p || !value || (offset & 3)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (!p->bar1) error = -ENXIO;
else if (p->bar1_length < 4 || offset > p->bar1_length - 4) error = -ERANGE;
else if (write) writel(*value, p->bar1 + offset);
else *value = readl(p->bar1 + offset);
mutex_unlock(&p->access);
return error;
}
int nv_rs_pci_bar1_read32(struct nv_rs_pci *p, u64 offset, u32 *value)
{
return nv_rs_pci_bar1_access(p, offset, value, false);
}
int nv_rs_pci_bar1_write32(struct nv_rs_pci *p, u64 offset, u32 value)
{
return nv_rs_pci_bar1_access(p, offset, &value, true);
}
/* Zeroes BAR1 [offset, offset + bytes) through a mapping of its own. The read makes sure the writes have arrived. */
int nv_rs_pci_bar1_zero(struct nv_rs_pci *p, u64 offset, u64 bytes)
{
#if defined(NV_RS_PCI_TEST_EDU) || defined(NV_RS_PCI_HOST_FIXTURE)
if (!p || !bytes || ((offset | bytes) & 0xfff)) return -EINVAL;
return -ENODEV;
#else
void __iomem *va;
if (!p || !bytes || ((offset | bytes) & 0xfff)) return -EINVAL;
if (nv_rs_pci_is_removed(p)) return -ENODEV;
if (!(pci_resource_flags(p->pdev, 1) & IORESOURCE_MEM) ||
!pci_resource_start(p->pdev, 1) || pci_resource_len(p->pdev, 1) < bytes ||
offset > pci_resource_len(p->pdev, 1) - bytes)
return -ENXIO;
va = ioremap_wc(pci_resource_start(p->pdev, 1) + offset, bytes);
if (!va) return -ENOMEM;
memset_io(va, 0, bytes);
nv_rs_pci_context_wc_flush();
(void)readl((char __iomem *)va + bytes - 4);
iounmap(va);
cond_resched();
return 0;
#endif
}
int nv_rs_pci_bar1_wc_map(struct nv_rs_pci *p, u64 offset, u64 bytes)
{
#if defined(NV_RS_PCI_TEST_EDU) || defined(NV_RS_PCI_HOST_FIXTURE)
if (!p || !bytes || ((offset | bytes) & 0xfff)) return -EINVAL;
return -ENODEV;
#else
int error = 0;
if (!p || !bytes || ((offset | bytes) & 0xfff)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p)) error = -ENODEV;
else if (p->bar1_wc) error = -EBUSY;
else if (!(pci_resource_flags(p->pdev, 1) & IORESOURCE_MEM) ||
!pci_resource_start(p->pdev, 1) || pci_resource_len(p->pdev, 1) < bytes ||
offset > pci_resource_len(p->pdev, 1) - bytes)
error = -ENXIO;
else {
p->bar1_wc = ioremap_wc(pci_resource_start(p->pdev, 1) + offset, bytes);
if (!p->bar1_wc) error = -ENOMEM;
else p->bar1_wc_length = bytes;
}
mutex_unlock(&p->access);
return error;
#endif
}
void nv_rs_pci_bar1_wc_unmap(struct nv_rs_pci *p)
{
if (!p) return;
#if !defined(NV_RS_PCI_TEST_EDU) && !defined(NV_RS_PCI_HOST_FIXTURE)
mutex_lock(&p->access);
if (p->bar1_wc) iounmap(p->bar1_wc);
p->bar1_wc = NULL;
p->bar1_wc_length = 0;
mutex_unlock(&p->access);
#endif
}
int nv_rs_pci_bar1_wc_read64(struct nv_rs_pci *p, u64 rel_off, u64 *value)
{
if (!p || !value || (rel_off & 7)) return -EINVAL;
if (nv_rs_pci_is_removed(p) || !p->bar1_wc) return -ENODEV;
if (p->bar1_wc_length < 8 || rel_off > p->bar1_wc_length - 8) return -ERANGE;
{
char __iomem *src = (char __iomem *)p->bar1_wc + rel_off;
u32 lo = readl(src);
u32 hi = readl(src + 4);
*value = (u64)lo | ((u64)hi << 32);
}
return 0;
}
int nv_rs_pci_bar1_wc_write64(struct nv_rs_pci *p, u64 rel_off, u64 value)
{
if (!p || (rel_off & 7)) return -EINVAL;
if (nv_rs_pci_is_removed(p) || !p->bar1_wc) return -ENODEV;
if (p->bar1_wc_length < 8 || rel_off > p->bar1_wc_length - 8) return -ERANGE;
{
char __iomem *dst = (char __iomem *)p->bar1_wc + rel_off;
writel_relaxed((u32)value, dst);
writel_relaxed((u32)(value >> 32), dst + 4);
nv_rs_pci_context_wc_flush();
}
return 0;
}
int nv_rs_pci_bar1_wc_write_words(struct nv_rs_pci *p, u64 rel_off, const u32 *words, u32 count)
{
u32 i;
if (!p || !words || (rel_off & 3)) return -EINVAL;
if (nv_rs_pci_is_removed(p) || !p->bar1_wc) return -ENODEV;
if (rel_off > p->bar1_wc_length || (u64)count * 4 > p->bar1_wc_length - rel_off)
return -ERANGE;
{
char __iomem *dst = (char __iomem *)p->bar1_wc + rel_off;
for (i = 0; i < count; i++)
writel_relaxed(words[i], dst + 4 * i);
nv_rs_pci_context_wc_flush();
}
return 0;
}
int nv_rs_pci_bar1_wc_fill32(struct nv_rs_pci *p, u64 rel_off, u32 value, u32 count)
{
u32 i;
if (!p || (rel_off & 3)) return -EINVAL;
if (nv_rs_pci_is_removed(p) || !p->bar1_wc) return -ENODEV;
if (rel_off > p->bar1_wc_length || (u64)count * 4 > p->bar1_wc_length - rel_off)
return -ERANGE;
{
char __iomem *dst = (char __iomem *)p->bar1_wc + rel_off;
for (i = 0; i < count; i++)
writel_relaxed(value, dst + 4 * i);
nv_rs_pci_context_wc_flush();
}
return 0;
}
static inline void nv_rs_pci_write_pte64(u64 pte, void __iomem *dst)
{
#if !defined(NV_RS_PCI_HOST_FIXTURE) && defined(CONFIG_64BIT)
/* Single atomic 64-bit MMIO store so live MMU PTEs never tear across DWORDS. */
writeq_relaxed(pte, dst);
#else
writel_relaxed((u32)pte, dst);
writel_relaxed((u32)(pte >> 32), (char __iomem *)dst + 4);
#endif
}
int nv_rs_pci_bar1_wc_write_vram_ptes(struct nv_rs_pci *p, u64 rel_off, u64 phys_start,
u64 step_bytes, u64 flags, u32 count)
{
u32 i;
if (!p || (rel_off & 7)) return -EINVAL;
if (nv_rs_pci_is_removed(p) || !p->bar1_wc) return -ENODEV;
if (rel_off > p->bar1_wc_length || (u64)count * 8 > p->bar1_wc_length - rel_off)
return -ERANGE;
{
char __iomem *dst = (char __iomem *)p->bar1_wc + rel_off;
for (i = 0; i < count; i++) {
u64 pa = phys_start + (u64)i * step_bytes;
u64 pte = (pa >> 4) | flags;
nv_rs_pci_write_pte64(pte, dst + 8 * i);
}
nv_rs_pci_context_wc_flush();
}
return 0;
}
int nv_rs_pci_bar1_wc_write_sysmem_ptes(struct nv_rs_pci *p, u64 rel_off, const u64 *dmas,
u64 flags, u32 count)
{
u32 i;
if (!p || !dmas || (rel_off & 7)) return -EINVAL;
if (nv_rs_pci_is_removed(p) || !p->bar1_wc) return -ENODEV;
if (rel_off > p->bar1_wc_length || (u64)count * 8 > p->bar1_wc_length - rel_off)
return -ERANGE;
{
char __iomem *dst = (char __iomem *)p->bar1_wc + rel_off;
for (i = 0; i < count; i++) {
u64 pte = (dmas[i] >> 4) | flags;
nv_rs_pci_write_pte64(pte, dst + 8 * i);
}
nv_rs_pci_context_wc_flush();
}
return 0;
}
int nv_rs_pci_read32(struct nv_rs_pci *p, u64 offset, u32 *value)
{
return nv_rs_pci_access(p, offset, value, false);
}
int nv_rs_pci_write32(struct nv_rs_pci *p, u64 offset, u32 value)
{
return nv_rs_pci_access(p, offset, &value, true);
}
int nv_rs_pci_bar0_fill32(struct nv_rs_pci *p, u64 offset, u32 value, u32 count)
{
u32 i;
int error = 0;
if (!p || (offset & 3)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p) || !p->bar0)
error = -ENODEV;
else if (offset > p->length || (u64)count * 4 > p->length - offset)
error = -ERANGE;
else {
char __iomem *dst = (char __iomem *)p->bar0 + offset;
for (i = 0; i < count; i++)
writel(value, dst + 4 * i);
}
mutex_unlock(&p->access);
return error;
}
int nv_rs_pci_bar0_write_vram_ptes(struct nv_rs_pci *p, u64 offset, u64 phys_start,
u64 step_bytes, u64 flags, u32 count)
{
u32 i;
int error = 0;
if (!p || (offset & 7)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p) || !p->bar0)
error = -ENODEV;
else if (offset > p->length || (u64)count * 8 > p->length - offset)
error = -ERANGE;
else {
char __iomem *dst = (char __iomem *)p->bar0 + offset;
for (i = 0; i < count; i++) {
u64 pa = phys_start + (u64)i * step_bytes;
u64 pte = (pa >> 4) | flags;
nv_rs_pci_write_pte64(pte, dst + 8 * i);
}
wmb();
}
mutex_unlock(&p->access);
return error;
}
int nv_rs_pci_bar0_write_sysmem_ptes(struct nv_rs_pci *p, u64 offset, const u64 *dmas,
u64 flags, u32 count)
{
u32 i;
int error = 0;
if (!p || !dmas || (offset & 7)) return -EINVAL;
mutex_lock(&p->access);
if (nv_rs_pci_is_removed(p) || !p->bar0)
error = -ENODEV;
else if (offset > p->length || (u64)count * 8 > p->length - offset)
error = -ERANGE;
else {
char __iomem *dst = (char __iomem *)p->bar0 + offset;
for (i = 0; i < count; i++) {
u64 pte = (dmas[i] >> 4) | flags;
nv_rs_pci_write_pte64(pte, dst + 8 * i);
}
wmb();
}
mutex_unlock(&p->access);
return error;
}
/* Explicit readback for a caller-selected safe register, never unconditional
* offset-zero reads after writes: register side effects are chip-specific. */
int nv_rs_pci_flush32(struct nv_rs_pci *p, u64 offset, u32 *value)
{
return nv_rs_pci_read32(p, offset, value);
}
/* BAR mappings remain alive under the IRQ owner's lease. Lifecycle contract:
* mask, synchronize and close IRQs before mark_removed/drop. Control register
* RMWs still require chip-specific locking by the HAL. */
static int nv_rs_pci_irq_access(struct nv_rs_pci *p, u64 offset, u32 *value, bool write)
{
if (!p || !value || (offset & 3)) return -EINVAL;
if (nv_rs_pci_is_removed(p)) return -ENODEV;
if (p->length < 4 || offset > p->length - 4) return -ERANGE;
if (write) writel(*value, p->bar0 + offset);
else *value = readl(p->bar0 + offset);
return 0;
}
int nv_rs_pci_read32_irq(struct nv_rs_pci *p, u64 offset, u32 *value)
{ return nv_rs_pci_irq_access(p, offset, value, false); }
int nv_rs_pci_write32_irq(struct nv_rs_pci *p, u64 offset, u32 value)
{ return nv_rs_pci_irq_access(p, offset, &value, true); }
unsigned long nv_rs_pci_timer_irq_lock(struct nv_rs_pci *p)
{
unsigned long flags;
raw_spin_lock_irqsave(&p->timer_irq, flags);
return flags;
}
void nv_rs_pci_timer_irq_unlock(struct nv_rs_pci *p, unsigned long flags)
{
raw_spin_unlock_irqrestore(&p->timer_irq, flags);
}
unsigned long nv_rs_pci_fifo_irq_lock(struct nv_rs_pci *p)
{
unsigned long flags;
raw_spin_lock_irqsave(&p->fifo_irq, flags);
return flags;
}
void nv_rs_pci_fifo_irq_unlock(struct nv_rs_pci *p, unsigned long flags)
{
raw_spin_unlock_irqrestore(&p->fifo_irq, flags);
}
void nv_rs_pci_context_wc_flush(void)
{
#if defined(CONFIG_X86_64) || (defined(NV_RS_PCI_HOST_FIXTURE) && defined(__x86_64__))
__asm__ __volatile__("sfence" ::: "memory");
#elif defined(CONFIG_ARM64) || (defined(NV_RS_PCI_HOST_FIXTURE) && defined(__aarch64__))
__asm__ __volatile__("dsb st" ::: "memory");
#elif defined(NV_RS_PCI_HOST_FIXTURE)
__sync_synchronize();
#else
wmb();
#endif
}
unsigned long nv_rs_pci_context_irq_lock(struct nv_rs_pci *p)
{
unsigned long flags;
raw_spin_lock_irqsave(&p->fifo_context_irq, flags);
return flags;
}
void nv_rs_pci_context_irq_unlock(struct nv_rs_pci *p, unsigned long flags)
{
raw_spin_unlock_irqrestore(&p->fifo_context_irq, flags);
}
unsigned long nv_rs_pci_pmu_mutex_lock(struct nv_rs_pci *p)
{
unsigned long flags;
raw_spin_lock_irqsave(&p->pmu_mutex_irq, flags);
return flags;
}
void nv_rs_pci_pmu_mutex_unlock(struct nv_rs_pci *p, unsigned long flags)
{
raw_spin_unlock_irqrestore(&p->pmu_mutex_irq, flags);
}
unsigned long nv_rs_pci_pmu_dmem_lock(struct nv_rs_pci *p)
{
unsigned long flags;
raw_spin_lock_irqsave(&p->pmu_dmem_irq, flags);
return flags;
}
void nv_rs_pci_pmu_dmem_unlock(struct nv_rs_pci *p, unsigned long flags)
{
raw_spin_unlock_irqrestore(&p->pmu_dmem_irq, flags);
}