1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Secure Processor device driver
5 * Copyright (C) 2013,2019 Advanced Micro Devices, Inc.
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * Author: Gary R Hook <gary.hook@amd.com>
11 #include <linux/bitfield.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/pci.h>
16 #include <linux/pci_ids.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/kthread.h>
19 #include <linux/sched.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/delay.h>
23 #include <linux/ccp.h>
29 /* used for version string AA.BB.CC.DD */
30 #define AA GENMASK(31, 24)
31 #define BB GENMASK(23, 16)
32 #define CC GENMASK(15, 8)
33 #define DD GENMASK(7, 0)
35 #define MSIX_VECTORS 2
39 struct msix_entry msix_entry
[MSIX_VECTORS
];
41 static struct sp_device
*sp_dev_master
;
43 #define version_attribute_show(name, _offset) \
44 static ssize_t name##_show(struct device *d, struct device_attribute *attr, \
47 struct sp_device *sp = dev_get_drvdata(d); \
48 struct psp_device *psp = sp->psp_data; \
49 unsigned int val = ioread32(psp->io_regs + _offset); \
50 return sysfs_emit(buf, "%02lx.%02lx.%02lx.%02lx\n", \
54 FIELD_GET(DD, val)); \
57 version_attribute_show(bootloader_version
, psp
->vdata
->bootloader_info_reg
)
58 static DEVICE_ATTR_RO(bootloader_version
);
59 version_attribute_show(tee_version
, psp
->vdata
->tee
->info_reg
)
60 static DEVICE_ATTR_RO(tee_version
);
62 static struct attribute
*psp_firmware_attrs
[] = {
63 &dev_attr_bootloader_version
.attr
,
64 &dev_attr_tee_version
.attr
,
68 static umode_t
psp_firmware_is_visible(struct kobject
*kobj
, struct attribute
*attr
, int idx
)
70 struct device
*dev
= kobj_to_dev(kobj
);
71 struct sp_device
*sp
= dev_get_drvdata(dev
);
72 struct psp_device
*psp
= sp
->psp_data
;
73 unsigned int val
= 0xffffffff;
78 if (attr
== &dev_attr_bootloader_version
.attr
&&
79 psp
->vdata
->bootloader_info_reg
)
80 val
= ioread32(psp
->io_regs
+ psp
->vdata
->bootloader_info_reg
);
82 if (attr
== &dev_attr_tee_version
.attr
&& psp
->capability
.tee
&&
83 psp
->vdata
->tee
->info_reg
)
84 val
= ioread32(psp
->io_regs
+ psp
->vdata
->tee
->info_reg
);
86 /* If platform disallows accessing this register it will be all f's */
87 if (val
!= 0xffffffff)
93 static struct attribute_group psp_firmware_attr_group
= {
94 .attrs
= psp_firmware_attrs
,
95 .is_visible
= psp_firmware_is_visible
,
98 static const struct attribute_group
*psp_groups
[] = {
99 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
100 &psp_security_attr_group
,
102 &psp_firmware_attr_group
,
106 static int sp_get_msix_irqs(struct sp_device
*sp
)
108 struct sp_pci
*sp_pci
= sp
->dev_specific
;
109 struct device
*dev
= sp
->dev
;
110 struct pci_dev
*pdev
= to_pci_dev(dev
);
113 for (v
= 0; v
< ARRAY_SIZE(sp_pci
->msix_entry
); v
++)
114 sp_pci
->msix_entry
[v
].entry
= v
;
116 ret
= pci_enable_msix_range(pdev
, sp_pci
->msix_entry
, 1, v
);
120 sp_pci
->msix_count
= ret
;
121 sp
->use_tasklet
= true;
123 sp
->psp_irq
= sp_pci
->msix_entry
[0].vector
;
124 sp
->ccp_irq
= (sp_pci
->msix_count
> 1) ? sp_pci
->msix_entry
[1].vector
125 : sp_pci
->msix_entry
[0].vector
;
129 static int sp_get_msi_irq(struct sp_device
*sp
)
131 struct device
*dev
= sp
->dev
;
132 struct pci_dev
*pdev
= to_pci_dev(dev
);
135 ret
= pci_enable_msi(pdev
);
139 sp
->ccp_irq
= pdev
->irq
;
140 sp
->psp_irq
= pdev
->irq
;
145 static int sp_get_irqs(struct sp_device
*sp
)
147 struct device
*dev
= sp
->dev
;
150 ret
= sp_get_msix_irqs(sp
);
154 /* Couldn't get MSI-X vectors, try MSI */
155 dev_notice(dev
, "could not enable MSI-X (%d), trying MSI\n", ret
);
156 ret
= sp_get_msi_irq(sp
);
160 /* Couldn't get MSI interrupt */
161 dev_notice(dev
, "could not enable MSI (%d)\n", ret
);
166 static void sp_free_irqs(struct sp_device
*sp
)
168 struct sp_pci
*sp_pci
= sp
->dev_specific
;
169 struct device
*dev
= sp
->dev
;
170 struct pci_dev
*pdev
= to_pci_dev(dev
);
172 if (sp_pci
->msix_count
)
173 pci_disable_msix(pdev
);
174 else if (sp
->psp_irq
)
175 pci_disable_msi(pdev
);
181 static bool sp_pci_is_master(struct sp_device
*sp
)
183 struct device
*dev_cur
, *dev_new
;
184 struct pci_dev
*pdev_cur
, *pdev_new
;
187 dev_cur
= sp_dev_master
->dev
;
189 pdev_new
= to_pci_dev(dev_new
);
190 pdev_cur
= to_pci_dev(dev_cur
);
192 if (pdev_new
->bus
->number
< pdev_cur
->bus
->number
)
195 if (PCI_SLOT(pdev_new
->devfn
) < PCI_SLOT(pdev_cur
->devfn
))
198 if (PCI_FUNC(pdev_new
->devfn
) < PCI_FUNC(pdev_cur
->devfn
))
204 static void psp_set_master(struct sp_device
*sp
)
206 if (!sp_dev_master
) {
211 if (sp_pci_is_master(sp
))
215 static struct sp_device
*psp_get_master(void)
217 return sp_dev_master
;
220 static void psp_clear_master(struct sp_device
*sp
)
222 if (sp
== sp_dev_master
) {
223 sp_dev_master
= NULL
;
224 dev_dbg(sp
->dev
, "Cleared sp_dev_master\n");
228 static int sp_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
230 struct sp_device
*sp
;
231 struct sp_pci
*sp_pci
;
232 struct device
*dev
= &pdev
->dev
;
233 void __iomem
* const *iomap_table
;
238 sp
= sp_alloc_struct(dev
);
242 sp_pci
= devm_kzalloc(dev
, sizeof(*sp_pci
), GFP_KERNEL
);
246 sp
->dev_specific
= sp_pci
;
247 sp
->dev_vdata
= (struct sp_dev_vdata
*)id
->driver_data
;
248 if (!sp
->dev_vdata
) {
250 dev_err(dev
, "missing driver data\n");
254 ret
= pcim_enable_device(pdev
);
256 dev_err(dev
, "pcim_enable_device failed (%d)\n", ret
);
260 bar_mask
= pci_select_bars(pdev
, IORESOURCE_MEM
);
261 ret
= pcim_iomap_regions(pdev
, bar_mask
, "ccp");
263 dev_err(dev
, "pcim_iomap_regions failed (%d)\n", ret
);
267 iomap_table
= pcim_iomap_table(pdev
);
269 dev_err(dev
, "pcim_iomap_table failed\n");
274 sp
->io_map
= iomap_table
[sp
->dev_vdata
->bar
];
276 dev_err(dev
, "ioremap failed\n");
281 ret
= sp_get_irqs(sp
);
285 pci_set_master(pdev
);
286 sp
->set_psp_master_device
= psp_set_master
;
287 sp
->get_psp_master_device
= psp_get_master
;
288 sp
->clear_psp_master_device
= psp_clear_master
;
290 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(48));
292 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32));
294 dev_err(dev
, "dma_set_mask_and_coherent failed (%d)\n",
300 dev_set_drvdata(dev
, sp
);
311 dev_notice(dev
, "initialization failed\n");
315 static void sp_pci_shutdown(struct pci_dev
*pdev
)
317 struct device
*dev
= &pdev
->dev
;
318 struct sp_device
*sp
= dev_get_drvdata(dev
);
326 static void sp_pci_remove(struct pci_dev
*pdev
)
328 struct device
*dev
= &pdev
->dev
;
329 struct sp_device
*sp
= dev_get_drvdata(dev
);
339 static int __maybe_unused
sp_pci_suspend(struct device
*dev
)
341 struct sp_device
*sp
= dev_get_drvdata(dev
);
343 return sp_suspend(sp
);
346 static int __maybe_unused
sp_pci_resume(struct device
*dev
)
348 struct sp_device
*sp
= dev_get_drvdata(dev
);
350 return sp_resume(sp
);
353 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
354 static const struct sev_vdata sevv1
= {
355 .cmdresp_reg
= 0x10580, /* C2PMSG_32 */
356 .cmdbuff_addr_lo_reg
= 0x105e0, /* C2PMSG_56 */
357 .cmdbuff_addr_hi_reg
= 0x105e4, /* C2PMSG_57 */
360 static const struct sev_vdata sevv2
= {
361 .cmdresp_reg
= 0x10980, /* C2PMSG_32 */
362 .cmdbuff_addr_lo_reg
= 0x109e0, /* C2PMSG_56 */
363 .cmdbuff_addr_hi_reg
= 0x109e4, /* C2PMSG_57 */
366 static const struct tee_vdata teev1
= {
367 .ring_wptr_reg
= 0x10550, /* C2PMSG_20 */
368 .ring_rptr_reg
= 0x10554, /* C2PMSG_21 */
369 .info_reg
= 0x109e8, /* C2PMSG_58 */
372 static const struct tee_vdata teev2
= {
373 .ring_wptr_reg
= 0x10950, /* C2PMSG_20 */
374 .ring_rptr_reg
= 0x10954, /* C2PMSG_21 */
377 static const struct platform_access_vdata pa_v1
= {
378 .cmdresp_reg
= 0x10570, /* C2PMSG_28 */
379 .cmdbuff_addr_lo_reg
= 0x10574, /* C2PMSG_29 */
380 .cmdbuff_addr_hi_reg
= 0x10578, /* C2PMSG_30 */
381 .doorbell_button_reg
= 0x10a24, /* C2PMSG_73 */
382 .doorbell_cmd_reg
= 0x10a40, /* C2PMSG_80 */
385 static const struct platform_access_vdata pa_v2
= {
386 .doorbell_button_reg
= 0x10a24, /* C2PMSG_73 */
387 .doorbell_cmd_reg
= 0x10a40, /* C2PMSG_80 */
390 static const struct psp_vdata pspv1
= {
392 .bootloader_info_reg
= 0x105ec, /* C2PMSG_59 */
393 .feature_reg
= 0x105fc, /* C2PMSG_63 */
394 .inten_reg
= 0x10610, /* P2CMSG_INTEN */
395 .intsts_reg
= 0x10614, /* P2CMSG_INTSTS */
398 static const struct psp_vdata pspv2
= {
400 .platform_access
= &pa_v1
,
401 .bootloader_info_reg
= 0x109ec, /* C2PMSG_59 */
402 .feature_reg
= 0x109fc, /* C2PMSG_63 */
403 .inten_reg
= 0x10690, /* P2CMSG_INTEN */
404 .intsts_reg
= 0x10694, /* P2CMSG_INTSTS */
405 .platform_features
= PLATFORM_FEATURE_HSTI
,
408 static const struct psp_vdata pspv3
= {
410 .platform_access
= &pa_v1
,
411 .cmdresp_reg
= 0x10544, /* C2PMSG_17 */
412 .cmdbuff_addr_lo_reg
= 0x10548, /* C2PMSG_18 */
413 .cmdbuff_addr_hi_reg
= 0x1054c, /* C2PMSG_19 */
414 .bootloader_info_reg
= 0x109ec, /* C2PMSG_59 */
415 .feature_reg
= 0x109fc, /* C2PMSG_63 */
416 .inten_reg
= 0x10690, /* P2CMSG_INTEN */
417 .intsts_reg
= 0x10694, /* P2CMSG_INTSTS */
418 .platform_features
= PLATFORM_FEATURE_DBC
|
419 PLATFORM_FEATURE_HSTI
,
422 static const struct psp_vdata pspv4
= {
425 .cmdresp_reg
= 0x10544, /* C2PMSG_17 */
426 .cmdbuff_addr_lo_reg
= 0x10548, /* C2PMSG_18 */
427 .cmdbuff_addr_hi_reg
= 0x1054c, /* C2PMSG_19 */
428 .bootloader_info_reg
= 0x109ec, /* C2PMSG_59 */
429 .feature_reg
= 0x109fc, /* C2PMSG_63 */
430 .inten_reg
= 0x10690, /* P2CMSG_INTEN */
431 .intsts_reg
= 0x10694, /* P2CMSG_INTSTS */
434 static const struct psp_vdata pspv5
= {
436 .platform_access
= &pa_v2
,
437 .cmdresp_reg
= 0x10944, /* C2PMSG_17 */
438 .cmdbuff_addr_lo_reg
= 0x10948, /* C2PMSG_18 */
439 .cmdbuff_addr_hi_reg
= 0x1094c, /* C2PMSG_19 */
440 .feature_reg
= 0x109fc, /* C2PMSG_63 */
441 .inten_reg
= 0x10510, /* P2CMSG_INTEN */
442 .intsts_reg
= 0x10514, /* P2CMSG_INTSTS */
445 static const struct psp_vdata pspv6
= {
448 .cmdresp_reg
= 0x10944, /* C2PMSG_17 */
449 .cmdbuff_addr_lo_reg
= 0x10948, /* C2PMSG_18 */
450 .cmdbuff_addr_hi_reg
= 0x1094c, /* C2PMSG_19 */
451 .feature_reg
= 0x109fc, /* C2PMSG_63 */
452 .inten_reg
= 0x10510, /* P2CMSG_INTEN */
453 .intsts_reg
= 0x10514, /* P2CMSG_INTSTS */
458 static const struct sp_dev_vdata dev_vdata
[] = {
461 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
467 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
468 .ccp_vdata
= &ccpv5a
,
470 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
476 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
477 .ccp_vdata
= &ccpv5b
,
482 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
483 .ccp_vdata
= &ccpv5a
,
485 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
491 #ifdef CONFIG_CRYPTO_DEV_SP_CCP
492 .ccp_vdata
= &ccpv5a
,
494 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
500 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
506 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
512 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
518 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
523 static const struct pci_device_id sp_pci_table
[] = {
524 { PCI_VDEVICE(AMD
, 0x1537), (kernel_ulong_t
)&dev_vdata
[0] },
525 { PCI_VDEVICE(AMD
, 0x1456), (kernel_ulong_t
)&dev_vdata
[1] },
526 { PCI_VDEVICE(AMD
, 0x1468), (kernel_ulong_t
)&dev_vdata
[2] },
527 { PCI_VDEVICE(AMD
, 0x1486), (kernel_ulong_t
)&dev_vdata
[3] },
528 { PCI_VDEVICE(AMD
, 0x15DF), (kernel_ulong_t
)&dev_vdata
[4] },
529 { PCI_VDEVICE(AMD
, 0x14CA), (kernel_ulong_t
)&dev_vdata
[5] },
530 { PCI_VDEVICE(AMD
, 0x15C7), (kernel_ulong_t
)&dev_vdata
[6] },
531 { PCI_VDEVICE(AMD
, 0x1649), (kernel_ulong_t
)&dev_vdata
[6] },
532 { PCI_VDEVICE(AMD
, 0x17E0), (kernel_ulong_t
)&dev_vdata
[7] },
533 { PCI_VDEVICE(AMD
, 0x156E), (kernel_ulong_t
)&dev_vdata
[8] },
534 /* Last entry must be zero */
537 MODULE_DEVICE_TABLE(pci
, sp_pci_table
);
539 static SIMPLE_DEV_PM_OPS(sp_pci_pm_ops
, sp_pci_suspend
, sp_pci_resume
);
541 static struct pci_driver sp_pci_driver
= {
543 .id_table
= sp_pci_table
,
544 .probe
= sp_pci_probe
,
545 .remove
= sp_pci_remove
,
546 .shutdown
= sp_pci_shutdown
,
547 .driver
.pm
= &sp_pci_pm_ops
,
548 .dev_groups
= psp_groups
,
551 int sp_pci_init(void)
553 return pci_register_driver(&sp_pci_driver
);
556 void sp_pci_exit(void)
558 pci_unregister_driver(&sp_pci_driver
);