blob: d7a25a121fcb421ca07925c7c0b0ced289513777 [file] [log] [blame]
Jordan Crouse156cfbc2012-01-24 09:32:04 -07001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
Lucille Sylvester10297892012-02-27 13:54:47 -070018#include <linux/pm_qos_params.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <linux/earlysuspend.h>
20
21#include "kgsl.h"
22#include "kgsl_mmu.h"
23#include "kgsl_pwrctrl.h"
24#include "kgsl_log.h"
25#include "kgsl_pwrscale.h"
26
27#define KGSL_TIMEOUT_NONE 0
28#define KGSL_TIMEOUT_DEFAULT 0xFFFFFFFF
29
30#define FIRST_TIMEOUT (HZ / 2)
31
32
33/* KGSL device state is initialized to INIT when platform_probe *
34 * sucessfully initialized the device. Once a device has been opened *
35 * (started) it becomes active. NAP implies that only low latency *
36 * resources (for now clocks on some platforms) are off. SLEEP implies *
37 * that the KGSL module believes a device is idle (has been inactive *
38 * past its timer) and all system resources are released. SUSPEND is *
39 * requested by the kernel and will be enforced upon all open devices. */
40
41#define KGSL_STATE_NONE 0x00000000
42#define KGSL_STATE_INIT 0x00000001
43#define KGSL_STATE_ACTIVE 0x00000002
44#define KGSL_STATE_NAP 0x00000004
45#define KGSL_STATE_SLEEP 0x00000008
46#define KGSL_STATE_SUSPEND 0x00000010
47#define KGSL_STATE_HUNG 0x00000020
48#define KGSL_STATE_DUMP_AND_RECOVER 0x00000040
Suman Tatiraju24569022011-10-27 11:11:12 -070049#define KGSL_STATE_SLUMBER 0x00000080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
51#define KGSL_GRAPHICS_MEMORY_LOW_WATERMARK 0x1000000
52
53#define KGSL_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK)))
54
55struct kgsl_device;
56struct platform_device;
57struct kgsl_device_private;
58struct kgsl_context;
59struct kgsl_power_stats;
60
61struct kgsl_functable {
62 /* Mandatory functions - these functions must be implemented
63 by the client device. The driver will not check for a NULL
64 pointer before calling the hook.
65 */
66 void (*regread) (struct kgsl_device *device,
67 unsigned int offsetwords, unsigned int *value);
68 void (*regwrite) (struct kgsl_device *device,
69 unsigned int offsetwords, unsigned int value);
70 int (*idle) (struct kgsl_device *device, unsigned int timeout);
71 unsigned int (*isidle) (struct kgsl_device *device);
72 int (*suspend_context) (struct kgsl_device *device);
73 int (*start) (struct kgsl_device *device, unsigned int init_ram);
74 int (*stop) (struct kgsl_device *device);
75 int (*getproperty) (struct kgsl_device *device,
76 enum kgsl_property_type type, void *value,
77 unsigned int sizebytes);
78 int (*waittimestamp) (struct kgsl_device *device,
79 unsigned int timestamp, unsigned int msecs);
80 unsigned int (*readtimestamp) (struct kgsl_device *device,
81 enum kgsl_timestamp_type type);
82 int (*issueibcmds) (struct kgsl_device_private *dev_priv,
83 struct kgsl_context *context, struct kgsl_ibdesc *ibdesc,
84 unsigned int sizedwords, uint32_t *timestamp,
85 unsigned int flags);
86 int (*setup_pt)(struct kgsl_device *device,
87 struct kgsl_pagetable *pagetable);
Jordan Crouse9f739212011-07-28 08:37:57 -060088 void (*cleanup_pt)(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070089 struct kgsl_pagetable *pagetable);
90 void (*power_stats)(struct kgsl_device *device,
91 struct kgsl_power_stats *stats);
92 void (*irqctrl)(struct kgsl_device *device, int state);
Jordan Crousea0758f22011-12-07 11:19:22 -070093 unsigned int (*gpuid)(struct kgsl_device *device);
Jordan Crouse156cfbc2012-01-24 09:32:04 -070094 void * (*snapshot)(struct kgsl_device *device, void *snapshot,
95 int *remain, int hang);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 /* Optional functions - these functions are not mandatory. The
97 driver will check that the function pointer is not NULL before
98 calling the hook */
99 void (*setstate) (struct kgsl_device *device, uint32_t flags);
100 int (*drawctxt_create) (struct kgsl_device *device,
101 struct kgsl_pagetable *pagetable, struct kgsl_context *context,
102 uint32_t flags);
103 void (*drawctxt_destroy) (struct kgsl_device *device,
104 struct kgsl_context *context);
105 long (*ioctl) (struct kgsl_device_private *dev_priv,
106 unsigned int cmd, void *data);
107};
108
109struct kgsl_memregion {
110 unsigned char *mmio_virt_base;
111 unsigned int mmio_phys_base;
112 uint32_t gpu_base;
113 unsigned int sizebytes;
114};
115
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600116/* MH register values */
117struct kgsl_mh {
118 unsigned int mharb;
119 unsigned int mh_intf_cfg1;
120 unsigned int mh_intf_cfg2;
121 uint32_t mpu_base;
122 int mpu_range;
123};
124
Jordan Croused4bc9d22011-11-17 13:39:21 -0700125struct kgsl_event {
126 uint32_t timestamp;
127 void (*func)(struct kgsl_device *, void *, u32);
128 void *priv;
129 struct list_head list;
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -0700130 struct kgsl_device_private *owner;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700131};
132
133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134struct kgsl_device {
135 struct device *dev;
136 const char *name;
137 unsigned int ver_major;
138 unsigned int ver_minor;
139 uint32_t flags;
140 enum kgsl_deviceid id;
141 struct kgsl_memregion regspace;
142 struct kgsl_memdesc memstore;
143 const char *iomemname;
144
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600145 struct kgsl_mh mh;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 struct kgsl_mmu mmu;
147 struct completion hwaccess_gate;
148 const struct kgsl_functable *ftbl;
149 struct work_struct idle_check_ws;
150 struct timer_list idle_timer;
151 struct kgsl_pwrctrl pwrctrl;
152 int open_count;
153
154 struct atomic_notifier_head ts_notifier_list;
155 struct mutex mutex;
156 uint32_t state;
157 uint32_t requested_state;
158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 unsigned int active_cnt;
160 struct completion suspend_gate;
161
162 wait_queue_head_t wait_queue;
163 struct workqueue_struct *work_queue;
164 struct device *parentdev;
165 struct completion recovery_gate;
166 struct dentry *d_debugfs;
167 struct idr context_idr;
168 struct early_suspend display_off;
169
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700170 void *snapshot; /* Pointer to the snapshot memory region */
171 int snapshot_maxsize; /* Max size of the snapshot region */
172 int snapshot_size; /* Current size of the snapshot region */
173 u32 snapshot_timestamp; /* Timestamp of the last valid snapshot */
174 int snapshot_frozen; /* 1 if the snapshot output is frozen until
175 it gets read by the user. This avoids
176 losing the output on multiple hangs */
177 struct kobject snapshot_kobj;
178
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 /* Logging levels */
180 int cmd_log;
181 int ctxt_log;
182 int drv_log;
183 int mem_log;
184 int pwr_log;
185 struct wake_lock idle_wakelock;
186 struct kgsl_pwrscale pwrscale;
187 struct kobject pwrscale_kobj;
Lucille Sylvester10297892012-02-27 13:54:47 -0700188 struct pm_qos_request_list pm_qos_req_dma;
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600189 struct work_struct ts_expired_ws;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700190 struct list_head events;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700191 s64 on_time;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192};
193
194struct kgsl_context {
195 uint32_t id;
196
197 /* Pointer to the owning device instance */
198 struct kgsl_device_private *dev_priv;
199
200 /* Pointer to the device specific context information */
201 void *devctxt;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700202 /*
203 * Status indicating whether a gpu reset occurred and whether this
204 * context was responsible for causing it
205 */
206 unsigned int reset_status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207};
208
209struct kgsl_process_private {
210 unsigned int refcnt;
211 pid_t pid;
212 spinlock_t mem_lock;
213 struct list_head mem_list;
214 struct kgsl_pagetable *pagetable;
215 struct list_head list;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600216 struct kobject kobj;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217
218 struct {
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600219 unsigned int cur;
220 unsigned int max;
221 } stats[KGSL_MEM_ENTRY_MAX];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222};
223
224struct kgsl_device_private {
225 struct kgsl_device *device;
226 struct kgsl_process_private *process_priv;
227};
228
229struct kgsl_power_stats {
230 s64 total_time;
231 s64 busy_time;
232};
233
234struct kgsl_device *kgsl_get_device(int dev_idx);
235
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600236static inline void kgsl_process_add_stats(struct kgsl_process_private *priv,
237 unsigned int type, size_t size)
238{
239 priv->stats[type].cur += size;
240 if (priv->stats[type].max < priv->stats[type].cur)
241 priv->stats[type].max = priv->stats[type].cur;
242}
243
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244static inline void kgsl_regread(struct kgsl_device *device,
245 unsigned int offsetwords,
246 unsigned int *value)
247{
248 device->ftbl->regread(device, offsetwords, value);
249}
250
251static inline void kgsl_regwrite(struct kgsl_device *device,
252 unsigned int offsetwords,
253 unsigned int value)
254{
255 device->ftbl->regwrite(device, offsetwords, value);
256}
257
258static inline int kgsl_idle(struct kgsl_device *device, unsigned int timeout)
259{
260 return device->ftbl->idle(device, timeout);
261}
262
Jordan Crousea0758f22011-12-07 11:19:22 -0700263static inline unsigned int kgsl_gpuid(struct kgsl_device *device)
264{
265 return device->ftbl->gpuid(device);
266}
267
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268static inline int kgsl_create_device_sysfs_files(struct device *root,
269 const struct device_attribute **list)
270{
271 int ret = 0, i;
272 for (i = 0; list[i] != NULL; i++)
273 ret |= device_create_file(root, list[i]);
274 return ret;
275}
276
277static inline void kgsl_remove_device_sysfs_files(struct device *root,
278 const struct device_attribute **list)
279{
280 int i;
281 for (i = 0; list[i] != NULL; i++)
282 device_remove_file(root, list[i]);
283}
284
285static inline struct kgsl_mmu *
286kgsl_get_mmu(struct kgsl_device *device)
287{
288 return (struct kgsl_mmu *) (device ? &device->mmu : NULL);
289}
290
291static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev)
292{
293 int i;
294
295 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
296 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev)
297 return kgsl_driver.devp[i];
298 }
299
300 return NULL;
301}
302
303static inline int kgsl_create_device_workqueue(struct kgsl_device *device)
304{
Steve Muckle3e042df2011-11-18 11:25:04 -0800305 device->work_queue = create_singlethread_workqueue(device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 if (!device->work_queue) {
Steve Muckle3e042df2011-11-18 11:25:04 -0800307 KGSL_DRV_ERR(device,
308 "create_singlethread_workqueue(%s) failed\n",
309 device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 return -EINVAL;
311 }
312 return 0;
313}
314
315static inline struct kgsl_context *
316kgsl_find_context(struct kgsl_device_private *dev_priv, uint32_t id)
317{
318 struct kgsl_context *ctxt =
319 idr_find(&dev_priv->device->context_idr, id);
320
321 /* Make sure that the context belongs to the current instance so
322 that other processes can't guess context IDs and mess things up */
323
324 return (ctxt && ctxt->dev_priv == dev_priv) ? ctxt : NULL;
325}
326
327int kgsl_check_timestamp(struct kgsl_device *device, unsigned int timestamp);
328
329int kgsl_register_ts_notifier(struct kgsl_device *device,
330 struct notifier_block *nb);
331
332int kgsl_unregister_ts_notifier(struct kgsl_device *device,
333 struct notifier_block *nb);
334
335int kgsl_device_platform_probe(struct kgsl_device *device,
336 irqreturn_t (*dev_isr) (int, void*));
337void kgsl_device_platform_remove(struct kgsl_device *device);
338
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700339const char *kgsl_pwrstate_to_str(unsigned int state);
340
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700341int kgsl_device_snapshot_init(struct kgsl_device *device);
342int kgsl_device_snapshot(struct kgsl_device *device, int hang);
343void kgsl_device_snapshot_close(struct kgsl_device *device);
344
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700345#endif /* __KGSL_DEVICE_H */