blob: 6020c4ae72fcdbe5432b2bd1f84499cba3124426 [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
pjambhlekar9d1fb192017-05-03 09:32:09 +053047 return NULL;
Gary R Hookc7019c42016-03-01 13:49:15 -060048}
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
pjambhlekar9d1fb192017-05-03 09:32:09 +053059 return NULL;
Gary R Hookc7019c42016-03-01 13:49:15 -060060}
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);
Gustavo A. R. Silva28a2cc62017-06-30 00:59:52 -050069 if (ret < 0) {
70 dev_notice(dev, "unable to get IRQ (%d)\n", ret);
Tom Lendackyc4f4b322014-06-05 10:17:57 -050071 return ret;
Gustavo A. R. Silva28a2cc62017-06-30 00:59:52 -050072 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -050073
74 ccp->irq = ret;
Gary R Hookea0375a2016-03-01 13:49:25 -060075 ret = request_irq(ccp->irq, ccp->vdata->perform->irqhandler, 0,
76 ccp->name, dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -050077 if (ret) {
78 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
79 return ret;
80 }
81
82 return 0;
83}
84
85static int ccp_get_irqs(struct ccp_device *ccp)
86{
87 struct device *dev = ccp->dev;
88 int ret;
89
90 ret = ccp_get_irq(ccp);
91 if (!ret)
92 return 0;
93
94 /* Couldn't get an interrupt */
95 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
96
97 return ret;
98}
99
100static void ccp_free_irqs(struct ccp_device *ccp)
101{
102 struct device *dev = ccp->dev;
103
104 free_irq(ccp->irq, dev);
105}
106
107static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
108{
109 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +0800110 struct platform_device *pdev = to_platform_device(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500111 struct resource *ior;
112
113 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114 if (ior && (resource_size(ior) >= 0x800))
115 return ior;
116
117 return NULL;
118}
119
120static int ccp_platform_probe(struct platform_device *pdev)
121{
122 struct ccp_device *ccp;
Tom Lendacky6c506342015-02-03 13:07:29 -0600123 struct ccp_platform *ccp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500124 struct device *dev = &pdev->dev;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700125 enum dev_dma_attr attr;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500126 struct resource *ior;
127 int ret;
128
129 ret = -ENOMEM;
130 ccp = ccp_alloc_struct(dev);
131 if (!ccp)
132 goto e_err;
133
Tom Lendacky6c506342015-02-03 13:07:29 -0600134 ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL);
135 if (!ccp_platform)
136 goto e_err;
137
138 ccp->dev_specific = ccp_platform;
Gary R Hookc7019c42016-03-01 13:49:15 -0600139 ccp->vdata = pdev->dev.of_node ? ccp_get_of_version(pdev)
140 : ccp_get_acpi_version(pdev);
141 if (!ccp->vdata || !ccp->vdata->version) {
142 ret = -ENODEV;
143 dev_err(dev, "missing driver data\n");
144 goto e_err;
145 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500146 ccp->get_irq = ccp_get_irqs;
147 ccp->free_irq = ccp_free_irqs;
148
149 ior = ccp_find_mmio_area(ccp);
150 ccp->io_map = devm_ioremap_resource(dev, ior);
151 if (IS_ERR(ccp->io_map)) {
152 ret = PTR_ERR(ccp->io_map);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600153 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500154 }
155 ccp->io_regs = ccp->io_map;
156
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700157 attr = device_get_dma_attr(dev);
158 if (attr == DEV_DMA_NOT_SUPPORTED) {
159 dev_err(dev, "DMA is not supported");
160 goto e_err;
161 }
162
163 ccp_platform->coherent = (attr == DEV_DMA_COHERENT);
164 if (ccp_platform->coherent)
165 ccp->axcache = CACHE_WB_NO_ALLOC;
166 else
167 ccp->axcache = CACHE_NONE;
168
Tom Lendacky261bf072015-02-03 13:07:17 -0600169 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
170 if (ret) {
171 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600172 goto e_err;
Tom Lendacky261bf072015-02-03 13:07:17 -0600173 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500174
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500175 dev_set_drvdata(dev, ccp);
176
Gary R Hookea0375a2016-03-01 13:49:25 -0600177 ret = ccp->vdata->perform->init(ccp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500178 if (ret)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600179 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500180
181 dev_notice(dev, "enabled\n");
182
183 return 0;
184
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500185e_err:
186 dev_notice(dev, "initialization failed\n");
187 return ret;
188}
189
190static int ccp_platform_remove(struct platform_device *pdev)
191{
192 struct device *dev = &pdev->dev;
193 struct ccp_device *ccp = dev_get_drvdata(dev);
194
Gary R Hookea0375a2016-03-01 13:49:25 -0600195 ccp->vdata->perform->destroy(ccp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500196
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500197 dev_notice(dev, "disabled\n");
198
199 return 0;
200}
201
202#ifdef CONFIG_PM
203static int ccp_platform_suspend(struct platform_device *pdev,
204 pm_message_t state)
205{
206 struct device *dev = &pdev->dev;
207 struct ccp_device *ccp = dev_get_drvdata(dev);
208 unsigned long flags;
209 unsigned int i;
210
211 spin_lock_irqsave(&ccp->cmd_lock, flags);
212
213 ccp->suspending = 1;
214
215 /* Wake all the queue kthreads to prepare for suspend */
216 for (i = 0; i < ccp->cmd_q_count; i++)
217 wake_up_process(ccp->cmd_q[i].kthread);
218
219 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
220
221 /* Wait for all queue kthreads to say they're done */
222 while (!ccp_queues_suspended(ccp))
223 wait_event_interruptible(ccp->suspend_queue,
224 ccp_queues_suspended(ccp));
225
226 return 0;
227}
228
229static int ccp_platform_resume(struct platform_device *pdev)
230{
231 struct device *dev = &pdev->dev;
232 struct ccp_device *ccp = dev_get_drvdata(dev);
233 unsigned long flags;
234 unsigned int i;
235
236 spin_lock_irqsave(&ccp->cmd_lock, flags);
237
238 ccp->suspending = 0;
239
240 /* Wake up all the kthreads */
241 for (i = 0; i < ccp->cmd_q_count; i++) {
242 ccp->cmd_q[i].suspended = 0;
243 wake_up_process(ccp->cmd_q[i].kthread);
244 }
245
246 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
247
248 return 0;
249}
250#endif
251
Tom Lendacky6c506342015-02-03 13:07:29 -0600252#ifdef CONFIG_ACPI
253static const struct acpi_device_id ccp_acpi_match[] = {
Gary R Hookc7019c42016-03-01 13:49:15 -0600254 { "AMDI0C00", (kernel_ulong_t)&ccpv3 },
Tom Lendacky6c506342015-02-03 13:07:29 -0600255 { },
256};
Tom Lendacky61705112015-06-30 12:57:14 -0500257MODULE_DEVICE_TABLE(acpi, ccp_acpi_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600258#endif
259
260#ifdef CONFIG_OF
261static const struct of_device_id ccp_of_match[] = {
Gary R Hookc7019c42016-03-01 13:49:15 -0600262 { .compatible = "amd,ccp-seattle-v1a",
263 .data = (const void *)&ccpv3 },
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500264 { },
265};
Tom Lendacky61705112015-06-30 12:57:14 -0500266MODULE_DEVICE_TABLE(of, ccp_of_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600267#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500268
269static struct platform_driver ccp_platform_driver = {
270 .driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500271 .name = "ccp",
Tom Lendacky6c506342015-02-03 13:07:29 -0600272#ifdef CONFIG_ACPI
273 .acpi_match_table = ccp_acpi_match,
274#endif
275#ifdef CONFIG_OF
276 .of_match_table = ccp_of_match,
277#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500278 },
279 .probe = ccp_platform_probe,
280 .remove = ccp_platform_remove,
281#ifdef CONFIG_PM
282 .suspend = ccp_platform_suspend,
283 .resume = ccp_platform_resume,
284#endif
285};
286
287int ccp_platform_init(void)
288{
289 return platform_driver_register(&ccp_platform_driver);
290}
291
292void ccp_platform_exit(void)
293{
294 platform_driver_unregister(&ccp_platform_driver);
295}