bochs_fbdev.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "bochs.h"
  8. /* ---------------------------------------------------------------------- */
  9. static int bochsfb_mmap(struct fb_info *info,
  10. struct vm_area_struct *vma)
  11. {
  12. struct drm_fb_helper *fb_helper = info->par;
  13. struct bochs_device *bochs =
  14. container_of(fb_helper, struct bochs_device, fb.helper);
  15. struct bochs_bo *bo = gem_to_bochs_bo(bochs->fb.gfb.obj);
  16. return ttm_fbdev_mmap(vma, &bo->bo);
  17. }
  18. static struct fb_ops bochsfb_ops = {
  19. .owner = THIS_MODULE,
  20. .fb_check_var = drm_fb_helper_check_var,
  21. .fb_set_par = drm_fb_helper_set_par,
  22. .fb_fillrect = sys_fillrect,
  23. .fb_copyarea = sys_copyarea,
  24. .fb_imageblit = sys_imageblit,
  25. .fb_pan_display = drm_fb_helper_pan_display,
  26. .fb_blank = drm_fb_helper_blank,
  27. .fb_setcmap = drm_fb_helper_setcmap,
  28. .fb_mmap = bochsfb_mmap,
  29. };
  30. static int bochsfb_create_object(struct bochs_device *bochs,
  31. struct drm_mode_fb_cmd2 *mode_cmd,
  32. struct drm_gem_object **gobj_p)
  33. {
  34. struct drm_device *dev = bochs->dev;
  35. struct drm_gem_object *gobj;
  36. u32 size;
  37. int ret = 0;
  38. size = mode_cmd->pitches[0] * mode_cmd->height;
  39. ret = bochs_gem_create(dev, size, true, &gobj);
  40. if (ret)
  41. return ret;
  42. *gobj_p = gobj;
  43. return ret;
  44. }
  45. static int bochsfb_create(struct drm_fb_helper *helper,
  46. struct drm_fb_helper_surface_size *sizes)
  47. {
  48. struct bochs_device *bochs =
  49. container_of(helper, struct bochs_device, fb.helper);
  50. struct drm_device *dev = bochs->dev;
  51. struct fb_info *info;
  52. struct drm_framebuffer *fb;
  53. struct drm_mode_fb_cmd2 mode_cmd;
  54. struct device *device = &dev->pdev->dev;
  55. struct drm_gem_object *gobj = NULL;
  56. struct bochs_bo *bo = NULL;
  57. int size, ret;
  58. if (sizes->surface_bpp != 32)
  59. return -EINVAL;
  60. mode_cmd.width = sizes->surface_width;
  61. mode_cmd.height = sizes->surface_height;
  62. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  63. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  64. sizes->surface_depth);
  65. size = mode_cmd.pitches[0] * mode_cmd.height;
  66. /* alloc, pin & map bo */
  67. ret = bochsfb_create_object(bochs, &mode_cmd, &gobj);
  68. if (ret) {
  69. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  70. return ret;
  71. }
  72. bo = gem_to_bochs_bo(gobj);
  73. ret = ttm_bo_reserve(&bo->bo, true, false, false, NULL);
  74. if (ret)
  75. return ret;
  76. ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
  77. if (ret) {
  78. DRM_ERROR("failed to pin fbcon\n");
  79. ttm_bo_unreserve(&bo->bo);
  80. return ret;
  81. }
  82. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages,
  83. &bo->kmap);
  84. if (ret) {
  85. DRM_ERROR("failed to kmap fbcon\n");
  86. ttm_bo_unreserve(&bo->bo);
  87. return ret;
  88. }
  89. ttm_bo_unreserve(&bo->bo);
  90. /* init fb device */
  91. info = framebuffer_alloc(0, device);
  92. if (info == NULL)
  93. return -ENOMEM;
  94. info->par = &bochs->fb.helper;
  95. ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj);
  96. if (ret)
  97. return ret;
  98. bochs->fb.size = size;
  99. /* setup helper */
  100. fb = &bochs->fb.gfb.base;
  101. bochs->fb.helper.fb = fb;
  102. bochs->fb.helper.fbdev = info;
  103. strcpy(info->fix.id, "bochsdrmfb");
  104. info->flags = FBINFO_DEFAULT;
  105. info->fbops = &bochsfb_ops;
  106. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  107. drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width,
  108. sizes->fb_height);
  109. info->screen_base = bo->kmap.virtual;
  110. info->screen_size = size;
  111. drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node);
  112. info->fix.smem_start = 0;
  113. info->fix.smem_len = size;
  114. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  115. if (ret) {
  116. DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
  117. return -ENOMEM;
  118. }
  119. return 0;
  120. }
  121. static int bochs_fbdev_destroy(struct bochs_device *bochs)
  122. {
  123. struct bochs_framebuffer *gfb = &bochs->fb.gfb;
  124. struct fb_info *info;
  125. DRM_DEBUG_DRIVER("\n");
  126. if (bochs->fb.helper.fbdev) {
  127. info = bochs->fb.helper.fbdev;
  128. unregister_framebuffer(info);
  129. if (info->cmap.len)
  130. fb_dealloc_cmap(&info->cmap);
  131. framebuffer_release(info);
  132. }
  133. if (gfb->obj) {
  134. drm_gem_object_unreference_unlocked(gfb->obj);
  135. gfb->obj = NULL;
  136. }
  137. drm_fb_helper_fini(&bochs->fb.helper);
  138. drm_framebuffer_unregister_private(&gfb->base);
  139. drm_framebuffer_cleanup(&gfb->base);
  140. return 0;
  141. }
  142. void bochs_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  143. u16 blue, int regno)
  144. {
  145. }
  146. void bochs_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  147. u16 *blue, int regno)
  148. {
  149. *red = regno;
  150. *green = regno;
  151. *blue = regno;
  152. }
  153. static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
  154. .gamma_set = bochs_fb_gamma_set,
  155. .gamma_get = bochs_fb_gamma_get,
  156. .fb_probe = bochsfb_create,
  157. };
  158. int bochs_fbdev_init(struct bochs_device *bochs)
  159. {
  160. int ret;
  161. drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
  162. &bochs_fb_helper_funcs);
  163. ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper,
  164. 1, 1);
  165. if (ret)
  166. return ret;
  167. ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper);
  168. if (ret)
  169. goto fini;
  170. drm_helper_disable_unused_functions(bochs->dev);
  171. ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32);
  172. if (ret)
  173. goto fini;
  174. bochs->fb.initialized = true;
  175. return 0;
  176. fini:
  177. drm_fb_helper_fini(&bochs->fb.helper);
  178. return ret;
  179. }
  180. void bochs_fbdev_fini(struct bochs_device *bochs)
  181. {
  182. if (!bochs->fb.initialized)
  183. return;
  184. bochs_fbdev_destroy(bochs);
  185. bochs->fb.initialized = false;
  186. }