0b4e5d11704335888c0d6818a44d486675ee97ba
[releases.git] / bochs_kms.c
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
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
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
43         if (old_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);
47                 if (ret) {
48                         DRM_ERROR("failed to reserve old_fb bo\n");
49                 } else {
50                         bochs_bo_unpin(bo);
51                         ttm_bo_unreserve(&bo->bo);
52                 }
53         }
54
55         if (WARN_ON(crtc->primary->fb == NULL))
56                 return -EINVAL;
57
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);
61         if (ret)
62                 return ret;
63
64         ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
65         if (ret) {
66                 ttm_bo_unreserve(&bo->bo);
67                 return ret;
68         }
69
70         ttm_bo_unreserve(&bo->bo);
71         bochs_hw_setbase(bochs, x, y, gpu_addr);
72         return 0;
73 }
74
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)
79 {
80         struct bochs_device *bochs =
81                 container_of(crtc, struct bochs_device, crtc);
82
83         bochs_hw_setmode(bochs, mode);
84         bochs_crtc_mode_set_base(crtc, x, y, old_fb);
85         return 0;
86 }
87
88 static void bochs_crtc_prepare(struct drm_crtc *crtc)
89 {
90 }
91
92 static void bochs_crtc_commit(struct drm_crtc *crtc)
93 {
94 }
95
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)
100 {
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;
105
106         crtc->primary->fb = fb;
107         bochs_crtc_mode_set_base(crtc, 0, 0, old_fb);
108         if (event) {
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);
112         }
113         return 0;
114 }
115
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,
121 };
122
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,
129 };
130
131 static void bochs_crtc_init(struct drm_device *dev)
132 {
133         struct bochs_device *bochs = dev->dev_private;
134         struct drm_crtc *crtc = &bochs->crtc;
135
136         drm_crtc_init(dev, crtc, &bochs_crtc_funcs);
137         drm_crtc_helper_add(crtc, &bochs_helper_funcs);
138 }
139
140 static void bochs_encoder_mode_set(struct drm_encoder *encoder,
141                                    struct drm_display_mode *mode,
142                                    struct drm_display_mode *adjusted_mode)
143 {
144 }
145
146 static void bochs_encoder_dpms(struct drm_encoder *encoder, int state)
147 {
148 }
149
150 static void bochs_encoder_prepare(struct drm_encoder *encoder)
151 {
152 }
153
154 static void bochs_encoder_commit(struct drm_encoder *encoder)
155 {
156 }
157
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,
163 };
164
165 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
166         .destroy = drm_encoder_cleanup,
167 };
168
169 static void bochs_encoder_init(struct drm_device *dev)
170 {
171         struct bochs_device *bochs = dev->dev_private;
172         struct drm_encoder *encoder = &bochs->encoder;
173
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);
178 }
179
180
181 static int bochs_connector_get_modes(struct drm_connector *connector)
182 {
183         int count;
184
185         count = drm_add_modes_noedid(connector, 8192, 8192);
186         drm_set_preferred_mode(connector, defx, defy);
187         return count;
188 }
189
190 static int bochs_connector_mode_valid(struct drm_connector *connector,
191                                       struct drm_display_mode *mode)
192 {
193         struct bochs_device *bochs =
194                 container_of(connector, struct bochs_device, connector);
195         unsigned long size = mode->hdisplay * mode->vdisplay * 4;
196
197         /*
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'
202          */
203         if (size * 2 > bochs->fb_size)
204                 return MODE_BAD;
205
206         return MODE_OK;
207 }
208
209 static struct drm_encoder *
210 bochs_connector_best_encoder(struct drm_connector *connector)
211 {
212         int enc_id = connector->encoder_ids[0];
213         /* pick the encoder ids */
214         if (enc_id)
215                 return drm_encoder_find(connector->dev, enc_id);
216         return NULL;
217 }
218
219 static enum drm_connector_status bochs_connector_detect(struct drm_connector
220                                                         *connector, bool force)
221 {
222         return connector_status_connected;
223 }
224
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,
229 };
230
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,
236 };
237
238 static void bochs_connector_init(struct drm_device *dev)
239 {
240         struct bochs_device *bochs = dev->dev_private;
241         struct drm_connector *connector = &bochs->connector;
242
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);
248 }
249
250
251 int bochs_kms_init(struct bochs_device *bochs)
252 {
253         drm_mode_config_init(bochs->dev);
254         bochs->mode_config_initialized = true;
255
256         bochs->dev->mode_config.max_width = 8192;
257         bochs->dev->mode_config.max_height = 8192;
258
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;
262
263         bochs->dev->mode_config.funcs = &bochs_mode_funcs;
264
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,
269                                           &bochs->encoder);
270
271         return 0;
272 }
273
274 void bochs_kms_fini(struct bochs_device *bochs)
275 {
276         if (bochs->mode_config_initialized) {
277                 drm_mode_config_cleanup(bochs->dev);
278                 bochs->mode_config_initialized = false;
279         }
280 }