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