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