ttm_memory.c revision 247835
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 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/dev/drm2/ttm/ttm_memory.c 247835 2013-03-05 09:49:34Z kib $"); 30 31#include <dev/drm2/drmP.h> 32#include <dev/drm2/ttm/ttm_memory.h> 33#include <dev/drm2/ttm/ttm_module.h> 34#include <dev/drm2/ttm/ttm_page_alloc.h> 35 36#define TTM_MEMORY_ALLOC_RETRIES 4 37 38struct ttm_mem_zone { 39 u_int kobj_ref; 40 struct ttm_mem_global *glob; 41 const char *name; 42 uint64_t zone_mem; 43 uint64_t emer_mem; 44 uint64_t max_mem; 45 uint64_t swap_limit; 46 uint64_t used_mem; 47}; 48 49MALLOC_DEFINE(M_TTM_ZONE, "ttm_zone", "TTM Zone"); 50 51static void ttm_mem_zone_kobj_release(struct ttm_mem_zone *zone) 52{ 53 54 printf("pTTM] Zone %7s: Used memory at exit: %llu kiB\n", 55 zone->name, (unsigned long long)zone->used_mem >> 10); 56 free(zone, M_TTM_ZONE); 57} 58 59#if 0 60/* XXXKIB sysctl */ 61static ssize_t ttm_mem_zone_show(struct ttm_mem_zone *zone; 62 struct attribute *attr, 63 char *buffer) 64{ 65 uint64_t val = 0; 66 67 mtx_lock(&zone->glob->lock); 68 if (attr == &ttm_mem_sys) 69 val = zone->zone_mem; 70 else if (attr == &ttm_mem_emer) 71 val = zone->emer_mem; 72 else if (attr == &ttm_mem_max) 73 val = zone->max_mem; 74 else if (attr == &ttm_mem_swap) 75 val = zone->swap_limit; 76 else if (attr == &ttm_mem_used) 77 val = zone->used_mem; 78 mtx_unlock(&zone->glob->lock); 79 80 return snprintf(buffer, PAGE_SIZE, "%llu\n", 81 (unsigned long long) val >> 10); 82} 83#endif 84 85static void ttm_check_swapping(struct ttm_mem_global *glob); 86 87#if 0 88/* XXXKIB sysctl */ 89static ssize_t ttm_mem_zone_store(struct ttm_mem_zone *zone, 90 struct attribute *attr, 91 const char *buffer, 92 size_t size) 93{ 94 int chars; 95 unsigned long val; 96 uint64_t val64; 97 98 chars = sscanf(buffer, "%lu", &val); 99 if (chars == 0) 100 return size; 101 102 val64 = val; 103 val64 <<= 10; 104 105 mtx_lock(&zone->glob->lock); 106 if (val64 > zone->zone_mem) 107 val64 = zone->zone_mem; 108 if (attr == &ttm_mem_emer) { 109 zone->emer_mem = val64; 110 if (zone->max_mem > val64) 111 zone->max_mem = val64; 112 } else if (attr == &ttm_mem_max) { 113 zone->max_mem = val64; 114 if (zone->emer_mem < val64) 115 zone->emer_mem = val64; 116 } else if (attr == &ttm_mem_swap) 117 zone->swap_limit = val64; 118 mtx_unlock(&zone->glob->lock); 119 120 ttm_check_swapping(zone->glob); 121 122 return size; 123} 124#endif 125 126static void ttm_mem_global_kobj_release(struct ttm_mem_global *glob) 127{ 128 129 free(glob, M_TTM_ZONE); 130} 131 132static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, 133 bool from_wq, uint64_t extra) 134{ 135 unsigned int i; 136 struct ttm_mem_zone *zone; 137 uint64_t target; 138 139 for (i = 0; i < glob->num_zones; ++i) { 140 zone = glob->zones[i]; 141 142 if (from_wq) 143 target = zone->swap_limit; 144 else if (priv_check(curthread, PRIV_VM_MLOCK) == 0) 145 target = zone->emer_mem; 146 else 147 target = zone->max_mem; 148 149 target = (extra > target) ? 0ULL : target; 150 151 if (zone->used_mem > target) 152 return true; 153 } 154 return false; 155} 156 157/** 158 * At this point we only support a single shrink callback. 159 * Extend this if needed, perhaps using a linked list of callbacks. 160 * Note that this function is reentrant: 161 * many threads may try to swap out at any given time. 162 */ 163 164static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq, 165 uint64_t extra) 166{ 167 int ret; 168 struct ttm_mem_shrink *shrink; 169 170 mtx_lock(&glob->lock); 171 if (glob->shrink == NULL) 172 goto out; 173 174 while (ttm_zones_above_swap_target(glob, from_wq, extra)) { 175 shrink = glob->shrink; 176 mtx_unlock(&glob->lock); 177 ret = shrink->do_shrink(shrink); 178 mtx_lock(&glob->lock); 179 if (unlikely(ret != 0)) 180 goto out; 181 } 182out: 183 mtx_unlock(&glob->lock); 184} 185 186 187 188static void ttm_shrink_work(void *arg, int pending __unused) 189{ 190 struct ttm_mem_global *glob = arg; 191 192 ttm_shrink(glob, true, 0ULL); 193} 194 195static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob, 196 uint64_t mem) 197{ 198 struct ttm_mem_zone *zone; 199 200 zone = malloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO); 201 202 zone->name = "kernel"; 203 zone->zone_mem = mem; 204 zone->max_mem = mem >> 1; 205 zone->emer_mem = (mem >> 1) + (mem >> 2); 206 zone->swap_limit = zone->max_mem - (mem >> 3); 207 zone->used_mem = 0; 208 zone->glob = glob; 209 glob->zone_kernel = zone; 210 refcount_init(&zone->kobj_ref, 1); 211 glob->zones[glob->num_zones++] = zone; 212 return 0; 213} 214 215static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob, 216 uint64_t mem) 217{ 218 struct ttm_mem_zone *zone; 219 220 zone = malloc(sizeof(*zone), M_TTM_ZONE, M_WAITOK | M_ZERO); 221 222 /** 223 * No special dma32 zone needed. 224 */ 225 226 if (mem <= ((uint64_t) 1ULL << 32)) { 227 free(zone, M_TTM_ZONE); 228 return 0; 229 } 230 231 /* 232 * Limit max dma32 memory to 4GB for now 233 * until we can figure out how big this 234 * zone really is. 235 */ 236 237 mem = ((uint64_t) 1ULL << 32); 238 zone->name = "dma32"; 239 zone->zone_mem = mem; 240 zone->max_mem = mem >> 1; 241 zone->emer_mem = (mem >> 1) + (mem >> 2); 242 zone->swap_limit = zone->max_mem - (mem >> 3); 243 zone->used_mem = 0; 244 zone->glob = glob; 245 glob->zone_dma32 = zone; 246 refcount_init(&zone->kobj_ref, 1); 247 glob->zones[glob->num_zones++] = zone; 248 return 0; 249} 250 251int ttm_mem_global_init(struct ttm_mem_global *glob) 252{ 253 u_int64_t mem; 254 int ret; 255 int i; 256 struct ttm_mem_zone *zone; 257 258 mtx_init(&glob->lock, "ttmgz", NULL, MTX_DEF); 259 glob->swap_queue = taskqueue_create("ttm_swap", M_WAITOK, 260 taskqueue_thread_enqueue, &glob->swap_queue); 261 taskqueue_start_threads(&glob->swap_queue, 1, PVM, "ttm swap"); 262 TASK_INIT(&glob->work, 0, ttm_shrink_work, glob); 263 264 refcount_init(&glob->kobj_ref, 1); 265 266 mem = physmem * PAGE_SIZE; 267 268 ret = ttm_mem_init_kernel_zone(glob, mem); 269 if (unlikely(ret != 0)) 270 goto out_no_zone; 271 ret = ttm_mem_init_dma32_zone(glob, mem); 272 if (unlikely(ret != 0)) 273 goto out_no_zone; 274 for (i = 0; i < glob->num_zones; ++i) { 275 zone = glob->zones[i]; 276 printf("[TTM] Zone %7s: Available graphics memory: %llu kiB\n", 277 zone->name, (unsigned long long)zone->max_mem >> 10); 278 } 279 ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE)); 280 ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE)); 281 return 0; 282out_no_zone: 283 ttm_mem_global_release(glob); 284 return ret; 285} 286 287void ttm_mem_global_release(struct ttm_mem_global *glob) 288{ 289 unsigned int i; 290 struct ttm_mem_zone *zone; 291 292 /* let the page allocator first stop the shrink work. */ 293 ttm_page_alloc_fini(); 294 ttm_dma_page_alloc_fini(); 295 296 taskqueue_drain(glob->swap_queue, &glob->work); 297 taskqueue_free(glob->swap_queue); 298 glob->swap_queue = NULL; 299 for (i = 0; i < glob->num_zones; ++i) { 300 zone = glob->zones[i]; 301 if (refcount_release(&zone->kobj_ref)) 302 ttm_mem_zone_kobj_release(zone); 303 } 304 if (refcount_release(&glob->kobj_ref)) 305 ttm_mem_global_kobj_release(glob); 306} 307 308static void ttm_check_swapping(struct ttm_mem_global *glob) 309{ 310 bool needs_swapping = false; 311 unsigned int i; 312 struct ttm_mem_zone *zone; 313 314 mtx_lock(&glob->lock); 315 for (i = 0; i < glob->num_zones; ++i) { 316 zone = glob->zones[i]; 317 if (zone->used_mem > zone->swap_limit) { 318 needs_swapping = true; 319 break; 320 } 321 } 322 323 mtx_unlock(&glob->lock); 324 325 if (unlikely(needs_swapping)) 326 taskqueue_enqueue(glob->swap_queue, &glob->work); 327 328} 329 330static void ttm_mem_global_free_zone(struct ttm_mem_global *glob, 331 struct ttm_mem_zone *single_zone, 332 uint64_t amount) 333{ 334 unsigned int i; 335 struct ttm_mem_zone *zone; 336 337 mtx_lock(&glob->lock); 338 for (i = 0; i < glob->num_zones; ++i) { 339 zone = glob->zones[i]; 340 if (single_zone && zone != single_zone) 341 continue; 342 zone->used_mem -= amount; 343 } 344 mtx_unlock(&glob->lock); 345} 346 347void ttm_mem_global_free(struct ttm_mem_global *glob, 348 uint64_t amount) 349{ 350 return ttm_mem_global_free_zone(glob, NULL, amount); 351} 352 353static int ttm_mem_global_reserve(struct ttm_mem_global *glob, 354 struct ttm_mem_zone *single_zone, 355 uint64_t amount, bool reserve) 356{ 357 uint64_t limit; 358 int ret = -ENOMEM; 359 unsigned int i; 360 struct ttm_mem_zone *zone; 361 362 mtx_lock(&glob->lock); 363 for (i = 0; i < glob->num_zones; ++i) { 364 zone = glob->zones[i]; 365 if (single_zone && zone != single_zone) 366 continue; 367 368 limit = (priv_check(curthread, PRIV_VM_MLOCK) == 0) ? 369 zone->emer_mem : zone->max_mem; 370 371 if (zone->used_mem > limit) 372 goto out_unlock; 373 } 374 375 if (reserve) { 376 for (i = 0; i < glob->num_zones; ++i) { 377 zone = glob->zones[i]; 378 if (single_zone && zone != single_zone) 379 continue; 380 zone->used_mem += amount; 381 } 382 } 383 384 ret = 0; 385out_unlock: 386 mtx_unlock(&glob->lock); 387 ttm_check_swapping(glob); 388 389 return ret; 390} 391 392 393static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob, 394 struct ttm_mem_zone *single_zone, 395 uint64_t memory, 396 bool no_wait, bool interruptible) 397{ 398 int count = TTM_MEMORY_ALLOC_RETRIES; 399 400 while (unlikely(ttm_mem_global_reserve(glob, 401 single_zone, 402 memory, true) 403 != 0)) { 404 if (no_wait) 405 return -ENOMEM; 406 if (unlikely(count-- == 0)) 407 return -ENOMEM; 408 ttm_shrink(glob, false, memory + (memory >> 2) + 16); 409 } 410 411 return 0; 412} 413 414int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory, 415 bool no_wait, bool interruptible) 416{ 417 /** 418 * Normal allocations of kernel memory are registered in 419 * all zones. 420 */ 421 422 return ttm_mem_global_alloc_zone(glob, NULL, memory, no_wait, 423 interruptible); 424} 425 426#define page_to_pfn(pp) OFF_TO_IDX(VM_PAGE_TO_PHYS(pp)) 427 428int ttm_mem_global_alloc_page(struct ttm_mem_global *glob, 429 struct vm_page *page, 430 bool no_wait, bool interruptible) 431{ 432 433 struct ttm_mem_zone *zone = NULL; 434 435 /** 436 * Page allocations may be registed in a single zone 437 * only if highmem or !dma32. 438 */ 439 440 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL) 441 zone = glob->zone_kernel; 442 return ttm_mem_global_alloc_zone(glob, zone, PAGE_SIZE, no_wait, 443 interruptible); 444} 445 446void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct vm_page *page) 447{ 448 struct ttm_mem_zone *zone = NULL; 449 450 if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL) 451 zone = glob->zone_kernel; 452 ttm_mem_global_free_zone(glob, zone, PAGE_SIZE); 453} 454 455 456size_t ttm_round_pot(size_t size) 457{ 458 if ((size & (size - 1)) == 0) 459 return size; 460 else if (size > PAGE_SIZE) 461 return PAGE_ALIGN(size); 462 else { 463 size_t tmp_size = 4; 464 465 while (tmp_size < size) 466 tmp_size <<= 1; 467 468 return tmp_size; 469 } 470 return 0; 471} 472