Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Red Hat, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "virtgpu_drv.h" |
| 27 | #include <drm/drm_plane_helper.h> |
| 28 | #include <drm/drm_atomic_helper.h> |
| 29 | |
| 30 | static const uint32_t virtio_gpu_formats[] = { |
| 31 | DRM_FORMAT_XRGB8888, |
| 32 | DRM_FORMAT_ARGB8888, |
| 33 | DRM_FORMAT_BGRX8888, |
| 34 | DRM_FORMAT_BGRA8888, |
| 35 | DRM_FORMAT_RGBX8888, |
| 36 | DRM_FORMAT_RGBA8888, |
| 37 | DRM_FORMAT_XBGR8888, |
| 38 | DRM_FORMAT_ABGR8888, |
| 39 | }; |
| 40 | |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 41 | static const uint32_t virtio_gpu_cursor_formats[] = { |
| 42 | DRM_FORMAT_ARGB8888, |
| 43 | }; |
| 44 | |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 45 | static void virtio_gpu_plane_destroy(struct drm_plane *plane) |
| 46 | { |
| 47 | kfree(plane); |
| 48 | } |
| 49 | |
| 50 | static const struct drm_plane_funcs virtio_gpu_plane_funcs = { |
| 51 | .update_plane = drm_atomic_helper_update_plane, |
| 52 | .disable_plane = drm_atomic_helper_disable_plane, |
| 53 | .destroy = virtio_gpu_plane_destroy, |
| 54 | .reset = drm_atomic_helper_plane_reset, |
| 55 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 56 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 57 | }; |
| 58 | |
| 59 | static int virtio_gpu_plane_atomic_check(struct drm_plane *plane, |
| 60 | struct drm_plane_state *state) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 65 | static void virtio_gpu_primary_plane_update(struct drm_plane *plane, |
| 66 | struct drm_plane_state *old_state) |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 67 | { |
| 68 | struct drm_device *dev = plane->dev; |
| 69 | struct virtio_gpu_device *vgdev = dev->dev_private; |
Gerd Hoffmann | d3767d4 | 2016-05-27 14:20:24 +0200 | [diff] [blame] | 70 | struct virtio_gpu_output *output = NULL; |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 71 | struct virtio_gpu_framebuffer *vgfb; |
| 72 | struct virtio_gpu_object *bo; |
| 73 | uint32_t handle; |
| 74 | |
Gerd Hoffmann | d3767d4 | 2016-05-27 14:20:24 +0200 | [diff] [blame] | 75 | if (plane->state->crtc) |
| 76 | output = drm_crtc_to_virtio_gpu_output(plane->state->crtc); |
| 77 | if (old_state->crtc) |
| 78 | output = drm_crtc_to_virtio_gpu_output(old_state->crtc); |
| 79 | WARN_ON(!output); |
| 80 | |
Rob Herring | 11c94ac | 2016-01-13 15:52:07 -0600 | [diff] [blame] | 81 | if (plane->state->fb) { |
| 82 | vgfb = to_virtio_gpu_framebuffer(plane->state->fb); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 83 | bo = gem_to_virtio_gpu_obj(vgfb->obj); |
| 84 | handle = bo->hw_res_handle; |
Rob Herring | 4109e7f7 | 2016-01-13 15:52:09 -0600 | [diff] [blame] | 85 | if (bo->dumb) { |
| 86 | virtio_gpu_cmd_transfer_to_host_2d |
| 87 | (vgdev, handle, 0, |
| 88 | cpu_to_le32(plane->state->crtc_w), |
| 89 | cpu_to_le32(plane->state->crtc_h), |
| 90 | plane->state->crtc_x, plane->state->crtc_y, NULL); |
| 91 | } |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 92 | } else { |
| 93 | handle = 0; |
| 94 | } |
| 95 | |
| 96 | DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d\n", handle, |
| 97 | plane->state->crtc_w, plane->state->crtc_h, |
| 98 | plane->state->crtc_x, plane->state->crtc_y); |
| 99 | virtio_gpu_cmd_set_scanout(vgdev, output->index, handle, |
| 100 | plane->state->crtc_w, |
| 101 | plane->state->crtc_h, |
| 102 | plane->state->crtc_x, |
| 103 | plane->state->crtc_y); |
Rob Herring | bd17d1c | 2016-01-13 15:52:08 -0600 | [diff] [blame] | 104 | virtio_gpu_cmd_resource_flush(vgdev, handle, |
| 105 | plane->state->crtc_x, |
| 106 | plane->state->crtc_y, |
| 107 | plane->state->crtc_w, |
| 108 | plane->state->crtc_h); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 109 | } |
| 110 | |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 111 | static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, |
| 112 | struct drm_plane_state *old_state) |
| 113 | { |
| 114 | struct drm_device *dev = plane->dev; |
| 115 | struct virtio_gpu_device *vgdev = dev->dev_private; |
| 116 | struct virtio_gpu_output *output = NULL; |
| 117 | struct virtio_gpu_framebuffer *vgfb; |
| 118 | struct virtio_gpu_fence *fence = NULL; |
| 119 | struct virtio_gpu_object *bo = NULL; |
| 120 | uint32_t handle; |
| 121 | int ret = 0; |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 122 | |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 123 | if (plane->state->crtc) |
| 124 | output = drm_crtc_to_virtio_gpu_output(plane->state->crtc); |
| 125 | if (old_state->crtc) |
| 126 | output = drm_crtc_to_virtio_gpu_output(old_state->crtc); |
| 127 | WARN_ON(!output); |
| 128 | |
| 129 | if (plane->state->fb) { |
| 130 | vgfb = to_virtio_gpu_framebuffer(plane->state->fb); |
| 131 | bo = gem_to_virtio_gpu_obj(vgfb->obj); |
| 132 | handle = bo->hw_res_handle; |
| 133 | } else { |
| 134 | handle = 0; |
| 135 | } |
| 136 | |
| 137 | if (bo && bo->dumb && (plane->state->fb != old_state->fb)) { |
| 138 | /* new cursor -- update & wait */ |
| 139 | virtio_gpu_cmd_transfer_to_host_2d |
| 140 | (vgdev, handle, 0, |
| 141 | cpu_to_le32(plane->state->crtc_w), |
| 142 | cpu_to_le32(plane->state->crtc_h), |
| 143 | 0, 0, &fence); |
| 144 | ret = virtio_gpu_object_reserve(bo, false); |
| 145 | if (!ret) { |
| 146 | reservation_object_add_excl_fence(bo->tbo.resv, |
| 147 | &fence->f); |
| 148 | fence_put(&fence->f); |
| 149 | fence = NULL; |
| 150 | virtio_gpu_object_unreserve(bo); |
| 151 | virtio_gpu_object_wait(bo, false); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (plane->state->fb != old_state->fb) { |
Gerd Hoffmann | 86f752d | 2016-05-31 09:36:21 +0200 | [diff] [blame^] | 156 | DRM_DEBUG("update, handle %d, pos +%d+%d, hot %d,%d\n", handle, |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 157 | plane->state->crtc_x, |
Gerd Hoffmann | 86f752d | 2016-05-31 09:36:21 +0200 | [diff] [blame^] | 158 | plane->state->crtc_y, |
| 159 | plane->state->fb ? plane->state->fb->hot_x : 0, |
| 160 | plane->state->fb ? plane->state->fb->hot_y : 0); |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 161 | output->cursor.hdr.type = |
| 162 | cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR); |
| 163 | output->cursor.resource_id = cpu_to_le32(handle); |
Gerd Hoffmann | 86f752d | 2016-05-31 09:36:21 +0200 | [diff] [blame^] | 164 | if (plane->state->fb) { |
| 165 | output->cursor.hot_x = |
| 166 | cpu_to_le32(plane->state->fb->hot_x); |
| 167 | output->cursor.hot_y = |
| 168 | cpu_to_le32(plane->state->fb->hot_y); |
| 169 | } else { |
| 170 | output->cursor.hot_x = cpu_to_le32(0); |
| 171 | output->cursor.hot_y = cpu_to_le32(0); |
| 172 | } |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 173 | } else { |
| 174 | DRM_DEBUG("move +%d+%d\n", |
| 175 | plane->state->crtc_x, |
| 176 | plane->state->crtc_y); |
| 177 | output->cursor.hdr.type = |
| 178 | cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR); |
| 179 | } |
| 180 | output->cursor.pos.x = cpu_to_le32(plane->state->crtc_x); |
| 181 | output->cursor.pos.y = cpu_to_le32(plane->state->crtc_y); |
| 182 | virtio_gpu_cursor_ping(vgdev, output); |
| 183 | } |
| 184 | |
| 185 | static const struct drm_plane_helper_funcs virtio_gpu_primary_helper_funcs = { |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 186 | .atomic_check = virtio_gpu_plane_atomic_check, |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 187 | .atomic_update = virtio_gpu_primary_plane_update, |
| 188 | }; |
| 189 | |
| 190 | static const struct drm_plane_helper_funcs virtio_gpu_cursor_helper_funcs = { |
| 191 | .atomic_check = virtio_gpu_plane_atomic_check, |
| 192 | .atomic_update = virtio_gpu_cursor_plane_update, |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev, |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 196 | enum drm_plane_type type, |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 197 | int index) |
| 198 | { |
| 199 | struct drm_device *dev = vgdev->ddev; |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 200 | const struct drm_plane_helper_funcs *funcs; |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 201 | struct drm_plane *plane; |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 202 | const uint32_t *formats; |
| 203 | int ret, nformats; |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 204 | |
| 205 | plane = kzalloc(sizeof(*plane), GFP_KERNEL); |
| 206 | if (!plane) |
| 207 | return ERR_PTR(-ENOMEM); |
| 208 | |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 209 | if (type == DRM_PLANE_TYPE_CURSOR) { |
| 210 | formats = virtio_gpu_cursor_formats; |
| 211 | nformats = ARRAY_SIZE(virtio_gpu_cursor_formats); |
| 212 | funcs = &virtio_gpu_cursor_helper_funcs; |
| 213 | } else { |
| 214 | formats = virtio_gpu_formats; |
| 215 | nformats = ARRAY_SIZE(virtio_gpu_formats); |
| 216 | funcs = &virtio_gpu_primary_helper_funcs; |
| 217 | } |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 218 | ret = drm_universal_plane_init(dev, plane, 1 << index, |
| 219 | &virtio_gpu_plane_funcs, |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 220 | formats, nformats, |
| 221 | type, NULL); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 222 | if (ret) |
| 223 | goto err_plane_init; |
| 224 | |
Gerd Hoffmann | bbbed88 | 2016-05-26 11:42:52 +0200 | [diff] [blame] | 225 | drm_plane_helper_add(plane, funcs); |
Dave Airlie | dc5698e | 2013-09-09 10:02:56 +1000 | [diff] [blame] | 226 | return plane; |
| 227 | |
| 228 | err_plane_init: |
| 229 | kfree(plane); |
| 230 | return ERR_PTR(ret); |
| 231 | } |