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))); |
Mohammed Nayeem Ur Rahman | 80f45dc | 2019-09-23 19:35:19 +0530 | [diff] [blame^] | 2484 | if (err == AEE_EUNSUPPORTED) { |
| 2485 | remote_arg_t ra[1]; |
| 2486 | |
| 2487 | pr_warn("ADSPRPC:Failed to get security key with updated remote call, falling back to older method"); |
| 2488 | ra[0].buf.pv = (void *)&routargs; |
| 2489 | ra[0].buf.len = sizeof(routargs); |
| 2490 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 0, 1); |
| 2491 | ioctl.inv.pra = ra; |
| 2492 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2493 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
| 2494 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2495 | if (err) |
| 2496 | goto bail; |
Mohammed Nayeem Ur Rahman | 80f45dc | 2019-09-23 19:35:19 +0530 | [diff] [blame^] | 2497 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2498 | desc.args[0] = TZ_PIL_AUTH_QDSP6_PROC; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2499 | desc.args[1] = phys; |
| 2500 | desc.args[2] = size; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2501 | desc.args[3] = routargs.skey; |
| 2502 | desc.arginfo = SCM_ARGS(4); |
| 2503 | err = scm_call2(SCM_SIP_FNID(SCM_SVC_PIL, |
| 2504 | TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID), &desc); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2505 | } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 2506 | VERIFY(err, !hyp_assign_phys(phys, (uint64_t)size, |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 2507 | me->channel[fl->cid].rhvm.vmid, |
| 2508 | me->channel[fl->cid].rhvm.vmcount, |
| 2509 | destVM, destVMperm, 1)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2510 | if (err) |
| 2511 | goto bail; |
| 2512 | } |
| 2513 | |
| 2514 | bail: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2515 | return err; |
| 2516 | } |
| 2517 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2518 | static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr, |
| 2519 | uint64_t phys, size_t size, uint32_t flags) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2520 | { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2521 | struct fastrpc_ioctl_invoke_crc ioctl; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2522 | remote_arg_t ra[1]; |
| 2523 | int err = 0; |
| 2524 | struct { |
| 2525 | int pid; |
| 2526 | uintptr_t vaddrout; |
Tharun Kumar Merugu | fb51f94 | 2017-12-07 10:32:15 +0530 | [diff] [blame] | 2527 | size_t size; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2528 | } inargs; |
| 2529 | |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 2530 | inargs.pid = fl->tgid; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2531 | inargs.size = size; |
| 2532 | inargs.vaddrout = raddr; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2533 | ra[0].buf.pv = (void *)&inargs; |
| 2534 | ra[0].buf.len = sizeof(inargs); |
| 2535 | |
Tharun Kumar Merugu | cc2e11e | 2019-02-02 01:22:47 +0530 | [diff] [blame] | 2536 | ioctl.inv.handle = FASTRPC_STATIC_HANDLE_KERNEL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2537 | if (fl->apps->compat) |
| 2538 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0); |
| 2539 | else |
| 2540 | ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0); |
| 2541 | ioctl.inv.pra = ra; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2542 | ioctl.fds = NULL; |
| 2543 | ioctl.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 2544 | ioctl.crc = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2545 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, |
| 2546 | FASTRPC_MODE_PARALLEL, 1, &ioctl))); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2547 | if (err) |
| 2548 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2549 | if (flags == ADSP_MMAP_HEAP_ADDR || |
| 2550 | flags == ADSP_MMAP_REMOTE_HEAP_ADDR) { |
| 2551 | VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, phys, size, flags)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2552 | if (err) |
| 2553 | goto bail; |
| 2554 | } |
| 2555 | bail: |
| 2556 | return err; |
| 2557 | } |
| 2558 | |
| 2559 | static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl) |
| 2560 | { |
| 2561 | struct fastrpc_mmap *match = NULL, *map = NULL; |
| 2562 | struct hlist_node *n = NULL; |
| 2563 | int err = 0, ret = 0; |
| 2564 | struct fastrpc_apps *me = &gfa; |
| 2565 | struct ramdump_segment *ramdump_segments_rh = NULL; |
| 2566 | |
| 2567 | do { |
| 2568 | match = NULL; |
| 2569 | spin_lock(&me->hlock); |
| 2570 | hlist_for_each_entry_safe(map, n, &me->maps, hn) { |
| 2571 | match = map; |
| 2572 | hlist_del_init(&map->hn); |
| 2573 | break; |
| 2574 | } |
| 2575 | spin_unlock(&me->hlock); |
| 2576 | |
| 2577 | if (match) { |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2578 | VERIFY(err, !fastrpc_munmap_on_dsp_rh(fl, match->phys, |
| 2579 | match->size, match->flags)); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2580 | if (err) |
| 2581 | goto bail; |
| 2582 | if (me->channel[0].ramdumpenabled) { |
| 2583 | ramdump_segments_rh = kcalloc(1, |
| 2584 | sizeof(struct ramdump_segment), GFP_KERNEL); |
| 2585 | if (ramdump_segments_rh) { |
| 2586 | ramdump_segments_rh->address = |
| 2587 | match->phys; |
| 2588 | ramdump_segments_rh->size = match->size; |
| 2589 | ret = do_elf_ramdump( |
| 2590 | me->channel[0].remoteheap_ramdump_dev, |
| 2591 | ramdump_segments_rh, 1); |
| 2592 | if (ret < 0) |
| 2593 | pr_err("ADSPRPC: unable to dump heap"); |
| 2594 | kfree(ramdump_segments_rh); |
| 2595 | } |
| 2596 | } |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2597 | fastrpc_mmap_free(match, 0); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2598 | } |
| 2599 | } while (match); |
| 2600 | bail: |
| 2601 | if (err && match) |
| 2602 | fastrpc_mmap_add(match); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2603 | return err; |
| 2604 | } |
| 2605 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 2606 | static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl) |
| 2607 | { |
| 2608 | struct fastrpc_apps *me = &gfa; |
| 2609 | int session = 0, err = 0; |
| 2610 | |
| 2611 | VERIFY(err, !fastrpc_get_adsp_session( |
| 2612 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session)); |
| 2613 | if (err) |
| 2614 | goto bail; |
| 2615 | if (me->channel[fl->cid].spd[session].pdrcount != |
| 2616 | me->channel[fl->cid].spd[session].prevpdrcount) { |
| 2617 | if (fastrpc_mmap_remove_ssr(fl)) |
| 2618 | pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n"); |
| 2619 | me->channel[fl->cid].spd[session].prevpdrcount = |
| 2620 | me->channel[fl->cid].spd[session].pdrcount; |
| 2621 | } |
| 2622 | if (!me->channel[fl->cid].spd[session].ispdup) { |
| 2623 | VERIFY(err, 0); |
| 2624 | if (err) { |
| 2625 | err = -ENOTCONN; |
| 2626 | goto bail; |
| 2627 | } |
| 2628 | } |
| 2629 | bail: |
| 2630 | return err; |
| 2631 | } |
| 2632 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2633 | 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] | 2634 | size_t len, struct fastrpc_mmap **ppmap); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2635 | |
| 2636 | static void fastrpc_mmap_add(struct fastrpc_mmap *map); |
| 2637 | |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 2638 | static inline void get_fastrpc_ioctl_mmap_64( |
| 2639 | struct fastrpc_ioctl_mmap_64 *mmap64, |
| 2640 | struct fastrpc_ioctl_mmap *immap) |
| 2641 | { |
| 2642 | immap->fd = mmap64->fd; |
| 2643 | immap->flags = mmap64->flags; |
| 2644 | immap->vaddrin = (uintptr_t)mmap64->vaddrin; |
| 2645 | immap->size = mmap64->size; |
| 2646 | } |
| 2647 | |
| 2648 | static inline void put_fastrpc_ioctl_mmap_64( |
| 2649 | struct fastrpc_ioctl_mmap_64 *mmap64, |
| 2650 | struct fastrpc_ioctl_mmap *immap) |
| 2651 | { |
| 2652 | mmap64->vaddrout = (uint64_t)immap->vaddrout; |
| 2653 | } |
| 2654 | |
| 2655 | static inline void get_fastrpc_ioctl_munmap_64( |
| 2656 | struct fastrpc_ioctl_munmap_64 *munmap64, |
| 2657 | struct fastrpc_ioctl_munmap *imunmap) |
| 2658 | { |
| 2659 | imunmap->vaddrout = (uintptr_t)munmap64->vaddrout; |
| 2660 | imunmap->size = munmap64->size; |
| 2661 | } |
| 2662 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2663 | static int fastrpc_internal_munmap(struct fastrpc_file *fl, |
| 2664 | struct fastrpc_ioctl_munmap *ud) |
| 2665 | { |
| 2666 | int err = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2667 | struct fastrpc_mmap *map = NULL; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2668 | struct fastrpc_buf *rbuf = NULL, *free = NULL; |
| 2669 | struct hlist_node *n; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2670 | |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2671 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2672 | |
| 2673 | spin_lock(&fl->hlock); |
| 2674 | hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) { |
| 2675 | if (rbuf->raddr && (rbuf->flags == ADSP_MMAP_ADD_PAGES)) { |
| 2676 | if ((rbuf->raddr == ud->vaddrout) && |
| 2677 | (rbuf->size == ud->size)) { |
| 2678 | free = rbuf; |
| 2679 | break; |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | spin_unlock(&fl->hlock); |
| 2684 | |
| 2685 | if (free) { |
| 2686 | VERIFY(err, !fastrpc_munmap_on_dsp(fl, free->raddr, |
| 2687 | free->phys, free->size, free->flags)); |
| 2688 | if (err) |
| 2689 | goto bail; |
| 2690 | fastrpc_buf_free(rbuf, 0); |
| 2691 | mutex_unlock(&fl->map_mutex); |
| 2692 | return err; |
| 2693 | } |
| 2694 | |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2695 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2696 | VERIFY(err, !fastrpc_mmap_remove(fl, ud->vaddrout, ud->size, &map)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2697 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2698 | if (err) |
| 2699 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2700 | VERIFY(err, !fastrpc_munmap_on_dsp(fl, map->raddr, |
| 2701 | map->phys, map->size, map->flags)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2702 | if (err) |
| 2703 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2704 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2705 | fastrpc_mmap_free(map, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2706 | mutex_unlock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2707 | bail: |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2708 | if (err && map) { |
| 2709 | mutex_lock(&fl->fl_map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2710 | fastrpc_mmap_add(map); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2711 | mutex_unlock(&fl->fl_map_mutex); |
| 2712 | } |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2713 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2714 | return err; |
| 2715 | } |
| 2716 | |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2717 | static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl, |
| 2718 | struct fastrpc_ioctl_munmap_fd *ud) { |
| 2719 | int err = 0; |
| 2720 | struct fastrpc_mmap *map = NULL; |
| 2721 | |
| 2722 | VERIFY(err, (fl && ud)); |
| 2723 | if (err) |
| 2724 | goto bail; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2725 | mutex_lock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 09fc615 | 2018-02-16 13:13:12 +0530 | [diff] [blame] | 2726 | if (fastrpc_mmap_find(fl, ud->fd, ud->va, ud->len, 0, 0, &map)) { |
| 2727 | 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] | 2728 | ud->fd, (unsigned long long)ud->va, |
| 2729 | (unsigned int)ud->len); |
| 2730 | err = -1; |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2731 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2732 | goto bail; |
| 2733 | } |
| 2734 | if (map) |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2735 | fastrpc_mmap_free(map, 0); |
| 2736 | mutex_unlock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2737 | bail: |
| 2738 | return err; |
| 2739 | } |
| 2740 | |
| 2741 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2742 | static int fastrpc_internal_mmap(struct fastrpc_file *fl, |
| 2743 | struct fastrpc_ioctl_mmap *ud) |
| 2744 | { |
| 2745 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2746 | struct fastrpc_mmap *map = NULL; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2747 | struct fastrpc_buf *rbuf = NULL; |
| 2748 | unsigned long dma_attr = 0; |
| 2749 | uintptr_t raddr = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2750 | int err = 0; |
| 2751 | |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2752 | mutex_lock(&fl->map_mutex); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2753 | if (ud->flags == ADSP_MMAP_ADD_PAGES) { |
| 2754 | if (ud->vaddrin) { |
| 2755 | err = -EINVAL; |
| 2756 | pr_err("adsprpc: %s: %s: ERROR: adding user allocated pages is not supported\n", |
| 2757 | current->comm, __func__); |
| 2758 | goto bail; |
| 2759 | } |
| 2760 | dma_attr = DMA_ATTR_EXEC_MAPPING | |
| 2761 | DMA_ATTR_NO_KERNEL_MAPPING | |
| 2762 | DMA_ATTR_FORCE_NON_COHERENT; |
| 2763 | err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags, |
| 2764 | 1, &rbuf); |
| 2765 | if (err) |
| 2766 | goto bail; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2767 | err = fastrpc_mmap_on_dsp(fl, ud->flags, 0, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2768 | rbuf->phys, rbuf->size, &raddr); |
| 2769 | if (err) |
| 2770 | goto bail; |
| 2771 | rbuf->raddr = raddr; |
| 2772 | } else { |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2773 | |
| 2774 | uintptr_t va_to_dsp; |
| 2775 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2776 | mutex_lock(&fl->fl_map_mutex); |
| 2777 | if (!fastrpc_mmap_find(fl, ud->fd, (uintptr_t)ud->vaddrin, |
| 2778 | ud->size, ud->flags, 1, &map)) { |
| 2779 | mutex_unlock(&fl->fl_map_mutex); |
| 2780 | mutex_unlock(&fl->map_mutex); |
| 2781 | return 0; |
| 2782 | } |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2783 | |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2784 | VERIFY(err, !fastrpc_mmap_create(fl, ud->fd, 0, |
| 2785 | (uintptr_t)ud->vaddrin, ud->size, |
| 2786 | ud->flags, &map)); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2787 | mutex_unlock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2788 | if (err) |
| 2789 | goto bail; |
Tharun Kumar Merugu | 0d0b69e | 2018-09-14 22:30:58 +0530 | [diff] [blame] | 2790 | |
| 2791 | if (ud->flags == ADSP_MMAP_HEAP_ADDR || |
| 2792 | ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) |
| 2793 | va_to_dsp = 0; |
| 2794 | else |
| 2795 | va_to_dsp = (uintptr_t)map->va; |
| 2796 | 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] | 2797 | map->phys, map->size, &raddr)); |
| 2798 | if (err) |
| 2799 | goto bail; |
| 2800 | map->raddr = raddr; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2801 | } |
Mohammed Nayeem Ur Rahman | 3ac5d32 | 2018-09-24 13:54:08 +0530 | [diff] [blame] | 2802 | ud->vaddrout = raddr; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2803 | bail: |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2804 | if (err && map) { |
| 2805 | mutex_lock(&fl->fl_map_mutex); |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 2806 | fastrpc_mmap_free(map, 0); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2807 | mutex_unlock(&fl->fl_map_mutex); |
| 2808 | } |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 2809 | mutex_unlock(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2810 | return err; |
| 2811 | } |
| 2812 | |
| 2813 | static void fastrpc_channel_close(struct kref *kref) |
| 2814 | { |
| 2815 | struct fastrpc_apps *me = &gfa; |
| 2816 | struct fastrpc_channel_ctx *ctx; |
| 2817 | int cid; |
| 2818 | |
| 2819 | ctx = container_of(kref, struct fastrpc_channel_ctx, kref); |
| 2820 | cid = ctx - &gcinfo[0]; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 2821 | if (!me->glink) |
| 2822 | smd_close(ctx->chan); |
| 2823 | else |
| 2824 | fastrpc_glink_close(ctx->chan, cid); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2825 | ctx->chan = NULL; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2826 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2827 | pr_info("'closed /dev/%s c %d %d'\n", gcinfo[cid].name, |
| 2828 | MAJOR(me->dev_no), cid); |
| 2829 | } |
| 2830 | |
| 2831 | static void fastrpc_context_list_dtor(struct fastrpc_file *fl); |
| 2832 | |
| 2833 | static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan, |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2834 | int secure, int sharedcb, struct fastrpc_session_ctx **session) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2835 | { |
| 2836 | struct fastrpc_apps *me = &gfa; |
| 2837 | int idx = 0, err = 0; |
| 2838 | |
| 2839 | if (chan->sesscount) { |
| 2840 | for (idx = 0; idx < chan->sesscount; ++idx) { |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2841 | if ((sharedcb && chan->session[idx].smmu.sharedcb) || |
| 2842 | (!chan->session[idx].used && |
| 2843 | chan->session[idx].smmu.secure |
| 2844 | == secure && !sharedcb)) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2845 | chan->session[idx].used = 1; |
| 2846 | break; |
| 2847 | } |
| 2848 | } |
| 2849 | VERIFY(err, idx < chan->sesscount); |
| 2850 | if (err) |
| 2851 | goto bail; |
| 2852 | chan->session[idx].smmu.faults = 0; |
| 2853 | } else { |
| 2854 | VERIFY(err, me->dev != NULL); |
| 2855 | if (err) |
| 2856 | goto bail; |
| 2857 | chan->session[0].dev = me->dev; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2858 | chan->session[0].smmu.dev = me->dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | *session = &chan->session[idx]; |
| 2862 | bail: |
| 2863 | return err; |
| 2864 | } |
| 2865 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2866 | static bool fastrpc_glink_notify_rx_intent_req(void *h, const void *priv, |
| 2867 | size_t size) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2868 | { |
| 2869 | if (glink_queue_rx_intent(h, NULL, size)) |
| 2870 | return false; |
| 2871 | return true; |
| 2872 | } |
| 2873 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2874 | static void fastrpc_glink_notify_tx_done(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2875 | const void *pkt_priv, const void *ptr) |
| 2876 | { |
| 2877 | } |
| 2878 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2879 | static void fastrpc_glink_notify_rx(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2880 | const void *pkt_priv, const void *ptr, size_t size) |
| 2881 | { |
| 2882 | struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)ptr; |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2883 | struct fastrpc_apps *me = &gfa; |
| 2884 | uint32_t index; |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2885 | int err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2886 | |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2887 | VERIFY(err, (rsp && size >= sizeof(*rsp))); |
| 2888 | if (err) |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2889 | goto bail; |
| 2890 | |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2891 | index = (uint32_t)((rsp->ctx & FASTRPC_CTXID_MASK) >> 4); |
| 2892 | VERIFY(err, index < FASTRPC_CTX_MAX); |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2893 | if (err) |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2894 | goto bail; |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2895 | |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2896 | VERIFY(err, !IS_ERR_OR_NULL(me->ctxtable[index])); |
| 2897 | if (err) |
| 2898 | goto bail; |
| 2899 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2900 | VERIFY(err, ((me->ctxtable[index]->ctxid == (rsp->ctx & ~3)) && |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2901 | me->ctxtable[index]->magic == FASTRPC_CTX_MAGIC)); |
| 2902 | if (err) |
| 2903 | goto bail; |
| 2904 | |
Mohammed Nayeem Ur Rahman | 32ba95d | 2019-07-26 17:31:37 +0530 | [diff] [blame] | 2905 | me->ctxtable[index]->handle = handle; |
| 2906 | me->ctxtable[index]->ptr = ptr; |
| 2907 | |
Tharun Kumar Merugu | 9c908aa | 2018-02-06 12:03:48 +0530 | [diff] [blame] | 2908 | context_notify_user(me->ctxtable[index], rsp->retval); |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2909 | bail: |
c_mtharu | fdac689 | 2017-10-12 13:09:01 +0530 | [diff] [blame] | 2910 | if (err) |
| 2911 | pr_err("adsprpc: invalid response or context\n"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 2914 | static void fastrpc_glink_notify_state(void *handle, const void *priv, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2915 | unsigned int event) |
| 2916 | { |
| 2917 | struct fastrpc_apps *me = &gfa; |
| 2918 | int cid = (int)(uintptr_t)priv; |
| 2919 | struct fastrpc_glink_info *link; |
| 2920 | |
| 2921 | if (cid < 0 || cid >= NUM_CHANNELS) |
| 2922 | return; |
| 2923 | link = &me->channel[cid].link; |
| 2924 | switch (event) { |
| 2925 | case GLINK_CONNECTED: |
| 2926 | link->port_state = FASTRPC_LINK_CONNECTED; |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 2927 | complete(&me->channel[cid].workport); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2928 | break; |
| 2929 | case GLINK_LOCAL_DISCONNECTED: |
| 2930 | link->port_state = FASTRPC_LINK_DISCONNECTED; |
| 2931 | break; |
| 2932 | case GLINK_REMOTE_DISCONNECTED: |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2933 | break; |
| 2934 | default: |
| 2935 | break; |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | static int fastrpc_session_alloc(struct fastrpc_channel_ctx *chan, int secure, |
| 2940 | struct fastrpc_session_ctx **session) |
| 2941 | { |
| 2942 | int err = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2943 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2944 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2945 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2946 | if (!*session) |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 2947 | err = fastrpc_session_alloc_locked(chan, secure, 0, session); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2948 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2949 | return err; |
| 2950 | } |
| 2951 | |
| 2952 | static void fastrpc_session_free(struct fastrpc_channel_ctx *chan, |
| 2953 | struct fastrpc_session_ctx *session) |
| 2954 | { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2955 | struct fastrpc_apps *me = &gfa; |
| 2956 | |
| 2957 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2958 | session->used = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 2959 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | static int fastrpc_file_free(struct fastrpc_file *fl) |
| 2963 | { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2964 | struct hlist_node *n = NULL; |
Tharun Kumar Merugu | 3e96676 | 2018-04-04 10:56:44 +0530 | [diff] [blame] | 2965 | struct fastrpc_mmap *map = NULL, *lmap = NULL; |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 2966 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2967 | int cid; |
| 2968 | |
| 2969 | if (!fl) |
| 2970 | return 0; |
| 2971 | cid = fl->cid; |
| 2972 | |
Tharun Kumar Merugu | 622d871 | 2017-09-15 15:30:06 +0530 | [diff] [blame] | 2973 | (void)fastrpc_release_current_dsp_process(fl); |
| 2974 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2975 | spin_lock(&fl->apps->hlock); |
| 2976 | hlist_del_init(&fl->hn); |
| 2977 | spin_unlock(&fl->apps->hlock); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 2978 | kfree(fl->debug_buf); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2979 | |
Sathish Ambley | d7fbcbb | 2017-03-08 10:55:48 -0800 | [diff] [blame] | 2980 | if (!fl->sctx) { |
| 2981 | kfree(fl); |
| 2982 | return 0; |
| 2983 | } |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 2984 | spin_lock(&fl->hlock); |
| 2985 | fl->file_close = 1; |
| 2986 | spin_unlock(&fl->hlock); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2987 | if (!IS_ERR_OR_NULL(fl->init_mem)) |
| 2988 | fastrpc_buf_free(fl->init_mem, 0); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 2989 | fastrpc_context_list_dtor(fl); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 2990 | fastrpc_cached_buf_list_free(fl); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 2991 | mutex_lock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 3e96676 | 2018-04-04 10:56:44 +0530 | [diff] [blame] | 2992 | do { |
| 2993 | lmap = NULL; |
| 2994 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 2995 | hlist_del_init(&map->hn); |
| 2996 | lmap = map; |
| 2997 | break; |
| 2998 | } |
| 2999 | fastrpc_mmap_free(lmap, 1); |
| 3000 | } while (lmap); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3001 | mutex_unlock(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 3002 | if (fl->refcount && (fl->ssrcount == fl->apps->channel[cid].ssrcount)) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3003 | kref_put_mutex(&fl->apps->channel[cid].kref, |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3004 | fastrpc_channel_close, &fl->apps->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3005 | if (fl->sctx) |
| 3006 | fastrpc_session_free(&fl->apps->channel[cid], fl->sctx); |
| 3007 | if (fl->secsctx) |
| 3008 | fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3009 | |
| 3010 | mutex_lock(&fl->perf_mutex); |
| 3011 | do { |
| 3012 | struct hlist_node *pn = NULL; |
| 3013 | |
| 3014 | fperf = NULL; |
| 3015 | hlist_for_each_entry_safe(perf, pn, &fl->perf, hn) { |
| 3016 | hlist_del_init(&perf->hn); |
| 3017 | fperf = perf; |
| 3018 | break; |
| 3019 | } |
| 3020 | kfree(fperf); |
| 3021 | } while (fperf); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3022 | fastrpc_remote_buf_list_free(fl); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3023 | mutex_unlock(&fl->perf_mutex); |
| 3024 | mutex_destroy(&fl->perf_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3025 | mutex_destroy(&fl->fl_map_mutex); |
Tharun Kumar Merugu | 8714e64 | 2018-05-17 15:21:08 +0530 | [diff] [blame] | 3026 | mutex_destroy(&fl->map_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3027 | kfree(fl); |
| 3028 | return 0; |
| 3029 | } |
| 3030 | |
| 3031 | static int fastrpc_device_release(struct inode *inode, struct file *file) |
| 3032 | { |
| 3033 | struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data; |
| 3034 | |
| 3035 | if (fl) { |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3036 | if (fl->qos_request && pm_qos_request_active(&fl->pm_qos_req)) |
| 3037 | pm_qos_remove_request(&fl->pm_qos_req); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3038 | if (fl->debugfs_file != NULL) |
| 3039 | debugfs_remove(fl->debugfs_file); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3040 | fastrpc_file_free(fl); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3041 | file->private_data = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3042 | } |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
| 3046 | static void fastrpc_link_state_handler(struct glink_link_state_cb_info *cb_info, |
| 3047 | void *priv) |
| 3048 | { |
| 3049 | struct fastrpc_apps *me = &gfa; |
| 3050 | int cid = (int)((uintptr_t)priv); |
| 3051 | struct fastrpc_glink_info *link; |
| 3052 | |
| 3053 | if (cid < 0 || cid >= NUM_CHANNELS) |
| 3054 | return; |
| 3055 | |
| 3056 | link = &me->channel[cid].link; |
| 3057 | switch (cb_info->link_state) { |
| 3058 | case GLINK_LINK_STATE_UP: |
| 3059 | link->link_state = FASTRPC_LINK_STATE_UP; |
| 3060 | complete(&me->channel[cid].work); |
| 3061 | break; |
| 3062 | case GLINK_LINK_STATE_DOWN: |
| 3063 | link->link_state = FASTRPC_LINK_STATE_DOWN; |
| 3064 | break; |
| 3065 | default: |
| 3066 | pr_err("adsprpc: unknown link state %d\n", cb_info->link_state); |
| 3067 | break; |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | static int fastrpc_glink_register(int cid, struct fastrpc_apps *me) |
| 3072 | { |
| 3073 | int err = 0; |
| 3074 | struct fastrpc_glink_info *link; |
| 3075 | |
| 3076 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 3077 | if (err) |
| 3078 | goto bail; |
| 3079 | |
| 3080 | link = &me->channel[cid].link; |
| 3081 | if (link->link_notify_handle != NULL) |
| 3082 | goto bail; |
| 3083 | |
| 3084 | link->link_info.glink_link_state_notif_cb = fastrpc_link_state_handler; |
| 3085 | link->link_notify_handle = glink_register_link_state_cb( |
| 3086 | &link->link_info, |
| 3087 | (void *)((uintptr_t)cid)); |
| 3088 | VERIFY(err, !IS_ERR_OR_NULL(me->channel[cid].link.link_notify_handle)); |
| 3089 | if (err) { |
| 3090 | link->link_notify_handle = NULL; |
| 3091 | goto bail; |
| 3092 | } |
| 3093 | VERIFY(err, wait_for_completion_timeout(&me->channel[cid].work, |
| 3094 | RPC_TIMEOUT)); |
| 3095 | bail: |
| 3096 | return err; |
| 3097 | } |
| 3098 | |
| 3099 | static void fastrpc_glink_close(void *chan, int cid) |
| 3100 | { |
| 3101 | int err = 0; |
| 3102 | struct fastrpc_glink_info *link; |
| 3103 | |
| 3104 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 3105 | if (err) |
| 3106 | return; |
| 3107 | link = &gfa.channel[cid].link; |
| 3108 | |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 3109 | if (link->port_state == FASTRPC_LINK_CONNECTED || |
| 3110 | link->port_state == FASTRPC_LINK_REMOTE_DISCONNECTING) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3111 | link->port_state = FASTRPC_LINK_DISCONNECTING; |
| 3112 | glink_close(chan); |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | static int fastrpc_glink_open(int cid) |
| 3117 | { |
| 3118 | int err = 0; |
| 3119 | void *handle = NULL; |
| 3120 | struct fastrpc_apps *me = &gfa; |
| 3121 | struct glink_open_config *cfg; |
| 3122 | struct fastrpc_glink_info *link; |
| 3123 | |
| 3124 | VERIFY(err, (cid >= 0 && cid < NUM_CHANNELS)); |
| 3125 | if (err) |
| 3126 | goto bail; |
| 3127 | link = &me->channel[cid].link; |
| 3128 | cfg = &me->channel[cid].link.cfg; |
| 3129 | VERIFY(err, (link->link_state == FASTRPC_LINK_STATE_UP)); |
| 3130 | if (err) |
| 3131 | goto bail; |
| 3132 | |
Tharun Kumar Merugu | ca0db526 | 2017-05-10 12:53:12 +0530 | [diff] [blame] | 3133 | VERIFY(err, (link->port_state == FASTRPC_LINK_DISCONNECTED)); |
| 3134 | if (err) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3135 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3136 | |
| 3137 | link->port_state = FASTRPC_LINK_CONNECTING; |
| 3138 | cfg->priv = (void *)(uintptr_t)cid; |
| 3139 | cfg->edge = gcinfo[cid].link.link_info.edge; |
| 3140 | cfg->transport = gcinfo[cid].link.link_info.transport; |
| 3141 | cfg->name = FASTRPC_GLINK_GUID; |
| 3142 | cfg->notify_rx = fastrpc_glink_notify_rx; |
| 3143 | cfg->notify_tx_done = fastrpc_glink_notify_tx_done; |
| 3144 | cfg->notify_state = fastrpc_glink_notify_state; |
| 3145 | cfg->notify_rx_intent_req = fastrpc_glink_notify_rx_intent_req; |
| 3146 | handle = glink_open(cfg); |
| 3147 | VERIFY(err, !IS_ERR_OR_NULL(handle)); |
c_mtharu | 6e1d26b | 2017-10-09 16:05:24 +0530 | [diff] [blame] | 3148 | if (err) { |
| 3149 | if (link->port_state == FASTRPC_LINK_CONNECTING) |
| 3150 | link->port_state = FASTRPC_LINK_DISCONNECTED; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3151 | goto bail; |
c_mtharu | 6e1d26b | 2017-10-09 16:05:24 +0530 | [diff] [blame] | 3152 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3153 | me->channel[cid].chan = handle; |
| 3154 | bail: |
| 3155 | return err; |
| 3156 | } |
| 3157 | |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3158 | static int fastrpc_debugfs_open(struct inode *inode, struct file *filp) |
| 3159 | { |
| 3160 | filp->private_data = inode->i_private; |
| 3161 | return 0; |
| 3162 | } |
| 3163 | |
| 3164 | static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer, |
| 3165 | size_t count, loff_t *position) |
| 3166 | { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3167 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3168 | struct fastrpc_file *fl = filp->private_data; |
| 3169 | struct hlist_node *n; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3170 | struct fastrpc_buf *buf = NULL; |
| 3171 | struct fastrpc_mmap *map = NULL; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3172 | struct fastrpc_mmap *gmaps = NULL; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3173 | struct smq_invoke_ctx *ictx = NULL; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3174 | struct fastrpc_channel_ctx *chan = NULL; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3175 | unsigned int len = 0; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3176 | int i, j, sess_used = 0, ret = 0; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3177 | char *fileinfo = NULL; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3178 | char single_line[UL_SIZE] = "----------------"; |
| 3179 | char title[UL_SIZE] = "========================="; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3180 | |
| 3181 | fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL); |
| 3182 | if (!fileinfo) |
| 3183 | goto bail; |
| 3184 | if (fl == NULL) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3185 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3186 | "\n%s %s %s\n", title, " CHANNEL INFO ", title); |
| 3187 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3188 | "%-8s|%-9s|%-9s|%-14s|%-9s|%-13s\n", |
| 3189 | "susbsys", "refcount", "sesscount", "issubsystemup", |
| 3190 | "ssrcount", "session_used"); |
| 3191 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3192 | "-%s%s%s%s-\n", single_line, single_line, |
| 3193 | single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3194 | for (i = 0; i < NUM_CHANNELS; i++) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3195 | sess_used = 0; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3196 | chan = &gcinfo[i]; |
| 3197 | len += scnprintf(fileinfo + len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3198 | DEBUGFS_SIZE - len, "%-8s", chan->subsys); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3199 | len += scnprintf(fileinfo + len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3200 | DEBUGFS_SIZE - len, "|%-9d", |
| 3201 | chan->kref.refcount.counter); |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3202 | len += scnprintf(fileinfo + len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3203 | DEBUGFS_SIZE - len, "|%-9d", |
| 3204 | chan->sesscount); |
| 3205 | len += scnprintf(fileinfo + len, |
| 3206 | DEBUGFS_SIZE - len, "|%-14d", |
| 3207 | chan->issubsystemup); |
| 3208 | len += scnprintf(fileinfo + len, |
| 3209 | DEBUGFS_SIZE - len, "|%-9d", |
| 3210 | chan->ssrcount); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3211 | for (j = 0; j < chan->sesscount; j++) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3212 | sess_used += chan->session[j].used; |
| 3213 | } |
| 3214 | len += scnprintf(fileinfo + len, |
| 3215 | DEBUGFS_SIZE - len, "|%-13d\n", sess_used); |
| 3216 | |
| 3217 | } |
| 3218 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3219 | "\n%s%s%s\n", "=============", |
| 3220 | " CMA HEAP ", "=============="); |
| 3221 | len += scnprintf(fileinfo + len, |
| 3222 | DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size"); |
| 3223 | len += scnprintf(fileinfo + len, |
| 3224 | DEBUGFS_SIZE - len, "--%s%s---\n", |
| 3225 | single_line, single_line); |
| 3226 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3227 | "0x%-18llX", me->range.addr); |
| 3228 | len += scnprintf(fileinfo + len, |
| 3229 | DEBUGFS_SIZE - len, "|0x%-18llX\n", me->range.size); |
| 3230 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3231 | "\n==========%s %s %s===========\n", |
| 3232 | title, " GMAPS ", title); |
| 3233 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3234 | "%-20s|%-20s|%-20s|%-20s\n", |
| 3235 | "fd", "phys", "size", "va"); |
| 3236 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3237 | "%s%s%s%s%s\n", single_line, single_line, |
| 3238 | single_line, single_line, single_line); |
| 3239 | hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) { |
| 3240 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3241 | "%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n", |
| 3242 | gmaps->fd, gmaps->phys, |
| 3243 | (uint32_t)gmaps->size, |
| 3244 | gmaps->va); |
| 3245 | } |
| 3246 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3247 | "%-20s|%-20s|%-20s|%-20s\n", |
| 3248 | "len", "refs", "raddr", "flags"); |
| 3249 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3250 | "%s%s%s%s%s\n", single_line, single_line, |
| 3251 | single_line, single_line, single_line); |
| 3252 | hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) { |
| 3253 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3254 | "0x%-18X|%-20d|%-20lu|%-20u\n", |
| 3255 | (uint32_t)gmaps->len, gmaps->refs, |
| 3256 | gmaps->raddr, gmaps->flags); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3257 | } |
| 3258 | } else { |
| 3259 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3260 | "\n%s %13s %d\n", "cid", ":", fl->cid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3261 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3262 | "%s %12s %d\n", "tgid", ":", fl->tgid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3263 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3264 | "%s %7s %d\n", "sessionid", ":", fl->sessionid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3265 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3266 | "%s %8s %d\n", "ssrcount", ":", fl->ssrcount); |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3267 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3268 | "%s %8s %d\n", "refcount", ":", fl->refcount); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3269 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3270 | "%s %14s %d\n", "pd", ":", fl->pd); |
| 3271 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3272 | "%s %9s %s\n", "spdname", ":", fl->spdname); |
| 3273 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3274 | "%s %6s %d\n", "file_close", ":", fl->file_close); |
| 3275 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3276 | "%s %8s %d\n", "sharedcb", ":", fl->sharedcb); |
| 3277 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3278 | "%s %9s %d\n", "profile", ":", fl->profile); |
| 3279 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3280 | "%s %3s %d\n", "smmu.coherent", ":", |
| 3281 | fl->sctx->smmu.coherent); |
| 3282 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3283 | "%s %4s %d\n", "smmu.enabled", ":", |
| 3284 | fl->sctx->smmu.enabled); |
| 3285 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3286 | "%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb); |
| 3287 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3288 | "%s %5s %d\n", "smmu.secure", ":", |
| 3289 | fl->sctx->smmu.secure); |
| 3290 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3291 | "%s %5s %d\n", "smmu.faults", ":", |
| 3292 | fl->sctx->smmu.faults); |
| 3293 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3294 | "%s %s %d\n", "link.link_state", |
| 3295 | ":", *&me->channel[fl->cid].link.link_state); |
| 3296 | |
| 3297 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3298 | "\n=======%s %s %s======\n", title, |
| 3299 | " LIST OF MAPS ", title); |
| 3300 | |
| 3301 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3302 | "%-20s|%-20s|%-20s\n", "va", "phys", "size"); |
| 3303 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3304 | "%s%s%s%s%s\n", |
| 3305 | single_line, single_line, single_line, |
| 3306 | single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3307 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3308 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3309 | "0x%-20lX|0x%-20llX|0x%-20zu\n\n", |
| 3310 | map->va, map->phys, |
| 3311 | map->size); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3312 | } |
| 3313 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3314 | "%-20s|%-20s|%-20s|%-20s\n", |
| 3315 | "len", "refs", |
| 3316 | "raddr", "uncached"); |
| 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 | "%-20zu|%-20d|0x%-20lX|%-20d\n\n", |
| 3324 | map->len, map->refs, map->raddr, |
| 3325 | map->uncached); |
| 3326 | } |
| 3327 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3328 | "%-20s|%-20s\n", "secure", "attr"); |
| 3329 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3330 | "%s%s%s%s%s\n", |
| 3331 | single_line, single_line, single_line, |
| 3332 | single_line, single_line); |
| 3333 | hlist_for_each_entry_safe(map, n, &fl->maps, hn) { |
| 3334 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3335 | "%-20d|0x%-20lX\n\n", |
| 3336 | map->secure, map->attr); |
| 3337 | } |
| 3338 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3339 | "%s %d\n\n", |
| 3340 | "KERNEL MEMORY ALLOCATION:", 1); |
| 3341 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3342 | "\n======%s %s %s======\n", title, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3343 | " LIST OF CACHED BUFS ", title); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3344 | spin_lock(&fl->hlock); |
| 3345 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3346 | "%-19s|%-19s|%-19s|%-19s\n", |
| 3347 | "virt", "phys", "size", "dma_attr"); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3348 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3349 | "%s%s%s%s%s\n", single_line, single_line, |
| 3350 | single_line, single_line, single_line); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3351 | 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] | 3352 | len += scnprintf(fileinfo + len, |
| 3353 | DEBUGFS_SIZE - len, |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3354 | "0x%-17p|0x%-17llX|%-19zu|0x%-17lX\n", |
| 3355 | buf->virt, (uint64_t)buf->phys, buf->size, |
| 3356 | buf->dma_attr); |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3357 | } |
| 3358 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3359 | "\n%s %s %s\n", title, |
| 3360 | " LIST OF PENDING SMQCONTEXTS ", title); |
| 3361 | |
| 3362 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3363 | "%-20s|%-10s|%-10s|%-10s|%-20s\n", |
| 3364 | "sc", "pid", "tgid", "used", "ctxid"); |
| 3365 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3366 | "%s%s%s%s%s\n", single_line, single_line, |
| 3367 | single_line, single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3368 | hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) { |
| 3369 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3370 | "0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n", |
| 3371 | ictx->sc, ictx->pid, ictx->tgid, |
| 3372 | ictx->used, ictx->ctxid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3373 | } |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3374 | |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3375 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3376 | "\n%s %s %s\n", title, |
| 3377 | " LIST OF INTERRUPTED SMQCONTEXTS ", title); |
| 3378 | |
| 3379 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3380 | "%-20s|%-10s|%-10s|%-10s|%-20s\n", |
| 3381 | "sc", "pid", "tgid", "used", "ctxid"); |
| 3382 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3383 | "%s%s%s%s%s\n", single_line, single_line, |
| 3384 | single_line, single_line, single_line); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3385 | 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] | 3386 | len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len, |
| 3387 | "%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n", |
| 3388 | ictx->sc, ictx->pid, ictx->tgid, |
| 3389 | ictx->used, ictx->ctxid); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 3390 | } |
| 3391 | spin_unlock(&fl->hlock); |
| 3392 | } |
| 3393 | if (len > DEBUGFS_SIZE) |
| 3394 | len = DEBUGFS_SIZE; |
| 3395 | ret = simple_read_from_buffer(buffer, count, position, fileinfo, len); |
| 3396 | kfree(fileinfo); |
| 3397 | bail: |
| 3398 | return ret; |
| 3399 | } |
| 3400 | |
| 3401 | static const struct file_operations debugfs_fops = { |
| 3402 | .open = fastrpc_debugfs_open, |
| 3403 | .read = fastrpc_debugfs_read, |
| 3404 | }; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3405 | static int fastrpc_channel_open(struct fastrpc_file *fl) |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3406 | { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3407 | struct fastrpc_apps *me = &gfa; |
Tharun Kumar Merugu | c42c6e2 | 2018-05-29 15:50:46 +0530 | [diff] [blame] | 3408 | int cid, ii, err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3409 | |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3410 | mutex_lock(&me->smd_mutex); |
| 3411 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3412 | VERIFY(err, fl && fl->sctx); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3413 | if (err) |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3414 | goto bail; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3415 | cid = fl->cid; |
c_mtharu | 314a420 | 2017-11-15 22:09:17 +0530 | [diff] [blame] | 3416 | VERIFY(err, cid >= 0 && cid < NUM_CHANNELS); |
| 3417 | if (err) |
| 3418 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3419 | if (me->channel[cid].ssrcount != |
| 3420 | me->channel[cid].prevssrcount) { |
| 3421 | if (!me->channel[cid].issubsystemup) { |
| 3422 | VERIFY(err, 0); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3423 | if (err) { |
| 3424 | err = -ENOTCONN; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3425 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3426 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3427 | } |
| 3428 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3429 | fl->ssrcount = me->channel[cid].ssrcount; |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 3430 | fl->refcount = 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3431 | if ((kref_get_unless_zero(&me->channel[cid].kref) == 0) || |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3432 | (me->channel[cid].chan == NULL)) { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3433 | if (me->glink) { |
| 3434 | VERIFY(err, 0 == fastrpc_glink_register(cid, me)); |
| 3435 | if (err) |
| 3436 | goto bail; |
| 3437 | VERIFY(err, 0 == fastrpc_glink_open(cid)); |
| 3438 | } else { |
| 3439 | VERIFY(err, !smd_named_open_on_edge(FASTRPC_SMD_GUID, |
| 3440 | gcinfo[cid].channel, |
| 3441 | (smd_channel_t **)&me->channel[cid].chan, |
| 3442 | (void *)(uintptr_t)cid, |
| 3443 | smd_event_handler)); |
| 3444 | } |
Tharun Kumar Merugu | 53a8ec9 | 2017-07-14 15:52:49 +0530 | [diff] [blame] | 3445 | VERIFY(err, |
| 3446 | wait_for_completion_timeout(&me->channel[cid].workport, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3447 | RPC_TIMEOUT)); |
| 3448 | if (err) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3449 | me->channel[cid].chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3450 | goto bail; |
| 3451 | } |
| 3452 | kref_init(&me->channel[cid].kref); |
| 3453 | pr_info("'opened /dev/%s c %d %d'\n", gcinfo[cid].name, |
| 3454 | MAJOR(me->dev_no), cid); |
Tharun Kumar Merugu | c42c6e2 | 2018-05-29 15:50:46 +0530 | [diff] [blame] | 3455 | |
| 3456 | for (ii = 0; ii < FASTRPC_GLINK_INTENT_NUM && me->glink; ii++) |
| 3457 | glink_queue_rx_intent(me->channel[cid].chan, NULL, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3458 | FASTRPC_GLINK_INTENT_LEN); |
Tharun Kumar Merugu | c42c6e2 | 2018-05-29 15:50:46 +0530 | [diff] [blame] | 3459 | |
Tharun Kumar Merugu | d86fc8c | 2018-01-04 16:35:31 +0530 | [diff] [blame] | 3460 | if (cid == 0 && me->channel[cid].ssrcount != |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3461 | me->channel[cid].prevssrcount) { |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3462 | if (fastrpc_mmap_remove_ssr(fl)) |
| 3463 | pr_err("ADSPRPC: SSR: Failed to unmap remote heap\n"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3464 | me->channel[cid].prevssrcount = |
| 3465 | me->channel[cid].ssrcount; |
| 3466 | } |
| 3467 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3468 | |
| 3469 | bail: |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3470 | mutex_unlock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3471 | return err; |
| 3472 | } |
| 3473 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3474 | static int fastrpc_device_open(struct inode *inode, struct file *filp) |
| 3475 | { |
| 3476 | int err = 0; |
Sathish Ambley | 567012b | 2017-03-06 11:55:04 -0800 | [diff] [blame] | 3477 | struct dentry *debugfs_file; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3478 | struct fastrpc_file *fl = NULL; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3479 | struct fastrpc_apps *me = &gfa; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3480 | char strpid[PID_SIZE]; |
| 3481 | int buf_size = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3482 | |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3483 | /* |
| 3484 | * Indicates the device node opened |
| 3485 | * MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV |
| 3486 | */ |
| 3487 | int dev_minor = MINOR(inode->i_rdev); |
| 3488 | |
| 3489 | VERIFY(err, ((dev_minor == MINOR_NUM_DEV) || |
| 3490 | (dev_minor == MINOR_NUM_SECURE_DEV))); |
| 3491 | if (err) { |
| 3492 | pr_err("adsprpc: Invalid dev minor num %d\n", dev_minor); |
| 3493 | return err; |
| 3494 | } |
| 3495 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3496 | VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL))); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3497 | if (err) |
| 3498 | return err; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3499 | snprintf(strpid, PID_SIZE, "%d", current->pid); |
Mohammed Nayeem Ur Rahman | 2d65b4a | 2018-10-10 16:34:37 +0530 | [diff] [blame] | 3500 | buf_size = strlen(current->comm) + strlen("_") + strlen(strpid) + 1; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 3501 | fl->debug_buf = kzalloc(buf_size, GFP_KERNEL); |
| 3502 | snprintf(fl->debug_buf, UL_SIZE, "%.10s%s%d", |
| 3503 | current->comm, "_", current->pid); |
| 3504 | debugfs_file = debugfs_create_file(fl->debug_buf, 0644, |
| 3505 | debugfs_root, fl, &debugfs_fops); |
| 3506 | |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3507 | context_list_ctor(&fl->clst); |
| 3508 | spin_lock_init(&fl->hlock); |
| 3509 | INIT_HLIST_HEAD(&fl->maps); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3510 | INIT_HLIST_HEAD(&fl->perf); |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3511 | INIT_HLIST_HEAD(&fl->cached_bufs); |
| 3512 | INIT_HLIST_HEAD(&fl->remote_bufs); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3513 | INIT_HLIST_NODE(&fl->hn); |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 3514 | fl->sessionid = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3515 | fl->tgid = current->tgid; |
| 3516 | fl->apps = me; |
| 3517 | fl->mode = FASTRPC_MODE_SERIAL; |
| 3518 | fl->cid = -1; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3519 | fl->dev_minor = dev_minor; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3520 | fl->init_mem = NULL; |
Sathish Ambley | 567012b | 2017-03-06 11:55:04 -0800 | [diff] [blame] | 3521 | if (debugfs_file != NULL) |
| 3522 | fl->debugfs_file = debugfs_file; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3523 | fl->qos_request = 0; |
Tharun Kumar Merugu | 3517334 | 2018-02-08 16:13:17 +0530 | [diff] [blame] | 3524 | fl->refcount = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3525 | filp->private_data = fl; |
Tharun Kumar Merugu | c31eac5 | 2018-01-02 11:42:45 +0530 | [diff] [blame] | 3526 | mutex_init(&fl->map_mutex); |
Tharun Kumar Merugu | ed7a847 | 2018-01-25 12:10:15 +0530 | [diff] [blame] | 3527 | mutex_init(&fl->fl_map_mutex); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3528 | spin_lock(&me->hlock); |
| 3529 | hlist_add_head(&fl->hn, &me->drivers); |
| 3530 | spin_unlock(&me->hlock); |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3531 | mutex_init(&fl->perf_mutex); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3532 | return 0; |
| 3533 | } |
| 3534 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3535 | static int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info) |
| 3536 | { |
| 3537 | int err = 0; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3538 | uint32_t cid; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3539 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3540 | VERIFY(err, fl != NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3541 | if (err) |
| 3542 | goto bail; |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3543 | if (fl->cid == -1) { |
| 3544 | cid = *info; |
| 3545 | VERIFY(err, cid < NUM_CHANNELS); |
| 3546 | if (err) |
| 3547 | goto bail; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3548 | /* Check to see if the device node is non-secure */ |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 3549 | if (fl->dev_minor == MINOR_NUM_DEV && |
| 3550 | fl->apps->secure_flag == true) { |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 3551 | /* |
| 3552 | * For non secure device node check and make sure that |
| 3553 | * the channel allows non-secure access |
| 3554 | * If not, bail. Session will not start. |
| 3555 | * cid will remain -1 and client will not be able to |
| 3556 | * invoke any other methods without failure |
| 3557 | */ |
| 3558 | if (fl->apps->channel[cid].secure == SECURE_CHANNEL) { |
| 3559 | err = -EPERM; |
| 3560 | pr_err("adsprpc: GetInfo failed dev %d, cid %d, secure %d\n", |
| 3561 | fl->dev_minor, cid, |
| 3562 | fl->apps->channel[cid].secure); |
| 3563 | goto bail; |
| 3564 | } |
| 3565 | } |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3566 | fl->cid = cid; |
| 3567 | fl->ssrcount = fl->apps->channel[cid].ssrcount; |
| 3568 | VERIFY(err, !fastrpc_session_alloc_locked( |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3569 | &fl->apps->channel[cid], 0, fl->sharedcb, &fl->sctx)); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3570 | if (err) |
| 3571 | goto bail; |
| 3572 | } |
Tharun Kumar Merugu | 80be7d6 | 2017-08-02 11:03:22 +0530 | [diff] [blame] | 3573 | VERIFY(err, fl->sctx != NULL); |
| 3574 | if (err) |
| 3575 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3576 | *info = (fl->sctx->smmu.enabled ? 1 : 0); |
| 3577 | bail: |
| 3578 | return err; |
| 3579 | } |
| 3580 | |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3581 | static int fastrpc_internal_control(struct fastrpc_file *fl, |
| 3582 | struct fastrpc_ioctl_control *cp) |
| 3583 | { |
Mohammed Nayeem Ur Rahman | 18d633f | 2019-05-28 15:11:40 +0530 | [diff] [blame] | 3584 | struct fastrpc_apps *me = &gfa; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3585 | int err = 0; |
| 3586 | int latency; |
| 3587 | |
| 3588 | VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps)); |
| 3589 | if (err) |
| 3590 | goto bail; |
| 3591 | VERIFY(err, !IS_ERR_OR_NULL(cp)); |
| 3592 | if (err) |
| 3593 | goto bail; |
| 3594 | |
| 3595 | switch (cp->req) { |
| 3596 | case FASTRPC_CONTROL_LATENCY: |
| 3597 | latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ? |
| 3598 | fl->apps->latency : PM_QOS_DEFAULT_VALUE; |
| 3599 | VERIFY(err, latency != 0); |
| 3600 | if (err) |
| 3601 | goto bail; |
| 3602 | if (!fl->qos_request) { |
| 3603 | pm_qos_add_request(&fl->pm_qos_req, |
| 3604 | PM_QOS_CPU_DMA_LATENCY, latency); |
| 3605 | fl->qos_request = 1; |
| 3606 | } else |
| 3607 | pm_qos_update_request(&fl->pm_qos_req, latency); |
| 3608 | break; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3609 | case FASTRPC_CONTROL_SMMU: |
Mohammed Nayeem Ur Rahman | 18d633f | 2019-05-28 15:11:40 +0530 | [diff] [blame] | 3610 | if (!me->legacy) |
| 3611 | fl->sharedcb = cp->smmu.sharedcb; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3612 | break; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3613 | case FASTRPC_CONTROL_KALLOC: |
| 3614 | cp->kalloc.kalloc_support = 1; |
| 3615 | break; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3616 | default: |
| 3617 | err = -ENOTTY; |
| 3618 | break; |
| 3619 | } |
| 3620 | bail: |
| 3621 | return err; |
| 3622 | } |
| 3623 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3624 | static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num, |
| 3625 | unsigned long ioctl_param) |
| 3626 | { |
| 3627 | union { |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3628 | struct fastrpc_ioctl_invoke_crc inv; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3629 | struct fastrpc_ioctl_mmap mmap; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3630 | struct fastrpc_ioctl_mmap_64 mmap64; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3631 | struct fastrpc_ioctl_munmap munmap; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3632 | struct fastrpc_ioctl_munmap_64 munmap64; |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 3633 | struct fastrpc_ioctl_munmap_fd munmap_fd; |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 3634 | struct fastrpc_ioctl_init_attrs init; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3635 | struct fastrpc_ioctl_perf perf; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3636 | struct fastrpc_ioctl_control cp; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3637 | } p; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3638 | union { |
| 3639 | struct fastrpc_ioctl_mmap mmap; |
| 3640 | struct fastrpc_ioctl_munmap munmap; |
| 3641 | } i; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3642 | void *param = (char *)ioctl_param; |
| 3643 | struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data; |
| 3644 | int size = 0, err = 0; |
| 3645 | uint32_t info; |
| 3646 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3647 | p.inv.fds = NULL; |
| 3648 | p.inv.attrs = NULL; |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3649 | p.inv.crc = NULL; |
tharun kumar | 9f899ea | 2017-07-03 17:07:03 +0530 | [diff] [blame] | 3650 | spin_lock(&fl->hlock); |
| 3651 | if (fl->file_close == 1) { |
| 3652 | err = EBADF; |
| 3653 | pr_warn("ADSPRPC: fastrpc_device_release is happening, So not sending any new requests to DSP"); |
| 3654 | spin_unlock(&fl->hlock); |
| 3655 | goto bail; |
| 3656 | } |
| 3657 | spin_unlock(&fl->hlock); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3658 | |
| 3659 | switch (ioctl_num) { |
| 3660 | case FASTRPC_IOCTL_INVOKE: |
| 3661 | size = sizeof(struct fastrpc_ioctl_invoke); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3662 | /* fall through */ |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3663 | case FASTRPC_IOCTL_INVOKE_FD: |
| 3664 | if (!size) |
| 3665 | size = sizeof(struct fastrpc_ioctl_invoke_fd); |
| 3666 | /* fall through */ |
| 3667 | case FASTRPC_IOCTL_INVOKE_ATTRS: |
| 3668 | if (!size) |
| 3669 | size = sizeof(struct fastrpc_ioctl_invoke_attrs); |
Sathish Ambley | bae5190 | 2017-07-03 15:00:49 -0700 | [diff] [blame] | 3670 | /* fall through */ |
| 3671 | case FASTRPC_IOCTL_INVOKE_CRC: |
| 3672 | if (!size) |
| 3673 | size = sizeof(struct fastrpc_ioctl_invoke_crc); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3674 | K_COPY_FROM_USER(err, 0, &p.inv, param, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3675 | if (err) |
| 3676 | goto bail; |
| 3677 | VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode, |
| 3678 | 0, &p.inv))); |
| 3679 | if (err) |
| 3680 | goto bail; |
| 3681 | break; |
| 3682 | case FASTRPC_IOCTL_MMAP: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3683 | K_COPY_FROM_USER(err, 0, &p.mmap, param, |
| 3684 | sizeof(p.mmap)); |
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_mmap(fl, &p.mmap))); |
| 3688 | if (err) |
| 3689 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3690 | K_COPY_TO_USER(err, 0, param, &p.mmap, sizeof(p.mmap)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3691 | if (err) |
| 3692 | goto bail; |
| 3693 | break; |
| 3694 | case FASTRPC_IOCTL_MUNMAP: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3695 | K_COPY_FROM_USER(err, 0, &p.munmap, param, |
| 3696 | sizeof(p.munmap)); |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 3697 | if (err) |
| 3698 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3699 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl, |
| 3700 | &p.munmap))); |
| 3701 | if (err) |
| 3702 | goto bail; |
| 3703 | break; |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3704 | case FASTRPC_IOCTL_MMAP_64: |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3705 | K_COPY_FROM_USER(err, 0, &p.mmap64, param, |
| 3706 | sizeof(p.mmap64)); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3707 | if (err) |
| 3708 | goto bail; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3709 | get_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap); |
| 3710 | VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap))); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3711 | if (err) |
| 3712 | goto bail; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3713 | put_fastrpc_ioctl_mmap_64(&p.mmap64, &i.mmap); |
| 3714 | 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] | 3715 | if (err) |
| 3716 | goto bail; |
| 3717 | break; |
| 3718 | case FASTRPC_IOCTL_MUNMAP_64: |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3719 | K_COPY_FROM_USER(err, 0, &p.munmap64, param, |
| 3720 | sizeof(p.munmap64)); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3721 | if (err) |
| 3722 | goto bail; |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3723 | get_fastrpc_ioctl_munmap_64(&p.munmap64, &i.munmap); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3724 | VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl, |
Tharun Kumar Merugu | 92b5e13 | 2018-07-18 15:03:35 +0530 | [diff] [blame] | 3725 | &i.munmap))); |
Tharun Kumar Merugu | 55be90d | 2018-05-31 11:41:03 +0530 | [diff] [blame] | 3726 | if (err) |
| 3727 | goto bail; |
| 3728 | break; |
c_mtharu | 7bd6a42 | 2017-10-17 18:15:37 +0530 | [diff] [blame] | 3729 | case FASTRPC_IOCTL_MUNMAP_FD: |
| 3730 | K_COPY_FROM_USER(err, 0, &p.munmap_fd, param, |
| 3731 | sizeof(p.munmap_fd)); |
| 3732 | if (err) |
| 3733 | goto bail; |
| 3734 | VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl, |
| 3735 | &p.munmap_fd))); |
| 3736 | if (err) |
| 3737 | goto bail; |
| 3738 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3739 | case FASTRPC_IOCTL_SETMODE: |
| 3740 | switch ((uint32_t)ioctl_param) { |
| 3741 | case FASTRPC_MODE_PARALLEL: |
| 3742 | case FASTRPC_MODE_SERIAL: |
| 3743 | fl->mode = (uint32_t)ioctl_param; |
| 3744 | break; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3745 | case FASTRPC_MODE_PROFILE: |
| 3746 | fl->profile = (uint32_t)ioctl_param; |
| 3747 | break; |
Tharun Kumar Merugu | d4d07948 | 2017-09-06 11:22:19 +0530 | [diff] [blame] | 3748 | case FASTRPC_MODE_SESSION: |
| 3749 | fl->sessionid = 1; |
| 3750 | fl->tgid |= (1 << SESSION_ID_INDEX); |
| 3751 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3752 | default: |
| 3753 | err = -ENOTTY; |
| 3754 | break; |
| 3755 | } |
| 3756 | break; |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3757 | case FASTRPC_IOCTL_GETPERF: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3758 | K_COPY_FROM_USER(err, 0, &p.perf, |
| 3759 | param, sizeof(p.perf)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3760 | if (err) |
| 3761 | goto bail; |
| 3762 | p.perf.numkeys = sizeof(struct fastrpc_perf)/sizeof(int64_t); |
| 3763 | if (p.perf.keys) { |
| 3764 | char *keys = PERF_KEYS; |
| 3765 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3766 | K_COPY_TO_USER(err, 0, (void *)p.perf.keys, |
| 3767 | keys, strlen(keys)+1); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3768 | if (err) |
| 3769 | goto bail; |
| 3770 | } |
| 3771 | if (p.perf.data) { |
Tharun Kumar Merugu | 7c966dd | 2018-01-04 18:07:03 +0530 | [diff] [blame] | 3772 | struct fastrpc_perf *perf = NULL, *fperf = NULL; |
| 3773 | struct hlist_node *n = NULL; |
| 3774 | |
| 3775 | mutex_lock(&fl->perf_mutex); |
| 3776 | hlist_for_each_entry_safe(perf, n, &fl->perf, hn) { |
| 3777 | if (perf->tid == current->pid) { |
| 3778 | fperf = perf; |
| 3779 | break; |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | mutex_unlock(&fl->perf_mutex); |
| 3784 | |
| 3785 | if (fperf) { |
| 3786 | K_COPY_TO_USER(err, 0, (void *)p.perf.data, |
| 3787 | fperf, sizeof(*fperf)); |
| 3788 | } |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3789 | } |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3790 | K_COPY_TO_USER(err, 0, param, &p.perf, sizeof(p.perf)); |
Sathish Ambley | a21b5b5 | 2017-01-11 16:11:01 -0800 | [diff] [blame] | 3791 | if (err) |
| 3792 | goto bail; |
| 3793 | break; |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3794 | case FASTRPC_IOCTL_CONTROL: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3795 | K_COPY_FROM_USER(err, 0, &p.cp, param, |
| 3796 | sizeof(p.cp)); |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3797 | if (err) |
| 3798 | goto bail; |
| 3799 | VERIFY(err, 0 == (err = fastrpc_internal_control(fl, &p.cp))); |
| 3800 | if (err) |
| 3801 | goto bail; |
Tharun Kumar Merugu | e073de7 | 2018-07-30 23:57:47 +0530 | [diff] [blame] | 3802 | if (p.cp.req == FASTRPC_CONTROL_KALLOC) { |
| 3803 | K_COPY_TO_USER(err, 0, param, &p.cp, sizeof(p.cp)); |
| 3804 | if (err) |
| 3805 | goto bail; |
| 3806 | } |
Tharun Kumar Merugu | 5f6ca61c | 2017-08-11 11:43:11 +0530 | [diff] [blame] | 3807 | break; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3808 | case FASTRPC_IOCTL_GETINFO: |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3809 | K_COPY_FROM_USER(err, 0, &info, param, sizeof(info)); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 3810 | if (err) |
| 3811 | goto bail; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3812 | VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info))); |
| 3813 | if (err) |
| 3814 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3815 | K_COPY_TO_USER(err, 0, param, &info, sizeof(info)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3816 | if (err) |
| 3817 | goto bail; |
| 3818 | break; |
| 3819 | case FASTRPC_IOCTL_INIT: |
Sathish Ambley | d6300c3 | 2017-01-18 09:50:43 -0800 | [diff] [blame] | 3820 | p.init.attrs = 0; |
| 3821 | p.init.siglen = 0; |
| 3822 | size = sizeof(struct fastrpc_ioctl_init); |
| 3823 | /* fall through */ |
| 3824 | case FASTRPC_IOCTL_INIT_ATTRS: |
| 3825 | if (!size) |
| 3826 | size = sizeof(struct fastrpc_ioctl_init_attrs); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3827 | K_COPY_FROM_USER(err, 0, &p.init, param, size); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3828 | if (err) |
| 3829 | goto bail; |
Tharun Kumar Merugu | 4ea0eac | 2017-08-22 11:42:51 +0530 | [diff] [blame] | 3830 | VERIFY(err, p.init.init.filelen >= 0 && |
Tharun Kumar Merugu | d1f388a | 2017-10-01 10:51:11 +0530 | [diff] [blame] | 3831 | p.init.init.filelen < INIT_FILELEN_MAX); |
| 3832 | if (err) |
| 3833 | goto bail; |
| 3834 | VERIFY(err, p.init.init.memlen >= 0 && |
| 3835 | p.init.init.memlen < INIT_MEMLEN_MAX); |
Tharun Kumar Merugu | 4ea0eac | 2017-08-22 11:42:51 +0530 | [diff] [blame] | 3836 | if (err) |
| 3837 | goto bail; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3838 | VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3839 | if (err) |
| 3840 | goto bail; |
| 3841 | break; |
| 3842 | |
| 3843 | default: |
| 3844 | err = -ENOTTY; |
| 3845 | pr_info("bad ioctl: %d\n", ioctl_num); |
| 3846 | break; |
| 3847 | } |
| 3848 | bail: |
| 3849 | return err; |
| 3850 | } |
| 3851 | |
| 3852 | static int fastrpc_restart_notifier_cb(struct notifier_block *nb, |
| 3853 | unsigned long code, |
| 3854 | void *data) |
| 3855 | { |
| 3856 | struct fastrpc_apps *me = &gfa; |
| 3857 | struct fastrpc_channel_ctx *ctx; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3858 | struct notif_data *notifdata = data; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3859 | int cid; |
| 3860 | |
| 3861 | ctx = container_of(nb, struct fastrpc_channel_ctx, nb); |
| 3862 | cid = ctx - &me->channel[0]; |
| 3863 | if (code == SUBSYS_BEFORE_SHUTDOWN) { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3864 | mutex_lock(&me->smd_mutex); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3865 | ctx->ssrcount++; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3866 | ctx->issubsystemup = 0; |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3867 | if (ctx->chan) { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3868 | if (me->glink) |
| 3869 | fastrpc_glink_close(ctx->chan, cid); |
| 3870 | else |
| 3871 | smd_close(ctx->chan); |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 3872 | ctx->chan = NULL; |
| 3873 | pr_info("'restart notifier: closed /dev/%s c %d %d'\n", |
| 3874 | gcinfo[cid].name, MAJOR(me->dev_no), cid); |
| 3875 | } |
| 3876 | mutex_unlock(&me->smd_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3877 | if (cid == 0) |
| 3878 | me->staticpd_flags = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3879 | fastrpc_notify_drivers(me, cid); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3880 | } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) { |
| 3881 | if (me->channel[0].remoteheap_ramdump_dev && |
| 3882 | notifdata->enable_ramdump) { |
| 3883 | me->channel[0].ramdumpenabled = 1; |
| 3884 | } |
| 3885 | } else if (code == SUBSYS_AFTER_POWERUP) { |
| 3886 | ctx->issubsystemup = 1; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | return NOTIFY_DONE; |
| 3890 | } |
| 3891 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3892 | static int fastrpc_pdr_notifier_cb(struct notifier_block *pdrnb, |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 3893 | unsigned long code, |
| 3894 | void *data) |
| 3895 | { |
| 3896 | struct fastrpc_apps *me = &gfa; |
| 3897 | struct fastrpc_static_pd *spd; |
| 3898 | struct notif_data *notifdata = data; |
| 3899 | |
| 3900 | spd = container_of(pdrnb, struct fastrpc_static_pd, pdrnb); |
| 3901 | if (code == SERVREG_NOTIF_SERVICE_STATE_DOWN_V01) { |
| 3902 | mutex_lock(&me->smd_mutex); |
| 3903 | spd->pdrcount++; |
| 3904 | spd->ispdup = 0; |
| 3905 | pr_info("ADSPRPC: Audio PDR notifier %d %s\n", |
| 3906 | MAJOR(me->dev_no), spd->spdname); |
| 3907 | mutex_unlock(&me->smd_mutex); |
| 3908 | if (!strcmp(spd->spdname, |
| 3909 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME)) |
| 3910 | me->staticpd_flags = 0; |
| 3911 | fastrpc_notify_pdr_drivers(me, spd->spdname); |
| 3912 | } else if (code == SUBSYS_RAMDUMP_NOTIFICATION) { |
| 3913 | if (me->channel[0].remoteheap_ramdump_dev && |
| 3914 | notifdata->enable_ramdump) { |
| 3915 | me->channel[0].ramdumpenabled = 1; |
| 3916 | } |
| 3917 | } else if (code == SERVREG_NOTIF_SERVICE_STATE_UP_V01) { |
| 3918 | spd->ispdup = 1; |
| 3919 | } |
| 3920 | |
| 3921 | return NOTIFY_DONE; |
| 3922 | } |
| 3923 | |
| 3924 | static int fastrpc_get_service_location_notify(struct notifier_block *nb, |
| 3925 | unsigned long opcode, void *data) |
| 3926 | { |
| 3927 | struct fastrpc_static_pd *spd; |
| 3928 | struct pd_qmi_client_data *pdr = data; |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3929 | int curr_state = 0, i = 0; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 3930 | |
| 3931 | spd = container_of(nb, struct fastrpc_static_pd, get_service_nb); |
| 3932 | if (opcode == LOCATOR_DOWN) { |
| 3933 | pr_err("ADSPRPC: Audio PD restart notifier locator down\n"); |
| 3934 | return NOTIFY_DONE; |
| 3935 | } |
| 3936 | |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3937 | for (i = 0; i < pdr->total_domains; i++) { |
| 3938 | if ((!strcmp(pdr->domain_list[i].name, |
| 3939 | "msm/adsp/audio_pd")) || |
| 3940 | (!strcmp(pdr->domain_list[i].name, |
| 3941 | "msm/adsp/sensor_pd"))) { |
| 3942 | spd->pdrhandle = |
| 3943 | service_notif_register_notifier( |
| 3944 | pdr->domain_list[i].name, |
| 3945 | pdr->domain_list[i].instance_id, |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 3946 | &spd->pdrnb, &curr_state); |
Tharun Kumar Merugu | ad4beb8 | 2018-05-10 19:51:48 +0530 | [diff] [blame] | 3947 | if (IS_ERR(spd->pdrhandle)) { |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3948 | pr_err("ADSPRPC: Unable to register notifier\n"); |
Tharun Kumar Merugu | ad4beb8 | 2018-05-10 19:51:48 +0530 | [diff] [blame] | 3949 | } else if (curr_state == |
| 3950 | SERVREG_NOTIF_SERVICE_STATE_UP_V01) { |
| 3951 | pr_info("ADSPRPC: STATE_UP_V01 received\n"); |
| 3952 | spd->ispdup = 1; |
| 3953 | } else if (curr_state == |
| 3954 | SERVREG_NOTIF_SERVICE_STATE_UNINIT_V01) { |
| 3955 | pr_info("ADSPRPC: STATE_UNINIT_V01 received\n"); |
| 3956 | } |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 3957 | break; |
| 3958 | } |
| 3959 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 3960 | |
| 3961 | return NOTIFY_DONE; |
| 3962 | } |
| 3963 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3964 | static const struct file_operations fops = { |
| 3965 | .open = fastrpc_device_open, |
| 3966 | .release = fastrpc_device_release, |
| 3967 | .unlocked_ioctl = fastrpc_device_ioctl, |
| 3968 | .compat_ioctl = compat_fastrpc_device_ioctl, |
| 3969 | }; |
| 3970 | |
| 3971 | static const struct of_device_id fastrpc_match_table[] = { |
| 3972 | { .compatible = "qcom,msm-fastrpc-adsp", }, |
| 3973 | { .compatible = "qcom,msm-fastrpc-compute", }, |
| 3974 | { .compatible = "qcom,msm-fastrpc-compute-cb", }, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 3975 | { .compatible = "qcom,msm-fastrpc-legacy-compute", }, |
| 3976 | { .compatible = "qcom,msm-fastrpc-legacy-compute-cb", }, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3977 | { .compatible = "qcom,msm-adsprpc-mem-region", }, |
| 3978 | {} |
| 3979 | }; |
| 3980 | |
| 3981 | static int fastrpc_cb_probe(struct device *dev) |
| 3982 | { |
| 3983 | struct fastrpc_channel_ctx *chan; |
| 3984 | struct fastrpc_session_ctx *sess; |
| 3985 | struct of_phandle_args iommuspec; |
| 3986 | const char *name; |
| 3987 | unsigned int start = 0x80000000; |
| 3988 | int err = 0, i; |
| 3989 | int secure_vmid = VMID_CP_PIXEL; |
| 3990 | |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 3991 | VERIFY(err, NULL != (name = of_get_property(dev->of_node, |
| 3992 | "label", NULL))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 3993 | if (err) |
| 3994 | goto bail; |
| 3995 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 3996 | if (!gcinfo[i].name) |
| 3997 | continue; |
| 3998 | if (!strcmp(name, gcinfo[i].name)) |
| 3999 | break; |
| 4000 | } |
| 4001 | VERIFY(err, i < NUM_CHANNELS); |
| 4002 | if (err) |
| 4003 | goto bail; |
| 4004 | chan = &gcinfo[i]; |
| 4005 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 4006 | if (err) |
| 4007 | goto bail; |
| 4008 | |
| 4009 | VERIFY(err, !of_parse_phandle_with_args(dev->of_node, "iommus", |
| 4010 | "#iommu-cells", 0, &iommuspec)); |
| 4011 | if (err) |
| 4012 | goto bail; |
| 4013 | sess = &chan->session[chan->sesscount]; |
| 4014 | sess->smmu.cb = iommuspec.args[0] & 0xf; |
| 4015 | sess->used = 0; |
| 4016 | sess->smmu.coherent = of_property_read_bool(dev->of_node, |
| 4017 | "dma-coherent"); |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4018 | sess->smmu.sharedcb = of_property_read_bool(dev->of_node, |
| 4019 | "shared-cb"); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4020 | sess->smmu.secure = of_property_read_bool(dev->of_node, |
| 4021 | "qcom,secure-context-bank"); |
| 4022 | if (sess->smmu.secure) |
| 4023 | start = 0x60000000; |
| 4024 | VERIFY(err, !IS_ERR_OR_NULL(sess->smmu.mapping = |
| 4025 | arm_iommu_create_mapping(&platform_bus_type, |
Tharun Kumar Merugu | ca183f9 | 2017-04-27 17:43:27 +0530 | [diff] [blame] | 4026 | start, 0x78000000))); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4027 | if (err) |
| 4028 | goto bail; |
| 4029 | |
| 4030 | if (sess->smmu.secure) |
| 4031 | iommu_domain_set_attr(sess->smmu.mapping->domain, |
| 4032 | DOMAIN_ATTR_SECURE_VMID, |
| 4033 | &secure_vmid); |
| 4034 | |
| 4035 | VERIFY(err, !arm_iommu_attach_device(dev, sess->smmu.mapping)); |
| 4036 | if (err) |
| 4037 | goto bail; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4038 | sess->smmu.dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4039 | sess->smmu.enabled = 1; |
| 4040 | chan->sesscount++; |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 4041 | debugfs_global_file = debugfs_create_file("global", 0644, debugfs_root, |
| 4042 | NULL, &debugfs_fops); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4043 | bail: |
| 4044 | return err; |
| 4045 | } |
| 4046 | |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4047 | static int fastrpc_cb_legacy_probe(struct device *dev) |
| 4048 | { |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4049 | struct fastrpc_channel_ctx *chan; |
| 4050 | struct fastrpc_session_ctx *first_sess = NULL, *sess = NULL; |
| 4051 | const char *name; |
| 4052 | unsigned int *sids = NULL, sids_size = 0; |
| 4053 | int err = 0, ret = 0, i; |
| 4054 | |
| 4055 | unsigned int start = 0x80000000; |
| 4056 | |
| 4057 | VERIFY(err, NULL != (name = of_get_property(dev->of_node, |
| 4058 | "label", NULL))); |
| 4059 | if (err) |
| 4060 | goto bail; |
| 4061 | |
| 4062 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 4063 | if (!gcinfo[i].name) |
| 4064 | continue; |
| 4065 | if (!strcmp(name, gcinfo[i].name)) |
| 4066 | break; |
| 4067 | } |
| 4068 | VERIFY(err, i < NUM_CHANNELS); |
| 4069 | if (err) |
| 4070 | goto bail; |
| 4071 | |
| 4072 | chan = &gcinfo[i]; |
| 4073 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 4074 | if (err) |
| 4075 | goto bail; |
| 4076 | |
| 4077 | first_sess = &chan->session[chan->sesscount]; |
| 4078 | |
| 4079 | VERIFY(err, NULL != of_get_property(dev->of_node, |
| 4080 | "sids", &sids_size)); |
| 4081 | if (err) |
| 4082 | goto bail; |
| 4083 | |
| 4084 | VERIFY(err, NULL != (sids = kzalloc(sids_size, GFP_KERNEL))); |
| 4085 | if (err) |
| 4086 | goto bail; |
| 4087 | ret = of_property_read_u32_array(dev->of_node, "sids", sids, |
| 4088 | sids_size/sizeof(unsigned int)); |
| 4089 | if (ret) |
| 4090 | goto bail; |
| 4091 | |
| 4092 | VERIFY(err, !IS_ERR_OR_NULL(first_sess->smmu.mapping = |
| 4093 | arm_iommu_create_mapping(&platform_bus_type, |
| 4094 | start, 0x78000000))); |
| 4095 | if (err) |
| 4096 | goto bail; |
| 4097 | |
| 4098 | VERIFY(err, !arm_iommu_attach_device(dev, first_sess->smmu.mapping)); |
| 4099 | if (err) |
| 4100 | goto bail; |
| 4101 | |
| 4102 | |
| 4103 | for (i = 0; i < sids_size/sizeof(unsigned int); i++) { |
| 4104 | VERIFY(err, chan->sesscount < NUM_SESSIONS); |
| 4105 | if (err) |
| 4106 | goto bail; |
| 4107 | sess = &chan->session[chan->sesscount]; |
| 4108 | sess->smmu.cb = sids[i]; |
| 4109 | sess->smmu.dev = dev; |
| 4110 | sess->smmu.mapping = first_sess->smmu.mapping; |
| 4111 | sess->smmu.enabled = 1; |
| 4112 | sess->used = 0; |
| 4113 | sess->smmu.coherent = false; |
| 4114 | sess->smmu.secure = false; |
| 4115 | chan->sesscount++; |
| 4116 | } |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4117 | bail: |
| 4118 | kfree(sids); |
| 4119 | return err; |
| 4120 | } |
| 4121 | |
| 4122 | |
| 4123 | |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 4124 | static void init_secure_vmid_list(struct device *dev, char *prop_name, |
| 4125 | struct secure_vm *destvm) |
| 4126 | { |
| 4127 | int err = 0; |
| 4128 | u32 len = 0, i = 0; |
| 4129 | u32 *rhvmlist = NULL; |
| 4130 | u32 *rhvmpermlist = NULL; |
| 4131 | |
| 4132 | if (!of_find_property(dev->of_node, prop_name, &len)) |
| 4133 | goto bail; |
| 4134 | if (len == 0) |
| 4135 | goto bail; |
| 4136 | len /= sizeof(u32); |
| 4137 | VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL))); |
| 4138 | if (err) |
| 4139 | goto bail; |
| 4140 | VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32), |
| 4141 | GFP_KERNEL))); |
| 4142 | if (err) |
| 4143 | goto bail; |
| 4144 | for (i = 0; i < len; i++) { |
| 4145 | err = of_property_read_u32_index(dev->of_node, prop_name, i, |
| 4146 | &rhvmlist[i]); |
| 4147 | rhvmpermlist[i] = PERM_READ | PERM_WRITE | PERM_EXEC; |
| 4148 | pr_info("ADSPRPC: Secure VMID = %d", rhvmlist[i]); |
| 4149 | if (err) { |
| 4150 | pr_err("ADSPRPC: Failed to read VMID\n"); |
| 4151 | goto bail; |
| 4152 | } |
| 4153 | } |
| 4154 | destvm->vmid = rhvmlist; |
| 4155 | destvm->vmperm = rhvmpermlist; |
| 4156 | destvm->vmcount = len; |
| 4157 | bail: |
| 4158 | if (err) { |
| 4159 | kfree(rhvmlist); |
| 4160 | kfree(rhvmpermlist); |
| 4161 | } |
| 4162 | } |
| 4163 | |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4164 | static void configure_secure_channels(uint32_t secure_domains) |
| 4165 | { |
| 4166 | struct fastrpc_apps *me = &gfa; |
| 4167 | int ii = 0; |
| 4168 | /* |
| 4169 | * secure_domains contains the bitmask of the secure channels |
| 4170 | * Bit 0 - ADSP |
| 4171 | * Bit 1 - MDSP |
| 4172 | * Bit 2 - SLPI |
| 4173 | * Bit 3 - CDSP |
| 4174 | */ |
| 4175 | for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) { |
| 4176 | int secure = (secure_domains >> ii) & 0x01; |
| 4177 | |
| 4178 | me->channel[ii].secure = secure; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4183 | static int fastrpc_probe(struct platform_device *pdev) |
| 4184 | { |
| 4185 | int err = 0; |
| 4186 | struct fastrpc_apps *me = &gfa; |
| 4187 | struct device *dev = &pdev->dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4188 | struct device_node *ion_node, *node; |
| 4189 | struct platform_device *ion_pdev; |
| 4190 | struct cma *cma; |
| 4191 | uint32_t val; |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4192 | int ret = 0; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4193 | uint32_t secure_domains; |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4194 | |
| 4195 | if (of_device_is_compatible(dev->of_node, |
| 4196 | "qcom,msm-fastrpc-compute")) { |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 4197 | init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid", |
| 4198 | &gcinfo[0].rhvm); |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4199 | |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4200 | |
| 4201 | of_property_read_u32(dev->of_node, "qcom,rpc-latency-us", |
| 4202 | &me->latency); |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4203 | if (of_get_property(dev->of_node, |
| 4204 | "qcom,secure-domains", NULL) != NULL) { |
| 4205 | VERIFY(err, !of_property_read_u32(dev->of_node, |
| 4206 | "qcom,secure-domains", |
| 4207 | &secure_domains)); |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4208 | if (!err) { |
| 4209 | me->secure_flag = true; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4210 | configure_secure_channels(secure_domains); |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4211 | } else { |
| 4212 | me->secure_flag = false; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4213 | pr_info("adsprpc: unable to read the domain configuration from dts\n"); |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4214 | } |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4215 | } |
c_mtharu | 63ffc01 | 2017-11-16 15:26:56 +0530 | [diff] [blame] | 4216 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4217 | if (of_device_is_compatible(dev->of_node, |
| 4218 | "qcom,msm-fastrpc-compute-cb")) |
| 4219 | return fastrpc_cb_probe(dev); |
| 4220 | |
| 4221 | if (of_device_is_compatible(dev->of_node, |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4222 | "qcom,msm-fastrpc-legacy-compute")) { |
| 4223 | me->glink = false; |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4224 | me->legacy = 1; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | if (of_device_is_compatible(dev->of_node, |
| 4228 | "qcom,msm-fastrpc-legacy-compute-cb")){ |
| 4229 | return fastrpc_cb_legacy_probe(dev); |
| 4230 | } |
| 4231 | |
| 4232 | if (of_device_is_compatible(dev->of_node, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4233 | "qcom,msm-adsprpc-mem-region")) { |
| 4234 | me->dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4235 | ion_node = of_find_compatible_node(NULL, NULL, "qcom,msm-ion"); |
| 4236 | if (ion_node) { |
| 4237 | for_each_available_child_of_node(ion_node, node) { |
| 4238 | if (of_property_read_u32(node, "reg", &val)) |
| 4239 | continue; |
| 4240 | if (val != ION_ADSP_HEAP_ID) |
| 4241 | continue; |
| 4242 | ion_pdev = of_find_device_by_node(node); |
| 4243 | if (!ion_pdev) |
| 4244 | break; |
| 4245 | cma = dev_get_cma_area(&ion_pdev->dev); |
| 4246 | if (cma) { |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4247 | me->range.addr = cma_get_base(cma); |
| 4248 | me->range.size = |
| 4249 | (size_t)cma_get_size(cma); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4250 | } |
| 4251 | break; |
| 4252 | } |
| 4253 | } |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4254 | if (me->range.addr && !of_property_read_bool(dev->of_node, |
Tharun Kumar Merugu | 6b7a4a2 | 2018-01-17 16:08:07 +0530 | [diff] [blame] | 4255 | "restrict-access")) { |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4256 | int srcVM[1] = {VMID_HLOS}; |
| 4257 | int destVM[4] = {VMID_HLOS, VMID_MSS_MSA, VMID_SSC_Q6, |
| 4258 | VMID_ADSP_Q6}; |
Sathish Ambley | 84d1186 | 2017-05-15 14:36:05 -0700 | [diff] [blame] | 4259 | int destVMperm[4] = {PERM_READ | PERM_WRITE | PERM_EXEC, |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4260 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 4261 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 4262 | PERM_READ | PERM_WRITE | PERM_EXEC, |
| 4263 | }; |
| 4264 | |
Tharun Kumar Merugu | 4f2dcc8 | 2018-03-29 00:35:49 +0530 | [diff] [blame] | 4265 | VERIFY(err, !hyp_assign_phys(me->range.addr, |
| 4266 | me->range.size, srcVM, 1, |
| 4267 | destVM, destVMperm, 4)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4268 | if (err) |
| 4269 | goto bail; |
| 4270 | } |
| 4271 | return 0; |
| 4272 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4273 | if (of_property_read_bool(dev->of_node, |
| 4274 | "qcom,fastrpc-adsp-audio-pdr")) { |
| 4275 | int session; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4276 | |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4277 | VERIFY(err, !fastrpc_get_adsp_session( |
| 4278 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, &session)); |
| 4279 | if (err) |
| 4280 | goto spdbail; |
| 4281 | me->channel[0].spd[session].get_service_nb.notifier_call = |
| 4282 | fastrpc_get_service_location_notify; |
| 4283 | ret = get_service_location( |
| 4284 | AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME, |
| 4285 | AUDIO_PDR_ADSP_SERVICE_NAME, |
| 4286 | &me->channel[0].spd[session].get_service_nb); |
| 4287 | if (ret) |
| 4288 | pr_err("ADSPRPC: Get service location failed: %d\n", |
| 4289 | ret); |
| 4290 | } |
Tharun Kumar Merugu | 848c095 | 2018-02-07 21:37:19 +0530 | [diff] [blame] | 4291 | if (of_property_read_bool(dev->of_node, |
| 4292 | "qcom,fastrpc-adsp-sensors-pdr")) { |
| 4293 | int session; |
| 4294 | |
| 4295 | VERIFY(err, !fastrpc_get_adsp_session( |
| 4296 | SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, &session)); |
| 4297 | if (err) |
| 4298 | goto spdbail; |
| 4299 | me->channel[0].spd[session].get_service_nb.notifier_call = |
| 4300 | fastrpc_get_service_location_notify; |
| 4301 | ret = get_service_location( |
| 4302 | SENSORS_PDR_SERVICE_LOCATION_CLIENT_NAME, |
| 4303 | SENSORS_PDR_ADSP_SERVICE_NAME, |
| 4304 | &me->channel[0].spd[session].get_service_nb); |
| 4305 | if (ret) |
| 4306 | pr_err("ADSPRPC: Get service location failed: %d\n", |
| 4307 | ret); |
| 4308 | } |
Tharun Kumar Merugu | df86066 | 2018-01-17 19:59:50 +0530 | [diff] [blame] | 4309 | spdbail: |
| 4310 | err = 0; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4311 | VERIFY(err, !of_platform_populate(pdev->dev.of_node, |
| 4312 | fastrpc_match_table, |
| 4313 | NULL, &pdev->dev)); |
| 4314 | if (err) |
| 4315 | goto bail; |
| 4316 | bail: |
| 4317 | return err; |
| 4318 | } |
| 4319 | |
| 4320 | static void fastrpc_deinit(void) |
| 4321 | { |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 4322 | struct fastrpc_apps *me = &gfa; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4323 | struct fastrpc_channel_ctx *chan = gcinfo; |
| 4324 | int i, j; |
| 4325 | |
| 4326 | for (i = 0; i < NUM_CHANNELS; i++, chan++) { |
| 4327 | if (chan->chan) { |
| 4328 | kref_put_mutex(&chan->kref, |
Tharun Kumar Merugu | 642fcce | 2017-12-07 19:22:10 +0530 | [diff] [blame] | 4329 | fastrpc_channel_close, &me->smd_mutex); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4330 | chan->chan = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4331 | } |
| 4332 | for (j = 0; j < NUM_SESSIONS; j++) { |
| 4333 | struct fastrpc_session_ctx *sess = &chan->session[j]; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4334 | if (sess->smmu.dev) { |
| 4335 | arm_iommu_detach_device(sess->smmu.dev); |
| 4336 | sess->smmu.dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4337 | } |
| 4338 | if (sess->smmu.mapping) { |
| 4339 | arm_iommu_release_mapping(sess->smmu.mapping); |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4340 | sess->smmu.mapping = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4341 | } |
| 4342 | } |
Tharun Kumar Merugu | 3937e91 | 2017-12-21 16:24:37 +0530 | [diff] [blame] | 4343 | kfree(chan->rhvm.vmid); |
| 4344 | kfree(chan->rhvm.vmperm); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | static struct platform_driver fastrpc_driver = { |
| 4349 | .probe = fastrpc_probe, |
| 4350 | .driver = { |
| 4351 | .name = "fastrpc", |
| 4352 | .owner = THIS_MODULE, |
| 4353 | .of_match_table = fastrpc_match_table, |
| 4354 | }, |
| 4355 | }; |
| 4356 | |
| 4357 | static int __init fastrpc_device_init(void) |
| 4358 | { |
| 4359 | struct fastrpc_apps *me = &gfa; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4360 | struct device *dev = NULL; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4361 | struct device *secure_dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4362 | int err = 0, i; |
| 4363 | |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 4364 | debugfs_root = debugfs_create_dir("adsprpc", NULL); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4365 | memset(me, 0, sizeof(*me)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4366 | fastrpc_init(me); |
| 4367 | me->dev = NULL; |
Tharun Kumar Merugu | bbc78f2 | 2018-01-22 19:26:44 +0530 | [diff] [blame] | 4368 | me->glink = true; |
zhaochen | fc79857 | 2018-08-17 15:32:37 +0800 | [diff] [blame] | 4369 | me->secure_flag = false; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4370 | VERIFY(err, 0 == platform_driver_register(&fastrpc_driver)); |
| 4371 | if (err) |
| 4372 | goto register_bail; |
| 4373 | VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS, |
| 4374 | DEVICE_NAME)); |
| 4375 | if (err) |
| 4376 | goto alloc_chrdev_bail; |
| 4377 | cdev_init(&me->cdev, &fops); |
| 4378 | me->cdev.owner = THIS_MODULE; |
| 4379 | 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] | 4380 | NUM_DEVICES)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4381 | if (err) |
| 4382 | goto cdev_init_bail; |
| 4383 | me->class = class_create(THIS_MODULE, "fastrpc"); |
| 4384 | VERIFY(err, !IS_ERR(me->class)); |
| 4385 | if (err) |
| 4386 | goto class_create_bail; |
| 4387 | me->compat = (fops.compat_ioctl == NULL) ? 0 : 1; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4388 | |
| 4389 | /* |
| 4390 | * Create devices and register with sysfs |
| 4391 | * Create first device with minor number 0 |
| 4392 | */ |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4393 | dev = device_create(me->class, NULL, |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4394 | MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV), |
| 4395 | NULL, DEVICE_NAME); |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4396 | VERIFY(err, !IS_ERR_OR_NULL(dev)); |
| 4397 | if (err) |
| 4398 | goto device_create_bail; |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4399 | |
| 4400 | /* Create secure device with minor number for secure device */ |
| 4401 | secure_dev = device_create(me->class, NULL, |
| 4402 | MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV), |
| 4403 | NULL, DEVICE_NAME_SECURE); |
| 4404 | VERIFY(err, !IS_ERR_OR_NULL(secure_dev)); |
| 4405 | if (err) |
| 4406 | goto device_create_bail; |
| 4407 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4408 | for (i = 0; i < NUM_CHANNELS; i++) { |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4409 | me->channel[i].dev = secure_dev; |
| 4410 | if (i == CDSP_DOMAIN_ID) |
| 4411 | me->channel[i].dev = dev; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4412 | me->channel[i].ssrcount = 0; |
| 4413 | me->channel[i].prevssrcount = 0; |
c_mtharu | e1a5ce1 | 2017-10-13 20:47:09 +0530 | [diff] [blame] | 4414 | me->channel[i].issubsystemup = 1; |
| 4415 | me->channel[i].ramdumpenabled = 0; |
| 4416 | me->channel[i].remoteheap_ramdump_dev = NULL; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4417 | me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb; |
| 4418 | me->channel[i].handle = subsys_notif_register_notifier( |
| 4419 | gcinfo[i].subsys, |
| 4420 | &me->channel[i].nb); |
| 4421 | } |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4422 | me->client = msm_ion_client_create(DEVICE_NAME); |
| 4423 | VERIFY(err, !IS_ERR_OR_NULL(me->client)); |
| 4424 | if (err) |
| 4425 | goto device_create_bail; |
Mohammed Nayeem Ur Rahman | 492e386 | 2018-08-17 12:53:43 +0530 | [diff] [blame] | 4426 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4427 | return 0; |
| 4428 | device_create_bail: |
| 4429 | for (i = 0; i < NUM_CHANNELS; i++) { |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4430 | if (me->channel[i].handle) |
| 4431 | subsys_notif_unregister_notifier(me->channel[i].handle, |
| 4432 | &me->channel[i].nb); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4433 | } |
Sathish Ambley | 36849af | 2017-02-02 09:35:55 -0800 | [diff] [blame] | 4434 | if (!IS_ERR_OR_NULL(dev)) |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4435 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), |
| 4436 | MINOR_NUM_DEV)); |
| 4437 | if (!IS_ERR_OR_NULL(secure_dev)) |
| 4438 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), |
| 4439 | MINOR_NUM_SECURE_DEV)); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4440 | class_destroy(me->class); |
| 4441 | class_create_bail: |
| 4442 | cdev_del(&me->cdev); |
| 4443 | cdev_init_bail: |
| 4444 | unregister_chrdev_region(me->dev_no, NUM_CHANNELS); |
| 4445 | alloc_chrdev_bail: |
| 4446 | register_bail: |
| 4447 | fastrpc_deinit(); |
| 4448 | return err; |
| 4449 | } |
| 4450 | |
| 4451 | static void __exit fastrpc_device_exit(void) |
| 4452 | { |
| 4453 | struct fastrpc_apps *me = &gfa; |
| 4454 | int i; |
| 4455 | |
| 4456 | fastrpc_file_list_dtor(me); |
| 4457 | fastrpc_deinit(); |
| 4458 | for (i = 0; i < NUM_CHANNELS; i++) { |
| 4459 | if (!gcinfo[i].name) |
| 4460 | continue; |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4461 | subsys_notif_unregister_notifier(me->channel[i].handle, |
| 4462 | &me->channel[i].nb); |
| 4463 | } |
Tharun Kumar Merugu | d996b26 | 2018-07-18 22:28:53 +0530 | [diff] [blame] | 4464 | |
| 4465 | /* Destroy the secure and non secure devices */ |
| 4466 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV)); |
| 4467 | device_destroy(me->class, MKDEV(MAJOR(me->dev_no), |
| 4468 | MINOR_NUM_SECURE_DEV)); |
| 4469 | |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4470 | class_destroy(me->class); |
| 4471 | cdev_del(&me->cdev); |
| 4472 | unregister_chrdev_region(me->dev_no, NUM_CHANNELS); |
| 4473 | ion_client_destroy(me->client); |
Sathish Ambley | 1ca6823 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 4474 | debugfs_remove_recursive(debugfs_root); |
Sathish Ambley | 69e1ab0 | 2016-10-18 10:28:15 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | late_initcall(fastrpc_device_init); |
| 4478 | module_exit(fastrpc_device_exit); |
| 4479 | |
| 4480 | MODULE_LICENSE("GPL v2"); |