blob: 8de24836edb1a330a5d549d585c456c23f7a31b2 [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
108 if (handle) {
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +0100109 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
110 handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000111 if (!ret) {
112 if (!surface->snooper.image) {
113 DRM_ERROR("surface not suitable for cursor\n");
Jakob Bornecrantze5c8dbb2011-11-03 21:03:06 +0100114 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000115 return -EINVAL;
116 }
117 } else {
118 ret = vmw_user_dmabuf_lookup(tfile,
119 handle, &dmabuf);
120 if (ret) {
121 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
122 return -EINVAL;
123 }
124 }
125 }
126
127 /* takedown old cursor */
128 if (du->cursor_surface) {
129 du->cursor_surface->snooper.crtc = NULL;
130 vmw_surface_unreference(&du->cursor_surface);
131 }
132 if (du->cursor_dmabuf)
133 vmw_dmabuf_unreference(&du->cursor_dmabuf);
134
135 /* setup new image */
136 if (surface) {
137 /* vmw_user_surface_lookup takes one reference */
138 du->cursor_surface = surface;
139
140 du->cursor_surface->snooper.crtc = crtc;
141 du->cursor_age = du->cursor_surface->snooper.age;
142 vmw_cursor_update_image(dev_priv, surface->snooper.image,
143 64, 64, du->hotspot_x, du->hotspot_y);
144 } else if (dmabuf) {
145 struct ttm_bo_kmap_obj map;
146 unsigned long kmap_offset;
147 unsigned long kmap_num;
148 void *virtual;
149 bool dummy;
150
151 /* vmw_user_surface_lookup takes one reference */
152 du->cursor_dmabuf = dmabuf;
153
154 kmap_offset = 0;
155 kmap_num = (64*64*4) >> PAGE_SHIFT;
156
157 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
158 if (unlikely(ret != 0)) {
159 DRM_ERROR("reserve failed\n");
160 return -EINVAL;
161 }
162
163 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
164 if (unlikely(ret != 0))
165 goto err_unreserve;
166
167 virtual = ttm_kmap_obj_virtual(&map, &dummy);
168 vmw_cursor_update_image(dev_priv, virtual, 64, 64,
169 du->hotspot_x, du->hotspot_y);
170
171 ttm_bo_kunmap(&map);
172err_unreserve:
173 ttm_bo_unreserve(&dmabuf->base);
174
175 } else {
176 vmw_cursor_update_position(dev_priv, false, 0, 0);
177 return 0;
178 }
179
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100180 vmw_cursor_update_position(dev_priv, true,
181 du->cursor_x + du->hotspot_x,
182 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000183
184 return 0;
185}
186
187int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
188{
189 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
190 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
191 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
192
193 du->cursor_x = x + crtc->x;
194 du->cursor_y = y + crtc->y;
195
196 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100197 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
203void vmw_kms_cursor_snoop(struct vmw_surface *srf,
204 struct ttm_object_file *tfile,
205 struct ttm_buffer_object *bo,
206 SVGA3dCmdHeader *header)
207{
208 struct ttm_bo_kmap_obj map;
209 unsigned long kmap_offset;
210 unsigned long kmap_num;
211 SVGA3dCopyBox *box;
212 unsigned box_count;
213 void *virtual;
214 bool dummy;
215 struct vmw_dma_cmd {
216 SVGA3dCmdHeader header;
217 SVGA3dCmdSurfaceDMA dma;
218 } *cmd;
219 int ret;
220
221 cmd = container_of(header, struct vmw_dma_cmd, header);
222
223 /* No snooper installed */
224 if (!srf->snooper.image)
225 return;
226
227 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
228 DRM_ERROR("face and mipmap for cursors should never != 0\n");
229 return;
230 }
231
232 if (cmd->header.size < 64) {
233 DRM_ERROR("at least one full copy box must be given\n");
234 return;
235 }
236
237 box = (SVGA3dCopyBox *)&cmd[1];
238 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
239 sizeof(SVGA3dCopyBox);
240
241 if (cmd->dma.guest.pitch != (64 * 4) ||
242 cmd->dma.guest.ptr.offset % PAGE_SIZE ||
243 box->x != 0 || box->y != 0 || box->z != 0 ||
244 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
245 box->w != 64 || box->h != 64 || box->d != 1 ||
246 box_count != 1) {
247 /* TODO handle none page aligned offsets */
248 /* TODO handle partial uploads and pitch != 256 */
249 /* TODO handle more then one copy (size != 64) */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300250 DRM_ERROR("lazy programmer, can't handle weird stuff\n");
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000251 return;
252 }
253
254 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
255 kmap_num = (64*64*4) >> PAGE_SHIFT;
256
257 ret = ttm_bo_reserve(bo, true, false, false, 0);
258 if (unlikely(ret != 0)) {
259 DRM_ERROR("reserve failed\n");
260 return;
261 }
262
263 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
264 if (unlikely(ret != 0))
265 goto err_unreserve;
266
267 virtual = ttm_kmap_obj_virtual(&map, &dummy);
268
269 memcpy(srf->snooper.image, virtual, 64*64*4);
270 srf->snooper.age++;
271
272 /* we can't call this function from this function since execbuf has
273 * reserved fifo space.
274 *
275 * if (srf->snooper.crtc)
276 * vmw_ldu_crtc_cursor_update_image(dev_priv,
277 * srf->snooper.image, 64, 64,
278 * du->hotspot_x, du->hotspot_y);
279 */
280
281 ttm_bo_kunmap(&map);
282err_unreserve:
283 ttm_bo_unreserve(bo);
284}
285
286void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
287{
288 struct drm_device *dev = dev_priv->dev;
289 struct vmw_display_unit *du;
290 struct drm_crtc *crtc;
291
292 mutex_lock(&dev->mode_config.mutex);
293
294 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
295 du = vmw_crtc_to_du(crtc);
296 if (!du->cursor_surface ||
297 du->cursor_age == du->cursor_surface->snooper.age)
298 continue;
299
300 du->cursor_age = du->cursor_surface->snooper.age;
301 vmw_cursor_update_image(dev_priv,
302 du->cursor_surface->snooper.image,
303 64, 64, du->hotspot_x, du->hotspot_y);
304 }
305
306 mutex_unlock(&dev->mode_config.mutex);
307}
308
309/*
310 * Generic framebuffer code
311 */
312
313int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
314 struct drm_file *file_priv,
315 unsigned int *handle)
316{
317 if (handle)
318 handle = 0;
319
320 return 0;
321}
322
323/*
324 * Surface framebuffer code
325 */
326
327#define vmw_framebuffer_to_vfbs(x) \
328 container_of(x, struct vmw_framebuffer_surface, base.base)
329
330struct vmw_framebuffer_surface {
331 struct vmw_framebuffer base;
332 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200333 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200334 struct list_head head;
335 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000336};
337
338void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
339{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200340 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000341 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200342 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000343
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200344
345 mutex_lock(&vmaster->fb_surf_mutex);
346 list_del(&vfbs->head);
347 mutex_unlock(&vmaster->fb_surf_mutex);
348
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200349 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000350 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200351 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200352 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000353
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200354 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000355}
356
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200357static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200358 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200359 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200360 unsigned flags, unsigned color,
361 struct drm_clip_rect *clips,
362 unsigned num_clips, int inc)
363{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200364 struct drm_clip_rect *clips_ptr;
365 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
366 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200367 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200368 int i, num_units;
369 int ret = 0; /* silence warning */
370 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200371
372 struct {
373 SVGA3dCmdHeader header;
374 SVGA3dCmdBlitSurfaceToScreen body;
375 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200376 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200377
378
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200379 num_units = 0;
380 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
381 head) {
382 if (crtc->fb != &framebuffer->base)
383 continue;
384 units[num_units++] = vmw_crtc_to_du(crtc);
385 }
386
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200387 BUG_ON(!clips || !num_clips);
388
389 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200390 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200391 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200392 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200393 return -ENOMEM;
394 }
395
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200396 left = clips->x1;
397 right = clips->x2;
398 top = clips->y1;
399 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200400
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200401 clips_ptr = clips;
402 for (i = 1; i < num_clips; i++, clips_ptr += inc) {
403 left = min_t(int, left, (int)clips_ptr->x1);
404 right = max_t(int, right, (int)clips_ptr->x2);
405 top = min_t(int, top, (int)clips_ptr->y1);
406 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200407 }
408
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200409 /* only need to do this once */
410 memset(cmd, 0, fifo_size);
411 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
412 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
413
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200414 cmd->body.srcRect.left = left;
415 cmd->body.srcRect.right = right;
416 cmd->body.srcRect.top = top;
417 cmd->body.srcRect.bottom = bottom;
418
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200419 clips_ptr = clips;
420 blits = (SVGASignedRect *)&cmd[1];
421 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
422 blits[i].left = clips_ptr->x1 - left;
423 blits[i].right = clips_ptr->x2 - left;
424 blits[i].top = clips_ptr->y1 - top;
425 blits[i].bottom = clips_ptr->y2 - top;
426 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200427
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200428 /* do per unit writing, reuse fifo for each */
429 for (i = 0; i < num_units; i++) {
430 struct vmw_display_unit *unit = units[i];
431 int clip_x1 = left - unit->crtc.x;
432 int clip_y1 = top - unit->crtc.y;
433 int clip_x2 = right - unit->crtc.x;
434 int clip_y2 = bottom - unit->crtc.y;
435
436 /* skip any crtcs that misses the clip region */
437 if (clip_x1 >= unit->crtc.mode.hdisplay ||
438 clip_y1 >= unit->crtc.mode.vdisplay ||
439 clip_x2 <= 0 || clip_y2 <= 0)
440 continue;
441
442 /* need to reset sid as it is changed by execbuf */
443 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
444
445 cmd->body.destScreenId = unit->unit;
446
447 /*
448 * The blit command is a lot more resilient then the
449 * readback command when it comes to clip rects. So its
450 * okay to go out of bounds.
451 */
452
453 cmd->body.destRect.left = clip_x1;
454 cmd->body.destRect.right = clip_x2;
455 cmd->body.destRect.top = clip_y1;
456 cmd->body.destRect.bottom = clip_y2;
457
458
459 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
460 fifo_size, 0, NULL);
461
462 if (unlikely(ret != 0))
463 break;
464 }
465
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200466 kfree(cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200467
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200468 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200469}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000470
471int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200472 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000473 unsigned flags, unsigned color,
474 struct drm_clip_rect *clips,
475 unsigned num_clips)
476{
477 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200478 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000479 struct vmw_framebuffer_surface *vfbs =
480 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000481 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200482 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000483
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200484 if (unlikely(vfbs->master != file_priv->master))
485 return -EINVAL;
486
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200487 /* Require ScreenObject support for 3D */
488 if (!dev_priv->sou_priv)
489 return -EINVAL;
490
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200491 ret = ttm_read_lock(&vmaster->lock, true);
492 if (unlikely(ret != 0))
493 return ret;
494
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000495 if (!num_clips) {
496 num_clips = 1;
497 clips = &norect;
498 norect.x1 = norect.y1 = 0;
499 norect.x2 = framebuffer->width;
500 norect.y2 = framebuffer->height;
501 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
502 num_clips /= 2;
503 inc = 2; /* skip source rects */
504 }
505
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200506 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200507 flags, color,
508 clips, num_clips, inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000509
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200510 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000511 return 0;
512}
513
514static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
515 .destroy = vmw_framebuffer_surface_destroy,
516 .dirty = vmw_framebuffer_surface_dirty,
517 .create_handle = vmw_framebuffer_create_handle,
518};
519
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200520static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200521 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200522 struct vmw_surface *surface,
523 struct vmw_framebuffer **out,
524 const struct drm_mode_fb_cmd
525 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000526
527{
528 struct drm_device *dev = dev_priv->dev;
529 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200530 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200531 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000532 int ret;
533
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200534 /* 3D is only supported on HWv8 hosts which supports screen objects */
535 if (!dev_priv->sou_priv)
536 return -ENOSYS;
537
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200538 /*
539 * Sanity checks.
540 */
541
542 if (unlikely(surface->mip_levels[0] != 1 ||
543 surface->num_sizes != 1 ||
544 surface->sizes[0].width < mode_cmd->width ||
545 surface->sizes[0].height < mode_cmd->height ||
546 surface->sizes[0].depth != 1)) {
547 DRM_ERROR("Incompatible surface dimensions "
548 "for requested mode.\n");
549 return -EINVAL;
550 }
551
552 switch (mode_cmd->depth) {
553 case 32:
554 format = SVGA3D_A8R8G8B8;
555 break;
556 case 24:
557 format = SVGA3D_X8R8G8B8;
558 break;
559 case 16:
560 format = SVGA3D_R5G6B5;
561 break;
562 case 15:
563 format = SVGA3D_A1R5G5B5;
564 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000565 case 8:
566 format = SVGA3D_LUMINANCE8;
567 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200568 default:
569 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
570 return -EINVAL;
571 }
572
573 if (unlikely(format != surface->format)) {
574 DRM_ERROR("Invalid surface format for requested mode.\n");
575 return -EINVAL;
576 }
577
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000578 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
579 if (!vfbs) {
580 ret = -ENOMEM;
581 goto out_err1;
582 }
583
584 ret = drm_framebuffer_init(dev, &vfbs->base.base,
585 &vmw_framebuffer_surface_funcs);
586 if (ret)
587 goto out_err2;
588
589 if (!vmw_surface_reference(surface)) {
590 DRM_ERROR("failed to reference surface %p\n", surface);
591 goto out_err3;
592 }
593
594 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200595 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
596 vfbs->base.base.pitch = mode_cmd->pitch;
597 vfbs->base.base.depth = mode_cmd->depth;
598 vfbs->base.base.width = mode_cmd->width;
599 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000600 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200601 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200602 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200603
604 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200605 list_add_tail(&vfbs->head, &vmaster->fb_surf);
606 mutex_unlock(&vmaster->fb_surf_mutex);
607
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000608 *out = &vfbs->base;
609
610 return 0;
611
612out_err3:
613 drm_framebuffer_cleanup(&vfbs->base.base);
614out_err2:
615 kfree(vfbs);
616out_err1:
617 return ret;
618}
619
620/*
621 * Dmabuf framebuffer code
622 */
623
624#define vmw_framebuffer_to_vfbd(x) \
625 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
626
627struct vmw_framebuffer_dmabuf {
628 struct vmw_framebuffer base;
629 struct vmw_dma_buffer *buffer;
630};
631
632void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
633{
634 struct vmw_framebuffer_dmabuf *vfbd =
635 vmw_framebuffer_to_vfbd(framebuffer);
636
637 drm_framebuffer_cleanup(framebuffer);
638 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200639 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000640
641 kfree(vfbd);
642}
643
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200644static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
645 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200646 unsigned flags, unsigned color,
647 struct drm_clip_rect *clips,
648 unsigned num_clips, int increment)
649{
650 size_t fifo_size;
651 int i;
652
653 struct {
654 uint32_t header;
655 SVGAFifoCmdUpdate body;
656 } *cmd;
657
658 fifo_size = sizeof(*cmd) * num_clips;
659 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
660 if (unlikely(cmd == NULL)) {
661 DRM_ERROR("Fifo reserve failed.\n");
662 return -ENOMEM;
663 }
664
665 memset(cmd, 0, fifo_size);
666 for (i = 0; i < num_clips; i++, clips += increment) {
667 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
668 cmd[i].body.x = cpu_to_le32(clips->x1);
669 cmd[i].body.y = cpu_to_le32(clips->y1);
670 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
671 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
672 }
673
674 vmw_fifo_commit(dev_priv, fifo_size);
675 return 0;
676}
677
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200678static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
679 struct vmw_private *dev_priv,
680 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200681{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200682 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200683 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200684 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200685
686 struct {
687 uint32_t header;
688 SVGAFifoCmdDefineGMRFB body;
689 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200690
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200691 /* Emulate RGBA support, contrary to svga_reg.h this is not
692 * supported by hosts. This is only a problem if we are reading
693 * this value later and expecting what we uploaded back.
694 */
695 if (depth == 32)
696 depth = 24;
697
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200698 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200699 cmd = kmalloc(fifo_size, GFP_KERNEL);
700 if (unlikely(cmd == NULL)) {
701 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
702 return -ENOMEM;
703 }
704
705 memset(cmd, 0, fifo_size);
706 cmd->header = SVGA_CMD_DEFINE_GMRFB;
707 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200708 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200709 cmd->body.format.reserved = 0;
710 cmd->body.bytesPerLine = framebuffer->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200711 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200712 cmd->body.ptr.offset = 0;
713
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200714 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
715 fifo_size, 0, NULL);
716
717 kfree(cmd);
718
719 return ret;
720}
721
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200722static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
723 struct vmw_private *dev_priv,
724 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200725 unsigned flags, unsigned color,
726 struct drm_clip_rect *clips,
727 unsigned num_clips, int increment)
728{
729 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
730 struct drm_clip_rect *clips_ptr;
731 int i, k, num_units, ret;
732 struct drm_crtc *crtc;
733 size_t fifo_size;
734
735 struct {
736 uint32_t header;
737 SVGAFifoCmdBlitGMRFBToScreen body;
738 } *blits;
739
740 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
741 if (unlikely(ret != 0))
742 return ret; /* define_gmrfb prints warnings */
743
744 fifo_size = sizeof(*blits) * num_clips;
745 blits = kmalloc(fifo_size, GFP_KERNEL);
746 if (unlikely(blits == NULL)) {
747 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
748 return -ENOMEM;
749 }
750
751 num_units = 0;
752 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
753 if (crtc->fb != &framebuffer->base)
754 continue;
755 units[num_units++] = vmw_crtc_to_du(crtc);
756 }
757
758 for (k = 0; k < num_units; k++) {
759 struct vmw_display_unit *unit = units[k];
760 int hit_num = 0;
761
762 clips_ptr = clips;
763 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
764 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
765 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
766 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
767 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
768
769 /* skip any crtcs that misses the clip region */
770 if (clip_x1 >= unit->crtc.mode.hdisplay ||
771 clip_y1 >= unit->crtc.mode.vdisplay ||
772 clip_x2 <= 0 || clip_y2 <= 0)
773 continue;
774
775 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
776 blits[hit_num].body.destScreenId = unit->unit;
777 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
778 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
779 blits[hit_num].body.destRect.left = clip_x1;
780 blits[hit_num].body.destRect.top = clip_y1;
781 blits[hit_num].body.destRect.right = clip_x2;
782 blits[hit_num].body.destRect.bottom = clip_y2;
783 hit_num++;
784 }
785
786 /* no clips hit the crtc */
787 if (hit_num == 0)
788 continue;
789
790 fifo_size = sizeof(*blits) * hit_num;
791 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
792 fifo_size, 0, NULL);
793
794 if (unlikely(ret != 0))
795 break;
796 }
797
798 kfree(blits);
799
800 return ret;
801}
802
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000803int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200804 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000805 unsigned flags, unsigned color,
806 struct drm_clip_rect *clips,
807 unsigned num_clips)
808{
809 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200810 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200811 struct vmw_framebuffer_dmabuf *vfbd =
812 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000813 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200814 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000815
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200816 ret = ttm_read_lock(&vmaster->lock, true);
817 if (unlikely(ret != 0))
818 return ret;
819
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100820 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000821 num_clips = 1;
822 clips = &norect;
823 norect.x1 = norect.y1 = 0;
824 norect.x2 = framebuffer->width;
825 norect.y2 = framebuffer->height;
826 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
827 num_clips /= 2;
828 increment = 2;
829 }
830
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200831 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200832 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200833 flags, color,
834 clips, num_clips, increment);
835 } else {
836 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200837 flags, color,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200838 clips, num_clips, increment);
839 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000840
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200841 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200842 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000843}
844
845static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
846 .destroy = vmw_framebuffer_dmabuf_destroy,
847 .dirty = vmw_framebuffer_dmabuf_dirty,
848 .create_handle = vmw_framebuffer_create_handle,
849};
850
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200851/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200852 * Pin the dmabuffer to the start of vram.
853 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000854static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
855{
856 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
857 struct vmw_framebuffer_dmabuf *vfbd =
858 vmw_framebuffer_to_vfbd(&vfb->base);
859 int ret;
860
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200861 /* This code should not be used with screen objects */
862 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200863
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000864 vmw_overlay_pause_all(dev_priv);
865
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200866 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000867
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000868 vmw_overlay_resume_all(dev_priv);
869
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200870 WARN_ON(ret != 0);
871
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000872 return 0;
873}
874
875static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
876{
877 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
878 struct vmw_framebuffer_dmabuf *vfbd =
879 vmw_framebuffer_to_vfbd(&vfb->base);
880
881 if (!vfbd->buffer) {
882 WARN_ON(!vfbd->buffer);
883 return 0;
884 }
885
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200886 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000887}
888
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200889static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
890 struct vmw_dma_buffer *dmabuf,
891 struct vmw_framebuffer **out,
892 const struct drm_mode_fb_cmd
893 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000894
895{
896 struct drm_device *dev = dev_priv->dev;
897 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200898 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000899 int ret;
900
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200901 requested_size = mode_cmd->height * mode_cmd->pitch;
902 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
903 DRM_ERROR("Screen buffer object size is too small "
904 "for requested mode.\n");
905 return -EINVAL;
906 }
907
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200908 /* Limited framebuffer color depth support for screen objects */
909 if (dev_priv->sou_priv) {
910 switch (mode_cmd->depth) {
911 case 32:
912 case 24:
913 /* Only support 32 bpp for 32 and 24 depth fbs */
914 if (mode_cmd->bpp == 32)
915 break;
916
917 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
918 mode_cmd->depth, mode_cmd->bpp);
919 return -EINVAL;
920 case 16:
921 case 15:
922 /* Only support 16 bpp for 16 and 15 depth fbs */
923 if (mode_cmd->bpp == 16)
924 break;
925
926 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
927 mode_cmd->depth, mode_cmd->bpp);
928 return -EINVAL;
929 default:
930 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
931 return -EINVAL;
932 }
933 }
934
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000935 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
936 if (!vfbd) {
937 ret = -ENOMEM;
938 goto out_err1;
939 }
940
941 ret = drm_framebuffer_init(dev, &vfbd->base.base,
942 &vmw_framebuffer_dmabuf_funcs);
943 if (ret)
944 goto out_err2;
945
946 if (!vmw_dmabuf_reference(dmabuf)) {
947 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
948 goto out_err3;
949 }
950
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200951 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
952 vfbd->base.base.pitch = mode_cmd->pitch;
953 vfbd->base.base.depth = mode_cmd->depth;
954 vfbd->base.base.width = mode_cmd->width;
955 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200956 if (!dev_priv->sou_priv) {
957 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
958 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
959 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200960 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000961 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200962 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000963 *out = &vfbd->base;
964
965 return 0;
966
967out_err3:
968 drm_framebuffer_cleanup(&vfbd->base.base);
969out_err2:
970 kfree(vfbd);
971out_err1:
972 return ret;
973}
974
975/*
976 * Generic Kernel modesetting functions
977 */
978
979static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
980 struct drm_file *file_priv,
981 struct drm_mode_fb_cmd *mode_cmd)
982{
983 struct vmw_private *dev_priv = vmw_priv(dev);
984 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
985 struct vmw_framebuffer *vfb = NULL;
986 struct vmw_surface *surface = NULL;
987 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200988 struct ttm_base_object *user_obj;
Thomas Hellstrome133e7372010-10-05 12:43:04 +0200989 u64 required_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000990 int ret;
991
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200992 /**
993 * This code should be conditioned on Screen Objects not being used.
994 * If screen objects are used, we can allocate a GMR to hold the
995 * requested framebuffer.
996 */
997
998 required_size = mode_cmd->pitch * mode_cmd->height;
Thomas Hellstrome133e7372010-10-05 12:43:04 +0200999 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001000 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001001 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001002 }
1003
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001004 /*
1005 * Take a reference on the user object of the resource
1006 * backing the kms fb. This ensures that user-space handle
1007 * lookups on that resource will always work as long as
1008 * it's registered with a kms framebuffer. This is important,
1009 * since vmw_execbuf_process identifies resources in the
1010 * command stream using user-space handles.
1011 */
1012
1013 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1014 if (unlikely(user_obj == NULL)) {
1015 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1016 return ERR_PTR(-ENOENT);
1017 }
1018
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001019 /**
1020 * End conditioned code.
1021 */
1022
Thomas Hellstrom7a73ba72009-12-22 16:53:41 +01001023 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
1024 mode_cmd->handle, &surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001025 if (ret)
1026 goto try_dmabuf;
1027
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001028 if (!surface->scanout)
1029 goto err_not_scanout;
1030
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001031 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
1032 &vfb, mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001033
1034 /* vmw_user_surface_lookup takes one ref so does new_fb */
1035 vmw_surface_unreference(&surface);
1036
1037 if (ret) {
1038 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001039 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001040 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001041 } else
1042 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001043 return &vfb->base;
1044
1045try_dmabuf:
1046 DRM_INFO("%s: trying buffer\n", __func__);
1047
1048 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
1049 if (ret) {
1050 DRM_ERROR("failed to find buffer: %i\n", ret);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001051 return ERR_PTR(-ENOENT);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001052 }
1053
1054 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001055 mode_cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001056
1057 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
1058 vmw_dmabuf_unreference(&bo);
1059
1060 if (ret) {
1061 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001062 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001063 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001064 } else
1065 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001066
1067 return &vfb->base;
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001068
1069err_not_scanout:
1070 DRM_ERROR("surface not marked as scanout\n");
1071 /* vmw_user_surface_lookup takes one ref */
1072 vmw_surface_unreference(&surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001073 ttm_base_object_unref(&user_obj);
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001074
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001075 return ERR_PTR(-EINVAL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001076}
1077
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001078static struct drm_mode_config_funcs vmw_kms_funcs = {
1079 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001080};
1081
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001082int vmw_kms_present(struct vmw_private *dev_priv,
1083 struct drm_file *file_priv,
1084 struct vmw_framebuffer *vfb,
1085 struct vmw_surface *surface,
1086 uint32_t sid,
1087 int32_t destX, int32_t destY,
1088 struct drm_vmw_rect *clips,
1089 uint32_t num_clips)
1090{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001091 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1092 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001093 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001094 int i, k, num_units;
1095 int ret = 0; /* silence warning */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001096
1097 struct {
1098 SVGA3dCmdHeader header;
1099 SVGA3dCmdBlitSurfaceToScreen body;
1100 } *cmd;
1101 SVGASignedRect *blits;
1102
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001103 num_units = 0;
1104 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1105 if (crtc->fb != &vfb->base)
1106 continue;
1107 units[num_units++] = vmw_crtc_to_du(crtc);
1108 }
1109
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001110 BUG_ON(surface == NULL);
1111 BUG_ON(!clips || !num_clips);
1112
1113 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1114 cmd = kmalloc(fifo_size, GFP_KERNEL);
1115 if (unlikely(cmd == NULL)) {
1116 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1117 return -ENOMEM;
1118 }
1119
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001120 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001121 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001122 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1123 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1124
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001125 cmd->body.srcRect.left = 0;
1126 cmd->body.srcRect.right = surface->sizes[0].width;
1127 cmd->body.srcRect.top = 0;
1128 cmd->body.srcRect.bottom = surface->sizes[0].height;
1129
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001130 blits = (SVGASignedRect *)&cmd[1];
1131 for (i = 0; i < num_clips; i++) {
1132 blits[i].left = clips[i].x;
1133 blits[i].right = clips[i].x + clips[i].w;
1134 blits[i].top = clips[i].y;
1135 blits[i].bottom = clips[i].y + clips[i].h;
1136 }
1137
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001138 for (k = 0; k < num_units; k++) {
1139 struct vmw_display_unit *unit = units[k];
1140 int clip_x1 = destX - unit->crtc.x;
1141 int clip_y1 = destY - unit->crtc.y;
1142 int clip_x2 = clip_x1 + surface->sizes[0].width;
1143 int clip_y2 = clip_y1 + surface->sizes[0].height;
1144
1145 /* skip any crtcs that misses the clip region */
1146 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1147 clip_y1 >= unit->crtc.mode.vdisplay ||
1148 clip_x2 <= 0 || clip_y2 <= 0)
1149 continue;
1150
1151 /* need to reset sid as it is changed by execbuf */
1152 cmd->body.srcImage.sid = sid;
1153
1154 cmd->body.destScreenId = unit->unit;
1155
1156 /*
1157 * The blit command is a lot more resilient then the
1158 * readback command when it comes to clip rects. So its
1159 * okay to go out of bounds.
1160 */
1161
1162 cmd->body.destRect.left = clip_x1;
1163 cmd->body.destRect.right = clip_x2;
1164 cmd->body.destRect.top = clip_y1;
1165 cmd->body.destRect.bottom = clip_y2;
1166
1167 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1168 fifo_size, 0, NULL);
1169
1170 if (unlikely(ret != 0))
1171 break;
1172 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001173
1174 kfree(cmd);
1175
1176 return ret;
1177}
1178
1179int vmw_kms_readback(struct vmw_private *dev_priv,
1180 struct drm_file *file_priv,
1181 struct vmw_framebuffer *vfb,
1182 struct drm_vmw_fence_rep __user *user_fence_rep,
1183 struct drm_vmw_rect *clips,
1184 uint32_t num_clips)
1185{
1186 struct vmw_framebuffer_dmabuf *vfbd =
1187 vmw_framebuffer_to_vfbd(&vfb->base);
1188 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1189 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1190 struct drm_crtc *crtc;
1191 size_t fifo_size;
1192 int i, k, ret, num_units, blits_pos;
1193
1194 struct {
1195 uint32_t header;
1196 SVGAFifoCmdDefineGMRFB body;
1197 } *cmd;
1198 struct {
1199 uint32_t header;
1200 SVGAFifoCmdBlitScreenToGMRFB body;
1201 } *blits;
1202
1203 num_units = 0;
1204 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1205 if (crtc->fb != &vfb->base)
1206 continue;
1207 units[num_units++] = vmw_crtc_to_du(crtc);
1208 }
1209
1210 BUG_ON(dmabuf == NULL);
1211 BUG_ON(!clips || !num_clips);
1212
1213 /* take a safe guess at fifo size */
1214 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1215 cmd = kmalloc(fifo_size, GFP_KERNEL);
1216 if (unlikely(cmd == NULL)) {
1217 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1218 return -ENOMEM;
1219 }
1220
1221 memset(cmd, 0, fifo_size);
1222 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1223 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1224 cmd->body.format.colorDepth = vfb->base.depth;
1225 cmd->body.format.reserved = 0;
1226 cmd->body.bytesPerLine = vfb->base.pitch;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001227 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001228 cmd->body.ptr.offset = 0;
1229
1230 blits = (void *)&cmd[1];
1231 blits_pos = 0;
1232 for (i = 0; i < num_units; i++) {
1233 struct drm_vmw_rect *c = clips;
1234 for (k = 0; k < num_clips; k++, c++) {
1235 /* transform clip coords to crtc origin based coords */
1236 int clip_x1 = c->x - units[i]->crtc.x;
1237 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1238 int clip_y1 = c->y - units[i]->crtc.y;
1239 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1240 int dest_x = c->x;
1241 int dest_y = c->y;
1242
1243 /* compensate for clipping, we negate
1244 * a negative number and add that.
1245 */
1246 if (clip_x1 < 0)
1247 dest_x += -clip_x1;
1248 if (clip_y1 < 0)
1249 dest_y += -clip_y1;
1250
1251 /* clip */
1252 clip_x1 = max(clip_x1, 0);
1253 clip_y1 = max(clip_y1, 0);
1254 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1255 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1256
1257 /* and cull any rects that misses the crtc */
1258 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1259 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1260 clip_x2 <= 0 || clip_y2 <= 0)
1261 continue;
1262
1263 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1264 blits[blits_pos].body.srcScreenId = units[i]->unit;
1265 blits[blits_pos].body.destOrigin.x = dest_x;
1266 blits[blits_pos].body.destOrigin.y = dest_y;
1267
1268 blits[blits_pos].body.srcRect.left = clip_x1;
1269 blits[blits_pos].body.srcRect.top = clip_y1;
1270 blits[blits_pos].body.srcRect.right = clip_x2;
1271 blits[blits_pos].body.srcRect.bottom = clip_y2;
1272 blits_pos++;
1273 }
1274 }
1275 /* reset size here and use calculated exact size from loops */
1276 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1277
1278 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1279 0, user_fence_rep);
1280
1281 kfree(cmd);
1282
1283 return ret;
1284}
1285
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001286int vmw_kms_init(struct vmw_private *dev_priv)
1287{
1288 struct drm_device *dev = dev_priv->dev;
1289 int ret;
1290
1291 drm_mode_config_init(dev);
1292 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001293 dev->mode_config.min_width = 1;
1294 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001295 /* assumed largest fb size */
1296 dev->mode_config.max_width = 8192;
1297 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001298
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001299 ret = vmw_kms_init_screen_object_display(dev_priv);
1300 if (ret) /* Fallback */
1301 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001302
1303 return 0;
1304}
1305
1306int vmw_kms_close(struct vmw_private *dev_priv)
1307{
1308 /*
1309 * Docs says we should take the lock before calling this function
1310 * but since it destroys encoders and our destructor calls
1311 * drm_encoder_cleanup which takes the lock we deadlock.
1312 */
1313 drm_mode_config_cleanup(dev_priv->dev);
1314 vmw_kms_close_legacy_display_system(dev_priv);
1315 return 0;
1316}
1317
1318int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1319 struct drm_file *file_priv)
1320{
1321 struct drm_vmw_cursor_bypass_arg *arg = data;
1322 struct vmw_display_unit *du;
1323 struct drm_mode_object *obj;
1324 struct drm_crtc *crtc;
1325 int ret = 0;
1326
1327
1328 mutex_lock(&dev->mode_config.mutex);
1329 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1330
1331 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1332 du = vmw_crtc_to_du(crtc);
1333 du->hotspot_x = arg->xhot;
1334 du->hotspot_y = arg->yhot;
1335 }
1336
1337 mutex_unlock(&dev->mode_config.mutex);
1338 return 0;
1339 }
1340
1341 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1342 if (!obj) {
1343 ret = -EINVAL;
1344 goto out;
1345 }
1346
1347 crtc = obj_to_crtc(obj);
1348 du = vmw_crtc_to_du(crtc);
1349
1350 du->hotspot_x = arg->xhot;
1351 du->hotspot_y = arg->yhot;
1352
1353out:
1354 mutex_unlock(&dev->mode_config.mutex);
1355
1356 return ret;
1357}
1358
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001359int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001360 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001361 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001362{
1363 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1364 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1365 else if (vmw_fifo_have_pitchlock(vmw_priv))
1366 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1367 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1368 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001369 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001370
1371 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1372 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1373 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1374 return -EINVAL;
1375 }
1376
1377 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001378}
1379
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001380int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1381{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001382 struct vmw_vga_topology_state *save;
1383 uint32_t i;
1384
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001385 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1386 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001387 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001388 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1389 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001390 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001391 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001392 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1393 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001394
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001395 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1396 return 0;
1397
1398 vmw_priv->num_displays = vmw_read(vmw_priv,
1399 SVGA_REG_NUM_GUEST_DISPLAYS);
1400
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001401 if (vmw_priv->num_displays == 0)
1402 vmw_priv->num_displays = 1;
1403
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001404 for (i = 0; i < vmw_priv->num_displays; ++i) {
1405 save = &vmw_priv->vga_save[i];
1406 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1407 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1408 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1409 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1410 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1411 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1412 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001413 if (i == 0 && vmw_priv->num_displays == 1 &&
1414 save->width == 0 && save->height == 0) {
1415
1416 /*
1417 * It should be fairly safe to assume that these
1418 * values are uninitialized.
1419 */
1420
1421 save->width = vmw_priv->vga_width - save->pos_x;
1422 save->height = vmw_priv->vga_height - save->pos_y;
1423 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001424 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001425
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001426 return 0;
1427}
1428
1429int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1430{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001431 struct vmw_vga_topology_state *save;
1432 uint32_t i;
1433
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001434 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1435 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001436 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001437 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1438 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1439 vmw_priv->vga_pitchlock);
1440 else if (vmw_fifo_have_pitchlock(vmw_priv))
1441 iowrite32(vmw_priv->vga_pitchlock,
1442 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001443
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001444 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1445 return 0;
1446
1447 for (i = 0; i < vmw_priv->num_displays; ++i) {
1448 save = &vmw_priv->vga_save[i];
1449 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1450 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1451 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1452 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1453 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1454 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1455 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1456 }
1457
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001458 return 0;
1459}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001460
Thomas Hellstrome133e7372010-10-05 12:43:04 +02001461bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1462 uint32_t pitch,
1463 uint32_t height)
1464{
1465 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1466}
1467
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001468
1469/**
1470 * Function called by DRM code called with vbl_lock held.
1471 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001472u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1473{
1474 return 0;
1475}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001476
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001477/**
1478 * Function called by DRM code called with vbl_lock held.
1479 */
1480int vmw_enable_vblank(struct drm_device *dev, int crtc)
1481{
1482 return -ENOSYS;
1483}
1484
1485/**
1486 * Function called by DRM code called with vbl_lock held.
1487 */
1488void vmw_disable_vblank(struct drm_device *dev, int crtc)
1489{
1490}
1491
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001492
1493/*
1494 * Small shared kms functions.
1495 */
1496
1497int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1498 struct drm_vmw_rect *rects)
1499{
1500 struct drm_device *dev = dev_priv->dev;
1501 struct vmw_display_unit *du;
1502 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001503
1504 mutex_lock(&dev->mode_config.mutex);
1505
1506#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001507 {
1508 unsigned int i;
1509
1510 DRM_INFO("%s: new layout ", __func__);
1511 for (i = 0; i < num; i++)
1512 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1513 rects[i].w, rects[i].h);
1514 DRM_INFO("\n");
1515 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001516#endif
1517
1518 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1519 du = vmw_connector_to_du(con);
1520 if (num > du->unit) {
1521 du->pref_width = rects[du->unit].w;
1522 du->pref_height = rects[du->unit].h;
1523 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001524 du->gui_x = rects[du->unit].x;
1525 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001526 } else {
1527 du->pref_width = 800;
1528 du->pref_height = 600;
1529 du->pref_active = false;
1530 }
1531 con->status = vmw_du_connector_detect(con, true);
1532 }
1533
1534 mutex_unlock(&dev->mode_config.mutex);
1535
1536 return 0;
1537}
1538
1539void vmw_du_crtc_save(struct drm_crtc *crtc)
1540{
1541}
1542
1543void vmw_du_crtc_restore(struct drm_crtc *crtc)
1544{
1545}
1546
1547void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1548 u16 *r, u16 *g, u16 *b,
1549 uint32_t start, uint32_t size)
1550{
1551 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1552 int i;
1553
1554 for (i = 0; i < size; i++) {
1555 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1556 r[i], g[i], b[i]);
1557 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1558 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1559 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1560 }
1561}
1562
1563void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1564{
1565}
1566
1567void vmw_du_connector_save(struct drm_connector *connector)
1568{
1569}
1570
1571void vmw_du_connector_restore(struct drm_connector *connector)
1572{
1573}
1574
1575enum drm_connector_status
1576vmw_du_connector_detect(struct drm_connector *connector, bool force)
1577{
1578 uint32_t num_displays;
1579 struct drm_device *dev = connector->dev;
1580 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001581 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001582
1583 mutex_lock(&dev_priv->hw_mutex);
1584 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1585 mutex_unlock(&dev_priv->hw_mutex);
1586
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001587 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1588 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001589 connector_status_connected : connector_status_disconnected);
1590}
1591
1592static struct drm_display_mode vmw_kms_connector_builtin[] = {
1593 /* 640x480@60Hz */
1594 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1595 752, 800, 0, 480, 489, 492, 525, 0,
1596 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1597 /* 800x600@60Hz */
1598 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1599 968, 1056, 0, 600, 601, 605, 628, 0,
1600 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1601 /* 1024x768@60Hz */
1602 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1603 1184, 1344, 0, 768, 771, 777, 806, 0,
1604 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1605 /* 1152x864@75Hz */
1606 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1607 1344, 1600, 0, 864, 865, 868, 900, 0,
1608 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1609 /* 1280x768@60Hz */
1610 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1611 1472, 1664, 0, 768, 771, 778, 798, 0,
1612 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1613 /* 1280x800@60Hz */
1614 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1615 1480, 1680, 0, 800, 803, 809, 831, 0,
1616 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1617 /* 1280x960@60Hz */
1618 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1619 1488, 1800, 0, 960, 961, 964, 1000, 0,
1620 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1621 /* 1280x1024@60Hz */
1622 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1623 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1624 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1625 /* 1360x768@60Hz */
1626 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1627 1536, 1792, 0, 768, 771, 777, 795, 0,
1628 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1629 /* 1440x1050@60Hz */
1630 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1631 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1632 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1633 /* 1440x900@60Hz */
1634 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1635 1672, 1904, 0, 900, 903, 909, 934, 0,
1636 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1637 /* 1600x1200@60Hz */
1638 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1639 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1640 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1641 /* 1680x1050@60Hz */
1642 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1643 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1644 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1645 /* 1792x1344@60Hz */
1646 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1647 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1648 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1649 /* 1853x1392@60Hz */
1650 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1651 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1652 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1653 /* 1920x1200@60Hz */
1654 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1655 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1656 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1657 /* 1920x1440@60Hz */
1658 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1659 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1660 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1661 /* 2560x1600@60Hz */
1662 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1663 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1664 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1665 /* Terminate */
1666 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1667};
1668
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01001669/**
1670 * vmw_guess_mode_timing - Provide fake timings for a
1671 * 60Hz vrefresh mode.
1672 *
1673 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1674 * members filled in.
1675 */
1676static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1677{
1678 mode->hsync_start = mode->hdisplay + 50;
1679 mode->hsync_end = mode->hsync_start + 50;
1680 mode->htotal = mode->hsync_end + 50;
1681
1682 mode->vsync_start = mode->vdisplay + 50;
1683 mode->vsync_end = mode->vsync_start + 50;
1684 mode->vtotal = mode->vsync_end + 50;
1685
1686 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1687 mode->vrefresh = drm_mode_vrefresh(mode);
1688}
1689
1690
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001691int vmw_du_connector_fill_modes(struct drm_connector *connector,
1692 uint32_t max_width, uint32_t max_height)
1693{
1694 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1695 struct drm_device *dev = connector->dev;
1696 struct vmw_private *dev_priv = vmw_priv(dev);
1697 struct drm_display_mode *mode = NULL;
1698 struct drm_display_mode *bmode;
1699 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1700 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1701 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1702 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1703 };
1704 int i;
1705
1706 /* Add preferred mode */
1707 {
1708 mode = drm_mode_duplicate(dev, &prefmode);
1709 if (!mode)
1710 return 0;
1711 mode->hdisplay = du->pref_width;
1712 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01001713 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001714
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001715 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1716 mode->vdisplay)) {
1717 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001718 } else {
1719 drm_mode_destroy(dev, mode);
1720 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001721 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001722
1723 if (du->pref_mode) {
1724 list_del_init(&du->pref_mode->head);
1725 drm_mode_destroy(dev, du->pref_mode);
1726 }
1727
1728 /* mode might be null here, this is intended */
1729 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001730 }
1731
1732 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1733 bmode = &vmw_kms_connector_builtin[i];
1734 if (bmode->hdisplay > max_width ||
1735 bmode->vdisplay > max_height)
1736 continue;
1737
1738 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1739 bmode->vdisplay))
1740 continue;
1741
1742 mode = drm_mode_duplicate(dev, bmode);
1743 if (!mode)
1744 return 0;
1745 mode->vrefresh = drm_mode_vrefresh(mode);
1746
1747 drm_mode_probed_add(connector, mode);
1748 }
1749
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001750 /* Move the prefered mode first, help apps pick the right mode. */
1751 if (du->pref_mode)
1752 list_move(&du->pref_mode->head, &connector->probed_modes);
1753
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001754 drm_mode_connector_list_update(connector);
1755
1756 return 1;
1757}
1758
1759int vmw_du_connector_set_property(struct drm_connector *connector,
1760 struct drm_property *property,
1761 uint64_t val)
1762{
1763 return 0;
1764}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001765
1766
1767int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1768 struct drm_file *file_priv)
1769{
1770 struct vmw_private *dev_priv = vmw_priv(dev);
1771 struct drm_vmw_update_layout_arg *arg =
1772 (struct drm_vmw_update_layout_arg *)data;
1773 struct vmw_master *vmaster = vmw_master(file_priv->master);
1774 void __user *user_rects;
1775 struct drm_vmw_rect *rects;
1776 unsigned rects_size;
1777 int ret;
1778 int i;
1779 struct drm_mode_config *mode_config = &dev->mode_config;
1780
1781 ret = ttm_read_lock(&vmaster->lock, true);
1782 if (unlikely(ret != 0))
1783 return ret;
1784
1785 if (!arg->num_outputs) {
1786 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1787 vmw_du_update_layout(dev_priv, 1, &def_rect);
1788 goto out_unlock;
1789 }
1790
1791 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1792 rects = kzalloc(rects_size, GFP_KERNEL);
1793 if (unlikely(!rects)) {
1794 ret = -ENOMEM;
1795 goto out_unlock;
1796 }
1797
1798 user_rects = (void __user *)(unsigned long)arg->rects;
1799 ret = copy_from_user(rects, user_rects, rects_size);
1800 if (unlikely(ret != 0)) {
1801 DRM_ERROR("Failed to get rects.\n");
1802 ret = -EFAULT;
1803 goto out_free;
1804 }
1805
1806 for (i = 0; i < arg->num_outputs; ++i) {
1807 if (rects->x < 0 ||
1808 rects->y < 0 ||
1809 rects->x + rects->w > mode_config->max_width ||
1810 rects->y + rects->h > mode_config->max_height) {
1811 DRM_ERROR("Invalid GUI layout.\n");
1812 ret = -EINVAL;
1813 goto out_free;
1814 }
1815 }
1816
1817 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1818
1819out_free:
1820 kfree(rects);
1821out_unlock:
1822 ttm_read_unlock(&vmaster->lock);
1823 return ret;
1824}