1 // SPDX-License-Identifier: MIT 2 3 #include <linux/moduleparam.h> 4 #include <linux/vmalloc.h> 5 6 #include <drm/drm_crtc_helper.h> 7 #include <drm/drm_drv.h> 8 #include <drm/drm_fb_helper.h> 9 #include <drm/drm_framebuffer.h> 10 #include <drm/drm_print.h> 11 12 #include <drm/drm_fbdev_generic.h> 13 14 /* @user: 1=userspace, 0=fbcon */ 15 static int drm_fbdev_fb_open(struct fb_info *info, int user) 16 { 17 struct drm_fb_helper *fb_helper = info->par; 18 19 /* No need to take a ref for fbcon because it unbinds on unregister */ 20 if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) 21 return -ENODEV; 22 23 return 0; 24 } 25 26 static int drm_fbdev_fb_release(struct fb_info *info, int user) 27 { 28 struct drm_fb_helper *fb_helper = info->par; 29 30 if (user) 31 module_put(fb_helper->dev->driver->fops->owner); 32 33 return 0; 34 } 35 36 static void drm_fbdev_fb_destroy(struct fb_info *info) 37 { 38 struct drm_fb_helper *fb_helper = info->par; 39 void *shadow = info->screen_buffer; 40 41 if (!fb_helper->dev) 42 return; 43 44 fb_deferred_io_cleanup(info); 45 drm_fb_helper_fini(fb_helper); 46 vfree(shadow); 47 drm_client_framebuffer_delete(fb_helper->buffer); 48 49 drm_client_release(&fb_helper->client); 50 drm_fb_helper_unprepare(fb_helper); 51 kfree(fb_helper); 52 } 53 54 static const struct fb_ops drm_fbdev_fb_ops = { 55 .owner = THIS_MODULE, 56 .fb_open = drm_fbdev_fb_open, 57 .fb_release = drm_fbdev_fb_release, 58 .fb_read = drm_fb_helper_sys_read, 59 .fb_write = drm_fb_helper_sys_write, 60 DRM_FB_HELPER_DEFAULT_OPS, 61 .fb_fillrect = drm_fb_helper_sys_fillrect, 62 .fb_copyarea = drm_fb_helper_sys_copyarea, 63 .fb_imageblit = drm_fb_helper_sys_imageblit, 64 .fb_mmap = fb_deferred_io_mmap, 65 .fb_destroy = drm_fbdev_fb_destroy, 66 }; 67 68 /* 69 * This function uses the client API to create a framebuffer backed by a dumb buffer. 70 */ 71 static int drm_fbdev_fb_probe(struct drm_fb_helper *fb_helper, 72 struct drm_fb_helper_surface_size *sizes) 73 { 74 struct drm_client_dev *client = &fb_helper->client; 75 struct drm_device *dev = fb_helper->dev; 76 struct drm_client_buffer *buffer; 77 struct drm_framebuffer *fb; 78 struct fb_info *info; 79 u32 format; 80 int ret; 81 82 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n", 83 sizes->surface_width, sizes->surface_height, 84 sizes->surface_bpp); 85 86 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); 87 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 88 sizes->surface_height, format); 89 if (IS_ERR(buffer)) 90 return PTR_ERR(buffer); 91 92 fb_helper->buffer = buffer; 93 fb_helper->fb = buffer->fb; 94 fb = buffer->fb; 95 96 info = drm_fb_helper_alloc_info(fb_helper); 97 if (IS_ERR(info)) 98 return PTR_ERR(info); 99 100 info->fbops = &drm_fbdev_fb_ops; 101 info->screen_size = sizes->surface_height * fb->pitches[0]; 102 info->fix.smem_len = info->screen_size; 103 info->flags = FBINFO_DEFAULT; 104 105 drm_fb_helper_fill_info(info, fb_helper, sizes); 106 107 info->screen_buffer = vzalloc(info->screen_size); 108 if (!info->screen_buffer) 109 return -ENOMEM; 110 info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST; 111 112 info->fix.smem_start = page_to_phys(vmalloc_to_page(info->screen_buffer)); 113 114 /* Set a default deferred I/O handler */ 115 fb_helper->fbdefio.delay = HZ / 20; 116 fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io; 117 118 info->fbdefio = &fb_helper->fbdefio; 119 ret = fb_deferred_io_init(info); 120 if (ret) 121 return ret; 122 123 return 0; 124 } 125 126 static void drm_fbdev_damage_blit_real(struct drm_fb_helper *fb_helper, 127 struct drm_clip_rect *clip, 128 struct iosys_map *dst) 129 { 130 struct drm_framebuffer *fb = fb_helper->fb; 131 size_t offset = clip->y1 * fb->pitches[0]; 132 size_t len = clip->x2 - clip->x1; 133 unsigned int y; 134 void *src; 135 136 switch (drm_format_info_bpp(fb->format, 0)) { 137 case 1: 138 offset += clip->x1 / 8; 139 len = DIV_ROUND_UP(len + clip->x1 % 8, 8); 140 break; 141 case 2: 142 offset += clip->x1 / 4; 143 len = DIV_ROUND_UP(len + clip->x1 % 4, 4); 144 break; 145 case 4: 146 offset += clip->x1 / 2; 147 len = DIV_ROUND_UP(len + clip->x1 % 2, 2); 148 break; 149 default: 150 offset += clip->x1 * fb->format->cpp[0]; 151 len *= fb->format->cpp[0]; 152 break; 153 } 154 155 src = fb_helper->info->screen_buffer + offset; 156 iosys_map_incr(dst, offset); /* go to first pixel within clip rect */ 157 158 for (y = clip->y1; y < clip->y2; y++) { 159 iosys_map_memcpy_to(dst, 0, src, len); 160 iosys_map_incr(dst, fb->pitches[0]); 161 src += fb->pitches[0]; 162 } 163 } 164 165 static int drm_fbdev_damage_blit(struct drm_fb_helper *fb_helper, 166 struct drm_clip_rect *clip) 167 { 168 struct drm_client_buffer *buffer = fb_helper->buffer; 169 struct iosys_map map, dst; 170 int ret; 171 172 /* 173 * We have to pin the client buffer to its current location while 174 * flushing the shadow buffer. In the general case, concurrent 175 * modesetting operations could try to move the buffer and would 176 * fail. The modeset has to be serialized by acquiring the reservation 177 * object of the underlying BO here. 178 * 179 * For fbdev emulation, we only have to protect against fbdev modeset 180 * operations. Nothing else will involve the client buffer's BO. So it 181 * is sufficient to acquire struct drm_fb_helper.lock here. 182 */ 183 mutex_lock(&fb_helper->lock); 184 185 ret = drm_client_buffer_vmap(buffer, &map); 186 if (ret) 187 goto out; 188 189 dst = map; 190 drm_fbdev_damage_blit_real(fb_helper, clip, &dst); 191 192 drm_client_buffer_vunmap(buffer); 193 194 out: 195 mutex_unlock(&fb_helper->lock); 196 197 return ret; 198 } 199 200 static int drm_fbdev_fb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip) 201 { 202 struct drm_device *dev = helper->dev; 203 int ret; 204 205 /* Call damage handlers only if necessary */ 206 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2)) 207 return 0; 208 209 ret = drm_fbdev_damage_blit(helper, clip); 210 if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret)) 211 return ret; 212 213 if (helper->fb->funcs->dirty) { 214 ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1); 215 if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret)) 216 return ret; 217 } 218 219 return 0; 220 } 221 222 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { 223 .fb_probe = drm_fbdev_fb_probe, 224 .fb_dirty = drm_fbdev_fb_dirty, 225 }; 226 227 static void drm_fbdev_client_unregister(struct drm_client_dev *client) 228 { 229 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 230 231 if (fb_helper->info) { 232 drm_fb_helper_unregister_info(fb_helper); 233 } else { 234 drm_client_release(&fb_helper->client); 235 drm_fb_helper_unprepare(fb_helper); 236 kfree(fb_helper); 237 } 238 } 239 240 static int drm_fbdev_client_restore(struct drm_client_dev *client) 241 { 242 drm_fb_helper_lastclose(client->dev); 243 244 return 0; 245 } 246 247 static int drm_fbdev_client_hotplug(struct drm_client_dev *client) 248 { 249 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 250 struct drm_device *dev = client->dev; 251 int ret; 252 253 if (dev->fb_helper) 254 return drm_fb_helper_hotplug_event(dev->fb_helper); 255 256 ret = drm_fb_helper_init(dev, fb_helper); 257 if (ret) 258 goto err_drm_err; 259 260 if (!drm_drv_uses_atomic_modeset(dev)) 261 drm_helper_disable_unused_functions(dev); 262 263 ret = drm_fb_helper_initial_config(fb_helper); 264 if (ret) 265 goto err_drm_fb_helper_fini; 266 267 return 0; 268 269 err_drm_fb_helper_fini: 270 drm_fb_helper_fini(fb_helper); 271 err_drm_err: 272 drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret); 273 return ret; 274 } 275 276 static const struct drm_client_funcs drm_fbdev_client_funcs = { 277 .owner = THIS_MODULE, 278 .unregister = drm_fbdev_client_unregister, 279 .restore = drm_fbdev_client_restore, 280 .hotplug = drm_fbdev_client_hotplug, 281 }; 282 283 /** 284 * drm_fbdev_generic_setup() - Setup generic fbdev emulation 285 * @dev: DRM device 286 * @preferred_bpp: Preferred bits per pixel for the device. 287 * 288 * This function sets up generic fbdev emulation for drivers that supports 289 * dumb buffers with a virtual address and that can be mmap'ed. 290 * drm_fbdev_generic_setup() shall be called after the DRM driver registered 291 * the new DRM device with drm_dev_register(). 292 * 293 * Restore, hotplug events and teardown are all taken care of. Drivers that do 294 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. 295 * Simple drivers might use drm_mode_config_helper_suspend(). 296 * 297 * In order to provide fixed mmap-able memory ranges, generic fbdev emulation 298 * uses a shadow buffer in system memory. The implementation blits the shadow 299 * fbdev buffer onto the real buffer in regular intervals. 300 * 301 * This function is safe to call even when there are no connectors present. 302 * Setup will be retried on the next hotplug event. 303 * 304 * The fbdev is destroyed by drm_dev_unregister(). 305 */ 306 void drm_fbdev_generic_setup(struct drm_device *dev, 307 unsigned int preferred_bpp) 308 { 309 struct drm_fb_helper *fb_helper; 310 int ret; 311 312 drm_WARN(dev, !dev->registered, "Device has not been registered.\n"); 313 drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n"); 314 315 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 316 if (!fb_helper) 317 return; 318 drm_fb_helper_prepare(dev, fb_helper, preferred_bpp, &drm_fb_helper_generic_funcs); 319 320 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); 321 if (ret) { 322 drm_err(dev, "Failed to register client: %d\n", ret); 323 goto err_drm_client_init; 324 } 325 326 ret = drm_fbdev_client_hotplug(&fb_helper->client); 327 if (ret) 328 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); 329 330 drm_client_register(&fb_helper->client); 331 332 return; 333 334 err_drm_client_init: 335 drm_fb_helper_unprepare(fb_helper); 336 kfree(fb_helper); 337 return; 338 } 339 EXPORT_SYMBOL(drm_fbdev_generic_setup); 340