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 93#ifdef CONFIG_ACPI 94static int ccp_acpi_support(struct ccp_device *ccp) 95{ 96 struct ccp_platform *ccp_platform = ccp->dev_specific; 97 struct acpi_device *adev = ACPI_COMPANION(ccp->dev); 98 acpi_handle handle; 99 acpi_status status; 100 unsigned long long data; 101 int cca; 102 103 /* Retrieve the device cache coherency value */ 104 handle = adev->handle; 105 do { 106 status = acpi_evaluate_integer(handle, "_CCA", NULL, &data); 107 if (!ACPI_FAILURE(status)) { 108 cca = data; 109 break; 110 } 111 } while (!ACPI_FAILURE(status)); 112 113 if (ACPI_FAILURE(status)) { 114 dev_err(ccp->dev, "error obtaining acpi coherency value\n"); 115 return -EINVAL; 116 } 117 118 ccp_platform->coherent = !!cca; 119 120 return 0; 121} 122#else /* CONFIG_ACPI */ 123static int ccp_acpi_support(struct ccp_device *ccp) 124{ 125 return -EINVAL; 126} 127#endif 128 129#ifdef CONFIG_OF 130static int ccp_of_support(struct ccp_device *ccp) 131{ 132 struct ccp_platform *ccp_platform = ccp->dev_specific; 133 134 ccp_platform->coherent = of_dma_is_coherent(ccp->dev->of_node); 135 136 return 0; 137} 138#else 139static int ccp_of_support(struct ccp_device *ccp) 140{ 141 return -EINVAL; 142} 143#endif 144 145static int ccp_platform_probe(struct platform_device *pdev) 146{ 147 struct ccp_device *ccp; 148 struct ccp_platform *ccp_platform; 149 struct device *dev = &pdev->dev; 150 struct acpi_device *adev = ACPI_COMPANION(dev); 151 struct resource *ior; 152 int ret; 153 154 ret = -ENOMEM; 155 ccp = ccp_alloc_struct(dev); 156 if (!ccp) 157 goto e_err; 158 159 ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL); 160 if (!ccp_platform) 161 goto e_err; 162 163 ccp->dev_specific = ccp_platform; 164 ccp->get_irq = ccp_get_irqs; 165 ccp->free_irq = ccp_free_irqs; 166 167 ccp_platform->use_acpi = (!adev || acpi_disabled) ? 0 : 1; 168 169 ior = ccp_find_mmio_area(ccp); 170 ccp->io_map = devm_ioremap_resource(dev, ior); 171 if (IS_ERR(ccp->io_map)) { 172 ret = PTR_ERR(ccp->io_map); 173 goto e_err; 174 } 175 ccp->io_regs = ccp->io_map; 176 177 if (!dev->dma_mask) 178 dev->dma_mask = &dev->coherent_dma_mask; 179 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); 180 if (ret) { 181 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); 182 goto e_err; 183 } 184 185 if (ccp_platform->use_acpi) 186 ret = ccp_acpi_support(ccp); 187 else 188 ret = ccp_of_support(ccp); 189 if (ret) 190 goto e_err; 191 192 if (ccp_platform->coherent) 193 ccp->axcache = CACHE_WB_NO_ALLOC; 194 else 195 ccp->axcache = CACHE_NONE; 196 197 dev_set_drvdata(dev, ccp); 198 199 ret = ccp_init(ccp); 200 if (ret) 201 goto e_err; 202 203 dev_notice(dev, "enabled\n"); 204 205 return 0; 206 207e_err: 208 dev_notice(dev, "initialization failed\n"); 209 return ret; 210} 211 212static int ccp_platform_remove(struct platform_device *pdev) 213{ 214 struct device *dev = &pdev->dev; 215 struct ccp_device *ccp = dev_get_drvdata(dev); 216 217 ccp_destroy(ccp); 218 219 dev_notice(dev, "disabled\n"); 220 221 return 0; 222} 223 224#ifdef CONFIG_PM 225static int ccp_platform_suspend(struct platform_device *pdev, 226 pm_message_t state) 227{ 228 struct device *dev = &pdev->dev; 229 struct ccp_device *ccp = dev_get_drvdata(dev); 230 unsigned long flags; 231 unsigned int i; 232 233 spin_lock_irqsave(&ccp->cmd_lock, flags); 234 235 ccp->suspending = 1; 236 237 /* Wake all the queue kthreads to prepare for suspend */ 238 for (i = 0; i < ccp->cmd_q_count; i++) 239 wake_up_process(ccp->cmd_q[i].kthread); 240 241 spin_unlock_irqrestore(&ccp->cmd_lock, flags); 242 243 /* Wait for all queue kthreads to say they're done */ 244 while (!ccp_queues_suspended(ccp)) 245 wait_event_interruptible(ccp->suspend_queue, 246 ccp_queues_suspended(ccp)); 247 248 return 0; 249} 250 251static int ccp_platform_resume(struct platform_device *pdev) 252{ 253 struct device *dev = &pdev->dev; 254 struct ccp_device *ccp = dev_get_drvdata(dev); 255 unsigned long flags; 256 unsigned int i; 257 258 spin_lock_irqsave(&ccp->cmd_lock, flags); 259 260 ccp->suspending = 0; 261 262 /* Wake up all the kthreads */ 263 for (i = 0; i < ccp->cmd_q_count; i++) { 264 ccp->cmd_q[i].suspended = 0; 265 wake_up_process(ccp->cmd_q[i].kthread); 266 } 267 268 spin_unlock_irqrestore(&ccp->cmd_lock, flags); 269 270 return 0; 271} 272#endif 273 274#ifdef CONFIG_ACPI 275static const struct acpi_device_id ccp_acpi_match[] = { 276 { "AMDI0C00", 0 }, 277 { }, 278}; 279#endif 280 281#ifdef CONFIG_OF 282static const struct of_device_id ccp_of_match[] = { 283 { .compatible = "amd,ccp-seattle-v1a" }, 284 { }, 285}; 286#endif 287 288static struct platform_driver ccp_platform_driver = { 289 .driver = { 290 .name = "AMD Cryptographic Coprocessor", 291#ifdef CONFIG_ACPI 292 .acpi_match_table = ccp_acpi_match, 293#endif 294#ifdef CONFIG_OF 295 .of_match_table = ccp_of_match, 296#endif 297 }, 298 .probe = ccp_platform_probe, 299 .remove = ccp_platform_remove, 300#ifdef CONFIG_PM 301 .suspend = ccp_platform_suspend, 302 .resume = ccp_platform_resume, 303#endif 304}; 305 306int ccp_platform_init(void) 307{ 308 return platform_driver_register(&ccp_platform_driver); 309} 310 311void ccp_platform_exit(void) 312{ 313 platform_driver_unregister(&ccp_platform_driver); 314} 315