blob: e84d47342ec200943efa3c3f47239fe764e96252 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2008-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#include <linux/fb.h>
14#include <linux/file.h>
15#include <linux/fs.h>
16#include <linux/debugfs.h>
17#include <linux/uaccess.h>
18#include <linux/interrupt.h>
19#include <linux/workqueue.h>
20#include <linux/android_pmem.h>
21#include <linux/vmalloc.h>
22#include <linux/pm_runtime.h>
23
24#include <linux/ashmem.h>
25#include <linux/major.h>
Jordan Crouse8eab35a2011-10-12 16:57:48 -060026#include <linux/ion.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027
28#include "kgsl.h"
29#include "kgsl_debugfs.h"
30#include "kgsl_cffdump.h"
31#include "kgsl_log.h"
32#include "kgsl_sharedmem.h"
33#include "kgsl_device.h"
34
35#undef MODULE_PARAM_PREFIX
36#define MODULE_PARAM_PREFIX "kgsl."
37
38static int kgsl_pagetable_count = KGSL_PAGETABLE_COUNT;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060039static char *ksgl_mmu_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040module_param_named(ptcount, kgsl_pagetable_count, int, 0);
41MODULE_PARM_DESC(kgsl_pagetable_count,
42"Minimum number of pagetables for KGSL to allocate at initialization time");
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060043module_param_named(mmutype, ksgl_mmu_type, charp, 0);
44MODULE_PARM_DESC(ksgl_mmu_type,
45"Type of MMU to be used for graphics. Valid values are 'iommu' or 'gpummu' or 'nommu'");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046
Jordan Crouse8eab35a2011-10-12 16:57:48 -060047static struct ion_client *kgsl_ion_client;
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049static inline struct kgsl_mem_entry *
50kgsl_mem_entry_create(void)
51{
52 struct kgsl_mem_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
53
54 if (!entry)
55 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*entry));
56 else
57 kref_init(&entry->refcount);
58
59 return entry;
60}
61
62void
63kgsl_mem_entry_destroy(struct kref *kref)
64{
65 struct kgsl_mem_entry *entry = container_of(kref,
66 struct kgsl_mem_entry,
67 refcount);
Jordan Crouse1b897cf2011-10-12 16:57:48 -060068
69 entry->priv->stats[entry->memtype].cur -= entry->memdesc.size;
70
71 if (entry->memtype != KGSL_MEM_ENTRY_KERNEL)
72 kgsl_driver.stats.mapped -= entry->memdesc.size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070073
Jordan Crouse8eab35a2011-10-12 16:57:48 -060074 /*
75 * Ion takes care of freeing the sglist for us (how nice </sarcasm>) so
76 * unmap the dma before freeing the sharedmem so kgsl_sharedmem_free
77 * doesn't try to free it again
78 */
79
80 if (entry->memtype == KGSL_MEM_ENTRY_ION) {
81 ion_unmap_dma(kgsl_ion_client, entry->priv_data);
82 entry->memdesc.sg = NULL;
83 }
84
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085 kgsl_sharedmem_free(&entry->memdesc);
86
Jordan Crouse1b897cf2011-10-12 16:57:48 -060087 switch (entry->memtype) {
88 case KGSL_MEM_ENTRY_PMEM:
89 case KGSL_MEM_ENTRY_ASHMEM:
90 if (entry->priv_data)
91 fput(entry->priv_data);
92 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -060093 case KGSL_MEM_ENTRY_ION:
94 ion_free(kgsl_ion_client, entry->priv_data);
95 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096 }
97
98 kfree(entry);
99}
100EXPORT_SYMBOL(kgsl_mem_entry_destroy);
101
102static
103void kgsl_mem_entry_attach_process(struct kgsl_mem_entry *entry,
104 struct kgsl_process_private *process)
105{
106 spin_lock(&process->mem_lock);
107 list_add(&entry->list, &process->mem_list);
108 spin_unlock(&process->mem_lock);
109
110 entry->priv = process;
111}
112
113/* Allocate a new context id */
114
115static struct kgsl_context *
116kgsl_create_context(struct kgsl_device_private *dev_priv)
117{
118 struct kgsl_context *context;
119 int ret, id;
120
121 context = kzalloc(sizeof(*context), GFP_KERNEL);
122
123 if (context == NULL)
124 return NULL;
125
126 while (1) {
127 if (idr_pre_get(&dev_priv->device->context_idr,
128 GFP_KERNEL) == 0) {
129 kfree(context);
130 return NULL;
131 }
132
133 ret = idr_get_new(&dev_priv->device->context_idr,
134 context, &id);
135
136 if (ret != -EAGAIN)
137 break;
138 }
139
140 if (ret) {
141 kfree(context);
142 return NULL;
143 }
144
145 context->id = id;
146 context->dev_priv = dev_priv;
147
148 return context;
149}
150
151static void
152kgsl_destroy_context(struct kgsl_device_private *dev_priv,
153 struct kgsl_context *context)
154{
155 int id;
156
157 if (context == NULL)
158 return;
159
160 /* Fire a bug if the devctxt hasn't been freed */
161 BUG_ON(context->devctxt);
162
163 id = context->id;
164 kfree(context);
165
166 idr_remove(&dev_priv->device->context_idr, id);
167}
168
169/* to be called when a process is destroyed, this walks the memqueue and
170 * frees any entryies that belong to the dying process
171 */
172static void kgsl_memqueue_cleanup(struct kgsl_device *device,
173 struct kgsl_process_private *private)
174{
175 struct kgsl_mem_entry *entry, *entry_tmp;
176
177 if (!private)
178 return;
179
180 BUG_ON(!mutex_is_locked(&device->mutex));
181
182 list_for_each_entry_safe(entry, entry_tmp, &device->memqueue, list) {
183 if (entry->priv == private) {
184 list_del(&entry->list);
185 kgsl_mem_entry_put(entry);
186 }
187 }
188}
189
190static void kgsl_memqueue_freememontimestamp(struct kgsl_device *device,
191 struct kgsl_mem_entry *entry,
192 uint32_t timestamp,
193 enum kgsl_timestamp_type type)
194{
195 BUG_ON(!mutex_is_locked(&device->mutex));
196
197 entry->free_timestamp = timestamp;
198
199 list_add_tail(&entry->list, &device->memqueue);
200}
201
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600202static void kgsl_timestamp_expired(struct work_struct *work)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203{
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600204 struct kgsl_device *device = container_of(work, struct kgsl_device,
205 ts_expired_ws);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 struct kgsl_mem_entry *entry, *entry_tmp;
207 uint32_t ts_processed;
208
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600209 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210
211 /* get current EOP timestamp */
212 ts_processed = device->ftbl->readtimestamp(device,
213 KGSL_TIMESTAMP_RETIRED);
214
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600215 /* Flush the freememontimestamp queue */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 list_for_each_entry_safe(entry, entry_tmp, &device->memqueue, list) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 if (!timestamp_cmp(ts_processed, entry->free_timestamp))
218 break;
219
220 list_del(&entry->list);
221 kgsl_mem_entry_put(entry);
222 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 mutex_unlock(&device->mutex);
225}
226
227static void kgsl_check_idle_locked(struct kgsl_device *device)
228{
229 if (device->pwrctrl.nap_allowed == true &&
230 device->state == KGSL_STATE_ACTIVE &&
231 device->requested_state == KGSL_STATE_NONE) {
232 device->requested_state = KGSL_STATE_NAP;
233 if (kgsl_pwrctrl_sleep(device) != 0)
234 mod_timer(&device->idle_timer,
235 jiffies +
236 device->pwrctrl.interval_timeout);
237 }
238}
239
240static void kgsl_check_idle(struct kgsl_device *device)
241{
242 mutex_lock(&device->mutex);
243 kgsl_check_idle_locked(device);
244 mutex_unlock(&device->mutex);
245}
246
247struct kgsl_device *kgsl_get_device(int dev_idx)
248{
249 int i;
250 struct kgsl_device *ret = NULL;
251
252 mutex_lock(&kgsl_driver.devlock);
253
254 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
255 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) {
256 ret = kgsl_driver.devp[i];
257 break;
258 }
259 }
260
261 mutex_unlock(&kgsl_driver.devlock);
262 return ret;
263}
264EXPORT_SYMBOL(kgsl_get_device);
265
266static struct kgsl_device *kgsl_get_minor(int minor)
267{
268 struct kgsl_device *ret = NULL;
269
270 if (minor < 0 || minor >= KGSL_DEVICE_MAX)
271 return NULL;
272
273 mutex_lock(&kgsl_driver.devlock);
274 ret = kgsl_driver.devp[minor];
275 mutex_unlock(&kgsl_driver.devlock);
276
277 return ret;
278}
279
280int kgsl_register_ts_notifier(struct kgsl_device *device,
281 struct notifier_block *nb)
282{
283 BUG_ON(device == NULL);
284 return atomic_notifier_chain_register(&device->ts_notifier_list,
285 nb);
286}
287EXPORT_SYMBOL(kgsl_register_ts_notifier);
288
289int kgsl_unregister_ts_notifier(struct kgsl_device *device,
290 struct notifier_block *nb)
291{
292 BUG_ON(device == NULL);
293 return atomic_notifier_chain_unregister(&device->ts_notifier_list,
294 nb);
295}
296EXPORT_SYMBOL(kgsl_unregister_ts_notifier);
297
298int kgsl_check_timestamp(struct kgsl_device *device, unsigned int timestamp)
299{
300 unsigned int ts_processed;
301
302 ts_processed = device->ftbl->readtimestamp(device,
303 KGSL_TIMESTAMP_RETIRED);
304
305 return timestamp_cmp(ts_processed, timestamp);
306}
307EXPORT_SYMBOL(kgsl_check_timestamp);
308
309static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state)
310{
311 int status = -EINVAL;
312 unsigned int nap_allowed_saved;
313 struct kgsl_pwrscale_policy *policy_saved;
314
315 if (!device)
316 return -EINVAL;
317
318 KGSL_PWR_WARN(device, "suspend start\n");
319
320 mutex_lock(&device->mutex);
321 nap_allowed_saved = device->pwrctrl.nap_allowed;
322 device->pwrctrl.nap_allowed = false;
323 policy_saved = device->pwrscale.policy;
324 device->pwrscale.policy = NULL;
325 device->requested_state = KGSL_STATE_SUSPEND;
326 /* Make sure no user process is waiting for a timestamp *
327 * before supending */
328 if (device->active_cnt != 0) {
329 mutex_unlock(&device->mutex);
330 wait_for_completion(&device->suspend_gate);
331 mutex_lock(&device->mutex);
332 }
333 /* Don't let the timer wake us during suspended sleep. */
Jeremy Gebben1757a852011-07-11 16:04:38 -0600334 del_timer_sync(&device->idle_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700335 switch (device->state) {
336 case KGSL_STATE_INIT:
337 break;
338 case KGSL_STATE_ACTIVE:
339 /* Wait for the device to become idle */
340 device->ftbl->idle(device, KGSL_TIMEOUT_DEFAULT);
341 case KGSL_STATE_NAP:
342 case KGSL_STATE_SLEEP:
343 /* Get the completion ready to be waited upon. */
344 INIT_COMPLETION(device->hwaccess_gate);
345 device->ftbl->suspend_context(device);
346 device->ftbl->stop(device);
347 device->state = KGSL_STATE_SUSPEND;
348 KGSL_PWR_WARN(device, "state -> SUSPEND, device %d\n",
349 device->id);
350 break;
Suman Tatiraju24569022011-10-27 11:11:12 -0700351 case KGSL_STATE_SLUMBER:
352 INIT_COMPLETION(device->hwaccess_gate);
353 device->state = KGSL_STATE_SUSPEND;
354 KGSL_PWR_WARN(device, "state -> SUSPEND, device %d\n",
355 device->id);
356 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 default:
358 KGSL_PWR_ERR(device, "suspend fail, device %d\n",
359 device->id);
360 goto end;
361 }
362 device->requested_state = KGSL_STATE_NONE;
363 device->pwrctrl.nap_allowed = nap_allowed_saved;
364 device->pwrscale.policy = policy_saved;
365 status = 0;
366
367end:
368 mutex_unlock(&device->mutex);
369 KGSL_PWR_WARN(device, "suspend end\n");
370 return status;
371}
372
373static int kgsl_resume_device(struct kgsl_device *device)
374{
375 int status = -EINVAL;
376
377 if (!device)
378 return -EINVAL;
379
380 KGSL_PWR_WARN(device, "resume start\n");
381 mutex_lock(&device->mutex);
382 if (device->state == KGSL_STATE_SUSPEND) {
Suman Tatiraju24569022011-10-27 11:11:12 -0700383 device->state = KGSL_STATE_SLUMBER;
384 status = 0;
385 KGSL_PWR_WARN(device,
386 "state -> SLUMBER, device %d\n",
387 device->id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 complete_all(&device->hwaccess_gate);
389 }
390 device->requested_state = KGSL_STATE_NONE;
391
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 KGSL_PWR_WARN(device, "resume end\n");
394 return status;
395}
396
397static int kgsl_suspend(struct device *dev)
398{
399
400 pm_message_t arg = {0};
401 struct kgsl_device *device = dev_get_drvdata(dev);
402 return kgsl_suspend_device(device, arg);
403}
404
405static int kgsl_resume(struct device *dev)
406{
407 struct kgsl_device *device = dev_get_drvdata(dev);
408 return kgsl_resume_device(device);
409}
410
411static int kgsl_runtime_suspend(struct device *dev)
412{
413 return 0;
414}
415
416static int kgsl_runtime_resume(struct device *dev)
417{
418 return 0;
419}
420
421const struct dev_pm_ops kgsl_pm_ops = {
422 .suspend = kgsl_suspend,
423 .resume = kgsl_resume,
424 .runtime_suspend = kgsl_runtime_suspend,
425 .runtime_resume = kgsl_runtime_resume,
426};
427EXPORT_SYMBOL(kgsl_pm_ops);
428
429void kgsl_early_suspend_driver(struct early_suspend *h)
430{
431 struct kgsl_device *device = container_of(h,
432 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700433 KGSL_PWR_WARN(device, "early suspend start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530434 mutex_lock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700435 device->requested_state = KGSL_STATE_SLUMBER;
436 kgsl_pwrctrl_sleep(device);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530437 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700438 KGSL_PWR_WARN(device, "early suspend end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439}
440EXPORT_SYMBOL(kgsl_early_suspend_driver);
441
442int kgsl_suspend_driver(struct platform_device *pdev,
443 pm_message_t state)
444{
445 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
446 return kgsl_suspend_device(device, state);
447}
448EXPORT_SYMBOL(kgsl_suspend_driver);
449
450int kgsl_resume_driver(struct platform_device *pdev)
451{
452 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
453 return kgsl_resume_device(device);
454}
455EXPORT_SYMBOL(kgsl_resume_driver);
456
457void kgsl_late_resume_driver(struct early_suspend *h)
458{
459 struct kgsl_device *device = container_of(h,
460 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700461 KGSL_PWR_WARN(device, "late resume start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530462 mutex_lock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700463 kgsl_pwrctrl_wake(device);
464 device->pwrctrl.restore_slumber = 0;
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530465 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700466 kgsl_check_idle(device);
467 KGSL_PWR_WARN(device, "late resume end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468}
469EXPORT_SYMBOL(kgsl_late_resume_driver);
470
471/* file operations */
472static struct kgsl_process_private *
473kgsl_get_process_private(struct kgsl_device_private *cur_dev_priv)
474{
475 struct kgsl_process_private *private;
476
477 mutex_lock(&kgsl_driver.process_mutex);
478 list_for_each_entry(private, &kgsl_driver.process_list, list) {
479 if (private->pid == task_tgid_nr(current)) {
480 private->refcnt++;
481 goto out;
482 }
483 }
484
485 /* no existing process private found for this dev_priv, create one */
486 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
487 if (private == NULL) {
488 KGSL_DRV_ERR(cur_dev_priv->device, "kzalloc(%d) failed\n",
489 sizeof(struct kgsl_process_private));
490 goto out;
491 }
492
493 spin_lock_init(&private->mem_lock);
494 private->refcnt = 1;
495 private->pid = task_tgid_nr(current);
496
497 INIT_LIST_HEAD(&private->mem_list);
498
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600499 if (kgsl_mmu_enabled())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 {
501 unsigned long pt_name;
502
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 pt_name = task_tgid_nr(current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504 private->pagetable = kgsl_mmu_getpagetable(pt_name);
505 if (private->pagetable == NULL) {
506 kfree(private);
507 private = NULL;
508 goto out;
509 }
510 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700511
512 list_add(&private->list, &kgsl_driver.process_list);
513
514 kgsl_process_init_sysfs(private);
515
516out:
517 mutex_unlock(&kgsl_driver.process_mutex);
518 return private;
519}
520
521static void
522kgsl_put_process_private(struct kgsl_device *device,
523 struct kgsl_process_private *private)
524{
525 struct kgsl_mem_entry *entry = NULL;
526 struct kgsl_mem_entry *entry_tmp = NULL;
527
528 if (!private)
529 return;
530
531 mutex_lock(&kgsl_driver.process_mutex);
532
533 if (--private->refcnt)
534 goto unlock;
535
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 kgsl_process_uninit_sysfs(private);
537
538 list_del(&private->list);
539
540 list_for_each_entry_safe(entry, entry_tmp, &private->mem_list, list) {
541 list_del(&entry->list);
542 kgsl_mem_entry_put(entry);
543 }
544
545 kgsl_mmu_putpagetable(private->pagetable);
546 kfree(private);
547unlock:
548 mutex_unlock(&kgsl_driver.process_mutex);
549}
550
551static int kgsl_release(struct inode *inodep, struct file *filep)
552{
553 int result = 0;
Jordan Crouse2db0af92011-08-08 16:05:09 -0600554 struct kgsl_device_private *dev_priv = filep->private_data;
555 struct kgsl_process_private *private = dev_priv->process_priv;
556 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 struct kgsl_context *context;
558 int next = 0;
559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 filep->private_data = NULL;
561
562 mutex_lock(&device->mutex);
563 kgsl_check_suspended(device);
564
565 while (1) {
Jordan Crouse2db0af92011-08-08 16:05:09 -0600566 context = idr_get_next(&device->context_idr, &next);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 if (context == NULL)
568 break;
569
570 if (context->dev_priv == dev_priv) {
571 device->ftbl->drawctxt_destroy(device, context);
572 kgsl_destroy_context(dev_priv, context);
573 }
574
575 next = next + 1;
576 }
577
578 device->open_count--;
579 if (device->open_count == 0) {
580 result = device->ftbl->stop(device);
581 device->state = KGSL_STATE_INIT;
582 KGSL_PWR_WARN(device, "state -> INIT, device %d\n", device->id);
583 }
584 /* clean up any to-be-freed entries that belong to this
585 * process and this device
586 */
587 kgsl_memqueue_cleanup(device, private);
588
589 mutex_unlock(&device->mutex);
590 kfree(dev_priv);
591
592 kgsl_put_process_private(device, private);
593
594 pm_runtime_put(device->parentdev);
595 return result;
596}
597
598static int kgsl_open(struct inode *inodep, struct file *filep)
599{
600 int result;
601 struct kgsl_device_private *dev_priv;
602 struct kgsl_device *device;
603 unsigned int minor = iminor(inodep);
604
605 device = kgsl_get_minor(minor);
606 BUG_ON(device == NULL);
607
608 if (filep->f_flags & O_EXCL) {
609 KGSL_DRV_ERR(device, "O_EXCL not allowed\n");
610 return -EBUSY;
611 }
612
613 result = pm_runtime_get_sync(device->parentdev);
614 if (result < 0) {
615 KGSL_DRV_ERR(device,
616 "Runtime PM: Unable to wake up the device, rc = %d\n",
617 result);
618 return result;
619 }
620 result = 0;
621
622 dev_priv = kzalloc(sizeof(struct kgsl_device_private), GFP_KERNEL);
623 if (dev_priv == NULL) {
624 KGSL_DRV_ERR(device, "kzalloc failed(%d)\n",
625 sizeof(struct kgsl_device_private));
626 result = -ENOMEM;
627 goto err_pmruntime;
628 }
629
630 dev_priv->device = device;
631 filep->private_data = dev_priv;
632
633 /* Get file (per process) private struct */
634 dev_priv->process_priv = kgsl_get_process_private(dev_priv);
635 if (dev_priv->process_priv == NULL) {
636 result = -ENOMEM;
637 goto err_freedevpriv;
638 }
639
640 mutex_lock(&device->mutex);
641 kgsl_check_suspended(device);
642
643 if (device->open_count == 0) {
644 result = device->ftbl->start(device, true);
645
646 if (result) {
647 mutex_unlock(&device->mutex);
648 goto err_putprocess;
649 }
650 device->state = KGSL_STATE_ACTIVE;
651 KGSL_PWR_WARN(device,
652 "state -> ACTIVE, device %d\n", minor);
653 }
654 device->open_count++;
655 mutex_unlock(&device->mutex);
656
657 KGSL_DRV_INFO(device, "Initialized %s: mmu=%s pagetable_count=%d\n",
658 device->name, kgsl_mmu_enabled() ? "on" : "off",
659 kgsl_pagetable_count);
660
661 return result;
662
663err_putprocess:
664 kgsl_put_process_private(device, dev_priv->process_priv);
665err_freedevpriv:
666 filep->private_data = NULL;
667 kfree(dev_priv);
668err_pmruntime:
669 pm_runtime_put(device->parentdev);
670 return result;
671}
672
673
674/*call with private->mem_lock locked */
675static struct kgsl_mem_entry *
676kgsl_sharedmem_find(struct kgsl_process_private *private, unsigned int gpuaddr)
677{
678 struct kgsl_mem_entry *entry = NULL, *result = NULL;
679
680 BUG_ON(private == NULL);
681
682 gpuaddr &= PAGE_MASK;
683
684 list_for_each_entry(entry, &private->mem_list, list) {
685 if (entry->memdesc.gpuaddr == gpuaddr) {
686 result = entry;
687 break;
688 }
689 }
690 return result;
691}
692
693/*call with private->mem_lock locked */
694struct kgsl_mem_entry *
695kgsl_sharedmem_find_region(struct kgsl_process_private *private,
696 unsigned int gpuaddr,
697 size_t size)
698{
699 struct kgsl_mem_entry *entry = NULL, *result = NULL;
700
701 BUG_ON(private == NULL);
702
703 list_for_each_entry(entry, &private->mem_list, list) {
704 if (gpuaddr >= entry->memdesc.gpuaddr &&
705 ((gpuaddr + size) <=
706 (entry->memdesc.gpuaddr + entry->memdesc.size))) {
707 result = entry;
708 break;
709 }
710 }
711
712 return result;
713}
714EXPORT_SYMBOL(kgsl_sharedmem_find_region);
715
716uint8_t *kgsl_gpuaddr_to_vaddr(const struct kgsl_memdesc *memdesc,
717 unsigned int gpuaddr, unsigned int *size)
718{
719 BUG_ON(memdesc->hostptr == NULL);
720
721 if (memdesc->gpuaddr == 0 || (gpuaddr < memdesc->gpuaddr ||
722 gpuaddr >= memdesc->gpuaddr + memdesc->size))
723 return NULL;
724
725 *size = memdesc->size - (gpuaddr - memdesc->gpuaddr);
726 return memdesc->hostptr + (gpuaddr - memdesc->gpuaddr);
727}
728EXPORT_SYMBOL(kgsl_gpuaddr_to_vaddr);
729
730/*call all ioctl sub functions with driver locked*/
731static long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
732 unsigned int cmd, void *data)
733{
734 int result = 0;
735 struct kgsl_device_getproperty *param = data;
736
737 switch (param->type) {
738 case KGSL_PROP_VERSION:
739 {
740 struct kgsl_version version;
741 if (param->sizebytes != sizeof(version)) {
742 result = -EINVAL;
743 break;
744 }
745
746 version.drv_major = KGSL_VERSION_MAJOR;
747 version.drv_minor = KGSL_VERSION_MINOR;
748 version.dev_major = dev_priv->device->ver_major;
749 version.dev_minor = dev_priv->device->ver_minor;
750
751 if (copy_to_user(param->value, &version, sizeof(version)))
752 result = -EFAULT;
753
754 break;
755 }
756 default:
757 result = dev_priv->device->ftbl->getproperty(
758 dev_priv->device, param->type,
759 param->value, param->sizebytes);
760 }
761
762
763 return result;
764}
765
766static long kgsl_ioctl_device_waittimestamp(struct kgsl_device_private
767 *dev_priv, unsigned int cmd,
768 void *data)
769{
770 int result = 0;
771 struct kgsl_device_waittimestamp *param = data;
772
773 /* Set the active count so that suspend doesn't do the
774 wrong thing */
775
776 dev_priv->device->active_cnt++;
777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 result = dev_priv->device->ftbl->waittimestamp(dev_priv->device,
779 param->timestamp,
780 param->timeout);
781
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 /* Fire off any pending suspend operations that are in flight */
783
784 INIT_COMPLETION(dev_priv->device->suspend_gate);
785 dev_priv->device->active_cnt--;
786 complete(&dev_priv->device->suspend_gate);
787
788 return result;
789}
790static bool check_ibdesc(struct kgsl_device_private *dev_priv,
791 struct kgsl_ibdesc *ibdesc, unsigned int numibs,
792 bool parse)
793{
794 bool result = true;
795 unsigned int i;
796 for (i = 0; i < numibs; i++) {
797 struct kgsl_mem_entry *entry;
798 spin_lock(&dev_priv->process_priv->mem_lock);
799 entry = kgsl_sharedmem_find_region(dev_priv->process_priv,
800 ibdesc[i].gpuaddr, ibdesc[i].sizedwords * sizeof(uint));
801 spin_unlock(&dev_priv->process_priv->mem_lock);
802 if (entry == NULL) {
803 KGSL_DRV_ERR(dev_priv->device,
804 "invalid cmd buffer gpuaddr %08x " \
805 "sizedwords %d\n", ibdesc[i].gpuaddr,
806 ibdesc[i].sizedwords);
807 result = false;
808 break;
809 }
810
811 if (parse && !kgsl_cffdump_parse_ibs(dev_priv, &entry->memdesc,
812 ibdesc[i].gpuaddr, ibdesc[i].sizedwords, true)) {
813 KGSL_DRV_ERR(dev_priv->device,
814 "invalid cmd buffer gpuaddr %08x " \
815 "sizedwords %d numibs %d/%d\n",
816 ibdesc[i].gpuaddr,
817 ibdesc[i].sizedwords, i+1, numibs);
818 result = false;
819 break;
820 }
821 }
822 return result;
823}
824
825static long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
826 unsigned int cmd, void *data)
827{
828 int result = 0;
829 struct kgsl_ringbuffer_issueibcmds *param = data;
830 struct kgsl_ibdesc *ibdesc;
831 struct kgsl_context *context;
832
833#ifdef CONFIG_MSM_KGSL_DRM
834 kgsl_gpu_mem_flush(DRM_KGSL_GEM_CACHE_OP_TO_DEV);
835#endif
836
837 context = kgsl_find_context(dev_priv, param->drawctxt_id);
838 if (context == NULL) {
839 result = -EINVAL;
840 KGSL_DRV_ERR(dev_priv->device,
841 "invalid drawctxt drawctxt_id %d\n",
842 param->drawctxt_id);
843 goto done;
844 }
845
846 if (param->flags & KGSL_CONTEXT_SUBMIT_IB_LIST) {
847 KGSL_DRV_INFO(dev_priv->device,
848 "Using IB list mode for ib submission, numibs: %d\n",
849 param->numibs);
850 if (!param->numibs) {
851 KGSL_DRV_ERR(dev_priv->device,
852 "Invalid numibs as parameter: %d\n",
853 param->numibs);
854 result = -EINVAL;
855 goto done;
856 }
857
858 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc) * param->numibs,
859 GFP_KERNEL);
860 if (!ibdesc) {
861 KGSL_MEM_ERR(dev_priv->device,
862 "kzalloc(%d) failed\n",
863 sizeof(struct kgsl_ibdesc) * param->numibs);
864 result = -ENOMEM;
865 goto done;
866 }
867
868 if (copy_from_user(ibdesc, (void *)param->ibdesc_addr,
869 sizeof(struct kgsl_ibdesc) * param->numibs)) {
870 result = -EFAULT;
871 KGSL_DRV_ERR(dev_priv->device,
872 "copy_from_user failed\n");
873 goto free_ibdesc;
874 }
875 } else {
876 KGSL_DRV_INFO(dev_priv->device,
877 "Using single IB submission mode for ib submission\n");
878 /* If user space driver is still using the old mode of
879 * submitting single ib then we need to support that as well */
880 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc), GFP_KERNEL);
881 if (!ibdesc) {
882 KGSL_MEM_ERR(dev_priv->device,
883 "kzalloc(%d) failed\n",
884 sizeof(struct kgsl_ibdesc));
885 result = -ENOMEM;
886 goto done;
887 }
888 ibdesc[0].gpuaddr = param->ibdesc_addr;
889 ibdesc[0].sizedwords = param->numibs;
890 param->numibs = 1;
891 }
892
893 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, true)) {
894 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc");
895 result = -EINVAL;
896 goto free_ibdesc;
897 }
898
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899 result = dev_priv->device->ftbl->issueibcmds(dev_priv,
900 context,
901 ibdesc,
902 param->numibs,
903 &param->timestamp,
904 param->flags);
905
906 if (result != 0)
907 goto free_ibdesc;
908
909 /* this is a check to try to detect if a command buffer was freed
910 * during issueibcmds().
911 */
912 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, false)) {
913 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc AFTER issue");
914 result = -EINVAL;
915 goto free_ibdesc;
916 }
917
918free_ibdesc:
919 kfree(ibdesc);
920done:
921
922#ifdef CONFIG_MSM_KGSL_DRM
923 kgsl_gpu_mem_flush(DRM_KGSL_GEM_CACHE_OP_FROM_DEV);
924#endif
925
926 return result;
927}
928
929static long kgsl_ioctl_cmdstream_readtimestamp(struct kgsl_device_private
930 *dev_priv, unsigned int cmd,
931 void *data)
932{
933 struct kgsl_cmdstream_readtimestamp *param = data;
934
935 param->timestamp =
936 dev_priv->device->ftbl->readtimestamp(dev_priv->device,
937 param->type);
938
939 return 0;
940}
941
942static long kgsl_ioctl_cmdstream_freememontimestamp(struct kgsl_device_private
943 *dev_priv, unsigned int cmd,
944 void *data)
945{
946 int result = 0;
947 struct kgsl_cmdstream_freememontimestamp *param = data;
948 struct kgsl_mem_entry *entry = NULL;
949
950 spin_lock(&dev_priv->process_priv->mem_lock);
951 entry = kgsl_sharedmem_find(dev_priv->process_priv, param->gpuaddr);
952 if (entry)
953 list_del(&entry->list);
954 spin_unlock(&dev_priv->process_priv->mem_lock);
955
956 if (entry) {
957 kgsl_memqueue_freememontimestamp(dev_priv->device, entry,
958 param->timestamp, param->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959 } else {
960 KGSL_DRV_ERR(dev_priv->device,
961 "invalid gpuaddr %08x\n", param->gpuaddr);
962 result = -EINVAL;
963 }
964
965 return result;
966}
967
968static long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
969 unsigned int cmd, void *data)
970{
971 int result = 0;
972 struct kgsl_drawctxt_create *param = data;
973 struct kgsl_context *context = NULL;
974
975 context = kgsl_create_context(dev_priv);
976
977 if (context == NULL) {
978 result = -ENOMEM;
979 goto done;
980 }
981
982 if (dev_priv->device->ftbl->drawctxt_create)
983 result = dev_priv->device->ftbl->drawctxt_create(
984 dev_priv->device, dev_priv->process_priv->pagetable,
985 context, param->flags);
986
987 param->drawctxt_id = context->id;
988
989done:
990 if (result && context)
991 kgsl_destroy_context(dev_priv, context);
992
993 return result;
994}
995
996static long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
997 unsigned int cmd, void *data)
998{
999 int result = 0;
1000 struct kgsl_drawctxt_destroy *param = data;
1001 struct kgsl_context *context;
1002
1003 context = kgsl_find_context(dev_priv, param->drawctxt_id);
1004
1005 if (context == NULL) {
1006 result = -EINVAL;
1007 goto done;
1008 }
1009
1010 if (dev_priv->device->ftbl->drawctxt_destroy)
1011 dev_priv->device->ftbl->drawctxt_destroy(dev_priv->device,
1012 context);
1013
1014 kgsl_destroy_context(dev_priv, context);
1015
1016done:
1017 return result;
1018}
1019
1020static long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1021 unsigned int cmd, void *data)
1022{
1023 int result = 0;
1024 struct kgsl_sharedmem_free *param = data;
1025 struct kgsl_process_private *private = dev_priv->process_priv;
1026 struct kgsl_mem_entry *entry = NULL;
1027
1028 spin_lock(&private->mem_lock);
1029 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1030 if (entry)
1031 list_del(&entry->list);
1032 spin_unlock(&private->mem_lock);
1033
1034 if (entry) {
1035 kgsl_mem_entry_put(entry);
1036 } else {
1037 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1038 result = -EINVAL;
1039 }
1040
1041 return result;
1042}
1043
1044static struct vm_area_struct *kgsl_get_vma_from_start_addr(unsigned int addr)
1045{
1046 struct vm_area_struct *vma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047
1048 down_read(&current->mm->mmap_sem);
1049 vma = find_vma(current->mm, addr);
1050 up_read(&current->mm->mmap_sem);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001051 if (!vma)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 KGSL_CORE_ERR("find_vma(%x) failed\n", addr);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001053
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 return vma;
1055}
1056
1057static long
1058kgsl_ioctl_sharedmem_from_vmalloc(struct kgsl_device_private *dev_priv,
1059 unsigned int cmd, void *data)
1060{
1061 int result = 0, len = 0;
1062 struct kgsl_process_private *private = dev_priv->process_priv;
1063 struct kgsl_sharedmem_from_vmalloc *param = data;
1064 struct kgsl_mem_entry *entry = NULL;
1065 struct vm_area_struct *vma;
1066
1067 if (!kgsl_mmu_enabled())
1068 return -ENODEV;
1069
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 if (!param->hostptr) {
1071 KGSL_CORE_ERR("invalid hostptr %x\n", param->hostptr);
1072 result = -EINVAL;
1073 goto error;
1074 }
1075
1076 vma = kgsl_get_vma_from_start_addr(param->hostptr);
1077 if (!vma) {
1078 result = -EINVAL;
1079 goto error;
1080 }
Jordan Crouse2c542b62011-07-26 08:30:20 -06001081
1082 /*
1083 * If the user specified a length, use it, otherwise try to
1084 * infer the length if the vma region
1085 */
1086 if (param->gpuaddr != 0) {
1087 len = param->gpuaddr;
1088 } else {
1089 /*
1090 * For this to work, we have to assume the VMA region is only
1091 * for this single allocation. If it isn't, then bail out
1092 */
1093 if (vma->vm_pgoff || (param->hostptr != vma->vm_start)) {
1094 KGSL_CORE_ERR("VMA region does not match hostaddr\n");
1095 result = -EINVAL;
1096 goto error;
1097 }
1098
1099 len = vma->vm_end - vma->vm_start;
1100 }
1101
1102 /* Make sure it fits */
1103 if (len == 0 || param->hostptr + len > vma->vm_end) {
1104 KGSL_CORE_ERR("Invalid memory allocation length %d\n", len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 result = -EINVAL;
1106 goto error;
1107 }
1108
1109 entry = kgsl_mem_entry_create();
1110 if (entry == NULL) {
1111 result = -ENOMEM;
1112 goto error;
1113 }
1114
1115 result = kgsl_sharedmem_vmalloc_user(&entry->memdesc,
1116 private->pagetable, len,
1117 param->flags);
1118 if (result != 0)
1119 goto error_free_entry;
1120
1121 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1122
1123 result = remap_vmalloc_range(vma, (void *) entry->memdesc.hostptr, 0);
1124 if (result) {
1125 KGSL_CORE_ERR("remap_vmalloc_range failed: %d\n", result);
1126 goto error_free_vmalloc;
1127 }
1128
1129 param->gpuaddr = entry->memdesc.gpuaddr;
1130
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001131 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132
1133 kgsl_mem_entry_attach_process(entry, private);
1134
1135 /* Process specific statistics */
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001136 kgsl_process_add_stats(private, entry->memtype, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137
1138 kgsl_check_idle(dev_priv->device);
1139 return 0;
1140
1141error_free_vmalloc:
1142 kgsl_sharedmem_free(&entry->memdesc);
1143
1144error_free_entry:
1145 kfree(entry);
1146
1147error:
1148 kgsl_check_idle(dev_priv->device);
1149 return result;
1150}
1151
1152static inline int _check_region(unsigned long start, unsigned long size,
1153 uint64_t len)
1154{
1155 uint64_t end = ((uint64_t) start) + size;
1156 return (end > len);
1157}
1158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159static int kgsl_get_phys_file(int fd, unsigned long *start, unsigned long *len,
1160 unsigned long *vstart, struct file **filep)
1161{
1162 struct file *fbfile;
1163 int ret = 0;
1164 dev_t rdev;
1165 struct fb_info *info;
1166
1167 *filep = NULL;
Jordan Crousefd978432011-09-02 14:34:32 -06001168#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001169 if (!get_pmem_file(fd, start, vstart, len, filep))
1170 return 0;
Jordan Crousefd978432011-09-02 14:34:32 -06001171#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172
1173 fbfile = fget(fd);
1174 if (fbfile == NULL) {
1175 KGSL_CORE_ERR("fget_light failed\n");
1176 return -1;
1177 }
1178
1179 rdev = fbfile->f_dentry->d_inode->i_rdev;
1180 info = MAJOR(rdev) == FB_MAJOR ? registered_fb[MINOR(rdev)] : NULL;
1181 if (info) {
1182 *start = info->fix.smem_start;
1183 *len = info->fix.smem_len;
1184 *vstart = (unsigned long)__va(info->fix.smem_start);
1185 ret = 0;
1186 } else {
1187 KGSL_CORE_ERR("framebuffer minor %d not found\n",
1188 MINOR(rdev));
1189 ret = -1;
1190 }
1191
1192 fput(fbfile);
1193
1194 return ret;
1195}
1196
1197static int kgsl_setup_phys_file(struct kgsl_mem_entry *entry,
1198 struct kgsl_pagetable *pagetable,
1199 unsigned int fd, unsigned int offset,
1200 size_t size)
1201{
1202 int ret;
1203 unsigned long phys, virt, len;
1204 struct file *filep;
1205
1206 ret = kgsl_get_phys_file(fd, &phys, &len, &virt, &filep);
1207 if (ret)
1208 return ret;
1209
Wei Zou4061c0b2011-07-08 10:24:22 -07001210 if (phys == 0) {
1211 ret = -EINVAL;
1212 goto err;
1213 }
1214
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 if (offset >= len) {
1216 ret = -EINVAL;
1217 goto err;
1218 }
1219
1220 if (size == 0)
1221 size = len;
1222
1223 /* Adjust the size of the region to account for the offset */
1224 size += offset & ~PAGE_MASK;
1225
1226 size = ALIGN(size, PAGE_SIZE);
1227
1228 if (_check_region(offset & PAGE_MASK, size, len)) {
1229 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1230 "than pmem region length %ld\n",
1231 offset & PAGE_MASK, size, len);
1232 ret = -EINVAL;
1233 goto err;
1234
1235 }
1236
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001237 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238
1239 entry->memdesc.pagetable = pagetable;
1240 entry->memdesc.size = size;
1241 entry->memdesc.physaddr = phys + (offset & PAGE_MASK);
1242 entry->memdesc.hostptr = (void *) (virt + (offset & PAGE_MASK));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001243
1244 ret = memdesc_sg_phys(&entry->memdesc,
1245 phys + (offset & PAGE_MASK), size);
1246 if (ret)
1247 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248
1249 return 0;
1250err:
Jordan Crousefd978432011-09-02 14:34:32 -06001251#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001252 put_pmem_file(filep);
Jordan Crousefd978432011-09-02 14:34:32 -06001253#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001254 return ret;
1255}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001256
Jordan Croused17e9aa2011-10-12 16:57:48 -06001257static int memdesc_sg_virt(struct kgsl_memdesc *memdesc,
1258 void *addr, int size)
1259{
1260 int i;
1261 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
1262 unsigned long paddr = (unsigned long) addr;
1263
1264 memdesc->sg = kmalloc(sglen * sizeof(struct scatterlist),
1265 GFP_KERNEL);
1266 if (memdesc->sg == NULL)
1267 return -ENOMEM;
1268
1269 memdesc->sglen = sglen;
1270 sg_init_table(memdesc->sg, sglen);
1271
1272 spin_lock(&current->mm->page_table_lock);
1273
1274 for (i = 0; i < sglen; i++, paddr += PAGE_SIZE) {
1275 struct page *page;
1276 pmd_t *ppmd;
1277 pte_t *ppte;
1278 pgd_t *ppgd = pgd_offset(current->mm, paddr);
1279
1280 if (pgd_none(*ppgd) || pgd_bad(*ppgd))
1281 goto err;
1282
1283 ppmd = pmd_offset(ppgd, paddr);
1284 if (pmd_none(*ppmd) || pmd_bad(*ppmd))
1285 goto err;
1286
1287 ppte = pte_offset_map(ppmd, paddr);
1288 if (ppte == NULL)
1289 goto err;
1290
1291 page = pfn_to_page(pte_pfn(*ppte));
1292 if (!page)
1293 goto err;
1294
1295 sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0);
1296 pte_unmap(ppte);
1297 }
1298
1299 spin_unlock(&current->mm->page_table_lock);
1300
1301 return 0;
1302
1303err:
1304 spin_unlock(&current->mm->page_table_lock);
1305 kfree(memdesc->sg);
1306 memdesc->sg = NULL;
1307
1308 return -EINVAL;
1309}
1310
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311static int kgsl_setup_hostptr(struct kgsl_mem_entry *entry,
1312 struct kgsl_pagetable *pagetable,
1313 void *hostptr, unsigned int offset,
1314 size_t size)
1315{
1316 struct vm_area_struct *vma;
1317 unsigned int len;
1318
1319 down_read(&current->mm->mmap_sem);
1320 vma = find_vma(current->mm, (unsigned int) hostptr);
1321 up_read(&current->mm->mmap_sem);
1322
1323 if (!vma) {
1324 KGSL_CORE_ERR("find_vma(%p) failed\n", hostptr);
1325 return -EINVAL;
1326 }
1327
1328 /* We don't necessarily start at vma->vm_start */
1329 len = vma->vm_end - (unsigned long) hostptr;
1330
1331 if (offset >= len)
1332 return -EINVAL;
1333
1334 if (!KGSL_IS_PAGE_ALIGNED((unsigned long) hostptr) ||
1335 !KGSL_IS_PAGE_ALIGNED(len)) {
1336 KGSL_CORE_ERR("user address len(%u)"
1337 "and start(%p) must be page"
1338 "aligned\n", len, hostptr);
1339 return -EINVAL;
1340 }
1341
1342 if (size == 0)
1343 size = len;
1344
1345 /* Adjust the size of the region to account for the offset */
1346 size += offset & ~PAGE_MASK;
1347
1348 size = ALIGN(size, PAGE_SIZE);
1349
1350 if (_check_region(offset & PAGE_MASK, size, len)) {
1351 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1352 "than region length %d\n",
1353 offset & PAGE_MASK, size, len);
1354 return -EINVAL;
1355 }
1356
1357 entry->memdesc.pagetable = pagetable;
1358 entry->memdesc.size = size;
1359 entry->memdesc.hostptr = hostptr + (offset & PAGE_MASK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360
Jordan Croused17e9aa2011-10-12 16:57:48 -06001361 return memdesc_sg_virt(&entry->memdesc,
1362 hostptr + (offset & PAGE_MASK), size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001363}
1364
1365#ifdef CONFIG_ASHMEM
1366static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1367 struct kgsl_pagetable *pagetable,
1368 int fd, void *hostptr, size_t size)
1369{
1370 int ret;
1371 struct vm_area_struct *vma;
1372 struct file *filep, *vmfile;
1373 unsigned long len;
Jordan Crouse2c542b62011-07-26 08:30:20 -06001374 unsigned int hostaddr = (unsigned int) hostptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001375
Jordan Crouse2c542b62011-07-26 08:30:20 -06001376 vma = kgsl_get_vma_from_start_addr(hostaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 if (vma == NULL)
1378 return -EINVAL;
1379
Jordan Crouse2c542b62011-07-26 08:30:20 -06001380 if (vma->vm_pgoff || vma->vm_start != hostaddr) {
1381 KGSL_CORE_ERR("Invalid vma region\n");
1382 return -EINVAL;
1383 }
1384
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 len = vma->vm_end - vma->vm_start;
1386
1387 if (size == 0)
1388 size = len;
1389
1390 if (size != len) {
1391 KGSL_CORE_ERR("Invalid size %d for vma region %p\n",
1392 size, hostptr);
1393 return -EINVAL;
1394 }
1395
1396 ret = get_ashmem_file(fd, &filep, &vmfile, &len);
1397
1398 if (ret) {
1399 KGSL_CORE_ERR("get_ashmem_file failed\n");
1400 return ret;
1401 }
1402
1403 if (vmfile != vma->vm_file) {
1404 KGSL_CORE_ERR("ashmem shmem file does not match vma\n");
1405 ret = -EINVAL;
1406 goto err;
1407 }
1408
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001409 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 entry->memdesc.pagetable = pagetable;
1411 entry->memdesc.size = ALIGN(size, PAGE_SIZE);
1412 entry->memdesc.hostptr = hostptr;
Jordan Croused17e9aa2011-10-12 16:57:48 -06001413
1414 ret = memdesc_sg_virt(&entry->memdesc, hostptr, size);
1415 if (ret)
1416 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417
1418 return 0;
1419
1420err:
1421 put_ashmem_file(filep);
1422 return ret;
1423}
1424#else
1425static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1426 struct kgsl_pagetable *pagetable,
1427 int fd, void *hostptr, size_t size)
1428{
1429 return -EINVAL;
1430}
1431#endif
1432
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001433static int kgsl_setup_ion(struct kgsl_mem_entry *entry,
1434 struct kgsl_pagetable *pagetable, int fd)
1435{
1436 struct ion_handle *handle;
1437 struct scatterlist *s;
1438 unsigned long flags;
1439
1440 if (kgsl_ion_client == NULL) {
1441 kgsl_ion_client = msm_ion_client_create(UINT_MAX, KGSL_NAME);
1442 if (kgsl_ion_client == NULL)
1443 return -ENODEV;
1444 }
1445
1446 handle = ion_import_fd(kgsl_ion_client, fd);
1447 if (IS_ERR_OR_NULL(handle))
1448 return PTR_ERR(handle);
1449
1450 entry->memtype = KGSL_MEM_ENTRY_ION;
1451 entry->priv_data = handle;
1452 entry->memdesc.pagetable = pagetable;
1453 entry->memdesc.size = 0;
1454
1455 if (ion_handle_get_flags(kgsl_ion_client, handle, &flags))
1456 goto err;
1457
1458 entry->memdesc.sg = ion_map_dma(kgsl_ion_client, handle, flags);
1459
1460 if (IS_ERR_OR_NULL(entry->memdesc.sg))
1461 goto err;
1462
1463 /* Calculate the size of the memdesc from the sglist */
1464
1465 entry->memdesc.sglen = 0;
1466
1467 for (s = entry->memdesc.sg; s != NULL; s = sg_next(s)) {
1468 entry->memdesc.size += s->length;
1469 entry->memdesc.sglen++;
1470 }
1471
1472 return 0;
1473err:
1474 ion_free(kgsl_ion_client, handle);
1475 return -ENOMEM;
1476}
1477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478static long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
1479 unsigned int cmd, void *data)
1480{
1481 int result = -EINVAL;
1482 struct kgsl_map_user_mem *param = data;
1483 struct kgsl_mem_entry *entry = NULL;
1484 struct kgsl_process_private *private = dev_priv->process_priv;
Jason848741a2011-07-12 10:24:25 -07001485 enum kgsl_user_mem_type memtype;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486
1487 entry = kgsl_mem_entry_create();
1488
1489 if (entry == NULL)
1490 return -ENOMEM;
1491
Jason848741a2011-07-12 10:24:25 -07001492 if (_IOC_SIZE(cmd) == sizeof(struct kgsl_sharedmem_from_pmem))
1493 memtype = KGSL_USER_MEM_TYPE_PMEM;
1494 else
1495 memtype = param->memtype;
1496
1497 switch (memtype) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498 case KGSL_USER_MEM_TYPE_PMEM:
1499 if (param->fd == 0 || param->len == 0)
1500 break;
1501
1502 result = kgsl_setup_phys_file(entry, private->pagetable,
1503 param->fd, param->offset,
1504 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001505 entry->memtype = KGSL_MEM_ENTRY_PMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506 break;
1507
1508 case KGSL_USER_MEM_TYPE_ADDR:
1509 if (!kgsl_mmu_enabled()) {
1510 KGSL_DRV_ERR(dev_priv->device,
1511 "Cannot map paged memory with the "
1512 "MMU disabled\n");
1513 break;
1514 }
1515
1516 if (param->hostptr == 0)
1517 break;
1518
1519 result = kgsl_setup_hostptr(entry, private->pagetable,
1520 (void *) param->hostptr,
1521 param->offset, param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001522 entry->memtype = KGSL_MEM_ENTRY_USER;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523 break;
1524
1525 case KGSL_USER_MEM_TYPE_ASHMEM:
1526 if (!kgsl_mmu_enabled()) {
1527 KGSL_DRV_ERR(dev_priv->device,
1528 "Cannot map paged memory with the "
1529 "MMU disabled\n");
1530 break;
1531 }
1532
1533 if (param->hostptr == 0)
1534 break;
1535
1536 result = kgsl_setup_ashmem(entry, private->pagetable,
1537 param->fd, (void *) param->hostptr,
1538 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001539
1540 entry->memtype = KGSL_MEM_ENTRY_ASHMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001541 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001542 case KGSL_USER_MEM_TYPE_ION:
1543 result = kgsl_setup_ion(entry, private->pagetable,
1544 param->fd);
1545 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001546 default:
Jason848741a2011-07-12 10:24:25 -07001547 KGSL_CORE_ERR("Invalid memory type: %x\n", memtype);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001548 break;
1549 }
1550
1551 if (result)
1552 goto error;
1553
1554 result = kgsl_mmu_map(private->pagetable,
1555 &entry->memdesc,
1556 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
1557
1558 if (result)
1559 goto error_put_file_ptr;
1560
1561 /* Adjust the returned value for a non 4k aligned offset */
1562 param->gpuaddr = entry->memdesc.gpuaddr + (param->offset & ~PAGE_MASK);
1563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564 KGSL_STATS_ADD(param->len, kgsl_driver.stats.mapped,
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001565 kgsl_driver.stats.mapped_max);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001567 kgsl_process_add_stats(private, entry->memtype, param->len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001568
1569 kgsl_mem_entry_attach_process(entry, private);
1570
1571 kgsl_check_idle(dev_priv->device);
1572 return result;
1573
1574 error_put_file_ptr:
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001575 if (entry->priv_data)
1576 fput(entry->priv_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577
1578error:
1579 kfree(entry);
1580 kgsl_check_idle(dev_priv->device);
1581 return result;
1582}
1583
1584/*This function flushes a graphics memory allocation from CPU cache
1585 *when caching is enabled with MMU*/
1586static long
1587kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
1588 unsigned int cmd, void *data)
1589{
1590 int result = 0;
1591 struct kgsl_mem_entry *entry;
1592 struct kgsl_sharedmem_free *param = data;
1593 struct kgsl_process_private *private = dev_priv->process_priv;
1594
1595 spin_lock(&private->mem_lock);
1596 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1597 if (!entry) {
1598 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1599 result = -EINVAL;
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001600 goto done;
1601 }
1602 if (!entry->memdesc.hostptr)
1603 entry->memdesc.hostptr =
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 kgsl_gpuaddr_to_vaddr(&entry->memdesc,
1605 param->gpuaddr, &entry->memdesc.size);
1606
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001607 if (!entry->memdesc.hostptr) {
1608 KGSL_CORE_ERR("invalid hostptr with gpuaddr %08x\n",
1609 param->gpuaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001611 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001612
1613 kgsl_cache_range_op(&entry->memdesc, KGSL_CACHE_OP_CLEAN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614done:
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001615 spin_unlock(&private->mem_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001616 return result;
1617}
1618
1619static long
1620kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
1621 unsigned int cmd, void *data)
1622{
1623 struct kgsl_process_private *private = dev_priv->process_priv;
1624 struct kgsl_gpumem_alloc *param = data;
1625 struct kgsl_mem_entry *entry;
1626 int result;
1627
1628 entry = kgsl_mem_entry_create();
1629 if (entry == NULL)
1630 return -ENOMEM;
1631
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632 result = kgsl_allocate_user(&entry->memdesc, private->pagetable,
1633 param->size, param->flags);
1634
1635 if (result == 0) {
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001636 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 kgsl_mem_entry_attach_process(entry, private);
1638 param->gpuaddr = entry->memdesc.gpuaddr;
1639
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001640 kgsl_process_add_stats(private, entry->memtype, param->size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 } else
1642 kfree(entry);
1643
1644 kgsl_check_idle(dev_priv->device);
1645 return result;
1646}
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001647static long kgsl_ioctl_cff_syncmem(struct kgsl_device_private *dev_priv,
1648 unsigned int cmd, void *data)
1649{
1650 int result = 0;
1651 struct kgsl_cff_syncmem *param = data;
1652 struct kgsl_process_private *private = dev_priv->process_priv;
1653 struct kgsl_mem_entry *entry = NULL;
1654
1655 spin_lock(&private->mem_lock);
1656 entry = kgsl_sharedmem_find_region(private, param->gpuaddr, param->len);
1657 if (entry)
1658 kgsl_cffdump_syncmem(dev_priv, &entry->memdesc, param->gpuaddr,
1659 param->len, true);
1660 else
1661 result = -EINVAL;
1662 spin_unlock(&private->mem_lock);
1663 return result;
1664}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001666static long kgsl_ioctl_cff_user_event(struct kgsl_device_private *dev_priv,
1667 unsigned int cmd, void *data)
1668{
1669 int result = 0;
1670 struct kgsl_cff_user_event *param = data;
1671
1672 kgsl_cffdump_user_event(param->cff_opcode, param->op1, param->op2,
1673 param->op3, param->op4, param->op5);
1674
1675 return result;
1676}
1677
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678typedef long (*kgsl_ioctl_func_t)(struct kgsl_device_private *,
1679 unsigned int, void *);
1680
1681#define KGSL_IOCTL_FUNC(_cmd, _func, _lock) \
1682 [_IOC_NR(_cmd)] = { .cmd = _cmd, .func = _func, .lock = _lock }
1683
1684static const struct {
1685 unsigned int cmd;
1686 kgsl_ioctl_func_t func;
1687 int lock;
1688} kgsl_ioctl_funcs[] = {
1689 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_GETPROPERTY,
1690 kgsl_ioctl_device_getproperty, 1),
1691 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_WAITTIMESTAMP,
1692 kgsl_ioctl_device_waittimestamp, 1),
1693 KGSL_IOCTL_FUNC(IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS,
1694 kgsl_ioctl_rb_issueibcmds, 1),
1695 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_READTIMESTAMP,
1696 kgsl_ioctl_cmdstream_readtimestamp, 1),
1697 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP,
1698 kgsl_ioctl_cmdstream_freememontimestamp, 1),
1699 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_CREATE,
1700 kgsl_ioctl_drawctxt_create, 1),
1701 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_DESTROY,
1702 kgsl_ioctl_drawctxt_destroy, 1),
1703 KGSL_IOCTL_FUNC(IOCTL_KGSL_MAP_USER_MEM,
1704 kgsl_ioctl_map_user_mem, 0),
1705 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_PMEM,
1706 kgsl_ioctl_map_user_mem, 0),
1707 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FREE,
1708 kgsl_ioctl_sharedmem_free, 0),
1709 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_VMALLOC,
1710 kgsl_ioctl_sharedmem_from_vmalloc, 0),
1711 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FLUSH_CACHE,
1712 kgsl_ioctl_sharedmem_flush_cache, 0),
1713 KGSL_IOCTL_FUNC(IOCTL_KGSL_GPUMEM_ALLOC,
1714 kgsl_ioctl_gpumem_alloc, 0),
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001715 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_SYNCMEM,
1716 kgsl_ioctl_cff_syncmem, 0),
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001717 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_USER_EVENT,
1718 kgsl_ioctl_cff_user_event, 0),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001719};
1720
1721static long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1722{
1723 struct kgsl_device_private *dev_priv = filep->private_data;
1724 unsigned int nr = _IOC_NR(cmd);
1725 kgsl_ioctl_func_t func;
1726 int lock, ret;
1727 char ustack[64];
1728 void *uptr = NULL;
1729
1730 BUG_ON(dev_priv == NULL);
1731
1732 /* Workaround for an previously incorrectly defined ioctl code.
1733 This helps ensure binary compatability */
1734
1735 if (cmd == IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP_OLD)
1736 cmd = IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP;
Jason Varbedian80ba33d2011-07-11 17:29:05 -07001737 else if (cmd == IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_OLD)
1738 cmd = IOCTL_KGSL_CMDSTREAM_READTIMESTAMP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001739
1740 if (cmd & (IOC_IN | IOC_OUT)) {
1741 if (_IOC_SIZE(cmd) < sizeof(ustack))
1742 uptr = ustack;
1743 else {
1744 uptr = kzalloc(_IOC_SIZE(cmd), GFP_KERNEL);
1745 if (uptr == NULL) {
1746 KGSL_MEM_ERR(dev_priv->device,
1747 "kzalloc(%d) failed\n", _IOC_SIZE(cmd));
1748 ret = -ENOMEM;
1749 goto done;
1750 }
1751 }
1752
1753 if (cmd & IOC_IN) {
1754 if (copy_from_user(uptr, (void __user *) arg,
1755 _IOC_SIZE(cmd))) {
1756 ret = -EFAULT;
1757 goto done;
1758 }
1759 } else
1760 memset(uptr, 0, _IOC_SIZE(cmd));
1761 }
1762
1763 if (nr < ARRAY_SIZE(kgsl_ioctl_funcs) &&
1764 kgsl_ioctl_funcs[nr].func != NULL) {
1765 func = kgsl_ioctl_funcs[nr].func;
1766 lock = kgsl_ioctl_funcs[nr].lock;
1767 } else {
1768 func = dev_priv->device->ftbl->ioctl;
1769 if (!func) {
1770 KGSL_DRV_INFO(dev_priv->device,
1771 "invalid ioctl code %08x\n", cmd);
1772 ret = -EINVAL;
1773 goto done;
1774 }
1775 lock = 1;
1776 }
1777
1778 if (lock) {
1779 mutex_lock(&dev_priv->device->mutex);
1780 kgsl_check_suspended(dev_priv->device);
1781 }
1782
1783 ret = func(dev_priv, cmd, uptr);
1784
1785 if (lock) {
1786 kgsl_check_idle_locked(dev_priv->device);
1787 mutex_unlock(&dev_priv->device->mutex);
1788 }
1789
1790 if (ret == 0 && (cmd & IOC_OUT)) {
1791 if (copy_to_user((void __user *) arg, uptr, _IOC_SIZE(cmd)))
1792 ret = -EFAULT;
1793 }
1794
1795done:
1796 if (_IOC_SIZE(cmd) >= sizeof(ustack))
1797 kfree(uptr);
1798
1799 return ret;
1800}
1801
1802static int
1803kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
1804{
1805 struct kgsl_memdesc *memdesc = &device->memstore;
1806 int result;
1807 unsigned int vma_size = vma->vm_end - vma->vm_start;
1808
1809 /* The memstore can only be mapped as read only */
1810
1811 if (vma->vm_flags & VM_WRITE)
1812 return -EPERM;
1813
1814 if (memdesc->size != vma_size) {
1815 KGSL_MEM_ERR(device, "memstore bad size: %d should be %d\n",
1816 vma_size, memdesc->size);
1817 return -EINVAL;
1818 }
1819
1820 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1821
1822 result = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1823 vma_size, vma->vm_page_prot);
1824 if (result != 0)
1825 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
1826 result);
1827
1828 return result;
1829}
1830
Jordan Crouse4283e172011-09-26 14:45:47 -06001831/*
1832 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
1833 * Increase the refcount to make sure that the accounting stays correct
1834 */
1835
1836static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
1837{
1838 struct kgsl_mem_entry *entry = vma->vm_private_data;
1839 kgsl_mem_entry_get(entry);
1840}
1841
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842static int
1843kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1844{
1845 struct kgsl_mem_entry *entry = vma->vm_private_data;
1846
Jordan Croused17e9aa2011-10-12 16:57:48 -06001847 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848 return VM_FAULT_SIGBUS;
1849
1850 return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
1851}
1852
1853static void
1854kgsl_gpumem_vm_close(struct vm_area_struct *vma)
1855{
1856 struct kgsl_mem_entry *entry = vma->vm_private_data;
1857 kgsl_mem_entry_put(entry);
1858}
1859
1860static struct vm_operations_struct kgsl_gpumem_vm_ops = {
Jordan Crouse4283e172011-09-26 14:45:47 -06001861 .open = kgsl_gpumem_vm_open,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001862 .fault = kgsl_gpumem_vm_fault,
1863 .close = kgsl_gpumem_vm_close,
1864};
1865
1866static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
1867{
1868 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001869 struct kgsl_device_private *dev_priv = file->private_data;
1870 struct kgsl_process_private *private = dev_priv->process_priv;
Jordan Crouse976cf0e2011-09-12 10:41:49 -06001871 struct kgsl_mem_entry *tmp, *entry = NULL;
Jordan Crouse2db0af92011-08-08 16:05:09 -06001872 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001873
1874 /* Handle leagacy behavior for memstore */
1875
1876 if (vma_offset == device->memstore.physaddr)
1877 return kgsl_mmap_memstore(device, vma);
1878
1879 /* Find a chunk of GPU memory */
1880
1881 spin_lock(&private->mem_lock);
Jordan Crouse976cf0e2011-09-12 10:41:49 -06001882 list_for_each_entry(tmp, &private->mem_list, list) {
1883 if (vma_offset == tmp->memdesc.gpuaddr) {
1884 kgsl_mem_entry_get(tmp);
1885 entry = tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 break;
1887 }
1888 }
1889 spin_unlock(&private->mem_lock);
1890
1891 if (entry == NULL)
1892 return -EINVAL;
1893
Jordan Croused17e9aa2011-10-12 16:57:48 -06001894 if (!entry->memdesc.ops ||
1895 !entry->memdesc.ops->vmflags ||
1896 !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 return -EINVAL;
1898
1899 vma->vm_flags |= entry->memdesc.ops->vmflags(&entry->memdesc);
1900
1901 vma->vm_private_data = entry;
1902 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1903 vma->vm_ops = &kgsl_gpumem_vm_ops;
1904 vma->vm_file = file;
1905
1906 return 0;
1907}
1908
1909static const struct file_operations kgsl_fops = {
1910 .owner = THIS_MODULE,
1911 .release = kgsl_release,
1912 .open = kgsl_open,
1913 .mmap = kgsl_mmap,
1914 .unlocked_ioctl = kgsl_ioctl,
1915};
1916
1917struct kgsl_driver kgsl_driver = {
1918 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
1919 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
1920 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
1921};
1922EXPORT_SYMBOL(kgsl_driver);
1923
1924void kgsl_unregister_device(struct kgsl_device *device)
1925{
1926 int minor;
1927
1928 mutex_lock(&kgsl_driver.devlock);
1929 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
1930 if (device == kgsl_driver.devp[minor])
1931 break;
1932 }
1933
1934 mutex_unlock(&kgsl_driver.devlock);
1935
1936 if (minor == KGSL_DEVICE_MAX)
1937 return;
1938
1939 kgsl_cffdump_close(device->id);
1940 kgsl_pwrctrl_uninit_sysfs(device);
1941
1942 wake_lock_destroy(&device->idle_wakelock);
1943 pm_qos_remove_request(&device->pm_qos_req_dma);
1944
1945 idr_destroy(&device->context_idr);
1946
1947 if (device->memstore.hostptr)
1948 kgsl_sharedmem_free(&device->memstore);
1949
1950 kgsl_mmu_close(device);
1951
1952 if (device->work_queue) {
1953 destroy_workqueue(device->work_queue);
1954 device->work_queue = NULL;
1955 }
1956
1957 device_destroy(kgsl_driver.class,
1958 MKDEV(MAJOR(kgsl_driver.major), minor));
1959
1960 mutex_lock(&kgsl_driver.devlock);
1961 kgsl_driver.devp[minor] = NULL;
1962 mutex_unlock(&kgsl_driver.devlock);
1963}
1964EXPORT_SYMBOL(kgsl_unregister_device);
1965
1966int
1967kgsl_register_device(struct kgsl_device *device)
1968{
1969 int minor, ret;
1970 dev_t dev;
1971
1972 /* Find a minor for the device */
1973
1974 mutex_lock(&kgsl_driver.devlock);
1975 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
1976 if (kgsl_driver.devp[minor] == NULL) {
1977 kgsl_driver.devp[minor] = device;
1978 break;
1979 }
1980 }
1981
1982 mutex_unlock(&kgsl_driver.devlock);
1983
1984 if (minor == KGSL_DEVICE_MAX) {
1985 KGSL_CORE_ERR("minor devices exhausted\n");
1986 return -ENODEV;
1987 }
1988
1989 /* Create the device */
1990 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
1991 device->dev = device_create(kgsl_driver.class,
1992 device->parentdev,
1993 dev, device,
1994 device->name);
1995
1996 if (IS_ERR(device->dev)) {
1997 ret = PTR_ERR(device->dev);
1998 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
1999 goto err_devlist;
2000 }
2001
2002 dev_set_drvdata(device->parentdev, device);
2003
2004 /* Generic device initialization */
2005 init_waitqueue_head(&device->wait_queue);
2006
2007 kgsl_cffdump_open(device->id);
2008
2009 init_completion(&device->hwaccess_gate);
2010 init_completion(&device->suspend_gate);
2011
2012 ATOMIC_INIT_NOTIFIER_HEAD(&device->ts_notifier_list);
2013
2014 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
2015 ret = kgsl_create_device_workqueue(device);
2016 if (ret)
2017 goto err_devlist;
2018
2019 INIT_WORK(&device->idle_check_ws, kgsl_idle_check);
Jordan Crouse1bf80aa2011-10-12 16:57:47 -06002020 INIT_WORK(&device->ts_expired_ws, kgsl_timestamp_expired);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002021
2022 INIT_LIST_HEAD(&device->memqueue);
2023
2024 ret = kgsl_mmu_init(device);
2025 if (ret != 0)
2026 goto err_dest_work_q;
2027
2028 ret = kgsl_allocate_contiguous(&device->memstore,
2029 sizeof(struct kgsl_devmemstore));
2030
2031 if (ret != 0)
2032 goto err_close_mmu;
2033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034 wake_lock_init(&device->idle_wakelock, WAKE_LOCK_IDLE, device->name);
2035 pm_qos_add_request(&device->pm_qos_req_dma, PM_QOS_CPU_DMA_LATENCY,
2036 PM_QOS_DEFAULT_VALUE);
2037
2038 idr_init(&device->context_idr);
2039
2040 /* sysfs and debugfs initalization - failure here is non fatal */
2041
2042 /* Initialize logging */
2043 kgsl_device_debugfs_init(device);
2044
2045 /* Initialize common sysfs entries */
2046 kgsl_pwrctrl_init_sysfs(device);
2047
2048 return 0;
2049
2050err_close_mmu:
2051 kgsl_mmu_close(device);
2052err_dest_work_q:
2053 destroy_workqueue(device->work_queue);
2054 device->work_queue = NULL;
2055err_devlist:
2056 mutex_lock(&kgsl_driver.devlock);
2057 kgsl_driver.devp[minor] = NULL;
2058 mutex_unlock(&kgsl_driver.devlock);
2059
2060 return ret;
2061}
2062EXPORT_SYMBOL(kgsl_register_device);
2063
2064int kgsl_device_platform_probe(struct kgsl_device *device,
2065 irqreturn_t (*dev_isr) (int, void*))
2066{
2067 int status = -EINVAL;
2068 struct kgsl_memregion *regspace = NULL;
2069 struct resource *res;
2070 struct platform_device *pdev =
2071 container_of(device->parentdev, struct platform_device, dev);
2072
2073 pm_runtime_enable(device->parentdev);
2074
2075 status = kgsl_pwrctrl_init(device);
2076 if (status)
2077 goto error;
2078
2079 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2080 device->iomemname);
2081 if (res == NULL) {
2082 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
2083 status = -EINVAL;
2084 goto error_pwrctrl_close;
2085 }
2086 if (res->start == 0 || resource_size(res) == 0) {
2087 KGSL_DRV_ERR(device, "dev %d invalid regspace\n", device->id);
2088 status = -EINVAL;
2089 goto error_pwrctrl_close;
2090 }
2091
2092 regspace = &device->regspace;
2093 regspace->mmio_phys_base = res->start;
2094 regspace->sizebytes = resource_size(res);
2095
2096 if (!request_mem_region(regspace->mmio_phys_base,
2097 regspace->sizebytes, device->name)) {
2098 KGSL_DRV_ERR(device, "request_mem_region failed\n");
2099 status = -ENODEV;
2100 goto error_pwrctrl_close;
2101 }
2102
2103 regspace->mmio_virt_base = ioremap(regspace->mmio_phys_base,
2104 regspace->sizebytes);
2105
2106 if (regspace->mmio_virt_base == NULL) {
2107 KGSL_DRV_ERR(device, "ioremap failed\n");
2108 status = -ENODEV;
2109 goto error_release_mem;
2110 }
2111
2112 status = request_irq(device->pwrctrl.interrupt_num, dev_isr,
2113 IRQF_TRIGGER_HIGH, device->name, device);
2114 if (status) {
2115 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
2116 device->pwrctrl.interrupt_num, status);
2117 goto error_iounmap;
2118 }
2119 device->pwrctrl.have_irq = 1;
2120 disable_irq(device->pwrctrl.interrupt_num);
2121
2122 KGSL_DRV_INFO(device,
2123 "dev_id %d regs phys 0x%08x size 0x%08x virt %p\n",
2124 device->id, regspace->mmio_phys_base,
2125 regspace->sizebytes, regspace->mmio_virt_base);
2126
2127
2128 status = kgsl_register_device(device);
2129 if (!status)
2130 return status;
2131
2132 free_irq(device->pwrctrl.interrupt_num, NULL);
2133 device->pwrctrl.have_irq = 0;
2134error_iounmap:
2135 iounmap(regspace->mmio_virt_base);
2136 regspace->mmio_virt_base = NULL;
2137error_release_mem:
2138 release_mem_region(regspace->mmio_phys_base, regspace->sizebytes);
2139error_pwrctrl_close:
2140 kgsl_pwrctrl_close(device);
2141error:
2142 return status;
2143}
2144EXPORT_SYMBOL(kgsl_device_platform_probe);
2145
2146void kgsl_device_platform_remove(struct kgsl_device *device)
2147{
2148 struct kgsl_memregion *regspace = &device->regspace;
2149
2150 kgsl_unregister_device(device);
2151
2152 if (regspace->mmio_virt_base != NULL) {
2153 iounmap(regspace->mmio_virt_base);
2154 regspace->mmio_virt_base = NULL;
2155 release_mem_region(regspace->mmio_phys_base,
2156 regspace->sizebytes);
2157 }
2158 kgsl_pwrctrl_close(device);
2159
2160 pm_runtime_disable(device->parentdev);
2161}
2162EXPORT_SYMBOL(kgsl_device_platform_remove);
2163
2164static int __devinit
2165kgsl_ptdata_init(void)
2166{
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002167 kgsl_driver.ptpool = kgsl_mmu_ptpool_init(KGSL_PAGETABLE_SIZE,
2168 kgsl_pagetable_count);
2169 if (!kgsl_driver.ptpool)
2170 return -ENOMEM;
2171 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172}
2173
2174static void kgsl_core_exit(void)
2175{
2176 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
2177
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002178 kgsl_mmu_ptpool_destroy(&kgsl_driver.ptpool);
2179 kgsl_driver.ptpool = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180
2181 device_unregister(&kgsl_driver.virtdev);
2182
2183 if (kgsl_driver.class) {
2184 class_destroy(kgsl_driver.class);
2185 kgsl_driver.class = NULL;
2186 }
2187
2188 kgsl_drm_exit();
2189 kgsl_cffdump_destroy();
Jordan Croused8f1c6b2011-10-04 09:31:29 -06002190 kgsl_core_debugfs_close();
2191 kgsl_sharedmem_uninit_sysfs();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002192}
2193
2194static int __init kgsl_core_init(void)
2195{
2196 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 /* alloc major and minor device numbers */
2198 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
2199 KGSL_NAME);
2200 if (result < 0) {
2201 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
2202 goto err;
2203 }
2204
2205 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
2206 kgsl_driver.cdev.owner = THIS_MODULE;
2207 kgsl_driver.cdev.ops = &kgsl_fops;
2208 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
2209 KGSL_DEVICE_MAX);
2210
2211 if (result) {
2212 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d,"
2213 " result= %d\n", kgsl_driver.major, result);
2214 goto err;
2215 }
2216
2217 kgsl_driver.class = class_create(THIS_MODULE, KGSL_NAME);
2218
2219 if (IS_ERR(kgsl_driver.class)) {
2220 result = PTR_ERR(kgsl_driver.class);
2221 KGSL_CORE_ERR("failed to create class %s", KGSL_NAME);
2222 goto err;
2223 }
2224
2225 /* Make a virtual device for managing core related things
2226 in sysfs */
2227 kgsl_driver.virtdev.class = kgsl_driver.class;
2228 dev_set_name(&kgsl_driver.virtdev, "kgsl");
2229 result = device_register(&kgsl_driver.virtdev);
2230 if (result) {
2231 KGSL_CORE_ERR("driver_register failed\n");
2232 goto err;
2233 }
2234
2235 /* Make kobjects in the virtual device for storing statistics */
2236
2237 kgsl_driver.ptkobj =
2238 kobject_create_and_add("pagetables",
2239 &kgsl_driver.virtdev.kobj);
2240
2241 kgsl_driver.prockobj =
2242 kobject_create_and_add("proc",
2243 &kgsl_driver.virtdev.kobj);
2244
2245 kgsl_core_debugfs_init();
2246
2247 kgsl_sharedmem_init_sysfs();
2248 kgsl_cffdump_init();
2249
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250 INIT_LIST_HEAD(&kgsl_driver.process_list);
2251
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002252 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
2253
2254 kgsl_mmu_set_mmutype(ksgl_mmu_type);
2255
2256 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype()) {
2257 result = kgsl_ptdata_init();
2258 if (result)
2259 goto err;
2260 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261
2262 result = kgsl_drm_init(NULL);
2263
2264 if (result)
2265 goto err;
2266
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002267 kgsl_mmu_set_mmutype(ksgl_mmu_type);
2268
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002269 return 0;
2270
2271err:
2272 kgsl_core_exit();
2273 return result;
2274}
2275
2276module_init(kgsl_core_init);
2277module_exit(kgsl_core_exit);
2278
2279MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
2280MODULE_DESCRIPTION("MSM GPU driver");
2281MODULE_LICENSE("GPL");