1 /**************************************************************************
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include <drm/vmwgfx_drm.h>
30 #include "vmwgfx_kms.h"
32 int vmw_getparam_ioctl(struct drm_device *dev, void *data,
33 struct drm_file *file_priv)
35 struct vmw_private *dev_priv = vmw_priv(dev);
36 struct drm_vmw_getparam_arg *param =
37 (struct drm_vmw_getparam_arg *)data;
39 switch (param->param) {
40 case DRM_VMW_PARAM_NUM_STREAMS:
41 param->value = vmw_overlay_num_overlays(dev_priv);
43 case DRM_VMW_PARAM_NUM_FREE_STREAMS:
44 param->value = vmw_overlay_num_free_overlays(dev_priv);
46 case DRM_VMW_PARAM_3D:
47 param->value = vmw_fifo_have_3d(dev_priv) ? 1 : 0;
49 case DRM_VMW_PARAM_HW_CAPS:
50 param->value = dev_priv->capabilities;
52 case DRM_VMW_PARAM_FIFO_CAPS:
53 param->value = dev_priv->fifo.capabilities;
55 case DRM_VMW_PARAM_MAX_FB_SIZE:
56 param->value = dev_priv->prim_bb_mem;
58 case DRM_VMW_PARAM_FIFO_HW_VERSION:
60 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
61 const struct vmw_fifo_state *fifo = &dev_priv->fifo;
65 ((fifo->capabilities &
66 SVGA_FIFO_CAP_3D_HWVERSION_REVISED) ?
67 SVGA_FIFO_3D_HWVERSION_REVISED :
68 SVGA_FIFO_3D_HWVERSION));
71 case DRM_VMW_PARAM_MAX_SURF_MEMORY:
72 param->value = dev_priv->memory_size;
74 case DRM_VMW_PARAM_3D_CAPS_SIZE:
75 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS)
76 param->value = SVGA3D_DEVCAP_MAX;
78 param->value = (SVGA_FIFO_3D_CAPS_LAST -
79 SVGA_FIFO_3D_CAPS + 1);
80 param->value *= sizeof(uint32_t);
83 DRM_ERROR("Illegal vmwgfx get param request: %d\n",
92 int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
93 struct drm_file *file_priv)
95 struct drm_vmw_get_3d_cap_arg *arg =
96 (struct drm_vmw_get_3d_cap_arg *) data;
97 struct vmw_private *dev_priv = vmw_priv(dev);
99 __le32 __iomem *fifo_mem;
100 void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
103 bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
105 if (unlikely(arg->pad64 != 0)) {
106 DRM_ERROR("Illegal GET_3D_CAP argument.\n");
111 size = SVGA3D_DEVCAP_MAX;
113 size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1);
115 size *= sizeof(uint32_t);
117 if (arg->max_size < size)
118 size = arg->max_size;
120 bounce = vmalloc(size);
121 if (unlikely(bounce == NULL)) {
122 DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
128 uint32_t *bounce32 = (uint32_t *) bounce;
130 mutex_lock(&dev_priv->hw_mutex);
131 for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
132 vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
133 *bounce32++ = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
135 mutex_unlock(&dev_priv->hw_mutex);
139 fifo_mem = dev_priv->mmio_virt;
140 memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
143 ret = copy_to_user(buffer, bounce, size);
148 if (unlikely(ret != 0))
149 DRM_ERROR("Failed to report 3D caps info.\n");
154 int vmw_present_ioctl(struct drm_device *dev, void *data,
155 struct drm_file *file_priv)
157 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
158 struct vmw_private *dev_priv = vmw_priv(dev);
159 struct drm_vmw_present_arg *arg =
160 (struct drm_vmw_present_arg *)data;
161 struct vmw_surface *surface;
162 struct vmw_master *vmaster = vmw_master(file_priv->master);
163 struct drm_vmw_rect __user *clips_ptr;
164 struct drm_vmw_rect *clips = NULL;
165 struct drm_framebuffer *fb;
166 struct vmw_framebuffer *vfb;
167 struct vmw_resource *res;
171 num_clips = arg->num_clips;
172 clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
174 if (unlikely(num_clips == 0))
177 if (clips_ptr == NULL) {
178 DRM_ERROR("Variable clips_ptr must be specified.\n");
183 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
185 DRM_ERROR("Failed to allocate clip rect list.\n");
190 ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
192 DRM_ERROR("Failed to copy clip rects from userspace.\n");
197 drm_modeset_lock_all(dev);
199 fb = drm_framebuffer_lookup(dev, arg->fb_id);
201 DRM_ERROR("Invalid framebuffer id.\n");
205 vfb = vmw_framebuffer_to_vfb(fb);
207 ret = ttm_read_lock(&vmaster->lock, true);
208 if (unlikely(ret != 0))
209 goto out_no_ttm_lock;
211 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
212 user_surface_converter,
217 surface = vmw_res_to_srf(res);
218 ret = vmw_kms_present(dev_priv, file_priv,
219 vfb, surface, arg->sid,
220 arg->dest_x, arg->dest_y,
223 /* vmw_user_surface_lookup takes one ref so does new_fb */
224 vmw_surface_unreference(&surface);
227 ttm_read_unlock(&vmaster->lock);
229 drm_framebuffer_unreference(fb);
231 drm_modeset_unlock_all(dev);
238 int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
239 struct drm_file *file_priv)
241 struct vmw_private *dev_priv = vmw_priv(dev);
242 struct drm_vmw_present_readback_arg *arg =
243 (struct drm_vmw_present_readback_arg *)data;
244 struct drm_vmw_fence_rep __user *user_fence_rep =
245 (struct drm_vmw_fence_rep __user *)
246 (unsigned long)arg->fence_rep;
247 struct vmw_master *vmaster = vmw_master(file_priv->master);
248 struct drm_vmw_rect __user *clips_ptr;
249 struct drm_vmw_rect *clips = NULL;
250 struct drm_framebuffer *fb;
251 struct vmw_framebuffer *vfb;
255 num_clips = arg->num_clips;
256 clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
258 if (unlikely(num_clips == 0))
261 if (clips_ptr == NULL) {
262 DRM_ERROR("Argument clips_ptr must be specified.\n");
267 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
269 DRM_ERROR("Failed to allocate clip rect list.\n");
274 ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
276 DRM_ERROR("Failed to copy clip rects from userspace.\n");
281 drm_modeset_lock_all(dev);
283 fb = drm_framebuffer_lookup(dev, arg->fb_id);
285 DRM_ERROR("Invalid framebuffer id.\n");
290 vfb = vmw_framebuffer_to_vfb(fb);
292 DRM_ERROR("Framebuffer not dmabuf backed.\n");
294 goto out_no_ttm_lock;
297 ret = ttm_read_lock(&vmaster->lock, true);
298 if (unlikely(ret != 0))
299 goto out_no_ttm_lock;
301 ret = vmw_kms_readback(dev_priv, file_priv,
305 ttm_read_unlock(&vmaster->lock);
307 drm_framebuffer_unreference(fb);
309 drm_modeset_unlock_all(dev);
318 * vmw_fops_poll - wrapper around the drm_poll function
320 * @filp: See the linux fops poll documentation.
321 * @wait: See the linux fops poll documentation.
323 * Wrapper around the drm_poll function that makes sure the device is
324 * processing the fifo if drm_poll decides to wait.
326 unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
328 struct drm_file *file_priv = filp->private_data;
329 struct vmw_private *dev_priv =
330 vmw_priv(file_priv->minor->dev);
332 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
333 return drm_poll(filp, wait);
338 * vmw_fops_read - wrapper around the drm_read function
340 * @filp: See the linux fops read documentation.
341 * @buffer: See the linux fops read documentation.
342 * @count: See the linux fops read documentation.
343 * offset: See the linux fops read documentation.
345 * Wrapper around the drm_read function that makes sure the device is
346 * processing the fifo if drm_read decides to wait.
348 ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
349 size_t count, loff_t *offset)
351 struct drm_file *file_priv = filp->private_data;
352 struct vmw_private *dev_priv =
353 vmw_priv(file_priv->minor->dev);
355 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
356 return drm_read(filp, buffer, count, offset);