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