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