]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/bochs/bochs_kms.c
drm/bochs: drop mode_config_initialized
[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 #include <drm/drm_gem_framebuffer_helper.h>
13 #include <drm/drm_probe_helper.h>
14
15 static int defx = 1024;
16 static int defy = 768;
17
18 module_param(defx, int, 0444);
19 module_param(defy, int, 0444);
20 MODULE_PARM_DESC(defx, "default x resolution");
21 MODULE_PARM_DESC(defy, "default y resolution");
22
23 /* ---------------------------------------------------------------------- */
24
25 static void bochs_crtc_mode_set_nofb(struct drm_crtc *crtc)
26 {
27         struct bochs_device *bochs =
28                 container_of(crtc, struct bochs_device, crtc);
29
30         bochs_hw_setmode(bochs, &crtc->mode);
31 }
32
33 static void bochs_crtc_atomic_enable(struct drm_crtc *crtc,
34                                      struct drm_crtc_state *old_crtc_state)
35 {
36 }
37
38 static void bochs_crtc_atomic_flush(struct drm_crtc *crtc,
39                                     struct drm_crtc_state *old_crtc_state)
40 {
41         struct drm_device *dev = crtc->dev;
42         struct drm_pending_vblank_event *event;
43
44         if (crtc->state && crtc->state->event) {
45                 unsigned long irqflags;
46
47                 spin_lock_irqsave(&dev->event_lock, irqflags);
48                 event = crtc->state->event;
49                 crtc->state->event = NULL;
50                 drm_crtc_send_vblank_event(crtc, event);
51                 spin_unlock_irqrestore(&dev->event_lock, irqflags);
52         }
53 }
54
55
56 /* These provide the minimum set of functions required to handle a CRTC */
57 static const struct drm_crtc_funcs bochs_crtc_funcs = {
58         .set_config = drm_atomic_helper_set_config,
59         .destroy = drm_crtc_cleanup,
60         .page_flip = drm_atomic_helper_page_flip,
61         .reset = drm_atomic_helper_crtc_reset,
62         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
63         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
64 };
65
66 static const struct drm_crtc_helper_funcs bochs_helper_funcs = {
67         .mode_set_nofb = bochs_crtc_mode_set_nofb,
68         .atomic_enable = bochs_crtc_atomic_enable,
69         .atomic_flush = bochs_crtc_atomic_flush,
70 };
71
72 static const uint32_t bochs_formats[] = {
73         DRM_FORMAT_XRGB8888,
74         DRM_FORMAT_BGRX8888,
75 };
76
77 static void bochs_plane_atomic_update(struct drm_plane *plane,
78                                       struct drm_plane_state *old_state)
79 {
80         struct bochs_device *bochs = plane->dev->dev_private;
81         struct bochs_bo *bo;
82
83         if (!plane->state->fb)
84                 return;
85         bo = gem_to_bochs_bo(plane->state->fb->obj[0]);
86         bochs_hw_setbase(bochs,
87                          plane->state->crtc_x,
88                          plane->state->crtc_y,
89                          bo->bo.offset);
90         bochs_hw_setformat(bochs, plane->state->fb->format);
91 }
92
93 static int bochs_plane_prepare_fb(struct drm_plane *plane,
94                                 struct drm_plane_state *new_state)
95 {
96         struct bochs_bo *bo;
97
98         if (!new_state->fb)
99                 return 0;
100         bo = gem_to_bochs_bo(new_state->fb->obj[0]);
101         return bochs_bo_pin(bo, TTM_PL_FLAG_VRAM);
102 }
103
104 static void bochs_plane_cleanup_fb(struct drm_plane *plane,
105                                    struct drm_plane_state *old_state)
106 {
107         struct bochs_bo *bo;
108
109         if (!old_state->fb)
110                 return;
111         bo = gem_to_bochs_bo(old_state->fb->obj[0]);
112         bochs_bo_unpin(bo);
113 }
114
115 static const struct drm_plane_helper_funcs bochs_plane_helper_funcs = {
116         .atomic_update = bochs_plane_atomic_update,
117         .prepare_fb = bochs_plane_prepare_fb,
118         .cleanup_fb = bochs_plane_cleanup_fb,
119 };
120
121 static const struct drm_plane_funcs bochs_plane_funcs = {
122        .update_plane   = drm_atomic_helper_update_plane,
123        .disable_plane  = drm_atomic_helper_disable_plane,
124        .destroy        = drm_primary_helper_destroy,
125        .reset          = drm_atomic_helper_plane_reset,
126        .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
127        .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
128 };
129
130 static struct drm_plane *bochs_primary_plane(struct drm_device *dev)
131 {
132         struct drm_plane *primary;
133         int ret;
134
135         primary = kzalloc(sizeof(*primary), GFP_KERNEL);
136         if (primary == NULL) {
137                 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
138                 return NULL;
139         }
140
141         ret = drm_universal_plane_init(dev, primary, 0,
142                                        &bochs_plane_funcs,
143                                        bochs_formats,
144                                        ARRAY_SIZE(bochs_formats),
145                                        NULL,
146                                        DRM_PLANE_TYPE_PRIMARY, NULL);
147         if (ret) {
148                 kfree(primary);
149                 return NULL;
150         }
151
152         drm_plane_helper_add(primary, &bochs_plane_helper_funcs);
153         return primary;
154 }
155
156 static void bochs_crtc_init(struct drm_device *dev)
157 {
158         struct bochs_device *bochs = dev->dev_private;
159         struct drm_crtc *crtc = &bochs->crtc;
160         struct drm_plane *primary = bochs_primary_plane(dev);
161
162         drm_crtc_init_with_planes(dev, crtc, primary, NULL,
163                                   &bochs_crtc_funcs, NULL);
164         drm_crtc_helper_add(crtc, &bochs_helper_funcs);
165 }
166
167 static const struct drm_encoder_funcs bochs_encoder_encoder_funcs = {
168         .destroy = drm_encoder_cleanup,
169 };
170
171 static void bochs_encoder_init(struct drm_device *dev)
172 {
173         struct bochs_device *bochs = dev->dev_private;
174         struct drm_encoder *encoder = &bochs->encoder;
175
176         encoder->possible_crtcs = 0x1;
177         drm_encoder_init(dev, encoder, &bochs_encoder_encoder_funcs,
178                          DRM_MODE_ENCODER_DAC, NULL);
179 }
180
181
182 static int bochs_connector_get_modes(struct drm_connector *connector)
183 {
184         struct bochs_device *bochs =
185                 container_of(connector, struct bochs_device, connector);
186         int count = 0;
187
188         if (bochs->edid)
189                 count = drm_add_edid_modes(connector, bochs->edid);
190
191         if (!count) {
192                 count = drm_add_modes_noedid(connector, 8192, 8192);
193                 drm_set_preferred_mode(connector, defx, defy);
194         }
195         return count;
196 }
197
198 static enum drm_mode_status bochs_connector_mode_valid(struct drm_connector *connector,
199                                       struct drm_display_mode *mode)
200 {
201         struct bochs_device *bochs =
202                 container_of(connector, struct bochs_device, connector);
203         unsigned long size = mode->hdisplay * mode->vdisplay * 4;
204
205         /*
206          * Make sure we can fit two framebuffers into video memory.
207          * This allows up to 1600x1200 with 16 MB (default size).
208          * If you want more try this:
209          *     'qemu -vga std -global VGA.vgamem_mb=32 $otherargs'
210          */
211         if (size * 2 > bochs->fb_size)
212                 return MODE_BAD;
213
214         return MODE_OK;
215 }
216
217 static const struct drm_connector_helper_funcs bochs_connector_connector_helper_funcs = {
218         .get_modes = bochs_connector_get_modes,
219         .mode_valid = bochs_connector_mode_valid,
220 };
221
222 static const struct drm_connector_funcs bochs_connector_connector_funcs = {
223         .dpms = drm_helper_connector_dpms,
224         .fill_modes = drm_helper_probe_single_connector_modes,
225         .destroy = drm_connector_cleanup,
226         .reset = drm_atomic_helper_connector_reset,
227         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
228         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
229 };
230
231 static void bochs_connector_init(struct drm_device *dev)
232 {
233         struct bochs_device *bochs = dev->dev_private;
234         struct drm_connector *connector = &bochs->connector;
235
236         drm_connector_init(dev, connector, &bochs_connector_connector_funcs,
237                            DRM_MODE_CONNECTOR_VIRTUAL);
238         drm_connector_helper_add(connector,
239                                  &bochs_connector_connector_helper_funcs);
240         drm_connector_register(connector);
241
242         bochs_hw_load_edid(bochs);
243         if (bochs->edid) {
244                 DRM_INFO("Found EDID data blob.\n");
245                 drm_connector_attach_edid_property(connector);
246                 drm_connector_update_edid_property(connector, bochs->edid);
247         }
248 }
249
250 static struct drm_framebuffer *
251 bochs_gem_fb_create(struct drm_device *dev, struct drm_file *file,
252                     const struct drm_mode_fb_cmd2 *mode_cmd)
253 {
254         if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888 &&
255             mode_cmd->pixel_format != DRM_FORMAT_BGRX8888)
256                 return ERR_PTR(-EINVAL);
257
258         return drm_gem_fb_create(dev, file, mode_cmd);
259 }
260
261 const struct drm_mode_config_funcs bochs_mode_funcs = {
262         .fb_create = bochs_gem_fb_create,
263         .atomic_check = drm_atomic_helper_check,
264         .atomic_commit = drm_atomic_helper_commit,
265 };
266
267 int bochs_kms_init(struct bochs_device *bochs)
268 {
269         drm_mode_config_init(bochs->dev);
270
271         bochs->dev->mode_config.max_width = 8192;
272         bochs->dev->mode_config.max_height = 8192;
273
274         bochs->dev->mode_config.fb_base = bochs->fb_base;
275         bochs->dev->mode_config.preferred_depth = 24;
276         bochs->dev->mode_config.prefer_shadow = 0;
277         bochs->dev->mode_config.quirk_addfb_prefer_host_byte_order = true;
278
279         bochs->dev->mode_config.funcs = &bochs_mode_funcs;
280
281         bochs_crtc_init(bochs->dev);
282         bochs_encoder_init(bochs->dev);
283         bochs_connector_init(bochs->dev);
284         drm_connector_attach_encoder(&bochs->connector,
285                                           &bochs->encoder);
286
287         drm_mode_config_reset(bochs->dev);
288
289         return 0;
290 }
291
292 void bochs_kms_fini(struct bochs_device *bochs)
293 {
294         drm_atomic_helper_shutdown(bochs->dev);
295         drm_mode_config_cleanup(bochs->dev);
296 }