blob: 6e8cb14813769a8629a56dcb5e46a0a227f7f6ec [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 **************************************************************************/
Paul Gortmakere0cd3602011-08-30 11:04:30 -040027#include <linux/module.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000028
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000030#include "vmwgfx_drv.h"
David Howells760285e2012-10-02 18:01:07 +010031#include <drm/ttm/ttm_placement.h>
32#include <drm/ttm/ttm_bo_driver.h>
33#include <drm/ttm/ttm_object.h>
34#include <drm/ttm/ttm_module.h>
Thomas Hellstromd92d9852013-10-24 01:49:26 -070035#include <linux/dma_remapping.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000036
37#define VMWGFX_DRIVER_NAME "vmwgfx"
38#define VMWGFX_DRIVER_DESC "Linux drm driver for VMware graphics devices"
39#define VMWGFX_CHIP_SVGAII 0
40#define VMW_FB_RESERVATION 0
41
Jakob Bornecrantzeb4f9232012-02-09 16:56:46 +010042#define VMW_MIN_INITIAL_WIDTH 800
43#define VMW_MIN_INITIAL_HEIGHT 600
44
45
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000046/**
47 * Fully encoded drm commands. Might move to vmw_drm.h
48 */
49
50#define DRM_IOCTL_VMW_GET_PARAM \
51 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GET_PARAM, \
52 struct drm_vmw_getparam_arg)
53#define DRM_IOCTL_VMW_ALLOC_DMABUF \
54 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_ALLOC_DMABUF, \
55 union drm_vmw_alloc_dmabuf_arg)
56#define DRM_IOCTL_VMW_UNREF_DMABUF \
57 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_DMABUF, \
58 struct drm_vmw_unref_dmabuf_arg)
59#define DRM_IOCTL_VMW_CURSOR_BYPASS \
60 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_CURSOR_BYPASS, \
61 struct drm_vmw_cursor_bypass_arg)
62
63#define DRM_IOCTL_VMW_CONTROL_STREAM \
64 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_CONTROL_STREAM, \
65 struct drm_vmw_control_stream_arg)
66#define DRM_IOCTL_VMW_CLAIM_STREAM \
67 DRM_IOR(DRM_COMMAND_BASE + DRM_VMW_CLAIM_STREAM, \
68 struct drm_vmw_stream_arg)
69#define DRM_IOCTL_VMW_UNREF_STREAM \
70 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_STREAM, \
71 struct drm_vmw_stream_arg)
72
73#define DRM_IOCTL_VMW_CREATE_CONTEXT \
74 DRM_IOR(DRM_COMMAND_BASE + DRM_VMW_CREATE_CONTEXT, \
75 struct drm_vmw_context_arg)
76#define DRM_IOCTL_VMW_UNREF_CONTEXT \
77 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_CONTEXT, \
78 struct drm_vmw_context_arg)
79#define DRM_IOCTL_VMW_CREATE_SURFACE \
80 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_SURFACE, \
81 union drm_vmw_surface_create_arg)
82#define DRM_IOCTL_VMW_UNREF_SURFACE \
83 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_SURFACE, \
84 struct drm_vmw_surface_arg)
85#define DRM_IOCTL_VMW_REF_SURFACE \
86 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_REF_SURFACE, \
87 union drm_vmw_surface_reference_arg)
88#define DRM_IOCTL_VMW_EXECBUF \
89 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_EXECBUF, \
90 struct drm_vmw_execbuf_arg)
Thomas Hellstromae2a1042011-09-01 20:18:44 +000091#define DRM_IOCTL_VMW_GET_3D_CAP \
92 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_GET_3D_CAP, \
93 struct drm_vmw_get_3d_cap_arg)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000094#define DRM_IOCTL_VMW_FENCE_WAIT \
95 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_FENCE_WAIT, \
96 struct drm_vmw_fence_wait_arg)
Thomas Hellstromae2a1042011-09-01 20:18:44 +000097#define DRM_IOCTL_VMW_FENCE_SIGNALED \
98 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_FENCE_SIGNALED, \
99 struct drm_vmw_fence_signaled_arg)
100#define DRM_IOCTL_VMW_FENCE_UNREF \
101 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_FENCE_UNREF, \
102 struct drm_vmw_fence_arg)
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200103#define DRM_IOCTL_VMW_FENCE_EVENT \
104 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_FENCE_EVENT, \
105 struct drm_vmw_fence_event_arg)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200106#define DRM_IOCTL_VMW_PRESENT \
107 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_PRESENT, \
108 struct drm_vmw_present_arg)
109#define DRM_IOCTL_VMW_PRESENT_READBACK \
110 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_PRESENT_READBACK, \
111 struct drm_vmw_present_readback_arg)
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +0200112#define DRM_IOCTL_VMW_UPDATE_LAYOUT \
113 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UPDATE_LAYOUT, \
114 struct drm_vmw_update_layout_arg)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000115
116/**
117 * The core DRM version of this macro doesn't account for
118 * DRM_COMMAND_BASE.
119 */
120
121#define VMW_IOCTL_DEF(ioctl, func, flags) \
Dave Airlie1b2f1482010-08-14 20:20:34 +1000122 [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = {DRM_##ioctl, flags, func, DRM_IOCTL_##ioctl}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000123
124/**
125 * Ioctl definitions.
126 */
127
Rob Clarkbaa70942013-08-02 13:27:49 -0400128static const struct drm_ioctl_desc vmw_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +1000129 VMW_IOCTL_DEF(VMW_GET_PARAM, vmw_getparam_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100130 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000131 VMW_IOCTL_DEF(VMW_ALLOC_DMABUF, vmw_dmabuf_alloc_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100132 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000133 VMW_IOCTL_DEF(VMW_UNREF_DMABUF, vmw_dmabuf_unref_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100134 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000135 VMW_IOCTL_DEF(VMW_CURSOR_BYPASS,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100136 vmw_kms_cursor_bypass_ioctl,
137 DRM_MASTER | DRM_CONTROL_ALLOW | DRM_UNLOCKED),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000138
Dave Airlie1b2f1482010-08-14 20:20:34 +1000139 VMW_IOCTL_DEF(VMW_CONTROL_STREAM, vmw_overlay_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100140 DRM_MASTER | DRM_CONTROL_ALLOW | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000141 VMW_IOCTL_DEF(VMW_CLAIM_STREAM, vmw_stream_claim_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100142 DRM_MASTER | DRM_CONTROL_ALLOW | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000143 VMW_IOCTL_DEF(VMW_UNREF_STREAM, vmw_stream_unref_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100144 DRM_MASTER | DRM_CONTROL_ALLOW | DRM_UNLOCKED),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000145
Dave Airlie1b2f1482010-08-14 20:20:34 +1000146 VMW_IOCTL_DEF(VMW_CREATE_CONTEXT, vmw_context_define_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100147 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000148 VMW_IOCTL_DEF(VMW_UNREF_CONTEXT, vmw_context_destroy_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100149 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000150 VMW_IOCTL_DEF(VMW_CREATE_SURFACE, vmw_surface_define_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100151 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000152 VMW_IOCTL_DEF(VMW_UNREF_SURFACE, vmw_surface_destroy_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100153 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000154 VMW_IOCTL_DEF(VMW_REF_SURFACE, vmw_surface_reference_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100155 DRM_AUTH | DRM_UNLOCKED),
Dave Airlie1b2f1482010-08-14 20:20:34 +1000156 VMW_IOCTL_DEF(VMW_EXECBUF, vmw_execbuf_ioctl,
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100157 DRM_AUTH | DRM_UNLOCKED),
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000158 VMW_IOCTL_DEF(VMW_FENCE_WAIT, vmw_fence_obj_wait_ioctl,
159 DRM_AUTH | DRM_UNLOCKED),
160 VMW_IOCTL_DEF(VMW_FENCE_SIGNALED,
161 vmw_fence_obj_signaled_ioctl,
162 DRM_AUTH | DRM_UNLOCKED),
163 VMW_IOCTL_DEF(VMW_FENCE_UNREF, vmw_fence_obj_unref_ioctl,
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +0200164 DRM_AUTH | DRM_UNLOCKED),
Thomas Hellstrom57c5ee72011-10-10 12:23:26 +0200165 VMW_IOCTL_DEF(VMW_FENCE_EVENT,
166 vmw_fence_event_ioctl,
167 DRM_AUTH | DRM_UNLOCKED),
Thomas Hellstromf63f6a52011-09-01 20:18:41 +0000168 VMW_IOCTL_DEF(VMW_GET_3D_CAP, vmw_get_cap_3d_ioctl,
169 DRM_AUTH | DRM_UNLOCKED),
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200170
171 /* these allow direct access to the framebuffers mark as master only */
172 VMW_IOCTL_DEF(VMW_PRESENT, vmw_present_ioctl,
173 DRM_MASTER | DRM_AUTH | DRM_UNLOCKED),
174 VMW_IOCTL_DEF(VMW_PRESENT_READBACK,
175 vmw_present_readback_ioctl,
176 DRM_MASTER | DRM_AUTH | DRM_UNLOCKED),
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +0200177 VMW_IOCTL_DEF(VMW_UPDATE_LAYOUT,
178 vmw_kms_update_layout_ioctl,
179 DRM_MASTER | DRM_UNLOCKED),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000180};
181
182static struct pci_device_id vmw_pci_id_list[] = {
183 {0x15ad, 0x0405, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VMWGFX_CHIP_SVGAII},
184 {0, 0, 0}
185};
Dave Airliec4903422012-08-28 21:40:51 -0400186MODULE_DEVICE_TABLE(pci, vmw_pci_id_list);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000187
Dave Airlie5d2afab2012-08-28 21:38:49 -0400188static int enable_fbdev = IS_ENABLED(CONFIG_DRM_VMWGFX_FBCON);
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700189static int vmw_force_iommu;
190static int vmw_restrict_iommu;
191static int vmw_force_coherent;
Thomas Hellstrom0d00c482014-01-15 20:19:53 +0100192static int vmw_restrict_dma_mask;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000193
194static int vmw_probe(struct pci_dev *, const struct pci_device_id *);
195static void vmw_master_init(struct vmw_master *);
Thomas Hellstromd9f36a02010-01-13 22:28:43 +0100196static int vmwgfx_pm_notifier(struct notifier_block *nb, unsigned long val,
197 void *ptr);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000198
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200199MODULE_PARM_DESC(enable_fbdev, "Enable vmwgfx fbdev");
200module_param_named(enable_fbdev, enable_fbdev, int, 0600);
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700201MODULE_PARM_DESC(force_dma_api, "Force using the DMA API for TTM pages");
202module_param_named(force_dma_api, vmw_force_iommu, int, 0600);
203MODULE_PARM_DESC(restrict_iommu, "Try to limit IOMMU usage for TTM pages");
204module_param_named(restrict_iommu, vmw_restrict_iommu, int, 0600);
205MODULE_PARM_DESC(force_coherent, "Force coherent TTM pages");
206module_param_named(force_coherent, vmw_force_coherent, int, 0600);
Thomas Hellstrom0d00c482014-01-15 20:19:53 +0100207MODULE_PARM_DESC(restrict_dma_mask, "Restrict DMA mask to 44 bits with IOMMU");
208module_param_named(restrict_dma_mask, vmw_restrict_dma_mask, int, 0600);
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700209
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200210
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000211static void vmw_print_capabilities(uint32_t capabilities)
212{
213 DRM_INFO("Capabilities:\n");
214 if (capabilities & SVGA_CAP_RECT_COPY)
215 DRM_INFO(" Rect copy.\n");
216 if (capabilities & SVGA_CAP_CURSOR)
217 DRM_INFO(" Cursor.\n");
218 if (capabilities & SVGA_CAP_CURSOR_BYPASS)
219 DRM_INFO(" Cursor bypass.\n");
220 if (capabilities & SVGA_CAP_CURSOR_BYPASS_2)
221 DRM_INFO(" Cursor bypass 2.\n");
222 if (capabilities & SVGA_CAP_8BIT_EMULATION)
223 DRM_INFO(" 8bit emulation.\n");
224 if (capabilities & SVGA_CAP_ALPHA_CURSOR)
225 DRM_INFO(" Alpha cursor.\n");
226 if (capabilities & SVGA_CAP_3D)
227 DRM_INFO(" 3D.\n");
228 if (capabilities & SVGA_CAP_EXTENDED_FIFO)
229 DRM_INFO(" Extended Fifo.\n");
230 if (capabilities & SVGA_CAP_MULTIMON)
231 DRM_INFO(" Multimon.\n");
232 if (capabilities & SVGA_CAP_PITCHLOCK)
233 DRM_INFO(" Pitchlock.\n");
234 if (capabilities & SVGA_CAP_IRQMASK)
235 DRM_INFO(" Irq mask.\n");
236 if (capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)
237 DRM_INFO(" Display Topology.\n");
238 if (capabilities & SVGA_CAP_GMR)
239 DRM_INFO(" GMR.\n");
240 if (capabilities & SVGA_CAP_TRACES)
241 DRM_INFO(" Traces.\n");
Thomas Hellstromdcca2862011-08-31 07:42:51 +0000242 if (capabilities & SVGA_CAP_GMR2)
243 DRM_INFO(" GMR2.\n");
244 if (capabilities & SVGA_CAP_SCREEN_OBJECT_2)
245 DRM_INFO(" Screen Object 2.\n");
Thomas Hellstromc1234db2012-11-21 10:35:08 +0100246 if (capabilities & SVGA_CAP_COMMAND_BUFFERS)
247 DRM_INFO(" Command Buffers.\n");
248 if (capabilities & SVGA_CAP_CMD_BUFFERS_2)
249 DRM_INFO(" Command Buffers 2.\n");
250 if (capabilities & SVGA_CAP_GBOBJECTS)
251 DRM_INFO(" Guest Backed Resources.\n");
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000252}
253
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200254
255/**
256 * vmw_execbuf_prepare_dummy_query - Initialize a query result structure at
257 * the start of a buffer object.
258 *
259 * @dev_priv: The device private structure.
260 *
261 * This function will idle the buffer using an uninterruptible wait, then
262 * map the first page and initialize a pending occlusion query result structure,
263 * Finally it will unmap the buffer.
264 *
265 * TODO: Since we're only mapping a single page, we should optimize the map
266 * to use kmap_atomic / iomap_atomic.
267 */
268static void vmw_dummy_query_bo_prepare(struct vmw_private *dev_priv)
269{
270 struct ttm_bo_kmap_obj map;
271 volatile SVGA3dQueryResult *result;
272 bool dummy;
273 int ret;
274 struct ttm_bo_device *bdev = &dev_priv->bdev;
275 struct ttm_buffer_object *bo = dev_priv->dummy_query_bo;
276
277 ttm_bo_reserve(bo, false, false, false, 0);
278 spin_lock(&bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +0200279 ret = ttm_bo_wait(bo, false, false, false);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200280 spin_unlock(&bdev->fence_lock);
281 if (unlikely(ret != 0))
282 (void) vmw_fallback_wait(dev_priv, false, true, 0, false,
283 10*HZ);
284
285 ret = ttm_bo_kmap(bo, 0, 1, &map);
286 if (likely(ret == 0)) {
287 result = ttm_kmap_obj_virtual(&map, &dummy);
288 result->totalSize = sizeof(*result);
289 result->state = SVGA3D_QUERYSTATE_PENDING;
290 result->result32 = 0xff;
291 ttm_bo_kunmap(&map);
292 } else
293 DRM_ERROR("Dummy query buffer map failed.\n");
294 ttm_bo_unreserve(bo);
295}
296
297
298/**
299 * vmw_dummy_query_bo_create - create a bo to hold a dummy query result
300 *
301 * @dev_priv: A device private structure.
302 *
303 * This function creates a small buffer object that holds the query
304 * result for dummy queries emitted as query barriers.
305 * No interruptible waits are done within this function.
306 *
307 * Returns an error if bo creation fails.
308 */
309static int vmw_dummy_query_bo_create(struct vmw_private *dev_priv)
310{
311 return ttm_bo_create(&dev_priv->bdev,
312 PAGE_SIZE,
313 ttm_bo_type_device,
314 &vmw_vram_sys_placement,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +0000315 0, false, NULL,
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200316 &dev_priv->dummy_query_bo);
317}
318
319
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000320static int vmw_request_device(struct vmw_private *dev_priv)
321{
322 int ret;
323
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000324 ret = vmw_fifo_init(dev_priv, &dev_priv->fifo);
325 if (unlikely(ret != 0)) {
326 DRM_ERROR("Unable to initialize FIFO.\n");
327 return ret;
328 }
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000329 vmw_fence_fifo_up(dev_priv->fman);
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200330 ret = vmw_dummy_query_bo_create(dev_priv);
331 if (unlikely(ret != 0))
332 goto out_no_query_bo;
333 vmw_dummy_query_bo_prepare(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000334
335 return 0;
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200336
337out_no_query_bo:
338 vmw_fence_fifo_down(dev_priv->fman);
339 vmw_fifo_release(dev_priv, &dev_priv->fifo);
340 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000341}
342
343static void vmw_release_device(struct vmw_private *dev_priv)
344{
Thomas Hellstrome2fa3a72011-10-04 20:13:30 +0200345 /*
346 * Previous destructions should've released
347 * the pinned bo.
348 */
349
350 BUG_ON(dev_priv->pinned_bo != NULL);
351
352 ttm_bo_unref(&dev_priv->dummy_query_bo);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000353 vmw_fence_fifo_down(dev_priv->fman);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000354 vmw_fifo_release(dev_priv, &dev_priv->fifo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000355}
356
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000357/**
358 * Increase the 3d resource refcount.
359 * If the count was prevously zero, initialize the fifo, switching to svga
360 * mode. Note that the master holds a ref as well, and may request an
361 * explicit switch to svga mode if fb is not running, using @unhide_svga.
362 */
363int vmw_3d_resource_inc(struct vmw_private *dev_priv,
364 bool unhide_svga)
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200365{
366 int ret = 0;
367
368 mutex_lock(&dev_priv->release_mutex);
369 if (unlikely(dev_priv->num_3d_resources++ == 0)) {
370 ret = vmw_request_device(dev_priv);
371 if (unlikely(ret != 0))
372 --dev_priv->num_3d_resources;
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000373 } else if (unhide_svga) {
374 mutex_lock(&dev_priv->hw_mutex);
375 vmw_write(dev_priv, SVGA_REG_ENABLE,
376 vmw_read(dev_priv, SVGA_REG_ENABLE) &
377 ~SVGA_REG_ENABLE_HIDE);
378 mutex_unlock(&dev_priv->hw_mutex);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200379 }
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000380
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200381 mutex_unlock(&dev_priv->release_mutex);
382 return ret;
383}
384
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000385/**
386 * Decrease the 3d resource refcount.
387 * If the count reaches zero, disable the fifo, switching to vga mode.
388 * Note that the master holds a refcount as well, and may request an
389 * explicit switch to vga mode when it releases its refcount to account
390 * for the situation of an X server vt switch to VGA with 3d resources
391 * active.
392 */
393void vmw_3d_resource_dec(struct vmw_private *dev_priv,
394 bool hide_svga)
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200395{
396 int32_t n3d;
397
398 mutex_lock(&dev_priv->release_mutex);
399 if (unlikely(--dev_priv->num_3d_resources == 0))
400 vmw_release_device(dev_priv);
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000401 else if (hide_svga) {
402 mutex_lock(&dev_priv->hw_mutex);
403 vmw_write(dev_priv, SVGA_REG_ENABLE,
404 vmw_read(dev_priv, SVGA_REG_ENABLE) |
405 SVGA_REG_ENABLE_HIDE);
406 mutex_unlock(&dev_priv->hw_mutex);
407 }
408
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200409 n3d = (int32_t) dev_priv->num_3d_resources;
410 mutex_unlock(&dev_priv->release_mutex);
411
412 BUG_ON(n3d < 0);
413}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000414
Jakob Bornecrantzeb4f9232012-02-09 16:56:46 +0100415/**
416 * Sets the initial_[width|height] fields on the given vmw_private.
417 *
418 * It does so by reading SVGA_REG_[WIDTH|HEIGHT] regs and then
Thomas Hellstrom67d4a872012-02-09 16:56:47 +0100419 * clamping the value to fb_max_[width|height] fields and the
420 * VMW_MIN_INITIAL_[WIDTH|HEIGHT].
421 * If the values appear to be invalid, set them to
Jakob Bornecrantzeb4f9232012-02-09 16:56:46 +0100422 * VMW_MIN_INITIAL_[WIDTH|HEIGHT].
423 */
424static void vmw_get_initial_size(struct vmw_private *dev_priv)
425{
426 uint32_t width;
427 uint32_t height;
428
429 width = vmw_read(dev_priv, SVGA_REG_WIDTH);
430 height = vmw_read(dev_priv, SVGA_REG_HEIGHT);
431
432 width = max_t(uint32_t, width, VMW_MIN_INITIAL_WIDTH);
Jakob Bornecrantzeb4f9232012-02-09 16:56:46 +0100433 height = max_t(uint32_t, height, VMW_MIN_INITIAL_HEIGHT);
Thomas Hellstrom67d4a872012-02-09 16:56:47 +0100434
435 if (width > dev_priv->fb_max_width ||
436 height > dev_priv->fb_max_height) {
437
438 /*
439 * This is a host error and shouldn't occur.
440 */
441
442 width = VMW_MIN_INITIAL_WIDTH;
443 height = VMW_MIN_INITIAL_HEIGHT;
444 }
Jakob Bornecrantzeb4f9232012-02-09 16:56:46 +0100445
446 dev_priv->initial_width = width;
447 dev_priv->initial_height = height;
448}
449
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700450/**
451 * vmw_dma_select_mode - Determine how DMA mappings should be set up for this
452 * system.
453 *
454 * @dev_priv: Pointer to a struct vmw_private
455 *
456 * This functions tries to determine the IOMMU setup and what actions
457 * need to be taken by the driver to make system pages visible to the
458 * device.
459 * If this function decides that DMA is not possible, it returns -EINVAL.
460 * The driver may then try to disable features of the device that require
461 * DMA.
462 */
463static int vmw_dma_select_mode(struct vmw_private *dev_priv)
464{
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700465 static const char *names[vmw_dma_map_max] = {
466 [vmw_dma_phys] = "Using physical TTM page addresses.",
467 [vmw_dma_alloc_coherent] = "Using coherent TTM pages.",
468 [vmw_dma_map_populate] = "Keeping DMA mappings.",
469 [vmw_dma_map_bind] = "Giving up DMA mappings early."};
Thomas Hellstrome14cd952013-11-11 23:49:26 -0800470#ifdef CONFIG_X86
471 const struct dma_map_ops *dma_ops = get_dma_ops(dev_priv->dev->dev);
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700472
473#ifdef CONFIG_INTEL_IOMMU
474 if (intel_iommu_enabled) {
475 dev_priv->map_mode = vmw_dma_map_populate;
476 goto out_fixup;
477 }
478#endif
479
480 if (!(vmw_force_iommu || vmw_force_coherent)) {
481 dev_priv->map_mode = vmw_dma_phys;
482 DRM_INFO("DMA map mode: %s\n", names[dev_priv->map_mode]);
483 return 0;
484 }
485
486 dev_priv->map_mode = vmw_dma_map_populate;
487
488 if (dma_ops->sync_single_for_cpu)
489 dev_priv->map_mode = vmw_dma_alloc_coherent;
490#ifdef CONFIG_SWIOTLB
491 if (swiotlb_nr_tbl() == 0)
492 dev_priv->map_mode = vmw_dma_map_populate;
493#endif
494
Dave Airlie21136942013-11-08 16:12:42 +1000495#ifdef CONFIG_INTEL_IOMMU
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700496out_fixup:
Dave Airlie21136942013-11-08 16:12:42 +1000497#endif
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700498 if (dev_priv->map_mode == vmw_dma_map_populate &&
499 vmw_restrict_iommu)
500 dev_priv->map_mode = vmw_dma_map_bind;
501
502 if (vmw_force_coherent)
503 dev_priv->map_mode = vmw_dma_alloc_coherent;
504
505#if !defined(CONFIG_SWIOTLB) && !defined(CONFIG_INTEL_IOMMU)
506 /*
507 * No coherent page pool
508 */
509 if (dev_priv->map_mode == vmw_dma_alloc_coherent)
510 return -EINVAL;
511#endif
512
Thomas Hellstrome14cd952013-11-11 23:49:26 -0800513#else /* CONFIG_X86 */
514 dev_priv->map_mode = vmw_dma_map_populate;
515#endif /* CONFIG_X86 */
516
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700517 DRM_INFO("DMA map mode: %s\n", names[dev_priv->map_mode]);
518
519 return 0;
520}
521
Thomas Hellstrom0d00c482014-01-15 20:19:53 +0100522/**
523 * vmw_dma_masks - set required page- and dma masks
524 *
525 * @dev: Pointer to struct drm-device
526 *
527 * With 32-bit we can only handle 32 bit PFNs. Optionally set that
528 * restriction also for 64-bit systems.
529 */
530#ifdef CONFIG_INTEL_IOMMU
531static int vmw_dma_masks(struct vmw_private *dev_priv)
532{
533 struct drm_device *dev = dev_priv->dev;
534
535 if (intel_iommu_enabled &&
536 (sizeof(unsigned long) == 4 || vmw_restrict_dma_mask)) {
537 DRM_INFO("Restricting DMA addresses to 44 bits.\n");
538 return dma_set_mask(dev->dev, DMA_BIT_MASK(44));
539 }
540 return 0;
541}
542#else
543static int vmw_dma_masks(struct vmw_private *dev_priv)
544{
545 return 0;
546}
547#endif
548
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000549static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
550{
551 struct vmw_private *dev_priv;
552 int ret;
Peter Hanzelc1886602010-01-30 03:38:07 +0000553 uint32_t svga_id;
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000554 enum vmw_res_type i;
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700555 bool refuse_dma = false;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000556
557 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
558 if (unlikely(dev_priv == NULL)) {
559 DRM_ERROR("Failed allocating a device private struct.\n");
560 return -ENOMEM;
561 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000562
Dave Airlie466e69b2011-12-19 11:15:29 +0000563 pci_set_master(dev->pdev);
564
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000565 dev_priv->dev = dev;
566 dev_priv->vmw_chipset = chipset;
Thomas Hellstrom6bcd8d3c2011-09-01 20:18:42 +0000567 dev_priv->last_read_seqno = (uint32_t) -100;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000568 mutex_init(&dev_priv->hw_mutex);
569 mutex_init(&dev_priv->cmdbuf_mutex);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200570 mutex_init(&dev_priv->release_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000571 rwlock_init(&dev_priv->resource_lock);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000572
573 for (i = vmw_res_context; i < vmw_res_max; ++i) {
574 idr_init(&dev_priv->res_idr[i]);
575 INIT_LIST_HEAD(&dev_priv->res_lru[i]);
576 }
577
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000578 mutex_init(&dev_priv->init_mutex);
579 init_waitqueue_head(&dev_priv->fence_queue);
580 init_waitqueue_head(&dev_priv->fifo_queue);
Thomas Hellstrom4f73a962011-09-01 20:18:43 +0000581 dev_priv->fence_queue_waiters = 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000582 atomic_set(&dev_priv->fifo_queue_waiters, 0);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000583
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200584 dev_priv->used_memory_size = 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000585
586 dev_priv->io_start = pci_resource_start(dev->pdev, 0);
587 dev_priv->vram_start = pci_resource_start(dev->pdev, 1);
588 dev_priv->mmio_start = pci_resource_start(dev->pdev, 2);
589
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200590 dev_priv->enable_fb = enable_fbdev;
591
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000592 mutex_lock(&dev_priv->hw_mutex);
Peter Hanzelc1886602010-01-30 03:38:07 +0000593
594 vmw_write(dev_priv, SVGA_REG_ID, SVGA_ID_2);
595 svga_id = vmw_read(dev_priv, SVGA_REG_ID);
596 if (svga_id != SVGA_ID_2) {
597 ret = -ENOSYS;
Masanari Iida49625902012-02-05 22:50:36 +0900598 DRM_ERROR("Unsupported SVGA ID 0x%x\n", svga_id);
Peter Hanzelc1886602010-01-30 03:38:07 +0000599 mutex_unlock(&dev_priv->hw_mutex);
600 goto out_err0;
601 }
602
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000603 dev_priv->capabilities = vmw_read(dev_priv, SVGA_REG_CAPABILITIES);
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700604 ret = vmw_dma_select_mode(dev_priv);
605 if (unlikely(ret != 0)) {
606 DRM_INFO("Restricting capabilities due to IOMMU setup.\n");
607 refuse_dma = true;
608 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000609
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200610 dev_priv->vram_size = vmw_read(dev_priv, SVGA_REG_VRAM_SIZE);
611 dev_priv->mmio_size = vmw_read(dev_priv, SVGA_REG_MEM_SIZE);
612 dev_priv->fb_max_width = vmw_read(dev_priv, SVGA_REG_MAX_WIDTH);
613 dev_priv->fb_max_height = vmw_read(dev_priv, SVGA_REG_MAX_HEIGHT);
Jakob Bornecrantzeb4f9232012-02-09 16:56:46 +0100614
615 vmw_get_initial_size(dev_priv);
616
Thomas Hellstrom0d00c482014-01-15 20:19:53 +0100617 if (dev_priv->capabilities & SVGA_CAP_GMR2) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000618 dev_priv->max_gmr_ids =
619 vmw_read(dev_priv, SVGA_REG_GMR_MAX_IDS);
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000620 dev_priv->max_gmr_pages =
621 vmw_read(dev_priv, SVGA_REG_GMRS_MAX_PAGES);
622 dev_priv->memory_size =
623 vmw_read(dev_priv, SVGA_REG_MEMORY_SIZE);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200624 dev_priv->memory_size -= dev_priv->vram_size;
625 } else {
626 /*
627 * An arbitrary limit of 512MiB on surface
628 * memory. But all HWV8 hardware supports GMR2.
629 */
630 dev_priv->memory_size = 512*1024*1024;
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000631 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000632
Thomas Hellstrom0d00c482014-01-15 20:19:53 +0100633 ret = vmw_dma_masks(dev_priv);
634 if (unlikely(ret != 0))
635 goto out_err0;
636
Thomas Hellstrombc2d6502012-11-21 10:32:36 +0100637 dev_priv->prim_bb_mem = dev_priv->vram_size;
638
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000639 mutex_unlock(&dev_priv->hw_mutex);
640
641 vmw_print_capabilities(dev_priv->capabilities);
642
Thomas Hellstrom0d00c482014-01-15 20:19:53 +0100643 if (dev_priv->capabilities & SVGA_CAP_GMR2) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000644 DRM_INFO("Max GMR ids is %u\n",
645 (unsigned)dev_priv->max_gmr_ids);
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000646 DRM_INFO("Max number of GMR pages is %u\n",
647 (unsigned)dev_priv->max_gmr_pages);
Thomas Hellstrom5bb39e82011-10-04 20:13:33 +0200648 DRM_INFO("Max dedicated hypervisor surface memory is %u kiB\n",
649 (unsigned)dev_priv->memory_size / 1024);
Thomas Hellstromfb17f182011-08-31 07:42:53 +0000650 }
Thomas Hellstrombc2d6502012-11-21 10:32:36 +0100651 DRM_INFO("Maximum display memory size is %u kiB\n",
652 dev_priv->prim_bb_mem / 1024);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000653 DRM_INFO("VRAM at 0x%08x size is %u kiB\n",
654 dev_priv->vram_start, dev_priv->vram_size / 1024);
655 DRM_INFO("MMIO at 0x%08x size is %u kiB\n",
656 dev_priv->mmio_start, dev_priv->mmio_size / 1024);
657
658 ret = vmw_ttm_global_init(dev_priv);
659 if (unlikely(ret != 0))
660 goto out_err0;
661
662
663 vmw_master_init(&dev_priv->fbdev_master);
664 ttm_lock_set_kill(&dev_priv->fbdev_master.lock, false, SIGTERM);
665 dev_priv->active_master = &dev_priv->fbdev_master;
666
Dave Airliea2c06ee2011-02-23 14:24:01 +1000667
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000668 ret = ttm_bo_device_init(&dev_priv->bdev,
669 dev_priv->bo_global_ref.ref.object,
670 &vmw_bo_driver, VMWGFX_FILE_PAGE_OFFSET,
671 false);
672 if (unlikely(ret != 0)) {
673 DRM_ERROR("Failed initializing TTM buffer object driver.\n");
674 goto out_err1;
675 }
676
677 ret = ttm_bo_init_mm(&dev_priv->bdev, TTM_PL_VRAM,
678 (dev_priv->vram_size >> PAGE_SHIFT));
679 if (unlikely(ret != 0)) {
680 DRM_ERROR("Failed initializing memory manager for VRAM.\n");
681 goto out_err2;
682 }
683
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200684 dev_priv->has_gmr = true;
Thomas Hellstromd92d9852013-10-24 01:49:26 -0700685 if (((dev_priv->capabilities & (SVGA_CAP_GMR | SVGA_CAP_GMR2)) == 0) ||
686 refuse_dma || ttm_bo_init_mm(&dev_priv->bdev, VMW_PL_GMR,
687 dev_priv->max_gmr_ids) != 0) {
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200688 DRM_INFO("No GMR memory available. "
689 "Graphics memory resources are very limited.\n");
690 dev_priv->has_gmr = false;
691 }
692
Andy Lutomirski247d36d2013-05-13 23:58:41 +0000693 dev_priv->mmio_mtrr = arch_phys_wc_add(dev_priv->mmio_start,
694 dev_priv->mmio_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000695
696 dev_priv->mmio_virt = ioremap_wc(dev_priv->mmio_start,
697 dev_priv->mmio_size);
698
699 if (unlikely(dev_priv->mmio_virt == NULL)) {
700 ret = -ENOMEM;
701 DRM_ERROR("Failed mapping MMIO.\n");
702 goto out_err3;
703 }
704
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200705 /* Need mmio memory to check for fifo pitchlock cap. */
706 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY) &&
707 !(dev_priv->capabilities & SVGA_CAP_PITCHLOCK) &&
708 !vmw_fifo_have_pitchlock(dev_priv)) {
709 ret = -ENOSYS;
710 DRM_ERROR("Hardware has no pitchlock\n");
711 goto out_err4;
712 }
713
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000714 dev_priv->tdev = ttm_object_device_init
Thomas Hellstrom69977ff2013-11-13 01:50:46 -0800715 (dev_priv->mem_global_ref.object, 12, &vmw_prime_dmabuf_ops);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000716
717 if (unlikely(dev_priv->tdev == NULL)) {
718 DRM_ERROR("Unable to initialize TTM object management.\n");
719 ret = -ENOMEM;
720 goto out_err4;
721 }
722
723 dev->dev_private = dev_priv;
724
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000725 ret = pci_request_regions(dev->pdev, "vmwgfx probe");
726 dev_priv->stealth = (ret != 0);
727 if (dev_priv->stealth) {
728 /**
729 * Request at least the mmio PCI resource.
730 */
731
732 DRM_INFO("It appears like vesafb is loaded. "
Thomas Hellstromf2d12b82010-02-15 14:45:22 +0000733 "Ignore above error if any.\n");
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000734 ret = pci_request_region(dev->pdev, 2, "vmwgfx stealth probe");
735 if (unlikely(ret != 0)) {
736 DRM_ERROR("Failed reserving the SVGA MMIO resource.\n");
737 goto out_no_device;
738 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000739 }
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000740
Thomas Hellstrom506ff752012-11-09 12:26:14 +0000741 if (dev_priv->capabilities & SVGA_CAP_IRQMASK) {
742 ret = drm_irq_install(dev);
743 if (ret != 0) {
744 DRM_ERROR("Failed installing irq: %d\n", ret);
745 goto out_no_irq;
746 }
747 }
748
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000749 dev_priv->fman = vmw_fence_manager_init(dev_priv);
Wei Yongjun14bbf202013-08-26 15:15:37 +0800750 if (unlikely(dev_priv->fman == NULL)) {
751 ret = -ENOMEM;
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000752 goto out_no_fman;
Wei Yongjun14bbf202013-08-26 15:15:37 +0800753 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200754
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200755 vmw_kms_save_vga(dev_priv);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200756
757 /* Start kms and overlay systems, needs fifo. */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +0200758 ret = vmw_kms_init(dev_priv);
759 if (unlikely(ret != 0))
760 goto out_no_kms;
Thomas Hellstromf2d12b82010-02-15 14:45:22 +0000761 vmw_overlay_init(dev_priv);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200762
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200763 if (dev_priv->enable_fb) {
Thomas Hellstrom506ff752012-11-09 12:26:14 +0000764 ret = vmw_3d_resource_inc(dev_priv, true);
765 if (unlikely(ret != 0))
766 goto out_no_fifo;
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200767 vmw_fb_init(dev_priv);
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +0200768 }
769
Thomas Hellstromd9f36a02010-01-13 22:28:43 +0100770 dev_priv->pm_nb.notifier_call = vmwgfx_pm_notifier;
771 register_pm_notifier(&dev_priv->pm_nb);
772
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000773 return 0;
774
Thomas Hellstrom506ff752012-11-09 12:26:14 +0000775out_no_fifo:
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200776 vmw_overlay_close(dev_priv);
777 vmw_kms_close(dev_priv);
778out_no_kms:
Thomas Hellstrom506ff752012-11-09 12:26:14 +0000779 vmw_kms_restore_vga(dev_priv);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000780 vmw_fence_manager_takedown(dev_priv->fman);
781out_no_fman:
Thomas Hellstrom506ff752012-11-09 12:26:14 +0000782 if (dev_priv->capabilities & SVGA_CAP_IRQMASK)
783 drm_irq_uninstall(dev_priv->dev);
784out_no_irq:
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200785 if (dev_priv->stealth)
786 pci_release_region(dev->pdev, 2);
787 else
788 pci_release_regions(dev->pdev);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000789out_no_device:
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000790 ttm_object_device_release(&dev_priv->tdev);
791out_err4:
792 iounmap(dev_priv->mmio_virt);
793out_err3:
Andy Lutomirski247d36d2013-05-13 23:58:41 +0000794 arch_phys_wc_del(dev_priv->mmio_mtrr);
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200795 if (dev_priv->has_gmr)
796 (void) ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_GMR);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000797 (void)ttm_bo_clean_mm(&dev_priv->bdev, TTM_PL_VRAM);
798out_err2:
799 (void)ttm_bo_device_release(&dev_priv->bdev);
800out_err1:
801 vmw_ttm_global_release(dev_priv);
802out_err0:
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000803 for (i = vmw_res_context; i < vmw_res_max; ++i)
804 idr_destroy(&dev_priv->res_idr[i]);
805
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000806 kfree(dev_priv);
807 return ret;
808}
809
810static int vmw_driver_unload(struct drm_device *dev)
811{
812 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000813 enum vmw_res_type i;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000814
Thomas Hellstromd9f36a02010-01-13 22:28:43 +0100815 unregister_pm_notifier(&dev_priv->pm_nb);
816
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000817 if (dev_priv->ctx.res_ht_initialized)
818 drm_ht_remove(&dev_priv->ctx.res_ht);
Thomas Hellstrombe38ab62011-08-31 07:42:54 +0000819 if (dev_priv->ctx.cmd_bounce)
820 vfree(dev_priv->ctx.cmd_bounce);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200821 if (dev_priv->enable_fb) {
822 vmw_fb_close(dev_priv);
823 vmw_kms_restore_vga(dev_priv);
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000824 vmw_3d_resource_dec(dev_priv, false);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200825 }
Thomas Hellstromf2d12b82010-02-15 14:45:22 +0000826 vmw_kms_close(dev_priv);
827 vmw_overlay_close(dev_priv);
Thomas Hellstromae2a1042011-09-01 20:18:44 +0000828 vmw_fence_manager_takedown(dev_priv->fman);
Thomas Hellstrom506ff752012-11-09 12:26:14 +0000829 if (dev_priv->capabilities & SVGA_CAP_IRQMASK)
830 drm_irq_uninstall(dev_priv->dev);
Thomas Hellstromf2d12b82010-02-15 14:45:22 +0000831 if (dev_priv->stealth)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000832 pci_release_region(dev->pdev, 2);
Thomas Hellstromf2d12b82010-02-15 14:45:22 +0000833 else
834 pci_release_regions(dev->pdev);
835
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000836 ttm_object_device_release(&dev_priv->tdev);
837 iounmap(dev_priv->mmio_virt);
Andy Lutomirski247d36d2013-05-13 23:58:41 +0000838 arch_phys_wc_del(dev_priv->mmio_mtrr);
Thomas Hellstrom135cba02010-10-26 21:21:47 +0200839 if (dev_priv->has_gmr)
840 (void)ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_GMR);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000841 (void)ttm_bo_clean_mm(&dev_priv->bdev, TTM_PL_VRAM);
842 (void)ttm_bo_device_release(&dev_priv->bdev);
843 vmw_ttm_global_release(dev_priv);
Thomas Hellstromc0951b72012-11-20 12:19:35 +0000844
845 for (i = vmw_res_context; i < vmw_res_max; ++i)
846 idr_destroy(&dev_priv->res_idr[i]);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000847
848 kfree(dev_priv);
849
850 return 0;
851}
852
Thomas Hellstrom6b82ef52012-02-09 16:56:42 +0100853static void vmw_preclose(struct drm_device *dev,
854 struct drm_file *file_priv)
855{
856 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
857 struct vmw_private *dev_priv = vmw_priv(dev);
858
859 vmw_event_fence_fpriv_gone(dev_priv->fman, &vmw_fp->fence_events);
860}
861
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000862static void vmw_postclose(struct drm_device *dev,
863 struct drm_file *file_priv)
864{
865 struct vmw_fpriv *vmw_fp;
866
867 vmw_fp = vmw_fpriv(file_priv);
Thomas Hellstromc4249852013-10-09 01:42:51 -0700868
869 if (vmw_fp->locked_master) {
870 struct vmw_master *vmaster =
871 vmw_master(vmw_fp->locked_master);
872
873 ttm_lock_set_kill(&vmaster->lock, true, SIGTERM);
874 ttm_vt_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000875 drm_master_put(&vmw_fp->locked_master);
Thomas Hellstromc4249852013-10-09 01:42:51 -0700876 }
877
878 ttm_object_file_release(&vmw_fp->tfile);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000879 kfree(vmw_fp);
880}
881
882static int vmw_driver_open(struct drm_device *dev, struct drm_file *file_priv)
883{
884 struct vmw_private *dev_priv = vmw_priv(dev);
885 struct vmw_fpriv *vmw_fp;
886 int ret = -ENOMEM;
887
888 vmw_fp = kzalloc(sizeof(*vmw_fp), GFP_KERNEL);
889 if (unlikely(vmw_fp == NULL))
890 return ret;
891
Thomas Hellstrom6b82ef52012-02-09 16:56:42 +0100892 INIT_LIST_HEAD(&vmw_fp->fence_events);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000893 vmw_fp->tfile = ttm_object_file_init(dev_priv->tdev, 10);
894 if (unlikely(vmw_fp->tfile == NULL))
895 goto out_no_tfile;
896
897 file_priv->driver_priv = vmw_fp;
Ilija Hadzic949c4a32012-05-15 16:40:10 -0400898 dev_priv->bdev.dev_mapping = dev->dev_mapping;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000899
900 return 0;
901
902out_no_tfile:
903 kfree(vmw_fp);
904 return ret;
905}
906
907static long vmw_unlocked_ioctl(struct file *filp, unsigned int cmd,
908 unsigned long arg)
909{
910 struct drm_file *file_priv = filp->private_data;
911 struct drm_device *dev = file_priv->minor->dev;
912 unsigned int nr = DRM_IOCTL_NR(cmd);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000913
914 /*
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100915 * Do extra checking on driver private ioctls.
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000916 */
917
918 if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END)
919 && (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
Rob Clarkbaa70942013-08-02 13:27:49 -0400920 const struct drm_ioctl_desc *ioctl =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000921 &vmw_ioctls[nr - DRM_COMMAND_BASE];
922
Thomas Hellstrom2854eed2010-09-30 12:18:33 +0200923 if (unlikely(ioctl->cmd_drv != cmd)) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000924 DRM_ERROR("Invalid command format, ioctl %d\n",
925 nr - DRM_COMMAND_BASE);
926 return -EINVAL;
927 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000928 }
929
Thomas Hellstrome1f78002009-12-08 12:57:51 +0100930 return drm_ioctl(filp, cmd, arg);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000931}
932
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000933static void vmw_lastclose(struct drm_device *dev)
934{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000935 struct drm_crtc *crtc;
936 struct drm_mode_set set;
937 int ret;
938
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000939 set.x = 0;
940 set.y = 0;
941 set.fb = NULL;
942 set.mode = NULL;
943 set.connectors = NULL;
944 set.num_connectors = 0;
945
946 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
947 set.crtc = crtc;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100948 ret = drm_mode_set_config_internal(&set);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000949 WARN_ON(ret != 0);
950 }
951
952}
953
954static void vmw_master_init(struct vmw_master *vmaster)
955{
956 ttm_lock_init(&vmaster->lock);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200957 INIT_LIST_HEAD(&vmaster->fb_surf);
958 mutex_init(&vmaster->fb_surf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000959}
960
961static int vmw_master_create(struct drm_device *dev,
962 struct drm_master *master)
963{
964 struct vmw_master *vmaster;
965
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000966 vmaster = kzalloc(sizeof(*vmaster), GFP_KERNEL);
967 if (unlikely(vmaster == NULL))
968 return -ENOMEM;
969
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200970 vmw_master_init(vmaster);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000971 ttm_lock_set_kill(&vmaster->lock, true, SIGTERM);
972 master->driver_priv = vmaster;
973
974 return 0;
975}
976
977static void vmw_master_destroy(struct drm_device *dev,
978 struct drm_master *master)
979{
980 struct vmw_master *vmaster = vmw_master(master);
981
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000982 master->driver_priv = NULL;
983 kfree(vmaster);
984}
985
986
987static int vmw_master_set(struct drm_device *dev,
988 struct drm_file *file_priv,
989 bool from_open)
990{
991 struct vmw_private *dev_priv = vmw_priv(dev);
992 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
993 struct vmw_master *active = dev_priv->active_master;
994 struct vmw_master *vmaster = vmw_master(file_priv->master);
995 int ret = 0;
996
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200997 if (!dev_priv->enable_fb) {
Thomas Hellstrom05730b32011-08-31 07:42:52 +0000998 ret = vmw_3d_resource_inc(dev_priv, true);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +0200999 if (unlikely(ret != 0))
1000 return ret;
1001 vmw_kms_save_vga(dev_priv);
1002 mutex_lock(&dev_priv->hw_mutex);
1003 vmw_write(dev_priv, SVGA_REG_TRACES, 0);
1004 mutex_unlock(&dev_priv->hw_mutex);
1005 }
1006
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001007 if (active) {
1008 BUG_ON(active != &dev_priv->fbdev_master);
1009 ret = ttm_vt_lock(&active->lock, false, vmw_fp->tfile);
1010 if (unlikely(ret != 0))
1011 goto out_no_active_lock;
1012
1013 ttm_lock_set_kill(&active->lock, true, SIGTERM);
1014 ret = ttm_bo_evict_mm(&dev_priv->bdev, TTM_PL_VRAM);
1015 if (unlikely(ret != 0)) {
1016 DRM_ERROR("Unable to clean VRAM on "
1017 "master drop.\n");
1018 }
1019
1020 dev_priv->active_master = NULL;
1021 }
1022
1023 ttm_lock_set_kill(&vmaster->lock, false, SIGTERM);
1024 if (!from_open) {
1025 ttm_vt_unlock(&vmaster->lock);
1026 BUG_ON(vmw_fp->locked_master != file_priv->master);
1027 drm_master_put(&vmw_fp->locked_master);
1028 }
1029
1030 dev_priv->active_master = vmaster;
1031
1032 return 0;
1033
1034out_no_active_lock:
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001035 if (!dev_priv->enable_fb) {
Thomas Hellstromba723fe82012-11-09 12:26:11 +00001036 vmw_kms_restore_vga(dev_priv);
1037 vmw_3d_resource_dec(dev_priv, true);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001038 mutex_lock(&dev_priv->hw_mutex);
1039 vmw_write(dev_priv, SVGA_REG_TRACES, 1);
1040 mutex_unlock(&dev_priv->hw_mutex);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001041 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001042 return ret;
1043}
1044
1045static void vmw_master_drop(struct drm_device *dev,
1046 struct drm_file *file_priv,
1047 bool from_release)
1048{
1049 struct vmw_private *dev_priv = vmw_priv(dev);
1050 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1051 struct vmw_master *vmaster = vmw_master(file_priv->master);
1052 int ret;
1053
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001054 /**
1055 * Make sure the master doesn't disappear while we have
1056 * it locked.
1057 */
1058
1059 vmw_fp->locked_master = drm_master_get(file_priv->master);
1060 ret = ttm_vt_lock(&vmaster->lock, false, vmw_fp->tfile);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001061 if (unlikely((ret != 0))) {
1062 DRM_ERROR("Unable to lock TTM at VT switch.\n");
1063 drm_master_put(&vmw_fp->locked_master);
1064 }
1065
Thomas Hellstromc4249852013-10-09 01:42:51 -07001066 ttm_lock_set_kill(&vmaster->lock, false, SIGTERM);
1067 vmw_execbuf_release_pinned_bo(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001068
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001069 if (!dev_priv->enable_fb) {
1070 ret = ttm_bo_evict_mm(&dev_priv->bdev, TTM_PL_VRAM);
1071 if (unlikely(ret != 0))
1072 DRM_ERROR("Unable to clean VRAM on master drop.\n");
Thomas Hellstromba723fe82012-11-09 12:26:11 +00001073 vmw_kms_restore_vga(dev_priv);
1074 vmw_3d_resource_dec(dev_priv, true);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001075 mutex_lock(&dev_priv->hw_mutex);
1076 vmw_write(dev_priv, SVGA_REG_TRACES, 1);
1077 mutex_unlock(&dev_priv->hw_mutex);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001078 }
1079
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001080 dev_priv->active_master = &dev_priv->fbdev_master;
1081 ttm_lock_set_kill(&dev_priv->fbdev_master.lock, false, SIGTERM);
1082 ttm_vt_unlock(&dev_priv->fbdev_master.lock);
1083
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001084 if (dev_priv->enable_fb)
1085 vmw_fb_on(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001086}
1087
1088
1089static void vmw_remove(struct pci_dev *pdev)
1090{
1091 struct drm_device *dev = pci_get_drvdata(pdev);
1092
1093 drm_put_dev(dev);
1094}
1095
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001096static int vmwgfx_pm_notifier(struct notifier_block *nb, unsigned long val,
1097 void *ptr)
1098{
1099 struct vmw_private *dev_priv =
1100 container_of(nb, struct vmw_private, pm_nb);
1101 struct vmw_master *vmaster = dev_priv->active_master;
1102
1103 switch (val) {
1104 case PM_HIBERNATION_PREPARE:
1105 case PM_SUSPEND_PREPARE:
1106 ttm_suspend_lock(&vmaster->lock);
1107
1108 /**
1109 * This empties VRAM and unbinds all GMR bindings.
1110 * Buffer contents is moved to swappable memory.
1111 */
Thomas Hellstromc0951b72012-11-20 12:19:35 +00001112 vmw_execbuf_release_pinned_bo(dev_priv);
1113 vmw_resource_evict_all(dev_priv);
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001114 ttm_bo_swapout_all(&dev_priv->bdev);
Thomas Hellstrom094e0fa2010-10-05 12:43:00 +02001115
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001116 break;
1117 case PM_POST_HIBERNATION:
1118 case PM_POST_SUSPEND:
Thomas Hellstrom094e0fa2010-10-05 12:43:00 +02001119 case PM_POST_RESTORE:
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001120 ttm_suspend_unlock(&vmaster->lock);
Thomas Hellstrom094e0fa2010-10-05 12:43:00 +02001121
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001122 break;
1123 case PM_RESTORE_PREPARE:
1124 break;
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001125 default:
1126 break;
1127 }
1128 return 0;
1129}
1130
1131/**
1132 * These might not be needed with the virtual SVGA device.
1133 */
1134
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001135static int vmw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001136{
Thomas Hellstrom094e0fa2010-10-05 12:43:00 +02001137 struct drm_device *dev = pci_get_drvdata(pdev);
1138 struct vmw_private *dev_priv = vmw_priv(dev);
1139
1140 if (dev_priv->num_3d_resources != 0) {
1141 DRM_INFO("Can't suspend or hibernate "
1142 "while 3D resources are active.\n");
1143 return -EBUSY;
1144 }
1145
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001146 pci_save_state(pdev);
1147 pci_disable_device(pdev);
1148 pci_set_power_state(pdev, PCI_D3hot);
1149 return 0;
1150}
1151
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001152static int vmw_pci_resume(struct pci_dev *pdev)
Thomas Hellstromd9f36a02010-01-13 22:28:43 +01001153{
1154 pci_set_power_state(pdev, PCI_D0);
1155 pci_restore_state(pdev);
1156 return pci_enable_device(pdev);
1157}
1158
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001159static int vmw_pm_suspend(struct device *kdev)
1160{
1161 struct pci_dev *pdev = to_pci_dev(kdev);
1162 struct pm_message dummy;
1163
1164 dummy.event = 0;
1165
1166 return vmw_pci_suspend(pdev, dummy);
1167}
1168
1169static int vmw_pm_resume(struct device *kdev)
1170{
1171 struct pci_dev *pdev = to_pci_dev(kdev);
1172
1173 return vmw_pci_resume(pdev);
1174}
1175
1176static int vmw_pm_prepare(struct device *kdev)
1177{
1178 struct pci_dev *pdev = to_pci_dev(kdev);
1179 struct drm_device *dev = pci_get_drvdata(pdev);
1180 struct vmw_private *dev_priv = vmw_priv(dev);
1181
1182 /**
1183 * Release 3d reference held by fbdev and potentially
1184 * stop fifo.
1185 */
1186 dev_priv->suspended = true;
1187 if (dev_priv->enable_fb)
Thomas Hellstrom05730b32011-08-31 07:42:52 +00001188 vmw_3d_resource_dec(dev_priv, true);
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001189
1190 if (dev_priv->num_3d_resources != 0) {
1191
1192 DRM_INFO("Can't suspend or hibernate "
1193 "while 3D resources are active.\n");
1194
1195 if (dev_priv->enable_fb)
Thomas Hellstrom05730b32011-08-31 07:42:52 +00001196 vmw_3d_resource_inc(dev_priv, true);
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001197 dev_priv->suspended = false;
1198 return -EBUSY;
1199 }
1200
1201 return 0;
1202}
1203
1204static void vmw_pm_complete(struct device *kdev)
1205{
1206 struct pci_dev *pdev = to_pci_dev(kdev);
1207 struct drm_device *dev = pci_get_drvdata(pdev);
1208 struct vmw_private *dev_priv = vmw_priv(dev);
1209
Thomas Hellstrom95e8f6a2012-11-09 10:05:57 +01001210 mutex_lock(&dev_priv->hw_mutex);
1211 vmw_write(dev_priv, SVGA_REG_ID, SVGA_ID_2);
1212 (void) vmw_read(dev_priv, SVGA_REG_ID);
1213 mutex_unlock(&dev_priv->hw_mutex);
1214
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001215 /**
1216 * Reclaim 3d reference held by fbdev and potentially
1217 * start fifo.
1218 */
1219 if (dev_priv->enable_fb)
Thomas Hellstrom05730b32011-08-31 07:42:52 +00001220 vmw_3d_resource_inc(dev_priv, false);
Thomas Hellstrom7fbd7212010-10-05 12:43:01 +02001221
1222 dev_priv->suspended = false;
1223}
1224
1225static const struct dev_pm_ops vmw_pm_ops = {
1226 .prepare = vmw_pm_prepare,
1227 .complete = vmw_pm_complete,
1228 .suspend = vmw_pm_suspend,
1229 .resume = vmw_pm_resume,
1230};
1231
Arjan van de Vene08e96d2011-10-31 07:28:57 -07001232static const struct file_operations vmwgfx_driver_fops = {
1233 .owner = THIS_MODULE,
1234 .open = drm_open,
1235 .release = drm_release,
1236 .unlocked_ioctl = vmw_unlocked_ioctl,
1237 .mmap = vmw_mmap,
1238 .poll = vmw_fops_poll,
1239 .read = vmw_fops_read,
Arjan van de Vene08e96d2011-10-31 07:28:57 -07001240#if defined(CONFIG_COMPAT)
1241 .compat_ioctl = drm_compat_ioctl,
1242#endif
1243 .llseek = noop_llseek,
1244};
1245
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001246static struct drm_driver driver = {
1247 .driver_features = DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED |
Thomas Hellstrom69977ff2013-11-13 01:50:46 -08001248 DRIVER_MODESET | DRIVER_PRIME,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001249 .load = vmw_driver_load,
1250 .unload = vmw_driver_unload,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001251 .lastclose = vmw_lastclose,
1252 .irq_preinstall = vmw_irq_preinstall,
1253 .irq_postinstall = vmw_irq_postinstall,
1254 .irq_uninstall = vmw_irq_uninstall,
1255 .irq_handler = vmw_irq_handler,
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001256 .get_vblank_counter = vmw_get_vblank_counter,
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001257 .enable_vblank = vmw_enable_vblank,
1258 .disable_vblank = vmw_disable_vblank,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001259 .ioctls = vmw_ioctls,
1260 .num_ioctls = DRM_ARRAY_SIZE(vmw_ioctls),
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001261 .master_create = vmw_master_create,
1262 .master_destroy = vmw_master_destroy,
1263 .master_set = vmw_master_set,
1264 .master_drop = vmw_master_drop,
1265 .open = vmw_driver_open,
Thomas Hellstrom6b82ef52012-02-09 16:56:42 +01001266 .preclose = vmw_preclose,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001267 .postclose = vmw_postclose,
Dave Airlie5e1782d2012-08-28 01:53:54 +00001268
1269 .dumb_create = vmw_dumb_create,
1270 .dumb_map_offset = vmw_dumb_map_offset,
1271 .dumb_destroy = vmw_dumb_destroy,
1272
Thomas Hellstrom69977ff2013-11-13 01:50:46 -08001273 .prime_fd_to_handle = vmw_prime_fd_to_handle,
1274 .prime_handle_to_fd = vmw_prime_handle_to_fd,
1275
Arjan van de Vene08e96d2011-10-31 07:28:57 -07001276 .fops = &vmwgfx_driver_fops,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001277 .name = VMWGFX_DRIVER_NAME,
1278 .desc = VMWGFX_DRIVER_DESC,
1279 .date = VMWGFX_DRIVER_DATE,
1280 .major = VMWGFX_DRIVER_MAJOR,
1281 .minor = VMWGFX_DRIVER_MINOR,
1282 .patchlevel = VMWGFX_DRIVER_PATCHLEVEL
1283};
1284
Dave Airlie8410ea32010-12-15 03:16:38 +10001285static struct pci_driver vmw_pci_driver = {
1286 .name = VMWGFX_DRIVER_NAME,
1287 .id_table = vmw_pci_id_list,
1288 .probe = vmw_probe,
1289 .remove = vmw_remove,
1290 .driver = {
1291 .pm = &vmw_pm_ops
1292 }
1293};
1294
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001295static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1296{
Jordan Crousedcdb1672010-05-27 13:40:25 -06001297 return drm_get_pci_dev(pdev, ent, &driver);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001298}
1299
1300static int __init vmwgfx_init(void)
1301{
1302 int ret;
Dave Airlie8410ea32010-12-15 03:16:38 +10001303 ret = drm_pci_init(&driver, &vmw_pci_driver);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001304 if (ret)
1305 DRM_ERROR("Failed initializing DRM.\n");
1306 return ret;
1307}
1308
1309static void __exit vmwgfx_exit(void)
1310{
Dave Airlie8410ea32010-12-15 03:16:38 +10001311 drm_pci_exit(&driver, &vmw_pci_driver);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001312}
1313
1314module_init(vmwgfx_init);
1315module_exit(vmwgfx_exit);
1316
1317MODULE_AUTHOR("VMware Inc. and others");
1318MODULE_DESCRIPTION("Standalone drm driver for the VMware SVGA device");
1319MODULE_LICENSE("GPL and additional rights");
Thomas Hellstrom73558ea2010-10-05 12:43:07 +02001320MODULE_VERSION(__stringify(VMWGFX_DRIVER_MAJOR) "."
1321 __stringify(VMWGFX_DRIVER_MINOR) "."
1322 __stringify(VMWGFX_DRIVER_PATCHLEVEL) "."
1323 "0");