blob: 71734f254fd154ceefffd006d891e5a75c817087 [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 *
Gary R Hook553d2372016-03-01 13:49:04 -06004 * Copyright (C) 2014,2016 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
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050036static const struct acpi_device_id sp_acpi_match[];
37static const struct of_device_id sp_of_match[];
Gary R Hookc7019c42016-03-01 13:49:15 -060038
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050039static struct sp_dev_vdata *sp_get_of_version(struct platform_device *pdev)
Gary R Hookc7019c42016-03-01 13:49:15 -060040{
41#ifdef CONFIG_OF
42 const struct of_device_id *match;
43
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050044 match = of_match_node(sp_of_match, pdev->dev.of_node);
Gary R Hookc7019c42016-03-01 13:49:15 -060045 if (match && match->data)
Brijesh Singh720419f2017-07-06 09:59:14 -050046 return (struct sp_dev_vdata *)match->data;
Gary R Hookc7019c42016-03-01 13:49:15 -060047#endif
pjambhlekar9d1fb192017-05-03 09:32:09 +053048 return NULL;
Gary R Hookc7019c42016-03-01 13:49:15 -060049}
50
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050051static struct sp_dev_vdata *sp_get_acpi_version(struct platform_device *pdev)
Gary R Hookc7019c42016-03-01 13:49:15 -060052{
53#ifdef CONFIG_ACPI
54 const struct acpi_device_id *match;
55
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050056 match = acpi_match_device(sp_acpi_match, &pdev->dev);
Gary R Hookc7019c42016-03-01 13:49:15 -060057 if (match && match->driver_data)
Brijesh Singh720419f2017-07-06 09:59:14 -050058 return (struct sp_dev_vdata *)match->driver_data;
Gary R Hookc7019c42016-03-01 13:49:15 -060059#endif
pjambhlekar9d1fb192017-05-03 09:32:09 +053060 return NULL;
Gary R Hookc7019c42016-03-01 13:49:15 -060061}
62
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050063static int sp_get_irqs(struct sp_device *sp)
Tom Lendackyc4f4b322014-06-05 10:17:57 -050064{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050065 struct sp_platform *sp_platform = sp->dev_specific;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050066 struct device *dev = sp->dev;
Geliang Tangc6c59bf2015-12-23 20:49:01 +080067 struct platform_device *pdev = to_platform_device(dev);
Brijesh Singhf4d18d62017-07-06 09:59:15 -050068 unsigned int i, count;
Tom Lendackyc4f4b322014-06-05 10:17:57 -050069 int ret;
70
Brijesh Singhf4d18d62017-07-06 09:59:15 -050071 for (i = 0, count = 0; i < pdev->num_resources; i++) {
72 struct resource *res = &pdev->resource[i];
73
74 if (resource_type(res) == IORESOURCE_IRQ)
75 count++;
76 }
77
Brijesh Singhd0ebbc02017-07-06 09:59:16 -050078 sp_platform->irq_count = count;
Brijesh Singhf4d18d62017-07-06 09:59:15 -050079
Tom Lendackyc4f4b322014-06-05 10:17:57 -050080 ret = platform_get_irq(pdev, 0);
Gustavo A. R. Silva28a2cc62017-06-30 00:59:52 -050081 if (ret < 0) {
82 dev_notice(dev, "unable to get IRQ (%d)\n", ret);
Tom Lendackyc4f4b322014-06-05 10:17:57 -050083 return ret;
Gustavo A. R. Silva28a2cc62017-06-30 00:59:52 -050084 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -050085
Brijesh Singhf4d18d62017-07-06 09:59:15 -050086 sp->psp_irq = ret;
87 if (count == 1) {
88 sp->ccp_irq = ret;
89 } else {
90 ret = platform_get_irq(pdev, 1);
91 if (ret < 0) {
92 dev_notice(dev, "unable to get IRQ (%d)\n", ret);
93 return ret;
94 }
95
96 sp->ccp_irq = ret;
Tom Lendackyc4f4b322014-06-05 10:17:57 -050097 }
98
99 return 0;
100}
101
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500102static int sp_platform_probe(struct platform_device *pdev)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500103{
Brijesh Singh720419f2017-07-06 09:59:14 -0500104 struct sp_device *sp;
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500105 struct sp_platform *sp_platform;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500106 struct device *dev = &pdev->dev;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700107 enum dev_dma_attr attr;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500108 struct resource *ior;
109 int ret;
110
111 ret = -ENOMEM;
Brijesh Singh720419f2017-07-06 09:59:14 -0500112 sp = sp_alloc_struct(dev);
113 if (!sp)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500114 goto e_err;
115
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500116 sp_platform = devm_kzalloc(dev, sizeof(*sp_platform), GFP_KERNEL);
117 if (!sp_platform)
Tom Lendacky6c506342015-02-03 13:07:29 -0600118 goto e_err;
119
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500120 sp->dev_specific = sp_platform;
121 sp->dev_vdata = pdev->dev.of_node ? sp_get_of_version(pdev)
122 : sp_get_acpi_version(pdev);
Brijesh Singh720419f2017-07-06 09:59:14 -0500123 if (!sp->dev_vdata) {
Gary R Hookc7019c42016-03-01 13:49:15 -0600124 ret = -ENODEV;
125 dev_err(dev, "missing driver data\n");
126 goto e_err;
127 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500128
Brijesh Singh970e8302017-07-06 09:59:13 -0500129 ior = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Brijesh Singh720419f2017-07-06 09:59:14 -0500130 sp->io_map = devm_ioremap_resource(dev, ior);
131 if (IS_ERR(sp->io_map)) {
132 ret = PTR_ERR(sp->io_map);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600133 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500134 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500135
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700136 attr = device_get_dma_attr(dev);
137 if (attr == DEV_DMA_NOT_SUPPORTED) {
138 dev_err(dev, "DMA is not supported");
139 goto e_err;
140 }
141
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500142 sp_platform->coherent = (attr == DEV_DMA_COHERENT);
143 if (sp_platform->coherent)
Brijesh Singh720419f2017-07-06 09:59:14 -0500144 sp->axcache = CACHE_WB_NO_ALLOC;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700145 else
Brijesh Singh720419f2017-07-06 09:59:14 -0500146 sp->axcache = CACHE_NONE;
Suthikulpanit, Suravee1831eff2015-10-28 15:50:50 -0700147
Tom Lendacky261bf072015-02-03 13:07:17 -0600148 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
149 if (ret) {
150 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600151 goto e_err;
Tom Lendacky261bf072015-02-03 13:07:17 -0600152 }
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500153
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500154 ret = sp_get_irqs(sp);
Brijesh Singhf4d18d62017-07-06 09:59:15 -0500155 if (ret)
156 goto e_err;
157
Brijesh Singh720419f2017-07-06 09:59:14 -0500158 dev_set_drvdata(dev, sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500159
Brijesh Singh720419f2017-07-06 09:59:14 -0500160 ret = sp_init(sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500161 if (ret)
Tom Lendackybe03a3a2015-02-03 13:07:23 -0600162 goto e_err;
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500163
164 dev_notice(dev, "enabled\n");
165
166 return 0;
167
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500168e_err:
169 dev_notice(dev, "initialization failed\n");
170 return ret;
171}
172
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500173static int sp_platform_remove(struct platform_device *pdev)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500174{
175 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500176 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500177
Brijesh Singh720419f2017-07-06 09:59:14 -0500178 sp_destroy(sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500179
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500180 dev_notice(dev, "disabled\n");
181
182 return 0;
183}
184
185#ifdef CONFIG_PM
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500186static int sp_platform_suspend(struct platform_device *pdev,
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500187 pm_message_t state)
188{
189 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500190 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500191
Brijesh Singh720419f2017-07-06 09:59:14 -0500192 return sp_suspend(sp, state);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500193}
194
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500195static int sp_platform_resume(struct platform_device *pdev)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500196{
197 struct device *dev = &pdev->dev;
Brijesh Singh720419f2017-07-06 09:59:14 -0500198 struct sp_device *sp = dev_get_drvdata(dev);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500199
Brijesh Singh720419f2017-07-06 09:59:14 -0500200 return sp_resume(sp);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500201}
202#endif
203
Brijesh Singh720419f2017-07-06 09:59:14 -0500204static const struct sp_dev_vdata dev_vdata[] = {
205 {
206 .bar = 0,
207#ifdef CONFIG_CRYPTO_DEV_SP_CCP
208 .ccp_vdata = &ccpv3_platform,
209#endif
210 },
211};
212
Tom Lendacky6c506342015-02-03 13:07:29 -0600213#ifdef CONFIG_ACPI
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500214static const struct acpi_device_id sp_acpi_match[] = {
Brijesh Singh720419f2017-07-06 09:59:14 -0500215 { "AMDI0C00", (kernel_ulong_t)&dev_vdata[0] },
Tom Lendacky6c506342015-02-03 13:07:29 -0600216 { },
217};
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500218MODULE_DEVICE_TABLE(acpi, sp_acpi_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600219#endif
220
221#ifdef CONFIG_OF
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500222static const struct of_device_id sp_of_match[] = {
Gary R Hookc7019c42016-03-01 13:49:15 -0600223 { .compatible = "amd,ccp-seattle-v1a",
Brijesh Singh720419f2017-07-06 09:59:14 -0500224 .data = (const void *)&dev_vdata[0] },
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500225 { },
226};
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500227MODULE_DEVICE_TABLE(of, sp_of_match);
Tom Lendacky6c506342015-02-03 13:07:29 -0600228#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500229
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500230static struct platform_driver sp_platform_driver = {
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500231 .driver = {
Tom Lendacky166db192015-10-01 16:32:50 -0500232 .name = "ccp",
Tom Lendacky6c506342015-02-03 13:07:29 -0600233#ifdef CONFIG_ACPI
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500234 .acpi_match_table = sp_acpi_match,
Tom Lendacky6c506342015-02-03 13:07:29 -0600235#endif
236#ifdef CONFIG_OF
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500237 .of_match_table = sp_of_match,
Tom Lendacky6c506342015-02-03 13:07:29 -0600238#endif
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500239 },
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500240 .probe = sp_platform_probe,
241 .remove = sp_platform_remove,
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500242#ifdef CONFIG_PM
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500243 .suspend = sp_platform_suspend,
244 .resume = sp_platform_resume,
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500245#endif
246};
247
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500248int sp_platform_init(void)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500249{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500250 return platform_driver_register(&sp_platform_driver);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500251}
252
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500253void sp_platform_exit(void)
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500254{
Brijesh Singhd0ebbc02017-07-06 09:59:16 -0500255 platform_driver_unregister(&sp_platform_driver);
Tom Lendackyc4f4b322014-06-05 10:17:57 -0500256}