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