xref: /openbmc/linux/drivers/crypto/ccp/sp-pci.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d0ebbc0cSBrijesh Singh /*
3d0ebbc0cSBrijesh Singh  * AMD Secure Processor device driver
4d0ebbc0cSBrijesh Singh  *
533960accSRijo Thomas  * Copyright (C) 2013,2019 Advanced Micro Devices, Inc.
6d0ebbc0cSBrijesh Singh  *
7d0ebbc0cSBrijesh Singh  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8d0ebbc0cSBrijesh Singh  * Author: Gary R Hook <gary.hook@amd.com>
9d0ebbc0cSBrijesh Singh  */
10d0ebbc0cSBrijesh Singh 
112e424c33SMario Limonciello #include <linux/bitfield.h>
12d0ebbc0cSBrijesh Singh #include <linux/module.h>
13d0ebbc0cSBrijesh Singh #include <linux/kernel.h>
14d0ebbc0cSBrijesh Singh #include <linux/device.h>
15d0ebbc0cSBrijesh Singh #include <linux/pci.h>
16d0ebbc0cSBrijesh Singh #include <linux/pci_ids.h>
17d0ebbc0cSBrijesh Singh #include <linux/dma-mapping.h>
18d0ebbc0cSBrijesh Singh #include <linux/kthread.h>
19d0ebbc0cSBrijesh Singh #include <linux/sched.h>
20d0ebbc0cSBrijesh Singh #include <linux/interrupt.h>
21d0ebbc0cSBrijesh Singh #include <linux/spinlock.h>
22d0ebbc0cSBrijesh Singh #include <linux/delay.h>
23d0ebbc0cSBrijesh Singh #include <linux/ccp.h>
24d0ebbc0cSBrijesh Singh 
25d0ebbc0cSBrijesh Singh #include "ccp-dev.h"
26b93566f1SRijo Thomas #include "psp-dev.h"
27d0ebbc0cSBrijesh Singh 
282e424c33SMario Limonciello /* used for version string AA.BB.CC.DD */
292e424c33SMario Limonciello #define AA				GENMASK(31, 24)
302e424c33SMario Limonciello #define BB				GENMASK(23, 16)
312e424c33SMario Limonciello #define CC				GENMASK(15, 8)
322e424c33SMario Limonciello #define DD				GENMASK(7, 0)
332e424c33SMario Limonciello 
34d0ebbc0cSBrijesh Singh #define MSIX_VECTORS			2
35d0ebbc0cSBrijesh Singh 
36d0ebbc0cSBrijesh Singh struct sp_pci {
37d0ebbc0cSBrijesh Singh 	int msix_count;
38d0ebbc0cSBrijesh Singh 	struct msix_entry msix_entry[MSIX_VECTORS];
39d0ebbc0cSBrijesh Singh };
402a6170dfSBrijesh Singh static struct sp_device *sp_dev_master;
41d0ebbc0cSBrijesh Singh 
42b5827637SMario Limonciello #define security_attribute_show(name, def)					\
4350c4deccSMario Limonciello static ssize_t name##_show(struct device *d, struct device_attribute *attr,	\
4450c4deccSMario Limonciello 			   char *buf)						\
4550c4deccSMario Limonciello {										\
4650c4deccSMario Limonciello 	struct sp_device *sp = dev_get_drvdata(d);				\
4750c4deccSMario Limonciello 	struct psp_device *psp = sp->psp_data;					\
4850c4deccSMario Limonciello 	int bit = PSP_SECURITY_##def << PSP_CAPABILITY_PSP_SECURITY_OFFSET;	\
4950c4deccSMario Limonciello 	return sysfs_emit(buf, "%d\n", (psp->capability & bit) > 0);		\
5050c4deccSMario Limonciello }
5150c4deccSMario Limonciello 
52b5827637SMario Limonciello security_attribute_show(fused_part, FUSED_PART)
5350c4deccSMario Limonciello static DEVICE_ATTR_RO(fused_part);
54b5827637SMario Limonciello security_attribute_show(debug_lock_on, DEBUG_LOCK_ON)
5550c4deccSMario Limonciello static DEVICE_ATTR_RO(debug_lock_on);
56b5827637SMario Limonciello security_attribute_show(tsme_status, TSME_STATUS)
5750c4deccSMario Limonciello static DEVICE_ATTR_RO(tsme_status);
58b5827637SMario Limonciello security_attribute_show(anti_rollback_status, ANTI_ROLLBACK_STATUS)
5950c4deccSMario Limonciello static DEVICE_ATTR_RO(anti_rollback_status);
60b5827637SMario Limonciello security_attribute_show(rpmc_production_enabled, RPMC_PRODUCTION_ENABLED)
6150c4deccSMario Limonciello static DEVICE_ATTR_RO(rpmc_production_enabled);
62b5827637SMario Limonciello security_attribute_show(rpmc_spirom_available, RPMC_SPIROM_AVAILABLE)
6350c4deccSMario Limonciello static DEVICE_ATTR_RO(rpmc_spirom_available);
64b5827637SMario Limonciello security_attribute_show(hsp_tpm_available, HSP_TPM_AVAILABLE)
6550c4deccSMario Limonciello static DEVICE_ATTR_RO(hsp_tpm_available);
66b5827637SMario Limonciello security_attribute_show(rom_armor_enforced, ROM_ARMOR_ENFORCED)
6750c4deccSMario Limonciello static DEVICE_ATTR_RO(rom_armor_enforced);
6850c4deccSMario Limonciello 
69b5827637SMario Limonciello static struct attribute *psp_security_attrs[] = {
7050c4deccSMario Limonciello 	&dev_attr_fused_part.attr,
7150c4deccSMario Limonciello 	&dev_attr_debug_lock_on.attr,
7250c4deccSMario Limonciello 	&dev_attr_tsme_status.attr,
7350c4deccSMario Limonciello 	&dev_attr_anti_rollback_status.attr,
7450c4deccSMario Limonciello 	&dev_attr_rpmc_production_enabled.attr,
7550c4deccSMario Limonciello 	&dev_attr_rpmc_spirom_available.attr,
7650c4deccSMario Limonciello 	&dev_attr_hsp_tpm_available.attr,
7750c4deccSMario Limonciello 	&dev_attr_rom_armor_enforced.attr,
7850c4deccSMario Limonciello 	NULL
7950c4deccSMario Limonciello };
8050c4deccSMario Limonciello 
psp_security_is_visible(struct kobject * kobj,struct attribute * attr,int idx)8150c4deccSMario Limonciello static umode_t psp_security_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
8250c4deccSMario Limonciello {
8350c4deccSMario Limonciello 	struct device *dev = kobj_to_dev(kobj);
8450c4deccSMario Limonciello 	struct sp_device *sp = dev_get_drvdata(dev);
8550c4deccSMario Limonciello 	struct psp_device *psp = sp->psp_data;
8650c4deccSMario Limonciello 
8750c4deccSMario Limonciello 	if (psp && (psp->capability & PSP_CAPABILITY_PSP_SECURITY_REPORTING))
8850c4deccSMario Limonciello 		return 0444;
8950c4deccSMario Limonciello 
9050c4deccSMario Limonciello 	return 0;
9150c4deccSMario Limonciello }
9250c4deccSMario Limonciello 
93b5827637SMario Limonciello static struct attribute_group psp_security_attr_group = {
94b5827637SMario Limonciello 	.attrs = psp_security_attrs,
9550c4deccSMario Limonciello 	.is_visible = psp_security_is_visible,
9650c4deccSMario Limonciello };
9750c4deccSMario Limonciello 
982e424c33SMario Limonciello #define version_attribute_show(name, _offset)					\
992e424c33SMario Limonciello static ssize_t name##_show(struct device *d, struct device_attribute *attr,	\
1002e424c33SMario Limonciello 			   char *buf)						\
1012e424c33SMario Limonciello {										\
1022e424c33SMario Limonciello 	struct sp_device *sp = dev_get_drvdata(d);				\
1032e424c33SMario Limonciello 	struct psp_device *psp = sp->psp_data;					\
1042e424c33SMario Limonciello 	unsigned int val = ioread32(psp->io_regs + _offset);			\
1052e424c33SMario Limonciello 	return sysfs_emit(buf, "%02lx.%02lx.%02lx.%02lx\n",			\
1062e424c33SMario Limonciello 			  FIELD_GET(AA, val),			\
1072e424c33SMario Limonciello 			  FIELD_GET(BB, val),			\
1082e424c33SMario Limonciello 			  FIELD_GET(CC, val),			\
1092e424c33SMario Limonciello 			  FIELD_GET(DD, val));			\
1102e424c33SMario Limonciello }
1112e424c33SMario Limonciello 
1122e424c33SMario Limonciello version_attribute_show(bootloader_version, psp->vdata->bootloader_info_reg)
1132e424c33SMario Limonciello static DEVICE_ATTR_RO(bootloader_version);
1142e424c33SMario Limonciello version_attribute_show(tee_version, psp->vdata->tee->info_reg)
1152e424c33SMario Limonciello static DEVICE_ATTR_RO(tee_version);
1162e424c33SMario Limonciello 
1172e424c33SMario Limonciello static struct attribute *psp_firmware_attrs[] = {
1182e424c33SMario Limonciello 	&dev_attr_bootloader_version.attr,
1192e424c33SMario Limonciello 	&dev_attr_tee_version.attr,
1202e424c33SMario Limonciello 	NULL,
1212e424c33SMario Limonciello };
1222e424c33SMario Limonciello 
psp_firmware_is_visible(struct kobject * kobj,struct attribute * attr,int idx)1232e424c33SMario Limonciello static umode_t psp_firmware_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
1242e424c33SMario Limonciello {
1252e424c33SMario Limonciello 	struct device *dev = kobj_to_dev(kobj);
1262e424c33SMario Limonciello 	struct sp_device *sp = dev_get_drvdata(dev);
1272e424c33SMario Limonciello 	struct psp_device *psp = sp->psp_data;
1282e424c33SMario Limonciello 	unsigned int val = 0xffffffff;
1292e424c33SMario Limonciello 
1302e424c33SMario Limonciello 	if (!psp)
1312e424c33SMario Limonciello 		return 0;
1322e424c33SMario Limonciello 
1332e424c33SMario Limonciello 	if (attr == &dev_attr_bootloader_version.attr &&
1342e424c33SMario Limonciello 	    psp->vdata->bootloader_info_reg)
1352e424c33SMario Limonciello 		val = ioread32(psp->io_regs + psp->vdata->bootloader_info_reg);
1362e424c33SMario Limonciello 
1372e424c33SMario Limonciello 	if (attr == &dev_attr_tee_version.attr &&
1382e424c33SMario Limonciello 	    psp->capability & PSP_CAPABILITY_TEE &&
1392e424c33SMario Limonciello 	    psp->vdata->tee->info_reg)
1402e424c33SMario Limonciello 		val = ioread32(psp->io_regs + psp->vdata->tee->info_reg);
1412e424c33SMario Limonciello 
1422e424c33SMario Limonciello 	/* If platform disallows accessing this register it will be all f's */
1432e424c33SMario Limonciello 	if (val != 0xffffffff)
1442e424c33SMario Limonciello 		return 0444;
1452e424c33SMario Limonciello 
1462e424c33SMario Limonciello 	return 0;
1472e424c33SMario Limonciello }
1482e424c33SMario Limonciello 
1492e424c33SMario Limonciello static struct attribute_group psp_firmware_attr_group = {
1502e424c33SMario Limonciello 	.attrs = psp_firmware_attrs,
1512e424c33SMario Limonciello 	.is_visible = psp_firmware_is_visible,
1522e424c33SMario Limonciello };
1532e424c33SMario Limonciello 
15450c4deccSMario Limonciello static const struct attribute_group *psp_groups[] = {
155b5827637SMario Limonciello 	&psp_security_attr_group,
1562e424c33SMario Limonciello 	&psp_firmware_attr_group,
15750c4deccSMario Limonciello 	NULL,
15850c4deccSMario Limonciello };
15950c4deccSMario Limonciello 
sp_get_msix_irqs(struct sp_device * sp)160d0ebbc0cSBrijesh Singh static int sp_get_msix_irqs(struct sp_device *sp)
161d0ebbc0cSBrijesh Singh {
162d0ebbc0cSBrijesh Singh 	struct sp_pci *sp_pci = sp->dev_specific;
163d0ebbc0cSBrijesh Singh 	struct device *dev = sp->dev;
164d0ebbc0cSBrijesh Singh 	struct pci_dev *pdev = to_pci_dev(dev);
165d0ebbc0cSBrijesh Singh 	int v, ret;
166d0ebbc0cSBrijesh Singh 
167d0ebbc0cSBrijesh Singh 	for (v = 0; v < ARRAY_SIZE(sp_pci->msix_entry); v++)
168d0ebbc0cSBrijesh Singh 		sp_pci->msix_entry[v].entry = v;
169d0ebbc0cSBrijesh Singh 
170d0ebbc0cSBrijesh Singh 	ret = pci_enable_msix_range(pdev, sp_pci->msix_entry, 1, v);
171d0ebbc0cSBrijesh Singh 	if (ret < 0)
172d0ebbc0cSBrijesh Singh 		return ret;
173d0ebbc0cSBrijesh Singh 
174d0ebbc0cSBrijesh Singh 	sp_pci->msix_count = ret;
175d0ebbc0cSBrijesh Singh 	sp->use_tasklet = true;
176d0ebbc0cSBrijesh Singh 
177d0ebbc0cSBrijesh Singh 	sp->psp_irq = sp_pci->msix_entry[0].vector;
178d0ebbc0cSBrijesh Singh 	sp->ccp_irq = (sp_pci->msix_count > 1) ? sp_pci->msix_entry[1].vector
179d0ebbc0cSBrijesh Singh 					       : sp_pci->msix_entry[0].vector;
180d0ebbc0cSBrijesh Singh 	return 0;
181d0ebbc0cSBrijesh Singh }
182d0ebbc0cSBrijesh Singh 
sp_get_msi_irq(struct sp_device * sp)183d0ebbc0cSBrijesh Singh static int sp_get_msi_irq(struct sp_device *sp)
184d0ebbc0cSBrijesh Singh {
185d0ebbc0cSBrijesh Singh 	struct device *dev = sp->dev;
186d0ebbc0cSBrijesh Singh 	struct pci_dev *pdev = to_pci_dev(dev);
187d0ebbc0cSBrijesh Singh 	int ret;
188d0ebbc0cSBrijesh Singh 
189d0ebbc0cSBrijesh Singh 	ret = pci_enable_msi(pdev);
190d0ebbc0cSBrijesh Singh 	if (ret)
191d0ebbc0cSBrijesh Singh 		return ret;
192d0ebbc0cSBrijesh Singh 
193d0ebbc0cSBrijesh Singh 	sp->ccp_irq = pdev->irq;
194d0ebbc0cSBrijesh Singh 	sp->psp_irq = pdev->irq;
195d0ebbc0cSBrijesh Singh 
196d0ebbc0cSBrijesh Singh 	return 0;
197d0ebbc0cSBrijesh Singh }
198d0ebbc0cSBrijesh Singh 
sp_get_irqs(struct sp_device * sp)199d0ebbc0cSBrijesh Singh static int sp_get_irqs(struct sp_device *sp)
200d0ebbc0cSBrijesh Singh {
201d0ebbc0cSBrijesh Singh 	struct device *dev = sp->dev;
202d0ebbc0cSBrijesh Singh 	int ret;
203d0ebbc0cSBrijesh Singh 
204d0ebbc0cSBrijesh Singh 	ret = sp_get_msix_irqs(sp);
205d0ebbc0cSBrijesh Singh 	if (!ret)
206d0ebbc0cSBrijesh Singh 		return 0;
207d0ebbc0cSBrijesh Singh 
208d0ebbc0cSBrijesh Singh 	/* Couldn't get MSI-X vectors, try MSI */
209d0ebbc0cSBrijesh Singh 	dev_notice(dev, "could not enable MSI-X (%d), trying MSI\n", ret);
210d0ebbc0cSBrijesh Singh 	ret = sp_get_msi_irq(sp);
211d0ebbc0cSBrijesh Singh 	if (!ret)
212d0ebbc0cSBrijesh Singh 		return 0;
213d0ebbc0cSBrijesh Singh 
214d0ebbc0cSBrijesh Singh 	/* Couldn't get MSI interrupt */
215d0ebbc0cSBrijesh Singh 	dev_notice(dev, "could not enable MSI (%d)\n", ret);
216d0ebbc0cSBrijesh Singh 
217d0ebbc0cSBrijesh Singh 	return ret;
218d0ebbc0cSBrijesh Singh }
219d0ebbc0cSBrijesh Singh 
sp_free_irqs(struct sp_device * sp)220d0ebbc0cSBrijesh Singh static void sp_free_irqs(struct sp_device *sp)
221d0ebbc0cSBrijesh Singh {
222d0ebbc0cSBrijesh Singh 	struct sp_pci *sp_pci = sp->dev_specific;
223d0ebbc0cSBrijesh Singh 	struct device *dev = sp->dev;
224d0ebbc0cSBrijesh Singh 	struct pci_dev *pdev = to_pci_dev(dev);
225d0ebbc0cSBrijesh Singh 
226d0ebbc0cSBrijesh Singh 	if (sp_pci->msix_count)
227d0ebbc0cSBrijesh Singh 		pci_disable_msix(pdev);
228d0ebbc0cSBrijesh Singh 	else if (sp->psp_irq)
229d0ebbc0cSBrijesh Singh 		pci_disable_msi(pdev);
230d0ebbc0cSBrijesh Singh 
231d0ebbc0cSBrijesh Singh 	sp->ccp_irq = 0;
232d0ebbc0cSBrijesh Singh 	sp->psp_irq = 0;
233d0ebbc0cSBrijesh Singh }
234d0ebbc0cSBrijesh Singh 
sp_pci_is_master(struct sp_device * sp)2352a6170dfSBrijesh Singh static bool sp_pci_is_master(struct sp_device *sp)
2362a6170dfSBrijesh Singh {
2372a6170dfSBrijesh Singh 	struct device *dev_cur, *dev_new;
2382a6170dfSBrijesh Singh 	struct pci_dev *pdev_cur, *pdev_new;
2392a6170dfSBrijesh Singh 
2402a6170dfSBrijesh Singh 	dev_new = sp->dev;
2412a6170dfSBrijesh Singh 	dev_cur = sp_dev_master->dev;
2422a6170dfSBrijesh Singh 
2432a6170dfSBrijesh Singh 	pdev_new = to_pci_dev(dev_new);
2442a6170dfSBrijesh Singh 	pdev_cur = to_pci_dev(dev_cur);
2452a6170dfSBrijesh Singh 
2462a6170dfSBrijesh Singh 	if (pdev_new->bus->number < pdev_cur->bus->number)
2472a6170dfSBrijesh Singh 		return true;
2482a6170dfSBrijesh Singh 
2492a6170dfSBrijesh Singh 	if (PCI_SLOT(pdev_new->devfn) < PCI_SLOT(pdev_cur->devfn))
2502a6170dfSBrijesh Singh 		return true;
2512a6170dfSBrijesh Singh 
2522a6170dfSBrijesh Singh 	if (PCI_FUNC(pdev_new->devfn) < PCI_FUNC(pdev_cur->devfn))
2532a6170dfSBrijesh Singh 		return true;
2542a6170dfSBrijesh Singh 
2552a6170dfSBrijesh Singh 	return false;
2562a6170dfSBrijesh Singh }
2572a6170dfSBrijesh Singh 
psp_set_master(struct sp_device * sp)2582a6170dfSBrijesh Singh static void psp_set_master(struct sp_device *sp)
2592a6170dfSBrijesh Singh {
2602a6170dfSBrijesh Singh 	if (!sp_dev_master) {
2612a6170dfSBrijesh Singh 		sp_dev_master = sp;
2622a6170dfSBrijesh Singh 		return;
2632a6170dfSBrijesh Singh 	}
2642a6170dfSBrijesh Singh 
2652a6170dfSBrijesh Singh 	if (sp_pci_is_master(sp))
2662a6170dfSBrijesh Singh 		sp_dev_master = sp;
2672a6170dfSBrijesh Singh }
2682a6170dfSBrijesh Singh 
psp_get_master(void)2692a6170dfSBrijesh Singh static struct sp_device *psp_get_master(void)
2702a6170dfSBrijesh Singh {
2712a6170dfSBrijesh Singh 	return sp_dev_master;
2722a6170dfSBrijesh Singh }
2732a6170dfSBrijesh Singh 
psp_clear_master(struct sp_device * sp)27415f7a4c6SJohn Allen static void psp_clear_master(struct sp_device *sp)
27515f7a4c6SJohn Allen {
27615f7a4c6SJohn Allen 	if (sp == sp_dev_master) {
27715f7a4c6SJohn Allen 		sp_dev_master = NULL;
27815f7a4c6SJohn Allen 		dev_dbg(sp->dev, "Cleared sp_dev_master\n");
27915f7a4c6SJohn Allen 	}
28015f7a4c6SJohn Allen }
28115f7a4c6SJohn Allen 
sp_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)282d0ebbc0cSBrijesh Singh static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
283d0ebbc0cSBrijesh Singh {
284d0ebbc0cSBrijesh Singh 	struct sp_device *sp;
285d0ebbc0cSBrijesh Singh 	struct sp_pci *sp_pci;
286d0ebbc0cSBrijesh Singh 	struct device *dev = &pdev->dev;
287d0ebbc0cSBrijesh Singh 	void __iomem * const *iomap_table;
288d0ebbc0cSBrijesh Singh 	int bar_mask;
289d0ebbc0cSBrijesh Singh 	int ret;
290d0ebbc0cSBrijesh Singh 
291d0ebbc0cSBrijesh Singh 	ret = -ENOMEM;
292d0ebbc0cSBrijesh Singh 	sp = sp_alloc_struct(dev);
293d0ebbc0cSBrijesh Singh 	if (!sp)
294d0ebbc0cSBrijesh Singh 		goto e_err;
295d0ebbc0cSBrijesh Singh 
296d0ebbc0cSBrijesh Singh 	sp_pci = devm_kzalloc(dev, sizeof(*sp_pci), GFP_KERNEL);
297d0ebbc0cSBrijesh Singh 	if (!sp_pci)
298d0ebbc0cSBrijesh Singh 		goto e_err;
299d0ebbc0cSBrijesh Singh 
300d0ebbc0cSBrijesh Singh 	sp->dev_specific = sp_pci;
301d0ebbc0cSBrijesh Singh 	sp->dev_vdata = (struct sp_dev_vdata *)id->driver_data;
302d0ebbc0cSBrijesh Singh 	if (!sp->dev_vdata) {
303d0ebbc0cSBrijesh Singh 		ret = -ENODEV;
304d0ebbc0cSBrijesh Singh 		dev_err(dev, "missing driver data\n");
305d0ebbc0cSBrijesh Singh 		goto e_err;
306d0ebbc0cSBrijesh Singh 	}
307d0ebbc0cSBrijesh Singh 
308d0ebbc0cSBrijesh Singh 	ret = pcim_enable_device(pdev);
309d0ebbc0cSBrijesh Singh 	if (ret) {
310d0ebbc0cSBrijesh Singh 		dev_err(dev, "pcim_enable_device failed (%d)\n", ret);
311d0ebbc0cSBrijesh Singh 		goto e_err;
312d0ebbc0cSBrijesh Singh 	}
313d0ebbc0cSBrijesh Singh 
314d0ebbc0cSBrijesh Singh 	bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);
315d0ebbc0cSBrijesh Singh 	ret = pcim_iomap_regions(pdev, bar_mask, "ccp");
316d0ebbc0cSBrijesh Singh 	if (ret) {
317d0ebbc0cSBrijesh Singh 		dev_err(dev, "pcim_iomap_regions failed (%d)\n", ret);
318d0ebbc0cSBrijesh Singh 		goto e_err;
319d0ebbc0cSBrijesh Singh 	}
320d0ebbc0cSBrijesh Singh 
321d0ebbc0cSBrijesh Singh 	iomap_table = pcim_iomap_table(pdev);
322d0ebbc0cSBrijesh Singh 	if (!iomap_table) {
323d0ebbc0cSBrijesh Singh 		dev_err(dev, "pcim_iomap_table failed\n");
324d0ebbc0cSBrijesh Singh 		ret = -ENOMEM;
325d0ebbc0cSBrijesh Singh 		goto e_err;
326d0ebbc0cSBrijesh Singh 	}
327d0ebbc0cSBrijesh Singh 
328d0ebbc0cSBrijesh Singh 	sp->io_map = iomap_table[sp->dev_vdata->bar];
329d0ebbc0cSBrijesh Singh 	if (!sp->io_map) {
330d0ebbc0cSBrijesh Singh 		dev_err(dev, "ioremap failed\n");
331d0ebbc0cSBrijesh Singh 		ret = -ENOMEM;
332d0ebbc0cSBrijesh Singh 		goto e_err;
333d0ebbc0cSBrijesh Singh 	}
334d0ebbc0cSBrijesh Singh 
335d0ebbc0cSBrijesh Singh 	ret = sp_get_irqs(sp);
336d0ebbc0cSBrijesh Singh 	if (ret)
337d0ebbc0cSBrijesh Singh 		goto e_err;
338d0ebbc0cSBrijesh Singh 
339d0ebbc0cSBrijesh Singh 	pci_set_master(pdev);
3402a6170dfSBrijesh Singh 	sp->set_psp_master_device = psp_set_master;
3412a6170dfSBrijesh Singh 	sp->get_psp_master_device = psp_get_master;
34215f7a4c6SJohn Allen 	sp->clear_psp_master_device = psp_clear_master;
343d0ebbc0cSBrijesh Singh 
344d0ebbc0cSBrijesh Singh 	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
345d0ebbc0cSBrijesh Singh 	if (ret) {
346d0ebbc0cSBrijesh Singh 		ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
347d0ebbc0cSBrijesh Singh 		if (ret) {
348d0ebbc0cSBrijesh Singh 			dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n",
349d0ebbc0cSBrijesh Singh 				ret);
350a6f8e68eSChristophe JAILLET 			goto free_irqs;
351d0ebbc0cSBrijesh Singh 		}
352d0ebbc0cSBrijesh Singh 	}
353d0ebbc0cSBrijesh Singh 
354d0ebbc0cSBrijesh Singh 	dev_set_drvdata(dev, sp);
355d0ebbc0cSBrijesh Singh 
356d0ebbc0cSBrijesh Singh 	ret = sp_init(sp);
357d0ebbc0cSBrijesh Singh 	if (ret)
358a6f8e68eSChristophe JAILLET 		goto free_irqs;
359d0ebbc0cSBrijesh Singh 
360d0ebbc0cSBrijesh Singh 	return 0;
361d0ebbc0cSBrijesh Singh 
362a6f8e68eSChristophe JAILLET free_irqs:
363a6f8e68eSChristophe JAILLET 	sp_free_irqs(sp);
364d0ebbc0cSBrijesh Singh e_err:
365d0ebbc0cSBrijesh Singh 	dev_notice(dev, "initialization failed\n");
366d0ebbc0cSBrijesh Singh 	return ret;
367d0ebbc0cSBrijesh Singh }
368d0ebbc0cSBrijesh Singh 
sp_pci_shutdown(struct pci_dev * pdev)3695441a07aSBrijesh Singh static void sp_pci_shutdown(struct pci_dev *pdev)
3705441a07aSBrijesh Singh {
3715441a07aSBrijesh Singh 	struct device *dev = &pdev->dev;
3725441a07aSBrijesh Singh 	struct sp_device *sp = dev_get_drvdata(dev);
3735441a07aSBrijesh Singh 
3745441a07aSBrijesh Singh 	if (!sp)
3755441a07aSBrijesh Singh 		return;
3765441a07aSBrijesh Singh 
3775441a07aSBrijesh Singh 	sp_destroy(sp);
3785441a07aSBrijesh Singh }
3795441a07aSBrijesh Singh 
sp_pci_remove(struct pci_dev * pdev)380d0ebbc0cSBrijesh Singh static void sp_pci_remove(struct pci_dev *pdev)
381d0ebbc0cSBrijesh Singh {
382d0ebbc0cSBrijesh Singh 	struct device *dev = &pdev->dev;
383d0ebbc0cSBrijesh Singh 	struct sp_device *sp = dev_get_drvdata(dev);
384d0ebbc0cSBrijesh Singh 
385d0ebbc0cSBrijesh Singh 	if (!sp)
386d0ebbc0cSBrijesh Singh 		return;
387d0ebbc0cSBrijesh Singh 
388d0ebbc0cSBrijesh Singh 	sp_destroy(sp);
389d0ebbc0cSBrijesh Singh 
390d0ebbc0cSBrijesh Singh 	sp_free_irqs(sp);
391d0ebbc0cSBrijesh Singh }
392d0ebbc0cSBrijesh Singh 
sp_pci_suspend(struct device * dev)393f892a21fSVaibhav Gupta static int __maybe_unused sp_pci_suspend(struct device *dev)
394d0ebbc0cSBrijesh Singh {
395d0ebbc0cSBrijesh Singh 	struct sp_device *sp = dev_get_drvdata(dev);
396d0ebbc0cSBrijesh Singh 
397f892a21fSVaibhav Gupta 	return sp_suspend(sp);
398d0ebbc0cSBrijesh Singh }
399d0ebbc0cSBrijesh Singh 
sp_pci_resume(struct device * dev)400f892a21fSVaibhav Gupta static int __maybe_unused sp_pci_resume(struct device *dev)
401d0ebbc0cSBrijesh Singh {
402d0ebbc0cSBrijesh Singh 	struct sp_device *sp = dev_get_drvdata(dev);
403d0ebbc0cSBrijesh Singh 
404d0ebbc0cSBrijesh Singh 	return sp_resume(sp);
405d0ebbc0cSBrijesh Singh }
406d0ebbc0cSBrijesh Singh 
4072a6170dfSBrijesh Singh #ifdef CONFIG_CRYPTO_DEV_SP_PSP
4086eb0cc72SRijo Thomas static const struct sev_vdata sevv1 = {
409675c3919STom Lendacky 	.cmdresp_reg		= 0x10580,	/* C2PMSG_32 */
410675c3919STom Lendacky 	.cmdbuff_addr_lo_reg	= 0x105e0,	/* C2PMSG_56 */
411675c3919STom Lendacky 	.cmdbuff_addr_hi_reg	= 0x105e4,	/* C2PMSG_57 */
4126eb0cc72SRijo Thomas };
4136eb0cc72SRijo Thomas 
4146eb0cc72SRijo Thomas static const struct sev_vdata sevv2 = {
415675c3919STom Lendacky 	.cmdresp_reg		= 0x10980,	/* C2PMSG_32 */
416675c3919STom Lendacky 	.cmdbuff_addr_lo_reg	= 0x109e0,	/* C2PMSG_56 */
417675c3919STom Lendacky 	.cmdbuff_addr_hi_reg	= 0x109e4,	/* C2PMSG_57 */
4186eb0cc72SRijo Thomas };
4196eb0cc72SRijo Thomas 
42033960accSRijo Thomas static const struct tee_vdata teev1 = {
421675c3919STom Lendacky 	.cmdresp_reg		= 0x10544,	/* C2PMSG_17 */
422675c3919STom Lendacky 	.cmdbuff_addr_lo_reg	= 0x10548,	/* C2PMSG_18 */
423675c3919STom Lendacky 	.cmdbuff_addr_hi_reg	= 0x1054c,	/* C2PMSG_19 */
424675c3919STom Lendacky 	.ring_wptr_reg          = 0x10550,	/* C2PMSG_20 */
425675c3919STom Lendacky 	.ring_rptr_reg          = 0x10554,	/* C2PMSG_21 */
426e938b08aSMario Limonciello 	.info_reg		= 0x109e8,	/* C2PMSG_58 */
42733960accSRijo Thomas };
42833960accSRijo Thomas 
4294aa0931bSMario Limonciello static const struct tee_vdata teev2 = {
4304aa0931bSMario Limonciello 	.cmdresp_reg		= 0x10944,	/* C2PMSG_17 */
4314aa0931bSMario Limonciello 	.cmdbuff_addr_lo_reg	= 0x10948,	/* C2PMSG_18 */
4324aa0931bSMario Limonciello 	.cmdbuff_addr_hi_reg	= 0x1094c,	/* C2PMSG_19 */
4334aa0931bSMario Limonciello 	.ring_wptr_reg		= 0x10950,	/* C2PMSG_20 */
4344aa0931bSMario Limonciello 	.ring_rptr_reg		= 0x10954,	/* C2PMSG_21 */
4354aa0931bSMario Limonciello };
4364aa0931bSMario Limonciello 
43722351239SMario Limonciello static const struct platform_access_vdata pa_v1 = {
43822351239SMario Limonciello 	.cmdresp_reg		= 0x10570,	/* C2PMSG_28 */
43922351239SMario Limonciello 	.cmdbuff_addr_lo_reg	= 0x10574,	/* C2PMSG_29 */
44022351239SMario Limonciello 	.cmdbuff_addr_hi_reg	= 0x10578,	/* C2PMSG_30 */
441d5812571SMario Limonciello 	.doorbell_button_reg	= 0x10a24,	/* C2PMSG_73 */
442d5812571SMario Limonciello 	.doorbell_cmd_reg	= 0x10a40,	/* C2PMSG_80 */
44322351239SMario Limonciello };
44422351239SMario Limonciello 
4454aa0931bSMario Limonciello static const struct platform_access_vdata pa_v2 = {
4464aa0931bSMario Limonciello 	.doorbell_button_reg	= 0x10a24,	/* C2PMSG_73 */
4474aa0931bSMario Limonciello 	.doorbell_cmd_reg	= 0x10a40,	/* C2PMSG_80 */
4484aa0931bSMario Limonciello };
4494aa0931bSMario Limonciello 
4506eb0cc72SRijo Thomas static const struct psp_vdata pspv1 = {
4516eb0cc72SRijo Thomas 	.sev			= &sevv1,
452e938b08aSMario Limonciello 	.bootloader_info_reg	= 0x105ec,	/* C2PMSG_59 */
453675c3919STom Lendacky 	.feature_reg		= 0x105fc,	/* C2PMSG_63 */
454675c3919STom Lendacky 	.inten_reg		= 0x10610,	/* P2CMSG_INTEN */
455675c3919STom Lendacky 	.intsts_reg		= 0x10614,	/* P2CMSG_INTSTS */
4562a6170dfSBrijesh Singh };
457dcbc0c6eSTom Lendacky 
458dcbc0c6eSTom Lendacky static const struct psp_vdata pspv2 = {
4596eb0cc72SRijo Thomas 	.sev			= &sevv2,
460e938b08aSMario Limonciello 	.bootloader_info_reg	= 0x109ec,	/* C2PMSG_59 */
461675c3919STom Lendacky 	.feature_reg		= 0x109fc,	/* C2PMSG_63 */
462675c3919STom Lendacky 	.inten_reg		= 0x10690,	/* P2CMSG_INTEN */
463675c3919STom Lendacky 	.intsts_reg		= 0x10694,	/* P2CMSG_INTSTS */
464dcbc0c6eSTom Lendacky };
46533960accSRijo Thomas 
46633960accSRijo Thomas static const struct psp_vdata pspv3 = {
46733960accSRijo Thomas 	.tee			= &teev1,
46822351239SMario Limonciello 	.platform_access	= &pa_v1,
469e938b08aSMario Limonciello 	.bootloader_info_reg	= 0x109ec,	/* C2PMSG_59 */
470675c3919STom Lendacky 	.feature_reg		= 0x109fc,	/* C2PMSG_63 */
471675c3919STom Lendacky 	.inten_reg		= 0x10690,	/* P2CMSG_INTEN */
472675c3919STom Lendacky 	.intsts_reg		= 0x10694,	/* P2CMSG_INTSTS */
473*c04cf9e1SMario Limonciello 	.platform_features	= PLATFORM_FEATURE_DBC,
47433960accSRijo Thomas };
47510da230aSMario Limonciello 
47610da230aSMario Limonciello static const struct psp_vdata pspv4 = {
47710da230aSMario Limonciello 	.sev			= &sevv2,
47810da230aSMario Limonciello 	.tee			= &teev1,
479e938b08aSMario Limonciello 	.bootloader_info_reg	= 0x109ec,	/* C2PMSG_59 */
480675c3919STom Lendacky 	.feature_reg		= 0x109fc,	/* C2PMSG_63 */
481675c3919STom Lendacky 	.inten_reg		= 0x10690,	/* P2CMSG_INTEN */
482675c3919STom Lendacky 	.intsts_reg		= 0x10694,	/* P2CMSG_INTSTS */
48310da230aSMario Limonciello };
48410da230aSMario Limonciello 
4854aa0931bSMario Limonciello static const struct psp_vdata pspv5 = {
4864aa0931bSMario Limonciello 	.tee			= &teev2,
4874aa0931bSMario Limonciello 	.platform_access	= &pa_v2,
4884aa0931bSMario Limonciello 	.feature_reg		= 0x109fc,	/* C2PMSG_63 */
4894aa0931bSMario Limonciello 	.inten_reg		= 0x10510,	/* P2CMSG_INTEN */
4904aa0931bSMario Limonciello 	.intsts_reg		= 0x10514,	/* P2CMSG_INTSTS */
4914aa0931bSMario Limonciello };
4924aa0931bSMario Limonciello 
493bb4185e5SJohn Allen static const struct psp_vdata pspv6 = {
494bb4185e5SJohn Allen 	.sev                    = &sevv2,
495bb4185e5SJohn Allen 	.tee                    = &teev2,
496bb4185e5SJohn Allen 	.feature_reg            = 0x109fc,	/* C2PMSG_63 */
497bb4185e5SJohn Allen 	.inten_reg              = 0x10510,	/* P2CMSG_INTEN */
498bb4185e5SJohn Allen 	.intsts_reg             = 0x10514,	/* P2CMSG_INTSTS */
499bb4185e5SJohn Allen };
500bb4185e5SJohn Allen 
5012a6170dfSBrijesh Singh #endif
5022a6170dfSBrijesh Singh 
503d0ebbc0cSBrijesh Singh static const struct sp_dev_vdata dev_vdata[] = {
504dcbc0c6eSTom Lendacky 	{	/* 0 */
505d0ebbc0cSBrijesh Singh 		.bar = 2,
506d0ebbc0cSBrijesh Singh #ifdef CONFIG_CRYPTO_DEV_SP_CCP
507d0ebbc0cSBrijesh Singh 		.ccp_vdata = &ccpv3,
508d0ebbc0cSBrijesh Singh #endif
509d0ebbc0cSBrijesh Singh 	},
510dcbc0c6eSTom Lendacky 	{	/* 1 */
511d0ebbc0cSBrijesh Singh 		.bar = 2,
512d0ebbc0cSBrijesh Singh #ifdef CONFIG_CRYPTO_DEV_SP_CCP
513d0ebbc0cSBrijesh Singh 		.ccp_vdata = &ccpv5a,
514d0ebbc0cSBrijesh Singh #endif
5152a6170dfSBrijesh Singh #ifdef CONFIG_CRYPTO_DEV_SP_PSP
516dcbc0c6eSTom Lendacky 		.psp_vdata = &pspv1,
5172a6170dfSBrijesh Singh #endif
518d0ebbc0cSBrijesh Singh 	},
519dcbc0c6eSTom Lendacky 	{	/* 2 */
520d0ebbc0cSBrijesh Singh 		.bar = 2,
521d0ebbc0cSBrijesh Singh #ifdef CONFIG_CRYPTO_DEV_SP_CCP
522d0ebbc0cSBrijesh Singh 		.ccp_vdata = &ccpv5b,
523d0ebbc0cSBrijesh Singh #endif
524d0ebbc0cSBrijesh Singh 	},
525dcbc0c6eSTom Lendacky 	{	/* 3 */
526dcbc0c6eSTom Lendacky 		.bar = 2,
527dcbc0c6eSTom Lendacky #ifdef CONFIG_CRYPTO_DEV_SP_CCP
528dcbc0c6eSTom Lendacky 		.ccp_vdata = &ccpv5a,
529dcbc0c6eSTom Lendacky #endif
530dcbc0c6eSTom Lendacky #ifdef CONFIG_CRYPTO_DEV_SP_PSP
531dcbc0c6eSTom Lendacky 		.psp_vdata = &pspv2,
532dcbc0c6eSTom Lendacky #endif
533dcbc0c6eSTom Lendacky 	},
53433960accSRijo Thomas 	{	/* 4 */
53533960accSRijo Thomas 		.bar = 2,
53633960accSRijo Thomas #ifdef CONFIG_CRYPTO_DEV_SP_CCP
53733960accSRijo Thomas 		.ccp_vdata = &ccpv5a,
53833960accSRijo Thomas #endif
53933960accSRijo Thomas #ifdef CONFIG_CRYPTO_DEV_SP_PSP
54033960accSRijo Thomas 		.psp_vdata = &pspv3,
54133960accSRijo Thomas #endif
54233960accSRijo Thomas 	},
5433438de03SJohn Allen 	{	/* 5 */
5443438de03SJohn Allen 		.bar = 2,
5453438de03SJohn Allen #ifdef CONFIG_CRYPTO_DEV_SP_PSP
54610da230aSMario Limonciello 		.psp_vdata = &pspv4,
5473438de03SJohn Allen #endif
5483438de03SJohn Allen 	},
54996ec8dfdSMario Limonciello 	{	/* 6 */
55096ec8dfdSMario Limonciello 		.bar = 2,
55196ec8dfdSMario Limonciello #ifdef CONFIG_CRYPTO_DEV_SP_PSP
55296ec8dfdSMario Limonciello 		.psp_vdata = &pspv3,
55396ec8dfdSMario Limonciello #endif
55496ec8dfdSMario Limonciello 	},
5554aa0931bSMario Limonciello 	{	/* 7 */
5564aa0931bSMario Limonciello 		.bar = 2,
5574aa0931bSMario Limonciello #ifdef CONFIG_CRYPTO_DEV_SP_PSP
5584aa0931bSMario Limonciello 		.psp_vdata = &pspv5,
5594aa0931bSMario Limonciello #endif
5604aa0931bSMario Limonciello 	},
561bb4185e5SJohn Allen 	{	/* 8 */
562bb4185e5SJohn Allen 		.bar = 2,
563bb4185e5SJohn Allen #ifdef CONFIG_CRYPTO_DEV_SP_PSP
564bb4185e5SJohn Allen 		.psp_vdata = &pspv6,
565bb4185e5SJohn Allen #endif
566bb4185e5SJohn Allen 	},
567d0ebbc0cSBrijesh Singh };
568d0ebbc0cSBrijesh Singh static const struct pci_device_id sp_pci_table[] = {
569d0ebbc0cSBrijesh Singh 	{ PCI_VDEVICE(AMD, 0x1537), (kernel_ulong_t)&dev_vdata[0] },
570d0ebbc0cSBrijesh Singh 	{ PCI_VDEVICE(AMD, 0x1456), (kernel_ulong_t)&dev_vdata[1] },
571d0ebbc0cSBrijesh Singh 	{ PCI_VDEVICE(AMD, 0x1468), (kernel_ulong_t)&dev_vdata[2] },
572dcbc0c6eSTom Lendacky 	{ PCI_VDEVICE(AMD, 0x1486), (kernel_ulong_t)&dev_vdata[3] },
57333960accSRijo Thomas 	{ PCI_VDEVICE(AMD, 0x15DF), (kernel_ulong_t)&dev_vdata[4] },
5743438de03SJohn Allen 	{ PCI_VDEVICE(AMD, 0x14CA), (kernel_ulong_t)&dev_vdata[5] },
57596ec8dfdSMario Limonciello 	{ PCI_VDEVICE(AMD, 0x15C7), (kernel_ulong_t)&dev_vdata[6] },
576c79a3169SMario Limonciello 	{ PCI_VDEVICE(AMD, 0x1649), (kernel_ulong_t)&dev_vdata[6] },
5774aa0931bSMario Limonciello 	{ PCI_VDEVICE(AMD, 0x17E0), (kernel_ulong_t)&dev_vdata[7] },
578bb4185e5SJohn Allen 	{ PCI_VDEVICE(AMD, 0x156E), (kernel_ulong_t)&dev_vdata[8] },
579d0ebbc0cSBrijesh Singh 	/* Last entry must be zero */
580d0ebbc0cSBrijesh Singh 	{ 0, }
581d0ebbc0cSBrijesh Singh };
582d0ebbc0cSBrijesh Singh MODULE_DEVICE_TABLE(pci, sp_pci_table);
583d0ebbc0cSBrijesh Singh 
584f892a21fSVaibhav Gupta static SIMPLE_DEV_PM_OPS(sp_pci_pm_ops, sp_pci_suspend, sp_pci_resume);
585f892a21fSVaibhav Gupta 
586d0ebbc0cSBrijesh Singh static struct pci_driver sp_pci_driver = {
587d0ebbc0cSBrijesh Singh 	.name = "ccp",
588d0ebbc0cSBrijesh Singh 	.id_table = sp_pci_table,
589d0ebbc0cSBrijesh Singh 	.probe = sp_pci_probe,
590d0ebbc0cSBrijesh Singh 	.remove = sp_pci_remove,
5915441a07aSBrijesh Singh 	.shutdown = sp_pci_shutdown,
592f892a21fSVaibhav Gupta 	.driver.pm = &sp_pci_pm_ops,
59350c4deccSMario Limonciello 	.dev_groups = psp_groups,
594d0ebbc0cSBrijesh Singh };
595d0ebbc0cSBrijesh Singh 
sp_pci_init(void)596d0ebbc0cSBrijesh Singh int sp_pci_init(void)
597d0ebbc0cSBrijesh Singh {
598d0ebbc0cSBrijesh Singh 	return pci_register_driver(&sp_pci_driver);
599d0ebbc0cSBrijesh Singh }
600d0ebbc0cSBrijesh Singh 
sp_pci_exit(void)601d0ebbc0cSBrijesh Singh void sp_pci_exit(void)
602d0ebbc0cSBrijesh Singh {
603d0ebbc0cSBrijesh Singh 	pci_unregister_driver(&sp_pci_driver);
604d0ebbc0cSBrijesh Singh }
605