Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
0004  * Copyright (C) 2018-2021 Linaro Ltd.
0005  */
0006 
0007 #include <linux/clk.h>
0008 #include <linux/device.h>
0009 #include <linux/interconnect.h>
0010 #include <linux/pm.h>
0011 #include <linux/pm_runtime.h>
0012 #include <linux/bitops.h>
0013 
0014 #include "linux/soc/qcom/qcom_aoss.h"
0015 
0016 #include "ipa.h"
0017 #include "ipa_power.h"
0018 #include "ipa_endpoint.h"
0019 #include "ipa_modem.h"
0020 #include "ipa_data.h"
0021 
0022 /**
0023  * DOC: IPA Power Management
0024  *
0025  * The IPA hardware is enabled when the IPA core clock and all the
0026  * interconnects (buses) it depends on are enabled.  Runtime power
0027  * management is used to determine whether the core clock and
0028  * interconnects are enabled, and if not in use to be suspended
0029  * automatically.
0030  *
0031  * The core clock currently runs at a fixed clock rate when enabled,
0032  * an all interconnects use a fixed average and peak bandwidth.
0033  */
0034 
0035 #define IPA_AUTOSUSPEND_DELAY   500 /* milliseconds */
0036 
0037 /**
0038  * enum ipa_power_flag - IPA power flags
0039  * @IPA_POWER_FLAG_RESUMED: Whether resume from suspend has been signaled
0040  * @IPA_POWER_FLAG_SYSTEM:  Hardware is system (not runtime) suspended
0041  * @IPA_POWER_FLAG_STOPPED: Modem TX is disabled by ipa_start_xmit()
0042  * @IPA_POWER_FLAG_STARTED: Modem TX was enabled by ipa_runtime_resume()
0043  * @IPA_POWER_FLAG_COUNT:   Number of defined power flags
0044  */
0045 enum ipa_power_flag {
0046     IPA_POWER_FLAG_RESUMED,
0047     IPA_POWER_FLAG_SYSTEM,
0048     IPA_POWER_FLAG_STOPPED,
0049     IPA_POWER_FLAG_STARTED,
0050     IPA_POWER_FLAG_COUNT,       /* Last; not a flag */
0051 };
0052 
0053 /**
0054  * struct ipa_power - IPA power management information
0055  * @dev:        IPA device pointer
0056  * @core:       IPA core clock
0057  * @qmp:        QMP handle for AOSS communication
0058  * @spinlock:       Protects modem TX queue enable/disable
0059  * @flags:      Boolean state flags
0060  * @interconnect_count: Number of elements in interconnect[]
0061  * @interconnect:   Interconnect array
0062  */
0063 struct ipa_power {
0064     struct device *dev;
0065     struct clk *core;
0066     struct qmp *qmp;
0067     spinlock_t spinlock;    /* used with STOPPED/STARTED power flags */
0068     DECLARE_BITMAP(flags, IPA_POWER_FLAG_COUNT);
0069     u32 interconnect_count;
0070     struct icc_bulk_data interconnect[];
0071 };
0072 
0073 /* Initialize interconnects required for IPA operation */
0074 static int ipa_interconnect_init(struct ipa_power *power,
0075                  const struct ipa_interconnect_data *data)
0076 {
0077     struct icc_bulk_data *interconnect;
0078     int ret;
0079     u32 i;
0080 
0081     /* Initialize our interconnect data array for bulk operations */
0082     interconnect = &power->interconnect[0];
0083     for (i = 0; i < power->interconnect_count; i++) {
0084         /* interconnect->path is filled in by of_icc_bulk_get() */
0085         interconnect->name = data->name;
0086         interconnect->avg_bw = data->average_bandwidth;
0087         interconnect->peak_bw = data->peak_bandwidth;
0088         data++;
0089         interconnect++;
0090     }
0091 
0092     ret = of_icc_bulk_get(power->dev, power->interconnect_count,
0093                   power->interconnect);
0094     if (ret)
0095         return ret;
0096 
0097     /* All interconnects are initially disabled */
0098     icc_bulk_disable(power->interconnect_count, power->interconnect);
0099 
0100     /* Set the bandwidth values to be used when enabled */
0101     ret = icc_bulk_set_bw(power->interconnect_count, power->interconnect);
0102     if (ret)
0103         icc_bulk_put(power->interconnect_count, power->interconnect);
0104 
0105     return ret;
0106 }
0107 
0108 /* Inverse of ipa_interconnect_init() */
0109 static void ipa_interconnect_exit(struct ipa_power *power)
0110 {
0111     icc_bulk_put(power->interconnect_count, power->interconnect);
0112 }
0113 
0114 /* Enable IPA power, enabling interconnects and the core clock */
0115 static int ipa_power_enable(struct ipa *ipa)
0116 {
0117     struct ipa_power *power = ipa->power;
0118     int ret;
0119 
0120     ret = icc_bulk_enable(power->interconnect_count, power->interconnect);
0121     if (ret)
0122         return ret;
0123 
0124     ret = clk_prepare_enable(power->core);
0125     if (ret) {
0126         dev_err(power->dev, "error %d enabling core clock\n", ret);
0127         icc_bulk_disable(power->interconnect_count,
0128                  power->interconnect);
0129     }
0130 
0131     return ret;
0132 }
0133 
0134 /* Inverse of ipa_power_enable() */
0135 static void ipa_power_disable(struct ipa *ipa)
0136 {
0137     struct ipa_power *power = ipa->power;
0138 
0139     clk_disable_unprepare(power->core);
0140 
0141     icc_bulk_disable(power->interconnect_count, power->interconnect);
0142 }
0143 
0144 static int ipa_runtime_suspend(struct device *dev)
0145 {
0146     struct ipa *ipa = dev_get_drvdata(dev);
0147 
0148     /* Endpoints aren't usable until setup is complete */
0149     if (ipa->setup_complete) {
0150         __clear_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags);
0151         ipa_endpoint_suspend(ipa);
0152         gsi_suspend(&ipa->gsi);
0153     }
0154 
0155     ipa_power_disable(ipa);
0156 
0157     return 0;
0158 }
0159 
0160 static int ipa_runtime_resume(struct device *dev)
0161 {
0162     struct ipa *ipa = dev_get_drvdata(dev);
0163     int ret;
0164 
0165     ret = ipa_power_enable(ipa);
0166     if (WARN_ON(ret < 0))
0167         return ret;
0168 
0169     /* Endpoints aren't usable until setup is complete */
0170     if (ipa->setup_complete) {
0171         gsi_resume(&ipa->gsi);
0172         ipa_endpoint_resume(ipa);
0173     }
0174 
0175     return 0;
0176 }
0177 
0178 static int ipa_suspend(struct device *dev)
0179 {
0180     struct ipa *ipa = dev_get_drvdata(dev);
0181 
0182     __set_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
0183 
0184     return pm_runtime_force_suspend(dev);
0185 }
0186 
0187 static int ipa_resume(struct device *dev)
0188 {
0189     struct ipa *ipa = dev_get_drvdata(dev);
0190     int ret;
0191 
0192     ret = pm_runtime_force_resume(dev);
0193 
0194     __clear_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags);
0195 
0196     return ret;
0197 }
0198 
0199 /* Return the current IPA core clock rate */
0200 u32 ipa_core_clock_rate(struct ipa *ipa)
0201 {
0202     return ipa->power ? (u32)clk_get_rate(ipa->power->core) : 0;
0203 }
0204 
0205 /**
0206  * ipa_suspend_handler() - Handle the suspend IPA interrupt
0207  * @ipa:    IPA pointer
0208  * @irq_id: IPA interrupt type (unused)
0209  *
0210  * If an RX endpoint is suspended, and the IPA has a packet destined for
0211  * that endpoint, the IPA generates a SUSPEND interrupt to inform the AP
0212  * that it should resume the endpoint.  If we get one of these interrupts
0213  * we just wake up the system.
0214  */
0215 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id)
0216 {
0217     /* To handle an IPA interrupt we will have resumed the hardware
0218      * just to handle the interrupt, so we're done.  If we are in a
0219      * system suspend, trigger a system resume.
0220      */
0221     if (!__test_and_set_bit(IPA_POWER_FLAG_RESUMED, ipa->power->flags))
0222         if (test_bit(IPA_POWER_FLAG_SYSTEM, ipa->power->flags))
0223             pm_wakeup_dev_event(&ipa->pdev->dev, 0, true);
0224 
0225     /* Acknowledge/clear the suspend interrupt on all endpoints */
0226     ipa_interrupt_suspend_clear_all(ipa->interrupt);
0227 }
0228 
0229 /* The next few functions coordinate stopping and starting the modem
0230  * network device transmit queue.
0231  *
0232  * Transmit can be running concurrent with power resume, and there's a
0233  * chance the resume completes before the transmit path stops the queue,
0234  * leaving the queue in a stopped state.  The next two functions are used
0235  * to avoid this: ipa_power_modem_queue_stop() is used by ipa_start_xmit()
0236  * to conditionally stop the TX queue; and ipa_power_modem_queue_start()
0237  * is used by ipa_runtime_resume() to conditionally restart it.
0238  *
0239  * Two flags and a spinlock are used.  If the queue is stopped, the STOPPED
0240  * power flag is set.  And if the queue is started, the STARTED flag is set.
0241  * The queue is only started on resume if the STOPPED flag is set.  And the
0242  * queue is only started in ipa_start_xmit() if the STARTED flag is *not*
0243  * set.  As a result, the queue remains operational if the two activites
0244  * happen concurrently regardless of the order they complete.  The spinlock
0245  * ensures the flag and TX queue operations are done atomically.
0246  *
0247  * The first function stops the modem netdev transmit queue, but only if
0248  * the STARTED flag is *not* set.  That flag is cleared if it was set.
0249  * If the queue is stopped, the STOPPED flag is set.  This is called only
0250  * from the power ->runtime_resume operation.
0251  */
0252 void ipa_power_modem_queue_stop(struct ipa *ipa)
0253 {
0254     struct ipa_power *power = ipa->power;
0255     unsigned long flags;
0256 
0257     spin_lock_irqsave(&power->spinlock, flags);
0258 
0259     if (!__test_and_clear_bit(IPA_POWER_FLAG_STARTED, power->flags)) {
0260         netif_stop_queue(ipa->modem_netdev);
0261         __set_bit(IPA_POWER_FLAG_STOPPED, power->flags);
0262     }
0263 
0264     spin_unlock_irqrestore(&power->spinlock, flags);
0265 }
0266 
0267 /* This function starts the modem netdev transmit queue, but only if the
0268  * STOPPED flag is set.  That flag is cleared if it was set.  If the queue
0269  * was restarted, the STARTED flag is set; this allows ipa_start_xmit()
0270  * to skip stopping the queue in the event of a race.
0271  */
0272 void ipa_power_modem_queue_wake(struct ipa *ipa)
0273 {
0274     struct ipa_power *power = ipa->power;
0275     unsigned long flags;
0276 
0277     spin_lock_irqsave(&power->spinlock, flags);
0278 
0279     if (__test_and_clear_bit(IPA_POWER_FLAG_STOPPED, power->flags)) {
0280         __set_bit(IPA_POWER_FLAG_STARTED, power->flags);
0281         netif_wake_queue(ipa->modem_netdev);
0282     }
0283 
0284     spin_unlock_irqrestore(&power->spinlock, flags);
0285 }
0286 
0287 /* This function clears the STARTED flag once the TX queue is operating */
0288 void ipa_power_modem_queue_active(struct ipa *ipa)
0289 {
0290     clear_bit(IPA_POWER_FLAG_STARTED, ipa->power->flags);
0291 }
0292 
0293 static int ipa_power_retention_init(struct ipa_power *power)
0294 {
0295     struct qmp *qmp = qmp_get(power->dev);
0296 
0297     if (IS_ERR(qmp)) {
0298         if (PTR_ERR(qmp) == -EPROBE_DEFER)
0299             return -EPROBE_DEFER;
0300 
0301         /* We assume any other error means it's not defined/needed */
0302         qmp = NULL;
0303     }
0304     power->qmp = qmp;
0305 
0306     return 0;
0307 }
0308 
0309 static void ipa_power_retention_exit(struct ipa_power *power)
0310 {
0311     qmp_put(power->qmp);
0312     power->qmp = NULL;
0313 }
0314 
0315 /* Control register retention on power collapse */
0316 void ipa_power_retention(struct ipa *ipa, bool enable)
0317 {
0318     static const char fmt[] = "{ class: bcm, res: ipa_pc, val: %c }";
0319     struct ipa_power *power = ipa->power;
0320     char buf[36];   /* Exactly enough for fmt[]; size a multiple of 4 */
0321     int ret;
0322 
0323     if (!power->qmp)
0324         return;     /* Not needed on this platform */
0325 
0326     (void)snprintf(buf, sizeof(buf), fmt, enable ? '1' : '0');
0327 
0328     ret = qmp_send(power->qmp, buf, sizeof(buf));
0329     if (ret)
0330         dev_err(power->dev, "error %d sending QMP %sable request\n",
0331             ret, enable ? "en" : "dis");
0332 }
0333 
0334 int ipa_power_setup(struct ipa *ipa)
0335 {
0336     int ret;
0337 
0338     ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND,
0339               ipa_suspend_handler);
0340 
0341     ret = device_init_wakeup(&ipa->pdev->dev, true);
0342     if (ret)
0343         ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
0344 
0345     return ret;
0346 }
0347 
0348 void ipa_power_teardown(struct ipa *ipa)
0349 {
0350     (void)device_init_wakeup(&ipa->pdev->dev, false);
0351     ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND);
0352 }
0353 
0354 /* Initialize IPA power management */
0355 struct ipa_power *
0356 ipa_power_init(struct device *dev, const struct ipa_power_data *data)
0357 {
0358     struct ipa_power *power;
0359     struct clk *clk;
0360     size_t size;
0361     int ret;
0362 
0363     clk = clk_get(dev, "core");
0364     if (IS_ERR(clk)) {
0365         dev_err_probe(dev, PTR_ERR(clk), "error getting core clock\n");
0366 
0367         return ERR_CAST(clk);
0368     }
0369 
0370     ret = clk_set_rate(clk, data->core_clock_rate);
0371     if (ret) {
0372         dev_err(dev, "error %d setting core clock rate to %u\n",
0373             ret, data->core_clock_rate);
0374         goto err_clk_put;
0375     }
0376 
0377     size = struct_size(power, interconnect, data->interconnect_count);
0378     power = kzalloc(size, GFP_KERNEL);
0379     if (!power) {
0380         ret = -ENOMEM;
0381         goto err_clk_put;
0382     }
0383     power->dev = dev;
0384     power->core = clk;
0385     spin_lock_init(&power->spinlock);
0386     power->interconnect_count = data->interconnect_count;
0387 
0388     ret = ipa_interconnect_init(power, data->interconnect_data);
0389     if (ret)
0390         goto err_kfree;
0391 
0392     ret = ipa_power_retention_init(power);
0393     if (ret)
0394         goto err_interconnect_exit;
0395 
0396     pm_runtime_set_autosuspend_delay(dev, IPA_AUTOSUSPEND_DELAY);
0397     pm_runtime_use_autosuspend(dev);
0398     pm_runtime_enable(dev);
0399 
0400     return power;
0401 
0402 err_interconnect_exit:
0403     ipa_interconnect_exit(power);
0404 err_kfree:
0405     kfree(power);
0406 err_clk_put:
0407     clk_put(clk);
0408 
0409     return ERR_PTR(ret);
0410 }
0411 
0412 /* Inverse of ipa_power_init() */
0413 void ipa_power_exit(struct ipa_power *power)
0414 {
0415     struct device *dev = power->dev;
0416     struct clk *clk = power->core;
0417 
0418     pm_runtime_disable(dev);
0419     pm_runtime_dont_use_autosuspend(dev);
0420     ipa_power_retention_exit(power);
0421     ipa_interconnect_exit(power);
0422     kfree(power);
0423     clk_put(clk);
0424 }
0425 
0426 const struct dev_pm_ops ipa_pm_ops = {
0427     .suspend        = ipa_suspend,
0428     .resume         = ipa_resume,
0429     .runtime_suspend    = ipa_runtime_suspend,
0430     .runtime_resume     = ipa_runtime_resume,
0431 };