Skip to content

Commit

Permalink
drm/etnaviv: rework MMU handling
Browse files Browse the repository at this point in the history
This reworks the MMU handling to make it possible to have multiple MMU contexts.
A context is basically one instance of GPU page tables. Currently we have one
set of page tables per GPU, which isn't all that clever, as it has the
following two consequences:

1. All GPU clients (aka processes) are sharing the same pagetables, which means
there is no isolation between clients, but only between GPU assigned memory
spaces and the rest of the system. Better than nothing, but also not great.

2. Clients operating on the same set of buffers with different etnaviv GPU
cores, e.g. a workload using both the 2D and 3D GPU, need to map the used
buffers into the pagetable sets of each used GPU.

This patch reworks all the MMU handling to introduce the abstraction of the
MMU context. A context can be shared across different GPU cores, as long as
they have compatible MMU implementations, which is the case for all systems
with Vivante GPUs seen in the wild.

As MMUv1 is not able to change pagetables on the fly, without a
"stop the world" operation, which stops GPU, changes pagetables via CPU
interaction, restarts GPU, the implementation introduces a shared context on
MMUv1, which is returned whenever there is a request for a new context.

This patch assigns a MMU context to each GPU, so on MMUv2 systems there is
still one set of pagetables per GPU, but due to the shared context MMUv1
systems see a change in behavior as now a single pagetable set is used
across all GPU cores.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de>
Reviewed-by: Guido Günther <agx@sigxcpu.org>
  • Loading branch information
Lucas Stach committed Aug 15, 2019
1 parent 4900dda commit 27b6727
Show file tree
Hide file tree
Showing 15 changed files with 461 additions and 430 deletions.
10 changes: 5 additions & 5 deletions drivers/gpu/drm/etnaviv/etnaviv_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe
return buffer->user_size / 8;
}

u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu)
u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id)
{
struct etnaviv_cmdbuf *buffer = &gpu->buffer;

Expand All @@ -216,7 +216,7 @@ u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu)
buffer->user_size = 0;

CMD_LOAD_STATE(buffer, VIVS_MMUv2_PTA_CONFIG,
VIVS_MMUv2_PTA_CONFIG_INDEX(0));
VIVS_MMUv2_PTA_CONFIG_INDEX(id));

CMD_END(buffer);

Expand Down Expand Up @@ -315,7 +315,7 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
u32 return_target, return_dwords;
u32 link_target, link_dwords;
bool switch_context = gpu->exec_state != exec_state;
unsigned int new_flush_seq = READ_ONCE(gpu->mmu->flush_seq);
unsigned int new_flush_seq = READ_ONCE(gpu->mmu_context->flush_seq);
bool need_flush = gpu->flush_seq != new_flush_seq;

lockdep_assert_held(&gpu->lock);
Expand All @@ -339,7 +339,7 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,

/* flush command */
if (need_flush) {
if (gpu->mmu->version == ETNAVIV_IOMMU_V1)
if (gpu->mmu_context->global->version == ETNAVIV_IOMMU_V1)
extra_dwords += 1;
else
extra_dwords += 3;
Expand All @@ -353,7 +353,7 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,

if (need_flush) {
/* Add the MMU flush */
if (gpu->mmu->version == ETNAVIV_IOMMU_V1) {
if (gpu->mmu_context->global->version == ETNAVIV_IOMMU_V1) {
CMD_LOAD_STATE(buffer, VIVS_GL_FLUSH_MMU,
VIVS_GL_FLUSH_MMU_FLUSH_FEMMU |
VIVS_GL_FLUSH_MMU_FLUSH_UNK1 |
Expand Down
8 changes: 4 additions & 4 deletions drivers/gpu/drm/etnaviv/etnaviv_cmdbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ etnaviv_cmdbuf_suballoc_new(struct device *dev)
}

int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc,
struct etnaviv_iommu *mmu,
struct etnaviv_iommu_context *context,
struct etnaviv_vram_mapping *mapping,
u32 memory_base)
{
return etnaviv_iommu_get_suballoc_va(mmu, mapping, memory_base,
return etnaviv_iommu_get_suballoc_va(context, mapping, memory_base,
suballoc->paddr, SUBALLOC_SIZE);
}

void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu *mmu,
void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context,
struct etnaviv_vram_mapping *mapping)
{
etnaviv_iommu_put_suballoc_va(mmu, mapping);
etnaviv_iommu_put_suballoc_va(context, mapping);
}

void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
Expand Down
6 changes: 3 additions & 3 deletions drivers/gpu/drm/etnaviv/etnaviv_cmdbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <linux/types.h>

struct device;
struct etnaviv_iommu;
struct etnaviv_iommu_context;
struct etnaviv_vram_mapping;
struct etnaviv_cmdbuf_suballoc;
struct etnaviv_perfmon_request;
Expand All @@ -28,10 +28,10 @@ struct etnaviv_cmdbuf_suballoc *
etnaviv_cmdbuf_suballoc_new(struct device *dev);
void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc);
int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc,
struct etnaviv_iommu *mmu,
struct etnaviv_iommu_context *context,
struct etnaviv_vram_mapping *mapping,
u32 memory_base);
void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu *mmu,
void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context,
struct etnaviv_vram_mapping *mapping);


Expand Down
6 changes: 3 additions & 3 deletions drivers/gpu/drm/etnaviv/etnaviv_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ static int etnaviv_mmu_show(struct etnaviv_gpu *gpu, struct seq_file *m)

seq_printf(m, "Active Objects (%s):\n", dev_name(gpu->dev));

mutex_lock(&gpu->mmu->lock);
drm_mm_print(&gpu->mmu->mm, &p);
mutex_unlock(&gpu->mmu->lock);
mutex_lock(&gpu->mmu_context->lock);
drm_mm_print(&gpu->mmu_context->mm, &p);
mutex_unlock(&gpu->mmu_context->lock);

return 0;
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/gpu/drm/etnaviv/etnaviv_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct etnaviv_gpu;
struct etnaviv_mmu;
struct etnaviv_gem_object;
struct etnaviv_gem_submit;
struct etnaviv_iommu_global;

struct etnaviv_file_private {
/*
Expand All @@ -37,6 +38,7 @@ struct etnaviv_drm_private {
struct etnaviv_gpu *gpu[ETNA_MAX_PIPES];

struct etnaviv_cmdbuf_suballoc *cmdbuf_suballoc;
struct etnaviv_iommu_global *mmu_global;

/* list of GEM objects: */
struct mutex gem_lock;
Expand Down Expand Up @@ -69,7 +71,7 @@ int etnaviv_gem_new_userptr(struct drm_device *dev, struct drm_file *file,
uintptr_t ptr, u32 size, u32 flags, u32 *handle);
u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu);
u16 etnaviv_buffer_config_mmuv2(struct etnaviv_gpu *gpu, u32 mtlb_addr, u32 safe_addr);
u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu);
u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id);
void etnaviv_buffer_end(struct etnaviv_gpu *gpu);
void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
Expand Down
12 changes: 6 additions & 6 deletions drivers/gpu/drm/etnaviv/etnaviv_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static void etnaviv_core_dump_registers(struct core_dump_iterator *iter,
}

static void etnaviv_core_dump_mmu(struct core_dump_iterator *iter,
struct etnaviv_iommu *mmu, size_t mmu_size)
struct etnaviv_iommu_context *mmu, size_t mmu_size)
{
etnaviv_iommu_dump(mmu, iter->data);

Expand Down Expand Up @@ -125,9 +125,9 @@ void etnaviv_core_dump(struct etnaviv_gem_submit *submit)
return;
etnaviv_dump_core = false;

mutex_lock(&gpu->mmu->lock);
mutex_lock(&gpu->mmu_context->lock);

mmu_size = etnaviv_iommu_dump_size(gpu->mmu);
mmu_size = etnaviv_iommu_dump_size(gpu->mmu_context);

/* We always dump registers, mmu, ring, hanging cmdbuf and end marker */
n_obj = 5;
Expand Down Expand Up @@ -157,7 +157,7 @@ void etnaviv_core_dump(struct etnaviv_gem_submit *submit)
iter.start = __vmalloc(file_size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY,
PAGE_KERNEL);
if (!iter.start) {
mutex_unlock(&gpu->mmu->lock);
mutex_unlock(&gpu->mmu_context->lock);
dev_warn(gpu->dev, "failed to allocate devcoredump file\n");
return;
}
Expand All @@ -169,7 +169,7 @@ void etnaviv_core_dump(struct etnaviv_gem_submit *submit)
memset(iter.hdr, 0, iter.data - iter.start);

etnaviv_core_dump_registers(&iter, gpu);
etnaviv_core_dump_mmu(&iter, gpu->mmu, mmu_size);
etnaviv_core_dump_mmu(&iter, gpu->mmu_context, mmu_size);
etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr,
gpu->buffer.size,
etnaviv_cmdbuf_get_va(&gpu->buffer,
Expand Down Expand Up @@ -221,7 +221,7 @@ void etnaviv_core_dump(struct etnaviv_gem_submit *submit)
obj->base.size);
}

mutex_unlock(&gpu->mmu->lock);
mutex_unlock(&gpu->mmu_context->lock);

etnaviv_core_dump_header(&iter, ETDUMP_BUF_END, iter.data);

Expand Down
24 changes: 12 additions & 12 deletions drivers/gpu/drm/etnaviv/etnaviv_gem.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ int etnaviv_gem_mmap_offset(struct drm_gem_object *obj, u64 *offset)

static struct etnaviv_vram_mapping *
etnaviv_gem_get_vram_mapping(struct etnaviv_gem_object *obj,
struct etnaviv_iommu *mmu)
struct etnaviv_iommu_context *context)
{
struct etnaviv_vram_mapping *mapping;

list_for_each_entry(mapping, &obj->vram_list, obj_node) {
if (mapping->mmu == mmu)
if (mapping->context == context)
return mapping;
}

Expand Down Expand Up @@ -256,7 +256,7 @@ struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
int ret = 0;

mutex_lock(&etnaviv_obj->lock);
mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu);
mapping = etnaviv_gem_get_vram_mapping(etnaviv_obj, gpu->mmu_context);
if (mapping) {
/*
* Holding the object lock prevents the use count changing
Expand All @@ -265,12 +265,12 @@ struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
* the MMU owns this mapping to close this race.
*/
if (mapping->use == 0) {
mutex_lock(&gpu->mmu->lock);
if (mapping->mmu == gpu->mmu)
mutex_lock(&gpu->mmu_context->lock);
if (mapping->context == gpu->mmu_context)
mapping->use += 1;
else
mapping = NULL;
mutex_unlock(&gpu->mmu->lock);
mutex_unlock(&gpu->mmu_context->lock);
if (mapping)
goto out;
} else {
Expand Down Expand Up @@ -303,11 +303,11 @@ struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
list_del(&mapping->obj_node);
}

mapping->mmu = gpu->mmu;
mapping->context = gpu->mmu_context;
mapping->use = 1;

ret = etnaviv_iommu_map_gem(gpu->mmu, etnaviv_obj, gpu->memory_base,
mapping);
ret = etnaviv_iommu_map_gem(gpu->mmu_context, etnaviv_obj,
gpu->memory_base, mapping);
if (ret < 0)
kfree(mapping);
else
Expand Down Expand Up @@ -525,12 +525,12 @@ void etnaviv_gem_free_object(struct drm_gem_object *obj)

list_for_each_entry_safe(mapping, tmp, &etnaviv_obj->vram_list,
obj_node) {
struct etnaviv_iommu *mmu = mapping->mmu;
struct etnaviv_iommu_context *context = mapping->context;

WARN_ON(mapping->use);

if (mmu)
etnaviv_iommu_unmap_gem(mmu, mapping);
if (context)
etnaviv_iommu_unmap_gem(context, mapping);

list_del(&mapping->obj_node);
kfree(mapping);
Expand Down
2 changes: 1 addition & 1 deletion drivers/gpu/drm/etnaviv/etnaviv_gem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct etnaviv_vram_mapping {
struct list_head scan_node;
struct list_head mmu_node;
struct etnaviv_gem_object *object;
struct etnaviv_iommu *mmu;
struct etnaviv_iommu_context *context;
struct drm_mm_node vram_node;
unsigned int use;
u32 iova;
Expand Down
31 changes: 20 additions & 11 deletions drivers/gpu/drm/etnaviv/etnaviv_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu)
etnaviv_gpu_setup_pulse_eater(gpu);

/* setup the MMU */
etnaviv_iommu_restore(gpu);
etnaviv_iommu_restore(gpu, gpu->mmu_context);

/* Start command processor */
prefetch = etnaviv_buffer_init(gpu);
Expand Down Expand Up @@ -754,14 +754,19 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
goto fail;
}

gpu->mmu = etnaviv_iommu_new(gpu);
if (IS_ERR(gpu->mmu)) {
dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
ret = PTR_ERR(gpu->mmu);
ret = etnaviv_iommu_global_init(gpu);
if (ret)
goto fail;

gpu->mmu_context = etnaviv_iommu_context_init(priv->mmu_global);
if (IS_ERR(gpu->mmu_context)) {
dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n");
ret = PTR_ERR(gpu->mmu_context);
goto iommu_global_fini;
}

ret = etnaviv_cmdbuf_suballoc_map(priv->cmdbuf_suballoc, gpu->mmu,
ret = etnaviv_cmdbuf_suballoc_map(priv->cmdbuf_suballoc,
gpu->mmu_context,
&gpu->cmdbuf_mapping,
gpu->memory_base);
if (ret) {
Expand All @@ -777,7 +782,7 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
goto unmap_suballoc;
}

if (gpu->mmu->version == ETNAVIV_IOMMU_V1 &&
if (!(gpu->identity.minor_features1 & chipMinorFeatures1_MMU_VERSION) &&
etnaviv_cmdbuf_get_va(&gpu->buffer, &gpu->cmdbuf_mapping) > 0x80000000) {
ret = -EINVAL;
dev_err(gpu->dev,
Expand Down Expand Up @@ -808,9 +813,11 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
free_buffer:
etnaviv_cmdbuf_free(&gpu->buffer);
unmap_suballoc:
etnaviv_cmdbuf_suballoc_unmap(gpu->mmu, &gpu->cmdbuf_mapping);
etnaviv_cmdbuf_suballoc_unmap(gpu->mmu_context, &gpu->cmdbuf_mapping);
destroy_iommu:
etnaviv_iommu_destroy(gpu->mmu);
etnaviv_iommu_context_put(gpu->mmu_context);
iommu_global_fini:
etnaviv_iommu_global_fini(gpu);
fail:
pm_runtime_mark_last_busy(gpu->dev);
pm_runtime_put_autosuspend(gpu->dev);
Expand Down Expand Up @@ -1683,8 +1690,10 @@ static void etnaviv_gpu_unbind(struct device *dev, struct device *master,

if (gpu->initialized) {
etnaviv_cmdbuf_free(&gpu->buffer);
etnaviv_cmdbuf_suballoc_unmap(gpu->mmu, &gpu->cmdbuf_mapping);
etnaviv_iommu_destroy(gpu->mmu);
etnaviv_cmdbuf_suballoc_unmap(gpu->mmu_context,
&gpu->cmdbuf_mapping);
etnaviv_iommu_context_put(gpu->mmu_context);
etnaviv_iommu_global_fini(gpu);
gpu->initialized = false;
}

Expand Down
3 changes: 2 additions & 1 deletion drivers/gpu/drm/etnaviv/etnaviv_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "etnaviv_cmdbuf.h"
#include "etnaviv_gem.h"
#include "etnaviv_mmu.h"
#include "etnaviv_drv.h"

struct etnaviv_gem_submit;
Expand Down Expand Up @@ -136,7 +137,7 @@ struct etnaviv_gpu {
void __iomem *mmio;
int irq;

struct etnaviv_iommu *mmu;
struct etnaviv_iommu_context *mmu_context;
unsigned int flush_seq;

/* Power Control: */
Expand Down
Loading

0 comments on commit 27b6727

Please sign in to comment.