xref: /linux/drivers/hwtracing/coresight/ultrasoc-smb.c (revision f728c17fc97aea7a33151d9ba64106291c62bb02)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /*
3  * Siemens System Memory Buffer driver.
4  * Copyright(c) 2022, HiSilicon Limited.
5  */
6 
7 #include <linux/atomic.h>
8 #include <linux/acpi.h>
9 #include <linux/circ_buf.h>
10 #include <linux/err.h>
11 #include <linux/fs.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/platform_device.h>
15 
16 #include "coresight-etm-perf.h"
17 #include "coresight-priv.h"
18 #include "ultrasoc-smb.h"
19 
20 DEFINE_CORESIGHT_DEVLIST(sink_devs, "ultra_smb");
21 
22 #define ULTRASOC_SMB_DSM_UUID	"82ae1283-7f6a-4cbe-aa06-53e8fb24db18"
23 
24 static bool smb_buffer_not_empty(struct smb_drv_data *drvdata)
25 {
26 	u32 buf_status = readl(drvdata->base + SMB_LB_INT_STS_REG);
27 
28 	return FIELD_GET(SMB_LB_INT_STS_NOT_EMPTY_MSK, buf_status);
29 }
30 
31 static void smb_update_data_size(struct smb_drv_data *drvdata)
32 {
33 	struct smb_data_buffer *sdb = &drvdata->sdb;
34 	u32 buf_wrptr;
35 
36 	buf_wrptr = readl(drvdata->base + SMB_LB_WR_ADDR_REG) -
37 			  sdb->buf_hw_base;
38 
39 	/* Buffer is full */
40 	if (buf_wrptr == sdb->buf_rdptr && smb_buffer_not_empty(drvdata)) {
41 		sdb->data_size = sdb->buf_size;
42 		return;
43 	}
44 
45 	/* The buffer mode is circular buffer mode */
46 	sdb->data_size = CIRC_CNT(buf_wrptr, sdb->buf_rdptr,
47 				  sdb->buf_size);
48 }
49 
50 /*
51  * The read pointer adds @nbytes bytes (may round up to the beginning)
52  * after the data is read or discarded, while needing to update the
53  * available data size.
54  */
55 static void smb_update_read_ptr(struct smb_drv_data *drvdata, u32 nbytes)
56 {
57 	struct smb_data_buffer *sdb = &drvdata->sdb;
58 
59 	sdb->buf_rdptr += nbytes;
60 	sdb->buf_rdptr %= sdb->buf_size;
61 	writel(sdb->buf_hw_base + sdb->buf_rdptr,
62 	       drvdata->base + SMB_LB_RD_ADDR_REG);
63 
64 	sdb->data_size -= nbytes;
65 }
66 
67 static void smb_reset_buffer(struct smb_drv_data *drvdata)
68 {
69 	struct smb_data_buffer *sdb = &drvdata->sdb;
70 	u32 write_ptr;
71 
72 	/*
73 	 * We must flush and discard any data left in hardware path
74 	 * to avoid corrupting the next session.
75 	 * Note: The write pointer will never exceed the read pointer.
76 	 */
77 	writel(SMB_LB_PURGE_PURGED, drvdata->base + SMB_LB_PURGE_REG);
78 
79 	/* Reset SMB logical buffer status flags */
80 	writel(SMB_LB_INT_STS_RESET, drvdata->base + SMB_LB_INT_STS_REG);
81 
82 	write_ptr = readl(drvdata->base + SMB_LB_WR_ADDR_REG);
83 
84 	/* Do nothing, not data left in hardware path */
85 	if (!write_ptr || write_ptr == sdb->buf_rdptr + sdb->buf_hw_base)
86 		return;
87 
88 	/*
89 	 * The SMB_LB_WR_ADDR_REG register is read-only,
90 	 * Synchronize the read pointer to write pointer.
91 	 */
92 	writel(write_ptr, drvdata->base + SMB_LB_RD_ADDR_REG);
93 	sdb->buf_rdptr = write_ptr - sdb->buf_hw_base;
94 }
95 
96 static int smb_open(struct inode *inode, struct file *file)
97 {
98 	struct smb_drv_data *drvdata = container_of(file->private_data,
99 					struct smb_drv_data, miscdev);
100 
101 	guard(spinlock)(&drvdata->spinlock);
102 
103 	if (drvdata->reading)
104 		return -EBUSY;
105 
106 	if (atomic_read(&drvdata->csdev->refcnt))
107 		return -EBUSY;
108 
109 	smb_update_data_size(drvdata);
110 	drvdata->reading = true;
111 
112 	return 0;
113 }
114 
115 static ssize_t smb_read(struct file *file, char __user *data, size_t len,
116 			loff_t *ppos)
117 {
118 	struct smb_drv_data *drvdata = container_of(file->private_data,
119 					struct smb_drv_data, miscdev);
120 	struct smb_data_buffer *sdb = &drvdata->sdb;
121 	struct device *dev = &drvdata->csdev->dev;
122 	ssize_t to_copy = 0;
123 
124 	if (!len)
125 		return 0;
126 
127 	if (!sdb->data_size)
128 		return 0;
129 
130 	to_copy = min(sdb->data_size, len);
131 
132 	/* Copy parts of trace data when read pointer wrap around SMB buffer */
133 	if (sdb->buf_rdptr + to_copy > sdb->buf_size)
134 		to_copy = sdb->buf_size - sdb->buf_rdptr;
135 
136 	if (copy_to_user(data, sdb->buf_base + sdb->buf_rdptr, to_copy)) {
137 		dev_dbg(dev, "Failed to copy data to user\n");
138 		return -EFAULT;
139 	}
140 
141 	*ppos += to_copy;
142 	smb_update_read_ptr(drvdata, to_copy);
143 	if (!sdb->data_size)
144 		smb_reset_buffer(drvdata);
145 
146 	dev_dbg(dev, "%zu bytes copied\n", to_copy);
147 	return to_copy;
148 }
149 
150 static int smb_release(struct inode *inode, struct file *file)
151 {
152 	struct smb_drv_data *drvdata = container_of(file->private_data,
153 					struct smb_drv_data, miscdev);
154 
155 	guard(spinlock)(&drvdata->spinlock);
156 	drvdata->reading = false;
157 
158 	return 0;
159 }
160 
161 static const struct file_operations smb_fops = {
162 	.owner		= THIS_MODULE,
163 	.open		= smb_open,
164 	.read		= smb_read,
165 	.release	= smb_release,
166 	.llseek		= no_llseek,
167 };
168 
169 static ssize_t buf_size_show(struct device *dev, struct device_attribute *attr,
170 			     char *buf)
171 {
172 	struct smb_drv_data *drvdata = dev_get_drvdata(dev->parent);
173 
174 	return sysfs_emit(buf, "0x%lx\n", drvdata->sdb.buf_size);
175 }
176 static DEVICE_ATTR_RO(buf_size);
177 
178 static struct attribute *smb_sink_attrs[] = {
179 	coresight_simple_reg32(read_pos, SMB_LB_RD_ADDR_REG),
180 	coresight_simple_reg32(write_pos, SMB_LB_WR_ADDR_REG),
181 	coresight_simple_reg32(buf_status, SMB_LB_INT_STS_REG),
182 	&dev_attr_buf_size.attr,
183 	NULL
184 };
185 
186 static const struct attribute_group smb_sink_group = {
187 	.attrs = smb_sink_attrs,
188 	.name = "mgmt",
189 };
190 
191 static const struct attribute_group *smb_sink_groups[] = {
192 	&smb_sink_group,
193 	NULL
194 };
195 
196 static void smb_enable_hw(struct smb_drv_data *drvdata)
197 {
198 	writel(SMB_GLB_EN_HW_ENABLE, drvdata->base + SMB_GLB_EN_REG);
199 }
200 
201 static void smb_disable_hw(struct smb_drv_data *drvdata)
202 {
203 	writel(0x0, drvdata->base + SMB_GLB_EN_REG);
204 }
205 
206 static void smb_enable_sysfs(struct coresight_device *csdev)
207 {
208 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
209 
210 	if (drvdata->mode != CS_MODE_DISABLED)
211 		return;
212 
213 	smb_enable_hw(drvdata);
214 	drvdata->mode = CS_MODE_SYSFS;
215 }
216 
217 static int smb_enable_perf(struct coresight_device *csdev, void *data)
218 {
219 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
220 	struct perf_output_handle *handle = data;
221 	struct cs_buffers *buf = etm_perf_sink_config(handle);
222 	pid_t pid;
223 
224 	if (!buf)
225 		return -EINVAL;
226 
227 	/* Get a handle on the pid of the target process */
228 	pid = buf->pid;
229 
230 	/* Device is already in used by other session */
231 	if (drvdata->pid != -1 && drvdata->pid != pid)
232 		return -EBUSY;
233 
234 	if (drvdata->pid == -1) {
235 		smb_enable_hw(drvdata);
236 		drvdata->pid = pid;
237 		drvdata->mode = CS_MODE_PERF;
238 	}
239 
240 	return 0;
241 }
242 
243 static int smb_enable(struct coresight_device *csdev, enum cs_mode mode,
244 		      void *data)
245 {
246 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
247 	int ret = 0;
248 
249 	guard(spinlock)(&drvdata->spinlock);
250 
251 	/* Do nothing, the trace data is reading by other interface now */
252 	if (drvdata->reading)
253 		return -EBUSY;
254 
255 	/* Do nothing, the SMB is already enabled as other mode */
256 	if (drvdata->mode != CS_MODE_DISABLED && drvdata->mode != mode)
257 		return -EBUSY;
258 
259 	switch (mode) {
260 	case CS_MODE_SYSFS:
261 		smb_enable_sysfs(csdev);
262 		break;
263 	case CS_MODE_PERF:
264 		ret = smb_enable_perf(csdev, data);
265 		break;
266 	default:
267 		ret = -EINVAL;
268 	}
269 
270 	if (ret)
271 		return ret;
272 
273 	atomic_inc(&csdev->refcnt);
274 	dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n");
275 
276 	return ret;
277 }
278 
279 static int smb_disable(struct coresight_device *csdev)
280 {
281 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
282 
283 	guard(spinlock)(&drvdata->spinlock);
284 
285 	if (drvdata->reading)
286 		return -EBUSY;
287 
288 	if (atomic_dec_return(&csdev->refcnt))
289 		return -EBUSY;
290 
291 	/* Complain if we (somehow) got out of sync */
292 	WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
293 
294 	smb_disable_hw(drvdata);
295 
296 	/* Dissociate from the target process. */
297 	drvdata->pid = -1;
298 	drvdata->mode = CS_MODE_DISABLED;
299 	dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n");
300 
301 	return 0;
302 }
303 
304 static void *smb_alloc_buffer(struct coresight_device *csdev,
305 			      struct perf_event *event, void **pages,
306 			      int nr_pages, bool overwrite)
307 {
308 	struct cs_buffers *buf;
309 	int node;
310 
311 	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
312 	buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
313 	if (!buf)
314 		return NULL;
315 
316 	buf->snapshot = overwrite;
317 	buf->nr_pages = nr_pages;
318 	buf->data_pages = pages;
319 	buf->pid = task_pid_nr(event->owner);
320 
321 	return buf;
322 }
323 
324 static void smb_free_buffer(void *config)
325 {
326 	struct cs_buffers *buf = config;
327 
328 	kfree(buf);
329 }
330 
331 static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
332 				 struct cs_buffers *buf,
333 				 unsigned long head)
334 {
335 	struct smb_data_buffer *sdb = &drvdata->sdb;
336 	char **dst_pages = (char **)buf->data_pages;
337 	unsigned long to_copy;
338 	long pg_idx, pg_offset;
339 
340 	pg_idx = head >> PAGE_SHIFT;
341 	pg_offset = head & (PAGE_SIZE - 1);
342 
343 	while (sdb->data_size) {
344 		unsigned long pg_space = PAGE_SIZE - pg_offset;
345 
346 		to_copy = min(sdb->data_size, pg_space);
347 
348 		/* Copy parts of trace data when read pointer wrap around */
349 		if (sdb->buf_rdptr + to_copy > sdb->buf_size)
350 			to_copy = sdb->buf_size - sdb->buf_rdptr;
351 
352 		memcpy(dst_pages[pg_idx] + pg_offset,
353 			      sdb->buf_base + sdb->buf_rdptr, to_copy);
354 
355 		pg_offset += to_copy;
356 		if (pg_offset >= PAGE_SIZE) {
357 			pg_offset = 0;
358 			pg_idx++;
359 			pg_idx %= buf->nr_pages;
360 		}
361 		smb_update_read_ptr(drvdata, to_copy);
362 	}
363 
364 	smb_reset_buffer(drvdata);
365 }
366 
367 static unsigned long smb_update_buffer(struct coresight_device *csdev,
368 				       struct perf_output_handle *handle,
369 				       void *sink_config)
370 {
371 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
372 	struct smb_data_buffer *sdb = &drvdata->sdb;
373 	struct cs_buffers *buf = sink_config;
374 	unsigned long data_size;
375 	bool lost = false;
376 
377 	if (!buf)
378 		return 0;
379 
380 	guard(spinlock)(&drvdata->spinlock);
381 
382 	/* Don't do anything if another tracer is using this sink. */
383 	if (atomic_read(&csdev->refcnt) != 1)
384 		return 0;
385 
386 	smb_disable_hw(drvdata);
387 	smb_update_data_size(drvdata);
388 
389 	/*
390 	 * The SMB buffer may be bigger than the space available in the
391 	 * perf ring buffer (handle->size). If so advance the offset so
392 	 * that we get the latest trace data.
393 	 */
394 	if (sdb->data_size > handle->size) {
395 		smb_update_read_ptr(drvdata, sdb->data_size - handle->size);
396 		lost = true;
397 	}
398 
399 	data_size = sdb->data_size;
400 	smb_sync_perf_buffer(drvdata, buf, handle->head);
401 	if (!buf->snapshot && lost)
402 		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
403 
404 	return data_size;
405 }
406 
407 static const struct coresight_ops_sink smb_cs_ops = {
408 	.enable		= smb_enable,
409 	.disable	= smb_disable,
410 	.alloc_buffer	= smb_alloc_buffer,
411 	.free_buffer	= smb_free_buffer,
412 	.update_buffer	= smb_update_buffer,
413 };
414 
415 static const struct coresight_ops cs_ops = {
416 	.sink_ops	= &smb_cs_ops,
417 };
418 
419 static int smb_init_data_buffer(struct platform_device *pdev,
420 				struct smb_data_buffer *sdb)
421 {
422 	struct resource *res;
423 	void *base;
424 
425 	res = platform_get_resource(pdev, IORESOURCE_MEM, SMB_BUF_ADDR_RES);
426 	if (!res) {
427 		dev_err(&pdev->dev, "SMB device failed to get resource\n");
428 		return -EINVAL;
429 	}
430 
431 	sdb->buf_rdptr = 0;
432 	sdb->buf_hw_base = FIELD_GET(SMB_BUF_ADDR_LO_MSK, res->start);
433 	sdb->buf_size = resource_size(res);
434 	if (sdb->buf_size == 0)
435 		return -EINVAL;
436 
437 	/*
438 	 * This is a chunk of memory, use classic mapping with better
439 	 * performance.
440 	 */
441 	base = devm_memremap(&pdev->dev, sdb->buf_hw_base, sdb->buf_size,
442 				MEMREMAP_WB);
443 	if (IS_ERR(base))
444 		return PTR_ERR(base);
445 
446 	sdb->buf_base = base;
447 
448 	return 0;
449 }
450 
451 static void smb_init_hw(struct smb_drv_data *drvdata)
452 {
453 	smb_disable_hw(drvdata);
454 
455 	writel(SMB_LB_CFG_LO_DEFAULT, drvdata->base + SMB_LB_CFG_LO_REG);
456 	writel(SMB_LB_CFG_HI_DEFAULT, drvdata->base + SMB_LB_CFG_HI_REG);
457 	writel(SMB_GLB_CFG_DEFAULT, drvdata->base + SMB_GLB_CFG_REG);
458 	writel(SMB_GLB_INT_CFG, drvdata->base + SMB_GLB_INT_REG);
459 	writel(SMB_LB_INT_CTRL_CFG, drvdata->base + SMB_LB_INT_CTRL_REG);
460 }
461 
462 static int smb_register_sink(struct platform_device *pdev,
463 			     struct smb_drv_data *drvdata)
464 {
465 	struct coresight_platform_data *pdata = NULL;
466 	struct coresight_desc desc = { 0 };
467 	int ret;
468 
469 	pdata = coresight_get_platform_data(&pdev->dev);
470 	if (IS_ERR(pdata))
471 		return PTR_ERR(pdata);
472 
473 	desc.type = CORESIGHT_DEV_TYPE_SINK;
474 	desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
475 	desc.ops = &cs_ops;
476 	desc.pdata = pdata;
477 	desc.dev = &pdev->dev;
478 	desc.groups = smb_sink_groups;
479 	desc.name = coresight_alloc_device_name(&sink_devs, &pdev->dev);
480 	if (!desc.name) {
481 		dev_err(&pdev->dev, "Failed to alloc coresight device name");
482 		return -ENOMEM;
483 	}
484 	desc.access = CSDEV_ACCESS_IOMEM(drvdata->base);
485 
486 	drvdata->csdev = coresight_register(&desc);
487 	if (IS_ERR(drvdata->csdev))
488 		return PTR_ERR(drvdata->csdev);
489 
490 	drvdata->miscdev.name = desc.name;
491 	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
492 	drvdata->miscdev.fops = &smb_fops;
493 	ret = misc_register(&drvdata->miscdev);
494 	if (ret) {
495 		coresight_unregister(drvdata->csdev);
496 		dev_err(&pdev->dev, "Failed to register misc, ret=%d\n", ret);
497 	}
498 
499 	return ret;
500 }
501 
502 static void smb_unregister_sink(struct smb_drv_data *drvdata)
503 {
504 	misc_deregister(&drvdata->miscdev);
505 	coresight_unregister(drvdata->csdev);
506 }
507 
508 static int smb_config_inport(struct device *dev, bool enable)
509 {
510 	u64 func = enable ? 1 : 0;
511 	union acpi_object *obj;
512 	guid_t guid;
513 	u64 rev = 0;
514 
515 	/*
516 	 * Using DSM calls to enable/disable ultrasoc hardwares on
517 	 * tracing path, to prevent ultrasoc packet format being exposed.
518 	 */
519 	if (guid_parse(ULTRASOC_SMB_DSM_UUID, &guid)) {
520 		dev_err(dev, "Get GUID failed\n");
521 		return -EINVAL;
522 	}
523 
524 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, rev, func, NULL);
525 	if (!obj) {
526 		dev_err(dev, "ACPI handle failed\n");
527 		return -ENODEV;
528 	}
529 
530 	ACPI_FREE(obj);
531 
532 	return 0;
533 }
534 
535 static int smb_probe(struct platform_device *pdev)
536 {
537 	struct device *dev = &pdev->dev;
538 	struct smb_drv_data *drvdata;
539 	int ret;
540 
541 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
542 	if (!drvdata)
543 		return -ENOMEM;
544 
545 	drvdata->base = devm_platform_ioremap_resource(pdev, SMB_REG_ADDR_RES);
546 	if (IS_ERR(drvdata->base)) {
547 		dev_err(dev, "Failed to ioremap resource\n");
548 		return PTR_ERR(drvdata->base);
549 	}
550 
551 	smb_init_hw(drvdata);
552 
553 	ret = smb_init_data_buffer(pdev, &drvdata->sdb);
554 	if (ret) {
555 		dev_err(dev, "Failed to init buffer, ret = %d\n", ret);
556 		return ret;
557 	}
558 
559 	ret = smb_config_inport(dev, true);
560 	if (ret)
561 		return ret;
562 
563 	smb_reset_buffer(drvdata);
564 	platform_set_drvdata(pdev, drvdata);
565 	spin_lock_init(&drvdata->spinlock);
566 	drvdata->pid = -1;
567 
568 	ret = smb_register_sink(pdev, drvdata);
569 	if (ret) {
570 		smb_config_inport(&pdev->dev, false);
571 		dev_err(dev, "Failed to register SMB sink\n");
572 		return ret;
573 	}
574 
575 	return 0;
576 }
577 
578 static void smb_remove(struct platform_device *pdev)
579 {
580 	struct smb_drv_data *drvdata = platform_get_drvdata(pdev);
581 
582 	smb_unregister_sink(drvdata);
583 
584 	smb_config_inport(&pdev->dev, false);
585 }
586 
587 #ifdef CONFIG_ACPI
588 static const struct acpi_device_id ultrasoc_smb_acpi_match[] = {
589 	{"HISI03A1", 0},
590 	{}
591 };
592 MODULE_DEVICE_TABLE(acpi, ultrasoc_smb_acpi_match);
593 #endif
594 
595 static struct platform_driver smb_driver = {
596 	.driver = {
597 		.name = "ultrasoc-smb",
598 		.acpi_match_table = ACPI_PTR(ultrasoc_smb_acpi_match),
599 		.suppress_bind_attrs = true,
600 	},
601 	.probe = smb_probe,
602 	.remove_new = smb_remove,
603 };
604 module_platform_driver(smb_driver);
605 
606 MODULE_DESCRIPTION("UltraSoc SMB CoreSight driver");
607 MODULE_LICENSE("Dual MIT/GPL");
608 MODULE_AUTHOR("Jonathan Zhou <jonathan.zhouwen@huawei.com>");
609 MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>");
610