15718399fSFrançois Tigeot /**************************************************************************
25718399fSFrançois Tigeot *
35718399fSFrançois Tigeot * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
45718399fSFrançois Tigeot * All Rights Reserved.
55718399fSFrançois Tigeot *
65718399fSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
75718399fSFrançois Tigeot * copy of this software and associated documentation files (the
85718399fSFrançois Tigeot * "Software"), to deal in the Software without restriction, including
95718399fSFrançois Tigeot * without limitation the rights to use, copy, modify, merge, publish,
105718399fSFrançois Tigeot * distribute, sub license, and/or sell copies of the Software, and to
115718399fSFrançois Tigeot * permit persons to whom the Software is furnished to do so, subject to
125718399fSFrançois Tigeot * the following conditions:
135718399fSFrançois Tigeot *
145718399fSFrançois Tigeot * The above copyright notice and this permission notice (including the
155718399fSFrançois Tigeot * next paragraph) shall be included in all copies or substantial portions
165718399fSFrançois Tigeot * of the Software.
175718399fSFrançois Tigeot *
185718399fSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
195718399fSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
205718399fSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
215718399fSFrançois Tigeot * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
225718399fSFrançois Tigeot * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
235718399fSFrançois Tigeot * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
245718399fSFrançois Tigeot * USE OR OTHER DEALINGS IN THE SOFTWARE.
255718399fSFrançois Tigeot *
265718399fSFrançois Tigeot **************************************************************************/
275718399fSFrançois Tigeot /*
285718399fSFrançois Tigeot * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
295718399fSFrançois Tigeot */
305718399fSFrançois Tigeot
310bece63dSImre Vadasz #define pr_fmt(fmt) "[TTM] " fmt
320bece63dSImre Vadasz
3343e748b9SFrançois Tigeot #include <linux/sched.h>
3443e748b9SFrançois Tigeot #include <linux/highmem.h>
3543e748b9SFrançois Tigeot #include <linux/pagemap.h>
3643e748b9SFrançois Tigeot #include <linux/shmem_fs.h>
3743e748b9SFrançois Tigeot #include <linux/file.h>
3843e748b9SFrançois Tigeot #include <linux/swap.h>
3943e748b9SFrançois Tigeot #include <linux/slab.h>
40c19c6249SFrançois Tigeot #include <linux/export.h>
41a85cb24fSFrançois Tigeot #include <drm/drm_cache.h>
42216f7a2cSFrançois Tigeot #include <drm/ttm/ttm_module.h>
43216f7a2cSFrançois Tigeot #include <drm/ttm/ttm_bo_driver.h>
44216f7a2cSFrançois Tigeot #include <drm/ttm/ttm_placement.h>
45216f7a2cSFrançois Tigeot #include <drm/ttm/ttm_page_alloc.h>
46*932d855eSSergey Zigachev #include <drm/ttm/ttm_set_memory.h>
47*932d855eSSergey Zigachev
48*932d855eSSergey Zigachev /**
49*932d855eSSergey Zigachev * Allocates a ttm structure for the given BO.
50*932d855eSSergey Zigachev */
ttm_tt_create(struct ttm_buffer_object * bo,bool zero_alloc)51*932d855eSSergey Zigachev int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
52*932d855eSSergey Zigachev {
53*932d855eSSergey Zigachev struct ttm_bo_device *bdev = bo->bdev;
54*932d855eSSergey Zigachev uint32_t page_flags = 0;
55*932d855eSSergey Zigachev
56*932d855eSSergey Zigachev reservation_object_assert_held(bo->resv);
57*932d855eSSergey Zigachev
58*932d855eSSergey Zigachev if (bdev->need_dma32)
59*932d855eSSergey Zigachev page_flags |= TTM_PAGE_FLAG_DMA32;
60*932d855eSSergey Zigachev
61*932d855eSSergey Zigachev if (bdev->no_retry)
62*932d855eSSergey Zigachev page_flags |= TTM_PAGE_FLAG_NO_RETRY;
63*932d855eSSergey Zigachev
64*932d855eSSergey Zigachev switch (bo->type) {
65*932d855eSSergey Zigachev case ttm_bo_type_device:
66*932d855eSSergey Zigachev if (zero_alloc)
67*932d855eSSergey Zigachev page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
68*932d855eSSergey Zigachev break;
69*932d855eSSergey Zigachev case ttm_bo_type_kernel:
70*932d855eSSergey Zigachev break;
71*932d855eSSergey Zigachev case ttm_bo_type_sg:
72*932d855eSSergey Zigachev page_flags |= TTM_PAGE_FLAG_SG;
73*932d855eSSergey Zigachev break;
74*932d855eSSergey Zigachev default:
75*932d855eSSergey Zigachev bo->ttm = NULL;
76*932d855eSSergey Zigachev pr_err("Illegal buffer object type\n");
77*932d855eSSergey Zigachev return -EINVAL;
78*932d855eSSergey Zigachev }
79*932d855eSSergey Zigachev
80*932d855eSSergey Zigachev bo->ttm = bdev->driver->ttm_tt_create(bo, page_flags);
81*932d855eSSergey Zigachev if (unlikely(bo->ttm == NULL))
82*932d855eSSergey Zigachev return -ENOMEM;
83*932d855eSSergey Zigachev
84*932d855eSSergey Zigachev return 0;
85*932d855eSSergey Zigachev }
865718399fSFrançois Tigeot
875718399fSFrançois Tigeot /**
885718399fSFrançois Tigeot * Allocates storage for pointers to the pages that back the ttm.
895718399fSFrançois Tigeot */
ttm_tt_alloc_page_directory(struct ttm_tt * ttm)90*932d855eSSergey Zigachev static int ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
915718399fSFrançois Tigeot {
923f2dd94aSFrançois Tigeot ttm->pages = kvmalloc_array(ttm->num_pages, sizeof(void*),
933f2dd94aSFrançois Tigeot GFP_KERNEL | __GFP_ZERO);
94*932d855eSSergey Zigachev if (!ttm->pages)
95*932d855eSSergey Zigachev return -ENOMEM;
96*932d855eSSergey Zigachev return 0;
975718399fSFrançois Tigeot }
985718399fSFrançois Tigeot
ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt * ttm)99*932d855eSSergey Zigachev static int ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
1005718399fSFrançois Tigeot {
1013f2dd94aSFrançois Tigeot ttm->ttm.pages = kvmalloc_array(ttm->ttm.num_pages,
1021cfef1a5SFrançois Tigeot sizeof(*ttm->ttm.pages) +
1033f2dd94aSFrançois Tigeot sizeof(*ttm->dma_address),
1043f2dd94aSFrançois Tigeot GFP_KERNEL | __GFP_ZERO);
105*932d855eSSergey Zigachev if (!ttm->ttm.pages)
106*932d855eSSergey Zigachev return -ENOMEM;
1071e12ee3bSFrançois Tigeot ttm->dma_address = (void *) (ttm->ttm.pages + ttm->ttm.num_pages);
108*932d855eSSergey Zigachev return 0;
1095718399fSFrançois Tigeot }
1105718399fSFrançois Tigeot
ttm_sg_tt_alloc_page_directory(struct ttm_dma_tt * ttm)111*932d855eSSergey Zigachev static int ttm_sg_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
112*932d855eSSergey Zigachev {
113*932d855eSSergey Zigachev ttm->dma_address = kvmalloc_array(ttm->ttm.num_pages,
114*932d855eSSergey Zigachev sizeof(*ttm->dma_address),
115*932d855eSSergey Zigachev GFP_KERNEL | __GFP_ZERO);
116*932d855eSSergey Zigachev if (!ttm->dma_address)
117*932d855eSSergey Zigachev return -ENOMEM;
118*932d855eSSergey Zigachev return 0;
119*932d855eSSergey Zigachev }
120*932d855eSSergey Zigachev
ttm_tt_set_page_caching(struct page * p,enum ttm_caching_state c_old,enum ttm_caching_state c_new)121*932d855eSSergey Zigachev static int ttm_tt_set_page_caching(struct page *p,
1225718399fSFrançois Tigeot enum ttm_caching_state c_old,
1235718399fSFrançois Tigeot enum ttm_caching_state c_new)
1245718399fSFrançois Tigeot {
12543e748b9SFrançois Tigeot int ret = 0;
1265718399fSFrançois Tigeot
1275718399fSFrançois Tigeot #if 0
12843e748b9SFrançois Tigeot if (PageHighMem(p))
12943e748b9SFrançois Tigeot return 0;
13043e748b9SFrançois Tigeot #endif
13143e748b9SFrançois Tigeot
1325718399fSFrançois Tigeot if (c_old != tt_cached) {
1335718399fSFrançois Tigeot /* p isn't in the default caching state, set it to
1345718399fSFrançois Tigeot * writeback first to free its current memtype. */
13543e748b9SFrançois Tigeot
136*932d855eSSergey Zigachev ret = ttm_set_pages_wb(p, 1);
13743e748b9SFrançois Tigeot if (ret)
13843e748b9SFrançois Tigeot return ret;
1395718399fSFrançois Tigeot }
1405718399fSFrançois Tigeot
1415718399fSFrançois Tigeot if (c_new == tt_wc)
142f0bba3d1SFrançois Tigeot pmap_page_set_memattr((struct vm_page *)p, VM_MEMATTR_WRITE_COMBINING);
1435718399fSFrançois Tigeot else if (c_new == tt_uncached)
144*932d855eSSergey Zigachev ret = ttm_set_pages_uc(p, 1);
1455718399fSFrançois Tigeot
146a85cb24fSFrançois Tigeot return ret;
1475718399fSFrançois Tigeot }
1485718399fSFrançois Tigeot
1495718399fSFrançois Tigeot /*
1505718399fSFrançois Tigeot * Change caching policy for the linear kernel map
1515718399fSFrançois Tigeot * for range of pages in a ttm.
1525718399fSFrançois Tigeot */
1535718399fSFrançois Tigeot
ttm_tt_set_caching(struct ttm_tt * ttm,enum ttm_caching_state c_state)1545718399fSFrançois Tigeot static int ttm_tt_set_caching(struct ttm_tt *ttm,
1555718399fSFrançois Tigeot enum ttm_caching_state c_state)
1565718399fSFrançois Tigeot {
1575718399fSFrançois Tigeot int i, j;
158f0bba3d1SFrançois Tigeot struct page *cur_page;
1595718399fSFrançois Tigeot int ret;
1605718399fSFrançois Tigeot
1615718399fSFrançois Tigeot if (ttm->caching_state == c_state)
1625718399fSFrançois Tigeot return 0;
1635718399fSFrançois Tigeot
1645718399fSFrançois Tigeot if (ttm->state == tt_unpopulated) {
1655718399fSFrançois Tigeot /* Change caching but don't populate */
1665718399fSFrançois Tigeot ttm->caching_state = c_state;
1675718399fSFrançois Tigeot return 0;
1685718399fSFrançois Tigeot }
1695718399fSFrançois Tigeot
1705718399fSFrançois Tigeot if (ttm->caching_state == tt_cached)
1715718399fSFrançois Tigeot drm_clflush_pages(ttm->pages, ttm->num_pages);
1725718399fSFrançois Tigeot
1735718399fSFrançois Tigeot for (i = 0; i < ttm->num_pages; ++i) {
1745718399fSFrançois Tigeot cur_page = ttm->pages[i];
1755718399fSFrançois Tigeot if (likely(cur_page != NULL)) {
1765718399fSFrançois Tigeot ret = ttm_tt_set_page_caching(cur_page,
1775718399fSFrançois Tigeot ttm->caching_state,
1785718399fSFrançois Tigeot c_state);
1795718399fSFrançois Tigeot if (unlikely(ret != 0))
1805718399fSFrançois Tigeot goto out_err;
1815718399fSFrançois Tigeot }
1825718399fSFrançois Tigeot }
1835718399fSFrançois Tigeot
1845718399fSFrançois Tigeot ttm->caching_state = c_state;
1855718399fSFrançois Tigeot
1865718399fSFrançois Tigeot return 0;
1875718399fSFrançois Tigeot
1885718399fSFrançois Tigeot out_err:
1895718399fSFrançois Tigeot for (j = 0; j < i; ++j) {
1905718399fSFrançois Tigeot cur_page = ttm->pages[j];
191c19c6249SFrançois Tigeot if (likely(cur_page != NULL)) {
1925718399fSFrançois Tigeot (void)ttm_tt_set_page_caching(cur_page, c_state,
1935718399fSFrançois Tigeot ttm->caching_state);
1945718399fSFrançois Tigeot }
1955718399fSFrançois Tigeot }
1965718399fSFrançois Tigeot
1975718399fSFrançois Tigeot return ret;
1985718399fSFrançois Tigeot }
1995718399fSFrançois Tigeot
ttm_tt_set_placement_caching(struct ttm_tt * ttm,uint32_t placement)2005718399fSFrançois Tigeot int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
2015718399fSFrançois Tigeot {
2025718399fSFrançois Tigeot enum ttm_caching_state state;
2035718399fSFrançois Tigeot
2045718399fSFrançois Tigeot if (placement & TTM_PL_FLAG_WC)
2055718399fSFrançois Tigeot state = tt_wc;
2065718399fSFrançois Tigeot else if (placement & TTM_PL_FLAG_UNCACHED)
2075718399fSFrançois Tigeot state = tt_uncached;
2085718399fSFrançois Tigeot else
2095718399fSFrançois Tigeot state = tt_cached;
2105718399fSFrançois Tigeot
2115718399fSFrançois Tigeot return ttm_tt_set_caching(ttm, state);
2125718399fSFrançois Tigeot }
213c19c6249SFrançois Tigeot EXPORT_SYMBOL(ttm_tt_set_placement_caching);
2145718399fSFrançois Tigeot
ttm_tt_destroy(struct ttm_tt * ttm)2155718399fSFrançois Tigeot void ttm_tt_destroy(struct ttm_tt *ttm)
2165718399fSFrançois Tigeot {
2171dedbd3bSFrançois Tigeot if (ttm == NULL)
2185718399fSFrançois Tigeot return;
2195718399fSFrançois Tigeot
2205718399fSFrançois Tigeot ttm_tt_unbind(ttm);
2215718399fSFrançois Tigeot
22243e748b9SFrançois Tigeot if (ttm->state == tt_unbound)
22343e748b9SFrançois Tigeot ttm_tt_unpopulate(ttm);
2245718399fSFrançois Tigeot
2255718399fSFrançois Tigeot if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
2265718399fSFrançois Tigeot ttm->swap_storage)
2275718399fSFrançois Tigeot vm_object_deallocate(ttm->swap_storage);
2285718399fSFrançois Tigeot
2295718399fSFrançois Tigeot ttm->swap_storage = NULL;
2305718399fSFrançois Tigeot ttm->func->destroy(ttm);
2315718399fSFrançois Tigeot }
2325718399fSFrançois Tigeot
233*932d855eSSergey Zigachev static
ttm_tt_init_fields(struct ttm_tt * ttm,struct ttm_buffer_object * bo,uint32_t page_flags)234*932d855eSSergey Zigachev void ttm_tt_init_fields(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
235*932d855eSSergey Zigachev uint32_t page_flags)
2365718399fSFrançois Tigeot {
237*932d855eSSergey Zigachev ttm->bdev = bo->bdev;
238*932d855eSSergey Zigachev ttm->num_pages = bo->num_pages;
2395718399fSFrançois Tigeot ttm->caching_state = tt_cached;
2405718399fSFrançois Tigeot ttm->page_flags = page_flags;
2415718399fSFrançois Tigeot ttm->state = tt_unpopulated;
2425718399fSFrançois Tigeot ttm->swap_storage = NULL;
243*932d855eSSergey Zigachev ttm->sg = bo->sg;
244*932d855eSSergey Zigachev }
2455718399fSFrançois Tigeot
ttm_tt_init(struct ttm_tt * ttm,struct ttm_buffer_object * bo,uint32_t page_flags)246*932d855eSSergey Zigachev int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
247*932d855eSSergey Zigachev uint32_t page_flags)
248*932d855eSSergey Zigachev {
249*932d855eSSergey Zigachev ttm_tt_init_fields(ttm, bo, page_flags);
250*932d855eSSergey Zigachev
251*932d855eSSergey Zigachev if (ttm_tt_alloc_page_directory(ttm)) {
2520bece63dSImre Vadasz pr_err("Failed allocating page table\n");
2535718399fSFrançois Tigeot return -ENOMEM;
2545718399fSFrançois Tigeot }
2555718399fSFrançois Tigeot return 0;
2565718399fSFrançois Tigeot }
257c19c6249SFrançois Tigeot EXPORT_SYMBOL(ttm_tt_init);
2585718399fSFrançois Tigeot
ttm_tt_fini(struct ttm_tt * ttm)2595718399fSFrançois Tigeot void ttm_tt_fini(struct ttm_tt *ttm)
2605718399fSFrançois Tigeot {
2613f2dd94aSFrançois Tigeot kvfree(ttm->pages);
2625718399fSFrançois Tigeot ttm->pages = NULL;
2635718399fSFrançois Tigeot }
264c19c6249SFrançois Tigeot EXPORT_SYMBOL(ttm_tt_fini);
2655718399fSFrançois Tigeot
ttm_dma_tt_init(struct ttm_dma_tt * ttm_dma,struct ttm_buffer_object * bo,uint32_t page_flags)266*932d855eSSergey Zigachev int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
267*932d855eSSergey Zigachev uint32_t page_flags)
2685718399fSFrançois Tigeot {
2695718399fSFrançois Tigeot struct ttm_tt *ttm = &ttm_dma->ttm;
2705718399fSFrançois Tigeot
271*932d855eSSergey Zigachev ttm_tt_init_fields(ttm, bo, page_flags);
2725718399fSFrançois Tigeot
2735718399fSFrançois Tigeot INIT_LIST_HEAD(&ttm_dma->pages_list);
274*932d855eSSergey Zigachev if (ttm_dma_tt_alloc_page_directory(ttm_dma)) {
2750bece63dSImre Vadasz pr_err("Failed allocating page table\n");
2765718399fSFrançois Tigeot return -ENOMEM;
2775718399fSFrançois Tigeot }
2785718399fSFrançois Tigeot return 0;
2795718399fSFrançois Tigeot }
280c19c6249SFrançois Tigeot EXPORT_SYMBOL(ttm_dma_tt_init);
2815718399fSFrançois Tigeot
ttm_sg_tt_init(struct ttm_dma_tt * ttm_dma,struct ttm_buffer_object * bo,uint32_t page_flags)282*932d855eSSergey Zigachev int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
283*932d855eSSergey Zigachev uint32_t page_flags)
284*932d855eSSergey Zigachev {
285*932d855eSSergey Zigachev struct ttm_tt *ttm = &ttm_dma->ttm;
286*932d855eSSergey Zigachev int ret;
287*932d855eSSergey Zigachev
288*932d855eSSergey Zigachev ttm_tt_init_fields(ttm, bo, page_flags);
289*932d855eSSergey Zigachev
290*932d855eSSergey Zigachev INIT_LIST_HEAD(&ttm_dma->pages_list);
291*932d855eSSergey Zigachev if (page_flags & TTM_PAGE_FLAG_SG)
292*932d855eSSergey Zigachev ret = ttm_sg_tt_alloc_page_directory(ttm_dma);
293*932d855eSSergey Zigachev else
294*932d855eSSergey Zigachev ret = ttm_dma_tt_alloc_page_directory(ttm_dma);
295*932d855eSSergey Zigachev if (ret) {
296*932d855eSSergey Zigachev pr_err("Failed allocating page table\n");
297*932d855eSSergey Zigachev return -ENOMEM;
298*932d855eSSergey Zigachev }
299*932d855eSSergey Zigachev return 0;
300*932d855eSSergey Zigachev }
301*932d855eSSergey Zigachev EXPORT_SYMBOL(ttm_sg_tt_init);
302*932d855eSSergey Zigachev
ttm_dma_tt_fini(struct ttm_dma_tt * ttm_dma)3035718399fSFrançois Tigeot void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
3045718399fSFrançois Tigeot {
3055718399fSFrançois Tigeot struct ttm_tt *ttm = &ttm_dma->ttm;
3065718399fSFrançois Tigeot
307*932d855eSSergey Zigachev if (ttm->pages)
3083f2dd94aSFrançois Tigeot kvfree(ttm->pages);
309*932d855eSSergey Zigachev else
310*932d855eSSergey Zigachev kvfree(ttm_dma->dma_address);
3115718399fSFrançois Tigeot ttm->pages = NULL;
3125718399fSFrançois Tigeot ttm_dma->dma_address = NULL;
3135718399fSFrançois Tigeot }
314c19c6249SFrançois Tigeot EXPORT_SYMBOL(ttm_dma_tt_fini);
3155718399fSFrançois Tigeot
ttm_tt_unbind(struct ttm_tt * ttm)3165718399fSFrançois Tigeot void ttm_tt_unbind(struct ttm_tt *ttm)
3175718399fSFrançois Tigeot {
3185718399fSFrançois Tigeot int ret;
3195718399fSFrançois Tigeot
3205718399fSFrançois Tigeot if (ttm->state == tt_bound) {
3215718399fSFrançois Tigeot ret = ttm->func->unbind(ttm);
322c19c6249SFrançois Tigeot BUG_ON(ret);
3235718399fSFrançois Tigeot ttm->state = tt_unbound;
3245718399fSFrançois Tigeot }
3255718399fSFrançois Tigeot }
3265718399fSFrançois Tigeot
ttm_tt_bind(struct ttm_tt * ttm,struct ttm_mem_reg * bo_mem,struct ttm_operation_ctx * ctx)327*932d855eSSergey Zigachev int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
328*932d855eSSergey Zigachev struct ttm_operation_ctx *ctx)
3295718399fSFrançois Tigeot {
3305718399fSFrançois Tigeot int ret = 0;
3315718399fSFrançois Tigeot
3325718399fSFrançois Tigeot if (!ttm)
3335718399fSFrançois Tigeot return -EINVAL;
3345718399fSFrançois Tigeot
3355718399fSFrançois Tigeot if (ttm->state == tt_bound)
3365718399fSFrançois Tigeot return 0;
3375718399fSFrançois Tigeot
338*932d855eSSergey Zigachev ret = ttm_tt_populate(ttm, ctx);
3395718399fSFrançois Tigeot if (ret)
3405718399fSFrançois Tigeot return ret;
3415718399fSFrançois Tigeot
3425718399fSFrançois Tigeot ret = ttm->func->bind(ttm, bo_mem);
3435718399fSFrançois Tigeot if (unlikely(ret != 0))
3445718399fSFrançois Tigeot return ret;
3455718399fSFrançois Tigeot
3465718399fSFrançois Tigeot ttm->state = tt_bound;
3475718399fSFrançois Tigeot
3485718399fSFrançois Tigeot return 0;
3495718399fSFrançois Tigeot }
350c19c6249SFrançois Tigeot EXPORT_SYMBOL(ttm_tt_bind);
3515718399fSFrançois Tigeot
ttm_tt_swapin(struct ttm_tt * ttm)3525718399fSFrançois Tigeot int ttm_tt_swapin(struct ttm_tt *ttm)
3535718399fSFrançois Tigeot {
354a85cb24fSFrançois Tigeot vm_object_t swap_storage;
35543e748b9SFrançois Tigeot struct page *from_page;
35643e748b9SFrançois Tigeot struct page *to_page;
35743e748b9SFrançois Tigeot int i;
35843e748b9SFrançois Tigeot int ret = -ENOMEM;
3595718399fSFrançois Tigeot
360a85cb24fSFrançois Tigeot swap_storage = ttm->swap_storage;
361a85cb24fSFrançois Tigeot BUG_ON(swap_storage == NULL);
3625718399fSFrançois Tigeot
363a85cb24fSFrançois Tigeot VM_OBJECT_LOCK(swap_storage);
364a85cb24fSFrançois Tigeot vm_object_pip_add(swap_storage, 1);
3655718399fSFrançois Tigeot for (i = 0; i < ttm->num_pages; ++i) {
366a85cb24fSFrançois Tigeot from_page = (struct page *)vm_page_grab(swap_storage, i, VM_ALLOC_NORMAL |
367f6201ebfSMatthew Dillon VM_ALLOC_RETRY);
36843e748b9SFrançois Tigeot if (((struct vm_page *)from_page)->valid != VM_PAGE_BITS_ALL) {
369a85cb24fSFrançois Tigeot if (vm_pager_has_page(swap_storage, i)) {
3702854a88cSMatthew Dillon if (vm_pager_get_page(swap_storage, i,
371a85cb24fSFrançois Tigeot (struct vm_page **)&from_page, 1) != VM_PAGER_OK) {
37243e748b9SFrançois Tigeot vm_page_free((struct vm_page *)from_page);
3735718399fSFrançois Tigeot ret = -EIO;
37443e748b9SFrançois Tigeot goto out_err;
3755718399fSFrançois Tigeot }
376f2f9e6ecSMatthew Dillon } else {
37743e748b9SFrançois Tigeot vm_page_zero_invalid((struct vm_page *)from_page, TRUE);
378f2f9e6ecSMatthew Dillon }
3795718399fSFrançois Tigeot }
38043e748b9SFrançois Tigeot to_page = ttm->pages[i];
3815718399fSFrançois Tigeot if (unlikely(to_page == NULL)) {
38243e748b9SFrançois Tigeot vm_page_wakeup((struct vm_page *)from_page);
38343e748b9SFrançois Tigeot goto out_err;
3845718399fSFrançois Tigeot }
38543e748b9SFrançois Tigeot
38643e748b9SFrançois Tigeot pmap_copy_page(VM_PAGE_TO_PHYS((struct vm_page *)from_page),
38743e748b9SFrançois Tigeot VM_PAGE_TO_PHYS((struct vm_page *)to_page));
38843e748b9SFrançois Tigeot vm_page_wakeup((struct vm_page *)from_page);
3895718399fSFrançois Tigeot }
390a85cb24fSFrançois Tigeot vm_object_pip_wakeup(swap_storage);
391a85cb24fSFrançois Tigeot VM_OBJECT_UNLOCK(swap_storage);
3925718399fSFrançois Tigeot
3935718399fSFrançois Tigeot if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
394a85cb24fSFrançois Tigeot vm_object_deallocate(swap_storage);
3955718399fSFrançois Tigeot ttm->swap_storage = NULL;
3965718399fSFrançois Tigeot ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
3975718399fSFrançois Tigeot
39843e748b9SFrançois Tigeot return 0;
39943e748b9SFrançois Tigeot out_err:
400a85cb24fSFrançois Tigeot vm_object_pip_wakeup(swap_storage);
401a85cb24fSFrançois Tigeot VM_OBJECT_UNLOCK(swap_storage);
402a85cb24fSFrançois Tigeot
40343e748b9SFrançois Tigeot return ret;
4045718399fSFrançois Tigeot }
4055718399fSFrançois Tigeot
ttm_tt_swapout(struct ttm_tt * ttm,vm_object_t persistent_swap_storage)4065718399fSFrançois Tigeot int ttm_tt_swapout(struct ttm_tt *ttm, vm_object_t persistent_swap_storage)
4075718399fSFrançois Tigeot {
4085718399fSFrançois Tigeot vm_object_t obj;
4095718399fSFrançois Tigeot vm_page_t from_page, to_page;
4105718399fSFrançois Tigeot int i;
4115718399fSFrançois Tigeot
412c19c6249SFrançois Tigeot BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
413c19c6249SFrançois Tigeot BUG_ON(ttm->caching_state != tt_cached);
4145718399fSFrançois Tigeot
415c19c6249SFrançois Tigeot if (!persistent_swap_storage) {
4165718399fSFrançois Tigeot obj = swap_pager_alloc(NULL,
4175718399fSFrançois Tigeot IDX_TO_OFF(ttm->num_pages), VM_PROT_DEFAULT, 0);
4185718399fSFrançois Tigeot if (obj == NULL) {
4190bece63dSImre Vadasz pr_err("Failed allocating swap storage\n");
4205718399fSFrançois Tigeot return (-ENOMEM);
4215718399fSFrançois Tigeot }
4225718399fSFrançois Tigeot } else
4235718399fSFrançois Tigeot obj = persistent_swap_storage;
4245718399fSFrançois Tigeot
42504b45e6fSzrj VM_OBJECT_LOCK(obj);
4265718399fSFrançois Tigeot vm_object_pip_add(obj, 1);
4275718399fSFrançois Tigeot for (i = 0; i < ttm->num_pages; ++i) {
428f0bba3d1SFrançois Tigeot from_page = (struct vm_page *)ttm->pages[i];
4295718399fSFrançois Tigeot if (unlikely(from_page == NULL))
4305718399fSFrançois Tigeot continue;
431f6201ebfSMatthew Dillon to_page = vm_page_grab(obj, i, VM_ALLOC_NORMAL |
432f6201ebfSMatthew Dillon VM_ALLOC_RETRY);
4335718399fSFrançois Tigeot pmap_copy_page(VM_PAGE_TO_PHYS(from_page),
4345718399fSFrançois Tigeot VM_PAGE_TO_PHYS(to_page));
4355718399fSFrançois Tigeot to_page->valid = VM_PAGE_BITS_ALL;
4366f486c69SFrançois Tigeot vm_page_dirty(to_page);
4375718399fSFrançois Tigeot vm_page_wakeup(to_page);
4385718399fSFrançois Tigeot }
4395718399fSFrançois Tigeot vm_object_pip_wakeup(obj);
44004b45e6fSzrj VM_OBJECT_UNLOCK(obj);
4415718399fSFrançois Tigeot
44243e748b9SFrançois Tigeot ttm_tt_unpopulate(ttm);
4435718399fSFrançois Tigeot ttm->swap_storage = obj;
4445718399fSFrançois Tigeot ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
445c19c6249SFrançois Tigeot if (persistent_swap_storage)
4465718399fSFrançois Tigeot ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
447c19c6249SFrançois Tigeot
448c19c6249SFrançois Tigeot return 0;
4495718399fSFrançois Tigeot }
45043e748b9SFrançois Tigeot
ttm_tt_add_mapping(struct ttm_tt * ttm)451*932d855eSSergey Zigachev static void ttm_tt_add_mapping(struct ttm_tt *ttm)
452*932d855eSSergey Zigachev {
453*932d855eSSergey Zigachev #if 0
454*932d855eSSergey Zigachev pgoff_t i;
455*932d855eSSergey Zigachev #endif
456*932d855eSSergey Zigachev
457*932d855eSSergey Zigachev if (ttm->page_flags & TTM_PAGE_FLAG_SG)
458*932d855eSSergey Zigachev return;
459*932d855eSSergey Zigachev #if 0
460*932d855eSSergey Zigachev for (i = 0; i < ttm->num_pages; ++i)
461*932d855eSSergey Zigachev ttm->pages[i]->mapping = ttm->bdev->dev_mapping;
462*932d855eSSergey Zigachev #endif
463*932d855eSSergey Zigachev }
464*932d855eSSergey Zigachev
ttm_tt_populate(struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)465*932d855eSSergey Zigachev int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
466*932d855eSSergey Zigachev {
467*932d855eSSergey Zigachev int ret;
468*932d855eSSergey Zigachev
469*932d855eSSergey Zigachev if (ttm->state != tt_unpopulated)
470*932d855eSSergey Zigachev return 0;
471*932d855eSSergey Zigachev
472*932d855eSSergey Zigachev if (ttm->bdev->driver->ttm_tt_populate)
473*932d855eSSergey Zigachev ret = ttm->bdev->driver->ttm_tt_populate(ttm, ctx);
474*932d855eSSergey Zigachev else
475*932d855eSSergey Zigachev ret = ttm_pool_populate(ttm, ctx);
476*932d855eSSergey Zigachev if (!ret)
477*932d855eSSergey Zigachev ttm_tt_add_mapping(ttm);
478*932d855eSSergey Zigachev return ret;
479*932d855eSSergey Zigachev }
480*932d855eSSergey Zigachev
ttm_tt_clear_mapping(struct ttm_tt * ttm)48143e748b9SFrançois Tigeot static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
48243e748b9SFrançois Tigeot {
48343e748b9SFrançois Tigeot #if 0
48443e748b9SFrançois Tigeot pgoff_t i;
48543e748b9SFrançois Tigeot struct page **page = ttm->pages;
48643e748b9SFrançois Tigeot
48743e748b9SFrançois Tigeot if (ttm->page_flags & TTM_PAGE_FLAG_SG)
48843e748b9SFrançois Tigeot return;
48943e748b9SFrançois Tigeot
49043e748b9SFrançois Tigeot for (i = 0; i < ttm->num_pages; ++i) {
49143e748b9SFrançois Tigeot (*page)->mapping = NULL;
49243e748b9SFrançois Tigeot (*page++)->index = 0;
49343e748b9SFrançois Tigeot }
49443e748b9SFrançois Tigeot #endif
49543e748b9SFrançois Tigeot }
49643e748b9SFrançois Tigeot
ttm_tt_unpopulate(struct ttm_tt * ttm)49743e748b9SFrançois Tigeot void ttm_tt_unpopulate(struct ttm_tt *ttm)
49843e748b9SFrançois Tigeot {
49943e748b9SFrançois Tigeot if (ttm->state == tt_unpopulated)
50043e748b9SFrançois Tigeot return;
50143e748b9SFrançois Tigeot
50243e748b9SFrançois Tigeot ttm_tt_clear_mapping(ttm);
503*932d855eSSergey Zigachev if (ttm->bdev->driver->ttm_tt_unpopulate)
50443e748b9SFrançois Tigeot ttm->bdev->driver->ttm_tt_unpopulate(ttm);
505*932d855eSSergey Zigachev else
506*932d855eSSergey Zigachev ttm_pool_unpopulate(ttm);
50743e748b9SFrançois Tigeot }
508