bochs_fbdev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. DRM_FB_HELPER_DEFAULT_OPS,
  21. .fb_fillrect = drm_fb_helper_sys_fillrect,
  22. .fb_copyarea = drm_fb_helper_sys_copyarea,
  23. .fb_imageblit = drm_fb_helper_sys_imageblit,
  24. .fb_mmap = bochsfb_mmap,
  25. };
  26. static int bochsfb_create_object(struct bochs_device *bochs,
  27. const struct drm_mode_fb_cmd2 *mode_cmd,
  28. struct drm_gem_object **gobj_p)
  29. {
  30. struct drm_device *dev = bochs->dev;
  31. struct drm_gem_object *gobj;
  32. u32 size;
  33. int ret = 0;
  34. size = mode_cmd->pitches[0] * mode_cmd->height;
  35. ret = bochs_gem_create(dev, size, true, &gobj);
  36. if (ret)
  37. return ret;
  38. *gobj_p = gobj;
  39. return ret;
  40. }
  41. static int bochsfb_create(struct drm_fb_helper *helper,
  42. struct drm_fb_helper_surface_size *sizes)
  43. {
  44. struct bochs_device *bochs =
  45. container_of(helper, struct bochs_device, fb.helper);
  46. struct fb_info *info;
  47. struct drm_framebuffer *fb;
  48. struct drm_mode_fb_cmd2 mode_cmd;
  49. struct drm_gem_object *gobj = NULL;
  50. struct bochs_bo *bo = NULL;
  51. int size, ret;
  52. if (sizes->surface_bpp != 32)
  53. return -EINVAL;
  54. mode_cmd.width = sizes->surface_width;
  55. mode_cmd.height = sizes->surface_height;
  56. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  57. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  58. sizes->surface_depth);
  59. size = mode_cmd.pitches[0] * mode_cmd.height;
  60. /* alloc, pin & map bo */
  61. ret = bochsfb_create_object(bochs, &mode_cmd, &gobj);
  62. if (ret) {
  63. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  64. return ret;
  65. }
  66. bo = gem_to_bochs_bo(gobj);
  67. ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
  68. if (ret)
  69. return ret;
  70. ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
  71. if (ret) {
  72. DRM_ERROR("failed to pin fbcon\n");
  73. ttm_bo_unreserve(&bo->bo);
  74. return ret;
  75. }
  76. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages,
  77. &bo->kmap);
  78. if (ret) {
  79. DRM_ERROR("failed to kmap fbcon\n");
  80. ttm_bo_unreserve(&bo->bo);
  81. return ret;
  82. }
  83. ttm_bo_unreserve(&bo->bo);
  84. /* init fb device */
  85. info = drm_fb_helper_alloc_fbi(helper);
  86. if (IS_ERR(info))
  87. return PTR_ERR(info);
  88. info->par = &bochs->fb.helper;
  89. ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj);
  90. if (ret) {
  91. drm_fb_helper_release_fbi(helper);
  92. return ret;
  93. }
  94. bochs->fb.size = size;
  95. /* setup helper */
  96. fb = &bochs->fb.gfb.base;
  97. bochs->fb.helper.fb = fb;
  98. strcpy(info->fix.id, "bochsdrmfb");
  99. info->flags = FBINFO_DEFAULT;
  100. info->fbops = &bochsfb_ops;
  101. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  102. drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width,
  103. sizes->fb_height);
  104. info->screen_base = bo->kmap.virtual;
  105. info->screen_size = size;
  106. drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node);
  107. info->fix.smem_start = 0;
  108. info->fix.smem_len = size;
  109. return 0;
  110. }
  111. static int bochs_fbdev_destroy(struct bochs_device *bochs)
  112. {
  113. struct bochs_framebuffer *gfb = &bochs->fb.gfb;
  114. DRM_DEBUG_DRIVER("\n");
  115. drm_fb_helper_unregister_fbi(&bochs->fb.helper);
  116. drm_fb_helper_release_fbi(&bochs->fb.helper);
  117. if (gfb->obj) {
  118. drm_gem_object_unreference_unlocked(gfb->obj);
  119. gfb->obj = NULL;
  120. }
  121. drm_fb_helper_fini(&bochs->fb.helper);
  122. drm_framebuffer_unregister_private(&gfb->base);
  123. drm_framebuffer_cleanup(&gfb->base);
  124. return 0;
  125. }
  126. static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
  127. .fb_probe = bochsfb_create,
  128. };
  129. int bochs_fbdev_init(struct bochs_device *bochs)
  130. {
  131. int ret;
  132. drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
  133. &bochs_fb_helper_funcs);
  134. ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper,
  135. 1, 1);
  136. if (ret)
  137. return ret;
  138. ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper);
  139. if (ret)
  140. goto fini;
  141. drm_helper_disable_unused_functions(bochs->dev);
  142. ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32);
  143. if (ret)
  144. goto fini;
  145. bochs->fb.initialized = true;
  146. return 0;
  147. fini:
  148. drm_fb_helper_fini(&bochs->fb.helper);
  149. return ret;
  150. }
  151. void bochs_fbdev_fini(struct bochs_device *bochs)
  152. {
  153. if (!bochs->fb.initialized)
  154. return;
  155. bochs_fbdev_destroy(bochs);
  156. bochs->fb.initialized = false;
  157. }