blob: b3017435932c2e06143a469ea8222b31a001c12a [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2002,2007-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#ifndef __KGSL_DEVICE_H
14#define __KGSL_DEVICE_H
15
16#include <linux/idr.h>
17#include <linux/wakelock.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070018#include <linux/earlysuspend.h>
19
20#include "kgsl.h"
21#include "kgsl_mmu.h"
22#include "kgsl_pwrctrl.h"
23#include "kgsl_log.h"
24#include "kgsl_pwrscale.h"
25
26#define KGSL_TIMEOUT_NONE 0
27#define KGSL_TIMEOUT_DEFAULT 0xFFFFFFFF
28
29#define FIRST_TIMEOUT (HZ / 2)
30
31
32/* KGSL device state is initialized to INIT when platform_probe *
33 * sucessfully initialized the device. Once a device has been opened *
34 * (started) it becomes active. NAP implies that only low latency *
35 * resources (for now clocks on some platforms) are off. SLEEP implies *
36 * that the KGSL module believes a device is idle (has been inactive *
37 * past its timer) and all system resources are released. SUSPEND is *
38 * requested by the kernel and will be enforced upon all open devices. */
39
40#define KGSL_STATE_NONE 0x00000000
41#define KGSL_STATE_INIT 0x00000001
42#define KGSL_STATE_ACTIVE 0x00000002
43#define KGSL_STATE_NAP 0x00000004
44#define KGSL_STATE_SLEEP 0x00000008
45#define KGSL_STATE_SUSPEND 0x00000010
46#define KGSL_STATE_HUNG 0x00000020
47#define KGSL_STATE_DUMP_AND_RECOVER 0x00000040
Suman Tatiraju24569022011-10-27 11:11:12 -070048#define KGSL_STATE_SLUMBER 0x00000080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049
50#define KGSL_GRAPHICS_MEMORY_LOW_WATERMARK 0x1000000
51
52#define KGSL_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK)))
53
54struct kgsl_device;
55struct platform_device;
56struct kgsl_device_private;
57struct kgsl_context;
58struct kgsl_power_stats;
59
60struct kgsl_functable {
61 /* Mandatory functions - these functions must be implemented
62 by the client device. The driver will not check for a NULL
63 pointer before calling the hook.
64 */
65 void (*regread) (struct kgsl_device *device,
66 unsigned int offsetwords, unsigned int *value);
67 void (*regwrite) (struct kgsl_device *device,
68 unsigned int offsetwords, unsigned int value);
69 int (*idle) (struct kgsl_device *device, unsigned int timeout);
70 unsigned int (*isidle) (struct kgsl_device *device);
71 int (*suspend_context) (struct kgsl_device *device);
72 int (*start) (struct kgsl_device *device, unsigned int init_ram);
73 int (*stop) (struct kgsl_device *device);
74 int (*getproperty) (struct kgsl_device *device,
75 enum kgsl_property_type type, void *value,
76 unsigned int sizebytes);
77 int (*waittimestamp) (struct kgsl_device *device,
78 unsigned int timestamp, unsigned int msecs);
79 unsigned int (*readtimestamp) (struct kgsl_device *device,
80 enum kgsl_timestamp_type type);
81 int (*issueibcmds) (struct kgsl_device_private *dev_priv,
82 struct kgsl_context *context, struct kgsl_ibdesc *ibdesc,
83 unsigned int sizedwords, uint32_t *timestamp,
84 unsigned int flags);
85 int (*setup_pt)(struct kgsl_device *device,
86 struct kgsl_pagetable *pagetable);
Jordan Crouse9f739212011-07-28 08:37:57 -060087 void (*cleanup_pt)(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088 struct kgsl_pagetable *pagetable);
89 void (*power_stats)(struct kgsl_device *device,
90 struct kgsl_power_stats *stats);
91 void (*irqctrl)(struct kgsl_device *device, int state);
Jordan Crousea0758f22011-12-07 11:19:22 -070092 unsigned int (*gpuid)(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070093 /* Optional functions - these functions are not mandatory. The
94 driver will check that the function pointer is not NULL before
95 calling the hook */
96 void (*setstate) (struct kgsl_device *device, uint32_t flags);
97 int (*drawctxt_create) (struct kgsl_device *device,
98 struct kgsl_pagetable *pagetable, struct kgsl_context *context,
99 uint32_t flags);
100 void (*drawctxt_destroy) (struct kgsl_device *device,
101 struct kgsl_context *context);
102 long (*ioctl) (struct kgsl_device_private *dev_priv,
103 unsigned int cmd, void *data);
104};
105
106struct kgsl_memregion {
107 unsigned char *mmio_virt_base;
108 unsigned int mmio_phys_base;
109 uint32_t gpu_base;
110 unsigned int sizebytes;
111};
112
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600113/* MH register values */
114struct kgsl_mh {
115 unsigned int mharb;
116 unsigned int mh_intf_cfg1;
117 unsigned int mh_intf_cfg2;
118 uint32_t mpu_base;
119 int mpu_range;
120};
121
Jordan Croused4bc9d22011-11-17 13:39:21 -0700122struct kgsl_event {
123 uint32_t timestamp;
124 void (*func)(struct kgsl_device *, void *, u32);
125 void *priv;
126 struct list_head list;
127};
128
129
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130struct kgsl_device {
131 struct device *dev;
132 const char *name;
133 unsigned int ver_major;
134 unsigned int ver_minor;
135 uint32_t flags;
136 enum kgsl_deviceid id;
137 struct kgsl_memregion regspace;
138 struct kgsl_memdesc memstore;
139 const char *iomemname;
140
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600141 struct kgsl_mh mh;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 struct kgsl_mmu mmu;
143 struct completion hwaccess_gate;
144 const struct kgsl_functable *ftbl;
145 struct work_struct idle_check_ws;
146 struct timer_list idle_timer;
147 struct kgsl_pwrctrl pwrctrl;
148 int open_count;
149
150 struct atomic_notifier_head ts_notifier_list;
151 struct mutex mutex;
152 uint32_t state;
153 uint32_t requested_state;
154
155 struct list_head memqueue;
156 unsigned int active_cnt;
157 struct completion suspend_gate;
158
159 wait_queue_head_t wait_queue;
160 struct workqueue_struct *work_queue;
161 struct device *parentdev;
162 struct completion recovery_gate;
163 struct dentry *d_debugfs;
164 struct idr context_idr;
165 struct early_suspend display_off;
166
167 /* Logging levels */
168 int cmd_log;
169 int ctxt_log;
170 int drv_log;
171 int mem_log;
172 int pwr_log;
173 struct wake_lock idle_wakelock;
174 struct kgsl_pwrscale pwrscale;
175 struct kobject pwrscale_kobj;
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600176 struct work_struct ts_expired_ws;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700177 struct list_head events;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700178 s64 on_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179};
180
181struct kgsl_context {
182 uint32_t id;
183
184 /* Pointer to the owning device instance */
185 struct kgsl_device_private *dev_priv;
186
187 /* Pointer to the device specific context information */
188 void *devctxt;
189};
190
191struct kgsl_process_private {
192 unsigned int refcnt;
193 pid_t pid;
194 spinlock_t mem_lock;
195 struct list_head mem_list;
196 struct kgsl_pagetable *pagetable;
197 struct list_head list;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600198 struct kobject kobj;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199
200 struct {
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600201 unsigned int cur;
202 unsigned int max;
203 } stats[KGSL_MEM_ENTRY_MAX];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204};
205
206struct kgsl_device_private {
207 struct kgsl_device *device;
208 struct kgsl_process_private *process_priv;
209};
210
211struct kgsl_power_stats {
212 s64 total_time;
213 s64 busy_time;
214};
215
216struct kgsl_device *kgsl_get_device(int dev_idx);
217
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600218static inline void kgsl_process_add_stats(struct kgsl_process_private *priv,
219 unsigned int type, size_t size)
220{
221 priv->stats[type].cur += size;
222 if (priv->stats[type].max < priv->stats[type].cur)
223 priv->stats[type].max = priv->stats[type].cur;
224}
225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226static inline void kgsl_regread(struct kgsl_device *device,
227 unsigned int offsetwords,
228 unsigned int *value)
229{
230 device->ftbl->regread(device, offsetwords, value);
231}
232
233static inline void kgsl_regwrite(struct kgsl_device *device,
234 unsigned int offsetwords,
235 unsigned int value)
236{
237 device->ftbl->regwrite(device, offsetwords, value);
238}
239
240static inline int kgsl_idle(struct kgsl_device *device, unsigned int timeout)
241{
242 return device->ftbl->idle(device, timeout);
243}
244
Jordan Crousea0758f22011-12-07 11:19:22 -0700245static inline unsigned int kgsl_gpuid(struct kgsl_device *device)
246{
247 return device->ftbl->gpuid(device);
248}
249
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250static inline int kgsl_create_device_sysfs_files(struct device *root,
251 const struct device_attribute **list)
252{
253 int ret = 0, i;
254 for (i = 0; list[i] != NULL; i++)
255 ret |= device_create_file(root, list[i]);
256 return ret;
257}
258
259static inline void kgsl_remove_device_sysfs_files(struct device *root,
260 const struct device_attribute **list)
261{
262 int i;
263 for (i = 0; list[i] != NULL; i++)
264 device_remove_file(root, list[i]);
265}
266
267static inline struct kgsl_mmu *
268kgsl_get_mmu(struct kgsl_device *device)
269{
270 return (struct kgsl_mmu *) (device ? &device->mmu : NULL);
271}
272
273static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev)
274{
275 int i;
276
277 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
278 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev)
279 return kgsl_driver.devp[i];
280 }
281
282 return NULL;
283}
284
285static inline int kgsl_create_device_workqueue(struct kgsl_device *device)
286{
Steve Muckle3e042df2011-11-18 11:25:04 -0800287 device->work_queue = create_singlethread_workqueue(device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 if (!device->work_queue) {
Steve Muckle3e042df2011-11-18 11:25:04 -0800289 KGSL_DRV_ERR(device,
290 "create_singlethread_workqueue(%s) failed\n",
291 device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 return -EINVAL;
293 }
294 return 0;
295}
296
297static inline struct kgsl_context *
298kgsl_find_context(struct kgsl_device_private *dev_priv, uint32_t id)
299{
300 struct kgsl_context *ctxt =
301 idr_find(&dev_priv->device->context_idr, id);
302
303 /* Make sure that the context belongs to the current instance so
304 that other processes can't guess context IDs and mess things up */
305
306 return (ctxt && ctxt->dev_priv == dev_priv) ? ctxt : NULL;
307}
308
309int kgsl_check_timestamp(struct kgsl_device *device, unsigned int timestamp);
310
311int kgsl_register_ts_notifier(struct kgsl_device *device,
312 struct notifier_block *nb);
313
314int kgsl_unregister_ts_notifier(struct kgsl_device *device,
315 struct notifier_block *nb);
316
317int kgsl_device_platform_probe(struct kgsl_device *device,
318 irqreturn_t (*dev_isr) (int, void*));
319void kgsl_device_platform_remove(struct kgsl_device *device);
320
321#endif /* __KGSL_DEVICE_H */