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