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