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