Merge tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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         if (WARN_ON(crtc->primary->fb == NULL))
81                 return -EINVAL;
82
83         bochs_hw_setmode(bochs, mode, crtc->primary->fb->format);
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                                 struct drm_modeset_acquire_ctx *ctx)
101 {
102         struct bochs_device *bochs =
103                 container_of(crtc, struct bochs_device, crtc);
104         struct drm_framebuffer *old_fb = crtc->primary->fb;
105         unsigned long irqflags;
106
107         crtc->primary->fb = fb;
108         bochs_crtc_mode_set_base(crtc, 0, 0, old_fb);
109         if (event) {
110                 spin_lock_irqsave(&bochs->dev->event_lock, irqflags);
111                 drm_crtc_send_vblank_event(crtc, event);
112                 spin_unlock_irqrestore(&bochs->dev->event_lock, irqflags);
113         }
114         return 0;
115 }
116
117 /* These provide the minimum set of functions required to handle a CRTC */
118 static const struct drm_crtc_funcs bochs_crtc_funcs = {
119         .set_config = drm_crtc_helper_set_config,
120         .destroy = drm_crtc_cleanup,
121         .page_flip = bochs_crtc_page_flip,
122 };
123
124 static const struct drm_crtc_helper_funcs bochs_helper_funcs = {
125         .dpms = bochs_crtc_dpms,
126         .mode_set = bochs_crtc_mode_set,
127         .mode_set_base = bochs_crtc_mode_set_base,
128         .prepare = bochs_crtc_prepare,
129         .commit = bochs_crtc_commit,
130 };
131
132 static const uint32_t bochs_formats[] = {
133         DRM_FORMAT_XRGB8888,
134         DRM_FORMAT_BGRX8888,
135 };
136
137 static struct drm_plane *bochs_primary_plane(struct drm_device *dev)
138 {
139         struct drm_plane *primary;
140         int ret;
141
142         primary = kzalloc(sizeof(*primary), GFP_KERNEL);
143         if (primary == NULL) {
144                 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
145                 return NULL;
146         }
147
148         ret = drm_universal_plane_init(dev, primary, 0,
149                                        &drm_primary_helper_funcs,
150                                        bochs_formats,
151                                        ARRAY_SIZE(bochs_formats),
152                                        NULL,
153                                        DRM_PLANE_TYPE_PRIMARY, NULL);
154         if (ret) {
155                 kfree(primary);
156                 primary = NULL;
157         }
158
159         return primary;
160 }
161
162 static void bochs_crtc_init(struct drm_device *dev)
163 {
164         struct bochs_device *bochs = dev->dev_private;
165         struct drm_crtc *crtc = &bochs->crtc;
166         struct drm_plane *primary = bochs_primary_plane(dev);
167
168         drm_crtc_init_with_planes(dev, crtc, primary, NULL,
169                                   &bochs_crtc_funcs, NULL);
170         drm_crtc_helper_add(crtc, &bochs_helper_funcs);
171 }
172
173 static void bochs_encoder_mode_set(struct drm_encoder *encoder,
174                                    struct drm_display_mode *mode,
175                                    struct drm_display_mode *adjusted_mode)
176 {
177 }
178
179 static void bochs_encoder_dpms(struct drm_encoder *encoder, int state)
180 {
181 }
182
183 static void bochs_encoder_prepare(struct drm_encoder *encoder)
184 {
185 }
186
187 static void bochs_encoder_commit(struct drm_encoder *encoder)
188 {
189 }
190
191 static const struct drm_encoder_helper_funcs bochs_encoder_helper_funcs = {
192         .dpms = bochs_encoder_dpms,
193         .mode_set = bochs_encoder_mode_set,
194         .prepare = bochs_encoder_prepare,
195         .commit = bochs_encoder_commit,
196 };
197
198 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
199         .destroy = drm_encoder_cleanup,
200 };
201
202 static void bochs_encoder_init(struct drm_device *dev)
203 {
204         struct bochs_device *bochs = dev->dev_private;
205         struct drm_encoder *encoder = &bochs->encoder;
206
207         encoder->possible_crtcs = 0x1;
208         drm_encoder_init(dev, encoder, &bochs_encoder_encoder_funcs,
209                          DRM_MODE_ENCODER_DAC, NULL);
210         drm_encoder_helper_add(encoder, &bochs_encoder_helper_funcs);
211 }
212
213
214 static int bochs_connector_get_modes(struct drm_connector *connector)
215 {
216         struct bochs_device *bochs =
217                 container_of(connector, struct bochs_device, connector);
218         int count = 0;
219
220         if (bochs->edid)
221                 count = drm_add_edid_modes(connector, bochs->edid);
222
223         if (!count) {
224                 count = drm_add_modes_noedid(connector, 8192, 8192);
225                 drm_set_preferred_mode(connector, defx, defy);
226         }
227         return count;
228 }
229
230 static enum drm_mode_status bochs_connector_mode_valid(struct drm_connector *connector,
231                                       struct drm_display_mode *mode)
232 {
233         struct bochs_device *bochs =
234                 container_of(connector, struct bochs_device, connector);
235         unsigned long size = mode->hdisplay * mode->vdisplay * 4;
236
237         /*
238          * Make sure we can fit two framebuffers into video memory.
239          * This allows up to 1600x1200 with 16 MB (default size).
240          * If you want more try this:
241          *     'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
242          */
243         if (size * 2 > bochs->fb_size)
244                 return MODE_BAD;
245
246         return MODE_OK;
247 }
248
249 static struct drm_encoder *
250 bochs_connector_best_encoder(struct drm_connector *connector)
251 {
252         int enc_id = connector->encoder_ids[0];
253         /* pick the encoder ids */
254         if (enc_id)
255                 return drm_encoder_find(connector->dev, NULL, enc_id);
256         return NULL;
257 }
258
259 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
260         .get_modes = bochs_connector_get_modes,
261         .mode_valid = bochs_connector_mode_valid,
262         .best_encoder = bochs_connector_best_encoder,
263 };
264
265 static const struct drm_connector_funcs bochs_connector_connector_funcs = {
266         .dpms = drm_helper_connector_dpms,
267         .fill_modes = drm_helper_probe_single_connector_modes,
268         .destroy = drm_connector_cleanup,
269 };
270
271 static void bochs_connector_init(struct drm_device *dev)
272 {
273         struct bochs_device *bochs = dev->dev_private;
274         struct drm_connector *connector = &bochs->connector;
275
276         drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
277                            DRM_MODE_CONNECTOR_VIRTUAL);
278         drm_connector_helper_add(connector,
279                                  &bochs_connector_connector_helper_funcs);
280         drm_connector_register(connector);
281
282         bochs_hw_load_edid(bochs);
283         if (bochs->edid) {
284                 DRM_INFO("Found EDID data blob.\n");
285                 drm_connector_attach_edid_property(connector);
286                 drm_connector_update_edid_property(connector, bochs->edid);
287         }
288 }
289
290
291 int bochs_kms_init(struct bochs_device *bochs)
292 {
293         drm_mode_config_init(bochs->dev);
294         bochs->mode_config_initialized = true;
295
296         bochs->dev->mode_config.max_width = 8192;
297         bochs->dev->mode_config.max_height = 8192;
298
299         bochs->dev->mode_config.fb_base = bochs->fb_base;
300         bochs->dev->mode_config.preferred_depth = 24;
301         bochs->dev->mode_config.prefer_shadow = 0;
302         bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
303
304         bochs->dev->mode_config.funcs = &bochs_mode_funcs;
305
306         bochs_crtc_init(bochs->dev);
307         bochs_encoder_init(bochs->dev);
308         bochs_connector_init(bochs->dev);
309         drm_connector_attach_encoder(&bochs->connector,
310                                           &bochs->encoder);
311
312         return 0;
313 }
314
315 void bochs_kms_fini(struct bochs_device *bochs)
316 {
317         if (bochs->mode_config_initialized) {
318                 drm_mode_config_cleanup(bochs->dev);
319                 bochs->mode_config_initialized = false;
320         }
321 }