Linux Audio

Check our new training course

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