blob: 63cc74ee6d2a0f9ddbb617f2c45a568ac02d300b [file] [log] [blame]
Brijesh Singh720419f2017-07-06 09:59:14 -05001/*
2 * AMD Secure Processor driver
3 *
4 * Copyright (C) 2017 Advanced Micro Devices, Inc.
5 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 * Author: Gary R Hook <gary.hook@amd.com>
8 * Author: Brijesh Singh <brijesh.singh@amd.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/kthread.h>
18#include <linux/sched.h>
19#include <linux/interrupt.h>
20#include <linux/spinlock.h>
21#include <linux/spinlock_types.h>
22#include <linux/types.h>
23#include <linux/ccp.h>
24
25#include "ccp-dev.h"
26#include "sp-dev.h"
27
28MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
29MODULE_AUTHOR("Gary R Hook <gary.hook@amd.com>");
30MODULE_LICENSE("GPL");
31MODULE_VERSION("1.1.0");
32MODULE_DESCRIPTION("AMD Secure Processor driver");
33
34/* List of SPs, SP count, read-write access lock, and access functions
35 *
36 * Lock structure: get sp_unit_lock for reading whenever we need to
37 * examine the SP list.
38 */
39static DEFINE_RWLOCK(sp_unit_lock);
40static LIST_HEAD(sp_units);
41
42/* Ever-increasing value to produce unique unit numbers */
43static atomic_t sp_ordinal;
44
45static void sp_add_device(struct sp_device *sp)
46{
47 unsigned long flags;
48
49 write_lock_irqsave(&sp_unit_lock, flags);
50
51 list_add_tail(&sp->entry, &sp_units);
52
53 write_unlock_irqrestore(&sp_unit_lock, flags);
54}
55
56static void sp_del_device(struct sp_device *sp)
57{
58 unsigned long flags;
59
60 write_lock_irqsave(&sp_unit_lock, flags);
61
62 list_del(&sp->entry);
63
64 write_unlock_irqrestore(&sp_unit_lock, flags);
65}
66
67/**
68 * sp_alloc_struct - allocate and initialize the sp_device struct
69 *
70 * @dev: device struct of the SP
71 */
72struct sp_device *sp_alloc_struct(struct device *dev)
73{
74 struct sp_device *sp;
75
76 sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL);
77 if (!sp)
78 return NULL;
79
80 sp->dev = dev;
81 sp->ord = atomic_inc_return(&sp_ordinal);
82 snprintf(sp->name, SP_MAX_NAME_LEN, "sp-%u", sp->ord);
83
84 return sp;
85}
86
87int sp_init(struct sp_device *sp)
88{
89 sp_add_device(sp);
90
91 if (sp->dev_vdata->ccp_vdata)
92 ccp_dev_init(sp);
93
94 return 0;
95}
96
97void sp_destroy(struct sp_device *sp)
98{
99 if (sp->dev_vdata->ccp_vdata)
100 ccp_dev_destroy(sp);
101
102 sp_del_device(sp);
103}
104
105#ifdef CONFIG_PM
106int sp_suspend(struct sp_device *sp, pm_message_t state)
107{
108 int ret;
109
110 if (sp->dev_vdata->ccp_vdata) {
111 ret = ccp_dev_suspend(sp, state);
112 if (ret)
113 return ret;
114 }
115
116 return 0;
117}
118
119int sp_resume(struct sp_device *sp)
120{
121 int ret;
122
123 if (sp->dev_vdata->ccp_vdata) {
124 ret = ccp_dev_resume(sp);
125 if (ret)
126 return ret;
127 }
128
129 return 0;
130}
131#endif
132
133static int __init sp_mod_init(void)
134{
135#ifdef CONFIG_X86
136 int ret;
137
138 ret = ccp_pci_init();
139 if (ret)
140 return ret;
141
142 /* Don't leave the driver loaded if init failed */
143 if (ccp_present() != 0) {
144 ccp_pci_exit();
145 return -ENODEV;
146 }
147
148 return 0;
149#endif
150
151#ifdef CONFIG_ARM64
152 int ret;
153
154 ret = ccp_platform_init();
155 if (ret)
156 return ret;
157
158 /* Don't leave the driver loaded if init failed */
159 if (ccp_present() != 0) {
160 ccp_platform_exit();
161 return -ENODEV;
162 }
163
164 return 0;
165#endif
166
167 return -ENODEV;
168}
169
170static void __exit sp_mod_exit(void)
171{
172#ifdef CONFIG_X86
173 ccp_pci_exit();
174#endif
175
176#ifdef CONFIG_ARM64
177 ccp_platform_exit();
178#endif
179}
180
181module_init(sp_mod_init);
182module_exit(sp_mod_exit);