brintos

brintos / linux-shallow public Read only

0
0
Text · 8.2 KiB · 2b9d856 Raw
258 lines · c
1/**************************************************************************2 *3 * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA4 * All Rights Reserved.5 *6 * Permission is hereby granted, free of charge, to any person obtaining a7 * copy of this software and associated documentation files (the8 * "Software"), to deal in the Software without restriction, including9 * without limitation the rights to use, copy, modify, merge, publish,10 * distribute, sub license, and/or sell copies of the Software, and to11 * permit persons to whom the Software is furnished to do so, subject to12 * the following conditions:13 *14 * The above copyright notice and this permission notice (including the15 * next paragraph) shall be included in all copies or substantial portions16 * of the Software.17 *18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL21 * 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 OR23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE24 * 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 35struct ttm_device;36struct ttm_tt;37struct ttm_resource;38struct ttm_buffer_object;39struct ttm_operation_ctx;40 41/**42 * struct ttm_tt - This is a structure holding the pages, caching- and aperture43 * binding status for a buffer object that isn't backed by fixed (VRAM / AGP)44 * memory.45 */46struct ttm_tt {47	/** @pages: Array of pages backing the data. */48	struct page **pages;49	/**50	 * @page_flags: The page flags.51	 *52	 * Supported values:53	 *54	 * TTM_TT_FLAG_SWAPPED: Set by TTM when the pages have been unpopulated55	 * and swapped out by TTM.  Calling ttm_tt_populate() will then swap the56	 * pages back in, and unset the flag. Drivers should in general never57	 * need to touch this.58	 *59	 * TTM_TT_FLAG_ZERO_ALLOC: Set if the pages will be zeroed on60	 * allocation.61	 *62	 * TTM_TT_FLAG_EXTERNAL: Set if the underlying pages were allocated63	 * externally, like with dma-buf or userptr. This effectively disables64	 * TTM swapping out such pages.  Also important is to prevent TTM from65	 * ever directly mapping these pages.66	 *67	 * Note that enum ttm_bo_type.ttm_bo_type_sg objects will always enable68	 * this flag.69	 *70	 * TTM_TT_FLAG_EXTERNAL_MAPPABLE: Same behaviour as71	 * TTM_TT_FLAG_EXTERNAL, but with the reduced restriction that it is72	 * still valid to use TTM to map the pages directly. This is useful when73	 * implementing a ttm_tt backend which still allocates driver owned74	 * pages underneath(say with shmem).75	 *76	 * Note that since this also implies TTM_TT_FLAG_EXTERNAL, the usage77	 * here should always be:78	 *79	 *   page_flags = TTM_TT_FLAG_EXTERNAL |80	 *		  TTM_TT_FLAG_EXTERNAL_MAPPABLE;81	 *82	 * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as83	 * not encrypted. The framework will try to match what the dma layer84	 * is doing, but note that it is a little fragile because ttm page85	 * fault handling abuses the DMA api a bit and dma_map_attrs can't be86	 * used to assure pgprot always matches.87	 *88	 * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is89	 * set by TTM after ttm_tt_populate() has successfully returned, and is90	 * then unset when TTM calls ttm_tt_unpopulate().91	 */92#define TTM_TT_FLAG_SWAPPED		BIT(0)93#define TTM_TT_FLAG_ZERO_ALLOC		BIT(1)94#define TTM_TT_FLAG_EXTERNAL		BIT(2)95#define TTM_TT_FLAG_EXTERNAL_MAPPABLE	BIT(3)96#define TTM_TT_FLAG_DECRYPTED		BIT(4)97 98#define TTM_TT_FLAG_PRIV_POPULATED	BIT(5)99	uint32_t page_flags;100	/** @num_pages: Number of pages in the page array. */101	uint32_t num_pages;102	/** @sg: for SG objects via dma-buf. */103	struct sg_table *sg;104	/** @dma_address: The DMA (bus) addresses of the pages. */105	dma_addr_t *dma_address;106	/** @swap_storage: Pointer to shmem struct file for swap storage. */107	struct file *swap_storage;108	/**109	 * @caching: The current caching state of the pages, see enum110	 * ttm_caching.111	 */112	enum ttm_caching caching;113};114 115/**116 * struct ttm_kmap_iter_tt - Specialization of a mappig iterator for a tt.117 * @base: Embedded struct ttm_kmap_iter providing the usage interface118 * @tt: Cached struct ttm_tt.119 * @prot: Cached page protection for mapping.120 */121struct ttm_kmap_iter_tt {122	struct ttm_kmap_iter base;123	struct ttm_tt *tt;124	pgprot_t prot;125};126 127static inline bool ttm_tt_is_populated(struct ttm_tt *tt)128{129	return tt->page_flags & TTM_TT_FLAG_PRIV_POPULATED;130}131 132/**133 * ttm_tt_create134 *135 * @bo: pointer to a struct ttm_buffer_object136 * @zero_alloc: true if allocated pages needs to be zeroed137 *138 * Make sure we have a TTM structure allocated for the given BO.139 * No pages are actually allocated.140 */141int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);142 143/**144 * ttm_tt_init145 *146 * @ttm: The struct ttm_tt.147 * @bo: The buffer object we create the ttm for.148 * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.149 * @caching: the desired caching state of the pages150 * @extra_pages: Extra pages needed for the driver.151 *152 * Create a struct ttm_tt to back data with system memory pages.153 * No pages are actually allocated.154 * Returns:155 * NULL: Out of memory.156 */157int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,158		uint32_t page_flags, enum ttm_caching caching,159		unsigned long extra_pages);160int ttm_sg_tt_init(struct ttm_tt *ttm_dma, struct ttm_buffer_object *bo,161		   uint32_t page_flags, enum ttm_caching caching);162 163/**164 * ttm_tt_fini165 *166 * @ttm: the ttm_tt structure.167 *168 * Free memory of ttm_tt structure169 */170void ttm_tt_fini(struct ttm_tt *ttm);171 172/**173 * ttm_tt_destroy:174 *175 * @bdev: the ttm_device this object belongs to176 * @ttm: The struct ttm_tt.177 *178 * Unbind, unpopulate and destroy common struct ttm_tt.179 */180void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm);181 182/**183 * ttm_tt_swapin:184 *185 * @ttm: The struct ttm_tt.186 *187 * Swap in a previously swap out ttm_tt.188 */189int ttm_tt_swapin(struct ttm_tt *ttm);190int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,191		   gfp_t gfp_flags);192 193/**194 * ttm_tt_populate - allocate pages for a ttm195 *196 * @bdev: the ttm_device this object belongs to197 * @ttm: Pointer to the ttm_tt structure198 * @ctx: operation context for populating the tt object.199 *200 * Calls the driver method to allocate pages for a ttm201 */202int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm,203		    struct ttm_operation_ctx *ctx);204 205/**206 * ttm_tt_unpopulate - free pages from a ttm207 *208 * @bdev: the ttm_device this object belongs to209 * @ttm: Pointer to the ttm_tt structure210 *211 * Calls the driver method to free all pages from a ttm212 */213void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);214 215/**216 * ttm_tt_mark_for_clear - Mark pages for clearing on populate.217 *218 * @ttm: Pointer to the ttm_tt structure219 *220 * Marks pages for clearing so that the next time the page vector is221 * populated, the pages will be cleared.222 */223static inline void ttm_tt_mark_for_clear(struct ttm_tt *ttm)224{225	ttm->page_flags |= TTM_TT_FLAG_ZERO_ALLOC;226}227 228void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages);229 230struct ttm_kmap_iter *ttm_kmap_iter_tt_init(struct ttm_kmap_iter_tt *iter_tt,231					    struct ttm_tt *tt);232unsigned long ttm_tt_pages_limit(void);233#if IS_ENABLED(CONFIG_AGP)234#include <linux/agp_backend.h>235 236/**237 * ttm_agp_tt_create238 *239 * @bo: Buffer object we allocate the ttm for.240 * @bridge: The agp bridge this device is sitting on.241 * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.242 *243 *244 * Create a TTM backend that uses the indicated AGP bridge as an aperture245 * for TT memory. This function uses the linux agpgart interface to246 * bind and unbind memory backing a ttm_tt.247 */248struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,249				 struct agp_bridge_data *bridge,250				 uint32_t page_flags);251int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);252void ttm_agp_unbind(struct ttm_tt *ttm);253void ttm_agp_destroy(struct ttm_tt *ttm);254bool ttm_agp_is_bound(struct ttm_tt *ttm);255#endif256 257#endif258