asid.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2019 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include "habanalabs.h"
  7. #include <linux/slab.h>
  8. int hl_asid_init(struct hl_device *hdev)
  9. {
  10. hdev->asid_bitmap = bitmap_zalloc(hdev->asic_prop.max_asid, GFP_KERNEL);
  11. if (!hdev->asid_bitmap)
  12. return -ENOMEM;
  13. mutex_init(&hdev->asid_mutex);
  14. /* ASID 0 is reserved for the kernel driver and device CPU */
  15. set_bit(0, hdev->asid_bitmap);
  16. return 0;
  17. }
  18. void hl_asid_fini(struct hl_device *hdev)
  19. {
  20. mutex_destroy(&hdev->asid_mutex);
  21. bitmap_free(hdev->asid_bitmap);
  22. }
  23. unsigned long hl_asid_alloc(struct hl_device *hdev)
  24. {
  25. unsigned long found;
  26. mutex_lock(&hdev->asid_mutex);
  27. found = find_first_zero_bit(hdev->asid_bitmap,
  28. hdev->asic_prop.max_asid);
  29. if (found == hdev->asic_prop.max_asid)
  30. found = 0;
  31. else
  32. set_bit(found, hdev->asid_bitmap);
  33. mutex_unlock(&hdev->asid_mutex);
  34. return found;
  35. }
  36. void hl_asid_free(struct hl_device *hdev, unsigned long asid)
  37. {
  38. if (asid == HL_KERNEL_ASID_ID || asid >= hdev->asic_prop.max_asid) {
  39. dev_crit(hdev->dev, "Invalid ASID %lu", asid);
  40. return;
  41. }
  42. clear_bit(asid, hdev->asid_bitmap);
  43. }