2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013 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/pci.h>
16 #include <linux/pci_ids.h>
17 #include <linux/kthread.h>
18 #include <linux/sched.h>
19 #include <linux/interrupt.h>
20 #include <linux/spinlock.h>
21 #include <linux/delay.h>
22 #include <linux/ccp.h>
27 #define MSIX_VECTORS 2
36 struct ccp_msix msix[MSIX_VECTORS];
39 static int ccp_get_msix_irqs(struct ccp_device *ccp)
41 struct ccp_pci *ccp_pci = ccp->dev_specific;
42 struct device *dev = ccp->dev;
43 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
44 struct msix_entry msix_entry[MSIX_VECTORS];
45 unsigned int name_len = sizeof(ccp_pci->msix[0].name) - 1;
48 for (v = 0; v < ARRAY_SIZE(msix_entry); v++)
49 msix_entry[v].entry = v;
51 ret = pci_enable_msix_range(pdev, msix_entry, 1, v);
55 ccp_pci->msix_count = ret;
56 for (v = 0; v < ccp_pci->msix_count; v++) {
57 /* Set the interrupt names and request the irqs */
58 snprintf(ccp_pci->msix[v].name, name_len, "ccp-%u", v);
59 ccp_pci->msix[v].vector = msix_entry[v].vector;
60 ret = request_irq(ccp_pci->msix[v].vector, ccp_irq_handler,
61 0, ccp_pci->msix[v].name, dev);
63 dev_notice(dev, "unable to allocate MSI-X IRQ (%d)\n",
73 free_irq(ccp_pci->msix[v].vector, dev);
75 pci_disable_msix(pdev);
77 ccp_pci->msix_count = 0;
82 static int ccp_get_msi_irq(struct ccp_device *ccp)
84 struct device *dev = ccp->dev;
85 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
88 ret = pci_enable_msi(pdev);
92 ret = request_irq(pdev->irq, ccp_irq_handler, 0, "ccp", dev);
94 dev_notice(dev, "unable to allocate MSI IRQ (%d)\n", ret);
101 pci_disable_msi(pdev);
106 static int ccp_get_irqs(struct ccp_device *ccp)
108 struct device *dev = ccp->dev;
111 ret = ccp_get_msix_irqs(ccp);
115 /* Couldn't get MSI-X vectors, try MSI */
116 dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
117 ret = ccp_get_msi_irq(ccp);
121 /* Couldn't get MSI interrupt */
122 dev_notice(dev, "could not enable MSI (%d)\n", ret);
127 static void ccp_free_irqs(struct ccp_device *ccp)
129 struct ccp_pci *ccp_pci = ccp->dev_specific;
130 struct device *dev = ccp->dev;
131 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
133 if (ccp_pci->msix_count) {
134 while (ccp_pci->msix_count--)
135 free_irq(ccp_pci->msix[ccp_pci->msix_count].vector,
137 pci_disable_msix(pdev);
139 free_irq(pdev->irq, dev);
140 pci_disable_msi(pdev);
144 static int ccp_find_mmio_area(struct ccp_device *ccp)
146 struct device *dev = ccp->dev;
147 struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
148 resource_size_t io_len;
149 unsigned long io_flags;
152 io_flags = pci_resource_flags(pdev, IO_BAR);
153 io_len = pci_resource_len(pdev, IO_BAR);
154 if ((io_flags & IORESOURCE_MEM) && (io_len >= (IO_OFFSET + 0x800)))
157 for (bar = 0; bar < PCI_STD_RESOURCE_END; bar++) {
158 io_flags = pci_resource_flags(pdev, bar);
159 io_len = pci_resource_len(pdev, bar);
160 if ((io_flags & IORESOURCE_MEM) &&
161 (io_len >= (IO_OFFSET + 0x800)))
168 static int ccp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
170 struct ccp_device *ccp;
171 struct ccp_pci *ccp_pci;
172 struct device *dev = &pdev->dev;
177 ccp = ccp_alloc_struct(dev);
181 ccp_pci = kzalloc(sizeof(*ccp_pci), GFP_KERNEL);
186 ccp->dev_specific = ccp_pci;
187 ccp->get_irq = ccp_get_irqs;
188 ccp->free_irq = ccp_free_irqs;
190 ret = pci_request_regions(pdev, "ccp");
192 dev_err(dev, "pci_request_regions failed (%d)\n", ret);
196 ret = pci_enable_device(pdev);
198 dev_err(dev, "pci_enable_device failed (%d)\n", ret);
202 pci_set_master(pdev);
204 ret = ccp_find_mmio_area(ccp);
210 ccp->io_map = pci_iomap(pdev, bar, 0);
211 if (ccp->io_map == NULL) {
212 dev_err(dev, "pci_iomap failed\n");
215 ccp->io_regs = ccp->io_map + IO_OFFSET;
217 ret = dma_set_mask(dev, DMA_BIT_MASK(48));
219 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(48));
222 "pci_set_consistent_dma_mask failed (%d)\n",
227 ret = dma_set_mask(dev, DMA_BIT_MASK(32));
229 dev_err(dev, "pci_set_dma_mask failed (%d)\n", ret);
234 dev_set_drvdata(dev, ccp);
240 dev_notice(dev, "enabled\n");
245 pci_iounmap(pdev, ccp->io_map);
248 pci_disable_device(pdev);
251 pci_release_regions(pdev);
260 dev_notice(dev, "initialization failed\n");
264 static void ccp_pci_remove(struct pci_dev *pdev)
266 struct device *dev = &pdev->dev;
267 struct ccp_device *ccp = dev_get_drvdata(dev);
274 pci_iounmap(pdev, ccp->io_map);
276 pci_disable_device(pdev);
278 pci_release_regions(pdev);
282 dev_notice(dev, "disabled\n");
286 static int ccp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
288 struct device *dev = &pdev->dev;
289 struct ccp_device *ccp = dev_get_drvdata(dev);
293 spin_lock_irqsave(&ccp->cmd_lock, flags);
297 /* Wake all the queue kthreads to prepare for suspend */
298 for (i = 0; i < ccp->cmd_q_count; i++)
299 wake_up_process(ccp->cmd_q[i].kthread);
301 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
303 /* Wait for all queue kthreads to say they're done */
304 while (!ccp_queues_suspended(ccp))
305 wait_event_interruptible(ccp->suspend_queue,
306 ccp_queues_suspended(ccp));
311 static int ccp_pci_resume(struct pci_dev *pdev)
313 struct device *dev = &pdev->dev;
314 struct ccp_device *ccp = dev_get_drvdata(dev);
318 spin_lock_irqsave(&ccp->cmd_lock, flags);
322 /* Wake up all the kthreads */
323 for (i = 0; i < ccp->cmd_q_count; i++) {
324 ccp->cmd_q[i].suspended = 0;
325 wake_up_process(ccp->cmd_q[i].kthread);
328 spin_unlock_irqrestore(&ccp->cmd_lock, flags);
334 static DEFINE_PCI_DEVICE_TABLE(ccp_pci_table) = {
335 { PCI_VDEVICE(AMD, 0x1537), },
336 /* Last entry must be zero */
339 MODULE_DEVICE_TABLE(pci, ccp_pci_table);
341 static struct pci_driver ccp_pci_driver = {
342 .name = "AMD Cryptographic Coprocessor",
343 .id_table = ccp_pci_table,
344 .probe = ccp_pci_probe,
345 .remove = ccp_pci_remove,
347 .suspend = ccp_pci_suspend,
348 .resume = ccp_pci_resume,
352 int ccp_pci_init(void)
354 return pci_register_driver(&ccp_pci_driver);
357 void ccp_pci_exit(void)
359 pci_unregister_driver(&ccp_pci_driver);