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 { |
| 32 | int use_acpi; |
| 33 | int coherent; |
| 34 | }; |
| 35 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 36 | static int ccp_get_irq(struct ccp_device *ccp) |
| 37 | { |
| 38 | struct device *dev = ccp->dev; |
| 39 | struct platform_device *pdev = container_of(dev, |
| 40 | struct platform_device, dev); |
| 41 | int ret; |
| 42 | |
| 43 | ret = platform_get_irq(pdev, 0); |
| 44 | if (ret < 0) |
| 45 | return ret; |
| 46 | |
| 47 | ccp->irq = ret; |
| 48 | ret = request_irq(ccp->irq, ccp_irq_handler, 0, "ccp", dev); |
| 49 | if (ret) { |
| 50 | dev_notice(dev, "unable to allocate IRQ (%d)\n", ret); |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int ccp_get_irqs(struct ccp_device *ccp) |
| 58 | { |
| 59 | struct device *dev = ccp->dev; |
| 60 | int ret; |
| 61 | |
| 62 | ret = ccp_get_irq(ccp); |
| 63 | if (!ret) |
| 64 | return 0; |
| 65 | |
| 66 | /* Couldn't get an interrupt */ |
| 67 | dev_notice(dev, "could not enable interrupts (%d)\n", ret); |
| 68 | |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static void ccp_free_irqs(struct ccp_device *ccp) |
| 73 | { |
| 74 | struct device *dev = ccp->dev; |
| 75 | |
| 76 | free_irq(ccp->irq, dev); |
| 77 | } |
| 78 | |
| 79 | static struct resource *ccp_find_mmio_area(struct ccp_device *ccp) |
| 80 | { |
| 81 | struct device *dev = ccp->dev; |
| 82 | struct platform_device *pdev = container_of(dev, |
| 83 | struct platform_device, dev); |
| 84 | struct resource *ior; |
| 85 | |
| 86 | ior = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 87 | if (ior && (resource_size(ior) >= 0x800)) |
| 88 | return ior; |
| 89 | |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | static int ccp_platform_probe(struct platform_device *pdev) |
| 94 | { |
| 95 | struct ccp_device *ccp; |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 96 | struct ccp_platform *ccp_platform; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 97 | struct device *dev = &pdev->dev; |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 98 | struct acpi_device *adev = ACPI_COMPANION(dev); |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 99 | struct resource *ior; |
| 100 | int ret; |
| 101 | |
| 102 | ret = -ENOMEM; |
| 103 | ccp = ccp_alloc_struct(dev); |
| 104 | if (!ccp) |
| 105 | goto e_err; |
| 106 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 107 | ccp_platform = devm_kzalloc(dev, sizeof(*ccp_platform), GFP_KERNEL); |
| 108 | if (!ccp_platform) |
| 109 | goto e_err; |
| 110 | |
| 111 | ccp->dev_specific = ccp_platform; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 112 | ccp->get_irq = ccp_get_irqs; |
| 113 | ccp->free_irq = ccp_free_irqs; |
| 114 | |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 115 | ccp_platform->use_acpi = (!adev || acpi_disabled) ? 0 : 1; |
| 116 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 117 | ior = ccp_find_mmio_area(ccp); |
| 118 | ccp->io_map = devm_ioremap_resource(dev, ior); |
| 119 | if (IS_ERR(ccp->io_map)) { |
| 120 | ret = PTR_ERR(ccp->io_map); |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 121 | goto e_err; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 122 | } |
| 123 | ccp->io_regs = ccp->io_map; |
| 124 | |
Tom Lendacky | 261bf07 | 2015-02-03 13:07:17 -0600 | [diff] [blame] | 125 | ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); |
| 126 | if (ret) { |
| 127 | dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 128 | goto e_err; |
Tom Lendacky | 261bf07 | 2015-02-03 13:07:17 -0600 | [diff] [blame] | 129 | } |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 130 | |
Suthikulpanit, Suravee | 04825cf | 2015-06-10 11:08:55 -0500 | [diff] [blame] | 131 | ccp_platform->coherent = device_dma_is_coherent(ccp->dev); |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 132 | if (ccp_platform->coherent) |
Tom Lendacky | 126ae9a | 2014-07-10 10:58:35 -0500 | [diff] [blame] | 133 | ccp->axcache = CACHE_WB_NO_ALLOC; |
| 134 | else |
| 135 | ccp->axcache = CACHE_NONE; |
| 136 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 137 | dev_set_drvdata(dev, ccp); |
| 138 | |
| 139 | ret = ccp_init(ccp); |
| 140 | if (ret) |
Tom Lendacky | be03a3a | 2015-02-03 13:07:23 -0600 | [diff] [blame] | 141 | goto e_err; |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 142 | |
| 143 | dev_notice(dev, "enabled\n"); |
| 144 | |
| 145 | return 0; |
| 146 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 147 | e_err: |
| 148 | dev_notice(dev, "initialization failed\n"); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | static int ccp_platform_remove(struct platform_device *pdev) |
| 153 | { |
| 154 | struct device *dev = &pdev->dev; |
| 155 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 156 | |
| 157 | ccp_destroy(ccp); |
| 158 | |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 159 | dev_notice(dev, "disabled\n"); |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | #ifdef CONFIG_PM |
| 165 | static int ccp_platform_suspend(struct platform_device *pdev, |
| 166 | pm_message_t state) |
| 167 | { |
| 168 | struct device *dev = &pdev->dev; |
| 169 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 170 | unsigned long flags; |
| 171 | unsigned int i; |
| 172 | |
| 173 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 174 | |
| 175 | ccp->suspending = 1; |
| 176 | |
| 177 | /* Wake all the queue kthreads to prepare for suspend */ |
| 178 | for (i = 0; i < ccp->cmd_q_count; i++) |
| 179 | wake_up_process(ccp->cmd_q[i].kthread); |
| 180 | |
| 181 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 182 | |
| 183 | /* Wait for all queue kthreads to say they're done */ |
| 184 | while (!ccp_queues_suspended(ccp)) |
| 185 | wait_event_interruptible(ccp->suspend_queue, |
| 186 | ccp_queues_suspended(ccp)); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int ccp_platform_resume(struct platform_device *pdev) |
| 192 | { |
| 193 | struct device *dev = &pdev->dev; |
| 194 | struct ccp_device *ccp = dev_get_drvdata(dev); |
| 195 | unsigned long flags; |
| 196 | unsigned int i; |
| 197 | |
| 198 | spin_lock_irqsave(&ccp->cmd_lock, flags); |
| 199 | |
| 200 | ccp->suspending = 0; |
| 201 | |
| 202 | /* Wake up all the kthreads */ |
| 203 | for (i = 0; i < ccp->cmd_q_count; i++) { |
| 204 | ccp->cmd_q[i].suspended = 0; |
| 205 | wake_up_process(ccp->cmd_q[i].kthread); |
| 206 | } |
| 207 | |
| 208 | spin_unlock_irqrestore(&ccp->cmd_lock, flags); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | #endif |
| 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[] = { |
| 216 | { "AMDI0C00", 0 }, |
| 217 | { }, |
| 218 | }; |
| 219 | #endif |
| 220 | |
| 221 | #ifdef CONFIG_OF |
| 222 | static const struct of_device_id ccp_of_match[] = { |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 223 | { .compatible = "amd,ccp-seattle-v1a" }, |
| 224 | { }, |
| 225 | }; |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 226 | #endif |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 227 | |
| 228 | static struct platform_driver ccp_platform_driver = { |
| 229 | .driver = { |
| 230 | .name = "AMD Cryptographic Coprocessor", |
Tom Lendacky | 6c50634 | 2015-02-03 13:07:29 -0600 | [diff] [blame] | 231 | #ifdef CONFIG_ACPI |
| 232 | .acpi_match_table = ccp_acpi_match, |
| 233 | #endif |
| 234 | #ifdef CONFIG_OF |
| 235 | .of_match_table = ccp_of_match, |
| 236 | #endif |
Tom Lendacky | c4f4b32 | 2014-06-05 10:17:57 -0500 | [diff] [blame] | 237 | }, |
| 238 | .probe = ccp_platform_probe, |
| 239 | .remove = ccp_platform_remove, |
| 240 | #ifdef CONFIG_PM |
| 241 | .suspend = ccp_platform_suspend, |
| 242 | .resume = ccp_platform_resume, |
| 243 | #endif |
| 244 | }; |
| 245 | |
| 246 | int ccp_platform_init(void) |
| 247 | { |
| 248 | return platform_driver_register(&ccp_platform_driver); |
| 249 | } |
| 250 | |
| 251 | void ccp_platform_exit(void) |
| 252 | { |
| 253 | platform_driver_unregister(&ccp_platform_driver); |
| 254 | } |