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_atomic_helper.h>
10 #include <drm/drm_plane_helper.h>
11 #include <drm/drm_atomic_uapi.h>
13 static int defx = 1024;
14 static int defy = 768;
16 module_param(defx, int, 0444);
17 module_param(defy, int, 0444);
18 MODULE_PARM_DESC(defx, "default x resolution");
19 MODULE_PARM_DESC(defy, "default y resolution");
21 /* ---------------------------------------------------------------------- */
23 static void bochs_crtc_dpms(struct drm_crtc *crtc, int mode)
26 case DRM_MODE_DPMS_ON:
27 case DRM_MODE_DPMS_STANDBY:
28 case DRM_MODE_DPMS_SUSPEND:
29 case DRM_MODE_DPMS_OFF:
35 static int bochs_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
36 struct drm_framebuffer *old_fb)
38 struct bochs_device *bochs =
39 container_of(crtc, struct bochs_device, crtc);
45 bo = gem_to_bochs_bo(old_fb->obj[0]);
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 bo = gem_to_bochs_bo(crtc->primary->fb->obj[0]);
59 ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
63 ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
65 ttm_bo_unreserve(&bo->bo);
69 ttm_bo_unreserve(&bo->bo);
70 bochs_hw_setbase(bochs, x, y, gpu_addr);
74 static int bochs_crtc_mode_set(struct drm_crtc *crtc,
75 struct drm_display_mode *mode,
76 struct drm_display_mode *adjusted_mode,
77 int x, int y, struct drm_framebuffer *old_fb)
79 struct bochs_device *bochs =
80 container_of(crtc, struct bochs_device, crtc);
82 if (WARN_ON(crtc->primary->fb == NULL))
85 bochs_hw_setmode(bochs, mode);
86 bochs_hw_setformat(bochs, crtc->primary->fb->format);
87 bochs_crtc_mode_set_base(crtc, x, y, old_fb);
91 static void bochs_crtc_mode_set_nofb(struct drm_crtc *crtc)
93 struct bochs_device *bochs =
94 container_of(crtc, struct bochs_device, crtc);
96 bochs_hw_setmode(bochs, &crtc->mode);
99 static void bochs_crtc_prepare(struct drm_crtc *crtc)
103 static void bochs_crtc_commit(struct drm_crtc *crtc)
107 static int bochs_crtc_page_flip(struct drm_crtc *crtc,
108 struct drm_framebuffer *fb,
109 struct drm_pending_vblank_event *event,
110 uint32_t page_flip_flags,
111 struct drm_modeset_acquire_ctx *ctx)
113 struct bochs_device *bochs =
114 container_of(crtc, struct bochs_device, crtc);
115 struct drm_framebuffer *old_fb = crtc->primary->fb;
116 unsigned long irqflags;
118 drm_atomic_set_fb_for_plane(crtc->primary->state, fb);
119 bochs_crtc_mode_set_base(crtc, 0, 0, old_fb);
121 spin_lock_irqsave(&bochs->dev->event_lock, irqflags);
122 drm_crtc_send_vblank_event(crtc, event);
123 spin_unlock_irqrestore(&bochs->dev->event_lock, irqflags);
128 static void bochs_crtc_atomic_enable(struct drm_crtc *crtc,
129 struct drm_crtc_state *old_crtc_state)
133 static void bochs_crtc_atomic_flush(struct drm_crtc *crtc,
134 struct drm_crtc_state *old_crtc_state)
136 struct drm_device *dev = crtc->dev;
137 struct drm_pending_vblank_event *event;
139 if (crtc->state && crtc->state->event) {
140 unsigned long irqflags;
142 spin_lock_irqsave(&dev->event_lock, irqflags);
143 event = crtc->state->event;
144 crtc->state->event = NULL;
145 drm_crtc_send_vblank_event(crtc, event);
146 spin_unlock_irqrestore(&dev->event_lock, irqflags);
151 /* These provide the minimum set of functions required to handle a CRTC */
152 static const struct drm_crtc_funcs bochs_crtc_funcs = {
153 .set_config = drm_atomic_helper_set_config,
154 .destroy = drm_crtc_cleanup,
155 .page_flip = bochs_crtc_page_flip,
156 .reset = drm_atomic_helper_crtc_reset,
157 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
158 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
161 static const struct drm_crtc_helper_funcs bochs_helper_funcs = {
162 .dpms = bochs_crtc_dpms,
163 .mode_set = bochs_crtc_mode_set,
164 .mode_set_base = bochs_crtc_mode_set_base,
165 .mode_set_nofb = bochs_crtc_mode_set_nofb,
166 .prepare = bochs_crtc_prepare,
167 .commit = bochs_crtc_commit,
168 .atomic_enable = bochs_crtc_atomic_enable,
169 .atomic_flush = bochs_crtc_atomic_flush,
172 static const uint32_t bochs_formats[] = {
177 static void bochs_plane_atomic_update(struct drm_plane *plane,
178 struct drm_plane_state *old_state)
180 struct bochs_device *bochs = plane->dev->dev_private;
183 if (!plane->state->fb)
185 bo = gem_to_bochs_bo(plane->state->fb->obj[0]);
186 bochs_hw_setbase(bochs,
187 plane->state->crtc_x,
188 plane->state->crtc_y,
190 bochs_hw_setformat(bochs, plane->state->fb->format);
193 static int bochs_plane_prepare_fb(struct drm_plane *plane,
194 struct drm_plane_state *new_state)
201 bo = gem_to_bochs_bo(new_state->fb->obj[0]);
203 ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
206 ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
207 ttm_bo_unreserve(&bo->bo);
211 static void bochs_plane_cleanup_fb(struct drm_plane *plane,
212 struct drm_plane_state *old_state)
219 bo = gem_to_bochs_bo(old_state->fb->obj[0]);
220 ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
224 ttm_bo_unreserve(&bo->bo);
227 static const struct drm_plane_helper_funcs bochs_plane_helper_funcs = {
228 .atomic_update = bochs_plane_atomic_update,
229 .prepare_fb = bochs_plane_prepare_fb,
230 .cleanup_fb = bochs_plane_cleanup_fb,
233 static const struct drm_plane_funcs bochs_plane_funcs = {
234 .update_plane = drm_atomic_helper_update_plane,
235 .disable_plane = drm_atomic_helper_disable_plane,
236 .destroy = drm_primary_helper_destroy,
237 .reset = drm_atomic_helper_plane_reset,
238 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
239 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
242 static struct drm_plane *bochs_primary_plane(struct drm_device *dev)
244 struct drm_plane *primary;
247 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
248 if (primary == NULL) {
249 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
253 ret = drm_universal_plane_init(dev, primary, 0,
256 ARRAY_SIZE(bochs_formats),
258 DRM_PLANE_TYPE_PRIMARY, NULL);
264 drm_plane_helper_add(primary, &bochs_plane_helper_funcs);
268 static void bochs_crtc_init(struct drm_device *dev)
270 struct bochs_device *bochs = dev->dev_private;
271 struct drm_crtc *crtc = &bochs->crtc;
272 struct drm_plane *primary = bochs_primary_plane(dev);
274 drm_crtc_init_with_planes(dev, crtc, primary, NULL,
275 &bochs_crtc_funcs, NULL);
276 drm_crtc_helper_add(crtc, &bochs_helper_funcs);
279 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
280 .destroy = drm_encoder_cleanup,
283 static void bochs_encoder_init(struct drm_device *dev)
285 struct bochs_device *bochs = dev->dev_private;
286 struct drm_encoder *encoder = &bochs->encoder;
288 encoder->possible_crtcs = 0x1;
289 drm_encoder_init(dev, encoder, &bochs_encoder_encoder_funcs,
290 DRM_MODE_ENCODER_DAC, NULL);
294 static int bochs_connector_get_modes(struct drm_connector *connector)
296 struct bochs_device *bochs =
297 container_of(connector, struct bochs_device, connector);
301 count = drm_add_edid_modes(connector, bochs->edid);
304 count = drm_add_modes_noedid(connector, 8192, 8192);
305 drm_set_preferred_mode(connector, defx, defy);
310 static enum drm_mode_status bochs_connector_mode_valid(struct drm_connector *connector,
311 struct drm_display_mode *mode)
313 struct bochs_device *bochs =
314 container_of(connector, struct bochs_device, connector);
315 unsigned long size = mode->hdisplay * mode->vdisplay * 4;
318 * Make sure we can fit two framebuffers into video memory.
319 * This allows up to 1600x1200 with 16 MB (default size).
320 * If you want more try this:
321 * 'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
323 if (size * 2 > bochs->fb_size)
329 static struct drm_encoder *
330 bochs_connector_best_encoder(struct drm_connector *connector)
332 int enc_id = connector->encoder_ids[0];
333 /* pick the encoder ids */
335 return drm_encoder_find(connector->dev, NULL, enc_id);
339 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
340 .get_modes = bochs_connector_get_modes,
341 .mode_valid = bochs_connector_mode_valid,
342 .best_encoder = bochs_connector_best_encoder,
345 static const struct drm_connector_funcs bochs_connector_connector_funcs = {
346 .dpms = drm_helper_connector_dpms,
347 .fill_modes = drm_helper_probe_single_connector_modes,
348 .destroy = drm_connector_cleanup,
349 .reset = drm_atomic_helper_connector_reset,
350 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
351 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
354 static void bochs_connector_init(struct drm_device *dev)
356 struct bochs_device *bochs = dev->dev_private;
357 struct drm_connector *connector = &bochs->connector;
359 drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
360 DRM_MODE_CONNECTOR_VIRTUAL);
361 drm_connector_helper_add(connector,
362 &bochs_connector_connector_helper_funcs);
363 drm_connector_register(connector);
365 bochs_hw_load_edid(bochs);
367 DRM_INFO("Found EDID data blob.\n");
368 drm_connector_attach_edid_property(connector);
369 drm_connector_update_edid_property(connector, bochs->edid);
374 int bochs_kms_init(struct bochs_device *bochs)
376 drm_mode_config_init(bochs->dev);
377 bochs->mode_config_initialized = true;
379 bochs->dev->mode_config.max_width = 8192;
380 bochs->dev->mode_config.max_height = 8192;
382 bochs->dev->mode_config.fb_base = bochs->fb_base;
383 bochs->dev->mode_config.preferred_depth = 24;
384 bochs->dev->mode_config.prefer_shadow = 0;
385 bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
387 bochs->dev->mode_config.funcs = &bochs_mode_funcs;
389 bochs_crtc_init(bochs->dev);
390 bochs_encoder_init(bochs->dev);
391 bochs_connector_init(bochs->dev);
392 drm_connector_attach_encoder(&bochs->connector,
395 drm_mode_config_reset(bochs->dev);
400 void bochs_kms_fini(struct bochs_device *bochs)
402 if (bochs->mode_config_initialized) {
403 drm_mode_config_cleanup(bochs->dev);
404 bochs->mode_config_initialized = false;