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