2 * Copyright (C) 2011-2012 ARM Limited. All rights reserved.
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.
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.
11 #ifndef __MALI_KERNEL_MEMORY_ENGINE_H__
12 #define __MALI_KERNEL_MEMORY_ENGINE_H__
14 typedef void * mali_allocation_engine;
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;
18 typedef struct mali_physical_memory_allocation
20 void (*release)(void * ctx, void * handle); /**< Function to call on to release the physical memory */
23 struct mali_physical_memory_allocation * next;
24 } mali_physical_memory_allocation;
26 struct mali_page_table_block;
28 typedef struct mali_page_table_block
30 void (*release)(struct mali_page_table_block *page_table_block);
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;
39 /** @addtogroup _mali_osk_low_level_memory
44 MALI_MEMORY_ALLOCATION_FLAG_MAP_INTO_USERSPACE = 0x1,
45 MALI_MEMORY_ALLOCATION_FLAG_MAP_GUARD_PAGE = 0x2,
46 } mali_memory_allocation_flag;
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
52 #define MALI_MEMORY_ALLOCATION_OS_ALLOCATED_PHYSADDR_MAGIC ((u32)(-1))
54 typedef struct mali_memory_allocation
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 u32 cache_settings; /* type: mali_memory_cache_settings, found in <linux/mali/mali_utgard_uk_types.h> Ump DD breaks if we include it...*/
64 _mali_osk_lock_t * lock;
66 /* Manager specific information pointers */
67 void * mali_addr_mapping_info; /**< Mali address allocation specific info */
68 void * process_addr_mapping_info; /**< Mapping manager specific info */
70 mali_physical_memory_allocation physical_allocation;
72 _mali_osk_list_t list; /**< List for linking together memory allocations into the session's memory head */
73 } mali_memory_allocation;
74 /** @} */ /* end group _mali_osk_low_level_memory */
77 typedef struct mali_physical_memory_allocator
79 mali_physical_memory_allocation_result (*allocate)(void* ctx, mali_allocation_engine * engine, mali_memory_allocation * descriptor, u32* offset, mali_physical_memory_allocation * alloc_info);
80 mali_physical_memory_allocation_result (*allocate_page_table_block)(void * ctx, mali_page_table_block * block); /* MALI_MEM_ALLOC_PARTIAL not allowed */
81 void (*destroy)(struct mali_physical_memory_allocator * allocator);
82 u32 (*stat)(struct mali_physical_memory_allocator * allocator);
84 const char * name; /**< Descriptive name for use in mali_allocation_engine_report_allocators, or NULL */
85 u32 alloc_order; /**< Order in which the allocations should happen */
86 struct mali_physical_memory_allocator * next;
87 } mali_physical_memory_allocator;
89 typedef struct mali_kernel_mem_address_manager
91 _mali_osk_errcode_t (*allocate)(mali_memory_allocation *); /**< Function to call to reserve an address */
92 void (*release)(mali_memory_allocation *); /**< Function to call to free the address allocated */
95 * Function called for each physical sub allocation.
96 * Called for each physical block allocated by the physical memory manager.
97 * @param[in] descriptor The memory descriptor in question
98 * @param[in] off Offset from the start of range
99 * @param[in,out] phys_addr A pointer to the physical address of the start of the
100 * physical block. When *phys_addr == MALI_MEMORY_ALLOCATION_OS_ALLOCATED_PHYSADDR_MAGIC
101 * is used, this requests the function to allocate the physical page
102 * itself, and return it through the pointer provided.
103 * @param[in] size Length in bytes of the physical block
104 * @return _MALI_OSK_ERR_OK on success.
105 * A value of type _mali_osk_errcode_t other than _MALI_OSK_ERR_OK indicates failure.
106 * Specifically, _MALI_OSK_ERR_UNSUPPORTED indicates that the function
107 * does not support allocating physical pages itself.
109 _mali_osk_errcode_t (*map_physical)(mali_memory_allocation * descriptor, u32 offset, u32 *phys_addr, u32 size);
112 * Function called to remove a physical sub allocation.
113 * Called on error paths where one of the address managers fails.
115 * @note this is optional. For address managers where this is not
116 * implemented, the value of this member is NULL. The memory engine
117 * currently does not require the mali address manager to be able to
118 * unmap individual pages, but the process address manager must have this
121 * @param[in] descriptor The memory descriptor in question
122 * @param[in] off Offset from the start of range
123 * @param[in] size Length in bytes of the physical block
124 * @param[in] flags flags to use on a per-page basis. For OS-allocated
125 * physical pages, this must include _MALI_OSK_MEM_MAPREGION_FLAG_OS_ALLOCATED_PHYSADDR.
126 * @return _MALI_OSK_ERR_OK on success.
127 * A value of type _mali_osk_errcode_t other than _MALI_OSK_ERR_OK indicates failure.
129 void (*unmap_physical)(mali_memory_allocation * descriptor, u32 offset, u32 size, _mali_osk_mem_mapregion_flags_t flags);
131 } mali_kernel_mem_address_manager;
133 mali_allocation_engine mali_allocation_engine_create(mali_kernel_mem_address_manager * mali_address_manager, mali_kernel_mem_address_manager * process_address_manager);
135 void mali_allocation_engine_destroy(mali_allocation_engine engine);
137 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 );
138 void mali_allocation_engine_release_memory(mali_allocation_engine engine, mali_memory_allocation * descriptor);
140 void mali_allocation_engine_release_pt1_mali_pagetables_unmap(mali_allocation_engine engine, mali_memory_allocation * descriptor);
141 void mali_allocation_engine_release_pt2_physical_memory_free(mali_allocation_engine engine, mali_memory_allocation * descriptor);
143 int mali_allocation_engine_map_physical(mali_allocation_engine engine, mali_memory_allocation * descriptor, u32 offset, u32 phys, u32 cpu_usage_adjust, u32 size);
144 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);
146 int mali_allocation_engine_allocate_page_tables(mali_allocation_engine, mali_page_table_block * descriptor, mali_physical_memory_allocator * physical_provider);
148 void mali_allocation_engine_report_allocators(mali_physical_memory_allocator * physical_provider);
150 u32 mali_allocation_engine_memory_usage(mali_physical_memory_allocator *allocator);
152 #endif /* __MALI_KERNEL_MEMORY_ENGINE_H__ */