bochs_fbdev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_cfb_fillrect,
  22. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  23. .fb_imageblit = drm_fb_helper_cfb_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. return ret;
  92. bochs->fb.size = size;
  93. /* setup helper */
  94. fb = &bochs->fb.gfb.base;
  95. bochs->fb.helper.fb = fb;
  96. strcpy(info->fix.id, "bochsdrmfb");
  97. info->fbops = &bochsfb_ops;
  98. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  99. drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width,
  100. sizes->fb_height);
  101. info->screen_base = bo->kmap.virtual;
  102. info->screen_size = size;
  103. drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node);
  104. info->fix.smem_start = 0;
  105. info->fix.smem_len = size;
  106. bochs->fb.initialized = true;
  107. return 0;
  108. }
  109. static int bochs_fbdev_destroy(struct bochs_device *bochs)
  110. {
  111. struct bochs_framebuffer *gfb = &bochs->fb.gfb;
  112. DRM_DEBUG_DRIVER("\n");
  113. drm_fb_helper_unregister_fbi(&bochs->fb.helper);
  114. if (gfb->obj) {
  115. drm_gem_object_unreference_unlocked(gfb->obj);
  116. gfb->obj = NULL;
  117. }
  118. drm_framebuffer_unregister_private(&gfb->base);
  119. drm_framebuffer_cleanup(&gfb->base);
  120. return 0;
  121. }
  122. static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
  123. .fb_probe = bochsfb_create,
  124. };
  125. int bochs_fbdev_init(struct bochs_device *bochs)
  126. {
  127. int ret;
  128. drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
  129. &bochs_fb_helper_funcs);
  130. ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper, 1);
  131. if (ret)
  132. return ret;
  133. ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper);
  134. if (ret)
  135. goto fini;
  136. drm_helper_disable_unused_functions(bochs->dev);
  137. ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32);
  138. if (ret)
  139. goto fini;
  140. return 0;
  141. fini:
  142. drm_fb_helper_fini(&bochs->fb.helper);
  143. return ret;
  144. }
  145. void bochs_fbdev_fini(struct bochs_device *bochs)
  146. {
  147. if (bochs->fb.initialized)
  148. bochs_fbdev_destroy(bochs);
  149. if (bochs->fb.helper.fbdev)
  150. drm_fb_helper_fini(&bochs->fb.helper);
  151. bochs->fb.initialized = false;
  152. }