rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * AMD Secure Processor device driver |
| 3 | * |
| 4 | * Copyright (C) 2013,2016 Advanced Micro Devices, Inc. |
| 5 | * |
| 6 | * Author: Tom Lendacky <thomas.lendacky@amd.com> |
| 7 | * Author: Gary R Hook <gary.hook@amd.com> |
| 8 | * |
| 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> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/pci_ids.h> |
| 19 | #include <linux/dma-mapping.h> |
| 20 | #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 | |
| 29 | #define MSIX_VECTORS 2 |
| 30 | |
| 31 | struct sp_pci { |
| 32 | int msix_count; |
| 33 | struct msix_entry msix_entry[MSIX_VECTORS]; |
| 34 | }; |
| 35 | |
| 36 | static int sp_get_msix_irqs(struct sp_device *sp) |
| 37 | { |
| 38 | struct sp_pci *sp_pci = sp->dev_specific; |
| 39 | struct device *dev = sp->dev; |
| 40 | struct pci_dev *pdev = to_pci_dev(dev); |
| 41 | int v, ret; |
| 42 | |
| 43 | for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++) |
| 44 | sp_pci->msix_entry[v].entry = v; |
| 45 | |
| 46 | ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v); |
| 47 | if (ret < 0) |
| 48 | return ret; |
| 49 | |
| 50 | sp_pci->msix_count = ret; |
| 51 | sp->use_tasklet = true; |
| 52 | |
| 53 | sp->psp_irq = sp_pci->msix_entry[0].vector; |
| 54 | sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector |
| 55 | : sp_pci->msix_entry[0].vector; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int sp_get_msi_irq(struct sp_device *sp) |
| 60 | { |
| 61 | struct device *dev = sp->dev; |
| 62 | struct pci_dev *pdev = to_pci_dev(dev); |
| 63 | int ret; |
| 64 | |
| 65 | ret = pci_enable_msi(pdev); |
| 66 | if (ret) |
| 67 | return ret; |
| 68 | |
| 69 | sp->ccp_irq = pdev->irq; |
| 70 | sp->psp_irq = pdev->irq; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int sp_get_irqs(struct sp_device *sp) |
| 76 | { |
| 77 | struct device *dev = sp->dev; |
| 78 | int ret; |
| 79 | |
| 80 | ret = sp_get_msix_irqs(sp); |
| 81 | if (!ret) |
| 82 | return 0; |
| 83 | |
| 84 | /* Couldn't get MSI-X vectors, try MSI */ |
| 85 | dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret); |
| 86 | ret = sp_get_msi_irq(sp); |
| 87 | if (!ret) |
| 88 | return 0; |
| 89 | |
| 90 | /* Couldn't get MSI interrupt */ |
| 91 | dev_notice(dev, "could not enable MSI (%d)\n", ret); |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | static void sp_free_irqs(struct sp_device *sp) |
| 97 | { |
| 98 | struct sp_pci *sp_pci = sp->dev_specific; |
| 99 | struct device *dev = sp->dev; |
| 100 | struct pci_dev *pdev = to_pci_dev(dev); |
| 101 | |
| 102 | if (sp_pci->msix_count) |
| 103 | pci_disable_msix(pdev); |
| 104 | else if (sp->psp_irq) |
| 105 | pci_disable_msi(pdev); |
| 106 | |
| 107 | sp->ccp_irq = 0; |
| 108 | sp->psp_irq = 0; |
| 109 | } |
| 110 | |
| 111 | static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 112 | { |
| 113 | struct sp_device *sp; |
| 114 | struct sp_pci *sp_pci; |
| 115 | struct device *dev = &pdev->dev; |
| 116 | void __iomem * const *iomap_table; |
| 117 | int bar_mask; |
| 118 | int ret; |
| 119 | |
| 120 | ret = -ENOMEM; |
| 121 | sp = sp_alloc_struct(dev); |
| 122 | if (!sp) |
| 123 | goto e_err; |
| 124 | |
| 125 | sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL); |
| 126 | if (!sp_pci) |
| 127 | goto e_err; |
| 128 | |
| 129 | sp->dev_specific = sp_pci; |
| 130 | sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data; |
| 131 | if (!sp->dev_vdata) { |
| 132 | ret = -ENODEV; |
| 133 | dev_err(dev, "missing driver data\n"); |
| 134 | goto e_err; |
| 135 | } |
| 136 | |
| 137 | ret = pcim_enable_device(pdev); |
| 138 | if (ret) { |
| 139 | dev_err(dev, "pcim_enable_device failed (%d)\n", ret); |
| 140 | goto e_err; |
| 141 | } |
| 142 | |
| 143 | bar_mask = pci_select_bars(pdev, IORESOURCE_MEM); |
| 144 | ret = pcim_iomap_regions(pdev, bar_mask, "ccp"); |
| 145 | if (ret) { |
| 146 | dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret); |
| 147 | goto e_err; |
| 148 | } |
| 149 | |
| 150 | iomap_table = pcim_iomap_table(pdev); |
| 151 | if (!iomap_table) { |
| 152 | dev_err(dev, "pcim_iomap_table failed\n"); |
| 153 | ret = -ENOMEM; |
| 154 | goto e_err; |
| 155 | } |
| 156 | |
| 157 | sp->io_map = iomap_table[sp->dev_vdata->bar]; |
| 158 | if (!sp->io_map) { |
| 159 | dev_err(dev, "ioremap failed\n"); |
| 160 | ret = -ENOMEM; |
| 161 | goto e_err; |
| 162 | } |
| 163 | |
| 164 | ret = sp_get_irqs(sp); |
| 165 | if (ret) |
| 166 | goto e_err; |
| 167 | |
| 168 | pci_set_master(pdev); |
| 169 | |
| 170 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); |
| 171 | if (ret) { |
| 172 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
| 173 | if (ret) { |
| 174 | dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", |
| 175 | ret); |
| 176 | goto e_err; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | dev_set_drvdata(dev, sp); |
| 181 | |
| 182 | ret = sp_init(sp); |
| 183 | if (ret) |
| 184 | goto e_err; |
| 185 | |
| 186 | dev_notice(dev, "enabled\n"); |
| 187 | |
| 188 | return 0; |
| 189 | |
| 190 | e_err: |
| 191 | dev_notice(dev, "initialization failed\n"); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static void sp_pci_remove(struct pci_dev *pdev) |
| 196 | { |
| 197 | struct device *dev = &pdev->dev; |
| 198 | struct sp_device *sp = dev_get_drvdata(dev); |
| 199 | |
| 200 | if (!sp) |
| 201 | return; |
| 202 | |
| 203 | sp_destroy(sp); |
| 204 | |
| 205 | sp_free_irqs(sp); |
| 206 | |
| 207 | dev_notice(dev, "disabled\n"); |
| 208 | } |
| 209 | |
| 210 | #ifdef CONFIG_PM |
| 211 | static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state) |
| 212 | { |
| 213 | struct device *dev = &pdev->dev; |
| 214 | struct sp_device *sp = dev_get_drvdata(dev); |
| 215 | |
| 216 | return sp_suspend(sp, state); |
| 217 | } |
| 218 | |
| 219 | static int sp_pci_resume(struct pci_dev *pdev) |
| 220 | { |
| 221 | struct device *dev = &pdev->dev; |
| 222 | struct sp_device *sp = dev_get_drvdata(dev); |
| 223 | |
| 224 | return sp_resume(sp); |
| 225 | } |
| 226 | #endif |
| 227 | |
| 228 | static const struct sp_dev_vdata dev_vdata[] = { |
| 229 | { |
| 230 | .bar = 2, |
| 231 | #ifdef CONFIG_CRYPTO_DEV_SP_CCP |
| 232 | .ccp_vdata = &ccpv3, |
| 233 | #endif |
| 234 | }, |
| 235 | { |
| 236 | .bar = 2, |
| 237 | #ifdef CONFIG_CRYPTO_DEV_SP_CCP |
| 238 | .ccp_vdata = &ccpv5a, |
| 239 | #endif |
| 240 | }, |
| 241 | { |
| 242 | .bar = 2, |
| 243 | #ifdef CONFIG_CRYPTO_DEV_SP_CCP |
| 244 | .ccp_vdata = &ccpv5b, |
| 245 | #endif |
| 246 | }, |
| 247 | }; |
| 248 | static const struct pci_device_id sp_pci_table[] = { |
| 249 | { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] }, |
| 250 | { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] }, |
| 251 | { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] }, |
| 252 | /* Last entry must be zero */ |
| 253 | { 0, } |
| 254 | }; |
| 255 | MODULE_DEVICE_TABLE(pci, sp_pci_table); |
| 256 | |
| 257 | static struct pci_driver sp_pci_driver = { |
| 258 | .name = "ccp", |
| 259 | .id_table = sp_pci_table, |
| 260 | .probe = sp_pci_probe, |
| 261 | .remove = sp_pci_remove, |
| 262 | #ifdef CONFIG_PM |
| 263 | .suspend = sp_pci_suspend, |
| 264 | .resume = sp_pci_resume, |
| 265 | #endif |
| 266 | }; |
| 267 | |
| 268 | int sp_pci_init(void) |
| 269 | { |
| 270 | return pci_register_driver(&sp_pci_driver); |
| 271 | } |
| 272 | |
| 273 | void sp_pci_exit(void) |
| 274 | { |
| 275 | pci_unregister_driver(&sp_pci_driver); |
| 276 | } |