[PATCH v4 05/14] accel/thames: Create a context per client on device open

From: Tomeu Vizoso

Date: Sun Nov 29 2026 - 06:37:42 EST


And manage a page table for each, using IOMMU_IO_PGTABLE_LPAE.

The firmware will switch to this page table when executing a user DSP
kernel, in the DSP core's MMU.

Each Context gets an ASID, allocated cyclically so a freed one is not
handed out again at once, and an entry in the IPC Context table with its
page table base and a mapping generation, which the firmware compares
before a Job to invalidate the core's TLB when the mappings changed.
Context serials and generations start from the boot clock, above every
number a previous load of the module handed out, so the firmware never
takes a stale entry for the current one. Every Context maps the three
windows (the L2 main and auxiliary SRAM, and the DRU's Kernel channels
as device memory) and the Kernels' stack and save area at the fixed
user-region addresses. The stack and save area are GEM DMA objects. Only
these three windows, which live as long as the Context, are mapped with
2 MiB blocks; everything else is mapped in 4 KiB pages, so io-pgtable
frees no page table while the Context lives. There are 63 Contexts, the
table's 64 entries less entry 0. A core that attaches after a Context
exists gets the Context's entries written, and must report the first DRU
channel the Contexts map.

The driver depends on 4 KiB host pages: with larger pages the save area
and the stack would overlap.

Signed-off-by: Tomeu Vizoso <tomeu@xxxxxxxxxxxxxxx>

---

v4:
- The Kernels' stack and save area are GEM DMA objects, created through
a gem_create_object hook; the driver's own allocator is gone.
- Only the L2 and DRU windows are mapped with 2 MiB blocks.
- A core that attaches later gets the Context entries written and must
report the same first DRU channel.
- Depend on what IOMMU_IO_PGTABLE_LPAE needs, and on 4 KiB host pages.
---
drivers/accel/thames/Kconfig | 5 +
drivers/accel/thames/Makefile | 3 +-
drivers/accel/thames/thames_core.c | 15 +-
drivers/accel/thames/thames_device.h | 17 ++
drivers/accel/thames/thames_drv.c | 52 ++++
drivers/accel/thames/thames_drv.h | 18 ++
drivers/accel/thames/thames_gem.c | 505 +++++++++++++++++++++++++++++++++++
drivers/accel/thames/thames_gem.h | 45 ++++
8 files changed, 657 insertions(+), 3 deletions(-)

diff --git a/drivers/accel/thames/Kconfig b/drivers/accel/thames/Kconfig
index c621002d8efa..e96a81fdd86f 100644
--- a/drivers/accel/thames/Kconfig
+++ b/drivers/accel/thames/Kconfig
@@ -3,9 +3,14 @@
config DRM_ACCEL_THAMES
tristate "Thames (support for TI C7x DSP accelerators)"
depends on DRM_ACCEL
+ depends on ARM64 || COMPILE_TEST
+ depends on !GENERIC_ATOMIC64 # for IOMMU_IO_PGTABLE_LPAE
+ depends on IOMMU_SUPPORT
+ depends on PAGE_SIZE_4KB || COMPILE_TEST
depends on TI_K3_DSP_REMOTEPROC
depends on REMOTEPROC
depends on MAILBOX
+ select IOMMU_IO_PGTABLE_LPAE
help
Choose this option if you have a Texas Instruments J722S and want to
use its C7x DSP cores as compute accelerators.
diff --git a/drivers/accel/thames/Makefile b/drivers/accel/thames/Makefile
index 175247b31cb2..bb81fcd58927 100644
--- a/drivers/accel/thames/Makefile
+++ b/drivers/accel/thames/Makefile
@@ -4,4 +4,5 @@ obj-$(CONFIG_DRM_ACCEL_THAMES) := thames.o

thames-y := \
thames_core.o \
- thames_drv.o
+ thames_drv.o \
+ thames_gem.o
diff --git a/drivers/accel/thames/thames_core.c b/drivers/accel/thames/thames_core.c
index 8053eeeb3902..d1140a374bd6 100644
--- a/drivers/accel/thames/thames_core.c
+++ b/drivers/accel/thames/thames_core.c
@@ -22,6 +22,7 @@
#include "thames_abi.h"
#include "thames_core.h"
#include "thames_device.h"
+#include "thames_gem.h"

#define THAMES_RESTART_TIMEOUT_MS 5000

@@ -140,8 +141,16 @@ static int thames_core_read_caps(struct thames_core *core, struct thames_handsha
return -EINVAL;
}

- if (tdev->has_caps && memcmp(caps, &tdev->caps, sizeof(*caps))) {
- dev_err(core->dev, "Capabilities or window sizes differ from the first core's\n");
+ /*
+ * A Context maps every core's DRU channels when the Context is
+ * created, from channel 0 on a core that has not attached yet, so a
+ * core attaching later must report the first channel its Contexts map.
+ */
+ if (tdev->has_caps &&
+ (memcmp(caps, &tdev->caps, sizeof(*caps)) ||
+ dru_kernel_channel_first != core->dru_kernel_channel_first)) {
+ dev_err(core->dev,
+ "Capabilities, window sizes or first DRU channel differ from those the device was set up with\n");
return -EINVAL;
}

@@ -410,6 +419,7 @@ static int thames_core_attach(struct thames_core *core)
}

core->ring_tail = head;
+ thames_context_rewrite_entries(core);

core->suspended = false;

@@ -640,6 +650,7 @@ int thames_core_resume(struct thames_core *core)
}
core->ring_tail = head;

+ thames_context_rewrite_entries(core);
goto restart;

offline:
diff --git a/drivers/accel/thames/thames_device.h b/drivers/accel/thames/thames_device.h
index 7388281de055..347654e78e98 100644
--- a/drivers/accel/thames/thames_device.h
+++ b/drivers/accel/thames/thames_device.h
@@ -50,6 +50,23 @@ struct thames_device {
struct thames_caps caps;
bool has_caps;

+ struct ida ctx_ida;
+
+ /*
+ * The firmware keeps the last Context serial and mapping generation it
+ * saw across a reload of this driver, so both count from the boot clock
+ * and no number repeats one an earlier load handed out.
+ */
+ atomic64_t last_context_serial;
+ atomic64_t mapping_generation;
+
+ struct ida asid_ida;
+ u32 asid_next;
+ struct mutex asid_lock;
+
+ struct list_head contexts;
+ struct mutex contexts_lock;
+
};

#define to_thames_device(drm_dev) \
diff --git a/drivers/accel/thames/thames_drv.c b/drivers/accel/thames/thames_drv.c
index c0059df157f1..6add8308fd5c 100644
--- a/drivers/accel/thames/thames_drv.c
+++ b/drivers/accel/thames/thames_drv.c
@@ -22,6 +22,7 @@

#include "thames_core.h"
#include "thames_drv.h"
+#include "thames_gem.h"

static struct platform_device *drm_dev;
static struct thames_device *tdev;
@@ -54,12 +55,19 @@ static int thames_open(struct drm_device *dev, struct drm_file *file)

thames_priv->tdev = tdev;
kref_init(&thames_priv->refcount);
+ mutex_init(&thames_priv->mm_lock);

file->driver_priv = thames_priv;

+ ret = thames_context_create(thames_priv);
+ if (ret)
+ goto err_close_job;
+
drm_dev_exit(idx);
return 0;

+err_close_job:
+ kfree(thames_priv);
err_exit:
drm_dev_exit(idx);
return ret;
@@ -69,6 +77,9 @@ static void thames_file_priv_release(struct kref *kref)
{
struct thames_file_priv *priv = container_of(kref, struct thames_file_priv, refcount);

+ thames_context_destroy(priv);
+ thames_context_va_fini(priv);
+ mutex_destroy(&priv->mm_lock);
kfree(priv);
}

@@ -85,6 +96,9 @@ static void thames_postclose(struct drm_device *dev, struct drm_file *file)
{
struct thames_file_priv *thames_priv = file->driver_priv;

+ dev_dbg(dev->dev, "Context %u: file closed, %u references left\n",
+ thames_priv->context_id, kref_read(&thames_priv->refcount) - 1);
+
thames_file_priv_put(thames_priv);
}

@@ -166,6 +180,7 @@ static const struct drm_driver thames_drm_driver = {
DRIVER_SYNCOBJ_TIMELINE,
.open = thames_open,
.postclose = thames_postclose,
+ .gem_create_object = thames_gem_create_object,
.ioctls = thames_drm_driver_ioctls,
.num_ioctls = ARRAY_SIZE(thames_drm_driver_ioctls),
.fops = &thames_accel_driver_fops,
@@ -175,17 +190,36 @@ static const struct drm_driver thames_drm_driver = {
.minor = THAMES_DRIVER_MINOR,
};

+static void thames_device_release(struct drm_device *ddev, void *data)
+{
+ struct thames_device *tdev = to_thames_device(ddev);
+
+ ida_destroy(&tdev->ctx_ida);
+ ida_destroy(&tdev->asid_ida);
+}
+
static struct thames_device *thames_device_init(struct platform_device *pdev,
const struct drm_driver *thames_drm_driver)
{
struct device *dev = &pdev->dev;
struct thames_device *tdev;
+ u64 seed;
int err;

tdev = devm_drm_dev_alloc(dev, thames_drm_driver, struct thames_device, ddev);
if (IS_ERR(tdev))
return tdev;

+ /*
+ * Live as long as the DRM device: a client's buffers and last Jobs
+ * reach them after an unbind.
+ */
+ ida_init(&tdev->ctx_ida);
+ ida_init(&tdev->asid_ida);
+ err = drmm_add_action_or_reset(&tdev->ddev, thames_device_release, NULL);
+ if (err)
+ return ERR_PTR(err);
+
dev_set_drvdata(dev, tdev);

dma_set_max_seg_size(dev, UINT_MAX);
@@ -198,6 +232,24 @@ static struct thames_device *thames_device_init(struct platform_device *pdev,
if (err)
return ERR_PTR(err);

+ err = drmm_mutex_init(&tdev->ddev, &tdev->asid_lock);
+ if (err)
+ return ERR_PTR(err);
+
+ err = drmm_mutex_init(&tdev->ddev, &tdev->contexts_lock);
+ if (err)
+ return ERR_PTR(err);
+
+ INIT_LIST_HEAD(&tdev->contexts);
+
+ tdev->asid_next = 1;
+ /*
+ * Above every number the previous load handed out, which counted up by
+ * one from an earlier reading of the same clock.
+ */
+ seed = ktime_get_boottime_ns();
+ atomic64_set(&tdev->last_context_serial, seed);
+ atomic64_set(&tdev->mapping_generation, seed);

return tdev;
}
diff --git a/drivers/accel/thames/thames_drv.h b/drivers/accel/thames/thames_drv.h
index 8fab34bc0638..63980d1acefe 100644
--- a/drivers/accel/thames/thames_drv.h
+++ b/drivers/accel/thames/thames_drv.h
@@ -27,6 +27,24 @@ struct thames_file_priv {
*/
struct kref refcount;

+ struct io_pgtable_ops *pgtbl_ops[THAMES_MAX_CORES];
+ u64 page_table_base[THAMES_MAX_CORES];
+ u32 asid;
+
+ struct list_head context_node;
+
+ struct drm_mm mm;
+ struct mutex mm_lock;
+ struct drm_mm_node first_page;
+ struct drm_mm_node user_region;
+ struct drm_mm_node window_nodes[THAMES_NUM_WINDOWS];
+
+ u32 context_id;
+
+ u64 context_serial;
+
+ struct drm_gem_dma_object *user_stack;
+ struct drm_gem_dma_object *save_area;
};

void thames_file_priv_put(struct thames_file_priv *priv);
diff --git a/drivers/accel/thames/thames_gem.c b/drivers/accel/thames/thames_gem.c
new file mode 100644
index 000000000000..e1f36a0daf3b
--- /dev/null
+++ b/drivers/accel/thames/thames_gem.c
@@ -0,0 +1,505 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright 2026 Texas Instruments Incorporated - https://www.ti.com/ */
+
+#include <linux/bits.h>
+#include <linux/dma-mapping.h>
+#include <linux/dma-resv.h>
+#include <linux/idr.h>
+#include <linux/io-pgtable.h>
+#include <linux/iommu.h>
+#include <linux/minmax.h>
+#include <linux/scatterlist.h>
+#include <linux/sizes.h>
+
+#include <drm/drm_device.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_gem_dma_helper.h>
+#include <drm/drm_print.h>
+#include <drm/drm_timeout.h>
+#include <drm/thames_accel.h>
+
+#include "thames_abi.h"
+#include "thames_device.h"
+#include "thames_drv.h"
+#include "thames_gem.h"
+
+static size_t thames_pgsize(u64 addr, size_t size, size_t pgsize_max, size_t *count)
+{
+ size_t blk_offset = -addr % SZ_2M;
+
+ if (pgsize_max < SZ_2M || blk_offset || size < SZ_2M) {
+ *count = min_not_zero(blk_offset, size) / SZ_4K;
+ return SZ_4K;
+ }
+
+ *count = size / SZ_2M;
+ return SZ_2M;
+}
+
+static void thames_pgtable_unmap(struct io_pgtable_ops *ops, u64 dsp_va, u64 size,
+ size_t pgsize_max)
+{
+ u64 offset = 0;
+
+ while (offset < size) {
+ size_t pgcount, unmapped;
+ size_t pgsize = thames_pgsize(dsp_va + offset, size - offset, pgsize_max,
+ &pgcount);
+
+ unmapped = ops->unmap_pages(ops, dsp_va + offset, pgsize, pgcount, NULL);
+ if (WARN_ON(!unmapped))
+ break;
+
+ offset += unmapped;
+ }
+}
+
+static int thames_pgtable_map(struct io_pgtable_ops *ops, u64 dsp_va, int prot,
+ phys_addr_t paddr, size_t size, size_t pgsize_max)
+{
+ size_t offset = 0;
+
+ while (offset < size) {
+ size_t pgcount, mapped = 0;
+ size_t pgsize = thames_pgsize((dsp_va + offset) | (paddr + offset),
+ size - offset, pgsize_max, &pgcount);
+ int ret = ops->map_pages(ops, dsp_va + offset, paddr + offset, pgsize, pgcount,
+ prot, GFP_KERNEL, &mapped);
+
+ if (!ret && !mapped)
+ ret = -ENOMEM;
+ if (ret) {
+ thames_pgtable_unmap(ops, dsp_va, offset, pgsize_max);
+ return ret;
+ }
+
+ offset += mapped;
+ }
+
+ return 0;
+}
+
+/*
+ * The device has no IOMMU, so a DMA address is the physical address a page
+ * table entry holds.
+ */
+static int thames_context_map(struct thames_file_priv *priv, u64 dsp_va, int prot,
+ phys_addr_t paddr, size_t size)
+{
+ unsigned int c;
+ int ret;
+
+ for (c = 0; c < priv->tdev->num_cores; c++) {
+ ret = thames_pgtable_map(priv->pgtbl_ops[c], dsp_va, prot, paddr, size,
+ SZ_4K);
+ if (ret) {
+ while (c--)
+ thames_pgtable_unmap(priv->pgtbl_ops[c], dsp_va, size, SZ_4K);
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+/* Cyclic, so a freed ASID is not handed out again at once. */
+static int thames_asid_alloc(struct thames_device *tdev)
+{
+ u32 max = BIT(tdev->caps.asid_bits) - 1;
+ int asid = -ENOSPC;
+
+ guard(mutex)(&tdev->asid_lock);
+
+ if (tdev->asid_next <= max)
+ asid = ida_alloc_range(&tdev->asid_ida, tdev->asid_next, max, GFP_KERNEL);
+ if (asid == -ENOSPC)
+ asid = ida_alloc_range(&tdev->asid_ida, 1, max, GFP_KERNEL);
+ if (asid < 0)
+ return asid;
+
+ tdev->asid_next = asid + 1;
+
+ return asid;
+}
+
+/*
+ * io-pgtable-arm writes MAIR attribute indexes into the page table entries:
+ * 0 non-cacheable, 1 cacheable, 2 device. The firmware programs its own MAIR
+ * with the same indexes, its cacheable attribute 0x7d where io-pgtable-arm's is
+ * 0xff. The check against this word catches an io-pgtable-arm that moves an
+ * index.
+ */
+#define THAMES_PGTABLE_MAIR_LOW 0xf404ff44u
+
+static void thames_context_pgtable_fini(struct thames_file_priv *priv)
+{
+ unsigned int c;
+
+ for (c = 0; c < THAMES_MAX_CORES; c++) {
+ if (priv->pgtbl_ops[c])
+ free_io_pgtable_ops(priv->pgtbl_ops[c]);
+ priv->pgtbl_ops[c] = NULL;
+ }
+}
+
+/*
+ * Also for offline cores, so a core that attaches later finds a page table for
+ * every Context already open.
+ */
+static int thames_context_pgtable_init(struct thames_file_priv *priv)
+{
+ struct thames_device *tdev = priv->tdev;
+ unsigned int c;
+
+ for (c = 0; c < tdev->num_cores; c++) {
+ struct io_pgtable_cfg cfg = {
+ .pgsize_bitmap = SZ_4K | SZ_2M,
+ .ias = 48,
+ .oas = 40,
+ .coherent_walk = false,
+ .iommu_dev = tdev->ddev.dev,
+ };
+ struct io_pgtable *iop;
+
+ priv->pgtbl_ops[c] = alloc_io_pgtable_ops(ARM_64_LPAE_S1, &cfg, priv);
+ if (!priv->pgtbl_ops[c])
+ return -ENOMEM;
+
+ iop = io_pgtable_ops_to_pgtable(priv->pgtbl_ops[c]);
+ if (WARN_ON((iop->cfg.arm_lpae_s1_cfg.mair & 0xffffffffU) !=
+ THAMES_PGTABLE_MAIR_LOW))
+ return -EINVAL;
+
+ priv->page_table_base[c] = iop->cfg.arm_lpae_s1_cfg.ttbr;
+ }
+
+ return 0;
+}
+
+static struct thames_context_entry *thames_context_entry(struct thames_core *core,
+ struct thames_file_priv *priv)
+{
+ return &core->ipc->context_table[priv->context_id];
+}
+
+/*
+ * Every core set up, offline ones included: a core taken offline after a
+ * timeout may still run the Jobs on its Ring, which read these entries.
+ */
+#define for_each_core(tdev, core) \
+ for ((core) = (tdev)->cores; (core) < (tdev)->cores + (tdev)->num_cores; (core)++)
+
+static int thames_context_map_core_regions(struct thames_file_priv *priv)
+{
+ struct thames_device *tdev = priv->tdev;
+ unsigned int c, i;
+ int ret;
+
+ for (c = 0; c < tdev->num_cores; c++) {
+ const struct thames_core *core = &tdev->cores[c];
+
+ for (i = 0; i < THAMES_NUM_WINDOWS; i++) {
+ const struct resource *window = thames_core_window(core, i);
+ int prot = IOMMU_READ | IOMMU_WRITE | IOMMU_NOEXEC;
+ phys_addr_t paddr = window->start;
+ u64 size = resource_size(window);
+
+ if (i == THAMES_WINDOW_DRU) {
+ prot |= IOMMU_MMIO;
+ paddr += (u64)core->dru_kernel_channel_first *
+ tdev->caps.dru_channel_stride;
+ size = tdev->caps.window_sizes[THAMES_WINDOW_DRU];
+ } else {
+ prot |= IOMMU_CACHE;
+ }
+
+ ret = thames_pgtable_map(priv->pgtbl_ops[c], thames_window_va(tdev, i),
+ prot, paddr, size, SZ_2M);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static const struct drm_gem_object_funcs thames_gem_funcs;
+
+struct drm_gem_object *thames_gem_create_object(struct drm_device *ddev, size_t size)
+{
+ struct thames_gem_object *bo;
+
+ bo = kzalloc_obj(*bo, GFP_KERNEL);
+ if (!bo)
+ return ERR_PTR(-ENOMEM);
+
+ bo->base.base.funcs = &thames_gem_funcs;
+
+ return &bo->base.base;
+}
+
+static struct drm_gem_dma_object *thames_context_alloc_user_pages(struct thames_file_priv *priv,
+ u64 dsp_va, size_t size)
+{
+ int prot = IOMMU_READ | IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_CACHE;
+ struct drm_gem_dma_object *dma_obj;
+ int ret;
+
+ dma_obj = drm_gem_dma_create(&priv->tdev->ddev, size);
+ if (IS_ERR(dma_obj))
+ return dma_obj;
+
+ ret = thames_context_map(priv, dsp_va, prot, dma_obj->dma_addr, dma_obj->base.size);
+ if (ret) {
+ drm_gem_object_put(&dma_obj->base);
+ return ERR_PTR(ret);
+ }
+
+ return dma_obj;
+}
+
+static int thames_context_map_user_pages(struct thames_file_priv *priv)
+{
+ struct drm_gem_dma_object *dma_obj;
+
+ dma_obj = thames_context_alloc_user_pages(priv, THAMES_USER_STACK_BASE,
+ THAMES_USER_STACK_SIZE);
+ if (IS_ERR(dma_obj))
+ return PTR_ERR(dma_obj);
+ priv->user_stack = dma_obj;
+
+ dma_obj = thames_context_alloc_user_pages(priv, THAMES_USER_SAVE_AREA_BASE,
+ THAMES_USER_SAVE_AREA_SIZE);
+ if (IS_ERR(dma_obj))
+ return PTR_ERR(dma_obj);
+ priv->save_area = dma_obj;
+
+ return 0;
+}
+
+/* Only after the page tables are freed, so none maps a page given back. */
+static void thames_context_free_user_pages(struct thames_file_priv *priv)
+{
+ if (priv->save_area)
+ drm_gem_object_put(&priv->save_area->base);
+ if (priv->user_stack)
+ drm_gem_object_put(&priv->user_stack->base);
+ priv->save_area = NULL;
+ priv->user_stack = NULL;
+}
+
+/* Every core's entry: a removed mapping may still sit in either core's TLB. */
+static void thames_context_bump_generation(struct thames_file_priv *priv)
+{
+ u64 generation = atomic64_inc_return(&priv->tdev->mapping_generation);
+ struct thames_core *core;
+ int idx;
+
+ if (!drm_dev_enter(&priv->tdev->ddev, &idx))
+ return;
+
+ for_each_core(priv->tdev, core)
+ WRITE_ONCE(thames_context_entry(core, priv)->mapping_generation, generation);
+ /* Order the generation before the next submission. */
+ wmb();
+
+ drm_dev_exit(idx);
+}
+
+static void thames_context_write_entry(struct thames_file_priv *priv)
+{
+ struct thames_core *core;
+
+ for_each_core(priv->tdev, core) {
+ struct thames_context_entry *entry = thames_context_entry(core, priv);
+
+ entry->page_table_base = priv->page_table_base[core->index];
+ entry->asid = priv->asid;
+ }
+ thames_context_bump_generation(priv);
+}
+
+/*
+ * A new generation, so the restarted firmware invalidates its TLB whatever it
+ * remembers.
+ */
+void thames_context_rewrite_entries(struct thames_core *core)
+{
+ struct thames_device *tdev = core->tdev;
+ struct thames_file_priv *priv;
+
+ guard(mutex)(&tdev->contexts_lock);
+
+ list_for_each_entry(priv, &tdev->contexts, context_node) {
+ struct thames_context_entry *entry = thames_context_entry(core, priv);
+
+ entry->page_table_base = priv->page_table_base[core->index];
+ entry->asid = priv->asid;
+ thames_context_bump_generation(priv);
+ }
+}
+
+/* Offline cores too: their entry would otherwise name a freed page table. */
+static void thames_context_clear_entry(struct thames_file_priv *priv)
+{
+ struct thames_core *core;
+ int idx;
+
+ if (!drm_dev_enter(&priv->tdev->ddev, &idx))
+ return;
+
+ for_each_core(priv->tdev, core)
+ memset(thames_context_entry(core, priv), 0, sizeof(struct thames_context_entry));
+ /* Order the cleared entry before the id is freed. */
+ wmb();
+
+ drm_dev_exit(idx);
+}
+
+#define THAMES_DSP_VA_SIZE (1ULL << 47)
+
+static int thames_context_reserve(struct thames_file_priv *priv, struct drm_mm_node *node,
+ u64 start, u64 size)
+{
+ node->start = start;
+ node->size = size;
+
+ return drm_mm_reserve_node(&priv->mm, node);
+}
+
+void thames_context_va_fini(struct thames_file_priv *priv)
+{
+ unsigned int i;
+
+ for (i = 0; i < THAMES_NUM_WINDOWS; i++) {
+ if (drm_mm_node_allocated(&priv->window_nodes[i]))
+ drm_mm_remove_node(&priv->window_nodes[i]);
+ }
+ if (drm_mm_node_allocated(&priv->user_region))
+ drm_mm_remove_node(&priv->user_region);
+ if (drm_mm_node_allocated(&priv->first_page))
+ drm_mm_remove_node(&priv->first_page);
+
+ drm_mm_takedown(&priv->mm);
+}
+
+static int thames_context_va_init(struct thames_file_priv *priv)
+{
+ struct thames_device *tdev = priv->tdev;
+ unsigned int i;
+ int ret;
+
+ drm_mm_init(&priv->mm, 0, THAMES_DSP_VA_SIZE);
+
+ ret = thames_context_reserve(priv, &priv->first_page, 0, PAGE_SIZE);
+ if (ret)
+ goto err_fini;
+
+ ret = thames_context_reserve(priv, &priv->user_region, THAMES_USER_REGION_BASE,
+ THAMES_USER_REGION_SIZE);
+ if (ret)
+ goto err_fini;
+
+ for (i = 0; i < THAMES_NUM_WINDOWS; i++) {
+ ret = thames_context_reserve(priv, &priv->window_nodes[i],
+ thames_window_va(tdev, i),
+ resource_size(thames_core_window(&tdev->cores[0], i)));
+ if (ret)
+ goto err_fini;
+ }
+
+ return 0;
+
+err_fini:
+ thames_context_va_fini(priv);
+ return ret;
+}
+
+int thames_context_create(struct thames_file_priv *priv)
+{
+ struct thames_device *tdev = priv->tdev;
+ int ret, asid;
+
+ ret = ida_alloc_range(&tdev->ctx_ida, 1, THAMES_MAX_CONTEXTS - 1, GFP_KERNEL);
+ if (ret < 0)
+ return ret;
+
+ priv->context_id = ret;
+ priv->context_serial = atomic64_inc_return(&tdev->last_context_serial);
+
+ ret = thames_context_va_init(priv);
+ if (ret)
+ goto err_free_id;
+
+ ret = thames_context_pgtable_init(priv);
+ if (ret)
+ goto err_free_pgtable;
+
+ ret = thames_context_map_core_regions(priv);
+ if (ret)
+ goto err_free_pgtable;
+
+ ret = thames_context_map_user_pages(priv);
+ if (ret)
+ goto err_free_pgtable;
+
+ asid = thames_asid_alloc(tdev);
+ if (asid < 0) {
+ ret = asid;
+ goto err_free_pgtable;
+ }
+ priv->asid = asid;
+ dev_dbg(tdev->ddev.dev, "Context %u: ASID %d\n", priv->context_id, asid);
+
+ thames_context_write_entry(priv);
+
+ scoped_guard(mutex, &tdev->contexts_lock)
+ list_add_tail(&priv->context_node, &tdev->contexts);
+
+ return 0;
+
+err_free_pgtable:
+ thames_context_pgtable_fini(priv);
+ thames_context_free_user_pages(priv);
+ thames_context_va_fini(priv);
+err_free_id:
+ ida_free(&tdev->ctx_ida, priv->context_id);
+ return ret;
+}
+
+void thames_context_destroy(struct thames_file_priv *priv)
+{
+ struct thames_device *tdev = priv->tdev;
+
+ /*
+ * Each submitted Job holds a reference on the Context until the Job is
+ * cleaned up, so no Job can run on the mappings removed here.
+ */
+ scoped_guard(mutex, &tdev->contexts_lock)
+ list_del(&priv->context_node);
+
+ thames_context_clear_entry(priv);
+
+ thames_context_pgtable_fini(priv);
+ thames_context_free_user_pages(priv);
+ ida_free(&tdev->asid_ida, priv->asid);
+ ida_free(&tdev->ctx_ida, priv->context_id);
+
+ dev_dbg(tdev->ddev.dev, "Context %u: destroyed, ASID %u back in the pool\n",
+ priv->context_id, priv->asid);
+}
+
+static void thames_gem_bo_free(struct drm_gem_object *obj)
+{
+ drm_gem_dma_free(to_drm_gem_dma_obj(obj));
+}
+
+static const struct drm_gem_object_funcs thames_gem_funcs = {
+ .free = thames_gem_bo_free,
+ .print_info = drm_gem_dma_object_print_info,
+ .get_sg_table = drm_gem_dma_object_get_sg_table,
+ .vmap = drm_gem_dma_object_vmap,
+ .mmap = drm_gem_dma_object_mmap,
+ .vm_ops = &drm_gem_dma_vm_ops,
+};
+
diff --git a/drivers/accel/thames/thames_gem.h b/drivers/accel/thames/thames_gem.h
new file mode 100644
index 000000000000..dc4f6fd0d8af
--- /dev/null
+++ b/drivers/accel/thames/thames_gem.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright 2026 Texas Instruments Incorporated - https://www.ti.com/ */
+
+#ifndef __THAMES_GEM_H__
+#define __THAMES_GEM_H__
+
+#include <linux/sizes.h>
+
+#include <drm/drm_gem_dma_helper.h>
+#include <drm/drm_gem_shmem_helper.h>
+#include <drm/drm_mm.h>
+
+#include "thames_abi.h"
+
+struct thames_core;
+struct thames_device;
+struct thames_file_priv;
+
+struct thames_gem_object {
+ /*
+ * Both start with their drm_gem_object, so base.base is the GEM object
+ * either way.
+ */
+ union {
+ struct drm_gem_dma_object base;
+ struct drm_gem_shmem_object shmem;
+ };
+};
+
+
+struct drm_gem_object *thames_gem_create_object(struct drm_device *ddev, size_t size);
+
+int thames_context_create(struct thames_file_priv *priv);
+
+void thames_context_destroy(struct thames_file_priv *priv);
+void thames_context_rewrite_entries(struct thames_core *core);
+
+void thames_context_va_fini(struct thames_file_priv *priv);
+
+static inline struct thames_gem_object *to_thames_bo(struct drm_gem_object *obj)
+{
+ return container_of(obj, struct thames_gem_object, base.base);
+}
+
+#endif

--
2.54.0