coresight-tmc.c 10.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
3 4
 *
 * Description: CoreSight Trace Memory Controller driver
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/err.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/spinlock.h>
19
#include <linux/pm_runtime.h>
20 21 22 23 24
#include <linux/of.h>
#include <linux/coresight.h>
#include <linux/amba/bus.h>

#include "coresight-priv.h"
25
#include "coresight-tmc.h"
26

27
void tmc_wait_for_tmcready(struct tmc_drvdata *drvdata)
28 29 30
{
	/* Ensure formatter, unformatter and hardware fifo are empty */
	if (coresight_timeout(drvdata->base,
31
			      TMC_STS, TMC_STS_TMCREADY_BIT, 1)) {
32
		dev_err(drvdata->dev,
33
			"timeout while waiting for TMC to be Ready\n");
34 35 36
	}
}

37
void tmc_flush_and_stop(struct tmc_drvdata *drvdata)
38 39 40 41 42 43
{
	u32 ffcr;

	ffcr = readl_relaxed(drvdata->base + TMC_FFCR);
	ffcr |= TMC_FFCR_STOP_ON_FLUSH;
	writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
44
	ffcr |= BIT(TMC_FFCR_FLUSHMAN_BIT);
45 46 47 48 49
	writel_relaxed(ffcr, drvdata->base + TMC_FFCR);
	/* Ensure flush completes */
	if (coresight_timeout(drvdata->base,
			      TMC_FFCR, TMC_FFCR_FLUSHMAN_BIT, 0)) {
		dev_err(drvdata->dev,
50
		"timeout while waiting for completion of Manual Flush\n");
51 52
	}

53
	tmc_wait_for_tmcready(drvdata);
54 55
}

56
void tmc_enable_hw(struct tmc_drvdata *drvdata)
57 58 59 60
{
	writel_relaxed(TMC_CTL_CAPT_EN, drvdata->base + TMC_CTL);
}

61
void tmc_disable_hw(struct tmc_drvdata *drvdata)
62 63 64 65 66 67
{
	writel_relaxed(0x0, drvdata->base + TMC_CTL);
}

static int tmc_read_prepare(struct tmc_drvdata *drvdata)
{
68
	int ret = 0;
69

70 71 72
	switch (drvdata->config_type) {
	case TMC_CONFIG_TYPE_ETB:
	case TMC_CONFIG_TYPE_ETF:
73
		ret = tmc_read_prepare_etb(drvdata);
74 75
		break;
	case TMC_CONFIG_TYPE_ETR:
76
		ret = tmc_read_prepare_etr(drvdata);
77 78 79
		break;
	default:
		ret = -EINVAL;
80
	}
81

82 83 84
	if (!ret)
		dev_info(drvdata->dev, "TMC read start\n");

85 86 87
	return ret;
}

88
static int tmc_read_unprepare(struct tmc_drvdata *drvdata)
89
{
90
	int ret = 0;
91

92 93 94
	switch (drvdata->config_type) {
	case TMC_CONFIG_TYPE_ETB:
	case TMC_CONFIG_TYPE_ETF:
95
		ret = tmc_read_unprepare_etb(drvdata);
96 97
		break;
	case TMC_CONFIG_TYPE_ETR:
98
		ret = tmc_read_unprepare_etr(drvdata);
99 100
		break;
	default:
101
		ret = -EINVAL;
102
	}
103

104 105
	if (!ret)
		dev_info(drvdata->dev, "TMC read end\n");
106 107

	return ret;
108 109 110 111
}

static int tmc_open(struct inode *inode, struct file *file)
{
112
	int ret;
113 114 115 116 117 118
	struct tmc_drvdata *drvdata = container_of(file->private_data,
						   struct tmc_drvdata, miscdev);

	ret = tmc_read_prepare(drvdata);
	if (ret)
		return ret;
119

120 121 122 123 124 125 126 127 128 129 130 131 132
	nonseekable_open(inode, file);

	dev_dbg(drvdata->dev, "%s: successfully opened\n", __func__);
	return 0;
}

static ssize_t tmc_read(struct file *file, char __user *data, size_t len,
			loff_t *ppos)
{
	struct tmc_drvdata *drvdata = container_of(file->private_data,
						   struct tmc_drvdata, miscdev);
	char *bufp = drvdata->buf + *ppos;

133 134
	if (*ppos + len > drvdata->len)
		len = drvdata->len - *ppos;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

	if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
		if (bufp == (char *)(drvdata->vaddr + drvdata->size))
			bufp = drvdata->vaddr;
		else if (bufp > (char *)(drvdata->vaddr + drvdata->size))
			bufp -= drvdata->size;
		if ((bufp + len) > (char *)(drvdata->vaddr + drvdata->size))
			len = (char *)(drvdata->vaddr + drvdata->size) - bufp;
	}

	if (copy_to_user(data, bufp, len)) {
		dev_dbg(drvdata->dev, "%s: copy_to_user failed\n", __func__);
		return -EFAULT;
	}

	*ppos += len;

152
	dev_dbg(drvdata->dev, "%s: %zu bytes copied, %d bytes left\n",
153
		__func__, len, (int)(drvdata->len - *ppos));
154 155 156 157 158
	return len;
}

static int tmc_release(struct inode *inode, struct file *file)
{
159
	int ret;
160 161 162
	struct tmc_drvdata *drvdata = container_of(file->private_data,
						   struct tmc_drvdata, miscdev);

163 164 165
	ret = tmc_read_unprepare(drvdata);
	if (ret)
		return ret;
166 167 168 169 170 171 172 173 174 175 176 177 178

	dev_dbg(drvdata->dev, "%s: released\n", __func__);
	return 0;
}

static const struct file_operations tmc_fops = {
	.owner		= THIS_MODULE,
	.open		= tmc_open,
	.read		= tmc_read,
	.release	= tmc_release,
	.llseek		= no_llseek,
};

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
static enum tmc_mem_intf_width tmc_get_memwidth(u32 devid)
{
	enum tmc_mem_intf_width memwidth;

	/*
	 * Excerpt from the TRM:
	 *
	 * DEVID::MEMWIDTH[10:8]
	 * 0x2 Memory interface databus is 32 bits wide.
	 * 0x3 Memory interface databus is 64 bits wide.
	 * 0x4 Memory interface databus is 128 bits wide.
	 * 0x5 Memory interface databus is 256 bits wide.
	 */
	switch (BMVAL(devid, 8, 10)) {
	case 0x2:
		memwidth = TMC_MEM_INTF_WIDTH_32BITS;
		break;
	case 0x3:
		memwidth = TMC_MEM_INTF_WIDTH_64BITS;
		break;
	case 0x4:
		memwidth = TMC_MEM_INTF_WIDTH_128BITS;
		break;
	case 0x5:
		memwidth = TMC_MEM_INTF_WIDTH_256BITS;
		break;
	default:
		memwidth = 0;
	}

	return memwidth;
}

212 213 214 215 216 217 218 219 220 221 222 223 224
#define coresight_tmc_reg(name, offset)			\
	coresight_simple_reg32(struct tmc_drvdata, name, offset)
#define coresight_tmc_reg64(name, lo_off, hi_off)	\
	coresight_simple_reg64(struct tmc_drvdata, name, lo_off, hi_off)

coresight_tmc_reg(rsz, TMC_RSZ);
coresight_tmc_reg(sts, TMC_STS);
coresight_tmc_reg(trg, TMC_TRG);
coresight_tmc_reg(ctl, TMC_CTL);
coresight_tmc_reg(ffsr, TMC_FFSR);
coresight_tmc_reg(ffcr, TMC_FFCR);
coresight_tmc_reg(mode, TMC_MODE);
coresight_tmc_reg(pscr, TMC_PSCR);
225
coresight_tmc_reg(axictl, TMC_AXICTL);
226 227 228
coresight_tmc_reg(devid, CORESIGHT_DEVID);
coresight_tmc_reg64(rrp, TMC_RRP, TMC_RRPHI);
coresight_tmc_reg64(rwp, TMC_RWP, TMC_RWPHI);
229
coresight_tmc_reg64(dba, TMC_DBALO, TMC_DBAHI);
230 231 232 233 234 235 236 237 238 239 240 241 242

static struct attribute *coresight_tmc_mgmt_attrs[] = {
	&dev_attr_rsz.attr,
	&dev_attr_sts.attr,
	&dev_attr_rrp.attr,
	&dev_attr_rwp.attr,
	&dev_attr_trg.attr,
	&dev_attr_ctl.attr,
	&dev_attr_ffsr.attr,
	&dev_attr_ffcr.attr,
	&dev_attr_mode.attr,
	&dev_attr_pscr.attr,
	&dev_attr_devid.attr,
243 244
	&dev_attr_dba.attr,
	&dev_attr_axictl.attr,
245 246
	NULL,
};
247

248 249
static ssize_t trigger_cntr_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
{
	struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);
	unsigned long val = drvdata->trigger_cntr;

	return sprintf(buf, "%#lx\n", val);
}

static ssize_t trigger_cntr_store(struct device *dev,
			     struct device_attribute *attr,
			     const char *buf, size_t size)
{
	int ret;
	unsigned long val;
	struct tmc_drvdata *drvdata = dev_get_drvdata(dev->parent);

	ret = kstrtoul(buf, 16, &val);
	if (ret)
		return ret;

	drvdata->trigger_cntr = val;
	return size;
}
static DEVICE_ATTR_RW(trigger_cntr);

274
static struct attribute *coresight_tmc_attrs[] = {
275 276 277 278
	&dev_attr_trigger_cntr.attr,
	NULL,
};

279 280
static const struct attribute_group coresight_tmc_group = {
	.attrs = coresight_tmc_attrs,
281 282
};

283 284 285 286 287 288 289 290
static const struct attribute_group coresight_tmc_mgmt_group = {
	.attrs = coresight_tmc_mgmt_attrs,
	.name = "mgmt",
};

const struct attribute_group *coresight_tmc_groups[] = {
	&coresight_tmc_group,
	&coresight_tmc_mgmt_group,
291 292 293
	NULL,
};

294 295 296 297
/* Detect and initialise the capabilities of a TMC ETR */
static int tmc_etr_setup_caps(struct tmc_drvdata *drvdata,
			     u32 devid, void *dev_caps)
{
298 299
	u32 dma_mask = 0;

300 301 302
	/* Set the unadvertised capabilities */
	tmc_etr_init_caps(drvdata, (u32)(unsigned long)dev_caps);

303 304
	if (!(devid & TMC_DEVID_NOSCAT))
		tmc_etr_set_cap(drvdata, TMC_ETR_SG);
305 306 307 308 309 310

	/* Check if the AXI address width is available */
	if (devid & TMC_DEVID_AXIAW_VALID)
		dma_mask = ((devid >> TMC_DEVID_AXIAW_SHIFT) &
				TMC_DEVID_AXIAW_MASK);

311
	/*
312 313
	 * Unless specified in the device configuration, ETR uses a 40-bit
	 * AXI master in place of the embedded SRAM of ETB/ETF.
314
	 */
315 316 317 318 319 320 321 322 323 324 325 326 327
	switch (dma_mask) {
	case 32:
	case 40:
	case 44:
	case 48:
	case 52:
		dev_info(drvdata->dev, "Detected dma mask %dbits\n", dma_mask);
		break;
	default:
		dma_mask = 40;
	}

	return dma_set_mask_and_coherent(drvdata->dev, DMA_BIT_MASK(dma_mask));
328 329
}

330 331 332 333 334 335 336 337 338
static int tmc_probe(struct amba_device *adev, const struct amba_id *id)
{
	int ret = 0;
	u32 devid;
	void __iomem *base;
	struct device *dev = &adev->dev;
	struct coresight_platform_data *pdata = NULL;
	struct tmc_drvdata *drvdata;
	struct resource *res = &adev->res;
339
	struct coresight_desc desc = { 0 };
340 341 342 343
	struct device_node *np = adev->dev.of_node;

	if (np) {
		pdata = of_get_coresight_platform_data(dev, np);
344 345 346 347
		if (IS_ERR(pdata)) {
			ret = PTR_ERR(pdata);
			goto out;
		}
348 349 350
		adev->dev.platform_data = pdata;
	}

351
	ret = -ENOMEM;
352 353
	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
	if (!drvdata)
354 355
		goto out;

356 357 358 359 360
	drvdata->dev = &adev->dev;
	dev_set_drvdata(dev, drvdata);

	/* Validity for the resource is already checked by the AMBA core */
	base = devm_ioremap_resource(dev, res);
361 362 363 364
	if (IS_ERR(base)) {
		ret = PTR_ERR(base);
		goto out;
	}
365 366 367 368 369 370 371

	drvdata->base = base;

	spin_lock_init(&drvdata->spinlock);

	devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
	drvdata->config_type = BMVAL(devid, 6, 7);
372
	drvdata->memwidth = tmc_get_memwidth(devid);
373 374 375 376 377 378 379 380 381 382 383 384

	if (drvdata->config_type == TMC_CONFIG_TYPE_ETR) {
		if (np)
			ret = of_property_read_u32(np,
						   "arm,buffer-size",
						   &drvdata->size);
		if (ret)
			drvdata->size = SZ_1M;
	} else {
		drvdata->size = readl_relaxed(drvdata->base + TMC_RSZ) * 4;
	}

385
	pm_runtime_put(&adev->dev);
386

387 388 389
	desc.pdata = pdata;
	desc.dev = dev;
	desc.groups = coresight_tmc_groups;
390

391 392
	switch (drvdata->config_type) {
	case TMC_CONFIG_TYPE_ETB:
393 394 395
		desc.type = CORESIGHT_DEV_TYPE_SINK;
		desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
		desc.ops = &tmc_etb_cs_ops;
396 397
		break;
	case TMC_CONFIG_TYPE_ETR:
398 399 400
		desc.type = CORESIGHT_DEV_TYPE_SINK;
		desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
		desc.ops = &tmc_etr_cs_ops;
401
		ret = tmc_etr_setup_caps(drvdata, devid, id->data);
402 403
		if (ret)
			goto out;
404 405
		break;
	case TMC_CONFIG_TYPE_ETF:
406 407 408
		desc.type = CORESIGHT_DEV_TYPE_LINKSINK;
		desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_FIFO;
		desc.ops = &tmc_etf_cs_ops;
409 410 411 412 413
		break;
	default:
		pr_err("%s: Unsupported TMC config\n", pdata->name);
		ret = -EINVAL;
		goto out;
414 415
	}

416
	drvdata->csdev = coresight_register(&desc);
417 418
	if (IS_ERR(drvdata->csdev)) {
		ret = PTR_ERR(drvdata->csdev);
419
		goto out;
420 421 422 423 424 425 426
	}

	drvdata->miscdev.name = pdata->name;
	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
	drvdata->miscdev.fops = &tmc_fops;
	ret = misc_register(&drvdata->miscdev);
	if (ret)
427 428
		coresight_unregister(drvdata->csdev);
out:
429 430 431
	return ret;
}

A
Arvind Yadav 已提交
432
static const struct amba_id tmc_ids[] = {
433
	{
434 435
		.id     = 0x000bb961,
		.mask   = 0x000fffff,
436
	},
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	{
		/* Coresight SoC 600 TMC-ETR/ETS */
		.id	= 0x000bb9e8,
		.mask	= 0x000fffff,
		.data	= (void *)(unsigned long)CORESIGHT_SOC_600_ETR_CAPS,
	},
	{
		/* Coresight SoC 600 TMC-ETB */
		.id	= 0x000bb9e9,
		.mask	= 0x000fffff,
	},
	{
		/* Coresight SoC 600 TMC-ETF */
		.id	= 0x000bb9ea,
		.mask	= 0x000fffff,
	},
453 454 455 456 457 458 459
	{ 0, 0},
};

static struct amba_driver tmc_driver = {
	.drv = {
		.name   = "coresight-tmc",
		.owner  = THIS_MODULE,
460
		.suppress_bind_attrs = true,
461 462 463 464
	},
	.probe		= tmc_probe,
	.id_table	= tmc_ids,
};
465
builtin_amba_driver(tmc_driver);