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