bochs_kms.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_plane_helper.h>
  9. static int defx = 1024;
  10. static int defy = 768;
  11. module_param(defx, int, 0444);
  12. module_param(defy, int, 0444);
  13. MODULE_PARM_DESC(defx, "default x resolution");
  14. MODULE_PARM_DESC(defy, "default y resolution");
  15. /* ---------------------------------------------------------------------- */
  16. static void bochs_crtc_dpms(struct drm_crtc *crtc, int mode)
  17. {
  18. switch (mode) {
  19. case DRM_MODE_DPMS_ON:
  20. case DRM_MODE_DPMS_STANDBY:
  21. case DRM_MODE_DPMS_SUSPEND:
  22. case DRM_MODE_DPMS_OFF:
  23. default:
  24. return;
  25. }
  26. }
  27. static int bochs_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  28. struct drm_framebuffer *old_fb)
  29. {
  30. struct bochs_device *bochs =
  31. container_of(crtc, struct bochs_device, crtc);
  32. struct bochs_bo *bo;
  33. u64 gpu_addr = 0;
  34. int ret;
  35. if (old_fb) {
  36. bo = gem_to_bochs_bo(old_fb->obj[0]);
  37. ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
  38. if (ret) {
  39. DRM_ERROR("failed to reserve old_fb bo\n");
  40. } else {
  41. bochs_bo_unpin(bo);
  42. ttm_bo_unreserve(&bo->bo);
  43. }
  44. }
  45. if (WARN_ON(crtc->primary->fb == NULL))
  46. return -EINVAL;
  47. bo = gem_to_bochs_bo(crtc->primary->fb->obj[0]);
  48. ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
  49. if (ret)
  50. return ret;
  51. ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  52. if (ret) {
  53. ttm_bo_unreserve(&bo->bo);
  54. return ret;
  55. }
  56. ttm_bo_unreserve(&bo->bo);
  57. bochs_hw_setbase(bochs, x, y, gpu_addr);
  58. return 0;
  59. }
  60. static int bochs_crtc_mode_set(struct drm_crtc *crtc,
  61. struct drm_display_mode *mode,
  62. struct drm_display_mode *adjusted_mode,
  63. int x, int y, struct drm_framebuffer *old_fb)
  64. {
  65. struct bochs_device *bochs =
  66. container_of(crtc, struct bochs_device, crtc);
  67. if (WARN_ON(crtc->primary->fb == NULL))
  68. return -EINVAL;
  69. bochs_hw_setmode(bochs, mode, crtc->primary->fb->format);
  70. bochs_crtc_mode_set_base(crtc, x, y, old_fb);
  71. return 0;
  72. }
  73. static void bochs_crtc_prepare(struct drm_crtc *crtc)
  74. {
  75. }
  76. static void bochs_crtc_commit(struct drm_crtc *crtc)
  77. {
  78. }
  79. static int bochs_crtc_page_flip(struct drm_crtc *crtc,
  80. struct drm_framebuffer *fb,
  81. struct drm_pending_vblank_event *event,
  82. uint32_t page_flip_flags,
  83. struct drm_modeset_acquire_ctx *ctx)
  84. {
  85. struct bochs_device *bochs =
  86. container_of(crtc, struct bochs_device, crtc);
  87. struct drm_framebuffer *old_fb = crtc->primary->fb;
  88. unsigned long irqflags;
  89. crtc->primary->fb = fb;
  90. bochs_crtc_mode_set_base(crtc, 0, 0, old_fb);
  91. if (event) {
  92. spin_lock_irqsave(&bochs->dev->event_lock, irqflags);
  93. drm_crtc_send_vblank_event(crtc, event);
  94. spin_unlock_irqrestore(&bochs->dev->event_lock, irqflags);
  95. }
  96. return 0;
  97. }
  98. /* These provide the minimum set of functions required to handle a CRTC */
  99. static const struct drm_crtc_funcs bochs_crtc_funcs = {
  100. .set_config = drm_crtc_helper_set_config,
  101. .destroy = drm_crtc_cleanup,
  102. .page_flip = bochs_crtc_page_flip,
  103. };
  104. static const struct drm_crtc_helper_funcs bochs_helper_funcs = {
  105. .dpms = bochs_crtc_dpms,
  106. .mode_set = bochs_crtc_mode_set,
  107. .mode_set_base = bochs_crtc_mode_set_base,
  108. .prepare = bochs_crtc_prepare,
  109. .commit = bochs_crtc_commit,
  110. };
  111. static const uint32_t bochs_formats[] = {
  112. DRM_FORMAT_XRGB8888,
  113. DRM_FORMAT_BGRX8888,
  114. };
  115. static struct drm_plane *bochs_primary_plane(struct drm_device *dev)
  116. {
  117. struct drm_plane *primary;
  118. int ret;
  119. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  120. if (primary == NULL) {
  121. DRM_DEBUG_KMS("Failed to allocate primary plane\n");
  122. return NULL;
  123. }
  124. ret = drm_universal_plane_init(dev, primary, 0,
  125. &drm_primary_helper_funcs,
  126. bochs_formats,
  127. ARRAY_SIZE(bochs_formats),
  128. NULL,
  129. DRM_PLANE_TYPE_PRIMARY, NULL);
  130. if (ret) {
  131. kfree(primary);
  132. primary = NULL;
  133. }
  134. return primary;
  135. }
  136. static void bochs_crtc_init(struct drm_device *dev)
  137. {
  138. struct bochs_device *bochs = dev->dev_private;
  139. struct drm_crtc *crtc = &bochs->crtc;
  140. struct drm_plane *primary = bochs_primary_plane(dev);
  141. drm_crtc_init_with_planes(dev, crtc, primary, NULL,
  142. &bochs_crtc_funcs, NULL);
  143. drm_crtc_helper_add(crtc, &bochs_helper_funcs);
  144. }
  145. static void bochs_encoder_mode_set(struct drm_encoder *encoder,
  146. struct drm_display_mode *mode,
  147. struct drm_display_mode *adjusted_mode)
  148. {
  149. }
  150. static void bochs_encoder_dpms(struct drm_encoder *encoder, int state)
  151. {
  152. }
  153. static void bochs_encoder_prepare(struct drm_encoder *encoder)
  154. {
  155. }
  156. static void bochs_encoder_commit(struct drm_encoder *encoder)
  157. {
  158. }
  159. static const struct drm_encoder_helper_funcs bochs_encoder_helper_funcs = {
  160. .dpms = bochs_encoder_dpms,
  161. .mode_set = bochs_encoder_mode_set,
  162. .prepare = bochs_encoder_prepare,
  163. .commit = bochs_encoder_commit,
  164. };
  165. static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
  166. .destroy = drm_encoder_cleanup,
  167. };
  168. static void bochs_encoder_init(struct drm_device *dev)
  169. {
  170. struct bochs_device *bochs = dev->dev_private;
  171. struct drm_encoder *encoder = &bochs->encoder;
  172. encoder->possible_crtcs = 0x1;
  173. drm_encoder_init(dev, encoder, &bochs_encoder_encoder_funcs,
  174. DRM_MODE_ENCODER_DAC, NULL);
  175. drm_encoder_helper_add(encoder, &bochs_encoder_helper_funcs);
  176. }
  177. static int bochs_connector_get_modes(struct drm_connector *connector)
  178. {
  179. int count;
  180. count = drm_add_modes_noedid(connector, 8192, 8192);
  181. drm_set_preferred_mode(connector, defx, defy);
  182. return count;
  183. }
  184. static enum drm_mode_status bochs_connector_mode_valid(struct drm_connector *connector,
  185. struct drm_display_mode *mode)
  186. {
  187. struct bochs_device *bochs =
  188. container_of(connector, struct bochs_device, connector);
  189. unsigned long size = mode->hdisplay * mode->vdisplay * 4;
  190. /*
  191. * Make sure we can fit two framebuffers into video memory.
  192. * This allows up to 1600x1200 with 16 MB (default size).
  193. * If you want more try this:
  194. * 'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
  195. */
  196. if (size * 2 > bochs->fb_size)
  197. return MODE_BAD;
  198. return MODE_OK;
  199. }
  200. static struct drm_encoder *
  201. bochs_connector_best_encoder(struct drm_connector *connector)
  202. {
  203. int enc_id = connector->encoder_ids[0];
  204. /* pick the encoder ids */
  205. if (enc_id)
  206. return drm_encoder_find(connector->dev, NULL, enc_id);
  207. return NULL;
  208. }
  209. static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
  210. .get_modes = bochs_connector_get_modes,
  211. .mode_valid = bochs_connector_mode_valid,
  212. .best_encoder = bochs_connector_best_encoder,
  213. };
  214. static const struct drm_connector_funcs bochs_connector_connector_funcs = {
  215. .dpms = drm_helper_connector_dpms,
  216. .fill_modes = drm_helper_probe_single_connector_modes,
  217. .destroy = drm_connector_cleanup,
  218. };
  219. static void bochs_connector_init(struct drm_device *dev)
  220. {
  221. struct bochs_device *bochs = dev->dev_private;
  222. struct drm_connector *connector = &bochs->connector;
  223. drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
  224. DRM_MODE_CONNECTOR_VIRTUAL);
  225. drm_connector_helper_add(connector,
  226. &bochs_connector_connector_helper_funcs);
  227. drm_connector_register(connector);
  228. }
  229. int bochs_kms_init(struct bochs_device *bochs)
  230. {
  231. drm_mode_config_init(bochs->dev);
  232. bochs->mode_config_initialized = true;
  233. bochs->dev->mode_config.max_width = 8192;
  234. bochs->dev->mode_config.max_height = 8192;
  235. bochs->dev->mode_config.fb_base = bochs->fb_base;
  236. bochs->dev->mode_config.preferred_depth = 24;
  237. bochs->dev->mode_config.prefer_shadow = 0;
  238. bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
  239. bochs->dev->mode_config.funcs = &bochs_mode_funcs;
  240. bochs_crtc_init(bochs->dev);
  241. bochs_encoder_init(bochs->dev);
  242. bochs_connector_init(bochs->dev);
  243. drm_connector_attach_encoder(&bochs->connector,
  244. &bochs->encoder);
  245. return 0;
  246. }
  247. void bochs_kms_fini(struct bochs_device *bochs)
  248. {
  249. if (bochs->mode_config_initialized) {
  250. drm_mode_config_cleanup(bochs->dev);
  251. bochs->mode_config_initialized = false;
  252. }
  253. }