Commit | Line | Data |
---|---|---|
f6f9279f SK |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | // Copyright (c) 2011-2018, The Linux Foundation. All rights reserved. | |
3 | // Copyright (c) 2018, Linaro Limited | |
4 | ||
c68cfb71 | 5 | #include <linux/completion.h> |
f6f9279f | 6 | #include <linux/device.h> |
c68cfb71 | 7 | #include <linux/dma-buf.h> |
f6f9279f | 8 | #include <linux/dma-mapping.h> |
265751a5 | 9 | #include <linux/dma-resv.h> |
f6f9279f SK |
10 | #include <linux/idr.h> |
11 | #include <linux/list.h> | |
12 | #include <linux/miscdevice.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/of_address.h> | |
15 | #include <linux/of.h> | |
25e8dfb8 | 16 | #include <linux/sort.h> |
f6f9279f SK |
17 | #include <linux/of_platform.h> |
18 | #include <linux/rpmsg.h> | |
19 | #include <linux/scatterlist.h> | |
20 | #include <linux/slab.h> | |
e90d9119 | 21 | #include <linux/qcom_scm.h> |
c68cfb71 | 22 | #include <uapi/misc/fastrpc.h> |
1ce91d45 | 23 | #include <linux/of_reserved_mem.h> |
f6f9279f SK |
24 | |
25 | #define ADSP_DOMAIN_ID (0) | |
26 | #define MDSP_DOMAIN_ID (1) | |
27 | #define SDSP_DOMAIN_ID (2) | |
28 | #define CDSP_DOMAIN_ID (3) | |
29 | #define FASTRPC_DEV_MAX 4 /* adsp, mdsp, slpi, cdsp*/ | |
689a2d9f | 30 | #define FASTRPC_MAX_SESSIONS 14 |
e90d9119 | 31 | #define FASTRPC_MAX_VMIDS 16 |
c68cfb71 SK |
32 | #define FASTRPC_ALIGN 128 |
33 | #define FASTRPC_MAX_FDLIST 16 | |
34 | #define FASTRPC_MAX_CRCLIST 64 | |
35 | #define FASTRPC_PHYS(p) ((p) & 0xffffffff) | |
f6f9279f | 36 | #define FASTRPC_CTX_MAX (256) |
d73f71c7 | 37 | #define FASTRPC_INIT_HANDLE 1 |
6c16fd8b | 38 | #define FASTRPC_DSP_UTILITIES_HANDLE 2 |
f6f9279f | 39 | #define FASTRPC_CTXID_MASK (0xFF0) |
efcd2390 | 40 | #define INIT_FILELEN_MAX (2 * 1024 * 1024) |
08715610 | 41 | #define INIT_FILE_NAMELEN_MAX (128) |
f6f9279f | 42 | #define FASTRPC_DEVICE_NAME "fastrpc" |
08715610 AV |
43 | |
44 | /* Add memory to static PD pool, protection thru XPU */ | |
45 | #define ADSP_MMAP_HEAP_ADDR 4 | |
46 | /* MAP static DMA buffer on DSP User PD */ | |
47 | #define ADSP_MMAP_DMA_BUFFER 6 | |
48 | /* Add memory to static PD pool protection thru hypervisor */ | |
49 | #define ADSP_MMAP_REMOTE_HEAP_ADDR 8 | |
50 | /* Add memory to userPD pool, for user heap */ | |
2419e55e | 51 | #define ADSP_MMAP_ADD_PAGES 0x1000 |
08715610 AV |
52 | /* Add memory to userPD pool, for LLC heap */ |
53 | #define ADSP_MMAP_ADD_PAGES_LLC 0x3000, | |
54 | ||
6c16fd8b J |
55 | #define DSP_UNSUPPORTED_API (0x80000414) |
56 | /* MAX NUMBER of DSP ATTRIBUTES SUPPORTED */ | |
57 | #define FASTRPC_MAX_DSP_ATTRIBUTES (256) | |
58 | #define FASTRPC_MAX_DSP_ATTRIBUTES_LEN (sizeof(u32) * FASTRPC_MAX_DSP_ATTRIBUTES) | |
f6f9279f | 59 | |
c68cfb71 SK |
60 | /* Retrives number of input buffers from the scalars parameter */ |
61 | #define REMOTE_SCALARS_INBUFS(sc) (((sc) >> 16) & 0x0ff) | |
62 | ||
63 | /* Retrives number of output buffers from the scalars parameter */ | |
64 | #define REMOTE_SCALARS_OUTBUFS(sc) (((sc) >> 8) & 0x0ff) | |
65 | ||
66 | /* Retrives number of input handles from the scalars parameter */ | |
67 | #define REMOTE_SCALARS_INHANDLES(sc) (((sc) >> 4) & 0x0f) | |
68 | ||
69 | /* Retrives number of output handles from the scalars parameter */ | |
70 | #define REMOTE_SCALARS_OUTHANDLES(sc) ((sc) & 0x0f) | |
71 | ||
72 | #define REMOTE_SCALARS_LENGTH(sc) (REMOTE_SCALARS_INBUFS(sc) + \ | |
73 | REMOTE_SCALARS_OUTBUFS(sc) + \ | |
74 | REMOTE_SCALARS_INHANDLES(sc)+ \ | |
75 | REMOTE_SCALARS_OUTHANDLES(sc)) | |
76 | #define FASTRPC_BUILD_SCALARS(attr, method, in, out, oin, oout) \ | |
77 | (((attr & 0x07) << 29) | \ | |
78 | ((method & 0x1f) << 24) | \ | |
79 | ((in & 0xff) << 16) | \ | |
80 | ((out & 0xff) << 8) | \ | |
81 | ((oin & 0x0f) << 4) | \ | |
82 | (oout & 0x0f)) | |
83 | ||
84 | #define FASTRPC_SCALARS(method, in, out) \ | |
85 | FASTRPC_BUILD_SCALARS(0, method, in, out, 0, 0) | |
86 | ||
d73f71c7 | 87 | #define FASTRPC_CREATE_PROCESS_NARGS 6 |
08715610 | 88 | #define FASTRPC_CREATE_STATIC_PROCESS_NARGS 3 |
d73f71c7 SK |
89 | /* Remote Method id table */ |
90 | #define FASTRPC_RMID_INIT_ATTACH 0 | |
91 | #define FASTRPC_RMID_INIT_RELEASE 1 | |
2419e55e JRO |
92 | #define FASTRPC_RMID_INIT_MMAP 4 |
93 | #define FASTRPC_RMID_INIT_MUNMAP 5 | |
d73f71c7 SK |
94 | #define FASTRPC_RMID_INIT_CREATE 6 |
95 | #define FASTRPC_RMID_INIT_CREATE_ATTR 7 | |
96 | #define FASTRPC_RMID_INIT_CREATE_STATIC 8 | |
5c1b97c7 J |
97 | #define FASTRPC_RMID_INIT_MEM_MAP 10 |
98 | #define FASTRPC_RMID_INIT_MEM_UNMAP 11 | |
d73f71c7 | 99 | |
84195d20 | 100 | /* Protection Domain(PD) ids */ |
1959ab9e | 101 | #define ROOT_PD (0) |
84195d20 JM |
102 | #define USER_PD (1) |
103 | #define SENSORS_PD (2) | |
104 | ||
965602ea | 105 | #define miscdev_to_fdevice(d) container_of(d, struct fastrpc_device, miscdev) |
f6f9279f SK |
106 | |
107 | static const char *domains[FASTRPC_DEV_MAX] = { "adsp", "mdsp", | |
108 | "sdsp", "cdsp"}; | |
c68cfb71 SK |
109 | struct fastrpc_phy_page { |
110 | u64 addr; /* physical address */ | |
111 | u64 size; /* size of contiguous region */ | |
112 | }; | |
113 | ||
114 | struct fastrpc_invoke_buf { | |
115 | u32 num; /* number of contiguous regions */ | |
116 | u32 pgidx; /* index to start of contiguous region */ | |
117 | }; | |
118 | ||
35a82b87 VKG |
119 | struct fastrpc_remote_dmahandle { |
120 | s32 fd; /* dma handle fd */ | |
121 | u32 offset; /* dma handle offset */ | |
122 | u32 len; /* dma handle length */ | |
123 | }; | |
124 | ||
125 | struct fastrpc_remote_buf { | |
126 | u64 pv; /* buffer pointer */ | |
127 | u64 len; /* length of buffer */ | |
128 | }; | |
129 | ||
130 | union fastrpc_remote_arg { | |
131 | struct fastrpc_remote_buf buf; | |
132 | struct fastrpc_remote_dmahandle dma; | |
c68cfb71 SK |
133 | }; |
134 | ||
2419e55e JRO |
135 | struct fastrpc_mmap_rsp_msg { |
136 | u64 vaddr; | |
137 | }; | |
138 | ||
139 | struct fastrpc_mmap_req_msg { | |
140 | s32 pgid; | |
141 | u32 flags; | |
142 | u64 vaddr; | |
143 | s32 num; | |
144 | }; | |
145 | ||
5c1b97c7 J |
146 | struct fastrpc_mem_map_req_msg { |
147 | s32 pgid; | |
148 | s32 fd; | |
149 | s32 offset; | |
150 | u32 flags; | |
151 | u64 vaddrin; | |
152 | s32 num; | |
153 | s32 data_len; | |
154 | }; | |
155 | ||
2419e55e JRO |
156 | struct fastrpc_munmap_req_msg { |
157 | s32 pgid; | |
158 | u64 vaddr; | |
159 | u64 size; | |
160 | }; | |
161 | ||
5c1b97c7 J |
162 | struct fastrpc_mem_unmap_req_msg { |
163 | s32 pgid; | |
164 | s32 fd; | |
165 | u64 vaddrin; | |
166 | u64 len; | |
167 | }; | |
168 | ||
c68cfb71 SK |
169 | struct fastrpc_msg { |
170 | int pid; /* process group id */ | |
171 | int tid; /* thread id */ | |
172 | u64 ctx; /* invoke caller context */ | |
173 | u32 handle; /* handle to invoke */ | |
174 | u32 sc; /* scalars structure describing the data */ | |
175 | u64 addr; /* physical address */ | |
176 | u64 size; /* size of contiguous region */ | |
177 | }; | |
178 | ||
179 | struct fastrpc_invoke_rsp { | |
180 | u64 ctx; /* invoke caller context */ | |
181 | int retval; /* invoke return value */ | |
182 | }; | |
183 | ||
25e8dfb8 SK |
184 | struct fastrpc_buf_overlap { |
185 | u64 start; | |
186 | u64 end; | |
187 | int raix; | |
188 | u64 mstart; | |
189 | u64 mend; | |
190 | u64 offset; | |
191 | }; | |
192 | ||
c68cfb71 SK |
193 | struct fastrpc_buf { |
194 | struct fastrpc_user *fl; | |
6cffd795 | 195 | struct dma_buf *dmabuf; |
c68cfb71 SK |
196 | struct device *dev; |
197 | void *virt; | |
198 | u64 phys; | |
199 | u64 size; | |
6cffd795 SK |
200 | /* Lock for dma buf attachments */ |
201 | struct mutex lock; | |
202 | struct list_head attachments; | |
2419e55e JRO |
203 | /* mmap support */ |
204 | struct list_head node; /* list of user requested mmaps */ | |
205 | uintptr_t raddr; | |
6cffd795 SK |
206 | }; |
207 | ||
208 | struct fastrpc_dma_buf_attachment { | |
209 | struct device *dev; | |
210 | struct sg_table sgt; | |
211 | struct list_head node; | |
c68cfb71 SK |
212 | }; |
213 | ||
214 | struct fastrpc_map { | |
215 | struct list_head node; | |
216 | struct fastrpc_user *fl; | |
217 | int fd; | |
218 | struct dma_buf *buf; | |
219 | struct sg_table *table; | |
220 | struct dma_buf_attachment *attach; | |
221 | u64 phys; | |
222 | u64 size; | |
223 | void *va; | |
224 | u64 len; | |
5c1b97c7 | 225 | u64 raddr; |
e90d9119 | 226 | u32 attr; |
c68cfb71 SK |
227 | struct kref refcount; |
228 | }; | |
229 | ||
230 | struct fastrpc_invoke_ctx { | |
231 | int nscalars; | |
232 | int nbufs; | |
233 | int retval; | |
234 | int pid; | |
235 | int tgid; | |
236 | u32 sc; | |
237 | u32 *crc; | |
238 | u64 ctxid; | |
239 | u64 msg_sz; | |
240 | struct kref refcount; | |
241 | struct list_head node; /* list of ctxs */ | |
242 | struct completion work; | |
8e7389c7 | 243 | struct work_struct put_work; |
c68cfb71 SK |
244 | struct fastrpc_msg msg; |
245 | struct fastrpc_user *fl; | |
35a82b87 | 246 | union fastrpc_remote_arg *rpra; |
c68cfb71 SK |
247 | struct fastrpc_map **maps; |
248 | struct fastrpc_buf *buf; | |
249 | struct fastrpc_invoke_args *args; | |
25e8dfb8 | 250 | struct fastrpc_buf_overlap *olaps; |
c68cfb71 SK |
251 | struct fastrpc_channel_ctx *cctx; |
252 | }; | |
f6f9279f SK |
253 | |
254 | struct fastrpc_session_ctx { | |
255 | struct device *dev; | |
256 | int sid; | |
257 | bool used; | |
258 | bool valid; | |
259 | }; | |
260 | ||
261 | struct fastrpc_channel_ctx { | |
262 | int domain_id; | |
263 | int sesscount; | |
e90d9119 VKG |
264 | int vmcount; |
265 | u32 perms; | |
266 | struct qcom_scm_vmperm vmperms[FASTRPC_MAX_VMIDS]; | |
f6f9279f SK |
267 | struct rpmsg_device *rpdev; |
268 | struct fastrpc_session_ctx session[FASTRPC_MAX_SESSIONS]; | |
269 | spinlock_t lock; | |
270 | struct idr ctx_idr; | |
271 | struct list_head users; | |
278d56f9 | 272 | struct kref refcount; |
6c16fd8b J |
273 | /* Flag if dsp attributes are cached */ |
274 | bool valid_attributes; | |
275 | u32 dsp_attributes[FASTRPC_MAX_DSP_ATTRIBUTES]; | |
3abe3ab3 | 276 | struct fastrpc_device *secure_fdevice; |
965602ea | 277 | struct fastrpc_device *fdevice; |
08715610 | 278 | struct fastrpc_buf *remote_heap; |
76e8e4ac | 279 | struct list_head invoke_interrupted_mmaps; |
3abe3ab3 | 280 | bool secure; |
7f1f4812 | 281 | bool unsigned_support; |
9bde43a0 | 282 | u64 dma_mask; |
965602ea SK |
283 | }; |
284 | ||
285 | struct fastrpc_device { | |
286 | struct fastrpc_channel_ctx *cctx; | |
287 | struct miscdevice miscdev; | |
3abe3ab3 | 288 | bool secure; |
f6f9279f SK |
289 | }; |
290 | ||
291 | struct fastrpc_user { | |
292 | struct list_head user; | |
293 | struct list_head maps; | |
294 | struct list_head pending; | |
2419e55e | 295 | struct list_head mmaps; |
f6f9279f SK |
296 | |
297 | struct fastrpc_channel_ctx *cctx; | |
298 | struct fastrpc_session_ctx *sctx; | |
c68cfb71 | 299 | struct fastrpc_buf *init_mem; |
f6f9279f SK |
300 | |
301 | int tgid; | |
302 | int pd; | |
7f1f4812 | 303 | bool is_secure_dev; |
f6f9279f SK |
304 | /* Lock for lists */ |
305 | spinlock_t lock; | |
306 | /* lock for allocations */ | |
307 | struct mutex mutex; | |
308 | }; | |
309 | ||
c68cfb71 SK |
310 | static void fastrpc_free_map(struct kref *ref) |
311 | { | |
312 | struct fastrpc_map *map; | |
313 | ||
314 | map = container_of(ref, struct fastrpc_map, refcount); | |
315 | ||
316 | if (map->table) { | |
e90d9119 VKG |
317 | if (map->attr & FASTRPC_ATTR_SECUREMAP) { |
318 | struct qcom_scm_vmperm perm; | |
319 | int err = 0; | |
320 | ||
321 | perm.vmid = QCOM_SCM_VMID_HLOS; | |
322 | perm.perm = QCOM_SCM_PERM_RWX; | |
323 | err = qcom_scm_assign_mem(map->phys, map->size, | |
324 | &(map->fl->cctx->vmperms[0].vmid), &perm, 1); | |
325 | if (err) { | |
326 | dev_err(map->fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d", | |
327 | map->phys, map->size, err); | |
328 | return; | |
329 | } | |
330 | } | |
791da5c7 DO |
331 | dma_buf_unmap_attachment_unlocked(map->attach, map->table, |
332 | DMA_BIDIRECTIONAL); | |
c68cfb71 SK |
333 | dma_buf_detach(map->buf, map->attach); |
334 | dma_buf_put(map->buf); | |
335 | } | |
336 | ||
5bb96c8f AV |
337 | if (map->fl) { |
338 | spin_lock(&map->fl->lock); | |
339 | list_del(&map->node); | |
340 | spin_unlock(&map->fl->lock); | |
341 | map->fl = NULL; | |
342 | } | |
343 | ||
c68cfb71 SK |
344 | kfree(map); |
345 | } | |
346 | ||
347 | static void fastrpc_map_put(struct fastrpc_map *map) | |
348 | { | |
349 | if (map) | |
350 | kref_put(&map->refcount, fastrpc_free_map); | |
351 | } | |
352 | ||
96b328d1 | 353 | static int fastrpc_map_get(struct fastrpc_map *map) |
c68cfb71 | 354 | { |
96b328d1 OJ |
355 | if (!map) |
356 | return -ENOENT; | |
357 | ||
358 | return kref_get_unless_zero(&map->refcount) ? 0 : -ENOENT; | |
c68cfb71 SK |
359 | } |
360 | ||
8f6c1d8c VKG |
361 | |
362 | static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd, | |
9446fa16 | 363 | struct fastrpc_map **ppmap, bool take_ref) |
c68cfb71 | 364 | { |
9446fa16 | 365 | struct fastrpc_session_ctx *sess = fl->sctx; |
c68cfb71 | 366 | struct fastrpc_map *map = NULL; |
9446fa16 | 367 | int ret = -ENOENT; |
c68cfb71 | 368 | |
9446fa16 | 369 | spin_lock(&fl->lock); |
c68cfb71 | 370 | list_for_each_entry(map, &fl->maps, node) { |
9446fa16 AV |
371 | if (map->fd != fd) |
372 | continue; | |
c68cfb71 | 373 | |
9446fa16 AV |
374 | if (take_ref) { |
375 | ret = fastrpc_map_get(map); | |
376 | if (ret) { | |
377 | dev_dbg(sess->dev, "%s: Failed to get map fd=%d ret=%d\n", | |
378 | __func__, fd, ret); | |
379 | break; | |
380 | } | |
381 | } | |
8f6c1d8c | 382 | |
9446fa16 AV |
383 | *ppmap = map; |
384 | ret = 0; | |
385 | break; | |
386 | } | |
387 | spin_unlock(&fl->lock); | |
8f6c1d8c VKG |
388 | |
389 | return ret; | |
390 | } | |
391 | ||
c68cfb71 SK |
392 | static void fastrpc_buf_free(struct fastrpc_buf *buf) |
393 | { | |
394 | dma_free_coherent(buf->dev, buf->size, buf->virt, | |
395 | FASTRPC_PHYS(buf->phys)); | |
396 | kfree(buf); | |
397 | } | |
398 | ||
6f18c7e8 | 399 | static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, |
c68cfb71 SK |
400 | u64 size, struct fastrpc_buf **obuf) |
401 | { | |
402 | struct fastrpc_buf *buf; | |
403 | ||
404 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); | |
405 | if (!buf) | |
406 | return -ENOMEM; | |
407 | ||
6cffd795 | 408 | INIT_LIST_HEAD(&buf->attachments); |
2419e55e | 409 | INIT_LIST_HEAD(&buf->node); |
6cffd795 SK |
410 | mutex_init(&buf->lock); |
411 | ||
c68cfb71 SK |
412 | buf->fl = fl; |
413 | buf->virt = NULL; | |
414 | buf->phys = 0; | |
415 | buf->size = size; | |
416 | buf->dev = dev; | |
2419e55e | 417 | buf->raddr = 0; |
c68cfb71 SK |
418 | |
419 | buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, | |
420 | GFP_KERNEL); | |
41db5f83 JRO |
421 | if (!buf->virt) { |
422 | mutex_destroy(&buf->lock); | |
423 | kfree(buf); | |
c68cfb71 | 424 | return -ENOMEM; |
41db5f83 | 425 | } |
c68cfb71 | 426 | |
6f18c7e8 AV |
427 | *obuf = buf; |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | static int fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, | |
433 | u64 size, struct fastrpc_buf **obuf) | |
434 | { | |
435 | int ret; | |
436 | struct fastrpc_buf *buf; | |
437 | ||
438 | ret = __fastrpc_buf_alloc(fl, dev, size, obuf); | |
439 | if (ret) | |
440 | return ret; | |
441 | ||
442 | buf = *obuf; | |
443 | ||
c68cfb71 SK |
444 | if (fl->sctx && fl->sctx->sid) |
445 | buf->phys += ((u64)fl->sctx->sid << 32); | |
446 | ||
c68cfb71 SK |
447 | return 0; |
448 | } | |
449 | ||
6f18c7e8 AV |
450 | static int fastrpc_remote_heap_alloc(struct fastrpc_user *fl, struct device *dev, |
451 | u64 size, struct fastrpc_buf **obuf) | |
452 | { | |
453 | struct device *rdev = &fl->cctx->rpdev->dev; | |
454 | ||
455 | return __fastrpc_buf_alloc(fl, rdev, size, obuf); | |
456 | } | |
457 | ||
278d56f9 BA |
458 | static void fastrpc_channel_ctx_free(struct kref *ref) |
459 | { | |
460 | struct fastrpc_channel_ctx *cctx; | |
461 | ||
462 | cctx = container_of(ref, struct fastrpc_channel_ctx, refcount); | |
463 | ||
464 | kfree(cctx); | |
465 | } | |
466 | ||
467 | static void fastrpc_channel_ctx_get(struct fastrpc_channel_ctx *cctx) | |
468 | { | |
469 | kref_get(&cctx->refcount); | |
470 | } | |
471 | ||
472 | static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) | |
473 | { | |
474 | kref_put(&cctx->refcount, fastrpc_channel_ctx_free); | |
475 | } | |
476 | ||
c68cfb71 SK |
477 | static void fastrpc_context_free(struct kref *ref) |
478 | { | |
479 | struct fastrpc_invoke_ctx *ctx; | |
480 | struct fastrpc_channel_ctx *cctx; | |
977e6c8d | 481 | unsigned long flags; |
c68cfb71 SK |
482 | int i; |
483 | ||
484 | ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount); | |
485 | cctx = ctx->cctx; | |
486 | ||
8f6c1d8c | 487 | for (i = 0; i < ctx->nbufs; i++) |
c68cfb71 SK |
488 | fastrpc_map_put(ctx->maps[i]); |
489 | ||
490 | if (ctx->buf) | |
491 | fastrpc_buf_free(ctx->buf); | |
492 | ||
977e6c8d | 493 | spin_lock_irqsave(&cctx->lock, flags); |
c68cfb71 | 494 | idr_remove(&cctx->ctx_idr, ctx->ctxid >> 4); |
977e6c8d | 495 | spin_unlock_irqrestore(&cctx->lock, flags); |
c68cfb71 SK |
496 | |
497 | kfree(ctx->maps); | |
25e8dfb8 | 498 | kfree(ctx->olaps); |
c68cfb71 | 499 | kfree(ctx); |
278d56f9 BA |
500 | |
501 | fastrpc_channel_ctx_put(cctx); | |
c68cfb71 SK |
502 | } |
503 | ||
504 | static void fastrpc_context_get(struct fastrpc_invoke_ctx *ctx) | |
505 | { | |
506 | kref_get(&ctx->refcount); | |
507 | } | |
508 | ||
509 | static void fastrpc_context_put(struct fastrpc_invoke_ctx *ctx) | |
510 | { | |
511 | kref_put(&ctx->refcount, fastrpc_context_free); | |
512 | } | |
513 | ||
8e7389c7 TE |
514 | static void fastrpc_context_put_wq(struct work_struct *work) |
515 | { | |
516 | struct fastrpc_invoke_ctx *ctx = | |
517 | container_of(work, struct fastrpc_invoke_ctx, put_work); | |
518 | ||
519 | fastrpc_context_put(ctx); | |
520 | } | |
521 | ||
25e8dfb8 SK |
522 | #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) |
523 | static int olaps_cmp(const void *a, const void *b) | |
524 | { | |
525 | struct fastrpc_buf_overlap *pa = (struct fastrpc_buf_overlap *)a; | |
526 | struct fastrpc_buf_overlap *pb = (struct fastrpc_buf_overlap *)b; | |
527 | /* sort with lowest starting buffer first */ | |
528 | int st = CMP(pa->start, pb->start); | |
529 | /* sort with highest ending buffer first */ | |
530 | int ed = CMP(pb->end, pa->end); | |
531 | ||
532 | return st == 0 ? ed : st; | |
533 | } | |
534 | ||
535 | static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx) | |
536 | { | |
537 | u64 max_end = 0; | |
538 | int i; | |
539 | ||
540 | for (i = 0; i < ctx->nbufs; ++i) { | |
541 | ctx->olaps[i].start = ctx->args[i].ptr; | |
542 | ctx->olaps[i].end = ctx->olaps[i].start + ctx->args[i].length; | |
543 | ctx->olaps[i].raix = i; | |
544 | } | |
545 | ||
546 | sort(ctx->olaps, ctx->nbufs, sizeof(*ctx->olaps), olaps_cmp, NULL); | |
547 | ||
548 | for (i = 0; i < ctx->nbufs; ++i) { | |
549 | /* Falling inside previous range */ | |
550 | if (ctx->olaps[i].start < max_end) { | |
551 | ctx->olaps[i].mstart = max_end; | |
552 | ctx->olaps[i].mend = ctx->olaps[i].end; | |
553 | ctx->olaps[i].offset = max_end - ctx->olaps[i].start; | |
554 | ||
555 | if (ctx->olaps[i].end > max_end) { | |
556 | max_end = ctx->olaps[i].end; | |
557 | } else { | |
558 | ctx->olaps[i].mend = 0; | |
559 | ctx->olaps[i].mstart = 0; | |
560 | } | |
561 | ||
562 | } else { | |
563 | ctx->olaps[i].mend = ctx->olaps[i].end; | |
564 | ctx->olaps[i].mstart = ctx->olaps[i].start; | |
565 | ctx->olaps[i].offset = 0; | |
566 | max_end = ctx->olaps[i].end; | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
c68cfb71 SK |
571 | static struct fastrpc_invoke_ctx *fastrpc_context_alloc( |
572 | struct fastrpc_user *user, u32 kernel, u32 sc, | |
573 | struct fastrpc_invoke_args *args) | |
574 | { | |
575 | struct fastrpc_channel_ctx *cctx = user->cctx; | |
576 | struct fastrpc_invoke_ctx *ctx = NULL; | |
977e6c8d | 577 | unsigned long flags; |
c68cfb71 SK |
578 | int ret; |
579 | ||
580 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); | |
581 | if (!ctx) | |
582 | return ERR_PTR(-ENOMEM); | |
583 | ||
584 | INIT_LIST_HEAD(&ctx->node); | |
585 | ctx->fl = user; | |
586 | ctx->nscalars = REMOTE_SCALARS_LENGTH(sc); | |
587 | ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) + | |
588 | REMOTE_SCALARS_OUTBUFS(sc); | |
589 | ||
590 | if (ctx->nscalars) { | |
591 | ctx->maps = kcalloc(ctx->nscalars, | |
592 | sizeof(*ctx->maps), GFP_KERNEL); | |
593 | if (!ctx->maps) { | |
594 | kfree(ctx); | |
595 | return ERR_PTR(-ENOMEM); | |
596 | } | |
25e8dfb8 SK |
597 | ctx->olaps = kcalloc(ctx->nscalars, |
598 | sizeof(*ctx->olaps), GFP_KERNEL); | |
599 | if (!ctx->olaps) { | |
600 | kfree(ctx->maps); | |
601 | kfree(ctx); | |
602 | return ERR_PTR(-ENOMEM); | |
603 | } | |
c68cfb71 | 604 | ctx->args = args; |
25e8dfb8 | 605 | fastrpc_get_buff_overlaps(ctx); |
c68cfb71 SK |
606 | } |
607 | ||
278d56f9 BA |
608 | /* Released in fastrpc_context_put() */ |
609 | fastrpc_channel_ctx_get(cctx); | |
610 | ||
c68cfb71 SK |
611 | ctx->sc = sc; |
612 | ctx->retval = -1; | |
613 | ctx->pid = current->pid; | |
614 | ctx->tgid = user->tgid; | |
615 | ctx->cctx = cctx; | |
616 | init_completion(&ctx->work); | |
8e7389c7 | 617 | INIT_WORK(&ctx->put_work, fastrpc_context_put_wq); |
c68cfb71 SK |
618 | |
619 | spin_lock(&user->lock); | |
620 | list_add_tail(&ctx->node, &user->pending); | |
621 | spin_unlock(&user->lock); | |
622 | ||
977e6c8d | 623 | spin_lock_irqsave(&cctx->lock, flags); |
c68cfb71 SK |
624 | ret = idr_alloc_cyclic(&cctx->ctx_idr, ctx, 1, |
625 | FASTRPC_CTX_MAX, GFP_ATOMIC); | |
626 | if (ret < 0) { | |
977e6c8d | 627 | spin_unlock_irqrestore(&cctx->lock, flags); |
c68cfb71 SK |
628 | goto err_idr; |
629 | } | |
630 | ctx->ctxid = ret << 4; | |
977e6c8d | 631 | spin_unlock_irqrestore(&cctx->lock, flags); |
c68cfb71 SK |
632 | |
633 | kref_init(&ctx->refcount); | |
634 | ||
635 | return ctx; | |
636 | err_idr: | |
637 | spin_lock(&user->lock); | |
638 | list_del(&ctx->node); | |
639 | spin_unlock(&user->lock); | |
278d56f9 | 640 | fastrpc_channel_ctx_put(cctx); |
c68cfb71 | 641 | kfree(ctx->maps); |
25e8dfb8 | 642 | kfree(ctx->olaps); |
c68cfb71 SK |
643 | kfree(ctx); |
644 | ||
645 | return ERR_PTR(ret); | |
646 | } | |
647 | ||
6cffd795 SK |
648 | static struct sg_table * |
649 | fastrpc_map_dma_buf(struct dma_buf_attachment *attachment, | |
650 | enum dma_data_direction dir) | |
651 | { | |
652 | struct fastrpc_dma_buf_attachment *a = attachment->priv; | |
653 | struct sg_table *table; | |
b212658a | 654 | int ret; |
6cffd795 SK |
655 | |
656 | table = &a->sgt; | |
657 | ||
b212658a JM |
658 | ret = dma_map_sgtable(attachment->dev, table, dir, 0); |
659 | if (ret) | |
660 | table = ERR_PTR(ret); | |
6cffd795 SK |
661 | return table; |
662 | } | |
663 | ||
664 | static void fastrpc_unmap_dma_buf(struct dma_buf_attachment *attach, | |
665 | struct sg_table *table, | |
666 | enum dma_data_direction dir) | |
667 | { | |
7cd7edb8 | 668 | dma_unmap_sgtable(attach->dev, table, dir, 0); |
6cffd795 SK |
669 | } |
670 | ||
671 | static void fastrpc_release(struct dma_buf *dmabuf) | |
672 | { | |
673 | struct fastrpc_buf *buffer = dmabuf->priv; | |
674 | ||
675 | fastrpc_buf_free(buffer); | |
676 | } | |
677 | ||
678 | static int fastrpc_dma_buf_attach(struct dma_buf *dmabuf, | |
679 | struct dma_buf_attachment *attachment) | |
680 | { | |
681 | struct fastrpc_dma_buf_attachment *a; | |
682 | struct fastrpc_buf *buffer = dmabuf->priv; | |
683 | int ret; | |
684 | ||
685 | a = kzalloc(sizeof(*a), GFP_KERNEL); | |
686 | if (!a) | |
687 | return -ENOMEM; | |
688 | ||
689 | ret = dma_get_sgtable(buffer->dev, &a->sgt, buffer->virt, | |
690 | FASTRPC_PHYS(buffer->phys), buffer->size); | |
691 | if (ret < 0) { | |
692 | dev_err(buffer->dev, "failed to get scatterlist from DMA API\n"); | |
fc739a05 | 693 | kfree(a); |
6cffd795 SK |
694 | return -EINVAL; |
695 | } | |
696 | ||
697 | a->dev = attachment->dev; | |
698 | INIT_LIST_HEAD(&a->node); | |
699 | attachment->priv = a; | |
700 | ||
701 | mutex_lock(&buffer->lock); | |
702 | list_add(&a->node, &buffer->attachments); | |
703 | mutex_unlock(&buffer->lock); | |
704 | ||
705 | return 0; | |
706 | } | |
707 | ||
708 | static void fastrpc_dma_buf_detatch(struct dma_buf *dmabuf, | |
709 | struct dma_buf_attachment *attachment) | |
710 | { | |
711 | struct fastrpc_dma_buf_attachment *a = attachment->priv; | |
712 | struct fastrpc_buf *buffer = dmabuf->priv; | |
713 | ||
714 | mutex_lock(&buffer->lock); | |
715 | list_del(&a->node); | |
716 | mutex_unlock(&buffer->lock); | |
cf61860e | 717 | sg_free_table(&a->sgt); |
6cffd795 SK |
718 | kfree(a); |
719 | } | |
720 | ||
7938f421 | 721 | static int fastrpc_vmap(struct dma_buf *dmabuf, struct iosys_map *map) |
6cffd795 SK |
722 | { |
723 | struct fastrpc_buf *buf = dmabuf->priv; | |
724 | ||
7938f421 | 725 | iosys_map_set_vaddr(map, buf->virt); |
6619ccf1 TZ |
726 | |
727 | return 0; | |
6cffd795 SK |
728 | } |
729 | ||
730 | static int fastrpc_mmap(struct dma_buf *dmabuf, | |
731 | struct vm_area_struct *vma) | |
732 | { | |
733 | struct fastrpc_buf *buf = dmabuf->priv; | |
734 | size_t size = vma->vm_end - vma->vm_start; | |
735 | ||
265751a5 DO |
736 | dma_resv_assert_held(dmabuf->resv); |
737 | ||
6cffd795 SK |
738 | return dma_mmap_coherent(buf->dev, vma, buf->virt, |
739 | FASTRPC_PHYS(buf->phys), size); | |
740 | } | |
741 | ||
742 | static const struct dma_buf_ops fastrpc_dma_buf_ops = { | |
743 | .attach = fastrpc_dma_buf_attach, | |
744 | .detach = fastrpc_dma_buf_detatch, | |
745 | .map_dma_buf = fastrpc_map_dma_buf, | |
746 | .unmap_dma_buf = fastrpc_unmap_dma_buf, | |
747 | .mmap = fastrpc_mmap, | |
6cffd795 SK |
748 | .vmap = fastrpc_vmap, |
749 | .release = fastrpc_release, | |
750 | }; | |
751 | ||
c68cfb71 | 752 | static int fastrpc_map_create(struct fastrpc_user *fl, int fd, |
e90d9119 | 753 | u64 len, u32 attr, struct fastrpc_map **ppmap) |
c68cfb71 SK |
754 | { |
755 | struct fastrpc_session_ctx *sess = fl->sctx; | |
756 | struct fastrpc_map *map = NULL; | |
757 | int err = 0; | |
758 | ||
9446fa16 | 759 | if (!fastrpc_map_lookup(fl, fd, ppmap, true)) |
c68cfb71 SK |
760 | return 0; |
761 | ||
762 | map = kzalloc(sizeof(*map), GFP_KERNEL); | |
763 | if (!map) | |
764 | return -ENOMEM; | |
765 | ||
766 | INIT_LIST_HEAD(&map->node); | |
334f1a1c AV |
767 | kref_init(&map->refcount); |
768 | ||
c68cfb71 SK |
769 | map->fl = fl; |
770 | map->fd = fd; | |
771 | map->buf = dma_buf_get(fd); | |
682a6044 WY |
772 | if (IS_ERR(map->buf)) { |
773 | err = PTR_ERR(map->buf); | |
c68cfb71 SK |
774 | goto get_err; |
775 | } | |
776 | ||
777 | map->attach = dma_buf_attach(map->buf, sess->dev); | |
778 | if (IS_ERR(map->attach)) { | |
779 | dev_err(sess->dev, "Failed to attach dmabuf\n"); | |
780 | err = PTR_ERR(map->attach); | |
781 | goto attach_err; | |
782 | } | |
783 | ||
791da5c7 | 784 | map->table = dma_buf_map_attachment_unlocked(map->attach, DMA_BIDIRECTIONAL); |
c68cfb71 SK |
785 | if (IS_ERR(map->table)) { |
786 | err = PTR_ERR(map->table); | |
787 | goto map_err; | |
788 | } | |
789 | ||
790 | map->phys = sg_dma_address(map->table->sgl); | |
791 | map->phys += ((u64)fl->sctx->sid << 32); | |
792 | map->size = len; | |
793 | map->va = sg_virt(map->table->sgl); | |
794 | map->len = len; | |
c68cfb71 | 795 | |
e90d9119 VKG |
796 | if (attr & FASTRPC_ATTR_SECUREMAP) { |
797 | /* | |
798 | * If subsystem VMIDs are defined in DTSI, then do | |
799 | * hyp_assign from HLOS to those VM(s) | |
800 | */ | |
801 | unsigned int perms = BIT(QCOM_SCM_VMID_HLOS); | |
802 | ||
803 | map->attr = attr; | |
804 | err = qcom_scm_assign_mem(map->phys, (u64)map->size, &perms, | |
805 | fl->cctx->vmperms, fl->cctx->vmcount); | |
806 | if (err) { | |
807 | dev_err(sess->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d", | |
808 | map->phys, map->size, err); | |
809 | goto map_err; | |
810 | } | |
811 | } | |
c68cfb71 SK |
812 | spin_lock(&fl->lock); |
813 | list_add_tail(&map->node, &fl->maps); | |
814 | spin_unlock(&fl->lock); | |
815 | *ppmap = map; | |
816 | ||
817 | return 0; | |
818 | ||
819 | map_err: | |
820 | dma_buf_detach(map->buf, map->attach); | |
821 | attach_err: | |
822 | dma_buf_put(map->buf); | |
823 | get_err: | |
334f1a1c | 824 | fastrpc_map_put(map); |
c68cfb71 SK |
825 | |
826 | return err; | |
827 | } | |
828 | ||
829 | /* | |
830 | * Fastrpc payload buffer with metadata looks like: | |
831 | * | |
832 | * >>>>>> START of METADATA <<<<<<<<< | |
833 | * +---------------------------------+ | |
834 | * | Arguments | | |
35a82b87 | 835 | * | type:(union fastrpc_remote_arg)| |
c68cfb71 SK |
836 | * | (0 - N) | |
837 | * +---------------------------------+ | |
838 | * | Invoke Buffer list | | |
839 | * | type:(struct fastrpc_invoke_buf)| | |
840 | * | (0 - N) | | |
841 | * +---------------------------------+ | |
842 | * | Page info list | | |
843 | * | type:(struct fastrpc_phy_page) | | |
844 | * | (0 - N) | | |
845 | * +---------------------------------+ | |
846 | * | Optional info | | |
847 | * |(can be specific to SoC/Firmware)| | |
848 | * +---------------------------------+ | |
849 | * >>>>>>>> END of METADATA <<<<<<<<< | |
850 | * +---------------------------------+ | |
851 | * | Inline ARGS | | |
852 | * | (0-N) | | |
853 | * +---------------------------------+ | |
854 | */ | |
855 | ||
856 | static int fastrpc_get_meta_size(struct fastrpc_invoke_ctx *ctx) | |
857 | { | |
858 | int size = 0; | |
859 | ||
35a82b87 | 860 | size = (sizeof(struct fastrpc_remote_buf) + |
c68cfb71 SK |
861 | sizeof(struct fastrpc_invoke_buf) + |
862 | sizeof(struct fastrpc_phy_page)) * ctx->nscalars + | |
863 | sizeof(u64) * FASTRPC_MAX_FDLIST + | |
864 | sizeof(u32) * FASTRPC_MAX_CRCLIST; | |
865 | ||
866 | return size; | |
867 | } | |
868 | ||
869 | static u64 fastrpc_get_payload_size(struct fastrpc_invoke_ctx *ctx, int metalen) | |
870 | { | |
871 | u64 size = 0; | |
3a1bf591 | 872 | int oix; |
c68cfb71 SK |
873 | |
874 | size = ALIGN(metalen, FASTRPC_ALIGN); | |
3a1bf591 J |
875 | for (oix = 0; oix < ctx->nbufs; oix++) { |
876 | int i = ctx->olaps[oix].raix; | |
877 | ||
c68cfb71 | 878 | if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1) { |
25e8dfb8 | 879 | |
3a1bf591 | 880 | if (ctx->olaps[oix].offset == 0) |
25e8dfb8 SK |
881 | size = ALIGN(size, FASTRPC_ALIGN); |
882 | ||
3a1bf591 | 883 | size += (ctx->olaps[oix].mend - ctx->olaps[oix].mstart); |
c68cfb71 SK |
884 | } |
885 | } | |
886 | ||
887 | return size; | |
888 | } | |
889 | ||
890 | static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) | |
891 | { | |
892 | struct device *dev = ctx->fl->sctx->dev; | |
893 | int i, err; | |
894 | ||
895 | for (i = 0; i < ctx->nscalars; ++i) { | |
c68cfb71 SK |
896 | |
897 | if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 || | |
898 | ctx->args[i].length == 0) | |
899 | continue; | |
900 | ||
901 | err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, | |
e90d9119 | 902 | ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]); |
c68cfb71 SK |
903 | if (err) { |
904 | dev_err(dev, "Error Creating map %d\n", err); | |
905 | return -EINVAL; | |
906 | } | |
907 | ||
908 | } | |
909 | return 0; | |
910 | } | |
911 | ||
54f7c85b VKG |
912 | static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len) |
913 | { | |
914 | return (struct fastrpc_invoke_buf *)(&pra[len]); | |
915 | } | |
916 | ||
917 | static struct fastrpc_phy_page *fastrpc_phy_page_start(struct fastrpc_invoke_buf *buf, int len) | |
918 | { | |
919 | return (struct fastrpc_phy_page *)(&buf[len]); | |
920 | } | |
921 | ||
c68cfb71 SK |
922 | static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) |
923 | { | |
924 | struct device *dev = ctx->fl->sctx->dev; | |
35a82b87 | 925 | union fastrpc_remote_arg *rpra; |
c68cfb71 SK |
926 | struct fastrpc_invoke_buf *list; |
927 | struct fastrpc_phy_page *pages; | |
25e8dfb8 SK |
928 | int inbufs, i, oix, err = 0; |
929 | u64 len, rlen, pkt_size; | |
02b45b47 | 930 | u64 pg_start, pg_end; |
c68cfb71 SK |
931 | uintptr_t args; |
932 | int metalen; | |
933 | ||
c68cfb71 SK |
934 | inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); |
935 | metalen = fastrpc_get_meta_size(ctx); | |
936 | pkt_size = fastrpc_get_payload_size(ctx, metalen); | |
937 | ||
938 | err = fastrpc_create_maps(ctx); | |
939 | if (err) | |
940 | return err; | |
941 | ||
942 | ctx->msg_sz = pkt_size; | |
943 | ||
944 | err = fastrpc_buf_alloc(ctx->fl, dev, pkt_size, &ctx->buf); | |
945 | if (err) | |
946 | return err; | |
947 | ||
948 | rpra = ctx->buf->virt; | |
54f7c85b VKG |
949 | list = fastrpc_invoke_buf_start(rpra, ctx->nscalars); |
950 | pages = fastrpc_phy_page_start(list, ctx->nscalars); | |
c68cfb71 SK |
951 | args = (uintptr_t)ctx->buf->virt + metalen; |
952 | rlen = pkt_size - metalen; | |
953 | ctx->rpra = rpra; | |
954 | ||
25e8dfb8 SK |
955 | for (oix = 0; oix < ctx->nbufs; ++oix) { |
956 | int mlen; | |
957 | ||
958 | i = ctx->olaps[oix].raix; | |
959 | len = ctx->args[i].length; | |
c68cfb71 | 960 | |
35a82b87 VKG |
961 | rpra[i].buf.pv = 0; |
962 | rpra[i].buf.len = len; | |
c68cfb71 SK |
963 | list[i].num = len ? 1 : 0; |
964 | list[i].pgidx = i; | |
965 | ||
966 | if (!len) | |
967 | continue; | |
968 | ||
c68cfb71 | 969 | if (ctx->maps[i]) { |
80f3afd7 SK |
970 | struct vm_area_struct *vma = NULL; |
971 | ||
35a82b87 | 972 | rpra[i].buf.pv = (u64) ctx->args[i].ptr; |
c68cfb71 | 973 | pages[i].addr = ctx->maps[i]->phys; |
80f3afd7 | 974 | |
f9a470db | 975 | mmap_read_lock(current->mm); |
80f3afd7 SK |
976 | vma = find_vma(current->mm, ctx->args[i].ptr); |
977 | if (vma) | |
978 | pages[i].addr += ctx->args[i].ptr - | |
979 | vma->vm_start; | |
f9a470db | 980 | mmap_read_unlock(current->mm); |
80f3afd7 | 981 | |
02b45b47 SK |
982 | pg_start = (ctx->args[i].ptr & PAGE_MASK) >> PAGE_SHIFT; |
983 | pg_end = ((ctx->args[i].ptr + len - 1) & PAGE_MASK) >> | |
984 | PAGE_SHIFT; | |
985 | pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; | |
986 | ||
c68cfb71 | 987 | } else { |
25e8dfb8 SK |
988 | |
989 | if (ctx->olaps[oix].offset == 0) { | |
990 | rlen -= ALIGN(args, FASTRPC_ALIGN) - args; | |
991 | args = ALIGN(args, FASTRPC_ALIGN); | |
992 | } | |
993 | ||
994 | mlen = ctx->olaps[oix].mend - ctx->olaps[oix].mstart; | |
995 | ||
996 | if (rlen < mlen) | |
c68cfb71 SK |
997 | goto bail; |
998 | ||
35a82b87 | 999 | rpra[i].buf.pv = args - ctx->olaps[oix].offset; |
25e8dfb8 SK |
1000 | pages[i].addr = ctx->buf->phys - |
1001 | ctx->olaps[oix].offset + | |
1002 | (pkt_size - rlen); | |
c68cfb71 | 1003 | pages[i].addr = pages[i].addr & PAGE_MASK; |
25e8dfb8 | 1004 | |
02b45b47 SK |
1005 | pg_start = (args & PAGE_MASK) >> PAGE_SHIFT; |
1006 | pg_end = ((args + len - 1) & PAGE_MASK) >> PAGE_SHIFT; | |
1007 | pages[i].size = (pg_end - pg_start + 1) * PAGE_SIZE; | |
25e8dfb8 SK |
1008 | args = args + mlen; |
1009 | rlen -= mlen; | |
c68cfb71 SK |
1010 | } |
1011 | ||
1012 | if (i < inbufs && !ctx->maps[i]) { | |
35a82b87 | 1013 | void *dst = (void *)(uintptr_t)rpra[i].buf.pv; |
c68cfb71 SK |
1014 | void *src = (void *)(uintptr_t)ctx->args[i].ptr; |
1015 | ||
1016 | if (!kernel) { | |
1017 | if (copy_from_user(dst, (void __user *)src, | |
1018 | len)) { | |
1019 | err = -EFAULT; | |
1020 | goto bail; | |
1021 | } | |
1022 | } else { | |
1023 | memcpy(dst, src, len); | |
1024 | } | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | for (i = ctx->nbufs; i < ctx->nscalars; ++i) { | |
c68cfb71 SK |
1029 | list[i].num = ctx->args[i].length ? 1 : 0; |
1030 | list[i].pgidx = i; | |
35a82b87 VKG |
1031 | if (ctx->maps[i]) { |
1032 | pages[i].addr = ctx->maps[i]->phys; | |
1033 | pages[i].size = ctx->maps[i]->size; | |
1034 | } | |
1035 | rpra[i].dma.fd = ctx->args[i].fd; | |
1036 | rpra[i].dma.len = ctx->args[i].length; | |
1037 | rpra[i].dma.offset = (u64) ctx->args[i].ptr; | |
c68cfb71 SK |
1038 | } |
1039 | ||
1040 | bail: | |
1041 | if (err) | |
1042 | dev_err(dev, "Error: get invoke args failed:%d\n", err); | |
1043 | ||
1044 | return err; | |
1045 | } | |
1046 | ||
1047 | static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, | |
1048 | u32 kernel) | |
1049 | { | |
35a82b87 | 1050 | union fastrpc_remote_arg *rpra = ctx->rpra; |
8f6c1d8c VKG |
1051 | struct fastrpc_user *fl = ctx->fl; |
1052 | struct fastrpc_map *mmap = NULL; | |
1053 | struct fastrpc_invoke_buf *list; | |
1054 | struct fastrpc_phy_page *pages; | |
1055 | u64 *fdlist; | |
1056 | int i, inbufs, outbufs, handles; | |
c68cfb71 SK |
1057 | |
1058 | inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); | |
8f6c1d8c VKG |
1059 | outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc); |
1060 | handles = REMOTE_SCALARS_INHANDLES(ctx->sc) + REMOTE_SCALARS_OUTHANDLES(ctx->sc); | |
1061 | list = fastrpc_invoke_buf_start(rpra, ctx->nscalars); | |
1062 | pages = fastrpc_phy_page_start(list, ctx->nscalars); | |
1063 | fdlist = (uint64_t *)(pages + inbufs + outbufs + handles); | |
c68cfb71 SK |
1064 | |
1065 | for (i = inbufs; i < ctx->nbufs; ++i) { | |
847afd7b | 1066 | if (!ctx->maps[i]) { |
35a82b87 | 1067 | void *src = (void *)(uintptr_t)rpra[i].buf.pv; |
847afd7b | 1068 | void *dst = (void *)(uintptr_t)ctx->args[i].ptr; |
35a82b87 | 1069 | u64 len = rpra[i].buf.len; |
c68cfb71 | 1070 | |
847afd7b J |
1071 | if (!kernel) { |
1072 | if (copy_to_user((void __user *)dst, src, len)) | |
1073 | return -EFAULT; | |
1074 | } else { | |
1075 | memcpy(dst, src, len); | |
1076 | } | |
c68cfb71 SK |
1077 | } |
1078 | } | |
1079 | ||
8f6c1d8c VKG |
1080 | for (i = 0; i < FASTRPC_MAX_FDLIST; i++) { |
1081 | if (!fdlist[i]) | |
1082 | break; | |
9446fa16 | 1083 | if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false)) |
8f6c1d8c VKG |
1084 | fastrpc_map_put(mmap); |
1085 | } | |
1086 | ||
c68cfb71 SK |
1087 | return 0; |
1088 | } | |
1089 | ||
1090 | static int fastrpc_invoke_send(struct fastrpc_session_ctx *sctx, | |
1091 | struct fastrpc_invoke_ctx *ctx, | |
1092 | u32 kernel, uint32_t handle) | |
1093 | { | |
1094 | struct fastrpc_channel_ctx *cctx; | |
1095 | struct fastrpc_user *fl = ctx->fl; | |
1096 | struct fastrpc_msg *msg = &ctx->msg; | |
74003385 | 1097 | int ret; |
c68cfb71 SK |
1098 | |
1099 | cctx = fl->cctx; | |
1100 | msg->pid = fl->tgid; | |
1101 | msg->tid = current->pid; | |
1102 | ||
1103 | if (kernel) | |
1104 | msg->pid = 0; | |
1105 | ||
1106 | msg->ctx = ctx->ctxid | fl->pd; | |
1107 | msg->handle = handle; | |
1108 | msg->sc = ctx->sc; | |
1109 | msg->addr = ctx->buf ? ctx->buf->phys : 0; | |
1110 | msg->size = roundup(ctx->msg_sz, PAGE_SIZE); | |
1111 | fastrpc_context_get(ctx); | |
1112 | ||
74003385 SK |
1113 | ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg)); |
1114 | ||
1115 | if (ret) | |
1116 | fastrpc_context_put(ctx); | |
1117 | ||
1118 | return ret; | |
1119 | ||
c68cfb71 SK |
1120 | } |
1121 | ||
1122 | static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, | |
1123 | u32 handle, u32 sc, | |
1124 | struct fastrpc_invoke_args *args) | |
1125 | { | |
1126 | struct fastrpc_invoke_ctx *ctx = NULL; | |
76e8e4ac AV |
1127 | struct fastrpc_buf *buf, *b; |
1128 | ||
c68cfb71 SK |
1129 | int err = 0; |
1130 | ||
1131 | if (!fl->sctx) | |
1132 | return -EINVAL; | |
1133 | ||
2e369878 BA |
1134 | if (!fl->cctx->rpdev) |
1135 | return -EPIPE; | |
1136 | ||
20c40794 DB |
1137 | if (handle == FASTRPC_INIT_HANDLE && !kernel) { |
1138 | dev_warn_ratelimited(fl->sctx->dev, "user app trying to send a kernel RPC message (%d)\n", handle); | |
1139 | return -EPERM; | |
1140 | } | |
1141 | ||
c68cfb71 SK |
1142 | ctx = fastrpc_context_alloc(fl, kernel, sc, args); |
1143 | if (IS_ERR(ctx)) | |
1144 | return PTR_ERR(ctx); | |
1145 | ||
1146 | if (ctx->nscalars) { | |
1147 | err = fastrpc_get_args(kernel, ctx); | |
1148 | if (err) | |
1149 | goto bail; | |
1150 | } | |
415a0729 SK |
1151 | |
1152 | /* make sure that all CPU memory writes are seen by DSP */ | |
1153 | dma_wmb(); | |
c68cfb71 SK |
1154 | /* Send invoke buffer to remote dsp */ |
1155 | err = fastrpc_invoke_send(fl->sctx, ctx, kernel, handle); | |
1156 | if (err) | |
1157 | goto bail; | |
1158 | ||
55bcda35 JRO |
1159 | if (kernel) { |
1160 | if (!wait_for_completion_timeout(&ctx->work, 10 * HZ)) | |
1161 | err = -ETIMEDOUT; | |
1162 | } else { | |
1163 | err = wait_for_completion_interruptible(&ctx->work); | |
1164 | } | |
1165 | ||
c68cfb71 SK |
1166 | if (err) |
1167 | goto bail; | |
1168 | ||
1169 | /* Check the response from remote dsp */ | |
1170 | err = ctx->retval; | |
1171 | if (err) | |
1172 | goto bail; | |
1173 | ||
1174 | if (ctx->nscalars) { | |
415a0729 SK |
1175 | /* make sure that all memory writes by DSP are seen by CPU */ |
1176 | dma_rmb(); | |
c68cfb71 SK |
1177 | /* populate all the output buffers with results */ |
1178 | err = fastrpc_put_args(ctx, kernel); | |
1179 | if (err) | |
1180 | goto bail; | |
1181 | } | |
1182 | ||
1183 | bail: | |
387f6255 JRO |
1184 | if (err != -ERESTARTSYS && err != -ETIMEDOUT) { |
1185 | /* We are done with this compute context */ | |
1186 | spin_lock(&fl->lock); | |
1187 | list_del(&ctx->node); | |
1188 | spin_unlock(&fl->lock); | |
1189 | fastrpc_context_put(ctx); | |
1190 | } | |
08715610 | 1191 | |
76e8e4ac AV |
1192 | if (err == -ERESTARTSYS) { |
1193 | list_for_each_entry_safe(buf, b, &fl->mmaps, node) { | |
1194 | list_del(&buf->node); | |
1195 | list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps); | |
1196 | } | |
1197 | } | |
1198 | ||
c68cfb71 SK |
1199 | if (err) |
1200 | dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err); | |
1201 | ||
1202 | return err; | |
1203 | } | |
1204 | ||
7f1f4812 J |
1205 | static bool is_session_rejected(struct fastrpc_user *fl, bool unsigned_pd_request) |
1206 | { | |
1207 | /* Check if the device node is non-secure and channel is secure*/ | |
1208 | if (!fl->is_secure_dev && fl->cctx->secure) { | |
1209 | /* | |
1210 | * Allow untrusted applications to offload only to Unsigned PD when | |
1211 | * channel is configured as secure and block untrusted apps on channel | |
1212 | * that does not support unsigned PD offload | |
1213 | */ | |
1214 | if (!fl->cctx->unsigned_support || !unsigned_pd_request) { | |
1215 | dev_err(&fl->cctx->rpdev->dev, "Error: Untrusted application trying to offload to signed PD"); | |
1216 | return true; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | return false; | |
1221 | } | |
1222 | ||
08715610 AV |
1223 | static int fastrpc_init_create_static_process(struct fastrpc_user *fl, |
1224 | char __user *argp) | |
1225 | { | |
1226 | struct fastrpc_init_create_static init; | |
1227 | struct fastrpc_invoke_args *args; | |
1228 | struct fastrpc_phy_page pages[1]; | |
1229 | char *name; | |
1230 | int err; | |
1231 | struct { | |
1232 | int pgid; | |
1233 | u32 namelen; | |
1234 | u32 pageslen; | |
1235 | } inbuf; | |
1236 | u32 sc; | |
1237 | ||
1238 | args = kcalloc(FASTRPC_CREATE_STATIC_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); | |
1239 | if (!args) | |
1240 | return -ENOMEM; | |
1241 | ||
1242 | if (copy_from_user(&init, argp, sizeof(init))) { | |
1243 | err = -EFAULT; | |
1244 | goto err; | |
1245 | } | |
1246 | ||
1247 | if (init.namelen > INIT_FILE_NAMELEN_MAX) { | |
1248 | err = -EINVAL; | |
1249 | goto err; | |
1250 | } | |
1251 | ||
1252 | name = kzalloc(init.namelen, GFP_KERNEL); | |
1253 | if (!name) { | |
1254 | err = -ENOMEM; | |
1255 | goto err; | |
1256 | } | |
1257 | ||
1258 | if (copy_from_user(name, (void __user *)(uintptr_t)init.name, init.namelen)) { | |
1259 | err = -EFAULT; | |
1260 | goto err_name; | |
1261 | } | |
1262 | ||
1263 | if (!fl->cctx->remote_heap) { | |
1264 | err = fastrpc_remote_heap_alloc(fl, fl->sctx->dev, init.memlen, | |
1265 | &fl->cctx->remote_heap); | |
1266 | if (err) | |
1267 | goto err_name; | |
1268 | ||
1269 | /* Map if we have any heap VMIDs associated with this ADSP Static Process. */ | |
1270 | if (fl->cctx->vmcount) { | |
1271 | unsigned int perms = BIT(QCOM_SCM_VMID_HLOS); | |
1272 | ||
1273 | err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys, | |
1274 | (u64)fl->cctx->remote_heap->size, &perms, | |
1275 | fl->cctx->vmperms, fl->cctx->vmcount); | |
1276 | if (err) { | |
1277 | dev_err(fl->sctx->dev, "Failed to assign memory with phys 0x%llx size 0x%llx err %d", | |
1278 | fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err); | |
1279 | goto err_map; | |
1280 | } | |
1281 | } | |
1282 | } | |
1283 | ||
1284 | inbuf.pgid = fl->tgid; | |
1285 | inbuf.namelen = init.namelen; | |
1286 | inbuf.pageslen = 0; | |
1287 | fl->pd = USER_PD; | |
1288 | ||
1289 | args[0].ptr = (u64)(uintptr_t)&inbuf; | |
1290 | args[0].length = sizeof(inbuf); | |
1291 | args[0].fd = -1; | |
1292 | ||
1293 | args[1].ptr = (u64)(uintptr_t)name; | |
1294 | args[1].length = inbuf.namelen; | |
1295 | args[1].fd = -1; | |
1296 | ||
1297 | pages[0].addr = fl->cctx->remote_heap->phys; | |
1298 | pages[0].size = fl->cctx->remote_heap->size; | |
1299 | ||
1300 | args[2].ptr = (u64)(uintptr_t) pages; | |
1301 | args[2].length = sizeof(*pages); | |
1302 | args[2].fd = -1; | |
1303 | ||
1304 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_STATIC, 3, 0); | |
1305 | ||
1306 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
1307 | sc, args); | |
1308 | if (err) | |
1309 | goto err_invoke; | |
1310 | ||
1311 | kfree(args); | |
1312 | ||
1313 | return 0; | |
1314 | err_invoke: | |
1315 | if (fl->cctx->vmcount) { | |
1316 | struct qcom_scm_vmperm perm; | |
1317 | ||
1318 | perm.vmid = QCOM_SCM_VMID_HLOS; | |
1319 | perm.perm = QCOM_SCM_PERM_RWX; | |
1320 | err = qcom_scm_assign_mem(fl->cctx->remote_heap->phys, | |
1321 | (u64)fl->cctx->remote_heap->size, | |
1322 | &(fl->cctx->vmperms[0].vmid), &perm, 1); | |
1323 | if (err) | |
1324 | dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d", | |
1325 | fl->cctx->remote_heap->phys, fl->cctx->remote_heap->size, err); | |
1326 | } | |
1327 | err_map: | |
1328 | fastrpc_buf_free(fl->cctx->remote_heap); | |
1329 | err_name: | |
1330 | kfree(name); | |
1331 | err: | |
1332 | kfree(args); | |
1333 | ||
1334 | return err; | |
1335 | } | |
1336 | ||
d73f71c7 SK |
1337 | static int fastrpc_init_create_process(struct fastrpc_user *fl, |
1338 | char __user *argp) | |
1339 | { | |
1340 | struct fastrpc_init_create init; | |
1341 | struct fastrpc_invoke_args *args; | |
1342 | struct fastrpc_phy_page pages[1]; | |
1343 | struct fastrpc_map *map = NULL; | |
1344 | struct fastrpc_buf *imem = NULL; | |
1345 | int memlen; | |
1346 | int err; | |
1347 | struct { | |
1348 | int pgid; | |
1349 | u32 namelen; | |
1350 | u32 filelen; | |
1351 | u32 pageslen; | |
1352 | u32 attrs; | |
1353 | u32 siglen; | |
1354 | } inbuf; | |
1355 | u32 sc; | |
7f1f4812 | 1356 | bool unsigned_module = false; |
d73f71c7 SK |
1357 | |
1358 | args = kcalloc(FASTRPC_CREATE_PROCESS_NARGS, sizeof(*args), GFP_KERNEL); | |
1359 | if (!args) | |
1360 | return -ENOMEM; | |
1361 | ||
1362 | if (copy_from_user(&init, argp, sizeof(init))) { | |
1363 | err = -EFAULT; | |
b49f6d83 | 1364 | goto err; |
d73f71c7 SK |
1365 | } |
1366 | ||
7f1f4812 J |
1367 | if (init.attrs & FASTRPC_MODE_UNSIGNED_MODULE) |
1368 | unsigned_module = true; | |
1369 | ||
1370 | if (is_session_rejected(fl, unsigned_module)) { | |
1371 | err = -ECONNREFUSED; | |
1372 | goto err; | |
1373 | } | |
1374 | ||
d73f71c7 SK |
1375 | if (init.filelen > INIT_FILELEN_MAX) { |
1376 | err = -EINVAL; | |
b49f6d83 | 1377 | goto err; |
d73f71c7 SK |
1378 | } |
1379 | ||
1380 | inbuf.pgid = fl->tgid; | |
1381 | inbuf.namelen = strlen(current->comm) + 1; | |
1382 | inbuf.filelen = init.filelen; | |
1383 | inbuf.pageslen = 1; | |
1384 | inbuf.attrs = init.attrs; | |
1385 | inbuf.siglen = init.siglen; | |
84195d20 | 1386 | fl->pd = USER_PD; |
d73f71c7 SK |
1387 | |
1388 | if (init.filelen && init.filefd) { | |
e90d9119 | 1389 | err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map); |
d73f71c7 | 1390 | if (err) |
b49f6d83 | 1391 | goto err; |
d73f71c7 SK |
1392 | } |
1393 | ||
1394 | memlen = ALIGN(max(INIT_FILELEN_MAX, (int)init.filelen * 4), | |
1395 | 1024 * 1024); | |
1396 | err = fastrpc_buf_alloc(fl, fl->sctx->dev, memlen, | |
1397 | &imem); | |
b49f6d83 TE |
1398 | if (err) |
1399 | goto err_alloc; | |
d73f71c7 SK |
1400 | |
1401 | fl->init_mem = imem; | |
1402 | args[0].ptr = (u64)(uintptr_t)&inbuf; | |
1403 | args[0].length = sizeof(inbuf); | |
1404 | args[0].fd = -1; | |
1405 | ||
1406 | args[1].ptr = (u64)(uintptr_t)current->comm; | |
1407 | args[1].length = inbuf.namelen; | |
1408 | args[1].fd = -1; | |
1409 | ||
1410 | args[2].ptr = (u64) init.file; | |
1411 | args[2].length = inbuf.filelen; | |
1412 | args[2].fd = init.filefd; | |
1413 | ||
1414 | pages[0].addr = imem->phys; | |
1415 | pages[0].size = imem->size; | |
1416 | ||
1417 | args[3].ptr = (u64)(uintptr_t) pages; | |
1418 | args[3].length = 1 * sizeof(*pages); | |
1419 | args[3].fd = -1; | |
1420 | ||
1421 | args[4].ptr = (u64)(uintptr_t)&inbuf.attrs; | |
1422 | args[4].length = sizeof(inbuf.attrs); | |
1423 | args[4].fd = -1; | |
1424 | ||
1425 | args[5].ptr = (u64)(uintptr_t) &inbuf.siglen; | |
1426 | args[5].length = sizeof(inbuf.siglen); | |
1427 | args[5].fd = -1; | |
1428 | ||
1429 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE, 4, 0); | |
1430 | if (init.attrs) | |
1431 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_CREATE_ATTR, 6, 0); | |
1432 | ||
1433 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
1434 | sc, args); | |
b49f6d83 TE |
1435 | if (err) |
1436 | goto err_invoke; | |
1437 | ||
1438 | kfree(args); | |
d73f71c7 | 1439 | |
b49f6d83 TE |
1440 | return 0; |
1441 | ||
1442 | err_invoke: | |
1443 | fl->init_mem = NULL; | |
1444 | fastrpc_buf_free(imem); | |
1445 | err_alloc: | |
5bb96c8f | 1446 | fastrpc_map_put(map); |
b49f6d83 | 1447 | err: |
d73f71c7 SK |
1448 | kfree(args); |
1449 | ||
1450 | return err; | |
1451 | } | |
1452 | ||
f6f9279f SK |
1453 | static struct fastrpc_session_ctx *fastrpc_session_alloc( |
1454 | struct fastrpc_channel_ctx *cctx) | |
1455 | { | |
1456 | struct fastrpc_session_ctx *session = NULL; | |
977e6c8d | 1457 | unsigned long flags; |
f6f9279f SK |
1458 | int i; |
1459 | ||
977e6c8d | 1460 | spin_lock_irqsave(&cctx->lock, flags); |
f6f9279f SK |
1461 | for (i = 0; i < cctx->sesscount; i++) { |
1462 | if (!cctx->session[i].used && cctx->session[i].valid) { | |
1463 | cctx->session[i].used = true; | |
1464 | session = &cctx->session[i]; | |
1465 | break; | |
1466 | } | |
1467 | } | |
977e6c8d | 1468 | spin_unlock_irqrestore(&cctx->lock, flags); |
f6f9279f SK |
1469 | |
1470 | return session; | |
1471 | } | |
1472 | ||
1473 | static void fastrpc_session_free(struct fastrpc_channel_ctx *cctx, | |
1474 | struct fastrpc_session_ctx *session) | |
1475 | { | |
977e6c8d SK |
1476 | unsigned long flags; |
1477 | ||
1478 | spin_lock_irqsave(&cctx->lock, flags); | |
f6f9279f | 1479 | session->used = false; |
977e6c8d | 1480 | spin_unlock_irqrestore(&cctx->lock, flags); |
f6f9279f SK |
1481 | } |
1482 | ||
d73f71c7 SK |
1483 | static int fastrpc_release_current_dsp_process(struct fastrpc_user *fl) |
1484 | { | |
1485 | struct fastrpc_invoke_args args[1]; | |
1486 | int tgid = 0; | |
1487 | u32 sc; | |
1488 | ||
1489 | tgid = fl->tgid; | |
1490 | args[0].ptr = (u64)(uintptr_t) &tgid; | |
1491 | args[0].length = sizeof(tgid); | |
1492 | args[0].fd = -1; | |
d73f71c7 SK |
1493 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_RELEASE, 1, 0); |
1494 | ||
1495 | return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
1496 | sc, &args[0]); | |
1497 | } | |
1498 | ||
f6f9279f SK |
1499 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
1500 | { | |
1501 | struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; | |
1502 | struct fastrpc_channel_ctx *cctx = fl->cctx; | |
c68cfb71 SK |
1503 | struct fastrpc_invoke_ctx *ctx, *n; |
1504 | struct fastrpc_map *map, *m; | |
2419e55e | 1505 | struct fastrpc_buf *buf, *b; |
977e6c8d | 1506 | unsigned long flags; |
f6f9279f | 1507 | |
d73f71c7 SK |
1508 | fastrpc_release_current_dsp_process(fl); |
1509 | ||
977e6c8d | 1510 | spin_lock_irqsave(&cctx->lock, flags); |
f6f9279f | 1511 | list_del(&fl->user); |
977e6c8d | 1512 | spin_unlock_irqrestore(&cctx->lock, flags); |
f6f9279f | 1513 | |
c68cfb71 SK |
1514 | if (fl->init_mem) |
1515 | fastrpc_buf_free(fl->init_mem); | |
1516 | ||
1517 | list_for_each_entry_safe(ctx, n, &fl->pending, node) { | |
1518 | list_del(&ctx->node); | |
1519 | fastrpc_context_put(ctx); | |
1520 | } | |
1521 | ||
5bb96c8f | 1522 | list_for_each_entry_safe(map, m, &fl->maps, node) |
c68cfb71 | 1523 | fastrpc_map_put(map); |
c68cfb71 | 1524 | |
2419e55e JRO |
1525 | list_for_each_entry_safe(buf, b, &fl->mmaps, node) { |
1526 | list_del(&buf->node); | |
1527 | fastrpc_buf_free(buf); | |
1528 | } | |
1529 | ||
f6f9279f | 1530 | fastrpc_session_free(cctx, fl->sctx); |
278d56f9 | 1531 | fastrpc_channel_ctx_put(cctx); |
f6f9279f SK |
1532 | |
1533 | mutex_destroy(&fl->mutex); | |
1534 | kfree(fl); | |
1535 | file->private_data = NULL; | |
1536 | ||
1537 | return 0; | |
1538 | } | |
1539 | ||
1540 | static int fastrpc_device_open(struct inode *inode, struct file *filp) | |
1541 | { | |
965602ea SK |
1542 | struct fastrpc_channel_ctx *cctx; |
1543 | struct fastrpc_device *fdevice; | |
f6f9279f | 1544 | struct fastrpc_user *fl = NULL; |
977e6c8d | 1545 | unsigned long flags; |
f6f9279f | 1546 | |
965602ea SK |
1547 | fdevice = miscdev_to_fdevice(filp->private_data); |
1548 | cctx = fdevice->cctx; | |
1549 | ||
f6f9279f SK |
1550 | fl = kzalloc(sizeof(*fl), GFP_KERNEL); |
1551 | if (!fl) | |
1552 | return -ENOMEM; | |
1553 | ||
278d56f9 BA |
1554 | /* Released in fastrpc_device_release() */ |
1555 | fastrpc_channel_ctx_get(cctx); | |
1556 | ||
f6f9279f SK |
1557 | filp->private_data = fl; |
1558 | spin_lock_init(&fl->lock); | |
1559 | mutex_init(&fl->mutex); | |
1560 | INIT_LIST_HEAD(&fl->pending); | |
1561 | INIT_LIST_HEAD(&fl->maps); | |
2419e55e | 1562 | INIT_LIST_HEAD(&fl->mmaps); |
f6f9279f SK |
1563 | INIT_LIST_HEAD(&fl->user); |
1564 | fl->tgid = current->tgid; | |
1565 | fl->cctx = cctx; | |
7f1f4812 | 1566 | fl->is_secure_dev = fdevice->secure; |
7c11df42 TE |
1567 | |
1568 | fl->sctx = fastrpc_session_alloc(cctx); | |
1569 | if (!fl->sctx) { | |
1570 | dev_err(&cctx->rpdev->dev, "No session available\n"); | |
1571 | mutex_destroy(&fl->mutex); | |
1572 | kfree(fl); | |
1573 | ||
1574 | return -EBUSY; | |
1575 | } | |
1576 | ||
977e6c8d | 1577 | spin_lock_irqsave(&cctx->lock, flags); |
f6f9279f | 1578 | list_add_tail(&fl->user, &cctx->users); |
977e6c8d | 1579 | spin_unlock_irqrestore(&cctx->lock, flags); |
f6f9279f SK |
1580 | |
1581 | return 0; | |
1582 | } | |
1583 | ||
6cffd795 SK |
1584 | static int fastrpc_dmabuf_alloc(struct fastrpc_user *fl, char __user *argp) |
1585 | { | |
1586 | struct fastrpc_alloc_dma_buf bp; | |
1587 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); | |
1588 | struct fastrpc_buf *buf = NULL; | |
1589 | int err; | |
1590 | ||
1591 | if (copy_from_user(&bp, argp, sizeof(bp))) | |
1592 | return -EFAULT; | |
1593 | ||
1594 | err = fastrpc_buf_alloc(fl, fl->sctx->dev, bp.size, &buf); | |
1595 | if (err) | |
1596 | return err; | |
1597 | exp_info.ops = &fastrpc_dma_buf_ops; | |
1598 | exp_info.size = bp.size; | |
1599 | exp_info.flags = O_RDWR; | |
1600 | exp_info.priv = buf; | |
1601 | buf->dmabuf = dma_buf_export(&exp_info); | |
1602 | if (IS_ERR(buf->dmabuf)) { | |
1603 | err = PTR_ERR(buf->dmabuf); | |
1604 | fastrpc_buf_free(buf); | |
1605 | return err; | |
1606 | } | |
1607 | ||
1608 | bp.fd = dma_buf_fd(buf->dmabuf, O_ACCMODE); | |
1609 | if (bp.fd < 0) { | |
1610 | dma_buf_put(buf->dmabuf); | |
1611 | return -EINVAL; | |
1612 | } | |
1613 | ||
1614 | if (copy_to_user(argp, &bp, sizeof(bp))) { | |
46963e2e MK |
1615 | /* |
1616 | * The usercopy failed, but we can't do much about it, as | |
1617 | * dma_buf_fd() already called fd_install() and made the | |
1618 | * file descriptor accessible for the current process. It | |
1619 | * might already be closed and dmabuf no longer valid when | |
1620 | * we reach this point. Therefore "leak" the fd and rely on | |
1621 | * the process exit path to do any required cleanup. | |
1622 | */ | |
6cffd795 SK |
1623 | return -EFAULT; |
1624 | } | |
1625 | ||
6cffd795 SK |
1626 | return 0; |
1627 | } | |
1628 | ||
6010d9be | 1629 | static int fastrpc_init_attach(struct fastrpc_user *fl, int pd) |
d73f71c7 SK |
1630 | { |
1631 | struct fastrpc_invoke_args args[1]; | |
1632 | int tgid = fl->tgid; | |
1633 | u32 sc; | |
1634 | ||
1635 | args[0].ptr = (u64)(uintptr_t) &tgid; | |
1636 | args[0].length = sizeof(tgid); | |
1637 | args[0].fd = -1; | |
d73f71c7 | 1638 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_ATTACH, 1, 0); |
6010d9be | 1639 | fl->pd = pd; |
d73f71c7 SK |
1640 | |
1641 | return fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, | |
1642 | sc, &args[0]); | |
1643 | } | |
1644 | ||
c68cfb71 SK |
1645 | static int fastrpc_invoke(struct fastrpc_user *fl, char __user *argp) |
1646 | { | |
1647 | struct fastrpc_invoke_args *args = NULL; | |
1648 | struct fastrpc_invoke inv; | |
1649 | u32 nscalars; | |
1650 | int err; | |
1651 | ||
1652 | if (copy_from_user(&inv, argp, sizeof(inv))) | |
1653 | return -EFAULT; | |
1654 | ||
1655 | /* nscalars is truncated here to max supported value */ | |
1656 | nscalars = REMOTE_SCALARS_LENGTH(inv.sc); | |
1657 | if (nscalars) { | |
1658 | args = kcalloc(nscalars, sizeof(*args), GFP_KERNEL); | |
1659 | if (!args) | |
1660 | return -ENOMEM; | |
1661 | ||
1662 | if (copy_from_user(args, (void __user *)(uintptr_t)inv.args, | |
1663 | nscalars * sizeof(*args))) { | |
1664 | kfree(args); | |
1665 | return -EFAULT; | |
1666 | } | |
1667 | } | |
1668 | ||
1669 | err = fastrpc_internal_invoke(fl, false, inv.handle, inv.sc, args); | |
1670 | kfree(args); | |
1671 | ||
1672 | return err; | |
1673 | } | |
1674 | ||
6c16fd8b J |
1675 | static int fastrpc_get_info_from_dsp(struct fastrpc_user *fl, uint32_t *dsp_attr_buf, |
1676 | uint32_t dsp_attr_buf_len) | |
1677 | { | |
1678 | struct fastrpc_invoke_args args[2] = { 0 }; | |
1679 | ||
1680 | /* Capability filled in userspace */ | |
1681 | dsp_attr_buf[0] = 0; | |
1682 | ||
1683 | args[0].ptr = (u64)(uintptr_t)&dsp_attr_buf_len; | |
1684 | args[0].length = sizeof(dsp_attr_buf_len); | |
1685 | args[0].fd = -1; | |
1686 | args[1].ptr = (u64)(uintptr_t)&dsp_attr_buf[1]; | |
1687 | args[1].length = dsp_attr_buf_len; | |
1688 | args[1].fd = -1; | |
f667f56b | 1689 | fl->pd = USER_PD; |
6c16fd8b J |
1690 | |
1691 | return fastrpc_internal_invoke(fl, true, FASTRPC_DSP_UTILITIES_HANDLE, | |
1692 | FASTRPC_SCALARS(0, 1, 1), args); | |
1693 | } | |
1694 | ||
1695 | static int fastrpc_get_info_from_kernel(struct fastrpc_ioctl_capability *cap, | |
1696 | struct fastrpc_user *fl) | |
1697 | { | |
1698 | struct fastrpc_channel_ctx *cctx = fl->cctx; | |
1699 | uint32_t attribute_id = cap->attribute_id; | |
1700 | uint32_t *dsp_attributes; | |
1701 | unsigned long flags; | |
1702 | uint32_t domain = cap->domain; | |
1703 | int err; | |
1704 | ||
1705 | spin_lock_irqsave(&cctx->lock, flags); | |
1706 | /* check if we already have queried dsp for attributes */ | |
1707 | if (cctx->valid_attributes) { | |
1708 | spin_unlock_irqrestore(&cctx->lock, flags); | |
1709 | goto done; | |
1710 | } | |
1711 | spin_unlock_irqrestore(&cctx->lock, flags); | |
1712 | ||
1713 | dsp_attributes = kzalloc(FASTRPC_MAX_DSP_ATTRIBUTES_LEN, GFP_KERNEL); | |
1714 | if (!dsp_attributes) | |
1715 | return -ENOMEM; | |
1716 | ||
1717 | err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN); | |
1718 | if (err == DSP_UNSUPPORTED_API) { | |
1719 | dev_info(&cctx->rpdev->dev, | |
1720 | "Warning: DSP capabilities not supported on domain: %d\n", domain); | |
1721 | kfree(dsp_attributes); | |
1722 | return -EOPNOTSUPP; | |
1723 | } else if (err) { | |
1724 | dev_err(&cctx->rpdev->dev, "Error: dsp information is incorrect err: %d\n", err); | |
1725 | kfree(dsp_attributes); | |
1726 | return err; | |
1727 | } | |
1728 | ||
1729 | spin_lock_irqsave(&cctx->lock, flags); | |
1730 | memcpy(cctx->dsp_attributes, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES_LEN); | |
1731 | cctx->valid_attributes = true; | |
1732 | spin_unlock_irqrestore(&cctx->lock, flags); | |
1733 | kfree(dsp_attributes); | |
1734 | done: | |
1735 | cap->capability = cctx->dsp_attributes[attribute_id]; | |
1736 | return 0; | |
1737 | } | |
1738 | ||
1739 | static int fastrpc_get_dsp_info(struct fastrpc_user *fl, char __user *argp) | |
1740 | { | |
1741 | struct fastrpc_ioctl_capability cap = {0}; | |
1742 | int err = 0; | |
1743 | ||
1744 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
1745 | return -EFAULT; | |
1746 | ||
1747 | cap.capability = 0; | |
1748 | if (cap.domain >= FASTRPC_DEV_MAX) { | |
1749 | dev_err(&fl->cctx->rpdev->dev, "Error: Invalid domain id:%d, err:%d\n", | |
1750 | cap.domain, err); | |
1751 | return -ECHRNG; | |
1752 | } | |
1753 | ||
1754 | /* Fastrpc Capablities does not support modem domain */ | |
1755 | if (cap.domain == MDSP_DOMAIN_ID) { | |
1756 | dev_err(&fl->cctx->rpdev->dev, "Error: modem not supported %d\n", err); | |
1757 | return -ECHRNG; | |
1758 | } | |
1759 | ||
1760 | if (cap.attribute_id >= FASTRPC_MAX_DSP_ATTRIBUTES) { | |
1761 | dev_err(&fl->cctx->rpdev->dev, "Error: invalid attribute: %d, err: %d\n", | |
1762 | cap.attribute_id, err); | |
1763 | return -EOVERFLOW; | |
1764 | } | |
1765 | ||
1766 | err = fastrpc_get_info_from_kernel(&cap, fl); | |
1767 | if (err) | |
1768 | return err; | |
1769 | ||
1770 | if (copy_to_user(argp, &cap.capability, sizeof(cap.capability))) | |
1771 | return -EFAULT; | |
1772 | ||
1773 | return 0; | |
1774 | } | |
1775 | ||
72fa6f78 | 1776 | static int fastrpc_req_munmap_impl(struct fastrpc_user *fl, struct fastrpc_buf *buf) |
2419e55e JRO |
1777 | { |
1778 | struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; | |
2419e55e JRO |
1779 | struct fastrpc_munmap_req_msg req_msg; |
1780 | struct device *dev = fl->sctx->dev; | |
1781 | int err; | |
1782 | u32 sc; | |
1783 | ||
2419e55e JRO |
1784 | req_msg.pgid = fl->tgid; |
1785 | req_msg.size = buf->size; | |
1786 | req_msg.vaddr = buf->raddr; | |
1787 | ||
1788 | args[0].ptr = (u64) (uintptr_t) &req_msg; | |
1789 | args[0].length = sizeof(req_msg); | |
1790 | ||
1791 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MUNMAP, 1, 0); | |
1792 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, | |
1793 | &args[0]); | |
1794 | if (!err) { | |
1795 | dev_dbg(dev, "unmmap\tpt 0x%09lx OK\n", buf->raddr); | |
1796 | spin_lock(&fl->lock); | |
1797 | list_del(&buf->node); | |
1798 | spin_unlock(&fl->lock); | |
1799 | fastrpc_buf_free(buf); | |
1800 | } else { | |
1801 | dev_err(dev, "unmmap\tpt 0x%09lx ERROR\n", buf->raddr); | |
1802 | } | |
1803 | ||
1804 | return err; | |
1805 | } | |
1806 | ||
1807 | static int fastrpc_req_munmap(struct fastrpc_user *fl, char __user *argp) | |
1808 | { | |
72fa6f78 | 1809 | struct fastrpc_buf *buf = NULL, *iter, *b; |
2419e55e | 1810 | struct fastrpc_req_munmap req; |
72fa6f78 | 1811 | struct device *dev = fl->sctx->dev; |
2419e55e JRO |
1812 | |
1813 | if (copy_from_user(&req, argp, sizeof(req))) | |
1814 | return -EFAULT; | |
1815 | ||
72fa6f78 AV |
1816 | spin_lock(&fl->lock); |
1817 | list_for_each_entry_safe(iter, b, &fl->mmaps, node) { | |
1818 | if ((iter->raddr == req.vaddrout) && (iter->size == req.size)) { | |
1819 | buf = iter; | |
1820 | break; | |
1821 | } | |
1822 | } | |
1823 | spin_unlock(&fl->lock); | |
1824 | ||
1825 | if (!buf) { | |
1826 | dev_err(dev, "mmap\t\tpt 0x%09llx [len 0x%08llx] not in list\n", | |
1827 | req.vaddrout, req.size); | |
1828 | return -EINVAL; | |
1829 | } | |
1830 | ||
1831 | return fastrpc_req_munmap_impl(fl, buf); | |
2419e55e JRO |
1832 | } |
1833 | ||
1834 | static int fastrpc_req_mmap(struct fastrpc_user *fl, char __user *argp) | |
1835 | { | |
1836 | struct fastrpc_invoke_args args[3] = { [0 ... 2] = { 0 } }; | |
1837 | struct fastrpc_buf *buf = NULL; | |
1838 | struct fastrpc_mmap_req_msg req_msg; | |
1839 | struct fastrpc_mmap_rsp_msg rsp_msg; | |
2419e55e JRO |
1840 | struct fastrpc_phy_page pages; |
1841 | struct fastrpc_req_mmap req; | |
1842 | struct device *dev = fl->sctx->dev; | |
1843 | int err; | |
1844 | u32 sc; | |
1845 | ||
1846 | if (copy_from_user(&req, argp, sizeof(req))) | |
1847 | return -EFAULT; | |
1848 | ||
532ad70c | 1849 | if (req.flags != ADSP_MMAP_ADD_PAGES && req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR) { |
2419e55e | 1850 | dev_err(dev, "flag not supported 0x%x\n", req.flags); |
532ad70c | 1851 | |
2419e55e JRO |
1852 | return -EINVAL; |
1853 | } | |
1854 | ||
1855 | if (req.vaddrin) { | |
1856 | dev_err(dev, "adding user allocated pages is not supported\n"); | |
1857 | return -EINVAL; | |
1858 | } | |
1859 | ||
1860 | err = fastrpc_buf_alloc(fl, fl->sctx->dev, req.size, &buf); | |
1861 | if (err) { | |
1862 | dev_err(dev, "failed to allocate buffer\n"); | |
1863 | return err; | |
1864 | } | |
1865 | ||
1866 | req_msg.pgid = fl->tgid; | |
1867 | req_msg.flags = req.flags; | |
1868 | req_msg.vaddr = req.vaddrin; | |
1869 | req_msg.num = sizeof(pages); | |
1870 | ||
1871 | args[0].ptr = (u64) (uintptr_t) &req_msg; | |
1872 | args[0].length = sizeof(req_msg); | |
1873 | ||
1874 | pages.addr = buf->phys; | |
1875 | pages.size = buf->size; | |
1876 | ||
1877 | args[1].ptr = (u64) (uintptr_t) &pages; | |
1878 | args[1].length = sizeof(pages); | |
1879 | ||
1880 | args[2].ptr = (u64) (uintptr_t) &rsp_msg; | |
1881 | args[2].length = sizeof(rsp_msg); | |
1882 | ||
1883 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MMAP, 2, 1); | |
1884 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, | |
1885 | &args[0]); | |
1886 | if (err) { | |
1887 | dev_err(dev, "mmap error (len 0x%08llx)\n", buf->size); | |
1888 | goto err_invoke; | |
1889 | } | |
1890 | ||
1891 | /* update the buffer to be able to deallocate the memory on the DSP */ | |
1892 | buf->raddr = (uintptr_t) rsp_msg.vaddr; | |
1893 | ||
1894 | /* let the client know the address to use */ | |
1895 | req.vaddrout = rsp_msg.vaddr; | |
1896 | ||
532ad70c AV |
1897 | /* Add memory to static PD pool, protection thru hypervisor */ |
1898 | if (req.flags != ADSP_MMAP_REMOTE_HEAP_ADDR && fl->cctx->vmcount) { | |
1899 | struct qcom_scm_vmperm perm; | |
532ad70c AV |
1900 | |
1901 | perm.vmid = QCOM_SCM_VMID_HLOS; | |
1902 | perm.perm = QCOM_SCM_PERM_RWX; | |
1903 | err = qcom_scm_assign_mem(buf->phys, buf->size, | |
1904 | &(fl->cctx->vmperms[0].vmid), &perm, 1); | |
1905 | if (err) { | |
1906 | dev_err(fl->sctx->dev, "Failed to assign memory phys 0x%llx size 0x%llx err %d", | |
1907 | buf->phys, buf->size, err); | |
1908 | goto err_assign; | |
1909 | } | |
1910 | } | |
1911 | ||
2419e55e JRO |
1912 | spin_lock(&fl->lock); |
1913 | list_add_tail(&buf->node, &fl->mmaps); | |
1914 | spin_unlock(&fl->lock); | |
1915 | ||
1916 | if (copy_to_user((void __user *)argp, &req, sizeof(req))) { | |
72fa6f78 AV |
1917 | err = -EFAULT; |
1918 | goto err_assign; | |
2419e55e JRO |
1919 | } |
1920 | ||
1921 | dev_dbg(dev, "mmap\t\tpt 0x%09lx OK [len 0x%08llx]\n", | |
1922 | buf->raddr, buf->size); | |
1923 | ||
1924 | return 0; | |
1925 | ||
72fa6f78 AV |
1926 | err_assign: |
1927 | fastrpc_req_munmap_impl(fl, buf); | |
2419e55e JRO |
1928 | err_invoke: |
1929 | fastrpc_buf_free(buf); | |
1930 | ||
1931 | return err; | |
1932 | } | |
1933 | ||
5c1b97c7 J |
1934 | static int fastrpc_req_mem_unmap_impl(struct fastrpc_user *fl, struct fastrpc_mem_unmap *req) |
1935 | { | |
1936 | struct fastrpc_invoke_args args[1] = { [0] = { 0 } }; | |
c5c07c59 | 1937 | struct fastrpc_map *map = NULL, *iter, *m; |
5c1b97c7 J |
1938 | struct fastrpc_mem_unmap_req_msg req_msg = { 0 }; |
1939 | int err = 0; | |
1940 | u32 sc; | |
1941 | struct device *dev = fl->sctx->dev; | |
1942 | ||
1943 | spin_lock(&fl->lock); | |
c5c07c59 SK |
1944 | list_for_each_entry_safe(iter, m, &fl->maps, node) { |
1945 | if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) { | |
1946 | map = iter; | |
5c1b97c7 | 1947 | break; |
c5c07c59 | 1948 | } |
5c1b97c7 J |
1949 | } |
1950 | ||
1951 | spin_unlock(&fl->lock); | |
1952 | ||
1953 | if (!map) { | |
1954 | dev_err(dev, "map not in list\n"); | |
1955 | return -EINVAL; | |
1956 | } | |
1957 | ||
1958 | req_msg.pgid = fl->tgid; | |
1959 | req_msg.len = map->len; | |
1960 | req_msg.vaddrin = map->raddr; | |
1961 | req_msg.fd = map->fd; | |
1962 | ||
1963 | args[0].ptr = (u64) (uintptr_t) &req_msg; | |
1964 | args[0].length = sizeof(req_msg); | |
1965 | ||
1966 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_UNMAP, 1, 0); | |
1967 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, | |
1968 | &args[0]); | |
1969 | fastrpc_map_put(map); | |
1970 | if (err) | |
1971 | dev_err(dev, "unmmap\tpt fd = %d, 0x%09llx error\n", map->fd, map->raddr); | |
1972 | ||
1973 | return err; | |
1974 | } | |
1975 | ||
1976 | static int fastrpc_req_mem_unmap(struct fastrpc_user *fl, char __user *argp) | |
1977 | { | |
1978 | struct fastrpc_mem_unmap req; | |
1979 | ||
1980 | if (copy_from_user(&req, argp, sizeof(req))) | |
1981 | return -EFAULT; | |
1982 | ||
1983 | return fastrpc_req_mem_unmap_impl(fl, &req); | |
1984 | } | |
1985 | ||
1986 | static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp) | |
1987 | { | |
1988 | struct fastrpc_invoke_args args[4] = { [0 ... 3] = { 0 } }; | |
1989 | struct fastrpc_mem_map_req_msg req_msg = { 0 }; | |
1990 | struct fastrpc_mmap_rsp_msg rsp_msg = { 0 }; | |
1991 | struct fastrpc_mem_unmap req_unmap = { 0 }; | |
1992 | struct fastrpc_phy_page pages = { 0 }; | |
1993 | struct fastrpc_mem_map req; | |
1994 | struct device *dev = fl->sctx->dev; | |
1995 | struct fastrpc_map *map = NULL; | |
1996 | int err; | |
1997 | u32 sc; | |
1998 | ||
1999 | if (copy_from_user(&req, argp, sizeof(req))) | |
2000 | return -EFAULT; | |
2001 | ||
2002 | /* create SMMU mapping */ | |
3abe3ab3 | 2003 | err = fastrpc_map_create(fl, req.fd, req.length, 0, &map); |
5c1b97c7 J |
2004 | if (err) { |
2005 | dev_err(dev, "failed to map buffer, fd = %d\n", req.fd); | |
2006 | return err; | |
2007 | } | |
2008 | ||
2009 | req_msg.pgid = fl->tgid; | |
2010 | req_msg.fd = req.fd; | |
2011 | req_msg.offset = req.offset; | |
2012 | req_msg.vaddrin = req.vaddrin; | |
2013 | map->va = (void *) (uintptr_t) req.vaddrin; | |
2014 | req_msg.flags = req.flags; | |
2015 | req_msg.num = sizeof(pages); | |
2016 | req_msg.data_len = 0; | |
2017 | ||
2018 | args[0].ptr = (u64) (uintptr_t) &req_msg; | |
2019 | args[0].length = sizeof(req_msg); | |
2020 | ||
2021 | pages.addr = map->phys; | |
2022 | pages.size = map->size; | |
2023 | ||
2024 | args[1].ptr = (u64) (uintptr_t) &pages; | |
2025 | args[1].length = sizeof(pages); | |
2026 | ||
2027 | args[2].ptr = (u64) (uintptr_t) &pages; | |
2028 | args[2].length = 0; | |
2029 | ||
2030 | args[3].ptr = (u64) (uintptr_t) &rsp_msg; | |
2031 | args[3].length = sizeof(rsp_msg); | |
2032 | ||
2033 | sc = FASTRPC_SCALARS(FASTRPC_RMID_INIT_MEM_MAP, 3, 1); | |
2034 | err = fastrpc_internal_invoke(fl, true, FASTRPC_INIT_HANDLE, sc, &args[0]); | |
2035 | if (err) { | |
2036 | dev_err(dev, "mem mmap error, fd %d, vaddr %llx, size %lld\n", | |
2037 | req.fd, req.vaddrin, map->size); | |
2038 | goto err_invoke; | |
2039 | } | |
2040 | ||
2041 | /* update the buffer to be able to deallocate the memory on the DSP */ | |
2042 | map->raddr = rsp_msg.vaddr; | |
2043 | ||
2044 | /* let the client know the address to use */ | |
2045 | req.vaddrout = rsp_msg.vaddr; | |
2046 | ||
2047 | if (copy_to_user((void __user *)argp, &req, sizeof(req))) { | |
2048 | /* unmap the memory and release the buffer */ | |
2049 | req_unmap.vaddr = (uintptr_t) rsp_msg.vaddr; | |
2050 | req_unmap.length = map->size; | |
2051 | fastrpc_req_mem_unmap_impl(fl, &req_unmap); | |
2052 | return -EFAULT; | |
2053 | } | |
2054 | ||
2055 | return 0; | |
2056 | ||
2057 | err_invoke: | |
2058 | fastrpc_map_put(map); | |
2059 | ||
2060 | return err; | |
2061 | } | |
2062 | ||
c68cfb71 SK |
2063 | static long fastrpc_device_ioctl(struct file *file, unsigned int cmd, |
2064 | unsigned long arg) | |
2065 | { | |
2066 | struct fastrpc_user *fl = (struct fastrpc_user *)file->private_data; | |
2067 | char __user *argp = (char __user *)arg; | |
2068 | int err; | |
2069 | ||
2070 | switch (cmd) { | |
2071 | case FASTRPC_IOCTL_INVOKE: | |
2072 | err = fastrpc_invoke(fl, argp); | |
2073 | break; | |
d73f71c7 | 2074 | case FASTRPC_IOCTL_INIT_ATTACH: |
1959ab9e | 2075 | err = fastrpc_init_attach(fl, ROOT_PD); |
6010d9be JM |
2076 | break; |
2077 | case FASTRPC_IOCTL_INIT_ATTACH_SNS: | |
2078 | err = fastrpc_init_attach(fl, SENSORS_PD); | |
d73f71c7 | 2079 | break; |
08715610 AV |
2080 | case FASTRPC_IOCTL_INIT_CREATE_STATIC: |
2081 | err = fastrpc_init_create_static_process(fl, argp); | |
2082 | break; | |
d73f71c7 SK |
2083 | case FASTRPC_IOCTL_INIT_CREATE: |
2084 | err = fastrpc_init_create_process(fl, argp); | |
2085 | break; | |
6cffd795 SK |
2086 | case FASTRPC_IOCTL_ALLOC_DMA_BUFF: |
2087 | err = fastrpc_dmabuf_alloc(fl, argp); | |
2088 | break; | |
2419e55e JRO |
2089 | case FASTRPC_IOCTL_MMAP: |
2090 | err = fastrpc_req_mmap(fl, argp); | |
2091 | break; | |
2092 | case FASTRPC_IOCTL_MUNMAP: | |
2093 | err = fastrpc_req_munmap(fl, argp); | |
2094 | break; | |
5c1b97c7 J |
2095 | case FASTRPC_IOCTL_MEM_MAP: |
2096 | err = fastrpc_req_mem_map(fl, argp); | |
2097 | break; | |
2098 | case FASTRPC_IOCTL_MEM_UNMAP: | |
2099 | err = fastrpc_req_mem_unmap(fl, argp); | |
2100 | break; | |
6c16fd8b J |
2101 | case FASTRPC_IOCTL_GET_DSP_INFO: |
2102 | err = fastrpc_get_dsp_info(fl, argp); | |
2103 | break; | |
c68cfb71 SK |
2104 | default: |
2105 | err = -ENOTTY; | |
2106 | break; | |
2107 | } | |
2108 | ||
2109 | return err; | |
2110 | } | |
2111 | ||
f6f9279f SK |
2112 | static const struct file_operations fastrpc_fops = { |
2113 | .open = fastrpc_device_open, | |
2114 | .release = fastrpc_device_release, | |
c68cfb71 SK |
2115 | .unlocked_ioctl = fastrpc_device_ioctl, |
2116 | .compat_ioctl = fastrpc_device_ioctl, | |
f6f9279f SK |
2117 | }; |
2118 | ||
2119 | static int fastrpc_cb_probe(struct platform_device *pdev) | |
2120 | { | |
2121 | struct fastrpc_channel_ctx *cctx; | |
2122 | struct fastrpc_session_ctx *sess; | |
2123 | struct device *dev = &pdev->dev; | |
2124 | int i, sessions = 0; | |
977e6c8d | 2125 | unsigned long flags; |
01b76c32 | 2126 | int rc; |
f6f9279f SK |
2127 | |
2128 | cctx = dev_get_drvdata(dev->parent); | |
2129 | if (!cctx) | |
2130 | return -EINVAL; | |
2131 | ||
2132 | of_property_read_u32(dev->of_node, "qcom,nsessions", &sessions); | |
2133 | ||
977e6c8d | 2134 | spin_lock_irqsave(&cctx->lock, flags); |
9baa1415 JH |
2135 | if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) { |
2136 | dev_err(&pdev->dev, "too many sessions\n"); | |
2137 | spin_unlock_irqrestore(&cctx->lock, flags); | |
2138 | return -ENOSPC; | |
2139 | } | |
d245f43a | 2140 | sess = &cctx->session[cctx->sesscount++]; |
f6f9279f SK |
2141 | sess->used = false; |
2142 | sess->valid = true; | |
2143 | sess->dev = dev; | |
2144 | dev_set_drvdata(dev, sess); | |
2145 | ||
2146 | if (of_property_read_u32(dev->of_node, "reg", &sess->sid)) | |
2147 | dev_info(dev, "FastRPC Session ID not specified in DT\n"); | |
2148 | ||
2149 | if (sessions > 0) { | |
2150 | struct fastrpc_session_ctx *dup_sess; | |
2151 | ||
2152 | for (i = 1; i < sessions; i++) { | |
d245f43a | 2153 | if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) |
f6f9279f | 2154 | break; |
d245f43a | 2155 | dup_sess = &cctx->session[cctx->sesscount++]; |
f6f9279f SK |
2156 | memcpy(dup_sess, sess, sizeof(*dup_sess)); |
2157 | } | |
2158 | } | |
977e6c8d | 2159 | spin_unlock_irqrestore(&cctx->lock, flags); |
01b76c32 BY |
2160 | rc = dma_set_mask(dev, DMA_BIT_MASK(32)); |
2161 | if (rc) { | |
2162 | dev_err(dev, "32-bit DMA enable failed\n"); | |
2163 | return rc; | |
2164 | } | |
f6f9279f SK |
2165 | |
2166 | return 0; | |
2167 | } | |
2168 | ||
2169 | static int fastrpc_cb_remove(struct platform_device *pdev) | |
2170 | { | |
2171 | struct fastrpc_channel_ctx *cctx = dev_get_drvdata(pdev->dev.parent); | |
2172 | struct fastrpc_session_ctx *sess = dev_get_drvdata(&pdev->dev); | |
977e6c8d | 2173 | unsigned long flags; |
f6f9279f SK |
2174 | int i; |
2175 | ||
977e6c8d | 2176 | spin_lock_irqsave(&cctx->lock, flags); |
f6f9279f SK |
2177 | for (i = 1; i < FASTRPC_MAX_SESSIONS; i++) { |
2178 | if (cctx->session[i].sid == sess->sid) { | |
2179 | cctx->session[i].valid = false; | |
2180 | cctx->sesscount--; | |
2181 | } | |
2182 | } | |
977e6c8d | 2183 | spin_unlock_irqrestore(&cctx->lock, flags); |
f6f9279f SK |
2184 | |
2185 | return 0; | |
2186 | } | |
2187 | ||
2188 | static const struct of_device_id fastrpc_match_table[] = { | |
2189 | { .compatible = "qcom,fastrpc-compute-cb", }, | |
2190 | {} | |
2191 | }; | |
2192 | ||
2193 | static struct platform_driver fastrpc_cb_driver = { | |
2194 | .probe = fastrpc_cb_probe, | |
2195 | .remove = fastrpc_cb_remove, | |
2196 | .driver = { | |
2197 | .name = "qcom,fastrpc-cb", | |
2198 | .of_match_table = fastrpc_match_table, | |
2199 | .suppress_bind_attrs = true, | |
2200 | }, | |
2201 | }; | |
2202 | ||
965602ea | 2203 | static int fastrpc_device_register(struct device *dev, struct fastrpc_channel_ctx *cctx, |
3abe3ab3 | 2204 | bool is_secured, const char *domain) |
965602ea SK |
2205 | { |
2206 | struct fastrpc_device *fdev; | |
2207 | int err; | |
2208 | ||
2209 | fdev = devm_kzalloc(dev, sizeof(*fdev), GFP_KERNEL); | |
2210 | if (!fdev) | |
2211 | return -ENOMEM; | |
2212 | ||
3abe3ab3 | 2213 | fdev->secure = is_secured; |
965602ea SK |
2214 | fdev->cctx = cctx; |
2215 | fdev->miscdev.minor = MISC_DYNAMIC_MINOR; | |
2216 | fdev->miscdev.fops = &fastrpc_fops; | |
3abe3ab3 SK |
2217 | fdev->miscdev.name = devm_kasprintf(dev, GFP_KERNEL, "fastrpc-%s%s", |
2218 | domain, is_secured ? "-secure" : ""); | |
965602ea | 2219 | err = misc_register(&fdev->miscdev); |
3abe3ab3 SK |
2220 | if (!err) { |
2221 | if (is_secured) | |
2222 | cctx->secure_fdevice = fdev; | |
2223 | else | |
2224 | cctx->fdevice = fdev; | |
2225 | } | |
965602ea SK |
2226 | |
2227 | return err; | |
2228 | } | |
2229 | ||
f6f9279f SK |
2230 | static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) |
2231 | { | |
2232 | struct device *rdev = &rpdev->dev; | |
2233 | struct fastrpc_channel_ctx *data; | |
e90d9119 | 2234 | int i, err, domain_id = -1, vmcount; |
f6f9279f | 2235 | const char *domain; |
3abe3ab3 | 2236 | bool secure_dsp; |
e90d9119 | 2237 | unsigned int vmids[FASTRPC_MAX_VMIDS]; |
f6f9279f | 2238 | |
f6f9279f SK |
2239 | err = of_property_read_string(rdev->of_node, "label", &domain); |
2240 | if (err) { | |
2241 | dev_info(rdev, "FastRPC Domain not specified in DT\n"); | |
2242 | return err; | |
2243 | } | |
2244 | ||
2245 | for (i = 0; i <= CDSP_DOMAIN_ID; i++) { | |
2246 | if (!strcmp(domains[i], domain)) { | |
2247 | domain_id = i; | |
2248 | break; | |
2249 | } | |
2250 | } | |
2251 | ||
2252 | if (domain_id < 0) { | |
2253 | dev_info(rdev, "FastRPC Invalid Domain ID %d\n", domain_id); | |
2254 | return -EINVAL; | |
2255 | } | |
2256 | ||
1ce91d45 AV |
2257 | if (of_reserved_mem_device_init_by_idx(rdev, rdev->of_node, 0)) |
2258 | dev_info(rdev, "no reserved DMA memory for FASTRPC\n"); | |
2259 | ||
e90d9119 VKG |
2260 | vmcount = of_property_read_variable_u32_array(rdev->of_node, |
2261 | "qcom,vmids", &vmids[0], 0, FASTRPC_MAX_VMIDS); | |
2262 | if (vmcount < 0) | |
2263 | vmcount = 0; | |
2264 | else if (!qcom_scm_is_available()) | |
2265 | return -EPROBE_DEFER; | |
2266 | ||
278d56f9 BA |
2267 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
2268 | if (!data) | |
2269 | return -ENOMEM; | |
2270 | ||
e90d9119 VKG |
2271 | if (vmcount) { |
2272 | data->vmcount = vmcount; | |
2273 | data->perms = BIT(QCOM_SCM_VMID_HLOS); | |
2274 | for (i = 0; i < data->vmcount; i++) { | |
2275 | data->vmperms[i].vmid = vmids[i]; | |
2276 | data->vmperms[i].perm = QCOM_SCM_PERM_RWX; | |
2277 | } | |
2278 | } | |
3abe3ab3 SK |
2279 | |
2280 | secure_dsp = !(of_property_read_bool(rdev->of_node, "qcom,non-secure-domain")); | |
2281 | data->secure = secure_dsp; | |
2282 | ||
2283 | switch (domain_id) { | |
2284 | case ADSP_DOMAIN_ID: | |
2285 | case MDSP_DOMAIN_ID: | |
2286 | case SDSP_DOMAIN_ID: | |
7f1f4812 J |
2287 | /* Unsigned PD offloading is only supported on CDSP*/ |
2288 | data->unsigned_support = false; | |
3abe3ab3 SK |
2289 | err = fastrpc_device_register(rdev, data, secure_dsp, domains[domain_id]); |
2290 | if (err) | |
2291 | goto fdev_error; | |
2292 | break; | |
2293 | case CDSP_DOMAIN_ID: | |
7f1f4812 | 2294 | data->unsigned_support = true; |
3abe3ab3 SK |
2295 | /* Create both device nodes so that we can allow both Signed and Unsigned PD */ |
2296 | err = fastrpc_device_register(rdev, data, true, domains[domain_id]); | |
2297 | if (err) | |
2298 | goto fdev_error; | |
2299 | ||
2300 | err = fastrpc_device_register(rdev, data, false, domains[domain_id]); | |
2301 | if (err) | |
2302 | goto fdev_error; | |
2303 | break; | |
2304 | default: | |
2305 | err = -EINVAL; | |
2306 | goto fdev_error; | |
0978de9f | 2307 | } |
f6f9279f | 2308 | |
278d56f9 BA |
2309 | kref_init(&data->refcount); |
2310 | ||
f6f9279f | 2311 | dev_set_drvdata(&rpdev->dev, data); |
9bde43a0 | 2312 | rdev->dma_mask = &data->dma_mask; |
f6f9279f SK |
2313 | dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32)); |
2314 | INIT_LIST_HEAD(&data->users); | |
76e8e4ac | 2315 | INIT_LIST_HEAD(&data->invoke_interrupted_mmaps); |
f6f9279f SK |
2316 | spin_lock_init(&data->lock); |
2317 | idr_init(&data->ctx_idr); | |
2318 | data->domain_id = domain_id; | |
2319 | data->rpdev = rpdev; | |
2320 | ||
2321 | return of_platform_populate(rdev->of_node, NULL, NULL, rdev); | |
3abe3ab3 SK |
2322 | fdev_error: |
2323 | kfree(data); | |
2324 | return err; | |
f6f9279f SK |
2325 | } |
2326 | ||
c68cfb71 SK |
2327 | static void fastrpc_notify_users(struct fastrpc_user *user) |
2328 | { | |
2329 | struct fastrpc_invoke_ctx *ctx; | |
2330 | ||
2331 | spin_lock(&user->lock); | |
2332 | list_for_each_entry(ctx, &user->pending, node) | |
2333 | complete(&ctx->work); | |
2334 | spin_unlock(&user->lock); | |
2335 | } | |
2336 | ||
f6f9279f SK |
2337 | static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) |
2338 | { | |
2339 | struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); | |
76e8e4ac | 2340 | struct fastrpc_buf *buf, *b; |
c68cfb71 | 2341 | struct fastrpc_user *user; |
977e6c8d | 2342 | unsigned long flags; |
c68cfb71 | 2343 | |
977e6c8d | 2344 | spin_lock_irqsave(&cctx->lock, flags); |
c68cfb71 SK |
2345 | list_for_each_entry(user, &cctx->users, user) |
2346 | fastrpc_notify_users(user); | |
977e6c8d | 2347 | spin_unlock_irqrestore(&cctx->lock, flags); |
f6f9279f | 2348 | |
965602ea SK |
2349 | if (cctx->fdevice) |
2350 | misc_deregister(&cctx->fdevice->miscdev); | |
2351 | ||
3abe3ab3 SK |
2352 | if (cctx->secure_fdevice) |
2353 | misc_deregister(&cctx->secure_fdevice->miscdev); | |
2354 | ||
76e8e4ac AV |
2355 | list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node) |
2356 | list_del(&buf->node); | |
2357 | ||
08715610 AV |
2358 | if (cctx->remote_heap) |
2359 | fastrpc_buf_free(cctx->remote_heap); | |
2360 | ||
f6f9279f | 2361 | of_platform_depopulate(&rpdev->dev); |
278d56f9 | 2362 | |
2e369878 | 2363 | cctx->rpdev = NULL; |
278d56f9 | 2364 | fastrpc_channel_ctx_put(cctx); |
f6f9279f SK |
2365 | } |
2366 | ||
2367 | static int fastrpc_rpmsg_callback(struct rpmsg_device *rpdev, void *data, | |
2368 | int len, void *priv, u32 addr) | |
2369 | { | |
c68cfb71 SK |
2370 | struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev); |
2371 | struct fastrpc_invoke_rsp *rsp = data; | |
2372 | struct fastrpc_invoke_ctx *ctx; | |
2373 | unsigned long flags; | |
2374 | unsigned long ctxid; | |
2375 | ||
2376 | if (len < sizeof(*rsp)) | |
2377 | return -EINVAL; | |
2378 | ||
2379 | ctxid = ((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); | |
2380 | ||
2381 | spin_lock_irqsave(&cctx->lock, flags); | |
2382 | ctx = idr_find(&cctx->ctx_idr, ctxid); | |
2383 | spin_unlock_irqrestore(&cctx->lock, flags); | |
2384 | ||
2385 | if (!ctx) { | |
2386 | dev_err(&rpdev->dev, "No context ID matches response\n"); | |
2387 | return -ENOENT; | |
2388 | } | |
2389 | ||
2390 | ctx->retval = rsp->retval; | |
2391 | complete(&ctx->work); | |
8e7389c7 TE |
2392 | |
2393 | /* | |
2394 | * The DMA buffer associated with the context cannot be freed in | |
2395 | * interrupt context so schedule it through a worker thread to | |
2396 | * avoid a kernel BUG. | |
2397 | */ | |
2398 | schedule_work(&ctx->put_work); | |
c68cfb71 | 2399 | |
f6f9279f SK |
2400 | return 0; |
2401 | } | |
2402 | ||
2403 | static const struct of_device_id fastrpc_rpmsg_of_match[] = { | |
2404 | { .compatible = "qcom,fastrpc" }, | |
2405 | { }, | |
2406 | }; | |
2407 | MODULE_DEVICE_TABLE(of, fastrpc_rpmsg_of_match); | |
2408 | ||
2409 | static struct rpmsg_driver fastrpc_driver = { | |
2410 | .probe = fastrpc_rpmsg_probe, | |
2411 | .remove = fastrpc_rpmsg_remove, | |
2412 | .callback = fastrpc_rpmsg_callback, | |
2413 | .drv = { | |
2414 | .name = "qcom,fastrpc", | |
2415 | .of_match_table = fastrpc_rpmsg_of_match, | |
2416 | }, | |
2417 | }; | |
2418 | ||
2419 | static int fastrpc_init(void) | |
2420 | { | |
2421 | int ret; | |
2422 | ||
2423 | ret = platform_driver_register(&fastrpc_cb_driver); | |
2424 | if (ret < 0) { | |
2425 | pr_err("fastrpc: failed to register cb driver\n"); | |
2426 | return ret; | |
2427 | } | |
2428 | ||
2429 | ret = register_rpmsg_driver(&fastrpc_driver); | |
2430 | if (ret < 0) { | |
2431 | pr_err("fastrpc: failed to register rpmsg driver\n"); | |
2432 | platform_driver_unregister(&fastrpc_cb_driver); | |
2433 | return ret; | |
2434 | } | |
2435 | ||
2436 | return 0; | |
2437 | } | |
2438 | module_init(fastrpc_init); | |
2439 | ||
2440 | static void fastrpc_exit(void) | |
2441 | { | |
2442 | platform_driver_unregister(&fastrpc_cb_driver); | |
2443 | unregister_rpmsg_driver(&fastrpc_driver); | |
2444 | } | |
2445 | module_exit(fastrpc_exit); | |
2446 | ||
2447 | MODULE_LICENSE("GPL v2"); | |
16b0314a | 2448 | MODULE_IMPORT_NS(DMA_BUF); |