linux/drivers/crypto/ccp/ccp-platform.c
<<
>>
Prefs
   1/*
   2 * AMD Cryptographic Coprocessor (CCP) driver
   3 *
   4 * Copyright (C) 2014 Advanced Micro Devices, Inc.
   5 *
   6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/kernel.h>
  15#include <linux/device.h>
  16#include <linux/platform_device.h>
  17#include <linux/ioport.h>
  18#include <linux/dma-mapping.h>
  19#include <linux/kthread.h>
  20#include <linux/sched.h>
  21#include <linux/interrupt.h>
  22#include <linux/spinlock.h>
  23#include <linux/delay.h>
  24#include <linux/ccp.h>
  25#include <linux/of.h>
  26#include <linux/of_address.h>
  27#include <linux/acpi.h>
  28
  29#include "ccp-dev.h"
  30
  31struct ccp_platform {
  32        int use_acpi;
  33        int coherent;
  34};
  35
  36static int ccp_get_irq(struct ccp_device *ccp)
  37{
  38        struct device *dev = ccp->dev;
  39        struct platform_device *pdev = container_of(dev,
  40                                        struct platform_device, dev);
  41        int ret;
  42
  43        ret = platform_get_irq(pdev, 0);
  44        if (ret < 0)
  45                return ret;
  46
  47        ccp->irq = ret;
  48        ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
  49        if (ret) {
  50                dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
  51                return ret;
  52        }
  53
  54        return 0;
  55}
  56
  57static int ccp_get_irqs(struct ccp_device *ccp)
  58{
  59        struct device *dev = ccp->dev;
  60        int ret;
  61
  62        ret = ccp_get_irq(ccp);
  63        if (!ret)
  64                return 0;
  65
  66        /* Couldn't get an interrupt */
  67        dev_notice(dev, "could not enable interrupts (%d)\n", ret);
  68
  69        return ret;
  70}
  71
  72static void ccp_free_irqs(struct ccp_device *ccp)
  73{
  74        struct device *dev = ccp->dev;
  75
  76        free_irq(ccp->irq, dev);
  77}
  78
  79static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
  80{
  81        struct device *dev = ccp->dev;
  82        struct platform_device *pdev = container_of(dev,
  83                                        struct platform_device, dev);
  84        struct resource *ior;
  85
  86        ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  87        if (ior && (resource_size(ior) >= 0x800))
  88                return ior;
  89
  90        return NULL;
  91}
  92
  93static int ccp_platform_probe(struct platform_device *pdev)
  94{
  95        struct ccp_device *ccp;
  96        struct ccp_platform *ccp_platform;
  97        struct device *dev = &pdev->dev;
  98        struct acpi_device *adev = ACPI_COMPANION(dev);
  99        struct resource *ior;
 100        int ret;
 101
 102        ret = -ENOMEM;
 103        ccp = ccp_alloc_struct(dev);
 104        if (!ccp)
 105                goto e_err;
 106
 107        ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL);
 108        if (!ccp_platform)
 109                goto e_err;
 110
 111        ccp->dev_specific = ccp_platform;
 112        ccp->get_irq = ccp_get_irqs;
 113        ccp->free_irq = ccp_free_irqs;
 114
 115        ccp_platform->use_acpi = (!adev || acpi_disabled) ? 0 : 1;
 116
 117        ior = ccp_find_mmio_area(ccp);
 118        ccp->io_map = devm_ioremap_resource(dev, ior);
 119        if (IS_ERR(ccp->io_map)) {
 120                ret = PTR_ERR(ccp->io_map);
 121                goto e_err;
 122        }
 123        ccp->io_regs = ccp->io_map;
 124
 125        ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
 126        if (ret) {
 127                dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
 128                goto e_err;
 129        }
 130
 131        ccp_platform->coherent = device_dma_is_coherent(ccp->dev);
 132        if (ccp_platform->coherent)
 133                ccp->axcache = CACHE_WB_NO_ALLOC;
 134        else
 135                ccp->axcache = CACHE_NONE;
 136
 137        dev_set_drvdata(dev, ccp);
 138
 139        ret = ccp_init(ccp);
 140        if (ret)
 141                goto e_err;
 142
 143        dev_notice(dev, "enabled\n");
 144
 145        return 0;
 146
 147e_err:
 148        dev_notice(dev, "initialization failed\n");
 149        return ret;
 150}
 151
 152static int ccp_platform_remove(struct platform_device *pdev)
 153{
 154        struct device *dev = &pdev->dev;
 155        struct ccp_device *ccp = dev_get_drvdata(dev);
 156
 157        ccp_destroy(ccp);
 158
 159        dev_notice(dev, "disabled\n");
 160
 161        return 0;
 162}
 163
 164#ifdef CONFIG_PM
 165static int ccp_platform_suspend(struct platform_device *pdev,
 166                                pm_message_t state)
 167{
 168        struct device *dev = &pdev->dev;
 169        struct ccp_device *ccp = dev_get_drvdata(dev);
 170        unsigned long flags;
 171        unsigned int i;
 172
 173        spin_lock_irqsave(&ccp->cmd_lock, flags);
 174
 175        ccp->suspending = 1;
 176
 177        /* Wake all the queue kthreads to prepare for suspend */
 178        for (i = 0; i < ccp->cmd_q_count; i++)
 179                wake_up_process(ccp->cmd_q[i].kthread);
 180
 181        spin_unlock_irqrestore(&ccp->cmd_lock, flags);
 182
 183        /* Wait for all queue kthreads to say they're done */
 184        while (!ccp_queues_suspended(ccp))
 185                wait_event_interruptible(ccp->suspend_queue,
 186                                         ccp_queues_suspended(ccp));
 187
 188        return 0;
 189}
 190
 191static int ccp_platform_resume(struct platform_device *pdev)
 192{
 193        struct device *dev = &pdev->dev;
 194        struct ccp_device *ccp = dev_get_drvdata(dev);
 195        unsigned long flags;
 196        unsigned int i;
 197
 198        spin_lock_irqsave(&ccp->cmd_lock, flags);
 199
 200        ccp->suspending = 0;
 201
 202        /* Wake up all the kthreads */
 203        for (i = 0; i < ccp->cmd_q_count; i++) {
 204                ccp->cmd_q[i].suspended = 0;
 205                wake_up_process(ccp->cmd_q[i].kthread);
 206        }
 207
 208        spin_unlock_irqrestore(&ccp->cmd_lock, flags);
 209
 210        return 0;
 211}
 212#endif
 213
 214#ifdef CONFIG_ACPI
 215static const struct acpi_device_id ccp_acpi_match[] = {
 216        { "AMDI0C00", 0 },
 217        { },
 218};
 219MODULE_DEVICE_TABLE(acpi, ccp_acpi_match);
 220#endif
 221
 222#ifdef CONFIG_OF
 223static const struct of_device_id ccp_of_match[] = {
 224        { .compatible = "amd,ccp-seattle-v1a" },
 225        { },
 226};
 227MODULE_DEVICE_TABLE(of, ccp_of_match);
 228#endif
 229
 230static struct platform_driver ccp_platform_driver = {
 231        .driver = {
 232                .name = "AMD Cryptographic Coprocessor",
 233#ifdef CONFIG_ACPI
 234                .acpi_match_table = ccp_acpi_match,
 235#endif
 236#ifdef CONFIG_OF
 237                .of_match_table = ccp_of_match,
 238#endif
 239        },
 240        .probe = ccp_platform_probe,
 241        .remove = ccp_platform_remove,
 242#ifdef CONFIG_PM
 243        .suspend = ccp_platform_suspend,
 244        .resume = ccp_platform_resume,
 245#endif
 246};
 247
 248int ccp_platform_init(void)
 249{
 250        return platform_driver_register(&ccp_platform_driver);
 251}
 252
 253void ccp_platform_exit(void)
 254{
 255        platform_driver_unregister(&ccp_platform_driver);
 256}
 257