mhi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (c) 2020 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/msi.h>
  7. #include <linux/pci.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/ioport.h>
  11. #include "core.h"
  12. #include "debug.h"
  13. #include "mhi.h"
  14. #include "pci.h"
  15. #include "pcic.h"
  16. #define MHI_TIMEOUT_DEFAULT_MS 20000
  17. #define RDDM_DUMP_SIZE 0x420000
  18. static struct mhi_channel_config ath11k_mhi_channels_qca6390[] = {
  19. {
  20. .num = 0,
  21. .name = "LOOPBACK",
  22. .num_elements = 32,
  23. .event_ring = 0,
  24. .dir = DMA_TO_DEVICE,
  25. .ee_mask = 0x4,
  26. .pollcfg = 0,
  27. .doorbell = MHI_DB_BRST_DISABLE,
  28. .lpm_notify = false,
  29. .offload_channel = false,
  30. .doorbell_mode_switch = false,
  31. .auto_queue = false,
  32. },
  33. {
  34. .num = 1,
  35. .name = "LOOPBACK",
  36. .num_elements = 32,
  37. .event_ring = 0,
  38. .dir = DMA_FROM_DEVICE,
  39. .ee_mask = 0x4,
  40. .pollcfg = 0,
  41. .doorbell = MHI_DB_BRST_DISABLE,
  42. .lpm_notify = false,
  43. .offload_channel = false,
  44. .doorbell_mode_switch = false,
  45. .auto_queue = false,
  46. },
  47. {
  48. .num = 20,
  49. .name = "IPCR",
  50. .num_elements = 64,
  51. .event_ring = 1,
  52. .dir = DMA_TO_DEVICE,
  53. .ee_mask = 0x4,
  54. .pollcfg = 0,
  55. .doorbell = MHI_DB_BRST_DISABLE,
  56. .lpm_notify = false,
  57. .offload_channel = false,
  58. .doorbell_mode_switch = false,
  59. .auto_queue = false,
  60. },
  61. {
  62. .num = 21,
  63. .name = "IPCR",
  64. .num_elements = 64,
  65. .event_ring = 1,
  66. .dir = DMA_FROM_DEVICE,
  67. .ee_mask = 0x4,
  68. .pollcfg = 0,
  69. .doorbell = MHI_DB_BRST_DISABLE,
  70. .lpm_notify = false,
  71. .offload_channel = false,
  72. .doorbell_mode_switch = false,
  73. .auto_queue = true,
  74. },
  75. };
  76. static struct mhi_event_config ath11k_mhi_events_qca6390[] = {
  77. {
  78. .num_elements = 32,
  79. .irq_moderation_ms = 0,
  80. .irq = 1,
  81. .mode = MHI_DB_BRST_DISABLE,
  82. .data_type = MHI_ER_CTRL,
  83. .hardware_event = false,
  84. .client_managed = false,
  85. .offload_channel = false,
  86. },
  87. {
  88. .num_elements = 256,
  89. .irq_moderation_ms = 1,
  90. .irq = 2,
  91. .mode = MHI_DB_BRST_DISABLE,
  92. .priority = 1,
  93. .hardware_event = false,
  94. .client_managed = false,
  95. .offload_channel = false,
  96. },
  97. };
  98. static struct mhi_controller_config ath11k_mhi_config_qca6390 = {
  99. .max_channels = 128,
  100. .timeout_ms = 2000,
  101. .use_bounce_buf = false,
  102. .buf_len = 0,
  103. .num_channels = ARRAY_SIZE(ath11k_mhi_channels_qca6390),
  104. .ch_cfg = ath11k_mhi_channels_qca6390,
  105. .num_events = ARRAY_SIZE(ath11k_mhi_events_qca6390),
  106. .event_cfg = ath11k_mhi_events_qca6390,
  107. };
  108. static struct mhi_channel_config ath11k_mhi_channels_qcn9074[] = {
  109. {
  110. .num = 0,
  111. .name = "LOOPBACK",
  112. .num_elements = 32,
  113. .event_ring = 1,
  114. .dir = DMA_TO_DEVICE,
  115. .ee_mask = 0x14,
  116. .pollcfg = 0,
  117. .doorbell = MHI_DB_BRST_DISABLE,
  118. .lpm_notify = false,
  119. .offload_channel = false,
  120. .doorbell_mode_switch = false,
  121. .auto_queue = false,
  122. },
  123. {
  124. .num = 1,
  125. .name = "LOOPBACK",
  126. .num_elements = 32,
  127. .event_ring = 1,
  128. .dir = DMA_FROM_DEVICE,
  129. .ee_mask = 0x14,
  130. .pollcfg = 0,
  131. .doorbell = MHI_DB_BRST_DISABLE,
  132. .lpm_notify = false,
  133. .offload_channel = false,
  134. .doorbell_mode_switch = false,
  135. .auto_queue = false,
  136. },
  137. {
  138. .num = 20,
  139. .name = "IPCR",
  140. .num_elements = 32,
  141. .event_ring = 1,
  142. .dir = DMA_TO_DEVICE,
  143. .ee_mask = 0x14,
  144. .pollcfg = 0,
  145. .doorbell = MHI_DB_BRST_DISABLE,
  146. .lpm_notify = false,
  147. .offload_channel = false,
  148. .doorbell_mode_switch = false,
  149. .auto_queue = false,
  150. },
  151. {
  152. .num = 21,
  153. .name = "IPCR",
  154. .num_elements = 32,
  155. .event_ring = 1,
  156. .dir = DMA_FROM_DEVICE,
  157. .ee_mask = 0x14,
  158. .pollcfg = 0,
  159. .doorbell = MHI_DB_BRST_DISABLE,
  160. .lpm_notify = false,
  161. .offload_channel = false,
  162. .doorbell_mode_switch = false,
  163. .auto_queue = true,
  164. },
  165. };
  166. static struct mhi_event_config ath11k_mhi_events_qcn9074[] = {
  167. {
  168. .num_elements = 32,
  169. .irq_moderation_ms = 0,
  170. .irq = 1,
  171. .data_type = MHI_ER_CTRL,
  172. .mode = MHI_DB_BRST_DISABLE,
  173. .hardware_event = false,
  174. .client_managed = false,
  175. .offload_channel = false,
  176. },
  177. {
  178. .num_elements = 256,
  179. .irq_moderation_ms = 1,
  180. .irq = 2,
  181. .mode = MHI_DB_BRST_DISABLE,
  182. .priority = 1,
  183. .hardware_event = false,
  184. .client_managed = false,
  185. .offload_channel = false,
  186. },
  187. };
  188. static struct mhi_controller_config ath11k_mhi_config_qcn9074 = {
  189. .max_channels = 30,
  190. .timeout_ms = 10000,
  191. .use_bounce_buf = false,
  192. .buf_len = 0,
  193. .num_channels = ARRAY_SIZE(ath11k_mhi_channels_qcn9074),
  194. .ch_cfg = ath11k_mhi_channels_qcn9074,
  195. .num_events = ARRAY_SIZE(ath11k_mhi_events_qcn9074),
  196. .event_cfg = ath11k_mhi_events_qcn9074,
  197. };
  198. void ath11k_mhi_set_mhictrl_reset(struct ath11k_base *ab)
  199. {
  200. u32 val;
  201. val = ath11k_pcic_read32(ab, MHISTATUS);
  202. ath11k_dbg(ab, ATH11K_DBG_PCI, "MHISTATUS 0x%x\n", val);
  203. /* Observed on QCA6390 that after SOC_GLOBAL_RESET, MHISTATUS
  204. * has SYSERR bit set and thus need to set MHICTRL_RESET
  205. * to clear SYSERR.
  206. */
  207. ath11k_pcic_write32(ab, MHICTRL, MHICTRL_RESET_MASK);
  208. mdelay(10);
  209. }
  210. static void ath11k_mhi_reset_txvecdb(struct ath11k_base *ab)
  211. {
  212. ath11k_pcic_write32(ab, PCIE_TXVECDB, 0);
  213. }
  214. static void ath11k_mhi_reset_txvecstatus(struct ath11k_base *ab)
  215. {
  216. ath11k_pcic_write32(ab, PCIE_TXVECSTATUS, 0);
  217. }
  218. static void ath11k_mhi_reset_rxvecdb(struct ath11k_base *ab)
  219. {
  220. ath11k_pcic_write32(ab, PCIE_RXVECDB, 0);
  221. }
  222. static void ath11k_mhi_reset_rxvecstatus(struct ath11k_base *ab)
  223. {
  224. ath11k_pcic_write32(ab, PCIE_RXVECSTATUS, 0);
  225. }
  226. void ath11k_mhi_clear_vector(struct ath11k_base *ab)
  227. {
  228. ath11k_mhi_reset_txvecdb(ab);
  229. ath11k_mhi_reset_txvecstatus(ab);
  230. ath11k_mhi_reset_rxvecdb(ab);
  231. ath11k_mhi_reset_rxvecstatus(ab);
  232. }
  233. static int ath11k_mhi_get_msi(struct ath11k_pci *ab_pci)
  234. {
  235. struct ath11k_base *ab = ab_pci->ab;
  236. u32 user_base_data, base_vector;
  237. int ret, num_vectors, i;
  238. int *irq;
  239. unsigned int msi_data;
  240. ret = ath11k_pcic_get_user_msi_assignment(ab, "MHI", &num_vectors,
  241. &user_base_data, &base_vector);
  242. if (ret)
  243. return ret;
  244. ath11k_dbg(ab, ATH11K_DBG_PCI, "Number of assigned MSI for MHI is %d, base vector is %d\n",
  245. num_vectors, base_vector);
  246. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  247. if (!irq)
  248. return -ENOMEM;
  249. for (i = 0; i < num_vectors; i++) {
  250. msi_data = base_vector;
  251. if (test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
  252. msi_data += i;
  253. irq[i] = ath11k_pci_get_msi_irq(ab, msi_data);
  254. }
  255. ab_pci->mhi_ctrl->irq = irq;
  256. ab_pci->mhi_ctrl->nr_irqs = num_vectors;
  257. return 0;
  258. }
  259. static int ath11k_mhi_op_runtime_get(struct mhi_controller *mhi_cntrl)
  260. {
  261. return 0;
  262. }
  263. static void ath11k_mhi_op_runtime_put(struct mhi_controller *mhi_cntrl)
  264. {
  265. }
  266. static char *ath11k_mhi_op_callback_to_str(enum mhi_callback reason)
  267. {
  268. switch (reason) {
  269. case MHI_CB_IDLE:
  270. return "MHI_CB_IDLE";
  271. case MHI_CB_PENDING_DATA:
  272. return "MHI_CB_PENDING_DATA";
  273. case MHI_CB_LPM_ENTER:
  274. return "MHI_CB_LPM_ENTER";
  275. case MHI_CB_LPM_EXIT:
  276. return "MHI_CB_LPM_EXIT";
  277. case MHI_CB_EE_RDDM:
  278. return "MHI_CB_EE_RDDM";
  279. case MHI_CB_EE_MISSION_MODE:
  280. return "MHI_CB_EE_MISSION_MODE";
  281. case MHI_CB_SYS_ERROR:
  282. return "MHI_CB_SYS_ERROR";
  283. case MHI_CB_FATAL_ERROR:
  284. return "MHI_CB_FATAL_ERROR";
  285. case MHI_CB_BW_REQ:
  286. return "MHI_CB_BW_REQ";
  287. default:
  288. return "UNKNOWN";
  289. }
  290. };
  291. static void ath11k_mhi_op_status_cb(struct mhi_controller *mhi_cntrl,
  292. enum mhi_callback cb)
  293. {
  294. struct ath11k_base *ab = dev_get_drvdata(mhi_cntrl->cntrl_dev);
  295. ath11k_dbg(ab, ATH11K_DBG_BOOT, "mhi notify status reason %s\n",
  296. ath11k_mhi_op_callback_to_str(cb));
  297. switch (cb) {
  298. case MHI_CB_SYS_ERROR:
  299. ath11k_warn(ab, "firmware crashed: MHI_CB_SYS_ERROR\n");
  300. break;
  301. case MHI_CB_EE_RDDM:
  302. if (!(test_bit(ATH11K_FLAG_UNREGISTERING, &ab->dev_flags)))
  303. queue_work(ab->workqueue_aux, &ab->reset_work);
  304. break;
  305. default:
  306. break;
  307. }
  308. }
  309. static int ath11k_mhi_op_read_reg(struct mhi_controller *mhi_cntrl,
  310. void __iomem *addr,
  311. u32 *out)
  312. {
  313. *out = readl(addr);
  314. return 0;
  315. }
  316. static void ath11k_mhi_op_write_reg(struct mhi_controller *mhi_cntrl,
  317. void __iomem *addr,
  318. u32 val)
  319. {
  320. writel(val, addr);
  321. }
  322. static int ath11k_mhi_read_addr_from_dt(struct mhi_controller *mhi_ctrl)
  323. {
  324. struct device_node *np;
  325. struct resource res;
  326. int ret;
  327. np = of_find_node_by_type(NULL, "memory");
  328. if (!np)
  329. return -ENOENT;
  330. ret = of_address_to_resource(np, 0, &res);
  331. of_node_put(np);
  332. if (ret)
  333. return ret;
  334. mhi_ctrl->iova_start = res.start + 0x1000000;
  335. mhi_ctrl->iova_stop = res.end;
  336. return 0;
  337. }
  338. int ath11k_mhi_register(struct ath11k_pci *ab_pci)
  339. {
  340. struct ath11k_base *ab = ab_pci->ab;
  341. struct mhi_controller *mhi_ctrl;
  342. struct mhi_controller_config *ath11k_mhi_config;
  343. int ret;
  344. mhi_ctrl = mhi_alloc_controller();
  345. if (!mhi_ctrl)
  346. return -ENOMEM;
  347. ath11k_core_create_firmware_path(ab, ATH11K_AMSS_FILE,
  348. ab_pci->amss_path,
  349. sizeof(ab_pci->amss_path));
  350. ab_pci->mhi_ctrl = mhi_ctrl;
  351. mhi_ctrl->cntrl_dev = ab->dev;
  352. mhi_ctrl->fw_image = ab_pci->amss_path;
  353. mhi_ctrl->regs = ab->mem;
  354. mhi_ctrl->reg_len = ab->mem_len;
  355. ret = ath11k_mhi_get_msi(ab_pci);
  356. if (ret) {
  357. ath11k_err(ab, "failed to get msi for mhi\n");
  358. goto free_controller;
  359. }
  360. if (!test_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags))
  361. mhi_ctrl->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
  362. if (test_bit(ATH11K_FLAG_FIXED_MEM_RGN, &ab->dev_flags)) {
  363. ret = ath11k_mhi_read_addr_from_dt(mhi_ctrl);
  364. if (ret < 0)
  365. goto free_controller;
  366. } else {
  367. mhi_ctrl->iova_start = 0;
  368. mhi_ctrl->iova_stop = 0xFFFFFFFF;
  369. }
  370. mhi_ctrl->rddm_size = RDDM_DUMP_SIZE;
  371. mhi_ctrl->sbl_size = SZ_512K;
  372. mhi_ctrl->seg_len = SZ_512K;
  373. mhi_ctrl->fbc_download = true;
  374. mhi_ctrl->runtime_get = ath11k_mhi_op_runtime_get;
  375. mhi_ctrl->runtime_put = ath11k_mhi_op_runtime_put;
  376. mhi_ctrl->status_cb = ath11k_mhi_op_status_cb;
  377. mhi_ctrl->read_reg = ath11k_mhi_op_read_reg;
  378. mhi_ctrl->write_reg = ath11k_mhi_op_write_reg;
  379. switch (ab->hw_rev) {
  380. case ATH11K_HW_QCN9074_HW10:
  381. ath11k_mhi_config = &ath11k_mhi_config_qcn9074;
  382. break;
  383. case ATH11K_HW_QCA6390_HW20:
  384. case ATH11K_HW_WCN6855_HW20:
  385. case ATH11K_HW_WCN6855_HW21:
  386. ath11k_mhi_config = &ath11k_mhi_config_qca6390;
  387. break;
  388. default:
  389. ath11k_err(ab, "failed assign mhi_config for unknown hw rev %d\n",
  390. ab->hw_rev);
  391. ret = -EINVAL;
  392. goto free_controller;
  393. }
  394. ret = mhi_register_controller(mhi_ctrl, ath11k_mhi_config);
  395. if (ret) {
  396. ath11k_err(ab, "failed to register to mhi bus, err = %d\n", ret);
  397. goto free_controller;
  398. }
  399. return 0;
  400. free_controller:
  401. mhi_free_controller(mhi_ctrl);
  402. ab_pci->mhi_ctrl = NULL;
  403. return ret;
  404. }
  405. void ath11k_mhi_unregister(struct ath11k_pci *ab_pci)
  406. {
  407. struct mhi_controller *mhi_ctrl = ab_pci->mhi_ctrl;
  408. mhi_unregister_controller(mhi_ctrl);
  409. kfree(mhi_ctrl->irq);
  410. mhi_free_controller(mhi_ctrl);
  411. }
  412. int ath11k_mhi_start(struct ath11k_pci *ab_pci)
  413. {
  414. struct ath11k_base *ab = ab_pci->ab;
  415. int ret;
  416. ab_pci->mhi_ctrl->timeout_ms = MHI_TIMEOUT_DEFAULT_MS;
  417. ret = mhi_prepare_for_power_up(ab_pci->mhi_ctrl);
  418. if (ret) {
  419. ath11k_warn(ab, "failed to prepare mhi: %d", ret);
  420. return ret;
  421. }
  422. ret = mhi_sync_power_up(ab_pci->mhi_ctrl);
  423. if (ret) {
  424. ath11k_warn(ab, "failed to power up mhi: %d", ret);
  425. return ret;
  426. }
  427. return 0;
  428. }
  429. void ath11k_mhi_stop(struct ath11k_pci *ab_pci)
  430. {
  431. mhi_power_down(ab_pci->mhi_ctrl, true);
  432. mhi_unprepare_after_power_down(ab_pci->mhi_ctrl);
  433. }
  434. int ath11k_mhi_suspend(struct ath11k_pci *ab_pci)
  435. {
  436. struct ath11k_base *ab = ab_pci->ab;
  437. int ret;
  438. ret = mhi_pm_suspend(ab_pci->mhi_ctrl);
  439. if (ret) {
  440. ath11k_warn(ab, "failed to suspend mhi: %d", ret);
  441. return ret;
  442. }
  443. return 0;
  444. }
  445. int ath11k_mhi_resume(struct ath11k_pci *ab_pci)
  446. {
  447. struct ath11k_base *ab = ab_pci->ab;
  448. int ret;
  449. /* Do force MHI resume as some devices like QCA6390, WCN6855
  450. * are not in M3 state but they are functional. So just ignore
  451. * the MHI state while resuming.
  452. */
  453. ret = mhi_pm_resume_force(ab_pci->mhi_ctrl);
  454. if (ret) {
  455. ath11k_warn(ab, "failed to resume mhi: %d", ret);
  456. return ret;
  457. }
  458. return 0;
  459. }