blob: 37d18bc0cdf1b99a38dd45d19aab3cf566d19b02 [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
29#include "kgsl.h"
30#include "kgsl_debugfs.h"
31#include "kgsl_cffdump.h"
32#include "kgsl_log.h"
33#include "kgsl_sharedmem.h"
34#include "kgsl_device.h"
Norman Geed7402ff2011-10-28 08:51:11 -060035#include "kgsl_trace.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
37#undef MODULE_PARAM_PREFIX
38#define MODULE_PARAM_PREFIX "kgsl."
39
40static int kgsl_pagetable_count = KGSL_PAGETABLE_COUNT;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060041static char *ksgl_mmu_type;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042module_param_named(ptcount, kgsl_pagetable_count, int, 0);
43MODULE_PARM_DESC(kgsl_pagetable_count,
44"Minimum number of pagetables for KGSL to allocate at initialization time");
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060045module_param_named(mmutype, ksgl_mmu_type, charp, 0);
46MODULE_PARM_DESC(ksgl_mmu_type,
47"Type of MMU to be used for graphics. Valid values are 'iommu' or 'gpummu' or 'nommu'");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048
Jordan Crouse8eab35a2011-10-12 16:57:48 -060049static struct ion_client *kgsl_ion_client;
50
Jordan Croused4bc9d22011-11-17 13:39:21 -070051/**
52 * kgsl_add_event - Add a new timstamp event for the KGSL device
53 * @device - KGSL device for the new event
54 * @ts - the timestamp to trigger the event on
55 * @cb - callback function to call when the timestamp expires
56 * @priv - private data for the specific event type
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -070057 * @owner - driver instance that owns this event
Jordan Croused4bc9d22011-11-17 13:39:21 -070058 *
59 * @returns - 0 on success or error code on failure
60 */
61
Wei Zouc8c01632012-03-24 17:27:26 -070062static int kgsl_add_event(struct kgsl_device *device, u32 ts,
63 void (*cb)(struct kgsl_device *, void *, u32), void *priv,
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -070064 struct kgsl_device_private *owner)
Jordan Croused4bc9d22011-11-17 13:39:21 -070065{
66 struct kgsl_event *event;
67 struct list_head *n;
Wei Zouc8c01632012-03-24 17:27:26 -070068 unsigned int cur = device->ftbl->readtimestamp(device,
69 KGSL_TIMESTAMP_RETIRED);
Jordan Croused4bc9d22011-11-17 13:39:21 -070070
71 if (cb == NULL)
72 return -EINVAL;
73
74 /* Check to see if the requested timestamp has already fired */
75
Wei Zouc8c01632012-03-24 17:27:26 -070076 if (timestamp_cmp(cur, ts) >= 0) {
77 cb(device, priv, cur);
Jordan Croused4bc9d22011-11-17 13:39:21 -070078 return 0;
79 }
80
81 event = kzalloc(sizeof(*event), GFP_KERNEL);
82 if (event == NULL)
83 return -ENOMEM;
84
85 event->timestamp = ts;
86 event->priv = priv;
87 event->func = cb;
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -070088 event->owner = owner;
Jordan Croused4bc9d22011-11-17 13:39:21 -070089
Wei Zouc8c01632012-03-24 17:27:26 -070090 /* Add the event in order to the list */
Jordan Croused4bc9d22011-11-17 13:39:21 -070091
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}
Jordan Croused4bc9d22011-11-17 13:39:21 -0700108
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -0700109/**
110 * kgsl_cancel_events - Cancel all events for a process
111 * @device - KGSL device for the events to cancel
112 * @owner - driver instance that owns the events to cancel
113 *
114 */
115static void kgsl_cancel_events(struct kgsl_device *device,
116 struct kgsl_device_private *owner)
117{
118 struct kgsl_event *event, *event_tmp;
Wei Zouc8c01632012-03-24 17:27:26 -0700119 unsigned int cur = device->ftbl->readtimestamp(device,
120 KGSL_TIMESTAMP_RETIRED);
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -0700121
122 list_for_each_entry_safe(event, event_tmp, &device->events, list) {
123 if (event->owner != owner)
124 continue;
125 /*
126 * "cancel" the events by calling their callback.
127 * Currently, events are used for lock and memory
128 * management, so if the process is dying the right
129 * thing to do is release or free.
130 */
131 if (event->func)
Wei Zouc8c01632012-03-24 17:27:26 -0700132 event->func(device, event->priv, cur);
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -0700133
134 list_del(&event->list);
135 kfree(event);
136 }
137}
138
Jordan Crouse0fdf3a02012-03-16 14:53:41 -0600139/* kgsl_get_mem_entry - get the mem_entry structure for the specified object
140 * @ptbase - the pagetable base of the object
141 * @gpuaddr - the GPU address of the object
142 * @size - Size of the region to search
143 */
144
145struct kgsl_mem_entry *kgsl_get_mem_entry(unsigned int ptbase,
146 unsigned int gpuaddr, unsigned int size)
147{
148 struct kgsl_process_private *priv;
149 struct kgsl_mem_entry *entry;
150
151 mutex_lock(&kgsl_driver.process_mutex);
152
153 list_for_each_entry(priv, &kgsl_driver.process_list, list) {
154 if (!kgsl_mmu_pt_equal(priv->pagetable, ptbase))
155 continue;
156 spin_lock(&priv->mem_lock);
157 entry = kgsl_sharedmem_find_region(priv, gpuaddr, size);
158
159 if (entry) {
160 spin_unlock(&priv->mem_lock);
161 mutex_unlock(&kgsl_driver.process_mutex);
162 return entry;
163 }
164 spin_unlock(&priv->mem_lock);
165 }
166 mutex_unlock(&kgsl_driver.process_mutex);
167
168 return NULL;
169}
170EXPORT_SYMBOL(kgsl_get_mem_entry);
171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172static inline struct kgsl_mem_entry *
173kgsl_mem_entry_create(void)
174{
175 struct kgsl_mem_entry *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
176
177 if (!entry)
178 KGSL_CORE_ERR("kzalloc(%d) failed\n", sizeof(*entry));
179 else
180 kref_init(&entry->refcount);
181
182 return entry;
183}
184
185void
186kgsl_mem_entry_destroy(struct kref *kref)
187{
188 struct kgsl_mem_entry *entry = container_of(kref,
189 struct kgsl_mem_entry,
190 refcount);
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600191
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600192 if (entry->memtype != KGSL_MEM_ENTRY_KERNEL)
193 kgsl_driver.stats.mapped -= entry->memdesc.size;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600195 /*
196 * Ion takes care of freeing the sglist for us (how nice </sarcasm>) so
197 * unmap the dma before freeing the sharedmem so kgsl_sharedmem_free
198 * doesn't try to free it again
199 */
200
201 if (entry->memtype == KGSL_MEM_ENTRY_ION) {
202 ion_unmap_dma(kgsl_ion_client, entry->priv_data);
203 entry->memdesc.sg = NULL;
204 }
205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 kgsl_sharedmem_free(&entry->memdesc);
207
Jordan Crouse1b897cf2011-10-12 16:57:48 -0600208 switch (entry->memtype) {
209 case KGSL_MEM_ENTRY_PMEM:
210 case KGSL_MEM_ENTRY_ASHMEM:
211 if (entry->priv_data)
212 fput(entry->priv_data);
213 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -0600214 case KGSL_MEM_ENTRY_ION:
215 ion_free(kgsl_ion_client, entry->priv_data);
216 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 }
218
219 kfree(entry);
220}
221EXPORT_SYMBOL(kgsl_mem_entry_destroy);
222
223static
224void kgsl_mem_entry_attach_process(struct kgsl_mem_entry *entry,
225 struct kgsl_process_private *process)
226{
227 spin_lock(&process->mem_lock);
228 list_add(&entry->list, &process->mem_list);
229 spin_unlock(&process->mem_lock);
230
231 entry->priv = process;
232}
233
Jordan Crouse00714012012-03-16 14:53:40 -0600234/* Detach a memory entry from a process and unmap it from the MMU */
235
236static void kgsl_mem_entry_detach_process(struct kgsl_mem_entry *entry)
237{
238 if (entry == NULL)
239 return;
240
241 entry->priv->stats[entry->memtype].cur -= entry->memdesc.size;
242 entry->priv = NULL;
243
244 kgsl_mmu_unmap(entry->memdesc.pagetable, &entry->memdesc);
245
246 kgsl_mem_entry_put(entry);
247}
248
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249/* Allocate a new context id */
250
251static struct kgsl_context *
252kgsl_create_context(struct kgsl_device_private *dev_priv)
253{
254 struct kgsl_context *context;
255 int ret, id;
256
257 context = kzalloc(sizeof(*context), GFP_KERNEL);
258
259 if (context == NULL)
260 return NULL;
261
262 while (1) {
263 if (idr_pre_get(&dev_priv->device->context_idr,
264 GFP_KERNEL) == 0) {
265 kfree(context);
266 return NULL;
267 }
268
Wei Zouc8c01632012-03-24 17:27:26 -0700269 ret = idr_get_new(&dev_priv->device->context_idr,
270 context, &id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271
272 if (ret != -EAGAIN)
273 break;
274 }
275
276 if (ret) {
277 kfree(context);
278 return NULL;
279 }
280
281 context->id = id;
282 context->dev_priv = dev_priv;
283
284 return context;
285}
286
287static void
288kgsl_destroy_context(struct kgsl_device_private *dev_priv,
289 struct kgsl_context *context)
290{
291 int id;
292
293 if (context == NULL)
294 return;
295
296 /* Fire a bug if the devctxt hasn't been freed */
297 BUG_ON(context->devctxt);
298
299 id = context->id;
300 kfree(context);
301
302 idr_remove(&dev_priv->device->context_idr, id);
303}
304
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600305static void kgsl_timestamp_expired(struct work_struct *work)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306{
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600307 struct kgsl_device *device = container_of(work, struct kgsl_device,
308 ts_expired_ws);
Jordan Croused4bc9d22011-11-17 13:39:21 -0700309 struct kgsl_event *event, *event_tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310 uint32_t ts_processed;
311
Jordan Crouse1bf80aa2011-10-12 16:57:47 -0600312 mutex_lock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313
Wei Zouc8c01632012-03-24 17:27:26 -0700314 /* get current EOP timestamp */
315 ts_processed = device->ftbl->readtimestamp(device,
316 KGSL_TIMESTAMP_RETIRED);
317
Jordan Croused4bc9d22011-11-17 13:39:21 -0700318 /* Process expired events */
319 list_for_each_entry_safe(event, event_tmp, &device->events, list) {
320 if (timestamp_cmp(ts_processed, event->timestamp) < 0)
Wei Zouc8c01632012-03-24 17:27:26 -0700321 break;
Jordan Croused4bc9d22011-11-17 13:39:21 -0700322
323 if (event->func)
Wei Zouc8c01632012-03-24 17:27:26 -0700324 event->func(device, event->priv, ts_processed);
Jordan Croused4bc9d22011-11-17 13:39:21 -0700325
326 list_del(&event->list);
327 kfree(event);
328 }
329
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700330 mutex_unlock(&device->mutex);
331}
332
333static void kgsl_check_idle_locked(struct kgsl_device *device)
334{
335 if (device->pwrctrl.nap_allowed == true &&
336 device->state == KGSL_STATE_ACTIVE &&
337 device->requested_state == KGSL_STATE_NONE) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700338 kgsl_pwrctrl_request_state(device, KGSL_STATE_NAP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 if (kgsl_pwrctrl_sleep(device) != 0)
340 mod_timer(&device->idle_timer,
341 jiffies +
342 device->pwrctrl.interval_timeout);
343 }
344}
345
346static void kgsl_check_idle(struct kgsl_device *device)
347{
348 mutex_lock(&device->mutex);
349 kgsl_check_idle_locked(device);
350 mutex_unlock(&device->mutex);
351}
352
353struct kgsl_device *kgsl_get_device(int dev_idx)
354{
355 int i;
356 struct kgsl_device *ret = NULL;
357
358 mutex_lock(&kgsl_driver.devlock);
359
360 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
361 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->id == dev_idx) {
362 ret = kgsl_driver.devp[i];
363 break;
364 }
365 }
366
367 mutex_unlock(&kgsl_driver.devlock);
368 return ret;
369}
370EXPORT_SYMBOL(kgsl_get_device);
371
372static struct kgsl_device *kgsl_get_minor(int minor)
373{
374 struct kgsl_device *ret = NULL;
375
376 if (minor < 0 || minor >= KGSL_DEVICE_MAX)
377 return NULL;
378
379 mutex_lock(&kgsl_driver.devlock);
380 ret = kgsl_driver.devp[minor];
381 mutex_unlock(&kgsl_driver.devlock);
382
383 return ret;
384}
385
386int kgsl_register_ts_notifier(struct kgsl_device *device,
387 struct notifier_block *nb)
388{
389 BUG_ON(device == NULL);
390 return atomic_notifier_chain_register(&device->ts_notifier_list,
391 nb);
392}
393EXPORT_SYMBOL(kgsl_register_ts_notifier);
394
395int kgsl_unregister_ts_notifier(struct kgsl_device *device,
396 struct notifier_block *nb)
397{
398 BUG_ON(device == NULL);
399 return atomic_notifier_chain_unregister(&device->ts_notifier_list,
400 nb);
401}
402EXPORT_SYMBOL(kgsl_unregister_ts_notifier);
403
Wei Zouc8c01632012-03-24 17:27:26 -0700404int kgsl_check_timestamp(struct kgsl_device *device, unsigned int timestamp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405{
406 unsigned int ts_processed;
407
Wei Zouc8c01632012-03-24 17:27:26 -0700408 ts_processed = device->ftbl->readtimestamp(device,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409 KGSL_TIMESTAMP_RETIRED);
410
Jordan Crousee6239dd2011-11-17 13:39:21 -0700411 return (timestamp_cmp(ts_processed, timestamp) >= 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700412}
413EXPORT_SYMBOL(kgsl_check_timestamp);
414
415static int kgsl_suspend_device(struct kgsl_device *device, pm_message_t state)
416{
417 int status = -EINVAL;
418 unsigned int nap_allowed_saved;
419 struct kgsl_pwrscale_policy *policy_saved;
420
421 if (!device)
422 return -EINVAL;
423
424 KGSL_PWR_WARN(device, "suspend start\n");
425
426 mutex_lock(&device->mutex);
427 nap_allowed_saved = device->pwrctrl.nap_allowed;
428 device->pwrctrl.nap_allowed = false;
429 policy_saved = device->pwrscale.policy;
430 device->pwrscale.policy = NULL;
Jeremy Gebben388c2972011-12-16 09:05:07 -0700431 kgsl_pwrctrl_request_state(device, KGSL_STATE_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700432 /* Make sure no user process is waiting for a timestamp *
433 * before supending */
434 if (device->active_cnt != 0) {
435 mutex_unlock(&device->mutex);
436 wait_for_completion(&device->suspend_gate);
437 mutex_lock(&device->mutex);
438 }
Suman Tatiraju4a32c652012-02-17 11:59:05 -0800439 /* Don't let the timer wake us during suspended sleep. */
440 del_timer_sync(&device->idle_timer);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700441 switch (device->state) {
442 case KGSL_STATE_INIT:
443 break;
444 case KGSL_STATE_ACTIVE:
445 /* Wait for the device to become idle */
446 device->ftbl->idle(device, KGSL_TIMEOUT_DEFAULT);
447 case KGSL_STATE_NAP:
448 case KGSL_STATE_SLEEP:
449 /* Get the completion ready to be waited upon. */
450 INIT_COMPLETION(device->hwaccess_gate);
451 device->ftbl->suspend_context(device);
452 device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700453 kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 break;
Suman Tatiraju24569022011-10-27 11:11:12 -0700455 case KGSL_STATE_SLUMBER:
456 INIT_COMPLETION(device->hwaccess_gate);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700457 kgsl_pwrctrl_set_state(device, KGSL_STATE_SUSPEND);
Suman Tatiraju24569022011-10-27 11:11:12 -0700458 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 default:
460 KGSL_PWR_ERR(device, "suspend fail, device %d\n",
461 device->id);
462 goto end;
463 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700464 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465 device->pwrctrl.nap_allowed = nap_allowed_saved;
466 device->pwrscale.policy = policy_saved;
467 status = 0;
468
469end:
470 mutex_unlock(&device->mutex);
471 KGSL_PWR_WARN(device, "suspend end\n");
472 return status;
473}
474
475static int kgsl_resume_device(struct kgsl_device *device)
476{
477 int status = -EINVAL;
478
479 if (!device)
480 return -EINVAL;
481
482 KGSL_PWR_WARN(device, "resume start\n");
483 mutex_lock(&device->mutex);
484 if (device->state == KGSL_STATE_SUSPEND) {
Jeremy Gebben388c2972011-12-16 09:05:07 -0700485 kgsl_pwrctrl_set_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju24569022011-10-27 11:11:12 -0700486 status = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 complete_all(&device->hwaccess_gate);
488 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700489 kgsl_pwrctrl_request_state(device, KGSL_STATE_NONE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 mutex_unlock(&device->mutex);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 KGSL_PWR_WARN(device, "resume end\n");
493 return status;
494}
495
496static int kgsl_suspend(struct device *dev)
497{
498
499 pm_message_t arg = {0};
500 struct kgsl_device *device = dev_get_drvdata(dev);
501 return kgsl_suspend_device(device, arg);
502}
503
504static int kgsl_resume(struct device *dev)
505{
506 struct kgsl_device *device = dev_get_drvdata(dev);
507 return kgsl_resume_device(device);
508}
509
510static int kgsl_runtime_suspend(struct device *dev)
511{
512 return 0;
513}
514
515static int kgsl_runtime_resume(struct device *dev)
516{
517 return 0;
518}
519
520const struct dev_pm_ops kgsl_pm_ops = {
521 .suspend = kgsl_suspend,
522 .resume = kgsl_resume,
523 .runtime_suspend = kgsl_runtime_suspend,
524 .runtime_resume = kgsl_runtime_resume,
525};
526EXPORT_SYMBOL(kgsl_pm_ops);
527
528void kgsl_early_suspend_driver(struct early_suspend *h)
529{
530 struct kgsl_device *device = container_of(h,
531 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700532 KGSL_PWR_WARN(device, "early suspend start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530533 mutex_lock(&device->mutex);
Lucille Sylvester344e4622012-01-18 15:53:21 -0700534 kgsl_pwrctrl_request_state(device, KGSL_STATE_SLUMBER);
Suman Tatiraju24569022011-10-27 11:11:12 -0700535 kgsl_pwrctrl_sleep(device);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530536 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700537 KGSL_PWR_WARN(device, "early suspend end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538}
539EXPORT_SYMBOL(kgsl_early_suspend_driver);
540
541int kgsl_suspend_driver(struct platform_device *pdev,
542 pm_message_t state)
543{
544 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
545 return kgsl_suspend_device(device, state);
546}
547EXPORT_SYMBOL(kgsl_suspend_driver);
548
549int kgsl_resume_driver(struct platform_device *pdev)
550{
551 struct kgsl_device *device = dev_get_drvdata(&pdev->dev);
552 return kgsl_resume_device(device);
553}
554EXPORT_SYMBOL(kgsl_resume_driver);
555
556void kgsl_late_resume_driver(struct early_suspend *h)
557{
558 struct kgsl_device *device = container_of(h,
559 struct kgsl_device, display_off);
Suman Tatiraju24569022011-10-27 11:11:12 -0700560 KGSL_PWR_WARN(device, "late resume start\n");
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530561 mutex_lock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700562 device->pwrctrl.restore_slumber = 0;
Suman Tatiraju3005cdd2012-03-19 14:38:11 -0700563 kgsl_pwrctrl_wake(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700564 kgsl_pwrctrl_pwrlevel_change(device, KGSL_PWRLEVEL_TURBO);
Ranjhith Kalisamy8b636952011-09-03 14:48:31 +0530565 mutex_unlock(&device->mutex);
Suman Tatiraju24569022011-10-27 11:11:12 -0700566 kgsl_check_idle(device);
567 KGSL_PWR_WARN(device, "late resume end\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568}
569EXPORT_SYMBOL(kgsl_late_resume_driver);
570
571/* file operations */
572static struct kgsl_process_private *
573kgsl_get_process_private(struct kgsl_device_private *cur_dev_priv)
574{
575 struct kgsl_process_private *private;
576
577 mutex_lock(&kgsl_driver.process_mutex);
578 list_for_each_entry(private, &kgsl_driver.process_list, list) {
579 if (private->pid == task_tgid_nr(current)) {
580 private->refcnt++;
581 goto out;
582 }
583 }
584
585 /* no existing process private found for this dev_priv, create one */
586 private = kzalloc(sizeof(struct kgsl_process_private), GFP_KERNEL);
587 if (private == NULL) {
588 KGSL_DRV_ERR(cur_dev_priv->device, "kzalloc(%d) failed\n",
589 sizeof(struct kgsl_process_private));
590 goto out;
591 }
592
593 spin_lock_init(&private->mem_lock);
594 private->refcnt = 1;
595 private->pid = task_tgid_nr(current);
596
597 INIT_LIST_HEAD(&private->mem_list);
598
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600599 if (kgsl_mmu_enabled())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 {
601 unsigned long pt_name;
602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 pt_name = task_tgid_nr(current);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 private->pagetable = kgsl_mmu_getpagetable(pt_name);
605 if (private->pagetable == NULL) {
606 kfree(private);
607 private = NULL;
608 goto out;
609 }
610 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700611
612 list_add(&private->list, &kgsl_driver.process_list);
613
614 kgsl_process_init_sysfs(private);
615
616out:
617 mutex_unlock(&kgsl_driver.process_mutex);
618 return private;
619}
620
621static void
622kgsl_put_process_private(struct kgsl_device *device,
623 struct kgsl_process_private *private)
624{
625 struct kgsl_mem_entry *entry = NULL;
626 struct kgsl_mem_entry *entry_tmp = NULL;
627
628 if (!private)
629 return;
630
631 mutex_lock(&kgsl_driver.process_mutex);
632
633 if (--private->refcnt)
634 goto unlock;
635
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 kgsl_process_uninit_sysfs(private);
637
638 list_del(&private->list);
639
640 list_for_each_entry_safe(entry, entry_tmp, &private->mem_list, list) {
641 list_del(&entry->list);
Jordan Crouse00714012012-03-16 14:53:40 -0600642 kgsl_mem_entry_detach_process(entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 }
644
645 kgsl_mmu_putpagetable(private->pagetable);
646 kfree(private);
647unlock:
648 mutex_unlock(&kgsl_driver.process_mutex);
649}
650
651static int kgsl_release(struct inode *inodep, struct file *filep)
652{
653 int result = 0;
Jordan Crouse2db0af92011-08-08 16:05:09 -0600654 struct kgsl_device_private *dev_priv = filep->private_data;
655 struct kgsl_process_private *private = dev_priv->process_priv;
656 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 struct kgsl_context *context;
658 int next = 0;
659
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 filep->private_data = NULL;
661
662 mutex_lock(&device->mutex);
663 kgsl_check_suspended(device);
664
665 while (1) {
Jordan Crouse2db0af92011-08-08 16:05:09 -0600666 context = idr_get_next(&device->context_idr, &next);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 if (context == NULL)
668 break;
669
670 if (context->dev_priv == dev_priv) {
671 device->ftbl->drawctxt_destroy(device, context);
672 kgsl_destroy_context(dev_priv, context);
673 }
674
675 next = next + 1;
676 }
677
678 device->open_count--;
679 if (device->open_count == 0) {
680 result = device->ftbl->stop(device);
Jeremy Gebben388c2972011-12-16 09:05:07 -0700681 kgsl_pwrctrl_set_state(device, KGSL_STATE_INIT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700682 }
683 /* clean up any to-be-freed entries that belong to this
684 * process and this device
685 */
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -0700686 kgsl_cancel_events(device, dev_priv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687
688 mutex_unlock(&device->mutex);
689 kfree(dev_priv);
690
691 kgsl_put_process_private(device, private);
692
693 pm_runtime_put(device->parentdev);
694 return result;
695}
696
697static int kgsl_open(struct inode *inodep, struct file *filep)
698{
699 int result;
700 struct kgsl_device_private *dev_priv;
701 struct kgsl_device *device;
702 unsigned int minor = iminor(inodep);
703
704 device = kgsl_get_minor(minor);
705 BUG_ON(device == NULL);
706
707 if (filep->f_flags & O_EXCL) {
708 KGSL_DRV_ERR(device, "O_EXCL not allowed\n");
709 return -EBUSY;
710 }
711
712 result = pm_runtime_get_sync(device->parentdev);
713 if (result < 0) {
714 KGSL_DRV_ERR(device,
715 "Runtime PM: Unable to wake up the device, rc = %d\n",
716 result);
717 return result;
718 }
719 result = 0;
720
721 dev_priv = kzalloc(sizeof(struct kgsl_device_private), GFP_KERNEL);
722 if (dev_priv == NULL) {
723 KGSL_DRV_ERR(device, "kzalloc failed(%d)\n",
724 sizeof(struct kgsl_device_private));
725 result = -ENOMEM;
726 goto err_pmruntime;
727 }
728
729 dev_priv->device = device;
730 filep->private_data = dev_priv;
731
732 /* Get file (per process) private struct */
733 dev_priv->process_priv = kgsl_get_process_private(dev_priv);
734 if (dev_priv->process_priv == NULL) {
735 result = -ENOMEM;
736 goto err_freedevpriv;
737 }
738
739 mutex_lock(&device->mutex);
740 kgsl_check_suspended(device);
741
742 if (device->open_count == 0) {
743 result = device->ftbl->start(device, true);
744
745 if (result) {
746 mutex_unlock(&device->mutex);
747 goto err_putprocess;
748 }
Jeremy Gebben388c2972011-12-16 09:05:07 -0700749 kgsl_pwrctrl_set_state(device, KGSL_STATE_ACTIVE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700750 }
751 device->open_count++;
752 mutex_unlock(&device->mutex);
753
754 KGSL_DRV_INFO(device, "Initialized %s: mmu=%s pagetable_count=%d\n",
755 device->name, kgsl_mmu_enabled() ? "on" : "off",
756 kgsl_pagetable_count);
757
758 return result;
759
760err_putprocess:
761 kgsl_put_process_private(device, dev_priv->process_priv);
762err_freedevpriv:
763 filep->private_data = NULL;
764 kfree(dev_priv);
765err_pmruntime:
766 pm_runtime_put(device->parentdev);
767 return result;
768}
769
770
771/*call with private->mem_lock locked */
772static struct kgsl_mem_entry *
773kgsl_sharedmem_find(struct kgsl_process_private *private, unsigned int gpuaddr)
774{
775 struct kgsl_mem_entry *entry = NULL, *result = NULL;
776
777 BUG_ON(private == NULL);
778
779 gpuaddr &= PAGE_MASK;
780
781 list_for_each_entry(entry, &private->mem_list, list) {
782 if (entry->memdesc.gpuaddr == gpuaddr) {
783 result = entry;
784 break;
785 }
786 }
787 return result;
788}
789
790/*call with private->mem_lock locked */
791struct kgsl_mem_entry *
792kgsl_sharedmem_find_region(struct kgsl_process_private *private,
793 unsigned int gpuaddr,
794 size_t size)
795{
796 struct kgsl_mem_entry *entry = NULL, *result = NULL;
797
798 BUG_ON(private == NULL);
799
800 list_for_each_entry(entry, &private->mem_list, list) {
Jeremy Gebben16e80fa2011-11-30 15:56:29 -0700801 if (kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802 result = entry;
803 break;
804 }
805 }
806
807 return result;
808}
809EXPORT_SYMBOL(kgsl_sharedmem_find_region);
810
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811/*call all ioctl sub functions with driver locked*/
812static long kgsl_ioctl_device_getproperty(struct kgsl_device_private *dev_priv,
813 unsigned int cmd, void *data)
814{
815 int result = 0;
816 struct kgsl_device_getproperty *param = data;
817
818 switch (param->type) {
819 case KGSL_PROP_VERSION:
820 {
821 struct kgsl_version version;
822 if (param->sizebytes != sizeof(version)) {
823 result = -EINVAL;
824 break;
825 }
826
827 version.drv_major = KGSL_VERSION_MAJOR;
828 version.drv_minor = KGSL_VERSION_MINOR;
829 version.dev_major = dev_priv->device->ver_major;
830 version.dev_minor = dev_priv->device->ver_minor;
831
832 if (copy_to_user(param->value, &version, sizeof(version)))
833 result = -EFAULT;
834
835 break;
836 }
Shubhraprakash Das2dfe5dd2012-02-10 13:49:53 -0700837 case KGSL_PROP_GPU_RESET_STAT:
838 {
839 /* Return reset status of given context and clear it */
840 uint32_t id;
841 struct kgsl_context *context;
842
843 if (param->sizebytes != sizeof(unsigned int)) {
844 result = -EINVAL;
845 break;
846 }
847 /* We expect the value passed in to contain the context id */
848 if (copy_from_user(&id, param->value,
849 sizeof(unsigned int))) {
850 result = -EFAULT;
851 break;
852 }
853 context = kgsl_find_context(dev_priv, id);
854 if (!context) {
855 result = -EINVAL;
856 break;
857 }
858 /*
859 * Copy the reset status to value which also serves as
860 * the out parameter
861 */
862 if (copy_to_user(param->value, &(context->reset_status),
863 sizeof(unsigned int))) {
864 result = -EFAULT;
865 break;
866 }
867 /* Clear reset status once its been queried */
868 context->reset_status = KGSL_CTX_STAT_NO_ERROR;
869 break;
870 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 default:
872 result = dev_priv->device->ftbl->getproperty(
873 dev_priv->device, param->type,
874 param->value, param->sizebytes);
875 }
876
877
878 return result;
879}
880
Wei Zouc8c01632012-03-24 17:27:26 -0700881static long kgsl_ioctl_device_waittimestamp(struct kgsl_device_private
882 *dev_priv, unsigned int cmd,
883 void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884{
885 int result = 0;
Wei Zouc8c01632012-03-24 17:27:26 -0700886 struct kgsl_device_waittimestamp *param = data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
Wei Zouc8c01632012-03-24 17:27:26 -0700888 /* Set the active count so that suspend doesn't do the
889 wrong thing */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
891 dev_priv->device->active_cnt++;
892
Wei Zouc8c01632012-03-24 17:27:26 -0700893 trace_kgsl_waittimestamp_entry(dev_priv->device, param);
Norman Geed7402ff2011-10-28 08:51:11 -0600894
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 result = dev_priv->device->ftbl->waittimestamp(dev_priv->device,
Wei Zouc8c01632012-03-24 17:27:26 -0700896 param->timestamp,
897 param->timeout);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700898
Norman Geed7402ff2011-10-28 08:51:11 -0600899 trace_kgsl_waittimestamp_exit(dev_priv->device, result);
900
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 /* Fire off any pending suspend operations that are in flight */
902
903 INIT_COMPLETION(dev_priv->device->suspend_gate);
904 dev_priv->device->active_cnt--;
905 complete(&dev_priv->device->suspend_gate);
906
907 return result;
908}
909static bool check_ibdesc(struct kgsl_device_private *dev_priv,
910 struct kgsl_ibdesc *ibdesc, unsigned int numibs,
911 bool parse)
912{
913 bool result = true;
914 unsigned int i;
915 for (i = 0; i < numibs; i++) {
916 struct kgsl_mem_entry *entry;
917 spin_lock(&dev_priv->process_priv->mem_lock);
918 entry = kgsl_sharedmem_find_region(dev_priv->process_priv,
919 ibdesc[i].gpuaddr, ibdesc[i].sizedwords * sizeof(uint));
920 spin_unlock(&dev_priv->process_priv->mem_lock);
921 if (entry == NULL) {
922 KGSL_DRV_ERR(dev_priv->device,
923 "invalid cmd buffer gpuaddr %08x " \
924 "sizedwords %d\n", ibdesc[i].gpuaddr,
925 ibdesc[i].sizedwords);
926 result = false;
927 break;
928 }
929
930 if (parse && !kgsl_cffdump_parse_ibs(dev_priv, &entry->memdesc,
931 ibdesc[i].gpuaddr, ibdesc[i].sizedwords, true)) {
932 KGSL_DRV_ERR(dev_priv->device,
933 "invalid cmd buffer gpuaddr %08x " \
934 "sizedwords %d numibs %d/%d\n",
935 ibdesc[i].gpuaddr,
936 ibdesc[i].sizedwords, i+1, numibs);
937 result = false;
938 break;
939 }
940 }
941 return result;
942}
943
944static long kgsl_ioctl_rb_issueibcmds(struct kgsl_device_private *dev_priv,
945 unsigned int cmd, void *data)
946{
947 int result = 0;
948 struct kgsl_ringbuffer_issueibcmds *param = data;
949 struct kgsl_ibdesc *ibdesc;
950 struct kgsl_context *context;
951
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 context = kgsl_find_context(dev_priv, param->drawctxt_id);
953 if (context == NULL) {
954 result = -EINVAL;
955 KGSL_DRV_ERR(dev_priv->device,
Wei Zouc8c01632012-03-24 17:27:26 -0700956 "invalid drawctxt drawctxt_id %d\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 param->drawctxt_id);
958 goto done;
959 }
960
961 if (param->flags & KGSL_CONTEXT_SUBMIT_IB_LIST) {
962 KGSL_DRV_INFO(dev_priv->device,
963 "Using IB list mode for ib submission, numibs: %d\n",
964 param->numibs);
965 if (!param->numibs) {
966 KGSL_DRV_ERR(dev_priv->device,
967 "Invalid numibs as parameter: %d\n",
968 param->numibs);
969 result = -EINVAL;
970 goto done;
971 }
972
973 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc) * param->numibs,
974 GFP_KERNEL);
975 if (!ibdesc) {
976 KGSL_MEM_ERR(dev_priv->device,
977 "kzalloc(%d) failed\n",
978 sizeof(struct kgsl_ibdesc) * param->numibs);
979 result = -ENOMEM;
980 goto done;
981 }
982
983 if (copy_from_user(ibdesc, (void *)param->ibdesc_addr,
984 sizeof(struct kgsl_ibdesc) * param->numibs)) {
985 result = -EFAULT;
986 KGSL_DRV_ERR(dev_priv->device,
987 "copy_from_user failed\n");
988 goto free_ibdesc;
989 }
990 } else {
991 KGSL_DRV_INFO(dev_priv->device,
992 "Using single IB submission mode for ib submission\n");
993 /* If user space driver is still using the old mode of
994 * submitting single ib then we need to support that as well */
995 ibdesc = kzalloc(sizeof(struct kgsl_ibdesc), GFP_KERNEL);
996 if (!ibdesc) {
997 KGSL_MEM_ERR(dev_priv->device,
998 "kzalloc(%d) failed\n",
999 sizeof(struct kgsl_ibdesc));
1000 result = -ENOMEM;
1001 goto done;
1002 }
1003 ibdesc[0].gpuaddr = param->ibdesc_addr;
1004 ibdesc[0].sizedwords = param->numibs;
1005 param->numibs = 1;
1006 }
1007
1008 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, true)) {
1009 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc");
1010 result = -EINVAL;
1011 goto free_ibdesc;
1012 }
1013
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 result = dev_priv->device->ftbl->issueibcmds(dev_priv,
1015 context,
1016 ibdesc,
1017 param->numibs,
1018 &param->timestamp,
1019 param->flags);
1020
Norman Geed7402ff2011-10-28 08:51:11 -06001021 trace_kgsl_issueibcmds(dev_priv->device, param, result);
Wei Zouc8c01632012-03-24 17:27:26 -07001022
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 if (result != 0)
1024 goto free_ibdesc;
1025
1026 /* this is a check to try to detect if a command buffer was freed
1027 * during issueibcmds().
1028 */
1029 if (!check_ibdesc(dev_priv, ibdesc, param->numibs, false)) {
1030 KGSL_DRV_ERR(dev_priv->device, "bad ibdesc AFTER issue");
1031 result = -EINVAL;
1032 goto free_ibdesc;
1033 }
1034
1035free_ibdesc:
1036 kfree(ibdesc);
1037done:
1038
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 return result;
1040}
1041
1042static long kgsl_ioctl_cmdstream_readtimestamp(struct kgsl_device_private
1043 *dev_priv, unsigned int cmd,
1044 void *data)
1045{
1046 struct kgsl_cmdstream_readtimestamp *param = data;
1047
Wei Zouc8c01632012-03-24 17:27:26 -07001048 param->timestamp =
1049 dev_priv->device->ftbl->readtimestamp(dev_priv->device,
1050 param->type);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051
Wei Zouc8c01632012-03-24 17:27:26 -07001052 trace_kgsl_readtimestamp(dev_priv->device, param);
Norman Geed7402ff2011-10-28 08:51:11 -06001053
Wei Zouc8c01632012-03-24 17:27:26 -07001054 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001055}
1056
Jeremy Gebbenc81a3c62012-02-07 16:10:23 -07001057static void kgsl_freemem_event_cb(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001058 void *priv, u32 timestamp)
Jeremy Gebbenc81a3c62012-02-07 16:10:23 -07001059{
1060 struct kgsl_mem_entry *entry = priv;
1061 spin_lock(&entry->priv->mem_lock);
1062 list_del(&entry->list);
1063 spin_unlock(&entry->priv->mem_lock);
Wei Zouc8c01632012-03-24 17:27:26 -07001064 trace_kgsl_mem_timestamp_free(entry, timestamp);
Jordan Crouse00714012012-03-16 14:53:40 -06001065 kgsl_mem_entry_detach_process(entry);
Jeremy Gebbenc81a3c62012-02-07 16:10:23 -07001066}
1067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068static long kgsl_ioctl_cmdstream_freememontimestamp(struct kgsl_device_private
1069 *dev_priv, unsigned int cmd,
1070 void *data)
1071{
Wei Zouc8c01632012-03-24 17:27:26 -07001072 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 struct kgsl_cmdstream_freememontimestamp *param = data;
Wei Zouc8c01632012-03-24 17:27:26 -07001074 struct kgsl_mem_entry *entry = NULL;
1075 struct kgsl_device *device = dev_priv->device;
1076 unsigned int cur;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077
Wei Zouc8c01632012-03-24 17:27:26 -07001078 spin_lock(&dev_priv->process_priv->mem_lock);
1079 entry = kgsl_sharedmem_find(dev_priv->process_priv, param->gpuaddr);
1080 spin_unlock(&dev_priv->process_priv->mem_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081
Wei Zouc8c01632012-03-24 17:27:26 -07001082 if (entry) {
1083 cur = device->ftbl->readtimestamp(device,
1084 KGSL_TIMESTAMP_RETIRED);
Jeremy Gebbena5859272012-03-01 12:46:28 -07001085
Wei Zouc8c01632012-03-24 17:27:26 -07001086 trace_kgsl_mem_timestamp_queue(entry, cur);
1087 result = kgsl_add_event(dev_priv->device, param->timestamp,
1088 kgsl_freemem_event_cb, entry, dev_priv);
1089 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 KGSL_DRV_ERR(dev_priv->device,
Wei Zouc8c01632012-03-24 17:27:26 -07001091 "invalid gpuaddr %08x\n", param->gpuaddr);
1092 result = -EINVAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001093 }
1094
Wei Zouc8c01632012-03-24 17:27:26 -07001095 return result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096}
1097
1098static long kgsl_ioctl_drawctxt_create(struct kgsl_device_private *dev_priv,
1099 unsigned int cmd, void *data)
1100{
1101 int result = 0;
1102 struct kgsl_drawctxt_create *param = data;
1103 struct kgsl_context *context = NULL;
1104
1105 context = kgsl_create_context(dev_priv);
1106
1107 if (context == NULL) {
1108 result = -ENOMEM;
1109 goto done;
1110 }
1111
1112 if (dev_priv->device->ftbl->drawctxt_create)
1113 result = dev_priv->device->ftbl->drawctxt_create(
1114 dev_priv->device, dev_priv->process_priv->pagetable,
1115 context, param->flags);
1116
1117 param->drawctxt_id = context->id;
Wei Zouc8c01632012-03-24 17:27:26 -07001118
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119done:
1120 if (result && context)
1121 kgsl_destroy_context(dev_priv, context);
1122
1123 return result;
1124}
1125
1126static long kgsl_ioctl_drawctxt_destroy(struct kgsl_device_private *dev_priv,
1127 unsigned int cmd, void *data)
1128{
1129 int result = 0;
1130 struct kgsl_drawctxt_destroy *param = data;
1131 struct kgsl_context *context;
1132
1133 context = kgsl_find_context(dev_priv, param->drawctxt_id);
1134
1135 if (context == NULL) {
1136 result = -EINVAL;
1137 goto done;
1138 }
1139
1140 if (dev_priv->device->ftbl->drawctxt_destroy)
1141 dev_priv->device->ftbl->drawctxt_destroy(dev_priv->device,
1142 context);
1143
1144 kgsl_destroy_context(dev_priv, context);
1145
1146done:
1147 return result;
1148}
1149
1150static long kgsl_ioctl_sharedmem_free(struct kgsl_device_private *dev_priv,
1151 unsigned int cmd, void *data)
1152{
1153 int result = 0;
1154 struct kgsl_sharedmem_free *param = data;
1155 struct kgsl_process_private *private = dev_priv->process_priv;
1156 struct kgsl_mem_entry *entry = NULL;
1157
1158 spin_lock(&private->mem_lock);
1159 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1160 if (entry)
1161 list_del(&entry->list);
1162 spin_unlock(&private->mem_lock);
1163
1164 if (entry) {
Jeremy Gebbena5859272012-03-01 12:46:28 -07001165 trace_kgsl_mem_free(entry);
Jordan Crouse00714012012-03-16 14:53:40 -06001166 kgsl_mem_entry_detach_process(entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 } else {
1168 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1169 result = -EINVAL;
1170 }
1171
1172 return result;
1173}
1174
1175static struct vm_area_struct *kgsl_get_vma_from_start_addr(unsigned int addr)
1176{
1177 struct vm_area_struct *vma;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178
1179 down_read(&current->mm->mmap_sem);
1180 vma = find_vma(current->mm, addr);
1181 up_read(&current->mm->mmap_sem);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001182 if (!vma)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 KGSL_CORE_ERR("find_vma(%x) failed\n", addr);
Jordan Crouse2c542b62011-07-26 08:30:20 -06001184
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001185 return vma;
1186}
1187
1188static long
1189kgsl_ioctl_sharedmem_from_vmalloc(struct kgsl_device_private *dev_priv,
1190 unsigned int cmd, void *data)
1191{
1192 int result = 0, len = 0;
1193 struct kgsl_process_private *private = dev_priv->process_priv;
1194 struct kgsl_sharedmem_from_vmalloc *param = data;
1195 struct kgsl_mem_entry *entry = NULL;
1196 struct vm_area_struct *vma;
1197
1198 if (!kgsl_mmu_enabled())
1199 return -ENODEV;
1200
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 if (!param->hostptr) {
1202 KGSL_CORE_ERR("invalid hostptr %x\n", param->hostptr);
1203 result = -EINVAL;
1204 goto error;
1205 }
1206
1207 vma = kgsl_get_vma_from_start_addr(param->hostptr);
1208 if (!vma) {
1209 result = -EINVAL;
1210 goto error;
1211 }
Jordan Crouse2c542b62011-07-26 08:30:20 -06001212
1213 /*
1214 * If the user specified a length, use it, otherwise try to
1215 * infer the length if the vma region
1216 */
1217 if (param->gpuaddr != 0) {
1218 len = param->gpuaddr;
1219 } else {
1220 /*
1221 * For this to work, we have to assume the VMA region is only
1222 * for this single allocation. If it isn't, then bail out
1223 */
1224 if (vma->vm_pgoff || (param->hostptr != vma->vm_start)) {
1225 KGSL_CORE_ERR("VMA region does not match hostaddr\n");
1226 result = -EINVAL;
1227 goto error;
1228 }
1229
1230 len = vma->vm_end - vma->vm_start;
1231 }
1232
1233 /* Make sure it fits */
1234 if (len == 0 || param->hostptr + len > vma->vm_end) {
1235 KGSL_CORE_ERR("Invalid memory allocation length %d\n", len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236 result = -EINVAL;
1237 goto error;
1238 }
1239
1240 entry = kgsl_mem_entry_create();
1241 if (entry == NULL) {
1242 result = -ENOMEM;
1243 goto error;
1244 }
1245
1246 result = kgsl_sharedmem_vmalloc_user(&entry->memdesc,
1247 private->pagetable, len,
1248 param->flags);
1249 if (result != 0)
1250 goto error_free_entry;
1251
1252 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1253
1254 result = remap_vmalloc_range(vma, (void *) entry->memdesc.hostptr, 0);
1255 if (result) {
1256 KGSL_CORE_ERR("remap_vmalloc_range failed: %d\n", result);
1257 goto error_free_vmalloc;
1258 }
1259
1260 param->gpuaddr = entry->memdesc.gpuaddr;
1261
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001262 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263
1264 kgsl_mem_entry_attach_process(entry, private);
1265
Jeremy Gebbena5859272012-03-01 12:46:28 -07001266 trace_kgsl_mem_alloc(entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001267 /* Process specific statistics */
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001268 kgsl_process_add_stats(private, entry->memtype, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269
1270 kgsl_check_idle(dev_priv->device);
1271 return 0;
1272
1273error_free_vmalloc:
1274 kgsl_sharedmem_free(&entry->memdesc);
1275
1276error_free_entry:
1277 kfree(entry);
1278
1279error:
1280 kgsl_check_idle(dev_priv->device);
1281 return result;
1282}
1283
1284static inline int _check_region(unsigned long start, unsigned long size,
1285 uint64_t len)
1286{
1287 uint64_t end = ((uint64_t) start) + size;
1288 return (end > len);
1289}
1290
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291static int kgsl_get_phys_file(int fd, unsigned long *start, unsigned long *len,
1292 unsigned long *vstart, struct file **filep)
1293{
1294 struct file *fbfile;
1295 int ret = 0;
1296 dev_t rdev;
1297 struct fb_info *info;
1298
1299 *filep = NULL;
Jordan Crousefd978432011-09-02 14:34:32 -06001300#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 if (!get_pmem_file(fd, start, vstart, len, filep))
1302 return 0;
Jordan Crousefd978432011-09-02 14:34:32 -06001303#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304
1305 fbfile = fget(fd);
1306 if (fbfile == NULL) {
1307 KGSL_CORE_ERR("fget_light failed\n");
1308 return -1;
1309 }
1310
1311 rdev = fbfile->f_dentry->d_inode->i_rdev;
1312 info = MAJOR(rdev) == FB_MAJOR ? registered_fb[MINOR(rdev)] : NULL;
1313 if (info) {
1314 *start = info->fix.smem_start;
1315 *len = info->fix.smem_len;
1316 *vstart = (unsigned long)__va(info->fix.smem_start);
1317 ret = 0;
1318 } else {
1319 KGSL_CORE_ERR("framebuffer minor %d not found\n",
1320 MINOR(rdev));
1321 ret = -1;
1322 }
1323
1324 fput(fbfile);
1325
1326 return ret;
1327}
1328
1329static int kgsl_setup_phys_file(struct kgsl_mem_entry *entry,
1330 struct kgsl_pagetable *pagetable,
1331 unsigned int fd, unsigned int offset,
1332 size_t size)
1333{
1334 int ret;
1335 unsigned long phys, virt, len;
1336 struct file *filep;
1337
1338 ret = kgsl_get_phys_file(fd, &phys, &len, &virt, &filep);
1339 if (ret)
1340 return ret;
1341
Wei Zou4061c0b2011-07-08 10:24:22 -07001342 if (phys == 0) {
1343 ret = -EINVAL;
1344 goto err;
1345 }
1346
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001347 if (offset >= len) {
1348 ret = -EINVAL;
1349 goto err;
1350 }
1351
1352 if (size == 0)
1353 size = len;
1354
1355 /* Adjust the size of the region to account for the offset */
1356 size += offset & ~PAGE_MASK;
1357
1358 size = ALIGN(size, PAGE_SIZE);
1359
1360 if (_check_region(offset & PAGE_MASK, size, len)) {
1361 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1362 "than pmem region length %ld\n",
1363 offset & PAGE_MASK, size, len);
1364 ret = -EINVAL;
1365 goto err;
1366
1367 }
1368
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001369 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370
1371 entry->memdesc.pagetable = pagetable;
1372 entry->memdesc.size = size;
1373 entry->memdesc.physaddr = phys + (offset & PAGE_MASK);
1374 entry->memdesc.hostptr = (void *) (virt + (offset & PAGE_MASK));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001375
1376 ret = memdesc_sg_phys(&entry->memdesc,
1377 phys + (offset & PAGE_MASK), size);
1378 if (ret)
1379 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380
1381 return 0;
1382err:
Jordan Crousefd978432011-09-02 14:34:32 -06001383#ifdef CONFIG_ANDROID_PMEM
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384 put_pmem_file(filep);
Jordan Crousefd978432011-09-02 14:34:32 -06001385#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 return ret;
1387}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388
Jordan Croused17e9aa2011-10-12 16:57:48 -06001389static int memdesc_sg_virt(struct kgsl_memdesc *memdesc,
1390 void *addr, int size)
1391{
1392 int i;
1393 int sglen = PAGE_ALIGN(size) / PAGE_SIZE;
1394 unsigned long paddr = (unsigned long) addr;
1395
Jeff Boody28afec42012-01-18 15:47:46 -07001396 memdesc->sg = vmalloc(sglen * sizeof(struct scatterlist));
Jordan Croused17e9aa2011-10-12 16:57:48 -06001397 if (memdesc->sg == NULL)
1398 return -ENOMEM;
1399
1400 memdesc->sglen = sglen;
1401 sg_init_table(memdesc->sg, sglen);
1402
1403 spin_lock(&current->mm->page_table_lock);
1404
1405 for (i = 0; i < sglen; i++, paddr += PAGE_SIZE) {
1406 struct page *page;
1407 pmd_t *ppmd;
1408 pte_t *ppte;
1409 pgd_t *ppgd = pgd_offset(current->mm, paddr);
1410
1411 if (pgd_none(*ppgd) || pgd_bad(*ppgd))
1412 goto err;
1413
1414 ppmd = pmd_offset(ppgd, paddr);
1415 if (pmd_none(*ppmd) || pmd_bad(*ppmd))
1416 goto err;
1417
1418 ppte = pte_offset_map(ppmd, paddr);
1419 if (ppte == NULL)
1420 goto err;
1421
1422 page = pfn_to_page(pte_pfn(*ppte));
1423 if (!page)
1424 goto err;
1425
1426 sg_set_page(&memdesc->sg[i], page, PAGE_SIZE, 0);
1427 pte_unmap(ppte);
1428 }
1429
1430 spin_unlock(&current->mm->page_table_lock);
1431
1432 return 0;
1433
1434err:
1435 spin_unlock(&current->mm->page_table_lock);
Jeff Boody28afec42012-01-18 15:47:46 -07001436 vfree(memdesc->sg);
Jordan Croused17e9aa2011-10-12 16:57:48 -06001437 memdesc->sg = NULL;
1438
1439 return -EINVAL;
1440}
1441
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442static int kgsl_setup_hostptr(struct kgsl_mem_entry *entry,
1443 struct kgsl_pagetable *pagetable,
1444 void *hostptr, unsigned int offset,
1445 size_t size)
1446{
1447 struct vm_area_struct *vma;
1448 unsigned int len;
1449
1450 down_read(&current->mm->mmap_sem);
1451 vma = find_vma(current->mm, (unsigned int) hostptr);
1452 up_read(&current->mm->mmap_sem);
1453
1454 if (!vma) {
1455 KGSL_CORE_ERR("find_vma(%p) failed\n", hostptr);
1456 return -EINVAL;
1457 }
1458
1459 /* We don't necessarily start at vma->vm_start */
1460 len = vma->vm_end - (unsigned long) hostptr;
1461
1462 if (offset >= len)
1463 return -EINVAL;
1464
1465 if (!KGSL_IS_PAGE_ALIGNED((unsigned long) hostptr) ||
1466 !KGSL_IS_PAGE_ALIGNED(len)) {
1467 KGSL_CORE_ERR("user address len(%u)"
1468 "and start(%p) must be page"
1469 "aligned\n", len, hostptr);
1470 return -EINVAL;
1471 }
1472
1473 if (size == 0)
1474 size = len;
1475
1476 /* Adjust the size of the region to account for the offset */
1477 size += offset & ~PAGE_MASK;
1478
1479 size = ALIGN(size, PAGE_SIZE);
1480
1481 if (_check_region(offset & PAGE_MASK, size, len)) {
1482 KGSL_CORE_ERR("Offset (%ld) + size (%d) is larger"
1483 "than region length %d\n",
1484 offset & PAGE_MASK, size, len);
1485 return -EINVAL;
1486 }
1487
1488 entry->memdesc.pagetable = pagetable;
1489 entry->memdesc.size = size;
1490 entry->memdesc.hostptr = hostptr + (offset & PAGE_MASK);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491
Jordan Croused17e9aa2011-10-12 16:57:48 -06001492 return memdesc_sg_virt(&entry->memdesc,
1493 hostptr + (offset & PAGE_MASK), size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001494}
1495
1496#ifdef CONFIG_ASHMEM
1497static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1498 struct kgsl_pagetable *pagetable,
1499 int fd, void *hostptr, size_t size)
1500{
1501 int ret;
1502 struct vm_area_struct *vma;
1503 struct file *filep, *vmfile;
1504 unsigned long len;
Jordan Crouse2c542b62011-07-26 08:30:20 -06001505 unsigned int hostaddr = (unsigned int) hostptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506
Jordan Crouse2c542b62011-07-26 08:30:20 -06001507 vma = kgsl_get_vma_from_start_addr(hostaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001508 if (vma == NULL)
1509 return -EINVAL;
1510
Jordan Crouse2c542b62011-07-26 08:30:20 -06001511 if (vma->vm_pgoff || vma->vm_start != hostaddr) {
1512 KGSL_CORE_ERR("Invalid vma region\n");
1513 return -EINVAL;
1514 }
1515
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001516 len = vma->vm_end - vma->vm_start;
1517
1518 if (size == 0)
1519 size = len;
1520
1521 if (size != len) {
1522 KGSL_CORE_ERR("Invalid size %d for vma region %p\n",
1523 size, hostptr);
1524 return -EINVAL;
1525 }
1526
1527 ret = get_ashmem_file(fd, &filep, &vmfile, &len);
1528
1529 if (ret) {
1530 KGSL_CORE_ERR("get_ashmem_file failed\n");
1531 return ret;
1532 }
1533
1534 if (vmfile != vma->vm_file) {
1535 KGSL_CORE_ERR("ashmem shmem file does not match vma\n");
1536 ret = -EINVAL;
1537 goto err;
1538 }
1539
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001540 entry->priv_data = filep;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001541 entry->memdesc.pagetable = pagetable;
1542 entry->memdesc.size = ALIGN(size, PAGE_SIZE);
1543 entry->memdesc.hostptr = hostptr;
Jordan Croused17e9aa2011-10-12 16:57:48 -06001544
1545 ret = memdesc_sg_virt(&entry->memdesc, hostptr, size);
1546 if (ret)
1547 goto err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001548
1549 return 0;
1550
1551err:
1552 put_ashmem_file(filep);
1553 return ret;
1554}
1555#else
1556static int kgsl_setup_ashmem(struct kgsl_mem_entry *entry,
1557 struct kgsl_pagetable *pagetable,
1558 int fd, void *hostptr, size_t size)
1559{
1560 return -EINVAL;
1561}
1562#endif
1563
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001564static int kgsl_setup_ion(struct kgsl_mem_entry *entry,
1565 struct kgsl_pagetable *pagetable, int fd)
1566{
1567 struct ion_handle *handle;
1568 struct scatterlist *s;
1569 unsigned long flags;
1570
1571 if (kgsl_ion_client == NULL) {
1572 kgsl_ion_client = msm_ion_client_create(UINT_MAX, KGSL_NAME);
1573 if (kgsl_ion_client == NULL)
1574 return -ENODEV;
1575 }
1576
1577 handle = ion_import_fd(kgsl_ion_client, fd);
1578 if (IS_ERR_OR_NULL(handle))
1579 return PTR_ERR(handle);
1580
1581 entry->memtype = KGSL_MEM_ENTRY_ION;
1582 entry->priv_data = handle;
1583 entry->memdesc.pagetable = pagetable;
1584 entry->memdesc.size = 0;
1585
1586 if (ion_handle_get_flags(kgsl_ion_client, handle, &flags))
1587 goto err;
1588
1589 entry->memdesc.sg = ion_map_dma(kgsl_ion_client, handle, flags);
1590
1591 if (IS_ERR_OR_NULL(entry->memdesc.sg))
1592 goto err;
1593
1594 /* Calculate the size of the memdesc from the sglist */
1595
1596 entry->memdesc.sglen = 0;
1597
1598 for (s = entry->memdesc.sg; s != NULL; s = sg_next(s)) {
1599 entry->memdesc.size += s->length;
1600 entry->memdesc.sglen++;
1601 }
1602
1603 return 0;
1604err:
1605 ion_free(kgsl_ion_client, handle);
1606 return -ENOMEM;
1607}
1608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001609static long kgsl_ioctl_map_user_mem(struct kgsl_device_private *dev_priv,
1610 unsigned int cmd, void *data)
1611{
1612 int result = -EINVAL;
1613 struct kgsl_map_user_mem *param = data;
1614 struct kgsl_mem_entry *entry = NULL;
1615 struct kgsl_process_private *private = dev_priv->process_priv;
Jason848741a2011-07-12 10:24:25 -07001616 enum kgsl_user_mem_type memtype;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001617
1618 entry = kgsl_mem_entry_create();
1619
1620 if (entry == NULL)
1621 return -ENOMEM;
1622
Jason848741a2011-07-12 10:24:25 -07001623 if (_IOC_SIZE(cmd) == sizeof(struct kgsl_sharedmem_from_pmem))
1624 memtype = KGSL_USER_MEM_TYPE_PMEM;
1625 else
1626 memtype = param->memtype;
1627
1628 switch (memtype) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629 case KGSL_USER_MEM_TYPE_PMEM:
1630 if (param->fd == 0 || param->len == 0)
1631 break;
1632
1633 result = kgsl_setup_phys_file(entry, private->pagetable,
1634 param->fd, param->offset,
1635 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001636 entry->memtype = KGSL_MEM_ENTRY_PMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 break;
1638
1639 case KGSL_USER_MEM_TYPE_ADDR:
1640 if (!kgsl_mmu_enabled()) {
1641 KGSL_DRV_ERR(dev_priv->device,
1642 "Cannot map paged memory with the "
1643 "MMU disabled\n");
1644 break;
1645 }
1646
1647 if (param->hostptr == 0)
1648 break;
1649
1650 result = kgsl_setup_hostptr(entry, private->pagetable,
1651 (void *) param->hostptr,
1652 param->offset, param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001653 entry->memtype = KGSL_MEM_ENTRY_USER;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001654 break;
1655
1656 case KGSL_USER_MEM_TYPE_ASHMEM:
1657 if (!kgsl_mmu_enabled()) {
1658 KGSL_DRV_ERR(dev_priv->device,
1659 "Cannot map paged memory with the "
1660 "MMU disabled\n");
1661 break;
1662 }
1663
1664 if (param->hostptr == 0)
1665 break;
1666
1667 result = kgsl_setup_ashmem(entry, private->pagetable,
1668 param->fd, (void *) param->hostptr,
1669 param->len);
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001670
1671 entry->memtype = KGSL_MEM_ENTRY_ASHMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672 break;
Jordan Crouse8eab35a2011-10-12 16:57:48 -06001673 case KGSL_USER_MEM_TYPE_ION:
1674 result = kgsl_setup_ion(entry, private->pagetable,
1675 param->fd);
1676 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677 default:
Jason848741a2011-07-12 10:24:25 -07001678 KGSL_CORE_ERR("Invalid memory type: %x\n", memtype);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 break;
1680 }
1681
1682 if (result)
1683 goto error;
1684
1685 result = kgsl_mmu_map(private->pagetable,
1686 &entry->memdesc,
1687 GSL_PT_PAGE_RV | GSL_PT_PAGE_WV);
1688
1689 if (result)
1690 goto error_put_file_ptr;
1691
1692 /* Adjust the returned value for a non 4k aligned offset */
1693 param->gpuaddr = entry->memdesc.gpuaddr + (param->offset & ~PAGE_MASK);
1694
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001695 KGSL_STATS_ADD(param->len, kgsl_driver.stats.mapped,
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001696 kgsl_driver.stats.mapped_max);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001697
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001698 kgsl_process_add_stats(private, entry->memtype, param->len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001699
1700 kgsl_mem_entry_attach_process(entry, private);
Jeremy Gebbena5859272012-03-01 12:46:28 -07001701 trace_kgsl_mem_map(entry, param->fd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001702
1703 kgsl_check_idle(dev_priv->device);
1704 return result;
1705
1706 error_put_file_ptr:
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001707 if (entry->priv_data)
1708 fput(entry->priv_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709
1710error:
1711 kfree(entry);
1712 kgsl_check_idle(dev_priv->device);
1713 return result;
1714}
1715
1716/*This function flushes a graphics memory allocation from CPU cache
1717 *when caching is enabled with MMU*/
1718static long
1719kgsl_ioctl_sharedmem_flush_cache(struct kgsl_device_private *dev_priv,
1720 unsigned int cmd, void *data)
1721{
1722 int result = 0;
1723 struct kgsl_mem_entry *entry;
1724 struct kgsl_sharedmem_free *param = data;
1725 struct kgsl_process_private *private = dev_priv->process_priv;
1726
1727 spin_lock(&private->mem_lock);
1728 entry = kgsl_sharedmem_find(private, param->gpuaddr);
1729 if (!entry) {
1730 KGSL_CORE_ERR("invalid gpuaddr %08x\n", param->gpuaddr);
1731 result = -EINVAL;
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001732 goto done;
1733 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001734 if (!entry->memdesc.hostptr) {
1735 KGSL_CORE_ERR("invalid hostptr with gpuaddr %08x\n",
1736 param->gpuaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 goto done;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738 }
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001739
1740 kgsl_cache_range_op(&entry->memdesc, KGSL_CACHE_OP_CLEAN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001741done:
Jeremy Gebben690f9d12011-08-08 16:33:49 -06001742 spin_unlock(&private->mem_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743 return result;
1744}
1745
1746static long
1747kgsl_ioctl_gpumem_alloc(struct kgsl_device_private *dev_priv,
1748 unsigned int cmd, void *data)
1749{
1750 struct kgsl_process_private *private = dev_priv->process_priv;
1751 struct kgsl_gpumem_alloc *param = data;
1752 struct kgsl_mem_entry *entry;
1753 int result;
1754
1755 entry = kgsl_mem_entry_create();
1756 if (entry == NULL)
1757 return -ENOMEM;
1758
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001759 result = kgsl_allocate_user(&entry->memdesc, private->pagetable,
1760 param->size, param->flags);
1761
1762 if (result == 0) {
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001763 entry->memtype = KGSL_MEM_ENTRY_KERNEL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 kgsl_mem_entry_attach_process(entry, private);
1765 param->gpuaddr = entry->memdesc.gpuaddr;
1766
Jordan Crouse1b897cf2011-10-12 16:57:48 -06001767 kgsl_process_add_stats(private, entry->memtype, param->size);
Jeremy Gebbena5859272012-03-01 12:46:28 -07001768 trace_kgsl_mem_alloc(entry);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769 } else
1770 kfree(entry);
1771
1772 kgsl_check_idle(dev_priv->device);
1773 return result;
1774}
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001775static long kgsl_ioctl_cff_syncmem(struct kgsl_device_private *dev_priv,
1776 unsigned int cmd, void *data)
1777{
1778 int result = 0;
1779 struct kgsl_cff_syncmem *param = data;
1780 struct kgsl_process_private *private = dev_priv->process_priv;
1781 struct kgsl_mem_entry *entry = NULL;
1782
1783 spin_lock(&private->mem_lock);
1784 entry = kgsl_sharedmem_find_region(private, param->gpuaddr, param->len);
1785 if (entry)
1786 kgsl_cffdump_syncmem(dev_priv, &entry->memdesc, param->gpuaddr,
1787 param->len, true);
1788 else
1789 result = -EINVAL;
1790 spin_unlock(&private->mem_lock);
1791 return result;
1792}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001794static long kgsl_ioctl_cff_user_event(struct kgsl_device_private *dev_priv,
1795 unsigned int cmd, void *data)
1796{
1797 int result = 0;
1798 struct kgsl_cff_user_event *param = data;
1799
1800 kgsl_cffdump_user_event(param->cff_opcode, param->op1, param->op2,
1801 param->op3, param->op4, param->op5);
1802
1803 return result;
1804}
1805
Jordan Croused4bc9d22011-11-17 13:39:21 -07001806#ifdef CONFIG_GENLOCK
1807struct kgsl_genlock_event_priv {
1808 struct genlock_handle *handle;
1809 struct genlock *lock;
1810};
1811
1812/**
1813 * kgsl_genlock_event_cb - Event callback for a genlock timestamp event
1814 * @device - The KGSL device that expired the timestamp
1815 * @priv - private data for the event
1816 * @timestamp - the timestamp that triggered the event
1817 *
1818 * Release a genlock lock following the expiration of a timestamp
1819 */
1820
1821static void kgsl_genlock_event_cb(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001822 void *priv, u32 timestamp)
Jordan Croused4bc9d22011-11-17 13:39:21 -07001823{
1824 struct kgsl_genlock_event_priv *ev = priv;
1825 int ret;
1826
1827 ret = genlock_lock(ev->handle, GENLOCK_UNLOCK, 0, 0);
1828 if (ret)
1829 KGSL_CORE_ERR("Error while unlocking genlock: %d\n", ret);
1830
1831 genlock_put_handle(ev->handle);
1832
1833 kfree(ev);
1834}
1835
1836/**
1837 * kgsl_add_genlock-event - Create a new genlock event
1838 * @device - KGSL device to create the event on
1839 * @timestamp - Timestamp to trigger the event
1840 * @data - User space buffer containing struct kgsl_genlock_event_priv
1841 * @len - length of the userspace buffer
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -07001842 * @owner - driver instance that owns this event
Jordan Croused4bc9d22011-11-17 13:39:21 -07001843 * @returns 0 on success or error code on error
1844 *
1845 * Attack to a genlock handle and register an event to release the
1846 * genlock lock when the timestamp expires
1847 */
1848
1849static int kgsl_add_genlock_event(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001850 u32 timestamp, void __user *data, int len,
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -07001851 struct kgsl_device_private *owner)
Jordan Croused4bc9d22011-11-17 13:39:21 -07001852{
1853 struct kgsl_genlock_event_priv *event;
1854 struct kgsl_timestamp_event_genlock priv;
1855 int ret;
1856
1857 if (len != sizeof(priv))
1858 return -EINVAL;
1859
1860 if (copy_from_user(&priv, data, sizeof(priv)))
1861 return -EFAULT;
1862
1863 event = kzalloc(sizeof(*event), GFP_KERNEL);
1864
1865 if (event == NULL)
1866 return -ENOMEM;
1867
1868 event->handle = genlock_get_handle_fd(priv.handle);
1869
1870 if (IS_ERR(event->handle)) {
1871 int ret = PTR_ERR(event->handle);
1872 kfree(event);
1873 return ret;
1874 }
1875
Wei Zouc8c01632012-03-24 17:27:26 -07001876 ret = kgsl_add_event(device, timestamp, kgsl_genlock_event_cb, event,
1877 owner);
Jordan Croused4bc9d22011-11-17 13:39:21 -07001878 if (ret)
1879 kfree(event);
1880
1881 return ret;
1882}
1883#else
1884static long kgsl_add_genlock_event(struct kgsl_device *device,
Wei Zouc8c01632012-03-24 17:27:26 -07001885 u32 timestamp, void __user *data, int len,
Jeremy Gebbenfd87f9a2012-02-10 07:06:09 -07001886 struct kgsl_device_private *owner)
Jordan Croused4bc9d22011-11-17 13:39:21 -07001887{
1888 return -EINVAL;
1889}
1890#endif
1891
1892/**
1893 * kgsl_ioctl_timestamp_event - Register a new timestamp event from userspace
1894 * @dev_priv - pointer to the private device structure
1895 * @cmd - the ioctl cmd passed from kgsl_ioctl
1896 * @data - the user data buffer from kgsl_ioctl
1897 * @returns 0 on success or error code on failure
1898 */
1899
1900static long kgsl_ioctl_timestamp_event(struct kgsl_device_private *dev_priv,
1901 unsigned int cmd, void *data)
1902{
1903 struct kgsl_timestamp_event *param = data;
1904 int ret;
1905
1906 switch (param->type) {
1907 case KGSL_TIMESTAMP_EVENT_GENLOCK:
1908 ret = kgsl_add_genlock_event(dev_priv->device,
Wei Zouc8c01632012-03-24 17:27:26 -07001909 param->timestamp, param->priv, param->len,
1910 dev_priv);
Jordan Croused4bc9d22011-11-17 13:39:21 -07001911 break;
1912 default:
1913 ret = -EINVAL;
1914 }
1915
1916 return ret;
1917}
1918
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919typedef long (*kgsl_ioctl_func_t)(struct kgsl_device_private *,
1920 unsigned int, void *);
1921
1922#define KGSL_IOCTL_FUNC(_cmd, _func, _lock) \
1923 [_IOC_NR(_cmd)] = { .cmd = _cmd, .func = _func, .lock = _lock }
1924
1925static const struct {
1926 unsigned int cmd;
1927 kgsl_ioctl_func_t func;
1928 int lock;
1929} kgsl_ioctl_funcs[] = {
1930 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_GETPROPERTY,
1931 kgsl_ioctl_device_getproperty, 1),
1932 KGSL_IOCTL_FUNC(IOCTL_KGSL_DEVICE_WAITTIMESTAMP,
1933 kgsl_ioctl_device_waittimestamp, 1),
1934 KGSL_IOCTL_FUNC(IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS,
1935 kgsl_ioctl_rb_issueibcmds, 1),
1936 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_READTIMESTAMP,
1937 kgsl_ioctl_cmdstream_readtimestamp, 1),
1938 KGSL_IOCTL_FUNC(IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP,
1939 kgsl_ioctl_cmdstream_freememontimestamp, 1),
1940 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_CREATE,
1941 kgsl_ioctl_drawctxt_create, 1),
1942 KGSL_IOCTL_FUNC(IOCTL_KGSL_DRAWCTXT_DESTROY,
1943 kgsl_ioctl_drawctxt_destroy, 1),
1944 KGSL_IOCTL_FUNC(IOCTL_KGSL_MAP_USER_MEM,
1945 kgsl_ioctl_map_user_mem, 0),
1946 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_PMEM,
1947 kgsl_ioctl_map_user_mem, 0),
1948 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FREE,
1949 kgsl_ioctl_sharedmem_free, 0),
1950 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FROM_VMALLOC,
1951 kgsl_ioctl_sharedmem_from_vmalloc, 0),
1952 KGSL_IOCTL_FUNC(IOCTL_KGSL_SHAREDMEM_FLUSH_CACHE,
1953 kgsl_ioctl_sharedmem_flush_cache, 0),
1954 KGSL_IOCTL_FUNC(IOCTL_KGSL_GPUMEM_ALLOC,
1955 kgsl_ioctl_gpumem_alloc, 0),
Jeremy Gebbena7423e42011-04-18 15:11:21 -06001956 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_SYNCMEM,
1957 kgsl_ioctl_cff_syncmem, 0),
Sushmita Susheelendra41f8fa32011-05-11 17:15:58 -06001958 KGSL_IOCTL_FUNC(IOCTL_KGSL_CFF_USER_EVENT,
1959 kgsl_ioctl_cff_user_event, 0),
Jordan Croused4bc9d22011-11-17 13:39:21 -07001960 KGSL_IOCTL_FUNC(IOCTL_KGSL_TIMESTAMP_EVENT,
Lucille Sylvester9329cf02011-12-02 14:30:41 -07001961 kgsl_ioctl_timestamp_event, 1),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962};
1963
1964static long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1965{
1966 struct kgsl_device_private *dev_priv = filep->private_data;
1967 unsigned int nr = _IOC_NR(cmd);
1968 kgsl_ioctl_func_t func;
1969 int lock, ret;
1970 char ustack[64];
1971 void *uptr = NULL;
1972
1973 BUG_ON(dev_priv == NULL);
1974
1975 /* Workaround for an previously incorrectly defined ioctl code.
1976 This helps ensure binary compatability */
1977
1978 if (cmd == IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP_OLD)
1979 cmd = IOCTL_KGSL_CMDSTREAM_FREEMEMONTIMESTAMP;
Jason Varbedian80ba33d2011-07-11 17:29:05 -07001980 else if (cmd == IOCTL_KGSL_CMDSTREAM_READTIMESTAMP_OLD)
1981 cmd = IOCTL_KGSL_CMDSTREAM_READTIMESTAMP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001982
1983 if (cmd & (IOC_IN | IOC_OUT)) {
1984 if (_IOC_SIZE(cmd) < sizeof(ustack))
1985 uptr = ustack;
1986 else {
1987 uptr = kzalloc(_IOC_SIZE(cmd), GFP_KERNEL);
1988 if (uptr == NULL) {
1989 KGSL_MEM_ERR(dev_priv->device,
1990 "kzalloc(%d) failed\n", _IOC_SIZE(cmd));
1991 ret = -ENOMEM;
1992 goto done;
1993 }
1994 }
1995
1996 if (cmd & IOC_IN) {
1997 if (copy_from_user(uptr, (void __user *) arg,
1998 _IOC_SIZE(cmd))) {
1999 ret = -EFAULT;
2000 goto done;
2001 }
2002 } else
2003 memset(uptr, 0, _IOC_SIZE(cmd));
2004 }
2005
2006 if (nr < ARRAY_SIZE(kgsl_ioctl_funcs) &&
2007 kgsl_ioctl_funcs[nr].func != NULL) {
2008 func = kgsl_ioctl_funcs[nr].func;
2009 lock = kgsl_ioctl_funcs[nr].lock;
2010 } else {
2011 func = dev_priv->device->ftbl->ioctl;
2012 if (!func) {
2013 KGSL_DRV_INFO(dev_priv->device,
2014 "invalid ioctl code %08x\n", cmd);
Jeremy Gebbenc15b4612012-01-09 09:44:11 -07002015 ret = -ENOIOCTLCMD;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 goto done;
2017 }
2018 lock = 1;
2019 }
2020
2021 if (lock) {
2022 mutex_lock(&dev_priv->device->mutex);
2023 kgsl_check_suspended(dev_priv->device);
2024 }
2025
2026 ret = func(dev_priv, cmd, uptr);
2027
2028 if (lock) {
2029 kgsl_check_idle_locked(dev_priv->device);
2030 mutex_unlock(&dev_priv->device->mutex);
2031 }
2032
2033 if (ret == 0 && (cmd & IOC_OUT)) {
2034 if (copy_to_user((void __user *) arg, uptr, _IOC_SIZE(cmd)))
2035 ret = -EFAULT;
2036 }
2037
2038done:
2039 if (_IOC_SIZE(cmd) >= sizeof(ustack))
2040 kfree(uptr);
2041
2042 return ret;
2043}
2044
2045static int
2046kgsl_mmap_memstore(struct kgsl_device *device, struct vm_area_struct *vma)
2047{
2048 struct kgsl_memdesc *memdesc = &device->memstore;
2049 int result;
2050 unsigned int vma_size = vma->vm_end - vma->vm_start;
2051
2052 /* The memstore can only be mapped as read only */
2053
2054 if (vma->vm_flags & VM_WRITE)
2055 return -EPERM;
2056
2057 if (memdesc->size != vma_size) {
2058 KGSL_MEM_ERR(device, "memstore bad size: %d should be %d\n",
2059 vma_size, memdesc->size);
2060 return -EINVAL;
2061 }
2062
2063 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2064
2065 result = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
2066 vma_size, vma->vm_page_prot);
2067 if (result != 0)
2068 KGSL_MEM_ERR(device, "remap_pfn_range failed: %d\n",
2069 result);
2070
2071 return result;
2072}
2073
Jordan Crouse4283e172011-09-26 14:45:47 -06002074/*
2075 * kgsl_gpumem_vm_open is called whenever a vma region is copied or split.
2076 * Increase the refcount to make sure that the accounting stays correct
2077 */
2078
2079static void kgsl_gpumem_vm_open(struct vm_area_struct *vma)
2080{
2081 struct kgsl_mem_entry *entry = vma->vm_private_data;
2082 kgsl_mem_entry_get(entry);
2083}
2084
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085static int
2086kgsl_gpumem_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2087{
2088 struct kgsl_mem_entry *entry = vma->vm_private_data;
2089
Jordan Croused17e9aa2011-10-12 16:57:48 -06002090 if (!entry->memdesc.ops || !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 return VM_FAULT_SIGBUS;
2092
2093 return entry->memdesc.ops->vmfault(&entry->memdesc, vma, vmf);
2094}
2095
2096static void
2097kgsl_gpumem_vm_close(struct vm_area_struct *vma)
2098{
2099 struct kgsl_mem_entry *entry = vma->vm_private_data;
2100 kgsl_mem_entry_put(entry);
2101}
2102
2103static struct vm_operations_struct kgsl_gpumem_vm_ops = {
Jordan Crouse4283e172011-09-26 14:45:47 -06002104 .open = kgsl_gpumem_vm_open,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002105 .fault = kgsl_gpumem_vm_fault,
2106 .close = kgsl_gpumem_vm_close,
2107};
2108
2109static int kgsl_mmap(struct file *file, struct vm_area_struct *vma)
2110{
2111 unsigned long vma_offset = vma->vm_pgoff << PAGE_SHIFT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002112 struct kgsl_device_private *dev_priv = file->private_data;
2113 struct kgsl_process_private *private = dev_priv->process_priv;
Jordan Crouse976cf0e2011-09-12 10:41:49 -06002114 struct kgsl_mem_entry *tmp, *entry = NULL;
Jordan Crouse2db0af92011-08-08 16:05:09 -06002115 struct kgsl_device *device = dev_priv->device;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002116
2117 /* Handle leagacy behavior for memstore */
2118
2119 if (vma_offset == device->memstore.physaddr)
2120 return kgsl_mmap_memstore(device, vma);
2121
2122 /* Find a chunk of GPU memory */
2123
2124 spin_lock(&private->mem_lock);
Jordan Crouse976cf0e2011-09-12 10:41:49 -06002125 list_for_each_entry(tmp, &private->mem_list, list) {
2126 if (vma_offset == tmp->memdesc.gpuaddr) {
2127 kgsl_mem_entry_get(tmp);
2128 entry = tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002129 break;
2130 }
2131 }
2132 spin_unlock(&private->mem_lock);
2133
2134 if (entry == NULL)
2135 return -EINVAL;
2136
Jordan Croused17e9aa2011-10-12 16:57:48 -06002137 if (!entry->memdesc.ops ||
2138 !entry->memdesc.ops->vmflags ||
2139 !entry->memdesc.ops->vmfault)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002140 return -EINVAL;
2141
2142 vma->vm_flags |= entry->memdesc.ops->vmflags(&entry->memdesc);
2143
2144 vma->vm_private_data = entry;
2145 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
2146 vma->vm_ops = &kgsl_gpumem_vm_ops;
2147 vma->vm_file = file;
2148
2149 return 0;
2150}
2151
2152static const struct file_operations kgsl_fops = {
2153 .owner = THIS_MODULE,
2154 .release = kgsl_release,
2155 .open = kgsl_open,
2156 .mmap = kgsl_mmap,
2157 .unlocked_ioctl = kgsl_ioctl,
2158};
2159
2160struct kgsl_driver kgsl_driver = {
2161 .process_mutex = __MUTEX_INITIALIZER(kgsl_driver.process_mutex),
2162 .ptlock = __SPIN_LOCK_UNLOCKED(kgsl_driver.ptlock),
2163 .devlock = __MUTEX_INITIALIZER(kgsl_driver.devlock),
2164};
2165EXPORT_SYMBOL(kgsl_driver);
2166
2167void kgsl_unregister_device(struct kgsl_device *device)
2168{
2169 int minor;
2170
2171 mutex_lock(&kgsl_driver.devlock);
2172 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2173 if (device == kgsl_driver.devp[minor])
2174 break;
2175 }
2176
2177 mutex_unlock(&kgsl_driver.devlock);
2178
2179 if (minor == KGSL_DEVICE_MAX)
2180 return;
2181
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002182 kgsl_device_snapshot_close(device);
2183
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184 kgsl_cffdump_close(device->id);
2185 kgsl_pwrctrl_uninit_sysfs(device);
2186
Lucille Sylvester10297892012-02-27 13:54:47 -07002187 wake_lock_destroy(&device->idle_wakelock);
2188 pm_qos_remove_request(&device->pm_qos_req_dma);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002189
2190 idr_destroy(&device->context_idr);
2191
2192 if (device->memstore.hostptr)
2193 kgsl_sharedmem_free(&device->memstore);
2194
2195 kgsl_mmu_close(device);
2196
2197 if (device->work_queue) {
2198 destroy_workqueue(device->work_queue);
2199 device->work_queue = NULL;
2200 }
2201
2202 device_destroy(kgsl_driver.class,
2203 MKDEV(MAJOR(kgsl_driver.major), minor));
2204
2205 mutex_lock(&kgsl_driver.devlock);
2206 kgsl_driver.devp[minor] = NULL;
2207 mutex_unlock(&kgsl_driver.devlock);
2208}
2209EXPORT_SYMBOL(kgsl_unregister_device);
2210
2211int
2212kgsl_register_device(struct kgsl_device *device)
2213{
2214 int minor, ret;
2215 dev_t dev;
2216
2217 /* Find a minor for the device */
2218
2219 mutex_lock(&kgsl_driver.devlock);
2220 for (minor = 0; minor < KGSL_DEVICE_MAX; minor++) {
2221 if (kgsl_driver.devp[minor] == NULL) {
2222 kgsl_driver.devp[minor] = device;
2223 break;
2224 }
2225 }
2226
2227 mutex_unlock(&kgsl_driver.devlock);
2228
2229 if (minor == KGSL_DEVICE_MAX) {
2230 KGSL_CORE_ERR("minor devices exhausted\n");
2231 return -ENODEV;
2232 }
2233
2234 /* Create the device */
2235 dev = MKDEV(MAJOR(kgsl_driver.major), minor);
2236 device->dev = device_create(kgsl_driver.class,
2237 device->parentdev,
2238 dev, device,
2239 device->name);
2240
2241 if (IS_ERR(device->dev)) {
2242 ret = PTR_ERR(device->dev);
2243 KGSL_CORE_ERR("device_create(%s): %d\n", device->name, ret);
2244 goto err_devlist;
2245 }
2246
2247 dev_set_drvdata(device->parentdev, device);
2248
2249 /* Generic device initialization */
2250 init_waitqueue_head(&device->wait_queue);
2251
2252 kgsl_cffdump_open(device->id);
2253
2254 init_completion(&device->hwaccess_gate);
2255 init_completion(&device->suspend_gate);
2256
2257 ATOMIC_INIT_NOTIFIER_HEAD(&device->ts_notifier_list);
2258
2259 setup_timer(&device->idle_timer, kgsl_timer, (unsigned long) device);
2260 ret = kgsl_create_device_workqueue(device);
2261 if (ret)
2262 goto err_devlist;
2263
2264 INIT_WORK(&device->idle_check_ws, kgsl_idle_check);
Jordan Crouse1bf80aa2011-10-12 16:57:47 -06002265 INIT_WORK(&device->ts_expired_ws, kgsl_timestamp_expired);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002266
Jordan Croused4bc9d22011-11-17 13:39:21 -07002267 INIT_LIST_HEAD(&device->events);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002268
2269 ret = kgsl_mmu_init(device);
2270 if (ret != 0)
2271 goto err_dest_work_q;
2272
Wei Zouc8c01632012-03-24 17:27:26 -07002273 ret = kgsl_allocate_contiguous(&device->memstore,
2274 sizeof(struct kgsl_devmemstore));
2275
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002276 if (ret != 0)
2277 goto err_close_mmu;
2278
Lucille Sylvester10297892012-02-27 13:54:47 -07002279 wake_lock_init(&device->idle_wakelock, WAKE_LOCK_IDLE, device->name);
2280 pm_qos_add_request(&device->pm_qos_req_dma, PM_QOS_CPU_DMA_LATENCY,
2281 PM_QOS_DEFAULT_VALUE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282
2283 idr_init(&device->context_idr);
2284
Jordan Crouse156cfbc2012-01-24 09:32:04 -07002285 /* Initalize the snapshot engine */
2286 kgsl_device_snapshot_init(device);
2287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002288 /* sysfs and debugfs initalization - failure here is non fatal */
2289
2290 /* Initialize logging */
2291 kgsl_device_debugfs_init(device);
2292
2293 /* Initialize common sysfs entries */
2294 kgsl_pwrctrl_init_sysfs(device);
2295
2296 return 0;
2297
2298err_close_mmu:
2299 kgsl_mmu_close(device);
2300err_dest_work_q:
2301 destroy_workqueue(device->work_queue);
2302 device->work_queue = NULL;
2303err_devlist:
2304 mutex_lock(&kgsl_driver.devlock);
2305 kgsl_driver.devp[minor] = NULL;
2306 mutex_unlock(&kgsl_driver.devlock);
2307
2308 return ret;
2309}
2310EXPORT_SYMBOL(kgsl_register_device);
2311
2312int kgsl_device_platform_probe(struct kgsl_device *device,
2313 irqreturn_t (*dev_isr) (int, void*))
2314{
Michael Street8bacdd02012-01-05 14:55:01 -08002315 int result;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002316 int status = -EINVAL;
2317 struct kgsl_memregion *regspace = NULL;
2318 struct resource *res;
2319 struct platform_device *pdev =
2320 container_of(device->parentdev, struct platform_device, dev);
2321
2322 pm_runtime_enable(device->parentdev);
2323
2324 status = kgsl_pwrctrl_init(device);
2325 if (status)
2326 goto error;
2327
2328 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2329 device->iomemname);
2330 if (res == NULL) {
2331 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
2332 status = -EINVAL;
2333 goto error_pwrctrl_close;
2334 }
2335 if (res->start == 0 || resource_size(res) == 0) {
2336 KGSL_DRV_ERR(device, "dev %d invalid regspace\n", device->id);
2337 status = -EINVAL;
2338 goto error_pwrctrl_close;
2339 }
2340
2341 regspace = &device->regspace;
2342 regspace->mmio_phys_base = res->start;
2343 regspace->sizebytes = resource_size(res);
2344
2345 if (!request_mem_region(regspace->mmio_phys_base,
2346 regspace->sizebytes, device->name)) {
2347 KGSL_DRV_ERR(device, "request_mem_region failed\n");
2348 status = -ENODEV;
2349 goto error_pwrctrl_close;
2350 }
2351
2352 regspace->mmio_virt_base = ioremap(regspace->mmio_phys_base,
2353 regspace->sizebytes);
2354
2355 if (regspace->mmio_virt_base == NULL) {
2356 KGSL_DRV_ERR(device, "ioremap failed\n");
2357 status = -ENODEV;
2358 goto error_release_mem;
2359 }
2360
2361 status = request_irq(device->pwrctrl.interrupt_num, dev_isr,
2362 IRQF_TRIGGER_HIGH, device->name, device);
2363 if (status) {
2364 KGSL_DRV_ERR(device, "request_irq(%d) failed: %d\n",
2365 device->pwrctrl.interrupt_num, status);
2366 goto error_iounmap;
2367 }
2368 device->pwrctrl.have_irq = 1;
2369 disable_irq(device->pwrctrl.interrupt_num);
2370
2371 KGSL_DRV_INFO(device,
2372 "dev_id %d regs phys 0x%08x size 0x%08x virt %p\n",
2373 device->id, regspace->mmio_phys_base,
2374 regspace->sizebytes, regspace->mmio_virt_base);
2375
Michael Street8bacdd02012-01-05 14:55:01 -08002376 result = kgsl_drm_init(pdev);
2377 if (result)
2378 goto error_iounmap;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002379
2380 status = kgsl_register_device(device);
2381 if (!status)
2382 return status;
2383
2384 free_irq(device->pwrctrl.interrupt_num, NULL);
2385 device->pwrctrl.have_irq = 0;
2386error_iounmap:
2387 iounmap(regspace->mmio_virt_base);
2388 regspace->mmio_virt_base = NULL;
2389error_release_mem:
2390 release_mem_region(regspace->mmio_phys_base, regspace->sizebytes);
2391error_pwrctrl_close:
2392 kgsl_pwrctrl_close(device);
2393error:
2394 return status;
2395}
2396EXPORT_SYMBOL(kgsl_device_platform_probe);
2397
2398void kgsl_device_platform_remove(struct kgsl_device *device)
2399{
2400 struct kgsl_memregion *regspace = &device->regspace;
2401
2402 kgsl_unregister_device(device);
2403
2404 if (regspace->mmio_virt_base != NULL) {
2405 iounmap(regspace->mmio_virt_base);
2406 regspace->mmio_virt_base = NULL;
2407 release_mem_region(regspace->mmio_phys_base,
2408 regspace->sizebytes);
2409 }
2410 kgsl_pwrctrl_close(device);
2411
2412 pm_runtime_disable(device->parentdev);
2413}
2414EXPORT_SYMBOL(kgsl_device_platform_remove);
2415
2416static int __devinit
2417kgsl_ptdata_init(void)
2418{
Jordan Crouse6d76c4d2012-03-26 09:50:43 -06002419 kgsl_driver.ptpool = kgsl_mmu_ptpool_init(kgsl_pagetable_count);
2420
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002421 if (!kgsl_driver.ptpool)
2422 return -ENOMEM;
2423 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002424}
2425
2426static void kgsl_core_exit(void)
2427{
2428 unregister_chrdev_region(kgsl_driver.major, KGSL_DEVICE_MAX);
2429
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002430 kgsl_mmu_ptpool_destroy(&kgsl_driver.ptpool);
2431 kgsl_driver.ptpool = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002432
2433 device_unregister(&kgsl_driver.virtdev);
2434
2435 if (kgsl_driver.class) {
2436 class_destroy(kgsl_driver.class);
2437 kgsl_driver.class = NULL;
2438 }
2439
2440 kgsl_drm_exit();
2441 kgsl_cffdump_destroy();
Jordan Croused8f1c6b2011-10-04 09:31:29 -06002442 kgsl_core_debugfs_close();
2443 kgsl_sharedmem_uninit_sysfs();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002444}
2445
2446static int __init kgsl_core_init(void)
2447{
2448 int result = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002449 /* alloc major and minor device numbers */
2450 result = alloc_chrdev_region(&kgsl_driver.major, 0, KGSL_DEVICE_MAX,
2451 KGSL_NAME);
2452 if (result < 0) {
2453 KGSL_CORE_ERR("alloc_chrdev_region failed err = %d\n", result);
2454 goto err;
2455 }
2456
2457 cdev_init(&kgsl_driver.cdev, &kgsl_fops);
2458 kgsl_driver.cdev.owner = THIS_MODULE;
2459 kgsl_driver.cdev.ops = &kgsl_fops;
2460 result = cdev_add(&kgsl_driver.cdev, MKDEV(MAJOR(kgsl_driver.major), 0),
2461 KGSL_DEVICE_MAX);
2462
2463 if (result) {
2464 KGSL_CORE_ERR("kgsl: cdev_add() failed, dev_num= %d,"
2465 " result= %d\n", kgsl_driver.major, result);
2466 goto err;
2467 }
2468
2469 kgsl_driver.class = class_create(THIS_MODULE, KGSL_NAME);
2470
2471 if (IS_ERR(kgsl_driver.class)) {
2472 result = PTR_ERR(kgsl_driver.class);
2473 KGSL_CORE_ERR("failed to create class %s", KGSL_NAME);
2474 goto err;
2475 }
2476
2477 /* Make a virtual device for managing core related things
2478 in sysfs */
2479 kgsl_driver.virtdev.class = kgsl_driver.class;
2480 dev_set_name(&kgsl_driver.virtdev, "kgsl");
2481 result = device_register(&kgsl_driver.virtdev);
2482 if (result) {
2483 KGSL_CORE_ERR("driver_register failed\n");
2484 goto err;
2485 }
2486
2487 /* Make kobjects in the virtual device for storing statistics */
2488
2489 kgsl_driver.ptkobj =
2490 kobject_create_and_add("pagetables",
2491 &kgsl_driver.virtdev.kobj);
2492
2493 kgsl_driver.prockobj =
2494 kobject_create_and_add("proc",
2495 &kgsl_driver.virtdev.kobj);
2496
2497 kgsl_core_debugfs_init();
2498
2499 kgsl_sharedmem_init_sysfs();
2500 kgsl_cffdump_init();
2501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002502 INIT_LIST_HEAD(&kgsl_driver.process_list);
2503
Shubhraprakash Das767fdda2011-08-15 15:49:45 -06002504 INIT_LIST_HEAD(&kgsl_driver.pagetable_list);
2505
2506 kgsl_mmu_set_mmutype(ksgl_mmu_type);
2507
2508 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype()) {
2509 result = kgsl_ptdata_init();
2510 if (result)
2511 goto err;
2512 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002514 return 0;
2515
2516err:
2517 kgsl_core_exit();
2518 return result;
2519}
2520
2521module_init(kgsl_core_init);
2522module_exit(kgsl_core_exit);
2523
2524MODULE_AUTHOR("Qualcomm Innovation Center, Inc.");
2525MODULE_DESCRIPTION("MSM GPU driver");
2526MODULE_LICENSE("GPL");