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