usb_bam.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/panic_notifier.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/msm-sps.h>
  12. #include <linux/sched/clock.h>
  13. #include <linux/usb_bam.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/pm_runtime.h>
  17. #define USB_THRESHOLD 512
  18. #define USB_BAM_MAX_STR_LEN 50
  19. #define DBG_MAX_MSG 512UL
  20. #define DBG_MSG_LEN 160UL
  21. #define TIME_BUF_LEN 17
  22. #define DBG_EVENT_LEN 143
  23. #define LOGLEVEL_NONE 8
  24. #define LOGLEVEL_DEBUG 7
  25. #define LOGLEVEL_ERR 3
  26. #define log_event(log_level, x...) \
  27. do { \
  28. unsigned long flags; \
  29. char *buf; \
  30. if (log_level == LOGLEVEL_DEBUG) \
  31. pr_debug(x); \
  32. else if (log_level == LOGLEVEL_ERR) \
  33. pr_err(x); \
  34. write_lock_irqsave(&usb_bam_dbg.lck, flags); \
  35. buf = usb_bam_dbg.buf[usb_bam_dbg.idx]; \
  36. put_timestamp(buf); \
  37. snprintf(&buf[TIME_BUF_LEN - 1], DBG_EVENT_LEN, x); \
  38. usb_bam_dbg.idx = (usb_bam_dbg.idx + 1) % DBG_MAX_MSG; \
  39. write_unlock_irqrestore(&usb_bam_dbg.lck, flags); \
  40. } while (0)
  41. #define log_event_none(x, ...) log_event(LOGLEVEL_NONE, x, ##__VA_ARGS__)
  42. #define log_event_dbg(x, ...) log_event(LOGLEVEL_DEBUG, x, ##__VA_ARGS__)
  43. #define log_event_err(x, ...) log_event(LOGLEVEL_ERR, x, ##__VA_ARGS__)
  44. enum usb_bam_event_type {
  45. USB_BAM_EVENT_WAKEUP_PIPE = 0, /* Wake a pipe */
  46. USB_BAM_EVENT_WAKEUP, /* Wake a bam (first pipe waked) */
  47. USB_BAM_EVENT_INACTIVITY, /* Inactivity on all pipes */
  48. };
  49. struct usb_bam_sps_type {
  50. struct sps_pipe **sps_pipes;
  51. struct sps_connect *sps_connections;
  52. };
  53. /*
  54. * struct usb_bam_event_info: suspend/resume event information.
  55. * @type: usb bam event type.
  56. * @event: holds event data.
  57. * @callback: suspend/resume callback.
  58. * @param: port num (for suspend) or NULL (for resume).
  59. * @event_w: holds work queue parameters.
  60. */
  61. struct usb_bam_event_info {
  62. enum usb_bam_event_type type;
  63. struct sps_register_event event;
  64. int (*callback)(void *ptr);
  65. void *param;
  66. struct work_struct event_w;
  67. };
  68. /*
  69. * struct usb_bam_pipe_connect: pipe connection information
  70. * between USB/HSIC BAM and another BAM. USB/HSIC BAM can be
  71. * either src BAM or dst BAM
  72. * @name: pipe description.
  73. * @mem_type: type of memory used for BAM FIFOs
  74. * @src_phy_addr: src bam physical address.
  75. * @src_pipe_index: src bam pipe index.
  76. * @dst_phy_addr: dst bam physical address.
  77. * @dst_pipe_index: dst bam pipe index.
  78. * @data_fifo_base_offset: data fifo offset.
  79. * @data_fifo_size: data fifo size.
  80. * @desc_fifo_base_offset: descriptor fifo offset.
  81. * @desc_fifo_size: descriptor fifo size.
  82. * @data_mem_buf: data fifo buffer.
  83. * @desc_mem_buf: descriptor fifo buffer.
  84. * @event: event for wakeup.
  85. * @enabled: true if pipe is enabled.
  86. * @suspended: true if pipe is suspended.
  87. * @cons_stopped: true is pipe has consumer requests stopped.
  88. * @prod_stopped: true if pipe has producer requests stopped.
  89. * @priv: private data to return upon activity_notify
  90. * or inactivity_notify callbacks.
  91. * @activity_notify: callback to invoke on activity on one of the in pipes.
  92. * @inactivity_notify: callback to invoke on inactivity on all pipes.
  93. * @start: callback to invoke to enqueue transfers on a pipe.
  94. * @stop: callback to invoke on dequeue transfers on a pipe.
  95. * @start_stop_param: param for the start/stop callbacks.
  96. */
  97. struct usb_bam_pipe_connect {
  98. const char *name;
  99. u32 pipe_num;
  100. enum usb_pipe_mem_type mem_type;
  101. enum usb_bam_pipe_dir dir;
  102. enum usb_ctrl bam_type;
  103. enum peer_bam peer_bam;
  104. enum usb_bam_pipe_type pipe_type;
  105. u32 src_phy_addr;
  106. u32 src_pipe_index;
  107. u32 dst_phy_addr;
  108. u32 dst_pipe_index;
  109. u32 data_fifo_base_offset;
  110. u32 data_fifo_size;
  111. u32 desc_fifo_base_offset;
  112. u32 desc_fifo_size;
  113. struct sps_mem_buffer data_mem_buf;
  114. struct sps_mem_buffer desc_mem_buf;
  115. struct usb_bam_event_info event;
  116. bool enabled;
  117. bool suspended;
  118. bool cons_stopped;
  119. bool prod_stopped;
  120. void *priv;
  121. int (*activity_notify)(void *priv);
  122. int (*inactivity_notify)(void *priv);
  123. void (*start)(void *ptr, enum usb_bam_pipe_dir);
  124. void (*stop)(void *ptr, enum usb_bam_pipe_dir);
  125. void *start_stop_param;
  126. bool reset_pipe_after_lpm;
  127. };
  128. /**
  129. * struct msm_usb_bam_data: pipe connection information
  130. * between USB/HSIC BAM and another BAM. USB/HSIC BAM can be
  131. * either src BAM or dst BAM
  132. * @usb_bam_num_pipes: max number of pipes to use.
  133. * @active_conn_num: number of active pipe connections.
  134. * @usb_bam_fifo_baseaddr: base address for bam pipe's data and descriptor
  135. * fifos. This can be on chip memory (ocimem) or usb
  136. * private memory.
  137. * @reset_on_connect: BAM must be reset before its first pipe connect
  138. * @reset_on_disconnect: BAM must be reset after its last pipe disconnect
  139. * @disable_clk_gating: Disable clock gating
  140. * @override_threshold: Override the default threshold value for Read/Write
  141. * event generation by the BAM towards another BAM.
  142. * @max_mbps_highspeed: Maximum Mbits per seconds that the USB core
  143. * can work at in bam2bam mode when connected to HS host.
  144. * @max_mbps_superspeed: Maximum Mbits per seconds that the USB core
  145. * can work at in bam2bam mode when connected to SS host.
  146. */
  147. struct msm_usb_bam_data {
  148. u8 max_connections;
  149. int usb_bam_num_pipes;
  150. phys_addr_t usb_bam_fifo_baseaddr;
  151. bool reset_on_connect;
  152. bool reset_on_disconnect;
  153. bool disable_clk_gating;
  154. u32 override_threshold;
  155. u32 max_mbps_highspeed;
  156. u32 max_mbps_superspeed;
  157. enum usb_ctrl bam_type;
  158. };
  159. /*
  160. * struct usb_bam_ctx_type - represents the usb bam driver entity
  161. * @usb_bam_sps: holds the sps pipes the usb bam driver holds
  162. * against the sps driver.
  163. * @usb_bam_pdev: the platform device that represents the usb bam.
  164. * @usb_bam_wq: Worqueue used for managing states of reset against
  165. * a peer bam.
  166. * @max_connections: The maximum number of pipes that are configured
  167. * in the platform data.
  168. * @h_bam: the handle/device of the sps driver.
  169. * @pipes_enabled_per_bam: the number of pipes currently enabled.
  170. * @inactivity_timer_ms: The timeout configuration per each bam for inactivity
  171. * timer feature.
  172. * @is_bam_inactivity: Is there no activity on all pipes belongs to a
  173. * specific bam. (no activity = no data is pulled or pushed
  174. * from/into ones of the pipes).
  175. * @usb_bam_connections: array (allocated on probe) having all BAM connections
  176. * @usb_bam_lock: to protect fields of ctx or usb_bam_connections
  177. */
  178. struct usb_bam_ctx_type {
  179. struct usb_bam_sps_type usb_bam_sps;
  180. struct resource *io_res;
  181. int irq;
  182. struct platform_device *usb_bam_pdev;
  183. struct workqueue_struct *usb_bam_wq;
  184. u8 max_connections;
  185. unsigned long h_bam;
  186. u8 pipes_enabled_per_bam;
  187. u32 inactivity_timer_ms;
  188. bool is_bam_inactivity;
  189. struct usb_bam_pipe_connect *usb_bam_connections;
  190. struct msm_usb_bam_data *usb_bam_data;
  191. spinlock_t usb_bam_lock;
  192. };
  193. static struct usb_bam_ctx_type msm_usb_bam[MAX_BAMS];
  194. /* USB bam type used as a peer of the qdss in bam2bam mode */
  195. static enum usb_ctrl qdss_usb_bam_type;
  196. static int __usb_bam_register_wake_cb(enum usb_ctrl bam_type, int idx,
  197. int (*callback)(void *user),
  198. void *param, bool trigger_cb_per_pipe);
  199. static struct {
  200. char buf[DBG_MAX_MSG][DBG_MSG_LEN]; /* buffer */
  201. unsigned int idx; /* index */
  202. rwlock_t lck; /* lock */
  203. } __maybe_unused usb_bam_dbg = {
  204. .idx = 0,
  205. .lck = __RW_LOCK_UNLOCKED(lck)
  206. };
  207. /*put_timestamp - writes time stamp to buffer */
  208. static void __maybe_unused put_timestamp(char *tbuf)
  209. {
  210. unsigned long long t;
  211. unsigned long nanosec_rem;
  212. t = cpu_clock(smp_processor_id());
  213. nanosec_rem = do_div(t, 1000000000)/1000;
  214. snprintf(tbuf, TIME_BUF_LEN, "[%5lu.%06lu]: ", (unsigned long)t,
  215. nanosec_rem);
  216. }
  217. static inline enum usb_ctrl get_bam_type_from_core_name(const char *name)
  218. {
  219. return USB_CTRL_UNUSED;
  220. }
  221. static void usb_bam_set_inactivity_timer(enum usb_ctrl bam)
  222. {
  223. struct sps_timer_ctrl timer_ctrl;
  224. struct usb_bam_pipe_connect *pipe_connect;
  225. struct sps_pipe *pipe = NULL;
  226. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam];
  227. int i;
  228. /*
  229. * Since we configure global incativity timer for all pipes
  230. * and not per each pipe, it is enough to use some pipe
  231. * handle associated with this bam, so just find the first one.
  232. * This pipe handle is required due to SPS driver API we use below.
  233. */
  234. for (i = 0; i < ctx->max_connections; i++) {
  235. pipe_connect = &ctx->usb_bam_connections[i];
  236. if (pipe_connect->bam_type == bam && pipe_connect->enabled) {
  237. pipe = ctx->usb_bam_sps.sps_pipes[i];
  238. break;
  239. }
  240. }
  241. if (!pipe) {
  242. pr_warn("%s: Bam has no connected pipes\n", __func__);
  243. return;
  244. }
  245. timer_ctrl.op = SPS_TIMER_OP_CONFIG;
  246. timer_ctrl.mode = SPS_TIMER_MODE_ONESHOT;
  247. timer_ctrl.timeout_msec = ctx->inactivity_timer_ms;
  248. sps_timer_ctrl(pipe, &timer_ctrl, NULL);
  249. timer_ctrl.op = SPS_TIMER_OP_RESET;
  250. sps_timer_ctrl(pipe, &timer_ctrl, NULL);
  251. }
  252. static int usb_bam_alloc_buffer(struct usb_bam_pipe_connect *pipe_connect)
  253. {
  254. int ret = 0;
  255. struct usb_bam_ctx_type *ctx = &msm_usb_bam[pipe_connect->bam_type];
  256. struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf);
  257. struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf);
  258. struct device *dev = &ctx->usb_bam_pdev->dev;
  259. struct sg_table data_sgt, desc_sgt;
  260. dma_addr_t data_iova, desc_iova;
  261. pr_debug("%s: data_fifo size:%x desc_fifo_size:%x\n",
  262. __func__, pipe_connect->data_fifo_size,
  263. pipe_connect->desc_fifo_size);
  264. if (dev->parent)
  265. dev = dev->parent;
  266. switch (pipe_connect->mem_type) {
  267. case SPS_PIPE_MEM:
  268. log_event_dbg("%s: USB BAM using SPS pipe memory\n", __func__);
  269. ret = sps_setup_bam2bam_fifo(data_buf,
  270. pipe_connect->data_fifo_base_offset,
  271. pipe_connect->data_fifo_size, 1);
  272. if (ret) {
  273. log_event_err("%s: data fifo setup failure %d\n",
  274. __func__, ret);
  275. goto err_exit;
  276. }
  277. ret = sps_setup_bam2bam_fifo(desc_buf,
  278. pipe_connect->desc_fifo_base_offset,
  279. pipe_connect->desc_fifo_size, 1);
  280. if (ret) {
  281. log_event_err("%s: desc. fifo setup failure %d\n",
  282. __func__, ret);
  283. goto err_exit;
  284. }
  285. break;
  286. case OCI_MEM:
  287. if (pipe_connect->mem_type == OCI_MEM)
  288. log_event_dbg("%s: USB BAM using ocimem\n", __func__);
  289. if (data_buf->base) {
  290. log_event_err("%s: Already allocated OCI Memory\n",
  291. __func__);
  292. break;
  293. }
  294. data_buf->phys_base = pipe_connect->data_fifo_base_offset +
  295. ctx->usb_bam_data->usb_bam_fifo_baseaddr;
  296. data_buf->size = pipe_connect->data_fifo_size;
  297. data_buf->base = ioremap(data_buf->phys_base, data_buf->size);
  298. if (!data_buf->base) {
  299. log_event_err("%s: ioremap failed for data fifo\n",
  300. __func__);
  301. ret = -ENOMEM;
  302. goto err_exit;
  303. }
  304. memset_io(data_buf->base, 0, data_buf->size);
  305. data_buf->iova = dma_map_resource(dev, data_buf->phys_base,
  306. data_buf->size, DMA_BIDIRECTIONAL, 0);
  307. if (dma_mapping_error(dev, data_buf->iova))
  308. log_event_err("%s(): oci_mem: err mapping data_buf\n",
  309. __func__);
  310. log_event_dbg("%s: data_buf:%s virt:%pK, phys:%lx, iova:%lx\n",
  311. __func__, dev_name(dev), data_buf->base,
  312. (unsigned long)data_buf->phys_base, data_buf->iova);
  313. desc_buf->phys_base = pipe_connect->desc_fifo_base_offset +
  314. ctx->usb_bam_data->usb_bam_fifo_baseaddr;
  315. desc_buf->size = pipe_connect->desc_fifo_size;
  316. desc_buf->base = ioremap(desc_buf->phys_base, desc_buf->size);
  317. if (!desc_buf->base) {
  318. log_event_err("%s: ioremap failed for desc fifo\n",
  319. __func__);
  320. iounmap(data_buf->base);
  321. ret = -ENOMEM;
  322. goto err_exit;
  323. }
  324. memset_io(desc_buf->base, 0, desc_buf->size);
  325. desc_buf->iova = dma_map_resource(dev, desc_buf->phys_base,
  326. desc_buf->size,
  327. DMA_BIDIRECTIONAL, 0);
  328. if (dma_mapping_error(dev, desc_buf->iova))
  329. log_event_err("%s(): oci_mem: err mapping desc_buf\n",
  330. __func__);
  331. log_event_dbg("%s: desc_buf:%s virt:%pK, phys:%lx, iova:%lx\n",
  332. __func__, dev_name(dev), desc_buf->base,
  333. (unsigned long)desc_buf->phys_base, desc_buf->iova);
  334. break;
  335. case SYSTEM_MEM:
  336. log_event_dbg("%s: USB BAM using system memory\n", __func__);
  337. if (data_buf->base) {
  338. log_event_err("%s: Already allocated memory\n",
  339. __func__);
  340. break;
  341. }
  342. /* BAM would use system memory, allocate FIFOs */
  343. data_buf->base = dma_alloc_attrs(dev,
  344. pipe_connect->data_fifo_size,
  345. &data_iova, GFP_KERNEL,
  346. DMA_ATTR_FORCE_CONTIGUOUS);
  347. if (!data_buf->base) {
  348. log_event_err("%s: data_fifo: dma_alloc_attr failed\n",
  349. __func__);
  350. ret = -ENOMEM;
  351. goto err_exit;
  352. }
  353. memset(data_buf->base, 0, pipe_connect->data_fifo_size);
  354. data_buf->iova = data_iova;
  355. dma_get_sgtable(dev, &data_sgt, data_buf->base, data_buf->iova,
  356. pipe_connect->data_fifo_size);
  357. data_buf->phys_base = page_to_phys(sg_page(data_sgt.sgl));
  358. sg_free_table(&data_sgt);
  359. log_event_dbg("%s: data_buf:%s virt:%pK, phys:%lx, iova:%lx\n",
  360. __func__, dev_name(dev), data_buf->base,
  361. (unsigned long)data_buf->phys_base, data_buf->iova);
  362. desc_buf->size = pipe_connect->desc_fifo_size;
  363. desc_buf->base = dma_alloc_attrs(dev,
  364. pipe_connect->desc_fifo_size,
  365. &desc_iova, GFP_KERNEL,
  366. DMA_ATTR_FORCE_CONTIGUOUS);
  367. if (!desc_buf->base) {
  368. log_event_err("%s: desc_fifo: dma_alloc_attr failed\n",
  369. __func__);
  370. dma_free_attrs(dev, pipe_connect->data_fifo_size,
  371. data_buf->base, data_buf->iova,
  372. DMA_ATTR_FORCE_CONTIGUOUS);
  373. ret = -ENOMEM;
  374. goto err_exit;
  375. }
  376. memset(desc_buf->base, 0, pipe_connect->desc_fifo_size);
  377. desc_buf->iova = desc_iova;
  378. dma_get_sgtable(dev, &desc_sgt, desc_buf->base, desc_buf->iova,
  379. desc_buf->size);
  380. desc_buf->phys_base = page_to_phys(sg_page(desc_sgt.sgl));
  381. sg_free_table(&desc_sgt);
  382. log_event_dbg("%s: desc_buf:%s virt:%pK, phys:%lx, iova:%lx\n",
  383. __func__, dev_name(dev), desc_buf->base,
  384. (unsigned long)desc_buf->phys_base, desc_buf->iova);
  385. break;
  386. default:
  387. log_event_err("%s: invalid mem type\n", __func__);
  388. ret = -EINVAL;
  389. }
  390. return ret;
  391. err_exit:
  392. return ret;
  393. }
  394. int usb_bam_alloc_fifos(enum usb_ctrl cur_bam, u8 idx)
  395. {
  396. int ret;
  397. struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam];
  398. struct usb_bam_pipe_connect *pipe_connect =
  399. &ctx->usb_bam_connections[idx];
  400. ret = usb_bam_alloc_buffer(pipe_connect);
  401. if (ret) {
  402. log_event_err("%s(): Error(%d) allocating buffer\n",
  403. __func__, ret);
  404. return ret;
  405. }
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(usb_bam_alloc_fifos);
  409. int usb_bam_free_fifos(enum usb_ctrl cur_bam, u8 idx)
  410. {
  411. struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam];
  412. struct usb_bam_pipe_connect *pipe_connect =
  413. &ctx->usb_bam_connections[idx];
  414. struct sps_connect *sps_connection =
  415. &ctx->usb_bam_sps.sps_connections[idx];
  416. struct device *dev = &ctx->usb_bam_pdev->dev;
  417. u32 data_fifo_size;
  418. pr_debug("%s(): data size:%x desc size:%x\n",
  419. __func__, sps_connection->data.size,
  420. sps_connection->desc.size);
  421. if (dev->parent)
  422. dev = dev->parent;
  423. switch (pipe_connect->mem_type) {
  424. case SYSTEM_MEM:
  425. log_event_dbg("%s: Freeing system memory used by PIPE\n",
  426. __func__);
  427. if (sps_connection->data.iova) {
  428. data_fifo_size = sps_connection->data.size;
  429. dma_free_attrs(dev, data_fifo_size,
  430. sps_connection->data.base,
  431. sps_connection->data.iova,
  432. DMA_ATTR_FORCE_CONTIGUOUS);
  433. sps_connection->data.iova = 0;
  434. sps_connection->data.phys_base = 0;
  435. pipe_connect->data_mem_buf.base = NULL;
  436. }
  437. if (sps_connection->desc.iova) {
  438. dma_free_attrs(dev, sps_connection->desc.size,
  439. sps_connection->desc.base,
  440. sps_connection->desc.iova,
  441. DMA_ATTR_FORCE_CONTIGUOUS);
  442. sps_connection->desc.iova = 0;
  443. sps_connection->desc.phys_base = 0;
  444. pipe_connect->desc_mem_buf.base = NULL;
  445. }
  446. break;
  447. case OCI_MEM:
  448. log_event_dbg("Freeing oci memory used by BAM PIPE\n");
  449. if (sps_connection->data.base) {
  450. if (sps_connection->data.iova) {
  451. dma_unmap_resource(dev,
  452. sps_connection->data.iova,
  453. sps_connection->data.size,
  454. DMA_BIDIRECTIONAL, 0);
  455. sps_connection->data.iova = 0;
  456. }
  457. iounmap(sps_connection->data.base);
  458. sps_connection->data.base = NULL;
  459. pipe_connect->data_mem_buf.base = NULL;
  460. }
  461. if (sps_connection->desc.base) {
  462. if (sps_connection->desc.iova) {
  463. dma_unmap_resource(dev,
  464. sps_connection->desc.iova,
  465. sps_connection->desc.size,
  466. DMA_BIDIRECTIONAL, 0);
  467. sps_connection->desc.iova = 0;
  468. }
  469. iounmap(sps_connection->desc.base);
  470. sps_connection->desc.base = NULL;
  471. pipe_connect->desc_mem_buf.base = NULL;
  472. }
  473. break;
  474. case SPS_PIPE_MEM:
  475. log_event_dbg("%s: nothing to be be\n", __func__);
  476. break;
  477. }
  478. return 0;
  479. }
  480. EXPORT_SYMBOL(usb_bam_free_fifos);
  481. static int connect_pipe(enum usb_ctrl cur_bam, u8 idx, u32 *usb_pipe_idx,
  482. unsigned long iova)
  483. {
  484. int ret;
  485. struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam];
  486. struct usb_bam_sps_type usb_bam_sps = ctx->usb_bam_sps;
  487. struct sps_pipe **pipe = &(usb_bam_sps.sps_pipes[idx]);
  488. struct sps_connect *sps_connection = &usb_bam_sps.sps_connections[idx];
  489. struct usb_bam_pipe_connect *pipe_connect =
  490. &ctx->usb_bam_connections[idx];
  491. enum usb_bam_pipe_dir dir = pipe_connect->dir;
  492. struct sps_mem_buffer *data_buf = &(pipe_connect->data_mem_buf);
  493. struct sps_mem_buffer *desc_buf = &(pipe_connect->desc_mem_buf);
  494. *pipe = sps_alloc_endpoint();
  495. if (*pipe == NULL) {
  496. log_event_err("%s: sps_alloc_endpoint failed\n", __func__);
  497. return -ENOMEM;
  498. }
  499. ret = sps_get_config(*pipe, sps_connection);
  500. if (ret) {
  501. log_event_err("%s: tx get config failed %d\n", __func__, ret);
  502. goto free_sps_endpoint;
  503. }
  504. ret = sps_phy2h(pipe_connect->src_phy_addr, &(sps_connection->source));
  505. if (ret) {
  506. log_event_err("%s: sps_phy2h failed (src BAM) %d\n",
  507. __func__, ret);
  508. goto free_sps_endpoint;
  509. }
  510. sps_connection->src_pipe_index = pipe_connect->src_pipe_index;
  511. ret = sps_phy2h(pipe_connect->dst_phy_addr,
  512. &(sps_connection->destination));
  513. if (ret) {
  514. log_event_err("%s: sps_phy2h failed (dst BAM) %d\n",
  515. __func__, ret);
  516. goto free_sps_endpoint;
  517. }
  518. sps_connection->dest_pipe_index = pipe_connect->dst_pipe_index;
  519. if (dir == USB_TO_PEER_PERIPHERAL) {
  520. sps_connection->mode = SPS_MODE_SRC;
  521. *usb_pipe_idx = pipe_connect->src_pipe_index;
  522. sps_connection->dest_iova = iova;
  523. } else {
  524. sps_connection->mode = SPS_MODE_DEST;
  525. *usb_pipe_idx = pipe_connect->dst_pipe_index;
  526. sps_connection->source_iova = iova;
  527. }
  528. sps_connection->data = *data_buf;
  529. sps_connection->desc = *desc_buf;
  530. sps_connection->event_thresh = 16;
  531. sps_connection->options = SPS_O_AUTO_ENABLE;
  532. ret = sps_connect(*pipe, sps_connection);
  533. if (ret < 0) {
  534. log_event_err("%s: sps_connect failed %d\n", __func__, ret);
  535. goto error;
  536. }
  537. return 0;
  538. error:
  539. sps_disconnect(*pipe);
  540. free_sps_endpoint:
  541. sps_free_endpoint(*pipe);
  542. *pipe = NULL;
  543. return ret;
  544. }
  545. static int disconnect_pipe(enum usb_ctrl cur_bam, u8 idx)
  546. {
  547. struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam];
  548. struct sps_pipe *pipe = ctx->usb_bam_sps.sps_pipes[idx];
  549. struct sps_connect *sps_connection =
  550. &ctx->usb_bam_sps.sps_connections[idx];
  551. sps_disconnect(pipe);
  552. sps_free_endpoint(pipe);
  553. ctx->usb_bam_sps.sps_pipes[idx] = NULL;
  554. sps_connection->options &= ~SPS_O_AUTO_ENABLE;
  555. return 0;
  556. }
  557. int get_qdss_bam_info(enum usb_ctrl cur_bam, u8 idx,
  558. phys_addr_t *p_addr, u32 *bam_size)
  559. {
  560. int ret = 0;
  561. struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam];
  562. struct usb_bam_pipe_connect *pipe_connect =
  563. &ctx->usb_bam_connections[idx];
  564. unsigned long peer_bam_handle;
  565. ret = sps_phy2h(pipe_connect->src_phy_addr, &peer_bam_handle);
  566. if (ret) {
  567. log_event_err("%s: sps_phy2h failed (src BAM) %d\n",
  568. __func__, ret);
  569. return ret;
  570. }
  571. ret = sps_get_bam_addr(peer_bam_handle, p_addr, bam_size);
  572. if (ret) {
  573. log_event_err("%s: sps_get_bam_addr failed%d\n",
  574. __func__, ret);
  575. return ret;
  576. }
  577. return 0;
  578. }
  579. EXPORT_SYMBOL(get_qdss_bam_info);
  580. int usb_bam_connect(enum usb_ctrl cur_bam, int idx, u32 *bam_pipe_idx,
  581. unsigned long iova)
  582. {
  583. int ret;
  584. struct usb_bam_ctx_type *ctx = &msm_usb_bam[cur_bam];
  585. struct usb_bam_pipe_connect *pipe_connect =
  586. &ctx->usb_bam_connections[idx];
  587. struct device *bam_dev = &ctx->usb_bam_pdev->dev;
  588. if (pipe_connect->enabled) {
  589. pr_warn("%s: connection %d was already established\n",
  590. __func__, idx);
  591. return 0;
  592. }
  593. if (!bam_pipe_idx) {
  594. log_event_err("%s: invalid bam_pipe_idx\n", __func__);
  595. return -EINVAL;
  596. }
  597. if (idx < 0 || idx > ctx->max_connections) {
  598. log_event_err("idx is wrong %d\n", idx);
  599. return -EINVAL;
  600. }
  601. log_event_dbg("%s: PM Runtime GET %d, count: %d\n",
  602. __func__, idx, get_pm_runtime_counter(bam_dev));
  603. pm_runtime_get_sync(bam_dev);
  604. spin_lock(&ctx->usb_bam_lock);
  605. /* Check if BAM requires RESET before connect and reset of first pipe */
  606. if ((ctx->usb_bam_data->reset_on_connect) &&
  607. (ctx->pipes_enabled_per_bam == 0)) {
  608. spin_unlock(&ctx->usb_bam_lock);
  609. sps_device_reset(ctx->h_bam);
  610. spin_lock(&ctx->usb_bam_lock);
  611. }
  612. spin_unlock(&ctx->usb_bam_lock);
  613. ret = connect_pipe(cur_bam, idx, bam_pipe_idx, iova);
  614. if (ret) {
  615. log_event_err("%s: pipe connection[%d] failure\n",
  616. __func__, idx);
  617. log_event_dbg("%s: err, PM RT PUT %d, count: %d\n",
  618. __func__, idx, get_pm_runtime_counter(bam_dev));
  619. pm_runtime_put_sync(bam_dev);
  620. return ret;
  621. }
  622. log_event_dbg("%s: pipe connection[%d] success\n", __func__, idx);
  623. pipe_connect->enabled = true;
  624. spin_lock(&ctx->usb_bam_lock);
  625. ctx->pipes_enabled_per_bam += 1;
  626. spin_unlock(&ctx->usb_bam_lock);
  627. return 0;
  628. }
  629. EXPORT_SYMBOL(usb_bam_connect);
  630. int usb_bam_get_pipe_type(enum usb_ctrl bam_type, u8 idx,
  631. enum usb_bam_pipe_type *type)
  632. {
  633. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type];
  634. struct usb_bam_pipe_connect *pipe_connect =
  635. &ctx->usb_bam_connections[idx];
  636. if (idx >= ctx->max_connections) {
  637. log_event_err("%s: Invalid connection index\n", __func__);
  638. return -EINVAL;
  639. }
  640. if (!type) {
  641. log_event_err("%s: null pointer provided for type\n", __func__);
  642. return -EINVAL;
  643. }
  644. *type = pipe_connect->pipe_type;
  645. return 0;
  646. }
  647. EXPORT_SYMBOL(usb_bam_get_pipe_type);
  648. static void usb_bam_work(struct work_struct *w)
  649. {
  650. int i;
  651. struct usb_bam_event_info *event_info =
  652. container_of(w, struct usb_bam_event_info, event_w);
  653. struct usb_bam_pipe_connect *pipe_connect =
  654. container_of(event_info, struct usb_bam_pipe_connect, event);
  655. struct usb_bam_ctx_type *ctx = &msm_usb_bam[pipe_connect->bam_type];
  656. struct usb_bam_pipe_connect *pipe_iter;
  657. int (*callback)(void *priv);
  658. void *param = NULL;
  659. switch (event_info->type) {
  660. case USB_BAM_EVENT_WAKEUP:
  661. case USB_BAM_EVENT_WAKEUP_PIPE:
  662. log_event_dbg("%s received USB_BAM_EVENT_WAKEUP\n", __func__);
  663. /* Notify about wakeup / activity of the bam */
  664. if (event_info->callback)
  665. event_info->callback(event_info->param);
  666. /*
  667. * Reset inactivity timer counter if this pipe's bam
  668. * has inactivity timeout.
  669. */
  670. spin_lock(&ctx->usb_bam_lock);
  671. if (ctx->inactivity_timer_ms)
  672. usb_bam_set_inactivity_timer(pipe_connect->bam_type);
  673. spin_unlock(&ctx->usb_bam_lock);
  674. break;
  675. case USB_BAM_EVENT_INACTIVITY:
  676. log_event_dbg("%s received USB_BAM_EVENT_INACTIVITY\n",
  677. __func__);
  678. /*
  679. * Since event info is one structure per pipe, it might be
  680. * overridden when we will register the wakeup events below,
  681. * and still we want ot register the wakeup events before we
  682. * notify on the inactivity in order to identify the next
  683. * activity as soon as possible.
  684. */
  685. callback = event_info->callback;
  686. param = event_info->param;
  687. /*
  688. * Upon inactivity, configure wakeup irq for all pipes
  689. * that are into the usb bam.
  690. */
  691. spin_lock(&ctx->usb_bam_lock);
  692. for (i = 0; i < ctx->max_connections; i++) {
  693. pipe_iter = &ctx->usb_bam_connections[i];
  694. if (pipe_iter->bam_type == pipe_connect->bam_type &&
  695. pipe_iter->dir == PEER_PERIPHERAL_TO_USB &&
  696. pipe_iter->enabled) {
  697. log_event_dbg("%s: Register wakeup on pipe %pK\n",
  698. __func__, pipe_iter);
  699. __usb_bam_register_wake_cb(
  700. pipe_connect->bam_type, i,
  701. pipe_iter->activity_notify,
  702. pipe_iter->priv,
  703. false);
  704. }
  705. }
  706. spin_unlock(&ctx->usb_bam_lock);
  707. /* Notify about the inactivity to the USB class driver */
  708. if (callback)
  709. callback(param);
  710. break;
  711. default:
  712. log_event_err("%s: unknown usb bam event type %d\n", __func__,
  713. event_info->type);
  714. }
  715. }
  716. static void usb_bam_wake_cb(struct sps_event_notify *notify)
  717. {
  718. struct usb_bam_event_info *event_info =
  719. (struct usb_bam_event_info *)notify->user;
  720. struct usb_bam_pipe_connect *pipe_connect =
  721. container_of(event_info,
  722. struct usb_bam_pipe_connect,
  723. event);
  724. enum usb_ctrl bam = pipe_connect->bam_type;
  725. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam];
  726. spin_lock(&ctx->usb_bam_lock);
  727. if (event_info->type == USB_BAM_EVENT_WAKEUP_PIPE)
  728. queue_work(ctx->usb_bam_wq, &event_info->event_w);
  729. else if (event_info->type == USB_BAM_EVENT_WAKEUP &&
  730. ctx->is_bam_inactivity) {
  731. /*
  732. * Sps wake event is per pipe, so usb_bam_wake_cb is
  733. * called per pipe. However, we want to filter the wake
  734. * event to be wake event per all the pipes.
  735. * Therefore, the first pipe that awaked will be considered
  736. * as global bam wake event.
  737. */
  738. ctx->is_bam_inactivity = false;
  739. queue_work(ctx->usb_bam_wq, &event_info->event_w);
  740. }
  741. spin_unlock(&ctx->usb_bam_lock);
  742. }
  743. static int __usb_bam_register_wake_cb(enum usb_ctrl bam_type, int idx,
  744. int (*callback)(void *user), void *param,
  745. bool trigger_cb_per_pipe)
  746. {
  747. struct sps_pipe *pipe;
  748. struct sps_connect *sps_connection;
  749. struct usb_bam_pipe_connect *pipe_connect;
  750. struct usb_bam_event_info *wake_event_info;
  751. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type];
  752. int ret;
  753. if (idx < 0 || idx > ctx->max_connections) {
  754. log_event_err("%s:idx is wrong %d\n", __func__, idx);
  755. return -EINVAL;
  756. }
  757. pipe = ctx->usb_bam_sps.sps_pipes[idx];
  758. sps_connection = &ctx->usb_bam_sps.sps_connections[idx];
  759. pipe_connect = &ctx->usb_bam_connections[idx];
  760. wake_event_info = &pipe_connect->event;
  761. wake_event_info->type = (trigger_cb_per_pipe ?
  762. USB_BAM_EVENT_WAKEUP_PIPE :
  763. USB_BAM_EVENT_WAKEUP);
  764. wake_event_info->param = param;
  765. wake_event_info->callback = callback;
  766. wake_event_info->event.mode = SPS_TRIGGER_CALLBACK;
  767. wake_event_info->event.xfer_done = NULL;
  768. wake_event_info->event.callback = callback ? usb_bam_wake_cb : NULL;
  769. wake_event_info->event.user = wake_event_info;
  770. wake_event_info->event.options = SPS_O_WAKEUP;
  771. ret = sps_register_event(pipe, &wake_event_info->event);
  772. if (ret) {
  773. log_event_err("%s: sps_register_event() failed %d\n",
  774. __func__, ret);
  775. return ret;
  776. }
  777. sps_connection->options = callback ?
  778. (SPS_O_AUTO_ENABLE | SPS_O_WAKEUP | SPS_O_WAKEUP_IS_ONESHOT) :
  779. SPS_O_AUTO_ENABLE;
  780. ret = sps_set_config(pipe, sps_connection);
  781. if (ret) {
  782. log_event_err("%s: sps_set_config() failed %d\n",
  783. __func__, ret);
  784. return ret;
  785. }
  786. log_event_dbg("%s: success\n", __func__);
  787. return 0;
  788. }
  789. int usb_bam_register_wake_cb(enum usb_ctrl bam_type, u8 idx,
  790. int (*callback)(void *user), void *param)
  791. {
  792. return __usb_bam_register_wake_cb(bam_type, idx, callback, param, true);
  793. }
  794. int usb_bam_register_start_stop_cbs(enum usb_ctrl bam_type, u8 dst_idx,
  795. void (*start)(void *, enum usb_bam_pipe_dir),
  796. void (*stop)(void *, enum usb_bam_pipe_dir), void *param)
  797. {
  798. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type];
  799. struct usb_bam_pipe_connect *pipe_connect =
  800. &ctx->usb_bam_connections[dst_idx];
  801. log_event_dbg("%s: Register for %d\n", __func__, dst_idx);
  802. pipe_connect->start = start;
  803. pipe_connect->stop = stop;
  804. pipe_connect->start_stop_param = param;
  805. return 0;
  806. }
  807. int usb_bam_disconnect_pipe(enum usb_ctrl bam_type, u8 idx)
  808. {
  809. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type];
  810. struct usb_bam_pipe_connect *pipe_connect;
  811. struct device *bam_dev = &ctx->usb_bam_pdev->dev;
  812. int ret;
  813. pipe_connect = &ctx->usb_bam_connections[idx];
  814. if (!pipe_connect->enabled) {
  815. log_event_err("%s: connection %d isn't enabled\n",
  816. __func__, idx);
  817. return 0;
  818. }
  819. ret = disconnect_pipe(bam_type, idx);
  820. if (ret) {
  821. log_event_err("%s: src pipe disconnection failure\n", __func__);
  822. return ret;
  823. }
  824. pipe_connect->enabled = false;
  825. spin_lock(&ctx->usb_bam_lock);
  826. if (!ctx->pipes_enabled_per_bam) {
  827. log_event_err("%s: wrong pipes enabled counter for bam_type=%d\n",
  828. __func__, bam_type);
  829. } else {
  830. ctx->pipes_enabled_per_bam -= 1;
  831. }
  832. spin_unlock(&ctx->usb_bam_lock);
  833. log_event_dbg("%s: success disconnecting pipe %d\n", __func__, idx);
  834. if (ctx->usb_bam_data->reset_on_disconnect
  835. && !ctx->pipes_enabled_per_bam)
  836. sps_device_reset(ctx->h_bam);
  837. /* This function is directly called by USB Transport drivers
  838. * to disconnect pipes. Drop runtime usage count here.
  839. */
  840. log_event_dbg("%s: PM Runtime PUT %d, count: %d\n", __func__, idx,
  841. get_pm_runtime_counter(bam_dev));
  842. pm_runtime_put_sync(bam_dev);
  843. return 0;
  844. }
  845. EXPORT_SYMBOL(usb_bam_disconnect_pipe);
  846. static void usb_bam_sps_events(enum sps_callback_case sps_cb_case, void *user)
  847. {
  848. int i;
  849. struct usb_bam_ctx_type *ctx = user;
  850. struct usb_bam_pipe_connect *pipe_connect;
  851. struct usb_bam_event_info *event_info;
  852. switch (sps_cb_case) {
  853. case SPS_CALLBACK_BAM_TIMER_IRQ:
  854. log_event_dbg("%s: received SPS_CALLBACK_BAM_TIMER_IRQ\n",
  855. __func__);
  856. spin_lock(&ctx->usb_bam_lock);
  857. ctx->is_bam_inactivity = true;
  858. for (i = 0; i < ctx->max_connections; i++) {
  859. pipe_connect = &ctx->usb_bam_connections[i];
  860. /*
  861. * Notify inactivity once, Since it is global
  862. * for all pipes on bam. Notify only if we have
  863. * connected pipes.
  864. */
  865. if (pipe_connect->enabled) {
  866. event_info = &pipe_connect->event;
  867. event_info->type = USB_BAM_EVENT_INACTIVITY;
  868. event_info->param = pipe_connect->priv;
  869. event_info->callback =
  870. pipe_connect->inactivity_notify;
  871. queue_work(ctx->usb_bam_wq,
  872. &event_info->event_w);
  873. break;
  874. }
  875. }
  876. spin_unlock(&ctx->usb_bam_lock);
  877. break;
  878. default:
  879. log_event_dbg("%s: received sps_cb_case=%d\n", __func__,
  880. (int)sps_cb_case);
  881. }
  882. }
  883. static struct msm_usb_bam_data *usb_bam_dt_to_data(
  884. struct platform_device *pdev, u32 usb_addr)
  885. {
  886. struct msm_usb_bam_data *usb_bam_data;
  887. struct device_node *node = pdev->dev.of_node;
  888. int rc = 0;
  889. u8 i = 0;
  890. u32 bam = USB_CTRL_UNUSED;
  891. u32 addr = 0;
  892. u32 threshold, max_connections = 0;
  893. static struct usb_bam_pipe_connect *usb_bam_connections;
  894. usb_bam_data = devm_kzalloc(&pdev->dev, sizeof(*usb_bam_data),
  895. GFP_KERNEL);
  896. if (!usb_bam_data)
  897. return NULL;
  898. usb_bam_data->bam_type = bam;
  899. usb_bam_data->reset_on_connect = of_property_read_bool(node,
  900. "qcom,reset-bam-on-connect");
  901. usb_bam_data->reset_on_disconnect = of_property_read_bool(node,
  902. "qcom,reset-bam-on-disconnect");
  903. rc = of_property_read_u32(node, "qcom,usb-bam-num-pipes",
  904. &usb_bam_data->usb_bam_num_pipes);
  905. if (rc) {
  906. log_event_err("Invalid usb bam num pipes property\n");
  907. return NULL;
  908. }
  909. rc = of_property_read_u32(node, "qcom,usb-bam-max-mbps-highspeed",
  910. &usb_bam_data->max_mbps_highspeed);
  911. if (rc)
  912. usb_bam_data->max_mbps_highspeed = 0;
  913. rc = of_property_read_u32(node, "qcom,usb-bam-max-mbps-superspeed",
  914. &usb_bam_data->max_mbps_superspeed);
  915. if (rc)
  916. usb_bam_data->max_mbps_superspeed = 0;
  917. rc = of_property_read_u32(node, "qcom,usb-bam-fifo-baseaddr", &addr);
  918. if (rc)
  919. pr_debug("%s: Invalid usb base address property\n", __func__);
  920. else
  921. usb_bam_data->usb_bam_fifo_baseaddr = addr;
  922. usb_bam_data->disable_clk_gating = of_property_read_bool(node,
  923. "qcom,disable-clk-gating");
  924. rc = of_property_read_u32(node, "qcom,usb-bam-override-threshold",
  925. &threshold);
  926. if (rc)
  927. usb_bam_data->override_threshold = USB_THRESHOLD;
  928. else
  929. usb_bam_data->override_threshold = threshold;
  930. for_each_child_of_node(pdev->dev.of_node, node)
  931. max_connections++;
  932. if (!max_connections) {
  933. log_event_err("%s: error: max_connections is zero\n", __func__);
  934. goto err;
  935. }
  936. usb_bam_connections = devm_kzalloc(&pdev->dev, max_connections *
  937. sizeof(struct usb_bam_pipe_connect), GFP_KERNEL);
  938. if (!usb_bam_connections) {
  939. log_event_err("%s: devm_kzalloc failed(%d)\n",
  940. __func__, __LINE__);
  941. return NULL;
  942. }
  943. /* retrieve device tree parameters */
  944. for_each_child_of_node(pdev->dev.of_node, node) {
  945. usb_bam_connections[i].bam_type = bam;
  946. rc = of_property_read_string(node, "label",
  947. &usb_bam_connections[i].name);
  948. if (rc)
  949. goto err;
  950. rc = of_property_read_u32(node, "qcom,usb-bam-mem-type",
  951. &usb_bam_connections[i].mem_type);
  952. if (rc)
  953. goto err;
  954. if (usb_bam_connections[i].mem_type == OCI_MEM) {
  955. if (!usb_bam_data->usb_bam_fifo_baseaddr) {
  956. log_event_err("%s: base address is missing\n",
  957. __func__);
  958. goto err;
  959. }
  960. }
  961. rc = of_property_read_u32(node, "qcom,peer-bam",
  962. &usb_bam_connections[i].peer_bam);
  963. if (rc) {
  964. log_event_err("%s: peer bam is missing in device tree\n",
  965. __func__);
  966. goto err;
  967. }
  968. /*
  969. * Store USB bam_type to be used with QDSS. As only one device
  970. * bam is currently supported, check the same in DT connections
  971. */
  972. if (usb_bam_connections[i].peer_bam == QDSS_P_BAM) {
  973. if (qdss_usb_bam_type) {
  974. log_event_err("%s: overriding QDSS pipe!, update DT\n",
  975. __func__);
  976. }
  977. qdss_usb_bam_type = usb_bam_connections[i].bam_type;
  978. }
  979. rc = of_property_read_u32(node, "qcom,dir",
  980. &usb_bam_connections[i].dir);
  981. if (rc) {
  982. log_event_err("%s: direction is missing in device tree\n",
  983. __func__);
  984. goto err;
  985. }
  986. rc = of_property_read_u32(node, "qcom,pipe-num",
  987. &usb_bam_connections[i].pipe_num);
  988. if (rc) {
  989. log_event_err("%s: pipe num is missing in device tree\n",
  990. __func__);
  991. goto err;
  992. }
  993. rc = of_property_read_u32(node, "qcom,pipe-connection-type",
  994. &usb_bam_connections[i].pipe_type);
  995. if (rc)
  996. pr_debug("%s: pipe type is defaulting to bam2bam\n",
  997. __func__);
  998. of_property_read_u32(node, "qcom,peer-bam-physical-address",
  999. &addr);
  1000. if (usb_bam_connections[i].dir == USB_TO_PEER_PERIPHERAL) {
  1001. usb_bam_connections[i].src_phy_addr = usb_addr;
  1002. usb_bam_connections[i].dst_phy_addr = addr;
  1003. } else {
  1004. usb_bam_connections[i].src_phy_addr = addr;
  1005. usb_bam_connections[i].dst_phy_addr = usb_addr;
  1006. }
  1007. of_property_read_u32(node, "qcom,src-bam-pipe-index",
  1008. &usb_bam_connections[i].src_pipe_index);
  1009. of_property_read_u32(node, "qcom,dst-bam-pipe-index",
  1010. &usb_bam_connections[i].dst_pipe_index);
  1011. of_property_read_u32(node, "qcom,data-fifo-offset",
  1012. &usb_bam_connections[i].data_fifo_base_offset);
  1013. rc = of_property_read_u32(node, "qcom,data-fifo-size",
  1014. &usb_bam_connections[i].data_fifo_size);
  1015. if (rc)
  1016. goto err;
  1017. of_property_read_u32(node, "qcom,descriptor-fifo-offset",
  1018. &usb_bam_connections[i].desc_fifo_base_offset);
  1019. rc = of_property_read_u32(node, "qcom,descriptor-fifo-size",
  1020. &usb_bam_connections[i].desc_fifo_size);
  1021. if (rc)
  1022. goto err;
  1023. i++;
  1024. }
  1025. msm_usb_bam[bam].usb_bam_connections = usb_bam_connections;
  1026. msm_usb_bam[bam].max_connections = max_connections;
  1027. return usb_bam_data;
  1028. err:
  1029. log_event_err("%s: failed\n", __func__);
  1030. return NULL;
  1031. }
  1032. static int usb_bam_init(struct platform_device *pdev)
  1033. {
  1034. int ret;
  1035. struct usb_bam_ctx_type *ctx = dev_get_drvdata(&pdev->dev);
  1036. enum usb_ctrl bam_type = ctx->usb_bam_data->bam_type;
  1037. struct sps_bam_props props;
  1038. struct device *dev;
  1039. memset(&props, 0, sizeof(props));
  1040. props.phys_addr = ctx->io_res->start;
  1041. props.virt_size = resource_size(ctx->io_res);
  1042. props.irq = ctx->irq;
  1043. props.summing_threshold = ctx->usb_bam_data->override_threshold;
  1044. props.event_threshold = ctx->usb_bam_data->override_threshold;
  1045. props.num_pipes = ctx->usb_bam_data->usb_bam_num_pipes;
  1046. props.callback = usb_bam_sps_events;
  1047. props.user = &msm_usb_bam[bam_type];
  1048. if (ctx->usb_bam_data->disable_clk_gating)
  1049. props.options |= SPS_BAM_NO_LOCAL_CLK_GATING;
  1050. dev = &ctx->usb_bam_pdev->dev;
  1051. if (dev && dev->parent && device_property_present(dev->parent, "iommus")
  1052. && !device_property_present(dev->parent,
  1053. "qcom,smmu-s1-bypass")) {
  1054. pr_info("%s: setting SPS_BAM_SMMU_EN flag with (%s)\n",
  1055. __func__, dev_name(dev));
  1056. props.options |= SPS_BAM_SMMU_EN;
  1057. }
  1058. ret = sps_register_bam_device(&props, &ctx->h_bam);
  1059. if (ret < 0) {
  1060. log_event_err("%s: register bam error %d\n", __func__, ret);
  1061. return -EFAULT;
  1062. }
  1063. return 0;
  1064. }
  1065. static int enable_usb_bam(struct platform_device *pdev)
  1066. {
  1067. int ret;
  1068. struct usb_bam_ctx_type *ctx = dev_get_drvdata(&pdev->dev);
  1069. ret = usb_bam_init(pdev);
  1070. if (ret)
  1071. return ret;
  1072. ctx->usb_bam_sps.sps_pipes = devm_kzalloc(&pdev->dev,
  1073. ctx->max_connections * sizeof(struct sps_pipe *),
  1074. GFP_KERNEL);
  1075. if (!ctx->usb_bam_sps.sps_pipes) {
  1076. log_event_err("%s: failed to allocate sps_pipes\n", __func__);
  1077. return -ENOMEM;
  1078. }
  1079. ctx->usb_bam_sps.sps_connections = devm_kzalloc(&pdev->dev,
  1080. ctx->max_connections * sizeof(struct sps_connect),
  1081. GFP_KERNEL);
  1082. if (!ctx->usb_bam_sps.sps_connections) {
  1083. log_event_err("%s: failed to allocate sps_connections\n",
  1084. __func__);
  1085. return -ENOMEM;
  1086. }
  1087. return 0;
  1088. }
  1089. static int usb_bam_panic_notifier(struct notifier_block *this,
  1090. unsigned long event, void *ptr)
  1091. {
  1092. int i;
  1093. struct usb_bam_ctx_type *ctx;
  1094. for (i = 0; i < MAX_BAMS; i++) {
  1095. ctx = &msm_usb_bam[i];
  1096. if (ctx->h_bam)
  1097. break;
  1098. }
  1099. if (i == MAX_BAMS)
  1100. goto fail;
  1101. if (!ctx->pipes_enabled_per_bam)
  1102. goto fail;
  1103. pr_err("%s: dump usb bam registers here in call back!\n",
  1104. __func__);
  1105. sps_get_bam_debug_info(ctx->h_bam, 93,
  1106. (SPS_BAM_PIPE(0) | SPS_BAM_PIPE(1)), 0, 2);
  1107. fail:
  1108. return NOTIFY_DONE;
  1109. }
  1110. static struct notifier_block usb_bam_panic_blk = {
  1111. .notifier_call = usb_bam_panic_notifier,
  1112. };
  1113. void usb_bam_register_panic_hdlr(void)
  1114. {
  1115. atomic_notifier_chain_register(&panic_notifier_list,
  1116. &usb_bam_panic_blk);
  1117. }
  1118. static void usb_bam_unregister_panic_hdlr(void)
  1119. {
  1120. atomic_notifier_chain_unregister(&panic_notifier_list,
  1121. &usb_bam_panic_blk);
  1122. }
  1123. static int usb_bam_probe(struct platform_device *pdev)
  1124. {
  1125. int ret, i, irq;
  1126. struct resource *io_res;
  1127. enum usb_ctrl bam_type;
  1128. struct usb_bam_ctx_type *ctx;
  1129. struct msm_usb_bam_data *usb_bam_data;
  1130. io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1131. if (!io_res) {
  1132. dev_err(&pdev->dev, "missing BAM memory resource\n");
  1133. return -ENODEV;
  1134. }
  1135. irq = platform_get_irq(pdev, 0);
  1136. if (irq < 0) {
  1137. dev_err(&pdev->dev, "Unable to get IRQ resource\n");
  1138. return irq;
  1139. }
  1140. /* specify BAM physical address to be filled in BAM connections */
  1141. usb_bam_data = usb_bam_dt_to_data(pdev, io_res->start);
  1142. if (!usb_bam_data)
  1143. return -EINVAL;
  1144. bam_type = usb_bam_data->bam_type;
  1145. ctx = &msm_usb_bam[bam_type];
  1146. dev_set_drvdata(&pdev->dev, ctx);
  1147. ctx->usb_bam_pdev = pdev;
  1148. ctx->irq = irq;
  1149. ctx->io_res = io_res;
  1150. ctx->usb_bam_data = usb_bam_data;
  1151. for (i = 0; i < ctx->max_connections; i++) {
  1152. ctx->usb_bam_connections[i].enabled = false;
  1153. INIT_WORK(&ctx->usb_bam_connections[i].event.event_w,
  1154. usb_bam_work);
  1155. }
  1156. ctx->usb_bam_wq = alloc_workqueue("usb_bam_wq",
  1157. WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
  1158. if (!ctx->usb_bam_wq) {
  1159. log_event_err("unable to create workqueue usb_bam_wq\n");
  1160. return -ENOMEM;
  1161. }
  1162. ret = enable_usb_bam(pdev);
  1163. if (ret) {
  1164. destroy_workqueue(ctx->usb_bam_wq);
  1165. return ret;
  1166. }
  1167. pm_runtime_no_callbacks(&pdev->dev);
  1168. pm_runtime_set_active(&pdev->dev);
  1169. pm_runtime_enable(&pdev->dev);
  1170. spin_lock_init(&ctx->usb_bam_lock);
  1171. usb_bam_register_panic_hdlr();
  1172. return ret;
  1173. }
  1174. int get_bam2bam_connection_info(enum usb_ctrl bam_type, u8 idx,
  1175. u32 *usb_bam_pipe_idx, struct sps_mem_buffer *desc_fifo,
  1176. struct sps_mem_buffer *data_fifo, enum usb_pipe_mem_type *mem_type)
  1177. {
  1178. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type];
  1179. struct usb_bam_pipe_connect *pipe_connect =
  1180. &ctx->usb_bam_connections[idx];
  1181. enum usb_bam_pipe_dir dir = pipe_connect->dir;
  1182. if (dir == USB_TO_PEER_PERIPHERAL)
  1183. *usb_bam_pipe_idx = pipe_connect->src_pipe_index;
  1184. else
  1185. *usb_bam_pipe_idx = pipe_connect->dst_pipe_index;
  1186. if (data_fifo)
  1187. memcpy(data_fifo, &pipe_connect->data_mem_buf,
  1188. sizeof(struct sps_mem_buffer));
  1189. if (desc_fifo)
  1190. memcpy(desc_fifo, &pipe_connect->desc_mem_buf,
  1191. sizeof(struct sps_mem_buffer));
  1192. if (mem_type)
  1193. *mem_type = pipe_connect->mem_type;
  1194. return 0;
  1195. }
  1196. EXPORT_SYMBOL(get_bam2bam_connection_info);
  1197. int get_qdss_bam_connection_info(unsigned long *usb_bam_handle,
  1198. u32 *usb_bam_pipe_idx, u32 *peer_pipe_idx,
  1199. struct sps_mem_buffer *desc_fifo, struct sps_mem_buffer *data_fifo,
  1200. enum usb_pipe_mem_type *mem_type)
  1201. {
  1202. u8 idx;
  1203. struct usb_bam_ctx_type *ctx = &msm_usb_bam[qdss_usb_bam_type];
  1204. struct sps_connect *sps_connection;
  1205. /* QDSS uses only one pipe */
  1206. idx = usb_bam_get_connection_idx(qdss_usb_bam_type, QDSS_P_BAM,
  1207. PEER_PERIPHERAL_TO_USB, 0);
  1208. get_bam2bam_connection_info(qdss_usb_bam_type, idx, usb_bam_pipe_idx,
  1209. desc_fifo, data_fifo, mem_type);
  1210. sps_connection = &ctx->usb_bam_sps.sps_connections[idx];
  1211. *usb_bam_handle = sps_connection->destination;
  1212. *peer_pipe_idx = sps_connection->src_pipe_index;
  1213. return 0;
  1214. }
  1215. EXPORT_SYMBOL(get_qdss_bam_connection_info);
  1216. int usb_bam_get_connection_idx(enum usb_ctrl bam_type, enum peer_bam client,
  1217. enum usb_bam_pipe_dir dir, u32 num)
  1218. {
  1219. struct usb_bam_ctx_type *ctx = &msm_usb_bam[bam_type];
  1220. u8 i;
  1221. for (i = 0; i < ctx->max_connections; i++) {
  1222. if (ctx->usb_bam_connections[i].peer_bam == client &&
  1223. ctx->usb_bam_connections[i].dir == dir &&
  1224. ctx->usb_bam_connections[i].pipe_num == num) {
  1225. log_event_dbg("%s: index %d was found\n", __func__, i);
  1226. return i;
  1227. }
  1228. }
  1229. log_event_err("%s: failed for %d\n", __func__, bam_type);
  1230. return -ENODEV;
  1231. }
  1232. EXPORT_SYMBOL(usb_bam_get_connection_idx);
  1233. enum usb_ctrl usb_bam_get_bam_type(const char *core_name)
  1234. {
  1235. enum usb_ctrl bam_type = get_bam_type_from_core_name(core_name);
  1236. if (bam_type < 0 || bam_type >= MAX_BAMS) {
  1237. log_event_err("%s: Invalid bam, type=%d, name=%s\n",
  1238. __func__, bam_type, core_name);
  1239. return -EINVAL;
  1240. }
  1241. return bam_type;
  1242. }
  1243. EXPORT_SYMBOL(usb_bam_get_bam_type);
  1244. static int usb_bam_remove(struct platform_device *pdev)
  1245. {
  1246. struct usb_bam_ctx_type *ctx = dev_get_drvdata(&pdev->dev);
  1247. usb_bam_unregister_panic_hdlr();
  1248. sps_deregister_bam_device(ctx->h_bam);
  1249. destroy_workqueue(ctx->usb_bam_wq);
  1250. return 0;
  1251. }
  1252. static const struct of_device_id usb_bam_dt_match[] = {
  1253. { .compatible = "qcom,usb-bam-msm",
  1254. },
  1255. {}
  1256. };
  1257. MODULE_DEVICE_TABLE(of, usb_bam_dt_match);
  1258. static struct platform_driver usb_bam_driver = {
  1259. .probe = usb_bam_probe,
  1260. .remove = usb_bam_remove,
  1261. .driver = {
  1262. .name = "usb_bam",
  1263. .of_match_table = usb_bam_dt_match,
  1264. },
  1265. };
  1266. static int __init init(void)
  1267. {
  1268. return platform_driver_register(&usb_bam_driver);
  1269. }
  1270. module_init(init);
  1271. static void __exit cleanup(void)
  1272. {
  1273. platform_driver_unregister(&usb_bam_driver);
  1274. }
  1275. module_exit(cleanup);
  1276. MODULE_DESCRIPTION("MSM USB BAM DRIVER");
  1277. MODULE_LICENSE("GPL");