1 // SPDX-License-Identifier: GPL-2.0-only
3 * AMD Secure Processor driver
5 * Copyright (C) 2017-2018 Advanced Micro Devices, Inc.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/kthread.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/spinlock.h>
18 #include <linux/spinlock_types.h>
19 #include <linux/types.h>
20 #include <linux/ccp.h>
28 MODULE_LICENSE("GPL");
29 MODULE_VERSION("1.1.0");
30 MODULE_DESCRIPTION("AMD Secure Processor driver");
32 /* List of SPs, SP count, read-write access lock, and access functions
34 * Lock structure: get sp_unit_lock for reading whenever we need to
35 * examine the SP list.
37 static DEFINE_RWLOCK(sp_unit_lock);
38 static LIST_HEAD(sp_units);
40 /* Ever-increasing value to produce unique unit numbers */
41 static atomic_t sp_ordinal;
43 static void sp_add_device(struct sp_device *sp)
47 write_lock_irqsave(&sp_unit_lock, flags);
49 list_add_tail(&sp->entry, &sp_units);
51 write_unlock_irqrestore(&sp_unit_lock, flags);
54 static void sp_del_device(struct sp_device *sp)
58 write_lock_irqsave(&sp_unit_lock, flags);
62 write_unlock_irqrestore(&sp_unit_lock, flags);
65 static irqreturn_t sp_irq_handler(int irq, void *data)
67 struct sp_device *sp = data;
69 if (sp->ccp_irq_handler)
70 sp->ccp_irq_handler(irq, sp->ccp_irq_data);
72 if (sp->psp_irq_handler)
73 sp->psp_irq_handler(irq, sp->psp_irq_data);
78 int sp_request_ccp_irq(struct sp_device *sp, irq_handler_t handler,
79 const char *name, void *data)
83 if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->psp_vdata) {
84 /* Need a common routine to manage all interrupts */
85 sp->ccp_irq_data = data;
86 sp->ccp_irq_handler = handler;
88 if (!sp->irq_registered) {
89 ret = request_irq(sp->ccp_irq, sp_irq_handler, 0,
94 sp->irq_registered = true;
97 /* Each sub-device can manage it's own interrupt */
98 ret = request_irq(sp->ccp_irq, handler, 0, name, data);
106 int sp_request_psp_irq(struct sp_device *sp, irq_handler_t handler,
107 const char *name, void *data)
111 if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->ccp_vdata) {
112 /* Need a common routine to manage all interrupts */
113 sp->psp_irq_data = data;
114 sp->psp_irq_handler = handler;
116 if (!sp->irq_registered) {
117 ret = request_irq(sp->psp_irq, sp_irq_handler, 0,
122 sp->irq_registered = true;
125 /* Each sub-device can manage it's own interrupt */
126 ret = request_irq(sp->psp_irq, handler, 0, name, data);
134 void sp_free_ccp_irq(struct sp_device *sp, void *data)
136 if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->psp_vdata) {
137 /* Using common routine to manage all interrupts */
138 if (!sp->psp_irq_handler) {
139 /* Nothing else using it, so free it */
140 free_irq(sp->ccp_irq, sp);
142 sp->irq_registered = false;
145 sp->ccp_irq_handler = NULL;
146 sp->ccp_irq_data = NULL;
148 /* Each sub-device can manage it's own interrupt */
149 free_irq(sp->ccp_irq, data);
153 void sp_free_psp_irq(struct sp_device *sp, void *data)
155 if ((sp->psp_irq == sp->ccp_irq) && sp->dev_vdata->ccp_vdata) {
156 /* Using common routine to manage all interrupts */
157 if (!sp->ccp_irq_handler) {
158 /* Nothing else using it, so free it */
159 free_irq(sp->psp_irq, sp);
161 sp->irq_registered = false;
164 sp->psp_irq_handler = NULL;
165 sp->psp_irq_data = NULL;
167 /* Each sub-device can manage it's own interrupt */
168 free_irq(sp->psp_irq, data);
173 * sp_alloc_struct - allocate and initialize the sp_device struct
175 * @dev: device struct of the SP
177 struct sp_device *sp_alloc_struct(struct device *dev)
179 struct sp_device *sp;
181 sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL);
186 sp->ord = atomic_inc_return(&sp_ordinal);
187 snprintf(sp->name, SP_MAX_NAME_LEN, "sp-%u", sp->ord);
192 int sp_init(struct sp_device *sp)
196 if (sp->dev_vdata->ccp_vdata)
199 if (sp->dev_vdata->psp_vdata)
204 void sp_destroy(struct sp_device *sp)
206 if (sp->dev_vdata->ccp_vdata)
209 if (sp->dev_vdata->psp_vdata)
215 int sp_suspend(struct sp_device *sp)
217 if (sp->dev_vdata->ccp_vdata) {
224 int sp_resume(struct sp_device *sp)
226 if (sp->dev_vdata->ccp_vdata) {
233 struct sp_device *sp_get_psp_master_device(void)
235 struct sp_device *i, *ret = NULL;
238 write_lock_irqsave(&sp_unit_lock, flags);
239 if (list_empty(&sp_units))
242 list_for_each_entry(i, &sp_units, entry) {
243 if (i->psp_data && i->get_psp_master_device) {
244 ret = i->get_psp_master_device();
250 write_unlock_irqrestore(&sp_unit_lock, flags);
254 static int __init sp_mod_init(void)
257 static bool initialized;
267 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
279 ret = sp_platform_init();
289 #if IS_BUILTIN(CONFIG_KVM_AMD) && IS_ENABLED(CONFIG_KVM_AMD_SEV)
290 int __init sev_module_init(void)
292 return sp_mod_init();
296 static void __exit sp_mod_exit(void)
300 #ifdef CONFIG_CRYPTO_DEV_SP_PSP
312 module_init(sp_mod_init);
313 module_exit(sp_mod_exit);