hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2010, 2013-2014, 2016-2017 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
/**
 * @file ump_ukk_wrappers.c
 * Defines the wrapper functions which turn Linux IOCTL calls into _ukk_ calls for the reference implementation
 */
 
 
#include <linux/uaccess.h>           /* user space access */
 
#include "ump_osk.h"
#include "ump_uk_types.h"
#include "ump_ukk.h"
#include "ump_kernel_common.h"
#include <linux/scatterlist.h>
#include "ump_kernel_interface_ref_drv.h"
#include "mali_osk_list.h"
 
extern struct device *ump_global_mdev;
 
/*
 * IOCTL operation; Allocate UMP memory
 */
int ump_allocate_wrapper(u32 __user *argument, struct ump_session_data   *session_data)
{
   _ump_uk_allocate_s user_interaction;
   _mali_osk_errcode_t err;
 
   /* Sanity check input parameters */
   if (NULL == argument || NULL == session_data) {
       MSG_ERR(("NULL parameter in ump_ioctl_allocate()\n"));
       return -ENOTTY;
   }
 
   /* Copy the user space memory to kernel space (so we safely can read it) */
   if (0 != copy_from_user(&user_interaction, argument, sizeof(user_interaction))) {
       MSG_ERR(("copy_from_user() in ump_ioctl_allocate()\n"));
       return -EFAULT;
   }
 
   user_interaction.ctx = (void *) session_data;
 
   err = _ump_ukk_allocate(&user_interaction);
   if (_MALI_OSK_ERR_OK != err) {
       DBG_MSG(1, ("_ump_ukk_allocate() failed in ump_ioctl_allocate()\n"));
       return ump_map_errcode(err);
   }
   user_interaction.ctx = NULL;
 
   if (0 != copy_to_user(argument, &user_interaction, sizeof(user_interaction))) {
       /* If the copy fails then we should release the memory. We can use the IOCTL release to accomplish this */
       _ump_uk_release_s release_args;
 
       MSG_ERR(("copy_to_user() failed in ump_ioctl_allocate()\n"));
 
       release_args.ctx = (void *) session_data;
       release_args.secure_id = user_interaction.secure_id;
 
       err = _ump_ukk_release(&release_args);
       if (_MALI_OSK_ERR_OK != err) {
           MSG_ERR(("_ump_ukk_release() also failed when trying to release newly allocated memory in ump_ioctl_allocate()\n"));
       }
 
       return -EFAULT;
   }
 
   return 0; /* success */
}
 
#ifdef CONFIG_DMA_SHARED_BUFFER
static ump_dd_handle get_ump_handle_from_dmabuf(struct ump_session_data *session_data,
       struct dma_buf *dmabuf)
{
   ump_session_memory_list_element *session_mem, *tmp;
   struct dma_buf_attachment *attach;
   ump_dd_handle ump_handle;
 
   DEBUG_ASSERT_POINTER(session_data);
 
   _mali_osk_mutex_wait(session_data->lock);
 
   _MALI_OSK_LIST_FOREACHENTRY(session_mem, tmp,
                   &session_data->list_head_session_memory_list,
                   ump_session_memory_list_element, list) {
       if (session_mem->mem->import_attach) {
           attach = session_mem->mem->import_attach;
           if (attach->dmabuf == dmabuf) {
               _mali_osk_mutex_signal(session_data->lock);
               ump_handle = (ump_dd_handle)session_mem->mem;
               ump_random_mapping_get(device.secure_id_map, ump_dd_secure_id_get(ump_handle));
               return ump_handle;
           }
       }
   }
 
   _mali_osk_mutex_signal(session_data->lock);
 
   return NULL;
}
 
int ump_dmabuf_import_wrapper(u32 __user *argument,
                 struct ump_session_data  *session_data)
{
   ump_session_memory_list_element *session = NULL;
   _ump_uk_dmabuf_s ump_dmabuf;
   ump_dd_handle ump_handle;
   ump_dd_physical_block *blocks = NULL;
   struct dma_buf_attachment *attach = NULL;
   struct dma_buf *dma_buf;
   struct sg_table *sgt = NULL;
   struct scatterlist *sgl;
   unsigned int i = 0;
   int ret = 0;
 
   /* Sanity check input parameters */
   if (!argument || !session_data) {
       MSG_ERR(("NULL parameter.\n"));
       return -EINVAL;
   }
 
   if (copy_from_user(&ump_dmabuf, argument,
              sizeof(_ump_uk_dmabuf_s))) {
       MSG_ERR(("copy_from_user() failed.\n"));
       return -EFAULT;
   }
 
   dma_buf = dma_buf_get(ump_dmabuf.fd);
   if (IS_ERR(dma_buf))
       return PTR_ERR(dma_buf);
 
   /*
    * if already imported then increase a refcount to the ump descriptor
    * and call dma_buf_put() and then go to found to return previous
    * ump secure id.
    */
   ump_handle = get_ump_handle_from_dmabuf(session_data, dma_buf);
   if (ump_handle) {
       dma_buf_put(dma_buf);
       goto found;
   }
 
   attach = dma_buf_attach(dma_buf, ump_global_mdev);
   if (IS_ERR(attach)) {
       ret = PTR_ERR(attach);
       goto err_dma_buf_put;
   }
 
   sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
   if (IS_ERR(sgt)) {
       ret = PTR_ERR(sgt);
       goto err_dma_buf_detach;
   }
 
   blocks = (ump_dd_physical_block *)_mali_osk_malloc(sizeof(ump_dd_physical_block) * sgt->nents);
   if (!blocks) {
       DBG_MSG(1, ("Failed to allocate blocks.\n"));
       ret = -EFAULT;
       goto err_dma_buf_unmap;
   }
   for_each_sg(sgt->sgl, sgl, sgt->nents, i) {
       blocks[i].addr = sg_phys(sgl);
       blocks[i].size = sg_dma_len(sgl);
   }
 
   /*
    * Initialize the session memory list element, and add it
    * to the session object
    */
   session = _mali_osk_calloc(1, sizeof(*session));
   if (!session) {
       DBG_MSG(1, ("Failed to allocate session.\n"));
       ret = -EFAULT;
       goto err_free_block;
   }
 
   ump_handle = ump_dd_handle_create_from_phys_blocks(blocks, i);
   if (UMP_DD_HANDLE_INVALID == ump_handle) {
       DBG_MSG(1, ("Failed to create ump handle.\n"));
       ret = -EFAULT;
       goto err_free_session;
   }
 
   session->mem = (ump_dd_mem *)ump_handle;
   session->mem->import_attach = attach;
   session->mem->sgt = sgt;
 
   _mali_osk_mutex_wait(session_data->lock);
   _mali_osk_list_add(&(session->list),
              &(session_data->list_head_session_memory_list));
   _mali_osk_mutex_signal(session_data->lock);
 
   _mali_osk_free(blocks);
 
found:
   ump_dmabuf.ctx = (void *)session_data;
   ump_dmabuf.secure_id = ump_dd_secure_id_get(ump_handle);
   ump_dmabuf.size = ump_dd_size_get(ump_handle);
 
   if (copy_to_user(argument, &ump_dmabuf,
            sizeof(_ump_uk_dmabuf_s))) {
       MSG_ERR(("copy_to_user() failed.\n"));
       ret =  -EFAULT;
       goto err_release_ump_handle;
   }
 
   return ret;
 
err_release_ump_handle:
   ump_dd_reference_release(ump_handle);
err_free_session:
   _mali_osk_free(session);
err_free_block:
   _mali_osk_free(blocks);
err_dma_buf_unmap:
   dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
err_dma_buf_detach:
   dma_buf_detach(dma_buf, attach);
err_dma_buf_put:
   dma_buf_put(dma_buf);
   return ret;
}
#endif