bochs_fbdev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 = drm_fb_helper_sys_fillrect,
  23. .fb_copyarea = drm_fb_helper_sys_copyarea,
  24. .fb_imageblit = drm_fb_helper_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 fb_info *info;
  51. struct drm_framebuffer *fb;
  52. struct drm_mode_fb_cmd2 mode_cmd;
  53. struct drm_gem_object *gobj = NULL;
  54. struct bochs_bo *bo = NULL;
  55. int size, ret;
  56. if (sizes->surface_bpp != 32)
  57. return -EINVAL;
  58. mode_cmd.width = sizes->surface_width;
  59. mode_cmd.height = sizes->surface_height;
  60. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  61. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  62. sizes->surface_depth);
  63. size = mode_cmd.pitches[0] * mode_cmd.height;
  64. /* alloc, pin & map bo */
  65. ret = bochsfb_create_object(bochs, &mode_cmd, &gobj);
  66. if (ret) {
  67. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  68. return ret;
  69. }
  70. bo = gem_to_bochs_bo(gobj);
  71. ret = ttm_bo_reserve(&bo->bo, true, false, false, NULL);
  72. if (ret)
  73. return ret;
  74. ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
  75. if (ret) {
  76. DRM_ERROR("failed to pin fbcon\n");
  77. ttm_bo_unreserve(&bo->bo);
  78. return ret;
  79. }
  80. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages,
  81. &bo->kmap);
  82. if (ret) {
  83. DRM_ERROR("failed to kmap fbcon\n");
  84. ttm_bo_unreserve(&bo->bo);
  85. return ret;
  86. }
  87. ttm_bo_unreserve(&bo->bo);
  88. /* init fb device */
  89. info = drm_fb_helper_alloc_fbi(helper);
  90. if (IS_ERR(info))
  91. return PTR_ERR(info);
  92. info->par = &bochs->fb.helper;
  93. ret = bochs_framebuffer_init(bochs->dev, &bochs->fb.gfb, &mode_cmd, gobj);
  94. if (ret) {
  95. drm_fb_helper_release_fbi(helper);
  96. return ret;
  97. }
  98. bochs->fb.size = size;
  99. /* setup helper */
  100. fb = &bochs->fb.gfb.base;
  101. bochs->fb.helper.fb = fb;
  102. strcpy(info->fix.id, "bochsdrmfb");
  103. info->flags = FBINFO_DEFAULT;
  104. info->fbops = &bochsfb_ops;
  105. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  106. drm_fb_helper_fill_var(info, &bochs->fb.helper, sizes->fb_width,
  107. sizes->fb_height);
  108. info->screen_base = bo->kmap.virtual;
  109. info->screen_size = size;
  110. drm_vma_offset_remove(&bo->bo.bdev->vma_manager, &bo->bo.vma_node);
  111. info->fix.smem_start = 0;
  112. info->fix.smem_len = size;
  113. return 0;
  114. }
  115. static int bochs_fbdev_destroy(struct bochs_device *bochs)
  116. {
  117. struct bochs_framebuffer *gfb = &bochs->fb.gfb;
  118. DRM_DEBUG_DRIVER("\n");
  119. drm_fb_helper_unregister_fbi(&bochs->fb.helper);
  120. drm_fb_helper_release_fbi(&bochs->fb.helper);
  121. if (gfb->obj) {
  122. drm_gem_object_unreference_unlocked(gfb->obj);
  123. gfb->obj = NULL;
  124. }
  125. drm_fb_helper_fini(&bochs->fb.helper);
  126. drm_framebuffer_unregister_private(&gfb->base);
  127. drm_framebuffer_cleanup(&gfb->base);
  128. return 0;
  129. }
  130. void bochs_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  131. u16 blue, int regno)
  132. {
  133. }
  134. void bochs_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  135. u16 *blue, int regno)
  136. {
  137. *red = regno;
  138. *green = regno;
  139. *blue = regno;
  140. }
  141. static const struct drm_fb_helper_funcs bochs_fb_helper_funcs = {
  142. .gamma_set = bochs_fb_gamma_set,
  143. .gamma_get = bochs_fb_gamma_get,
  144. .fb_probe = bochsfb_create,
  145. };
  146. int bochs_fbdev_init(struct bochs_device *bochs)
  147. {
  148. int ret;
  149. drm_fb_helper_prepare(bochs->dev, &bochs->fb.helper,
  150. &bochs_fb_helper_funcs);
  151. ret = drm_fb_helper_init(bochs->dev, &bochs->fb.helper,
  152. 1, 1);
  153. if (ret)
  154. return ret;
  155. ret = drm_fb_helper_single_add_all_connectors(&bochs->fb.helper);
  156. if (ret)
  157. goto fini;
  158. drm_helper_disable_unused_functions(bochs->dev);
  159. ret = drm_fb_helper_initial_config(&bochs->fb.helper, 32);
  160. if (ret)
  161. goto fini;
  162. bochs->fb.initialized = true;
  163. return 0;
  164. fini:
  165. drm_fb_helper_fini(&bochs->fb.helper);
  166. return ret;
  167. }
  168. void bochs_fbdev_fini(struct bochs_device *bochs)
  169. {
  170. if (!bochs->fb.initialized)
  171. return;
  172. bochs_fbdev_destroy(bochs);
  173. bochs->fb.initialized = false;
  174. }