Mayank Rana | c295e34 | 2017-02-14 17:13:44 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/stat.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/of.h> |
Mayank Rana | c295e34 | 2017-02-14 17:13:44 -0800 | [diff] [blame] | 23 | #include <linux/msm-sps.h> |
| 24 | #include <linux/ipa.h> |
| 25 | #include <linux/usb_bam.h> |
| 26 | #include <linux/workqueue.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/pm_runtime.h> |
| 29 | |
| 30 | #define USB_THRESHOLD 512 |
| 31 | #define USB_BAM_MAX_STR_LEN 50 |
| 32 | #define USB_BAM_TIMEOUT (10*HZ) |
| 33 | #define DBG_MAX_MSG 512UL |
| 34 | #define DBG_MSG_LEN 160UL |
| 35 | #define TIME_BUF_LEN 17 |
| 36 | #define DBG_EVENT_LEN 143 |
| 37 | |
| 38 | #define USB_BAM_NR_PORTS 4 |
| 39 | |
| 40 | /* Additional memory to be allocated than required data fifo size */ |
| 41 | #define DATA_FIFO_EXTRA_MEM_ALLOC_SIZE 512 |
| 42 | |
| 43 | #define ARRAY_INDEX_FROM_ADDR(base, addr) ((addr) - (base)) |
| 44 | |
| 45 | #define ENABLE_EVENT_LOG 1 |
| 46 | static unsigned int enable_event_log = ENABLE_EVENT_LOG; |
| 47 | module_param(enable_event_log, uint, 0644); |
| 48 | MODULE_PARM_DESC(enable_event_log, "enable event logging in debug buffer"); |
| 49 | |
| 50 | #define LOGLEVEL_NONE 8 |
| 51 | #define LOGLEVEL_DEBUG 7 |
| 52 | #define LOGLEVEL_ERR 3 |
| 53 | |
| 54 | #define log_event(log_level, x...) \ |
| 55 | do { \ |
| 56 | unsigned long flags; \ |
| 57 | char *buf; \ |
| 58 | if (log_level == LOGLEVEL_DEBUG) \ |
| 59 | pr_debug(x); \ |
| 60 | else if (log_level == LOGLEVEL_ERR) \ |
| 61 | pr_err(x); \ |
| 62 | if (enable_event_log) { \ |
| 63 | write_lock_irqsave(&usb_bam_dbg.lck, flags); \ |
| 64 | buf = usb_bam_dbg.buf[usb_bam_dbg.idx]; \ |
| 65 | put_timestamp(buf); \ |
| 66 | snprintf(&buf[TIME_BUF_LEN - 1], DBG_EVENT_LEN, x); \ |
| 67 | usb_bam_dbg.idx = (usb_bam_dbg.idx + 1) % DBG_MAX_MSG; \ |
| 68 | write_unlock_irqrestore(&usb_bam_dbg.lck, flags); \ |
| 69 | } \ |
| 70 | } while (0) |
| 71 | |
| 72 | #define log_event_none(x, ...) log_event(LOGLEVEL_NONE, x, ##__VA_ARGS__) |
| 73 | #define log_event_dbg(x, ...) log_event(LOGLEVEL_DEBUG, x, ##__VA_ARGS__) |
| 74 | #define log_event_err(x, ...) log_event(LOGLEVEL_ERR, x, ##__VA_ARGS__) |
| 75 | |
| 76 | struct usb_bam_sps_type { |
| 77 | struct sps_pipe **sps_pipes; |
| 78 | struct sps_connect *sps_connections; |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * struct usb_bam_ctx_type - represents the usb bam driver entity |
| 83 | * @usb_bam_sps: holds the sps pipes the usb bam driver holds |
| 84 | * against the sps driver. |
| 85 | * @usb_bam_pdev: the platform device that represents the usb bam. |
| 86 | * @usb_bam_wq: Worqueue used for managing states of reset against |
| 87 | * a peer bam. |
| 88 | * @max_connections: The maximum number of pipes that are configured |
| 89 | * in the platform data. |
| 90 | * @h_bam: This array stores for each BAM ("ssusb", "hsusb" or "hsic") the |
| 91 | * handle/device of the sps driver. |
| 92 | * @pipes_enabled_per_bam: This array stores for each BAM |
| 93 | * ("ssusb", "hsusb" or "hsic") the number of pipes currently enabled. |
| 94 | * @inactivity_timer_ms: The timeout configuration per each bam for inactivity |
| 95 | * timer feature. |
| 96 | * @is_bam_inactivity: Is there no activity on all pipes belongs to a |
| 97 | * specific bam. (no activity = no data is pulled or pushed |
| 98 | * from/into ones of the pipes). |
| 99 | * @usb_bam_connections: array (allocated on probe) having all BAM connections |
| 100 | * @usb_bam_lock: to protect fields of ctx or usb_bam_connections |
| 101 | */ |
| 102 | struct usb_bam_ctx_type { |
| 103 | struct usb_bam_sps_type usb_bam_sps; |
| 104 | struct resource *io_res; |
| 105 | void __iomem *regs; |
| 106 | int irq; |
| 107 | struct platform_device *usb_bam_pdev; |
| 108 | struct workqueue_struct *usb_bam_wq; |
| 109 | u8 max_connections; |
| 110 | unsigned long h_bam; |
| 111 | u8 pipes_enabled_per_bam; |
| 112 | u32 inactivity_timer_ms; |
| 113 | bool is_bam_inactivity; |
| 114 | struct usb_bam_pipe_connect *usb_bam_connections; |
| 115 | spinlock_t usb_bam_lock; |
| 116 | }; |
| 117 | |
| 118 | static char *bam_enable_strings[MAX_BAMS] = { |
| 119 | [DWC3_CTRL] = "ssusb", |
| 120 | [CI_CTRL] = "hsusb", |
| 121 | [HSIC_CTRL] = "hsic", |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | * CI_CTRL & DWC3_CTRL shouldn't be used simultaneously |
| 126 | * since both share the same prod & cons rm resourses |
| 127 | */ |
| 128 | static enum ipa_client_type ipa_rm_resource_prod[MAX_BAMS] = { |
| 129 | [CI_CTRL] = IPA_RM_RESOURCE_USB_PROD, |
| 130 | [HSIC_CTRL] = IPA_RM_RESOURCE_HSIC_PROD, |
| 131 | [DWC3_CTRL] = IPA_RM_RESOURCE_USB_PROD, |
| 132 | }; |
| 133 | |
| 134 | static enum ipa_client_type ipa_rm_resource_cons[MAX_BAMS] = { |
| 135 | [CI_CTRL] = IPA_RM_RESOURCE_USB_CONS, |
| 136 | [HSIC_CTRL] = IPA_RM_RESOURCE_HSIC_CONS, |
| 137 | [DWC3_CTRL] = IPA_RM_RESOURCE_USB_CONS, |
| 138 | }; |
| 139 | |
| 140 | static int usb_cons_request_resource(void); |
| 141 | static int usb_cons_release_resource(void); |
| 142 | static int ss_usb_cons_request_resource(void); |
| 143 | static int ss_usb_cons_release_resource(void); |
| 144 | static int hsic_cons_request_resource(void); |
| 145 | static int hsic_cons_release_resource(void); |
| 146 | |
| 147 | static int (*request_resource_cb[MAX_BAMS])(void) = { |
| 148 | [CI_CTRL] = usb_cons_request_resource, |
| 149 | [HSIC_CTRL] = hsic_cons_request_resource, |
| 150 | [DWC3_CTRL] = ss_usb_cons_request_resource, |
| 151 | }; |
| 152 | |
| 153 | static int (*release_resource_cb[MAX_BAMS])(void) = { |
| 154 | [CI_CTRL] = usb_cons_release_resource, |
| 155 | [HSIC_CTRL] = hsic_cons_release_resource, |
| 156 | [DWC3_CTRL] = ss_usb_cons_release_resource, |
| 157 | }; |
| 158 | |
| 159 | struct usb_bam_ipa_handshake_info { |
| 160 | enum ipa_rm_event cur_prod_state; |
| 161 | enum ipa_rm_event cur_cons_state; |
| 162 | |
| 163 | enum usb_bam_mode cur_bam_mode; |
| 164 | enum usb_ctrl bam_type; |
| 165 | int connect_complete; |
| 166 | int bus_suspend; |
| 167 | bool disconnected; |
| 168 | bool in_lpm; |
| 169 | u8 prod_pipes_enabled_per_bam; |
| 170 | |
| 171 | int (*wake_cb)(void *); |
| 172 | void *wake_param; |
| 173 | |
| 174 | u32 suspend_src_idx[USB_BAM_NR_PORTS]; |
| 175 | u32 suspend_dst_idx[USB_BAM_NR_PORTS]; |
| 176 | u32 resume_src_idx[USB_BAM_NR_PORTS]; |
| 177 | u32 resume_dst_idx[USB_BAM_NR_PORTS]; |
| 178 | |
| 179 | u32 pipes_to_suspend; |
| 180 | u32 pipes_suspended; |
| 181 | u32 pipes_resumed; |
| 182 | |
| 183 | struct completion prod_avail; |
| 184 | struct completion prod_released; |
| 185 | |
| 186 | struct mutex suspend_resume_mutex; |
| 187 | struct work_struct resume_work; |
| 188 | struct work_struct finish_suspend_work; |
| 189 | }; |
| 190 | |
| 191 | struct usb_bam_host_info { |
| 192 | struct device *dev; |
| 193 | bool in_lpm; |
| 194 | }; |
| 195 | |
| 196 | static spinlock_t usb_bam_ipa_handshake_info_lock; |
| 197 | static struct usb_bam_ipa_handshake_info info[MAX_BAMS]; |
| 198 | |
| 199 | static struct usb_bam_ctx_type msm_usb_bam[MAX_BAMS]; |
| 200 | /* USB bam type used as a peer of the qdss in bam2bam mode */ |
| 201 | static enum usb_ctrl qdss_usb_bam_type; |
| 202 | |
| 203 | static struct usb_bam_host_info host_info[MAX_BAMS]; |
| 204 | |
| 205 | static int __usb_bam_register_wake_cb(enum usb_ctrl bam_type, int idx, |
| 206 | int (*callback)(void *user), |
| 207 | void *param, bool trigger_cb_per_pipe); |
| 208 | static void wait_for_prod_release(enum usb_ctrl cur_bam); |
| 209 | static void usb_bam_start_suspend(struct usb_bam_ipa_handshake_info *info_ptr); |
| 210 | |
| 211 | static struct { |
| 212 | char buf[DBG_MAX_MSG][DBG_MSG_LEN]; /* buffer */ |
| 213 | unsigned int idx; /* index */ |
| 214 | rwlock_t lck; /* lock */ |
| 215 | } __maybe_unused usb_bam_dbg = { |
| 216 | .idx = 0, |
| 217 | .lck = __RW_LOCK_UNLOCKED(lck) |
| 218 | }; |
| 219 | |
| 220 | /*put_timestamp - writes time stamp to buffer */ |
| 221 | static void __maybe_unused put_timestamp(char *tbuf) |
| 222 | { |
| 223 | unsigned long long t; |
| 224 | unsigned long nanosec_rem; |
| 225 | |
| 226 | t = cpu_clock(smp_processor_id()); |
| 227 | nanosec_rem = do_div(t, 1000000000)/1000; |
| 228 | snprintf(tbuf, TIME_BUF_LEN, "[%5lu.%06lu]: ", (unsigned long)t, |
| 229 | nanosec_rem); |
| 230 | } |
| 231 | |
| 232 | void msm_bam_set_hsic_host_dev(struct device *dev) |
| 233 | { |
| 234 | if (dev) { |
| 235 | /* Hold the device until allowing lpm */ |
| 236 | info[HSIC_CTRL].in_lpm = false; |
| 237 | log_event_dbg("%s: Getting hsic device %p\n", __func__, dev); |
| 238 | pm_runtime_get(dev); |
| 239 | } else if (host_info[HSIC_CTRL].dev) { |
| 240 | log_event_dbg("%s: Try Putting hsic device %p, lpm:%d\n", |
| 241 | __func__, host_info[HSIC_CTRL].dev, |
| 242 | info[HSIC_CTRL].in_lpm); |
| 243 | /* Just release previous device if not already done */ |
| 244 | if (!info[HSIC_CTRL].in_lpm) { |
| 245 | info[HSIC_CTRL].in_lpm = true; |
| 246 | pm_runtime_put(host_info[HSIC_CTRL].dev); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | host_info[HSIC_CTRL].dev = dev; |
| 251 | host_info[HSIC_CTRL].in_lpm = false; |
| 252 | } |
| 253 | |
| 254 | void msm_bam_set_usb_host_dev(struct device *dev) |
| 255 | { |
| 256 | host_info[CI_CTRL].dev = dev; |
| 257 | host_info[CI_CTRL].in_lpm = false; |
| 258 | } |
| 259 | |
| 260 | static int get_bam_type_from_core_name(const char *name) |
| 261 | { |
| 262 | if (strnstr(name, bam_enable_strings[DWC3_CTRL], |
| 263 | USB_BAM_MAX_STR_LEN) || |
| 264 | strnstr(name, "dwc3", USB_BAM_MAX_STR_LEN)) |
| 265 | return DWC3_CTRL; |
| 266 | else if (strnstr(name, bam_enable_strings[HSIC_CTRL], |
| 267 | USB_BAM_MAX_STR_LEN) || |
| 268 | strnstr(name, "ci13xxx_msm_hsic", USB_BAM_MAX_STR_LEN)) |
| 269 | return HSIC_CTRL; |
| 270 | else if (strnstr(name, bam_enable_strings[CI_CTRL], |
| 271 | USB_BAM_MAX_STR_LEN) || |
| 272 | strnstr(name, "ci", USB_BAM_MAX_STR_LEN)) |
| 273 | return CI_CTRL; |
| 274 | |
| 275 | log_event_err("%s: invalid BAM name(%s)\n", __func__, name); |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
| 279 | static void usb_bam_set_inactivity_timer(enum usb_ctrl bam) |
| 280 | { |
| 281 | struct sps_timer_ctrl timer_ctrl; |
| 282 | struct usb_bam_pipe_connect *pipe_connect; |
| 283 | struct sps_pipe *pipe = NULL; |
| 284 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam]; |
| 285 | int i; |
| 286 | |
| 287 | log_event_dbg("%s: enter\n", __func__); |
| 288 | |
| 289 | /* |
| 290 | * Since we configure global incativity timer for all pipes |
| 291 | * and not per each pipe, it is enough to use some pipe |
| 292 | * handle associated with this bam, so just find the first one. |
| 293 | * This pipe handle is required due to SPS driver API we use below. |
| 294 | */ |
| 295 | for (i = 0; i < ctx->max_connections; i++) { |
| 296 | pipe_connect = &ctx->usb_bam_connections[i]; |
| 297 | if (pipe_connect->bam_type == bam && pipe_connect->enabled) { |
| 298 | pipe = ctx->usb_bam_sps.sps_pipes[i]; |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (!pipe) { |
| 304 | pr_warn("%s: Bam %s has no connected pipes\n", __func__, |
| 305 | bam_enable_strings[bam]); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | timer_ctrl.op = SPS_TIMER_OP_CONFIG; |
| 310 | timer_ctrl.mode = SPS_TIMER_MODE_ONESHOT; |
| 311 | timer_ctrl.timeout_msec = ctx->inactivity_timer_ms; |
| 312 | sps_timer_ctrl(pipe, &timer_ctrl, NULL); |
| 313 | |
| 314 | timer_ctrl.op = SPS_TIMER_OP_RESET; |
| 315 | sps_timer_ctrl(pipe, &timer_ctrl, NULL); |
| 316 | } |
| 317 | |
| 318 | static int usb_bam_alloc_buffer(struct usb_bam_pipe_connect *pipe_connect) |
| 319 | { |
| 320 | int ret = 0; |
| 321 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[pipe_connect->bam_type]; |
| 322 | struct msm_usb_bam_platform_data *pdata = |
| 323 | ctx->usb_bam_pdev->dev.platform_data; |
| 324 | struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf); |
| 325 | struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf); |
| 326 | |
| 327 | pr_debug("%s: data_fifo size:%x desc_fifo_size:%x\n", |
| 328 | __func__, pipe_connect->data_fifo_size, |
| 329 | pipe_connect->desc_fifo_size); |
| 330 | switch (pipe_connect->mem_type) { |
| 331 | case SPS_PIPE_MEM: |
| 332 | log_event_dbg("%s: USB BAM using SPS pipe memory\n", __func__); |
| 333 | ret = sps_setup_bam2bam_fifo(data_buf, |
| 334 | pipe_connect->data_fifo_base_offset, |
| 335 | pipe_connect->data_fifo_size, 1); |
| 336 | if (ret) { |
| 337 | log_event_err("%s: data fifo setup failure %d\n", |
| 338 | __func__, ret); |
| 339 | goto err_exit; |
| 340 | } |
| 341 | |
| 342 | ret = sps_setup_bam2bam_fifo(desc_buf, |
| 343 | pipe_connect->desc_fifo_base_offset, |
| 344 | pipe_connect->desc_fifo_size, 1); |
| 345 | if (ret) { |
| 346 | log_event_err("%s: desc. fifo setup failure %d\n", |
| 347 | __func__, ret); |
| 348 | goto err_exit; |
| 349 | } |
| 350 | break; |
| 351 | case OCI_MEM: |
| 352 | if (pipe_connect->mem_type == OCI_MEM) |
| 353 | log_event_dbg("%s: USB BAM using ocimem\n", __func__); |
| 354 | |
| 355 | if (data_buf->base) { |
| 356 | log_event_err("%s: Already allocated OCI Memory\n", |
| 357 | __func__); |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | data_buf->phys_base = pipe_connect->data_fifo_base_offset + |
| 362 | pdata->usb_bam_fifo_baseaddr; |
| 363 | data_buf->size = pipe_connect->data_fifo_size; |
| 364 | data_buf->base = ioremap(data_buf->phys_base, data_buf->size); |
| 365 | if (!data_buf->base) { |
| 366 | log_event_err("%s: ioremap failed for data fifo\n", |
| 367 | __func__); |
| 368 | ret = -ENOMEM; |
| 369 | goto err_exit; |
| 370 | } |
| 371 | memset_io(data_buf->base, 0, data_buf->size); |
| 372 | |
| 373 | desc_buf->phys_base = pipe_connect->desc_fifo_base_offset + |
| 374 | pdata->usb_bam_fifo_baseaddr; |
| 375 | desc_buf->size = pipe_connect->desc_fifo_size; |
| 376 | desc_buf->base = ioremap(desc_buf->phys_base, desc_buf->size); |
| 377 | if (!desc_buf->base) { |
| 378 | log_event_err("%s: ioremap failed for desc fifo\n", |
| 379 | __func__); |
| 380 | iounmap(data_buf->base); |
| 381 | ret = -ENOMEM; |
| 382 | goto err_exit; |
| 383 | } |
| 384 | memset_io(desc_buf->base, 0, desc_buf->size); |
| 385 | break; |
| 386 | case SYSTEM_MEM: |
| 387 | log_event_dbg("%s: USB BAM using system memory\n", __func__); |
| 388 | |
| 389 | if (data_buf->base) { |
| 390 | log_event_err("%s: Already allocated memory\n", |
| 391 | __func__); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | /* BAM would use system memory, allocate FIFOs */ |
| 396 | data_buf->size = pipe_connect->data_fifo_size; |
| 397 | /* On platforms which use CI controller, USB HW can fetch |
| 398 | * additional 128 bytes at the end of circular buffer when |
| 399 | * AXI prefetch is enabled and hence requirement is to |
| 400 | * allocate 512 bytes more than required length. |
| 401 | */ |
| 402 | if (pipe_connect->bam_type == CI_CTRL) |
| 403 | data_buf->base = |
| 404 | dma_alloc_coherent(&ctx->usb_bam_pdev->dev, |
| 405 | (pipe_connect->data_fifo_size + |
| 406 | DATA_FIFO_EXTRA_MEM_ALLOC_SIZE), |
| 407 | &(data_buf->phys_base), |
| 408 | GFP_KERNEL); |
| 409 | else |
| 410 | data_buf->base = |
| 411 | dma_alloc_coherent(&ctx->usb_bam_pdev->dev, |
| 412 | pipe_connect->data_fifo_size, |
| 413 | &(data_buf->phys_base), |
| 414 | GFP_KERNEL); |
| 415 | if (!data_buf->base) { |
| 416 | log_event_err("%s: dma_alloc_coherent failed for data fifo\n", |
| 417 | __func__); |
| 418 | ret = -ENOMEM; |
| 419 | goto err_exit; |
| 420 | } |
| 421 | memset(data_buf->base, 0, pipe_connect->data_fifo_size); |
| 422 | |
| 423 | desc_buf->size = pipe_connect->desc_fifo_size; |
| 424 | desc_buf->base = dma_alloc_coherent(&ctx->usb_bam_pdev->dev, |
| 425 | pipe_connect->desc_fifo_size, |
| 426 | &(desc_buf->phys_base), |
| 427 | GFP_KERNEL); |
| 428 | if (!desc_buf->base) { |
| 429 | log_event_err("%s: dma_alloc_coherent failed for desc fifo\n", |
| 430 | __func__); |
| 431 | if (pipe_connect->bam_type == CI_CTRL) |
| 432 | dma_free_coherent(&ctx->usb_bam_pdev->dev, |
| 433 | (pipe_connect->data_fifo_size + |
| 434 | DATA_FIFO_EXTRA_MEM_ALLOC_SIZE), |
| 435 | data_buf->base, |
| 436 | data_buf->phys_base); |
| 437 | else |
| 438 | dma_free_coherent(&ctx->usb_bam_pdev->dev, |
| 439 | pipe_connect->data_fifo_size, |
| 440 | data_buf->base, |
| 441 | data_buf->phys_base); |
| 442 | ret = -ENOMEM; |
| 443 | goto err_exit; |
| 444 | } |
| 445 | memset(desc_buf->base, 0, pipe_connect->desc_fifo_size); |
| 446 | break; |
| 447 | default: |
| 448 | log_event_err("%s: invalid mem type\n", __func__); |
| 449 | ret = -EINVAL; |
| 450 | } |
| 451 | |
| 452 | return ret; |
| 453 | |
| 454 | err_exit: |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | int usb_bam_alloc_fifos(enum usb_ctrl cur_bam, u8 idx) |
| 459 | { |
| 460 | int ret; |
| 461 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 462 | struct usb_bam_pipe_connect *pipe_connect = |
| 463 | &ctx->usb_bam_connections[idx]; |
| 464 | |
| 465 | ret = usb_bam_alloc_buffer(pipe_connect); |
| 466 | if (ret) { |
| 467 | log_event_err("%s(): Error(%d) allocating buffer\n", |
| 468 | __func__, ret); |
| 469 | return ret; |
| 470 | } |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | int usb_bam_free_fifos(enum usb_ctrl cur_bam, u8 idx) |
| 475 | { |
| 476 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 477 | struct usb_bam_pipe_connect *pipe_connect = |
| 478 | &ctx->usb_bam_connections[idx]; |
| 479 | struct sps_connect *sps_connection = |
| 480 | &ctx->usb_bam_sps.sps_connections[idx]; |
| 481 | |
| 482 | pr_debug("%s(): data size:%x desc size:%x\n", |
| 483 | __func__, sps_connection->data.size, |
| 484 | sps_connection->desc.size); |
| 485 | |
| 486 | switch (pipe_connect->mem_type) { |
| 487 | case SYSTEM_MEM: |
| 488 | log_event_dbg("%s: Freeing system memory used by PIPE\n", |
| 489 | __func__); |
| 490 | if (sps_connection->data.phys_base) { |
| 491 | if (cur_bam == CI_CTRL) |
| 492 | dma_free_coherent(&ctx->usb_bam_pdev->dev, |
| 493 | (sps_connection->data.size + |
| 494 | DATA_FIFO_EXTRA_MEM_ALLOC_SIZE), |
| 495 | sps_connection->data.base, |
| 496 | sps_connection->data.phys_base); |
| 497 | else |
| 498 | dma_free_coherent(&ctx->usb_bam_pdev->dev, |
| 499 | sps_connection->data.size, |
| 500 | sps_connection->data.base, |
| 501 | sps_connection->data.phys_base); |
| 502 | sps_connection->data.phys_base = 0; |
| 503 | pipe_connect->data_mem_buf.base = NULL; |
| 504 | } |
| 505 | if (sps_connection->desc.phys_base) { |
| 506 | dma_free_coherent(&ctx->usb_bam_pdev->dev, |
| 507 | sps_connection->desc.size, |
| 508 | sps_connection->desc.base, |
| 509 | sps_connection->desc.phys_base); |
| 510 | sps_connection->desc.phys_base = 0; |
| 511 | pipe_connect->desc_mem_buf.base = NULL; |
| 512 | } |
| 513 | break; |
| 514 | case OCI_MEM: |
| 515 | log_event_dbg("Freeing oci memory used by BAM PIPE\n"); |
| 516 | if (sps_connection->data.base) { |
| 517 | iounmap(sps_connection->data.base); |
| 518 | sps_connection->data.base = NULL; |
| 519 | pipe_connect->data_mem_buf.base = NULL; |
| 520 | } |
| 521 | if (sps_connection->desc.base) { |
| 522 | iounmap(sps_connection->desc.base); |
| 523 | sps_connection->desc.base = NULL; |
| 524 | pipe_connect->desc_mem_buf.base = NULL; |
| 525 | } |
| 526 | break; |
| 527 | case SPS_PIPE_MEM: |
| 528 | log_event_dbg("%s: nothing to be be\n", __func__); |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static int connect_pipe(enum usb_ctrl cur_bam, u8 idx, u32 *usb_pipe_idx) |
| 536 | { |
| 537 | int ret; |
| 538 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 539 | struct usb_bam_sps_type usb_bam_sps = ctx->usb_bam_sps; |
| 540 | struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]); |
| 541 | struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx]; |
| 542 | struct usb_bam_pipe_connect *pipe_connect = |
| 543 | &ctx->usb_bam_connections[idx]; |
| 544 | enum usb_bam_pipe_dir dir = pipe_connect->dir; |
| 545 | struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf); |
| 546 | struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf); |
| 547 | |
| 548 | *pipe = sps_alloc_endpoint(); |
| 549 | if (*pipe == NULL) { |
| 550 | log_event_err("%s: sps_alloc_endpoint failed\n", __func__); |
| 551 | return -ENOMEM; |
| 552 | } |
| 553 | |
| 554 | ret = sps_get_config(*pipe, sps_connection); |
| 555 | if (ret) { |
| 556 | log_event_err("%s: tx get config failed %d\n", __func__, ret); |
| 557 | goto free_sps_endpoint; |
| 558 | } |
| 559 | |
| 560 | ret = sps_phy2h(pipe_connect->src_phy_addr, &(sps_connection->source)); |
| 561 | if (ret) { |
| 562 | log_event_err("%s: sps_phy2h failed (src BAM) %d\n", |
| 563 | __func__, ret); |
| 564 | goto free_sps_endpoint; |
| 565 | } |
| 566 | |
| 567 | sps_connection->src_pipe_index = pipe_connect->src_pipe_index; |
| 568 | ret = sps_phy2h(pipe_connect->dst_phy_addr, |
| 569 | &(sps_connection->destination)); |
| 570 | if (ret) { |
| 571 | log_event_err("%s: sps_phy2h failed (dst BAM) %d\n", |
| 572 | __func__, ret); |
| 573 | goto free_sps_endpoint; |
| 574 | } |
| 575 | sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index; |
| 576 | |
| 577 | if (dir == USB_TO_PEER_PERIPHERAL) { |
| 578 | sps_connection->mode = SPS_MODE_SRC; |
| 579 | *usb_pipe_idx = pipe_connect->src_pipe_index; |
| 580 | } else { |
| 581 | sps_connection->mode = SPS_MODE_DEST; |
| 582 | *usb_pipe_idx = pipe_connect->dst_pipe_index; |
| 583 | } |
| 584 | |
| 585 | sps_connection->data = *data_buf; |
| 586 | sps_connection->desc = *desc_buf; |
| 587 | sps_connection->event_thresh = 16; |
| 588 | sps_connection->options = SPS_O_AUTO_ENABLE; |
| 589 | |
| 590 | ret = sps_connect(*pipe, sps_connection); |
| 591 | if (ret < 0) { |
| 592 | log_event_err("%s: sps_connect failed %d\n", __func__, ret); |
| 593 | goto error; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | error: |
| 599 | sps_disconnect(*pipe); |
| 600 | free_sps_endpoint: |
| 601 | sps_free_endpoint(*pipe); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static int connect_pipe_sys2bam_ipa(enum usb_ctrl cur_bam, u8 idx, |
| 606 | struct usb_bam_connect_ipa_params *ipa_params) |
| 607 | { |
| 608 | int ret; |
| 609 | enum usb_bam_pipe_dir dir = ipa_params->dir; |
| 610 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 611 | struct usb_bam_pipe_connect *pipe_connect = |
| 612 | &ctx->usb_bam_connections[idx]; |
| 613 | struct ipa_sys_connect_params sys_in_params; |
| 614 | unsigned long usb_handle; |
| 615 | phys_addr_t usb_phy_addr; |
| 616 | u32 clnt_hdl = 0; |
| 617 | |
| 618 | memset(&sys_in_params, 0, sizeof(sys_in_params)); |
| 619 | |
| 620 | if (dir == USB_TO_PEER_PERIPHERAL) { |
| 621 | usb_phy_addr = pipe_connect->src_phy_addr; |
| 622 | sys_in_params.client = ipa_params->src_client; |
| 623 | ipa_params->ipa_cons_ep_idx = |
| 624 | ipa_get_ep_mapping(sys_in_params.client); |
| 625 | } else { |
| 626 | usb_phy_addr = pipe_connect->dst_phy_addr; |
| 627 | sys_in_params.client = ipa_params->dst_client; |
| 628 | ipa_params->ipa_prod_ep_idx = |
| 629 | ipa_get_ep_mapping(sys_in_params.client); |
| 630 | } |
| 631 | |
| 632 | log_event_dbg("%s(): ipa_prod_ep_idx:%d ipa_cons_ep_idx:%d\n", |
| 633 | __func__, ipa_params->ipa_prod_ep_idx, |
| 634 | ipa_params->ipa_cons_ep_idx); |
| 635 | |
| 636 | /* Get HSUSB / HSIC bam handle */ |
| 637 | ret = sps_phy2h(usb_phy_addr, &usb_handle); |
| 638 | if (ret) { |
| 639 | log_event_err("%s: sps_phy2h failed (HSUSB/HSIC BAM) %d\n", |
| 640 | __func__, ret); |
| 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | pipe_connect->activity_notify = ipa_params->activity_notify; |
| 645 | pipe_connect->inactivity_notify = ipa_params->inactivity_notify; |
| 646 | pipe_connect->priv = ipa_params->priv; |
| 647 | |
| 648 | /* IPA sys connection params */ |
| 649 | sys_in_params.desc_fifo_sz = pipe_connect->desc_fifo_size; |
| 650 | sys_in_params.priv = ipa_params->priv; |
| 651 | sys_in_params.notify = ipa_params->notify; |
| 652 | sys_in_params.skip_ep_cfg = ipa_params->skip_ep_cfg; |
| 653 | sys_in_params.keep_ipa_awake = ipa_params->keep_ipa_awake; |
| 654 | memcpy(&sys_in_params.ipa_ep_cfg, &ipa_params->ipa_ep_cfg, |
| 655 | sizeof(struct ipa_ep_cfg)); |
| 656 | |
| 657 | ret = ipa_setup_sys_pipe(&sys_in_params, &clnt_hdl); |
| 658 | if (ret) { |
| 659 | log_event_err("%s: ipa_connect failed\n", __func__); |
| 660 | return ret; |
| 661 | } |
| 662 | pipe_connect->ipa_clnt_hdl = clnt_hdl; |
| 663 | if (dir == USB_TO_PEER_PERIPHERAL) |
| 664 | ipa_params->cons_clnt_hdl = clnt_hdl; |
| 665 | else |
| 666 | ipa_params->prod_clnt_hdl = clnt_hdl; |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static int connect_pipe_bam2bam_ipa(enum usb_ctrl cur_bam, u8 idx, |
| 672 | struct usb_bam_connect_ipa_params *ipa_params) |
| 673 | { |
| 674 | int ret; |
| 675 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 676 | struct usb_bam_sps_type usb_bam_sps = ctx->usb_bam_sps; |
| 677 | enum usb_bam_pipe_dir dir = ipa_params->dir; |
| 678 | struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]); |
| 679 | struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx]; |
| 680 | struct usb_bam_pipe_connect *pipe_connect = |
| 681 | &ctx->usb_bam_connections[idx]; |
| 682 | struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf); |
| 683 | struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf); |
| 684 | struct ipa_connect_params ipa_in_params; |
| 685 | struct ipa_sps_params sps_out_params; |
| 686 | u32 usb_phy_addr; |
| 687 | unsigned long usb_handle; |
| 688 | u32 clnt_hdl = 0; |
| 689 | |
| 690 | memset(&ipa_in_params, 0, sizeof(ipa_in_params)); |
| 691 | memset(&sps_out_params, 0, sizeof(sps_out_params)); |
| 692 | |
| 693 | if (dir == USB_TO_PEER_PERIPHERAL) { |
| 694 | usb_phy_addr = pipe_connect->src_phy_addr; |
| 695 | ipa_in_params.client_ep_idx = pipe_connect->src_pipe_index; |
| 696 | ipa_in_params.client = ipa_params->src_client; |
| 697 | } else { |
| 698 | usb_phy_addr = pipe_connect->dst_phy_addr; |
| 699 | ipa_in_params.client_ep_idx = pipe_connect->dst_pipe_index; |
| 700 | ipa_in_params.client = ipa_params->dst_client; |
| 701 | } |
| 702 | /* Get HSUSB / HSIC bam handle */ |
| 703 | ret = sps_phy2h(usb_phy_addr, &usb_handle); |
| 704 | if (ret) { |
| 705 | log_event_err("%s: sps_phy2h failed (HSUSB/HSIC BAM) %d\n", |
| 706 | __func__, ret); |
| 707 | return ret; |
| 708 | } |
| 709 | |
| 710 | pipe_connect->activity_notify = ipa_params->activity_notify; |
| 711 | pipe_connect->inactivity_notify = ipa_params->inactivity_notify; |
| 712 | pipe_connect->priv = ipa_params->priv; |
| 713 | pipe_connect->reset_pipe_after_lpm = ipa_params->reset_pipe_after_lpm; |
| 714 | |
| 715 | /* IPA input parameters */ |
| 716 | ipa_in_params.client_bam_hdl = usb_handle; |
| 717 | ipa_in_params.desc_fifo_sz = pipe_connect->desc_fifo_size; |
| 718 | ipa_in_params.data_fifo_sz = pipe_connect->data_fifo_size; |
| 719 | ipa_in_params.notify = ipa_params->notify; |
| 720 | ipa_in_params.priv = ipa_params->priv; |
| 721 | ipa_in_params.skip_ep_cfg = ipa_params->skip_ep_cfg; |
| 722 | ipa_in_params.keep_ipa_awake = ipa_params->keep_ipa_awake; |
| 723 | |
| 724 | ipa_in_params.desc = pipe_connect->desc_mem_buf; |
| 725 | ipa_in_params.data = pipe_connect->data_mem_buf; |
| 726 | |
| 727 | memcpy(&ipa_in_params.ipa_ep_cfg, &ipa_params->ipa_ep_cfg, |
| 728 | sizeof(struct ipa_ep_cfg)); |
| 729 | |
| 730 | ret = ipa_connect(&ipa_in_params, &sps_out_params, &clnt_hdl); |
| 731 | if (ret) { |
| 732 | log_event_err("%s: ipa_connect failed\n", __func__); |
| 733 | return ret; |
| 734 | } |
| 735 | pipe_connect->ipa_clnt_hdl = clnt_hdl; |
| 736 | |
| 737 | *pipe = sps_alloc_endpoint(); |
| 738 | if (*pipe == NULL) { |
| 739 | log_event_err("%s: sps_alloc_endpoint failed\n", __func__); |
| 740 | ret = -ENOMEM; |
| 741 | goto disconnect_ipa; |
| 742 | } |
| 743 | |
| 744 | ret = sps_get_config(*pipe, sps_connection); |
| 745 | if (ret) { |
| 746 | log_event_err("%s: tx get config failed %d\n", __func__, ret); |
| 747 | goto free_sps_endpoints; |
| 748 | } |
| 749 | |
| 750 | if (dir == USB_TO_PEER_PERIPHERAL) { |
| 751 | /* USB src IPA dest */ |
| 752 | sps_connection->mode = SPS_MODE_SRC; |
| 753 | ipa_params->cons_clnt_hdl = clnt_hdl; |
| 754 | sps_connection->source = usb_handle; |
| 755 | sps_connection->destination = sps_out_params.ipa_bam_hdl; |
| 756 | sps_connection->src_pipe_index = pipe_connect->src_pipe_index; |
| 757 | sps_connection->dest_pipe_index = sps_out_params.ipa_ep_idx; |
| 758 | ipa_params->ipa_cons_ep_idx = sps_out_params.ipa_ep_idx; |
| 759 | *(ipa_params->src_pipe) = sps_connection->src_pipe_index; |
| 760 | pipe_connect->dst_pipe_index = sps_out_params.ipa_ep_idx; |
| 761 | log_event_dbg("%s: BAM pipe usb[%x]->ipa[%x] connection\n", |
| 762 | __func__, |
| 763 | pipe_connect->src_pipe_index, |
| 764 | pipe_connect->dst_pipe_index); |
| 765 | sps_connection->options = SPS_O_NO_DISABLE; |
| 766 | } else { |
| 767 | /* IPA src, USB dest */ |
| 768 | sps_connection->mode = SPS_MODE_DEST; |
| 769 | ipa_params->prod_clnt_hdl = clnt_hdl; |
| 770 | sps_connection->source = sps_out_params.ipa_bam_hdl; |
| 771 | sps_connection->destination = usb_handle; |
| 772 | sps_connection->src_pipe_index = sps_out_params.ipa_ep_idx; |
| 773 | ipa_params->ipa_prod_ep_idx = sps_out_params.ipa_ep_idx; |
| 774 | sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index; |
| 775 | *(ipa_params->dst_pipe) = sps_connection->dest_pipe_index; |
| 776 | pipe_connect->src_pipe_index = sps_out_params.ipa_ep_idx; |
| 777 | log_event_dbg("%s: BAM pipe ipa[%x]->usb[%x] connection\n", |
| 778 | __func__, |
| 779 | pipe_connect->src_pipe_index, |
| 780 | pipe_connect->dst_pipe_index); |
| 781 | sps_connection->options = 0; |
| 782 | } |
| 783 | |
| 784 | sps_connection->data = *data_buf; |
| 785 | sps_connection->desc = *desc_buf; |
| 786 | sps_connection->event_thresh = 16; |
| 787 | sps_connection->options |= SPS_O_AUTO_ENABLE; |
| 788 | |
| 789 | ret = sps_connect(*pipe, sps_connection); |
| 790 | if (ret < 0) { |
| 791 | log_event_err("%s: sps_connect failed %d\n", __func__, ret); |
| 792 | goto error; |
| 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | |
| 797 | error: |
| 798 | sps_disconnect(*pipe); |
| 799 | free_sps_endpoints: |
| 800 | sps_free_endpoint(*pipe); |
| 801 | disconnect_ipa: |
| 802 | ipa_disconnect(clnt_hdl); |
| 803 | return ret; |
| 804 | } |
| 805 | |
| 806 | static int disconnect_pipe(enum usb_ctrl cur_bam, u8 idx) |
| 807 | { |
| 808 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 809 | struct sps_pipe *pipe = ctx->usb_bam_sps.sps_pipes[idx]; |
| 810 | struct sps_connect *sps_connection = |
| 811 | &ctx->usb_bam_sps.sps_connections[idx]; |
| 812 | |
| 813 | sps_disconnect(pipe); |
| 814 | sps_free_endpoint(pipe); |
| 815 | ctx->usb_bam_sps.sps_pipes[idx] = NULL; |
| 816 | sps_connection->options &= ~SPS_O_AUTO_ENABLE; |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static bool _hsic_host_bam_resume_core(void) |
| 822 | { |
| 823 | log_event_dbg("%s: enter\n", __func__); |
| 824 | |
| 825 | /* Exit from "full suspend" in case of hsic host */ |
| 826 | if (host_info[HSIC_CTRL].dev && info[HSIC_CTRL].in_lpm) { |
| 827 | log_event_dbg("%s: Getting hsic device %p\n", __func__, |
| 828 | host_info[HSIC_CTRL].dev); |
| 829 | pm_runtime_get(host_info[HSIC_CTRL].dev); |
| 830 | info[HSIC_CTRL].in_lpm = false; |
| 831 | return true; |
| 832 | } |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | static void _hsic_host_bam_suspend_core(void) |
| 837 | { |
| 838 | log_event_dbg("%s: enter\n", __func__); |
| 839 | |
| 840 | if (host_info[HSIC_CTRL].dev && !info[HSIC_CTRL].in_lpm) { |
| 841 | log_event_dbg("%s: Putting hsic host device %p\n", __func__, |
| 842 | host_info[HSIC_CTRL].dev); |
| 843 | pm_runtime_put(host_info[HSIC_CTRL].dev); |
| 844 | info[HSIC_CTRL].in_lpm = true; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | static void usb_bam_suspend_core(enum usb_ctrl bam_type, |
| 849 | enum usb_bam_mode bam_mode, |
| 850 | bool disconnect) |
| 851 | { |
| 852 | log_event_dbg("%s: enter bam=%s\n", __func__, |
| 853 | bam_enable_strings[bam_type]); |
| 854 | |
| 855 | if ((bam_mode == USB_BAM_DEVICE) || (bam_type != HSIC_CTRL)) { |
| 856 | log_event_err("%s: Invalid BAM type %d\n", __func__, bam_type); |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | _hsic_host_bam_suspend_core(); |
| 861 | } |
| 862 | |
| 863 | static bool usb_bam_resume_core(enum usb_ctrl bam_type, |
| 864 | enum usb_bam_mode bam_mode) |
| 865 | { |
| 866 | log_event_dbg("%s: enter bam=%s\n", __func__, |
| 867 | bam_enable_strings[bam_type]); |
| 868 | |
| 869 | if ((bam_mode == USB_BAM_DEVICE) || (bam_type != HSIC_CTRL)) { |
| 870 | log_event_err("%s: Invalid BAM type %d\n", __func__, bam_type); |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | return _hsic_host_bam_resume_core(); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * usb_bam_disconnect_ipa_prod() - disconnects USB consumer(i.e. IPA producer) |
| 879 | * @ipa_params: USB IPA related parameters |
| 880 | * @cur_bam: USB controller used for BAM functionality |
| 881 | * |
| 882 | * It performs disconnect with IPA driver for IPA producer pipe and |
| 883 | * with SPS driver for USB BAM consumer pipe. This API also takes care |
| 884 | * of SYS2BAM and BAM2BAM IPA disconnect functionality. |
| 885 | * |
| 886 | * Return: 0 in case of success, errno otherwise. |
| 887 | */ |
| 888 | static int usb_bam_disconnect_ipa_prod( |
| 889 | struct usb_bam_connect_ipa_params *ipa_params, |
| 890 | enum usb_ctrl cur_bam) |
| 891 | { |
| 892 | int ret; |
| 893 | u8 idx = 0; |
| 894 | struct usb_bam_pipe_connect *pipe_connect; |
| 895 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 896 | |
| 897 | idx = ipa_params->dst_idx; |
| 898 | pipe_connect = &ctx->usb_bam_connections[idx]; |
| 899 | pipe_connect->activity_notify = NULL; |
| 900 | pipe_connect->inactivity_notify = NULL; |
| 901 | pipe_connect->priv = NULL; |
| 902 | |
| 903 | /* close IPA -> USB pipe */ |
| 904 | if (pipe_connect->pipe_type == USB_BAM_PIPE_BAM2BAM) { |
| 905 | ret = ipa_disconnect(ipa_params->prod_clnt_hdl); |
| 906 | if (ret) { |
| 907 | log_event_err("%s: dst pipe disconnection failure\n", |
| 908 | __func__); |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | ret = usb_bam_disconnect_pipe(cur_bam, idx); |
| 913 | if (ret) { |
| 914 | log_event_err("%s: failure to disconnect pipe %d\n", |
| 915 | __func__, idx); |
| 916 | return ret; |
| 917 | } |
| 918 | } else { |
| 919 | ret = ipa_teardown_sys_pipe(ipa_params->prod_clnt_hdl); |
| 920 | if (ret) { |
| 921 | log_event_err("%s: dst pipe disconnection failure\n", |
| 922 | __func__); |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | pipe_connect->enabled = false; |
| 927 | spin_lock(&ctx->usb_bam_lock); |
| 928 | if (ctx->pipes_enabled_per_bam == 0) |
| 929 | log_event_err("%s: wrong pipes enabled counter for bam=%d\n", |
| 930 | __func__, pipe_connect->bam_type); |
| 931 | else |
| 932 | ctx->pipes_enabled_per_bam -= 1; |
| 933 | spin_unlock(&ctx->usb_bam_lock); |
| 934 | } |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * usb_bam_disconnect_ipa_cons() - disconnects USB producer(i.e. IPA consumer) |
| 941 | * @ipa_params: USB IPA related parameters |
| 942 | * @cur_bam: USB controller used for BAM functionality |
| 943 | * |
| 944 | * It performs disconnect with IPA driver for IPA consumer pipe and |
| 945 | * with SPS driver for USB BAM producer pipe. This API also takes care |
| 946 | * of SYS2BAM and BAM2BAM IPA disconnect functionality. |
| 947 | * |
| 948 | * Return: 0 in case of success, errno otherwise. |
| 949 | */ |
| 950 | static int usb_bam_disconnect_ipa_cons( |
| 951 | struct usb_bam_connect_ipa_params *ipa_params, |
| 952 | enum usb_ctrl cur_bam) |
| 953 | { |
| 954 | int ret; |
| 955 | u8 idx = 0; |
| 956 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 957 | struct usb_bam_pipe_connect *pipe_connect; |
| 958 | struct sps_pipe *pipe; |
| 959 | u32 timeout = 10, pipe_empty; |
| 960 | struct usb_bam_sps_type usb_bam_sps = ctx->usb_bam_sps; |
| 961 | struct sps_connect *sps_connection; |
| 962 | bool inject_zlt = true; |
| 963 | |
| 964 | idx = ipa_params->src_idx; |
| 965 | pipe = ctx->usb_bam_sps.sps_pipes[idx]; |
| 966 | pipe_connect = &ctx->usb_bam_connections[idx]; |
| 967 | sps_connection = &usb_bam_sps.sps_connections[idx]; |
| 968 | |
| 969 | pipe_connect->activity_notify = NULL; |
| 970 | pipe_connect->inactivity_notify = NULL; |
| 971 | pipe_connect->priv = NULL; |
| 972 | |
| 973 | /* |
| 974 | * On some platforms, there is a chance that flow control |
| 975 | * is disabled from IPA side, due to this IPA core may not |
| 976 | * consume data from USB. Hence notify IPA to enable flow |
| 977 | * control and then check sps pipe is empty or not before |
| 978 | * processing USB->IPA pipes disconnect. |
| 979 | */ |
| 980 | ipa_clear_endpoint_delay(ipa_params->cons_clnt_hdl); |
| 981 | retry: |
| 982 | /* Make sure pipe is empty before disconnecting it */ |
| 983 | while (1) { |
| 984 | ret = sps_is_pipe_empty(pipe, &pipe_empty); |
| 985 | if (ret) { |
| 986 | log_event_err("%s: sps_is_pipe_empty failed with %d\n", |
| 987 | __func__, ret); |
| 988 | return ret; |
| 989 | } |
| 990 | if (pipe_empty || !--timeout) |
| 991 | break; |
| 992 | |
| 993 | /* Check again */ |
| 994 | usleep_range(1000, 2000); |
| 995 | } |
| 996 | |
| 997 | if (!pipe_empty) { |
| 998 | if (inject_zlt) { |
| 999 | pr_debug("%s: Inject ZLT\n", __func__); |
| 1000 | log_event_dbg("%s: Inject ZLT\n", __func__); |
| 1001 | inject_zlt = false; |
| 1002 | sps_pipe_inject_zlt(sps_connection->destination, |
| 1003 | sps_connection->dest_pipe_index); |
| 1004 | timeout = 10; |
| 1005 | goto retry; |
| 1006 | } |
| 1007 | log_event_err("%s: src pipe(USB) not empty, wait timed out!\n", |
| 1008 | __func__); |
| 1009 | sps_get_bam_debug_info(ctx->h_bam, 93, |
| 1010 | (SPS_BAM_PIPE(0) | SPS_BAM_PIPE(1)), 0, 2); |
| 1011 | ipa_bam_reg_dump(); |
| 1012 | panic("%s:SPS pipe not empty for USB->IPA\n", __func__); |
| 1013 | } |
| 1014 | |
| 1015 | /* Do the release handshake with the IPA via RM */ |
| 1016 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1017 | info[cur_bam].connect_complete = 0; |
| 1018 | info[cur_bam].disconnected = 1; |
| 1019 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1020 | |
| 1021 | /* Start release handshake on the last USB BAM producer pipe */ |
| 1022 | if (info[cur_bam].prod_pipes_enabled_per_bam == 1) |
| 1023 | wait_for_prod_release(cur_bam); |
| 1024 | |
| 1025 | /* close USB -> IPA pipe */ |
| 1026 | if (pipe_connect->pipe_type == USB_BAM_PIPE_BAM2BAM) { |
| 1027 | ret = ipa_disconnect(ipa_params->cons_clnt_hdl); |
| 1028 | if (ret) { |
| 1029 | log_event_err("%s: src pipe disconnection failure\n", |
| 1030 | __func__); |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
| 1034 | ret = usb_bam_disconnect_pipe(cur_bam, idx); |
| 1035 | if (ret) { |
| 1036 | log_event_err("%s: failure to disconnect pipe %d\n", |
| 1037 | __func__, idx); |
| 1038 | return ret; |
| 1039 | } |
| 1040 | } else { |
| 1041 | ret = ipa_teardown_sys_pipe(ipa_params->cons_clnt_hdl); |
| 1042 | if (ret) { |
| 1043 | log_event_err("%s: src pipe disconnection failure\n", |
| 1044 | __func__); |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
| 1048 | pipe_connect->enabled = false; |
| 1049 | spin_lock(&ctx->usb_bam_lock); |
| 1050 | if (ctx->pipes_enabled_per_bam == 0) |
| 1051 | log_event_err("%s: wrong pipes enabled counter for bam=%d\n", |
| 1052 | __func__, pipe_connect->bam_type); |
| 1053 | else |
| 1054 | ctx->pipes_enabled_per_bam -= 1; |
| 1055 | spin_unlock(&ctx->usb_bam_lock); |
| 1056 | } |
| 1057 | |
| 1058 | pipe_connect->ipa_clnt_hdl = -1; |
| 1059 | info[cur_bam].prod_pipes_enabled_per_bam -= 1; |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | int usb_bam_connect(enum usb_ctrl cur_bam, int idx, u32 *bam_pipe_idx) |
| 1065 | { |
| 1066 | int ret; |
| 1067 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1068 | struct usb_bam_pipe_connect *pipe_connect = |
| 1069 | &ctx->usb_bam_connections[idx]; |
| 1070 | struct device *bam_dev = &ctx->usb_bam_pdev->dev; |
| 1071 | struct msm_usb_bam_platform_data *pdata = bam_dev->platform_data; |
| 1072 | enum usb_bam_mode cur_mode; |
| 1073 | |
| 1074 | if (pipe_connect->enabled) { |
| 1075 | pr_warn("%s: connection %d was already established\n", |
| 1076 | __func__, idx); |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | if (!bam_pipe_idx) { |
| 1081 | log_event_err("%s: invalid bam_pipe_idx\n", __func__); |
| 1082 | return -EINVAL; |
| 1083 | } |
| 1084 | if (idx < 0 || idx > ctx->max_connections) { |
| 1085 | log_event_err("idx is wrong %d\n", idx); |
| 1086 | return -EINVAL; |
| 1087 | } |
| 1088 | |
| 1089 | cur_mode = pipe_connect->bam_mode; |
| 1090 | |
| 1091 | log_event_dbg("%s: PM Runtime GET %d, count: %d\n", |
| 1092 | __func__, idx, get_pm_runtime_counter(bam_dev)); |
| 1093 | pm_runtime_get_sync(bam_dev); |
| 1094 | |
| 1095 | spin_lock(&ctx->usb_bam_lock); |
| 1096 | /* Check if BAM requires RESET before connect and reset of first pipe */ |
| 1097 | if ((pdata->reset_on_connect == true) && |
| 1098 | (ctx->pipes_enabled_per_bam == 0)) { |
| 1099 | spin_unlock(&ctx->usb_bam_lock); |
| 1100 | |
| 1101 | if (cur_bam == CI_CTRL) |
| 1102 | msm_hw_bam_disable(1); |
| 1103 | |
| 1104 | sps_device_reset(ctx->h_bam); |
| 1105 | |
| 1106 | if (cur_bam == CI_CTRL) |
| 1107 | msm_hw_bam_disable(0); |
| 1108 | |
| 1109 | spin_lock(&ctx->usb_bam_lock); |
| 1110 | } |
| 1111 | spin_unlock(&ctx->usb_bam_lock); |
| 1112 | |
| 1113 | /* Set the BAM mode (host/device) according to connected pipe */ |
| 1114 | info[cur_bam].cur_bam_mode = pipe_connect->bam_mode; |
| 1115 | |
| 1116 | ret = connect_pipe(cur_bam, idx, bam_pipe_idx); |
| 1117 | if (ret) { |
| 1118 | log_event_err("%s: pipe connection[%d] failure\n", |
| 1119 | __func__, idx); |
| 1120 | log_event_dbg("%s: err, PM RT PUT %d, count: %d\n", |
| 1121 | __func__, idx, get_pm_runtime_counter(bam_dev)); |
| 1122 | pm_runtime_put_sync(bam_dev); |
| 1123 | return ret; |
| 1124 | } |
| 1125 | log_event_dbg("%s: pipe connection[%d] success\n", __func__, idx); |
| 1126 | pipe_connect->enabled = 1; |
| 1127 | spin_lock(&ctx->usb_bam_lock); |
| 1128 | ctx->pipes_enabled_per_bam += 1; |
| 1129 | spin_unlock(&ctx->usb_bam_lock); |
| 1130 | |
| 1131 | return 0; |
| 1132 | } |
| 1133 | |
| 1134 | static int __sps_reset_pipe(enum usb_ctrl bam_type, |
| 1135 | struct sps_pipe *pipe, u32 idx) |
| 1136 | { |
| 1137 | int ret; |
| 1138 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 1139 | struct sps_connect *sps_connection = |
| 1140 | &ctx->usb_bam_sps.sps_connections[idx]; |
| 1141 | |
| 1142 | ret = sps_disconnect(pipe); |
| 1143 | if (ret) { |
| 1144 | log_event_err("%s: sps_disconnect() failed %d\n", |
| 1145 | __func__, ret); |
| 1146 | return ret; |
| 1147 | } |
| 1148 | |
| 1149 | ret = sps_connect(pipe, sps_connection); |
| 1150 | if (ret < 0) { |
| 1151 | log_event_err("%s: sps_connect() failed %d\n", __func__, ret); |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | static void reset_pipe_for_resume(struct usb_bam_pipe_connect *pipe_connect) |
| 1159 | { |
| 1160 | int ret; |
| 1161 | enum usb_ctrl bam_type = pipe_connect->bam_type; |
| 1162 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 1163 | u32 idx = ARRAY_INDEX_FROM_ADDR(ctx->usb_bam_connections, pipe_connect); |
| 1164 | struct sps_pipe *pipe = ctx->usb_bam_sps.sps_pipes[idx]; |
| 1165 | |
| 1166 | if (!pipe_connect->reset_pipe_after_lpm || |
| 1167 | pipe_connect->pipe_type != USB_BAM_PIPE_BAM2BAM) { |
| 1168 | log_event_dbg("No need to reset pipe %d\n", idx); |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | ret = __sps_reset_pipe(bam_type, pipe, idx); |
| 1173 | if (ret) { |
| 1174 | log_event_err("%s failed to reset the USB sps pipe\n", |
| 1175 | __func__); |
| 1176 | return; |
| 1177 | } |
| 1178 | |
| 1179 | ret = ipa_reset_endpoint(pipe_connect->ipa_clnt_hdl); |
| 1180 | if (ret) { |
| 1181 | log_event_err("%s failed to reset the IPA pipe\n", __func__); |
| 1182 | return; |
| 1183 | } |
| 1184 | log_event_dbg("%s: USB/IPA pipes reset after resume\n", __func__); |
| 1185 | } |
| 1186 | |
| 1187 | /* Stop PROD transfers in case they were started */ |
| 1188 | static void stop_prod_transfers(struct usb_bam_pipe_connect *pipe_connect) |
| 1189 | { |
| 1190 | if (pipe_connect->stop && !pipe_connect->prod_stopped) { |
| 1191 | log_event_dbg("%s: Stop PROD transfers on\n", __func__); |
| 1192 | pipe_connect->stop(pipe_connect->start_stop_param, |
| 1193 | USB_TO_PEER_PERIPHERAL); |
| 1194 | pipe_connect->prod_stopped = true; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | static void start_prod_transfers(struct usb_bam_pipe_connect *pipe_connect) |
| 1199 | { |
| 1200 | log_event_err("%s: Starting PROD\n", __func__); |
| 1201 | if (pipe_connect->start && pipe_connect->prod_stopped) { |
| 1202 | log_event_dbg("%s: Enqueue PROD transfer\n", __func__); |
| 1203 | pipe_connect->start(pipe_connect->start_stop_param, |
| 1204 | USB_TO_PEER_PERIPHERAL); |
| 1205 | pipe_connect->prod_stopped = false; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | static void start_cons_transfers(struct usb_bam_pipe_connect *pipe_connect) |
| 1210 | { |
| 1211 | /* Start CONS transfer */ |
| 1212 | if (pipe_connect->start && pipe_connect->cons_stopped) { |
| 1213 | log_event_dbg("%s: Enqueue CONS transfer\n", __func__); |
| 1214 | pipe_connect->start(pipe_connect->start_stop_param, |
| 1215 | PEER_PERIPHERAL_TO_USB); |
| 1216 | pipe_connect->cons_stopped = 0; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /* Stop CONS transfers in case they were started */ |
| 1221 | static void stop_cons_transfers(struct usb_bam_pipe_connect *pipe_connect) |
| 1222 | { |
| 1223 | if (pipe_connect->stop && !pipe_connect->cons_stopped) { |
| 1224 | log_event_dbg("%s: Stop CONS transfers\n", __func__); |
| 1225 | pipe_connect->stop(pipe_connect->start_stop_param, |
| 1226 | PEER_PERIPHERAL_TO_USB); |
| 1227 | pipe_connect->cons_stopped = 1; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | static void resume_suspended_pipes(enum usb_ctrl cur_bam) |
| 1232 | { |
| 1233 | u32 idx, dst_idx; |
| 1234 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1235 | struct usb_bam_pipe_connect *pipe_connect; |
| 1236 | |
| 1237 | log_event_dbg("Resuming: suspend pipes =%d\n", |
| 1238 | info[cur_bam].pipes_suspended); |
| 1239 | |
| 1240 | while (info[cur_bam].pipes_suspended >= 1) { |
| 1241 | idx = info[cur_bam].pipes_suspended - 1; |
| 1242 | dst_idx = info[cur_bam].resume_dst_idx[idx]; |
| 1243 | pipe_connect = &ctx->usb_bam_connections[dst_idx]; |
| 1244 | if (pipe_connect->cons_stopped) { |
| 1245 | log_event_dbg("%s: Starting CONS on %d\n", __func__, |
| 1246 | dst_idx); |
| 1247 | start_cons_transfers(pipe_connect); |
| 1248 | } |
| 1249 | |
| 1250 | log_event_dbg("%s: Starting PROD on %d\n", __func__, dst_idx); |
| 1251 | start_prod_transfers(pipe_connect); |
| 1252 | info[cur_bam].pipes_suspended--; |
| 1253 | info[cur_bam].pipes_resumed++; |
| 1254 | /* Suspend was aborted, renew pm_runtime vote */ |
| 1255 | log_event_dbg("%s: PM Runtime GET %d, count: %d\n", __func__, |
| 1256 | idx, get_pm_runtime_counter(&ctx->usb_bam_pdev->dev)); |
| 1257 | pm_runtime_get(&ctx->usb_bam_pdev->dev); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | static inline int all_pipes_suspended(enum usb_ctrl cur_bam) |
| 1262 | { |
| 1263 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1264 | |
| 1265 | log_event_dbg("%s: pipes_suspended=%d pipes_enabled_per_bam=%d\n", |
| 1266 | __func__, info[cur_bam].pipes_suspended, |
| 1267 | ctx->pipes_enabled_per_bam); |
| 1268 | |
| 1269 | return info[cur_bam].pipes_suspended == ctx->pipes_enabled_per_bam; |
| 1270 | } |
| 1271 | |
| 1272 | static void usb_bam_finish_suspend(enum usb_ctrl cur_bam) |
| 1273 | { |
| 1274 | int ret, bam2bam; |
| 1275 | u32 cons_empty, idx, dst_idx; |
| 1276 | struct sps_pipe *cons_pipe; |
| 1277 | struct usb_bam_pipe_connect *pipe_connect; |
| 1278 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1279 | struct device *bam_dev = &ctx->usb_bam_pdev->dev; |
| 1280 | |
| 1281 | mutex_lock(&info[cur_bam].suspend_resume_mutex); |
| 1282 | |
| 1283 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1284 | /* If cable was disconnected, let disconnection seq do everything */ |
| 1285 | if (info[cur_bam].disconnected || all_pipes_suspended(cur_bam)) { |
| 1286 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1287 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1288 | log_event_dbg("%s: Cable disconnected\n", __func__); |
| 1289 | return; |
| 1290 | } |
| 1291 | log_event_dbg("%s: bam:%s RT GET: %d\n", __func__, |
| 1292 | bam_enable_strings[cur_bam], get_pm_runtime_counter(bam_dev)); |
| 1293 | pm_runtime_get(bam_dev); |
| 1294 | |
| 1295 | /* If resume was called don't finish this work */ |
| 1296 | if (!info[cur_bam].bus_suspend) { |
| 1297 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1298 | log_event_dbg("%s: Bus resume in progress\n", __func__); |
| 1299 | goto no_lpm; |
| 1300 | } |
| 1301 | |
| 1302 | /* Go over all pipes, stop and suspend them, and go to lpm */ |
| 1303 | while (!all_pipes_suspended(cur_bam)) { |
| 1304 | idx = info[cur_bam].pipes_suspended; |
| 1305 | dst_idx = info[cur_bam].suspend_dst_idx[idx]; |
| 1306 | cons_pipe = ctx->usb_bam_sps.sps_pipes[dst_idx]; |
| 1307 | pipe_connect = &ctx->usb_bam_connections[dst_idx]; |
| 1308 | |
| 1309 | log_event_dbg("pipes_suspended=%d pipes_to_suspend=%d\n", |
| 1310 | info[cur_bam].pipes_suspended, |
| 1311 | info[cur_bam].pipes_to_suspend); |
| 1312 | |
| 1313 | bam2bam = (pipe_connect->pipe_type == USB_BAM_PIPE_BAM2BAM); |
| 1314 | |
| 1315 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1316 | |
| 1317 | if (bam2bam) { |
| 1318 | ret = sps_is_pipe_empty(cons_pipe, &cons_empty); |
| 1319 | if (ret) { |
| 1320 | log_event_err("%s: sps_is_pipe_empty failed with %d\n", |
| 1321 | __func__, ret); |
| 1322 | goto no_lpm; |
| 1323 | } |
| 1324 | } else { |
| 1325 | log_event_err("%s: pipe type is not B2B\n", __func__); |
| 1326 | cons_empty = true; |
| 1327 | } |
| 1328 | |
| 1329 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1330 | /* Stop CONS transfers and go to lpm if no more data in the */ |
| 1331 | /* pipes */ |
| 1332 | if (cons_empty) { |
| 1333 | log_event_dbg("%s: Stopping CONS transfers on dst_idx=%d\n" |
| 1334 | , __func__, dst_idx); |
| 1335 | stop_cons_transfers(pipe_connect); |
| 1336 | |
| 1337 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1338 | log_event_dbg("%s: Suspending pipe\n", __func__); |
| 1339 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1340 | info[cur_bam].resume_src_idx[idx] = |
| 1341 | info[cur_bam].suspend_src_idx[idx]; |
| 1342 | info[cur_bam].resume_dst_idx[idx] = |
| 1343 | info[cur_bam].suspend_dst_idx[idx]; |
| 1344 | info[cur_bam].pipes_suspended++; |
| 1345 | |
| 1346 | log_event_dbg("%s: PM Runtime PUT %d, count: %d\n", |
| 1347 | __func__, idx, get_pm_runtime_counter(bam_dev)); |
| 1348 | pm_runtime_put(&ctx->usb_bam_pdev->dev); |
| 1349 | } else { |
| 1350 | log_event_dbg("%s: Pipe is not empty, not going to LPM\n", |
| 1351 | __func__); |
| 1352 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1353 | goto no_lpm; |
| 1354 | } |
| 1355 | } |
| 1356 | info[cur_bam].pipes_to_suspend = 0; |
| 1357 | info[cur_bam].pipes_resumed = 0; |
| 1358 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1359 | |
| 1360 | /* ACK on the last pipe */ |
| 1361 | if (info[cur_bam].pipes_suspended == ctx->pipes_enabled_per_bam && |
| 1362 | info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_RELEASED) { |
| 1363 | ipa_rm_notify_completion( |
| 1364 | IPA_RM_RESOURCE_RELEASED, |
| 1365 | ipa_rm_resource_cons[cur_bam]); |
| 1366 | } |
| 1367 | |
| 1368 | log_event_dbg("%s: Starting LPM on Bus Suspend, RT PUT:%d\n", __func__, |
| 1369 | get_pm_runtime_counter(bam_dev)); |
| 1370 | /* Put to match _get at the beginning of this routine */ |
| 1371 | pm_runtime_put_sync(bam_dev); |
| 1372 | |
| 1373 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1374 | |
| 1375 | return; |
| 1376 | |
| 1377 | no_lpm: |
| 1378 | |
| 1379 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1380 | resume_suspended_pipes(cur_bam); |
| 1381 | info[cur_bam].pipes_resumed = 0; |
| 1382 | info[cur_bam].pipes_to_suspend = 0; |
| 1383 | info[cur_bam].pipes_suspended = 0; |
| 1384 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1385 | /* |
| 1386 | * Finish the handshake. Resume Sequence will start automatically |
| 1387 | * by the data in the pipes. |
| 1388 | */ |
| 1389 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_RELEASED) |
| 1390 | ipa_rm_notify_completion(IPA_RM_RESOURCE_RELEASED, |
| 1391 | ipa_rm_resource_cons[cur_bam]); |
| 1392 | |
| 1393 | /* Put to match _get at the beginning of this routine */ |
| 1394 | pm_runtime_put(bam_dev); |
| 1395 | |
| 1396 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1397 | } |
| 1398 | |
| 1399 | void usb_bam_finish_suspend_(struct work_struct *w) |
| 1400 | { |
| 1401 | enum usb_ctrl cur_bam; |
| 1402 | struct usb_bam_ipa_handshake_info *info_ptr; |
| 1403 | |
| 1404 | info_ptr = container_of(w, struct usb_bam_ipa_handshake_info, |
| 1405 | finish_suspend_work); |
| 1406 | cur_bam = info_ptr->cur_bam_mode; |
| 1407 | |
| 1408 | log_event_dbg("%s: Finishing suspend sequence(BAM=%s)\n", __func__, |
| 1409 | bam_enable_strings[cur_bam]); |
| 1410 | usb_bam_finish_suspend(cur_bam); |
| 1411 | } |
| 1412 | |
| 1413 | static void usb_prod_notify_cb(void *user_data, enum ipa_rm_event event, |
| 1414 | unsigned long data) |
| 1415 | { |
| 1416 | enum usb_ctrl *cur_bam = (void *)user_data; |
| 1417 | |
| 1418 | switch (event) { |
| 1419 | case IPA_RM_RESOURCE_GRANTED: |
| 1420 | log_event_dbg("%s: %s_PROD resource granted\n", |
| 1421 | __func__, bam_enable_strings[*cur_bam]); |
| 1422 | info[*cur_bam].cur_prod_state = IPA_RM_RESOURCE_GRANTED; |
| 1423 | complete_all(&info[*cur_bam].prod_avail); |
| 1424 | break; |
| 1425 | case IPA_RM_RESOURCE_RELEASED: |
| 1426 | log_event_dbg("%s: %s_PROD resource released\n", |
| 1427 | __func__, bam_enable_strings[*cur_bam]); |
| 1428 | info[*cur_bam].cur_prod_state = IPA_RM_RESOURCE_RELEASED; |
| 1429 | complete_all(&info[*cur_bam].prod_released); |
| 1430 | break; |
| 1431 | default: |
| 1432 | break; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * usb_bam_resume_host: vote for hsic host core resume. |
| 1438 | * |
| 1439 | * NOTE: This function should be called in a context that hold |
| 1440 | * usb_bam_lock. |
| 1441 | */ |
| 1442 | static void usb_bam_resume_host(enum usb_ctrl bam_type) |
| 1443 | { |
| 1444 | int i; |
| 1445 | struct usb_bam_pipe_connect *pipe_iter; |
| 1446 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 1447 | |
| 1448 | log_event_dbg("%s: enter bam=%s\n", __func__, |
| 1449 | bam_enable_strings[bam_type]); |
| 1450 | |
| 1451 | if (usb_bam_resume_core(bam_type, USB_BAM_HOST)) { |
| 1452 | for (i = 0; i < ctx->max_connections; i++) { |
| 1453 | pipe_iter = &ctx->usb_bam_connections[i]; |
| 1454 | if (pipe_iter->enabled && pipe_iter->suspended) |
| 1455 | pipe_iter->suspended = false; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | static int cons_request_resource(enum usb_ctrl cur_bam) |
| 1461 | { |
| 1462 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1463 | int ret = -EINPROGRESS; |
| 1464 | |
| 1465 | log_event_dbg("%s: Request %s_CONS resource\n", |
| 1466 | __func__, bam_enable_strings[cur_bam]); |
| 1467 | |
| 1468 | spin_lock(&ctx->usb_bam_lock); |
| 1469 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1470 | info[cur_bam].cur_cons_state = IPA_RM_RESOURCE_GRANTED; |
| 1471 | |
| 1472 | switch (info[cur_bam].cur_bam_mode) { |
| 1473 | case USB_BAM_DEVICE: |
| 1474 | if (ctx->pipes_enabled_per_bam && |
| 1475 | info[cur_bam].connect_complete) { |
| 1476 | if (!all_pipes_suspended(cur_bam) && |
| 1477 | !info[cur_bam].bus_suspend) { |
| 1478 | log_event_dbg("%s: ACK on cons_request\n", |
| 1479 | __func__); |
| 1480 | ret = 0; |
| 1481 | } else if (info[cur_bam].bus_suspend) { |
| 1482 | info[cur_bam].bus_suspend = 0; |
| 1483 | log_event_dbg("%s: Wake up host\n", __func__); |
| 1484 | if (info[cur_bam].wake_cb) |
| 1485 | info[cur_bam].wake_cb( |
| 1486 | info[cur_bam].wake_param); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | break; |
| 1491 | case USB_BAM_HOST: |
| 1492 | /* |
| 1493 | * Vote for hsic resume, however the core |
| 1494 | * resume may not be completed yet or on the other hand |
| 1495 | * hsic core might already be resumed, due to a vote |
| 1496 | * by other driver, in this case we will just renew our |
| 1497 | * vote here. |
| 1498 | */ |
| 1499 | usb_bam_resume_host(cur_bam); |
| 1500 | |
| 1501 | /* |
| 1502 | * Return success if there are pipes connected |
| 1503 | * and hsic core is actually not in lpm. |
| 1504 | * If in lpm, grant will occur on resume |
| 1505 | * finish (see msm_bam_hsic_notify_on_resume) |
| 1506 | */ |
| 1507 | if (ctx->pipes_enabled_per_bam && !host_info[cur_bam].in_lpm) |
| 1508 | ret = 0; |
| 1509 | |
| 1510 | break; |
| 1511 | |
| 1512 | default: |
| 1513 | break; |
| 1514 | } |
| 1515 | |
| 1516 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1517 | spin_unlock(&ctx->usb_bam_lock); |
| 1518 | |
| 1519 | if (ret == -EINPROGRESS) |
| 1520 | log_event_dbg("%s: EINPROGRESS on cons_request\n", __func__); |
| 1521 | |
| 1522 | return ret; |
| 1523 | } |
| 1524 | |
| 1525 | static int ss_usb_cons_request_resource(void) |
| 1526 | { |
| 1527 | return cons_request_resource(DWC3_CTRL); |
| 1528 | } |
| 1529 | |
| 1530 | |
| 1531 | static int usb_cons_request_resource(void) |
| 1532 | { |
| 1533 | return cons_request_resource(CI_CTRL); |
| 1534 | } |
| 1535 | |
| 1536 | static int hsic_cons_request_resource(void) |
| 1537 | { |
| 1538 | return cons_request_resource(HSIC_CTRL); |
| 1539 | } |
| 1540 | |
| 1541 | static int cons_release_resource(enum usb_ctrl cur_bam) |
| 1542 | { |
| 1543 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1544 | |
| 1545 | log_event_dbg("%s: Release %s_CONS resource\n", |
| 1546 | __func__, bam_enable_strings[cur_bam]); |
| 1547 | |
| 1548 | info[cur_bam].cur_cons_state = IPA_RM_RESOURCE_RELEASED; |
| 1549 | |
| 1550 | spin_lock(&ctx->usb_bam_lock); |
| 1551 | if (!ctx->pipes_enabled_per_bam) { |
| 1552 | spin_unlock(&ctx->usb_bam_lock); |
| 1553 | log_event_dbg("%s: ACK on cons_release\n", __func__); |
| 1554 | return 0; |
| 1555 | } |
| 1556 | spin_unlock(&ctx->usb_bam_lock); |
| 1557 | |
| 1558 | if (info[cur_bam].cur_bam_mode == USB_BAM_DEVICE) { |
| 1559 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1560 | if (info[cur_bam].bus_suspend) { |
| 1561 | queue_work(ctx->usb_bam_wq, |
| 1562 | &info[cur_bam].finish_suspend_work); |
| 1563 | } |
| 1564 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1565 | |
| 1566 | log_event_dbg("%s: EINPROGRESS cons_release\n", __func__); |
| 1567 | return -EINPROGRESS; |
| 1568 | } else if (info[cur_bam].cur_bam_mode == USB_BAM_HOST) { |
| 1569 | /* |
| 1570 | * Allow to go to lpm for now. Actual state will be checked |
| 1571 | * in msm_bam_hsic_lpm_ok() / msm_bam_lpm_ok() just before |
| 1572 | * going to lpm. |
| 1573 | */ |
| 1574 | usb_bam_suspend_core(cur_bam, info[cur_bam].cur_bam_mode, 1); |
| 1575 | } |
| 1576 | |
| 1577 | return 0; |
| 1578 | } |
| 1579 | |
| 1580 | static int hsic_cons_release_resource(void) |
| 1581 | { |
| 1582 | return cons_release_resource(HSIC_CTRL); |
| 1583 | } |
| 1584 | |
| 1585 | static int usb_cons_release_resource(void) |
| 1586 | { |
| 1587 | return cons_release_resource(CI_CTRL); |
| 1588 | } |
| 1589 | |
| 1590 | static int ss_usb_cons_release_resource(void) |
| 1591 | { |
| 1592 | return cons_release_resource(DWC3_CTRL); |
| 1593 | } |
| 1594 | |
| 1595 | static void usb_bam_ipa_create_resources(enum usb_ctrl cur_bam) |
| 1596 | { |
| 1597 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1598 | struct msm_usb_bam_platform_data *pdata = |
| 1599 | ctx->usb_bam_pdev->dev.platform_data; |
| 1600 | struct ipa_rm_create_params usb_prod_create_params; |
| 1601 | struct ipa_rm_create_params usb_cons_create_params; |
| 1602 | int ret; |
| 1603 | |
| 1604 | /* Create USB/HSIC_PROD entity */ |
| 1605 | memset(&usb_prod_create_params, 0, sizeof(usb_prod_create_params)); |
| 1606 | usb_prod_create_params.name = ipa_rm_resource_prod[cur_bam]; |
| 1607 | usb_prod_create_params.reg_params.notify_cb = usb_prod_notify_cb; |
| 1608 | usb_prod_create_params.reg_params.user_data = &pdata->bam_type; |
| 1609 | usb_prod_create_params.floor_voltage = IPA_VOLTAGE_SVS; |
| 1610 | ret = ipa_rm_create_resource(&usb_prod_create_params); |
| 1611 | if (ret) { |
| 1612 | log_event_err("%s: Failed to create USB_PROD resource\n", |
| 1613 | __func__); |
| 1614 | return; |
| 1615 | } |
| 1616 | |
| 1617 | /* Create USB_CONS entity */ |
| 1618 | memset(&usb_cons_create_params, 0, sizeof(usb_cons_create_params)); |
| 1619 | usb_cons_create_params.name = ipa_rm_resource_cons[cur_bam]; |
| 1620 | usb_cons_create_params.request_resource = request_resource_cb[cur_bam]; |
| 1621 | usb_cons_create_params.release_resource = release_resource_cb[cur_bam]; |
| 1622 | usb_cons_create_params.floor_voltage = IPA_VOLTAGE_SVS; |
| 1623 | ret = ipa_rm_create_resource(&usb_cons_create_params); |
| 1624 | if (ret) { |
| 1625 | log_event_err("%s: Failed to create USB_CONS resource\n", |
| 1626 | __func__); |
| 1627 | return; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | static void wait_for_prod_granted(enum usb_ctrl cur_bam) |
| 1632 | { |
| 1633 | int ret; |
| 1634 | |
| 1635 | log_event_dbg("%s Request %s_PROD_RES\n", __func__, |
| 1636 | bam_enable_strings[cur_bam]); |
| 1637 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_GRANTED) |
| 1638 | log_event_dbg("%s: CONS already granted for some reason\n", |
| 1639 | __func__); |
| 1640 | if (info[cur_bam].cur_prod_state == IPA_RM_RESOURCE_GRANTED) |
| 1641 | log_event_dbg("%s: PROD already granted for some reason\n", |
| 1642 | __func__); |
| 1643 | |
| 1644 | init_completion(&info[cur_bam].prod_avail); |
| 1645 | |
| 1646 | ret = ipa_rm_request_resource(ipa_rm_resource_prod[cur_bam]); |
| 1647 | if (!ret) { |
| 1648 | info[cur_bam].cur_prod_state = IPA_RM_RESOURCE_GRANTED; |
| 1649 | complete_all(&info[cur_bam].prod_avail); |
| 1650 | log_event_dbg("%s: PROD_GRANTED without wait\n", __func__); |
| 1651 | } else if (ret == -EINPROGRESS) { |
| 1652 | log_event_dbg("%s: Waiting for PROD_GRANTED\n", __func__); |
| 1653 | if (!wait_for_completion_timeout(&info[cur_bam].prod_avail, |
| 1654 | USB_BAM_TIMEOUT)) |
| 1655 | log_event_err("%s: Timeout wainting for PROD_GRANTED\n", |
| 1656 | __func__); |
| 1657 | } else |
| 1658 | log_event_err("%s: ipa_rm_request_resource ret =%d\n", |
| 1659 | __func__, ret); |
| 1660 | } |
| 1661 | |
| 1662 | void notify_usb_connected(enum usb_ctrl cur_bam) |
| 1663 | { |
| 1664 | log_event_dbg("%s: enter\n", __func__); |
| 1665 | |
| 1666 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1667 | if (info[cur_bam].cur_bam_mode == USB_BAM_DEVICE) |
| 1668 | info[cur_bam].connect_complete = 1; |
| 1669 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1670 | |
| 1671 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_GRANTED) { |
| 1672 | log_event_dbg("%s: Notify %s CONS_GRANTED\n", __func__, |
| 1673 | bam_enable_strings[cur_bam]); |
| 1674 | ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED, |
| 1675 | ipa_rm_resource_cons[cur_bam]); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | static void wait_for_prod_release(enum usb_ctrl cur_bam) |
| 1680 | { |
| 1681 | int ret; |
| 1682 | |
| 1683 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_RELEASED) |
| 1684 | log_event_dbg("%s consumer already released\n", __func__); |
| 1685 | if (info[cur_bam].cur_prod_state == IPA_RM_RESOURCE_RELEASED) |
| 1686 | log_event_dbg("%s producer already released\n", __func__); |
| 1687 | |
| 1688 | init_completion(&info[cur_bam].prod_released); |
| 1689 | log_event_dbg("%s: Releasing %s_PROD\n", __func__, |
| 1690 | bam_enable_strings[cur_bam]); |
| 1691 | ret = ipa_rm_release_resource(ipa_rm_resource_prod[cur_bam]); |
| 1692 | if (!ret) { |
| 1693 | log_event_dbg("%s: Released without waiting\n", __func__); |
| 1694 | info[cur_bam].cur_prod_state = IPA_RM_RESOURCE_RELEASED; |
| 1695 | complete_all(&info[cur_bam].prod_released); |
| 1696 | } else if (ret == -EINPROGRESS) { |
| 1697 | log_event_dbg("%s: Waiting for PROD_RELEASED\n", __func__); |
| 1698 | if (!wait_for_completion_timeout(&info[cur_bam].prod_released, |
| 1699 | USB_BAM_TIMEOUT)) |
| 1700 | log_event_err("%s: Timeout waiting for PROD_RELEASED\n", |
| 1701 | __func__); |
| 1702 | } else { |
| 1703 | log_event_err("%s: ipa_rm_request_resource ret =%d\n", |
| 1704 | __func__, ret); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | static bool check_pipes_empty(enum usb_ctrl bam_type, u8 src_idx, u8 dst_idx) |
| 1709 | { |
| 1710 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 1711 | struct sps_pipe *prod_pipe, *cons_pipe; |
| 1712 | struct usb_bam_pipe_connect *prod_pipe_connect, *cons_pipe_connect; |
| 1713 | u32 prod_empty, cons_empty; |
| 1714 | |
| 1715 | prod_pipe_connect = &ctx->usb_bam_connections[src_idx]; |
| 1716 | cons_pipe_connect = &ctx->usb_bam_connections[dst_idx]; |
| 1717 | if (!prod_pipe_connect->enabled || !cons_pipe_connect->enabled) { |
| 1718 | log_event_err("%s: pipes are not enabled dst=%d src=%d\n", |
| 1719 | __func__, prod_pipe_connect->enabled, |
| 1720 | cons_pipe_connect->enabled); |
| 1721 | } |
| 1722 | |
| 1723 | /* If we have any remaints in the pipes we don't go to sleep */ |
| 1724 | prod_pipe = ctx->usb_bam_sps.sps_pipes[src_idx]; |
| 1725 | cons_pipe = ctx->usb_bam_sps.sps_pipes[dst_idx]; |
| 1726 | log_event_dbg("prod_pipe=%p, cons_pipe=%p\n", prod_pipe, cons_pipe); |
| 1727 | |
| 1728 | if (!cons_pipe || (!prod_pipe && |
| 1729 | prod_pipe_connect->pipe_type == USB_BAM_PIPE_BAM2BAM)) { |
| 1730 | log_event_err("Missing a pipe!\n"); |
| 1731 | return false; |
| 1732 | } |
| 1733 | |
| 1734 | if (prod_pipe && sps_is_pipe_empty(prod_pipe, &prod_empty)) { |
| 1735 | log_event_err("sps_is_pipe_empty(prod) failed\n"); |
| 1736 | return false; |
| 1737 | } |
| 1738 | |
| 1739 | prod_empty = true; |
| 1740 | if (sps_is_pipe_empty(cons_pipe, &cons_empty)) { |
| 1741 | log_event_err("sps_is_pipe_empty(cons) failed\n"); |
| 1742 | return false; |
| 1743 | } |
| 1744 | |
| 1745 | if (!prod_empty || !cons_empty) { |
| 1746 | log_event_err("pipes not empty prod=%d cond=%d\n", |
| 1747 | prod_empty, cons_empty); |
| 1748 | return false; |
| 1749 | } |
| 1750 | |
| 1751 | return true; |
| 1752 | |
| 1753 | } |
| 1754 | |
| 1755 | void usb_bam_suspend(enum usb_ctrl cur_bam, |
| 1756 | struct usb_bam_connect_ipa_params *ipa_params) |
| 1757 | { |
| 1758 | struct usb_bam_pipe_connect *pipe_connect; |
| 1759 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1760 | enum usb_bam_mode bam_mode; |
| 1761 | u8 src_idx, dst_idx; |
| 1762 | |
| 1763 | log_event_dbg("%s: enter\n", __func__); |
| 1764 | |
| 1765 | if (!ipa_params) { |
| 1766 | log_event_err("%s: Invalid ipa params\n", __func__); |
| 1767 | return; |
| 1768 | } |
| 1769 | |
| 1770 | src_idx = ipa_params->src_idx; |
| 1771 | dst_idx = ipa_params->dst_idx; |
| 1772 | |
| 1773 | if (src_idx >= ctx->max_connections || |
| 1774 | dst_idx >= ctx->max_connections) { |
| 1775 | log_event_err("%s: Invalid connection index src=%d dst=%d\n", |
| 1776 | __func__, src_idx, dst_idx); |
| 1777 | } |
| 1778 | |
| 1779 | pipe_connect = &ctx->usb_bam_connections[src_idx]; |
| 1780 | bam_mode = pipe_connect->bam_mode; |
| 1781 | if (bam_mode != USB_BAM_DEVICE) |
| 1782 | return; |
| 1783 | |
| 1784 | log_event_dbg("%s: Starting suspend sequence(BAM=%s)\n", __func__, |
| 1785 | bam_enable_strings[cur_bam]); |
| 1786 | |
| 1787 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1788 | info[cur_bam].bus_suspend = 1; |
| 1789 | |
| 1790 | /* If cable was disconnected, let disconnection seq do everything */ |
| 1791 | if (info[cur_bam].disconnected) { |
| 1792 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1793 | log_event_dbg("%s: Cable disconnected\n", __func__); |
| 1794 | return; |
| 1795 | } |
| 1796 | |
| 1797 | log_event_dbg("%s: Adding src=%d dst=%d in pipes_to_suspend=%d\n", |
| 1798 | __func__, src_idx, |
| 1799 | dst_idx, info[cur_bam].pipes_to_suspend); |
| 1800 | info[cur_bam].suspend_src_idx[info[cur_bam].pipes_to_suspend] = src_idx; |
| 1801 | info[cur_bam].suspend_dst_idx[info[cur_bam].pipes_to_suspend] = dst_idx; |
| 1802 | info[cur_bam].pipes_to_suspend++; |
| 1803 | |
| 1804 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1805 | |
| 1806 | usb_bam_start_suspend(&info[cur_bam]); |
| 1807 | } |
| 1808 | |
| 1809 | static void usb_bam_start_suspend(struct usb_bam_ipa_handshake_info *info_ptr) |
| 1810 | { |
| 1811 | struct usb_bam_pipe_connect *pipe_connect; |
| 1812 | enum usb_ctrl cur_bam = info_ptr->bam_type; |
| 1813 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1814 | u8 src_idx, dst_idx; |
| 1815 | int pipes_to_suspend; |
| 1816 | |
| 1817 | cur_bam = info_ptr->bam_type; |
| 1818 | log_event_dbg("%s: Starting suspend sequence(BAM=%s)\n", __func__, |
| 1819 | bam_enable_strings[cur_bam]); |
| 1820 | |
| 1821 | mutex_lock(&info[cur_bam].suspend_resume_mutex); |
| 1822 | |
| 1823 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1824 | /* If cable was disconnected, let disconnection seq do everything */ |
| 1825 | if (info[cur_bam].disconnected) { |
| 1826 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1827 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1828 | log_event_dbg("%s: Cable disconnected\n", __func__); |
| 1829 | return; |
| 1830 | } |
| 1831 | |
| 1832 | pipes_to_suspend = info[cur_bam].pipes_to_suspend; |
| 1833 | if (!info[cur_bam].bus_suspend || !pipes_to_suspend) { |
| 1834 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1835 | log_event_dbg("%s: Resume started, not suspending\n", __func__); |
| 1836 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1837 | return; |
| 1838 | } |
| 1839 | |
| 1840 | src_idx = info[cur_bam].suspend_src_idx[pipes_to_suspend - 1]; |
| 1841 | dst_idx = info[cur_bam].suspend_dst_idx[pipes_to_suspend - 1]; |
| 1842 | |
| 1843 | pipe_connect = &ctx->usb_bam_connections[dst_idx]; |
| 1844 | stop_prod_transfers(pipe_connect); |
| 1845 | |
| 1846 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1847 | |
| 1848 | /* Don't start LPM seq if data in the pipes */ |
| 1849 | if (!check_pipes_empty(cur_bam, src_idx, dst_idx)) { |
| 1850 | start_prod_transfers(pipe_connect); |
| 1851 | info[cur_bam].pipes_to_suspend = 0; |
| 1852 | info[cur_bam].bus_suspend = 0; |
| 1853 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1854 | return; |
| 1855 | } |
| 1856 | |
| 1857 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1858 | |
| 1859 | /* Start release handshake on the last pipe */ |
| 1860 | if (info[cur_bam].pipes_to_suspend * 2 == ctx->pipes_enabled_per_bam) { |
| 1861 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1862 | wait_for_prod_release(cur_bam); |
| 1863 | } else { |
| 1864 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1865 | } |
| 1866 | |
| 1867 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1868 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_RELEASED) |
| 1869 | usb_bam_finish_suspend(cur_bam); |
| 1870 | else |
| 1871 | log_event_dbg("Consumer not released yet\n"); |
| 1872 | } |
| 1873 | |
| 1874 | static void usb_bam_finish_resume(struct work_struct *w) |
| 1875 | { |
| 1876 | /* TODO: Change this when HSIC device support is introduced */ |
| 1877 | enum usb_ctrl cur_bam; |
| 1878 | struct usb_bam_ipa_handshake_info *info_ptr; |
| 1879 | struct usb_bam_pipe_connect *pipe_connect; |
| 1880 | struct usb_bam_ctx_type *ctx; |
| 1881 | struct device *bam_dev; |
| 1882 | u32 idx, dst_idx, suspended; |
| 1883 | |
| 1884 | info_ptr = container_of(w, struct usb_bam_ipa_handshake_info, |
| 1885 | resume_work); |
| 1886 | cur_bam = info_ptr->bam_type; |
| 1887 | ctx = &msm_usb_bam[cur_bam]; |
| 1888 | bam_dev = &ctx->usb_bam_pdev->dev; |
| 1889 | |
| 1890 | log_event_dbg("%s: enter bam=%s, RT GET: %d\n", __func__, |
| 1891 | bam_enable_strings[cur_bam], get_pm_runtime_counter(bam_dev)); |
| 1892 | |
| 1893 | pm_runtime_get_sync(bam_dev); |
| 1894 | |
| 1895 | mutex_lock(&info[cur_bam].suspend_resume_mutex); |
| 1896 | |
| 1897 | /* Suspend or disconnect happened in the meantime */ |
| 1898 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1899 | if (info[cur_bam].bus_suspend || info[cur_bam].disconnected) { |
| 1900 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1901 | log_event_dbg("%s: Bus suspended, not resuming, RT PUT: %d\n", |
| 1902 | __func__, get_pm_runtime_counter(bam_dev)); |
| 1903 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1904 | pm_runtime_put_sync(bam_dev); |
| 1905 | return; |
| 1906 | } |
| 1907 | info[cur_bam].pipes_to_suspend = 0; |
| 1908 | |
| 1909 | log_event_dbg("Resuming: pipes_suspended =%d\n", |
| 1910 | info[cur_bam].pipes_suspended); |
| 1911 | |
| 1912 | suspended = info[cur_bam].pipes_suspended; |
| 1913 | while (suspended >= 1) { |
| 1914 | idx = suspended - 1; |
| 1915 | dst_idx = info[cur_bam].resume_dst_idx[idx]; |
| 1916 | pipe_connect = &ctx->usb_bam_connections[dst_idx]; |
| 1917 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1918 | reset_pipe_for_resume(pipe_connect); |
| 1919 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1920 | if (pipe_connect->cons_stopped) { |
| 1921 | log_event_dbg("%s: Starting CONS on %d\n", __func__, |
| 1922 | dst_idx); |
| 1923 | start_cons_transfers(pipe_connect); |
| 1924 | } |
| 1925 | suspended--; |
| 1926 | } |
| 1927 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_GRANTED) { |
| 1928 | log_event_dbg("%s: Notify CONS_GRANTED\n", __func__); |
| 1929 | ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED, |
| 1930 | ipa_rm_resource_cons[cur_bam]); |
| 1931 | } |
| 1932 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1933 | |
| 1934 | /* Start handshake for the first pipe resumed */ |
| 1935 | if (info[cur_bam].pipes_resumed == 0) |
| 1936 | wait_for_prod_granted(cur_bam); |
| 1937 | |
| 1938 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 1939 | while (info[cur_bam].pipes_suspended >= 1) { |
| 1940 | idx = info[cur_bam].pipes_suspended - 1; |
| 1941 | dst_idx = info[cur_bam].resume_dst_idx[idx]; |
| 1942 | pipe_connect = &ctx->usb_bam_connections[dst_idx]; |
| 1943 | log_event_dbg("%s: Starting PROD on %d\n", __func__, dst_idx); |
| 1944 | start_prod_transfers(pipe_connect); |
| 1945 | info[cur_bam].pipes_suspended--; |
| 1946 | info[cur_bam].pipes_resumed++; |
| 1947 | log_event_dbg("%s: PM Runtime GET %d, count: %d\n", |
| 1948 | __func__, idx, get_pm_runtime_counter(bam_dev)); |
| 1949 | pm_runtime_get(&ctx->usb_bam_pdev->dev); |
| 1950 | } |
| 1951 | |
| 1952 | if (info[cur_bam].pipes_resumed == ctx->pipes_enabled_per_bam) { |
| 1953 | info[cur_bam].pipes_resumed = 0; |
| 1954 | if (info[cur_bam].cur_cons_state == IPA_RM_RESOURCE_GRANTED) { |
| 1955 | log_event_dbg("%s: Notify CONS_GRANTED\n", __func__); |
| 1956 | ipa_rm_notify_completion(IPA_RM_RESOURCE_GRANTED, |
| 1957 | ipa_rm_resource_cons[cur_bam]); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 1962 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 1963 | log_event_dbg("%s: done..PM Runtime PUT :%d\n", |
| 1964 | __func__, get_pm_runtime_counter(bam_dev)); |
| 1965 | /* Put to match _get at the beginning of this routine */ |
| 1966 | pm_runtime_put(&ctx->usb_bam_pdev->dev); |
| 1967 | } |
| 1968 | |
| 1969 | void usb_bam_resume(enum usb_ctrl cur_bam, |
| 1970 | struct usb_bam_connect_ipa_params *ipa_params) |
| 1971 | { |
| 1972 | u8 src_idx, dst_idx; |
| 1973 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 1974 | struct usb_bam_pipe_connect *pipe_connect; |
| 1975 | |
| 1976 | log_event_dbg("%s: Resuming\n", __func__); |
| 1977 | |
| 1978 | if (!ipa_params) { |
| 1979 | log_event_err("%s: Invalid ipa params\n", __func__); |
| 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | src_idx = ipa_params->src_idx; |
| 1984 | dst_idx = ipa_params->dst_idx; |
| 1985 | |
| 1986 | if (src_idx >= ctx->max_connections || |
| 1987 | dst_idx >= ctx->max_connections) { |
| 1988 | log_event_err("%s: Invalid connection index src=%d dst=%d\n", |
| 1989 | __func__, src_idx, dst_idx); |
| 1990 | return; |
| 1991 | } |
| 1992 | |
| 1993 | pipe_connect = &ctx->usb_bam_connections[src_idx]; |
| 1994 | log_event_dbg("%s: bam=%s mode =%d\n", __func__, |
| 1995 | bam_enable_strings[cur_bam], pipe_connect->bam_mode); |
| 1996 | if (pipe_connect->bam_mode != USB_BAM_DEVICE) |
| 1997 | return; |
| 1998 | |
| 1999 | info[cur_bam].in_lpm = false; |
| 2000 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 2001 | info[cur_bam].bus_suspend = 0; |
| 2002 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 2003 | queue_work(ctx->usb_bam_wq, &info[cur_bam].resume_work); |
| 2004 | } |
| 2005 | |
| 2006 | static void _msm_bam_wait_for_host_prod_granted(enum usb_ctrl bam_type) |
| 2007 | { |
| 2008 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2009 | |
| 2010 | spin_lock(&ctx->usb_bam_lock); |
| 2011 | |
| 2012 | log_event_dbg("%s: enter bam=%s\n", __func__, |
| 2013 | bam_enable_strings[bam_type]); |
| 2014 | ctx->is_bam_inactivity = false; |
| 2015 | |
| 2016 | /* Get back to resume state including wakeup ipa */ |
| 2017 | usb_bam_resume_core(bam_type, USB_BAM_HOST); |
| 2018 | |
| 2019 | /* Ensure getting the producer resource */ |
| 2020 | wait_for_prod_granted(bam_type); |
| 2021 | |
| 2022 | spin_unlock(&ctx->usb_bam_lock); |
| 2023 | |
| 2024 | } |
| 2025 | |
| 2026 | void msm_bam_wait_for_hsic_host_prod_granted(void) |
| 2027 | { |
| 2028 | log_event_dbg("%s: start\n", __func__); |
| 2029 | _msm_bam_wait_for_host_prod_granted(HSIC_CTRL); |
| 2030 | } |
| 2031 | |
| 2032 | static void _msm_bam_host_notify_on_resume(enum usb_ctrl bam_type) |
| 2033 | { |
| 2034 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2035 | |
| 2036 | spin_lock(&ctx->usb_bam_lock); |
| 2037 | log_event_dbg("%s: enter bam=%s\n", __func__, |
| 2038 | bam_enable_strings[bam_type]); |
| 2039 | |
| 2040 | host_info[bam_type].in_lpm = false; |
| 2041 | |
| 2042 | /* HSIC resume completed. Notify CONS grant if CONS was requested */ |
| 2043 | notify_usb_connected(bam_type); |
| 2044 | |
| 2045 | /* |
| 2046 | * This function is called to notify the usb bam driver |
| 2047 | * that the hsic core and hsic bam hw are fully resumed |
| 2048 | * and clocked on. Therefore we can now set the inactivity |
| 2049 | * timer to the hsic bam hw. |
| 2050 | */ |
| 2051 | if (ctx->inactivity_timer_ms) |
| 2052 | usb_bam_set_inactivity_timer(bam_type); |
| 2053 | |
| 2054 | spin_unlock(&ctx->usb_bam_lock); |
| 2055 | } |
| 2056 | |
| 2057 | static bool msm_bam_host_lpm_ok(enum usb_ctrl bam_type) |
| 2058 | { |
| 2059 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2060 | struct usb_bam_pipe_connect *pipe_iter; |
| 2061 | int i; |
| 2062 | |
| 2063 | log_event_dbg("%s: enter bam=%s\n", __func__, |
| 2064 | bam_enable_strings[bam_type]); |
| 2065 | |
| 2066 | if (!host_info[bam_type].dev) |
| 2067 | return true; |
| 2068 | |
| 2069 | log_event_dbg("%s: Starting hsic full suspend sequence\n", |
| 2070 | __func__); |
| 2071 | |
| 2072 | /* |
| 2073 | * Start low power mode by releasing the device only if the resources |
| 2074 | * were indeed released and we are still in inactivity state (wakeup |
| 2075 | * event has not occurred while we were waiting for resources release |
| 2076 | */ |
| 2077 | spin_lock(&ctx->usb_bam_lock); |
| 2078 | |
| 2079 | if (info[bam_type].cur_cons_state == IPA_RM_RESOURCE_RELEASED && |
| 2080 | info[bam_type].cur_prod_state == IPA_RM_RESOURCE_RELEASED && |
| 2081 | ctx->is_bam_inactivity && info[bam_type].in_lpm) { |
| 2082 | |
| 2083 | pr_debug("%s(): checking HSIC Host pipe state\n", __func__); |
| 2084 | if (!msm_bam_hsic_host_pipe_empty()) { |
| 2085 | log_event_err("%s(): HSIC HOST Pipe is not empty\n", |
| 2086 | __func__); |
| 2087 | spin_unlock(&ctx->usb_bam_lock); |
| 2088 | return false; |
| 2089 | } |
| 2090 | |
| 2091 | /* HSIC host will go now to lpm */ |
| 2092 | log_event_dbg("%s: vote for suspend hsic %p\n", |
| 2093 | __func__, host_info[bam_type].dev); |
| 2094 | |
| 2095 | for (i = 0; i < ctx->max_connections; i++) { |
| 2096 | pipe_iter = &ctx->usb_bam_connections[i]; |
| 2097 | if (pipe_iter->bam_type == bam_type && |
| 2098 | pipe_iter->enabled && !pipe_iter->suspended) |
| 2099 | pipe_iter->suspended = true; |
| 2100 | } |
| 2101 | |
| 2102 | host_info[bam_type].in_lpm = true; |
| 2103 | spin_unlock(&ctx->usb_bam_lock); |
| 2104 | |
| 2105 | return true; |
| 2106 | } |
| 2107 | |
| 2108 | /* We don't allow lpm, therefore renew our vote here */ |
| 2109 | if (info[bam_type].in_lpm) { |
| 2110 | log_event_dbg("%s: Not allow lpm while ref count=0\n", |
| 2111 | __func__); |
| 2112 | log_event_dbg("%s: inactivity=%d, c_s=%d p_s=%d\n", __func__, |
| 2113 | ctx->is_bam_inactivity, info[bam_type].cur_cons_state, |
| 2114 | info[bam_type].cur_prod_state); |
| 2115 | pm_runtime_get(host_info[bam_type].dev); |
| 2116 | info[bam_type].in_lpm = false; |
| 2117 | spin_unlock(&ctx->usb_bam_lock); |
| 2118 | } else { |
| 2119 | spin_unlock(&ctx->usb_bam_lock); |
| 2120 | } |
| 2121 | |
| 2122 | return false; |
| 2123 | } |
| 2124 | |
| 2125 | void msm_bam_hsic_host_notify_on_resume(void) |
| 2126 | { |
| 2127 | _msm_bam_host_notify_on_resume(HSIC_CTRL); |
| 2128 | } |
| 2129 | |
| 2130 | static int usb_bam_set_ipa_perf(enum usb_ctrl cur_bam, |
| 2131 | enum usb_bam_pipe_dir dir, |
| 2132 | enum usb_device_speed usb_connection_speed) |
| 2133 | { |
| 2134 | int ret; |
| 2135 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 2136 | struct ipa_rm_perf_profile ipa_rm_perf_prof; |
| 2137 | struct msm_usb_bam_platform_data *pdata = |
| 2138 | ctx->usb_bam_pdev->dev.platform_data; |
| 2139 | |
| 2140 | if (usb_connection_speed == USB_SPEED_SUPER) |
| 2141 | ipa_rm_perf_prof.max_supported_bandwidth_mbps = |
| 2142 | pdata->max_mbps_superspeed; |
| 2143 | else |
| 2144 | /* Bam2Bam is supported only for SS and HS (HW limitation) */ |
| 2145 | ipa_rm_perf_prof.max_supported_bandwidth_mbps = |
| 2146 | pdata->max_mbps_highspeed; |
| 2147 | |
| 2148 | /* |
| 2149 | * Having a max mbps property in dtsi file is a must |
| 2150 | * for target with IPA capability. |
| 2151 | */ |
| 2152 | if (!ipa_rm_perf_prof.max_supported_bandwidth_mbps) { |
| 2153 | log_event_err("%s: Max mbps is required for speed %d\n", |
| 2154 | __func__, usb_connection_speed); |
| 2155 | return -EINVAL; |
| 2156 | } |
| 2157 | |
| 2158 | if (dir == USB_TO_PEER_PERIPHERAL) { |
| 2159 | log_event_dbg("%s: vote ipa_perf resource=%d perf=%d mbps\n", |
| 2160 | __func__, ipa_rm_resource_prod[cur_bam], |
| 2161 | ipa_rm_perf_prof.max_supported_bandwidth_mbps); |
| 2162 | ret = ipa_rm_set_perf_profile(ipa_rm_resource_prod[cur_bam], |
| 2163 | &ipa_rm_perf_prof); |
| 2164 | } else { |
| 2165 | log_event_dbg("%s: vote ipa_perf resource=%d perf=%d mbps\n", |
| 2166 | __func__, ipa_rm_resource_cons[cur_bam], |
| 2167 | ipa_rm_perf_prof.max_supported_bandwidth_mbps); |
| 2168 | ret = ipa_rm_set_perf_profile(ipa_rm_resource_cons[cur_bam], |
| 2169 | &ipa_rm_perf_prof); |
| 2170 | } |
| 2171 | |
| 2172 | return ret; |
| 2173 | } |
| 2174 | |
| 2175 | int usb_bam_connect_ipa(enum usb_ctrl cur_bam, |
| 2176 | struct usb_bam_connect_ipa_params *ipa_params) |
| 2177 | { |
| 2178 | u8 idx; |
| 2179 | enum usb_bam_mode cur_mode; |
| 2180 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 2181 | struct usb_bam_pipe_connect *pipe_connect; |
| 2182 | struct device *bam_dev = &ctx->usb_bam_pdev->dev; |
| 2183 | struct msm_usb_bam_platform_data *pdata = bam_dev->platform_data; |
| 2184 | int ret; |
| 2185 | bool bam2bam, is_dpl; |
| 2186 | |
| 2187 | log_event_dbg("%s: start\n", __func__); |
| 2188 | |
| 2189 | if (!ipa_params) { |
| 2190 | log_event_err("%s: Invalid ipa params\n", __func__); |
| 2191 | return -EINVAL; |
| 2192 | } |
| 2193 | |
| 2194 | if (ipa_params->dir == USB_TO_PEER_PERIPHERAL) |
| 2195 | idx = ipa_params->src_idx; |
| 2196 | else |
| 2197 | idx = ipa_params->dst_idx; |
| 2198 | |
| 2199 | if (idx >= ctx->max_connections) { |
| 2200 | log_event_err("%s: Invalid connection index\n", __func__); |
| 2201 | return -EINVAL; |
| 2202 | } |
| 2203 | pipe_connect = &ctx->usb_bam_connections[idx]; |
| 2204 | |
| 2205 | if (pipe_connect->enabled) { |
| 2206 | log_event_err("%s: connection %d was already established\n", |
| 2207 | __func__, idx); |
| 2208 | return 0; |
| 2209 | } |
| 2210 | |
| 2211 | ret = usb_bam_set_ipa_perf(pipe_connect->bam_type, ipa_params->dir, |
| 2212 | ipa_params->usb_connection_speed); |
| 2213 | if (ret) { |
| 2214 | log_event_err("%s: call to usb_bam_set_ipa_perf failed %d\n", |
| 2215 | __func__, ret); |
| 2216 | return ret; |
| 2217 | } |
| 2218 | |
| 2219 | log_event_dbg("%s: enter\n", __func__); |
| 2220 | |
| 2221 | cur_mode = pipe_connect->bam_mode; |
| 2222 | bam2bam = (pipe_connect->pipe_type == USB_BAM_PIPE_BAM2BAM); |
| 2223 | |
| 2224 | if (ipa_params->dst_client == IPA_CLIENT_USB_DPL_CONS) |
| 2225 | is_dpl = true; |
| 2226 | else |
| 2227 | is_dpl = false; |
| 2228 | |
| 2229 | /* Set the BAM mode (host/device) according to connected pipe */ |
| 2230 | info[cur_bam].cur_bam_mode = pipe_connect->bam_mode; |
| 2231 | |
| 2232 | if (cur_mode == USB_BAM_DEVICE) { |
| 2233 | mutex_lock(&info[cur_bam].suspend_resume_mutex); |
| 2234 | |
| 2235 | spin_lock(&ctx->usb_bam_lock); |
| 2236 | if (ctx->pipes_enabled_per_bam == 0) { |
| 2237 | spin_unlock(&ctx->usb_bam_lock); |
| 2238 | spin_lock(&usb_bam_ipa_handshake_info_lock); |
| 2239 | info[cur_bam].connect_complete = 0; |
| 2240 | info[cur_bam].disconnected = 0; |
| 2241 | info[cur_bam].bus_suspend = 0; |
| 2242 | info[cur_bam].pipes_suspended = 0; |
| 2243 | info[cur_bam].pipes_to_suspend = 0; |
| 2244 | info[cur_bam].pipes_resumed = 0; |
| 2245 | spin_unlock(&usb_bam_ipa_handshake_info_lock); |
| 2246 | } else { |
| 2247 | spin_unlock(&ctx->usb_bam_lock); |
| 2248 | } |
| 2249 | pipe_connect->cons_stopped = 0; |
| 2250 | pipe_connect->prod_stopped = 0; |
| 2251 | } |
| 2252 | |
| 2253 | log_event_dbg("%s: PM Runtime GET %d, count: %d\n", |
| 2254 | __func__, idx, get_pm_runtime_counter(bam_dev)); |
| 2255 | pm_runtime_get_sync(bam_dev); |
| 2256 | |
| 2257 | /* Check if BAM requires RESET before connect and reset first pipe */ |
| 2258 | spin_lock(&ctx->usb_bam_lock); |
| 2259 | if (pdata->reset_on_connect && !ctx->pipes_enabled_per_bam) { |
| 2260 | spin_unlock(&ctx->usb_bam_lock); |
| 2261 | if (cur_bam == CI_CTRL) |
| 2262 | msm_hw_bam_disable(1); |
| 2263 | |
| 2264 | sps_device_reset(ctx->h_bam); |
| 2265 | |
| 2266 | if (cur_bam == CI_CTRL) |
| 2267 | msm_hw_bam_disable(0); |
| 2268 | |
| 2269 | /* On re-connect assume out from lpm for HOST BAM */ |
| 2270 | if (cur_mode == USB_BAM_HOST) |
| 2271 | usb_bam_resume_core(cur_bam, cur_mode); |
| 2272 | |
| 2273 | /* On re-connect assume out from lpm for all BAMs */ |
| 2274 | info[cur_bam].in_lpm = false; |
| 2275 | } else { |
| 2276 | spin_unlock(&ctx->usb_bam_lock); |
| 2277 | if (!ctx->pipes_enabled_per_bam) |
| 2278 | pr_debug("No BAM reset on connect, just pipe reset\n"); |
| 2279 | } |
| 2280 | |
| 2281 | if (ipa_params->dir == USB_TO_PEER_PERIPHERAL) { |
| 2282 | if (info[cur_bam].prod_pipes_enabled_per_bam == 0) |
| 2283 | wait_for_prod_granted(cur_bam); |
| 2284 | info[cur_bam].prod_pipes_enabled_per_bam += 1; |
| 2285 | } |
| 2286 | |
| 2287 | if (bam2bam) |
| 2288 | ret = connect_pipe_bam2bam_ipa(cur_bam, idx, ipa_params); |
| 2289 | else |
| 2290 | ret = connect_pipe_sys2bam_ipa(cur_bam, idx, ipa_params); |
| 2291 | |
| 2292 | if (ret) { |
| 2293 | log_event_err("%s: pipe connection failure RT PUT: %d\n", |
| 2294 | __func__, get_pm_runtime_counter(bam_dev)); |
| 2295 | pm_runtime_put_sync(bam_dev); |
| 2296 | if (cur_mode == USB_BAM_DEVICE) |
| 2297 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 2298 | return ret; |
| 2299 | } |
| 2300 | |
| 2301 | log_event_dbg("%s: pipe connection success\n", __func__); |
| 2302 | spin_lock(&ctx->usb_bam_lock); |
| 2303 | pipe_connect->enabled = 1; |
| 2304 | pipe_connect->suspended = 0; |
| 2305 | |
| 2306 | /* Set global inactivity timer upon first pipe connection */ |
| 2307 | if (!ctx->pipes_enabled_per_bam && ctx->inactivity_timer_ms && |
| 2308 | pipe_connect->inactivity_notify && bam2bam) |
| 2309 | usb_bam_set_inactivity_timer(cur_bam); |
| 2310 | |
| 2311 | ctx->pipes_enabled_per_bam += 1; |
| 2312 | |
| 2313 | /* |
| 2314 | * Notify USB connected on the first two pipes connected for |
| 2315 | * tethered function's producer and consumer only. Current |
| 2316 | * understanding is that there won't be more than 3 pipes used |
| 2317 | * in USB BAM2BAM IPA mode i.e. 2 consumers and 1 producer. |
| 2318 | * If more producer and consumer pipe are being used, this |
| 2319 | * logic is required to be revisited here. |
| 2320 | */ |
| 2321 | if (ctx->pipes_enabled_per_bam >= 2 && |
| 2322 | ipa_params->dir == PEER_PERIPHERAL_TO_USB && !is_dpl) |
| 2323 | notify_usb_connected(cur_bam); |
| 2324 | spin_unlock(&ctx->usb_bam_lock); |
| 2325 | |
| 2326 | if (cur_mode == USB_BAM_DEVICE) |
| 2327 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 2328 | |
| 2329 | log_event_dbg("%s: done\n", __func__); |
| 2330 | return 0; |
| 2331 | } |
| 2332 | EXPORT_SYMBOL(usb_bam_connect_ipa); |
| 2333 | |
| 2334 | int usb_bam_get_pipe_type(enum usb_ctrl bam_type, u8 idx, |
| 2335 | enum usb_bam_pipe_type *type) |
| 2336 | { |
| 2337 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2338 | struct usb_bam_pipe_connect *pipe_connect = |
| 2339 | &ctx->usb_bam_connections[idx]; |
| 2340 | |
| 2341 | if (idx >= ctx->max_connections) { |
| 2342 | log_event_err("%s: Invalid connection index\n", __func__); |
| 2343 | return -EINVAL; |
| 2344 | } |
| 2345 | |
| 2346 | if (!type) { |
| 2347 | log_event_err("%s: null pointer provided for type\n", __func__); |
| 2348 | return -EINVAL; |
| 2349 | } |
| 2350 | |
| 2351 | *type = pipe_connect->pipe_type; |
| 2352 | return 0; |
| 2353 | } |
| 2354 | EXPORT_SYMBOL(usb_bam_get_pipe_type); |
| 2355 | |
| 2356 | static void usb_bam_work(struct work_struct *w) |
| 2357 | { |
| 2358 | int i; |
| 2359 | struct usb_bam_event_info *event_info = |
| 2360 | container_of(w, struct usb_bam_event_info, event_w); |
| 2361 | struct usb_bam_pipe_connect *pipe_connect = |
| 2362 | container_of(event_info, struct usb_bam_pipe_connect, event); |
| 2363 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[pipe_connect->bam_type]; |
| 2364 | struct usb_bam_pipe_connect *pipe_iter; |
| 2365 | int (*callback)(void *priv); |
| 2366 | void *param = NULL; |
| 2367 | |
| 2368 | switch (event_info->type) { |
| 2369 | case USB_BAM_EVENT_WAKEUP: |
| 2370 | case USB_BAM_EVENT_WAKEUP_PIPE: |
| 2371 | |
| 2372 | log_event_dbg("%s received USB_BAM_EVENT_WAKEUP\n", __func__); |
| 2373 | |
| 2374 | /* |
| 2375 | * Make sure the PROD resource is granted before |
| 2376 | * wakeup hsic host class driver (done by the callback below) |
| 2377 | */ |
| 2378 | if (pipe_connect->peer_bam == IPA_P_BAM && |
| 2379 | pipe_connect->bam_mode == USB_BAM_HOST && |
| 2380 | info[pipe_connect->bam_type].cur_prod_state |
| 2381 | != IPA_RM_RESOURCE_GRANTED) { |
| 2382 | wait_for_prod_granted(pipe_connect->bam_type); |
| 2383 | } |
| 2384 | |
| 2385 | /* |
| 2386 | * Check if need to resume the hsic host. |
| 2387 | * On one hand, since we got the wakeup interrupt |
| 2388 | * the hsic bam clocks are already enabled, so no need |
| 2389 | * to actualluy resume the hardware... However, we still need |
| 2390 | * to update the usb bam driver state (to set in_lpm=false), |
| 2391 | * and to wake ipa and to hold again the hsic host |
| 2392 | * device again to avoid it going to low poer mode next time |
| 2393 | * until we complete releasing the hsic consumer and producer |
| 2394 | * resources against the ipa resource manager. |
| 2395 | */ |
| 2396 | spin_lock(&ctx->usb_bam_lock); |
| 2397 | if (pipe_connect->bam_mode == USB_BAM_HOST) |
| 2398 | usb_bam_resume_host(pipe_connect->bam_type); |
| 2399 | spin_unlock(&ctx->usb_bam_lock); |
| 2400 | |
| 2401 | /* Notify about wakeup / activity of the bam */ |
| 2402 | if (event_info->callback) |
| 2403 | event_info->callback(event_info->param); |
| 2404 | |
| 2405 | /* |
| 2406 | * Reset inactivity timer counter if this pipe's bam |
| 2407 | * has inactivity timeout. |
| 2408 | */ |
| 2409 | spin_lock(&ctx->usb_bam_lock); |
| 2410 | if (ctx->inactivity_timer_ms) |
| 2411 | usb_bam_set_inactivity_timer(pipe_connect->bam_type); |
| 2412 | spin_unlock(&ctx->usb_bam_lock); |
| 2413 | |
| 2414 | if (pipe_connect->bam_mode == USB_BAM_DEVICE) { |
| 2415 | /* A2 wakeup not from LPM (CONS was up) */ |
| 2416 | wait_for_prod_granted(pipe_connect->bam_type); |
| 2417 | if (pipe_connect->start) { |
| 2418 | log_event_dbg("%s: Enqueue PROD transfer\n", |
| 2419 | __func__); |
| 2420 | pipe_connect->start( |
| 2421 | pipe_connect->start_stop_param, |
| 2422 | USB_TO_PEER_PERIPHERAL); |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | break; |
| 2427 | |
| 2428 | case USB_BAM_EVENT_INACTIVITY: |
| 2429 | |
| 2430 | log_event_dbg("%s received USB_BAM_EVENT_INACTIVITY\n", |
| 2431 | __func__); |
| 2432 | |
| 2433 | /* |
| 2434 | * Since event info is one structure per pipe, it might be |
| 2435 | * overridden when we will register the wakeup events below, |
| 2436 | * and still we want ot register the wakeup events before we |
| 2437 | * notify on the inactivity in order to identify the next |
| 2438 | * activity as soon as possible. |
| 2439 | */ |
| 2440 | callback = event_info->callback; |
| 2441 | param = event_info->param; |
| 2442 | |
| 2443 | /* |
| 2444 | * Upon inactivity, configure wakeup irq for all pipes |
| 2445 | * that are into the usb bam. |
| 2446 | */ |
| 2447 | spin_lock(&ctx->usb_bam_lock); |
| 2448 | for (i = 0; i < ctx->max_connections; i++) { |
| 2449 | pipe_iter = &ctx->usb_bam_connections[i]; |
| 2450 | if (pipe_iter->bam_type == pipe_connect->bam_type && |
| 2451 | pipe_iter->dir == PEER_PERIPHERAL_TO_USB && |
| 2452 | pipe_iter->enabled) { |
| 2453 | log_event_dbg("%s: Register wakeup on pipe %p\n", |
| 2454 | __func__, pipe_iter); |
| 2455 | __usb_bam_register_wake_cb( |
| 2456 | pipe_connect->bam_type, i, |
| 2457 | pipe_iter->activity_notify, |
| 2458 | pipe_iter->priv, |
| 2459 | false); |
| 2460 | } |
| 2461 | } |
| 2462 | spin_unlock(&ctx->usb_bam_lock); |
| 2463 | |
| 2464 | /* Notify about the inactivity to the USB class driver */ |
| 2465 | if (callback) |
| 2466 | callback(param); |
| 2467 | |
| 2468 | wait_for_prod_release(pipe_connect->bam_type); |
| 2469 | log_event_dbg("%s: complete wait on hsic producer s=%d\n", |
| 2470 | __func__, info[pipe_connect->bam_type].cur_prod_state); |
| 2471 | |
| 2472 | /* |
| 2473 | * Allow to go to lpm for now if also consumer is down. |
| 2474 | * If consumer is up, we will wait to the release consumer |
| 2475 | * notification. |
| 2476 | */ |
| 2477 | if (host_info[pipe_connect->bam_type].dev && |
| 2478 | info[pipe_connect->bam_type].cur_cons_state == |
| 2479 | IPA_RM_RESOURCE_RELEASED && |
| 2480 | !info[pipe_connect->bam_type].in_lpm) { |
| 2481 | usb_bam_suspend_core(pipe_connect->bam_type, |
| 2482 | pipe_connect->bam_mode, 1); |
| 2483 | } |
| 2484 | |
| 2485 | break; |
| 2486 | default: |
| 2487 | log_event_err("%s: unknown usb bam event type %d\n", __func__, |
| 2488 | event_info->type); |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | static void usb_bam_wake_cb(struct sps_event_notify *notify) |
| 2493 | { |
| 2494 | struct usb_bam_event_info *event_info = |
| 2495 | (struct usb_bam_event_info *)notify->user; |
| 2496 | struct usb_bam_pipe_connect *pipe_connect = |
| 2497 | container_of(event_info, |
| 2498 | struct usb_bam_pipe_connect, |
| 2499 | event); |
| 2500 | enum usb_ctrl bam = pipe_connect->bam_type; |
| 2501 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam]; |
| 2502 | |
| 2503 | spin_lock(&ctx->usb_bam_lock); |
| 2504 | |
| 2505 | if (event_info->type == USB_BAM_EVENT_WAKEUP_PIPE) |
| 2506 | queue_work(ctx->usb_bam_wq, &event_info->event_w); |
| 2507 | else if (event_info->type == USB_BAM_EVENT_WAKEUP && |
| 2508 | ctx->is_bam_inactivity) { |
| 2509 | |
| 2510 | /* |
| 2511 | * Sps wake event is per pipe, so usb_bam_wake_cb is |
| 2512 | * called per pipe. However, we want to filter the wake |
| 2513 | * event to be wake event per all the pipes. |
| 2514 | * Therefore, the first pipe that awaked will be considered |
| 2515 | * as global bam wake event. |
| 2516 | */ |
| 2517 | ctx->is_bam_inactivity = false; |
| 2518 | |
| 2519 | queue_work(ctx->usb_bam_wq, &event_info->event_w); |
| 2520 | } |
| 2521 | |
| 2522 | spin_unlock(&ctx->usb_bam_lock); |
| 2523 | } |
| 2524 | |
| 2525 | static int __usb_bam_register_wake_cb(enum usb_ctrl bam_type, int idx, |
| 2526 | int (*callback)(void *user), void *param, |
| 2527 | bool trigger_cb_per_pipe) |
| 2528 | { |
| 2529 | struct sps_pipe *pipe; |
| 2530 | struct sps_connect *sps_connection; |
| 2531 | struct usb_bam_pipe_connect *pipe_connect; |
| 2532 | struct usb_bam_event_info *wake_event_info; |
| 2533 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2534 | int ret; |
| 2535 | |
| 2536 | if (idx < 0 || idx > ctx->max_connections) { |
| 2537 | log_event_err("%s:idx is wrong %d\n", __func__, idx); |
| 2538 | return -EINVAL; |
| 2539 | } |
| 2540 | pipe = ctx->usb_bam_sps.sps_pipes[idx]; |
| 2541 | sps_connection = &ctx->usb_bam_sps.sps_connections[idx]; |
| 2542 | pipe_connect = &ctx->usb_bam_connections[idx]; |
| 2543 | wake_event_info = &pipe_connect->event; |
| 2544 | |
| 2545 | wake_event_info->type = (trigger_cb_per_pipe ? |
| 2546 | USB_BAM_EVENT_WAKEUP_PIPE : |
| 2547 | USB_BAM_EVENT_WAKEUP); |
| 2548 | wake_event_info->param = param; |
| 2549 | wake_event_info->callback = callback; |
| 2550 | wake_event_info->event.mode = SPS_TRIGGER_CALLBACK; |
| 2551 | wake_event_info->event.xfer_done = NULL; |
| 2552 | wake_event_info->event.callback = callback ? usb_bam_wake_cb : NULL; |
| 2553 | wake_event_info->event.user = wake_event_info; |
| 2554 | wake_event_info->event.options = SPS_O_WAKEUP; |
| 2555 | ret = sps_register_event(pipe, &wake_event_info->event); |
| 2556 | if (ret) { |
| 2557 | log_event_err("%s: sps_register_event() failed %d\n", |
| 2558 | __func__, ret); |
| 2559 | return ret; |
| 2560 | } |
| 2561 | |
| 2562 | sps_connection->options = callback ? |
| 2563 | (SPS_O_AUTO_ENABLE | SPS_O_WAKEUP | SPS_O_WAKEUP_IS_ONESHOT) : |
| 2564 | SPS_O_AUTO_ENABLE; |
| 2565 | ret = sps_set_config(pipe, sps_connection); |
| 2566 | if (ret) { |
| 2567 | log_event_err("%s: sps_set_config() failed %d\n", |
| 2568 | __func__, ret); |
| 2569 | return ret; |
| 2570 | } |
| 2571 | log_event_dbg("%s: success\n", __func__); |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
| 2575 | int usb_bam_register_wake_cb(enum usb_ctrl bam_type, u8 idx, |
| 2576 | int (*callback)(void *user), void *param) |
| 2577 | { |
| 2578 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2579 | struct usb_bam_pipe_connect *pipe_connect = |
| 2580 | &ctx->usb_bam_connections[idx]; |
| 2581 | |
| 2582 | info[pipe_connect->bam_type].wake_cb = callback; |
| 2583 | info[pipe_connect->bam_type].wake_param = param; |
| 2584 | return __usb_bam_register_wake_cb(bam_type, idx, callback, param, true); |
| 2585 | } |
| 2586 | |
| 2587 | int usb_bam_register_start_stop_cbs(enum usb_ctrl bam_type, u8 dst_idx, |
| 2588 | void (*start)(void *, enum usb_bam_pipe_dir), |
| 2589 | void (*stop)(void *, enum usb_bam_pipe_dir), void *param) |
| 2590 | { |
| 2591 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2592 | struct usb_bam_pipe_connect *pipe_connect = |
| 2593 | &ctx->usb_bam_connections[dst_idx]; |
| 2594 | |
| 2595 | log_event_dbg("%s: Register for %d\n", __func__, dst_idx); |
| 2596 | pipe_connect->start = start; |
| 2597 | pipe_connect->stop = stop; |
| 2598 | pipe_connect->start_stop_param = param; |
| 2599 | |
| 2600 | return 0; |
| 2601 | } |
| 2602 | |
| 2603 | int usb_bam_disconnect_pipe(enum usb_ctrl bam_type, u8 idx) |
| 2604 | { |
| 2605 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 2606 | struct usb_bam_pipe_connect *pipe_connect; |
| 2607 | struct device *bam_dev = &ctx->usb_bam_pdev->dev; |
| 2608 | int ret; |
| 2609 | struct msm_usb_bam_platform_data *pdata = |
| 2610 | ctx->usb_bam_pdev->dev.platform_data; |
| 2611 | |
| 2612 | pipe_connect = &ctx->usb_bam_connections[idx]; |
| 2613 | |
| 2614 | if (!pipe_connect->enabled) { |
| 2615 | log_event_err("%s: connection %d isn't enabled\n", |
| 2616 | __func__, idx); |
| 2617 | return 0; |
| 2618 | } |
| 2619 | |
| 2620 | ret = disconnect_pipe(bam_type, idx); |
| 2621 | if (ret) { |
| 2622 | log_event_err("%s: src pipe disconnection failure\n", __func__); |
| 2623 | return ret; |
| 2624 | } |
| 2625 | |
| 2626 | pipe_connect->enabled = 0; |
| 2627 | spin_lock(&ctx->usb_bam_lock); |
| 2628 | if (!ctx->pipes_enabled_per_bam) { |
| 2629 | log_event_err("%s: wrong pipes enabled counter for bam_type=%d\n", |
| 2630 | __func__, bam_type); |
| 2631 | } else { |
| 2632 | ctx->pipes_enabled_per_bam -= 1; |
| 2633 | } |
| 2634 | spin_unlock(&ctx->usb_bam_lock); |
| 2635 | |
| 2636 | log_event_dbg("%s: success disconnecting pipe %d\n", __func__, idx); |
| 2637 | |
| 2638 | if (pdata->reset_on_disconnect && !ctx->pipes_enabled_per_bam) { |
| 2639 | if (bam_type == CI_CTRL) |
| 2640 | msm_hw_bam_disable(1); |
| 2641 | |
| 2642 | sps_device_reset(ctx->h_bam); |
| 2643 | |
| 2644 | if (bam_type == CI_CTRL) |
| 2645 | msm_hw_bam_disable(0); |
| 2646 | /* Enable usb irq here which is disabled in function drivers |
| 2647 | * during disconnect after BAM reset. |
| 2648 | */ |
| 2649 | if (bam_type == CI_CTRL) |
| 2650 | msm_usb_irq_disable(false); |
| 2651 | } |
| 2652 | /* This function is directly called by USB Transport drivers |
| 2653 | * to disconnect pipes. Drop runtime usage count here. For |
| 2654 | * IPA, caller takes care of it |
| 2655 | */ |
| 2656 | if (pipe_connect->peer_bam != IPA_P_BAM) { |
| 2657 | log_event_dbg("%s: PM Runtime PUT %d, count: %d\n", |
| 2658 | __func__, idx, get_pm_runtime_counter(bam_dev)); |
| 2659 | pm_runtime_put_sync(bam_dev); |
| 2660 | } |
| 2661 | |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | /** |
| 2666 | * is_ipa_hanlde_valid: Check if ipa_handle is valid or not |
| 2667 | * @ipa_handle: IPA Handle for producer or consumer |
| 2668 | * |
| 2669 | * Returns true is ipa handle is valid. |
| 2670 | */ |
| 2671 | static bool is_ipa_handle_valid(u32 ipa_handle) |
| 2672 | { |
| 2673 | |
| 2674 | return (ipa_handle != -1); |
| 2675 | } |
| 2676 | |
| 2677 | int usb_bam_disconnect_ipa(enum usb_ctrl cur_bam, |
| 2678 | struct usb_bam_connect_ipa_params *ipa_params) |
| 2679 | { |
| 2680 | int ret = 0, pipes_disconncted = 0; |
| 2681 | u8 idx = 0; |
| 2682 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam]; |
| 2683 | struct usb_bam_pipe_connect *pipe_connect; |
| 2684 | struct device *bam_dev = &ctx->usb_bam_pdev->dev; |
| 2685 | enum usb_bam_mode bam_mode; |
| 2686 | |
| 2687 | if (!is_ipa_handle_valid(ipa_params->prod_clnt_hdl) && |
| 2688 | !is_ipa_handle_valid(ipa_params->cons_clnt_hdl)) { |
| 2689 | log_event_err("%s: Both IPA handles are invalid.\n", __func__); |
| 2690 | return -EINVAL; |
| 2691 | } |
| 2692 | |
| 2693 | log_event_dbg("%s: Starting disconnect sequence\n", __func__); |
| 2694 | log_event_dbg("%s(): prod_clnt_hdl:%d cons_clnt_hdl:%d\n", __func__, |
| 2695 | ipa_params->prod_clnt_hdl, ipa_params->cons_clnt_hdl); |
| 2696 | if (is_ipa_handle_valid(ipa_params->prod_clnt_hdl)) |
| 2697 | idx = ipa_params->dst_idx; |
| 2698 | if (is_ipa_handle_valid(ipa_params->cons_clnt_hdl)) |
| 2699 | idx = ipa_params->src_idx; |
| 2700 | pipe_connect = &ctx->usb_bam_connections[idx]; |
| 2701 | bam_mode = pipe_connect->bam_mode; |
| 2702 | |
| 2703 | if (bam_mode != USB_BAM_DEVICE) |
| 2704 | return -EINVAL; |
| 2705 | |
| 2706 | mutex_lock(&info[cur_bam].suspend_resume_mutex); |
| 2707 | /* Delay USB core to go into lpm before we finish our handshake */ |
| 2708 | if (is_ipa_handle_valid(ipa_params->prod_clnt_hdl)) { |
| 2709 | ret = usb_bam_disconnect_ipa_prod(ipa_params, cur_bam); |
| 2710 | if (ret) |
| 2711 | goto out; |
| 2712 | pipes_disconncted++; |
| 2713 | } |
| 2714 | |
| 2715 | if (is_ipa_handle_valid(ipa_params->cons_clnt_hdl)) { |
| 2716 | ret = usb_bam_disconnect_ipa_cons(ipa_params, cur_bam); |
| 2717 | if (ret) |
| 2718 | goto out; |
| 2719 | pipes_disconncted++; |
| 2720 | } |
| 2721 | |
| 2722 | /* Notify CONS release on the last cons pipe released */ |
| 2723 | if (!ctx->pipes_enabled_per_bam) { |
| 2724 | if (info[cur_bam].cur_cons_state == |
| 2725 | IPA_RM_RESOURCE_RELEASED) { |
| 2726 | log_event_dbg("%s: Notify CONS_RELEASED\n", __func__); |
| 2727 | ipa_rm_notify_completion( |
| 2728 | IPA_RM_RESOURCE_RELEASED, |
| 2729 | ipa_rm_resource_cons[cur_bam]); |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | out: |
| 2734 | /* Pipes are connected one by one, but can get disconnected in pairs */ |
| 2735 | while (pipes_disconncted--) { |
| 2736 | if (!info[cur_bam].pipes_suspended) { |
| 2737 | log_event_dbg("%s: PM Runtime PUT %d, count: %d\n", |
| 2738 | __func__, pipes_disconncted, |
| 2739 | get_pm_runtime_counter(bam_dev)); |
| 2740 | pm_runtime_put_sync(&ctx->usb_bam_pdev->dev); |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | mutex_unlock(&info[cur_bam].suspend_resume_mutex); |
| 2745 | |
| 2746 | return ret; |
| 2747 | } |
| 2748 | EXPORT_SYMBOL(usb_bam_disconnect_ipa); |
| 2749 | |
| 2750 | static void usb_bam_sps_events(enum sps_callback_case sps_cb_case, void *user) |
| 2751 | { |
| 2752 | int i; |
| 2753 | int bam; |
| 2754 | struct usb_bam_ctx_type *ctx; |
| 2755 | struct usb_bam_pipe_connect *pipe_connect; |
| 2756 | struct usb_bam_event_info *event_info; |
| 2757 | |
| 2758 | switch (sps_cb_case) { |
| 2759 | case SPS_CALLBACK_BAM_TIMER_IRQ: |
| 2760 | |
| 2761 | log_event_dbg("%s: received SPS_CALLBACK_BAM_TIMER_IRQ\n", |
| 2762 | __func__); |
| 2763 | |
| 2764 | bam = get_bam_type_from_core_name((char *)user); |
| 2765 | if (bam < 0 || bam >= MAX_BAMS) { |
| 2766 | log_event_err("%s: Invalid bam, type=%d ,name=%s\n", |
| 2767 | __func__, bam, (char *)user); |
| 2768 | return; |
| 2769 | } |
| 2770 | ctx = &msm_usb_bam[bam]; |
| 2771 | spin_lock(&ctx->usb_bam_lock); |
| 2772 | |
| 2773 | ctx->is_bam_inactivity = true; |
| 2774 | log_event_dbg("%s: Inactivity happened on bam=%s,%d\n", |
| 2775 | __func__, (char *)user, bam); |
| 2776 | |
| 2777 | for (i = 0; i < ctx->max_connections; i++) { |
| 2778 | pipe_connect = &ctx->usb_bam_connections[i]; |
| 2779 | |
| 2780 | /* |
| 2781 | * Notify inactivity once, Since it is global |
| 2782 | * for all pipes on bam. Notify only if we have |
| 2783 | * connected pipes. |
| 2784 | */ |
| 2785 | if (pipe_connect->enabled) { |
| 2786 | event_info = &pipe_connect->event; |
| 2787 | event_info->type = USB_BAM_EVENT_INACTIVITY; |
| 2788 | event_info->param = pipe_connect->priv; |
| 2789 | event_info->callback = |
| 2790 | pipe_connect->inactivity_notify; |
| 2791 | queue_work(ctx->usb_bam_wq, |
| 2792 | &event_info->event_w); |
| 2793 | break; |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | spin_unlock(&ctx->usb_bam_lock); |
| 2798 | |
| 2799 | break; |
| 2800 | default: |
| 2801 | log_event_dbg("%s: received sps_cb_case=%d\n", __func__, |
| 2802 | (int)sps_cb_case); |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | static struct msm_usb_bam_platform_data *usb_bam_dt_to_pdata( |
| 2807 | struct platform_device *pdev, u32 usb_addr) |
| 2808 | { |
| 2809 | struct msm_usb_bam_platform_data *pdata; |
| 2810 | struct device_node *node = pdev->dev.of_node; |
| 2811 | int rc = 0; |
| 2812 | u8 i = 0; |
| 2813 | u32 bam, bam_mode; |
| 2814 | u32 addr = 0; |
| 2815 | u32 threshold, max_connections = 0; |
| 2816 | static struct usb_bam_pipe_connect *usb_bam_connections; |
| 2817 | |
| 2818 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 2819 | if (!pdata) |
| 2820 | return NULL; |
| 2821 | |
| 2822 | rc = of_property_read_u32(node, "qcom,bam-type", &bam); |
| 2823 | if (rc) { |
| 2824 | log_event_err("%s: bam type is missing in device tree\n", |
| 2825 | __func__); |
| 2826 | return NULL; |
| 2827 | } |
| 2828 | if (bam >= MAX_BAMS) { |
| 2829 | log_event_err("%s: Invalid bam type %d in device tree\n", |
| 2830 | __func__, bam); |
| 2831 | return NULL; |
| 2832 | } |
| 2833 | pdata->bam_type = bam; |
| 2834 | |
| 2835 | rc = of_property_read_u32(node, "qcom,bam-mode", &bam_mode); |
| 2836 | if (rc) { |
| 2837 | pr_debug("%s: bam mode is missing in device tree\n", |
| 2838 | __func__); |
| 2839 | /* Default to DEVICE if bam_mode is not specified */ |
| 2840 | bam_mode = USB_BAM_DEVICE; |
| 2841 | } |
| 2842 | |
| 2843 | pdata->reset_on_connect = of_property_read_bool(node, |
| 2844 | "qcom,reset-bam-on-connect"); |
| 2845 | |
| 2846 | pdata->reset_on_disconnect = of_property_read_bool(node, |
| 2847 | "qcom,reset-bam-on-disconnect"); |
| 2848 | |
| 2849 | rc = of_property_read_u32(node, "qcom,usb-bam-num-pipes", |
| 2850 | &pdata->usb_bam_num_pipes); |
| 2851 | if (rc) { |
| 2852 | log_event_err("Invalid usb bam num pipes property\n"); |
| 2853 | return NULL; |
| 2854 | } |
| 2855 | |
| 2856 | rc = of_property_read_u32(node, "qcom,usb-bam-max-mbps-highspeed", |
| 2857 | &pdata->max_mbps_highspeed); |
| 2858 | if (rc) |
| 2859 | pdata->max_mbps_highspeed = 0; |
| 2860 | |
| 2861 | rc = of_property_read_u32(node, "qcom,usb-bam-max-mbps-superspeed", |
| 2862 | &pdata->max_mbps_superspeed); |
| 2863 | if (rc) |
| 2864 | pdata->max_mbps_superspeed = 0; |
| 2865 | |
| 2866 | rc = of_property_read_u32(node, "qcom,usb-bam-fifo-baseaddr", &addr); |
| 2867 | if (rc) |
| 2868 | pr_debug("%s: Invalid usb base address property\n", __func__); |
| 2869 | else |
| 2870 | pdata->usb_bam_fifo_baseaddr = addr; |
| 2871 | |
| 2872 | pdata->ignore_core_reset_ack = of_property_read_bool(node, |
| 2873 | "qcom,ignore-core-reset-ack"); |
| 2874 | |
| 2875 | pdata->disable_clk_gating = of_property_read_bool(node, |
| 2876 | "qcom,disable-clk-gating"); |
| 2877 | |
| 2878 | rc = of_property_read_u32(node, "qcom,usb-bam-override-threshold", |
| 2879 | &threshold); |
| 2880 | if (rc) |
| 2881 | pdata->override_threshold = USB_THRESHOLD; |
| 2882 | else |
| 2883 | pdata->override_threshold = threshold; |
| 2884 | |
| 2885 | pdata->enable_hsusb_bam_on_boot = of_property_read_bool(node, |
| 2886 | "qcom,enable-hsusb-bam-on-boot"); |
| 2887 | |
| 2888 | for_each_child_of_node(pdev->dev.of_node, node) |
| 2889 | max_connections++; |
| 2890 | |
| 2891 | if (!max_connections) { |
| 2892 | log_event_err("%s: error: max_connections is zero\n", __func__); |
| 2893 | goto err; |
| 2894 | } |
| 2895 | |
| 2896 | usb_bam_connections = devm_kzalloc(&pdev->dev, max_connections * |
| 2897 | sizeof(struct usb_bam_pipe_connect), GFP_KERNEL); |
| 2898 | |
| 2899 | if (!usb_bam_connections) { |
| 2900 | log_event_err("%s: devm_kzalloc failed(%d)\n", |
| 2901 | __func__, __LINE__); |
| 2902 | return NULL; |
| 2903 | } |
| 2904 | |
| 2905 | /* retrieve device tree parameters */ |
| 2906 | for_each_child_of_node(pdev->dev.of_node, node) { |
| 2907 | usb_bam_connections[i].bam_type = bam; |
| 2908 | usb_bam_connections[i].bam_mode = bam_mode; |
| 2909 | |
| 2910 | rc = of_property_read_string(node, "label", |
| 2911 | &usb_bam_connections[i].name); |
| 2912 | if (rc) |
| 2913 | goto err; |
| 2914 | |
| 2915 | rc = of_property_read_u32(node, "qcom,usb-bam-mem-type", |
| 2916 | &usb_bam_connections[i].mem_type); |
| 2917 | if (rc) |
| 2918 | goto err; |
| 2919 | |
| 2920 | if (usb_bam_connections[i].mem_type == OCI_MEM) { |
| 2921 | if (!pdata->usb_bam_fifo_baseaddr) { |
| 2922 | log_event_err("%s: base address is missing\n", |
| 2923 | __func__); |
| 2924 | goto err; |
| 2925 | } |
| 2926 | } |
| 2927 | rc = of_property_read_u32(node, "qcom,peer-bam", |
| 2928 | &usb_bam_connections[i].peer_bam); |
| 2929 | if (rc) { |
| 2930 | log_event_err("%s: peer bam is missing in device tree\n", |
| 2931 | __func__); |
| 2932 | goto err; |
| 2933 | } |
| 2934 | /* |
| 2935 | * Store USB bam_type to be used with QDSS. As only one device |
| 2936 | * bam is currently supported, check the same in DT connections |
| 2937 | */ |
| 2938 | if (usb_bam_connections[i].peer_bam == QDSS_P_BAM) { |
| 2939 | if (qdss_usb_bam_type) { |
| 2940 | log_event_err("%s: overriding QDSS pipe!, update DT\n", |
| 2941 | __func__); |
| 2942 | } |
| 2943 | qdss_usb_bam_type = usb_bam_connections[i].bam_type; |
| 2944 | } |
| 2945 | |
| 2946 | rc = of_property_read_u32(node, "qcom,dir", |
| 2947 | &usb_bam_connections[i].dir); |
| 2948 | if (rc) { |
| 2949 | log_event_err("%s: direction is missing in device tree\n", |
| 2950 | __func__); |
| 2951 | goto err; |
| 2952 | } |
| 2953 | |
| 2954 | rc = of_property_read_u32(node, "qcom,pipe-num", |
| 2955 | &usb_bam_connections[i].pipe_num); |
| 2956 | if (rc) { |
| 2957 | log_event_err("%s: pipe num is missing in device tree\n", |
| 2958 | __func__); |
| 2959 | goto err; |
| 2960 | } |
| 2961 | |
| 2962 | rc = of_property_read_u32(node, "qcom,pipe-connection-type", |
| 2963 | &usb_bam_connections[i].pipe_type); |
| 2964 | if (rc) |
| 2965 | pr_debug("%s: pipe type is defaulting to bam2bam\n", |
| 2966 | __func__); |
| 2967 | |
| 2968 | of_property_read_u32(node, "qcom,peer-bam-physical-address", |
| 2969 | &addr); |
| 2970 | if (usb_bam_connections[i].dir == USB_TO_PEER_PERIPHERAL) { |
| 2971 | usb_bam_connections[i].src_phy_addr = usb_addr; |
| 2972 | usb_bam_connections[i].dst_phy_addr = addr; |
| 2973 | } else { |
| 2974 | usb_bam_connections[i].src_phy_addr = addr; |
| 2975 | usb_bam_connections[i].dst_phy_addr = usb_addr; |
| 2976 | } |
| 2977 | |
| 2978 | of_property_read_u32(node, "qcom,src-bam-pipe-index", |
| 2979 | &usb_bam_connections[i].src_pipe_index); |
| 2980 | |
| 2981 | of_property_read_u32(node, "qcom,dst-bam-pipe-index", |
| 2982 | &usb_bam_connections[i].dst_pipe_index); |
| 2983 | |
| 2984 | of_property_read_u32(node, "qcom,data-fifo-offset", |
| 2985 | &usb_bam_connections[i].data_fifo_base_offset); |
| 2986 | |
| 2987 | rc = of_property_read_u32(node, "qcom,data-fifo-size", |
| 2988 | &usb_bam_connections[i].data_fifo_size); |
| 2989 | if (rc) |
| 2990 | goto err; |
| 2991 | |
| 2992 | of_property_read_u32(node, "qcom,descriptor-fifo-offset", |
| 2993 | &usb_bam_connections[i].desc_fifo_base_offset); |
| 2994 | |
| 2995 | rc = of_property_read_u32(node, "qcom,descriptor-fifo-size", |
| 2996 | &usb_bam_connections[i].desc_fifo_size); |
| 2997 | if (rc) |
| 2998 | goto err; |
| 2999 | i++; |
| 3000 | } |
| 3001 | |
| 3002 | msm_usb_bam[bam].usb_bam_connections = usb_bam_connections; |
| 3003 | msm_usb_bam[bam].max_connections = max_connections; |
| 3004 | |
| 3005 | return pdata; |
| 3006 | err: |
| 3007 | log_event_err("%s: failed\n", __func__); |
| 3008 | return NULL; |
| 3009 | } |
| 3010 | |
| 3011 | static int usb_bam_init(struct platform_device *pdev) |
| 3012 | { |
| 3013 | int ret; |
| 3014 | struct msm_usb_bam_platform_data *pdata = pdev->dev.platform_data; |
| 3015 | enum usb_ctrl bam_type = pdata->bam_type; |
| 3016 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 3017 | struct sps_bam_props props; |
| 3018 | |
| 3019 | memset(&props, 0, sizeof(props)); |
| 3020 | |
| 3021 | pr_debug("%s: usb_bam_init - %s\n", __func__, |
| 3022 | bam_enable_strings[bam_type]); |
| 3023 | |
| 3024 | props.phys_addr = ctx->io_res->start; |
| 3025 | props.virt_addr = ctx->regs; |
| 3026 | props.virt_size = resource_size(ctx->io_res); |
| 3027 | props.irq = ctx->irq; |
| 3028 | props.summing_threshold = pdata->override_threshold; |
| 3029 | props.event_threshold = pdata->override_threshold; |
| 3030 | props.num_pipes = pdata->usb_bam_num_pipes; |
| 3031 | props.callback = usb_bam_sps_events; |
| 3032 | props.user = bam_enable_strings[bam_type]; |
| 3033 | |
| 3034 | /* |
| 3035 | * HSUSB and HSIC Cores don't support RESET ACK signal to BAMs. |
| 3036 | * Hence let BAM to ignore acknowledge from USB while resetting PIPE. |
| 3037 | */ |
| 3038 | if (pdata->ignore_core_reset_ack && bam_type != DWC3_CTRL) |
| 3039 | props.options = SPS_BAM_NO_EXT_P_RST; |
| 3040 | |
| 3041 | if (pdata->disable_clk_gating) |
| 3042 | props.options |= SPS_BAM_NO_LOCAL_CLK_GATING; |
| 3043 | |
| 3044 | /* |
| 3045 | * HSUSB BAM is not NDP BAM and it must be enabled early before |
| 3046 | * starting peripheral controller to avoid switching USB core mode |
| 3047 | * from legacy to BAM with ongoing data transfers. |
| 3048 | */ |
| 3049 | if (pdata->enable_hsusb_bam_on_boot && bam_type == CI_CTRL) { |
| 3050 | pr_debug("Register and enable HSUSB BAM\n"); |
| 3051 | props.options |= SPS_BAM_OPT_ENABLE_AT_BOOT; |
| 3052 | } |
| 3053 | ret = sps_register_bam_device(&props, &ctx->h_bam); |
| 3054 | |
| 3055 | if (ret < 0) { |
| 3056 | log_event_err("%s: register bam error %d\n", __func__, ret); |
| 3057 | return -EFAULT; |
| 3058 | } |
| 3059 | |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | static int enable_usb_bam(struct platform_device *pdev) |
| 3064 | { |
| 3065 | int ret; |
| 3066 | struct msm_usb_bam_platform_data *pdata = pdev->dev.platform_data; |
| 3067 | enum usb_ctrl bam_type = pdata->bam_type; |
| 3068 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 3069 | |
| 3070 | ret = usb_bam_init(pdev); |
| 3071 | if (ret) { |
| 3072 | log_event_err("failed to init bam %s\n", |
| 3073 | bam_enable_strings[bam_type]); |
| 3074 | return ret; |
| 3075 | } |
| 3076 | |
| 3077 | ctx->usb_bam_sps.sps_pipes = devm_kzalloc(&pdev->dev, |
| 3078 | ctx->max_connections * sizeof(struct sps_pipe *), |
| 3079 | GFP_KERNEL); |
| 3080 | |
| 3081 | if (!ctx->usb_bam_sps.sps_pipes) { |
| 3082 | log_event_err("%s: failed to allocate sps_pipes\n", __func__); |
| 3083 | return -ENOMEM; |
| 3084 | } |
| 3085 | |
| 3086 | ctx->usb_bam_sps.sps_connections = devm_kzalloc(&pdev->dev, |
| 3087 | ctx->max_connections * sizeof(struct sps_connect), |
| 3088 | GFP_KERNEL); |
| 3089 | if (!ctx->usb_bam_sps.sps_connections) { |
| 3090 | log_event_err("%s: failed to allocate sps_connections\n", |
| 3091 | __func__); |
| 3092 | return -ENOMEM; |
| 3093 | } |
| 3094 | |
| 3095 | return 0; |
| 3096 | } |
| 3097 | |
| 3098 | static ssize_t |
| 3099 | usb_bam_show_inactivity_timer(struct device *dev, struct device_attribute *attr, |
| 3100 | char *buf) |
| 3101 | { |
| 3102 | char *buff = buf; |
| 3103 | int i; |
| 3104 | |
| 3105 | for (i = 0; i < ARRAY_SIZE(bam_enable_strings); i++) { |
| 3106 | buff += snprintf(buff, PAGE_SIZE, "%s: %dms\n", |
| 3107 | bam_enable_strings[i], |
| 3108 | msm_usb_bam[i].inactivity_timer_ms); |
| 3109 | } |
| 3110 | |
| 3111 | return buff - buf; |
| 3112 | } |
| 3113 | |
| 3114 | static ssize_t usb_bam_store_inactivity_timer(struct device *dev, |
| 3115 | struct device_attribute *attr, |
| 3116 | const char *buff, size_t count) |
| 3117 | { |
| 3118 | char buf[USB_BAM_MAX_STR_LEN]; |
| 3119 | char *trimmed_buf, *bam_str, *bam_name, *timer; |
| 3120 | int timer_d; |
| 3121 | int bam, ret; |
| 3122 | |
| 3123 | if (strnstr(buff, "help", USB_BAM_MAX_STR_LEN)) { |
| 3124 | pr_info("Usage: <bam_name> <ms>,<bam_name> <ms>,...\n"); |
| 3125 | pr_info("\tbam_name: [%s, %s, %s]\n", |
| 3126 | bam_enable_strings[DWC3_CTRL], |
| 3127 | bam_enable_strings[CI_CTRL], |
| 3128 | bam_enable_strings[HSIC_CTRL]); |
| 3129 | pr_info("\tms: time in ms. Use 0 to disable timer\n"); |
| 3130 | return count; |
| 3131 | } |
| 3132 | |
| 3133 | strlcpy(buf, buff, sizeof(buf)); |
| 3134 | trimmed_buf = strim(buf); |
| 3135 | |
| 3136 | while (trimmed_buf) { |
| 3137 | bam_str = strsep(&trimmed_buf, ","); |
| 3138 | if (bam_str) { |
| 3139 | bam_name = strsep(&bam_str, " "); |
| 3140 | bam = get_bam_type_from_core_name(bam_name); |
| 3141 | if (bam < 0 || bam >= MAX_BAMS) { |
| 3142 | log_event_err("%s: Invalid bam, type=%d ,name=%s\n", |
| 3143 | __func__, bam, bam_name); |
| 3144 | return -EINVAL; |
| 3145 | } |
| 3146 | |
| 3147 | timer = strsep(&bam_str, " "); |
| 3148 | |
| 3149 | if (!timer) |
| 3150 | continue; |
| 3151 | |
| 3152 | ret = kstrtoint(timer, 0, &timer_d); |
| 3153 | if (ret) { |
| 3154 | log_event_err("%s: err:%d with value:(%d)\n", |
| 3155 | __func__, ret, timer_d); |
| 3156 | return ret; |
| 3157 | } |
| 3158 | |
| 3159 | /* Apply new timer setting if bam has running pipes */ |
| 3160 | if (msm_usb_bam[bam].inactivity_timer_ms != timer_d) { |
| 3161 | msm_usb_bam[bam].inactivity_timer_ms = timer_d; |
| 3162 | if (msm_usb_bam[bam].pipes_enabled_per_bam > 0 |
| 3163 | && !info[bam].in_lpm) |
| 3164 | usb_bam_set_inactivity_timer(bam); |
| 3165 | } |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | return count; |
| 3170 | } |
| 3171 | |
| 3172 | static DEVICE_ATTR(inactivity_timer, 0600, |
| 3173 | usb_bam_show_inactivity_timer, |
| 3174 | usb_bam_store_inactivity_timer); |
| 3175 | |
| 3176 | static int usb_bam_panic_notifier(struct notifier_block *this, |
| 3177 | unsigned long event, void *ptr) |
| 3178 | { |
| 3179 | int i; |
| 3180 | struct usb_bam_ctx_type *ctx; |
| 3181 | |
| 3182 | for (i = 0; i < MAX_BAMS; i++) { |
| 3183 | ctx = &msm_usb_bam[i]; |
| 3184 | if (ctx->h_bam) |
| 3185 | break; |
| 3186 | } |
| 3187 | |
| 3188 | if (i == MAX_BAMS) |
| 3189 | goto fail; |
| 3190 | |
| 3191 | if (!ctx->pipes_enabled_per_bam || info[i].pipes_suspended) |
| 3192 | goto fail; |
| 3193 | |
| 3194 | pr_err("%s: dump usb bam registers here in call back!\n", |
| 3195 | __func__); |
| 3196 | sps_get_bam_debug_info(ctx->h_bam, 93, |
| 3197 | (SPS_BAM_PIPE(0) | SPS_BAM_PIPE(1)), 0, 2); |
| 3198 | |
| 3199 | fail: |
| 3200 | return NOTIFY_DONE; |
| 3201 | } |
| 3202 | |
| 3203 | static struct notifier_block usb_bam_panic_blk = { |
| 3204 | .notifier_call = usb_bam_panic_notifier, |
| 3205 | }; |
| 3206 | |
| 3207 | void usb_bam_register_panic_hdlr(void) |
| 3208 | { |
| 3209 | atomic_notifier_chain_register(&panic_notifier_list, |
| 3210 | &usb_bam_panic_blk); |
| 3211 | } |
| 3212 | |
| 3213 | static int usb_bam_probe(struct platform_device *pdev) |
| 3214 | { |
| 3215 | int ret, i, irq; |
| 3216 | struct resource *io_res; |
| 3217 | void __iomem *regs; |
| 3218 | enum usb_ctrl bam_type; |
| 3219 | struct usb_bam_ctx_type *ctx; |
| 3220 | struct msm_usb_bam_platform_data *pdata; |
| 3221 | |
| 3222 | dev_dbg(&pdev->dev, "usb_bam_probe\n"); |
| 3223 | |
| 3224 | ret = device_create_file(&pdev->dev, &dev_attr_inactivity_timer); |
| 3225 | if (ret) { |
| 3226 | dev_err(&pdev->dev, "failed to create fs node\n"); |
| 3227 | return ret; |
| 3228 | } |
| 3229 | |
| 3230 | io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 3231 | if (!io_res) { |
| 3232 | dev_err(&pdev->dev, "missing BAM memory resource\n"); |
| 3233 | return -ENODEV; |
| 3234 | } |
| 3235 | |
| 3236 | irq = platform_get_irq(pdev, 0); |
| 3237 | if (irq < 0) { |
| 3238 | dev_err(&pdev->dev, "Unable to get IRQ resource\n"); |
| 3239 | return irq; |
| 3240 | } |
| 3241 | |
| 3242 | regs = devm_ioremap(&pdev->dev, io_res->start, resource_size(io_res)); |
| 3243 | if (!regs) { |
| 3244 | log_event_err("%s: ioremap failed\n", __func__); |
| 3245 | return -ENOMEM; |
| 3246 | } |
| 3247 | |
| 3248 | /* specify BAM physical address to be filled in BAM connections */ |
| 3249 | pdata = usb_bam_dt_to_pdata(pdev, io_res->start); |
| 3250 | if (!pdata) |
| 3251 | return -EINVAL; |
| 3252 | |
| 3253 | pdev->dev.platform_data = pdata; |
| 3254 | bam_type = pdata->bam_type; |
| 3255 | ctx = &msm_usb_bam[bam_type]; |
| 3256 | |
| 3257 | ctx->usb_bam_pdev = pdev; |
| 3258 | ctx->irq = irq; |
| 3259 | ctx->regs = regs; |
| 3260 | ctx->io_res = io_res; |
| 3261 | |
| 3262 | for (i = 0; i < ctx->max_connections; i++) { |
| 3263 | ctx->usb_bam_connections[i].enabled = 0; |
| 3264 | INIT_WORK(&ctx->usb_bam_connections[i].event.event_w, |
| 3265 | usb_bam_work); |
| 3266 | } |
| 3267 | |
| 3268 | init_completion(&info[bam_type].prod_avail); |
| 3269 | complete(&info[bam_type].prod_avail); |
| 3270 | init_completion(&info[bam_type].prod_released); |
| 3271 | complete(&info[bam_type].prod_released); |
| 3272 | info[bam_type].cur_prod_state = IPA_RM_RESOURCE_RELEASED; |
| 3273 | info[bam_type].cur_cons_state = IPA_RM_RESOURCE_RELEASED; |
| 3274 | info[bam_type].bam_type = bam_type; |
| 3275 | INIT_WORK(&info[bam_type].resume_work, usb_bam_finish_resume); |
| 3276 | INIT_WORK(&info[bam_type].finish_suspend_work, usb_bam_finish_suspend_); |
| 3277 | mutex_init(&info[bam_type].suspend_resume_mutex); |
| 3278 | |
| 3279 | ctx->usb_bam_wq = alloc_workqueue("usb_bam_wq", |
| 3280 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
| 3281 | if (!ctx->usb_bam_wq) { |
| 3282 | log_event_err("unable to create workqueue usb_bam_wq\n"); |
| 3283 | return -ENOMEM; |
| 3284 | } |
| 3285 | |
| 3286 | ret = enable_usb_bam(pdev); |
| 3287 | if (ret) { |
| 3288 | destroy_workqueue(ctx->usb_bam_wq); |
| 3289 | return ret; |
| 3290 | } |
| 3291 | |
| 3292 | pm_runtime_no_callbacks(&pdev->dev); |
| 3293 | pm_runtime_set_active(&pdev->dev); |
| 3294 | pm_runtime_enable(&pdev->dev); |
| 3295 | |
| 3296 | spin_lock_init(&usb_bam_ipa_handshake_info_lock); |
| 3297 | if (ipa_get_transport_type() == IPA_TRANSPORT_TYPE_SPS && |
| 3298 | ipa_is_ready()) |
| 3299 | usb_bam_ipa_create_resources(bam_type); |
| 3300 | spin_lock_init(&ctx->usb_bam_lock); |
| 3301 | |
| 3302 | usb_bam_register_panic_hdlr(); |
| 3303 | return ret; |
| 3304 | } |
| 3305 | |
| 3306 | bool usb_bam_get_prod_granted(enum usb_ctrl bam_type, u8 idx) |
| 3307 | { |
| 3308 | return (info[bam_type].cur_prod_state == IPA_RM_RESOURCE_GRANTED); |
| 3309 | } |
| 3310 | EXPORT_SYMBOL(usb_bam_get_prod_granted); |
| 3311 | |
| 3312 | int get_bam2bam_connection_info(enum usb_ctrl bam_type, u8 idx, |
| 3313 | u32 *usb_bam_pipe_idx, struct sps_mem_buffer *desc_fifo, |
| 3314 | struct sps_mem_buffer *data_fifo, enum usb_pipe_mem_type *mem_type) |
| 3315 | { |
| 3316 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 3317 | struct usb_bam_pipe_connect *pipe_connect = |
| 3318 | &ctx->usb_bam_connections[idx]; |
| 3319 | enum usb_bam_pipe_dir dir = pipe_connect->dir; |
| 3320 | |
| 3321 | if (dir == USB_TO_PEER_PERIPHERAL) |
| 3322 | *usb_bam_pipe_idx = pipe_connect->src_pipe_index; |
| 3323 | else |
| 3324 | *usb_bam_pipe_idx = pipe_connect->dst_pipe_index; |
| 3325 | |
| 3326 | if (data_fifo) |
| 3327 | memcpy(data_fifo, &pipe_connect->data_mem_buf, |
| 3328 | sizeof(struct sps_mem_buffer)); |
| 3329 | if (desc_fifo) |
| 3330 | memcpy(desc_fifo, &pipe_connect->desc_mem_buf, |
| 3331 | sizeof(struct sps_mem_buffer)); |
| 3332 | if (mem_type) |
| 3333 | *mem_type = pipe_connect->mem_type; |
| 3334 | |
| 3335 | return 0; |
| 3336 | } |
| 3337 | EXPORT_SYMBOL(get_bam2bam_connection_info); |
| 3338 | |
| 3339 | int get_qdss_bam_connection_info(unsigned long *usb_bam_handle, |
| 3340 | u32 *usb_bam_pipe_idx, u32 *peer_pipe_idx, |
| 3341 | struct sps_mem_buffer *desc_fifo, struct sps_mem_buffer *data_fifo, |
| 3342 | enum usb_pipe_mem_type *mem_type) |
| 3343 | { |
| 3344 | u8 idx; |
| 3345 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[qdss_usb_bam_type]; |
| 3346 | struct sps_connect *sps_connection; |
| 3347 | |
| 3348 | /* QDSS uses only one pipe */ |
| 3349 | idx = usb_bam_get_connection_idx(qdss_usb_bam_type, QDSS_P_BAM, |
| 3350 | PEER_PERIPHERAL_TO_USB, USB_BAM_DEVICE, 0); |
| 3351 | |
| 3352 | get_bam2bam_connection_info(qdss_usb_bam_type, idx, usb_bam_pipe_idx, |
| 3353 | desc_fifo, data_fifo, mem_type); |
| 3354 | |
| 3355 | |
| 3356 | sps_connection = &ctx->usb_bam_sps.sps_connections[idx]; |
| 3357 | *usb_bam_handle = sps_connection->destination; |
| 3358 | *peer_pipe_idx = sps_connection->src_pipe_index; |
| 3359 | |
| 3360 | return 0; |
| 3361 | } |
| 3362 | EXPORT_SYMBOL(get_qdss_bam_connection_info); |
| 3363 | |
| 3364 | int usb_bam_get_connection_idx(enum usb_ctrl bam_type, enum peer_bam client, |
| 3365 | enum usb_bam_pipe_dir dir, enum usb_bam_mode bam_mode, u32 num) |
| 3366 | { |
| 3367 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 3368 | u8 i; |
| 3369 | |
| 3370 | for (i = 0; i < ctx->max_connections; i++) { |
| 3371 | if (ctx->usb_bam_connections[i].peer_bam == client && |
| 3372 | ctx->usb_bam_connections[i].dir == dir && |
| 3373 | ctx->usb_bam_connections[i].bam_mode == bam_mode && |
| 3374 | ctx->usb_bam_connections[i].pipe_num == num) { |
| 3375 | log_event_dbg("%s: index %d was found\n", __func__, i); |
| 3376 | return i; |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | log_event_err("%s: failed for %d\n", __func__, bam_type); |
| 3381 | return -ENODEV; |
| 3382 | } |
| 3383 | EXPORT_SYMBOL(usb_bam_get_connection_idx); |
| 3384 | |
| 3385 | int usb_bam_get_bam_type(const char *core_name) |
| 3386 | { |
| 3387 | int bam_type = get_bam_type_from_core_name(core_name); |
| 3388 | |
| 3389 | if (bam_type < 0 || bam_type >= MAX_BAMS) { |
| 3390 | log_event_err("%s: Invalid bam, type=%d, name=%s\n", |
| 3391 | __func__, bam_type, core_name); |
| 3392 | return -EINVAL; |
| 3393 | } |
| 3394 | |
| 3395 | return bam_type; |
| 3396 | } |
| 3397 | EXPORT_SYMBOL(usb_bam_get_bam_type); |
| 3398 | |
| 3399 | bool msm_usb_bam_enable(enum usb_ctrl bam, bool bam_enable) |
| 3400 | { |
| 3401 | struct msm_usb_bam_platform_data *pdata; |
| 3402 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam]; |
| 3403 | |
| 3404 | if (!ctx->usb_bam_pdev) |
| 3405 | return 0; |
| 3406 | |
| 3407 | pdata = ctx->usb_bam_pdev->dev.platform_data; |
| 3408 | if ((bam != CI_CTRL) || !(bam_enable || |
| 3409 | pdata->enable_hsusb_bam_on_boot)) |
| 3410 | return 0; |
| 3411 | |
| 3412 | msm_hw_bam_disable(1); |
| 3413 | sps_device_reset(ctx->h_bam); |
| 3414 | msm_hw_bam_disable(0); |
| 3415 | |
| 3416 | return 0; |
| 3417 | } |
| 3418 | EXPORT_SYMBOL(msm_usb_bam_enable); |
| 3419 | |
| 3420 | /** |
| 3421 | * msm_bam_hsic_host_pipe_empty - Check all HSIC host BAM pipe state |
| 3422 | * |
| 3423 | * return true if all BAM pipe used for HSIC Host mode is empty. |
| 3424 | */ |
| 3425 | bool msm_bam_hsic_host_pipe_empty(void) |
| 3426 | { |
| 3427 | struct usb_bam_pipe_connect *pipe_connect; |
| 3428 | struct sps_pipe *pipe = NULL; |
| 3429 | enum usb_ctrl bam = HSIC_CTRL; |
| 3430 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam]; |
| 3431 | int i, ret; |
| 3432 | u32 status; |
| 3433 | |
| 3434 | pr_debug("%s: enter\n", __func__); |
| 3435 | |
| 3436 | for (i = 0; i < ctx->max_connections; i++) { |
| 3437 | pipe_connect = &ctx->usb_bam_connections[i]; |
| 3438 | if (pipe_connect->enabled) { |
| 3439 | pipe = ctx->usb_bam_sps.sps_pipes[i]; |
| 3440 | ret = sps_is_pipe_empty(pipe, &status); |
| 3441 | if (ret) { |
| 3442 | log_event_err("%s(): sps_is_pipe_empty() failed\n", |
| 3443 | __func__); |
| 3444 | log_event_err("%s(): SRC index(%d), DEST index(%d):\n", |
| 3445 | __func__, |
| 3446 | pipe_connect->src_pipe_index, |
| 3447 | pipe_connect->dst_pipe_index); |
| 3448 | WARN_ON(1); |
| 3449 | } |
| 3450 | |
| 3451 | if (!status) { |
| 3452 | log_event_err("%s(): pipe is not empty.\n", |
| 3453 | __func__); |
| 3454 | log_event_err("%s(): SRC index(%d), DEST index(%d):\n", |
| 3455 | __func__, |
| 3456 | pipe_connect->src_pipe_index, |
| 3457 | pipe_connect->dst_pipe_index); |
| 3458 | return false; |
| 3459 | } |
| 3460 | |
| 3461 | pr_debug("%s(): SRC index(%d), DEST index(%d):\n", |
| 3462 | __func__, |
| 3463 | pipe_connect->src_pipe_index, |
| 3464 | pipe_connect->dst_pipe_index); |
| 3465 | } |
| 3466 | |
| 3467 | } |
| 3468 | |
| 3469 | if (!pipe) |
| 3470 | log_event_err("%s: Bam %s has no connected pipes\n", __func__, |
| 3471 | bam_enable_strings[bam]); |
| 3472 | |
| 3473 | return true; |
| 3474 | } |
| 3475 | EXPORT_SYMBOL(msm_bam_hsic_host_pipe_empty); |
| 3476 | |
| 3477 | bool msm_bam_hsic_lpm_ok(void) |
| 3478 | { |
| 3479 | log_event_dbg("%s: enter\n", __func__); |
| 3480 | |
| 3481 | if (info[HSIC_CTRL].cur_bam_mode == USB_BAM_HOST) |
| 3482 | return msm_bam_host_lpm_ok(HSIC_CTRL); |
| 3483 | |
| 3484 | /* Runtime PM is used to manage device mode LPM */ |
| 3485 | return 0; |
| 3486 | } |
| 3487 | EXPORT_SYMBOL(msm_bam_hsic_lpm_ok); |
| 3488 | |
| 3489 | static int usb_bam_remove(struct platform_device *pdev) |
| 3490 | { |
| 3491 | struct msm_usb_bam_platform_data *pdata = pdev->dev.platform_data; |
| 3492 | enum usb_ctrl bam_type = pdata->bam_type; |
| 3493 | struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type]; |
| 3494 | |
| 3495 | destroy_workqueue(ctx->usb_bam_wq); |
| 3496 | |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
| 3500 | static const struct of_device_id usb_bam_dt_match[] = { |
| 3501 | { .compatible = "qcom,usb-bam-msm", |
| 3502 | }, |
| 3503 | {} |
| 3504 | }; |
| 3505 | MODULE_DEVICE_TABLE(of, usb_bam_dt_match); |
| 3506 | |
| 3507 | static struct platform_driver usb_bam_driver = { |
| 3508 | .probe = usb_bam_probe, |
| 3509 | .remove = usb_bam_remove, |
| 3510 | .driver = { |
| 3511 | .name = "usb_bam", |
| 3512 | .of_match_table = usb_bam_dt_match, |
| 3513 | }, |
| 3514 | }; |
| 3515 | |
| 3516 | static int __init init(void) |
| 3517 | { |
| 3518 | return platform_driver_register(&usb_bam_driver); |
| 3519 | } |
| 3520 | module_init(init); |
| 3521 | |
| 3522 | static void __exit cleanup(void) |
| 3523 | { |
| 3524 | platform_driver_unregister(&usb_bam_driver); |
| 3525 | } |
| 3526 | module_exit(cleanup); |
| 3527 | |
| 3528 | MODULE_DESCRIPTION("MSM USB BAM DRIVER"); |
| 3529 | MODULE_LICENSE("GPL v2"); |