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