Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
Sinclair Yeh | 54fbde8 | 2015-07-29 12:38:02 -0700 | [diff] [blame] | 3 | * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | |
| 28 | #include "vmwgfx_kms.h" |
Sinclair Yeh | 060e2ad | 2017-03-23 14:18:32 -0700 | [diff] [blame^] | 29 | #include <drm/drm_plane_helper.h> |
Sinclair Yeh | 9c2542a | 2017-03-23 11:33:39 -0700 | [diff] [blame] | 30 | #include <drm/drm_atomic.h> |
| 31 | #include <drm/drm_atomic_helper.h> |
Sinclair Yeh | 060e2ad | 2017-03-23 14:18:32 -0700 | [diff] [blame^] | 32 | #include <drm/drm_rect.h> |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 33 | |
Jakob Bornecrantz | 56d1c78 | 2011-10-04 20:13:22 +0200 | [diff] [blame] | 34 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 35 | /* Might need a hrtimer here? */ |
| 36 | #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1) |
| 37 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 38 | void vmw_du_cleanup(struct vmw_display_unit *du) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 39 | { |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 40 | drm_plane_cleanup(&du->primary); |
| 41 | drm_plane_cleanup(&du->cursor); |
| 42 | |
Thomas Wood | 34ea3d3 | 2014-05-29 16:57:41 +0100 | [diff] [blame] | 43 | drm_connector_unregister(&du->connector); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 44 | drm_crtc_cleanup(&du->crtc); |
| 45 | drm_encoder_cleanup(&du->encoder); |
| 46 | drm_connector_cleanup(&du->connector); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Display Unit Cursor functions |
| 51 | */ |
| 52 | |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 53 | static int vmw_cursor_update_image(struct vmw_private *dev_priv, |
| 54 | u32 *image, u32 width, u32 height, |
| 55 | u32 hotspotX, u32 hotspotY) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 56 | { |
| 57 | struct { |
| 58 | u32 cmd; |
| 59 | SVGAFifoCmdDefineAlphaCursor cursor; |
| 60 | } *cmd; |
| 61 | u32 image_size = width * height * 4; |
| 62 | u32 cmd_size = sizeof(*cmd) + image_size; |
| 63 | |
| 64 | if (!image) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | cmd = vmw_fifo_reserve(dev_priv, cmd_size); |
| 68 | if (unlikely(cmd == NULL)) { |
| 69 | DRM_ERROR("Fifo reserve failed.\n"); |
| 70 | return -ENOMEM; |
| 71 | } |
| 72 | |
| 73 | memset(cmd, 0, sizeof(*cmd)); |
| 74 | |
| 75 | memcpy(&cmd[1], image, image_size); |
| 76 | |
Thomas Hellstrom | b9eb1a6 | 2015-04-02 02:39:45 -0700 | [diff] [blame] | 77 | cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR; |
| 78 | cmd->cursor.id = 0; |
| 79 | cmd->cursor.width = width; |
| 80 | cmd->cursor.height = height; |
| 81 | cmd->cursor.hotspotX = hotspotX; |
| 82 | cmd->cursor.hotspotY = hotspotY; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 83 | |
Thomas Hellstrom | 4e0858a | 2015-11-05 02:18:55 -0800 | [diff] [blame] | 84 | vmw_fifo_commit_flush(dev_priv, cmd_size); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 89 | static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv, |
| 90 | struct vmw_dma_buffer *dmabuf, |
| 91 | u32 width, u32 height, |
| 92 | u32 hotspotX, u32 hotspotY) |
Jakob Bornecrantz | 6a91d97 | 2011-11-28 13:19:10 +0100 | [diff] [blame] | 93 | { |
| 94 | struct ttm_bo_kmap_obj map; |
| 95 | unsigned long kmap_offset; |
| 96 | unsigned long kmap_num; |
| 97 | void *virtual; |
| 98 | bool dummy; |
| 99 | int ret; |
| 100 | |
| 101 | kmap_offset = 0; |
| 102 | kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 103 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 104 | ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL); |
Jakob Bornecrantz | 6a91d97 | 2011-11-28 13:19:10 +0100 | [diff] [blame] | 105 | if (unlikely(ret != 0)) { |
| 106 | DRM_ERROR("reserve failed\n"); |
| 107 | return -EINVAL; |
| 108 | } |
| 109 | |
| 110 | ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map); |
| 111 | if (unlikely(ret != 0)) |
| 112 | goto err_unreserve; |
| 113 | |
| 114 | virtual = ttm_kmap_obj_virtual(&map, &dummy); |
| 115 | ret = vmw_cursor_update_image(dev_priv, virtual, width, height, |
| 116 | hotspotX, hotspotY); |
| 117 | |
| 118 | ttm_bo_kunmap(&map); |
| 119 | err_unreserve: |
| 120 | ttm_bo_unreserve(&dmabuf->base); |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 126 | static void vmw_cursor_update_position(struct vmw_private *dev_priv, |
| 127 | bool show, int x, int y) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 128 | { |
Thomas Hellstrom | b76ff5e | 2015-10-28 10:44:04 +0100 | [diff] [blame] | 129 | u32 *fifo_mem = dev_priv->mmio_virt; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 130 | uint32_t count; |
| 131 | |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 132 | spin_lock(&dev_priv->cursor_lock); |
Thomas Hellstrom | b76ff5e | 2015-10-28 10:44:04 +0100 | [diff] [blame] | 133 | vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON); |
| 134 | vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X); |
| 135 | vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y); |
| 136 | count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT); |
| 137 | vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT); |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 138 | spin_unlock(&dev_priv->cursor_lock); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Thomas Hellstrom | 8fbf9d9 | 2015-11-26 19:45:16 +0100 | [diff] [blame] | 141 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 142 | void vmw_kms_cursor_snoop(struct vmw_surface *srf, |
| 143 | struct ttm_object_file *tfile, |
| 144 | struct ttm_buffer_object *bo, |
| 145 | SVGA3dCmdHeader *header) |
| 146 | { |
| 147 | struct ttm_bo_kmap_obj map; |
| 148 | unsigned long kmap_offset; |
| 149 | unsigned long kmap_num; |
| 150 | SVGA3dCopyBox *box; |
| 151 | unsigned box_count; |
| 152 | void *virtual; |
| 153 | bool dummy; |
| 154 | struct vmw_dma_cmd { |
| 155 | SVGA3dCmdHeader header; |
| 156 | SVGA3dCmdSurfaceDMA dma; |
| 157 | } *cmd; |
Jakob Bornecrantz | 2ac8637 | 2011-11-03 21:03:08 +0100 | [diff] [blame] | 158 | int i, ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 159 | |
| 160 | cmd = container_of(header, struct vmw_dma_cmd, header); |
| 161 | |
| 162 | /* No snooper installed */ |
| 163 | if (!srf->snooper.image) |
| 164 | return; |
| 165 | |
| 166 | if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) { |
| 167 | DRM_ERROR("face and mipmap for cursors should never != 0\n"); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if (cmd->header.size < 64) { |
| 172 | DRM_ERROR("at least one full copy box must be given\n"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | box = (SVGA3dCopyBox *)&cmd[1]; |
| 177 | box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) / |
| 178 | sizeof(SVGA3dCopyBox); |
| 179 | |
Jakob Bornecrantz | 2ac8637 | 2011-11-03 21:03:08 +0100 | [diff] [blame] | 180 | if (cmd->dma.guest.ptr.offset % PAGE_SIZE || |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 181 | box->x != 0 || box->y != 0 || box->z != 0 || |
| 182 | box->srcx != 0 || box->srcy != 0 || box->srcz != 0 || |
Jakob Bornecrantz | 2ac8637 | 2011-11-03 21:03:08 +0100 | [diff] [blame] | 183 | box->d != 1 || box_count != 1) { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 184 | /* TODO handle none page aligned offsets */ |
Jakob Bornecrantz | 2ac8637 | 2011-11-03 21:03:08 +0100 | [diff] [blame] | 185 | /* TODO handle more dst & src != 0 */ |
| 186 | /* TODO handle more then one copy */ |
| 187 | DRM_ERROR("Cant snoop dma request for cursor!\n"); |
| 188 | DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n", |
| 189 | box->srcx, box->srcy, box->srcz, |
| 190 | box->x, box->y, box->z, |
| 191 | box->w, box->h, box->d, box_count, |
| 192 | cmd->dma.guest.ptr.offset); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT; |
| 197 | kmap_num = (64*64*4) >> PAGE_SHIFT; |
| 198 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 199 | ret = ttm_bo_reserve(bo, true, false, NULL); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 200 | if (unlikely(ret != 0)) { |
| 201 | DRM_ERROR("reserve failed\n"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map); |
| 206 | if (unlikely(ret != 0)) |
| 207 | goto err_unreserve; |
| 208 | |
| 209 | virtual = ttm_kmap_obj_virtual(&map, &dummy); |
| 210 | |
Jakob Bornecrantz | 2ac8637 | 2011-11-03 21:03:08 +0100 | [diff] [blame] | 211 | if (box->w == 64 && cmd->dma.guest.pitch == 64*4) { |
| 212 | memcpy(srf->snooper.image, virtual, 64*64*4); |
| 213 | } else { |
| 214 | /* Image is unsigned pointer. */ |
| 215 | for (i = 0; i < box->h; i++) |
| 216 | memcpy(srf->snooper.image + i * 64, |
| 217 | virtual + i * cmd->dma.guest.pitch, |
| 218 | box->w * 4); |
| 219 | } |
| 220 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 221 | srf->snooper.age++; |
| 222 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 223 | ttm_bo_kunmap(&map); |
| 224 | err_unreserve: |
| 225 | ttm_bo_unreserve(bo); |
| 226 | } |
| 227 | |
Thomas Hellstrom | 8fbf9d9 | 2015-11-26 19:45:16 +0100 | [diff] [blame] | 228 | /** |
| 229 | * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots |
| 230 | * |
| 231 | * @dev_priv: Pointer to the device private struct. |
| 232 | * |
| 233 | * Clears all legacy hotspots. |
| 234 | */ |
| 235 | void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv) |
| 236 | { |
| 237 | struct drm_device *dev = dev_priv->dev; |
| 238 | struct vmw_display_unit *du; |
| 239 | struct drm_crtc *crtc; |
| 240 | |
| 241 | drm_modeset_lock_all(dev); |
| 242 | drm_for_each_crtc(crtc, dev) { |
| 243 | du = vmw_crtc_to_du(crtc); |
| 244 | |
| 245 | du->hotspot_x = 0; |
| 246 | du->hotspot_y = 0; |
| 247 | } |
| 248 | drm_modeset_unlock_all(dev); |
| 249 | } |
| 250 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 251 | void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv) |
| 252 | { |
| 253 | struct drm_device *dev = dev_priv->dev; |
| 254 | struct vmw_display_unit *du; |
| 255 | struct drm_crtc *crtc; |
| 256 | |
| 257 | mutex_lock(&dev->mode_config.mutex); |
| 258 | |
| 259 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 260 | du = vmw_crtc_to_du(crtc); |
| 261 | if (!du->cursor_surface || |
| 262 | du->cursor_age == du->cursor_surface->snooper.age) |
| 263 | continue; |
| 264 | |
| 265 | du->cursor_age = du->cursor_surface->snooper.age; |
| 266 | vmw_cursor_update_image(dev_priv, |
| 267 | du->cursor_surface->snooper.image, |
Thomas Hellstrom | 8fbf9d9 | 2015-11-26 19:45:16 +0100 | [diff] [blame] | 268 | 64, 64, |
| 269 | du->hotspot_x + du->core_hotspot_x, |
| 270 | du->hotspot_y + du->core_hotspot_y); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | mutex_unlock(&dev->mode_config.mutex); |
| 274 | } |
| 275 | |
Sinclair Yeh | 36cc79b | 2017-03-23 11:28:11 -0700 | [diff] [blame] | 276 | |
| 277 | |
| 278 | /** |
| 279 | * vmw_du_cursor_plane_update() - Update cursor image and location |
| 280 | * |
| 281 | * @plane: plane object to update |
| 282 | * @crtc: owning CRTC of @plane |
| 283 | * @fb: framebuffer to flip onto plane |
| 284 | * @crtc_x: x offset of plane on crtc |
| 285 | * @crtc_y: y offset of plane on crtc |
| 286 | * @crtc_w: width of plane rectangle on crtc |
| 287 | * @crtc_h: height of plane rectangle on crtc |
| 288 | * @src_x: Not used |
| 289 | * @src_y: Not used |
| 290 | * @src_w: Not used |
| 291 | * @src_h: Not used |
| 292 | * |
| 293 | * |
| 294 | * RETURNS: |
| 295 | * Zero on success, error code on failure |
| 296 | */ |
| 297 | int vmw_du_cursor_plane_update(struct drm_plane *plane, |
| 298 | struct drm_crtc *crtc, |
| 299 | struct drm_framebuffer *fb, |
| 300 | int crtc_x, int crtc_y, |
| 301 | unsigned int crtc_w, |
| 302 | unsigned int crtc_h, |
| 303 | uint32_t src_x, uint32_t src_y, |
| 304 | uint32_t src_w, uint32_t src_h) |
| 305 | { |
| 306 | struct vmw_private *dev_priv = vmw_priv(crtc->dev); |
| 307 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 308 | struct vmw_surface *surface = NULL; |
| 309 | struct vmw_dma_buffer *dmabuf = NULL; |
| 310 | s32 hotspot_x, hotspot_y; |
| 311 | int ret; |
| 312 | |
| 313 | hotspot_x = du->hotspot_x + fb->hot_x; |
| 314 | hotspot_y = du->hotspot_y + fb->hot_y; |
| 315 | |
| 316 | /* A lot of the code assumes this */ |
| 317 | if (crtc_w != 64 || crtc_h != 64) { |
| 318 | ret = -EINVAL; |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | if (vmw_framebuffer_to_vfb(fb)->dmabuf) |
| 323 | dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer; |
| 324 | else |
| 325 | surface = vmw_framebuffer_to_vfbs(fb)->surface; |
| 326 | |
| 327 | if (surface && !surface->snooper.image) { |
| 328 | DRM_ERROR("surface not suitable for cursor\n"); |
| 329 | ret = -EINVAL; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | /* setup new image */ |
| 334 | ret = 0; |
| 335 | if (surface) { |
| 336 | /* vmw_user_surface_lookup takes one reference */ |
| 337 | du->cursor_surface = surface; |
| 338 | |
| 339 | du->cursor_age = du->cursor_surface->snooper.age; |
| 340 | |
| 341 | ret = vmw_cursor_update_image(dev_priv, surface->snooper.image, |
| 342 | 64, 64, hotspot_x, hotspot_y); |
| 343 | } else if (dmabuf) { |
| 344 | /* vmw_user_surface_lookup takes one reference */ |
| 345 | du->cursor_dmabuf = dmabuf; |
| 346 | |
| 347 | ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, crtc_w, crtc_h, |
| 348 | hotspot_x, hotspot_y); |
| 349 | } else { |
| 350 | vmw_cursor_update_position(dev_priv, false, 0, 0); |
| 351 | goto out; |
| 352 | } |
| 353 | |
| 354 | if (!ret) { |
| 355 | du->cursor_x = crtc_x + du->set_gui_x; |
| 356 | du->cursor_y = crtc_y + du->set_gui_y; |
| 357 | |
| 358 | vmw_cursor_update_position(dev_priv, true, |
| 359 | du->cursor_x + hotspot_x, |
| 360 | du->cursor_y + hotspot_y); |
| 361 | } |
| 362 | |
| 363 | out: |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | int vmw_du_cursor_plane_disable(struct drm_plane *plane) |
| 369 | { |
| 370 | if (plane->fb) { |
| 371 | drm_framebuffer_unreference(plane->fb); |
| 372 | plane->fb = NULL; |
| 373 | } |
| 374 | |
| 375 | return -EINVAL; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | void vmw_du_cursor_plane_destroy(struct drm_plane *plane) |
| 380 | { |
| 381 | vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0); |
| 382 | |
| 383 | drm_plane_cleanup(plane); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | void vmw_du_primary_plane_destroy(struct drm_plane *plane) |
| 388 | { |
| 389 | drm_plane_cleanup(plane); |
| 390 | |
| 391 | /* Planes are static in our case so we don't free it */ |
| 392 | } |
| 393 | |
| 394 | |
Sinclair Yeh | 060e2ad | 2017-03-23 14:18:32 -0700 | [diff] [blame^] | 395 | /** |
| 396 | * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface |
| 397 | * |
| 398 | * @vps: plane state associated with the display surface |
| 399 | * @unreference: true if we also want to unreference the display. |
| 400 | */ |
| 401 | void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps, |
| 402 | bool unreference) |
| 403 | { |
| 404 | if (vps->surf) { |
| 405 | if (vps->pinned) { |
| 406 | vmw_resource_unpin(&vps->surf->res); |
| 407 | vps->pinned--; |
| 408 | } |
| 409 | |
| 410 | if (unreference) { |
| 411 | if (vps->pinned) |
| 412 | DRM_ERROR("Surface still pinned\n"); |
| 413 | vmw_surface_unreference(&vps->surf); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * vmw_du_plane_cleanup_fb - Unpins the cursor |
| 421 | * |
| 422 | * @plane: display plane |
| 423 | * @old_state: Contains the FB to clean up |
| 424 | * |
| 425 | * Unpins the framebuffer surface |
| 426 | * |
| 427 | * Returns 0 on success |
| 428 | */ |
| 429 | void |
| 430 | vmw_du_plane_cleanup_fb(struct drm_plane *plane, |
| 431 | struct drm_plane_state *old_state) |
| 432 | { |
| 433 | struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state); |
| 434 | |
| 435 | vmw_du_plane_unpin_surf(vps, false); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /** |
| 440 | * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it |
| 441 | * |
| 442 | * @plane: display plane |
| 443 | * @new_state: info on the new plane state, including the FB |
| 444 | * |
| 445 | * Returns 0 on success |
| 446 | */ |
| 447 | int |
| 448 | vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane, |
| 449 | struct drm_plane_state *new_state) |
| 450 | { |
| 451 | struct drm_framebuffer *fb = new_state->fb; |
| 452 | struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state); |
| 453 | |
| 454 | |
| 455 | if (vps->surf) |
| 456 | vmw_surface_unreference(&vps->surf); |
| 457 | |
| 458 | if (vps->dmabuf) |
| 459 | vmw_dmabuf_unreference(&vps->dmabuf); |
| 460 | |
| 461 | if (fb) { |
| 462 | if (vmw_framebuffer_to_vfb(fb)->dmabuf) { |
| 463 | vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer; |
| 464 | vmw_dmabuf_reference(vps->dmabuf); |
| 465 | } else { |
| 466 | vps->surf = vmw_framebuffer_to_vfbs(fb)->surface; |
| 467 | vmw_surface_reference(vps->surf); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void |
| 476 | vmw_du_cursor_plane_atomic_disable(struct drm_plane *plane, |
| 477 | struct drm_plane_state *old_state) |
| 478 | { |
| 479 | struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc; |
| 480 | struct vmw_private *dev_priv = vmw_priv(crtc->dev); |
| 481 | |
| 482 | drm_atomic_set_fb_for_plane(plane->state, NULL); |
| 483 | vmw_cursor_update_position(dev_priv, false, 0, 0); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | void |
| 488 | vmw_du_cursor_plane_atomic_update(struct drm_plane *plane, |
| 489 | struct drm_plane_state *old_state) |
| 490 | { |
| 491 | struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc; |
| 492 | struct vmw_private *dev_priv = vmw_priv(crtc->dev); |
| 493 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 494 | struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state); |
| 495 | s32 hotspot_x, hotspot_y; |
| 496 | int ret = 0; |
| 497 | |
| 498 | |
| 499 | hotspot_x = du->hotspot_x; |
| 500 | hotspot_y = du->hotspot_y; |
| 501 | du->cursor_surface = vps->surf; |
| 502 | du->cursor_dmabuf = vps->dmabuf; |
| 503 | |
| 504 | /* setup new image */ |
| 505 | if (vps->surf) { |
| 506 | du->cursor_age = du->cursor_surface->snooper.age; |
| 507 | |
| 508 | ret = vmw_cursor_update_image(dev_priv, |
| 509 | vps->surf->snooper.image, |
| 510 | 64, 64, hotspot_x, hotspot_y); |
| 511 | } else if (vps->dmabuf) { |
| 512 | ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf, |
| 513 | plane->state->crtc_w, |
| 514 | plane->state->crtc_h, |
| 515 | hotspot_x, hotspot_y); |
| 516 | } else { |
| 517 | vmw_cursor_update_position(dev_priv, false, 0, 0); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | if (!ret) { |
| 522 | du->cursor_x = plane->state->crtc_x + du->set_gui_x; |
| 523 | du->cursor_y = plane->state->crtc_y + du->set_gui_y; |
| 524 | |
| 525 | vmw_cursor_update_position(dev_priv, true, |
| 526 | du->cursor_x + hotspot_x, |
| 527 | du->cursor_y + hotspot_y); |
| 528 | } else { |
| 529 | DRM_ERROR("Failed to update cursor image\n"); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | |
| 534 | /** |
| 535 | * vmw_du_primary_plane_atomic_check - check if the new state is okay |
| 536 | * |
| 537 | * @plane: display plane |
| 538 | * @state: info on the new plane state, including the FB |
| 539 | * |
| 540 | * Check if the new state is settable given the current state. Other |
| 541 | * than what the atomic helper checks, we care about crtc fitting |
| 542 | * the FB and maintaining one active framebuffer. |
| 543 | * |
| 544 | * Returns 0 on success |
| 545 | */ |
| 546 | int vmw_du_primary_plane_atomic_check(struct drm_plane *plane, |
| 547 | struct drm_plane_state *state) |
| 548 | { |
| 549 | struct drm_framebuffer *new_fb = state->fb; |
| 550 | bool visible; |
| 551 | |
| 552 | struct drm_rect src = { |
| 553 | .x1 = state->src_x, |
| 554 | .y1 = state->src_y, |
| 555 | .x2 = state->src_x + state->src_w, |
| 556 | .y2 = state->src_y + state->src_h, |
| 557 | }; |
| 558 | struct drm_rect dest = { |
| 559 | .x1 = state->crtc_x, |
| 560 | .y1 = state->crtc_y, |
| 561 | .x2 = state->crtc_x + state->crtc_w, |
| 562 | .y2 = state->crtc_y + state->crtc_h, |
| 563 | }; |
| 564 | struct drm_rect clip = dest; |
| 565 | int ret; |
| 566 | |
| 567 | ret = drm_plane_helper_check_update(plane, state->crtc, new_fb, |
| 568 | &src, &dest, &clip, |
| 569 | DRM_ROTATE_0, |
| 570 | DRM_PLANE_HELPER_NO_SCALING, |
| 571 | DRM_PLANE_HELPER_NO_SCALING, |
| 572 | false, true, &visible); |
| 573 | |
| 574 | |
| 575 | if (!ret && new_fb) { |
| 576 | struct drm_crtc *crtc = state->crtc; |
| 577 | struct vmw_connector_state *vcs; |
| 578 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 579 | struct vmw_private *dev_priv = vmw_priv(crtc->dev); |
| 580 | struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb); |
| 581 | |
| 582 | vcs = vmw_connector_state_to_vcs(du->connector.state); |
| 583 | |
| 584 | if ((dest.x2 > new_fb->width || |
| 585 | dest.y2 > new_fb->height)) { |
| 586 | DRM_ERROR("CRTC area outside of framebuffer\n"); |
| 587 | return -EINVAL; |
| 588 | } |
| 589 | |
| 590 | /* Only one active implicit framebuffer at a time. */ |
| 591 | mutex_lock(&dev_priv->global_kms_state_mutex); |
| 592 | if (vcs->is_implicit && dev_priv->implicit_fb && |
| 593 | !(dev_priv->num_implicit == 1 && du->active_implicit) |
| 594 | && dev_priv->implicit_fb != vfb) { |
| 595 | DRM_ERROR("Multiple implicit framebuffers " |
| 596 | "not supported.\n"); |
| 597 | ret = -EINVAL; |
| 598 | } |
| 599 | mutex_unlock(&dev_priv->global_kms_state_mutex); |
| 600 | } |
| 601 | |
| 602 | |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /** |
| 608 | * vmw_du_cursor_plane_atomic_check - check if the new state is okay |
| 609 | * |
| 610 | * @plane: cursor plane |
| 611 | * @state: info on the new plane state |
| 612 | * |
| 613 | * This is a chance to fail if the new cursor state does not fit |
| 614 | * our requirements. |
| 615 | * |
| 616 | * Returns 0 on success |
| 617 | */ |
| 618 | int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane, |
| 619 | struct drm_plane_state *new_state) |
| 620 | { |
| 621 | int ret = 0; |
| 622 | struct vmw_surface *surface = NULL; |
| 623 | struct drm_framebuffer *fb = new_state->fb; |
| 624 | |
| 625 | |
| 626 | /* Turning off */ |
| 627 | if (!fb) |
| 628 | return ret; |
| 629 | |
| 630 | /* A lot of the code assumes this */ |
| 631 | if (new_state->crtc_w != 64 || new_state->crtc_h != 64) { |
| 632 | DRM_ERROR("Invalid cursor dimensions (%d, %d)\n", |
| 633 | new_state->crtc_w, new_state->crtc_h); |
| 634 | ret = -EINVAL; |
| 635 | } |
| 636 | |
| 637 | if (!vmw_framebuffer_to_vfb(fb)->dmabuf) |
| 638 | surface = vmw_framebuffer_to_vfbs(fb)->surface; |
| 639 | |
| 640 | if (surface && !surface->snooper.image) { |
| 641 | DRM_ERROR("surface not suitable for cursor\n"); |
| 642 | ret = -EINVAL; |
| 643 | } |
| 644 | |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | |
Sinclair Yeh | 06ec419 | 2017-03-23 13:14:54 -0700 | [diff] [blame] | 649 | int vmw_du_crtc_atomic_check(struct drm_crtc *crtc, |
| 650 | struct drm_crtc_state *new_state) |
| 651 | { |
| 652 | struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc); |
| 653 | int connector_mask = 1 << drm_connector_index(&du->connector); |
| 654 | bool has_primary = new_state->plane_mask & |
| 655 | BIT(drm_plane_index(crtc->primary)); |
| 656 | |
| 657 | /* We always want to have an active plane with an active CRTC */ |
| 658 | if (has_primary != new_state->enable) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | |
| 662 | if (new_state->connector_mask != connector_mask && |
| 663 | new_state->connector_mask != 0) { |
| 664 | DRM_ERROR("Invalid connectors configuration\n"); |
| 665 | return -EINVAL; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Our virtual device does not have a dot clock, so use the logical |
| 670 | * clock value as the dot clock. |
| 671 | */ |
| 672 | if (new_state->mode.crtc_clock == 0) |
| 673 | new_state->adjusted_mode.crtc_clock = new_state->mode.clock; |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc, |
| 680 | struct drm_crtc_state *old_crtc_state) |
| 681 | { |
| 682 | } |
| 683 | |
| 684 | |
| 685 | void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc, |
| 686 | struct drm_crtc_state *old_crtc_state) |
| 687 | { |
| 688 | struct drm_pending_vblank_event *event = crtc->state->event; |
| 689 | |
| 690 | if (event) { |
| 691 | crtc->state->event = NULL; |
| 692 | |
| 693 | spin_lock_irq(&crtc->dev->event_lock); |
| 694 | if (drm_crtc_vblank_get(crtc) == 0) |
| 695 | drm_crtc_arm_vblank_event(crtc, event); |
| 696 | else |
| 697 | drm_crtc_send_vblank_event(crtc, event); |
| 698 | spin_unlock_irq(&crtc->dev->event_lock); |
| 699 | } |
| 700 | |
| 701 | } |
| 702 | |
| 703 | |
Sinclair Yeh | 9c2542a | 2017-03-23 11:33:39 -0700 | [diff] [blame] | 704 | /** |
| 705 | * vmw_du_crtc_duplicate_state - duplicate crtc state |
| 706 | * @crtc: DRM crtc |
| 707 | * |
| 708 | * Allocates and returns a copy of the crtc state (both common and |
| 709 | * vmw-specific) for the specified crtc. |
| 710 | * |
| 711 | * Returns: The newly allocated crtc state, or NULL on failure. |
| 712 | */ |
| 713 | struct drm_crtc_state * |
| 714 | vmw_du_crtc_duplicate_state(struct drm_crtc *crtc) |
| 715 | { |
| 716 | struct drm_crtc_state *state; |
| 717 | struct vmw_crtc_state *vcs; |
| 718 | |
| 719 | if (WARN_ON(!crtc->state)) |
| 720 | return NULL; |
| 721 | |
| 722 | vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL); |
| 723 | |
| 724 | if (!vcs) |
| 725 | return NULL; |
| 726 | |
| 727 | state = &vcs->base; |
| 728 | |
| 729 | __drm_atomic_helper_crtc_duplicate_state(crtc, state); |
| 730 | |
| 731 | return state; |
| 732 | } |
| 733 | |
| 734 | |
| 735 | /** |
| 736 | * vmw_du_crtc_reset - creates a blank vmw crtc state |
| 737 | * @crtc: DRM crtc |
| 738 | * |
| 739 | * Resets the atomic state for @crtc by freeing the state pointer (which |
| 740 | * might be NULL, e.g. at driver load time) and allocating a new empty state |
| 741 | * object. |
| 742 | */ |
| 743 | void vmw_du_crtc_reset(struct drm_crtc *crtc) |
| 744 | { |
| 745 | struct vmw_crtc_state *vcs; |
| 746 | |
| 747 | |
| 748 | if (crtc->state) { |
| 749 | __drm_atomic_helper_crtc_destroy_state(crtc->state); |
| 750 | |
| 751 | kfree(vmw_crtc_state_to_vcs(crtc->state)); |
| 752 | } |
| 753 | |
| 754 | vcs = kzalloc(sizeof(*vcs), GFP_KERNEL); |
| 755 | |
| 756 | if (!vcs) { |
| 757 | DRM_ERROR("Cannot allocate vmw_crtc_state\n"); |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | crtc->state = &vcs->base; |
| 762 | crtc->state->crtc = crtc; |
| 763 | } |
| 764 | |
| 765 | |
| 766 | /** |
| 767 | * vmw_du_crtc_destroy_state - destroy crtc state |
| 768 | * @crtc: DRM crtc |
| 769 | * @state: state object to destroy |
| 770 | * |
| 771 | * Destroys the crtc state (both common and vmw-specific) for the |
| 772 | * specified plane. |
| 773 | */ |
| 774 | void |
| 775 | vmw_du_crtc_destroy_state(struct drm_crtc *crtc, |
| 776 | struct drm_crtc_state *state) |
| 777 | { |
| 778 | drm_atomic_helper_crtc_destroy_state(crtc, state); |
| 779 | } |
| 780 | |
| 781 | |
Sinclair Yeh | cc5ec45 | 2017-03-23 11:36:05 -0700 | [diff] [blame] | 782 | /** |
| 783 | * vmw_du_plane_duplicate_state - duplicate plane state |
| 784 | * @plane: drm plane |
| 785 | * |
| 786 | * Allocates and returns a copy of the plane state (both common and |
| 787 | * vmw-specific) for the specified plane. |
| 788 | * |
| 789 | * Returns: The newly allocated plane state, or NULL on failure. |
| 790 | */ |
| 791 | struct drm_plane_state * |
| 792 | vmw_du_plane_duplicate_state(struct drm_plane *plane) |
| 793 | { |
| 794 | struct drm_plane_state *state; |
| 795 | struct vmw_plane_state *vps; |
| 796 | |
| 797 | vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL); |
| 798 | |
| 799 | if (!vps) |
| 800 | return NULL; |
| 801 | |
| 802 | vps->pinned = 0; |
| 803 | |
| 804 | /* Each ref counted resource needs to be acquired again */ |
| 805 | if (vps->surf) |
| 806 | (void) vmw_surface_reference(vps->surf); |
| 807 | |
| 808 | if (vps->dmabuf) |
| 809 | (void) vmw_dmabuf_reference(vps->dmabuf); |
| 810 | |
| 811 | state = &vps->base; |
| 812 | |
| 813 | __drm_atomic_helper_plane_duplicate_state(plane, state); |
| 814 | |
| 815 | return state; |
| 816 | } |
| 817 | |
| 818 | |
| 819 | /** |
| 820 | * vmw_du_plane_reset - creates a blank vmw plane state |
| 821 | * @plane: drm plane |
| 822 | * |
| 823 | * Resets the atomic state for @plane by freeing the state pointer (which might |
| 824 | * be NULL, e.g. at driver load time) and allocating a new empty state object. |
| 825 | */ |
| 826 | void vmw_du_plane_reset(struct drm_plane *plane) |
| 827 | { |
| 828 | struct vmw_plane_state *vps; |
| 829 | |
| 830 | |
| 831 | if (plane->state) |
| 832 | vmw_du_plane_destroy_state(plane, plane->state); |
| 833 | |
| 834 | vps = kzalloc(sizeof(*vps), GFP_KERNEL); |
| 835 | |
| 836 | if (!vps) { |
| 837 | DRM_ERROR("Cannot allocate vmw_plane_state\n"); |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | plane->state = &vps->base; |
| 842 | plane->state->plane = plane; |
| 843 | plane->state->rotation = DRM_ROTATE_0; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | /** |
| 848 | * vmw_du_plane_destroy_state - destroy plane state |
| 849 | * @plane: DRM plane |
| 850 | * @state: state object to destroy |
| 851 | * |
| 852 | * Destroys the plane state (both common and vmw-specific) for the |
| 853 | * specified plane. |
| 854 | */ |
| 855 | void |
| 856 | vmw_du_plane_destroy_state(struct drm_plane *plane, |
| 857 | struct drm_plane_state *state) |
| 858 | { |
| 859 | struct vmw_plane_state *vps = vmw_plane_state_to_vps(state); |
| 860 | |
| 861 | |
| 862 | if (vps->surf) |
| 863 | vmw_surface_unreference(&vps->surf); |
| 864 | |
| 865 | if (vps->dmabuf) |
| 866 | vmw_dmabuf_unreference(&vps->dmabuf); |
| 867 | |
| 868 | drm_atomic_helper_plane_destroy_state(plane, state); |
| 869 | } |
| 870 | |
| 871 | |
Sinclair Yeh | d7721ca | 2017-03-23 11:48:44 -0700 | [diff] [blame] | 872 | /** |
| 873 | * vmw_du_connector_duplicate_state - duplicate connector state |
| 874 | * @connector: DRM connector |
| 875 | * |
| 876 | * Allocates and returns a copy of the connector state (both common and |
| 877 | * vmw-specific) for the specified connector. |
| 878 | * |
| 879 | * Returns: The newly allocated connector state, or NULL on failure. |
| 880 | */ |
| 881 | struct drm_connector_state * |
| 882 | vmw_du_connector_duplicate_state(struct drm_connector *connector) |
| 883 | { |
| 884 | struct drm_connector_state *state; |
| 885 | struct vmw_connector_state *vcs; |
| 886 | |
| 887 | if (WARN_ON(!connector->state)) |
| 888 | return NULL; |
| 889 | |
| 890 | vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL); |
| 891 | |
| 892 | if (!vcs) |
| 893 | return NULL; |
| 894 | |
| 895 | state = &vcs->base; |
| 896 | |
| 897 | __drm_atomic_helper_connector_duplicate_state(connector, state); |
| 898 | |
| 899 | return state; |
| 900 | } |
| 901 | |
| 902 | |
| 903 | /** |
| 904 | * vmw_du_connector_reset - creates a blank vmw connector state |
| 905 | * @connector: DRM connector |
| 906 | * |
| 907 | * Resets the atomic state for @connector by freeing the state pointer (which |
| 908 | * might be NULL, e.g. at driver load time) and allocating a new empty state |
| 909 | * object. |
| 910 | */ |
| 911 | void vmw_du_connector_reset(struct drm_connector *connector) |
| 912 | { |
| 913 | struct vmw_connector_state *vcs; |
| 914 | |
| 915 | |
| 916 | if (connector->state) { |
| 917 | __drm_atomic_helper_connector_destroy_state(connector->state); |
| 918 | |
| 919 | kfree(vmw_connector_state_to_vcs(connector->state)); |
| 920 | } |
| 921 | |
| 922 | vcs = kzalloc(sizeof(*vcs), GFP_KERNEL); |
| 923 | |
| 924 | if (!vcs) { |
| 925 | DRM_ERROR("Cannot allocate vmw_connector_state\n"); |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | __drm_atomic_helper_connector_reset(connector, &vcs->base); |
| 930 | } |
| 931 | |
| 932 | |
| 933 | /** |
| 934 | * vmw_du_connector_destroy_state - destroy connector state |
| 935 | * @connector: DRM connector |
| 936 | * @state: state object to destroy |
| 937 | * |
| 938 | * Destroys the connector state (both common and vmw-specific) for the |
| 939 | * specified plane. |
| 940 | */ |
| 941 | void |
| 942 | vmw_du_connector_destroy_state(struct drm_connector *connector, |
| 943 | struct drm_connector_state *state) |
| 944 | { |
| 945 | drm_atomic_helper_connector_destroy_state(connector, state); |
| 946 | } |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 947 | /* |
| 948 | * Generic framebuffer code |
| 949 | */ |
| 950 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 951 | /* |
| 952 | * Surface framebuffer code |
| 953 | */ |
| 954 | |
Rashika Kheria | 847c596 | 2014-01-06 22:18:10 +0530 | [diff] [blame] | 955 | static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 956 | { |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 957 | struct vmw_framebuffer_surface *vfbs = |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 958 | vmw_framebuffer_to_vfbs(framebuffer); |
| 959 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 960 | drm_framebuffer_cleanup(framebuffer); |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 961 | vmw_surface_unreference(&vfbs->surface); |
Thomas Hellstrom | a278724 | 2015-06-29 12:55:07 -0700 | [diff] [blame] | 962 | if (vfbs->base.user_obj) |
| 963 | ttm_base_object_unref(&vfbs->base.user_obj); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 964 | |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 965 | kfree(vfbs); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Rashika Kheria | 847c596 | 2014-01-06 22:18:10 +0530 | [diff] [blame] | 968 | static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer, |
Thomas Hellstrom | 02b0016 | 2010-10-05 12:43:02 +0200 | [diff] [blame] | 969 | struct drm_file *file_priv, |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 970 | unsigned flags, unsigned color, |
| 971 | struct drm_clip_rect *clips, |
| 972 | unsigned num_clips) |
| 973 | { |
| 974 | struct vmw_private *dev_priv = vmw_priv(framebuffer->dev); |
| 975 | struct vmw_framebuffer_surface *vfbs = |
| 976 | vmw_framebuffer_to_vfbs(framebuffer); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 977 | struct drm_clip_rect norect; |
Jakob Bornecrantz | 5deb65c | 2011-10-04 20:13:18 +0200 | [diff] [blame] | 978 | int ret, inc = 1; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 979 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 980 | /* Legacy Display Unit does not support 3D */ |
| 981 | if (dev_priv->active_display_unit == vmw_du_legacy) |
Jakob Bornecrantz | 01e8141 | 2011-10-04 20:13:24 +0200 | [diff] [blame] | 982 | return -EINVAL; |
| 983 | |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 984 | drm_modeset_lock_all(dev_priv->dev); |
| 985 | |
Thomas Hellstrom | 294adf7 | 2014-02-27 12:34:51 +0100 | [diff] [blame] | 986 | ret = ttm_read_lock(&dev_priv->reservation_sem, true); |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 987 | if (unlikely(ret != 0)) { |
| 988 | drm_modeset_unlock_all(dev_priv->dev); |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 989 | return ret; |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 990 | } |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 991 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 992 | if (!num_clips) { |
| 993 | num_clips = 1; |
| 994 | clips = &norect; |
| 995 | norect.x1 = norect.y1 = 0; |
| 996 | norect.x2 = framebuffer->width; |
| 997 | norect.y2 = framebuffer->height; |
| 998 | } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { |
| 999 | num_clips /= 2; |
| 1000 | inc = 2; /* skip source rects */ |
| 1001 | } |
| 1002 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1003 | if (dev_priv->active_display_unit == vmw_du_screen_object) |
Thomas Hellstrom | 10b1e0c | 2015-06-26 02:14:27 -0700 | [diff] [blame] | 1004 | ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base, |
| 1005 | clips, NULL, NULL, 0, 0, |
| 1006 | num_clips, inc, NULL); |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1007 | else |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1008 | ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base, |
| 1009 | clips, NULL, NULL, 0, 0, |
| 1010 | num_clips, inc, NULL); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1011 | |
Thomas Hellstrom | 3eab3d9 | 2015-06-25 11:57:56 -0700 | [diff] [blame] | 1012 | vmw_fifo_flush(dev_priv, false); |
Thomas Hellstrom | 294adf7 | 2014-02-27 12:34:51 +0100 | [diff] [blame] | 1013 | ttm_read_unlock(&dev_priv->reservation_sem); |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 1014 | |
| 1015 | drm_modeset_unlock_all(dev_priv->dev); |
| 1016 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1017 | return 0; |
| 1018 | } |
| 1019 | |
Thomas Hellstrom | 10b1e0c | 2015-06-26 02:14:27 -0700 | [diff] [blame] | 1020 | /** |
| 1021 | * vmw_kms_readback - Perform a readback from the screen system to |
| 1022 | * a dma-buffer backed framebuffer. |
| 1023 | * |
| 1024 | * @dev_priv: Pointer to the device private structure. |
| 1025 | * @file_priv: Pointer to a struct drm_file identifying the caller. |
| 1026 | * Must be set to NULL if @user_fence_rep is NULL. |
| 1027 | * @vfb: Pointer to the dma-buffer backed framebuffer. |
| 1028 | * @user_fence_rep: User-space provided structure for fence information. |
| 1029 | * Must be set to non-NULL if @file_priv is non-NULL. |
| 1030 | * @vclips: Array of clip rects. |
| 1031 | * @num_clips: Number of clip rects in @vclips. |
| 1032 | * |
| 1033 | * Returns 0 on success, negative error code on failure. -ERESTARTSYS if |
| 1034 | * interrupted. |
| 1035 | */ |
| 1036 | int vmw_kms_readback(struct vmw_private *dev_priv, |
| 1037 | struct drm_file *file_priv, |
| 1038 | struct vmw_framebuffer *vfb, |
| 1039 | struct drm_vmw_fence_rep __user *user_fence_rep, |
| 1040 | struct drm_vmw_rect *vclips, |
| 1041 | uint32_t num_clips) |
| 1042 | { |
| 1043 | switch (dev_priv->active_display_unit) { |
| 1044 | case vmw_du_screen_object: |
| 1045 | return vmw_kms_sou_readback(dev_priv, file_priv, vfb, |
| 1046 | user_fence_rep, vclips, num_clips); |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1047 | case vmw_du_screen_target: |
| 1048 | return vmw_kms_stdu_dma(dev_priv, file_priv, vfb, |
| 1049 | user_fence_rep, NULL, vclips, num_clips, |
| 1050 | 1, false, true); |
Thomas Hellstrom | 10b1e0c | 2015-06-26 02:14:27 -0700 | [diff] [blame] | 1051 | default: |
| 1052 | WARN_ONCE(true, |
| 1053 | "Readback called with invalid display system.\n"); |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1054 | } |
Thomas Hellstrom | 10b1e0c | 2015-06-26 02:14:27 -0700 | [diff] [blame] | 1055 | |
| 1056 | return -ENOSYS; |
| 1057 | } |
| 1058 | |
| 1059 | |
Ville Syrjälä | d7955fc | 2015-12-15 12:21:15 +0100 | [diff] [blame] | 1060 | static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1061 | .destroy = vmw_framebuffer_surface_destroy, |
| 1062 | .dirty = vmw_framebuffer_surface_dirty, |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1063 | }; |
| 1064 | |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1065 | static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv, |
| 1066 | struct vmw_surface *surface, |
| 1067 | struct vmw_framebuffer **out, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1068 | const struct drm_mode_fb_cmd2 |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1069 | *mode_cmd, |
| 1070 | bool is_dmabuf_proxy) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1071 | |
| 1072 | { |
| 1073 | struct drm_device *dev = dev_priv->dev; |
| 1074 | struct vmw_framebuffer_surface *vfbs; |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1075 | enum SVGA3dSurfaceFormat format; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1076 | int ret; |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1077 | struct drm_format_name_buf format_name; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1078 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1079 | /* 3D is only supported on HWv8 and newer hosts */ |
| 1080 | if (dev_priv->active_display_unit == vmw_du_legacy) |
Jakob Bornecrantz | 01e8141 | 2011-10-04 20:13:24 +0200 | [diff] [blame] | 1081 | return -ENOSYS; |
| 1082 | |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1083 | /* |
| 1084 | * Sanity checks. |
| 1085 | */ |
| 1086 | |
Jakob Bornecrantz | e7ac921 | 2011-11-28 13:19:12 +0100 | [diff] [blame] | 1087 | /* Surface must be marked as a scanout. */ |
| 1088 | if (unlikely(!surface->scanout)) |
| 1089 | return -EINVAL; |
| 1090 | |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1091 | if (unlikely(surface->mip_levels[0] != 1 || |
| 1092 | surface->num_sizes != 1 || |
Thomas Hellstrom | b360a3c | 2014-01-15 08:51:36 +0100 | [diff] [blame] | 1093 | surface->base_size.width < mode_cmd->width || |
| 1094 | surface->base_size.height < mode_cmd->height || |
| 1095 | surface->base_size.depth != 1)) { |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1096 | DRM_ERROR("Incompatible surface dimensions " |
| 1097 | "for requested mode.\n"); |
| 1098 | return -EINVAL; |
| 1099 | } |
| 1100 | |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1101 | switch (mode_cmd->pixel_format) { |
| 1102 | case DRM_FORMAT_ARGB8888: |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1103 | format = SVGA3D_A8R8G8B8; |
| 1104 | break; |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1105 | case DRM_FORMAT_XRGB8888: |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1106 | format = SVGA3D_X8R8G8B8; |
| 1107 | break; |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1108 | case DRM_FORMAT_RGB565: |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1109 | format = SVGA3D_R5G6B5; |
| 1110 | break; |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1111 | case DRM_FORMAT_XRGB1555: |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1112 | format = SVGA3D_A1R5G5B5; |
| 1113 | break; |
| 1114 | default: |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1115 | DRM_ERROR("Invalid pixel format: %s\n", |
| 1116 | drm_get_format_name(mode_cmd->pixel_format, &format_name)); |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1117 | return -EINVAL; |
| 1118 | } |
| 1119 | |
Thomas Hellstrom | d80efd5 | 2015-08-10 10:39:35 -0700 | [diff] [blame] | 1120 | /* |
| 1121 | * For DX, surface format validation is done when surface->scanout |
| 1122 | * is set. |
| 1123 | */ |
| 1124 | if (!dev_priv->has_dx && format != surface->format) { |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1125 | DRM_ERROR("Invalid surface format for requested mode.\n"); |
| 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1129 | vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL); |
| 1130 | if (!vfbs) { |
| 1131 | ret = -ENOMEM; |
| 1132 | goto out_err1; |
| 1133 | } |
| 1134 | |
Ville Syrjälä | a3f913c | 2016-12-14 22:48:59 +0200 | [diff] [blame] | 1135 | drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd); |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1136 | vfbs->surface = vmw_surface_reference(surface); |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1137 | vfbs->base.user_handle = mode_cmd->handles[0]; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1138 | vfbs->is_dmabuf_proxy = is_dmabuf_proxy; |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 1139 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1140 | *out = &vfbs->base; |
| 1141 | |
Daniel Vetter | 80f0b5a | 2012-12-13 23:39:01 +0100 | [diff] [blame] | 1142 | ret = drm_framebuffer_init(dev, &vfbs->base.base, |
| 1143 | &vmw_framebuffer_surface_funcs); |
| 1144 | if (ret) |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1145 | goto out_err2; |
Daniel Vetter | 80f0b5a | 2012-12-13 23:39:01 +0100 | [diff] [blame] | 1146 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1147 | return 0; |
| 1148 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1149 | out_err2: |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1150 | vmw_surface_unreference(&surface); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1151 | kfree(vfbs); |
| 1152 | out_err1: |
| 1153 | return ret; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Dmabuf framebuffer code |
| 1158 | */ |
| 1159 | |
Rashika Kheria | 847c596 | 2014-01-06 22:18:10 +0530 | [diff] [blame] | 1160 | static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1161 | { |
| 1162 | struct vmw_framebuffer_dmabuf *vfbd = |
| 1163 | vmw_framebuffer_to_vfbd(framebuffer); |
| 1164 | |
| 1165 | drm_framebuffer_cleanup(framebuffer); |
| 1166 | vmw_dmabuf_unreference(&vfbd->buffer); |
Thomas Hellstrom | a278724 | 2015-06-29 12:55:07 -0700 | [diff] [blame] | 1167 | if (vfbd->base.user_obj) |
| 1168 | ttm_base_object_unref(&vfbd->base.user_obj); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1169 | |
| 1170 | kfree(vfbd); |
| 1171 | } |
| 1172 | |
Rashika Kheria | 847c596 | 2014-01-06 22:18:10 +0530 | [diff] [blame] | 1173 | static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer, |
Thomas Hellstrom | 02b0016 | 2010-10-05 12:43:02 +0200 | [diff] [blame] | 1174 | struct drm_file *file_priv, |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1175 | unsigned flags, unsigned color, |
| 1176 | struct drm_clip_rect *clips, |
| 1177 | unsigned num_clips) |
| 1178 | { |
| 1179 | struct vmw_private *dev_priv = vmw_priv(framebuffer->dev); |
Jakob Bornecrantz | 5deb65c | 2011-10-04 20:13:18 +0200 | [diff] [blame] | 1180 | struct vmw_framebuffer_dmabuf *vfbd = |
| 1181 | vmw_framebuffer_to_vfbd(framebuffer); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1182 | struct drm_clip_rect norect; |
Jakob Bornecrantz | 5deb65c | 2011-10-04 20:13:18 +0200 | [diff] [blame] | 1183 | int ret, increment = 1; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1184 | |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 1185 | drm_modeset_lock_all(dev_priv->dev); |
| 1186 | |
Thomas Hellstrom | 294adf7 | 2014-02-27 12:34:51 +0100 | [diff] [blame] | 1187 | ret = ttm_read_lock(&dev_priv->reservation_sem, true); |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 1188 | if (unlikely(ret != 0)) { |
| 1189 | drm_modeset_unlock_all(dev_priv->dev); |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 1190 | return ret; |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 1191 | } |
Thomas Hellstrom | 3a939a5 | 2010-10-05 12:43:03 +0200 | [diff] [blame] | 1192 | |
Thomas Hellstrom | df1c93b | 2010-01-13 22:28:36 +0100 | [diff] [blame] | 1193 | if (!num_clips) { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1194 | num_clips = 1; |
| 1195 | clips = &norect; |
| 1196 | norect.x1 = norect.y1 = 0; |
| 1197 | norect.x2 = framebuffer->width; |
| 1198 | norect.y2 = framebuffer->height; |
| 1199 | } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { |
| 1200 | num_clips /= 2; |
| 1201 | increment = 2; |
| 1202 | } |
| 1203 | |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1204 | switch (dev_priv->active_display_unit) { |
| 1205 | case vmw_du_screen_target: |
| 1206 | ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL, |
| 1207 | clips, NULL, num_clips, increment, |
| 1208 | true, true); |
| 1209 | break; |
| 1210 | case vmw_du_screen_object: |
Thomas Hellstrom | 10b1e0c | 2015-06-26 02:14:27 -0700 | [diff] [blame] | 1211 | ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base, |
Thomas Hellstrom | 897b818 | 2016-02-12 08:32:08 +0100 | [diff] [blame] | 1212 | clips, NULL, num_clips, |
| 1213 | increment, true, NULL); |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1214 | break; |
Thomas Hellstrom | 352b20d | 2015-06-29 12:57:37 -0700 | [diff] [blame] | 1215 | case vmw_du_legacy: |
| 1216 | ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0, |
| 1217 | clips, num_clips, increment); |
| 1218 | break; |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1219 | default: |
Thomas Hellstrom | 352b20d | 2015-06-29 12:57:37 -0700 | [diff] [blame] | 1220 | ret = -EINVAL; |
| 1221 | WARN_ONCE(true, "Dirty called with invalid display system.\n"); |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1222 | break; |
Jakob Bornecrantz | 56d1c78 | 2011-10-04 20:13:22 +0200 | [diff] [blame] | 1223 | } |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1224 | |
Thomas Hellstrom | 3eab3d9 | 2015-06-25 11:57:56 -0700 | [diff] [blame] | 1225 | vmw_fifo_flush(dev_priv, false); |
Thomas Hellstrom | 294adf7 | 2014-02-27 12:34:51 +0100 | [diff] [blame] | 1226 | ttm_read_unlock(&dev_priv->reservation_sem); |
Ville Syrjälä | 73e9efd | 2013-12-04 14:13:58 +0200 | [diff] [blame] | 1227 | |
| 1228 | drm_modeset_unlock_all(dev_priv->dev); |
| 1229 | |
Jakob Bornecrantz | 5deb65c | 2011-10-04 20:13:18 +0200 | [diff] [blame] | 1230 | return ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Ville Syrjälä | d7955fc | 2015-12-15 12:21:15 +0100 | [diff] [blame] | 1233 | static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1234 | .destroy = vmw_framebuffer_dmabuf_destroy, |
| 1235 | .dirty = vmw_framebuffer_dmabuf_dirty, |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1236 | }; |
| 1237 | |
Jakob Bornecrantz | 497a3ff | 2011-10-04 20:13:14 +0200 | [diff] [blame] | 1238 | /** |
Jakob Bornecrantz | 497a3ff | 2011-10-04 20:13:14 +0200 | [diff] [blame] | 1239 | * Pin the dmabuffer to the start of vram. |
| 1240 | */ |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1241 | static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1242 | { |
| 1243 | struct vmw_private *dev_priv = vmw_priv(vfb->base.dev); |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1244 | struct vmw_dma_buffer *buf; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1245 | int ret; |
| 1246 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1247 | buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer : |
| 1248 | vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup; |
Jakob Bornecrantz | d7e1958 | 2010-05-28 11:21:59 +0200 | [diff] [blame] | 1249 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1250 | if (!buf) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1251 | return 0; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1252 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1253 | switch (dev_priv->active_display_unit) { |
| 1254 | case vmw_du_legacy: |
| 1255 | vmw_overlay_pause_all(dev_priv); |
| 1256 | ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false); |
| 1257 | vmw_overlay_resume_all(dev_priv); |
| 1258 | break; |
| 1259 | case vmw_du_screen_object: |
| 1260 | case vmw_du_screen_target: |
| 1261 | if (vfb->dmabuf) |
| 1262 | return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf, |
| 1263 | false); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1264 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1265 | return vmw_dmabuf_pin_in_placement(dev_priv, buf, |
| 1266 | &vmw_mob_placement, false); |
| 1267 | default: |
| 1268 | return -EINVAL; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1269 | } |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1270 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb) |
| 1275 | { |
| 1276 | struct vmw_private *dev_priv = vmw_priv(vfb->base.dev); |
| 1277 | struct vmw_dma_buffer *buf; |
| 1278 | |
| 1279 | buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer : |
| 1280 | vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup; |
| 1281 | |
| 1282 | if (WARN_ON(!buf)) |
| 1283 | return 0; |
| 1284 | |
| 1285 | return vmw_dmabuf_unpin(dev_priv, buf, false); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1288 | /** |
| 1289 | * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf |
| 1290 | * |
| 1291 | * @dev: DRM device |
| 1292 | * @mode_cmd: parameters for the new surface |
| 1293 | * @dmabuf_mob: MOB backing the DMA buf |
| 1294 | * @srf_out: newly created surface |
| 1295 | * |
| 1296 | * When the content FB is a DMA buf, we create a surface as a proxy to the |
| 1297 | * same buffer. This way we can do a surface copy rather than a surface DMA. |
| 1298 | * This is a more efficient approach |
| 1299 | * |
| 1300 | * RETURNS: |
| 1301 | * 0 on success, error code otherwise |
| 1302 | */ |
| 1303 | static int vmw_create_dmabuf_proxy(struct drm_device *dev, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1304 | const struct drm_mode_fb_cmd2 *mode_cmd, |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1305 | struct vmw_dma_buffer *dmabuf_mob, |
| 1306 | struct vmw_surface **srf_out) |
| 1307 | { |
| 1308 | uint32_t format; |
Thomas Hellstrom | 8cd9f25 | 2017-01-19 10:53:02 -0800 | [diff] [blame] | 1309 | struct drm_vmw_size content_base_size = {0}; |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1310 | struct vmw_resource *res; |
Thomas Hellstrom | a50e2bf | 2016-01-08 20:29:40 +0100 | [diff] [blame] | 1311 | unsigned int bytes_pp; |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1312 | struct drm_format_name_buf format_name; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1313 | int ret; |
| 1314 | |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1315 | switch (mode_cmd->pixel_format) { |
| 1316 | case DRM_FORMAT_ARGB8888: |
| 1317 | case DRM_FORMAT_XRGB8888: |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1318 | format = SVGA3D_X8R8G8B8; |
Thomas Hellstrom | a50e2bf | 2016-01-08 20:29:40 +0100 | [diff] [blame] | 1319 | bytes_pp = 4; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1320 | break; |
| 1321 | |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1322 | case DRM_FORMAT_RGB565: |
| 1323 | case DRM_FORMAT_XRGB1555: |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1324 | format = SVGA3D_R5G6B5; |
Thomas Hellstrom | a50e2bf | 2016-01-08 20:29:40 +0100 | [diff] [blame] | 1325 | bytes_pp = 2; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1326 | break; |
| 1327 | |
| 1328 | case 8: |
| 1329 | format = SVGA3D_P8; |
Thomas Hellstrom | a50e2bf | 2016-01-08 20:29:40 +0100 | [diff] [blame] | 1330 | bytes_pp = 1; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1331 | break; |
| 1332 | |
| 1333 | default: |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1334 | DRM_ERROR("Invalid framebuffer format %s\n", |
| 1335 | drm_get_format_name(mode_cmd->pixel_format, &format_name)); |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1336 | return -EINVAL; |
| 1337 | } |
| 1338 | |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1339 | content_base_size.width = mode_cmd->pitches[0] / bytes_pp; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1340 | content_base_size.height = mode_cmd->height; |
| 1341 | content_base_size.depth = 1; |
| 1342 | |
| 1343 | ret = vmw_surface_gb_priv_define(dev, |
| 1344 | 0, /* kernel visible only */ |
| 1345 | 0, /* flags */ |
| 1346 | format, |
| 1347 | true, /* can be a scanout buffer */ |
| 1348 | 1, /* num of mip levels */ |
| 1349 | 0, |
Thomas Hellstrom | d80efd5 | 2015-08-10 10:39:35 -0700 | [diff] [blame] | 1350 | 0, |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1351 | content_base_size, |
| 1352 | srf_out); |
| 1353 | if (ret) { |
| 1354 | DRM_ERROR("Failed to allocate proxy content buffer\n"); |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1358 | res = &(*srf_out)->res; |
Sinclair Yeh | f89c6c3 | 2015-06-26 01:54:28 -0700 | [diff] [blame] | 1359 | |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1360 | /* Reserve and switch the backing mob. */ |
| 1361 | mutex_lock(&res->dev_priv->cmdbuf_mutex); |
| 1362 | (void) vmw_resource_reserve(res, false, true); |
| 1363 | vmw_dmabuf_unreference(&res->backup); |
| 1364 | res->backup = vmw_dmabuf_reference(dmabuf_mob); |
| 1365 | res->backup_offset = 0; |
Thomas Hellstrom | d80efd5 | 2015-08-10 10:39:35 -0700 | [diff] [blame] | 1366 | vmw_resource_unreserve(res, false, NULL, 0); |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1367 | mutex_unlock(&res->dev_priv->cmdbuf_mutex); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1368 | |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1372 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1373 | |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1374 | static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv, |
| 1375 | struct vmw_dma_buffer *dmabuf, |
| 1376 | struct vmw_framebuffer **out, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1377 | const struct drm_mode_fb_cmd2 |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1378 | *mode_cmd) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1379 | |
| 1380 | { |
| 1381 | struct drm_device *dev = dev_priv->dev; |
| 1382 | struct vmw_framebuffer_dmabuf *vfbd; |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1383 | unsigned int requested_size; |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1384 | struct drm_format_name_buf format_name; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1385 | int ret; |
| 1386 | |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1387 | requested_size = mode_cmd->height * mode_cmd->pitches[0]; |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1388 | if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) { |
| 1389 | DRM_ERROR("Screen buffer object size is too small " |
| 1390 | "for requested mode.\n"); |
| 1391 | return -EINVAL; |
| 1392 | } |
| 1393 | |
Jakob Bornecrantz | c337ada | 2011-10-04 20:13:34 +0200 | [diff] [blame] | 1394 | /* Limited framebuffer color depth support for screen objects */ |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1395 | if (dev_priv->active_display_unit == vmw_du_screen_object) { |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1396 | switch (mode_cmd->pixel_format) { |
| 1397 | case DRM_FORMAT_XRGB8888: |
| 1398 | case DRM_FORMAT_ARGB8888: |
| 1399 | break; |
| 1400 | case DRM_FORMAT_XRGB1555: |
| 1401 | case DRM_FORMAT_RGB565: |
| 1402 | break; |
Jakob Bornecrantz | c337ada | 2011-10-04 20:13:34 +0200 | [diff] [blame] | 1403 | default: |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1404 | DRM_ERROR("Invalid pixel format: %s\n", |
| 1405 | drm_get_format_name(mode_cmd->pixel_format, &format_name)); |
Jakob Bornecrantz | c337ada | 2011-10-04 20:13:34 +0200 | [diff] [blame] | 1406 | return -EINVAL; |
| 1407 | } |
| 1408 | } |
| 1409 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1410 | vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL); |
| 1411 | if (!vfbd) { |
| 1412 | ret = -ENOMEM; |
| 1413 | goto out_err1; |
| 1414 | } |
| 1415 | |
Ville Syrjälä | a3f913c | 2016-12-14 22:48:59 +0200 | [diff] [blame] | 1416 | drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd); |
Jakob Bornecrantz | 2fcd5a7 | 2011-10-04 20:13:26 +0200 | [diff] [blame] | 1417 | vfbd->base.dmabuf = true; |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1418 | vfbd->buffer = vmw_dmabuf_reference(dmabuf); |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1419 | vfbd->base.user_handle = mode_cmd->handles[0]; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1420 | *out = &vfbd->base; |
| 1421 | |
Daniel Vetter | 80f0b5a | 2012-12-13 23:39:01 +0100 | [diff] [blame] | 1422 | ret = drm_framebuffer_init(dev, &vfbd->base.base, |
| 1423 | &vmw_framebuffer_dmabuf_funcs); |
| 1424 | if (ret) |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1425 | goto out_err2; |
Daniel Vetter | 80f0b5a | 2012-12-13 23:39:01 +0100 | [diff] [blame] | 1426 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1427 | return 0; |
| 1428 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1429 | out_err2: |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1430 | vmw_dmabuf_unreference(&dmabuf); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1431 | kfree(vfbd); |
| 1432 | out_err1: |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1436 | /** |
| 1437 | * vmw_kms_new_framebuffer - Create a new framebuffer. |
| 1438 | * |
| 1439 | * @dev_priv: Pointer to device private struct. |
| 1440 | * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around. |
| 1441 | * Either @dmabuf or @surface must be NULL. |
| 1442 | * @surface: Pointer to a surface to wrap the kms framebuffer around. |
| 1443 | * Either @dmabuf or @surface must be NULL. |
| 1444 | * @only_2d: No presents will occur to this dma buffer based framebuffer. This |
| 1445 | * Helps the code to do some important optimizations. |
| 1446 | * @mode_cmd: Frame-buffer metadata. |
| 1447 | */ |
| 1448 | struct vmw_framebuffer * |
| 1449 | vmw_kms_new_framebuffer(struct vmw_private *dev_priv, |
| 1450 | struct vmw_dma_buffer *dmabuf, |
| 1451 | struct vmw_surface *surface, |
| 1452 | bool only_2d, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1453 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1454 | { |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1455 | struct vmw_framebuffer *vfb = NULL; |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1456 | bool is_dmabuf_proxy = false; |
| 1457 | int ret; |
| 1458 | |
| 1459 | /* |
| 1460 | * We cannot use the SurfaceDMA command in an non-accelerated VM, |
| 1461 | * therefore, wrap the DMA buf in a surface so we can use the |
| 1462 | * SurfaceCopy command. |
| 1463 | */ |
| 1464 | if (dmabuf && only_2d && |
| 1465 | dev_priv->active_display_unit == vmw_du_screen_target) { |
| 1466 | ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd, |
| 1467 | dmabuf, &surface); |
| 1468 | if (ret) |
| 1469 | return ERR_PTR(ret); |
| 1470 | |
| 1471 | is_dmabuf_proxy = true; |
| 1472 | } |
| 1473 | |
| 1474 | /* Create the new framebuffer depending one what we have */ |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1475 | if (surface) { |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1476 | ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb, |
| 1477 | mode_cmd, |
| 1478 | is_dmabuf_proxy); |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1479 | |
| 1480 | /* |
| 1481 | * vmw_create_dmabuf_proxy() adds a reference that is no longer |
| 1482 | * needed |
| 1483 | */ |
| 1484 | if (is_dmabuf_proxy) |
| 1485 | vmw_surface_unreference(&surface); |
| 1486 | } else if (dmabuf) { |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1487 | ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb, |
| 1488 | mode_cmd); |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1489 | } else { |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1490 | BUG(); |
Sinclair Yeh | 05c9501 | 2015-08-11 22:53:39 -0700 | [diff] [blame] | 1491 | } |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1492 | |
| 1493 | if (ret) |
| 1494 | return ERR_PTR(ret); |
| 1495 | |
| 1496 | vfb->pin = vmw_framebuffer_pin; |
| 1497 | vfb->unpin = vmw_framebuffer_unpin; |
| 1498 | |
| 1499 | return vfb; |
| 1500 | } |
| 1501 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1502 | /* |
| 1503 | * Generic Kernel modesetting functions |
| 1504 | */ |
| 1505 | |
| 1506 | static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev, |
| 1507 | struct drm_file *file_priv, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1508 | const struct drm_mode_fb_cmd2 *mode_cmd) |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1509 | { |
| 1510 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1511 | struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile; |
| 1512 | struct vmw_framebuffer *vfb = NULL; |
| 1513 | struct vmw_surface *surface = NULL; |
| 1514 | struct vmw_dma_buffer *bo = NULL; |
Thomas Hellstrom | 90ff18b | 2011-10-04 20:13:32 +0200 | [diff] [blame] | 1515 | struct ttm_base_object *user_obj; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1516 | int ret; |
| 1517 | |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1518 | /** |
| 1519 | * This code should be conditioned on Screen Objects not being used. |
| 1520 | * If screen objects are used, we can allocate a GMR to hold the |
| 1521 | * requested framebuffer. |
| 1522 | */ |
| 1523 | |
Xi Wang | 8a78389 | 2011-12-21 05:18:33 -0500 | [diff] [blame] | 1524 | if (!vmw_kms_validate_mode_vram(dev_priv, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1525 | mode_cmd->pitches[0], |
| 1526 | mode_cmd->height)) { |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1527 | DRM_ERROR("Requested mode exceed bounding box limit.\n"); |
Jakob Bornecrantz | d982640 | 2011-11-03 21:03:04 +0100 | [diff] [blame] | 1528 | return ERR_PTR(-ENOMEM); |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Thomas Hellstrom | 90ff18b | 2011-10-04 20:13:32 +0200 | [diff] [blame] | 1531 | /* |
| 1532 | * Take a reference on the user object of the resource |
| 1533 | * backing the kms fb. This ensures that user-space handle |
| 1534 | * lookups on that resource will always work as long as |
| 1535 | * it's registered with a kms framebuffer. This is important, |
| 1536 | * since vmw_execbuf_process identifies resources in the |
| 1537 | * command stream using user-space handles. |
| 1538 | */ |
| 1539 | |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1540 | user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]); |
Thomas Hellstrom | 90ff18b | 2011-10-04 20:13:32 +0200 | [diff] [blame] | 1541 | if (unlikely(user_obj == NULL)) { |
| 1542 | DRM_ERROR("Could not locate requested kms frame buffer.\n"); |
| 1543 | return ERR_PTR(-ENOENT); |
| 1544 | } |
| 1545 | |
Thomas Hellstrom | d3216a0 | 2010-10-05 12:42:59 +0200 | [diff] [blame] | 1546 | /** |
| 1547 | * End conditioned code. |
| 1548 | */ |
| 1549 | |
Jakob Bornecrantz | e7ac921 | 2011-11-28 13:19:12 +0100 | [diff] [blame] | 1550 | /* returns either a dmabuf or surface */ |
| 1551 | ret = vmw_user_lookup_handle(dev_priv, tfile, |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1552 | mode_cmd->handles[0], |
Jakob Bornecrantz | e7ac921 | 2011-11-28 13:19:12 +0100 | [diff] [blame] | 1553 | &surface, &bo); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1554 | if (ret) |
Jakob Bornecrantz | e7ac921 | 2011-11-28 13:19:12 +0100 | [diff] [blame] | 1555 | goto err_out; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1556 | |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1557 | vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface, |
| 1558 | !(dev_priv->capabilities & SVGA_CAP_3D), |
Daniel Vetter | dabdcdc | 2016-12-02 08:07:40 +0100 | [diff] [blame] | 1559 | mode_cmd); |
Thomas Hellstrom | fd006a4 | 2015-06-28 02:50:56 -0700 | [diff] [blame] | 1560 | if (IS_ERR(vfb)) { |
| 1561 | ret = PTR_ERR(vfb); |
| 1562 | goto err_out; |
| 1563 | } |
Jakob Bornecrantz | 5ffdb65 | 2010-01-30 03:38:08 +0000 | [diff] [blame] | 1564 | |
Jakob Bornecrantz | e7ac921 | 2011-11-28 13:19:12 +0100 | [diff] [blame] | 1565 | err_out: |
| 1566 | /* vmw_user_lookup_handle takes one ref so does new_fb */ |
| 1567 | if (bo) |
| 1568 | vmw_dmabuf_unreference(&bo); |
| 1569 | if (surface) |
| 1570 | vmw_surface_unreference(&surface); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1571 | |
| 1572 | if (ret) { |
| 1573 | DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret); |
Thomas Hellstrom | 90ff18b | 2011-10-04 20:13:32 +0200 | [diff] [blame] | 1574 | ttm_base_object_unref(&user_obj); |
Chris Wilson | cce13ff | 2010-08-08 13:36:38 +0100 | [diff] [blame] | 1575 | return ERR_PTR(ret); |
Thomas Hellstrom | 90ff18b | 2011-10-04 20:13:32 +0200 | [diff] [blame] | 1576 | } else |
| 1577 | vfb->user_obj = user_obj; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1578 | |
| 1579 | return &vfb->base; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Laurent Pinchart | e6ecefa | 2012-05-17 13:27:23 +0200 | [diff] [blame] | 1582 | static const struct drm_mode_config_funcs vmw_kms_funcs = { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1583 | .fb_create = vmw_kms_fb_create, |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1584 | }; |
| 1585 | |
Thomas Hellstrom | b9eb1a6 | 2015-04-02 02:39:45 -0700 | [diff] [blame] | 1586 | static int vmw_kms_generic_present(struct vmw_private *dev_priv, |
| 1587 | struct drm_file *file_priv, |
| 1588 | struct vmw_framebuffer *vfb, |
| 1589 | struct vmw_surface *surface, |
| 1590 | uint32_t sid, |
| 1591 | int32_t destX, int32_t destY, |
| 1592 | struct drm_vmw_rect *clips, |
| 1593 | uint32_t num_clips) |
Jakob Bornecrantz | 2fcd5a7 | 2011-10-04 20:13:26 +0200 | [diff] [blame] | 1594 | { |
Thomas Hellstrom | 10b1e0c | 2015-06-26 02:14:27 -0700 | [diff] [blame] | 1595 | return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips, |
| 1596 | &surface->res, destX, destY, |
| 1597 | num_clips, 1, NULL); |
Jakob Bornecrantz | 2fcd5a7 | 2011-10-04 20:13:26 +0200 | [diff] [blame] | 1598 | } |
| 1599 | |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1600 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1601 | int vmw_kms_present(struct vmw_private *dev_priv, |
| 1602 | struct drm_file *file_priv, |
| 1603 | struct vmw_framebuffer *vfb, |
| 1604 | struct vmw_surface *surface, |
| 1605 | uint32_t sid, |
| 1606 | int32_t destX, int32_t destY, |
| 1607 | struct drm_vmw_rect *clips, |
| 1608 | uint32_t num_clips) |
| 1609 | { |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1610 | int ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1611 | |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 1612 | switch (dev_priv->active_display_unit) { |
| 1613 | case vmw_du_screen_target: |
| 1614 | ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips, |
| 1615 | &surface->res, destX, destY, |
| 1616 | num_clips, 1, NULL); |
| 1617 | break; |
| 1618 | case vmw_du_screen_object: |
| 1619 | ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface, |
| 1620 | sid, destX, destY, clips, |
| 1621 | num_clips); |
| 1622 | break; |
| 1623 | default: |
| 1624 | WARN_ONCE(true, |
| 1625 | "Present called with invalid display system.\n"); |
| 1626 | ret = -ENOSYS; |
| 1627 | break; |
Jakob Bornecrantz | d7e1958 | 2010-05-28 11:21:59 +0200 | [diff] [blame] | 1628 | } |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1629 | if (ret) |
| 1630 | return ret; |
Jakob Bornecrantz | d7e1958 | 2010-05-28 11:21:59 +0200 | [diff] [blame] | 1631 | |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1632 | vmw_fifo_flush(dev_priv, false); |
Michel Dänzer | 0bef23f | 2011-08-31 07:42:50 +0000 | [diff] [blame] | 1633 | |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1634 | return 0; |
Thomas Hellstrom | 7c4f778 | 2010-06-01 11:38:17 +0200 | [diff] [blame] | 1635 | } |
| 1636 | |
Thomas Hellstrom | 578e609 | 2016-02-12 09:45:42 +0100 | [diff] [blame] | 1637 | static void |
| 1638 | vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv) |
| 1639 | { |
| 1640 | if (dev_priv->hotplug_mode_update_property) |
| 1641 | return; |
| 1642 | |
| 1643 | dev_priv->hotplug_mode_update_property = |
| 1644 | drm_property_create_range(dev_priv->dev, |
| 1645 | DRM_MODE_PROP_IMMUTABLE, |
| 1646 | "hotplug_mode_update", 0, 1); |
| 1647 | |
| 1648 | if (!dev_priv->hotplug_mode_update_property) |
| 1649 | return; |
| 1650 | |
| 1651 | } |
| 1652 | |
Jakob Bornecrantz | d7e1958 | 2010-05-28 11:21:59 +0200 | [diff] [blame] | 1653 | int vmw_kms_init(struct vmw_private *dev_priv) |
| 1654 | { |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1655 | struct drm_device *dev = dev_priv->dev; |
| 1656 | int ret; |
| 1657 | |
| 1658 | drm_mode_config_init(dev); |
| 1659 | dev->mode_config.funcs = &vmw_kms_funcs; |
| 1660 | dev->mode_config.min_width = 1; |
| 1661 | dev->mode_config.min_height = 1; |
Sinclair Yeh | 65ade7d | 2015-07-16 10:49:13 -0700 | [diff] [blame] | 1662 | dev->mode_config.max_width = dev_priv->texture_max_width; |
| 1663 | dev->mode_config.max_height = dev_priv->texture_max_height; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1664 | |
Thomas Hellstrom | 578e609 | 2016-02-12 09:45:42 +0100 | [diff] [blame] | 1665 | drm_mode_create_suggested_offset_properties(dev); |
| 1666 | vmw_kms_create_hotplug_mode_update_property(dev_priv); |
| 1667 | |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1668 | ret = vmw_kms_stdu_init_display(dev_priv); |
| 1669 | if (ret) { |
| 1670 | ret = vmw_kms_sou_init_display(dev_priv); |
| 1671 | if (ret) /* Fallback */ |
| 1672 | ret = vmw_kms_ldu_init_display(dev_priv); |
| 1673 | } |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1674 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1675 | return ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | int vmw_kms_close(struct vmw_private *dev_priv) |
| 1679 | { |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1680 | int ret; |
| 1681 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1682 | /* |
| 1683 | * Docs says we should take the lock before calling this function |
| 1684 | * but since it destroys encoders and our destructor calls |
| 1685 | * drm_encoder_cleanup which takes the lock we deadlock. |
| 1686 | */ |
| 1687 | drm_mode_config_cleanup(dev_priv->dev); |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1688 | if (dev_priv->active_display_unit == vmw_du_screen_object) |
| 1689 | ret = vmw_kms_sou_close_display(dev_priv); |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1690 | else if (dev_priv->active_display_unit == vmw_du_screen_target) |
| 1691 | ret = vmw_kms_stdu_close_display(dev_priv); |
Jakob Bornecrantz | c0d1831 | 2011-11-09 10:25:26 +0100 | [diff] [blame] | 1692 | else |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 1693 | ret = vmw_kms_ldu_close_display(dev_priv); |
| 1694 | |
| 1695 | return ret; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data, |
| 1699 | struct drm_file *file_priv) |
| 1700 | { |
| 1701 | struct drm_vmw_cursor_bypass_arg *arg = data; |
| 1702 | struct vmw_display_unit *du; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1703 | struct drm_crtc *crtc; |
| 1704 | int ret = 0; |
| 1705 | |
| 1706 | |
| 1707 | mutex_lock(&dev->mode_config.mutex); |
| 1708 | if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) { |
| 1709 | |
| 1710 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 1711 | du = vmw_crtc_to_du(crtc); |
| 1712 | du->hotspot_x = arg->xhot; |
| 1713 | du->hotspot_y = arg->yhot; |
| 1714 | } |
| 1715 | |
| 1716 | mutex_unlock(&dev->mode_config.mutex); |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
Rob Clark | a4cd5d6 | 2014-07-17 23:30:02 -0400 | [diff] [blame] | 1720 | crtc = drm_crtc_find(dev, arg->crtc_id); |
| 1721 | if (!crtc) { |
Ville Syrjälä | 4ae87ff | 2013-10-17 13:35:05 +0300 | [diff] [blame] | 1722 | ret = -ENOENT; |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1723 | goto out; |
| 1724 | } |
| 1725 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1726 | du = vmw_crtc_to_du(crtc); |
| 1727 | |
| 1728 | du->hotspot_x = arg->xhot; |
| 1729 | du->hotspot_y = arg->yhot; |
| 1730 | |
| 1731 | out: |
| 1732 | mutex_unlock(&dev->mode_config.mutex); |
| 1733 | |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
| 1737 | int vmw_kms_write_svga(struct vmw_private *vmw_priv, |
| 1738 | unsigned width, unsigned height, unsigned pitch, |
| 1739 | unsigned bpp, unsigned depth) |
| 1740 | { |
| 1741 | if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) |
| 1742 | vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch); |
| 1743 | else if (vmw_fifo_have_pitchlock(vmw_priv)) |
Thomas Hellstrom | b76ff5e | 2015-10-28 10:44:04 +0100 | [diff] [blame] | 1744 | vmw_mmio_write(pitch, vmw_priv->mmio_virt + |
| 1745 | SVGA_FIFO_PITCHLOCK); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1746 | vmw_write(vmw_priv, SVGA_REG_WIDTH, width); |
| 1747 | vmw_write(vmw_priv, SVGA_REG_HEIGHT, height); |
| 1748 | vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp); |
| 1749 | |
| 1750 | if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) { |
| 1751 | DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n", |
| 1752 | depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH)); |
| 1753 | return -EINVAL; |
| 1754 | } |
| 1755 | |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
| 1759 | int vmw_kms_save_vga(struct vmw_private *vmw_priv) |
| 1760 | { |
| 1761 | struct vmw_vga_topology_state *save; |
| 1762 | uint32_t i; |
| 1763 | |
| 1764 | vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH); |
| 1765 | vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT); |
| 1766 | vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL); |
| 1767 | if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) |
| 1768 | vmw_priv->vga_pitchlock = |
| 1769 | vmw_read(vmw_priv, SVGA_REG_PITCHLOCK); |
| 1770 | else if (vmw_fifo_have_pitchlock(vmw_priv)) |
Thomas Hellstrom | b76ff5e | 2015-10-28 10:44:04 +0100 | [diff] [blame] | 1771 | vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt + |
| 1772 | SVGA_FIFO_PITCHLOCK); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1773 | |
| 1774 | if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) |
| 1775 | return 0; |
| 1776 | |
| 1777 | vmw_priv->num_displays = vmw_read(vmw_priv, |
| 1778 | SVGA_REG_NUM_GUEST_DISPLAYS); |
| 1779 | |
| 1780 | if (vmw_priv->num_displays == 0) |
| 1781 | vmw_priv->num_displays = 1; |
| 1782 | |
| 1783 | for (i = 0; i < vmw_priv->num_displays; ++i) { |
| 1784 | save = &vmw_priv->vga_save[i]; |
| 1785 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i); |
| 1786 | save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY); |
| 1787 | save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X); |
| 1788 | save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y); |
| 1789 | save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH); |
| 1790 | save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT); |
| 1791 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID); |
| 1792 | if (i == 0 && vmw_priv->num_displays == 1 && |
| 1793 | save->width == 0 && save->height == 0) { |
| 1794 | |
| 1795 | /* |
| 1796 | * It should be fairly safe to assume that these |
| 1797 | * values are uninitialized. |
| 1798 | */ |
| 1799 | |
| 1800 | save->width = vmw_priv->vga_width - save->pos_x; |
| 1801 | save->height = vmw_priv->vga_height - save->pos_y; |
| 1802 | } |
| 1803 | } |
| 1804 | |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | int vmw_kms_restore_vga(struct vmw_private *vmw_priv) |
| 1809 | { |
| 1810 | struct vmw_vga_topology_state *save; |
| 1811 | uint32_t i; |
| 1812 | |
| 1813 | vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width); |
| 1814 | vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height); |
Jakob Bornecrantz | d7e1958 | 2010-05-28 11:21:59 +0200 | [diff] [blame] | 1815 | vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp); |
| 1816 | if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) |
| 1817 | vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, |
| 1818 | vmw_priv->vga_pitchlock); |
| 1819 | else if (vmw_fifo_have_pitchlock(vmw_priv)) |
Thomas Hellstrom | b76ff5e | 2015-10-28 10:44:04 +0100 | [diff] [blame] | 1820 | vmw_mmio_write(vmw_priv->vga_pitchlock, |
| 1821 | vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK); |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1822 | |
Thomas Hellstrom | 7c4f778 | 2010-06-01 11:38:17 +0200 | [diff] [blame] | 1823 | if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) |
| 1824 | return 0; |
| 1825 | |
| 1826 | for (i = 0; i < vmw_priv->num_displays; ++i) { |
| 1827 | save = &vmw_priv->vga_save[i]; |
| 1828 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i); |
| 1829 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary); |
| 1830 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x); |
| 1831 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y); |
| 1832 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width); |
| 1833 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height); |
| 1834 | vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID); |
| 1835 | } |
| 1836 | |
Jakob Bornecrantz | fb1d973 | 2009-12-10 00:19:58 +0000 | [diff] [blame] | 1837 | return 0; |
| 1838 | } |
Jakob Bornecrantz | d8bd19d | 2010-06-01 11:54:20 +0200 | [diff] [blame] | 1839 | |
Thomas Hellstrom | e133e73 | 2010-10-05 12:43:04 +0200 | [diff] [blame] | 1840 | bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv, |
| 1841 | uint32_t pitch, |
| 1842 | uint32_t height) |
| 1843 | { |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 1844 | return ((u64) pitch * (u64) height) < (u64) |
| 1845 | ((dev_priv->active_display_unit == vmw_du_screen_target) ? |
| 1846 | dev_priv->prim_bb_mem : dev_priv->vram_size); |
Thomas Hellstrom | e133e73 | 2010-10-05 12:43:04 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
Jakob Bornecrantz | 1c482ab | 2011-10-17 11:59:45 +0200 | [diff] [blame] | 1849 | |
| 1850 | /** |
| 1851 | * Function called by DRM code called with vbl_lock held. |
| 1852 | */ |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 1853 | u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe) |
Thomas Hellstrom | 7a1c2f6 | 2010-10-01 10:21:49 +0200 | [diff] [blame] | 1854 | { |
| 1855 | return 0; |
| 1856 | } |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1857 | |
Jakob Bornecrantz | 1c482ab | 2011-10-17 11:59:45 +0200 | [diff] [blame] | 1858 | /** |
| 1859 | * Function called by DRM code called with vbl_lock held. |
| 1860 | */ |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 1861 | int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe) |
Jakob Bornecrantz | 1c482ab | 2011-10-17 11:59:45 +0200 | [diff] [blame] | 1862 | { |
| 1863 | return -ENOSYS; |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * Function called by DRM code called with vbl_lock held. |
| 1868 | */ |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 1869 | void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe) |
Jakob Bornecrantz | 1c482ab | 2011-10-17 11:59:45 +0200 | [diff] [blame] | 1870 | { |
| 1871 | } |
| 1872 | |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1873 | |
| 1874 | /* |
| 1875 | * Small shared kms functions. |
| 1876 | */ |
| 1877 | |
Rashika Kheria | 847c596 | 2014-01-06 22:18:10 +0530 | [diff] [blame] | 1878 | static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num, |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1879 | struct drm_vmw_rect *rects) |
| 1880 | { |
| 1881 | struct drm_device *dev = dev_priv->dev; |
| 1882 | struct vmw_display_unit *du; |
| 1883 | struct drm_connector *con; |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1884 | |
| 1885 | mutex_lock(&dev->mode_config.mutex); |
| 1886 | |
| 1887 | #if 0 |
Thomas Hellstrom | 6ea77d1 | 2011-10-04 20:13:36 +0200 | [diff] [blame] | 1888 | { |
| 1889 | unsigned int i; |
| 1890 | |
| 1891 | DRM_INFO("%s: new layout ", __func__); |
| 1892 | for (i = 0; i < num; i++) |
| 1893 | DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y, |
| 1894 | rects[i].w, rects[i].h); |
| 1895 | DRM_INFO("\n"); |
| 1896 | } |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1897 | #endif |
| 1898 | |
| 1899 | list_for_each_entry(con, &dev->mode_config.connector_list, head) { |
| 1900 | du = vmw_connector_to_du(con); |
| 1901 | if (num > du->unit) { |
| 1902 | du->pref_width = rects[du->unit].w; |
| 1903 | du->pref_height = rects[du->unit].h; |
| 1904 | du->pref_active = true; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 1905 | du->gui_x = rects[du->unit].x; |
| 1906 | du->gui_y = rects[du->unit].y; |
Thomas Hellstrom | 578e609 | 2016-02-12 09:45:42 +0100 | [diff] [blame] | 1907 | drm_object_property_set_value |
| 1908 | (&con->base, dev->mode_config.suggested_x_property, |
| 1909 | du->gui_x); |
| 1910 | drm_object_property_set_value |
| 1911 | (&con->base, dev->mode_config.suggested_y_property, |
| 1912 | du->gui_y); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1913 | } else { |
| 1914 | du->pref_width = 800; |
| 1915 | du->pref_height = 600; |
| 1916 | du->pref_active = false; |
Thomas Hellstrom | 578e609 | 2016-02-12 09:45:42 +0100 | [diff] [blame] | 1917 | drm_object_property_set_value |
| 1918 | (&con->base, dev->mode_config.suggested_x_property, |
| 1919 | 0); |
| 1920 | drm_object_property_set_value |
| 1921 | (&con->base, dev->mode_config.suggested_y_property, |
| 1922 | 0); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1923 | } |
| 1924 | con->status = vmw_du_connector_detect(con, true); |
| 1925 | } |
| 1926 | |
| 1927 | mutex_unlock(&dev->mode_config.mutex); |
Thomas Hellstrom | 578e609 | 2016-02-12 09:45:42 +0100 | [diff] [blame] | 1928 | drm_sysfs_hotplug_event(dev); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1929 | |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
Maarten Lankhorst | 7ea7728 | 2016-06-07 12:49:30 +0200 | [diff] [blame] | 1933 | int vmw_du_crtc_gamma_set(struct drm_crtc *crtc, |
| 1934 | u16 *r, u16 *g, u16 *b, |
| 1935 | uint32_t size) |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1936 | { |
| 1937 | struct vmw_private *dev_priv = vmw_priv(crtc->dev); |
| 1938 | int i; |
| 1939 | |
| 1940 | for (i = 0; i < size; i++) { |
| 1941 | DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i, |
| 1942 | r[i], g[i], b[i]); |
| 1943 | vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8); |
| 1944 | vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8); |
| 1945 | vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8); |
| 1946 | } |
Maarten Lankhorst | 7ea7728 | 2016-06-07 12:49:30 +0200 | [diff] [blame] | 1947 | |
| 1948 | return 0; |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1949 | } |
| 1950 | |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 1951 | int vmw_du_connector_dpms(struct drm_connector *connector, int mode) |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1952 | { |
Maarten Lankhorst | 9a69a9a | 2015-07-21 11:34:55 +0200 | [diff] [blame] | 1953 | return 0; |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1954 | } |
| 1955 | |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1956 | enum drm_connector_status |
| 1957 | vmw_du_connector_detect(struct drm_connector *connector, bool force) |
| 1958 | { |
| 1959 | uint32_t num_displays; |
| 1960 | struct drm_device *dev = connector->dev; |
| 1961 | struct vmw_private *dev_priv = vmw_priv(dev); |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 1962 | struct vmw_display_unit *du = vmw_connector_to_du(connector); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1963 | |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1964 | num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1965 | |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 1966 | return ((vmw_connector_to_du(connector)->unit < num_displays && |
| 1967 | du->pref_active) ? |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 1968 | connector_status_connected : connector_status_disconnected); |
| 1969 | } |
| 1970 | |
| 1971 | static struct drm_display_mode vmw_kms_connector_builtin[] = { |
| 1972 | /* 640x480@60Hz */ |
| 1973 | { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656, |
| 1974 | 752, 800, 0, 480, 489, 492, 525, 0, |
| 1975 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, |
| 1976 | /* 800x600@60Hz */ |
| 1977 | { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840, |
| 1978 | 968, 1056, 0, 600, 601, 605, 628, 0, |
| 1979 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 1980 | /* 1024x768@60Hz */ |
| 1981 | { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048, |
| 1982 | 1184, 1344, 0, 768, 771, 777, 806, 0, |
| 1983 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) }, |
| 1984 | /* 1152x864@75Hz */ |
| 1985 | { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216, |
| 1986 | 1344, 1600, 0, 864, 865, 868, 900, 0, |
| 1987 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 1988 | /* 1280x768@60Hz */ |
| 1989 | { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344, |
| 1990 | 1472, 1664, 0, 768, 771, 778, 798, 0, |
| 1991 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 1992 | /* 1280x800@60Hz */ |
| 1993 | { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352, |
| 1994 | 1480, 1680, 0, 800, 803, 809, 831, 0, |
| 1995 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) }, |
| 1996 | /* 1280x960@60Hz */ |
| 1997 | { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376, |
| 1998 | 1488, 1800, 0, 960, 961, 964, 1000, 0, |
| 1999 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2000 | /* 1280x1024@60Hz */ |
| 2001 | { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328, |
| 2002 | 1440, 1688, 0, 1024, 1025, 1028, 1066, 0, |
| 2003 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2004 | /* 1360x768@60Hz */ |
| 2005 | { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424, |
| 2006 | 1536, 1792, 0, 768, 771, 777, 795, 0, |
| 2007 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2008 | /* 1440x1050@60Hz */ |
| 2009 | { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488, |
| 2010 | 1632, 1864, 0, 1050, 1053, 1057, 1089, 0, |
| 2011 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2012 | /* 1440x900@60Hz */ |
| 2013 | { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520, |
| 2014 | 1672, 1904, 0, 900, 903, 909, 934, 0, |
| 2015 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2016 | /* 1600x1200@60Hz */ |
| 2017 | { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664, |
| 2018 | 1856, 2160, 0, 1200, 1201, 1204, 1250, 0, |
| 2019 | DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2020 | /* 1680x1050@60Hz */ |
| 2021 | { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784, |
| 2022 | 1960, 2240, 0, 1050, 1053, 1059, 1089, 0, |
| 2023 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2024 | /* 1792x1344@60Hz */ |
| 2025 | { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920, |
| 2026 | 2120, 2448, 0, 1344, 1345, 1348, 1394, 0, |
| 2027 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2028 | /* 1853x1392@60Hz */ |
| 2029 | { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952, |
| 2030 | 2176, 2528, 0, 1392, 1393, 1396, 1439, 0, |
| 2031 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2032 | /* 1920x1200@60Hz */ |
| 2033 | { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056, |
| 2034 | 2256, 2592, 0, 1200, 1203, 1209, 1245, 0, |
| 2035 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2036 | /* 1920x1440@60Hz */ |
| 2037 | { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048, |
| 2038 | 2256, 2600, 0, 1440, 1441, 1444, 1500, 0, |
| 2039 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2040 | /* 2560x1600@60Hz */ |
| 2041 | { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752, |
| 2042 | 3032, 3504, 0, 1600, 1603, 1609, 1658, 0, |
| 2043 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) }, |
| 2044 | /* Terminate */ |
| 2045 | { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) }, |
| 2046 | }; |
| 2047 | |
Thomas Hellstrom | 1543b4d | 2011-11-02 09:43:10 +0100 | [diff] [blame] | 2048 | /** |
| 2049 | * vmw_guess_mode_timing - Provide fake timings for a |
| 2050 | * 60Hz vrefresh mode. |
| 2051 | * |
| 2052 | * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay |
| 2053 | * members filled in. |
| 2054 | */ |
Thomas Hellstrom | a278724 | 2015-06-29 12:55:07 -0700 | [diff] [blame] | 2055 | void vmw_guess_mode_timing(struct drm_display_mode *mode) |
Thomas Hellstrom | 1543b4d | 2011-11-02 09:43:10 +0100 | [diff] [blame] | 2056 | { |
| 2057 | mode->hsync_start = mode->hdisplay + 50; |
| 2058 | mode->hsync_end = mode->hsync_start + 50; |
| 2059 | mode->htotal = mode->hsync_end + 50; |
| 2060 | |
| 2061 | mode->vsync_start = mode->vdisplay + 50; |
| 2062 | mode->vsync_end = mode->vsync_start + 50; |
| 2063 | mode->vtotal = mode->vsync_end + 50; |
| 2064 | |
| 2065 | mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6; |
| 2066 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 2067 | } |
| 2068 | |
| 2069 | |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2070 | int vmw_du_connector_fill_modes(struct drm_connector *connector, |
| 2071 | uint32_t max_width, uint32_t max_height) |
| 2072 | { |
| 2073 | struct vmw_display_unit *du = vmw_connector_to_du(connector); |
| 2074 | struct drm_device *dev = connector->dev; |
| 2075 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 2076 | struct drm_display_mode *mode = NULL; |
| 2077 | struct drm_display_mode *bmode; |
| 2078 | struct drm_display_mode prefmode = { DRM_MODE("preferred", |
| 2079 | DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, |
| 2080 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 2081 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) |
| 2082 | }; |
| 2083 | int i; |
Sinclair Yeh | 7c20d21 | 2016-06-29 11:29:47 -0700 | [diff] [blame] | 2084 | u32 assumed_bpp = 4; |
Sinclair Yeh | 9a72384 | 2014-10-31 09:58:06 +0100 | [diff] [blame] | 2085 | |
Sinclair Yeh | 04319d8 | 2016-06-29 12:15:48 -0700 | [diff] [blame] | 2086 | if (dev_priv->assume_16bpp) |
| 2087 | assumed_bpp = 2; |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2088 | |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 2089 | if (dev_priv->active_display_unit == vmw_du_screen_target) { |
| 2090 | max_width = min(max_width, dev_priv->stdu_max_width); |
| 2091 | max_height = min(max_height, dev_priv->stdu_max_height); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /* Add preferred mode */ |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 2095 | mode = drm_mode_duplicate(dev, &prefmode); |
| 2096 | if (!mode) |
| 2097 | return 0; |
| 2098 | mode->hdisplay = du->pref_width; |
| 2099 | mode->vdisplay = du->pref_height; |
| 2100 | vmw_guess_mode_timing(mode); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2101 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 2102 | if (vmw_kms_validate_mode_vram(dev_priv, |
| 2103 | mode->hdisplay * assumed_bpp, |
| 2104 | mode->vdisplay)) { |
| 2105 | drm_mode_probed_add(connector, mode); |
| 2106 | } else { |
| 2107 | drm_mode_destroy(dev, mode); |
| 2108 | mode = NULL; |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 2111 | if (du->pref_mode) { |
| 2112 | list_del_init(&du->pref_mode->head); |
| 2113 | drm_mode_destroy(dev, du->pref_mode); |
| 2114 | } |
| 2115 | |
| 2116 | /* mode might be null here, this is intended */ |
| 2117 | du->pref_mode = mode; |
| 2118 | |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2119 | for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) { |
| 2120 | bmode = &vmw_kms_connector_builtin[i]; |
| 2121 | if (bmode->hdisplay > max_width || |
| 2122 | bmode->vdisplay > max_height) |
| 2123 | continue; |
| 2124 | |
Sinclair Yeh | 9a72384 | 2014-10-31 09:58:06 +0100 | [diff] [blame] | 2125 | if (!vmw_kms_validate_mode_vram(dev_priv, |
| 2126 | bmode->hdisplay * assumed_bpp, |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2127 | bmode->vdisplay)) |
| 2128 | continue; |
| 2129 | |
| 2130 | mode = drm_mode_duplicate(dev, bmode); |
| 2131 | if (!mode) |
| 2132 | return 0; |
| 2133 | mode->vrefresh = drm_mode_vrefresh(mode); |
| 2134 | |
| 2135 | drm_mode_probed_add(connector, mode); |
| 2136 | } |
| 2137 | |
Ville Syrjälä | 6af3e65 | 2015-12-03 23:14:14 +0200 | [diff] [blame] | 2138 | drm_mode_connector_list_update(connector); |
Thomas Hellstrom | f6b0500 | 2015-06-29 12:59:58 -0700 | [diff] [blame] | 2139 | /* Move the prefered mode first, help apps pick the right mode. */ |
| 2140 | drm_mode_sort(&connector->modes); |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2141 | |
| 2142 | return 1; |
| 2143 | } |
| 2144 | |
| 2145 | int vmw_du_connector_set_property(struct drm_connector *connector, |
| 2146 | struct drm_property *property, |
| 2147 | uint64_t val) |
| 2148 | { |
Thomas Hellstrom | 76404ac | 2016-02-12 09:55:45 +0100 | [diff] [blame] | 2149 | struct vmw_display_unit *du = vmw_connector_to_du(connector); |
| 2150 | struct vmw_private *dev_priv = vmw_priv(connector->dev); |
| 2151 | |
| 2152 | if (property == dev_priv->implicit_placement_property) |
| 2153 | du->is_implicit = val; |
| 2154 | |
Jakob Bornecrantz | 626ab77 | 2011-10-04 20:13:20 +0200 | [diff] [blame] | 2155 | return 0; |
| 2156 | } |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2157 | |
| 2158 | |
Sinclair Yeh | 9c2542a | 2017-03-23 11:33:39 -0700 | [diff] [blame] | 2159 | |
Sinclair Yeh | d7721ca | 2017-03-23 11:48:44 -0700 | [diff] [blame] | 2160 | /** |
| 2161 | * vmw_du_connector_atomic_set_property - Atomic version of get property |
| 2162 | * |
| 2163 | * @crtc - crtc the property is associated with |
| 2164 | * |
| 2165 | * Returns: |
| 2166 | * Zero on success, negative errno on failure. |
| 2167 | */ |
| 2168 | int |
| 2169 | vmw_du_connector_atomic_set_property(struct drm_connector *connector, |
| 2170 | struct drm_connector_state *state, |
| 2171 | struct drm_property *property, |
| 2172 | uint64_t val) |
| 2173 | { |
| 2174 | struct vmw_private *dev_priv = vmw_priv(connector->dev); |
| 2175 | struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state); |
| 2176 | struct vmw_display_unit *du = vmw_connector_to_du(connector); |
| 2177 | |
| 2178 | |
| 2179 | if (property == dev_priv->implicit_placement_property) { |
| 2180 | vcs->is_implicit = val; |
| 2181 | |
| 2182 | /* |
| 2183 | * We should really be doing a drm_atomic_commit() to |
| 2184 | * commit the new state, but since this doesn't cause |
| 2185 | * an immedate state change, this is probably ok |
| 2186 | */ |
| 2187 | du->is_implicit = vcs->is_implicit; |
| 2188 | } else { |
| 2189 | return -EINVAL; |
| 2190 | } |
| 2191 | |
| 2192 | return 0; |
| 2193 | } |
| 2194 | |
| 2195 | |
| 2196 | /** |
| 2197 | * vmw_du_connector_atomic_get_property - Atomic version of get property |
| 2198 | * |
| 2199 | * @connector - connector the property is associated with |
| 2200 | * |
| 2201 | * Returns: |
| 2202 | * Zero on success, negative errno on failure. |
| 2203 | */ |
| 2204 | int |
| 2205 | vmw_du_connector_atomic_get_property(struct drm_connector *connector, |
| 2206 | const struct drm_connector_state *state, |
| 2207 | struct drm_property *property, |
| 2208 | uint64_t *val) |
| 2209 | { |
| 2210 | struct vmw_private *dev_priv = vmw_priv(connector->dev); |
| 2211 | struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state); |
| 2212 | |
| 2213 | if (property == dev_priv->implicit_placement_property) |
| 2214 | *val = vcs->is_implicit; |
| 2215 | else { |
| 2216 | DRM_ERROR("Invalid Property %s\n", property->name); |
| 2217 | return -EINVAL; |
| 2218 | } |
| 2219 | |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2224 | int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data, |
| 2225 | struct drm_file *file_priv) |
| 2226 | { |
| 2227 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 2228 | struct drm_vmw_update_layout_arg *arg = |
| 2229 | (struct drm_vmw_update_layout_arg *)data; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2230 | void __user *user_rects; |
| 2231 | struct drm_vmw_rect *rects; |
| 2232 | unsigned rects_size; |
| 2233 | int ret; |
| 2234 | int i; |
Sinclair Yeh | 65ade7d | 2015-07-16 10:49:13 -0700 | [diff] [blame] | 2235 | u64 total_pixels = 0; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2236 | struct drm_mode_config *mode_config = &dev->mode_config; |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 2237 | struct drm_vmw_rect bounding_box = {0}; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2238 | |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2239 | if (!arg->num_outputs) { |
| 2240 | struct drm_vmw_rect def_rect = {0, 0, 800, 600}; |
| 2241 | vmw_du_update_layout(dev_priv, 1, &def_rect); |
Thomas Hellstrom | 5151adb | 2015-03-09 01:56:21 -0700 | [diff] [blame] | 2242 | return 0; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect); |
Xi Wang | bab9efc | 2011-11-28 12:25:43 +0100 | [diff] [blame] | 2246 | rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect), |
| 2247 | GFP_KERNEL); |
Thomas Hellstrom | 5151adb | 2015-03-09 01:56:21 -0700 | [diff] [blame] | 2248 | if (unlikely(!rects)) |
| 2249 | return -ENOMEM; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2250 | |
| 2251 | user_rects = (void __user *)(unsigned long)arg->rects; |
| 2252 | ret = copy_from_user(rects, user_rects, rects_size); |
| 2253 | if (unlikely(ret != 0)) { |
| 2254 | DRM_ERROR("Failed to get rects.\n"); |
| 2255 | ret = -EFAULT; |
| 2256 | goto out_free; |
| 2257 | } |
| 2258 | |
| 2259 | for (i = 0; i < arg->num_outputs; ++i) { |
Xi Wang | bab9efc | 2011-11-28 12:25:43 +0100 | [diff] [blame] | 2260 | if (rects[i].x < 0 || |
| 2261 | rects[i].y < 0 || |
| 2262 | rects[i].x + rects[i].w > mode_config->max_width || |
| 2263 | rects[i].y + rects[i].h > mode_config->max_height) { |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2264 | DRM_ERROR("Invalid GUI layout.\n"); |
| 2265 | ret = -EINVAL; |
| 2266 | goto out_free; |
| 2267 | } |
Sinclair Yeh | c8261a9 | 2015-06-26 01:23:42 -0700 | [diff] [blame] | 2268 | |
| 2269 | /* |
| 2270 | * bounding_box.w and bunding_box.h are used as |
| 2271 | * lower-right coordinates |
| 2272 | */ |
| 2273 | if (rects[i].x + rects[i].w > bounding_box.w) |
| 2274 | bounding_box.w = rects[i].x + rects[i].w; |
| 2275 | |
| 2276 | if (rects[i].y + rects[i].h > bounding_box.h) |
| 2277 | bounding_box.h = rects[i].y + rects[i].h; |
Sinclair Yeh | 65ade7d | 2015-07-16 10:49:13 -0700 | [diff] [blame] | 2278 | |
| 2279 | total_pixels += (u64) rects[i].w * (u64) rects[i].h; |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2280 | } |
| 2281 | |
Sinclair Yeh | 65ade7d | 2015-07-16 10:49:13 -0700 | [diff] [blame] | 2282 | if (dev_priv->active_display_unit == vmw_du_screen_target) { |
| 2283 | /* |
| 2284 | * For Screen Targets, the limits for a toplogy are: |
| 2285 | * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem |
| 2286 | * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem |
| 2287 | */ |
Thomas Hellstrom | 0f58038 | 2017-01-19 11:01:04 -0800 | [diff] [blame] | 2288 | u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4; |
Sinclair Yeh | 65ade7d | 2015-07-16 10:49:13 -0700 | [diff] [blame] | 2289 | u64 pixel_mem = total_pixels * 4; |
| 2290 | |
| 2291 | if (bb_mem > dev_priv->prim_bb_mem) { |
| 2292 | DRM_ERROR("Topology is beyond supported limits.\n"); |
Sinclair Yeh | 35c0512 | 2015-06-26 01:42:06 -0700 | [diff] [blame] | 2293 | ret = -EINVAL; |
| 2294 | goto out_free; |
| 2295 | } |
| 2296 | |
Sinclair Yeh | 65ade7d | 2015-07-16 10:49:13 -0700 | [diff] [blame] | 2297 | if (pixel_mem > dev_priv->prim_bb_mem) { |
| 2298 | DRM_ERROR("Combined output size too large\n"); |
| 2299 | ret = -EINVAL; |
| 2300 | goto out_free; |
| 2301 | } |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | vmw_du_update_layout(dev_priv, arg->num_outputs, rects); |
| 2305 | |
| 2306 | out_free: |
| 2307 | kfree(rects); |
Thomas Hellstrom | cd2b89e | 2011-10-25 23:35:53 +0200 | [diff] [blame] | 2308 | return ret; |
| 2309 | } |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2310 | |
| 2311 | /** |
| 2312 | * vmw_kms_helper_dirty - Helper to build commands and perform actions based |
| 2313 | * on a set of cliprects and a set of display units. |
| 2314 | * |
| 2315 | * @dev_priv: Pointer to a device private structure. |
| 2316 | * @framebuffer: Pointer to the framebuffer on which to perform the actions. |
| 2317 | * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL. |
| 2318 | * Cliprects are given in framebuffer coordinates. |
| 2319 | * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must |
| 2320 | * be NULL. Cliprects are given in source coordinates. |
| 2321 | * @dest_x: X coordinate offset for the crtc / destination clip rects. |
| 2322 | * @dest_y: Y coordinate offset for the crtc / destination clip rects. |
| 2323 | * @num_clips: Number of cliprects in the @clips or @vclips array. |
| 2324 | * @increment: Integer with which to increment the clip counter when looping. |
| 2325 | * Used to skip a predetermined number of clip rects. |
| 2326 | * @dirty: Closure structure. See the description of struct vmw_kms_dirty. |
| 2327 | */ |
| 2328 | int vmw_kms_helper_dirty(struct vmw_private *dev_priv, |
| 2329 | struct vmw_framebuffer *framebuffer, |
| 2330 | const struct drm_clip_rect *clips, |
| 2331 | const struct drm_vmw_rect *vclips, |
| 2332 | s32 dest_x, s32 dest_y, |
| 2333 | int num_clips, |
| 2334 | int increment, |
| 2335 | struct vmw_kms_dirty *dirty) |
| 2336 | { |
| 2337 | struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS]; |
| 2338 | struct drm_crtc *crtc; |
| 2339 | u32 num_units = 0; |
| 2340 | u32 i, k; |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2341 | |
| 2342 | dirty->dev_priv = dev_priv; |
| 2343 | |
| 2344 | list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) { |
| 2345 | if (crtc->primary->fb != &framebuffer->base) |
| 2346 | continue; |
| 2347 | units[num_units++] = vmw_crtc_to_du(crtc); |
| 2348 | } |
| 2349 | |
| 2350 | for (k = 0; k < num_units; k++) { |
| 2351 | struct vmw_display_unit *unit = units[k]; |
| 2352 | s32 crtc_x = unit->crtc.x; |
| 2353 | s32 crtc_y = unit->crtc.y; |
| 2354 | s32 crtc_width = unit->crtc.mode.hdisplay; |
| 2355 | s32 crtc_height = unit->crtc.mode.vdisplay; |
| 2356 | const struct drm_clip_rect *clips_ptr = clips; |
| 2357 | const struct drm_vmw_rect *vclips_ptr = vclips; |
| 2358 | |
| 2359 | dirty->unit = unit; |
| 2360 | if (dirty->fifo_reserve_size > 0) { |
| 2361 | dirty->cmd = vmw_fifo_reserve(dev_priv, |
| 2362 | dirty->fifo_reserve_size); |
| 2363 | if (!dirty->cmd) { |
| 2364 | DRM_ERROR("Couldn't reserve fifo space " |
| 2365 | "for dirty blits.\n"); |
Christian Engelmayer | f3b8c0c | 2015-09-19 00:32:24 +0200 | [diff] [blame] | 2366 | return -ENOMEM; |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2367 | } |
| 2368 | memset(dirty->cmd, 0, dirty->fifo_reserve_size); |
| 2369 | } |
| 2370 | dirty->num_hits = 0; |
| 2371 | for (i = 0; i < num_clips; i++, clips_ptr += increment, |
| 2372 | vclips_ptr += increment) { |
| 2373 | s32 clip_left; |
| 2374 | s32 clip_top; |
| 2375 | |
| 2376 | /* |
| 2377 | * Select clip array type. Note that integer type |
| 2378 | * in @clips is unsigned short, whereas in @vclips |
| 2379 | * it's 32-bit. |
| 2380 | */ |
| 2381 | if (clips) { |
| 2382 | dirty->fb_x = (s32) clips_ptr->x1; |
| 2383 | dirty->fb_y = (s32) clips_ptr->y1; |
| 2384 | dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x - |
| 2385 | crtc_x; |
| 2386 | dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y - |
| 2387 | crtc_y; |
| 2388 | } else { |
| 2389 | dirty->fb_x = vclips_ptr->x; |
| 2390 | dirty->fb_y = vclips_ptr->y; |
| 2391 | dirty->unit_x2 = dirty->fb_x + vclips_ptr->w + |
| 2392 | dest_x - crtc_x; |
| 2393 | dirty->unit_y2 = dirty->fb_y + vclips_ptr->h + |
| 2394 | dest_y - crtc_y; |
| 2395 | } |
| 2396 | |
| 2397 | dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x; |
| 2398 | dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y; |
| 2399 | |
| 2400 | /* Skip this clip if it's outside the crtc region */ |
| 2401 | if (dirty->unit_x1 >= crtc_width || |
| 2402 | dirty->unit_y1 >= crtc_height || |
| 2403 | dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0) |
| 2404 | continue; |
| 2405 | |
| 2406 | /* Clip right and bottom to crtc limits */ |
| 2407 | dirty->unit_x2 = min_t(s32, dirty->unit_x2, |
| 2408 | crtc_width); |
| 2409 | dirty->unit_y2 = min_t(s32, dirty->unit_y2, |
| 2410 | crtc_height); |
| 2411 | |
| 2412 | /* Clip left and top to crtc limits */ |
| 2413 | clip_left = min_t(s32, dirty->unit_x1, 0); |
| 2414 | clip_top = min_t(s32, dirty->unit_y1, 0); |
| 2415 | dirty->unit_x1 -= clip_left; |
| 2416 | dirty->unit_y1 -= clip_top; |
| 2417 | dirty->fb_x -= clip_left; |
| 2418 | dirty->fb_y -= clip_top; |
| 2419 | |
| 2420 | dirty->clip(dirty); |
| 2421 | } |
| 2422 | |
| 2423 | dirty->fifo_commit(dirty); |
| 2424 | } |
| 2425 | |
| 2426 | return 0; |
| 2427 | } |
| 2428 | |
| 2429 | /** |
| 2430 | * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before |
| 2431 | * command submission. |
| 2432 | * |
| 2433 | * @dev_priv. Pointer to a device private structure. |
| 2434 | * @buf: The buffer object |
| 2435 | * @interruptible: Whether to perform waits as interruptible. |
| 2436 | * @validate_as_mob: Whether the buffer should be validated as a MOB. If false, |
| 2437 | * The buffer will be validated as a GMR. Already pinned buffers will not be |
| 2438 | * validated. |
| 2439 | * |
| 2440 | * Returns 0 on success, negative error code on failure, -ERESTARTSYS if |
| 2441 | * interrupted by a signal. |
| 2442 | */ |
| 2443 | int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv, |
| 2444 | struct vmw_dma_buffer *buf, |
| 2445 | bool interruptible, |
| 2446 | bool validate_as_mob) |
| 2447 | { |
| 2448 | struct ttm_buffer_object *bo = &buf->base; |
| 2449 | int ret; |
| 2450 | |
Christian König | dfd5e50 | 2016-04-06 11:12:03 +0200 | [diff] [blame] | 2451 | ttm_bo_reserve(bo, false, false, NULL); |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2452 | ret = vmw_validate_single_buffer(dev_priv, bo, interruptible, |
| 2453 | validate_as_mob); |
| 2454 | if (ret) |
| 2455 | ttm_bo_unreserve(bo); |
| 2456 | |
| 2457 | return ret; |
| 2458 | } |
| 2459 | |
| 2460 | /** |
| 2461 | * vmw_kms_helper_buffer_revert - Undo the actions of |
| 2462 | * vmw_kms_helper_buffer_prepare. |
| 2463 | * |
| 2464 | * @res: Pointer to the buffer object. |
| 2465 | * |
| 2466 | * Helper to be used if an error forces the caller to undo the actions of |
| 2467 | * vmw_kms_helper_buffer_prepare. |
| 2468 | */ |
| 2469 | void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf) |
| 2470 | { |
| 2471 | if (buf) |
| 2472 | ttm_bo_unreserve(&buf->base); |
| 2473 | } |
| 2474 | |
| 2475 | /** |
| 2476 | * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after |
| 2477 | * kms command submission. |
| 2478 | * |
| 2479 | * @dev_priv: Pointer to a device private structure. |
| 2480 | * @file_priv: Pointer to a struct drm_file representing the caller's |
| 2481 | * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely |
| 2482 | * if non-NULL, @user_fence_rep must be non-NULL. |
| 2483 | * @buf: The buffer object. |
| 2484 | * @out_fence: Optional pointer to a fence pointer. If non-NULL, a |
| 2485 | * ref-counted fence pointer is returned here. |
| 2486 | * @user_fence_rep: Optional pointer to a user-space provided struct |
| 2487 | * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the |
| 2488 | * function copies fence data to user-space in a fail-safe manner. |
| 2489 | */ |
| 2490 | void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv, |
| 2491 | struct drm_file *file_priv, |
| 2492 | struct vmw_dma_buffer *buf, |
| 2493 | struct vmw_fence_obj **out_fence, |
| 2494 | struct drm_vmw_fence_rep __user * |
| 2495 | user_fence_rep) |
| 2496 | { |
| 2497 | struct vmw_fence_obj *fence; |
| 2498 | uint32_t handle; |
| 2499 | int ret; |
| 2500 | |
| 2501 | ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence, |
| 2502 | file_priv ? &handle : NULL); |
| 2503 | if (buf) |
| 2504 | vmw_fence_single_bo(&buf->base, fence); |
| 2505 | if (file_priv) |
| 2506 | vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), |
| 2507 | ret, user_fence_rep, fence, |
| 2508 | handle); |
| 2509 | if (out_fence) |
| 2510 | *out_fence = fence; |
| 2511 | else |
| 2512 | vmw_fence_obj_unreference(&fence); |
| 2513 | |
| 2514 | vmw_kms_helper_buffer_revert(buf); |
| 2515 | } |
| 2516 | |
| 2517 | |
| 2518 | /** |
| 2519 | * vmw_kms_helper_resource_revert - Undo the actions of |
| 2520 | * vmw_kms_helper_resource_prepare. |
| 2521 | * |
| 2522 | * @res: Pointer to the resource. Typically a surface. |
| 2523 | * |
| 2524 | * Helper to be used if an error forces the caller to undo the actions of |
| 2525 | * vmw_kms_helper_resource_prepare. |
| 2526 | */ |
| 2527 | void vmw_kms_helper_resource_revert(struct vmw_resource *res) |
| 2528 | { |
| 2529 | vmw_kms_helper_buffer_revert(res->backup); |
Thomas Hellstrom | d80efd5 | 2015-08-10 10:39:35 -0700 | [diff] [blame] | 2530 | vmw_resource_unreserve(res, false, NULL, 0); |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2531 | mutex_unlock(&res->dev_priv->cmdbuf_mutex); |
| 2532 | } |
| 2533 | |
| 2534 | /** |
| 2535 | * vmw_kms_helper_resource_prepare - Reserve and validate a resource before |
| 2536 | * command submission. |
| 2537 | * |
| 2538 | * @res: Pointer to the resource. Typically a surface. |
| 2539 | * @interruptible: Whether to perform waits as interruptible. |
| 2540 | * |
| 2541 | * Reserves and validates also the backup buffer if a guest-backed resource. |
| 2542 | * Returns 0 on success, negative error code on failure. -ERESTARTSYS if |
| 2543 | * interrupted by a signal. |
| 2544 | */ |
| 2545 | int vmw_kms_helper_resource_prepare(struct vmw_resource *res, |
| 2546 | bool interruptible) |
| 2547 | { |
| 2548 | int ret = 0; |
| 2549 | |
| 2550 | if (interruptible) |
| 2551 | ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex); |
| 2552 | else |
| 2553 | mutex_lock(&res->dev_priv->cmdbuf_mutex); |
| 2554 | |
| 2555 | if (unlikely(ret != 0)) |
| 2556 | return -ERESTARTSYS; |
| 2557 | |
| 2558 | ret = vmw_resource_reserve(res, interruptible, false); |
| 2559 | if (ret) |
| 2560 | goto out_unlock; |
| 2561 | |
| 2562 | if (res->backup) { |
| 2563 | ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup, |
| 2564 | interruptible, |
| 2565 | res->dev_priv->has_mob); |
| 2566 | if (ret) |
| 2567 | goto out_unreserve; |
| 2568 | } |
| 2569 | ret = vmw_resource_validate(res); |
| 2570 | if (ret) |
| 2571 | goto out_revert; |
| 2572 | return 0; |
| 2573 | |
| 2574 | out_revert: |
| 2575 | vmw_kms_helper_buffer_revert(res->backup); |
| 2576 | out_unreserve: |
Thomas Hellstrom | d80efd5 | 2015-08-10 10:39:35 -0700 | [diff] [blame] | 2577 | vmw_resource_unreserve(res, false, NULL, 0); |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2578 | out_unlock: |
| 2579 | mutex_unlock(&res->dev_priv->cmdbuf_mutex); |
| 2580 | return ret; |
| 2581 | } |
| 2582 | |
| 2583 | /** |
| 2584 | * vmw_kms_helper_resource_finish - Unreserve and fence a resource after |
| 2585 | * kms command submission. |
| 2586 | * |
| 2587 | * @res: Pointer to the resource. Typically a surface. |
| 2588 | * @out_fence: Optional pointer to a fence pointer. If non-NULL, a |
| 2589 | * ref-counted fence pointer is returned here. |
| 2590 | */ |
| 2591 | void vmw_kms_helper_resource_finish(struct vmw_resource *res, |
| 2592 | struct vmw_fence_obj **out_fence) |
| 2593 | { |
| 2594 | if (res->backup || out_fence) |
| 2595 | vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup, |
| 2596 | out_fence, NULL); |
| 2597 | |
Thomas Hellstrom | d80efd5 | 2015-08-10 10:39:35 -0700 | [diff] [blame] | 2598 | vmw_resource_unreserve(res, false, NULL, 0); |
Thomas Hellstrom | 1a4b172 | 2015-06-26 02:03:53 -0700 | [diff] [blame] | 2599 | mutex_unlock(&res->dev_priv->cmdbuf_mutex); |
| 2600 | } |
Thomas Hellstrom | 6bf6bf0 | 2015-06-26 02:22:40 -0700 | [diff] [blame] | 2601 | |
| 2602 | /** |
| 2603 | * vmw_kms_update_proxy - Helper function to update a proxy surface from |
| 2604 | * its backing MOB. |
| 2605 | * |
| 2606 | * @res: Pointer to the surface resource |
| 2607 | * @clips: Clip rects in framebuffer (surface) space. |
| 2608 | * @num_clips: Number of clips in @clips. |
| 2609 | * @increment: Integer with which to increment the clip counter when looping. |
| 2610 | * Used to skip a predetermined number of clip rects. |
| 2611 | * |
| 2612 | * This function makes sure the proxy surface is updated from its backing MOB |
| 2613 | * using the region given by @clips. The surface resource @res and its backing |
| 2614 | * MOB needs to be reserved and validated on call. |
| 2615 | */ |
| 2616 | int vmw_kms_update_proxy(struct vmw_resource *res, |
| 2617 | const struct drm_clip_rect *clips, |
| 2618 | unsigned num_clips, |
| 2619 | int increment) |
| 2620 | { |
| 2621 | struct vmw_private *dev_priv = res->dev_priv; |
| 2622 | struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size; |
| 2623 | struct { |
| 2624 | SVGA3dCmdHeader header; |
| 2625 | SVGA3dCmdUpdateGBImage body; |
| 2626 | } *cmd; |
| 2627 | SVGA3dBox *box; |
| 2628 | size_t copy_size = 0; |
| 2629 | int i; |
| 2630 | |
| 2631 | if (!clips) |
| 2632 | return 0; |
| 2633 | |
| 2634 | cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips); |
| 2635 | if (!cmd) { |
| 2636 | DRM_ERROR("Couldn't reserve fifo space for proxy surface " |
| 2637 | "update.\n"); |
| 2638 | return -ENOMEM; |
| 2639 | } |
| 2640 | |
| 2641 | for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) { |
| 2642 | box = &cmd->body.box; |
| 2643 | |
| 2644 | cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE; |
| 2645 | cmd->header.size = sizeof(cmd->body); |
| 2646 | cmd->body.image.sid = res->id; |
| 2647 | cmd->body.image.face = 0; |
| 2648 | cmd->body.image.mipmap = 0; |
| 2649 | |
| 2650 | if (clips->x1 > size->width || clips->x2 > size->width || |
| 2651 | clips->y1 > size->height || clips->y2 > size->height) { |
| 2652 | DRM_ERROR("Invalid clips outsize of framebuffer.\n"); |
| 2653 | return -EINVAL; |
| 2654 | } |
| 2655 | |
| 2656 | box->x = clips->x1; |
| 2657 | box->y = clips->y1; |
| 2658 | box->z = 0; |
| 2659 | box->w = clips->x2 - clips->x1; |
| 2660 | box->h = clips->y2 - clips->y1; |
| 2661 | box->d = 1; |
| 2662 | |
| 2663 | copy_size += sizeof(*cmd); |
| 2664 | } |
| 2665 | |
| 2666 | vmw_fifo_commit(dev_priv, copy_size); |
| 2667 | |
| 2668 | return 0; |
| 2669 | } |
Thomas Hellstrom | a278724 | 2015-06-29 12:55:07 -0700 | [diff] [blame] | 2670 | |
| 2671 | int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv, |
| 2672 | unsigned unit, |
| 2673 | u32 max_width, |
| 2674 | u32 max_height, |
| 2675 | struct drm_connector **p_con, |
| 2676 | struct drm_crtc **p_crtc, |
| 2677 | struct drm_display_mode **p_mode) |
| 2678 | { |
| 2679 | struct drm_connector *con; |
| 2680 | struct vmw_display_unit *du; |
| 2681 | struct drm_display_mode *mode; |
| 2682 | int i = 0; |
| 2683 | |
| 2684 | list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list, |
| 2685 | head) { |
| 2686 | if (i == unit) |
| 2687 | break; |
| 2688 | |
| 2689 | ++i; |
| 2690 | } |
| 2691 | |
| 2692 | if (i != unit) { |
| 2693 | DRM_ERROR("Could not find initial display unit.\n"); |
| 2694 | return -EINVAL; |
| 2695 | } |
| 2696 | |
| 2697 | if (list_empty(&con->modes)) |
| 2698 | (void) vmw_du_connector_fill_modes(con, max_width, max_height); |
| 2699 | |
| 2700 | if (list_empty(&con->modes)) { |
| 2701 | DRM_ERROR("Could not find initial display mode.\n"); |
| 2702 | return -EINVAL; |
| 2703 | } |
| 2704 | |
| 2705 | du = vmw_connector_to_du(con); |
| 2706 | *p_con = con; |
| 2707 | *p_crtc = &du->crtc; |
| 2708 | |
| 2709 | list_for_each_entry(mode, &con->modes, head) { |
| 2710 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
| 2711 | break; |
| 2712 | } |
| 2713 | |
| 2714 | if (mode->type & DRM_MODE_TYPE_PREFERRED) |
| 2715 | *p_mode = mode; |
| 2716 | else { |
| 2717 | WARN_ONCE(true, "Could not find initial preferred mode.\n"); |
| 2718 | *p_mode = list_first_entry(&con->modes, |
| 2719 | struct drm_display_mode, |
| 2720 | head); |
| 2721 | } |
| 2722 | |
| 2723 | return 0; |
| 2724 | } |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2725 | |
| 2726 | /** |
| 2727 | * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer |
| 2728 | * |
| 2729 | * @dev_priv: Pointer to a device private struct. |
| 2730 | * @du: The display unit of the crtc. |
| 2731 | */ |
| 2732 | void vmw_kms_del_active(struct vmw_private *dev_priv, |
| 2733 | struct vmw_display_unit *du) |
| 2734 | { |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2735 | mutex_lock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2736 | if (du->active_implicit) { |
| 2737 | if (--(dev_priv->num_implicit) == 0) |
| 2738 | dev_priv->implicit_fb = NULL; |
| 2739 | du->active_implicit = false; |
| 2740 | } |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2741 | mutex_unlock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /** |
| 2745 | * vmw_kms_add_active - register a crtc binding to an implicit framebuffer |
| 2746 | * |
| 2747 | * @vmw_priv: Pointer to a device private struct. |
| 2748 | * @du: The display unit of the crtc. |
| 2749 | * @vfb: The implicit framebuffer |
| 2750 | * |
| 2751 | * Registers a binding to an implicit framebuffer. |
| 2752 | */ |
| 2753 | void vmw_kms_add_active(struct vmw_private *dev_priv, |
| 2754 | struct vmw_display_unit *du, |
| 2755 | struct vmw_framebuffer *vfb) |
| 2756 | { |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2757 | mutex_lock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2758 | WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb); |
| 2759 | |
| 2760 | if (!du->active_implicit && du->is_implicit) { |
| 2761 | dev_priv->implicit_fb = vfb; |
| 2762 | du->active_implicit = true; |
| 2763 | dev_priv->num_implicit++; |
| 2764 | } |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2765 | mutex_unlock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /** |
| 2769 | * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc. |
| 2770 | * |
| 2771 | * @dev_priv: Pointer to device-private struct. |
| 2772 | * @crtc: The crtc we want to flip. |
| 2773 | * |
| 2774 | * Returns true or false depending whether it's OK to flip this crtc |
| 2775 | * based on the criterion that we must not have more than one implicit |
| 2776 | * frame-buffer at any one time. |
| 2777 | */ |
| 2778 | bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv, |
| 2779 | struct drm_crtc *crtc) |
| 2780 | { |
| 2781 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2782 | bool ret; |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2783 | |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2784 | mutex_lock(&dev_priv->global_kms_state_mutex); |
| 2785 | ret = !du->is_implicit || dev_priv->num_implicit == 1; |
| 2786 | mutex_unlock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2787 | |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2788 | return ret; |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | /** |
| 2792 | * vmw_kms_update_implicit_fb - Update the implicit fb. |
| 2793 | * |
| 2794 | * @dev_priv: Pointer to device-private struct. |
| 2795 | * @crtc: The crtc the new implicit frame-buffer is bound to. |
| 2796 | */ |
| 2797 | void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv, |
| 2798 | struct drm_crtc *crtc) |
| 2799 | { |
| 2800 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 2801 | struct vmw_framebuffer *vfb; |
| 2802 | |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2803 | mutex_lock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2804 | |
| 2805 | if (!du->is_implicit) |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2806 | goto out_unlock; |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2807 | |
| 2808 | vfb = vmw_framebuffer_to_vfb(crtc->primary->fb); |
| 2809 | WARN_ON_ONCE(dev_priv->num_implicit != 1 && |
| 2810 | dev_priv->implicit_fb != vfb); |
| 2811 | |
| 2812 | dev_priv->implicit_fb = vfb; |
Thomas Hellstrom | 93cd168 | 2016-05-03 11:24:35 +0200 | [diff] [blame] | 2813 | out_unlock: |
| 2814 | mutex_unlock(&dev_priv->global_kms_state_mutex); |
Thomas Hellstrom | 75c0685 | 2016-02-12 09:00:26 +0100 | [diff] [blame] | 2815 | } |
Thomas Hellstrom | 76404ac | 2016-02-12 09:55:45 +0100 | [diff] [blame] | 2816 | |
| 2817 | /** |
| 2818 | * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement |
| 2819 | * property. |
| 2820 | * |
| 2821 | * @dev_priv: Pointer to a device private struct. |
| 2822 | * @immutable: Whether the property is immutable. |
| 2823 | * |
| 2824 | * Sets up the implicit placement property unless it's already set up. |
| 2825 | */ |
| 2826 | void |
| 2827 | vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv, |
| 2828 | bool immutable) |
| 2829 | { |
| 2830 | if (dev_priv->implicit_placement_property) |
| 2831 | return; |
| 2832 | |
| 2833 | dev_priv->implicit_placement_property = |
| 2834 | drm_property_create_range(dev_priv->dev, |
| 2835 | immutable ? |
| 2836 | DRM_MODE_PROP_IMMUTABLE : 0, |
| 2837 | "implicit_placement", 0, 1); |
| 2838 | |
| 2839 | } |