Skip to content

Commit 222ec45

Browse files
committed
drm/fb_helper: Support framebuffers in I/O memory
At least sparc64 requires I/O-specific access to framebuffers. This patch updates the fbdev console accordingly. For drivers with direct access to the framebuffer memory, the callback functions in struct fb_ops test for the type of memory and call the rsp fb_sys_ of fb_cfb_ functions. Read and write operations are implemented internally by DRM's fbdev helper. For drivers that employ a shadow buffer, fbdev's blit function retrieves the framebuffer address as struct dma_buf_map, and uses dma_buf_map interfaces to access the buffer. The bochs driver on sparc64 uses a workaround to flag the framebuffer as I/O memory and avoid a HW exception. With the introduction of struct dma_buf_map, this is not required any longer. The patch removes the rsp code from both, bochs and fbdev. v7: * use min_t(size_t,) (kernel test robot) * return the number of bytes read/written, if any (fbdev testcase) v5: * implement fb_read/fb_write internally (Daniel, Sam) v4: * move dma_buf_map changes into separate patch (Daniel) * TODO list: comment on fbdev updates (Daniel) Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch> Reviewed-by: Sam Ravnborg <sam@ravnborg.org> Tested-by: Sam Ravnborg <sam@ravnborg.org> Link: https://patchwork.freedesktop.org/patch/msgid/20201103093015.1063-11-tzimmermann@suse.de
1 parent b4e7090 commit 222ec45

4 files changed

Lines changed: 223 additions & 29 deletions

File tree

‎Documentation/gpu/todo.rst‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,28 @@ Convert drivers to use drm_fbdev_generic_setup()
201201
------------------------------------------------
202202

203203
Most drivers can use drm_fbdev_generic_setup(). Driver have to implement
204-
atomic modesetting and GEM vmap support. Current generic fbdev emulation
205-
expects the framebuffer in system memory (or system-like memory).
204+
atomic modesetting and GEM vmap support. Historically, generic fbdev emulation
205+
expected the framebuffer in system memory or system-like memory. By employing
206+
struct dma_buf_map, drivers with frambuffers in I/O memory can be supported
207+
as well.
206208

207209
Contact: Maintainer of the driver you plan to convert
208210

209211
Level: Intermediate
210212

213+
Reimplement functions in drm_fbdev_fb_ops without fbdev
214+
-------------------------------------------------------
215+
216+
A number of callback functions in drm_fbdev_fb_ops could benefit from
217+
being rewritten without dependencies on the fbdev module. Some of the
218+
helpers could further benefit from using struct dma_buf_map instead of
219+
raw pointers.
220+
221+
Contact: Thomas Zimmermann <tzimmermann@suse.de>, Daniel Vetter
222+
223+
Level: Advanced
224+
225+
211226
drm_framebuffer_funcs and drm_mode_config_funcs.fb_create cleanup
212227
-----------------------------------------------------------------
213228

‎drivers/gpu/drm/bochs/bochs_kms.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ int bochs_kms_init(struct bochs_device *bochs)
151151
bochs->dev->mode_config.preferred_depth = 24;
152152
bochs->dev->mode_config.prefer_shadow = 0;
153153
bochs->dev->mode_config.prefer_shadow_fbdev = 1;
154-
bochs->dev->mode_config.fbdev_use_iomem = true;
155154
bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
156155

157156
bochs->dev->mode_config.funcs = &bochs_mode_funcs;

‎drivers/gpu/drm/drm_fb_helper.c‎

Lines changed: 206 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,24 +372,22 @@ static void drm_fb_helper_resume_worker(struct work_struct *work)
372372
}
373373

374374
static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper,
375-
struct drm_clip_rect *clip)
375+
struct drm_clip_rect *clip,
376+
struct dma_buf_map *dst)
376377
{
377378
struct drm_framebuffer *fb = fb_helper->fb;
378379
unsigned int cpp = fb->format->cpp[0];
379380
size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp;
380381
void *src = fb_helper->fbdev->screen_buffer + offset;
381-
void *dst = fb_helper->buffer->map.vaddr + offset;
382382
size_t len = (clip->x2 - clip->x1) * cpp;
383383
unsigned int y;
384384

385-
for (y = clip->y1; y < clip->y2; y++) {
386-
if (!fb_helper->dev->mode_config.fbdev_use_iomem)
387-
memcpy(dst, src, len);
388-
else
389-
memcpy_toio((void __iomem *)dst, src, len);
385+
dma_buf_map_incr(dst, offset); /* go to first pixel within clip rect */
390386

387+
for (y = clip->y1; y < clip->y2; y++) {
388+
dma_buf_map_memcpy_to(dst, src, len);
389+
dma_buf_map_incr(dst, fb->pitches[0]);
391390
src += fb->pitches[0];
392-
dst += fb->pitches[0];
393391
}
394392
}
395393

@@ -417,8 +415,9 @@ static void drm_fb_helper_dirty_work(struct work_struct *work)
417415
ret = drm_client_buffer_vmap(helper->buffer, &map);
418416
if (ret)
419417
return;
420-
drm_fb_helper_dirty_blit_real(helper, &clip_copy);
418+
drm_fb_helper_dirty_blit_real(helper, &clip_copy, &map);
421419
}
420+
422421
if (helper->fb->funcs->dirty)
423422
helper->fb->funcs->dirty(helper->fb, NULL, 0, 0,
424423
&clip_copy, 1);
@@ -2027,18 +2026,211 @@ static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
20272026
return -ENODEV;
20282027
}
20292028

2029+
static bool drm_fbdev_use_iomem(struct fb_info *info)
2030+
{
2031+
struct drm_fb_helper *fb_helper = info->par;
2032+
struct drm_client_buffer *buffer = fb_helper->buffer;
2033+
2034+
return !drm_fbdev_use_shadow_fb(fb_helper) && buffer->map.is_iomem;
2035+
}
2036+
2037+
static ssize_t fb_read_screen_base(struct fb_info *info, char __user *buf, size_t count,
2038+
loff_t pos)
2039+
{
2040+
const char __iomem *src = info->screen_base + pos;
2041+
size_t alloc_size = min(count, PAGE_SIZE);
2042+
ssize_t ret = 0;
2043+
int err = 0;
2044+
char *tmp;
2045+
2046+
tmp = kmalloc(alloc_size, GFP_KERNEL);
2047+
if (!tmp)
2048+
return -ENOMEM;
2049+
2050+
while (count) {
2051+
size_t c = min_t(size_t, count, alloc_size);
2052+
2053+
memcpy_fromio(tmp, src, c);
2054+
if (copy_to_user(buf, tmp, c)) {
2055+
err = -EFAULT;
2056+
break;
2057+
}
2058+
2059+
src += c;
2060+
buf += c;
2061+
ret += c;
2062+
count -= c;
2063+
}
2064+
2065+
kfree(tmp);
2066+
2067+
return ret ? ret : err;
2068+
}
2069+
2070+
static ssize_t fb_read_screen_buffer(struct fb_info *info, char __user *buf, size_t count,
2071+
loff_t pos)
2072+
{
2073+
const char *src = info->screen_buffer + pos;
2074+
2075+
if (copy_to_user(buf, src, count))
2076+
return -EFAULT;
2077+
2078+
return count;
2079+
}
2080+
2081+
static ssize_t drm_fbdev_fb_read(struct fb_info *info, char __user *buf,
2082+
size_t count, loff_t *ppos)
2083+
{
2084+
loff_t pos = *ppos;
2085+
size_t total_size;
2086+
ssize_t ret;
2087+
2088+
if (info->screen_size)
2089+
total_size = info->screen_size;
2090+
else
2091+
total_size = info->fix.smem_len;
2092+
2093+
if (pos >= total_size)
2094+
return 0;
2095+
if (count >= total_size)
2096+
count = total_size;
2097+
if (total_size - count < pos)
2098+
count = total_size - pos;
2099+
2100+
if (drm_fbdev_use_iomem(info))
2101+
ret = fb_read_screen_base(info, buf, count, pos);
2102+
else
2103+
ret = fb_read_screen_buffer(info, buf, count, pos);
2104+
2105+
if (ret > 0)
2106+
*ppos += ret;
2107+
2108+
return ret;
2109+
}
2110+
2111+
static ssize_t fb_write_screen_base(struct fb_info *info, const char __user *buf, size_t count,
2112+
loff_t pos)
2113+
{
2114+
char __iomem *dst = info->screen_base + pos;
2115+
size_t alloc_size = min(count, PAGE_SIZE);
2116+
ssize_t ret = 0;
2117+
int err = 0;
2118+
u8 *tmp;
2119+
2120+
tmp = kmalloc(alloc_size, GFP_KERNEL);
2121+
if (!tmp)
2122+
return -ENOMEM;
2123+
2124+
while (count) {
2125+
size_t c = min_t(size_t, count, alloc_size);
2126+
2127+
if (copy_from_user(tmp, buf, c)) {
2128+
err = -EFAULT;
2129+
break;
2130+
}
2131+
memcpy_toio(dst, tmp, c);
2132+
2133+
dst += c;
2134+
buf += c;
2135+
ret += c;
2136+
count -= c;
2137+
}
2138+
2139+
kfree(tmp);
2140+
2141+
return ret ? ret : err;
2142+
}
2143+
2144+
static ssize_t fb_write_screen_buffer(struct fb_info *info, const char __user *buf, size_t count,
2145+
loff_t pos)
2146+
{
2147+
char *dst = info->screen_buffer + pos;
2148+
2149+
if (copy_from_user(dst, buf, count))
2150+
return -EFAULT;
2151+
2152+
return count;
2153+
}
2154+
2155+
static ssize_t drm_fbdev_fb_write(struct fb_info *info, const char __user *buf,
2156+
size_t count, loff_t *ppos)
2157+
{
2158+
loff_t pos = *ppos;
2159+
size_t total_size;
2160+
ssize_t ret;
2161+
int err = 0;
2162+
2163+
if (info->screen_size)
2164+
total_size = info->screen_size;
2165+
else
2166+
total_size = info->fix.smem_len;
2167+
2168+
if (pos > total_size)
2169+
return -EFBIG;
2170+
if (count > total_size) {
2171+
err = -EFBIG;
2172+
count = total_size;
2173+
}
2174+
if (total_size - count < pos) {
2175+
if (!err)
2176+
err = -ENOSPC;
2177+
count = total_size - pos;
2178+
}
2179+
2180+
/*
2181+
* Copy to framebuffer even if we already logged an error. Emulates
2182+
* the behavior of the original fbdev implementation.
2183+
*/
2184+
if (drm_fbdev_use_iomem(info))
2185+
ret = fb_write_screen_base(info, buf, count, pos);
2186+
else
2187+
ret = fb_write_screen_buffer(info, buf, count, pos);
2188+
2189+
if (ret > 0)
2190+
*ppos += ret;
2191+
2192+
return ret ? ret : err;
2193+
}
2194+
2195+
static void drm_fbdev_fb_fillrect(struct fb_info *info,
2196+
const struct fb_fillrect *rect)
2197+
{
2198+
if (drm_fbdev_use_iomem(info))
2199+
drm_fb_helper_cfb_fillrect(info, rect);
2200+
else
2201+
drm_fb_helper_sys_fillrect(info, rect);
2202+
}
2203+
2204+
static void drm_fbdev_fb_copyarea(struct fb_info *info,
2205+
const struct fb_copyarea *area)
2206+
{
2207+
if (drm_fbdev_use_iomem(info))
2208+
drm_fb_helper_cfb_copyarea(info, area);
2209+
else
2210+
drm_fb_helper_sys_copyarea(info, area);
2211+
}
2212+
2213+
static void drm_fbdev_fb_imageblit(struct fb_info *info,
2214+
const struct fb_image *image)
2215+
{
2216+
if (drm_fbdev_use_iomem(info))
2217+
drm_fb_helper_cfb_imageblit(info, image);
2218+
else
2219+
drm_fb_helper_sys_imageblit(info, image);
2220+
}
2221+
20302222
static const struct fb_ops drm_fbdev_fb_ops = {
20312223
.owner = THIS_MODULE,
20322224
DRM_FB_HELPER_DEFAULT_OPS,
20332225
.fb_open = drm_fbdev_fb_open,
20342226
.fb_release = drm_fbdev_fb_release,
20352227
.fb_destroy = drm_fbdev_fb_destroy,
20362228
.fb_mmap = drm_fbdev_fb_mmap,
2037-
.fb_read = drm_fb_helper_sys_read,
2038-
.fb_write = drm_fb_helper_sys_write,
2039-
.fb_fillrect = drm_fb_helper_sys_fillrect,
2040-
.fb_copyarea = drm_fb_helper_sys_copyarea,
2041-
.fb_imageblit = drm_fb_helper_sys_imageblit,
2229+
.fb_read = drm_fbdev_fb_read,
2230+
.fb_write = drm_fbdev_fb_write,
2231+
.fb_fillrect = drm_fbdev_fb_fillrect,
2232+
.fb_copyarea = drm_fbdev_fb_copyarea,
2233+
.fb_imageblit = drm_fbdev_fb_imageblit,
20422234
};
20432235

20442236
static struct fb_deferred_io drm_fbdev_defio = {

‎include/drm/drm_mode_config.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -877,18 +877,6 @@ struct drm_mode_config {
877877
*/
878878
bool prefer_shadow_fbdev;
879879

880-
/**
881-
* @fbdev_use_iomem:
882-
*
883-
* Set to true if framebuffer reside in iomem.
884-
* When set to true memcpy_toio() is used when copying the framebuffer in
885-
* drm_fb_helper.drm_fb_helper_dirty_blit_real().
886-
*
887-
* FIXME: This should be replaced with a per-mapping is_iomem
888-
* flag (like ttm does), and then used everywhere in fbdev code.
889-
*/
890-
bool fbdev_use_iomem;
891-
892880
/**
893881
* @quirk_addfb_prefer_xbgr_30bpp:
894882
*

0 commit comments

Comments
 (0)