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