x86/sgx: Add a page reclaimer
[linux-2.6-block.git] / arch / x86 / kernel / cpu / sgx / sgx.h
CommitLineData
e7e05452
SC
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _X86_SGX_H
3#define _X86_SGX_H
4
5#include <linux/bitops.h>
6#include <linux/err.h>
7#include <linux/io.h>
8#include <linux/rwsem.h>
9#include <linux/types.h>
10#include <asm/asm.h>
11#include "arch.h"
12
13#undef pr_fmt
14#define pr_fmt(fmt) "sgx: " fmt
15
16#define SGX_MAX_EPC_SECTIONS 8
c6d26d37 17#define SGX_EEXTEND_BLOCK_SIZE 256
1728ab54
JS
18#define SGX_NR_TO_SCAN 16
19#define SGX_NR_LOW_PAGES 32
20#define SGX_NR_HIGH_PAGES 64
21
22/* Pages, which are being tracked by the page reclaimer. */
23#define SGX_EPC_PAGE_RECLAIMER_TRACKED BIT(0)
e7e05452
SC
24
25struct sgx_epc_page {
26 unsigned int section;
1728ab54
JS
27 unsigned int flags;
28 struct sgx_encl_page *owner;
e7e05452
SC
29 struct list_head list;
30};
31
32/*
33 * The firmware can define multiple chunks of EPC to the different areas of the
34 * physical memory e.g. for memory areas of the each node. This structure is
35 * used to store EPC pages for one EPC section and virtual memory area where
36 * the pages have been mapped.
37 */
38struct sgx_epc_section {
39 unsigned long phys_addr;
40 void *virt_addr;
41 struct list_head page_list;
42 struct list_head laundry_list;
43 struct sgx_epc_page *pages;
1728ab54 44 unsigned long free_cnt;
e7e05452
SC
45 spinlock_t lock;
46};
47
48extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS];
49
50static inline unsigned long sgx_get_epc_phys_addr(struct sgx_epc_page *page)
51{
52 struct sgx_epc_section *section = &sgx_epc_sections[page->section];
53 unsigned long index;
54
55 index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page);
56
57 return section->phys_addr + index * PAGE_SIZE;
58}
59
60static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page)
61{
62 struct sgx_epc_section *section = &sgx_epc_sections[page->section];
63 unsigned long index;
64
65 index = ((unsigned long)page - (unsigned long)section->pages) / sizeof(*page);
66
67 return section->virt_addr + index * PAGE_SIZE;
68}
69
d2285493
JS
70struct sgx_epc_page *__sgx_alloc_epc_page(void);
71void sgx_free_epc_page(struct sgx_epc_page *page);
72
1728ab54
JS
73void sgx_mark_page_reclaimable(struct sgx_epc_page *page);
74int sgx_unmark_page_reclaimable(struct sgx_epc_page *page);
75struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim);
76
e7e05452 77#endif /* _X86_SGX_H */