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 	int ret = 0;
101 
102 	mutex_lock(&drvdata->mutex);
103 
104 	if (drvdata->reading) {
105 		ret = -EBUSY;
106 		goto out;
107 	}
108 
109 	if (atomic_read(drvdata->csdev->refcnt)) {
110 		ret = -EBUSY;
111 		goto out;
112 	}
113 
114 	smb_update_data_size(drvdata);
115 
116 	drvdata->reading = true;
117 out:
118 	mutex_unlock(&drvdata->mutex);
119 
120 	return ret;
121 }
122 
123 static ssize_t smb_read(struct file *file, char __user *data, size_t len,
124 			loff_t *ppos)
125 {
126 	struct smb_drv_data *drvdata = container_of(file->private_data,
127 					struct smb_drv_data, miscdev);
128 	struct smb_data_buffer *sdb = &drvdata->sdb;
129 	struct device *dev = &drvdata->csdev->dev;
130 	ssize_t to_copy = 0;
131 
132 	if (!len)
133 		return 0;
134 
135 	mutex_lock(&drvdata->mutex);
136 
137 	if (!sdb->data_size)
138 		goto out;
139 
140 	to_copy = min(sdb->data_size, len);
141 
142 	/* Copy parts of trace data when read pointer wrap around SMB buffer */
143 	if (sdb->buf_rdptr + to_copy > sdb->buf_size)
144 		to_copy = sdb->buf_size - sdb->buf_rdptr;
145 
146 	if (copy_to_user(data, sdb->buf_base + sdb->buf_rdptr, to_copy)) {
147 		dev_dbg(dev, "Failed to copy data to user\n");
148 		to_copy = -EFAULT;
149 		goto out;
150 	}
151 
152 	*ppos += to_copy;
153 
154 	smb_update_read_ptr(drvdata, to_copy);
155 
156 	dev_dbg(dev, "%zu bytes copied\n", to_copy);
157 out:
158 	if (!sdb->data_size)
159 		smb_reset_buffer(drvdata);
160 	mutex_unlock(&drvdata->mutex);
161 
162 	return to_copy;
163 }
164 
165 static int smb_release(struct inode *inode, struct file *file)
166 {
167 	struct smb_drv_data *drvdata = container_of(file->private_data,
168 					struct smb_drv_data, miscdev);
169 
170 	mutex_lock(&drvdata->mutex);
171 	drvdata->reading = false;
172 	mutex_unlock(&drvdata->mutex);
173 
174 	return 0;
175 }
176 
177 static const struct file_operations smb_fops = {
178 	.owner		= THIS_MODULE,
179 	.open		= smb_open,
180 	.read		= smb_read,
181 	.release	= smb_release,
182 	.llseek		= no_llseek,
183 };
184 
185 static ssize_t buf_size_show(struct device *dev, struct device_attribute *attr,
186 			     char *buf)
187 {
188 	struct smb_drv_data *drvdata = dev_get_drvdata(dev->parent);
189 
190 	return sysfs_emit(buf, "0x%lx\n", drvdata->sdb.buf_size);
191 }
192 static DEVICE_ATTR_RO(buf_size);
193 
194 static struct attribute *smb_sink_attrs[] = {
195 	coresight_simple_reg32(read_pos, SMB_LB_RD_ADDR_REG),
196 	coresight_simple_reg32(write_pos, SMB_LB_WR_ADDR_REG),
197 	coresight_simple_reg32(buf_status, SMB_LB_INT_STS_REG),
198 	&dev_attr_buf_size.attr,
199 	NULL
200 };
201 
202 static const struct attribute_group smb_sink_group = {
203 	.attrs = smb_sink_attrs,
204 	.name = "mgmt",
205 };
206 
207 static const struct attribute_group *smb_sink_groups[] = {
208 	&smb_sink_group,
209 	NULL
210 };
211 
212 static void smb_enable_hw(struct smb_drv_data *drvdata)
213 {
214 	writel(SMB_GLB_EN_HW_ENABLE, drvdata->base + SMB_GLB_EN_REG);
215 }
216 
217 static void smb_disable_hw(struct smb_drv_data *drvdata)
218 {
219 	writel(0x0, drvdata->base + SMB_GLB_EN_REG);
220 }
221 
222 static void smb_enable_sysfs(struct coresight_device *csdev)
223 {
224 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
225 
226 	if (drvdata->mode != CS_MODE_DISABLED)
227 		return;
228 
229 	smb_enable_hw(drvdata);
230 	drvdata->mode = CS_MODE_SYSFS;
231 }
232 
233 static int smb_enable_perf(struct coresight_device *csdev, void *data)
234 {
235 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
236 	struct perf_output_handle *handle = data;
237 	struct cs_buffers *buf = etm_perf_sink_config(handle);
238 	pid_t pid;
239 
240 	if (!buf)
241 		return -EINVAL;
242 
243 	/* Get a handle on the pid of the target process */
244 	pid = buf->pid;
245 
246 	/* Device is already in used by other session */
247 	if (drvdata->pid != -1 && drvdata->pid != pid)
248 		return -EBUSY;
249 
250 	if (drvdata->pid == -1) {
251 		smb_enable_hw(drvdata);
252 		drvdata->pid = pid;
253 		drvdata->mode = CS_MODE_PERF;
254 	}
255 
256 	return 0;
257 }
258 
259 static int smb_enable(struct coresight_device *csdev, u32 mode, void *data)
260 {
261 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
262 	int ret = 0;
263 
264 	mutex_lock(&drvdata->mutex);
265 
266 	/* Do nothing, the trace data is reading by other interface now */
267 	if (drvdata->reading) {
268 		ret = -EBUSY;
269 		goto out;
270 	}
271 
272 	/* Do nothing, the SMB is already enabled as other mode */
273 	if (drvdata->mode != CS_MODE_DISABLED && drvdata->mode != mode) {
274 		ret = -EBUSY;
275 		goto out;
276 	}
277 
278 	switch (mode) {
279 	case CS_MODE_SYSFS:
280 		smb_enable_sysfs(csdev);
281 		break;
282 	case CS_MODE_PERF:
283 		ret = smb_enable_perf(csdev, data);
284 		break;
285 	default:
286 		ret = -EINVAL;
287 	}
288 
289 	if (ret)
290 		goto out;
291 
292 	atomic_inc(csdev->refcnt);
293 
294 	dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n");
295 out:
296 	mutex_unlock(&drvdata->mutex);
297 
298 	return ret;
299 }
300 
301 static int smb_disable(struct coresight_device *csdev)
302 {
303 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
304 	int ret = 0;
305 
306 	mutex_lock(&drvdata->mutex);
307 
308 	if (drvdata->reading) {
309 		ret = -EBUSY;
310 		goto out;
311 	}
312 
313 	if (atomic_dec_return(csdev->refcnt)) {
314 		ret = -EBUSY;
315 		goto out;
316 	}
317 
318 	/* Complain if we (somehow) got out of sync */
319 	WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
320 
321 	smb_disable_hw(drvdata);
322 
323 	/* Dissociate from the target process. */
324 	drvdata->pid = -1;
325 	drvdata->mode = CS_MODE_DISABLED;
326 
327 	dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n");
328 out:
329 	mutex_unlock(&drvdata->mutex);
330 
331 	return ret;
332 }
333 
334 static void *smb_alloc_buffer(struct coresight_device *csdev,
335 			      struct perf_event *event, void **pages,
336 			      int nr_pages, bool overwrite)
337 {
338 	struct cs_buffers *buf;
339 	int node;
340 
341 	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
342 	buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
343 	if (!buf)
344 		return NULL;
345 
346 	buf->snapshot = overwrite;
347 	buf->nr_pages = nr_pages;
348 	buf->data_pages = pages;
349 	buf->pid = task_pid_nr(event->owner);
350 
351 	return buf;
352 }
353 
354 static void smb_free_buffer(void *config)
355 {
356 	struct cs_buffers *buf = config;
357 
358 	kfree(buf);
359 }
360 
361 static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
362 				 struct cs_buffers *buf,
363 				 unsigned long head)
364 {
365 	struct smb_data_buffer *sdb = &drvdata->sdb;
366 	char **dst_pages = (char **)buf->data_pages;
367 	unsigned long to_copy;
368 	long pg_idx, pg_offset;
369 
370 	pg_idx = head >> PAGE_SHIFT;
371 	pg_offset = head & (PAGE_SIZE - 1);
372 
373 	while (sdb->data_size) {
374 		unsigned long pg_space = PAGE_SIZE - pg_offset;
375 
376 		to_copy = min(sdb->data_size, pg_space);
377 
378 		/* Copy parts of trace data when read pointer wrap around */
379 		if (sdb->buf_rdptr + to_copy > sdb->buf_size)
380 			to_copy = sdb->buf_size - sdb->buf_rdptr;
381 
382 		memcpy(dst_pages[pg_idx] + pg_offset,
383 			      sdb->buf_base + sdb->buf_rdptr, to_copy);
384 
385 		pg_offset += to_copy;
386 		if (pg_offset >= PAGE_SIZE) {
387 			pg_offset = 0;
388 			pg_idx++;
389 			pg_idx %= buf->nr_pages;
390 		}
391 		smb_update_read_ptr(drvdata, to_copy);
392 	}
393 
394 	smb_reset_buffer(drvdata);
395 }
396 
397 static unsigned long smb_update_buffer(struct coresight_device *csdev,
398 				       struct perf_output_handle *handle,
399 				       void *sink_config)
400 {
401 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
402 	struct smb_data_buffer *sdb = &drvdata->sdb;
403 	struct cs_buffers *buf = sink_config;
404 	unsigned long data_size = 0;
405 	bool lost = false;
406 
407 	if (!buf)
408 		return 0;
409 
410 	mutex_lock(&drvdata->mutex);
411 
412 	/* Don't do anything if another tracer is using this sink. */
413 	if (atomic_read(csdev->refcnt) != 1)
414 		goto out;
415 
416 	smb_disable_hw(drvdata);
417 	smb_update_data_size(drvdata);
418 
419 	/*
420 	 * The SMB buffer may be bigger than the space available in the
421 	 * perf ring buffer (handle->size). If so advance the offset so
422 	 * that we get the latest trace data.
423 	 */
424 	if (sdb->data_size > handle->size) {
425 		smb_update_read_ptr(drvdata, sdb->data_size - handle->size);
426 		lost = true;
427 	}
428 
429 	data_size = sdb->data_size;
430 	smb_sync_perf_buffer(drvdata, buf, handle->head);
431 	if (!buf->snapshot && lost)
432 		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
433 out:
434 	mutex_unlock(&drvdata->mutex);
435 
436 	return data_size;
437 }
438 
439 static const struct coresight_ops_sink smb_cs_ops = {
440 	.enable		= smb_enable,
441 	.disable	= smb_disable,
442 	.alloc_buffer	= smb_alloc_buffer,
443 	.free_buffer	= smb_free_buffer,
444 	.update_buffer	= smb_update_buffer,
445 };
446 
447 static const struct coresight_ops cs_ops = {
448 	.sink_ops	= &smb_cs_ops,
449 };
450 
451 static int smb_init_data_buffer(struct platform_device *pdev,
452 				struct smb_data_buffer *sdb)
453 {
454 	struct resource *res;
455 	void *base;
456 
457 	res = platform_get_resource(pdev, IORESOURCE_MEM, SMB_BUF_ADDR_RES);
458 	if (!res) {
459 		dev_err(&pdev->dev, "SMB device failed to get resource\n");
460 		return -EINVAL;
461 	}
462 
463 	sdb->buf_rdptr = 0;
464 	sdb->buf_hw_base = FIELD_GET(SMB_BUF_ADDR_LO_MSK, res->start);
465 	sdb->buf_size = resource_size(res);
466 	if (sdb->buf_size == 0)
467 		return -EINVAL;
468 
469 	/*
470 	 * This is a chunk of memory, use classic mapping with better
471 	 * performance.
472 	 */
473 	base = devm_memremap(&pdev->dev, sdb->buf_hw_base, sdb->buf_size,
474 				MEMREMAP_WB);
475 	if (IS_ERR(base))
476 		return PTR_ERR(base);
477 
478 	sdb->buf_base = base;
479 
480 	return 0;
481 }
482 
483 static void smb_init_hw(struct smb_drv_data *drvdata)
484 {
485 	smb_disable_hw(drvdata);
486 	smb_reset_buffer(drvdata);
487 
488 	writel(SMB_LB_CFG_LO_DEFAULT, drvdata->base + SMB_LB_CFG_LO_REG);
489 	writel(SMB_LB_CFG_HI_DEFAULT, drvdata->base + SMB_LB_CFG_HI_REG);
490 	writel(SMB_GLB_CFG_DEFAULT, drvdata->base + SMB_GLB_CFG_REG);
491 	writel(SMB_GLB_INT_CFG, drvdata->base + SMB_GLB_INT_REG);
492 	writel(SMB_LB_INT_CTRL_CFG, drvdata->base + SMB_LB_INT_CTRL_REG);
493 }
494 
495 static int smb_register_sink(struct platform_device *pdev,
496 			     struct smb_drv_data *drvdata)
497 {
498 	struct coresight_platform_data *pdata = NULL;
499 	struct coresight_desc desc = { 0 };
500 	int ret;
501 
502 	pdata = coresight_get_platform_data(&pdev->dev);
503 	if (IS_ERR(pdata))
504 		return PTR_ERR(pdata);
505 
506 	desc.type = CORESIGHT_DEV_TYPE_SINK;
507 	desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
508 	desc.ops = &cs_ops;
509 	desc.pdata = pdata;
510 	desc.dev = &pdev->dev;
511 	desc.groups = smb_sink_groups;
512 	desc.name = coresight_alloc_device_name(&sink_devs, &pdev->dev);
513 	if (!desc.name) {
514 		dev_err(&pdev->dev, "Failed to alloc coresight device name");
515 		return -ENOMEM;
516 	}
517 	desc.access = CSDEV_ACCESS_IOMEM(drvdata->base);
518 
519 	drvdata->csdev = coresight_register(&desc);
520 	if (IS_ERR(drvdata->csdev))
521 		return PTR_ERR(drvdata->csdev);
522 
523 	drvdata->miscdev.name = desc.name;
524 	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
525 	drvdata->miscdev.fops = &smb_fops;
526 	ret = misc_register(&drvdata->miscdev);
527 	if (ret) {
528 		coresight_unregister(drvdata->csdev);
529 		dev_err(&pdev->dev, "Failed to register misc, ret=%d\n", ret);
530 	}
531 
532 	return ret;
533 }
534 
535 static void smb_unregister_sink(struct smb_drv_data *drvdata)
536 {
537 	misc_deregister(&drvdata->miscdev);
538 	coresight_unregister(drvdata->csdev);
539 }
540 
541 static int smb_config_inport(struct device *dev, bool enable)
542 {
543 	u64 func = enable ? 1 : 0;
544 	union acpi_object *obj;
545 	guid_t guid;
546 	u64 rev = 0;
547 
548 	/*
549 	 * Using DSM calls to enable/disable ultrasoc hardwares on
550 	 * tracing path, to prevent ultrasoc packet format being exposed.
551 	 */
552 	if (guid_parse(ULTRASOC_SMB_DSM_UUID, &guid)) {
553 		dev_err(dev, "Get GUID failed\n");
554 		return -EINVAL;
555 	}
556 
557 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, rev, func, NULL);
558 	if (!obj) {
559 		dev_err(dev, "ACPI handle failed\n");
560 		return -ENODEV;
561 	}
562 
563 	ACPI_FREE(obj);
564 
565 	return 0;
566 }
567 
568 static int smb_probe(struct platform_device *pdev)
569 {
570 	struct device *dev = &pdev->dev;
571 	struct smb_drv_data *drvdata;
572 	int ret;
573 
574 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
575 	if (!drvdata)
576 		return -ENOMEM;
577 
578 	drvdata->base = devm_platform_ioremap_resource(pdev, SMB_REG_ADDR_RES);
579 	if (IS_ERR(drvdata->base)) {
580 		dev_err(dev, "Failed to ioremap resource\n");
581 		return PTR_ERR(drvdata->base);
582 	}
583 
584 	smb_init_hw(drvdata);
585 
586 	ret = smb_init_data_buffer(pdev, &drvdata->sdb);
587 	if (ret) {
588 		dev_err(dev, "Failed to init buffer, ret = %d\n", ret);
589 		return ret;
590 	}
591 
592 	mutex_init(&drvdata->mutex);
593 	drvdata->pid = -1;
594 
595 	ret = smb_register_sink(pdev, drvdata);
596 	if (ret) {
597 		dev_err(dev, "Failed to register SMB sink\n");
598 		return ret;
599 	}
600 
601 	ret = smb_config_inport(dev, true);
602 	if (ret) {
603 		smb_unregister_sink(drvdata);
604 		return ret;
605 	}
606 
607 	platform_set_drvdata(pdev, drvdata);
608 
609 	return 0;
610 }
611 
612 static int smb_remove(struct platform_device *pdev)
613 {
614 	struct smb_drv_data *drvdata = platform_get_drvdata(pdev);
615 	int ret;
616 
617 	ret = smb_config_inport(&pdev->dev, false);
618 	if (ret)
619 		return ret;
620 
621 	smb_unregister_sink(drvdata);
622 
623 	return 0;
624 }
625 
626 #ifdef CONFIG_ACPI
627 static const struct acpi_device_id ultrasoc_smb_acpi_match[] = {
628 	{"HISI03A1", 0},
629 	{}
630 };
631 MODULE_DEVICE_TABLE(acpi, ultrasoc_smb_acpi_match);
632 #endif
633 
634 static struct platform_driver smb_driver = {
635 	.driver = {
636 		.name = "ultrasoc-smb",
637 		.acpi_match_table = ACPI_PTR(ultrasoc_smb_acpi_match),
638 		.suppress_bind_attrs = true,
639 	},
640 	.probe = smb_probe,
641 	.remove = smb_remove,
642 };
643 module_platform_driver(smb_driver);
644 
645 MODULE_DESCRIPTION("UltraSoc SMB CoreSight driver");
646 MODULE_LICENSE("Dual MIT/GPL");
647 MODULE_AUTHOR("Jonathan Zhou <jonathan.zhouwen@huawei.com>");
648 MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>");
649