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