1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2023 Linaro Ltd.
7 #include <linux/types.h>
8 #include <linux/atomic.h>
9 #include <linux/bitfield.h>
10 #include <linux/device.h>
11 #include <linux/bug.h>
13 #include <linux/firmware.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/firmware/qcom/qcom_scm.h>
20 #include <linux/soc/qcom/mdt_loader.h>
23 #include "ipa_power.h"
25 #include "ipa_endpoint.h"
26 #include "ipa_resource.h"
30 #include "ipa_table.h"
31 #include "ipa_smp2p.h"
32 #include "ipa_modem.h"
34 #include "ipa_interrupt.h"
35 #include "gsi_trans.h"
36 #include "ipa_sysfs.h"
39 * DOC: The IP Accelerator
41 * This driver supports the Qualcomm IP Accelerator (IPA), which is a
42 * networking component found in many Qualcomm SoCs. The IPA is connected
43 * to the application processor (AP), but is also connected (and partially
44 * controlled by) other "execution environments" (EEs), such as a modem.
46 * The IPA is the conduit between the AP and the modem that carries network
47 * traffic. This driver presents a network interface representing the
48 * connection of the modem to external (e.g. LTE) networks.
50 * The IPA provides protocol checksum calculation, offloading this work
51 * from the AP. The IPA offers additional functionality, including routing,
52 * filtering, and NAT support, but that more advanced functionality is not
53 * currently supported. Despite that, some resources--including routing
54 * tables and filter tables--are defined in this driver because they must
55 * be initialized even when the advanced hardware features are not used.
57 * There are two distinct layers that implement the IPA hardware, and this
58 * is reflected in the organization of the driver. The generic software
59 * interface (GSI) is an integral component of the IPA, providing a
60 * well-defined communication layer between the AP subsystem and the IPA
61 * core. The GSI implements a set of "channels" used for communication
62 * between the AP and the IPA.
64 * The IPA layer uses GSI channels to implement its "endpoints". And while
65 * a GSI channel carries data between the AP and the IPA, a pair of IPA
66 * endpoints is used to carry traffic between two EEs. Specifically, the main
67 * modem network interface is implemented by two pairs of endpoints: a TX
68 * endpoint on the AP coupled with an RX endpoint on the modem; and another
69 * RX endpoint on the AP receiving data from a TX endpoint on the modem.
72 /* The name of the GSI firmware file relative to /lib/firmware */
73 #define IPA_FW_PATH_DEFAULT "ipa_fws.mdt"
76 /* Shift of 19.2 MHz timestamp to achieve lower resolution timestamps */
77 /* IPA v5.5+ does not specify Qtime timestamp config for DPL */
78 #define DPL_TIMESTAMP_SHIFT 14 /* ~1.172 kHz, ~853 usec per tick */
79 #define TAG_TIMESTAMP_SHIFT 14
80 #define NAT_TIMESTAMP_SHIFT 24 /* ~1.144 Hz, ~874 msec per tick */
82 /* Divider for 19.2 MHz crystal oscillator clock to get common timer clock */
83 #define IPA_XO_CLOCK_DIVIDER 192 /* 1 is subtracted where used */
86 * enum ipa_firmware_loader: How GSI firmware gets loaded
88 * @IPA_LOADER_DEFER: System not ready; try again later
89 * @IPA_LOADER_SELF: AP loads GSI firmware
90 * @IPA_LOADER_MODEM: Modem loads GSI firmware, signals when done
91 * @IPA_LOADER_SKIP: Neither AP nor modem need to load GSI firmware
92 * @IPA_LOADER_INVALID: GSI firmware loader specification is invalid
94 enum ipa_firmware_loader {
103 * ipa_setup() - Set up IPA hardware
106 * Perform initialization that requires issuing immediate commands on
107 * the command TX endpoint. If the modem is doing GSI firmware load
108 * and initialization, this function will be called when an SMP2P
109 * interrupt has been signaled by the modem. Otherwise it will be
110 * called from ipa_probe() after GSI firmware has been successfully
111 * loaded, authenticated, and started by Trust Zone.
113 int ipa_setup(struct ipa *ipa)
115 struct ipa_endpoint *exception_endpoint;
116 struct ipa_endpoint *command_endpoint;
117 struct device *dev = &ipa->pdev->dev;
120 ret = gsi_setup(&ipa->gsi);
124 ret = ipa_power_setup(ipa);
126 goto err_gsi_teardown;
128 ipa_endpoint_setup(ipa);
130 /* We need to use the AP command TX endpoint to perform other
131 * initialization, so we enable first.
133 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
134 ret = ipa_endpoint_enable_one(command_endpoint);
136 goto err_endpoint_teardown;
138 ret = ipa_mem_setup(ipa); /* No matching teardown required */
140 goto err_command_disable;
142 ret = ipa_table_setup(ipa); /* No matching teardown required */
144 goto err_command_disable;
146 /* Enable the exception handling endpoint, and tell the hardware
147 * to use it by default.
149 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
150 ret = ipa_endpoint_enable_one(exception_endpoint);
152 goto err_command_disable;
154 ipa_endpoint_default_route_set(ipa, exception_endpoint->endpoint_id);
156 /* We're all set. Now prepare for communication with the modem */
157 ret = ipa_qmi_setup(ipa);
159 goto err_default_route_clear;
161 ipa->setup_complete = true;
163 dev_info(dev, "IPA driver setup completed successfully\n");
167 err_default_route_clear:
168 ipa_endpoint_default_route_clear(ipa);
169 ipa_endpoint_disable_one(exception_endpoint);
171 ipa_endpoint_disable_one(command_endpoint);
172 err_endpoint_teardown:
173 ipa_endpoint_teardown(ipa);
174 ipa_power_teardown(ipa);
176 gsi_teardown(&ipa->gsi);
182 * ipa_teardown() - Inverse of ipa_setup()
185 static void ipa_teardown(struct ipa *ipa)
187 struct ipa_endpoint *exception_endpoint;
188 struct ipa_endpoint *command_endpoint;
190 /* We're going to tear everything down, as if setup never completed */
191 ipa->setup_complete = false;
193 ipa_qmi_teardown(ipa);
194 ipa_endpoint_default_route_clear(ipa);
195 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
196 ipa_endpoint_disable_one(exception_endpoint);
197 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
198 ipa_endpoint_disable_one(command_endpoint);
199 ipa_endpoint_teardown(ipa);
200 ipa_power_teardown(ipa);
201 gsi_teardown(&ipa->gsi);
205 ipa_hardware_config_bcr(struct ipa *ipa, const struct ipa_data *data)
207 const struct reg *reg;
210 /* IPA v4.5+ has no backward compatibility register */
211 if (ipa->version >= IPA_VERSION_4_5)
214 reg = ipa_reg(ipa, IPA_BCR);
215 val = data->backward_compat;
216 iowrite32(val, ipa->reg_virt + reg_offset(reg));
219 static void ipa_hardware_config_tx(struct ipa *ipa)
221 enum ipa_version version = ipa->version;
222 const struct reg *reg;
226 if (version <= IPA_VERSION_4_0 || version >= IPA_VERSION_4_5)
229 /* Disable PA mask to allow HOLB drop */
230 reg = ipa_reg(ipa, IPA_TX_CFG);
231 offset = reg_offset(reg);
233 val = ioread32(ipa->reg_virt + offset);
235 val &= ~reg_bit(reg, PA_MASK_EN);
237 iowrite32(val, ipa->reg_virt + offset);
240 static void ipa_hardware_config_clkon(struct ipa *ipa)
242 enum ipa_version version = ipa->version;
243 const struct reg *reg;
246 if (version >= IPA_VERSION_4_5)
249 if (version < IPA_VERSION_4_0 && version != IPA_VERSION_3_1)
252 /* Implement some hardware workarounds */
253 reg = ipa_reg(ipa, CLKON_CFG);
254 if (version == IPA_VERSION_3_1) {
255 /* Disable MISC clock gating */
256 val = reg_bit(reg, CLKON_MISC);
257 } else { /* IPA v4.0+ */
258 /* Enable open global clocks in the CLKON configuration */
259 val = reg_bit(reg, CLKON_GLOBAL);
260 val |= reg_bit(reg, GLOBAL_2X_CLK);
263 iowrite32(val, ipa->reg_virt + reg_offset(reg));
266 /* Configure bus access behavior for IPA components */
267 static void ipa_hardware_config_comp(struct ipa *ipa)
269 const struct reg *reg;
273 /* Nothing to configure prior to IPA v4.0 */
274 if (ipa->version < IPA_VERSION_4_0)
277 reg = ipa_reg(ipa, COMP_CFG);
278 offset = reg_offset(reg);
280 val = ioread32(ipa->reg_virt + offset);
282 if (ipa->version == IPA_VERSION_4_0) {
283 val &= ~reg_bit(reg, IPA_QMB_SELECT_CONS_EN);
284 val &= ~reg_bit(reg, IPA_QMB_SELECT_PROD_EN);
285 val &= ~reg_bit(reg, IPA_QMB_SELECT_GLOBAL_EN);
286 } else if (ipa->version < IPA_VERSION_4_5) {
287 val |= reg_bit(reg, GSI_MULTI_AXI_MASTERS_DIS);
289 /* For IPA v4.5+ FULL_FLUSH_WAIT_RS_CLOSURE_EN is 0 */
292 val |= reg_bit(reg, GSI_MULTI_INORDER_RD_DIS);
293 val |= reg_bit(reg, GSI_MULTI_INORDER_WR_DIS);
295 iowrite32(val, ipa->reg_virt + offset);
298 /* Configure DDR and (possibly) PCIe max read/write QSB values */
300 ipa_hardware_config_qsb(struct ipa *ipa, const struct ipa_data *data)
302 const struct ipa_qsb_data *data0;
303 const struct ipa_qsb_data *data1;
304 const struct reg *reg;
307 /* QMB 0 represents DDR; QMB 1 (if present) represents PCIe */
308 data0 = &data->qsb_data[IPA_QSB_MASTER_DDR];
309 if (data->qsb_count > 1)
310 data1 = &data->qsb_data[IPA_QSB_MASTER_PCIE];
312 /* Max outstanding write accesses for QSB masters */
313 reg = ipa_reg(ipa, QSB_MAX_WRITES);
315 val = reg_encode(reg, GEN_QMB_0_MAX_WRITES, data0->max_writes);
316 if (data->qsb_count > 1)
317 val |= reg_encode(reg, GEN_QMB_1_MAX_WRITES, data1->max_writes);
319 iowrite32(val, ipa->reg_virt + reg_offset(reg));
321 /* Max outstanding read accesses for QSB masters */
322 reg = ipa_reg(ipa, QSB_MAX_READS);
324 val = reg_encode(reg, GEN_QMB_0_MAX_READS, data0->max_reads);
325 if (ipa->version >= IPA_VERSION_4_0)
326 val |= reg_encode(reg, GEN_QMB_0_MAX_READS_BEATS,
327 data0->max_reads_beats);
328 if (data->qsb_count > 1) {
329 val = reg_encode(reg, GEN_QMB_1_MAX_READS, data1->max_reads);
330 if (ipa->version >= IPA_VERSION_4_0)
331 val |= reg_encode(reg, GEN_QMB_1_MAX_READS_BEATS,
332 data1->max_reads_beats);
335 iowrite32(val, ipa->reg_virt + reg_offset(reg));
338 /* The internal inactivity timer clock is used for the aggregation timer */
339 #define TIMER_FREQUENCY 32000 /* 32 KHz inactivity timer clock */
341 /* Compute the value to use in the COUNTER_CFG register AGGR_GRANULARITY
342 * field to represent the given number of microseconds. The value is one
343 * less than the number of timer ticks in the requested period. 0 is not
344 * a valid granularity value (so for example @usec must be at least 16 for
345 * a TIMER_FREQUENCY of 32000).
347 static __always_inline u32 ipa_aggr_granularity_val(u32 usec)
349 return DIV_ROUND_CLOSEST(usec * TIMER_FREQUENCY, USEC_PER_SEC) - 1;
352 /* IPA uses unified Qtime starting at IPA v4.5, implementing various
353 * timestamps and timers independent of the IPA core clock rate. The
354 * Qtimer is based on a 56-bit timestamp incremented at each tick of
355 * a 19.2 MHz SoC crystal oscillator (XO clock).
357 * For IPA timestamps (tag, NAT, data path logging) a lower resolution
358 * timestamp is achieved by shifting the Qtimer timestamp value right
359 * some number of bits to produce the low-order bits of the coarser
360 * granularity timestamp.
362 * For timers, a common timer clock is derived from the XO clock using
363 * a divider (we use 192, to produce a 100kHz timer clock). From
364 * this common clock, three "pulse generators" are used to produce
365 * timer ticks at a configurable frequency. IPA timers (such as
366 * those used for aggregation or head-of-line block handling) now
367 * define their period based on one of these pulse generators.
369 static void ipa_qtime_config(struct ipa *ipa)
371 const struct reg *reg;
375 /* Timer clock divider must be disabled when we change the rate */
376 reg = ipa_reg(ipa, TIMERS_XO_CLK_DIV_CFG);
377 iowrite32(0, ipa->reg_virt + reg_offset(reg));
379 reg = ipa_reg(ipa, QTIME_TIMESTAMP_CFG);
380 if (ipa->version < IPA_VERSION_5_5) {
381 /* Set DPL time stamp resolution to use Qtime (not 1 msec) */
382 val = reg_encode(reg, DPL_TIMESTAMP_LSB, DPL_TIMESTAMP_SHIFT);
383 val |= reg_bit(reg, DPL_TIMESTAMP_SEL);
385 /* Configure tag and NAT Qtime timestamp resolution as well */
386 val = reg_encode(reg, TAG_TIMESTAMP_LSB, TAG_TIMESTAMP_SHIFT);
387 val = reg_encode(reg, NAT_TIMESTAMP_LSB, NAT_TIMESTAMP_SHIFT);
389 iowrite32(val, ipa->reg_virt + reg_offset(reg));
391 /* Set granularity of pulse generators used for other timers */
392 reg = ipa_reg(ipa, TIMERS_PULSE_GRAN_CFG);
393 val = reg_encode(reg, PULSE_GRAN_0, IPA_GRAN_100_US);
394 val |= reg_encode(reg, PULSE_GRAN_1, IPA_GRAN_1_MS);
395 if (ipa->version >= IPA_VERSION_5_0) {
396 val |= reg_encode(reg, PULSE_GRAN_2, IPA_GRAN_10_MS);
397 val |= reg_encode(reg, PULSE_GRAN_3, IPA_GRAN_10_MS);
399 val |= reg_encode(reg, PULSE_GRAN_2, IPA_GRAN_1_MS);
402 iowrite32(val, ipa->reg_virt + reg_offset(reg));
404 /* Actual divider is 1 more than value supplied here */
405 reg = ipa_reg(ipa, TIMERS_XO_CLK_DIV_CFG);
406 offset = reg_offset(reg);
408 val = reg_encode(reg, DIV_VALUE, IPA_XO_CLOCK_DIVIDER - 1);
410 iowrite32(val, ipa->reg_virt + offset);
412 /* Divider value is set; re-enable the common timer clock divider */
413 val |= reg_bit(reg, DIV_ENABLE);
415 iowrite32(val, ipa->reg_virt + offset);
418 /* Before IPA v4.5 timing is controlled by a counter register */
419 static void ipa_hardware_config_counter(struct ipa *ipa)
421 u32 granularity = ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY);
422 const struct reg *reg;
425 reg = ipa_reg(ipa, COUNTER_CFG);
426 /* If defined, EOT_COAL_GRANULARITY is 0 */
427 val = reg_encode(reg, AGGR_GRANULARITY, granularity);
428 iowrite32(val, ipa->reg_virt + reg_offset(reg));
431 static void ipa_hardware_config_timing(struct ipa *ipa)
433 if (ipa->version < IPA_VERSION_4_5)
434 ipa_hardware_config_counter(ipa);
436 ipa_qtime_config(ipa);
439 static void ipa_hardware_config_hashing(struct ipa *ipa)
441 const struct reg *reg;
443 /* Other than IPA v4.2, all versions enable "hashing". Starting
444 * with IPA v5.0, the filter and router tables are implemented
445 * differently, but the default configuration enables this feature
446 * (now referred to as "cacheing"), so there's nothing to do here.
448 if (ipa->version != IPA_VERSION_4_2)
451 /* IPA v4.2 does not support hashed tables, so disable them */
452 reg = ipa_reg(ipa, FILT_ROUT_HASH_EN);
454 /* IPV6_ROUTER_HASH, IPV6_FILTER_HASH, IPV4_ROUTER_HASH,
455 * IPV4_FILTER_HASH are all zero.
457 iowrite32(0, ipa->reg_virt + reg_offset(reg));
460 static void ipa_idle_indication_cfg(struct ipa *ipa,
461 u32 enter_idle_debounce_thresh,
462 bool const_non_idle_enable)
464 const struct reg *reg;
467 if (ipa->version < IPA_VERSION_3_5_1)
470 reg = ipa_reg(ipa, IDLE_INDICATION_CFG);
471 val = reg_encode(reg, ENTER_IDLE_DEBOUNCE_THRESH,
472 enter_idle_debounce_thresh);
473 if (const_non_idle_enable)
474 val |= reg_bit(reg, CONST_NON_IDLE_ENABLE);
476 iowrite32(val, ipa->reg_virt + reg_offset(reg));
480 * ipa_hardware_dcd_config() - Enable dynamic clock division on IPA
483 * Configures when the IPA signals it is idle to the global clock
484 * controller, which can respond by scaling down the clock to save
487 static void ipa_hardware_dcd_config(struct ipa *ipa)
489 /* Recommended values for IPA 3.5 and later according to IPA HPG */
490 ipa_idle_indication_cfg(ipa, 256, false);
493 static void ipa_hardware_dcd_deconfig(struct ipa *ipa)
495 /* Power-on reset values */
496 ipa_idle_indication_cfg(ipa, 0, true);
500 * ipa_hardware_config() - Primitive hardware initialization
502 * @data: IPA configuration data
504 static void ipa_hardware_config(struct ipa *ipa, const struct ipa_data *data)
506 ipa_hardware_config_bcr(ipa, data);
507 ipa_hardware_config_tx(ipa);
508 ipa_hardware_config_clkon(ipa);
509 ipa_hardware_config_comp(ipa);
510 ipa_hardware_config_qsb(ipa, data);
511 ipa_hardware_config_timing(ipa);
512 ipa_hardware_config_hashing(ipa);
513 ipa_hardware_dcd_config(ipa);
517 * ipa_hardware_deconfig() - Inverse of ipa_hardware_config()
520 * This restores the power-on reset values (even if they aren't different)
522 static void ipa_hardware_deconfig(struct ipa *ipa)
524 /* Mostly we just leave things as we set them. */
525 ipa_hardware_dcd_deconfig(ipa);
529 * ipa_config() - Configure IPA hardware
531 * @data: IPA configuration data
533 * Perform initialization requiring IPA power to be enabled.
535 static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
539 ipa_hardware_config(ipa, data);
541 ret = ipa_mem_config(ipa);
543 goto err_hardware_deconfig;
545 ipa->interrupt = ipa_interrupt_config(ipa);
546 if (IS_ERR(ipa->interrupt)) {
547 ret = PTR_ERR(ipa->interrupt);
548 ipa->interrupt = NULL;
549 goto err_mem_deconfig;
554 ret = ipa_endpoint_config(ipa);
556 goto err_uc_deconfig;
558 ipa_table_config(ipa); /* No deconfig required */
560 /* Assign resource limitation to each group; no deconfig required */
561 ret = ipa_resource_config(ipa, data->resource_data);
563 goto err_endpoint_deconfig;
565 ret = ipa_modem_config(ipa);
567 goto err_endpoint_deconfig;
571 err_endpoint_deconfig:
572 ipa_endpoint_deconfig(ipa);
574 ipa_uc_deconfig(ipa);
575 ipa_interrupt_deconfig(ipa->interrupt);
576 ipa->interrupt = NULL;
578 ipa_mem_deconfig(ipa);
579 err_hardware_deconfig:
580 ipa_hardware_deconfig(ipa);
586 * ipa_deconfig() - Inverse of ipa_config()
589 static void ipa_deconfig(struct ipa *ipa)
591 ipa_modem_deconfig(ipa);
592 ipa_endpoint_deconfig(ipa);
593 ipa_uc_deconfig(ipa);
594 ipa_interrupt_deconfig(ipa->interrupt);
595 ipa->interrupt = NULL;
596 ipa_mem_deconfig(ipa);
597 ipa_hardware_deconfig(ipa);
600 static int ipa_firmware_load(struct device *dev)
602 const struct firmware *fw;
603 struct device_node *node;
611 node = of_parse_phandle(dev->of_node, "memory-region", 0);
613 dev_err(dev, "DT error getting \"memory-region\" property\n");
617 ret = of_address_to_resource(node, 0, &res);
620 dev_err(dev, "error %d getting \"memory-region\" resource\n",
625 /* Use name from DTB if specified; use default for *any* error */
626 ret = of_property_read_string(dev->of_node, "firmware-name", &path);
628 dev_dbg(dev, "error %d getting \"firmware-name\" resource\n",
630 path = IPA_FW_PATH_DEFAULT;
633 ret = request_firmware(&fw, path, dev);
635 dev_err(dev, "error %d requesting \"%s\"\n", ret, path);
640 size = (size_t)resource_size(&res);
641 virt = memremap(phys, size, MEMREMAP_WC);
643 dev_err(dev, "unable to remap firmware memory\n");
645 goto out_release_firmware;
648 ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
650 dev_err(dev, "error %d loading \"%s\"\n", ret, path);
651 else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID)))
652 dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
655 out_release_firmware:
656 release_firmware(fw);
661 static const struct of_device_id ipa_match[] = {
663 .compatible = "qcom,msm8998-ipa",
664 .data = &ipa_data_v3_1,
667 .compatible = "qcom,sdm845-ipa",
668 .data = &ipa_data_v3_5_1,
671 .compatible = "qcom,sc7180-ipa",
672 .data = &ipa_data_v4_2,
675 .compatible = "qcom,sdx55-ipa",
676 .data = &ipa_data_v4_5,
679 .compatible = "qcom,sm6350-ipa",
680 .data = &ipa_data_v4_7,
683 .compatible = "qcom,sm8350-ipa",
684 .data = &ipa_data_v4_9,
687 .compatible = "qcom,sc7280-ipa",
688 .data = &ipa_data_v4_11,
691 .compatible = "qcom,sdx65-ipa",
692 .data = &ipa_data_v5_0,
695 .compatible = "qcom,sm8550-ipa",
696 .data = &ipa_data_v5_5,
700 MODULE_DEVICE_TABLE(of, ipa_match);
702 /* Check things that can be validated at build time. This just
703 * groups these things BUILD_BUG_ON() calls don't clutter the rest
706 static void ipa_validate_build(void)
708 /* At one time we assumed a 64-bit build, allowing some do_div()
709 * calls to be replaced by simple division or modulo operations.
710 * We currently only perform divide and modulo operations on u32,
711 * u16, or size_t objects, and of those only size_t has any chance
712 * of being a 64-bit value. (It should be guaranteed 32 bits wide
713 * on a 32-bit build, but there is no harm in verifying that.)
715 BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT) && sizeof(size_t) != 4);
717 /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */
718 BUILD_BUG_ON(GSI_EE_AP != 0);
720 /* There's no point if we have no channels or event rings */
721 BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX);
722 BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX);
724 /* GSI hardware design limits */
725 BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32);
726 BUILD_BUG_ON(GSI_EVT_RING_COUNT_MAX > 31);
728 /* The number of TREs in a transaction is limited by the channel's
729 * TLV FIFO size. A transaction structure uses 8-bit fields
730 * to represents the number of TREs it has allocated and used.
732 BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX);
734 /* This is used as a divisor */
735 BUILD_BUG_ON(!IPA_AGGR_GRANULARITY);
737 /* Aggregation granularity value can't be 0, and must fit */
738 BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY));
741 static enum ipa_firmware_loader ipa_firmware_loader(struct device *dev)
747 /* Look up the old and new properties by name */
748 modem_init = of_property_read_bool(dev->of_node, "modem-init");
749 ret = of_property_read_string(dev->of_node, "qcom,gsi-loader", &str);
751 /* If the new property doesn't exist, it's legacy behavior */
752 if (ret == -EINVAL) {
754 return IPA_LOADER_MODEM;
758 /* Any other error on the new property means it's poorly defined */
760 return IPA_LOADER_INVALID;
762 /* New property value exists; if old one does too, that's invalid */
764 return IPA_LOADER_INVALID;
766 /* Modem loads GSI firmware for "modem" */
767 if (!strcmp(str, "modem"))
768 return IPA_LOADER_MODEM;
770 /* No GSI firmware load is needed for "skip" */
771 if (!strcmp(str, "skip"))
772 return IPA_LOADER_SKIP;
774 /* Any value other than "self" is an error */
775 if (strcmp(str, "self"))
776 return IPA_LOADER_INVALID;
778 /* We need Trust Zone to load firmware; make sure it's available */
779 if (qcom_scm_is_available())
780 return IPA_LOADER_SELF;
782 return IPA_LOADER_DEFER;
786 * ipa_probe() - IPA platform driver probe function
787 * @pdev: Platform device pointer
789 * Return: 0 if successful, or a negative error code (possibly
792 * This is the main entry point for the IPA driver. Initialization proceeds
794 * - The "init" stage involves activities that can be initialized without
795 * access to the IPA hardware.
796 * - The "config" stage requires IPA power to be active so IPA registers
797 * can be accessed, but does not require the use of IPA immediate commands.
798 * - The "setup" stage uses IPA immediate commands, and so requires the GSI
799 * layer to be initialized.
801 * A Boolean Device Tree "modem-init" property determines whether GSI
802 * initialization will be performed by the AP (Trust Zone) or the modem.
803 * If the AP does GSI initialization, the setup phase is entered after
804 * this has completed successfully. Otherwise the modem initializes
805 * the GSI layer and signals it has finished by sending an SMP2P interrupt
806 * to the AP; this triggers the start if IPA setup.
808 static int ipa_probe(struct platform_device *pdev)
810 struct device *dev = &pdev->dev;
811 enum ipa_firmware_loader loader;
812 const struct ipa_data *data;
813 struct ipa_power *power;
817 ipa_validate_build();
819 /* Get configuration data early; needed for power initialization */
820 data = of_device_get_match_data(dev);
822 dev_err(dev, "matched hardware not supported\n");
826 if (!ipa_version_supported(data->version)) {
827 dev_err(dev, "unsupported IPA version %u\n", data->version);
831 if (!data->modem_route_count) {
832 dev_err(dev, "modem_route_count cannot be zero\n");
836 loader = ipa_firmware_loader(dev);
837 if (loader == IPA_LOADER_INVALID)
839 if (loader == IPA_LOADER_DEFER)
840 return -EPROBE_DEFER;
842 /* The clock and interconnects might not be ready when we're
843 * probed, so might return -EPROBE_DEFER.
845 power = ipa_power_init(dev, data->power_data);
847 return PTR_ERR(power);
849 /* No more EPROBE_DEFER. Allocate and initialize the IPA structure */
850 ipa = kzalloc(sizeof(*ipa), GFP_KERNEL);
857 dev_set_drvdata(dev, ipa);
859 ipa->version = data->version;
860 ipa->modem_route_count = data->modem_route_count;
861 init_completion(&ipa->completion);
863 ret = ipa_reg_init(ipa);
867 ret = ipa_mem_init(ipa, data->mem_data);
871 ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count,
872 data->endpoint_data);
876 /* Result is a non-zero mask of endpoints that support filtering */
877 ret = ipa_endpoint_init(ipa, data->endpoint_count, data->endpoint_data);
881 ret = ipa_table_init(ipa);
883 goto err_endpoint_exit;
885 ret = ipa_smp2p_init(ipa, loader == IPA_LOADER_MODEM);
889 /* Power needs to be active for config and setup */
890 ret = pm_runtime_get_sync(dev);
891 if (WARN_ON(ret < 0))
894 ret = ipa_config(ipa, data);
898 dev_info(dev, "IPA driver initialized");
900 /* If the modem is loading GSI firmware, it will trigger a call to
901 * ipa_setup() when it has finished. In that case we're done here.
903 if (loader == IPA_LOADER_MODEM)
906 if (loader == IPA_LOADER_SELF) {
907 /* The AP is loading GSI firmware; do so now */
908 ret = ipa_firmware_load(dev);
911 } /* Otherwise loader == IPA_LOADER_SKIP */
913 /* GSI firmware is loaded; proceed to setup */
914 ret = ipa_setup(ipa);
918 pm_runtime_mark_last_busy(dev);
919 (void)pm_runtime_put_autosuspend(dev);
926 pm_runtime_put_noidle(dev);
931 ipa_endpoint_exit(ipa);
941 ipa_power_exit(power);
946 static void ipa_remove(struct platform_device *pdev)
948 struct ipa *ipa = dev_get_drvdata(&pdev->dev);
949 struct ipa_power *power = ipa->power;
950 struct device *dev = &pdev->dev;
953 /* Prevent the modem from triggering a call to ipa_setup(). This
954 * also ensures a modem-initiated setup that's underway completes.
956 ipa_smp2p_irq_disable_setup(ipa);
958 ret = pm_runtime_get_sync(dev);
959 if (WARN_ON(ret < 0))
962 if (ipa->setup_complete) {
963 ret = ipa_modem_stop(ipa);
964 /* If starting or stopping is in progress, try once more */
966 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
967 ret = ipa_modem_stop(ipa);
971 * Not cleaning up here properly might also yield a
972 * crash later on. As the device is still unregistered
973 * in this case, this might even yield a crash later on.
975 dev_err(dev, "Failed to stop modem (%pe), leaking resources\n",
985 pm_runtime_put_noidle(dev);
988 ipa_endpoint_exit(ipa);
993 ipa_power_exit(power);
995 dev_info(dev, "IPA driver removed");
998 static const struct attribute_group *ipa_attribute_groups[] = {
999 &ipa_attribute_group,
1000 &ipa_feature_attribute_group,
1001 &ipa_endpoint_id_attribute_group,
1002 &ipa_modem_attribute_group,
1006 static struct platform_driver ipa_driver = {
1008 .remove_new = ipa_remove,
1009 .shutdown = ipa_remove,
1013 .of_match_table = ipa_match,
1014 .dev_groups = ipa_attribute_groups,
1018 module_platform_driver(ipa_driver);
1020 MODULE_LICENSE("GPL v2");
1021 MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver");