xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdkfd/kfd_pasid.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
1 /*	$NetBSD: kfd_pasid.c,v 1.2 2018/08/27 04:58:20 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2014 Advanced Micro Devices, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: kfd_pasid.c,v 1.2 2018/08/27 04:58:20 riastradh Exp $");
27 
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include "kfd_priv.h"
31 
32 static unsigned long *pasid_bitmap;
33 static unsigned int pasid_limit;
34 static DEFINE_MUTEX(pasid_mutex);
35 
36 int kfd_pasid_init(void)
37 {
38 	pasid_limit = KFD_MAX_NUM_OF_PROCESSES;
39 
40 	pasid_bitmap = kcalloc(BITS_TO_LONGS(pasid_limit), sizeof(long), GFP_KERNEL);
41 	if (!pasid_bitmap)
42 		return -ENOMEM;
43 
44 	set_bit(0, pasid_bitmap); /* PASID 0 is reserved. */
45 
46 	return 0;
47 }
48 
49 void kfd_pasid_exit(void)
50 {
51 	kfree(pasid_bitmap);
52 }
53 
54 bool kfd_set_pasid_limit(unsigned int new_limit)
55 {
56 	if (new_limit < pasid_limit) {
57 		bool ok;
58 
59 		mutex_lock(&pasid_mutex);
60 
61 		/* ensure that no pasids >= new_limit are in-use */
62 		ok = (find_next_bit(pasid_bitmap, pasid_limit, new_limit) ==
63 								pasid_limit);
64 		if (ok)
65 			pasid_limit = new_limit;
66 
67 		mutex_unlock(&pasid_mutex);
68 
69 		return ok;
70 	}
71 
72 	return true;
73 }
74 
75 inline unsigned int kfd_get_pasid_limit(void)
76 {
77 	return pasid_limit;
78 }
79 
80 unsigned int kfd_pasid_alloc(void)
81 {
82 	unsigned int found;
83 
84 	mutex_lock(&pasid_mutex);
85 
86 	found = find_first_zero_bit(pasid_bitmap, pasid_limit);
87 	if (found == pasid_limit)
88 		found = 0;
89 	else
90 		set_bit(found, pasid_bitmap);
91 
92 	mutex_unlock(&pasid_mutex);
93 
94 	return found;
95 }
96 
97 void kfd_pasid_free(unsigned int pasid)
98 {
99 	BUG_ON(pasid == 0 || pasid >= pasid_limit);
100 	clear_bit(pasid, pasid_bitmap);
101 }
102