Linux Audio

Check our new training course

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