blob: 24eb630af6c2d4a95e5d1631fbaf19a8a86fbcb7 [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"
Jeff Boodyfe6c39c2012-08-09 13:54:50 -060025#include <linux/sync.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026
27#define KGSL_TIMEOUT_NONE 0
28#define KGSL_TIMEOUT_DEFAULT 0xFFFFFFFF
Tarun Karra3335f142012-06-19 14:11:48 -070029#define KGSL_TIMEOUT_PART 2000 /* 2 sec */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31#define FIRST_TIMEOUT (HZ / 2)
32
33
34/* KGSL device state is initialized to INIT when platform_probe *
35 * sucessfully initialized the device. Once a device has been opened *
36 * (started) it becomes active. NAP implies that only low latency *
37 * resources (for now clocks on some platforms) are off. SLEEP implies *
38 * that the KGSL module believes a device is idle (has been inactive *
39 * past its timer) and all system resources are released. SUSPEND is *
40 * requested by the kernel and will be enforced upon all open devices. */
41
42#define KGSL_STATE_NONE 0x00000000
43#define KGSL_STATE_INIT 0x00000001
44#define KGSL_STATE_ACTIVE 0x00000002
45#define KGSL_STATE_NAP 0x00000004
46#define KGSL_STATE_SLEEP 0x00000008
47#define KGSL_STATE_SUSPEND 0x00000010
48#define KGSL_STATE_HUNG 0x00000020
49#define KGSL_STATE_DUMP_AND_RECOVER 0x00000040
Suman Tatiraju24569022011-10-27 11:11:12 -070050#define KGSL_STATE_SLUMBER 0x00000080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
52#define KGSL_GRAPHICS_MEMORY_LOW_WATERMARK 0x1000000
53
54#define KGSL_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK)))
55
56struct kgsl_device;
57struct platform_device;
58struct kgsl_device_private;
59struct kgsl_context;
60struct kgsl_power_stats;
61
62struct kgsl_functable {
63 /* Mandatory functions - these functions must be implemented
64 by the client device. The driver will not check for a NULL
65 pointer before calling the hook.
66 */
67 void (*regread) (struct kgsl_device *device,
68 unsigned int offsetwords, unsigned int *value);
69 void (*regwrite) (struct kgsl_device *device,
70 unsigned int offsetwords, unsigned int value);
Jordan Crousea29a2e02012-08-14 09:09:23 -060071 int (*idle) (struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072 unsigned int (*isidle) (struct kgsl_device *device);
73 int (*suspend_context) (struct kgsl_device *device);
74 int (*start) (struct kgsl_device *device, unsigned int init_ram);
75 int (*stop) (struct kgsl_device *device);
76 int (*getproperty) (struct kgsl_device *device,
77 enum kgsl_property_type type, void *value,
78 unsigned int sizebytes);
79 int (*waittimestamp) (struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -070080 struct kgsl_context *context, unsigned int timestamp,
81 unsigned int msecs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082 unsigned int (*readtimestamp) (struct kgsl_device *device,
Carter Cooper7e7f02e2012-02-15 09:36:31 -070083 struct kgsl_context *context, enum kgsl_timestamp_type type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 int (*issueibcmds) (struct kgsl_device_private *dev_priv,
85 struct kgsl_context *context, struct kgsl_ibdesc *ibdesc,
86 unsigned int sizedwords, uint32_t *timestamp,
87 unsigned int flags);
88 int (*setup_pt)(struct kgsl_device *device,
89 struct kgsl_pagetable *pagetable);
Jordan Crouse9f739212011-07-28 08:37:57 -060090 void (*cleanup_pt)(struct kgsl_device *device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091 struct kgsl_pagetable *pagetable);
92 void (*power_stats)(struct kgsl_device *device,
93 struct kgsl_power_stats *stats);
94 void (*irqctrl)(struct kgsl_device *device, int state);
Jordan Croused6535882012-06-20 08:22:16 -060095 unsigned int (*gpuid)(struct kgsl_device *device, unsigned int *chipid);
Jordan Crouse156cfbc2012-01-24 09:32:04 -070096 void * (*snapshot)(struct kgsl_device *device, void *snapshot,
97 int *remain, int hang);
Jordan Crouseb368e9b2012-04-27 14:01:59 -060098 irqreturn_t (*irq_handler)(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099 /* Optional functions - these functions are not mandatory. The
100 driver will check that the function pointer is not NULL before
101 calling the hook */
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600102 void (*setstate) (struct kgsl_device *device, unsigned int context_id,
103 uint32_t flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 int (*drawctxt_create) (struct kgsl_device *device,
105 struct kgsl_pagetable *pagetable, struct kgsl_context *context,
106 uint32_t flags);
107 void (*drawctxt_destroy) (struct kgsl_device *device,
108 struct kgsl_context *context);
109 long (*ioctl) (struct kgsl_device_private *dev_priv,
110 unsigned int cmd, void *data);
Jordan Crouseed7dd7f2012-03-29 13:16:02 -0600111 int (*setproperty) (struct kgsl_device *device,
112 enum kgsl_property_type type, void *value,
113 unsigned int sizebytes);
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -0600114 int (*postmortem_dump) (struct kgsl_device *device, int manual);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700115};
116
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600117/* MH register values */
118struct kgsl_mh {
119 unsigned int mharb;
120 unsigned int mh_intf_cfg1;
121 unsigned int mh_intf_cfg2;
122 uint32_t mpu_base;
123 int mpu_range;
124};
125
Jordan Croused4bc9d22011-11-17 13:39:21 -0700126struct kgsl_event {
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700127 struct kgsl_context *context;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700128 uint32_t timestamp;
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700129 void (*func)(struct kgsl_device *, void *, u32, u32);
Jordan Croused4bc9d22011-11-17 13:39:21 -0700130 void *priv;
131 struct list_head list;
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600132 void *owner;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700133};
134
135
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136struct kgsl_device {
137 struct device *dev;
138 const char *name;
139 unsigned int ver_major;
140 unsigned int ver_minor;
141 uint32_t flags;
142 enum kgsl_deviceid id;
Jordan Crouse7501d452012-04-19 08:58:44 -0600143 unsigned long reg_phys;
144 void *reg_virt;
145 unsigned int reg_len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 struct kgsl_memdesc memstore;
147 const char *iomemname;
148
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600149 struct kgsl_mh mh;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700150 struct kgsl_mmu mmu;
151 struct completion hwaccess_gate;
152 const struct kgsl_functable *ftbl;
153 struct work_struct idle_check_ws;
154 struct timer_list idle_timer;
155 struct kgsl_pwrctrl pwrctrl;
156 int open_count;
157
158 struct atomic_notifier_head ts_notifier_list;
159 struct mutex mutex;
160 uint32_t state;
161 uint32_t requested_state;
162
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 unsigned int active_cnt;
164 struct completion suspend_gate;
165
166 wait_queue_head_t wait_queue;
167 struct workqueue_struct *work_queue;
168 struct device *parentdev;
169 struct completion recovery_gate;
170 struct dentry *d_debugfs;
171 struct idr context_idr;
172 struct early_suspend display_off;
173
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700174 void *snapshot; /* Pointer to the snapshot memory region */
175 int snapshot_maxsize; /* Max size of the snapshot region */
176 int snapshot_size; /* Current size of the snapshot region */
177 u32 snapshot_timestamp; /* Timestamp of the last valid snapshot */
178 int snapshot_frozen; /* 1 if the snapshot output is frozen until
179 it gets read by the user. This avoids
180 losing the output on multiple hangs */
181 struct kobject snapshot_kobj;
182
Jordan Crouse9610b6b2012-03-16 14:53:42 -0600183 /*
184 * List of GPU buffers that have been frozen in memory until they can be
185 * dumped
186 */
187 struct list_head snapshot_obj_list;
188
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189 /* Logging levels */
190 int cmd_log;
191 int ctxt_log;
192 int drv_log;
193 int mem_log;
194 int pwr_log;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 struct kgsl_pwrscale pwrscale;
196 struct kobject pwrscale_kobj;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700197 struct pm_qos_request pm_qos_req_dma;
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600198 struct work_struct ts_expired_ws;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700199 struct list_head events;
Lucille Sylvester808eca22011-11-03 10:26:29 -0700200 s64 on_time;
Harsh Vardhan Dwivedi715fb832012-05-18 00:24:18 -0600201
202 /* Postmortem Control switches */
203 int pm_regs_enabled;
204 int pm_ib_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205};
206
Jeremy Gebben84d75d02012-03-01 14:47:45 -0700207void kgsl_timestamp_expired(struct work_struct *work);
208
209#define KGSL_DEVICE_COMMON_INIT(_dev) \
210 .hwaccess_gate = COMPLETION_INITIALIZER((_dev).hwaccess_gate),\
211 .suspend_gate = COMPLETION_INITIALIZER((_dev).suspend_gate),\
212 .recovery_gate = COMPLETION_INITIALIZER((_dev).recovery_gate),\
213 .ts_notifier_list = ATOMIC_NOTIFIER_INIT((_dev).ts_notifier_list),\
214 .idle_check_ws = __WORK_INITIALIZER((_dev).idle_check_ws,\
215 kgsl_idle_check),\
216 .ts_expired_ws = __WORK_INITIALIZER((_dev).ts_expired_ws,\
217 kgsl_timestamp_expired),\
218 .context_idr = IDR_INIT((_dev).context_idr),\
219 .events = LIST_HEAD_INIT((_dev).events),\
220 .wait_queue = __WAIT_QUEUE_HEAD_INITIALIZER((_dev).wait_queue),\
221 .mutex = __MUTEX_INITIALIZER((_dev).mutex),\
222 .state = KGSL_STATE_INIT,\
223 .ver_major = DRIVER_VERSION_MAJOR,\
Carter Cooper12481fd2012-06-05 11:18:34 -0600224 .ver_minor = DRIVER_VERSION_MINOR
Jeremy Gebben84d75d02012-03-01 14:47:45 -0700225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226struct kgsl_context {
Jeremy Gebben9ad86922012-05-08 15:33:23 -0600227 struct kref refcount;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 uint32_t id;
229
230 /* Pointer to the owning device instance */
231 struct kgsl_device_private *dev_priv;
232
233 /* Pointer to the device specific context information */
234 void *devctxt;
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700235 /*
236 * Status indicating whether a gpu reset occurred and whether this
237 * context was responsible for causing it
238 */
239 unsigned int reset_status;
Jeff Boodyfe6c39c2012-08-09 13:54:50 -0600240
241 /*
242 * Timeline used to create fences that can be signaled when a
243 * sync_pt timestamp expires.
244 */
245 struct sync_timeline *timeline;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246};
247
248struct kgsl_process_private {
249 unsigned int refcnt;
250 pid_t pid;
251 spinlock_t mem_lock;
Jordan Crousec9559e42012-04-05 16:55:56 -0600252 struct rb_root mem_rb;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253 struct kgsl_pagetable *pagetable;
254 struct list_head list;
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600255 struct kobject kobj;
Jeremy Gebbenddf93012012-09-25 10:57:38 -0600256 struct dentry *debug_root;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
258 struct {
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600259 unsigned int cur;
260 unsigned int max;
261 } stats[KGSL_MEM_ENTRY_MAX];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262};
263
264struct kgsl_device_private {
265 struct kgsl_device *device;
266 struct kgsl_process_private *process_priv;
267};
268
269struct kgsl_power_stats {
270 s64 total_time;
271 s64 busy_time;
272};
273
274struct kgsl_device *kgsl_get_device(int dev_idx);
275
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600276static inline void kgsl_process_add_stats(struct kgsl_process_private *priv,
277 unsigned int type, size_t size)
278{
279 priv->stats[type].cur += size;
280 if (priv->stats[type].max < priv->stats[type].cur)
281 priv->stats[type].max = priv->stats[type].cur;
282}
283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284static inline void kgsl_regread(struct kgsl_device *device,
285 unsigned int offsetwords,
286 unsigned int *value)
287{
288 device->ftbl->regread(device, offsetwords, value);
289}
290
291static inline void kgsl_regwrite(struct kgsl_device *device,
292 unsigned int offsetwords,
293 unsigned int value)
294{
295 device->ftbl->regwrite(device, offsetwords, value);
296}
297
Jordan Crousea29a2e02012-08-14 09:09:23 -0600298static inline int kgsl_idle(struct kgsl_device *device)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299{
Jordan Crousea29a2e02012-08-14 09:09:23 -0600300 return device->ftbl->idle(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301}
302
Jordan Croused6535882012-06-20 08:22:16 -0600303static inline unsigned int kgsl_gpuid(struct kgsl_device *device,
304 unsigned int *chipid)
Jordan Crousea0758f22011-12-07 11:19:22 -0700305{
Jordan Croused6535882012-06-20 08:22:16 -0600306 return device->ftbl->gpuid(device, chipid);
Jordan Crousea0758f22011-12-07 11:19:22 -0700307}
308
Jeremy Gebben731dac52012-05-10 11:13:42 -0600309static inline unsigned int kgsl_readtimestamp(struct kgsl_device *device,
310 struct kgsl_context *context,
311 enum kgsl_timestamp_type type)
312{
313 return device->ftbl->readtimestamp(device, context, type);
314}
315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316static inline int kgsl_create_device_sysfs_files(struct device *root,
317 const struct device_attribute **list)
318{
319 int ret = 0, i;
320 for (i = 0; list[i] != NULL; i++)
321 ret |= device_create_file(root, list[i]);
322 return ret;
323}
324
325static inline void kgsl_remove_device_sysfs_files(struct device *root,
326 const struct device_attribute **list)
327{
328 int i;
329 for (i = 0; list[i] != NULL; i++)
330 device_remove_file(root, list[i]);
331}
332
333static inline struct kgsl_mmu *
334kgsl_get_mmu(struct kgsl_device *device)
335{
336 return (struct kgsl_mmu *) (device ? &device->mmu : NULL);
337}
338
339static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev)
340{
341 int i;
342
343 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
344 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev)
345 return kgsl_driver.devp[i];
346 }
347
348 return NULL;
349}
350
351static inline int kgsl_create_device_workqueue(struct kgsl_device *device)
352{
Steve Muckle3e042df2011-11-18 11:25:04 -0800353 device->work_queue = create_singlethread_workqueue(device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 if (!device->work_queue) {
Steve Muckle3e042df2011-11-18 11:25:04 -0800355 KGSL_DRV_ERR(device,
356 "create_singlethread_workqueue(%s) failed\n",
357 device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700358 return -EINVAL;
359 }
360 return 0;
361}
362
363static inline struct kgsl_context *
364kgsl_find_context(struct kgsl_device_private *dev_priv, uint32_t id)
365{
366 struct kgsl_context *ctxt =
367 idr_find(&dev_priv->device->context_idr, id);
368
369 /* Make sure that the context belongs to the current instance so
370 that other processes can't guess context IDs and mess things up */
371
372 return (ctxt && ctxt->dev_priv == dev_priv) ? ctxt : NULL;
373}
374
Carter Cooper7e7f02e2012-02-15 09:36:31 -0700375int kgsl_check_timestamp(struct kgsl_device *device,
376 struct kgsl_context *context, unsigned int timestamp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700377
378int kgsl_register_ts_notifier(struct kgsl_device *device,
379 struct notifier_block *nb);
380
381int kgsl_unregister_ts_notifier(struct kgsl_device *device,
382 struct notifier_block *nb);
383
Jordan Crouseb368e9b2012-04-27 14:01:59 -0600384int kgsl_device_platform_probe(struct kgsl_device *device);
385
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700386void kgsl_device_platform_remove(struct kgsl_device *device);
387
Jeremy Gebbenb50f3312011-12-16 08:58:33 -0700388const char *kgsl_pwrstate_to_str(unsigned int state);
389
Jordan Crouse156cfbc2012-01-24 09:32:04 -0700390int kgsl_device_snapshot_init(struct kgsl_device *device);
391int kgsl_device_snapshot(struct kgsl_device *device, int hang);
392void kgsl_device_snapshot_close(struct kgsl_device *device);
393
Jordan Croused8aa4be2012-04-18 09:31:07 -0600394static inline struct kgsl_device_platform_data *
395kgsl_device_get_drvdata(struct kgsl_device *dev)
396{
397 struct platform_device *pdev =
398 container_of(dev->parentdev, struct platform_device, dev);
399
400 return pdev->dev.platform_data;
401}
402
Jeremy Gebben9ad86922012-05-08 15:33:23 -0600403/**
404 * kgsl_context_get - Get context reference count
405 * @context
406 *
407 * Asynchronous code that holds a pointer to a context
408 * must hold a reference count on it. The kgsl device
409 * mutex must be held while the context reference count
410 * is changed.
411 */
412static inline void
413kgsl_context_get(struct kgsl_context *context)
414{
415 kref_get(&context->refcount);
416}
417
418void kgsl_context_destroy(struct kref *kref);
419
420/**
421 * kgsl_context_put - Release context reference count
422 * @context
423 *
424 */
425static inline void
426kgsl_context_put(struct kgsl_context *context)
427{
428 kref_put(&context->refcount, kgsl_context_destroy);
429}
430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431#endif /* __KGSL_DEVICE_H */