blob: f5f43c50698ac4e2fa3cea375d73bc000290f27d [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
Brijesh Singhd0ebbc02017-07-06 09:59:16 -05002 * AMD Secure Processor device driver
Tom Lendacky63b94502013-11-12 11:46:16 -06003 *
Gary R Hook553d2372016-03-01 13:49:04 -06004 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hookfba88552016-07-26 19:09:20 -05007 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06008 *
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>
Tom Lendacky3d775652014-06-05 10:17:45 -050016#include <linux/device.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060017#include <linux/pci.h>
18#include <linux/pci_ids.h>
Tom Lendacky3d775652014-06-05 10:17:45 -050019#include <linux/dma-mapping.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060020#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"
Brijesh Singh2a6170d2017-12-04 10:57:28 -060028#include "psp-dev.h"
Tom Lendacky63b94502013-11-12 11:46:16 -060029
Tom Lendacky63b94502013-11-12 11:46:16 -060030#define MSIX_VECTORS 2
31
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050032struct sp_pci {
Tom Lendacky63b94502013-11-12 11:46:16 -060033 int msix_count;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050034 struct msix_entry msix_entry[MSIX_VECTORS];
Tom Lendacky63b94502013-11-12 11:46:16 -060035};
Brijesh Singh2a6170d2017-12-04 10:57:28 -060036static struct sp_device *sp_dev_master;
Tom Lendacky63b94502013-11-12 11:46:16 -060037
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050038static int sp_get_msix_irqs(struct sp_device *sp)
Tom Lendacky63b94502013-11-12 11:46:16 -060039{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050040 struct sp_pci *sp_pci = sp->dev_specific;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050041 struct device *dev = sp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080042 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -060043 int v, ret;
44
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050045 for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
46 sp_pci->msix_entry[v].entry = v;
Tom Lendacky63b94502013-11-12 11:46:16 -060047
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050048 ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
Alexander Gordeev5347ee82014-04-15 09:54:31 +020049 if (ret < 0)
Tom Lendacky63b94502013-11-12 11:46:16 -060050 return ret;
51
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050052 sp_pci->msix_count = ret;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050053 sp->use_tasklet = true;
Tom Lendacky63b94502013-11-12 11:46:16 -060054
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050055 sp->psp_irq = sp_pci->msix_entry[0].vector;
56 sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
57 : sp_pci->msix_entry[0].vector;
Tom Lendacky63b94502013-11-12 11:46:16 -060058 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -060059}
60
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050061static int sp_get_msi_irq(struct sp_device *sp)
Tom Lendacky63b94502013-11-12 11:46:16 -060062{
Brijesh Singhf4d18d62017-07-06 09:59:15 -050063 struct device *dev = sp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080064 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -060065 int ret;
66
67 ret = pci_enable_msi(pdev);
68 if (ret)
69 return ret;
70
Brijesh Singhf4d18d62017-07-06 09:59:15 -050071 sp->ccp_irq = pdev->irq;
72 sp->psp_irq = pdev->irq;
Tom Lendacky63b94502013-11-12 11:46:16 -060073
74 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -060075}
76
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050077static int sp_get_irqs(struct sp_device *sp)
Tom Lendacky63b94502013-11-12 11:46:16 -060078{
Brijesh Singhf4d18d62017-07-06 09:59:15 -050079 struct device *dev = sp->dev;
Tom Lendacky63b94502013-11-12 11:46:16 -060080 int ret;
81
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050082 ret = sp_get_msix_irqs(sp);
Tom Lendacky63b94502013-11-12 11:46:16 -060083 if (!ret)
84 return 0;
85
86 /* Couldn't get MSI-X vectors, try MSI */
87 dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050088 ret = sp_get_msi_irq(sp);
Tom Lendacky63b94502013-11-12 11:46:16 -060089 if (!ret)
90 return 0;
91
92 /* Couldn't get MSI interrupt */
93 dev_notice(dev, "could not enable MSI (%d)\n", ret);
94
95 return ret;
96}
97
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050098static void sp_free_irqs(struct sp_device *sp)
Tom Lendacky63b94502013-11-12 11:46:16 -060099{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500100 struct sp_pci *sp_pci = sp->dev_specific;
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500101 struct device *dev = sp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +0800102 struct pci_dev *pdev = to_pci_dev(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600103
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500104 if (sp_pci->msix_count)
Tom Lendacky63b94502013-11-12 11:46:16 -0600105 pci_disable_msix(pdev);
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500106 else if (sp->psp_irq)
Tom Lendacky63b94502013-11-12 11:46:16 -0600107 pci_disable_msi(pdev);
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500108
109 sp->ccp_irq = 0;
110 sp->psp_irq = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600111}
112
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600113static bool sp_pci_is_master(struct sp_device *sp)
114{
115 struct device *dev_cur, *dev_new;
116 struct pci_dev *pdev_cur, *pdev_new;
117
118 dev_new = sp->dev;
119 dev_cur = sp_dev_master->dev;
120
121 pdev_new = to_pci_dev(dev_new);
122 pdev_cur = to_pci_dev(dev_cur);
123
124 if (pdev_new->bus->number < pdev_cur->bus->number)
125 return true;
126
127 if (PCI_SLOT(pdev_new->devfn) < PCI_SLOT(pdev_cur->devfn))
128 return true;
129
130 if (PCI_FUNC(pdev_new->devfn) < PCI_FUNC(pdev_cur->devfn))
131 return true;
132
133 return false;
134}
135
136static void psp_set_master(struct sp_device *sp)
137{
138 if (!sp_dev_master) {
139 sp_dev_master = sp;
140 return;
141 }
142
143 if (sp_pci_is_master(sp))
144 sp_dev_master = sp;
145}
146
147static struct sp_device *psp_get_master(void)
148{
149 return sp_dev_master;
150}
151
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500152static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Tom Lendacky63b94502013-11-12 11:46:16 -0600153{
Brijesh Singh720419f2017-07-06 09:59:14 -0500154 struct sp_device *sp;
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500155 struct sp_pci *sp_pci;
Tom Lendacky63b94502013-11-12 11:46:16 -0600156 struct device *dev = &pdev->dev;
Brijesh Singh970e8302017-07-06 09:59:13 -0500157 void __iomem * const *iomap_table;
158 int bar_mask;
Tom Lendacky63b94502013-11-12 11:46:16 -0600159 int ret;
160
161 ret = -ENOMEM;
Brijesh Singh720419f2017-07-06 09:59:14 -0500162 sp = sp_alloc_struct(dev);
163 if (!sp)
Tom Lendacky63b94502013-11-12 11:46:16 -0600164 goto e_err;
165
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500166 sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
167 if (!sp_pci)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600168 goto e_err;
169
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500170 sp->dev_specific = sp_pci;
Brijesh Singh720419f2017-07-06 09:59:14 -0500171 sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
172 if (!sp->dev_vdata) {
Gary R Hookc7019c42016-03-01 13:49:15 -0600173 ret = -ENODEV;
174 dev_err(dev, "missing driver data\n");
175 goto e_err;
176 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600177
Brijesh Singh970e8302017-07-06 09:59:13 -0500178 ret = pcim_enable_device(pdev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600179 if (ret) {
Brijesh Singh970e8302017-07-06 09:59:13 -0500180 dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600181 goto e_err;
Tom Lendacky63b94502013-11-12 11:46:16 -0600182 }
183
Brijesh Singh970e8302017-07-06 09:59:13 -0500184 bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
185 ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
Tom Lendacky63b94502013-11-12 11:46:16 -0600186 if (ret) {
Brijesh Singh970e8302017-07-06 09:59:13 -0500187 dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
188 goto e_err;
189 }
190
191 iomap_table = pcim_iomap_table(pdev);
192 if (!iomap_table) {
193 dev_err(dev, "pcim_iomap_table failed\n");
194 ret = -ENOMEM;
195 goto e_err;
196 }
197
Brijesh Singh720419f2017-07-06 09:59:14 -0500198 sp->io_map = iomap_table[sp->dev_vdata->bar];
199 if (!sp->io_map) {
Brijesh Singh970e8302017-07-06 09:59:13 -0500200 dev_err(dev, "ioremap failed\n");
201 ret = -ENOMEM;
202 goto e_err;
Tom Lendacky63b94502013-11-12 11:46:16 -0600203 }
204
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500205 ret = sp_get_irqs(sp);
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500206 if (ret)
207 goto e_err;
208
Tom Lendacky63b94502013-11-12 11:46:16 -0600209 pci_set_master(pdev);
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600210 sp->set_psp_master_device = psp_set_master;
211 sp->get_psp_master_device = psp_get_master;
Tom Lendacky63b94502013-11-12 11:46:16 -0600212
Tom Lendacky3d775652014-06-05 10:17:45 -0500213 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
214 if (ret) {
215 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
Tom Lendacky63b94502013-11-12 11:46:16 -0600216 if (ret) {
Tom Lendacky3d775652014-06-05 10:17:45 -0500217 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
Tom Lendacky63b94502013-11-12 11:46:16 -0600218 ret);
Brijesh Singh970e8302017-07-06 09:59:13 -0500219 goto e_err;
Tom Lendacky63b94502013-11-12 11:46:16 -0600220 }
221 }
222
Brijesh Singh720419f2017-07-06 09:59:14 -0500223 dev_set_drvdata(dev, sp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600224
Brijesh Singh720419f2017-07-06 09:59:14 -0500225 ret = sp_init(sp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600226 if (ret)
Brijesh Singh970e8302017-07-06 09:59:13 -0500227 goto e_err;
Tom Lendacky63b94502013-11-12 11:46:16 -0600228
229 dev_notice(dev, "enabled\n");
230
231 return 0;
232
Tom Lendacky63b94502013-11-12 11:46:16 -0600233e_err:
234 dev_notice(dev, "initialization failed\n");
235 return ret;
236}
237
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500238static void sp_pci_remove(struct pci_dev *pdev)
Tom Lendacky63b94502013-11-12 11:46:16 -0600239{
240 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500241 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600242
Brijesh Singh720419f2017-07-06 09:59:14 -0500243 if (!sp)
Tom Lendackydb34cf92014-01-06 13:34:29 -0600244 return;
245
Brijesh Singh720419f2017-07-06 09:59:14 -0500246 sp_destroy(sp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600247
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500248 sp_free_irqs(sp);
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500249
Tom Lendacky63b94502013-11-12 11:46:16 -0600250 dev_notice(dev, "disabled\n");
251}
252
253#ifdef CONFIG_PM
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500254static int sp_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Tom Lendacky63b94502013-11-12 11:46:16 -0600255{
256 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500257 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600258
Brijesh Singh720419f2017-07-06 09:59:14 -0500259 return sp_suspend(sp, state);
Tom Lendacky63b94502013-11-12 11:46:16 -0600260}
261
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500262static int sp_pci_resume(struct pci_dev *pdev)
Tom Lendacky63b94502013-11-12 11:46:16 -0600263{
264 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500265 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendacky63b94502013-11-12 11:46:16 -0600266
Brijesh Singh720419f2017-07-06 09:59:14 -0500267 return sp_resume(sp);
Tom Lendacky63b94502013-11-12 11:46:16 -0600268}
269#endif
270
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600271#ifdef CONFIG_CRYPTO_DEV_SP_PSP
272static const struct psp_vdata psp_entry = {
273 .offset = 0x10500,
274};
275#endif
276
Brijesh Singh720419f2017-07-06 09:59:14 -0500277static const struct sp_dev_vdata dev_vdata[] = {
278 {
279 .bar = 2,
280#ifdef CONFIG_CRYPTO_DEV_SP_CCP
281 .ccp_vdata = &ccpv3,
282#endif
283 },
284 {
285 .bar = 2,
286#ifdef CONFIG_CRYPTO_DEV_SP_CCP
287 .ccp_vdata = &ccpv5a,
288#endif
Brijesh Singh2a6170d2017-12-04 10:57:28 -0600289#ifdef CONFIG_CRYPTO_DEV_SP_PSP
290 .psp_vdata = &psp_entry
291#endif
Brijesh Singh720419f2017-07-06 09:59:14 -0500292 },
293 {
294 .bar = 2,
295#ifdef CONFIG_CRYPTO_DEV_SP_CCP
296 .ccp_vdata = &ccpv5b,
297#endif
298 },
299};
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500300static const struct pci_device_id sp_pci_table[] = {
Brijesh Singh720419f2017-07-06 09:59:14 -0500301 { PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
302 { PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
303 { PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
Tom Lendacky63b94502013-11-12 11:46:16 -0600304 /* Last entry must be zero */
305 { 0, }
306};
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500307MODULE_DEVICE_TABLE(pci, sp_pci_table);
Tom Lendacky63b94502013-11-12 11:46:16 -0600308
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500309static struct pci_driver sp_pci_driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500310 .name = "ccp",
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500311 .id_table = sp_pci_table,
312 .probe = sp_pci_probe,
313 .remove = sp_pci_remove,
Tom Lendacky63b94502013-11-12 11:46:16 -0600314#ifdef CONFIG_PM
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500315 .suspend = sp_pci_suspend,
316 .resume = sp_pci_resume,
Tom Lendacky63b94502013-11-12 11:46:16 -0600317#endif
318};
319
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500320int sp_pci_init(void)
Tom Lendacky63b94502013-11-12 11:46:16 -0600321{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500322 return pci_register_driver(&sp_pci_driver);
Tom Lendacky63b94502013-11-12 11:46:16 -0600323}
324
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500325void sp_pci_exit(void)
Tom Lendacky63b94502013-11-12 11:46:16 -0600326{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500327 pci_unregister_driver(&sp_pci_driver);
Tom Lendacky63b94502013-11-12 11:46:16 -0600328}