Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  sst.c - Intel SST Driver for audio engine
0004  *
0005  *  Copyright (C) 2008-14   Intel Corp
0006  *  Authors:    Vinod Koul <vinod.koul@intel.com>
0007  *      Harsha Priya <priya.harsha@intel.com>
0008  *      Dharageswari R <dharageswari.r@intel.com>
0009  *      KP Jeeja <jeeja.kp@intel.com>
0010  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0011  *
0012  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0013  */
0014 #include <linux/module.h>
0015 #include <linux/fs.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/io.h>
0018 #include <linux/firmware.h>
0019 #include <linux/pm_runtime.h>
0020 #include <linux/pm_qos.h>
0021 #include <linux/async.h>
0022 #include <linux/acpi.h>
0023 #include <linux/sysfs.h>
0024 #include <sound/core.h>
0025 #include <sound/soc.h>
0026 #include <asm/platform_sst_audio.h>
0027 #include "../sst-mfld-platform.h"
0028 #include "sst.h"
0029 
0030 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
0031 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
0032 MODULE_DESCRIPTION("Intel (R) SST(R) Audio Engine Driver");
0033 MODULE_LICENSE("GPL v2");
0034 
0035 static inline bool sst_is_process_reply(u32 msg_id)
0036 {
0037     return ((msg_id & PROCESS_MSG) ? true : false);
0038 }
0039 
0040 static inline bool sst_validate_mailbox_size(unsigned int size)
0041 {
0042     return ((size <= SST_MAILBOX_SIZE) ? true : false);
0043 }
0044 
0045 static irqreturn_t intel_sst_interrupt_mrfld(int irq, void *context)
0046 {
0047     union interrupt_reg_mrfld isr;
0048     union ipc_header_mrfld header;
0049     union sst_imr_reg_mrfld imr;
0050     struct ipc_post *msg = NULL;
0051     unsigned int size;
0052     struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
0053     irqreturn_t retval = IRQ_HANDLED;
0054 
0055     /* Interrupt arrived, check src */
0056     isr.full = sst_shim_read64(drv->shim, SST_ISRX);
0057 
0058     if (isr.part.done_interrupt) {
0059         /* Clear done bit */
0060         spin_lock(&drv->ipc_spin_lock);
0061         header.full = sst_shim_read64(drv->shim,
0062                     drv->ipc_reg.ipcx);
0063         header.p.header_high.part.done = 0;
0064         sst_shim_write64(drv->shim, drv->ipc_reg.ipcx, header.full);
0065 
0066         /* write 1 to clear status register */;
0067         isr.part.done_interrupt = 1;
0068         sst_shim_write64(drv->shim, SST_ISRX, isr.full);
0069         spin_unlock(&drv->ipc_spin_lock);
0070 
0071         /* we can send more messages to DSP so trigger work */
0072         queue_work(drv->post_msg_wq, &drv->ipc_post_msg_wq);
0073         retval = IRQ_HANDLED;
0074     }
0075 
0076     if (isr.part.busy_interrupt) {
0077         /* message from dsp so copy that */
0078         spin_lock(&drv->ipc_spin_lock);
0079         imr.full = sst_shim_read64(drv->shim, SST_IMRX);
0080         imr.part.busy_interrupt = 1;
0081         sst_shim_write64(drv->shim, SST_IMRX, imr.full);
0082         spin_unlock(&drv->ipc_spin_lock);
0083         header.full =  sst_shim_read64(drv->shim, drv->ipc_reg.ipcd);
0084 
0085         if (sst_create_ipc_msg(&msg, header.p.header_high.part.large)) {
0086             drv->ops->clear_interrupt(drv);
0087             return IRQ_HANDLED;
0088         }
0089 
0090         if (header.p.header_high.part.large) {
0091             size = header.p.header_low_payload;
0092             if (sst_validate_mailbox_size(size)) {
0093                 memcpy_fromio(msg->mailbox_data,
0094                     drv->mailbox + drv->mailbox_recv_offset, size);
0095             } else {
0096                 dev_err(drv->dev,
0097                     "Mailbox not copied, payload size is: %u\n", size);
0098                 header.p.header_low_payload = 0;
0099             }
0100         }
0101 
0102         msg->mrfld_header = header;
0103         msg->is_process_reply =
0104             sst_is_process_reply(header.p.header_high.part.msg_id);
0105         spin_lock(&drv->rx_msg_lock);
0106         list_add_tail(&msg->node, &drv->rx_list);
0107         spin_unlock(&drv->rx_msg_lock);
0108         drv->ops->clear_interrupt(drv);
0109         retval = IRQ_WAKE_THREAD;
0110     }
0111     return retval;
0112 }
0113 
0114 static irqreturn_t intel_sst_irq_thread_mrfld(int irq, void *context)
0115 {
0116     struct intel_sst_drv *drv = (struct intel_sst_drv *) context;
0117     struct ipc_post *__msg, *msg;
0118     unsigned long irq_flags;
0119 
0120     spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
0121     if (list_empty(&drv->rx_list)) {
0122         spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
0123         return IRQ_HANDLED;
0124     }
0125 
0126     list_for_each_entry_safe(msg, __msg, &drv->rx_list, node) {
0127         list_del(&msg->node);
0128         spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
0129         if (msg->is_process_reply)
0130             drv->ops->process_message(msg);
0131         else
0132             drv->ops->process_reply(drv, msg);
0133 
0134         if (msg->is_large)
0135             kfree(msg->mailbox_data);
0136         kfree(msg);
0137         spin_lock_irqsave(&drv->rx_msg_lock, irq_flags);
0138     }
0139     spin_unlock_irqrestore(&drv->rx_msg_lock, irq_flags);
0140     return IRQ_HANDLED;
0141 }
0142 
0143 static int sst_save_dsp_context_v2(struct intel_sst_drv *sst)
0144 {
0145     int ret = 0;
0146 
0147     ret = sst_prepare_and_post_msg(sst, SST_TASK_ID_MEDIA, IPC_CMD,
0148             IPC_PREP_D3, PIPE_RSVD, 0, NULL, NULL,
0149             true, true, false, true);
0150 
0151     if (ret < 0) {
0152         dev_err(sst->dev, "not suspending FW!!, Err: %d\n", ret);
0153         return -EIO;
0154     }
0155 
0156     return 0;
0157 }
0158 
0159 
0160 static struct intel_sst_ops mrfld_ops = {
0161     .interrupt = intel_sst_interrupt_mrfld,
0162     .irq_thread = intel_sst_irq_thread_mrfld,
0163     .clear_interrupt = intel_sst_clear_intr_mrfld,
0164     .start = sst_start_mrfld,
0165     .reset = intel_sst_reset_dsp_mrfld,
0166     .post_message = sst_post_message_mrfld,
0167     .process_reply = sst_process_reply_mrfld,
0168     .save_dsp_context =  sst_save_dsp_context_v2,
0169     .alloc_stream = sst_alloc_stream_mrfld,
0170     .post_download = sst_post_download_mrfld,
0171 };
0172 
0173 int sst_driver_ops(struct intel_sst_drv *sst)
0174 {
0175 
0176     switch (sst->dev_id) {
0177     case SST_MRFLD_PCI_ID:
0178     case SST_BYT_ACPI_ID:
0179     case SST_CHV_ACPI_ID:
0180         sst->tstamp = SST_TIME_STAMP_MRFLD;
0181         sst->ops = &mrfld_ops;
0182         return 0;
0183 
0184     default:
0185         dev_err(sst->dev,
0186             "SST Driver capabilities missing for dev_id: %x",
0187             sst->dev_id);
0188         return -EINVAL;
0189     }
0190 }
0191 
0192 void sst_process_pending_msg(struct work_struct *work)
0193 {
0194     struct intel_sst_drv *ctx = container_of(work,
0195             struct intel_sst_drv, ipc_post_msg_wq);
0196 
0197     ctx->ops->post_message(ctx, NULL, false);
0198 }
0199 
0200 static int sst_workqueue_init(struct intel_sst_drv *ctx)
0201 {
0202     INIT_LIST_HEAD(&ctx->memcpy_list);
0203     INIT_LIST_HEAD(&ctx->rx_list);
0204     INIT_LIST_HEAD(&ctx->ipc_dispatch_list);
0205     INIT_LIST_HEAD(&ctx->block_list);
0206     INIT_WORK(&ctx->ipc_post_msg_wq, sst_process_pending_msg);
0207     init_waitqueue_head(&ctx->wait_queue);
0208 
0209     ctx->post_msg_wq =
0210         create_singlethread_workqueue("sst_post_msg_wq");
0211     if (!ctx->post_msg_wq)
0212         return -EBUSY;
0213     return 0;
0214 }
0215 
0216 static void sst_init_locks(struct intel_sst_drv *ctx)
0217 {
0218     mutex_init(&ctx->sst_lock);
0219     spin_lock_init(&ctx->rx_msg_lock);
0220     spin_lock_init(&ctx->ipc_spin_lock);
0221     spin_lock_init(&ctx->block_lock);
0222 }
0223 
0224 int sst_alloc_drv_context(struct intel_sst_drv **ctx,
0225         struct device *dev, unsigned int dev_id)
0226 {
0227     *ctx = devm_kzalloc(dev, sizeof(struct intel_sst_drv), GFP_KERNEL);
0228     if (!(*ctx))
0229         return -ENOMEM;
0230 
0231     (*ctx)->dev = dev;
0232     (*ctx)->dev_id = dev_id;
0233 
0234     return 0;
0235 }
0236 EXPORT_SYMBOL_GPL(sst_alloc_drv_context);
0237 
0238 static ssize_t firmware_version_show(struct device *dev,
0239                 struct device_attribute *attr, char *buf)
0240 {
0241     struct intel_sst_drv *ctx = dev_get_drvdata(dev);
0242 
0243     if (ctx->fw_version.type == 0 && ctx->fw_version.major == 0 &&
0244         ctx->fw_version.minor == 0 && ctx->fw_version.build == 0)
0245         return sprintf(buf, "FW not yet loaded\n");
0246     else
0247         return sprintf(buf, "v%02x.%02x.%02x.%02x\n",
0248                    ctx->fw_version.type, ctx->fw_version.major,
0249                    ctx->fw_version.minor, ctx->fw_version.build);
0250 
0251 }
0252 
0253 static DEVICE_ATTR_RO(firmware_version);
0254 
0255 static const struct attribute *sst_fw_version_attrs[] = {
0256     &dev_attr_firmware_version.attr,
0257     NULL,
0258 };
0259 
0260 static const struct attribute_group sst_fw_version_attr_group = {
0261     .attrs = (struct attribute **)sst_fw_version_attrs,
0262 };
0263 
0264 int sst_context_init(struct intel_sst_drv *ctx)
0265 {
0266     int ret = 0, i;
0267 
0268     if (!ctx->pdata)
0269         return -EINVAL;
0270 
0271     if (!ctx->pdata->probe_data)
0272         return -EINVAL;
0273 
0274     memcpy(&ctx->info, ctx->pdata->probe_data, sizeof(ctx->info));
0275 
0276     ret = sst_driver_ops(ctx);
0277     if (ret != 0)
0278         return -EINVAL;
0279 
0280     sst_init_locks(ctx);
0281     sst_set_fw_state_locked(ctx, SST_RESET);
0282 
0283     /* pvt_id 0 reserved for async messages */
0284     ctx->pvt_id = 1;
0285     ctx->stream_cnt = 0;
0286     ctx->fw_in_mem = NULL;
0287     /* we use memcpy, so set to 0 */
0288     ctx->use_dma = 0;
0289     ctx->use_lli = 0;
0290 
0291     if (sst_workqueue_init(ctx))
0292         return -EINVAL;
0293 
0294     ctx->mailbox_recv_offset = ctx->pdata->ipc_info->mbox_recv_off;
0295     ctx->ipc_reg.ipcx = SST_IPCX + ctx->pdata->ipc_info->ipc_offset;
0296     ctx->ipc_reg.ipcd = SST_IPCD + ctx->pdata->ipc_info->ipc_offset;
0297 
0298     dev_info(ctx->dev, "Got drv data max stream %d\n",
0299                 ctx->info.max_streams);
0300 
0301     for (i = 1; i <= ctx->info.max_streams; i++) {
0302         struct stream_info *stream = &ctx->streams[i];
0303 
0304         memset(stream, 0, sizeof(*stream));
0305         stream->pipe_id = PIPE_RSVD;
0306         mutex_init(&stream->lock);
0307     }
0308 
0309     /* Register the ISR */
0310     ret = devm_request_threaded_irq(ctx->dev, ctx->irq_num, ctx->ops->interrupt,
0311                     ctx->ops->irq_thread, 0, SST_DRV_NAME,
0312                     ctx);
0313     if (ret)
0314         goto do_free_mem;
0315 
0316     dev_dbg(ctx->dev, "Registered IRQ %#x\n", ctx->irq_num);
0317 
0318     /* default intr are unmasked so set this as masked */
0319     sst_shim_write64(ctx->shim, SST_IMRX, 0xFFFF0038);
0320 
0321     ctx->qos = devm_kzalloc(ctx->dev,
0322         sizeof(struct pm_qos_request), GFP_KERNEL);
0323     if (!ctx->qos) {
0324         ret = -ENOMEM;
0325         goto do_free_mem;
0326     }
0327     cpu_latency_qos_add_request(ctx->qos, PM_QOS_DEFAULT_VALUE);
0328 
0329     dev_dbg(ctx->dev, "Requesting FW %s now...\n", ctx->firmware_name);
0330     ret = request_firmware_nowait(THIS_MODULE, true, ctx->firmware_name,
0331                       ctx->dev, GFP_KERNEL, ctx, sst_firmware_load_cb);
0332     if (ret) {
0333         dev_err(ctx->dev, "Firmware download failed:%d\n", ret);
0334         goto do_free_mem;
0335     }
0336 
0337     ret = sysfs_create_group(&ctx->dev->kobj,
0338                  &sst_fw_version_attr_group);
0339     if (ret) {
0340         dev_err(ctx->dev,
0341             "Unable to create sysfs\n");
0342         goto err_sysfs;
0343     }
0344 
0345     sst_register(ctx->dev);
0346     return 0;
0347 err_sysfs:
0348     sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
0349 
0350 do_free_mem:
0351     destroy_workqueue(ctx->post_msg_wq);
0352     return ret;
0353 }
0354 EXPORT_SYMBOL_GPL(sst_context_init);
0355 
0356 void sst_context_cleanup(struct intel_sst_drv *ctx)
0357 {
0358     pm_runtime_get_noresume(ctx->dev);
0359     pm_runtime_disable(ctx->dev);
0360     sst_unregister(ctx->dev);
0361     sst_set_fw_state_locked(ctx, SST_SHUTDOWN);
0362     sysfs_remove_group(&ctx->dev->kobj, &sst_fw_version_attr_group);
0363     destroy_workqueue(ctx->post_msg_wq);
0364     cpu_latency_qos_remove_request(ctx->qos);
0365     kfree(ctx->fw_sg_list.src);
0366     kfree(ctx->fw_sg_list.dst);
0367     ctx->fw_sg_list.list_len = 0;
0368     kfree(ctx->fw_in_mem);
0369     ctx->fw_in_mem = NULL;
0370     sst_memcpy_free_resources(ctx);
0371 }
0372 EXPORT_SYMBOL_GPL(sst_context_cleanup);
0373 
0374 void sst_configure_runtime_pm(struct intel_sst_drv *ctx)
0375 {
0376     pm_runtime_set_autosuspend_delay(ctx->dev, SST_SUSPEND_DELAY);
0377     pm_runtime_use_autosuspend(ctx->dev);
0378     /*
0379      * For acpi devices, the actual physical device state is
0380      * initially active. So change the state to active before
0381      * enabling the pm
0382      */
0383 
0384     if (!acpi_disabled)
0385         pm_runtime_set_active(ctx->dev);
0386 
0387     pm_runtime_enable(ctx->dev);
0388 
0389     if (acpi_disabled)
0390         pm_runtime_set_active(ctx->dev);
0391     else
0392         pm_runtime_put_noidle(ctx->dev);
0393 }
0394 EXPORT_SYMBOL_GPL(sst_configure_runtime_pm);
0395 
0396 static int intel_sst_runtime_suspend(struct device *dev)
0397 {
0398     int ret = 0;
0399     struct intel_sst_drv *ctx = dev_get_drvdata(dev);
0400 
0401     if (ctx->sst_state == SST_RESET) {
0402         dev_dbg(dev, "LPE is already in RESET state, No action\n");
0403         return 0;
0404     }
0405     /* save fw context */
0406     if (ctx->ops->save_dsp_context(ctx))
0407         return -EBUSY;
0408 
0409     /* Move the SST state to Reset */
0410     sst_set_fw_state_locked(ctx, SST_RESET);
0411 
0412     synchronize_irq(ctx->irq_num);
0413     flush_workqueue(ctx->post_msg_wq);
0414 
0415     ctx->ops->reset(ctx);
0416 
0417     return ret;
0418 }
0419 
0420 static int intel_sst_suspend(struct device *dev)
0421 {
0422     struct intel_sst_drv *ctx = dev_get_drvdata(dev);
0423     struct sst_fw_save *fw_save;
0424     int i, ret;
0425 
0426     /* check first if we are already in SW reset */
0427     if (ctx->sst_state == SST_RESET)
0428         return 0;
0429 
0430     /*
0431      * check if any stream is active and running
0432      * they should already by suspend by soc_suspend
0433      */
0434     for (i = 1; i <= ctx->info.max_streams; i++) {
0435         struct stream_info *stream = &ctx->streams[i];
0436 
0437         if (stream->status == STREAM_RUNNING) {
0438             dev_err(dev, "stream %d is running, can't suspend, abort\n", i);
0439             return -EBUSY;
0440         }
0441 
0442         if (ctx->pdata->streams_lost_on_suspend) {
0443             stream->resume_status = stream->status;
0444             stream->resume_prev = stream->prev;
0445             if (stream->status != STREAM_UN_INIT)
0446                 sst_free_stream(ctx, i);
0447         }
0448     }
0449     synchronize_irq(ctx->irq_num);
0450     flush_workqueue(ctx->post_msg_wq);
0451 
0452     /* Move the SST state to Reset */
0453     sst_set_fw_state_locked(ctx, SST_RESET);
0454 
0455     /* tell DSP we are suspending */
0456     if (ctx->ops->save_dsp_context(ctx))
0457         return -EBUSY;
0458 
0459     /* save the memories */
0460     fw_save = kzalloc(sizeof(*fw_save), GFP_KERNEL);
0461     if (!fw_save)
0462         return -ENOMEM;
0463     fw_save->iram = kvzalloc(ctx->iram_end - ctx->iram_base, GFP_KERNEL);
0464     if (!fw_save->iram) {
0465         ret = -ENOMEM;
0466         goto iram;
0467     }
0468     fw_save->dram = kvzalloc(ctx->dram_end - ctx->dram_base, GFP_KERNEL);
0469     if (!fw_save->dram) {
0470         ret = -ENOMEM;
0471         goto dram;
0472     }
0473     fw_save->sram = kvzalloc(SST_MAILBOX_SIZE, GFP_KERNEL);
0474     if (!fw_save->sram) {
0475         ret = -ENOMEM;
0476         goto sram;
0477     }
0478 
0479     fw_save->ddr = kvzalloc(ctx->ddr_end - ctx->ddr_base, GFP_KERNEL);
0480     if (!fw_save->ddr) {
0481         ret = -ENOMEM;
0482         goto ddr;
0483     }
0484 
0485     memcpy32_fromio(fw_save->iram, ctx->iram, ctx->iram_end - ctx->iram_base);
0486     memcpy32_fromio(fw_save->dram, ctx->dram, ctx->dram_end - ctx->dram_base);
0487     memcpy32_fromio(fw_save->sram, ctx->mailbox, SST_MAILBOX_SIZE);
0488     memcpy32_fromio(fw_save->ddr, ctx->ddr, ctx->ddr_end - ctx->ddr_base);
0489 
0490     ctx->fw_save = fw_save;
0491     ctx->ops->reset(ctx);
0492     return 0;
0493 ddr:
0494     kvfree(fw_save->sram);
0495 sram:
0496     kvfree(fw_save->dram);
0497 dram:
0498     kvfree(fw_save->iram);
0499 iram:
0500     kfree(fw_save);
0501     return ret;
0502 }
0503 
0504 static int intel_sst_resume(struct device *dev)
0505 {
0506     struct intel_sst_drv *ctx = dev_get_drvdata(dev);
0507     struct sst_fw_save *fw_save = ctx->fw_save;
0508     struct sst_block *block;
0509     int i, ret = 0;
0510 
0511     if (!fw_save)
0512         return 0;
0513 
0514     sst_set_fw_state_locked(ctx, SST_FW_LOADING);
0515 
0516     /* we have to restore the memory saved */
0517     ctx->ops->reset(ctx);
0518 
0519     ctx->fw_save = NULL;
0520 
0521     memcpy32_toio(ctx->iram, fw_save->iram, ctx->iram_end - ctx->iram_base);
0522     memcpy32_toio(ctx->dram, fw_save->dram, ctx->dram_end - ctx->dram_base);
0523     memcpy32_toio(ctx->mailbox, fw_save->sram, SST_MAILBOX_SIZE);
0524     memcpy32_toio(ctx->ddr, fw_save->ddr, ctx->ddr_end - ctx->ddr_base);
0525 
0526     kvfree(fw_save->sram);
0527     kvfree(fw_save->dram);
0528     kvfree(fw_save->iram);
0529     kvfree(fw_save->ddr);
0530     kfree(fw_save);
0531 
0532     block = sst_create_block(ctx, 0, FW_DWNL_ID);
0533     if (block == NULL)
0534         return -ENOMEM;
0535 
0536 
0537     /* start and wait for ack */
0538     ctx->ops->start(ctx);
0539     ret = sst_wait_timeout(ctx, block);
0540     if (ret) {
0541         dev_err(ctx->dev, "fw download failed %d\n", ret);
0542         /* FW download failed due to timeout */
0543         ret = -EBUSY;
0544 
0545     } else {
0546         sst_set_fw_state_locked(ctx, SST_FW_RUNNING);
0547     }
0548 
0549     if (ctx->pdata->streams_lost_on_suspend) {
0550         for (i = 1; i <= ctx->info.max_streams; i++) {
0551             struct stream_info *stream = &ctx->streams[i];
0552 
0553             if (stream->resume_status != STREAM_UN_INIT) {
0554                 dev_dbg(ctx->dev, "Re-allocing stream %d status %d prev %d\n",
0555                     i, stream->resume_status,
0556                     stream->resume_prev);
0557                 sst_realloc_stream(ctx, i);
0558                 stream->status = stream->resume_status;
0559                 stream->prev = stream->resume_prev;
0560             }
0561         }
0562     }
0563 
0564     sst_free_block(ctx, block);
0565     return ret;
0566 }
0567 
0568 const struct dev_pm_ops intel_sst_pm = {
0569     .suspend = intel_sst_suspend,
0570     .resume = intel_sst_resume,
0571     .runtime_suspend = intel_sst_runtime_suspend,
0572 };
0573 EXPORT_SYMBOL_GPL(intel_sst_pm);