-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
xilinx_puf.c
309 lines (261 loc) · 7.64 KB
/
xilinx_puf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-License-Identifier: GPL-2.0
/*
* Driver for Xilinx PUF device.
*
* Copyright (C) 2022 - 2023, Advanced Micro Devices, Inc.
*
* Description:
* This driver is developed for PUF registration and regeneration support.
*/
#include <linux/dma-mapping.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/of_reserved_mem.h>
#include <linux/platform_device.h>
#include <linux/firmware/xlnx-zynqmp.h>
#include <uapi/misc/xilinx_puf.h>
/**
* struct puf_params - parameters for PUF
* @pufoperation: PUF registration or regeneration operation
* @globalvarfilter: global variation filter
* @readoption: option to read PUF data from efuse cache or ram address
* @shuttervalue: shutter value for PUF registration/regeneration
* @readsyndromeaddr: address to store the syndrome data during registration
* @chashaddr: CHASH address
* @auxaddr: AUX address
* @pufidaddr: PUF ID address
* @writesyndromeaddr: address where syndrome data is present and it is passed to the user
* @trimsyndataaddr: trimmed syndrome data will be stored
*/
struct puf_params {
u8 pufoperation;
u8 globalvarfilter;
u8 readoption;
u32 shuttervalue;
u64 readsyndromeaddr;
u64 chashaddr;
u64 auxaddr;
u64 pufidaddr;
u64 writesyndromeaddr;
u64 trimsyndataaddr;
};
/**
* struct xpuf_dev - Driver data for PUF
* @dev: pointer to device struct
* @miscdev: misc device handle
*/
struct xpuf_dev {
struct device *dev;
struct miscdevice miscdev;
};
static int xlnx_puf_cfg(struct xpuf_dev *puf, struct puf_usrparams *pufreq)
{
struct pufdata *pufdat;
struct device *dev = puf->dev;
struct puf_params *pufin;
struct puf_helperdata *pufhd;
dma_addr_t dma_addr_in;
dma_addr_t dma_addr_data;
int ret;
pufin = dma_alloc_coherent(dev, sizeof(struct puf_params), &dma_addr_in, GFP_KERNEL);
if (!pufin)
return -ENOMEM;
pufin->pufoperation = pufreq->pufoperation;
pufin->globalvarfilter = pufreq->globalvarfilter;
pufin->shuttervalue = pufreq->shuttervalue;
if (pufin->pufoperation == PUF_REGIS) {
pufdat = dma_alloc_coherent(dev, sizeof(struct pufdata), &dma_addr_data,
GFP_KERNEL);
if (!pufdat)
goto cleanup_pufin;
pufin->readsyndromeaddr = (u64)(dma_addr_data);
pufin->chashaddr = (u64)(pufin->readsyndromeaddr + sizeof(pufdat->pufhd.syndata));
pufin->auxaddr = (u64)(pufin->chashaddr + sizeof(pufdat->pufhd.chash));
pufin->pufidaddr = (u64)(pufin->auxaddr + sizeof(pufdat->pufhd.aux));
pufin->trimsyndataaddr = (u64)(pufin->pufidaddr + sizeof(pufdat->pufid));
ret = versal_pm_puf_registration(dma_addr_in);
if (ret != 0)
goto cleanup_pufdata;
if (copy_to_user((void *)pufreq->pufdataaddr, pufdat, sizeof(struct pufdata))) {
ret = -EFAULT;
goto cleanup_pufdata;
}
} else if ((pufin->pufoperation == PUF_REGEN) || (pufin->pufoperation == PUF_REGEN_ID)) {
pufin->readoption = pufreq->readoption;
pufhd = dma_alloc_coherent(dev, (sizeof(struct puf_helperdata) +
PUF_ID_LEN_IN_BYTES), &dma_addr_data,
GFP_KERNEL);
if (!pufhd)
goto cleanup_pufin;
if (copy_from_user(pufhd, (void *)pufreq->pufdataaddr,
sizeof(struct puf_helperdata))) {
ret = -EFAULT;
goto cleanup_pufdata;
}
pufin->writesyndromeaddr = (u64)(dma_addr_data);
pufin->chashaddr = (u64)(pufin->writesyndromeaddr + sizeof(pufhd->syndata));
pufin->auxaddr = (u64)(pufin->chashaddr + sizeof(pufhd->chash));
pufin->pufidaddr = (u64)(pufin->auxaddr + sizeof(pufhd->aux));
ret = versal_pm_puf_regeneration(dma_addr_in);
if (ret != 0)
goto cleanup_pufdata;
if (copy_to_user((void *)pufreq->pufidaddr, ((char *)pufhd +
sizeof(struct puf_helperdata)),
PUF_ID_LEN_IN_BYTES)) {
ret = -EFAULT;
goto cleanup_pufdata;
}
} else {
ret = -EINVAL;
goto cleanup_pufin;
}
cleanup_pufdata:
if (pufin->pufoperation == PUF_REGIS)
dma_free_coherent(dev, sizeof(struct pufdata), pufdat, dma_addr_data);
else if (pufin->pufoperation == PUF_REGEN)
dma_free_coherent(dev, (sizeof(struct puf_helperdata) + PUF_ID_LEN_IN_BYTES),
pufhd, dma_addr_data);
cleanup_pufin:
dma_free_coherent(dev, sizeof(struct puf_params), pufin, dma_addr_in);
return ret;
}
static long xlnx_puf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct puf_usrparams pufreq;
struct xpuf_dev *puf = file->private_data;
void __user *data = NULL;
int ret;
if (_IOC_TYPE(cmd) != PUF_IOC_MAGIC)
return -ENOTTY;
/* check if ioctl argument is present and valid */
if (_IOC_DIR(cmd) != _IOC_NONE) {
data = (void __user *)arg;
if (!data)
return -EINVAL;
}
switch (cmd) {
case PUF_REGISTRATION:
case PUF_REGENERATION:
case PUF_REGEN_ID_ONLY:
if (copy_from_user(&pufreq, data,
sizeof(struct puf_usrparams)))
return -EINVAL;
ret = xlnx_puf_cfg(puf, &pufreq);
break;
default:
ret = -EOPNOTSUPP;
break;
}
return ret;
}
/**
* xlnx_puf_open - open puf device
* @inode: inode object
* @file: file object
*
* Return: 0 if successful; otherwise -errno
*/
static int xlnx_puf_open(struct inode *inode, struct file *file)
{
struct xpuf_dev *xpuf;
xpuf = container_of(file->private_data, struct xpuf_dev, miscdev);
file->private_data = xpuf;
return 0;
}
/**
* xlnx_puf_release - release puf resources
* @inode: inode object
* @file: file object
*
* Return: 0 if successful; otherwise -errno
*/
static int xlnx_puf_release(struct inode *inode, struct file *file)
{
struct xpuf_dev *xpuf = file->private_data;
dev_dbg(xpuf->dev, "%s: device /dev/xpuf unregistered\n", __func__);
return 0;
}
static const struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = xlnx_puf_open,
.release = xlnx_puf_release,
.unlocked_ioctl = xlnx_puf_ioctl,
};
/**
* xlnx_puf_probe - probe puf device
* @pdev: Pointer to puf platform device structure
*
* Return: 0 if successful; otherwise -errno
*/
static int xlnx_puf_probe(struct platform_device *pdev)
{
int ret;
struct xpuf_dev *xpuf;
struct device *dev = &pdev->dev;
xpuf = devm_kzalloc(dev, sizeof(*xpuf), GFP_KERNEL);
if (!xpuf)
return -ENOMEM;
xpuf->dev = dev;
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
if (ret < 0) {
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
if (ret < 0) {
dev_err(dev, "no usable DMA configuration\n");
return ret;
}
}
xpuf->miscdev.minor = MISC_DYNAMIC_MINOR;
xpuf->miscdev.name = "xpuf";
xpuf->miscdev.fops = &dev_fops;
xpuf->miscdev.parent = dev;
if (misc_register(&xpuf->miscdev))
return -ENODEV;
platform_set_drvdata(pdev, xpuf);
dev_dbg(dev, "puf registered as /dev/xpuf successfully");
return 0;
}
/**
* xlnx_puf_remove - clean up structures
* @pdev: The structure containing the device's details
*
* Return: 0 on success.
*/
static int xlnx_puf_remove(struct platform_device *pdev)
{
struct xpuf_dev *xpuf = platform_get_drvdata(pdev);
platform_set_drvdata(pdev, NULL);
misc_deregister(&xpuf->miscdev);
dev_dbg(xpuf->dev, "device /dev/xpuf removed\n");
return 0;
}
static struct platform_driver xlnx_puf_drv = {
.probe = xlnx_puf_probe,
.remove = xlnx_puf_remove,
.driver = {
.name = "xlnx-puf",
},
};
static int __init xlnx_puf_driver_init(void)
{
struct platform_device *pdev;
int ret;
ret = platform_driver_register(&xlnx_puf_drv);
if (ret)
return ret;
pdev = platform_device_register_simple(xlnx_puf_drv.driver.name,
0, NULL, 0);
if (IS_ERR(pdev)) {
ret = PTR_ERR(pdev);
platform_driver_unregister(&xlnx_puf_drv);
}
return ret;
}
static void __exit xlnx_puf_driver_exit(void)
{
platform_driver_unregister(&xlnx_puf_drv);
}
module_init(xlnx_puf_driver_init);
module_exit(xlnx_puf_driver_exit);
MODULE_AUTHOR("Praveen Teja Kundanala <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Xilinx Versal PUF driver");