Commit | Line | Data |
---|---|---|
1297bf2e | 1 | /* SPDX-License-Identifier: GPL-2.0 OR MIT */ |
ba4e7d97 TH |
2 | /************************************************************************** |
3 | * | |
4 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA | |
5 | * All Rights Reserved. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a | |
8 | * copy of this software and associated documentation files (the | |
9 | * "Software"), to deal in the Software without restriction, including | |
10 | * without limitation the rights to use, copy, modify, merge, publish, | |
11 | * distribute, sub license, and/or sell copies of the Software, and to | |
12 | * permit persons to whom the Software is furnished to do so, subject to | |
13 | * the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice (including the | |
16 | * next paragraph) shall be included in all copies or substantial portions | |
17 | * of the Software. | |
18 | * | |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL | |
22 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, | |
23 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
24 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | |
25 | * USE OR OTHER DEALINGS IN THE SOFTWARE. | |
26 | * | |
27 | **************************************************************************/ | |
28 | ||
25d0479a JP |
29 | #define pr_fmt(fmt) "[TTM] " fmt |
30 | ||
760285e7 DH |
31 | #include <drm/ttm/ttm_memory.h> |
32 | #include <drm/ttm/ttm_module.h> | |
ba4e7d97 TH |
33 | #include <linux/spinlock.h> |
34 | #include <linux/sched.h> | |
35 | #include <linux/wait.h> | |
36 | #include <linux/mm.h> | |
37 | #include <linux/module.h> | |
5a0e3ad6 | 38 | #include <linux/slab.h> |
ec3fe391 | 39 | #include <linux/swap.h> |
d099fc8f | 40 | #include <drm/ttm/ttm_pool.h> |
ba4e7d97 | 41 | |
ba4e7d97 TH |
42 | #define TTM_MEMORY_ALLOC_RETRIES 4 |
43 | ||
27eb1fa9 CK |
44 | struct ttm_mem_global ttm_mem_glob; |
45 | EXPORT_SYMBOL(ttm_mem_glob); | |
46 | ||
5fd9cbad TH |
47 | struct ttm_mem_zone { |
48 | struct kobject kobj; | |
49 | struct ttm_mem_global *glob; | |
50 | const char *name; | |
51 | uint64_t zone_mem; | |
52 | uint64_t emer_mem; | |
53 | uint64_t max_mem; | |
54 | uint64_t swap_limit; | |
55 | uint64_t used_mem; | |
56 | }; | |
57 | ||
58 | static struct attribute ttm_mem_sys = { | |
59 | .name = "zone_memory", | |
60 | .mode = S_IRUGO | |
61 | }; | |
62 | static struct attribute ttm_mem_emer = { | |
63 | .name = "emergency_memory", | |
64 | .mode = S_IRUGO | S_IWUSR | |
65 | }; | |
66 | static struct attribute ttm_mem_max = { | |
67 | .name = "available_memory", | |
68 | .mode = S_IRUGO | S_IWUSR | |
69 | }; | |
70 | static struct attribute ttm_mem_swap = { | |
71 | .name = "swap_limit", | |
72 | .mode = S_IRUGO | S_IWUSR | |
73 | }; | |
74 | static struct attribute ttm_mem_used = { | |
75 | .name = "used_memory", | |
76 | .mode = S_IRUGO | |
77 | }; | |
78 | ||
79 | static void ttm_mem_zone_kobj_release(struct kobject *kobj) | |
80 | { | |
81 | struct ttm_mem_zone *zone = | |
82 | container_of(kobj, struct ttm_mem_zone, kobj); | |
83 | ||
71ec9094 | 84 | pr_info("Zone %7s: Used memory at exit: %llu KiB\n", |
25d0479a | 85 | zone->name, (unsigned long long)zone->used_mem >> 10); |
5fd9cbad TH |
86 | kfree(zone); |
87 | } | |
88 | ||
89 | static ssize_t ttm_mem_zone_show(struct kobject *kobj, | |
90 | struct attribute *attr, | |
91 | char *buffer) | |
92 | { | |
93 | struct ttm_mem_zone *zone = | |
94 | container_of(kobj, struct ttm_mem_zone, kobj); | |
95 | uint64_t val = 0; | |
96 | ||
97 | spin_lock(&zone->glob->lock); | |
98 | if (attr == &ttm_mem_sys) | |
99 | val = zone->zone_mem; | |
100 | else if (attr == &ttm_mem_emer) | |
101 | val = zone->emer_mem; | |
102 | else if (attr == &ttm_mem_max) | |
103 | val = zone->max_mem; | |
104 | else if (attr == &ttm_mem_swap) | |
105 | val = zone->swap_limit; | |
106 | else if (attr == &ttm_mem_used) | |
107 | val = zone->used_mem; | |
108 | spin_unlock(&zone->glob->lock); | |
109 | ||
110 | return snprintf(buffer, PAGE_SIZE, "%llu\n", | |
111 | (unsigned long long) val >> 10); | |
112 | } | |
113 | ||
114 | static void ttm_check_swapping(struct ttm_mem_global *glob); | |
115 | ||
116 | static ssize_t ttm_mem_zone_store(struct kobject *kobj, | |
117 | struct attribute *attr, | |
118 | const char *buffer, | |
119 | size_t size) | |
120 | { | |
121 | struct ttm_mem_zone *zone = | |
122 | container_of(kobj, struct ttm_mem_zone, kobj); | |
123 | int chars; | |
124 | unsigned long val; | |
125 | uint64_t val64; | |
126 | ||
127 | chars = sscanf(buffer, "%lu", &val); | |
128 | if (chars == 0) | |
129 | return size; | |
130 | ||
131 | val64 = val; | |
132 | val64 <<= 10; | |
133 | ||
134 | spin_lock(&zone->glob->lock); | |
135 | if (val64 > zone->zone_mem) | |
136 | val64 = zone->zone_mem; | |
137 | if (attr == &ttm_mem_emer) { | |
138 | zone->emer_mem = val64; | |
139 | if (zone->max_mem > val64) | |
140 | zone->max_mem = val64; | |
141 | } else if (attr == &ttm_mem_max) { | |
142 | zone->max_mem = val64; | |
143 | if (zone->emer_mem < val64) | |
144 | zone->emer_mem = val64; | |
145 | } else if (attr == &ttm_mem_swap) | |
146 | zone->swap_limit = val64; | |
147 | spin_unlock(&zone->glob->lock); | |
148 | ||
149 | ttm_check_swapping(zone->glob); | |
150 | ||
151 | return size; | |
152 | } | |
153 | ||
154 | static struct attribute *ttm_mem_zone_attrs[] = { | |
155 | &ttm_mem_sys, | |
156 | &ttm_mem_emer, | |
157 | &ttm_mem_max, | |
158 | &ttm_mem_swap, | |
159 | &ttm_mem_used, | |
160 | NULL | |
161 | }; | |
162 | ||
52cf25d0 | 163 | static const struct sysfs_ops ttm_mem_zone_ops = { |
5fd9cbad TH |
164 | .show = &ttm_mem_zone_show, |
165 | .store = &ttm_mem_zone_store | |
166 | }; | |
167 | ||
168 | static struct kobj_type ttm_mem_zone_kobj_type = { | |
169 | .release = &ttm_mem_zone_kobj_release, | |
170 | .sysfs_ops = &ttm_mem_zone_ops, | |
171 | .default_attrs = ttm_mem_zone_attrs, | |
172 | }; | |
173 | ||
ec3fe391 RH |
174 | static struct attribute ttm_mem_global_lower_mem_limit = { |
175 | .name = "lower_mem_limit", | |
176 | .mode = S_IRUGO | S_IWUSR | |
177 | }; | |
178 | ||
179 | static ssize_t ttm_mem_global_show(struct kobject *kobj, | |
180 | struct attribute *attr, | |
181 | char *buffer) | |
182 | { | |
183 | struct ttm_mem_global *glob = | |
184 | container_of(kobj, struct ttm_mem_global, kobj); | |
185 | uint64_t val = 0; | |
186 | ||
187 | spin_lock(&glob->lock); | |
188 | val = glob->lower_mem_limit; | |
189 | spin_unlock(&glob->lock); | |
190 | /* convert from number of pages to KB */ | |
191 | val <<= (PAGE_SHIFT - 10); | |
192 | return snprintf(buffer, PAGE_SIZE, "%llu\n", | |
193 | (unsigned long long) val); | |
194 | } | |
195 | ||
196 | static ssize_t ttm_mem_global_store(struct kobject *kobj, | |
197 | struct attribute *attr, | |
198 | const char *buffer, | |
199 | size_t size) | |
200 | { | |
201 | int chars; | |
202 | uint64_t val64; | |
203 | unsigned long val; | |
204 | struct ttm_mem_global *glob = | |
205 | container_of(kobj, struct ttm_mem_global, kobj); | |
206 | ||
207 | chars = sscanf(buffer, "%lu", &val); | |
208 | if (chars == 0) | |
209 | return size; | |
210 | ||
211 | val64 = val; | |
212 | /* convert from KB to number of pages */ | |
213 | val64 >>= (PAGE_SHIFT - 10); | |
214 | ||
215 | spin_lock(&glob->lock); | |
216 | glob->lower_mem_limit = val64; | |
217 | spin_unlock(&glob->lock); | |
218 | ||
219 | return size; | |
220 | } | |
221 | ||
ec3fe391 RH |
222 | static struct attribute *ttm_mem_global_attrs[] = { |
223 | &ttm_mem_global_lower_mem_limit, | |
224 | NULL | |
225 | }; | |
226 | ||
227 | static const struct sysfs_ops ttm_mem_global_ops = { | |
228 | .show = &ttm_mem_global_show, | |
229 | .store = &ttm_mem_global_store, | |
230 | }; | |
231 | ||
5fd9cbad | 232 | static struct kobj_type ttm_mem_glob_kobj_type = { |
ec3fe391 RH |
233 | .sysfs_ops = &ttm_mem_global_ops, |
234 | .default_attrs = ttm_mem_global_attrs, | |
5fd9cbad TH |
235 | }; |
236 | ||
237 | static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, | |
238 | bool from_wq, uint64_t extra) | |
239 | { | |
240 | unsigned int i; | |
241 | struct ttm_mem_zone *zone; | |
242 | uint64_t target; | |
243 | ||
244 | for (i = 0; i < glob->num_zones; ++i) { | |
245 | zone = glob->zones[i]; | |
246 | ||
247 | if (from_wq) | |
248 | target = zone->swap_limit; | |
249 | else if (capable(CAP_SYS_ADMIN)) | |
250 | target = zone->emer_mem; | |
251 | else | |
252 | target = zone->max_mem; | |
253 | ||
254 | target = (extra > target) ? 0ULL : target; | |
255 | ||
256 | if (zone->used_mem > target) | |
257 | return true; | |
258 | } | |
259 | return false; | |
260 | } | |
261 | ||
92fdb97d | 262 | /* |
ba4e7d97 TH |
263 | * At this point we only support a single shrink callback. |
264 | * Extend this if needed, perhaps using a linked list of callbacks. | |
265 | * Note that this function is reentrant: | |
266 | * many threads may try to swap out at any given time. | |
267 | */ | |
268 | ||
5fd9cbad | 269 | static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq, |
dc947770 | 270 | uint64_t extra, struct ttm_operation_ctx *ctx) |
ba4e7d97 TH |
271 | { |
272 | int ret; | |
ba4e7d97 TH |
273 | |
274 | spin_lock(&glob->lock); | |
ba4e7d97 | 275 | |
5fd9cbad | 276 | while (ttm_zones_above_swap_target(glob, from_wq, extra)) { |
ba4e7d97 | 277 | spin_unlock(&glob->lock); |
4561b366 | 278 | ret = ttm_bo_swapout(ctx); |
ba4e7d97 TH |
279 | spin_lock(&glob->lock); |
280 | if (unlikely(ret != 0)) | |
a6c26af8 | 281 | break; |
ba4e7d97 | 282 | } |
a6c26af8 | 283 | |
ba4e7d97 TH |
284 | spin_unlock(&glob->lock); |
285 | } | |
286 | ||
287 | static void ttm_shrink_work(struct work_struct *work) | |
288 | { | |
dc947770 RH |
289 | struct ttm_operation_ctx ctx = { |
290 | .interruptible = false, | |
291 | .no_wait_gpu = false | |
292 | }; | |
ba4e7d97 TH |
293 | struct ttm_mem_global *glob = |
294 | container_of(work, struct ttm_mem_global, work); | |
295 | ||
dc947770 | 296 | ttm_shrink(glob, true, 0ULL, &ctx); |
ba4e7d97 TH |
297 | } |
298 | ||
5fd9cbad TH |
299 | static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob, |
300 | const struct sysinfo *si) | |
301 | { | |
302 | struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL); | |
303 | uint64_t mem; | |
759e4f83 | 304 | int ret; |
5fd9cbad TH |
305 | |
306 | if (unlikely(!zone)) | |
307 | return -ENOMEM; | |
308 | ||
309 | mem = si->totalram - si->totalhigh; | |
310 | mem *= si->mem_unit; | |
311 | ||
312 | zone->name = "kernel"; | |
313 | zone->zone_mem = mem; | |
314 | zone->max_mem = mem >> 1; | |
315 | zone->emer_mem = (mem >> 1) + (mem >> 2); | |
316 | zone->swap_limit = zone->max_mem - (mem >> 3); | |
317 | zone->used_mem = 0; | |
318 | zone->glob = glob; | |
319 | glob->zone_kernel = zone; | |
b642ed06 RD |
320 | ret = kobject_init_and_add( |
321 | &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name); | |
759e4f83 TH |
322 | if (unlikely(ret != 0)) { |
323 | kobject_put(&zone->kobj); | |
324 | return ret; | |
325 | } | |
326 | glob->zones[glob->num_zones++] = zone; | |
327 | return 0; | |
5fd9cbad TH |
328 | } |
329 | ||
330 | #ifdef CONFIG_HIGHMEM | |
331 | static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob, | |
332 | const struct sysinfo *si) | |
333 | { | |
46a79fa0 | 334 | struct ttm_mem_zone *zone; |
5fd9cbad | 335 | uint64_t mem; |
759e4f83 | 336 | int ret; |
5fd9cbad | 337 | |
5fd9cbad TH |
338 | if (si->totalhigh == 0) |
339 | return 0; | |
340 | ||
46a79fa0 DC |
341 | zone = kzalloc(sizeof(*zone), GFP_KERNEL); |
342 | if (unlikely(!zone)) | |
343 | return -ENOMEM; | |
344 | ||
5fd9cbad TH |
345 | mem = si->totalram; |
346 | mem *= si->mem_unit; | |
347 | ||
348 | zone->name = "highmem"; | |
349 | zone->zone_mem = mem; | |
350 | zone->max_mem = mem >> 1; | |
351 | zone->emer_mem = (mem >> 1) + (mem >> 2); | |
352 | zone->swap_limit = zone->max_mem - (mem >> 3); | |
353 | zone->used_mem = 0; | |
354 | zone->glob = glob; | |
355 | glob->zone_highmem = zone; | |
b642ed06 | 356 | ret = kobject_init_and_add( |
109ab909 KC |
357 | &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s", |
358 | zone->name); | |
759e4f83 TH |
359 | if (unlikely(ret != 0)) { |
360 | kobject_put(&zone->kobj); | |
361 | return ret; | |
362 | } | |
363 | glob->zones[glob->num_zones++] = zone; | |
364 | return 0; | |
5fd9cbad TH |
365 | } |
366 | #else | |
367 | static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob, | |
368 | const struct sysinfo *si) | |
369 | { | |
370 | struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL); | |
371 | uint64_t mem; | |
759e4f83 | 372 | int ret; |
5fd9cbad TH |
373 | |
374 | if (unlikely(!zone)) | |
375 | return -ENOMEM; | |
376 | ||
377 | mem = si->totalram; | |
378 | mem *= si->mem_unit; | |
379 | ||
380 | /** | |
381 | * No special dma32 zone needed. | |
382 | */ | |
383 | ||
ec42a6e7 DA |
384 | if (mem <= ((uint64_t) 1ULL << 32)) { |
385 | kfree(zone); | |
5fd9cbad | 386 | return 0; |
ec42a6e7 | 387 | } |
5fd9cbad TH |
388 | |
389 | /* | |
390 | * Limit max dma32 memory to 4GB for now | |
391 | * until we can figure out how big this | |
392 | * zone really is. | |
393 | */ | |
394 | ||
395 | mem = ((uint64_t) 1ULL << 32); | |
396 | zone->name = "dma32"; | |
397 | zone->zone_mem = mem; | |
398 | zone->max_mem = mem >> 1; | |
399 | zone->emer_mem = (mem >> 1) + (mem >> 2); | |
400 | zone->swap_limit = zone->max_mem - (mem >> 3); | |
401 | zone->used_mem = 0; | |
402 | zone->glob = glob; | |
403 | glob->zone_dma32 = zone; | |
b642ed06 RD |
404 | ret = kobject_init_and_add( |
405 | &zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name); | |
759e4f83 TH |
406 | if (unlikely(ret != 0)) { |
407 | kobject_put(&zone->kobj); | |
408 | return ret; | |
409 | } | |
410 | glob->zones[glob->num_zones++] = zone; | |
411 | return 0; | |
5fd9cbad TH |
412 | } |
413 | #endif | |
414 | ||
ba4e7d97 TH |
415 | int ttm_mem_global_init(struct ttm_mem_global *glob) |
416 | { | |
417 | struct sysinfo si; | |
5fd9cbad TH |
418 | int ret; |
419 | int i; | |
420 | struct ttm_mem_zone *zone; | |
ba4e7d97 TH |
421 | |
422 | spin_lock_init(&glob->lock); | |
423 | glob->swap_queue = create_singlethread_workqueue("ttm_swap"); | |
424 | INIT_WORK(&glob->work, ttm_shrink_work); | |
b642ed06 RD |
425 | ret = kobject_init_and_add( |
426 | &glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting"); | |
759e4f83 TH |
427 | if (unlikely(ret != 0)) { |
428 | kobject_put(&glob->kobj); | |
429 | return ret; | |
430 | } | |
ba4e7d97 TH |
431 | |
432 | si_meminfo(&si); | |
433 | ||
ec3fe391 RH |
434 | /* set it as 0 by default to keep original behavior of OOM */ |
435 | glob->lower_mem_limit = 0; | |
436 | ||
5fd9cbad TH |
437 | ret = ttm_mem_init_kernel_zone(glob, &si); |
438 | if (unlikely(ret != 0)) | |
439 | goto out_no_zone; | |
440 | #ifdef CONFIG_HIGHMEM | |
441 | ret = ttm_mem_init_highmem_zone(glob, &si); | |
442 | if (unlikely(ret != 0)) | |
443 | goto out_no_zone; | |
444 | #else | |
445 | ret = ttm_mem_init_dma32_zone(glob, &si); | |
446 | if (unlikely(ret != 0)) | |
447 | goto out_no_zone; | |
448 | #endif | |
449 | for (i = 0; i < glob->num_zones; ++i) { | |
450 | zone = glob->zones[i]; | |
71ec9094 | 451 | pr_info("Zone %7s: Available graphics memory: %llu KiB\n", |
25d0479a | 452 | zone->name, (unsigned long long)zone->max_mem >> 10); |
5fd9cbad | 453 | } |
256dd44b | 454 | ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE)); |
ba4e7d97 | 455 | return 0; |
5fd9cbad TH |
456 | out_no_zone: |
457 | ttm_mem_global_release(glob); | |
458 | return ret; | |
ba4e7d97 | 459 | } |
ba4e7d97 TH |
460 | |
461 | void ttm_mem_global_release(struct ttm_mem_global *glob) | |
462 | { | |
5fd9cbad | 463 | struct ttm_mem_zone *zone; |
bd426411 | 464 | unsigned int i; |
5fd9cbad | 465 | |
1403b1a3 | 466 | /* let the page allocator first stop the shrink work. */ |
d099fc8f | 467 | ttm_pool_mgr_fini(); |
1403b1a3 | 468 | |
ba4e7d97 TH |
469 | flush_workqueue(glob->swap_queue); |
470 | destroy_workqueue(glob->swap_queue); | |
471 | glob->swap_queue = NULL; | |
5fd9cbad TH |
472 | for (i = 0; i < glob->num_zones; ++i) { |
473 | zone = glob->zones[i]; | |
474 | kobject_del(&zone->kobj); | |
475 | kobject_put(&zone->kobj); | |
bd426411 | 476 | } |
5fd9cbad TH |
477 | kobject_del(&glob->kobj); |
478 | kobject_put(&glob->kobj); | |
bd426411 | 479 | memset(glob, 0, sizeof(*glob)); |
ba4e7d97 | 480 | } |
ba4e7d97 | 481 | |
5fd9cbad | 482 | static void ttm_check_swapping(struct ttm_mem_global *glob) |
ba4e7d97 | 483 | { |
5fd9cbad TH |
484 | bool needs_swapping = false; |
485 | unsigned int i; | |
486 | struct ttm_mem_zone *zone; | |
ba4e7d97 TH |
487 | |
488 | spin_lock(&glob->lock); | |
5fd9cbad TH |
489 | for (i = 0; i < glob->num_zones; ++i) { |
490 | zone = glob->zones[i]; | |
491 | if (zone->used_mem > zone->swap_limit) { | |
492 | needs_swapping = true; | |
493 | break; | |
494 | } | |
495 | } | |
496 | ||
ba4e7d97 TH |
497 | spin_unlock(&glob->lock); |
498 | ||
499 | if (unlikely(needs_swapping)) | |
500 | (void)queue_work(glob->swap_queue, &glob->work); | |
501 | ||
502 | } | |
503 | ||
5fd9cbad TH |
504 | static void ttm_mem_global_free_zone(struct ttm_mem_global *glob, |
505 | struct ttm_mem_zone *single_zone, | |
506 | uint64_t amount) | |
ba4e7d97 | 507 | { |
5fd9cbad TH |
508 | unsigned int i; |
509 | struct ttm_mem_zone *zone; | |
510 | ||
ba4e7d97 | 511 | spin_lock(&glob->lock); |
5fd9cbad TH |
512 | for (i = 0; i < glob->num_zones; ++i) { |
513 | zone = glob->zones[i]; | |
514 | if (single_zone && zone != single_zone) | |
515 | continue; | |
516 | zone->used_mem -= amount; | |
517 | } | |
ba4e7d97 TH |
518 | spin_unlock(&glob->lock); |
519 | } | |
520 | ||
5fd9cbad TH |
521 | void ttm_mem_global_free(struct ttm_mem_global *glob, |
522 | uint64_t amount) | |
523 | { | |
3c889912 | 524 | return ttm_mem_global_free_zone(glob, glob->zone_kernel, amount); |
5fd9cbad | 525 | } |
4bfd75cb | 526 | EXPORT_SYMBOL(ttm_mem_global_free); |
5fd9cbad | 527 | |
ec3fe391 RH |
528 | /* |
529 | * check if the available mem is under lower memory limit | |
530 | * | |
531 | * a. if no swap disk at all or free swap space is under swap_mem_limit | |
532 | * but available system mem is bigger than sys_mem_limit, allow TTM | |
533 | * allocation; | |
534 | * | |
535 | * b. if the available system mem is less than sys_mem_limit but free | |
536 | * swap disk is bigger than swap_mem_limit, allow TTM allocation. | |
537 | */ | |
538 | bool | |
539 | ttm_check_under_lowerlimit(struct ttm_mem_global *glob, | |
540 | uint64_t num_pages, | |
541 | struct ttm_operation_ctx *ctx) | |
542 | { | |
543 | int64_t available; | |
544 | ||
c44dfe4d CK |
545 | /* We allow over commit during suspend */ |
546 | if (ctx->force_alloc) | |
ec3fe391 RH |
547 | return false; |
548 | ||
549 | available = get_nr_swap_pages() + si_mem_available(); | |
550 | available -= num_pages; | |
551 | if (available < glob->lower_mem_limit) | |
552 | return true; | |
553 | ||
554 | return false; | |
555 | } | |
ec3fe391 | 556 | |
ba4e7d97 | 557 | static int ttm_mem_global_reserve(struct ttm_mem_global *glob, |
5fd9cbad TH |
558 | struct ttm_mem_zone *single_zone, |
559 | uint64_t amount, bool reserve) | |
ba4e7d97 TH |
560 | { |
561 | uint64_t limit; | |
ba4e7d97 | 562 | int ret = -ENOMEM; |
5fd9cbad TH |
563 | unsigned int i; |
564 | struct ttm_mem_zone *zone; | |
ba4e7d97 TH |
565 | |
566 | spin_lock(&glob->lock); | |
5fd9cbad TH |
567 | for (i = 0; i < glob->num_zones; ++i) { |
568 | zone = glob->zones[i]; | |
569 | if (single_zone && zone != single_zone) | |
570 | continue; | |
ba4e7d97 | 571 | |
5fd9cbad TH |
572 | limit = (capable(CAP_SYS_ADMIN)) ? |
573 | zone->emer_mem : zone->max_mem; | |
ba4e7d97 | 574 | |
5fd9cbad TH |
575 | if (zone->used_mem > limit) |
576 | goto out_unlock; | |
577 | } | |
ba4e7d97 TH |
578 | |
579 | if (reserve) { | |
5fd9cbad TH |
580 | for (i = 0; i < glob->num_zones; ++i) { |
581 | zone = glob->zones[i]; | |
582 | if (single_zone && zone != single_zone) | |
583 | continue; | |
584 | zone->used_mem += amount; | |
585 | } | |
ba4e7d97 | 586 | } |
5fd9cbad | 587 | |
ba4e7d97 TH |
588 | ret = 0; |
589 | out_unlock: | |
590 | spin_unlock(&glob->lock); | |
591 | ttm_check_swapping(glob); | |
592 | ||
593 | return ret; | |
594 | } | |
595 | ||
5fd9cbad TH |
596 | |
597 | static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob, | |
598 | struct ttm_mem_zone *single_zone, | |
599 | uint64_t memory, | |
279c01f6 | 600 | struct ttm_operation_ctx *ctx) |
ba4e7d97 TH |
601 | { |
602 | int count = TTM_MEMORY_ALLOC_RETRIES; | |
603 | ||
5fd9cbad TH |
604 | while (unlikely(ttm_mem_global_reserve(glob, |
605 | single_zone, | |
606 | memory, true) | |
ba4e7d97 | 607 | != 0)) { |
279c01f6 | 608 | if (ctx->no_wait_gpu) |
ba4e7d97 TH |
609 | return -ENOMEM; |
610 | if (unlikely(count-- == 0)) | |
611 | return -ENOMEM; | |
dc947770 | 612 | ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx); |
ba4e7d97 TH |
613 | } |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
5fd9cbad | 618 | int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory, |
279c01f6 | 619 | struct ttm_operation_ctx *ctx) |
5fd9cbad TH |
620 | { |
621 | /** | |
622 | * Normal allocations of kernel memory are registered in | |
3c889912 | 623 | * the kernel zone. |
5fd9cbad TH |
624 | */ |
625 | ||
3c889912 | 626 | return ttm_mem_global_alloc_zone(glob, glob->zone_kernel, memory, ctx); |
5fd9cbad | 627 | } |
4bfd75cb | 628 | EXPORT_SYMBOL(ttm_mem_global_alloc); |
5fd9cbad TH |
629 | |
630 | int ttm_mem_global_alloc_page(struct ttm_mem_global *glob, | |
9de2fb99 RH |
631 | struct page *page, uint64_t size, |
632 | struct ttm_operation_ctx *ctx) | |
5fd9cbad | 633 | { |
5fd9cbad TH |
634 | struct ttm_mem_zone *zone = NULL; |
635 | ||
636 | /** | |
637 | * Page allocations may be registed in a single zone | |
638 | * only if highmem or !dma32. | |
639 | */ | |
640 | ||
641 | #ifdef CONFIG_HIGHMEM | |
642 | if (PageHighMem(page) && glob->zone_highmem != NULL) | |
643 | zone = glob->zone_highmem; | |
644 | #else | |
645 | if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL) | |
646 | zone = glob->zone_kernel; | |
647 | #endif | |
9de2fb99 | 648 | return ttm_mem_global_alloc_zone(glob, zone, size, ctx); |
5fd9cbad TH |
649 | } |
650 | ||
d188bfa5 CK |
651 | void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page, |
652 | uint64_t size) | |
5fd9cbad TH |
653 | { |
654 | struct ttm_mem_zone *zone = NULL; | |
655 | ||
656 | #ifdef CONFIG_HIGHMEM | |
657 | if (PageHighMem(page) && glob->zone_highmem != NULL) | |
658 | zone = glob->zone_highmem; | |
659 | #else | |
660 | if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL) | |
661 | zone = glob->zone_kernel; | |
662 | #endif | |
d188bfa5 | 663 | ttm_mem_global_free_zone(glob, zone, size); |
5fd9cbad TH |
664 | } |
665 | ||
ba4e7d97 TH |
666 | size_t ttm_round_pot(size_t size) |
667 | { | |
668 | if ((size & (size - 1)) == 0) | |
669 | return size; | |
670 | else if (size > PAGE_SIZE) | |
671 | return PAGE_ALIGN(size); | |
672 | else { | |
673 | size_t tmp_size = 4; | |
674 | ||
675 | while (tmp_size < size) | |
676 | tmp_size <<= 1; | |
677 | ||
678 | return tmp_size; | |
679 | } | |
680 | return 0; | |
681 | } | |
4bfd75cb | 682 | EXPORT_SYMBOL(ttm_round_pot); |