ipa_main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2018-2022 Linaro Ltd.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/atomic.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/device.h>
  9. #include <linux/bug.h>
  10. #include <linux/io.h>
  11. #include <linux/firmware.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_address.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/qcom_scm.h>
  18. #include <linux/soc/qcom/mdt_loader.h>
  19. #include "ipa.h"
  20. #include "ipa_power.h"
  21. #include "ipa_data.h"
  22. #include "ipa_endpoint.h"
  23. #include "ipa_resource.h"
  24. #include "ipa_cmd.h"
  25. #include "ipa_reg.h"
  26. #include "ipa_mem.h"
  27. #include "ipa_table.h"
  28. #include "ipa_smp2p.h"
  29. #include "ipa_modem.h"
  30. #include "ipa_uc.h"
  31. #include "ipa_interrupt.h"
  32. #include "gsi_trans.h"
  33. #include "ipa_sysfs.h"
  34. /**
  35. * DOC: The IP Accelerator
  36. *
  37. * This driver supports the Qualcomm IP Accelerator (IPA), which is a
  38. * networking component found in many Qualcomm SoCs. The IPA is connected
  39. * to the application processor (AP), but is also connected (and partially
  40. * controlled by) other "execution environments" (EEs), such as a modem.
  41. *
  42. * The IPA is the conduit between the AP and the modem that carries network
  43. * traffic. This driver presents a network interface representing the
  44. * connection of the modem to external (e.g. LTE) networks.
  45. *
  46. * The IPA provides protocol checksum calculation, offloading this work
  47. * from the AP. The IPA offers additional functionality, including routing,
  48. * filtering, and NAT support, but that more advanced functionality is not
  49. * currently supported. Despite that, some resources--including routing
  50. * tables and filter tables--are defined in this driver because they must
  51. * be initialized even when the advanced hardware features are not used.
  52. *
  53. * There are two distinct layers that implement the IPA hardware, and this
  54. * is reflected in the organization of the driver. The generic software
  55. * interface (GSI) is an integral component of the IPA, providing a
  56. * well-defined communication layer between the AP subsystem and the IPA
  57. * core. The GSI implements a set of "channels" used for communication
  58. * between the AP and the IPA.
  59. *
  60. * The IPA layer uses GSI channels to implement its "endpoints". And while
  61. * a GSI channel carries data between the AP and the IPA, a pair of IPA
  62. * endpoints is used to carry traffic between two EEs. Specifically, the main
  63. * modem network interface is implemented by two pairs of endpoints: a TX
  64. * endpoint on the AP coupled with an RX endpoint on the modem; and another
  65. * RX endpoint on the AP receiving data from a TX endpoint on the modem.
  66. */
  67. /* The name of the GSI firmware file relative to /lib/firmware */
  68. #define IPA_FW_PATH_DEFAULT "ipa_fws.mdt"
  69. #define IPA_PAS_ID 15
  70. /* Shift of 19.2 MHz timestamp to achieve lower resolution timestamps */
  71. #define DPL_TIMESTAMP_SHIFT 14 /* ~1.172 kHz, ~853 usec per tick */
  72. #define TAG_TIMESTAMP_SHIFT 14
  73. #define NAT_TIMESTAMP_SHIFT 24 /* ~1.144 Hz, ~874 msec per tick */
  74. /* Divider for 19.2 MHz crystal oscillator clock to get common timer clock */
  75. #define IPA_XO_CLOCK_DIVIDER 192 /* 1 is subtracted where used */
  76. /**
  77. * ipa_setup() - Set up IPA hardware
  78. * @ipa: IPA pointer
  79. *
  80. * Perform initialization that requires issuing immediate commands on
  81. * the command TX endpoint. If the modem is doing GSI firmware load
  82. * and initialization, this function will be called when an SMP2P
  83. * interrupt has been signaled by the modem. Otherwise it will be
  84. * called from ipa_probe() after GSI firmware has been successfully
  85. * loaded, authenticated, and started by Trust Zone.
  86. */
  87. int ipa_setup(struct ipa *ipa)
  88. {
  89. struct ipa_endpoint *exception_endpoint;
  90. struct ipa_endpoint *command_endpoint;
  91. struct device *dev = &ipa->pdev->dev;
  92. int ret;
  93. ret = gsi_setup(&ipa->gsi);
  94. if (ret)
  95. return ret;
  96. ret = ipa_power_setup(ipa);
  97. if (ret)
  98. goto err_gsi_teardown;
  99. ipa_endpoint_setup(ipa);
  100. /* We need to use the AP command TX endpoint to perform other
  101. * initialization, so we enable first.
  102. */
  103. command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
  104. ret = ipa_endpoint_enable_one(command_endpoint);
  105. if (ret)
  106. goto err_endpoint_teardown;
  107. ret = ipa_mem_setup(ipa); /* No matching teardown required */
  108. if (ret)
  109. goto err_command_disable;
  110. ret = ipa_table_setup(ipa); /* No matching teardown required */
  111. if (ret)
  112. goto err_command_disable;
  113. /* Enable the exception handling endpoint, and tell the hardware
  114. * to use it by default.
  115. */
  116. exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
  117. ret = ipa_endpoint_enable_one(exception_endpoint);
  118. if (ret)
  119. goto err_command_disable;
  120. ipa_endpoint_default_route_set(ipa, exception_endpoint->endpoint_id);
  121. /* We're all set. Now prepare for communication with the modem */
  122. ret = ipa_qmi_setup(ipa);
  123. if (ret)
  124. goto err_default_route_clear;
  125. ipa->setup_complete = true;
  126. dev_info(dev, "IPA driver setup completed successfully\n");
  127. return 0;
  128. err_default_route_clear:
  129. ipa_endpoint_default_route_clear(ipa);
  130. ipa_endpoint_disable_one(exception_endpoint);
  131. err_command_disable:
  132. ipa_endpoint_disable_one(command_endpoint);
  133. err_endpoint_teardown:
  134. ipa_endpoint_teardown(ipa);
  135. ipa_power_teardown(ipa);
  136. err_gsi_teardown:
  137. gsi_teardown(&ipa->gsi);
  138. return ret;
  139. }
  140. /**
  141. * ipa_teardown() - Inverse of ipa_setup()
  142. * @ipa: IPA pointer
  143. */
  144. static void ipa_teardown(struct ipa *ipa)
  145. {
  146. struct ipa_endpoint *exception_endpoint;
  147. struct ipa_endpoint *command_endpoint;
  148. /* We're going to tear everything down, as if setup never completed */
  149. ipa->setup_complete = false;
  150. ipa_qmi_teardown(ipa);
  151. ipa_endpoint_default_route_clear(ipa);
  152. exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
  153. ipa_endpoint_disable_one(exception_endpoint);
  154. command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
  155. ipa_endpoint_disable_one(command_endpoint);
  156. ipa_endpoint_teardown(ipa);
  157. ipa_power_teardown(ipa);
  158. gsi_teardown(&ipa->gsi);
  159. }
  160. static void
  161. ipa_hardware_config_bcr(struct ipa *ipa, const struct ipa_data *data)
  162. {
  163. const struct ipa_reg *reg;
  164. u32 val;
  165. /* IPA v4.5+ has no backward compatibility register */
  166. if (ipa->version >= IPA_VERSION_4_5)
  167. return;
  168. reg = ipa_reg(ipa, IPA_BCR);
  169. val = data->backward_compat;
  170. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  171. }
  172. static void ipa_hardware_config_tx(struct ipa *ipa)
  173. {
  174. enum ipa_version version = ipa->version;
  175. const struct ipa_reg *reg;
  176. u32 offset;
  177. u32 val;
  178. if (version <= IPA_VERSION_4_0 || version >= IPA_VERSION_4_5)
  179. return;
  180. /* Disable PA mask to allow HOLB drop */
  181. reg = ipa_reg(ipa, IPA_TX_CFG);
  182. offset = ipa_reg_offset(reg);
  183. val = ioread32(ipa->reg_virt + offset);
  184. val &= ~ipa_reg_bit(reg, PA_MASK_EN);
  185. iowrite32(val, ipa->reg_virt + offset);
  186. }
  187. static void ipa_hardware_config_clkon(struct ipa *ipa)
  188. {
  189. enum ipa_version version = ipa->version;
  190. const struct ipa_reg *reg;
  191. u32 val;
  192. if (version >= IPA_VERSION_4_5)
  193. return;
  194. if (version < IPA_VERSION_4_0 && version != IPA_VERSION_3_1)
  195. return;
  196. /* Implement some hardware workarounds */
  197. reg = ipa_reg(ipa, CLKON_CFG);
  198. if (version == IPA_VERSION_3_1) {
  199. /* Disable MISC clock gating */
  200. val = ipa_reg_bit(reg, CLKON_MISC);
  201. } else { /* IPA v4.0+ */
  202. /* Enable open global clocks in the CLKON configuration */
  203. val = ipa_reg_bit(reg, CLKON_GLOBAL);
  204. val |= ipa_reg_bit(reg, GLOBAL_2X_CLK);
  205. }
  206. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  207. }
  208. /* Configure bus access behavior for IPA components */
  209. static void ipa_hardware_config_comp(struct ipa *ipa)
  210. {
  211. const struct ipa_reg *reg;
  212. u32 offset;
  213. u32 val;
  214. /* Nothing to configure prior to IPA v4.0 */
  215. if (ipa->version < IPA_VERSION_4_0)
  216. return;
  217. reg = ipa_reg(ipa, COMP_CFG);
  218. offset = ipa_reg_offset(reg);
  219. val = ioread32(ipa->reg_virt + offset);
  220. if (ipa->version == IPA_VERSION_4_0) {
  221. val &= ~ipa_reg_bit(reg, IPA_QMB_SELECT_CONS_EN);
  222. val &= ~ipa_reg_bit(reg, IPA_QMB_SELECT_PROD_EN);
  223. val &= ~ipa_reg_bit(reg, IPA_QMB_SELECT_GLOBAL_EN);
  224. } else if (ipa->version < IPA_VERSION_4_5) {
  225. val |= ipa_reg_bit(reg, GSI_MULTI_AXI_MASTERS_DIS);
  226. } else {
  227. /* For IPA v4.5 FULL_FLUSH_WAIT_RS_CLOSURE_EN is 0 */
  228. }
  229. val |= ipa_reg_bit(reg, GSI_MULTI_INORDER_RD_DIS);
  230. val |= ipa_reg_bit(reg, GSI_MULTI_INORDER_WR_DIS);
  231. iowrite32(val, ipa->reg_virt + offset);
  232. }
  233. /* Configure DDR and (possibly) PCIe max read/write QSB values */
  234. static void
  235. ipa_hardware_config_qsb(struct ipa *ipa, const struct ipa_data *data)
  236. {
  237. const struct ipa_qsb_data *data0;
  238. const struct ipa_qsb_data *data1;
  239. const struct ipa_reg *reg;
  240. u32 val;
  241. /* QMB 0 represents DDR; QMB 1 (if present) represents PCIe */
  242. data0 = &data->qsb_data[IPA_QSB_MASTER_DDR];
  243. if (data->qsb_count > 1)
  244. data1 = &data->qsb_data[IPA_QSB_MASTER_PCIE];
  245. /* Max outstanding write accesses for QSB masters */
  246. reg = ipa_reg(ipa, QSB_MAX_WRITES);
  247. val = ipa_reg_encode(reg, GEN_QMB_0_MAX_WRITES, data0->max_writes);
  248. if (data->qsb_count > 1)
  249. val |= ipa_reg_encode(reg, GEN_QMB_1_MAX_WRITES,
  250. data1->max_writes);
  251. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  252. /* Max outstanding read accesses for QSB masters */
  253. reg = ipa_reg(ipa, QSB_MAX_READS);
  254. val = ipa_reg_encode(reg, GEN_QMB_0_MAX_READS, data0->max_reads);
  255. if (ipa->version >= IPA_VERSION_4_0)
  256. val |= ipa_reg_encode(reg, GEN_QMB_0_MAX_READS_BEATS,
  257. data0->max_reads_beats);
  258. if (data->qsb_count > 1) {
  259. val = ipa_reg_encode(reg, GEN_QMB_1_MAX_READS,
  260. data1->max_reads);
  261. if (ipa->version >= IPA_VERSION_4_0)
  262. val |= ipa_reg_encode(reg, GEN_QMB_1_MAX_READS_BEATS,
  263. data1->max_reads_beats);
  264. }
  265. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  266. }
  267. /* The internal inactivity timer clock is used for the aggregation timer */
  268. #define TIMER_FREQUENCY 32000 /* 32 KHz inactivity timer clock */
  269. /* Compute the value to use in the COUNTER_CFG register AGGR_GRANULARITY
  270. * field to represent the given number of microseconds. The value is one
  271. * less than the number of timer ticks in the requested period. 0 is not
  272. * a valid granularity value (so for example @usec must be at least 16 for
  273. * a TIMER_FREQUENCY of 32000).
  274. */
  275. static __always_inline u32 ipa_aggr_granularity_val(u32 usec)
  276. {
  277. return DIV_ROUND_CLOSEST(usec * TIMER_FREQUENCY, USEC_PER_SEC) - 1;
  278. }
  279. /* IPA uses unified Qtime starting at IPA v4.5, implementing various
  280. * timestamps and timers independent of the IPA core clock rate. The
  281. * Qtimer is based on a 56-bit timestamp incremented at each tick of
  282. * a 19.2 MHz SoC crystal oscillator (XO clock).
  283. *
  284. * For IPA timestamps (tag, NAT, data path logging) a lower resolution
  285. * timestamp is achieved by shifting the Qtimer timestamp value right
  286. * some number of bits to produce the low-order bits of the coarser
  287. * granularity timestamp.
  288. *
  289. * For timers, a common timer clock is derived from the XO clock using
  290. * a divider (we use 192, to produce a 100kHz timer clock). From
  291. * this common clock, three "pulse generators" are used to produce
  292. * timer ticks at a configurable frequency. IPA timers (such as
  293. * those used for aggregation or head-of-line block handling) now
  294. * define their period based on one of these pulse generators.
  295. */
  296. static void ipa_qtime_config(struct ipa *ipa)
  297. {
  298. const struct ipa_reg *reg;
  299. u32 offset;
  300. u32 val;
  301. /* Timer clock divider must be disabled when we change the rate */
  302. reg = ipa_reg(ipa, TIMERS_XO_CLK_DIV_CFG);
  303. iowrite32(0, ipa->reg_virt + ipa_reg_offset(reg));
  304. reg = ipa_reg(ipa, QTIME_TIMESTAMP_CFG);
  305. /* Set DPL time stamp resolution to use Qtime (instead of 1 msec) */
  306. val = ipa_reg_encode(reg, DPL_TIMESTAMP_LSB, DPL_TIMESTAMP_SHIFT);
  307. val |= ipa_reg_bit(reg, DPL_TIMESTAMP_SEL);
  308. /* Configure tag and NAT Qtime timestamp resolution as well */
  309. val = ipa_reg_encode(reg, TAG_TIMESTAMP_LSB, TAG_TIMESTAMP_SHIFT);
  310. val = ipa_reg_encode(reg, NAT_TIMESTAMP_LSB, NAT_TIMESTAMP_SHIFT);
  311. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  312. /* Set granularity of pulse generators used for other timers */
  313. reg = ipa_reg(ipa, TIMERS_PULSE_GRAN_CFG);
  314. val = ipa_reg_encode(reg, PULSE_GRAN_0, IPA_GRAN_100_US);
  315. val |= ipa_reg_encode(reg, PULSE_GRAN_1, IPA_GRAN_1_MS);
  316. val |= ipa_reg_encode(reg, PULSE_GRAN_2, IPA_GRAN_1_MS);
  317. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  318. /* Actual divider is 1 more than value supplied here */
  319. reg = ipa_reg(ipa, TIMERS_XO_CLK_DIV_CFG);
  320. offset = ipa_reg_offset(reg);
  321. val = ipa_reg_encode(reg, DIV_VALUE, IPA_XO_CLOCK_DIVIDER - 1);
  322. iowrite32(val, ipa->reg_virt + offset);
  323. /* Divider value is set; re-enable the common timer clock divider */
  324. val |= ipa_reg_bit(reg, DIV_ENABLE);
  325. iowrite32(val, ipa->reg_virt + offset);
  326. }
  327. /* Before IPA v4.5 timing is controlled by a counter register */
  328. static void ipa_hardware_config_counter(struct ipa *ipa)
  329. {
  330. u32 granularity = ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY);
  331. const struct ipa_reg *reg;
  332. u32 val;
  333. reg = ipa_reg(ipa, COUNTER_CFG);
  334. /* If defined, EOT_COAL_GRANULARITY is 0 */
  335. val = ipa_reg_encode(reg, AGGR_GRANULARITY, granularity);
  336. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  337. }
  338. static void ipa_hardware_config_timing(struct ipa *ipa)
  339. {
  340. if (ipa->version < IPA_VERSION_4_5)
  341. ipa_hardware_config_counter(ipa);
  342. else
  343. ipa_qtime_config(ipa);
  344. }
  345. static void ipa_hardware_config_hashing(struct ipa *ipa)
  346. {
  347. const struct ipa_reg *reg;
  348. if (ipa->version != IPA_VERSION_4_2)
  349. return;
  350. /* IPA v4.2 does not support hashed tables, so disable them */
  351. reg = ipa_reg(ipa, FILT_ROUT_HASH_EN);
  352. /* IPV6_ROUTER_HASH, IPV6_FILTER_HASH, IPV4_ROUTER_HASH,
  353. * IPV4_FILTER_HASH are all zero.
  354. */
  355. iowrite32(0, ipa->reg_virt + ipa_reg_offset(reg));
  356. }
  357. static void ipa_idle_indication_cfg(struct ipa *ipa,
  358. u32 enter_idle_debounce_thresh,
  359. bool const_non_idle_enable)
  360. {
  361. const struct ipa_reg *reg;
  362. u32 val;
  363. if (ipa->version < IPA_VERSION_3_5_1)
  364. return;
  365. reg = ipa_reg(ipa, IDLE_INDICATION_CFG);
  366. val = ipa_reg_encode(reg, ENTER_IDLE_DEBOUNCE_THRESH,
  367. enter_idle_debounce_thresh);
  368. if (const_non_idle_enable)
  369. val |= ipa_reg_bit(reg, CONST_NON_IDLE_ENABLE);
  370. iowrite32(val, ipa->reg_virt + ipa_reg_offset(reg));
  371. }
  372. /**
  373. * ipa_hardware_dcd_config() - Enable dynamic clock division on IPA
  374. * @ipa: IPA pointer
  375. *
  376. * Configures when the IPA signals it is idle to the global clock
  377. * controller, which can respond by scaling down the clock to save
  378. * power.
  379. */
  380. static void ipa_hardware_dcd_config(struct ipa *ipa)
  381. {
  382. /* Recommended values for IPA 3.5 and later according to IPA HPG */
  383. ipa_idle_indication_cfg(ipa, 256, false);
  384. }
  385. static void ipa_hardware_dcd_deconfig(struct ipa *ipa)
  386. {
  387. /* Power-on reset values */
  388. ipa_idle_indication_cfg(ipa, 0, true);
  389. }
  390. /**
  391. * ipa_hardware_config() - Primitive hardware initialization
  392. * @ipa: IPA pointer
  393. * @data: IPA configuration data
  394. */
  395. static void ipa_hardware_config(struct ipa *ipa, const struct ipa_data *data)
  396. {
  397. ipa_hardware_config_bcr(ipa, data);
  398. ipa_hardware_config_tx(ipa);
  399. ipa_hardware_config_clkon(ipa);
  400. ipa_hardware_config_comp(ipa);
  401. ipa_hardware_config_qsb(ipa, data);
  402. ipa_hardware_config_timing(ipa);
  403. ipa_hardware_config_hashing(ipa);
  404. ipa_hardware_dcd_config(ipa);
  405. }
  406. /**
  407. * ipa_hardware_deconfig() - Inverse of ipa_hardware_config()
  408. * @ipa: IPA pointer
  409. *
  410. * This restores the power-on reset values (even if they aren't different)
  411. */
  412. static void ipa_hardware_deconfig(struct ipa *ipa)
  413. {
  414. /* Mostly we just leave things as we set them. */
  415. ipa_hardware_dcd_deconfig(ipa);
  416. }
  417. /**
  418. * ipa_config() - Configure IPA hardware
  419. * @ipa: IPA pointer
  420. * @data: IPA configuration data
  421. *
  422. * Perform initialization requiring IPA power to be enabled.
  423. */
  424. static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
  425. {
  426. int ret;
  427. ipa_hardware_config(ipa, data);
  428. ret = ipa_mem_config(ipa);
  429. if (ret)
  430. goto err_hardware_deconfig;
  431. ipa->interrupt = ipa_interrupt_config(ipa);
  432. if (IS_ERR(ipa->interrupt)) {
  433. ret = PTR_ERR(ipa->interrupt);
  434. ipa->interrupt = NULL;
  435. goto err_mem_deconfig;
  436. }
  437. ipa_uc_config(ipa);
  438. ret = ipa_endpoint_config(ipa);
  439. if (ret)
  440. goto err_uc_deconfig;
  441. ipa_table_config(ipa); /* No deconfig required */
  442. /* Assign resource limitation to each group; no deconfig required */
  443. ret = ipa_resource_config(ipa, data->resource_data);
  444. if (ret)
  445. goto err_endpoint_deconfig;
  446. ret = ipa_modem_config(ipa);
  447. if (ret)
  448. goto err_endpoint_deconfig;
  449. return 0;
  450. err_endpoint_deconfig:
  451. ipa_endpoint_deconfig(ipa);
  452. err_uc_deconfig:
  453. ipa_uc_deconfig(ipa);
  454. ipa_interrupt_deconfig(ipa->interrupt);
  455. ipa->interrupt = NULL;
  456. err_mem_deconfig:
  457. ipa_mem_deconfig(ipa);
  458. err_hardware_deconfig:
  459. ipa_hardware_deconfig(ipa);
  460. return ret;
  461. }
  462. /**
  463. * ipa_deconfig() - Inverse of ipa_config()
  464. * @ipa: IPA pointer
  465. */
  466. static void ipa_deconfig(struct ipa *ipa)
  467. {
  468. ipa_modem_deconfig(ipa);
  469. ipa_endpoint_deconfig(ipa);
  470. ipa_uc_deconfig(ipa);
  471. ipa_interrupt_deconfig(ipa->interrupt);
  472. ipa->interrupt = NULL;
  473. ipa_mem_deconfig(ipa);
  474. ipa_hardware_deconfig(ipa);
  475. }
  476. static int ipa_firmware_load(struct device *dev)
  477. {
  478. const struct firmware *fw;
  479. struct device_node *node;
  480. struct resource res;
  481. phys_addr_t phys;
  482. const char *path;
  483. ssize_t size;
  484. void *virt;
  485. int ret;
  486. node = of_parse_phandle(dev->of_node, "memory-region", 0);
  487. if (!node) {
  488. dev_err(dev, "DT error getting \"memory-region\" property\n");
  489. return -EINVAL;
  490. }
  491. ret = of_address_to_resource(node, 0, &res);
  492. of_node_put(node);
  493. if (ret) {
  494. dev_err(dev, "error %d getting \"memory-region\" resource\n",
  495. ret);
  496. return ret;
  497. }
  498. /* Use name from DTB if specified; use default for *any* error */
  499. ret = of_property_read_string(dev->of_node, "firmware-name", &path);
  500. if (ret) {
  501. dev_dbg(dev, "error %d getting \"firmware-name\" resource\n",
  502. ret);
  503. path = IPA_FW_PATH_DEFAULT;
  504. }
  505. ret = request_firmware(&fw, path, dev);
  506. if (ret) {
  507. dev_err(dev, "error %d requesting \"%s\"\n", ret, path);
  508. return ret;
  509. }
  510. phys = res.start;
  511. size = (size_t)resource_size(&res);
  512. virt = memremap(phys, size, MEMREMAP_WC);
  513. if (!virt) {
  514. dev_err(dev, "unable to remap firmware memory\n");
  515. ret = -ENOMEM;
  516. goto out_release_firmware;
  517. }
  518. ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
  519. if (ret)
  520. dev_err(dev, "error %d loading \"%s\"\n", ret, path);
  521. else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID)))
  522. dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
  523. memunmap(virt);
  524. out_release_firmware:
  525. release_firmware(fw);
  526. return ret;
  527. }
  528. static const struct of_device_id ipa_match[] = {
  529. {
  530. .compatible = "qcom,msm8998-ipa",
  531. .data = &ipa_data_v3_1,
  532. },
  533. {
  534. .compatible = "qcom,sdm845-ipa",
  535. .data = &ipa_data_v3_5_1,
  536. },
  537. {
  538. .compatible = "qcom,sc7180-ipa",
  539. .data = &ipa_data_v4_2,
  540. },
  541. {
  542. .compatible = "qcom,sdx55-ipa",
  543. .data = &ipa_data_v4_5,
  544. },
  545. {
  546. .compatible = "qcom,sm8350-ipa",
  547. .data = &ipa_data_v4_9,
  548. },
  549. {
  550. .compatible = "qcom,sc7280-ipa",
  551. .data = &ipa_data_v4_11,
  552. },
  553. { },
  554. };
  555. MODULE_DEVICE_TABLE(of, ipa_match);
  556. /* Check things that can be validated at build time. This just
  557. * groups these things BUILD_BUG_ON() calls don't clutter the rest
  558. * of the code.
  559. * */
  560. static void ipa_validate_build(void)
  561. {
  562. /* At one time we assumed a 64-bit build, allowing some do_div()
  563. * calls to be replaced by simple division or modulo operations.
  564. * We currently only perform divide and modulo operations on u32,
  565. * u16, or size_t objects, and of those only size_t has any chance
  566. * of being a 64-bit value. (It should be guaranteed 32 bits wide
  567. * on a 32-bit build, but there is no harm in verifying that.)
  568. */
  569. BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT) && sizeof(size_t) != 4);
  570. /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */
  571. BUILD_BUG_ON(GSI_EE_AP != 0);
  572. /* There's no point if we have no channels or event rings */
  573. BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX);
  574. BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX);
  575. /* GSI hardware design limits */
  576. BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32);
  577. BUILD_BUG_ON(GSI_EVT_RING_COUNT_MAX > 31);
  578. /* The number of TREs in a transaction is limited by the channel's
  579. * TLV FIFO size. A transaction structure uses 8-bit fields
  580. * to represents the number of TREs it has allocated and used.
  581. */
  582. BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX);
  583. /* This is used as a divisor */
  584. BUILD_BUG_ON(!IPA_AGGR_GRANULARITY);
  585. /* Aggregation granularity value can't be 0, and must fit */
  586. BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY));
  587. }
  588. /**
  589. * ipa_probe() - IPA platform driver probe function
  590. * @pdev: Platform device pointer
  591. *
  592. * Return: 0 if successful, or a negative error code (possibly
  593. * EPROBE_DEFER)
  594. *
  595. * This is the main entry point for the IPA driver. Initialization proceeds
  596. * in several stages:
  597. * - The "init" stage involves activities that can be initialized without
  598. * access to the IPA hardware.
  599. * - The "config" stage requires IPA power to be active so IPA registers
  600. * can be accessed, but does not require the use of IPA immediate commands.
  601. * - The "setup" stage uses IPA immediate commands, and so requires the GSI
  602. * layer to be initialized.
  603. *
  604. * A Boolean Device Tree "modem-init" property determines whether GSI
  605. * initialization will be performed by the AP (Trust Zone) or the modem.
  606. * If the AP does GSI initialization, the setup phase is entered after
  607. * this has completed successfully. Otherwise the modem initializes
  608. * the GSI layer and signals it has finished by sending an SMP2P interrupt
  609. * to the AP; this triggers the start if IPA setup.
  610. */
  611. static int ipa_probe(struct platform_device *pdev)
  612. {
  613. struct device *dev = &pdev->dev;
  614. const struct ipa_data *data;
  615. struct ipa_power *power;
  616. bool modem_init;
  617. struct ipa *ipa;
  618. int ret;
  619. ipa_validate_build();
  620. /* Get configuration data early; needed for power initialization */
  621. data = of_device_get_match_data(dev);
  622. if (!data) {
  623. dev_err(dev, "matched hardware not supported\n");
  624. return -ENODEV;
  625. }
  626. if (!ipa_version_supported(data->version)) {
  627. dev_err(dev, "unsupported IPA version %u\n", data->version);
  628. return -EINVAL;
  629. }
  630. /* If we need Trust Zone, make sure it's available */
  631. modem_init = of_property_read_bool(dev->of_node, "modem-init");
  632. if (!modem_init)
  633. if (!qcom_scm_is_available())
  634. return -EPROBE_DEFER;
  635. /* The clock and interconnects might not be ready when we're
  636. * probed, so might return -EPROBE_DEFER.
  637. */
  638. power = ipa_power_init(dev, data->power_data);
  639. if (IS_ERR(power))
  640. return PTR_ERR(power);
  641. /* No more EPROBE_DEFER. Allocate and initialize the IPA structure */
  642. ipa = kzalloc(sizeof(*ipa), GFP_KERNEL);
  643. if (!ipa) {
  644. ret = -ENOMEM;
  645. goto err_power_exit;
  646. }
  647. ipa->pdev = pdev;
  648. dev_set_drvdata(dev, ipa);
  649. ipa->power = power;
  650. ipa->version = data->version;
  651. init_completion(&ipa->completion);
  652. ret = ipa_reg_init(ipa);
  653. if (ret)
  654. goto err_kfree_ipa;
  655. ret = ipa_mem_init(ipa, data->mem_data);
  656. if (ret)
  657. goto err_reg_exit;
  658. ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count,
  659. data->endpoint_data);
  660. if (ret)
  661. goto err_mem_exit;
  662. /* Result is a non-zero mask of endpoints that support filtering */
  663. ipa->filter_map = ipa_endpoint_init(ipa, data->endpoint_count,
  664. data->endpoint_data);
  665. if (!ipa->filter_map) {
  666. ret = -EINVAL;
  667. goto err_gsi_exit;
  668. }
  669. ret = ipa_table_init(ipa);
  670. if (ret)
  671. goto err_endpoint_exit;
  672. ret = ipa_smp2p_init(ipa, modem_init);
  673. if (ret)
  674. goto err_table_exit;
  675. /* Power needs to be active for config and setup */
  676. ret = pm_runtime_get_sync(dev);
  677. if (WARN_ON(ret < 0))
  678. goto err_power_put;
  679. ret = ipa_config(ipa, data);
  680. if (ret)
  681. goto err_power_put;
  682. dev_info(dev, "IPA driver initialized");
  683. /* If the modem is doing early initialization, it will trigger a
  684. * call to ipa_setup() when it has finished. In that case we're
  685. * done here.
  686. */
  687. if (modem_init)
  688. goto done;
  689. /* Otherwise we need to load the firmware and have Trust Zone validate
  690. * and install it. If that succeeds we can proceed with setup.
  691. */
  692. ret = ipa_firmware_load(dev);
  693. if (ret)
  694. goto err_deconfig;
  695. ret = ipa_setup(ipa);
  696. if (ret)
  697. goto err_deconfig;
  698. done:
  699. pm_runtime_mark_last_busy(dev);
  700. (void)pm_runtime_put_autosuspend(dev);
  701. return 0;
  702. err_deconfig:
  703. ipa_deconfig(ipa);
  704. err_power_put:
  705. pm_runtime_put_noidle(dev);
  706. ipa_smp2p_exit(ipa);
  707. err_table_exit:
  708. ipa_table_exit(ipa);
  709. err_endpoint_exit:
  710. ipa_endpoint_exit(ipa);
  711. err_gsi_exit:
  712. gsi_exit(&ipa->gsi);
  713. err_mem_exit:
  714. ipa_mem_exit(ipa);
  715. err_reg_exit:
  716. ipa_reg_exit(ipa);
  717. err_kfree_ipa:
  718. kfree(ipa);
  719. err_power_exit:
  720. ipa_power_exit(power);
  721. return ret;
  722. }
  723. static int ipa_remove(struct platform_device *pdev)
  724. {
  725. struct ipa *ipa = dev_get_drvdata(&pdev->dev);
  726. struct ipa_power *power = ipa->power;
  727. struct device *dev = &pdev->dev;
  728. int ret;
  729. /* Prevent the modem from triggering a call to ipa_setup(). This
  730. * also ensures a modem-initiated setup that's underway completes.
  731. */
  732. ipa_smp2p_irq_disable_setup(ipa);
  733. ret = pm_runtime_get_sync(dev);
  734. if (WARN_ON(ret < 0))
  735. goto out_power_put;
  736. if (ipa->setup_complete) {
  737. ret = ipa_modem_stop(ipa);
  738. /* If starting or stopping is in progress, try once more */
  739. if (ret == -EBUSY) {
  740. usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
  741. ret = ipa_modem_stop(ipa);
  742. }
  743. if (ret)
  744. return ret;
  745. ipa_teardown(ipa);
  746. }
  747. ipa_deconfig(ipa);
  748. out_power_put:
  749. pm_runtime_put_noidle(dev);
  750. ipa_smp2p_exit(ipa);
  751. ipa_table_exit(ipa);
  752. ipa_endpoint_exit(ipa);
  753. gsi_exit(&ipa->gsi);
  754. ipa_mem_exit(ipa);
  755. ipa_reg_exit(ipa);
  756. kfree(ipa);
  757. ipa_power_exit(power);
  758. dev_info(dev, "IPA driver removed");
  759. return 0;
  760. }
  761. static void ipa_shutdown(struct platform_device *pdev)
  762. {
  763. int ret;
  764. ret = ipa_remove(pdev);
  765. if (ret)
  766. dev_err(&pdev->dev, "shutdown: remove returned %d\n", ret);
  767. }
  768. static const struct attribute_group *ipa_attribute_groups[] = {
  769. &ipa_attribute_group,
  770. &ipa_feature_attribute_group,
  771. &ipa_endpoint_id_attribute_group,
  772. &ipa_modem_attribute_group,
  773. NULL,
  774. };
  775. static struct platform_driver ipa_driver = {
  776. .probe = ipa_probe,
  777. .remove = ipa_remove,
  778. .shutdown = ipa_shutdown,
  779. .driver = {
  780. .name = "ipa",
  781. .pm = &ipa_pm_ops,
  782. .of_match_table = ipa_match,
  783. .dev_groups = ipa_attribute_groups,
  784. },
  785. };
  786. module_platform_driver(ipa_driver);
  787. MODULE_LICENSE("GPL v2");
  788. MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver");