Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License, or |
| 5 | * (at your option) any later version. |
| 6 | */ |
| 7 | |
| 8 | #include "bochs.h" |
| 9 | |
| 10 | /* ---------------------------------------------------------------------- */ |
| 11 | |
Gerd Hoffmann | 49b70a3 | 2014-11-19 12:28:11 +0100 | [diff] [blame] | 12 | static int bochsfb_mmap(struct fb_info *info, |
| 13 | struct vm_area_struct *vma) |
| 14 | { |
| 15 | struct drm_fb_helper *fb_helper = info->par; |
| 16 | struct bochs_device *bochs = |
| 17 | container_of(fb_helper, struct bochs_device, fb.helper); |
| 18 | struct bochs_bo *bo = gem_to_bochs_bo(bochs->fb.gfb.obj); |
| 19 | |
| 20 | return ttm_fbdev_mmap(vma, &bo->bo); |
| 21 | } |
| 22 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 23 | static struct fb_ops bochsfb_ops = { |
| 24 | .owner = THIS_MODULE, |
Stefan Christ | d4d938c | 2016-11-14 00:03:15 +0100 | [diff] [blame] | 25 | DRM_FB_HELPER_DEFAULT_OPS, |
Mark Cave-Ayland | 931e8c6 | 2017-07-02 22:52:43 +0100 | [diff] [blame] | 26 | .fb_fillrect = drm_fb_helper_cfb_fillrect, |
| 27 | .fb_copyarea = drm_fb_helper_cfb_copyarea, |
| 28 | .fb_imageblit = drm_fb_helper_cfb_imageblit, |
Gerd Hoffmann | 49b70a3 | 2014-11-19 12:28:11 +0100 | [diff] [blame] | 29 | .fb_mmap = bochsfb_mmap, |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static int bochsfb_create_object(struct bochs_device *bochs, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 33 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 34 | struct drm_gem_object **gobj_p) |
| 35 | { |
| 36 | struct drm_device *dev = bochs->dev; |
| 37 | struct drm_gem_object *gobj; |
| 38 | u32 size; |
| 39 | int ret = 0; |
| 40 | |
| 41 | size = mode_cmd->pitches[0] * mode_cmd->height; |
| 42 | ret = bochs_gem_create(dev, size, true, &gobj); |
| 43 | if (ret) |
| 44 | return ret; |
| 45 | |
| 46 | *gobj_p = gobj; |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | static int bochsfb_create(struct drm_fb_helper *helper, |
| 51 | struct drm_fb_helper_surface_size *sizes) |
| 52 | { |
| 53 | struct bochs_device *bochs = |
| 54 | container_of(helper, struct bochs_device, fb.helper); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 55 | struct fb_info *info; |
| 56 | struct drm_framebuffer *fb; |
| 57 | struct drm_mode_fb_cmd2 mode_cmd; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 58 | struct drm_gem_object *gobj = NULL; |
| 59 | struct bochs_bo *bo = NULL; |
| 60 | int size, ret; |
| 61 | |
| 62 | if (sizes->surface_bpp != 32) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | mode_cmd.width = sizes->surface_width; |
| 66 | mode_cmd.height = sizes->surface_height; |
| 67 | mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8); |
| 68 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 69 | sizes->surface_depth); |
| 70 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 71 | |
| 72 | /* alloc, pin & map bo */ |
| 73 | ret = bochsfb_create_object(bochs, &mode_cmd, &gobj); |
| 74 | if (ret) { |
| 75 | DRM_ERROR("failed to create fbcon backing object %d\n", ret); |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | bo = gem_to_bochs_bo(gobj); |
| 80 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 81 | ret = ttm_bo_reserve(&bo->bo, true, false, NULL); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 82 | if (ret) |
| 83 | return ret; |
| 84 | |
| 85 | ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL); |
| 86 | if (ret) { |
| 87 | DRM_ERROR("failed to pin fbcon\n"); |
| 88 | ttm_bo_unreserve(&bo->bo); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, |
| 93 | &bo->kmap); |
| 94 | if (ret) { |
| 95 | DRM_ERROR("failed to kmap fbcon\n"); |
| 96 | ttm_bo_unreserve(&bo->bo); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | ttm_bo_unreserve(&bo->bo); |
| 101 | |
| 102 | /* init fb device */ |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 103 | info = drm_fb_helper_alloc_fbi(helper); |
| 104 | if (IS_ERR(info)) |
| 105 | return PTR_ERR(info); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 106 | |
| 107 | info->par = &bochs->fb.helper; |
| 108 | |
| 109 | ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj); |
Daniel Vetter | da7bdda | 2017-02-07 17:16:03 +0100 | [diff] [blame] | 110 | if (ret) |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 111 | return ret; |
| 112 | |
| 113 | bochs->fb.size = size; |
| 114 | |
| 115 | /* setup helper */ |
| 116 | fb = &bochs->fb.gfb.base; |
| 117 | bochs->fb.helper.fb = fb; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 118 | |
| 119 | strcpy(info->fix.id, "bochsdrmfb"); |
| 120 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 121 | info->fbops = &bochsfb_ops; |
| 122 | |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 123 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 124 | drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width, |
| 125 | sizes->fb_height); |
| 126 | |
| 127 | info->screen_base = bo->kmap.virtual; |
| 128 | info->screen_size = size; |
| 129 | |
Gerd Hoffmann | 49b70a3 | 2014-11-19 12:28:11 +0100 | [diff] [blame] | 130 | drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node); |
| 131 | info->fix.smem_start = 0; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 132 | info->fix.smem_len = size; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 133 | |
Gabriel Krisman Bertazi | 4fa13db | 2017-03-17 15:14:09 -0300 | [diff] [blame] | 134 | bochs->fb.initialized = true; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int bochs_fbdev_destroy(struct bochs_device *bochs) |
| 139 | { |
| 140 | struct bochs_framebuffer *gfb = &bochs->fb.gfb; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 141 | |
| 142 | DRM_DEBUG_DRIVER("\n"); |
| 143 | |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 144 | drm_fb_helper_unregister_fbi(&bochs->fb.helper); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 145 | |
| 146 | if (gfb->obj) { |
| 147 | drm_gem_object_unreference_unlocked(gfb->obj); |
| 148 | gfb->obj = NULL; |
| 149 | } |
| 150 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 151 | drm_framebuffer_unregister_private(&gfb->base); |
| 152 | drm_framebuffer_cleanup(&gfb->base); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 157 | static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = { |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 158 | .fb_probe = bochsfb_create, |
| 159 | }; |
| 160 | |
| 161 | int bochs_fbdev_init(struct bochs_device *bochs) |
| 162 | { |
| 163 | int ret; |
| 164 | |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 165 | drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper, |
| 166 | &bochs_fb_helper_funcs); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 167 | |
Gabriel Krisman Bertazi | e4563f6 | 2017-02-02 14:26:40 -0200 | [diff] [blame] | 168 | ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper, 1); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 169 | if (ret) |
| 170 | return ret; |
| 171 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 172 | ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper); |
| 173 | if (ret) |
| 174 | goto fini; |
| 175 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 176 | drm_helper_disable_unused_functions(bochs->dev); |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 177 | |
| 178 | ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32); |
| 179 | if (ret) |
| 180 | goto fini; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 181 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 182 | return 0; |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 183 | |
| 184 | fini: |
| 185 | drm_fb_helper_fini(&bochs->fb.helper); |
| 186 | return ret; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void bochs_fbdev_fini(struct bochs_device *bochs) |
| 190 | { |
Gabriel Krisman Bertazi | 4fa13db | 2017-03-17 15:14:09 -0300 | [diff] [blame] | 191 | if (bochs->fb.initialized) |
| 192 | bochs_fbdev_destroy(bochs); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 193 | |
Gabriel Krisman Bertazi | c041131 | 2017-03-24 01:54:44 -0300 | [diff] [blame] | 194 | if (bochs->fb.helper.fbdev) |
| 195 | drm_fb_helper_fini(&bochs->fb.helper); |
| 196 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 197 | bochs->fb.initialized = false; |
| 198 | } |