2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2014 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
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>
26 #include <linux/of_address.h>
27 #include <linux/acpi.h>
36 static int ccp_get_irq(struct ccp_device *ccp)
38 struct device *dev = ccp->dev;
39 struct platform_device *pdev = container_of(dev,
40 struct platform_device, dev);
43 ret = platform_get_irq(pdev, 0);
48 ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
50 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
57 static int ccp_get_irqs(struct ccp_device *ccp)
59 struct device *dev = ccp->dev;
62 ret = ccp_get_irq(ccp);
66 /* Couldn't get an interrupt */
67 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
72 static void ccp_free_irqs(struct ccp_device *ccp)
74 struct device *dev = ccp->dev;
76 free_irq(ccp->irq, dev);
79 static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
81 struct device *dev = ccp->dev;
82 struct platform_device *pdev = container_of(dev,
83 struct platform_device, dev);
86 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
87 if (ior && (resource_size(ior) >= 0x800))
94 static int ccp_acpi_support(struct ccp_device *ccp)
96 struct ccp_platform *ccp_platform = ccp->dev_specific;
97 struct acpi_device *adev = ACPI_COMPANION(ccp->dev);
100 unsigned long long data;
103 /* Retrieve the device cache coherency value */
104 handle = adev->handle;
106 status = acpi_evaluate_integer(handle, "_CCA", NULL, &data);
107 if (!ACPI_FAILURE(status)) {
111 } while (!ACPI_FAILURE(status));
113 if (ACPI_FAILURE(status)) {
114 dev_err(ccp->dev, "error obtaining acpi coherency value\n");
118 ccp_platform->coherent = !!cca;
122 #else /* CONFIG_ACPI */
123 static int ccp_acpi_support(struct ccp_device *ccp)
130 static int ccp_of_support(struct ccp_device *ccp)
132 struct ccp_platform *ccp_platform = ccp->dev_specific;
134 ccp_platform->coherent = of_dma_is_coherent(ccp->dev->of_node);
139 static int ccp_of_support(struct ccp_device *ccp)
145 static int ccp_platform_probe(struct platform_device *pdev)
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;
155 ccp = ccp_alloc_struct(dev);
159 ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL);
163 ccp->dev_specific = ccp_platform;
164 ccp->get_irq = ccp_get_irqs;
165 ccp->free_irq = ccp_free_irqs;
167 ccp_platform->use_acpi = (!adev || acpi_disabled) ? 0 : 1;
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);
175 ccp->io_regs = ccp->io_map;
178 dev->dma_mask = &dev->coherent_dma_mask;
179 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
181 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
185 if (ccp_platform->use_acpi)
186 ret = ccp_acpi_support(ccp);
188 ret = ccp_of_support(ccp);
192 if (ccp_platform->coherent)
193 ccp->axcache = CACHE_WB_NO_ALLOC;
195 ccp->axcache = CACHE_NONE;
197 dev_set_drvdata(dev, ccp);
203 dev_notice(dev, "enabled\n");
208 dev_notice(dev, "initialization failed\n");
212 static int ccp_platform_remove(struct platform_device *pdev)
214 struct device *dev = &pdev->dev;
215 struct ccp_device *ccp = dev_get_drvdata(dev);
219 dev_notice(dev, "disabled\n");
225 static int ccp_platform_suspend(struct platform_device *pdev,
228 struct device *dev = &pdev->dev;
229 struct ccp_device *ccp = dev_get_drvdata(dev);
233 spin_lock_irqsave(&ccp->cmd_lock, flags);
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);
241 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
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));
251 static int ccp_platform_resume(struct platform_device *pdev)
253 struct device *dev = &pdev->dev;
254 struct ccp_device *ccp = dev_get_drvdata(dev);
258 spin_lock_irqsave(&ccp->cmd_lock, flags);
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);
268 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
275 static const struct acpi_device_id ccp_acpi_match[] = {
282 static const struct of_device_id ccp_of_match[] = {
283 { .compatible = "amd,ccp-seattle-v1a" },
288 static struct platform_driver ccp_platform_driver = {
290 .name = "AMD Cryptographic Coprocessor",
292 .acpi_match_table = ccp_acpi_match,
295 .of_match_table = ccp_of_match,
298 .probe = ccp_platform_probe,
299 .remove = ccp_platform_remove,
301 .suspend = ccp_platform_suspend,
302 .resume = ccp_platform_resume,
306 int ccp_platform_init(void)
308 return platform_driver_register(&ccp_platform_driver);
311 void ccp_platform_exit(void)
313 platform_driver_unregister(&ccp_platform_driver);