ipa_main.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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_reserved_mem.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 resource res;
  493. phys_addr_t phys;
  494. const char *path;
  495. ssize_t size;
  496. void *virt;
  497. int ret;
  498. ret = of_reserved_mem_region_to_resource(dev->of_node, 0, &res);
  499. if (ret) {
  500. dev_err(dev, "error %d getting \"memory-region\" resource\n",
  501. ret);
  502. return ret;
  503. }
  504. /* Use name from DTB if specified; use default for *any* error */
  505. ret = of_property_read_string(dev->of_node, "firmware-name", &path);
  506. if (ret) {
  507. dev_dbg(dev, "error %d getting \"firmware-name\" resource\n",
  508. ret);
  509. path = IPA_FW_PATH_DEFAULT;
  510. }
  511. ret = request_firmware(&fw, path, dev);
  512. if (ret) {
  513. dev_err(dev, "error %d requesting \"%s\"\n", ret, path);
  514. return ret;
  515. }
  516. phys = res.start;
  517. size = (size_t)resource_size(&res);
  518. virt = memremap(phys, size, MEMREMAP_WC);
  519. if (!virt) {
  520. dev_err(dev, "unable to remap firmware memory\n");
  521. ret = -ENOMEM;
  522. goto out_release_firmware;
  523. }
  524. ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
  525. if (ret)
  526. dev_err(dev, "error %d loading \"%s\"\n", ret, path);
  527. else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID)))
  528. dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
  529. memunmap(virt);
  530. out_release_firmware:
  531. release_firmware(fw);
  532. return ret;
  533. }
  534. static const struct of_device_id ipa_match[] = {
  535. {
  536. .compatible = "qcom,msm8998-ipa",
  537. .data = &ipa_data_v3_1,
  538. },
  539. {
  540. .compatible = "qcom,sdm845-ipa",
  541. .data = &ipa_data_v3_5_1,
  542. },
  543. {
  544. .compatible = "qcom,sc7180-ipa",
  545. .data = &ipa_data_v4_2,
  546. },
  547. {
  548. .compatible = "qcom,sdx55-ipa",
  549. .data = &ipa_data_v4_5,
  550. },
  551. {
  552. .compatible = "qcom,sm6350-ipa",
  553. .data = &ipa_data_v4_7,
  554. },
  555. {
  556. .compatible = "qcom,sm8350-ipa",
  557. .data = &ipa_data_v4_9,
  558. },
  559. {
  560. .compatible = "qcom,sc7280-ipa",
  561. .data = &ipa_data_v4_11,
  562. },
  563. {
  564. .compatible = "qcom,sdx65-ipa",
  565. .data = &ipa_data_v5_0,
  566. },
  567. {
  568. .compatible = "qcom,sm8550-ipa",
  569. .data = &ipa_data_v5_5,
  570. },
  571. { },
  572. };
  573. MODULE_DEVICE_TABLE(of, ipa_match);
  574. /* Check things that can be validated at build time. This just
  575. * groups these things BUILD_BUG_ON() calls don't clutter the rest
  576. * of the code.
  577. * */
  578. static void ipa_validate_build(void)
  579. {
  580. /* At one time we assumed a 64-bit build, allowing some do_div()
  581. * calls to be replaced by simple division or modulo operations.
  582. * We currently only perform divide and modulo operations on u32,
  583. * u16, or size_t objects, and of those only size_t has any chance
  584. * of being a 64-bit value. (It should be guaranteed 32 bits wide
  585. * on a 32-bit build, but there is no harm in verifying that.)
  586. */
  587. BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT) && sizeof(size_t) != 4);
  588. /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */
  589. BUILD_BUG_ON(GSI_EE_AP != 0);
  590. /* There's no point if we have no channels or event rings */
  591. BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX);
  592. BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX);
  593. /* GSI hardware design limits */
  594. BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32);
  595. BUILD_BUG_ON(GSI_EVT_RING_COUNT_MAX > 31);
  596. /* The number of TREs in a transaction is limited by the channel's
  597. * TLV FIFO size. A transaction structure uses 8-bit fields
  598. * to represents the number of TREs it has allocated and used.
  599. */
  600. BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX);
  601. /* This is used as a divisor */
  602. BUILD_BUG_ON(!IPA_AGGR_GRANULARITY);
  603. /* Aggregation granularity value can't be 0, and must fit */
  604. BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY));
  605. }
  606. static enum ipa_firmware_loader ipa_firmware_loader(struct device *dev)
  607. {
  608. bool modem_init;
  609. const char *str;
  610. int ret;
  611. /* Look up the old and new properties by name */
  612. modem_init = of_property_read_bool(dev->of_node, "modem-init");
  613. ret = of_property_read_string(dev->of_node, "qcom,gsi-loader", &str);
  614. /* If the new property doesn't exist, it's legacy behavior */
  615. if (ret == -EINVAL) {
  616. if (modem_init)
  617. return IPA_LOADER_MODEM;
  618. goto out_self;
  619. }
  620. /* Any other error on the new property means it's poorly defined */
  621. if (ret)
  622. return IPA_LOADER_INVALID;
  623. /* New property value exists; if old one does too, that's invalid */
  624. if (modem_init)
  625. return IPA_LOADER_INVALID;
  626. /* Modem loads GSI firmware for "modem" */
  627. if (!strcmp(str, "modem"))
  628. return IPA_LOADER_MODEM;
  629. /* No GSI firmware load is needed for "skip" */
  630. if (!strcmp(str, "skip"))
  631. return IPA_LOADER_SKIP;
  632. /* Any value other than "self" is an error */
  633. if (strcmp(str, "self"))
  634. return IPA_LOADER_INVALID;
  635. out_self:
  636. /* We need Trust Zone to load firmware; make sure it's available */
  637. if (qcom_scm_is_available())
  638. return IPA_LOADER_SELF;
  639. return IPA_LOADER_DEFER;
  640. }
  641. /**
  642. * ipa_probe() - IPA platform driver probe function
  643. * @pdev: Platform device pointer
  644. *
  645. * Return: 0 if successful, or a negative error code (possibly
  646. * EPROBE_DEFER)
  647. *
  648. * This is the main entry point for the IPA driver. Initialization proceeds
  649. * in several stages:
  650. * - The "init" stage involves activities that can be initialized without
  651. * access to the IPA hardware.
  652. * - The "config" stage requires IPA power to be active so IPA registers
  653. * can be accessed, but does not require the use of IPA immediate commands.
  654. * - The "setup" stage uses IPA immediate commands, and so requires the GSI
  655. * layer to be initialized.
  656. *
  657. * A Boolean Device Tree "modem-init" property determines whether GSI
  658. * initialization will be performed by the AP (Trust Zone) or the modem.
  659. * If the AP does GSI initialization, the setup phase is entered after
  660. * this has completed successfully. Otherwise the modem initializes
  661. * the GSI layer and signals it has finished by sending an SMP2P interrupt
  662. * to the AP; this triggers the start if IPA setup.
  663. */
  664. static int ipa_probe(struct platform_device *pdev)
  665. {
  666. struct device *dev = &pdev->dev;
  667. struct ipa_interrupt *interrupt;
  668. enum ipa_firmware_loader loader;
  669. const struct ipa_data *data;
  670. struct ipa_power *power;
  671. struct ipa *ipa;
  672. int ret;
  673. ipa_validate_build();
  674. /* Get configuration data early; needed for power initialization */
  675. data = of_device_get_match_data(dev);
  676. if (!data) {
  677. dev_err(dev, "matched hardware not supported\n");
  678. return -ENODEV;
  679. }
  680. if (!data->modem_route_count) {
  681. dev_err(dev, "modem_route_count cannot be zero\n");
  682. return -EINVAL;
  683. }
  684. loader = ipa_firmware_loader(dev);
  685. if (loader == IPA_LOADER_INVALID)
  686. return -EINVAL;
  687. if (loader == IPA_LOADER_DEFER)
  688. return -EPROBE_DEFER;
  689. /* The IPA interrupt might not be ready when we're probed, so this
  690. * might return -EPROBE_DEFER.
  691. */
  692. interrupt = ipa_interrupt_init(pdev);
  693. if (IS_ERR(interrupt))
  694. return PTR_ERR(interrupt);
  695. /* The clock and interconnects might not be ready when we're probed,
  696. * so this might return -EPROBE_DEFER.
  697. */
  698. power = ipa_power_init(dev, data->power_data);
  699. if (IS_ERR(power)) {
  700. ret = PTR_ERR(power);
  701. goto err_interrupt_exit;
  702. }
  703. /* No more EPROBE_DEFER. Allocate and initialize the IPA structure */
  704. ipa = kzalloc_obj(*ipa);
  705. if (!ipa) {
  706. ret = -ENOMEM;
  707. goto err_power_exit;
  708. }
  709. ipa->dev = dev;
  710. dev_set_drvdata(dev, ipa);
  711. ipa->interrupt = interrupt;
  712. ipa->power = power;
  713. ipa->version = data->version;
  714. ipa->modem_route_count = data->modem_route_count;
  715. init_completion(&ipa->completion);
  716. ret = ipa_reg_init(ipa, pdev);
  717. if (ret)
  718. goto err_kfree_ipa;
  719. ret = ipa_mem_init(ipa, pdev, data->mem_data);
  720. if (ret)
  721. goto err_reg_exit;
  722. ret = ipa_cmd_init(ipa);
  723. if (ret)
  724. goto err_mem_exit;
  725. ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count,
  726. data->endpoint_data);
  727. if (ret)
  728. goto err_mem_exit;
  729. /* Result is a non-zero mask of endpoints that support filtering */
  730. ret = ipa_endpoint_init(ipa, data->endpoint_count, data->endpoint_data);
  731. if (ret)
  732. goto err_gsi_exit;
  733. ret = ipa_table_init(ipa);
  734. if (ret)
  735. goto err_endpoint_exit;
  736. ret = ipa_smp2p_init(ipa, pdev, loader == IPA_LOADER_MODEM);
  737. if (ret)
  738. goto err_table_exit;
  739. /* Power needs to be active for config and setup */
  740. ret = pm_runtime_get_sync(dev);
  741. if (WARN_ON(ret < 0))
  742. goto err_power_put;
  743. ret = ipa_config(ipa, data);
  744. if (ret)
  745. goto err_power_put;
  746. dev_info(dev, "IPA driver initialized");
  747. /* If the modem is loading GSI firmware, it will trigger a call to
  748. * ipa_setup() when it has finished. In that case we're done here.
  749. */
  750. if (loader == IPA_LOADER_MODEM)
  751. goto done;
  752. if (loader == IPA_LOADER_SELF) {
  753. /* The AP is loading GSI firmware; do so now */
  754. ret = ipa_firmware_load(dev);
  755. if (ret)
  756. goto err_deconfig;
  757. } /* Otherwise loader == IPA_LOADER_SKIP */
  758. /* GSI firmware is loaded; proceed to setup */
  759. ret = ipa_setup(ipa);
  760. if (ret)
  761. goto err_deconfig;
  762. done:
  763. (void)pm_runtime_put_autosuspend(dev);
  764. return 0;
  765. err_deconfig:
  766. ipa_deconfig(ipa);
  767. err_power_put:
  768. pm_runtime_put_noidle(dev);
  769. ipa_smp2p_exit(ipa);
  770. err_table_exit:
  771. ipa_table_exit(ipa);
  772. err_endpoint_exit:
  773. ipa_endpoint_exit(ipa);
  774. err_gsi_exit:
  775. gsi_exit(&ipa->gsi);
  776. err_mem_exit:
  777. ipa_mem_exit(ipa);
  778. err_reg_exit:
  779. ipa_reg_exit(ipa);
  780. err_kfree_ipa:
  781. kfree(ipa);
  782. err_power_exit:
  783. ipa_power_exit(power);
  784. err_interrupt_exit:
  785. ipa_interrupt_exit(interrupt);
  786. return ret;
  787. }
  788. static void ipa_remove(struct platform_device *pdev)
  789. {
  790. struct ipa_interrupt *interrupt;
  791. struct ipa_power *power;
  792. struct device *dev;
  793. struct ipa *ipa;
  794. int ret;
  795. ipa = dev_get_drvdata(&pdev->dev);
  796. dev = ipa->dev;
  797. WARN_ON(dev != &pdev->dev);
  798. power = ipa->power;
  799. interrupt = ipa->interrupt;
  800. /* Prevent the modem from triggering a call to ipa_setup(). This
  801. * also ensures a modem-initiated setup that's underway completes.
  802. */
  803. ipa_smp2p_irq_disable_setup(ipa);
  804. ret = pm_runtime_get_sync(dev);
  805. if (WARN_ON(ret < 0))
  806. goto out_power_put;
  807. if (ipa->setup_complete) {
  808. ret = ipa_modem_stop(ipa);
  809. /* If starting or stopping is in progress, try once more */
  810. if (ret == -EBUSY) {
  811. usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
  812. ret = ipa_modem_stop(ipa);
  813. }
  814. if (ret) {
  815. /*
  816. * Not cleaning up here properly might also yield a
  817. * crash later on. As the device is still unregistered
  818. * in this case, this might even yield a crash later on.
  819. */
  820. dev_err(dev, "Failed to stop modem (%pe), leaking resources\n",
  821. ERR_PTR(ret));
  822. return;
  823. }
  824. ipa_teardown(ipa);
  825. }
  826. ipa_deconfig(ipa);
  827. out_power_put:
  828. pm_runtime_put_noidle(dev);
  829. ipa_smp2p_exit(ipa);
  830. ipa_table_exit(ipa);
  831. ipa_endpoint_exit(ipa);
  832. gsi_exit(&ipa->gsi);
  833. ipa_mem_exit(ipa);
  834. ipa_reg_exit(ipa);
  835. kfree(ipa);
  836. ipa_power_exit(power);
  837. ipa_interrupt_exit(interrupt);
  838. dev_info(dev, "IPA driver removed");
  839. }
  840. static const struct attribute_group *ipa_attribute_groups[] = {
  841. &ipa_attribute_group,
  842. &ipa_feature_attribute_group,
  843. &ipa_endpoint_id_attribute_group,
  844. &ipa_modem_attribute_group,
  845. NULL,
  846. };
  847. static struct platform_driver ipa_driver = {
  848. .probe = ipa_probe,
  849. .remove = ipa_remove,
  850. .shutdown = ipa_remove,
  851. .driver = {
  852. .name = "ipa",
  853. .pm = &ipa_pm_ops,
  854. .of_match_table = ipa_match,
  855. .dev_groups = ipa_attribute_groups,
  856. },
  857. };
  858. module_platform_driver(ipa_driver);
  859. MODULE_LICENSE("GPL v2");
  860. MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver");