Files
nvdriver/kernel-open/nvidia/dma_adapter.h
BlivionIaG dfda0a29a4 dma: device-owned coherent DMA contexts for GV11B platform GPUs (#606)
Add nv_rs_dma_context_create_dev(struct device *, bits, **) and
LinuxDma::from_device so Tegra194 GV11B platform GPUs, which have no
nv_rs_pci lease, can own a coherent DMA context.

ctx->pci is now optional. Creation, close and force_close share
context_init/context_destroy/context_release_owner: a PCI-owned context
releases its DMA user count and PCI reference exactly as before, a
device-owned context drops its get_device() reference with put_device().
Removal checks go through context_removed(), which consults
nv_rs_pci_is_removed only when ctx->pci is set. The PCI path keeps its
check order and errno values (-EINVAL, -ENODEV, -ERANGE, -ENOMEM).

The dma-mock fixture models get_device/put_device and covers the
device-owned lifecycle, every create failure path, force_close, and
independence from PCI removal state.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 07:20:57 -08:00

73 lines
4.6 KiB
C

/* SPDX-License-Identifier: MIT */
#ifndef NV_RS_DMA_ADAPTER_H
#define NV_RS_DMA_ADAPTER_H
#include <linux/types.h>
struct device;
struct nv_rs_pci;
struct nv_rs_dma_context;
struct nv_rs_dma_backing {
u64 token, cpu_token, bytes, alignment;
u32 coherent, reserved;
};
struct nv_rs_dma_mapping { u64 token, address, bytes; };
/* Caller must own/enabled device and negotiate its DMA masks before creation.
* Context holds a PCI lease; close succeeds only after every allocation
* has been explicitly unmapped and freed. Never call on a foreign-owned GPU.
* The owner serializes close against every context operation; the internal
* mutex serializes slot operations, not raw-pointer context lifetime.
* PCI references do not pin module text; the enclosing GPU/driver owner must
* keep code loaded until the context is closed. */
int nv_rs_dma_context_create(struct nv_rs_pci *, unsigned int dma_bits,
struct nv_rs_dma_context **);
/* Platform GPUs without a PCI lease (Tegra194 GV11B). The caller has already
* negotiated dev's DMA masks; the context holds a get_device() reference that
* close drops with put_device(). There is no removal state: allocation and
* mapping never report -ENODEV, so the owner must close before unbinding. */
int nv_rs_dma_context_create_dev(struct device *dev, unsigned int dma_bits,
struct nv_rs_dma_context **);
int nv_rs_dma_context_close(struct nv_rs_dma_context *);
/* Last resort when close returns -EBUSY: frees every allocation still held,
* mapped or not, then closes. Only for an owner that has established that the
* device can no longer reach system memory (bus mastering off, or removed);
* otherwise the device writes into freed pages. Stores the number freed. */
int nv_rs_dma_context_force_close(struct nv_rs_dma_context *, unsigned int *freed);
/* Allocations the context still holds. */
unsigned int nv_rs_dma_context_live(struct nv_rs_dma_context *);
/* Reports a context whose owner let go of it without closing it. Nothing is
* freed: the memory may still be in use by the device. */
void nv_rs_dma_context_leaked(struct nv_rs_dma_context *);
int nv_rs_dma_allocate(struct nv_rs_dma_context *, u64 bytes, u64 alignment,
struct nv_rs_dma_backing *);
int nv_rs_dma_map(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
struct nv_rs_dma_mapping *);
int nv_rs_dma_sync_device(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
const struct nv_rs_dma_mapping *, u64 offset, u64 bytes);
int nv_rs_dma_sync_cpu(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
const struct nv_rs_dma_mapping *, u64 offset, u64 bytes);
int nv_rs_dma_unmap(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
const struct nv_rs_dma_mapping *);
int nv_rs_dma_release(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *);
/* CPU access to coherent backing. Buffers are validated kernel memory, never
* user pointers. Caller proves GPU quiescence/ownership for the copied range. */
int nv_rs_dma_read(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
u64 offset, void *destination, u64 bytes);
int nv_rs_dma_write(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
u64 offset, const void *source, u64 bytes);
/* Device-shared coherent words: aligned native 32-bit access only, validated
* against both live backing and mapping. Caller owns the device protocol:
* acquire barrier AFTER observing valid, no producer reuse before GET, and
* store barrier BEFORE publishing GET. These functions do not quiesce DMA.
* They sleep on the context mutex and cannot run in hardirq context. */
int nv_rs_dma_read_word(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
const struct nv_rs_dma_mapping *, u64 offset, u32 *value);
int nv_rs_dma_write_word(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
const struct nv_rs_dma_mapping *, u64 offset, u32 value);
/* Returns the direct kernel virtual address and byte size of a mapped coherent
* backing for lockless ring-buffer polling (e.g. GSP CMDQ/MSGQ). Caller must
* clear its cached pointer before unmapping or releasing the backing. */
int nv_rs_dma_cpu_ptr(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *,
const struct nv_rs_dma_mapping *, void **cpu_out, u64 *bytes_out);
/* Full system-memory publication fence; not a GPU cache/TLB invalidation. */
int nv_rs_dma_flush_sysmem(struct nv_rs_dma_context *, const struct nv_rs_dma_backing *);
#endif