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