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