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