vmwgfx_devcaps.c 4.2 KB

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