Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1 | /* |
Jeya R | b70b4ad | 2021-01-25 10:28:42 -0800 | [diff] [blame] | 2 | * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved. |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/dma-buf.h> |
| 15 | #include <linux/dma-mapping.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/completion.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/cdev.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/hash.h> |
| 26 | #include <linux/msm_ion.h> |
| 27 | #include <soc/qcom/secure_buffer.h> |
| 28 | #include <soc/qcom/glink.h> |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 29 | #include <soc/qcom/smd.h> |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 30 | #include <soc/qcom/subsystem_notif.h> |
| 31 | #include <soc/qcom/subsystem_restart.h> |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 32 | #include <soc/qcom/service-notifier.h> |
| 33 | #include <soc/qcom/service-locator.h> |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 34 | #include <linux/scatterlist.h> |
| 35 | #include <linux/fs.h> |
| 36 | #include <linux/uaccess.h> |
| 37 | #include <linux/device.h> |
| 38 | #include <linux/of.h> |
| 39 | #include <linux/of_address.h> |
| 40 | #include <linux/of_platform.h> |
| 41 | #include <linux/dma-contiguous.h> |
| 42 | #include <linux/cma.h> |
| 43 | #include <linux/iommu.h> |
| 44 | #include <linux/kref.h> |
| 45 | #include <linux/sort.h> |
| 46 | #include <linux/msm_dma_iommu_mapping.h> |
| 47 | #include <asm/dma-iommu.h> |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 48 | #include <soc/qcom/scm.h> |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 49 | #include "adsprpc_compat.h" |
| 50 | #include "adsprpc_shared.h" |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 51 | #include <soc/qcom/ramdump.h> |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 52 | #include <linux/debugfs.h> |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 53 | #include <linux/pm_qos.h> |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 54 | #define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C |
| 55 | #define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D |
| 56 | #define TZ_PIL_AUTH_QDSP6_PROC 1 |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 57 | #define ADSP_MMAP_HEAP_ADDR 4 |
| 58 | #define ADSP_MMAP_REMOTE_HEAP_ADDR 8 |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 59 | #define ADSP_MMAP_ADD_PAGES 0x1000 |
Tharun Kumar Merugu | 35a94a5 | 2018-02-01 21:09:04 +0530 | [diff] [blame] | 60 | #define FASTRPC_DMAHANDLE_NOMAP (16) |
| 61 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 62 | #define FASTRPC_ENOSUCH 39 |
| 63 | #define VMID_SSC_Q6 5 |
| 64 | #define VMID_ADSP_Q6 6 |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 65 | #define DEBUGFS_SIZE 3072 |
| 66 | #define UL_SIZE 25 |
| 67 | #define PID_SIZE 10 |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 68 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 69 | #define AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME "audio_pdr_adsprpc" |
| 70 | #define AUDIO_PDR_ADSP_SERVICE_NAME "avs/audio" |
| 71 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 72 | #define SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_adsprpc" |
| 73 | #define SENSORS_PDR_ADSP_SERVICE_NAME "tms/servreg" |
| 74 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 75 | #define RPC_TIMEOUT (5 * HZ) |
| 76 | #define BALIGN 128 |
| 77 | #define NUM_CHANNELS 4 /* adsp, mdsp, slpi, cdsp*/ |
| 78 | #define NUM_SESSIONS 9 /*8 compute, 1 cpz*/ |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 79 | #define M_FDLIST (16) |
| 80 | #define M_CRCLIST (64) |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 81 | #define SESSION_ID_INDEX (30) |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 82 | #define FASTRPC_CTX_MAGIC (0xbeeddeed) |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 83 | #define FASTRPC_CTX_MAX (256) |
| 84 | #define FASTRPC_CTXID_MASK (0xFF0) |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 85 | #define NUM_DEVICES 2 /* adsprpc-smd, adsprpc-smd-secure */ |
| 86 | #define MINOR_NUM_DEV 0 |
| 87 | #define MINOR_NUM_SECURE_DEV 1 |
| 88 | #define NON_SECURE_CHANNEL 0 |
| 89 | #define SECURE_CHANNEL 1 |
| 90 | |
| 91 | #define ADSP_DOMAIN_ID (0) |
| 92 | #define MDSP_DOMAIN_ID (1) |
| 93 | #define SDSP_DOMAIN_ID (2) |
| 94 | #define CDSP_DOMAIN_ID (3) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 95 | |
| 96 | #define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0) |
| 97 | |
| 98 | #define FASTRPC_LINK_STATE_DOWN (0x0) |
| 99 | #define FASTRPC_LINK_STATE_UP (0x1) |
| 100 | #define FASTRPC_LINK_DISCONNECTED (0x0) |
| 101 | #define FASTRPC_LINK_CONNECTING (0x1) |
| 102 | #define FASTRPC_LINK_CONNECTED (0x3) |
| 103 | #define FASTRPC_LINK_DISCONNECTING (0x7) |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 104 | #define FASTRPC_LINK_REMOTE_DISCONNECTING (0x8) |
| 105 | #define FASTRPC_GLINK_INTENT_LEN (64) |
Tharun Kumar Merugu | c42c6e2 | 2018-05-29 15:50:46 +0530 | [diff] [blame] | 106 | #define FASTRPC_GLINK_INTENT_NUM (16) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 107 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 108 | #define PERF_KEYS \ |
| 109 | "count:flush:map:copy:glink:getargs:putargs:invalidate:invoke:tid:ptr" |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 110 | #define FASTRPC_STATIC_HANDLE_KERNEL (1) |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 111 | #define FASTRPC_STATIC_HANDLE_LISTENER (3) |
| 112 | #define FASTRPC_STATIC_HANDLE_MAX (20) |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 113 | #define FASTRPC_LATENCY_CTRL_ENB (1) |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 114 | |
Mohammed Nayeem Ur Rahman | 62f7f9c | 2020-04-13 11:16:19 +0530 | [diff] [blame] | 115 | #define MAX_SIZE_LIMIT (0x78000000) |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 116 | #define INIT_FILELEN_MAX (2*1024*1024) |
| 117 | #define INIT_MEMLEN_MAX (8*1024*1024) |
| 118 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 119 | #define PERF_END (void)0 |
| 120 | |
| 121 | #define PERF(enb, cnt, ff) \ |
| 122 | {\ |
| 123 | struct timespec startT = {0};\ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 124 | int64_t *counter = cnt;\ |
| 125 | if (enb && counter) {\ |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 126 | getnstimeofday(&startT);\ |
| 127 | } \ |
| 128 | ff ;\ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 129 | if (enb && counter) {\ |
| 130 | *counter += getnstimediff(&startT);\ |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 131 | } \ |
| 132 | } |
| 133 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 134 | #define GET_COUNTER(perf_ptr, offset) \ |
| 135 | (perf_ptr != NULL ?\ |
| 136 | (((offset >= 0) && (offset < PERF_KEY_MAX)) ?\ |
| 137 | (int64_t *)(perf_ptr + offset)\ |
| 138 | : (int64_t *)NULL) : (int64_t *)NULL) |
| 139 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 140 | static int fastrpc_glink_open(int cid); |
| 141 | static void fastrpc_glink_close(void *chan, int cid); |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 142 | static int fastrpc_pdr_notifier_cb(struct notifier_block *nb, |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 143 | unsigned long code, |
| 144 | void *data); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 145 | static struct dentry *debugfs_root; |
| 146 | static struct dentry *debugfs_global_file; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 147 | |
| 148 | static inline uint64_t buf_page_start(uint64_t buf) |
| 149 | { |
| 150 | uint64_t start = (uint64_t) buf & PAGE_MASK; |
| 151 | return start; |
| 152 | } |
| 153 | |
| 154 | static inline uint64_t buf_page_offset(uint64_t buf) |
| 155 | { |
| 156 | uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1); |
| 157 | return offset; |
| 158 | } |
| 159 | |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 160 | static inline uint64_t buf_num_pages(uint64_t buf, size_t len) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 161 | { |
| 162 | uint64_t start = buf_page_start(buf) >> PAGE_SHIFT; |
| 163 | uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 164 | uint64_t nPages = end - start + 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 165 | return nPages; |
| 166 | } |
| 167 | |
| 168 | static inline uint64_t buf_page_size(uint32_t size) |
| 169 | { |
| 170 | uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK; |
| 171 | |
| 172 | return sz > PAGE_SIZE ? sz : PAGE_SIZE; |
| 173 | } |
| 174 | |
| 175 | static inline void *uint64_to_ptr(uint64_t addr) |
| 176 | { |
| 177 | void *ptr = (void *)((uintptr_t)addr); |
| 178 | |
| 179 | return ptr; |
| 180 | } |
| 181 | |
| 182 | static inline uint64_t ptr_to_uint64(void *ptr) |
| 183 | { |
| 184 | uint64_t addr = (uint64_t)((uintptr_t)ptr); |
| 185 | |
| 186 | return addr; |
| 187 | } |
| 188 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 189 | struct secure_vm { |
| 190 | int *vmid; |
| 191 | int *vmperm; |
| 192 | int vmcount; |
| 193 | }; |
| 194 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 195 | struct fastrpc_file; |
| 196 | |
| 197 | struct fastrpc_buf { |
| 198 | struct hlist_node hn; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 199 | struct hlist_node hn_rem; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 200 | struct fastrpc_file *fl; |
| 201 | void *virt; |
| 202 | uint64_t phys; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 203 | size_t size; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 204 | unsigned long dma_attr; |
| 205 | uintptr_t raddr; |
| 206 | uint32_t flags; |
| 207 | int remote; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | struct fastrpc_ctx_lst; |
| 211 | |
| 212 | struct overlap { |
| 213 | uintptr_t start; |
| 214 | uintptr_t end; |
| 215 | int raix; |
| 216 | uintptr_t mstart; |
| 217 | uintptr_t mend; |
| 218 | uintptr_t offset; |
| 219 | }; |
| 220 | |
| 221 | struct smq_invoke_ctx { |
| 222 | struct hlist_node hn; |
| 223 | struct completion work; |
| 224 | int retval; |
| 225 | int pid; |
| 226 | int tgid; |
| 227 | remote_arg_t *lpra; |
| 228 | remote_arg64_t *rpra; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 229 | remote_arg64_t *lrpra; /* Local copy of rpra for put_args */ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 230 | int *fds; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 231 | struct fastrpc_mmap **maps; |
| 232 | struct fastrpc_buf *buf; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 233 | struct fastrpc_buf *lbuf; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 234 | size_t used; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 235 | struct fastrpc_file *fl; |
| 236 | uint32_t sc; |
| 237 | struct overlap *overs; |
| 238 | struct overlap **overps; |
| 239 | struct smq_msg msg; |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 240 | unsigned int magic; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 241 | unsigned int *attrs; |
| 242 | uint32_t *crc; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 243 | uint64_t ctxid; |
Mohammed Nayeem Ur Rahman | 32ba95d | 2019-07-26 17:31:37 +0530 | [diff] [blame] | 244 | void *handle; |
| 245 | const void *ptr; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | struct fastrpc_ctx_lst { |
| 249 | struct hlist_head pending; |
| 250 | struct hlist_head interrupted; |
| 251 | }; |
| 252 | |
| 253 | struct fastrpc_smmu { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 254 | struct device *dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 255 | struct dma_iommu_mapping *mapping; |
| 256 | int cb; |
| 257 | int enabled; |
| 258 | int faults; |
| 259 | int secure; |
| 260 | int coherent; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 261 | int sharedcb; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | struct fastrpc_session_ctx { |
| 265 | struct device *dev; |
| 266 | struct fastrpc_smmu smmu; |
| 267 | int used; |
| 268 | }; |
| 269 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 270 | struct fastrpc_static_pd { |
| 271 | char *spdname; |
| 272 | struct notifier_block pdrnb; |
| 273 | struct notifier_block get_service_nb; |
| 274 | void *pdrhandle; |
| 275 | int pdrcount; |
| 276 | int prevpdrcount; |
| 277 | int ispdup; |
| 278 | }; |
| 279 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 280 | struct fastrpc_glink_info { |
| 281 | int link_state; |
| 282 | int port_state; |
| 283 | struct glink_open_config cfg; |
| 284 | struct glink_link_info link_info; |
| 285 | void *link_notify_handle; |
| 286 | }; |
| 287 | |
| 288 | struct fastrpc_channel_ctx { |
| 289 | char *name; |
| 290 | char *subsys; |
| 291 | void *chan; |
| 292 | struct device *dev; |
| 293 | struct fastrpc_session_ctx session[NUM_SESSIONS]; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 294 | struct fastrpc_static_pd spd[NUM_SESSIONS]; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 295 | struct completion work; |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 296 | struct completion workport; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 297 | struct notifier_block nb; |
| 298 | struct kref kref; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 299 | int channel; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 300 | int sesscount; |
| 301 | int ssrcount; |
| 302 | void *handle; |
| 303 | int prevssrcount; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 304 | int issubsystemup; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 305 | int vmid; |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 306 | struct secure_vm rhvm; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 307 | int ramdumpenabled; |
| 308 | void *remoteheap_ramdump_dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 309 | struct fastrpc_glink_info link; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 310 | /* Indicates, if channel is restricted to secure node only */ |
| 311 | int secure; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | struct fastrpc_apps { |
| 315 | struct fastrpc_channel_ctx *channel; |
| 316 | struct cdev cdev; |
| 317 | struct class *class; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 318 | struct mutex smd_mutex; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 319 | struct smq_phy_page range; |
| 320 | struct hlist_head maps; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 321 | uint32_t staticpd_flags; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 322 | dev_t dev_no; |
| 323 | int compat; |
| 324 | struct hlist_head drivers; |
| 325 | spinlock_t hlock; |
| 326 | struct ion_client *client; |
| 327 | struct device *dev; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 328 | unsigned int latency; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 329 | bool glink; |
| 330 | bool legacy; |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 331 | bool secure_flag; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 332 | spinlock_t ctxlock; |
| 333 | struct smq_invoke_ctx *ctxtable[FASTRPC_CTX_MAX]; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | struct fastrpc_mmap { |
| 337 | struct hlist_node hn; |
| 338 | struct fastrpc_file *fl; |
| 339 | struct fastrpc_apps *apps; |
| 340 | int fd; |
| 341 | uint32_t flags; |
| 342 | struct dma_buf *buf; |
| 343 | struct sg_table *table; |
| 344 | struct dma_buf_attachment *attach; |
| 345 | struct ion_handle *handle; |
| 346 | uint64_t phys; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 347 | size_t size; |
| 348 | uintptr_t va; |
| 349 | size_t len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 350 | int refs; |
| 351 | uintptr_t raddr; |
| 352 | int uncached; |
| 353 | int secure; |
| 354 | uintptr_t attr; |
| 355 | }; |
| 356 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 357 | enum fastrpc_perfkeys { |
| 358 | PERF_COUNT = 0, |
| 359 | PERF_FLUSH = 1, |
| 360 | PERF_MAP = 2, |
| 361 | PERF_COPY = 3, |
| 362 | PERF_LINK = 4, |
| 363 | PERF_GETARGS = 5, |
| 364 | PERF_PUTARGS = 6, |
| 365 | PERF_INVARGS = 7, |
| 366 | PERF_INVOKE = 8, |
| 367 | PERF_KEY_MAX = 9, |
| 368 | }; |
| 369 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 370 | struct fastrpc_perf { |
| 371 | int64_t count; |
| 372 | int64_t flush; |
| 373 | int64_t map; |
| 374 | int64_t copy; |
| 375 | int64_t link; |
| 376 | int64_t getargs; |
| 377 | int64_t putargs; |
| 378 | int64_t invargs; |
| 379 | int64_t invoke; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 380 | int64_t tid; |
| 381 | struct hlist_node hn; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 382 | }; |
| 383 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 384 | struct fastrpc_file { |
| 385 | struct hlist_node hn; |
| 386 | spinlock_t hlock; |
| 387 | struct hlist_head maps; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 388 | struct hlist_head cached_bufs; |
| 389 | struct hlist_head remote_bufs; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 390 | struct fastrpc_ctx_lst clst; |
| 391 | struct fastrpc_session_ctx *sctx; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 392 | struct fastrpc_buf *init_mem; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 393 | struct fastrpc_session_ctx *secsctx; |
| 394 | uint32_t mode; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 395 | uint32_t profile; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 396 | int sessionid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 397 | int tgid; |
| 398 | int cid; |
| 399 | int ssrcount; |
| 400 | int pd; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 401 | char *spdname; |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 402 | int file_close; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 403 | int sharedcb; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 404 | struct fastrpc_apps *apps; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 405 | struct hlist_head perf; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 406 | struct dentry *debugfs_file; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 407 | struct mutex perf_mutex; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 408 | struct pm_qos_request pm_qos_req; |
| 409 | int qos_request; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 410 | struct mutex map_mutex; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 411 | struct mutex fl_map_mutex; |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 412 | int refcount; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 413 | /* Identifies the device (MINOR_NUM_DEV / MINOR_NUM_SECURE_DEV) */ |
| 414 | int dev_minor; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 415 | char *debug_buf; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 416 | }; |
| 417 | |
| 418 | static struct fastrpc_apps gfa; |
| 419 | |
| 420 | static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = { |
| 421 | { |
| 422 | .name = "adsprpc-smd", |
| 423 | .subsys = "adsp", |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 424 | .channel = SMD_APPS_QDSP, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 425 | .link.link_info.edge = "lpass", |
| 426 | .link.link_info.transport = "smem", |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 427 | .spd = { |
| 428 | { |
| 429 | .spdname = |
| 430 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, |
| 431 | .pdrnb.notifier_call = |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 432 | fastrpc_pdr_notifier_cb, |
| 433 | }, |
| 434 | { |
| 435 | .spdname = |
| 436 | SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, |
| 437 | .pdrnb.notifier_call = |
| 438 | fastrpc_pdr_notifier_cb, |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 439 | } |
| 440 | }, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 441 | }, |
| 442 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 443 | .name = "mdsprpc-smd", |
| 444 | .subsys = "modem", |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 445 | .channel = SMD_APPS_MODEM, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 446 | .link.link_info.edge = "mpss", |
| 447 | .link.link_info.transport = "smem", |
| 448 | }, |
| 449 | { |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 450 | .name = "sdsprpc-smd", |
| 451 | .subsys = "slpi", |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 452 | .channel = SMD_APPS_DSPS, |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 453 | .link.link_info.edge = "dsps", |
| 454 | .link.link_info.transport = "smem", |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 455 | }, |
| 456 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 457 | .name = "cdsprpc-smd", |
| 458 | .subsys = "cdsp", |
| 459 | .link.link_info.edge = "cdsp", |
| 460 | .link.link_info.transport = "smem", |
| 461 | }, |
| 462 | }; |
| 463 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 464 | static int hlosvm[1] = {VMID_HLOS}; |
| 465 | static int hlosvmperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 466 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 467 | static inline int64_t getnstimediff(struct timespec *start) |
| 468 | { |
| 469 | int64_t ns; |
| 470 | struct timespec ts, b; |
| 471 | |
| 472 | getnstimeofday(&ts); |
| 473 | b = timespec_sub(ts, *start); |
| 474 | ns = timespec_to_ns(&b); |
| 475 | return ns; |
| 476 | } |
| 477 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 478 | static inline int64_t *getperfcounter(struct fastrpc_file *fl, int key) |
| 479 | { |
| 480 | int err = 0; |
| 481 | int64_t *val = NULL; |
| 482 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
| 483 | struct hlist_node *n = NULL; |
| 484 | |
| 485 | VERIFY(err, !IS_ERR_OR_NULL(fl)); |
| 486 | if (err) |
| 487 | goto bail; |
| 488 | |
| 489 | mutex_lock(&fl->perf_mutex); |
| 490 | hlist_for_each_entry_safe(perf, n, &fl->perf, hn) { |
| 491 | if (perf->tid == current->pid) { |
| 492 | fperf = perf; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (IS_ERR_OR_NULL(fperf)) { |
| 498 | fperf = kzalloc(sizeof(*fperf), GFP_KERNEL); |
| 499 | |
| 500 | VERIFY(err, !IS_ERR_OR_NULL(fperf)); |
| 501 | if (err) { |
| 502 | mutex_unlock(&fl->perf_mutex); |
| 503 | kfree(fperf); |
| 504 | goto bail; |
| 505 | } |
| 506 | |
| 507 | fperf->tid = current->pid; |
| 508 | hlist_add_head(&fperf->hn, &fl->perf); |
| 509 | } |
| 510 | |
| 511 | val = ((int64_t *)fperf) + key; |
| 512 | mutex_unlock(&fl->perf_mutex); |
| 513 | bail: |
| 514 | return val; |
| 515 | } |
| 516 | |
| 517 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 518 | static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache) |
| 519 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 520 | struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 521 | int vmid, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 522 | |
| 523 | if (!fl) |
| 524 | return; |
| 525 | if (cache) { |
| 526 | spin_lock(&fl->hlock); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 527 | hlist_add_head(&buf->hn, &fl->cached_bufs); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 528 | spin_unlock(&fl->hlock); |
| 529 | return; |
| 530 | } |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 531 | if (buf->remote) { |
| 532 | spin_lock(&fl->hlock); |
| 533 | hlist_del_init(&buf->hn_rem); |
| 534 | spin_unlock(&fl->hlock); |
| 535 | buf->remote = 0; |
| 536 | buf->raddr = 0; |
| 537 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 538 | if (!IS_ERR_OR_NULL(buf->virt)) { |
| 539 | int destVM[1] = {VMID_HLOS}; |
| 540 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 541 | |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 542 | VERIFY(err, fl->sctx != NULL); |
| 543 | if (err) |
| 544 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 545 | if (fl->sctx->smmu.cb) |
| 546 | buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32); |
| 547 | vmid = fl->apps->channel[fl->cid].vmid; |
| 548 | if (vmid) { |
| 549 | int srcVM[2] = {VMID_HLOS, vmid}; |
| 550 | |
| 551 | hyp_assign_phys(buf->phys, buf_page_size(buf->size), |
| 552 | srcVM, 2, destVM, destVMperm, 1); |
| 553 | } |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 554 | dma_free_attrs(fl->sctx->smmu.dev, buf->size, buf->virt, |
| 555 | buf->phys, buf->dma_attr); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 556 | } |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 557 | bail: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 558 | kfree(buf); |
| 559 | } |
| 560 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 561 | static void fastrpc_cached_buf_list_free(struct fastrpc_file *fl) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 562 | { |
| 563 | struct fastrpc_buf *buf, *free; |
| 564 | |
| 565 | do { |
| 566 | struct hlist_node *n; |
| 567 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 568 | free = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 569 | spin_lock(&fl->hlock); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 570 | hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 571 | hlist_del_init(&buf->hn); |
| 572 | free = buf; |
| 573 | break; |
| 574 | } |
| 575 | spin_unlock(&fl->hlock); |
| 576 | if (free) |
| 577 | fastrpc_buf_free(free, 0); |
| 578 | } while (free); |
| 579 | } |
| 580 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 581 | static void fastrpc_remote_buf_list_free(struct fastrpc_file *fl) |
| 582 | { |
| 583 | struct fastrpc_buf *buf, *free; |
| 584 | |
| 585 | do { |
| 586 | struct hlist_node *n; |
| 587 | |
| 588 | free = NULL; |
| 589 | spin_lock(&fl->hlock); |
| 590 | hlist_for_each_entry_safe(buf, n, &fl->remote_bufs, hn_rem) { |
| 591 | free = buf; |
| 592 | break; |
| 593 | } |
| 594 | spin_unlock(&fl->hlock); |
| 595 | if (free) |
| 596 | fastrpc_buf_free(free, 0); |
| 597 | } while (free); |
| 598 | } |
| 599 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 600 | static void fastrpc_mmap_add(struct fastrpc_mmap *map) |
| 601 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 602 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 603 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 604 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 605 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 606 | spin_lock(&me->hlock); |
| 607 | hlist_add_head(&map->hn, &me->maps); |
| 608 | spin_unlock(&me->hlock); |
| 609 | } else { |
| 610 | struct fastrpc_file *fl = map->fl; |
| 611 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 612 | hlist_add_head(&map->hn, &fl->maps); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 613 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 614 | } |
| 615 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 616 | static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 617 | uintptr_t va, size_t len, int mflags, int refs, |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 618 | struct fastrpc_mmap **ppmap) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 619 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 620 | struct fastrpc_apps *me = &gfa; |
| 621 | struct fastrpc_mmap *match = NULL, *map = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 622 | struct hlist_node *n; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 623 | |
| 624 | if ((va + len) < va) |
| 625 | return -EOVERFLOW; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 626 | if (mflags == ADSP_MMAP_HEAP_ADDR || |
| 627 | mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 628 | spin_lock(&me->hlock); |
| 629 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 630 | if (va >= map->va && |
| 631 | va + len <= map->va + map->len && |
| 632 | map->fd == fd) { |
Tharun Kumar Merugu | 496ad34 | 2019-06-06 15:01:42 +0530 | [diff] [blame] | 633 | if (refs) { |
| 634 | if (map->refs + 1 == INT_MAX) { |
| 635 | spin_unlock(&me->hlock); |
| 636 | return -ETOOMANYREFS; |
| 637 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 638 | map->refs++; |
Tharun Kumar Merugu | 496ad34 | 2019-06-06 15:01:42 +0530 | [diff] [blame] | 639 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 640 | match = map; |
| 641 | break; |
| 642 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 643 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 644 | spin_unlock(&me->hlock); |
| 645 | } else { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 646 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 647 | if (va >= map->va && |
| 648 | va + len <= map->va + map->len && |
| 649 | map->fd == fd) { |
Tharun Kumar Merugu | 496ad34 | 2019-06-06 15:01:42 +0530 | [diff] [blame] | 650 | if (refs) { |
| 651 | if (map->refs + 1 == INT_MAX) |
| 652 | return -ETOOMANYREFS; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 653 | map->refs++; |
Tharun Kumar Merugu | 496ad34 | 2019-06-06 15:01:42 +0530 | [diff] [blame] | 654 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 655 | match = map; |
| 656 | break; |
| 657 | } |
| 658 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 659 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 660 | if (match) { |
| 661 | *ppmap = match; |
| 662 | return 0; |
| 663 | } |
| 664 | return -ENOTTY; |
| 665 | } |
| 666 | |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 667 | static int dma_alloc_memory(dma_addr_t *region_phys, void **vaddr, size_t size, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 668 | unsigned long dma_attrs) |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 669 | { |
Jeya R | e931076 | 2020-07-29 12:10:54 +0530 | [diff] [blame] | 670 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 671 | struct fastrpc_apps *me = &gfa; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 672 | |
| 673 | if (me->dev == NULL) { |
| 674 | pr_err("device adsprpc-mem is not initialized\n"); |
| 675 | return -ENODEV; |
| 676 | } |
Jeya R | e931076 | 2020-07-29 12:10:54 +0530 | [diff] [blame] | 677 | VERIFY(err, size > 0 && size < MAX_SIZE_LIMIT); |
| 678 | if (err) { |
| 679 | err = -EFAULT; |
| 680 | pr_err("adsprpc: %s: invalid allocation size 0x%zx\n", |
| 681 | __func__, size); |
| 682 | return err; |
| 683 | } |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 684 | *vaddr = dma_alloc_attrs(me->dev, size, region_phys, GFP_KERNEL, |
Tharun Kumar Merugu | 48d5ff3 | 2018-04-16 19:24:16 +0530 | [diff] [blame] | 685 | dma_attrs); |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 686 | if (IS_ERR_OR_NULL(*vaddr)) { |
| 687 | pr_err("adsprpc: %s: %s: dma_alloc_attrs failed for size 0x%zx, returned %pK\n", |
| 688 | current->comm, __func__, size, (*vaddr)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 689 | return -ENOMEM; |
| 690 | } |
| 691 | return 0; |
| 692 | } |
| 693 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 694 | static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 695 | size_t len, struct fastrpc_mmap **ppmap) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 696 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 697 | struct fastrpc_mmap *match = NULL, *map; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 698 | struct hlist_node *n; |
| 699 | struct fastrpc_apps *me = &gfa; |
| 700 | |
| 701 | spin_lock(&me->hlock); |
| 702 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 703 | if (map->raddr == va && |
| 704 | map->raddr + map->len == va + len && |
| 705 | map->refs == 1) { |
| 706 | match = map; |
| 707 | hlist_del_init(&map->hn); |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | spin_unlock(&me->hlock); |
| 712 | if (match) { |
| 713 | *ppmap = match; |
| 714 | return 0; |
| 715 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 716 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 717 | if (map->raddr == va && |
| 718 | map->raddr + map->len == va + len && |
| 719 | map->refs == 1) { |
| 720 | match = map; |
| 721 | hlist_del_init(&map->hn); |
| 722 | break; |
| 723 | } |
| 724 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 725 | if (match) { |
| 726 | *ppmap = match; |
| 727 | return 0; |
| 728 | } |
| 729 | return -ENOTTY; |
| 730 | } |
| 731 | |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 732 | static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 733 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 734 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 735 | struct fastrpc_file *fl; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 736 | int vmid, cid = -1, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 737 | struct fastrpc_session_ctx *sess; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 738 | |
| 739 | if (!map) |
| 740 | return; |
| 741 | fl = map->fl; |
Jeya R | b70b4ad | 2021-01-25 10:28:42 -0800 | [diff] [blame] | 742 | if (!fl) |
| 743 | return; |
| 744 | if (!(map->flags == ADSP_MMAP_HEAP_ADDR || |
Jeya R | ccafee2 | 2020-05-26 18:17:26 +0530 | [diff] [blame] | 745 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)) { |
| 746 | cid = fl->cid; |
| 747 | VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS); |
| 748 | if (err) { |
| 749 | err = -ECHRNG; |
| 750 | pr_err("adsprpc: ERROR:%s, Invalid channel id: %d, err:%d", |
| 751 | __func__, cid, err); |
| 752 | return; |
| 753 | } |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 754 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 755 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 756 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 757 | spin_lock(&me->hlock); |
| 758 | map->refs--; |
| 759 | if (!map->refs) |
| 760 | hlist_del_init(&map->hn); |
| 761 | spin_unlock(&me->hlock); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 762 | if (map->refs > 0) |
| 763 | return; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 764 | } else { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 765 | map->refs--; |
| 766 | if (!map->refs) |
| 767 | hlist_del_init(&map->hn); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 768 | if (map->refs > 0 && !flags) |
| 769 | return; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 770 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 771 | if (map->flags == ADSP_MMAP_HEAP_ADDR || |
| 772 | map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 773 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 774 | if (me->dev == NULL) { |
| 775 | pr_err("failed to free remote heap allocation\n"); |
| 776 | return; |
| 777 | } |
| 778 | if (map->phys) { |
Tharun Kumar Merugu | 72c9025 | 2019-08-29 18:36:08 +0530 | [diff] [blame] | 779 | unsigned long dma_attrs = DMA_ATTR_SKIP_ZEROING | |
| 780 | DMA_ATTR_NO_KERNEL_MAPPING; |
Tharun Kumar Merugu | 48d5ff3 | 2018-04-16 19:24:16 +0530 | [diff] [blame] | 781 | dma_free_attrs(me->dev, map->size, (void *)map->va, |
| 782 | (dma_addr_t)map->phys, dma_attrs); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 783 | } |
Tharun Kumar Merugu | 35a94a5 | 2018-02-01 21:09:04 +0530 | [diff] [blame] | 784 | } else if (map->flags == FASTRPC_DMAHANDLE_NOMAP) { |
| 785 | if (!IS_ERR_OR_NULL(map->handle)) |
| 786 | ion_free(fl->apps->client, map->handle); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 787 | } else { |
| 788 | int destVM[1] = {VMID_HLOS}; |
| 789 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 790 | |
| 791 | if (map->secure) |
| 792 | sess = fl->secsctx; |
| 793 | else |
| 794 | sess = fl->sctx; |
| 795 | |
| 796 | if (!IS_ERR_OR_NULL(map->handle)) |
| 797 | ion_free(fl->apps->client, map->handle); |
| 798 | if (sess && sess->smmu.enabled) { |
| 799 | if (map->size || map->phys) |
| 800 | msm_dma_unmap_sg(sess->smmu.dev, |
| 801 | map->table->sgl, |
| 802 | map->table->nents, DMA_BIDIRECTIONAL, |
| 803 | map->buf); |
| 804 | } |
| 805 | vmid = fl->apps->channel[fl->cid].vmid; |
| 806 | if (vmid && map->phys) { |
| 807 | int srcVM[2] = {VMID_HLOS, vmid}; |
| 808 | |
| 809 | hyp_assign_phys(map->phys, buf_page_size(map->size), |
| 810 | srcVM, 2, destVM, destVMperm, 1); |
| 811 | } |
| 812 | |
| 813 | if (!IS_ERR_OR_NULL(map->table)) |
| 814 | dma_buf_unmap_attachment(map->attach, map->table, |
| 815 | DMA_BIDIRECTIONAL); |
| 816 | if (!IS_ERR_OR_NULL(map->attach)) |
| 817 | dma_buf_detach(map->buf, map->attach); |
| 818 | if (!IS_ERR_OR_NULL(map->buf)) |
| 819 | dma_buf_put(map->buf); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 820 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 821 | kfree(map); |
| 822 | } |
| 823 | |
| 824 | static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure, |
| 825 | struct fastrpc_session_ctx **session); |
| 826 | |
| 827 | static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 828 | unsigned int attr, uintptr_t va, size_t len, int mflags, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 829 | struct fastrpc_mmap **ppmap) |
| 830 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 831 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 832 | struct fastrpc_session_ctx *sess; |
| 833 | struct fastrpc_apps *apps = fl->apps; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 834 | struct fastrpc_mmap *map = NULL; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 835 | struct fastrpc_channel_ctx *chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 836 | unsigned long attrs; |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 837 | dma_addr_t region_phys = 0; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 838 | void *region_vaddr = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 839 | unsigned long flags; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 840 | int err = 0, vmid, cid = -1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 841 | |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 842 | cid = fl->cid; |
| 843 | VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS); |
| 844 | if (err) { |
| 845 | err = -ECHRNG; |
| 846 | goto bail; |
| 847 | } |
| 848 | chan = &apps->channel[cid]; |
Sathish Ambley | ae5ee54 | 2017-01-16 22:24:23 -0800 | [diff] [blame] | 849 | if (!fastrpc_mmap_find(fl, fd, va, len, mflags, 1, ppmap)) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 850 | return 0; |
| 851 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 852 | VERIFY(err, !IS_ERR_OR_NULL(map)); |
| 853 | if (err) |
| 854 | goto bail; |
| 855 | INIT_HLIST_NODE(&map->hn); |
| 856 | map->flags = mflags; |
| 857 | map->refs = 1; |
| 858 | map->fl = fl; |
| 859 | map->fd = fd; |
| 860 | map->attr = attr; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 861 | if (mflags == ADSP_MMAP_HEAP_ADDR || |
| 862 | mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 863 | unsigned long dma_attrs = DMA_ATTR_SKIP_ZEROING | |
| 864 | DMA_ATTR_NO_KERNEL_MAPPING; |
| 865 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 866 | map->apps = me; |
| 867 | map->fl = NULL; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 868 | VERIFY(err, !dma_alloc_memory(®ion_phys, ®ion_vaddr, |
| 869 | len, dma_attrs)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 870 | if (err) |
| 871 | goto bail; |
c_mtharu | f931ff9 | 2017-11-30 19:35:30 +0530 | [diff] [blame] | 872 | map->phys = (uintptr_t)region_phys; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 873 | map->size = len; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 874 | map->va = (uintptr_t)region_vaddr; |
Tharun Kumar Merugu | 35a94a5 | 2018-02-01 21:09:04 +0530 | [diff] [blame] | 875 | } else if (mflags == FASTRPC_DMAHANDLE_NOMAP) { |
| 876 | ion_phys_addr_t iphys; |
| 877 | |
| 878 | VERIFY(err, !IS_ERR_OR_NULL(map->handle = |
| 879 | ion_import_dma_buf_fd(fl->apps->client, fd))); |
| 880 | if (err) |
| 881 | goto bail; |
| 882 | |
| 883 | map->uncached = 1; |
| 884 | map->buf = NULL; |
| 885 | map->attach = NULL; |
| 886 | map->table = NULL; |
| 887 | map->va = 0; |
| 888 | map->phys = 0; |
| 889 | |
| 890 | err = ion_phys(fl->apps->client, map->handle, |
| 891 | &iphys, &map->size); |
| 892 | if (err) |
| 893 | goto bail; |
| 894 | map->phys = (uint64_t)iphys; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 895 | } else { |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 896 | if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) { |
| 897 | pr_info("adsprpc: buffer mapped with persist attr %x\n", |
| 898 | (unsigned int)map->attr); |
| 899 | map->refs = 2; |
| 900 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 901 | VERIFY(err, !IS_ERR_OR_NULL(map->handle = |
| 902 | ion_import_dma_buf_fd(fl->apps->client, fd))); |
| 903 | if (err) |
| 904 | goto bail; |
| 905 | VERIFY(err, !ion_handle_get_flags(fl->apps->client, map->handle, |
| 906 | &flags)); |
| 907 | if (err) |
| 908 | goto bail; |
| 909 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 910 | map->secure = flags & ION_FLAG_SECURE; |
| 911 | if (map->secure) { |
| 912 | if (!fl->secsctx) |
| 913 | err = fastrpc_session_alloc(chan, 1, |
| 914 | &fl->secsctx); |
| 915 | if (err) |
| 916 | goto bail; |
| 917 | } |
| 918 | if (map->secure) |
| 919 | sess = fl->secsctx; |
| 920 | else |
| 921 | sess = fl->sctx; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 922 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 923 | VERIFY(err, !IS_ERR_OR_NULL(sess)); |
| 924 | if (err) |
| 925 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 926 | |
| 927 | map->uncached = !ION_IS_CACHED(flags); |
| 928 | if (map->attr & FASTRPC_ATTR_NOVA && !sess->smmu.coherent) |
| 929 | map->uncached = 1; |
| 930 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 931 | VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd))); |
| 932 | if (err) |
| 933 | goto bail; |
| 934 | VERIFY(err, !IS_ERR_OR_NULL(map->attach = |
| 935 | dma_buf_attach(map->buf, sess->smmu.dev))); |
| 936 | if (err) |
| 937 | goto bail; |
| 938 | VERIFY(err, !IS_ERR_OR_NULL(map->table = |
| 939 | dma_buf_map_attachment(map->attach, |
| 940 | DMA_BIDIRECTIONAL))); |
| 941 | if (err) |
| 942 | goto bail; |
| 943 | if (sess->smmu.enabled) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 944 | attrs = DMA_ATTR_EXEC_MAPPING; |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 945 | |
| 946 | if (map->attr & FASTRPC_ATTR_NON_COHERENT || |
| 947 | (sess->smmu.coherent && map->uncached)) |
| 948 | attrs |= DMA_ATTR_FORCE_NON_COHERENT; |
| 949 | else if (map->attr & FASTRPC_ATTR_COHERENT) |
| 950 | attrs |= DMA_ATTR_FORCE_COHERENT; |
| 951 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 952 | VERIFY(err, map->table->nents == |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 953 | msm_dma_map_sg_attrs(sess->smmu.dev, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 954 | map->table->sgl, map->table->nents, |
| 955 | DMA_BIDIRECTIONAL, map->buf, attrs)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 956 | if (err) |
| 957 | goto bail; |
| 958 | } else { |
| 959 | VERIFY(err, map->table->nents == 1); |
| 960 | if (err) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 961 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 962 | } |
| 963 | map->phys = sg_dma_address(map->table->sgl); |
Tharun Kumar Merugu | 93f319a | 2018-02-01 17:35:42 +0530 | [diff] [blame] | 964 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 965 | if (sess->smmu.cb) { |
| 966 | map->phys += ((uint64_t)sess->smmu.cb << 32); |
| 967 | map->size = sg_dma_len(map->table->sgl); |
| 968 | } else { |
| 969 | map->size = buf_page_size(len); |
| 970 | } |
Tharun Kumar Merugu | 93f319a | 2018-02-01 17:35:42 +0530 | [diff] [blame] | 971 | |
Mohammed Nayeem Ur Rahman | 62f7f9c | 2020-04-13 11:16:19 +0530 | [diff] [blame] | 972 | VERIFY(err, map->size >= len && map->size < MAX_SIZE_LIMIT); |
| 973 | if (err) { |
| 974 | err = -EFAULT; |
| 975 | goto bail; |
| 976 | } |
| 977 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 978 | vmid = fl->apps->channel[fl->cid].vmid; |
Tharun Kumar Merugu | 93f319a | 2018-02-01 17:35:42 +0530 | [diff] [blame] | 979 | if (!sess->smmu.enabled && !vmid) { |
| 980 | VERIFY(err, map->phys >= me->range.addr && |
| 981 | map->phys + map->size <= |
| 982 | me->range.addr + me->range.size); |
| 983 | if (err) { |
| 984 | pr_err("adsprpc: mmap fail out of range\n"); |
| 985 | goto bail; |
| 986 | } |
| 987 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 988 | if (vmid) { |
| 989 | int srcVM[1] = {VMID_HLOS}; |
| 990 | int destVM[2] = {VMID_HLOS, vmid}; |
| 991 | int destVMperm[2] = {PERM_READ | PERM_WRITE, |
| 992 | PERM_READ | PERM_WRITE | PERM_EXEC}; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 993 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 994 | VERIFY(err, !hyp_assign_phys(map->phys, |
| 995 | buf_page_size(map->size), |
| 996 | srcVM, 1, destVM, destVMperm, 2)); |
| 997 | if (err) |
| 998 | goto bail; |
| 999 | } |
| 1000 | map->va = va; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1001 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1002 | map->len = len; |
| 1003 | |
| 1004 | fastrpc_mmap_add(map); |
| 1005 | *ppmap = map; |
| 1006 | |
| 1007 | bail: |
| 1008 | if (err && map) |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1009 | fastrpc_mmap_free(map, 0); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1010 | return err; |
| 1011 | } |
| 1012 | |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1013 | static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1014 | unsigned long dma_attr, uint32_t rflags, |
| 1015 | int remote, struct fastrpc_buf **obuf) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1016 | { |
| 1017 | int err = 0, vmid; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1018 | struct fastrpc_buf *buf = NULL, *fr = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1019 | struct hlist_node *n; |
| 1020 | |
Mohammed Nayeem Ur Rahman | 62f7f9c | 2020-04-13 11:16:19 +0530 | [diff] [blame] | 1021 | VERIFY(err, size > 0 && size < MAX_SIZE_LIMIT); |
| 1022 | if (err) { |
| 1023 | err = -EFAULT; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1024 | goto bail; |
Mohammed Nayeem Ur Rahman | 62f7f9c | 2020-04-13 11:16:19 +0530 | [diff] [blame] | 1025 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1026 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1027 | if (!remote) { |
| 1028 | /* find the smallest buffer that fits in the cache */ |
| 1029 | spin_lock(&fl->hlock); |
| 1030 | hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) { |
| 1031 | if (buf->size >= size && (!fr || fr->size > buf->size)) |
| 1032 | fr = buf; |
| 1033 | } |
| 1034 | if (fr) |
| 1035 | hlist_del_init(&fr->hn); |
| 1036 | spin_unlock(&fl->hlock); |
| 1037 | if (fr) { |
| 1038 | *obuf = fr; |
| 1039 | return 0; |
| 1040 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1041 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1042 | buf = NULL; |
| 1043 | VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1044 | if (err) |
| 1045 | goto bail; |
| 1046 | INIT_HLIST_NODE(&buf->hn); |
| 1047 | buf->fl = fl; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1048 | buf->virt = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1049 | buf->phys = 0; |
| 1050 | buf->size = size; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1051 | buf->dma_attr = dma_attr; |
| 1052 | buf->flags = rflags; |
| 1053 | buf->raddr = 0; |
| 1054 | buf->remote = 0; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 1055 | VERIFY(err, fl && fl->sctx != NULL); |
| 1056 | if (err) { |
| 1057 | err = -EBADR; |
| 1058 | goto bail; |
| 1059 | } |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1060 | buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size, |
| 1061 | (dma_addr_t *)&buf->phys, |
| 1062 | GFP_KERNEL, buf->dma_attr); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1063 | if (IS_ERR_OR_NULL(buf->virt)) { |
| 1064 | /* free cache and retry */ |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1065 | fastrpc_cached_buf_list_free(fl); |
| 1066 | buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size, |
| 1067 | (dma_addr_t *)&buf->phys, |
| 1068 | GFP_KERNEL, buf->dma_attr); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1069 | VERIFY(err, !IS_ERR_OR_NULL(buf->virt)); |
| 1070 | } |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1071 | if (err) { |
| 1072 | err = -ENOMEM; |
| 1073 | pr_err("adsprpc: %s: %s: dma_alloc_attrs failed for size 0x%zx\n", |
| 1074 | current->comm, __func__, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1075 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1076 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1077 | if (fl->sctx->smmu.cb) |
| 1078 | buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32); |
| 1079 | vmid = fl->apps->channel[fl->cid].vmid; |
| 1080 | if (vmid) { |
| 1081 | int srcVM[1] = {VMID_HLOS}; |
| 1082 | int destVM[2] = {VMID_HLOS, vmid}; |
| 1083 | int destVMperm[2] = {PERM_READ | PERM_WRITE, |
| 1084 | PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 1085 | |
| 1086 | VERIFY(err, !hyp_assign_phys(buf->phys, buf_page_size(size), |
| 1087 | srcVM, 1, destVM, destVMperm, 2)); |
| 1088 | if (err) |
| 1089 | goto bail; |
| 1090 | } |
| 1091 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1092 | if (remote) { |
| 1093 | INIT_HLIST_NODE(&buf->hn_rem); |
| 1094 | spin_lock(&fl->hlock); |
| 1095 | hlist_add_head(&buf->hn_rem, &fl->remote_bufs); |
| 1096 | spin_unlock(&fl->hlock); |
| 1097 | buf->remote = remote; |
| 1098 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1099 | *obuf = buf; |
| 1100 | bail: |
| 1101 | if (err && buf) |
| 1102 | fastrpc_buf_free(buf, 0); |
| 1103 | return err; |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | static int context_restore_interrupted(struct fastrpc_file *fl, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1108 | struct fastrpc_ioctl_invoke_crc *inv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1109 | struct smq_invoke_ctx **po) |
| 1110 | { |
| 1111 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1112 | struct smq_invoke_ctx *ctx = NULL, *ictx = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1113 | struct hlist_node *n; |
| 1114 | struct fastrpc_ioctl_invoke *invoke = &inv->inv; |
| 1115 | |
| 1116 | spin_lock(&fl->hlock); |
| 1117 | hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) { |
| 1118 | if (ictx->pid == current->pid) { |
| 1119 | if (invoke->sc != ictx->sc || ictx->fl != fl) |
| 1120 | err = -1; |
| 1121 | else { |
| 1122 | ctx = ictx; |
| 1123 | hlist_del_init(&ctx->hn); |
| 1124 | hlist_add_head(&ctx->hn, &fl->clst.pending); |
| 1125 | } |
| 1126 | break; |
| 1127 | } |
| 1128 | } |
| 1129 | spin_unlock(&fl->hlock); |
| 1130 | if (ctx) |
| 1131 | *po = ctx; |
| 1132 | return err; |
| 1133 | } |
| 1134 | |
| 1135 | #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1) |
| 1136 | static int overlap_ptr_cmp(const void *a, const void *b) |
| 1137 | { |
| 1138 | struct overlap *pa = *((struct overlap **)a); |
| 1139 | struct overlap *pb = *((struct overlap **)b); |
| 1140 | /* sort with lowest starting buffer first */ |
| 1141 | int st = CMP(pa->start, pb->start); |
| 1142 | /* sort with highest ending buffer first */ |
| 1143 | int ed = CMP(pb->end, pa->end); |
| 1144 | return st == 0 ? ed : st; |
| 1145 | } |
| 1146 | |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 1147 | static int context_build_overlap(struct smq_invoke_ctx *ctx) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1148 | { |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 1149 | int i, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1150 | remote_arg_t *lpra = ctx->lpra; |
| 1151 | int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc); |
| 1152 | int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc); |
| 1153 | int nbufs = inbufs + outbufs; |
| 1154 | struct overlap max; |
| 1155 | |
| 1156 | for (i = 0; i < nbufs; ++i) { |
| 1157 | ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv; |
| 1158 | ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len; |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 1159 | if (lpra[i].buf.len) { |
| 1160 | VERIFY(err, ctx->overs[i].end > ctx->overs[i].start); |
| 1161 | if (err) |
| 1162 | goto bail; |
| 1163 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1164 | ctx->overs[i].raix = i; |
| 1165 | ctx->overps[i] = &ctx->overs[i]; |
| 1166 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1167 | sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1168 | max.start = 0; |
| 1169 | max.end = 0; |
| 1170 | for (i = 0; i < nbufs; ++i) { |
| 1171 | if (ctx->overps[i]->start < max.end) { |
| 1172 | ctx->overps[i]->mstart = max.end; |
| 1173 | ctx->overps[i]->mend = ctx->overps[i]->end; |
| 1174 | ctx->overps[i]->offset = max.end - |
| 1175 | ctx->overps[i]->start; |
| 1176 | if (ctx->overps[i]->end > max.end) { |
| 1177 | max.end = ctx->overps[i]->end; |
| 1178 | } else { |
| 1179 | ctx->overps[i]->mend = 0; |
| 1180 | ctx->overps[i]->mstart = 0; |
| 1181 | } |
| 1182 | } else { |
| 1183 | ctx->overps[i]->mend = ctx->overps[i]->end; |
| 1184 | ctx->overps[i]->mstart = ctx->overps[i]->start; |
| 1185 | ctx->overps[i]->offset = 0; |
| 1186 | max = *ctx->overps[i]; |
| 1187 | } |
| 1188 | } |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 1189 | bail: |
| 1190 | return err; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | #define K_COPY_FROM_USER(err, kernel, dst, src, size) \ |
| 1194 | do {\ |
| 1195 | if (!(kernel))\ |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1196 | VERIFY(err, 0 == copy_from_user((dst),\ |
| 1197 | (void const __user *)(src),\ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1198 | (size)));\ |
| 1199 | else\ |
| 1200 | memmove((dst), (src), (size));\ |
| 1201 | } while (0) |
| 1202 | |
| 1203 | #define K_COPY_TO_USER(err, kernel, dst, src, size) \ |
| 1204 | do {\ |
| 1205 | if (!(kernel))\ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1206 | VERIFY(err, 0 == copy_to_user((void __user *)(dst),\ |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1207 | (src), (size)));\ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1208 | else\ |
| 1209 | memmove((dst), (src), (size));\ |
| 1210 | } while (0) |
| 1211 | |
| 1212 | |
| 1213 | static void context_free(struct smq_invoke_ctx *ctx); |
| 1214 | |
| 1215 | static int context_alloc(struct fastrpc_file *fl, uint32_t kernel, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1216 | struct fastrpc_ioctl_invoke_crc *invokefd, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1217 | struct smq_invoke_ctx **po) |
| 1218 | { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1219 | struct fastrpc_apps *me = &gfa; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1220 | int err = 0, bufs, ii, size = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1221 | struct smq_invoke_ctx *ctx = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1222 | struct fastrpc_ctx_lst *clst = &fl->clst; |
| 1223 | struct fastrpc_ioctl_invoke *invoke = &invokefd->inv; |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1224 | unsigned long irq_flags = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1225 | |
| 1226 | bufs = REMOTE_SCALARS_LENGTH(invoke->sc); |
| 1227 | size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) + |
| 1228 | sizeof(*ctx->fds) * (bufs) + |
| 1229 | sizeof(*ctx->attrs) * (bufs) + |
| 1230 | sizeof(*ctx->overs) * (bufs) + |
| 1231 | sizeof(*ctx->overps) * (bufs); |
| 1232 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1233 | VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1234 | if (err) |
| 1235 | goto bail; |
| 1236 | |
| 1237 | INIT_HLIST_NODE(&ctx->hn); |
| 1238 | hlist_add_fake(&ctx->hn); |
| 1239 | ctx->fl = fl; |
| 1240 | ctx->maps = (struct fastrpc_mmap **)(&ctx[1]); |
| 1241 | ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]); |
| 1242 | ctx->fds = (int *)(&ctx->lpra[bufs]); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1243 | if (me->legacy) { |
| 1244 | ctx->overs = (struct overlap *)(&ctx->fds[bufs]); |
| 1245 | ctx->overps = (struct overlap **)(&ctx->overs[bufs]); |
| 1246 | } else { |
| 1247 | ctx->attrs = (unsigned int *)(&ctx->fds[bufs]); |
| 1248 | ctx->overs = (struct overlap *)(&ctx->attrs[bufs]); |
| 1249 | ctx->overps = (struct overlap **)(&ctx->overs[bufs]); |
| 1250 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1251 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1252 | K_COPY_FROM_USER(err, kernel, (void *)ctx->lpra, invoke->pra, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1253 | bufs * sizeof(*ctx->lpra)); |
| 1254 | if (err) |
| 1255 | goto bail; |
| 1256 | |
| 1257 | if (invokefd->fds) { |
| 1258 | K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds, |
| 1259 | bufs * sizeof(*ctx->fds)); |
| 1260 | if (err) |
| 1261 | goto bail; |
| 1262 | } |
| 1263 | if (invokefd->attrs) { |
| 1264 | K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs, |
| 1265 | bufs * sizeof(*ctx->attrs)); |
| 1266 | if (err) |
| 1267 | goto bail; |
| 1268 | } |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1269 | ctx->crc = (uint32_t *)invokefd->crc; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1270 | ctx->sc = invoke->sc; |
Sathish Ambley | 9466d67 | 2017-01-25 10:51:55 -0800 | [diff] [blame] | 1271 | if (bufs) { |
| 1272 | VERIFY(err, 0 == context_build_overlap(ctx)); |
| 1273 | if (err) |
| 1274 | goto bail; |
| 1275 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1276 | ctx->retval = -1; |
| 1277 | ctx->pid = current->pid; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1278 | ctx->tgid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1279 | init_completion(&ctx->work); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 1280 | ctx->magic = FASTRPC_CTX_MAGIC; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1281 | |
| 1282 | spin_lock(&fl->hlock); |
| 1283 | hlist_add_head(&ctx->hn, &clst->pending); |
| 1284 | spin_unlock(&fl->hlock); |
| 1285 | |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1286 | spin_lock_irqsave(&me->ctxlock, irq_flags); |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1287 | for (ii = 0; ii < FASTRPC_CTX_MAX; ii++) { |
| 1288 | if (!me->ctxtable[ii]) { |
| 1289 | me->ctxtable[ii] = ctx; |
| 1290 | ctx->ctxid = (ptr_to_uint64(ctx) & ~0xFFF)|(ii << 4); |
| 1291 | break; |
| 1292 | } |
| 1293 | } |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1294 | spin_unlock_irqrestore(&me->ctxlock, irq_flags); |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1295 | VERIFY(err, ii < FASTRPC_CTX_MAX); |
| 1296 | if (err) { |
| 1297 | pr_err("adsprpc: out of context memory\n"); |
| 1298 | goto bail; |
| 1299 | } |
| 1300 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1301 | *po = ctx; |
| 1302 | bail: |
| 1303 | if (ctx && err) |
| 1304 | context_free(ctx); |
| 1305 | return err; |
| 1306 | } |
| 1307 | |
| 1308 | static void context_save_interrupted(struct smq_invoke_ctx *ctx) |
| 1309 | { |
| 1310 | struct fastrpc_ctx_lst *clst = &ctx->fl->clst; |
| 1311 | |
| 1312 | spin_lock(&ctx->fl->hlock); |
| 1313 | hlist_del_init(&ctx->hn); |
| 1314 | hlist_add_head(&ctx->hn, &clst->interrupted); |
| 1315 | spin_unlock(&ctx->fl->hlock); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static void context_free(struct smq_invoke_ctx *ctx) |
| 1319 | { |
| 1320 | int i; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1321 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1322 | int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) + |
| 1323 | REMOTE_SCALARS_OUTBUFS(ctx->sc); |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1324 | unsigned long irq_flags = 0; |
| 1325 | void *handle = NULL; |
| 1326 | const void *ptr = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1327 | spin_lock(&ctx->fl->hlock); |
| 1328 | hlist_del_init(&ctx->hn); |
| 1329 | spin_unlock(&ctx->fl->hlock); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1330 | mutex_lock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1331 | for (i = 0; i < nbufs; ++i) |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1332 | fastrpc_mmap_free(ctx->maps[i], 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1333 | |
| 1334 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1335 | fastrpc_buf_free(ctx->buf, 1); |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1336 | fastrpc_buf_free(ctx->lbuf, 1); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 1337 | ctx->magic = 0; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1338 | ctx->ctxid = 0; |
| 1339 | |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1340 | spin_lock_irqsave(&me->ctxlock, irq_flags); |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1341 | for (i = 0; i < FASTRPC_CTX_MAX; i++) { |
| 1342 | if (me->ctxtable[i] == ctx) { |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1343 | handle = me->ctxtable[i]->handle; |
| 1344 | ptr = me->ctxtable[i]->ptr; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1345 | me->ctxtable[i] = NULL; |
| 1346 | break; |
| 1347 | } |
| 1348 | } |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 1349 | spin_unlock_irqrestore(&me->ctxlock, irq_flags); |
| 1350 | if (handle) { |
| 1351 | glink_rx_done(handle, ptr, true); |
| 1352 | handle = NULL; |
| 1353 | } |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1354 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1355 | kfree(ctx); |
| 1356 | } |
| 1357 | |
| 1358 | static void context_notify_user(struct smq_invoke_ctx *ctx, int retval) |
| 1359 | { |
| 1360 | ctx->retval = retval; |
| 1361 | complete(&ctx->work); |
| 1362 | } |
| 1363 | |
| 1364 | |
| 1365 | static void fastrpc_notify_users(struct fastrpc_file *me) |
| 1366 | { |
| 1367 | struct smq_invoke_ctx *ictx; |
| 1368 | struct hlist_node *n; |
| 1369 | |
| 1370 | spin_lock(&me->hlock); |
| 1371 | hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) { |
| 1372 | complete(&ictx->work); |
| 1373 | } |
| 1374 | hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) { |
| 1375 | complete(&ictx->work); |
| 1376 | } |
| 1377 | spin_unlock(&me->hlock); |
| 1378 | |
| 1379 | } |
| 1380 | |
Tharun Kumar Merugu | 77dd587 | 2018-04-02 12:48:17 +0530 | [diff] [blame] | 1381 | |
| 1382 | static void fastrpc_notify_users_staticpd_pdr(struct fastrpc_file *me) |
| 1383 | { |
| 1384 | struct smq_invoke_ctx *ictx; |
| 1385 | struct hlist_node *n; |
| 1386 | |
| 1387 | spin_lock(&me->hlock); |
| 1388 | hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) { |
| 1389 | if (ictx->msg.pid) |
| 1390 | complete(&ictx->work); |
| 1391 | } |
| 1392 | hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) { |
| 1393 | if (ictx->msg.pid) |
| 1394 | complete(&ictx->work); |
| 1395 | } |
| 1396 | spin_unlock(&me->hlock); |
| 1397 | } |
| 1398 | |
| 1399 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1400 | static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid) |
| 1401 | { |
| 1402 | struct fastrpc_file *fl; |
| 1403 | struct hlist_node *n; |
| 1404 | |
| 1405 | spin_lock(&me->hlock); |
| 1406 | hlist_for_each_entry_safe(fl, n, &me->drivers, hn) { |
| 1407 | if (fl->cid == cid) |
| 1408 | fastrpc_notify_users(fl); |
| 1409 | } |
| 1410 | spin_unlock(&me->hlock); |
| 1411 | |
| 1412 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 1413 | |
| 1414 | static void fastrpc_notify_pdr_drivers(struct fastrpc_apps *me, char *spdname) |
| 1415 | { |
| 1416 | struct fastrpc_file *fl; |
| 1417 | struct hlist_node *n; |
| 1418 | |
| 1419 | spin_lock(&me->hlock); |
| 1420 | hlist_for_each_entry_safe(fl, n, &me->drivers, hn) { |
| 1421 | if (fl->spdname && !strcmp(spdname, fl->spdname)) |
Tharun Kumar Merugu | 77dd587 | 2018-04-02 12:48:17 +0530 | [diff] [blame] | 1422 | fastrpc_notify_users_staticpd_pdr(fl); |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 1423 | } |
| 1424 | spin_unlock(&me->hlock); |
| 1425 | |
| 1426 | } |
| 1427 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1428 | static void context_list_ctor(struct fastrpc_ctx_lst *me) |
| 1429 | { |
| 1430 | INIT_HLIST_HEAD(&me->interrupted); |
| 1431 | INIT_HLIST_HEAD(&me->pending); |
| 1432 | } |
| 1433 | |
| 1434 | static void fastrpc_context_list_dtor(struct fastrpc_file *fl) |
| 1435 | { |
| 1436 | struct fastrpc_ctx_lst *clst = &fl->clst; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1437 | struct smq_invoke_ctx *ictx = NULL, *ctxfree; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1438 | struct hlist_node *n; |
| 1439 | |
| 1440 | do { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1441 | ctxfree = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1442 | spin_lock(&fl->hlock); |
| 1443 | hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) { |
| 1444 | hlist_del_init(&ictx->hn); |
| 1445 | ctxfree = ictx; |
| 1446 | break; |
| 1447 | } |
| 1448 | spin_unlock(&fl->hlock); |
| 1449 | if (ctxfree) |
| 1450 | context_free(ctxfree); |
| 1451 | } while (ctxfree); |
| 1452 | do { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1453 | ctxfree = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1454 | spin_lock(&fl->hlock); |
| 1455 | hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) { |
| 1456 | hlist_del_init(&ictx->hn); |
| 1457 | ctxfree = ictx; |
| 1458 | break; |
| 1459 | } |
| 1460 | spin_unlock(&fl->hlock); |
| 1461 | if (ctxfree) |
| 1462 | context_free(ctxfree); |
| 1463 | } while (ctxfree); |
| 1464 | } |
| 1465 | |
| 1466 | static int fastrpc_file_free(struct fastrpc_file *fl); |
| 1467 | static void fastrpc_file_list_dtor(struct fastrpc_apps *me) |
| 1468 | { |
| 1469 | struct fastrpc_file *fl, *free; |
| 1470 | struct hlist_node *n; |
| 1471 | |
| 1472 | do { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1473 | free = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1474 | spin_lock(&me->hlock); |
| 1475 | hlist_for_each_entry_safe(fl, n, &me->drivers, hn) { |
| 1476 | hlist_del_init(&fl->hn); |
| 1477 | free = fl; |
| 1478 | break; |
| 1479 | } |
| 1480 | spin_unlock(&me->hlock); |
| 1481 | if (free) |
| 1482 | fastrpc_file_free(free); |
| 1483 | } while (free); |
| 1484 | } |
| 1485 | |
| 1486 | static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx) |
| 1487 | { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1488 | struct fastrpc_apps *me = &gfa; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1489 | remote_arg64_t *rpra, *lrpra; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1490 | remote_arg_t *lpra = ctx->lpra; |
| 1491 | struct smq_invoke_buf *list; |
| 1492 | struct smq_phy_page *pages, *ipage; |
| 1493 | uint32_t sc = ctx->sc; |
| 1494 | int inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1495 | int outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1496 | int handles, bufs = inbufs + outbufs; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1497 | uintptr_t args; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1498 | size_t rlen = 0, copylen = 0, metalen = 0, lrpralen = 0; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1499 | int i, oix; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1500 | int err = 0; |
| 1501 | int mflags = 0; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1502 | uint64_t *fdlist; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1503 | uint32_t *crclist; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1504 | int64_t *perf_counter = getperfcounter(ctx->fl, PERF_COUNT); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1505 | |
| 1506 | /* calculate size of the metadata */ |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1507 | rpra = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1508 | list = smq_invoke_buf_start(rpra, sc); |
| 1509 | pages = smq_phy_page_start(sc, list); |
| 1510 | ipage = pages; |
| 1511 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1512 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1513 | for (i = 0; i < bufs; ++i) { |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1514 | uintptr_t buf = (uintptr_t)lpra[i].buf.pv; |
| 1515 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1516 | |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1517 | mutex_lock(&ctx->fl->fl_map_mutex); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1518 | if (ctx->fds[i] && (ctx->fds[i] != -1)) { |
| 1519 | unsigned int attrs = 0; |
| 1520 | |
| 1521 | if (ctx->attrs) |
| 1522 | attrs = ctx->attrs[i]; |
| 1523 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1524 | fastrpc_mmap_create(ctx->fl, ctx->fds[i], |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1525 | attrs, buf, len, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1526 | mflags, &ctx->maps[i]); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1527 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1528 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1529 | ipage += 1; |
| 1530 | } |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1531 | PERF_END); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1532 | handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1533 | mutex_lock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1534 | for (i = bufs; i < bufs + handles; i++) { |
Tharun Kumar Merugu | 35a94a5 | 2018-02-01 21:09:04 +0530 | [diff] [blame] | 1535 | int dmaflags = 0; |
| 1536 | |
| 1537 | if (ctx->attrs && (ctx->attrs[i] & FASTRPC_ATTR_NOMAP)) |
| 1538 | dmaflags = FASTRPC_DMAHANDLE_NOMAP; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1539 | VERIFY(err, !fastrpc_mmap_create(ctx->fl, ctx->fds[i], |
Tharun Kumar Merugu | 35a94a5 | 2018-02-01 21:09:04 +0530 | [diff] [blame] | 1540 | FASTRPC_ATTR_NOVA, 0, 0, dmaflags, &ctx->maps[i])); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1541 | if (err) { |
| 1542 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1543 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1544 | } |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1545 | ipage += 1; |
| 1546 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1547 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1548 | if (!me->legacy) { |
| 1549 | metalen = copylen = (size_t)&ipage[0] + |
| 1550 | (sizeof(uint64_t) * M_FDLIST) + |
| 1551 | (sizeof(uint32_t) * M_CRCLIST); |
| 1552 | } else { |
| 1553 | metalen = copylen = (size_t)&ipage[0]; |
| 1554 | } |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1555 | |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1556 | /* allocate new local rpra buffer */ |
| 1557 | lrpralen = (size_t)&list[0]; |
| 1558 | if (lrpralen) { |
| 1559 | err = fastrpc_buf_alloc(ctx->fl, lrpralen, 0, 0, 0, &ctx->lbuf); |
| 1560 | if (err) |
| 1561 | goto bail; |
| 1562 | } |
| 1563 | if (ctx->lbuf->virt) |
| 1564 | memset(ctx->lbuf->virt, 0, lrpralen); |
| 1565 | |
| 1566 | lrpra = ctx->lbuf->virt; |
| 1567 | ctx->lrpra = lrpra; |
| 1568 | |
| 1569 | /* calculate len required for copying */ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1570 | for (oix = 0; oix < inbufs + outbufs; ++oix) { |
| 1571 | int i = ctx->overps[oix]->raix; |
Sathish Ambley | d209c1e | 2016-12-13 15:27:30 -0800 | [diff] [blame] | 1572 | uintptr_t mstart, mend; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1573 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1574 | |
| 1575 | if (!len) |
| 1576 | continue; |
| 1577 | if (ctx->maps[i]) |
| 1578 | continue; |
| 1579 | if (ctx->overps[oix]->offset == 0) |
| 1580 | copylen = ALIGN(copylen, BALIGN); |
Sathish Ambley | d209c1e | 2016-12-13 15:27:30 -0800 | [diff] [blame] | 1581 | mstart = ctx->overps[oix]->mstart; |
| 1582 | mend = ctx->overps[oix]->mend; |
| 1583 | VERIFY(err, (mend - mstart) <= LONG_MAX); |
| 1584 | if (err) |
| 1585 | goto bail; |
| 1586 | copylen += mend - mstart; |
| 1587 | VERIFY(err, copylen >= 0); |
| 1588 | if (err) |
| 1589 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1590 | } |
| 1591 | ctx->used = copylen; |
| 1592 | |
| 1593 | /* allocate new buffer */ |
| 1594 | if (copylen) { |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 1595 | err = fastrpc_buf_alloc(ctx->fl, copylen, 0, 0, 0, &ctx->buf); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1596 | if (err) |
| 1597 | goto bail; |
| 1598 | } |
Tharun Kumar Merugu | e3361f9 | 2017-06-22 10:45:43 +0530 | [diff] [blame] | 1599 | if (ctx->buf->virt && metalen <= copylen) |
| 1600 | memset(ctx->buf->virt, 0, metalen); |
| 1601 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1602 | /* copy metadata */ |
| 1603 | rpra = ctx->buf->virt; |
| 1604 | ctx->rpra = rpra; |
| 1605 | list = smq_invoke_buf_start(rpra, sc); |
| 1606 | pages = smq_phy_page_start(sc, list); |
| 1607 | ipage = pages; |
| 1608 | args = (uintptr_t)ctx->buf->virt + metalen; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1609 | for (i = 0; i < bufs + handles; ++i) { |
| 1610 | if (lpra[i].buf.len) |
| 1611 | list[i].num = 1; |
| 1612 | else |
| 1613 | list[i].num = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1614 | list[i].pgidx = ipage - pages; |
| 1615 | ipage++; |
| 1616 | } |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 1617 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1618 | /* map ion buffers */ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1619 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP), |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1620 | for (i = 0; rpra && lrpra && i < inbufs + outbufs; ++i) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1621 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1622 | uint64_t buf = ptr_to_uint64(lpra[i].buf.pv); |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1623 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1624 | |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1625 | rpra[i].buf.pv = lrpra[i].buf.pv = 0; |
| 1626 | rpra[i].buf.len = lrpra[i].buf.len = len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1627 | if (!len) |
| 1628 | continue; |
| 1629 | if (map) { |
| 1630 | struct vm_area_struct *vma; |
| 1631 | uintptr_t offset; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1632 | uint64_t num = buf_num_pages(buf, len); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1633 | int idx = list[i].pgidx; |
| 1634 | |
| 1635 | if (map->attr & FASTRPC_ATTR_NOVA) { |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1636 | offset = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1637 | } else { |
| 1638 | down_read(¤t->mm->mmap_sem); |
| 1639 | VERIFY(err, NULL != (vma = find_vma(current->mm, |
| 1640 | map->va))); |
| 1641 | if (err) { |
| 1642 | up_read(¤t->mm->mmap_sem); |
| 1643 | goto bail; |
| 1644 | } |
| 1645 | offset = buf_page_start(buf) - vma->vm_start; |
| 1646 | up_read(¤t->mm->mmap_sem); |
| 1647 | VERIFY(err, offset < (uintptr_t)map->size); |
| 1648 | if (err) |
| 1649 | goto bail; |
| 1650 | } |
| 1651 | pages[idx].addr = map->phys + offset; |
| 1652 | pages[idx].size = num << PAGE_SHIFT; |
| 1653 | } |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1654 | rpra[i].buf.pv = lrpra[i].buf.pv = buf; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1655 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1656 | PERF_END); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1657 | for (i = bufs; i < bufs + handles; ++i) { |
| 1658 | struct fastrpc_mmap *map = ctx->maps[i]; |
Jeya R | 4c7abf2 | 2020-07-23 16:00:50 +0530 | [diff] [blame] | 1659 | if (map) { |
| 1660 | pages[i].addr = map->phys; |
| 1661 | pages[i].size = map->size; |
| 1662 | } |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1663 | } |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1664 | if (!me->legacy) { |
| 1665 | fdlist = (uint64_t *)&pages[bufs + handles]; |
| 1666 | for (i = 0; i < M_FDLIST; i++) |
| 1667 | fdlist[i] = 0; |
| 1668 | crclist = (uint32_t *)&fdlist[M_FDLIST]; |
| 1669 | memset(crclist, 0, sizeof(uint32_t)*M_CRCLIST); |
| 1670 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1671 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1672 | /* copy non ion buffers */ |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1673 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1674 | rlen = copylen - metalen; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1675 | for (oix = 0; rpra && lrpra && oix < inbufs + outbufs; ++oix) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1676 | int i = ctx->overps[oix]->raix; |
| 1677 | struct fastrpc_mmap *map = ctx->maps[i]; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1678 | size_t mlen; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1679 | uint64_t buf; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 1680 | size_t len = lpra[i].buf.len; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1681 | |
| 1682 | if (!len) |
| 1683 | continue; |
| 1684 | if (map) |
| 1685 | continue; |
| 1686 | if (ctx->overps[oix]->offset == 0) { |
| 1687 | rlen -= ALIGN(args, BALIGN) - args; |
| 1688 | args = ALIGN(args, BALIGN); |
| 1689 | } |
Sathish Ambley | d209c1e | 2016-12-13 15:27:30 -0800 | [diff] [blame] | 1690 | mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1691 | VERIFY(err, rlen >= mlen); |
| 1692 | if (err) |
| 1693 | goto bail; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1694 | rpra[i].buf.pv = lrpra[i].buf.pv = |
| 1695 | (args - ctx->overps[oix]->offset); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1696 | pages[list[i].pgidx].addr = ctx->buf->phys - |
| 1697 | ctx->overps[oix]->offset + |
| 1698 | (copylen - rlen); |
| 1699 | pages[list[i].pgidx].addr = |
| 1700 | buf_page_start(pages[list[i].pgidx].addr); |
| 1701 | buf = rpra[i].buf.pv; |
| 1702 | pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE; |
| 1703 | if (i < inbufs) { |
| 1704 | K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf), |
| 1705 | lpra[i].buf.pv, len); |
| 1706 | if (err) |
| 1707 | goto bail; |
| 1708 | } |
| 1709 | args = args + mlen; |
| 1710 | rlen -= mlen; |
| 1711 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1712 | PERF_END); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1713 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 1714 | PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1715 | for (oix = 0; oix < inbufs + outbufs; ++oix) { |
| 1716 | int i = ctx->overps[oix]->raix; |
| 1717 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1718 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1719 | if (map && map->uncached) |
| 1720 | continue; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 1721 | if (ctx->fl->sctx && ctx->fl->sctx->smmu.coherent && |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1722 | !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT))) |
| 1723 | continue; |
| 1724 | if (map && (map->attr & FASTRPC_ATTR_COHERENT)) |
| 1725 | continue; |
| 1726 | |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1727 | if (rpra && lrpra && rpra[i].buf.len && |
| 1728 | ctx->overps[oix]->mstart) { |
Tharun Kumar Merugu | b67336e | 2017-08-08 18:56:03 +0530 | [diff] [blame] | 1729 | if (map && map->handle) |
| 1730 | msm_ion_do_cache_op(ctx->fl->apps->client, |
| 1731 | map->handle, |
| 1732 | uint64_to_ptr(rpra[i].buf.pv), |
| 1733 | rpra[i].buf.len, |
| 1734 | ION_IOC_CLEAN_INV_CACHES); |
| 1735 | else |
| 1736 | dmac_flush_range(uint64_to_ptr(rpra[i].buf.pv), |
| 1737 | uint64_to_ptr(rpra[i].buf.pv |
| 1738 | + rpra[i].buf.len)); |
| 1739 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1740 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1741 | PERF_END); |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1742 | for (i = bufs; rpra && lrpra && i < bufs + handles; i++) { |
Jeya R | 4c7abf2 | 2020-07-23 16:00:50 +0530 | [diff] [blame] | 1743 | if (ctx->fds) |
| 1744 | rpra[i].dma.fd = lrpra[i].dma.fd = ctx->fds[i]; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1745 | rpra[i].dma.len = lrpra[i].dma.len = (uint32_t)lpra[i].buf.len; |
| 1746 | rpra[i].dma.offset = lrpra[i].dma.offset = |
| 1747 | (uint32_t)(uintptr_t)lpra[i].buf.pv; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1748 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 1749 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1750 | bail: |
| 1751 | return err; |
| 1752 | } |
| 1753 | |
| 1754 | static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx, |
| 1755 | remote_arg_t *upra) |
| 1756 | { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1757 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1758 | uint32_t sc = ctx->sc; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1759 | struct smq_invoke_buf *list; |
| 1760 | struct smq_phy_page *pages; |
| 1761 | struct fastrpc_mmap *mmap; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1762 | uint64_t *fdlist = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1763 | uint32_t *crclist = NULL; |
| 1764 | |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1765 | remote_arg64_t *rpra = ctx->lrpra; |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1766 | int i, inbufs, outbufs, handles; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1767 | int err = 0; |
| 1768 | |
| 1769 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1770 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1771 | handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc); |
| 1772 | list = smq_invoke_buf_start(ctx->rpra, sc); |
| 1773 | pages = smq_phy_page_start(sc, list); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1774 | if (!me->legacy) { |
| 1775 | fdlist = (uint64_t *)(pages + inbufs + outbufs + handles); |
| 1776 | crclist = (uint32_t *)(fdlist + M_FDLIST); |
| 1777 | } |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1778 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1779 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 1780 | if (!ctx->maps[i]) { |
| 1781 | K_COPY_TO_USER(err, kernel, |
| 1782 | ctx->lpra[i].buf.pv, |
| 1783 | uint64_to_ptr(rpra[i].buf.pv), |
| 1784 | rpra[i].buf.len); |
| 1785 | if (err) |
| 1786 | goto bail; |
| 1787 | } else { |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1788 | mutex_lock(&ctx->fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1789 | fastrpc_mmap_free(ctx->maps[i], 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1790 | mutex_unlock(&ctx->fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1791 | ctx->maps[i] = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1792 | } |
| 1793 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1794 | mutex_lock(&ctx->fl->fl_map_mutex); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1795 | if (fdlist && (inbufs + outbufs + handles)) { |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1796 | for (i = 0; i < M_FDLIST; i++) { |
| 1797 | if (!fdlist[i]) |
| 1798 | break; |
| 1799 | if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], 0, 0, |
Sathish Ambley | ae5ee54 | 2017-01-16 22:24:23 -0800 | [diff] [blame] | 1800 | 0, 0, &mmap)) |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 1801 | fastrpc_mmap_free(mmap, 0); |
Sathish Ambley | 58dc64d | 2016-11-29 17:11:53 -0800 | [diff] [blame] | 1802 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1803 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 1804 | mutex_unlock(&ctx->fl->fl_map_mutex); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1805 | if (ctx->crc && crclist && rpra) |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1806 | K_COPY_TO_USER(err, kernel, ctx->crc, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 1807 | crclist, M_CRCLIST*sizeof(uint32_t)); |
| 1808 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1809 | bail: |
| 1810 | return err; |
| 1811 | } |
| 1812 | |
| 1813 | static void inv_args_pre(struct smq_invoke_ctx *ctx) |
| 1814 | { |
| 1815 | int i, inbufs, outbufs; |
| 1816 | uint32_t sc = ctx->sc; |
| 1817 | remote_arg64_t *rpra = ctx->rpra; |
| 1818 | uintptr_t end; |
| 1819 | |
| 1820 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1821 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 1822 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 1823 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1824 | |
| 1825 | if (map && map->uncached) |
| 1826 | continue; |
| 1827 | if (!rpra[i].buf.len) |
| 1828 | continue; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 1829 | if (ctx->fl && ctx->fl->sctx && ctx->fl->sctx->smmu.coherent && |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1830 | !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT))) |
| 1831 | continue; |
| 1832 | if (map && (map->attr & FASTRPC_ATTR_COHERENT)) |
| 1833 | continue; |
| 1834 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1835 | if (buf_page_start(ptr_to_uint64((void *)rpra)) == |
| 1836 | buf_page_start(rpra[i].buf.pv)) |
| 1837 | continue; |
Tharun Kumar Merugu | b67336e | 2017-08-08 18:56:03 +0530 | [diff] [blame] | 1838 | if (!IS_CACHE_ALIGNED((uintptr_t) |
| 1839 | uint64_to_ptr(rpra[i].buf.pv))) { |
| 1840 | if (map && map->handle) |
| 1841 | msm_ion_do_cache_op(ctx->fl->apps->client, |
| 1842 | map->handle, |
| 1843 | uint64_to_ptr(rpra[i].buf.pv), |
| 1844 | sizeof(uintptr_t), |
| 1845 | ION_IOC_CLEAN_INV_CACHES); |
| 1846 | else |
| 1847 | dmac_flush_range( |
| 1848 | uint64_to_ptr(rpra[i].buf.pv), (char *) |
| 1849 | uint64_to_ptr(rpra[i].buf.pv + 1)); |
| 1850 | } |
| 1851 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1852 | end = (uintptr_t)uint64_to_ptr(rpra[i].buf.pv + |
| 1853 | rpra[i].buf.len); |
Tharun Kumar Merugu | b67336e | 2017-08-08 18:56:03 +0530 | [diff] [blame] | 1854 | if (!IS_CACHE_ALIGNED(end)) { |
| 1855 | if (map && map->handle) |
| 1856 | msm_ion_do_cache_op(ctx->fl->apps->client, |
| 1857 | map->handle, |
| 1858 | uint64_to_ptr(end), |
| 1859 | sizeof(uintptr_t), |
| 1860 | ION_IOC_CLEAN_INV_CACHES); |
| 1861 | else |
| 1862 | dmac_flush_range((char *)end, |
| 1863 | (char *)end + 1); |
| 1864 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | static void inv_args(struct smq_invoke_ctx *ctx) |
| 1869 | { |
| 1870 | int i, inbufs, outbufs; |
| 1871 | uint32_t sc = ctx->sc; |
Tharun Kumar Merugu | b31cc73 | 2019-05-07 00:39:43 +0530 | [diff] [blame] | 1872 | remote_arg64_t *rpra = ctx->lrpra; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1873 | |
| 1874 | inbufs = REMOTE_SCALARS_INBUFS(sc); |
| 1875 | outbufs = REMOTE_SCALARS_OUTBUFS(sc); |
| 1876 | for (i = inbufs; i < inbufs + outbufs; ++i) { |
| 1877 | struct fastrpc_mmap *map = ctx->maps[i]; |
| 1878 | |
| 1879 | if (map && map->uncached) |
| 1880 | continue; |
| 1881 | if (!rpra[i].buf.len) |
| 1882 | continue; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 1883 | if (ctx->fl && ctx->fl->sctx && ctx->fl->sctx->smmu.coherent && |
Tharun Kumar Merugu | 2e5f12e | 2017-07-06 12:04:40 +0530 | [diff] [blame] | 1884 | !(map && (map->attr & FASTRPC_ATTR_NON_COHERENT))) |
| 1885 | continue; |
| 1886 | if (map && (map->attr & FASTRPC_ATTR_COHERENT)) |
| 1887 | continue; |
| 1888 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1889 | if (buf_page_start(ptr_to_uint64((void *)rpra)) == |
| 1890 | buf_page_start(rpra[i].buf.pv)) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1891 | continue; |
| 1892 | } |
| 1893 | if (map && map->handle) |
| 1894 | msm_ion_do_cache_op(ctx->fl->apps->client, map->handle, |
| 1895 | (char *)uint64_to_ptr(rpra[i].buf.pv), |
| 1896 | rpra[i].buf.len, ION_IOC_INV_CACHES); |
| 1897 | else |
| 1898 | dmac_inv_range((char *)uint64_to_ptr(rpra[i].buf.pv), |
| 1899 | (char *)uint64_to_ptr(rpra[i].buf.pv |
| 1900 | + rpra[i].buf.len)); |
| 1901 | } |
| 1902 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx, |
| 1906 | uint32_t kernel, uint32_t handle) |
| 1907 | { |
| 1908 | struct smq_msg *msg = &ctx->msg; |
| 1909 | struct fastrpc_file *fl = ctx->fl; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 1910 | int err = 0, len, cid = -1; |
| 1911 | struct fastrpc_channel_ctx *channel_ctx = NULL; |
| 1912 | |
| 1913 | cid = fl->cid; |
| 1914 | VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS); |
| 1915 | if (err) { |
| 1916 | err = -ECHRNG; |
| 1917 | goto bail; |
| 1918 | } |
| 1919 | channel_ctx = &fl->apps->channel[fl->cid]; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1920 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 1921 | VERIFY(err, NULL != channel_ctx->chan); |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 1922 | if (err) { |
| 1923 | err = -ECHRNG; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1924 | goto bail; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 1925 | } |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1926 | msg->pid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1927 | msg->tid = current->pid; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 1928 | if (fl->sessionid) |
| 1929 | msg->tid |= (1 << SESSION_ID_INDEX); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1930 | if (kernel) |
| 1931 | msg->pid = 0; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1932 | msg->invoke.header.ctx = ctx->ctxid | fl->pd; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1933 | msg->invoke.header.handle = handle; |
| 1934 | msg->invoke.header.sc = ctx->sc; |
| 1935 | msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0; |
| 1936 | msg->invoke.page.size = buf_page_size(ctx->used); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1937 | if (fl->apps->glink) { |
| 1938 | if (fl->ssrcount != channel_ctx->ssrcount) { |
| 1939 | err = -ECONNRESET; |
| 1940 | goto bail; |
| 1941 | } |
| 1942 | VERIFY(err, channel_ctx->link.port_state == |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1943 | FASTRPC_LINK_CONNECTED); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1944 | if (err) |
| 1945 | goto bail; |
| 1946 | err = glink_tx(channel_ctx->chan, |
| 1947 | (void *)&fl->apps->channel[fl->cid], msg, sizeof(*msg), |
| 1948 | GLINK_TX_REQ_INTENT); |
| 1949 | } else { |
| 1950 | spin_lock(&fl->apps->hlock); |
| 1951 | len = smd_write((smd_channel_t *) |
| 1952 | channel_ctx->chan, |
| 1953 | msg, sizeof(*msg)); |
| 1954 | spin_unlock(&fl->apps->hlock); |
| 1955 | VERIFY(err, len == sizeof(*msg)); |
| 1956 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 1957 | bail: |
| 1958 | return err; |
| 1959 | } |
| 1960 | |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1961 | static void fastrpc_smd_read_handler(int cid) |
| 1962 | { |
| 1963 | struct fastrpc_apps *me = &gfa; |
| 1964 | struct smq_invoke_rsp rsp = {0}; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1965 | int ret = 0, err = 0; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1966 | uint32_t index; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1967 | |
| 1968 | do { |
| 1969 | ret = smd_read_from_cb(me->channel[cid].chan, &rsp, |
| 1970 | sizeof(rsp)); |
| 1971 | if (ret != sizeof(rsp)) |
| 1972 | break; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1973 | |
| 1974 | index = (uint32_t)((rsp.ctx & FASTRPC_CTXID_MASK) >> 4); |
| 1975 | VERIFY(err, index < FASTRPC_CTX_MAX); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1976 | if (err) |
| 1977 | goto bail; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1978 | |
| 1979 | VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index])); |
| 1980 | if (err) |
| 1981 | goto bail; |
| 1982 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 1983 | VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp.ctx & ~3)) && |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 1984 | me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC)); |
| 1985 | if (err) |
| 1986 | goto bail; |
| 1987 | |
| 1988 | context_notify_user(me->ctxtable[index], rsp.retval); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 1989 | } while (ret == sizeof(rsp)); |
| 1990 | bail: |
| 1991 | if (err) |
| 1992 | pr_err("adsprpc: invalid response or context\n"); |
| 1993 | |
| 1994 | } |
| 1995 | |
| 1996 | static void smd_event_handler(void *priv, unsigned int event) |
| 1997 | { |
| 1998 | struct fastrpc_apps *me = &gfa; |
| 1999 | int cid = (int)(uintptr_t)priv; |
| 2000 | |
| 2001 | switch (event) { |
| 2002 | case SMD_EVENT_OPEN: |
| 2003 | complete(&me->channel[cid].workport); |
| 2004 | break; |
| 2005 | case SMD_EVENT_CLOSE: |
| 2006 | fastrpc_notify_drivers(me, cid); |
| 2007 | break; |
| 2008 | case SMD_EVENT_DATA: |
| 2009 | fastrpc_smd_read_handler(cid); |
| 2010 | break; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2015 | static void fastrpc_init(struct fastrpc_apps *me) |
| 2016 | { |
| 2017 | int i; |
| 2018 | |
| 2019 | INIT_HLIST_HEAD(&me->drivers); |
Tharun Kumar Merugu | bcd6fbf | 2018-01-04 17:49:34 +0530 | [diff] [blame] | 2020 | INIT_HLIST_HEAD(&me->maps); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2021 | spin_lock_init(&me->hlock); |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2022 | spin_lock_init(&me->ctxlock); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2023 | mutex_init(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2024 | me->channel = &gcinfo[0]; |
| 2025 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 2026 | init_completion(&me->channel[i].work); |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 2027 | init_completion(&me->channel[i].workport); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2028 | me->channel[i].sesscount = 0; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 2029 | /* All channels are secure by default except CDSP */ |
| 2030 | me->channel[i].secure = SECURE_CHANNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2031 | } |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 2032 | /* Set CDSP channel to non secure */ |
| 2033 | me->channel[CDSP_DOMAIN_ID].secure = NON_SECURE_CHANNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl); |
| 2037 | |
| 2038 | static int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode, |
| 2039 | uint32_t kernel, |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2040 | struct fastrpc_ioctl_invoke_crc *inv) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2041 | { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2042 | struct smq_invoke_ctx *ctx = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2043 | struct fastrpc_ioctl_invoke *invoke = &inv->inv; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 2044 | int err = 0, cid = -1, interrupted = 0; |
Maria Yu | 757199c | 2017-09-22 16:05:49 +0800 | [diff] [blame] | 2045 | struct timespec invoket = {0}; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 2046 | int64_t *perf_counter = NULL; |
| 2047 | |
| 2048 | cid = fl->cid; |
| 2049 | VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS); |
| 2050 | if (err) { |
| 2051 | err = -ECHRNG; |
| 2052 | goto bail; |
| 2053 | } |
| 2054 | VERIFY(err, fl->sctx != NULL); |
| 2055 | if (err) { |
| 2056 | err = -EBADR; |
| 2057 | goto bail; |
| 2058 | } |
| 2059 | perf_counter = getperfcounter(fl, PERF_COUNT); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2060 | |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2061 | if (fl->profile) |
| 2062 | getnstimeofday(&invoket); |
Tharun Kumar Merugu | e3edf3e | 2017-07-27 12:34:07 +0530 | [diff] [blame] | 2063 | |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2064 | if (!kernel) { |
| 2065 | VERIFY(err, invoke->handle != FASTRPC_STATIC_HANDLE_KERNEL); |
| 2066 | if (err) { |
| 2067 | pr_err("adsprpc: ERROR: %s: user application %s trying to send a kernel RPC message to channel %d", |
| 2068 | __func__, current->comm, cid); |
| 2069 | goto bail; |
| 2070 | } |
| 2071 | } |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2072 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2073 | if (!kernel) { |
| 2074 | VERIFY(err, 0 == context_restore_interrupted(fl, inv, |
| 2075 | &ctx)); |
| 2076 | if (err) |
| 2077 | goto bail; |
| 2078 | if (fl->sctx->smmu.faults) |
| 2079 | err = FASTRPC_ENOSUCH; |
| 2080 | if (err) |
| 2081 | goto bail; |
| 2082 | if (ctx) |
| 2083 | goto wait; |
| 2084 | } |
| 2085 | |
| 2086 | VERIFY(err, 0 == context_alloc(fl, kernel, inv, &ctx)); |
| 2087 | if (err) |
| 2088 | goto bail; |
| 2089 | |
| 2090 | if (REMOTE_SCALARS_LENGTH(ctx->sc)) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2091 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2092 | VERIFY(err, 0 == get_args(kernel, ctx)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2093 | PERF_END); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2094 | if (err) |
| 2095 | goto bail; |
| 2096 | } |
| 2097 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2098 | if (!fl->sctx->smmu.coherent) { |
| 2099 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2100 | inv_args_pre(ctx); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2101 | PERF_END); |
| 2102 | } |
| 2103 | |
| 2104 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2105 | VERIFY(err, 0 == fastrpc_invoke_send(ctx, kernel, invoke->handle)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2106 | PERF_END); |
| 2107 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2108 | if (err) |
| 2109 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2110 | wait: |
| 2111 | if (kernel) |
| 2112 | wait_for_completion(&ctx->work); |
| 2113 | else { |
| 2114 | interrupted = wait_for_completion_interruptible(&ctx->work); |
| 2115 | VERIFY(err, 0 == (err = interrupted)); |
| 2116 | if (err) |
| 2117 | goto bail; |
| 2118 | } |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2119 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS), |
Sathish Ambley | c432b50 | 2017-06-05 12:03:42 -0700 | [diff] [blame] | 2120 | if (!fl->sctx->smmu.coherent) |
| 2121 | inv_args(ctx); |
| 2122 | PERF_END); |
| 2123 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2124 | VERIFY(err, 0 == (err = ctx->retval)); |
| 2125 | if (err) |
| 2126 | goto bail; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2127 | |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2128 | PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS), |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2129 | VERIFY(err, 0 == put_args(kernel, ctx, invoke->pra)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2130 | PERF_END); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2131 | if (err) |
| 2132 | goto bail; |
| 2133 | bail: |
| 2134 | if (ctx && interrupted == -ERESTARTSYS) |
| 2135 | context_save_interrupted(ctx); |
| 2136 | else if (ctx) |
| 2137 | context_free(ctx); |
| 2138 | if (fl->ssrcount != fl->apps->channel[cid].ssrcount) |
| 2139 | err = ECONNRESET; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2140 | |
| 2141 | if (fl->profile && !interrupted) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2142 | if (invoke->handle != FASTRPC_STATIC_HANDLE_LISTENER) { |
| 2143 | int64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE); |
| 2144 | |
| 2145 | if (count) |
| 2146 | *count += getnstimediff(&invoket); |
| 2147 | } |
| 2148 | if (invoke->handle > FASTRPC_STATIC_HANDLE_MAX) { |
| 2149 | int64_t *count = GET_COUNTER(perf_counter, PERF_COUNT); |
| 2150 | |
| 2151 | if (count) |
| 2152 | *count = *count+1; |
| 2153 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 2154 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2155 | return err; |
| 2156 | } |
| 2157 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 2158 | static int fastrpc_get_adsp_session(char *name, int *session) |
| 2159 | { |
| 2160 | struct fastrpc_apps *me = &gfa; |
| 2161 | int err = 0, i; |
| 2162 | |
| 2163 | for (i = 0; i < NUM_SESSIONS; i++) { |
| 2164 | if (!me->channel[0].spd[i].spdname) |
| 2165 | continue; |
| 2166 | if (!strcmp(name, me->channel[0].spd[i].spdname)) |
| 2167 | break; |
| 2168 | } |
| 2169 | VERIFY(err, i < NUM_SESSIONS); |
| 2170 | if (err) |
| 2171 | goto bail; |
| 2172 | *session = i; |
| 2173 | bail: |
| 2174 | return err; |
| 2175 | } |
| 2176 | |
| 2177 | static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2178 | static int fastrpc_channel_open(struct fastrpc_file *fl); |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 2179 | static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2180 | static int fastrpc_init_process(struct fastrpc_file *fl, |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2181 | struct fastrpc_ioctl_init_attrs *uproc) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2182 | { |
| 2183 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2184 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2185 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2186 | struct fastrpc_ioctl_init *init = &uproc->init; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2187 | struct smq_phy_page pages[1]; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2188 | struct fastrpc_mmap *file = NULL, *mem = NULL; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2189 | struct fastrpc_buf *imem = NULL; |
| 2190 | unsigned long imem_dma_attr = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2191 | char *proc_name = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2192 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2193 | VERIFY(err, 0 == (err = fastrpc_channel_open(fl))); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2194 | if (err) |
| 2195 | goto bail; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2196 | if (init->flags == FASTRPC_INIT_ATTACH || |
| 2197 | init->flags == FASTRPC_INIT_ATTACH_SENSORS) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2198 | remote_arg_t ra[1]; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2199 | int tgid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2200 | |
| 2201 | ra[0].buf.pv = (void *)&tgid; |
| 2202 | ra[0].buf.len = sizeof(tgid); |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2203 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2204 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0); |
| 2205 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2206 | ioctl.fds = NULL; |
| 2207 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2208 | ioctl.crc = NULL; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2209 | if (init->flags == FASTRPC_INIT_ATTACH) |
| 2210 | fl->pd = 0; |
| 2211 | else if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) { |
| 2212 | fl->spdname = SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME; |
| 2213 | fl->pd = 2; |
| 2214 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2215 | VERIFY(err, !(err = fastrpc_internal_invoke(fl, |
| 2216 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2217 | if (err) |
| 2218 | goto bail; |
| 2219 | } else if (init->flags == FASTRPC_INIT_CREATE) { |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2220 | remote_arg_t ra[6]; |
| 2221 | int fds[6]; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2222 | int mflags = 0; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2223 | int memlen; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2224 | struct { |
| 2225 | int pgid; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2226 | unsigned int namelen; |
| 2227 | unsigned int filelen; |
| 2228 | unsigned int pageslen; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2229 | int attrs; |
| 2230 | int siglen; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2231 | } inbuf; |
| 2232 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2233 | inbuf.pgid = fl->tgid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2234 | inbuf.namelen = strlen(current->comm) + 1; |
| 2235 | inbuf.filelen = init->filelen; |
| 2236 | fl->pd = 1; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2237 | |
Tharun Kumar Merugu | df85289 | 2017-12-07 16:27:37 +0530 | [diff] [blame] | 2238 | VERIFY(err, access_ok(0, (void __user *)init->file, |
| 2239 | init->filelen)); |
| 2240 | if (err) |
| 2241 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2242 | if (init->filelen) { |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2243 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2244 | VERIFY(err, !fastrpc_mmap_create(fl, init->filefd, 0, |
| 2245 | init->file, init->filelen, mflags, &file)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2246 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2247 | if (err) |
| 2248 | goto bail; |
| 2249 | } |
| 2250 | inbuf.pageslen = 1; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2251 | |
| 2252 | VERIFY(err, !init->mem); |
| 2253 | if (err) { |
| 2254 | err = -EINVAL; |
| 2255 | pr_err("adsprpc: %s: %s: ERROR: donated memory allocated in userspace\n", |
| 2256 | current->comm, __func__); |
| 2257 | goto bail; |
| 2258 | } |
| 2259 | memlen = ALIGN(max(1024*1024*3, (int)init->filelen * 4), |
| 2260 | 1024*1024); |
| 2261 | imem_dma_attr = DMA_ATTR_EXEC_MAPPING | |
| 2262 | DMA_ATTR_NO_KERNEL_MAPPING | |
| 2263 | DMA_ATTR_FORCE_NON_COHERENT; |
| 2264 | err = fastrpc_buf_alloc(fl, memlen, imem_dma_attr, 0, 0, &imem); |
Tharun Kumar Merugu | df85289 | 2017-12-07 16:27:37 +0530 | [diff] [blame] | 2265 | if (err) |
| 2266 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2267 | fl->init_mem = imem; |
| 2268 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2269 | inbuf.pageslen = 1; |
| 2270 | ra[0].buf.pv = (void *)&inbuf; |
| 2271 | ra[0].buf.len = sizeof(inbuf); |
| 2272 | fds[0] = 0; |
| 2273 | |
| 2274 | ra[1].buf.pv = (void *)current->comm; |
| 2275 | ra[1].buf.len = inbuf.namelen; |
| 2276 | fds[1] = 0; |
| 2277 | |
| 2278 | ra[2].buf.pv = (void *)init->file; |
| 2279 | ra[2].buf.len = inbuf.filelen; |
| 2280 | fds[2] = init->filefd; |
| 2281 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2282 | pages[0].addr = imem->phys; |
| 2283 | pages[0].size = imem->size; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2284 | ra[3].buf.pv = (void *)pages; |
| 2285 | ra[3].buf.len = 1 * sizeof(*pages); |
| 2286 | fds[3] = 0; |
| 2287 | |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2288 | inbuf.attrs = uproc->attrs; |
| 2289 | ra[4].buf.pv = (void *)&(inbuf.attrs); |
| 2290 | ra[4].buf.len = sizeof(inbuf.attrs); |
| 2291 | fds[4] = 0; |
| 2292 | |
| 2293 | inbuf.siglen = uproc->siglen; |
| 2294 | ra[5].buf.pv = (void *)&(inbuf.siglen); |
| 2295 | ra[5].buf.len = sizeof(inbuf.siglen); |
| 2296 | fds[5] = 0; |
| 2297 | |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2298 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2299 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0); |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 2300 | if (uproc->attrs) |
| 2301 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 6, 0); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2302 | ioctl.inv.pra = ra; |
| 2303 | ioctl.fds = fds; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2304 | ioctl.attrs = NULL; |
| 2305 | ioctl.crc = NULL; |
| 2306 | VERIFY(err, !(err = fastrpc_internal_invoke(fl, |
| 2307 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2308 | if (err) |
| 2309 | goto bail; |
| 2310 | } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) { |
| 2311 | remote_arg_t ra[3]; |
| 2312 | uint64_t phys = 0; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2313 | size_t size = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2314 | int fds[3]; |
| 2315 | struct { |
| 2316 | int pgid; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2317 | unsigned int namelen; |
| 2318 | unsigned int pageslen; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2319 | } inbuf; |
| 2320 | |
| 2321 | if (!init->filelen) |
| 2322 | goto bail; |
| 2323 | |
| 2324 | proc_name = kzalloc(init->filelen, GFP_KERNEL); |
| 2325 | VERIFY(err, !IS_ERR_OR_NULL(proc_name)); |
| 2326 | if (err) |
| 2327 | goto bail; |
| 2328 | VERIFY(err, 0 == copy_from_user((void *)proc_name, |
| 2329 | (void __user *)init->file, init->filelen)); |
| 2330 | if (err) |
| 2331 | goto bail; |
| 2332 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2333 | fl->pd = 1; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2334 | inbuf.pgid = current->tgid; |
c_mtharu | 81a0aa7 | 2017-11-07 16:13:21 +0530 | [diff] [blame] | 2335 | inbuf.namelen = init->filelen; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2336 | inbuf.pageslen = 0; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 2337 | |
| 2338 | if (!strcmp(proc_name, "audiopd")) { |
| 2339 | fl->spdname = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME; |
| 2340 | VERIFY(err, !fastrpc_mmap_remove_pdr(fl)); |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 2341 | if (err) |
| 2342 | goto bail; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 2343 | } |
| 2344 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2345 | if (!me->staticpd_flags) { |
| 2346 | inbuf.pageslen = 1; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2347 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2348 | VERIFY(err, !fastrpc_mmap_create(fl, -1, 0, init->mem, |
| 2349 | init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR, |
| 2350 | &mem)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2351 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2352 | if (err) |
| 2353 | goto bail; |
| 2354 | phys = mem->phys; |
| 2355 | size = mem->size; |
| 2356 | VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2357 | hlosvm, 1, me->channel[fl->cid].rhvm.vmid, |
| 2358 | me->channel[fl->cid].rhvm.vmperm, |
| 2359 | me->channel[fl->cid].rhvm.vmcount)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2360 | if (err) { |
| 2361 | pr_err("ADSPRPC: hyp_assign_phys fail err %d", |
| 2362 | err); |
| 2363 | pr_err("map->phys %llx, map->size %d\n", |
| 2364 | phys, (int)size); |
| 2365 | goto bail; |
| 2366 | } |
| 2367 | me->staticpd_flags = 1; |
| 2368 | } |
| 2369 | |
| 2370 | ra[0].buf.pv = (void *)&inbuf; |
| 2371 | ra[0].buf.len = sizeof(inbuf); |
| 2372 | fds[0] = 0; |
| 2373 | |
| 2374 | ra[1].buf.pv = (void *)proc_name; |
| 2375 | ra[1].buf.len = inbuf.namelen; |
| 2376 | fds[1] = 0; |
| 2377 | |
| 2378 | pages[0].addr = phys; |
| 2379 | pages[0].size = size; |
| 2380 | |
| 2381 | ra[2].buf.pv = (void *)pages; |
| 2382 | ra[2].buf.len = sizeof(*pages); |
| 2383 | fds[2] = 0; |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2384 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2385 | |
| 2386 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0); |
| 2387 | ioctl.inv.pra = ra; |
| 2388 | ioctl.fds = NULL; |
| 2389 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2390 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2391 | VERIFY(err, !(err = fastrpc_internal_invoke(fl, |
| 2392 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2393 | if (err) |
| 2394 | goto bail; |
| 2395 | } else { |
| 2396 | err = -ENOTTY; |
| 2397 | } |
| 2398 | bail: |
c_mtharu | d91205a | 2017-11-07 16:01:06 +0530 | [diff] [blame] | 2399 | kfree(proc_name); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2400 | if (err && (init->flags == FASTRPC_INIT_CREATE_STATIC)) |
| 2401 | me->staticpd_flags = 0; |
| 2402 | if (mem && err) { |
| 2403 | if (mem->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) |
| 2404 | hyp_assign_phys(mem->phys, (uint64_t)mem->size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2405 | me->channel[fl->cid].rhvm.vmid, |
| 2406 | me->channel[fl->cid].rhvm.vmcount, |
| 2407 | hlosvm, hlosvmperm, 1); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2408 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2409 | fastrpc_mmap_free(mem, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2410 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2411 | } |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2412 | if (file) { |
| 2413 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2414 | fastrpc_mmap_free(file, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2415 | mutex_unlock(&fl->fl_map_mutex); |
| 2416 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2417 | return err; |
| 2418 | } |
| 2419 | |
| 2420 | static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl) |
| 2421 | { |
| 2422 | int err = 0; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2423 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2424 | remote_arg_t ra[1]; |
| 2425 | int tgid = 0; |
| 2426 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 2427 | VERIFY(err, fl->cid >= 0 && fl->cid < NUM_CHANNELS); |
| 2428 | if (err) |
| 2429 | goto bail; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 2430 | VERIFY(err, fl->sctx != NULL); |
| 2431 | if (err) |
| 2432 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2433 | VERIFY(err, fl->apps->channel[fl->cid].chan != NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2434 | if (err) |
| 2435 | goto bail; |
| 2436 | tgid = fl->tgid; |
| 2437 | ra[0].buf.pv = (void *)&tgid; |
| 2438 | ra[0].buf.len = sizeof(tgid); |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2439 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2440 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0); |
| 2441 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2442 | ioctl.fds = NULL; |
| 2443 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2444 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2445 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2446 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2447 | bail: |
| 2448 | return err; |
| 2449 | } |
| 2450 | |
| 2451 | static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2452 | uintptr_t va, uint64_t phys, |
| 2453 | size_t size, uintptr_t *raddr) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2454 | { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2455 | struct fastrpc_ioctl_invoke_crc ioctl; |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 2456 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2457 | struct smq_phy_page page; |
| 2458 | int num = 1; |
| 2459 | remote_arg_t ra[3]; |
| 2460 | int err = 0; |
| 2461 | struct { |
| 2462 | int pid; |
| 2463 | uint32_t flags; |
| 2464 | uintptr_t vaddrin; |
| 2465 | int num; |
| 2466 | } inargs; |
| 2467 | struct { |
| 2468 | uintptr_t vaddrout; |
| 2469 | } routargs; |
| 2470 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2471 | inargs.pid = fl->tgid; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2472 | inargs.vaddrin = (uintptr_t)va; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2473 | inargs.flags = flags; |
| 2474 | inargs.num = fl->apps->compat ? num * sizeof(page) : num; |
| 2475 | ra[0].buf.pv = (void *)&inargs; |
| 2476 | ra[0].buf.len = sizeof(inargs); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2477 | page.addr = phys; |
| 2478 | page.size = size; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2479 | ra[1].buf.pv = (void *)&page; |
| 2480 | ra[1].buf.len = num * sizeof(page); |
| 2481 | |
| 2482 | ra[2].buf.pv = (void *)&routargs; |
| 2483 | ra[2].buf.len = sizeof(routargs); |
| 2484 | |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2485 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2486 | if (fl->apps->compat) |
| 2487 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1); |
| 2488 | else |
| 2489 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1); |
| 2490 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2491 | ioctl.fds = NULL; |
| 2492 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2493 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2494 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2495 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2496 | *raddr = (uintptr_t)routargs.vaddrout; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2497 | if (err) |
| 2498 | goto bail; |
| 2499 | if (flags == ADSP_MMAP_HEAP_ADDR) { |
| 2500 | struct scm_desc desc = {0}; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2501 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2502 | desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2503 | desc.args[1] = phys; |
| 2504 | desc.args[2] = size; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2505 | desc.arginfo = SCM_ARGS(3); |
| 2506 | err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL, |
| 2507 | TZ_PIL_PROTECT_MEM_SUBSYS_ID), &desc); |
| 2508 | } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2509 | VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2510 | hlosvm, 1, me->channel[fl->cid].rhvm.vmid, |
| 2511 | me->channel[fl->cid].rhvm.vmperm, |
| 2512 | me->channel[fl->cid].rhvm.vmcount)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2513 | if (err) |
| 2514 | goto bail; |
| 2515 | } |
| 2516 | bail: |
| 2517 | return err; |
| 2518 | } |
| 2519 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2520 | static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, uint64_t phys, |
| 2521 | size_t size, uint32_t flags) |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2522 | { |
| 2523 | int err = 0; |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 2524 | struct fastrpc_apps *me = &gfa; |
Tharun Kumar Merugu | 72c9025 | 2019-08-29 18:36:08 +0530 | [diff] [blame] | 2525 | int tgid = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2526 | int destVM[1] = {VMID_HLOS}; |
| 2527 | int destVMperm[1] = {PERM_READ | PERM_WRITE | PERM_EXEC}; |
| 2528 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2529 | if (flags == ADSP_MMAP_HEAP_ADDR) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2530 | struct fastrpc_ioctl_invoke_crc ioctl; |
| 2531 | struct scm_desc desc = {0}; |
Tharun Kumar Merugu | 72c9025 | 2019-08-29 18:36:08 +0530 | [diff] [blame] | 2532 | remote_arg_t ra[2]; |
| 2533 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2534 | struct { |
| 2535 | uint8_t skey; |
| 2536 | } routargs; |
| 2537 | |
Tharun Kumar Merugu | 72c9025 | 2019-08-29 18:36:08 +0530 | [diff] [blame] | 2538 | if (fl == NULL) |
| 2539 | goto bail; |
| 2540 | tgid = fl->tgid; |
| 2541 | ra[0].buf.pv = (void *)&tgid; |
| 2542 | ra[0].buf.len = sizeof(tgid); |
| 2543 | ra[1].buf.pv = (void *)&routargs; |
| 2544 | ra[1].buf.len = sizeof(routargs); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2545 | |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2546 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Tharun Kumar Merugu | 72c9025 | 2019-08-29 18:36:08 +0530 | [diff] [blame] | 2547 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(9, 1, 1); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2548 | ioctl.inv.pra = ra; |
| 2549 | ioctl.fds = NULL; |
| 2550 | ioctl.attrs = NULL; |
| 2551 | ioctl.crc = NULL; |
Tharun Kumar Merugu | 72c9025 | 2019-08-29 18:36:08 +0530 | [diff] [blame] | 2552 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2553 | |
| 2554 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2555 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
Mohammed Nayeem Ur Rahman | 80f45dc | 2019-09-23 19:35:19 +0530 | [diff] [blame] | 2556 | if (err == AEE_EUNSUPPORTED) { |
| 2557 | remote_arg_t ra[1]; |
| 2558 | |
| 2559 | pr_warn("ADSPRPC:Failed to get security key with updated remote call, falling back to older method"); |
| 2560 | ra[0].buf.pv = (void *)&routargs; |
| 2561 | ra[0].buf.len = sizeof(routargs); |
| 2562 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1); |
| 2563 | ioctl.inv.pra = ra; |
| 2564 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2565 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2566 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2567 | if (err) |
| 2568 | goto bail; |
Mohammed Nayeem Ur Rahman | 80f45dc | 2019-09-23 19:35:19 +0530 | [diff] [blame] | 2569 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2570 | desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2571 | desc.args[1] = phys; |
| 2572 | desc.args[2] = size; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2573 | desc.args[3] = routargs.skey; |
| 2574 | desc.arginfo = SCM_ARGS(4); |
| 2575 | err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL, |
| 2576 | TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2577 | } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 2578 | VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2579 | me->channel[fl->cid].rhvm.vmid, |
| 2580 | me->channel[fl->cid].rhvm.vmcount, |
| 2581 | destVM, destVMperm, 1)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2582 | if (err) |
| 2583 | goto bail; |
| 2584 | } |
| 2585 | |
| 2586 | bail: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2587 | return err; |
| 2588 | } |
| 2589 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2590 | static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr, |
| 2591 | uint64_t phys, size_t size, uint32_t flags) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2592 | { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2593 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2594 | remote_arg_t ra[1]; |
| 2595 | int err = 0; |
| 2596 | struct { |
| 2597 | int pid; |
| 2598 | uintptr_t vaddrout; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2599 | size_t size; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2600 | } inargs; |
| 2601 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2602 | inargs.pid = fl->tgid; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2603 | inargs.size = size; |
| 2604 | inargs.vaddrout = raddr; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2605 | ra[0].buf.pv = (void *)&inargs; |
| 2606 | ra[0].buf.len = sizeof(inargs); |
| 2607 | |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2608 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2609 | if (fl->apps->compat) |
| 2610 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0); |
| 2611 | else |
| 2612 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0); |
| 2613 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2614 | ioctl.fds = NULL; |
| 2615 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2616 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2617 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2618 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2619 | if (err) |
| 2620 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2621 | if (flags == ADSP_MMAP_HEAP_ADDR || |
| 2622 | flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 2623 | VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, phys, size, flags)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2624 | if (err) |
| 2625 | goto bail; |
| 2626 | } |
| 2627 | bail: |
| 2628 | return err; |
| 2629 | } |
| 2630 | |
| 2631 | static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl) |
| 2632 | { |
| 2633 | struct fastrpc_mmap *match = NULL, *map = NULL; |
| 2634 | struct hlist_node *n = NULL; |
| 2635 | int err = 0, ret = 0; |
| 2636 | struct fastrpc_apps *me = &gfa; |
| 2637 | struct ramdump_segment *ramdump_segments_rh = NULL; |
| 2638 | |
| 2639 | do { |
| 2640 | match = NULL; |
| 2641 | spin_lock(&me->hlock); |
| 2642 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 2643 | match = map; |
| 2644 | hlist_del_init(&map->hn); |
| 2645 | break; |
| 2646 | } |
| 2647 | spin_unlock(&me->hlock); |
| 2648 | |
| 2649 | if (match) { |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2650 | VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match->phys, |
| 2651 | match->size, match->flags)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2652 | if (err) |
| 2653 | goto bail; |
| 2654 | if (me->channel[0].ramdumpenabled) { |
| 2655 | ramdump_segments_rh = kcalloc(1, |
| 2656 | sizeof(struct ramdump_segment), GFP_KERNEL); |
| 2657 | if (ramdump_segments_rh) { |
| 2658 | ramdump_segments_rh->address = |
| 2659 | match->phys; |
| 2660 | ramdump_segments_rh->size = match->size; |
| 2661 | ret = do_elf_ramdump( |
| 2662 | me->channel[0].remoteheap_ramdump_dev, |
| 2663 | ramdump_segments_rh, 1); |
| 2664 | if (ret < 0) |
| 2665 | pr_err("ADSPRPC: unable to dump heap"); |
| 2666 | kfree(ramdump_segments_rh); |
| 2667 | } |
| 2668 | } |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2669 | fastrpc_mmap_free(match, 0); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2670 | } |
| 2671 | } while (match); |
| 2672 | bail: |
| 2673 | if (err && match) |
| 2674 | fastrpc_mmap_add(match); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2675 | return err; |
| 2676 | } |
| 2677 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 2678 | static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl) |
| 2679 | { |
| 2680 | struct fastrpc_apps *me = &gfa; |
| 2681 | int session = 0, err = 0; |
| 2682 | |
| 2683 | VERIFY(err, !fastrpc_get_adsp_session( |
| 2684 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session)); |
| 2685 | if (err) |
| 2686 | goto bail; |
| 2687 | if (me->channel[fl->cid].spd[session].pdrcount != |
| 2688 | me->channel[fl->cid].spd[session].prevpdrcount) { |
| 2689 | if (fastrpc_mmap_remove_ssr(fl)) |
| 2690 | pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n"); |
| 2691 | me->channel[fl->cid].spd[session].prevpdrcount = |
| 2692 | me->channel[fl->cid].spd[session].pdrcount; |
| 2693 | } |
| 2694 | if (!me->channel[fl->cid].spd[session].ispdup) { |
| 2695 | VERIFY(err, 0); |
| 2696 | if (err) { |
| 2697 | err = -ENOTCONN; |
| 2698 | goto bail; |
| 2699 | } |
| 2700 | } |
| 2701 | bail: |
| 2702 | return err; |
| 2703 | } |
| 2704 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2705 | static int fastrpc_mmap_remove(struct fastrpc_file *fl, uintptr_t va, |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2706 | size_t len, struct fastrpc_mmap **ppmap); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2707 | |
| 2708 | static void fastrpc_mmap_add(struct fastrpc_mmap *map); |
| 2709 | |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 2710 | static inline void get_fastrpc_ioctl_mmap_64( |
| 2711 | struct fastrpc_ioctl_mmap_64 *mmap64, |
| 2712 | struct fastrpc_ioctl_mmap *immap) |
| 2713 | { |
| 2714 | immap->fd = mmap64->fd; |
| 2715 | immap->flags = mmap64->flags; |
| 2716 | immap->vaddrin = (uintptr_t)mmap64->vaddrin; |
| 2717 | immap->size = mmap64->size; |
| 2718 | } |
| 2719 | |
| 2720 | static inline void put_fastrpc_ioctl_mmap_64( |
| 2721 | struct fastrpc_ioctl_mmap_64 *mmap64, |
| 2722 | struct fastrpc_ioctl_mmap *immap) |
| 2723 | { |
| 2724 | mmap64->vaddrout = (uint64_t)immap->vaddrout; |
| 2725 | } |
| 2726 | |
| 2727 | static inline void get_fastrpc_ioctl_munmap_64( |
| 2728 | struct fastrpc_ioctl_munmap_64 *munmap64, |
| 2729 | struct fastrpc_ioctl_munmap *imunmap) |
| 2730 | { |
| 2731 | imunmap->vaddrout = (uintptr_t)munmap64->vaddrout; |
| 2732 | imunmap->size = munmap64->size; |
| 2733 | } |
| 2734 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2735 | static int fastrpc_internal_munmap(struct fastrpc_file *fl, |
| 2736 | struct fastrpc_ioctl_munmap *ud) |
| 2737 | { |
| 2738 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2739 | struct fastrpc_mmap *map = NULL; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2740 | struct fastrpc_buf *rbuf = NULL, *free = NULL; |
| 2741 | struct hlist_node *n; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2742 | |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2743 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2744 | |
| 2745 | spin_lock(&fl->hlock); |
| 2746 | hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) { |
| 2747 | if (rbuf->raddr && (rbuf->flags == ADSP_MMAP_ADD_PAGES)) { |
| 2748 | if ((rbuf->raddr == ud->vaddrout) && |
| 2749 | (rbuf->size == ud->size)) { |
| 2750 | free = rbuf; |
| 2751 | break; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | spin_unlock(&fl->hlock); |
| 2756 | |
| 2757 | if (free) { |
| 2758 | VERIFY(err, !fastrpc_munmap_on_dsp(fl, free->raddr, |
| 2759 | free->phys, free->size, free->flags)); |
| 2760 | if (err) |
| 2761 | goto bail; |
| 2762 | fastrpc_buf_free(rbuf, 0); |
| 2763 | mutex_unlock(&fl->map_mutex); |
| 2764 | return err; |
| 2765 | } |
| 2766 | |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2767 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2768 | VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2769 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2770 | if (err) |
| 2771 | goto bail; |
Vamsi krishna Gattupalli | 4e87ecb | 2020-12-14 11:06:27 +0530 | [diff] [blame] | 2772 | VERIFY(err, map != NULL); |
| 2773 | if (err) { |
| 2774 | err = -EINVAL; |
| 2775 | goto bail; |
| 2776 | } |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2777 | VERIFY(err, !fastrpc_munmap_on_dsp(fl, map->raddr, |
Vamsi krishna Gattupalli | 4e87ecb | 2020-12-14 11:06:27 +0530 | [diff] [blame] | 2778 | map->phys, map->size, map->flags)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2779 | if (err) |
| 2780 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2781 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2782 | fastrpc_mmap_free(map, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2783 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2784 | bail: |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2785 | if (err && map) { |
| 2786 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2787 | fastrpc_mmap_add(map); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2788 | mutex_unlock(&fl->fl_map_mutex); |
| 2789 | } |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2790 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2791 | return err; |
| 2792 | } |
| 2793 | |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2794 | static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl, |
| 2795 | struct fastrpc_ioctl_munmap_fd *ud) { |
| 2796 | int err = 0; |
| 2797 | struct fastrpc_mmap *map = NULL; |
| 2798 | |
| 2799 | VERIFY(err, (fl && ud)); |
| 2800 | if (err) |
| 2801 | goto bail; |
Mohammed Nayeem Ur Rahman | fc548a5 | 2020-01-07 17:07:55 +0530 | [diff] [blame] | 2802 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2803 | mutex_lock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 09fc615 | 2018-02-16 13:13:12 +0530 | [diff] [blame] | 2804 | if (fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) { |
| 2805 | pr_err("adsprpc: mapping not found to unmap %d va %llx %x\n", |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2806 | ud->fd, (unsigned long long)ud->va, |
| 2807 | (unsigned int)ud->len); |
| 2808 | err = -1; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2809 | mutex_unlock(&fl->fl_map_mutex); |
Mohammed Nayeem Ur Rahman | fc548a5 | 2020-01-07 17:07:55 +0530 | [diff] [blame] | 2810 | mutex_unlock(&fl->map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2811 | goto bail; |
| 2812 | } |
| 2813 | if (map) |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2814 | fastrpc_mmap_free(map, 0); |
| 2815 | mutex_unlock(&fl->fl_map_mutex); |
Mohammed Nayeem Ur Rahman | fc548a5 | 2020-01-07 17:07:55 +0530 | [diff] [blame] | 2816 | mutex_unlock(&fl->map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2817 | bail: |
| 2818 | return err; |
| 2819 | } |
| 2820 | |
| 2821 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2822 | static int fastrpc_internal_mmap(struct fastrpc_file *fl, |
| 2823 | struct fastrpc_ioctl_mmap *ud) |
| 2824 | { |
| 2825 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2826 | struct fastrpc_mmap *map = NULL; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2827 | struct fastrpc_buf *rbuf = NULL; |
| 2828 | unsigned long dma_attr = 0; |
| 2829 | uintptr_t raddr = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2830 | int err = 0; |
| 2831 | |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2832 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2833 | if (ud->flags == ADSP_MMAP_ADD_PAGES) { |
| 2834 | if (ud->vaddrin) { |
| 2835 | err = -EINVAL; |
| 2836 | pr_err("adsprpc: %s: %s: ERROR: adding user allocated pages is not supported\n", |
| 2837 | current->comm, __func__); |
| 2838 | goto bail; |
| 2839 | } |
| 2840 | dma_attr = DMA_ATTR_EXEC_MAPPING | |
| 2841 | DMA_ATTR_NO_KERNEL_MAPPING | |
| 2842 | DMA_ATTR_FORCE_NON_COHERENT; |
| 2843 | err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags, |
| 2844 | 1, &rbuf); |
| 2845 | if (err) |
| 2846 | goto bail; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2847 | err = fastrpc_mmap_on_dsp(fl, ud->flags, 0, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2848 | rbuf->phys, rbuf->size, &raddr); |
| 2849 | if (err) |
| 2850 | goto bail; |
| 2851 | rbuf->raddr = raddr; |
| 2852 | } else { |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2853 | |
| 2854 | uintptr_t va_to_dsp; |
| 2855 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2856 | mutex_lock(&fl->fl_map_mutex); |
| 2857 | if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin, |
| 2858 | ud->size, ud->flags, 1, &map)) { |
Mohammed Nayeem Ur Rahman | af5f610 | 2019-10-09 13:36:52 +0530 | [diff] [blame] | 2859 | ud->vaddrout = map->raddr; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2860 | mutex_unlock(&fl->fl_map_mutex); |
| 2861 | mutex_unlock(&fl->map_mutex); |
| 2862 | return 0; |
| 2863 | } |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2864 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2865 | VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0, |
| 2866 | (uintptr_t)ud->vaddrin, ud->size, |
| 2867 | ud->flags, &map)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2868 | mutex_unlock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2869 | if (err) |
| 2870 | goto bail; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2871 | |
| 2872 | if (ud->flags == ADSP_MMAP_HEAP_ADDR || |
| 2873 | ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) |
| 2874 | va_to_dsp = 0; |
| 2875 | else |
| 2876 | va_to_dsp = (uintptr_t)map->va; |
| 2877 | VERIFY(err, 0 == fastrpc_mmap_on_dsp(fl, ud->flags, va_to_dsp, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2878 | map->phys, map->size, &raddr)); |
| 2879 | if (err) |
| 2880 | goto bail; |
| 2881 | map->raddr = raddr; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2882 | } |
Mohammed Nayeem Ur Rahman | 3ac5d32 | 2018-09-24 13:54:08 +0530 | [diff] [blame] | 2883 | ud->vaddrout = raddr; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2884 | bail: |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2885 | if (err && map) { |
| 2886 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2887 | fastrpc_mmap_free(map, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2888 | mutex_unlock(&fl->fl_map_mutex); |
| 2889 | } |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2890 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2891 | return err; |
| 2892 | } |
| 2893 | |
| 2894 | static void fastrpc_channel_close(struct kref *kref) |
| 2895 | { |
| 2896 | struct fastrpc_apps *me = &gfa; |
| 2897 | struct fastrpc_channel_ctx *ctx; |
| 2898 | int cid; |
| 2899 | |
| 2900 | ctx = container_of(kref, struct fastrpc_channel_ctx, kref); |
| 2901 | cid = ctx - &gcinfo[0]; |
Mohammed Nayeem Ur Rahman | a967be6 | 2019-09-23 20:56:15 +0530 | [diff] [blame] | 2902 | if (me->glink) { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 2903 | fastrpc_glink_close(ctx->chan, cid); |
Mohammed Nayeem Ur Rahman | a967be6 | 2019-09-23 20:56:15 +0530 | [diff] [blame] | 2904 | ctx->chan = NULL; |
| 2905 | } |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2906 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2907 | pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name, |
| 2908 | MAJOR(me->dev_no), cid); |
| 2909 | } |
| 2910 | |
| 2911 | static void fastrpc_context_list_dtor(struct fastrpc_file *fl); |
| 2912 | |
| 2913 | static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan, |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2914 | int secure, int sharedcb, struct fastrpc_session_ctx **session) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2915 | { |
| 2916 | struct fastrpc_apps *me = &gfa; |
| 2917 | int idx = 0, err = 0; |
| 2918 | |
| 2919 | if (chan->sesscount) { |
| 2920 | for (idx = 0; idx < chan->sesscount; ++idx) { |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2921 | if ((sharedcb && chan->session[idx].smmu.sharedcb) || |
| 2922 | (!chan->session[idx].used && |
| 2923 | chan->session[idx].smmu.secure |
| 2924 | == secure && !sharedcb)) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2925 | chan->session[idx].used = 1; |
| 2926 | break; |
| 2927 | } |
| 2928 | } |
| 2929 | VERIFY(err, idx < chan->sesscount); |
| 2930 | if (err) |
| 2931 | goto bail; |
| 2932 | chan->session[idx].smmu.faults = 0; |
| 2933 | } else { |
| 2934 | VERIFY(err, me->dev != NULL); |
| 2935 | if (err) |
| 2936 | goto bail; |
| 2937 | chan->session[0].dev = me->dev; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2938 | chan->session[0].smmu.dev = me->dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | *session = &chan->session[idx]; |
| 2942 | bail: |
| 2943 | return err; |
| 2944 | } |
| 2945 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2946 | static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv, |
| 2947 | size_t size) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2948 | { |
| 2949 | if (glink_queue_rx_intent(h, NULL, size)) |
| 2950 | return false; |
| 2951 | return true; |
| 2952 | } |
| 2953 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2954 | static void fastrpc_glink_notify_tx_done(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2955 | const void *pkt_priv, const void *ptr) |
| 2956 | { |
| 2957 | } |
| 2958 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2959 | static void fastrpc_glink_notify_rx(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2960 | const void *pkt_priv, const void *ptr, size_t size) |
| 2961 | { |
| 2962 | struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2963 | struct fastrpc_apps *me = &gfa; |
| 2964 | uint32_t index; |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2965 | int err = 0; |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 2966 | unsigned long irq_flags = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2967 | |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2968 | VERIFY(err, (rsp && size >= sizeof(*rsp))); |
| 2969 | if (err) |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2970 | goto bail; |
| 2971 | |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2972 | index = (uint32_t)((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); |
| 2973 | VERIFY(err, index < FASTRPC_CTX_MAX); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2974 | if (err) |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2975 | goto bail; |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2976 | |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2977 | VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index])); |
| 2978 | if (err) |
| 2979 | goto bail; |
| 2980 | |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 2981 | spin_lock_irqsave(&me->ctxlock, irq_flags); |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2982 | VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp->ctx & ~3)) && |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2983 | me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC)); |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 2984 | if (err) { |
| 2985 | spin_unlock_irqrestore(&me->ctxlock, irq_flags); |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2986 | goto bail; |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 2987 | } |
Mohammed Nayeem Ur Rahman | 32ba95d | 2019-07-26 17:31:37 +0530 | [diff] [blame] | 2988 | me->ctxtable[index]->handle = handle; |
| 2989 | me->ctxtable[index]->ptr = ptr; |
Jeya R | 8fa59d6 | 2020-11-04 20:42:59 +0530 | [diff] [blame] | 2990 | spin_unlock_irqrestore(&me->ctxlock, irq_flags); |
Mohammed Nayeem Ur Rahman | 32ba95d | 2019-07-26 17:31:37 +0530 | [diff] [blame] | 2991 | |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2992 | context_notify_user(me->ctxtable[index], rsp->retval); |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2993 | bail: |
Jeya R | 859f801 | 2020-08-09 02:09:14 +0530 | [diff] [blame] | 2994 | if (err) { |
| 2995 | glink_rx_done(handle, ptr, true); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2996 | pr_err("adsprpc: invalid response or context\n"); |
Jeya R | 859f801 | 2020-08-09 02:09:14 +0530 | [diff] [blame] | 2997 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3000 | static void fastrpc_glink_notify_state(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3001 | unsigned int event) |
| 3002 | { |
| 3003 | struct fastrpc_apps *me = &gfa; |
| 3004 | int cid = (int)(uintptr_t)priv; |
| 3005 | struct fastrpc_glink_info *link; |
| 3006 | |
| 3007 | if (cid < 0 || cid >= NUM_CHANNELS) |
| 3008 | return; |
| 3009 | link = &me->channel[cid].link; |
| 3010 | switch (event) { |
| 3011 | case GLINK_CONNECTED: |
| 3012 | link->port_state = FASTRPC_LINK_CONNECTED; |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 3013 | complete(&me->channel[cid].workport); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3014 | break; |
| 3015 | case GLINK_LOCAL_DISCONNECTED: |
| 3016 | link->port_state = FASTRPC_LINK_DISCONNECTED; |
| 3017 | break; |
| 3018 | case GLINK_REMOTE_DISCONNECTED: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3019 | break; |
| 3020 | default: |
| 3021 | break; |
| 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure, |
| 3026 | struct fastrpc_session_ctx **session) |
| 3027 | { |
| 3028 | int err = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3029 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3030 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3031 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3032 | if (!*session) |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3033 | err = fastrpc_session_alloc_locked(chan, secure, 0, session); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3034 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3035 | return err; |
| 3036 | } |
| 3037 | |
| 3038 | static void fastrpc_session_free(struct fastrpc_channel_ctx *chan, |
| 3039 | struct fastrpc_session_ctx *session) |
| 3040 | { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3041 | struct fastrpc_apps *me = &gfa; |
| 3042 | |
| 3043 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3044 | session->used = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3045 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | static int fastrpc_file_free(struct fastrpc_file *fl) |
| 3049 | { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3050 | struct hlist_node *n = NULL; |
Tharun Kumar Merugu | 3e96676 | 2018-04-04 10:56:44 +0530 | [diff] [blame] | 3051 | struct fastrpc_mmap *map = NULL, *lmap = NULL; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3052 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3053 | int cid; |
| 3054 | |
| 3055 | if (!fl) |
| 3056 | return 0; |
| 3057 | cid = fl->cid; |
| 3058 | |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 3059 | (void)fastrpc_release_current_dsp_process(fl); |
| 3060 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3061 | spin_lock(&fl->apps->hlock); |
| 3062 | hlist_del_init(&fl->hn); |
| 3063 | spin_unlock(&fl->apps->hlock); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3064 | kfree(fl->debug_buf); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3065 | |
Sathish Ambley | d7fbcbb | 2017-03-08 10:55:48 -0800 | [diff] [blame] | 3066 | if (!fl->sctx) { |
| 3067 | kfree(fl); |
| 3068 | return 0; |
| 3069 | } |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 3070 | spin_lock(&fl->hlock); |
| 3071 | fl->file_close = 1; |
| 3072 | spin_unlock(&fl->hlock); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3073 | if (!IS_ERR_OR_NULL(fl->init_mem)) |
| 3074 | fastrpc_buf_free(fl->init_mem, 0); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3075 | fastrpc_context_list_dtor(fl); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3076 | fastrpc_cached_buf_list_free(fl); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3077 | mutex_lock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 3e96676 | 2018-04-04 10:56:44 +0530 | [diff] [blame] | 3078 | do { |
| 3079 | lmap = NULL; |
| 3080 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 3081 | hlist_del_init(&map->hn); |
| 3082 | lmap = map; |
| 3083 | break; |
| 3084 | } |
| 3085 | fastrpc_mmap_free(lmap, 1); |
| 3086 | } while (lmap); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3087 | mutex_unlock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 3088 | if (fl->refcount && (fl->ssrcount == fl->apps->channel[cid].ssrcount)) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3089 | kref_put_mutex(&fl->apps->channel[cid].kref, |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3090 | fastrpc_channel_close, &fl->apps->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3091 | if (fl->sctx) |
| 3092 | fastrpc_session_free(&fl->apps->channel[cid], fl->sctx); |
| 3093 | if (fl->secsctx) |
| 3094 | fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3095 | |
| 3096 | mutex_lock(&fl->perf_mutex); |
| 3097 | do { |
| 3098 | struct hlist_node *pn = NULL; |
| 3099 | |
| 3100 | fperf = NULL; |
| 3101 | hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) { |
| 3102 | hlist_del_init(&perf->hn); |
| 3103 | fperf = perf; |
| 3104 | break; |
| 3105 | } |
| 3106 | kfree(fperf); |
| 3107 | } while (fperf); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3108 | fastrpc_remote_buf_list_free(fl); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3109 | mutex_unlock(&fl->perf_mutex); |
| 3110 | mutex_destroy(&fl->perf_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3111 | mutex_destroy(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 8714e64 | 2018-05-17 15:21:08 +0530 | [diff] [blame] | 3112 | mutex_destroy(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3113 | kfree(fl); |
| 3114 | return 0; |
| 3115 | } |
| 3116 | |
| 3117 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 3118 | { |
| 3119 | struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data; |
| 3120 | |
| 3121 | if (fl) { |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3122 | if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req)) |
| 3123 | pm_qos_remove_request(&fl->pm_qos_req); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3124 | if (fl->debugfs_file != NULL) |
| 3125 | debugfs_remove(fl->debugfs_file); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3126 | fastrpc_file_free(fl); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3127 | file->private_data = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3128 | } |
| 3129 | return 0; |
| 3130 | } |
| 3131 | |
| 3132 | static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info, |
| 3133 | void *priv) |
| 3134 | { |
| 3135 | struct fastrpc_apps *me = &gfa; |
| 3136 | int cid = (int)((uintptr_t)priv); |
| 3137 | struct fastrpc_glink_info *link; |
| 3138 | |
| 3139 | if (cid < 0 || cid >= NUM_CHANNELS) |
| 3140 | return; |
| 3141 | |
| 3142 | link = &me->channel[cid].link; |
| 3143 | switch (cb_info->link_state) { |
| 3144 | case GLINK_LINK_STATE_UP: |
| 3145 | link->link_state = FASTRPC_LINK_STATE_UP; |
| 3146 | complete(&me->channel[cid].work); |
| 3147 | break; |
| 3148 | case GLINK_LINK_STATE_DOWN: |
| 3149 | link->link_state = FASTRPC_LINK_STATE_DOWN; |
| 3150 | break; |
| 3151 | default: |
| 3152 | pr_err("adsprpc: unknown link state %d\n", cb_info->link_state); |
| 3153 | break; |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | static int fastrpc_glink_register(int cid, struct fastrpc_apps *me) |
| 3158 | { |
| 3159 | int err = 0; |
| 3160 | struct fastrpc_glink_info *link; |
| 3161 | |
| 3162 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 3163 | if (err) |
| 3164 | goto bail; |
| 3165 | |
| 3166 | link = &me->channel[cid].link; |
| 3167 | if (link->link_notify_handle != NULL) |
| 3168 | goto bail; |
| 3169 | |
| 3170 | link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler; |
| 3171 | link->link_notify_handle = glink_register_link_state_cb( |
| 3172 | &link->link_info, |
| 3173 | (void *)((uintptr_t)cid)); |
| 3174 | VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle)); |
| 3175 | if (err) { |
| 3176 | link->link_notify_handle = NULL; |
| 3177 | goto bail; |
| 3178 | } |
| 3179 | VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work, |
| 3180 | RPC_TIMEOUT)); |
| 3181 | bail: |
| 3182 | return err; |
| 3183 | } |
| 3184 | |
| 3185 | static void fastrpc_glink_close(void *chan, int cid) |
| 3186 | { |
| 3187 | int err = 0; |
| 3188 | struct fastrpc_glink_info *link; |
| 3189 | |
| 3190 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 3191 | if (err) |
| 3192 | return; |
| 3193 | link = &gfa.channel[cid].link; |
| 3194 | |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 3195 | if (link->port_state == FASTRPC_LINK_CONNECTED || |
| 3196 | link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3197 | link->port_state = FASTRPC_LINK_DISCONNECTING; |
| 3198 | glink_close(chan); |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | static int fastrpc_glink_open(int cid) |
| 3203 | { |
| 3204 | int err = 0; |
| 3205 | void *handle = NULL; |
| 3206 | struct fastrpc_apps *me = &gfa; |
| 3207 | struct glink_open_config *cfg; |
| 3208 | struct fastrpc_glink_info *link; |
| 3209 | |
| 3210 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 3211 | if (err) |
| 3212 | goto bail; |
| 3213 | link = &me->channel[cid].link; |
| 3214 | cfg = &me->channel[cid].link.cfg; |
| 3215 | VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP)); |
| 3216 | if (err) |
| 3217 | goto bail; |
| 3218 | |
Tharun Kumar Merugu | ca0db526 | 2017-05-10 12:53:12 +0530 | [diff] [blame] | 3219 | VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED)); |
| 3220 | if (err) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3221 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3222 | |
| 3223 | link->port_state = FASTRPC_LINK_CONNECTING; |
| 3224 | cfg->priv = (void *)(uintptr_t)cid; |
| 3225 | cfg->edge = gcinfo[cid].link.link_info.edge; |
| 3226 | cfg->transport = gcinfo[cid].link.link_info.transport; |
| 3227 | cfg->name = FASTRPC_GLINK_GUID; |
| 3228 | cfg->notify_rx = fastrpc_glink_notify_rx; |
| 3229 | cfg->notify_tx_done = fastrpc_glink_notify_tx_done; |
| 3230 | cfg->notify_state = fastrpc_glink_notify_state; |
| 3231 | cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req; |
| 3232 | handle = glink_open(cfg); |
| 3233 | VERIFY(err, !IS_ERR_OR_NULL(handle)); |
c_mtharu | 6e1d26b | 2017-10-09 16:05:24 +0530 | [diff] [blame] | 3234 | if (err) { |
| 3235 | if (link->port_state == FASTRPC_LINK_CONNECTING) |
| 3236 | link->port_state = FASTRPC_LINK_DISCONNECTED; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3237 | goto bail; |
c_mtharu | 6e1d26b | 2017-10-09 16:05:24 +0530 | [diff] [blame] | 3238 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3239 | me->channel[cid].chan = handle; |
| 3240 | bail: |
| 3241 | return err; |
| 3242 | } |
| 3243 | |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3244 | static int fastrpc_debugfs_open(struct inode *inode, struct file *filp) |
| 3245 | { |
| 3246 | filp->private_data = inode->i_private; |
| 3247 | return 0; |
| 3248 | } |
| 3249 | |
| 3250 | static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer, |
| 3251 | size_t count, loff_t *position) |
| 3252 | { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3253 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3254 | struct fastrpc_file *fl = filp->private_data; |
| 3255 | struct hlist_node *n; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3256 | struct fastrpc_buf *buf = NULL; |
| 3257 | struct fastrpc_mmap *map = NULL; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3258 | struct fastrpc_mmap *gmaps = NULL; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3259 | struct smq_invoke_ctx *ictx = NULL; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3260 | struct fastrpc_channel_ctx *chan = NULL; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3261 | unsigned int len = 0; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3262 | int i, j, sess_used = 0, ret = 0; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3263 | char *fileinfo = NULL; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3264 | char single_line[UL_SIZE] = "----------------"; |
| 3265 | char title[UL_SIZE] = "========================="; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3266 | |
| 3267 | fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL); |
| 3268 | if (!fileinfo) |
| 3269 | goto bail; |
| 3270 | if (fl == NULL) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3271 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3272 | "\n%s %s %s\n", title, " CHANNEL INFO ", title); |
| 3273 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3274 | "%-8s|%-9s|%-9s|%-14s|%-9s|%-13s\n", |
| 3275 | "susbsys", "refcount", "sesscount", "issubsystemup", |
| 3276 | "ssrcount", "session_used"); |
| 3277 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3278 | "-%s%s%s%s-\n", single_line, single_line, |
| 3279 | single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3280 | for (i = 0; i < NUM_CHANNELS; i++) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3281 | sess_used = 0; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3282 | chan = &gcinfo[i]; |
| 3283 | len += scnprintf(fileinfo + len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3284 | DEBUGFS_SIZE - len, "%-8s", chan->subsys); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3285 | len += scnprintf(fileinfo + len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3286 | DEBUGFS_SIZE - len, "|%-9d", |
| 3287 | chan->kref.refcount.counter); |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3288 | len += scnprintf(fileinfo + len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3289 | DEBUGFS_SIZE - len, "|%-9d", |
| 3290 | chan->sesscount); |
| 3291 | len += scnprintf(fileinfo + len, |
| 3292 | DEBUGFS_SIZE - len, "|%-14d", |
| 3293 | chan->issubsystemup); |
| 3294 | len += scnprintf(fileinfo + len, |
| 3295 | DEBUGFS_SIZE - len, "|%-9d", |
| 3296 | chan->ssrcount); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3297 | for (j = 0; j < chan->sesscount; j++) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3298 | sess_used += chan->session[j].used; |
| 3299 | } |
| 3300 | len += scnprintf(fileinfo + len, |
| 3301 | DEBUGFS_SIZE - len, "|%-13d\n", sess_used); |
| 3302 | |
| 3303 | } |
| 3304 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3305 | "\n%s%s%s\n", "=============", |
| 3306 | " CMA HEAP ", "=============="); |
| 3307 | len += scnprintf(fileinfo + len, |
| 3308 | DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size"); |
| 3309 | len += scnprintf(fileinfo + len, |
| 3310 | DEBUGFS_SIZE - len, "--%s%s---\n", |
| 3311 | single_line, single_line); |
| 3312 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3313 | "0x%-18llX", me->range.addr); |
| 3314 | len += scnprintf(fileinfo + len, |
| 3315 | DEBUGFS_SIZE - len, "|0x%-18llX\n", me->range.size); |
| 3316 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3317 | "\n==========%s %s %s===========\n", |
| 3318 | title, " GMAPS ", title); |
| 3319 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3320 | "%-20s|%-20s|%-20s|%-20s\n", |
| 3321 | "fd", "phys", "size", "va"); |
| 3322 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3323 | "%s%s%s%s%s\n", single_line, single_line, |
| 3324 | single_line, single_line, single_line); |
| 3325 | hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) { |
| 3326 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3327 | "%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n", |
| 3328 | gmaps->fd, gmaps->phys, |
| 3329 | (uint32_t)gmaps->size, |
| 3330 | gmaps->va); |
| 3331 | } |
| 3332 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3333 | "%-20s|%-20s|%-20s|%-20s\n", |
| 3334 | "len", "refs", "raddr", "flags"); |
| 3335 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3336 | "%s%s%s%s%s\n", single_line, single_line, |
| 3337 | single_line, single_line, single_line); |
| 3338 | hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) { |
| 3339 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3340 | "0x%-18X|%-20d|%-20lu|%-20u\n", |
| 3341 | (uint32_t)gmaps->len, gmaps->refs, |
| 3342 | gmaps->raddr, gmaps->flags); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3343 | } |
| 3344 | } else { |
| 3345 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3346 | "\n%s %13s %d\n", "cid", ":", fl->cid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3347 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3348 | "%s %12s %d\n", "tgid", ":", fl->tgid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3349 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3350 | "%s %7s %d\n", "sessionid", ":", fl->sessionid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3351 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3352 | "%s %8s %d\n", "ssrcount", ":", fl->ssrcount); |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3353 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3354 | "%s %8s %d\n", "refcount", ":", fl->refcount); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3355 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3356 | "%s %14s %d\n", "pd", ":", fl->pd); |
| 3357 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3358 | "%s %9s %s\n", "spdname", ":", fl->spdname); |
| 3359 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3360 | "%s %6s %d\n", "file_close", ":", fl->file_close); |
| 3361 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3362 | "%s %8s %d\n", "sharedcb", ":", fl->sharedcb); |
| 3363 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3364 | "%s %9s %d\n", "profile", ":", fl->profile); |
| 3365 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3366 | "%s %3s %d\n", "smmu.coherent", ":", |
| 3367 | fl->sctx->smmu.coherent); |
| 3368 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3369 | "%s %4s %d\n", "smmu.enabled", ":", |
| 3370 | fl->sctx->smmu.enabled); |
| 3371 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3372 | "%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb); |
| 3373 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3374 | "%s %5s %d\n", "smmu.secure", ":", |
| 3375 | fl->sctx->smmu.secure); |
| 3376 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3377 | "%s %5s %d\n", "smmu.faults", ":", |
| 3378 | fl->sctx->smmu.faults); |
| 3379 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3380 | "%s %s %d\n", "link.link_state", |
| 3381 | ":", *&me->channel[fl->cid].link.link_state); |
| 3382 | |
| 3383 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3384 | "\n=======%s %s %s======\n", title, |
| 3385 | " LIST OF MAPS ", title); |
| 3386 | |
| 3387 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3388 | "%-20s|%-20s|%-20s\n", "va", "phys", "size"); |
| 3389 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3390 | "%s%s%s%s%s\n", |
| 3391 | single_line, single_line, single_line, |
| 3392 | single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3393 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3394 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3395 | "0x%-20lX|0x%-20llX|0x%-20zu\n\n", |
| 3396 | map->va, map->phys, |
| 3397 | map->size); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3398 | } |
| 3399 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3400 | "%-20s|%-20s|%-20s|%-20s\n", |
| 3401 | "len", "refs", |
| 3402 | "raddr", "uncached"); |
| 3403 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3404 | "%s%s%s%s%s\n", |
| 3405 | single_line, single_line, single_line, |
| 3406 | single_line, single_line); |
| 3407 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 3408 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3409 | "%-20zu|%-20d|0x%-20lX|%-20d\n\n", |
| 3410 | map->len, map->refs, map->raddr, |
| 3411 | map->uncached); |
| 3412 | } |
| 3413 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3414 | "%-20s|%-20s\n", "secure", "attr"); |
| 3415 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3416 | "%s%s%s%s%s\n", |
| 3417 | single_line, single_line, single_line, |
| 3418 | single_line, single_line); |
| 3419 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 3420 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3421 | "%-20d|0x%-20lX\n\n", |
| 3422 | map->secure, map->attr); |
| 3423 | } |
| 3424 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3425 | "%s %d\n\n", |
| 3426 | "KERNEL MEMORY ALLOCATION:", 1); |
| 3427 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3428 | "\n======%s %s %s======\n", title, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3429 | " LIST OF CACHED BUFS ", title); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3430 | spin_lock(&fl->hlock); |
| 3431 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3432 | "%-19s|%-19s|%-19s|%-19s\n", |
| 3433 | "virt", "phys", "size", "dma_attr"); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3434 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3435 | "%s%s%s%s%s\n", single_line, single_line, |
| 3436 | single_line, single_line, single_line); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3437 | hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3438 | len += scnprintf(fileinfo + len, |
| 3439 | DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3440 | "0x%-17p|0x%-17llX|%-19zu|0x%-17lX\n", |
| 3441 | buf->virt, (uint64_t)buf->phys, buf->size, |
| 3442 | buf->dma_attr); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3443 | } |
| 3444 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3445 | "\n%s %s %s\n", title, |
| 3446 | " LIST OF PENDING SMQCONTEXTS ", title); |
| 3447 | |
| 3448 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3449 | "%-20s|%-10s|%-10s|%-10s|%-20s\n", |
| 3450 | "sc", "pid", "tgid", "used", "ctxid"); |
| 3451 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3452 | "%s%s%s%s%s\n", single_line, single_line, |
| 3453 | single_line, single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3454 | hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) { |
| 3455 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3456 | "0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n", |
| 3457 | ictx->sc, ictx->pid, ictx->tgid, |
| 3458 | ictx->used, ictx->ctxid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3459 | } |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3460 | |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3461 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3462 | "\n%s %s %s\n", title, |
| 3463 | " LIST OF INTERRUPTED SMQCONTEXTS ", title); |
| 3464 | |
| 3465 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3466 | "%-20s|%-10s|%-10s|%-10s|%-20s\n", |
| 3467 | "sc", "pid", "tgid", "used", "ctxid"); |
| 3468 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3469 | "%s%s%s%s%s\n", single_line, single_line, |
| 3470 | single_line, single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3471 | hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3472 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3473 | "%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n", |
| 3474 | ictx->sc, ictx->pid, ictx->tgid, |
| 3475 | ictx->used, ictx->ctxid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3476 | } |
| 3477 | spin_unlock(&fl->hlock); |
| 3478 | } |
| 3479 | if (len > DEBUGFS_SIZE) |
| 3480 | len = DEBUGFS_SIZE; |
| 3481 | ret = simple_read_from_buffer(buffer, count, position, fileinfo, len); |
| 3482 | kfree(fileinfo); |
| 3483 | bail: |
| 3484 | return ret; |
| 3485 | } |
| 3486 | |
| 3487 | static const struct file_operations debugfs_fops = { |
| 3488 | .open = fastrpc_debugfs_open, |
| 3489 | .read = fastrpc_debugfs_read, |
| 3490 | }; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3491 | static int fastrpc_channel_open(struct fastrpc_file *fl) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3492 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3493 | struct fastrpc_apps *me = &gfa; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 3494 | int cid = -1, ii, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3495 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3496 | mutex_lock(&me->smd_mutex); |
| 3497 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3498 | VERIFY(err, fl && fl->sctx); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3499 | if (err) |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3500 | goto bail; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3501 | cid = fl->cid; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 3502 | VERIFY(err, cid >= ADSP_DOMAIN_ID && cid < NUM_CHANNELS); |
| 3503 | if (err) { |
| 3504 | err = -ECHRNG; |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 3505 | goto bail; |
Mohammed Nayeem Ur Rahman | cd83646 | 2020-04-01 14:30:33 +0530 | [diff] [blame] | 3506 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3507 | if (me->channel[cid].ssrcount != |
| 3508 | me->channel[cid].prevssrcount) { |
| 3509 | if (!me->channel[cid].issubsystemup) { |
| 3510 | VERIFY(err, 0); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3511 | if (err) { |
| 3512 | err = -ENOTCONN; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3513 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3514 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3515 | } |
| 3516 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3517 | fl->ssrcount = me->channel[cid].ssrcount; |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 3518 | fl->refcount = 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3519 | if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) || |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3520 | (me->channel[cid].chan == NULL)) { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3521 | if (me->glink) { |
| 3522 | VERIFY(err, 0 == fastrpc_glink_register(cid, me)); |
| 3523 | if (err) |
| 3524 | goto bail; |
| 3525 | VERIFY(err, 0 == fastrpc_glink_open(cid)); |
Mohammed Nayeem Ur Rahman | a967be6 | 2019-09-23 20:56:15 +0530 | [diff] [blame] | 3526 | VERIFY(err, |
| 3527 | wait_for_completion_timeout(&me->channel[cid].workport, |
| 3528 | RPC_TIMEOUT)); |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3529 | } else { |
Mohammed Nayeem Ur Rahman | a967be6 | 2019-09-23 20:56:15 +0530 | [diff] [blame] | 3530 | if (me->channel[cid].chan == NULL) { |
| 3531 | VERIFY(err, !smd_named_open_on_edge( |
| 3532 | FASTRPC_SMD_GUID, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3533 | gcinfo[cid].channel, |
| 3534 | (smd_channel_t **)&me->channel[cid].chan, |
| 3535 | (void *)(uintptr_t)cid, |
| 3536 | smd_event_handler)); |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 3537 | VERIFY(err, |
| 3538 | wait_for_completion_timeout(&me->channel[cid].workport, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3539 | RPC_TIMEOUT)); |
Mohammed Nayeem Ur Rahman | a967be6 | 2019-09-23 20:56:15 +0530 | [diff] [blame] | 3540 | |
| 3541 | } |
| 3542 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3543 | if (err) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3544 | me->channel[cid].chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3545 | goto bail; |
| 3546 | } |
| 3547 | kref_init(&me->channel[cid].kref); |
| 3548 | pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name, |
| 3549 | MAJOR(me->dev_no), cid); |
Tharun Kumar Merugu | c42c6e2 | 2018-05-29 15:50:46 +0530 | [diff] [blame] | 3550 | |
| 3551 | for (ii = 0; ii < FASTRPC_GLINK_INTENT_NUM && me->glink; ii++) |
| 3552 | glink_queue_rx_intent(me->channel[cid].chan, NULL, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3553 | FASTRPC_GLINK_INTENT_LEN); |
Tharun Kumar Merugu | c42c6e2 | 2018-05-29 15:50:46 +0530 | [diff] [blame] | 3554 | |
Tharun Kumar Merugu | d86fc8c | 2018-01-04 16:35:31 +0530 | [diff] [blame] | 3555 | if (cid == 0 && me->channel[cid].ssrcount != |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3556 | me->channel[cid].prevssrcount) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3557 | if (fastrpc_mmap_remove_ssr(fl)) |
| 3558 | pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3559 | me->channel[cid].prevssrcount = |
| 3560 | me->channel[cid].ssrcount; |
| 3561 | } |
| 3562 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3563 | |
| 3564 | bail: |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3565 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3566 | return err; |
| 3567 | } |
| 3568 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3569 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 3570 | { |
| 3571 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3572 | struct fastrpc_file *fl = NULL; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3573 | struct fastrpc_apps *me = &gfa; |
| 3574 | |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3575 | /* |
| 3576 | * Indicates the device node opened |
| 3577 | * MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV |
| 3578 | */ |
| 3579 | int dev_minor = MINOR(inode->i_rdev); |
| 3580 | |
| 3581 | VERIFY(err, ((dev_minor == MINOR_NUM_DEV) || |
| 3582 | (dev_minor == MINOR_NUM_SECURE_DEV))); |
| 3583 | if (err) { |
| 3584 | pr_err("adsprpc: Invalid dev minor num %d\n", dev_minor); |
| 3585 | return err; |
| 3586 | } |
| 3587 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3588 | VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL))); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3589 | if (err) |
| 3590 | return err; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3591 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3592 | context_list_ctor(&fl->clst); |
| 3593 | spin_lock_init(&fl->hlock); |
| 3594 | INIT_HLIST_HEAD(&fl->maps); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3595 | INIT_HLIST_HEAD(&fl->perf); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3596 | INIT_HLIST_HEAD(&fl->cached_bufs); |
| 3597 | INIT_HLIST_HEAD(&fl->remote_bufs); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3598 | INIT_HLIST_NODE(&fl->hn); |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 3599 | fl->sessionid = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3600 | fl->apps = me; |
| 3601 | fl->mode = FASTRPC_MODE_SERIAL; |
| 3602 | fl->cid = -1; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3603 | fl->dev_minor = dev_minor; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3604 | fl->init_mem = NULL; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3605 | fl->qos_request = 0; |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 3606 | fl->refcount = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3607 | filp->private_data = fl; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 3608 | mutex_init(&fl->map_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3609 | mutex_init(&fl->fl_map_mutex); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3610 | spin_lock(&me->hlock); |
| 3611 | hlist_add_head(&fl->hn, &me->drivers); |
| 3612 | spin_unlock(&me->hlock); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3613 | mutex_init(&fl->perf_mutex); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3614 | return 0; |
| 3615 | } |
| 3616 | |
Edgar Flores | 1a772fa | 2020-02-07 14:59:29 -0800 | [diff] [blame] | 3617 | static int fastrpc_set_process_info(struct fastrpc_file *fl) |
| 3618 | { |
| 3619 | int err = 0, buf_size = 0; |
| 3620 | char strpid[PID_SIZE]; |
| 3621 | |
| 3622 | fl->tgid = current->tgid; |
| 3623 | snprintf(strpid, PID_SIZE, "%d", current->pid); |
| 3624 | buf_size = strlen(current->comm) + strlen("_") + strlen(strpid) + 1; |
| 3625 | fl->debug_buf = kzalloc(buf_size, GFP_KERNEL); |
| 3626 | if (!fl->debug_buf) { |
| 3627 | err = -ENOMEM; |
| 3628 | return err; |
| 3629 | } |
| 3630 | snprintf(fl->debug_buf, UL_SIZE, "%.10s%s%d", |
| 3631 | current->comm, "_", current->pid); |
| 3632 | fl->debugfs_file = debugfs_create_file(fl->debug_buf, 0644, |
| 3633 | debugfs_root, fl, &debugfs_fops); |
| 3634 | if (!fl->debugfs_file) |
| 3635 | pr_warn("Error: %s: %s: failed to create debugfs file %s\n", |
| 3636 | current->comm, __func__, fl->debug_buf); |
| 3637 | return err; |
| 3638 | } |
| 3639 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3640 | static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info) |
| 3641 | { |
| 3642 | int err = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3643 | uint32_t cid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3644 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3645 | VERIFY(err, fl != NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3646 | if (err) |
| 3647 | goto bail; |
Edgar Flores | 1a772fa | 2020-02-07 14:59:29 -0800 | [diff] [blame] | 3648 | err = fastrpc_set_process_info(fl); |
| 3649 | if (err) |
| 3650 | goto bail; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3651 | if (fl->cid == -1) { |
| 3652 | cid = *info; |
| 3653 | VERIFY(err, cid < NUM_CHANNELS); |
| 3654 | if (err) |
| 3655 | goto bail; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3656 | /* Check to see if the device node is non-secure */ |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 3657 | if (fl->dev_minor == MINOR_NUM_DEV && |
| 3658 | fl->apps->secure_flag == true) { |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3659 | /* |
| 3660 | * For non secure device node check and make sure that |
| 3661 | * the channel allows non-secure access |
| 3662 | * If not, bail. Session will not start. |
| 3663 | * cid will remain -1 and client will not be able to |
| 3664 | * invoke any other methods without failure |
| 3665 | */ |
| 3666 | if (fl->apps->channel[cid].secure == SECURE_CHANNEL) { |
| 3667 | err = -EPERM; |
| 3668 | pr_err("adsprpc: GetInfo failed dev %d, cid %d, secure %d\n", |
| 3669 | fl->dev_minor, cid, |
| 3670 | fl->apps->channel[cid].secure); |
| 3671 | goto bail; |
| 3672 | } |
| 3673 | } |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3674 | fl->cid = cid; |
| 3675 | fl->ssrcount = fl->apps->channel[cid].ssrcount; |
| 3676 | VERIFY(err, !fastrpc_session_alloc_locked( |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3677 | &fl->apps->channel[cid], 0, fl->sharedcb, &fl->sctx)); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3678 | if (err) |
| 3679 | goto bail; |
| 3680 | } |
Tharun Kumar Merugu | 80be7d6 | 2017-08-02 11:03:22 +0530 | [diff] [blame] | 3681 | VERIFY(err, fl->sctx != NULL); |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 3682 | if (err) { |
| 3683 | err = -EBADR; |
Tharun Kumar Merugu | 80be7d6 | 2017-08-02 11:03:22 +0530 | [diff] [blame] | 3684 | goto bail; |
Jeya R | 984a1a3 | 2021-01-18 15:38:07 +0530 | [diff] [blame] | 3685 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3686 | *info = (fl->sctx->smmu.enabled ? 1 : 0); |
| 3687 | bail: |
| 3688 | return err; |
| 3689 | } |
| 3690 | |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3691 | static int fastrpc_internal_control(struct fastrpc_file *fl, |
| 3692 | struct fastrpc_ioctl_control *cp) |
| 3693 | { |
Mohammed Nayeem Ur Rahman | 18d633f | 2019-05-28 15:11:40 +0530 | [diff] [blame] | 3694 | struct fastrpc_apps *me = &gfa; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3695 | int err = 0; |
| 3696 | int latency; |
| 3697 | |
| 3698 | VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps)); |
| 3699 | if (err) |
| 3700 | goto bail; |
| 3701 | VERIFY(err, !IS_ERR_OR_NULL(cp)); |
| 3702 | if (err) |
| 3703 | goto bail; |
| 3704 | |
| 3705 | switch (cp->req) { |
| 3706 | case FASTRPC_CONTROL_LATENCY: |
| 3707 | latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ? |
| 3708 | fl->apps->latency : PM_QOS_DEFAULT_VALUE; |
| 3709 | VERIFY(err, latency != 0); |
| 3710 | if (err) |
| 3711 | goto bail; |
| 3712 | if (!fl->qos_request) { |
| 3713 | pm_qos_add_request(&fl->pm_qos_req, |
| 3714 | PM_QOS_CPU_DMA_LATENCY, latency); |
| 3715 | fl->qos_request = 1; |
| 3716 | } else |
| 3717 | pm_qos_update_request(&fl->pm_qos_req, latency); |
| 3718 | break; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3719 | case FASTRPC_CONTROL_SMMU: |
Mohammed Nayeem Ur Rahman | 18d633f | 2019-05-28 15:11:40 +0530 | [diff] [blame] | 3720 | if (!me->legacy) |
| 3721 | fl->sharedcb = cp->smmu.sharedcb; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3722 | break; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3723 | case FASTRPC_CONTROL_KALLOC: |
| 3724 | cp->kalloc.kalloc_support = 1; |
| 3725 | break; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3726 | default: |
| 3727 | err = -ENOTTY; |
| 3728 | break; |
| 3729 | } |
| 3730 | bail: |
| 3731 | return err; |
| 3732 | } |
| 3733 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3734 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 3735 | unsigned long ioctl_param) |
| 3736 | { |
| 3737 | union { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3738 | struct fastrpc_ioctl_invoke_crc inv; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3739 | struct fastrpc_ioctl_mmap mmap; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3740 | struct fastrpc_ioctl_mmap_64 mmap64; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3741 | struct fastrpc_ioctl_munmap munmap; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3742 | struct fastrpc_ioctl_munmap_64 munmap64; |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 3743 | struct fastrpc_ioctl_munmap_fd munmap_fd; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 3744 | struct fastrpc_ioctl_init_attrs init; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3745 | struct fastrpc_ioctl_perf perf; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3746 | struct fastrpc_ioctl_control cp; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3747 | } p; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3748 | union { |
| 3749 | struct fastrpc_ioctl_mmap mmap; |
| 3750 | struct fastrpc_ioctl_munmap munmap; |
| 3751 | } i; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3752 | void *param = (char *)ioctl_param; |
| 3753 | struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data; |
| 3754 | int size = 0, err = 0; |
| 3755 | uint32_t info; |
| 3756 | |
Jeya R | b70b4ad | 2021-01-25 10:28:42 -0800 | [diff] [blame] | 3757 | VERIFY(err, fl != NULL); |
| 3758 | if (err) { |
| 3759 | err = -EBADR; |
| 3760 | goto bail; |
| 3761 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3762 | p.inv.fds = NULL; |
| 3763 | p.inv.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3764 | p.inv.crc = NULL; |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 3765 | spin_lock(&fl->hlock); |
| 3766 | if (fl->file_close == 1) { |
| 3767 | err = EBADF; |
| 3768 | pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP"); |
| 3769 | spin_unlock(&fl->hlock); |
| 3770 | goto bail; |
| 3771 | } |
| 3772 | spin_unlock(&fl->hlock); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3773 | |
| 3774 | switch (ioctl_num) { |
| 3775 | case FASTRPC_IOCTL_INVOKE: |
| 3776 | size = sizeof(struct fastrpc_ioctl_invoke); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3777 | /* fall through */ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3778 | case FASTRPC_IOCTL_INVOKE_FD: |
| 3779 | if (!size) |
| 3780 | size = sizeof(struct fastrpc_ioctl_invoke_fd); |
| 3781 | /* fall through */ |
| 3782 | case FASTRPC_IOCTL_INVOKE_ATTRS: |
| 3783 | if (!size) |
| 3784 | size = sizeof(struct fastrpc_ioctl_invoke_attrs); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3785 | /* fall through */ |
| 3786 | case FASTRPC_IOCTL_INVOKE_CRC: |
| 3787 | if (!size) |
| 3788 | size = sizeof(struct fastrpc_ioctl_invoke_crc); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3789 | K_COPY_FROM_USER(err, 0, &p.inv, param, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3790 | if (err) |
| 3791 | goto bail; |
| 3792 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode, |
| 3793 | 0, &p.inv))); |
| 3794 | if (err) |
| 3795 | goto bail; |
| 3796 | break; |
| 3797 | case FASTRPC_IOCTL_MMAP: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3798 | K_COPY_FROM_USER(err, 0, &p.mmap, param, |
| 3799 | sizeof(p.mmap)); |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 3800 | if (err) |
| 3801 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3802 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p.mmap))); |
| 3803 | if (err) |
| 3804 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3805 | K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3806 | if (err) |
| 3807 | goto bail; |
| 3808 | break; |
| 3809 | case FASTRPC_IOCTL_MUNMAP: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3810 | K_COPY_FROM_USER(err, 0, &p.munmap, param, |
| 3811 | sizeof(p.munmap)); |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 3812 | if (err) |
| 3813 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3814 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl, |
| 3815 | &p.munmap))); |
| 3816 | if (err) |
| 3817 | goto bail; |
| 3818 | break; |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3819 | case FASTRPC_IOCTL_MMAP_64: |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3820 | K_COPY_FROM_USER(err, 0, &p.mmap64, param, |
| 3821 | sizeof(p.mmap64)); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3822 | if (err) |
| 3823 | goto bail; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3824 | get_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap); |
| 3825 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap))); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3826 | if (err) |
| 3827 | goto bail; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3828 | put_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap); |
| 3829 | K_COPY_TO_USER(err, 0, param, &p.mmap64, sizeof(p.mmap64)); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3830 | if (err) |
| 3831 | goto bail; |
| 3832 | break; |
| 3833 | case FASTRPC_IOCTL_MUNMAP_64: |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3834 | K_COPY_FROM_USER(err, 0, &p.munmap64, param, |
| 3835 | sizeof(p.munmap64)); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3836 | if (err) |
| 3837 | goto bail; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3838 | get_fastrpc_ioctl_munmap_64(&p.munmap64, &i.munmap); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3839 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl, |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3840 | &i.munmap))); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3841 | if (err) |
| 3842 | goto bail; |
| 3843 | break; |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 3844 | case FASTRPC_IOCTL_MUNMAP_FD: |
| 3845 | K_COPY_FROM_USER(err, 0, &p.munmap_fd, param, |
| 3846 | sizeof(p.munmap_fd)); |
| 3847 | if (err) |
| 3848 | goto bail; |
| 3849 | VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl, |
| 3850 | &p.munmap_fd))); |
| 3851 | if (err) |
| 3852 | goto bail; |
| 3853 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3854 | case FASTRPC_IOCTL_SETMODE: |
| 3855 | switch ((uint32_t)ioctl_param) { |
| 3856 | case FASTRPC_MODE_PARALLEL: |
| 3857 | case FASTRPC_MODE_SERIAL: |
| 3858 | fl->mode = (uint32_t)ioctl_param; |
| 3859 | break; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3860 | case FASTRPC_MODE_PROFILE: |
| 3861 | fl->profile = (uint32_t)ioctl_param; |
| 3862 | break; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 3863 | case FASTRPC_MODE_SESSION: |
| 3864 | fl->sessionid = 1; |
| 3865 | fl->tgid |= (1 << SESSION_ID_INDEX); |
| 3866 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3867 | default: |
| 3868 | err = -ENOTTY; |
| 3869 | break; |
| 3870 | } |
| 3871 | break; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3872 | case FASTRPC_IOCTL_GETPERF: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3873 | K_COPY_FROM_USER(err, 0, &p.perf, |
| 3874 | param, sizeof(p.perf)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3875 | if (err) |
| 3876 | goto bail; |
| 3877 | p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t); |
| 3878 | if (p.perf.keys) { |
| 3879 | char *keys = PERF_KEYS; |
| 3880 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3881 | K_COPY_TO_USER(err, 0, (void *)p.perf.keys, |
| 3882 | keys, strlen(keys)+1); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3883 | if (err) |
| 3884 | goto bail; |
| 3885 | } |
| 3886 | if (p.perf.data) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3887 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
| 3888 | struct hlist_node *n = NULL; |
| 3889 | |
| 3890 | mutex_lock(&fl->perf_mutex); |
| 3891 | hlist_for_each_entry_safe(perf, n, &fl->perf, hn) { |
| 3892 | if (perf->tid == current->pid) { |
| 3893 | fperf = perf; |
| 3894 | break; |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | mutex_unlock(&fl->perf_mutex); |
| 3899 | |
| 3900 | if (fperf) { |
| 3901 | K_COPY_TO_USER(err, 0, (void *)p.perf.data, |
| 3902 | fperf, sizeof(*fperf)); |
| 3903 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3904 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3905 | K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3906 | if (err) |
| 3907 | goto bail; |
| 3908 | break; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3909 | case FASTRPC_IOCTL_CONTROL: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3910 | K_COPY_FROM_USER(err, 0, &p.cp, param, |
| 3911 | sizeof(p.cp)); |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3912 | if (err) |
| 3913 | goto bail; |
| 3914 | VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp))); |
| 3915 | if (err) |
| 3916 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3917 | if (p.cp.req == FASTRPC_CONTROL_KALLOC) { |
| 3918 | K_COPY_TO_USER(err, 0, param, &p.cp, sizeof(p.cp)); |
| 3919 | if (err) |
| 3920 | goto bail; |
| 3921 | } |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3922 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3923 | case FASTRPC_IOCTL_GETINFO: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3924 | K_COPY_FROM_USER(err, 0, &info, param, sizeof(info)); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3925 | if (err) |
| 3926 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3927 | VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info))); |
| 3928 | if (err) |
| 3929 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3930 | K_COPY_TO_USER(err, 0, param, &info, sizeof(info)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3931 | if (err) |
| 3932 | goto bail; |
| 3933 | break; |
| 3934 | case FASTRPC_IOCTL_INIT: |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 3935 | p.init.attrs = 0; |
| 3936 | p.init.siglen = 0; |
| 3937 | size = sizeof(struct fastrpc_ioctl_init); |
| 3938 | /* fall through */ |
| 3939 | case FASTRPC_IOCTL_INIT_ATTRS: |
| 3940 | if (!size) |
| 3941 | size = sizeof(struct fastrpc_ioctl_init_attrs); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3942 | K_COPY_FROM_USER(err, 0, &p.init, param, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3943 | if (err) |
| 3944 | goto bail; |
Tharun Kumar Merugu | 4ea0eac | 2017-08-22 11:42:51 +0530 | [diff] [blame] | 3945 | VERIFY(err, p.init.init.filelen >= 0 && |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 3946 | p.init.init.filelen < INIT_FILELEN_MAX); |
| 3947 | if (err) |
| 3948 | goto bail; |
| 3949 | VERIFY(err, p.init.init.memlen >= 0 && |
| 3950 | p.init.init.memlen < INIT_MEMLEN_MAX); |
Tharun Kumar Merugu | 4ea0eac | 2017-08-22 11:42:51 +0530 | [diff] [blame] | 3951 | if (err) |
| 3952 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3953 | VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3954 | if (err) |
| 3955 | goto bail; |
| 3956 | break; |
| 3957 | |
| 3958 | default: |
| 3959 | err = -ENOTTY; |
| 3960 | pr_info("bad ioctl: %d\n", ioctl_num); |
| 3961 | break; |
| 3962 | } |
| 3963 | bail: |
| 3964 | return err; |
| 3965 | } |
| 3966 | |
| 3967 | static int fastrpc_restart_notifier_cb(struct notifier_block *nb, |
| 3968 | unsigned long code, |
| 3969 | void *data) |
| 3970 | { |
| 3971 | struct fastrpc_apps *me = &gfa; |
| 3972 | struct fastrpc_channel_ctx *ctx; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3973 | struct notif_data *notifdata = data; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3974 | int cid; |
| 3975 | |
| 3976 | ctx = container_of(nb, struct fastrpc_channel_ctx, nb); |
| 3977 | cid = ctx - &me->channel[0]; |
| 3978 | if (code == SUBSYS_BEFORE_SHUTDOWN) { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3979 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3980 | ctx->ssrcount++; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3981 | ctx->issubsystemup = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3982 | if (ctx->chan) { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3983 | if (me->glink) |
| 3984 | fastrpc_glink_close(ctx->chan, cid); |
| 3985 | else |
| 3986 | smd_close(ctx->chan); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3987 | ctx->chan = NULL; |
| 3988 | pr_info("'restart notifier: closed /dev/%s c %d %d'\n", |
| 3989 | gcinfo[cid].name, MAJOR(me->dev_no), cid); |
| 3990 | } |
| 3991 | mutex_unlock(&me->smd_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3992 | if (cid == 0) |
| 3993 | me->staticpd_flags = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3994 | fastrpc_notify_drivers(me, cid); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3995 | } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) { |
| 3996 | if (me->channel[0].remoteheap_ramdump_dev && |
| 3997 | notifdata->enable_ramdump) { |
| 3998 | me->channel[0].ramdumpenabled = 1; |
| 3999 | } |
| 4000 | } else if (code == SUBSYS_AFTER_POWERUP) { |
| 4001 | ctx->issubsystemup = 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | return NOTIFY_DONE; |
| 4005 | } |
| 4006 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4007 | static int fastrpc_pdr_notifier_cb(struct notifier_block *pdrnb, |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4008 | unsigned long code, |
| 4009 | void *data) |
| 4010 | { |
| 4011 | struct fastrpc_apps *me = &gfa; |
| 4012 | struct fastrpc_static_pd *spd; |
| 4013 | struct notif_data *notifdata = data; |
| 4014 | |
| 4015 | spd = container_of(pdrnb, struct fastrpc_static_pd, pdrnb); |
| 4016 | if (code == SERVREG_NOTIF_SERVICE_STATE_DOWN_V01) { |
| 4017 | mutex_lock(&me->smd_mutex); |
| 4018 | spd->pdrcount++; |
| 4019 | spd->ispdup = 0; |
| 4020 | pr_info("ADSPRPC: Audio PDR notifier %d %s\n", |
| 4021 | MAJOR(me->dev_no), spd->spdname); |
| 4022 | mutex_unlock(&me->smd_mutex); |
| 4023 | if (!strcmp(spd->spdname, |
| 4024 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME)) |
| 4025 | me->staticpd_flags = 0; |
| 4026 | fastrpc_notify_pdr_drivers(me, spd->spdname); |
| 4027 | } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) { |
| 4028 | if (me->channel[0].remoteheap_ramdump_dev && |
| 4029 | notifdata->enable_ramdump) { |
| 4030 | me->channel[0].ramdumpenabled = 1; |
| 4031 | } |
| 4032 | } else if (code == SERVREG_NOTIF_SERVICE_STATE_UP_V01) { |
| 4033 | spd->ispdup = 1; |
| 4034 | } |
| 4035 | |
| 4036 | return NOTIFY_DONE; |
| 4037 | } |
| 4038 | |
| 4039 | static int fastrpc_get_service_location_notify(struct notifier_block *nb, |
Vamsi krishna Gattupalli | 4c11c60 | 2020-08-25 19:34:14 +0530 | [diff] [blame] | 4040 | unsigned long opcode, void *data) |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4041 | { |
| 4042 | struct fastrpc_static_pd *spd; |
| 4043 | struct pd_qmi_client_data *pdr = data; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4044 | int curr_state = 0, i = 0; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4045 | |
| 4046 | spd = container_of(nb, struct fastrpc_static_pd, get_service_nb); |
| 4047 | if (opcode == LOCATOR_DOWN) { |
| 4048 | pr_err("ADSPRPC: Audio PD restart notifier locator down\n"); |
| 4049 | return NOTIFY_DONE; |
| 4050 | } |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4051 | for (i = 0; i < pdr->total_domains; i++) { |
Vamsi krishna Gattupalli | 4c11c60 | 2020-08-25 19:34:14 +0530 | [diff] [blame] | 4052 | if ((!strcmp(spd->spdname, "audio_pdr_adsprpc")) |
| 4053 | && (!strcmp(pdr->domain_list[i].name, |
| 4054 | "msm/adsp/audio_pd"))) { |
| 4055 | goto pdr_register; |
| 4056 | } else if ((!strcmp(spd->spdname, "sensors_pdr_adsprpc")) |
| 4057 | && (!strcmp(pdr->domain_list[i].name, |
| 4058 | "msm/adsp/sensor_pd"))) { |
| 4059 | goto pdr_register; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4060 | } |
| 4061 | } |
Vamsi krishna Gattupalli | 4c11c60 | 2020-08-25 19:34:14 +0530 | [diff] [blame] | 4062 | return NOTIFY_DONE; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4063 | |
Vamsi krishna Gattupalli | 4c11c60 | 2020-08-25 19:34:14 +0530 | [diff] [blame] | 4064 | pdr_register: |
| 4065 | if (!spd->pdrhandle) { |
| 4066 | spd->pdrhandle = |
| 4067 | service_notif_register_notifier( |
| 4068 | pdr->domain_list[i].name, |
| 4069 | pdr->domain_list[i].instance_id, |
| 4070 | &spd->pdrnb, &curr_state); |
| 4071 | } else { |
| 4072 | pr_err("ADSPRPC: %s is already registered\n", spd->spdname); |
| 4073 | } |
| 4074 | |
| 4075 | if (IS_ERR(spd->pdrhandle)) |
| 4076 | pr_err("ADSPRPC: Unable to register notifier\n"); |
| 4077 | |
| 4078 | if (curr_state == SERVREG_NOTIF_SERVICE_STATE_UP_V01) { |
| 4079 | pr_info("ADSPRPC: %s is up\n", spd->spdname); |
| 4080 | spd->ispdup = 1; |
| 4081 | } else if (curr_state == SERVREG_NOTIF_SERVICE_STATE_UNINIT_V01) { |
| 4082 | pr_info("ADSPRPC: %s is uninitialzed\n", spd->spdname); |
| 4083 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4084 | return NOTIFY_DONE; |
| 4085 | } |
| 4086 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4087 | static const struct file_operations fops = { |
| 4088 | .open = fastrpc_device_open, |
| 4089 | .release = fastrpc_device_release, |
| 4090 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 4091 | .compat_ioctl = compat_fastrpc_device_ioctl, |
| 4092 | }; |
| 4093 | |
| 4094 | static const struct of_device_id fastrpc_match_table[] = { |
| 4095 | { .compatible = "qcom,msm-fastrpc-adsp", }, |
| 4096 | { .compatible = "qcom,msm-fastrpc-compute", }, |
| 4097 | { .compatible = "qcom,msm-fastrpc-compute-cb", }, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4098 | { .compatible = "qcom,msm-fastrpc-legacy-compute", }, |
| 4099 | { .compatible = "qcom,msm-fastrpc-legacy-compute-cb", }, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4100 | { .compatible = "qcom,msm-adsprpc-mem-region", }, |
| 4101 | {} |
| 4102 | }; |
| 4103 | |
| 4104 | static int fastrpc_cb_probe(struct device *dev) |
| 4105 | { |
| 4106 | struct fastrpc_channel_ctx *chan; |
| 4107 | struct fastrpc_session_ctx *sess; |
| 4108 | struct of_phandle_args iommuspec; |
| 4109 | const char *name; |
| 4110 | unsigned int start = 0x80000000; |
| 4111 | int err = 0, i; |
| 4112 | int secure_vmid = VMID_CP_PIXEL; |
| 4113 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4114 | VERIFY(err, NULL != (name = of_get_property(dev->of_node, |
| 4115 | "label", NULL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4116 | if (err) |
| 4117 | goto bail; |
| 4118 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 4119 | if (!gcinfo[i].name) |
| 4120 | continue; |
| 4121 | if (!strcmp(name, gcinfo[i].name)) |
| 4122 | break; |
| 4123 | } |
| 4124 | VERIFY(err, i < NUM_CHANNELS); |
| 4125 | if (err) |
| 4126 | goto bail; |
| 4127 | chan = &gcinfo[i]; |
| 4128 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 4129 | if (err) |
| 4130 | goto bail; |
| 4131 | |
| 4132 | VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus", |
| 4133 | "#iommu-cells", 0, &iommuspec)); |
| 4134 | if (err) |
| 4135 | goto bail; |
| 4136 | sess = &chan->session[chan->sesscount]; |
| 4137 | sess->smmu.cb = iommuspec.args[0] & 0xf; |
| 4138 | sess->used = 0; |
| 4139 | sess->smmu.coherent = of_property_read_bool(dev->of_node, |
| 4140 | "dma-coherent"); |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4141 | sess->smmu.sharedcb = of_property_read_bool(dev->of_node, |
| 4142 | "shared-cb"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4143 | sess->smmu.secure = of_property_read_bool(dev->of_node, |
| 4144 | "qcom,secure-context-bank"); |
| 4145 | if (sess->smmu.secure) |
| 4146 | start = 0x60000000; |
| 4147 | VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping = |
| 4148 | arm_iommu_create_mapping(&platform_bus_type, |
Mohammed Nayeem Ur Rahman | 62f7f9c | 2020-04-13 11:16:19 +0530 | [diff] [blame] | 4149 | start, MAX_SIZE_LIMIT))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4150 | if (err) |
| 4151 | goto bail; |
| 4152 | |
| 4153 | if (sess->smmu.secure) |
| 4154 | iommu_domain_set_attr(sess->smmu.mapping->domain, |
| 4155 | DOMAIN_ATTR_SECURE_VMID, |
| 4156 | &secure_vmid); |
| 4157 | |
| 4158 | VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping)); |
| 4159 | if (err) |
| 4160 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4161 | sess->smmu.dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4162 | sess->smmu.enabled = 1; |
| 4163 | chan->sesscount++; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 4164 | debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root, |
| 4165 | NULL, &debugfs_fops); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4166 | bail: |
| 4167 | return err; |
| 4168 | } |
| 4169 | |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4170 | static int fastrpc_cb_legacy_probe(struct device *dev) |
| 4171 | { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4172 | struct fastrpc_channel_ctx *chan; |
| 4173 | struct fastrpc_session_ctx *first_sess = NULL, *sess = NULL; |
| 4174 | const char *name; |
| 4175 | unsigned int *sids = NULL, sids_size = 0; |
| 4176 | int err = 0, ret = 0, i; |
| 4177 | |
| 4178 | unsigned int start = 0x80000000; |
| 4179 | |
| 4180 | VERIFY(err, NULL != (name = of_get_property(dev->of_node, |
| 4181 | "label", NULL))); |
| 4182 | if (err) |
| 4183 | goto bail; |
| 4184 | |
| 4185 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 4186 | if (!gcinfo[i].name) |
| 4187 | continue; |
| 4188 | if (!strcmp(name, gcinfo[i].name)) |
| 4189 | break; |
| 4190 | } |
| 4191 | VERIFY(err, i < NUM_CHANNELS); |
| 4192 | if (err) |
| 4193 | goto bail; |
| 4194 | |
| 4195 | chan = &gcinfo[i]; |
| 4196 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 4197 | if (err) |
| 4198 | goto bail; |
| 4199 | |
| 4200 | first_sess = &chan->session[chan->sesscount]; |
| 4201 | |
| 4202 | VERIFY(err, NULL != of_get_property(dev->of_node, |
| 4203 | "sids", &sids_size)); |
| 4204 | if (err) |
| 4205 | goto bail; |
| 4206 | |
| 4207 | VERIFY(err, NULL != (sids = kzalloc(sids_size, GFP_KERNEL))); |
| 4208 | if (err) |
| 4209 | goto bail; |
| 4210 | ret = of_property_read_u32_array(dev->of_node, "sids", sids, |
| 4211 | sids_size/sizeof(unsigned int)); |
| 4212 | if (ret) |
| 4213 | goto bail; |
| 4214 | |
| 4215 | VERIFY(err, !IS_ERR_OR_NULL(first_sess->smmu.mapping = |
| 4216 | arm_iommu_create_mapping(&platform_bus_type, |
| 4217 | start, 0x78000000))); |
| 4218 | if (err) |
| 4219 | goto bail; |
| 4220 | |
| 4221 | VERIFY(err, !arm_iommu_attach_device(dev, first_sess->smmu.mapping)); |
| 4222 | if (err) |
| 4223 | goto bail; |
| 4224 | |
| 4225 | |
| 4226 | for (i = 0; i < sids_size/sizeof(unsigned int); i++) { |
| 4227 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 4228 | if (err) |
| 4229 | goto bail; |
| 4230 | sess = &chan->session[chan->sesscount]; |
| 4231 | sess->smmu.cb = sids[i]; |
| 4232 | sess->smmu.dev = dev; |
| 4233 | sess->smmu.mapping = first_sess->smmu.mapping; |
| 4234 | sess->smmu.enabled = 1; |
| 4235 | sess->used = 0; |
| 4236 | sess->smmu.coherent = false; |
| 4237 | sess->smmu.secure = false; |
| 4238 | chan->sesscount++; |
| 4239 | } |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4240 | bail: |
| 4241 | kfree(sids); |
| 4242 | return err; |
| 4243 | } |
| 4244 | |
| 4245 | |
| 4246 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 4247 | static void init_secure_vmid_list(struct device *dev, char *prop_name, |
| 4248 | struct secure_vm *destvm) |
| 4249 | { |
| 4250 | int err = 0; |
| 4251 | u32 len = 0, i = 0; |
| 4252 | u32 *rhvmlist = NULL; |
| 4253 | u32 *rhvmpermlist = NULL; |
| 4254 | |
| 4255 | if (!of_find_property(dev->of_node, prop_name, &len)) |
| 4256 | goto bail; |
| 4257 | if (len == 0) |
| 4258 | goto bail; |
| 4259 | len /= sizeof(u32); |
| 4260 | VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL))); |
| 4261 | if (err) |
| 4262 | goto bail; |
| 4263 | VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32), |
| 4264 | GFP_KERNEL))); |
| 4265 | if (err) |
| 4266 | goto bail; |
| 4267 | for (i = 0; i < len; i++) { |
| 4268 | err = of_property_read_u32_index(dev->of_node, prop_name, i, |
| 4269 | &rhvmlist[i]); |
| 4270 | rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC; |
| 4271 | pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]); |
| 4272 | if (err) { |
| 4273 | pr_err("ADSPRPC: Failed to read VMID\n"); |
| 4274 | goto bail; |
| 4275 | } |
| 4276 | } |
| 4277 | destvm->vmid = rhvmlist; |
| 4278 | destvm->vmperm = rhvmpermlist; |
| 4279 | destvm->vmcount = len; |
| 4280 | bail: |
| 4281 | if (err) { |
| 4282 | kfree(rhvmlist); |
| 4283 | kfree(rhvmpermlist); |
| 4284 | } |
| 4285 | } |
| 4286 | |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4287 | static void configure_secure_channels(uint32_t secure_domains) |
| 4288 | { |
| 4289 | struct fastrpc_apps *me = &gfa; |
| 4290 | int ii = 0; |
| 4291 | /* |
| 4292 | * secure_domains contains the bitmask of the secure channels |
| 4293 | * Bit 0 - ADSP |
| 4294 | * Bit 1 - MDSP |
| 4295 | * Bit 2 - SLPI |
| 4296 | * Bit 3 - CDSP |
| 4297 | */ |
| 4298 | for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) { |
| 4299 | int secure = (secure_domains >> ii) & 0x01; |
| 4300 | |
| 4301 | me->channel[ii].secure = secure; |
| 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4306 | static int fastrpc_probe(struct platform_device *pdev) |
| 4307 | { |
| 4308 | int err = 0; |
| 4309 | struct fastrpc_apps *me = &gfa; |
| 4310 | struct device *dev = &pdev->dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4311 | struct device_node *ion_node, *node; |
| 4312 | struct platform_device *ion_pdev; |
| 4313 | struct cma *cma; |
| 4314 | uint32_t val; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4315 | int ret = 0; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4316 | uint32_t secure_domains; |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4317 | |
| 4318 | if (of_device_is_compatible(dev->of_node, |
| 4319 | "qcom,msm-fastrpc-compute")) { |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 4320 | init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid", |
| 4321 | &gcinfo[0].rhvm); |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4322 | |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4323 | |
| 4324 | of_property_read_u32(dev->of_node, "qcom,rpc-latency-us", |
| 4325 | &me->latency); |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4326 | if (of_get_property(dev->of_node, |
| 4327 | "qcom,secure-domains", NULL) != NULL) { |
| 4328 | VERIFY(err, !of_property_read_u32(dev->of_node, |
| 4329 | "qcom,secure-domains", |
| 4330 | &secure_domains)); |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4331 | if (!err) { |
| 4332 | me->secure_flag = true; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4333 | configure_secure_channels(secure_domains); |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4334 | } else { |
| 4335 | me->secure_flag = false; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4336 | pr_info("adsprpc: unable to read the domain configuration from dts\n"); |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4337 | } |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4338 | } |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4339 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4340 | if (of_device_is_compatible(dev->of_node, |
| 4341 | "qcom,msm-fastrpc-compute-cb")) |
| 4342 | return fastrpc_cb_probe(dev); |
| 4343 | |
| 4344 | if (of_device_is_compatible(dev->of_node, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4345 | "qcom,msm-fastrpc-legacy-compute")) { |
| 4346 | me->glink = false; |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4347 | me->legacy = 1; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | if (of_device_is_compatible(dev->of_node, |
| 4351 | "qcom,msm-fastrpc-legacy-compute-cb")){ |
| 4352 | return fastrpc_cb_legacy_probe(dev); |
| 4353 | } |
| 4354 | |
| 4355 | if (of_device_is_compatible(dev->of_node, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4356 | "qcom,msm-adsprpc-mem-region")) { |
| 4357 | me->dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4358 | ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion"); |
| 4359 | if (ion_node) { |
| 4360 | for_each_available_child_of_node(ion_node, node) { |
| 4361 | if (of_property_read_u32(node, "reg", &val)) |
| 4362 | continue; |
| 4363 | if (val != ION_ADSP_HEAP_ID) |
| 4364 | continue; |
| 4365 | ion_pdev = of_find_device_by_node(node); |
| 4366 | if (!ion_pdev) |
| 4367 | break; |
| 4368 | cma = dev_get_cma_area(&ion_pdev->dev); |
| 4369 | if (cma) { |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4370 | me->range.addr = cma_get_base(cma); |
| 4371 | me->range.size = |
| 4372 | (size_t)cma_get_size(cma); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4373 | } |
| 4374 | break; |
| 4375 | } |
| 4376 | } |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4377 | if (me->range.addr && !of_property_read_bool(dev->of_node, |
Tharun Kumar Merugu | 6b7a4a2 | 2018-01-17 16:08:07 +0530 | [diff] [blame] | 4378 | "restrict-access")) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4379 | int srcVM[1] = {VMID_HLOS}; |
| 4380 | int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6, |
| 4381 | VMID_ADSP_Q6}; |
Sathish Ambley | 84d1186 | 2017-05-15 14:36:05 -0700 | [diff] [blame] | 4382 | int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4383 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 4384 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 4385 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 4386 | }; |
| 4387 | |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4388 | VERIFY(err, !hyp_assign_phys(me->range.addr, |
| 4389 | me->range.size, srcVM, 1, |
| 4390 | destVM, destVMperm, 4)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4391 | if (err) |
| 4392 | goto bail; |
| 4393 | } |
| 4394 | return 0; |
| 4395 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4396 | if (of_property_read_bool(dev->of_node, |
| 4397 | "qcom,fastrpc-adsp-audio-pdr")) { |
| 4398 | int session; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4399 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4400 | VERIFY(err, !fastrpc_get_adsp_session( |
| 4401 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session)); |
| 4402 | if (err) |
| 4403 | goto spdbail; |
| 4404 | me->channel[0].spd[session].get_service_nb.notifier_call = |
| 4405 | fastrpc_get_service_location_notify; |
| 4406 | ret = get_service_location( |
| 4407 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, |
| 4408 | AUDIO_PDR_ADSP_SERVICE_NAME, |
| 4409 | &me->channel[0].spd[session].get_service_nb); |
| 4410 | if (ret) |
| 4411 | pr_err("ADSPRPC: Get service location failed: %d\n", |
| 4412 | ret); |
| 4413 | } |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4414 | if (of_property_read_bool(dev->of_node, |
| 4415 | "qcom,fastrpc-adsp-sensors-pdr")) { |
| 4416 | int session; |
| 4417 | |
| 4418 | VERIFY(err, !fastrpc_get_adsp_session( |
| 4419 | SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, &session)); |
| 4420 | if (err) |
| 4421 | goto spdbail; |
| 4422 | me->channel[0].spd[session].get_service_nb.notifier_call = |
| 4423 | fastrpc_get_service_location_notify; |
| 4424 | ret = get_service_location( |
| 4425 | SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, |
| 4426 | SENSORS_PDR_ADSP_SERVICE_NAME, |
| 4427 | &me->channel[0].spd[session].get_service_nb); |
| 4428 | if (ret) |
| 4429 | pr_err("ADSPRPC: Get service location failed: %d\n", |
| 4430 | ret); |
| 4431 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4432 | spdbail: |
| 4433 | err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4434 | VERIFY(err, !of_platform_populate(pdev->dev.of_node, |
| 4435 | fastrpc_match_table, |
| 4436 | NULL, &pdev->dev)); |
| 4437 | if (err) |
| 4438 | goto bail; |
| 4439 | bail: |
| 4440 | return err; |
| 4441 | } |
| 4442 | |
| 4443 | static void fastrpc_deinit(void) |
| 4444 | { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 4445 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4446 | struct fastrpc_channel_ctx *chan = gcinfo; |
| 4447 | int i, j; |
| 4448 | |
| 4449 | for (i = 0; i < NUM_CHANNELS; i++, chan++) { |
| 4450 | if (chan->chan) { |
| 4451 | kref_put_mutex(&chan->kref, |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 4452 | fastrpc_channel_close, &me->smd_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4453 | chan->chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4454 | } |
| 4455 | for (j = 0; j < NUM_SESSIONS; j++) { |
| 4456 | struct fastrpc_session_ctx *sess = &chan->session[j]; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4457 | if (sess->smmu.dev) { |
| 4458 | arm_iommu_detach_device(sess->smmu.dev); |
| 4459 | sess->smmu.dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4460 | } |
| 4461 | if (sess->smmu.mapping) { |
| 4462 | arm_iommu_release_mapping(sess->smmu.mapping); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4463 | sess->smmu.mapping = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4464 | } |
| 4465 | } |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 4466 | kfree(chan->rhvm.vmid); |
| 4467 | kfree(chan->rhvm.vmperm); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | static struct platform_driver fastrpc_driver = { |
| 4472 | .probe = fastrpc_probe, |
| 4473 | .driver = { |
| 4474 | .name = "fastrpc", |
| 4475 | .owner = THIS_MODULE, |
| 4476 | .of_match_table = fastrpc_match_table, |
| 4477 | }, |
| 4478 | }; |
| 4479 | |
| 4480 | static int __init fastrpc_device_init(void) |
| 4481 | { |
| 4482 | struct fastrpc_apps *me = &gfa; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4483 | struct device *dev = NULL; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4484 | struct device *secure_dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4485 | int err = 0, i; |
| 4486 | |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 4487 | debugfs_root = debugfs_create_dir("adsprpc", NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4488 | memset(me, 0, sizeof(*me)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4489 | fastrpc_init(me); |
| 4490 | me->dev = NULL; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4491 | me->glink = true; |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4492 | me->secure_flag = false; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4493 | VERIFY(err, 0 == platform_driver_register(&fastrpc_driver)); |
| 4494 | if (err) |
| 4495 | goto register_bail; |
| 4496 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS, |
| 4497 | DEVICE_NAME)); |
| 4498 | if (err) |
| 4499 | goto alloc_chrdev_bail; |
| 4500 | cdev_init(&me->cdev, &fops); |
| 4501 | me->cdev.owner = THIS_MODULE; |
| 4502 | VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0), |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4503 | NUM_DEVICES)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4504 | if (err) |
| 4505 | goto cdev_init_bail; |
| 4506 | me->class = class_create(THIS_MODULE, "fastrpc"); |
| 4507 | VERIFY(err, !IS_ERR(me->class)); |
| 4508 | if (err) |
| 4509 | goto class_create_bail; |
| 4510 | me->compat = (fops.compat_ioctl == NULL) ? 0 : 1; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4511 | |
| 4512 | /* |
| 4513 | * Create devices and register with sysfs |
| 4514 | * Create first device with minor number 0 |
| 4515 | */ |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4516 | dev = device_create(me->class, NULL, |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4517 | MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV), |
| 4518 | NULL, DEVICE_NAME); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4519 | VERIFY(err, !IS_ERR_OR_NULL(dev)); |
| 4520 | if (err) |
| 4521 | goto device_create_bail; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4522 | |
| 4523 | /* Create secure device with minor number for secure device */ |
| 4524 | secure_dev = device_create(me->class, NULL, |
| 4525 | MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV), |
| 4526 | NULL, DEVICE_NAME_SECURE); |
| 4527 | VERIFY(err, !IS_ERR_OR_NULL(secure_dev)); |
| 4528 | if (err) |
| 4529 | goto device_create_bail; |
| 4530 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4531 | for (i = 0; i < NUM_CHANNELS; i++) { |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4532 | me->channel[i].dev = secure_dev; |
| 4533 | if (i == CDSP_DOMAIN_ID) |
| 4534 | me->channel[i].dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4535 | me->channel[i].ssrcount = 0; |
| 4536 | me->channel[i].prevssrcount = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4537 | me->channel[i].issubsystemup = 1; |
| 4538 | me->channel[i].ramdumpenabled = 0; |
| 4539 | me->channel[i].remoteheap_ramdump_dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4540 | me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb; |
| 4541 | me->channel[i].handle = subsys_notif_register_notifier( |
| 4542 | gcinfo[i].subsys, |
| 4543 | &me->channel[i].nb); |
| 4544 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4545 | me->client = msm_ion_client_create(DEVICE_NAME); |
| 4546 | VERIFY(err, !IS_ERR_OR_NULL(me->client)); |
| 4547 | if (err) |
| 4548 | goto device_create_bail; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 4549 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4550 | return 0; |
| 4551 | device_create_bail: |
| 4552 | for (i = 0; i < NUM_CHANNELS; i++) { |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4553 | if (me->channel[i].handle) |
| 4554 | subsys_notif_unregister_notifier(me->channel[i].handle, |
| 4555 | &me->channel[i].nb); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4556 | } |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4557 | if (!IS_ERR_OR_NULL(dev)) |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4558 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), |
| 4559 | MINOR_NUM_DEV)); |
| 4560 | if (!IS_ERR_OR_NULL(secure_dev)) |
| 4561 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), |
| 4562 | MINOR_NUM_SECURE_DEV)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4563 | class_destroy(me->class); |
| 4564 | class_create_bail: |
| 4565 | cdev_del(&me->cdev); |
| 4566 | cdev_init_bail: |
| 4567 | unregister_chrdev_region(me->dev_no, NUM_CHANNELS); |
| 4568 | alloc_chrdev_bail: |
| 4569 | register_bail: |
| 4570 | fastrpc_deinit(); |
| 4571 | return err; |
| 4572 | } |
| 4573 | |
| 4574 | static void __exit fastrpc_device_exit(void) |
| 4575 | { |
| 4576 | struct fastrpc_apps *me = &gfa; |
| 4577 | int i; |
| 4578 | |
| 4579 | fastrpc_file_list_dtor(me); |
| 4580 | fastrpc_deinit(); |
| 4581 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 4582 | if (!gcinfo[i].name) |
| 4583 | continue; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4584 | subsys_notif_unregister_notifier(me->channel[i].handle, |
| 4585 | &me->channel[i].nb); |
| 4586 | } |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4587 | |
| 4588 | /* Destroy the secure and non secure devices */ |
| 4589 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV)); |
| 4590 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), |
| 4591 | MINOR_NUM_SECURE_DEV)); |
| 4592 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4593 | class_destroy(me->class); |
| 4594 | cdev_del(&me->cdev); |
| 4595 | unregister_chrdev_region(me->dev_no, NUM_CHANNELS); |
| 4596 | ion_client_destroy(me->client); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 4597 | debugfs_remove_recursive(debugfs_root); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | late_initcall(fastrpc_device_init); |
| 4601 | module_exit(fastrpc_device_exit); |
| 4602 | |
| 4603 | MODULE_LICENSE("GPL v2"); |