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