xref: /linux/include/drm/ttm/ttm_tt.h (revision 0ad53fe3ae82443c74ff8cfd7bd13377cc1134a3)
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 #ifndef _TTM_TT_H_
28 #define _TTM_TT_H_
29 
30 #include <linux/pagemap.h>
31 #include <linux/types.h>
32 #include <drm/ttm/ttm_caching.h>
33 #include <drm/ttm/ttm_kmap_iter.h>
34 
35 struct ttm_device;
36 struct ttm_tt;
37 struct ttm_resource;
38 struct ttm_buffer_object;
39 struct ttm_operation_ctx;
40 
41 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
42 #define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
43 #define TTM_PAGE_FLAG_SG              (1 << 8)
44 #define TTM_PAGE_FLAG_NO_RETRY	      (1 << 9)
45 
46 #define TTM_PAGE_FLAG_PRIV_POPULATED  (1 << 31)
47 
48 /**
49  * struct ttm_tt
50  *
51  * @pages: Array of pages backing the data.
52  * @page_flags: see TTM_PAGE_FLAG_*
53  * @num_pages: Number of pages in the page array.
54  * @sg: for SG objects via dma-buf
55  * @dma_address: The DMA (bus) addresses of the pages
56  * @swap_storage: Pointer to shmem struct file for swap storage.
57  * @pages_list: used by some page allocation backend
58  * @caching: The current caching state of the pages, see enum ttm_caching.
59  *
60  * This is a structure holding the pages, caching- and aperture binding
61  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
62  * memory.
63  */
64 struct ttm_tt {
65 	struct page **pages;
66 	uint32_t page_flags;
67 	uint32_t num_pages;
68 	struct sg_table *sg;
69 	dma_addr_t *dma_address;
70 	struct file *swap_storage;
71 	enum ttm_caching caching;
72 };
73 
74 /**
75  * struct ttm_kmap_iter_tt - Specialization of a mappig iterator for a tt.
76  * @base: Embedded struct ttm_kmap_iter providing the usage interface
77  * @tt: Cached struct ttm_tt.
78  * @prot: Cached page protection for mapping.
79  */
80 struct ttm_kmap_iter_tt {
81 	struct ttm_kmap_iter base;
82 	struct ttm_tt *tt;
83 	pgprot_t prot;
84 };
85 
86 static inline bool ttm_tt_is_populated(struct ttm_tt *tt)
87 {
88 	return tt->page_flags & TTM_PAGE_FLAG_PRIV_POPULATED;
89 }
90 
91 /**
92  * ttm_tt_create
93  *
94  * @bo: pointer to a struct ttm_buffer_object
95  * @zero_alloc: true if allocated pages needs to be zeroed
96  *
97  * Make sure we have a TTM structure allocated for the given BO.
98  * No pages are actually allocated.
99  */
100 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
101 
102 /**
103  * ttm_tt_init
104  *
105  * @ttm: The struct ttm_tt.
106  * @bo: The buffer object we create the ttm for.
107  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
108  * @caching: the desired caching state of the pages
109  *
110  * Create a struct ttm_tt to back data with system memory pages.
111  * No pages are actually allocated.
112  * Returns:
113  * NULL: Out of memory.
114  */
115 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
116 		uint32_t page_flags, enum ttm_caching caching);
117 int ttm_sg_tt_init(struct ttm_tt *ttm_dma, struct ttm_buffer_object *bo,
118 		   uint32_t page_flags, enum ttm_caching caching);
119 
120 /**
121  * ttm_tt_fini
122  *
123  * @ttm: the ttm_tt structure.
124  *
125  * Free memory of ttm_tt structure
126  */
127 void ttm_tt_fini(struct ttm_tt *ttm);
128 
129 /**
130  * ttm_tt_destroy:
131  *
132  * @bdev: the ttm_device this object belongs to
133  * @ttm: The struct ttm_tt.
134  *
135  * Unbind, unpopulate and destroy common struct ttm_tt.
136  */
137 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm);
138 
139 /**
140  * ttm_tt_swapin:
141  *
142  * @ttm: The struct ttm_tt.
143  *
144  * Swap in a previously swap out ttm_tt.
145  */
146 int ttm_tt_swapin(struct ttm_tt *ttm);
147 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
148 		   gfp_t gfp_flags);
149 
150 /**
151  * ttm_tt_populate - allocate pages for a ttm
152  *
153  * @bdev: the ttm_device this object belongs to
154  * @ttm: Pointer to the ttm_tt structure
155  * @ctx: operation context for populating the tt object.
156  *
157  * Calls the driver method to allocate pages for a ttm
158  */
159 int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm,
160 		    struct ttm_operation_ctx *ctx);
161 
162 /**
163  * ttm_tt_unpopulate - free pages from a ttm
164  *
165  * @bdev: the ttm_device this object belongs to
166  * @ttm: Pointer to the ttm_tt structure
167  *
168  * Calls the driver method to free all pages from a ttm
169  */
170 void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
171 
172 /**
173  * ttm_tt_mark_for_clear - Mark pages for clearing on populate.
174  *
175  * @ttm: Pointer to the ttm_tt structure
176  *
177  * Marks pages for clearing so that the next time the page vector is
178  * populated, the pages will be cleared.
179  */
180 static inline void ttm_tt_mark_for_clear(struct ttm_tt *ttm)
181 {
182 	ttm->page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
183 }
184 
185 void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages);
186 
187 struct ttm_kmap_iter *ttm_kmap_iter_tt_init(struct ttm_kmap_iter_tt *iter_tt,
188 					    struct ttm_tt *tt);
189 
190 #if IS_ENABLED(CONFIG_AGP)
191 #include <linux/agp_backend.h>
192 
193 /**
194  * ttm_agp_tt_create
195  *
196  * @bo: Buffer object we allocate the ttm for.
197  * @bridge: The agp bridge this device is sitting on.
198  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
199  *
200  *
201  * Create a TTM backend that uses the indicated AGP bridge as an aperture
202  * for TT memory. This function uses the linux agpgart interface to
203  * bind and unbind memory backing a ttm_tt.
204  */
205 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
206 				 struct agp_bridge_data *bridge,
207 				 uint32_t page_flags);
208 int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);
209 void ttm_agp_unbind(struct ttm_tt *ttm);
210 void ttm_agp_destroy(struct ttm_tt *ttm);
211 bool ttm_agp_is_bound(struct ttm_tt *ttm);
212 #endif
213 
214 #endif
215