162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR MIT */
262306a36Sopenharmony_ci/**************************************************************************
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright 2021 VMware, Inc., Palo Alto, CA., USA
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
762306a36Sopenharmony_ci * copy of this software and associated documentation files (the
862306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
962306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
1062306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
1162306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1262306a36Sopenharmony_ci * the following conditions:
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
1562306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
1662306a36Sopenharmony_ci * of the Software.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1962306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2062306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
2162306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2262306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2362306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2462306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci **************************************************************************/
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include "vmwgfx_devcaps.h"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#include "vmwgfx_drv.h"
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistruct svga_3d_compat_cap {
3462306a36Sopenharmony_ci	SVGA3dFifoCapsRecordHeader header;
3562306a36Sopenharmony_ci	SVGA3dFifoCapPair pairs[SVGA3D_DEVCAP_MAX];
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistatic u32 vmw_mask_legacy_multisample(unsigned int cap, u32 fmt_value)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	/*
4262306a36Sopenharmony_ci	 * A version of user-space exists which use MULTISAMPLE_MASKABLESAMPLES
4362306a36Sopenharmony_ci	 * to check the sample count supported by virtual device. Since there
4462306a36Sopenharmony_ci	 * never was support for multisample count for backing MOB return 0.
4562306a36Sopenharmony_ci	 *
4662306a36Sopenharmony_ci	 * MULTISAMPLE_MASKABLESAMPLES devcap is marked as deprecated by virtual
4762306a36Sopenharmony_ci	 * device.
4862306a36Sopenharmony_ci	 */
4962306a36Sopenharmony_ci	if (cap == SVGA3D_DEVCAP_DEAD5)
5062306a36Sopenharmony_ci		return 0;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	return fmt_value;
5362306a36Sopenharmony_ci}
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic int vmw_fill_compat_cap(struct vmw_private *dev_priv, void *bounce,
5662306a36Sopenharmony_ci			       size_t size)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	struct svga_3d_compat_cap *compat_cap =
5962306a36Sopenharmony_ci		(struct svga_3d_compat_cap *) bounce;
6062306a36Sopenharmony_ci	unsigned int i;
6162306a36Sopenharmony_ci	size_t pair_offset = offsetof(struct svga_3d_compat_cap, pairs);
6262306a36Sopenharmony_ci	unsigned int max_size;
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	if (size < pair_offset)
6562306a36Sopenharmony_ci		return -EINVAL;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	max_size = (size - pair_offset) / sizeof(SVGA3dFifoCapPair);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	if (max_size > SVGA3D_DEVCAP_MAX)
7062306a36Sopenharmony_ci		max_size = SVGA3D_DEVCAP_MAX;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	compat_cap->header.length =
7362306a36Sopenharmony_ci		(pair_offset + max_size * sizeof(SVGA3dFifoCapPair)) / sizeof(u32);
7462306a36Sopenharmony_ci	compat_cap->header.type = SVGA3D_FIFO_CAPS_RECORD_DEVCAPS;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	for (i = 0; i < max_size; ++i) {
7762306a36Sopenharmony_ci		compat_cap->pairs[i][0] = i;
7862306a36Sopenharmony_ci		compat_cap->pairs[i][1] = vmw_mask_legacy_multisample
7962306a36Sopenharmony_ci			(i, dev_priv->devcaps[i]);
8062306a36Sopenharmony_ci	}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	return 0;
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ciint vmw_devcaps_create(struct vmw_private *vmw)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
8862306a36Sopenharmony_ci	uint32_t i;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	if (gb_objects) {
9162306a36Sopenharmony_ci		vmw->devcaps = vzalloc(sizeof(uint32_t) * SVGA3D_DEVCAP_MAX);
9262306a36Sopenharmony_ci		if (!vmw->devcaps)
9362306a36Sopenharmony_ci			return -ENOMEM;
9462306a36Sopenharmony_ci		for (i = 0; i < SVGA3D_DEVCAP_MAX; ++i) {
9562306a36Sopenharmony_ci			vmw_write(vmw, SVGA_REG_DEV_CAP, i);
9662306a36Sopenharmony_ci			vmw->devcaps[i] = vmw_read(vmw, SVGA_REG_DEV_CAP);
9762306a36Sopenharmony_ci		}
9862306a36Sopenharmony_ci	}
9962306a36Sopenharmony_ci	return 0;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_civoid vmw_devcaps_destroy(struct vmw_private *vmw)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	vfree(vmw->devcaps);
10562306a36Sopenharmony_ci	vmw->devcaps = NULL;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ciuint32 vmw_devcaps_size(const struct vmw_private *vmw,
11062306a36Sopenharmony_ci			bool gb_aware)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
11362306a36Sopenharmony_ci	if (gb_objects && gb_aware)
11462306a36Sopenharmony_ci		return SVGA3D_DEVCAP_MAX * sizeof(uint32_t);
11562306a36Sopenharmony_ci	else if (gb_objects)
11662306a36Sopenharmony_ci		return  sizeof(struct svga_3d_compat_cap) +
11762306a36Sopenharmony_ci				sizeof(uint32_t);
11862306a36Sopenharmony_ci	else if (vmw->fifo_mem != NULL)
11962306a36Sopenharmony_ci		return (SVGA_FIFO_3D_CAPS_LAST - SVGA_FIFO_3D_CAPS + 1) *
12062306a36Sopenharmony_ci				sizeof(uint32_t);
12162306a36Sopenharmony_ci	else
12262306a36Sopenharmony_ci		return 0;
12362306a36Sopenharmony_ci}
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ciint vmw_devcaps_copy(struct vmw_private *vmw, bool gb_aware,
12662306a36Sopenharmony_ci		     void *dst, uint32_t dst_size)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	int ret;
12962306a36Sopenharmony_ci	bool gb_objects = !!(vmw->capabilities & SVGA_CAP_GBOBJECTS);
13062306a36Sopenharmony_ci	if (gb_objects && gb_aware) {
13162306a36Sopenharmony_ci		memcpy(dst, vmw->devcaps, dst_size);
13262306a36Sopenharmony_ci	} else if (gb_objects) {
13362306a36Sopenharmony_ci		ret = vmw_fill_compat_cap(vmw, dst, dst_size);
13462306a36Sopenharmony_ci		if (unlikely(ret != 0))
13562306a36Sopenharmony_ci			return ret;
13662306a36Sopenharmony_ci	} else if (vmw->fifo_mem) {
13762306a36Sopenharmony_ci		u32 *fifo_mem = vmw->fifo_mem;
13862306a36Sopenharmony_ci		memcpy(dst, &fifo_mem[SVGA_FIFO_3D_CAPS], dst_size);
13962306a36Sopenharmony_ci	} else
14062306a36Sopenharmony_ci		return -EINVAL;
14162306a36Sopenharmony_ci	return 0;
14262306a36Sopenharmony_ci}
143