1*d87f36a0SRajneesh Bhardwaj // SPDX-License-Identifier: GPL-2.0 OR MIT
219f6d2a6SOded Gabbay /*
3*d87f36a0SRajneesh Bhardwaj * Copyright 2014-2022 Advanced Micro Devices, Inc.
419f6d2a6SOded Gabbay *
519f6d2a6SOded Gabbay * Permission is hereby granted, free of charge, to any person obtaining a
619f6d2a6SOded Gabbay * copy of this software and associated documentation files (the "Software"),
719f6d2a6SOded Gabbay * to deal in the Software without restriction, including without limitation
819f6d2a6SOded Gabbay * the rights to use, copy, modify, merge, publish, distribute, sublicense,
919f6d2a6SOded Gabbay * and/or sell copies of the Software, and to permit persons to whom the
1019f6d2a6SOded Gabbay * Software is furnished to do so, subject to the following conditions:
1119f6d2a6SOded Gabbay *
1219f6d2a6SOded Gabbay * The above copyright notice and this permission notice shall be included in
1319f6d2a6SOded Gabbay * all copies or substantial portions of the Software.
1419f6d2a6SOded Gabbay *
1519f6d2a6SOded Gabbay * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1619f6d2a6SOded Gabbay * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1719f6d2a6SOded Gabbay * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1819f6d2a6SOded Gabbay * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1919f6d2a6SOded Gabbay * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2019f6d2a6SOded Gabbay * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2119f6d2a6SOded Gabbay * OTHER DEALINGS IN THE SOFTWARE.
2219f6d2a6SOded Gabbay */
2319f6d2a6SOded Gabbay
2419f6d2a6SOded Gabbay #include <linux/types.h>
2519f6d2a6SOded Gabbay #include "kfd_priv.h"
265b87245fSAmber Lin #include "amdgpu_ids.h"
2719f6d2a6SOded Gabbay
28d2791c45SFelix Kuehling static unsigned int pasid_bits = 16;
29c1213911SFelix Kuehling static bool pasids_allocated; /* = false */
3019f6d2a6SOded Gabbay
kfd_set_pasid_limit(unsigned int new_limit)3119f6d2a6SOded Gabbay bool kfd_set_pasid_limit(unsigned int new_limit)
3219f6d2a6SOded Gabbay {
33d2791c45SFelix Kuehling if (new_limit < 2)
34d2791c45SFelix Kuehling return false;
3519f6d2a6SOded Gabbay
36d2791c45SFelix Kuehling if (new_limit < (1U << pasid_bits)) {
37c1213911SFelix Kuehling if (pasids_allocated)
38d2791c45SFelix Kuehling /* We've already allocated user PASIDs, too late to
39d2791c45SFelix Kuehling * change the limit
40d2791c45SFelix Kuehling */
41d2791c45SFelix Kuehling return false;
4219f6d2a6SOded Gabbay
43d2791c45SFelix Kuehling while (new_limit < (1U << pasid_bits))
44d2791c45SFelix Kuehling pasid_bits--;
4519f6d2a6SOded Gabbay }
4619f6d2a6SOded Gabbay
4719f6d2a6SOded Gabbay return true;
4819f6d2a6SOded Gabbay }
4919f6d2a6SOded Gabbay
kfd_get_pasid_limit(void)50d2791c45SFelix Kuehling unsigned int kfd_get_pasid_limit(void)
5119f6d2a6SOded Gabbay {
52d2791c45SFelix Kuehling return 1U << pasid_bits;
5319f6d2a6SOded Gabbay }
5419f6d2a6SOded Gabbay
kfd_pasid_alloc(void)55c7b6bac9SFenghua Yu u32 kfd_pasid_alloc(void)
5619f6d2a6SOded Gabbay {
57c1213911SFelix Kuehling int r = amdgpu_pasid_alloc(pasid_bits);
5819f6d2a6SOded Gabbay
59c1213911SFelix Kuehling if (r > 0) {
60c1213911SFelix Kuehling pasids_allocated = true;
61c1213911SFelix Kuehling return r;
62d2791c45SFelix Kuehling }
6319f6d2a6SOded Gabbay
64c1213911SFelix Kuehling return 0;
6519f6d2a6SOded Gabbay }
6619f6d2a6SOded Gabbay
kfd_pasid_free(u32 pasid)67c7b6bac9SFenghua Yu void kfd_pasid_free(u32 pasid)
6819f6d2a6SOded Gabbay {
695b87245fSAmber Lin amdgpu_pasid_free(pasid);
7019f6d2a6SOded Gabbay }
71