]> Git Repo - linux.git/blob - drivers/gpu/drm/virtio/virtgpu_display.c
Merge tag 'for-linus-4.16-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / gpu / drm / virtio / virtgpu_display.c
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  * All Rights Reserved.
4  *
5  * Authors:
6  *    Dave Airlie
7  *    Alon Levy
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include "virtgpu_drv.h"
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/drm_atomic_helper.h>
31
32 #define XRES_MIN    32
33 #define YRES_MIN    32
34
35 #define XRES_DEF  1024
36 #define YRES_DEF   768
37
38 #define XRES_MAX  8192
39 #define YRES_MAX  8192
40
41 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
42         .set_config             = drm_atomic_helper_set_config,
43         .destroy                = drm_crtc_cleanup,
44
45         .page_flip              = drm_atomic_helper_page_flip,
46         .reset                  = drm_atomic_helper_crtc_reset,
47         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
48         .atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
49 };
50
51 static void virtio_gpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
52 {
53         struct virtio_gpu_framebuffer *virtio_gpu_fb
54                 = to_virtio_gpu_framebuffer(fb);
55
56         drm_gem_object_put_unlocked(virtio_gpu_fb->obj);
57         drm_framebuffer_cleanup(fb);
58         kfree(virtio_gpu_fb);
59 }
60
61 static int
62 virtio_gpu_framebuffer_surface_dirty(struct drm_framebuffer *fb,
63                                      struct drm_file *file_priv,
64                                      unsigned flags, unsigned color,
65                                      struct drm_clip_rect *clips,
66                                      unsigned num_clips)
67 {
68         struct virtio_gpu_framebuffer *virtio_gpu_fb
69                 = to_virtio_gpu_framebuffer(fb);
70
71         return virtio_gpu_surface_dirty(virtio_gpu_fb, clips, num_clips);
72 }
73
74 static int
75 virtio_gpu_framebuffer_create_handle(struct drm_framebuffer *fb,
76                                      struct drm_file *file_priv,
77                                      unsigned int *handle)
78 {
79         struct virtio_gpu_framebuffer *virtio_gpu_fb =
80                 to_virtio_gpu_framebuffer(fb);
81
82         return drm_gem_handle_create(file_priv, virtio_gpu_fb->obj, handle);
83 }
84
85 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
86         .create_handle = virtio_gpu_framebuffer_create_handle,
87         .destroy = virtio_gpu_user_framebuffer_destroy,
88         .dirty = virtio_gpu_framebuffer_surface_dirty,
89 };
90
91 int
92 virtio_gpu_framebuffer_init(struct drm_device *dev,
93                             struct virtio_gpu_framebuffer *vgfb,
94                             const struct drm_mode_fb_cmd2 *mode_cmd,
95                             struct drm_gem_object *obj)
96 {
97         int ret;
98         struct virtio_gpu_object *bo;
99         vgfb->obj = obj;
100
101         bo = gem_to_virtio_gpu_obj(obj);
102
103         drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
104
105         ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
106         if (ret) {
107                 vgfb->obj = NULL;
108                 return ret;
109         }
110
111         spin_lock_init(&vgfb->dirty_lock);
112         vgfb->x1 = vgfb->y1 = INT_MAX;
113         vgfb->x2 = vgfb->y2 = 0;
114         return 0;
115 }
116
117 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
118 {
119         struct drm_device *dev = crtc->dev;
120         struct virtio_gpu_device *vgdev = dev->dev_private;
121         struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
122
123         virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
124                                    crtc->mode.hdisplay,
125                                    crtc->mode.vdisplay, 0, 0);
126 }
127
128 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
129                                           struct drm_crtc_state *old_state)
130 {
131 }
132
133 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
134                                            struct drm_crtc_state *old_state)
135 {
136         struct drm_device *dev = crtc->dev;
137         struct virtio_gpu_device *vgdev = dev->dev_private;
138         struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
139
140         virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
141 }
142
143 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
144                                         struct drm_crtc_state *state)
145 {
146         return 0;
147 }
148
149 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
150                                          struct drm_crtc_state *old_state)
151 {
152         unsigned long flags;
153
154         spin_lock_irqsave(&crtc->dev->event_lock, flags);
155         if (crtc->state->event)
156                 drm_crtc_send_vblank_event(crtc, crtc->state->event);
157         crtc->state->event = NULL;
158         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
159 }
160
161 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
162         .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
163         .atomic_check  = virtio_gpu_crtc_atomic_check,
164         .atomic_flush  = virtio_gpu_crtc_atomic_flush,
165         .atomic_enable = virtio_gpu_crtc_atomic_enable,
166         .atomic_disable = virtio_gpu_crtc_atomic_disable,
167 };
168
169 static void virtio_gpu_enc_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 virtio_gpu_enc_enable(struct drm_encoder *encoder)
176 {
177 }
178
179 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
180 {
181 }
182
183 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
184 {
185         struct virtio_gpu_output *output =
186                 drm_connector_to_virtio_gpu_output(connector);
187         struct drm_display_mode *mode = NULL;
188         int count, width, height;
189
190         width  = le32_to_cpu(output->info.r.width);
191         height = le32_to_cpu(output->info.r.height);
192         count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
193
194         if (width == 0 || height == 0) {
195                 width = XRES_DEF;
196                 height = YRES_DEF;
197                 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
198         } else {
199                 DRM_DEBUG("add mode: %dx%d\n", width, height);
200                 mode = drm_cvt_mode(connector->dev, width, height, 60,
201                                     false, false, false);
202                 mode->type |= DRM_MODE_TYPE_PREFERRED;
203                 drm_mode_probed_add(connector, mode);
204                 count++;
205         }
206
207         return count;
208 }
209
210 static int virtio_gpu_conn_mode_valid(struct drm_connector *connector,
211                                       struct drm_display_mode *mode)
212 {
213         struct virtio_gpu_output *output =
214                 drm_connector_to_virtio_gpu_output(connector);
215         int width, height;
216
217         width  = le32_to_cpu(output->info.r.width);
218         height = le32_to_cpu(output->info.r.height);
219
220         if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
221                 return MODE_OK;
222         if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
223                 return MODE_OK;
224         if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 &&
225             mode->vdisplay <= height && mode->vdisplay >= height - 16)
226                 return MODE_OK;
227
228         DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
229         return MODE_BAD;
230 }
231
232 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
233         .mode_set   = virtio_gpu_enc_mode_set,
234         .enable     = virtio_gpu_enc_enable,
235         .disable    = virtio_gpu_enc_disable,
236 };
237
238 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
239         .get_modes    = virtio_gpu_conn_get_modes,
240         .mode_valid   = virtio_gpu_conn_mode_valid,
241 };
242
243 static enum drm_connector_status virtio_gpu_conn_detect(
244                         struct drm_connector *connector,
245                         bool force)
246 {
247         struct virtio_gpu_output *output =
248                 drm_connector_to_virtio_gpu_output(connector);
249
250         if (output->info.enabled)
251                 return connector_status_connected;
252         else
253                 return connector_status_disconnected;
254 }
255
256 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
257 {
258         struct virtio_gpu_output *virtio_gpu_output =
259                 drm_connector_to_virtio_gpu_output(connector);
260
261         drm_connector_unregister(connector);
262         drm_connector_cleanup(connector);
263         kfree(virtio_gpu_output);
264 }
265
266 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
267         .detect = virtio_gpu_conn_detect,
268         .fill_modes = drm_helper_probe_single_connector_modes,
269         .destroy = virtio_gpu_conn_destroy,
270         .reset = drm_atomic_helper_connector_reset,
271         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
272         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
273 };
274
275 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
276         .destroy = drm_encoder_cleanup,
277 };
278
279 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
280 {
281         struct drm_device *dev = vgdev->ddev;
282         struct virtio_gpu_output *output = vgdev->outputs + index;
283         struct drm_connector *connector = &output->conn;
284         struct drm_encoder *encoder = &output->enc;
285         struct drm_crtc *crtc = &output->crtc;
286         struct drm_plane *primary, *cursor;
287
288         output->index = index;
289         if (index == 0) {
290                 output->info.enabled = cpu_to_le32(true);
291                 output->info.r.width = cpu_to_le32(XRES_DEF);
292                 output->info.r.height = cpu_to_le32(YRES_DEF);
293         }
294
295         primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
296         if (IS_ERR(primary))
297                 return PTR_ERR(primary);
298         cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
299         if (IS_ERR(cursor))
300                 return PTR_ERR(cursor);
301         drm_crtc_init_with_planes(dev, crtc, primary, cursor,
302                                   &virtio_gpu_crtc_funcs, NULL);
303         drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
304         primary->crtc = crtc;
305         cursor->crtc = crtc;
306
307         drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
308                            DRM_MODE_CONNECTOR_VIRTUAL);
309         drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
310
311         drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
312                          DRM_MODE_ENCODER_VIRTUAL, NULL);
313         drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
314         encoder->possible_crtcs = 1 << index;
315
316         drm_mode_connector_attach_encoder(connector, encoder);
317         drm_connector_register(connector);
318         return 0;
319 }
320
321 static struct drm_framebuffer *
322 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
323                                    struct drm_file *file_priv,
324                                    const struct drm_mode_fb_cmd2 *mode_cmd)
325 {
326         struct drm_gem_object *obj = NULL;
327         struct virtio_gpu_framebuffer *virtio_gpu_fb;
328         int ret;
329
330         /* lookup object associated with res handle */
331         obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
332         if (!obj)
333                 return ERR_PTR(-EINVAL);
334
335         virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
336         if (virtio_gpu_fb == NULL)
337                 return ERR_PTR(-ENOMEM);
338
339         ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
340         if (ret) {
341                 kfree(virtio_gpu_fb);
342                 drm_gem_object_put_unlocked(obj);
343                 return NULL;
344         }
345
346         return &virtio_gpu_fb->base;
347 }
348
349 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
350 {
351         struct drm_device *dev = state->dev;
352
353         drm_atomic_helper_commit_modeset_disables(dev, state);
354         drm_atomic_helper_commit_modeset_enables(dev, state);
355         drm_atomic_helper_commit_planes(dev, state, 0);
356
357         drm_atomic_helper_commit_hw_done(state);
358
359         drm_atomic_helper_wait_for_vblanks(dev, state);
360         drm_atomic_helper_cleanup_planes(dev, state);
361 }
362
363 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
364         .atomic_commit_tail = vgdev_atomic_commit_tail,
365 };
366
367 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
368         .fb_create = virtio_gpu_user_framebuffer_create,
369         .atomic_check = drm_atomic_helper_check,
370         .atomic_commit = drm_atomic_helper_commit,
371 };
372
373 int virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
374 {
375         int i;
376
377         drm_mode_config_init(vgdev->ddev);
378         vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
379         vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
380
381         /* modes will be validated against the framebuffer size */
382         vgdev->ddev->mode_config.min_width = XRES_MIN;
383         vgdev->ddev->mode_config.min_height = YRES_MIN;
384         vgdev->ddev->mode_config.max_width = XRES_MAX;
385         vgdev->ddev->mode_config.max_height = YRES_MAX;
386
387         for (i = 0 ; i < vgdev->num_scanouts; ++i)
388                 vgdev_output_init(vgdev, i);
389
390         drm_mode_config_reset(vgdev->ddev);
391         return 0;
392 }
393
394 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
395 {
396         virtio_gpu_fbdev_fini(vgdev);
397         drm_mode_config_cleanup(vgdev->ddev);
398 }
This page took 0.060675 seconds and 4 git commands to generate.