blob: d24228efbaaa50edab95cce9eeb2a420e2ed35fa [file] [log] [blame]
Tom Lendackyc4f4b322014-06-05 10:17:57 -05001/*
Brijesh Singhd0ebbc02017-07-06 09:59:16 -05002 * AMD Secure Processor device driver
Tom Lendackyc4f4b322014-06-05 10:17:57 -05003 *
Hook, Garyfa5cd1c2018-12-18 15:48:29 +00004 * Copyright (C) 2014,2018 Advanced Micro Devices, Inc.
Tom Lendackyc4f4b322014-06-05 10:17:57 -05005 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/ioport.h>
18#include <linux/dma-mapping.h>
19#include <linux/kthread.h>
20#include <linux/sched.h>
21#include <linux/interrupt.h>
22#include <linux/spinlock.h>
23#include <linux/delay.h>
24#include <linux/ccp.h>
Tom Lendacky126ae9a2014-07-10 10:58:35 -050025#include <linux/of.h>
Tom Lendacky6c506342015-02-03 13:07:29 -060026#include <linux/of_address.h>
27#include <linux/acpi.h>
Tom Lendackyc4f4b322014-06-05 10:17:57 -050028
29#include "ccp-dev.h"
30
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050031struct sp_platform {
Tom Lendacky6c506342015-02-03 13:07:29 -060032 int coherent;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050033 unsigned int irq_count;
Tom Lendacky6c506342015-02-03 13:07:29 -060034};
35
Nathan Chancellor3512dcb2018-09-24 10:26:15 -070036static const struct sp_dev_vdata dev_vdata[] = {
37 {
38 .bar = 0,
39#ifdef CONFIG_CRYPTO_DEV_SP_CCP
40 .ccp_vdata = &ccpv3_platform,
41#endif
42 },
43};
44
45#ifdef CONFIG_ACPI
46static const struct acpi_device_id sp_acpi_match[] = {
47 { "AMDI0C00", (kernel_ulong_t)&dev_vdata[0] },
48 { },
49};
50MODULE_DEVICE_TABLE(acpi, sp_acpi_match);
51#endif
52
53#ifdef CONFIG_OF
54static const struct of_device_id sp_of_match[] = {
55 { .compatible = "amd,ccp-seattle-v1a",
56 .data = (const void *)&dev_vdata[0] },
57 { },
58};
59MODULE_DEVICE_TABLE(of, sp_of_match);
60#endif
Gary R Hookc7019c42016-03-01 13:49:15 -060061
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050062static struct sp_dev_vdata *sp_get_of_version(struct platform_device *pdev)
Gary R Hookc7019c42016-03-01 13:49:15 -060063{
64#ifdef CONFIG_OF
65 const struct of_device_id *match;
66
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050067 match = of_match_node(sp_of_match, pdev->dev.of_node);
Gary R Hookc7019c42016-03-01 13:49:15 -060068 if (match && match->data)
Brijesh Singh720419f2017-07-06 09:59:14 -050069 return (struct sp_dev_vdata *)match->data;
Gary R Hookc7019c42016-03-01 13:49:15 -060070#endif
pjambhlekar9d1fb192017-05-03 09:32:09 +053071 return NULL;
Gary R Hookc7019c42016-03-01 13:49:15 -060072}
73
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050074static struct sp_dev_vdata *sp_get_acpi_version(struct platform_device *pdev)
Gary R Hookc7019c42016-03-01 13:49:15 -060075{
76#ifdef CONFIG_ACPI
77 const struct acpi_device_id *match;
78
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050079 match = acpi_match_device(sp_acpi_match, &pdev->dev);
Gary R Hookc7019c42016-03-01 13:49:15 -060080 if (match && match->driver_data)
Brijesh Singh720419f2017-07-06 09:59:14 -050081 return (struct sp_dev_vdata *)match->driver_data;
Gary R Hookc7019c42016-03-01 13:49:15 -060082#endif
pjambhlekar9d1fb192017-05-03 09:32:09 +053083 return NULL;
Gary R Hookc7019c42016-03-01 13:49:15 -060084}
85
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050086static int sp_get_irqs(struct sp_device *sp)
Tom Lendackyc4f4b322014-06-05 10:17:57 -050087{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050088 struct sp_platform *sp_platform = sp->dev_specific;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050089 struct device *dev = sp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080090 struct platform_device *pdev = to_platform_device(dev);
Brijesh Singhf4d18d62017-07-06 09:59:15 -050091 unsigned int i, count;
Tom Lendackyc4f4b322014-06-05 10:17:57 -050092 int ret;
93
Brijesh Singhf4d18d62017-07-06 09:59:15 -050094 for (i = 0, count = 0; i < pdev->num_resources; i++) {
95 struct resource *res = &pdev->resource[i];
96
97 if (resource_type(res) == IORESOURCE_IRQ)
98 count++;
99 }
100
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500101 sp_platform->irq_count = count;
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500102
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500103 ret = platform_get_irq(pdev, 0);
Gustavo A. R. Silva28a2cc62017-06-30 00:59:52 -0500104 if (ret < 0) {
105 dev_notice(dev, "unable to get IRQ (%d)\n", ret);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500106 return ret;
Gustavo A. R. Silva28a2cc62017-06-30 00:59:52 -0500107 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500108
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500109 sp->psp_irq = ret;
110 if (count == 1) {
111 sp->ccp_irq = ret;
112 } else {
113 ret = platform_get_irq(pdev, 1);
114 if (ret < 0) {
115 dev_notice(dev, "unable to get IRQ (%d)\n", ret);
116 return ret;
117 }
118
119 sp->ccp_irq = ret;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500120 }
121
122 return 0;
123}
124
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500125static int sp_platform_probe(struct platform_device *pdev)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500126{
Brijesh Singh720419f2017-07-06 09:59:14 -0500127 struct sp_device *sp;
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500128 struct sp_platform *sp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500129 struct device *dev = &pdev->dev;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700130 enum dev_dma_attr attr;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500131 struct resource *ior;
132 int ret;
133
134 ret = -ENOMEM;
Brijesh Singh720419f2017-07-06 09:59:14 -0500135 sp = sp_alloc_struct(dev);
136 if (!sp)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500137 goto e_err;
138
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500139 sp_platform = devm_kzalloc(dev, sizeof(*sp_platform), GFP_KERNEL);
140 if (!sp_platform)
Tom Lendacky6c506342015-02-03 13:07:29 -0600141 goto e_err;
142
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500143 sp->dev_specific = sp_platform;
144 sp->dev_vdata = pdev->dev.of_node ? sp_get_of_version(pdev)
145 : sp_get_acpi_version(pdev);
Brijesh Singh720419f2017-07-06 09:59:14 -0500146 if (!sp->dev_vdata) {
Gary R Hookc7019c42016-03-01 13:49:15 -0600147 ret = -ENODEV;
148 dev_err(dev, "missing driver data\n");
149 goto e_err;
150 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500151
Brijesh Singh970e8302017-07-06 09:59:13 -0500152 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Brijesh Singh720419f2017-07-06 09:59:14 -0500153 sp->io_map = devm_ioremap_resource(dev, ior);
154 if (IS_ERR(sp->io_map)) {
155 ret = PTR_ERR(sp->io_map);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600156 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500157 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500158
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700159 attr = device_get_dma_attr(dev);
160 if (attr == DEV_DMA_NOT_SUPPORTED) {
161 dev_err(dev, "DMA is not supported");
162 goto e_err;
163 }
164
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500165 sp_platform->coherent = (attr == DEV_DMA_COHERENT);
166 if (sp_platform->coherent)
Brijesh Singh720419f2017-07-06 09:59:14 -0500167 sp->axcache = CACHE_WB_NO_ALLOC;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700168 else
Brijesh Singh720419f2017-07-06 09:59:14 -0500169 sp->axcache = CACHE_NONE;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700170
Tom Lendacky261bf072015-02-03 13:07:17 -0600171 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
172 if (ret) {
173 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600174 goto e_err;
Tom Lendacky261bf072015-02-03 13:07:17 -0600175 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500176
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500177 ret = sp_get_irqs(sp);
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500178 if (ret)
179 goto e_err;
180
Brijesh Singh720419f2017-07-06 09:59:14 -0500181 dev_set_drvdata(dev, sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500182
Brijesh Singh720419f2017-07-06 09:59:14 -0500183 ret = sp_init(sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500184 if (ret)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600185 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500186
187 dev_notice(dev, "enabled\n");
188
189 return 0;
190
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500191e_err:
192 dev_notice(dev, "initialization failed\n");
193 return ret;
194}
195
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500196static int sp_platform_remove(struct platform_device *pdev)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500197{
198 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500199 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500200
Brijesh Singh720419f2017-07-06 09:59:14 -0500201 sp_destroy(sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500202
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500203 dev_notice(dev, "disabled\n");
204
205 return 0;
206}
207
208#ifdef CONFIG_PM
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500209static int sp_platform_suspend(struct platform_device *pdev,
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500210 pm_message_t state)
211{
212 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500213 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500214
Brijesh Singh720419f2017-07-06 09:59:14 -0500215 return sp_suspend(sp, state);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500216}
217
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500218static int sp_platform_resume(struct platform_device *pdev)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500219{
220 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500221 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500222
Brijesh Singh720419f2017-07-06 09:59:14 -0500223 return sp_resume(sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500224}
225#endif
226
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500227static struct platform_driver sp_platform_driver = {
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500228 .driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500229 .name = "ccp",
Tom Lendacky6c506342015-02-03 13:07:29 -0600230#ifdef CONFIG_ACPI
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500231 .acpi_match_table = sp_acpi_match,
Tom Lendacky6c506342015-02-03 13:07:29 -0600232#endif
233#ifdef CONFIG_OF
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500234 .of_match_table = sp_of_match,
Tom Lendacky6c506342015-02-03 13:07:29 -0600235#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500236 },
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500237 .probe = sp_platform_probe,
238 .remove = sp_platform_remove,
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500239#ifdef CONFIG_PM
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500240 .suspend = sp_platform_suspend,
241 .resume = sp_platform_resume,
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500242#endif
243};
244
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500245int sp_platform_init(void)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500246{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500247 return platform_driver_register(&sp_platform_driver);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500248}
249
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500250void sp_platform_exit(void)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500251{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500252 platform_driver_unregister(&sp_platform_driver);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500253}