xref: /linux/arch/arm64/mm/mem_encrypt.c (revision c771600c6af14749609b49565ffb4cac2959710d)
1e7bafbf7SWill Deacon // SPDX-License-Identifier: GPL-2.0-only
2e7bafbf7SWill Deacon /*
3e7bafbf7SWill Deacon  * Implementation of the memory encryption/decryption API.
4e7bafbf7SWill Deacon  *
5e7bafbf7SWill Deacon  * Since the low-level details of the operation depend on the
6e7bafbf7SWill Deacon  * Confidential Computing environment (e.g. pKVM, CCA, ...), this just
7e7bafbf7SWill Deacon  * acts as a top-level dispatcher to whatever hooks may have been
8e7bafbf7SWill Deacon  * registered.
9e7bafbf7SWill Deacon  *
10e7bafbf7SWill Deacon  * Author: Will Deacon <will@kernel.org>
11e7bafbf7SWill Deacon  * Copyright (C) 2024 Google LLC
12e7bafbf7SWill Deacon  *
13e7bafbf7SWill Deacon  * "Hello, boils and ghouls!"
14e7bafbf7SWill Deacon  */
15e7bafbf7SWill Deacon 
16e7bafbf7SWill Deacon #include <linux/bug.h>
17e7bafbf7SWill Deacon #include <linux/compiler.h>
18e7bafbf7SWill Deacon #include <linux/err.h>
19e7bafbf7SWill Deacon #include <linux/mm.h>
20e7bafbf7SWill Deacon 
21e7bafbf7SWill Deacon #include <asm/mem_encrypt.h>
22e7bafbf7SWill Deacon 
23e7bafbf7SWill Deacon static const struct arm64_mem_crypt_ops *crypt_ops;
24e7bafbf7SWill Deacon 
arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops * ops)25e7bafbf7SWill Deacon int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops)
26e7bafbf7SWill Deacon {
27e7bafbf7SWill Deacon 	if (WARN_ON(crypt_ops))
28e7bafbf7SWill Deacon 		return -EBUSY;
29e7bafbf7SWill Deacon 
30e7bafbf7SWill Deacon 	crypt_ops = ops;
31e7bafbf7SWill Deacon 	return 0;
32e7bafbf7SWill Deacon }
33e7bafbf7SWill Deacon 
set_memory_encrypted(unsigned long addr,int numpages)34e7bafbf7SWill Deacon int set_memory_encrypted(unsigned long addr, int numpages)
35e7bafbf7SWill Deacon {
36e7bafbf7SWill Deacon 	if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
37e7bafbf7SWill Deacon 		return 0;
38e7bafbf7SWill Deacon 
39e7bafbf7SWill Deacon 	return crypt_ops->encrypt(addr, numpages);
40e7bafbf7SWill Deacon }
41e7bafbf7SWill Deacon EXPORT_SYMBOL_GPL(set_memory_encrypted);
42e7bafbf7SWill Deacon 
set_memory_decrypted(unsigned long addr,int numpages)43e7bafbf7SWill Deacon int set_memory_decrypted(unsigned long addr, int numpages)
44e7bafbf7SWill Deacon {
45e7bafbf7SWill Deacon 	if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
46e7bafbf7SWill Deacon 		return 0;
47e7bafbf7SWill Deacon 
48e7bafbf7SWill Deacon 	return crypt_ops->decrypt(addr, numpages);
49e7bafbf7SWill Deacon }
50e7bafbf7SWill Deacon EXPORT_SYMBOL_GPL(set_memory_decrypted);
51