blob: e446781b1892ae4091afafc4beb257d042a6174a [file] [log] [blame]
Tom Lendackyc4f4b322014-06-05 10:17:57 -05001/*
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>
Tom Lendacky126ae9a2014-07-10 10:58:35 -050025#include <linux/of.h>
Tom Lendacky6c506342015-02-03 13:07:29 -060026#include <linux/of_address.h>
27#include <linux/acpi.h>
Tom Lendackyc4f4b322014-06-05 10:17:57 -050028
29#include "ccp-dev.h"
30
Tom Lendacky6c506342015-02-03 13:07:29 -060031struct ccp_platform {
32 int use_acpi;
33 int coherent;
34};
35
Tom Lendackyc4f4b322014-06-05 10:17:57 -050036static 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;
Tom Lendacky6c506342015-02-03 13:07:29 -060096 struct ccp_platform *ccp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -050097 struct device *dev = &pdev->dev;
Tom Lendacky6c506342015-02-03 13:07:29 -060098 struct acpi_device *adev = ACPI_COMPANION(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -050099 struct resource *ior;
100 int ret;
101
102 ret = -ENOMEM;
103 ccp = ccp_alloc_struct(dev);
104 if (!ccp)
105 goto e_err;
106
Tom Lendacky6c506342015-02-03 13:07:29 -0600107 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;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500112 ccp->get_irq = ccp_get_irqs;
113 ccp->free_irq = ccp_free_irqs;
114
Tom Lendacky6c506342015-02-03 13:07:29 -0600115 ccp_platform->use_acpi = (!adev || acpi_disabled) ? 0 : 1;
116
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500117 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);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600121 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500122 }
123 ccp->io_regs = ccp->io_map;
124
125 if (!dev->dma_mask)
126 dev->dma_mask = &dev->coherent_dma_mask;
Tom Lendacky261bf072015-02-03 13:07:17 -0600127 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
128 if (ret) {
129 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600130 goto e_err;
Tom Lendacky261bf072015-02-03 13:07:17 -0600131 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500132
Suthikulpanit, Suravee04825cf2015-06-10 11:08:55 -0500133 ccp_platform->coherent = device_dma_is_coherent(ccp->dev);
Tom Lendacky6c506342015-02-03 13:07:29 -0600134 if (ccp_platform->coherent)
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500135 ccp->axcache = CACHE_WB_NO_ALLOC;
136 else
137 ccp->axcache = CACHE_NONE;
138
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500139 dev_set_drvdata(dev, ccp);
140
141 ret = ccp_init(ccp);
142 if (ret)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600143 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500144
145 dev_notice(dev, "enabled\n");
146
147 return 0;
148
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500149e_err:
150 dev_notice(dev, "initialization failed\n");
151 return ret;
152}
153
154static int ccp_platform_remove(struct platform_device *pdev)
155{
156 struct device *dev = &pdev->dev;
157 struct ccp_device *ccp = dev_get_drvdata(dev);
158
159 ccp_destroy(ccp);
160
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500161 dev_notice(dev, "disabled\n");
162
163 return 0;
164}
165
166#ifdef CONFIG_PM
167static int ccp_platform_suspend(struct platform_device *pdev,
168 pm_message_t state)
169{
170 struct device *dev = &pdev->dev;
171 struct ccp_device *ccp = dev_get_drvdata(dev);
172 unsigned long flags;
173 unsigned int i;
174
175 spin_lock_irqsave(&ccp->cmd_lock, flags);
176
177 ccp->suspending = 1;
178
179 /* Wake all the queue kthreads to prepare for suspend */
180 for (i = 0; i < ccp->cmd_q_count; i++)
181 wake_up_process(ccp->cmd_q[i].kthread);
182
183 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
184
185 /* Wait for all queue kthreads to say they're done */
186 while (!ccp_queues_suspended(ccp))
187 wait_event_interruptible(ccp->suspend_queue,
188 ccp_queues_suspended(ccp));
189
190 return 0;
191}
192
193static int ccp_platform_resume(struct platform_device *pdev)
194{
195 struct device *dev = &pdev->dev;
196 struct ccp_device *ccp = dev_get_drvdata(dev);
197 unsigned long flags;
198 unsigned int i;
199
200 spin_lock_irqsave(&ccp->cmd_lock, flags);
201
202 ccp->suspending = 0;
203
204 /* Wake up all the kthreads */
205 for (i = 0; i < ccp->cmd_q_count; i++) {
206 ccp->cmd_q[i].suspended = 0;
207 wake_up_process(ccp->cmd_q[i].kthread);
208 }
209
210 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
211
212 return 0;
213}
214#endif
215
Tom Lendacky6c506342015-02-03 13:07:29 -0600216#ifdef CONFIG_ACPI
217static const struct acpi_device_id ccp_acpi_match[] = {
218 { "AMDI0C00", 0 },
219 { },
220};
221#endif
222
223#ifdef CONFIG_OF
224static const struct of_device_id ccp_of_match[] = {
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500225 { .compatible = "amd,ccp-seattle-v1a" },
226 { },
227};
Tom Lendacky6c506342015-02-03 13:07:29 -0600228#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500229
230static struct platform_driver ccp_platform_driver = {
231 .driver = {
232 .name = "AMD Cryptographic Coprocessor",
Tom Lendacky6c506342015-02-03 13:07:29 -0600233#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
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500239 },
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}