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);
82 case DRM_VMW_PARAM_MAX_MOB_MEMORY:
83 param->value = dev_priv->max_mob_pages * PAGE_SIZE;
86 DRM_ERROR("Illegal vmwgfx get param request: %d\n",
95 int vmw_get_cap_3d_ioctl(struct drm_device *dev, void *data,
96 struct drm_file *file_priv)
98 struct drm_vmw_get_3d_cap_arg *arg =
99 (struct drm_vmw_get_3d_cap_arg *) data;
100 struct vmw_private *dev_priv = vmw_priv(dev);
102 __le32 __iomem *fifo_mem;
103 void __user *buffer = (void __user *)((unsigned long)(arg->buffer));
106 bool gb_objects = !!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS);
108 if (unlikely(arg->pad64 != 0)) {
109 DRM_ERROR("Illegal GET_3D_CAP argument.\n");
114 size = SVGA3D_DEVCAP_MAX;
116 size = (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1);
118 size *= sizeof(uint32_t);
120 if (arg->max_size < size)
121 size = arg->max_size;
123 bounce = vmalloc(size);
124 if (unlikely(bounce == NULL)) {
125 DRM_ERROR("Failed to allocate bounce buffer for 3D caps.\n");
131 uint32_t *bounce32 = (uint32_t *) bounce;
133 mutex_lock(&dev_priv->hw_mutex);
134 for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
135 vmw_write(dev_priv, SVGA_REG_DEV_CAP, i);
136 *bounce32++ = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
138 mutex_unlock(&dev_priv->hw_mutex);
142 fifo_mem = dev_priv->mmio_virt;
143 memcpy_fromio(bounce, &fifo_mem[SVGA_FIFO_3D_CAPS], size);
146 ret = copy_to_user(buffer, bounce, size);
151 if (unlikely(ret != 0))
152 DRM_ERROR("Failed to report 3D caps info.\n");
157 int vmw_present_ioctl(struct drm_device *dev, void *data,
158 struct drm_file *file_priv)
160 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
161 struct vmw_private *dev_priv = vmw_priv(dev);
162 struct drm_vmw_present_arg *arg =
163 (struct drm_vmw_present_arg *)data;
164 struct vmw_surface *surface;
165 struct vmw_master *vmaster = vmw_master(file_priv->master);
166 struct drm_vmw_rect __user *clips_ptr;
167 struct drm_vmw_rect *clips = NULL;
168 struct drm_framebuffer *fb;
169 struct vmw_framebuffer *vfb;
170 struct vmw_resource *res;
174 num_clips = arg->num_clips;
175 clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
177 if (unlikely(num_clips == 0))
180 if (clips_ptr == NULL) {
181 DRM_ERROR("Variable clips_ptr must be specified.\n");
186 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
188 DRM_ERROR("Failed to allocate clip rect list.\n");
193 ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
195 DRM_ERROR("Failed to copy clip rects from userspace.\n");
200 drm_modeset_lock_all(dev);
202 fb = drm_framebuffer_lookup(dev, arg->fb_id);
204 DRM_ERROR("Invalid framebuffer id.\n");
208 vfb = vmw_framebuffer_to_vfb(fb);
210 ret = ttm_read_lock(&vmaster->lock, true);
211 if (unlikely(ret != 0))
212 goto out_no_ttm_lock;
214 ret = vmw_user_resource_lookup_handle(dev_priv, tfile, arg->sid,
215 user_surface_converter,
220 surface = vmw_res_to_srf(res);
221 ret = vmw_kms_present(dev_priv, file_priv,
222 vfb, surface, arg->sid,
223 arg->dest_x, arg->dest_y,
226 /* vmw_user_surface_lookup takes one ref so does new_fb */
227 vmw_surface_unreference(&surface);
230 ttm_read_unlock(&vmaster->lock);
232 drm_framebuffer_unreference(fb);
234 drm_modeset_unlock_all(dev);
241 int vmw_present_readback_ioctl(struct drm_device *dev, void *data,
242 struct drm_file *file_priv)
244 struct vmw_private *dev_priv = vmw_priv(dev);
245 struct drm_vmw_present_readback_arg *arg =
246 (struct drm_vmw_present_readback_arg *)data;
247 struct drm_vmw_fence_rep __user *user_fence_rep =
248 (struct drm_vmw_fence_rep __user *)
249 (unsigned long)arg->fence_rep;
250 struct vmw_master *vmaster = vmw_master(file_priv->master);
251 struct drm_vmw_rect __user *clips_ptr;
252 struct drm_vmw_rect *clips = NULL;
253 struct drm_framebuffer *fb;
254 struct vmw_framebuffer *vfb;
258 num_clips = arg->num_clips;
259 clips_ptr = (struct drm_vmw_rect *)(unsigned long)arg->clips_ptr;
261 if (unlikely(num_clips == 0))
264 if (clips_ptr == NULL) {
265 DRM_ERROR("Argument clips_ptr must be specified.\n");
270 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
272 DRM_ERROR("Failed to allocate clip rect list.\n");
277 ret = copy_from_user(clips, clips_ptr, num_clips * sizeof(*clips));
279 DRM_ERROR("Failed to copy clip rects from userspace.\n");
284 drm_modeset_lock_all(dev);
286 fb = drm_framebuffer_lookup(dev, arg->fb_id);
288 DRM_ERROR("Invalid framebuffer id.\n");
293 vfb = vmw_framebuffer_to_vfb(fb);
295 DRM_ERROR("Framebuffer not dmabuf backed.\n");
297 goto out_no_ttm_lock;
300 ret = ttm_read_lock(&vmaster->lock, true);
301 if (unlikely(ret != 0))
302 goto out_no_ttm_lock;
304 ret = vmw_kms_readback(dev_priv, file_priv,
308 ttm_read_unlock(&vmaster->lock);
310 drm_framebuffer_unreference(fb);
312 drm_modeset_unlock_all(dev);
321 * vmw_fops_poll - wrapper around the drm_poll function
323 * @filp: See the linux fops poll documentation.
324 * @wait: See the linux fops poll documentation.
326 * Wrapper around the drm_poll function that makes sure the device is
327 * processing the fifo if drm_poll decides to wait.
329 unsigned int vmw_fops_poll(struct file *filp, struct poll_table_struct *wait)
331 struct drm_file *file_priv = filp->private_data;
332 struct vmw_private *dev_priv =
333 vmw_priv(file_priv->minor->dev);
335 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
336 return drm_poll(filp, wait);
341 * vmw_fops_read - wrapper around the drm_read function
343 * @filp: See the linux fops read documentation.
344 * @buffer: See the linux fops read documentation.
345 * @count: See the linux fops read documentation.
346 * offset: See the linux fops read documentation.
348 * Wrapper around the drm_read function that makes sure the device is
349 * processing the fifo if drm_read decides to wait.
351 ssize_t vmw_fops_read(struct file *filp, char __user *buffer,
352 size_t count, loff_t *offset)
354 struct drm_file *file_priv = filp->private_data;
355 struct vmw_private *dev_priv =
356 vmw_priv(file_priv->minor->dev);
358 vmw_fifo_ping_host(dev_priv, SVGA_SYNC_GENERIC);
359 return drm_read(filp, buffer, count, offset);