Skip to content

Commit d92223e

Browse files
committed
drm/vmwgfx: Simplify devcaps code
Make devcaps code self-contained so that it's easier to cache and operate on them. As the number of devcaps got bigger the code dealing with them got more and more tricky. Lets create a central place to deal with all the complexity. This lets us remove the lock we used to require to deal with register write races because we only read the devcaps at initialization. Signed-off-by: Zack Rusin <zackr@vmware.com> Reviewed-by: Roland Scheidegger <sroland@vmware.com> Reviewed-by: Martin Krastev <krastevm@vmware.com> Link: https://patchwork.freedesktop.org/patch/msgid/20210609172307.131929-2-zackr@vmware.com
1 parent ab6f24b commit d92223e

7 files changed

Lines changed: 221 additions & 116 deletions

File tree

‎drivers/gpu/drm/vmwgfx/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
99
vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
1010
vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
1111
vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
12-
ttm_object.o ttm_memory.o
12+
vmwgfx_devcaps.o ttm_object.o ttm_memory.o
1313

1414
vmwgfx-$(CONFIG_DRM_FBDEV_EMULATION) += vmwgfx_fb.o
1515
vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o

‎drivers/gpu/drm/vmwgfx/vmwgfx_cmd.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <drm/ttm/ttm_placement.h>
3131

3232
#include "vmwgfx_drv.h"
33+
#include "vmwgfx_devcaps.h"
3334

3435
bool vmw_supports_3d(struct vmw_private *dev_priv)
3536
{
@@ -45,10 +46,7 @@ bool vmw_supports_3d(struct vmw_private *dev_priv)
4546
if (!dev_priv->has_mob)
4647
return false;
4748

48-
spin_lock(&dev_priv->cap_lock);
49-
vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_3D);
50-
result = vmw_read(dev_priv, SVGA_REG_DEV_CAP);
51-
spin_unlock(&dev_priv->cap_lock);
49+
result = vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_3D);
5250

5351
return (result != 0);
5452
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2+
/**************************************************************************
3+
*
4+
* Copyright 2021 VMware, Inc., Palo Alto, CA., USA
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a
7+
* copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sub license, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice (including the
15+
* next paragraph) shall be included in all copies or substantial portions
16+
* of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21+
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24+
* USE OR OTHER DEALINGS IN THE SOFTWARE.
25+
*
26+
**************************************************************************/
27+
28+
#include "vmwgfx_devcaps.h"
29+
30+
#include "vmwgfx_drv.h"
31+
32+
33+
struct svga_3d_compat_cap {
34+
SVGA3dCapsRecordHeader header;
35+
SVGA3dCapPair pairs[SVGA3D_DEVCAP_MAX];
36+
};
37+
38+
39+
static u32 vmw_mask_legacy_multisample(unsigned int cap, u32 fmt_value)
40+
{
41+
/*
42+
* A version of user-space exists which use MULTISAMPLE_MASKABLESAMPLES
43+
* to check the sample count supported by virtual device. Since there
44+
* never was support for multisample count for backing MOB return 0.
45+
*
46+
* MULTISAMPLE_MASKABLESAMPLES devcap is marked as deprecated by virtual
47+
* device.
48+
*/
49+
if (cap == SVGA3D_DEVCAP_DEAD5)
50+
return 0;
51+
52+
return fmt_value;
53+
}
54+
55+
static int vmw_fill_compat_cap(struct vmw_private *dev_priv, void *bounce,
56+
size_t size)
57+
{
58+
struct svga_3d_compat_cap *compat_cap =
59+
(struct svga_3d_compat_cap *) bounce;
60+
unsigned int i;
61+
size_t pair_offset = offsetof(struct svga_3d_compat_cap, pairs);
62+
unsigned int max_size;
63+
64+
if (size < pair_offset)
65+
return -EINVAL;
66+
67+
max_size = (size - pair_offset) / sizeof(SVGA3dCapPair);
68+
69+
if (max_size > SVGA3D_DEVCAP_MAX)
70+
max_size = SVGA3D_DEVCAP_MAX;
71+
72+
compat_cap->header.length =
73+
(pair_offset + max_size * sizeof(SVGA3dCapPair)) / sizeof(u32);
74+
compat_cap->header.type = SVGA3DCAPS_RECORD_DEVCAPS;
75+
76+
for (i = 0; i < max_size; ++i) {
77+
compat_cap->pairs[i][0] = i;
78+
compat_cap->pairs[i][1] = vmw_mask_legacy_multisample
79+
(i, dev_priv->devcaps[i]);
80+
}
81+
82+
return 0;
83+
}
84+
85+
int vmw_devcaps_create(struct vmw_private *vmw)
86+
{
87+
bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
88+
uint32_t i;
89+
90+
if (gb_objects) {
91+
vmw->devcaps = vzalloc(sizeof(uint32_t) * SVGA3D_DEVCAP_MAX);
92+
if (!vmw->devcaps)
93+
return -ENOMEM;
94+
for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
95+
vmw_write(vmw, SVGA_REG_DEV_CAP, i);
96+
vmw->devcaps[i] = vmw_read(vmw, SVGA_REG_DEV_CAP);
97+
}
98+
}
99+
return 0;
100+
}
101+
102+
void vmw_devcaps_destroy(struct vmw_private *vmw)
103+
{
104+
vfree(vmw->devcaps);
105+
vmw->devcaps = NULL;
106+
}
107+
108+
109+
uint32 vmw_devcaps_size(const struct vmw_private *vmw,
110+
bool gb_aware)
111+
{
112+
bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
113+
if (gb_objects && gb_aware)
114+
return SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
115+
else if (gb_objects)
116+
return sizeof(struct svga_3d_compat_cap) +
117+
sizeof(uint32_t);
118+
else if (vmw->fifo_mem != NULL)
119+
return (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) *
120+
sizeof(uint32_t);
121+
else
122+
return 0;
123+
}
124+
125+
int vmw_devcaps_copy(struct vmw_private *vmw, bool gb_aware,
126+
void *dst, uint32_t dst_size)
127+
{
128+
int ret;
129+
bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
130+
if (gb_objects && gb_aware) {
131+
memcpy(dst, vmw->devcaps, dst_size);
132+
} else if (gb_objects) {
133+
ret = vmw_fill_compat_cap(vmw, dst, dst_size);
134+
if (unlikely(ret != 0))
135+
return ret;
136+
} else if (vmw->fifo_mem) {
137+
u32 *fifo_mem = vmw->fifo_mem;
138+
memcpy(dst, &fifo_mem[SVGA_FIFO_3D_CAPS], dst_size);
139+
} else
140+
return -EINVAL;
141+
return 0;
142+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2+
/**************************************************************************
3+
*
4+
* Copyright 2021 VMware, Inc., Palo Alto, CA., USA
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a
7+
* copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sub license, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice (including the
15+
* next paragraph) shall be included in all copies or substantial portions
16+
* of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21+
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24+
* USE OR OTHER DEALINGS IN THE SOFTWARE.
25+
*
26+
**************************************************************************/
27+
28+
#ifndef _VMWGFX_DEVCAPS_H_
29+
#define _VMWGFX_DEVCAPS_H_
30+
31+
#include "vmwgfx_drv.h"
32+
33+
#include "device_include/svga3d_caps.h"
34+
35+
int vmw_devcaps_create(struct vmw_private *vmw);
36+
void vmw_devcaps_destroy(struct vmw_private *vmw);
37+
uint32_t vmw_devcaps_size(const struct vmw_private *vmw, bool gb_aware);
38+
int vmw_devcaps_copy(struct vmw_private *vmw, bool gb_aware,
39+
void *dst, uint32_t dst_size);
40+
41+
static inline uint32_t vmw_devcap_get(struct vmw_private *vmw,
42+
uint32_t devcap)
43+
{
44+
bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
45+
if (gb_objects)
46+
return vmw->devcaps[devcap];
47+
return 0;
48+
}
49+
50+
#endif

‎drivers/gpu/drm/vmwgfx/vmwgfx_drv.c‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
#include "ttm_object.h"
4343
#include "vmwgfx_binding.h"
44+
#include "vmwgfx_devcaps.h"
4445
#include "vmwgfx_drv.h"
4546

4647
#define VMWGFX_DRIVER_DESC "Linux drm driver for VMware graphics devices"
@@ -792,7 +793,6 @@ static int vmw_driver_load(struct vmw_private *dev_priv, u32 pci_id)
792793
spin_lock_init(&dev_priv->resource_lock);
793794
spin_lock_init(&dev_priv->hw_lock);
794795
spin_lock_init(&dev_priv->waiter_lock);
795-
spin_lock_init(&dev_priv->cap_lock);
796796
spin_lock_init(&dev_priv->cursor_lock);
797797

798798
ret = vmw_setup_pci_resources(dev_priv, pci_id);
@@ -982,6 +982,12 @@ static int vmw_driver_load(struct vmw_private *dev_priv, u32 pci_id)
982982
goto out_no_vram;
983983
}
984984

985+
ret = vmw_devcaps_create(dev_priv);
986+
if (unlikely(ret != 0)) {
987+
DRM_ERROR("Failed initializing device caps.\n");
988+
goto out_no_vram;
989+
}
990+
985991
/*
986992
* "Guest Memory Regions" is an aperture like feature with
987993
* one slot per bo. There is an upper limit of the number of
@@ -1008,27 +1014,20 @@ static int vmw_driver_load(struct vmw_private *dev_priv, u32 pci_id)
10081014
}
10091015

10101016
if (dev_priv->has_mob && (dev_priv->capabilities & SVGA_CAP_DX)) {
1011-
spin_lock(&dev_priv->cap_lock);
1012-
vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_DXCONTEXT);
1013-
if (vmw_read(dev_priv, SVGA_REG_DEV_CAP))
1017+
if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_DXCONTEXT))
10141018
dev_priv->sm_type = VMW_SM_4;
1015-
spin_unlock(&dev_priv->cap_lock);
10161019
}
10171020

10181021
vmw_validation_mem_init_ttm(dev_priv, VMWGFX_VALIDATION_MEM_GRAN);
10191022

10201023
/* SVGA_CAP2_DX2 (DefineGBSurface_v3) is needed for SM4_1 support */
10211024
if (has_sm4_context(dev_priv) &&
10221025
(dev_priv->capabilities2 & SVGA_CAP2_DX2)) {
1023-
vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_SM41);
1024-
1025-
if (vmw_read(dev_priv, SVGA_REG_DEV_CAP))
1026+
if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_SM41))
10261027
dev_priv->sm_type = VMW_SM_4_1;
1027-
10281028
if (has_sm4_1_context(dev_priv) &&
1029-
(dev_priv->capabilities2 & SVGA_CAP2_DX3)) {
1030-
vmw_write(dev_priv, SVGA_REG_DEV_CAP, SVGA3D_DEVCAP_SM5);
1031-
if (vmw_read(dev_priv, SVGA_REG_DEV_CAP))
1029+
(dev_priv->capabilities2 & SVGA_CAP2_DX3)) {
1030+
if (vmw_devcap_get(dev_priv, SVGA3D_DEVCAP_SM5))
10321031
dev_priv->sm_type = VMW_SM_5;
10331032
}
10341033
}
@@ -1073,6 +1072,7 @@ static int vmw_driver_load(struct vmw_private *dev_priv, u32 pci_id)
10731072
vmw_gmrid_man_fini(dev_priv, VMW_PL_MOB);
10741073
if (dev_priv->has_gmr)
10751074
vmw_gmrid_man_fini(dev_priv, VMW_PL_GMR);
1075+
vmw_devcaps_destroy(dev_priv);
10761076
vmw_vram_manager_fini(dev_priv);
10771077
out_no_vram:
10781078
ttm_device_fini(&dev_priv->bdev);
@@ -1121,6 +1121,7 @@ static void vmw_driver_unload(struct drm_device *dev)
11211121
vmw_release_device_early(dev_priv);
11221122
if (dev_priv->has_mob)
11231123
vmw_gmrid_man_fini(dev_priv, VMW_PL_MOB);
1124+
vmw_devcaps_destroy(dev_priv);
11241125
vmw_vram_manager_fini(dev_priv);
11251126
ttm_device_fini(&dev_priv->bdev);
11261127
drm_vma_offset_manager_destroy(&dev_priv->vma_manager);

‎drivers/gpu/drm/vmwgfx/vmwgfx_drv.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,6 @@ struct vmw_private {
513513
bool has_gmr;
514514
bool has_mob;
515515
spinlock_t hw_lock;
516-
spinlock_t cap_lock;
517516
bool assume_16bpp;
518517

519518
enum vmw_sm_type sm_type;
@@ -629,6 +628,8 @@ struct vmw_private {
629628

630629
/* Validation memory reservation */
631630
struct vmw_validation_mem vvm;
631+
632+
uint32 *devcaps;
632633
};
633634

634635
static inline struct vmw_surface *vmw_res_to_srf(struct vmw_resource *res)

0 commit comments

Comments
 (0)