summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/platform/hal/linux/misc.c
blob: db7b8cf3a2d1ee71abf13f547cbdaefabac2d1d7 (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
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2010-2012 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <gsl.h>

#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/hardirq.h>
#include <linux/semaphore.h>

typedef struct _gsl_autogate_t {
    struct timer_list timer;	
    spinlock_t lock;
    int active;
    /* pending indicate the timer has been fired but clock not yet disabled. */
    int pending;
    int timeout;
    gsl_device_t *dev;
    struct work_struct dis_task;
} gsl_autogate_t;

static gsl_autogate_t *g_autogate[2];
static DEFINE_MUTEX(sem_dev);

#define KGSL_DEVICE_IDLE_TIMEOUT 2000	/* unit ms */

static void clk_disable_task(struct work_struct *work)
{
	gsl_autogate_t *autogate;
	autogate = container_of(work, gsl_autogate_t, dis_task);
	if (autogate->dev->ftbl.device_idle)
		autogate->dev->ftbl.device_idle(autogate->dev, GSL_TIMEOUT_DEFAULT);
	kgsl_clock(autogate->dev->id, 0);
	autogate->pending = 0;
}

static int _kgsl_device_active(gsl_device_t *dev, int all)
{
	unsigned long flags;
	int to_active = 0;
	gsl_autogate_t *autogate = dev->autogate;
	if (!autogate) {
		printk(KERN_ERR "%s: autogate has exited!\n", __func__);
		return 0;
	}
//	printk(KERN_ERR "%s:%d id %d active %d\n", __func__, __LINE__, dev->id, autogate->active);

	spin_lock_irqsave(&autogate->lock, flags);
	if (in_interrupt()) {
		if (!autogate->active && !autogate->pending)
			BUG();
	} else {
		to_active = !autogate->active;
		autogate->active = 1;
	}
	mod_timer(&autogate->timer, jiffies + msecs_to_jiffies(autogate->timeout));
	spin_unlock_irqrestore(&autogate->lock, flags);
	if (to_active)
		kgsl_clock(autogate->dev->id, 1);
	if (to_active && all) {
		int index;
		index = autogate->dev->id == GSL_DEVICE_G12 ? GSL_DEVICE_YAMATO - 1 :
			GSL_DEVICE_G12 - 1;
		mutex_lock(&sem_dev);
		if (g_autogate[index])
			_kgsl_device_active(g_autogate[index]->dev, 0);
		mutex_unlock(&sem_dev);
	}
	return 0;
}
int kgsl_device_active(gsl_device_t *dev)
{
	return _kgsl_device_active(dev, 0);
}

static void kgsl_device_inactive(unsigned long data)
{
	gsl_autogate_t *autogate = (gsl_autogate_t *)data;
	unsigned long flags;

//	printk(KERN_ERR "%s:%d id %d active %d\n", __func__, __LINE__, autogate->dev->id, autogate->active);
	del_timer(&autogate->timer);
	spin_lock_irqsave(&autogate->lock, flags);
	if (autogate->active) {
		autogate->active = 0;
		autogate->pending = 1;
		schedule_work(&autogate->dis_task);
	}
	spin_unlock_irqrestore(&autogate->lock, flags);
}

int kgsl_device_clock(gsl_deviceid_t id, int enable)
{
	int ret = GSL_SUCCESS;
	gsl_device_t *device;

	device = &gsl_driver.device[id-1];       // device_id is 1 based
	if (device->flags & GSL_FLAGS_INITIALIZED) {
		if (enable)
			kgsl_device_active(device);
		else
			kgsl_device_inactive((unsigned long)device);
	} else {
		printk(KERN_ERR "%s: Dev %d clock is already off!\n", __func__, id);
		ret = GSL_FAILURE;
	}
	
	return ret;
}

int kgsl_device_autogate_init(gsl_device_t *dev)
{
	gsl_autogate_t *autogate;

//	printk(KERN_ERR "%s:%d id %d\n", __func__, __LINE__, dev->id);
	autogate = kzalloc(sizeof(gsl_autogate_t), GFP_KERNEL);
	if (!autogate) {
		printk(KERN_ERR "%s: out of memory!\n", __func__);
		return -ENOMEM;
	}
	mutex_lock(&sem_dev);
	autogate->dev = dev;
	autogate->active = 1;
	spin_lock_init(&autogate->lock);
	autogate->timeout = KGSL_DEVICE_IDLE_TIMEOUT;
	init_timer(&autogate->timer);
	autogate->timer.expires = jiffies + msecs_to_jiffies(autogate->timeout);
	autogate->timer.function = kgsl_device_inactive;
	autogate->timer.data = (unsigned long)autogate;
	add_timer(&autogate->timer);
	INIT_WORK(&autogate->dis_task, clk_disable_task);
	dev->autogate = autogate;
	g_autogate[dev->id - 1] = autogate;
	mutex_unlock(&sem_dev);
	return 0;
}

void kgsl_device_autogate_exit(gsl_device_t *dev)
{
	gsl_autogate_t *autogate = dev->autogate;

//	printk(KERN_ERR "%s:%d id %d active %d\n", __func__, __LINE__, dev->id,  autogate->active);
	mutex_lock(&sem_dev);
	del_timer_sync(&autogate->timer);
	if (!autogate->active)
		kgsl_clock(autogate->dev->id, 1);
	flush_work(&autogate->dis_task);
	g_autogate[dev->id - 1] = NULL;
	mutex_unlock(&sem_dev);
	kfree(autogate);
	dev->autogate = NULL;
}