xref: /linux/include/drm/ttm/ttm_backup.h (revision c27e360545373b7aee9862a5beef3b9fb3df0c25)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 
6 #ifndef _TTM_BACKUP_H_
7 #define _TTM_BACKUP_H_
8 
9 #include <linux/mm_types.h>
10 #include <linux/shmem_fs.h>
11 
12 /**
13  * ttm_backup_handle_to_page_ptr() - Convert handle to struct page pointer
14  * @handle: The handle to convert.
15  *
16  * Converts an opaque handle received from a ttm_backup_backup_*()
17  * function to an (invalid) struct page pointer suitable for a struct page array.
18  *
19  * Return: An (invalid) struct page pointer.
20  */
21 static inline struct page *
22 ttm_backup_handle_to_page_ptr(unsigned long handle)
23 {
24 	return (struct page *)(handle << 1 | 1);
25 }
26 
27 /**
28  * ttm_backup_page_ptr_is_handle() - Whether a struct page pointer is a handle
29  * @page: The struct page pointer to check.
30  *
31  * Return: true if the struct page pointer is a handld returned from
32  * ttm_backup_handle_to_page_ptr(). False otherwise.
33  */
34 static inline bool ttm_backup_page_ptr_is_handle(const struct page *page)
35 {
36 	return (unsigned long)page & 1;
37 }
38 
39 /**
40  * ttm_backup_page_ptr_to_handle() - Convert a struct page pointer to a handle
41  * @page: The struct page pointer to convert
42  *
43  * Return: The handle that was previously used in
44  * ttm_backup_handle_to_page_ptr() to obtain a struct page pointer, suitable
45  * for use as argument in the struct ttm_backup_drop() or
46  * ttm_backup_copy_page() functions.
47  */
48 static inline unsigned long
49 ttm_backup_page_ptr_to_handle(const struct page *page)
50 {
51 	WARN_ON(!ttm_backup_page_ptr_is_handle(page));
52 	return (unsigned long)page >> 1;
53 }
54 
55 void ttm_backup_drop(struct file *backup, pgoff_t handle);
56 
57 int ttm_backup_copy_page(struct file *backup, struct page *dst,
58 			 pgoff_t handle, bool intr, gfp_t additional_gfp);
59 
60 s64
61 ttm_backup_backup_folio(struct file *backup, struct folio *folio,
62 			unsigned int order, bool writeback, pgoff_t idx,
63 			gfp_t folio_gfp, gfp_t alloc_gfp,
64 			pgoff_t *nr_pages_backed);
65 
66 void ttm_backup_fini(struct file *backup);
67 
68 u64 ttm_backup_bytes_avail(void);
69 
70 struct file *ttm_backup_shmem_create(loff_t size);
71 
72 #endif
73