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