1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "vmwgfx_drv.h"
31 #include "vmwgfx_bo.h"
32 #include "vmwgfx_binding.h"
33 #include "vmwgfx_devcaps.h"
34 #include "vmwgfx_mksstat.h"
35 #include "vmwgfx_vkms.h"
36 #include "ttm_object.h"
38 #include <drm/drm_client_setup.h>
39 #include <drm/drm_drv.h>
40 #include <drm/drm_fbdev_ttm.h>
41 #include <drm/drm_gem_ttm_helper.h>
42 #include <drm/drm_ioctl.h>
43 #include <drm/drm_module.h>
44 #include <drm/drm_sysfs.h>
45 #include <drm/ttm/ttm_range_manager.h>
46 #include <drm/ttm/ttm_placement.h>
47 #include <generated/utsrelease.h>
50 #include <asm/hypervisor.h>
53 #include <linux/aperture.h>
54 #include <linux/cc_platform.h>
55 #include <linux/dma-mapping.h>
56 #include <linux/module.h>
57 #include <linux/pci.h>
58 #include <linux/version.h>
59 #include <linux/vmalloc.h>
61 #define VMWGFX_DRIVER_DESC "Linux drm driver for VMware graphics devices"
64 * Fully encoded drm commands. Might move to vmw_drm.h
67 #define DRM_IOCTL_VMW_GET_PARAM \
68 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GET_PARAM, \
69 struct drm_vmw_getparam_arg)
70 #define DRM_IOCTL_VMW_ALLOC_DMABUF \
71 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_ALLOC_DMABUF, \
72 union drm_vmw_alloc_dmabuf_arg)
73 #define DRM_IOCTL_VMW_UNREF_DMABUF \
74 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_DMABUF, \
75 struct drm_vmw_unref_dmabuf_arg)
76 #define DRM_IOCTL_VMW_CURSOR_BYPASS \
77 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_CURSOR_BYPASS, \
78 struct drm_vmw_cursor_bypass_arg)
80 #define DRM_IOCTL_VMW_CONTROL_STREAM \
81 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_CONTROL_STREAM, \
82 struct drm_vmw_control_stream_arg)
83 #define DRM_IOCTL_VMW_CLAIM_STREAM \
84 DRM_IOR(DRM_COMMAND_BASE + DRM_VMW_CLAIM_STREAM, \
85 struct drm_vmw_stream_arg)
86 #define DRM_IOCTL_VMW_UNREF_STREAM \
87 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_STREAM, \
88 struct drm_vmw_stream_arg)
90 #define DRM_IOCTL_VMW_CREATE_CONTEXT \
91 DRM_IOR(DRM_COMMAND_BASE + DRM_VMW_CREATE_CONTEXT, \
92 struct drm_vmw_context_arg)
93 #define DRM_IOCTL_VMW_UNREF_CONTEXT \
94 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_CONTEXT, \
95 struct drm_vmw_context_arg)
96 #define DRM_IOCTL_VMW_CREATE_SURFACE \
97 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_SURFACE, \
98 union drm_vmw_surface_create_arg)
99 #define DRM_IOCTL_VMW_UNREF_SURFACE \
100 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_SURFACE, \
101 struct drm_vmw_surface_arg)
102 #define DRM_IOCTL_VMW_REF_SURFACE \
103 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_REF_SURFACE, \
104 union drm_vmw_surface_reference_arg)
105 #define DRM_IOCTL_VMW_EXECBUF \
106 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_EXECBUF, \
107 struct drm_vmw_execbuf_arg)
108 #define DRM_IOCTL_VMW_GET_3D_CAP \
109 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_GET_3D_CAP, \
110 struct drm_vmw_get_3d_cap_arg)
111 #define DRM_IOCTL_VMW_FENCE_WAIT \
112 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_FENCE_WAIT, \
113 struct drm_vmw_fence_wait_arg)
114 #define DRM_IOCTL_VMW_FENCE_SIGNALED \
115 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_FENCE_SIGNALED, \
116 struct drm_vmw_fence_signaled_arg)
117 #define DRM_IOCTL_VMW_FENCE_UNREF \
118 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_FENCE_UNREF, \
119 struct drm_vmw_fence_arg)
120 #define DRM_IOCTL_VMW_FENCE_EVENT \
121 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_FENCE_EVENT, \
122 struct drm_vmw_fence_event_arg)
123 #define DRM_IOCTL_VMW_PRESENT \
124 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_PRESENT, \
125 struct drm_vmw_present_arg)
126 #define DRM_IOCTL_VMW_PRESENT_READBACK \
127 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_PRESENT_READBACK, \
128 struct drm_vmw_present_readback_arg)
129 #define DRM_IOCTL_VMW_UPDATE_LAYOUT \
130 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UPDATE_LAYOUT, \
131 struct drm_vmw_update_layout_arg)
132 #define DRM_IOCTL_VMW_CREATE_SHADER \
133 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_SHADER, \
134 struct drm_vmw_shader_create_arg)
135 #define DRM_IOCTL_VMW_UNREF_SHADER \
136 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_UNREF_SHADER, \
137 struct drm_vmw_shader_arg)
138 #define DRM_IOCTL_VMW_GB_SURFACE_CREATE \
139 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_CREATE, \
140 union drm_vmw_gb_surface_create_arg)
141 #define DRM_IOCTL_VMW_GB_SURFACE_REF \
142 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_REF, \
143 union drm_vmw_gb_surface_reference_arg)
144 #define DRM_IOCTL_VMW_SYNCCPU \
145 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_SYNCCPU, \
146 struct drm_vmw_synccpu_arg)
147 #define DRM_IOCTL_VMW_CREATE_EXTENDED_CONTEXT \
148 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_CREATE_EXTENDED_CONTEXT, \
149 struct drm_vmw_context_arg)
150 #define DRM_IOCTL_VMW_GB_SURFACE_CREATE_EXT \
151 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_CREATE_EXT, \
152 union drm_vmw_gb_surface_create_ext_arg)
153 #define DRM_IOCTL_VMW_GB_SURFACE_REF_EXT \
154 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_GB_SURFACE_REF_EXT, \
155 union drm_vmw_gb_surface_reference_ext_arg)
156 #define DRM_IOCTL_VMW_MSG \
157 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_MSG, \
158 struct drm_vmw_msg_arg)
159 #define DRM_IOCTL_VMW_MKSSTAT_RESET \
160 DRM_IO(DRM_COMMAND_BASE + DRM_VMW_MKSSTAT_RESET)
161 #define DRM_IOCTL_VMW_MKSSTAT_ADD \
162 DRM_IOWR(DRM_COMMAND_BASE + DRM_VMW_MKSSTAT_ADD, \
163 struct drm_vmw_mksstat_add_arg)
164 #define DRM_IOCTL_VMW_MKSSTAT_REMOVE \
165 DRM_IOW(DRM_COMMAND_BASE + DRM_VMW_MKSSTAT_REMOVE, \
166 struct drm_vmw_mksstat_remove_arg)
172 static const struct drm_ioctl_desc vmw_ioctls[] = {
173 DRM_IOCTL_DEF_DRV(VMW_GET_PARAM, vmw_getparam_ioctl,
175 DRM_IOCTL_DEF_DRV(VMW_ALLOC_DMABUF, vmw_gem_object_create_ioctl,
177 DRM_IOCTL_DEF_DRV(VMW_UNREF_DMABUF, vmw_bo_unref_ioctl,
179 DRM_IOCTL_DEF_DRV(VMW_CURSOR_BYPASS,
180 vmw_kms_cursor_bypass_ioctl,
183 DRM_IOCTL_DEF_DRV(VMW_CONTROL_STREAM, vmw_overlay_ioctl,
185 DRM_IOCTL_DEF_DRV(VMW_CLAIM_STREAM, vmw_stream_claim_ioctl,
187 DRM_IOCTL_DEF_DRV(VMW_UNREF_STREAM, vmw_stream_unref_ioctl,
190 DRM_IOCTL_DEF_DRV(VMW_CREATE_CONTEXT, vmw_context_define_ioctl,
192 DRM_IOCTL_DEF_DRV(VMW_UNREF_CONTEXT, vmw_context_destroy_ioctl,
194 DRM_IOCTL_DEF_DRV(VMW_CREATE_SURFACE, vmw_surface_define_ioctl,
196 DRM_IOCTL_DEF_DRV(VMW_UNREF_SURFACE, vmw_surface_destroy_ioctl,
198 DRM_IOCTL_DEF_DRV(VMW_REF_SURFACE, vmw_surface_reference_ioctl,
200 DRM_IOCTL_DEF_DRV(VMW_EXECBUF, vmw_execbuf_ioctl,
202 DRM_IOCTL_DEF_DRV(VMW_FENCE_WAIT, vmw_fence_obj_wait_ioctl,
204 DRM_IOCTL_DEF_DRV(VMW_FENCE_SIGNALED,
205 vmw_fence_obj_signaled_ioctl,
207 DRM_IOCTL_DEF_DRV(VMW_FENCE_UNREF, vmw_fence_obj_unref_ioctl,
209 DRM_IOCTL_DEF_DRV(VMW_FENCE_EVENT, vmw_fence_event_ioctl,
211 DRM_IOCTL_DEF_DRV(VMW_GET_3D_CAP, vmw_get_cap_3d_ioctl,
214 /* these allow direct access to the framebuffers mark as master only */
215 DRM_IOCTL_DEF_DRV(VMW_PRESENT, vmw_present_ioctl,
216 DRM_MASTER | DRM_AUTH),
217 DRM_IOCTL_DEF_DRV(VMW_PRESENT_READBACK,
218 vmw_present_readback_ioctl,
219 DRM_MASTER | DRM_AUTH),
221 * The permissions of the below ioctl are overridden in
222 * vmw_generic_ioctl(). We require either
223 * DRM_MASTER or capable(CAP_SYS_ADMIN).
225 DRM_IOCTL_DEF_DRV(VMW_UPDATE_LAYOUT,
226 vmw_kms_update_layout_ioctl,
228 DRM_IOCTL_DEF_DRV(VMW_CREATE_SHADER,
229 vmw_shader_define_ioctl,
231 DRM_IOCTL_DEF_DRV(VMW_UNREF_SHADER,
232 vmw_shader_destroy_ioctl,
234 DRM_IOCTL_DEF_DRV(VMW_GB_SURFACE_CREATE,
235 vmw_gb_surface_define_ioctl,
237 DRM_IOCTL_DEF_DRV(VMW_GB_SURFACE_REF,
238 vmw_gb_surface_reference_ioctl,
240 DRM_IOCTL_DEF_DRV(VMW_SYNCCPU,
241 vmw_user_bo_synccpu_ioctl,
243 DRM_IOCTL_DEF_DRV(VMW_CREATE_EXTENDED_CONTEXT,
244 vmw_extended_context_define_ioctl,
246 DRM_IOCTL_DEF_DRV(VMW_GB_SURFACE_CREATE_EXT,
247 vmw_gb_surface_define_ext_ioctl,
249 DRM_IOCTL_DEF_DRV(VMW_GB_SURFACE_REF_EXT,
250 vmw_gb_surface_reference_ext_ioctl,
252 DRM_IOCTL_DEF_DRV(VMW_MSG,
255 DRM_IOCTL_DEF_DRV(VMW_MKSSTAT_RESET,
256 vmw_mksstat_reset_ioctl,
258 DRM_IOCTL_DEF_DRV(VMW_MKSSTAT_ADD,
259 vmw_mksstat_add_ioctl,
261 DRM_IOCTL_DEF_DRV(VMW_MKSSTAT_REMOVE,
262 vmw_mksstat_remove_ioctl,
266 static const struct pci_device_id vmw_pci_id_list[] = {
267 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, VMWGFX_PCI_ID_SVGA2) },
268 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, VMWGFX_PCI_ID_SVGA3) },
271 MODULE_DEVICE_TABLE(pci, vmw_pci_id_list);
273 static int vmw_restrict_iommu;
274 static int vmw_force_coherent;
275 static int vmw_restrict_dma_mask;
276 static int vmw_assume_16bpp;
278 static int vmw_probe(struct pci_dev *, const struct pci_device_id *);
279 static int vmwgfx_pm_notifier(struct notifier_block *nb, unsigned long val,
282 MODULE_PARM_DESC(restrict_iommu, "Try to limit IOMMU usage for TTM pages");
283 module_param_named(restrict_iommu, vmw_restrict_iommu, int, 0600);
284 MODULE_PARM_DESC(force_coherent, "Force coherent TTM pages");
285 module_param_named(force_coherent, vmw_force_coherent, int, 0600);
286 MODULE_PARM_DESC(restrict_dma_mask, "Restrict DMA mask to 44 bits with IOMMU");
287 module_param_named(restrict_dma_mask, vmw_restrict_dma_mask, int, 0600);
288 MODULE_PARM_DESC(assume_16bpp, "Assume 16-bpp when filtering modes");
289 module_param_named(assume_16bpp, vmw_assume_16bpp, int, 0600);
297 static const struct bitmap_name cap1_names[] = {
298 { SVGA_CAP_RECT_COPY, "rect copy" },
299 { SVGA_CAP_CURSOR, "cursor" },
300 { SVGA_CAP_CURSOR_BYPASS, "cursor bypass" },
301 { SVGA_CAP_CURSOR_BYPASS_2, "cursor bypass 2" },
302 { SVGA_CAP_8BIT_EMULATION, "8bit emulation" },
303 { SVGA_CAP_ALPHA_CURSOR, "alpha cursor" },
304 { SVGA_CAP_3D, "3D" },
305 { SVGA_CAP_EXTENDED_FIFO, "extended fifo" },
306 { SVGA_CAP_MULTIMON, "multimon" },
307 { SVGA_CAP_PITCHLOCK, "pitchlock" },
308 { SVGA_CAP_IRQMASK, "irq mask" },
309 { SVGA_CAP_DISPLAY_TOPOLOGY, "display topology" },
310 { SVGA_CAP_GMR, "gmr" },
311 { SVGA_CAP_TRACES, "traces" },
312 { SVGA_CAP_GMR2, "gmr2" },
313 { SVGA_CAP_SCREEN_OBJECT_2, "screen object 2" },
314 { SVGA_CAP_COMMAND_BUFFERS, "command buffers" },
315 { SVGA_CAP_CMD_BUFFERS_2, "command buffers 2" },
316 { SVGA_CAP_GBOBJECTS, "gbobject" },
317 { SVGA_CAP_DX, "dx" },
318 { SVGA_CAP_HP_CMD_QUEUE, "hp cmd queue" },
319 { SVGA_CAP_NO_BB_RESTRICTION, "no bb restriction" },
320 { SVGA_CAP_CAP2_REGISTER, "cap2 register" },
324 static const struct bitmap_name cap2_names[] = {
325 { SVGA_CAP2_GROW_OTABLE, "grow otable" },
326 { SVGA_CAP2_INTRA_SURFACE_COPY, "intra surface copy" },
327 { SVGA_CAP2_DX2, "dx2" },
328 { SVGA_CAP2_GB_MEMSIZE_2, "gb memsize 2" },
329 { SVGA_CAP2_SCREENDMA_REG, "screendma reg" },
330 { SVGA_CAP2_OTABLE_PTDEPTH_2, "otable ptdepth2" },
331 { SVGA_CAP2_NON_MS_TO_MS_STRETCHBLT, "non ms to ms stretchblt" },
332 { SVGA_CAP2_CURSOR_MOB, "cursor mob" },
333 { SVGA_CAP2_MSHINT, "mshint" },
334 { SVGA_CAP2_CB_MAX_SIZE_4MB, "cb max size 4mb" },
335 { SVGA_CAP2_DX3, "dx3" },
336 { SVGA_CAP2_FRAME_TYPE, "frame type" },
337 { SVGA_CAP2_COTABLE_COPY, "cotable copy" },
338 { SVGA_CAP2_TRACE_FULL_FB, "trace full fb" },
339 { SVGA_CAP2_EXTRA_REGS, "extra regs" },
340 { SVGA_CAP2_LO_STAGING, "lo staging" },
343 static void vmw_print_bitmap(struct drm_device *drm,
344 const char *prefix, uint32_t bitmap,
345 const struct bitmap_name *bnames,
351 for (i = 0; i < num_names; ++i) {
352 if ((bitmap & bnames[i].value) != 0) {
353 offset += snprintf(buf + offset,
354 ARRAY_SIZE(buf) - offset,
355 "%s, ", bnames[i].name);
356 bitmap &= ~bnames[i].value;
360 drm_info(drm, "%s: %s\n", prefix, buf);
362 drm_dbg(drm, "%s: unknown enums: %x\n", prefix, bitmap);
366 static void vmw_print_sm_type(struct vmw_private *dev_priv)
368 static const char *names[] = {
369 [VMW_SM_LEGACY] = "Legacy",
371 [VMW_SM_4_1] = "SM4_1",
373 [VMW_SM_5_1X] = "SM_5_1X",
374 [VMW_SM_MAX] = "Invalid"
376 BUILD_BUG_ON(ARRAY_SIZE(names) != (VMW_SM_MAX + 1));
377 drm_info(&dev_priv->drm, "Available shader model: %s.\n",
378 names[dev_priv->sm_type]);
382 * vmw_dummy_query_bo_create - create a bo to hold a dummy query result
384 * @dev_priv: A device private structure.
386 * This function creates a small buffer object that holds the query
387 * result for dummy queries emitted as query barriers.
388 * The function will then map the first page and initialize a pending
389 * occlusion query result structure, Finally it will unmap the buffer.
390 * No interruptible waits are done within this function.
392 * Returns an error if bo creation or initialization fails.
394 static int vmw_dummy_query_bo_create(struct vmw_private *dev_priv)
398 struct ttm_bo_kmap_obj map;
399 volatile SVGA3dQueryResult *result;
401 struct vmw_bo_params bo_params = {
402 .domain = VMW_BO_DOMAIN_SYS,
403 .busy_domain = VMW_BO_DOMAIN_SYS,
404 .bo_type = ttm_bo_type_kernel,
410 * Create the vbo as pinned, so that a tryreserve will
411 * immediately succeed. This is because we're the only
412 * user of the bo currently.
414 ret = vmw_bo_create(dev_priv, &bo_params, &vbo);
415 if (unlikely(ret != 0))
418 ret = ttm_bo_reserve(&vbo->tbo, false, true, NULL);
420 vmw_bo_pin_reserved(vbo, true);
422 ret = ttm_bo_kmap(&vbo->tbo, 0, 1, &map);
423 if (likely(ret == 0)) {
424 result = ttm_kmap_obj_virtual(&map, &dummy);
425 result->totalSize = sizeof(*result);
426 result->state = SVGA3D_QUERYSTATE_PENDING;
427 result->result32 = 0xff;
430 vmw_bo_pin_reserved(vbo, false);
431 ttm_bo_unreserve(&vbo->tbo);
433 if (unlikely(ret != 0)) {
434 DRM_ERROR("Dummy query buffer map failed.\n");
435 vmw_bo_unreference(&vbo);
437 dev_priv->dummy_query_bo = vbo;
442 static int vmw_device_init(struct vmw_private *dev_priv)
444 bool uses_fb_traces = false;
446 dev_priv->enable_state = vmw_read(dev_priv, SVGA_REG_ENABLE);
447 dev_priv->config_done_state = vmw_read(dev_priv, SVGA_REG_CONFIG_DONE);
448 dev_priv->traces_state = vmw_read(dev_priv, SVGA_REG_TRACES);
450 vmw_write(dev_priv, SVGA_REG_ENABLE, SVGA_REG_ENABLE_ENABLE |
451 SVGA_REG_ENABLE_HIDE);
453 uses_fb_traces = !vmw_cmd_supported(dev_priv) &&
454 (dev_priv->capabilities & SVGA_CAP_TRACES) != 0;
456 vmw_write(dev_priv, SVGA_REG_TRACES, uses_fb_traces);
457 dev_priv->fifo = vmw_fifo_create(dev_priv);
458 if (IS_ERR(dev_priv->fifo)) {
459 int err = PTR_ERR(dev_priv->fifo);
460 dev_priv->fifo = NULL;
462 } else if (!dev_priv->fifo) {
463 vmw_write(dev_priv, SVGA_REG_CONFIG_DONE, 1);
466 dev_priv->last_read_seqno = vmw_fence_read(dev_priv);
467 atomic_set(&dev_priv->marker_seq, dev_priv->last_read_seqno);
471 static void vmw_device_fini(struct vmw_private *vmw)
476 vmw_write(vmw, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
477 while (vmw_read(vmw, SVGA_REG_BUSY) != 0)
480 vmw->last_read_seqno = vmw_fence_read(vmw);
482 vmw_write(vmw, SVGA_REG_CONFIG_DONE,
483 vmw->config_done_state);
484 vmw_write(vmw, SVGA_REG_ENABLE,
486 vmw_write(vmw, SVGA_REG_TRACES,
489 vmw_fifo_destroy(vmw);
493 * vmw_request_device_late - Perform late device setup
495 * @dev_priv: Pointer to device private.
497 * This function performs setup of otables and enables large command
498 * buffer submission. These tasks are split out to a separate function
499 * because it reverts vmw_release_device_early and is intended to be used
500 * by an error path in the hibernation code.
502 static int vmw_request_device_late(struct vmw_private *dev_priv)
506 if (dev_priv->has_mob) {
507 ret = vmw_otables_setup(dev_priv);
508 if (unlikely(ret != 0)) {
509 DRM_ERROR("Unable to initialize "
510 "guest Memory OBjects.\n");
515 if (dev_priv->cman) {
516 ret = vmw_cmdbuf_set_pool_size(dev_priv->cman, 256*4096);
518 struct vmw_cmdbuf_man *man = dev_priv->cman;
520 dev_priv->cman = NULL;
521 vmw_cmdbuf_man_destroy(man);
528 static int vmw_request_device(struct vmw_private *dev_priv)
532 ret = vmw_device_init(dev_priv);
533 if (unlikely(ret != 0)) {
534 DRM_ERROR("Unable to initialize the device.\n");
537 vmw_fence_fifo_up(dev_priv->fman);
538 dev_priv->cman = vmw_cmdbuf_man_create(dev_priv);
539 if (IS_ERR(dev_priv->cman)) {
540 dev_priv->cman = NULL;
541 dev_priv->sm_type = VMW_SM_LEGACY;
544 ret = vmw_request_device_late(dev_priv);
548 ret = vmw_dummy_query_bo_create(dev_priv);
549 if (unlikely(ret != 0))
550 goto out_no_query_bo;
556 vmw_cmdbuf_remove_pool(dev_priv->cman);
557 if (dev_priv->has_mob) {
558 struct ttm_resource_manager *man;
560 man = ttm_manager_type(&dev_priv->bdev, VMW_PL_MOB);
561 ttm_resource_manager_evict_all(&dev_priv->bdev, man);
562 vmw_otables_takedown(dev_priv);
565 vmw_cmdbuf_man_destroy(dev_priv->cman);
567 vmw_fence_fifo_down(dev_priv->fman);
568 vmw_device_fini(dev_priv);
573 * vmw_release_device_early - Early part of fifo takedown.
575 * @dev_priv: Pointer to device private struct.
577 * This is the first part of command submission takedown, to be called before
578 * buffer management is taken down.
580 static void vmw_release_device_early(struct vmw_private *dev_priv)
583 * Previous destructions should've released
587 BUG_ON(dev_priv->pinned_bo != NULL);
589 vmw_bo_unreference(&dev_priv->dummy_query_bo);
591 vmw_cmdbuf_remove_pool(dev_priv->cman);
593 if (dev_priv->has_mob) {
594 struct ttm_resource_manager *man;
596 man = ttm_manager_type(&dev_priv->bdev, VMW_PL_MOB);
597 ttm_resource_manager_evict_all(&dev_priv->bdev, man);
598 vmw_otables_takedown(dev_priv);
603 * vmw_release_device_late - Late part of fifo takedown.
605 * @dev_priv: Pointer to device private struct.
607 * This is the last part of the command submission takedown, to be called when
608 * command submission is no longer needed. It may wait on pending fences.
610 static void vmw_release_device_late(struct vmw_private *dev_priv)
612 vmw_fence_fifo_down(dev_priv->fman);
614 vmw_cmdbuf_man_destroy(dev_priv->cman);
616 vmw_device_fini(dev_priv);
620 * Sets the initial_[width|height] fields on the given vmw_private.
622 * It does so by reading SVGA_REG_[WIDTH|HEIGHT] regs and then
623 * clamping the value to fb_max_[width|height] fields and the
624 * VMW_MIN_INITIAL_[WIDTH|HEIGHT].
625 * If the values appear to be invalid, set them to
626 * VMW_MIN_INITIAL_[WIDTH|HEIGHT].
628 static void vmw_get_initial_size(struct vmw_private *dev_priv)
633 width = vmw_read(dev_priv, SVGA_REG_WIDTH);
634 height = vmw_read(dev_priv, SVGA_REG_HEIGHT);
636 width = max_t(uint32_t, width, VMWGFX_MIN_INITIAL_WIDTH);
637 height = max_t(uint32_t, height, VMWGFX_MIN_INITIAL_HEIGHT);
639 if (width > dev_priv->fb_max_width ||
640 height > dev_priv->fb_max_height) {
643 * This is a host error and shouldn't occur.
646 width = VMWGFX_MIN_INITIAL_WIDTH;
647 height = VMWGFX_MIN_INITIAL_HEIGHT;
650 dev_priv->initial_width = width;
651 dev_priv->initial_height = height;
655 * vmw_dma_select_mode - Determine how DMA mappings should be set up for this
658 * @dev_priv: Pointer to a struct vmw_private
660 * This functions tries to determine what actions need to be taken by the
661 * driver to make system pages visible to the device.
662 * If this function decides that DMA is not possible, it returns -EINVAL.
663 * The driver may then try to disable features of the device that require
666 static int vmw_dma_select_mode(struct vmw_private *dev_priv)
668 static const char *names[vmw_dma_map_max] = {
669 [vmw_dma_alloc_coherent] = "Using coherent TTM pages.",
670 [vmw_dma_map_populate] = "Caching DMA mappings.",
671 [vmw_dma_map_bind] = "Giving up DMA mappings early."};
674 * When running with SEV we always want dma mappings, because
675 * otherwise ttm tt pool pages will bounce through swiotlb running
676 * out of available space.
678 if (vmw_force_coherent || cc_platform_has(CC_ATTR_MEM_ENCRYPT))
679 dev_priv->map_mode = vmw_dma_alloc_coherent;
680 else if (vmw_restrict_iommu)
681 dev_priv->map_mode = vmw_dma_map_bind;
683 dev_priv->map_mode = vmw_dma_map_populate;
685 drm_info(&dev_priv->drm,
686 "DMA map mode: %s\n", names[dev_priv->map_mode]);
691 * vmw_dma_masks - set required page- and dma masks
693 * @dev_priv: Pointer to struct drm-device
695 * With 32-bit we can only handle 32 bit PFNs. Optionally set that
696 * restriction also for 64-bit systems.
698 static int vmw_dma_masks(struct vmw_private *dev_priv)
700 struct drm_device *dev = &dev_priv->drm;
703 ret = dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(64));
704 if (sizeof(unsigned long) == 4 || vmw_restrict_dma_mask) {
705 drm_info(&dev_priv->drm,
706 "Restricting DMA addresses to 44 bits.\n");
707 return dma_set_mask_and_coherent(dev->dev, DMA_BIT_MASK(44));
713 static int vmw_vram_manager_init(struct vmw_private *dev_priv)
716 ret = ttm_range_man_init(&dev_priv->bdev, TTM_PL_VRAM, false,
717 dev_priv->vram_size >> PAGE_SHIFT);
718 ttm_resource_manager_set_used(ttm_manager_type(&dev_priv->bdev, TTM_PL_VRAM), false);
722 static void vmw_vram_manager_fini(struct vmw_private *dev_priv)
724 ttm_range_man_fini(&dev_priv->bdev, TTM_PL_VRAM);
727 static int vmw_setup_pci_resources(struct vmw_private *dev,
730 resource_size_t rmmio_start;
731 resource_size_t rmmio_size;
732 resource_size_t fifo_start;
733 resource_size_t fifo_size;
735 struct pci_dev *pdev = to_pci_dev(dev->drm.dev);
737 pci_set_master(pdev);
739 ret = pci_request_regions(pdev, "vmwgfx probe");
743 dev->pci_id = pci_id;
744 if (pci_id == VMWGFX_PCI_ID_SVGA3) {
745 rmmio_start = pci_resource_start(pdev, 0);
746 rmmio_size = pci_resource_len(pdev, 0);
747 dev->vram_start = pci_resource_start(pdev, 2);
748 dev->vram_size = pci_resource_len(pdev, 2);
751 "Register MMIO at 0x%pa size is %llu KiB\n",
752 &rmmio_start, (uint64_t)rmmio_size / 1024);
753 dev->rmmio = devm_ioremap(dev->drm.dev,
758 "Failed mapping registers mmio memory.\n");
759 pci_release_regions(pdev);
762 } else if (pci_id == VMWGFX_PCI_ID_SVGA2) {
763 dev->io_start = pci_resource_start(pdev, 0);
764 dev->vram_start = pci_resource_start(pdev, 1);
765 dev->vram_size = pci_resource_len(pdev, 1);
766 fifo_start = pci_resource_start(pdev, 2);
767 fifo_size = pci_resource_len(pdev, 2);
770 "FIFO at %pa size is %llu KiB\n",
771 &fifo_start, (uint64_t)fifo_size / 1024);
772 dev->fifo_mem = devm_memremap(dev->drm.dev,
777 if (IS_ERR(dev->fifo_mem)) {
779 "Failed mapping FIFO memory.\n");
780 pci_release_regions(pdev);
781 return PTR_ERR(dev->fifo_mem);
784 pci_release_regions(pdev);
789 * This is approximate size of the vram, the exact size will only
790 * be known after we read SVGA_REG_VRAM_SIZE. The PCI resource
791 * size will be equal to or bigger than the size reported by
792 * SVGA_REG_VRAM_SIZE.
795 "VRAM at %pa size is %llu KiB\n",
796 &dev->vram_start, (uint64_t)dev->vram_size / 1024);
801 static int vmw_detect_version(struct vmw_private *dev)
805 vmw_write(dev, SVGA_REG_ID, vmw_is_svga_v3(dev) ?
806 SVGA_ID_3 : SVGA_ID_2);
807 svga_id = vmw_read(dev, SVGA_REG_ID);
808 if (svga_id != SVGA_ID_2 && svga_id != SVGA_ID_3) {
810 "Unsupported SVGA ID 0x%x on chipset 0x%x\n",
811 svga_id, dev->pci_id);
814 BUG_ON(vmw_is_svga_v3(dev) && (svga_id != SVGA_ID_3));
816 "Running on SVGA version %d.\n", (svga_id & 0xff));
820 static void vmw_write_driver_id(struct vmw_private *dev)
822 if ((dev->capabilities2 & SVGA_CAP2_DX2) != 0) {
823 vmw_write(dev, SVGA_REG_GUEST_DRIVER_ID,
824 SVGA_REG_GUEST_DRIVER_ID_LINUX);
826 vmw_write(dev, SVGA_REG_GUEST_DRIVER_VERSION1,
827 LINUX_VERSION_MAJOR << 24 |
828 LINUX_VERSION_PATCHLEVEL << 16 |
829 LINUX_VERSION_SUBLEVEL);
830 vmw_write(dev, SVGA_REG_GUEST_DRIVER_VERSION2,
831 VMWGFX_DRIVER_MAJOR << 24 |
832 VMWGFX_DRIVER_MINOR << 16 |
833 VMWGFX_DRIVER_PATCHLEVEL);
834 vmw_write(dev, SVGA_REG_GUEST_DRIVER_VERSION3, 0);
836 vmw_write(dev, SVGA_REG_GUEST_DRIVER_ID,
837 SVGA_REG_GUEST_DRIVER_ID_SUBMIT);
841 static void vmw_sw_context_init(struct vmw_private *dev_priv)
843 struct vmw_sw_context *sw_context = &dev_priv->ctx;
845 hash_init(sw_context->res_ht);
848 static void vmw_sw_context_fini(struct vmw_private *dev_priv)
850 struct vmw_sw_context *sw_context = &dev_priv->ctx;
852 vfree(sw_context->cmd_bounce);
853 if (sw_context->staged_bindings)
854 vmw_binding_state_free(sw_context->staged_bindings);
857 static int vmw_driver_load(struct vmw_private *dev_priv, u32 pci_id)
861 bool refuse_dma = false;
862 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
864 vmw_sw_context_init(dev_priv);
866 mutex_init(&dev_priv->cmdbuf_mutex);
867 mutex_init(&dev_priv->binding_mutex);
868 spin_lock_init(&dev_priv->resource_lock);
869 spin_lock_init(&dev_priv->hw_lock);
870 spin_lock_init(&dev_priv->waiter_lock);
871 spin_lock_init(&dev_priv->cursor_lock);
873 ret = vmw_setup_pci_resources(dev_priv, pci_id);
876 ret = vmw_detect_version(dev_priv);
878 goto out_no_pci_or_version;
881 for (i = vmw_res_context; i < vmw_res_max; ++i) {
882 idr_init_base(&dev_priv->res_idr[i], 1);
883 INIT_LIST_HEAD(&dev_priv->res_lru[i]);
886 init_waitqueue_head(&dev_priv->fence_queue);
887 init_waitqueue_head(&dev_priv->fifo_queue);
888 dev_priv->fence_queue_waiters = 0;
889 dev_priv->fifo_queue_waiters = 0;
891 dev_priv->used_memory_size = 0;
893 dev_priv->assume_16bpp = !!vmw_assume_16bpp;
895 dev_priv->capabilities = vmw_read(dev_priv, SVGA_REG_CAPABILITIES);
896 vmw_print_bitmap(&dev_priv->drm, "Capabilities",
897 dev_priv->capabilities,
898 cap1_names, ARRAY_SIZE(cap1_names));
899 if (dev_priv->capabilities & SVGA_CAP_CAP2_REGISTER) {
900 dev_priv->capabilities2 = vmw_read(dev_priv, SVGA_REG_CAP2);
901 vmw_print_bitmap(&dev_priv->drm, "Capabilities2",
902 dev_priv->capabilities2,
903 cap2_names, ARRAY_SIZE(cap2_names));
906 if (!vmwgfx_supported(dev_priv)) {
907 vmw_disable_backdoor();
908 drm_err_once(&dev_priv->drm,
909 "vmwgfx seems to be running on an unsupported hypervisor.");
910 drm_err_once(&dev_priv->drm,
911 "This configuration is likely broken.");
912 drm_err_once(&dev_priv->drm,
913 "Please switch to a supported graphics device to avoid problems.");
916 vmw_vkms_init(dev_priv);
918 ret = vmw_dma_select_mode(dev_priv);
919 if (unlikely(ret != 0)) {
920 drm_info(&dev_priv->drm,
921 "Restricting capabilities since DMA not available.\n");
923 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS)
924 drm_info(&dev_priv->drm,
925 "Disabling 3D acceleration.\n");
928 dev_priv->vram_size = vmw_read(dev_priv, SVGA_REG_VRAM_SIZE);
929 dev_priv->fifo_mem_size = vmw_read(dev_priv, SVGA_REG_MEM_SIZE);
930 dev_priv->fb_max_width = vmw_read(dev_priv, SVGA_REG_MAX_WIDTH);
931 dev_priv->fb_max_height = vmw_read(dev_priv, SVGA_REG_MAX_HEIGHT);
933 vmw_get_initial_size(dev_priv);
935 if (dev_priv->capabilities & SVGA_CAP_GMR2) {
936 dev_priv->max_gmr_ids =
937 vmw_read(dev_priv, SVGA_REG_GMR_MAX_IDS);
938 dev_priv->max_gmr_pages =
939 vmw_read(dev_priv, SVGA_REG_GMRS_MAX_PAGES);
940 dev_priv->memory_size =
941 vmw_read(dev_priv, SVGA_REG_MEMORY_SIZE);
942 dev_priv->memory_size -= dev_priv->vram_size;
945 * An arbitrary limit of 512MiB on surface
946 * memory. But all HWV8 hardware supports GMR2.
948 dev_priv->memory_size = 512*1024*1024;
950 dev_priv->max_mob_pages = 0;
951 dev_priv->max_mob_size = 0;
952 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS) {
955 if (dev_priv->capabilities2 & SVGA_CAP2_GB_MEMSIZE_2)
956 mem_size = vmw_read(dev_priv,
957 SVGA_REG_GBOBJECT_MEM_SIZE_KB);
961 SVGA_REG_SUGGESTED_GBOBJECT_MEM_SIZE_KB);
963 dev_priv->max_mob_pages = mem_size * 1024 / PAGE_SIZE;
964 dev_priv->max_primary_mem =
965 vmw_read(dev_priv, SVGA_REG_MAX_PRIMARY_MEM);
966 dev_priv->max_mob_size =
967 vmw_read(dev_priv, SVGA_REG_MOB_MAX_SIZE);
968 dev_priv->stdu_max_width =
969 vmw_read(dev_priv, SVGA_REG_SCREENTARGET_MAX_WIDTH);
970 dev_priv->stdu_max_height =
971 vmw_read(dev_priv, SVGA_REG_SCREENTARGET_MAX_HEIGHT);
973 vmw_write(dev_priv, SVGA_REG_DEV_CAP,
974 SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH);
975 dev_priv->texture_max_width = vmw_read(dev_priv,
977 vmw_write(dev_priv, SVGA_REG_DEV_CAP,
978 SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT);
979 dev_priv->texture_max_height = vmw_read(dev_priv,
982 dev_priv->texture_max_width = 8192;
983 dev_priv->texture_max_height = 8192;
984 dev_priv->max_primary_mem = dev_priv->vram_size;
986 drm_info(&dev_priv->drm,
987 "Legacy memory limits: VRAM = %llu KiB, FIFO = %llu KiB, surface = %u KiB\n",
988 (u64)dev_priv->vram_size / 1024,
989 (u64)dev_priv->fifo_mem_size / 1024,
990 dev_priv->memory_size / 1024);
992 drm_info(&dev_priv->drm,
993 "MOB limits: max mob size = %u KiB, max mob pages = %u\n",
994 dev_priv->max_mob_size / 1024, dev_priv->max_mob_pages);
996 ret = vmw_dma_masks(dev_priv);
997 if (unlikely(ret != 0))
1000 dma_set_max_seg_size(dev_priv->drm.dev, U32_MAX);
1002 if (dev_priv->capabilities & SVGA_CAP_GMR2) {
1003 drm_info(&dev_priv->drm,
1004 "Max GMR ids is %u\n",
1005 (unsigned)dev_priv->max_gmr_ids);
1006 drm_info(&dev_priv->drm,
1007 "Max number of GMR pages is %u\n",
1008 (unsigned)dev_priv->max_gmr_pages);
1010 drm_info(&dev_priv->drm,
1011 "Maximum display memory size is %llu KiB\n",
1012 (uint64_t)dev_priv->max_primary_mem / 1024);
1014 /* Need mmio memory to check for fifo pitchlock cap. */
1015 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY) &&
1016 !(dev_priv->capabilities & SVGA_CAP_PITCHLOCK) &&
1017 !vmw_fifo_have_pitchlock(dev_priv)) {
1019 DRM_ERROR("Hardware has no pitchlock\n");
1023 dev_priv->tdev = ttm_object_device_init(&vmw_prime_dmabuf_ops);
1025 if (unlikely(dev_priv->tdev == NULL)) {
1026 drm_err(&dev_priv->drm,
1027 "Unable to initialize TTM object management.\n");
1032 if (dev_priv->capabilities & SVGA_CAP_IRQMASK) {
1033 ret = vmw_irq_install(dev_priv);
1035 drm_err(&dev_priv->drm,
1036 "Failed installing irq: %d\n", ret);
1041 dev_priv->fman = vmw_fence_manager_init(dev_priv);
1042 if (unlikely(dev_priv->fman == NULL)) {
1047 ret = ttm_device_init(&dev_priv->bdev, &vmw_bo_driver,
1049 dev_priv->drm.anon_inode->i_mapping,
1050 dev_priv->drm.vma_offset_manager,
1051 dev_priv->map_mode == vmw_dma_alloc_coherent,
1053 if (unlikely(ret != 0)) {
1054 drm_err(&dev_priv->drm,
1055 "Failed initializing TTM buffer object driver.\n");
1060 * Enable VRAM, but initially don't use it until SVGA is enabled and
1064 ret = vmw_vram_manager_init(dev_priv);
1065 if (unlikely(ret != 0)) {
1066 drm_err(&dev_priv->drm,
1067 "Failed initializing memory manager for VRAM.\n");
1071 ret = vmw_devcaps_create(dev_priv);
1072 if (unlikely(ret != 0)) {
1073 drm_err(&dev_priv->drm,
1074 "Failed initializing device caps.\n");
1079 * "Guest Memory Regions" is an aperture like feature with
1080 * one slot per bo. There is an upper limit of the number of
1081 * slots as well as the bo size.
1083 dev_priv->has_gmr = true;
1084 /* TODO: This is most likely not correct */
1085 if (((dev_priv->capabilities & (SVGA_CAP_GMR | SVGA_CAP_GMR2)) == 0) ||
1087 vmw_gmrid_man_init(dev_priv, VMW_PL_GMR) != 0) {
1088 drm_info(&dev_priv->drm,
1089 "No GMR memory available. "
1090 "Graphics memory resources are very limited.\n");
1091 dev_priv->has_gmr = false;
1094 if (dev_priv->capabilities & SVGA_CAP_GBOBJECTS && !refuse_dma) {
1095 dev_priv->has_mob = true;
1097 if (vmw_gmrid_man_init(dev_priv, VMW_PL_MOB) != 0) {
1098 drm_info(&dev_priv->drm,
1099 "No MOB memory available. "
1100 "3D will be disabled.\n");
1101 dev_priv->has_mob = false;
1103 if (vmw_sys_man_init(dev_priv) != 0) {
1104 drm_info(&dev_priv->drm,
1105 "No MOB page table memory available. "
1106 "3D will be disabled.\n");
1107 dev_priv->has_mob = false;
1111 if (dev_priv->has_mob && (dev_priv->capabilities & SVGA_CAP_DX)) {
1112 if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_DXCONTEXT))
1113 dev_priv->sm_type = VMW_SM_4;
1116 /* SVGA_CAP2_DX2 (DefineGBSurface_v3) is needed for SM4_1 support */
1117 if (has_sm4_context(dev_priv) &&
1118 (dev_priv->capabilities2 & SVGA_CAP2_DX2)) {
1119 if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_SM41))
1120 dev_priv->sm_type = VMW_SM_4_1;
1121 if (has_sm4_1_context(dev_priv) &&
1122 (dev_priv->capabilities2 & SVGA_CAP2_DX3)) {
1123 if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_SM5)) {
1124 dev_priv->sm_type = VMW_SM_5;
1125 if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_GL43))
1126 dev_priv->sm_type = VMW_SM_5_1X;
1131 ret = vmw_kms_init(dev_priv);
1132 if (unlikely(ret != 0))
1134 vmw_overlay_init(dev_priv);
1136 ret = vmw_request_device(dev_priv);
1140 vmw_print_sm_type(dev_priv);
1141 vmw_host_printf("vmwgfx: Module Version: %d.%d.%d (kernel: %s)",
1142 VMWGFX_DRIVER_MAJOR, VMWGFX_DRIVER_MINOR,
1143 VMWGFX_DRIVER_PATCHLEVEL, UTS_RELEASE);
1144 vmw_write_driver_id(dev_priv);
1146 dev_priv->pm_nb.notifier_call = vmwgfx_pm_notifier;
1147 register_pm_notifier(&dev_priv->pm_nb);
1152 vmw_overlay_close(dev_priv);
1153 vmw_kms_close(dev_priv);
1155 if (dev_priv->has_mob) {
1156 vmw_gmrid_man_fini(dev_priv, VMW_PL_MOB);
1157 vmw_sys_man_fini(dev_priv);
1159 if (dev_priv->has_gmr)
1160 vmw_gmrid_man_fini(dev_priv, VMW_PL_GMR);
1161 vmw_devcaps_destroy(dev_priv);
1162 vmw_vram_manager_fini(dev_priv);
1164 ttm_device_fini(&dev_priv->bdev);
1166 vmw_fence_manager_takedown(dev_priv->fman);
1168 if (dev_priv->capabilities & SVGA_CAP_IRQMASK)
1169 vmw_irq_uninstall(&dev_priv->drm);
1171 ttm_object_device_release(&dev_priv->tdev);
1173 for (i = vmw_res_context; i < vmw_res_max; ++i)
1174 idr_destroy(&dev_priv->res_idr[i]);
1176 if (dev_priv->ctx.staged_bindings)
1177 vmw_binding_state_free(dev_priv->ctx.staged_bindings);
1178 out_no_pci_or_version:
1179 pci_release_regions(pdev);
1183 static void vmw_driver_unload(struct drm_device *dev)
1185 struct vmw_private *dev_priv = vmw_priv(dev);
1186 struct pci_dev *pdev = to_pci_dev(dev->dev);
1187 enum vmw_res_type i;
1189 unregister_pm_notifier(&dev_priv->pm_nb);
1191 vmw_sw_context_fini(dev_priv);
1192 vmw_fifo_resource_dec(dev_priv);
1194 vmw_svga_disable(dev_priv);
1196 vmw_vkms_cleanup(dev_priv);
1197 vmw_kms_close(dev_priv);
1198 vmw_overlay_close(dev_priv);
1200 if (dev_priv->has_gmr)
1201 vmw_gmrid_man_fini(dev_priv, VMW_PL_GMR);
1203 vmw_release_device_early(dev_priv);
1204 if (dev_priv->has_mob) {
1205 vmw_gmrid_man_fini(dev_priv, VMW_PL_MOB);
1206 vmw_sys_man_fini(dev_priv);
1208 vmw_devcaps_destroy(dev_priv);
1209 vmw_vram_manager_fini(dev_priv);
1210 ttm_device_fini(&dev_priv->bdev);
1211 vmw_release_device_late(dev_priv);
1212 vmw_fence_manager_takedown(dev_priv->fman);
1213 if (dev_priv->capabilities & SVGA_CAP_IRQMASK)
1214 vmw_irq_uninstall(&dev_priv->drm);
1216 ttm_object_device_release(&dev_priv->tdev);
1218 for (i = vmw_res_context; i < vmw_res_max; ++i)
1219 idr_destroy(&dev_priv->res_idr[i]);
1221 vmw_mksstat_remove_all(dev_priv);
1223 pci_release_regions(pdev);
1226 static void vmw_postclose(struct drm_device *dev,
1227 struct drm_file *file_priv)
1229 struct vmw_fpriv *vmw_fp = vmw_fpriv(file_priv);
1231 ttm_object_file_release(&vmw_fp->tfile);
1235 static int vmw_driver_open(struct drm_device *dev, struct drm_file *file_priv)
1237 struct vmw_private *dev_priv = vmw_priv(dev);
1238 struct vmw_fpriv *vmw_fp;
1241 vmw_fp = kzalloc(sizeof(*vmw_fp), GFP_KERNEL);
1242 if (unlikely(!vmw_fp))
1245 vmw_fp->tfile = ttm_object_file_init(dev_priv->tdev);
1246 if (unlikely(vmw_fp->tfile == NULL))
1249 file_priv->driver_priv = vmw_fp;
1258 static long vmw_generic_ioctl(struct file *filp, unsigned int cmd,
1260 long (*ioctl_func)(struct file *, unsigned int,
1263 struct drm_file *file_priv = filp->private_data;
1264 struct drm_device *dev = file_priv->minor->dev;
1265 unsigned int nr = DRM_IOCTL_NR(cmd);
1269 * Do extra checking on driver private ioctls.
1272 if ((nr >= DRM_COMMAND_BASE) && (nr < DRM_COMMAND_END)
1273 && (nr < DRM_COMMAND_BASE + dev->driver->num_ioctls)) {
1274 const struct drm_ioctl_desc *ioctl =
1275 &vmw_ioctls[nr - DRM_COMMAND_BASE];
1277 if (nr == DRM_COMMAND_BASE + DRM_VMW_EXECBUF) {
1278 return ioctl_func(filp, cmd, arg);
1279 } else if (nr == DRM_COMMAND_BASE + DRM_VMW_UPDATE_LAYOUT) {
1280 if (!drm_is_current_master(file_priv) &&
1281 !capable(CAP_SYS_ADMIN))
1285 if (unlikely(ioctl->cmd != cmd))
1286 goto out_io_encoding;
1288 flags = ioctl->flags;
1289 } else if (!drm_ioctl_flags(nr, &flags))
1292 return ioctl_func(filp, cmd, arg);
1295 DRM_ERROR("Invalid command format, ioctl %d\n",
1296 nr - DRM_COMMAND_BASE);
1301 static long vmw_unlocked_ioctl(struct file *filp, unsigned int cmd,
1304 return vmw_generic_ioctl(filp, cmd, arg, &drm_ioctl);
1307 #ifdef CONFIG_COMPAT
1308 static long vmw_compat_ioctl(struct file *filp, unsigned int cmd,
1311 return vmw_generic_ioctl(filp, cmd, arg, &drm_compat_ioctl);
1315 static void vmw_master_set(struct drm_device *dev,
1316 struct drm_file *file_priv,
1320 * Inform a new master that the layout may have changed while
1324 drm_sysfs_hotplug_event(dev);
1327 static void vmw_master_drop(struct drm_device *dev,
1328 struct drm_file *file_priv)
1330 struct vmw_private *dev_priv = vmw_priv(dev);
1332 vmw_kms_legacy_hotspot_clear(dev_priv);
1335 bool vmwgfx_supported(struct vmw_private *vmw)
1337 #if defined(CONFIG_X86)
1338 return hypervisor_is_type(X86_HYPER_VMWARE);
1339 #elif defined(CONFIG_ARM64)
1341 * On aarch64 only svga3 is supported
1343 return vmw->pci_id == VMWGFX_PCI_ID_SVGA3;
1345 drm_warn_once(&vmw->drm,
1346 "vmwgfx is running on an unknown architecture.");
1352 * __vmw_svga_enable - Enable SVGA mode, FIFO and use of VRAM.
1354 * @dev_priv: Pointer to device private struct.
1355 * Needs the reservation sem to be held in non-exclusive mode.
1357 static void __vmw_svga_enable(struct vmw_private *dev_priv)
1359 struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, TTM_PL_VRAM);
1361 if (!ttm_resource_manager_used(man)) {
1362 vmw_write(dev_priv, SVGA_REG_ENABLE, SVGA_REG_ENABLE_ENABLE);
1363 ttm_resource_manager_set_used(man, true);
1368 * vmw_svga_enable - Enable SVGA mode, FIFO and use of VRAM.
1370 * @dev_priv: Pointer to device private struct.
1372 void vmw_svga_enable(struct vmw_private *dev_priv)
1374 __vmw_svga_enable(dev_priv);
1378 * __vmw_svga_disable - Disable SVGA mode and use of VRAM.
1380 * @dev_priv: Pointer to device private struct.
1381 * Needs the reservation sem to be held in exclusive mode.
1382 * Will not empty VRAM. VRAM must be emptied by caller.
1384 static void __vmw_svga_disable(struct vmw_private *dev_priv)
1386 struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, TTM_PL_VRAM);
1388 if (ttm_resource_manager_used(man)) {
1389 ttm_resource_manager_set_used(man, false);
1390 vmw_write(dev_priv, SVGA_REG_ENABLE,
1391 SVGA_REG_ENABLE_HIDE |
1392 SVGA_REG_ENABLE_ENABLE);
1397 * vmw_svga_disable - Disable SVGA_MODE, and use of VRAM. Keep the fifo
1400 * @dev_priv: Pointer to device private struct.
1403 void vmw_svga_disable(struct vmw_private *dev_priv)
1405 struct ttm_resource_manager *man = ttm_manager_type(&dev_priv->bdev, TTM_PL_VRAM);
1407 * Disabling SVGA will turn off device modesetting capabilities, so
1408 * notify KMS about that so that it doesn't cache atomic state that
1409 * isn't valid anymore, for example crtcs turned on.
1410 * Strictly we'd want to do this under the SVGA lock (or an SVGA mutex),
1411 * but vmw_kms_lost_device() takes the reservation sem and thus we'll
1412 * end up with lock order reversal. Thus, a master may actually perform
1413 * a new modeset just after we call vmw_kms_lost_device() and race with
1414 * vmw_svga_disable(), but that should at worst cause atomic KMS state
1415 * to be inconsistent with the device, causing modesetting problems.
1418 vmw_kms_lost_device(&dev_priv->drm);
1419 if (ttm_resource_manager_used(man)) {
1420 if (ttm_resource_manager_evict_all(&dev_priv->bdev, man))
1421 DRM_ERROR("Failed evicting VRAM buffers.\n");
1422 ttm_resource_manager_set_used(man, false);
1423 vmw_write(dev_priv, SVGA_REG_ENABLE,
1424 SVGA_REG_ENABLE_HIDE |
1425 SVGA_REG_ENABLE_ENABLE);
1429 static void vmw_remove(struct pci_dev *pdev)
1431 struct drm_device *dev = pci_get_drvdata(pdev);
1433 drm_dev_unregister(dev);
1434 vmw_driver_unload(dev);
1437 static void vmw_debugfs_resource_managers_init(struct vmw_private *vmw)
1439 struct drm_minor *minor = vmw->drm.primary;
1440 struct dentry *root = minor->debugfs_root;
1442 ttm_resource_manager_create_debugfs(ttm_manager_type(&vmw->bdev, TTM_PL_SYSTEM),
1443 root, "system_ttm");
1444 ttm_resource_manager_create_debugfs(ttm_manager_type(&vmw->bdev, TTM_PL_VRAM),
1447 ttm_resource_manager_create_debugfs(ttm_manager_type(&vmw->bdev, VMW_PL_GMR),
1450 ttm_resource_manager_create_debugfs(ttm_manager_type(&vmw->bdev, VMW_PL_MOB),
1452 ttm_resource_manager_create_debugfs(ttm_manager_type(&vmw->bdev, VMW_PL_SYSTEM),
1453 root, "system_mob_ttm");
1457 static int vmwgfx_pm_notifier(struct notifier_block *nb, unsigned long val,
1460 struct vmw_private *dev_priv =
1461 container_of(nb, struct vmw_private, pm_nb);
1464 case PM_HIBERNATION_PREPARE:
1466 * Take the reservation sem in write mode, which will make sure
1467 * there are no other processes holding a buffer object
1468 * reservation, meaning we should be able to evict all buffer
1469 * objects if needed.
1470 * Once user-space processes have been frozen, we can release
1473 dev_priv->suspend_locked = true;
1475 case PM_POST_HIBERNATION:
1476 case PM_POST_RESTORE:
1477 if (READ_ONCE(dev_priv->suspend_locked)) {
1478 dev_priv->suspend_locked = false;
1487 static int vmw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1489 struct drm_device *dev = pci_get_drvdata(pdev);
1490 struct vmw_private *dev_priv = vmw_priv(dev);
1492 if (dev_priv->refuse_hibernation)
1495 pci_save_state(pdev);
1496 pci_disable_device(pdev);
1497 pci_set_power_state(pdev, PCI_D3hot);
1501 static int vmw_pci_resume(struct pci_dev *pdev)
1503 pci_set_power_state(pdev, PCI_D0);
1504 pci_restore_state(pdev);
1505 return pci_enable_device(pdev);
1508 static int vmw_pm_suspend(struct device *kdev)
1510 struct pci_dev *pdev = to_pci_dev(kdev);
1511 struct pm_message dummy;
1515 return vmw_pci_suspend(pdev, dummy);
1518 static int vmw_pm_resume(struct device *kdev)
1520 struct pci_dev *pdev = to_pci_dev(kdev);
1522 return vmw_pci_resume(pdev);
1525 static int vmw_pm_freeze(struct device *kdev)
1527 struct pci_dev *pdev = to_pci_dev(kdev);
1528 struct drm_device *dev = pci_get_drvdata(pdev);
1529 struct vmw_private *dev_priv = vmw_priv(dev);
1530 struct ttm_operation_ctx ctx = {
1531 .interruptible = false,
1532 .no_wait_gpu = false
1537 * No user-space processes should be running now.
1539 ret = vmw_kms_suspend(&dev_priv->drm);
1541 DRM_ERROR("Failed to freeze modesetting.\n");
1545 vmw_execbuf_release_pinned_bo(dev_priv);
1546 vmw_resource_evict_all(dev_priv);
1547 vmw_release_device_early(dev_priv);
1548 while (ttm_device_swapout(&dev_priv->bdev, &ctx, GFP_KERNEL) > 0);
1549 vmw_fifo_resource_dec(dev_priv);
1550 if (atomic_read(&dev_priv->num_fifo_resources) != 0) {
1551 DRM_ERROR("Can't hibernate while 3D resources are active.\n");
1552 vmw_fifo_resource_inc(dev_priv);
1553 WARN_ON(vmw_request_device_late(dev_priv));
1554 dev_priv->suspend_locked = false;
1555 if (dev_priv->suspend_state)
1556 vmw_kms_resume(dev);
1560 vmw_fence_fifo_down(dev_priv->fman);
1561 __vmw_svga_disable(dev_priv);
1563 vmw_release_device_late(dev_priv);
1567 static int vmw_pm_restore(struct device *kdev)
1569 struct pci_dev *pdev = to_pci_dev(kdev);
1570 struct drm_device *dev = pci_get_drvdata(pdev);
1571 struct vmw_private *dev_priv = vmw_priv(dev);
1574 vmw_detect_version(dev_priv);
1576 vmw_fifo_resource_inc(dev_priv);
1578 ret = vmw_request_device(dev_priv);
1582 __vmw_svga_enable(dev_priv);
1584 vmw_fence_fifo_up(dev_priv->fman);
1585 dev_priv->suspend_locked = false;
1586 if (dev_priv->suspend_state)
1587 vmw_kms_resume(&dev_priv->drm);
1592 static const struct dev_pm_ops vmw_pm_ops = {
1593 .freeze = vmw_pm_freeze,
1594 .thaw = vmw_pm_restore,
1595 .restore = vmw_pm_restore,
1596 .suspend = vmw_pm_suspend,
1597 .resume = vmw_pm_resume,
1600 static const struct file_operations vmwgfx_driver_fops = {
1601 .owner = THIS_MODULE,
1603 .release = drm_release,
1604 .unlocked_ioctl = vmw_unlocked_ioctl,
1605 .mmap = drm_gem_mmap,
1608 #if defined(CONFIG_COMPAT)
1609 .compat_ioctl = vmw_compat_ioctl,
1611 .llseek = noop_llseek,
1612 .fop_flags = FOP_UNSIGNED_OFFSET,
1615 static const struct drm_driver driver = {
1617 DRIVER_MODESET | DRIVER_RENDER | DRIVER_ATOMIC | DRIVER_GEM | DRIVER_CURSOR_HOTSPOT,
1618 .ioctls = vmw_ioctls,
1619 .num_ioctls = ARRAY_SIZE(vmw_ioctls),
1620 .master_set = vmw_master_set,
1621 .master_drop = vmw_master_drop,
1622 .open = vmw_driver_open,
1623 .postclose = vmw_postclose,
1625 .dumb_create = vmw_dumb_create,
1626 .dumb_map_offset = drm_gem_ttm_dumb_map_offset,
1628 .prime_fd_to_handle = vmw_prime_fd_to_handle,
1629 .prime_handle_to_fd = vmw_prime_handle_to_fd,
1630 .gem_prime_import_sg_table = vmw_prime_import_sg_table,
1632 DRM_FBDEV_TTM_DRIVER_OPS,
1634 .fops = &vmwgfx_driver_fops,
1635 .name = VMWGFX_DRIVER_NAME,
1636 .desc = VMWGFX_DRIVER_DESC,
1637 .date = VMWGFX_DRIVER_DATE,
1638 .major = VMWGFX_DRIVER_MAJOR,
1639 .minor = VMWGFX_DRIVER_MINOR,
1640 .patchlevel = VMWGFX_DRIVER_PATCHLEVEL
1643 static struct pci_driver vmw_pci_driver = {
1644 .name = VMWGFX_DRIVER_NAME,
1645 .id_table = vmw_pci_id_list,
1647 .remove = vmw_remove,
1653 static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1655 struct vmw_private *vmw;
1658 ret = aperture_remove_conflicting_pci_devices(pdev, driver.name);
1662 ret = pcim_enable_device(pdev);
1666 vmw = devm_drm_dev_alloc(&pdev->dev, &driver,
1667 struct vmw_private, drm);
1673 pci_set_drvdata(pdev, &vmw->drm);
1675 ret = vmw_driver_load(vmw, ent->device);
1679 ret = drm_dev_register(&vmw->drm, 0);
1683 vmw_fifo_resource_inc(vmw);
1684 vmw_svga_enable(vmw);
1685 drm_client_setup(&vmw->drm, NULL);
1687 vmw_debugfs_gem_init(vmw);
1688 vmw_debugfs_resource_managers_init(vmw);
1692 vmw_driver_unload(&vmw->drm);
1697 drm_module_pci_driver(vmw_pci_driver);
1699 MODULE_AUTHOR("VMware Inc. and others");
1700 MODULE_DESCRIPTION("Standalone drm driver for the VMware SVGA device");
1701 MODULE_LICENSE("GPL and additional rights");
1702 MODULE_VERSION(__stringify(VMWGFX_DRIVER_MAJOR) "."
1703 __stringify(VMWGFX_DRIVER_MINOR) "."
1704 __stringify(VMWGFX_DRIVER_PATCHLEVEL) "."