bochs_fbdev.c 4.6 KB

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