aboutsummaryrefslogtreecommitdiff
path: root/drivers/acpi/pfr_telemetry.c
blob: 9abf350bd7a5a28cc63d8960c395b02a0482b240 (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// SPDX-License-Identifier: GPL-2.0
/*
 * ACPI Platform Firmware Runtime Telemetry driver
 *
 * Copyright (C) 2021 Intel Corporation
 * Author: Chen Yu <yu.c.chen@intel.com>
 *
 * This driver allows user space to fetch telemetry data from the
 * firmware with the help of the Platform Firmware Runtime Telemetry
 * interface.
 */
#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/uio.h>
#include <linux/uuid.h>

#include <uapi/linux/pfrut.h>

#define PFRT_LOG_EXEC_IDX	0
#define PFRT_LOG_HISTORY_IDX	1

#define PFRT_LOG_ERR		0
#define PFRT_LOG_WARN	1
#define PFRT_LOG_INFO	2
#define PFRT_LOG_VERB	4

#define PFRT_FUNC_SET_LEV		1
#define PFRT_FUNC_GET_LEV		2
#define PFRT_FUNC_GET_DATA		3

#define PFRT_REVID_1		1
#define PFRT_REVID_2		2
#define PFRT_DEFAULT_REV_ID	PFRT_REVID_1

enum log_index {
	LOG_STATUS_IDX = 0,
	LOG_EXT_STATUS_IDX = 1,
	LOG_MAX_SZ_IDX = 2,
	LOG_CHUNK1_LO_IDX = 3,
	LOG_CHUNK1_HI_IDX = 4,
	LOG_CHUNK1_SZ_IDX = 5,
	LOG_CHUNK2_LO_IDX = 6,
	LOG_CHUNK2_HI_IDX = 7,
	LOG_CHUNK2_SZ_IDX = 8,
	LOG_ROLLOVER_CNT_IDX = 9,
	LOG_RESET_CNT_IDX = 10,
	LOG_NR_IDX
};

struct pfrt_log_device {
	int index;
	struct pfrt_log_info info;
	struct device *parent_dev;
	struct miscdevice miscdev;
};

/* pfrt_guid is the parameter for _DSM method */
static const guid_t pfrt_log_guid =
	GUID_INIT(0x75191659, 0x8178, 0x4D9D, 0xB8, 0x8F, 0xAC, 0x5E,
		  0x5E, 0x93, 0xE8, 0xBF);

static DEFINE_IDA(pfrt_log_ida);

static inline struct pfrt_log_device *to_pfrt_log_dev(struct file *file)
{
	return container_of(file->private_data, struct pfrt_log_device, miscdev);
}

static int get_pfrt_log_data_info(struct pfrt_log_data_info *data_info,
				  struct pfrt_log_device *pfrt_log_dev)
{
	acpi_handle handle = ACPI_HANDLE(pfrt_log_dev->parent_dev);
	union acpi_object *out_obj, in_obj, in_buf;
	int ret = -EBUSY;

	memset(data_info, 0, sizeof(*data_info));
	memset(&in_obj, 0, sizeof(in_obj));
	memset(&in_buf, 0, sizeof(in_buf));
	in_obj.type = ACPI_TYPE_PACKAGE;
	in_obj.package.count = 1;
	in_obj.package.elements = &in_buf;
	in_buf.type = ACPI_TYPE_INTEGER;
	in_buf.integer.value = pfrt_log_dev->info.log_type;

	out_obj = acpi_evaluate_dsm_typed(handle, &pfrt_log_guid,
					  pfrt_log_dev->info.log_revid, PFRT_FUNC_GET_DATA,
					  &in_obj, ACPI_TYPE_PACKAGE);
	if (!out_obj)
		return -EINVAL;

	if (out_obj->package.count < LOG_NR_IDX ||
	    out_obj->package.elements[LOG_STATUS_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_EXT_STATUS_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_MAX_SZ_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_CHUNK1_LO_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_CHUNK1_HI_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_CHUNK1_SZ_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_CHUNK2_LO_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_CHUNK2_HI_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_CHUNK2_SZ_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_ROLLOVER_CNT_IDX].type != ACPI_TYPE_INTEGER ||
	    out_obj->package.elements[LOG_RESET_CNT_IDX].type != ACPI_TYPE_INTEGER)
		goto free_acpi_buffer;

	data_info->status = out_obj->package.elements[LOG_STATUS_IDX].integer.value;
	data_info->ext_status =
		out_obj->package.elements[LOG_EXT_STATUS_IDX].integer.value;
	if (data_info->status != DSM_SUCCEED) {
		dev_dbg(pfrt_log_dev->parent_dev, "Error Status:%d\n", data_info->status);
		dev_dbg(pfrt_log_dev->parent_dev, "Error Extend Status:%d\n",
			data_info->ext_status);
		goto free_acpi_buffer;
	}

	data_info->max_data_size =
		out_obj->package.elements[LOG_MAX_SZ_IDX].integer.value;
	data_info->chunk1_addr_lo =
		out_obj->package.elements[LOG_CHUNK1_LO_IDX].integer.value;
	data_info->chunk1_addr_hi =
		out_obj->package.elements[LOG_CHUNK1_HI_IDX].integer.value;
	data_info->chunk1_size =
		out_obj->package.elements[LOG_CHUNK1_SZ_IDX].integer.value;
	data_info->chunk2_addr_lo =
		out_obj->package.elements[LOG_CHUNK2_LO_IDX].integer.value;
	data_info->chunk2_addr_hi =
		out_obj->package.elements[LOG_CHUNK2_HI_IDX].integer.value;
	data_info->chunk2_size =
		out_obj->package.elements[LOG_CHUNK2_SZ_IDX].integer.value;
	data_info->rollover_cnt =
		out_obj->package.elements[LOG_ROLLOVER_CNT_IDX].integer.value;
	data_info->reset_cnt =
		out_obj->package.elements[LOG_RESET_CNT_IDX].integer.value;

	ret = 0;

free_acpi_buffer:
	kfree(out_obj);

	return ret;
}

static int set_pfrt_log_level(int level, struct pfrt_log_device *pfrt_log_dev)
{
	acpi_handle handle = ACPI_HANDLE(pfrt_log_dev->parent_dev);
	union acpi_object *out_obj, *obj, in_obj, in_buf;
	enum pfru_dsm_status status, ext_status;
	int ret = 0;

	memset(&in_obj, 0, sizeof(in_obj));
	memset(&in_buf, 0, sizeof(in_buf));
	in_obj.type = ACPI_TYPE_PACKAGE;
	in_obj.package.count = 1;
	in_obj.package.elements = &in_buf;
	in_buf.type = ACPI_TYPE_INTEGER;
	in_buf.integer.value = level;

	out_obj = acpi_evaluate_dsm_typed(handle, &pfrt_log_guid,
					  pfrt_log_dev->info.log_revid, PFRT_FUNC_SET_LEV,
					  &in_obj, ACPI_TYPE_PACKAGE);
	if (!out_obj)
		return -EINVAL;

	obj = &out_obj->package.elements[0];
	status = obj->integer.value;
	if (status != DSM_SUCCEED) {
		obj = &out_obj->package.elements[1];
		ext_status = obj->integer.value;
		dev_dbg(pfrt_log_dev->parent_dev, "Error Status:%d\n", status);
		dev_dbg(pfrt_log_dev->parent_dev, "Error Extend Status:%d\n", ext_status);
		ret = -EBUSY;
	}

	kfree(out_obj);

	return ret;
}

static int get_pfrt_log_level(struct pfrt_log_device *pfrt_log_dev)
{
	acpi_handle handle = ACPI_HANDLE(pfrt_log_dev->parent_dev);
	union acpi_object *out_obj, *obj;
	enum pfru_dsm_status status, ext_status;
	int ret = -EBUSY;

	out_obj = acpi_evaluate_dsm_typed(handle, &pfrt_log_guid,
					  pfrt_log_dev->info.log_revid, PFRT_FUNC_GET_LEV,
					  NULL, ACPI_TYPE_PACKAGE);
	if (!out_obj)
		return -EINVAL;

	obj = &out_obj->package.elements[0];
	if (obj->type != ACPI_TYPE_INTEGER)
		goto free_acpi_buffer;

	status = obj->integer.value;
	if (status != DSM_SUCCEED) {
		obj = &out_obj->package.elements[1];
		ext_status = obj->integer.value;
		dev_dbg(pfrt_log_dev->parent_dev, "Error Status:%d\n", status);
		dev_dbg(pfrt_log_dev->parent_dev, "Error Extend Status:%d\n", ext_status);
		goto free_acpi_buffer;
	}

	obj = &out_obj->package.elements[2];
	if (obj->type != ACPI_TYPE_INTEGER)
		goto free_acpi_buffer;

	ret = obj->integer.value;

free_acpi_buffer:
	kfree(out_obj);

	return ret;
}

static int valid_log_level(u32 level)
{
	return level == PFRT_LOG_ERR || level == PFRT_LOG_WARN ||
	       level == PFRT_LOG_INFO || level == PFRT_LOG_VERB;
}

static int valid_log_type(u32 type)
{
	return type == PFRT_LOG_EXEC_IDX || type == PFRT_LOG_HISTORY_IDX;
}

static inline int valid_log_revid(u32 id)
{
	return id == PFRT_REVID_1 || id == PFRT_REVID_2;
}

static long pfrt_log_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct pfrt_log_device *pfrt_log_dev = to_pfrt_log_dev(file);
	struct pfrt_log_data_info data_info;
	struct pfrt_log_info info;
	void __user *p;
	int ret = 0;

	p = (void __user *)arg;

	switch (cmd) {
	case PFRT_LOG_IOC_SET_INFO:
		if (copy_from_user(&info, p, sizeof(info)))
			return -EFAULT;

		if (valid_log_revid(info.log_revid))
			pfrt_log_dev->info.log_revid = info.log_revid;

		if (valid_log_level(info.log_level)) {
			ret = set_pfrt_log_level(info.log_level, pfrt_log_dev);
			if (ret < 0)
				return ret;

			pfrt_log_dev->info.log_level = info.log_level;
		}

		if (valid_log_type(info.log_type))
			pfrt_log_dev->info.log_type = info.log_type;

		return 0;

	case PFRT_LOG_IOC_GET_INFO:
		info.log_level = get_pfrt_log_level(pfrt_log_dev);
		if (ret < 0)
			return ret;

		info.log_type = pfrt_log_dev->info.log_type;
		info.log_revid = pfrt_log_dev->info.log_revid;
		if (copy_to_user(p, &info, sizeof(info)))
			return -EFAULT;

		return 0;

	case PFRT_LOG_IOC_GET_DATA_INFO:
		ret = get_pfrt_log_data_info(&data_info, pfrt_log_dev);
		if (ret)
			return ret;

		if (copy_to_user(p, &data_info, sizeof(struct pfrt_log_data_info)))
			return -EFAULT;

		return 0;

	default:
		return -ENOTTY;
	}
}

static int
pfrt_log_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct pfrt_log_device *pfrt_log_dev;
	struct pfrt_log_data_info info;
	unsigned long psize, vsize;
	phys_addr_t base_addr;
	int ret;

	if (vma->vm_flags & VM_WRITE)
		return -EROFS;

	/* changing from read to write with mprotect is not allowed */
	vma->vm_flags &= ~VM_MAYWRITE;

	pfrt_log_dev = to_pfrt_log_dev(file);

	ret = get_pfrt_log_data_info(&info, pfrt_log_dev);
	if (ret)
		return ret;

	base_addr = (phys_addr_t)((info.chunk2_addr_hi << 32) | info.chunk2_addr_lo);
	/* pfrt update has not been launched yet */
	if (!base_addr)
		return -ENODEV;

	psize = info.max_data_size;
	/* base address and total buffer size must be page aligned */
	if (!PAGE_ALIGNED(base_addr) || !PAGE_ALIGNED(psize))
		return -ENODEV;

	vsize = vma->vm_end - vma->vm_start;
	if (vsize > psize)
		return -EINVAL;

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	if (io_remap_pfn_range(vma, vma->vm_start, PFN_DOWN(base_addr),
			       vsize, vma->vm_page_prot))
		return -EAGAIN;

	return 0;
}

static const struct file_operations acpi_pfrt_log_fops = {
	.owner		= THIS_MODULE,
	.mmap		= pfrt_log_mmap,
	.unlocked_ioctl = pfrt_log_ioctl,
	.llseek		= noop_llseek,
};

static int acpi_pfrt_log_remove(struct platform_device *pdev)
{
	struct pfrt_log_device *pfrt_log_dev = platform_get_drvdata(pdev);

	misc_deregister(&pfrt_log_dev->miscdev);

	return 0;
}

static void pfrt_log_put_idx(void *data)
{
	struct pfrt_log_device *pfrt_log_dev = data;

	ida_free(&pfrt_log_ida, pfrt_log_dev->index);
}

static int acpi_pfrt_log_probe(struct platform_device *pdev)
{
	acpi_handle handle = ACPI_HANDLE(&pdev->dev);
	struct pfrt_log_device *pfrt_log_dev;
	int ret;

	if (!acpi_has_method(handle, "_DSM")) {
		dev_dbg(&pdev->dev, "Missing _DSM\n");
		return -ENODEV;
	}

	pfrt_log_dev = devm_kzalloc(&pdev->dev, sizeof(*pfrt_log_dev), GFP_KERNEL);
	if (!pfrt_log_dev)
		return -ENOMEM;

	ret = ida_alloc(&pfrt_log_ida, GFP_KERNEL);
	if (ret < 0)
		return ret;

	pfrt_log_dev->index = ret;
	ret = devm_add_action_or_reset(&pdev->dev, pfrt_log_put_idx, pfrt_log_dev);
	if (ret)
		return ret;

	pfrt_log_dev->info.log_revid = PFRT_DEFAULT_REV_ID;
	pfrt_log_dev->parent_dev = &pdev->dev;

	pfrt_log_dev->miscdev.minor = MISC_DYNAMIC_MINOR;
	pfrt_log_dev->miscdev.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
						    "pfrt%d",
						    pfrt_log_dev->index);
	if (!pfrt_log_dev->miscdev.name)
		return -ENOMEM;

	pfrt_log_dev->miscdev.nodename = devm_kasprintf(&pdev->dev, GFP_KERNEL,
							"acpi_pfr_telemetry%d",
							pfrt_log_dev->index);
	if (!pfrt_log_dev->miscdev.nodename)
		return -ENOMEM;

	pfrt_log_dev->miscdev.fops = &acpi_pfrt_log_fops;
	pfrt_log_dev->miscdev.parent = &pdev->dev;

	ret = misc_register(&pfrt_log_dev->miscdev);
	if (ret)
		return ret;

	platform_set_drvdata(pdev, pfrt_log_dev);

	return 0;
}

static const struct acpi_device_id acpi_pfrt_log_ids[] = {
	{"INTC1081"},
	{}
};
MODULE_DEVICE_TABLE(acpi, acpi_pfrt_log_ids);

static struct platform_driver acpi_pfrt_log_driver = {
	.driver = {
		.name = "pfr_telemetry",
		.acpi_match_table = acpi_pfrt_log_ids,
	},
	.probe = acpi_pfrt_log_probe,
	.remove = acpi_pfrt_log_remove,
};
module_platform_driver(acpi_pfrt_log_driver);

MODULE_DESCRIPTION("Platform Firmware Runtime Update Telemetry driver");
MODULE_LICENSE("GPL v2");