dm: split discard requests on target boundaries
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / drm / ttm / ttm_memory.h
blobb199170b3c2ca92fd8a56e9a58bf1056401aa720
1 /**************************************************************************
3 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 #ifndef TTM_MEMORY_H
29 #define TTM_MEMORY_H
31 #include <linux/workqueue.h>
32 #include <linux/spinlock.h>
33 #include <linux/wait.h>
34 #include <linux/errno.h>
35 #include <linux/kobject.h>
36 #include <linux/mm.h>
38 /**
39 * struct ttm_mem_shrink - callback to shrink TTM memory usage.
41 * @do_shrink: The callback function.
43 * Arguments to the do_shrink functions are intended to be passed using
44 * inheritance. That is, the argument class derives from struct ttm_mem_srink,
45 * and can be accessed using container_of().
48 struct ttm_mem_shrink {
49 int (*do_shrink) (struct ttm_mem_shrink *);
52 /**
53 * struct ttm_mem_global - Global memory accounting structure.
55 * @shrink: A single callback to shrink TTM memory usage. Extend this
56 * to a linked list to be able to handle multiple callbacks when needed.
57 * @swap_queue: A workqueue to handle shrinking in low memory situations. We
58 * need a separate workqueue since it will spend a lot of time waiting
59 * for the GPU, and this will otherwise block other workqueue tasks(?)
60 * At this point we use only a single-threaded workqueue.
61 * @work: The workqueue callback for the shrink queue.
62 * @queue: Wait queue for processes suspended waiting for memory.
63 * @lock: Lock to protect the @shrink - and the memory accounting members,
64 * that is, essentially the whole structure with some exceptions.
65 * @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.
71 * Note that this structure is not per device. It should be global for all
72 * graphics devices.
75 #define TTM_MEM_MAX_ZONES 2
76 struct ttm_mem_zone;
77 struct ttm_mem_global {
78 struct kobject kobj;
79 struct ttm_mem_shrink *shrink;
80 struct workqueue_struct *swap_queue;
81 struct work_struct work;
82 wait_queue_head_t queue;
83 spinlock_t lock;
84 struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
85 unsigned int num_zones;
86 struct ttm_mem_zone *zone_kernel;
87 #ifdef CONFIG_HIGHMEM
88 struct ttm_mem_zone *zone_highmem;
89 #else
90 struct ttm_mem_zone *zone_dma32;
91 #endif
94 /**
95 * ttm_mem_init_shrink - initialize a struct ttm_mem_shrink object
97 * @shrink: The object to initialize.
98 * @func: The callback function.
101 static inline void ttm_mem_init_shrink(struct ttm_mem_shrink *shrink,
102 int (*func) (struct ttm_mem_shrink *))
104 shrink->do_shrink = func;
108 * ttm_mem_register_shrink - register a struct ttm_mem_shrink object.
110 * @glob: The struct ttm_mem_global object to register with.
111 * @shrink: An initialized struct ttm_mem_shrink object to register.
113 * Returns:
114 * -EBUSY: There's already a callback registered. (May change).
117 static inline int ttm_mem_register_shrink(struct ttm_mem_global *glob,
118 struct ttm_mem_shrink *shrink)
120 spin_lock(&glob->lock);
121 if (glob->shrink != NULL) {
122 spin_unlock(&glob->lock);
123 return -EBUSY;
125 glob->shrink = shrink;
126 spin_unlock(&glob->lock);
127 return 0;
131 * ttm_mem_unregister_shrink - unregister a struct ttm_mem_shrink object.
133 * @glob: The struct ttm_mem_global object to unregister from.
134 * @shrink: A previously registert struct ttm_mem_shrink object.
138 static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
139 struct ttm_mem_shrink *shrink)
141 spin_lock(&glob->lock);
142 BUG_ON(glob->shrink != shrink);
143 glob->shrink = NULL;
144 spin_unlock(&glob->lock);
147 extern int ttm_mem_global_init(struct ttm_mem_global *glob);
148 extern void ttm_mem_global_release(struct ttm_mem_global *glob);
149 extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
150 bool no_wait, bool interruptible);
151 extern void ttm_mem_global_free(struct ttm_mem_global *glob,
152 uint64_t amount);
153 extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
154 struct page *page,
155 bool no_wait, bool interruptible);
156 extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
157 struct page *page);
158 extern size_t ttm_round_pot(size_t size);
159 #endif