xref: /openbmc/linux/include/drm/ttm/ttm_tt.h (revision 05911c5d)
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 #include <drm/ttm/ttm_caching.h>
32 
33 struct ttm_bo_device;
34 struct ttm_tt;
35 struct ttm_resource;
36 struct ttm_buffer_object;
37 struct ttm_operation_ctx;
38 
39 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
40 #define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
41 #define TTM_PAGE_FLAG_SG              (1 << 8)
42 #define TTM_PAGE_FLAG_NO_RETRY	      (1 << 9)
43 
44 #define TTM_PAGE_FLAG_PRIV_POPULATED  (1 << 31)
45 
46 /**
47  * struct ttm_tt
48  *
49  * @pages: Array of pages backing the data.
50  * @page_flags: see TTM_PAGE_FLAG_*
51  * @num_pages: Number of pages in the page array.
52  * @sg: for SG objects via dma-buf
53  * @dma_address: The DMA (bus) addresses of the pages
54  * @swap_storage: Pointer to shmem struct file for swap storage.
55  * @pages_list: used by some page allocation backend
56  * @caching: The current caching state of the pages.
57  *
58  * This is a structure holding the pages, caching- and aperture binding
59  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
60  * memory.
61  */
62 struct ttm_tt {
63 	struct page **pages;
64 	uint32_t page_flags;
65 	uint32_t num_pages;
66 	struct sg_table *sg;
67 	dma_addr_t *dma_address;
68 	struct file *swap_storage;
69 	enum ttm_caching caching;
70 };
71 
72 static inline bool ttm_tt_is_populated(struct ttm_tt *tt)
73 {
74 	return tt->page_flags & TTM_PAGE_FLAG_PRIV_POPULATED;
75 }
76 
77 /**
78  * ttm_tt_create
79  *
80  * @bo: pointer to a struct ttm_buffer_object
81  * @zero_alloc: true if allocated pages needs to be zeroed
82  *
83  * Make sure we have a TTM structure allocated for the given BO.
84  * No pages are actually allocated.
85  */
86 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
87 
88 /**
89  * ttm_tt_init
90  *
91  * @ttm: The struct ttm_tt.
92  * @bo: The buffer object we create the ttm for.
93  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
94  * @caching: the desired caching state of the pages
95  *
96  * Create a struct ttm_tt to back data with system memory pages.
97  * No pages are actually allocated.
98  * Returns:
99  * NULL: Out of memory.
100  */
101 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
102 		uint32_t page_flags, enum ttm_caching caching);
103 int ttm_sg_tt_init(struct ttm_tt *ttm_dma, struct ttm_buffer_object *bo,
104 		   uint32_t page_flags, enum ttm_caching caching);
105 
106 /**
107  * ttm_tt_fini
108  *
109  * @ttm: the ttm_tt structure.
110  *
111  * Free memory of ttm_tt structure
112  */
113 void ttm_tt_fini(struct ttm_tt *ttm);
114 
115 /**
116  * ttm_ttm_destroy:
117  *
118  * @ttm: The struct ttm_tt.
119  *
120  * Unbind, unpopulate and destroy common struct ttm_tt.
121  */
122 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm);
123 
124 /**
125  * ttm_tt_destroy_common:
126  *
127  * Called from driver to destroy common path.
128  */
129 void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm);
130 
131 /**
132  * ttm_tt_swapin:
133  *
134  * @ttm: The struct ttm_tt.
135  *
136  * Swap in a previously swap out ttm_tt.
137  */
138 int ttm_tt_swapin(struct ttm_tt *ttm);
139 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
140 		   gfp_t gfp_flags);
141 
142 /**
143  * ttm_tt_populate - allocate pages for a ttm
144  *
145  * @ttm: Pointer to the ttm_tt structure
146  *
147  * Calls the driver method to allocate pages for a ttm
148  */
149 int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_operation_ctx *ctx);
150 
151 /**
152  * ttm_tt_unpopulate - free pages from a ttm
153  *
154  * @ttm: Pointer to the ttm_tt structure
155  *
156  * Calls the driver method to free all pages from a ttm
157  */
158 void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
159 
160 void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages);
161 
162 #if IS_ENABLED(CONFIG_AGP)
163 #include <linux/agp_backend.h>
164 
165 /**
166  * ttm_agp_tt_create
167  *
168  * @bo: Buffer object we allocate the ttm for.
169  * @bridge: The agp bridge this device is sitting on.
170  * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
171  *
172  *
173  * Create a TTM backend that uses the indicated AGP bridge as an aperture
174  * for TT memory. This function uses the linux agpgart interface to
175  * bind and unbind memory backing a ttm_tt.
176  */
177 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
178 				 struct agp_bridge_data *bridge,
179 				 uint32_t page_flags);
180 int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);
181 void ttm_agp_unbind(struct ttm_tt *ttm);
182 void ttm_agp_destroy(struct ttm_tt *ttm);
183 bool ttm_agp_is_bound(struct ttm_tt *ttm);
184 #endif
185 
186 #endif
187