Skip to content

Commit 8f2cb93

Browse files
committed
drm/gm12u320: Simplify upload work
Instead of having a work item that never stops (which really should be a kthread), with a dedicated workqueue to not upset anyone else, use a delayed work. A bunch of changes: - We can throw out all the custom wakeup and requeue logic and state tracking. If we schedule the work with a 0 delay it'll get scheduled immediately. - Persistent state (frame & draw_status_timeout) need to be moved out of the work. - diff is bigger than the changes, biggest chunk is reindenting the work fn because it lost its while loop. Lots of code deleting as consequence all over. Specifically we can delete the drm_driver.release code now! v2: Review from Hans: - Use mod_delayed_work in the plane update path to make sure we do actually schedule immediately). In the worker we still want queue_delayed_work, which won't modify the timeout when the work is already scheduled. Which is exactly what we want if the work races with a plane update. - Switch to system_long_wq, Hans says on usb2 a plane upload can take 80 ms. Reviewed-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Daniel Vetter <daniel.vetter@intel.com> Cc: Hans de Goede <hdegoede@redhat.com> Cc: "Noralf Trønnes" <noralf@tronnes.org> Link: https://patchwork.freedesktop.org/patch/msgid/20200323144950.3018436-46-daniel.vetter@ffwll.ch
1 parent 7ef64ed commit 8f2cb93

1 file changed

Lines changed: 68 additions & 103 deletions

File tree

‎drivers/gpu/drm/tiny/gm12u320.c‎

Lines changed: 68 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ struct gm12u320_device {
8989
unsigned char *cmd_buf;
9090
unsigned char *data_buf[GM12U320_BLOCK_COUNT];
9191
struct {
92-
bool run;
93-
struct workqueue_struct *workq;
94-
struct work_struct work;
95-
wait_queue_head_t waitq;
92+
struct delayed_work work;
9693
struct mutex lock;
9794
struct drm_framebuffer *fb;
9895
struct drm_rect rect;
96+
int frame;
97+
int draw_status_timeout;
9998
} fb_update;
10099
};
101100

@@ -183,19 +182,9 @@ static int gm12u320_usb_alloc(struct gm12u320_device *gm12u320)
183182
data_block_footer, DATA_BLOCK_FOOTER_SIZE);
184183
}
185184

186-
gm12u320->fb_update.workq = create_singlethread_workqueue(DRIVER_NAME);
187-
if (!gm12u320->fb_update.workq)
188-
return -ENOMEM;
189-
190185
return 0;
191186
}
192187

193-
static void gm12u320_usb_free(struct gm12u320_device *gm12u320)
194-
{
195-
if (gm12u320->fb_update.workq)
196-
destroy_workqueue(gm12u320->fb_update.workq);
197-
}
198-
199188
static int gm12u320_misc_request(struct gm12u320_device *gm12u320,
200189
u8 req_a, u8 req_b,
201190
u8 arg_a, u8 arg_b, u8 arg_c, u8 arg_d)
@@ -338,80 +327,77 @@ static void gm12u320_copy_fb_to_blocks(struct gm12u320_device *gm12u320)
338327
static void gm12u320_fb_update_work(struct work_struct *work)
339328
{
340329
struct gm12u320_device *gm12u320 =
341-
container_of(work, struct gm12u320_device, fb_update.work);
342-
int draw_status_timeout = FIRST_FRAME_TIMEOUT;
330+
container_of(to_delayed_work(work), struct gm12u320_device,
331+
fb_update.work);
343332
int block, block_size, len;
344-
int frame = 0;
345333
int ret = 0;
346334

347-
while (gm12u320->fb_update.run) {
348-
gm12u320_copy_fb_to_blocks(gm12u320);
349-
350-
for (block = 0; block < GM12U320_BLOCK_COUNT; block++) {
351-
if (block == GM12U320_BLOCK_COUNT - 1)
352-
block_size = DATA_LAST_BLOCK_SIZE;
353-
else
354-
block_size = DATA_BLOCK_SIZE;
355-
356-
/* Send data command to device */
357-
memcpy(gm12u320->cmd_buf, cmd_data, CMD_SIZE);
358-
gm12u320->cmd_buf[8] = block_size & 0xff;
359-
gm12u320->cmd_buf[9] = block_size >> 8;
360-
gm12u320->cmd_buf[20] = 0xfc - block * 4;
361-
gm12u320->cmd_buf[21] = block | (frame << 7);
362-
363-
ret = usb_bulk_msg(gm12u320->udev,
364-
usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
365-
gm12u320->cmd_buf, CMD_SIZE, &len,
366-
CMD_TIMEOUT);
367-
if (ret || len != CMD_SIZE)
368-
goto err;
369-
370-
/* Send data block to device */
371-
ret = usb_bulk_msg(gm12u320->udev,
372-
usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
373-
gm12u320->data_buf[block], block_size,
374-
&len, DATA_TIMEOUT);
375-
if (ret || len != block_size)
376-
goto err;
377-
378-
/* Read status */
379-
ret = usb_bulk_msg(gm12u320->udev,
380-
usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
381-
gm12u320->cmd_buf, READ_STATUS_SIZE, &len,
382-
CMD_TIMEOUT);
383-
if (ret || len != READ_STATUS_SIZE)
384-
goto err;
385-
}
335+
gm12u320_copy_fb_to_blocks(gm12u320);
336+
337+
for (block = 0; block < GM12U320_BLOCK_COUNT; block++) {
338+
if (block == GM12U320_BLOCK_COUNT - 1)
339+
block_size = DATA_LAST_BLOCK_SIZE;
340+
else
341+
block_size = DATA_BLOCK_SIZE;
342+
343+
/* Send data command to device */
344+
memcpy(gm12u320->cmd_buf, cmd_data, CMD_SIZE);
345+
gm12u320->cmd_buf[8] = block_size & 0xff;
346+
gm12u320->cmd_buf[9] = block_size >> 8;
347+
gm12u320->cmd_buf[20] = 0xfc - block * 4;
348+
gm12u320->cmd_buf[21] =
349+
block | (gm12u320->fb_update.frame << 7);
386350

387-
/* Send draw command to device */
388-
memcpy(gm12u320->cmd_buf, cmd_draw, CMD_SIZE);
389351
ret = usb_bulk_msg(gm12u320->udev,
390352
usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
391-
gm12u320->cmd_buf, CMD_SIZE, &len, CMD_TIMEOUT);
353+
gm12u320->cmd_buf, CMD_SIZE, &len,
354+
CMD_TIMEOUT);
392355
if (ret || len != CMD_SIZE)
393356
goto err;
394357

358+
/* Send data block to device */
359+
ret = usb_bulk_msg(gm12u320->udev,
360+
usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
361+
gm12u320->data_buf[block], block_size,
362+
&len, DATA_TIMEOUT);
363+
if (ret || len != block_size)
364+
goto err;
365+
395366
/* Read status */
396367
ret = usb_bulk_msg(gm12u320->udev,
397368
usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
398369
gm12u320->cmd_buf, READ_STATUS_SIZE, &len,
399-
draw_status_timeout);
370+
CMD_TIMEOUT);
400371
if (ret || len != READ_STATUS_SIZE)
401372
goto err;
402-
403-
draw_status_timeout = CMD_TIMEOUT;
404-
frame = !frame;
405-
406-
/*
407-
* We must draw a frame every 2s otherwise the projector
408-
* switches back to showing its logo.
409-
*/
410-
wait_event_timeout(gm12u320->fb_update.waitq,
411-
!gm12u320->fb_update.run ||
412-
gm12u320->fb_update.fb != NULL,
413-
IDLE_TIMEOUT);
414373
}
374+
375+
/* Send draw command to device */
376+
memcpy(gm12u320->cmd_buf, cmd_draw, CMD_SIZE);
377+
ret = usb_bulk_msg(gm12u320->udev,
378+
usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
379+
gm12u320->cmd_buf, CMD_SIZE, &len, CMD_TIMEOUT);
380+
if (ret || len != CMD_SIZE)
381+
goto err;
382+
383+
/* Read status */
384+
ret = usb_bulk_msg(gm12u320->udev,
385+
usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
386+
gm12u320->cmd_buf, READ_STATUS_SIZE, &len,
387+
gm12u320->fb_update.draw_status_timeout);
388+
if (ret || len != READ_STATUS_SIZE)
389+
goto err;
390+
391+
gm12u320->fb_update.draw_status_timeout = CMD_TIMEOUT;
392+
gm12u320->fb_update.frame = !gm12u320->fb_update.frame;
393+
394+
/*
395+
* We must draw a frame every 2s otherwise the projector
396+
* switches back to showing its logo.
397+
*/
398+
queue_delayed_work(system_long_wq, &gm12u320->fb_update.work,
399+
IDLE_TIMEOUT);
400+
415401
return;
416402
err:
417403
/* Do not log errors caused by module unload or device unplug */
@@ -446,36 +432,24 @@ static void gm12u320_fb_mark_dirty(struct drm_framebuffer *fb,
446432
mutex_unlock(&gm12u320->fb_update.lock);
447433

448434
if (wakeup)
449-
wake_up(&gm12u320->fb_update.waitq);
435+
mod_delayed_work(system_long_wq, &gm12u320->fb_update.work, 0);
450436

451437
if (old_fb)
452438
drm_framebuffer_put(old_fb);
453439
}
454440

455-
static void gm12u320_start_fb_update(struct gm12u320_device *gm12u320)
456-
{
457-
mutex_lock(&gm12u320->fb_update.lock);
458-
gm12u320->fb_update.run = true;
459-
mutex_unlock(&gm12u320->fb_update.lock);
460-
461-
queue_work(gm12u320->fb_update.workq, &gm12u320->fb_update.work);
462-
}
463-
464441
static void gm12u320_stop_fb_update(struct gm12u320_device *gm12u320)
465442
{
466-
mutex_lock(&gm12u320->fb_update.lock);
467-
gm12u320->fb_update.run = false;
468-
mutex_unlock(&gm12u320->fb_update.lock);
443+
struct drm_framebuffer *old_fb;
469444

470-
wake_up(&gm12u320->fb_update.waitq);
471-
cancel_work_sync(&gm12u320->fb_update.work);
445+
cancel_delayed_work_sync(&gm12u320->fb_update.work);
472446

473447
mutex_lock(&gm12u320->fb_update.lock);
474-
if (gm12u320->fb_update.fb) {
475-
drm_framebuffer_put(gm12u320->fb_update.fb);
476-
gm12u320->fb_update.fb = NULL;
477-
}
448+
old_fb = gm12u320->fb_update.fb;
449+
gm12u320->fb_update.fb = NULL;
478450
mutex_unlock(&gm12u320->fb_update.lock);
451+
452+
drm_framebuffer_put(old_fb);
479453
}
480454

481455
static int gm12u320_set_ecomode(struct gm12u320_device *gm12u320)
@@ -583,11 +557,11 @@ static void gm12u320_pipe_enable(struct drm_simple_display_pipe *pipe,
583557
struct drm_crtc_state *crtc_state,
584558
struct drm_plane_state *plane_state)
585559
{
586-
struct gm12u320_device *gm12u320 = pipe->crtc.dev->dev_private;
587560
struct drm_rect rect = { 0, 0, GM12U320_USER_WIDTH, GM12U320_HEIGHT };
561+
struct gm12u320_device *gm12u320 = pipe->crtc.dev->dev_private;
588562

563+
gm12u320->fb_update.draw_status_timeout = FIRST_FRAME_TIMEOUT;
589564
gm12u320_fb_mark_dirty(plane_state->fb, &rect);
590-
gm12u320_start_fb_update(gm12u320);
591565
}
592566

593567
static void gm12u320_pipe_disable(struct drm_simple_display_pipe *pipe)
@@ -622,13 +596,6 @@ static const uint64_t gm12u320_pipe_modifiers[] = {
622596
DRM_FORMAT_MOD_INVALID
623597
};
624598

625-
static void gm12u320_driver_release(struct drm_device *dev)
626-
{
627-
struct gm12u320_device *gm12u320 = dev->dev_private;
628-
629-
gm12u320_usb_free(gm12u320);
630-
}
631-
632599
DEFINE_DRM_GEM_FOPS(gm12u320_fops);
633600

634601
static struct drm_driver gm12u320_drm_driver = {
@@ -640,7 +607,6 @@ static struct drm_driver gm12u320_drm_driver = {
640607
.major = DRIVER_MAJOR,
641608
.minor = DRIVER_MINOR,
642609

643-
.release = gm12u320_driver_release,
644610
.fops = &gm12u320_fops,
645611
DRM_GEM_SHMEM_DRIVER_OPS,
646612
};
@@ -670,9 +636,8 @@ static int gm12u320_usb_probe(struct usb_interface *interface,
670636
return -ENOMEM;
671637

672638
gm12u320->udev = interface_to_usbdev(interface);
673-
INIT_WORK(&gm12u320->fb_update.work, gm12u320_fb_update_work);
639+
INIT_DELAYED_WORK(&gm12u320->fb_update.work, gm12u320_fb_update_work);
674640
mutex_init(&gm12u320->fb_update.lock);
675-
init_waitqueue_head(&gm12u320->fb_update.waitq);
676641

677642
dev = &gm12u320->dev;
678643
ret = devm_drm_dev_init(&interface->dev, dev, &gm12u320_drm_driver);

0 commit comments

Comments
 (0)