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