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>
12 #include <drm/drm_gem_framebuffer_helper.h>
13 #include <drm/drm_probe_helper.h>
15 static int defx
= 1024;
16 static int defy
= 768;
18 module_param(defx
, int, 0444);
19 module_param(defy
, int, 0444);
20 MODULE_PARM_DESC(defx
, "default x resolution");
21 MODULE_PARM_DESC(defy
, "default y resolution");
23 /* ---------------------------------------------------------------------- */
25 static void bochs_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
27 struct bochs_device
*bochs
=
28 container_of(crtc
, struct bochs_device
, crtc
);
30 bochs_hw_setmode(bochs
, &crtc
->mode
);
33 static void bochs_crtc_atomic_enable(struct drm_crtc
*crtc
,
34 struct drm_crtc_state
*old_crtc_state
)
38 static void bochs_crtc_atomic_flush(struct drm_crtc
*crtc
,
39 struct drm_crtc_state
*old_crtc_state
)
41 struct drm_device
*dev
= crtc
->dev
;
42 struct drm_pending_vblank_event
*event
;
44 if (crtc
->state
&& crtc
->state
->event
) {
45 unsigned long irqflags
;
47 spin_lock_irqsave(&dev
->event_lock
, irqflags
);
48 event
= crtc
->state
->event
;
49 crtc
->state
->event
= NULL
;
50 drm_crtc_send_vblank_event(crtc
, event
);
51 spin_unlock_irqrestore(&dev
->event_lock
, irqflags
);
56 /* These provide the minimum set of functions required to handle a CRTC */
57 static const struct drm_crtc_funcs bochs_crtc_funcs
= {
58 .set_config
= drm_atomic_helper_set_config
,
59 .destroy
= drm_crtc_cleanup
,
60 .page_flip
= drm_atomic_helper_page_flip
,
61 .reset
= drm_atomic_helper_crtc_reset
,
62 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
63 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
66 static const struct drm_crtc_helper_funcs bochs_helper_funcs
= {
67 .mode_set_nofb
= bochs_crtc_mode_set_nofb
,
68 .atomic_enable
= bochs_crtc_atomic_enable
,
69 .atomic_flush
= bochs_crtc_atomic_flush
,
72 static const uint32_t bochs_formats
[] = {
77 static void bochs_plane_atomic_update(struct drm_plane
*plane
,
78 struct drm_plane_state
*old_state
)
80 struct bochs_device
*bochs
= plane
->dev
->dev_private
;
83 if (!plane
->state
->fb
)
85 bo
= gem_to_bochs_bo(plane
->state
->fb
->obj
[0]);
86 bochs_hw_setbase(bochs
,
90 bochs_hw_setformat(bochs
, plane
->state
->fb
->format
);
93 static int bochs_plane_prepare_fb(struct drm_plane
*plane
,
94 struct drm_plane_state
*new_state
)
100 bo
= gem_to_bochs_bo(new_state
->fb
->obj
[0]);
101 return bochs_bo_pin(bo
, TTM_PL_FLAG_VRAM
);
104 static void bochs_plane_cleanup_fb(struct drm_plane
*plane
,
105 struct drm_plane_state
*old_state
)
111 bo
= gem_to_bochs_bo(old_state
->fb
->obj
[0]);
115 static const struct drm_plane_helper_funcs bochs_plane_helper_funcs
= {
116 .atomic_update
= bochs_plane_atomic_update
,
117 .prepare_fb
= bochs_plane_prepare_fb
,
118 .cleanup_fb
= bochs_plane_cleanup_fb
,
121 static const struct drm_plane_funcs bochs_plane_funcs
= {
122 .update_plane
= drm_atomic_helper_update_plane
,
123 .disable_plane
= drm_atomic_helper_disable_plane
,
124 .destroy
= drm_primary_helper_destroy
,
125 .reset
= drm_atomic_helper_plane_reset
,
126 .atomic_duplicate_state
= drm_atomic_helper_plane_duplicate_state
,
127 .atomic_destroy_state
= drm_atomic_helper_plane_destroy_state
,
130 static struct drm_plane
*bochs_primary_plane(struct drm_device
*dev
)
132 struct drm_plane
*primary
;
135 primary
= kzalloc(sizeof(*primary
), GFP_KERNEL
);
136 if (primary
== NULL
) {
137 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
141 ret
= drm_universal_plane_init(dev
, primary
, 0,
144 ARRAY_SIZE(bochs_formats
),
146 DRM_PLANE_TYPE_PRIMARY
, NULL
);
152 drm_plane_helper_add(primary
, &bochs_plane_helper_funcs
);
156 static void bochs_crtc_init(struct drm_device
*dev
)
158 struct bochs_device
*bochs
= dev
->dev_private
;
159 struct drm_crtc
*crtc
= &bochs
->crtc
;
160 struct drm_plane
*primary
= bochs_primary_plane(dev
);
162 drm_crtc_init_with_planes(dev
, crtc
, primary
, NULL
,
163 &bochs_crtc_funcs
, NULL
);
164 drm_crtc_helper_add(crtc
, &bochs_helper_funcs
);
167 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs
= {
168 .destroy
= drm_encoder_cleanup
,
171 static void bochs_encoder_init(struct drm_device
*dev
)
173 struct bochs_device
*bochs
= dev
->dev_private
;
174 struct drm_encoder
*encoder
= &bochs
->encoder
;
176 encoder
->possible_crtcs
= 0x1;
177 drm_encoder_init(dev
, encoder
, &bochs_encoder_encoder_funcs
,
178 DRM_MODE_ENCODER_DAC
, NULL
);
182 static int bochs_connector_get_modes(struct drm_connector
*connector
)
184 struct bochs_device
*bochs
=
185 container_of(connector
, struct bochs_device
, connector
);
189 count
= drm_add_edid_modes(connector
, bochs
->edid
);
192 count
= drm_add_modes_noedid(connector
, 8192, 8192);
193 drm_set_preferred_mode(connector
, defx
, defy
);
198 static enum drm_mode_status
bochs_connector_mode_valid(struct drm_connector
*connector
,
199 struct drm_display_mode
*mode
)
201 struct bochs_device
*bochs
=
202 container_of(connector
, struct bochs_device
, connector
);
203 unsigned long size
= mode
->hdisplay
* mode
->vdisplay
* 4;
206 * Make sure we can fit two framebuffers into video memory.
207 * This allows up to 1600x1200 with 16 MB (default size).
208 * If you want more try this:
209 * 'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
211 if (size
* 2 > bochs
->fb_size
)
217 static struct drm_encoder
*
218 bochs_connector_best_encoder(struct drm_connector
*connector
)
220 int enc_id
= connector
->encoder_ids
[0];
221 /* pick the encoder ids */
223 return drm_encoder_find(connector
->dev
, NULL
, enc_id
);
227 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs
= {
228 .get_modes
= bochs_connector_get_modes
,
229 .mode_valid
= bochs_connector_mode_valid
,
230 .best_encoder
= bochs_connector_best_encoder
,
233 static const struct drm_connector_funcs bochs_connector_connector_funcs
= {
234 .dpms
= drm_helper_connector_dpms
,
235 .fill_modes
= drm_helper_probe_single_connector_modes
,
236 .destroy
= drm_connector_cleanup
,
237 .reset
= drm_atomic_helper_connector_reset
,
238 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
239 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
242 static void bochs_connector_init(struct drm_device
*dev
)
244 struct bochs_device
*bochs
= dev
->dev_private
;
245 struct drm_connector
*connector
= &bochs
->connector
;
247 drm_connector_init(dev
, connector
, &bochs_connector_connector_funcs
,
248 DRM_MODE_CONNECTOR_VIRTUAL
);
249 drm_connector_helper_add(connector
,
250 &bochs_connector_connector_helper_funcs
);
251 drm_connector_register(connector
);
253 bochs_hw_load_edid(bochs
);
255 DRM_INFO("Found EDID data blob.\n");
256 drm_connector_attach_edid_property(connector
);
257 drm_connector_update_edid_property(connector
, bochs
->edid
);
261 static struct drm_framebuffer
*
262 bochs_gem_fb_create(struct drm_device
*dev
, struct drm_file
*file
,
263 const struct drm_mode_fb_cmd2
*mode_cmd
)
265 if (mode_cmd
->pixel_format
!= DRM_FORMAT_XRGB8888
&&
266 mode_cmd
->pixel_format
!= DRM_FORMAT_BGRX8888
)
267 return ERR_PTR(-EINVAL
);
269 return drm_gem_fb_create(dev
, file
, mode_cmd
);
272 const struct drm_mode_config_funcs bochs_mode_funcs
= {
273 .fb_create
= bochs_gem_fb_create
,
274 .atomic_check
= drm_atomic_helper_check
,
275 .atomic_commit
= drm_atomic_helper_commit
,
278 int bochs_kms_init(struct bochs_device
*bochs
)
280 drm_mode_config_init(bochs
->dev
);
281 bochs
->mode_config_initialized
= true;
283 bochs
->dev
->mode_config
.max_width
= 8192;
284 bochs
->dev
->mode_config
.max_height
= 8192;
286 bochs
->dev
->mode_config
.fb_base
= bochs
->fb_base
;
287 bochs
->dev
->mode_config
.preferred_depth
= 24;
288 bochs
->dev
->mode_config
.prefer_shadow
= 0;
289 bochs
->dev
->mode_config
.quirk_addfb_prefer_host_byte_order
= true;
291 bochs
->dev
->mode_config
.funcs
= &bochs_mode_funcs
;
293 bochs_crtc_init(bochs
->dev
);
294 bochs_encoder_init(bochs
->dev
);
295 bochs_connector_init(bochs
->dev
);
296 drm_connector_attach_encoder(&bochs
->connector
,
299 drm_mode_config_reset(bochs
->dev
);
304 void bochs_kms_fini(struct bochs_device
*bochs
)
306 if (bochs
->mode_config_initialized
) {
307 drm_mode_config_cleanup(bochs
->dev
);
308 bochs
->mode_config_initialized
= false;