Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 1 | /* |
| 2 | * AMD Cryptographic Coprocessor (CCP) driver |
| 3 | * |
| 4 | * Copyright (C) 2014 Advanced Micro Devices, Inc. |
| 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 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 35 | static int ccp_get_irq(struct ccp_device *ccp) |
| 36 | { |
| 37 | struct device *dev = ccp->dev; |
Geliang Tang | c6c59bf | 2015-12-23 20:49:01 +0800 | [diff] [blame^] | 38 | struct platform_device *pdev = to_platform_device(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 39 | int ret; |
| 40 | |
| 41 | ret = platform_get_irq(pdev, 0); |
| 42 | if (ret < 0) |
| 43 | return ret; |
| 44 | |
| 45 | ccp->irq = ret; |
| 46 | ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev); |
| 47 | if (ret) { |
| 48 | dev_notice(dev, "unable to allocate IRQ (%d)\n", ret); |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int ccp_get_irqs(struct ccp_device *ccp) |
| 56 | { |
| 57 | struct device *dev = ccp->dev; |
| 58 | int ret; |
| 59 | |
| 60 | ret = ccp_get_irq(ccp); |
| 61 | if (!ret) |
| 62 | return 0; |
| 63 | |
| 64 | /* Couldn't get an interrupt */ |
| 65 | dev_notice(dev, "could not enable interrupts (%d)\n", ret); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | static void ccp_free_irqs(struct ccp_device *ccp) |
| 71 | { |
| 72 | struct device *dev = ccp->dev; |
| 73 | |
| 74 | free_irq(ccp->irq, dev); |
| 75 | } |
| 76 | |
| 77 | static struct resource *ccp_find_mmio_area(struct ccp_device *ccp) |
| 78 | { |
| 79 | struct device *dev = ccp->dev; |
Geliang Tang | c6c59bf | 2015-12-23 20:49:01 +0800 | [diff] [blame^] | 80 | struct platform_device *pdev = to_platform_device(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 81 | struct resource *ior; |
| 82 | |
| 83 | ior = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 84 | if (ior && (resource_size(ior) >= 0x800)) |
| 85 | return ior; |
| 86 | |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | static int ccp_platform_probe(struct platform_device *pdev) |
| 91 | { |
| 92 | struct ccp_device *ccp; |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 93 | struct ccp_platform *ccp_platform; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 94 | struct device *dev = &pdev->dev; |
Suthikulpanit, Suravee | 1831eff | 2015-10-28 15:50:50 -0700 | [diff] [blame] | 95 | enum dev_dma_attr attr; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 96 | struct resource *ior; |
| 97 | int ret; |
| 98 | |
| 99 | ret = -ENOMEM; |
| 100 | ccp = ccp_alloc_struct(dev); |
| 101 | if (!ccp) |
| 102 | goto e_err; |
| 103 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 104 | ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL); |
| 105 | if (!ccp_platform) |
| 106 | goto e_err; |
| 107 | |
| 108 | ccp->dev_specific = ccp_platform; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 109 | ccp->get_irq = ccp_get_irqs; |
| 110 | ccp->free_irq = ccp_free_irqs; |
| 111 | |
| 112 | ior = ccp_find_mmio_area(ccp); |
| 113 | ccp->io_map = devm_ioremap_resource(dev, ior); |
| 114 | if (IS_ERR(ccp->io_map)) { |
| 115 | ret = PTR_ERR(ccp->io_map); |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 116 | goto e_err; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 117 | } |
| 118 | ccp->io_regs = ccp->io_map; |
| 119 | |
Suthikulpanit, Suravee | 1831eff | 2015-10-28 15:50:50 -0700 | [diff] [blame] | 120 | attr = device_get_dma_attr(dev); |
| 121 | if (attr == DEV_DMA_NOT_SUPPORTED) { |
| 122 | dev_err(dev, "DMA is not supported"); |
| 123 | goto e_err; |
| 124 | } |
| 125 | |
| 126 | ccp_platform->coherent = (attr == DEV_DMA_COHERENT); |
| 127 | if (ccp_platform->coherent) |
| 128 | ccp->axcache = CACHE_WB_NO_ALLOC; |
| 129 | else |
| 130 | ccp->axcache = CACHE_NONE; |
| 131 | |
Tom Lendacky | 261bf07 | 2015-02-03 13:07:17 -0600 | [diff] [blame] | 132 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); |
| 133 | if (ret) { |
| 134 | dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 135 | goto e_err; |
Tom Lendacky | 261bf07 | 2015-02-03 13:07:17 -0600 | [diff] [blame] | 136 | } |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 137 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 138 | dev_set_drvdata(dev, ccp); |
| 139 | |
| 140 | ret = ccp_init(ccp); |
| 141 | if (ret) |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 142 | goto e_err; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 143 | |
| 144 | dev_notice(dev, "enabled\n"); |
| 145 | |
| 146 | return 0; |
| 147 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 148 | e_err: |
| 149 | dev_notice(dev, "initialization failed\n"); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static int ccp_platform_remove(struct platform_device *pdev) |
| 154 | { |
| 155 | struct device *dev = &pdev->dev; |
| 156 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 157 | |
| 158 | ccp_destroy(ccp); |
| 159 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 160 | dev_notice(dev, "disabled\n"); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | #ifdef CONFIG_PM |
| 166 | static int ccp_platform_suspend(struct platform_device *pdev, |
| 167 | pm_message_t state) |
| 168 | { |
| 169 | struct device *dev = &pdev->dev; |
| 170 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 171 | unsigned long flags; |
| 172 | unsigned int i; |
| 173 | |
| 174 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 175 | |
| 176 | ccp->suspending = 1; |
| 177 | |
| 178 | /* Wake all the queue kthreads to prepare for suspend */ |
| 179 | for (i = 0; i < ccp->cmd_q_count; i++) |
| 180 | wake_up_process(ccp->cmd_q[i].kthread); |
| 181 | |
| 182 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 183 | |
| 184 | /* Wait for all queue kthreads to say they're done */ |
| 185 | while (!ccp_queues_suspended(ccp)) |
| 186 | wait_event_interruptible(ccp->suspend_queue, |
| 187 | ccp_queues_suspended(ccp)); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int ccp_platform_resume(struct platform_device *pdev) |
| 193 | { |
| 194 | struct device *dev = &pdev->dev; |
| 195 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 196 | unsigned long flags; |
| 197 | unsigned int i; |
| 198 | |
| 199 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 200 | |
| 201 | ccp->suspending = 0; |
| 202 | |
| 203 | /* Wake up all the kthreads */ |
| 204 | for (i = 0; i < ccp->cmd_q_count; i++) { |
| 205 | ccp->cmd_q[i].suspended = 0; |
| 206 | wake_up_process(ccp->cmd_q[i].kthread); |
| 207 | } |
| 208 | |
| 209 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | #endif |
| 214 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 215 | #ifdef CONFIG_ACPI |
| 216 | static const struct acpi_device_id ccp_acpi_match[] = { |
| 217 | { "AMDI0C00", 0 }, |
| 218 | { }, |
| 219 | }; |
Tom Lendacky | 6170511 | 2015-06-30 12:57:14 -0500 | [diff] [blame] | 220 | MODULE_DEVICE_TABLE(acpi, ccp_acpi_match); |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 221 | #endif |
| 222 | |
| 223 | #ifdef CONFIG_OF |
| 224 | static const struct of_device_id ccp_of_match[] = { |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 225 | { .compatible = "amd,ccp-seattle-v1a" }, |
| 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 | } |