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