1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2018-2021 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_device.h>
17 #include <linux/of_address.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/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_modem.h"
33 #include "ipa_interrupt.h"
34 #include "gsi_trans.h"
35 #include "ipa_sysfs.h"
38 * DOC: The IP Accelerator
40 * This driver supports the Qualcomm IP Accelerator (IPA), which is a
41 * networking component found in many Qualcomm SoCs. The IPA is connected
42 * to the application processor (AP), but is also connected (and partially
43 * controlled by) other "execution environments" (EEs), such as a modem.
45 * The IPA is the conduit between the AP and the modem that carries network
46 * traffic. This driver presents a network interface representing the
47 * connection of the modem to external (e.g. LTE) networks.
49 * The IPA provides protocol checksum calculation, offloading this work
50 * from the AP. The IPA offers additional functionality, including routing,
51 * filtering, and NAT support, but that more advanced functionality is not
52 * currently supported. Despite that, some resources--including routing
53 * tables and filter tables--are defined in this driver because they must
54 * be initialized even when the advanced hardware features are not used.
56 * There are two distinct layers that implement the IPA hardware, and this
57 * is reflected in the organization of the driver. The generic software
58 * interface (GSI) is an integral component of the IPA, providing a
59 * well-defined communication layer between the AP subsystem and the IPA
60 * core. The GSI implements a set of "channels" used for communication
61 * between the AP and the IPA.
63 * The IPA layer uses GSI channels to implement its "endpoints". And while
64 * a GSI channel carries data between the AP and the IPA, a pair of IPA
65 * endpoints is used to carry traffic between two EEs. Specifically, the main
66 * modem network interface is implemented by two pairs of endpoints: a TX
67 * endpoint on the AP coupled with an RX endpoint on the modem; and another
68 * RX endpoint on the AP receiving data from a TX endpoint on the modem.
71 /* The name of the GSI firmware file relative to /lib/firmware */
72 #define IPA_FW_PATH_DEFAULT "ipa_fws.mdt"
75 /* Shift of 19.2 MHz timestamp to achieve lower resolution timestamps */
76 #define DPL_TIMESTAMP_SHIFT 14 /* ~1.172 kHz, ~853 usec per tick */
77 #define TAG_TIMESTAMP_SHIFT 14
78 #define NAT_TIMESTAMP_SHIFT 24 /* ~1.144 Hz, ~874 msec per tick */
80 /* Divider for 19.2 MHz crystal oscillator clock to get common timer clock */
81 #define IPA_XO_CLOCK_DIVIDER 192 /* 1 is subtracted where used */
84 * ipa_setup() - Set up IPA hardware
87 * Perform initialization that requires issuing immediate commands on
88 * the command TX endpoint. If the modem is doing GSI firmware load
89 * and initialization, this function will be called when an SMP2P
90 * interrupt has been signaled by the modem. Otherwise it will be
91 * called from ipa_probe() after GSI firmware has been successfully
92 * loaded, authenticated, and started by Trust Zone.
94 int ipa_setup(struct ipa *ipa)
96 struct ipa_endpoint *exception_endpoint;
97 struct ipa_endpoint *command_endpoint;
98 struct device *dev = &ipa->pdev->dev;
101 ret = gsi_setup(&ipa->gsi);
105 ret = ipa_power_setup(ipa);
107 goto err_gsi_teardown;
109 ipa_endpoint_setup(ipa);
111 /* We need to use the AP command TX endpoint to perform other
112 * initialization, so we enable first.
114 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
115 ret = ipa_endpoint_enable_one(command_endpoint);
117 goto err_endpoint_teardown;
119 ret = ipa_mem_setup(ipa); /* No matching teardown required */
121 goto err_command_disable;
123 ret = ipa_table_setup(ipa); /* No matching teardown required */
125 goto err_command_disable;
127 /* Enable the exception handling endpoint, and tell the hardware
128 * to use it by default.
130 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
131 ret = ipa_endpoint_enable_one(exception_endpoint);
133 goto err_command_disable;
135 ipa_endpoint_default_route_set(ipa, exception_endpoint->endpoint_id);
137 /* We're all set. Now prepare for communication with the modem */
138 ret = ipa_qmi_setup(ipa);
140 goto err_default_route_clear;
142 ipa->setup_complete = true;
144 dev_info(dev, "IPA driver setup completed successfully\n");
148 err_default_route_clear:
149 ipa_endpoint_default_route_clear(ipa);
150 ipa_endpoint_disable_one(exception_endpoint);
152 ipa_endpoint_disable_one(command_endpoint);
153 err_endpoint_teardown:
154 ipa_endpoint_teardown(ipa);
155 ipa_power_teardown(ipa);
157 gsi_teardown(&ipa->gsi);
163 * ipa_teardown() - Inverse of ipa_setup()
166 static void ipa_teardown(struct ipa *ipa)
168 struct ipa_endpoint *exception_endpoint;
169 struct ipa_endpoint *command_endpoint;
171 /* We're going to tear everything down, as if setup never completed */
172 ipa->setup_complete = false;
174 ipa_qmi_teardown(ipa);
175 ipa_endpoint_default_route_clear(ipa);
176 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
177 ipa_endpoint_disable_one(exception_endpoint);
178 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
179 ipa_endpoint_disable_one(command_endpoint);
180 ipa_endpoint_teardown(ipa);
181 ipa_power_teardown(ipa);
182 gsi_teardown(&ipa->gsi);
185 /* Configure bus access behavior for IPA components */
186 static void ipa_hardware_config_comp(struct ipa *ipa)
190 /* Nothing to configure prior to IPA v4.0 */
191 if (ipa->version < IPA_VERSION_4_0)
194 val = ioread32(ipa->reg_virt + IPA_REG_COMP_CFG_OFFSET);
196 if (ipa->version == IPA_VERSION_4_0) {
197 val &= ~IPA_QMB_SELECT_CONS_EN_FMASK;
198 val &= ~IPA_QMB_SELECT_PROD_EN_FMASK;
199 val &= ~IPA_QMB_SELECT_GLOBAL_EN_FMASK;
200 } else if (ipa->version < IPA_VERSION_4_5) {
201 val |= GSI_MULTI_AXI_MASTERS_DIS_FMASK;
203 /* For IPA v4.5 IPA_FULL_FLUSH_WAIT_RSC_CLOSE_EN is 0 */
206 val |= GSI_MULTI_INORDER_RD_DIS_FMASK;
207 val |= GSI_MULTI_INORDER_WR_DIS_FMASK;
209 iowrite32(val, ipa->reg_virt + IPA_REG_COMP_CFG_OFFSET);
212 /* Configure DDR and (possibly) PCIe max read/write QSB values */
214 ipa_hardware_config_qsb(struct ipa *ipa, const struct ipa_data *data)
216 const struct ipa_qsb_data *data0;
217 const struct ipa_qsb_data *data1;
220 /* QMB 0 represents DDR; QMB 1 (if present) represents PCIe */
221 data0 = &data->qsb_data[IPA_QSB_MASTER_DDR];
222 if (data->qsb_count > 1)
223 data1 = &data->qsb_data[IPA_QSB_MASTER_PCIE];
225 /* Max outstanding write accesses for QSB masters */
226 val = u32_encode_bits(data0->max_writes, GEN_QMB_0_MAX_WRITES_FMASK);
227 if (data->qsb_count > 1)
228 val |= u32_encode_bits(data1->max_writes,
229 GEN_QMB_1_MAX_WRITES_FMASK);
230 iowrite32(val, ipa->reg_virt + IPA_REG_QSB_MAX_WRITES_OFFSET);
232 /* Max outstanding read accesses for QSB masters */
233 val = u32_encode_bits(data0->max_reads, GEN_QMB_0_MAX_READS_FMASK);
234 if (ipa->version >= IPA_VERSION_4_0)
235 val |= u32_encode_bits(data0->max_reads_beats,
236 GEN_QMB_0_MAX_READS_BEATS_FMASK);
237 if (data->qsb_count > 1) {
238 val |= u32_encode_bits(data1->max_reads,
239 GEN_QMB_1_MAX_READS_FMASK);
240 if (ipa->version >= IPA_VERSION_4_0)
241 val |= u32_encode_bits(data1->max_reads_beats,
242 GEN_QMB_1_MAX_READS_BEATS_FMASK);
244 iowrite32(val, ipa->reg_virt + IPA_REG_QSB_MAX_READS_OFFSET);
247 /* The internal inactivity timer clock is used for the aggregation timer */
248 #define TIMER_FREQUENCY 32000 /* 32 KHz inactivity timer clock */
250 /* Compute the value to use in the COUNTER_CFG register AGGR_GRANULARITY
251 * field to represent the given number of microseconds. The value is one
252 * less than the number of timer ticks in the requested period. 0 is not
253 * a valid granularity value (so for example @usec must be at least 16 for
254 * a TIMER_FREQUENCY of 32000).
256 static __always_inline u32 ipa_aggr_granularity_val(u32 usec)
258 return DIV_ROUND_CLOSEST(usec * TIMER_FREQUENCY, USEC_PER_SEC) - 1;
261 /* IPA uses unified Qtime starting at IPA v4.5, implementing various
262 * timestamps and timers independent of the IPA core clock rate. The
263 * Qtimer is based on a 56-bit timestamp incremented at each tick of
264 * a 19.2 MHz SoC crystal oscillator (XO clock).
266 * For IPA timestamps (tag, NAT, data path logging) a lower resolution
267 * timestamp is achieved by shifting the Qtimer timestamp value right
268 * some number of bits to produce the low-order bits of the coarser
269 * granularity timestamp.
271 * For timers, a common timer clock is derived from the XO clock using
272 * a divider (we use 192, to produce a 100kHz timer clock). From
273 * this common clock, three "pulse generators" are used to produce
274 * timer ticks at a configurable frequency. IPA timers (such as
275 * those used for aggregation or head-of-line block handling) now
276 * define their period based on one of these pulse generators.
278 static void ipa_qtime_config(struct ipa *ipa)
282 /* Timer clock divider must be disabled when we change the rate */
283 iowrite32(0, ipa->reg_virt + IPA_REG_TIMERS_XO_CLK_DIV_CFG_OFFSET);
285 /* Set DPL time stamp resolution to use Qtime (instead of 1 msec) */
286 val = u32_encode_bits(DPL_TIMESTAMP_SHIFT, DPL_TIMESTAMP_LSB_FMASK);
287 val |= u32_encode_bits(1, DPL_TIMESTAMP_SEL_FMASK);
288 /* Configure tag and NAT Qtime timestamp resolution as well */
289 val |= u32_encode_bits(TAG_TIMESTAMP_SHIFT, TAG_TIMESTAMP_LSB_FMASK);
290 val |= u32_encode_bits(NAT_TIMESTAMP_SHIFT, NAT_TIMESTAMP_LSB_FMASK);
291 iowrite32(val, ipa->reg_virt + IPA_REG_QTIME_TIMESTAMP_CFG_OFFSET);
293 /* Set granularity of pulse generators used for other timers */
294 val = u32_encode_bits(IPA_GRAN_100_US, GRAN_0_FMASK);
295 val |= u32_encode_bits(IPA_GRAN_1_MS, GRAN_1_FMASK);
296 val |= u32_encode_bits(IPA_GRAN_1_MS, GRAN_2_FMASK);
297 iowrite32(val, ipa->reg_virt + IPA_REG_TIMERS_PULSE_GRAN_CFG_OFFSET);
299 /* Actual divider is 1 more than value supplied here */
300 val = u32_encode_bits(IPA_XO_CLOCK_DIVIDER - 1, DIV_VALUE_FMASK);
301 iowrite32(val, ipa->reg_virt + IPA_REG_TIMERS_XO_CLK_DIV_CFG_OFFSET);
303 /* Divider value is set; re-enable the common timer clock divider */
304 val |= u32_encode_bits(1, DIV_ENABLE_FMASK);
305 iowrite32(val, ipa->reg_virt + IPA_REG_TIMERS_XO_CLK_DIV_CFG_OFFSET);
308 static void ipa_idle_indication_cfg(struct ipa *ipa,
309 u32 enter_idle_debounce_thresh,
310 bool const_non_idle_enable)
315 val = u32_encode_bits(enter_idle_debounce_thresh,
316 ENTER_IDLE_DEBOUNCE_THRESH_FMASK);
317 if (const_non_idle_enable)
318 val |= CONST_NON_IDLE_ENABLE_FMASK;
320 offset = ipa_reg_idle_indication_cfg_offset(ipa->version);
321 iowrite32(val, ipa->reg_virt + offset);
325 * ipa_hardware_dcd_config() - Enable dynamic clock division on IPA
328 * Configures when the IPA signals it is idle to the global clock
329 * controller, which can respond by scaling down the clock to save
332 static void ipa_hardware_dcd_config(struct ipa *ipa)
334 /* Recommended values for IPA 3.5 and later according to IPA HPG */
335 ipa_idle_indication_cfg(ipa, 256, false);
338 static void ipa_hardware_dcd_deconfig(struct ipa *ipa)
340 /* Power-on reset values */
341 ipa_idle_indication_cfg(ipa, 0, true);
345 * ipa_hardware_config() - Primitive hardware initialization
347 * @data: IPA configuration data
349 static void ipa_hardware_config(struct ipa *ipa, const struct ipa_data *data)
351 enum ipa_version version = ipa->version;
355 /* IPA v4.5+ has no backward compatibility register */
356 if (version < IPA_VERSION_4_5) {
357 val = data->backward_compat;
358 iowrite32(val, ipa->reg_virt + IPA_REG_BCR_OFFSET);
361 /* Implement some hardware workarounds */
362 if (version >= IPA_VERSION_4_0 && version < IPA_VERSION_4_5) {
363 /* Disable PA mask to allow HOLB drop */
364 val = ioread32(ipa->reg_virt + IPA_REG_TX_CFG_OFFSET);
365 val &= ~PA_MASK_EN_FMASK;
366 iowrite32(val, ipa->reg_virt + IPA_REG_TX_CFG_OFFSET);
368 /* Enable open global clocks in the CLKON configuration */
369 val = GLOBAL_FMASK | GLOBAL_2X_CLK_FMASK;
370 } else if (version == IPA_VERSION_3_1) {
371 val = MISC_FMASK; /* Disable MISC clock gating */
373 val = 0; /* No CLKON configuration needed */
376 iowrite32(val, ipa->reg_virt + IPA_REG_CLKON_CFG_OFFSET);
378 ipa_hardware_config_comp(ipa);
380 /* Configure system bus limits */
381 ipa_hardware_config_qsb(ipa, data);
383 if (version < IPA_VERSION_4_5) {
384 /* Configure aggregation timer granularity */
385 granularity = ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY);
386 val = u32_encode_bits(granularity, AGGR_GRANULARITY_FMASK);
387 iowrite32(val, ipa->reg_virt + IPA_REG_COUNTER_CFG_OFFSET);
389 ipa_qtime_config(ipa);
392 /* IPA v4.2 does not support hashed tables, so disable them */
393 if (version == IPA_VERSION_4_2) {
394 u32 offset = ipa_reg_filt_rout_hash_en_offset(version);
396 iowrite32(0, ipa->reg_virt + offset);
399 /* Enable dynamic clock division */
400 ipa_hardware_dcd_config(ipa);
404 * ipa_hardware_deconfig() - Inverse of ipa_hardware_config()
407 * This restores the power-on reset values (even if they aren't different)
409 static void ipa_hardware_deconfig(struct ipa *ipa)
411 /* Mostly we just leave things as we set them. */
412 ipa_hardware_dcd_deconfig(ipa);
416 * ipa_config() - Configure IPA hardware
418 * @data: IPA configuration data
420 * Perform initialization requiring IPA power to be enabled.
422 static int ipa_config(struct ipa *ipa, const struct ipa_data *data)
426 ipa_hardware_config(ipa, data);
428 ret = ipa_mem_config(ipa);
430 goto err_hardware_deconfig;
432 ipa->interrupt = ipa_interrupt_config(ipa);
433 if (IS_ERR(ipa->interrupt)) {
434 ret = PTR_ERR(ipa->interrupt);
435 ipa->interrupt = NULL;
436 goto err_mem_deconfig;
441 ret = ipa_endpoint_config(ipa);
443 goto err_uc_deconfig;
445 ipa_table_config(ipa); /* No deconfig required */
447 /* Assign resource limitation to each group; no deconfig required */
448 ret = ipa_resource_config(ipa, data->resource_data);
450 goto err_endpoint_deconfig;
452 ret = ipa_modem_config(ipa);
454 goto err_endpoint_deconfig;
458 err_endpoint_deconfig:
459 ipa_endpoint_deconfig(ipa);
461 ipa_uc_deconfig(ipa);
462 ipa_interrupt_deconfig(ipa->interrupt);
463 ipa->interrupt = NULL;
465 ipa_mem_deconfig(ipa);
466 err_hardware_deconfig:
467 ipa_hardware_deconfig(ipa);
473 * ipa_deconfig() - Inverse of ipa_config()
476 static void ipa_deconfig(struct ipa *ipa)
478 ipa_modem_deconfig(ipa);
479 ipa_endpoint_deconfig(ipa);
480 ipa_uc_deconfig(ipa);
481 ipa_interrupt_deconfig(ipa->interrupt);
482 ipa->interrupt = NULL;
483 ipa_mem_deconfig(ipa);
484 ipa_hardware_deconfig(ipa);
487 static int ipa_firmware_load(struct device *dev)
489 const struct firmware *fw;
490 struct device_node *node;
498 node = of_parse_phandle(dev->of_node, "memory-region", 0);
500 dev_err(dev, "DT error getting \"memory-region\" property\n");
504 ret = of_address_to_resource(node, 0, &res);
507 dev_err(dev, "error %d getting \"memory-region\" resource\n",
512 /* Use name from DTB if specified; use default for *any* error */
513 ret = of_property_read_string(dev->of_node, "firmware-name", &path);
515 dev_dbg(dev, "error %d getting \"firmware-name\" resource\n",
517 path = IPA_FW_PATH_DEFAULT;
520 ret = request_firmware(&fw, path, dev);
522 dev_err(dev, "error %d requesting \"%s\"\n", ret, path);
527 size = (size_t)resource_size(&res);
528 virt = memremap(phys, size, MEMREMAP_WC);
530 dev_err(dev, "unable to remap firmware memory\n");
532 goto out_release_firmware;
535 ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL);
537 dev_err(dev, "error %d loading \"%s\"\n", ret, path);
538 else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID)))
539 dev_err(dev, "error %d authenticating \"%s\"\n", ret, path);
542 out_release_firmware:
543 release_firmware(fw);
548 static const struct of_device_id ipa_match[] = {
550 .compatible = "qcom,msm8998-ipa",
551 .data = &ipa_data_v3_1,
554 .compatible = "qcom,sdm845-ipa",
555 .data = &ipa_data_v3_5_1,
558 .compatible = "qcom,sc7180-ipa",
559 .data = &ipa_data_v4_2,
562 .compatible = "qcom,sdx55-ipa",
563 .data = &ipa_data_v4_5,
566 .compatible = "qcom,sm8350-ipa",
567 .data = &ipa_data_v4_9,
570 .compatible = "qcom,sc7280-ipa",
571 .data = &ipa_data_v4_11,
575 MODULE_DEVICE_TABLE(of, ipa_match);
577 /* Check things that can be validated at build time. This just
578 * groups these things BUILD_BUG_ON() calls don't clutter the rest
581 static void ipa_validate_build(void)
583 /* At one time we assumed a 64-bit build, allowing some do_div()
584 * calls to be replaced by simple division or modulo operations.
585 * We currently only perform divide and modulo operations on u32,
586 * u16, or size_t objects, and of those only size_t has any chance
587 * of being a 64-bit value. (It should be guaranteed 32 bits wide
588 * on a 32-bit build, but there is no harm in verifying that.)
590 BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT) && sizeof(size_t) != 4);
592 /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */
593 BUILD_BUG_ON(GSI_EE_AP != 0);
595 /* There's no point if we have no channels or event rings */
596 BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX);
597 BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX);
599 /* GSI hardware design limits */
600 BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32);
601 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.
607 BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX);
609 /* This is used as a divisor */
610 BUILD_BUG_ON(!IPA_AGGR_GRANULARITY);
612 /* Aggregation granularity value can't be 0, and must fit */
613 BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY));
614 BUILD_BUG_ON(ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY) >
615 field_max(AGGR_GRANULARITY_FMASK));
618 static bool ipa_version_valid(enum ipa_version version)
621 case IPA_VERSION_3_0:
622 case IPA_VERSION_3_1:
623 case IPA_VERSION_3_5:
624 case IPA_VERSION_3_5_1:
625 case IPA_VERSION_4_0:
626 case IPA_VERSION_4_1:
627 case IPA_VERSION_4_2:
628 case IPA_VERSION_4_5:
629 case IPA_VERSION_4_7:
630 case IPA_VERSION_4_9:
631 case IPA_VERSION_4_11:
640 * ipa_probe() - IPA platform driver probe function
641 * @pdev: Platform device pointer
643 * Return: 0 if successful, or a negative error code (possibly
646 * This is the main entry point for the IPA driver. Initialization proceeds
648 * - The "init" stage involves activities that can be initialized without
649 * access to the IPA hardware.
650 * - The "config" stage requires IPA power to be active so IPA registers
651 * can be accessed, but does not require the use of IPA immediate commands.
652 * - The "setup" stage uses IPA immediate commands, and so requires the GSI
653 * layer to be initialized.
655 * A Boolean Device Tree "modem-init" property determines whether GSI
656 * initialization will be performed by the AP (Trust Zone) or the modem.
657 * If the AP does GSI initialization, the setup phase is entered after
658 * this has completed successfully. Otherwise the modem initializes
659 * the GSI layer and signals it has finished by sending an SMP2P interrupt
660 * to the AP; this triggers the start if IPA setup.
662 static int ipa_probe(struct platform_device *pdev)
664 struct device *dev = &pdev->dev;
665 const struct ipa_data *data;
666 struct ipa_power *power;
671 ipa_validate_build();
673 /* Get configuration data early; needed for power initialization */
674 data = of_device_get_match_data(dev);
676 dev_err(dev, "matched hardware not supported\n");
680 if (!ipa_version_valid(data->version)) {
681 dev_err(dev, "invalid IPA version\n");
685 /* If we need Trust Zone, make sure it's available */
686 modem_init = of_property_read_bool(dev->of_node, "modem-init");
688 if (!qcom_scm_is_available())
689 return -EPROBE_DEFER;
691 /* The clock and interconnects might not be ready when we're
692 * probed, so might return -EPROBE_DEFER.
694 power = ipa_power_init(dev, data->power_data);
696 return PTR_ERR(power);
698 /* No more EPROBE_DEFER. Allocate and initialize the IPA structure */
699 ipa = kzalloc(sizeof(*ipa), GFP_KERNEL);
706 dev_set_drvdata(dev, ipa);
708 ipa->version = data->version;
709 init_completion(&ipa->completion);
711 ret = ipa_reg_init(ipa);
715 ret = ipa_mem_init(ipa, data->mem_data);
719 ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count,
720 data->endpoint_data);
724 /* Result is a non-zero mask of endpoints that support filtering */
725 ipa->filter_map = ipa_endpoint_init(ipa, data->endpoint_count,
726 data->endpoint_data);
727 if (!ipa->filter_map) {
732 ret = ipa_table_init(ipa);
734 goto err_endpoint_exit;
736 ret = ipa_modem_init(ipa, modem_init);
740 /* Power needs to be active for config and setup */
741 ret = pm_runtime_get_sync(dev);
742 if (WARN_ON(ret < 0))
745 ret = ipa_config(ipa, data);
749 dev_info(dev, "IPA driver initialized");
751 /* If the modem is doing early initialization, it will trigger a
752 * call to ipa_setup() when it has finished. In that case we're
758 /* Otherwise we need to load the firmware and have Trust Zone validate
759 * and install it. If that succeeds we can proceed with setup.
761 ret = ipa_firmware_load(dev);
765 ret = ipa_setup(ipa);
769 pm_runtime_mark_last_busy(dev);
770 (void)pm_runtime_put_autosuspend(dev);
777 pm_runtime_put_noidle(dev);
782 ipa_endpoint_exit(ipa);
792 ipa_power_exit(power);
797 static int ipa_remove(struct platform_device *pdev)
799 struct ipa *ipa = dev_get_drvdata(&pdev->dev);
800 struct ipa_power *power = ipa->power;
801 struct device *dev = &pdev->dev;
804 ret = pm_runtime_get_sync(dev);
805 if (WARN_ON(ret < 0))
808 if (ipa->setup_complete) {
809 ret = ipa_modem_stop(ipa);
810 /* If starting or stopping is in progress, try once more */
812 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
813 ret = ipa_modem_stop(ipa);
823 pm_runtime_put_noidle(dev);
826 ipa_endpoint_exit(ipa);
831 ipa_power_exit(power);
836 static void ipa_shutdown(struct platform_device *pdev)
840 ret = ipa_remove(pdev);
842 dev_err(&pdev->dev, "shutdown: remove returned %d\n", ret);
845 static const struct attribute_group *ipa_attribute_groups[] = {
846 &ipa_attribute_group,
847 &ipa_feature_attribute_group,
848 &ipa_modem_attribute_group,
852 static struct platform_driver ipa_driver = {
854 .remove = ipa_remove,
855 .shutdown = ipa_shutdown,
859 .of_match_table = ipa_match,
860 .dev_groups = ipa_attribute_groups,
864 module_platform_driver(ipa_driver);
866 MODULE_LICENSE("GPL v2");
867 MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver");