i915_memcpy.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/kernel.h>
  25. #include <asm/fpu/api.h>
  26. #include "i915_drv.h"
  27. static DEFINE_STATIC_KEY_FALSE(has_movntdqa);
  28. #ifdef CONFIG_AS_MOVNTDQA
  29. static void __memcpy_ntdqa(void *dst, const void *src, unsigned long len)
  30. {
  31. kernel_fpu_begin();
  32. len >>= 4;
  33. while (len >= 4) {
  34. asm("movntdqa (%0), %%xmm0\n"
  35. "movntdqa 16(%0), %%xmm1\n"
  36. "movntdqa 32(%0), %%xmm2\n"
  37. "movntdqa 48(%0), %%xmm3\n"
  38. "movaps %%xmm0, (%1)\n"
  39. "movaps %%xmm1, 16(%1)\n"
  40. "movaps %%xmm2, 32(%1)\n"
  41. "movaps %%xmm3, 48(%1)\n"
  42. :: "r" (src), "r" (dst) : "memory");
  43. src += 64;
  44. dst += 64;
  45. len -= 4;
  46. }
  47. while (len--) {
  48. asm("movntdqa (%0), %%xmm0\n"
  49. "movaps %%xmm0, (%1)\n"
  50. :: "r" (src), "r" (dst) : "memory");
  51. src += 16;
  52. dst += 16;
  53. }
  54. kernel_fpu_end();
  55. }
  56. #endif
  57. /**
  58. * i915_memcpy_from_wc: perform an accelerated *aligned* read from WC
  59. * @dst: destination pointer
  60. * @src: source pointer
  61. * @len: how many bytes to copy
  62. *
  63. * i915_memcpy_from_wc copies @len bytes from @src to @dst using
  64. * non-temporal instructions where available. Note that all arguments
  65. * (@src, @dst) must be aligned to 16 bytes and @len must be a multiple
  66. * of 16.
  67. *
  68. * To test whether accelerated reads from WC are supported, use
  69. * i915_memcpy_from_wc(NULL, NULL, 0);
  70. *
  71. * Returns true if the copy was successful, false if the preconditions
  72. * are not met.
  73. */
  74. bool i915_memcpy_from_wc(void *dst, const void *src, unsigned long len)
  75. {
  76. if (unlikely(((unsigned long)dst | (unsigned long)src | len) & 15))
  77. return false;
  78. #ifdef CONFIG_AS_MOVNTDQA
  79. if (static_branch_likely(&has_movntdqa)) {
  80. if (likely(len))
  81. __memcpy_ntdqa(dst, src, len);
  82. return true;
  83. }
  84. #endif
  85. return false;
  86. }
  87. void i915_memcpy_init_early(struct drm_i915_private *dev_priv)
  88. {
  89. if (static_cpu_has(X86_FEATURE_XMM4_1))
  90. static_branch_enable(&has_movntdqa);
  91. }