blob: 512a262e6169ca9f9a4eff1911a20f21aeeb59fe [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
Jeff Boody28afec42012-01-18 15:47:46 -07001321 memdesc->sg = vmalloc(sglen * sizeof(struct scatterlist));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001322 if (memdesc->sg == NULL)
1323 return -ENOMEM;
1324
1325 memdesc->sglen = sglen;
1326 sg_init_table(memdesc->sg, sglen);
1327
1328 spin_lock(&current->mm->page_table_lock);
1329
1330 for (i = 0; i < sglen; i++, paddr += PAGE_SIZE) {
1331 struct page *page;
1332 pmd_t *ppmd;
1333 pte_t *ppte;
1334 pgd_t *ppgd = pgd_offset(current->mm, paddr);
1335
1336 if (pgd_none(*ppgd) || pgd_bad(*ppgd))
1337 goto err;
1338
1339 ppmd = pmd_offset(ppgd, paddr);
1340 if (pmd_none(*ppmd) || pmd_bad(*ppmd))
1341 goto err;
1342
1343 ppte = pte_offset_map(ppmd, paddr);
1344 if (ppte == NULL)
1345 goto err;
1346
1347 page = pfn_to_page(pte_pfn(*ppte));
1348 if (!page)
1349 goto err;
1350
1351 sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0);
1352 pte_unmap(ppte);
1353 }
1354
1355 spin_unlock(&current->mm->page_table_lock);
1356
1357 return 0;
1358
1359err:
1360 spin_unlock(&current->mm->page_table_lock);
Jeff Boody28afec42012-01-18 15:47:46 -07001361 vfree(memdesc->sg);
Jordan Croused17e9aa2011-10-12 16:57:48 -06001362 memdesc->sg = NULL;
1363
1364 return -EINVAL;
1365}
1366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367static int kgsl_setup_hostptr(struct kgsl_mem_entry *entry,
1368 struct kgsl_pagetable *pagetable,
1369 void *hostptr, unsigned int offset,
1370 size_t size)
1371{
1372 struct vm_area_struct *vma;
1373 unsigned int len;
1374
1375 down_read(&current->mm->mmap_sem);
1376 vma = find_vma(current->mm, (unsigned int) hostptr);
1377 up_read(&current->mm->mmap_sem);
1378
1379 if (!vma) {
1380 KGSL_CORE_ERR("find_vma(%p) failed\n", hostptr);
1381 return -EINVAL;
1382 }
1383
1384 /* We don't necessarily start at vma->vm_start */
1385 len = vma->vm_end - (unsigned long) hostptr;
1386
1387 if (offset >= len)
1388 return -EINVAL;
1389
1390 if (!KGSL_IS_PAGE_ALIGNED((unsigned long) hostptr) ||
1391 !KGSL_IS_PAGE_ALIGNED(len)) {
1392 KGSL_CORE_ERR("user address len(%u)"
1393 "and start(%p) must be page"
1394 "aligned\n", len, hostptr);
1395 return -EINVAL;
1396 }
1397
1398 if (size == 0)
1399 size = len;
1400
1401 /* Adjust the size of the region to account for the offset */
1402 size += offset & ~PAGE_MASK;
1403
1404 size = ALIGN(size, PAGE_SIZE);
1405
1406 if (_check_region(offset & PAGE_MASK, size, len)) {
1407 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1408 "than region length %d\n",
1409 offset & PAGE_MASK, size, len);
1410 return -EINVAL;
1411 }
1412
1413 entry->memdesc.pagetable = pagetable;
1414 entry->memdesc.size = size;
1415 entry->memdesc.hostptr = hostptr + (offset & PAGE_MASK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416
Jordan Croused17e9aa2011-10-12 16:57:48 -06001417 return memdesc_sg_virt(&entry->memdesc,
1418 hostptr + (offset & PAGE_MASK), size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419}
1420
1421#ifdef CONFIG_ASHMEM
1422static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1423 struct kgsl_pagetable *pagetable,
1424 int fd, void *hostptr, size_t size)
1425{
1426 int ret;
1427 struct vm_area_struct *vma;
1428 struct file *filep, *vmfile;
1429 unsigned long len;
Jordan Crouse2c542b62011-07-26 08:30:20 -06001430 unsigned int hostaddr = (unsigned int) hostptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431
Jordan Crouse2c542b62011-07-26 08:30:20 -06001432 vma = kgsl_get_vma_from_start_addr(hostaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 if (vma == NULL)
1434 return -EINVAL;
1435
Jordan Crouse2c542b62011-07-26 08:30:20 -06001436 if (vma->vm_pgoff || vma->vm_start != hostaddr) {
1437 KGSL_CORE_ERR("Invalid vma region\n");
1438 return -EINVAL;
1439 }
1440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001441 len = vma->vm_end - vma->vm_start;
1442
1443 if (size == 0)
1444 size = len;
1445
1446 if (size != len) {
1447 KGSL_CORE_ERR("Invalid size %d for vma region %p\n",
1448 size, hostptr);
1449 return -EINVAL;
1450 }
1451
1452 ret = get_ashmem_file(fd, &filep, &vmfile, &len);
1453
1454 if (ret) {
1455 KGSL_CORE_ERR("get_ashmem_file failed\n");
1456 return ret;
1457 }
1458
1459 if (vmfile != vma->vm_file) {
1460 KGSL_CORE_ERR("ashmem shmem file does not match vma\n");
1461 ret = -EINVAL;
1462 goto err;
1463 }
1464
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001465 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001466 entry->memdesc.pagetable = pagetable;
1467 entry->memdesc.size = ALIGN(size, PAGE_SIZE);
1468 entry->memdesc.hostptr = hostptr;
Jordan Croused17e9aa2011-10-12 16:57:48 -06001469
1470 ret = memdesc_sg_virt(&entry->memdesc, hostptr, size);
1471 if (ret)
1472 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001473
1474 return 0;
1475
1476err:
1477 put_ashmem_file(filep);
1478 return ret;
1479}
1480#else
1481static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1482 struct kgsl_pagetable *pagetable,
1483 int fd, void *hostptr, size_t size)
1484{
1485 return -EINVAL;
1486}
1487#endif
1488
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001489static int kgsl_setup_ion(struct kgsl_mem_entry *entry,
1490 struct kgsl_pagetable *pagetable, int fd)
1491{
1492 struct ion_handle *handle;
1493 struct scatterlist *s;
1494 unsigned long flags;
1495
1496 if (kgsl_ion_client == NULL) {
1497 kgsl_ion_client = msm_ion_client_create(UINT_MAX, KGSL_NAME);
1498 if (kgsl_ion_client == NULL)
1499 return -ENODEV;
1500 }
1501
1502 handle = ion_import_fd(kgsl_ion_client, fd);
1503 if (IS_ERR_OR_NULL(handle))
1504 return PTR_ERR(handle);
1505
1506 entry->memtype = KGSL_MEM_ENTRY_ION;
1507 entry->priv_data = handle;
1508 entry->memdesc.pagetable = pagetable;
1509 entry->memdesc.size = 0;
1510
1511 if (ion_handle_get_flags(kgsl_ion_client, handle, &flags))
1512 goto err;
1513
1514 entry->memdesc.sg = ion_map_dma(kgsl_ion_client, handle, flags);
1515
1516 if (IS_ERR_OR_NULL(entry->memdesc.sg))
1517 goto err;
1518
1519 /* Calculate the size of the memdesc from the sglist */
1520
1521 entry->memdesc.sglen = 0;
1522
1523 for (s = entry->memdesc.sg; s != NULL; s = sg_next(s)) {
1524 entry->memdesc.size += s->length;
1525 entry->memdesc.sglen++;
1526 }
1527
1528 return 0;
1529err:
1530 ion_free(kgsl_ion_client, handle);
1531 return -ENOMEM;
1532}
1533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001534static long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
1535 unsigned int cmd, void *data)
1536{
1537 int result = -EINVAL;
1538 struct kgsl_map_user_mem *param = data;
1539 struct kgsl_mem_entry *entry = NULL;
1540 struct kgsl_process_private *private = dev_priv->process_priv;
Jason848741a2011-07-12 10:24:25 -07001541 enum kgsl_user_mem_type memtype;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542
1543 entry = kgsl_mem_entry_create();
1544
1545 if (entry == NULL)
1546 return -ENOMEM;
1547
Jason848741a2011-07-12 10:24:25 -07001548 if (_IOC_SIZE(cmd) == sizeof(struct kgsl_sharedmem_from_pmem))
1549 memtype = KGSL_USER_MEM_TYPE_PMEM;
1550 else
1551 memtype = param->memtype;
1552
1553 switch (memtype) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 case KGSL_USER_MEM_TYPE_PMEM:
1555 if (param->fd == 0 || param->len == 0)
1556 break;
1557
1558 result = kgsl_setup_phys_file(entry, private->pagetable,
1559 param->fd, param->offset,
1560 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001561 entry->memtype = KGSL_MEM_ENTRY_PMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562 break;
1563
1564 case KGSL_USER_MEM_TYPE_ADDR:
1565 if (!kgsl_mmu_enabled()) {
1566 KGSL_DRV_ERR(dev_priv->device,
1567 "Cannot map paged memory with the "
1568 "MMU disabled\n");
1569 break;
1570 }
1571
1572 if (param->hostptr == 0)
1573 break;
1574
1575 result = kgsl_setup_hostptr(entry, private->pagetable,
1576 (void *) param->hostptr,
1577 param->offset, param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001578 entry->memtype = KGSL_MEM_ENTRY_USER;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579 break;
1580
1581 case KGSL_USER_MEM_TYPE_ASHMEM:
1582 if (!kgsl_mmu_enabled()) {
1583 KGSL_DRV_ERR(dev_priv->device,
1584 "Cannot map paged memory with the "
1585 "MMU disabled\n");
1586 break;
1587 }
1588
1589 if (param->hostptr == 0)
1590 break;
1591
1592 result = kgsl_setup_ashmem(entry, private->pagetable,
1593 param->fd, (void *) param->hostptr,
1594 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001595
1596 entry->memtype = KGSL_MEM_ENTRY_ASHMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001597 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001598 case KGSL_USER_MEM_TYPE_ION:
1599 result = kgsl_setup_ion(entry, private->pagetable,
1600 param->fd);
1601 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001602 default:
Jason848741a2011-07-12 10:24:25 -07001603 KGSL_CORE_ERR("Invalid memory type: %x\n", memtype);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 break;
1605 }
1606
1607 if (result)
1608 goto error;
1609
1610 result = kgsl_mmu_map(private->pagetable,
1611 &entry->memdesc,
1612 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
1613
1614 if (result)
1615 goto error_put_file_ptr;
1616
1617 /* Adjust the returned value for a non 4k aligned offset */
1618 param->gpuaddr = entry->memdesc.gpuaddr + (param->offset & ~PAGE_MASK);
1619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620 KGSL_STATS_ADD(param->len, kgsl_driver.stats.mapped,
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001621 kgsl_driver.stats.mapped_max);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001623 kgsl_process_add_stats(private, entry->memtype, param->len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001624
1625 kgsl_mem_entry_attach_process(entry, private);
1626
1627 kgsl_check_idle(dev_priv->device);
1628 return result;
1629
1630 error_put_file_ptr:
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001631 if (entry->priv_data)
1632 fput(entry->priv_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633
1634error:
1635 kfree(entry);
1636 kgsl_check_idle(dev_priv->device);
1637 return result;
1638}
1639
1640/*This function flushes a graphics memory allocation from CPU cache
1641 *when caching is enabled with MMU*/
1642static long
1643kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
1644 unsigned int cmd, void *data)
1645{
1646 int result = 0;
1647 struct kgsl_mem_entry *entry;
1648 struct kgsl_sharedmem_free *param = data;
1649 struct kgsl_process_private *private = dev_priv->process_priv;
1650
1651 spin_lock(&private->mem_lock);
1652 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1653 if (!entry) {
1654 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1655 result = -EINVAL;
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001656 goto done;
1657 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001658 if (!entry->memdesc.hostptr) {
1659 KGSL_CORE_ERR("invalid hostptr with gpuaddr %08x\n",
1660 param->gpuaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001661 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001663
1664 kgsl_cache_range_op(&entry->memdesc, KGSL_CACHE_OP_CLEAN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665done:
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001666 spin_unlock(&private->mem_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667 return result;
1668}
1669
1670static long
1671kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
1672 unsigned int cmd, void *data)
1673{
1674 struct kgsl_process_private *private = dev_priv->process_priv;
1675 struct kgsl_gpumem_alloc *param = data;
1676 struct kgsl_mem_entry *entry;
1677 int result;
1678
1679 entry = kgsl_mem_entry_create();
1680 if (entry == NULL)
1681 return -ENOMEM;
1682
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001683 result = kgsl_allocate_user(&entry->memdesc, private->pagetable,
1684 param->size, param->flags);
1685
1686 if (result == 0) {
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001687 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688 kgsl_mem_entry_attach_process(entry, private);
1689 param->gpuaddr = entry->memdesc.gpuaddr;
1690
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001691 kgsl_process_add_stats(private, entry->memtype, param->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001692 } else
1693 kfree(entry);
1694
1695 kgsl_check_idle(dev_priv->device);
1696 return result;
1697}
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001698static long kgsl_ioctl_cff_syncmem(struct kgsl_device_private *dev_priv,
1699 unsigned int cmd, void *data)
1700{
1701 int result = 0;
1702 struct kgsl_cff_syncmem *param = data;
1703 struct kgsl_process_private *private = dev_priv->process_priv;
1704 struct kgsl_mem_entry *entry = NULL;
1705
1706 spin_lock(&private->mem_lock);
1707 entry = kgsl_sharedmem_find_region(private, param->gpuaddr, param->len);
1708 if (entry)
1709 kgsl_cffdump_syncmem(dev_priv, &entry->memdesc, param->gpuaddr,
1710 param->len, true);
1711 else
1712 result = -EINVAL;
1713 spin_unlock(&private->mem_lock);
1714 return result;
1715}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001716
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001717static long kgsl_ioctl_cff_user_event(struct kgsl_device_private *dev_priv,
1718 unsigned int cmd, void *data)
1719{
1720 int result = 0;
1721 struct kgsl_cff_user_event *param = data;
1722
1723 kgsl_cffdump_user_event(param->cff_opcode, param->op1, param->op2,
1724 param->op3, param->op4, param->op5);
1725
1726 return result;
1727}
1728
Jordan Croused4bc9d22011-11-17 13:39:21 -07001729#ifdef CONFIG_GENLOCK
1730struct kgsl_genlock_event_priv {
1731 struct genlock_handle *handle;
1732 struct genlock *lock;
1733};
1734
1735/**
1736 * kgsl_genlock_event_cb - Event callback for a genlock timestamp event
1737 * @device - The KGSL device that expired the timestamp
1738 * @priv - private data for the event
1739 * @timestamp - the timestamp that triggered the event
1740 *
1741 * Release a genlock lock following the expiration of a timestamp
1742 */
1743
1744static void kgsl_genlock_event_cb(struct kgsl_device *device,
1745 void *priv, u32 timestamp)
1746{
1747 struct kgsl_genlock_event_priv *ev = priv;
1748 int ret;
1749
1750 ret = genlock_lock(ev->handle, GENLOCK_UNLOCK, 0, 0);
1751 if (ret)
1752 KGSL_CORE_ERR("Error while unlocking genlock: %d\n", ret);
1753
1754 genlock_put_handle(ev->handle);
1755
1756 kfree(ev);
1757}
1758
1759/**
1760 * kgsl_add_genlock-event - Create a new genlock event
1761 * @device - KGSL device to create the event on
1762 * @timestamp - Timestamp to trigger the event
1763 * @data - User space buffer containing struct kgsl_genlock_event_priv
1764 * @len - length of the userspace buffer
1765 * @returns 0 on success or error code on error
1766 *
1767 * Attack to a genlock handle and register an event to release the
1768 * genlock lock when the timestamp expires
1769 */
1770
1771static int kgsl_add_genlock_event(struct kgsl_device *device,
1772 u32 timestamp, void __user *data, int len)
1773{
1774 struct kgsl_genlock_event_priv *event;
1775 struct kgsl_timestamp_event_genlock priv;
1776 int ret;
1777
1778 if (len != sizeof(priv))
1779 return -EINVAL;
1780
1781 if (copy_from_user(&priv, data, sizeof(priv)))
1782 return -EFAULT;
1783
1784 event = kzalloc(sizeof(*event), GFP_KERNEL);
1785
1786 if (event == NULL)
1787 return -ENOMEM;
1788
1789 event->handle = genlock_get_handle_fd(priv.handle);
1790
1791 if (IS_ERR(event->handle)) {
1792 int ret = PTR_ERR(event->handle);
1793 kfree(event);
1794 return ret;
1795 }
1796
1797 ret = kgsl_add_event(device, timestamp, kgsl_genlock_event_cb, event);
1798 if (ret)
1799 kfree(event);
1800
1801 return ret;
1802}
1803#else
1804static long kgsl_add_genlock_event(struct kgsl_device *device,
1805 u32 timestamp, void __user *data, int len)
1806{
1807 return -EINVAL;
1808}
1809#endif
1810
1811/**
1812 * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace
1813 * @dev_priv - pointer to the private device structure
1814 * @cmd - the ioctl cmd passed from kgsl_ioctl
1815 * @data - the user data buffer from kgsl_ioctl
1816 * @returns 0 on success or error code on failure
1817 */
1818
1819static long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
1820 unsigned int cmd, void *data)
1821{
1822 struct kgsl_timestamp_event *param = data;
1823 int ret;
1824
1825 switch (param->type) {
1826 case KGSL_TIMESTAMP_EVENT_GENLOCK:
1827 ret = kgsl_add_genlock_event(dev_priv->device,
1828 param->timestamp, param->priv, param->len);
1829 break;
1830 default:
1831 ret = -EINVAL;
1832 }
1833
1834 return ret;
1835}
1836
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001837typedef long (*kgsl_ioctl_func_t)(struct kgsl_device_private *,
1838 unsigned int, void *);
1839
1840#define KGSL_IOCTL_FUNC(_cmd, _func, _lock) \
1841 [_IOC_NR(_cmd)] = { .cmd = _cmd, .func = _func, .lock = _lock }
1842
1843static const struct {
1844 unsigned int cmd;
1845 kgsl_ioctl_func_t func;
1846 int lock;
1847} kgsl_ioctl_funcs[] = {
1848 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_GETPROPERTY,
1849 kgsl_ioctl_device_getproperty, 1),
1850 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_WAITTIMESTAMP,
1851 kgsl_ioctl_device_waittimestamp, 1),
1852 KGSL_IOCTL_FUNC(IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS,
1853 kgsl_ioctl_rb_issueibcmds, 1),
1854 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_READTIMESTAMP,
1855 kgsl_ioctl_cmdstream_readtimestamp, 1),
1856 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP,
1857 kgsl_ioctl_cmdstream_freememontimestamp, 1),
1858 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_CREATE,
1859 kgsl_ioctl_drawctxt_create, 1),
1860 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_DESTROY,
1861 kgsl_ioctl_drawctxt_destroy, 1),
1862 KGSL_IOCTL_FUNC(IOCTL_KGSL_MAP_USER_MEM,
1863 kgsl_ioctl_map_user_mem, 0),
1864 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_PMEM,
1865 kgsl_ioctl_map_user_mem, 0),
1866 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FREE,
1867 kgsl_ioctl_sharedmem_free, 0),
1868 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_VMALLOC,
1869 kgsl_ioctl_sharedmem_from_vmalloc, 0),
1870 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FLUSH_CACHE,
1871 kgsl_ioctl_sharedmem_flush_cache, 0),
1872 KGSL_IOCTL_FUNC(IOCTL_KGSL_GPUMEM_ALLOC,
1873 kgsl_ioctl_gpumem_alloc, 0),
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001874 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_SYNCMEM,
1875 kgsl_ioctl_cff_syncmem, 0),
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001876 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_USER_EVENT,
1877 kgsl_ioctl_cff_user_event, 0),
Jordan Croused4bc9d22011-11-17 13:39:21 -07001878 KGSL_IOCTL_FUNC(IOCTL_KGSL_TIMESTAMP_EVENT,
Lucille Sylvester9329cf02011-12-02 14:30:41 -07001879 kgsl_ioctl_timestamp_event, 1),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880};
1881
1882static long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1883{
1884 struct kgsl_device_private *dev_priv = filep->private_data;
1885 unsigned int nr = _IOC_NR(cmd);
1886 kgsl_ioctl_func_t func;
1887 int lock, ret;
1888 char ustack[64];
1889 void *uptr = NULL;
1890
1891 BUG_ON(dev_priv == NULL);
1892
1893 /* Workaround for an previously incorrectly defined ioctl code.
1894 This helps ensure binary compatability */
1895
1896 if (cmd == IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP_OLD)
1897 cmd = IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP;
Jason Varbedian80ba33d2011-07-11 17:29:05 -07001898 else if (cmd == IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_OLD)
1899 cmd = IOCTL_KGSL_CMDSTREAM_READTIMESTAMP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900
1901 if (cmd & (IOC_IN | IOC_OUT)) {
1902 if (_IOC_SIZE(cmd) < sizeof(ustack))
1903 uptr = ustack;
1904 else {
1905 uptr = kzalloc(_IOC_SIZE(cmd), GFP_KERNEL);
1906 if (uptr == NULL) {
1907 KGSL_MEM_ERR(dev_priv->device,
1908 "kzalloc(%d) failed\n", _IOC_SIZE(cmd));
1909 ret = -ENOMEM;
1910 goto done;
1911 }
1912 }
1913
1914 if (cmd & IOC_IN) {
1915 if (copy_from_user(uptr, (void __user *) arg,
1916 _IOC_SIZE(cmd))) {
1917 ret = -EFAULT;
1918 goto done;
1919 }
1920 } else
1921 memset(uptr, 0, _IOC_SIZE(cmd));
1922 }
1923
1924 if (nr < ARRAY_SIZE(kgsl_ioctl_funcs) &&
1925 kgsl_ioctl_funcs[nr].func != NULL) {
1926 func = kgsl_ioctl_funcs[nr].func;
1927 lock = kgsl_ioctl_funcs[nr].lock;
1928 } else {
1929 func = dev_priv->device->ftbl->ioctl;
1930 if (!func) {
1931 KGSL_DRV_INFO(dev_priv->device,
1932 "invalid ioctl code %08x\n", cmd);
1933 ret = -EINVAL;
1934 goto done;
1935 }
1936 lock = 1;
1937 }
1938
1939 if (lock) {
1940 mutex_lock(&dev_priv->device->mutex);
1941 kgsl_check_suspended(dev_priv->device);
1942 }
1943
1944 ret = func(dev_priv, cmd, uptr);
1945
1946 if (lock) {
1947 kgsl_check_idle_locked(dev_priv->device);
1948 mutex_unlock(&dev_priv->device->mutex);
1949 }
1950
1951 if (ret == 0 && (cmd & IOC_OUT)) {
1952 if (copy_to_user((void __user *) arg, uptr, _IOC_SIZE(cmd)))
1953 ret = -EFAULT;
1954 }
1955
1956done:
1957 if (_IOC_SIZE(cmd) >= sizeof(ustack))
1958 kfree(uptr);
1959
1960 return ret;
1961}
1962
1963static int
1964kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
1965{
1966 struct kgsl_memdesc *memdesc = &device->memstore;
1967 int result;
1968 unsigned int vma_size = vma->vm_end - vma->vm_start;
1969
1970 /* The memstore can only be mapped as read only */
1971
1972 if (vma->vm_flags & VM_WRITE)
1973 return -EPERM;
1974
1975 if (memdesc->size != vma_size) {
1976 KGSL_MEM_ERR(device, "memstore bad size: %d should be %d\n",
1977 vma_size, memdesc->size);
1978 return -EINVAL;
1979 }
1980
1981 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1982
1983 result = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1984 vma_size, vma->vm_page_prot);
1985 if (result != 0)
1986 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
1987 result);
1988
1989 return result;
1990}
1991
Jordan Crouse4283e172011-09-26 14:45:47 -06001992/*
1993 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
1994 * Increase the refcount to make sure that the accounting stays correct
1995 */
1996
1997static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
1998{
1999 struct kgsl_mem_entry *entry = vma->vm_private_data;
2000 kgsl_mem_entry_get(entry);
2001}
2002
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003static int
2004kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2005{
2006 struct kgsl_mem_entry *entry = vma->vm_private_data;
2007
Jordan Croused17e9aa2011-10-12 16:57:48 -06002008 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009 return VM_FAULT_SIGBUS;
2010
2011 return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
2012}
2013
2014static void
2015kgsl_gpumem_vm_close(struct vm_area_struct *vma)
2016{
2017 struct kgsl_mem_entry *entry = vma->vm_private_data;
2018 kgsl_mem_entry_put(entry);
2019}
2020
2021static struct vm_operations_struct kgsl_gpumem_vm_ops = {
Jordan Crouse4283e172011-09-26 14:45:47 -06002022 .open = kgsl_gpumem_vm_open,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023 .fault = kgsl_gpumem_vm_fault,
2024 .close = kgsl_gpumem_vm_close,
2025};
2026
2027static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
2028{
2029 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030 struct kgsl_device_private *dev_priv = file->private_data;
2031 struct kgsl_process_private *private = dev_priv->process_priv;
Jordan Crouse976cf0e2011-09-12 10:41:49 -06002032 struct kgsl_mem_entry *tmp, *entry = NULL;
Jordan Crouse2db0af92011-08-08 16:05:09 -06002033 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034
2035 /* Handle leagacy behavior for memstore */
2036
2037 if (vma_offset == device->memstore.physaddr)
2038 return kgsl_mmap_memstore(device, vma);
2039
2040 /* Find a chunk of GPU memory */
2041
2042 spin_lock(&private->mem_lock);
Jordan Crouse976cf0e2011-09-12 10:41:49 -06002043 list_for_each_entry(tmp, &private->mem_list, list) {
2044 if (vma_offset == tmp->memdesc.gpuaddr) {
2045 kgsl_mem_entry_get(tmp);
2046 entry = tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002047 break;
2048 }
2049 }
2050 spin_unlock(&private->mem_lock);
2051
2052 if (entry == NULL)
2053 return -EINVAL;
2054
Jordan Croused17e9aa2011-10-12 16:57:48 -06002055 if (!entry->memdesc.ops ||
2056 !entry->memdesc.ops->vmflags ||
2057 !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058 return -EINVAL;
2059
2060 vma->vm_flags |= entry->memdesc.ops->vmflags(&entry->memdesc);
2061
2062 vma->vm_private_data = entry;
2063 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
2064 vma->vm_ops = &kgsl_gpumem_vm_ops;
2065 vma->vm_file = file;
2066
2067 return 0;
2068}
2069
2070static const struct file_operations kgsl_fops = {
2071 .owner = THIS_MODULE,
2072 .release = kgsl_release,
2073 .open = kgsl_open,
2074 .mmap = kgsl_mmap,
2075 .unlocked_ioctl = kgsl_ioctl,
2076};
2077
2078struct kgsl_driver kgsl_driver = {
2079 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
2080 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
2081 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
2082};
2083EXPORT_SYMBOL(kgsl_driver);
2084
2085void kgsl_unregister_device(struct kgsl_device *device)
2086{
2087 int minor;
2088
2089 mutex_lock(&kgsl_driver.devlock);
2090 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2091 if (device == kgsl_driver.devp[minor])
2092 break;
2093 }
2094
2095 mutex_unlock(&kgsl_driver.devlock);
2096
2097 if (minor == KGSL_DEVICE_MAX)
2098 return;
2099
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002100 kgsl_device_snapshot_close(device);
2101
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 kgsl_cffdump_close(device->id);
2103 kgsl_pwrctrl_uninit_sysfs(device);
2104
Lucille Sylvesteref44e7332011-11-02 13:21:17 -07002105 if (cpu_is_msm8x60())
2106 wake_lock_destroy(&device->idle_wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107
2108 idr_destroy(&device->context_idr);
2109
2110 if (device->memstore.hostptr)
2111 kgsl_sharedmem_free(&device->memstore);
2112
2113 kgsl_mmu_close(device);
2114
2115 if (device->work_queue) {
2116 destroy_workqueue(device->work_queue);
2117 device->work_queue = NULL;
2118 }
2119
2120 device_destroy(kgsl_driver.class,
2121 MKDEV(MAJOR(kgsl_driver.major), minor));
2122
2123 mutex_lock(&kgsl_driver.devlock);
2124 kgsl_driver.devp[minor] = NULL;
2125 mutex_unlock(&kgsl_driver.devlock);
2126}
2127EXPORT_SYMBOL(kgsl_unregister_device);
2128
2129int
2130kgsl_register_device(struct kgsl_device *device)
2131{
2132 int minor, ret;
2133 dev_t dev;
2134
2135 /* Find a minor for the device */
2136
2137 mutex_lock(&kgsl_driver.devlock);
2138 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2139 if (kgsl_driver.devp[minor] == NULL) {
2140 kgsl_driver.devp[minor] = device;
2141 break;
2142 }
2143 }
2144
2145 mutex_unlock(&kgsl_driver.devlock);
2146
2147 if (minor == KGSL_DEVICE_MAX) {
2148 KGSL_CORE_ERR("minor devices exhausted\n");
2149 return -ENODEV;
2150 }
2151
2152 /* Create the device */
2153 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
2154 device->dev = device_create(kgsl_driver.class,
2155 device->parentdev,
2156 dev, device,
2157 device->name);
2158
2159 if (IS_ERR(device->dev)) {
2160 ret = PTR_ERR(device->dev);
2161 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
2162 goto err_devlist;
2163 }
2164
2165 dev_set_drvdata(device->parentdev, device);
2166
2167 /* Generic device initialization */
2168 init_waitqueue_head(&device->wait_queue);
2169
2170 kgsl_cffdump_open(device->id);
2171
2172 init_completion(&device->hwaccess_gate);
2173 init_completion(&device->suspend_gate);
2174
2175 ATOMIC_INIT_NOTIFIER_HEAD(&device->ts_notifier_list);
2176
2177 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
2178 ret = kgsl_create_device_workqueue(device);
2179 if (ret)
2180 goto err_devlist;
2181
2182 INIT_WORK(&device->idle_check_ws, kgsl_idle_check);
Jordan Crouse1bf80aa2011-10-12 16:57:47 -06002183 INIT_WORK(&device->ts_expired_ws, kgsl_timestamp_expired);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184
2185 INIT_LIST_HEAD(&device->memqueue);
Jordan Croused4bc9d22011-11-17 13:39:21 -07002186 INIT_LIST_HEAD(&device->events);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187
2188 ret = kgsl_mmu_init(device);
2189 if (ret != 0)
2190 goto err_dest_work_q;
2191
2192 ret = kgsl_allocate_contiguous(&device->memstore,
2193 sizeof(struct kgsl_devmemstore));
2194
2195 if (ret != 0)
2196 goto err_close_mmu;
2197
Lucille Sylvesteref44e7332011-11-02 13:21:17 -07002198 if (cpu_is_msm8x60())
2199 wake_lock_init(&device->idle_wakelock,
2200 WAKE_LOCK_IDLE, device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201
2202 idr_init(&device->context_idr);
2203
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002204 /* Initalize the snapshot engine */
2205 kgsl_device_snapshot_init(device);
2206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002207 /* sysfs and debugfs initalization - failure here is non fatal */
2208
2209 /* Initialize logging */
2210 kgsl_device_debugfs_init(device);
2211
2212 /* Initialize common sysfs entries */
2213 kgsl_pwrctrl_init_sysfs(device);
2214
2215 return 0;
2216
2217err_close_mmu:
2218 kgsl_mmu_close(device);
2219err_dest_work_q:
2220 destroy_workqueue(device->work_queue);
2221 device->work_queue = NULL;
2222err_devlist:
2223 mutex_lock(&kgsl_driver.devlock);
2224 kgsl_driver.devp[minor] = NULL;
2225 mutex_unlock(&kgsl_driver.devlock);
2226
2227 return ret;
2228}
2229EXPORT_SYMBOL(kgsl_register_device);
2230
2231int kgsl_device_platform_probe(struct kgsl_device *device,
2232 irqreturn_t (*dev_isr) (int, void*))
2233{
Michael Street8bacdd02012-01-05 14:55:01 -08002234 int result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002235 int status = -EINVAL;
2236 struct kgsl_memregion *regspace = NULL;
2237 struct resource *res;
2238 struct platform_device *pdev =
2239 container_of(device->parentdev, struct platform_device, dev);
2240
2241 pm_runtime_enable(device->parentdev);
2242
2243 status = kgsl_pwrctrl_init(device);
2244 if (status)
2245 goto error;
2246
2247 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2248 device->iomemname);
2249 if (res == NULL) {
2250 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
2251 status = -EINVAL;
2252 goto error_pwrctrl_close;
2253 }
2254 if (res->start == 0 || resource_size(res) == 0) {
2255 KGSL_DRV_ERR(device, "dev %d invalid regspace\n", device->id);
2256 status = -EINVAL;
2257 goto error_pwrctrl_close;
2258 }
2259
2260 regspace = &device->regspace;
2261 regspace->mmio_phys_base = res->start;
2262 regspace->sizebytes = resource_size(res);
2263
2264 if (!request_mem_region(regspace->mmio_phys_base,
2265 regspace->sizebytes, device->name)) {
2266 KGSL_DRV_ERR(device, "request_mem_region failed\n");
2267 status = -ENODEV;
2268 goto error_pwrctrl_close;
2269 }
2270
2271 regspace->mmio_virt_base = ioremap(regspace->mmio_phys_base,
2272 regspace->sizebytes);
2273
2274 if (regspace->mmio_virt_base == NULL) {
2275 KGSL_DRV_ERR(device, "ioremap failed\n");
2276 status = -ENODEV;
2277 goto error_release_mem;
2278 }
2279
2280 status = request_irq(device->pwrctrl.interrupt_num, dev_isr,
2281 IRQF_TRIGGER_HIGH, device->name, device);
2282 if (status) {
2283 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
2284 device->pwrctrl.interrupt_num, status);
2285 goto error_iounmap;
2286 }
2287 device->pwrctrl.have_irq = 1;
2288 disable_irq(device->pwrctrl.interrupt_num);
2289
2290 KGSL_DRV_INFO(device,
2291 "dev_id %d regs phys 0x%08x size 0x%08x virt %p\n",
2292 device->id, regspace->mmio_phys_base,
2293 regspace->sizebytes, regspace->mmio_virt_base);
2294
Michael Street8bacdd02012-01-05 14:55:01 -08002295 result = kgsl_drm_init(pdev);
2296 if (result)
2297 goto error_iounmap;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002298
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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002433 return 0;
2434
2435err:
2436 kgsl_core_exit();
2437 return result;
2438}
2439
2440module_init(kgsl_core_init);
2441module_exit(kgsl_core_exit);
2442
2443MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
2444MODULE_DESCRIPTION("MSM GPU driver");
2445MODULE_LICENSE("GPL");