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