Skip to content

Commit 35c8b4b

Browse files
drm/vc4: Allocate binner bo when starting to use the V3D
The binner BO is not required until the V3D is in use, so avoid allocating it at probe and do it on the first non-dumb BO allocation. Keep track of which clients are using the V3D and liberate the buffer when there is none left, using a kref. Protect the logic with a mutex to avoid race conditions. The binner BO is created at the time of the first render ioctl and is destroyed when there is no client and no exec job using it left. The Out-Of-Memory (OOM) interrupt also gets some tweaking, to avoid enabling it before having allocated a binner bo. We also want to keep the BO alive during runtime suspend/resume to avoid failing to allocate it at resume. This happens when the CMA pool is full at that point and results in a hard crash. Signed-off-by: Paul Kocialkowski <paul.kocialkowski@bootlin.com> Reviewed-by: Eric Anholt <eric@anholt.net> Link: https://patchwork.freedesktop.org/patch/msgid/20190516145544.29051-5-paul.kocialkowski@bootlin.com
1 parent e43fe02 commit 35c8b4b

6 files changed

Lines changed: 125 additions & 20 deletions

File tree

‎drivers/gpu/drm/vc4/vc4_bo.c‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,13 +799,36 @@ vc4_prime_import_sg_table(struct drm_device *dev,
799799
return obj;
800800
}
801801

802+
static int vc4_grab_bin_bo(struct vc4_dev *vc4, struct vc4_file *vc4file)
803+
{
804+
int ret;
805+
806+
if (!vc4->v3d)
807+
return -ENODEV;
808+
809+
if (vc4file->bin_bo_used)
810+
return 0;
811+
812+
ret = vc4_v3d_bin_bo_get(vc4, &vc4file->bin_bo_used);
813+
if (ret)
814+
return ret;
815+
816+
return 0;
817+
}
818+
802819
int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
803820
struct drm_file *file_priv)
804821
{
805822
struct drm_vc4_create_bo *args = data;
823+
struct vc4_file *vc4file = file_priv->driver_priv;
824+
struct vc4_dev *vc4 = to_vc4_dev(dev);
806825
struct vc4_bo *bo = NULL;
807826
int ret;
808827

828+
ret = vc4_grab_bin_bo(vc4, vc4file);
829+
if (ret)
830+
return ret;
831+
809832
/*
810833
* We can't allocate from the BO cache, because the BOs don't
811834
* get zeroed, and that might leak data between users.
@@ -846,6 +869,8 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
846869
struct drm_file *file_priv)
847870
{
848871
struct drm_vc4_create_shader_bo *args = data;
872+
struct vc4_file *vc4file = file_priv->driver_priv;
873+
struct vc4_dev *vc4 = to_vc4_dev(dev);
849874
struct vc4_bo *bo = NULL;
850875
int ret;
851876

@@ -865,6 +890,10 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
865890
return -EINVAL;
866891
}
867892

893+
ret = vc4_grab_bin_bo(vc4, vc4file);
894+
if (ret)
895+
return ret;
896+
868897
bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
869898
if (IS_ERR(bo))
870899
return PTR_ERR(bo);
@@ -894,7 +923,7 @@ vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
894923
*/
895924
ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
896925

897-
fail:
926+
fail:
898927
drm_gem_object_put_unlocked(&bo->base.base);
899928

900929
return ret;

‎drivers/gpu/drm/vc4/vc4_drv.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,12 @@ static int vc4_open(struct drm_device *dev, struct drm_file *file)
128128

129129
static void vc4_close(struct drm_device *dev, struct drm_file *file)
130130
{
131+
struct vc4_dev *vc4 = to_vc4_dev(dev);
131132
struct vc4_file *vc4file = file->driver_priv;
132133

134+
if (vc4file->bin_bo_used)
135+
vc4_v3d_bin_bo_put(vc4);
136+
133137
vc4_perfmon_close_file(vc4file);
134138
kfree(vc4file);
135139
}
@@ -274,6 +278,8 @@ static int vc4_drm_bind(struct device *dev)
274278
drm->dev_private = vc4;
275279
INIT_LIST_HEAD(&vc4->debugfs_list);
276280

281+
mutex_init(&vc4->bin_bo_lock);
282+
277283
ret = vc4_bo_cache_init(drm);
278284
if (ret)
279285
goto dev_put;

‎drivers/gpu/drm/vc4/vc4_drv.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ struct vc4_dev {
216216
* the minor is available (after drm_dev_register()).
217217
*/
218218
struct list_head debugfs_list;
219+
220+
/* Mutex for binner bo allocation. */
221+
struct mutex bin_bo_lock;
222+
/* Reference count for our binner bo. */
223+
struct kref bin_bo_kref;
219224
};
220225

221226
static inline struct vc4_dev *
@@ -584,6 +589,11 @@ struct vc4_exec_info {
584589
* NULL otherwise.
585590
*/
586591
struct vc4_perfmon *perfmon;
592+
593+
/* Whether the exec has taken a reference to the binner BO, which should
594+
* happen with a VC4_PACKET_TILE_BINNING_MODE_CONFIG packet.
595+
*/
596+
bool bin_bo_used;
587597
};
588598

589599
/* Per-open file private data. Any driver-specific resource that has to be
@@ -594,6 +604,8 @@ struct vc4_file {
594604
struct idr idr;
595605
struct mutex lock;
596606
} perfmon;
607+
608+
bool bin_bo_used;
597609
};
598610

599611
static inline struct vc4_exec_info *
@@ -833,6 +845,8 @@ void vc4_plane_async_set_fb(struct drm_plane *plane,
833845
extern struct platform_driver vc4_v3d_driver;
834846
extern const struct of_device_id vc4_v3d_dt_match[];
835847
int vc4_v3d_get_bin_slot(struct vc4_dev *vc4);
848+
int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used);
849+
void vc4_v3d_bin_bo_put(struct vc4_dev *vc4);
836850
int vc4_v3d_pm_get(struct vc4_dev *vc4);
837851
void vc4_v3d_pm_put(struct vc4_dev *vc4);
838852

‎drivers/gpu/drm/vc4/vc4_gem.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,7 @@ static int
820820
vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
821821
{
822822
struct drm_vc4_submit_cl *args = exec->args;
823+
struct vc4_dev *vc4 = to_vc4_dev(dev);
823824
void *temp = NULL;
824825
void *bin;
825826
int ret = 0;
@@ -918,6 +919,12 @@ vc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
918919
if (ret)
919920
goto fail;
920921

922+
if (exec->found_tile_binning_mode_config_packet) {
923+
ret = vc4_v3d_bin_bo_get(vc4, &exec->bin_bo_used);
924+
if (ret)
925+
goto fail;
926+
}
927+
921928
/* Block waiting on any previous rendering into the CS's VBO,
922929
* IB, or textures, so that pixels are actually written by the
923930
* time we try to read them.
@@ -966,6 +973,10 @@ vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
966973
vc4->bin_alloc_used &= ~exec->bin_slots;
967974
spin_unlock_irqrestore(&vc4->job_lock, irqflags);
968975

976+
/* Release the reference on the binner BO if needed. */
977+
if (exec->bin_bo_used)
978+
vc4_v3d_bin_bo_put(vc4);
979+
969980
/* Release the reference we had on the perf monitor. */
970981
vc4_perfmon_put(exec->perfmon);
971982

‎drivers/gpu/drm/vc4/vc4_irq.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,22 @@ vc4_overflow_mem_work(struct work_struct *work)
5959
{
6060
struct vc4_dev *vc4 =
6161
container_of(work, struct vc4_dev, overflow_mem_work);
62-
struct vc4_bo *bo = vc4->bin_bo;
62+
struct vc4_bo *bo;
6363
int bin_bo_slot;
6464
struct vc4_exec_info *exec;
6565
unsigned long irqflags;
6666

67-
if (!bo)
68-
return;
67+
mutex_lock(&vc4->bin_bo_lock);
68+
69+
if (!vc4->bin_bo)
70+
goto complete;
71+
72+
bo = vc4->bin_bo;
6973

7074
bin_bo_slot = vc4_v3d_get_bin_slot(vc4);
7175
if (bin_bo_slot < 0) {
7276
DRM_ERROR("Couldn't allocate binner overflow mem\n");
73-
return;
77+
goto complete;
7478
}
7579

7680
spin_lock_irqsave(&vc4->job_lock, irqflags);
@@ -101,6 +105,9 @@ vc4_overflow_mem_work(struct work_struct *work)
101105
V3D_WRITE(V3D_INTCTL, V3D_INT_OUTOMEM);
102106
V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
103107
spin_unlock_irqrestore(&vc4->job_lock, irqflags);
108+
109+
complete:
110+
mutex_unlock(&vc4->bin_bo_lock);
104111
}
105112

106113
static void
@@ -252,8 +259,10 @@ vc4_irq_postinstall(struct drm_device *dev)
252259
if (!vc4->v3d)
253260
return 0;
254261

255-
/* Enable both the render done and out of memory interrupts. */
256-
V3D_WRITE(V3D_INTENA, V3D_DRIVER_IRQS);
262+
/* Enable the render done interrupts. The out-of-memory interrupt is
263+
* enabled as soon as we have a binner BO allocated.
264+
*/
265+
V3D_WRITE(V3D_INTENA, V3D_INT_FLDONE | V3D_INT_FRDONE);
257266

258267
return 0;
259268
}

‎drivers/gpu/drm/vc4/vc4_v3d.c‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ static int bin_bo_alloc(struct vc4_dev *vc4)
294294
WARN_ON_ONCE(sizeof(vc4->bin_alloc_used) * 8 !=
295295
bo->base.base.size / vc4->bin_alloc_size);
296296

297+
kref_init(&vc4->bin_bo_kref);
298+
299+
/* Enable the out-of-memory interrupt to set our
300+
* newly-allocated binner BO, potentially from an
301+
* already-pending-but-masked interrupt.
302+
*/
303+
V3D_WRITE(V3D_INTENA, V3D_INT_OUTOMEM);
304+
297305
break;
298306
}
299307

@@ -313,6 +321,47 @@ static int bin_bo_alloc(struct vc4_dev *vc4)
313321
return ret;
314322
}
315323

324+
int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used)
325+
{
326+
int ret = 0;
327+
328+
mutex_lock(&vc4->bin_bo_lock);
329+
330+
if (used && *used)
331+
goto complete;
332+
333+
if (vc4->bin_bo)
334+
kref_get(&vc4->bin_bo_kref);
335+
else
336+
ret = bin_bo_alloc(vc4);
337+
338+
if (ret == 0 && used)
339+
*used = true;
340+
341+
complete:
342+
mutex_unlock(&vc4->bin_bo_lock);
343+
344+
return ret;
345+
}
346+
347+
static void bin_bo_release(struct kref *ref)
348+
{
349+
struct vc4_dev *vc4 = container_of(ref, struct vc4_dev, bin_bo_kref);
350+
351+
if (WARN_ON_ONCE(!vc4->bin_bo))
352+
return;
353+
354+
drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
355+
vc4->bin_bo = NULL;
356+
}
357+
358+
void vc4_v3d_bin_bo_put(struct vc4_dev *vc4)
359+
{
360+
mutex_lock(&vc4->bin_bo_lock);
361+
kref_put(&vc4->bin_bo_kref, bin_bo_release);
362+
mutex_unlock(&vc4->bin_bo_lock);
363+
}
364+
316365
#ifdef CONFIG_PM
317366
static int vc4_v3d_runtime_suspend(struct device *dev)
318367
{
@@ -321,9 +370,6 @@ static int vc4_v3d_runtime_suspend(struct device *dev)
321370

322371
vc4_irq_uninstall(vc4->dev);
323372

324-
drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
325-
vc4->bin_bo = NULL;
326-
327373
clk_disable_unprepare(v3d->clk);
328374

329375
return 0;
@@ -335,10 +381,6 @@ static int vc4_v3d_runtime_resume(struct device *dev)
335381
struct vc4_dev *vc4 = v3d->vc4;
336382
int ret;
337383

338-
ret = bin_bo_alloc(vc4);
339-
if (ret)
340-
return ret;
341-
342384
ret = clk_prepare_enable(v3d->clk);
343385
if (ret != 0)
344386
return ret;
@@ -405,12 +447,6 @@ static int vc4_v3d_bind(struct device *dev, struct device *master, void *data)
405447
if (ret != 0)
406448
return ret;
407449

408-
ret = bin_bo_alloc(vc4);
409-
if (ret) {
410-
clk_disable_unprepare(v3d->clk);
411-
return ret;
412-
}
413-
414450
/* Reset the binner overflow address/size at setup, to be sure
415451
* we don't reuse an old one.
416452
*/

0 commit comments

Comments
 (0)