1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/slab.h>
12 int hl_asid_init(struct hl_device *hdev)
14 hdev->asid_bitmap = kcalloc(BITS_TO_LONGS(hdev->asic_prop.max_asid),
15 sizeof(*hdev->asid_bitmap), GFP_KERNEL);
16 if (!hdev->asid_bitmap)
19 mutex_init(&hdev->asid_mutex);
21 /* ASID 0 is reserved for KMD */
22 set_bit(0, hdev->asid_bitmap);
27 void hl_asid_fini(struct hl_device *hdev)
29 mutex_destroy(&hdev->asid_mutex);
30 kfree(hdev->asid_bitmap);
33 unsigned long hl_asid_alloc(struct hl_device *hdev)
37 mutex_lock(&hdev->asid_mutex);
39 found = find_first_zero_bit(hdev->asid_bitmap,
40 hdev->asic_prop.max_asid);
41 if (found == hdev->asic_prop.max_asid)
44 set_bit(found, hdev->asid_bitmap);
46 mutex_unlock(&hdev->asid_mutex);
51 void hl_asid_free(struct hl_device *hdev, unsigned long asid)
53 if (WARN((asid == 0 || asid >= hdev->asic_prop.max_asid),
54 "Invalid ASID %lu", asid))
56 clear_bit(asid, hdev->asid_bitmap);