18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2014 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/types.h>
248c2ecf20Sopenharmony_ci#include "kfd_priv.h"
258c2ecf20Sopenharmony_ci#include "amdgpu_ids.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic unsigned int pasid_bits = 16;
288c2ecf20Sopenharmony_cistatic bool pasids_allocated; /* = false */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cibool kfd_set_pasid_limit(unsigned int new_limit)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	if (new_limit < 2)
338c2ecf20Sopenharmony_ci		return false;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (new_limit < (1U << pasid_bits)) {
368c2ecf20Sopenharmony_ci		if (pasids_allocated)
378c2ecf20Sopenharmony_ci			/* We've already allocated user PASIDs, too late to
388c2ecf20Sopenharmony_ci			 * change the limit
398c2ecf20Sopenharmony_ci			 */
408c2ecf20Sopenharmony_ci			return false;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci		while (new_limit < (1U << pasid_bits))
438c2ecf20Sopenharmony_ci			pasid_bits--;
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	return true;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciunsigned int kfd_get_pasid_limit(void)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	return 1U << pasid_bits;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciu32 kfd_pasid_alloc(void)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	int r = amdgpu_pasid_alloc(pasid_bits);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (r > 0) {
598c2ecf20Sopenharmony_ci		pasids_allocated = true;
608c2ecf20Sopenharmony_ci		return r;
618c2ecf20Sopenharmony_ci	}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return 0;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_civoid kfd_pasid_free(u32 pasid)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	amdgpu_pasid_free(pasid);
698c2ecf20Sopenharmony_ci}
70