blob: 072bcedef3869813ca8ee8d62c26f6450cdb054c [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
Gary R Hook553d2372016-03-01 13:49:04 -06004 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hookfba88552016-07-26 19:09:20 -05007 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/kernel.h>
Tom Lendacky3d775652014-06-05 10:17:45 -050016#include <linux/device.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060017#include <linux/pci.h>
18#include <linux/pci_ids.h>
Tom Lendacky3d775652014-06-05 10:17:45 -050019#include <linux/dma-mapping.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060020#include <linux/kthread.h>
21#include <linux/sched.h>
22#include <linux/interrupt.h>
23#include <linux/spinlock.h>
24#include <linux/delay.h>
25#include <linux/ccp.h>
26
27#include "ccp-dev.h"
28
Tom Lendacky63b94502013-11-12 11:46:16 -060029#define MSIX_VECTORS 2
30
31struct ccp_msix {
32 u32 vector;
33 char name[16];
34};
35
36struct ccp_pci {
37 int msix_count;
38 struct ccp_msix msix[MSIX_VECTORS];
39};
40
41static int ccp_get_msix_irqs(struct ccp_device *ccp)
42{
43 struct ccp_pci *ccp_pci = ccp->dev_specific;
44 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080045 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -060046 struct msix_entry msix_entry[MSIX_VECTORS];
47 unsigned int name_len = sizeof(ccp_pci->msix[0].name) - 1;
48 int v, ret;
49
50 for (v = 0; v < ARRAY_SIZE(msix_entry); v++)
51 msix_entry[v].entry = v;
52
Alexander Gordeev5347ee82014-04-15 09:54:31 +020053 ret = pci_enable_msix_range(pdev, msix_entry, 1, v);
54 if (ret < 0)
Tom Lendacky63b94502013-11-12 11:46:16 -060055 return ret;
56
Alexander Gordeev5347ee82014-04-15 09:54:31 +020057 ccp_pci->msix_count = ret;
Tom Lendacky63b94502013-11-12 11:46:16 -060058 for (v = 0; v < ccp_pci->msix_count; v++) {
59 /* Set the interrupt names and request the irqs */
Gary R Hook553d2372016-03-01 13:49:04 -060060 snprintf(ccp_pci->msix[v].name, name_len, "%s-%u",
61 ccp->name, v);
Tom Lendacky63b94502013-11-12 11:46:16 -060062 ccp_pci->msix[v].vector = msix_entry[v].vector;
Gary R Hookea0375a2016-03-01 13:49:25 -060063 ret = request_irq(ccp_pci->msix[v].vector,
64 ccp->vdata->perform->irqhandler,
Tom Lendacky63b94502013-11-12 11:46:16 -060065 0, ccp_pci->msix[v].name, dev);
66 if (ret) {
67 dev_notice(dev, "unable to allocate MSI-X IRQ (%d)\n",
68 ret);
69 goto e_irq;
70 }
71 }
72
73 return 0;
74
75e_irq:
76 while (v--)
77 free_irq(ccp_pci->msix[v].vector, dev);
78
79 pci_disable_msix(pdev);
80
81 ccp_pci->msix_count = 0;
82
83 return ret;
84}
85
86static int ccp_get_msi_irq(struct ccp_device *ccp)
87{
88 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080089 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -060090 int ret;
91
92 ret = pci_enable_msi(pdev);
93 if (ret)
94 return ret;
95
Tom Lendacky3d775652014-06-05 10:17:45 -050096 ccp->irq = pdev->irq;
Gary R Hookea0375a2016-03-01 13:49:25 -060097 ret = request_irq(ccp->irq, ccp->vdata->perform->irqhandler, 0,
98 ccp->name, dev);
Tom Lendacky63b94502013-11-12 11:46:16 -060099 if (ret) {
100 dev_notice(dev, "unable to allocate MSI IRQ (%d)\n", ret);
101 goto e_msi;
102 }
103
104 return 0;
105
106e_msi:
107 pci_disable_msi(pdev);
108
109 return ret;
110}
111
112static int ccp_get_irqs(struct ccp_device *ccp)
113{
114 struct device *dev = ccp->dev;
115 int ret;
116
117 ret = ccp_get_msix_irqs(ccp);
118 if (!ret)
119 return 0;
120
121 /* Couldn't get MSI-X vectors, try MSI */
122 dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
123 ret = ccp_get_msi_irq(ccp);
124 if (!ret)
125 return 0;
126
127 /* Couldn't get MSI interrupt */
128 dev_notice(dev, "could not enable MSI (%d)\n", ret);
129
130 return ret;
131}
132
133static void ccp_free_irqs(struct ccp_device *ccp)
134{
135 struct ccp_pci *ccp_pci = ccp->dev_specific;
136 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +0800137 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600138
139 if (ccp_pci->msix_count) {
140 while (ccp_pci->msix_count--)
141 free_irq(ccp_pci->msix[ccp_pci->msix_count].vector,
142 dev);
143 pci_disable_msix(pdev);
144 } else {
Tom Lendacky3d775652014-06-05 10:17:45 -0500145 free_irq(ccp->irq, dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600146 pci_disable_msi(pdev);
147 }
148}
149
150static int ccp_find_mmio_area(struct ccp_device *ccp)
151{
152 struct device *dev = ccp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +0800153 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600154 resource_size_t io_len;
155 unsigned long io_flags;
Tom Lendacky63b94502013-11-12 11:46:16 -0600156
Gary R Hookfba88552016-07-26 19:09:20 -0500157 io_flags = pci_resource_flags(pdev, ccp->vdata->bar);
158 io_len = pci_resource_len(pdev, ccp->vdata->bar);
159 if ((io_flags & IORESOURCE_MEM) &&
160 (io_len >= (ccp->vdata->offset + 0x800)))
161 return ccp->vdata->bar;
Tom Lendacky63b94502013-11-12 11:46:16 -0600162
Tom Lendacky63b94502013-11-12 11:46:16 -0600163 return -EIO;
164}
165
166static int ccp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
167{
168 struct ccp_device *ccp;
169 struct ccp_pci *ccp_pci;
170 struct device *dev = &pdev->dev;
171 unsigned int bar;
172 int ret;
173
174 ret = -ENOMEM;
175 ccp = ccp_alloc_struct(dev);
176 if (!ccp)
177 goto e_err;
178
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600179 ccp_pci = devm_kzalloc(dev, sizeof(*ccp_pci), GFP_KERNEL);
180 if (!ccp_pci)
181 goto e_err;
182
Tom Lendacky63b94502013-11-12 11:46:16 -0600183 ccp->dev_specific = ccp_pci;
Gary R Hookc7019c42016-03-01 13:49:15 -0600184 ccp->vdata = (struct ccp_vdata *)id->driver_data;
185 if (!ccp->vdata || !ccp->vdata->version) {
186 ret = -ENODEV;
187 dev_err(dev, "missing driver data\n");
188 goto e_err;
189 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600190 ccp->get_irq = ccp_get_irqs;
191 ccp->free_irq = ccp_free_irqs;
192
193 ret = pci_request_regions(pdev, "ccp");
194 if (ret) {
195 dev_err(dev, "pci_request_regions failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600196 goto e_err;
Tom Lendacky63b94502013-11-12 11:46:16 -0600197 }
198
199 ret = pci_enable_device(pdev);
200 if (ret) {
201 dev_err(dev, "pci_enable_device failed (%d)\n", ret);
202 goto e_regions;
203 }
204
205 pci_set_master(pdev);
206
207 ret = ccp_find_mmio_area(ccp);
208 if (ret < 0)
209 goto e_device;
210 bar = ret;
211
212 ret = -EIO;
213 ccp->io_map = pci_iomap(pdev, bar, 0);
Tom Lendacky8db88462015-02-03 13:07:05 -0600214 if (!ccp->io_map) {
Tom Lendacky63b94502013-11-12 11:46:16 -0600215 dev_err(dev, "pci_iomap failed\n");
216 goto e_device;
217 }
Gary R Hookfba88552016-07-26 19:09:20 -0500218 ccp->io_regs = ccp->io_map + ccp->vdata->offset;
Tom Lendacky63b94502013-11-12 11:46:16 -0600219
Tom Lendacky3d775652014-06-05 10:17:45 -0500220 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
221 if (ret) {
222 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Tom Lendacky63b94502013-11-12 11:46:16 -0600223 if (ret) {
Tom Lendacky3d775652014-06-05 10:17:45 -0500224 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
Tom Lendacky63b94502013-11-12 11:46:16 -0600225 ret);
Tom Lendacky3d775652014-06-05 10:17:45 -0500226 goto e_iomap;
Tom Lendacky63b94502013-11-12 11:46:16 -0600227 }
228 }
229
230 dev_set_drvdata(dev, ccp);
231
Gary R Hookea0375a2016-03-01 13:49:25 -0600232 ret = ccp->vdata->perform->init(ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600233 if (ret)
Tom Lendacky3d775652014-06-05 10:17:45 -0500234 goto e_iomap;
Tom Lendacky63b94502013-11-12 11:46:16 -0600235
236 dev_notice(dev, "enabled\n");
237
238 return 0;
239
Tom Lendacky3d775652014-06-05 10:17:45 -0500240e_iomap:
Tom Lendacky63b94502013-11-12 11:46:16 -0600241 pci_iounmap(pdev, ccp->io_map);
242
243e_device:
244 pci_disable_device(pdev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600245
246e_regions:
247 pci_release_regions(pdev);
248
Tom Lendacky63b94502013-11-12 11:46:16 -0600249e_err:
250 dev_notice(dev, "initialization failed\n");
251 return ret;
252}
253
254static void ccp_pci_remove(struct pci_dev *pdev)
255{
256 struct device *dev = &pdev->dev;
257 struct ccp_device *ccp = dev_get_drvdata(dev);
258
Tom Lendackydb34cf92014-01-06 13:34:29 -0600259 if (!ccp)
260 return;
261
Gary R Hookea0375a2016-03-01 13:49:25 -0600262 ccp->vdata->perform->destroy(ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600263
264 pci_iounmap(pdev, ccp->io_map);
265
266 pci_disable_device(pdev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600267
268 pci_release_regions(pdev);
269
Tom Lendacky63b94502013-11-12 11:46:16 -0600270 dev_notice(dev, "disabled\n");
271}
272
273#ifdef CONFIG_PM
274static int ccp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
275{
276 struct device *dev = &pdev->dev;
277 struct ccp_device *ccp = dev_get_drvdata(dev);
278 unsigned long flags;
279 unsigned int i;
280
281 spin_lock_irqsave(&ccp->cmd_lock, flags);
282
283 ccp->suspending = 1;
284
285 /* Wake all the queue kthreads to prepare for suspend */
286 for (i = 0; i < ccp->cmd_q_count; i++)
287 wake_up_process(ccp->cmd_q[i].kthread);
288
289 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
290
291 /* Wait for all queue kthreads to say they're done */
292 while (!ccp_queues_suspended(ccp))
293 wait_event_interruptible(ccp->suspend_queue,
294 ccp_queues_suspended(ccp));
295
296 return 0;
297}
298
299static int ccp_pci_resume(struct pci_dev *pdev)
300{
301 struct device *dev = &pdev->dev;
302 struct ccp_device *ccp = dev_get_drvdata(dev);
303 unsigned long flags;
304 unsigned int i;
305
306 spin_lock_irqsave(&ccp->cmd_lock, flags);
307
308 ccp->suspending = 0;
309
310 /* Wake up all the kthreads */
311 for (i = 0; i < ccp->cmd_q_count; i++) {
312 ccp->cmd_q[i].suspended = 0;
313 wake_up_process(ccp->cmd_q[i].kthread);
314 }
315
316 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
317
318 return 0;
319}
320#endif
321
Benoit Taine9baa3c32014-08-08 15:56:03 +0200322static const struct pci_device_id ccp_pci_table[] = {
Gary R Hookc7019c42016-03-01 13:49:15 -0600323 { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&ccpv3 },
Tom Lendacky63b94502013-11-12 11:46:16 -0600324 /* Last entry must be zero */
325 { 0, }
326};
327MODULE_DEVICE_TABLE(pci, ccp_pci_table);
328
329static struct pci_driver ccp_pci_driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500330 .name = "ccp",
Tom Lendacky63b94502013-11-12 11:46:16 -0600331 .id_table = ccp_pci_table,
332 .probe = ccp_pci_probe,
333 .remove = ccp_pci_remove,
334#ifdef CONFIG_PM
335 .suspend = ccp_pci_suspend,
336 .resume = ccp_pci_resume,
337#endif
338};
339
340int ccp_pci_init(void)
341{
342 return pci_register_driver(&ccp_pci_driver);
343}
344
345void ccp_pci_exit(void)
346{
347 pci_unregister_driver(&ccp_pci_driver);
348}