/**************************************************************************1*2* Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,21* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR22* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE23* USE OR OTHER DEALINGS IN THE SOFTWARE.24*25*26**************************************************************************/27/*28* Simple memory MANager interface that keeps track on allocate regions on a29* per "owner" basis. All regions associated with an "owner" can be released30* with a simple call. Typically if the "owner" exists. The owner is any31* "unsigned long" identifier. Can typically be a pointer to a file private32* struct or a context identifier.33*34* Authors:35* Thomas Hellström <thomas-at-tungstengraphics-dot-com>36*/3738#ifndef DRM_SMAN_H39#define DRM_SMAN_H4041#include "drmP.h"42#include "drm_hashtab.h"4344/*45* A class that is an abstration of a simple memory allocator.46* The sman implementation provides a default such allocator47* using the drm_mm.c implementation. But the user can replace it.48* See the SiS implementation, which may use the SiS FB kernel module49* for memory management.50*/5152struct drm_sman_mm {53/* private info. If allocated, needs to be destroyed by the destroy54function */55void *private;5657/* Allocate a memory block with given size and alignment.58Return an opaque reference to the memory block */5960void *(*allocate) (void *private, unsigned long size,61unsigned alignment);6263/* Free a memory block. "ref" is the opaque reference that we got from64the "alloc" function */6566void (*free) (void *private, void *ref);6768/* Free all resources associated with this allocator */6970void (*destroy) (void *private);7172/* Return a memory offset from the opaque reference returned from the73"alloc" function */7475unsigned long (*offset) (void *private, void *ref);76};7778struct drm_memblock_item {79struct list_head owner_list;80struct drm_hash_item user_hash;81void *mm_info;82struct drm_sman_mm *mm;83struct drm_sman *sman;84};8586struct drm_sman {87struct drm_sman_mm *mm;88int num_managers;89struct drm_open_hash owner_hash_tab;90struct drm_open_hash user_hash_tab;91struct list_head owner_items;92};9394/*95* Take down a memory manager. This function should only be called after a96* successful init and after a call to drm_sman_cleanup.97*/9899extern void drm_sman_takedown(struct drm_sman * sman);100101/*102* Allocate structures for a manager.103* num_managers are the number of memory pools to manage. (VRAM, AGP, ....)104* user_order is the log2 of the number of buckets in the user hash table.105* set this to approximately log2 of the max number of memory regions106* that will be allocated for _all_ pools together.107* owner_order is the log2 of the number of buckets in the owner hash table.108* set this to approximately log2 of109* the number of client file connections that will110* be using the manager.111*112*/113114extern int drm_sman_init(struct drm_sman * sman, unsigned int num_managers,115unsigned int user_order, unsigned int owner_order);116117/*118* Initialize a drm_mm.c allocator. Should be called only once for each119* manager unless a customized allogator is used.120*/121122extern int drm_sman_set_range(struct drm_sman * sman, unsigned int manager,123unsigned long start, unsigned long size);124125/*126* Initialize a customized allocator for one of the managers.127* (See the SiS module). The object pointed to by "allocator" is copied,128* so it can be destroyed after this call.129*/130131extern int drm_sman_set_manager(struct drm_sman * sman, unsigned int mananger,132struct drm_sman_mm * allocator);133134/*135* Allocate a memory block. Aligment is not implemented yet.136*/137138extern struct drm_memblock_item *drm_sman_alloc(struct drm_sman * sman,139unsigned int manager,140unsigned long size,141unsigned alignment,142unsigned long owner);143/*144* Free a memory block identified by its user hash key.145*/146147extern int drm_sman_free_key(struct drm_sman * sman, unsigned int key);148149/*150* returns 1 iff there are no stale memory blocks associated with this owner.151* Typically called to determine if we need to idle the hardware and call152* drm_sman_owner_cleanup. If there are no stale memory blocks, it removes all153* resources associated with owner.154*/155156extern int drm_sman_owner_clean(struct drm_sman * sman, unsigned long owner);157158/*159* Frees all stale memory blocks associated with this owner. Note that this160* requires that the hardware is finished with all blocks, so the graphics engine161* should be idled before this call is made. This function also frees162* any resources associated with "owner" and should be called when owner163* is not going to be referenced anymore.164*/165166extern void drm_sman_owner_cleanup(struct drm_sman * sman, unsigned long owner);167168/*169* Frees all stale memory blocks associated with the memory manager.170* See idling above.171*/172173extern void drm_sman_cleanup(struct drm_sman * sman);174175#endif176177178