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