drm/ttm: s/FLAG_SG/FLAG_EXTERNAL/
[linux-2.6-microblaze.git] / include / drm / ttm / ttm_tt.h
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_TT_FLAG_SWAPPED     (1 << 0)
42 #define TTM_TT_FLAG_ZERO_ALLOC  (1 << 1)
43 #define TTM_TT_FLAG_EXTERNAL    (1 << 2)
44
45 #define TTM_TT_FLAG_PRIV_POPULATED  (1 << 31)
46
47 /**
48  * struct ttm_tt
49  *
50  * @pages: Array of pages backing the data.
51  * @page_flags: see TTM_TT_FLAG_*
52  * @num_pages: Number of pages in the page array.
53  * @sg: for SG objects via dma-buf
54  * @dma_address: The DMA (bus) addresses of the pages
55  * @swap_storage: Pointer to shmem struct file for swap storage.
56  * @pages_list: used by some page allocation backend
57  * @caching: The current caching state of the pages, see enum ttm_caching.
58  *
59  * This is a structure holding the pages, caching- and aperture binding
60  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
61  * memory.
62  */
63 struct ttm_tt {
64         struct page **pages;
65         uint32_t page_flags;
66         uint32_t num_pages;
67         struct sg_table *sg;
68         dma_addr_t *dma_address;
69         struct file *swap_storage;
70         enum ttm_caching caching;
71 };
72
73 /**
74  * struct ttm_kmap_iter_tt - Specialization of a mappig iterator for a tt.
75  * @base: Embedded struct ttm_kmap_iter providing the usage interface
76  * @tt: Cached struct ttm_tt.
77  * @prot: Cached page protection for mapping.
78  */
79 struct ttm_kmap_iter_tt {
80         struct ttm_kmap_iter base;
81         struct ttm_tt *tt;
82         pgprot_t prot;
83 };
84
85 static inline bool ttm_tt_is_populated(struct ttm_tt *tt)
86 {
87         return tt->page_flags & TTM_TT_FLAG_PRIV_POPULATED;
88 }
89
90 /**
91  * ttm_tt_create
92  *
93  * @bo: pointer to a struct ttm_buffer_object
94  * @zero_alloc: true if allocated pages needs to be zeroed
95  *
96  * Make sure we have a TTM structure allocated for the given BO.
97  * No pages are actually allocated.
98  */
99 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
100
101 /**
102  * ttm_tt_init
103  *
104  * @ttm: The struct ttm_tt.
105  * @bo: The buffer object we create the ttm for.
106  * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
107  * @caching: the desired caching state of the pages
108  *
109  * Create a struct ttm_tt to back data with system memory pages.
110  * No pages are actually allocated.
111  * Returns:
112  * NULL: Out of memory.
113  */
114 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
115                 uint32_t page_flags, enum ttm_caching caching);
116 int ttm_sg_tt_init(struct ttm_tt *ttm_dma, struct ttm_buffer_object *bo,
117                    uint32_t page_flags, enum ttm_caching caching);
118
119 /**
120  * ttm_tt_fini
121  *
122  * @ttm: the ttm_tt structure.
123  *
124  * Free memory of ttm_tt structure
125  */
126 void ttm_tt_fini(struct ttm_tt *ttm);
127
128 /**
129  * ttm_tt_destroy:
130  *
131  * @bdev: the ttm_device this object belongs to
132  * @ttm: The struct ttm_tt.
133  *
134  * Unbind, unpopulate and destroy common struct ttm_tt.
135  */
136 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm);
137
138 /**
139  * ttm_tt_swapin:
140  *
141  * @ttm: The struct ttm_tt.
142  *
143  * Swap in a previously swap out ttm_tt.
144  */
145 int ttm_tt_swapin(struct ttm_tt *ttm);
146 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
147                    gfp_t gfp_flags);
148
149 /**
150  * ttm_tt_populate - allocate pages for a ttm
151  *
152  * @bdev: the ttm_device this object belongs to
153  * @ttm: Pointer to the ttm_tt structure
154  * @ctx: operation context for populating the tt object.
155  *
156  * Calls the driver method to allocate pages for a ttm
157  */
158 int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm,
159                     struct ttm_operation_ctx *ctx);
160
161 /**
162  * ttm_tt_unpopulate - free pages from a ttm
163  *
164  * @bdev: the ttm_device this object belongs to
165  * @ttm: Pointer to the ttm_tt structure
166  *
167  * Calls the driver method to free all pages from a ttm
168  */
169 void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
170
171 /**
172  * ttm_tt_mark_for_clear - Mark pages for clearing on populate.
173  *
174  * @ttm: Pointer to the ttm_tt structure
175  *
176  * Marks pages for clearing so that the next time the page vector is
177  * populated, the pages will be cleared.
178  */
179 static inline void ttm_tt_mark_for_clear(struct ttm_tt *ttm)
180 {
181         ttm->page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
182 }
183
184 void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages);
185
186 struct ttm_kmap_iter *ttm_kmap_iter_tt_init(struct ttm_kmap_iter_tt *iter_tt,
187                                             struct ttm_tt *tt);
188
189 #if IS_ENABLED(CONFIG_AGP)
190 #include <linux/agp_backend.h>
191
192 /**
193  * ttm_agp_tt_create
194  *
195  * @bo: Buffer object we allocate the ttm for.
196  * @bridge: The agp bridge this device is sitting on.
197  * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
198  *
199  *
200  * Create a TTM backend that uses the indicated AGP bridge as an aperture
201  * for TT memory. This function uses the linux agpgart interface to
202  * bind and unbind memory backing a ttm_tt.
203  */
204 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
205                                  struct agp_bridge_data *bridge,
206                                  uint32_t page_flags);
207 int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);
208 void ttm_agp_unbind(struct ttm_tt *ttm);
209 void ttm_agp_destroy(struct ttm_tt *ttm);
210 bool ttm_agp_is_bound(struct ttm_tt *ttm);
211 #endif
212
213 #endif