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, |
| 25 | .fb_check_var = drm_fb_helper_check_var, |
| 26 | .fb_set_par = drm_fb_helper_set_par, |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 27 | .fb_fillrect = drm_fb_helper_sys_fillrect, |
| 28 | .fb_copyarea = drm_fb_helper_sys_copyarea, |
| 29 | .fb_imageblit = drm_fb_helper_sys_imageblit, |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 30 | .fb_pan_display = drm_fb_helper_pan_display, |
| 31 | .fb_blank = drm_fb_helper_blank, |
| 32 | .fb_setcmap = drm_fb_helper_setcmap, |
Gerd Hoffmann | 49b70a3 | 2014-11-19 12:28:11 +0100 | [diff] [blame] | 33 | .fb_mmap = bochsfb_mmap, |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static int bochsfb_create_object(struct bochs_device *bochs, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 37 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 38 | struct drm_gem_object **gobj_p) |
| 39 | { |
| 40 | struct drm_device *dev = bochs->dev; |
| 41 | struct drm_gem_object *gobj; |
| 42 | u32 size; |
| 43 | int ret = 0; |
| 44 | |
| 45 | size = mode_cmd->pitches[0] * mode_cmd->height; |
| 46 | ret = bochs_gem_create(dev, size, true, &gobj); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | |
| 50 | *gobj_p = gobj; |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | static int bochsfb_create(struct drm_fb_helper *helper, |
| 55 | struct drm_fb_helper_surface_size *sizes) |
| 56 | { |
| 57 | struct bochs_device *bochs = |
| 58 | container_of(helper, struct bochs_device, fb.helper); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 59 | struct fb_info *info; |
| 60 | struct drm_framebuffer *fb; |
| 61 | struct drm_mode_fb_cmd2 mode_cmd; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 62 | struct drm_gem_object *gobj = NULL; |
| 63 | struct bochs_bo *bo = NULL; |
| 64 | int size, ret; |
| 65 | |
| 66 | if (sizes->surface_bpp != 32) |
| 67 | return -EINVAL; |
| 68 | |
| 69 | mode_cmd.width = sizes->surface_width; |
| 70 | mode_cmd.height = sizes->surface_height; |
| 71 | mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8); |
| 72 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 73 | sizes->surface_depth); |
| 74 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 75 | |
| 76 | /* alloc, pin & map bo */ |
| 77 | ret = bochsfb_create_object(bochs, &mode_cmd, &gobj); |
| 78 | if (ret) { |
| 79 | DRM_ERROR("failed to create fbcon backing object %d\n", ret); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | bo = gem_to_bochs_bo(gobj); |
| 84 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 85 | ret = ttm_bo_reserve(&bo->bo, true, false, NULL); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 86 | if (ret) |
| 87 | return ret; |
| 88 | |
| 89 | ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL); |
| 90 | if (ret) { |
| 91 | DRM_ERROR("failed to pin fbcon\n"); |
| 92 | ttm_bo_unreserve(&bo->bo); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, |
| 97 | &bo->kmap); |
| 98 | if (ret) { |
| 99 | DRM_ERROR("failed to kmap fbcon\n"); |
| 100 | ttm_bo_unreserve(&bo->bo); |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | ttm_bo_unreserve(&bo->bo); |
| 105 | |
| 106 | /* init fb device */ |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 107 | info = drm_fb_helper_alloc_fbi(helper); |
| 108 | if (IS_ERR(info)) |
| 109 | return PTR_ERR(info); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 110 | |
| 111 | info->par = &bochs->fb.helper; |
| 112 | |
| 113 | ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj); |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 114 | if (ret) { |
| 115 | drm_fb_helper_release_fbi(helper); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 116 | return ret; |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 117 | } |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 118 | |
| 119 | bochs->fb.size = size; |
| 120 | |
| 121 | /* setup helper */ |
| 122 | fb = &bochs->fb.gfb.base; |
| 123 | bochs->fb.helper.fb = fb; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 124 | |
| 125 | strcpy(info->fix.id, "bochsdrmfb"); |
| 126 | |
| 127 | info->flags = FBINFO_DEFAULT; |
| 128 | info->fbops = &bochsfb_ops; |
| 129 | |
| 130 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); |
| 131 | drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width, |
| 132 | sizes->fb_height); |
| 133 | |
| 134 | info->screen_base = bo->kmap.virtual; |
| 135 | info->screen_size = size; |
| 136 | |
Gerd Hoffmann | 49b70a3 | 2014-11-19 12:28:11 +0100 | [diff] [blame] | 137 | drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node); |
| 138 | info->fix.smem_start = 0; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 139 | info->fix.smem_len = size; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 140 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static int bochs_fbdev_destroy(struct bochs_device *bochs) |
| 145 | { |
| 146 | struct bochs_framebuffer *gfb = &bochs->fb.gfb; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 147 | |
| 148 | DRM_DEBUG_DRIVER("\n"); |
| 149 | |
Archit Taneja | 6a75297 | 2015-07-31 16:21:59 +0530 | [diff] [blame] | 150 | drm_fb_helper_unregister_fbi(&bochs->fb.helper); |
| 151 | drm_fb_helper_release_fbi(&bochs->fb.helper); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 152 | |
| 153 | if (gfb->obj) { |
| 154 | drm_gem_object_unreference_unlocked(gfb->obj); |
| 155 | gfb->obj = NULL; |
| 156 | } |
| 157 | |
| 158 | drm_fb_helper_fini(&bochs->fb.helper); |
| 159 | drm_framebuffer_unregister_private(&gfb->base); |
| 160 | drm_framebuffer_cleanup(&gfb->base); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Thierry Reding | 3a49387 | 2014-06-27 17:19:23 +0200 | [diff] [blame] | 165 | static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = { |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 166 | .fb_probe = bochsfb_create, |
| 167 | }; |
| 168 | |
| 169 | int bochs_fbdev_init(struct bochs_device *bochs) |
| 170 | { |
| 171 | int ret; |
| 172 | |
Thierry Reding | 10a2310 | 2014-06-27 17:19:24 +0200 | [diff] [blame] | 173 | drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper, |
| 174 | &bochs_fb_helper_funcs); |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 175 | |
| 176 | ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper, |
| 177 | 1, 1); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 181 | ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper); |
| 182 | if (ret) |
| 183 | goto fini; |
| 184 | |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 185 | drm_helper_disable_unused_functions(bochs->dev); |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 186 | |
| 187 | ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32); |
| 188 | if (ret) |
| 189 | goto fini; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 190 | |
| 191 | bochs->fb.initialized = true; |
| 192 | return 0; |
Thierry Reding | 01934c2 | 2014-12-19 11:21:32 +0100 | [diff] [blame] | 193 | |
| 194 | fini: |
| 195 | drm_fb_helper_fini(&bochs->fb.helper); |
| 196 | return ret; |
Gerd Hoffmann | 0a6659b | 2013-12-17 18:04:46 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void bochs_fbdev_fini(struct bochs_device *bochs) |
| 200 | { |
| 201 | if (!bochs->fb.initialized) |
| 202 | return; |
| 203 | |
| 204 | bochs_fbdev_destroy(bochs); |
| 205 | bochs->fb.initialized = false; |
| 206 | } |