bochs_fbdev.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <drm/drm_gem_framebuffer_helper.h>
  9. /* ---------------------------------------------------------------------- */
  10. static int bochsfb_mmap(struct fb_info *info,
  11. struct vm_area_struct *vma)
  12. {
  13. struct drm_fb_helper *fb_helper = info->par;
  14. struct bochs_bo *bo = gem_to_bochs_bo(fb_helper->fb->obj[0]);
  15. return ttm_fbdev_mmap(vma, &bo->bo);
  16. }
  17. static struct fb_ops bochsfb_ops = {
  18. .owner = THIS_MODULE,
  19. DRM_FB_HELPER_DEFAULT_OPS,
  20. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  21. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  22. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  23. .fb_mmap = bochsfb_mmap,
  24. };
  25. static int bochsfb_create_object(struct bochs_device *bochs,
  26. const struct drm_mode_fb_cmd2 *mode_cmd,
  27. struct drm_gem_object **gobj_p)
  28. {
  29. struct drm_device *dev = bochs->dev;
  30. struct drm_gem_object *gobj;
  31. u32 size;
  32. int ret = 0;
  33. size = mode_cmd->pitches[0] * mode_cmd->height;
  34. ret = bochs_gem_create(dev, size, true, &gobj);
  35. if (ret)
  36. return ret;
  37. *gobj_p = gobj;
  38. return ret;
  39. }
  40. static int bochsfb_create(struct drm_fb_helper *helper,
  41. struct drm_fb_helper_surface_size *sizes)
  42. {
  43. struct bochs_device *bochs =
  44. container_of(helper, struct bochs_device, fb.helper);
  45. struct fb_info *info;
  46. struct drm_framebuffer *fb;
  47. struct drm_mode_fb_cmd2 mode_cmd;
  48. struct drm_gem_object *gobj = NULL;
  49. struct bochs_bo *bo = NULL;
  50. int size, ret;
  51. if (sizes->surface_bpp != 32)
  52. return -EINVAL;
  53. mode_cmd.width = sizes->surface_width;
  54. mode_cmd.height = sizes->surface_height;
  55. mode_cmd.pitches[0] = sizes->surface_width * 4;
  56. mode_cmd.pixel_format = DRM_FORMAT_HOST_XRGB8888;
  57. size = mode_cmd.pitches[0] * mode_cmd.height;
  58. /* alloc, pin & map bo */
  59. ret = bochsfb_create_object(bochs, &mode_cmd, &gobj);
  60. if (ret) {
  61. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  62. return ret;
  63. }
  64. bo = gem_to_bochs_bo(gobj);
  65. ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
  66. if (ret)
  67. return ret;
  68. ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
  69. if (ret) {
  70. DRM_ERROR("failed to pin fbcon\n");
  71. ttm_bo_unreserve(&bo->bo);
  72. return ret;
  73. }
  74. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages,
  75. &bo->kmap);
  76. if (ret) {
  77. DRM_ERROR("failed to kmap fbcon\n");
  78. ttm_bo_unreserve(&bo->bo);
  79. return ret;
  80. }
  81. ttm_bo_unreserve(&bo->bo);
  82. /* init fb device */
  83. info = drm_fb_helper_alloc_fbi(helper);
  84. if (IS_ERR(info)) {
  85. DRM_ERROR("Failed to allocate fbi: %ld\n", PTR_ERR(info));
  86. return PTR_ERR(info);
  87. }
  88. info->par = &bochs->fb.helper;
  89. fb = drm_gem_fbdev_fb_create(bochs->dev, sizes, 0, gobj, NULL);
  90. if (IS_ERR(fb)) {
  91. DRM_ERROR("Failed to create framebuffer: %ld\n", PTR_ERR(fb));
  92. return PTR_ERR(fb);
  93. }
  94. /* setup helper */
  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. return 0;
  107. }
  108. static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
  109. .fb_probe = bochsfb_create,
  110. };
  111. static struct drm_framebuffer *
  112. bochs_gem_fb_create(struct drm_device *dev, struct drm_file *file,
  113. const struct drm_mode_fb_cmd2 *mode_cmd)
  114. {
  115. if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888 &&
  116. mode_cmd->pixel_format != DRM_FORMAT_BGRX8888)
  117. return ERR_PTR(-EINVAL);
  118. return drm_gem_fb_create(dev, file, mode_cmd);
  119. }
  120. const struct drm_mode_config_funcs bochs_mode_funcs = {
  121. .fb_create = bochs_gem_fb_create,
  122. };
  123. int bochs_fbdev_init(struct bochs_device *bochs)
  124. {
  125. return drm_fb_helper_fbdev_setup(bochs->dev, &bochs->fb.helper,
  126. &bochs_fb_helper_funcs, 32, 1);
  127. }
  128. void bochs_fbdev_fini(struct bochs_device *bochs)
  129. {
  130. drm_fb_helper_fbdev_teardown(bochs->dev);
  131. }