xref: /freebsd/sys/dev/drm2/ttm/ttm_memory.h (revision cc426dd31990b8b50b210efc450e404596548ca1)
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 /* $FreeBSD$ */
28 
29 #ifndef TTM_MEMORY_H
30 #define TTM_MEMORY_H
31 
32 /**
33  * struct ttm_mem_shrink - callback to shrink TTM memory usage.
34  *
35  * @do_shrink: The callback function.
36  *
37  * Arguments to the do_shrink functions are intended to be passed using
38  * inheritance. That is, the argument class derives from struct ttm_mem_shrink,
39  * and can be accessed using container_of().
40  */
41 
42 struct ttm_mem_shrink {
43 	int (*do_shrink) (struct ttm_mem_shrink *);
44 };
45 
46 /**
47  * struct ttm_mem_global - Global memory accounting structure.
48  *
49  * @shrink: A single callback to shrink TTM memory usage. Extend this
50  * to a linked list to be able to handle multiple callbacks when needed.
51  * @swap_queue: A workqueue to handle shrinking in low memory situations. We
52  * need a separate workqueue since it will spend a lot of time waiting
53  * for the GPU, and this will otherwise block other workqueue tasks(?)
54  * At this point we use only a single-threaded workqueue.
55  * @work: The workqueue callback for the shrink queue.
56  * @lock: Lock to protect the @shrink - and the memory accounting members,
57  * that is, essentially the whole structure with some exceptions.
58  * @zones: Array of pointers to accounting zones.
59  * @num_zones: Number of populated entries in the @zones array.
60  * @zone_kernel: Pointer to the kernel zone.
61  * @zone_highmem: Pointer to the highmem zone if there is one.
62  * @zone_dma32: Pointer to the dma32 zone if there is one.
63  *
64  * Note that this structure is not per device. It should be global for all
65  * graphics devices.
66  */
67 
68 #define TTM_MEM_MAX_ZONES 2
69 struct ttm_mem_zone;
70 struct ttm_mem_global {
71 	u_int kobj_ref;
72 	struct ttm_mem_shrink *shrink;
73 	struct taskqueue *swap_queue;
74 	struct task work;
75 	struct mtx lock;
76 	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
77 	unsigned int num_zones;
78 	struct ttm_mem_zone *zone_kernel;
79 	struct ttm_mem_zone *zone_dma32;
80 };
81 
82 /**
83  * ttm_mem_init_shrink - initialize a struct ttm_mem_shrink object
84  *
85  * @shrink: The object to initialize.
86  * @func: The callback function.
87  */
88 
89 static inline void ttm_mem_init_shrink(struct ttm_mem_shrink *shrink,
90 				       int (*func) (struct ttm_mem_shrink *))
91 {
92 	shrink->do_shrink = func;
93 }
94 
95 /**
96  * ttm_mem_register_shrink - register a struct ttm_mem_shrink object.
97  *
98  * @glob: The struct ttm_mem_global object to register with.
99  * @shrink: An initialized struct ttm_mem_shrink object to register.
100  *
101  * Returns:
102  * -EBUSY: There's already a callback registered. (May change).
103  */
104 
105 static inline int ttm_mem_register_shrink(struct ttm_mem_global *glob,
106 					  struct ttm_mem_shrink *shrink)
107 {
108 	mtx_lock(&glob->lock);
109 	if (glob->shrink != NULL) {
110 		mtx_unlock(&glob->lock);
111 		return -EBUSY;
112 	}
113 	glob->shrink = shrink;
114 	mtx_unlock(&glob->lock);
115 	return 0;
116 }
117 
118 /**
119  * ttm_mem_unregister_shrink - unregister a struct ttm_mem_shrink object.
120  *
121  * @glob: The struct ttm_mem_global object to unregister from.
122  * @shrink: A previously registert struct ttm_mem_shrink object.
123  *
124  */
125 
126 static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
127 					     struct ttm_mem_shrink *shrink)
128 {
129 	mtx_lock(&glob->lock);
130 	MPASS(glob->shrink == shrink);
131 	glob->shrink = NULL;
132 	mtx_unlock(&glob->lock);
133 }
134 
135 struct vm_page;
136 
137 extern int ttm_mem_global_init(struct ttm_mem_global *glob);
138 extern void ttm_mem_global_release(struct ttm_mem_global *glob);
139 extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
140 				bool no_wait, bool interruptible);
141 extern void ttm_mem_global_free(struct ttm_mem_global *glob,
142 				uint64_t amount);
143 extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
144 				     struct vm_page *page,
145 				     bool no_wait, bool interruptible);
146 extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
147 				     struct vm_page *page);
148 extern size_t ttm_round_pot(size_t size);
149 #endif
150