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 <linux/vmalloc.h>
29 #include "vmwgfx_devcaps.h"
30
31 #include "vmwgfx_drv.h"
32
33
34 struct svga_3d_compat_cap {
35 SVGA3dFifoCapsRecordHeader header;
36 SVGA3dFifoCapPair pairs[SVGA3D_DEVCAP_MAX];
37 };
38
39
vmw_mask_legacy_multisample(unsigned int cap,u32 fmt_value)40 static u32 vmw_mask_legacy_multisample(unsigned int cap, u32 fmt_value)
41 {
42 /*
43 * A version of user-space exists which use MULTISAMPLE_MASKABLESAMPLES
44 * to check the sample count supported by virtual device. Since there
45 * never was support for multisample count for backing MOB return 0.
46 *
47 * MULTISAMPLE_MASKABLESAMPLES devcap is marked as deprecated by virtual
48 * device.
49 */
50 if (cap == SVGA3D_DEVCAP_DEAD5)
51 return 0;
52
53 return fmt_value;
54 }
55
vmw_fill_compat_cap(struct vmw_private * dev_priv,void * bounce,size_t size)56 static int vmw_fill_compat_cap(struct vmw_private *dev_priv, void *bounce,
57 size_t size)
58 {
59 struct svga_3d_compat_cap *compat_cap =
60 (struct svga_3d_compat_cap *) bounce;
61 unsigned int i;
62 size_t pair_offset = offsetof(struct svga_3d_compat_cap, pairs);
63 unsigned int max_size;
64
65 if (size < pair_offset)
66 return -EINVAL;
67
68 max_size = (size - pair_offset) / sizeof(SVGA3dFifoCapPair);
69
70 if (max_size > SVGA3D_DEVCAP_MAX)
71 max_size = SVGA3D_DEVCAP_MAX;
72
73 compat_cap->header.length =
74 (pair_offset + max_size * sizeof(SVGA3dFifoCapPair)) / sizeof(u32);
75 compat_cap->header.type = SVGA3D_FIFO_CAPS_RECORD_DEVCAPS;
76
77 for (i = 0; i < max_size; ++i) {
78 compat_cap->pairs[i][0] = i;
79 compat_cap->pairs[i][1] = vmw_mask_legacy_multisample
80 (i, dev_priv->devcaps[i]);
81 }
82
83 return 0;
84 }
85
vmw_devcaps_create(struct vmw_private * vmw)86 int vmw_devcaps_create(struct vmw_private *vmw)
87 {
88 bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
89 uint32_t i;
90
91 if (gb_objects) {
92 vmw->devcaps = vzalloc(sizeof(uint32_t) * SVGA3D_DEVCAP_MAX);
93 if (!vmw->devcaps)
94 return -ENOMEM;
95 for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
96 vmw_write(vmw, SVGA_REG_DEV_CAP, i);
97 vmw->devcaps[i] = vmw_read(vmw, SVGA_REG_DEV_CAP);
98 }
99 }
100 return 0;
101 }
102
vmw_devcaps_destroy(struct vmw_private * vmw)103 void vmw_devcaps_destroy(struct vmw_private *vmw)
104 {
105 vfree(vmw->devcaps);
106 vmw->devcaps = NULL;
107 }
108
109
vmw_devcaps_size(const struct vmw_private * vmw,bool gb_aware)110 uint32 vmw_devcaps_size(const struct vmw_private *vmw,
111 bool gb_aware)
112 {
113 bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
114 if (gb_objects && gb_aware)
115 return SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
116 else if (gb_objects)
117 return sizeof(struct svga_3d_compat_cap) +
118 sizeof(uint32_t);
119 else if (vmw->fifo_mem != NULL)
120 return (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) *
121 sizeof(uint32_t);
122 else
123 return 0;
124 }
125
vmw_devcaps_copy(struct vmw_private * vmw,bool gb_aware,void * dst,uint32_t dst_size)126 int vmw_devcaps_copy(struct vmw_private *vmw, bool gb_aware,
127 void *dst, uint32_t dst_size)
128 {
129 int ret;
130 bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
131 if (gb_objects && gb_aware) {
132 memcpy(dst, vmw->devcaps, dst_size);
133 } else if (gb_objects) {
134 ret = vmw_fill_compat_cap(vmw, dst, dst_size);
135 if (unlikely(ret != 0))
136 return ret;
137 } else if (vmw->fifo_mem) {
138 u32 *fifo_mem = vmw->fifo_mem;
139 memcpy(dst, &fifo_mem[SVGA_FIFO_3D_CAPS], dst_size);
140 } else
141 return -EINVAL;
142 return 0;
143 }
144