blob: 72dcbe81dd0706ee1e7e8c95c5f7d06ef38158da [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
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#ifndef TTM_MEMORY_H
29#define TTM_MEMORY_H
30
31#include <linux/workqueue.h>
32#include <linux/spinlock.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050033#include <linux/bug.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020034#include <linux/wait.h>
35#include <linux/errno.h>
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +020036#include <linux/kobject.h>
Martin Michlmayrc3a73ba2009-11-19 16:29:45 +000037#include <linux/mm.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020038
39/**
40 * struct ttm_mem_shrink - callback to shrink TTM memory usage.
41 *
42 * @do_shrink: The callback function.
43 *
44 * Arguments to the do_shrink functions are intended to be passed using
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +000045 * inheritance. That is, the argument class derives from struct ttm_mem_shrink,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020046 * and can be accessed using container_of().
47 */
48
49struct ttm_mem_shrink {
50 int (*do_shrink) (struct ttm_mem_shrink *);
51};
52
53/**
54 * struct ttm_mem_global - Global memory accounting structure.
55 *
56 * @shrink: A single callback to shrink TTM memory usage. Extend this
57 * to a linked list to be able to handle multiple callbacks when needed.
58 * @swap_queue: A workqueue to handle shrinking in low memory situations. We
59 * need a separate workqueue since it will spend a lot of time waiting
60 * for the GPU, and this will otherwise block other workqueue tasks(?)
61 * At this point we use only a single-threaded workqueue.
62 * @work: The workqueue callback for the shrink queue.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020063 * @lock: Lock to protect the @shrink - and the memory accounting members,
64 * that is, essentially the whole structure with some exceptions.
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +020065 * @zones: Array of pointers to accounting zones.
66 * @num_zones: Number of populated entries in the @zones array.
67 * @zone_kernel: Pointer to the kernel zone.
68 * @zone_highmem: Pointer to the highmem zone if there is one.
69 * @zone_dma32: Pointer to the dma32 zone if there is one.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020070 *
71 * Note that this structure is not per device. It should be global for all
72 * graphics devices.
73 */
74
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +020075#define TTM_MEM_MAX_ZONES 2
76struct ttm_mem_zone;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020077struct ttm_mem_global {
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +020078 struct kobject kobj;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020079 struct ttm_mem_shrink *shrink;
80 struct workqueue_struct *swap_queue;
81 struct work_struct work;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020082 spinlock_t lock;
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +020083 struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
84 unsigned int num_zones;
85 struct ttm_mem_zone *zone_kernel;
86#ifdef CONFIG_HIGHMEM
87 struct ttm_mem_zone *zone_highmem;
88#else
89 struct ttm_mem_zone *zone_dma32;
90#endif
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020091};
92
93/**
94 * ttm_mem_init_shrink - initialize a struct ttm_mem_shrink object
95 *
96 * @shrink: The object to initialize.
97 * @func: The callback function.
98 */
99
100static inline void ttm_mem_init_shrink(struct ttm_mem_shrink *shrink,
101 int (*func) (struct ttm_mem_shrink *))
102{
103 shrink->do_shrink = func;
104}
105
106/**
107 * ttm_mem_register_shrink - register a struct ttm_mem_shrink object.
108 *
109 * @glob: The struct ttm_mem_global object to register with.
110 * @shrink: An initialized struct ttm_mem_shrink object to register.
111 *
112 * Returns:
113 * -EBUSY: There's already a callback registered. (May change).
114 */
115
116static inline int ttm_mem_register_shrink(struct ttm_mem_global *glob,
117 struct ttm_mem_shrink *shrink)
118{
119 spin_lock(&glob->lock);
120 if (glob->shrink != NULL) {
121 spin_unlock(&glob->lock);
122 return -EBUSY;
123 }
124 glob->shrink = shrink;
125 spin_unlock(&glob->lock);
126 return 0;
127}
128
129/**
130 * ttm_mem_unregister_shrink - unregister a struct ttm_mem_shrink object.
131 *
132 * @glob: The struct ttm_mem_global object to unregister from.
133 * @shrink: A previously registert struct ttm_mem_shrink object.
134 *
135 */
136
137static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
138 struct ttm_mem_shrink *shrink)
139{
140 spin_lock(&glob->lock);
141 BUG_ON(glob->shrink != shrink);
142 glob->shrink = NULL;
143 spin_unlock(&glob->lock);
144}
145
146extern int ttm_mem_global_init(struct ttm_mem_global *glob);
147extern void ttm_mem_global_release(struct ttm_mem_global *glob);
148extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +0200149 bool no_wait, bool interruptible);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200150extern void ttm_mem_global_free(struct ttm_mem_global *glob,
Thomas Hellstrom5fd9cba2009-08-17 16:28:39 +0200151 uint64_t amount);
152extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
153 struct page *page,
154 bool no_wait, bool interruptible);
155extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
156 struct page *page);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200157extern size_t ttm_round_pot(size_t size);
158#endif