kfd_pasid.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  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 shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <linux/types.h>
  23. #include "kfd_priv.h"
  24. static unsigned int pasid_bits = 16;
  25. static const struct kfd2kgd_calls *kfd2kgd;
  26. bool kfd_set_pasid_limit(unsigned int new_limit)
  27. {
  28. if (new_limit < 2)
  29. return false;
  30. if (new_limit < (1U << pasid_bits)) {
  31. if (kfd2kgd)
  32. /* We've already allocated user PASIDs, too late to
  33. * change the limit
  34. */
  35. return false;
  36. while (new_limit < (1U << pasid_bits))
  37. pasid_bits--;
  38. }
  39. return true;
  40. }
  41. unsigned int kfd_get_pasid_limit(void)
  42. {
  43. return 1U << pasid_bits;
  44. }
  45. unsigned int kfd_pasid_alloc(void)
  46. {
  47. int r;
  48. /* Find the first best KFD device for calling KGD */
  49. if (!kfd2kgd) {
  50. struct kfd_dev *dev = NULL;
  51. unsigned int i = 0;
  52. while ((kfd_topology_enum_kfd_devices(i, &dev)) == 0) {
  53. if (dev && dev->kfd2kgd) {
  54. kfd2kgd = dev->kfd2kgd;
  55. break;
  56. }
  57. i++;
  58. }
  59. if (!kfd2kgd)
  60. return false;
  61. }
  62. r = kfd2kgd->alloc_pasid(pasid_bits);
  63. return r > 0 ? r : 0;
  64. }
  65. void kfd_pasid_free(unsigned int pasid)
  66. {
  67. if (kfd2kgd)
  68. kfd2kgd->free_pasid(pasid);
  69. }