upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / media / video / samsung / mali / common / mali_kernel_memory_engine.h
1 /*
2  * Copyright (C) 2010 ARM Limited. All rights reserved.
3  * 
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  * 
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 #ifndef __MALI_KERNEL_MEMORY_ENGINE_H__
12 #define  __MALI_KERNEL_MEMORY_ENGINE_H__
13
14 typedef void * mali_allocation_engine;
15
16 typedef enum { MALI_MEM_ALLOC_FINISHED, MALI_MEM_ALLOC_PARTIAL, MALI_MEM_ALLOC_NONE, MALI_MEM_ALLOC_INTERNAL_FAILURE } mali_physical_memory_allocation_result;
17
18 typedef struct mali_physical_memory_allocation
19 {
20         void (*release)(void * ctx, void * handle); /**< Function to call on to release the physical memory */
21         void * ctx;
22         void * handle;
23         struct mali_physical_memory_allocation * next;
24 } mali_physical_memory_allocation;
25
26 struct mali_page_table_block;
27
28 typedef struct mali_page_table_block
29 {
30         void (*release)(struct mali_page_table_block *page_table_block);
31         void * ctx;
32         void * handle;
33         u32 size; /**< In bytes, should be a multiple of MALI_MMU_PAGE_SIZE to avoid internal fragementation */
34         u32 phys_base; /**< Mali physical address */
35         mali_io_address mapping;
36 } mali_page_table_block;
37
38
39 /** @addtogroup _mali_osk_low_level_memory
40  * @{ */
41
42 typedef enum
43 {
44         MALI_MEMORY_ALLOCATION_FLAG_MAP_INTO_USERSPACE = 0x1,
45         MALI_MEMORY_ALLOCATION_FLAG_MAP_GUARD_PAGE     = 0x2,
46 } mali_memory_allocation_flag;
47
48 /**
49  * Supplying this 'magic' physical address requests that the OS allocate the
50  * physical address at page commit time, rather than committing a specific page
51  */
52 #define MALI_MEMORY_ALLOCATION_OS_ALLOCATED_PHYSADDR_MAGIC ((u32)(-1))
53
54 typedef struct mali_memory_allocation
55 {
56         /* Information about the allocation */
57         void * mapping; /**< CPU virtual address where the memory is mapped at */
58         u32 mali_address; /**< The Mali seen address of the memory allocation */
59         u32 size; /**< Size of the allocation */
60         u32 permission; /**< Permission settings */
61         mali_memory_allocation_flag flags;
62
63         _mali_osk_lock_t * lock;
64
65         /* Manager specific information pointers */
66         void * mali_addr_mapping_info; /**< Mali address allocation specific info */
67         void * process_addr_mapping_info; /**< Mapping manager specific info */
68
69         mali_physical_memory_allocation physical_allocation;
70
71         _mali_osk_list_t list; /**< List for linking together memory allocations into the session's memory head */
72 } mali_memory_allocation;
73 /** @} */ /* end group _mali_osk_low_level_memory */
74
75
76 typedef struct mali_physical_memory_allocator
77 {
78         mali_physical_memory_allocation_result (*allocate)(void* ctx, mali_allocation_engine * engine, mali_memory_allocation * descriptor, u32* offset, mali_physical_memory_allocation * alloc_info);
79         mali_physical_memory_allocation_result (*allocate_page_table_block)(void * ctx, mali_page_table_block * block); /* MALI_MEM_ALLOC_PARTIAL not allowed */
80         void (*destroy)(struct mali_physical_memory_allocator * allocator);
81         u32 (*stat)(struct mali_physical_memory_allocator * allocator);
82         void * ctx;
83         const char * name; /**< Descriptive name for use in mali_allocation_engine_report_allocators, or NULL */
84         u32 alloc_order; /**< Order in which the allocations should happen */
85         struct mali_physical_memory_allocator * next;
86 } mali_physical_memory_allocator;
87
88 typedef struct mali_kernel_mem_address_manager
89 {
90         _mali_osk_errcode_t (*allocate)(mali_memory_allocation *); /**< Function to call to reserve an address */
91         void (*release)(mali_memory_allocation *); /**< Function to call to free the address allocated */
92
93          /**
94           * Function called for each physical sub allocation.
95           * Called for each physical block allocated by the physical memory manager.
96           * @param[in] descriptor The memory descriptor in question
97           * @param[in] off Offset from the start of range
98           * @param[in,out] phys_addr A pointer to the physical address of the start of the
99           * physical block. When *phys_addr == MALI_MEMORY_ALLOCATION_OS_ALLOCATED_PHYSADDR_MAGIC
100           * is used, this requests the function to allocate the physical page
101           * itself, and return it through the pointer provided.
102           * @param[in] size Length in bytes of the physical block
103           * @return _MALI_OSK_ERR_OK on success.
104           * A value of type _mali_osk_errcode_t other than _MALI_OSK_ERR_OK indicates failure.
105           * Specifically, _MALI_OSK_ERR_UNSUPPORTED indicates that the function
106           * does not support allocating physical pages itself.
107           */
108          _mali_osk_errcode_t (*map_physical)(mali_memory_allocation * descriptor, u32 offset, u32 *phys_addr, u32 size);
109
110          /**
111           * Function called to remove a physical sub allocation.
112           * Called on error paths where one of the address managers fails.
113           *
114           * @note this is optional. For address managers where this is not
115           * implemented, the value of this member is NULL. The memory engine
116           * currently does not require the mali address manager to be able to
117           * unmap individual pages, but the process address manager must have this
118           * capability.
119           *
120           * @param[in] descriptor The memory descriptor in question
121           * @param[in] off Offset from the start of range
122           * @param[in] size Length in bytes of the physical block
123           * @param[in] flags flags to use on a per-page basis. For OS-allocated
124           * physical pages, this must include _MALI_OSK_MEM_MAPREGION_FLAG_OS_ALLOCATED_PHYSADDR.
125           * @return _MALI_OSK_ERR_OK on success.
126           * A value of type _mali_osk_errcode_t other than _MALI_OSK_ERR_OK indicates failure.
127           */
128         void (*unmap_physical)(mali_memory_allocation * descriptor, u32 offset, u32 size, _mali_osk_mem_mapregion_flags_t flags);
129
130 } mali_kernel_mem_address_manager;
131
132 mali_allocation_engine mali_allocation_engine_create(mali_kernel_mem_address_manager * mali_address_manager, mali_kernel_mem_address_manager * process_address_manager);
133
134 void mali_allocation_engine_destroy(mali_allocation_engine engine);
135
136 int mali_allocation_engine_allocate_memory(mali_allocation_engine engine, mali_memory_allocation * descriptor, mali_physical_memory_allocator * physical_provider, _mali_osk_list_t *tracking_list );
137 void mali_allocation_engine_release_memory(mali_allocation_engine engine, mali_memory_allocation * descriptor);
138
139 int mali_allocation_engine_map_physical(mali_allocation_engine engine, mali_memory_allocation * descriptor, u32 offset, u32 phys, u32 cpu_usage_adjust, u32 size);
140 void mali_allocation_engine_unmap_physical(mali_allocation_engine engine, mali_memory_allocation * descriptor, u32 offset, u32 size, _mali_osk_mem_mapregion_flags_t unmap_flags);
141
142 int mali_allocation_engine_allocate_page_tables(mali_allocation_engine, mali_page_table_block * descriptor, mali_physical_memory_allocator * physical_provider);
143
144 void mali_allocation_engine_report_allocators(mali_physical_memory_allocator * physical_provider);
145
146 u32 mali_allocation_engine_memory_usage(mali_physical_memory_allocator *allocator);
147
148 #endif /* __MALI_KERNEL_MEMORY_ENGINE_H__ */