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