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