Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 1 | /* |
| 2 | * AMD Cryptographic Coprocessor (CCP) driver |
| 3 | * |
Gary R Hook | 553d237 | 2016-03-01 13:49:04 -0600 | [diff] [blame] | 4 | * Copyright (C) 2014,2016 Advanced Micro Devices, Inc. |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 5 | * |
| 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 Lendacky | 126ae9a | 2014-07-10 10:58:35 -0500 | [diff] [blame] | 25 | #include <linux/of.h> |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 26 | #include <linux/of_address.h> |
| 27 | #include <linux/acpi.h> |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 28 | |
| 29 | #include "ccp-dev.h" |
| 30 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 31 | struct ccp_platform { |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 32 | int coherent; |
| 33 | }; |
| 34 | |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 35 | static const struct acpi_device_id ccp_acpi_match[]; |
| 36 | static const struct of_device_id ccp_of_match[]; |
| 37 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 38 | static struct sp_dev_vdata *ccp_get_of_version(struct platform_device *pdev) |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 39 | { |
| 40 | #ifdef CONFIG_OF |
| 41 | const struct of_device_id *match; |
| 42 | |
| 43 | match = of_match_node(ccp_of_match, pdev->dev.of_node); |
| 44 | if (match && match->data) |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 45 | return (struct sp_dev_vdata *)match->data; |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 46 | #endif |
pjambhlekar | 9d1fb19 | 2017-05-03 09:32:09 +0530 | [diff] [blame] | 47 | return NULL; |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 48 | } |
| 49 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 50 | static struct sp_dev_vdata *ccp_get_acpi_version(struct platform_device *pdev) |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 51 | { |
| 52 | #ifdef CONFIG_ACPI |
| 53 | const struct acpi_device_id *match; |
| 54 | |
| 55 | match = acpi_match_device(ccp_acpi_match, &pdev->dev); |
| 56 | if (match && match->driver_data) |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 57 | return (struct sp_dev_vdata *)match->driver_data; |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 58 | #endif |
pjambhlekar | 9d1fb19 | 2017-05-03 09:32:09 +0530 | [diff] [blame] | 59 | return NULL; |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 60 | } |
| 61 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 62 | static int ccp_get_irq(struct ccp_device *ccp) |
| 63 | { |
| 64 | struct device *dev = ccp->dev; |
Geliang Tang | c6c59bf | 2015-12-23 20:49:01 +0800 | [diff] [blame] | 65 | struct platform_device *pdev = to_platform_device(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 66 | int ret; |
| 67 | |
| 68 | ret = platform_get_irq(pdev, 0); |
Gustavo A. R. Silva | 28a2cc6 | 2017-06-30 00:59:52 -0500 | [diff] [blame] | 69 | if (ret < 0) { |
| 70 | dev_notice(dev, "unable to get IRQ (%d)\n", ret); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 71 | return ret; |
Gustavo A. R. Silva | 28a2cc6 | 2017-06-30 00:59:52 -0500 | [diff] [blame] | 72 | } |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 73 | |
| 74 | ccp->irq = ret; |
Gary R Hook | ea0375a | 2016-03-01 13:49:25 -0600 | [diff] [blame] | 75 | ret = request_irq(ccp->irq, ccp->vdata->perform->irqhandler, 0, |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 76 | ccp->name, ccp); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 77 | if (ret) { |
| 78 | dev_notice(dev, "unable to allocate IRQ (%d)\n", ret); |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int ccp_get_irqs(struct ccp_device *ccp) |
| 86 | { |
| 87 | struct device *dev = ccp->dev; |
| 88 | int ret; |
| 89 | |
| 90 | ret = ccp_get_irq(ccp); |
| 91 | if (!ret) |
| 92 | return 0; |
| 93 | |
| 94 | /* Couldn't get an interrupt */ |
| 95 | dev_notice(dev, "could not enable interrupts (%d)\n", ret); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static void ccp_free_irqs(struct ccp_device *ccp) |
| 101 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 102 | free_irq(ccp->irq, ccp); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 105 | static int ccp_platform_probe(struct platform_device *pdev) |
| 106 | { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 107 | struct sp_device *sp; |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 108 | struct ccp_platform *ccp_platform; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 109 | struct device *dev = &pdev->dev; |
Suthikulpanit, Suravee | 1831eff | 2015-10-28 15:50:50 -0700 | [diff] [blame] | 110 | enum dev_dma_attr attr; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 111 | struct resource *ior; |
| 112 | int ret; |
| 113 | |
| 114 | ret = -ENOMEM; |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 115 | sp = sp_alloc_struct(dev); |
| 116 | if (!sp) |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 117 | goto e_err; |
| 118 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 119 | ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL); |
| 120 | if (!ccp_platform) |
| 121 | goto e_err; |
| 122 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 123 | sp->dev_specific = ccp_platform; |
| 124 | sp->dev_vdata = pdev->dev.of_node ? ccp_get_of_version(pdev) |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 125 | : ccp_get_acpi_version(pdev); |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 126 | if (!sp->dev_vdata) { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 127 | ret = -ENODEV; |
| 128 | dev_err(dev, "missing driver data\n"); |
| 129 | goto e_err; |
| 130 | } |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 131 | sp->get_irq = ccp_get_irqs; |
| 132 | sp->free_irq = ccp_free_irqs; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 133 | |
Brijesh Singh | 970e830 | 2017-07-06 09:59:13 -0500 | [diff] [blame] | 134 | ior = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 135 | sp->io_map = devm_ioremap_resource(dev, ior); |
| 136 | if (IS_ERR(sp->io_map)) { |
| 137 | ret = PTR_ERR(sp->io_map); |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 138 | goto e_err; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 139 | } |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 140 | |
Suthikulpanit, Suravee | 1831eff | 2015-10-28 15:50:50 -0700 | [diff] [blame] | 141 | attr = device_get_dma_attr(dev); |
| 142 | if (attr == DEV_DMA_NOT_SUPPORTED) { |
| 143 | dev_err(dev, "DMA is not supported"); |
| 144 | goto e_err; |
| 145 | } |
| 146 | |
| 147 | ccp_platform->coherent = (attr == DEV_DMA_COHERENT); |
| 148 | if (ccp_platform->coherent) |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 149 | sp->axcache = CACHE_WB_NO_ALLOC; |
Suthikulpanit, Suravee | 1831eff | 2015-10-28 15:50:50 -0700 | [diff] [blame] | 150 | else |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 151 | sp->axcache = CACHE_NONE; |
Suthikulpanit, Suravee | 1831eff | 2015-10-28 15:50:50 -0700 | [diff] [blame] | 152 | |
Tom Lendacky | 261bf07 | 2015-02-03 13:07:17 -0600 | [diff] [blame] | 153 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); |
| 154 | if (ret) { |
| 155 | dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 156 | goto e_err; |
Tom Lendacky | 261bf07 | 2015-02-03 13:07:17 -0600 | [diff] [blame] | 157 | } |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 158 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 159 | dev_set_drvdata(dev, sp); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 160 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 161 | ret = sp_init(sp); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 162 | if (ret) |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 163 | goto e_err; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 164 | |
| 165 | dev_notice(dev, "enabled\n"); |
| 166 | |
| 167 | return 0; |
| 168 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 169 | e_err: |
| 170 | dev_notice(dev, "initialization failed\n"); |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static int ccp_platform_remove(struct platform_device *pdev) |
| 175 | { |
| 176 | struct device *dev = &pdev->dev; |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 177 | struct sp_device *sp = dev_get_drvdata(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 178 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 179 | sp_destroy(sp); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 180 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 181 | dev_notice(dev, "disabled\n"); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | #ifdef CONFIG_PM |
| 187 | static int ccp_platform_suspend(struct platform_device *pdev, |
| 188 | pm_message_t state) |
| 189 | { |
| 190 | struct device *dev = &pdev->dev; |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 191 | struct sp_device *sp = dev_get_drvdata(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 192 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 193 | return sp_suspend(sp, state); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static int ccp_platform_resume(struct platform_device *pdev) |
| 197 | { |
| 198 | struct device *dev = &pdev->dev; |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 199 | struct sp_device *sp = dev_get_drvdata(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 200 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 201 | return sp_resume(sp); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 202 | } |
| 203 | #endif |
| 204 | |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 205 | static const struct sp_dev_vdata dev_vdata[] = { |
| 206 | { |
| 207 | .bar = 0, |
| 208 | #ifdef CONFIG_CRYPTO_DEV_SP_CCP |
| 209 | .ccp_vdata = &ccpv3_platform, |
| 210 | #endif |
| 211 | }, |
| 212 | }; |
| 213 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 214 | #ifdef CONFIG_ACPI |
| 215 | static const struct acpi_device_id ccp_acpi_match[] = { |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 216 | { "AMDI0C00", (kernel_ulong_t)&dev_vdata[0] }, |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 217 | { }, |
| 218 | }; |
Tom Lendacky | 6170511 | 2015-06-30 12:57:14 -0500 | [diff] [blame] | 219 | MODULE_DEVICE_TABLE(acpi, ccp_acpi_match); |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 220 | #endif |
| 221 | |
| 222 | #ifdef CONFIG_OF |
| 223 | static const struct of_device_id ccp_of_match[] = { |
Gary R Hook | c7019c4 | 2016-03-01 13:49:15 -0600 | [diff] [blame] | 224 | { .compatible = "amd,ccp-seattle-v1a", |
Brijesh Singh | 720419f | 2017-07-06 09:59:14 -0500 | [diff] [blame^] | 225 | .data = (const void *)&dev_vdata[0] }, |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 226 | { }, |
| 227 | }; |
Tom Lendacky | 6170511 | 2015-06-30 12:57:14 -0500 | [diff] [blame] | 228 | MODULE_DEVICE_TABLE(of, ccp_of_match); |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 229 | #endif |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 230 | |
| 231 | static struct platform_driver ccp_platform_driver = { |
| 232 | .driver = { |
Tom Lendacky | 166db19 | 2015-10-01 16:32:50 -0500 | [diff] [blame] | 233 | .name = "ccp", |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 234 | #ifdef CONFIG_ACPI |
| 235 | .acpi_match_table = ccp_acpi_match, |
| 236 | #endif |
| 237 | #ifdef CONFIG_OF |
| 238 | .of_match_table = ccp_of_match, |
| 239 | #endif |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 240 | }, |
| 241 | .probe = ccp_platform_probe, |
| 242 | .remove = ccp_platform_remove, |
| 243 | #ifdef CONFIG_PM |
| 244 | .suspend = ccp_platform_suspend, |
| 245 | .resume = ccp_platform_resume, |
| 246 | #endif |
| 247 | }; |
| 248 | |
| 249 | int ccp_platform_init(void) |
| 250 | { |
| 251 | return platform_driver_register(&ccp_platform_driver); |
| 252 | } |
| 253 | |
| 254 | void ccp_platform_exit(void) |
| 255 | { |
| 256 | platform_driver_unregister(&ccp_platform_driver); |
| 257 | } |