GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / crypto / ccp / sp-pci.c
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 free_irqs;
177                 }
178         }
179
180         dev_set_drvdata(dev, sp);
181
182         ret = sp_init(sp);
183         if (ret)
184                 goto free_irqs;
185
186         dev_notice(dev, "enabled\n");
187
188         return 0;
189
190 free_irqs:
191         sp_free_irqs(sp);
192 e_err:
193         dev_notice(dev, "initialization failed\n");
194         return ret;
195 }
196
197 static void sp_pci_remove(struct pci_dev *pdev)
198 {
199         struct device *dev = &pdev->dev;
200         struct sp_device *sp = dev_get_drvdata(dev);
201
202         if (!sp)
203                 return;
204
205         sp_destroy(sp);
206
207         sp_free_irqs(sp);
208
209         dev_notice(dev, "disabled\n");
210 }
211
212 #ifdef CONFIG_PM
213 static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
214 {
215         struct device *dev = &pdev->dev;
216         struct sp_device *sp = dev_get_drvdata(dev);
217
218         return sp_suspend(sp, state);
219 }
220
221 static int sp_pci_resume(struct pci_dev *pdev)
222 {
223         struct device *dev = &pdev->dev;
224         struct sp_device *sp = dev_get_drvdata(dev);
225
226         return sp_resume(sp);
227 }
228 #endif
229
230 static const struct sp_dev_vdata dev_vdata[] = {
231         {
232                 .bar = 2,
233 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
234                 .ccp_vdata = &ccpv3,
235 #endif
236         },
237         {
238                 .bar = 2,
239 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
240                 .ccp_vdata = &ccpv5a,
241 #endif
242         },
243         {
244                 .bar = 2,
245 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
246                 .ccp_vdata = &ccpv5b,
247 #endif
248         },
249 };
250 static const struct pci_device_id sp_pci_table[] = {
251         { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
252         { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
253         { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
254         /* Last entry must be zero */
255         { 0, }
256 };
257 MODULE_DEVICE_TABLE(pci, sp_pci_table);
258
259 static struct pci_driver sp_pci_driver = {
260         .name = "ccp",
261         .id_table = sp_pci_table,
262         .probe = sp_pci_probe,
263         .remove = sp_pci_remove,
264 #ifdef CONFIG_PM
265         .suspend = sp_pci_suspend,
266         .resume = sp_pci_resume,
267 #endif
268 };
269
270 int sp_pci_init(void)
271 {
272         return pci_register_driver(&sp_pci_driver);
273 }
274
275 void sp_pci_exit(void)
276 {
277         pci_unregister_driver(&sp_pci_driver);
278 }