blob: 6e1cf228c7c0c3f7c2389957357befe6e5e583e1 [file] [log] [blame]
Tom Lendackyc4f4b322014-06-05 10:17:57 -05001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook553d2372016-03-01 13:49:04 -06004 * Copyright (C) 2014,2016 Advanced Micro Devices, Inc.
Tom Lendackyc4f4b322014-06-05 10:17:57 -05005 *
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 {
Tom Lendacky6c506342015-02-03 13:07:29 -060032 int coherent;
33};
34
Gary R Hookc7019c42016-03-01 13:49:15 -060035static const struct acpi_device_id ccp_acpi_match[];
36static const struct of_device_id ccp_of_match[];
37
38static struct ccp_vdata *ccp_get_of_version(struct platform_device *pdev)
39{
40#ifdef CONFIG_OF
41 const struct of_device_id *match;
42
43 match = of_match_node(ccp_of_match, pdev->dev.of_node);
44 if (match && match->data)
45 return (struct ccp_vdata *)match->data;
46#endif
47 return 0;
48}
49
50static struct ccp_vdata *ccp_get_acpi_version(struct platform_device *pdev)
51{
52#ifdef CONFIG_ACPI
53 const struct acpi_device_id *match;
54
55 match = acpi_match_device(ccp_acpi_match, &pdev->dev);
56 if (match && match->driver_data)
57 return (struct ccp_vdata *)match->driver_data;
58#endif
59 return 0;
60}
61
Tom Lendackyc4f4b322014-06-05 10:17:57 -050062static int ccp_get_irq(struct ccp_device *ccp)
63{
64 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080065 struct platform_device *pdev = to_platform_device(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -050066 int ret;
67
68 ret = platform_get_irq(pdev, 0);
69 if (ret < 0)
70 return ret;
71
72 ccp->irq = ret;
Gary R Hook553d2372016-03-01 13:49:04 -060073 ret = request_irq(ccp->irq, ccp_irq_handler, 0, ccp->name, dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -050074 if (ret) {
75 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
76 return ret;
77 }
78
79 return 0;
80}
81
82static int ccp_get_irqs(struct ccp_device *ccp)
83{
84 struct device *dev = ccp->dev;
85 int ret;
86
87 ret = ccp_get_irq(ccp);
88 if (!ret)
89 return 0;
90
91 /* Couldn't get an interrupt */
92 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
93
94 return ret;
95}
96
97static void ccp_free_irqs(struct ccp_device *ccp)
98{
99 struct device *dev = ccp->dev;
100
101 free_irq(ccp->irq, dev);
102}
103
104static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
105{
106 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +0800107 struct platform_device *pdev = to_platform_device(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500108 struct resource *ior;
109
110 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
111 if (ior && (resource_size(ior) >= 0x800))
112 return ior;
113
114 return NULL;
115}
116
117static int ccp_platform_probe(struct platform_device *pdev)
118{
119 struct ccp_device *ccp;
Tom Lendacky6c506342015-02-03 13:07:29 -0600120 struct ccp_platform *ccp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500121 struct device *dev = &pdev->dev;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700122 enum dev_dma_attr attr;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500123 struct resource *ior;
124 int ret;
125
126 ret = -ENOMEM;
127 ccp = ccp_alloc_struct(dev);
128 if (!ccp)
129 goto e_err;
130
Tom Lendacky6c506342015-02-03 13:07:29 -0600131 ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL);
132 if (!ccp_platform)
133 goto e_err;
134
135 ccp->dev_specific = ccp_platform;
Gary R Hookc7019c42016-03-01 13:49:15 -0600136 ccp->vdata = pdev->dev.of_node ? ccp_get_of_version(pdev)
137 : ccp_get_acpi_version(pdev);
138 if (!ccp->vdata || !ccp->vdata->version) {
139 ret = -ENODEV;
140 dev_err(dev, "missing driver data\n");
141 goto e_err;
142 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500143 ccp->get_irq = ccp_get_irqs;
144 ccp->free_irq = ccp_free_irqs;
145
146 ior = ccp_find_mmio_area(ccp);
147 ccp->io_map = devm_ioremap_resource(dev, ior);
148 if (IS_ERR(ccp->io_map)) {
149 ret = PTR_ERR(ccp->io_map);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600150 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500151 }
152 ccp->io_regs = ccp->io_map;
153
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700154 attr = device_get_dma_attr(dev);
155 if (attr == DEV_DMA_NOT_SUPPORTED) {
156 dev_err(dev, "DMA is not supported");
157 goto e_err;
158 }
159
160 ccp_platform->coherent = (attr == DEV_DMA_COHERENT);
161 if (ccp_platform->coherent)
162 ccp->axcache = CACHE_WB_NO_ALLOC;
163 else
164 ccp->axcache = CACHE_NONE;
165
Tom Lendacky261bf072015-02-03 13:07:17 -0600166 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
167 if (ret) {
168 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600169 goto e_err;
Tom Lendacky261bf072015-02-03 13:07:17 -0600170 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500171
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500172 dev_set_drvdata(dev, ccp);
173
174 ret = ccp_init(ccp);
175 if (ret)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600176 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500177
178 dev_notice(dev, "enabled\n");
179
180 return 0;
181
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500182e_err:
183 dev_notice(dev, "initialization failed\n");
184 return ret;
185}
186
187static int ccp_platform_remove(struct platform_device *pdev)
188{
189 struct device *dev = &pdev->dev;
190 struct ccp_device *ccp = dev_get_drvdata(dev);
191
192 ccp_destroy(ccp);
193
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500194 dev_notice(dev, "disabled\n");
195
196 return 0;
197}
198
199#ifdef CONFIG_PM
200static int ccp_platform_suspend(struct platform_device *pdev,
201 pm_message_t state)
202{
203 struct device *dev = &pdev->dev;
204 struct ccp_device *ccp = dev_get_drvdata(dev);
205 unsigned long flags;
206 unsigned int i;
207
208 spin_lock_irqsave(&ccp->cmd_lock, flags);
209
210 ccp->suspending = 1;
211
212 /* Wake all the queue kthreads to prepare for suspend */
213 for (i = 0; i < ccp->cmd_q_count; i++)
214 wake_up_process(ccp->cmd_q[i].kthread);
215
216 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
217
218 /* Wait for all queue kthreads to say they're done */
219 while (!ccp_queues_suspended(ccp))
220 wait_event_interruptible(ccp->suspend_queue,
221 ccp_queues_suspended(ccp));
222
223 return 0;
224}
225
226static int ccp_platform_resume(struct platform_device *pdev)
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 = 0;
236
237 /* Wake up all the kthreads */
238 for (i = 0; i < ccp->cmd_q_count; i++) {
239 ccp->cmd_q[i].suspended = 0;
240 wake_up_process(ccp->cmd_q[i].kthread);
241 }
242
243 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
244
245 return 0;
246}
247#endif
248
Tom Lendacky6c506342015-02-03 13:07:29 -0600249#ifdef CONFIG_ACPI
250static const struct acpi_device_id ccp_acpi_match[] = {
Gary R Hookc7019c42016-03-01 13:49:15 -0600251 { "AMDI0C00", (kernel_ulong_t)&ccpv3 },
Tom Lendacky6c506342015-02-03 13:07:29 -0600252 { },
253};
Tom Lendacky61705112015-06-30 12:57:14 -0500254MODULE_DEVICE_TABLE(acpi, ccp_acpi_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600255#endif
256
257#ifdef CONFIG_OF
258static const struct of_device_id ccp_of_match[] = {
Gary R Hookc7019c42016-03-01 13:49:15 -0600259 { .compatible = "amd,ccp-seattle-v1a",
260 .data = (const void *)&ccpv3 },
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500261 { },
262};
Tom Lendacky61705112015-06-30 12:57:14 -0500263MODULE_DEVICE_TABLE(of, ccp_of_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600264#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500265
266static struct platform_driver ccp_platform_driver = {
267 .driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500268 .name = "ccp",
Tom Lendacky6c506342015-02-03 13:07:29 -0600269#ifdef CONFIG_ACPI
270 .acpi_match_table = ccp_acpi_match,
271#endif
272#ifdef CONFIG_OF
273 .of_match_table = ccp_of_match,
274#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500275 },
276 .probe = ccp_platform_probe,
277 .remove = ccp_platform_remove,
278#ifdef CONFIG_PM
279 .suspend = ccp_platform_suspend,
280 .resume = ccp_platform_resume,
281#endif
282};
283
284int ccp_platform_init(void)
285{
286 return platform_driver_register(&ccp_platform_driver);
287}
288
289void ccp_platform_exit(void)
290{
291 platform_driver_unregister(&ccp_platform_driver);
292}