blob: 2a2e9169ac47d2df073b26ddf791abe9f7ac8c3b [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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070017#include <linux/pm_qos.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
Tarun Karra3335f142012-06-19 14:11:48 -070028#define KGSL_TIMEOUT_PART 2000 /* 2 sec */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029
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);
Jordan Crousea29a2e02012-08-14 09:09:23 -060070 int (*idle) (struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071 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,
Carter Cooper7e7f02e2012-02-15 09:36:31 -070079 struct kgsl_context *context, unsigned int timestamp,
80 unsigned int msecs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081 unsigned int (*readtimestamp) (struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -070082 struct kgsl_context *context, enum kgsl_timestamp_type type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 int (*issueibcmds) (struct kgsl_device_private *dev_priv,
84 struct kgsl_context *context, struct kgsl_ibdesc *ibdesc,
85 unsigned int sizedwords, uint32_t *timestamp,
86 unsigned int flags);
87 int (*setup_pt)(struct kgsl_device *device,
88 struct kgsl_pagetable *pagetable);
Jordan Crouse9f739212011-07-28 08:37:57 -060089 void (*cleanup_pt)(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 struct kgsl_pagetable *pagetable);
91 void (*power_stats)(struct kgsl_device *device,
92 struct kgsl_power_stats *stats);
93 void (*irqctrl)(struct kgsl_device *device, int state);
Jordan Croused6535882012-06-20 08:22:16 -060094 unsigned int (*gpuid)(struct kgsl_device *device, unsigned int *chipid);
Jordan Crouse156cfbc2012-01-24 09:32:04 -070095 void * (*snapshot)(struct kgsl_device *device, void *snapshot,
96 int *remain, int hang);
Jordan Crouseb368e9b2012-04-27 14:01:59 -060097 irqreturn_t (*irq_handler)(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098 /* Optional functions - these functions are not mandatory. The
99 driver will check that the function pointer is not NULL before
100 calling the hook */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600101 void (*setstate) (struct kgsl_device *device, unsigned int context_id,
102 uint32_t flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700103 int (*drawctxt_create) (struct kgsl_device *device,
104 struct kgsl_pagetable *pagetable, struct kgsl_context *context,
105 uint32_t flags);
106 void (*drawctxt_destroy) (struct kgsl_device *device,
107 struct kgsl_context *context);
108 long (*ioctl) (struct kgsl_device_private *dev_priv,
109 unsigned int cmd, void *data);
Jordan Crouseed7dd7f2012-03-29 13:16:02 -0600110 int (*setproperty) (struct kgsl_device *device,
111 enum kgsl_property_type type, void *value,
112 unsigned int sizebytes);
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -0600113 int (*postmortem_dump) (struct kgsl_device *device, int manual);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700114};
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 {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700126 struct kgsl_context *context;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700127 uint32_t timestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700128 void (*func)(struct kgsl_device *, void *, u32, u32);
Jordan Croused4bc9d22011-11-17 13:39:21 -0700129 void *priv;
130 struct list_head list;
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600131 void *owner;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700132};
133
134
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135struct kgsl_device {
136 struct device *dev;
137 const char *name;
138 unsigned int ver_major;
139 unsigned int ver_minor;
140 uint32_t flags;
141 enum kgsl_deviceid id;
Jordan Crouse7501d452012-04-19 08:58:44 -0600142 unsigned long reg_phys;
143 void *reg_virt;
144 unsigned int reg_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 struct kgsl_memdesc memstore;
146 const char *iomemname;
147
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600148 struct kgsl_mh mh;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 struct kgsl_mmu mmu;
150 struct completion hwaccess_gate;
151 const struct kgsl_functable *ftbl;
152 struct work_struct idle_check_ws;
153 struct timer_list idle_timer;
154 struct kgsl_pwrctrl pwrctrl;
155 int open_count;
156
157 struct atomic_notifier_head ts_notifier_list;
158 struct mutex mutex;
159 uint32_t state;
160 uint32_t requested_state;
161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 unsigned int active_cnt;
163 struct completion suspend_gate;
164
165 wait_queue_head_t wait_queue;
166 struct workqueue_struct *work_queue;
167 struct device *parentdev;
168 struct completion recovery_gate;
169 struct dentry *d_debugfs;
170 struct idr context_idr;
171 struct early_suspend display_off;
172
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700173 void *snapshot; /* Pointer to the snapshot memory region */
174 int snapshot_maxsize; /* Max size of the snapshot region */
175 int snapshot_size; /* Current size of the snapshot region */
176 u32 snapshot_timestamp; /* Timestamp of the last valid snapshot */
177 int snapshot_frozen; /* 1 if the snapshot output is frozen until
178 it gets read by the user. This avoids
179 losing the output on multiple hangs */
180 struct kobject snapshot_kobj;
181
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600182 /*
183 * List of GPU buffers that have been frozen in memory until they can be
184 * dumped
185 */
186 struct list_head snapshot_obj_list;
187
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 /* Logging levels */
189 int cmd_log;
190 int ctxt_log;
191 int drv_log;
192 int mem_log;
193 int pwr_log;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 struct kgsl_pwrscale pwrscale;
195 struct kobject pwrscale_kobj;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700196 struct pm_qos_request pm_qos_req_dma;
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600197 struct work_struct ts_expired_ws;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700198 struct list_head events;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700199 s64 on_time;
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -0600200
201 /* Postmortem Control switches */
202 int pm_regs_enabled;
203 int pm_ib_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204};
205
Jeremy Gebben84d75d02012-03-01 14:47:45 -0700206void kgsl_timestamp_expired(struct work_struct *work);
207
208#define KGSL_DEVICE_COMMON_INIT(_dev) \
209 .hwaccess_gate = COMPLETION_INITIALIZER((_dev).hwaccess_gate),\
210 .suspend_gate = COMPLETION_INITIALIZER((_dev).suspend_gate),\
211 .recovery_gate = COMPLETION_INITIALIZER((_dev).recovery_gate),\
212 .ts_notifier_list = ATOMIC_NOTIFIER_INIT((_dev).ts_notifier_list),\
213 .idle_check_ws = __WORK_INITIALIZER((_dev).idle_check_ws,\
214 kgsl_idle_check),\
215 .ts_expired_ws = __WORK_INITIALIZER((_dev).ts_expired_ws,\
216 kgsl_timestamp_expired),\
217 .context_idr = IDR_INIT((_dev).context_idr),\
218 .events = LIST_HEAD_INIT((_dev).events),\
219 .wait_queue = __WAIT_QUEUE_HEAD_INITIALIZER((_dev).wait_queue),\
220 .mutex = __MUTEX_INITIALIZER((_dev).mutex),\
221 .state = KGSL_STATE_INIT,\
222 .ver_major = DRIVER_VERSION_MAJOR,\
Carter Cooper12481fd2012-06-05 11:18:34 -0600223 .ver_minor = DRIVER_VERSION_MINOR
Jeremy Gebben84d75d02012-03-01 14:47:45 -0700224
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225struct kgsl_context {
Jeremy Gebben9ad86922012-05-08 15:33:23 -0600226 struct kref refcount;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 uint32_t id;
228
229 /* Pointer to the owning device instance */
230 struct kgsl_device_private *dev_priv;
231
232 /* Pointer to the device specific context information */
233 void *devctxt;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700234 /*
235 * Status indicating whether a gpu reset occurred and whether this
236 * context was responsible for causing it
237 */
238 unsigned int reset_status;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239};
240
241struct kgsl_process_private {
242 unsigned int refcnt;
243 pid_t pid;
244 spinlock_t mem_lock;
Jordan Crousec9559e42012-04-05 16:55:56 -0600245 struct rb_root mem_rb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 struct kgsl_pagetable *pagetable;
247 struct list_head list;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600248 struct kobject kobj;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249
250 struct {
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600251 unsigned int cur;
252 unsigned int max;
253 } stats[KGSL_MEM_ENTRY_MAX];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254};
255
256struct kgsl_device_private {
257 struct kgsl_device *device;
258 struct kgsl_process_private *process_priv;
259};
260
261struct kgsl_power_stats {
262 s64 total_time;
263 s64 busy_time;
264};
265
266struct kgsl_device *kgsl_get_device(int dev_idx);
267
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600268static inline void kgsl_process_add_stats(struct kgsl_process_private *priv,
269 unsigned int type, size_t size)
270{
271 priv->stats[type].cur += size;
272 if (priv->stats[type].max < priv->stats[type].cur)
273 priv->stats[type].max = priv->stats[type].cur;
274}
275
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276static inline void kgsl_regread(struct kgsl_device *device,
277 unsigned int offsetwords,
278 unsigned int *value)
279{
280 device->ftbl->regread(device, offsetwords, value);
281}
282
283static inline void kgsl_regwrite(struct kgsl_device *device,
284 unsigned int offsetwords,
285 unsigned int value)
286{
287 device->ftbl->regwrite(device, offsetwords, value);
288}
289
Jordan Crousea29a2e02012-08-14 09:09:23 -0600290static inline int kgsl_idle(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291{
Jordan Crousea29a2e02012-08-14 09:09:23 -0600292 return device->ftbl->idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293}
294
Jordan Croused6535882012-06-20 08:22:16 -0600295static inline unsigned int kgsl_gpuid(struct kgsl_device *device,
296 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -0700297{
Jordan Croused6535882012-06-20 08:22:16 -0600298 return device->ftbl->gpuid(device, chipid);
Jordan Crousea0758f22011-12-07 11:19:22 -0700299}
300
Jeremy Gebben731dac52012-05-10 11:13:42 -0600301static inline unsigned int kgsl_readtimestamp(struct kgsl_device *device,
302 struct kgsl_context *context,
303 enum kgsl_timestamp_type type)
304{
305 return device->ftbl->readtimestamp(device, context, type);
306}
307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308static inline int kgsl_create_device_sysfs_files(struct device *root,
309 const struct device_attribute **list)
310{
311 int ret = 0, i;
312 for (i = 0; list[i] != NULL; i++)
313 ret |= device_create_file(root, list[i]);
314 return ret;
315}
316
317static inline void kgsl_remove_device_sysfs_files(struct device *root,
318 const struct device_attribute **list)
319{
320 int i;
321 for (i = 0; list[i] != NULL; i++)
322 device_remove_file(root, list[i]);
323}
324
325static inline struct kgsl_mmu *
326kgsl_get_mmu(struct kgsl_device *device)
327{
328 return (struct kgsl_mmu *) (device ? &device->mmu : NULL);
329}
330
331static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev)
332{
333 int i;
334
335 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
336 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev)
337 return kgsl_driver.devp[i];
338 }
339
340 return NULL;
341}
342
343static inline int kgsl_create_device_workqueue(struct kgsl_device *device)
344{
Steve Muckle3e042df2011-11-18 11:25:04 -0800345 device->work_queue = create_singlethread_workqueue(device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700346 if (!device->work_queue) {
Steve Muckle3e042df2011-11-18 11:25:04 -0800347 KGSL_DRV_ERR(device,
348 "create_singlethread_workqueue(%s) failed\n",
349 device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 return -EINVAL;
351 }
352 return 0;
353}
354
355static inline struct kgsl_context *
356kgsl_find_context(struct kgsl_device_private *dev_priv, uint32_t id)
357{
358 struct kgsl_context *ctxt =
359 idr_find(&dev_priv->device->context_idr, id);
360
361 /* Make sure that the context belongs to the current instance so
362 that other processes can't guess context IDs and mess things up */
363
364 return (ctxt && ctxt->dev_priv == dev_priv) ? ctxt : NULL;
365}
366
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700367int kgsl_check_timestamp(struct kgsl_device *device,
368 struct kgsl_context *context, unsigned int timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369
370int kgsl_register_ts_notifier(struct kgsl_device *device,
371 struct notifier_block *nb);
372
373int kgsl_unregister_ts_notifier(struct kgsl_device *device,
374 struct notifier_block *nb);
375
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600376int kgsl_device_platform_probe(struct kgsl_device *device);
377
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700378void kgsl_device_platform_remove(struct kgsl_device *device);
379
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700380const char *kgsl_pwrstate_to_str(unsigned int state);
381
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700382int kgsl_device_snapshot_init(struct kgsl_device *device);
383int kgsl_device_snapshot(struct kgsl_device *device, int hang);
384void kgsl_device_snapshot_close(struct kgsl_device *device);
385
Jordan Croused8aa4be2012-04-18 09:31:07 -0600386static inline struct kgsl_device_platform_data *
387kgsl_device_get_drvdata(struct kgsl_device *dev)
388{
389 struct platform_device *pdev =
390 container_of(dev->parentdev, struct platform_device, dev);
391
392 return pdev->dev.platform_data;
393}
394
Jeremy Gebben9ad86922012-05-08 15:33:23 -0600395/**
396 * kgsl_context_get - Get context reference count
397 * @context
398 *
399 * Asynchronous code that holds a pointer to a context
400 * must hold a reference count on it. The kgsl device
401 * mutex must be held while the context reference count
402 * is changed.
403 */
404static inline void
405kgsl_context_get(struct kgsl_context *context)
406{
407 kref_get(&context->refcount);
408}
409
410void kgsl_context_destroy(struct kref *kref);
411
412/**
413 * kgsl_context_put - Release context reference count
414 * @context
415 *
416 */
417static inline void
418kgsl_context_put(struct kgsl_context *context)
419{
420 kref_put(&context->refcount, kgsl_context_destroy);
421}
422
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700423#endif /* __KGSL_DEVICE_H */