blob: bee671624a985bca87cb202a2473369bed9bd289 [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
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +010085int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
86 struct vmw_dma_buffer *dmabuf,
87 u32 width, u32 height,
88 u32 hotspotX, u32 hotspotY)
89{
90 struct ttm_bo_kmap_obj map;
91 unsigned long kmap_offset;
92 unsigned long kmap_num;
93 void *virtual;
94 bool dummy;
95 int ret;
96
97 kmap_offset = 0;
98 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
99
100 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
101 if (unlikely(ret != 0)) {
102 DRM_ERROR("reserve failed\n");
103 return -EINVAL;
104 }
105
106 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
107 if (unlikely(ret != 0))
108 goto err_unreserve;
109
110 virtual = ttm_kmap_obj_virtual(&map, &dummy);
111 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
112 hotspotX, hotspotY);
113
114 ttm_bo_kunmap(&map);
115err_unreserve:
116 ttm_bo_unreserve(&dmabuf->base);
117
118 return ret;
119}
120
121
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000122void vmw_cursor_update_position(struct vmw_private *dev_priv,
123 bool show, int x, int y)
124{
125 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
126 uint32_t count;
127
128 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
129 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
130 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
131 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
132 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
133}
134
135int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
136 uint32_t handle, uint32_t width, uint32_t height)
137{
138 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
139 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
140 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
141 struct vmw_surface *surface = NULL;
142 struct vmw_dma_buffer *dmabuf = NULL;
143 int ret;
144
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100145 /* A lot of the code assumes this */
146 if (handle && (width != 64 || height != 64))
147 return -EINVAL;
148
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000149 if (handle) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100150 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
151 handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000152 if (!ret) {
153 if (!surface->snooper.image) {
154 DRM_ERROR("surface not suitable for cursor\n");
Jakob Bornecrantze5c8dbb2011-11-03 21:03:06 +0100155 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000156 return -EINVAL;
157 }
158 } else {
159 ret = vmw_user_dmabuf_lookup(tfile,
160 handle, &dmabuf);
161 if (ret) {
162 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
163 return -EINVAL;
164 }
165 }
166 }
167
168 /* takedown old cursor */
169 if (du->cursor_surface) {
170 du->cursor_surface->snooper.crtc = NULL;
171 vmw_surface_unreference(&du->cursor_surface);
172 }
173 if (du->cursor_dmabuf)
174 vmw_dmabuf_unreference(&du->cursor_dmabuf);
175
176 /* setup new image */
177 if (surface) {
178 /* vmw_user_surface_lookup takes one reference */
179 du->cursor_surface = surface;
180
181 du->cursor_surface->snooper.crtc = crtc;
182 du->cursor_age = du->cursor_surface->snooper.age;
183 vmw_cursor_update_image(dev_priv, surface->snooper.image,
184 64, 64, du->hotspot_x, du->hotspot_y);
185 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000186 /* vmw_user_surface_lookup takes one reference */
187 du->cursor_dmabuf = dmabuf;
188
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100189 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
190 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000191 } else {
192 vmw_cursor_update_position(dev_priv, false, 0, 0);
193 return 0;
194 }
195
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100196 vmw_cursor_update_position(dev_priv, true,
197 du->cursor_x + du->hotspot_x,
198 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000199
200 return 0;
201}
202
203int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
204{
205 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
206 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
207 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
208
209 du->cursor_x = x + crtc->x;
210 du->cursor_y = y + crtc->y;
211
212 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100213 du->cursor_x + du->hotspot_x,
214 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000215
216 return 0;
217}
218
219void vmw_kms_cursor_snoop(struct vmw_surface *srf,
220 struct ttm_object_file *tfile,
221 struct ttm_buffer_object *bo,
222 SVGA3dCmdHeader *header)
223{
224 struct ttm_bo_kmap_obj map;
225 unsigned long kmap_offset;
226 unsigned long kmap_num;
227 SVGA3dCopyBox *box;
228 unsigned box_count;
229 void *virtual;
230 bool dummy;
231 struct vmw_dma_cmd {
232 SVGA3dCmdHeader header;
233 SVGA3dCmdSurfaceDMA dma;
234 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100235 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000236
237 cmd = container_of(header, struct vmw_dma_cmd, header);
238
239 /* No snooper installed */
240 if (!srf->snooper.image)
241 return;
242
243 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
244 DRM_ERROR("face and mipmap for cursors should never != 0\n");
245 return;
246 }
247
248 if (cmd->header.size < 64) {
249 DRM_ERROR("at least one full copy box must be given\n");
250 return;
251 }
252
253 box = (SVGA3dCopyBox *)&cmd[1];
254 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
255 sizeof(SVGA3dCopyBox);
256
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100257 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000258 box->x != 0 || box->y != 0 || box->z != 0 ||
259 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100260 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000261 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100262 /* TODO handle more dst & src != 0 */
263 /* TODO handle more then one copy */
264 DRM_ERROR("Cant snoop dma request for cursor!\n");
265 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
266 box->srcx, box->srcy, box->srcz,
267 box->x, box->y, box->z,
268 box->w, box->h, box->d, box_count,
269 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000270 return;
271 }
272
273 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
274 kmap_num = (64*64*4) >> PAGE_SHIFT;
275
276 ret = ttm_bo_reserve(bo, true, false, false, 0);
277 if (unlikely(ret != 0)) {
278 DRM_ERROR("reserve failed\n");
279 return;
280 }
281
282 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
283 if (unlikely(ret != 0))
284 goto err_unreserve;
285
286 virtual = ttm_kmap_obj_virtual(&map, &dummy);
287
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100288 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
289 memcpy(srf->snooper.image, virtual, 64*64*4);
290 } else {
291 /* Image is unsigned pointer. */
292 for (i = 0; i < box->h; i++)
293 memcpy(srf->snooper.image + i * 64,
294 virtual + i * cmd->dma.guest.pitch,
295 box->w * 4);
296 }
297
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000298 srf->snooper.age++;
299
300 /* we can't call this function from this function since execbuf has
301 * reserved fifo space.
302 *
303 * if (srf->snooper.crtc)
304 * vmw_ldu_crtc_cursor_update_image(dev_priv,
305 * srf->snooper.image, 64, 64,
306 * du->hotspot_x, du->hotspot_y);
307 */
308
309 ttm_bo_kunmap(&map);
310err_unreserve:
311 ttm_bo_unreserve(bo);
312}
313
314void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
315{
316 struct drm_device *dev = dev_priv->dev;
317 struct vmw_display_unit *du;
318 struct drm_crtc *crtc;
319
320 mutex_lock(&dev->mode_config.mutex);
321
322 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
323 du = vmw_crtc_to_du(crtc);
324 if (!du->cursor_surface ||
325 du->cursor_age == du->cursor_surface->snooper.age)
326 continue;
327
328 du->cursor_age = du->cursor_surface->snooper.age;
329 vmw_cursor_update_image(dev_priv,
330 du->cursor_surface->snooper.image,
331 64, 64, du->hotspot_x, du->hotspot_y);
332 }
333
334 mutex_unlock(&dev->mode_config.mutex);
335}
336
337/*
338 * Generic framebuffer code
339 */
340
341int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
342 struct drm_file *file_priv,
343 unsigned int *handle)
344{
345 if (handle)
346 handle = 0;
347
348 return 0;
349}
350
351/*
352 * Surface framebuffer code
353 */
354
355#define vmw_framebuffer_to_vfbs(x) \
356 container_of(x, struct vmw_framebuffer_surface, base.base)
357
358struct vmw_framebuffer_surface {
359 struct vmw_framebuffer base;
360 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200361 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200362 struct list_head head;
363 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000364};
365
366void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
367{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200368 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000369 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200370 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000371
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200372
373 mutex_lock(&vmaster->fb_surf_mutex);
374 list_del(&vfbs->head);
375 mutex_unlock(&vmaster->fb_surf_mutex);
376
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200377 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000378 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200379 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200380 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000381
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200382 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000383}
384
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200385static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200386 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200387 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200388 unsigned flags, unsigned color,
389 struct drm_clip_rect *clips,
390 unsigned num_clips, int inc)
391{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200392 struct drm_clip_rect *clips_ptr;
393 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
394 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200395 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200396 int i, num_units;
397 int ret = 0; /* silence warning */
398 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200399
400 struct {
401 SVGA3dCmdHeader header;
402 SVGA3dCmdBlitSurfaceToScreen body;
403 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200404 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200405
406
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200407 num_units = 0;
408 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
409 head) {
410 if (crtc->fb != &framebuffer->base)
411 continue;
412 units[num_units++] = vmw_crtc_to_du(crtc);
413 }
414
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200415 BUG_ON(!clips || !num_clips);
416
417 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200418 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200419 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200420 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200421 return -ENOMEM;
422 }
423
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200424 left = clips->x1;
425 right = clips->x2;
426 top = clips->y1;
427 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200428
Jakob Bornecrantzf0c8a652011-11-09 10:25:27 +0100429 /* skip the first clip rect */
430 for (i = 1, clips_ptr = clips + inc;
431 i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200432 left = min_t(int, left, (int)clips_ptr->x1);
433 right = max_t(int, right, (int)clips_ptr->x2);
434 top = min_t(int, top, (int)clips_ptr->y1);
435 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200436 }
437
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200438 /* only need to do this once */
439 memset(cmd, 0, fifo_size);
440 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
441 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
442
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200443 cmd->body.srcRect.left = left;
444 cmd->body.srcRect.right = right;
445 cmd->body.srcRect.top = top;
446 cmd->body.srcRect.bottom = bottom;
447
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200448 clips_ptr = clips;
449 blits = (SVGASignedRect *)&cmd[1];
450 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
451 blits[i].left = clips_ptr->x1 - left;
452 blits[i].right = clips_ptr->x2 - left;
453 blits[i].top = clips_ptr->y1 - top;
454 blits[i].bottom = clips_ptr->y2 - top;
455 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200456
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200457 /* do per unit writing, reuse fifo for each */
458 for (i = 0; i < num_units; i++) {
459 struct vmw_display_unit *unit = units[i];
460 int clip_x1 = left - unit->crtc.x;
461 int clip_y1 = top - unit->crtc.y;
462 int clip_x2 = right - unit->crtc.x;
463 int clip_y2 = bottom - unit->crtc.y;
464
465 /* skip any crtcs that misses the clip region */
466 if (clip_x1 >= unit->crtc.mode.hdisplay ||
467 clip_y1 >= unit->crtc.mode.vdisplay ||
468 clip_x2 <= 0 || clip_y2 <= 0)
469 continue;
470
471 /* need to reset sid as it is changed by execbuf */
472 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
473
474 cmd->body.destScreenId = unit->unit;
475
476 /*
477 * The blit command is a lot more resilient then the
478 * readback command when it comes to clip rects. So its
479 * okay to go out of bounds.
480 */
481
482 cmd->body.destRect.left = clip_x1;
483 cmd->body.destRect.right = clip_x2;
484 cmd->body.destRect.top = clip_y1;
485 cmd->body.destRect.bottom = clip_y2;
486
487
488 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
489 fifo_size, 0, NULL);
490
491 if (unlikely(ret != 0))
492 break;
493 }
494
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200495 kfree(cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200496
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200497 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200498}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000499
500int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200501 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000502 unsigned flags, unsigned color,
503 struct drm_clip_rect *clips,
504 unsigned num_clips)
505{
506 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200507 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000508 struct vmw_framebuffer_surface *vfbs =
509 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000510 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200511 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000512
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200513 if (unlikely(vfbs->master != file_priv->master))
514 return -EINVAL;
515
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200516 /* Require ScreenObject support for 3D */
517 if (!dev_priv->sou_priv)
518 return -EINVAL;
519
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200520 ret = ttm_read_lock(&vmaster->lock, true);
521 if (unlikely(ret != 0))
522 return ret;
523
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000524 if (!num_clips) {
525 num_clips = 1;
526 clips = &norect;
527 norect.x1 = norect.y1 = 0;
528 norect.x2 = framebuffer->width;
529 norect.y2 = framebuffer->height;
530 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
531 num_clips /= 2;
532 inc = 2; /* skip source rects */
533 }
534
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200535 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200536 flags, color,
537 clips, num_clips, inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000538
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200539 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000540 return 0;
541}
542
543static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
544 .destroy = vmw_framebuffer_surface_destroy,
545 .dirty = vmw_framebuffer_surface_dirty,
546 .create_handle = vmw_framebuffer_create_handle,
547};
548
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200549static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200550 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200551 struct vmw_surface *surface,
552 struct vmw_framebuffer **out,
553 const struct drm_mode_fb_cmd
554 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000555
556{
557 struct drm_device *dev = dev_priv->dev;
558 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200559 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200560 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000561 int ret;
562
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200563 /* 3D is only supported on HWv8 hosts which supports screen objects */
564 if (!dev_priv->sou_priv)
565 return -ENOSYS;
566
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200567 /*
568 * Sanity checks.
569 */
570
571 if (unlikely(surface->mip_levels[0] != 1 ||
572 surface->num_sizes != 1 ||
573 surface->sizes[0].width < mode_cmd->width ||
574 surface->sizes[0].height < mode_cmd->height ||
575 surface->sizes[0].depth != 1)) {
576 DRM_ERROR("Incompatible surface dimensions "
577 "for requested mode.\n");
578 return -EINVAL;
579 }
580
581 switch (mode_cmd->depth) {
582 case 32:
583 format = SVGA3D_A8R8G8B8;
584 break;
585 case 24:
586 format = SVGA3D_X8R8G8B8;
587 break;
588 case 16:
589 format = SVGA3D_R5G6B5;
590 break;
591 case 15:
592 format = SVGA3D_A1R5G5B5;
593 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000594 case 8:
595 format = SVGA3D_LUMINANCE8;
596 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200597 default:
598 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
599 return -EINVAL;
600 }
601
602 if (unlikely(format != surface->format)) {
603 DRM_ERROR("Invalid surface format for requested mode.\n");
604 return -EINVAL;
605 }
606
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000607 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
608 if (!vfbs) {
609 ret = -ENOMEM;
610 goto out_err1;
611 }
612
613 ret = drm_framebuffer_init(dev, &vfbs->base.base,
614 &vmw_framebuffer_surface_funcs);
615 if (ret)
616 goto out_err2;
617
618 if (!vmw_surface_reference(surface)) {
619 DRM_ERROR("failed to reference surface %p\n", surface);
620 goto out_err3;
621 }
622
623 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200624 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
625 vfbs->base.base.pitch = mode_cmd->pitch;
626 vfbs->base.base.depth = mode_cmd->depth;
627 vfbs->base.base.width = mode_cmd->width;
628 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000629 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200630 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200631 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200632
633 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200634 list_add_tail(&vfbs->head, &vmaster->fb_surf);
635 mutex_unlock(&vmaster->fb_surf_mutex);
636
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000637 *out = &vfbs->base;
638
639 return 0;
640
641out_err3:
642 drm_framebuffer_cleanup(&vfbs->base.base);
643out_err2:
644 kfree(vfbs);
645out_err1:
646 return ret;
647}
648
649/*
650 * Dmabuf framebuffer code
651 */
652
653#define vmw_framebuffer_to_vfbd(x) \
654 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
655
656struct vmw_framebuffer_dmabuf {
657 struct vmw_framebuffer base;
658 struct vmw_dma_buffer *buffer;
659};
660
661void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
662{
663 struct vmw_framebuffer_dmabuf *vfbd =
664 vmw_framebuffer_to_vfbd(framebuffer);
665
666 drm_framebuffer_cleanup(framebuffer);
667 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200668 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000669
670 kfree(vfbd);
671}
672
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200673static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
674 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200675 unsigned flags, unsigned color,
676 struct drm_clip_rect *clips,
677 unsigned num_clips, int increment)
678{
679 size_t fifo_size;
680 int i;
681
682 struct {
683 uint32_t header;
684 SVGAFifoCmdUpdate body;
685 } *cmd;
686
687 fifo_size = sizeof(*cmd) * num_clips;
688 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
689 if (unlikely(cmd == NULL)) {
690 DRM_ERROR("Fifo reserve failed.\n");
691 return -ENOMEM;
692 }
693
694 memset(cmd, 0, fifo_size);
695 for (i = 0; i < num_clips; i++, clips += increment) {
696 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
697 cmd[i].body.x = cpu_to_le32(clips->x1);
698 cmd[i].body.y = cpu_to_le32(clips->y1);
699 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
700 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
701 }
702
703 vmw_fifo_commit(dev_priv, fifo_size);
704 return 0;
705}
706
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200707static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
708 struct vmw_private *dev_priv,
709 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200710{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200711 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200712 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200713 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200714
715 struct {
716 uint32_t header;
717 SVGAFifoCmdDefineGMRFB body;
718 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200719
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200720 /* Emulate RGBA support, contrary to svga_reg.h this is not
721 * supported by hosts. This is only a problem if we are reading
722 * this value later and expecting what we uploaded back.
723 */
724 if (depth == 32)
725 depth = 24;
726
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200727 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200728 cmd = kmalloc(fifo_size, GFP_KERNEL);
729 if (unlikely(cmd == NULL)) {
730 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
731 return -ENOMEM;
732 }
733
734 memset(cmd, 0, fifo_size);
735 cmd->header = SVGA_CMD_DEFINE_GMRFB;
736 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200737 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200738 cmd->body.format.reserved = 0;
739 cmd->body.bytesPerLine = framebuffer->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200740 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200741 cmd->body.ptr.offset = 0;
742
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200743 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
744 fifo_size, 0, NULL);
745
746 kfree(cmd);
747
748 return ret;
749}
750
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200751static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
752 struct vmw_private *dev_priv,
753 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200754 unsigned flags, unsigned color,
755 struct drm_clip_rect *clips,
756 unsigned num_clips, int increment)
757{
758 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
759 struct drm_clip_rect *clips_ptr;
760 int i, k, num_units, ret;
761 struct drm_crtc *crtc;
762 size_t fifo_size;
763
764 struct {
765 uint32_t header;
766 SVGAFifoCmdBlitGMRFBToScreen body;
767 } *blits;
768
769 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
770 if (unlikely(ret != 0))
771 return ret; /* define_gmrfb prints warnings */
772
773 fifo_size = sizeof(*blits) * num_clips;
774 blits = kmalloc(fifo_size, GFP_KERNEL);
775 if (unlikely(blits == NULL)) {
776 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
777 return -ENOMEM;
778 }
779
780 num_units = 0;
781 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
782 if (crtc->fb != &framebuffer->base)
783 continue;
784 units[num_units++] = vmw_crtc_to_du(crtc);
785 }
786
787 for (k = 0; k < num_units; k++) {
788 struct vmw_display_unit *unit = units[k];
789 int hit_num = 0;
790
791 clips_ptr = clips;
792 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
793 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
794 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
795 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
796 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
797
798 /* skip any crtcs that misses the clip region */
799 if (clip_x1 >= unit->crtc.mode.hdisplay ||
800 clip_y1 >= unit->crtc.mode.vdisplay ||
801 clip_x2 <= 0 || clip_y2 <= 0)
802 continue;
803
804 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
805 blits[hit_num].body.destScreenId = unit->unit;
806 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
807 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
808 blits[hit_num].body.destRect.left = clip_x1;
809 blits[hit_num].body.destRect.top = clip_y1;
810 blits[hit_num].body.destRect.right = clip_x2;
811 blits[hit_num].body.destRect.bottom = clip_y2;
812 hit_num++;
813 }
814
815 /* no clips hit the crtc */
816 if (hit_num == 0)
817 continue;
818
819 fifo_size = sizeof(*blits) * hit_num;
820 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
821 fifo_size, 0, NULL);
822
823 if (unlikely(ret != 0))
824 break;
825 }
826
827 kfree(blits);
828
829 return ret;
830}
831
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000832int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200833 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000834 unsigned flags, unsigned color,
835 struct drm_clip_rect *clips,
836 unsigned num_clips)
837{
838 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200839 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200840 struct vmw_framebuffer_dmabuf *vfbd =
841 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000842 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200843 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000844
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200845 ret = ttm_read_lock(&vmaster->lock, true);
846 if (unlikely(ret != 0))
847 return ret;
848
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100849 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000850 num_clips = 1;
851 clips = &norect;
852 norect.x1 = norect.y1 = 0;
853 norect.x2 = framebuffer->width;
854 norect.y2 = framebuffer->height;
855 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
856 num_clips /= 2;
857 increment = 2;
858 }
859
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200860 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200861 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200862 flags, color,
863 clips, num_clips, increment);
864 } else {
865 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200866 flags, color,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200867 clips, num_clips, increment);
868 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000869
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200870 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200871 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000872}
873
874static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
875 .destroy = vmw_framebuffer_dmabuf_destroy,
876 .dirty = vmw_framebuffer_dmabuf_dirty,
877 .create_handle = vmw_framebuffer_create_handle,
878};
879
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200880/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200881 * Pin the dmabuffer to the start of vram.
882 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000883static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
884{
885 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
886 struct vmw_framebuffer_dmabuf *vfbd =
887 vmw_framebuffer_to_vfbd(&vfb->base);
888 int ret;
889
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200890 /* This code should not be used with screen objects */
891 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200892
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000893 vmw_overlay_pause_all(dev_priv);
894
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200895 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000896
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000897 vmw_overlay_resume_all(dev_priv);
898
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200899 WARN_ON(ret != 0);
900
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000901 return 0;
902}
903
904static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
905{
906 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
907 struct vmw_framebuffer_dmabuf *vfbd =
908 vmw_framebuffer_to_vfbd(&vfb->base);
909
910 if (!vfbd->buffer) {
911 WARN_ON(!vfbd->buffer);
912 return 0;
913 }
914
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200915 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000916}
917
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200918static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
919 struct vmw_dma_buffer *dmabuf,
920 struct vmw_framebuffer **out,
921 const struct drm_mode_fb_cmd
922 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000923
924{
925 struct drm_device *dev = dev_priv->dev;
926 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200927 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000928 int ret;
929
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200930 requested_size = mode_cmd->height * mode_cmd->pitch;
931 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
932 DRM_ERROR("Screen buffer object size is too small "
933 "for requested mode.\n");
934 return -EINVAL;
935 }
936
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200937 /* Limited framebuffer color depth support for screen objects */
938 if (dev_priv->sou_priv) {
939 switch (mode_cmd->depth) {
940 case 32:
941 case 24:
942 /* Only support 32 bpp for 32 and 24 depth fbs */
943 if (mode_cmd->bpp == 32)
944 break;
945
946 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
947 mode_cmd->depth, mode_cmd->bpp);
948 return -EINVAL;
949 case 16:
950 case 15:
951 /* Only support 16 bpp for 16 and 15 depth fbs */
952 if (mode_cmd->bpp == 16)
953 break;
954
955 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
956 mode_cmd->depth, mode_cmd->bpp);
957 return -EINVAL;
958 default:
959 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
960 return -EINVAL;
961 }
962 }
963
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000964 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
965 if (!vfbd) {
966 ret = -ENOMEM;
967 goto out_err1;
968 }
969
970 ret = drm_framebuffer_init(dev, &vfbd->base.base,
971 &vmw_framebuffer_dmabuf_funcs);
972 if (ret)
973 goto out_err2;
974
975 if (!vmw_dmabuf_reference(dmabuf)) {
976 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
977 goto out_err3;
978 }
979
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200980 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
981 vfbd->base.base.pitch = mode_cmd->pitch;
982 vfbd->base.base.depth = mode_cmd->depth;
983 vfbd->base.base.width = mode_cmd->width;
984 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200985 if (!dev_priv->sou_priv) {
986 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
987 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
988 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200989 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000990 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200991 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000992 *out = &vfbd->base;
993
994 return 0;
995
996out_err3:
997 drm_framebuffer_cleanup(&vfbd->base.base);
998out_err2:
999 kfree(vfbd);
1000out_err1:
1001 return ret;
1002}
1003
1004/*
1005 * Generic Kernel modesetting functions
1006 */
1007
1008static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1009 struct drm_file *file_priv,
1010 struct drm_mode_fb_cmd *mode_cmd)
1011{
1012 struct vmw_private *dev_priv = vmw_priv(dev);
1013 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1014 struct vmw_framebuffer *vfb = NULL;
1015 struct vmw_surface *surface = NULL;
1016 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001017 struct ttm_base_object *user_obj;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001018 u64 required_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001019 int ret;
1020
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001021 /**
1022 * This code should be conditioned on Screen Objects not being used.
1023 * If screen objects are used, we can allocate a GMR to hold the
1024 * requested framebuffer.
1025 */
1026
1027 required_size = mode_cmd->pitch * mode_cmd->height;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001028 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001029 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001030 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001031 }
1032
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001033 /*
1034 * Take a reference on the user object of the resource
1035 * backing the kms fb. This ensures that user-space handle
1036 * lookups on that resource will always work as long as
1037 * it's registered with a kms framebuffer. This is important,
1038 * since vmw_execbuf_process identifies resources in the
1039 * command stream using user-space handles.
1040 */
1041
1042 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1043 if (unlikely(user_obj == NULL)) {
1044 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1045 return ERR_PTR(-ENOENT);
1046 }
1047
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001048 /**
1049 * End conditioned code.
1050 */
1051
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001052 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
1053 mode_cmd->handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001054 if (ret)
1055 goto try_dmabuf;
1056
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001057 if (!surface->scanout)
1058 goto err_not_scanout;
1059
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001060 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
1061 &vfb, mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001062
1063 /* vmw_user_surface_lookup takes one ref so does new_fb */
1064 vmw_surface_unreference(&surface);
1065
1066 if (ret) {
1067 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001068 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001069 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001070 } else
1071 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001072 return &vfb->base;
1073
1074try_dmabuf:
1075 DRM_INFO("%s: trying buffer\n", __func__);
1076
1077 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
1078 if (ret) {
1079 DRM_ERROR("failed to find buffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001080 return ERR_PTR(-ENOENT);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001081 }
1082
1083 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001084 mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001085
1086 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
1087 vmw_dmabuf_unreference(&bo);
1088
1089 if (ret) {
1090 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001091 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001092 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001093 } else
1094 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001095
1096 return &vfb->base;
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001097
1098err_not_scanout:
1099 DRM_ERROR("surface not marked as scanout\n");
1100 /* vmw_user_surface_lookup takes one ref */
1101 vmw_surface_unreference(&surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001102 ttm_base_object_unref(&user_obj);
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001103
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001104 return ERR_PTR(-EINVAL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001105}
1106
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001107static struct drm_mode_config_funcs vmw_kms_funcs = {
1108 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001109};
1110
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001111int vmw_kms_present(struct vmw_private *dev_priv,
1112 struct drm_file *file_priv,
1113 struct vmw_framebuffer *vfb,
1114 struct vmw_surface *surface,
1115 uint32_t sid,
1116 int32_t destX, int32_t destY,
1117 struct drm_vmw_rect *clips,
1118 uint32_t num_clips)
1119{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001120 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1121 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001122 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001123 int i, k, num_units;
1124 int ret = 0; /* silence warning */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001125
1126 struct {
1127 SVGA3dCmdHeader header;
1128 SVGA3dCmdBlitSurfaceToScreen body;
1129 } *cmd;
1130 SVGASignedRect *blits;
1131
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001132 num_units = 0;
1133 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1134 if (crtc->fb != &vfb->base)
1135 continue;
1136 units[num_units++] = vmw_crtc_to_du(crtc);
1137 }
1138
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001139 BUG_ON(surface == NULL);
1140 BUG_ON(!clips || !num_clips);
1141
1142 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1143 cmd = kmalloc(fifo_size, GFP_KERNEL);
1144 if (unlikely(cmd == NULL)) {
1145 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1146 return -ENOMEM;
1147 }
1148
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001149 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001150 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001151 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1152 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1153
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001154 cmd->body.srcRect.left = 0;
1155 cmd->body.srcRect.right = surface->sizes[0].width;
1156 cmd->body.srcRect.top = 0;
1157 cmd->body.srcRect.bottom = surface->sizes[0].height;
1158
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001159 blits = (SVGASignedRect *)&cmd[1];
1160 for (i = 0; i < num_clips; i++) {
1161 blits[i].left = clips[i].x;
1162 blits[i].right = clips[i].x + clips[i].w;
1163 blits[i].top = clips[i].y;
1164 blits[i].bottom = clips[i].y + clips[i].h;
1165 }
1166
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001167 for (k = 0; k < num_units; k++) {
1168 struct vmw_display_unit *unit = units[k];
1169 int clip_x1 = destX - unit->crtc.x;
1170 int clip_y1 = destY - unit->crtc.y;
1171 int clip_x2 = clip_x1 + surface->sizes[0].width;
1172 int clip_y2 = clip_y1 + surface->sizes[0].height;
1173
1174 /* skip any crtcs that misses the clip region */
1175 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1176 clip_y1 >= unit->crtc.mode.vdisplay ||
1177 clip_x2 <= 0 || clip_y2 <= 0)
1178 continue;
1179
1180 /* need to reset sid as it is changed by execbuf */
1181 cmd->body.srcImage.sid = sid;
1182
1183 cmd->body.destScreenId = unit->unit;
1184
1185 /*
1186 * The blit command is a lot more resilient then the
1187 * readback command when it comes to clip rects. So its
1188 * okay to go out of bounds.
1189 */
1190
1191 cmd->body.destRect.left = clip_x1;
1192 cmd->body.destRect.right = clip_x2;
1193 cmd->body.destRect.top = clip_y1;
1194 cmd->body.destRect.bottom = clip_y2;
1195
1196 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1197 fifo_size, 0, NULL);
1198
1199 if (unlikely(ret != 0))
1200 break;
1201 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001202
1203 kfree(cmd);
1204
1205 return ret;
1206}
1207
1208int vmw_kms_readback(struct vmw_private *dev_priv,
1209 struct drm_file *file_priv,
1210 struct vmw_framebuffer *vfb,
1211 struct drm_vmw_fence_rep __user *user_fence_rep,
1212 struct drm_vmw_rect *clips,
1213 uint32_t num_clips)
1214{
1215 struct vmw_framebuffer_dmabuf *vfbd =
1216 vmw_framebuffer_to_vfbd(&vfb->base);
1217 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1218 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1219 struct drm_crtc *crtc;
1220 size_t fifo_size;
1221 int i, k, ret, num_units, blits_pos;
1222
1223 struct {
1224 uint32_t header;
1225 SVGAFifoCmdDefineGMRFB body;
1226 } *cmd;
1227 struct {
1228 uint32_t header;
1229 SVGAFifoCmdBlitScreenToGMRFB body;
1230 } *blits;
1231
1232 num_units = 0;
1233 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1234 if (crtc->fb != &vfb->base)
1235 continue;
1236 units[num_units++] = vmw_crtc_to_du(crtc);
1237 }
1238
1239 BUG_ON(dmabuf == NULL);
1240 BUG_ON(!clips || !num_clips);
1241
1242 /* take a safe guess at fifo size */
1243 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1244 cmd = kmalloc(fifo_size, GFP_KERNEL);
1245 if (unlikely(cmd == NULL)) {
1246 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1247 return -ENOMEM;
1248 }
1249
1250 memset(cmd, 0, fifo_size);
1251 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1252 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1253 cmd->body.format.colorDepth = vfb->base.depth;
1254 cmd->body.format.reserved = 0;
1255 cmd->body.bytesPerLine = vfb->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001256 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001257 cmd->body.ptr.offset = 0;
1258
1259 blits = (void *)&cmd[1];
1260 blits_pos = 0;
1261 for (i = 0; i < num_units; i++) {
1262 struct drm_vmw_rect *c = clips;
1263 for (k = 0; k < num_clips; k++, c++) {
1264 /* transform clip coords to crtc origin based coords */
1265 int clip_x1 = c->x - units[i]->crtc.x;
1266 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1267 int clip_y1 = c->y - units[i]->crtc.y;
1268 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1269 int dest_x = c->x;
1270 int dest_y = c->y;
1271
1272 /* compensate for clipping, we negate
1273 * a negative number and add that.
1274 */
1275 if (clip_x1 < 0)
1276 dest_x += -clip_x1;
1277 if (clip_y1 < 0)
1278 dest_y += -clip_y1;
1279
1280 /* clip */
1281 clip_x1 = max(clip_x1, 0);
1282 clip_y1 = max(clip_y1, 0);
1283 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1284 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1285
1286 /* and cull any rects that misses the crtc */
1287 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1288 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1289 clip_x2 <= 0 || clip_y2 <= 0)
1290 continue;
1291
1292 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1293 blits[blits_pos].body.srcScreenId = units[i]->unit;
1294 blits[blits_pos].body.destOrigin.x = dest_x;
1295 blits[blits_pos].body.destOrigin.y = dest_y;
1296
1297 blits[blits_pos].body.srcRect.left = clip_x1;
1298 blits[blits_pos].body.srcRect.top = clip_y1;
1299 blits[blits_pos].body.srcRect.right = clip_x2;
1300 blits[blits_pos].body.srcRect.bottom = clip_y2;
1301 blits_pos++;
1302 }
1303 }
1304 /* reset size here and use calculated exact size from loops */
1305 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1306
1307 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1308 0, user_fence_rep);
1309
1310 kfree(cmd);
1311
1312 return ret;
1313}
1314
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001315int vmw_kms_init(struct vmw_private *dev_priv)
1316{
1317 struct drm_device *dev = dev_priv->dev;
1318 int ret;
1319
1320 drm_mode_config_init(dev);
1321 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001322 dev->mode_config.min_width = 1;
1323 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001324 /* assumed largest fb size */
1325 dev->mode_config.max_width = 8192;
1326 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001327
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001328 ret = vmw_kms_init_screen_object_display(dev_priv);
1329 if (ret) /* Fallback */
1330 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001331
1332 return 0;
1333}
1334
1335int vmw_kms_close(struct vmw_private *dev_priv)
1336{
1337 /*
1338 * Docs says we should take the lock before calling this function
1339 * but since it destroys encoders and our destructor calls
1340 * drm_encoder_cleanup which takes the lock we deadlock.
1341 */
1342 drm_mode_config_cleanup(dev_priv->dev);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001343 if (dev_priv->sou_priv)
1344 vmw_kms_close_screen_object_display(dev_priv);
1345 else
1346 vmw_kms_close_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001347 return 0;
1348}
1349
1350int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1351 struct drm_file *file_priv)
1352{
1353 struct drm_vmw_cursor_bypass_arg *arg = data;
1354 struct vmw_display_unit *du;
1355 struct drm_mode_object *obj;
1356 struct drm_crtc *crtc;
1357 int ret = 0;
1358
1359
1360 mutex_lock(&dev->mode_config.mutex);
1361 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1362
1363 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1364 du = vmw_crtc_to_du(crtc);
1365 du->hotspot_x = arg->xhot;
1366 du->hotspot_y = arg->yhot;
1367 }
1368
1369 mutex_unlock(&dev->mode_config.mutex);
1370 return 0;
1371 }
1372
1373 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1374 if (!obj) {
1375 ret = -EINVAL;
1376 goto out;
1377 }
1378
1379 crtc = obj_to_crtc(obj);
1380 du = vmw_crtc_to_du(crtc);
1381
1382 du->hotspot_x = arg->xhot;
1383 du->hotspot_y = arg->yhot;
1384
1385out:
1386 mutex_unlock(&dev->mode_config.mutex);
1387
1388 return ret;
1389}
1390
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001391int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001392 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001393 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001394{
1395 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1396 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1397 else if (vmw_fifo_have_pitchlock(vmw_priv))
1398 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1399 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1400 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001401 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001402
1403 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1404 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1405 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1406 return -EINVAL;
1407 }
1408
1409 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001410}
1411
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001412int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1413{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001414 struct vmw_vga_topology_state *save;
1415 uint32_t i;
1416
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001417 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1418 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001419 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001420 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1421 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001422 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001423 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001424 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1425 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001426
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001427 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1428 return 0;
1429
1430 vmw_priv->num_displays = vmw_read(vmw_priv,
1431 SVGA_REG_NUM_GUEST_DISPLAYS);
1432
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001433 if (vmw_priv->num_displays == 0)
1434 vmw_priv->num_displays = 1;
1435
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001436 for (i = 0; i < vmw_priv->num_displays; ++i) {
1437 save = &vmw_priv->vga_save[i];
1438 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1439 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1440 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1441 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1442 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1443 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1444 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001445 if (i == 0 && vmw_priv->num_displays == 1 &&
1446 save->width == 0 && save->height == 0) {
1447
1448 /*
1449 * It should be fairly safe to assume that these
1450 * values are uninitialized.
1451 */
1452
1453 save->width = vmw_priv->vga_width - save->pos_x;
1454 save->height = vmw_priv->vga_height - save->pos_y;
1455 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001456 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001457
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001458 return 0;
1459}
1460
1461int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1462{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001463 struct vmw_vga_topology_state *save;
1464 uint32_t i;
1465
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001466 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1467 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001468 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001469 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1470 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1471 vmw_priv->vga_pitchlock);
1472 else if (vmw_fifo_have_pitchlock(vmw_priv))
1473 iowrite32(vmw_priv->vga_pitchlock,
1474 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001475
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001476 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1477 return 0;
1478
1479 for (i = 0; i < vmw_priv->num_displays; ++i) {
1480 save = &vmw_priv->vga_save[i];
1481 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1482 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1483 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1484 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1485 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1486 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1487 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1488 }
1489
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001490 return 0;
1491}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001492
Thomas Hellstrome133e732010-10-05 12:43:04 +02001493bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1494 uint32_t pitch,
1495 uint32_t height)
1496{
1497 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1498}
1499
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001500
1501/**
1502 * Function called by DRM code called with vbl_lock held.
1503 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001504u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1505{
1506 return 0;
1507}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001508
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001509/**
1510 * Function called by DRM code called with vbl_lock held.
1511 */
1512int vmw_enable_vblank(struct drm_device *dev, int crtc)
1513{
1514 return -ENOSYS;
1515}
1516
1517/**
1518 * Function called by DRM code called with vbl_lock held.
1519 */
1520void vmw_disable_vblank(struct drm_device *dev, int crtc)
1521{
1522}
1523
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001524
1525/*
1526 * Small shared kms functions.
1527 */
1528
1529int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1530 struct drm_vmw_rect *rects)
1531{
1532 struct drm_device *dev = dev_priv->dev;
1533 struct vmw_display_unit *du;
1534 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001535
1536 mutex_lock(&dev->mode_config.mutex);
1537
1538#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001539 {
1540 unsigned int i;
1541
1542 DRM_INFO("%s: new layout ", __func__);
1543 for (i = 0; i < num; i++)
1544 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1545 rects[i].w, rects[i].h);
1546 DRM_INFO("\n");
1547 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001548#endif
1549
1550 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1551 du = vmw_connector_to_du(con);
1552 if (num > du->unit) {
1553 du->pref_width = rects[du->unit].w;
1554 du->pref_height = rects[du->unit].h;
1555 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001556 du->gui_x = rects[du->unit].x;
1557 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001558 } else {
1559 du->pref_width = 800;
1560 du->pref_height = 600;
1561 du->pref_active = false;
1562 }
1563 con->status = vmw_du_connector_detect(con, true);
1564 }
1565
1566 mutex_unlock(&dev->mode_config.mutex);
1567
1568 return 0;
1569}
1570
1571void vmw_du_crtc_save(struct drm_crtc *crtc)
1572{
1573}
1574
1575void vmw_du_crtc_restore(struct drm_crtc *crtc)
1576{
1577}
1578
1579void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1580 u16 *r, u16 *g, u16 *b,
1581 uint32_t start, uint32_t size)
1582{
1583 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1584 int i;
1585
1586 for (i = 0; i < size; i++) {
1587 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1588 r[i], g[i], b[i]);
1589 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1590 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1591 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1592 }
1593}
1594
1595void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1596{
1597}
1598
1599void vmw_du_connector_save(struct drm_connector *connector)
1600{
1601}
1602
1603void vmw_du_connector_restore(struct drm_connector *connector)
1604{
1605}
1606
1607enum drm_connector_status
1608vmw_du_connector_detect(struct drm_connector *connector, bool force)
1609{
1610 uint32_t num_displays;
1611 struct drm_device *dev = connector->dev;
1612 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001613 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001614
1615 mutex_lock(&dev_priv->hw_mutex);
1616 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1617 mutex_unlock(&dev_priv->hw_mutex);
1618
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001619 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1620 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001621 connector_status_connected : connector_status_disconnected);
1622}
1623
1624static struct drm_display_mode vmw_kms_connector_builtin[] = {
1625 /* 640x480@60Hz */
1626 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1627 752, 800, 0, 480, 489, 492, 525, 0,
1628 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1629 /* 800x600@60Hz */
1630 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1631 968, 1056, 0, 600, 601, 605, 628, 0,
1632 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1633 /* 1024x768@60Hz */
1634 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1635 1184, 1344, 0, 768, 771, 777, 806, 0,
1636 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1637 /* 1152x864@75Hz */
1638 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1639 1344, 1600, 0, 864, 865, 868, 900, 0,
1640 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1641 /* 1280x768@60Hz */
1642 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1643 1472, 1664, 0, 768, 771, 778, 798, 0,
1644 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1645 /* 1280x800@60Hz */
1646 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1647 1480, 1680, 0, 800, 803, 809, 831, 0,
1648 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1649 /* 1280x960@60Hz */
1650 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1651 1488, 1800, 0, 960, 961, 964, 1000, 0,
1652 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1653 /* 1280x1024@60Hz */
1654 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1655 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1656 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1657 /* 1360x768@60Hz */
1658 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1659 1536, 1792, 0, 768, 771, 777, 795, 0,
1660 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1661 /* 1440x1050@60Hz */
1662 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1663 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1664 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1665 /* 1440x900@60Hz */
1666 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1667 1672, 1904, 0, 900, 903, 909, 934, 0,
1668 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1669 /* 1600x1200@60Hz */
1670 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1671 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1672 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1673 /* 1680x1050@60Hz */
1674 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1675 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1676 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1677 /* 1792x1344@60Hz */
1678 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1679 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1680 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1681 /* 1853x1392@60Hz */
1682 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1683 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1684 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1685 /* 1920x1200@60Hz */
1686 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1687 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1688 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1689 /* 1920x1440@60Hz */
1690 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1691 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1692 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1693 /* 2560x1600@60Hz */
1694 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1695 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1696 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1697 /* Terminate */
1698 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1699};
1700
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001701/**
1702 * vmw_guess_mode_timing - Provide fake timings for a
1703 * 60Hz vrefresh mode.
1704 *
1705 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1706 * members filled in.
1707 */
1708static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1709{
1710 mode->hsync_start = mode->hdisplay + 50;
1711 mode->hsync_end = mode->hsync_start + 50;
1712 mode->htotal = mode->hsync_end + 50;
1713
1714 mode->vsync_start = mode->vdisplay + 50;
1715 mode->vsync_end = mode->vsync_start + 50;
1716 mode->vtotal = mode->vsync_end + 50;
1717
1718 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1719 mode->vrefresh = drm_mode_vrefresh(mode);
1720}
1721
1722
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001723int vmw_du_connector_fill_modes(struct drm_connector *connector,
1724 uint32_t max_width, uint32_t max_height)
1725{
1726 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1727 struct drm_device *dev = connector->dev;
1728 struct vmw_private *dev_priv = vmw_priv(dev);
1729 struct drm_display_mode *mode = NULL;
1730 struct drm_display_mode *bmode;
1731 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1732 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1733 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1734 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1735 };
1736 int i;
1737
1738 /* Add preferred mode */
1739 {
1740 mode = drm_mode_duplicate(dev, &prefmode);
1741 if (!mode)
1742 return 0;
1743 mode->hdisplay = du->pref_width;
1744 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001745 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001746
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001747 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1748 mode->vdisplay)) {
1749 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001750 } else {
1751 drm_mode_destroy(dev, mode);
1752 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001753 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001754
1755 if (du->pref_mode) {
1756 list_del_init(&du->pref_mode->head);
1757 drm_mode_destroy(dev, du->pref_mode);
1758 }
1759
1760 /* mode might be null here, this is intended */
1761 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001762 }
1763
1764 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1765 bmode = &vmw_kms_connector_builtin[i];
1766 if (bmode->hdisplay > max_width ||
1767 bmode->vdisplay > max_height)
1768 continue;
1769
1770 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1771 bmode->vdisplay))
1772 continue;
1773
1774 mode = drm_mode_duplicate(dev, bmode);
1775 if (!mode)
1776 return 0;
1777 mode->vrefresh = drm_mode_vrefresh(mode);
1778
1779 drm_mode_probed_add(connector, mode);
1780 }
1781
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001782 /* Move the prefered mode first, help apps pick the right mode. */
1783 if (du->pref_mode)
1784 list_move(&du->pref_mode->head, &connector->probed_modes);
1785
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001786 drm_mode_connector_list_update(connector);
1787
1788 return 1;
1789}
1790
1791int vmw_du_connector_set_property(struct drm_connector *connector,
1792 struct drm_property *property,
1793 uint64_t val)
1794{
1795 return 0;
1796}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001797
1798
1799int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1800 struct drm_file *file_priv)
1801{
1802 struct vmw_private *dev_priv = vmw_priv(dev);
1803 struct drm_vmw_update_layout_arg *arg =
1804 (struct drm_vmw_update_layout_arg *)data;
1805 struct vmw_master *vmaster = vmw_master(file_priv->master);
1806 void __user *user_rects;
1807 struct drm_vmw_rect *rects;
1808 unsigned rects_size;
1809 int ret;
1810 int i;
1811 struct drm_mode_config *mode_config = &dev->mode_config;
1812
1813 ret = ttm_read_lock(&vmaster->lock, true);
1814 if (unlikely(ret != 0))
1815 return ret;
1816
1817 if (!arg->num_outputs) {
1818 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1819 vmw_du_update_layout(dev_priv, 1, &def_rect);
1820 goto out_unlock;
1821 }
1822
1823 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001824 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1825 GFP_KERNEL);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001826 if (unlikely(!rects)) {
1827 ret = -ENOMEM;
1828 goto out_unlock;
1829 }
1830
1831 user_rects = (void __user *)(unsigned long)arg->rects;
1832 ret = copy_from_user(rects, user_rects, rects_size);
1833 if (unlikely(ret != 0)) {
1834 DRM_ERROR("Failed to get rects.\n");
1835 ret = -EFAULT;
1836 goto out_free;
1837 }
1838
1839 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001840 if (rects[i].x < 0 ||
1841 rects[i].y < 0 ||
1842 rects[i].x + rects[i].w > mode_config->max_width ||
1843 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001844 DRM_ERROR("Invalid GUI layout.\n");
1845 ret = -EINVAL;
1846 goto out_free;
1847 }
1848 }
1849
1850 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1851
1852out_free:
1853 kfree(rects);
1854out_unlock:
1855 ttm_read_unlock(&vmaster->lock);
1856 return ret;
1857}