Skip to content

Commit eaa434d

Browse files
notrodanvet
authored andcommitted
drm/fb-helper: Add fb_deferred_io support
This adds deferred io support to drm_fb_helper. The fbdev framebuffer changes are flushed using the callback (struct drm_framebuffer *)->funcs->dirty() by a dedicated worker ensuring that it always runs in process context. For those wondering why we need to be able to handle atomic calling contexts: Both panic paths and cursor code and fbcon blanking can run from atomic. See commit bcb39af Author: Dave Airlie <airlied@redhat.com> Date: Thu Feb 7 11:19:15 2013 +1000 drm/udl: make usage as a console safer for where this was originally discovered. Signed-off-by: Noralf Trønnes <noralf@tronnes.org> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> [danvet: Augment commit message with why we need to handle atomic contexts.] Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: http://patchwork.freedesktop.org/patch/msgid/1461856717-6476-4-git-send-email-noralf@tronnes.org
1 parent 2b5e8e5 commit eaa434d

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

drivers/gpu/drm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ config DRM_KMS_FB_HELPER
5252
select FB_CFB_FILLRECT
5353
select FB_CFB_COPYAREA
5454
select FB_CFB_IMAGEBLIT
55+
select FB_DEFERRED_IO
5556
help
5657
FBDEV helpers for KMS drivers.
5758

drivers/gpu/drm/drm_fb_helper.c

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ static LIST_HEAD(kernel_fb_helper_list);
8484
* and set up an initial configuration using the detected hardware, drivers
8585
* should call drm_fb_helper_single_add_all_connectors() followed by
8686
* drm_fb_helper_initial_config().
87+
*
88+
* If CONFIG_FB_DEFERRED_IO is enabled and &drm_framebuffer_funcs ->dirty is
89+
* set, the drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit}
90+
* functions will accumulate changes and schedule &fb_helper .dirty_work to run
91+
* right away. This worker then calls the dirty() function ensuring that it
92+
* will always run in process context since the fb_*() function could be
93+
* running in atomic context. If drm_fb_helper_deferred_io() is used as the
94+
* deferred_io callback it will also schedule dirty_work with the damage
95+
* collected from the mmap page writes.
8796
*/
8897

8998
/**
@@ -637,6 +646,23 @@ static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
637646
kfree(helper->crtc_info);
638647
}
639648

649+
static void drm_fb_helper_dirty_work(struct work_struct *work)
650+
{
651+
struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
652+
dirty_work);
653+
struct drm_clip_rect *clip = &helper->dirty_clip;
654+
struct drm_clip_rect clip_copy;
655+
unsigned long flags;
656+
657+
spin_lock_irqsave(&helper->dirty_lock, flags);
658+
clip_copy = *clip;
659+
clip->x1 = clip->y1 = ~0;
660+
clip->x2 = clip->y2 = 0;
661+
spin_unlock_irqrestore(&helper->dirty_lock, flags);
662+
663+
helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
664+
}
665+
640666
/**
641667
* drm_fb_helper_prepare - setup a drm_fb_helper structure
642668
* @dev: DRM device
@@ -650,6 +676,9 @@ void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
650676
const struct drm_fb_helper_funcs *funcs)
651677
{
652678
INIT_LIST_HEAD(&helper->kernel_fb_list);
679+
spin_lock_init(&helper->dirty_lock);
680+
INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
681+
helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
653682
helper->funcs = funcs;
654683
helper->dev = dev;
655684
}
@@ -834,6 +863,59 @@ void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
834863
}
835864
EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
836865

866+
static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
867+
u32 width, u32 height)
868+
{
869+
struct drm_fb_helper *helper = info->par;
870+
struct drm_clip_rect *clip = &helper->dirty_clip;
871+
unsigned long flags;
872+
873+
if (!helper->fb->funcs->dirty)
874+
return;
875+
876+
spin_lock_irqsave(&helper->dirty_lock, flags);
877+
clip->x1 = min_t(u32, clip->x1, x);
878+
clip->y1 = min_t(u32, clip->y1, y);
879+
clip->x2 = max_t(u32, clip->x2, x + width);
880+
clip->y2 = max_t(u32, clip->y2, y + height);
881+
spin_unlock_irqrestore(&helper->dirty_lock, flags);
882+
883+
schedule_work(&helper->dirty_work);
884+
}
885+
886+
/**
887+
* drm_fb_helper_deferred_io() - fbdev deferred_io callback function
888+
* @info: fb_info struct pointer
889+
* @pagelist: list of dirty mmap framebuffer pages
890+
*
891+
* This function is used as the &fb_deferred_io ->deferred_io
892+
* callback function for flushing the fbdev mmap writes.
893+
*/
894+
void drm_fb_helper_deferred_io(struct fb_info *info,
895+
struct list_head *pagelist)
896+
{
897+
unsigned long start, end, min, max;
898+
struct page *page;
899+
u32 y1, y2;
900+
901+
min = ULONG_MAX;
902+
max = 0;
903+
list_for_each_entry(page, pagelist, lru) {
904+
start = page->index << PAGE_SHIFT;
905+
end = start + PAGE_SIZE - 1;
906+
min = min(min, start);
907+
max = max(max, end);
908+
}
909+
910+
if (min < max) {
911+
y1 = min / info->fix.line_length;
912+
y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
913+
info->var.yres);
914+
drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
915+
}
916+
}
917+
EXPORT_SYMBOL(drm_fb_helper_deferred_io);
918+
837919
/**
838920
* drm_fb_helper_sys_read - wrapper around fb_sys_read
839921
* @info: fb_info struct pointer
@@ -862,7 +944,14 @@ EXPORT_SYMBOL(drm_fb_helper_sys_read);
862944
ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
863945
size_t count, loff_t *ppos)
864946
{
865-
return fb_sys_write(info, buf, count, ppos);
947+
ssize_t ret;
948+
949+
ret = fb_sys_write(info, buf, count, ppos);
950+
if (ret > 0)
951+
drm_fb_helper_dirty(info, 0, 0, info->var.xres,
952+
info->var.yres);
953+
954+
return ret;
866955
}
867956
EXPORT_SYMBOL(drm_fb_helper_sys_write);
868957

@@ -877,6 +966,8 @@ void drm_fb_helper_sys_fillrect(struct fb_info *info,
877966
const struct fb_fillrect *rect)
878967
{
879968
sys_fillrect(info, rect);
969+
drm_fb_helper_dirty(info, rect->dx, rect->dy,
970+
rect->width, rect->height);
880971
}
881972
EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
882973

@@ -891,6 +982,8 @@ void drm_fb_helper_sys_copyarea(struct fb_info *info,
891982
const struct fb_copyarea *area)
892983
{
893984
sys_copyarea(info, area);
985+
drm_fb_helper_dirty(info, area->dx, area->dy,
986+
area->width, area->height);
894987
}
895988
EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
896989

@@ -905,6 +998,8 @@ void drm_fb_helper_sys_imageblit(struct fb_info *info,
905998
const struct fb_image *image)
906999
{
9071000
sys_imageblit(info, image);
1001+
drm_fb_helper_dirty(info, image->dx, image->dy,
1002+
image->width, image->height);
9081003
}
9091004
EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
9101005

@@ -919,6 +1014,8 @@ void drm_fb_helper_cfb_fillrect(struct fb_info *info,
9191014
const struct fb_fillrect *rect)
9201015
{
9211016
cfb_fillrect(info, rect);
1017+
drm_fb_helper_dirty(info, rect->dx, rect->dy,
1018+
rect->width, rect->height);
9221019
}
9231020
EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
9241021

@@ -933,6 +1030,8 @@ void drm_fb_helper_cfb_copyarea(struct fb_info *info,
9331030
const struct fb_copyarea *area)
9341031
{
9351032
cfb_copyarea(info, area);
1033+
drm_fb_helper_dirty(info, area->dx, area->dy,
1034+
area->width, area->height);
9361035
}
9371036
EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
9381037

@@ -947,6 +1046,8 @@ void drm_fb_helper_cfb_imageblit(struct fb_info *info,
9471046
const struct fb_image *image)
9481047
{
9491048
cfb_imageblit(info, image);
1049+
drm_fb_helper_dirty(info, image->dx, image->dy,
1050+
image->width, image->height);
9501051
}
9511052
EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
9521053

include/drm/drm_fb_helper.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ struct drm_fb_helper_connector {
172172
* @funcs: driver callbacks for fb helper
173173
* @fbdev: emulated fbdev device info struct
174174
* @pseudo_palette: fake palette of 16 colors
175+
* @dirty_clip: clip rectangle used with deferred_io to accumulate damage to
176+
* the screen buffer
177+
* @dirty_lock: spinlock protecting @dirty_clip
178+
* @dirty_work: worker used to flush the framebuffer
175179
*
176180
* This is the main structure used by the fbdev helpers. Drivers supporting
177181
* fbdev emulation should embedded this into their overall driver structure.
@@ -189,6 +193,9 @@ struct drm_fb_helper {
189193
const struct drm_fb_helper_funcs *funcs;
190194
struct fb_info *fbdev;
191195
u32 pseudo_palette[17];
196+
struct drm_clip_rect dirty_clip;
197+
spinlock_t dirty_lock;
198+
struct work_struct dirty_work;
192199

193200
/**
194201
* @kernel_fb_list:
@@ -245,6 +252,9 @@ void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
245252

246253
void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper);
247254

255+
void drm_fb_helper_deferred_io(struct fb_info *info,
256+
struct list_head *pagelist);
257+
248258
ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
249259
size_t count, loff_t *ppos);
250260
ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
@@ -368,6 +378,11 @@ static inline void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
368378
{
369379
}
370380

381+
static inline void drm_fb_helper_deferred_io(struct fb_info *info,
382+
struct list_head *pagelist)
383+
{
384+
}
385+
371386
static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info,
372387
char __user *buf, size_t count,
373388
loff_t *ppos)

0 commit comments

Comments
 (0)