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