blob: 1a0a17d893429ed86f6a755140995a62d8ace422 [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
Jeremy Gebben63904832012-02-07 16:10:55 -0700105 queue_work(device->work_queue, &device->ts_expired_ws);
Jordan Croused4bc9d22011-11-17 13:39:21 -0700106 return 0;
107}
108#endif
109
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110static inline struct kgsl_mem_entry *
111kgsl_mem_entry_create(void)
112{
113 struct kgsl_mem_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
114
115 if (!entry)
116 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*entry));
117 else
118 kref_init(&entry->refcount);
119
120 return entry;
121}
122
123void
124kgsl_mem_entry_destroy(struct kref *kref)
125{
126 struct kgsl_mem_entry *entry = container_of(kref,
127 struct kgsl_mem_entry,
128 refcount);
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600129
130 entry->priv->stats[entry->memtype].cur -= entry->memdesc.size;
131
132 if (entry->memtype != KGSL_MEM_ENTRY_KERNEL)
133 kgsl_driver.stats.mapped -= entry->memdesc.size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600135 /*
136 * Ion takes care of freeing the sglist for us (how nice </sarcasm>) so
137 * unmap the dma before freeing the sharedmem so kgsl_sharedmem_free
138 * doesn't try to free it again
139 */
140
141 if (entry->memtype == KGSL_MEM_ENTRY_ION) {
142 ion_unmap_dma(kgsl_ion_client, entry->priv_data);
143 entry->memdesc.sg = NULL;
144 }
145
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 kgsl_sharedmem_free(&entry->memdesc);
147
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600148 switch (entry->memtype) {
149 case KGSL_MEM_ENTRY_PMEM:
150 case KGSL_MEM_ENTRY_ASHMEM:
151 if (entry->priv_data)
152 fput(entry->priv_data);
153 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600154 case KGSL_MEM_ENTRY_ION:
155 ion_free(kgsl_ion_client, entry->priv_data);
156 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 }
158
159 kfree(entry);
160}
161EXPORT_SYMBOL(kgsl_mem_entry_destroy);
162
163static
164void kgsl_mem_entry_attach_process(struct kgsl_mem_entry *entry,
165 struct kgsl_process_private *process)
166{
167 spin_lock(&process->mem_lock);
168 list_add(&entry->list, &process->mem_list);
169 spin_unlock(&process->mem_lock);
170
171 entry->priv = process;
172}
173
174/* Allocate a new context id */
175
176static struct kgsl_context *
177kgsl_create_context(struct kgsl_device_private *dev_priv)
178{
179 struct kgsl_context *context;
180 int ret, id;
181
182 context = kzalloc(sizeof(*context), GFP_KERNEL);
183
184 if (context == NULL)
185 return NULL;
186
187 while (1) {
188 if (idr_pre_get(&dev_priv->device->context_idr,
189 GFP_KERNEL) == 0) {
190 kfree(context);
191 return NULL;
192 }
193
194 ret = idr_get_new(&dev_priv->device->context_idr,
195 context, &id);
196
197 if (ret != -EAGAIN)
198 break;
199 }
200
201 if (ret) {
202 kfree(context);
203 return NULL;
204 }
205
206 context->id = id;
207 context->dev_priv = dev_priv;
208
209 return context;
210}
211
212static void
213kgsl_destroy_context(struct kgsl_device_private *dev_priv,
214 struct kgsl_context *context)
215{
216 int id;
217
218 if (context == NULL)
219 return;
220
221 /* Fire a bug if the devctxt hasn't been freed */
222 BUG_ON(context->devctxt);
223
224 id = context->id;
225 kfree(context);
226
227 idr_remove(&dev_priv->device->context_idr, id);
228}
229
230/* to be called when a process is destroyed, this walks the memqueue and
231 * frees any entryies that belong to the dying process
232 */
233static void kgsl_memqueue_cleanup(struct kgsl_device *device,
234 struct kgsl_process_private *private)
235{
236 struct kgsl_mem_entry *entry, *entry_tmp;
237
238 if (!private)
239 return;
240
241 BUG_ON(!mutex_is_locked(&device->mutex));
242
243 list_for_each_entry_safe(entry, entry_tmp, &device->memqueue, list) {
244 if (entry->priv == private) {
245 list_del(&entry->list);
246 kgsl_mem_entry_put(entry);
247 }
248 }
249}
250
251static void kgsl_memqueue_freememontimestamp(struct kgsl_device *device,
252 struct kgsl_mem_entry *entry,
253 uint32_t timestamp,
254 enum kgsl_timestamp_type type)
255{
256 BUG_ON(!mutex_is_locked(&device->mutex));
257
258 entry->free_timestamp = timestamp;
259
260 list_add_tail(&entry->list, &device->memqueue);
261}
262
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600263static void kgsl_timestamp_expired(struct work_struct *work)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264{
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600265 struct kgsl_device *device = container_of(work, struct kgsl_device,
266 ts_expired_ws);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 struct kgsl_mem_entry *entry, *entry_tmp;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700268 struct kgsl_event *event, *event_tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269 uint32_t ts_processed;
270
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600271 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272
273 /* get current EOP timestamp */
274 ts_processed = device->ftbl->readtimestamp(device,
275 KGSL_TIMESTAMP_RETIRED);
276
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600277 /* Flush the freememontimestamp queue */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 list_for_each_entry_safe(entry, entry_tmp, &device->memqueue, list) {
Jordan Crousee6239dd2011-11-17 13:39:21 -0700279 if (timestamp_cmp(ts_processed, entry->free_timestamp) < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 break;
281
282 list_del(&entry->list);
283 kgsl_mem_entry_put(entry);
284 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700285
Jordan Croused4bc9d22011-11-17 13:39:21 -0700286 /* Process expired events */
287 list_for_each_entry_safe(event, event_tmp, &device->events, list) {
288 if (timestamp_cmp(ts_processed, event->timestamp) < 0)
289 break;
290
291 if (event->func)
292 event->func(device, event->priv, ts_processed);
293
294 list_del(&event->list);
295 kfree(event);
296 }
297
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298 mutex_unlock(&device->mutex);
299}
300
301static void kgsl_check_idle_locked(struct kgsl_device *device)
302{
303 if (device->pwrctrl.nap_allowed == true &&
304 device->state == KGSL_STATE_ACTIVE &&
305 device->requested_state == KGSL_STATE_NONE) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700306 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 if (kgsl_pwrctrl_sleep(device) != 0)
308 mod_timer(&device->idle_timer,
309 jiffies +
310 device->pwrctrl.interval_timeout);
311 }
312}
313
314static void kgsl_check_idle(struct kgsl_device *device)
315{
316 mutex_lock(&device->mutex);
317 kgsl_check_idle_locked(device);
318 mutex_unlock(&device->mutex);
319}
320
321struct kgsl_device *kgsl_get_device(int dev_idx)
322{
323 int i;
324 struct kgsl_device *ret = NULL;
325
326 mutex_lock(&kgsl_driver.devlock);
327
328 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
329 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) {
330 ret = kgsl_driver.devp[i];
331 break;
332 }
333 }
334
335 mutex_unlock(&kgsl_driver.devlock);
336 return ret;
337}
338EXPORT_SYMBOL(kgsl_get_device);
339
340static struct kgsl_device *kgsl_get_minor(int minor)
341{
342 struct kgsl_device *ret = NULL;
343
344 if (minor < 0 || minor >= KGSL_DEVICE_MAX)
345 return NULL;
346
347 mutex_lock(&kgsl_driver.devlock);
348 ret = kgsl_driver.devp[minor];
349 mutex_unlock(&kgsl_driver.devlock);
350
351 return ret;
352}
353
354int kgsl_register_ts_notifier(struct kgsl_device *device,
355 struct notifier_block *nb)
356{
357 BUG_ON(device == NULL);
358 return atomic_notifier_chain_register(&device->ts_notifier_list,
359 nb);
360}
361EXPORT_SYMBOL(kgsl_register_ts_notifier);
362
363int kgsl_unregister_ts_notifier(struct kgsl_device *device,
364 struct notifier_block *nb)
365{
366 BUG_ON(device == NULL);
367 return atomic_notifier_chain_unregister(&device->ts_notifier_list,
368 nb);
369}
370EXPORT_SYMBOL(kgsl_unregister_ts_notifier);
371
372int kgsl_check_timestamp(struct kgsl_device *device, unsigned int timestamp)
373{
374 unsigned int ts_processed;
375
376 ts_processed = device->ftbl->readtimestamp(device,
377 KGSL_TIMESTAMP_RETIRED);
378
Jordan Crousee6239dd2011-11-17 13:39:21 -0700379 return (timestamp_cmp(ts_processed, timestamp) >= 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380}
381EXPORT_SYMBOL(kgsl_check_timestamp);
382
383static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state)
384{
385 int status = -EINVAL;
386 unsigned int nap_allowed_saved;
387 struct kgsl_pwrscale_policy *policy_saved;
388
389 if (!device)
390 return -EINVAL;
391
392 KGSL_PWR_WARN(device, "suspend start\n");
393
394 mutex_lock(&device->mutex);
395 nap_allowed_saved = device->pwrctrl.nap_allowed;
396 device->pwrctrl.nap_allowed = false;
397 policy_saved = device->pwrscale.policy;
398 device->pwrscale.policy = NULL;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700399 kgsl_pwrctrl_request_state(device, KGSL_STATE_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 /* Make sure no user process is waiting for a timestamp *
401 * before supending */
402 if (device->active_cnt != 0) {
403 mutex_unlock(&device->mutex);
404 wait_for_completion(&device->suspend_gate);
405 mutex_lock(&device->mutex);
406 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700407 switch (device->state) {
408 case KGSL_STATE_INIT:
409 break;
410 case KGSL_STATE_ACTIVE:
411 /* Wait for the device to become idle */
412 device->ftbl->idle(device, KGSL_TIMEOUT_DEFAULT);
413 case KGSL_STATE_NAP:
414 case KGSL_STATE_SLEEP:
415 /* Get the completion ready to be waited upon. */
416 INIT_COMPLETION(device->hwaccess_gate);
417 device->ftbl->suspend_context(device);
Tarun Karraf8e5cd22012-01-09 14:10:09 -0700418 kgsl_pwrctrl_stop_work(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419 device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700420 kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421 break;
Suman Tatiraju24569022011-10-27 11:11:12 -0700422 case KGSL_STATE_SLUMBER:
423 INIT_COMPLETION(device->hwaccess_gate);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700424 kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);
Suman Tatiraju24569022011-10-27 11:11:12 -0700425 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700426 default:
427 KGSL_PWR_ERR(device, "suspend fail, device %d\n",
428 device->id);
429 goto end;
430 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700431 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 device->pwrctrl.nap_allowed = nap_allowed_saved;
433 device->pwrscale.policy = policy_saved;
434 status = 0;
435
436end:
437 mutex_unlock(&device->mutex);
438 KGSL_PWR_WARN(device, "suspend end\n");
439 return status;
440}
441
442static int kgsl_resume_device(struct kgsl_device *device)
443{
444 int status = -EINVAL;
445
446 if (!device)
447 return -EINVAL;
448
449 KGSL_PWR_WARN(device, "resume start\n");
450 mutex_lock(&device->mutex);
451 if (device->state == KGSL_STATE_SUSPEND) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700452 kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju24569022011-10-27 11:11:12 -0700453 status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 complete_all(&device->hwaccess_gate);
455 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700456 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 KGSL_PWR_WARN(device, "resume end\n");
460 return status;
461}
462
463static int kgsl_suspend(struct device *dev)
464{
465
466 pm_message_t arg = {0};
467 struct kgsl_device *device = dev_get_drvdata(dev);
468 return kgsl_suspend_device(device, arg);
469}
470
471static int kgsl_resume(struct device *dev)
472{
473 struct kgsl_device *device = dev_get_drvdata(dev);
474 return kgsl_resume_device(device);
475}
476
477static int kgsl_runtime_suspend(struct device *dev)
478{
479 return 0;
480}
481
482static int kgsl_runtime_resume(struct device *dev)
483{
484 return 0;
485}
486
487const struct dev_pm_ops kgsl_pm_ops = {
488 .suspend = kgsl_suspend,
489 .resume = kgsl_resume,
490 .runtime_suspend = kgsl_runtime_suspend,
491 .runtime_resume = kgsl_runtime_resume,
492};
493EXPORT_SYMBOL(kgsl_pm_ops);
494
495void kgsl_early_suspend_driver(struct early_suspend *h)
496{
497 struct kgsl_device *device = container_of(h,
498 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700499 KGSL_PWR_WARN(device, "early suspend start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530500 mutex_lock(&device->mutex);
Tarun Karraf8e5cd22012-01-09 14:10:09 -0700501 kgsl_pwrctrl_stop_work(device);
Lucille Sylvester344e4622012-01-18 15:53:21 -0700502 kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju24569022011-10-27 11:11:12 -0700503 kgsl_pwrctrl_sleep(device);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530504 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700505 KGSL_PWR_WARN(device, "early suspend end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506}
507EXPORT_SYMBOL(kgsl_early_suspend_driver);
508
509int kgsl_suspend_driver(struct platform_device *pdev,
510 pm_message_t state)
511{
512 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
513 return kgsl_suspend_device(device, state);
514}
515EXPORT_SYMBOL(kgsl_suspend_driver);
516
517int kgsl_resume_driver(struct platform_device *pdev)
518{
519 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
520 return kgsl_resume_device(device);
521}
522EXPORT_SYMBOL(kgsl_resume_driver);
523
524void kgsl_late_resume_driver(struct early_suspend *h)
525{
526 struct kgsl_device *device = container_of(h,
527 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700528 KGSL_PWR_WARN(device, "late resume start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530529 mutex_lock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700530 kgsl_pwrctrl_wake(device);
531 device->pwrctrl.restore_slumber = 0;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700532 kgsl_pwrctrl_pwrlevel_change(device, KGSL_PWRLEVEL_TURBO);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530533 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700534 kgsl_check_idle(device);
535 KGSL_PWR_WARN(device, "late resume end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536}
537EXPORT_SYMBOL(kgsl_late_resume_driver);
538
539/* file operations */
540static struct kgsl_process_private *
541kgsl_get_process_private(struct kgsl_device_private *cur_dev_priv)
542{
543 struct kgsl_process_private *private;
544
545 mutex_lock(&kgsl_driver.process_mutex);
546 list_for_each_entry(private, &kgsl_driver.process_list, list) {
547 if (private->pid == task_tgid_nr(current)) {
548 private->refcnt++;
549 goto out;
550 }
551 }
552
553 /* no existing process private found for this dev_priv, create one */
554 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
555 if (private == NULL) {
556 KGSL_DRV_ERR(cur_dev_priv->device, "kzalloc(%d) failed\n",
557 sizeof(struct kgsl_process_private));
558 goto out;
559 }
560
561 spin_lock_init(&private->mem_lock);
562 private->refcnt = 1;
563 private->pid = task_tgid_nr(current);
564
565 INIT_LIST_HEAD(&private->mem_list);
566
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600567 if (kgsl_mmu_enabled())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568 {
569 unsigned long pt_name;
570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 pt_name = task_tgid_nr(current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572 private->pagetable = kgsl_mmu_getpagetable(pt_name);
573 if (private->pagetable == NULL) {
574 kfree(private);
575 private = NULL;
576 goto out;
577 }
578 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579
580 list_add(&private->list, &kgsl_driver.process_list);
581
582 kgsl_process_init_sysfs(private);
583
584out:
585 mutex_unlock(&kgsl_driver.process_mutex);
586 return private;
587}
588
589static void
590kgsl_put_process_private(struct kgsl_device *device,
591 struct kgsl_process_private *private)
592{
593 struct kgsl_mem_entry *entry = NULL;
594 struct kgsl_mem_entry *entry_tmp = NULL;
595
596 if (!private)
597 return;
598
599 mutex_lock(&kgsl_driver.process_mutex);
600
601 if (--private->refcnt)
602 goto unlock;
603
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 kgsl_process_uninit_sysfs(private);
605
606 list_del(&private->list);
607
608 list_for_each_entry_safe(entry, entry_tmp, &private->mem_list, list) {
609 list_del(&entry->list);
610 kgsl_mem_entry_put(entry);
611 }
612
613 kgsl_mmu_putpagetable(private->pagetable);
614 kfree(private);
615unlock:
616 mutex_unlock(&kgsl_driver.process_mutex);
617}
618
619static int kgsl_release(struct inode *inodep, struct file *filep)
620{
621 int result = 0;
Jordan Crouse2db0af92011-08-08 16:05:09 -0600622 struct kgsl_device_private *dev_priv = filep->private_data;
623 struct kgsl_process_private *private = dev_priv->process_priv;
624 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 struct kgsl_context *context;
626 int next = 0;
627
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700628 filep->private_data = NULL;
629
630 mutex_lock(&device->mutex);
631 kgsl_check_suspended(device);
632
633 while (1) {
Jordan Crouse2db0af92011-08-08 16:05:09 -0600634 context = idr_get_next(&device->context_idr, &next);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 if (context == NULL)
636 break;
637
638 if (context->dev_priv == dev_priv) {
639 device->ftbl->drawctxt_destroy(device, context);
640 kgsl_destroy_context(dev_priv, context);
641 }
642
643 next = next + 1;
644 }
645
646 device->open_count--;
647 if (device->open_count == 0) {
Tarun Karraf8e5cd22012-01-09 14:10:09 -0700648 kgsl_pwrctrl_stop_work(device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 result = device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700650 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 }
652 /* clean up any to-be-freed entries that belong to this
653 * process and this device
654 */
655 kgsl_memqueue_cleanup(device, private);
656
657 mutex_unlock(&device->mutex);
658 kfree(dev_priv);
659
660 kgsl_put_process_private(device, private);
661
662 pm_runtime_put(device->parentdev);
663 return result;
664}
665
666static int kgsl_open(struct inode *inodep, struct file *filep)
667{
668 int result;
669 struct kgsl_device_private *dev_priv;
670 struct kgsl_device *device;
671 unsigned int minor = iminor(inodep);
672
673 device = kgsl_get_minor(minor);
674 BUG_ON(device == NULL);
675
676 if (filep->f_flags & O_EXCL) {
677 KGSL_DRV_ERR(device, "O_EXCL not allowed\n");
678 return -EBUSY;
679 }
680
681 result = pm_runtime_get_sync(device->parentdev);
682 if (result < 0) {
683 KGSL_DRV_ERR(device,
684 "Runtime PM: Unable to wake up the device, rc = %d\n",
685 result);
686 return result;
687 }
688 result = 0;
689
690 dev_priv = kzalloc(sizeof(struct kgsl_device_private), GFP_KERNEL);
691 if (dev_priv == NULL) {
692 KGSL_DRV_ERR(device, "kzalloc failed(%d)\n",
693 sizeof(struct kgsl_device_private));
694 result = -ENOMEM;
695 goto err_pmruntime;
696 }
697
698 dev_priv->device = device;
699 filep->private_data = dev_priv;
700
701 /* Get file (per process) private struct */
702 dev_priv->process_priv = kgsl_get_process_private(dev_priv);
703 if (dev_priv->process_priv == NULL) {
704 result = -ENOMEM;
705 goto err_freedevpriv;
706 }
707
708 mutex_lock(&device->mutex);
709 kgsl_check_suspended(device);
710
711 if (device->open_count == 0) {
712 result = device->ftbl->start(device, true);
713
714 if (result) {
715 mutex_unlock(&device->mutex);
716 goto err_putprocess;
717 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700718 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 }
720 device->open_count++;
721 mutex_unlock(&device->mutex);
722
723 KGSL_DRV_INFO(device, "Initialized %s: mmu=%s pagetable_count=%d\n",
724 device->name, kgsl_mmu_enabled() ? "on" : "off",
725 kgsl_pagetable_count);
726
727 return result;
728
729err_putprocess:
730 kgsl_put_process_private(device, dev_priv->process_priv);
731err_freedevpriv:
732 filep->private_data = NULL;
733 kfree(dev_priv);
734err_pmruntime:
735 pm_runtime_put(device->parentdev);
736 return result;
737}
738
739
740/*call with private->mem_lock locked */
741static struct kgsl_mem_entry *
742kgsl_sharedmem_find(struct kgsl_process_private *private, unsigned int gpuaddr)
743{
744 struct kgsl_mem_entry *entry = NULL, *result = NULL;
745
746 BUG_ON(private == NULL);
747
748 gpuaddr &= PAGE_MASK;
749
750 list_for_each_entry(entry, &private->mem_list, list) {
751 if (entry->memdesc.gpuaddr == gpuaddr) {
752 result = entry;
753 break;
754 }
755 }
756 return result;
757}
758
759/*call with private->mem_lock locked */
760struct kgsl_mem_entry *
761kgsl_sharedmem_find_region(struct kgsl_process_private *private,
762 unsigned int gpuaddr,
763 size_t size)
764{
765 struct kgsl_mem_entry *entry = NULL, *result = NULL;
766
767 BUG_ON(private == NULL);
768
769 list_for_each_entry(entry, &private->mem_list, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700770 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700771 result = entry;
772 break;
773 }
774 }
775
776 return result;
777}
778EXPORT_SYMBOL(kgsl_sharedmem_find_region);
779
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780/*call all ioctl sub functions with driver locked*/
781static long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
782 unsigned int cmd, void *data)
783{
784 int result = 0;
785 struct kgsl_device_getproperty *param = data;
786
787 switch (param->type) {
788 case KGSL_PROP_VERSION:
789 {
790 struct kgsl_version version;
791 if (param->sizebytes != sizeof(version)) {
792 result = -EINVAL;
793 break;
794 }
795
796 version.drv_major = KGSL_VERSION_MAJOR;
797 version.drv_minor = KGSL_VERSION_MINOR;
798 version.dev_major = dev_priv->device->ver_major;
799 version.dev_minor = dev_priv->device->ver_minor;
800
801 if (copy_to_user(param->value, &version, sizeof(version)))
802 result = -EFAULT;
803
804 break;
805 }
806 default:
807 result = dev_priv->device->ftbl->getproperty(
808 dev_priv->device, param->type,
809 param->value, param->sizebytes);
810 }
811
812
813 return result;
814}
815
816static long kgsl_ioctl_device_waittimestamp(struct kgsl_device_private
817 *dev_priv, unsigned int cmd,
818 void *data)
819{
820 int result = 0;
821 struct kgsl_device_waittimestamp *param = data;
822
823 /* Set the active count so that suspend doesn't do the
824 wrong thing */
825
826 dev_priv->device->active_cnt++;
827
Norman Geed7402ff2011-10-28 08:51:11 -0600828 trace_kgsl_waittimestamp_entry(dev_priv->device, param);
829
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 result = dev_priv->device->ftbl->waittimestamp(dev_priv->device,
831 param->timestamp,
832 param->timeout);
833
Norman Geed7402ff2011-10-28 08:51:11 -0600834 trace_kgsl_waittimestamp_exit(dev_priv->device, result);
835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 /* Fire off any pending suspend operations that are in flight */
837
838 INIT_COMPLETION(dev_priv->device->suspend_gate);
839 dev_priv->device->active_cnt--;
840 complete(&dev_priv->device->suspend_gate);
841
842 return result;
843}
844static bool check_ibdesc(struct kgsl_device_private *dev_priv,
845 struct kgsl_ibdesc *ibdesc, unsigned int numibs,
846 bool parse)
847{
848 bool result = true;
849 unsigned int i;
850 for (i = 0; i < numibs; i++) {
851 struct kgsl_mem_entry *entry;
852 spin_lock(&dev_priv->process_priv->mem_lock);
853 entry = kgsl_sharedmem_find_region(dev_priv->process_priv,
854 ibdesc[i].gpuaddr, ibdesc[i].sizedwords * sizeof(uint));
855 spin_unlock(&dev_priv->process_priv->mem_lock);
856 if (entry == NULL) {
857 KGSL_DRV_ERR(dev_priv->device,
858 "invalid cmd buffer gpuaddr %08x " \
859 "sizedwords %d\n", ibdesc[i].gpuaddr,
860 ibdesc[i].sizedwords);
861 result = false;
862 break;
863 }
864
865 if (parse && !kgsl_cffdump_parse_ibs(dev_priv, &entry->memdesc,
866 ibdesc[i].gpuaddr, ibdesc[i].sizedwords, true)) {
867 KGSL_DRV_ERR(dev_priv->device,
868 "invalid cmd buffer gpuaddr %08x " \
869 "sizedwords %d numibs %d/%d\n",
870 ibdesc[i].gpuaddr,
871 ibdesc[i].sizedwords, i+1, numibs);
872 result = false;
873 break;
874 }
875 }
876 return result;
877}
878
879static long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
880 unsigned int cmd, void *data)
881{
882 int result = 0;
883 struct kgsl_ringbuffer_issueibcmds *param = data;
884 struct kgsl_ibdesc *ibdesc;
885 struct kgsl_context *context;
886
887#ifdef CONFIG_MSM_KGSL_DRM
888 kgsl_gpu_mem_flush(DRM_KGSL_GEM_CACHE_OP_TO_DEV);
889#endif
890
891 context = kgsl_find_context(dev_priv, param->drawctxt_id);
892 if (context == NULL) {
893 result = -EINVAL;
894 KGSL_DRV_ERR(dev_priv->device,
895 "invalid drawctxt drawctxt_id %d\n",
896 param->drawctxt_id);
897 goto done;
898 }
899
900 if (param->flags & KGSL_CONTEXT_SUBMIT_IB_LIST) {
901 KGSL_DRV_INFO(dev_priv->device,
902 "Using IB list mode for ib submission, numibs: %d\n",
903 param->numibs);
904 if (!param->numibs) {
905 KGSL_DRV_ERR(dev_priv->device,
906 "Invalid numibs as parameter: %d\n",
907 param->numibs);
908 result = -EINVAL;
909 goto done;
910 }
911
912 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc) * param->numibs,
913 GFP_KERNEL);
914 if (!ibdesc) {
915 KGSL_MEM_ERR(dev_priv->device,
916 "kzalloc(%d) failed\n",
917 sizeof(struct kgsl_ibdesc) * param->numibs);
918 result = -ENOMEM;
919 goto done;
920 }
921
922 if (copy_from_user(ibdesc, (void *)param->ibdesc_addr,
923 sizeof(struct kgsl_ibdesc) * param->numibs)) {
924 result = -EFAULT;
925 KGSL_DRV_ERR(dev_priv->device,
926 "copy_from_user failed\n");
927 goto free_ibdesc;
928 }
929 } else {
930 KGSL_DRV_INFO(dev_priv->device,
931 "Using single IB submission mode for ib submission\n");
932 /* If user space driver is still using the old mode of
933 * submitting single ib then we need to support that as well */
934 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc), GFP_KERNEL);
935 if (!ibdesc) {
936 KGSL_MEM_ERR(dev_priv->device,
937 "kzalloc(%d) failed\n",
938 sizeof(struct kgsl_ibdesc));
939 result = -ENOMEM;
940 goto done;
941 }
942 ibdesc[0].gpuaddr = param->ibdesc_addr;
943 ibdesc[0].sizedwords = param->numibs;
944 param->numibs = 1;
945 }
946
947 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, true)) {
948 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc");
949 result = -EINVAL;
950 goto free_ibdesc;
951 }
952
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 result = dev_priv->device->ftbl->issueibcmds(dev_priv,
954 context,
955 ibdesc,
956 param->numibs,
957 &param->timestamp,
958 param->flags);
959
Norman Geed7402ff2011-10-28 08:51:11 -0600960 trace_kgsl_issueibcmds(dev_priv->device, param, result);
961
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962 if (result != 0)
963 goto free_ibdesc;
964
965 /* this is a check to try to detect if a command buffer was freed
966 * during issueibcmds().
967 */
968 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, false)) {
969 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc AFTER issue");
970 result = -EINVAL;
971 goto free_ibdesc;
972 }
973
974free_ibdesc:
975 kfree(ibdesc);
976done:
977
978#ifdef CONFIG_MSM_KGSL_DRM
979 kgsl_gpu_mem_flush(DRM_KGSL_GEM_CACHE_OP_FROM_DEV);
980#endif
981
982 return result;
983}
984
985static long kgsl_ioctl_cmdstream_readtimestamp(struct kgsl_device_private
986 *dev_priv, unsigned int cmd,
987 void *data)
988{
989 struct kgsl_cmdstream_readtimestamp *param = data;
990
991 param->timestamp =
992 dev_priv->device->ftbl->readtimestamp(dev_priv->device,
993 param->type);
994
Norman Geed7402ff2011-10-28 08:51:11 -0600995 trace_kgsl_readtimestamp(dev_priv->device, param);
996
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 return 0;
998}
999
1000static long kgsl_ioctl_cmdstream_freememontimestamp(struct kgsl_device_private
1001 *dev_priv, unsigned int cmd,
1002 void *data)
1003{
1004 int result = 0;
1005 struct kgsl_cmdstream_freememontimestamp *param = data;
1006 struct kgsl_mem_entry *entry = NULL;
1007
1008 spin_lock(&dev_priv->process_priv->mem_lock);
1009 entry = kgsl_sharedmem_find(dev_priv->process_priv, param->gpuaddr);
1010 if (entry)
1011 list_del(&entry->list);
1012 spin_unlock(&dev_priv->process_priv->mem_lock);
1013
1014 if (entry) {
1015 kgsl_memqueue_freememontimestamp(dev_priv->device, entry,
1016 param->timestamp, param->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 } else {
1018 KGSL_DRV_ERR(dev_priv->device,
1019 "invalid gpuaddr %08x\n", param->gpuaddr);
1020 result = -EINVAL;
1021 }
1022
1023 return result;
1024}
1025
1026static long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
1027 unsigned int cmd, void *data)
1028{
1029 int result = 0;
1030 struct kgsl_drawctxt_create *param = data;
1031 struct kgsl_context *context = NULL;
1032
1033 context = kgsl_create_context(dev_priv);
1034
1035 if (context == NULL) {
1036 result = -ENOMEM;
1037 goto done;
1038 }
1039
1040 if (dev_priv->device->ftbl->drawctxt_create)
1041 result = dev_priv->device->ftbl->drawctxt_create(
1042 dev_priv->device, dev_priv->process_priv->pagetable,
1043 context, param->flags);
1044
1045 param->drawctxt_id = context->id;
1046
1047done:
1048 if (result && context)
1049 kgsl_destroy_context(dev_priv, context);
1050
1051 return result;
1052}
1053
1054static long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
1055 unsigned int cmd, void *data)
1056{
1057 int result = 0;
1058 struct kgsl_drawctxt_destroy *param = data;
1059 struct kgsl_context *context;
1060
1061 context = kgsl_find_context(dev_priv, param->drawctxt_id);
1062
1063 if (context == NULL) {
1064 result = -EINVAL;
1065 goto done;
1066 }
1067
1068 if (dev_priv->device->ftbl->drawctxt_destroy)
1069 dev_priv->device->ftbl->drawctxt_destroy(dev_priv->device,
1070 context);
1071
1072 kgsl_destroy_context(dev_priv, context);
1073
1074done:
1075 return result;
1076}
1077
1078static long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1079 unsigned int cmd, void *data)
1080{
1081 int result = 0;
1082 struct kgsl_sharedmem_free *param = data;
1083 struct kgsl_process_private *private = dev_priv->process_priv;
1084 struct kgsl_mem_entry *entry = NULL;
1085
1086 spin_lock(&private->mem_lock);
1087 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1088 if (entry)
1089 list_del(&entry->list);
1090 spin_unlock(&private->mem_lock);
1091
1092 if (entry) {
1093 kgsl_mem_entry_put(entry);
1094 } else {
1095 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1096 result = -EINVAL;
1097 }
1098
1099 return result;
1100}
1101
1102static struct vm_area_struct *kgsl_get_vma_from_start_addr(unsigned int addr)
1103{
1104 struct vm_area_struct *vma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105
1106 down_read(&current->mm->mmap_sem);
1107 vma = find_vma(current->mm, addr);
1108 up_read(&current->mm->mmap_sem);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001109 if (!vma)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 KGSL_CORE_ERR("find_vma(%x) failed\n", addr);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001112 return vma;
1113}
1114
1115static long
1116kgsl_ioctl_sharedmem_from_vmalloc(struct kgsl_device_private *dev_priv,
1117 unsigned int cmd, void *data)
1118{
1119 int result = 0, len = 0;
1120 struct kgsl_process_private *private = dev_priv->process_priv;
1121 struct kgsl_sharedmem_from_vmalloc *param = data;
1122 struct kgsl_mem_entry *entry = NULL;
1123 struct vm_area_struct *vma;
1124
1125 if (!kgsl_mmu_enabled())
1126 return -ENODEV;
1127
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128 if (!param->hostptr) {
1129 KGSL_CORE_ERR("invalid hostptr %x\n", param->hostptr);
1130 result = -EINVAL;
1131 goto error;
1132 }
1133
1134 vma = kgsl_get_vma_from_start_addr(param->hostptr);
1135 if (!vma) {
1136 result = -EINVAL;
1137 goto error;
1138 }
Jordan Crouse2c542b62011-07-26 08:30:20 -06001139
1140 /*
1141 * If the user specified a length, use it, otherwise try to
1142 * infer the length if the vma region
1143 */
1144 if (param->gpuaddr != 0) {
1145 len = param->gpuaddr;
1146 } else {
1147 /*
1148 * For this to work, we have to assume the VMA region is only
1149 * for this single allocation. If it isn't, then bail out
1150 */
1151 if (vma->vm_pgoff || (param->hostptr != vma->vm_start)) {
1152 KGSL_CORE_ERR("VMA region does not match hostaddr\n");
1153 result = -EINVAL;
1154 goto error;
1155 }
1156
1157 len = vma->vm_end - vma->vm_start;
1158 }
1159
1160 /* Make sure it fits */
1161 if (len == 0 || param->hostptr + len > vma->vm_end) {
1162 KGSL_CORE_ERR("Invalid memory allocation length %d\n", len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 result = -EINVAL;
1164 goto error;
1165 }
1166
1167 entry = kgsl_mem_entry_create();
1168 if (entry == NULL) {
1169 result = -ENOMEM;
1170 goto error;
1171 }
1172
1173 result = kgsl_sharedmem_vmalloc_user(&entry->memdesc,
1174 private->pagetable, len,
1175 param->flags);
1176 if (result != 0)
1177 goto error_free_entry;
1178
1179 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1180
1181 result = remap_vmalloc_range(vma, (void *) entry->memdesc.hostptr, 0);
1182 if (result) {
1183 KGSL_CORE_ERR("remap_vmalloc_range failed: %d\n", result);
1184 goto error_free_vmalloc;
1185 }
1186
1187 param->gpuaddr = entry->memdesc.gpuaddr;
1188
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001189 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190
1191 kgsl_mem_entry_attach_process(entry, private);
1192
1193 /* Process specific statistics */
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001194 kgsl_process_add_stats(private, entry->memtype, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195
1196 kgsl_check_idle(dev_priv->device);
1197 return 0;
1198
1199error_free_vmalloc:
1200 kgsl_sharedmem_free(&entry->memdesc);
1201
1202error_free_entry:
1203 kfree(entry);
1204
1205error:
1206 kgsl_check_idle(dev_priv->device);
1207 return result;
1208}
1209
1210static inline int _check_region(unsigned long start, unsigned long size,
1211 uint64_t len)
1212{
1213 uint64_t end = ((uint64_t) start) + size;
1214 return (end > len);
1215}
1216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217static int kgsl_get_phys_file(int fd, unsigned long *start, unsigned long *len,
1218 unsigned long *vstart, struct file **filep)
1219{
1220 struct file *fbfile;
1221 int ret = 0;
1222 dev_t rdev;
1223 struct fb_info *info;
1224
1225 *filep = NULL;
Jordan Crousefd978432011-09-02 14:34:32 -06001226#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 if (!get_pmem_file(fd, start, vstart, len, filep))
1228 return 0;
Jordan Crousefd978432011-09-02 14:34:32 -06001229#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230
1231 fbfile = fget(fd);
1232 if (fbfile == NULL) {
1233 KGSL_CORE_ERR("fget_light failed\n");
1234 return -1;
1235 }
1236
1237 rdev = fbfile->f_dentry->d_inode->i_rdev;
1238 info = MAJOR(rdev) == FB_MAJOR ? registered_fb[MINOR(rdev)] : NULL;
1239 if (info) {
1240 *start = info->fix.smem_start;
1241 *len = info->fix.smem_len;
1242 *vstart = (unsigned long)__va(info->fix.smem_start);
1243 ret = 0;
1244 } else {
1245 KGSL_CORE_ERR("framebuffer minor %d not found\n",
1246 MINOR(rdev));
1247 ret = -1;
1248 }
1249
1250 fput(fbfile);
1251
1252 return ret;
1253}
1254
1255static int kgsl_setup_phys_file(struct kgsl_mem_entry *entry,
1256 struct kgsl_pagetable *pagetable,
1257 unsigned int fd, unsigned int offset,
1258 size_t size)
1259{
1260 int ret;
1261 unsigned long phys, virt, len;
1262 struct file *filep;
1263
1264 ret = kgsl_get_phys_file(fd, &phys, &len, &virt, &filep);
1265 if (ret)
1266 return ret;
1267
Wei Zou4061c0b2011-07-08 10:24:22 -07001268 if (phys == 0) {
1269 ret = -EINVAL;
1270 goto err;
1271 }
1272
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273 if (offset >= len) {
1274 ret = -EINVAL;
1275 goto err;
1276 }
1277
1278 if (size == 0)
1279 size = len;
1280
1281 /* Adjust the size of the region to account for the offset */
1282 size += offset & ~PAGE_MASK;
1283
1284 size = ALIGN(size, PAGE_SIZE);
1285
1286 if (_check_region(offset & PAGE_MASK, size, len)) {
1287 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1288 "than pmem region length %ld\n",
1289 offset & PAGE_MASK, size, len);
1290 ret = -EINVAL;
1291 goto err;
1292
1293 }
1294
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001295 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001296
1297 entry->memdesc.pagetable = pagetable;
1298 entry->memdesc.size = size;
1299 entry->memdesc.physaddr = phys + (offset & PAGE_MASK);
1300 entry->memdesc.hostptr = (void *) (virt + (offset & PAGE_MASK));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001301
1302 ret = memdesc_sg_phys(&entry->memdesc,
1303 phys + (offset & PAGE_MASK), size);
1304 if (ret)
1305 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306
1307 return 0;
1308err:
Jordan Crousefd978432011-09-02 14:34:32 -06001309#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 put_pmem_file(filep);
Jordan Crousefd978432011-09-02 14:34:32 -06001311#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001312 return ret;
1313}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001314
Jordan Croused17e9aa2011-10-12 16:57:48 -06001315static int memdesc_sg_virt(struct kgsl_memdesc *memdesc,
1316 void *addr, int size)
1317{
1318 int i;
1319 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
1320 unsigned long paddr = (unsigned long) addr;
1321
Jeff Boody28afec42012-01-18 15:47:46 -07001322 memdesc->sg = vmalloc(sglen * sizeof(struct scatterlist));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001323 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);
Jeff Boody28afec42012-01-18 15:47:46 -07001362 vfree(memdesc->sg);
Jordan Croused17e9aa2011-10-12 16:57:48 -06001363 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
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002101 kgsl_device_snapshot_close(device);
2102
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002103 kgsl_cffdump_close(device->id);
2104 kgsl_pwrctrl_uninit_sysfs(device);
2105
Lucille Sylvesteref44e7332011-11-02 13:21:17 -07002106 if (cpu_is_msm8x60())
2107 wake_lock_destroy(&device->idle_wakelock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002108
2109 idr_destroy(&device->context_idr);
2110
2111 if (device->memstore.hostptr)
2112 kgsl_sharedmem_free(&device->memstore);
2113
2114 kgsl_mmu_close(device);
2115
2116 if (device->work_queue) {
2117 destroy_workqueue(device->work_queue);
2118 device->work_queue = NULL;
2119 }
2120
2121 device_destroy(kgsl_driver.class,
2122 MKDEV(MAJOR(kgsl_driver.major), minor));
2123
2124 mutex_lock(&kgsl_driver.devlock);
2125 kgsl_driver.devp[minor] = NULL;
2126 mutex_unlock(&kgsl_driver.devlock);
2127}
2128EXPORT_SYMBOL(kgsl_unregister_device);
2129
2130int
2131kgsl_register_device(struct kgsl_device *device)
2132{
2133 int minor, ret;
2134 dev_t dev;
2135
2136 /* Find a minor for the device */
2137
2138 mutex_lock(&kgsl_driver.devlock);
2139 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2140 if (kgsl_driver.devp[minor] == NULL) {
2141 kgsl_driver.devp[minor] = device;
2142 break;
2143 }
2144 }
2145
2146 mutex_unlock(&kgsl_driver.devlock);
2147
2148 if (minor == KGSL_DEVICE_MAX) {
2149 KGSL_CORE_ERR("minor devices exhausted\n");
2150 return -ENODEV;
2151 }
2152
2153 /* Create the device */
2154 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
2155 device->dev = device_create(kgsl_driver.class,
2156 device->parentdev,
2157 dev, device,
2158 device->name);
2159
2160 if (IS_ERR(device->dev)) {
2161 ret = PTR_ERR(device->dev);
2162 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
2163 goto err_devlist;
2164 }
2165
2166 dev_set_drvdata(device->parentdev, device);
2167
2168 /* Generic device initialization */
2169 init_waitqueue_head(&device->wait_queue);
2170
2171 kgsl_cffdump_open(device->id);
2172
2173 init_completion(&device->hwaccess_gate);
2174 init_completion(&device->suspend_gate);
2175
2176 ATOMIC_INIT_NOTIFIER_HEAD(&device->ts_notifier_list);
2177
2178 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
2179 ret = kgsl_create_device_workqueue(device);
2180 if (ret)
2181 goto err_devlist;
2182
2183 INIT_WORK(&device->idle_check_ws, kgsl_idle_check);
Jordan Crouse1bf80aa2011-10-12 16:57:47 -06002184 INIT_WORK(&device->ts_expired_ws, kgsl_timestamp_expired);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185
2186 INIT_LIST_HEAD(&device->memqueue);
Jordan Croused4bc9d22011-11-17 13:39:21 -07002187 INIT_LIST_HEAD(&device->events);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188
2189 ret = kgsl_mmu_init(device);
2190 if (ret != 0)
2191 goto err_dest_work_q;
2192
2193 ret = kgsl_allocate_contiguous(&device->memstore,
2194 sizeof(struct kgsl_devmemstore));
2195
2196 if (ret != 0)
2197 goto err_close_mmu;
2198
Lucille Sylvesteref44e7332011-11-02 13:21:17 -07002199 if (cpu_is_msm8x60())
2200 wake_lock_init(&device->idle_wakelock,
2201 WAKE_LOCK_IDLE, device->name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002202
2203 idr_init(&device->context_idr);
2204
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002205 /* Initalize the snapshot engine */
2206 kgsl_device_snapshot_init(device);
2207
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208 /* sysfs and debugfs initalization - failure here is non fatal */
2209
2210 /* Initialize logging */
2211 kgsl_device_debugfs_init(device);
2212
2213 /* Initialize common sysfs entries */
2214 kgsl_pwrctrl_init_sysfs(device);
2215
2216 return 0;
2217
2218err_close_mmu:
2219 kgsl_mmu_close(device);
2220err_dest_work_q:
2221 destroy_workqueue(device->work_queue);
2222 device->work_queue = NULL;
2223err_devlist:
2224 mutex_lock(&kgsl_driver.devlock);
2225 kgsl_driver.devp[minor] = NULL;
2226 mutex_unlock(&kgsl_driver.devlock);
2227
2228 return ret;
2229}
2230EXPORT_SYMBOL(kgsl_register_device);
2231
2232int kgsl_device_platform_probe(struct kgsl_device *device,
2233 irqreturn_t (*dev_isr) (int, void*))
2234{
Michael Street8bacdd02012-01-05 14:55:01 -08002235 int result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002236 int status = -EINVAL;
2237 struct kgsl_memregion *regspace = NULL;
2238 struct resource *res;
2239 struct platform_device *pdev =
2240 container_of(device->parentdev, struct platform_device, dev);
2241
2242 pm_runtime_enable(device->parentdev);
2243
2244 status = kgsl_pwrctrl_init(device);
2245 if (status)
2246 goto error;
2247
2248 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2249 device->iomemname);
2250 if (res == NULL) {
2251 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
2252 status = -EINVAL;
2253 goto error_pwrctrl_close;
2254 }
2255 if (res->start == 0 || resource_size(res) == 0) {
2256 KGSL_DRV_ERR(device, "dev %d invalid regspace\n", device->id);
2257 status = -EINVAL;
2258 goto error_pwrctrl_close;
2259 }
2260
2261 regspace = &device->regspace;
2262 regspace->mmio_phys_base = res->start;
2263 regspace->sizebytes = resource_size(res);
2264
2265 if (!request_mem_region(regspace->mmio_phys_base,
2266 regspace->sizebytes, device->name)) {
2267 KGSL_DRV_ERR(device, "request_mem_region failed\n");
2268 status = -ENODEV;
2269 goto error_pwrctrl_close;
2270 }
2271
2272 regspace->mmio_virt_base = ioremap(regspace->mmio_phys_base,
2273 regspace->sizebytes);
2274
2275 if (regspace->mmio_virt_base == NULL) {
2276 KGSL_DRV_ERR(device, "ioremap failed\n");
2277 status = -ENODEV;
2278 goto error_release_mem;
2279 }
2280
2281 status = request_irq(device->pwrctrl.interrupt_num, dev_isr,
2282 IRQF_TRIGGER_HIGH, device->name, device);
2283 if (status) {
2284 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
2285 device->pwrctrl.interrupt_num, status);
2286 goto error_iounmap;
2287 }
2288 device->pwrctrl.have_irq = 1;
2289 disable_irq(device->pwrctrl.interrupt_num);
2290
2291 KGSL_DRV_INFO(device,
2292 "dev_id %d regs phys 0x%08x size 0x%08x virt %p\n",
2293 device->id, regspace->mmio_phys_base,
2294 regspace->sizebytes, regspace->mmio_virt_base);
2295
Michael Street8bacdd02012-01-05 14:55:01 -08002296 result = kgsl_drm_init(pdev);
2297 if (result)
2298 goto error_iounmap;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002299
2300 status = kgsl_register_device(device);
2301 if (!status)
2302 return status;
2303
2304 free_irq(device->pwrctrl.interrupt_num, NULL);
2305 device->pwrctrl.have_irq = 0;
2306error_iounmap:
2307 iounmap(regspace->mmio_virt_base);
2308 regspace->mmio_virt_base = NULL;
2309error_release_mem:
2310 release_mem_region(regspace->mmio_phys_base, regspace->sizebytes);
2311error_pwrctrl_close:
2312 kgsl_pwrctrl_close(device);
2313error:
2314 return status;
2315}
2316EXPORT_SYMBOL(kgsl_device_platform_probe);
2317
2318void kgsl_device_platform_remove(struct kgsl_device *device)
2319{
2320 struct kgsl_memregion *regspace = &device->regspace;
2321
2322 kgsl_unregister_device(device);
2323
2324 if (regspace->mmio_virt_base != NULL) {
2325 iounmap(regspace->mmio_virt_base);
2326 regspace->mmio_virt_base = NULL;
2327 release_mem_region(regspace->mmio_phys_base,
2328 regspace->sizebytes);
2329 }
2330 kgsl_pwrctrl_close(device);
2331
2332 pm_runtime_disable(device->parentdev);
2333}
2334EXPORT_SYMBOL(kgsl_device_platform_remove);
2335
2336static int __devinit
2337kgsl_ptdata_init(void)
2338{
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002339 kgsl_driver.ptpool = kgsl_mmu_ptpool_init(KGSL_PAGETABLE_SIZE,
2340 kgsl_pagetable_count);
2341 if (!kgsl_driver.ptpool)
2342 return -ENOMEM;
2343 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002344}
2345
2346static void kgsl_core_exit(void)
2347{
2348 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
2349
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002350 kgsl_mmu_ptpool_destroy(&kgsl_driver.ptpool);
2351 kgsl_driver.ptpool = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352
2353 device_unregister(&kgsl_driver.virtdev);
2354
2355 if (kgsl_driver.class) {
2356 class_destroy(kgsl_driver.class);
2357 kgsl_driver.class = NULL;
2358 }
2359
2360 kgsl_drm_exit();
2361 kgsl_cffdump_destroy();
Jordan Croused8f1c6b2011-10-04 09:31:29 -06002362 kgsl_core_debugfs_close();
2363 kgsl_sharedmem_uninit_sysfs();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364}
2365
2366static int __init kgsl_core_init(void)
2367{
2368 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002369 /* alloc major and minor device numbers */
2370 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
2371 KGSL_NAME);
2372 if (result < 0) {
2373 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
2374 goto err;
2375 }
2376
2377 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
2378 kgsl_driver.cdev.owner = THIS_MODULE;
2379 kgsl_driver.cdev.ops = &kgsl_fops;
2380 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
2381 KGSL_DEVICE_MAX);
2382
2383 if (result) {
2384 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d,"
2385 " result= %d\n", kgsl_driver.major, result);
2386 goto err;
2387 }
2388
2389 kgsl_driver.class = class_create(THIS_MODULE, KGSL_NAME);
2390
2391 if (IS_ERR(kgsl_driver.class)) {
2392 result = PTR_ERR(kgsl_driver.class);
2393 KGSL_CORE_ERR("failed to create class %s", KGSL_NAME);
2394 goto err;
2395 }
2396
2397 /* Make a virtual device for managing core related things
2398 in sysfs */
2399 kgsl_driver.virtdev.class = kgsl_driver.class;
2400 dev_set_name(&kgsl_driver.virtdev, "kgsl");
2401 result = device_register(&kgsl_driver.virtdev);
2402 if (result) {
2403 KGSL_CORE_ERR("driver_register failed\n");
2404 goto err;
2405 }
2406
2407 /* Make kobjects in the virtual device for storing statistics */
2408
2409 kgsl_driver.ptkobj =
2410 kobject_create_and_add("pagetables",
2411 &kgsl_driver.virtdev.kobj);
2412
2413 kgsl_driver.prockobj =
2414 kobject_create_and_add("proc",
2415 &kgsl_driver.virtdev.kobj);
2416
2417 kgsl_core_debugfs_init();
2418
2419 kgsl_sharedmem_init_sysfs();
2420 kgsl_cffdump_init();
2421
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 INIT_LIST_HEAD(&kgsl_driver.process_list);
2423
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002424 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
2425
2426 kgsl_mmu_set_mmutype(ksgl_mmu_type);
2427
2428 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype()) {
2429 result = kgsl_ptdata_init();
2430 if (result)
2431 goto err;
2432 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002433
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002434 return 0;
2435
2436err:
2437 kgsl_core_exit();
2438 return result;
2439}
2440
2441module_init(kgsl_core_init);
2442module_exit(kgsl_core_exit);
2443
2444MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
2445MODULE_DESCRIPTION("MSM GPU driver");
2446MODULE_LICENSE("GPL");