xref: /openbmc/linux/include/drm/ttm/ttm_tt.h (revision 6c9111bc)
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/types.h>
31 
32 struct ttm_tt;
33 struct ttm_resource;
34 struct ttm_buffer_object;
35 struct ttm_operation_ctx;
36 
37 #define TTM_PAGE_FLAG_WRITE           (1 << 3)
38 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
39 #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
40 #define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
41 #define TTM_PAGE_FLAG_DMA32           (1 << 7)
42 #define TTM_PAGE_FLAG_SG              (1 << 8)
43 #define TTM_PAGE_FLAG_NO_RETRY	      (1 << 9)
44 
45 #define TTM_PAGE_FLAG_PRIV_POPULATED  (1 << 31)
46 
47 enum ttm_caching_state {
48 	tt_uncached,
49 	tt_wc,
50 	tt_cached
51 };
52 
53 /**
54  * struct ttm_tt
55  *
56  * @pages: Array of pages backing the data.
57  * @num_pages: Number of pages in the page array.
58  * @bdev: Pointer to the current struct ttm_bo_device.
59  * @be: Pointer to the ttm backend.
60  * @swap_storage: Pointer to shmem struct file for swap storage.
61  * @caching_state: The current caching state of the pages.
62  * @state: The current binding state of the pages.
63  *
64  * This is a structure holding the pages, caching- and aperture binding
65  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
66  * memory.
67  */
68 struct ttm_tt {
69 	struct page **pages;
70 	uint32_t page_flags;
71 	unsigned long num_pages;
72 	struct sg_table *sg; /* for SG objects via dma-buf */
73 	struct file *swap_storage;
74 	enum ttm_caching_state caching_state;
75 };
76 
77 static inline bool ttm_tt_is_populated(struct ttm_tt *tt)
78 {
79 	return tt->page_flags & TTM_PAGE_FLAG_PRIV_POPULATED;
80 }
81 
82 static inline void ttm_tt_set_unpopulated(struct ttm_tt *tt)
83 {
84 	tt->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
85 }
86 
87 static inline void ttm_tt_set_populated(struct ttm_tt *tt)
88 {
89 	tt->page_flags |= TTM_PAGE_FLAG_PRIV_POPULATED;
90 }
91 
92 /**
93  * struct ttm_dma_tt
94  *
95  * @ttm: Base ttm_tt struct.
96  * @dma_address: The DMA (bus) addresses of the pages
97  * @pages_list: used by some page allocation backend
98  *
99  * This is a structure holding the pages, caching- and aperture binding
100  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
101  * memory.
102  */
103 struct ttm_dma_tt {
104 	struct ttm_tt ttm;
105 	dma_addr_t *dma_address;
106 	struct list_head pages_list;
107 };
108 
109 /**
110  * ttm_tt_create
111  *
112  * @bo: pointer to a struct ttm_buffer_object
113  * @zero_alloc: true if allocated pages needs to be zeroed
114  *
115  * Make sure we have a TTM structure allocated for the given BO.
116  * No pages are actually allocated.
117  */
118 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
119 
120 /**
121  * ttm_tt_init
122  *
123  * @ttm: The struct ttm_tt.
124  * @bo: The buffer object we create the ttm for.
125  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
126  *
127  * Create a struct ttm_tt to back data with system memory pages.
128  * No pages are actually allocated.
129  * Returns:
130  * NULL: Out of memory.
131  */
132 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
133 		uint32_t page_flags);
134 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
135 		    uint32_t page_flags);
136 int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
137 		   uint32_t page_flags);
138 
139 /**
140  * ttm_tt_fini
141  *
142  * @ttm: the ttm_tt structure.
143  *
144  * Free memory of ttm_tt structure
145  */
146 void ttm_tt_fini(struct ttm_tt *ttm);
147 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
148 
149 /**
150  * ttm_ttm_destroy:
151  *
152  * @ttm: The struct ttm_tt.
153  *
154  * Unbind, unpopulate and destroy common struct ttm_tt.
155  */
156 void ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
157 
158 /**
159  * ttm_tt_destroy_common:
160  *
161  * Called from driver to destroy common path.
162  */
163 void ttm_tt_destroy_common(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
164 
165 /**
166  * ttm_tt_swapin:
167  *
168  * @ttm: The struct ttm_tt.
169  *
170  * Swap in a previously swap out ttm_tt.
171  */
172 int ttm_tt_swapin(struct ttm_tt *ttm);
173 
174 /**
175  * ttm_tt_set_placement_caching:
176  *
177  * @ttm A struct ttm_tt the backing pages of which will change caching policy.
178  * @placement: Flag indicating the desired caching policy.
179  *
180  * This function will change caching policy of any default kernel mappings of
181  * the pages backing @ttm. If changing from cached to uncached or
182  * write-combined,
183  * all CPU caches will first be flushed to make sure the data of the pages
184  * hit RAM. This function may be very costly as it involves global TLB
185  * and cache flushes and potential page splitting / combining.
186  */
187 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
188 int ttm_tt_swapout(struct ttm_bo_device *bdev, struct ttm_tt *ttm, struct file *persistent_swap_storage);
189 
190 /**
191  * ttm_tt_populate - allocate pages for a ttm
192  *
193  * @ttm: Pointer to the ttm_tt structure
194  *
195  * Calls the driver method to allocate pages for a ttm
196  */
197 int ttm_tt_populate(struct ttm_bo_device *bdev, struct ttm_tt *ttm, struct ttm_operation_ctx *ctx);
198 
199 /**
200  * ttm_tt_unpopulate - free pages from a ttm
201  *
202  * @ttm: Pointer to the ttm_tt structure
203  *
204  * Calls the driver method to free all pages from a ttm
205  */
206 void ttm_tt_unpopulate(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
207 
208 #if IS_ENABLED(CONFIG_AGP)
209 #include <linux/agp_backend.h>
210 
211 /**
212  * ttm_agp_tt_create
213  *
214  * @bo: Buffer object we allocate the ttm for.
215  * @bridge: The agp bridge this device is sitting on.
216  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
217  *
218  *
219  * Create a TTM backend that uses the indicated AGP bridge as an aperture
220  * for TT memory. This function uses the linux agpgart interface to
221  * bind and unbind memory backing a ttm_tt.
222  */
223 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
224 				 struct agp_bridge_data *bridge,
225 				 uint32_t page_flags);
226 int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);
227 void ttm_agp_unbind(struct ttm_tt *ttm);
228 void ttm_agp_destroy(struct ttm_tt *ttm);
229 bool ttm_agp_is_bound(struct ttm_tt *ttm);
230 #endif
231 
232 #endif
233