Commit | Line | Data |
---|---|---|
c4f4b325 TL |
1 | /* |
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> | |
126ae9ad | 25 | #include <linux/of.h> |
6c506343 TL |
26 | #include <linux/of_address.h> |
27 | #include <linux/acpi.h> | |
c4f4b325 TL |
28 | |
29 | #include "ccp-dev.h" | |
30 | ||
6c506343 TL |
31 | struct ccp_platform { |
32 | int use_acpi; | |
33 | int coherent; | |
34 | }; | |
35 | ||
c4f4b325 TL |
36 | static 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 | ||
57 | static 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 | ||
72 | static void ccp_free_irqs(struct ccp_device *ccp) | |
73 | { | |
74 | struct device *dev = ccp->dev; | |
75 | ||
76 | free_irq(ccp->irq, dev); | |
77 | } | |
78 | ||
79 | static 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 | ||
93 | static int ccp_platform_probe(struct platform_device *pdev) | |
94 | { | |
95 | struct ccp_device *ccp; | |
6c506343 | 96 | struct ccp_platform *ccp_platform; |
c4f4b325 | 97 | struct device *dev = &pdev->dev; |
6c506343 | 98 | struct acpi_device *adev = ACPI_COMPANION(dev); |
1831eff8 | 99 | enum dev_dma_attr attr; |
c4f4b325 TL |
100 | struct resource *ior; |
101 | int ret; | |
102 | ||
103 | ret = -ENOMEM; | |
104 | ccp = ccp_alloc_struct(dev); | |
105 | if (!ccp) | |
106 | goto e_err; | |
107 | ||
6c506343 TL |
108 | ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL); |
109 | if (!ccp_platform) | |
110 | goto e_err; | |
111 | ||
112 | ccp->dev_specific = ccp_platform; | |
c4f4b325 TL |
113 | ccp->get_irq = ccp_get_irqs; |
114 | ccp->free_irq = ccp_free_irqs; | |
115 | ||
6c506343 TL |
116 | ccp_platform->use_acpi = (!adev || acpi_disabled) ? 0 : 1; |
117 | ||
c4f4b325 TL |
118 | ior = ccp_find_mmio_area(ccp); |
119 | ccp->io_map = devm_ioremap_resource(dev, ior); | |
120 | if (IS_ERR(ccp->io_map)) { | |
121 | ret = PTR_ERR(ccp->io_map); | |
be03a3a0 | 122 | goto e_err; |
c4f4b325 TL |
123 | } |
124 | ccp->io_regs = ccp->io_map; | |
125 | ||
1831eff8 SS |
126 | attr = device_get_dma_attr(dev); |
127 | if (attr == DEV_DMA_NOT_SUPPORTED) { | |
128 | dev_err(dev, "DMA is not supported"); | |
be03a3a0 | 129 | goto e_err; |
261bf074 | 130 | } |
c4f4b325 | 131 | |
1831eff8 | 132 | ccp_platform->coherent = (attr == DEV_DMA_COHERENT); |
6c506343 | 133 | if (ccp_platform->coherent) |
126ae9ad TL |
134 | ccp->axcache = CACHE_WB_NO_ALLOC; |
135 | else | |
136 | ccp->axcache = CACHE_NONE; | |
137 | ||
1831eff8 SS |
138 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); |
139 | if (ret) { | |
140 | dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); | |
141 | goto e_err; | |
142 | } | |
143 | ||
c4f4b325 TL |
144 | dev_set_drvdata(dev, ccp); |
145 | ||
146 | ret = ccp_init(ccp); | |
147 | if (ret) | |
be03a3a0 | 148 | goto e_err; |
c4f4b325 TL |
149 | |
150 | dev_notice(dev, "enabled\n"); | |
151 | ||
152 | return 0; | |
153 | ||
c4f4b325 TL |
154 | e_err: |
155 | dev_notice(dev, "initialization failed\n"); | |
156 | return ret; | |
157 | } | |
158 | ||
159 | static int ccp_platform_remove(struct platform_device *pdev) | |
160 | { | |
161 | struct device *dev = &pdev->dev; | |
162 | struct ccp_device *ccp = dev_get_drvdata(dev); | |
163 | ||
164 | ccp_destroy(ccp); | |
165 | ||
c4f4b325 TL |
166 | dev_notice(dev, "disabled\n"); |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | #ifdef CONFIG_PM | |
172 | static int ccp_platform_suspend(struct platform_device *pdev, | |
173 | pm_message_t state) | |
174 | { | |
175 | struct device *dev = &pdev->dev; | |
176 | struct ccp_device *ccp = dev_get_drvdata(dev); | |
177 | unsigned long flags; | |
178 | unsigned int i; | |
179 | ||
180 | spin_lock_irqsave(&ccp->cmd_lock, flags); | |
181 | ||
182 | ccp->suspending = 1; | |
183 | ||
184 | /* Wake all the queue kthreads to prepare for suspend */ | |
185 | for (i = 0; i < ccp->cmd_q_count; i++) | |
186 | wake_up_process(ccp->cmd_q[i].kthread); | |
187 | ||
188 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); | |
189 | ||
190 | /* Wait for all queue kthreads to say they're done */ | |
191 | while (!ccp_queues_suspended(ccp)) | |
192 | wait_event_interruptible(ccp->suspend_queue, | |
193 | ccp_queues_suspended(ccp)); | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
198 | static int ccp_platform_resume(struct platform_device *pdev) | |
199 | { | |
200 | struct device *dev = &pdev->dev; | |
201 | struct ccp_device *ccp = dev_get_drvdata(dev); | |
202 | unsigned long flags; | |
203 | unsigned int i; | |
204 | ||
205 | spin_lock_irqsave(&ccp->cmd_lock, flags); | |
206 | ||
207 | ccp->suspending = 0; | |
208 | ||
209 | /* Wake up all the kthreads */ | |
210 | for (i = 0; i < ccp->cmd_q_count; i++) { | |
211 | ccp->cmd_q[i].suspended = 0; | |
212 | wake_up_process(ccp->cmd_q[i].kthread); | |
213 | } | |
214 | ||
215 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | #endif | |
220 | ||
6c506343 TL |
221 | #ifdef CONFIG_ACPI |
222 | static const struct acpi_device_id ccp_acpi_match[] = { | |
223 | { "AMDI0C00", 0 }, | |
224 | { }, | |
225 | }; | |
6170511a | 226 | MODULE_DEVICE_TABLE(acpi, ccp_acpi_match); |
6c506343 TL |
227 | #endif |
228 | ||
229 | #ifdef CONFIG_OF | |
230 | static const struct of_device_id ccp_of_match[] = { | |
c4f4b325 TL |
231 | { .compatible = "amd,ccp-seattle-v1a" }, |
232 | { }, | |
233 | }; | |
6170511a | 234 | MODULE_DEVICE_TABLE(of, ccp_of_match); |
6c506343 | 235 | #endif |
c4f4b325 TL |
236 | |
237 | static struct platform_driver ccp_platform_driver = { | |
238 | .driver = { | |
239 | .name = "AMD Cryptographic Coprocessor", | |
6c506343 TL |
240 | #ifdef CONFIG_ACPI |
241 | .acpi_match_table = ccp_acpi_match, | |
242 | #endif | |
243 | #ifdef CONFIG_OF | |
244 | .of_match_table = ccp_of_match, | |
245 | #endif | |
c4f4b325 TL |
246 | }, |
247 | .probe = ccp_platform_probe, | |
248 | .remove = ccp_platform_remove, | |
249 | #ifdef CONFIG_PM | |
250 | .suspend = ccp_platform_suspend, | |
251 | .resume = ccp_platform_resume, | |
252 | #endif | |
253 | }; | |
254 | ||
255 | int ccp_platform_init(void) | |
256 | { | |
257 | return platform_driver_register(&ccp_platform_driver); | |
258 | } | |
259 | ||
260 | void ccp_platform_exit(void) | |
261 | { | |
262 | platform_driver_unregister(&ccp_platform_driver); | |
263 | } |