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