Duy Truong | e833aca | 2013-02-12 13:35:08 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2008-2012, The Linux Foundation. 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 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 14 | #include <linux/fb.h> |
| 15 | #include <linux/file.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/debugfs.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/android_pmem.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/pm_runtime.h> |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 24 | #include <linux/genlock.h> |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 25 | #include <linux/rbtree.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | #include <linux/ashmem.h> |
| 27 | #include <linux/major.h> |
Mitchel Humpherys | 5304470 | 2012-09-06 10:36:51 -0700 | [diff] [blame] | 28 | #include <linux/msm_ion.h> |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 29 | #include <linux/io.h> |
| 30 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | |
| 32 | #include "kgsl.h" |
| 33 | #include "kgsl_debugfs.h" |
| 34 | #include "kgsl_cffdump.h" |
| 35 | #include "kgsl_log.h" |
| 36 | #include "kgsl_sharedmem.h" |
| 37 | #include "kgsl_device.h" |
Norman Gee | d7402ff | 2011-10-28 08:51:11 -0600 | [diff] [blame] | 38 | #include "kgsl_trace.h" |
Jeff Boody | fe6c39c | 2012-08-09 13:54:50 -0600 | [diff] [blame] | 39 | #include "kgsl_sync.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 40 | |
| 41 | #undef MODULE_PARAM_PREFIX |
| 42 | #define MODULE_PARAM_PREFIX "kgsl." |
| 43 | |
| 44 | static int kgsl_pagetable_count = KGSL_PAGETABLE_COUNT; |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 45 | static char *ksgl_mmu_type; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 46 | module_param_named(ptcount, kgsl_pagetable_count, int, 0); |
| 47 | MODULE_PARM_DESC(kgsl_pagetable_count, |
| 48 | "Minimum number of pagetables for KGSL to allocate at initialization time"); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 49 | module_param_named(mmutype, ksgl_mmu_type, charp, 0); |
| 50 | MODULE_PARM_DESC(ksgl_mmu_type, |
| 51 | "Type of MMU to be used for graphics. Valid values are 'iommu' or 'gpummu' or 'nommu'"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 52 | |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 53 | static struct ion_client *kgsl_ion_client; |
| 54 | |
Jordan Crouse | 0fdf3a0 | 2012-03-16 14:53:41 -0600 | [diff] [blame] | 55 | /* kgsl_get_mem_entry - get the mem_entry structure for the specified object |
Shubhraprakash Das | 3cf33be | 2012-08-16 22:42:55 -0700 | [diff] [blame] | 56 | * @device - Pointer to the device structure |
Jordan Crouse | 0fdf3a0 | 2012-03-16 14:53:41 -0600 | [diff] [blame] | 57 | * @ptbase - the pagetable base of the object |
| 58 | * @gpuaddr - the GPU address of the object |
| 59 | * @size - Size of the region to search |
| 60 | */ |
| 61 | |
Shubhraprakash Das | 3cf33be | 2012-08-16 22:42:55 -0700 | [diff] [blame] | 62 | struct kgsl_mem_entry *kgsl_get_mem_entry(struct kgsl_device *device, |
| 63 | unsigned int ptbase, unsigned int gpuaddr, unsigned int size) |
Jordan Crouse | 0fdf3a0 | 2012-03-16 14:53:41 -0600 | [diff] [blame] | 64 | { |
| 65 | struct kgsl_process_private *priv; |
| 66 | struct kgsl_mem_entry *entry; |
| 67 | |
| 68 | mutex_lock(&kgsl_driver.process_mutex); |
| 69 | |
| 70 | list_for_each_entry(priv, &kgsl_driver.process_list, list) { |
Shubhraprakash Das | 3cf33be | 2012-08-16 22:42:55 -0700 | [diff] [blame] | 71 | if (!kgsl_mmu_pt_equal(&device->mmu, priv->pagetable, ptbase)) |
Jordan Crouse | 0fdf3a0 | 2012-03-16 14:53:41 -0600 | [diff] [blame] | 72 | continue; |
| 73 | spin_lock(&priv->mem_lock); |
| 74 | entry = kgsl_sharedmem_find_region(priv, gpuaddr, size); |
| 75 | |
| 76 | if (entry) { |
| 77 | spin_unlock(&priv->mem_lock); |
| 78 | mutex_unlock(&kgsl_driver.process_mutex); |
| 79 | return entry; |
| 80 | } |
| 81 | spin_unlock(&priv->mem_lock); |
| 82 | } |
| 83 | mutex_unlock(&kgsl_driver.process_mutex); |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | EXPORT_SYMBOL(kgsl_get_mem_entry); |
| 88 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 89 | static inline struct kgsl_mem_entry * |
| 90 | kgsl_mem_entry_create(void) |
| 91 | { |
| 92 | struct kgsl_mem_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 93 | |
| 94 | if (!entry) |
| 95 | KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*entry)); |
| 96 | else |
| 97 | kref_init(&entry->refcount); |
| 98 | |
| 99 | return entry; |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | kgsl_mem_entry_destroy(struct kref *kref) |
| 104 | { |
| 105 | struct kgsl_mem_entry *entry = container_of(kref, |
| 106 | struct kgsl_mem_entry, |
| 107 | refcount); |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 108 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 109 | if (entry->memtype != KGSL_MEM_ENTRY_KERNEL) |
| 110 | kgsl_driver.stats.mapped -= entry->memdesc.size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 112 | /* |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 113 | * Ion takes care of freeing the sglist for us so |
| 114 | * clear the sg before freeing the sharedmem so kgsl_sharedmem_free |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 115 | * doesn't try to free it again |
| 116 | */ |
| 117 | |
| 118 | if (entry->memtype == KGSL_MEM_ENTRY_ION) { |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 119 | entry->memdesc.sg = NULL; |
| 120 | } |
| 121 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 122 | kgsl_sharedmem_free(&entry->memdesc); |
| 123 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 124 | switch (entry->memtype) { |
| 125 | case KGSL_MEM_ENTRY_PMEM: |
| 126 | case KGSL_MEM_ENTRY_ASHMEM: |
| 127 | if (entry->priv_data) |
| 128 | fput(entry->priv_data); |
| 129 | break; |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 130 | case KGSL_MEM_ENTRY_ION: |
| 131 | ion_free(kgsl_ion_client, entry->priv_data); |
| 132 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | kfree(entry); |
| 136 | } |
| 137 | EXPORT_SYMBOL(kgsl_mem_entry_destroy); |
| 138 | |
| 139 | static |
| 140 | void kgsl_mem_entry_attach_process(struct kgsl_mem_entry *entry, |
| 141 | struct kgsl_process_private *process) |
| 142 | { |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 143 | struct rb_node **node; |
| 144 | struct rb_node *parent = NULL; |
| 145 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | spin_lock(&process->mem_lock); |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 147 | |
| 148 | node = &process->mem_rb.rb_node; |
| 149 | |
| 150 | while (*node) { |
| 151 | struct kgsl_mem_entry *cur; |
| 152 | |
| 153 | parent = *node; |
| 154 | cur = rb_entry(parent, struct kgsl_mem_entry, node); |
| 155 | |
| 156 | if (entry->memdesc.gpuaddr < cur->memdesc.gpuaddr) |
| 157 | node = &parent->rb_left; |
| 158 | else |
| 159 | node = &parent->rb_right; |
| 160 | } |
| 161 | |
| 162 | rb_link_node(&entry->node, parent, node); |
| 163 | rb_insert_color(&entry->node, &process->mem_rb); |
| 164 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | spin_unlock(&process->mem_lock); |
| 166 | |
| 167 | entry->priv = process; |
| 168 | } |
| 169 | |
Jordan Crouse | 0071401 | 2012-03-16 14:53:40 -0600 | [diff] [blame] | 170 | /* Detach a memory entry from a process and unmap it from the MMU */ |
| 171 | |
| 172 | static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry) |
| 173 | { |
| 174 | if (entry == NULL) |
| 175 | return; |
| 176 | |
| 177 | entry->priv->stats[entry->memtype].cur -= entry->memdesc.size; |
| 178 | entry->priv = NULL; |
| 179 | |
| 180 | kgsl_mmu_unmap(entry->memdesc.pagetable, &entry->memdesc); |
| 181 | |
| 182 | kgsl_mem_entry_put(entry); |
| 183 | } |
| 184 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 185 | /* Allocate a new context id */ |
| 186 | |
| 187 | static struct kgsl_context * |
| 188 | kgsl_create_context(struct kgsl_device_private *dev_priv) |
| 189 | { |
| 190 | struct kgsl_context *context; |
| 191 | int ret, id; |
| 192 | |
| 193 | context = kzalloc(sizeof(*context), GFP_KERNEL); |
| 194 | |
| 195 | if (context == NULL) |
| 196 | return NULL; |
| 197 | |
| 198 | while (1) { |
| 199 | if (idr_pre_get(&dev_priv->device->context_idr, |
| 200 | GFP_KERNEL) == 0) { |
| 201 | kfree(context); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 205 | ret = idr_get_new_above(&dev_priv->device->context_idr, |
| 206 | context, 1, &id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 207 | |
| 208 | if (ret != -EAGAIN) |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | if (ret) { |
| 213 | kfree(context); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 217 | /* MAX - 1, there is one memdesc in memstore for device info */ |
| 218 | if (id >= KGSL_MEMSTORE_MAX) { |
| 219 | KGSL_DRV_ERR(dev_priv->device, "cannot have more than %d " |
| 220 | "ctxts due to memstore limitation\n", |
| 221 | KGSL_MEMSTORE_MAX); |
| 222 | idr_remove(&dev_priv->device->context_idr, id); |
| 223 | kfree(context); |
| 224 | return NULL; |
| 225 | } |
| 226 | |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 227 | kref_init(&context->refcount); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 228 | context->id = id; |
| 229 | context->dev_priv = dev_priv; |
| 230 | |
Jeff Boody | fe6c39c | 2012-08-09 13:54:50 -0600 | [diff] [blame] | 231 | if (kgsl_sync_timeline_create(context)) { |
| 232 | idr_remove(&dev_priv->device->context_idr, id); |
Jordan Crouse | ef3456c | 2013-01-04 16:46:51 -0700 | [diff] [blame] | 233 | goto func_end; |
| 234 | } |
| 235 | |
| 236 | /* Initialize the pending event list */ |
| 237 | INIT_LIST_HEAD(&context->events); |
| 238 | |
| 239 | /* |
| 240 | * Initialize the node that is used to maintain the master list of |
| 241 | * contexts with pending events in the device structure. Normally we |
| 242 | * wouldn't take the time to initalize a node but at event add time we |
| 243 | * call list_empty() on the node as a quick way of determining if the |
| 244 | * context is already in the master list so it needs to always be either |
| 245 | * active or in an unused but initialized state |
| 246 | */ |
| 247 | |
| 248 | INIT_LIST_HEAD(&context->events_list); |
| 249 | |
| 250 | func_end: |
| 251 | if (ret) { |
Jeff Boody | fe6c39c | 2012-08-09 13:54:50 -0600 | [diff] [blame] | 252 | kfree(context); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 256 | return context; |
| 257 | } |
| 258 | |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 259 | /** |
| 260 | * kgsl_context_detach - Release the "master" context reference |
| 261 | * @context - The context that will be detached |
| 262 | * |
| 263 | * This is called when a context becomes unusable, because userspace |
| 264 | * has requested for it to be destroyed. The context itself may |
| 265 | * exist a bit longer until its reference count goes to zero. |
| 266 | * Other code referencing the context can detect that it has been |
| 267 | * detached because the context id will be set to KGSL_CONTEXT_INVALID. |
| 268 | */ |
| 269 | void |
| 270 | kgsl_context_detach(struct kgsl_context *context) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 271 | { |
| 272 | int id; |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 273 | struct kgsl_device *device; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | if (context == NULL) |
| 275 | return; |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 276 | device = context->dev_priv->device; |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 277 | trace_kgsl_context_detach(device, context); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | id = context->id; |
Jeremy Gebben | 1384a6a | 2012-05-17 14:34:17 -0600 | [diff] [blame] | 279 | |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 280 | if (device->ftbl->drawctxt_destroy) |
| 281 | device->ftbl->drawctxt_destroy(device, context); |
| 282 | /*device specific drawctxt_destroy MUST clean up devctxt */ |
| 283 | BUG_ON(context->devctxt); |
Jeremy Gebben | 1384a6a | 2012-05-17 14:34:17 -0600 | [diff] [blame] | 284 | /* |
| 285 | * Cancel events after the device-specific context is |
| 286 | * destroyed, to avoid possibly freeing memory while |
| 287 | * it is still in use by the GPU. |
| 288 | */ |
| 289 | kgsl_cancel_events_ctxt(device, context); |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 290 | idr_remove(&device->context_idr, id); |
| 291 | context->id = KGSL_CONTEXT_INVALID; |
| 292 | kgsl_context_put(context); |
| 293 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 294 | |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 295 | void |
| 296 | kgsl_context_destroy(struct kref *kref) |
| 297 | { |
| 298 | struct kgsl_context *context = container_of(kref, struct kgsl_context, |
| 299 | refcount); |
Jeff Boody | fe6c39c | 2012-08-09 13:54:50 -0600 | [diff] [blame] | 300 | kgsl_sync_timeline_destroy(context); |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 301 | kfree(context); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | static void kgsl_check_idle_locked(struct kgsl_device *device) |
| 305 | { |
| 306 | if (device->pwrctrl.nap_allowed == true && |
| 307 | device->state == KGSL_STATE_ACTIVE && |
| 308 | device->requested_state == KGSL_STATE_NONE) { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 309 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP); |
Lucille Sylvester | 721f7e7 | 2012-08-21 16:31:26 -0600 | [diff] [blame] | 310 | kgsl_pwrscale_idle(device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 311 | if (kgsl_pwrctrl_sleep(device) != 0) |
| 312 | mod_timer(&device->idle_timer, |
| 313 | jiffies + |
| 314 | device->pwrctrl.interval_timeout); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | static void kgsl_check_idle(struct kgsl_device *device) |
| 319 | { |
| 320 | mutex_lock(&device->mutex); |
| 321 | kgsl_check_idle_locked(device); |
| 322 | mutex_unlock(&device->mutex); |
| 323 | } |
| 324 | |
| 325 | struct kgsl_device *kgsl_get_device(int dev_idx) |
| 326 | { |
| 327 | int i; |
| 328 | struct kgsl_device *ret = NULL; |
| 329 | |
| 330 | mutex_lock(&kgsl_driver.devlock); |
| 331 | |
| 332 | for (i = 0; i < KGSL_DEVICE_MAX; i++) { |
| 333 | if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) { |
| 334 | ret = kgsl_driver.devp[i]; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | mutex_unlock(&kgsl_driver.devlock); |
| 340 | return ret; |
| 341 | } |
| 342 | EXPORT_SYMBOL(kgsl_get_device); |
| 343 | |
| 344 | static struct kgsl_device *kgsl_get_minor(int minor) |
| 345 | { |
| 346 | struct kgsl_device *ret = NULL; |
| 347 | |
| 348 | if (minor < 0 || minor >= KGSL_DEVICE_MAX) |
| 349 | return NULL; |
| 350 | |
| 351 | mutex_lock(&kgsl_driver.devlock); |
| 352 | ret = kgsl_driver.devp[minor]; |
| 353 | mutex_unlock(&kgsl_driver.devlock); |
| 354 | |
| 355 | return ret; |
| 356 | } |
| 357 | |
| 358 | int kgsl_register_ts_notifier(struct kgsl_device *device, |
| 359 | struct notifier_block *nb) |
| 360 | { |
| 361 | BUG_ON(device == NULL); |
| 362 | return atomic_notifier_chain_register(&device->ts_notifier_list, |
| 363 | nb); |
| 364 | } |
| 365 | EXPORT_SYMBOL(kgsl_register_ts_notifier); |
| 366 | |
| 367 | int kgsl_unregister_ts_notifier(struct kgsl_device *device, |
| 368 | struct notifier_block *nb) |
| 369 | { |
| 370 | BUG_ON(device == NULL); |
| 371 | return atomic_notifier_chain_unregister(&device->ts_notifier_list, |
| 372 | nb); |
| 373 | } |
| 374 | EXPORT_SYMBOL(kgsl_unregister_ts_notifier); |
| 375 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 376 | int kgsl_check_timestamp(struct kgsl_device *device, |
| 377 | struct kgsl_context *context, unsigned int timestamp) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 378 | { |
| 379 | unsigned int ts_processed; |
| 380 | |
Jeremy Gebben | 731dac5 | 2012-05-10 11:13:42 -0600 | [diff] [blame] | 381 | ts_processed = kgsl_readtimestamp(device, context, |
| 382 | KGSL_TIMESTAMP_RETIRED); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 383 | |
Jordan Crouse | e6239dd | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 384 | return (timestamp_cmp(ts_processed, timestamp) >= 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 385 | } |
| 386 | EXPORT_SYMBOL(kgsl_check_timestamp); |
| 387 | |
| 388 | static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state) |
| 389 | { |
| 390 | int status = -EINVAL; |
| 391 | unsigned int nap_allowed_saved; |
| 392 | struct kgsl_pwrscale_policy *policy_saved; |
| 393 | |
| 394 | if (!device) |
| 395 | return -EINVAL; |
| 396 | |
| 397 | KGSL_PWR_WARN(device, "suspend start\n"); |
| 398 | |
| 399 | mutex_lock(&device->mutex); |
| 400 | nap_allowed_saved = device->pwrctrl.nap_allowed; |
| 401 | device->pwrctrl.nap_allowed = false; |
| 402 | policy_saved = device->pwrscale.policy; |
| 403 | device->pwrscale.policy = NULL; |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 404 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SUSPEND); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 405 | /* Make sure no user process is waiting for a timestamp * |
| 406 | * before supending */ |
| 407 | if (device->active_cnt != 0) { |
| 408 | mutex_unlock(&device->mutex); |
| 409 | wait_for_completion(&device->suspend_gate); |
| 410 | mutex_lock(&device->mutex); |
| 411 | } |
Suman Tatiraju | 4a32c65 | 2012-02-17 11:59:05 -0800 | [diff] [blame] | 412 | /* Don't let the timer wake us during suspended sleep. */ |
| 413 | del_timer_sync(&device->idle_timer); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 414 | switch (device->state) { |
| 415 | case KGSL_STATE_INIT: |
| 416 | break; |
| 417 | case KGSL_STATE_ACTIVE: |
| 418 | /* Wait for the device to become idle */ |
Jordan Crouse | a29a2e0 | 2012-08-14 09:09:23 -0600 | [diff] [blame] | 419 | device->ftbl->idle(device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 420 | case KGSL_STATE_NAP: |
| 421 | case KGSL_STATE_SLEEP: |
| 422 | /* Get the completion ready to be waited upon. */ |
| 423 | INIT_COMPLETION(device->hwaccess_gate); |
| 424 | device->ftbl->suspend_context(device); |
| 425 | device->ftbl->stop(device); |
Suman Tatiraju | 48e7276 | 2012-05-03 11:12:03 -0700 | [diff] [blame] | 426 | pm_qos_update_request(&device->pm_qos_req_dma, |
| 427 | PM_QOS_DEFAULT_VALUE); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 428 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 429 | break; |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 430 | case KGSL_STATE_SLUMBER: |
| 431 | INIT_COMPLETION(device->hwaccess_gate); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 432 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 433 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 434 | default: |
| 435 | KGSL_PWR_ERR(device, "suspend fail, device %d\n", |
| 436 | device->id); |
| 437 | goto end; |
| 438 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 439 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 440 | device->pwrctrl.nap_allowed = nap_allowed_saved; |
| 441 | device->pwrscale.policy = policy_saved; |
| 442 | status = 0; |
| 443 | |
| 444 | end: |
| 445 | mutex_unlock(&device->mutex); |
| 446 | KGSL_PWR_WARN(device, "suspend end\n"); |
| 447 | return status; |
| 448 | } |
| 449 | |
| 450 | static int kgsl_resume_device(struct kgsl_device *device) |
| 451 | { |
| 452 | int status = -EINVAL; |
| 453 | |
| 454 | if (!device) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | KGSL_PWR_WARN(device, "resume start\n"); |
| 458 | mutex_lock(&device->mutex); |
| 459 | if (device->state == KGSL_STATE_SUSPEND) { |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 460 | kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 461 | status = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 462 | complete_all(&device->hwaccess_gate); |
| 463 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 464 | kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 465 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 466 | mutex_unlock(&device->mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 467 | KGSL_PWR_WARN(device, "resume end\n"); |
| 468 | return status; |
| 469 | } |
| 470 | |
| 471 | static int kgsl_suspend(struct device *dev) |
| 472 | { |
| 473 | |
| 474 | pm_message_t arg = {0}; |
| 475 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 476 | return kgsl_suspend_device(device, arg); |
| 477 | } |
| 478 | |
| 479 | static int kgsl_resume(struct device *dev) |
| 480 | { |
| 481 | struct kgsl_device *device = dev_get_drvdata(dev); |
| 482 | return kgsl_resume_device(device); |
| 483 | } |
| 484 | |
| 485 | static int kgsl_runtime_suspend(struct device *dev) |
| 486 | { |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int kgsl_runtime_resume(struct device *dev) |
| 491 | { |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | const struct dev_pm_ops kgsl_pm_ops = { |
| 496 | .suspend = kgsl_suspend, |
| 497 | .resume = kgsl_resume, |
| 498 | .runtime_suspend = kgsl_runtime_suspend, |
| 499 | .runtime_resume = kgsl_runtime_resume, |
| 500 | }; |
| 501 | EXPORT_SYMBOL(kgsl_pm_ops); |
| 502 | |
| 503 | void kgsl_early_suspend_driver(struct early_suspend *h) |
| 504 | { |
| 505 | struct kgsl_device *device = container_of(h, |
| 506 | struct kgsl_device, display_off); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 507 | KGSL_PWR_WARN(device, "early suspend start\n"); |
Ranjhith Kalisamy | 8b63695 | 2011-09-03 14:48:31 +0530 | [diff] [blame] | 508 | mutex_lock(&device->mutex); |
Lucille Sylvester | 4c889b7 | 2012-08-03 11:25:25 -0600 | [diff] [blame] | 509 | device->pwrctrl.restore_slumber = true; |
Lucille Sylvester | 344e462 | 2012-01-18 15:53:21 -0700 | [diff] [blame] | 510 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 511 | kgsl_pwrctrl_sleep(device); |
Ranjhith Kalisamy | 8b63695 | 2011-09-03 14:48:31 +0530 | [diff] [blame] | 512 | mutex_unlock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 513 | KGSL_PWR_WARN(device, "early suspend end\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 514 | } |
| 515 | EXPORT_SYMBOL(kgsl_early_suspend_driver); |
| 516 | |
| 517 | int kgsl_suspend_driver(struct platform_device *pdev, |
| 518 | pm_message_t state) |
| 519 | { |
| 520 | struct kgsl_device *device = dev_get_drvdata(&pdev->dev); |
| 521 | return kgsl_suspend_device(device, state); |
| 522 | } |
| 523 | EXPORT_SYMBOL(kgsl_suspend_driver); |
| 524 | |
| 525 | int kgsl_resume_driver(struct platform_device *pdev) |
| 526 | { |
| 527 | struct kgsl_device *device = dev_get_drvdata(&pdev->dev); |
| 528 | return kgsl_resume_device(device); |
| 529 | } |
| 530 | EXPORT_SYMBOL(kgsl_resume_driver); |
| 531 | |
| 532 | void kgsl_late_resume_driver(struct early_suspend *h) |
| 533 | { |
| 534 | struct kgsl_device *device = container_of(h, |
| 535 | struct kgsl_device, display_off); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 536 | KGSL_PWR_WARN(device, "late resume start\n"); |
Ranjhith Kalisamy | 8b63695 | 2011-09-03 14:48:31 +0530 | [diff] [blame] | 537 | mutex_lock(&device->mutex); |
Lucille Sylvester | 4c889b7 | 2012-08-03 11:25:25 -0600 | [diff] [blame] | 538 | device->pwrctrl.restore_slumber = false; |
Nilesh Shah | 94bdf2f | 2012-05-02 22:42:57 +0530 | [diff] [blame] | 539 | if (device->pwrscale.policy == NULL) |
| 540 | kgsl_pwrctrl_pwrlevel_change(device, KGSL_PWRLEVEL_TURBO); |
Suman Tatiraju | 3005cdd | 2012-03-19 14:38:11 -0700 | [diff] [blame] | 541 | kgsl_pwrctrl_wake(device); |
Ranjhith Kalisamy | 8b63695 | 2011-09-03 14:48:31 +0530 | [diff] [blame] | 542 | mutex_unlock(&device->mutex); |
Suman Tatiraju | 2456902 | 2011-10-27 11:11:12 -0700 | [diff] [blame] | 543 | kgsl_check_idle(device); |
| 544 | KGSL_PWR_WARN(device, "late resume end\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 545 | } |
| 546 | EXPORT_SYMBOL(kgsl_late_resume_driver); |
| 547 | |
| 548 | /* file operations */ |
| 549 | static struct kgsl_process_private * |
| 550 | kgsl_get_process_private(struct kgsl_device_private *cur_dev_priv) |
| 551 | { |
| 552 | struct kgsl_process_private *private; |
| 553 | |
| 554 | mutex_lock(&kgsl_driver.process_mutex); |
| 555 | list_for_each_entry(private, &kgsl_driver.process_list, list) { |
| 556 | if (private->pid == task_tgid_nr(current)) { |
| 557 | private->refcnt++; |
| 558 | goto out; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* no existing process private found for this dev_priv, create one */ |
| 563 | private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL); |
| 564 | if (private == NULL) { |
| 565 | KGSL_DRV_ERR(cur_dev_priv->device, "kzalloc(%d) failed\n", |
| 566 | sizeof(struct kgsl_process_private)); |
| 567 | goto out; |
| 568 | } |
| 569 | |
| 570 | spin_lock_init(&private->mem_lock); |
| 571 | private->refcnt = 1; |
| 572 | private->pid = task_tgid_nr(current); |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 573 | private->mem_rb = RB_ROOT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 575 | if (kgsl_mmu_enabled()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | { |
| 577 | unsigned long pt_name; |
| 578 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 579 | pt_name = task_tgid_nr(current); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | private->pagetable = kgsl_mmu_getpagetable(pt_name); |
| 581 | if (private->pagetable == NULL) { |
| 582 | kfree(private); |
| 583 | private = NULL; |
| 584 | goto out; |
| 585 | } |
| 586 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 587 | |
| 588 | list_add(&private->list, &kgsl_driver.process_list); |
| 589 | |
| 590 | kgsl_process_init_sysfs(private); |
Jeremy Gebben | ddf9301 | 2012-09-25 10:57:38 -0600 | [diff] [blame] | 591 | kgsl_process_init_debugfs(private); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 592 | |
| 593 | out: |
| 594 | mutex_unlock(&kgsl_driver.process_mutex); |
| 595 | return private; |
| 596 | } |
| 597 | |
| 598 | static void |
| 599 | kgsl_put_process_private(struct kgsl_device *device, |
| 600 | struct kgsl_process_private *private) |
| 601 | { |
| 602 | struct kgsl_mem_entry *entry = NULL; |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 603 | struct rb_node *node; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 604 | |
| 605 | if (!private) |
| 606 | return; |
| 607 | |
| 608 | mutex_lock(&kgsl_driver.process_mutex); |
| 609 | |
| 610 | if (--private->refcnt) |
| 611 | goto unlock; |
| 612 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 613 | kgsl_process_uninit_sysfs(private); |
Jeremy Gebben | ddf9301 | 2012-09-25 10:57:38 -0600 | [diff] [blame] | 614 | debugfs_remove_recursive(private->debug_root); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | |
| 616 | list_del(&private->list); |
| 617 | |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 618 | for (node = rb_first(&private->mem_rb); node; ) { |
| 619 | entry = rb_entry(node, struct kgsl_mem_entry, node); |
| 620 | node = rb_next(&entry->node); |
| 621 | |
| 622 | rb_erase(&entry->node, &private->mem_rb); |
Jordan Crouse | 0071401 | 2012-03-16 14:53:40 -0600 | [diff] [blame] | 623 | kgsl_mem_entry_detach_process(entry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | kgsl_mmu_putpagetable(private->pagetable); |
| 626 | kfree(private); |
| 627 | unlock: |
| 628 | mutex_unlock(&kgsl_driver.process_mutex); |
| 629 | } |
| 630 | |
| 631 | static int kgsl_release(struct inode *inodep, struct file *filep) |
| 632 | { |
| 633 | int result = 0; |
Jordan Crouse | 2db0af9 | 2011-08-08 16:05:09 -0600 | [diff] [blame] | 634 | struct kgsl_device_private *dev_priv = filep->private_data; |
| 635 | struct kgsl_process_private *private = dev_priv->process_priv; |
| 636 | struct kgsl_device *device = dev_priv->device; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | struct kgsl_context *context; |
| 638 | int next = 0; |
| 639 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 640 | filep->private_data = NULL; |
| 641 | |
| 642 | mutex_lock(&device->mutex); |
| 643 | kgsl_check_suspended(device); |
| 644 | |
| 645 | while (1) { |
Jordan Crouse | 2db0af9 | 2011-08-08 16:05:09 -0600 | [diff] [blame] | 646 | context = idr_get_next(&device->context_idr, &next); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 647 | if (context == NULL) |
| 648 | break; |
| 649 | |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 650 | if (context->dev_priv == dev_priv) |
| 651 | kgsl_context_detach(context); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | |
| 653 | next = next + 1; |
| 654 | } |
Jeremy Gebben | 1384a6a | 2012-05-17 14:34:17 -0600 | [diff] [blame] | 655 | /* |
| 656 | * Clean up any to-be-freed entries that belong to this |
| 657 | * process and this device. This is done after the context |
| 658 | * are destroyed to avoid possibly freeing memory while |
| 659 | * it is still in use by the GPU. |
| 660 | */ |
| 661 | kgsl_cancel_events(device, dev_priv); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 662 | |
| 663 | device->open_count--; |
| 664 | if (device->open_count == 0) { |
| 665 | result = device->ftbl->stop(device); |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 666 | kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 667 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 668 | |
| 669 | mutex_unlock(&device->mutex); |
| 670 | kfree(dev_priv); |
| 671 | |
| 672 | kgsl_put_process_private(device, private); |
| 673 | |
| 674 | pm_runtime_put(device->parentdev); |
| 675 | return result; |
| 676 | } |
| 677 | |
| 678 | static int kgsl_open(struct inode *inodep, struct file *filep) |
| 679 | { |
| 680 | int result; |
| 681 | struct kgsl_device_private *dev_priv; |
| 682 | struct kgsl_device *device; |
| 683 | unsigned int minor = iminor(inodep); |
| 684 | |
| 685 | device = kgsl_get_minor(minor); |
| 686 | BUG_ON(device == NULL); |
| 687 | |
| 688 | if (filep->f_flags & O_EXCL) { |
| 689 | KGSL_DRV_ERR(device, "O_EXCL not allowed\n"); |
| 690 | return -EBUSY; |
| 691 | } |
| 692 | |
| 693 | result = pm_runtime_get_sync(device->parentdev); |
| 694 | if (result < 0) { |
| 695 | KGSL_DRV_ERR(device, |
| 696 | "Runtime PM: Unable to wake up the device, rc = %d\n", |
| 697 | result); |
| 698 | return result; |
| 699 | } |
| 700 | result = 0; |
| 701 | |
| 702 | dev_priv = kzalloc(sizeof(struct kgsl_device_private), GFP_KERNEL); |
| 703 | if (dev_priv == NULL) { |
| 704 | KGSL_DRV_ERR(device, "kzalloc failed(%d)\n", |
| 705 | sizeof(struct kgsl_device_private)); |
| 706 | result = -ENOMEM; |
| 707 | goto err_pmruntime; |
| 708 | } |
| 709 | |
| 710 | dev_priv->device = device; |
| 711 | filep->private_data = dev_priv; |
| 712 | |
| 713 | /* Get file (per process) private struct */ |
| 714 | dev_priv->process_priv = kgsl_get_process_private(dev_priv); |
| 715 | if (dev_priv->process_priv == NULL) { |
| 716 | result = -ENOMEM; |
| 717 | goto err_freedevpriv; |
| 718 | } |
| 719 | |
| 720 | mutex_lock(&device->mutex); |
| 721 | kgsl_check_suspended(device); |
| 722 | |
| 723 | if (device->open_count == 0) { |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 724 | kgsl_sharedmem_set(&device->memstore, 0, 0, |
| 725 | device->memstore.size); |
| 726 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 727 | result = device->ftbl->start(device, true); |
| 728 | |
| 729 | if (result) { |
| 730 | mutex_unlock(&device->mutex); |
| 731 | goto err_putprocess; |
| 732 | } |
Jeremy Gebben | 388c297 | 2011-12-16 09:05:07 -0700 | [diff] [blame] | 733 | kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 734 | } |
| 735 | device->open_count++; |
| 736 | mutex_unlock(&device->mutex); |
| 737 | |
| 738 | KGSL_DRV_INFO(device, "Initialized %s: mmu=%s pagetable_count=%d\n", |
| 739 | device->name, kgsl_mmu_enabled() ? "on" : "off", |
| 740 | kgsl_pagetable_count); |
| 741 | |
| 742 | return result; |
| 743 | |
| 744 | err_putprocess: |
| 745 | kgsl_put_process_private(device, dev_priv->process_priv); |
| 746 | err_freedevpriv: |
| 747 | filep->private_data = NULL; |
| 748 | kfree(dev_priv); |
| 749 | err_pmruntime: |
| 750 | pm_runtime_put(device->parentdev); |
| 751 | return result; |
| 752 | } |
| 753 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 754 | /*call with private->mem_lock locked */ |
| 755 | struct kgsl_mem_entry * |
| 756 | kgsl_sharedmem_find_region(struct kgsl_process_private *private, |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 757 | unsigned int gpuaddr, size_t size) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 758 | { |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 759 | struct rb_node *node = private->mem_rb.rb_node; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 760 | |
Jordan Crouse | e22e21d | 2012-07-23 14:34:06 -0600 | [diff] [blame] | 761 | if (!kgsl_mmu_gpuaddr_in_range(gpuaddr)) |
| 762 | return NULL; |
| 763 | |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 764 | while (node != NULL) { |
| 765 | struct kgsl_mem_entry *entry; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 766 | |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 767 | entry = rb_entry(node, struct kgsl_mem_entry, node); |
| 768 | |
| 769 | |
| 770 | if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) |
| 771 | return entry; |
| 772 | |
| 773 | if (gpuaddr < entry->memdesc.gpuaddr) |
| 774 | node = node->rb_left; |
| 775 | else if (gpuaddr >= |
| 776 | (entry->memdesc.gpuaddr + entry->memdesc.size)) |
| 777 | node = node->rb_right; |
| 778 | else { |
| 779 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 783 | return NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 784 | } |
| 785 | EXPORT_SYMBOL(kgsl_sharedmem_find_region); |
| 786 | |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 787 | /*call with private->mem_lock locked */ |
| 788 | static inline struct kgsl_mem_entry * |
| 789 | kgsl_sharedmem_find(struct kgsl_process_private *private, unsigned int gpuaddr) |
| 790 | { |
| 791 | return kgsl_sharedmem_find_region(private, gpuaddr, 1); |
| 792 | } |
| 793 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 794 | /*call all ioctl sub functions with driver locked*/ |
| 795 | static long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv, |
| 796 | unsigned int cmd, void *data) |
| 797 | { |
| 798 | int result = 0; |
| 799 | struct kgsl_device_getproperty *param = data; |
| 800 | |
| 801 | switch (param->type) { |
| 802 | case KGSL_PROP_VERSION: |
| 803 | { |
| 804 | struct kgsl_version version; |
| 805 | if (param->sizebytes != sizeof(version)) { |
| 806 | result = -EINVAL; |
| 807 | break; |
| 808 | } |
| 809 | |
| 810 | version.drv_major = KGSL_VERSION_MAJOR; |
| 811 | version.drv_minor = KGSL_VERSION_MINOR; |
| 812 | version.dev_major = dev_priv->device->ver_major; |
| 813 | version.dev_minor = dev_priv->device->ver_minor; |
| 814 | |
| 815 | if (copy_to_user(param->value, &version, sizeof(version))) |
| 816 | result = -EFAULT; |
| 817 | |
| 818 | break; |
| 819 | } |
Shubhraprakash Das | 2dfe5dd | 2012-02-10 13:49:53 -0700 | [diff] [blame] | 820 | case KGSL_PROP_GPU_RESET_STAT: |
| 821 | { |
| 822 | /* Return reset status of given context and clear it */ |
| 823 | uint32_t id; |
| 824 | struct kgsl_context *context; |
| 825 | |
| 826 | if (param->sizebytes != sizeof(unsigned int)) { |
| 827 | result = -EINVAL; |
| 828 | break; |
| 829 | } |
| 830 | /* We expect the value passed in to contain the context id */ |
| 831 | if (copy_from_user(&id, param->value, |
| 832 | sizeof(unsigned int))) { |
| 833 | result = -EFAULT; |
| 834 | break; |
| 835 | } |
| 836 | context = kgsl_find_context(dev_priv, id); |
| 837 | if (!context) { |
| 838 | result = -EINVAL; |
| 839 | break; |
| 840 | } |
| 841 | /* |
| 842 | * Copy the reset status to value which also serves as |
| 843 | * the out parameter |
| 844 | */ |
| 845 | if (copy_to_user(param->value, &(context->reset_status), |
| 846 | sizeof(unsigned int))) { |
| 847 | result = -EFAULT; |
| 848 | break; |
| 849 | } |
| 850 | /* Clear reset status once its been queried */ |
| 851 | context->reset_status = KGSL_CTX_STAT_NO_ERROR; |
| 852 | break; |
| 853 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 854 | default: |
| 855 | result = dev_priv->device->ftbl->getproperty( |
| 856 | dev_priv->device, param->type, |
| 857 | param->value, param->sizebytes); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | return result; |
| 862 | } |
| 863 | |
Jordan Crouse | ed7dd7f | 2012-03-29 13:16:02 -0600 | [diff] [blame] | 864 | static long kgsl_ioctl_device_setproperty(struct kgsl_device_private *dev_priv, |
| 865 | unsigned int cmd, void *data) |
| 866 | { |
| 867 | int result = 0; |
| 868 | /* The getproperty struct is reused for setproperty too */ |
| 869 | struct kgsl_device_getproperty *param = data; |
| 870 | |
| 871 | if (dev_priv->device->ftbl->setproperty) |
| 872 | result = dev_priv->device->ftbl->setproperty( |
| 873 | dev_priv->device, param->type, |
| 874 | param->value, param->sizebytes); |
| 875 | |
| 876 | return result; |
| 877 | } |
| 878 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 879 | static long _device_waittimestamp(struct kgsl_device_private *dev_priv, |
| 880 | struct kgsl_context *context, |
| 881 | unsigned int timestamp, |
| 882 | unsigned int timeout) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 883 | { |
| 884 | int result = 0; |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 885 | struct kgsl_device *device = dev_priv->device; |
| 886 | unsigned int context_id = context ? context->id : KGSL_MEMSTORE_GLOBAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 887 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 888 | /* Set the active count so that suspend doesn't do the wrong thing */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 889 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 890 | device->active_cnt++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 891 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 892 | trace_kgsl_waittimestamp_entry(device, context_id, |
| 893 | kgsl_readtimestamp(device, context, |
| 894 | KGSL_TIMESTAMP_RETIRED), |
| 895 | timestamp, timeout); |
Norman Gee | d7402ff | 2011-10-28 08:51:11 -0600 | [diff] [blame] | 896 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 897 | result = device->ftbl->waittimestamp(dev_priv->device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 898 | context, timestamp, timeout); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 899 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 900 | trace_kgsl_waittimestamp_exit(device, |
| 901 | kgsl_readtimestamp(device, context, |
| 902 | KGSL_TIMESTAMP_RETIRED), |
| 903 | result); |
Norman Gee | d7402ff | 2011-10-28 08:51:11 -0600 | [diff] [blame] | 904 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 905 | /* Fire off any pending suspend operations that are in flight */ |
| 906 | |
| 907 | INIT_COMPLETION(dev_priv->device->suspend_gate); |
| 908 | dev_priv->device->active_cnt--; |
| 909 | complete(&dev_priv->device->suspend_gate); |
| 910 | |
| 911 | return result; |
| 912 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 913 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 914 | static long kgsl_ioctl_device_waittimestamp(struct kgsl_device_private |
| 915 | *dev_priv, unsigned int cmd, |
| 916 | void *data) |
| 917 | { |
| 918 | struct kgsl_device_waittimestamp *param = data; |
| 919 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 920 | return _device_waittimestamp(dev_priv, NULL, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 921 | param->timestamp, param->timeout); |
| 922 | } |
| 923 | |
| 924 | static long kgsl_ioctl_device_waittimestamp_ctxtid(struct kgsl_device_private |
| 925 | *dev_priv, unsigned int cmd, |
| 926 | void *data) |
| 927 | { |
| 928 | struct kgsl_device_waittimestamp_ctxtid *param = data; |
| 929 | struct kgsl_context *context; |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 930 | int result; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 931 | |
| 932 | context = kgsl_find_context(dev_priv, param->context_id); |
| 933 | if (context == NULL) { |
| 934 | KGSL_DRV_ERR(dev_priv->device, "invalid context_id %d\n", |
| 935 | param->context_id); |
| 936 | return -EINVAL; |
| 937 | } |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 938 | /* |
| 939 | * A reference count is needed here, because waittimestamp may |
| 940 | * block with the device mutex unlocked and userspace could |
| 941 | * request for the context to be destroyed during that time. |
| 942 | */ |
| 943 | kgsl_context_get(context); |
| 944 | result = _device_waittimestamp(dev_priv, context, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 945 | param->timestamp, param->timeout); |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 946 | kgsl_context_put(context); |
| 947 | return result; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 950 | static long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv, |
| 951 | unsigned int cmd, void *data) |
| 952 | { |
| 953 | int result = 0; |
| 954 | struct kgsl_ringbuffer_issueibcmds *param = data; |
| 955 | struct kgsl_ibdesc *ibdesc; |
| 956 | struct kgsl_context *context; |
| 957 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 958 | context = kgsl_find_context(dev_priv, param->drawctxt_id); |
| 959 | if (context == NULL) { |
| 960 | result = -EINVAL; |
| 961 | KGSL_DRV_ERR(dev_priv->device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 962 | "invalid context_id %d\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | param->drawctxt_id); |
| 964 | goto done; |
| 965 | } |
| 966 | |
| 967 | if (param->flags & KGSL_CONTEXT_SUBMIT_IB_LIST) { |
| 968 | KGSL_DRV_INFO(dev_priv->device, |
| 969 | "Using IB list mode for ib submission, numibs: %d\n", |
| 970 | param->numibs); |
| 971 | if (!param->numibs) { |
| 972 | KGSL_DRV_ERR(dev_priv->device, |
| 973 | "Invalid numibs as parameter: %d\n", |
| 974 | param->numibs); |
| 975 | result = -EINVAL; |
| 976 | goto done; |
| 977 | } |
| 978 | |
Jordan Crouse | 834c859 | 2012-07-24 10:06:35 -0600 | [diff] [blame] | 979 | /* |
| 980 | * Put a reasonable upper limit on the number of IBs that can be |
| 981 | * submitted |
| 982 | */ |
| 983 | |
| 984 | if (param->numibs > 10000) { |
| 985 | KGSL_DRV_ERR(dev_priv->device, |
| 986 | "Too many IBs submitted. count: %d max 10000\n", |
| 987 | param->numibs); |
| 988 | result = -EINVAL; |
| 989 | goto done; |
| 990 | } |
| 991 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 992 | ibdesc = kzalloc(sizeof(struct kgsl_ibdesc) * param->numibs, |
| 993 | GFP_KERNEL); |
| 994 | if (!ibdesc) { |
| 995 | KGSL_MEM_ERR(dev_priv->device, |
| 996 | "kzalloc(%d) failed\n", |
| 997 | sizeof(struct kgsl_ibdesc) * param->numibs); |
| 998 | result = -ENOMEM; |
| 999 | goto done; |
| 1000 | } |
| 1001 | |
| 1002 | if (copy_from_user(ibdesc, (void *)param->ibdesc_addr, |
| 1003 | sizeof(struct kgsl_ibdesc) * param->numibs)) { |
| 1004 | result = -EFAULT; |
| 1005 | KGSL_DRV_ERR(dev_priv->device, |
| 1006 | "copy_from_user failed\n"); |
| 1007 | goto free_ibdesc; |
| 1008 | } |
| 1009 | } else { |
| 1010 | KGSL_DRV_INFO(dev_priv->device, |
| 1011 | "Using single IB submission mode for ib submission\n"); |
| 1012 | /* If user space driver is still using the old mode of |
| 1013 | * submitting single ib then we need to support that as well */ |
| 1014 | ibdesc = kzalloc(sizeof(struct kgsl_ibdesc), GFP_KERNEL); |
| 1015 | if (!ibdesc) { |
| 1016 | KGSL_MEM_ERR(dev_priv->device, |
| 1017 | "kzalloc(%d) failed\n", |
| 1018 | sizeof(struct kgsl_ibdesc)); |
| 1019 | result = -ENOMEM; |
| 1020 | goto done; |
| 1021 | } |
| 1022 | ibdesc[0].gpuaddr = param->ibdesc_addr; |
| 1023 | ibdesc[0].sizedwords = param->numibs; |
| 1024 | param->numibs = 1; |
| 1025 | } |
| 1026 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1027 | result = dev_priv->device->ftbl->issueibcmds(dev_priv, |
| 1028 | context, |
| 1029 | ibdesc, |
| 1030 | param->numibs, |
| 1031 | ¶m->timestamp, |
| 1032 | param->flags); |
| 1033 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1034 | trace_kgsl_issueibcmds(dev_priv->device, param, ibdesc, result); |
Wei Zou | c8c0163 | 2012-03-24 17:27:26 -0700 | [diff] [blame] | 1035 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1036 | free_ibdesc: |
| 1037 | kfree(ibdesc); |
| 1038 | done: |
| 1039 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1040 | return result; |
| 1041 | } |
| 1042 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1043 | static long _cmdstream_readtimestamp(struct kgsl_device_private *dev_priv, |
| 1044 | struct kgsl_context *context, unsigned int type, |
| 1045 | unsigned int *timestamp) |
| 1046 | { |
Jeremy Gebben | 731dac5 | 2012-05-10 11:13:42 -0600 | [diff] [blame] | 1047 | *timestamp = kgsl_readtimestamp(dev_priv->device, context, type); |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1048 | |
| 1049 | trace_kgsl_readtimestamp(dev_priv->device, |
| 1050 | context ? context->id : KGSL_MEMSTORE_GLOBAL, |
| 1051 | type, *timestamp); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1056 | static long kgsl_ioctl_cmdstream_readtimestamp(struct kgsl_device_private |
| 1057 | *dev_priv, unsigned int cmd, |
| 1058 | void *data) |
| 1059 | { |
| 1060 | struct kgsl_cmdstream_readtimestamp *param = data; |
| 1061 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1062 | return _cmdstream_readtimestamp(dev_priv, NULL, |
| 1063 | param->type, ¶m->timestamp); |
| 1064 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1065 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1066 | static long kgsl_ioctl_cmdstream_readtimestamp_ctxtid(struct kgsl_device_private |
| 1067 | *dev_priv, unsigned int cmd, |
| 1068 | void *data) |
| 1069 | { |
| 1070 | struct kgsl_cmdstream_readtimestamp_ctxtid *param = data; |
| 1071 | struct kgsl_context *context; |
Norman Gee | d7402ff | 2011-10-28 08:51:11 -0600 | [diff] [blame] | 1072 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1073 | context = kgsl_find_context(dev_priv, param->context_id); |
| 1074 | if (context == NULL) { |
| 1075 | KGSL_DRV_ERR(dev_priv->device, "invalid context_id %d\n", |
| 1076 | param->context_id); |
| 1077 | return -EINVAL; |
| 1078 | } |
| 1079 | |
| 1080 | return _cmdstream_readtimestamp(dev_priv, context, |
| 1081 | param->type, ¶m->timestamp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Jeremy Gebben | c81a3c6 | 2012-02-07 16:10:23 -0700 | [diff] [blame] | 1084 | static void kgsl_freemem_event_cb(struct kgsl_device *device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1085 | void *priv, u32 id, u32 timestamp) |
Jeremy Gebben | c81a3c6 | 2012-02-07 16:10:23 -0700 | [diff] [blame] | 1086 | { |
| 1087 | struct kgsl_mem_entry *entry = priv; |
| 1088 | spin_lock(&entry->priv->mem_lock); |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 1089 | rb_erase(&entry->node, &entry->priv->mem_rb); |
Jeremy Gebben | c81a3c6 | 2012-02-07 16:10:23 -0700 | [diff] [blame] | 1090 | spin_unlock(&entry->priv->mem_lock); |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1091 | trace_kgsl_mem_timestamp_free(device, entry, id, timestamp, 0); |
Jordan Crouse | 0071401 | 2012-03-16 14:53:40 -0600 | [diff] [blame] | 1092 | kgsl_mem_entry_detach_process(entry); |
Jeremy Gebben | c81a3c6 | 2012-02-07 16:10:23 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1095 | static long _cmdstream_freememontimestamp(struct kgsl_device_private *dev_priv, |
| 1096 | unsigned int gpuaddr, struct kgsl_context *context, |
| 1097 | unsigned int timestamp, unsigned int type) |
| 1098 | { |
| 1099 | int result = 0; |
| 1100 | struct kgsl_mem_entry *entry = NULL; |
| 1101 | struct kgsl_device *device = dev_priv->device; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1102 | unsigned int context_id = context ? context->id : KGSL_MEMSTORE_GLOBAL; |
| 1103 | |
| 1104 | spin_lock(&dev_priv->process_priv->mem_lock); |
| 1105 | entry = kgsl_sharedmem_find(dev_priv->process_priv, gpuaddr); |
| 1106 | spin_unlock(&dev_priv->process_priv->mem_lock); |
| 1107 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1108 | if (!entry) { |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1109 | KGSL_DRV_ERR(dev_priv->device, |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1110 | "invalid gpuaddr %08x\n", gpuaddr); |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1111 | result = -EINVAL; |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1112 | goto done; |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1113 | } |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1114 | trace_kgsl_mem_timestamp_queue(device, entry, context_id, |
| 1115 | kgsl_readtimestamp(device, context, |
| 1116 | KGSL_TIMESTAMP_RETIRED), |
| 1117 | timestamp); |
| 1118 | result = kgsl_add_event(dev_priv->device, context_id, timestamp, |
| 1119 | kgsl_freemem_event_cb, entry, dev_priv); |
| 1120 | done: |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1121 | return result; |
| 1122 | } |
| 1123 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1124 | static long kgsl_ioctl_cmdstream_freememontimestamp(struct kgsl_device_private |
| 1125 | *dev_priv, unsigned int cmd, |
| 1126 | void *data) |
| 1127 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1128 | struct kgsl_cmdstream_freememontimestamp *param = data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1129 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1130 | return _cmdstream_freememontimestamp(dev_priv, param->gpuaddr, |
| 1131 | NULL, param->timestamp, param->type); |
| 1132 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1133 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1134 | static long kgsl_ioctl_cmdstream_freememontimestamp_ctxtid( |
| 1135 | struct kgsl_device_private |
| 1136 | *dev_priv, unsigned int cmd, |
| 1137 | void *data) |
| 1138 | { |
| 1139 | struct kgsl_cmdstream_freememontimestamp_ctxtid *param = data; |
| 1140 | struct kgsl_context *context; |
Jeremy Gebben | a585927 | 2012-03-01 12:46:28 -0700 | [diff] [blame] | 1141 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1142 | context = kgsl_find_context(dev_priv, param->context_id); |
| 1143 | if (context == NULL) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1144 | KGSL_DRV_ERR(dev_priv->device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1145 | "invalid drawctxt context_id %d\n", param->context_id); |
| 1146 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1149 | return _cmdstream_freememontimestamp(dev_priv, param->gpuaddr, |
| 1150 | context, param->timestamp, param->type); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | static long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv, |
| 1154 | unsigned int cmd, void *data) |
| 1155 | { |
| 1156 | int result = 0; |
| 1157 | struct kgsl_drawctxt_create *param = data; |
| 1158 | struct kgsl_context *context = NULL; |
| 1159 | |
| 1160 | context = kgsl_create_context(dev_priv); |
| 1161 | |
| 1162 | if (context == NULL) { |
| 1163 | result = -ENOMEM; |
| 1164 | goto done; |
| 1165 | } |
| 1166 | |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1167 | if (dev_priv->device->ftbl->drawctxt_create) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1168 | result = dev_priv->device->ftbl->drawctxt_create( |
| 1169 | dev_priv->device, dev_priv->process_priv->pagetable, |
| 1170 | context, param->flags); |
Jeremy Gebben | 4a3756c | 2012-05-08 16:51:43 -0600 | [diff] [blame] | 1171 | if (result) |
| 1172 | goto done; |
| 1173 | } |
| 1174 | trace_kgsl_context_create(dev_priv->device, context, param->flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1175 | param->drawctxt_id = context->id; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1176 | done: |
| 1177 | if (result && context) |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 1178 | kgsl_context_detach(context); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1179 | |
| 1180 | return result; |
| 1181 | } |
| 1182 | |
| 1183 | static long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv, |
| 1184 | unsigned int cmd, void *data) |
| 1185 | { |
| 1186 | int result = 0; |
| 1187 | struct kgsl_drawctxt_destroy *param = data; |
| 1188 | struct kgsl_context *context; |
| 1189 | |
| 1190 | context = kgsl_find_context(dev_priv, param->drawctxt_id); |
| 1191 | |
| 1192 | if (context == NULL) { |
| 1193 | result = -EINVAL; |
| 1194 | goto done; |
| 1195 | } |
| 1196 | |
Jeremy Gebben | 9ad8692 | 2012-05-08 15:33:23 -0600 | [diff] [blame] | 1197 | kgsl_context_detach(context); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1198 | done: |
| 1199 | return result; |
| 1200 | } |
| 1201 | |
| 1202 | static long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv, |
| 1203 | unsigned int cmd, void *data) |
| 1204 | { |
| 1205 | int result = 0; |
| 1206 | struct kgsl_sharedmem_free *param = data; |
| 1207 | struct kgsl_process_private *private = dev_priv->process_priv; |
| 1208 | struct kgsl_mem_entry *entry = NULL; |
| 1209 | |
| 1210 | spin_lock(&private->mem_lock); |
| 1211 | entry = kgsl_sharedmem_find(private, param->gpuaddr); |
| 1212 | if (entry) |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 1213 | rb_erase(&entry->node, &private->mem_rb); |
| 1214 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1215 | spin_unlock(&private->mem_lock); |
| 1216 | |
| 1217 | if (entry) { |
Jeremy Gebben | a585927 | 2012-03-01 12:46:28 -0700 | [diff] [blame] | 1218 | trace_kgsl_mem_free(entry); |
Jordan Crouse | 0071401 | 2012-03-16 14:53:40 -0600 | [diff] [blame] | 1219 | kgsl_mem_entry_detach_process(entry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1220 | } else { |
| 1221 | KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr); |
| 1222 | result = -EINVAL; |
| 1223 | } |
| 1224 | |
| 1225 | return result; |
| 1226 | } |
| 1227 | |
| 1228 | static struct vm_area_struct *kgsl_get_vma_from_start_addr(unsigned int addr) |
| 1229 | { |
| 1230 | struct vm_area_struct *vma; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1231 | |
| 1232 | down_read(¤t->mm->mmap_sem); |
| 1233 | vma = find_vma(current->mm, addr); |
| 1234 | up_read(¤t->mm->mmap_sem); |
Jordan Crouse | 2c542b6 | 2011-07-26 08:30:20 -0600 | [diff] [blame] | 1235 | if (!vma) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1236 | KGSL_CORE_ERR("find_vma(%x) failed\n", addr); |
Jordan Crouse | 2c542b6 | 2011-07-26 08:30:20 -0600 | [diff] [blame] | 1237 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1238 | return vma; |
| 1239 | } |
| 1240 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1241 | static inline int _check_region(unsigned long start, unsigned long size, |
| 1242 | uint64_t len) |
| 1243 | { |
| 1244 | uint64_t end = ((uint64_t) start) + size; |
| 1245 | return (end > len); |
| 1246 | } |
| 1247 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1248 | static int kgsl_get_phys_file(int fd, unsigned long *start, unsigned long *len, |
| 1249 | unsigned long *vstart, struct file **filep) |
| 1250 | { |
| 1251 | struct file *fbfile; |
| 1252 | int ret = 0; |
| 1253 | dev_t rdev; |
| 1254 | struct fb_info *info; |
| 1255 | |
| 1256 | *filep = NULL; |
Jordan Crouse | fd97843 | 2011-09-02 14:34:32 -0600 | [diff] [blame] | 1257 | #ifdef CONFIG_ANDROID_PMEM |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1258 | if (!get_pmem_file(fd, start, vstart, len, filep)) |
| 1259 | return 0; |
Jordan Crouse | fd97843 | 2011-09-02 14:34:32 -0600 | [diff] [blame] | 1260 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1261 | |
| 1262 | fbfile = fget(fd); |
| 1263 | if (fbfile == NULL) { |
| 1264 | KGSL_CORE_ERR("fget_light failed\n"); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
| 1268 | rdev = fbfile->f_dentry->d_inode->i_rdev; |
| 1269 | info = MAJOR(rdev) == FB_MAJOR ? registered_fb[MINOR(rdev)] : NULL; |
| 1270 | if (info) { |
| 1271 | *start = info->fix.smem_start; |
| 1272 | *len = info->fix.smem_len; |
| 1273 | *vstart = (unsigned long)__va(info->fix.smem_start); |
| 1274 | ret = 0; |
| 1275 | } else { |
| 1276 | KGSL_CORE_ERR("framebuffer minor %d not found\n", |
| 1277 | MINOR(rdev)); |
| 1278 | ret = -1; |
| 1279 | } |
| 1280 | |
| 1281 | fput(fbfile); |
| 1282 | |
| 1283 | return ret; |
| 1284 | } |
| 1285 | |
| 1286 | static int kgsl_setup_phys_file(struct kgsl_mem_entry *entry, |
| 1287 | struct kgsl_pagetable *pagetable, |
| 1288 | unsigned int fd, unsigned int offset, |
| 1289 | size_t size) |
| 1290 | { |
| 1291 | int ret; |
| 1292 | unsigned long phys, virt, len; |
| 1293 | struct file *filep; |
| 1294 | |
| 1295 | ret = kgsl_get_phys_file(fd, &phys, &len, &virt, &filep); |
| 1296 | if (ret) |
| 1297 | return ret; |
| 1298 | |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1299 | ret = -ERANGE; |
| 1300 | |
Wei Zou | 4061c0b | 2011-07-08 10:24:22 -0700 | [diff] [blame] | 1301 | if (phys == 0) { |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1302 | KGSL_CORE_ERR("kgsl_get_phys_file returned phys=0\n"); |
Wei Zou | 4061c0b | 2011-07-08 10:24:22 -0700 | [diff] [blame] | 1303 | goto err; |
| 1304 | } |
| 1305 | |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1306 | /* Make sure the length of the region, the offset and the desired |
| 1307 | * size are all page aligned or bail |
| 1308 | */ |
| 1309 | if ((len & ~PAGE_MASK) || |
| 1310 | (offset & ~PAGE_MASK) || |
| 1311 | (size & ~PAGE_MASK)) { |
| 1312 | KGSL_CORE_ERR("length %lu, offset %u or size %u " |
| 1313 | "is not page aligned\n", |
| 1314 | len, offset, size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1315 | goto err; |
| 1316 | } |
| 1317 | |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1318 | /* The size or offset can never be greater than the PMEM length */ |
| 1319 | if (offset >= len || size > len) { |
| 1320 | KGSL_CORE_ERR("offset %u or size %u " |
| 1321 | "exceeds pmem length %lu\n", |
| 1322 | offset, size, len); |
| 1323 | goto err; |
| 1324 | } |
| 1325 | |
| 1326 | /* If size is 0, then adjust it to default to the size of the region |
| 1327 | * minus the offset. If size isn't zero, then make sure that it will |
| 1328 | * fit inside of the region. |
| 1329 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1330 | if (size == 0) |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1331 | size = len - offset; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1332 | |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1333 | else if (_check_region(offset, size, len)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1334 | goto err; |
| 1335 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1336 | entry->priv_data = filep; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1337 | |
| 1338 | entry->memdesc.pagetable = pagetable; |
| 1339 | entry->memdesc.size = size; |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1340 | entry->memdesc.physaddr = phys + offset; |
| 1341 | entry->memdesc.hostptr = (void *) (virt + offset); |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1342 | |
Ajay Dudani | 62dd83e | 2012-09-11 16:38:13 -0600 | [diff] [blame] | 1343 | ret = memdesc_sg_phys(&entry->memdesc, phys + offset, size); |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1344 | if (ret) |
| 1345 | goto err; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1346 | |
| 1347 | return 0; |
| 1348 | err: |
Jordan Crouse | fd97843 | 2011-09-02 14:34:32 -0600 | [diff] [blame] | 1349 | #ifdef CONFIG_ANDROID_PMEM |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1350 | put_pmem_file(filep); |
Jordan Crouse | fd97843 | 2011-09-02 14:34:32 -0600 | [diff] [blame] | 1351 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1352 | return ret; |
| 1353 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1354 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1355 | static int memdesc_sg_virt(struct kgsl_memdesc *memdesc, |
| 1356 | void *addr, int size) |
| 1357 | { |
| 1358 | int i; |
| 1359 | int sglen = PAGE_ALIGN(size) / PAGE_SIZE; |
| 1360 | unsigned long paddr = (unsigned long) addr; |
| 1361 | |
Jordan Crouse | a652a07 | 2012-04-06 16:26:33 -0600 | [diff] [blame] | 1362 | memdesc->sg = kgsl_sg_alloc(sglen); |
| 1363 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1364 | if (memdesc->sg == NULL) |
| 1365 | return -ENOMEM; |
| 1366 | |
| 1367 | memdesc->sglen = sglen; |
Rajeev Kulkarni | 8dfdc336 | 2012-11-22 00:22:32 -0800 | [diff] [blame] | 1368 | memdesc->sglen_alloc = sglen; |
| 1369 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1370 | sg_init_table(memdesc->sg, sglen); |
| 1371 | |
| 1372 | spin_lock(¤t->mm->page_table_lock); |
| 1373 | |
| 1374 | for (i = 0; i < sglen; i++, paddr += PAGE_SIZE) { |
| 1375 | struct page *page; |
| 1376 | pmd_t *ppmd; |
| 1377 | pte_t *ppte; |
| 1378 | pgd_t *ppgd = pgd_offset(current->mm, paddr); |
| 1379 | |
| 1380 | if (pgd_none(*ppgd) || pgd_bad(*ppgd)) |
| 1381 | goto err; |
| 1382 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1383 | ppmd = pmd_offset(pud_offset(ppgd, paddr), paddr); |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1384 | if (pmd_none(*ppmd) || pmd_bad(*ppmd)) |
| 1385 | goto err; |
| 1386 | |
| 1387 | ppte = pte_offset_map(ppmd, paddr); |
| 1388 | if (ppte == NULL) |
| 1389 | goto err; |
| 1390 | |
| 1391 | page = pfn_to_page(pte_pfn(*ppte)); |
| 1392 | if (!page) |
| 1393 | goto err; |
| 1394 | |
| 1395 | sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0); |
| 1396 | pte_unmap(ppte); |
| 1397 | } |
| 1398 | |
| 1399 | spin_unlock(¤t->mm->page_table_lock); |
| 1400 | |
| 1401 | return 0; |
| 1402 | |
| 1403 | err: |
| 1404 | spin_unlock(¤t->mm->page_table_lock); |
Jordan Crouse | a652a07 | 2012-04-06 16:26:33 -0600 | [diff] [blame] | 1405 | kgsl_sg_free(memdesc->sg, sglen); |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1406 | memdesc->sg = NULL; |
| 1407 | |
| 1408 | return -EINVAL; |
| 1409 | } |
| 1410 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1411 | static int kgsl_setup_hostptr(struct kgsl_mem_entry *entry, |
| 1412 | struct kgsl_pagetable *pagetable, |
| 1413 | void *hostptr, unsigned int offset, |
| 1414 | size_t size) |
| 1415 | { |
| 1416 | struct vm_area_struct *vma; |
| 1417 | unsigned int len; |
| 1418 | |
| 1419 | down_read(¤t->mm->mmap_sem); |
| 1420 | vma = find_vma(current->mm, (unsigned int) hostptr); |
| 1421 | up_read(¤t->mm->mmap_sem); |
| 1422 | |
| 1423 | if (!vma) { |
| 1424 | KGSL_CORE_ERR("find_vma(%p) failed\n", hostptr); |
| 1425 | return -EINVAL; |
| 1426 | } |
| 1427 | |
| 1428 | /* We don't necessarily start at vma->vm_start */ |
| 1429 | len = vma->vm_end - (unsigned long) hostptr; |
| 1430 | |
| 1431 | if (offset >= len) |
| 1432 | return -EINVAL; |
| 1433 | |
| 1434 | if (!KGSL_IS_PAGE_ALIGNED((unsigned long) hostptr) || |
| 1435 | !KGSL_IS_PAGE_ALIGNED(len)) { |
| 1436 | KGSL_CORE_ERR("user address len(%u)" |
| 1437 | "and start(%p) must be page" |
| 1438 | "aligned\n", len, hostptr); |
| 1439 | return -EINVAL; |
| 1440 | } |
| 1441 | |
| 1442 | if (size == 0) |
| 1443 | size = len; |
| 1444 | |
| 1445 | /* Adjust the size of the region to account for the offset */ |
| 1446 | size += offset & ~PAGE_MASK; |
| 1447 | |
| 1448 | size = ALIGN(size, PAGE_SIZE); |
| 1449 | |
| 1450 | if (_check_region(offset & PAGE_MASK, size, len)) { |
| 1451 | KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger" |
| 1452 | "than region length %d\n", |
| 1453 | offset & PAGE_MASK, size, len); |
| 1454 | return -EINVAL; |
| 1455 | } |
| 1456 | |
| 1457 | entry->memdesc.pagetable = pagetable; |
| 1458 | entry->memdesc.size = size; |
| 1459 | entry->memdesc.hostptr = hostptr + (offset & PAGE_MASK); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1460 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1461 | return memdesc_sg_virt(&entry->memdesc, |
| 1462 | hostptr + (offset & PAGE_MASK), size); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | #ifdef CONFIG_ASHMEM |
| 1466 | static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry, |
| 1467 | struct kgsl_pagetable *pagetable, |
| 1468 | int fd, void *hostptr, size_t size) |
| 1469 | { |
| 1470 | int ret; |
| 1471 | struct vm_area_struct *vma; |
| 1472 | struct file *filep, *vmfile; |
| 1473 | unsigned long len; |
Jordan Crouse | 2c542b6 | 2011-07-26 08:30:20 -0600 | [diff] [blame] | 1474 | unsigned int hostaddr = (unsigned int) hostptr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1475 | |
Jordan Crouse | 2c542b6 | 2011-07-26 08:30:20 -0600 | [diff] [blame] | 1476 | vma = kgsl_get_vma_from_start_addr(hostaddr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1477 | if (vma == NULL) |
| 1478 | return -EINVAL; |
| 1479 | |
Jordan Crouse | 2c542b6 | 2011-07-26 08:30:20 -0600 | [diff] [blame] | 1480 | if (vma->vm_pgoff || vma->vm_start != hostaddr) { |
| 1481 | KGSL_CORE_ERR("Invalid vma region\n"); |
| 1482 | return -EINVAL; |
| 1483 | } |
| 1484 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1485 | len = vma->vm_end - vma->vm_start; |
| 1486 | |
| 1487 | if (size == 0) |
| 1488 | size = len; |
| 1489 | |
| 1490 | if (size != len) { |
| 1491 | KGSL_CORE_ERR("Invalid size %d for vma region %p\n", |
| 1492 | size, hostptr); |
| 1493 | return -EINVAL; |
| 1494 | } |
| 1495 | |
| 1496 | ret = get_ashmem_file(fd, &filep, &vmfile, &len); |
| 1497 | |
| 1498 | if (ret) { |
| 1499 | KGSL_CORE_ERR("get_ashmem_file failed\n"); |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | if (vmfile != vma->vm_file) { |
| 1504 | KGSL_CORE_ERR("ashmem shmem file does not match vma\n"); |
| 1505 | ret = -EINVAL; |
| 1506 | goto err; |
| 1507 | } |
| 1508 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1509 | entry->priv_data = filep; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1510 | entry->memdesc.pagetable = pagetable; |
| 1511 | entry->memdesc.size = ALIGN(size, PAGE_SIZE); |
| 1512 | entry->memdesc.hostptr = hostptr; |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1513 | |
| 1514 | ret = memdesc_sg_virt(&entry->memdesc, hostptr, size); |
| 1515 | if (ret) |
| 1516 | goto err; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1517 | |
| 1518 | return 0; |
| 1519 | |
| 1520 | err: |
| 1521 | put_ashmem_file(filep); |
| 1522 | return ret; |
| 1523 | } |
| 1524 | #else |
| 1525 | static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry, |
| 1526 | struct kgsl_pagetable *pagetable, |
| 1527 | int fd, void *hostptr, size_t size) |
| 1528 | { |
| 1529 | return -EINVAL; |
| 1530 | } |
| 1531 | #endif |
| 1532 | |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1533 | static int kgsl_setup_ion(struct kgsl_mem_entry *entry, |
| 1534 | struct kgsl_pagetable *pagetable, int fd) |
| 1535 | { |
| 1536 | struct ion_handle *handle; |
| 1537 | struct scatterlist *s; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1538 | struct sg_table *sg_table; |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1539 | |
Harsh Vardhan Dwivedi | f48af7f | 2012-04-13 12:50:44 -0600 | [diff] [blame] | 1540 | if (IS_ERR_OR_NULL(kgsl_ion_client)) |
| 1541 | return -ENODEV; |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1542 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1543 | handle = ion_import_dma_buf(kgsl_ion_client, fd); |
Ranjhith Kalisamy | 0d2e14f | 2012-08-14 19:49:39 +0530 | [diff] [blame] | 1544 | if (IS_ERR(handle)) |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1545 | return PTR_ERR(handle); |
Ranjhith Kalisamy | 0d2e14f | 2012-08-14 19:49:39 +0530 | [diff] [blame] | 1546 | else if (!handle) |
| 1547 | return -EINVAL; |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1548 | |
| 1549 | entry->memtype = KGSL_MEM_ENTRY_ION; |
| 1550 | entry->priv_data = handle; |
| 1551 | entry->memdesc.pagetable = pagetable; |
| 1552 | entry->memdesc.size = 0; |
| 1553 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1554 | sg_table = ion_sg_table(kgsl_ion_client, handle); |
| 1555 | |
| 1556 | if (IS_ERR_OR_NULL(sg_table)) |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1557 | goto err; |
| 1558 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1559 | entry->memdesc.sg = sg_table->sgl; |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1560 | |
| 1561 | /* Calculate the size of the memdesc from the sglist */ |
| 1562 | |
| 1563 | entry->memdesc.sglen = 0; |
| 1564 | |
| 1565 | for (s = entry->memdesc.sg; s != NULL; s = sg_next(s)) { |
| 1566 | entry->memdesc.size += s->length; |
| 1567 | entry->memdesc.sglen++; |
| 1568 | } |
| 1569 | |
| 1570 | return 0; |
| 1571 | err: |
| 1572 | ion_free(kgsl_ion_client, handle); |
| 1573 | return -ENOMEM; |
| 1574 | } |
| 1575 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1576 | static long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv, |
| 1577 | unsigned int cmd, void *data) |
| 1578 | { |
| 1579 | int result = -EINVAL; |
| 1580 | struct kgsl_map_user_mem *param = data; |
| 1581 | struct kgsl_mem_entry *entry = NULL; |
| 1582 | struct kgsl_process_private *private = dev_priv->process_priv; |
Jason | 848741a | 2011-07-12 10:24:25 -0700 | [diff] [blame] | 1583 | enum kgsl_user_mem_type memtype; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1584 | |
| 1585 | entry = kgsl_mem_entry_create(); |
| 1586 | |
| 1587 | if (entry == NULL) |
| 1588 | return -ENOMEM; |
| 1589 | |
Jason | 848741a | 2011-07-12 10:24:25 -0700 | [diff] [blame] | 1590 | if (_IOC_SIZE(cmd) == sizeof(struct kgsl_sharedmem_from_pmem)) |
| 1591 | memtype = KGSL_USER_MEM_TYPE_PMEM; |
| 1592 | else |
| 1593 | memtype = param->memtype; |
| 1594 | |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 1595 | entry->memdesc.flags = param->flags; |
| 1596 | |
Jason | 848741a | 2011-07-12 10:24:25 -0700 | [diff] [blame] | 1597 | switch (memtype) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1598 | case KGSL_USER_MEM_TYPE_PMEM: |
| 1599 | if (param->fd == 0 || param->len == 0) |
| 1600 | break; |
| 1601 | |
| 1602 | result = kgsl_setup_phys_file(entry, private->pagetable, |
| 1603 | param->fd, param->offset, |
| 1604 | param->len); |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1605 | entry->memtype = KGSL_MEM_ENTRY_PMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1606 | break; |
| 1607 | |
| 1608 | case KGSL_USER_MEM_TYPE_ADDR: |
Harsh Vardhan Dwivedi | a9eb7cb | 2012-03-26 15:21:38 -0600 | [diff] [blame] | 1609 | KGSL_DEV_ERR_ONCE(dev_priv->device, "User mem type " |
| 1610 | "KGSL_USER_MEM_TYPE_ADDR is deprecated\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1611 | if (!kgsl_mmu_enabled()) { |
| 1612 | KGSL_DRV_ERR(dev_priv->device, |
| 1613 | "Cannot map paged memory with the " |
| 1614 | "MMU disabled\n"); |
| 1615 | break; |
| 1616 | } |
| 1617 | |
| 1618 | if (param->hostptr == 0) |
| 1619 | break; |
| 1620 | |
| 1621 | result = kgsl_setup_hostptr(entry, private->pagetable, |
| 1622 | (void *) param->hostptr, |
| 1623 | param->offset, param->len); |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1624 | entry->memtype = KGSL_MEM_ENTRY_USER; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1625 | break; |
| 1626 | |
| 1627 | case KGSL_USER_MEM_TYPE_ASHMEM: |
| 1628 | if (!kgsl_mmu_enabled()) { |
| 1629 | KGSL_DRV_ERR(dev_priv->device, |
| 1630 | "Cannot map paged memory with the " |
| 1631 | "MMU disabled\n"); |
| 1632 | break; |
| 1633 | } |
| 1634 | |
| 1635 | if (param->hostptr == 0) |
| 1636 | break; |
| 1637 | |
| 1638 | result = kgsl_setup_ashmem(entry, private->pagetable, |
| 1639 | param->fd, (void *) param->hostptr, |
| 1640 | param->len); |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1641 | |
| 1642 | entry->memtype = KGSL_MEM_ENTRY_ASHMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1643 | break; |
Jordan Crouse | 8eab35a | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1644 | case KGSL_USER_MEM_TYPE_ION: |
| 1645 | result = kgsl_setup_ion(entry, private->pagetable, |
| 1646 | param->fd); |
| 1647 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1648 | default: |
Jason | 848741a | 2011-07-12 10:24:25 -0700 | [diff] [blame] | 1649 | KGSL_CORE_ERR("Invalid memory type: %x\n", memtype); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1650 | break; |
| 1651 | } |
| 1652 | |
| 1653 | if (result) |
| 1654 | goto error; |
| 1655 | |
Rajeev Kulkarni | 8dfdc336 | 2012-11-22 00:22:32 -0800 | [diff] [blame] | 1656 | if (entry->memdesc.size >= SZ_1M) |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 1657 | kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_1M)); |
Rajeev Kulkarni | 8dfdc336 | 2012-11-22 00:22:32 -0800 | [diff] [blame] | 1658 | else if (entry->memdesc.size >= SZ_64K) |
Jordan Crouse | dc67dfb | 2012-10-25 09:41:46 -0600 | [diff] [blame] | 1659 | kgsl_memdesc_set_align(&entry->memdesc, ilog2(SZ_64)); |
Rajeev Kulkarni | 8dfdc336 | 2012-11-22 00:22:32 -0800 | [diff] [blame] | 1660 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1661 | result = kgsl_mmu_map(private->pagetable, |
| 1662 | &entry->memdesc, |
| 1663 | GSL_PT_PAGE_RV | GSL_PT_PAGE_WV); |
| 1664 | |
| 1665 | if (result) |
| 1666 | goto error_put_file_ptr; |
| 1667 | |
| 1668 | /* Adjust the returned value for a non 4k aligned offset */ |
| 1669 | param->gpuaddr = entry->memdesc.gpuaddr + (param->offset & ~PAGE_MASK); |
| 1670 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1671 | KGSL_STATS_ADD(param->len, kgsl_driver.stats.mapped, |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1672 | kgsl_driver.stats.mapped_max); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1673 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1674 | kgsl_process_add_stats(private, entry->memtype, param->len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1675 | |
| 1676 | kgsl_mem_entry_attach_process(entry, private); |
Jeremy Gebben | a585927 | 2012-03-01 12:46:28 -0700 | [diff] [blame] | 1677 | trace_kgsl_mem_map(entry, param->fd); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1678 | |
| 1679 | kgsl_check_idle(dev_priv->device); |
| 1680 | return result; |
| 1681 | |
Jeremy Gebben | 53d4dd0 | 2012-05-07 15:42:00 -0600 | [diff] [blame] | 1682 | error_put_file_ptr: |
| 1683 | switch (entry->memtype) { |
| 1684 | case KGSL_MEM_ENTRY_PMEM: |
| 1685 | case KGSL_MEM_ENTRY_ASHMEM: |
| 1686 | if (entry->priv_data) |
| 1687 | fput(entry->priv_data); |
| 1688 | break; |
| 1689 | case KGSL_MEM_ENTRY_ION: |
Jeremy Gebben | 53d4dd0 | 2012-05-07 15:42:00 -0600 | [diff] [blame] | 1690 | ion_free(kgsl_ion_client, entry->priv_data); |
| 1691 | break; |
| 1692 | default: |
| 1693 | break; |
| 1694 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1695 | error: |
| 1696 | kfree(entry); |
| 1697 | kgsl_check_idle(dev_priv->device); |
| 1698 | return result; |
| 1699 | } |
| 1700 | |
| 1701 | /*This function flushes a graphics memory allocation from CPU cache |
| 1702 | *when caching is enabled with MMU*/ |
| 1703 | static long |
| 1704 | kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv, |
| 1705 | unsigned int cmd, void *data) |
| 1706 | { |
| 1707 | int result = 0; |
| 1708 | struct kgsl_mem_entry *entry; |
| 1709 | struct kgsl_sharedmem_free *param = data; |
| 1710 | struct kgsl_process_private *private = dev_priv->process_priv; |
| 1711 | |
| 1712 | spin_lock(&private->mem_lock); |
| 1713 | entry = kgsl_sharedmem_find(private, param->gpuaddr); |
| 1714 | if (!entry) { |
| 1715 | KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr); |
| 1716 | result = -EINVAL; |
Jeremy Gebben | 690f9d1 | 2011-08-08 16:33:49 -0600 | [diff] [blame] | 1717 | goto done; |
| 1718 | } |
Jeremy Gebben | 690f9d1 | 2011-08-08 16:33:49 -0600 | [diff] [blame] | 1719 | if (!entry->memdesc.hostptr) { |
| 1720 | KGSL_CORE_ERR("invalid hostptr with gpuaddr %08x\n", |
| 1721 | param->gpuaddr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1722 | goto done; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1723 | } |
Jeremy Gebben | 690f9d1 | 2011-08-08 16:33:49 -0600 | [diff] [blame] | 1724 | |
| 1725 | kgsl_cache_range_op(&entry->memdesc, KGSL_CACHE_OP_CLEAN); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1726 | done: |
Jeremy Gebben | 690f9d1 | 2011-08-08 16:33:49 -0600 | [diff] [blame] | 1727 | spin_unlock(&private->mem_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1728 | return result; |
| 1729 | } |
| 1730 | |
| 1731 | static long |
| 1732 | kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv, |
| 1733 | unsigned int cmd, void *data) |
| 1734 | { |
| 1735 | struct kgsl_process_private *private = dev_priv->process_priv; |
| 1736 | struct kgsl_gpumem_alloc *param = data; |
| 1737 | struct kgsl_mem_entry *entry; |
| 1738 | int result; |
| 1739 | |
| 1740 | entry = kgsl_mem_entry_create(); |
| 1741 | if (entry == NULL) |
| 1742 | return -ENOMEM; |
| 1743 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1744 | result = kgsl_allocate_user(&entry->memdesc, private->pagetable, |
| 1745 | param->size, param->flags); |
| 1746 | |
| 1747 | if (result == 0) { |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1748 | entry->memtype = KGSL_MEM_ENTRY_KERNEL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1749 | kgsl_mem_entry_attach_process(entry, private); |
| 1750 | param->gpuaddr = entry->memdesc.gpuaddr; |
| 1751 | |
Jordan Crouse | 1b897cf | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 1752 | kgsl_process_add_stats(private, entry->memtype, param->size); |
Jeremy Gebben | a585927 | 2012-03-01 12:46:28 -0700 | [diff] [blame] | 1753 | trace_kgsl_mem_alloc(entry); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1754 | } else |
| 1755 | kfree(entry); |
| 1756 | |
| 1757 | kgsl_check_idle(dev_priv->device); |
| 1758 | return result; |
| 1759 | } |
Jeremy Gebben | a7423e4 | 2011-04-18 15:11:21 -0600 | [diff] [blame] | 1760 | static long kgsl_ioctl_cff_syncmem(struct kgsl_device_private *dev_priv, |
| 1761 | unsigned int cmd, void *data) |
| 1762 | { |
| 1763 | int result = 0; |
| 1764 | struct kgsl_cff_syncmem *param = data; |
| 1765 | struct kgsl_process_private *private = dev_priv->process_priv; |
| 1766 | struct kgsl_mem_entry *entry = NULL; |
| 1767 | |
| 1768 | spin_lock(&private->mem_lock); |
| 1769 | entry = kgsl_sharedmem_find_region(private, param->gpuaddr, param->len); |
| 1770 | if (entry) |
| 1771 | kgsl_cffdump_syncmem(dev_priv, &entry->memdesc, param->gpuaddr, |
| 1772 | param->len, true); |
| 1773 | else |
| 1774 | result = -EINVAL; |
| 1775 | spin_unlock(&private->mem_lock); |
| 1776 | return result; |
| 1777 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1778 | |
Sushmita Susheelendra | 41f8fa3 | 2011-05-11 17:15:58 -0600 | [diff] [blame] | 1779 | static long kgsl_ioctl_cff_user_event(struct kgsl_device_private *dev_priv, |
| 1780 | unsigned int cmd, void *data) |
| 1781 | { |
| 1782 | int result = 0; |
| 1783 | struct kgsl_cff_user_event *param = data; |
| 1784 | |
| 1785 | kgsl_cffdump_user_event(param->cff_opcode, param->op1, param->op2, |
| 1786 | param->op3, param->op4, param->op5); |
| 1787 | |
| 1788 | return result; |
| 1789 | } |
| 1790 | |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1791 | #ifdef CONFIG_GENLOCK |
| 1792 | struct kgsl_genlock_event_priv { |
| 1793 | struct genlock_handle *handle; |
| 1794 | struct genlock *lock; |
| 1795 | }; |
| 1796 | |
| 1797 | /** |
| 1798 | * kgsl_genlock_event_cb - Event callback for a genlock timestamp event |
| 1799 | * @device - The KGSL device that expired the timestamp |
| 1800 | * @priv - private data for the event |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1801 | * @context_id - the context id that goes with the timestamp |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1802 | * @timestamp - the timestamp that triggered the event |
| 1803 | * |
| 1804 | * Release a genlock lock following the expiration of a timestamp |
| 1805 | */ |
| 1806 | |
| 1807 | static void kgsl_genlock_event_cb(struct kgsl_device *device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1808 | void *priv, u32 context_id, u32 timestamp) |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1809 | { |
| 1810 | struct kgsl_genlock_event_priv *ev = priv; |
| 1811 | int ret; |
| 1812 | |
| 1813 | ret = genlock_lock(ev->handle, GENLOCK_UNLOCK, 0, 0); |
| 1814 | if (ret) |
| 1815 | KGSL_CORE_ERR("Error while unlocking genlock: %d\n", ret); |
| 1816 | |
| 1817 | genlock_put_handle(ev->handle); |
| 1818 | |
| 1819 | kfree(ev); |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * kgsl_add_genlock-event - Create a new genlock event |
| 1824 | * @device - KGSL device to create the event on |
| 1825 | * @timestamp - Timestamp to trigger the event |
| 1826 | * @data - User space buffer containing struct kgsl_genlock_event_priv |
| 1827 | * @len - length of the userspace buffer |
Jeremy Gebben | fd87f9a | 2012-02-10 07:06:09 -0700 | [diff] [blame] | 1828 | * @owner - driver instance that owns this event |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1829 | * @returns 0 on success or error code on error |
| 1830 | * |
| 1831 | * Attack to a genlock handle and register an event to release the |
| 1832 | * genlock lock when the timestamp expires |
| 1833 | */ |
| 1834 | |
| 1835 | static int kgsl_add_genlock_event(struct kgsl_device *device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1836 | u32 context_id, u32 timestamp, void __user *data, int len, |
Jeremy Gebben | fd87f9a | 2012-02-10 07:06:09 -0700 | [diff] [blame] | 1837 | struct kgsl_device_private *owner) |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1838 | { |
| 1839 | struct kgsl_genlock_event_priv *event; |
| 1840 | struct kgsl_timestamp_event_genlock priv; |
| 1841 | int ret; |
| 1842 | |
| 1843 | if (len != sizeof(priv)) |
| 1844 | return -EINVAL; |
| 1845 | |
| 1846 | if (copy_from_user(&priv, data, sizeof(priv))) |
| 1847 | return -EFAULT; |
| 1848 | |
| 1849 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 1850 | |
| 1851 | if (event == NULL) |
| 1852 | return -ENOMEM; |
| 1853 | |
| 1854 | event->handle = genlock_get_handle_fd(priv.handle); |
| 1855 | |
| 1856 | if (IS_ERR(event->handle)) { |
| 1857 | int ret = PTR_ERR(event->handle); |
| 1858 | kfree(event); |
| 1859 | return ret; |
| 1860 | } |
| 1861 | |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1862 | ret = kgsl_add_event(device, context_id, timestamp, |
| 1863 | kgsl_genlock_event_cb, event, owner); |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1864 | if (ret) |
| 1865 | kfree(event); |
| 1866 | |
| 1867 | return ret; |
| 1868 | } |
| 1869 | #else |
| 1870 | static long kgsl_add_genlock_event(struct kgsl_device *device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1871 | u32 context_id, u32 timestamp, void __user *data, int len, |
Jeremy Gebben | fd87f9a | 2012-02-10 07:06:09 -0700 | [diff] [blame] | 1872 | struct kgsl_device_private *owner) |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1873 | { |
| 1874 | return -EINVAL; |
| 1875 | } |
| 1876 | #endif |
| 1877 | |
| 1878 | /** |
| 1879 | * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace |
| 1880 | * @dev_priv - pointer to the private device structure |
| 1881 | * @cmd - the ioctl cmd passed from kgsl_ioctl |
| 1882 | * @data - the user data buffer from kgsl_ioctl |
| 1883 | * @returns 0 on success or error code on failure |
| 1884 | */ |
| 1885 | |
| 1886 | static long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv, |
| 1887 | unsigned int cmd, void *data) |
| 1888 | { |
| 1889 | struct kgsl_timestamp_event *param = data; |
| 1890 | int ret; |
| 1891 | |
| 1892 | switch (param->type) { |
| 1893 | case KGSL_TIMESTAMP_EVENT_GENLOCK: |
| 1894 | ret = kgsl_add_genlock_event(dev_priv->device, |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1895 | param->context_id, param->timestamp, param->priv, |
| 1896 | param->len, dev_priv); |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1897 | break; |
Jeff Boody | fe6c39c | 2012-08-09 13:54:50 -0600 | [diff] [blame] | 1898 | case KGSL_TIMESTAMP_EVENT_FENCE: |
| 1899 | ret = kgsl_add_fence_event(dev_priv->device, |
| 1900 | param->context_id, param->timestamp, param->priv, |
| 1901 | param->len, dev_priv); |
| 1902 | break; |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1903 | default: |
| 1904 | ret = -EINVAL; |
| 1905 | } |
| 1906 | |
| 1907 | return ret; |
| 1908 | } |
| 1909 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1910 | typedef long (*kgsl_ioctl_func_t)(struct kgsl_device_private *, |
| 1911 | unsigned int, void *); |
| 1912 | |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1913 | #define KGSL_IOCTL_FUNC(_cmd, _func, _flags) \ |
| 1914 | [_IOC_NR((_cmd))] = \ |
| 1915 | { .cmd = (_cmd), .func = (_func), .flags = (_flags) } |
| 1916 | |
| 1917 | #define KGSL_IOCTL_LOCK BIT(0) |
| 1918 | #define KGSL_IOCTL_WAKE BIT(1) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1919 | |
| 1920 | static const struct { |
| 1921 | unsigned int cmd; |
| 1922 | kgsl_ioctl_func_t func; |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1923 | int flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1924 | } kgsl_ioctl_funcs[] = { |
| 1925 | KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_GETPROPERTY, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1926 | kgsl_ioctl_device_getproperty, |
| 1927 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1928 | KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_WAITTIMESTAMP, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1929 | kgsl_ioctl_device_waittimestamp, |
| 1930 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1931 | KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1932 | kgsl_ioctl_device_waittimestamp_ctxtid, |
| 1933 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1934 | KGSL_IOCTL_FUNC(IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1935 | kgsl_ioctl_rb_issueibcmds, |
| 1936 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1937 | KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1938 | kgsl_ioctl_cmdstream_readtimestamp, |
| 1939 | KGSL_IOCTL_LOCK), |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1940 | KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_CTXTID, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1941 | kgsl_ioctl_cmdstream_readtimestamp_ctxtid, |
| 1942 | KGSL_IOCTL_LOCK), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1943 | KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1944 | kgsl_ioctl_cmdstream_freememontimestamp, |
| 1945 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Carter Cooper | 7e7f02e | 2012-02-15 09:36:31 -0700 | [diff] [blame] | 1946 | KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP_CTXTID, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1947 | kgsl_ioctl_cmdstream_freememontimestamp_ctxtid, |
| 1948 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1949 | KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_CREATE, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1950 | kgsl_ioctl_drawctxt_create, |
| 1951 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1952 | KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_DESTROY, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1953 | kgsl_ioctl_drawctxt_destroy, |
| 1954 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1955 | KGSL_IOCTL_FUNC(IOCTL_KGSL_MAP_USER_MEM, |
| 1956 | kgsl_ioctl_map_user_mem, 0), |
| 1957 | KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_PMEM, |
| 1958 | kgsl_ioctl_map_user_mem, 0), |
| 1959 | KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FREE, |
| 1960 | kgsl_ioctl_sharedmem_free, 0), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1961 | KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FLUSH_CACHE, |
| 1962 | kgsl_ioctl_sharedmem_flush_cache, 0), |
| 1963 | KGSL_IOCTL_FUNC(IOCTL_KGSL_GPUMEM_ALLOC, |
| 1964 | kgsl_ioctl_gpumem_alloc, 0), |
Jeremy Gebben | a7423e4 | 2011-04-18 15:11:21 -0600 | [diff] [blame] | 1965 | KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_SYNCMEM, |
| 1966 | kgsl_ioctl_cff_syncmem, 0), |
Sushmita Susheelendra | 41f8fa3 | 2011-05-11 17:15:58 -0600 | [diff] [blame] | 1967 | KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_USER_EVENT, |
| 1968 | kgsl_ioctl_cff_user_event, 0), |
Jordan Crouse | d4bc9d2 | 2011-11-17 13:39:21 -0700 | [diff] [blame] | 1969 | KGSL_IOCTL_FUNC(IOCTL_KGSL_TIMESTAMP_EVENT, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1970 | kgsl_ioctl_timestamp_event, |
| 1971 | KGSL_IOCTL_LOCK), |
Jordan Crouse | ed7dd7f | 2012-03-29 13:16:02 -0600 | [diff] [blame] | 1972 | KGSL_IOCTL_FUNC(IOCTL_KGSL_SETPROPERTY, |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1973 | kgsl_ioctl_device_setproperty, |
| 1974 | KGSL_IOCTL_LOCK | KGSL_IOCTL_WAKE) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1975 | }; |
| 1976 | |
| 1977 | static long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
| 1978 | { |
| 1979 | struct kgsl_device_private *dev_priv = filep->private_data; |
Jordan Crouse | 1e76f61 | 2012-08-08 13:24:21 -0600 | [diff] [blame] | 1980 | unsigned int nr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1981 | kgsl_ioctl_func_t func; |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 1982 | int lock, ret, use_hw; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1983 | char ustack[64]; |
| 1984 | void *uptr = NULL; |
| 1985 | |
| 1986 | BUG_ON(dev_priv == NULL); |
| 1987 | |
| 1988 | /* Workaround for an previously incorrectly defined ioctl code. |
| 1989 | This helps ensure binary compatability */ |
| 1990 | |
| 1991 | if (cmd == IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP_OLD) |
| 1992 | cmd = IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP; |
Jason Varbedian | 80ba33d | 2011-07-11 17:29:05 -0700 | [diff] [blame] | 1993 | else if (cmd == IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_OLD) |
| 1994 | cmd = IOCTL_KGSL_CMDSTREAM_READTIMESTAMP; |
Jeff Boody | fe6c39c | 2012-08-09 13:54:50 -0600 | [diff] [blame] | 1995 | else if (cmd == IOCTL_KGSL_TIMESTAMP_EVENT_OLD) |
| 1996 | cmd = IOCTL_KGSL_TIMESTAMP_EVENT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1997 | |
Jordan Crouse | 1e76f61 | 2012-08-08 13:24:21 -0600 | [diff] [blame] | 1998 | nr = _IOC_NR(cmd); |
| 1999 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2000 | if (cmd & (IOC_IN | IOC_OUT)) { |
| 2001 | if (_IOC_SIZE(cmd) < sizeof(ustack)) |
| 2002 | uptr = ustack; |
| 2003 | else { |
| 2004 | uptr = kzalloc(_IOC_SIZE(cmd), GFP_KERNEL); |
| 2005 | if (uptr == NULL) { |
| 2006 | KGSL_MEM_ERR(dev_priv->device, |
| 2007 | "kzalloc(%d) failed\n", _IOC_SIZE(cmd)); |
| 2008 | ret = -ENOMEM; |
| 2009 | goto done; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | if (cmd & IOC_IN) { |
| 2014 | if (copy_from_user(uptr, (void __user *) arg, |
| 2015 | _IOC_SIZE(cmd))) { |
| 2016 | ret = -EFAULT; |
| 2017 | goto done; |
| 2018 | } |
| 2019 | } else |
| 2020 | memset(uptr, 0, _IOC_SIZE(cmd)); |
| 2021 | } |
| 2022 | |
| 2023 | if (nr < ARRAY_SIZE(kgsl_ioctl_funcs) && |
Jordan Crouse | 1e76f61 | 2012-08-08 13:24:21 -0600 | [diff] [blame] | 2024 | kgsl_ioctl_funcs[nr].func != NULL) { |
| 2025 | |
| 2026 | /* |
| 2027 | * Make sure that nobody tried to send us a malformed ioctl code |
| 2028 | * with a valid NR but bogus flags |
| 2029 | */ |
| 2030 | |
| 2031 | if (kgsl_ioctl_funcs[nr].cmd != cmd) { |
| 2032 | KGSL_DRV_ERR(dev_priv->device, |
| 2033 | "Malformed ioctl code %08x\n", cmd); |
| 2034 | ret = -ENOIOCTLCMD; |
| 2035 | goto done; |
| 2036 | } |
| 2037 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2038 | func = kgsl_ioctl_funcs[nr].func; |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 2039 | lock = kgsl_ioctl_funcs[nr].flags & KGSL_IOCTL_LOCK; |
| 2040 | use_hw = kgsl_ioctl_funcs[nr].flags & KGSL_IOCTL_WAKE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2041 | } else { |
| 2042 | func = dev_priv->device->ftbl->ioctl; |
| 2043 | if (!func) { |
| 2044 | KGSL_DRV_INFO(dev_priv->device, |
| 2045 | "invalid ioctl code %08x\n", cmd); |
Jeremy Gebben | c15b461 | 2012-01-09 09:44:11 -0700 | [diff] [blame] | 2046 | ret = -ENOIOCTLCMD; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2047 | goto done; |
| 2048 | } |
| 2049 | lock = 1; |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 2050 | use_hw = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | if (lock) { |
| 2054 | mutex_lock(&dev_priv->device->mutex); |
Vladimir Razgulin | 3834530 | 2013-01-22 18:41:59 -0700 | [diff] [blame] | 2055 | if (use_hw) |
| 2056 | kgsl_check_suspended(dev_priv->device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | ret = func(dev_priv, cmd, uptr); |
| 2060 | |
| 2061 | if (lock) { |
| 2062 | kgsl_check_idle_locked(dev_priv->device); |
| 2063 | mutex_unlock(&dev_priv->device->mutex); |
| 2064 | } |
| 2065 | |
| 2066 | if (ret == 0 && (cmd & IOC_OUT)) { |
| 2067 | if (copy_to_user((void __user *) arg, uptr, _IOC_SIZE(cmd))) |
| 2068 | ret = -EFAULT; |
| 2069 | } |
| 2070 | |
| 2071 | done: |
| 2072 | if (_IOC_SIZE(cmd) >= sizeof(ustack)) |
| 2073 | kfree(uptr); |
| 2074 | |
| 2075 | return ret; |
| 2076 | } |
| 2077 | |
| 2078 | static int |
| 2079 | kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma) |
| 2080 | { |
| 2081 | struct kgsl_memdesc *memdesc = &device->memstore; |
| 2082 | int result; |
| 2083 | unsigned int vma_size = vma->vm_end - vma->vm_start; |
| 2084 | |
| 2085 | /* The memstore can only be mapped as read only */ |
| 2086 | |
| 2087 | if (vma->vm_flags & VM_WRITE) |
| 2088 | return -EPERM; |
| 2089 | |
| 2090 | if (memdesc->size != vma_size) { |
| 2091 | KGSL_MEM_ERR(device, "memstore bad size: %d should be %d\n", |
| 2092 | vma_size, memdesc->size); |
| 2093 | return -EINVAL; |
| 2094 | } |
| 2095 | |
| 2096 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
| 2097 | |
Shubhraprakash Das | 87f6813 | 2012-07-30 23:25:13 -0700 | [diff] [blame] | 2098 | result = remap_pfn_range(vma, vma->vm_start, |
| 2099 | device->memstore.physaddr >> PAGE_SHIFT, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2100 | vma_size, vma->vm_page_prot); |
| 2101 | if (result != 0) |
| 2102 | KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n", |
| 2103 | result); |
| 2104 | |
| 2105 | return result; |
| 2106 | } |
| 2107 | |
Jordan Crouse | 4283e17 | 2011-09-26 14:45:47 -0600 | [diff] [blame] | 2108 | /* |
| 2109 | * kgsl_gpumem_vm_open is called whenever a vma region is copied or split. |
| 2110 | * Increase the refcount to make sure that the accounting stays correct |
| 2111 | */ |
| 2112 | |
| 2113 | static void kgsl_gpumem_vm_open(struct vm_area_struct *vma) |
| 2114 | { |
| 2115 | struct kgsl_mem_entry *entry = vma->vm_private_data; |
| 2116 | kgsl_mem_entry_get(entry); |
| 2117 | } |
| 2118 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2119 | static int |
| 2120 | kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 2121 | { |
| 2122 | struct kgsl_mem_entry *entry = vma->vm_private_data; |
| 2123 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 2124 | if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2125 | return VM_FAULT_SIGBUS; |
| 2126 | |
| 2127 | return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf); |
| 2128 | } |
| 2129 | |
| 2130 | static void |
| 2131 | kgsl_gpumem_vm_close(struct vm_area_struct *vma) |
| 2132 | { |
| 2133 | struct kgsl_mem_entry *entry = vma->vm_private_data; |
| 2134 | kgsl_mem_entry_put(entry); |
| 2135 | } |
| 2136 | |
| 2137 | static struct vm_operations_struct kgsl_gpumem_vm_ops = { |
Jordan Crouse | 4283e17 | 2011-09-26 14:45:47 -0600 | [diff] [blame] | 2138 | .open = kgsl_gpumem_vm_open, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2139 | .fault = kgsl_gpumem_vm_fault, |
| 2140 | .close = kgsl_gpumem_vm_close, |
| 2141 | }; |
| 2142 | |
| 2143 | static int kgsl_mmap(struct file *file, struct vm_area_struct *vma) |
| 2144 | { |
| 2145 | unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2146 | struct kgsl_device_private *dev_priv = file->private_data; |
| 2147 | struct kgsl_process_private *private = dev_priv->process_priv; |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 2148 | struct kgsl_mem_entry *entry = NULL; |
Jordan Crouse | 2db0af9 | 2011-08-08 16:05:09 -0600 | [diff] [blame] | 2149 | struct kgsl_device *device = dev_priv->device; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2150 | |
| 2151 | /* Handle leagacy behavior for memstore */ |
| 2152 | |
Shubhraprakash Das | 87f6813 | 2012-07-30 23:25:13 -0700 | [diff] [blame] | 2153 | if (vma_offset == device->memstore.gpuaddr) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2154 | return kgsl_mmap_memstore(device, vma); |
| 2155 | |
| 2156 | /* Find a chunk of GPU memory */ |
| 2157 | |
| 2158 | spin_lock(&private->mem_lock); |
Jordan Crouse | c9559e4 | 2012-04-05 16:55:56 -0600 | [diff] [blame] | 2159 | entry = kgsl_sharedmem_find(private, vma_offset); |
| 2160 | |
| 2161 | if (entry) |
| 2162 | kgsl_mem_entry_get(entry); |
| 2163 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2164 | spin_unlock(&private->mem_lock); |
| 2165 | |
| 2166 | if (entry == NULL) |
| 2167 | return -EINVAL; |
| 2168 | |
Jordan Crouse | d17e9aa | 2011-10-12 16:57:48 -0600 | [diff] [blame] | 2169 | if (!entry->memdesc.ops || |
| 2170 | !entry->memdesc.ops->vmflags || |
| 2171 | !entry->memdesc.ops->vmfault) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2172 | return -EINVAL; |
| 2173 | |
| 2174 | vma->vm_flags |= entry->memdesc.ops->vmflags(&entry->memdesc); |
| 2175 | |
| 2176 | vma->vm_private_data = entry; |
| 2177 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 2178 | vma->vm_ops = &kgsl_gpumem_vm_ops; |
| 2179 | vma->vm_file = file; |
| 2180 | |
| 2181 | return 0; |
| 2182 | } |
| 2183 | |
Jordan Crouse | b368e9b | 2012-04-27 14:01:59 -0600 | [diff] [blame] | 2184 | static irqreturn_t kgsl_irq_handler(int irq, void *data) |
| 2185 | { |
| 2186 | struct kgsl_device *device = data; |
| 2187 | |
| 2188 | return device->ftbl->irq_handler(device); |
| 2189 | |
| 2190 | } |
| 2191 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2192 | static const struct file_operations kgsl_fops = { |
| 2193 | .owner = THIS_MODULE, |
| 2194 | .release = kgsl_release, |
| 2195 | .open = kgsl_open, |
| 2196 | .mmap = kgsl_mmap, |
| 2197 | .unlocked_ioctl = kgsl_ioctl, |
| 2198 | }; |
| 2199 | |
| 2200 | struct kgsl_driver kgsl_driver = { |
| 2201 | .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex), |
| 2202 | .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock), |
| 2203 | .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock), |
| 2204 | }; |
| 2205 | EXPORT_SYMBOL(kgsl_driver); |
| 2206 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2207 | static void _unregister_device(struct kgsl_device *device) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2208 | { |
| 2209 | int minor; |
| 2210 | |
| 2211 | mutex_lock(&kgsl_driver.devlock); |
| 2212 | for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) { |
| 2213 | if (device == kgsl_driver.devp[minor]) |
| 2214 | break; |
| 2215 | } |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2216 | if (minor != KGSL_DEVICE_MAX) { |
| 2217 | device_destroy(kgsl_driver.class, |
| 2218 | MKDEV(MAJOR(kgsl_driver.major), minor)); |
| 2219 | kgsl_driver.devp[minor] = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2220 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2221 | mutex_unlock(&kgsl_driver.devlock); |
| 2222 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2223 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2224 | static int _register_device(struct kgsl_device *device) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2225 | { |
| 2226 | int minor, ret; |
| 2227 | dev_t dev; |
| 2228 | |
| 2229 | /* Find a minor for the device */ |
| 2230 | |
| 2231 | mutex_lock(&kgsl_driver.devlock); |
| 2232 | for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) { |
| 2233 | if (kgsl_driver.devp[minor] == NULL) { |
| 2234 | kgsl_driver.devp[minor] = device; |
| 2235 | break; |
| 2236 | } |
| 2237 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2238 | mutex_unlock(&kgsl_driver.devlock); |
| 2239 | |
| 2240 | if (minor == KGSL_DEVICE_MAX) { |
| 2241 | KGSL_CORE_ERR("minor devices exhausted\n"); |
| 2242 | return -ENODEV; |
| 2243 | } |
| 2244 | |
| 2245 | /* Create the device */ |
| 2246 | dev = MKDEV(MAJOR(kgsl_driver.major), minor); |
| 2247 | device->dev = device_create(kgsl_driver.class, |
| 2248 | device->parentdev, |
| 2249 | dev, device, |
| 2250 | device->name); |
| 2251 | |
| 2252 | if (IS_ERR(device->dev)) { |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2253 | mutex_lock(&kgsl_driver.devlock); |
| 2254 | kgsl_driver.devp[minor] = NULL; |
| 2255 | mutex_unlock(&kgsl_driver.devlock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2256 | ret = PTR_ERR(device->dev); |
| 2257 | KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret); |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2258 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | dev_set_drvdata(device->parentdev, device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2262 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2263 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2264 | |
Jordan Crouse | b368e9b | 2012-04-27 14:01:59 -0600 | [diff] [blame] | 2265 | int kgsl_device_platform_probe(struct kgsl_device *device) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2266 | { |
Michael Street | 8bacdd0 | 2012-01-05 14:55:01 -0800 | [diff] [blame] | 2267 | int result; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2268 | int status = -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2269 | struct resource *res; |
| 2270 | struct platform_device *pdev = |
| 2271 | container_of(device->parentdev, struct platform_device, dev); |
| 2272 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2273 | status = _register_device(device); |
| 2274 | if (status) |
| 2275 | return status; |
| 2276 | |
| 2277 | /* Initialize logging first, so that failures below actually print. */ |
| 2278 | kgsl_device_debugfs_init(device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2279 | |
| 2280 | status = kgsl_pwrctrl_init(device); |
| 2281 | if (status) |
| 2282 | goto error; |
| 2283 | |
Harsh Vardhan Dwivedi | f48af7f | 2012-04-13 12:50:44 -0600 | [diff] [blame] | 2284 | kgsl_ion_client = msm_ion_client_create(UINT_MAX, KGSL_NAME); |
| 2285 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2286 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 2287 | device->iomemname); |
| 2288 | if (res == NULL) { |
| 2289 | KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n"); |
| 2290 | status = -EINVAL; |
| 2291 | goto error_pwrctrl_close; |
| 2292 | } |
| 2293 | if (res->start == 0 || resource_size(res) == 0) { |
Jordan Crouse | 7501d45 | 2012-04-19 08:58:44 -0600 | [diff] [blame] | 2294 | KGSL_DRV_ERR(device, "dev %d invalid register region\n", |
| 2295 | device->id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2296 | status = -EINVAL; |
| 2297 | goto error_pwrctrl_close; |
| 2298 | } |
| 2299 | |
Jordan Crouse | 7501d45 | 2012-04-19 08:58:44 -0600 | [diff] [blame] | 2300 | device->reg_phys = res->start; |
| 2301 | device->reg_len = resource_size(res); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2302 | |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2303 | if (!devm_request_mem_region(device->dev, device->reg_phys, |
| 2304 | device->reg_len, device->name)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2305 | KGSL_DRV_ERR(device, "request_mem_region failed\n"); |
| 2306 | status = -ENODEV; |
| 2307 | goto error_pwrctrl_close; |
| 2308 | } |
| 2309 | |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2310 | device->reg_virt = devm_ioremap(device->dev, device->reg_phys, |
| 2311 | device->reg_len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2312 | |
Jordan Crouse | 7501d45 | 2012-04-19 08:58:44 -0600 | [diff] [blame] | 2313 | if (device->reg_virt == NULL) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2314 | KGSL_DRV_ERR(device, "ioremap failed\n"); |
| 2315 | status = -ENODEV; |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2316 | goto error_pwrctrl_close; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2317 | } |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2318 | /*acquire interrupt */ |
| 2319 | device->pwrctrl.interrupt_num = |
| 2320 | platform_get_irq_byname(pdev, device->pwrctrl.irq_name); |
| 2321 | |
| 2322 | if (device->pwrctrl.interrupt_num <= 0) { |
| 2323 | KGSL_DRV_ERR(device, "platform_get_irq_byname failed: %d\n", |
| 2324 | device->pwrctrl.interrupt_num); |
| 2325 | status = -EINVAL; |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2326 | goto error_pwrctrl_close; |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2327 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2328 | |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2329 | status = devm_request_irq(device->dev, device->pwrctrl.interrupt_num, |
| 2330 | kgsl_irq_handler, IRQF_TRIGGER_HIGH, |
| 2331 | device->name, device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2332 | if (status) { |
| 2333 | KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n", |
| 2334 | device->pwrctrl.interrupt_num, status); |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2335 | goto error_pwrctrl_close; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2336 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2337 | disable_irq(device->pwrctrl.interrupt_num); |
| 2338 | |
| 2339 | KGSL_DRV_INFO(device, |
Jordan Crouse | 7501d45 | 2012-04-19 08:58:44 -0600 | [diff] [blame] | 2340 | "dev_id %d regs phys 0x%08lx size 0x%08x virt %p\n", |
| 2341 | device->id, device->reg_phys, device->reg_len, |
| 2342 | device->reg_virt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2343 | |
Michael Street | 8bacdd0 | 2012-01-05 14:55:01 -0800 | [diff] [blame] | 2344 | result = kgsl_drm_init(pdev); |
| 2345 | if (result) |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2346 | goto error_pwrctrl_close; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2347 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2348 | kgsl_cffdump_open(device->id); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2349 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2350 | setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device); |
| 2351 | status = kgsl_create_device_workqueue(device); |
| 2352 | if (status) |
Jeremy Gebben | 4204d0f | 2012-03-01 16:06:21 -0700 | [diff] [blame] | 2353 | goto error_pwrctrl_close; |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2354 | |
| 2355 | status = kgsl_mmu_init(device); |
| 2356 | if (status != 0) { |
| 2357 | KGSL_DRV_ERR(device, "kgsl_mmu_init failed %d\n", status); |
| 2358 | goto error_dest_work_q; |
| 2359 | } |
| 2360 | |
| 2361 | status = kgsl_allocate_contiguous(&device->memstore, |
Richard Ruigrok | 2ad5e9d | 2012-06-14 14:22:05 -0700 | [diff] [blame] | 2362 | KGSL_MEMSTORE_SIZE); |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2363 | |
| 2364 | if (status != 0) { |
| 2365 | KGSL_DRV_ERR(device, "kgsl_allocate_contiguous failed %d\n", |
| 2366 | status); |
| 2367 | goto error_close_mmu; |
| 2368 | } |
| 2369 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2370 | pm_qos_add_request(&device->pm_qos_req_dma, PM_QOS_CPU_DMA_LATENCY, |
| 2371 | PM_QOS_DEFAULT_VALUE); |
| 2372 | |
| 2373 | /* Initalize the snapshot engine */ |
| 2374 | kgsl_device_snapshot_init(device); |
| 2375 | |
| 2376 | /* Initialize common sysfs entries */ |
| 2377 | kgsl_pwrctrl_init_sysfs(device); |
| 2378 | |
| 2379 | return 0; |
| 2380 | |
| 2381 | error_close_mmu: |
| 2382 | kgsl_mmu_close(device); |
| 2383 | error_dest_work_q: |
| 2384 | destroy_workqueue(device->work_queue); |
| 2385 | device->work_queue = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2386 | error_pwrctrl_close: |
| 2387 | kgsl_pwrctrl_close(device); |
| 2388 | error: |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2389 | _unregister_device(device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2390 | return status; |
| 2391 | } |
| 2392 | EXPORT_SYMBOL(kgsl_device_platform_probe); |
| 2393 | |
Harsh Vardhan Dwivedi | 715fb83 | 2012-05-18 00:24:18 -0600 | [diff] [blame] | 2394 | int kgsl_postmortem_dump(struct kgsl_device *device, int manual) |
| 2395 | { |
| 2396 | bool saved_nap; |
| 2397 | struct kgsl_pwrctrl *pwr = &device->pwrctrl; |
| 2398 | |
| 2399 | BUG_ON(device == NULL); |
| 2400 | |
| 2401 | kgsl_cffdump_hang(device->id); |
| 2402 | |
| 2403 | /* For a manual dump, make sure that the system is idle */ |
| 2404 | |
| 2405 | if (manual) { |
| 2406 | if (device->active_cnt != 0) { |
| 2407 | mutex_unlock(&device->mutex); |
| 2408 | wait_for_completion(&device->suspend_gate); |
| 2409 | mutex_lock(&device->mutex); |
| 2410 | } |
| 2411 | |
| 2412 | if (device->state == KGSL_STATE_ACTIVE) |
Jordan Crouse | a29a2e0 | 2012-08-14 09:09:23 -0600 | [diff] [blame] | 2413 | kgsl_idle(device); |
Harsh Vardhan Dwivedi | 715fb83 | 2012-05-18 00:24:18 -0600 | [diff] [blame] | 2414 | |
| 2415 | } |
| 2416 | KGSL_LOG_DUMP(device, "|%s| Dump Started\n", device->name); |
| 2417 | KGSL_LOG_DUMP(device, "POWER: FLAGS = %08lX | ACTIVE POWERLEVEL = %08X", |
| 2418 | pwr->power_flags, pwr->active_pwrlevel); |
| 2419 | |
| 2420 | KGSL_LOG_DUMP(device, "POWER: INTERVAL TIMEOUT = %08X ", |
| 2421 | pwr->interval_timeout); |
| 2422 | |
| 2423 | KGSL_LOG_DUMP(device, "GRP_CLK = %lu ", |
| 2424 | kgsl_get_clkrate(pwr->grp_clks[0])); |
| 2425 | |
| 2426 | KGSL_LOG_DUMP(device, "BUS CLK = %lu ", |
| 2427 | kgsl_get_clkrate(pwr->ebi1_clk)); |
| 2428 | |
| 2429 | /* Disable the idle timer so we don't get interrupted */ |
| 2430 | del_timer_sync(&device->idle_timer); |
| 2431 | mutex_unlock(&device->mutex); |
| 2432 | flush_workqueue(device->work_queue); |
| 2433 | mutex_lock(&device->mutex); |
| 2434 | |
| 2435 | /* Turn off napping to make sure we have the clocks full |
| 2436 | attention through the following process */ |
| 2437 | saved_nap = device->pwrctrl.nap_allowed; |
| 2438 | device->pwrctrl.nap_allowed = false; |
| 2439 | |
| 2440 | /* Force on the clocks */ |
| 2441 | kgsl_pwrctrl_wake(device); |
| 2442 | |
| 2443 | /* Disable the irq */ |
| 2444 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_OFF); |
| 2445 | |
| 2446 | /*Call the device specific postmortem dump function*/ |
| 2447 | device->ftbl->postmortem_dump(device, manual); |
| 2448 | |
| 2449 | /* Restore nap mode */ |
| 2450 | device->pwrctrl.nap_allowed = saved_nap; |
| 2451 | |
| 2452 | /* On a manual trigger, turn on the interrupts and put |
| 2453 | the clocks to sleep. They will recover themselves |
| 2454 | on the next event. For a hang, leave things as they |
| 2455 | are until recovery kicks in. */ |
| 2456 | |
| 2457 | if (manual) { |
| 2458 | kgsl_pwrctrl_irq(device, KGSL_PWRFLAGS_ON); |
| 2459 | |
| 2460 | /* try to go into a sleep mode until the next event */ |
| 2461 | kgsl_pwrctrl_request_state(device, KGSL_STATE_SLEEP); |
| 2462 | kgsl_pwrctrl_sleep(device); |
| 2463 | } |
| 2464 | |
| 2465 | KGSL_LOG_DUMP(device, "|%s| Dump Finished\n", device->name); |
| 2466 | |
| 2467 | return 0; |
| 2468 | } |
| 2469 | EXPORT_SYMBOL(kgsl_postmortem_dump); |
| 2470 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2471 | void kgsl_device_platform_remove(struct kgsl_device *device) |
| 2472 | { |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2473 | kgsl_device_snapshot_close(device); |
| 2474 | |
| 2475 | kgsl_cffdump_close(device->id); |
| 2476 | kgsl_pwrctrl_uninit_sysfs(device); |
| 2477 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2478 | pm_qos_remove_request(&device->pm_qos_req_dma); |
| 2479 | |
| 2480 | idr_destroy(&device->context_idr); |
| 2481 | |
| 2482 | kgsl_sharedmem_free(&device->memstore); |
| 2483 | |
| 2484 | kgsl_mmu_close(device); |
| 2485 | |
| 2486 | if (device->work_queue) { |
| 2487 | destroy_workqueue(device->work_queue); |
| 2488 | device->work_queue = NULL; |
| 2489 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2490 | kgsl_pwrctrl_close(device); |
| 2491 | |
Jeremy Gebben | 4f5f0de | 2012-03-01 15:51:37 -0700 | [diff] [blame] | 2492 | _unregister_device(device); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2493 | } |
| 2494 | EXPORT_SYMBOL(kgsl_device_platform_remove); |
| 2495 | |
| 2496 | static int __devinit |
| 2497 | kgsl_ptdata_init(void) |
| 2498 | { |
Jordan Crouse | 6d76c4d | 2012-03-26 09:50:43 -0600 | [diff] [blame] | 2499 | kgsl_driver.ptpool = kgsl_mmu_ptpool_init(kgsl_pagetable_count); |
| 2500 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 2501 | if (!kgsl_driver.ptpool) |
| 2502 | return -ENOMEM; |
| 2503 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | static void kgsl_core_exit(void) |
| 2507 | { |
Ranjhith Kalisamy | 4ad59e9 | 2012-05-31 19:15:11 +0530 | [diff] [blame] | 2508 | kgsl_mmu_ptpool_destroy(kgsl_driver.ptpool); |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 2509 | kgsl_driver.ptpool = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2510 | |
Ranjhith Kalisamy | dad9df5 | 2012-06-01 17:05:13 +0530 | [diff] [blame] | 2511 | kgsl_drm_exit(); |
| 2512 | kgsl_cffdump_destroy(); |
| 2513 | kgsl_core_debugfs_close(); |
Ranjhith Kalisamy | dad9df5 | 2012-06-01 17:05:13 +0530 | [diff] [blame] | 2514 | |
Harsh Vardhan Dwivedi | efa6b01 | 2012-06-15 13:02:27 -0600 | [diff] [blame] | 2515 | /* |
| 2516 | * We call kgsl_sharedmem_uninit_sysfs() and device_unregister() |
| 2517 | * only if kgsl_driver.virtdev has been populated. |
| 2518 | * We check at least one member of kgsl_driver.virtdev to |
| 2519 | * see if it is not NULL (and thus, has been populated). |
| 2520 | */ |
| 2521 | if (kgsl_driver.virtdev.class) { |
| 2522 | kgsl_sharedmem_uninit_sysfs(); |
| 2523 | device_unregister(&kgsl_driver.virtdev); |
| 2524 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2525 | |
| 2526 | if (kgsl_driver.class) { |
| 2527 | class_destroy(kgsl_driver.class); |
| 2528 | kgsl_driver.class = NULL; |
| 2529 | } |
| 2530 | |
Ranjhith Kalisamy | dad9df5 | 2012-06-01 17:05:13 +0530 | [diff] [blame] | 2531 | unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | static int __init kgsl_core_init(void) |
| 2535 | { |
| 2536 | int result = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2537 | /* alloc major and minor device numbers */ |
| 2538 | result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX, |
| 2539 | KGSL_NAME); |
| 2540 | if (result < 0) { |
| 2541 | KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result); |
| 2542 | goto err; |
| 2543 | } |
| 2544 | |
| 2545 | cdev_init(&kgsl_driver.cdev, &kgsl_fops); |
| 2546 | kgsl_driver.cdev.owner = THIS_MODULE; |
| 2547 | kgsl_driver.cdev.ops = &kgsl_fops; |
| 2548 | result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0), |
| 2549 | KGSL_DEVICE_MAX); |
| 2550 | |
| 2551 | if (result) { |
| 2552 | KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d," |
| 2553 | " result= %d\n", kgsl_driver.major, result); |
| 2554 | goto err; |
| 2555 | } |
| 2556 | |
| 2557 | kgsl_driver.class = class_create(THIS_MODULE, KGSL_NAME); |
| 2558 | |
| 2559 | if (IS_ERR(kgsl_driver.class)) { |
| 2560 | result = PTR_ERR(kgsl_driver.class); |
| 2561 | KGSL_CORE_ERR("failed to create class %s", KGSL_NAME); |
| 2562 | goto err; |
| 2563 | } |
| 2564 | |
| 2565 | /* Make a virtual device for managing core related things |
| 2566 | in sysfs */ |
| 2567 | kgsl_driver.virtdev.class = kgsl_driver.class; |
| 2568 | dev_set_name(&kgsl_driver.virtdev, "kgsl"); |
| 2569 | result = device_register(&kgsl_driver.virtdev); |
| 2570 | if (result) { |
| 2571 | KGSL_CORE_ERR("driver_register failed\n"); |
| 2572 | goto err; |
| 2573 | } |
| 2574 | |
| 2575 | /* Make kobjects in the virtual device for storing statistics */ |
| 2576 | |
| 2577 | kgsl_driver.ptkobj = |
| 2578 | kobject_create_and_add("pagetables", |
| 2579 | &kgsl_driver.virtdev.kobj); |
| 2580 | |
| 2581 | kgsl_driver.prockobj = |
| 2582 | kobject_create_and_add("proc", |
| 2583 | &kgsl_driver.virtdev.kobj); |
| 2584 | |
| 2585 | kgsl_core_debugfs_init(); |
| 2586 | |
| 2587 | kgsl_sharedmem_init_sysfs(); |
| 2588 | kgsl_cffdump_init(); |
| 2589 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2590 | INIT_LIST_HEAD(&kgsl_driver.process_list); |
| 2591 | |
Shubhraprakash Das | 767fdda | 2011-08-15 15:49:45 -0600 | [diff] [blame] | 2592 | INIT_LIST_HEAD(&kgsl_driver.pagetable_list); |
| 2593 | |
| 2594 | kgsl_mmu_set_mmutype(ksgl_mmu_type); |
| 2595 | |
| 2596 | if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype()) { |
| 2597 | result = kgsl_ptdata_init(); |
| 2598 | if (result) |
| 2599 | goto err; |
| 2600 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2601 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2602 | return 0; |
| 2603 | |
| 2604 | err: |
| 2605 | kgsl_core_exit(); |
| 2606 | return result; |
| 2607 | } |
| 2608 | |
| 2609 | module_init(kgsl_core_init); |
| 2610 | module_exit(kgsl_core_exit); |
| 2611 | |
| 2612 | MODULE_AUTHOR("Qualcomm Innovation Center, Inc."); |
| 2613 | MODULE_DESCRIPTION("MSM GPU driver"); |
| 2614 | MODULE_LICENSE("GPL"); |