blob: 8b923b7e9389aabd96ee10350c0f0ea9f66556f6 [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 {
Tom Lendacky6c506342015-02-03 13:07:29 -060032 int coherent;
33};
34
Tom Lendackyc4f4b322014-06-05 10:17:57 -050035static int ccp_get_irq(struct ccp_device *ccp)
36{
37 struct device *dev = ccp->dev;
38 struct platform_device *pdev = container_of(dev,
39 struct platform_device, dev);
40 int ret;
41
42 ret = platform_get_irq(pdev, 0);
43 if (ret < 0)
44 return ret;
45
46 ccp->irq = ret;
47 ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
48 if (ret) {
49 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
50 return ret;
51 }
52
53 return 0;
54}
55
56static int ccp_get_irqs(struct ccp_device *ccp)
57{
58 struct device *dev = ccp->dev;
59 int ret;
60
61 ret = ccp_get_irq(ccp);
62 if (!ret)
63 return 0;
64
65 /* Couldn't get an interrupt */
66 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
67
68 return ret;
69}
70
71static void ccp_free_irqs(struct ccp_device *ccp)
72{
73 struct device *dev = ccp->dev;
74
75 free_irq(ccp->irq, dev);
76}
77
78static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
79{
80 struct device *dev = ccp->dev;
81 struct platform_device *pdev = container_of(dev,
82 struct platform_device, dev);
83 struct resource *ior;
84
85 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86 if (ior && (resource_size(ior) >= 0x800))
87 return ior;
88
89 return NULL;
90}
91
92static int ccp_platform_probe(struct platform_device *pdev)
93{
94 struct ccp_device *ccp;
Tom Lendacky6c506342015-02-03 13:07:29 -060095 struct ccp_platform *ccp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -050096 struct device *dev = &pdev->dev;
97 struct resource *ior;
98 int ret;
99
100 ret = -ENOMEM;
101 ccp = ccp_alloc_struct(dev);
102 if (!ccp)
103 goto e_err;
104
Tom Lendacky6c506342015-02-03 13:07:29 -0600105 ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL);
106 if (!ccp_platform)
107 goto e_err;
108
109 ccp->dev_specific = ccp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500110 ccp->get_irq = ccp_get_irqs;
111 ccp->free_irq = ccp_free_irqs;
112
113 ior = ccp_find_mmio_area(ccp);
114 ccp->io_map = devm_ioremap_resource(dev, ior);
115 if (IS_ERR(ccp->io_map)) {
116 ret = PTR_ERR(ccp->io_map);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600117 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500118 }
119 ccp->io_regs = ccp->io_map;
120
Tom Lendacky261bf072015-02-03 13:07:17 -0600121 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
122 if (ret) {
123 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600124 goto e_err;
Tom Lendacky261bf072015-02-03 13:07:17 -0600125 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500126
Suthikulpanit, Suravee04825cf2015-06-10 11:08:55 -0500127 ccp_platform->coherent = device_dma_is_coherent(ccp->dev);
Tom Lendacky6c506342015-02-03 13:07:29 -0600128 if (ccp_platform->coherent)
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500129 ccp->axcache = CACHE_WB_NO_ALLOC;
130 else
131 ccp->axcache = CACHE_NONE;
132
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500133 dev_set_drvdata(dev, ccp);
134
135 ret = ccp_init(ccp);
136 if (ret)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600137 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500138
139 dev_notice(dev, "enabled\n");
140
141 return 0;
142
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500143e_err:
144 dev_notice(dev, "initialization failed\n");
145 return ret;
146}
147
148static int ccp_platform_remove(struct platform_device *pdev)
149{
150 struct device *dev = &pdev->dev;
151 struct ccp_device *ccp = dev_get_drvdata(dev);
152
153 ccp_destroy(ccp);
154
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500155 dev_notice(dev, "disabled\n");
156
157 return 0;
158}
159
160#ifdef CONFIG_PM
161static int ccp_platform_suspend(struct platform_device *pdev,
162 pm_message_t state)
163{
164 struct device *dev = &pdev->dev;
165 struct ccp_device *ccp = dev_get_drvdata(dev);
166 unsigned long flags;
167 unsigned int i;
168
169 spin_lock_irqsave(&ccp->cmd_lock, flags);
170
171 ccp->suspending = 1;
172
173 /* Wake all the queue kthreads to prepare for suspend */
174 for (i = 0; i < ccp->cmd_q_count; i++)
175 wake_up_process(ccp->cmd_q[i].kthread);
176
177 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
178
179 /* Wait for all queue kthreads to say they're done */
180 while (!ccp_queues_suspended(ccp))
181 wait_event_interruptible(ccp->suspend_queue,
182 ccp_queues_suspended(ccp));
183
184 return 0;
185}
186
187static int ccp_platform_resume(struct platform_device *pdev)
188{
189 struct device *dev = &pdev->dev;
190 struct ccp_device *ccp = dev_get_drvdata(dev);
191 unsigned long flags;
192 unsigned int i;
193
194 spin_lock_irqsave(&ccp->cmd_lock, flags);
195
196 ccp->suspending = 0;
197
198 /* Wake up all the kthreads */
199 for (i = 0; i < ccp->cmd_q_count; i++) {
200 ccp->cmd_q[i].suspended = 0;
201 wake_up_process(ccp->cmd_q[i].kthread);
202 }
203
204 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
205
206 return 0;
207}
208#endif
209
Tom Lendacky6c506342015-02-03 13:07:29 -0600210#ifdef CONFIG_ACPI
211static const struct acpi_device_id ccp_acpi_match[] = {
212 { "AMDI0C00", 0 },
213 { },
214};
Tom Lendacky61705112015-06-30 12:57:14 -0500215MODULE_DEVICE_TABLE(acpi, ccp_acpi_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600216#endif
217
218#ifdef CONFIG_OF
219static const struct of_device_id ccp_of_match[] = {
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500220 { .compatible = "amd,ccp-seattle-v1a" },
221 { },
222};
Tom Lendacky61705112015-06-30 12:57:14 -0500223MODULE_DEVICE_TABLE(of, ccp_of_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600224#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500225
226static struct platform_driver ccp_platform_driver = {
227 .driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500228 .name = "ccp",
Tom Lendacky6c506342015-02-03 13:07:29 -0600229#ifdef CONFIG_ACPI
230 .acpi_match_table = ccp_acpi_match,
231#endif
232#ifdef CONFIG_OF
233 .of_match_table = ccp_of_match,
234#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500235 },
236 .probe = ccp_platform_probe,
237 .remove = ccp_platform_remove,
238#ifdef CONFIG_PM
239 .suspend = ccp_platform_suspend,
240 .resume = ccp_platform_resume,
241#endif
242};
243
244int ccp_platform_init(void)
245{
246 return platform_driver_register(&ccp_platform_driver);
247}
248
249void ccp_platform_exit(void)
250{
251 platform_driver_unregister(&ccp_platform_driver);
252}