]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/bochs/bochs_kms.c
drm/bochs: atomic: use atomic set_config helper
[linux.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_atomic_helper.h>
10 #include <drm/drm_plane_helper.h>
11 #include <drm/drm_atomic_uapi.h>
12
13 static int defx = 1024;
14 static int defy = 768;
15
16 module_param(defx, int, 0444);
17 module_param(defy, int, 0444);
18 MODULE_PARM_DESC(defx, "default x resolution");
19 MODULE_PARM_DESC(defy, "default y resolution");
20
21 /* ---------------------------------------------------------------------- */
22
23 static void bochs_crtc_dpms(struct drm_crtc *crtc, int mode)
24 {
25         switch (mode) {
26         case DRM_MODE_DPMS_ON:
27         case DRM_MODE_DPMS_STANDBY:
28         case DRM_MODE_DPMS_SUSPEND:
29         case DRM_MODE_DPMS_OFF:
30         default:
31                 return;
32         }
33 }
34
35 static int bochs_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
36                                     struct drm_framebuffer *old_fb)
37 {
38         struct bochs_device *bochs =
39                 container_of(crtc, struct bochs_device, crtc);
40         struct bochs_bo *bo;
41         u64 gpu_addr = 0;
42         int ret;
43
44         if (old_fb) {
45                 bo = gem_to_bochs_bo(old_fb->obj[0]);
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         bo = gem_to_bochs_bo(crtc->primary->fb->obj[0]);
59         ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
60         if (ret)
61                 return ret;
62
63         ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
64         if (ret) {
65                 ttm_bo_unreserve(&bo->bo);
66                 return ret;
67         }
68
69         ttm_bo_unreserve(&bo->bo);
70         bochs_hw_setbase(bochs, x, y, gpu_addr);
71         return 0;
72 }
73
74 static int bochs_crtc_mode_set(struct drm_crtc *crtc,
75                                struct drm_display_mode *mode,
76                                struct drm_display_mode *adjusted_mode,
77                                int x, int y, struct drm_framebuffer *old_fb)
78 {
79         struct bochs_device *bochs =
80                 container_of(crtc, struct bochs_device, crtc);
81
82         if (WARN_ON(crtc->primary->fb == NULL))
83                 return -EINVAL;
84
85         bochs_hw_setmode(bochs, mode);
86         bochs_hw_setformat(bochs, crtc->primary->fb->format);
87         bochs_crtc_mode_set_base(crtc, x, y, old_fb);
88         return 0;
89 }
90
91 static void bochs_crtc_mode_set_nofb(struct drm_crtc *crtc)
92 {
93         struct bochs_device *bochs =
94                 container_of(crtc, struct bochs_device, crtc);
95
96         bochs_hw_setmode(bochs, &crtc->mode);
97 }
98
99 static void bochs_crtc_prepare(struct drm_crtc *crtc)
100 {
101 }
102
103 static void bochs_crtc_commit(struct drm_crtc *crtc)
104 {
105 }
106
107 static int bochs_crtc_page_flip(struct drm_crtc *crtc,
108                                 struct drm_framebuffer *fb,
109                                 struct drm_pending_vblank_event *event,
110                                 uint32_t page_flip_flags,
111                                 struct drm_modeset_acquire_ctx *ctx)
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         drm_atomic_set_fb_for_plane(crtc->primary->state, 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_crtc_send_vblank_event(crtc, event);
123                 spin_unlock_irqrestore(&bochs->dev->event_lock, irqflags);
124         }
125         return 0;
126 }
127
128 static void bochs_crtc_atomic_enable(struct drm_crtc *crtc,
129                                      struct drm_crtc_state *old_crtc_state)
130 {
131 }
132
133 static void bochs_crtc_atomic_flush(struct drm_crtc *crtc,
134                                     struct drm_crtc_state *old_crtc_state)
135 {
136         struct drm_device *dev = crtc->dev;
137         struct drm_pending_vblank_event *event;
138
139         if (crtc->state && crtc->state->event) {
140                 unsigned long irqflags;
141
142                 spin_lock_irqsave(&dev->event_lock, irqflags);
143                 event = crtc->state->event;
144                 crtc->state->event = NULL;
145                 drm_crtc_send_vblank_event(crtc, event);
146                 spin_unlock_irqrestore(&dev->event_lock, irqflags);
147         }
148 }
149
150
151 /* These provide the minimum set of functions required to handle a CRTC */
152 static const struct drm_crtc_funcs bochs_crtc_funcs = {
153         .set_config = drm_atomic_helper_set_config,
154         .destroy = drm_crtc_cleanup,
155         .page_flip = bochs_crtc_page_flip,
156         .reset = drm_atomic_helper_crtc_reset,
157         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
158         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
159 };
160
161 static const struct drm_crtc_helper_funcs bochs_helper_funcs = {
162         .dpms = bochs_crtc_dpms,
163         .mode_set = bochs_crtc_mode_set,
164         .mode_set_base = bochs_crtc_mode_set_base,
165         .mode_set_nofb = bochs_crtc_mode_set_nofb,
166         .prepare = bochs_crtc_prepare,
167         .commit = bochs_crtc_commit,
168         .atomic_enable = bochs_crtc_atomic_enable,
169         .atomic_flush = bochs_crtc_atomic_flush,
170 };
171
172 static const uint32_t bochs_formats[] = {
173         DRM_FORMAT_XRGB8888,
174         DRM_FORMAT_BGRX8888,
175 };
176
177 static void bochs_plane_atomic_update(struct drm_plane *plane,
178                                       struct drm_plane_state *old_state)
179 {
180         struct bochs_device *bochs = plane->dev->dev_private;
181         struct bochs_bo *bo;
182
183         if (!plane->state->fb)
184                 return;
185         bo = gem_to_bochs_bo(plane->state->fb->obj[0]);
186         bochs_hw_setbase(bochs,
187                          plane->state->crtc_x,
188                          plane->state->crtc_y,
189                          bo->bo.offset);
190         bochs_hw_setformat(bochs, plane->state->fb->format);
191 }
192
193 static int bochs_plane_prepare_fb(struct drm_plane *plane,
194                                 struct drm_plane_state *new_state)
195 {
196         struct bochs_bo *bo;
197         int ret;
198
199         if (!new_state->fb)
200                 return 0;
201         bo = gem_to_bochs_bo(new_state->fb->obj[0]);
202
203         ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
204         if (ret)
205                 return ret;
206         ret = bochs_bo_pin(bo, TTM_PL_FLAG_VRAM, NULL);
207         ttm_bo_unreserve(&bo->bo);
208         return ret;
209 }
210
211 static void bochs_plane_cleanup_fb(struct drm_plane *plane,
212                                    struct drm_plane_state *old_state)
213 {
214         struct bochs_bo *bo;
215         int ret;
216
217         if (!old_state->fb)
218                 return;
219         bo = gem_to_bochs_bo(old_state->fb->obj[0]);
220         ret = ttm_bo_reserve(&bo->bo, true, false, NULL);
221         if (ret)
222                 return;
223         bochs_bo_unpin(bo);
224         ttm_bo_unreserve(&bo->bo);
225 }
226
227 static const struct drm_plane_helper_funcs bochs_plane_helper_funcs = {
228         .atomic_update = bochs_plane_atomic_update,
229         .prepare_fb = bochs_plane_prepare_fb,
230         .cleanup_fb = bochs_plane_cleanup_fb,
231 };
232
233 static const struct drm_plane_funcs bochs_plane_funcs = {
234        .update_plane   = drm_atomic_helper_update_plane,
235        .disable_plane  = drm_atomic_helper_disable_plane,
236        .destroy        = drm_primary_helper_destroy,
237        .reset          = drm_atomic_helper_plane_reset,
238        .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
239        .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
240 };
241
242 static struct drm_plane *bochs_primary_plane(struct drm_device *dev)
243 {
244         struct drm_plane *primary;
245         int ret;
246
247         primary = kzalloc(sizeof(*primary), GFP_KERNEL);
248         if (primary == NULL) {
249                 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
250                 return NULL;
251         }
252
253         ret = drm_universal_plane_init(dev, primary, 0,
254                                        &bochs_plane_funcs,
255                                        bochs_formats,
256                                        ARRAY_SIZE(bochs_formats),
257                                        NULL,
258                                        DRM_PLANE_TYPE_PRIMARY, NULL);
259         if (ret) {
260                 kfree(primary);
261                 return NULL;
262         }
263
264         drm_plane_helper_add(primary, &bochs_plane_helper_funcs);
265         return primary;
266 }
267
268 static void bochs_crtc_init(struct drm_device *dev)
269 {
270         struct bochs_device *bochs = dev->dev_private;
271         struct drm_crtc *crtc = &bochs->crtc;
272         struct drm_plane *primary = bochs_primary_plane(dev);
273
274         drm_crtc_init_with_planes(dev, crtc, primary, NULL,
275                                   &bochs_crtc_funcs, NULL);
276         drm_crtc_helper_add(crtc, &bochs_helper_funcs);
277 }
278
279 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
280         .destroy = drm_encoder_cleanup,
281 };
282
283 static void bochs_encoder_init(struct drm_device *dev)
284 {
285         struct bochs_device *bochs = dev->dev_private;
286         struct drm_encoder *encoder = &bochs->encoder;
287
288         encoder->possible_crtcs = 0x1;
289         drm_encoder_init(dev, encoder, &bochs_encoder_encoder_funcs,
290                          DRM_MODE_ENCODER_DAC, NULL);
291 }
292
293
294 static int bochs_connector_get_modes(struct drm_connector *connector)
295 {
296         struct bochs_device *bochs =
297                 container_of(connector, struct bochs_device, connector);
298         int count = 0;
299
300         if (bochs->edid)
301                 count = drm_add_edid_modes(connector, bochs->edid);
302
303         if (!count) {
304                 count = drm_add_modes_noedid(connector, 8192, 8192);
305                 drm_set_preferred_mode(connector, defx, defy);
306         }
307         return count;
308 }
309
310 static enum drm_mode_status bochs_connector_mode_valid(struct drm_connector *connector,
311                                       struct drm_display_mode *mode)
312 {
313         struct bochs_device *bochs =
314                 container_of(connector, struct bochs_device, connector);
315         unsigned long size = mode->hdisplay * mode->vdisplay * 4;
316
317         /*
318          * Make sure we can fit two framebuffers into video memory.
319          * This allows up to 1600x1200 with 16 MB (default size).
320          * If you want more try this:
321          *     'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
322          */
323         if (size * 2 > bochs->fb_size)
324                 return MODE_BAD;
325
326         return MODE_OK;
327 }
328
329 static struct drm_encoder *
330 bochs_connector_best_encoder(struct drm_connector *connector)
331 {
332         int enc_id = connector->encoder_ids[0];
333         /* pick the encoder ids */
334         if (enc_id)
335                 return drm_encoder_find(connector->dev, NULL, enc_id);
336         return NULL;
337 }
338
339 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
340         .get_modes = bochs_connector_get_modes,
341         .mode_valid = bochs_connector_mode_valid,
342         .best_encoder = bochs_connector_best_encoder,
343 };
344
345 static const struct drm_connector_funcs bochs_connector_connector_funcs = {
346         .dpms = drm_helper_connector_dpms,
347         .fill_modes = drm_helper_probe_single_connector_modes,
348         .destroy = drm_connector_cleanup,
349         .reset = drm_atomic_helper_connector_reset,
350         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
351         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
352 };
353
354 static void bochs_connector_init(struct drm_device *dev)
355 {
356         struct bochs_device *bochs = dev->dev_private;
357         struct drm_connector *connector = &bochs->connector;
358
359         drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
360                            DRM_MODE_CONNECTOR_VIRTUAL);
361         drm_connector_helper_add(connector,
362                                  &bochs_connector_connector_helper_funcs);
363         drm_connector_register(connector);
364
365         bochs_hw_load_edid(bochs);
366         if (bochs->edid) {
367                 DRM_INFO("Found EDID data blob.\n");
368                 drm_connector_attach_edid_property(connector);
369                 drm_connector_update_edid_property(connector, bochs->edid);
370         }
371 }
372
373
374 int bochs_kms_init(struct bochs_device *bochs)
375 {
376         drm_mode_config_init(bochs->dev);
377         bochs->mode_config_initialized = true;
378
379         bochs->dev->mode_config.max_width = 8192;
380         bochs->dev->mode_config.max_height = 8192;
381
382         bochs->dev->mode_config.fb_base = bochs->fb_base;
383         bochs->dev->mode_config.preferred_depth = 24;
384         bochs->dev->mode_config.prefer_shadow = 0;
385         bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
386
387         bochs->dev->mode_config.funcs = &bochs_mode_funcs;
388
389         bochs_crtc_init(bochs->dev);
390         bochs_encoder_init(bochs->dev);
391         bochs_connector_init(bochs->dev);
392         drm_connector_attach_encoder(&bochs->connector,
393                                           &bochs->encoder);
394
395         drm_mode_config_reset(bochs->dev);
396
397         return 0;
398 }
399
400 void bochs_kms_fini(struct bochs_device *bochs)
401 {
402         if (bochs->mode_config_initialized) {
403                 drm_mode_config_cleanup(bochs->dev);
404                 bochs->mode_config_initialized = false;
405         }
406 }