Jordan Crouse | 156cfbc | 2012-01-24 09:32:04 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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> |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 18 | #include <linux/pm_qos_params.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 19 | #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 Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 49 | #define KGSL_STATE_SLUMBER 0x00000080 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | |
| 51 | #define KGSL_GRAPHICS_MEMORY_LOW_WATERMARK 0x1000000 |
| 52 | |
| 53 | #define KGSL_IS_PAGE_ALIGNED(addr) (!((addr) & (~PAGE_MASK))) |
| 54 | |
| 55 | struct kgsl_device; |
| 56 | struct platform_device; |
| 57 | struct kgsl_device_private; |
| 58 | struct kgsl_context; |
| 59 | struct kgsl_power_stats; |
| 60 | |
| 61 | struct 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, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 79 | struct kgsl_context *context, unsigned int timestamp, |
| 80 | unsigned int msecs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 81 | unsigned int (*readtimestamp) (struct kgsl_device *device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 82 | struct kgsl_context *context, enum kgsl_timestamp_type type); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 83 | 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 Crouse | 9f73921 | 2011-07-28 08:37:57 -0600 | [diff] [blame] | 89 | void (*cleanup_pt)(struct kgsl_device *device, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 90 | 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 Crouse | a0758f2 | 2011-12-07 11:19:22 -0700 | [diff] [blame] | 94 | unsigned int (*gpuid)(struct kgsl_device *device); |
Jordan Crouse | 156cfbc | 2012-01-24 09:32:04 -0700 | [diff] [blame] | 95 | void * (*snapshot)(struct kgsl_device *device, void *snapshot, |
| 96 | int *remain, int hang); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 97 | /* Optional functions - these functions are not mandatory. The |
| 98 | driver will check that the function pointer is not NULL before |
| 99 | calling the hook */ |
| 100 | void (*setstate) (struct kgsl_device *device, uint32_t flags); |
| 101 | int (*drawctxt_create) (struct kgsl_device *device, |
| 102 | struct kgsl_pagetable *pagetable, struct kgsl_context *context, |
| 103 | uint32_t flags); |
| 104 | void (*drawctxt_destroy) (struct kgsl_device *device, |
| 105 | struct kgsl_context *context); |
| 106 | long (*ioctl) (struct kgsl_device_private *dev_priv, |
| 107 | unsigned int cmd, void *data); |
Jordan Crouse | ed7dd7f | 2012-03-29 13:16:02 -0600 | [diff] [blame] | 108 | int (*setproperty) (struct kgsl_device *device, |
| 109 | enum kgsl_property_type type, void *value, |
| 110 | unsigned int sizebytes); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 113 | /* MH register values */ |
| 114 | struct 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 Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 122 | struct kgsl_event { |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 123 | struct kgsl_context *context; |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 124 | uint32_t timestamp; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 125 | void (*func)(struct kgsl_device *, void *, u32, u32); |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 126 | void *priv; |
| 127 | struct list_head list; |
Jeremy Gebben | fd87f9a | 2012-02-10 07:06:09 -0700 | [diff] [blame] | 128 | struct kgsl_device_private *owner; |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 132 | struct kgsl_device { |
| 133 | struct device *dev; |
| 134 | const char *name; |
| 135 | unsigned int ver_major; |
| 136 | unsigned int ver_minor; |
| 137 | uint32_t flags; |
| 138 | enum kgsl_deviceid id; |
Jordan Crouse | 7501d45 | 2012-04-19 08:58:44 -0600 | [diff] [blame^] | 139 | unsigned long reg_phys; |
| 140 | void *reg_virt; |
| 141 | unsigned int reg_len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 142 | struct kgsl_memdesc memstore; |
| 143 | const char *iomemname; |
| 144 | |
Jeremy Gebben | 4e8aada | 2011-07-12 10:07:47 -0600 | [diff] [blame] | 145 | struct kgsl_mh mh; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | 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 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 159 | unsigned int last_expired_ctxt_id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | unsigned int active_cnt; |
| 161 | struct completion suspend_gate; |
| 162 | |
| 163 | wait_queue_head_t wait_queue; |
| 164 | struct workqueue_struct *work_queue; |
| 165 | struct device *parentdev; |
| 166 | struct completion recovery_gate; |
| 167 | struct dentry *d_debugfs; |
| 168 | struct idr context_idr; |
| 169 | struct early_suspend display_off; |
| 170 | |
Jordan Crouse | 156cfbc | 2012-01-24 09:32:04 -0700 | [diff] [blame] | 171 | void *snapshot; /* Pointer to the snapshot memory region */ |
| 172 | int snapshot_maxsize; /* Max size of the snapshot region */ |
| 173 | int snapshot_size; /* Current size of the snapshot region */ |
| 174 | u32 snapshot_timestamp; /* Timestamp of the last valid snapshot */ |
| 175 | int snapshot_frozen; /* 1 if the snapshot output is frozen until |
| 176 | it gets read by the user. This avoids |
| 177 | losing the output on multiple hangs */ |
| 178 | struct kobject snapshot_kobj; |
| 179 | |
Jordan Crouse | 9610b6b | 2012-03-16 14:53:42 -0600 | [diff] [blame] | 180 | /* |
| 181 | * List of GPU buffers that have been frozen in memory until they can be |
| 182 | * dumped |
| 183 | */ |
| 184 | struct list_head snapshot_obj_list; |
| 185 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | /* Logging levels */ |
| 187 | int cmd_log; |
| 188 | int ctxt_log; |
| 189 | int drv_log; |
| 190 | int mem_log; |
| 191 | int pwr_log; |
| 192 | struct wake_lock idle_wakelock; |
| 193 | struct kgsl_pwrscale pwrscale; |
| 194 | struct kobject pwrscale_kobj; |
Lucille Sylvester | 1029789 | 2012-02-27 13:54:47 -0700 | [diff] [blame] | 195 | struct pm_qos_request_list pm_qos_req_dma; |
Jordan Crouse | 1bf80aa | 2011-10-12 16:57:47 -0600 | [diff] [blame] | 196 | struct work_struct ts_expired_ws; |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 197 | struct list_head events; |
Lucille Sylvester | 808eca2 | 2011-11-03 10:26:29 -0700 | [diff] [blame] | 198 | s64 on_time; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | struct kgsl_context { |
| 202 | uint32_t id; |
| 203 | |
| 204 | /* Pointer to the owning device instance */ |
| 205 | struct kgsl_device_private *dev_priv; |
| 206 | |
| 207 | /* Pointer to the device specific context information */ |
| 208 | void *devctxt; |
Shubhraprakash Das | 2dfe5dd | 2012-02-10 13:49:53 -0700 | [diff] [blame] | 209 | /* |
| 210 | * Status indicating whether a gpu reset occurred and whether this |
| 211 | * context was responsible for causing it |
| 212 | */ |
| 213 | unsigned int reset_status; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | struct kgsl_process_private { |
| 217 | unsigned int refcnt; |
| 218 | pid_t pid; |
| 219 | spinlock_t mem_lock; |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 220 | struct rb_root mem_rb; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | struct kgsl_pagetable *pagetable; |
| 222 | struct list_head list; |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 223 | struct kobject kobj; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 224 | |
| 225 | struct { |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 226 | unsigned int cur; |
| 227 | unsigned int max; |
| 228 | } stats[KGSL_MEM_ENTRY_MAX]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | struct kgsl_device_private { |
| 232 | struct kgsl_device *device; |
| 233 | struct kgsl_process_private *process_priv; |
| 234 | }; |
| 235 | |
| 236 | struct kgsl_power_stats { |
| 237 | s64 total_time; |
| 238 | s64 busy_time; |
| 239 | }; |
| 240 | |
| 241 | struct kgsl_device *kgsl_get_device(int dev_idx); |
| 242 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 243 | static inline void kgsl_process_add_stats(struct kgsl_process_private *priv, |
| 244 | unsigned int type, size_t size) |
| 245 | { |
| 246 | priv->stats[type].cur += size; |
| 247 | if (priv->stats[type].max < priv->stats[type].cur) |
| 248 | priv->stats[type].max = priv->stats[type].cur; |
| 249 | } |
| 250 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 251 | static inline void kgsl_regread(struct kgsl_device *device, |
| 252 | unsigned int offsetwords, |
| 253 | unsigned int *value) |
| 254 | { |
| 255 | device->ftbl->regread(device, offsetwords, value); |
| 256 | } |
| 257 | |
| 258 | static inline void kgsl_regwrite(struct kgsl_device *device, |
| 259 | unsigned int offsetwords, |
| 260 | unsigned int value) |
| 261 | { |
| 262 | device->ftbl->regwrite(device, offsetwords, value); |
| 263 | } |
| 264 | |
| 265 | static inline int kgsl_idle(struct kgsl_device *device, unsigned int timeout) |
| 266 | { |
| 267 | return device->ftbl->idle(device, timeout); |
| 268 | } |
| 269 | |
Jordan Crouse | a0758f2 | 2011-12-07 11:19:22 -0700 | [diff] [blame] | 270 | static inline unsigned int kgsl_gpuid(struct kgsl_device *device) |
| 271 | { |
| 272 | return device->ftbl->gpuid(device); |
| 273 | } |
| 274 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 275 | static inline int kgsl_create_device_sysfs_files(struct device *root, |
| 276 | const struct device_attribute **list) |
| 277 | { |
| 278 | int ret = 0, i; |
| 279 | for (i = 0; list[i] != NULL; i++) |
| 280 | ret |= device_create_file(root, list[i]); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static inline void kgsl_remove_device_sysfs_files(struct device *root, |
| 285 | const struct device_attribute **list) |
| 286 | { |
| 287 | int i; |
| 288 | for (i = 0; list[i] != NULL; i++) |
| 289 | device_remove_file(root, list[i]); |
| 290 | } |
| 291 | |
| 292 | static inline struct kgsl_mmu * |
| 293 | kgsl_get_mmu(struct kgsl_device *device) |
| 294 | { |
| 295 | return (struct kgsl_mmu *) (device ? &device->mmu : NULL); |
| 296 | } |
| 297 | |
| 298 | static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev) |
| 299 | { |
| 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < KGSL_DEVICE_MAX; i++) { |
| 303 | if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev) |
| 304 | return kgsl_driver.devp[i]; |
| 305 | } |
| 306 | |
| 307 | return NULL; |
| 308 | } |
| 309 | |
| 310 | static inline int kgsl_create_device_workqueue(struct kgsl_device *device) |
| 311 | { |
Steve Muckle | 3e042df | 2011-11-18 11:25:04 -0800 | [diff] [blame] | 312 | device->work_queue = create_singlethread_workqueue(device->name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 313 | if (!device->work_queue) { |
Steve Muckle | 3e042df | 2011-11-18 11:25:04 -0800 | [diff] [blame] | 314 | KGSL_DRV_ERR(device, |
| 315 | "create_singlethread_workqueue(%s) failed\n", |
| 316 | device->name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static inline struct kgsl_context * |
| 323 | kgsl_find_context(struct kgsl_device_private *dev_priv, uint32_t id) |
| 324 | { |
| 325 | struct kgsl_context *ctxt = |
| 326 | idr_find(&dev_priv->device->context_idr, id); |
| 327 | |
| 328 | /* Make sure that the context belongs to the current instance so |
| 329 | that other processes can't guess context IDs and mess things up */ |
| 330 | |
| 331 | return (ctxt && ctxt->dev_priv == dev_priv) ? ctxt : NULL; |
| 332 | } |
| 333 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 334 | int kgsl_check_timestamp(struct kgsl_device *device, |
| 335 | struct kgsl_context *context, unsigned int timestamp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 336 | |
| 337 | int kgsl_register_ts_notifier(struct kgsl_device *device, |
| 338 | struct notifier_block *nb); |
| 339 | |
| 340 | int kgsl_unregister_ts_notifier(struct kgsl_device *device, |
| 341 | struct notifier_block *nb); |
| 342 | |
| 343 | int kgsl_device_platform_probe(struct kgsl_device *device, |
| 344 | irqreturn_t (*dev_isr) (int, void*)); |
| 345 | void kgsl_device_platform_remove(struct kgsl_device *device); |
| 346 | |
Jeremy Gebben | b50f331 | 2011-12-16 08:58:33 -0700 | [diff] [blame] | 347 | const char *kgsl_pwrstate_to_str(unsigned int state); |
| 348 | |
Jordan Crouse | 156cfbc | 2012-01-24 09:32:04 -0700 | [diff] [blame] | 349 | int kgsl_device_snapshot_init(struct kgsl_device *device); |
| 350 | int kgsl_device_snapshot(struct kgsl_device *device, int hang); |
| 351 | void kgsl_device_snapshot_close(struct kgsl_device *device); |
| 352 | |
Jordan Crouse | d8aa4be | 2012-04-18 09:31:07 -0600 | [diff] [blame] | 353 | static inline struct kgsl_device_platform_data * |
| 354 | kgsl_device_get_drvdata(struct kgsl_device *dev) |
| 355 | { |
| 356 | struct platform_device *pdev = |
| 357 | container_of(dev->parentdev, struct platform_device, dev); |
| 358 | |
| 359 | return pdev->dev.platform_data; |
| 360 | } |
| 361 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 362 | #endif /* __KGSL_DEVICE_H */ |