blob: 34e83a0dc4017d7dea0620133e1f654a9a035eac [file] [log] [blame]
Tarun Karraf8e5cd22012-01-09 14:10:09 -07001/* Copyright (c) 2008-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700305 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 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;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700398 kgsl_pwrctrl_request_state(device, KGSL_STATE_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 /* 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 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700406 switch (device->state) {
407 case KGSL_STATE_INIT:
408 break;
409 case KGSL_STATE_ACTIVE:
410 /* Wait for the device to become idle */
411 device->ftbl->idle(device, KGSL_TIMEOUT_DEFAULT);
412 case KGSL_STATE_NAP:
413 case KGSL_STATE_SLEEP:
414 /* Get the completion ready to be waited upon. */
415 INIT_COMPLETION(device->hwaccess_gate);
416 device->ftbl->suspend_context(device);
Tarun Karraf8e5cd22012-01-09 14:10:09 -0700417 kgsl_pwrctrl_stop_work(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700418 device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700419 kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 break;
Suman Tatiraju24569022011-10-27 11:11:12 -0700421 case KGSL_STATE_SLUMBER:
422 INIT_COMPLETION(device->hwaccess_gate);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700423 kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);
Suman Tatiraju24569022011-10-27 11:11:12 -0700424 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425 default:
426 KGSL_PWR_ERR(device, "suspend fail, device %d\n",
427 device->id);
428 goto end;
429 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700430 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 device->pwrctrl.nap_allowed = nap_allowed_saved;
432 device->pwrscale.policy = policy_saved;
433 status = 0;
434
435end:
436 mutex_unlock(&device->mutex);
437 KGSL_PWR_WARN(device, "suspend end\n");
438 return status;
439}
440
441static int kgsl_resume_device(struct kgsl_device *device)
442{
443 int status = -EINVAL;
444
445 if (!device)
446 return -EINVAL;
447
448 KGSL_PWR_WARN(device, "resume start\n");
449 mutex_lock(&device->mutex);
450 if (device->state == KGSL_STATE_SUSPEND) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700451 kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju24569022011-10-27 11:11:12 -0700452 status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453 complete_all(&device->hwaccess_gate);
454 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700455 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 KGSL_PWR_WARN(device, "resume end\n");
459 return status;
460}
461
462static int kgsl_suspend(struct device *dev)
463{
464
465 pm_message_t arg = {0};
466 struct kgsl_device *device = dev_get_drvdata(dev);
467 return kgsl_suspend_device(device, arg);
468}
469
470static int kgsl_resume(struct device *dev)
471{
472 struct kgsl_device *device = dev_get_drvdata(dev);
473 return kgsl_resume_device(device);
474}
475
476static int kgsl_runtime_suspend(struct device *dev)
477{
478 return 0;
479}
480
481static int kgsl_runtime_resume(struct device *dev)
482{
483 return 0;
484}
485
486const struct dev_pm_ops kgsl_pm_ops = {
487 .suspend = kgsl_suspend,
488 .resume = kgsl_resume,
489 .runtime_suspend = kgsl_runtime_suspend,
490 .runtime_resume = kgsl_runtime_resume,
491};
492EXPORT_SYMBOL(kgsl_pm_ops);
493
494void kgsl_early_suspend_driver(struct early_suspend *h)
495{
496 struct kgsl_device *device = container_of(h,
497 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700498 KGSL_PWR_WARN(device, "early suspend start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530499 mutex_lock(&device->mutex);
Tarun Karraf8e5cd22012-01-09 14:10:09 -0700500 kgsl_pwrctrl_stop_work(device);
Lucille Sylvester344e4622012-01-18 15:53:21 -0700501 kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju24569022011-10-27 11:11:12 -0700502 kgsl_pwrctrl_sleep(device);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530503 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700504 KGSL_PWR_WARN(device, "early suspend end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700505}
506EXPORT_SYMBOL(kgsl_early_suspend_driver);
507
508int kgsl_suspend_driver(struct platform_device *pdev,
509 pm_message_t state)
510{
511 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
512 return kgsl_suspend_device(device, state);
513}
514EXPORT_SYMBOL(kgsl_suspend_driver);
515
516int kgsl_resume_driver(struct platform_device *pdev)
517{
518 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
519 return kgsl_resume_device(device);
520}
521EXPORT_SYMBOL(kgsl_resume_driver);
522
523void kgsl_late_resume_driver(struct early_suspend *h)
524{
525 struct kgsl_device *device = container_of(h,
526 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700527 KGSL_PWR_WARN(device, "late resume start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530528 mutex_lock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700529 kgsl_pwrctrl_wake(device);
530 device->pwrctrl.restore_slumber = 0;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700531 kgsl_pwrctrl_pwrlevel_change(device, KGSL_PWRLEVEL_TURBO);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530532 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700533 kgsl_check_idle(device);
534 KGSL_PWR_WARN(device, "late resume end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535}
536EXPORT_SYMBOL(kgsl_late_resume_driver);
537
538/* file operations */
539static struct kgsl_process_private *
540kgsl_get_process_private(struct kgsl_device_private *cur_dev_priv)
541{
542 struct kgsl_process_private *private;
543
544 mutex_lock(&kgsl_driver.process_mutex);
545 list_for_each_entry(private, &kgsl_driver.process_list, list) {
546 if (private->pid == task_tgid_nr(current)) {
547 private->refcnt++;
548 goto out;
549 }
550 }
551
552 /* no existing process private found for this dev_priv, create one */
553 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
554 if (private == NULL) {
555 KGSL_DRV_ERR(cur_dev_priv->device, "kzalloc(%d) failed\n",
556 sizeof(struct kgsl_process_private));
557 goto out;
558 }
559
560 spin_lock_init(&private->mem_lock);
561 private->refcnt = 1;
562 private->pid = task_tgid_nr(current);
563
564 INIT_LIST_HEAD(&private->mem_list);
565
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600566 if (kgsl_mmu_enabled())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 {
568 unsigned long pt_name;
569
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 pt_name = task_tgid_nr(current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 private->pagetable = kgsl_mmu_getpagetable(pt_name);
572 if (private->pagetable == NULL) {
573 kfree(private);
574 private = NULL;
575 goto out;
576 }
577 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700578
579 list_add(&private->list, &kgsl_driver.process_list);
580
581 kgsl_process_init_sysfs(private);
582
583out:
584 mutex_unlock(&kgsl_driver.process_mutex);
585 return private;
586}
587
588static void
589kgsl_put_process_private(struct kgsl_device *device,
590 struct kgsl_process_private *private)
591{
592 struct kgsl_mem_entry *entry = NULL;
593 struct kgsl_mem_entry *entry_tmp = NULL;
594
595 if (!private)
596 return;
597
598 mutex_lock(&kgsl_driver.process_mutex);
599
600 if (--private->refcnt)
601 goto unlock;
602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 kgsl_process_uninit_sysfs(private);
604
605 list_del(&private->list);
606
607 list_for_each_entry_safe(entry, entry_tmp, &private->mem_list, list) {
608 list_del(&entry->list);
609 kgsl_mem_entry_put(entry);
610 }
611
612 kgsl_mmu_putpagetable(private->pagetable);
613 kfree(private);
614unlock:
615 mutex_unlock(&kgsl_driver.process_mutex);
616}
617
618static int kgsl_release(struct inode *inodep, struct file *filep)
619{
620 int result = 0;
Jordan Crouse2db0af92011-08-08 16:05:09 -0600621 struct kgsl_device_private *dev_priv = filep->private_data;
622 struct kgsl_process_private *private = dev_priv->process_priv;
623 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 struct kgsl_context *context;
625 int next = 0;
626
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 filep->private_data = NULL;
628
629 mutex_lock(&device->mutex);
630 kgsl_check_suspended(device);
631
632 while (1) {
Jordan Crouse2db0af92011-08-08 16:05:09 -0600633 context = idr_get_next(&device->context_idr, &next);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634 if (context == NULL)
635 break;
636
637 if (context->dev_priv == dev_priv) {
638 device->ftbl->drawctxt_destroy(device, context);
639 kgsl_destroy_context(dev_priv, context);
640 }
641
642 next = next + 1;
643 }
644
645 device->open_count--;
646 if (device->open_count == 0) {
Tarun Karraf8e5cd22012-01-09 14:10:09 -0700647 kgsl_pwrctrl_stop_work(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 result = device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700649 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 }
651 /* clean up any to-be-freed entries that belong to this
652 * process and this device
653 */
654 kgsl_memqueue_cleanup(device, private);
655
656 mutex_unlock(&device->mutex);
657 kfree(dev_priv);
658
659 kgsl_put_process_private(device, private);
660
661 pm_runtime_put(device->parentdev);
662 return result;
663}
664
665static int kgsl_open(struct inode *inodep, struct file *filep)
666{
667 int result;
668 struct kgsl_device_private *dev_priv;
669 struct kgsl_device *device;
670 unsigned int minor = iminor(inodep);
671
672 device = kgsl_get_minor(minor);
673 BUG_ON(device == NULL);
674
675 if (filep->f_flags & O_EXCL) {
676 KGSL_DRV_ERR(device, "O_EXCL not allowed\n");
677 return -EBUSY;
678 }
679
680 result = pm_runtime_get_sync(device->parentdev);
681 if (result < 0) {
682 KGSL_DRV_ERR(device,
683 "Runtime PM: Unable to wake up the device, rc = %d\n",
684 result);
685 return result;
686 }
687 result = 0;
688
689 dev_priv = kzalloc(sizeof(struct kgsl_device_private), GFP_KERNEL);
690 if (dev_priv == NULL) {
691 KGSL_DRV_ERR(device, "kzalloc failed(%d)\n",
692 sizeof(struct kgsl_device_private));
693 result = -ENOMEM;
694 goto err_pmruntime;
695 }
696
697 dev_priv->device = device;
698 filep->private_data = dev_priv;
699
700 /* Get file (per process) private struct */
701 dev_priv->process_priv = kgsl_get_process_private(dev_priv);
702 if (dev_priv->process_priv == NULL) {
703 result = -ENOMEM;
704 goto err_freedevpriv;
705 }
706
707 mutex_lock(&device->mutex);
708 kgsl_check_suspended(device);
709
710 if (device->open_count == 0) {
711 result = device->ftbl->start(device, true);
712
713 if (result) {
714 mutex_unlock(&device->mutex);
715 goto err_putprocess;
716 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700717 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 }
719 device->open_count++;
720 mutex_unlock(&device->mutex);
721
722 KGSL_DRV_INFO(device, "Initialized %s: mmu=%s pagetable_count=%d\n",
723 device->name, kgsl_mmu_enabled() ? "on" : "off",
724 kgsl_pagetable_count);
725
726 return result;
727
728err_putprocess:
729 kgsl_put_process_private(device, dev_priv->process_priv);
730err_freedevpriv:
731 filep->private_data = NULL;
732 kfree(dev_priv);
733err_pmruntime:
734 pm_runtime_put(device->parentdev);
735 return result;
736}
737
738
739/*call with private->mem_lock locked */
740static struct kgsl_mem_entry *
741kgsl_sharedmem_find(struct kgsl_process_private *private, unsigned int gpuaddr)
742{
743 struct kgsl_mem_entry *entry = NULL, *result = NULL;
744
745 BUG_ON(private == NULL);
746
747 gpuaddr &= PAGE_MASK;
748
749 list_for_each_entry(entry, &private->mem_list, list) {
750 if (entry->memdesc.gpuaddr == gpuaddr) {
751 result = entry;
752 break;
753 }
754 }
755 return result;
756}
757
758/*call with private->mem_lock locked */
759struct kgsl_mem_entry *
760kgsl_sharedmem_find_region(struct kgsl_process_private *private,
761 unsigned int gpuaddr,
762 size_t size)
763{
764 struct kgsl_mem_entry *entry = NULL, *result = NULL;
765
766 BUG_ON(private == NULL);
767
768 list_for_each_entry(entry, &private->mem_list, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700769 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 result = entry;
771 break;
772 }
773 }
774
775 return result;
776}
777EXPORT_SYMBOL(kgsl_sharedmem_find_region);
778
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779/*call all ioctl sub functions with driver locked*/
780static long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
781 unsigned int cmd, void *data)
782{
783 int result = 0;
784 struct kgsl_device_getproperty *param = data;
785
786 switch (param->type) {
787 case KGSL_PROP_VERSION:
788 {
789 struct kgsl_version version;
790 if (param->sizebytes != sizeof(version)) {
791 result = -EINVAL;
792 break;
793 }
794
795 version.drv_major = KGSL_VERSION_MAJOR;
796 version.drv_minor = KGSL_VERSION_MINOR;
797 version.dev_major = dev_priv->device->ver_major;
798 version.dev_minor = dev_priv->device->ver_minor;
799
800 if (copy_to_user(param->value, &version, sizeof(version)))
801 result = -EFAULT;
802
803 break;
804 }
805 default:
806 result = dev_priv->device->ftbl->getproperty(
807 dev_priv->device, param->type,
808 param->value, param->sizebytes);
809 }
810
811
812 return result;
813}
814
815static long kgsl_ioctl_device_waittimestamp(struct kgsl_device_private
816 *dev_priv, unsigned int cmd,
817 void *data)
818{
819 int result = 0;
820 struct kgsl_device_waittimestamp *param = data;
821
822 /* Set the active count so that suspend doesn't do the
823 wrong thing */
824
825 dev_priv->device->active_cnt++;
826
Norman Geed7402ff2011-10-28 08:51:11 -0600827 trace_kgsl_waittimestamp_entry(dev_priv->device, param);
828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700829 result = dev_priv->device->ftbl->waittimestamp(dev_priv->device,
830 param->timestamp,
831 param->timeout);
832
Norman Geed7402ff2011-10-28 08:51:11 -0600833 trace_kgsl_waittimestamp_exit(dev_priv->device, result);
834
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 /* Fire off any pending suspend operations that are in flight */
836
837 INIT_COMPLETION(dev_priv->device->suspend_gate);
838 dev_priv->device->active_cnt--;
839 complete(&dev_priv->device->suspend_gate);
840
841 return result;
842}
843static bool check_ibdesc(struct kgsl_device_private *dev_priv,
844 struct kgsl_ibdesc *ibdesc, unsigned int numibs,
845 bool parse)
846{
847 bool result = true;
848 unsigned int i;
849 for (i = 0; i < numibs; i++) {
850 struct kgsl_mem_entry *entry;
851 spin_lock(&dev_priv->process_priv->mem_lock);
852 entry = kgsl_sharedmem_find_region(dev_priv->process_priv,
853 ibdesc[i].gpuaddr, ibdesc[i].sizedwords * sizeof(uint));
854 spin_unlock(&dev_priv->process_priv->mem_lock);
855 if (entry == NULL) {
856 KGSL_DRV_ERR(dev_priv->device,
857 "invalid cmd buffer gpuaddr %08x " \
858 "sizedwords %d\n", ibdesc[i].gpuaddr,
859 ibdesc[i].sizedwords);
860 result = false;
861 break;
862 }
863
864 if (parse && !kgsl_cffdump_parse_ibs(dev_priv, &entry->memdesc,
865 ibdesc[i].gpuaddr, ibdesc[i].sizedwords, true)) {
866 KGSL_DRV_ERR(dev_priv->device,
867 "invalid cmd buffer gpuaddr %08x " \
868 "sizedwords %d numibs %d/%d\n",
869 ibdesc[i].gpuaddr,
870 ibdesc[i].sizedwords, i+1, numibs);
871 result = false;
872 break;
873 }
874 }
875 return result;
876}
877
878static long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
879 unsigned int cmd, void *data)
880{
881 int result = 0;
882 struct kgsl_ringbuffer_issueibcmds *param = data;
883 struct kgsl_ibdesc *ibdesc;
884 struct kgsl_context *context;
885
886#ifdef CONFIG_MSM_KGSL_DRM
887 kgsl_gpu_mem_flush(DRM_KGSL_GEM_CACHE_OP_TO_DEV);
888#endif
889
890 context = kgsl_find_context(dev_priv, param->drawctxt_id);
891 if (context == NULL) {
892 result = -EINVAL;
893 KGSL_DRV_ERR(dev_priv->device,
894 "invalid drawctxt drawctxt_id %d\n",
895 param->drawctxt_id);
896 goto done;
897 }
898
899 if (param->flags & KGSL_CONTEXT_SUBMIT_IB_LIST) {
900 KGSL_DRV_INFO(dev_priv->device,
901 "Using IB list mode for ib submission, numibs: %d\n",
902 param->numibs);
903 if (!param->numibs) {
904 KGSL_DRV_ERR(dev_priv->device,
905 "Invalid numibs as parameter: %d\n",
906 param->numibs);
907 result = -EINVAL;
908 goto done;
909 }
910
911 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc) * param->numibs,
912 GFP_KERNEL);
913 if (!ibdesc) {
914 KGSL_MEM_ERR(dev_priv->device,
915 "kzalloc(%d) failed\n",
916 sizeof(struct kgsl_ibdesc) * param->numibs);
917 result = -ENOMEM;
918 goto done;
919 }
920
921 if (copy_from_user(ibdesc, (void *)param->ibdesc_addr,
922 sizeof(struct kgsl_ibdesc) * param->numibs)) {
923 result = -EFAULT;
924 KGSL_DRV_ERR(dev_priv->device,
925 "copy_from_user failed\n");
926 goto free_ibdesc;
927 }
928 } else {
929 KGSL_DRV_INFO(dev_priv->device,
930 "Using single IB submission mode for ib submission\n");
931 /* If user space driver is still using the old mode of
932 * submitting single ib then we need to support that as well */
933 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc), GFP_KERNEL);
934 if (!ibdesc) {
935 KGSL_MEM_ERR(dev_priv->device,
936 "kzalloc(%d) failed\n",
937 sizeof(struct kgsl_ibdesc));
938 result = -ENOMEM;
939 goto done;
940 }
941 ibdesc[0].gpuaddr = param->ibdesc_addr;
942 ibdesc[0].sizedwords = param->numibs;
943 param->numibs = 1;
944 }
945
946 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, true)) {
947 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc");
948 result = -EINVAL;
949 goto free_ibdesc;
950 }
951
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 result = dev_priv->device->ftbl->issueibcmds(dev_priv,
953 context,
954 ibdesc,
955 param->numibs,
956 &param->timestamp,
957 param->flags);
958
Norman Geed7402ff2011-10-28 08:51:11 -0600959 trace_kgsl_issueibcmds(dev_priv->device, param, result);
960
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 if (result != 0)
962 goto free_ibdesc;
963
964 /* this is a check to try to detect if a command buffer was freed
965 * during issueibcmds().
966 */
967 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, false)) {
968 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc AFTER issue");
969 result = -EINVAL;
970 goto free_ibdesc;
971 }
972
973free_ibdesc:
974 kfree(ibdesc);
975done:
976
977#ifdef CONFIG_MSM_KGSL_DRM
978 kgsl_gpu_mem_flush(DRM_KGSL_GEM_CACHE_OP_FROM_DEV);
979#endif
980
981 return result;
982}
983
984static long kgsl_ioctl_cmdstream_readtimestamp(struct kgsl_device_private
985 *dev_priv, unsigned int cmd,
986 void *data)
987{
988 struct kgsl_cmdstream_readtimestamp *param = data;
989
990 param->timestamp =
991 dev_priv->device->ftbl->readtimestamp(dev_priv->device,
992 param->type);
993
Norman Geed7402ff2011-10-28 08:51:11 -0600994 trace_kgsl_readtimestamp(dev_priv->device, param);
995
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996 return 0;
997}
998
999static long kgsl_ioctl_cmdstream_freememontimestamp(struct kgsl_device_private
1000 *dev_priv, unsigned int cmd,
1001 void *data)
1002{
1003 int result = 0;
1004 struct kgsl_cmdstream_freememontimestamp *param = data;
1005 struct kgsl_mem_entry *entry = NULL;
1006
1007 spin_lock(&dev_priv->process_priv->mem_lock);
1008 entry = kgsl_sharedmem_find(dev_priv->process_priv, param->gpuaddr);
1009 if (entry)
1010 list_del(&entry->list);
1011 spin_unlock(&dev_priv->process_priv->mem_lock);
1012
1013 if (entry) {
1014 kgsl_memqueue_freememontimestamp(dev_priv->device, entry,
1015 param->timestamp, param->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 } else {
1017 KGSL_DRV_ERR(dev_priv->device,
1018 "invalid gpuaddr %08x\n", param->gpuaddr);
1019 result = -EINVAL;
1020 }
1021
1022 return result;
1023}
1024
1025static long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
1026 unsigned int cmd, void *data)
1027{
1028 int result = 0;
1029 struct kgsl_drawctxt_create *param = data;
1030 struct kgsl_context *context = NULL;
1031
1032 context = kgsl_create_context(dev_priv);
1033
1034 if (context == NULL) {
1035 result = -ENOMEM;
1036 goto done;
1037 }
1038
1039 if (dev_priv->device->ftbl->drawctxt_create)
1040 result = dev_priv->device->ftbl->drawctxt_create(
1041 dev_priv->device, dev_priv->process_priv->pagetable,
1042 context, param->flags);
1043
1044 param->drawctxt_id = context->id;
1045
1046done:
1047 if (result && context)
1048 kgsl_destroy_context(dev_priv, context);
1049
1050 return result;
1051}
1052
1053static long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
1054 unsigned int cmd, void *data)
1055{
1056 int result = 0;
1057 struct kgsl_drawctxt_destroy *param = data;
1058 struct kgsl_context *context;
1059
1060 context = kgsl_find_context(dev_priv, param->drawctxt_id);
1061
1062 if (context == NULL) {
1063 result = -EINVAL;
1064 goto done;
1065 }
1066
1067 if (dev_priv->device->ftbl->drawctxt_destroy)
1068 dev_priv->device->ftbl->drawctxt_destroy(dev_priv->device,
1069 context);
1070
1071 kgsl_destroy_context(dev_priv, context);
1072
1073done:
1074 return result;
1075}
1076
1077static long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1078 unsigned int cmd, void *data)
1079{
1080 int result = 0;
1081 struct kgsl_sharedmem_free *param = data;
1082 struct kgsl_process_private *private = dev_priv->process_priv;
1083 struct kgsl_mem_entry *entry = NULL;
1084
1085 spin_lock(&private->mem_lock);
1086 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1087 if (entry)
1088 list_del(&entry->list);
1089 spin_unlock(&private->mem_lock);
1090
1091 if (entry) {
1092 kgsl_mem_entry_put(entry);
1093 } else {
1094 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1095 result = -EINVAL;
1096 }
1097
1098 return result;
1099}
1100
1101static struct vm_area_struct *kgsl_get_vma_from_start_addr(unsigned int addr)
1102{
1103 struct vm_area_struct *vma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104
1105 down_read(&current->mm->mmap_sem);
1106 vma = find_vma(current->mm, addr);
1107 up_read(&current->mm->mmap_sem);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001108 if (!vma)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 KGSL_CORE_ERR("find_vma(%x) failed\n", addr);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001110
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001111 return vma;
1112}
1113
1114static long
1115kgsl_ioctl_sharedmem_from_vmalloc(struct kgsl_device_private *dev_priv,
1116 unsigned int cmd, void *data)
1117{
1118 int result = 0, len = 0;
1119 struct kgsl_process_private *private = dev_priv->process_priv;
1120 struct kgsl_sharedmem_from_vmalloc *param = data;
1121 struct kgsl_mem_entry *entry = NULL;
1122 struct vm_area_struct *vma;
1123
1124 if (!kgsl_mmu_enabled())
1125 return -ENODEV;
1126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 if (!param->hostptr) {
1128 KGSL_CORE_ERR("invalid hostptr %x\n", param->hostptr);
1129 result = -EINVAL;
1130 goto error;
1131 }
1132
1133 vma = kgsl_get_vma_from_start_addr(param->hostptr);
1134 if (!vma) {
1135 result = -EINVAL;
1136 goto error;
1137 }
Jordan Crouse2c542b62011-07-26 08:30:20 -06001138
1139 /*
1140 * If the user specified a length, use it, otherwise try to
1141 * infer the length if the vma region
1142 */
1143 if (param->gpuaddr != 0) {
1144 len = param->gpuaddr;
1145 } else {
1146 /*
1147 * For this to work, we have to assume the VMA region is only
1148 * for this single allocation. If it isn't, then bail out
1149 */
1150 if (vma->vm_pgoff || (param->hostptr != vma->vm_start)) {
1151 KGSL_CORE_ERR("VMA region does not match hostaddr\n");
1152 result = -EINVAL;
1153 goto error;
1154 }
1155
1156 len = vma->vm_end - vma->vm_start;
1157 }
1158
1159 /* Make sure it fits */
1160 if (len == 0 || param->hostptr + len > vma->vm_end) {
1161 KGSL_CORE_ERR("Invalid memory allocation length %d\n", len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 result = -EINVAL;
1163 goto error;
1164 }
1165
1166 entry = kgsl_mem_entry_create();
1167 if (entry == NULL) {
1168 result = -ENOMEM;
1169 goto error;
1170 }
1171
1172 result = kgsl_sharedmem_vmalloc_user(&entry->memdesc,
1173 private->pagetable, len,
1174 param->flags);
1175 if (result != 0)
1176 goto error_free_entry;
1177
1178 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1179
1180 result = remap_vmalloc_range(vma, (void *) entry->memdesc.hostptr, 0);
1181 if (result) {
1182 KGSL_CORE_ERR("remap_vmalloc_range failed: %d\n", result);
1183 goto error_free_vmalloc;
1184 }
1185
1186 param->gpuaddr = entry->memdesc.gpuaddr;
1187
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001188 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189
1190 kgsl_mem_entry_attach_process(entry, private);
1191
1192 /* Process specific statistics */
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001193 kgsl_process_add_stats(private, entry->memtype, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194
1195 kgsl_check_idle(dev_priv->device);
1196 return 0;
1197
1198error_free_vmalloc:
1199 kgsl_sharedmem_free(&entry->memdesc);
1200
1201error_free_entry:
1202 kfree(entry);
1203
1204error:
1205 kgsl_check_idle(dev_priv->device);
1206 return result;
1207}
1208
1209static inline int _check_region(unsigned long start, unsigned long size,
1210 uint64_t len)
1211{
1212 uint64_t end = ((uint64_t) start) + size;
1213 return (end > len);
1214}
1215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216static int kgsl_get_phys_file(int fd, unsigned long *start, unsigned long *len,
1217 unsigned long *vstart, struct file **filep)
1218{
1219 struct file *fbfile;
1220 int ret = 0;
1221 dev_t rdev;
1222 struct fb_info *info;
1223
1224 *filep = NULL;
Jordan Crousefd978432011-09-02 14:34:32 -06001225#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 if (!get_pmem_file(fd, start, vstart, len, filep))
1227 return 0;
Jordan Crousefd978432011-09-02 14:34:32 -06001228#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229
1230 fbfile = fget(fd);
1231 if (fbfile == NULL) {
1232 KGSL_CORE_ERR("fget_light failed\n");
1233 return -1;
1234 }
1235
1236 rdev = fbfile->f_dentry->d_inode->i_rdev;
1237 info = MAJOR(rdev) == FB_MAJOR ? registered_fb[MINOR(rdev)] : NULL;
1238 if (info) {
1239 *start = info->fix.smem_start;
1240 *len = info->fix.smem_len;
1241 *vstart = (unsigned long)__va(info->fix.smem_start);
1242 ret = 0;
1243 } else {
1244 KGSL_CORE_ERR("framebuffer minor %d not found\n",
1245 MINOR(rdev));
1246 ret = -1;
1247 }
1248
1249 fput(fbfile);
1250
1251 return ret;
1252}
1253
1254static int kgsl_setup_phys_file(struct kgsl_mem_entry *entry,
1255 struct kgsl_pagetable *pagetable,
1256 unsigned int fd, unsigned int offset,
1257 size_t size)
1258{
1259 int ret;
1260 unsigned long phys, virt, len;
1261 struct file *filep;
1262
1263 ret = kgsl_get_phys_file(fd, &phys, &len, &virt, &filep);
1264 if (ret)
1265 return ret;
1266
Wei Zou4061c0b2011-07-08 10:24:22 -07001267 if (phys == 0) {
1268 ret = -EINVAL;
1269 goto err;
1270 }
1271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272 if (offset >= len) {
1273 ret = -EINVAL;
1274 goto err;
1275 }
1276
1277 if (size == 0)
1278 size = len;
1279
1280 /* Adjust the size of the region to account for the offset */
1281 size += offset & ~PAGE_MASK;
1282
1283 size = ALIGN(size, PAGE_SIZE);
1284
1285 if (_check_region(offset & PAGE_MASK, size, len)) {
1286 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1287 "than pmem region length %ld\n",
1288 offset & PAGE_MASK, size, len);
1289 ret = -EINVAL;
1290 goto err;
1291
1292 }
1293
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001294 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295
1296 entry->memdesc.pagetable = pagetable;
1297 entry->memdesc.size = size;
1298 entry->memdesc.physaddr = phys + (offset & PAGE_MASK);
1299 entry->memdesc.hostptr = (void *) (virt + (offset & PAGE_MASK));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001300
1301 ret = memdesc_sg_phys(&entry->memdesc,
1302 phys + (offset & PAGE_MASK), size);
1303 if (ret)
1304 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001305
1306 return 0;
1307err:
Jordan Crousefd978432011-09-02 14:34:32 -06001308#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 put_pmem_file(filep);
Jordan Crousefd978432011-09-02 14:34:32 -06001310#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 return ret;
1312}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313
Jordan Croused17e9aa2011-10-12 16:57:48 -06001314static int memdesc_sg_virt(struct kgsl_memdesc *memdesc,
1315 void *addr, int size)
1316{
1317 int i;
1318 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
1319 unsigned long paddr = (unsigned long) addr;
1320
1321 memdesc->sg = kmalloc(sglen * sizeof(struct scatterlist),
1322 GFP_KERNEL);
1323 if (memdesc->sg == NULL)
1324 return -ENOMEM;
1325
1326 memdesc->sglen = sglen;
1327 sg_init_table(memdesc->sg, sglen);
1328
1329 spin_lock(&current->mm->page_table_lock);
1330
1331 for (i = 0; i < sglen; i++, paddr += PAGE_SIZE) {
1332 struct page *page;
1333 pmd_t *ppmd;
1334 pte_t *ppte;
1335 pgd_t *ppgd = pgd_offset(current->mm, paddr);
1336
1337 if (pgd_none(*ppgd) || pgd_bad(*ppgd))
1338 goto err;
1339
1340 ppmd = pmd_offset(ppgd, paddr);
1341 if (pmd_none(*ppmd) || pmd_bad(*ppmd))
1342 goto err;
1343
1344 ppte = pte_offset_map(ppmd, paddr);
1345 if (ppte == NULL)
1346 goto err;
1347
1348 page = pfn_to_page(pte_pfn(*ppte));
1349 if (!page)
1350 goto err;
1351
1352 sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0);
1353 pte_unmap(ppte);
1354 }
1355
1356 spin_unlock(&current->mm->page_table_lock);
1357
1358 return 0;
1359
1360err:
1361 spin_unlock(&current->mm->page_table_lock);
1362 kfree(memdesc->sg);
1363 memdesc->sg = NULL;
1364
1365 return -EINVAL;
1366}
1367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001368static int kgsl_setup_hostptr(struct kgsl_mem_entry *entry,
1369 struct kgsl_pagetable *pagetable,
1370 void *hostptr, unsigned int offset,
1371 size_t size)
1372{
1373 struct vm_area_struct *vma;
1374 unsigned int len;
1375
1376 down_read(&current->mm->mmap_sem);
1377 vma = find_vma(current->mm, (unsigned int) hostptr);
1378 up_read(&current->mm->mmap_sem);
1379
1380 if (!vma) {
1381 KGSL_CORE_ERR("find_vma(%p) failed\n", hostptr);
1382 return -EINVAL;
1383 }
1384
1385 /* We don't necessarily start at vma->vm_start */
1386 len = vma->vm_end - (unsigned long) hostptr;
1387
1388 if (offset >= len)
1389 return -EINVAL;
1390
1391 if (!KGSL_IS_PAGE_ALIGNED((unsigned long) hostptr) ||
1392 !KGSL_IS_PAGE_ALIGNED(len)) {
1393 KGSL_CORE_ERR("user address len(%u)"
1394 "and start(%p) must be page"
1395 "aligned\n", len, hostptr);
1396 return -EINVAL;
1397 }
1398
1399 if (size == 0)
1400 size = len;
1401
1402 /* Adjust the size of the region to account for the offset */
1403 size += offset & ~PAGE_MASK;
1404
1405 size = ALIGN(size, PAGE_SIZE);
1406
1407 if (_check_region(offset & PAGE_MASK, size, len)) {
1408 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1409 "than region length %d\n",
1410 offset & PAGE_MASK, size, len);
1411 return -EINVAL;
1412 }
1413
1414 entry->memdesc.pagetable = pagetable;
1415 entry->memdesc.size = size;
1416 entry->memdesc.hostptr = hostptr + (offset & PAGE_MASK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417
Jordan Croused17e9aa2011-10-12 16:57:48 -06001418 return memdesc_sg_virt(&entry->memdesc,
1419 hostptr + (offset & PAGE_MASK), size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420}
1421
1422#ifdef CONFIG_ASHMEM
1423static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1424 struct kgsl_pagetable *pagetable,
1425 int fd, void *hostptr, size_t size)
1426{
1427 int ret;
1428 struct vm_area_struct *vma;
1429 struct file *filep, *vmfile;
1430 unsigned long len;
Jordan Crouse2c542b62011-07-26 08:30:20 -06001431 unsigned int hostaddr = (unsigned int) hostptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432
Jordan Crouse2c542b62011-07-26 08:30:20 -06001433 vma = kgsl_get_vma_from_start_addr(hostaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001434 if (vma == NULL)
1435 return -EINVAL;
1436
Jordan Crouse2c542b62011-07-26 08:30:20 -06001437 if (vma->vm_pgoff || vma->vm_start != hostaddr) {
1438 KGSL_CORE_ERR("Invalid vma region\n");
1439 return -EINVAL;
1440 }
1441
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442 len = vma->vm_end - vma->vm_start;
1443
1444 if (size == 0)
1445 size = len;
1446
1447 if (size != len) {
1448 KGSL_CORE_ERR("Invalid size %d for vma region %p\n",
1449 size, hostptr);
1450 return -EINVAL;
1451 }
1452
1453 ret = get_ashmem_file(fd, &filep, &vmfile, &len);
1454
1455 if (ret) {
1456 KGSL_CORE_ERR("get_ashmem_file failed\n");
1457 return ret;
1458 }
1459
1460 if (vmfile != vma->vm_file) {
1461 KGSL_CORE_ERR("ashmem shmem file does not match vma\n");
1462 ret = -EINVAL;
1463 goto err;
1464 }
1465
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001466 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467 entry->memdesc.pagetable = pagetable;
1468 entry->memdesc.size = ALIGN(size, PAGE_SIZE);
1469 entry->memdesc.hostptr = hostptr;
Jordan Croused17e9aa2011-10-12 16:57:48 -06001470
1471 ret = memdesc_sg_virt(&entry->memdesc, hostptr, size);
1472 if (ret)
1473 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474
1475 return 0;
1476
1477err:
1478 put_ashmem_file(filep);
1479 return ret;
1480}
1481#else
1482static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1483 struct kgsl_pagetable *pagetable,
1484 int fd, void *hostptr, size_t size)
1485{
1486 return -EINVAL;
1487}
1488#endif
1489
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001490static int kgsl_setup_ion(struct kgsl_mem_entry *entry,
1491 struct kgsl_pagetable *pagetable, int fd)
1492{
1493 struct ion_handle *handle;
1494 struct scatterlist *s;
1495 unsigned long flags;
1496
1497 if (kgsl_ion_client == NULL) {
1498 kgsl_ion_client = msm_ion_client_create(UINT_MAX, KGSL_NAME);
1499 if (kgsl_ion_client == NULL)
1500 return -ENODEV;
1501 }
1502
1503 handle = ion_import_fd(kgsl_ion_client, fd);
1504 if (IS_ERR_OR_NULL(handle))
1505 return PTR_ERR(handle);
1506
1507 entry->memtype = KGSL_MEM_ENTRY_ION;
1508 entry->priv_data = handle;
1509 entry->memdesc.pagetable = pagetable;
1510 entry->memdesc.size = 0;
1511
1512 if (ion_handle_get_flags(kgsl_ion_client, handle, &flags))
1513 goto err;
1514
1515 entry->memdesc.sg = ion_map_dma(kgsl_ion_client, handle, flags);
1516
1517 if (IS_ERR_OR_NULL(entry->memdesc.sg))
1518 goto err;
1519
1520 /* Calculate the size of the memdesc from the sglist */
1521
1522 entry->memdesc.sglen = 0;
1523
1524 for (s = entry->memdesc.sg; s != NULL; s = sg_next(s)) {
1525 entry->memdesc.size += s->length;
1526 entry->memdesc.sglen++;
1527 }
1528
1529 return 0;
1530err:
1531 ion_free(kgsl_ion_client, handle);
1532 return -ENOMEM;
1533}
1534
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001535static long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
1536 unsigned int cmd, void *data)
1537{
1538 int result = -EINVAL;
1539 struct kgsl_map_user_mem *param = data;
1540 struct kgsl_mem_entry *entry = NULL;
1541 struct kgsl_process_private *private = dev_priv->process_priv;
Jason848741a2011-07-12 10:24:25 -07001542 enum kgsl_user_mem_type memtype;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001543
1544 entry = kgsl_mem_entry_create();
1545
1546 if (entry == NULL)
1547 return -ENOMEM;
1548
Jason848741a2011-07-12 10:24:25 -07001549 if (_IOC_SIZE(cmd) == sizeof(struct kgsl_sharedmem_from_pmem))
1550 memtype = KGSL_USER_MEM_TYPE_PMEM;
1551 else
1552 memtype = param->memtype;
1553
1554 switch (memtype) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001555 case KGSL_USER_MEM_TYPE_PMEM:
1556 if (param->fd == 0 || param->len == 0)
1557 break;
1558
1559 result = kgsl_setup_phys_file(entry, private->pagetable,
1560 param->fd, param->offset,
1561 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001562 entry->memtype = KGSL_MEM_ENTRY_PMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563 break;
1564
1565 case KGSL_USER_MEM_TYPE_ADDR:
1566 if (!kgsl_mmu_enabled()) {
1567 KGSL_DRV_ERR(dev_priv->device,
1568 "Cannot map paged memory with the "
1569 "MMU disabled\n");
1570 break;
1571 }
1572
1573 if (param->hostptr == 0)
1574 break;
1575
1576 result = kgsl_setup_hostptr(entry, private->pagetable,
1577 (void *) param->hostptr,
1578 param->offset, param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001579 entry->memtype = KGSL_MEM_ENTRY_USER;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580 break;
1581
1582 case KGSL_USER_MEM_TYPE_ASHMEM:
1583 if (!kgsl_mmu_enabled()) {
1584 KGSL_DRV_ERR(dev_priv->device,
1585 "Cannot map paged memory with the "
1586 "MMU disabled\n");
1587 break;
1588 }
1589
1590 if (param->hostptr == 0)
1591 break;
1592
1593 result = kgsl_setup_ashmem(entry, private->pagetable,
1594 param->fd, (void *) param->hostptr,
1595 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001596
1597 entry->memtype = KGSL_MEM_ENTRY_ASHMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001599 case KGSL_USER_MEM_TYPE_ION:
1600 result = kgsl_setup_ion(entry, private->pagetable,
1601 param->fd);
1602 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603 default:
Jason848741a2011-07-12 10:24:25 -07001604 KGSL_CORE_ERR("Invalid memory type: %x\n", memtype);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 break;
1606 }
1607
1608 if (result)
1609 goto error;
1610
1611 result = kgsl_mmu_map(private->pagetable,
1612 &entry->memdesc,
1613 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
1614
1615 if (result)
1616 goto error_put_file_ptr;
1617
1618 /* Adjust the returned value for a non 4k aligned offset */
1619 param->gpuaddr = entry->memdesc.gpuaddr + (param->offset & ~PAGE_MASK);
1620
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001621 KGSL_STATS_ADD(param->len, kgsl_driver.stats.mapped,
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001622 kgsl_driver.stats.mapped_max);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001624 kgsl_process_add_stats(private, entry->memtype, param->len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625
1626 kgsl_mem_entry_attach_process(entry, private);
1627
1628 kgsl_check_idle(dev_priv->device);
1629 return result;
1630
1631 error_put_file_ptr:
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001632 if (entry->priv_data)
1633 fput(entry->priv_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001634
1635error:
1636 kfree(entry);
1637 kgsl_check_idle(dev_priv->device);
1638 return result;
1639}
1640
1641/*This function flushes a graphics memory allocation from CPU cache
1642 *when caching is enabled with MMU*/
1643static long
1644kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
1645 unsigned int cmd, void *data)
1646{
1647 int result = 0;
1648 struct kgsl_mem_entry *entry;
1649 struct kgsl_sharedmem_free *param = data;
1650 struct kgsl_process_private *private = dev_priv->process_priv;
1651
1652 spin_lock(&private->mem_lock);
1653 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1654 if (!entry) {
1655 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1656 result = -EINVAL;
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001657 goto done;
1658 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001659 if (!entry->memdesc.hostptr) {
1660 KGSL_CORE_ERR("invalid hostptr with gpuaddr %08x\n",
1661 param->gpuaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001663 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001664
1665 kgsl_cache_range_op(&entry->memdesc, KGSL_CACHE_OP_CLEAN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666done:
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001667 spin_unlock(&private->mem_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 return result;
1669}
1670
1671static long
1672kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
1673 unsigned int cmd, void *data)
1674{
1675 struct kgsl_process_private *private = dev_priv->process_priv;
1676 struct kgsl_gpumem_alloc *param = data;
1677 struct kgsl_mem_entry *entry;
1678 int result;
1679
1680 entry = kgsl_mem_entry_create();
1681 if (entry == NULL)
1682 return -ENOMEM;
1683
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001684 result = kgsl_allocate_user(&entry->memdesc, private->pagetable,
1685 param->size, param->flags);
1686
1687 if (result == 0) {
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001688 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689 kgsl_mem_entry_attach_process(entry, private);
1690 param->gpuaddr = entry->memdesc.gpuaddr;
1691
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001692 kgsl_process_add_stats(private, entry->memtype, param->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001693 } else
1694 kfree(entry);
1695
1696 kgsl_check_idle(dev_priv->device);
1697 return result;
1698}
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001699static long kgsl_ioctl_cff_syncmem(struct kgsl_device_private *dev_priv,
1700 unsigned int cmd, void *data)
1701{
1702 int result = 0;
1703 struct kgsl_cff_syncmem *param = data;
1704 struct kgsl_process_private *private = dev_priv->process_priv;
1705 struct kgsl_mem_entry *entry = NULL;
1706
1707 spin_lock(&private->mem_lock);
1708 entry = kgsl_sharedmem_find_region(private, param->gpuaddr, param->len);
1709 if (entry)
1710 kgsl_cffdump_syncmem(dev_priv, &entry->memdesc, param->gpuaddr,
1711 param->len, true);
1712 else
1713 result = -EINVAL;
1714 spin_unlock(&private->mem_lock);
1715 return result;
1716}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001717
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001718static long kgsl_ioctl_cff_user_event(struct kgsl_device_private *dev_priv,
1719 unsigned int cmd, void *data)
1720{
1721 int result = 0;
1722 struct kgsl_cff_user_event *param = data;
1723
1724 kgsl_cffdump_user_event(param->cff_opcode, param->op1, param->op2,
1725 param->op3, param->op4, param->op5);
1726
1727 return result;
1728}
1729
Jordan Croused4bc9d22011-11-17 13:39:21 -07001730#ifdef CONFIG_GENLOCK
1731struct kgsl_genlock_event_priv {
1732 struct genlock_handle *handle;
1733 struct genlock *lock;
1734};
1735
1736/**
1737 * kgsl_genlock_event_cb - Event callback for a genlock timestamp event
1738 * @device - The KGSL device that expired the timestamp
1739 * @priv - private data for the event
1740 * @timestamp - the timestamp that triggered the event
1741 *
1742 * Release a genlock lock following the expiration of a timestamp
1743 */
1744
1745static void kgsl_genlock_event_cb(struct kgsl_device *device,
1746 void *priv, u32 timestamp)
1747{
1748 struct kgsl_genlock_event_priv *ev = priv;
1749 int ret;
1750
1751 ret = genlock_lock(ev->handle, GENLOCK_UNLOCK, 0, 0);
1752 if (ret)
1753 KGSL_CORE_ERR("Error while unlocking genlock: %d\n", ret);
1754
1755 genlock_put_handle(ev->handle);
1756
1757 kfree(ev);
1758}
1759
1760/**
1761 * kgsl_add_genlock-event - Create a new genlock event
1762 * @device - KGSL device to create the event on
1763 * @timestamp - Timestamp to trigger the event
1764 * @data - User space buffer containing struct kgsl_genlock_event_priv
1765 * @len - length of the userspace buffer
1766 * @returns 0 on success or error code on error
1767 *
1768 * Attack to a genlock handle and register an event to release the
1769 * genlock lock when the timestamp expires
1770 */
1771
1772static int kgsl_add_genlock_event(struct kgsl_device *device,
1773 u32 timestamp, void __user *data, int len)
1774{
1775 struct kgsl_genlock_event_priv *event;
1776 struct kgsl_timestamp_event_genlock priv;
1777 int ret;
1778
1779 if (len != sizeof(priv))
1780 return -EINVAL;
1781
1782 if (copy_from_user(&priv, data, sizeof(priv)))
1783 return -EFAULT;
1784
1785 event = kzalloc(sizeof(*event), GFP_KERNEL);
1786
1787 if (event == NULL)
1788 return -ENOMEM;
1789
1790 event->handle = genlock_get_handle_fd(priv.handle);
1791
1792 if (IS_ERR(event->handle)) {
1793 int ret = PTR_ERR(event->handle);
1794 kfree(event);
1795 return ret;
1796 }
1797
1798 ret = kgsl_add_event(device, timestamp, kgsl_genlock_event_cb, event);
1799 if (ret)
1800 kfree(event);
1801
1802 return ret;
1803}
1804#else
1805static long kgsl_add_genlock_event(struct kgsl_device *device,
1806 u32 timestamp, void __user *data, int len)
1807{
1808 return -EINVAL;
1809}
1810#endif
1811
1812/**
1813 * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace
1814 * @dev_priv - pointer to the private device structure
1815 * @cmd - the ioctl cmd passed from kgsl_ioctl
1816 * @data - the user data buffer from kgsl_ioctl
1817 * @returns 0 on success or error code on failure
1818 */
1819
1820static long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
1821 unsigned int cmd, void *data)
1822{
1823 struct kgsl_timestamp_event *param = data;
1824 int ret;
1825
1826 switch (param->type) {
1827 case KGSL_TIMESTAMP_EVENT_GENLOCK:
1828 ret = kgsl_add_genlock_event(dev_priv->device,
1829 param->timestamp, param->priv, param->len);
1830 break;
1831 default:
1832 ret = -EINVAL;
1833 }
1834
1835 return ret;
1836}
1837
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838typedef long (*kgsl_ioctl_func_t)(struct kgsl_device_private *,
1839 unsigned int, void *);
1840
1841#define KGSL_IOCTL_FUNC(_cmd, _func, _lock) \
1842 [_IOC_NR(_cmd)] = { .cmd = _cmd, .func = _func, .lock = _lock }
1843
1844static const struct {
1845 unsigned int cmd;
1846 kgsl_ioctl_func_t func;
1847 int lock;
1848} kgsl_ioctl_funcs[] = {
1849 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_GETPROPERTY,
1850 kgsl_ioctl_device_getproperty, 1),
1851 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_WAITTIMESTAMP,
1852 kgsl_ioctl_device_waittimestamp, 1),
1853 KGSL_IOCTL_FUNC(IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS,
1854 kgsl_ioctl_rb_issueibcmds, 1),
1855 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_READTIMESTAMP,
1856 kgsl_ioctl_cmdstream_readtimestamp, 1),
1857 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP,
1858 kgsl_ioctl_cmdstream_freememontimestamp, 1),
1859 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_CREATE,
1860 kgsl_ioctl_drawctxt_create, 1),
1861 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_DESTROY,
1862 kgsl_ioctl_drawctxt_destroy, 1),
1863 KGSL_IOCTL_FUNC(IOCTL_KGSL_MAP_USER_MEM,
1864 kgsl_ioctl_map_user_mem, 0),
1865 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_PMEM,
1866 kgsl_ioctl_map_user_mem, 0),
1867 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FREE,
1868 kgsl_ioctl_sharedmem_free, 0),
1869 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_VMALLOC,
1870 kgsl_ioctl_sharedmem_from_vmalloc, 0),
1871 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FLUSH_CACHE,
1872 kgsl_ioctl_sharedmem_flush_cache, 0),
1873 KGSL_IOCTL_FUNC(IOCTL_KGSL_GPUMEM_ALLOC,
1874 kgsl_ioctl_gpumem_alloc, 0),
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001875 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_SYNCMEM,
1876 kgsl_ioctl_cff_syncmem, 0),
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001877 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_USER_EVENT,
1878 kgsl_ioctl_cff_user_event, 0),
Jordan Croused4bc9d22011-11-17 13:39:21 -07001879 KGSL_IOCTL_FUNC(IOCTL_KGSL_TIMESTAMP_EVENT,
Lucille Sylvester9329cf02011-12-02 14:30:41 -07001880 kgsl_ioctl_timestamp_event, 1),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001881};
1882
1883static long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1884{
1885 struct kgsl_device_private *dev_priv = filep->private_data;
1886 unsigned int nr = _IOC_NR(cmd);
1887 kgsl_ioctl_func_t func;
1888 int lock, ret;
1889 char ustack[64];
1890 void *uptr = NULL;
1891
1892 BUG_ON(dev_priv == NULL);
1893
1894 /* Workaround for an previously incorrectly defined ioctl code.
1895 This helps ensure binary compatability */
1896
1897 if (cmd == IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP_OLD)
1898 cmd = IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP;
Jason Varbedian80ba33d2011-07-11 17:29:05 -07001899 else if (cmd == IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_OLD)
1900 cmd = IOCTL_KGSL_CMDSTREAM_READTIMESTAMP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001901
1902 if (cmd & (IOC_IN | IOC_OUT)) {
1903 if (_IOC_SIZE(cmd) < sizeof(ustack))
1904 uptr = ustack;
1905 else {
1906 uptr = kzalloc(_IOC_SIZE(cmd), GFP_KERNEL);
1907 if (uptr == NULL) {
1908 KGSL_MEM_ERR(dev_priv->device,
1909 "kzalloc(%d) failed\n", _IOC_SIZE(cmd));
1910 ret = -ENOMEM;
1911 goto done;
1912 }
1913 }
1914
1915 if (cmd & IOC_IN) {
1916 if (copy_from_user(uptr, (void __user *) arg,
1917 _IOC_SIZE(cmd))) {
1918 ret = -EFAULT;
1919 goto done;
1920 }
1921 } else
1922 memset(uptr, 0, _IOC_SIZE(cmd));
1923 }
1924
1925 if (nr < ARRAY_SIZE(kgsl_ioctl_funcs) &&
1926 kgsl_ioctl_funcs[nr].func != NULL) {
1927 func = kgsl_ioctl_funcs[nr].func;
1928 lock = kgsl_ioctl_funcs[nr].lock;
1929 } else {
1930 func = dev_priv->device->ftbl->ioctl;
1931 if (!func) {
1932 KGSL_DRV_INFO(dev_priv->device,
1933 "invalid ioctl code %08x\n", cmd);
1934 ret = -EINVAL;
1935 goto done;
1936 }
1937 lock = 1;
1938 }
1939
1940 if (lock) {
1941 mutex_lock(&dev_priv->device->mutex);
1942 kgsl_check_suspended(dev_priv->device);
1943 }
1944
1945 ret = func(dev_priv, cmd, uptr);
1946
1947 if (lock) {
1948 kgsl_check_idle_locked(dev_priv->device);
1949 mutex_unlock(&dev_priv->device->mutex);
1950 }
1951
1952 if (ret == 0 && (cmd & IOC_OUT)) {
1953 if (copy_to_user((void __user *) arg, uptr, _IOC_SIZE(cmd)))
1954 ret = -EFAULT;
1955 }
1956
1957done:
1958 if (_IOC_SIZE(cmd) >= sizeof(ustack))
1959 kfree(uptr);
1960
1961 return ret;
1962}
1963
1964static int
1965kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
1966{
1967 struct kgsl_memdesc *memdesc = &device->memstore;
1968 int result;
1969 unsigned int vma_size = vma->vm_end - vma->vm_start;
1970
1971 /* The memstore can only be mapped as read only */
1972
1973 if (vma->vm_flags & VM_WRITE)
1974 return -EPERM;
1975
1976 if (memdesc->size != vma_size) {
1977 KGSL_MEM_ERR(device, "memstore bad size: %d should be %d\n",
1978 vma_size, memdesc->size);
1979 return -EINVAL;
1980 }
1981
1982 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1983
1984 result = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1985 vma_size, vma->vm_page_prot);
1986 if (result != 0)
1987 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
1988 result);
1989
1990 return result;
1991}
1992
Jordan Crouse4283e172011-09-26 14:45:47 -06001993/*
1994 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
1995 * Increase the refcount to make sure that the accounting stays correct
1996 */
1997
1998static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
1999{
2000 struct kgsl_mem_entry *entry = vma->vm_private_data;
2001 kgsl_mem_entry_get(entry);
2002}
2003
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002004static int
2005kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2006{
2007 struct kgsl_mem_entry *entry = vma->vm_private_data;
2008
Jordan Croused17e9aa2011-10-12 16:57:48 -06002009 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 return VM_FAULT_SIGBUS;
2011
2012 return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
2013}
2014
2015static void
2016kgsl_gpumem_vm_close(struct vm_area_struct *vma)
2017{
2018 struct kgsl_mem_entry *entry = vma->vm_private_data;
2019 kgsl_mem_entry_put(entry);
2020}
2021
2022static struct vm_operations_struct kgsl_gpumem_vm_ops = {
Jordan Crouse4283e172011-09-26 14:45:47 -06002023 .open = kgsl_gpumem_vm_open,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002024 .fault = kgsl_gpumem_vm_fault,
2025 .close = kgsl_gpumem_vm_close,
2026};
2027
2028static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
2029{
2030 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031 struct kgsl_device_private *dev_priv = file->private_data;
2032 struct kgsl_process_private *private = dev_priv->process_priv;
Jordan Crouse976cf0e2011-09-12 10:41:49 -06002033 struct kgsl_mem_entry *tmp, *entry = NULL;
Jordan Crouse2db0af92011-08-08 16:05:09 -06002034 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035
2036 /* Handle leagacy behavior for memstore */
2037
2038 if (vma_offset == device->memstore.physaddr)
2039 return kgsl_mmap_memstore(device, vma);
2040
2041 /* Find a chunk of GPU memory */
2042
2043 spin_lock(&private->mem_lock);
Jordan Crouse976cf0e2011-09-12 10:41:49 -06002044 list_for_each_entry(tmp, &private->mem_list, list) {
2045 if (vma_offset == tmp->memdesc.gpuaddr) {
2046 kgsl_mem_entry_get(tmp);
2047 entry = tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048 break;
2049 }
2050 }
2051 spin_unlock(&private->mem_lock);
2052
2053 if (entry == NULL)
2054 return -EINVAL;
2055
Jordan Croused17e9aa2011-10-12 16:57:48 -06002056 if (!entry->memdesc.ops ||
2057 !entry->memdesc.ops->vmflags ||
2058 !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002059 return -EINVAL;
2060
2061 vma->vm_flags |= entry->memdesc.ops->vmflags(&entry->memdesc);
2062
2063 vma->vm_private_data = entry;
2064 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
2065 vma->vm_ops = &kgsl_gpumem_vm_ops;
2066 vma->vm_file = file;
2067
2068 return 0;
2069}
2070
2071static const struct file_operations kgsl_fops = {
2072 .owner = THIS_MODULE,
2073 .release = kgsl_release,
2074 .open = kgsl_open,
2075 .mmap = kgsl_mmap,
2076 .unlocked_ioctl = kgsl_ioctl,
2077};
2078
2079struct kgsl_driver kgsl_driver = {
2080 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
2081 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
2082 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
2083};
2084EXPORT_SYMBOL(kgsl_driver);
2085
2086void kgsl_unregister_device(struct kgsl_device *device)
2087{
2088 int minor;
2089
2090 mutex_lock(&kgsl_driver.devlock);
2091 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2092 if (device == kgsl_driver.devp[minor])
2093 break;
2094 }
2095
2096 mutex_unlock(&kgsl_driver.devlock);
2097
2098 if (minor == KGSL_DEVICE_MAX)
2099 return;
2100
2101 kgsl_cffdump_close(device->id);
2102 kgsl_pwrctrl_uninit_sysfs(device);
2103
Lucille Sylvesteref44e7332011-11-02 13:21:17 -07002104 if (cpu_is_msm8x60())
2105 wake_lock_destroy(&device->idle_wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106
2107 idr_destroy(&device->context_idr);
2108
2109 if (device->memstore.hostptr)
2110 kgsl_sharedmem_free(&device->memstore);
2111
2112 kgsl_mmu_close(device);
2113
2114 if (device->work_queue) {
2115 destroy_workqueue(device->work_queue);
2116 device->work_queue = NULL;
2117 }
2118
2119 device_destroy(kgsl_driver.class,
2120 MKDEV(MAJOR(kgsl_driver.major), minor));
2121
2122 mutex_lock(&kgsl_driver.devlock);
2123 kgsl_driver.devp[minor] = NULL;
2124 mutex_unlock(&kgsl_driver.devlock);
2125}
2126EXPORT_SYMBOL(kgsl_unregister_device);
2127
2128int
2129kgsl_register_device(struct kgsl_device *device)
2130{
2131 int minor, ret;
2132 dev_t dev;
2133
2134 /* Find a minor for the device */
2135
2136 mutex_lock(&kgsl_driver.devlock);
2137 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2138 if (kgsl_driver.devp[minor] == NULL) {
2139 kgsl_driver.devp[minor] = device;
2140 break;
2141 }
2142 }
2143
2144 mutex_unlock(&kgsl_driver.devlock);
2145
2146 if (minor == KGSL_DEVICE_MAX) {
2147 KGSL_CORE_ERR("minor devices exhausted\n");
2148 return -ENODEV;
2149 }
2150
2151 /* Create the device */
2152 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
2153 device->dev = device_create(kgsl_driver.class,
2154 device->parentdev,
2155 dev, device,
2156 device->name);
2157
2158 if (IS_ERR(device->dev)) {
2159 ret = PTR_ERR(device->dev);
2160 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
2161 goto err_devlist;
2162 }
2163
2164 dev_set_drvdata(device->parentdev, device);
2165
2166 /* Generic device initialization */
2167 init_waitqueue_head(&device->wait_queue);
2168
2169 kgsl_cffdump_open(device->id);
2170
2171 init_completion(&device->hwaccess_gate);
2172 init_completion(&device->suspend_gate);
2173
2174 ATOMIC_INIT_NOTIFIER_HEAD(&device->ts_notifier_list);
2175
2176 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
2177 ret = kgsl_create_device_workqueue(device);
2178 if (ret)
2179 goto err_devlist;
2180
2181 INIT_WORK(&device->idle_check_ws, kgsl_idle_check);
Jordan Crouse1bf80aa2011-10-12 16:57:47 -06002182 INIT_WORK(&device->ts_expired_ws, kgsl_timestamp_expired);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002183
2184 INIT_LIST_HEAD(&device->memqueue);
Jordan Croused4bc9d22011-11-17 13:39:21 -07002185 INIT_LIST_HEAD(&device->events);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186
2187 ret = kgsl_mmu_init(device);
2188 if (ret != 0)
2189 goto err_dest_work_q;
2190
2191 ret = kgsl_allocate_contiguous(&device->memstore,
2192 sizeof(struct kgsl_devmemstore));
2193
2194 if (ret != 0)
2195 goto err_close_mmu;
2196
Lucille Sylvesteref44e7332011-11-02 13:21:17 -07002197 if (cpu_is_msm8x60())
2198 wake_lock_init(&device->idle_wakelock,
2199 WAKE_LOCK_IDLE, device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002200
2201 idr_init(&device->context_idr);
2202
2203 /* sysfs and debugfs initalization - failure here is non fatal */
2204
2205 /* Initialize logging */
2206 kgsl_device_debugfs_init(device);
2207
2208 /* Initialize common sysfs entries */
2209 kgsl_pwrctrl_init_sysfs(device);
2210
2211 return 0;
2212
2213err_close_mmu:
2214 kgsl_mmu_close(device);
2215err_dest_work_q:
2216 destroy_workqueue(device->work_queue);
2217 device->work_queue = NULL;
2218err_devlist:
2219 mutex_lock(&kgsl_driver.devlock);
2220 kgsl_driver.devp[minor] = NULL;
2221 mutex_unlock(&kgsl_driver.devlock);
2222
2223 return ret;
2224}
2225EXPORT_SYMBOL(kgsl_register_device);
2226
2227int kgsl_device_platform_probe(struct kgsl_device *device,
2228 irqreturn_t (*dev_isr) (int, void*))
2229{
2230 int status = -EINVAL;
2231 struct kgsl_memregion *regspace = NULL;
2232 struct resource *res;
2233 struct platform_device *pdev =
2234 container_of(device->parentdev, struct platform_device, dev);
2235
2236 pm_runtime_enable(device->parentdev);
2237
2238 status = kgsl_pwrctrl_init(device);
2239 if (status)
2240 goto error;
2241
2242 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2243 device->iomemname);
2244 if (res == NULL) {
2245 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
2246 status = -EINVAL;
2247 goto error_pwrctrl_close;
2248 }
2249 if (res->start == 0 || resource_size(res) == 0) {
2250 KGSL_DRV_ERR(device, "dev %d invalid regspace\n", device->id);
2251 status = -EINVAL;
2252 goto error_pwrctrl_close;
2253 }
2254
2255 regspace = &device->regspace;
2256 regspace->mmio_phys_base = res->start;
2257 regspace->sizebytes = resource_size(res);
2258
2259 if (!request_mem_region(regspace->mmio_phys_base,
2260 regspace->sizebytes, device->name)) {
2261 KGSL_DRV_ERR(device, "request_mem_region failed\n");
2262 status = -ENODEV;
2263 goto error_pwrctrl_close;
2264 }
2265
2266 regspace->mmio_virt_base = ioremap(regspace->mmio_phys_base,
2267 regspace->sizebytes);
2268
2269 if (regspace->mmio_virt_base == NULL) {
2270 KGSL_DRV_ERR(device, "ioremap failed\n");
2271 status = -ENODEV;
2272 goto error_release_mem;
2273 }
2274
2275 status = request_irq(device->pwrctrl.interrupt_num, dev_isr,
2276 IRQF_TRIGGER_HIGH, device->name, device);
2277 if (status) {
2278 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
2279 device->pwrctrl.interrupt_num, status);
2280 goto error_iounmap;
2281 }
2282 device->pwrctrl.have_irq = 1;
2283 disable_irq(device->pwrctrl.interrupt_num);
2284
2285 KGSL_DRV_INFO(device,
2286 "dev_id %d regs phys 0x%08x size 0x%08x virt %p\n",
2287 device->id, regspace->mmio_phys_base,
2288 regspace->sizebytes, regspace->mmio_virt_base);
2289
2290
2291 status = kgsl_register_device(device);
2292 if (!status)
2293 return status;
2294
2295 free_irq(device->pwrctrl.interrupt_num, NULL);
2296 device->pwrctrl.have_irq = 0;
2297error_iounmap:
2298 iounmap(regspace->mmio_virt_base);
2299 regspace->mmio_virt_base = NULL;
2300error_release_mem:
2301 release_mem_region(regspace->mmio_phys_base, regspace->sizebytes);
2302error_pwrctrl_close:
2303 kgsl_pwrctrl_close(device);
2304error:
2305 return status;
2306}
2307EXPORT_SYMBOL(kgsl_device_platform_probe);
2308
2309void kgsl_device_platform_remove(struct kgsl_device *device)
2310{
2311 struct kgsl_memregion *regspace = &device->regspace;
2312
2313 kgsl_unregister_device(device);
2314
2315 if (regspace->mmio_virt_base != NULL) {
2316 iounmap(regspace->mmio_virt_base);
2317 regspace->mmio_virt_base = NULL;
2318 release_mem_region(regspace->mmio_phys_base,
2319 regspace->sizebytes);
2320 }
2321 kgsl_pwrctrl_close(device);
2322
2323 pm_runtime_disable(device->parentdev);
2324}
2325EXPORT_SYMBOL(kgsl_device_platform_remove);
2326
2327static int __devinit
2328kgsl_ptdata_init(void)
2329{
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002330 kgsl_driver.ptpool = kgsl_mmu_ptpool_init(KGSL_PAGETABLE_SIZE,
2331 kgsl_pagetable_count);
2332 if (!kgsl_driver.ptpool)
2333 return -ENOMEM;
2334 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002335}
2336
2337static void kgsl_core_exit(void)
2338{
2339 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
2340
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002341 kgsl_mmu_ptpool_destroy(&kgsl_driver.ptpool);
2342 kgsl_driver.ptpool = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002343
2344 device_unregister(&kgsl_driver.virtdev);
2345
2346 if (kgsl_driver.class) {
2347 class_destroy(kgsl_driver.class);
2348 kgsl_driver.class = NULL;
2349 }
2350
2351 kgsl_drm_exit();
2352 kgsl_cffdump_destroy();
Jordan Croused8f1c6b2011-10-04 09:31:29 -06002353 kgsl_core_debugfs_close();
2354 kgsl_sharedmem_uninit_sysfs();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002355}
2356
2357static int __init kgsl_core_init(void)
2358{
2359 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002360 /* alloc major and minor device numbers */
2361 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
2362 KGSL_NAME);
2363 if (result < 0) {
2364 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
2365 goto err;
2366 }
2367
2368 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
2369 kgsl_driver.cdev.owner = THIS_MODULE;
2370 kgsl_driver.cdev.ops = &kgsl_fops;
2371 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
2372 KGSL_DEVICE_MAX);
2373
2374 if (result) {
2375 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d,"
2376 " result= %d\n", kgsl_driver.major, result);
2377 goto err;
2378 }
2379
2380 kgsl_driver.class = class_create(THIS_MODULE, KGSL_NAME);
2381
2382 if (IS_ERR(kgsl_driver.class)) {
2383 result = PTR_ERR(kgsl_driver.class);
2384 KGSL_CORE_ERR("failed to create class %s", KGSL_NAME);
2385 goto err;
2386 }
2387
2388 /* Make a virtual device for managing core related things
2389 in sysfs */
2390 kgsl_driver.virtdev.class = kgsl_driver.class;
2391 dev_set_name(&kgsl_driver.virtdev, "kgsl");
2392 result = device_register(&kgsl_driver.virtdev);
2393 if (result) {
2394 KGSL_CORE_ERR("driver_register failed\n");
2395 goto err;
2396 }
2397
2398 /* Make kobjects in the virtual device for storing statistics */
2399
2400 kgsl_driver.ptkobj =
2401 kobject_create_and_add("pagetables",
2402 &kgsl_driver.virtdev.kobj);
2403
2404 kgsl_driver.prockobj =
2405 kobject_create_and_add("proc",
2406 &kgsl_driver.virtdev.kobj);
2407
2408 kgsl_core_debugfs_init();
2409
2410 kgsl_sharedmem_init_sysfs();
2411 kgsl_cffdump_init();
2412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 INIT_LIST_HEAD(&kgsl_driver.process_list);
2414
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002415 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
2416
2417 kgsl_mmu_set_mmutype(ksgl_mmu_type);
2418
2419 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype()) {
2420 result = kgsl_ptdata_init();
2421 if (result)
2422 goto err;
2423 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002424
2425 result = kgsl_drm_init(NULL);
2426
2427 if (result)
2428 goto err;
2429
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002430 return 0;
2431
2432err:
2433 kgsl_core_exit();
2434 return result;
2435}
2436
2437module_init(kgsl_core_init);
2438module_exit(kgsl_core_exit);
2439
2440MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
2441MODULE_DESCRIPTION("MSM GPU driver");
2442MODULE_LICENSE("GPL");