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.
9 #include <drm/drm_plane_helper.h>
11 static int defx = 1024;
12 static int defy = 768;
14 module_param(defx, int, 0444);
15 module_param(defy, int, 0444);
16 MODULE_PARM_DESC(defx, "default x resolution");
17 MODULE_PARM_DESC(defy, "default y resolution");
19 /* ---------------------------------------------------------------------- */
21 static void bochs_crtc_dpms(struct drm_crtc *crtc, int mode)
24 case DRM_MODE_DPMS_ON:
25 case DRM_MODE_DPMS_STANDBY:
26 case DRM_MODE_DPMS_SUSPEND:
27 case DRM_MODE_DPMS_OFF:
33 static int bochs_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
34 struct drm_framebuffer *old_fb)
36 struct bochs_device *bochs =
37 container_of(crtc, struct bochs_device, crtc);
38 struct bochs_framebuffer *bochs_fb;
44 bochs_fb = to_bochs_framebuffer(old_fb);
45 bo = gem_to_bochs_bo(bochs_fb->obj);
46 ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
48 DRM_ERROR("failed to reserve old_fb bo\n");
51 ttm_bo_unreserve(&bo->bo);
55 if (WARN_ON(crtc->primary->fb == NULL))
58 bochs_fb = to_bochs_framebuffer(crtc->primary->fb);
59 bo = gem_to_bochs_bo(bochs_fb->obj);
60 ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
64 ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
66 ttm_bo_unreserve(&bo->bo);
70 ttm_bo_unreserve(&bo->bo);
71 bochs_hw_setbase(bochs, x, y, gpu_addr);
75 static int bochs_crtc_mode_set(struct drm_crtc *crtc,
76 struct drm_display_mode *mode,
77 struct drm_display_mode *adjusted_mode,
78 int x, int y, struct drm_framebuffer *old_fb)
80 struct bochs_device *bochs =
81 container_of(crtc, struct bochs_device, crtc);
83 bochs_hw_setmode(bochs, mode);
84 bochs_crtc_mode_set_base(crtc, x, y, old_fb);
88 static void bochs_crtc_prepare(struct drm_crtc *crtc)
92 static void bochs_crtc_commit(struct drm_crtc *crtc)
96 static int bochs_crtc_page_flip(struct drm_crtc *crtc,
97 struct drm_framebuffer *fb,
98 struct drm_pending_vblank_event *event,
99 uint32_t page_flip_flags)
101 struct bochs_device *bochs =
102 container_of(crtc, struct bochs_device, crtc);
103 struct drm_framebuffer *old_fb = crtc->primary->fb;
104 unsigned long irqflags;
106 crtc->primary->fb = fb;
107 bochs_crtc_mode_set_base(crtc, 0, 0, old_fb);
109 spin_lock_irqsave(&bochs->dev->event_lock, irqflags);
110 drm_crtc_send_vblank_event(crtc, event);
111 spin_unlock_irqrestore(&bochs->dev->event_lock, irqflags);
116 /* These provide the minimum set of functions required to handle a CRTC */
117 static const struct drm_crtc_funcs bochs_crtc_funcs = {
118 .set_config = drm_crtc_helper_set_config,
119 .destroy = drm_crtc_cleanup,
120 .page_flip = bochs_crtc_page_flip,
123 static const struct drm_crtc_helper_funcs bochs_helper_funcs = {
124 .dpms = bochs_crtc_dpms,
125 .mode_set = bochs_crtc_mode_set,
126 .mode_set_base = bochs_crtc_mode_set_base,
127 .prepare = bochs_crtc_prepare,
128 .commit = bochs_crtc_commit,
131 static void bochs_crtc_init(struct drm_device *dev)
133 struct bochs_device *bochs = dev->dev_private;
134 struct drm_crtc *crtc = &bochs->crtc;
136 drm_crtc_init(dev, crtc, &bochs_crtc_funcs);
137 drm_crtc_helper_add(crtc, &bochs_helper_funcs);
140 static void bochs_encoder_mode_set(struct drm_encoder *encoder,
141 struct drm_display_mode *mode,
142 struct drm_display_mode *adjusted_mode)
146 static void bochs_encoder_dpms(struct drm_encoder *encoder, int state)
150 static void bochs_encoder_prepare(struct drm_encoder *encoder)
154 static void bochs_encoder_commit(struct drm_encoder *encoder)
158 static const struct drm_encoder_helper_funcs bochs_encoder_helper_funcs = {
159 .dpms = bochs_encoder_dpms,
160 .mode_set = bochs_encoder_mode_set,
161 .prepare = bochs_encoder_prepare,
162 .commit = bochs_encoder_commit,
165 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
166 .destroy = drm_encoder_cleanup,
169 static void bochs_encoder_init(struct drm_device *dev)
171 struct bochs_device *bochs = dev->dev_private;
172 struct drm_encoder *encoder = &bochs->encoder;
174 encoder->possible_crtcs = 0x1;
175 drm_encoder_init(dev, encoder, &bochs_encoder_encoder_funcs,
176 DRM_MODE_ENCODER_DAC, NULL);
177 drm_encoder_helper_add(encoder, &bochs_encoder_helper_funcs);
181 static int bochs_connector_get_modes(struct drm_connector *connector)
185 count = drm_add_modes_noedid(connector, 8192, 8192);
186 drm_set_preferred_mode(connector, defx, defy);
190 static int bochs_connector_mode_valid(struct drm_connector *connector,
191 struct drm_display_mode *mode)
193 struct bochs_device *bochs =
194 container_of(connector, struct bochs_device, connector);
195 unsigned long size = mode->hdisplay * mode->vdisplay * 4;
198 * Make sure we can fit two framebuffers into video memory.
199 * This allows up to 1600x1200 with 16 MB (default size).
200 * If you want more try this:
201 * 'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
203 if (size * 2 > bochs->fb_size)
209 static struct drm_encoder *
210 bochs_connector_best_encoder(struct drm_connector *connector)
212 int enc_id = connector->encoder_ids[0];
213 /* pick the encoder ids */
215 return drm_encoder_find(connector->dev, enc_id);
219 static enum drm_connector_status bochs_connector_detect(struct drm_connector
220 *connector, bool force)
222 return connector_status_connected;
225 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
226 .get_modes = bochs_connector_get_modes,
227 .mode_valid = bochs_connector_mode_valid,
228 .best_encoder = bochs_connector_best_encoder,
231 static const struct drm_connector_funcs bochs_connector_connector_funcs = {
232 .dpms = drm_helper_connector_dpms,
233 .detect = bochs_connector_detect,
234 .fill_modes = drm_helper_probe_single_connector_modes,
235 .destroy = drm_connector_cleanup,
238 static void bochs_connector_init(struct drm_device *dev)
240 struct bochs_device *bochs = dev->dev_private;
241 struct drm_connector *connector = &bochs->connector;
243 drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
244 DRM_MODE_CONNECTOR_VIRTUAL);
245 drm_connector_helper_add(connector,
246 &bochs_connector_connector_helper_funcs);
247 drm_connector_register(connector);
251 int bochs_kms_init(struct bochs_device *bochs)
253 drm_mode_config_init(bochs->dev);
254 bochs->mode_config_initialized = true;
256 bochs->dev->mode_config.max_width = 8192;
257 bochs->dev->mode_config.max_height = 8192;
259 bochs->dev->mode_config.fb_base = bochs->fb_base;
260 bochs->dev->mode_config.preferred_depth = 24;
261 bochs->dev->mode_config.prefer_shadow = 0;
263 bochs->dev->mode_config.funcs = &bochs_mode_funcs;
265 bochs_crtc_init(bochs->dev);
266 bochs_encoder_init(bochs->dev);
267 bochs_connector_init(bochs->dev);
268 drm_mode_connector_attach_encoder(&bochs->connector,
274 void bochs_kms_fini(struct bochs_device *bochs)
276 if (bochs->mode_config_initialized) {
277 drm_mode_config_cleanup(bochs->dev);
278 bochs->mode_config_initialized = false;