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 = bitmap_zalloc(hdev->asic_prop.max_asid, GFP_KERNEL);
15 if (!hdev->asid_bitmap)
18 mutex_init(&hdev->asid_mutex);
20 /* ASID 0 is reserved for the kernel driver and device CPU */
21 set_bit(0, hdev->asid_bitmap);
26 void hl_asid_fini(struct hl_device *hdev)
28 mutex_destroy(&hdev->asid_mutex);
29 bitmap_free(hdev->asid_bitmap);
32 unsigned long hl_asid_alloc(struct hl_device *hdev)
36 mutex_lock(&hdev->asid_mutex);
38 found = find_first_zero_bit(hdev->asid_bitmap,
39 hdev->asic_prop.max_asid);
40 if (found == hdev->asic_prop.max_asid)
43 set_bit(found, hdev->asid_bitmap);
45 mutex_unlock(&hdev->asid_mutex);
50 void hl_asid_free(struct hl_device *hdev, unsigned long asid)
52 if (asid == HL_KERNEL_ASID_ID || asid >= hdev->asic_prop.max_asid) {
53 dev_crit(hdev->dev, "Invalid ASID %lu", asid);
57 clear_bit(asid, hdev->asid_bitmap);