blob: 1748a7142aca76811ab6fff8b4dbcf921496737b [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
2 *
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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"
29
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +020030
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000031/* Might need a hrtimer here? */
32#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000034void vmw_display_unit_cleanup(struct vmw_display_unit *du)
35{
36 if (du->cursor_surface)
37 vmw_surface_unreference(&du->cursor_surface);
38 if (du->cursor_dmabuf)
39 vmw_dmabuf_unreference(&du->cursor_dmabuf);
40 drm_crtc_cleanup(&du->crtc);
41 drm_encoder_cleanup(&du->encoder);
42 drm_connector_cleanup(&du->connector);
43}
44
45/*
46 * Display Unit Cursor functions
47 */
48
49int vmw_cursor_update_image(struct vmw_private *dev_priv,
50 u32 *image, u32 width, u32 height,
51 u32 hotspotX, u32 hotspotY)
52{
53 struct {
54 u32 cmd;
55 SVGAFifoCmdDefineAlphaCursor cursor;
56 } *cmd;
57 u32 image_size = width * height * 4;
58 u32 cmd_size = sizeof(*cmd) + image_size;
59
60 if (!image)
61 return -EINVAL;
62
63 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
64 if (unlikely(cmd == NULL)) {
65 DRM_ERROR("Fifo reserve failed.\n");
66 return -ENOMEM;
67 }
68
69 memset(cmd, 0, sizeof(*cmd));
70
71 memcpy(&cmd[1], image, image_size);
72
73 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
74 cmd->cursor.id = cpu_to_le32(0);
75 cmd->cursor.width = cpu_to_le32(width);
76 cmd->cursor.height = cpu_to_le32(height);
77 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
78 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
79
80 vmw_fifo_commit(dev_priv, cmd_size);
81
82 return 0;
83}
84
85void vmw_cursor_update_position(struct vmw_private *dev_priv,
86 bool show, int x, int y)
87{
88 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
89 uint32_t count;
90
91 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
92 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
93 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
94 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
95 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
96}
97
98int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
99 uint32_t handle, uint32_t width, uint32_t height)
100{
101 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
102 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
103 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
104 struct vmw_surface *surface = NULL;
105 struct vmw_dma_buffer *dmabuf = NULL;
106 int ret;
107
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100108 /* A lot of the code assumes this */
109 if (handle && (width != 64 || height != 64))
110 return -EINVAL;
111
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000112 if (handle) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100113 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
114 handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000115 if (!ret) {
116 if (!surface->snooper.image) {
117 DRM_ERROR("surface not suitable for cursor\n");
Jakob Bornecrantze5c8dbb2011-11-03 21:03:06 +0100118 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000119 return -EINVAL;
120 }
121 } else {
122 ret = vmw_user_dmabuf_lookup(tfile,
123 handle, &dmabuf);
124 if (ret) {
125 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
126 return -EINVAL;
127 }
128 }
129 }
130
131 /* takedown old cursor */
132 if (du->cursor_surface) {
133 du->cursor_surface->snooper.crtc = NULL;
134 vmw_surface_unreference(&du->cursor_surface);
135 }
136 if (du->cursor_dmabuf)
137 vmw_dmabuf_unreference(&du->cursor_dmabuf);
138
139 /* setup new image */
140 if (surface) {
141 /* vmw_user_surface_lookup takes one reference */
142 du->cursor_surface = surface;
143
144 du->cursor_surface->snooper.crtc = crtc;
145 du->cursor_age = du->cursor_surface->snooper.age;
146 vmw_cursor_update_image(dev_priv, surface->snooper.image,
147 64, 64, du->hotspot_x, du->hotspot_y);
148 } else if (dmabuf) {
149 struct ttm_bo_kmap_obj map;
150 unsigned long kmap_offset;
151 unsigned long kmap_num;
152 void *virtual;
153 bool dummy;
154
155 /* vmw_user_surface_lookup takes one reference */
156 du->cursor_dmabuf = dmabuf;
157
158 kmap_offset = 0;
159 kmap_num = (64*64*4) >> PAGE_SHIFT;
160
161 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
162 if (unlikely(ret != 0)) {
163 DRM_ERROR("reserve failed\n");
164 return -EINVAL;
165 }
166
167 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
168 if (unlikely(ret != 0))
169 goto err_unreserve;
170
171 virtual = ttm_kmap_obj_virtual(&map, &dummy);
172 vmw_cursor_update_image(dev_priv, virtual, 64, 64,
173 du->hotspot_x, du->hotspot_y);
174
175 ttm_bo_kunmap(&map);
176err_unreserve:
177 ttm_bo_unreserve(&dmabuf->base);
178
179 } else {
180 vmw_cursor_update_position(dev_priv, false, 0, 0);
181 return 0;
182 }
183
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100184 vmw_cursor_update_position(dev_priv, true,
185 du->cursor_x + du->hotspot_x,
186 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000187
188 return 0;
189}
190
191int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
192{
193 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
194 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
195 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
196
197 du->cursor_x = x + crtc->x;
198 du->cursor_y = y + crtc->y;
199
200 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100201 du->cursor_x + du->hotspot_x,
202 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000203
204 return 0;
205}
206
207void vmw_kms_cursor_snoop(struct vmw_surface *srf,
208 struct ttm_object_file *tfile,
209 struct ttm_buffer_object *bo,
210 SVGA3dCmdHeader *header)
211{
212 struct ttm_bo_kmap_obj map;
213 unsigned long kmap_offset;
214 unsigned long kmap_num;
215 SVGA3dCopyBox *box;
216 unsigned box_count;
217 void *virtual;
218 bool dummy;
219 struct vmw_dma_cmd {
220 SVGA3dCmdHeader header;
221 SVGA3dCmdSurfaceDMA dma;
222 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100223 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000224
225 cmd = container_of(header, struct vmw_dma_cmd, header);
226
227 /* No snooper installed */
228 if (!srf->snooper.image)
229 return;
230
231 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
232 DRM_ERROR("face and mipmap for cursors should never != 0\n");
233 return;
234 }
235
236 if (cmd->header.size < 64) {
237 DRM_ERROR("at least one full copy box must be given\n");
238 return;
239 }
240
241 box = (SVGA3dCopyBox *)&cmd[1];
242 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
243 sizeof(SVGA3dCopyBox);
244
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100245 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000246 box->x != 0 || box->y != 0 || box->z != 0 ||
247 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100248 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000249 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100250 /* TODO handle more dst & src != 0 */
251 /* TODO handle more then one copy */
252 DRM_ERROR("Cant snoop dma request for cursor!\n");
253 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
254 box->srcx, box->srcy, box->srcz,
255 box->x, box->y, box->z,
256 box->w, box->h, box->d, box_count,
257 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000258 return;
259 }
260
261 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
262 kmap_num = (64*64*4) >> PAGE_SHIFT;
263
264 ret = ttm_bo_reserve(bo, true, false, false, 0);
265 if (unlikely(ret != 0)) {
266 DRM_ERROR("reserve failed\n");
267 return;
268 }
269
270 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
271 if (unlikely(ret != 0))
272 goto err_unreserve;
273
274 virtual = ttm_kmap_obj_virtual(&map, &dummy);
275
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100276 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
277 memcpy(srf->snooper.image, virtual, 64*64*4);
278 } else {
279 /* Image is unsigned pointer. */
280 for (i = 0; i < box->h; i++)
281 memcpy(srf->snooper.image + i * 64,
282 virtual + i * cmd->dma.guest.pitch,
283 box->w * 4);
284 }
285
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000286 srf->snooper.age++;
287
288 /* we can't call this function from this function since execbuf has
289 * reserved fifo space.
290 *
291 * if (srf->snooper.crtc)
292 * vmw_ldu_crtc_cursor_update_image(dev_priv,
293 * srf->snooper.image, 64, 64,
294 * du->hotspot_x, du->hotspot_y);
295 */
296
297 ttm_bo_kunmap(&map);
298err_unreserve:
299 ttm_bo_unreserve(bo);
300}
301
302void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
303{
304 struct drm_device *dev = dev_priv->dev;
305 struct vmw_display_unit *du;
306 struct drm_crtc *crtc;
307
308 mutex_lock(&dev->mode_config.mutex);
309
310 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
311 du = vmw_crtc_to_du(crtc);
312 if (!du->cursor_surface ||
313 du->cursor_age == du->cursor_surface->snooper.age)
314 continue;
315
316 du->cursor_age = du->cursor_surface->snooper.age;
317 vmw_cursor_update_image(dev_priv,
318 du->cursor_surface->snooper.image,
319 64, 64, du->hotspot_x, du->hotspot_y);
320 }
321
322 mutex_unlock(&dev->mode_config.mutex);
323}
324
325/*
326 * Generic framebuffer code
327 */
328
329int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
330 struct drm_file *file_priv,
331 unsigned int *handle)
332{
333 if (handle)
334 handle = 0;
335
336 return 0;
337}
338
339/*
340 * Surface framebuffer code
341 */
342
343#define vmw_framebuffer_to_vfbs(x) \
344 container_of(x, struct vmw_framebuffer_surface, base.base)
345
346struct vmw_framebuffer_surface {
347 struct vmw_framebuffer base;
348 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200349 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200350 struct list_head head;
351 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000352};
353
354void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
355{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200356 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000357 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200358 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000359
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200360
361 mutex_lock(&vmaster->fb_surf_mutex);
362 list_del(&vfbs->head);
363 mutex_unlock(&vmaster->fb_surf_mutex);
364
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200365 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000366 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200367 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200368 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000369
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200370 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000371}
372
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200373static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200374 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200375 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200376 unsigned flags, unsigned color,
377 struct drm_clip_rect *clips,
378 unsigned num_clips, int inc)
379{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200380 struct drm_clip_rect *clips_ptr;
381 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
382 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200383 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200384 int i, num_units;
385 int ret = 0; /* silence warning */
386 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200387
388 struct {
389 SVGA3dCmdHeader header;
390 SVGA3dCmdBlitSurfaceToScreen body;
391 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200392 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200393
394
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200395 num_units = 0;
396 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
397 head) {
398 if (crtc->fb != &framebuffer->base)
399 continue;
400 units[num_units++] = vmw_crtc_to_du(crtc);
401 }
402
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200403 BUG_ON(!clips || !num_clips);
404
405 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200406 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200407 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200408 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200409 return -ENOMEM;
410 }
411
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200412 left = clips->x1;
413 right = clips->x2;
414 top = clips->y1;
415 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200416
Jakob Bornecrantzf0c8a652011-11-09 10:25:27 +0100417 /* skip the first clip rect */
418 for (i = 1, clips_ptr = clips + inc;
419 i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200420 left = min_t(int, left, (int)clips_ptr->x1);
421 right = max_t(int, right, (int)clips_ptr->x2);
422 top = min_t(int, top, (int)clips_ptr->y1);
423 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200424 }
425
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200426 /* only need to do this once */
427 memset(cmd, 0, fifo_size);
428 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
429 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
430
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200431 cmd->body.srcRect.left = left;
432 cmd->body.srcRect.right = right;
433 cmd->body.srcRect.top = top;
434 cmd->body.srcRect.bottom = bottom;
435
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200436 clips_ptr = clips;
437 blits = (SVGASignedRect *)&cmd[1];
438 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
439 blits[i].left = clips_ptr->x1 - left;
440 blits[i].right = clips_ptr->x2 - left;
441 blits[i].top = clips_ptr->y1 - top;
442 blits[i].bottom = clips_ptr->y2 - top;
443 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200444
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200445 /* do per unit writing, reuse fifo for each */
446 for (i = 0; i < num_units; i++) {
447 struct vmw_display_unit *unit = units[i];
448 int clip_x1 = left - unit->crtc.x;
449 int clip_y1 = top - unit->crtc.y;
450 int clip_x2 = right - unit->crtc.x;
451 int clip_y2 = bottom - unit->crtc.y;
452
453 /* skip any crtcs that misses the clip region */
454 if (clip_x1 >= unit->crtc.mode.hdisplay ||
455 clip_y1 >= unit->crtc.mode.vdisplay ||
456 clip_x2 <= 0 || clip_y2 <= 0)
457 continue;
458
459 /* need to reset sid as it is changed by execbuf */
460 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
461
462 cmd->body.destScreenId = unit->unit;
463
464 /*
465 * The blit command is a lot more resilient then the
466 * readback command when it comes to clip rects. So its
467 * okay to go out of bounds.
468 */
469
470 cmd->body.destRect.left = clip_x1;
471 cmd->body.destRect.right = clip_x2;
472 cmd->body.destRect.top = clip_y1;
473 cmd->body.destRect.bottom = clip_y2;
474
475
476 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
477 fifo_size, 0, NULL);
478
479 if (unlikely(ret != 0))
480 break;
481 }
482
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200483 kfree(cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200484
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200485 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200486}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000487
488int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200489 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000490 unsigned flags, unsigned color,
491 struct drm_clip_rect *clips,
492 unsigned num_clips)
493{
494 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200495 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000496 struct vmw_framebuffer_surface *vfbs =
497 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000498 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200499 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000500
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200501 if (unlikely(vfbs->master != file_priv->master))
502 return -EINVAL;
503
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200504 /* Require ScreenObject support for 3D */
505 if (!dev_priv->sou_priv)
506 return -EINVAL;
507
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200508 ret = ttm_read_lock(&vmaster->lock, true);
509 if (unlikely(ret != 0))
510 return ret;
511
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000512 if (!num_clips) {
513 num_clips = 1;
514 clips = &norect;
515 norect.x1 = norect.y1 = 0;
516 norect.x2 = framebuffer->width;
517 norect.y2 = framebuffer->height;
518 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
519 num_clips /= 2;
520 inc = 2; /* skip source rects */
521 }
522
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200523 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200524 flags, color,
525 clips, num_clips, inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000526
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200527 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000528 return 0;
529}
530
531static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
532 .destroy = vmw_framebuffer_surface_destroy,
533 .dirty = vmw_framebuffer_surface_dirty,
534 .create_handle = vmw_framebuffer_create_handle,
535};
536
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200537static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200538 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200539 struct vmw_surface *surface,
540 struct vmw_framebuffer **out,
541 const struct drm_mode_fb_cmd
542 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000543
544{
545 struct drm_device *dev = dev_priv->dev;
546 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200547 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200548 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000549 int ret;
550
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200551 /* 3D is only supported on HWv8 hosts which supports screen objects */
552 if (!dev_priv->sou_priv)
553 return -ENOSYS;
554
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200555 /*
556 * Sanity checks.
557 */
558
559 if (unlikely(surface->mip_levels[0] != 1 ||
560 surface->num_sizes != 1 ||
561 surface->sizes[0].width < mode_cmd->width ||
562 surface->sizes[0].height < mode_cmd->height ||
563 surface->sizes[0].depth != 1)) {
564 DRM_ERROR("Incompatible surface dimensions "
565 "for requested mode.\n");
566 return -EINVAL;
567 }
568
569 switch (mode_cmd->depth) {
570 case 32:
571 format = SVGA3D_A8R8G8B8;
572 break;
573 case 24:
574 format = SVGA3D_X8R8G8B8;
575 break;
576 case 16:
577 format = SVGA3D_R5G6B5;
578 break;
579 case 15:
580 format = SVGA3D_A1R5G5B5;
581 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000582 case 8:
583 format = SVGA3D_LUMINANCE8;
584 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200585 default:
586 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
587 return -EINVAL;
588 }
589
590 if (unlikely(format != surface->format)) {
591 DRM_ERROR("Invalid surface format for requested mode.\n");
592 return -EINVAL;
593 }
594
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000595 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
596 if (!vfbs) {
597 ret = -ENOMEM;
598 goto out_err1;
599 }
600
601 ret = drm_framebuffer_init(dev, &vfbs->base.base,
602 &vmw_framebuffer_surface_funcs);
603 if (ret)
604 goto out_err2;
605
606 if (!vmw_surface_reference(surface)) {
607 DRM_ERROR("failed to reference surface %p\n", surface);
608 goto out_err3;
609 }
610
611 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200612 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200613 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200614 vfbs->base.base.depth = mode_cmd->depth;
615 vfbs->base.base.width = mode_cmd->width;
616 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000617 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200618 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200619 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200620
621 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200622 list_add_tail(&vfbs->head, &vmaster->fb_surf);
623 mutex_unlock(&vmaster->fb_surf_mutex);
624
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000625 *out = &vfbs->base;
626
627 return 0;
628
629out_err3:
630 drm_framebuffer_cleanup(&vfbs->base.base);
631out_err2:
632 kfree(vfbs);
633out_err1:
634 return ret;
635}
636
637/*
638 * Dmabuf framebuffer code
639 */
640
641#define vmw_framebuffer_to_vfbd(x) \
642 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
643
644struct vmw_framebuffer_dmabuf {
645 struct vmw_framebuffer base;
646 struct vmw_dma_buffer *buffer;
647};
648
649void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
650{
651 struct vmw_framebuffer_dmabuf *vfbd =
652 vmw_framebuffer_to_vfbd(framebuffer);
653
654 drm_framebuffer_cleanup(framebuffer);
655 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200656 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000657
658 kfree(vfbd);
659}
660
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200661static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
662 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200663 unsigned flags, unsigned color,
664 struct drm_clip_rect *clips,
665 unsigned num_clips, int increment)
666{
667 size_t fifo_size;
668 int i;
669
670 struct {
671 uint32_t header;
672 SVGAFifoCmdUpdate body;
673 } *cmd;
674
675 fifo_size = sizeof(*cmd) * num_clips;
676 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
677 if (unlikely(cmd == NULL)) {
678 DRM_ERROR("Fifo reserve failed.\n");
679 return -ENOMEM;
680 }
681
682 memset(cmd, 0, fifo_size);
683 for (i = 0; i < num_clips; i++, clips += increment) {
684 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
685 cmd[i].body.x = cpu_to_le32(clips->x1);
686 cmd[i].body.y = cpu_to_le32(clips->y1);
687 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
688 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
689 }
690
691 vmw_fifo_commit(dev_priv, fifo_size);
692 return 0;
693}
694
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200695static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
696 struct vmw_private *dev_priv,
697 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200698{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200699 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200700 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200701 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200702
703 struct {
704 uint32_t header;
705 SVGAFifoCmdDefineGMRFB body;
706 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200707
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200708 /* Emulate RGBA support, contrary to svga_reg.h this is not
709 * supported by hosts. This is only a problem if we are reading
710 * this value later and expecting what we uploaded back.
711 */
712 if (depth == 32)
713 depth = 24;
714
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200715 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200716 cmd = kmalloc(fifo_size, GFP_KERNEL);
717 if (unlikely(cmd == NULL)) {
718 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
719 return -ENOMEM;
720 }
721
722 memset(cmd, 0, fifo_size);
723 cmd->header = SVGA_CMD_DEFINE_GMRFB;
724 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200725 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200726 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200727 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200728 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200729 cmd->body.ptr.offset = 0;
730
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200731 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
732 fifo_size, 0, NULL);
733
734 kfree(cmd);
735
736 return ret;
737}
738
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200739static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
740 struct vmw_private *dev_priv,
741 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200742 unsigned flags, unsigned color,
743 struct drm_clip_rect *clips,
744 unsigned num_clips, int increment)
745{
746 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
747 struct drm_clip_rect *clips_ptr;
748 int i, k, num_units, ret;
749 struct drm_crtc *crtc;
750 size_t fifo_size;
751
752 struct {
753 uint32_t header;
754 SVGAFifoCmdBlitGMRFBToScreen body;
755 } *blits;
756
757 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
758 if (unlikely(ret != 0))
759 return ret; /* define_gmrfb prints warnings */
760
761 fifo_size = sizeof(*blits) * num_clips;
762 blits = kmalloc(fifo_size, GFP_KERNEL);
763 if (unlikely(blits == NULL)) {
764 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
765 return -ENOMEM;
766 }
767
768 num_units = 0;
769 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
770 if (crtc->fb != &framebuffer->base)
771 continue;
772 units[num_units++] = vmw_crtc_to_du(crtc);
773 }
774
775 for (k = 0; k < num_units; k++) {
776 struct vmw_display_unit *unit = units[k];
777 int hit_num = 0;
778
779 clips_ptr = clips;
780 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
781 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
782 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
783 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
784 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
785
786 /* skip any crtcs that misses the clip region */
787 if (clip_x1 >= unit->crtc.mode.hdisplay ||
788 clip_y1 >= unit->crtc.mode.vdisplay ||
789 clip_x2 <= 0 || clip_y2 <= 0)
790 continue;
791
792 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
793 blits[hit_num].body.destScreenId = unit->unit;
794 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
795 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
796 blits[hit_num].body.destRect.left = clip_x1;
797 blits[hit_num].body.destRect.top = clip_y1;
798 blits[hit_num].body.destRect.right = clip_x2;
799 blits[hit_num].body.destRect.bottom = clip_y2;
800 hit_num++;
801 }
802
803 /* no clips hit the crtc */
804 if (hit_num == 0)
805 continue;
806
807 fifo_size = sizeof(*blits) * hit_num;
808 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
809 fifo_size, 0, NULL);
810
811 if (unlikely(ret != 0))
812 break;
813 }
814
815 kfree(blits);
816
817 return ret;
818}
819
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000820int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200821 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000822 unsigned flags, unsigned color,
823 struct drm_clip_rect *clips,
824 unsigned num_clips)
825{
826 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200827 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200828 struct vmw_framebuffer_dmabuf *vfbd =
829 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000830 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200831 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000832
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200833 ret = ttm_read_lock(&vmaster->lock, true);
834 if (unlikely(ret != 0))
835 return ret;
836
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100837 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000838 num_clips = 1;
839 clips = &norect;
840 norect.x1 = norect.y1 = 0;
841 norect.x2 = framebuffer->width;
842 norect.y2 = framebuffer->height;
843 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
844 num_clips /= 2;
845 increment = 2;
846 }
847
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200848 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200849 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200850 flags, color,
851 clips, num_clips, increment);
852 } else {
853 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200854 flags, color,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200855 clips, num_clips, increment);
856 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000857
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200858 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200859 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000860}
861
862static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
863 .destroy = vmw_framebuffer_dmabuf_destroy,
864 .dirty = vmw_framebuffer_dmabuf_dirty,
865 .create_handle = vmw_framebuffer_create_handle,
866};
867
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200868/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200869 * Pin the dmabuffer to the start of vram.
870 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000871static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
872{
873 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
874 struct vmw_framebuffer_dmabuf *vfbd =
875 vmw_framebuffer_to_vfbd(&vfb->base);
876 int ret;
877
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200878 /* This code should not be used with screen objects */
879 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200880
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000881 vmw_overlay_pause_all(dev_priv);
882
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200883 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000884
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000885 vmw_overlay_resume_all(dev_priv);
886
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200887 WARN_ON(ret != 0);
888
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000889 return 0;
890}
891
892static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
893{
894 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
895 struct vmw_framebuffer_dmabuf *vfbd =
896 vmw_framebuffer_to_vfbd(&vfb->base);
897
898 if (!vfbd->buffer) {
899 WARN_ON(!vfbd->buffer);
900 return 0;
901 }
902
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200903 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000904}
905
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200906static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
907 struct vmw_dma_buffer *dmabuf,
908 struct vmw_framebuffer **out,
909 const struct drm_mode_fb_cmd
910 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000911
912{
913 struct drm_device *dev = dev_priv->dev;
914 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200915 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000916 int ret;
917
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200918 requested_size = mode_cmd->height * mode_cmd->pitch;
919 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
920 DRM_ERROR("Screen buffer object size is too small "
921 "for requested mode.\n");
922 return -EINVAL;
923 }
924
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200925 /* Limited framebuffer color depth support for screen objects */
926 if (dev_priv->sou_priv) {
927 switch (mode_cmd->depth) {
928 case 32:
929 case 24:
930 /* Only support 32 bpp for 32 and 24 depth fbs */
931 if (mode_cmd->bpp == 32)
932 break;
933
934 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
935 mode_cmd->depth, mode_cmd->bpp);
936 return -EINVAL;
937 case 16:
938 case 15:
939 /* Only support 16 bpp for 16 and 15 depth fbs */
940 if (mode_cmd->bpp == 16)
941 break;
942
943 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
944 mode_cmd->depth, mode_cmd->bpp);
945 return -EINVAL;
946 default:
947 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
948 return -EINVAL;
949 }
950 }
951
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000952 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
953 if (!vfbd) {
954 ret = -ENOMEM;
955 goto out_err1;
956 }
957
958 ret = drm_framebuffer_init(dev, &vfbd->base.base,
959 &vmw_framebuffer_dmabuf_funcs);
960 if (ret)
961 goto out_err2;
962
963 if (!vmw_dmabuf_reference(dmabuf)) {
964 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
965 goto out_err3;
966 }
967
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200968 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200969 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200970 vfbd->base.base.depth = mode_cmd->depth;
971 vfbd->base.base.width = mode_cmd->width;
972 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200973 if (!dev_priv->sou_priv) {
974 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
975 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
976 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200977 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000978 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200979 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000980 *out = &vfbd->base;
981
982 return 0;
983
984out_err3:
985 drm_framebuffer_cleanup(&vfbd->base.base);
986out_err2:
987 kfree(vfbd);
988out_err1:
989 return ret;
990}
991
992/*
993 * Generic Kernel modesetting functions
994 */
995
996static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
997 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800998 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000999{
1000 struct vmw_private *dev_priv = vmw_priv(dev);
1001 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1002 struct vmw_framebuffer *vfb = NULL;
1003 struct vmw_surface *surface = NULL;
1004 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001005 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001006 struct drm_mode_fb_cmd mode_cmd;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001007 u64 required_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001008 int ret;
1009
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001010 mode_cmd.width = mode_cmd2->width;
1011 mode_cmd.height = mode_cmd2->height;
1012 mode_cmd.pitch = mode_cmd2->pitches[0];
1013 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +00001014 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001015 &mode_cmd.bpp);
1016
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001017 /**
1018 * This code should be conditioned on Screen Objects not being used.
1019 * If screen objects are used, we can allocate a GMR to hold the
1020 * requested framebuffer.
1021 */
1022
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001023 required_size = mode_cmd.pitch * mode_cmd.height;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001024 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001025 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001026 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001027 }
1028
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001029 /*
1030 * Take a reference on the user object of the resource
1031 * backing the kms fb. This ensures that user-space handle
1032 * lookups on that resource will always work as long as
1033 * it's registered with a kms framebuffer. This is important,
1034 * since vmw_execbuf_process identifies resources in the
1035 * command stream using user-space handles.
1036 */
1037
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001038 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001039 if (unlikely(user_obj == NULL)) {
1040 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1041 return ERR_PTR(-ENOENT);
1042 }
1043
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001044 /**
1045 * End conditioned code.
1046 */
1047
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001048 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001049 mode_cmd.handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001050 if (ret)
1051 goto try_dmabuf;
1052
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001053 if (!surface->scanout)
1054 goto err_not_scanout;
1055
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001056 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001057 &vfb, &mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001058
1059 /* vmw_user_surface_lookup takes one ref so does new_fb */
1060 vmw_surface_unreference(&surface);
1061
1062 if (ret) {
1063 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001064 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001065 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001066 } else
1067 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001068 return &vfb->base;
1069
1070try_dmabuf:
1071 DRM_INFO("%s: trying buffer\n", __func__);
1072
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001073 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd.handle, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001074 if (ret) {
1075 DRM_ERROR("failed to find buffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001076 return ERR_PTR(-ENOENT);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001077 }
1078
1079 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001080 &mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001081
1082 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
1083 vmw_dmabuf_unreference(&bo);
1084
1085 if (ret) {
1086 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001087 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001088 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001089 } else
1090 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001091
1092 return &vfb->base;
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001093
1094err_not_scanout:
1095 DRM_ERROR("surface not marked as scanout\n");
1096 /* vmw_user_surface_lookup takes one ref */
1097 vmw_surface_unreference(&surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001098 ttm_base_object_unref(&user_obj);
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001099
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001100 return ERR_PTR(-EINVAL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001101}
1102
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001103static struct drm_mode_config_funcs vmw_kms_funcs = {
1104 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001105};
1106
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001107int vmw_kms_present(struct vmw_private *dev_priv,
1108 struct drm_file *file_priv,
1109 struct vmw_framebuffer *vfb,
1110 struct vmw_surface *surface,
1111 uint32_t sid,
1112 int32_t destX, int32_t destY,
1113 struct drm_vmw_rect *clips,
1114 uint32_t num_clips)
1115{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001116 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1117 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001118 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001119 int i, k, num_units;
1120 int ret = 0; /* silence warning */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001121
1122 struct {
1123 SVGA3dCmdHeader header;
1124 SVGA3dCmdBlitSurfaceToScreen body;
1125 } *cmd;
1126 SVGASignedRect *blits;
1127
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001128 num_units = 0;
1129 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1130 if (crtc->fb != &vfb->base)
1131 continue;
1132 units[num_units++] = vmw_crtc_to_du(crtc);
1133 }
1134
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001135 BUG_ON(surface == NULL);
1136 BUG_ON(!clips || !num_clips);
1137
1138 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1139 cmd = kmalloc(fifo_size, GFP_KERNEL);
1140 if (unlikely(cmd == NULL)) {
1141 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1142 return -ENOMEM;
1143 }
1144
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001145 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001146 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001147 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1148 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1149
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001150 cmd->body.srcRect.left = 0;
1151 cmd->body.srcRect.right = surface->sizes[0].width;
1152 cmd->body.srcRect.top = 0;
1153 cmd->body.srcRect.bottom = surface->sizes[0].height;
1154
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001155 blits = (SVGASignedRect *)&cmd[1];
1156 for (i = 0; i < num_clips; i++) {
1157 blits[i].left = clips[i].x;
1158 blits[i].right = clips[i].x + clips[i].w;
1159 blits[i].top = clips[i].y;
1160 blits[i].bottom = clips[i].y + clips[i].h;
1161 }
1162
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001163 for (k = 0; k < num_units; k++) {
1164 struct vmw_display_unit *unit = units[k];
1165 int clip_x1 = destX - unit->crtc.x;
1166 int clip_y1 = destY - unit->crtc.y;
1167 int clip_x2 = clip_x1 + surface->sizes[0].width;
1168 int clip_y2 = clip_y1 + surface->sizes[0].height;
1169
1170 /* skip any crtcs that misses the clip region */
1171 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1172 clip_y1 >= unit->crtc.mode.vdisplay ||
1173 clip_x2 <= 0 || clip_y2 <= 0)
1174 continue;
1175
1176 /* need to reset sid as it is changed by execbuf */
1177 cmd->body.srcImage.sid = sid;
1178
1179 cmd->body.destScreenId = unit->unit;
1180
1181 /*
1182 * The blit command is a lot more resilient then the
1183 * readback command when it comes to clip rects. So its
1184 * okay to go out of bounds.
1185 */
1186
1187 cmd->body.destRect.left = clip_x1;
1188 cmd->body.destRect.right = clip_x2;
1189 cmd->body.destRect.top = clip_y1;
1190 cmd->body.destRect.bottom = clip_y2;
1191
1192 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1193 fifo_size, 0, NULL);
1194
1195 if (unlikely(ret != 0))
1196 break;
1197 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001198
1199 kfree(cmd);
1200
1201 return ret;
1202}
1203
1204int vmw_kms_readback(struct vmw_private *dev_priv,
1205 struct drm_file *file_priv,
1206 struct vmw_framebuffer *vfb,
1207 struct drm_vmw_fence_rep __user *user_fence_rep,
1208 struct drm_vmw_rect *clips,
1209 uint32_t num_clips)
1210{
1211 struct vmw_framebuffer_dmabuf *vfbd =
1212 vmw_framebuffer_to_vfbd(&vfb->base);
1213 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1214 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1215 struct drm_crtc *crtc;
1216 size_t fifo_size;
1217 int i, k, ret, num_units, blits_pos;
1218
1219 struct {
1220 uint32_t header;
1221 SVGAFifoCmdDefineGMRFB body;
1222 } *cmd;
1223 struct {
1224 uint32_t header;
1225 SVGAFifoCmdBlitScreenToGMRFB body;
1226 } *blits;
1227
1228 num_units = 0;
1229 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1230 if (crtc->fb != &vfb->base)
1231 continue;
1232 units[num_units++] = vmw_crtc_to_du(crtc);
1233 }
1234
1235 BUG_ON(dmabuf == NULL);
1236 BUG_ON(!clips || !num_clips);
1237
1238 /* take a safe guess at fifo size */
1239 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1240 cmd = kmalloc(fifo_size, GFP_KERNEL);
1241 if (unlikely(cmd == NULL)) {
1242 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1243 return -ENOMEM;
1244 }
1245
1246 memset(cmd, 0, fifo_size);
1247 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1248 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1249 cmd->body.format.colorDepth = vfb->base.depth;
1250 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001251 cmd->body.bytesPerLine = vfb->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001252 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001253 cmd->body.ptr.offset = 0;
1254
1255 blits = (void *)&cmd[1];
1256 blits_pos = 0;
1257 for (i = 0; i < num_units; i++) {
1258 struct drm_vmw_rect *c = clips;
1259 for (k = 0; k < num_clips; k++, c++) {
1260 /* transform clip coords to crtc origin based coords */
1261 int clip_x1 = c->x - units[i]->crtc.x;
1262 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1263 int clip_y1 = c->y - units[i]->crtc.y;
1264 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1265 int dest_x = c->x;
1266 int dest_y = c->y;
1267
1268 /* compensate for clipping, we negate
1269 * a negative number and add that.
1270 */
1271 if (clip_x1 < 0)
1272 dest_x += -clip_x1;
1273 if (clip_y1 < 0)
1274 dest_y += -clip_y1;
1275
1276 /* clip */
1277 clip_x1 = max(clip_x1, 0);
1278 clip_y1 = max(clip_y1, 0);
1279 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1280 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1281
1282 /* and cull any rects that misses the crtc */
1283 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1284 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1285 clip_x2 <= 0 || clip_y2 <= 0)
1286 continue;
1287
1288 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1289 blits[blits_pos].body.srcScreenId = units[i]->unit;
1290 blits[blits_pos].body.destOrigin.x = dest_x;
1291 blits[blits_pos].body.destOrigin.y = dest_y;
1292
1293 blits[blits_pos].body.srcRect.left = clip_x1;
1294 blits[blits_pos].body.srcRect.top = clip_y1;
1295 blits[blits_pos].body.srcRect.right = clip_x2;
1296 blits[blits_pos].body.srcRect.bottom = clip_y2;
1297 blits_pos++;
1298 }
1299 }
1300 /* reset size here and use calculated exact size from loops */
1301 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1302
1303 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1304 0, user_fence_rep);
1305
1306 kfree(cmd);
1307
1308 return ret;
1309}
1310
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001311int vmw_kms_init(struct vmw_private *dev_priv)
1312{
1313 struct drm_device *dev = dev_priv->dev;
1314 int ret;
1315
1316 drm_mode_config_init(dev);
1317 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001318 dev->mode_config.min_width = 1;
1319 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001320 /* assumed largest fb size */
1321 dev->mode_config.max_width = 8192;
1322 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001323
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001324 ret = vmw_kms_init_screen_object_display(dev_priv);
1325 if (ret) /* Fallback */
1326 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001327
1328 return 0;
1329}
1330
1331int vmw_kms_close(struct vmw_private *dev_priv)
1332{
1333 /*
1334 * Docs says we should take the lock before calling this function
1335 * but since it destroys encoders and our destructor calls
1336 * drm_encoder_cleanup which takes the lock we deadlock.
1337 */
1338 drm_mode_config_cleanup(dev_priv->dev);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001339 if (dev_priv->sou_priv)
1340 vmw_kms_close_screen_object_display(dev_priv);
1341 else
1342 vmw_kms_close_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001343 return 0;
1344}
1345
1346int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1347 struct drm_file *file_priv)
1348{
1349 struct drm_vmw_cursor_bypass_arg *arg = data;
1350 struct vmw_display_unit *du;
1351 struct drm_mode_object *obj;
1352 struct drm_crtc *crtc;
1353 int ret = 0;
1354
1355
1356 mutex_lock(&dev->mode_config.mutex);
1357 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1358
1359 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1360 du = vmw_crtc_to_du(crtc);
1361 du->hotspot_x = arg->xhot;
1362 du->hotspot_y = arg->yhot;
1363 }
1364
1365 mutex_unlock(&dev->mode_config.mutex);
1366 return 0;
1367 }
1368
1369 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1370 if (!obj) {
1371 ret = -EINVAL;
1372 goto out;
1373 }
1374
1375 crtc = obj_to_crtc(obj);
1376 du = vmw_crtc_to_du(crtc);
1377
1378 du->hotspot_x = arg->xhot;
1379 du->hotspot_y = arg->yhot;
1380
1381out:
1382 mutex_unlock(&dev->mode_config.mutex);
1383
1384 return ret;
1385}
1386
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001387int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001388 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001389 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001390{
1391 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1392 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1393 else if (vmw_fifo_have_pitchlock(vmw_priv))
1394 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1395 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1396 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001397 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001398
1399 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1400 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1401 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1402 return -EINVAL;
1403 }
1404
1405 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001406}
1407
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001408int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1409{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001410 struct vmw_vga_topology_state *save;
1411 uint32_t i;
1412
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001413 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1414 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001415 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001416 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1417 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001418 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001419 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001420 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1421 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001422
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001423 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1424 return 0;
1425
1426 vmw_priv->num_displays = vmw_read(vmw_priv,
1427 SVGA_REG_NUM_GUEST_DISPLAYS);
1428
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001429 if (vmw_priv->num_displays == 0)
1430 vmw_priv->num_displays = 1;
1431
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001432 for (i = 0; i < vmw_priv->num_displays; ++i) {
1433 save = &vmw_priv->vga_save[i];
1434 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1435 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1436 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1437 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1438 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1439 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1440 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001441 if (i == 0 && vmw_priv->num_displays == 1 &&
1442 save->width == 0 && save->height == 0) {
1443
1444 /*
1445 * It should be fairly safe to assume that these
1446 * values are uninitialized.
1447 */
1448
1449 save->width = vmw_priv->vga_width - save->pos_x;
1450 save->height = vmw_priv->vga_height - save->pos_y;
1451 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001452 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001453
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001454 return 0;
1455}
1456
1457int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1458{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001459 struct vmw_vga_topology_state *save;
1460 uint32_t i;
1461
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001462 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1463 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001464 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001465 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1466 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1467 vmw_priv->vga_pitchlock);
1468 else if (vmw_fifo_have_pitchlock(vmw_priv))
1469 iowrite32(vmw_priv->vga_pitchlock,
1470 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001471
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001472 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1473 return 0;
1474
1475 for (i = 0; i < vmw_priv->num_displays; ++i) {
1476 save = &vmw_priv->vga_save[i];
1477 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1478 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1479 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1480 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1481 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1482 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1483 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1484 }
1485
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001486 return 0;
1487}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001488
Thomas Hellstrome133e732010-10-05 12:43:04 +02001489bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1490 uint32_t pitch,
1491 uint32_t height)
1492{
1493 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1494}
1495
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001496
1497/**
1498 * Function called by DRM code called with vbl_lock held.
1499 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001500u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1501{
1502 return 0;
1503}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001504
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001505/**
1506 * Function called by DRM code called with vbl_lock held.
1507 */
1508int vmw_enable_vblank(struct drm_device *dev, int crtc)
1509{
1510 return -ENOSYS;
1511}
1512
1513/**
1514 * Function called by DRM code called with vbl_lock held.
1515 */
1516void vmw_disable_vblank(struct drm_device *dev, int crtc)
1517{
1518}
1519
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001520
1521/*
1522 * Small shared kms functions.
1523 */
1524
1525int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1526 struct drm_vmw_rect *rects)
1527{
1528 struct drm_device *dev = dev_priv->dev;
1529 struct vmw_display_unit *du;
1530 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001531
1532 mutex_lock(&dev->mode_config.mutex);
1533
1534#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001535 {
1536 unsigned int i;
1537
1538 DRM_INFO("%s: new layout ", __func__);
1539 for (i = 0; i < num; i++)
1540 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1541 rects[i].w, rects[i].h);
1542 DRM_INFO("\n");
1543 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001544#endif
1545
1546 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1547 du = vmw_connector_to_du(con);
1548 if (num > du->unit) {
1549 du->pref_width = rects[du->unit].w;
1550 du->pref_height = rects[du->unit].h;
1551 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001552 du->gui_x = rects[du->unit].x;
1553 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001554 } else {
1555 du->pref_width = 800;
1556 du->pref_height = 600;
1557 du->pref_active = false;
1558 }
1559 con->status = vmw_du_connector_detect(con, true);
1560 }
1561
1562 mutex_unlock(&dev->mode_config.mutex);
1563
1564 return 0;
1565}
1566
1567void vmw_du_crtc_save(struct drm_crtc *crtc)
1568{
1569}
1570
1571void vmw_du_crtc_restore(struct drm_crtc *crtc)
1572{
1573}
1574
1575void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1576 u16 *r, u16 *g, u16 *b,
1577 uint32_t start, uint32_t size)
1578{
1579 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1580 int i;
1581
1582 for (i = 0; i < size; i++) {
1583 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1584 r[i], g[i], b[i]);
1585 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1586 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1587 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1588 }
1589}
1590
1591void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1592{
1593}
1594
1595void vmw_du_connector_save(struct drm_connector *connector)
1596{
1597}
1598
1599void vmw_du_connector_restore(struct drm_connector *connector)
1600{
1601}
1602
1603enum drm_connector_status
1604vmw_du_connector_detect(struct drm_connector *connector, bool force)
1605{
1606 uint32_t num_displays;
1607 struct drm_device *dev = connector->dev;
1608 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001609 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001610
1611 mutex_lock(&dev_priv->hw_mutex);
1612 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1613 mutex_unlock(&dev_priv->hw_mutex);
1614
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001615 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1616 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001617 connector_status_connected : connector_status_disconnected);
1618}
1619
1620static struct drm_display_mode vmw_kms_connector_builtin[] = {
1621 /* 640x480@60Hz */
1622 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1623 752, 800, 0, 480, 489, 492, 525, 0,
1624 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1625 /* 800x600@60Hz */
1626 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1627 968, 1056, 0, 600, 601, 605, 628, 0,
1628 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1629 /* 1024x768@60Hz */
1630 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1631 1184, 1344, 0, 768, 771, 777, 806, 0,
1632 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1633 /* 1152x864@75Hz */
1634 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1635 1344, 1600, 0, 864, 865, 868, 900, 0,
1636 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1637 /* 1280x768@60Hz */
1638 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1639 1472, 1664, 0, 768, 771, 778, 798, 0,
1640 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1641 /* 1280x800@60Hz */
1642 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1643 1480, 1680, 0, 800, 803, 809, 831, 0,
1644 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1645 /* 1280x960@60Hz */
1646 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1647 1488, 1800, 0, 960, 961, 964, 1000, 0,
1648 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1649 /* 1280x1024@60Hz */
1650 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1651 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1652 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1653 /* 1360x768@60Hz */
1654 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1655 1536, 1792, 0, 768, 771, 777, 795, 0,
1656 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1657 /* 1440x1050@60Hz */
1658 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1659 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1660 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1661 /* 1440x900@60Hz */
1662 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1663 1672, 1904, 0, 900, 903, 909, 934, 0,
1664 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1665 /* 1600x1200@60Hz */
1666 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1667 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1668 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1669 /* 1680x1050@60Hz */
1670 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1671 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1672 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1673 /* 1792x1344@60Hz */
1674 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1675 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1676 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1677 /* 1853x1392@60Hz */
1678 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1679 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1680 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1681 /* 1920x1200@60Hz */
1682 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1683 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1684 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1685 /* 1920x1440@60Hz */
1686 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1687 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1688 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1689 /* 2560x1600@60Hz */
1690 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1691 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1692 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1693 /* Terminate */
1694 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1695};
1696
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001697/**
1698 * vmw_guess_mode_timing - Provide fake timings for a
1699 * 60Hz vrefresh mode.
1700 *
1701 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1702 * members filled in.
1703 */
1704static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1705{
1706 mode->hsync_start = mode->hdisplay + 50;
1707 mode->hsync_end = mode->hsync_start + 50;
1708 mode->htotal = mode->hsync_end + 50;
1709
1710 mode->vsync_start = mode->vdisplay + 50;
1711 mode->vsync_end = mode->vsync_start + 50;
1712 mode->vtotal = mode->vsync_end + 50;
1713
1714 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1715 mode->vrefresh = drm_mode_vrefresh(mode);
1716}
1717
1718
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001719int vmw_du_connector_fill_modes(struct drm_connector *connector,
1720 uint32_t max_width, uint32_t max_height)
1721{
1722 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1723 struct drm_device *dev = connector->dev;
1724 struct vmw_private *dev_priv = vmw_priv(dev);
1725 struct drm_display_mode *mode = NULL;
1726 struct drm_display_mode *bmode;
1727 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1728 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1729 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1730 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1731 };
1732 int i;
1733
1734 /* Add preferred mode */
1735 {
1736 mode = drm_mode_duplicate(dev, &prefmode);
1737 if (!mode)
1738 return 0;
1739 mode->hdisplay = du->pref_width;
1740 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001741 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001742
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001743 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1744 mode->vdisplay)) {
1745 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001746 } else {
1747 drm_mode_destroy(dev, mode);
1748 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001749 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001750
1751 if (du->pref_mode) {
1752 list_del_init(&du->pref_mode->head);
1753 drm_mode_destroy(dev, du->pref_mode);
1754 }
1755
1756 /* mode might be null here, this is intended */
1757 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001758 }
1759
1760 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1761 bmode = &vmw_kms_connector_builtin[i];
1762 if (bmode->hdisplay > max_width ||
1763 bmode->vdisplay > max_height)
1764 continue;
1765
1766 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1767 bmode->vdisplay))
1768 continue;
1769
1770 mode = drm_mode_duplicate(dev, bmode);
1771 if (!mode)
1772 return 0;
1773 mode->vrefresh = drm_mode_vrefresh(mode);
1774
1775 drm_mode_probed_add(connector, mode);
1776 }
1777
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001778 /* Move the prefered mode first, help apps pick the right mode. */
1779 if (du->pref_mode)
1780 list_move(&du->pref_mode->head, &connector->probed_modes);
1781
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001782 drm_mode_connector_list_update(connector);
1783
1784 return 1;
1785}
1786
1787int vmw_du_connector_set_property(struct drm_connector *connector,
1788 struct drm_property *property,
1789 uint64_t val)
1790{
1791 return 0;
1792}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001793
1794
1795int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1796 struct drm_file *file_priv)
1797{
1798 struct vmw_private *dev_priv = vmw_priv(dev);
1799 struct drm_vmw_update_layout_arg *arg =
1800 (struct drm_vmw_update_layout_arg *)data;
1801 struct vmw_master *vmaster = vmw_master(file_priv->master);
1802 void __user *user_rects;
1803 struct drm_vmw_rect *rects;
1804 unsigned rects_size;
1805 int ret;
1806 int i;
1807 struct drm_mode_config *mode_config = &dev->mode_config;
1808
1809 ret = ttm_read_lock(&vmaster->lock, true);
1810 if (unlikely(ret != 0))
1811 return ret;
1812
1813 if (!arg->num_outputs) {
1814 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1815 vmw_du_update_layout(dev_priv, 1, &def_rect);
1816 goto out_unlock;
1817 }
1818
1819 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001820 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1821 GFP_KERNEL);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001822 if (unlikely(!rects)) {
1823 ret = -ENOMEM;
1824 goto out_unlock;
1825 }
1826
1827 user_rects = (void __user *)(unsigned long)arg->rects;
1828 ret = copy_from_user(rects, user_rects, rects_size);
1829 if (unlikely(ret != 0)) {
1830 DRM_ERROR("Failed to get rects.\n");
1831 ret = -EFAULT;
1832 goto out_free;
1833 }
1834
1835 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001836 if (rects[i].x < 0 ||
1837 rects[i].y < 0 ||
1838 rects[i].x + rects[i].w > mode_config->max_width ||
1839 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001840 DRM_ERROR("Invalid GUI layout.\n");
1841 ret = -EINVAL;
1842 goto out_free;
1843 }
1844 }
1845
1846 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1847
1848out_free:
1849 kfree(rects);
1850out_unlock:
1851 ttm_read_unlock(&vmaster->lock);
1852 return ret;
1853}