blob: 04265a3c36085b96e92d182a4ffdb0fc20a600cc [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 Lendackyc4f4b322014-06-05 10:17:57 -050026
27#include "ccp-dev.h"
28
Tom Lendackyc4f4b322014-06-05 10:17:57 -050029static int ccp_get_irq(struct ccp_device *ccp)
30{
31 struct device *dev = ccp->dev;
32 struct platform_device *pdev = container_of(dev,
33 struct platform_device, dev);
34 int ret;
35
36 ret = platform_get_irq(pdev, 0);
37 if (ret < 0)
38 return ret;
39
40 ccp->irq = ret;
41 ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev);
42 if (ret) {
43 dev_notice(dev, "unable to allocate IRQ (%d)\n", ret);
44 return ret;
45 }
46
47 return 0;
48}
49
50static int ccp_get_irqs(struct ccp_device *ccp)
51{
52 struct device *dev = ccp->dev;
53 int ret;
54
55 ret = ccp_get_irq(ccp);
56 if (!ret)
57 return 0;
58
59 /* Couldn't get an interrupt */
60 dev_notice(dev, "could not enable interrupts (%d)\n", ret);
61
62 return ret;
63}
64
65static void ccp_free_irqs(struct ccp_device *ccp)
66{
67 struct device *dev = ccp->dev;
68
69 free_irq(ccp->irq, dev);
70}
71
72static struct resource *ccp_find_mmio_area(struct ccp_device *ccp)
73{
74 struct device *dev = ccp->dev;
75 struct platform_device *pdev = container_of(dev,
76 struct platform_device, dev);
77 struct resource *ior;
78
79 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
80 if (ior && (resource_size(ior) >= 0x800))
81 return ior;
82
83 return NULL;
84}
85
86static int ccp_platform_probe(struct platform_device *pdev)
87{
88 struct ccp_device *ccp;
89 struct device *dev = &pdev->dev;
90 struct resource *ior;
91 int ret;
92
93 ret = -ENOMEM;
94 ccp = ccp_alloc_struct(dev);
95 if (!ccp)
96 goto e_err;
97
98 ccp->dev_specific = NULL;
99 ccp->get_irq = ccp_get_irqs;
100 ccp->free_irq = ccp_free_irqs;
101
102 ior = ccp_find_mmio_area(ccp);
103 ccp->io_map = devm_ioremap_resource(dev, ior);
104 if (IS_ERR(ccp->io_map)) {
105 ret = PTR_ERR(ccp->io_map);
106 goto e_free;
107 }
108 ccp->io_regs = ccp->io_map;
109
110 if (!dev->dma_mask)
111 dev->dma_mask = &dev->coherent_dma_mask;
Tom Lendacky261bf072015-02-03 13:07:17 -0600112 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
113 if (ret) {
114 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
115 goto e_free;
116 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500117
Tom Lendacky126ae9a2014-07-10 10:58:35 -0500118 if (of_property_read_bool(dev->of_node, "dma-coherent"))
119 ccp->axcache = CACHE_WB_NO_ALLOC;
120 else
121 ccp->axcache = CACHE_NONE;
122
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500123 dev_set_drvdata(dev, ccp);
124
125 ret = ccp_init(ccp);
126 if (ret)
127 goto e_free;
128
129 dev_notice(dev, "enabled\n");
130
131 return 0;
132
133e_free:
134 kfree(ccp);
135
136e_err:
137 dev_notice(dev, "initialization failed\n");
138 return ret;
139}
140
141static int ccp_platform_remove(struct platform_device *pdev)
142{
143 struct device *dev = &pdev->dev;
144 struct ccp_device *ccp = dev_get_drvdata(dev);
145
146 ccp_destroy(ccp);
147
148 kfree(ccp);
149
150 dev_notice(dev, "disabled\n");
151
152 return 0;
153}
154
155#ifdef CONFIG_PM
156static int ccp_platform_suspend(struct platform_device *pdev,
157 pm_message_t state)
158{
159 struct device *dev = &pdev->dev;
160 struct ccp_device *ccp = dev_get_drvdata(dev);
161 unsigned long flags;
162 unsigned int i;
163
164 spin_lock_irqsave(&ccp->cmd_lock, flags);
165
166 ccp->suspending = 1;
167
168 /* Wake all the queue kthreads to prepare for suspend */
169 for (i = 0; i < ccp->cmd_q_count; i++)
170 wake_up_process(ccp->cmd_q[i].kthread);
171
172 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
173
174 /* Wait for all queue kthreads to say they're done */
175 while (!ccp_queues_suspended(ccp))
176 wait_event_interruptible(ccp->suspend_queue,
177 ccp_queues_suspended(ccp));
178
179 return 0;
180}
181
182static int ccp_platform_resume(struct platform_device *pdev)
183{
184 struct device *dev = &pdev->dev;
185 struct ccp_device *ccp = dev_get_drvdata(dev);
186 unsigned long flags;
187 unsigned int i;
188
189 spin_lock_irqsave(&ccp->cmd_lock, flags);
190
191 ccp->suspending = 0;
192
193 /* Wake up all the kthreads */
194 for (i = 0; i < ccp->cmd_q_count; i++) {
195 ccp->cmd_q[i].suspended = 0;
196 wake_up_process(ccp->cmd_q[i].kthread);
197 }
198
199 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
200
201 return 0;
202}
203#endif
204
205static const struct of_device_id ccp_platform_ids[] = {
206 { .compatible = "amd,ccp-seattle-v1a" },
207 { },
208};
209
210static struct platform_driver ccp_platform_driver = {
211 .driver = {
212 .name = "AMD Cryptographic Coprocessor",
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500213 .of_match_table = ccp_platform_ids,
214 },
215 .probe = ccp_platform_probe,
216 .remove = ccp_platform_remove,
217#ifdef CONFIG_PM
218 .suspend = ccp_platform_suspend,
219 .resume = ccp_platform_resume,
220#endif
221};
222
223int ccp_platform_init(void)
224{
225 return platform_driver_register(&ccp_platform_driver);
226}
227
228void ccp_platform_exit(void)
229{
230 platform_driver_unregister(&ccp_platform_driver);
231}