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