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_gem.h> 11 #include <drm/drm_print.h> 12 13 #include <drm/drm_fbdev_ttm.h> 14 15 /* @user: 1=userspace, 0=fbcon */ 16 static int drm_fbdev_ttm_fb_open(struct fb_info *info, int user) 17 { 18 struct drm_fb_helper *fb_helper = info->par; 19 20 /* No need to take a ref for fbcon because it unbinds on unregister */ 21 if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) 22 return -ENODEV; 23 24 return 0; 25 } 26 27 static int drm_fbdev_ttm_fb_release(struct fb_info *info, int user) 28 { 29 struct drm_fb_helper *fb_helper = info->par; 30 31 if (user) 32 module_put(fb_helper->dev->driver->fops->owner); 33 34 return 0; 35 } 36 37 FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(drm_fbdev_ttm, 38 drm_fb_helper_damage_range, 39 drm_fb_helper_damage_area); 40 41 static void drm_fbdev_ttm_fb_destroy(struct fb_info *info) 42 { 43 struct drm_fb_helper *fb_helper = info->par; 44 void *shadow = info->screen_buffer; 45 46 if (!fb_helper->dev) 47 return; 48 49 fb_deferred_io_cleanup(info); 50 drm_fb_helper_fini(fb_helper); 51 vfree(shadow); 52 drm_client_framebuffer_delete(fb_helper->buffer); 53 54 drm_client_release(&fb_helper->client); 55 drm_fb_helper_unprepare(fb_helper); 56 kfree(fb_helper); 57 } 58 59 static const struct fb_ops drm_fbdev_ttm_fb_ops = { 60 .owner = THIS_MODULE, 61 .fb_open = drm_fbdev_ttm_fb_open, 62 .fb_release = drm_fbdev_ttm_fb_release, 63 FB_DEFAULT_DEFERRED_OPS(drm_fbdev_ttm), 64 DRM_FB_HELPER_DEFAULT_OPS, 65 .fb_destroy = drm_fbdev_ttm_fb_destroy, 66 }; 67 68 static void drm_fbdev_ttm_damage_blit_real(struct drm_fb_helper *fb_helper, 69 struct drm_clip_rect *clip, 70 struct iosys_map *dst) 71 { 72 struct drm_framebuffer *fb = fb_helper->fb; 73 size_t offset = clip->y1 * fb->pitches[0]; 74 size_t len = clip->x2 - clip->x1; 75 unsigned int y; 76 void *src; 77 78 switch (drm_format_info_bpp(fb->format, 0)) { 79 case 1: 80 offset += clip->x1 / 8; 81 len = DIV_ROUND_UP(len + clip->x1 % 8, 8); 82 break; 83 case 2: 84 offset += clip->x1 / 4; 85 len = DIV_ROUND_UP(len + clip->x1 % 4, 4); 86 break; 87 case 4: 88 offset += clip->x1 / 2; 89 len = DIV_ROUND_UP(len + clip->x1 % 2, 2); 90 break; 91 default: 92 offset += clip->x1 * fb->format->cpp[0]; 93 len *= fb->format->cpp[0]; 94 break; 95 } 96 97 src = fb_helper->info->screen_buffer + offset; 98 iosys_map_incr(dst, offset); /* go to first pixel within clip rect */ 99 100 for (y = clip->y1; y < clip->y2; y++) { 101 iosys_map_memcpy_to(dst, 0, src, len); 102 iosys_map_incr(dst, fb->pitches[0]); 103 src += fb->pitches[0]; 104 } 105 } 106 107 static int drm_fbdev_ttm_damage_blit(struct drm_fb_helper *fb_helper, 108 struct drm_clip_rect *clip) 109 { 110 struct drm_client_buffer *buffer = fb_helper->buffer; 111 struct iosys_map map, dst; 112 int ret; 113 114 /* 115 * We have to pin the client buffer to its current location while 116 * flushing the shadow buffer. In the general case, concurrent 117 * modesetting operations could try to move the buffer and would 118 * fail. The modeset has to be serialized by acquiring the reservation 119 * object of the underlying BO here. 120 * 121 * For fbdev emulation, we only have to protect against fbdev modeset 122 * operations. Nothing else will involve the client buffer's BO. So it 123 * is sufficient to acquire struct drm_fb_helper.lock here. 124 */ 125 mutex_lock(&fb_helper->lock); 126 127 ret = drm_client_buffer_vmap_local(buffer, &map); 128 if (ret) 129 goto out; 130 131 dst = map; 132 drm_fbdev_ttm_damage_blit_real(fb_helper, clip, &dst); 133 134 drm_client_buffer_vunmap_local(buffer); 135 136 out: 137 mutex_unlock(&fb_helper->lock); 138 139 return ret; 140 } 141 142 static int drm_fbdev_ttm_helper_fb_dirty(struct drm_fb_helper *helper, 143 struct drm_clip_rect *clip) 144 { 145 struct drm_device *dev = helper->dev; 146 int ret; 147 148 /* Call damage handlers only if necessary */ 149 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2)) 150 return 0; 151 152 ret = drm_fbdev_ttm_damage_blit(helper, clip); 153 if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret)) 154 return ret; 155 156 if (helper->fb->funcs->dirty) { 157 ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1); 158 if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret)) 159 return ret; 160 } 161 162 return 0; 163 } 164 165 static const struct drm_fb_helper_funcs drm_fbdev_ttm_helper_funcs = { 166 .fb_dirty = drm_fbdev_ttm_helper_fb_dirty, 167 }; 168 169 /* 170 * struct drm_driver 171 */ 172 173 int drm_fbdev_ttm_driver_fbdev_probe(struct drm_fb_helper *fb_helper, 174 struct drm_fb_helper_surface_size *sizes) 175 { 176 struct drm_client_dev *client = &fb_helper->client; 177 struct drm_device *dev = fb_helper->dev; 178 struct drm_client_buffer *buffer; 179 struct fb_info *info; 180 size_t screen_size; 181 void *screen_buffer; 182 u32 format; 183 int ret; 184 185 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n", 186 sizes->surface_width, sizes->surface_height, 187 sizes->surface_bpp); 188 189 format = drm_driver_legacy_fb_format(dev, sizes->surface_bpp, 190 sizes->surface_depth); 191 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 192 sizes->surface_height, format); 193 if (IS_ERR(buffer)) 194 return PTR_ERR(buffer); 195 196 fb_helper->funcs = &drm_fbdev_ttm_helper_funcs; 197 fb_helper->buffer = buffer; 198 fb_helper->fb = buffer->fb; 199 200 screen_size = buffer->gem->size; 201 screen_buffer = vzalloc(screen_size); 202 if (!screen_buffer) { 203 ret = -ENOMEM; 204 goto err_drm_client_framebuffer_delete; 205 } 206 207 info = drm_fb_helper_alloc_info(fb_helper); 208 if (IS_ERR(info)) { 209 ret = PTR_ERR(info); 210 goto err_vfree; 211 } 212 213 drm_fb_helper_fill_info(info, fb_helper, sizes); 214 215 info->fbops = &drm_fbdev_ttm_fb_ops; 216 217 /* screen */ 218 info->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST; 219 info->screen_buffer = screen_buffer; 220 info->fix.smem_len = screen_size; 221 222 /* deferred I/O */ 223 fb_helper->fbdefio.delay = HZ / 20; 224 fb_helper->fbdefio.deferred_io = drm_fb_helper_deferred_io; 225 226 info->fbdefio = &fb_helper->fbdefio; 227 ret = fb_deferred_io_init(info); 228 if (ret) 229 goto err_drm_fb_helper_release_info; 230 231 return 0; 232 233 err_drm_fb_helper_release_info: 234 drm_fb_helper_release_info(fb_helper); 235 err_vfree: 236 vfree(screen_buffer); 237 err_drm_client_framebuffer_delete: 238 fb_helper->fb = NULL; 239 fb_helper->buffer = NULL; 240 drm_client_framebuffer_delete(buffer); 241 return ret; 242 } 243 EXPORT_SYMBOL(drm_fbdev_ttm_driver_fbdev_probe); 244