vmwgfx_drv.c revision 1.6 1 /* $NetBSD: vmwgfx_drv.c,v 1.6 2022/10/25 23:34:05 riastradh Exp $ */
2
3 // SPDX-License-Identifier: GPL-2.0 OR MIT
4 /**************************************************************************
5 *
6 * Copyright 2009-2016 VMware, Inc., Palo Alto, CA., USA
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_drv.c,v 1.6 2022/10/25 23:34:05 riastradh Exp $");
32
33 #include <linux/console.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/module.h>
36 #include <linux/pci.h>
37
38 #include <drm/drm_drv.h>
39 #include <drm/drm_ioctl.h>
40 #include <drm/drm_sysfs.h>
41 #include <drm/ttm/ttm_bo_driver.h>
42 #include <drm/ttm/ttm_module.h>
43 #include <drm/ttm/ttm_placement.h>
44
45 #include "ttm_object.h"
46 #include "vmwgfx_binding.h"
47 #include "vmwgfx_drv.h"
48
49 #define VMWGFX_DRIVER_DESC "Linux drm driver for VMware graphics devices"
50 #define VMWGFX_CHIP_SVGAII 0
51 #define VMW_FB_RESERVATION 0
52
53 #define VMW_MIN_INITIAL_WIDTH 800
54 #define VMW_MIN_INITIAL_HEIGHT 600
55
56 #ifndef VMWGFX_GIT_VERSION
57 #define VMWGFX_GIT_VERSION "Unknown"
58 #endif
59
60 #define VMWGFX_REPO "In Tree"
61
62 #define VMWGFX_VALIDATION_MEM_GRAN (16*PAGE_SIZE)
63
64
65 /**
66 * Fully encoded drm commands. Might move to vmw_drm.h
67 */
68
69 #define DRM_IOCTL_VMW_GET_PARAM \
70 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GET_PARAM, \
71 struct drm_vmw_getparam_arg)
72 #define DRM_IOCTL_VMW_ALLOC_DMABUF \
73 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_ALLOC_DMABUF, \
74 union drm_vmw_alloc_dmabuf_arg)
75 #define DRM_IOCTL_VMW_UNREF_DMABUF \
76 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_DMABUF, \
77 struct drm_vmw_unref_dmabuf_arg)
78 #define DRM_IOCTL_VMW_CURSOR_BYPASS \
79 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_CURSOR_BYPASS, \
80 struct drm_vmw_cursor_bypass_arg)
81
82 #define DRM_IOCTL_VMW_CONTROL_STREAM \
83 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_CONTROL_STREAM, \
84 struct drm_vmw_control_stream_arg)
85 #define DRM_IOCTL_VMW_CLAIM_STREAM \
86 DRM_IOR(DRM_COMMAND_BASE + DRM_VMW_CLAIM_STREAM, \
87 struct drm_vmw_stream_arg)
88 #define DRM_IOCTL_VMW_UNREF_STREAM \
89 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_STREAM, \
90 struct drm_vmw_stream_arg)
91
92 #define DRM_IOCTL_VMW_CREATE_CONTEXT \
93 DRM_IOR(DRM_COMMAND_BASE + DRM_VMW_CREATE_CONTEXT, \
94 struct drm_vmw_context_arg)
95 #define DRM_IOCTL_VMW_UNREF_CONTEXT \
96 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_CONTEXT, \
97 struct drm_vmw_context_arg)
98 #define DRM_IOCTL_VMW_CREATE_SURFACE \
99 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_SURFACE, \
100 union drm_vmw_surface_create_arg)
101 #define DRM_IOCTL_VMW_UNREF_SURFACE \
102 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_SURFACE, \
103 struct drm_vmw_surface_arg)
104 #define DRM_IOCTL_VMW_REF_SURFACE \
105 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_REF_SURFACE, \
106 union drm_vmw_surface_reference_arg)
107 #define DRM_IOCTL_VMW_EXECBUF \
108 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_EXECBUF, \
109 struct drm_vmw_execbuf_arg)
110 #define DRM_IOCTL_VMW_GET_3D_CAP \
111 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_GET_3D_CAP, \
112 struct drm_vmw_get_3d_cap_arg)
113 #define DRM_IOCTL_VMW_FENCE_WAIT \
114 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_FENCE_WAIT, \
115 struct drm_vmw_fence_wait_arg)
116 #define DRM_IOCTL_VMW_FENCE_SIGNALED \
117 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_FENCE_SIGNALED, \
118 struct drm_vmw_fence_signaled_arg)
119 #define DRM_IOCTL_VMW_FENCE_UNREF \
120 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_FENCE_UNREF, \
121 struct drm_vmw_fence_arg)
122 #define DRM_IOCTL_VMW_FENCE_EVENT \
123 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_FENCE_EVENT, \
124 struct drm_vmw_fence_event_arg)
125 #define DRM_IOCTL_VMW_PRESENT \
126 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_PRESENT, \
127 struct drm_vmw_present_arg)
128 #define DRM_IOCTL_VMW_PRESENT_READBACK \
129 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_PRESENT_READBACK, \
130 struct drm_vmw_present_readback_arg)
131 #define DRM_IOCTL_VMW_UPDATE_LAYOUT \
132 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UPDATE_LAYOUT, \
133 struct drm_vmw_update_layout_arg)
134 #define DRM_IOCTL_VMW_CREATE_SHADER \
135 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_SHADER, \
136 struct drm_vmw_shader_create_arg)
137 #define DRM_IOCTL_VMW_UNREF_SHADER \
138 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_SHADER, \
139 struct drm_vmw_shader_arg)
140 #define DRM_IOCTL_VMW_GB_SURFACE_CREATE \
141 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_CREATE, \
142 union drm_vmw_gb_surface_create_arg)
143 #define DRM_IOCTL_VMW_GB_SURFACE_REF \
144 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_REF, \
145 union drm_vmw_gb_surface_reference_arg)
146 #define DRM_IOCTL_VMW_SYNCCPU \
147 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_SYNCCPU, \
148 struct drm_vmw_synccpu_arg)
149 #define DRM_IOCTL_VMW_CREATE_EXTENDED_CONTEXT \
150 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_EXTENDED_CONTEXT, \
151 struct drm_vmw_context_arg)
152 #define DRM_IOCTL_VMW_GB_SURFACE_CREATE_EXT \
153 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_CREATE_EXT, \
154 union drm_vmw_gb_surface_create_ext_arg)
155 #define DRM_IOCTL_VMW_GB_SURFACE_REF_EXT \
156 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_REF_EXT, \
157 union drm_vmw_gb_surface_reference_ext_arg)
158 #define DRM_IOCTL_VMW_MSG \
159 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_MSG, \
160 struct drm_vmw_msg_arg)
161
162 /**
163 * The core DRM version of this macro doesn't account for
164 * DRM_COMMAND_BASE.
165 */
166
167 #define VMW_IOCTL_DEF(ioctl, func, flags) \
168 [DRM_IOCTL_NR(DRM_IOCTL_##ioctl) - DRM_COMMAND_BASE] = {DRM_IOCTL_##ioctl, flags, func}
169
170 /**
171 * Ioctl definitions.
172 */
173
174 static const struct drm_ioctl_desc vmw_ioctls[] = {
175 VMW_IOCTL_DEF(VMW_GET_PARAM, vmw_getparam_ioctl,
176 DRM_RENDER_ALLOW),
177 VMW_IOCTL_DEF(VMW_ALLOC_DMABUF, vmw_bo_alloc_ioctl,
178 DRM_RENDER_ALLOW),
179 VMW_IOCTL_DEF(VMW_UNREF_DMABUF, vmw_bo_unref_ioctl,
180 DRM_RENDER_ALLOW),
181 VMW_IOCTL_DEF(VMW_CURSOR_BYPASS,
182 vmw_kms_cursor_bypass_ioctl,
183 DRM_MASTER),
184
185 VMW_IOCTL_DEF(VMW_CONTROL_STREAM, vmw_overlay_ioctl,
186 DRM_MASTER),
187 VMW_IOCTL_DEF(VMW_CLAIM_STREAM, vmw_stream_claim_ioctl,
188 DRM_MASTER),
189 VMW_IOCTL_DEF(VMW_UNREF_STREAM, vmw_stream_unref_ioctl,
190 DRM_MASTER),
191
192 VMW_IOCTL_DEF(VMW_CREATE_CONTEXT, vmw_context_define_ioctl,
193 DRM_RENDER_ALLOW),
194 VMW_IOCTL_DEF(VMW_UNREF_CONTEXT, vmw_context_destroy_ioctl,
195 DRM_RENDER_ALLOW),
196 VMW_IOCTL_DEF(VMW_CREATE_SURFACE, vmw_surface_define_ioctl,
197 DRM_RENDER_ALLOW),
198 VMW_IOCTL_DEF(VMW_UNREF_SURFACE, vmw_surface_destroy_ioctl,
199 DRM_RENDER_ALLOW),
200 VMW_IOCTL_DEF(VMW_REF_SURFACE, vmw_surface_reference_ioctl,
201 DRM_RENDER_ALLOW),
202 VMW_IOCTL_DEF(VMW_EXECBUF, vmw_execbuf_ioctl,
203 DRM_RENDER_ALLOW),
204 VMW_IOCTL_DEF(VMW_FENCE_WAIT, vmw_fence_obj_wait_ioctl,
205 DRM_RENDER_ALLOW),
206 VMW_IOCTL_DEF(VMW_FENCE_SIGNALED,
207 vmw_fence_obj_signaled_ioctl,
208 DRM_RENDER_ALLOW),
209 VMW_IOCTL_DEF(VMW_FENCE_UNREF, vmw_fence_obj_unref_ioctl,
210 DRM_RENDER_ALLOW),
211 VMW_IOCTL_DEF(VMW_FENCE_EVENT, vmw_fence_event_ioctl,
212 DRM_RENDER_ALLOW),
213 VMW_IOCTL_DEF(VMW_GET_3D_CAP, vmw_get_cap_3d_ioctl,
214 DRM_RENDER_ALLOW),
215
216 /* these allow direct access to the framebuffers mark as master only */
217 VMW_IOCTL_DEF(VMW_PRESENT, vmw_present_ioctl,
218 DRM_MASTER | DRM_AUTH),
219 VMW_IOCTL_DEF(VMW_PRESENT_READBACK,
220 vmw_present_readback_ioctl,
221 DRM_MASTER | DRM_AUTH),
222 /*
223 * The permissions of the below ioctl are overridden in
224 * vmw_generic_ioctl(). We require either
225 * DRM_MASTER or capable(CAP_SYS_ADMIN).
226 */
227 VMW_IOCTL_DEF(VMW_UPDATE_LAYOUT,
228 vmw_kms_update_layout_ioctl,
229 DRM_RENDER_ALLOW),
230 VMW_IOCTL_DEF(VMW_CREATE_SHADER,
231 vmw_shader_define_ioctl,
232 DRM_RENDER_ALLOW),
233 VMW_IOCTL_DEF(VMW_UNREF_SHADER,
234 vmw_shader_destroy_ioctl,
235 DRM_RENDER_ALLOW),
236 VMW_IOCTL_DEF(VMW_GB_SURFACE_CREATE,
237 vmw_gb_surface_define_ioctl,
238 DRM_RENDER_ALLOW),
239 VMW_IOCTL_DEF(VMW_GB_SURFACE_REF,
240 vmw_gb_surface_reference_ioctl,
241 DRM_RENDER_ALLOW),
242 VMW_IOCTL_DEF(VMW_SYNCCPU,
243 vmw_user_bo_synccpu_ioctl,
244 DRM_RENDER_ALLOW),
245 VMW_IOCTL_DEF(VMW_CREATE_EXTENDED_CONTEXT,
246 vmw_extended_context_define_ioctl,
247 DRM_RENDER_ALLOW),
248 VMW_IOCTL_DEF(VMW_GB_SURFACE_CREATE_EXT,
249 vmw_gb_surface_define_ext_ioctl,
250 DRM_RENDER_ALLOW),
251 VMW_IOCTL_DEF(VMW_GB_SURFACE_REF_EXT,
252 vmw_gb_surface_reference_ext_ioctl,
253 DRM_RENDER_ALLOW),
254 VMW_IOCTL_DEF(VMW_MSG,
255 vmw_msg_ioctl,
256 DRM_RENDER_ALLOW),
257 };
258
259 static const struct pci_device_id vmw_pci_id_list[] = {
260 {0x15ad, 0x0405, PCI_ANY_ID, PCI_ANY_ID, 0, 0, VMWGFX_CHIP_SVGAII},
261 {0, 0, 0}
262 };
263 MODULE_DEVICE_TABLE(pci, vmw_pci_id_list);
264
265 static int enable_fbdev = IS_ENABLED(CONFIG_DRM_VMWGFX_FBCON);
266 static int vmw_force_iommu;
267 static int vmw_restrict_iommu;
268 static int vmw_force_coherent;
269 static int vmw_restrict_dma_mask;
270 static int vmw_assume_16bpp;
271
272 static int vmw_probe(struct pci_dev *, const struct pci_device_id *);
273 static int vmwgfx_pm_notifier(struct notifier_block *nb, unsigned long val,
274 void *ptr);
275
276 MODULE_PARM_DESC(enable_fbdev, "Enable vmwgfx fbdev");
277 module_param_named(enable_fbdev, enable_fbdev, int, 0600);
278 MODULE_PARM_DESC(force_dma_api, "Force using the DMA API for TTM pages");
279 module_param_named(force_dma_api, vmw_force_iommu, int, 0600);
280 MODULE_PARM_DESC(restrict_iommu, "Try to limit IOMMU usage for TTM pages");
281 module_param_named(restrict_iommu, vmw_restrict_iommu, int, 0600);
282 MODULE_PARM_DESC(force_coherent, "Force coherent TTM pages");
283 module_param_named(force_coherent, vmw_force_coherent, int, 0600);
284 MODULE_PARM_DESC(restrict_dma_mask, "Restrict DMA mask to 44 bits with IOMMU");
285 module_param_named(restrict_dma_mask, vmw_restrict_dma_mask, int, 0600);
286 MODULE_PARM_DESC(assume_16bpp, "Assume 16-bpp when filtering modes");
287 module_param_named(assume_16bpp, vmw_assume_16bpp, int, 0600);
288
289
290 static void vmw_print_capabilities2(uint32_t capabilities2)
291 {
292 DRM_INFO("Capabilities2:\n");
293 if (capabilities2 & SVGA_CAP2_GROW_OTABLE)
294 DRM_INFO(" Grow oTable.\n");
295 if (capabilities2 & SVGA_CAP2_INTRA_SURFACE_COPY)
296 DRM_INFO(" IntraSurface copy.\n");
297 }
298
299 static void vmw_print_capabilities(uint32_t capabilities)
300 {
301 DRM_INFO("Capabilities:\n");
302 if (capabilities & SVGA_CAP_RECT_COPY)
303 DRM_INFO(" Rect copy.\n");
304 if (capabilities & SVGA_CAP_CURSOR)
305 DRM_INFO(" Cursor.\n");
306 if (capabilities & SVGA_CAP_CURSOR_BYPASS)
307 DRM_INFO(" Cursor bypass.\n");
308 if (capabilities & SVGA_CAP_CURSOR_BYPASS_2)
309 DRM_INFO(" Cursor bypass 2.\n");
310 if (capabilities & SVGA_CAP_8BIT_EMULATION)
311 DRM_INFO(" 8bit emulation.\n");
312 if (capabilities & SVGA_CAP_ALPHA_CURSOR)
313 DRM_INFO(" Alpha cursor.\n");
314 if (capabilities & SVGA_CAP_3D)
315 DRM_INFO(" 3D.\n");
316 if (capabilities & SVGA_CAP_EXTENDED_FIFO)
317 DRM_INFO(" Extended Fifo.\n");
318 if (capabilities & SVGA_CAP_MULTIMON)
319 DRM_INFO(" Multimon.\n");
320 if (capabilities & SVGA_CAP_PITCHLOCK)
321 DRM_INFO(" Pitchlock.\n");
322 if (capabilities & SVGA_CAP_IRQMASK)
323 DRM_INFO(" Irq mask.\n");
324 if (capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)
325 DRM_INFO(" Display Topology.\n");
326 if (capabilities & SVGA_CAP_GMR)
327 DRM_INFO(" GMR.\n");
328 if (capabilities & SVGA_CAP_TRACES)
329 DRM_INFO(" Traces.\n");
330 if (capabilities & SVGA_CAP_GMR2)
331 DRM_INFO(" GMR2.\n");
332 if (capabilities & SVGA_CAP_SCREEN_OBJECT_2)
333 DRM_INFO(" Screen Object 2.\n");
334 if (capabilities & SVGA_CAP_COMMAND_BUFFERS)
335 DRM_INFO(" Command Buffers.\n");
336 if (capabilities & SVGA_CAP_CMD_BUFFERS_2)
337 DRM_INFO(" Command Buffers 2.\n");
338 if (capabilities & SVGA_CAP_GBOBJECTS)
339 DRM_INFO(" Guest Backed Resources.\n");
340 if (capabilities & SVGA_CAP_DX)
341 DRM_INFO(" DX Features.\n");
342 if (capabilities & SVGA_CAP_HP_CMD_QUEUE)
343 DRM_INFO(" HP Command Queue.\n");
344 }
345
346 /**
347 * vmw_dummy_query_bo_create - create a bo to hold a dummy query result
348 *
349 * @dev_priv: A device private structure.
350 *
351 * This function creates a small buffer object that holds the query
352 * result for dummy queries emitted as query barriers.
353 * The function will then map the first page and initialize a pending
354 * occlusion query result structure, Finally it will unmap the buffer.
355 * No interruptible waits are done within this function.
356 *
357 * Returns an error if bo creation or initialization fails.
358 */
359 static int vmw_dummy_query_bo_create(struct vmw_private *dev_priv)
360 {
361 int ret;
362 struct vmw_buffer_object *vbo;
363 struct ttm_bo_kmap_obj map;
364 volatile SVGA3dQueryResult *result;
365 bool dummy;
366
367 /*
368 * Create the vbo as pinned, so that a tryreserve will
369 * immediately succeed. This is because we're the only
370 * user of the bo currently.
371 */
372 vbo = kzalloc(sizeof(*vbo), GFP_KERNEL);
373 if (!vbo)
374 return -ENOMEM;
375
376 ret = vmw_bo_init(dev_priv, vbo, PAGE_SIZE,
377 &vmw_sys_ne_placement, false,
378 &vmw_bo_bo_free);
379 if (unlikely(ret != 0))
380 return ret;
381
382 ret = ttm_bo_reserve(&vbo->base, false, true, NULL);
383 BUG_ON(ret != 0);
384 vmw_bo_pin_reserved(vbo, true);
385
386 ret = ttm_bo_kmap(&vbo->base, 0, 1, &map);
387 if (likely(ret == 0)) {
388 result = ttm_kmap_obj_virtual(&map, &dummy);
389 result->totalSize = sizeof(*result);
390 result->state = SVGA3D_QUERYSTATE_PENDING;
391 result->result32 = 0xff;
392 ttm_bo_kunmap(&map);
393 }
394 vmw_bo_pin_reserved(vbo, false);
395 ttm_bo_unreserve(&vbo->base);
396
397 if (unlikely(ret != 0)) {
398 DRM_ERROR("Dummy query buffer map failed.\n");
399 vmw_bo_unreference(&vbo);
400 } else
401 dev_priv->dummy_query_bo = vbo;
402
403 return ret;
404 }
405
406 /**
407 * vmw_request_device_late - Perform late device setup
408 *
409 * @dev_priv: Pointer to device private.
410 *
411 * This function performs setup of otables and enables large command
412 * buffer submission. These tasks are split out to a separate function
413 * because it reverts vmw_release_device_early and is intended to be used
414 * by an error path in the hibernation code.
415 */
416 static int vmw_request_device_late(struct vmw_private *dev_priv)
417 {
418 int ret;
419
420 if (dev_priv->has_mob) {
421 ret = vmw_otables_setup(dev_priv);
422 if (unlikely(ret != 0)) {
423 DRM_ERROR("Unable to initialize "
424 "guest Memory OBjects.\n");
425 return ret;
426 }
427 }
428
429 if (dev_priv->cman) {
430 ret = vmw_cmdbuf_set_pool_size(dev_priv->cman,
431 256*4096, 2*4096);
432 if (ret) {
433 struct vmw_cmdbuf_man *man = dev_priv->cman;
434
435 dev_priv->cman = NULL;
436 vmw_cmdbuf_man_destroy(man);
437 }
438 }
439
440 return 0;
441 }
442
443 static int vmw_request_device(struct vmw_private *dev_priv)
444 {
445 int ret;
446
447 ret = vmw_fifo_init(dev_priv, &dev_priv->fifo);
448 if (unlikely(ret != 0)) {
449 DRM_ERROR("Unable to initialize FIFO.\n");
450 return ret;
451 }
452 vmw_fence_fifo_up(dev_priv->fman);
453 dev_priv->cman = vmw_cmdbuf_man_create(dev_priv);
454 if (IS_ERR(dev_priv->cman)) {
455 dev_priv->cman = NULL;
456 dev_priv->has_dx = false;
457 }
458
459 ret = vmw_request_device_late(dev_priv);
460 if (ret)
461 goto out_no_mob;
462
463 ret = vmw_dummy_query_bo_create(dev_priv);
464 if (unlikely(ret != 0))
465 goto out_no_query_bo;
466
467 return 0;
468
469 out_no_query_bo:
470 if (dev_priv->cman)
471 vmw_cmdbuf_remove_pool(dev_priv->cman);
472 if (dev_priv->has_mob) {
473 (void) ttm_bo_evict_mm(&dev_priv->bdev, VMW_PL_MOB);
474 vmw_otables_takedown(dev_priv);
475 }
476 if (dev_priv->cman)
477 vmw_cmdbuf_man_destroy(dev_priv->cman);
478 out_no_mob:
479 vmw_fence_fifo_down(dev_priv->fman);
480 vmw_fifo_release(dev_priv, &dev_priv->fifo);
481 return ret;
482 }
483
484 /**
485 * vmw_release_device_early - Early part of fifo takedown.
486 *
487 * @dev_priv: Pointer to device private struct.
488 *
489 * This is the first part of command submission takedown, to be called before
490 * buffer management is taken down.
491 */
492 static void vmw_release_device_early(struct vmw_private *dev_priv)
493 {
494 /*
495 * Previous destructions should've released
496 * the pinned bo.
497 */
498
499 BUG_ON(dev_priv->pinned_bo != NULL);
500
501 vmw_bo_unreference(&dev_priv->dummy_query_bo);
502 if (dev_priv->cman)
503 vmw_cmdbuf_remove_pool(dev_priv->cman);
504
505 if (dev_priv->has_mob) {
506 ttm_bo_evict_mm(&dev_priv->bdev, VMW_PL_MOB);
507 vmw_otables_takedown(dev_priv);
508 }
509 }
510
511 /**
512 * vmw_release_device_late - Late part of fifo takedown.
513 *
514 * @dev_priv: Pointer to device private struct.
515 *
516 * This is the last part of the command submission takedown, to be called when
517 * command submission is no longer needed. It may wait on pending fences.
518 */
519 static void vmw_release_device_late(struct vmw_private *dev_priv)
520 {
521 vmw_fence_fifo_down(dev_priv->fman);
522 if (dev_priv->cman)
523 vmw_cmdbuf_man_destroy(dev_priv->cman);
524
525 vmw_fifo_release(dev_priv, &dev_priv->fifo);
526 }
527
528 /**
529 * Sets the initial_[width|height] fields on the given vmw_private.
530 *
531 * It does so by reading SVGA_REG_[WIDTH|HEIGHT] regs and then
532 * clamping the value to fb_max_[width|height] fields and the
533 * VMW_MIN_INITIAL_[WIDTH|HEIGHT].
534 * If the values appear to be invalid, set them to
535 * VMW_MIN_INITIAL_[WIDTH|HEIGHT].
536 */
537 static void vmw_get_initial_size(struct vmw_private *dev_priv)
538 {
539 uint32_t width;
540 uint32_t height;
541
542 width = vmw_read(dev_priv, SVGA_REG_WIDTH);
543 height = vmw_read(dev_priv, SVGA_REG_HEIGHT);
544
545 width = max_t(uint32_t, width, VMW_MIN_INITIAL_WIDTH);
546 height = max_t(uint32_t, height, VMW_MIN_INITIAL_HEIGHT);
547
548 if (width > dev_priv->fb_max_width ||
549 height > dev_priv->fb_max_height) {
550
551 /*
552 * This is a host error and shouldn't occur.
553 */
554
555 width = VMW_MIN_INITIAL_WIDTH;
556 height = VMW_MIN_INITIAL_HEIGHT;
557 }
558
559 dev_priv->initial_width = width;
560 dev_priv->initial_height = height;
561 }
562
563 /**
564 * vmw_dma_select_mode - Determine how DMA mappings should be set up for this
565 * system.
566 *
567 * @dev_priv: Pointer to a struct vmw_private
568 *
569 * This functions tries to determine what actions need to be taken by the
570 * driver to make system pages visible to the device.
571 * If this function decides that DMA is not possible, it returns -EINVAL.
572 * The driver may then try to disable features of the device that require
573 * DMA.
574 */
575 static int vmw_dma_select_mode(struct vmw_private *dev_priv)
576 {
577 static const char *names[vmw_dma_map_max] = {
578 [vmw_dma_phys] = "Using physical TTM page addresses.",
579 [vmw_dma_alloc_coherent] = "Using coherent TTM pages.",
580 [vmw_dma_map_populate] = "Caching DMA mappings.",
581 [vmw_dma_map_bind] = "Giving up DMA mappings early."};
582
583 if (vmw_force_coherent)
584 dev_priv->map_mode = vmw_dma_alloc_coherent;
585 else if (vmw_restrict_iommu)
586 dev_priv->map_mode = vmw_dma_map_bind;
587 else
588 dev_priv->map_mode = vmw_dma_map_populate;
589
590 if (!IS_ENABLED(CONFIG_DRM_TTM_DMA_PAGE_POOL) &&
591 (dev_priv->map_mode == vmw_dma_alloc_coherent))
592 return -EINVAL;
593
594 DRM_INFO("DMA map mode: %s\n", names[dev_priv->map_mode]);
595 return 0;
596 }
597
598 /**
599 * vmw_dma_masks - set required page- and dma masks
600 *
601 * @dev: Pointer to struct drm-device
602 *
603 * With 32-bit we can only handle 32 bit PFNs. Optionally set that
604 * restriction also for 64-bit systems.
605 */
606 static int vmw_dma_masks(struct vmw_private *dev_priv)
607 {
608 struct drm_device *dev = dev_priv->dev;
609 int ret = 0;
610
611 ret = dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64));
612 if (dev_priv->map_mode != vmw_dma_phys &&
613 (sizeof(unsigned long) == 4 || vmw_restrict_dma_mask)) {
614 DRM_INFO("Restricting DMA addresses to 44 bits.\n");
615 return dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(44));
616 }
617
618 return ret;
619 }
620
621 static int vmw_driver_load(struct drm_device *dev, unsigned long chipset)
622 {
623 struct vmw_private *dev_priv;
624 int ret;
625 uint32_t svga_id;
626 enum vmw_res_type i;
627 bool refuse_dma = false;
628 char host_log[100] = {0};
629
630 dev_priv = kzalloc(sizeof(*dev_priv), GFP_KERNEL);
631 if (unlikely(!dev_priv)) {
632 DRM_ERROR("Failed allocating a device private struct.\n");
633 return -ENOMEM;
634 }
635
636 pci_set_master(dev->pdev);
637
638 dev_priv->dev = dev;
639 dev_priv->vmw_chipset = chipset;
640 dev_priv->last_read_seqno = (uint32_t) -100;
641 mutex_init(&dev_priv->cmdbuf_mutex);
642 mutex_init(&dev_priv->release_mutex);
643 mutex_init(&dev_priv->binding_mutex);
644 mutex_init(&dev_priv->global_kms_state_mutex);
645 ttm_lock_init(&dev_priv->reservation_sem);
646 spin_lock_init(&dev_priv->resource_lock);
647 spin_lock_init(&dev_priv->hw_lock);
648 spin_lock_init(&dev_priv->waiter_lock);
649 spin_lock_init(&dev_priv->cap_lock);
650 spin_lock_init(&dev_priv->svga_lock);
651 spin_lock_init(&dev_priv->cursor_lock);
652
653 for (i = vmw_res_context; i < vmw_res_max; ++i) {
654 idr_init(&dev_priv->res_idr[i]);
655 INIT_LIST_HEAD(&dev_priv->res_lru[i]);
656 }
657
658 DRM_INIT_WAITQUEUE(&dev_priv->fence_queue, "vmwgfence");
659 spin_lock_init(&dev_priv->fence_lock);
660 DRM_INIT_WAITQUEUE(&dev_priv->fifo_queue, "vmwgfifo");
661 spin_lock_init(&dev_priv->fifo_lock);
662 dev_priv->fence_queue_waiters = 0;
663 dev_priv->fifo_queue_waiters = 0;
664
665 dev_priv->used_memory_size = 0;
666
667 dev_priv->io_start = pci_resource_start(dev->pdev, 0);
668 dev_priv->vram_start = pci_resource_start(dev->pdev, 1);
669 dev_priv->mmio_start = pci_resource_start(dev->pdev, 2);
670
671 dev_priv->assume_16bpp = !!vmw_assume_16bpp;
672
673 dev_priv->enable_fb = enable_fbdev;
674
675 vmw_write(dev_priv, SVGA_REG_ID, SVGA_ID_2);
676 svga_id = vmw_read(dev_priv, SVGA_REG_ID);
677 if (svga_id != SVGA_ID_2) {
678 ret = -ENOSYS;
679 DRM_ERROR("Unsupported SVGA ID 0x%x\n", svga_id);
680 goto out_err0;
681 }
682
683 dev_priv->capabilities = vmw_read(dev_priv, SVGA_REG_CAPABILITIES);
684
685 if (dev_priv->capabilities & SVGA_CAP_CAP2_REGISTER) {
686 dev_priv->capabilities2 = vmw_read(dev_priv, SVGA_REG_CAP2);
687 }
688
689
690 ret = vmw_dma_select_mode(dev_priv);
691 if (unlikely(ret != 0)) {
692 DRM_INFO("Restricting capabilities due to IOMMU setup.\n");
693 refuse_dma = true;
694 }
695
696 dev_priv->vram_size = vmw_read(dev_priv, SVGA_REG_VRAM_SIZE);
697 dev_priv->mmio_size = vmw_read(dev_priv, SVGA_REG_MEM_SIZE);
698 dev_priv->fb_max_width = vmw_read(dev_priv, SVGA_REG_MAX_WIDTH);
699 dev_priv->fb_max_height = vmw_read(dev_priv, SVGA_REG_MAX_HEIGHT);
700
701 vmw_get_initial_size(dev_priv);
702
703 if (dev_priv->capabilities & SVGA_CAP_GMR2) {
704 dev_priv->max_gmr_ids =
705 vmw_read(dev_priv, SVGA_REG_GMR_MAX_IDS);
706 dev_priv->max_gmr_pages =
707 vmw_read(dev_priv, SVGA_REG_GMRS_MAX_PAGES);
708 dev_priv->memory_size =
709 vmw_read(dev_priv, SVGA_REG_MEMORY_SIZE);
710 dev_priv->memory_size -= dev_priv->vram_size;
711 } else {
712 /*
713 * An arbitrary limit of 512MiB on surface
714 * memory. But all HWV8 hardware supports GMR2.
715 */
716 dev_priv->memory_size = 512*1024*1024;
717 }
718 dev_priv->max_mob_pages = 0;
719 dev_priv->max_mob_size = 0;
720 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS) {
721 uint64_t mem_size =
722 vmw_read(dev_priv,
723 SVGA_REG_SUGGESTED_GBOBJECT_MEM_SIZE_KB);
724
725 /*
726 * Workaround for low memory 2D VMs to compensate for the
727 * allocation taken by fbdev
728 */
729 if (!(dev_priv->capabilities & SVGA_CAP_3D))
730 mem_size *= 3;
731
732 dev_priv->max_mob_pages = mem_size * 1024 / PAGE_SIZE;
733 dev_priv->prim_bb_mem =
734 vmw_read(dev_priv,
735 SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM);
736 dev_priv->max_mob_size =
737 vmw_read(dev_priv, SVGA_REG_MOB_MAX_SIZE);
738 dev_priv->stdu_max_width =
739 vmw_read(dev_priv, SVGA_REG_SCREENTARGET_MAX_WIDTH);
740 dev_priv->stdu_max_height =
741 vmw_read(dev_priv, SVGA_REG_SCREENTARGET_MAX_HEIGHT);
742
743 vmw_write(dev_priv, SVGA_REG_DEV_CAP,
744 SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH);
745 dev_priv->texture_max_width = vmw_read(dev_priv,
746 SVGA_REG_DEV_CAP);
747 vmw_write(dev_priv, SVGA_REG_DEV_CAP,
748 SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT);
749 dev_priv->texture_max_height = vmw_read(dev_priv,
750 SVGA_REG_DEV_CAP);
751 } else {
752 dev_priv->texture_max_width = 8192;
753 dev_priv->texture_max_height = 8192;
754 dev_priv->prim_bb_mem = dev_priv->vram_size;
755 }
756
757 vmw_print_capabilities(dev_priv->capabilities);
758 if (dev_priv->capabilities & SVGA_CAP_CAP2_REGISTER)
759 vmw_print_capabilities2(dev_priv->capabilities2);
760
761 ret = vmw_dma_masks(dev_priv);
762 if (unlikely(ret != 0))
763 goto out_err0;
764
765 dma_set_max_seg_size(dev->dev, min_t(unsigned int, U32_MAX & PAGE_MASK,
766 SCATTERLIST_MAX_SEGMENT));
767
768 if (dev_priv->capabilities & SVGA_CAP_GMR2) {
769 DRM_INFO("Max GMR ids is %u\n",
770 (unsigned)dev_priv->max_gmr_ids);
771 DRM_INFO("Max number of GMR pages is %u\n",
772 (unsigned)dev_priv->max_gmr_pages);
773 DRM_INFO("Max dedicated hypervisor surface memory is %u kiB\n",
774 (unsigned)dev_priv->memory_size / 1024);
775 }
776 DRM_INFO("Maximum display memory size is %u kiB\n",
777 dev_priv->prim_bb_mem / 1024);
778 DRM_INFO("VRAM at 0x%08x size is %u kiB\n",
779 dev_priv->vram_start, dev_priv->vram_size / 1024);
780 DRM_INFO("MMIO at 0x%08x size is %u kiB\n",
781 dev_priv->mmio_start, dev_priv->mmio_size / 1024);
782
783 dev_priv->mmio_virt = memremap(dev_priv->mmio_start,
784 dev_priv->mmio_size, MEMREMAP_WB);
785
786 if (unlikely(dev_priv->mmio_virt == NULL)) {
787 ret = -ENOMEM;
788 DRM_ERROR("Failed mapping MMIO.\n");
789 goto out_err0;
790 }
791
792 /* Need mmio memory to check for fifo pitchlock cap. */
793 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY) &&
794 !(dev_priv->capabilities & SVGA_CAP_PITCHLOCK) &&
795 !vmw_fifo_have_pitchlock(dev_priv)) {
796 ret = -ENOSYS;
797 DRM_ERROR("Hardware has no pitchlock\n");
798 goto out_err4;
799 }
800
801 dev_priv->tdev = ttm_object_device_init(&ttm_mem_glob, 12,
802 &vmw_prime_dmabuf_ops);
803
804 if (unlikely(dev_priv->tdev == NULL)) {
805 DRM_ERROR("Unable to initialize TTM object management.\n");
806 ret = -ENOMEM;
807 goto out_err4;
808 }
809
810 dev->dev_private = dev_priv;
811
812 ret = pci_request_regions(dev->pdev, "vmwgfx probe");
813 dev_priv->stealth = (ret != 0);
814 if (dev_priv->stealth) {
815 /**
816 * Request at least the mmio PCI resource.
817 */
818
819 DRM_INFO("It appears like vesafb is loaded. "
820 "Ignore above error if any.\n");
821 ret = pci_request_region(dev->pdev, 2, "vmwgfx stealth probe");
822 if (unlikely(ret != 0)) {
823 DRM_ERROR("Failed reserving the SVGA MMIO resource.\n");
824 goto out_no_device;
825 }
826 }
827
828 if (dev_priv->capabilities & SVGA_CAP_IRQMASK) {
829 ret = vmw_irq_install(dev, dev->pdev->irq);
830 if (ret != 0) {
831 DRM_ERROR("Failed installing irq: %d\n", ret);
832 goto out_no_irq;
833 }
834 }
835
836 dev_priv->fman = vmw_fence_manager_init(dev_priv);
837 if (unlikely(dev_priv->fman == NULL)) {
838 ret = -ENOMEM;
839 goto out_no_fman;
840 }
841
842 drm_vma_offset_manager_init(&dev_priv->vma_manager,
843 DRM_FILE_PAGE_OFFSET_START,
844 DRM_FILE_PAGE_OFFSET_SIZE);
845 ret = ttm_bo_device_init(&dev_priv->bdev,
846 &vmw_bo_driver,
847 dev->anon_inode->i_mapping,
848 &dev_priv->vma_manager,
849 false);
850 if (unlikely(ret != 0)) {
851 DRM_ERROR("Failed initializing TTM buffer object driver.\n");
852 goto out_no_bdev;
853 }
854
855 /*
856 * Enable VRAM, but initially don't use it until SVGA is enabled and
857 * unhidden.
858 */
859 ret = ttm_bo_init_mm(&dev_priv->bdev, TTM_PL_VRAM,
860 (dev_priv->vram_size >> PAGE_SHIFT));
861 if (unlikely(ret != 0)) {
862 DRM_ERROR("Failed initializing memory manager for VRAM.\n");
863 goto out_no_vram;
864 }
865 dev_priv->bdev.man[TTM_PL_VRAM].use_type = false;
866
867 dev_priv->has_gmr = true;
868 if (((dev_priv->capabilities & (SVGA_CAP_GMR | SVGA_CAP_GMR2)) == 0) ||
869 refuse_dma || ttm_bo_init_mm(&dev_priv->bdev, VMW_PL_GMR,
870 VMW_PL_GMR) != 0) {
871 DRM_INFO("No GMR memory available. "
872 "Graphics memory resources are very limited.\n");
873 dev_priv->has_gmr = false;
874 }
875
876 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS) {
877 dev_priv->has_mob = true;
878 if (ttm_bo_init_mm(&dev_priv->bdev, VMW_PL_MOB,
879 VMW_PL_MOB) != 0) {
880 DRM_INFO("No MOB memory available. "
881 "3D will be disabled.\n");
882 dev_priv->has_mob = false;
883 }
884 }
885
886 if (dev_priv->has_mob) {
887 spin_lock(&dev_priv->cap_lock);
888 vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_DXCONTEXT);
889 dev_priv->has_dx = !!vmw_read(dev_priv, SVGA_REG_DEV_CAP);
890 spin_unlock(&dev_priv->cap_lock);
891 }
892
893 vmw_validation_mem_init_ttm(dev_priv, VMWGFX_VALIDATION_MEM_GRAN);
894 ret = vmw_kms_init(dev_priv);
895 if (unlikely(ret != 0))
896 goto out_no_kms;
897 vmw_overlay_init(dev_priv);
898
899 ret = vmw_request_device(dev_priv);
900 if (ret)
901 goto out_no_fifo;
902
903 if (dev_priv->has_dx) {
904 /*
905 * SVGA_CAP2_DX2 (DefineGBSurface_v3) is needed for SM4_1
906 * support
907 */
908 if ((dev_priv->capabilities2 & SVGA_CAP2_DX2) != 0) {
909 vmw_write(dev_priv, SVGA_REG_DEV_CAP,
910 SVGA3D_DEVCAP_SM41);
911 dev_priv->has_sm4_1 = vmw_read(dev_priv,
912 SVGA_REG_DEV_CAP);
913 }
914 }
915
916 DRM_INFO("DX: %s\n", dev_priv->has_dx ? "yes." : "no.");
917 DRM_INFO("Atomic: %s\n", (dev->driver->driver_features & DRIVER_ATOMIC)
918 ? "yes." : "no.");
919 DRM_INFO("SM4_1: %s\n", dev_priv->has_sm4_1 ? "yes." : "no.");
920
921 snprintf(host_log, sizeof(host_log), "vmwgfx: %s-%s",
922 VMWGFX_REPO, VMWGFX_GIT_VERSION);
923 vmw_host_log(host_log);
924
925 memset(host_log, 0, sizeof(host_log));
926 snprintf(host_log, sizeof(host_log), "vmwgfx: Module Version: %d.%d.%d",
927 VMWGFX_DRIVER_MAJOR, VMWGFX_DRIVER_MINOR,
928 VMWGFX_DRIVER_PATCHLEVEL);
929 vmw_host_log(host_log);
930
931 if (dev_priv->enable_fb) {
932 vmw_fifo_resource_inc(dev_priv);
933 vmw_svga_enable(dev_priv);
934 vmw_fb_init(dev_priv);
935 }
936
937 dev_priv->pm_nb.notifier_call = vmwgfx_pm_notifier;
938 register_pm_notifier(&dev_priv->pm_nb);
939
940 return 0;
941
942 out_no_fifo:
943 vmw_overlay_close(dev_priv);
944 vmw_kms_close(dev_priv);
945 out_no_kms:
946 if (dev_priv->has_mob)
947 (void) ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_MOB);
948 if (dev_priv->has_gmr)
949 (void) ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_GMR);
950 (void)ttm_bo_clean_mm(&dev_priv->bdev, TTM_PL_VRAM);
951 out_no_vram:
952 (void)ttm_bo_device_release(&dev_priv->bdev);
953 out_no_bdev:
954 vmw_fence_manager_takedown(dev_priv->fman);
955 out_no_fman:
956 if (dev_priv->capabilities & SVGA_CAP_IRQMASK)
957 vmw_irq_uninstall(dev_priv->dev);
958 out_no_irq:
959 if (dev_priv->stealth)
960 pci_release_region(dev->pdev, 2);
961 else
962 pci_release_regions(dev->pdev);
963 out_no_device:
964 ttm_object_device_release(&dev_priv->tdev);
965 out_err4:
966 memunmap(dev_priv->mmio_virt);
967 out_err0:
968 spin_lock_destroy(&dev_priv->fifo_lock);
969 DRM_DESTROY_WAITQUEUE(&dev_priv->fifo_queue);
970 spin_lock_destroy(&dev_priv->fence_lock);
971 DRM_DESTROY_WAITQUEUE(&dev_priv->fence_queue);
972
973 for (i = vmw_res_context; i < vmw_res_max; ++i)
974 idr_destroy(&dev_priv->res_idr[i]);
975
976 if (dev_priv->ctx.staged_bindings)
977 vmw_binding_state_free(dev_priv->ctx.staged_bindings);
978 kfree(dev_priv);
979 return ret;
980 }
981
982 static void vmw_driver_unload(struct drm_device *dev)
983 {
984 struct vmw_private *dev_priv = vmw_priv(dev);
985 enum vmw_res_type i;
986
987 unregister_pm_notifier(&dev_priv->pm_nb);
988
989 if (dev_priv->ctx.res_ht_initialized)
990 drm_ht_remove(&dev_priv->ctx.res_ht);
991 vfree(dev_priv->ctx.cmd_bounce);
992 if (dev_priv->enable_fb) {
993 vmw_fb_off(dev_priv);
994 vmw_fb_close(dev_priv);
995 vmw_fifo_resource_dec(dev_priv);
996 vmw_svga_disable(dev_priv);
997 }
998
999 vmw_kms_close(dev_priv);
1000 vmw_overlay_close(dev_priv);
1001
1002 if (dev_priv->has_gmr)
1003 (void)ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_GMR);
1004 (void)ttm_bo_clean_mm(&dev_priv->bdev, TTM_PL_VRAM);
1005
1006 vmw_release_device_early(dev_priv);
1007 if (dev_priv->has_mob)
1008 (void) ttm_bo_clean_mm(&dev_priv->bdev, VMW_PL_MOB);
1009 (void) ttm_bo_device_release(&dev_priv->bdev);
1010 drm_vma_offset_manager_destroy(&dev_priv->vma_manager);
1011 vmw_release_device_late(dev_priv);
1012 vmw_fence_manager_takedown(dev_priv->fman);
1013 if (dev_priv->capabilities & SVGA_CAP_IRQMASK)
1014 vmw_irq_uninstall(dev_priv->dev);
1015 if (dev_priv->stealth)
1016 pci_release_region(dev->pdev, 2);
1017 else
1018 pci_release_regions(dev->pdev);
1019
1020 ttm_object_device_release(&dev_priv->tdev);
1021 memunmap(dev_priv->mmio_virt);
1022 if (dev_priv->ctx.staged_bindings)
1023 vmw_binding_state_free(dev_priv->ctx.staged_bindings);
1024
1025 spin_lock_destroy(&dev_priv->fifo_lock);
1026 DRM_DESTROY_WAITQUEUE(&dev_priv->fifo_queue);
1027 spin_lock_destroy(&dev_priv->fence_lock);
1028 DRM_DESTROY_WAITQUEUE(&dev_priv->fence_queue);
1029
1030 for (i = vmw_res_context; i < vmw_res_max; ++i)
1031 idr_destroy(&dev_priv->res_idr[i]);
1032
1033 kfree(dev_priv);
1034 }
1035
1036 static void vmw_postclose(struct drm_device *dev,
1037 struct drm_file *file_priv)
1038 {
1039 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1040
1041 ttm_object_file_release(&vmw_fp->tfile);
1042 kfree(vmw_fp);
1043 }
1044
1045 static int vmw_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1046 {
1047 struct vmw_private *dev_priv = vmw_priv(dev);
1048 struct vmw_fpriv *vmw_fp;
1049 int ret = -ENOMEM;
1050
1051 vmw_fp = kzalloc(sizeof(*vmw_fp), GFP_KERNEL);
1052 if (unlikely(!vmw_fp))
1053 return ret;
1054
1055 vmw_fp->tfile = ttm_object_file_init(dev_priv->tdev, 10);
1056 if (unlikely(vmw_fp->tfile == NULL))
1057 goto out_no_tfile;
1058
1059 file_priv->driver_priv = vmw_fp;
1060
1061 return 0;
1062
1063 out_no_tfile:
1064 kfree(vmw_fp);
1065 return ret;
1066 }
1067
1068 static long vmw_generic_ioctl(struct file *filp, unsigned int cmd,
1069 unsigned long arg,
1070 long (*ioctl_func)(struct file *, unsigned int,
1071 unsigned long))
1072 {
1073 struct drm_file *file_priv = filp->private_data;
1074 struct drm_device *dev = file_priv->minor->dev;
1075 unsigned int nr = DRM_IOCTL_NR(cmd);
1076 unsigned int flags;
1077
1078 /*
1079 * Do extra checking on driver private ioctls.
1080 */
1081
1082 if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END)
1083 && (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
1084 const struct drm_ioctl_desc *ioctl =
1085 &vmw_ioctls[nr - DRM_COMMAND_BASE];
1086
1087 if (nr == DRM_COMMAND_BASE + DRM_VMW_EXECBUF) {
1088 return ioctl_func(filp, cmd, arg);
1089 } else if (nr == DRM_COMMAND_BASE + DRM_VMW_UPDATE_LAYOUT) {
1090 if (!drm_is_current_master(file_priv) &&
1091 !capable(CAP_SYS_ADMIN))
1092 return -EACCES;
1093 }
1094
1095 if (unlikely(ioctl->cmd != cmd))
1096 goto out_io_encoding;
1097
1098 flags = ioctl->flags;
1099 } else if (!drm_ioctl_flags(nr, &flags))
1100 return -EINVAL;
1101
1102 return ioctl_func(filp, cmd, arg);
1103
1104 out_io_encoding:
1105 DRM_ERROR("Invalid command format, ioctl %d\n",
1106 nr - DRM_COMMAND_BASE);
1107
1108 return -EINVAL;
1109 }
1110
1111 static long vmw_unlocked_ioctl(struct file *filp, unsigned int cmd,
1112 unsigned long arg)
1113 {
1114 return vmw_generic_ioctl(filp, cmd, arg, &drm_ioctl);
1115 }
1116
1117 #ifdef CONFIG_COMPAT
1118 static long vmw_compat_ioctl(struct file *filp, unsigned int cmd,
1119 unsigned long arg)
1120 {
1121 return vmw_generic_ioctl(filp, cmd, arg, &drm_compat_ioctl);
1122 }
1123 #endif
1124
1125 static int vmw_master_set(struct drm_device *dev,
1126 struct drm_file *file_priv,
1127 bool from_open)
1128 {
1129 /*
1130 * Inform a new master that the layout may have changed while
1131 * it was gone.
1132 */
1133 if (!from_open)
1134 drm_sysfs_hotplug_event(dev);
1135
1136 return 0;
1137 }
1138
1139 static void vmw_master_drop(struct drm_device *dev,
1140 struct drm_file *file_priv)
1141 {
1142 struct vmw_private *dev_priv = vmw_priv(dev);
1143
1144 vmw_kms_legacy_hotspot_clear(dev_priv);
1145 if (!dev_priv->enable_fb)
1146 vmw_svga_disable(dev_priv);
1147 }
1148
1149 /**
1150 * __vmw_svga_enable - Enable SVGA mode, FIFO and use of VRAM.
1151 *
1152 * @dev_priv: Pointer to device private struct.
1153 * Needs the reservation sem to be held in non-exclusive mode.
1154 */
1155 static void __vmw_svga_enable(struct vmw_private *dev_priv)
1156 {
1157 spin_lock(&dev_priv->svga_lock);
1158 if (!dev_priv->bdev.man[TTM_PL_VRAM].use_type) {
1159 vmw_write(dev_priv, SVGA_REG_ENABLE, SVGA_REG_ENABLE);
1160 dev_priv->bdev.man[TTM_PL_VRAM].use_type = true;
1161 }
1162 spin_unlock(&dev_priv->svga_lock);
1163 }
1164
1165 /**
1166 * vmw_svga_enable - Enable SVGA mode, FIFO and use of VRAM.
1167 *
1168 * @dev_priv: Pointer to device private struct.
1169 */
1170 void vmw_svga_enable(struct vmw_private *dev_priv)
1171 {
1172 (void) ttm_read_lock(&dev_priv->reservation_sem, false);
1173 __vmw_svga_enable(dev_priv);
1174 ttm_read_unlock(&dev_priv->reservation_sem);
1175 }
1176
1177 /**
1178 * __vmw_svga_disable - Disable SVGA mode and use of VRAM.
1179 *
1180 * @dev_priv: Pointer to device private struct.
1181 * Needs the reservation sem to be held in exclusive mode.
1182 * Will not empty VRAM. VRAM must be emptied by caller.
1183 */
1184 static void __vmw_svga_disable(struct vmw_private *dev_priv)
1185 {
1186 spin_lock(&dev_priv->svga_lock);
1187 if (dev_priv->bdev.man[TTM_PL_VRAM].use_type) {
1188 dev_priv->bdev.man[TTM_PL_VRAM].use_type = false;
1189 vmw_write(dev_priv, SVGA_REG_ENABLE,
1190 SVGA_REG_ENABLE_HIDE |
1191 SVGA_REG_ENABLE_ENABLE);
1192 }
1193 spin_unlock(&dev_priv->svga_lock);
1194 }
1195
1196 /**
1197 * vmw_svga_disable - Disable SVGA_MODE, and use of VRAM. Keep the fifo
1198 * running.
1199 *
1200 * @dev_priv: Pointer to device private struct.
1201 * Will empty VRAM.
1202 */
1203 void vmw_svga_disable(struct vmw_private *dev_priv)
1204 {
1205 /*
1206 * Disabling SVGA will turn off device modesetting capabilities, so
1207 * notify KMS about that so that it doesn't cache atomic state that
1208 * isn't valid anymore, for example crtcs turned on.
1209 * Strictly we'd want to do this under the SVGA lock (or an SVGA mutex),
1210 * but vmw_kms_lost_device() takes the reservation sem and thus we'll
1211 * end up with lock order reversal. Thus, a master may actually perform
1212 * a new modeset just after we call vmw_kms_lost_device() and race with
1213 * vmw_svga_disable(), but that should at worst cause atomic KMS state
1214 * to be inconsistent with the device, causing modesetting problems.
1215 *
1216 */
1217 vmw_kms_lost_device(dev_priv->dev);
1218 ttm_write_lock(&dev_priv->reservation_sem, false);
1219 spin_lock(&dev_priv->svga_lock);
1220 if (dev_priv->bdev.man[TTM_PL_VRAM].use_type) {
1221 dev_priv->bdev.man[TTM_PL_VRAM].use_type = false;
1222 spin_unlock(&dev_priv->svga_lock);
1223 if (ttm_bo_evict_mm(&dev_priv->bdev, TTM_PL_VRAM))
1224 DRM_ERROR("Failed evicting VRAM buffers.\n");
1225 vmw_write(dev_priv, SVGA_REG_ENABLE,
1226 SVGA_REG_ENABLE_HIDE |
1227 SVGA_REG_ENABLE_ENABLE);
1228 } else
1229 spin_unlock(&dev_priv->svga_lock);
1230 ttm_write_unlock(&dev_priv->reservation_sem);
1231 }
1232
1233 static void vmw_remove(struct pci_dev *pdev)
1234 {
1235 struct drm_device *dev = pci_get_drvdata(pdev);
1236
1237 drm_dev_unregister(dev);
1238 vmw_driver_unload(dev);
1239 drm_dev_put(dev);
1240 pci_disable_device(pdev);
1241 }
1242
1243 static int vmwgfx_pm_notifier(struct notifier_block *nb, unsigned long val,
1244 void *ptr)
1245 {
1246 struct vmw_private *dev_priv =
1247 container_of(nb, struct vmw_private, pm_nb);
1248
1249 switch (val) {
1250 case PM_HIBERNATION_PREPARE:
1251 /*
1252 * Take the reservation sem in write mode, which will make sure
1253 * there are no other processes holding a buffer object
1254 * reservation, meaning we should be able to evict all buffer
1255 * objects if needed.
1256 * Once user-space processes have been frozen, we can release
1257 * the lock again.
1258 */
1259 ttm_suspend_lock(&dev_priv->reservation_sem);
1260 dev_priv->suspend_locked = true;
1261 break;
1262 case PM_POST_HIBERNATION:
1263 case PM_POST_RESTORE:
1264 if (READ_ONCE(dev_priv->suspend_locked)) {
1265 dev_priv->suspend_locked = false;
1266 ttm_suspend_unlock(&dev_priv->reservation_sem);
1267 }
1268 break;
1269 default:
1270 break;
1271 }
1272 return 0;
1273 }
1274
1275 static int vmw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1276 {
1277 struct drm_device *dev = pci_get_drvdata(pdev);
1278 struct vmw_private *dev_priv = vmw_priv(dev);
1279
1280 if (dev_priv->refuse_hibernation)
1281 return -EBUSY;
1282
1283 pci_save_state(pdev);
1284 pci_disable_device(pdev);
1285 pci_set_power_state(pdev, PCI_D3hot);
1286 return 0;
1287 }
1288
1289 static int vmw_pci_resume(struct pci_dev *pdev)
1290 {
1291 pci_set_power_state(pdev, PCI_D0);
1292 pci_restore_state(pdev);
1293 return pci_enable_device(pdev);
1294 }
1295
1296 static int vmw_pm_suspend(struct device *kdev)
1297 {
1298 struct pci_dev *pdev = to_pci_dev(kdev);
1299 struct pm_message dummy;
1300
1301 dummy.event = 0;
1302
1303 return vmw_pci_suspend(pdev, dummy);
1304 }
1305
1306 static int vmw_pm_resume(struct device *kdev)
1307 {
1308 struct pci_dev *pdev = to_pci_dev(kdev);
1309
1310 return vmw_pci_resume(pdev);
1311 }
1312
1313 static int vmw_pm_freeze(struct device *kdev)
1314 {
1315 struct pci_dev *pdev = to_pci_dev(kdev);
1316 struct drm_device *dev = pci_get_drvdata(pdev);
1317 struct vmw_private *dev_priv = vmw_priv(dev);
1318 int ret;
1319
1320 /*
1321 * Unlock for vmw_kms_suspend.
1322 * No user-space processes should be running now.
1323 */
1324 ttm_suspend_unlock(&dev_priv->reservation_sem);
1325 ret = vmw_kms_suspend(dev_priv->dev);
1326 if (ret) {
1327 ttm_suspend_lock(&dev_priv->reservation_sem);
1328 DRM_ERROR("Failed to freeze modesetting.\n");
1329 return ret;
1330 }
1331 if (dev_priv->enable_fb)
1332 vmw_fb_off(dev_priv);
1333
1334 ttm_suspend_lock(&dev_priv->reservation_sem);
1335 vmw_execbuf_release_pinned_bo(dev_priv);
1336 vmw_resource_evict_all(dev_priv);
1337 vmw_release_device_early(dev_priv);
1338 ttm_bo_swapout_all(&dev_priv->bdev);
1339 if (dev_priv->enable_fb)
1340 vmw_fifo_resource_dec(dev_priv);
1341 if (atomic_read(&dev_priv->num_fifo_resources) != 0) {
1342 DRM_ERROR("Can't hibernate while 3D resources are active.\n");
1343 if (dev_priv->enable_fb)
1344 vmw_fifo_resource_inc(dev_priv);
1345 WARN_ON(vmw_request_device_late(dev_priv));
1346 dev_priv->suspend_locked = false;
1347 ttm_suspend_unlock(&dev_priv->reservation_sem);
1348 if (dev_priv->suspend_state)
1349 vmw_kms_resume(dev);
1350 if (dev_priv->enable_fb)
1351 vmw_fb_on(dev_priv);
1352 return -EBUSY;
1353 }
1354
1355 vmw_fence_fifo_down(dev_priv->fman);
1356 __vmw_svga_disable(dev_priv);
1357
1358 vmw_release_device_late(dev_priv);
1359 return 0;
1360 }
1361
1362 static int vmw_pm_restore(struct device *kdev)
1363 {
1364 struct pci_dev *pdev = to_pci_dev(kdev);
1365 struct drm_device *dev = pci_get_drvdata(pdev);
1366 struct vmw_private *dev_priv = vmw_priv(dev);
1367 int ret;
1368
1369 vmw_write(dev_priv, SVGA_REG_ID, SVGA_ID_2);
1370 (void) vmw_read(dev_priv, SVGA_REG_ID);
1371
1372 if (dev_priv->enable_fb)
1373 vmw_fifo_resource_inc(dev_priv);
1374
1375 ret = vmw_request_device(dev_priv);
1376 if (ret)
1377 return ret;
1378
1379 if (dev_priv->enable_fb)
1380 __vmw_svga_enable(dev_priv);
1381
1382 vmw_fence_fifo_up(dev_priv->fman);
1383 dev_priv->suspend_locked = false;
1384 ttm_suspend_unlock(&dev_priv->reservation_sem);
1385 if (dev_priv->suspend_state)
1386 vmw_kms_resume(dev_priv->dev);
1387
1388 if (dev_priv->enable_fb)
1389 vmw_fb_on(dev_priv);
1390
1391 return 0;
1392 }
1393
1394 static const struct dev_pm_ops vmw_pm_ops = {
1395 .freeze = vmw_pm_freeze,
1396 .thaw = vmw_pm_restore,
1397 .restore = vmw_pm_restore,
1398 .suspend = vmw_pm_suspend,
1399 .resume = vmw_pm_resume,
1400 };
1401
1402 static const struct file_operations vmwgfx_driver_fops = {
1403 .owner = THIS_MODULE,
1404 .open = drm_open,
1405 .release = drm_release,
1406 .unlocked_ioctl = vmw_unlocked_ioctl,
1407 .mmap = vmw_mmap,
1408 .poll = vmw_fops_poll,
1409 .read = vmw_fops_read,
1410 #if defined(CONFIG_COMPAT)
1411 .compat_ioctl = vmw_compat_ioctl,
1412 #endif
1413 .llseek = noop_llseek,
1414 };
1415
1416 static struct drm_driver driver = {
1417 .driver_features =
1418 DRIVER_MODESET | DRIVER_RENDER | DRIVER_ATOMIC,
1419 .get_vblank_counter = vmw_get_vblank_counter,
1420 .enable_vblank = vmw_enable_vblank,
1421 .disable_vblank = vmw_disable_vblank,
1422 .ioctls = vmw_ioctls,
1423 .num_ioctls = ARRAY_SIZE(vmw_ioctls),
1424 .master_set = vmw_master_set,
1425 .master_drop = vmw_master_drop,
1426 .open = vmw_driver_open,
1427 .postclose = vmw_postclose,
1428
1429 .dumb_create = vmw_dumb_create,
1430 .dumb_map_offset = vmw_dumb_map_offset,
1431 .dumb_destroy = vmw_dumb_destroy,
1432
1433 .prime_fd_to_handle = vmw_prime_fd_to_handle,
1434 .prime_handle_to_fd = vmw_prime_handle_to_fd,
1435
1436 .fops = &vmwgfx_driver_fops,
1437 .name = VMWGFX_DRIVER_NAME,
1438 .desc = VMWGFX_DRIVER_DESC,
1439 .date = VMWGFX_DRIVER_DATE,
1440 .major = VMWGFX_DRIVER_MAJOR,
1441 .minor = VMWGFX_DRIVER_MINOR,
1442 .patchlevel = VMWGFX_DRIVER_PATCHLEVEL
1443 };
1444
1445 #ifdef __NetBSD__
1446
1447 static const struct drm_driver *const vmwgfx_driver = &driver;
1448 static const struct pci_device_id *const vmwgfx_pci_ids = vmw_pci_id_list;
1449 static const size_t vmwgfx_n_pci_ids = __arraycount(vmw_pci_id_list);
1450
1451 #else
1452
1453 static struct pci_driver vmw_pci_driver = {
1454 .name = VMWGFX_DRIVER_NAME,
1455 .id_table = vmw_pci_id_list,
1456 .probe = vmw_probe,
1457 .remove = vmw_remove,
1458 .driver = {
1459 .pm = &vmw_pm_ops
1460 }
1461 };
1462
1463 static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1464 {
1465 struct drm_device *dev;
1466 int ret;
1467
1468 ret = pci_enable_device(pdev);
1469 if (ret)
1470 return ret;
1471
1472 dev = drm_dev_alloc(&driver, &pdev->dev);
1473 if (IS_ERR(dev)) {
1474 ret = PTR_ERR(dev);
1475 goto err_pci_disable_device;
1476 }
1477
1478 dev->pdev = pdev;
1479 pci_set_drvdata(pdev, dev);
1480
1481 ret = vmw_driver_load(dev, ent->driver_data);
1482 if (ret)
1483 goto err_drm_dev_put;
1484
1485 ret = drm_dev_register(dev, ent->driver_data);
1486 if (ret)
1487 goto err_vmw_driver_unload;
1488
1489 return 0;
1490
1491 err_vmw_driver_unload:
1492 vmw_driver_unload(dev);
1493 err_drm_dev_put:
1494 drm_dev_put(dev);
1495 err_pci_disable_device:
1496 pci_disable_device(pdev);
1497 return ret;
1498 }
1499
1500 static int __init vmwgfx_init(void)
1501 {
1502 int ret;
1503
1504 if (vgacon_text_force())
1505 return -EINVAL;
1506
1507 ret = pci_register_driver(&vmw_pci_driver);
1508 if (ret)
1509 DRM_ERROR("Failed initializing DRM.\n");
1510 return ret;
1511 }
1512
1513 static void __exit vmwgfx_exit(void)
1514 {
1515 pci_unregister_driver(&vmw_pci_driver);
1516 }
1517
1518 #endif
1519
1520 module_init(vmwgfx_init);
1521 module_exit(vmwgfx_exit);
1522
1523 MODULE_AUTHOR("VMware Inc. and others");
1524 MODULE_DESCRIPTION("Standalone drm driver for the VMware SVGA device");
1525 MODULE_LICENSE("GPL and additional rights");
1526 MODULE_VERSION(__stringify(VMWGFX_DRIVER_MAJOR) "."
1527 __stringify(VMWGFX_DRIVER_MINOR) "."
1528 __stringify(VMWGFX_DRIVER_PATCHLEVEL) "."
1529 "0");
1530