Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 1 | /* |
| 2 | * AMD Cryptographic Coprocessor (CCP) driver |
| 3 | * |
| 4 | * Copyright (C) 2013 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/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> |
| 23 | |
| 24 | #include "ccp-dev.h" |
| 25 | |
| 26 | #define IO_BAR 2 |
| 27 | #define MSIX_VECTORS 2 |
| 28 | |
| 29 | struct ccp_msix { |
| 30 | u32 vector; |
| 31 | char name[16]; |
| 32 | }; |
| 33 | |
| 34 | struct ccp_pci { |
| 35 | int msix_count; |
| 36 | struct ccp_msix msix[MSIX_VECTORS]; |
| 37 | }; |
| 38 | |
| 39 | static int ccp_get_msix_irqs(struct ccp_device *ccp) |
| 40 | { |
| 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; |
| 46 | int v, ret; |
| 47 | |
| 48 | for (v = 0; v < ARRAY_SIZE(msix_entry); v++) |
| 49 | msix_entry[v].entry = v; |
| 50 | |
Alexander Gordeev | 5347ee8 | 2014-04-15 09:54:31 +0200 | [diff] [blame] | 51 | ret = pci_enable_msix_range(pdev, msix_entry, 1, v); |
| 52 | if (ret < 0) |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 53 | return ret; |
| 54 | |
Alexander Gordeev | 5347ee8 | 2014-04-15 09:54:31 +0200 | [diff] [blame] | 55 | ccp_pci->msix_count = ret; |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 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); |
| 62 | if (ret) { |
| 63 | dev_notice(dev, "unable to allocate MSI-X IRQ (%d)\n", |
| 64 | ret); |
| 65 | goto e_irq; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | |
| 71 | e_irq: |
| 72 | while (v--) |
| 73 | free_irq(ccp_pci->msix[v].vector, dev); |
| 74 | |
| 75 | pci_disable_msix(pdev); |
| 76 | |
| 77 | ccp_pci->msix_count = 0; |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | static int ccp_get_msi_irq(struct ccp_device *ccp) |
| 83 | { |
| 84 | struct device *dev = ccp->dev; |
| 85 | struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); |
| 86 | int ret; |
| 87 | |
| 88 | ret = pci_enable_msi(pdev); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | |
| 92 | ret = request_irq(pdev->irq, ccp_irq_handler, 0, "ccp", dev); |
| 93 | if (ret) { |
| 94 | dev_notice(dev, "unable to allocate MSI IRQ (%d)\n", ret); |
| 95 | goto e_msi; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | |
| 100 | e_msi: |
| 101 | pci_disable_msi(pdev); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | static int ccp_get_irqs(struct ccp_device *ccp) |
| 107 | { |
| 108 | struct device *dev = ccp->dev; |
| 109 | int ret; |
| 110 | |
| 111 | ret = ccp_get_msix_irqs(ccp); |
| 112 | if (!ret) |
| 113 | return 0; |
| 114 | |
| 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); |
| 118 | if (!ret) |
| 119 | return 0; |
| 120 | |
| 121 | /* Couldn't get MSI interrupt */ |
| 122 | dev_notice(dev, "could not enable MSI (%d)\n", ret); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static void ccp_free_irqs(struct ccp_device *ccp) |
| 128 | { |
| 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); |
| 132 | |
| 133 | if (ccp_pci->msix_count) { |
| 134 | while (ccp_pci->msix_count--) |
| 135 | free_irq(ccp_pci->msix[ccp_pci->msix_count].vector, |
| 136 | dev); |
| 137 | pci_disable_msix(pdev); |
| 138 | } else { |
| 139 | free_irq(pdev->irq, dev); |
| 140 | pci_disable_msi(pdev); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static int ccp_find_mmio_area(struct ccp_device *ccp) |
| 145 | { |
| 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; |
| 150 | int bar; |
| 151 | |
| 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))) |
| 155 | return IO_BAR; |
| 156 | |
| 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))) |
| 162 | return bar; |
| 163 | } |
| 164 | |
| 165 | return -EIO; |
| 166 | } |
| 167 | |
| 168 | static int ccp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 169 | { |
| 170 | struct ccp_device *ccp; |
| 171 | struct ccp_pci *ccp_pci; |
| 172 | struct device *dev = &pdev->dev; |
| 173 | unsigned int bar; |
| 174 | int ret; |
| 175 | |
| 176 | ret = -ENOMEM; |
| 177 | ccp = ccp_alloc_struct(dev); |
| 178 | if (!ccp) |
| 179 | goto e_err; |
| 180 | |
| 181 | ccp_pci = kzalloc(sizeof(*ccp_pci), GFP_KERNEL); |
| 182 | if (!ccp_pci) { |
| 183 | ret = -ENOMEM; |
| 184 | goto e_free1; |
| 185 | } |
| 186 | ccp->dev_specific = ccp_pci; |
| 187 | ccp->get_irq = ccp_get_irqs; |
| 188 | ccp->free_irq = ccp_free_irqs; |
| 189 | |
| 190 | ret = pci_request_regions(pdev, "ccp"); |
| 191 | if (ret) { |
| 192 | dev_err(dev, "pci_request_regions failed (%d)\n", ret); |
| 193 | goto e_free2; |
| 194 | } |
| 195 | |
| 196 | ret = pci_enable_device(pdev); |
| 197 | if (ret) { |
| 198 | dev_err(dev, "pci_enable_device failed (%d)\n", ret); |
| 199 | goto e_regions; |
| 200 | } |
| 201 | |
| 202 | pci_set_master(pdev); |
| 203 | |
| 204 | ret = ccp_find_mmio_area(ccp); |
| 205 | if (ret < 0) |
| 206 | goto e_device; |
| 207 | bar = ret; |
| 208 | |
| 209 | ret = -EIO; |
| 210 | ccp->io_map = pci_iomap(pdev, bar, 0); |
| 211 | if (ccp->io_map == NULL) { |
| 212 | dev_err(dev, "pci_iomap failed\n"); |
| 213 | goto e_device; |
| 214 | } |
| 215 | ccp->io_regs = ccp->io_map + IO_OFFSET; |
| 216 | |
| 217 | ret = dma_set_mask(dev, DMA_BIT_MASK(48)); |
| 218 | if (ret == 0) { |
| 219 | ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(48)); |
| 220 | if (ret) { |
| 221 | dev_err(dev, |
| 222 | "pci_set_consistent_dma_mask failed (%d)\n", |
| 223 | ret); |
| 224 | goto e_bar0; |
| 225 | } |
| 226 | } else { |
| 227 | ret = dma_set_mask(dev, DMA_BIT_MASK(32)); |
| 228 | if (ret) { |
| 229 | dev_err(dev, "pci_set_dma_mask failed (%d)\n", ret); |
| 230 | goto e_bar0; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | dev_set_drvdata(dev, ccp); |
| 235 | |
| 236 | ret = ccp_init(ccp); |
| 237 | if (ret) |
| 238 | goto e_bar0; |
| 239 | |
| 240 | dev_notice(dev, "enabled\n"); |
| 241 | |
| 242 | return 0; |
| 243 | |
| 244 | e_bar0: |
| 245 | pci_iounmap(pdev, ccp->io_map); |
| 246 | |
| 247 | e_device: |
| 248 | pci_disable_device(pdev); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 249 | |
| 250 | e_regions: |
| 251 | pci_release_regions(pdev); |
| 252 | |
| 253 | e_free2: |
| 254 | kfree(ccp_pci); |
| 255 | |
| 256 | e_free1: |
| 257 | kfree(ccp); |
| 258 | |
| 259 | e_err: |
| 260 | dev_notice(dev, "initialization failed\n"); |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static void ccp_pci_remove(struct pci_dev *pdev) |
| 265 | { |
| 266 | struct device *dev = &pdev->dev; |
| 267 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 268 | |
Tom Lendacky | db34cf9 | 2014-01-06 13:34:29 -0600 | [diff] [blame] | 269 | if (!ccp) |
| 270 | return; |
| 271 | |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 272 | ccp_destroy(ccp); |
| 273 | |
| 274 | pci_iounmap(pdev, ccp->io_map); |
| 275 | |
| 276 | pci_disable_device(pdev); |
Tom Lendacky | 63b9450 | 2013-11-12 11:46:16 -0600 | [diff] [blame] | 277 | |
| 278 | pci_release_regions(pdev); |
| 279 | |
| 280 | kfree(ccp); |
| 281 | |
| 282 | dev_notice(dev, "disabled\n"); |
| 283 | } |
| 284 | |
| 285 | #ifdef CONFIG_PM |
| 286 | static int ccp_pci_suspend(struct pci_dev *pdev, pm_message_t state) |
| 287 | { |
| 288 | struct device *dev = &pdev->dev; |
| 289 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 290 | unsigned long flags; |
| 291 | unsigned int i; |
| 292 | |
| 293 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 294 | |
| 295 | ccp->suspending = 1; |
| 296 | |
| 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); |
| 300 | |
| 301 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 302 | |
| 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)); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int ccp_pci_resume(struct pci_dev *pdev) |
| 312 | { |
| 313 | struct device *dev = &pdev->dev; |
| 314 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 315 | unsigned long flags; |
| 316 | unsigned int i; |
| 317 | |
| 318 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 319 | |
| 320 | ccp->suspending = 0; |
| 321 | |
| 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); |
| 326 | } |
| 327 | |
| 328 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | static DEFINE_PCI_DEVICE_TABLE(ccp_pci_table) = { |
| 335 | { PCI_VDEVICE(AMD, 0x1537), }, |
| 336 | /* Last entry must be zero */ |
| 337 | { 0, } |
| 338 | }; |
| 339 | MODULE_DEVICE_TABLE(pci, ccp_pci_table); |
| 340 | |
| 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, |
| 346 | #ifdef CONFIG_PM |
| 347 | .suspend = ccp_pci_suspend, |
| 348 | .resume = ccp_pci_resume, |
| 349 | #endif |
| 350 | }; |
| 351 | |
| 352 | int ccp_pci_init(void) |
| 353 | { |
| 354 | return pci_register_driver(&ccp_pci_driver); |
| 355 | } |
| 356 | |
| 357 | void ccp_pci_exit(void) |
| 358 | { |
| 359 | pci_unregister_driver(&ccp_pci_driver); |
| 360 | } |