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