summaryrefslogtreecommitdiff
path: root/drivers/edgetpu/edgetpu-wakelock.c
blob: 61af4fa06b0e228b1956d21317f4b75ac0bbf19b (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Wakelock for the runtime to explicitly claim it's going to use the EdgeTPU
 * device.
 *
 * Copyright (C) 2021 Google, Inc.
 */

#include <linux/err.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/slab.h>

#include "edgetpu-config.h"
#include "edgetpu-internal.h"
#include "edgetpu-wakelock.h"

/*
 * Returns the first event with a non-zero counter.
 * Returns EDGETPU_WAKELOCK_EVENT_END if all event counters are zero.
 *
 * Caller holds @wakelock->lock.
 */
static enum edgetpu_wakelock_event
wakelock_non_zero_event(struct edgetpu_wakelock *wakelock)
{
	int i;

	for (i = 0; i < EDGETPU_WAKELOCK_EVENT_END; i++)
		if (wakelock->event_count[i])
			return i;
	return EDGETPU_WAKELOCK_EVENT_END;
}

struct edgetpu_wakelock *edgetpu_wakelock_alloc(struct edgetpu_dev *etdev)
{
#ifndef EDGETPU_HAS_WAKELOCK
	return EDGETPU_NO_WAKELOCK;
#else /* !EDGETPU_HAS_WAKELOCK */
	struct edgetpu_wakelock *wakelock =
		kzalloc(sizeof(*wakelock), GFP_KERNEL);

	if (!wakelock)
		return NULL;
	wakelock->etdev = etdev;
	mutex_init(&wakelock->lock);
	/* Initialize client wakelock state to "released" */
	wakelock->req_count = 0;
	return wakelock;
#endif /* EDGETPU_HAS_WAKELOCK */
}

void edgetpu_wakelock_free(struct edgetpu_wakelock *wakelock)
{
	if (IS_ERR_OR_NULL(wakelock))
		return;
	kfree(wakelock);
}

bool edgetpu_wakelock_inc_event(struct edgetpu_wakelock *wakelock,
				enum edgetpu_wakelock_event evt)
{
	bool ret = true;

	if (NO_WAKELOCK(wakelock))
		return true;
	mutex_lock(&wakelock->lock);
	if (!wakelock->req_count) {
		ret = false;
		etdev_warn(
			wakelock->etdev,
			"invalid increase event %d when wakelock is released",
			evt);
	} else {
		++wakelock->event_count[evt];
		/* integer overflow.. */
		if (unlikely(wakelock->event_count[evt] == 0)) {
			--wakelock->event_count[evt];
			ret = false;
			etdev_warn_once(wakelock->etdev,
					"int overflow on increasing event %d",
					evt);
		}
	}
	mutex_unlock(&wakelock->lock);
	return ret;
}

bool edgetpu_wakelock_dec_event(struct edgetpu_wakelock *wakelock,
				enum edgetpu_wakelock_event evt)
{
	bool ret = true;

	if (NO_WAKELOCK(wakelock))
		return true;
	mutex_lock(&wakelock->lock);
	if (!wakelock->event_count[evt]) {
		ret = false;
		etdev_warn(wakelock->etdev, "event %d unbalanced decreasing",
			   evt);
	} else {
		--wakelock->event_count[evt];
	}
	mutex_unlock(&wakelock->lock);
	return ret;
}

uint edgetpu_wakelock_lock(struct edgetpu_wakelock *wakelock)
{
	if (NO_WAKELOCK(wakelock))
		return 1;
	mutex_lock(&wakelock->lock);
	return wakelock->req_count;
}

void edgetpu_wakelock_unlock(struct edgetpu_wakelock *wakelock)
{
	if (!NO_WAKELOCK(wakelock))
		mutex_unlock(&wakelock->lock);
}

int edgetpu_wakelock_acquire(struct edgetpu_wakelock *wakelock)
{
	int ret;

	if (NO_WAKELOCK(wakelock))
		return 1;
	ret = wakelock->req_count++;
	/* integer overflow */
	if (unlikely(ret < 0)) {
		wakelock->req_count--;
		return -EOVERFLOW;
	}
	return ret;
}

int edgetpu_wakelock_release(struct edgetpu_wakelock *wakelock)
{
	if (NO_WAKELOCK(wakelock))
		return 1;
	if (!wakelock->req_count) {
		etdev_warn(wakelock->etdev, "invalid wakelock release");
		return -EINVAL;
	}
	/* only need to check events when this is the last reference */
	if (wakelock->req_count == 1) {
		enum edgetpu_wakelock_event evt =
			wakelock_non_zero_event(wakelock);

		if (evt != EDGETPU_WAKELOCK_EVENT_END) {
			etdev_warn(
				wakelock->etdev,
				"event %d is happening, refusing wakelock release",
				evt);
			return -EAGAIN;
		}
	}
	return --wakelock->req_count;
}