bochs_kms.c 7.5 KB

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