2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/pci.h>
17 #include <linux/pci_ids.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>
29 #define IO_OFFSET 0x20000
31 #define MSIX_VECTORS 2
40 struct ccp_msix msix
[MSIX_VECTORS
];
43 static int ccp_get_msix_irqs(struct ccp_device
*ccp
)
45 struct ccp_pci
*ccp_pci
= ccp
->dev_specific
;
46 struct device
*dev
= ccp
->dev
;
47 struct pci_dev
*pdev
= to_pci_dev(dev
);
48 struct msix_entry msix_entry
[MSIX_VECTORS
];
49 unsigned int name_len
= sizeof(ccp_pci
->msix
[0].name
) - 1;
52 for (v
= 0; v
< ARRAY_SIZE(msix_entry
); v
++)
53 msix_entry
[v
].entry
= v
;
55 ret
= pci_enable_msix_range(pdev
, msix_entry
, 1, v
);
59 ccp_pci
->msix_count
= ret
;
60 for (v
= 0; v
< ccp_pci
->msix_count
; v
++) {
61 /* Set the interrupt names and request the irqs */
62 snprintf(ccp_pci
->msix
[v
].name
, name_len
, "%s-%u",
64 ccp_pci
->msix
[v
].vector
= msix_entry
[v
].vector
;
65 ret
= request_irq(ccp_pci
->msix
[v
].vector
,
66 ccp
->vdata
->perform
->irqhandler
,
67 0, ccp_pci
->msix
[v
].name
, dev
);
69 dev_notice(dev
, "unable to allocate MSI-X IRQ (%d)\n",
79 free_irq(ccp_pci
->msix
[v
].vector
, dev
);
81 pci_disable_msix(pdev
);
83 ccp_pci
->msix_count
= 0;
88 static int ccp_get_msi_irq(struct ccp_device
*ccp
)
90 struct device
*dev
= ccp
->dev
;
91 struct pci_dev
*pdev
= to_pci_dev(dev
);
94 ret
= pci_enable_msi(pdev
);
99 ret
= request_irq(ccp
->irq
, ccp
->vdata
->perform
->irqhandler
, 0,
102 dev_notice(dev
, "unable to allocate MSI IRQ (%d)\n", ret
);
109 pci_disable_msi(pdev
);
114 static int ccp_get_irqs(struct ccp_device
*ccp
)
116 struct device
*dev
= ccp
->dev
;
119 ret
= ccp_get_msix_irqs(ccp
);
123 /* Couldn't get MSI-X vectors, try MSI */
124 dev_notice(dev
, "could not enable MSI-X (%d), trying MSI\n", ret
);
125 ret
= ccp_get_msi_irq(ccp
);
129 /* Couldn't get MSI interrupt */
130 dev_notice(dev
, "could not enable MSI (%d)\n", ret
);
135 static void ccp_free_irqs(struct ccp_device
*ccp
)
137 struct ccp_pci
*ccp_pci
= ccp
->dev_specific
;
138 struct device
*dev
= ccp
->dev
;
139 struct pci_dev
*pdev
= to_pci_dev(dev
);
141 if (ccp_pci
->msix_count
) {
142 while (ccp_pci
->msix_count
--)
143 free_irq(ccp_pci
->msix
[ccp_pci
->msix_count
].vector
,
145 pci_disable_msix(pdev
);
147 free_irq(ccp
->irq
, dev
);
148 pci_disable_msi(pdev
);
152 static int ccp_find_mmio_area(struct ccp_device
*ccp
)
154 struct device
*dev
= ccp
->dev
;
155 struct pci_dev
*pdev
= to_pci_dev(dev
);
156 resource_size_t io_len
;
157 unsigned long io_flags
;
159 io_flags
= pci_resource_flags(pdev
, IO_BAR
);
160 io_len
= pci_resource_len(pdev
, IO_BAR
);
161 if ((io_flags
& IORESOURCE_MEM
) && (io_len
>= (IO_OFFSET
+ 0x800)))
167 static int ccp_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
169 struct ccp_device
*ccp
;
170 struct ccp_pci
*ccp_pci
;
171 struct device
*dev
= &pdev
->dev
;
176 ccp
= ccp_alloc_struct(dev
);
180 ccp_pci
= devm_kzalloc(dev
, sizeof(*ccp_pci
), GFP_KERNEL
);
184 ccp
->dev_specific
= ccp_pci
;
185 ccp
->vdata
= (struct ccp_vdata
*)id
->driver_data
;
186 if (!ccp
->vdata
|| !ccp
->vdata
->version
) {
188 dev_err(dev
, "missing driver data\n");
191 ccp
->get_irq
= ccp_get_irqs
;
192 ccp
->free_irq
= ccp_free_irqs
;
194 ret
= pci_request_regions(pdev
, "ccp");
196 dev_err(dev
, "pci_request_regions failed (%d)\n", ret
);
200 ret
= pci_enable_device(pdev
);
202 dev_err(dev
, "pci_enable_device failed (%d)\n", ret
);
206 pci_set_master(pdev
);
208 ret
= ccp_find_mmio_area(ccp
);
214 ccp
->io_map
= pci_iomap(pdev
, bar
, 0);
216 dev_err(dev
, "pci_iomap failed\n");
219 ccp
->io_regs
= ccp
->io_map
+ IO_OFFSET
;
221 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(48));
223 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32));
225 dev_err(dev
, "dma_set_mask_and_coherent failed (%d)\n",
231 dev_set_drvdata(dev
, ccp
);
233 ret
= ccp
->vdata
->perform
->init(ccp
);
237 dev_notice(dev
, "enabled\n");
242 pci_iounmap(pdev
, ccp
->io_map
);
245 pci_disable_device(pdev
);
248 pci_release_regions(pdev
);
251 dev_notice(dev
, "initialization failed\n");
255 static void ccp_pci_remove(struct pci_dev
*pdev
)
257 struct device
*dev
= &pdev
->dev
;
258 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
263 ccp
->vdata
->perform
->destroy(ccp
);
265 pci_iounmap(pdev
, ccp
->io_map
);
267 pci_disable_device(pdev
);
269 pci_release_regions(pdev
);
271 dev_notice(dev
, "disabled\n");
275 static int ccp_pci_suspend(struct pci_dev
*pdev
, pm_message_t state
)
277 struct device
*dev
= &pdev
->dev
;
278 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
282 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
286 /* Wake all the queue kthreads to prepare for suspend */
287 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
288 wake_up_process(ccp
->cmd_q
[i
].kthread
);
290 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
292 /* Wait for all queue kthreads to say they're done */
293 while (!ccp_queues_suspended(ccp
))
294 wait_event_interruptible(ccp
->suspend_queue
,
295 ccp_queues_suspended(ccp
));
300 static int ccp_pci_resume(struct pci_dev
*pdev
)
302 struct device
*dev
= &pdev
->dev
;
303 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
307 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
311 /* Wake up all the kthreads */
312 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
313 ccp
->cmd_q
[i
].suspended
= 0;
314 wake_up_process(ccp
->cmd_q
[i
].kthread
);
317 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
323 static const struct pci_device_id ccp_pci_table
[] = {
324 { PCI_VDEVICE(AMD
, 0x1537), (kernel_ulong_t
)&ccpv3
},
325 /* Last entry must be zero */
328 MODULE_DEVICE_TABLE(pci
, ccp_pci_table
);
330 static struct pci_driver ccp_pci_driver
= {
332 .id_table
= ccp_pci_table
,
333 .probe
= ccp_pci_probe
,
334 .remove
= ccp_pci_remove
,
336 .suspend
= ccp_pci_suspend
,
337 .resume
= ccp_pci_resume
,
341 int ccp_pci_init(void)
343 return pci_register_driver(&ccp_pci_driver
);
346 void ccp_pci_exit(void)
348 pci_unregister_driver(&ccp_pci_driver
);