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