• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
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 
29 #define pr_fmt(fmt) "[TTM] " fmt
30 
31 #include <drm/ttm/ttm_memory.h>
32 #include <drm/ttm/ttm_module.h>
33 #include <drm/ttm/ttm_page_alloc.h>
34 #include <linux/spinlock.h>
35 #include <linux/sched.h>
36 #include <linux/wait.h>
37 #include <linux/mm.h>
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/swap.h>
41 
42 #define TTM_MEMORY_ALLOC_RETRIES 4
43 
44 struct ttm_mem_zone {
45 	struct kobject kobj;
46 	struct ttm_mem_global *glob;
47 	const char *name;
48 	uint64_t zone_mem;
49 	uint64_t emer_mem;
50 	uint64_t max_mem;
51 	uint64_t swap_limit;
52 	uint64_t used_mem;
53 };
54 
55 static struct attribute ttm_mem_sys = {
56 	.name = "zone_memory",
57 	.mode = S_IRUGO
58 };
59 static struct attribute ttm_mem_emer = {
60 	.name = "emergency_memory",
61 	.mode = S_IRUGO | S_IWUSR
62 };
63 static struct attribute ttm_mem_max = {
64 	.name = "available_memory",
65 	.mode = S_IRUGO | S_IWUSR
66 };
67 static struct attribute ttm_mem_swap = {
68 	.name = "swap_limit",
69 	.mode = S_IRUGO | S_IWUSR
70 };
71 static struct attribute ttm_mem_used = {
72 	.name = "used_memory",
73 	.mode = S_IRUGO
74 };
75 
ttm_mem_zone_kobj_release(struct kobject * kobj)76 static void ttm_mem_zone_kobj_release(struct kobject *kobj)
77 {
78 	struct ttm_mem_zone *zone =
79 		container_of(kobj, struct ttm_mem_zone, kobj);
80 
81 	pr_info("Zone %7s: Used memory at exit: %llu kiB\n",
82 		zone->name, (unsigned long long)zone->used_mem >> 10);
83 	kfree(zone);
84 }
85 
ttm_mem_zone_show(struct kobject * kobj,struct attribute * attr,char * buffer)86 static ssize_t ttm_mem_zone_show(struct kobject *kobj,
87 				 struct attribute *attr,
88 				 char *buffer)
89 {
90 	struct ttm_mem_zone *zone =
91 		container_of(kobj, struct ttm_mem_zone, kobj);
92 	uint64_t val = 0;
93 
94 	spin_lock(&zone->glob->lock);
95 	if (attr == &ttm_mem_sys)
96 		val = zone->zone_mem;
97 	else if (attr == &ttm_mem_emer)
98 		val = zone->emer_mem;
99 	else if (attr == &ttm_mem_max)
100 		val = zone->max_mem;
101 	else if (attr == &ttm_mem_swap)
102 		val = zone->swap_limit;
103 	else if (attr == &ttm_mem_used)
104 		val = zone->used_mem;
105 	spin_unlock(&zone->glob->lock);
106 
107 	return snprintf(buffer, PAGE_SIZE, "%llu\n",
108 			(unsigned long long) val >> 10);
109 }
110 
111 static void ttm_check_swapping(struct ttm_mem_global *glob);
112 
ttm_mem_zone_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t size)113 static ssize_t ttm_mem_zone_store(struct kobject *kobj,
114 				  struct attribute *attr,
115 				  const char *buffer,
116 				  size_t size)
117 {
118 	struct ttm_mem_zone *zone =
119 		container_of(kobj, struct ttm_mem_zone, kobj);
120 	int chars;
121 	unsigned long val;
122 	uint64_t val64;
123 
124 	chars = sscanf(buffer, "%lu", &val);
125 	if (chars == 0)
126 		return size;
127 
128 	val64 = val;
129 	val64 <<= 10;
130 
131 	spin_lock(&zone->glob->lock);
132 	if (val64 > zone->zone_mem)
133 		val64 = zone->zone_mem;
134 	if (attr == &ttm_mem_emer) {
135 		zone->emer_mem = val64;
136 		if (zone->max_mem > val64)
137 			zone->max_mem = val64;
138 	} else if (attr == &ttm_mem_max) {
139 		zone->max_mem = val64;
140 		if (zone->emer_mem < val64)
141 			zone->emer_mem = val64;
142 	} else if (attr == &ttm_mem_swap)
143 		zone->swap_limit = val64;
144 	spin_unlock(&zone->glob->lock);
145 
146 	ttm_check_swapping(zone->glob);
147 
148 	return size;
149 }
150 
151 static struct attribute *ttm_mem_zone_attrs[] = {
152 	&ttm_mem_sys,
153 	&ttm_mem_emer,
154 	&ttm_mem_max,
155 	&ttm_mem_swap,
156 	&ttm_mem_used,
157 	NULL
158 };
159 
160 static const struct sysfs_ops ttm_mem_zone_ops = {
161 	.show = &ttm_mem_zone_show,
162 	.store = &ttm_mem_zone_store
163 };
164 
165 static struct kobj_type ttm_mem_zone_kobj_type = {
166 	.release = &ttm_mem_zone_kobj_release,
167 	.sysfs_ops = &ttm_mem_zone_ops,
168 	.default_attrs = ttm_mem_zone_attrs,
169 };
170 
171 static struct attribute ttm_mem_global_lower_mem_limit = {
172 	.name = "lower_mem_limit",
173 	.mode = S_IRUGO | S_IWUSR
174 };
175 
ttm_mem_global_show(struct kobject * kobj,struct attribute * attr,char * buffer)176 static ssize_t ttm_mem_global_show(struct kobject *kobj,
177 				 struct attribute *attr,
178 				 char *buffer)
179 {
180 	struct ttm_mem_global *glob =
181 		container_of(kobj, struct ttm_mem_global, kobj);
182 	uint64_t val = 0;
183 
184 	spin_lock(&glob->lock);
185 	val = glob->lower_mem_limit;
186 	spin_unlock(&glob->lock);
187 	/* convert from number of pages to KB */
188 	val <<= (PAGE_SHIFT - 10);
189 	return snprintf(buffer, PAGE_SIZE, "%llu\n",
190 			(unsigned long long) val);
191 }
192 
ttm_mem_global_store(struct kobject * kobj,struct attribute * attr,const char * buffer,size_t size)193 static ssize_t ttm_mem_global_store(struct kobject *kobj,
194 				  struct attribute *attr,
195 				  const char *buffer,
196 				  size_t size)
197 {
198 	int chars;
199 	uint64_t val64;
200 	unsigned long val;
201 	struct ttm_mem_global *glob =
202 		container_of(kobj, struct ttm_mem_global, kobj);
203 
204 	chars = sscanf(buffer, "%lu", &val);
205 	if (chars == 0)
206 		return size;
207 
208 	val64 = val;
209 	/* convert from KB to number of pages */
210 	val64 >>= (PAGE_SHIFT - 10);
211 
212 	spin_lock(&glob->lock);
213 	glob->lower_mem_limit = val64;
214 	spin_unlock(&glob->lock);
215 
216 	return size;
217 }
218 
219 static struct attribute *ttm_mem_global_attrs[] = {
220 	&ttm_mem_global_lower_mem_limit,
221 	NULL
222 };
223 
224 static const struct sysfs_ops ttm_mem_global_ops = {
225 	.show = &ttm_mem_global_show,
226 	.store = &ttm_mem_global_store,
227 };
228 
229 static struct kobj_type ttm_mem_glob_kobj_type = {
230 	.sysfs_ops = &ttm_mem_global_ops,
231 	.default_attrs = ttm_mem_global_attrs,
232 };
233 
ttm_zones_above_swap_target(struct ttm_mem_global * glob,bool from_wq,uint64_t extra)234 static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
235 					bool from_wq, uint64_t extra)
236 {
237 	unsigned int i;
238 	struct ttm_mem_zone *zone;
239 	uint64_t target;
240 
241 	for (i = 0; i < glob->num_zones; ++i) {
242 		zone = glob->zones[i];
243 
244 		if (from_wq)
245 			target = zone->swap_limit;
246 		else if (capable(CAP_SYS_ADMIN))
247 			target = zone->emer_mem;
248 		else
249 			target = zone->max_mem;
250 
251 		target = (extra > target) ? 0ULL : target;
252 
253 		if (zone->used_mem > target)
254 			return true;
255 	}
256 	return false;
257 }
258 
259 /**
260  * At this point we only support a single shrink callback.
261  * Extend this if needed, perhaps using a linked list of callbacks.
262  * Note that this function is reentrant:
263  * many threads may try to swap out at any given time.
264  */
265 
ttm_shrink(struct ttm_mem_global * glob,bool from_wq,uint64_t extra,struct ttm_operation_ctx * ctx)266 static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
267 			uint64_t extra, struct ttm_operation_ctx *ctx)
268 {
269 	int ret;
270 
271 	spin_lock(&glob->lock);
272 
273 	while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
274 		spin_unlock(&glob->lock);
275 		ret = ttm_bo_swapout(glob->bo_glob, ctx);
276 		spin_lock(&glob->lock);
277 		if (unlikely(ret != 0))
278 			break;
279 	}
280 
281 	spin_unlock(&glob->lock);
282 }
283 
ttm_shrink_work(struct work_struct * work)284 static void ttm_shrink_work(struct work_struct *work)
285 {
286 	struct ttm_operation_ctx ctx = {
287 		.interruptible = false,
288 		.no_wait_gpu = false
289 	};
290 	struct ttm_mem_global *glob =
291 	    container_of(work, struct ttm_mem_global, work);
292 
293 	ttm_shrink(glob, true, 0ULL, &ctx);
294 }
295 
ttm_mem_init_kernel_zone(struct ttm_mem_global * glob,const struct sysinfo * si)296 static int ttm_mem_init_kernel_zone(struct ttm_mem_global *glob,
297 				    const struct sysinfo *si)
298 {
299 	struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
300 	uint64_t mem;
301 	int ret;
302 
303 	if (unlikely(!zone))
304 		return -ENOMEM;
305 
306 	mem = si->totalram - si->totalhigh;
307 	mem *= si->mem_unit;
308 
309 	zone->name = "kernel";
310 	zone->zone_mem = mem;
311 	zone->max_mem = mem >> 1;
312 	zone->emer_mem = (mem >> 1) + (mem >> 2);
313 	zone->swap_limit = zone->max_mem - (mem >> 3);
314 	zone->used_mem = 0;
315 	zone->glob = glob;
316 	glob->zone_kernel = zone;
317 	ret = kobject_init_and_add(
318 		&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
319 	if (unlikely(ret != 0)) {
320 		kobject_put(&zone->kobj);
321 		return ret;
322 	}
323 	glob->zones[glob->num_zones++] = zone;
324 	return 0;
325 }
326 
327 #ifdef CONFIG_HIGHMEM
ttm_mem_init_highmem_zone(struct ttm_mem_global * glob,const struct sysinfo * si)328 static int ttm_mem_init_highmem_zone(struct ttm_mem_global *glob,
329 				     const struct sysinfo *si)
330 {
331 	struct ttm_mem_zone *zone;
332 	uint64_t mem;
333 	int ret;
334 
335 	if (si->totalhigh == 0)
336 		return 0;
337 
338 	zone = kzalloc(sizeof(*zone), GFP_KERNEL);
339 	if (unlikely(!zone))
340 		return -ENOMEM;
341 
342 	mem = si->totalram;
343 	mem *= si->mem_unit;
344 
345 	zone->name = "highmem";
346 	zone->zone_mem = mem;
347 	zone->max_mem = mem >> 1;
348 	zone->emer_mem = (mem >> 1) + (mem >> 2);
349 	zone->swap_limit = zone->max_mem - (mem >> 3);
350 	zone->used_mem = 0;
351 	zone->glob = glob;
352 	glob->zone_highmem = zone;
353 	ret = kobject_init_and_add(
354 		&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, "%s",
355 		zone->name);
356 	if (unlikely(ret != 0)) {
357 		kobject_put(&zone->kobj);
358 		return ret;
359 	}
360 	glob->zones[glob->num_zones++] = zone;
361 	return 0;
362 }
363 #else
ttm_mem_init_dma32_zone(struct ttm_mem_global * glob,const struct sysinfo * si)364 static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
365 				   const struct sysinfo *si)
366 {
367 	struct ttm_mem_zone *zone = kzalloc(sizeof(*zone), GFP_KERNEL);
368 	uint64_t mem;
369 	int ret;
370 
371 	if (unlikely(!zone))
372 		return -ENOMEM;
373 
374 	mem = si->totalram;
375 	mem *= si->mem_unit;
376 
377 	/**
378 	 * No special dma32 zone needed.
379 	 */
380 
381 	if (mem <= ((uint64_t) 1ULL << 32)) {
382 		kfree(zone);
383 		return 0;
384 	}
385 
386 	/*
387 	 * Limit max dma32 memory to 4GB for now
388 	 * until we can figure out how big this
389 	 * zone really is.
390 	 */
391 
392 	mem = ((uint64_t) 1ULL << 32);
393 	zone->name = "dma32";
394 	zone->zone_mem = mem;
395 	zone->max_mem = mem >> 1;
396 	zone->emer_mem = (mem >> 1) + (mem >> 2);
397 	zone->swap_limit = zone->max_mem - (mem >> 3);
398 	zone->used_mem = 0;
399 	zone->glob = glob;
400 	glob->zone_dma32 = zone;
401 	ret = kobject_init_and_add(
402 		&zone->kobj, &ttm_mem_zone_kobj_type, &glob->kobj, zone->name);
403 	if (unlikely(ret != 0)) {
404 		kobject_put(&zone->kobj);
405 		return ret;
406 	}
407 	glob->zones[glob->num_zones++] = zone;
408 	return 0;
409 }
410 #endif
411 
ttm_mem_global_init(struct ttm_mem_global * glob)412 int ttm_mem_global_init(struct ttm_mem_global *glob)
413 {
414 	struct sysinfo si;
415 	int ret;
416 	int i;
417 	struct ttm_mem_zone *zone;
418 
419 	spin_lock_init(&glob->lock);
420 	glob->swap_queue = create_singlethread_workqueue("ttm_swap");
421 	INIT_WORK(&glob->work, ttm_shrink_work);
422 	ret = kobject_init_and_add(
423 		&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
424 	if (unlikely(ret != 0)) {
425 		kobject_put(&glob->kobj);
426 		return ret;
427 	}
428 
429 	si_meminfo(&si);
430 
431 	/* set it as 0 by default to keep original behavior of OOM */
432 	glob->lower_mem_limit = 0;
433 
434 	ret = ttm_mem_init_kernel_zone(glob, &si);
435 	if (unlikely(ret != 0))
436 		goto out_no_zone;
437 #ifdef CONFIG_HIGHMEM
438 	ret = ttm_mem_init_highmem_zone(glob, &si);
439 	if (unlikely(ret != 0))
440 		goto out_no_zone;
441 #else
442 	ret = ttm_mem_init_dma32_zone(glob, &si);
443 	if (unlikely(ret != 0))
444 		goto out_no_zone;
445 #endif
446 	for (i = 0; i < glob->num_zones; ++i) {
447 		zone = glob->zones[i];
448 		pr_info("Zone %7s: Available graphics memory: %llu kiB\n",
449 			zone->name, (unsigned long long)zone->max_mem >> 10);
450 	}
451 	ttm_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
452 	ttm_dma_page_alloc_init(glob, glob->zone_kernel->max_mem/(2*PAGE_SIZE));
453 	return 0;
454 out_no_zone:
455 	ttm_mem_global_release(glob);
456 	return ret;
457 }
458 EXPORT_SYMBOL(ttm_mem_global_init);
459 
ttm_mem_global_release(struct ttm_mem_global * glob)460 void ttm_mem_global_release(struct ttm_mem_global *glob)
461 {
462 	unsigned int i;
463 	struct ttm_mem_zone *zone;
464 
465 	/* let the page allocator first stop the shrink work. */
466 	ttm_page_alloc_fini();
467 	ttm_dma_page_alloc_fini();
468 
469 	flush_workqueue(glob->swap_queue);
470 	destroy_workqueue(glob->swap_queue);
471 	glob->swap_queue = NULL;
472 	for (i = 0; i < glob->num_zones; ++i) {
473 		zone = glob->zones[i];
474 		kobject_del(&zone->kobj);
475 		kobject_put(&zone->kobj);
476 			}
477 	kobject_del(&glob->kobj);
478 	kobject_put(&glob->kobj);
479 }
480 EXPORT_SYMBOL(ttm_mem_global_release);
481 
ttm_check_swapping(struct ttm_mem_global * glob)482 static void ttm_check_swapping(struct ttm_mem_global *glob)
483 {
484 	bool needs_swapping = false;
485 	unsigned int i;
486 	struct ttm_mem_zone *zone;
487 
488 	spin_lock(&glob->lock);
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 
497 	spin_unlock(&glob->lock);
498 
499 	if (unlikely(needs_swapping))
500 		(void)queue_work(glob->swap_queue, &glob->work);
501 
502 }
503 
ttm_mem_global_free_zone(struct ttm_mem_global * glob,struct ttm_mem_zone * single_zone,uint64_t amount)504 static void ttm_mem_global_free_zone(struct ttm_mem_global *glob,
505 				     struct ttm_mem_zone *single_zone,
506 				     uint64_t amount)
507 {
508 	unsigned int i;
509 	struct ttm_mem_zone *zone;
510 
511 	spin_lock(&glob->lock);
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 	}
518 	spin_unlock(&glob->lock);
519 }
520 
ttm_mem_global_free(struct ttm_mem_global * glob,uint64_t amount)521 void ttm_mem_global_free(struct ttm_mem_global *glob,
522 			 uint64_t amount)
523 {
524 	return ttm_mem_global_free_zone(glob, NULL, amount);
525 }
526 EXPORT_SYMBOL(ttm_mem_global_free);
527 
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
ttm_check_under_lowerlimit(struct ttm_mem_global * glob,uint64_t num_pages,struct ttm_operation_ctx * ctx)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 
545 	if (ctx->flags & TTM_OPT_FLAG_FORCE_ALLOC)
546 		return false;
547 
548 	available = get_nr_swap_pages() + si_mem_available();
549 	available -= num_pages;
550 	if (available < glob->lower_mem_limit)
551 		return true;
552 
553 	return false;
554 }
555 EXPORT_SYMBOL(ttm_check_under_lowerlimit);
556 
ttm_mem_global_reserve(struct ttm_mem_global * glob,struct ttm_mem_zone * single_zone,uint64_t amount,bool reserve)557 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
558 				  struct ttm_mem_zone *single_zone,
559 				  uint64_t amount, bool reserve)
560 {
561 	uint64_t limit;
562 	int ret = -ENOMEM;
563 	unsigned int i;
564 	struct ttm_mem_zone *zone;
565 
566 	spin_lock(&glob->lock);
567 	for (i = 0; i < glob->num_zones; ++i) {
568 		zone = glob->zones[i];
569 		if (single_zone && zone != single_zone)
570 			continue;
571 
572 		limit = (capable(CAP_SYS_ADMIN)) ?
573 			zone->emer_mem : zone->max_mem;
574 
575 		if (zone->used_mem > limit)
576 			goto out_unlock;
577 	}
578 
579 	if (reserve) {
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 		}
586 	}
587 
588 	ret = 0;
589 out_unlock:
590 	spin_unlock(&glob->lock);
591 	ttm_check_swapping(glob);
592 
593 	return ret;
594 }
595 
596 
ttm_mem_global_alloc_zone(struct ttm_mem_global * glob,struct ttm_mem_zone * single_zone,uint64_t memory,struct ttm_operation_ctx * ctx)597 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
598 				     struct ttm_mem_zone *single_zone,
599 				     uint64_t memory,
600 				     struct ttm_operation_ctx *ctx)
601 {
602 	int count = TTM_MEMORY_ALLOC_RETRIES;
603 
604 	while (unlikely(ttm_mem_global_reserve(glob,
605 					       single_zone,
606 					       memory, true)
607 			!= 0)) {
608 		if (ctx->no_wait_gpu)
609 			return -ENOMEM;
610 		if (unlikely(count-- == 0))
611 			return -ENOMEM;
612 		ttm_shrink(glob, false, memory + (memory >> 2) + 16, ctx);
613 	}
614 
615 	return 0;
616 }
617 
ttm_mem_global_alloc(struct ttm_mem_global * glob,uint64_t memory,struct ttm_operation_ctx * ctx)618 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
619 			 struct ttm_operation_ctx *ctx)
620 {
621 	/**
622 	 * Normal allocations of kernel memory are registered in
623 	 * all zones.
624 	 */
625 
626 	return ttm_mem_global_alloc_zone(glob, NULL, memory, ctx);
627 }
628 EXPORT_SYMBOL(ttm_mem_global_alloc);
629 
ttm_mem_global_alloc_page(struct ttm_mem_global * glob,struct page * page,uint64_t size,struct ttm_operation_ctx * ctx)630 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
631 			      struct page *page, uint64_t size,
632 			      struct ttm_operation_ctx *ctx)
633 {
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
648 	return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
649 }
650 
ttm_mem_global_free_page(struct ttm_mem_global * glob,struct page * page,uint64_t size)651 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
652 			      uint64_t size)
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
663 	ttm_mem_global_free_zone(glob, zone, size);
664 }
665 
ttm_round_pot(size_t size)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 }
682 EXPORT_SYMBOL(ttm_round_pot);
683 
ttm_get_kernel_zone_memory_size(struct ttm_mem_global * glob)684 uint64_t ttm_get_kernel_zone_memory_size(struct ttm_mem_global *glob)
685 {
686 	return glob->zone_kernel->max_mem;
687 }
688 EXPORT_SYMBOL(ttm_get_kernel_zone_memory_size);
689