xref: /linux/include/drm/ttm/ttm_pool.h (revision 74ba587f402d5501af2c85e50cf1e4044263b6ca)
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /*
3  * Copyright 2020 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: Christian König
24  */
25 
26 #ifndef _TTM_PAGE_POOL_H_
27 #define _TTM_PAGE_POOL_H_
28 
29 #include <linux/mmzone.h>
30 #include <linux/llist.h>
31 #include <linux/spinlock.h>
32 #include <drm/ttm/ttm_caching.h>
33 
34 struct device;
35 struct seq_file;
36 struct ttm_backup_flags;
37 struct ttm_operation_ctx;
38 struct ttm_pool;
39 struct ttm_tt;
40 
41 /**
42  * struct ttm_pool_type - Pool for a certain memory type
43  *
44  * @pool: the pool we belong to, might be NULL for the global ones
45  * @order: the allocation order our pages have
46  * @caching: the caching type our pages have
47  * @shrinker_list: our place on the global shrinker list
48  * @lock: protection of the page list
49  * @pages: the list of pages in the pool
50  */
51 struct ttm_pool_type {
52 	struct ttm_pool *pool;
53 	unsigned int order;
54 	enum ttm_caching caching;
55 
56 	struct list_head shrinker_list;
57 
58 	spinlock_t lock;
59 	struct list_head pages;
60 };
61 
62 /**
63  * struct ttm_pool - Pool for all caching and orders
64  *
65  * @dev: the device we allocate pages for
66  * @nid: which numa node to use
67  * @alloc_flags: TTM_ALLOCATION_POOL_ flags
68  * @caching: pools for each caching/order
69  */
70 struct ttm_pool {
71 	struct device *dev;
72 	int nid;
73 
74 	unsigned int alloc_flags;
75 
76 	struct {
77 		struct ttm_pool_type orders[NR_PAGE_ORDERS];
78 	} caching[TTM_NUM_CACHING_TYPES];
79 };
80 
81 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
82 		   struct ttm_operation_ctx *ctx);
83 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt);
84 
85 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
86 		   int nid, unsigned int alloc_flags);
87 void ttm_pool_fini(struct ttm_pool *pool);
88 
89 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m);
90 
91 void ttm_pool_drop_backed_up(struct ttm_tt *tt);
92 
93 long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *ttm,
94 		     const struct ttm_backup_flags *flags);
95 int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
96 			       const struct ttm_operation_ctx *ctx);
97 
98 int ttm_pool_mgr_init(unsigned long num_pages);
99 void ttm_pool_mgr_fini(void);
100 
101 #endif
102