aboutsummaryrefslogtreecommitdiff
path: root/write-stm-device/stm_user_api.c
blob: 776f20c70852c7b2dab04a92bf6d80059e6229f5 (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
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include "stm_user_api.h"

void release_stm_resource(struct stm_dev *dev);

static void enable_sink(const char *dev_name, unsigned int enable)
{
	char buf[256] = {0};
	sprintf(buf, "echo %u > /sys/bus/coresight/devices/%s/enable_sink",
		enable, dev_name);
	system(buf);
}

static void enable_source(const char *dev_name, unsigned int enable)
{
	char buf[256] = {0};
	sprintf(buf, "echo %u > /sys/bus/coresight/devices/%s/enable_source",
		enable, dev_name);
	system(buf);
}

static int set_policy(int fd, struct stp_policy_id *policy,
		      unsigned int chan, unsigned int width)
{
	unsigned int chan_perpage = PAGE_SIZE / BYTES_PER_CHANNEL;

	if(width % chan_perpage || chan % chan_perpage) {
		chan = chan / chan_perpage;
		width = (width / chan_perpage + 1) * BYTES_PER_CHANNEL;
	}

	policy->channel = chan;
	policy->__reserved_0 = 0;
	policy->__reserved_1 = 0;
	policy->width = width;
	policy->size = sizeof(struct stp_policy_id) + POLICY_NAME_LEN;
	memcpy(policy->id, STP_POLICY_NAME, POLICY_NAME_LEN);

	if (ioctl(fd, STP_POLICY_ID_SET, policy) == -1) {
		printf("STP_POLICY_ID_SET failed %s %d\n",
		       strerror(errno), errno);
		return -1;
	}

	return 0;
}

/*
 * dev	- storing the information of stimulus resources and STM device
 * chan	- the start index of channels
 * width- the number of channels for request, it must be not larger than
 * (PAGE_SIZE / BYTES_PER_CHANNEL) due to a limitation in STM driver
 * http://lxr.free-electrons.com/source/drivers/hwtracing/stm/core.c?v=4.6#L542
 */
int request_stm_resource(struct stm_dev *dev, unsigned int chan,
			 unsigned int width)
{
	int fd;
	int ret = 0;
	struct stp_policy_id *policy;

	if ((fd = open(STM_DEVICE_NAME, O_RDWR | O_SYNC)) == -1) {
		printf("Failed to open %s %s\n", STM_DEVICE_NAME,
			strerror(errno));
		return -1;
	}
	dev->fd = fd;

	/*
	 * Before allocating a policy for STM, the sink connected with STM must
	 * be enabled.
	 */
	enable_sink(ETF_SYS_NAME, 1);

	/* set a master/channel policy for this STM device, this
	 * is because that kernel have to know how many channels
	 * would be mapped, and the size of mapped memory must be
	 * a multiple of page size.
	 */
	dev->policy = malloc(sizeof(struct stp_policy_id) + POLICY_NAME_LEN);
	if (!dev->policy) {
		ret = -1;
		printf("Failed to malloc policy.\n");
		goto out;
	}

	if (set_policy(fd, dev->policy, chan, width)) {
		ret = -1;
		printf("Failed to set policy.\n");
		goto out;
	}

	return ret;

out:
	release_stm_resource(dev);
	return ret;
}

void release_stm_resource(struct stm_dev *dev)
{
	if (dev->policy) {
		free(dev->policy);
		dev->policy = NULL;
	}
	if (dev->fd) {
		close(dev->fd);
		dev->fd = 0;
	}

	//enable_source(STM_SYS_NAME, 0);
}

unsigned int stm_char_write(int fd, const char *data, unsigned int size)
{
	write(fd, data, size);
}