• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 - Google LLC
4  * Author: David Brazdil <dbrazdil@google.com>
5  */
6 
7 #include <linux/kvm_host.h>
8 
dev_to_id(struct device * dev)9 static unsigned long dev_to_id(struct device *dev)
10 {
11 	/* Use the struct device pointer as a unique identifier. */
12 	return (unsigned long)dev;
13 }
14 
pkvm_iommu_driver_init(enum pkvm_iommu_driver_id id,void * data,size_t size)15 int pkvm_iommu_driver_init(enum pkvm_iommu_driver_id id, void *data, size_t size)
16 {
17 	return kvm_call_hyp_nvhe(__pkvm_iommu_driver_init, id, data, size);
18 }
19 
pkvm_iommu_register(struct device * dev,enum pkvm_iommu_driver_id drv_id,phys_addr_t pa,size_t size,struct device * parent)20 int pkvm_iommu_register(struct device *dev, enum pkvm_iommu_driver_id drv_id,
21 			phys_addr_t pa, size_t size, struct device *parent)
22 {
23 	void *mem;
24 	int ret;
25 
26 	/*
27 	 * Hypcall to register the device. It will return -ENOMEM if it needs
28 	 * more memory. In that case allocate a page and retry.
29 	 * We assume that hyp never allocates more than a page per hypcall.
30 	 */
31 	ret = kvm_call_hyp_nvhe(__pkvm_iommu_register, dev_to_id(dev),
32 				drv_id, pa, size, dev_to_id(parent), NULL, 0);
33 	if (ret == -ENOMEM) {
34 		mem = (void *)__get_free_page(GFP_KERNEL);
35 		if (!mem)
36 			return -ENOMEM;
37 
38 		ret = kvm_call_hyp_nvhe(__pkvm_iommu_register, dev_to_id(dev),
39 					drv_id, pa, size, dev_to_id(parent),
40 					mem, PAGE_SIZE);
41 	}
42 	return ret;
43 }
44 
pkvm_iommu_suspend(struct device * dev)45 int pkvm_iommu_suspend(struct device *dev)
46 {
47 	return kvm_call_hyp_nvhe(__pkvm_iommu_pm_notify, dev_to_id(dev),
48 				 PKVM_IOMMU_PM_SUSPEND);
49 }
50 EXPORT_SYMBOL_GPL(pkvm_iommu_suspend);
51 
pkvm_iommu_resume(struct device * dev)52 int pkvm_iommu_resume(struct device *dev)
53 {
54 	return kvm_call_hyp_nvhe(__pkvm_iommu_pm_notify, dev_to_id(dev),
55 				 PKVM_IOMMU_PM_RESUME);
56 }
57 EXPORT_SYMBOL_GPL(pkvm_iommu_resume);
58 
pkvm_iommu_finalize(void)59 int pkvm_iommu_finalize(void)
60 {
61 	return kvm_call_hyp_nvhe(__pkvm_iommu_finalize);
62 }
63 EXPORT_SYMBOL_GPL(pkvm_iommu_finalize);
64