xref: /freebsd-src/sys/dev/drm2/ttm/ttm_memory.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1*592ffb21SWarner Losh /**************************************************************************
2*592ffb21SWarner Losh  *
3*592ffb21SWarner Losh  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*592ffb21SWarner Losh  * All Rights Reserved.
5*592ffb21SWarner Losh  *
6*592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
7*592ffb21SWarner Losh  * copy of this software and associated documentation files (the
8*592ffb21SWarner Losh  * "Software"), to deal in the Software without restriction, including
9*592ffb21SWarner Losh  * without limitation the rights to use, copy, modify, merge, publish,
10*592ffb21SWarner Losh  * distribute, sub license, and/or sell copies of the Software, and to
11*592ffb21SWarner Losh  * permit persons to whom the Software is furnished to do so, subject to
12*592ffb21SWarner Losh  * the following conditions:
13*592ffb21SWarner Losh  *
14*592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the
15*592ffb21SWarner Losh  * next paragraph) shall be included in all copies or substantial portions
16*592ffb21SWarner Losh  * of the Software.
17*592ffb21SWarner Losh  *
18*592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*592ffb21SWarner Losh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*592ffb21SWarner Losh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*592ffb21SWarner Losh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*592ffb21SWarner Losh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*592ffb21SWarner Losh  *
26*592ffb21SWarner Losh  **************************************************************************/
27*592ffb21SWarner Losh 
28*592ffb21SWarner Losh #ifndef TTM_MEMORY_H
29*592ffb21SWarner Losh #define TTM_MEMORY_H
30*592ffb21SWarner Losh 
31*592ffb21SWarner Losh /**
32*592ffb21SWarner Losh  * struct ttm_mem_shrink - callback to shrink TTM memory usage.
33*592ffb21SWarner Losh  *
34*592ffb21SWarner Losh  * @do_shrink: The callback function.
35*592ffb21SWarner Losh  *
36*592ffb21SWarner Losh  * Arguments to the do_shrink functions are intended to be passed using
37*592ffb21SWarner Losh  * inheritance. That is, the argument class derives from struct ttm_mem_shrink,
38*592ffb21SWarner Losh  * and can be accessed using container_of().
39*592ffb21SWarner Losh  */
40*592ffb21SWarner Losh 
41*592ffb21SWarner Losh struct ttm_mem_shrink {
42*592ffb21SWarner Losh 	int (*do_shrink) (struct ttm_mem_shrink *);
43*592ffb21SWarner Losh };
44*592ffb21SWarner Losh 
45*592ffb21SWarner Losh /**
46*592ffb21SWarner Losh  * struct ttm_mem_global - Global memory accounting structure.
47*592ffb21SWarner Losh  *
48*592ffb21SWarner Losh  * @shrink: A single callback to shrink TTM memory usage. Extend this
49*592ffb21SWarner Losh  * to a linked list to be able to handle multiple callbacks when needed.
50*592ffb21SWarner Losh  * @swap_queue: A workqueue to handle shrinking in low memory situations. We
51*592ffb21SWarner Losh  * need a separate workqueue since it will spend a lot of time waiting
52*592ffb21SWarner Losh  * for the GPU, and this will otherwise block other workqueue tasks(?)
53*592ffb21SWarner Losh  * At this point we use only a single-threaded workqueue.
54*592ffb21SWarner Losh  * @work: The workqueue callback for the shrink queue.
55*592ffb21SWarner Losh  * @lock: Lock to protect the @shrink - and the memory accounting members,
56*592ffb21SWarner Losh  * that is, essentially the whole structure with some exceptions.
57*592ffb21SWarner Losh  * @zones: Array of pointers to accounting zones.
58*592ffb21SWarner Losh  * @num_zones: Number of populated entries in the @zones array.
59*592ffb21SWarner Losh  * @zone_kernel: Pointer to the kernel zone.
60*592ffb21SWarner Losh  * @zone_highmem: Pointer to the highmem zone if there is one.
61*592ffb21SWarner Losh  * @zone_dma32: Pointer to the dma32 zone if there is one.
62*592ffb21SWarner Losh  *
63*592ffb21SWarner Losh  * Note that this structure is not per device. It should be global for all
64*592ffb21SWarner Losh  * graphics devices.
65*592ffb21SWarner Losh  */
66*592ffb21SWarner Losh 
67*592ffb21SWarner Losh #define TTM_MEM_MAX_ZONES 2
68*592ffb21SWarner Losh struct ttm_mem_zone;
69*592ffb21SWarner Losh struct ttm_mem_global {
70*592ffb21SWarner Losh 	u_int kobj_ref;
71*592ffb21SWarner Losh 	struct ttm_mem_shrink *shrink;
72*592ffb21SWarner Losh 	struct taskqueue *swap_queue;
73*592ffb21SWarner Losh 	struct task work;
74*592ffb21SWarner Losh 	struct mtx lock;
75*592ffb21SWarner Losh 	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
76*592ffb21SWarner Losh 	unsigned int num_zones;
77*592ffb21SWarner Losh 	struct ttm_mem_zone *zone_kernel;
78*592ffb21SWarner Losh 	struct ttm_mem_zone *zone_dma32;
79*592ffb21SWarner Losh };
80*592ffb21SWarner Losh 
81*592ffb21SWarner Losh /**
82*592ffb21SWarner Losh  * ttm_mem_init_shrink - initialize a struct ttm_mem_shrink object
83*592ffb21SWarner Losh  *
84*592ffb21SWarner Losh  * @shrink: The object to initialize.
85*592ffb21SWarner Losh  * @func: The callback function.
86*592ffb21SWarner Losh  */
87*592ffb21SWarner Losh 
ttm_mem_init_shrink(struct ttm_mem_shrink * shrink,int (* func)(struct ttm_mem_shrink *))88*592ffb21SWarner Losh static inline void ttm_mem_init_shrink(struct ttm_mem_shrink *shrink,
89*592ffb21SWarner Losh 				       int (*func) (struct ttm_mem_shrink *))
90*592ffb21SWarner Losh {
91*592ffb21SWarner Losh 	shrink->do_shrink = func;
92*592ffb21SWarner Losh }
93*592ffb21SWarner Losh 
94*592ffb21SWarner Losh /**
95*592ffb21SWarner Losh  * ttm_mem_register_shrink - register a struct ttm_mem_shrink object.
96*592ffb21SWarner Losh  *
97*592ffb21SWarner Losh  * @glob: The struct ttm_mem_global object to register with.
98*592ffb21SWarner Losh  * @shrink: An initialized struct ttm_mem_shrink object to register.
99*592ffb21SWarner Losh  *
100*592ffb21SWarner Losh  * Returns:
101*592ffb21SWarner Losh  * -EBUSY: There's already a callback registered. (May change).
102*592ffb21SWarner Losh  */
103*592ffb21SWarner Losh 
ttm_mem_register_shrink(struct ttm_mem_global * glob,struct ttm_mem_shrink * shrink)104*592ffb21SWarner Losh static inline int ttm_mem_register_shrink(struct ttm_mem_global *glob,
105*592ffb21SWarner Losh 					  struct ttm_mem_shrink *shrink)
106*592ffb21SWarner Losh {
107*592ffb21SWarner Losh 	mtx_lock(&glob->lock);
108*592ffb21SWarner Losh 	if (glob->shrink != NULL) {
109*592ffb21SWarner Losh 		mtx_unlock(&glob->lock);
110*592ffb21SWarner Losh 		return -EBUSY;
111*592ffb21SWarner Losh 	}
112*592ffb21SWarner Losh 	glob->shrink = shrink;
113*592ffb21SWarner Losh 	mtx_unlock(&glob->lock);
114*592ffb21SWarner Losh 	return 0;
115*592ffb21SWarner Losh }
116*592ffb21SWarner Losh 
117*592ffb21SWarner Losh /**
118*592ffb21SWarner Losh  * ttm_mem_unregister_shrink - unregister a struct ttm_mem_shrink object.
119*592ffb21SWarner Losh  *
120*592ffb21SWarner Losh  * @glob: The struct ttm_mem_global object to unregister from.
121*592ffb21SWarner Losh  * @shrink: A previously registert struct ttm_mem_shrink object.
122*592ffb21SWarner Losh  *
123*592ffb21SWarner Losh  */
124*592ffb21SWarner Losh 
ttm_mem_unregister_shrink(struct ttm_mem_global * glob,struct ttm_mem_shrink * shrink)125*592ffb21SWarner Losh static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
126*592ffb21SWarner Losh 					     struct ttm_mem_shrink *shrink)
127*592ffb21SWarner Losh {
128*592ffb21SWarner Losh 	mtx_lock(&glob->lock);
129*592ffb21SWarner Losh 	MPASS(glob->shrink == shrink);
130*592ffb21SWarner Losh 	glob->shrink = NULL;
131*592ffb21SWarner Losh 	mtx_unlock(&glob->lock);
132*592ffb21SWarner Losh }
133*592ffb21SWarner Losh 
134*592ffb21SWarner Losh struct vm_page;
135*592ffb21SWarner Losh 
136*592ffb21SWarner Losh extern int ttm_mem_global_init(struct ttm_mem_global *glob);
137*592ffb21SWarner Losh extern void ttm_mem_global_release(struct ttm_mem_global *glob);
138*592ffb21SWarner Losh extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
139*592ffb21SWarner Losh 				bool no_wait, bool interruptible);
140*592ffb21SWarner Losh extern void ttm_mem_global_free(struct ttm_mem_global *glob,
141*592ffb21SWarner Losh 				uint64_t amount);
142*592ffb21SWarner Losh extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
143*592ffb21SWarner Losh 				     struct vm_page *page,
144*592ffb21SWarner Losh 				     bool no_wait, bool interruptible);
145*592ffb21SWarner Losh extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
146*592ffb21SWarner Losh 				     struct vm_page *page);
147*592ffb21SWarner Losh extern size_t ttm_round_pot(size_t size);
148*592ffb21SWarner Losh #endif
149