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