xref: /openbmc/linux/arch/x86/kernel/cpu/sgx/sgx.h (revision 901ddbb9)
1e7e05452SSean Christopherson /* SPDX-License-Identifier: GPL-2.0 */
2e7e05452SSean Christopherson #ifndef _X86_SGX_H
3e7e05452SSean Christopherson #define _X86_SGX_H
4e7e05452SSean Christopherson 
5e7e05452SSean Christopherson #include <linux/bitops.h>
6e7e05452SSean Christopherson #include <linux/err.h>
7e7e05452SSean Christopherson #include <linux/io.h>
8e7e05452SSean Christopherson #include <linux/rwsem.h>
9e7e05452SSean Christopherson #include <linux/types.h>
10e7e05452SSean Christopherson #include <asm/asm.h>
11e7e05452SSean Christopherson #include "arch.h"
12e7e05452SSean Christopherson 
13e7e05452SSean Christopherson #undef pr_fmt
14e7e05452SSean Christopherson #define pr_fmt(fmt) "sgx: " fmt
15e7e05452SSean Christopherson 
16e7e05452SSean Christopherson #define SGX_MAX_EPC_SECTIONS		8
17c6d26d37SJarkko Sakkinen #define SGX_EEXTEND_BLOCK_SIZE		256
181728ab54SJarkko Sakkinen #define SGX_NR_TO_SCAN			16
191728ab54SJarkko Sakkinen #define SGX_NR_LOW_PAGES		32
201728ab54SJarkko Sakkinen #define SGX_NR_HIGH_PAGES		64
211728ab54SJarkko Sakkinen 
221728ab54SJarkko Sakkinen /* Pages, which are being tracked by the page reclaimer. */
231728ab54SJarkko Sakkinen #define SGX_EPC_PAGE_RECLAIMER_TRACKED	BIT(0)
24e7e05452SSean Christopherson 
25e7e05452SSean Christopherson struct sgx_epc_page {
26e7e05452SSean Christopherson 	unsigned int section;
271728ab54SJarkko Sakkinen 	unsigned int flags;
281728ab54SJarkko Sakkinen 	struct sgx_encl_page *owner;
29e7e05452SSean Christopherson 	struct list_head list;
30e7e05452SSean Christopherson };
31e7e05452SSean Christopherson 
32e7e05452SSean Christopherson /*
33*901ddbb9SJarkko Sakkinen  * Contains the tracking data for NUMA nodes having EPC pages. Most importantly,
34*901ddbb9SJarkko Sakkinen  * the free page list local to the node is stored here.
35*901ddbb9SJarkko Sakkinen  */
36*901ddbb9SJarkko Sakkinen struct sgx_numa_node {
37*901ddbb9SJarkko Sakkinen 	struct list_head free_page_list;
38*901ddbb9SJarkko Sakkinen 	spinlock_t lock;
39*901ddbb9SJarkko Sakkinen };
40*901ddbb9SJarkko Sakkinen 
41*901ddbb9SJarkko Sakkinen /*
42e7e05452SSean Christopherson  * The firmware can define multiple chunks of EPC to the different areas of the
43e7e05452SSean Christopherson  * physical memory e.g. for memory areas of the each node. This structure is
44e7e05452SSean Christopherson  * used to store EPC pages for one EPC section and virtual memory area where
45e7e05452SSean Christopherson  * the pages have been mapped.
46e7e05452SSean Christopherson  */
47e7e05452SSean Christopherson struct sgx_epc_section {
48e7e05452SSean Christopherson 	unsigned long phys_addr;
49e7e05452SSean Christopherson 	void *virt_addr;
50e7e05452SSean Christopherson 	struct sgx_epc_page *pages;
51*901ddbb9SJarkko Sakkinen 	struct sgx_numa_node *node;
52e7e05452SSean Christopherson };
53e7e05452SSean Christopherson 
54e7e05452SSean Christopherson extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
55e7e05452SSean Christopherson 
56e7e05452SSean Christopherson static inline unsigned long sgx_get_epc_phys_addr(struct sgx_epc_page *page)
57e7e05452SSean Christopherson {
58e7e05452SSean Christopherson 	struct sgx_epc_section *section = &sgx_epc_sections[page->section];
59e7e05452SSean Christopherson 	unsigned long index;
60e7e05452SSean Christopherson 
61e7e05452SSean Christopherson 	index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page);
62e7e05452SSean Christopherson 
63e7e05452SSean Christopherson 	return section->phys_addr + index * PAGE_SIZE;
64e7e05452SSean Christopherson }
65e7e05452SSean Christopherson 
66e7e05452SSean Christopherson static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page)
67e7e05452SSean Christopherson {
68e7e05452SSean Christopherson 	struct sgx_epc_section *section = &sgx_epc_sections[page->section];
69e7e05452SSean Christopherson 	unsigned long index;
70e7e05452SSean Christopherson 
71e7e05452SSean Christopherson 	index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page);
72e7e05452SSean Christopherson 
73e7e05452SSean Christopherson 	return section->virt_addr + index * PAGE_SIZE;
74e7e05452SSean Christopherson }
75e7e05452SSean Christopherson 
76d2285493SJarkko Sakkinen struct sgx_epc_page *__sgx_alloc_epc_page(void);
77d2285493SJarkko Sakkinen void sgx_free_epc_page(struct sgx_epc_page *page);
78d2285493SJarkko Sakkinen 
791728ab54SJarkko Sakkinen void sgx_mark_page_reclaimable(struct sgx_epc_page *page);
801728ab54SJarkko Sakkinen int sgx_unmark_page_reclaimable(struct sgx_epc_page *page);
811728ab54SJarkko Sakkinen struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim);
821728ab54SJarkko Sakkinen 
83e7e05452SSean Christopherson #endif /* _X86_SGX_H */
84