vmwgfx_gmr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  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 "vmwgfx_drv.h"
  28. #include <drm/drmP.h>
  29. #include <drm/ttm/ttm_bo_driver.h>
  30. #define VMW_PPN_SIZE (sizeof(unsigned long))
  31. /* A future safe maximum remap size. */
  32. #define VMW_PPN_PER_REMAP ((31 * 1024) / VMW_PPN_SIZE)
  33. #define DMA_ADDR_INVALID ((dma_addr_t) 0)
  34. #define DMA_PAGE_INVALID 0UL
  35. static int vmw_gmr2_bind(struct vmw_private *dev_priv,
  36. struct vmw_piter *iter,
  37. unsigned long num_pages,
  38. int gmr_id)
  39. {
  40. SVGAFifoCmdDefineGMR2 define_cmd;
  41. SVGAFifoCmdRemapGMR2 remap_cmd;
  42. uint32_t *cmd;
  43. uint32_t *cmd_orig;
  44. uint32_t define_size = sizeof(define_cmd) + sizeof(*cmd);
  45. uint32_t remap_num = num_pages / VMW_PPN_PER_REMAP + ((num_pages % VMW_PPN_PER_REMAP) > 0);
  46. uint32_t remap_size = VMW_PPN_SIZE * num_pages + (sizeof(remap_cmd) + sizeof(*cmd)) * remap_num;
  47. uint32_t remap_pos = 0;
  48. uint32_t cmd_size = define_size + remap_size;
  49. uint32_t i;
  50. cmd_orig = cmd = vmw_fifo_reserve(dev_priv, cmd_size);
  51. if (unlikely(cmd == NULL))
  52. return -ENOMEM;
  53. define_cmd.gmrId = gmr_id;
  54. define_cmd.numPages = num_pages;
  55. *cmd++ = SVGA_CMD_DEFINE_GMR2;
  56. memcpy(cmd, &define_cmd, sizeof(define_cmd));
  57. cmd += sizeof(define_cmd) / sizeof(*cmd);
  58. /*
  59. * Need to split the command if there are too many
  60. * pages that goes into the gmr.
  61. */
  62. remap_cmd.gmrId = gmr_id;
  63. remap_cmd.flags = (VMW_PPN_SIZE > sizeof(*cmd)) ?
  64. SVGA_REMAP_GMR2_PPN64 : SVGA_REMAP_GMR2_PPN32;
  65. while (num_pages > 0) {
  66. unsigned long nr = min(num_pages, (unsigned long)VMW_PPN_PER_REMAP);
  67. remap_cmd.offsetPages = remap_pos;
  68. remap_cmd.numPages = nr;
  69. *cmd++ = SVGA_CMD_REMAP_GMR2;
  70. memcpy(cmd, &remap_cmd, sizeof(remap_cmd));
  71. cmd += sizeof(remap_cmd) / sizeof(*cmd);
  72. for (i = 0; i < nr; ++i) {
  73. if (VMW_PPN_SIZE <= 4)
  74. *cmd = vmw_piter_dma_addr(iter) >> PAGE_SHIFT;
  75. else
  76. *((uint64_t *)cmd) = vmw_piter_dma_addr(iter) >>
  77. PAGE_SHIFT;
  78. cmd += VMW_PPN_SIZE / sizeof(*cmd);
  79. vmw_piter_next(iter);
  80. }
  81. num_pages -= nr;
  82. remap_pos += nr;
  83. }
  84. BUG_ON(cmd != cmd_orig + cmd_size / sizeof(*cmd));
  85. vmw_fifo_commit(dev_priv, cmd_size);
  86. return 0;
  87. }
  88. static void vmw_gmr2_unbind(struct vmw_private *dev_priv,
  89. int gmr_id)
  90. {
  91. SVGAFifoCmdDefineGMR2 define_cmd;
  92. uint32_t define_size = sizeof(define_cmd) + 4;
  93. uint32_t *cmd;
  94. cmd = vmw_fifo_reserve(dev_priv, define_size);
  95. if (unlikely(cmd == NULL)) {
  96. DRM_ERROR("GMR2 unbind failed.\n");
  97. return;
  98. }
  99. define_cmd.gmrId = gmr_id;
  100. define_cmd.numPages = 0;
  101. *cmd++ = SVGA_CMD_DEFINE_GMR2;
  102. memcpy(cmd, &define_cmd, sizeof(define_cmd));
  103. vmw_fifo_commit(dev_priv, define_size);
  104. }
  105. int vmw_gmr_bind(struct vmw_private *dev_priv,
  106. const struct vmw_sg_table *vsgt,
  107. unsigned long num_pages,
  108. int gmr_id)
  109. {
  110. struct vmw_piter data_iter;
  111. vmw_piter_start(&data_iter, vsgt, 0);
  112. if (unlikely(!vmw_piter_next(&data_iter)))
  113. return 0;
  114. if (unlikely(!(dev_priv->capabilities & SVGA_CAP_GMR2)))
  115. return -EINVAL;
  116. return vmw_gmr2_bind(dev_priv, &data_iter, num_pages, gmr_id);
  117. }
  118. void vmw_gmr_unbind(struct vmw_private *dev_priv, int gmr_id)
  119. {
  120. if (likely(dev_priv->capabilities & SVGA_CAP_GMR2))
  121. vmw_gmr2_unbind(dev_priv, gmr_id);
  122. }