bochs_kms.c 7.8 KB

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