Skip to content

Commit 17e4660

Browse files
committed
drm/etnaviv: implement per-process address spaces on MMUv2
This builds on top of the MMU contexts introduced earlier. Instead of having one context per GPU core, each GPU client receives its own context. On MMUv1 this still means a single shared pagetable set is used by all clients, but on MMUv2 there is now a distinct set of pagetables for each client. As the command fetch is also translated via the MMU on MMUv2 the kernel command ringbuffer is mapped into each of the client pagetables. As the MMU context switch is a bit of a heavy operation, due to the needed cache and TLB flushing, this patch implements a lazy way of switching the MMU context. The kernel does not have its own MMU context, but reuses the last client context for all of its operations. This has some visible impact, as the GPU can now only be started once a client has submitted some work and we got the client MMU context assigned. Also the MMU context has a different lifetime than the general client context, as the GPU might still execute the kernel command buffer in the context of a client even after the client has completed all GPU work and has been terminated. Only when the GPU is runtime suspended or switches to another clients MMU context is the old context freed up. 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>
1 parent e6364d7 commit 17e4660

13 files changed

Lines changed: 209 additions & 116 deletions

‎drivers/gpu/drm/etnaviv/etnaviv_buffer.c‎

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ static void etnaviv_buffer_dump(struct etnaviv_gpu *gpu,
118118
u32 *ptr = buf->vaddr + off;
119119

120120
dev_info(gpu->dev, "virt %p phys 0x%08x free 0x%08x\n",
121-
ptr, etnaviv_cmdbuf_get_va(buf, &gpu->cmdbuf_mapping) +
121+
ptr, etnaviv_cmdbuf_get_va(buf,
122+
&gpu->mmu_context->cmdbuf_mapping) +
122123
off, size - len * 4 - off);
123124

124125
print_hex_dump(KERN_INFO, "cmd ", DUMP_PREFIX_OFFSET, 16, 4,
@@ -152,7 +153,8 @@ static u32 etnaviv_buffer_reserve(struct etnaviv_gpu *gpu,
152153
if (buffer->user_size + cmd_dwords * sizeof(u64) > buffer->size)
153154
buffer->user_size = 0;
154155

155-
return etnaviv_cmdbuf_get_va(buffer, &gpu->cmdbuf_mapping) +
156+
return etnaviv_cmdbuf_get_va(buffer,
157+
&gpu->mmu_context->cmdbuf_mapping) +
156158
buffer->user_size;
157159
}
158160

@@ -166,7 +168,8 @@ u16 etnaviv_buffer_init(struct etnaviv_gpu *gpu)
166168
buffer->user_size = 0;
167169

168170
CMD_WAIT(buffer);
169-
CMD_LINK(buffer, 2, etnaviv_cmdbuf_get_va(buffer, &gpu->cmdbuf_mapping)
171+
CMD_LINK(buffer, 2,
172+
etnaviv_cmdbuf_get_va(buffer, &gpu->mmu_context->cmdbuf_mapping)
170173
+ buffer->user_size - 4);
171174

172175
return buffer->user_size / 8;
@@ -293,7 +296,8 @@ void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event)
293296

294297
/* Append waitlink */
295298
CMD_WAIT(buffer);
296-
CMD_LINK(buffer, 2, etnaviv_cmdbuf_get_va(buffer, &gpu->cmdbuf_mapping)
299+
CMD_LINK(buffer, 2,
300+
etnaviv_cmdbuf_get_va(buffer, &gpu->mmu_context->cmdbuf_mapping)
297301
+ buffer->user_size - 4);
298302

299303
/*
@@ -308,26 +312,29 @@ void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event)
308312

309313
/* Append a command buffer to the ring buffer. */
310314
void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
311-
unsigned int event, struct etnaviv_cmdbuf *cmdbuf)
315+
struct etnaviv_iommu_context *mmu_context, unsigned int event,
316+
struct etnaviv_cmdbuf *cmdbuf)
312317
{
313318
struct etnaviv_cmdbuf *buffer = &gpu->buffer;
314319
unsigned int waitlink_offset = buffer->user_size - 16;
315320
u32 return_target, return_dwords;
316321
u32 link_target, link_dwords;
317322
bool switch_context = gpu->exec_state != exec_state;
323+
bool switch_mmu_context = gpu->mmu_context != mmu_context;
318324
unsigned int new_flush_seq = READ_ONCE(gpu->mmu_context->flush_seq);
319-
bool need_flush = gpu->flush_seq != new_flush_seq;
325+
bool need_flush = switch_mmu_context || gpu->flush_seq != new_flush_seq;
320326

321327
lockdep_assert_held(&gpu->lock);
322328

323329
if (drm_debug & DRM_UT_DRIVER)
324330
etnaviv_buffer_dump(gpu, buffer, 0, 0x50);
325331

326-
link_target = etnaviv_cmdbuf_get_va(cmdbuf, &gpu->cmdbuf_mapping);
332+
link_target = etnaviv_cmdbuf_get_va(cmdbuf,
333+
&gpu->mmu_context->cmdbuf_mapping);
327334
link_dwords = cmdbuf->size / 8;
328335

329336
/*
330-
* If we need maintanence prior to submitting this buffer, we will
337+
* If we need maintenance prior to submitting this buffer, we will
331338
* need to append a mmu flush load state, followed by a new
332339
* link to this buffer - a total of four additional words.
333340
*/
@@ -349,7 +356,24 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
349356
if (switch_context)
350357
extra_dwords += 4;
351358

359+
/* PTA load command */
360+
if (switch_mmu_context && gpu->sec_mode == ETNA_SEC_KERNEL)
361+
extra_dwords += 1;
362+
352363
target = etnaviv_buffer_reserve(gpu, buffer, extra_dwords);
364+
/*
365+
* Switch MMU context if necessary. Must be done after the
366+
* link target has been calculated, as the jump forward in the
367+
* kernel ring still uses the last active MMU context before
368+
* the switch.
369+
*/
370+
if (switch_mmu_context) {
371+
struct etnaviv_iommu_context *old_context = gpu->mmu_context;
372+
373+
etnaviv_iommu_context_get(mmu_context);
374+
gpu->mmu_context = mmu_context;
375+
etnaviv_iommu_context_put(old_context);
376+
}
353377

354378
if (need_flush) {
355379
/* Add the MMU flush */
@@ -361,10 +385,23 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
361385
VIVS_GL_FLUSH_MMU_FLUSH_PEMMU |
362386
VIVS_GL_FLUSH_MMU_FLUSH_UNK4);
363387
} else {
388+
u32 flush = VIVS_MMUv2_CONFIGURATION_MODE_MASK |
389+
VIVS_MMUv2_CONFIGURATION_FLUSH_FLUSH;
390+
391+
if (switch_mmu_context &&
392+
gpu->sec_mode == ETNA_SEC_KERNEL) {
393+
unsigned short id =
394+
etnaviv_iommuv2_get_pta_id(gpu->mmu_context);
395+
CMD_LOAD_STATE(buffer,
396+
VIVS_MMUv2_PTA_CONFIG,
397+
VIVS_MMUv2_PTA_CONFIG_INDEX(id));
398+
}
399+
400+
if (gpu->sec_mode == ETNA_SEC_NONE)
401+
flush |= etnaviv_iommuv2_get_mtlb_addr(gpu->mmu_context);
402+
364403
CMD_LOAD_STATE(buffer, VIVS_MMUv2_CONFIGURATION,
365-
VIVS_MMUv2_CONFIGURATION_MODE_MASK |
366-
VIVS_MMUv2_CONFIGURATION_ADDRESS_MASK |
367-
VIVS_MMUv2_CONFIGURATION_FLUSH_FLUSH);
404+
flush);
368405
CMD_SEM(buffer, SYNC_RECIPIENT_FE,
369406
SYNC_RECIPIENT_PE);
370407
CMD_STALL(buffer, SYNC_RECIPIENT_FE,
@@ -380,6 +417,8 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
380417
}
381418

382419
/* And the link to the submitted buffer */
420+
link_target = etnaviv_cmdbuf_get_va(cmdbuf,
421+
&gpu->mmu_context->cmdbuf_mapping);
383422
CMD_LINK(buffer, link_dwords, link_target);
384423

385424
/* Update the link target to point to above instructions */
@@ -416,13 +455,14 @@ void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
416455
CMD_LOAD_STATE(buffer, VIVS_GL_EVENT, VIVS_GL_EVENT_EVENT_ID(event) |
417456
VIVS_GL_EVENT_FROM_PE);
418457
CMD_WAIT(buffer);
419-
CMD_LINK(buffer, 2, etnaviv_cmdbuf_get_va(buffer, &gpu->cmdbuf_mapping)
458+
CMD_LINK(buffer, 2,
459+
etnaviv_cmdbuf_get_va(buffer, &gpu->mmu_context->cmdbuf_mapping)
420460
+ buffer->user_size - 4);
421461

422462
if (drm_debug & DRM_UT_DRIVER)
423463
pr_info("stream link to 0x%08x @ 0x%08x %p\n",
424464
return_target,
425-
etnaviv_cmdbuf_get_va(cmdbuf, &gpu->cmdbuf_mapping),
465+
etnaviv_cmdbuf_get_va(cmdbuf, &gpu->mmu_context->cmdbuf_mapping),
426466
cmdbuf->vaddr);
427467

428468
if (drm_debug & DRM_UT_DRIVER) {

‎drivers/gpu/drm/etnaviv/etnaviv_drv.c‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,19 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
5050
{
5151
struct etnaviv_drm_private *priv = dev->dev_private;
5252
struct etnaviv_file_private *ctx;
53-
int i;
53+
int ret, i;
5454

5555
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
5656
if (!ctx)
5757
return -ENOMEM;
5858

59+
ctx->mmu = etnaviv_iommu_context_init(priv->mmu_global,
60+
priv->cmdbuf_suballoc);
61+
if (!ctx->mmu) {
62+
ret = -ENOMEM;
63+
goto out_free;
64+
}
65+
5966
for (i = 0; i < ETNA_MAX_PIPES; i++) {
6067
struct etnaviv_gpu *gpu = priv->gpu[i];
6168
struct drm_sched_rq *rq;
@@ -70,6 +77,10 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
7077
file->driver_priv = ctx;
7178

7279
return 0;
80+
81+
out_free:
82+
kfree(ctx);
83+
return ret;
7384
}
7485

7586
static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
@@ -85,6 +96,8 @@ static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
8596
drm_sched_entity_destroy(&ctx->sched_entity[i]);
8697
}
8798

99+
etnaviv_iommu_context_put(ctx->mmu);
100+
88101
kfree(ctx);
89102
}
90103

@@ -116,12 +129,29 @@ static int etnaviv_mm_show(struct drm_device *dev, struct seq_file *m)
116129
static int etnaviv_mmu_show(struct etnaviv_gpu *gpu, struct seq_file *m)
117130
{
118131
struct drm_printer p = drm_seq_file_printer(m);
132+
struct etnaviv_iommu_context *mmu_context;
119133

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

122-
mutex_lock(&gpu->mmu_context->lock);
123-
drm_mm_print(&gpu->mmu_context->mm, &p);
124-
mutex_unlock(&gpu->mmu_context->lock);
136+
/*
137+
* Lock the GPU to avoid a MMU context switch just now and elevate
138+
* the refcount of the current context to avoid it disappearing from
139+
* under our feet.
140+
*/
141+
mutex_lock(&gpu->lock);
142+
mmu_context = gpu->mmu_context;
143+
if (mmu_context)
144+
etnaviv_iommu_context_get(mmu_context);
145+
mutex_unlock(&gpu->lock);
146+
147+
if (!mmu_context)
148+
return 0;
149+
150+
mutex_lock(&mmu_context->lock);
151+
drm_mm_print(&mmu_context->mm, &p);
152+
mutex_unlock(&mmu_context->lock);
153+
154+
etnaviv_iommu_context_put(mmu_context);
125155

126156
return 0;
127157
}

‎drivers/gpu/drm/etnaviv/etnaviv_drv.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ struct etnaviv_gem_submit;
2525
struct etnaviv_iommu_global;
2626

2727
struct etnaviv_file_private {
28-
/*
29-
* When per-context address spaces are supported we'd keep track of
30-
* the context's page-tables here.
31-
*/
28+
struct etnaviv_iommu_context *mmu;
3229
struct drm_sched_entity sched_entity[ETNA_MAX_PIPES];
3330
};
3431

@@ -75,6 +72,7 @@ u16 etnaviv_buffer_config_pta(struct etnaviv_gpu *gpu, unsigned short id);
7572
void etnaviv_buffer_end(struct etnaviv_gpu *gpu);
7673
void etnaviv_sync_point_queue(struct etnaviv_gpu *gpu, unsigned int event);
7774
void etnaviv_buffer_queue(struct etnaviv_gpu *gpu, u32 exec_state,
75+
struct etnaviv_iommu_context *mmu,
7876
unsigned int event, struct etnaviv_cmdbuf *cmdbuf);
7977
void etnaviv_validate_init(void);
8078
bool etnaviv_cmd_validate_one(struct etnaviv_gpu *gpu,

‎drivers/gpu/drm/etnaviv/etnaviv_dump.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ void etnaviv_core_dump(struct etnaviv_gem_submit *submit)
173173
etnaviv_core_dump_mem(&iter, ETDUMP_BUF_RING, gpu->buffer.vaddr,
174174
gpu->buffer.size,
175175
etnaviv_cmdbuf_get_va(&gpu->buffer,
176-
&gpu->cmdbuf_mapping));
176+
&gpu->mmu_context->cmdbuf_mapping));
177177

178178
etnaviv_core_dump_mem(&iter, ETDUMP_BUF_CMD,
179179
submit->cmdbuf.vaddr, submit->cmdbuf.size,
180180
etnaviv_cmdbuf_get_va(&submit->cmdbuf,
181-
&gpu->cmdbuf_mapping));
181+
&gpu->mmu_context->cmdbuf_mapping));
182182

183183
/* Reserve space for the bomap */
184184
if (n_bomap_pages) {

‎drivers/gpu/drm/etnaviv/etnaviv_gem.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping)
248248
}
249249

250250
struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
251-
struct drm_gem_object *obj, struct etnaviv_gpu *gpu,
252-
struct etnaviv_iommu_context *mmu_context)
251+
struct drm_gem_object *obj, struct etnaviv_iommu_context *mmu_context)
253252
{
254253
struct etnaviv_gem_object *etnaviv_obj = to_etnaviv_bo(obj);
255254
struct etnaviv_vram_mapping *mapping;
@@ -308,8 +307,8 @@ struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
308307
mapping->context = mmu_context;
309308
mapping->use = 1;
310309

311-
ret = etnaviv_iommu_map_gem(mmu_context, etnaviv_obj, gpu->memory_base,
312-
mapping);
310+
ret = etnaviv_iommu_map_gem(mmu_context, etnaviv_obj,
311+
mmu_context->global->memory_base, mapping);
313312
if (ret < 0) {
314313
etnaviv_iommu_context_put(mmu_context);
315314
kfree(mapping);

‎drivers/gpu/drm/etnaviv/etnaviv_gem.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ struct etnaviv_gem_submit {
9393
struct kref refcount;
9494
struct etnaviv_file_private *ctx;
9595
struct etnaviv_gpu *gpu;
96+
struct etnaviv_iommu_context *mmu_context, *prev_mmu_context;
9697
struct dma_fence *out_fence, *in_fence;
9798
int out_fence_id;
9899
struct list_head node; /* GPU active submit list */
@@ -119,8 +120,7 @@ struct page **etnaviv_gem_get_pages(struct etnaviv_gem_object *obj);
119120
void etnaviv_gem_put_pages(struct etnaviv_gem_object *obj);
120121

121122
struct etnaviv_vram_mapping *etnaviv_gem_mapping_get(
122-
struct drm_gem_object *obj, struct etnaviv_gpu *gpu,
123-
struct etnaviv_iommu_context *mmu_context);
123+
struct drm_gem_object *obj, struct etnaviv_iommu_context *mmu_context);
124124
void etnaviv_gem_mapping_unreference(struct etnaviv_vram_mapping *mapping);
125125

126126
#endif /* __ETNAVIV_GEM_H__ */

‎drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ static int submit_pin_objects(struct etnaviv_gem_submit *submit)
224224
struct etnaviv_vram_mapping *mapping;
225225

226226
mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base,
227-
submit->gpu,
228-
submit->gpu->mmu_context);
227+
submit->mmu_context);
229228
if (IS_ERR(mapping)) {
230229
ret = PTR_ERR(mapping);
231230
break;
@@ -362,6 +361,12 @@ static void submit_cleanup(struct kref *kref)
362361
if (submit->cmdbuf.suballoc)
363362
etnaviv_cmdbuf_free(&submit->cmdbuf);
364363

364+
if (submit->mmu_context)
365+
etnaviv_iommu_context_put(submit->mmu_context);
366+
367+
if (submit->prev_mmu_context)
368+
etnaviv_iommu_context_put(submit->prev_mmu_context);
369+
365370
for (i = 0; i < submit->nr_bos; i++) {
366371
struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj;
367372

@@ -503,6 +508,8 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
503508
goto err_submit_objects;
504509

505510
submit->ctx = file->driver_priv;
511+
etnaviv_iommu_context_get(submit->ctx->mmu);
512+
submit->mmu_context = submit->ctx->mmu;
506513
submit->exec_state = args->exec_state;
507514
submit->flags = args->flags;
508515

0 commit comments

Comments
 (0)