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