summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/platform/hal/linux/misc.c
blob: a356f334b187cddf4d773974e9b35ed9dd886f9a (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
/*
 * Copyright (C) 2010 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>

typedef struct _gsl_autogate_t {
    struct timer_list timer;	
    spinlock_t lock;
    int active;
    int timeout;
    gsl_device_t *dev;
} gsl_autogate_t;


#define KGSL_DEVICE_IDLE_TIMEOUT 5000	/* unit ms */

int kgsl_device_active(gsl_device_t *dev)
{
	unsigned long flags;
	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 (!autogate->active)
		kgsl_clock(autogate->dev->id, 1);
	autogate->active = 1;
	mod_timer(&autogate->timer, jiffies + msecs_to_jiffies(autogate->timeout));
	spin_unlock_irqrestore(&autogate->lock, flags);
	return 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);
	WARN(!autogate->active, "GPU Device %d is already inactive\n", autogate->dev->id);
	autogate->active = 0;
	/* idle check may sleep, so don't use it */
//	if (autogate->dev->ftbl.device_idle) 
//		autogate->dev->ftbl.device_idle(autogate->dev, GSL_TIMEOUT_DEFAULT);
	kgsl_clock(autogate->dev->id, 0);
	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 = kmalloc(sizeof(gsl_autogate_t), GFP_KERNEL);
	if (!autogate) {
		printk(KERN_ERR "%s: out of memory!\n", __func__);
		return -ENOMEM;
	}
	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);
	dev->autogate = autogate;
	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);
	if (autogate->active)
		del_timer(&autogate->timer);
	else
		kgsl_clock(autogate->dev->id, 1);

	kfree(autogate);
	dev->autogate = NULL;

}