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