Merge branch 'akpm' (patches from Andrew)
[linux/fpc-iii.git] / drivers / gpu / drm / virtio / virtgpu_display.c
blob0966208ec30d7a122f9b1dde7572d0e5d5d43101
1 /*
2 * Copyright (C) 2015 Red Hat, Inc.
3 * All Rights Reserved.
5 * Authors:
6 * Dave Airlie
7 * Alon Levy
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_damage_helper.h>
30 #include <drm/drm_fourcc.h>
31 #include <drm/drm_gem_framebuffer_helper.h>
32 #include <drm/drm_probe_helper.h>
33 #include <drm/drm_vblank.h>
35 #include "virtgpu_drv.h"
37 #define XRES_MIN 32
38 #define YRES_MIN 32
40 #define XRES_DEF 1024
41 #define YRES_DEF 768
43 #define XRES_MAX 8192
44 #define YRES_MAX 8192
46 #define drm_connector_to_virtio_gpu_output(x) \
47 container_of(x, struct virtio_gpu_output, conn)
49 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
50 .set_config = drm_atomic_helper_set_config,
51 .destroy = drm_crtc_cleanup,
53 .page_flip = drm_atomic_helper_page_flip,
54 .reset = drm_atomic_helper_crtc_reset,
55 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
56 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
59 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
60 .create_handle = drm_gem_fb_create_handle,
61 .destroy = drm_gem_fb_destroy,
62 .dirty = drm_atomic_helper_dirtyfb,
65 static int
66 virtio_gpu_framebuffer_init(struct drm_device *dev,
67 struct virtio_gpu_framebuffer *vgfb,
68 const struct drm_mode_fb_cmd2 *mode_cmd,
69 struct drm_gem_object *obj)
71 int ret;
73 vgfb->base.obj[0] = obj;
75 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
77 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
78 if (ret) {
79 vgfb->base.obj[0] = NULL;
80 return ret;
82 return 0;
85 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
87 struct drm_device *dev = crtc->dev;
88 struct virtio_gpu_device *vgdev = dev->dev_private;
89 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
91 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
92 crtc->mode.hdisplay,
93 crtc->mode.vdisplay, 0, 0);
96 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
97 struct drm_crtc_state *old_state)
99 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
101 output->enabled = true;
104 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
105 struct drm_crtc_state *old_state)
107 struct drm_device *dev = crtc->dev;
108 struct virtio_gpu_device *vgdev = dev->dev_private;
109 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
111 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
112 output->enabled = false;
115 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
116 struct drm_crtc_state *state)
118 return 0;
121 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
122 struct drm_crtc_state *old_state)
124 unsigned long flags;
126 spin_lock_irqsave(&crtc->dev->event_lock, flags);
127 if (crtc->state->event)
128 drm_crtc_send_vblank_event(crtc, crtc->state->event);
129 crtc->state->event = NULL;
130 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
133 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
134 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
135 .atomic_check = virtio_gpu_crtc_atomic_check,
136 .atomic_flush = virtio_gpu_crtc_atomic_flush,
137 .atomic_enable = virtio_gpu_crtc_atomic_enable,
138 .atomic_disable = virtio_gpu_crtc_atomic_disable,
141 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
142 struct drm_display_mode *mode,
143 struct drm_display_mode *adjusted_mode)
147 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
151 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
155 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
157 struct virtio_gpu_output *output =
158 drm_connector_to_virtio_gpu_output(connector);
159 struct drm_display_mode *mode = NULL;
160 int count, width, height;
162 if (output->edid) {
163 count = drm_add_edid_modes(connector, output->edid);
164 if (count)
165 return count;
168 width = le32_to_cpu(output->info.r.width);
169 height = le32_to_cpu(output->info.r.height);
170 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
172 if (width == 0 || height == 0) {
173 width = XRES_DEF;
174 height = YRES_DEF;
175 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
176 } else {
177 DRM_DEBUG("add mode: %dx%d\n", width, height);
178 mode = drm_cvt_mode(connector->dev, width, height, 60,
179 false, false, false);
180 mode->type |= DRM_MODE_TYPE_PREFERRED;
181 drm_mode_probed_add(connector, mode);
182 count++;
185 return count;
188 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
189 struct drm_display_mode *mode)
191 struct virtio_gpu_output *output =
192 drm_connector_to_virtio_gpu_output(connector);
193 int width, height;
195 width = le32_to_cpu(output->info.r.width);
196 height = le32_to_cpu(output->info.r.height);
198 if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
199 return MODE_OK;
200 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
201 return MODE_OK;
202 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 &&
203 mode->vdisplay <= height && mode->vdisplay >= height - 16)
204 return MODE_OK;
206 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
207 return MODE_BAD;
210 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
211 .mode_set = virtio_gpu_enc_mode_set,
212 .enable = virtio_gpu_enc_enable,
213 .disable = virtio_gpu_enc_disable,
216 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
217 .get_modes = virtio_gpu_conn_get_modes,
218 .mode_valid = virtio_gpu_conn_mode_valid,
221 static enum drm_connector_status virtio_gpu_conn_detect(
222 struct drm_connector *connector,
223 bool force)
225 struct virtio_gpu_output *output =
226 drm_connector_to_virtio_gpu_output(connector);
228 if (output->info.enabled)
229 return connector_status_connected;
230 else
231 return connector_status_disconnected;
234 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
236 drm_connector_unregister(connector);
237 drm_connector_cleanup(connector);
240 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
241 .detect = virtio_gpu_conn_detect,
242 .fill_modes = drm_helper_probe_single_connector_modes,
243 .destroy = virtio_gpu_conn_destroy,
244 .reset = drm_atomic_helper_connector_reset,
245 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
246 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
249 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
250 .destroy = drm_encoder_cleanup,
253 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
255 struct drm_device *dev = vgdev->ddev;
256 struct virtio_gpu_output *output = vgdev->outputs + index;
257 struct drm_connector *connector = &output->conn;
258 struct drm_encoder *encoder = &output->enc;
259 struct drm_crtc *crtc = &output->crtc;
260 struct drm_plane *primary, *cursor;
262 output->index = index;
263 if (index == 0) {
264 output->info.enabled = cpu_to_le32(true);
265 output->info.r.width = cpu_to_le32(XRES_DEF);
266 output->info.r.height = cpu_to_le32(YRES_DEF);
269 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
270 if (IS_ERR(primary))
271 return PTR_ERR(primary);
272 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
273 if (IS_ERR(cursor))
274 return PTR_ERR(cursor);
275 drm_crtc_init_with_planes(dev, crtc, primary, cursor,
276 &virtio_gpu_crtc_funcs, NULL);
277 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
279 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
280 DRM_MODE_CONNECTOR_VIRTUAL);
281 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
282 if (vgdev->has_edid)
283 drm_connector_attach_edid_property(connector);
285 drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
286 DRM_MODE_ENCODER_VIRTUAL, NULL);
287 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
288 encoder->possible_crtcs = 1 << index;
290 drm_connector_attach_encoder(connector, encoder);
291 drm_connector_register(connector);
292 return 0;
295 static struct drm_framebuffer *
296 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
297 struct drm_file *file_priv,
298 const struct drm_mode_fb_cmd2 *mode_cmd)
300 struct drm_gem_object *obj = NULL;
301 struct virtio_gpu_framebuffer *virtio_gpu_fb;
302 int ret;
304 if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
305 mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
306 return ERR_PTR(-ENOENT);
308 /* lookup object associated with res handle */
309 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
310 if (!obj)
311 return ERR_PTR(-EINVAL);
313 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
314 if (virtio_gpu_fb == NULL)
315 return ERR_PTR(-ENOMEM);
317 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
318 if (ret) {
319 kfree(virtio_gpu_fb);
320 drm_gem_object_put_unlocked(obj);
321 return NULL;
324 return &virtio_gpu_fb->base;
327 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
329 struct drm_device *dev = state->dev;
331 drm_atomic_helper_commit_modeset_disables(dev, state);
332 drm_atomic_helper_commit_modeset_enables(dev, state);
333 drm_atomic_helper_commit_planes(dev, state, 0);
335 drm_atomic_helper_commit_hw_done(state);
337 drm_atomic_helper_wait_for_vblanks(dev, state);
338 drm_atomic_helper_cleanup_planes(dev, state);
341 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
342 .atomic_commit_tail = vgdev_atomic_commit_tail,
345 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
346 .fb_create = virtio_gpu_user_framebuffer_create,
347 .atomic_check = drm_atomic_helper_check,
348 .atomic_commit = drm_atomic_helper_commit,
351 void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
353 int i;
355 drm_mode_config_init(vgdev->ddev);
356 vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
357 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
358 vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
360 /* modes will be validated against the framebuffer size */
361 vgdev->ddev->mode_config.min_width = XRES_MIN;
362 vgdev->ddev->mode_config.min_height = YRES_MIN;
363 vgdev->ddev->mode_config.max_width = XRES_MAX;
364 vgdev->ddev->mode_config.max_height = YRES_MAX;
366 for (i = 0 ; i < vgdev->num_scanouts; ++i)
367 vgdev_output_init(vgdev, i);
369 drm_mode_config_reset(vgdev->ddev);
372 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
374 int i;
376 for (i = 0 ; i < vgdev->num_scanouts; ++i)
377 kfree(vgdev->outputs[i].edid);
378 drm_atomic_helper_shutdown(vgdev->ddev);
379 drm_mode_config_cleanup(vgdev->ddev);