2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2014,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
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.
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>
26 #include <linux/of_address.h>
27 #include <linux/acpi.h>
35 static const struct acpi_device_id ccp_acpi_match
[];
36 static const struct of_device_id ccp_of_match
[];
38 static struct ccp_vdata
*ccp_get_of_version(struct platform_device
*pdev
)
41 const struct of_device_id
*match
;
43 match
= of_match_node(ccp_of_match
, pdev
->dev
.of_node
);
44 if (match
&& match
->data
)
45 return (struct ccp_vdata
*)match
->data
;
50 static struct ccp_vdata
*ccp_get_acpi_version(struct platform_device
*pdev
)
53 const struct acpi_device_id
*match
;
55 match
= acpi_match_device(ccp_acpi_match
, &pdev
->dev
);
56 if (match
&& match
->driver_data
)
57 return (struct ccp_vdata
*)match
->driver_data
;
62 static int ccp_get_irq(struct ccp_device
*ccp
)
64 struct device
*dev
= ccp
->dev
;
65 struct platform_device
*pdev
= to_platform_device(dev
);
68 ret
= platform_get_irq(pdev
, 0);
73 ret
= request_irq(ccp
->irq
, ccp
->vdata
->perform
->irqhandler
, 0,
76 dev_notice(dev
, "unable to allocate IRQ (%d)\n", ret
);
83 static int ccp_get_irqs(struct ccp_device
*ccp
)
85 struct device
*dev
= ccp
->dev
;
88 ret
= ccp_get_irq(ccp
);
92 /* Couldn't get an interrupt */
93 dev_notice(dev
, "could not enable interrupts (%d)\n", ret
);
98 static void ccp_free_irqs(struct ccp_device
*ccp
)
100 struct device
*dev
= ccp
->dev
;
102 free_irq(ccp
->irq
, dev
);
105 static struct resource
*ccp_find_mmio_area(struct ccp_device
*ccp
)
107 struct device
*dev
= ccp
->dev
;
108 struct platform_device
*pdev
= to_platform_device(dev
);
109 struct resource
*ior
;
111 ior
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
112 if (ior
&& (resource_size(ior
) >= 0x800))
118 static int ccp_platform_probe(struct platform_device
*pdev
)
120 struct ccp_device
*ccp
;
121 struct ccp_platform
*ccp_platform
;
122 struct device
*dev
= &pdev
->dev
;
123 enum dev_dma_attr attr
;
124 struct resource
*ior
;
128 ccp
= ccp_alloc_struct(dev
);
132 ccp_platform
= devm_kzalloc(dev
, sizeof(*ccp_platform
), GFP_KERNEL
);
136 ccp
->dev_specific
= ccp_platform
;
137 ccp
->vdata
= pdev
->dev
.of_node
? ccp_get_of_version(pdev
)
138 : ccp_get_acpi_version(pdev
);
139 if (!ccp
->vdata
|| !ccp
->vdata
->version
) {
141 dev_err(dev
, "missing driver data\n");
144 ccp
->get_irq
= ccp_get_irqs
;
145 ccp
->free_irq
= ccp_free_irqs
;
147 ior
= ccp_find_mmio_area(ccp
);
148 ccp
->io_map
= devm_ioremap_resource(dev
, ior
);
149 if (IS_ERR(ccp
->io_map
)) {
150 ret
= PTR_ERR(ccp
->io_map
);
153 ccp
->io_regs
= ccp
->io_map
;
155 attr
= device_get_dma_attr(dev
);
156 if (attr
== DEV_DMA_NOT_SUPPORTED
) {
157 dev_err(dev
, "DMA is not supported");
161 ccp_platform
->coherent
= (attr
== DEV_DMA_COHERENT
);
162 if (ccp_platform
->coherent
)
163 ccp
->axcache
= CACHE_WB_NO_ALLOC
;
165 ccp
->axcache
= CACHE_NONE
;
167 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(48));
169 dev_err(dev
, "dma_set_mask_and_coherent failed (%d)\n", ret
);
173 dev_set_drvdata(dev
, ccp
);
175 ret
= ccp
->vdata
->perform
->init(ccp
);
179 dev_notice(dev
, "enabled\n");
184 dev_notice(dev
, "initialization failed\n");
188 static int ccp_platform_remove(struct platform_device
*pdev
)
190 struct device
*dev
= &pdev
->dev
;
191 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
193 ccp
->vdata
->perform
->destroy(ccp
);
195 dev_notice(dev
, "disabled\n");
201 static int ccp_platform_suspend(struct platform_device
*pdev
,
204 struct device
*dev
= &pdev
->dev
;
205 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
209 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
213 /* Wake all the queue kthreads to prepare for suspend */
214 for (i
= 0; i
< ccp
->cmd_q_count
; i
++)
215 wake_up_process(ccp
->cmd_q
[i
].kthread
);
217 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
219 /* Wait for all queue kthreads to say they're done */
220 while (!ccp_queues_suspended(ccp
))
221 wait_event_interruptible(ccp
->suspend_queue
,
222 ccp_queues_suspended(ccp
));
227 static int ccp_platform_resume(struct platform_device
*pdev
)
229 struct device
*dev
= &pdev
->dev
;
230 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
234 spin_lock_irqsave(&ccp
->cmd_lock
, flags
);
238 /* Wake up all the kthreads */
239 for (i
= 0; i
< ccp
->cmd_q_count
; i
++) {
240 ccp
->cmd_q
[i
].suspended
= 0;
241 wake_up_process(ccp
->cmd_q
[i
].kthread
);
244 spin_unlock_irqrestore(&ccp
->cmd_lock
, flags
);
251 static const struct acpi_device_id ccp_acpi_match
[] = {
252 { "AMDI0C00", (kernel_ulong_t
)&ccpv3
},
255 MODULE_DEVICE_TABLE(acpi
, ccp_acpi_match
);
259 static const struct of_device_id ccp_of_match
[] = {
260 { .compatible
= "amd,ccp-seattle-v1a",
261 .data
= (const void *)&ccpv3
},
264 MODULE_DEVICE_TABLE(of
, ccp_of_match
);
267 static struct platform_driver ccp_platform_driver
= {
271 .acpi_match_table
= ccp_acpi_match
,
274 .of_match_table
= ccp_of_match
,
277 .probe
= ccp_platform_probe
,
278 .remove
= ccp_platform_remove
,
280 .suspend
= ccp_platform_suspend
,
281 .resume
= ccp_platform_resume
,
285 int ccp_platform_init(void)
287 return platform_driver_register(&ccp_platform_driver
);
290 void ccp_platform_exit(void)
292 platform_driver_unregister(&ccp_platform_driver
);