15718399fSFrançois Tigeot /* 25718399fSFrançois Tigeot * Copyright (c) Red Hat Inc. 35718399fSFrançois Tigeot 45718399fSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a 55718399fSFrançois Tigeot * copy of this software and associated documentation files (the "Software"), 65718399fSFrançois Tigeot * to deal in the Software without restriction, including without limitation 75718399fSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sub license, 85718399fSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the 95718399fSFrançois Tigeot * Software is furnished to do so, subject to the following conditions: 105718399fSFrançois Tigeot * 115718399fSFrançois Tigeot * The above copyright notice and this permission notice (including the 125718399fSFrançois Tigeot * next paragraph) shall be included in all copies or substantial portions 135718399fSFrançois Tigeot * of the Software. 145718399fSFrançois Tigeot * 155718399fSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 165718399fSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 175718399fSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 185718399fSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 195718399fSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 205718399fSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 215718399fSFrançois Tigeot * DEALINGS IN THE SOFTWARE. 225718399fSFrançois Tigeot * 235718399fSFrançois Tigeot * Authors: Dave Airlie <airlied@redhat.com> 245718399fSFrançois Tigeot * Jerome Glisse <jglisse@redhat.com> 255718399fSFrançois Tigeot * Pauli Nieminen <suokkos@gmail.com> 265718399fSFrançois Tigeot */ 275718399fSFrançois Tigeot /* 285718399fSFrançois Tigeot * Copyright (c) 2013 The FreeBSD Foundation 295718399fSFrançois Tigeot * All rights reserved. 305718399fSFrançois Tigeot * 315718399fSFrançois Tigeot * Portions of this software were developed by Konstantin Belousov 325718399fSFrançois Tigeot * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation. 335718399fSFrançois Tigeot */ 345718399fSFrançois Tigeot 355718399fSFrançois Tigeot /* simple list based uncached page pool 365718399fSFrançois Tigeot * - Pool collects resently freed pages for reuse 375718399fSFrançois Tigeot * - Use page->lru to keep a free list 385718399fSFrançois Tigeot * - doesn't track currently in use pages 395718399fSFrançois Tigeot */ 405718399fSFrançois Tigeot 410bece63dSImre Vadasz #define pr_fmt(fmt) "[TTM] " fmt 420bece63dSImre Vadasz 43*6af927c2SFrançois Tigeot #include <linux/list.h> 44*6af927c2SFrançois Tigeot #include <linux/spinlock.h> 45*6af927c2SFrançois Tigeot #include <linux/highmem.h> 46*6af927c2SFrançois Tigeot #include <linux/mm_types.h> 47*6af927c2SFrançois Tigeot #include <linux/module.h> 48*6af927c2SFrançois Tigeot #include <linux/mm.h> 49*6af927c2SFrançois Tigeot #include <linux/seq_file.h> /* for seq_printf */ 50*6af927c2SFrançois Tigeot #include <linux/dma-mapping.h> 515718399fSFrançois Tigeot 52*6af927c2SFrançois Tigeot #include <linux/atomic.h> 53*6af927c2SFrançois Tigeot 54216f7a2cSFrançois Tigeot #include <drm/ttm/ttm_bo_driver.h> 55216f7a2cSFrançois Tigeot #include <drm/ttm/ttm_page_alloc.h> 565718399fSFrançois Tigeot 57*6af927c2SFrançois Tigeot #include <sys/eventhandler.h> 58*6af927c2SFrançois Tigeot 595718399fSFrançois Tigeot #ifdef TTM_HAS_AGP 605718399fSFrançois Tigeot #include <asm/agp.h> 615718399fSFrançois Tigeot #endif 625718399fSFrançois Tigeot 63f0bba3d1SFrançois Tigeot #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *)) 645718399fSFrançois Tigeot #define SMALL_ALLOCATION 16 655718399fSFrançois Tigeot #define FREE_ALL_PAGES (~0U) 665718399fSFrançois Tigeot /* times are in msecs */ 675718399fSFrançois Tigeot #define PAGE_FREE_INTERVAL 1000 685718399fSFrançois Tigeot 695718399fSFrançois Tigeot /** 705718399fSFrançois Tigeot * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages. 715718399fSFrançois Tigeot * 725718399fSFrançois Tigeot * @lock: Protects the shared pool from concurrnet access. Must be used with 735718399fSFrançois Tigeot * irqsave/irqrestore variants because pool allocator maybe called from 745718399fSFrançois Tigeot * delayed work. 755718399fSFrançois Tigeot * @fill_lock: Prevent concurrent calls to fill. 765718399fSFrançois Tigeot * @list: Pool of free uc/wc pages for fast reuse. 775718399fSFrançois Tigeot * @gfp_flags: Flags to pass for alloc_page. 785718399fSFrançois Tigeot * @npages: Number of pages in pool. 795718399fSFrançois Tigeot */ 805718399fSFrançois Tigeot struct ttm_page_pool { 815718399fSFrançois Tigeot struct lock lock; 825718399fSFrançois Tigeot bool fill_lock; 835718399fSFrançois Tigeot struct pglist list; 84*6af927c2SFrançois Tigeot gfp_t gfp_flags; 855718399fSFrançois Tigeot unsigned npages; 865718399fSFrançois Tigeot char *name; 875718399fSFrançois Tigeot unsigned long nfrees; 885718399fSFrançois Tigeot unsigned long nrefills; 895718399fSFrançois Tigeot }; 905718399fSFrançois Tigeot 915718399fSFrançois Tigeot /** 925718399fSFrançois Tigeot * Limits for the pool. They are handled without locks because only place where 935718399fSFrançois Tigeot * they may change is in sysfs store. They won't have immediate effect anyway 945718399fSFrançois Tigeot * so forcing serialization to access them is pointless. 955718399fSFrançois Tigeot */ 965718399fSFrançois Tigeot 975718399fSFrançois Tigeot struct ttm_pool_opts { 985718399fSFrançois Tigeot unsigned alloc_size; 995718399fSFrançois Tigeot unsigned max_size; 1005718399fSFrançois Tigeot unsigned small; 1015718399fSFrançois Tigeot }; 1025718399fSFrançois Tigeot 1035718399fSFrançois Tigeot #define NUM_POOLS 4 1045718399fSFrançois Tigeot 1055718399fSFrançois Tigeot /** 1065718399fSFrançois Tigeot * struct ttm_pool_manager - Holds memory pools for fst allocation 1075718399fSFrançois Tigeot * 1085718399fSFrançois Tigeot * Manager is read only object for pool code so it doesn't need locking. 1095718399fSFrançois Tigeot * 1105718399fSFrançois Tigeot * @free_interval: minimum number of jiffies between freeing pages from pool. 1115718399fSFrançois Tigeot * @page_alloc_inited: reference counting for pool allocation. 1125718399fSFrançois Tigeot * @work: Work that is used to shrink the pool. Work is only run when there is 1135718399fSFrançois Tigeot * some pages to free. 1145718399fSFrançois Tigeot * @small_allocation: Limit in number of pages what is small allocation. 1155718399fSFrançois Tigeot * 1165718399fSFrançois Tigeot * @pools: All pool objects in use. 1175718399fSFrançois Tigeot **/ 1185718399fSFrançois Tigeot struct ttm_pool_manager { 1193a2096e8SFrançois Tigeot struct kobject kobj; 1205718399fSFrançois Tigeot eventhandler_tag lowmem_handler; 1215718399fSFrançois Tigeot struct ttm_pool_opts options; 1225718399fSFrançois Tigeot 1235718399fSFrançois Tigeot union { 124*6af927c2SFrançois Tigeot struct ttm_page_pool pools[NUM_POOLS]; 125*6af927c2SFrançois Tigeot struct { 126*6af927c2SFrançois Tigeot struct ttm_page_pool wc_pool; 127*6af927c2SFrançois Tigeot struct ttm_page_pool uc_pool; 128*6af927c2SFrançois Tigeot struct ttm_page_pool wc_pool_dma32; 129*6af927c2SFrançois Tigeot struct ttm_page_pool uc_pool_dma32; 1305718399fSFrançois Tigeot } ; 131*6af927c2SFrançois Tigeot }; 132*6af927c2SFrançois Tigeot }; 1335718399fSFrançois Tigeot 1343a2096e8SFrançois Tigeot static struct attribute ttm_page_pool_max = { 1353a2096e8SFrançois Tigeot .name = "pool_max_size", 1363a2096e8SFrançois Tigeot .mode = S_IRUGO | S_IWUSR 1373a2096e8SFrançois Tigeot }; 1383a2096e8SFrançois Tigeot static struct attribute ttm_page_pool_small = { 1393a2096e8SFrançois Tigeot .name = "pool_small_allocation", 1403a2096e8SFrançois Tigeot .mode = S_IRUGO | S_IWUSR 1413a2096e8SFrançois Tigeot }; 1423a2096e8SFrançois Tigeot static struct attribute ttm_page_pool_alloc_size = { 1433a2096e8SFrançois Tigeot .name = "pool_allocation_size", 1443a2096e8SFrançois Tigeot .mode = S_IRUGO | S_IWUSR 1453a2096e8SFrançois Tigeot }; 1463a2096e8SFrançois Tigeot 1473a2096e8SFrançois Tigeot static struct attribute *ttm_pool_attrs[] = { 1483a2096e8SFrançois Tigeot &ttm_page_pool_max, 1493a2096e8SFrançois Tigeot &ttm_page_pool_small, 1503a2096e8SFrançois Tigeot &ttm_page_pool_alloc_size, 1513a2096e8SFrançois Tigeot NULL 1523a2096e8SFrançois Tigeot }; 1533a2096e8SFrançois Tigeot 1543a2096e8SFrançois Tigeot static void ttm_pool_kobj_release(struct kobject *kobj) 1555718399fSFrançois Tigeot { 1563a2096e8SFrançois Tigeot struct ttm_pool_manager *m = 1573a2096e8SFrançois Tigeot container_of(kobj, struct ttm_pool_manager, kobj); 158175896dfSzrj kfree(m); 1595718399fSFrançois Tigeot } 1605718399fSFrançois Tigeot 1613a2096e8SFrançois Tigeot static ssize_t ttm_pool_store(struct kobject *kobj, 1625718399fSFrançois Tigeot struct attribute *attr, const char *buffer, size_t size) 1635718399fSFrançois Tigeot { 1643a2096e8SFrançois Tigeot struct ttm_pool_manager *m = 1653a2096e8SFrançois Tigeot container_of(kobj, struct ttm_pool_manager, kobj); 1665718399fSFrançois Tigeot int chars; 1675718399fSFrançois Tigeot unsigned val; 1683a2096e8SFrançois Tigeot chars = ksscanf(buffer, "%u", &val); 1695718399fSFrançois Tigeot if (chars == 0) 1705718399fSFrançois Tigeot return size; 1715718399fSFrançois Tigeot 1725718399fSFrançois Tigeot /* Convert kb to number of pages */ 1735718399fSFrançois Tigeot val = val / (PAGE_SIZE >> 10); 1745718399fSFrançois Tigeot 1755718399fSFrançois Tigeot if (attr == &ttm_page_pool_max) 1765718399fSFrançois Tigeot m->options.max_size = val; 1775718399fSFrançois Tigeot else if (attr == &ttm_page_pool_small) 1785718399fSFrançois Tigeot m->options.small = val; 1795718399fSFrançois Tigeot else if (attr == &ttm_page_pool_alloc_size) { 1805718399fSFrançois Tigeot if (val > NUM_PAGES_TO_ALLOC*8) { 1815718399fSFrançois Tigeot pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n", 1825718399fSFrançois Tigeot NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7), 1835718399fSFrançois Tigeot NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10)); 1845718399fSFrançois Tigeot return size; 1855718399fSFrançois Tigeot } else if (val > NUM_PAGES_TO_ALLOC) { 1865718399fSFrançois Tigeot pr_warn("Setting allocation size to larger than %lu is not recommended\n", 1875718399fSFrançois Tigeot NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10)); 1885718399fSFrançois Tigeot } 1895718399fSFrançois Tigeot m->options.alloc_size = val; 1905718399fSFrançois Tigeot } 1915718399fSFrançois Tigeot 1925718399fSFrançois Tigeot return size; 1935718399fSFrançois Tigeot } 1945718399fSFrançois Tigeot 1953a2096e8SFrançois Tigeot static ssize_t ttm_pool_show(struct kobject *kobj, 1965718399fSFrançois Tigeot struct attribute *attr, char *buffer) 1975718399fSFrançois Tigeot { 1983a2096e8SFrançois Tigeot struct ttm_pool_manager *m = 1993a2096e8SFrançois Tigeot container_of(kobj, struct ttm_pool_manager, kobj); 2005718399fSFrançois Tigeot unsigned val = 0; 2015718399fSFrançois Tigeot 2025718399fSFrançois Tigeot if (attr == &ttm_page_pool_max) 2035718399fSFrançois Tigeot val = m->options.max_size; 2045718399fSFrançois Tigeot else if (attr == &ttm_page_pool_small) 2055718399fSFrançois Tigeot val = m->options.small; 2065718399fSFrançois Tigeot else if (attr == &ttm_page_pool_alloc_size) 2075718399fSFrançois Tigeot val = m->options.alloc_size; 2085718399fSFrançois Tigeot 2095718399fSFrançois Tigeot val = val * (PAGE_SIZE >> 10); 2105718399fSFrançois Tigeot 2113a2096e8SFrançois Tigeot return ksnprintf(buffer, PAGE_SIZE, "%u\n", val); 2125718399fSFrançois Tigeot } 2133a2096e8SFrançois Tigeot 2143a2096e8SFrançois Tigeot static const struct sysfs_ops ttm_pool_sysfs_ops = { 2153a2096e8SFrançois Tigeot .show = &ttm_pool_show, 2163a2096e8SFrançois Tigeot .store = &ttm_pool_store, 2173a2096e8SFrançois Tigeot }; 2183a2096e8SFrançois Tigeot 2193a2096e8SFrançois Tigeot static struct kobj_type ttm_pool_kobj_type = { 2203a2096e8SFrançois Tigeot .release = &ttm_pool_kobj_release, 2213a2096e8SFrançois Tigeot .sysfs_ops = &ttm_pool_sysfs_ops, 2223a2096e8SFrançois Tigeot .default_attrs = ttm_pool_attrs, 2233a2096e8SFrançois Tigeot }; 2245718399fSFrançois Tigeot 2255718399fSFrançois Tigeot static struct ttm_pool_manager *_manager; 2265718399fSFrançois Tigeot 22751933d89SFrançois Tigeot #ifndef CONFIG_X86 228f0bba3d1SFrançois Tigeot static int set_pages_array_wb(struct page **pages, int addrinarray) 2295718399fSFrançois Tigeot { 230*6af927c2SFrançois Tigeot #ifdef TTM_HAS_AGP 2315718399fSFrançois Tigeot int i; 2325718399fSFrançois Tigeot 233*6af927c2SFrançois Tigeot for (i = 0; i < addrinarray; i++) 234f0bba3d1SFrançois Tigeot unmap_page_from_agp(pages[i]); 2355718399fSFrançois Tigeot #endif 2365718399fSFrançois Tigeot return 0; 2375718399fSFrançois Tigeot } 2385718399fSFrançois Tigeot 239f0bba3d1SFrançois Tigeot static int set_pages_array_wc(struct page **pages, int addrinarray) 2405718399fSFrançois Tigeot { 241*6af927c2SFrançois Tigeot #ifdef TTM_HAS_AGP 2425718399fSFrançois Tigeot int i; 2435718399fSFrançois Tigeot 244*6af927c2SFrançois Tigeot for (i = 0; i < addrinarray; i++) 2455718399fSFrançois Tigeot map_page_into_agp(pages[i]); 2465718399fSFrançois Tigeot #endif 2475718399fSFrançois Tigeot return 0; 2485718399fSFrançois Tigeot } 2495718399fSFrançois Tigeot 250f0bba3d1SFrançois Tigeot static int set_pages_array_uc(struct page **pages, int addrinarray) 2515718399fSFrançois Tigeot { 252*6af927c2SFrançois Tigeot #ifdef TTM_HAS_AGP 2535718399fSFrançois Tigeot int i; 2545718399fSFrançois Tigeot 255*6af927c2SFrançois Tigeot for (i = 0; i < addrinarray; i++) 2565718399fSFrançois Tigeot map_page_into_agp(pages[i]); 2575718399fSFrançois Tigeot #endif 2585718399fSFrançois Tigeot return 0; 2595718399fSFrançois Tigeot } 26051933d89SFrançois Tigeot #endif 2615718399fSFrançois Tigeot 2625718399fSFrançois Tigeot /** 2635718399fSFrançois Tigeot * Select the right pool or requested caching state and ttm flags. */ 2645718399fSFrançois Tigeot static struct ttm_page_pool *ttm_get_pool(int flags, 2655718399fSFrançois Tigeot enum ttm_caching_state cstate) 2665718399fSFrançois Tigeot { 2675718399fSFrançois Tigeot int pool_index; 2685718399fSFrançois Tigeot 2695718399fSFrançois Tigeot if (cstate == tt_cached) 2705718399fSFrançois Tigeot return NULL; 2715718399fSFrançois Tigeot 2725718399fSFrançois Tigeot if (cstate == tt_wc) 2735718399fSFrançois Tigeot pool_index = 0x0; 2745718399fSFrançois Tigeot else 2755718399fSFrançois Tigeot pool_index = 0x1; 2765718399fSFrançois Tigeot 2775718399fSFrançois Tigeot if (flags & TTM_PAGE_FLAG_DMA32) 2785718399fSFrançois Tigeot pool_index |= 0x2; 2795718399fSFrançois Tigeot 2805718399fSFrançois Tigeot return &_manager->pools[pool_index]; 2815718399fSFrançois Tigeot } 2825718399fSFrançois Tigeot 2835718399fSFrançois Tigeot /* set memory back to wb and free the pages. */ 284f0bba3d1SFrançois Tigeot static void ttm_pages_put(struct page *pages[], unsigned npages) 2855718399fSFrançois Tigeot { 2865718399fSFrançois Tigeot unsigned i; 2875718399fSFrançois Tigeot if (set_pages_array_wb(pages, npages)) 2880bece63dSImre Vadasz pr_err("Failed to set %d pages to wb!\n", npages); 2895718399fSFrançois Tigeot for (i = 0; i < npages; ++i) 290e5c1d8f1SFrançois Tigeot __free_page(pages[i]); 2915718399fSFrançois Tigeot } 2925718399fSFrançois Tigeot 2935718399fSFrançois Tigeot static void ttm_pool_update_free_locked(struct ttm_page_pool *pool, 2945718399fSFrançois Tigeot unsigned freed_pages) 2955718399fSFrançois Tigeot { 2965718399fSFrançois Tigeot pool->npages -= freed_pages; 2975718399fSFrançois Tigeot pool->nfrees += freed_pages; 2985718399fSFrançois Tigeot } 2995718399fSFrançois Tigeot 3005718399fSFrançois Tigeot /** 3015718399fSFrançois Tigeot * Free pages from pool. 3025718399fSFrançois Tigeot * 3035718399fSFrançois Tigeot * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC 3045718399fSFrançois Tigeot * number of pages in one go. 3055718399fSFrançois Tigeot * 3065718399fSFrançois Tigeot * @pool: to free the pages from 3075718399fSFrançois Tigeot * @free_all: If set to true will free all pages in pool 3085718399fSFrançois Tigeot **/ 3095718399fSFrançois Tigeot static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free) 3105718399fSFrançois Tigeot { 311*6af927c2SFrançois Tigeot unsigned long irq_flags; 312*6af927c2SFrançois Tigeot struct vm_page *p, *p1; 313f0bba3d1SFrançois Tigeot struct page **pages_to_free; 3145718399fSFrançois Tigeot unsigned freed_pages = 0, 3155718399fSFrançois Tigeot npages_to_free = nr_free; 3166f486c69SFrançois Tigeot unsigned i; 3175718399fSFrançois Tigeot 3185718399fSFrançois Tigeot if (NUM_PAGES_TO_ALLOC < nr_free) 3195718399fSFrançois Tigeot npages_to_free = NUM_PAGES_TO_ALLOC; 3205718399fSFrançois Tigeot 321f0bba3d1SFrançois Tigeot pages_to_free = kmalloc(npages_to_free * sizeof(struct page *), 322*6af927c2SFrançois Tigeot M_DRM, M_WAITOK); 323*6af927c2SFrançois Tigeot if (!pages_to_free) { 324*6af927c2SFrançois Tigeot pr_err("Failed to allocate memory for pool free operation\n"); 325*6af927c2SFrançois Tigeot return 0; 326*6af927c2SFrançois Tigeot } 3275718399fSFrançois Tigeot 3285718399fSFrançois Tigeot restart: 329*6af927c2SFrançois Tigeot spin_lock_irqsave(&pool->lock, irq_flags); 3305718399fSFrançois Tigeot 3315718399fSFrançois Tigeot TAILQ_FOREACH_REVERSE_MUTABLE(p, &pool->list, pglist, pageq, p1) { 3325718399fSFrançois Tigeot if (freed_pages >= npages_to_free) 3335718399fSFrançois Tigeot break; 3345718399fSFrançois Tigeot 335f0bba3d1SFrançois Tigeot pages_to_free[freed_pages++] = (struct page *)p; 3365718399fSFrançois Tigeot /* We can only remove NUM_PAGES_TO_ALLOC at a time. */ 3375718399fSFrançois Tigeot if (freed_pages >= NUM_PAGES_TO_ALLOC) { 3385718399fSFrançois Tigeot /* remove range of pages from the pool */ 3396f486c69SFrançois Tigeot for (i = 0; i < freed_pages; i++) 340f0bba3d1SFrançois Tigeot TAILQ_REMOVE(&pool->list, (struct vm_page *)pages_to_free[i], pageq); 3415718399fSFrançois Tigeot 3425718399fSFrançois Tigeot ttm_pool_update_free_locked(pool, freed_pages); 3435718399fSFrançois Tigeot /** 3445718399fSFrançois Tigeot * Because changing page caching is costly 3455718399fSFrançois Tigeot * we unlock the pool to prevent stalling. 3465718399fSFrançois Tigeot */ 347*6af927c2SFrançois Tigeot spin_unlock_irqrestore(&pool->lock, irq_flags); 3485718399fSFrançois Tigeot 3495718399fSFrançois Tigeot ttm_pages_put(pages_to_free, freed_pages); 3505718399fSFrançois Tigeot if (likely(nr_free != FREE_ALL_PAGES)) 3515718399fSFrançois Tigeot nr_free -= freed_pages; 3525718399fSFrançois Tigeot 3535718399fSFrançois Tigeot if (NUM_PAGES_TO_ALLOC >= nr_free) 3545718399fSFrançois Tigeot npages_to_free = nr_free; 3555718399fSFrançois Tigeot else 3565718399fSFrançois Tigeot npages_to_free = NUM_PAGES_TO_ALLOC; 3575718399fSFrançois Tigeot 3585718399fSFrançois Tigeot freed_pages = 0; 3595718399fSFrançois Tigeot 3605718399fSFrançois Tigeot /* free all so restart the processing */ 3615718399fSFrançois Tigeot if (nr_free) 3625718399fSFrançois Tigeot goto restart; 3635718399fSFrançois Tigeot 3645718399fSFrançois Tigeot /* Not allowed to fall through or break because 3655718399fSFrançois Tigeot * following context is inside spinlock while we are 3665718399fSFrançois Tigeot * outside here. 3675718399fSFrançois Tigeot */ 3685718399fSFrançois Tigeot goto out; 3695718399fSFrançois Tigeot 3705718399fSFrançois Tigeot } 3715718399fSFrançois Tigeot } 3725718399fSFrançois Tigeot 3735718399fSFrançois Tigeot /* remove range of pages from the pool */ 3745718399fSFrançois Tigeot if (freed_pages) { 3756f486c69SFrançois Tigeot for (i = 0; i < freed_pages; i++) 376f0bba3d1SFrançois Tigeot TAILQ_REMOVE(&pool->list, (struct vm_page *)pages_to_free[i], pageq); 3775718399fSFrançois Tigeot 3785718399fSFrançois Tigeot ttm_pool_update_free_locked(pool, freed_pages); 3795718399fSFrançois Tigeot nr_free -= freed_pages; 3805718399fSFrançois Tigeot } 3815718399fSFrançois Tigeot 382*6af927c2SFrançois Tigeot spin_unlock_irqrestore(&pool->lock, irq_flags); 3835718399fSFrançois Tigeot 3845718399fSFrançois Tigeot if (freed_pages) 3855718399fSFrançois Tigeot ttm_pages_put(pages_to_free, freed_pages); 3865718399fSFrançois Tigeot out: 387*6af927c2SFrançois Tigeot kfree(pages_to_free); 3885718399fSFrançois Tigeot return nr_free; 3895718399fSFrançois Tigeot } 3905718399fSFrançois Tigeot 3915718399fSFrançois Tigeot /* Get good estimation how many pages are free in pools */ 3925718399fSFrançois Tigeot static int ttm_pool_get_num_unused_pages(void) 3935718399fSFrançois Tigeot { 3945718399fSFrançois Tigeot unsigned i; 3955718399fSFrançois Tigeot int total = 0; 3965718399fSFrançois Tigeot for (i = 0; i < NUM_POOLS; ++i) 3975718399fSFrançois Tigeot total += _manager->pools[i].npages; 3985718399fSFrançois Tigeot 3995718399fSFrançois Tigeot return total; 4005718399fSFrançois Tigeot } 4015718399fSFrançois Tigeot 4025718399fSFrançois Tigeot /** 4035718399fSFrançois Tigeot * Callback for mm to request pool to reduce number of page held. 4045718399fSFrançois Tigeot */ 4055718399fSFrançois Tigeot static int ttm_pool_mm_shrink(void *arg) 4065718399fSFrançois Tigeot { 4075718399fSFrançois Tigeot static unsigned int start_pool = 0; 4085718399fSFrançois Tigeot unsigned i; 4095718399fSFrançois Tigeot unsigned pool_offset = atomic_fetchadd_int(&start_pool, 1); 4105718399fSFrançois Tigeot struct ttm_page_pool *pool; 4115718399fSFrançois Tigeot int shrink_pages = 100; /* XXXKIB */ 4125718399fSFrançois Tigeot 4135718399fSFrançois Tigeot pool_offset = pool_offset % NUM_POOLS; 4145718399fSFrançois Tigeot /* select start pool in round robin fashion */ 4155718399fSFrançois Tigeot for (i = 0; i < NUM_POOLS; ++i) { 4165718399fSFrançois Tigeot unsigned nr_free = shrink_pages; 4175718399fSFrançois Tigeot if (shrink_pages == 0) 4185718399fSFrançois Tigeot break; 4195718399fSFrançois Tigeot pool = &_manager->pools[(i + pool_offset)%NUM_POOLS]; 4205718399fSFrançois Tigeot shrink_pages = ttm_page_pool_free(pool, nr_free); 4215718399fSFrançois Tigeot } 4225718399fSFrançois Tigeot /* return estimated number of unused pages in pool */ 4235718399fSFrançois Tigeot return ttm_pool_get_num_unused_pages(); 4245718399fSFrançois Tigeot } 4255718399fSFrançois Tigeot 4265718399fSFrançois Tigeot static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager) 4275718399fSFrançois Tigeot { 4285718399fSFrançois Tigeot manager->lowmem_handler = EVENTHANDLER_REGISTER(vm_lowmem, 4295718399fSFrançois Tigeot ttm_pool_mm_shrink, manager, EVENTHANDLER_PRI_ANY); 4305718399fSFrançois Tigeot } 4315718399fSFrançois Tigeot 4325718399fSFrançois Tigeot static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager) 4335718399fSFrançois Tigeot { 4345718399fSFrançois Tigeot EVENTHANDLER_DEREGISTER(vm_lowmem, manager->lowmem_handler); 4355718399fSFrançois Tigeot } 4365718399fSFrançois Tigeot 437f0bba3d1SFrançois Tigeot static int ttm_set_pages_caching(struct page **pages, 4385718399fSFrançois Tigeot enum ttm_caching_state cstate, unsigned cpages) 4395718399fSFrançois Tigeot { 4405718399fSFrançois Tigeot int r = 0; 4415718399fSFrançois Tigeot /* Set page caching */ 4425718399fSFrançois Tigeot switch (cstate) { 4435718399fSFrançois Tigeot case tt_uncached: 4445718399fSFrançois Tigeot r = set_pages_array_uc(pages, cpages); 4455718399fSFrançois Tigeot if (r) 4460bece63dSImre Vadasz pr_err("Failed to set %d pages to uc!\n", cpages); 4475718399fSFrançois Tigeot break; 4485718399fSFrançois Tigeot case tt_wc: 4495718399fSFrançois Tigeot r = set_pages_array_wc(pages, cpages); 4505718399fSFrançois Tigeot if (r) 4510bece63dSImre Vadasz pr_err("Failed to set %d pages to wc!\n", cpages); 4525718399fSFrançois Tigeot break; 4535718399fSFrançois Tigeot default: 4545718399fSFrançois Tigeot break; 4555718399fSFrançois Tigeot } 4565718399fSFrançois Tigeot return r; 4575718399fSFrançois Tigeot } 4585718399fSFrançois Tigeot 4595718399fSFrançois Tigeot /** 4605718399fSFrançois Tigeot * Free pages the pages that failed to change the caching state. If there is 4615718399fSFrançois Tigeot * any pages that have changed their caching state already put them to the 4625718399fSFrançois Tigeot * pool. 4635718399fSFrançois Tigeot */ 4645718399fSFrançois Tigeot static void ttm_handle_caching_state_failure(struct pglist *pages, 4655718399fSFrançois Tigeot int ttm_flags, enum ttm_caching_state cstate, 466f0bba3d1SFrançois Tigeot struct page **failed_pages, unsigned cpages) 4675718399fSFrançois Tigeot { 4685718399fSFrançois Tigeot unsigned i; 4695718399fSFrançois Tigeot /* Failed pages have to be freed */ 4705718399fSFrançois Tigeot for (i = 0; i < cpages; ++i) { 471f0bba3d1SFrançois Tigeot TAILQ_REMOVE(pages, (struct vm_page *)failed_pages[i], pageq); 472e5c1d8f1SFrançois Tigeot __free_page(failed_pages[i]); 4735718399fSFrançois Tigeot } 4745718399fSFrançois Tigeot } 4755718399fSFrançois Tigeot 4765718399fSFrançois Tigeot /** 4775718399fSFrançois Tigeot * Allocate new pages with correct caching. 4785718399fSFrançois Tigeot * 4795718399fSFrançois Tigeot * This function is reentrant if caller updates count depending on number of 4805718399fSFrançois Tigeot * pages returned in pages array. 4815718399fSFrançois Tigeot */ 482*6af927c2SFrançois Tigeot static int ttm_alloc_new_pages(struct pglist *pages, gfp_t gfp_flags, 4835718399fSFrançois Tigeot int ttm_flags, enum ttm_caching_state cstate, unsigned count) 4845718399fSFrançois Tigeot { 485f0bba3d1SFrançois Tigeot struct page **caching_array; 486*6af927c2SFrançois Tigeot struct page *p; 4875718399fSFrançois Tigeot int r = 0; 488*6af927c2SFrançois Tigeot unsigned i, cpages; 4895718399fSFrançois Tigeot unsigned max_cpages = min(count, 490*6af927c2SFrançois Tigeot (unsigned)(PAGE_SIZE/sizeof(struct page *))); 4915718399fSFrançois Tigeot 4925718399fSFrançois Tigeot /* allocate array for page caching change */ 493*6af927c2SFrançois Tigeot caching_array = kmalloc(max_cpages*sizeof(struct page *), M_DRM, M_WAITOK); 494*6af927c2SFrançois Tigeot 495*6af927c2SFrançois Tigeot if (!caching_array) { 496*6af927c2SFrançois Tigeot pr_err("Unable to allocate table for new pages\n"); 497*6af927c2SFrançois Tigeot return -ENOMEM; 498*6af927c2SFrançois Tigeot } 4995718399fSFrançois Tigeot 5005718399fSFrançois Tigeot for (i = 0, cpages = 0; i < count; ++i) { 501*6af927c2SFrançois Tigeot p = alloc_page(gfp_flags); 502*6af927c2SFrançois Tigeot 5035718399fSFrançois Tigeot if (!p) { 5040bece63dSImre Vadasz pr_err("Unable to get page %u\n", i); 5055718399fSFrançois Tigeot 5065718399fSFrançois Tigeot /* store already allocated pages in the pool after 5075718399fSFrançois Tigeot * setting the caching state */ 5085718399fSFrançois Tigeot if (cpages) { 5095718399fSFrançois Tigeot r = ttm_set_pages_caching(caching_array, 5105718399fSFrançois Tigeot cstate, cpages); 5115718399fSFrançois Tigeot if (r) 5125718399fSFrançois Tigeot ttm_handle_caching_state_failure(pages, 5135718399fSFrançois Tigeot ttm_flags, cstate, 5145718399fSFrançois Tigeot caching_array, cpages); 5155718399fSFrançois Tigeot } 5165718399fSFrançois Tigeot r = -ENOMEM; 5175718399fSFrançois Tigeot goto out; 5185718399fSFrançois Tigeot } 519*6af927c2SFrançois Tigeot ((struct vm_page *)p)->flags |= PG_FICTITIOUS; 5205718399fSFrançois Tigeot 521*6af927c2SFrançois Tigeot #ifdef CONFIG_HIGHMEM 5225718399fSFrançois Tigeot /* gfp flags of highmem page should never be dma32 so we 5235718399fSFrançois Tigeot * we should be fine in such case 5245718399fSFrançois Tigeot */ 5255718399fSFrançois Tigeot if (!PageHighMem(p)) 5265718399fSFrançois Tigeot #endif 5275718399fSFrançois Tigeot { 528*6af927c2SFrançois Tigeot caching_array[cpages++] = p; 5295718399fSFrançois Tigeot if (cpages == max_cpages) { 5305718399fSFrançois Tigeot 5315718399fSFrançois Tigeot r = ttm_set_pages_caching(caching_array, 5325718399fSFrançois Tigeot cstate, cpages); 5335718399fSFrançois Tigeot if (r) { 5345718399fSFrançois Tigeot ttm_handle_caching_state_failure(pages, 5355718399fSFrançois Tigeot ttm_flags, cstate, 5365718399fSFrançois Tigeot caching_array, cpages); 5375718399fSFrançois Tigeot goto out; 5385718399fSFrançois Tigeot } 5395718399fSFrançois Tigeot cpages = 0; 5405718399fSFrançois Tigeot } 5415718399fSFrançois Tigeot } 5425718399fSFrançois Tigeot 543*6af927c2SFrançois Tigeot TAILQ_INSERT_HEAD(pages, (struct vm_page *)p, pageq); 5445718399fSFrançois Tigeot } 5455718399fSFrançois Tigeot 5465718399fSFrançois Tigeot if (cpages) { 5475718399fSFrançois Tigeot r = ttm_set_pages_caching(caching_array, cstate, cpages); 5485718399fSFrançois Tigeot if (r) 5495718399fSFrançois Tigeot ttm_handle_caching_state_failure(pages, 5505718399fSFrançois Tigeot ttm_flags, cstate, 5515718399fSFrançois Tigeot caching_array, cpages); 5525718399fSFrançois Tigeot } 5535718399fSFrançois Tigeot out: 554*6af927c2SFrançois Tigeot kfree(caching_array); 5555718399fSFrançois Tigeot 5565718399fSFrançois Tigeot return r; 5575718399fSFrançois Tigeot } 5585718399fSFrançois Tigeot 5595718399fSFrançois Tigeot /** 5605718399fSFrançois Tigeot * Fill the given pool if there aren't enough pages and the requested number of 5615718399fSFrançois Tigeot * pages is small. 5625718399fSFrançois Tigeot */ 5635718399fSFrançois Tigeot static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, 564*6af927c2SFrançois Tigeot int ttm_flags, enum ttm_caching_state cstate, unsigned count, 565*6af927c2SFrançois Tigeot unsigned long *irq_flags) 5665718399fSFrançois Tigeot { 5675718399fSFrançois Tigeot vm_page_t p; 5685718399fSFrançois Tigeot int r; 5695718399fSFrançois Tigeot unsigned cpages = 0; 5705718399fSFrançois Tigeot /** 5715718399fSFrançois Tigeot * Only allow one pool fill operation at a time. 5725718399fSFrançois Tigeot * If pool doesn't have enough pages for the allocation new pages are 5735718399fSFrançois Tigeot * allocated from outside of pool. 5745718399fSFrançois Tigeot */ 5755718399fSFrançois Tigeot if (pool->fill_lock) 5765718399fSFrançois Tigeot return; 5775718399fSFrançois Tigeot 5785718399fSFrançois Tigeot pool->fill_lock = true; 5795718399fSFrançois Tigeot 5805718399fSFrançois Tigeot /* If allocation request is small and there are not enough 5815718399fSFrançois Tigeot * pages in a pool we fill the pool up first. */ 5825718399fSFrançois Tigeot if (count < _manager->options.small 5835718399fSFrançois Tigeot && count > pool->npages) { 5845718399fSFrançois Tigeot struct pglist new_pages; 5855718399fSFrançois Tigeot unsigned alloc_size = _manager->options.alloc_size; 5865718399fSFrançois Tigeot 5875718399fSFrançois Tigeot /** 5885718399fSFrançois Tigeot * Can't change page caching if in irqsave context. We have to 5895718399fSFrançois Tigeot * drop the pool->lock. 5905718399fSFrançois Tigeot */ 591*6af927c2SFrançois Tigeot spin_unlock_irqrestore(&pool->lock, *irq_flags); 5925718399fSFrançois Tigeot 5935718399fSFrançois Tigeot TAILQ_INIT(&new_pages); 594*6af927c2SFrançois Tigeot r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags, 595*6af927c2SFrançois Tigeot cstate, alloc_size); 596*6af927c2SFrançois Tigeot spin_lock_irqsave(&pool->lock, *irq_flags); 5975718399fSFrançois Tigeot 5985718399fSFrançois Tigeot if (!r) { 5995718399fSFrançois Tigeot TAILQ_CONCAT(&pool->list, &new_pages, pageq); 6005718399fSFrançois Tigeot ++pool->nrefills; 6015718399fSFrançois Tigeot pool->npages += alloc_size; 6025718399fSFrançois Tigeot } else { 6030bece63dSImre Vadasz pr_err("Failed to fill pool (%p)\n", pool); 6045718399fSFrançois Tigeot /* If we have any pages left put them to the pool. */ 6055718399fSFrançois Tigeot TAILQ_FOREACH(p, &pool->list, pageq) { 6065718399fSFrançois Tigeot ++cpages; 6075718399fSFrançois Tigeot } 6085718399fSFrançois Tigeot TAILQ_CONCAT(&pool->list, &new_pages, pageq); 6095718399fSFrançois Tigeot pool->npages += cpages; 6105718399fSFrançois Tigeot } 6115718399fSFrançois Tigeot 6125718399fSFrançois Tigeot } 6135718399fSFrançois Tigeot pool->fill_lock = false; 6145718399fSFrançois Tigeot } 6155718399fSFrançois Tigeot 6165718399fSFrançois Tigeot /** 6175718399fSFrançois Tigeot * Cut 'count' number of pages from the pool and put them on the return list. 6185718399fSFrançois Tigeot * 6195718399fSFrançois Tigeot * @return count of pages still required to fulfill the request. 6205718399fSFrançois Tigeot */ 6215718399fSFrançois Tigeot static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool, 6225718399fSFrançois Tigeot struct pglist *pages, 6235718399fSFrançois Tigeot int ttm_flags, 6245718399fSFrançois Tigeot enum ttm_caching_state cstate, 6255718399fSFrançois Tigeot unsigned count) 6265718399fSFrançois Tigeot { 627*6af927c2SFrançois Tigeot unsigned long irq_flags; 6285718399fSFrançois Tigeot vm_page_t p; 6295718399fSFrançois Tigeot unsigned i; 6305718399fSFrançois Tigeot 631*6af927c2SFrançois Tigeot spin_lock_irqsave(&pool->lock, irq_flags); 632*6af927c2SFrançois Tigeot ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count, &irq_flags); 6335718399fSFrançois Tigeot 6345718399fSFrançois Tigeot if (count >= pool->npages) { 6355718399fSFrançois Tigeot /* take all pages from the pool */ 6365718399fSFrançois Tigeot TAILQ_CONCAT(pages, &pool->list, pageq); 6375718399fSFrançois Tigeot count -= pool->npages; 6385718399fSFrançois Tigeot pool->npages = 0; 6395718399fSFrançois Tigeot goto out; 6405718399fSFrançois Tigeot } 6415718399fSFrançois Tigeot for (i = 0; i < count; i++) { 6425718399fSFrançois Tigeot p = TAILQ_FIRST(&pool->list); 6435718399fSFrançois Tigeot TAILQ_REMOVE(&pool->list, p, pageq); 6445718399fSFrançois Tigeot TAILQ_INSERT_TAIL(pages, p, pageq); 6455718399fSFrançois Tigeot } 6465718399fSFrançois Tigeot pool->npages -= count; 6475718399fSFrançois Tigeot count = 0; 6485718399fSFrançois Tigeot out: 649*6af927c2SFrançois Tigeot spin_unlock_irqrestore(&pool->lock, irq_flags); 6505718399fSFrançois Tigeot return count; 6515718399fSFrançois Tigeot } 6525718399fSFrançois Tigeot 6535718399fSFrançois Tigeot /* Put all pages in pages list to correct pool to wait for reuse */ 654f0bba3d1SFrançois Tigeot static void ttm_put_pages(struct page **pages, unsigned npages, int flags, 6555718399fSFrançois Tigeot enum ttm_caching_state cstate) 6565718399fSFrançois Tigeot { 657*6af927c2SFrançois Tigeot unsigned long irq_flags; 6585718399fSFrançois Tigeot struct ttm_page_pool *pool = ttm_get_pool(flags, cstate); 6595718399fSFrançois Tigeot unsigned i; 660f0bba3d1SFrançois Tigeot struct vm_page *page; 6615718399fSFrançois Tigeot 6625718399fSFrançois Tigeot if (pool == NULL) { 6635718399fSFrançois Tigeot /* No pool for this memory type so free the pages */ 6645718399fSFrançois Tigeot for (i = 0; i < npages; i++) { 6655718399fSFrançois Tigeot if (pages[i]) { 666e5c1d8f1SFrançois Tigeot #if 0 667e5c1d8f1SFrançois Tigeot if (page_count(pages[i]) != 1) 668e5c1d8f1SFrançois Tigeot pr_err("Erroneous page count. Leaking pages.\n"); 669e5c1d8f1SFrançois Tigeot #endif 670e5c1d8f1SFrançois Tigeot __free_page(pages[i]); 6715718399fSFrançois Tigeot pages[i] = NULL; 6725718399fSFrançois Tigeot } 6735718399fSFrançois Tigeot } 6745718399fSFrançois Tigeot return; 6755718399fSFrançois Tigeot } 6765718399fSFrançois Tigeot 677*6af927c2SFrançois Tigeot spin_lock_irqsave(&pool->lock, irq_flags); 6785718399fSFrançois Tigeot for (i = 0; i < npages; i++) { 6795718399fSFrançois Tigeot if (pages[i]) { 680f0bba3d1SFrançois Tigeot page = (struct vm_page *)pages[i]; 681f0bba3d1SFrançois Tigeot TAILQ_INSERT_TAIL(&pool->list, page, pageq); 6825718399fSFrançois Tigeot pages[i] = NULL; 6835718399fSFrançois Tigeot pool->npages++; 6845718399fSFrançois Tigeot } 6855718399fSFrançois Tigeot } 6865718399fSFrançois Tigeot /* Check that we don't go over the pool limit */ 6875718399fSFrançois Tigeot npages = 0; 6885718399fSFrançois Tigeot if (pool->npages > _manager->options.max_size) { 6895718399fSFrançois Tigeot npages = pool->npages - _manager->options.max_size; 6905718399fSFrançois Tigeot /* free at least NUM_PAGES_TO_ALLOC number of pages 6915718399fSFrançois Tigeot * to reduce calls to set_memory_wb */ 6925718399fSFrançois Tigeot if (npages < NUM_PAGES_TO_ALLOC) 6935718399fSFrançois Tigeot npages = NUM_PAGES_TO_ALLOC; 6945718399fSFrançois Tigeot } 695*6af927c2SFrançois Tigeot spin_unlock_irqrestore(&pool->lock, irq_flags); 6965718399fSFrançois Tigeot if (npages) 6975718399fSFrançois Tigeot ttm_page_pool_free(pool, npages); 6985718399fSFrançois Tigeot } 6995718399fSFrançois Tigeot 7005718399fSFrançois Tigeot /* 7015718399fSFrançois Tigeot * On success pages list will hold count number of correctly 7025718399fSFrançois Tigeot * cached pages. 7035718399fSFrançois Tigeot */ 704f0bba3d1SFrançois Tigeot static int ttm_get_pages(struct page **pages, unsigned npages, int flags, 7055718399fSFrançois Tigeot enum ttm_caching_state cstate) 7065718399fSFrançois Tigeot { 7075718399fSFrançois Tigeot struct ttm_page_pool *pool = ttm_get_pool(flags, cstate); 7085718399fSFrançois Tigeot struct pglist plist; 709f0bba3d1SFrançois Tigeot struct vm_page *p = NULL; 710*6af927c2SFrançois Tigeot gfp_t gfp_flags = GFP_USER; 7115718399fSFrançois Tigeot unsigned count; 7125718399fSFrançois Tigeot int r; 7135718399fSFrançois Tigeot 714*6af927c2SFrançois Tigeot /* set zero flag for page allocation if required */ 715*6af927c2SFrançois Tigeot if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) 716*6af927c2SFrançois Tigeot gfp_flags |= __GFP_ZERO; 7175718399fSFrançois Tigeot 7185718399fSFrançois Tigeot /* No pool for cached pages */ 7195718399fSFrançois Tigeot if (pool == NULL) { 720*6af927c2SFrançois Tigeot if (flags & TTM_PAGE_FLAG_DMA32) 721*6af927c2SFrançois Tigeot gfp_flags |= GFP_DMA32; 722*6af927c2SFrançois Tigeot else 723*6af927c2SFrançois Tigeot gfp_flags |= GFP_HIGHUSER; 724*6af927c2SFrançois Tigeot 7255718399fSFrançois Tigeot for (r = 0; r < npages; ++r) { 726*6af927c2SFrançois Tigeot p = (struct vm_page *)alloc_page(gfp_flags); 7275718399fSFrançois Tigeot if (!p) { 728*6af927c2SFrançois Tigeot 7290bece63dSImre Vadasz pr_err("Unable to allocate page\n"); 7305718399fSFrançois Tigeot return -ENOMEM; 7315718399fSFrançois Tigeot } 7325718399fSFrançois Tigeot p->flags |= PG_FICTITIOUS; 733*6af927c2SFrançois Tigeot 734f0bba3d1SFrançois Tigeot pages[r] = (struct page *)p; 7355718399fSFrançois Tigeot } 7365718399fSFrançois Tigeot return 0; 7375718399fSFrançois Tigeot } 7385718399fSFrançois Tigeot 7395718399fSFrançois Tigeot /* combine zero flag to pool flags */ 740*6af927c2SFrançois Tigeot gfp_flags |= pool->gfp_flags; 7415718399fSFrançois Tigeot 7425718399fSFrançois Tigeot /* First we take pages from the pool */ 7435718399fSFrançois Tigeot TAILQ_INIT(&plist); 7445718399fSFrançois Tigeot npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages); 7455718399fSFrançois Tigeot count = 0; 7465718399fSFrançois Tigeot TAILQ_FOREACH(p, &plist, pageq) { 747f0bba3d1SFrançois Tigeot pages[count++] = (struct page *)p; 7485718399fSFrançois Tigeot } 7495718399fSFrançois Tigeot 7505718399fSFrançois Tigeot /* clear the pages coming from the pool if requested */ 7515718399fSFrançois Tigeot if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) { 7525718399fSFrançois Tigeot TAILQ_FOREACH(p, &plist, pageq) { 7535718399fSFrançois Tigeot pmap_zero_page(VM_PAGE_TO_PHYS(p)); 7545718399fSFrançois Tigeot } 7555718399fSFrançois Tigeot } 7565718399fSFrançois Tigeot 7575718399fSFrançois Tigeot /* If pool didn't have enough pages allocate new one. */ 7585718399fSFrançois Tigeot if (npages > 0) { 7595718399fSFrançois Tigeot /* ttm_alloc_new_pages doesn't reference pool so we can run 7605718399fSFrançois Tigeot * multiple requests in parallel. 7615718399fSFrançois Tigeot **/ 7625718399fSFrançois Tigeot TAILQ_INIT(&plist); 763*6af927c2SFrançois Tigeot r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate, npages); 7645718399fSFrançois Tigeot TAILQ_FOREACH(p, &plist, pageq) { 765f0bba3d1SFrançois Tigeot pages[count++] = (struct page *)p; 7665718399fSFrançois Tigeot } 7675718399fSFrançois Tigeot if (r) { 7685718399fSFrançois Tigeot /* If there is any pages in the list put them back to 7695718399fSFrançois Tigeot * the pool. */ 7700bece63dSImre Vadasz pr_err("Failed to allocate extra pages for large request\n"); 7715718399fSFrançois Tigeot ttm_put_pages(pages, count, flags, cstate); 7725718399fSFrançois Tigeot return r; 7735718399fSFrançois Tigeot } 7745718399fSFrançois Tigeot } 7755718399fSFrançois Tigeot 7765718399fSFrançois Tigeot return 0; 7775718399fSFrançois Tigeot } 7785718399fSFrançois Tigeot 779*6af927c2SFrançois Tigeot static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags, 7805718399fSFrançois Tigeot char *name) 7815718399fSFrançois Tigeot { 7825718399fSFrançois Tigeot lockinit(&pool->lock, "ttmpool", 0, LK_CANRECURSE); 7835718399fSFrançois Tigeot pool->fill_lock = false; 7845718399fSFrançois Tigeot TAILQ_INIT(&pool->list); 7855718399fSFrançois Tigeot pool->npages = pool->nfrees = 0; 786*6af927c2SFrançois Tigeot pool->gfp_flags = flags; 7875718399fSFrançois Tigeot pool->name = name; 7885718399fSFrançois Tigeot } 7895718399fSFrançois Tigeot 7905718399fSFrançois Tigeot int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages) 7915718399fSFrançois Tigeot { 7923a2096e8SFrançois Tigeot int ret; 7933a2096e8SFrançois Tigeot 7940bece63dSImre Vadasz WARN_ON(_manager); 7955718399fSFrançois Tigeot 7960bece63dSImre Vadasz pr_info("Initializing pool allocator\n"); 7975718399fSFrançois Tigeot 798175896dfSzrj _manager = kzalloc(sizeof(*_manager), GFP_KERNEL); 7995718399fSFrançois Tigeot 800*6af927c2SFrançois Tigeot ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc"); 801*6af927c2SFrançois Tigeot 802*6af927c2SFrançois Tigeot ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc"); 803*6af927c2SFrançois Tigeot 8045718399fSFrançois Tigeot ttm_page_pool_init_locked(&_manager->wc_pool_dma32, 805*6af927c2SFrançois Tigeot GFP_USER | GFP_DMA32, "wc dma"); 806*6af927c2SFrançois Tigeot 8075718399fSFrançois Tigeot ttm_page_pool_init_locked(&_manager->uc_pool_dma32, 808*6af927c2SFrançois Tigeot GFP_USER | GFP_DMA32, "uc dma"); 8095718399fSFrançois Tigeot 8105718399fSFrançois Tigeot _manager->options.max_size = max_pages; 8115718399fSFrançois Tigeot _manager->options.small = SMALL_ALLOCATION; 8125718399fSFrançois Tigeot _manager->options.alloc_size = NUM_PAGES_TO_ALLOC; 8135718399fSFrançois Tigeot 8143a2096e8SFrançois Tigeot ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type, 8153a2096e8SFrançois Tigeot &glob->kobj, "pool"); 8163a2096e8SFrançois Tigeot if (unlikely(ret != 0)) { 8173a2096e8SFrançois Tigeot kobject_put(&_manager->kobj); 8183a2096e8SFrançois Tigeot _manager = NULL; 8193a2096e8SFrançois Tigeot return ret; 8203a2096e8SFrançois Tigeot } 821*6af927c2SFrançois Tigeot 8225718399fSFrançois Tigeot ttm_pool_mm_shrink_init(_manager); 8235718399fSFrançois Tigeot 8245718399fSFrançois Tigeot return 0; 8255718399fSFrançois Tigeot } 8265718399fSFrançois Tigeot 8275718399fSFrançois Tigeot void ttm_page_alloc_fini(void) 8285718399fSFrançois Tigeot { 8295718399fSFrançois Tigeot int i; 8305718399fSFrançois Tigeot 8310bece63dSImre Vadasz pr_info("Finalizing pool allocator\n"); 8325718399fSFrançois Tigeot ttm_pool_mm_shrink_fini(_manager); 8335718399fSFrançois Tigeot 8345718399fSFrançois Tigeot for (i = 0; i < NUM_POOLS; ++i) 8355718399fSFrançois Tigeot ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES); 8365718399fSFrançois Tigeot 8373a2096e8SFrançois Tigeot kobject_put(&_manager->kobj); 8385718399fSFrançois Tigeot _manager = NULL; 8395718399fSFrançois Tigeot } 8405718399fSFrançois Tigeot 8415718399fSFrançois Tigeot int ttm_pool_populate(struct ttm_tt *ttm) 8425718399fSFrançois Tigeot { 8435718399fSFrançois Tigeot struct ttm_mem_global *mem_glob = ttm->glob->mem_glob; 8445718399fSFrançois Tigeot unsigned i; 8455718399fSFrançois Tigeot int ret; 8465718399fSFrançois Tigeot 8475718399fSFrançois Tigeot if (ttm->state != tt_unpopulated) 8485718399fSFrançois Tigeot return 0; 8495718399fSFrançois Tigeot 8505718399fSFrançois Tigeot for (i = 0; i < ttm->num_pages; ++i) { 8515718399fSFrançois Tigeot ret = ttm_get_pages(&ttm->pages[i], 1, 8525718399fSFrançois Tigeot ttm->page_flags, 8535718399fSFrançois Tigeot ttm->caching_state); 8545718399fSFrançois Tigeot if (ret != 0) { 8555718399fSFrançois Tigeot ttm_pool_unpopulate(ttm); 8565718399fSFrançois Tigeot return -ENOMEM; 8575718399fSFrançois Tigeot } 8585718399fSFrançois Tigeot 8595718399fSFrançois Tigeot ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i], 8605718399fSFrançois Tigeot false, false); 8615718399fSFrançois Tigeot if (unlikely(ret != 0)) { 8625718399fSFrançois Tigeot ttm_pool_unpopulate(ttm); 8635718399fSFrançois Tigeot return -ENOMEM; 8645718399fSFrançois Tigeot } 8655718399fSFrançois Tigeot } 8665718399fSFrançois Tigeot 8675718399fSFrançois Tigeot if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) { 8685718399fSFrançois Tigeot ret = ttm_tt_swapin(ttm); 8695718399fSFrançois Tigeot if (unlikely(ret != 0)) { 8705718399fSFrançois Tigeot ttm_pool_unpopulate(ttm); 8715718399fSFrançois Tigeot return ret; 8725718399fSFrançois Tigeot } 8735718399fSFrançois Tigeot } 8745718399fSFrançois Tigeot 8755718399fSFrançois Tigeot ttm->state = tt_unbound; 8765718399fSFrançois Tigeot return 0; 8775718399fSFrançois Tigeot } 8783a2096e8SFrançois Tigeot EXPORT_SYMBOL(ttm_pool_populate); 8795718399fSFrançois Tigeot 8805718399fSFrançois Tigeot void ttm_pool_unpopulate(struct ttm_tt *ttm) 8815718399fSFrançois Tigeot { 8825718399fSFrançois Tigeot unsigned i; 8835718399fSFrançois Tigeot 8845718399fSFrançois Tigeot for (i = 0; i < ttm->num_pages; ++i) { 8855718399fSFrançois Tigeot if (ttm->pages[i]) { 8865718399fSFrançois Tigeot ttm_mem_global_free_page(ttm->glob->mem_glob, 8875718399fSFrançois Tigeot ttm->pages[i]); 8885718399fSFrançois Tigeot ttm_put_pages(&ttm->pages[i], 1, 8895718399fSFrançois Tigeot ttm->page_flags, 8905718399fSFrançois Tigeot ttm->caching_state); 8915718399fSFrançois Tigeot } 8925718399fSFrançois Tigeot } 8935718399fSFrançois Tigeot ttm->state = tt_unpopulated; 8945718399fSFrançois Tigeot } 895*6af927c2SFrançois Tigeot EXPORT_SYMBOL(ttm_pool_unpopulate); 8965718399fSFrançois Tigeot 8975718399fSFrançois Tigeot #if 0 8985718399fSFrançois Tigeot int ttm_page_alloc_debugfs(struct seq_file *m, void *data) 8995718399fSFrançois Tigeot { 9005718399fSFrançois Tigeot struct ttm_page_pool *p; 9015718399fSFrançois Tigeot unsigned i; 9025718399fSFrançois Tigeot char *h[] = {"pool", "refills", "pages freed", "size"}; 9035718399fSFrançois Tigeot if (!_manager) { 9045718399fSFrançois Tigeot seq_printf(m, "No pool allocator running.\n"); 9055718399fSFrançois Tigeot return 0; 9065718399fSFrançois Tigeot } 9075718399fSFrançois Tigeot seq_printf(m, "%6s %12s %13s %8s\n", 9085718399fSFrançois Tigeot h[0], h[1], h[2], h[3]); 9095718399fSFrançois Tigeot for (i = 0; i < NUM_POOLS; ++i) { 9105718399fSFrançois Tigeot p = &_manager->pools[i]; 9115718399fSFrançois Tigeot 9125718399fSFrançois Tigeot seq_printf(m, "%6s %12ld %13ld %8d\n", 9135718399fSFrançois Tigeot p->name, p->nrefills, 9145718399fSFrançois Tigeot p->nfrees, p->npages); 9155718399fSFrançois Tigeot } 9165718399fSFrançois Tigeot return 0; 9175718399fSFrançois Tigeot } 9185718399fSFrançois Tigeot #endif 9193a2096e8SFrançois Tigeot EXPORT_SYMBOL(ttm_page_alloc_debugfs); 920