UPSTREAM: DT/arm,gic-v3: Documment PPI partition support
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / linux / mali_memory_ump.c
1 /*
2  * Copyright (C) 2012-2014 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 #include "mali_ukk.h"
12 #include "mali_osk.h"
13 #include "mali_kernel_common.h"
14 #include "mali_session.h"
15 #include "mali_kernel_linux.h"
16
17 #include "mali_memory.h"
18
19 #include "ump_kernel_interface.h"
20
21 static int mali_ump_map(struct mali_session_data *session, mali_mem_allocation *descriptor)
22 {
23         ump_dd_handle ump_mem;
24         u32 nr_blocks;
25         u32 i;
26         ump_dd_physical_block *ump_blocks;
27         struct mali_page_directory *pagedir;
28         u32 offset = 0;
29         u32 prop;
30         _mali_osk_errcode_t err;
31
32         MALI_DEBUG_ASSERT_POINTER(session);
33         MALI_DEBUG_ASSERT_POINTER(descriptor);
34         MALI_DEBUG_ASSERT(MALI_MEM_UMP == descriptor->type);
35
36         ump_mem = descriptor->ump_mem.handle;
37         MALI_DEBUG_ASSERT(UMP_DD_HANDLE_INVALID != ump_mem);
38
39         nr_blocks = ump_dd_phys_block_count_get(ump_mem);
40         if (nr_blocks == 0) {
41                 MALI_DEBUG_PRINT(1, ("No block count\n"));
42                 return -EINVAL;
43         }
44
45         ump_blocks = _mali_osk_malloc(sizeof(*ump_blocks) * nr_blocks);
46         if (NULL == ump_blocks) {
47                 return -ENOMEM;
48         }
49
50         if (UMP_DD_INVALID == ump_dd_phys_blocks_get(ump_mem, ump_blocks, nr_blocks)) {
51                 _mali_osk_free(ump_blocks);
52                 return -EFAULT;
53         }
54
55         pagedir = session->page_directory;
56         prop = descriptor->mali_mapping.properties;
57
58         err = mali_mem_mali_map_prepare(descriptor);
59         if (_MALI_OSK_ERR_OK != err) {
60                 MALI_DEBUG_PRINT(1, ("Mapping of UMP memory failed\n"));
61
62                 _mali_osk_free(ump_blocks);
63                 return -ENOMEM;
64         }
65
66         for (i = 0; i < nr_blocks; ++i) {
67                 u32 virt = descriptor->mali_mapping.addr + offset;
68
69                 MALI_DEBUG_PRINT(7, ("Mapping in 0x%08x size %d\n", ump_blocks[i].addr , ump_blocks[i].size));
70
71                 mali_mmu_pagedir_update(pagedir, virt, ump_blocks[i].addr,
72                                         ump_blocks[i].size, prop);
73
74                 offset += ump_blocks[i].size;
75         }
76
77         if (descriptor->flags & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
78                 u32 virt = descriptor->mali_mapping.addr + offset;
79
80                 /* Map in an extra virtual guard page at the end of the VMA */
81                 MALI_DEBUG_PRINT(6, ("Mapping in extra guard page\n"));
82
83                 mali_mmu_pagedir_update(pagedir, virt, ump_blocks[0].addr, _MALI_OSK_MALI_PAGE_SIZE, prop);
84
85                 offset += _MALI_OSK_MALI_PAGE_SIZE;
86         }
87
88         _mali_osk_free(ump_blocks);
89
90         return 0;
91 }
92
93 void mali_ump_unmap(struct mali_session_data *session, mali_mem_allocation *descriptor)
94 {
95         ump_dd_handle ump_mem;
96         struct mali_page_directory *pagedir;
97
98         ump_mem = descriptor->ump_mem.handle;
99         pagedir = session->page_directory;
100
101         MALI_DEBUG_ASSERT(UMP_DD_HANDLE_INVALID != ump_mem);
102
103         mali_mem_mali_map_free(descriptor);
104
105         ump_dd_reference_release(ump_mem);
106         return;
107 }
108
109 _mali_osk_errcode_t _mali_ukk_attach_ump_mem(_mali_uk_attach_ump_mem_s *args)
110 {
111         ump_dd_handle ump_mem;
112         struct mali_session_data *session;
113         mali_mem_allocation *descriptor;
114         int md, ret;
115
116         MALI_DEBUG_ASSERT_POINTER(args);
117         MALI_DEBUG_ASSERT(NULL != (void *)(uintptr_t)args->ctx);
118
119         session = (struct mali_session_data *)(uintptr_t)args->ctx;
120
121         /* check arguments */
122         /* NULL might be a valid Mali address */
123         if (!args->size) MALI_ERROR(_MALI_OSK_ERR_INVALID_ARGS);
124
125         /* size must be a multiple of the system page size */
126         if (args->size % _MALI_OSK_MALI_PAGE_SIZE) MALI_ERROR(_MALI_OSK_ERR_INVALID_ARGS);
127
128         MALI_DEBUG_PRINT(3,
129                          ("Requested to map ump memory with secure id %d into virtual memory 0x%08X, size 0x%08X\n",
130                           args->secure_id, args->mali_address, args->size));
131
132         ump_mem = ump_dd_handle_create_from_secure_id((int)args->secure_id);
133
134         if (UMP_DD_HANDLE_INVALID == ump_mem) MALI_ERROR(_MALI_OSK_ERR_FAULT);
135
136         descriptor = mali_mem_descriptor_create(session, MALI_MEM_UMP);
137         if (NULL == descriptor) {
138                 ump_dd_reference_release(ump_mem);
139                 MALI_ERROR(_MALI_OSK_ERR_NOMEM);
140         }
141
142         descriptor->ump_mem.handle = ump_mem;
143         descriptor->mali_mapping.addr = args->mali_address;
144         descriptor->size = args->size;
145         descriptor->mali_mapping.properties = MALI_MMU_FLAGS_DEFAULT;
146         descriptor->flags |= MALI_MEM_FLAG_DONT_CPU_MAP;
147
148         if (args->flags & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
149                 descriptor->flags = MALI_MEM_FLAG_MALI_GUARD_PAGE;
150         }
151
152         _mali_osk_mutex_wait(session->memory_lock);
153
154         ret = mali_ump_map(session, descriptor);
155         if (0 != ret) {
156                 _mali_osk_mutex_signal(session->memory_lock);
157                 ump_dd_reference_release(ump_mem);
158                 mali_mem_descriptor_destroy(descriptor);
159                 MALI_ERROR(_MALI_OSK_ERR_NOMEM);
160         }
161
162         _mali_osk_mutex_signal(session->memory_lock);
163
164
165         if (_MALI_OSK_ERR_OK != mali_descriptor_mapping_allocate_mapping(session->descriptor_mapping, descriptor, &md)) {
166                 ump_dd_reference_release(ump_mem);
167                 mali_mem_descriptor_destroy(descriptor);
168                 MALI_ERROR(_MALI_OSK_ERR_FAULT);
169         }
170
171         args->cookie = md;
172
173         MALI_DEBUG_PRINT(5, ("Returning from UMP attach\n"));
174
175         MALI_SUCCESS;
176 }
177
178 void mali_mem_ump_release(mali_mem_allocation *descriptor)
179 {
180         struct mali_session_data *session = descriptor->session;
181
182         MALI_DEBUG_ASSERT(MALI_MEM_UMP == descriptor->type);
183
184         mali_ump_unmap(session, descriptor);
185 }
186
187 _mali_osk_errcode_t _mali_ukk_release_ump_mem(_mali_uk_release_ump_mem_s *args)
188 {
189         mali_mem_allocation *descriptor;
190         struct mali_session_data *session;
191
192         MALI_DEBUG_ASSERT_POINTER(args);
193         MALI_DEBUG_ASSERT(NULL != (void *)(uintptr_t)args->ctx);
194
195         session = (struct mali_session_data *)(uintptr_t)args->ctx;
196
197         if (_MALI_OSK_ERR_OK != mali_descriptor_mapping_get(session->descriptor_mapping, args->cookie, (void **)&descriptor)) {
198                 MALI_DEBUG_PRINT(1, ("Invalid memory descriptor %d used to release ump memory\n", args->cookie));
199                 MALI_ERROR(_MALI_OSK_ERR_FAULT);
200         }
201
202         descriptor = mali_descriptor_mapping_free(session->descriptor_mapping, args->cookie);
203
204         if (NULL != descriptor) {
205                 _mali_osk_mutex_wait(session->memory_lock);
206                 mali_mem_ump_release(descriptor);
207                 _mali_osk_mutex_signal(session->memory_lock);
208
209                 mali_mem_descriptor_destroy(descriptor);
210         }
211
212         MALI_SUCCESS;
213 }