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