1 // SPDX-License-Identifier: MIT 2 3 #include <linux/fb.h> 4 5 #include <drm/drm_crtc_helper.h> 6 #include <drm/drm_drv.h> 7 #include <drm/drm_fb_helper.h> 8 #include <drm/drm_framebuffer.h> 9 #include <drm/drm_gem_dma_helper.h> 10 11 #include <drm/drm_fbdev_dma.h> 12 13 /* 14 * struct fb_ops 15 */ 16 17 static int drm_fbdev_dma_fb_open(struct fb_info *info, int user) 18 { 19 struct drm_fb_helper *fb_helper = info->par; 20 21 /* No need to take a ref for fbcon because it unbinds on unregister */ 22 if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) 23 return -ENODEV; 24 25 return 0; 26 } 27 28 static int drm_fbdev_dma_fb_release(struct fb_info *info, int user) 29 { 30 struct drm_fb_helper *fb_helper = info->par; 31 32 if (user) 33 module_put(fb_helper->dev->driver->fops->owner); 34 35 return 0; 36 } 37 38 static void drm_fbdev_dma_fb_destroy(struct fb_info *info) 39 { 40 struct drm_fb_helper *fb_helper = info->par; 41 42 if (!fb_helper->dev) 43 return; 44 45 drm_fb_helper_fini(fb_helper); 46 47 drm_client_buffer_vunmap(fb_helper->buffer); 48 drm_client_framebuffer_delete(fb_helper->buffer); 49 drm_client_release(&fb_helper->client); 50 drm_fb_helper_unprepare(fb_helper); 51 kfree(fb_helper); 52 } 53 54 static int drm_fbdev_dma_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 55 { 56 struct drm_fb_helper *fb_helper = info->par; 57 58 return drm_gem_prime_mmap(fb_helper->buffer->gem, vma); 59 } 60 61 static const struct fb_ops drm_fbdev_dma_fb_ops = { 62 .owner = THIS_MODULE, 63 .fb_open = drm_fbdev_dma_fb_open, 64 .fb_release = drm_fbdev_dma_fb_release, 65 __FB_DEFAULT_DMAMEM_OPS_RDWR, 66 DRM_FB_HELPER_DEFAULT_OPS, 67 __FB_DEFAULT_DMAMEM_OPS_DRAW, 68 .fb_mmap = drm_fbdev_dma_fb_mmap, 69 .fb_destroy = drm_fbdev_dma_fb_destroy, 70 }; 71 72 /* 73 * struct drm_fb_helper 74 */ 75 76 static int drm_fbdev_dma_helper_fb_probe(struct drm_fb_helper *fb_helper, 77 struct drm_fb_helper_surface_size *sizes) 78 { 79 struct drm_client_dev *client = &fb_helper->client; 80 struct drm_device *dev = fb_helper->dev; 81 struct drm_client_buffer *buffer; 82 struct drm_gem_dma_object *dma_obj; 83 struct drm_framebuffer *fb; 84 struct fb_info *info; 85 u32 format; 86 struct iosys_map map; 87 int ret; 88 89 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n", 90 sizes->surface_width, sizes->surface_height, 91 sizes->surface_bpp); 92 93 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); 94 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 95 sizes->surface_height, format); 96 if (IS_ERR(buffer)) 97 return PTR_ERR(buffer); 98 dma_obj = to_drm_gem_dma_obj(buffer->gem); 99 100 fb = buffer->fb; 101 if (drm_WARN_ON(dev, fb->funcs->dirty)) { 102 ret = -ENODEV; /* damage handling not supported; use generic emulation */ 103 goto err_drm_client_buffer_delete; 104 } 105 106 ret = drm_client_buffer_vmap(buffer, &map); 107 if (ret) { 108 goto err_drm_client_buffer_delete; 109 } else if (drm_WARN_ON(dev, map.is_iomem)) { 110 ret = -ENODEV; /* I/O memory not supported; use generic emulation */ 111 goto err_drm_client_buffer_delete; 112 } 113 114 fb_helper->buffer = buffer; 115 fb_helper->fb = buffer->fb; 116 117 info = drm_fb_helper_alloc_info(fb_helper); 118 if (IS_ERR(info)) { 119 ret = PTR_ERR(info); 120 goto err_drm_client_buffer_vunmap; 121 } 122 123 drm_fb_helper_fill_info(info, fb_helper, sizes); 124 125 info->fbops = &drm_fbdev_dma_fb_ops; 126 127 /* screen */ 128 info->flags |= FBINFO_VIRTFB; /* system memory */ 129 if (dma_obj->map_noncoherent) 130 info->flags |= FBINFO_READS_FAST; /* signal caching */ 131 info->screen_size = sizes->surface_height * fb->pitches[0]; 132 info->screen_buffer = map.vaddr; 133 if (!(info->flags & FBINFO_HIDE_SMEM_START)) { 134 if (!drm_WARN_ON(dev, is_vmalloc_addr(info->screen_buffer))) 135 info->fix.smem_start = page_to_phys(virt_to_page(info->screen_buffer)); 136 } 137 info->fix.smem_len = info->screen_size; 138 139 return 0; 140 141 err_drm_client_buffer_vunmap: 142 fb_helper->fb = NULL; 143 fb_helper->buffer = NULL; 144 drm_client_buffer_vunmap(buffer); 145 err_drm_client_buffer_delete: 146 drm_client_framebuffer_delete(buffer); 147 return ret; 148 } 149 150 static const struct drm_fb_helper_funcs drm_fbdev_dma_helper_funcs = { 151 .fb_probe = drm_fbdev_dma_helper_fb_probe, 152 }; 153 154 /* 155 * struct drm_client_funcs 156 */ 157 158 static void drm_fbdev_dma_client_unregister(struct drm_client_dev *client) 159 { 160 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 161 162 if (fb_helper->info) { 163 drm_fb_helper_unregister_info(fb_helper); 164 } else { 165 drm_client_release(&fb_helper->client); 166 drm_fb_helper_unprepare(fb_helper); 167 kfree(fb_helper); 168 } 169 } 170 171 static int drm_fbdev_dma_client_restore(struct drm_client_dev *client) 172 { 173 drm_fb_helper_lastclose(client->dev); 174 175 return 0; 176 } 177 178 static int drm_fbdev_dma_client_hotplug(struct drm_client_dev *client) 179 { 180 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 181 struct drm_device *dev = client->dev; 182 int ret; 183 184 if (dev->fb_helper) 185 return drm_fb_helper_hotplug_event(dev->fb_helper); 186 187 ret = drm_fb_helper_init(dev, fb_helper); 188 if (ret) 189 goto err_drm_err; 190 191 if (!drm_drv_uses_atomic_modeset(dev)) 192 drm_helper_disable_unused_functions(dev); 193 194 ret = drm_fb_helper_initial_config(fb_helper); 195 if (ret) 196 goto err_drm_fb_helper_fini; 197 198 return 0; 199 200 err_drm_fb_helper_fini: 201 drm_fb_helper_fini(fb_helper); 202 err_drm_err: 203 drm_err(dev, "fbdev-dma: Failed to setup generic emulation (ret=%d)\n", ret); 204 return ret; 205 } 206 207 static const struct drm_client_funcs drm_fbdev_dma_client_funcs = { 208 .owner = THIS_MODULE, 209 .unregister = drm_fbdev_dma_client_unregister, 210 .restore = drm_fbdev_dma_client_restore, 211 .hotplug = drm_fbdev_dma_client_hotplug, 212 }; 213 214 /** 215 * drm_fbdev_dma_setup() - Setup fbdev emulation for GEM DMA helpers 216 * @dev: DRM device 217 * @preferred_bpp: Preferred bits per pixel for the device. 218 * 32 is used if this is zero. 219 * 220 * This function sets up fbdev emulation for GEM DMA drivers that support 221 * dumb buffers with a virtual address and that can be mmap'ed. 222 * drm_fbdev_dma_setup() shall be called after the DRM driver registered 223 * the new DRM device with drm_dev_register(). 224 * 225 * Restore, hotplug events and teardown are all taken care of. Drivers that do 226 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. 227 * Simple drivers might use drm_mode_config_helper_suspend(). 228 * 229 * This function is safe to call even when there are no connectors present. 230 * Setup will be retried on the next hotplug event. 231 * 232 * The fbdev is destroyed by drm_dev_unregister(). 233 */ 234 void drm_fbdev_dma_setup(struct drm_device *dev, unsigned int preferred_bpp) 235 { 236 struct drm_fb_helper *fb_helper; 237 int ret; 238 239 drm_WARN(dev, !dev->registered, "Device has not been registered.\n"); 240 drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n"); 241 242 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 243 if (!fb_helper) 244 return; 245 drm_fb_helper_prepare(dev, fb_helper, preferred_bpp, &drm_fbdev_dma_helper_funcs); 246 247 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_dma_client_funcs); 248 if (ret) { 249 drm_err(dev, "Failed to register client: %d\n", ret); 250 goto err_drm_client_init; 251 } 252 253 drm_client_register(&fb_helper->client); 254 255 return; 256 257 err_drm_client_init: 258 drm_fb_helper_unprepare(fb_helper); 259 kfree(fb_helper); 260 } 261 EXPORT_SYMBOL(drm_fbdev_dma_setup); 262