Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sys / dev / drm / drm_sman.h
1 /**************************************************************************
2  *
3  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., 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  * __FBSDID("$FreeBSD: src/sys/dev/drm/drm_sman.h,v 1.1 2010/01/31 14:25:29 rnoland Exp $");
28  **************************************************************************/
29
30 /*
31  * Simple memory MANager interface that keeps track on allocate regions on a
32  * per "owner" basis. All regions associated with an "owner" can be released
33  * with a simple call. Typically if the "owner" exists. The owner is any
34  * "unsigned long" identifier. Can typically be a pointer to a file private
35  * struct or a context identifier.
36  *
37  * Authors:
38  * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
39  */
40
41 #ifndef DRM_SMAN_H
42 #define DRM_SMAN_H
43
44 #include "dev/drm/drm_hashtab.h"
45 #include "dev/drm/drm_linux_list.h"
46 #include "dev/drm/drm_mm.h"
47
48 /*
49  * A class that is an abstration of a simple memory allocator.
50  * The sman implementation provides a default such allocator
51  * using the drm_mm.c implementation. But the user can replace it.
52  * See the SiS implementation, which may use the SiS FB kernel module
53  * for memory management.
54  */
55
56 struct drm_sman_mm {
57         /* private info. If allocated, needs to be destroyed by the destroy
58            function */
59         void *private;
60
61         /* Allocate a memory block with given size and alignment.
62            Return an opaque reference to the memory block */
63
64         void *(*allocate) (void *private, unsigned long size,
65                            unsigned alignment);
66
67         /* Free a memory block. "ref" is the opaque reference that we got from
68            the "alloc" function */
69
70         void (*free) (void *private, void *ref);
71
72         /* Free all resources associated with this allocator */
73
74         void (*destroy) (void *private);
75
76         /* Return a memory offset from the opaque reference returned from the
77            "alloc" function */
78
79         unsigned long (*offset) (void *private, void *ref);
80 };
81
82 struct drm_memblock_item {
83         struct list_head owner_list;
84         struct drm_hash_item user_hash;
85         void *mm_info;
86         struct drm_sman_mm *mm;
87         struct drm_sman *sman;
88 };
89
90 struct drm_sman {
91         struct drm_sman_mm *mm;
92         int num_managers;
93         struct drm_open_hash owner_hash_tab;
94         struct drm_open_hash user_hash_tab;
95         struct list_head owner_items;
96 };
97
98 /*
99  * Take down a memory manager. This function should only be called after a
100  * successful init and after a call to drm_sman_cleanup.
101  */
102
103 extern void drm_sman_takedown(struct drm_sman * sman);
104
105 /*
106  * Allocate structures for a manager.
107  * num_managers are the number of memory pools to manage. (VRAM, AGP, ....)
108  * user_order is the log2 of the number of buckets in the user hash table.
109  *          set this to approximately log2 of the max number of memory regions
110  *          that will be allocated for _all_ pools together.
111  * owner_order is the log2 of the number of buckets in the owner hash table.
112  *          set this to approximately log2 of
113  *          the number of client file connections that will
114  *          be using the manager.
115  *
116  */
117
118 extern int drm_sman_init(struct drm_sman * sman, unsigned int num_managers,
119                          unsigned int user_order, unsigned int owner_order);
120
121 /*
122  * Initialize a drm_mm.c allocator. Should be called only once for each
123  * manager unless a customized allogator is used.
124  */
125
126 extern int drm_sman_set_range(struct drm_sman * sman, unsigned int manager,
127                               unsigned long start, unsigned long size);
128
129 /*
130  * Initialize a customized allocator for one of the managers.
131  * (See the SiS module). The object pointed to by "allocator" is copied,
132  * so it can be destroyed after this call.
133  */
134
135 extern int drm_sman_set_manager(struct drm_sman * sman, unsigned int mananger,
136                                 struct drm_sman_mm * allocator);
137
138 /*
139  * Allocate a memory block. Aligment is not implemented yet.
140  */
141
142 extern struct drm_memblock_item *drm_sman_alloc(struct drm_sman * sman,
143                                                 unsigned int manager,
144                                                 unsigned long size,
145                                                 unsigned alignment,
146                                                 unsigned long owner);
147 /*
148  * Free a memory block identified by its user hash key.
149  */
150
151 extern int drm_sman_free_key(struct drm_sman * sman, unsigned int key);
152
153 /*
154  * returns 1 iff there are no stale memory blocks associated with this owner.
155  * Typically called to determine if we need to idle the hardware and call
156  * drm_sman_owner_cleanup. If there are no stale memory blocks, it removes all
157  * resources associated with owner.
158  */
159
160 extern int drm_sman_owner_clean(struct drm_sman * sman, unsigned long owner);
161
162 /*
163  * Frees all stale memory blocks associated with this owner. Note that this
164  * requires that the hardware is finished with all blocks, so the graphics engine
165  * should be idled before this call is made. This function also frees
166  * any resources associated with "owner" and should be called when owner
167  * is not going to be referenced anymore.
168  */
169
170 extern void drm_sman_owner_cleanup(struct drm_sman * sman, unsigned long owner);
171
172 /*
173  * Frees all stale memory blocks associated with the memory manager.
174  * See idling above.
175  */
176
177 extern void drm_sman_cleanup(struct drm_sman * sman);
178
179 #endif