ipa_main.c 28 KB

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