Skip to content

Commit 5f5bb18

Browse files
committed
drm/vc4: hvs: Defer dlist slots deallocation
During normal operations, the cursor position update is done through an asynchronous plane update, which on the vc4 driver basically just modifies the right dlist word to move the plane to the new coordinates. However, when we have the overscan margins setup, we fall back to a regular commit when we are next to the edges. And since that commit happens to be on a cursor plane, it's considered a legacy cursor update by KMS. The main difference it makes is that it won't wait for its completion (ie, next vblank) before returning. This means if we have multiple commits happening in rapid succession, we can have several of them happening before the next vblank. In parallel, our dlist allocation is tied to a CRTC state, and each time we do a commit we end up with a new CRTC state, with the previous one being freed. This means that we free our previous dlist entry (but don't clear it though) every time a new one is being committed. Now, if we were to have two commits happening before the next vblank, we could end up freeing reusing the same dlist entries before the next vblank. Indeed, we would start from an initial state taking, for example, the dlist entries 10 to 20, then start a commit taking the entries 20 to 30 and setting the dlist pointer to 20, and freeing the dlist entries 10 to 20. However, since we haven't reach vblank yet, the HVS is still using the entries 10 to 20. If we were to make a new commit now, chances are the allocator are going to give the 10 to 20 entries back, and we would change their content to match the new state. If vblank hasn't happened yet, we just corrupted the active dlist entries. A first attempt to solve this was made by creating an intermediate dlist buffer to store the current (ie, as of the last commit) dlist content, that we would update each time the HVS is done with a frame. However, if the interrupt handler missed the vblank window, we would end up copying our intermediate dlist to the hardware one during the composition, essentially creating the same issue. Since making sure that our interrupt handler runs within a fixed, constrained, time window would require to make Linux a real-time kernel, this seems a bit out of scope. Instead, we can work around our original issue by keeping the dlist slots allocation longer. That way, we won't reuse a dlist slot while it's still in flight. In order to achieve this, instead of freeing the dlist slot when its associated CRTC state is destroyed, we'll queue it in a list. A naive implementation would free the buffers in that queue when we get our end of frame interrupt. However, there's still a race since, just like in the shadow dlist case, we don't control when the handler for that interrupt is going to run. Thus, we can end up with a commit adding an old dlist allocation to our queue during the window between our actual interrupt and when our handler will run. And since that buffer is still being used for the composition of the current frame, we can't free it right away, exposing us to the original bug. Fortunately for us, the hardware provides a frame counter that is increased each time the first line of a frame is being generated. Associating the frame counter the image is supposed to go away to the allocation, and then only deallocate buffers that have a counter below or equal to the one we see when the deallocation code should prevent the above race from occuring. Signed-off-by: Maxime Ripard <[email protected]>
1 parent 3e3324d commit 5f5bb18

File tree

4 files changed

+184
-23
lines changed

4 files changed

+184
-23
lines changed

drivers/gpu/drm/vc4/vc4_crtc.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -957,14 +957,8 @@ void vc4_crtc_destroy_state(struct drm_crtc *crtc,
957957
struct vc4_dev *vc4 = to_vc4_dev(crtc->dev);
958958
struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
959959

960-
if (drm_mm_node_allocated(&vc4_state->mm)) {
961-
unsigned long flags;
962-
963-
spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
964-
drm_mm_remove_node(&vc4_state->mm);
965-
spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
966-
967-
}
960+
vc4_hvs_mark_dlist_entry_stale(vc4->hvs, vc4_state->mm);
961+
vc4_state->mm = NULL;
968962

969963
drm_atomic_helper_crtc_destroy_state(crtc, state);
970964
}

drivers/gpu/drm/vc4/vc4_drv.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ struct vc4_hvs {
335335
struct drm_mm lbm_mm;
336336
spinlock_t mm_lock;
337337

338+
struct list_head stale_dlist_entries;
339+
struct work_struct free_dlist_work;
340+
338341
struct drm_mm_node mitchell_netravali_filter;
339342

340343
struct debugfs_regset32 regset;
@@ -573,10 +576,16 @@ struct drm_connector *vc4_get_crtc_connector(struct drm_crtc *crtc,
573576
struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,
574577
struct drm_crtc_state *state);
575578

579+
struct vc4_hvs_dlist_allocation {
580+
struct list_head node;
581+
struct drm_mm_node mm_node;
582+
unsigned int channel;
583+
u8 target_frame_count;
584+
};
585+
576586
struct vc4_crtc_state {
577587
struct drm_crtc_state base;
578-
/* Dlist area for this CRTC configuration. */
579-
struct drm_mm_node mm;
588+
struct vc4_hvs_dlist_allocation *mm;
580589
bool txp_armed;
581590
unsigned int assigned_channel;
582591

@@ -968,6 +977,8 @@ extern struct platform_driver vc4_hvs_driver;
968977
void vc4_hvs_stop_channel(struct vc4_hvs *hvs, unsigned int output);
969978
int vc4_hvs_get_fifo_from_output(struct vc4_hvs *hvs, unsigned int output);
970979
u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo);
980+
void vc4_hvs_mark_dlist_entry_stale(struct vc4_hvs *hvs,
981+
struct vc4_hvs_dlist_allocation *alloc);
971982
int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state);
972983
void vc4_hvs_atomic_begin(struct drm_crtc *crtc, struct drm_atomic_state *state);
973984
void vc4_hvs_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state);

drivers/gpu/drm/vc4/vc4_hvs.c

Lines changed: 168 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,150 @@ static void vc5_hvs_update_gamma_lut(struct vc4_hvs *hvs,
389389
vc5_hvs_lut_load(hvs, vc4_crtc);
390390
}
391391

392+
static void vc4_hvs_irq_enable_eof(const struct vc4_hvs *hvs,
393+
unsigned int channel)
394+
{
395+
u32 irq_mask = hvs->hvs5 ?
396+
SCALER5_DISPCTRL_DSPEIEOF(channel) :
397+
SCALER_DISPCTRL_DSPEIEOF(channel);
398+
399+
HVS_WRITE(SCALER_DISPCTRL,
400+
HVS_READ(SCALER_DISPCTRL) | irq_mask);
401+
}
402+
403+
static void vc4_hvs_irq_clear_eof(const struct vc4_hvs *hvs,
404+
unsigned int channel)
405+
{
406+
u32 irq_mask = hvs->hvs5 ?
407+
SCALER5_DISPCTRL_DSPEIEOF(channel) :
408+
SCALER_DISPCTRL_DSPEIEOF(channel);
409+
410+
HVS_WRITE(SCALER_DISPCTRL,
411+
HVS_READ(SCALER_DISPCTRL) & ~irq_mask);
412+
}
413+
414+
static struct vc4_hvs_dlist_allocation *
415+
vc4_hvs_alloc_dlist_entry(struct vc4_hvs *hvs,
416+
unsigned int channel,
417+
size_t dlist_count)
418+
{
419+
struct vc4_hvs_dlist_allocation *alloc;
420+
unsigned long flags;
421+
int ret;
422+
423+
if (channel == VC4_HVS_CHANNEL_DISABLED)
424+
return NULL;
425+
426+
alloc = kzalloc(sizeof(*alloc), GFP_KERNEL);
427+
if (!alloc)
428+
return ERR_PTR(-ENOMEM);
429+
430+
spin_lock_irqsave(&hvs->mm_lock, flags);
431+
ret = drm_mm_insert_node(&hvs->dlist_mm, &alloc->mm_node,
432+
dlist_count);
433+
spin_unlock_irqrestore(&hvs->mm_lock, flags);
434+
if (ret)
435+
return ERR_PTR(ret);
436+
437+
alloc->channel = channel;
438+
439+
return alloc;
440+
}
441+
442+
void vc4_hvs_mark_dlist_entry_stale(struct vc4_hvs *hvs,
443+
struct vc4_hvs_dlist_allocation *alloc)
444+
{
445+
unsigned long flags;
446+
u8 frcnt;
447+
448+
if (!alloc)
449+
return;
450+
451+
if (!drm_mm_node_allocated(&alloc->mm_node))
452+
return;
453+
454+
frcnt = vc4_hvs_get_fifo_frame_count(hvs, alloc->channel);
455+
alloc->target_frame_count = (frcnt + 1) & ((1 << 6) - 1);
456+
457+
spin_lock_irqsave(&hvs->mm_lock, flags);
458+
459+
list_add_tail(&alloc->node, &hvs->stale_dlist_entries);
460+
461+
HVS_WRITE(SCALER_DISPSTAT, SCALER_DISPSTAT_EOF(alloc->channel));
462+
vc4_hvs_irq_enable_eof(hvs, alloc->channel);
463+
464+
spin_unlock_irqrestore(&hvs->mm_lock, flags);
465+
}
466+
467+
static void vc4_hvs_schedule_dlist_sweep(struct vc4_hvs *hvs,
468+
unsigned int channel)
469+
{
470+
unsigned long flags;
471+
472+
spin_lock_irqsave(&hvs->mm_lock, flags);
473+
474+
if (!list_empty(&hvs->stale_dlist_entries))
475+
queue_work(system_unbound_wq, &hvs->free_dlist_work);
476+
477+
vc4_hvs_irq_clear_eof(hvs, channel);
478+
479+
spin_unlock_irqrestore(&hvs->mm_lock, flags);
480+
}
481+
482+
/*
483+
* Frame counts are essentially sequence numbers over 6 bits, and we
484+
* thus can use sequence number arithmetic and follow the RFC1982 to
485+
* implement proper comparison between them.
486+
*/
487+
static bool vc4_hvs_frcnt_lte(u8 cnt1, u8 cnt2)
488+
{
489+
return (s8)((cnt1 << 2) - (cnt2 << 2)) <= 0;
490+
}
491+
492+
/*
493+
* Some atomic commits (legacy cursor updates, mostly) will not wait for
494+
* the next vblank and will just return once the commit has been pushed
495+
* to the hardware.
496+
*
497+
* On the hardware side, our HVS stores the planes parameters in its
498+
* context RAM, and will use part of the RAM to store data during the
499+
* frame rendering.
500+
*
501+
* This interacts badly if we get multiple commits before the next
502+
* vblank since we could end up overwriting the DLIST entries used by
503+
* previous commits if our dlist allocation reuses that entry. In such a
504+
* case, we would overwrite the data currently being used by the
505+
* hardware, resulting in a corrupted frame.
506+
*
507+
* In order to work around this, we'll queue the dlist entries in a list
508+
* once the associated CRTC state is destroyed. The HVS only allows us
509+
* to know which entry is being active, but not which one are no longer
510+
* being used, so in order to avoid freeing entries that are still used
511+
* by the hardware we add a guesstimate of the frame count where our
512+
* entry will no longer be used, and thus will only free those entries
513+
* when we will have reached that frame count.
514+
*/
515+
static void vc4_hvs_dlist_free_work(struct work_struct *work)
516+
{
517+
struct vc4_hvs *hvs = container_of(work, struct vc4_hvs, free_dlist_work);
518+
struct vc4_hvs_dlist_allocation *cur, *next;
519+
unsigned long flags;
520+
521+
spin_lock_irqsave(&hvs->mm_lock, flags);
522+
list_for_each_entry_safe(cur, next, &hvs->stale_dlist_entries, node) {
523+
u8 frcnt;
524+
525+
frcnt = vc4_hvs_get_fifo_frame_count(hvs, cur->channel);
526+
if (!vc4_hvs_frcnt_lte(cur->target_frame_count, frcnt))
527+
continue;
528+
529+
list_del(&cur->node);
530+
drm_mm_remove_node(&cur->mm_node);
531+
kfree(cur);
532+
}
533+
spin_unlock_irqrestore(&hvs->mm_lock, flags);
534+
}
535+
392536
u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo)
393537
{
394538
u8 field = 0;
@@ -588,13 +732,12 @@ int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
588732
{
589733
struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
590734
struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
735+
struct vc4_hvs_dlist_allocation *alloc;
591736
struct drm_device *dev = crtc->dev;
592737
struct vc4_dev *vc4 = to_vc4_dev(dev);
593738
struct drm_plane *plane;
594-
unsigned long flags;
595739
const struct drm_plane_state *plane_state;
596740
u32 dlist_count = 0;
597-
int ret;
598741

599742
/* The pixelvalve can only feed one encoder (and encoders are
600743
* 1:1 with connectors.)
@@ -607,12 +750,11 @@ int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
607750

608751
dlist_count++; /* Account for SCALER_CTL0_END. */
609752

610-
spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
611-
ret = drm_mm_insert_node(&vc4->hvs->dlist_mm, &vc4_state->mm,
612-
dlist_count);
613-
spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
614-
if (ret)
615-
return ret;
753+
alloc = vc4_hvs_alloc_dlist_entry(vc4->hvs, vc4_state->assigned_channel, dlist_count);
754+
if (IS_ERR(alloc))
755+
return PTR_ERR(alloc);
756+
757+
vc4_state->mm = alloc;
616758

617759
return vc4_hvs_gamma_check(crtc, state);
618760
}
@@ -624,8 +766,9 @@ static void vc4_hvs_install_dlist(struct drm_crtc *crtc)
624766
struct vc4_hvs *hvs = vc4->hvs;
625767
struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
626768

769+
WARN_ON(!vc4_state->mm);
627770
HVS_WRITE(SCALER_DISPLISTX(vc4_state->assigned_channel),
628-
vc4_state->mm.start);
771+
vc4_state->mm->mm_node.start);
629772
}
630773

631774
static void vc4_hvs_update_dlist(struct drm_crtc *crtc)
@@ -650,8 +793,10 @@ static void vc4_hvs_update_dlist(struct drm_crtc *crtc)
650793
spin_unlock_irqrestore(&dev->event_lock, flags);
651794
}
652795

796+
WARN_ON(!vc4_state->mm);
797+
653798
spin_lock_irqsave(&vc4_crtc->irq_lock, flags);
654-
vc4_crtc->current_dlist = vc4_state->mm.start;
799+
vc4_crtc->current_dlist = vc4_state->mm->mm_node.start;
655800
spin_unlock_irqrestore(&vc4_crtc->irq_lock, flags);
656801
}
657802

@@ -708,8 +853,7 @@ void vc4_hvs_atomic_flush(struct drm_crtc *crtc,
708853
struct vc4_plane_state *vc4_plane_state;
709854
bool debug_dump_regs = false;
710855
bool enable_bg_fill = false;
711-
u32 __iomem *dlist_start = vc4->hvs->dlist + vc4_state->mm.start;
712-
u32 __iomem *dlist_next = dlist_start;
856+
u32 __iomem *dlist_start, *dlist_next;
713857

714858
if (vc4_state->assigned_channel == VC4_HVS_CHANNEL_DISABLED)
715859
return;
@@ -719,6 +863,9 @@ void vc4_hvs_atomic_flush(struct drm_crtc *crtc,
719863
vc4_hvs_dump_state(hvs);
720864
}
721865

866+
dlist_start = vc4->hvs->dlist + vc4_state->mm->mm_node.start;
867+
dlist_next = dlist_start;
868+
722869
/* Copy all the active planes' dlist contents to the hardware dlist. */
723870
drm_atomic_crtc_for_each_plane(plane, crtc) {
724871
/* Is this the first active plane? */
@@ -741,7 +888,8 @@ void vc4_hvs_atomic_flush(struct drm_crtc *crtc,
741888
writel(SCALER_CTL0_END, dlist_next);
742889
dlist_next++;
743890

744-
WARN_ON_ONCE(dlist_next - dlist_start != vc4_state->mm.size);
891+
WARN_ON(!vc4_state->mm);
892+
WARN_ON_ONCE(dlist_next - dlist_start != vc4_state->mm->mm_node.size);
745893

746894
if (enable_bg_fill)
747895
/* This sets a black background color fill, as is the case
@@ -846,6 +994,11 @@ static irqreturn_t vc4_hvs_irq_handler(int irq, void *data)
846994

847995
irqret = IRQ_HANDLED;
848996
}
997+
998+
if (status & SCALER_DISPSTAT_EOF(channel)) {
999+
vc4_hvs_schedule_dlist_sweep(hvs, channel);
1000+
irqret = IRQ_HANDLED;
1001+
}
8491002
}
8501003

8511004
/* Clear every per-channel interrupt flag. */
@@ -902,6 +1055,8 @@ static int vc4_hvs_bind(struct device *dev, struct device *master, void *data)
9021055
hvs->dlist = hvs->regs + SCALER5_DLIST_START;
9031056

9041057
spin_lock_init(&hvs->mm_lock);
1058+
INIT_LIST_HEAD(&hvs->stale_dlist_entries);
1059+
INIT_WORK(&hvs->free_dlist_work, vc4_hvs_dlist_free_work);
9051060

9061061
/* Set up the HVS display list memory manager. We never
9071062
* overwrite the setup from the bootloader (just 128b out of

drivers/gpu/drm/vc4/vc4_regs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
# define SCALER_DISPCTRL_DSPEIEOLN(x) BIT(8 + ((x) * 2))
235235
/* Enables Display 0 EOF contribution to SCALER_DISPSTAT_IRQDISP0 */
236236
# define SCALER_DISPCTRL_DSPEIEOF(x) BIT(7 + ((x) * 2))
237+
# define SCALER5_DISPCTRL_DSPEIEOF(x) BIT(7 + ((x) * 4))
237238

238239
# define SCALER_DISPCTRL_SLVRDEIRQ BIT(6)
239240
# define SCALER_DISPCTRL_SLVWREIRQ BIT(5)

0 commit comments

Comments
 (0)